This source file includes following definitions.
- mlx5_dr_is_fw_table
- mlx5_cmd_dr_update_root_ft
- set_miss_action
- mlx5_cmd_dr_create_flow_table
- mlx5_cmd_dr_destroy_flow_table
- mlx5_cmd_dr_modify_flow_table
- mlx5_cmd_dr_create_flow_group
- mlx5_cmd_dr_destroy_flow_group
- create_vport_action
- create_ft_action
- create_action_push_vlan
- mlx5_cmd_dr_create_fte
- mlx5_cmd_dr_packet_reformat_alloc
- mlx5_cmd_dr_packet_reformat_dealloc
- mlx5_cmd_dr_modify_header_alloc
- mlx5_cmd_dr_modify_header_dealloc
- mlx5_cmd_dr_update_fte
- mlx5_cmd_dr_delete_fte
- mlx5_cmd_dr_set_peer
- mlx5_cmd_dr_create_ns
- mlx5_cmd_dr_destroy_ns
- mlx5_fs_dr_is_supported
- mlx5_fs_cmd_get_dr_cmds
1
2
3
4 #include "mlx5_core.h"
5 #include "fs_core.h"
6 #include "fs_cmd.h"
7 #include "mlx5dr.h"
8 #include "fs_dr.h"
9
10 static bool mlx5_dr_is_fw_table(u32 flags)
11 {
12 if (flags & MLX5_FLOW_TABLE_TERMINATION)
13 return true;
14
15 return false;
16 }
17
18 static int mlx5_cmd_dr_update_root_ft(struct mlx5_flow_root_namespace *ns,
19 struct mlx5_flow_table *ft,
20 u32 underlay_qpn,
21 bool disconnect)
22 {
23 return mlx5_fs_cmd_get_fw_cmds()->update_root_ft(ns, ft, underlay_qpn,
24 disconnect);
25 }
26
27 static int set_miss_action(struct mlx5_flow_root_namespace *ns,
28 struct mlx5_flow_table *ft,
29 struct mlx5_flow_table *next_ft)
30 {
31 struct mlx5dr_action *old_miss_action;
32 struct mlx5dr_action *action = NULL;
33 struct mlx5dr_table *next_tbl;
34 int err;
35
36 next_tbl = next_ft ? next_ft->fs_dr_table.dr_table : NULL;
37 if (next_tbl) {
38 action = mlx5dr_action_create_dest_table(next_tbl);
39 if (!action)
40 return -EINVAL;
41 }
42 old_miss_action = ft->fs_dr_table.miss_action;
43 err = mlx5dr_table_set_miss_action(ft->fs_dr_table.dr_table, action);
44 if (err && action) {
45 err = mlx5dr_action_destroy(action);
46 if (err) {
47 action = NULL;
48 mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
49 err);
50 }
51 }
52 ft->fs_dr_table.miss_action = action;
53 if (old_miss_action) {
54 err = mlx5dr_action_destroy(old_miss_action);
55 if (err)
56 mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
57 err);
58 }
59
60 return err;
61 }
62
63 static int mlx5_cmd_dr_create_flow_table(struct mlx5_flow_root_namespace *ns,
64 struct mlx5_flow_table *ft,
65 unsigned int log_size,
66 struct mlx5_flow_table *next_ft)
67 {
68 struct mlx5dr_table *tbl;
69 int err;
70
71 if (mlx5_dr_is_fw_table(ft->flags))
72 return mlx5_fs_cmd_get_fw_cmds()->create_flow_table(ns, ft,
73 log_size,
74 next_ft);
75
76 tbl = mlx5dr_table_create(ns->fs_dr_domain.dr_domain,
77 ft->level);
78 if (!tbl) {
79 mlx5_core_err(ns->dev, "Failed creating dr flow_table\n");
80 return -EINVAL;
81 }
82
83 ft->fs_dr_table.dr_table = tbl;
84 ft->id = mlx5dr_table_get_id(tbl);
85
86 if (next_ft) {
87 err = set_miss_action(ns, ft, next_ft);
88 if (err) {
89 mlx5dr_table_destroy(tbl);
90 ft->fs_dr_table.dr_table = NULL;
91 return err;
92 }
93 }
94
95 return 0;
96 }
97
98 static int mlx5_cmd_dr_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
99 struct mlx5_flow_table *ft)
100 {
101 struct mlx5dr_action *action = ft->fs_dr_table.miss_action;
102 int err;
103
104 if (mlx5_dr_is_fw_table(ft->flags))
105 return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_table(ns, ft);
106
107 err = mlx5dr_table_destroy(ft->fs_dr_table.dr_table);
108 if (err) {
109 mlx5_core_err(ns->dev, "Failed to destroy flow_table (%d)\n",
110 err);
111 return err;
112 }
113 if (action) {
114 err = mlx5dr_action_destroy(action);
115 if (err) {
116 mlx5_core_err(ns->dev, "Failed to destroy action(%d)\n",
117 err);
118 return err;
119 }
120 }
121
122 return err;
123 }
124
125 static int mlx5_cmd_dr_modify_flow_table(struct mlx5_flow_root_namespace *ns,
126 struct mlx5_flow_table *ft,
127 struct mlx5_flow_table *next_ft)
128 {
129 return set_miss_action(ns, ft, next_ft);
130 }
131
132 static int mlx5_cmd_dr_create_flow_group(struct mlx5_flow_root_namespace *ns,
133 struct mlx5_flow_table *ft,
134 u32 *in,
135 struct mlx5_flow_group *fg)
136 {
137 struct mlx5dr_matcher *matcher;
138 u16 priority = MLX5_GET(create_flow_group_in, in,
139 start_flow_index);
140 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
141 in,
142 match_criteria_enable);
143 struct mlx5dr_match_parameters mask;
144
145 if (mlx5_dr_is_fw_table(ft->flags))
146 return mlx5_fs_cmd_get_fw_cmds()->create_flow_group(ns, ft, in,
147 fg);
148
149 mask.match_buf = MLX5_ADDR_OF(create_flow_group_in,
150 in, match_criteria);
151 mask.match_sz = sizeof(fg->mask.match_criteria);
152
153 matcher = mlx5dr_matcher_create(ft->fs_dr_table.dr_table,
154 priority,
155 match_criteria_enable,
156 &mask);
157 if (!matcher) {
158 mlx5_core_err(ns->dev, "Failed creating matcher\n");
159 return -EINVAL;
160 }
161
162 fg->fs_dr_matcher.dr_matcher = matcher;
163 return 0;
164 }
165
166 static int mlx5_cmd_dr_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
167 struct mlx5_flow_table *ft,
168 struct mlx5_flow_group *fg)
169 {
170 if (mlx5_dr_is_fw_table(ft->flags))
171 return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_group(ns, ft, fg);
172
173 return mlx5dr_matcher_destroy(fg->fs_dr_matcher.dr_matcher);
174 }
175
176 static struct mlx5dr_action *create_vport_action(struct mlx5dr_domain *domain,
177 struct mlx5_flow_rule *dst)
178 {
179 struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
180
181 return mlx5dr_action_create_dest_vport(domain, dest_attr->vport.num,
182 dest_attr->vport.flags &
183 MLX5_FLOW_DEST_VPORT_VHCA_ID,
184 dest_attr->vport.vhca_id);
185 }
186
187 static struct mlx5dr_action *create_ft_action(struct mlx5_core_dev *dev,
188 struct mlx5_flow_rule *dst)
189 {
190 struct mlx5_flow_table *dest_ft = dst->dest_attr.ft;
191
192 if (mlx5_dr_is_fw_table(dest_ft->flags))
193 return mlx5dr_create_action_dest_flow_fw_table(dest_ft, dev);
194 return mlx5dr_action_create_dest_table(dest_ft->fs_dr_table.dr_table);
195 }
196
197 static struct mlx5dr_action *create_action_push_vlan(struct mlx5dr_domain *domain,
198 struct mlx5_fs_vlan *vlan)
199 {
200 u16 n_ethtype = vlan->ethtype;
201 u8 prio = vlan->prio;
202 u16 vid = vlan->vid;
203 u32 vlan_hdr;
204
205 vlan_hdr = (u32)n_ethtype << 16 | (u32)(prio) << 12 | (u32)vid;
206 return mlx5dr_action_create_push_vlan(domain, htonl(vlan_hdr));
207 }
208
209 #define MLX5_FLOW_CONTEXT_ACTION_MAX 20
210 static int mlx5_cmd_dr_create_fte(struct mlx5_flow_root_namespace *ns,
211 struct mlx5_flow_table *ft,
212 struct mlx5_flow_group *group,
213 struct fs_fte *fte)
214 {
215 struct mlx5dr_domain *domain = ns->fs_dr_domain.dr_domain;
216 struct mlx5dr_action *term_action = NULL;
217 struct mlx5dr_match_parameters params;
218 struct mlx5_core_dev *dev = ns->dev;
219 struct mlx5dr_action **fs_dr_actions;
220 struct mlx5dr_action *tmp_action;
221 struct mlx5dr_action **actions;
222 bool delay_encap_set = false;
223 struct mlx5dr_rule *rule;
224 struct mlx5_flow_rule *dst;
225 int fs_dr_num_actions = 0;
226 int num_actions = 0;
227 size_t match_sz;
228 int err = 0;
229 int i;
230
231 if (mlx5_dr_is_fw_table(ft->flags))
232 return mlx5_fs_cmd_get_fw_cmds()->create_fte(ns, ft, group, fte);
233
234 actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX, sizeof(*actions),
235 GFP_KERNEL);
236 if (!actions)
237 return -ENOMEM;
238
239 fs_dr_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
240 sizeof(*fs_dr_actions), GFP_KERNEL);
241 if (!fs_dr_actions) {
242 kfree(actions);
243 return -ENOMEM;
244 }
245
246 match_sz = sizeof(fte->val);
247
248
249
250
251
252
253 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
254 tmp_action = create_action_push_vlan(domain, &fte->action.vlan[0]);
255 if (!tmp_action) {
256 err = -ENOMEM;
257 goto free_actions;
258 }
259 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
260 actions[num_actions++] = tmp_action;
261 }
262
263 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
264 tmp_action = create_action_push_vlan(domain, &fte->action.vlan[1]);
265 if (!tmp_action) {
266 err = -ENOMEM;
267 goto free_actions;
268 }
269 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
270 actions[num_actions++] = tmp_action;
271 }
272
273 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DECAP) {
274 enum mlx5dr_action_reformat_type decap_type =
275 DR_ACTION_REFORMAT_TYP_TNL_L2_TO_L2;
276
277 tmp_action = mlx5dr_action_create_packet_reformat(domain,
278 decap_type, 0,
279 NULL);
280 if (!tmp_action) {
281 err = -ENOMEM;
282 goto free_actions;
283 }
284 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
285 actions[num_actions++] = tmp_action;
286 }
287
288 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT) {
289 bool is_decap = fte->action.pkt_reformat->reformat_type ==
290 MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
291
292 if (is_decap)
293 actions[num_actions++] =
294 fte->action.pkt_reformat->action.dr_action;
295 else
296 delay_encap_set = true;
297 }
298
299 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) {
300 tmp_action =
301 mlx5dr_action_create_pop_vlan();
302 if (!tmp_action) {
303 err = -ENOMEM;
304 goto free_actions;
305 }
306 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
307 actions[num_actions++] = tmp_action;
308 }
309
310 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2) {
311 tmp_action =
312 mlx5dr_action_create_pop_vlan();
313 if (!tmp_action) {
314 err = -ENOMEM;
315 goto free_actions;
316 }
317 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
318 actions[num_actions++] = tmp_action;
319 }
320
321 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
322 actions[num_actions++] =
323 fte->action.modify_hdr->action.dr_action;
324
325 if (delay_encap_set)
326 actions[num_actions++] =
327 fte->action.pkt_reformat->action.dr_action;
328
329
330
331 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
332 tmp_action = mlx5dr_action_create_drop();
333 if (!tmp_action) {
334 err = -ENOMEM;
335 goto free_actions;
336 }
337 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
338 term_action = tmp_action;
339 }
340
341 if (fte->flow_context.flow_tag) {
342 tmp_action =
343 mlx5dr_action_create_tag(fte->flow_context.flow_tag);
344 if (!tmp_action) {
345 err = -ENOMEM;
346 goto free_actions;
347 }
348 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
349 actions[num_actions++] = tmp_action;
350 }
351
352 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
353 list_for_each_entry(dst, &fte->node.children, node.list) {
354 enum mlx5_flow_destination_type type = dst->dest_attr.type;
355
356 if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
357 err = -ENOSPC;
358 goto free_actions;
359 }
360
361 if (type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
362 continue;
363
364 switch (type) {
365 case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
366 tmp_action = create_ft_action(dev, dst);
367 if (!tmp_action) {
368 err = -ENOMEM;
369 goto free_actions;
370 }
371 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
372 term_action = tmp_action;
373 break;
374 case MLX5_FLOW_DESTINATION_TYPE_VPORT:
375 tmp_action = create_vport_action(domain, dst);
376 if (!tmp_action) {
377 err = -ENOMEM;
378 goto free_actions;
379 }
380 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
381 term_action = tmp_action;
382 break;
383 default:
384 err = -EOPNOTSUPP;
385 goto free_actions;
386 }
387 }
388 }
389
390 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
391 list_for_each_entry(dst, &fte->node.children, node.list) {
392 u32 id;
393
394 if (dst->dest_attr.type !=
395 MLX5_FLOW_DESTINATION_TYPE_COUNTER)
396 continue;
397
398 if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
399 err = -ENOSPC;
400 goto free_actions;
401 }
402
403 id = dst->dest_attr.counter_id;
404 tmp_action =
405 mlx5dr_action_create_flow_counter(id);
406 if (!tmp_action) {
407 err = -ENOMEM;
408 goto free_actions;
409 }
410
411 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
412 actions[num_actions++] = tmp_action;
413 }
414 }
415
416 params.match_sz = match_sz;
417 params.match_buf = (u64 *)fte->val;
418
419 if (term_action)
420 actions[num_actions++] = term_action;
421
422 rule = mlx5dr_rule_create(group->fs_dr_matcher.dr_matcher,
423 ¶ms,
424 num_actions,
425 actions);
426 if (!rule) {
427 err = -EINVAL;
428 goto free_actions;
429 }
430
431 kfree(actions);
432 fte->fs_dr_rule.dr_rule = rule;
433 fte->fs_dr_rule.num_actions = fs_dr_num_actions;
434 fte->fs_dr_rule.dr_actions = fs_dr_actions;
435
436 return 0;
437
438 free_actions:
439 for (i = 0; i < fs_dr_num_actions; i++)
440 if (!IS_ERR_OR_NULL(fs_dr_actions[i]))
441 mlx5dr_action_destroy(fs_dr_actions[i]);
442
443 mlx5_core_err(dev, "Failed to create dr rule err(%d)\n", err);
444 kfree(actions);
445 kfree(fs_dr_actions);
446 return err;
447 }
448
449 static int mlx5_cmd_dr_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
450 int reformat_type,
451 size_t size,
452 void *reformat_data,
453 enum mlx5_flow_namespace_type namespace,
454 struct mlx5_pkt_reformat *pkt_reformat)
455 {
456 struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
457 struct mlx5dr_action *action;
458 int dr_reformat;
459
460 switch (reformat_type) {
461 case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
462 case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
463 case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
464 dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L2;
465 break;
466 case MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
467 dr_reformat = DR_ACTION_REFORMAT_TYP_TNL_L3_TO_L2;
468 break;
469 case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
470 dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L3;
471 break;
472 default:
473 mlx5_core_err(ns->dev, "Packet-reformat not supported(%d)\n",
474 reformat_type);
475 return -EOPNOTSUPP;
476 }
477
478 action = mlx5dr_action_create_packet_reformat(dr_domain,
479 dr_reformat,
480 size,
481 reformat_data);
482 if (!action) {
483 mlx5_core_err(ns->dev, "Failed allocating packet-reformat action\n");
484 return -EINVAL;
485 }
486
487 pkt_reformat->action.dr_action = action;
488
489 return 0;
490 }
491
492 static void mlx5_cmd_dr_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
493 struct mlx5_pkt_reformat *pkt_reformat)
494 {
495 mlx5dr_action_destroy(pkt_reformat->action.dr_action);
496 }
497
498 static int mlx5_cmd_dr_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
499 u8 namespace, u8 num_actions,
500 void *modify_actions,
501 struct mlx5_modify_hdr *modify_hdr)
502 {
503 struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
504 struct mlx5dr_action *action;
505 size_t actions_sz;
506
507 actions_sz = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto) *
508 num_actions;
509 action = mlx5dr_action_create_modify_header(dr_domain, 0,
510 actions_sz,
511 modify_actions);
512 if (!action) {
513 mlx5_core_err(ns->dev, "Failed allocating modify-header action\n");
514 return -EINVAL;
515 }
516
517 modify_hdr->action.dr_action = action;
518
519 return 0;
520 }
521
522 static void mlx5_cmd_dr_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
523 struct mlx5_modify_hdr *modify_hdr)
524 {
525 mlx5dr_action_destroy(modify_hdr->action.dr_action);
526 }
527
528 static int mlx5_cmd_dr_update_fte(struct mlx5_flow_root_namespace *ns,
529 struct mlx5_flow_table *ft,
530 struct mlx5_flow_group *group,
531 int modify_mask,
532 struct fs_fte *fte)
533 {
534 return -EOPNOTSUPP;
535 }
536
537 static int mlx5_cmd_dr_delete_fte(struct mlx5_flow_root_namespace *ns,
538 struct mlx5_flow_table *ft,
539 struct fs_fte *fte)
540 {
541 struct mlx5_fs_dr_rule *rule = &fte->fs_dr_rule;
542 int err;
543 int i;
544
545 if (mlx5_dr_is_fw_table(ft->flags))
546 return mlx5_fs_cmd_get_fw_cmds()->delete_fte(ns, ft, fte);
547
548 err = mlx5dr_rule_destroy(rule->dr_rule);
549 if (err)
550 return err;
551
552 for (i = 0; i < rule->num_actions; i++)
553 if (!IS_ERR_OR_NULL(rule->dr_actions[i]))
554 mlx5dr_action_destroy(rule->dr_actions[i]);
555
556 kfree(rule->dr_actions);
557 return 0;
558 }
559
560 static int mlx5_cmd_dr_set_peer(struct mlx5_flow_root_namespace *ns,
561 struct mlx5_flow_root_namespace *peer_ns)
562 {
563 struct mlx5dr_domain *peer_domain = NULL;
564
565 if (peer_ns)
566 peer_domain = peer_ns->fs_dr_domain.dr_domain;
567 mlx5dr_domain_set_peer(ns->fs_dr_domain.dr_domain,
568 peer_domain);
569 return 0;
570 }
571
572 static int mlx5_cmd_dr_create_ns(struct mlx5_flow_root_namespace *ns)
573 {
574 ns->fs_dr_domain.dr_domain =
575 mlx5dr_domain_create(ns->dev,
576 MLX5DR_DOMAIN_TYPE_FDB);
577 if (!ns->fs_dr_domain.dr_domain) {
578 mlx5_core_err(ns->dev, "Failed to create dr flow namespace\n");
579 return -EOPNOTSUPP;
580 }
581 return 0;
582 }
583
584 static int mlx5_cmd_dr_destroy_ns(struct mlx5_flow_root_namespace *ns)
585 {
586 return mlx5dr_domain_destroy(ns->fs_dr_domain.dr_domain);
587 }
588
589 bool mlx5_fs_dr_is_supported(struct mlx5_core_dev *dev)
590 {
591 return mlx5dr_is_supported(dev);
592 }
593
594 static const struct mlx5_flow_cmds mlx5_flow_cmds_dr = {
595 .create_flow_table = mlx5_cmd_dr_create_flow_table,
596 .destroy_flow_table = mlx5_cmd_dr_destroy_flow_table,
597 .modify_flow_table = mlx5_cmd_dr_modify_flow_table,
598 .create_flow_group = mlx5_cmd_dr_create_flow_group,
599 .destroy_flow_group = mlx5_cmd_dr_destroy_flow_group,
600 .create_fte = mlx5_cmd_dr_create_fte,
601 .update_fte = mlx5_cmd_dr_update_fte,
602 .delete_fte = mlx5_cmd_dr_delete_fte,
603 .update_root_ft = mlx5_cmd_dr_update_root_ft,
604 .packet_reformat_alloc = mlx5_cmd_dr_packet_reformat_alloc,
605 .packet_reformat_dealloc = mlx5_cmd_dr_packet_reformat_dealloc,
606 .modify_header_alloc = mlx5_cmd_dr_modify_header_alloc,
607 .modify_header_dealloc = mlx5_cmd_dr_modify_header_dealloc,
608 .set_peer = mlx5_cmd_dr_set_peer,
609 .create_ns = mlx5_cmd_dr_create_ns,
610 .destroy_ns = mlx5_cmd_dr_destroy_ns,
611 };
612
613 const struct mlx5_flow_cmds *mlx5_fs_cmd_get_dr_cmds(void)
614 {
615 return &mlx5_flow_cmds_dr;
616 }