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