xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_geneve.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2018 Mellanox Technologies. */
3 
4 #include <net/geneve.h>
5 #include "lib/geneve.h"
6 #include "en/tc_tun.h"
7 
8 #define MLX5E_GENEVE_VER 0
9 
mlx5e_tc_tun_can_offload_geneve(struct mlx5e_priv * priv)10 static bool mlx5e_tc_tun_can_offload_geneve(struct mlx5e_priv *priv)
11 {
12 	return !!(MLX5_CAP_GEN(priv->mdev, flex_parser_protocols) & MLX5_FLEX_PROTO_GENEVE);
13 }
14 
mlx5e_tc_tun_calc_hlen_geneve(struct mlx5e_encap_entry * e)15 static int mlx5e_tc_tun_calc_hlen_geneve(struct mlx5e_encap_entry *e)
16 {
17 	return sizeof(struct udphdr) +
18 	       sizeof(struct genevehdr) +
19 	       e->tun_info->options_len;
20 }
21 
mlx5e_tc_tun_check_udp_dport_geneve(struct mlx5e_priv * priv,struct flow_cls_offload * f)22 static int mlx5e_tc_tun_check_udp_dport_geneve(struct mlx5e_priv *priv,
23 					       struct flow_cls_offload *f)
24 {
25 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
26 	struct netlink_ext_ack *extack = f->common.extack;
27 	struct flow_match_ports enc_ports;
28 
29 	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS))
30 		return -EOPNOTSUPP;
31 
32 	flow_rule_match_enc_ports(rule, &enc_ports);
33 
34 	/* Currently we support only default GENEVE
35 	 * port, so udp dst port must match.
36 	 */
37 	if (be16_to_cpu(enc_ports.key->dst) != GENEVE_UDP_PORT) {
38 		NL_SET_ERR_MSG_MOD(extack,
39 				   "Matched UDP dst port is not registered as a GENEVE port");
40 		netdev_warn(priv->netdev,
41 			    "UDP port %d is not registered as a GENEVE port\n",
42 			    be16_to_cpu(enc_ports.key->dst));
43 		return -EOPNOTSUPP;
44 	}
45 
46 	return 0;
47 }
48 
mlx5e_tc_tun_parse_udp_ports_geneve(struct mlx5e_priv * priv,struct mlx5_flow_spec * spec,struct flow_cls_offload * f,void * headers_c,void * headers_v)49 static int mlx5e_tc_tun_parse_udp_ports_geneve(struct mlx5e_priv *priv,
50 					       struct mlx5_flow_spec *spec,
51 					       struct flow_cls_offload *f,
52 					       void *headers_c,
53 					       void *headers_v)
54 {
55 	int err;
56 
57 	err = mlx5e_tc_tun_parse_udp_ports(priv, spec, f, headers_c, headers_v);
58 	if (err)
59 		return err;
60 
61 	return mlx5e_tc_tun_check_udp_dport_geneve(priv, f);
62 }
63 
mlx5e_tc_tun_init_encap_attr_geneve(struct net_device * tunnel_dev,struct mlx5e_priv * priv,struct mlx5e_encap_entry * e,struct netlink_ext_ack * extack)64 static int mlx5e_tc_tun_init_encap_attr_geneve(struct net_device *tunnel_dev,
65 					       struct mlx5e_priv *priv,
66 					       struct mlx5e_encap_entry *e,
67 					       struct netlink_ext_ack *extack)
68 {
69 	e->tunnel = &geneve_tunnel;
70 
71 	/* Reformat type for GENEVE encap is similar to VXLAN:
72 	 * in both cases the HW adds in the same place a
73 	 * defined encapsulation header that the SW provides.
74 	 */
75 	e->reformat_type = MLX5_REFORMAT_TYPE_L2_TO_VXLAN;
76 	return 0;
77 }
78 
mlx5e_tunnel_id_to_vni(__be64 tun_id,__u8 * vni)79 static void mlx5e_tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
80 {
81 #ifdef __BIG_ENDIAN
82 	vni[0] = (__force __u8)(tun_id >> 16);
83 	vni[1] = (__force __u8)(tun_id >> 8);
84 	vni[2] = (__force __u8)tun_id;
85 #else
86 	vni[0] = (__force __u8)((__force u64)tun_id >> 40);
87 	vni[1] = (__force __u8)((__force u64)tun_id >> 48);
88 	vni[2] = (__force __u8)((__force u64)tun_id >> 56);
89 #endif
90 }
91 
mlx5e_gen_ip_tunnel_header_geneve(char buf[],__u8 * ip_proto,struct mlx5e_encap_entry * e)92 static int mlx5e_gen_ip_tunnel_header_geneve(char buf[],
93 					     __u8 *ip_proto,
94 					     struct mlx5e_encap_entry *e)
95 {
96 	const struct ip_tunnel_info *tun_info = e->tun_info;
97 	struct udphdr *udp = (struct udphdr *)(buf);
98 	struct genevehdr *geneveh;
99 
100 	geneveh = (struct genevehdr *)((char *)udp + sizeof(struct udphdr));
101 
102 	*ip_proto = IPPROTO_UDP;
103 
104 	udp->dest = tun_info->key.tp_dst;
105 
106 	memset(geneveh, 0, sizeof(*geneveh));
107 	geneveh->ver = MLX5E_GENEVE_VER;
108 	geneveh->opt_len = tun_info->options_len / 4;
109 	geneveh->oam = test_bit(IP_TUNNEL_OAM_BIT, tun_info->key.tun_flags);
110 	geneveh->critical = test_bit(IP_TUNNEL_CRIT_OPT_BIT,
111 				     tun_info->key.tun_flags);
112 	mlx5e_tunnel_id_to_vni(tun_info->key.tun_id, geneveh->vni);
113 	geneveh->proto_type = htons(ETH_P_TEB);
114 
115 	if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, tun_info->key.tun_flags)) {
116 		if (!geneveh->opt_len)
117 			return -EOPNOTSUPP;
118 		ip_tunnel_info_opts_get(geneveh->options, tun_info);
119 	}
120 
121 	return 0;
122 }
123 
mlx5e_tc_tun_parse_geneve_vni(struct mlx5e_priv * priv,struct mlx5_flow_spec * spec,struct flow_cls_offload * f)124 static int mlx5e_tc_tun_parse_geneve_vni(struct mlx5e_priv *priv,
125 					 struct mlx5_flow_spec *spec,
126 					 struct flow_cls_offload *f)
127 {
128 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
129 	struct netlink_ext_ack *extack = f->common.extack;
130 	struct flow_match_enc_keyid enc_keyid;
131 	void *misc_c, *misc_v;
132 
133 	misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
134 	misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
135 
136 	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID))
137 		return 0;
138 
139 	flow_rule_match_enc_keyid(rule, &enc_keyid);
140 
141 	if (!enc_keyid.mask->keyid)
142 		return 0;
143 
144 	if (!MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, ft_field_support.outer_geneve_vni)) {
145 		NL_SET_ERR_MSG_MOD(extack, "Matching on GENEVE VNI is not supported");
146 		netdev_warn(priv->netdev, "Matching on GENEVE VNI is not supported\n");
147 		return -EOPNOTSUPP;
148 	}
149 
150 	MLX5_SET(fte_match_set_misc, misc_c, geneve_vni, be32_to_cpu(enc_keyid.mask->keyid));
151 	MLX5_SET(fte_match_set_misc, misc_v, geneve_vni, be32_to_cpu(enc_keyid.key->keyid));
152 
153 	return 0;
154 }
155 
mlx5e_tc_tun_parse_geneve_options(struct mlx5e_priv * priv,struct mlx5_flow_spec * spec,struct flow_cls_offload * f)156 static int mlx5e_tc_tun_parse_geneve_options(struct mlx5e_priv *priv,
157 					     struct mlx5_flow_spec *spec,
158 					     struct flow_cls_offload *f)
159 {
160 	u8 max_tlv_option_data_len = MLX5_CAP_GEN(priv->mdev, max_geneve_tlv_option_data_len);
161 	u8 max_tlv_options = MLX5_CAP_GEN(priv->mdev, max_geneve_tlv_options);
162 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
163 	struct netlink_ext_ack *extack = f->common.extack;
164 	void *misc_c, *misc_v, *misc_3_c, *misc_3_v;
165 	struct geneve_opt *option_key, *option_mask;
166 	__be32 opt_data_key = 0, opt_data_mask = 0;
167 	struct flow_match_enc_opts enc_opts;
168 	int res = 0;
169 
170 	misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
171 	misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
172 	misc_3_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_3);
173 	misc_3_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_3);
174 
175 	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS))
176 		return 0;
177 
178 	flow_rule_match_enc_opts(rule, &enc_opts);
179 
180 	if (memchr_inv(&enc_opts.mask->data, 0, sizeof(enc_opts.mask->data)) &&
181 	    !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
182 					ft_field_support.geneve_tlv_option_0_data)) {
183 		NL_SET_ERR_MSG_MOD(extack,
184 				   "Matching on GENEVE options is not supported");
185 		netdev_warn(priv->netdev,
186 			    "Matching on GENEVE options is not supported\n");
187 		return -EOPNOTSUPP;
188 	}
189 
190 	/* make sure that we're talking about GENEVE options */
191 
192 	if (enc_opts.key->dst_opt_type != IP_TUNNEL_GENEVE_OPT_BIT) {
193 		NL_SET_ERR_MSG_MOD(extack,
194 				   "Matching on GENEVE options: option type is not GENEVE");
195 		netdev_warn(priv->netdev,
196 			    "Matching on GENEVE options: option type is not GENEVE\n");
197 		return -EOPNOTSUPP;
198 	}
199 
200 	if (enc_opts.mask->len &&
201 	    !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
202 					ft_field_support.outer_geneve_opt_len)) {
203 		NL_SET_ERR_MSG_MOD(extack, "Matching on GENEVE options len is not supported");
204 		netdev_warn(priv->netdev,
205 			    "Matching on GENEVE options len is not supported\n");
206 		return -EOPNOTSUPP;
207 	}
208 
209 	/* max_geneve_tlv_option_data_len comes in multiples of 4 bytes, and it
210 	 * doesn't include the TLV option header. 'geneve_opt_len' is a total
211 	 * len of all the options, including the headers, also multiples of 4
212 	 * bytes. Len that comes from the dissector is in bytes.
213 	 */
214 
215 	if ((enc_opts.key->len / 4) > ((max_tlv_option_data_len + 1) * max_tlv_options)) {
216 		NL_SET_ERR_MSG_MOD(extack,
217 				   "Matching on GENEVE options: unsupported options len");
218 		netdev_warn(priv->netdev,
219 			    "Matching on GENEVE options: unsupported options len (len=%d)\n",
220 			    enc_opts.key->len);
221 		return -EOPNOTSUPP;
222 	}
223 
224 	MLX5_SET(fte_match_set_misc, misc_c, geneve_opt_len, enc_opts.mask->len / 4);
225 	MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len, enc_opts.key->len / 4);
226 
227 	/* we support matching on one option only, so just get it */
228 	option_key = (struct geneve_opt *)&enc_opts.key->data[0];
229 	option_mask = (struct geneve_opt *)&enc_opts.mask->data[0];
230 
231 	if (option_mask->opt_class == 0 && option_mask->type == 0 &&
232 	    !memchr_inv(option_mask->opt_data, 0, option_mask->length * 4))
233 		return 0;
234 
235 	if (option_key->length > max_tlv_option_data_len) {
236 		NL_SET_ERR_MSG_MOD(extack,
237 				   "Matching on GENEVE options: unsupported option len");
238 		netdev_warn(priv->netdev,
239 			    "Matching on GENEVE options: unsupported option len (key=%d, mask=%d)\n",
240 			    option_key->length, option_mask->length);
241 		return -EOPNOTSUPP;
242 	}
243 
244 	/* data can't be all 0 - fail to offload such rule */
245 	if (!memchr_inv(option_key->opt_data, 0, option_key->length * 4)) {
246 		NL_SET_ERR_MSG_MOD(extack,
247 				   "Matching on GENEVE options: can't match on 0 data field");
248 		netdev_warn(priv->netdev,
249 			    "Matching on GENEVE options: can't match on 0 data field\n");
250 		return -EOPNOTSUPP;
251 	}
252 
253 	/* add new GENEVE TLV options object */
254 	res = mlx5_geneve_tlv_option_add(priv->mdev->geneve, option_key);
255 	if (res) {
256 		NL_SET_ERR_MSG_MOD(extack,
257 				   "Matching on GENEVE options: failed creating TLV opt object");
258 		netdev_warn(priv->netdev,
259 			    "Matching on GENEVE options: failed creating TLV opt object (class:type:len = 0x%x:0x%x:%d)\n",
260 			    be16_to_cpu(option_key->opt_class),
261 			    option_key->type, option_key->length);
262 		return res;
263 	}
264 
265 	/* In general, after creating the object, need to query it
266 	 * in order to check which option data to set in misc3.
267 	 * But we support only geneve_tlv_option_0_data, so no
268 	 * point querying at this stage.
269 	 */
270 
271 	memcpy(&opt_data_key, option_key->opt_data, option_key->length * 4);
272 	memcpy(&opt_data_mask, option_mask->opt_data, option_mask->length * 4);
273 	MLX5_SET(fte_match_set_misc3, misc_3_v,
274 		 geneve_tlv_option_0_data, be32_to_cpu(opt_data_key));
275 	MLX5_SET(fte_match_set_misc3, misc_3_c,
276 		 geneve_tlv_option_0_data, be32_to_cpu(opt_data_mask));
277 	if (MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
278 				       ft_field_support.geneve_tlv_option_0_exist)) {
279 		MLX5_SET_TO_ONES(fte_match_set_misc, misc_c, geneve_tlv_option_0_exist);
280 		MLX5_SET_TO_ONES(fte_match_set_misc, misc_v, geneve_tlv_option_0_exist);
281 	}
282 
283 	spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS_3;
284 
285 	return 0;
286 }
287 
mlx5e_tc_tun_parse_geneve_params(struct mlx5e_priv * priv,struct mlx5_flow_spec * spec,struct flow_cls_offload * f)288 static int mlx5e_tc_tun_parse_geneve_params(struct mlx5e_priv *priv,
289 					    struct mlx5_flow_spec *spec,
290 					    struct flow_cls_offload *f)
291 {
292 	void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
293 	void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,  misc_parameters);
294 	struct netlink_ext_ack *extack = f->common.extack;
295 
296 	/* match on OAM - packets with OAM bit on should NOT be offloaded */
297 
298 	if (!MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, ft_field_support.outer_geneve_oam)) {
299 		NL_SET_ERR_MSG_MOD(extack, "Matching on GENEVE OAM is not supported");
300 		netdev_warn(priv->netdev, "Matching on GENEVE OAM is not supported\n");
301 		return -EOPNOTSUPP;
302 	}
303 	MLX5_SET_TO_ONES(fte_match_set_misc, misc_c, geneve_oam);
304 	MLX5_SET(fte_match_set_misc, misc_v, geneve_oam, 0);
305 
306 	/* Match on GENEVE protocol. We support only Transparent Eth Bridge. */
307 
308 	if (MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
309 				       ft_field_support.outer_geneve_protocol_type)) {
310 		MLX5_SET_TO_ONES(fte_match_set_misc, misc_c, geneve_protocol_type);
311 		MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type, ETH_P_TEB);
312 	}
313 
314 	spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS;
315 
316 	return 0;
317 }
318 
mlx5e_tc_tun_parse_geneve(struct mlx5e_priv * priv,struct mlx5_flow_spec * spec,struct flow_cls_offload * f,void * headers_c,void * headers_v)319 static int mlx5e_tc_tun_parse_geneve(struct mlx5e_priv *priv,
320 				     struct mlx5_flow_spec *spec,
321 				     struct flow_cls_offload *f,
322 				     void *headers_c,
323 				     void *headers_v)
324 {
325 	int err;
326 
327 	err = mlx5e_tc_tun_parse_geneve_params(priv, spec, f);
328 	if (err)
329 		return err;
330 
331 	err = mlx5e_tc_tun_parse_geneve_vni(priv, spec, f);
332 	if (err)
333 		return err;
334 
335 	return mlx5e_tc_tun_parse_geneve_options(priv, spec, f);
336 }
337 
mlx5e_tc_tun_encap_info_equal_geneve(struct mlx5e_encap_key * a,struct mlx5e_encap_key * b)338 static bool mlx5e_tc_tun_encap_info_equal_geneve(struct mlx5e_encap_key *a,
339 						 struct mlx5e_encap_key *b)
340 {
341 	return mlx5e_tc_tun_encap_info_equal_options(a, b,
342 						     IP_TUNNEL_GENEVE_OPT_BIT);
343 }
344 
345 struct mlx5e_tc_tunnel geneve_tunnel = {
346 	.tunnel_type          = MLX5E_TC_TUNNEL_TYPE_GENEVE,
347 	.match_level          = MLX5_MATCH_L4,
348 	.can_offload          = mlx5e_tc_tun_can_offload_geneve,
349 	.calc_hlen            = mlx5e_tc_tun_calc_hlen_geneve,
350 	.init_encap_attr      = mlx5e_tc_tun_init_encap_attr_geneve,
351 	.generate_ip_tun_hdr  = mlx5e_gen_ip_tunnel_header_geneve,
352 	.parse_udp_ports      = mlx5e_tc_tun_parse_udp_ports_geneve,
353 	.parse_tunnel         = mlx5e_tc_tun_parse_geneve,
354 	.encap_info_equal     = mlx5e_tc_tun_encap_info_equal_geneve,
355 };
356