1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2021, Intel Corporation. */
5 #include "ice_tc_lib.h"
8 #include "ice_protocol_type.h"
10 #define ICE_TC_METADATA_LKUP_IDX 0
13 * ice_tc_count_lkups - determine lookup count for switch filter
14 * @flags: TC-flower flags
15 * @headers: Pointer to TC flower filter header structure
16 * @fltr: Pointer to outer TC filter structure
18 * Determine lookup count based on TC flower input for switch filter.
21 ice_tc_count_lkups(u32 flags, struct ice_tc_flower_lyr_2_4_hdrs *headers,
22 struct ice_tc_flower_fltr *fltr)
24 int lkups_cnt = 1; /* 0th lookup is metadata */
26 /* Always add metadata as the 0th lookup. Included elements:
27 * - Direction flag (always present)
28 * - ICE_TC_FLWR_FIELD_VLAN_TPID (present if specified)
29 * - Tunnel flag (present if tunnel)
31 if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
34 if (flags & ICE_TC_FLWR_FIELD_TENANT_ID)
37 if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC)
40 if (flags & ICE_TC_FLWR_FIELD_GTP_OPTS)
43 if (flags & ICE_TC_FLWR_FIELD_PFCP_OPTS)
46 if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
47 ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
48 ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
49 ICE_TC_FLWR_FIELD_ENC_DEST_IPV6))
52 if (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
53 ICE_TC_FLWR_FIELD_ENC_IP_TTL))
56 if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT)
59 if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID)
62 /* are MAC fields specified? */
63 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC | ICE_TC_FLWR_FIELD_SRC_MAC))
66 /* is VLAN specified? */
67 if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO))
70 /* is CVLAN specified? */
71 if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO))
74 /* are PPPoE options specified? */
75 if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
76 ICE_TC_FLWR_FIELD_PPP_PROTO))
79 /* are IPv[4|6] fields specified? */
80 if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 | ICE_TC_FLWR_FIELD_SRC_IPV4 |
81 ICE_TC_FLWR_FIELD_DEST_IPV6 | ICE_TC_FLWR_FIELD_SRC_IPV6))
84 if (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))
87 /* are L2TPv3 options specified? */
88 if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID)
91 /* is L4 (TCP/UDP/any other L4 protocol fields) specified? */
92 if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
93 ICE_TC_FLWR_FIELD_SRC_L4_PORT))
99 static enum ice_protocol_type ice_proto_type_from_mac(bool inner)
101 return inner ? ICE_MAC_IL : ICE_MAC_OFOS;
104 static enum ice_protocol_type ice_proto_type_from_etype(bool inner)
106 return inner ? ICE_ETYPE_IL : ICE_ETYPE_OL;
109 static enum ice_protocol_type ice_proto_type_from_ipv4(bool inner)
111 return inner ? ICE_IPV4_IL : ICE_IPV4_OFOS;
114 static enum ice_protocol_type ice_proto_type_from_ipv6(bool inner)
116 return inner ? ICE_IPV6_IL : ICE_IPV6_OFOS;
119 static enum ice_protocol_type ice_proto_type_from_l4_port(u16 ip_proto)
131 static enum ice_protocol_type
132 ice_proto_type_from_tunnel(enum ice_tunnel_type type)
142 /* NO_PAY profiles will not work with GTP-U */
145 return ICE_GTP_NO_PAY;
153 static enum ice_sw_tunnel_type
154 ice_sw_type_from_tunnel(enum ice_tunnel_type type)
158 return ICE_SW_TUN_VXLAN;
160 return ICE_SW_TUN_GENEVE;
162 return ICE_SW_TUN_NVGRE;
164 return ICE_SW_TUN_GTPU;
166 return ICE_SW_TUN_GTPC;
168 return ICE_SW_TUN_PFCP;
174 static u16 ice_check_supported_vlan_tpid(u16 vlan_tpid)
187 ice_tc_fill_tunnel_outer(u32 flags, struct ice_tc_flower_fltr *fltr,
188 struct ice_adv_lkup_elem *list, int i)
190 struct ice_tc_flower_lyr_2_4_hdrs *hdr = &fltr->outer_headers;
192 if (flags & ICE_TC_FLWR_FIELD_TENANT_ID) {
195 list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
196 switch (fltr->tunnel_type) {
199 tenant_id = be32_to_cpu(fltr->tenant_id) << 8;
200 list[i].h_u.tnl_hdr.vni = cpu_to_be32(tenant_id);
201 memcpy(&list[i].m_u.tnl_hdr.vni, "\xff\xff\xff\x00", 4);
205 list[i].h_u.nvgre_hdr.tni_flow = fltr->tenant_id;
206 memcpy(&list[i].m_u.nvgre_hdr.tni_flow,
207 "\xff\xff\xff\xff", 4);
212 list[i].h_u.gtp_hdr.teid = fltr->tenant_id;
213 memcpy(&list[i].m_u.gtp_hdr.teid,
214 "\xff\xff\xff\xff", 4);
222 if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC) {
223 list[i].type = ice_proto_type_from_mac(false);
224 ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
225 hdr->l2_key.dst_mac);
226 ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
227 hdr->l2_mask.dst_mac);
231 if (flags & ICE_TC_FLWR_FIELD_GTP_OPTS) {
232 list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
234 if (fltr->gtp_pdu_info_masks.pdu_type) {
235 list[i].h_u.gtp_hdr.pdu_type =
236 fltr->gtp_pdu_info_keys.pdu_type << 4;
237 memcpy(&list[i].m_u.gtp_hdr.pdu_type, "\xf0", 1);
240 if (fltr->gtp_pdu_info_masks.qfi) {
241 list[i].h_u.gtp_hdr.qfi = fltr->gtp_pdu_info_keys.qfi;
242 memcpy(&list[i].m_u.gtp_hdr.qfi, "\x3f", 1);
248 if (flags & ICE_TC_FLWR_FIELD_PFCP_OPTS) {
249 struct ice_pfcp_hdr *hdr_h, *hdr_m;
251 hdr_h = &list[i].h_u.pfcp_hdr;
252 hdr_m = &list[i].m_u.pfcp_hdr;
253 list[i].type = ICE_PFCP;
255 hdr_h->flags = fltr->pfcp_meta_keys.type;
256 hdr_m->flags = fltr->pfcp_meta_masks.type & 0x01;
258 hdr_h->seid = fltr->pfcp_meta_keys.seid;
259 hdr_m->seid = fltr->pfcp_meta_masks.seid;
264 if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
265 ICE_TC_FLWR_FIELD_ENC_DEST_IPV4)) {
266 list[i].type = ice_proto_type_from_ipv4(false);
268 if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV4) {
269 list[i].h_u.ipv4_hdr.src_addr = hdr->l3_key.src_ipv4;
270 list[i].m_u.ipv4_hdr.src_addr = hdr->l3_mask.src_ipv4;
272 if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV4) {
273 list[i].h_u.ipv4_hdr.dst_addr = hdr->l3_key.dst_ipv4;
274 list[i].m_u.ipv4_hdr.dst_addr = hdr->l3_mask.dst_ipv4;
279 if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
280 ICE_TC_FLWR_FIELD_ENC_DEST_IPV6)) {
281 list[i].type = ice_proto_type_from_ipv6(false);
283 if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV6) {
284 memcpy(&list[i].h_u.ipv6_hdr.src_addr,
285 &hdr->l3_key.src_ipv6_addr,
286 sizeof(hdr->l3_key.src_ipv6_addr));
287 memcpy(&list[i].m_u.ipv6_hdr.src_addr,
288 &hdr->l3_mask.src_ipv6_addr,
289 sizeof(hdr->l3_mask.src_ipv6_addr));
291 if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV6) {
292 memcpy(&list[i].h_u.ipv6_hdr.dst_addr,
293 &hdr->l3_key.dst_ipv6_addr,
294 sizeof(hdr->l3_key.dst_ipv6_addr));
295 memcpy(&list[i].m_u.ipv6_hdr.dst_addr,
296 &hdr->l3_mask.dst_ipv6_addr,
297 sizeof(hdr->l3_mask.dst_ipv6_addr));
302 if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IP) &&
303 (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
304 ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
305 list[i].type = ice_proto_type_from_ipv4(false);
307 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
308 list[i].h_u.ipv4_hdr.tos = hdr->l3_key.tos;
309 list[i].m_u.ipv4_hdr.tos = hdr->l3_mask.tos;
312 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
313 list[i].h_u.ipv4_hdr.time_to_live = hdr->l3_key.ttl;
314 list[i].m_u.ipv4_hdr.time_to_live = hdr->l3_mask.ttl;
320 if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IPV6) &&
321 (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
322 ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
323 struct ice_ipv6_hdr *hdr_h, *hdr_m;
325 hdr_h = &list[i].h_u.ipv6_hdr;
326 hdr_m = &list[i].m_u.ipv6_hdr;
327 list[i].type = ice_proto_type_from_ipv6(false);
329 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
330 be32p_replace_bits(&hdr_h->be_ver_tc_flow,
332 ICE_IPV6_HDR_TC_MASK);
333 be32p_replace_bits(&hdr_m->be_ver_tc_flow,
335 ICE_IPV6_HDR_TC_MASK);
338 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
339 hdr_h->hop_limit = hdr->l3_key.ttl;
340 hdr_m->hop_limit = hdr->l3_mask.ttl;
346 if ((flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT) &&
347 hdr->l3_key.ip_proto == IPPROTO_UDP) {
348 list[i].type = ICE_UDP_OF;
349 list[i].h_u.l4_hdr.dst_port = hdr->l4_key.dst_port;
350 list[i].m_u.l4_hdr.dst_port = hdr->l4_mask.dst_port;
354 /* always fill matching on tunneled packets in metadata */
355 ice_rule_add_tunnel_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
361 * ice_tc_fill_rules - fill filter rules based on TC fltr
362 * @hw: pointer to HW structure
363 * @flags: tc flower field flags
364 * @tc_fltr: pointer to TC flower filter
365 * @list: list of advance rule elements
366 * @rule_info: pointer to information about rule
367 * @l4_proto: pointer to information such as L4 proto type
369 * Fill ice_adv_lkup_elem list based on TC flower flags and
370 * TC flower headers. This list should be used to add
371 * advance filter in hardware.
374 ice_tc_fill_rules(struct ice_hw *hw, u32 flags,
375 struct ice_tc_flower_fltr *tc_fltr,
376 struct ice_adv_lkup_elem *list,
377 struct ice_adv_rule_info *rule_info,
380 struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
383 int i = 1; /* 0th lookup is metadata */
385 rule_info->vlan_type = vlan_tpid;
387 /* Always add direction metadata */
388 ice_rule_add_direction_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
390 if (tc_fltr->direction == ICE_ESWITCH_FLTR_EGRESS) {
391 ice_rule_add_src_vsi_metadata(&list[i]);
395 rule_info->tun_type = ice_sw_type_from_tunnel(tc_fltr->tunnel_type);
396 if (tc_fltr->tunnel_type != TNL_LAST) {
397 i = ice_tc_fill_tunnel_outer(flags, tc_fltr, list, i);
399 /* PFCP is considered non-tunneled - don't swap headers. */
400 if (tc_fltr->tunnel_type != TNL_PFCP) {
401 headers = &tc_fltr->inner_headers;
406 if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID) {
407 list[i].type = ice_proto_type_from_etype(inner);
408 list[i].h_u.ethertype.ethtype_id = headers->l2_key.n_proto;
409 list[i].m_u.ethertype.ethtype_id = headers->l2_mask.n_proto;
413 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
414 ICE_TC_FLWR_FIELD_SRC_MAC)) {
415 struct ice_tc_l2_hdr *l2_key, *l2_mask;
417 l2_key = &headers->l2_key;
418 l2_mask = &headers->l2_mask;
420 list[i].type = ice_proto_type_from_mac(inner);
421 if (flags & ICE_TC_FLWR_FIELD_DST_MAC) {
422 ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
424 ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
427 if (flags & ICE_TC_FLWR_FIELD_SRC_MAC) {
428 ether_addr_copy(list[i].h_u.eth_hdr.src_addr,
430 ether_addr_copy(list[i].m_u.eth_hdr.src_addr,
437 if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO)) {
438 if (flags & ICE_TC_FLWR_FIELD_CVLAN)
439 list[i].type = ICE_VLAN_EX;
441 list[i].type = ICE_VLAN_OFOS;
443 if (flags & ICE_TC_FLWR_FIELD_VLAN) {
444 list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
445 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
448 if (flags & ICE_TC_FLWR_FIELD_VLAN_PRIO) {
449 if (flags & ICE_TC_FLWR_FIELD_VLAN) {
450 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
452 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
453 list[i].h_u.vlan_hdr.vlan = 0;
455 list[i].h_u.vlan_hdr.vlan |=
456 headers->vlan_hdr.vlan_prio;
462 if (flags & ICE_TC_FLWR_FIELD_VLAN_TPID) {
463 vlan_tpid = be16_to_cpu(headers->vlan_hdr.vlan_tpid);
464 rule_info->vlan_type =
465 ice_check_supported_vlan_tpid(vlan_tpid);
467 ice_rule_add_vlan_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
470 if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO)) {
471 list[i].type = ICE_VLAN_IN;
473 if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
474 list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
475 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
478 if (flags & ICE_TC_FLWR_FIELD_CVLAN_PRIO) {
479 if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
480 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
482 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
483 list[i].h_u.vlan_hdr.vlan = 0;
485 list[i].h_u.vlan_hdr.vlan |=
486 headers->cvlan_hdr.vlan_prio;
492 if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
493 ICE_TC_FLWR_FIELD_PPP_PROTO)) {
494 struct ice_pppoe_hdr *vals, *masks;
496 vals = &list[i].h_u.pppoe_hdr;
497 masks = &list[i].m_u.pppoe_hdr;
499 list[i].type = ICE_PPPOE;
501 if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
502 vals->session_id = headers->pppoe_hdr.session_id;
503 masks->session_id = cpu_to_be16(0xFFFF);
506 if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
507 vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
508 masks->ppp_prot_id = cpu_to_be16(0xFFFF);
514 /* copy L3 (IPv[4|6]: src, dest) address */
515 if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
516 ICE_TC_FLWR_FIELD_SRC_IPV4)) {
517 struct ice_tc_l3_hdr *l3_key, *l3_mask;
519 list[i].type = ice_proto_type_from_ipv4(inner);
520 l3_key = &headers->l3_key;
521 l3_mask = &headers->l3_mask;
522 if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
523 list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
524 list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
526 if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
527 list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
528 list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
531 } else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
532 ICE_TC_FLWR_FIELD_SRC_IPV6)) {
533 struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
534 struct ice_tc_l3_hdr *l3_key, *l3_mask;
536 list[i].type = ice_proto_type_from_ipv6(inner);
537 ipv6_hdr = &list[i].h_u.ipv6_hdr;
538 ipv6_mask = &list[i].m_u.ipv6_hdr;
539 l3_key = &headers->l3_key;
540 l3_mask = &headers->l3_mask;
542 if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
543 memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
544 sizeof(l3_key->dst_ipv6_addr));
545 memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
546 sizeof(l3_mask->dst_ipv6_addr));
548 if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
549 memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
550 sizeof(l3_key->src_ipv6_addr));
551 memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
552 sizeof(l3_mask->src_ipv6_addr));
557 if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
558 (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
559 list[i].type = ice_proto_type_from_ipv4(inner);
561 if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
562 list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
563 list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
566 if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
567 list[i].h_u.ipv4_hdr.time_to_live =
569 list[i].m_u.ipv4_hdr.time_to_live =
570 headers->l3_mask.ttl;
576 if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
577 (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
578 struct ice_ipv6_hdr *hdr_h, *hdr_m;
580 hdr_h = &list[i].h_u.ipv6_hdr;
581 hdr_m = &list[i].m_u.ipv6_hdr;
582 list[i].type = ice_proto_type_from_ipv6(inner);
584 if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
585 be32p_replace_bits(&hdr_h->be_ver_tc_flow,
587 ICE_IPV6_HDR_TC_MASK);
588 be32p_replace_bits(&hdr_m->be_ver_tc_flow,
589 headers->l3_mask.tos,
590 ICE_IPV6_HDR_TC_MASK);
593 if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
594 hdr_h->hop_limit = headers->l3_key.ttl;
595 hdr_m->hop_limit = headers->l3_mask.ttl;
601 if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
602 list[i].type = ICE_L2TPV3;
604 list[i].h_u.l2tpv3_sess_hdr.session_id =
605 headers->l2tpv3_hdr.session_id;
606 list[i].m_u.l2tpv3_sess_hdr.session_id =
607 cpu_to_be32(0xFFFFFFFF);
612 /* copy L4 (src, dest) port */
613 if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
614 ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
615 struct ice_tc_l4_hdr *l4_key, *l4_mask;
617 list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
618 l4_key = &headers->l4_key;
619 l4_mask = &headers->l4_mask;
621 if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
622 list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
623 list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
625 if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
626 list[i].h_u.l4_hdr.src_port = l4_key->src_port;
627 list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
636 * ice_tc_tun_get_type - get the tunnel type
637 * @tunnel_dev: ptr to tunnel device
639 * This function detects appropriate tunnel_type if specified device is
640 * tunnel device such as VXLAN/Geneve
642 static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
644 if (netif_is_vxlan(tunnel_dev))
646 if (netif_is_geneve(tunnel_dev))
648 if (netif_is_gretap(tunnel_dev) ||
649 netif_is_ip6gretap(tunnel_dev))
652 /* Assume GTP-U by default in case of GTP netdev.
653 * GTP-C may be selected later, based on enc_dst_port.
655 if (netif_is_gtp(tunnel_dev))
657 if (netif_is_pfcp(tunnel_dev))
662 bool ice_is_tunnel_supported(struct net_device *dev)
664 return ice_tc_tun_get_type(dev) != TNL_LAST;
667 static bool ice_tc_is_dev_uplink(struct net_device *dev)
669 return netif_is_ice(dev) || ice_is_tunnel_supported(dev);
672 static int ice_tc_setup_action(struct net_device *filter_dev,
673 struct ice_tc_flower_fltr *fltr,
674 struct net_device *target_dev,
675 enum ice_sw_fwd_act_type action)
677 struct ice_repr *repr;
679 if (action != ICE_FWD_TO_VSI && action != ICE_MIRROR_PACKET) {
680 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action to setup provided");
684 fltr->action.fltr_act = action;
686 if (ice_is_port_repr_netdev(filter_dev) &&
687 ice_is_port_repr_netdev(target_dev)) {
688 repr = ice_netdev_to_repr(target_dev);
690 fltr->dest_vsi = repr->src_vsi;
691 fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
692 } else if (ice_is_port_repr_netdev(filter_dev) &&
693 ice_tc_is_dev_uplink(target_dev)) {
694 repr = ice_netdev_to_repr(filter_dev);
696 fltr->dest_vsi = repr->src_vsi->back->eswitch.uplink_vsi;
697 fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
698 } else if (ice_tc_is_dev_uplink(filter_dev) &&
699 ice_is_port_repr_netdev(target_dev)) {
700 repr = ice_netdev_to_repr(target_dev);
702 fltr->dest_vsi = repr->src_vsi;
703 fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
705 NL_SET_ERR_MSG_MOD(fltr->extack,
706 "Unsupported netdevice in switchdev mode");
714 ice_tc_setup_drop_action(struct net_device *filter_dev,
715 struct ice_tc_flower_fltr *fltr)
717 fltr->action.fltr_act = ICE_DROP_PACKET;
719 if (ice_is_port_repr_netdev(filter_dev)) {
720 fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
721 } else if (ice_tc_is_dev_uplink(filter_dev)) {
722 fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
724 NL_SET_ERR_MSG_MOD(fltr->extack,
725 "Unsupported netdevice in switchdev mode");
732 static int ice_eswitch_tc_parse_action(struct net_device *filter_dev,
733 struct ice_tc_flower_fltr *fltr,
734 struct flow_action_entry *act)
739 case FLOW_ACTION_DROP:
740 err = ice_tc_setup_drop_action(filter_dev, fltr);
746 case FLOW_ACTION_REDIRECT:
747 err = ice_tc_setup_action(filter_dev, fltr,
748 act->dev, ICE_FWD_TO_VSI);
754 case FLOW_ACTION_MIRRED:
755 err = ice_tc_setup_action(filter_dev, fltr,
756 act->dev, ICE_MIRROR_PACKET);
763 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
771 ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
773 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
774 struct ice_adv_rule_info rule_info = { 0 };
775 struct ice_rule_query_data rule_added;
776 struct ice_hw *hw = &vsi->back->hw;
777 struct ice_adv_lkup_elem *list;
778 u32 flags = fltr->flags;
783 if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT) {
784 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
788 lkups_cnt = ice_tc_count_lkups(flags, headers, fltr);
789 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
793 i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
794 if (i != lkups_cnt) {
799 rule_info.sw_act.fltr_act = fltr->action.fltr_act;
800 if (fltr->action.fltr_act != ICE_DROP_PACKET)
801 rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
802 /* For now, making priority to be highest, and it also becomes
803 * the priority for recipe which will get created as a result of
804 * new extraction sequence based on input set.
805 * Priority '7' is max val for switch recipe, higher the number
806 * results into order of switch rule evaluation.
808 rule_info.priority = 7;
809 rule_info.flags_info.act_valid = true;
811 if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
813 rule_info.sw_act.flag |= ICE_FLTR_RX;
814 rule_info.sw_act.src = hw->pf_id;
815 rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
816 } else if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS &&
817 fltr->dest_vsi == vsi->back->eswitch.uplink_vsi) {
819 rule_info.sw_act.flag |= ICE_FLTR_TX;
820 rule_info.sw_act.src = vsi->idx;
821 rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
824 rule_info.sw_act.flag |= ICE_FLTR_TX;
825 rule_info.sw_act.src = vsi->idx;
826 rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
829 /* specify the cookie as filter_rule_id */
830 rule_info.fltr_rule_id = fltr->cookie;
831 rule_info.src_vsi = vsi->idx;
833 ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
834 if (ret == -EEXIST) {
835 NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
839 NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
843 /* store the output params, which are needed later for removing
844 * advanced switch filter
846 fltr->rid = rule_added.rid;
847 fltr->rule_id = rule_added.rule_id;
848 fltr->dest_vsi_handle = rule_added.vsi_handle;
856 * ice_locate_vsi_using_queue - locate VSI using queue (forward to queue action)
857 * @vsi: Pointer to VSI
858 * @queue: Queue index
860 * Locate the VSI using specified "queue". When ADQ is not enabled,
861 * always return input VSI, otherwise locate corresponding
862 * VSI based on per channel "offset" and "qcount"
865 ice_locate_vsi_using_queue(struct ice_vsi *vsi, int queue)
869 /* if ADQ is not active, passed VSI is the candidate VSI */
870 if (!ice_is_adq_active(vsi->back))
873 /* Locate the VSI (it could still be main PF VSI or CHNL_VSI depending
876 num_tc = vsi->mqprio_qopt.qopt.num_tc;
878 for (tc = 0; tc < num_tc; tc++) {
879 int qcount = vsi->mqprio_qopt.qopt.count[tc];
880 int offset = vsi->mqprio_qopt.qopt.offset[tc];
882 if (queue >= offset && queue < offset + qcount) {
883 /* for non-ADQ TCs, passed VSI is the candidate VSI */
884 if (tc < ICE_CHNL_START_TC)
887 return vsi->tc_map_vsi[tc];
893 static struct ice_rx_ring *
894 ice_locate_rx_ring_using_queue(struct ice_vsi *vsi,
895 struct ice_tc_flower_fltr *tc_fltr)
897 u16 queue = tc_fltr->action.fwd.q.queue;
899 return queue < vsi->num_rxq ? vsi->rx_rings[queue] : NULL;
903 * ice_tc_forward_action - Determine destination VSI and queue for the action
904 * @vsi: Pointer to VSI
905 * @tc_fltr: Pointer to TC flower filter structure
907 * Validates the tc forward action and determines the destination VSI and queue
908 * for the forward action.
910 static struct ice_vsi *
911 ice_tc_forward_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *tc_fltr)
913 struct ice_rx_ring *ring = NULL;
914 struct ice_vsi *dest_vsi = NULL;
915 struct ice_pf *pf = vsi->back;
920 dev = ice_pf_to_dev(pf);
922 /* Get the destination VSI and/or destination queue and validate them */
923 switch (tc_fltr->action.fltr_act) {
925 tc_class = tc_fltr->action.fwd.tc.tc_class;
926 /* Select the destination VSI */
927 if (tc_class < ICE_CHNL_START_TC) {
928 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
929 "Unable to add filter because of unsupported destination");
930 return ERR_PTR(-EOPNOTSUPP);
932 /* Locate ADQ VSI depending on hw_tc number */
933 dest_vsi = vsi->tc_map_vsi[tc_class];
936 /* Locate the Rx queue */
937 ring = ice_locate_rx_ring_using_queue(vsi, tc_fltr);
940 "Unable to locate Rx queue for action fwd_to_queue: %u\n",
941 tc_fltr->action.fwd.q.queue);
942 return ERR_PTR(-EINVAL);
944 /* Determine destination VSI even though the action is
945 * FWD_TO_QUEUE, because QUEUE is associated with VSI
947 q = tc_fltr->action.fwd.q.queue;
948 dest_vsi = ice_locate_vsi_using_queue(vsi, q);
952 "Unable to add filter because of unsupported action %u (supported actions: fwd to tc, fwd to queue)\n",
953 tc_fltr->action.fltr_act);
954 return ERR_PTR(-EINVAL);
956 /* Must have valid dest_vsi (it could be main VSI or ADQ VSI) */
959 "Unable to add filter because specified destination VSI doesn't exist\n");
960 return ERR_PTR(-EINVAL);
966 * ice_add_tc_flower_adv_fltr - add appropriate filter rules
967 * @vsi: Pointer to VSI
968 * @tc_fltr: Pointer to TC flower filter structure
970 * based on filter parameters using Advance recipes supported
974 ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
975 struct ice_tc_flower_fltr *tc_fltr)
977 struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
978 struct ice_adv_rule_info rule_info = {0};
979 struct ice_rule_query_data rule_added;
980 struct ice_adv_lkup_elem *list;
981 struct ice_pf *pf = vsi->back;
982 struct ice_hw *hw = &pf->hw;
983 u32 flags = tc_fltr->flags;
984 struct ice_vsi *dest_vsi;
991 dev = ice_pf_to_dev(pf);
992 if (ice_is_safe_mode(pf)) {
993 NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
997 if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
998 ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
999 ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
1000 ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
1001 ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
1002 NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
1006 /* validate forwarding action VSI and queue */
1007 if (ice_is_forward_action(tc_fltr->action.fltr_act)) {
1008 dest_vsi = ice_tc_forward_action(vsi, tc_fltr);
1009 if (IS_ERR(dest_vsi))
1010 return PTR_ERR(dest_vsi);
1013 lkups_cnt = ice_tc_count_lkups(flags, headers, tc_fltr);
1014 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
1018 i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
1019 if (i != lkups_cnt) {
1024 rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
1025 /* specify the cookie as filter_rule_id */
1026 rule_info.fltr_rule_id = tc_fltr->cookie;
1028 switch (tc_fltr->action.fltr_act) {
1029 case ICE_FWD_TO_VSI:
1030 rule_info.sw_act.vsi_handle = dest_vsi->idx;
1031 rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
1032 rule_info.sw_act.src = hw->pf_id;
1033 dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
1034 tc_fltr->action.fwd.tc.tc_class,
1035 rule_info.sw_act.vsi_handle, lkups_cnt);
1038 /* HW queue number in global space */
1039 rule_info.sw_act.fwd_id.q_id = tc_fltr->action.fwd.q.hw_queue;
1040 rule_info.sw_act.vsi_handle = dest_vsi->idx;
1041 rule_info.priority = ICE_SWITCH_FLTR_PRIO_QUEUE;
1042 rule_info.sw_act.src = hw->pf_id;
1043 dev_dbg(dev, "add switch rule action to forward to queue:%u (HW queue %u), lkups_cnt:%u\n",
1044 tc_fltr->action.fwd.q.queue,
1045 tc_fltr->action.fwd.q.hw_queue, lkups_cnt);
1047 case ICE_DROP_PACKET:
1048 rule_info.sw_act.flag |= ICE_FLTR_RX;
1049 rule_info.sw_act.src = hw->pf_id;
1050 rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
1057 ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
1058 if (ret == -EEXIST) {
1059 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
1060 "Unable to add filter because it already exist");
1064 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
1065 "Unable to add filter due to error");
1069 /* store the output params, which are needed later for removing
1070 * advanced switch filter
1072 tc_fltr->rid = rule_added.rid;
1073 tc_fltr->rule_id = rule_added.rule_id;
1074 tc_fltr->dest_vsi_handle = rule_added.vsi_handle;
1075 if (tc_fltr->action.fltr_act == ICE_FWD_TO_VSI ||
1076 tc_fltr->action.fltr_act == ICE_FWD_TO_Q) {
1077 tc_fltr->dest_vsi = dest_vsi;
1078 /* keep track of advanced switch filter for
1081 dest_vsi->num_chnl_fltr++;
1083 /* keeps track of channel filters for PF VSI */
1084 if (vsi->type == ICE_VSI_PF &&
1085 (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1086 ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
1087 pf->num_dmac_chnl_fltrs++;
1089 switch (tc_fltr->action.fltr_act) {
1090 case ICE_FWD_TO_VSI:
1091 dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is forward to TC %u, rid %u, rule_id %u, vsi_idx %u\n",
1093 tc_fltr->action.fwd.tc.tc_class, rule_added.rid,
1094 rule_added.rule_id, rule_added.vsi_handle);
1097 dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is forward to queue: %u (HW queue %u) , rid %u, rule_id %u\n",
1098 lkups_cnt, flags, tc_fltr->action.fwd.q.queue,
1099 tc_fltr->action.fwd.q.hw_queue, rule_added.rid,
1100 rule_added.rule_id);
1102 case ICE_DROP_PACKET:
1103 dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is drop, rid %u, rule_id %u\n",
1104 lkups_cnt, flags, rule_added.rid, rule_added.rule_id);
1115 * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
1116 * @match: Pointer to flow match structure
1117 * @fltr: Pointer to filter structure
1118 * @headers: Pointer to outer header fields
1119 * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
1122 ice_tc_set_pppoe(struct flow_match_pppoe *match,
1123 struct ice_tc_flower_fltr *fltr,
1124 struct ice_tc_flower_lyr_2_4_hdrs *headers)
1126 if (match->mask->session_id) {
1127 fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
1128 headers->pppoe_hdr.session_id = match->key->session_id;
1131 if (match->mask->ppp_proto) {
1132 fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
1133 headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
1136 return be16_to_cpu(match->key->type);
1140 * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
1141 * @match: Pointer to flow match structure
1142 * @fltr: Pointer to filter structure
1143 * @headers: inner or outer header fields
1144 * @is_encap: set true for tunnel IPv4 address
1147 ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
1148 struct ice_tc_flower_fltr *fltr,
1149 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1151 if (match->key->dst) {
1153 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
1155 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
1156 headers->l3_key.dst_ipv4 = match->key->dst;
1157 headers->l3_mask.dst_ipv4 = match->mask->dst;
1159 if (match->key->src) {
1161 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
1163 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
1164 headers->l3_key.src_ipv4 = match->key->src;
1165 headers->l3_mask.src_ipv4 = match->mask->src;
1171 * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
1172 * @match: Pointer to flow match structure
1173 * @fltr: Pointer to filter structure
1174 * @headers: inner or outer header fields
1175 * @is_encap: set true for tunnel IPv6 address
1178 ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
1179 struct ice_tc_flower_fltr *fltr,
1180 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1182 struct ice_tc_l3_hdr *l3_key, *l3_mask;
1184 /* src and dest IPV6 address should not be LOOPBACK
1185 * (0:0:0:0:0:0:0:1), which can be represented as ::1
1187 if (ipv6_addr_loopback(&match->key->dst) ||
1188 ipv6_addr_loopback(&match->key->src)) {
1189 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
1192 /* if src/dest IPv6 address is *,* error */
1193 if (ipv6_addr_any(&match->mask->dst) &&
1194 ipv6_addr_any(&match->mask->src)) {
1195 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
1198 if (!ipv6_addr_any(&match->mask->dst)) {
1200 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
1202 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
1204 if (!ipv6_addr_any(&match->mask->src)) {
1206 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
1208 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
1211 l3_key = &headers->l3_key;
1212 l3_mask = &headers->l3_mask;
1214 if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
1215 ICE_TC_FLWR_FIELD_SRC_IPV6)) {
1216 memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
1217 sizeof(match->key->src.s6_addr));
1218 memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
1219 sizeof(match->mask->src.s6_addr));
1221 if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
1222 ICE_TC_FLWR_FIELD_DEST_IPV6)) {
1223 memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
1224 sizeof(match->key->dst.s6_addr));
1225 memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
1226 sizeof(match->mask->dst.s6_addr));
1233 * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
1234 * @match: Pointer to flow match structure
1235 * @fltr: Pointer to filter structure
1236 * @headers: inner or outer header fields
1237 * @is_encap: set true for tunnel
1240 ice_tc_set_tos_ttl(struct flow_match_ip *match,
1241 struct ice_tc_flower_fltr *fltr,
1242 struct ice_tc_flower_lyr_2_4_hdrs *headers,
1245 if (match->mask->tos) {
1247 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
1249 fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
1251 headers->l3_key.tos = match->key->tos;
1252 headers->l3_mask.tos = match->mask->tos;
1255 if (match->mask->ttl) {
1257 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
1259 fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
1261 headers->l3_key.ttl = match->key->ttl;
1262 headers->l3_mask.ttl = match->mask->ttl;
1267 * ice_tc_set_port - Parse ports from TC flower filter
1268 * @match: Flow match structure
1269 * @fltr: Pointer to filter structure
1270 * @headers: inner or outer header fields
1271 * @is_encap: set true for tunnel port
1274 ice_tc_set_port(struct flow_match_ports match,
1275 struct ice_tc_flower_fltr *fltr,
1276 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1278 if (match.key->dst) {
1280 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
1282 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
1284 headers->l4_key.dst_port = match.key->dst;
1285 headers->l4_mask.dst_port = match.mask->dst;
1287 if (match.key->src) {
1289 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
1291 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
1293 headers->l4_key.src_port = match.key->src;
1294 headers->l4_mask.src_port = match.mask->src;
1299 static struct net_device *
1300 ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
1302 struct flow_action_entry *act;
1305 if (ice_is_tunnel_supported(dev))
1308 flow_action_for_each(i, act, &rule->action) {
1309 if (act->id == FLOW_ACTION_REDIRECT &&
1310 ice_is_tunnel_supported(act->dev))
1318 * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
1319 * @match: Flow match structure
1320 * @fltr: Pointer to filter structure
1322 * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
1323 * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
1324 * therefore making GTP-U the default choice (when destination port number is
1328 ice_parse_gtp_type(struct flow_match_ports match,
1329 struct ice_tc_flower_fltr *fltr)
1333 if (match.key->dst) {
1334 dst_port = be16_to_cpu(match.key->dst);
1340 fltr->tunnel_type = TNL_GTPC;
1343 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
1352 ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
1353 struct ice_tc_flower_fltr *fltr)
1355 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1356 struct netlink_ext_ack *extack = fltr->extack;
1357 struct flow_match_control enc_control;
1359 fltr->tunnel_type = ice_tc_tun_get_type(dev);
1360 headers->l3_key.ip_proto = IPPROTO_UDP;
1362 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1363 struct flow_match_enc_keyid enc_keyid;
1365 flow_rule_match_enc_keyid(rule, &enc_keyid);
1367 if (!enc_keyid.mask->keyid ||
1368 enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
1371 fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
1372 fltr->tenant_id = enc_keyid.key->keyid;
1375 flow_rule_match_enc_control(rule, &enc_control);
1377 if (flow_rule_has_enc_control_flags(enc_control.mask->flags, extack))
1380 if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1381 struct flow_match_ipv4_addrs match;
1383 flow_rule_match_enc_ipv4_addrs(rule, &match);
1384 if (ice_tc_set_ipv4(&match, fltr, headers, true))
1386 } else if (enc_control.key->addr_type ==
1387 FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1388 struct flow_match_ipv6_addrs match;
1390 flow_rule_match_enc_ipv6_addrs(rule, &match);
1391 if (ice_tc_set_ipv6(&match, fltr, headers, true))
1395 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
1396 struct flow_match_ip match;
1398 flow_rule_match_enc_ip(rule, &match);
1399 ice_tc_set_tos_ttl(&match, fltr, headers, true);
1402 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
1403 fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
1404 struct flow_match_ports match;
1406 flow_rule_match_enc_ports(rule, &match);
1408 if (fltr->tunnel_type != TNL_GTPU) {
1409 if (ice_tc_set_port(match, fltr, headers, true))
1412 if (ice_parse_gtp_type(match, fltr))
1417 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS) &&
1418 (fltr->tunnel_type == TNL_GTPU || fltr->tunnel_type == TNL_GTPC)) {
1419 struct flow_match_enc_opts match;
1421 flow_rule_match_enc_opts(rule, &match);
1423 memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
1424 sizeof(struct gtp_pdu_session_info));
1426 memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
1427 sizeof(struct gtp_pdu_session_info));
1429 fltr->flags |= ICE_TC_FLWR_FIELD_GTP_OPTS;
1432 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS) &&
1433 fltr->tunnel_type == TNL_PFCP) {
1434 struct flow_match_enc_opts match;
1436 flow_rule_match_enc_opts(rule, &match);
1438 memcpy(&fltr->pfcp_meta_keys, match.key->data,
1439 sizeof(struct pfcp_metadata));
1440 memcpy(&fltr->pfcp_meta_masks, match.mask->data,
1441 sizeof(struct pfcp_metadata));
1443 fltr->flags |= ICE_TC_FLWR_FIELD_PFCP_OPTS;
1450 * ice_parse_cls_flower - Parse TC flower filters provided by kernel
1451 * @vsi: Pointer to the VSI
1452 * @filter_dev: Pointer to device on which filter is being added
1453 * @f: Pointer to struct flow_cls_offload
1454 * @fltr: Pointer to filter structure
1457 ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
1458 struct flow_cls_offload *f,
1459 struct ice_tc_flower_fltr *fltr)
1461 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1462 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1463 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
1464 struct flow_dissector *dissector;
1465 struct net_device *tunnel_dev;
1467 dissector = rule->match.dissector;
1469 if (dissector->used_keys &
1470 ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
1471 BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
1472 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1473 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
1474 BIT_ULL(FLOW_DISSECTOR_KEY_CVLAN) |
1475 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1476 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1477 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1478 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1479 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1480 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1481 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1482 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1483 BIT_ULL(FLOW_DISSECTOR_KEY_IP) |
1484 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IP) |
1485 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
1486 BIT_ULL(FLOW_DISSECTOR_KEY_PPPOE) |
1487 BIT_ULL(FLOW_DISSECTOR_KEY_L2TPV3))) {
1488 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
1492 tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
1496 filter_dev = tunnel_dev;
1498 err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
1500 NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
1504 /* PFCP is considered non-tunneled - don't swap headers. */
1505 if (fltr->tunnel_type != TNL_PFCP) {
1506 /* Header pointers should point to the inner headers,
1507 * outer header were already set by
1508 * ice_parse_tunnel_attr().
1510 headers = &fltr->inner_headers;
1512 } else if (dissector->used_keys &
1513 (BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1514 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1515 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1516 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1517 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IP) |
1518 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1519 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_CONTROL))) {
1520 NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
1523 fltr->tunnel_type = TNL_LAST;
1526 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1527 struct flow_match_basic match;
1529 flow_rule_match_basic(rule, &match);
1531 n_proto_key = ntohs(match.key->n_proto);
1532 n_proto_mask = ntohs(match.mask->n_proto);
1534 if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
1535 fltr->tunnel_type == TNL_GTPU ||
1536 fltr->tunnel_type == TNL_GTPC) {
1540 fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1543 headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1544 headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
1545 headers->l3_key.ip_proto = match.key->ip_proto;
1548 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1549 struct flow_match_eth_addrs match;
1551 flow_rule_match_eth_addrs(rule, &match);
1553 if (!is_zero_ether_addr(match.key->dst)) {
1554 ether_addr_copy(headers->l2_key.dst_mac,
1556 ether_addr_copy(headers->l2_mask.dst_mac,
1558 fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1561 if (!is_zero_ether_addr(match.key->src)) {
1562 ether_addr_copy(headers->l2_key.src_mac,
1564 ether_addr_copy(headers->l2_mask.src_mac,
1566 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
1570 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
1571 is_vlan_dev(filter_dev)) {
1572 struct flow_dissector_key_vlan mask;
1573 struct flow_dissector_key_vlan key;
1574 struct flow_match_vlan match;
1576 if (is_vlan_dev(filter_dev)) {
1578 match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
1579 match.key->vlan_priority = 0;
1581 memset(match.mask, 0xff, sizeof(*match.mask));
1582 match.mask->vlan_priority = 0;
1584 flow_rule_match_vlan(rule, &match);
1587 if (match.mask->vlan_id) {
1588 if (match.mask->vlan_id == VLAN_VID_MASK) {
1589 fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
1590 headers->vlan_hdr.vlan_id =
1591 cpu_to_be16(match.key->vlan_id &
1594 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
1599 if (match.mask->vlan_priority) {
1600 fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
1601 headers->vlan_hdr.vlan_prio =
1602 be16_encode_bits(match.key->vlan_priority,
1606 if (match.mask->vlan_tpid) {
1607 headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
1608 fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_TPID;
1612 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
1613 struct flow_match_vlan match;
1615 if (!ice_is_dvm_ena(&vsi->back->hw)) {
1616 NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
1620 flow_rule_match_cvlan(rule, &match);
1622 if (match.mask->vlan_id) {
1623 if (match.mask->vlan_id == VLAN_VID_MASK) {
1624 fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
1625 headers->cvlan_hdr.vlan_id =
1626 cpu_to_be16(match.key->vlan_id &
1629 NL_SET_ERR_MSG_MOD(fltr->extack,
1635 if (match.mask->vlan_priority) {
1636 fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
1637 headers->cvlan_hdr.vlan_prio =
1638 be16_encode_bits(match.key->vlan_priority,
1643 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
1644 struct flow_match_pppoe match;
1646 flow_rule_match_pppoe(rule, &match);
1647 n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
1649 /* If ethertype equals ETH_P_PPP_SES, n_proto might be
1650 * overwritten by encapsulated protocol (ppp_proto field) or set
1651 * to 0. To correct this, flow_match_pppoe provides the type
1652 * field, which contains the actual ethertype (ETH_P_PPP_SES).
1654 headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1655 headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
1656 fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1659 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1660 struct flow_match_control match;
1662 flow_rule_match_control(rule, &match);
1664 addr_type = match.key->addr_type;
1666 if (flow_rule_has_control_flags(match.mask->flags,
1671 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1672 struct flow_match_ipv4_addrs match;
1674 flow_rule_match_ipv4_addrs(rule, &match);
1675 if (ice_tc_set_ipv4(&match, fltr, headers, false))
1679 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1680 struct flow_match_ipv6_addrs match;
1682 flow_rule_match_ipv6_addrs(rule, &match);
1683 if (ice_tc_set_ipv6(&match, fltr, headers, false))
1687 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
1688 struct flow_match_ip match;
1690 flow_rule_match_ip(rule, &match);
1691 ice_tc_set_tos_ttl(&match, fltr, headers, false);
1694 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
1695 struct flow_match_l2tpv3 match;
1697 flow_rule_match_l2tpv3(rule, &match);
1699 fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
1700 headers->l2tpv3_hdr.session_id = match.key->session_id;
1703 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
1704 struct flow_match_ports match;
1706 flow_rule_match_ports(rule, &match);
1707 if (ice_tc_set_port(match, fltr, headers, false))
1709 switch (headers->l3_key.ip_proto) {
1714 NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
1722 * ice_add_switch_fltr - Add TC flower filters
1723 * @vsi: Pointer to VSI
1724 * @fltr: Pointer to struct ice_tc_flower_fltr
1726 * Add filter in HW switch block
1729 ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1731 if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
1734 if (ice_is_eswitch_mode_switchdev(vsi->back))
1735 return ice_eswitch_add_tc_fltr(vsi, fltr);
1737 return ice_add_tc_flower_adv_fltr(vsi, fltr);
1741 * ice_prep_adq_filter - Prepare ADQ filter with the required additional headers
1742 * @vsi: Pointer to VSI
1743 * @fltr: Pointer to TC flower filter structure
1745 * Prepare ADQ filter with the required additional header fields
1748 ice_prep_adq_filter(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1750 if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
1751 (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1752 ICE_TC_FLWR_FIELD_SRC_MAC))) {
1753 NL_SET_ERR_MSG_MOD(fltr->extack,
1754 "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
1758 /* For ADQ, filter must include dest MAC address, otherwise unwanted
1759 * packets with unrelated MAC address get delivered to ADQ VSIs as long
1760 * as remaining filter criteria is satisfied such as dest IP address
1761 * and dest/src L4 port. Below code handles the following cases:
1762 * 1. For non-tunnel, if user specify MAC addresses, use them.
1763 * 2. For non-tunnel, if user didn't specify MAC address, add implicit
1764 * dest MAC to be lower netdev's active unicast MAC address
1765 * 3. For tunnel, as of now TC-filter through flower classifier doesn't
1766 * have provision for user to specify outer DMAC, hence driver to
1767 * implicitly add outer dest MAC to be lower netdev's active unicast
1770 if (fltr->tunnel_type != TNL_LAST &&
1771 !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
1772 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
1774 if (fltr->tunnel_type == TNL_LAST &&
1775 !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
1776 fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1778 if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1779 ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
1780 ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
1781 vsi->netdev->dev_addr);
1782 eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
1785 /* Make sure VLAN is already added to main VSI, before allowing ADQ to
1786 * add a VLAN based filter such as MAC + VLAN + L4 port.
1788 if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
1789 u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
1791 if (!ice_vlan_fltr_exist(&vsi->back->hw, vlan_id, vsi->idx)) {
1792 NL_SET_ERR_MSG_MOD(fltr->extack,
1793 "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
1801 * ice_handle_tclass_action - Support directing to a traffic class
1802 * @vsi: Pointer to VSI
1803 * @cls_flower: Pointer to TC flower offload structure
1804 * @fltr: Pointer to TC flower filter structure
1806 * Support directing traffic to a traffic class/queue-set
1809 ice_handle_tclass_action(struct ice_vsi *vsi,
1810 struct flow_cls_offload *cls_flower,
1811 struct ice_tc_flower_fltr *fltr)
1813 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
1815 /* user specified hw_tc (must be non-zero for ADQ TC), action is forward
1816 * to hw_tc (i.e. ADQ channel number)
1818 if (tc < ICE_CHNL_START_TC) {
1819 NL_SET_ERR_MSG_MOD(fltr->extack,
1820 "Unable to add filter because of unsupported destination");
1823 if (!(vsi->all_enatc & BIT(tc))) {
1824 NL_SET_ERR_MSG_MOD(fltr->extack,
1825 "Unable to add filter because of non-existence destination");
1828 fltr->action.fltr_act = ICE_FWD_TO_VSI;
1829 fltr->action.fwd.tc.tc_class = tc;
1831 return ice_prep_adq_filter(vsi, fltr);
1835 ice_tc_forward_to_queue(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1836 struct flow_action_entry *act)
1838 struct ice_vsi *ch_vsi = NULL;
1839 u16 queue = act->rx_queue;
1841 if (queue >= vsi->num_rxq) {
1842 NL_SET_ERR_MSG_MOD(fltr->extack,
1843 "Unable to add filter because specified queue is invalid");
1846 fltr->action.fltr_act = ICE_FWD_TO_Q;
1847 fltr->action.fwd.q.queue = queue;
1848 /* determine corresponding HW queue */
1849 fltr->action.fwd.q.hw_queue = vsi->rxq_map[queue];
1851 /* If ADQ is configured, and the queue belongs to ADQ VSI, then prepare
1854 ch_vsi = ice_locate_vsi_using_queue(vsi, fltr->action.fwd.q.queue);
1857 fltr->dest_vsi = ch_vsi;
1858 if (!ice_is_chnl_fltr(fltr))
1861 return ice_prep_adq_filter(vsi, fltr);
1865 ice_tc_parse_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1866 struct flow_action_entry *act)
1869 case FLOW_ACTION_RX_QUEUE_MAPPING:
1870 /* forward to queue */
1871 return ice_tc_forward_to_queue(vsi, fltr, act);
1872 case FLOW_ACTION_DROP:
1873 fltr->action.fltr_act = ICE_DROP_PACKET;
1876 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported TC action");
1882 * ice_parse_tc_flower_actions - Parse the actions for a TC filter
1883 * @filter_dev: Pointer to device on which filter is being added
1884 * @vsi: Pointer to VSI
1885 * @cls_flower: Pointer to TC flower offload structure
1886 * @fltr: Pointer to TC flower filter structure
1888 * Parse the actions for a TC filter
1890 static int ice_parse_tc_flower_actions(struct net_device *filter_dev,
1891 struct ice_vsi *vsi,
1892 struct flow_cls_offload *cls_flower,
1893 struct ice_tc_flower_fltr *fltr)
1895 struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1896 struct flow_action *flow_action = &rule->action;
1897 struct flow_action_entry *act;
1900 if (cls_flower->classid)
1901 return ice_handle_tclass_action(vsi, cls_flower, fltr);
1903 if (!flow_action_has_entries(flow_action))
1906 flow_action_for_each(i, act, flow_action) {
1907 if (ice_is_eswitch_mode_switchdev(vsi->back))
1908 err = ice_eswitch_tc_parse_action(filter_dev, fltr, act);
1910 err = ice_tc_parse_action(vsi, fltr, act);
1919 * ice_del_tc_fltr - deletes a filter from HW table
1920 * @vsi: Pointer to VSI
1921 * @fltr: Pointer to struct ice_tc_flower_fltr
1923 * This function deletes a filter from HW table and manages book-keeping
1925 static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1927 struct ice_rule_query_data rule_rem;
1928 struct ice_pf *pf = vsi->back;
1931 rule_rem.rid = fltr->rid;
1932 rule_rem.rule_id = fltr->rule_id;
1933 rule_rem.vsi_handle = fltr->dest_vsi_handle;
1934 err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
1936 if (err == -ENOENT) {
1937 NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
1940 NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
1944 /* update advanced switch filter count for destination
1945 * VSI if filter destination was VSI
1947 if (fltr->dest_vsi) {
1948 if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
1949 fltr->dest_vsi->num_chnl_fltr--;
1951 /* keeps track of channel filters for PF VSI */
1952 if (vsi->type == ICE_VSI_PF &&
1953 (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1954 ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
1955 pf->num_dmac_chnl_fltrs--;
1962 * ice_add_tc_fltr - adds a TC flower filter
1963 * @netdev: Pointer to netdev
1964 * @vsi: Pointer to VSI
1965 * @f: Pointer to flower offload structure
1966 * @__fltr: Pointer to struct ice_tc_flower_fltr
1968 * This function parses TC-flower input fields, parses action,
1969 * and adds a filter.
1972 ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
1973 struct flow_cls_offload *f,
1974 struct ice_tc_flower_fltr **__fltr)
1976 struct ice_tc_flower_fltr *fltr;
1979 /* by default, set output to be INVALID */
1982 fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1986 fltr->cookie = f->cookie;
1987 fltr->extack = f->common.extack;
1988 fltr->src_vsi = vsi;
1989 INIT_HLIST_NODE(&fltr->tc_flower_node);
1991 err = ice_parse_cls_flower(netdev, vsi, f, fltr);
1995 err = ice_parse_tc_flower_actions(netdev, vsi, f, fltr);
1999 err = ice_add_switch_fltr(vsi, fltr);
2003 /* return the newly created filter */
2013 * ice_find_tc_flower_fltr - Find the TC flower filter in the list
2014 * @pf: Pointer to PF
2015 * @cookie: filter specific cookie
2017 static struct ice_tc_flower_fltr *
2018 ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
2020 struct ice_tc_flower_fltr *fltr;
2022 hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
2023 if (cookie == fltr->cookie)
2030 * ice_add_cls_flower - add TC flower filters
2031 * @netdev: Pointer to filter device
2032 * @vsi: Pointer to VSI
2033 * @cls_flower: Pointer to flower offload structure
2036 ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
2037 struct flow_cls_offload *cls_flower)
2039 struct netlink_ext_ack *extack = cls_flower->common.extack;
2040 struct net_device *vsi_netdev = vsi->netdev;
2041 struct ice_tc_flower_fltr *fltr;
2042 struct ice_pf *pf = vsi->back;
2045 if (ice_is_reset_in_progress(pf->state))
2047 if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2050 if (ice_is_port_repr_netdev(netdev))
2051 vsi_netdev = netdev;
2053 if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
2054 !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
2055 /* Based on TC indirect notifications from kernel, all ice
2056 * devices get an instance of rule from higher level device.
2057 * Avoid triggering explicit error in this case.
2059 if (netdev == vsi_netdev)
2060 NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
2064 /* avoid duplicate entries, if exists - return error */
2065 fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
2067 NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
2071 /* prep and add TC-flower filter in HW */
2072 err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr);
2076 /* add filter into an ordered list */
2077 hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
2082 * ice_del_cls_flower - delete TC flower filters
2083 * @vsi: Pointer to VSI
2084 * @cls_flower: Pointer to struct flow_cls_offload
2087 ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
2089 struct ice_tc_flower_fltr *fltr;
2090 struct ice_pf *pf = vsi->back;
2094 fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
2096 if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
2097 hlist_empty(&pf->tc_flower_fltr_list))
2100 NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
2104 fltr->extack = cls_flower->common.extack;
2105 /* delete filter from HW */
2106 err = ice_del_tc_fltr(vsi, fltr);
2110 /* delete filter from an ordered list */
2111 hlist_del(&fltr->tc_flower_node);
2113 /* free the filter node */
2120 * ice_replay_tc_fltrs - replay TC filters
2121 * @pf: pointer to PF struct
2123 void ice_replay_tc_fltrs(struct ice_pf *pf)
2125 struct ice_tc_flower_fltr *fltr;
2126 struct hlist_node *node;
2128 hlist_for_each_entry_safe(fltr, node,
2129 &pf->tc_flower_fltr_list,
2131 fltr->extack = NULL;
2132 ice_add_switch_fltr(fltr->src_vsi, fltr);