• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2021, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_tc_lib.h"
6 #include "ice_fltr.h"
7 #include "ice_lib.h"
8 #include "ice_protocol_type.h"
9 
10 /**
11  * ice_tc_count_lkups - determine lookup count for switch filter
12  * @flags: TC-flower flags
13  * @headers: Pointer to TC flower filter header structure
14  * @fltr: Pointer to outer TC filter structure
15  *
16  * Determine lookup count based on TC flower input for switch filter.
17  */
18 static int
ice_tc_count_lkups(u32 flags,struct ice_tc_flower_lyr_2_4_hdrs * headers,struct ice_tc_flower_fltr * fltr)19 ice_tc_count_lkups(u32 flags, struct ice_tc_flower_lyr_2_4_hdrs *headers,
20 		   struct ice_tc_flower_fltr *fltr)
21 {
22 	int lkups_cnt = 0;
23 
24 	if (flags & ICE_TC_FLWR_FIELD_TENANT_ID)
25 		lkups_cnt++;
26 
27 	if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC)
28 		lkups_cnt++;
29 
30 	if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS)
31 		lkups_cnt++;
32 
33 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
34 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
35 		     ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
36 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV6))
37 		lkups_cnt++;
38 
39 	if (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
40 		     ICE_TC_FLWR_FIELD_ENC_IP_TTL))
41 		lkups_cnt++;
42 
43 	if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT)
44 		lkups_cnt++;
45 
46 	if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID)
47 		lkups_cnt++;
48 
49 	/* are MAC fields specified? */
50 	if (flags & (ICE_TC_FLWR_FIELD_DST_MAC | ICE_TC_FLWR_FIELD_SRC_MAC))
51 		lkups_cnt++;
52 
53 	/* is VLAN specified? */
54 	if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO))
55 		lkups_cnt++;
56 
57 	/* is CVLAN specified? */
58 	if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO))
59 		lkups_cnt++;
60 
61 	/* are PPPoE options specified? */
62 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
63 		     ICE_TC_FLWR_FIELD_PPP_PROTO))
64 		lkups_cnt++;
65 
66 	/* are IPv[4|6] fields specified? */
67 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 | ICE_TC_FLWR_FIELD_SRC_IPV4 |
68 		     ICE_TC_FLWR_FIELD_DEST_IPV6 | ICE_TC_FLWR_FIELD_SRC_IPV6))
69 		lkups_cnt++;
70 
71 	if (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))
72 		lkups_cnt++;
73 
74 	/* are L2TPv3 options specified? */
75 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID)
76 		lkups_cnt++;
77 
78 	/* is L4 (TCP/UDP/any other L4 protocol fields) specified? */
79 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
80 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT))
81 		lkups_cnt++;
82 
83 	return lkups_cnt;
84 }
85 
ice_proto_type_from_mac(bool inner)86 static enum ice_protocol_type ice_proto_type_from_mac(bool inner)
87 {
88 	return inner ? ICE_MAC_IL : ICE_MAC_OFOS;
89 }
90 
ice_proto_type_from_etype(bool inner)91 static enum ice_protocol_type ice_proto_type_from_etype(bool inner)
92 {
93 	return inner ? ICE_ETYPE_IL : ICE_ETYPE_OL;
94 }
95 
ice_proto_type_from_ipv4(bool inner)96 static enum ice_protocol_type ice_proto_type_from_ipv4(bool inner)
97 {
98 	return inner ? ICE_IPV4_IL : ICE_IPV4_OFOS;
99 }
100 
ice_proto_type_from_ipv6(bool inner)101 static enum ice_protocol_type ice_proto_type_from_ipv6(bool inner)
102 {
103 	return inner ? ICE_IPV6_IL : ICE_IPV6_OFOS;
104 }
105 
ice_proto_type_from_l4_port(u16 ip_proto)106 static enum ice_protocol_type ice_proto_type_from_l4_port(u16 ip_proto)
107 {
108 	switch (ip_proto) {
109 	case IPPROTO_TCP:
110 		return ICE_TCP_IL;
111 	case IPPROTO_UDP:
112 		return ICE_UDP_ILOS;
113 	}
114 
115 	return 0;
116 }
117 
118 static enum ice_protocol_type
ice_proto_type_from_tunnel(enum ice_tunnel_type type)119 ice_proto_type_from_tunnel(enum ice_tunnel_type type)
120 {
121 	switch (type) {
122 	case TNL_VXLAN:
123 		return ICE_VXLAN;
124 	case TNL_GENEVE:
125 		return ICE_GENEVE;
126 	case TNL_GRETAP:
127 		return ICE_NVGRE;
128 	case TNL_GTPU:
129 		/* NO_PAY profiles will not work with GTP-U */
130 		return ICE_GTP;
131 	case TNL_GTPC:
132 		return ICE_GTP_NO_PAY;
133 	default:
134 		return 0;
135 	}
136 }
137 
138 static enum ice_sw_tunnel_type
ice_sw_type_from_tunnel(enum ice_tunnel_type type)139 ice_sw_type_from_tunnel(enum ice_tunnel_type type)
140 {
141 	switch (type) {
142 	case TNL_VXLAN:
143 		return ICE_SW_TUN_VXLAN;
144 	case TNL_GENEVE:
145 		return ICE_SW_TUN_GENEVE;
146 	case TNL_GRETAP:
147 		return ICE_SW_TUN_NVGRE;
148 	case TNL_GTPU:
149 		return ICE_SW_TUN_GTPU;
150 	case TNL_GTPC:
151 		return ICE_SW_TUN_GTPC;
152 	default:
153 		return ICE_NON_TUN;
154 	}
155 }
156 
ice_check_supported_vlan_tpid(u16 vlan_tpid)157 static u16 ice_check_supported_vlan_tpid(u16 vlan_tpid)
158 {
159 	switch (vlan_tpid) {
160 	case ETH_P_8021Q:
161 	case ETH_P_8021AD:
162 	case ETH_P_QINQ1:
163 		return vlan_tpid;
164 	default:
165 		return 0;
166 	}
167 }
168 
169 static int
ice_tc_fill_tunnel_outer(u32 flags,struct ice_tc_flower_fltr * fltr,struct ice_adv_lkup_elem * list)170 ice_tc_fill_tunnel_outer(u32 flags, struct ice_tc_flower_fltr *fltr,
171 			 struct ice_adv_lkup_elem *list)
172 {
173 	struct ice_tc_flower_lyr_2_4_hdrs *hdr = &fltr->outer_headers;
174 	int i = 0;
175 
176 	if (flags & ICE_TC_FLWR_FIELD_TENANT_ID) {
177 		u32 tenant_id;
178 
179 		list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
180 		switch (fltr->tunnel_type) {
181 		case TNL_VXLAN:
182 		case TNL_GENEVE:
183 			tenant_id = be32_to_cpu(fltr->tenant_id) << 8;
184 			list[i].h_u.tnl_hdr.vni = cpu_to_be32(tenant_id);
185 			memcpy(&list[i].m_u.tnl_hdr.vni, "\xff\xff\xff\x00", 4);
186 			i++;
187 			break;
188 		case TNL_GRETAP:
189 			list[i].h_u.nvgre_hdr.tni_flow = fltr->tenant_id;
190 			memcpy(&list[i].m_u.nvgre_hdr.tni_flow,
191 			       "\xff\xff\xff\xff", 4);
192 			i++;
193 			break;
194 		case TNL_GTPC:
195 		case TNL_GTPU:
196 			list[i].h_u.gtp_hdr.teid = fltr->tenant_id;
197 			memcpy(&list[i].m_u.gtp_hdr.teid,
198 			       "\xff\xff\xff\xff", 4);
199 			i++;
200 			break;
201 		default:
202 			break;
203 		}
204 	}
205 
206 	if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC) {
207 		list[i].type = ice_proto_type_from_mac(false);
208 		ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
209 				hdr->l2_key.dst_mac);
210 		ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
211 				hdr->l2_mask.dst_mac);
212 		i++;
213 	}
214 
215 	if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS &&
216 	    (fltr->tunnel_type == TNL_GTPU || fltr->tunnel_type == TNL_GTPC)) {
217 		list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
218 
219 		if (fltr->gtp_pdu_info_masks.pdu_type) {
220 			list[i].h_u.gtp_hdr.pdu_type =
221 				fltr->gtp_pdu_info_keys.pdu_type << 4;
222 			memcpy(&list[i].m_u.gtp_hdr.pdu_type, "\xf0", 1);
223 		}
224 
225 		if (fltr->gtp_pdu_info_masks.qfi) {
226 			list[i].h_u.gtp_hdr.qfi = fltr->gtp_pdu_info_keys.qfi;
227 			memcpy(&list[i].m_u.gtp_hdr.qfi, "\x3f", 1);
228 		}
229 
230 		i++;
231 	}
232 
233 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
234 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV4)) {
235 		list[i].type = ice_proto_type_from_ipv4(false);
236 
237 		if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV4) {
238 			list[i].h_u.ipv4_hdr.src_addr = hdr->l3_key.src_ipv4;
239 			list[i].m_u.ipv4_hdr.src_addr = hdr->l3_mask.src_ipv4;
240 		}
241 		if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV4) {
242 			list[i].h_u.ipv4_hdr.dst_addr = hdr->l3_key.dst_ipv4;
243 			list[i].m_u.ipv4_hdr.dst_addr = hdr->l3_mask.dst_ipv4;
244 		}
245 		i++;
246 	}
247 
248 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
249 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV6)) {
250 		list[i].type = ice_proto_type_from_ipv6(false);
251 
252 		if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV6) {
253 			memcpy(&list[i].h_u.ipv6_hdr.src_addr,
254 			       &hdr->l3_key.src_ipv6_addr,
255 			       sizeof(hdr->l3_key.src_ipv6_addr));
256 			memcpy(&list[i].m_u.ipv6_hdr.src_addr,
257 			       &hdr->l3_mask.src_ipv6_addr,
258 			       sizeof(hdr->l3_mask.src_ipv6_addr));
259 		}
260 		if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV6) {
261 			memcpy(&list[i].h_u.ipv6_hdr.dst_addr,
262 			       &hdr->l3_key.dst_ipv6_addr,
263 			       sizeof(hdr->l3_key.dst_ipv6_addr));
264 			memcpy(&list[i].m_u.ipv6_hdr.dst_addr,
265 			       &hdr->l3_mask.dst_ipv6_addr,
266 			       sizeof(hdr->l3_mask.dst_ipv6_addr));
267 		}
268 		i++;
269 	}
270 
271 	if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IP) &&
272 	    (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
273 		      ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
274 		list[i].type = ice_proto_type_from_ipv4(false);
275 
276 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
277 			list[i].h_u.ipv4_hdr.tos = hdr->l3_key.tos;
278 			list[i].m_u.ipv4_hdr.tos = hdr->l3_mask.tos;
279 		}
280 
281 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
282 			list[i].h_u.ipv4_hdr.time_to_live = hdr->l3_key.ttl;
283 			list[i].m_u.ipv4_hdr.time_to_live = hdr->l3_mask.ttl;
284 		}
285 
286 		i++;
287 	}
288 
289 	if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IPV6) &&
290 	    (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
291 		      ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
292 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
293 
294 		hdr_h = &list[i].h_u.ipv6_hdr;
295 		hdr_m = &list[i].m_u.ipv6_hdr;
296 		list[i].type = ice_proto_type_from_ipv6(false);
297 
298 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
299 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
300 					   hdr->l3_key.tos,
301 					   ICE_IPV6_HDR_TC_MASK);
302 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
303 					   hdr->l3_mask.tos,
304 					   ICE_IPV6_HDR_TC_MASK);
305 		}
306 
307 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
308 			hdr_h->hop_limit = hdr->l3_key.ttl;
309 			hdr_m->hop_limit = hdr->l3_mask.ttl;
310 		}
311 
312 		i++;
313 	}
314 
315 	if ((flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT) &&
316 	    hdr->l3_key.ip_proto == IPPROTO_UDP) {
317 		list[i].type = ICE_UDP_OF;
318 		list[i].h_u.l4_hdr.dst_port = hdr->l4_key.dst_port;
319 		list[i].m_u.l4_hdr.dst_port = hdr->l4_mask.dst_port;
320 		i++;
321 	}
322 
323 	return i;
324 }
325 
326 /**
327  * ice_tc_fill_rules - fill filter rules based on TC fltr
328  * @hw: pointer to HW structure
329  * @flags: tc flower field flags
330  * @tc_fltr: pointer to TC flower filter
331  * @list: list of advance rule elements
332  * @rule_info: pointer to information about rule
333  * @l4_proto: pointer to information such as L4 proto type
334  *
335  * Fill ice_adv_lkup_elem list based on TC flower flags and
336  * TC flower headers. This list should be used to add
337  * advance filter in hardware.
338  */
339 static int
ice_tc_fill_rules(struct ice_hw * hw,u32 flags,struct ice_tc_flower_fltr * tc_fltr,struct ice_adv_lkup_elem * list,struct ice_adv_rule_info * rule_info,u16 * l4_proto)340 ice_tc_fill_rules(struct ice_hw *hw, u32 flags,
341 		  struct ice_tc_flower_fltr *tc_fltr,
342 		  struct ice_adv_lkup_elem *list,
343 		  struct ice_adv_rule_info *rule_info,
344 		  u16 *l4_proto)
345 {
346 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
347 	bool inner = false;
348 	u16 vlan_tpid = 0;
349 	int i = 0;
350 
351 	rule_info->vlan_type = vlan_tpid;
352 
353 	rule_info->tun_type = ice_sw_type_from_tunnel(tc_fltr->tunnel_type);
354 	if (tc_fltr->tunnel_type != TNL_LAST) {
355 		i = ice_tc_fill_tunnel_outer(flags, tc_fltr, list);
356 
357 		headers = &tc_fltr->inner_headers;
358 		inner = true;
359 	}
360 
361 	if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID) {
362 		list[i].type = ice_proto_type_from_etype(inner);
363 		list[i].h_u.ethertype.ethtype_id = headers->l2_key.n_proto;
364 		list[i].m_u.ethertype.ethtype_id = headers->l2_mask.n_proto;
365 		i++;
366 	}
367 
368 	if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
369 		     ICE_TC_FLWR_FIELD_SRC_MAC)) {
370 		struct ice_tc_l2_hdr *l2_key, *l2_mask;
371 
372 		l2_key = &headers->l2_key;
373 		l2_mask = &headers->l2_mask;
374 
375 		list[i].type = ice_proto_type_from_mac(inner);
376 		if (flags & ICE_TC_FLWR_FIELD_DST_MAC) {
377 			ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
378 					l2_key->dst_mac);
379 			ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
380 					l2_mask->dst_mac);
381 		}
382 		if (flags & ICE_TC_FLWR_FIELD_SRC_MAC) {
383 			ether_addr_copy(list[i].h_u.eth_hdr.src_addr,
384 					l2_key->src_mac);
385 			ether_addr_copy(list[i].m_u.eth_hdr.src_addr,
386 					l2_mask->src_mac);
387 		}
388 		i++;
389 	}
390 
391 	/* copy VLAN info */
392 	if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO)) {
393 		vlan_tpid = be16_to_cpu(headers->vlan_hdr.vlan_tpid);
394 		rule_info->vlan_type =
395 				ice_check_supported_vlan_tpid(vlan_tpid);
396 
397 		if (flags & ICE_TC_FLWR_FIELD_CVLAN)
398 			list[i].type = ICE_VLAN_EX;
399 		else
400 			list[i].type = ICE_VLAN_OFOS;
401 
402 		if (flags & ICE_TC_FLWR_FIELD_VLAN) {
403 			list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
404 			list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
405 		}
406 
407 		if (flags & ICE_TC_FLWR_FIELD_VLAN_PRIO) {
408 			if (flags & ICE_TC_FLWR_FIELD_VLAN) {
409 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
410 			} else {
411 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
412 				list[i].h_u.vlan_hdr.vlan = 0;
413 			}
414 			list[i].h_u.vlan_hdr.vlan |=
415 				headers->vlan_hdr.vlan_prio;
416 		}
417 
418 		i++;
419 	}
420 
421 	if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO)) {
422 		list[i].type = ICE_VLAN_IN;
423 
424 		if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
425 			list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
426 			list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
427 		}
428 
429 		if (flags & ICE_TC_FLWR_FIELD_CVLAN_PRIO) {
430 			if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
431 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
432 			} else {
433 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
434 				list[i].h_u.vlan_hdr.vlan = 0;
435 			}
436 			list[i].h_u.vlan_hdr.vlan |=
437 				headers->cvlan_hdr.vlan_prio;
438 		}
439 
440 		i++;
441 	}
442 
443 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
444 		     ICE_TC_FLWR_FIELD_PPP_PROTO)) {
445 		struct ice_pppoe_hdr *vals, *masks;
446 
447 		vals = &list[i].h_u.pppoe_hdr;
448 		masks = &list[i].m_u.pppoe_hdr;
449 
450 		list[i].type = ICE_PPPOE;
451 
452 		if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
453 			vals->session_id = headers->pppoe_hdr.session_id;
454 			masks->session_id = cpu_to_be16(0xFFFF);
455 		}
456 
457 		if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
458 			vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
459 			masks->ppp_prot_id = cpu_to_be16(0xFFFF);
460 		}
461 
462 		i++;
463 	}
464 
465 	/* copy L3 (IPv[4|6]: src, dest) address */
466 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
467 		     ICE_TC_FLWR_FIELD_SRC_IPV4)) {
468 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
469 
470 		list[i].type = ice_proto_type_from_ipv4(inner);
471 		l3_key = &headers->l3_key;
472 		l3_mask = &headers->l3_mask;
473 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
474 			list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
475 			list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
476 		}
477 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
478 			list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
479 			list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
480 		}
481 		i++;
482 	} else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
483 			    ICE_TC_FLWR_FIELD_SRC_IPV6)) {
484 		struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
485 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
486 
487 		list[i].type = ice_proto_type_from_ipv6(inner);
488 		ipv6_hdr = &list[i].h_u.ipv6_hdr;
489 		ipv6_mask = &list[i].m_u.ipv6_hdr;
490 		l3_key = &headers->l3_key;
491 		l3_mask = &headers->l3_mask;
492 
493 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
494 			memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
495 			       sizeof(l3_key->dst_ipv6_addr));
496 			memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
497 			       sizeof(l3_mask->dst_ipv6_addr));
498 		}
499 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
500 			memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
501 			       sizeof(l3_key->src_ipv6_addr));
502 			memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
503 			       sizeof(l3_mask->src_ipv6_addr));
504 		}
505 		i++;
506 	}
507 
508 	if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
509 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
510 		list[i].type = ice_proto_type_from_ipv4(inner);
511 
512 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
513 			list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
514 			list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
515 		}
516 
517 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
518 			list[i].h_u.ipv4_hdr.time_to_live =
519 				headers->l3_key.ttl;
520 			list[i].m_u.ipv4_hdr.time_to_live =
521 				headers->l3_mask.ttl;
522 		}
523 
524 		i++;
525 	}
526 
527 	if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
528 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
529 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
530 
531 		hdr_h = &list[i].h_u.ipv6_hdr;
532 		hdr_m = &list[i].m_u.ipv6_hdr;
533 		list[i].type = ice_proto_type_from_ipv6(inner);
534 
535 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
536 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
537 					   headers->l3_key.tos,
538 					   ICE_IPV6_HDR_TC_MASK);
539 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
540 					   headers->l3_mask.tos,
541 					   ICE_IPV6_HDR_TC_MASK);
542 		}
543 
544 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
545 			hdr_h->hop_limit = headers->l3_key.ttl;
546 			hdr_m->hop_limit = headers->l3_mask.ttl;
547 		}
548 
549 		i++;
550 	}
551 
552 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
553 		list[i].type = ICE_L2TPV3;
554 
555 		list[i].h_u.l2tpv3_sess_hdr.session_id =
556 			headers->l2tpv3_hdr.session_id;
557 		list[i].m_u.l2tpv3_sess_hdr.session_id =
558 			cpu_to_be32(0xFFFFFFFF);
559 
560 		i++;
561 	}
562 
563 	/* copy L4 (src, dest) port */
564 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
565 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
566 		struct ice_tc_l4_hdr *l4_key, *l4_mask;
567 
568 		list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
569 		l4_key = &headers->l4_key;
570 		l4_mask = &headers->l4_mask;
571 
572 		if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
573 			list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
574 			list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
575 		}
576 		if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
577 			list[i].h_u.l4_hdr.src_port = l4_key->src_port;
578 			list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
579 		}
580 		i++;
581 	}
582 
583 	return i;
584 }
585 
586 /**
587  * ice_tc_tun_get_type - get the tunnel type
588  * @tunnel_dev: ptr to tunnel device
589  *
590  * This function detects appropriate tunnel_type if specified device is
591  * tunnel device such as VXLAN/Geneve
592  */
ice_tc_tun_get_type(struct net_device * tunnel_dev)593 static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
594 {
595 	if (netif_is_vxlan(tunnel_dev))
596 		return TNL_VXLAN;
597 	if (netif_is_geneve(tunnel_dev))
598 		return TNL_GENEVE;
599 	if (netif_is_gretap(tunnel_dev) ||
600 	    netif_is_ip6gretap(tunnel_dev))
601 		return TNL_GRETAP;
602 
603 	/* Assume GTP-U by default in case of GTP netdev.
604 	 * GTP-C may be selected later, based on enc_dst_port.
605 	 */
606 	if (netif_is_gtp(tunnel_dev))
607 		return TNL_GTPU;
608 	return TNL_LAST;
609 }
610 
ice_is_tunnel_supported(struct net_device * dev)611 bool ice_is_tunnel_supported(struct net_device *dev)
612 {
613 	return ice_tc_tun_get_type(dev) != TNL_LAST;
614 }
615 
616 static int
ice_eswitch_tc_parse_action(struct ice_tc_flower_fltr * fltr,struct flow_action_entry * act)617 ice_eswitch_tc_parse_action(struct ice_tc_flower_fltr *fltr,
618 			    struct flow_action_entry *act)
619 {
620 	struct ice_repr *repr;
621 
622 	switch (act->id) {
623 	case FLOW_ACTION_DROP:
624 		fltr->action.fltr_act = ICE_DROP_PACKET;
625 		break;
626 
627 	case FLOW_ACTION_REDIRECT:
628 		fltr->action.fltr_act = ICE_FWD_TO_VSI;
629 
630 		if (ice_is_port_repr_netdev(act->dev)) {
631 			repr = ice_netdev_to_repr(act->dev);
632 
633 			fltr->dest_vsi = repr->src_vsi;
634 			fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
635 		} else if (netif_is_ice(act->dev) ||
636 			   ice_is_tunnel_supported(act->dev)) {
637 			fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
638 		} else {
639 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported netdevice in switchdev mode");
640 			return -EINVAL;
641 		}
642 
643 		break;
644 
645 	default:
646 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
647 		return -EINVAL;
648 	}
649 
650 	return 0;
651 }
652 
653 static int
ice_eswitch_add_tc_fltr(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr)654 ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
655 {
656 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
657 	struct ice_adv_rule_info rule_info = { 0 };
658 	struct ice_rule_query_data rule_added;
659 	struct ice_hw *hw = &vsi->back->hw;
660 	struct ice_adv_lkup_elem *list;
661 	u32 flags = fltr->flags;
662 	int lkups_cnt;
663 	int ret;
664 	int i;
665 
666 	if (!flags || (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT)) {
667 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
668 		return -EOPNOTSUPP;
669 	}
670 
671 	lkups_cnt = ice_tc_count_lkups(flags, headers, fltr);
672 	list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
673 	if (!list)
674 		return -ENOMEM;
675 
676 	i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
677 	if (i != lkups_cnt) {
678 		ret = -EINVAL;
679 		goto exit;
680 	}
681 
682 	/* egress traffic is always redirect to uplink */
683 	if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
684 		fltr->dest_vsi = vsi->back->switchdev.uplink_vsi;
685 
686 	rule_info.sw_act.fltr_act = fltr->action.fltr_act;
687 	if (fltr->action.fltr_act != ICE_DROP_PACKET)
688 		rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
689 	/* For now, making priority to be highest, and it also becomes
690 	 * the priority for recipe which will get created as a result of
691 	 * new extraction sequence based on input set.
692 	 * Priority '7' is max val for switch recipe, higher the number
693 	 * results into order of switch rule evaluation.
694 	 */
695 	rule_info.priority = 7;
696 	rule_info.flags_info.act_valid = true;
697 
698 	if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
699 		rule_info.sw_act.flag |= ICE_FLTR_RX;
700 		rule_info.sw_act.src = hw->pf_id;
701 		rule_info.rx = true;
702 		rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
703 	} else {
704 		rule_info.sw_act.flag |= ICE_FLTR_TX;
705 		rule_info.sw_act.src = vsi->idx;
706 		rule_info.rx = false;
707 		rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
708 	}
709 
710 	/* specify the cookie as filter_rule_id */
711 	rule_info.fltr_rule_id = fltr->cookie;
712 
713 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
714 	if (ret == -EEXIST) {
715 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
716 		ret = -EINVAL;
717 		goto exit;
718 	} else if (ret) {
719 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
720 		goto exit;
721 	}
722 
723 	/* store the output params, which are needed later for removing
724 	 * advanced switch filter
725 	 */
726 	fltr->rid = rule_added.rid;
727 	fltr->rule_id = rule_added.rule_id;
728 	fltr->dest_id = rule_added.vsi_handle;
729 
730 exit:
731 	kfree(list);
732 	return ret;
733 }
734 
735 /**
736  * ice_add_tc_flower_adv_fltr - add appropriate filter rules
737  * @vsi: Pointer to VSI
738  * @tc_fltr: Pointer to TC flower filter structure
739  *
740  * based on filter parameters using Advance recipes supported
741  * by OS package.
742  */
743 static int
ice_add_tc_flower_adv_fltr(struct ice_vsi * vsi,struct ice_tc_flower_fltr * tc_fltr)744 ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
745 			   struct ice_tc_flower_fltr *tc_fltr)
746 {
747 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
748 	struct ice_adv_rule_info rule_info = {0};
749 	struct ice_rule_query_data rule_added;
750 	struct ice_adv_lkup_elem *list;
751 	struct ice_pf *pf = vsi->back;
752 	struct ice_hw *hw = &pf->hw;
753 	u32 flags = tc_fltr->flags;
754 	struct ice_vsi *ch_vsi;
755 	struct device *dev;
756 	u16 lkups_cnt = 0;
757 	u16 l4_proto = 0;
758 	int ret = 0;
759 	u16 i = 0;
760 
761 	dev = ice_pf_to_dev(pf);
762 	if (ice_is_safe_mode(pf)) {
763 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
764 		return -EOPNOTSUPP;
765 	}
766 
767 	if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
768 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
769 				ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
770 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
771 				ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
772 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
773 		return -EOPNOTSUPP;
774 	}
775 
776 	/* get the channel (aka ADQ VSI) */
777 	if (tc_fltr->dest_vsi)
778 		ch_vsi = tc_fltr->dest_vsi;
779 	else
780 		ch_vsi = vsi->tc_map_vsi[tc_fltr->action.tc_class];
781 
782 	lkups_cnt = ice_tc_count_lkups(flags, headers, tc_fltr);
783 	list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
784 	if (!list)
785 		return -ENOMEM;
786 
787 	i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
788 	if (i != lkups_cnt) {
789 		ret = -EINVAL;
790 		goto exit;
791 	}
792 
793 	rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
794 	if (tc_fltr->action.tc_class >= ICE_CHNL_START_TC) {
795 		if (!ch_vsi) {
796 			NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because specified destination doesn't exist");
797 			ret = -EINVAL;
798 			goto exit;
799 		}
800 
801 		rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
802 		rule_info.sw_act.vsi_handle = ch_vsi->idx;
803 		rule_info.priority = 7;
804 		rule_info.sw_act.src = hw->pf_id;
805 		rule_info.rx = true;
806 		dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
807 			tc_fltr->action.tc_class,
808 			rule_info.sw_act.vsi_handle, lkups_cnt);
809 	} else {
810 		rule_info.sw_act.flag |= ICE_FLTR_TX;
811 		rule_info.sw_act.src = vsi->idx;
812 		rule_info.rx = false;
813 	}
814 
815 	/* specify the cookie as filter_rule_id */
816 	rule_info.fltr_rule_id = tc_fltr->cookie;
817 
818 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
819 	if (ret == -EEXIST) {
820 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
821 				   "Unable to add filter because it already exist");
822 		ret = -EINVAL;
823 		goto exit;
824 	} else if (ret) {
825 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
826 				   "Unable to add filter due to error");
827 		goto exit;
828 	}
829 
830 	/* store the output params, which are needed later for removing
831 	 * advanced switch filter
832 	 */
833 	tc_fltr->rid = rule_added.rid;
834 	tc_fltr->rule_id = rule_added.rule_id;
835 	if (tc_fltr->action.tc_class > 0 && ch_vsi) {
836 		/* For PF ADQ, VSI type is set as ICE_VSI_CHNL, and
837 		 * for PF ADQ filter, it is not yet set in tc_fltr,
838 		 * hence store the dest_vsi ptr in tc_fltr
839 		 */
840 		if (ch_vsi->type == ICE_VSI_CHNL)
841 			tc_fltr->dest_vsi = ch_vsi;
842 		/* keep track of advanced switch filter for
843 		 * destination VSI (channel VSI)
844 		 */
845 		ch_vsi->num_chnl_fltr++;
846 		/* in this case, dest_id is VSI handle (sw handle) */
847 		tc_fltr->dest_id = rule_added.vsi_handle;
848 
849 		/* keeps track of channel filters for PF VSI */
850 		if (vsi->type == ICE_VSI_PF &&
851 		    (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
852 			      ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
853 			pf->num_dmac_chnl_fltrs++;
854 	}
855 	dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x) for TC %u, rid %u, rule_id %u, vsi_idx %u\n",
856 		lkups_cnt, flags,
857 		tc_fltr->action.tc_class, rule_added.rid,
858 		rule_added.rule_id, rule_added.vsi_handle);
859 exit:
860 	kfree(list);
861 	return ret;
862 }
863 
864 /**
865  * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
866  * @match: Pointer to flow match structure
867  * @fltr: Pointer to filter structure
868  * @headers: Pointer to outer header fields
869  * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
870  */
871 static u16
ice_tc_set_pppoe(struct flow_match_pppoe * match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers)872 ice_tc_set_pppoe(struct flow_match_pppoe *match,
873 		 struct ice_tc_flower_fltr *fltr,
874 		 struct ice_tc_flower_lyr_2_4_hdrs *headers)
875 {
876 	if (match->mask->session_id) {
877 		fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
878 		headers->pppoe_hdr.session_id = match->key->session_id;
879 	}
880 
881 	if (match->mask->ppp_proto) {
882 		fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
883 		headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
884 	}
885 
886 	return be16_to_cpu(match->key->type);
887 }
888 
889 /**
890  * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
891  * @match: Pointer to flow match structure
892  * @fltr: Pointer to filter structure
893  * @headers: inner or outer header fields
894  * @is_encap: set true for tunnel IPv4 address
895  */
896 static int
ice_tc_set_ipv4(struct flow_match_ipv4_addrs * match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers,bool is_encap)897 ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
898 		struct ice_tc_flower_fltr *fltr,
899 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
900 {
901 	if (match->key->dst) {
902 		if (is_encap)
903 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
904 		else
905 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
906 		headers->l3_key.dst_ipv4 = match->key->dst;
907 		headers->l3_mask.dst_ipv4 = match->mask->dst;
908 	}
909 	if (match->key->src) {
910 		if (is_encap)
911 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
912 		else
913 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
914 		headers->l3_key.src_ipv4 = match->key->src;
915 		headers->l3_mask.src_ipv4 = match->mask->src;
916 	}
917 	return 0;
918 }
919 
920 /**
921  * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
922  * @match: Pointer to flow match structure
923  * @fltr: Pointer to filter structure
924  * @headers: inner or outer header fields
925  * @is_encap: set true for tunnel IPv6 address
926  */
927 static int
ice_tc_set_ipv6(struct flow_match_ipv6_addrs * match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers,bool is_encap)928 ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
929 		struct ice_tc_flower_fltr *fltr,
930 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
931 {
932 	struct ice_tc_l3_hdr *l3_key, *l3_mask;
933 
934 	/* src and dest IPV6 address should not be LOOPBACK
935 	 * (0:0:0:0:0:0:0:1), which can be represented as ::1
936 	 */
937 	if (ipv6_addr_loopback(&match->key->dst) ||
938 	    ipv6_addr_loopback(&match->key->src)) {
939 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
940 		return -EINVAL;
941 	}
942 	/* if src/dest IPv6 address is *,* error */
943 	if (ipv6_addr_any(&match->mask->dst) &&
944 	    ipv6_addr_any(&match->mask->src)) {
945 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
946 		return -EINVAL;
947 	}
948 	if (!ipv6_addr_any(&match->mask->dst)) {
949 		if (is_encap)
950 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
951 		else
952 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
953 	}
954 	if (!ipv6_addr_any(&match->mask->src)) {
955 		if (is_encap)
956 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
957 		else
958 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
959 	}
960 
961 	l3_key = &headers->l3_key;
962 	l3_mask = &headers->l3_mask;
963 
964 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
965 			   ICE_TC_FLWR_FIELD_SRC_IPV6)) {
966 		memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
967 		       sizeof(match->key->src.s6_addr));
968 		memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
969 		       sizeof(match->mask->src.s6_addr));
970 	}
971 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
972 			   ICE_TC_FLWR_FIELD_DEST_IPV6)) {
973 		memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
974 		       sizeof(match->key->dst.s6_addr));
975 		memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
976 		       sizeof(match->mask->dst.s6_addr));
977 	}
978 
979 	return 0;
980 }
981 
982 /**
983  * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
984  * @match: Pointer to flow match structure
985  * @fltr: Pointer to filter structure
986  * @headers: inner or outer header fields
987  * @is_encap: set true for tunnel
988  */
989 static void
ice_tc_set_tos_ttl(struct flow_match_ip * match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers,bool is_encap)990 ice_tc_set_tos_ttl(struct flow_match_ip *match,
991 		   struct ice_tc_flower_fltr *fltr,
992 		   struct ice_tc_flower_lyr_2_4_hdrs *headers,
993 		   bool is_encap)
994 {
995 	if (match->mask->tos) {
996 		if (is_encap)
997 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
998 		else
999 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
1000 
1001 		headers->l3_key.tos = match->key->tos;
1002 		headers->l3_mask.tos = match->mask->tos;
1003 	}
1004 
1005 	if (match->mask->ttl) {
1006 		if (is_encap)
1007 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
1008 		else
1009 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
1010 
1011 		headers->l3_key.ttl = match->key->ttl;
1012 		headers->l3_mask.ttl = match->mask->ttl;
1013 	}
1014 }
1015 
1016 /**
1017  * ice_tc_set_port - Parse ports from TC flower filter
1018  * @match: Flow match structure
1019  * @fltr: Pointer to filter structure
1020  * @headers: inner or outer header fields
1021  * @is_encap: set true for tunnel port
1022  */
1023 static int
ice_tc_set_port(struct flow_match_ports match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers,bool is_encap)1024 ice_tc_set_port(struct flow_match_ports match,
1025 		struct ice_tc_flower_fltr *fltr,
1026 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1027 {
1028 	if (match.key->dst) {
1029 		if (is_encap)
1030 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
1031 		else
1032 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
1033 
1034 		headers->l4_key.dst_port = match.key->dst;
1035 		headers->l4_mask.dst_port = match.mask->dst;
1036 	}
1037 	if (match.key->src) {
1038 		if (is_encap)
1039 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
1040 		else
1041 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
1042 
1043 		headers->l4_key.src_port = match.key->src;
1044 		headers->l4_mask.src_port = match.mask->src;
1045 	}
1046 	return 0;
1047 }
1048 
1049 static struct net_device *
ice_get_tunnel_device(struct net_device * dev,struct flow_rule * rule)1050 ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
1051 {
1052 	struct flow_action_entry *act;
1053 	int i;
1054 
1055 	if (ice_is_tunnel_supported(dev))
1056 		return dev;
1057 
1058 	flow_action_for_each(i, act, &rule->action) {
1059 		if (act->id == FLOW_ACTION_REDIRECT &&
1060 		    ice_is_tunnel_supported(act->dev))
1061 			return act->dev;
1062 	}
1063 
1064 	return NULL;
1065 }
1066 
1067 /**
1068  * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
1069  * @match: Flow match structure
1070  * @fltr: Pointer to filter structure
1071  *
1072  * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
1073  * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
1074  * therefore making GTP-U the default choice (when destination port number is
1075  * not specified).
1076  */
1077 static int
ice_parse_gtp_type(struct flow_match_ports match,struct ice_tc_flower_fltr * fltr)1078 ice_parse_gtp_type(struct flow_match_ports match,
1079 		   struct ice_tc_flower_fltr *fltr)
1080 {
1081 	u16 dst_port;
1082 
1083 	if (match.key->dst) {
1084 		dst_port = be16_to_cpu(match.key->dst);
1085 
1086 		switch (dst_port) {
1087 		case 2152:
1088 			break;
1089 		case 2123:
1090 			fltr->tunnel_type = TNL_GTPC;
1091 			break;
1092 		default:
1093 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
1094 			return -EINVAL;
1095 		}
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 static int
ice_parse_tunnel_attr(struct net_device * dev,struct flow_rule * rule,struct ice_tc_flower_fltr * fltr)1102 ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
1103 		      struct ice_tc_flower_fltr *fltr)
1104 {
1105 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1106 	struct flow_match_control enc_control;
1107 
1108 	fltr->tunnel_type = ice_tc_tun_get_type(dev);
1109 	headers->l3_key.ip_proto = IPPROTO_UDP;
1110 
1111 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1112 		struct flow_match_enc_keyid enc_keyid;
1113 
1114 		flow_rule_match_enc_keyid(rule, &enc_keyid);
1115 
1116 		if (!enc_keyid.mask->keyid ||
1117 		    enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
1118 			return -EINVAL;
1119 
1120 		fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
1121 		fltr->tenant_id = enc_keyid.key->keyid;
1122 	}
1123 
1124 	flow_rule_match_enc_control(rule, &enc_control);
1125 
1126 	if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1127 		struct flow_match_ipv4_addrs match;
1128 
1129 		flow_rule_match_enc_ipv4_addrs(rule, &match);
1130 		if (ice_tc_set_ipv4(&match, fltr, headers, true))
1131 			return -EINVAL;
1132 	} else if (enc_control.key->addr_type ==
1133 					FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1134 		struct flow_match_ipv6_addrs match;
1135 
1136 		flow_rule_match_enc_ipv6_addrs(rule, &match);
1137 		if (ice_tc_set_ipv6(&match, fltr, headers, true))
1138 			return -EINVAL;
1139 	}
1140 
1141 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
1142 		struct flow_match_ip match;
1143 
1144 		flow_rule_match_enc_ip(rule, &match);
1145 		ice_tc_set_tos_ttl(&match, fltr, headers, true);
1146 	}
1147 
1148 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
1149 	    fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
1150 		struct flow_match_ports match;
1151 
1152 		flow_rule_match_enc_ports(rule, &match);
1153 
1154 		if (fltr->tunnel_type != TNL_GTPU) {
1155 			if (ice_tc_set_port(match, fltr, headers, true))
1156 				return -EINVAL;
1157 		} else {
1158 			if (ice_parse_gtp_type(match, fltr))
1159 				return -EINVAL;
1160 		}
1161 	}
1162 
1163 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
1164 		struct flow_match_enc_opts match;
1165 
1166 		flow_rule_match_enc_opts(rule, &match);
1167 
1168 		memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
1169 		       sizeof(struct gtp_pdu_session_info));
1170 
1171 		memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
1172 		       sizeof(struct gtp_pdu_session_info));
1173 
1174 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_OPTS;
1175 	}
1176 
1177 	return 0;
1178 }
1179 
1180 /**
1181  * ice_parse_cls_flower - Parse TC flower filters provided by kernel
1182  * @vsi: Pointer to the VSI
1183  * @filter_dev: Pointer to device on which filter is being added
1184  * @f: Pointer to struct flow_cls_offload
1185  * @fltr: Pointer to filter structure
1186  */
1187 static int
ice_parse_cls_flower(struct net_device * filter_dev,struct ice_vsi * vsi,struct flow_cls_offload * f,struct ice_tc_flower_fltr * fltr)1188 ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
1189 		     struct flow_cls_offload *f,
1190 		     struct ice_tc_flower_fltr *fltr)
1191 {
1192 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1193 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1194 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
1195 	struct flow_dissector *dissector;
1196 	struct net_device *tunnel_dev;
1197 
1198 	dissector = rule->match.dissector;
1199 
1200 	if (dissector->used_keys &
1201 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1202 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
1203 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1204 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
1205 	      BIT(FLOW_DISSECTOR_KEY_CVLAN) |
1206 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1207 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1208 	      BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1209 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1210 	      BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1211 	      BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1212 	      BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1213 	      BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1214 	      BIT(FLOW_DISSECTOR_KEY_IP) |
1215 	      BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
1216 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
1217 	      BIT(FLOW_DISSECTOR_KEY_PPPOE) |
1218 	      BIT(FLOW_DISSECTOR_KEY_L2TPV3))) {
1219 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
1220 		return -EOPNOTSUPP;
1221 	}
1222 
1223 	tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
1224 	if (tunnel_dev) {
1225 		int err;
1226 
1227 		filter_dev = tunnel_dev;
1228 
1229 		err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
1230 		if (err) {
1231 			NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
1232 			return err;
1233 		}
1234 
1235 		/* header pointers should point to the inner headers, outer
1236 		 * header were already set by ice_parse_tunnel_attr
1237 		 */
1238 		headers = &fltr->inner_headers;
1239 	} else if (dissector->used_keys &
1240 		  (BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1241 		   BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1242 		   BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1243 		   BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))) {
1244 		NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
1245 		return -EOPNOTSUPP;
1246 	} else {
1247 		fltr->tunnel_type = TNL_LAST;
1248 	}
1249 
1250 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1251 		struct flow_match_basic match;
1252 
1253 		flow_rule_match_basic(rule, &match);
1254 
1255 		n_proto_key = ntohs(match.key->n_proto);
1256 		n_proto_mask = ntohs(match.mask->n_proto);
1257 
1258 		if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
1259 		    fltr->tunnel_type == TNL_GTPU ||
1260 		    fltr->tunnel_type == TNL_GTPC) {
1261 			n_proto_key = 0;
1262 			n_proto_mask = 0;
1263 		} else {
1264 			fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1265 		}
1266 
1267 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1268 		headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
1269 		headers->l3_key.ip_proto = match.key->ip_proto;
1270 	}
1271 
1272 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1273 		struct flow_match_eth_addrs match;
1274 
1275 		flow_rule_match_eth_addrs(rule, &match);
1276 
1277 		if (!is_zero_ether_addr(match.key->dst)) {
1278 			ether_addr_copy(headers->l2_key.dst_mac,
1279 					match.key->dst);
1280 			ether_addr_copy(headers->l2_mask.dst_mac,
1281 					match.mask->dst);
1282 			fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1283 		}
1284 
1285 		if (!is_zero_ether_addr(match.key->src)) {
1286 			ether_addr_copy(headers->l2_key.src_mac,
1287 					match.key->src);
1288 			ether_addr_copy(headers->l2_mask.src_mac,
1289 					match.mask->src);
1290 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
1291 		}
1292 	}
1293 
1294 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
1295 	    is_vlan_dev(filter_dev)) {
1296 		struct flow_dissector_key_vlan mask;
1297 		struct flow_dissector_key_vlan key;
1298 		struct flow_match_vlan match;
1299 
1300 		if (is_vlan_dev(filter_dev)) {
1301 			match.key = &key;
1302 			match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
1303 			match.key->vlan_priority = 0;
1304 			match.mask = &mask;
1305 			memset(match.mask, 0xff, sizeof(*match.mask));
1306 			match.mask->vlan_priority = 0;
1307 		} else {
1308 			flow_rule_match_vlan(rule, &match);
1309 		}
1310 
1311 		if (match.mask->vlan_id) {
1312 			if (match.mask->vlan_id == VLAN_VID_MASK) {
1313 				fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
1314 				headers->vlan_hdr.vlan_id =
1315 					cpu_to_be16(match.key->vlan_id &
1316 						    VLAN_VID_MASK);
1317 			} else {
1318 				NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
1319 				return -EINVAL;
1320 			}
1321 		}
1322 
1323 		if (match.mask->vlan_priority) {
1324 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
1325 			headers->vlan_hdr.vlan_prio =
1326 				be16_encode_bits(match.key->vlan_priority,
1327 						 VLAN_PRIO_MASK);
1328 		}
1329 
1330 		if (match.mask->vlan_tpid)
1331 			headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
1332 	}
1333 
1334 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
1335 		struct flow_match_vlan match;
1336 
1337 		if (!ice_is_dvm_ena(&vsi->back->hw)) {
1338 			NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
1339 			return -EINVAL;
1340 		}
1341 
1342 		flow_rule_match_cvlan(rule, &match);
1343 
1344 		if (match.mask->vlan_id) {
1345 			if (match.mask->vlan_id == VLAN_VID_MASK) {
1346 				fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
1347 				headers->cvlan_hdr.vlan_id =
1348 					cpu_to_be16(match.key->vlan_id &
1349 						    VLAN_VID_MASK);
1350 			} else {
1351 				NL_SET_ERR_MSG_MOD(fltr->extack,
1352 						   "Bad CVLAN mask");
1353 				return -EINVAL;
1354 			}
1355 		}
1356 
1357 		if (match.mask->vlan_priority) {
1358 			fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
1359 			headers->cvlan_hdr.vlan_prio =
1360 				be16_encode_bits(match.key->vlan_priority,
1361 						 VLAN_PRIO_MASK);
1362 		}
1363 	}
1364 
1365 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
1366 		struct flow_match_pppoe match;
1367 
1368 		flow_rule_match_pppoe(rule, &match);
1369 		n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
1370 
1371 		/* If ethertype equals ETH_P_PPP_SES, n_proto might be
1372 		 * overwritten by encapsulated protocol (ppp_proto field) or set
1373 		 * to 0. To correct this, flow_match_pppoe provides the type
1374 		 * field, which contains the actual ethertype (ETH_P_PPP_SES).
1375 		 */
1376 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1377 		headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
1378 		fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1379 	}
1380 
1381 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1382 		struct flow_match_control match;
1383 
1384 		flow_rule_match_control(rule, &match);
1385 
1386 		addr_type = match.key->addr_type;
1387 	}
1388 
1389 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1390 		struct flow_match_ipv4_addrs match;
1391 
1392 		flow_rule_match_ipv4_addrs(rule, &match);
1393 		if (ice_tc_set_ipv4(&match, fltr, headers, false))
1394 			return -EINVAL;
1395 	}
1396 
1397 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1398 		struct flow_match_ipv6_addrs match;
1399 
1400 		flow_rule_match_ipv6_addrs(rule, &match);
1401 		if (ice_tc_set_ipv6(&match, fltr, headers, false))
1402 			return -EINVAL;
1403 	}
1404 
1405 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
1406 		struct flow_match_ip match;
1407 
1408 		flow_rule_match_ip(rule, &match);
1409 		ice_tc_set_tos_ttl(&match, fltr, headers, false);
1410 	}
1411 
1412 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
1413 		struct flow_match_l2tpv3 match;
1414 
1415 		flow_rule_match_l2tpv3(rule, &match);
1416 
1417 		fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
1418 		headers->l2tpv3_hdr.session_id = match.key->session_id;
1419 	}
1420 
1421 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
1422 		struct flow_match_ports match;
1423 
1424 		flow_rule_match_ports(rule, &match);
1425 		if (ice_tc_set_port(match, fltr, headers, false))
1426 			return -EINVAL;
1427 		switch (headers->l3_key.ip_proto) {
1428 		case IPPROTO_TCP:
1429 		case IPPROTO_UDP:
1430 			break;
1431 		default:
1432 			NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
1433 			return -EINVAL;
1434 		}
1435 	}
1436 	return 0;
1437 }
1438 
1439 /**
1440  * ice_add_switch_fltr - Add TC flower filters
1441  * @vsi: Pointer to VSI
1442  * @fltr: Pointer to struct ice_tc_flower_fltr
1443  *
1444  * Add filter in HW switch block
1445  */
1446 static int
ice_add_switch_fltr(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr)1447 ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1448 {
1449 	if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
1450 		return -EOPNOTSUPP;
1451 
1452 	if (ice_is_eswitch_mode_switchdev(vsi->back))
1453 		return ice_eswitch_add_tc_fltr(vsi, fltr);
1454 
1455 	return ice_add_tc_flower_adv_fltr(vsi, fltr);
1456 }
1457 
1458 /**
1459  * ice_handle_tclass_action - Support directing to a traffic class
1460  * @vsi: Pointer to VSI
1461  * @cls_flower: Pointer to TC flower offload structure
1462  * @fltr: Pointer to TC flower filter structure
1463  *
1464  * Support directing traffic to a traffic class
1465  */
1466 static int
ice_handle_tclass_action(struct ice_vsi * vsi,struct flow_cls_offload * cls_flower,struct ice_tc_flower_fltr * fltr)1467 ice_handle_tclass_action(struct ice_vsi *vsi,
1468 			 struct flow_cls_offload *cls_flower,
1469 			 struct ice_tc_flower_fltr *fltr)
1470 {
1471 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
1472 	struct ice_vsi *main_vsi;
1473 
1474 	if (tc < 0) {
1475 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because specified destination is invalid");
1476 		return -EINVAL;
1477 	}
1478 	if (!tc) {
1479 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because of invalid destination");
1480 		return -EINVAL;
1481 	}
1482 
1483 	if (!(vsi->all_enatc & BIT(tc))) {
1484 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because of non-existence destination");
1485 		return -EINVAL;
1486 	}
1487 
1488 	/* Redirect to a TC class or Queue Group */
1489 	main_vsi = ice_get_main_vsi(vsi->back);
1490 	if (!main_vsi || !main_vsi->netdev) {
1491 		NL_SET_ERR_MSG_MOD(fltr->extack,
1492 				   "Unable to add filter because of invalid netdevice");
1493 		return -EINVAL;
1494 	}
1495 
1496 	if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
1497 	    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1498 			   ICE_TC_FLWR_FIELD_SRC_MAC))) {
1499 		NL_SET_ERR_MSG_MOD(fltr->extack,
1500 				   "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
1501 		return -EOPNOTSUPP;
1502 	}
1503 
1504 	/* For ADQ, filter must include dest MAC address, otherwise unwanted
1505 	 * packets with unrelated MAC address get delivered to ADQ VSIs as long
1506 	 * as remaining filter criteria is satisfied such as dest IP address
1507 	 * and dest/src L4 port. Following code is trying to handle:
1508 	 * 1. For non-tunnel, if user specify MAC addresses, use them (means
1509 	 * this code won't do anything
1510 	 * 2. For non-tunnel, if user didn't specify MAC address, add implicit
1511 	 * dest MAC to be lower netdev's active unicast MAC address
1512 	 * 3. For tunnel,  as of now TC-filter through flower classifier doesn't
1513 	 * have provision for user to specify outer DMAC, hence driver to
1514 	 * implicitly add outer dest MAC to be lower netdev's active unicast
1515 	 * MAC address.
1516 	 */
1517 	if (fltr->tunnel_type != TNL_LAST &&
1518 	    !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
1519 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
1520 
1521 	if (fltr->tunnel_type == TNL_LAST &&
1522 	    !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
1523 		fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1524 
1525 	if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1526 			   ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
1527 		ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
1528 				vsi->netdev->dev_addr);
1529 		eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
1530 	}
1531 
1532 	/* validate specified dest MAC address, make sure either it belongs to
1533 	 * lower netdev or any of MACVLAN. MACVLANs MAC address are added as
1534 	 * unicast MAC filter destined to main VSI.
1535 	 */
1536 	if (!ice_mac_fltr_exist(&main_vsi->back->hw,
1537 				fltr->outer_headers.l2_key.dst_mac,
1538 				main_vsi->idx)) {
1539 		NL_SET_ERR_MSG_MOD(fltr->extack,
1540 				   "Unable to add filter because legacy MAC filter for specified destination doesn't exist");
1541 		return -EINVAL;
1542 	}
1543 
1544 	/* Make sure VLAN is already added to main VSI, before allowing ADQ to
1545 	 * add a VLAN based filter such as MAC + VLAN + L4 port.
1546 	 */
1547 	if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
1548 		u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
1549 
1550 		if (!ice_vlan_fltr_exist(&main_vsi->back->hw, vlan_id,
1551 					 main_vsi->idx)) {
1552 			NL_SET_ERR_MSG_MOD(fltr->extack,
1553 					   "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
1554 			return -EINVAL;
1555 		}
1556 	}
1557 	fltr->action.fltr_act = ICE_FWD_TO_VSI;
1558 	fltr->action.tc_class = tc;
1559 
1560 	return 0;
1561 }
1562 
1563 /**
1564  * ice_parse_tc_flower_actions - Parse the actions for a TC filter
1565  * @vsi: Pointer to VSI
1566  * @cls_flower: Pointer to TC flower offload structure
1567  * @fltr: Pointer to TC flower filter structure
1568  *
1569  * Parse the actions for a TC filter
1570  */
1571 static int
ice_parse_tc_flower_actions(struct ice_vsi * vsi,struct flow_cls_offload * cls_flower,struct ice_tc_flower_fltr * fltr)1572 ice_parse_tc_flower_actions(struct ice_vsi *vsi,
1573 			    struct flow_cls_offload *cls_flower,
1574 			    struct ice_tc_flower_fltr *fltr)
1575 {
1576 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1577 	struct flow_action *flow_action = &rule->action;
1578 	struct flow_action_entry *act;
1579 	int i;
1580 
1581 	if (cls_flower->classid)
1582 		return ice_handle_tclass_action(vsi, cls_flower, fltr);
1583 
1584 	if (!flow_action_has_entries(flow_action))
1585 		return -EINVAL;
1586 
1587 	flow_action_for_each(i, act, flow_action) {
1588 		if (ice_is_eswitch_mode_switchdev(vsi->back)) {
1589 			int err = ice_eswitch_tc_parse_action(fltr, act);
1590 
1591 			if (err)
1592 				return err;
1593 			continue;
1594 		}
1595 		/* Allow only one rule per filter */
1596 
1597 		/* Drop action */
1598 		if (act->id == FLOW_ACTION_DROP) {
1599 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action DROP");
1600 			return -EINVAL;
1601 		}
1602 		fltr->action.fltr_act = ICE_FWD_TO_VSI;
1603 	}
1604 	return 0;
1605 }
1606 
1607 /**
1608  * ice_del_tc_fltr - deletes a filter from HW table
1609  * @vsi: Pointer to VSI
1610  * @fltr: Pointer to struct ice_tc_flower_fltr
1611  *
1612  * This function deletes a filter from HW table and manages book-keeping
1613  */
ice_del_tc_fltr(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr)1614 static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1615 {
1616 	struct ice_rule_query_data rule_rem;
1617 	struct ice_pf *pf = vsi->back;
1618 	int err;
1619 
1620 	rule_rem.rid = fltr->rid;
1621 	rule_rem.rule_id = fltr->rule_id;
1622 	rule_rem.vsi_handle = fltr->dest_id;
1623 	err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
1624 	if (err) {
1625 		if (err == -ENOENT) {
1626 			NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
1627 			return -ENOENT;
1628 		}
1629 		NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
1630 		return -EIO;
1631 	}
1632 
1633 	/* update advanced switch filter count for destination
1634 	 * VSI if filter destination was VSI
1635 	 */
1636 	if (fltr->dest_vsi) {
1637 		if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
1638 			fltr->dest_vsi->num_chnl_fltr--;
1639 
1640 			/* keeps track of channel filters for PF VSI */
1641 			if (vsi->type == ICE_VSI_PF &&
1642 			    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1643 					    ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
1644 				pf->num_dmac_chnl_fltrs--;
1645 		}
1646 	}
1647 	return 0;
1648 }
1649 
1650 /**
1651  * ice_add_tc_fltr - adds a TC flower filter
1652  * @netdev: Pointer to netdev
1653  * @vsi: Pointer to VSI
1654  * @f: Pointer to flower offload structure
1655  * @__fltr: Pointer to struct ice_tc_flower_fltr
1656  *
1657  * This function parses TC-flower input fields, parses action,
1658  * and adds a filter.
1659  */
1660 static int
ice_add_tc_fltr(struct net_device * netdev,struct ice_vsi * vsi,struct flow_cls_offload * f,struct ice_tc_flower_fltr ** __fltr)1661 ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
1662 		struct flow_cls_offload *f,
1663 		struct ice_tc_flower_fltr **__fltr)
1664 {
1665 	struct ice_tc_flower_fltr *fltr;
1666 	int err;
1667 
1668 	/* by default, set output to be INVALID */
1669 	*__fltr = NULL;
1670 
1671 	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1672 	if (!fltr)
1673 		return -ENOMEM;
1674 
1675 	fltr->cookie = f->cookie;
1676 	fltr->extack = f->common.extack;
1677 	fltr->src_vsi = vsi;
1678 	INIT_HLIST_NODE(&fltr->tc_flower_node);
1679 
1680 	err = ice_parse_cls_flower(netdev, vsi, f, fltr);
1681 	if (err < 0)
1682 		goto err;
1683 
1684 	err = ice_parse_tc_flower_actions(vsi, f, fltr);
1685 	if (err < 0)
1686 		goto err;
1687 
1688 	err = ice_add_switch_fltr(vsi, fltr);
1689 	if (err < 0)
1690 		goto err;
1691 
1692 	/* return the newly created filter */
1693 	*__fltr = fltr;
1694 
1695 	return 0;
1696 err:
1697 	kfree(fltr);
1698 	return err;
1699 }
1700 
1701 /**
1702  * ice_find_tc_flower_fltr - Find the TC flower filter in the list
1703  * @pf: Pointer to PF
1704  * @cookie: filter specific cookie
1705  */
1706 static struct ice_tc_flower_fltr *
ice_find_tc_flower_fltr(struct ice_pf * pf,unsigned long cookie)1707 ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
1708 {
1709 	struct ice_tc_flower_fltr *fltr;
1710 
1711 	hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
1712 		if (cookie == fltr->cookie)
1713 			return fltr;
1714 
1715 	return NULL;
1716 }
1717 
1718 /**
1719  * ice_add_cls_flower - add TC flower filters
1720  * @netdev: Pointer to filter device
1721  * @vsi: Pointer to VSI
1722  * @cls_flower: Pointer to flower offload structure
1723  */
1724 int
ice_add_cls_flower(struct net_device * netdev,struct ice_vsi * vsi,struct flow_cls_offload * cls_flower)1725 ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
1726 		   struct flow_cls_offload *cls_flower)
1727 {
1728 	struct netlink_ext_ack *extack = cls_flower->common.extack;
1729 	struct net_device *vsi_netdev = vsi->netdev;
1730 	struct ice_tc_flower_fltr *fltr;
1731 	struct ice_pf *pf = vsi->back;
1732 	int err;
1733 
1734 	if (ice_is_reset_in_progress(pf->state))
1735 		return -EBUSY;
1736 	if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
1737 		return -EINVAL;
1738 
1739 	if (ice_is_port_repr_netdev(netdev))
1740 		vsi_netdev = netdev;
1741 
1742 	if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
1743 	    !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
1744 		/* Based on TC indirect notifications from kernel, all ice
1745 		 * devices get an instance of rule from higher level device.
1746 		 * Avoid triggering explicit error in this case.
1747 		 */
1748 		if (netdev == vsi_netdev)
1749 			NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
1750 		return -EINVAL;
1751 	}
1752 
1753 	/* avoid duplicate entries, if exists - return error */
1754 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1755 	if (fltr) {
1756 		NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
1757 		return -EEXIST;
1758 	}
1759 
1760 	/* prep and add TC-flower filter in HW */
1761 	err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr);
1762 	if (err)
1763 		return err;
1764 
1765 	/* add filter into an ordered list */
1766 	hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
1767 	return 0;
1768 }
1769 
1770 /**
1771  * ice_del_cls_flower - delete TC flower filters
1772  * @vsi: Pointer to VSI
1773  * @cls_flower: Pointer to struct flow_cls_offload
1774  */
1775 int
ice_del_cls_flower(struct ice_vsi * vsi,struct flow_cls_offload * cls_flower)1776 ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
1777 {
1778 	struct ice_tc_flower_fltr *fltr;
1779 	struct ice_pf *pf = vsi->back;
1780 	int err;
1781 
1782 	/* find filter */
1783 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1784 	if (!fltr) {
1785 		if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
1786 		    hlist_empty(&pf->tc_flower_fltr_list))
1787 			return 0;
1788 
1789 		NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
1790 		return -EINVAL;
1791 	}
1792 
1793 	fltr->extack = cls_flower->common.extack;
1794 	/* delete filter from HW */
1795 	err = ice_del_tc_fltr(vsi, fltr);
1796 	if (err)
1797 		return err;
1798 
1799 	/* delete filter from an ordered list */
1800 	hlist_del(&fltr->tc_flower_node);
1801 
1802 	/* free the filter node */
1803 	kfree(fltr);
1804 
1805 	return 0;
1806 }
1807 
1808 /**
1809  * ice_replay_tc_fltrs - replay TC filters
1810  * @pf: pointer to PF struct
1811  */
ice_replay_tc_fltrs(struct ice_pf * pf)1812 void ice_replay_tc_fltrs(struct ice_pf *pf)
1813 {
1814 	struct ice_tc_flower_fltr *fltr;
1815 	struct hlist_node *node;
1816 
1817 	hlist_for_each_entry_safe(fltr, node,
1818 				  &pf->tc_flower_fltr_list,
1819 				  tc_flower_node) {
1820 		fltr->extack = NULL;
1821 		ice_add_switch_fltr(fltr->src_vsi, fltr);
1822 	}
1823 }
1824