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 bool
tc_act_can_offload_pedit(struct mlx5e_tc_act_parse_state * parse_state,const struct flow_action_entry * act,int act_index,struct mlx5_flow_attr * attr)82 tc_act_can_offload_pedit(struct mlx5e_tc_act_parse_state *parse_state,
83 			 const struct flow_action_entry *act,
84 			 int act_index,
85 			 struct mlx5_flow_attr *attr)
86 {
87 	return true;
88 }
89 
90 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)91 tc_act_parse_pedit(struct mlx5e_tc_act_parse_state *parse_state,
92 		   const struct flow_action_entry *act,
93 		   struct mlx5e_priv *priv,
94 		   struct mlx5_flow_attr *attr)
95 {
96 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
97 	struct mlx5e_tc_flow *flow = parse_state->flow;
98 	enum mlx5_flow_namespace_type ns_type;
99 	int err;
100 
101 	ns_type = mlx5e_get_flow_namespace(flow);
102 
103 	err = mlx5e_tc_act_pedit_parse_action(flow->priv, act, ns_type, attr->parse_attr->hdrs,
104 					      parse_state->extack);
105 	if (err)
106 		return err;
107 
108 	attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
109 
110 	if (ns_type == MLX5_FLOW_NAMESPACE_FDB)
111 		esw_attr->split_count = esw_attr->out_count;
112 
113 	return 0;
114 }
115 
116 struct mlx5e_tc_act mlx5e_tc_act_pedit = {
117 	.can_offload = tc_act_can_offload_pedit,
118 	.parse_action = tc_act_parse_pedit,
119 };
120