1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies */
10 static bool mlx5_dr_is_fw_table(u32 flags)
12 if (flags & MLX5_FLOW_TABLE_TERMINATION)
18 static int mlx5_cmd_dr_update_root_ft(struct mlx5_flow_root_namespace *ns,
19 struct mlx5_flow_table *ft,
23 return mlx5_fs_cmd_get_fw_cmds()->update_root_ft(ns, ft, underlay_qpn,
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)
31 struct mlx5dr_action *old_miss_action;
32 struct mlx5dr_action *action = NULL;
33 struct mlx5dr_table *next_tbl;
36 next_tbl = next_ft ? next_ft->fs_dr_table.dr_table : NULL;
38 action = mlx5dr_action_create_dest_table(next_tbl);
42 old_miss_action = ft->fs_dr_table.miss_action;
43 err = mlx5dr_table_set_miss_action(ft->fs_dr_table.dr_table, action);
45 err = mlx5dr_action_destroy(action);
48 mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
52 ft->fs_dr_table.miss_action = action;
53 if (old_miss_action) {
54 err = mlx5dr_action_destroy(old_miss_action);
56 mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
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)
68 struct mlx5dr_table *tbl;
71 if (mlx5_dr_is_fw_table(ft->flags))
72 return mlx5_fs_cmd_get_fw_cmds()->create_flow_table(ns, ft,
76 tbl = mlx5dr_table_create(ns->fs_dr_domain.dr_domain,
79 mlx5_core_err(ns->dev, "Failed creating dr flow_table\n");
83 ft->fs_dr_table.dr_table = tbl;
84 ft->id = mlx5dr_table_get_id(tbl);
87 err = set_miss_action(ns, ft, next_ft);
89 mlx5dr_table_destroy(tbl);
90 ft->fs_dr_table.dr_table = NULL;
98 static int mlx5_cmd_dr_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
99 struct mlx5_flow_table *ft)
101 struct mlx5dr_action *action = ft->fs_dr_table.miss_action;
104 if (mlx5_dr_is_fw_table(ft->flags))
105 return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_table(ns, ft);
107 err = mlx5dr_table_destroy(ft->fs_dr_table.dr_table);
109 mlx5_core_err(ns->dev, "Failed to destroy flow_table (%d)\n",
114 err = mlx5dr_action_destroy(action);
116 mlx5_core_err(ns->dev, "Failed to destroy action(%d)\n",
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)
129 return set_miss_action(ns, ft, next_ft);
132 static int mlx5_cmd_dr_create_flow_group(struct mlx5_flow_root_namespace *ns,
133 struct mlx5_flow_table *ft,
135 struct mlx5_flow_group *fg)
137 struct mlx5dr_matcher *matcher;
138 u16 priority = MLX5_GET(create_flow_group_in, in,
140 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
142 match_criteria_enable);
143 struct mlx5dr_match_parameters mask;
145 if (mlx5_dr_is_fw_table(ft->flags))
146 return mlx5_fs_cmd_get_fw_cmds()->create_flow_group(ns, ft, in,
149 mask.match_buf = MLX5_ADDR_OF(create_flow_group_in,
151 mask.match_sz = sizeof(fg->mask.match_criteria);
153 matcher = mlx5dr_matcher_create(ft->fs_dr_table.dr_table,
155 match_criteria_enable,
158 mlx5_core_err(ns->dev, "Failed creating matcher\n");
162 fg->fs_dr_matcher.dr_matcher = matcher;
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)
170 if (mlx5_dr_is_fw_table(ft->flags))
171 return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_group(ns, ft, fg);
173 return mlx5dr_matcher_destroy(fg->fs_dr_matcher.dr_matcher);
176 static struct mlx5dr_action *create_vport_action(struct mlx5dr_domain *domain,
177 struct mlx5_flow_rule *dst)
179 struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
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);
187 static struct mlx5dr_action *create_ft_action(struct mlx5_core_dev *dev,
188 struct mlx5_flow_rule *dst)
190 struct mlx5_flow_table *dest_ft = dst->dest_attr.ft;
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);
197 static struct mlx5dr_action *create_action_push_vlan(struct mlx5dr_domain *domain,
198 struct mlx5_fs_vlan *vlan)
200 u16 n_ethtype = vlan->ethtype;
201 u8 prio = vlan->prio;
205 vlan_hdr = (u32)n_ethtype << 16 | (u32)(prio) << 12 | (u32)vid;
206 return mlx5dr_action_create_push_vlan(domain, htonl(vlan_hdr));
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,
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;
231 if (mlx5_dr_is_fw_table(ft->flags))
232 return mlx5_fs_cmd_get_fw_cmds()->create_fte(ns, ft, group, fte);
234 actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX, sizeof(*actions),
239 fs_dr_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
240 sizeof(*fs_dr_actions), GFP_KERNEL);
241 if (!fs_dr_actions) {
246 match_sz = sizeof(fte->val);
248 /* The order of the actions are must to be keep, only the following
249 * order is supported by SW steering:
250 * TX: push vlan -> modify header -> encap
251 * RX: decap -> pop vlan -> modify header
253 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
254 tmp_action = create_action_push_vlan(domain, &fte->action.vlan[0]);
259 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
260 actions[num_actions++] = tmp_action;
263 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
264 tmp_action = create_action_push_vlan(domain, &fte->action.vlan[1]);
269 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
270 actions[num_actions++] = tmp_action;
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;
277 tmp_action = mlx5dr_action_create_packet_reformat(domain,
284 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
285 actions[num_actions++] = tmp_action;
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;
293 actions[num_actions++] =
294 fte->action.pkt_reformat->action.dr_action;
296 delay_encap_set = true;
299 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) {
301 mlx5dr_action_create_pop_vlan();
306 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
307 actions[num_actions++] = tmp_action;
310 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2) {
312 mlx5dr_action_create_pop_vlan();
317 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
318 actions[num_actions++] = tmp_action;
321 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
322 actions[num_actions++] =
323 fte->action.modify_hdr->action.dr_action;
326 actions[num_actions++] =
327 fte->action.pkt_reformat->action.dr_action;
329 /* The order of the actions below is not important */
331 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
332 tmp_action = mlx5dr_action_create_drop();
337 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
338 term_action = tmp_action;
341 if (fte->flow_context.flow_tag) {
343 mlx5dr_action_create_tag(fte->flow_context.flow_tag);
348 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
349 actions[num_actions++] = tmp_action;
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;
357 if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
363 case MLX5_FLOW_DESTINATION_TYPE_COUNTER:
364 id = dst->dest_attr.counter_id;
367 mlx5dr_action_create_flow_counter(id);
372 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
373 actions[num_actions++] = tmp_action;
375 case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
376 tmp_action = create_ft_action(dev, dst);
381 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
382 term_action = tmp_action;
384 case MLX5_FLOW_DESTINATION_TYPE_VPORT:
385 tmp_action = create_vport_action(domain, dst);
390 fs_dr_actions[fs_dr_num_actions++] = tmp_action;
391 term_action = tmp_action;
400 params.match_sz = match_sz;
401 params.match_buf = (u64 *)fte->val;
404 actions[num_actions++] = term_action;
406 rule = mlx5dr_rule_create(group->fs_dr_matcher.dr_matcher,
416 fte->fs_dr_rule.dr_rule = rule;
417 fte->fs_dr_rule.num_actions = fs_dr_num_actions;
418 fte->fs_dr_rule.dr_actions = fs_dr_actions;
423 for (i = 0; i < fs_dr_num_actions; i++)
424 if (!IS_ERR_OR_NULL(fs_dr_actions[i]))
425 mlx5dr_action_destroy(fs_dr_actions[i]);
427 mlx5_core_err(dev, "Failed to create dr rule err(%d)\n", err);
429 kfree(fs_dr_actions);
433 static int mlx5_cmd_dr_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
437 enum mlx5_flow_namespace_type namespace,
438 struct mlx5_pkt_reformat *pkt_reformat)
440 struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
441 struct mlx5dr_action *action;
444 switch (reformat_type) {
445 case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
446 case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
447 case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
448 dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L2;
450 case MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
451 dr_reformat = DR_ACTION_REFORMAT_TYP_TNL_L3_TO_L2;
453 case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
454 dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L3;
457 mlx5_core_err(ns->dev, "Packet-reformat not supported(%d)\n",
462 action = mlx5dr_action_create_packet_reformat(dr_domain,
467 mlx5_core_err(ns->dev, "Failed allocating packet-reformat action\n");
471 pkt_reformat->action.dr_action = action;
476 static void mlx5_cmd_dr_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
477 struct mlx5_pkt_reformat *pkt_reformat)
479 mlx5dr_action_destroy(pkt_reformat->action.dr_action);
482 static int mlx5_cmd_dr_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
483 u8 namespace, u8 num_actions,
484 void *modify_actions,
485 struct mlx5_modify_hdr *modify_hdr)
487 struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
488 struct mlx5dr_action *action;
491 actions_sz = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto) *
493 action = mlx5dr_action_create_modify_header(dr_domain, 0,
497 mlx5_core_err(ns->dev, "Failed allocating modify-header action\n");
501 modify_hdr->action.dr_action = action;
506 static void mlx5_cmd_dr_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
507 struct mlx5_modify_hdr *modify_hdr)
509 mlx5dr_action_destroy(modify_hdr->action.dr_action);
512 static int mlx5_cmd_dr_update_fte(struct mlx5_flow_root_namespace *ns,
513 struct mlx5_flow_table *ft,
514 struct mlx5_flow_group *group,
521 static int mlx5_cmd_dr_delete_fte(struct mlx5_flow_root_namespace *ns,
522 struct mlx5_flow_table *ft,
525 struct mlx5_fs_dr_rule *rule = &fte->fs_dr_rule;
529 if (mlx5_dr_is_fw_table(ft->flags))
530 return mlx5_fs_cmd_get_fw_cmds()->delete_fte(ns, ft, fte);
532 err = mlx5dr_rule_destroy(rule->dr_rule);
536 for (i = 0; i < rule->num_actions; i++)
537 if (!IS_ERR_OR_NULL(rule->dr_actions[i]))
538 mlx5dr_action_destroy(rule->dr_actions[i]);
540 kfree(rule->dr_actions);
544 static int mlx5_cmd_dr_set_peer(struct mlx5_flow_root_namespace *ns,
545 struct mlx5_flow_root_namespace *peer_ns)
547 struct mlx5dr_domain *peer_domain = NULL;
550 peer_domain = peer_ns->fs_dr_domain.dr_domain;
551 mlx5dr_domain_set_peer(ns->fs_dr_domain.dr_domain,
556 static int mlx5_cmd_dr_create_ns(struct mlx5_flow_root_namespace *ns)
558 ns->fs_dr_domain.dr_domain =
559 mlx5dr_domain_create(ns->dev,
560 MLX5DR_DOMAIN_TYPE_FDB);
561 if (!ns->fs_dr_domain.dr_domain) {
562 mlx5_core_err(ns->dev, "Failed to create dr flow namespace\n");
568 static int mlx5_cmd_dr_destroy_ns(struct mlx5_flow_root_namespace *ns)
570 return mlx5dr_domain_destroy(ns->fs_dr_domain.dr_domain);
573 bool mlx5_fs_dr_is_supported(struct mlx5_core_dev *dev)
575 return mlx5dr_is_supported(dev);
578 static const struct mlx5_flow_cmds mlx5_flow_cmds_dr = {
579 .create_flow_table = mlx5_cmd_dr_create_flow_table,
580 .destroy_flow_table = mlx5_cmd_dr_destroy_flow_table,
581 .modify_flow_table = mlx5_cmd_dr_modify_flow_table,
582 .create_flow_group = mlx5_cmd_dr_create_flow_group,
583 .destroy_flow_group = mlx5_cmd_dr_destroy_flow_group,
584 .create_fte = mlx5_cmd_dr_create_fte,
585 .update_fte = mlx5_cmd_dr_update_fte,
586 .delete_fte = mlx5_cmd_dr_delete_fte,
587 .update_root_ft = mlx5_cmd_dr_update_root_ft,
588 .packet_reformat_alloc = mlx5_cmd_dr_packet_reformat_alloc,
589 .packet_reformat_dealloc = mlx5_cmd_dr_packet_reformat_dealloc,
590 .modify_header_alloc = mlx5_cmd_dr_modify_header_alloc,
591 .modify_header_dealloc = mlx5_cmd_dr_modify_header_dealloc,
592 .set_peer = mlx5_cmd_dr_set_peer,
593 .create_ns = mlx5_cmd_dr_create_ns,
594 .destroy_ns = mlx5_cmd_dr_destroy_ns,
597 const struct mlx5_flow_cmds *mlx5_fs_cmd_get_dr_cmds(void)
599 return &mlx5_flow_cmds_dr;