1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies */
3
4 #include <linux/mlx5/vport.h>
5 #include "mlx5_core.h"
6 #include "fs_core.h"
7 #include "fs_cmd.h"
8 #include "mlx5dr.h"
9 #include "fs_dr.h"
10 #include "dr_types.h"
11
mlx5_cmd_dr_update_root_ft(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,u32 underlay_qpn,bool disconnect)12 static int mlx5_cmd_dr_update_root_ft(struct mlx5_flow_root_namespace *ns,
13 struct mlx5_flow_table *ft,
14 u32 underlay_qpn,
15 bool disconnect)
16 {
17 return mlx5_fs_cmd_get_fw_cmds()->update_root_ft(ns, ft, underlay_qpn,
18 disconnect);
19 }
20
set_miss_action(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_table * next_ft)21 static int set_miss_action(struct mlx5_flow_root_namespace *ns,
22 struct mlx5_flow_table *ft,
23 struct mlx5_flow_table *next_ft)
24 {
25 struct mlx5dr_action *old_miss_action;
26 struct mlx5dr_action *action = NULL;
27 struct mlx5dr_table *next_tbl;
28 int err;
29
30 next_tbl = next_ft ? next_ft->fs_dr_table.dr_table : NULL;
31 if (next_tbl) {
32 action = mlx5dr_action_create_dest_table(next_tbl);
33 if (!action)
34 return -EINVAL;
35 }
36 old_miss_action = ft->fs_dr_table.miss_action;
37 err = mlx5dr_table_set_miss_action(ft->fs_dr_table.dr_table, action);
38 if (err && action) {
39 err = mlx5dr_action_destroy(action);
40 if (err)
41 mlx5_core_err(ns->dev,
42 "Failed to destroy action (%d)\n", err);
43 action = NULL;
44 }
45 ft->fs_dr_table.miss_action = action;
46 if (old_miss_action) {
47 err = mlx5dr_action_destroy(old_miss_action);
48 if (err)
49 mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
50 err);
51 }
52
53 return err;
54 }
55
mlx5_cmd_dr_create_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_table_attr * ft_attr,struct mlx5_flow_table * next_ft)56 static int mlx5_cmd_dr_create_flow_table(struct mlx5_flow_root_namespace *ns,
57 struct mlx5_flow_table *ft,
58 struct mlx5_flow_table_attr *ft_attr,
59 struct mlx5_flow_table *next_ft)
60 {
61 struct mlx5dr_table *tbl;
62 u32 flags;
63 int err;
64
65 if (mlx5_fs_cmd_is_fw_term_table(ft))
66 return mlx5_fs_cmd_get_fw_cmds()->create_flow_table(ns, ft,
67 ft_attr,
68 next_ft);
69 flags = ft->flags;
70 /* turn off encap/decap if not supported for sw-str by fw */
71 if (!MLX5_CAP_FLOWTABLE(ns->dev, sw_owner_reformat_supported))
72 flags = ft->flags & ~(MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT |
73 MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
74
75 tbl = mlx5dr_table_create(ns->fs_dr_domain.dr_domain, ft->level, flags,
76 ft_attr->uid);
77 if (!tbl) {
78 mlx5_core_err(ns->dev, "Failed creating dr flow_table\n");
79 return -EINVAL;
80 }
81
82 ft->fs_dr_table.dr_table = tbl;
83 ft->id = mlx5dr_table_get_id(tbl);
84
85 if (next_ft) {
86 err = set_miss_action(ns, ft, next_ft);
87 if (err) {
88 mlx5dr_table_destroy(tbl);
89 ft->fs_dr_table.dr_table = NULL;
90 return err;
91 }
92 }
93
94 ft->max_fte = INT_MAX;
95
96 return 0;
97 }
98
mlx5_cmd_dr_destroy_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft)99 static int mlx5_cmd_dr_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
100 struct mlx5_flow_table *ft)
101 {
102 struct mlx5dr_action *action = ft->fs_dr_table.miss_action;
103 int err;
104
105 if (mlx5_fs_cmd_is_fw_term_table(ft))
106 return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_table(ns, ft);
107
108 err = mlx5dr_table_destroy(ft->fs_dr_table.dr_table);
109 if (err) {
110 mlx5_core_err(ns->dev, "Failed to destroy flow_table (%d)\n",
111 err);
112 return err;
113 }
114 if (action) {
115 err = mlx5dr_action_destroy(action);
116 if (err) {
117 mlx5_core_err(ns->dev, "Failed to destroy action(%d)\n",
118 err);
119 return err;
120 }
121 }
122
123 return err;
124 }
125
mlx5_cmd_dr_modify_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_table * next_ft)126 static int mlx5_cmd_dr_modify_flow_table(struct mlx5_flow_root_namespace *ns,
127 struct mlx5_flow_table *ft,
128 struct mlx5_flow_table *next_ft)
129 {
130 if (mlx5_fs_cmd_is_fw_term_table(ft))
131 return mlx5_fs_cmd_get_fw_cmds()->modify_flow_table(ns, ft, next_ft);
132
133 return set_miss_action(ns, ft, next_ft);
134 }
135
mlx5_cmd_dr_create_flow_group(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,u32 * in,struct mlx5_flow_group * fg)136 static int mlx5_cmd_dr_create_flow_group(struct mlx5_flow_root_namespace *ns,
137 struct mlx5_flow_table *ft,
138 u32 *in,
139 struct mlx5_flow_group *fg)
140 {
141 struct mlx5dr_matcher *matcher;
142 u32 priority = MLX5_GET(create_flow_group_in, in,
143 start_flow_index);
144 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
145 in,
146 match_criteria_enable);
147 struct mlx5dr_match_parameters mask;
148
149 if (mlx5_fs_cmd_is_fw_term_table(ft))
150 return mlx5_fs_cmd_get_fw_cmds()->create_flow_group(ns, ft, in,
151 fg);
152
153 mask.match_buf = MLX5_ADDR_OF(create_flow_group_in,
154 in, match_criteria);
155 mask.match_sz = sizeof(fg->mask.match_criteria);
156
157 matcher = mlx5dr_matcher_create(ft->fs_dr_table.dr_table,
158 priority,
159 match_criteria_enable,
160 &mask);
161 if (!matcher) {
162 mlx5_core_err(ns->dev, "Failed creating matcher\n");
163 return -EINVAL;
164 }
165
166 fg->fs_dr_matcher.dr_matcher = matcher;
167 return 0;
168 }
169
mlx5_cmd_dr_destroy_flow_group(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * fg)170 static int mlx5_cmd_dr_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
171 struct mlx5_flow_table *ft,
172 struct mlx5_flow_group *fg)
173 {
174 if (mlx5_fs_cmd_is_fw_term_table(ft))
175 return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_group(ns, ft, fg);
176
177 return mlx5dr_matcher_destroy(fg->fs_dr_matcher.dr_matcher);
178 }
179
create_vport_action(struct mlx5dr_domain * domain,struct mlx5_flow_rule * dst)180 static struct mlx5dr_action *create_vport_action(struct mlx5dr_domain *domain,
181 struct mlx5_flow_rule *dst)
182 {
183 struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
184
185 return mlx5dr_action_create_dest_vport(domain, dest_attr->vport.num,
186 dest_attr->vport.flags &
187 MLX5_FLOW_DEST_VPORT_VHCA_ID,
188 dest_attr->vport.vhca_id);
189 }
190
create_uplink_action(struct mlx5dr_domain * domain,struct mlx5_flow_rule * dst)191 static struct mlx5dr_action *create_uplink_action(struct mlx5dr_domain *domain,
192 struct mlx5_flow_rule *dst)
193 {
194 struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
195
196 return mlx5dr_action_create_dest_vport(domain, MLX5_VPORT_UPLINK, 1,
197 dest_attr->vport.vhca_id);
198 }
199
create_ft_action(struct mlx5dr_domain * domain,struct mlx5_flow_rule * dst)200 static struct mlx5dr_action *create_ft_action(struct mlx5dr_domain *domain,
201 struct mlx5_flow_rule *dst)
202 {
203 struct mlx5_flow_table *dest_ft = dst->dest_attr.ft;
204 struct mlx5dr_action *tbl_action;
205
206 if (mlx5dr_is_fw_table(dest_ft))
207 return mlx5dr_action_create_dest_flow_fw_table(domain, dest_ft);
208
209 tbl_action = mlx5dr_action_create_dest_table(dest_ft->fs_dr_table.dr_table);
210 if (tbl_action)
211 tbl_action->dest_tbl->is_wire_ft =
212 dest_ft->flags & MLX5_FLOW_TABLE_UPLINK_VPORT ? 1 : 0;
213
214 return tbl_action;
215 }
216
create_range_action(struct mlx5dr_domain * domain,struct mlx5_flow_rule * dst)217 static struct mlx5dr_action *create_range_action(struct mlx5dr_domain *domain,
218 struct mlx5_flow_rule *dst)
219 {
220 return mlx5dr_action_create_dest_match_range(domain,
221 dst->dest_attr.range.field,
222 dst->dest_attr.range.hit_ft,
223 dst->dest_attr.range.miss_ft,
224 dst->dest_attr.range.min,
225 dst->dest_attr.range.max);
226 }
227
create_action_push_vlan(struct mlx5dr_domain * domain,struct mlx5_fs_vlan * vlan)228 static struct mlx5dr_action *create_action_push_vlan(struct mlx5dr_domain *domain,
229 struct mlx5_fs_vlan *vlan)
230 {
231 u16 n_ethtype = vlan->ethtype;
232 u8 prio = vlan->prio;
233 u16 vid = vlan->vid;
234 u32 vlan_hdr;
235
236 vlan_hdr = (u32)n_ethtype << 16 | (u32)(prio) << 12 | (u32)vid;
237 return mlx5dr_action_create_push_vlan(domain, htonl(vlan_hdr));
238 }
239
contain_vport_reformat_action(struct mlx5_flow_rule * dst)240 static bool contain_vport_reformat_action(struct mlx5_flow_rule *dst)
241 {
242 return (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
243 dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) &&
244 dst->dest_attr.vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID;
245 }
246
247 /* We want to support a rule with 32 destinations, which means we need to
248 * account for 32 destinations plus usually a counter plus one more action
249 * for a multi-destination flow table.
250 */
251 #define MLX5_FLOW_CONTEXT_ACTION_MAX 34
mlx5_cmd_dr_create_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * group,struct fs_fte * fte)252 static int mlx5_cmd_dr_create_fte(struct mlx5_flow_root_namespace *ns,
253 struct mlx5_flow_table *ft,
254 struct mlx5_flow_group *group,
255 struct fs_fte *fte)
256 {
257 struct mlx5dr_domain *domain = ns->fs_dr_domain.dr_domain;
258 struct mlx5dr_action_dest *term_actions;
259 struct mlx5dr_match_parameters params;
260 struct mlx5_core_dev *dev = ns->dev;
261 struct mlx5dr_action **fs_dr_actions;
262 struct mlx5dr_action *tmp_action;
263 struct mlx5dr_action **actions;
264 bool delay_encap_set = false;
265 struct mlx5dr_rule *rule;
266 struct mlx5_flow_rule *dst;
267 int fs_dr_num_actions = 0;
268 int num_term_actions = 0;
269 int num_actions = 0;
270 size_t match_sz;
271 int err = 0;
272 int i;
273
274 if (mlx5_fs_cmd_is_fw_term_table(ft))
275 return mlx5_fs_cmd_get_fw_cmds()->create_fte(ns, ft, group, fte);
276
277 actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX, sizeof(*actions),
278 GFP_KERNEL);
279 if (!actions) {
280 err = -ENOMEM;
281 goto out_err;
282 }
283
284 fs_dr_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
285 sizeof(*fs_dr_actions), GFP_KERNEL);
286 if (!fs_dr_actions) {
287 err = -ENOMEM;
288 goto free_actions_alloc;
289 }
290
291 term_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
292 sizeof(*term_actions), GFP_KERNEL);
293 if (!term_actions) {
294 err = -ENOMEM;
295 goto free_fs_dr_actions_alloc;
296 }
297
298 match_sz = sizeof(fte->val);
299
300 /* Drop reformat action bit if destination vport set with reformat */
301 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
302 list_for_each_entry(dst, &fte->node.children, node.list) {
303 if (!contain_vport_reformat_action(dst))
304 continue;
305
306 fte->act_dests.action.action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
307 break;
308 }
309 }
310
311 /* The order of the actions are must to be keep, only the following
312 * order is supported by SW steering:
313 * TX: modify header -> push vlan -> encap
314 * RX: decap -> pop vlan -> modify header
315 */
316 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_DECAP) {
317 enum mlx5dr_action_reformat_type decap_type =
318 DR_ACTION_REFORMAT_TYP_TNL_L2_TO_L2;
319
320 tmp_action = mlx5dr_action_create_packet_reformat(domain,
321 decap_type,
322 0, 0, 0,
323 NULL);
324 if (!tmp_action) {
325 err = -ENOMEM;
326 goto free_actions;
327 }
328 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
329 actions[num_actions++] = tmp_action;
330 }
331
332 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT) {
333 bool is_decap;
334
335 if (fte->act_dests.action.pkt_reformat->owner == MLX5_FLOW_RESOURCE_OWNER_FW) {
336 err = -EINVAL;
337 mlx5dr_err(domain, "FW-owned reformat can't be used in SW rule\n");
338 goto free_actions;
339 }
340
341 is_decap = fte->act_dests.action.pkt_reformat->reformat_type ==
342 MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
343
344 if (is_decap)
345 actions[num_actions++] =
346 fte->act_dests.action.pkt_reformat->action.dr_action;
347 else
348 delay_encap_set = true;
349 }
350
351 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) {
352 tmp_action =
353 mlx5dr_action_create_pop_vlan();
354 if (!tmp_action) {
355 err = -ENOMEM;
356 goto free_actions;
357 }
358 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
359 actions[num_actions++] = tmp_action;
360 }
361
362 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2) {
363 tmp_action =
364 mlx5dr_action_create_pop_vlan();
365 if (!tmp_action) {
366 err = -ENOMEM;
367 goto free_actions;
368 }
369 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
370 actions[num_actions++] = tmp_action;
371 }
372
373 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
374 actions[num_actions++] =
375 fte->act_dests.action.modify_hdr->action.dr_action;
376
377 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
378 tmp_action = create_action_push_vlan(domain, &fte->act_dests.action.vlan[0]);
379 if (!tmp_action) {
380 err = -ENOMEM;
381 goto free_actions;
382 }
383 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
384 actions[num_actions++] = tmp_action;
385 }
386
387 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
388 tmp_action = create_action_push_vlan(domain, &fte->act_dests.action.vlan[1]);
389 if (!tmp_action) {
390 err = -ENOMEM;
391 goto free_actions;
392 }
393 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
394 actions[num_actions++] = tmp_action;
395 }
396
397 if (delay_encap_set)
398 actions[num_actions++] =
399 fte->act_dests.action.pkt_reformat->action.dr_action;
400
401 /* The order of the actions below is not important */
402
403 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
404 tmp_action = mlx5dr_action_create_drop();
405 if (!tmp_action) {
406 err = -ENOMEM;
407 goto free_actions;
408 }
409 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
410 term_actions[num_term_actions++].dest = tmp_action;
411 }
412
413 if (fte->act_dests.flow_context.flow_tag) {
414 tmp_action =
415 mlx5dr_action_create_tag(fte->act_dests.flow_context.flow_tag);
416 if (!tmp_action) {
417 err = -ENOMEM;
418 goto free_actions;
419 }
420 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
421 actions[num_actions++] = tmp_action;
422 }
423
424 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
425 list_for_each_entry(dst, &fte->node.children, node.list) {
426 enum mlx5_flow_destination_type type = dst->dest_attr.type;
427 u32 id;
428
429 if (fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
430 num_term_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
431 err = -EOPNOTSUPP;
432 goto free_actions;
433 }
434
435 if (type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
436 continue;
437
438 switch (type) {
439 case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
440 tmp_action = create_ft_action(domain, dst);
441 if (!tmp_action) {
442 err = -ENOMEM;
443 goto free_actions;
444 }
445 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
446 term_actions[num_term_actions++].dest = tmp_action;
447 break;
448 case MLX5_FLOW_DESTINATION_TYPE_UPLINK:
449 case MLX5_FLOW_DESTINATION_TYPE_VPORT:
450 tmp_action = type == MLX5_FLOW_DESTINATION_TYPE_VPORT ?
451 create_vport_action(domain, dst) :
452 create_uplink_action(domain, dst);
453 if (!tmp_action) {
454 err = -ENOMEM;
455 goto free_actions;
456 }
457 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
458 term_actions[num_term_actions].dest = tmp_action;
459
460 if (dst->dest_attr.vport.flags &
461 MLX5_FLOW_DEST_VPORT_REFORMAT_ID)
462 term_actions[num_term_actions].reformat =
463 dst->dest_attr.vport.pkt_reformat->action.dr_action;
464
465 num_term_actions++;
466 break;
467 case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM:
468 id = dst->dest_attr.ft_num;
469 tmp_action = mlx5dr_action_create_dest_table_num(domain,
470 id);
471 if (!tmp_action) {
472 err = -ENOMEM;
473 goto free_actions;
474 }
475 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
476 term_actions[num_term_actions++].dest = tmp_action;
477 break;
478 case MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER:
479 id = dst->dest_attr.sampler_id;
480 tmp_action = mlx5dr_action_create_flow_sampler(domain,
481 id);
482 if (!tmp_action) {
483 err = -ENOMEM;
484 goto free_actions;
485 }
486 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
487 term_actions[num_term_actions++].dest = tmp_action;
488 break;
489 case MLX5_FLOW_DESTINATION_TYPE_RANGE:
490 tmp_action = create_range_action(domain, dst);
491 if (!tmp_action) {
492 err = -ENOMEM;
493 goto free_actions;
494 }
495 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
496 term_actions[num_term_actions++].dest = tmp_action;
497 break;
498 default:
499 err = -EOPNOTSUPP;
500 goto free_actions;
501 }
502 }
503 }
504
505 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
506 list_for_each_entry(dst, &fte->node.children, node.list) {
507 u32 id;
508
509 if (dst->dest_attr.type !=
510 MLX5_FLOW_DESTINATION_TYPE_COUNTER)
511 continue;
512
513 if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
514 fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
515 err = -EOPNOTSUPP;
516 goto free_actions;
517 }
518
519 id = dst->dest_attr.counter_id;
520 tmp_action =
521 mlx5dr_action_create_flow_counter(id);
522 if (!tmp_action) {
523 err = -ENOMEM;
524 goto free_actions;
525 }
526
527 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
528 actions[num_actions++] = tmp_action;
529 }
530 }
531
532 if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO) {
533 struct mlx5_flow_act *action = &fte->act_dests.action;
534
535 if (fte->act_dests.action.exe_aso.type != MLX5_EXE_ASO_FLOW_METER) {
536 err = -EOPNOTSUPP;
537 goto free_actions;
538 }
539
540 tmp_action =
541 mlx5dr_action_create_aso(domain,
542 action->exe_aso.object_id,
543 action->exe_aso.return_reg_id,
544 action->exe_aso.type,
545 action->exe_aso.flow_meter.init_color,
546 action->exe_aso.flow_meter.meter_idx);
547 if (!tmp_action) {
548 err = -ENOMEM;
549 goto free_actions;
550 }
551 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
552 actions[num_actions++] = tmp_action;
553 }
554
555 params.match_sz = match_sz;
556 params.match_buf = (u64 *)fte->val;
557 if (num_term_actions == 1) {
558 if (term_actions->reformat) {
559 if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
560 err = -EOPNOTSUPP;
561 goto free_actions;
562 }
563 actions[num_actions++] = term_actions->reformat;
564 }
565
566 if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
567 err = -EOPNOTSUPP;
568 goto free_actions;
569 }
570 actions[num_actions++] = term_actions->dest;
571 } else if (num_term_actions > 1) {
572 bool ignore_flow_level =
573 !!(fte->act_dests.action.flags & FLOW_ACT_IGNORE_FLOW_LEVEL);
574 u32 flow_source = fte->act_dests.flow_context.flow_source;
575
576 if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
577 fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
578 err = -EOPNOTSUPP;
579 goto free_actions;
580 }
581 tmp_action = mlx5dr_action_create_mult_dest_tbl(domain,
582 term_actions,
583 num_term_actions,
584 ignore_flow_level,
585 flow_source);
586 if (!tmp_action) {
587 err = -EOPNOTSUPP;
588 goto free_actions;
589 }
590 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
591 actions[num_actions++] = tmp_action;
592 }
593
594 rule = mlx5dr_rule_create(group->fs_dr_matcher.dr_matcher,
595 ¶ms,
596 num_actions,
597 actions,
598 fte->act_dests.flow_context.flow_source);
599 if (!rule) {
600 err = -EINVAL;
601 goto free_actions;
602 }
603
604 kfree(term_actions);
605 kfree(actions);
606
607 fte->fs_dr_rule.dr_rule = rule;
608 fte->fs_dr_rule.num_actions = fs_dr_num_actions;
609 fte->fs_dr_rule.dr_actions = fs_dr_actions;
610
611 return 0;
612
613 free_actions:
614 /* Free in reverse order to handle action dependencies */
615 for (i = fs_dr_num_actions - 1; i >= 0; i--)
616 if (!IS_ERR_OR_NULL(fs_dr_actions[i]))
617 mlx5dr_action_destroy(fs_dr_actions[i]);
618
619 kfree(term_actions);
620 free_fs_dr_actions_alloc:
621 kfree(fs_dr_actions);
622 free_actions_alloc:
623 kfree(actions);
624 out_err:
625 mlx5_core_err(dev, "Failed to create dr rule err(%d)\n", err);
626 return err;
627 }
628
mlx5_cmd_dr_packet_reformat_alloc(struct mlx5_flow_root_namespace * ns,struct mlx5_pkt_reformat_params * params,enum mlx5_flow_namespace_type namespace,struct mlx5_pkt_reformat * pkt_reformat)629 static int mlx5_cmd_dr_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
630 struct mlx5_pkt_reformat_params *params,
631 enum mlx5_flow_namespace_type namespace,
632 struct mlx5_pkt_reformat *pkt_reformat)
633 {
634 struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
635 struct mlx5dr_action *action;
636 int dr_reformat;
637
638 switch (params->type) {
639 case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
640 case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
641 case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
642 dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L2;
643 break;
644 case MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
645 dr_reformat = DR_ACTION_REFORMAT_TYP_TNL_L3_TO_L2;
646 break;
647 case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
648 dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L3;
649 break;
650 case MLX5_REFORMAT_TYPE_INSERT_HDR:
651 dr_reformat = DR_ACTION_REFORMAT_TYP_INSERT_HDR;
652 break;
653 case MLX5_REFORMAT_TYPE_REMOVE_HDR:
654 dr_reformat = DR_ACTION_REFORMAT_TYP_REMOVE_HDR;
655 break;
656 default:
657 mlx5_core_err(ns->dev, "Packet-reformat not supported(%d)\n",
658 params->type);
659 return -EOPNOTSUPP;
660 }
661
662 action = mlx5dr_action_create_packet_reformat(dr_domain,
663 dr_reformat,
664 params->param_0,
665 params->param_1,
666 params->size,
667 params->data);
668 if (!action) {
669 mlx5_core_err(ns->dev, "Failed allocating packet-reformat action\n");
670 return -EINVAL;
671 }
672
673 pkt_reformat->owner = MLX5_FLOW_RESOURCE_OWNER_SW;
674 pkt_reformat->action.dr_action = action;
675
676 return 0;
677 }
678
mlx5_cmd_dr_packet_reformat_dealloc(struct mlx5_flow_root_namespace * ns,struct mlx5_pkt_reformat * pkt_reformat)679 static void mlx5_cmd_dr_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
680 struct mlx5_pkt_reformat *pkt_reformat)
681 {
682 mlx5dr_action_destroy(pkt_reformat->action.dr_action);
683 }
684
mlx5_cmd_dr_modify_header_alloc(struct mlx5_flow_root_namespace * ns,u8 namespace,u8 num_actions,void * modify_actions,struct mlx5_modify_hdr * modify_hdr)685 static int mlx5_cmd_dr_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
686 u8 namespace, u8 num_actions,
687 void *modify_actions,
688 struct mlx5_modify_hdr *modify_hdr)
689 {
690 struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
691 struct mlx5dr_action *action;
692 size_t actions_sz;
693
694 actions_sz = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto) *
695 num_actions;
696 action = mlx5dr_action_create_modify_header(dr_domain, 0,
697 actions_sz,
698 modify_actions);
699 if (!action) {
700 mlx5_core_err(ns->dev, "Failed allocating modify-header action\n");
701 return -EINVAL;
702 }
703
704 modify_hdr->owner = MLX5_FLOW_RESOURCE_OWNER_SW;
705 modify_hdr->action.dr_action = action;
706
707 return 0;
708 }
709
mlx5_cmd_dr_modify_header_dealloc(struct mlx5_flow_root_namespace * ns,struct mlx5_modify_hdr * modify_hdr)710 static void mlx5_cmd_dr_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
711 struct mlx5_modify_hdr *modify_hdr)
712 {
713 mlx5dr_action_destroy(modify_hdr->action.dr_action);
714 }
715
716 static int
mlx5_cmd_dr_destroy_match_definer(struct mlx5_flow_root_namespace * ns,int definer_id)717 mlx5_cmd_dr_destroy_match_definer(struct mlx5_flow_root_namespace *ns,
718 int definer_id)
719 {
720 return -EOPNOTSUPP;
721 }
722
mlx5_cmd_dr_create_match_definer(struct mlx5_flow_root_namespace * ns,u16 format_id,u32 * match_mask)723 static int mlx5_cmd_dr_create_match_definer(struct mlx5_flow_root_namespace *ns,
724 u16 format_id, u32 *match_mask)
725 {
726 return -EOPNOTSUPP;
727 }
728
mlx5_cmd_dr_delete_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct fs_fte * fte)729 static int mlx5_cmd_dr_delete_fte(struct mlx5_flow_root_namespace *ns,
730 struct mlx5_flow_table *ft,
731 struct fs_fte *fte)
732 {
733 struct mlx5_fs_dr_rule *rule = &fte->fs_dr_rule;
734 int err;
735 int i;
736
737 if (mlx5_fs_cmd_is_fw_term_table(ft))
738 return mlx5_fs_cmd_get_fw_cmds()->delete_fte(ns, ft, fte);
739
740 err = mlx5dr_rule_destroy(rule->dr_rule);
741 if (err)
742 return err;
743
744 /* Free in reverse order to handle action dependencies */
745 for (i = rule->num_actions - 1; i >= 0; i--)
746 if (!IS_ERR_OR_NULL(rule->dr_actions[i]))
747 mlx5dr_action_destroy(rule->dr_actions[i]);
748
749 kfree(rule->dr_actions);
750 return 0;
751 }
752
mlx5_cmd_dr_update_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * group,int modify_mask,struct fs_fte * fte)753 static int mlx5_cmd_dr_update_fte(struct mlx5_flow_root_namespace *ns,
754 struct mlx5_flow_table *ft,
755 struct mlx5_flow_group *group,
756 int modify_mask,
757 struct fs_fte *fte)
758 {
759 struct fs_fte fte_tmp = {};
760 int ret;
761
762 if (mlx5_fs_cmd_is_fw_term_table(ft))
763 return mlx5_fs_cmd_get_fw_cmds()->update_fte(ns, ft, group, modify_mask, fte);
764
765 /* Backup current dr rule details */
766 fte_tmp.fs_dr_rule = fte->fs_dr_rule;
767 memset(&fte->fs_dr_rule, 0, sizeof(struct mlx5_fs_dr_rule));
768
769 /* First add the new updated rule, then delete the old rule */
770 ret = mlx5_cmd_dr_create_fte(ns, ft, group, fte);
771 if (ret)
772 goto restore_fte;
773
774 ret = mlx5_cmd_dr_delete_fte(ns, ft, &fte_tmp);
775 WARN_ONCE(ret, "dr update fte duplicate rule deletion failed\n");
776 return ret;
777
778 restore_fte:
779 fte->fs_dr_rule = fte_tmp.fs_dr_rule;
780 return ret;
781 }
782
mlx5_cmd_dr_set_peer(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_root_namespace * peer_ns,u16 peer_vhca_id)783 static int mlx5_cmd_dr_set_peer(struct mlx5_flow_root_namespace *ns,
784 struct mlx5_flow_root_namespace *peer_ns,
785 u16 peer_vhca_id)
786 {
787 struct mlx5dr_domain *peer_domain = NULL;
788
789 if (peer_ns)
790 peer_domain = peer_ns->fs_dr_domain.dr_domain;
791 mlx5dr_domain_set_peer(ns->fs_dr_domain.dr_domain,
792 peer_domain, peer_vhca_id);
793 return 0;
794 }
795
mlx5_cmd_dr_create_ns(struct mlx5_flow_root_namespace * ns)796 static int mlx5_cmd_dr_create_ns(struct mlx5_flow_root_namespace *ns)
797 {
798 ns->fs_dr_domain.dr_domain =
799 mlx5dr_domain_create(ns->dev,
800 MLX5DR_DOMAIN_TYPE_FDB);
801 if (!ns->fs_dr_domain.dr_domain) {
802 mlx5_core_err(ns->dev, "Failed to create dr flow namespace\n");
803 return -EOPNOTSUPP;
804 }
805 return 0;
806 }
807
mlx5_cmd_dr_destroy_ns(struct mlx5_flow_root_namespace * ns)808 static int mlx5_cmd_dr_destroy_ns(struct mlx5_flow_root_namespace *ns)
809 {
810 return mlx5dr_domain_destroy(ns->fs_dr_domain.dr_domain);
811 }
812
mlx5_cmd_dr_get_capabilities(struct mlx5_flow_root_namespace * ns,enum fs_flow_table_type ft_type)813 static u32 mlx5_cmd_dr_get_capabilities(struct mlx5_flow_root_namespace *ns,
814 enum fs_flow_table_type ft_type)
815 {
816 u32 steering_caps = MLX5_FLOW_STEERING_CAP_DUPLICATE_MATCH;
817
818 if (ft_type != FS_FT_FDB ||
819 MLX5_CAP_GEN(ns->dev, steering_format_version) == MLX5_STEERING_FORMAT_CONNECTX_5)
820 return steering_caps;
821
822 steering_caps |= MLX5_FLOW_STEERING_CAP_VLAN_PUSH_ON_RX;
823 steering_caps |= MLX5_FLOW_STEERING_CAP_VLAN_POP_ON_TX;
824
825 if (mlx5dr_supp_match_ranges(ns->dev))
826 steering_caps |= MLX5_FLOW_STEERING_CAP_MATCH_RANGES;
827
828 return steering_caps;
829 }
830
mlx5_fs_dr_action_get_pkt_reformat_id(struct mlx5_pkt_reformat * pkt_reformat)831 int mlx5_fs_dr_action_get_pkt_reformat_id(struct mlx5_pkt_reformat *pkt_reformat)
832 {
833 switch (pkt_reformat->reformat_type) {
834 case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
835 case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
836 case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
837 case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
838 case MLX5_REFORMAT_TYPE_INSERT_HDR:
839 return mlx5dr_action_get_pkt_reformat_id(pkt_reformat->action.dr_action);
840 }
841 return -EOPNOTSUPP;
842 }
843
mlx5_fs_dr_is_supported(struct mlx5_core_dev * dev)844 bool mlx5_fs_dr_is_supported(struct mlx5_core_dev *dev)
845 {
846 return mlx5dr_is_supported(dev);
847 }
848
849 static const struct mlx5_flow_cmds mlx5_flow_cmds_dr = {
850 .create_flow_table = mlx5_cmd_dr_create_flow_table,
851 .destroy_flow_table = mlx5_cmd_dr_destroy_flow_table,
852 .modify_flow_table = mlx5_cmd_dr_modify_flow_table,
853 .create_flow_group = mlx5_cmd_dr_create_flow_group,
854 .destroy_flow_group = mlx5_cmd_dr_destroy_flow_group,
855 .create_fte = mlx5_cmd_dr_create_fte,
856 .update_fte = mlx5_cmd_dr_update_fte,
857 .delete_fte = mlx5_cmd_dr_delete_fte,
858 .update_root_ft = mlx5_cmd_dr_update_root_ft,
859 .packet_reformat_alloc = mlx5_cmd_dr_packet_reformat_alloc,
860 .packet_reformat_dealloc = mlx5_cmd_dr_packet_reformat_dealloc,
861 .modify_header_alloc = mlx5_cmd_dr_modify_header_alloc,
862 .modify_header_dealloc = mlx5_cmd_dr_modify_header_dealloc,
863 .create_match_definer = mlx5_cmd_dr_create_match_definer,
864 .destroy_match_definer = mlx5_cmd_dr_destroy_match_definer,
865 .set_peer = mlx5_cmd_dr_set_peer,
866 .create_ns = mlx5_cmd_dr_create_ns,
867 .destroy_ns = mlx5_cmd_dr_destroy_ns,
868 .get_capabilities = mlx5_cmd_dr_get_capabilities,
869 };
870
mlx5_fs_cmd_get_dr_cmds(void)871 const struct mlx5_flow_cmds *mlx5_fs_cmd_get_dr_cmds(void)
872 {
873 return &mlx5_flow_cmds_dr;
874 }
875