1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/bitfield.h>
35 #include <net/pkt_cls.h>
36 
37 #include "cmsg.h"
38 #include "main.h"
39 
40 static void
41 nfp_flower_compile_meta_tci(struct nfp_flower_meta_tci *frame,
42 			    struct tc_cls_flower_offload *flow, u8 key_type,
43 			    bool mask_version)
44 {
45 	struct fl_flow_key *target = mask_version ? flow->mask : flow->key;
46 	struct flow_dissector_key_vlan *flow_vlan;
47 	u16 tmp_tci;
48 
49 	memset(frame, 0, sizeof(struct nfp_flower_meta_tci));
50 	/* Populate the metadata frame. */
51 	frame->nfp_flow_key_layer = key_type;
52 	frame->mask_id = ~0;
53 
54 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
55 		flow_vlan = skb_flow_dissector_target(flow->dissector,
56 						      FLOW_DISSECTOR_KEY_VLAN,
57 						      target);
58 		/* Populate the tci field. */
59 		if (flow_vlan->vlan_id) {
60 			tmp_tci = FIELD_PREP(NFP_FLOWER_MASK_VLAN_PRIO,
61 					     flow_vlan->vlan_priority) |
62 				  FIELD_PREP(NFP_FLOWER_MASK_VLAN_VID,
63 					     flow_vlan->vlan_id) |
64 				  NFP_FLOWER_MASK_VLAN_CFI;
65 			frame->tci = cpu_to_be16(tmp_tci);
66 		}
67 	}
68 }
69 
70 static void
71 nfp_flower_compile_ext_meta(struct nfp_flower_ext_meta *frame, u32 key_ext)
72 {
73 	frame->nfp_flow_key_layer2 = cpu_to_be32(key_ext);
74 }
75 
76 static int
77 nfp_flower_compile_port(struct nfp_flower_in_port *frame, u32 cmsg_port,
78 			bool mask_version, enum nfp_flower_tun_type tun_type)
79 {
80 	if (mask_version) {
81 		frame->in_port = cpu_to_be32(~0);
82 		return 0;
83 	}
84 
85 	if (tun_type)
86 		frame->in_port = cpu_to_be32(NFP_FL_PORT_TYPE_TUN | tun_type);
87 	else
88 		frame->in_port = cpu_to_be32(cmsg_port);
89 
90 	return 0;
91 }
92 
93 static void
94 nfp_flower_compile_mac(struct nfp_flower_mac_mpls *frame,
95 		       struct tc_cls_flower_offload *flow,
96 		       bool mask_version)
97 {
98 	struct fl_flow_key *target = mask_version ? flow->mask : flow->key;
99 	struct flow_dissector_key_eth_addrs *addr;
100 
101 	memset(frame, 0, sizeof(struct nfp_flower_mac_mpls));
102 
103 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
104 		addr = skb_flow_dissector_target(flow->dissector,
105 						 FLOW_DISSECTOR_KEY_ETH_ADDRS,
106 						 target);
107 		/* Populate mac frame. */
108 		ether_addr_copy(frame->mac_dst, &addr->dst[0]);
109 		ether_addr_copy(frame->mac_src, &addr->src[0]);
110 	}
111 
112 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_MPLS)) {
113 		struct flow_dissector_key_mpls *mpls;
114 		u32 t_mpls;
115 
116 		mpls = skb_flow_dissector_target(flow->dissector,
117 						 FLOW_DISSECTOR_KEY_MPLS,
118 						 target);
119 
120 		t_mpls = FIELD_PREP(NFP_FLOWER_MASK_MPLS_LB, mpls->mpls_label) |
121 			 FIELD_PREP(NFP_FLOWER_MASK_MPLS_TC, mpls->mpls_tc) |
122 			 FIELD_PREP(NFP_FLOWER_MASK_MPLS_BOS, mpls->mpls_bos) |
123 			 NFP_FLOWER_MASK_MPLS_Q;
124 
125 		frame->mpls_lse = cpu_to_be32(t_mpls);
126 	}
127 }
128 
129 static void
130 nfp_flower_compile_tport(struct nfp_flower_tp_ports *frame,
131 			 struct tc_cls_flower_offload *flow,
132 			 bool mask_version)
133 {
134 	struct fl_flow_key *target = mask_version ? flow->mask : flow->key;
135 	struct flow_dissector_key_ports *tp;
136 
137 	memset(frame, 0, sizeof(struct nfp_flower_tp_ports));
138 
139 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
140 		tp = skb_flow_dissector_target(flow->dissector,
141 					       FLOW_DISSECTOR_KEY_PORTS,
142 					       target);
143 		frame->port_src = tp->src;
144 		frame->port_dst = tp->dst;
145 	}
146 }
147 
148 static void
149 nfp_flower_compile_ipv4(struct nfp_flower_ipv4 *frame,
150 			struct tc_cls_flower_offload *flow,
151 			bool mask_version)
152 {
153 	struct fl_flow_key *target = mask_version ? flow->mask : flow->key;
154 	struct flow_dissector_key_ipv4_addrs *addr;
155 	struct flow_dissector_key_basic *basic;
156 
157 	memset(frame, 0, sizeof(struct nfp_flower_ipv4));
158 
159 	if (dissector_uses_key(flow->dissector,
160 			       FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
161 		addr = skb_flow_dissector_target(flow->dissector,
162 						 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
163 						 target);
164 		frame->ipv4_src = addr->src;
165 		frame->ipv4_dst = addr->dst;
166 	}
167 
168 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
169 		basic = skb_flow_dissector_target(flow->dissector,
170 						  FLOW_DISSECTOR_KEY_BASIC,
171 						  target);
172 		frame->proto = basic->ip_proto;
173 	}
174 
175 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_IP)) {
176 		struct flow_dissector_key_ip *flow_ip;
177 
178 		flow_ip = skb_flow_dissector_target(flow->dissector,
179 						    FLOW_DISSECTOR_KEY_IP,
180 						    target);
181 		frame->tos = flow_ip->tos;
182 		frame->ttl = flow_ip->ttl;
183 	}
184 
185 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_TCP)) {
186 		struct flow_dissector_key_tcp *tcp;
187 		u32 tcp_flags;
188 
189 		tcp = skb_flow_dissector_target(flow->dissector,
190 						FLOW_DISSECTOR_KEY_TCP, target);
191 		tcp_flags = be16_to_cpu(tcp->flags);
192 
193 		if (tcp_flags & TCPHDR_FIN)
194 			frame->flags |= NFP_FL_TCP_FLAG_FIN;
195 		if (tcp_flags & TCPHDR_SYN)
196 			frame->flags |= NFP_FL_TCP_FLAG_SYN;
197 		if (tcp_flags & TCPHDR_RST)
198 			frame->flags |= NFP_FL_TCP_FLAG_RST;
199 		if (tcp_flags & TCPHDR_PSH)
200 			frame->flags |= NFP_FL_TCP_FLAG_PSH;
201 		if (tcp_flags & TCPHDR_URG)
202 			frame->flags |= NFP_FL_TCP_FLAG_URG;
203 	}
204 }
205 
206 static void
207 nfp_flower_compile_ipv6(struct nfp_flower_ipv6 *frame,
208 			struct tc_cls_flower_offload *flow,
209 			bool mask_version)
210 {
211 	struct fl_flow_key *target = mask_version ? flow->mask : flow->key;
212 	struct flow_dissector_key_ipv6_addrs *addr;
213 	struct flow_dissector_key_basic *basic;
214 
215 	memset(frame, 0, sizeof(struct nfp_flower_ipv6));
216 
217 	if (dissector_uses_key(flow->dissector,
218 			       FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
219 		addr = skb_flow_dissector_target(flow->dissector,
220 						 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
221 						 target);
222 		frame->ipv6_src = addr->src;
223 		frame->ipv6_dst = addr->dst;
224 	}
225 
226 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
227 		basic = skb_flow_dissector_target(flow->dissector,
228 						  FLOW_DISSECTOR_KEY_BASIC,
229 						  target);
230 		frame->proto = basic->ip_proto;
231 	}
232 
233 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_IP)) {
234 		struct flow_dissector_key_ip *flow_ip;
235 
236 		flow_ip = skb_flow_dissector_target(flow->dissector,
237 						    FLOW_DISSECTOR_KEY_IP,
238 						    target);
239 		frame->tos = flow_ip->tos;
240 		frame->ttl = flow_ip->ttl;
241 	}
242 }
243 
244 static void
245 nfp_flower_compile_ipv4_udp_tun(struct nfp_flower_ipv4_udp_tun *frame,
246 				struct tc_cls_flower_offload *flow,
247 				bool mask_version)
248 {
249 	struct fl_flow_key *target = mask_version ? flow->mask : flow->key;
250 	struct flow_dissector_key_ipv4_addrs *tun_ips;
251 	struct flow_dissector_key_keyid *vni;
252 
253 	memset(frame, 0, sizeof(struct nfp_flower_ipv4_udp_tun));
254 
255 	if (dissector_uses_key(flow->dissector,
256 			       FLOW_DISSECTOR_KEY_ENC_KEYID)) {
257 		u32 temp_vni;
258 
259 		vni = skb_flow_dissector_target(flow->dissector,
260 						FLOW_DISSECTOR_KEY_ENC_KEYID,
261 						target);
262 		temp_vni = be32_to_cpu(vni->keyid) << NFP_FL_TUN_VNI_OFFSET;
263 		frame->tun_id = cpu_to_be32(temp_vni);
264 	}
265 
266 	if (dissector_uses_key(flow->dissector,
267 			       FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
268 		tun_ips =
269 		   skb_flow_dissector_target(flow->dissector,
270 					     FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
271 					     target);
272 		frame->ip_src = tun_ips->src;
273 		frame->ip_dst = tun_ips->dst;
274 	}
275 }
276 
277 int nfp_flower_compile_flow_match(struct tc_cls_flower_offload *flow,
278 				  struct nfp_fl_key_ls *key_ls,
279 				  struct net_device *netdev,
280 				  struct nfp_fl_payload *nfp_flow,
281 				  enum nfp_flower_tun_type tun_type)
282 {
283 	struct nfp_repr *netdev_repr;
284 	int err;
285 	u8 *ext;
286 	u8 *msk;
287 
288 	memset(nfp_flow->unmasked_data, 0, key_ls->key_size);
289 	memset(nfp_flow->mask_data, 0, key_ls->key_size);
290 
291 	ext = nfp_flow->unmasked_data;
292 	msk = nfp_flow->mask_data;
293 
294 	/* Populate Exact Metadata. */
295 	nfp_flower_compile_meta_tci((struct nfp_flower_meta_tci *)ext,
296 				    flow, key_ls->key_layer, false);
297 	/* Populate Mask Metadata. */
298 	nfp_flower_compile_meta_tci((struct nfp_flower_meta_tci *)msk,
299 				    flow, key_ls->key_layer, true);
300 	ext += sizeof(struct nfp_flower_meta_tci);
301 	msk += sizeof(struct nfp_flower_meta_tci);
302 
303 	/* Populate Extended Metadata if Required. */
304 	if (NFP_FLOWER_LAYER_EXT_META & key_ls->key_layer) {
305 		nfp_flower_compile_ext_meta((struct nfp_flower_ext_meta *)ext,
306 					    key_ls->key_layer_two);
307 		nfp_flower_compile_ext_meta((struct nfp_flower_ext_meta *)msk,
308 					    key_ls->key_layer_two);
309 		ext += sizeof(struct nfp_flower_ext_meta);
310 		msk += sizeof(struct nfp_flower_ext_meta);
311 	}
312 
313 	/* Populate Exact Port data. */
314 	err = nfp_flower_compile_port((struct nfp_flower_in_port *)ext,
315 				      nfp_repr_get_port_id(netdev),
316 				      false, tun_type);
317 	if (err)
318 		return err;
319 
320 	/* Populate Mask Port Data. */
321 	err = nfp_flower_compile_port((struct nfp_flower_in_port *)msk,
322 				      nfp_repr_get_port_id(netdev),
323 				      true, tun_type);
324 	if (err)
325 		return err;
326 
327 	ext += sizeof(struct nfp_flower_in_port);
328 	msk += sizeof(struct nfp_flower_in_port);
329 
330 	if (NFP_FLOWER_LAYER_MAC & key_ls->key_layer) {
331 		/* Populate Exact MAC Data. */
332 		nfp_flower_compile_mac((struct nfp_flower_mac_mpls *)ext,
333 				       flow, false);
334 		/* Populate Mask MAC Data. */
335 		nfp_flower_compile_mac((struct nfp_flower_mac_mpls *)msk,
336 				       flow, true);
337 		ext += sizeof(struct nfp_flower_mac_mpls);
338 		msk += sizeof(struct nfp_flower_mac_mpls);
339 	}
340 
341 	if (NFP_FLOWER_LAYER_TP & key_ls->key_layer) {
342 		/* Populate Exact TP Data. */
343 		nfp_flower_compile_tport((struct nfp_flower_tp_ports *)ext,
344 					 flow, false);
345 		/* Populate Mask TP Data. */
346 		nfp_flower_compile_tport((struct nfp_flower_tp_ports *)msk,
347 					 flow, true);
348 		ext += sizeof(struct nfp_flower_tp_ports);
349 		msk += sizeof(struct nfp_flower_tp_ports);
350 	}
351 
352 	if (NFP_FLOWER_LAYER_IPV4 & key_ls->key_layer) {
353 		/* Populate Exact IPv4 Data. */
354 		nfp_flower_compile_ipv4((struct nfp_flower_ipv4 *)ext,
355 					flow, false);
356 		/* Populate Mask IPv4 Data. */
357 		nfp_flower_compile_ipv4((struct nfp_flower_ipv4 *)msk,
358 					flow, true);
359 		ext += sizeof(struct nfp_flower_ipv4);
360 		msk += sizeof(struct nfp_flower_ipv4);
361 	}
362 
363 	if (NFP_FLOWER_LAYER_IPV6 & key_ls->key_layer) {
364 		/* Populate Exact IPv4 Data. */
365 		nfp_flower_compile_ipv6((struct nfp_flower_ipv6 *)ext,
366 					flow, false);
367 		/* Populate Mask IPv4 Data. */
368 		nfp_flower_compile_ipv6((struct nfp_flower_ipv6 *)msk,
369 					flow, true);
370 		ext += sizeof(struct nfp_flower_ipv6);
371 		msk += sizeof(struct nfp_flower_ipv6);
372 	}
373 
374 	if (key_ls->key_layer & NFP_FLOWER_LAYER_VXLAN ||
375 	    key_ls->key_layer_two & NFP_FLOWER_LAYER2_GENEVE) {
376 		__be32 tun_dst;
377 
378 		/* Populate Exact VXLAN Data. */
379 		nfp_flower_compile_ipv4_udp_tun((void *)ext, flow, false);
380 		/* Populate Mask VXLAN Data. */
381 		nfp_flower_compile_ipv4_udp_tun((void *)msk, flow, true);
382 		tun_dst = ((struct nfp_flower_ipv4_udp_tun *)ext)->ip_dst;
383 		ext += sizeof(struct nfp_flower_ipv4_udp_tun);
384 		msk += sizeof(struct nfp_flower_ipv4_udp_tun);
385 
386 		/* Configure tunnel end point MAC. */
387 		if (nfp_netdev_is_nfp_repr(netdev)) {
388 			netdev_repr = netdev_priv(netdev);
389 			nfp_tunnel_write_macs(netdev_repr->app);
390 
391 			/* Store the tunnel destination in the rule data.
392 			 * This must be present and be an exact match.
393 			 */
394 			nfp_flow->nfp_tun_ipv4_addr = tun_dst;
395 			nfp_tunnel_add_ipv4_off(netdev_repr->app, tun_dst);
396 		}
397 	}
398 
399 	return 0;
400 }
401