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) ? 0 : 1;
52 	u8 htype = act->mangle.htype;
53 	int err = -EOPNOTSUPP;
54 	u32 mask, val, offset;
55 
56 	if (htype == FLOW_ACT_MANGLE_UNSPEC) {
57 		NL_SET_ERR_MSG_MOD(extack, "legacy pedit isn't offloaded");
58 		goto out_err;
59 	}
60 
61 	if (!mlx5e_mod_hdr_max_actions(priv->mdev, namespace)) {
62 		NL_SET_ERR_MSG_MOD(extack, "The pedit offload action is not supported");
63 		goto out_err;
64 	}
65 
66 	mask = act->mangle.mask;
67 	val = act->mangle.val;
68 	offset = act->mangle.offset;
69 
70 	err = set_pedit_val(htype, ~mask, val, offset, &hdrs[cmd], extack);
71 	if (err)
72 		goto out_err;
73 
74 	hdrs[cmd].pedits++;
75 
76 	return 0;
77 out_err:
78 	return err;
79 }
80 
81 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)82 tc_act_parse_pedit(struct mlx5e_tc_act_parse_state *parse_state,
83 		   const struct flow_action_entry *act,
84 		   struct mlx5e_priv *priv,
85 		   struct mlx5_flow_attr *attr)
86 {
87 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
88 	struct mlx5e_tc_flow *flow = parse_state->flow;
89 	enum mlx5_flow_namespace_type ns_type;
90 	int err;
91 
92 	ns_type = mlx5e_get_flow_namespace(flow);
93 
94 	err = mlx5e_tc_act_pedit_parse_action(flow->priv, act, ns_type, attr->parse_attr->hdrs,
95 					      parse_state->extack);
96 	if (err)
97 		return err;
98 
99 	attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
100 
101 	if (ns_type == MLX5_FLOW_NAMESPACE_FDB) {
102 		esw_attr->split_count = esw_attr->out_count;
103 		parse_state->if_count = 0;
104 	}
105 
106 	return 0;
107 }
108 
109 struct mlx5e_tc_act mlx5e_tc_act_pedit = {
110 	.parse_action = tc_act_parse_pedit,
111 };
112