1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 
4 #include <linux/if_vlan.h>
5 #include "act.h"
6 #include "pedit.h"
7 #include "en/tc_priv.h"
8 #include "en/mod_hdr.h"
9 
10 static int pedit_header_offsets[] = {
11 	[FLOW_ACT_MANGLE_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
12 	[FLOW_ACT_MANGLE_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
13 	[FLOW_ACT_MANGLE_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
14 	[FLOW_ACT_MANGLE_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
15 	[FLOW_ACT_MANGLE_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
16 };
17 
18 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
19 
20 static int
set_pedit_val(u8 hdr_type,u32 mask,u32 val,u32 offset,struct pedit_headers_action * hdrs,struct netlink_ext_ack * extack)21 set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
22 	      struct pedit_headers_action *hdrs,
23 	      struct netlink_ext_ack *extack)
24 {
25 	u32 *curr_pmask, *curr_pval;
26 
27 	curr_pmask = (u32 *)(pedit_header(&hdrs->masks, hdr_type) + offset);
28 	curr_pval  = (u32 *)(pedit_header(&hdrs->vals, hdr_type) + offset);
29 
30 	if (*curr_pmask & mask) { /* disallow acting twice on the same location */
31 		NL_SET_ERR_MSG_MOD(extack,
32 				   "curr_pmask and new mask same. Acting twice on same location");
33 		goto out_err;
34 	}
35 
36 	*curr_pmask |= mask;
37 	*curr_pval  |= (val & mask);
38 
39 	return 0;
40 
41 out_err:
42 	return -EOPNOTSUPP;
43 }
44 
45 int
mlx5e_tc_act_pedit_parse_action(struct mlx5e_priv * priv,const struct flow_action_entry * act,int namespace,struct pedit_headers_action * hdrs,struct netlink_ext_ack * extack)46 mlx5e_tc_act_pedit_parse_action(struct mlx5e_priv *priv,
47 				const struct flow_action_entry *act, int namespace,
48 				struct pedit_headers_action *hdrs,
49 				struct netlink_ext_ack *extack)
50 {
51 	u8 cmd = (act->id == FLOW_ACTION_MANGLE) ? TCA_PEDIT_KEY_EX_CMD_SET :
52 						   TCA_PEDIT_KEY_EX_CMD_ADD;
53 	u8 htype = act->mangle.htype;
54 	int err = -EOPNOTSUPP;
55 	u32 mask, val, offset;
56 
57 	if (htype == FLOW_ACT_MANGLE_UNSPEC) {
58 		NL_SET_ERR_MSG_MOD(extack, "legacy pedit isn't offloaded");
59 		goto out_err;
60 	}
61 
62 	if (!mlx5e_mod_hdr_max_actions(priv->mdev, namespace)) {
63 		NL_SET_ERR_MSG_MOD(extack, "The pedit offload action is not supported");
64 		goto out_err;
65 	}
66 
67 	mask = act->mangle.mask;
68 	val = act->mangle.val;
69 	offset = act->mangle.offset;
70 
71 	err = set_pedit_val(htype, ~mask, val, offset, &hdrs[cmd], extack);
72 	if (err)
73 		goto out_err;
74 
75 	hdrs[cmd].pedits++;
76 
77 	return 0;
78 out_err:
79 	return err;
80 }
81 
82 static int
tc_act_parse_pedit(struct mlx5e_tc_act_parse_state * parse_state,const struct flow_action_entry * act,struct mlx5e_priv * priv,struct mlx5_flow_attr * attr)83 tc_act_parse_pedit(struct mlx5e_tc_act_parse_state *parse_state,
84 		   const struct flow_action_entry *act,
85 		   struct mlx5e_priv *priv,
86 		   struct mlx5_flow_attr *attr)
87 {
88 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
89 	struct mlx5e_tc_flow *flow = parse_state->flow;
90 	enum mlx5_flow_namespace_type ns_type;
91 	int err;
92 
93 	ns_type = mlx5e_get_flow_namespace(flow);
94 
95 	err = mlx5e_tc_act_pedit_parse_action(flow->priv, act, ns_type, attr->parse_attr->hdrs,
96 					      parse_state->extack);
97 	if (err)
98 		return err;
99 
100 	attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
101 
102 	if (ns_type == MLX5_FLOW_NAMESPACE_FDB) {
103 		esw_attr->split_count = esw_attr->out_count;
104 		parse_state->if_count = 0;
105 	}
106 
107 	return 0;
108 }
109 
110 struct mlx5e_tc_act mlx5e_tc_act_pedit = {
111 	.parse_action = tc_act_parse_pedit,
112 };
113