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