]>
Commit | Line | Data |
---|---|---|
10caabda OS |
1 | // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
2 | // Copyright (c) 2019 Mellanox Technologies. | |
3 | ||
4 | #include <linux/mlx5/fs.h> | |
5 | #include "eswitch.h" | |
6 | ||
7 | struct mlx5_termtbl_handle { | |
8 | struct hlist_node termtbl_hlist; | |
9 | ||
10 | struct mlx5_flow_table *termtbl; | |
11 | struct mlx5_flow_act flow_act; | |
12 | struct mlx5_flow_destination dest; | |
13 | ||
14 | struct mlx5_flow_handle *rule; | |
15 | int ref_count; | |
16 | }; | |
17 | ||
18 | static u32 | |
19 | mlx5_eswitch_termtbl_hash(struct mlx5_flow_act *flow_act, | |
20 | struct mlx5_flow_destination *dest) | |
21 | { | |
22 | u32 hash; | |
23 | ||
24 | hash = jhash_1word(flow_act->action, 0); | |
25 | hash = jhash((const void *)&flow_act->vlan, | |
26 | sizeof(flow_act->vlan), hash); | |
27 | hash = jhash((const void *)&dest->vport.num, | |
28 | sizeof(dest->vport.num), hash); | |
29 | hash = jhash((const void *)&dest->vport.vhca_id, | |
30 | sizeof(dest->vport.num), hash); | |
31 | return hash; | |
32 | } | |
33 | ||
34 | static int | |
35 | mlx5_eswitch_termtbl_cmp(struct mlx5_flow_act *flow_act1, | |
36 | struct mlx5_flow_destination *dest1, | |
37 | struct mlx5_flow_act *flow_act2, | |
38 | struct mlx5_flow_destination *dest2) | |
39 | { | |
40 | return flow_act1->action != flow_act2->action || | |
41 | dest1->vport.num != dest2->vport.num || | |
42 | dest1->vport.vhca_id != dest2->vport.vhca_id || | |
43 | memcmp(&flow_act1->vlan, &flow_act2->vlan, | |
44 | sizeof(flow_act1->vlan)); | |
45 | } | |
46 | ||
47 | static int | |
48 | mlx5_eswitch_termtbl_create(struct mlx5_core_dev *dev, | |
49 | struct mlx5_termtbl_handle *tt, | |
50 | struct mlx5_flow_act *flow_act) | |
51 | { | |
5233794b | 52 | static const struct mlx5_flow_spec spec = {}; |
10caabda | 53 | struct mlx5_flow_namespace *root_ns; |
10caabda OS |
54 | int prio, flags; |
55 | int err; | |
56 | ||
57 | root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB); | |
58 | if (!root_ns) { | |
59 | esw_warn(dev, "Failed to get FDB flow namespace\n"); | |
60 | return -EOPNOTSUPP; | |
61 | } | |
62 | ||
63 | /* As this is the terminating action then the termination table is the | |
64 | * same prio as the slow path | |
65 | */ | |
66 | prio = FDB_SLOW_PATH; | |
67 | flags = MLX5_FLOW_TABLE_TERMINATION; | |
68 | tt->termtbl = mlx5_create_auto_grouped_flow_table(root_ns, prio, 1, 1, | |
69 | 0, flags); | |
70 | if (IS_ERR(tt->termtbl)) { | |
71 | esw_warn(dev, "Failed to create termination table\n"); | |
72 | return -EOPNOTSUPP; | |
73 | } | |
74 | ||
75 | tt->rule = mlx5_add_flow_rules(tt->termtbl, &spec, flow_act, | |
76 | &tt->dest, 1); | |
77 | ||
78 | if (IS_ERR(tt->rule)) { | |
79 | esw_warn(dev, "Failed to create termination table rule\n"); | |
80 | goto add_flow_err; | |
81 | } | |
82 | return 0; | |
83 | ||
84 | add_flow_err: | |
85 | err = mlx5_destroy_flow_table(tt->termtbl); | |
86 | if (err) | |
87 | esw_warn(dev, "Failed to destroy termination table\n"); | |
88 | ||
89 | return -EOPNOTSUPP; | |
90 | } | |
91 | ||
92 | static struct mlx5_termtbl_handle * | |
93 | mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch *esw, | |
94 | struct mlx5_flow_act *flow_act, | |
95 | struct mlx5_flow_destination *dest) | |
96 | { | |
97 | struct mlx5_termtbl_handle *tt; | |
98 | bool found = false; | |
99 | u32 hash_key; | |
100 | int err; | |
101 | ||
102 | mutex_lock(&esw->offloads.termtbl_mutex); | |
103 | ||
104 | hash_key = mlx5_eswitch_termtbl_hash(flow_act, dest); | |
105 | hash_for_each_possible(esw->offloads.termtbl_tbl, tt, | |
106 | termtbl_hlist, hash_key) { | |
107 | if (!mlx5_eswitch_termtbl_cmp(&tt->flow_act, &tt->dest, | |
108 | flow_act, dest)) { | |
109 | found = true; | |
110 | break; | |
111 | } | |
112 | } | |
113 | if (found) | |
114 | goto tt_add_ref; | |
115 | ||
116 | tt = kzalloc(sizeof(*tt), GFP_KERNEL); | |
117 | if (!tt) { | |
118 | err = -ENOMEM; | |
119 | goto tt_create_err; | |
120 | } | |
121 | ||
122 | tt->dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT; | |
123 | tt->dest.vport.num = dest->vport.num; | |
124 | tt->dest.vport.vhca_id = dest->vport.vhca_id; | |
125 | memcpy(&tt->flow_act, flow_act, sizeof(*flow_act)); | |
126 | ||
127 | err = mlx5_eswitch_termtbl_create(esw->dev, tt, flow_act); | |
128 | if (err) { | |
129 | esw_warn(esw->dev, "Failed to create termination table\n"); | |
130 | goto tt_create_err; | |
131 | } | |
132 | hash_add(esw->offloads.termtbl_tbl, &tt->termtbl_hlist, hash_key); | |
133 | tt_add_ref: | |
134 | tt->ref_count++; | |
135 | mutex_unlock(&esw->offloads.termtbl_mutex); | |
136 | return tt; | |
137 | tt_create_err: | |
138 | kfree(tt); | |
139 | mutex_unlock(&esw->offloads.termtbl_mutex); | |
140 | return ERR_PTR(err); | |
141 | } | |
142 | ||
143 | void | |
144 | mlx5_eswitch_termtbl_put(struct mlx5_eswitch *esw, | |
145 | struct mlx5_termtbl_handle *tt) | |
146 | { | |
147 | mutex_lock(&esw->offloads.termtbl_mutex); | |
148 | if (--tt->ref_count == 0) | |
149 | hash_del(&tt->termtbl_hlist); | |
150 | mutex_unlock(&esw->offloads.termtbl_mutex); | |
151 | ||
152 | if (!tt->ref_count) { | |
153 | mlx5_del_flow_rules(tt->rule); | |
154 | mlx5_destroy_flow_table(tt->termtbl); | |
155 | kfree(tt); | |
156 | } | |
157 | } | |
158 | ||
159 | static void | |
160 | mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act *src, | |
161 | struct mlx5_flow_act *dst) | |
162 | { | |
163 | if (!(src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)) | |
164 | return; | |
165 | ||
166 | src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH; | |
167 | dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH; | |
168 | memcpy(&dst->vlan[0], &src->vlan[0], sizeof(src->vlan[0])); | |
169 | memset(&src->vlan[0], 0, sizeof(src->vlan[0])); | |
170 | ||
171 | if (!(src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2)) | |
172 | return; | |
173 | ||
174 | src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2; | |
175 | dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2; | |
176 | memcpy(&dst->vlan[1], &src->vlan[1], sizeof(src->vlan[1])); | |
177 | memset(&src->vlan[1], 0, sizeof(src->vlan[1])); | |
178 | } | |
179 | ||
180 | bool | |
181 | mlx5_eswitch_termtbl_required(struct mlx5_eswitch *esw, | |
182 | struct mlx5_flow_act *flow_act, | |
183 | struct mlx5_flow_spec *spec) | |
184 | { | |
185 | u32 port_mask = MLX5_GET(fte_match_param, spec->match_criteria, | |
186 | misc_parameters.source_port); | |
187 | u32 port_value = MLX5_GET(fte_match_param, spec->match_value, | |
188 | misc_parameters.source_port); | |
189 | ||
190 | if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, termination_table)) | |
191 | return false; | |
192 | ||
193 | /* push vlan on RX */ | |
194 | return (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) && | |
195 | ((port_mask & port_value) == MLX5_VPORT_UPLINK); | |
196 | } | |
197 | ||
198 | struct mlx5_flow_handle * | |
199 | mlx5_eswitch_add_termtbl_rule(struct mlx5_eswitch *esw, | |
200 | struct mlx5_flow_table *fdb, | |
201 | struct mlx5_flow_spec *spec, | |
202 | struct mlx5_esw_flow_attr *attr, | |
203 | struct mlx5_flow_act *flow_act, | |
204 | struct mlx5_flow_destination *dest, | |
205 | int num_dest) | |
206 | { | |
207 | struct mlx5_flow_act term_tbl_act = {}; | |
208 | struct mlx5_flow_handle *rule = NULL; | |
209 | bool term_table_created = false; | |
210 | int num_vport_dests = 0; | |
211 | int i, curr_dest; | |
212 | ||
213 | mlx5_eswitch_termtbl_actions_move(flow_act, &term_tbl_act); | |
214 | term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST; | |
215 | ||
216 | for (i = 0; i < num_dest; i++) { | |
217 | struct mlx5_termtbl_handle *tt; | |
218 | ||
219 | /* only vport destinations can be terminated */ | |
220 | if (dest[i].type != MLX5_FLOW_DESTINATION_TYPE_VPORT) | |
221 | continue; | |
222 | ||
223 | /* get the terminating table for the action list */ | |
224 | tt = mlx5_eswitch_termtbl_get_create(esw, &term_tbl_act, | |
225 | &dest[i]); | |
226 | if (IS_ERR(tt)) { | |
227 | esw_warn(esw->dev, "Failed to create termination table\n"); | |
228 | goto revert_changes; | |
229 | } | |
230 | attr->dests[num_vport_dests].termtbl = tt; | |
231 | num_vport_dests++; | |
232 | ||
233 | /* link the destination with the termination table */ | |
234 | dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE; | |
235 | dest[i].ft = tt->termtbl; | |
236 | term_table_created = true; | |
237 | } | |
238 | ||
239 | /* at least one destination should reference a termination table */ | |
240 | if (!term_table_created) | |
241 | goto revert_changes; | |
242 | ||
243 | /* create the FTE */ | |
244 | rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest); | |
245 | if (IS_ERR(rule)) | |
246 | goto revert_changes; | |
247 | ||
248 | goto out; | |
249 | ||
250 | revert_changes: | |
251 | /* revert the changes that were made to the original flow_act | |
252 | * and fall-back to the original rule actions | |
253 | */ | |
254 | mlx5_eswitch_termtbl_actions_move(&term_tbl_act, flow_act); | |
255 | ||
256 | for (curr_dest = 0; curr_dest < num_vport_dests; curr_dest++) { | |
257 | struct mlx5_termtbl_handle *tt = attr->dests[curr_dest].termtbl; | |
258 | ||
259 | /* search for the destination associated with the | |
260 | * current term table | |
261 | */ | |
262 | for (i = 0; i < num_dest; i++) { | |
263 | if (dest[i].ft != tt->termtbl) | |
264 | continue; | |
265 | ||
266 | memset(&dest[i], 0, sizeof(dest[i])); | |
267 | dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT; | |
268 | dest[i].vport.num = tt->dest.vport.num; | |
269 | dest[i].vport.vhca_id = tt->dest.vport.vhca_id; | |
270 | mlx5_eswitch_termtbl_put(esw, tt); | |
271 | break; | |
272 | } | |
273 | } | |
274 | rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest); | |
275 | out: | |
276 | return rule; | |
277 | } |