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"
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
16 * Determine lookup count based on TC flower input for switch filter.
19 ice_tc_count_lkups(u32 flags, struct ice_tc_flower_lyr_2_4_hdrs *headers,
20 struct ice_tc_flower_fltr *fltr)
24 if (flags & ICE_TC_FLWR_FIELD_TENANT_ID)
27 if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC)
30 if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS)
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))
39 if (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
40 ICE_TC_FLWR_FIELD_ENC_IP_TTL))
43 if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT)
46 if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID)
49 /* are MAC fields specified? */
50 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC | ICE_TC_FLWR_FIELD_SRC_MAC))
53 /* is VLAN specified? */
54 if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO))
57 /* is VLAN TPID specified */
58 if (flags & ICE_TC_FLWR_FIELD_VLAN_TPID)
61 /* is CVLAN specified? */
62 if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO))
65 /* are PPPoE options specified? */
66 if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
67 ICE_TC_FLWR_FIELD_PPP_PROTO))
70 /* are IPv[4|6] fields specified? */
71 if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 | ICE_TC_FLWR_FIELD_SRC_IPV4 |
72 ICE_TC_FLWR_FIELD_DEST_IPV6 | ICE_TC_FLWR_FIELD_SRC_IPV6))
75 if (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))
78 /* are L2TPv3 options specified? */
79 if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID)
82 /* is L4 (TCP/UDP/any other L4 protocol fields) specified? */
83 if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
84 ICE_TC_FLWR_FIELD_SRC_L4_PORT))
87 /* matching for tunneled packets in metadata */
88 if (fltr->tunnel_type != TNL_LAST)
94 static enum ice_protocol_type ice_proto_type_from_mac(bool inner)
96 return inner ? ICE_MAC_IL : ICE_MAC_OFOS;
99 static enum ice_protocol_type ice_proto_type_from_etype(bool inner)
101 return inner ? ICE_ETYPE_IL : ICE_ETYPE_OL;
104 static enum ice_protocol_type ice_proto_type_from_ipv4(bool inner)
106 return inner ? ICE_IPV4_IL : ICE_IPV4_OFOS;
109 static enum ice_protocol_type ice_proto_type_from_ipv6(bool inner)
111 return inner ? ICE_IPV6_IL : ICE_IPV6_OFOS;
114 static enum ice_protocol_type ice_proto_type_from_l4_port(u16 ip_proto)
126 static enum ice_protocol_type
127 ice_proto_type_from_tunnel(enum ice_tunnel_type type)
137 /* NO_PAY profiles will not work with GTP-U */
140 return ICE_GTP_NO_PAY;
146 static enum ice_sw_tunnel_type
147 ice_sw_type_from_tunnel(enum ice_tunnel_type type)
151 return ICE_SW_TUN_VXLAN;
153 return ICE_SW_TUN_GENEVE;
155 return ICE_SW_TUN_NVGRE;
157 return ICE_SW_TUN_GTPU;
159 return ICE_SW_TUN_GTPC;
165 static u16 ice_check_supported_vlan_tpid(u16 vlan_tpid)
178 ice_tc_fill_tunnel_outer(u32 flags, struct ice_tc_flower_fltr *fltr,
179 struct ice_adv_lkup_elem *list)
181 struct ice_tc_flower_lyr_2_4_hdrs *hdr = &fltr->outer_headers;
184 if (flags & ICE_TC_FLWR_FIELD_TENANT_ID) {
187 list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
188 switch (fltr->tunnel_type) {
191 tenant_id = be32_to_cpu(fltr->tenant_id) << 8;
192 list[i].h_u.tnl_hdr.vni = cpu_to_be32(tenant_id);
193 memcpy(&list[i].m_u.tnl_hdr.vni, "\xff\xff\xff\x00", 4);
197 list[i].h_u.nvgre_hdr.tni_flow = fltr->tenant_id;
198 memcpy(&list[i].m_u.nvgre_hdr.tni_flow,
199 "\xff\xff\xff\xff", 4);
204 list[i].h_u.gtp_hdr.teid = fltr->tenant_id;
205 memcpy(&list[i].m_u.gtp_hdr.teid,
206 "\xff\xff\xff\xff", 4);
214 if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC) {
215 list[i].type = ice_proto_type_from_mac(false);
216 ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
217 hdr->l2_key.dst_mac);
218 ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
219 hdr->l2_mask.dst_mac);
223 if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS &&
224 (fltr->tunnel_type == TNL_GTPU || fltr->tunnel_type == TNL_GTPC)) {
225 list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
227 if (fltr->gtp_pdu_info_masks.pdu_type) {
228 list[i].h_u.gtp_hdr.pdu_type =
229 fltr->gtp_pdu_info_keys.pdu_type << 4;
230 memcpy(&list[i].m_u.gtp_hdr.pdu_type, "\xf0", 1);
233 if (fltr->gtp_pdu_info_masks.qfi) {
234 list[i].h_u.gtp_hdr.qfi = fltr->gtp_pdu_info_keys.qfi;
235 memcpy(&list[i].m_u.gtp_hdr.qfi, "\x3f", 1);
241 if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
242 ICE_TC_FLWR_FIELD_ENC_DEST_IPV4)) {
243 list[i].type = ice_proto_type_from_ipv4(false);
245 if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV4) {
246 list[i].h_u.ipv4_hdr.src_addr = hdr->l3_key.src_ipv4;
247 list[i].m_u.ipv4_hdr.src_addr = hdr->l3_mask.src_ipv4;
249 if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV4) {
250 list[i].h_u.ipv4_hdr.dst_addr = hdr->l3_key.dst_ipv4;
251 list[i].m_u.ipv4_hdr.dst_addr = hdr->l3_mask.dst_ipv4;
256 if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
257 ICE_TC_FLWR_FIELD_ENC_DEST_IPV6)) {
258 list[i].type = ice_proto_type_from_ipv6(false);
260 if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV6) {
261 memcpy(&list[i].h_u.ipv6_hdr.src_addr,
262 &hdr->l3_key.src_ipv6_addr,
263 sizeof(hdr->l3_key.src_ipv6_addr));
264 memcpy(&list[i].m_u.ipv6_hdr.src_addr,
265 &hdr->l3_mask.src_ipv6_addr,
266 sizeof(hdr->l3_mask.src_ipv6_addr));
268 if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV6) {
269 memcpy(&list[i].h_u.ipv6_hdr.dst_addr,
270 &hdr->l3_key.dst_ipv6_addr,
271 sizeof(hdr->l3_key.dst_ipv6_addr));
272 memcpy(&list[i].m_u.ipv6_hdr.dst_addr,
273 &hdr->l3_mask.dst_ipv6_addr,
274 sizeof(hdr->l3_mask.dst_ipv6_addr));
279 if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IP) &&
280 (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
281 ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
282 list[i].type = ice_proto_type_from_ipv4(false);
284 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
285 list[i].h_u.ipv4_hdr.tos = hdr->l3_key.tos;
286 list[i].m_u.ipv4_hdr.tos = hdr->l3_mask.tos;
289 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
290 list[i].h_u.ipv4_hdr.time_to_live = hdr->l3_key.ttl;
291 list[i].m_u.ipv4_hdr.time_to_live = hdr->l3_mask.ttl;
297 if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IPV6) &&
298 (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
299 ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
300 struct ice_ipv6_hdr *hdr_h, *hdr_m;
302 hdr_h = &list[i].h_u.ipv6_hdr;
303 hdr_m = &list[i].m_u.ipv6_hdr;
304 list[i].type = ice_proto_type_from_ipv6(false);
306 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
307 be32p_replace_bits(&hdr_h->be_ver_tc_flow,
309 ICE_IPV6_HDR_TC_MASK);
310 be32p_replace_bits(&hdr_m->be_ver_tc_flow,
312 ICE_IPV6_HDR_TC_MASK);
315 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
316 hdr_h->hop_limit = hdr->l3_key.ttl;
317 hdr_m->hop_limit = hdr->l3_mask.ttl;
323 if ((flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT) &&
324 hdr->l3_key.ip_proto == IPPROTO_UDP) {
325 list[i].type = ICE_UDP_OF;
326 list[i].h_u.l4_hdr.dst_port = hdr->l4_key.dst_port;
327 list[i].m_u.l4_hdr.dst_port = hdr->l4_mask.dst_port;
331 /* always fill matching on tunneled packets in metadata */
332 ice_rule_add_tunnel_metadata(&list[i]);
339 * ice_tc_fill_rules - fill filter rules based on TC fltr
340 * @hw: pointer to HW structure
341 * @flags: tc flower field flags
342 * @tc_fltr: pointer to TC flower filter
343 * @list: list of advance rule elements
344 * @rule_info: pointer to information about rule
345 * @l4_proto: pointer to information such as L4 proto type
347 * Fill ice_adv_lkup_elem list based on TC flower flags and
348 * TC flower headers. This list should be used to add
349 * advance filter in hardware.
352 ice_tc_fill_rules(struct ice_hw *hw, u32 flags,
353 struct ice_tc_flower_fltr *tc_fltr,
354 struct ice_adv_lkup_elem *list,
355 struct ice_adv_rule_info *rule_info,
358 struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
363 rule_info->vlan_type = vlan_tpid;
365 rule_info->tun_type = ice_sw_type_from_tunnel(tc_fltr->tunnel_type);
366 if (tc_fltr->tunnel_type != TNL_LAST) {
367 i = ice_tc_fill_tunnel_outer(flags, tc_fltr, list);
369 headers = &tc_fltr->inner_headers;
373 if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID) {
374 list[i].type = ice_proto_type_from_etype(inner);
375 list[i].h_u.ethertype.ethtype_id = headers->l2_key.n_proto;
376 list[i].m_u.ethertype.ethtype_id = headers->l2_mask.n_proto;
380 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
381 ICE_TC_FLWR_FIELD_SRC_MAC)) {
382 struct ice_tc_l2_hdr *l2_key, *l2_mask;
384 l2_key = &headers->l2_key;
385 l2_mask = &headers->l2_mask;
387 list[i].type = ice_proto_type_from_mac(inner);
388 if (flags & ICE_TC_FLWR_FIELD_DST_MAC) {
389 ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
391 ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
394 if (flags & ICE_TC_FLWR_FIELD_SRC_MAC) {
395 ether_addr_copy(list[i].h_u.eth_hdr.src_addr,
397 ether_addr_copy(list[i].m_u.eth_hdr.src_addr,
404 if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO)) {
405 if (flags & ICE_TC_FLWR_FIELD_CVLAN)
406 list[i].type = ICE_VLAN_EX;
408 list[i].type = ICE_VLAN_OFOS;
410 if (flags & ICE_TC_FLWR_FIELD_VLAN) {
411 list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
412 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
415 if (flags & ICE_TC_FLWR_FIELD_VLAN_PRIO) {
416 if (flags & ICE_TC_FLWR_FIELD_VLAN) {
417 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
419 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
420 list[i].h_u.vlan_hdr.vlan = 0;
422 list[i].h_u.vlan_hdr.vlan |=
423 headers->vlan_hdr.vlan_prio;
429 if (flags & ICE_TC_FLWR_FIELD_VLAN_TPID) {
430 vlan_tpid = be16_to_cpu(headers->vlan_hdr.vlan_tpid);
431 rule_info->vlan_type =
432 ice_check_supported_vlan_tpid(vlan_tpid);
434 ice_rule_add_vlan_metadata(&list[i]);
438 if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO)) {
439 list[i].type = ICE_VLAN_IN;
441 if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
442 list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
443 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
446 if (flags & ICE_TC_FLWR_FIELD_CVLAN_PRIO) {
447 if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
448 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
450 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
451 list[i].h_u.vlan_hdr.vlan = 0;
453 list[i].h_u.vlan_hdr.vlan |=
454 headers->cvlan_hdr.vlan_prio;
460 if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
461 ICE_TC_FLWR_FIELD_PPP_PROTO)) {
462 struct ice_pppoe_hdr *vals, *masks;
464 vals = &list[i].h_u.pppoe_hdr;
465 masks = &list[i].m_u.pppoe_hdr;
467 list[i].type = ICE_PPPOE;
469 if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
470 vals->session_id = headers->pppoe_hdr.session_id;
471 masks->session_id = cpu_to_be16(0xFFFF);
474 if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
475 vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
476 masks->ppp_prot_id = cpu_to_be16(0xFFFF);
482 /* copy L3 (IPv[4|6]: src, dest) address */
483 if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
484 ICE_TC_FLWR_FIELD_SRC_IPV4)) {
485 struct ice_tc_l3_hdr *l3_key, *l3_mask;
487 list[i].type = ice_proto_type_from_ipv4(inner);
488 l3_key = &headers->l3_key;
489 l3_mask = &headers->l3_mask;
490 if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
491 list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
492 list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
494 if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
495 list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
496 list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
499 } else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
500 ICE_TC_FLWR_FIELD_SRC_IPV6)) {
501 struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
502 struct ice_tc_l3_hdr *l3_key, *l3_mask;
504 list[i].type = ice_proto_type_from_ipv6(inner);
505 ipv6_hdr = &list[i].h_u.ipv6_hdr;
506 ipv6_mask = &list[i].m_u.ipv6_hdr;
507 l3_key = &headers->l3_key;
508 l3_mask = &headers->l3_mask;
510 if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
511 memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
512 sizeof(l3_key->dst_ipv6_addr));
513 memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
514 sizeof(l3_mask->dst_ipv6_addr));
516 if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
517 memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
518 sizeof(l3_key->src_ipv6_addr));
519 memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
520 sizeof(l3_mask->src_ipv6_addr));
525 if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
526 (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
527 list[i].type = ice_proto_type_from_ipv4(inner);
529 if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
530 list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
531 list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
534 if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
535 list[i].h_u.ipv4_hdr.time_to_live =
537 list[i].m_u.ipv4_hdr.time_to_live =
538 headers->l3_mask.ttl;
544 if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
545 (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
546 struct ice_ipv6_hdr *hdr_h, *hdr_m;
548 hdr_h = &list[i].h_u.ipv6_hdr;
549 hdr_m = &list[i].m_u.ipv6_hdr;
550 list[i].type = ice_proto_type_from_ipv6(inner);
552 if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
553 be32p_replace_bits(&hdr_h->be_ver_tc_flow,
555 ICE_IPV6_HDR_TC_MASK);
556 be32p_replace_bits(&hdr_m->be_ver_tc_flow,
557 headers->l3_mask.tos,
558 ICE_IPV6_HDR_TC_MASK);
561 if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
562 hdr_h->hop_limit = headers->l3_key.ttl;
563 hdr_m->hop_limit = headers->l3_mask.ttl;
569 if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
570 list[i].type = ICE_L2TPV3;
572 list[i].h_u.l2tpv3_sess_hdr.session_id =
573 headers->l2tpv3_hdr.session_id;
574 list[i].m_u.l2tpv3_sess_hdr.session_id =
575 cpu_to_be32(0xFFFFFFFF);
580 /* copy L4 (src, dest) port */
581 if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
582 ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
583 struct ice_tc_l4_hdr *l4_key, *l4_mask;
585 list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
586 l4_key = &headers->l4_key;
587 l4_mask = &headers->l4_mask;
589 if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
590 list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
591 list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
593 if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
594 list[i].h_u.l4_hdr.src_port = l4_key->src_port;
595 list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
604 * ice_tc_tun_get_type - get the tunnel type
605 * @tunnel_dev: ptr to tunnel device
607 * This function detects appropriate tunnel_type if specified device is
608 * tunnel device such as VXLAN/Geneve
610 static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
612 if (netif_is_vxlan(tunnel_dev))
614 if (netif_is_geneve(tunnel_dev))
616 if (netif_is_gretap(tunnel_dev) ||
617 netif_is_ip6gretap(tunnel_dev))
620 /* Assume GTP-U by default in case of GTP netdev.
621 * GTP-C may be selected later, based on enc_dst_port.
623 if (netif_is_gtp(tunnel_dev))
628 bool ice_is_tunnel_supported(struct net_device *dev)
630 return ice_tc_tun_get_type(dev) != TNL_LAST;
634 ice_eswitch_tc_parse_action(struct ice_tc_flower_fltr *fltr,
635 struct flow_action_entry *act)
637 struct ice_repr *repr;
640 case FLOW_ACTION_DROP:
641 fltr->action.fltr_act = ICE_DROP_PACKET;
644 case FLOW_ACTION_REDIRECT:
645 fltr->action.fltr_act = ICE_FWD_TO_VSI;
647 if (ice_is_port_repr_netdev(act->dev)) {
648 repr = ice_netdev_to_repr(act->dev);
650 fltr->dest_vsi = repr->src_vsi;
651 fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
652 } else if (netif_is_ice(act->dev) ||
653 ice_is_tunnel_supported(act->dev)) {
654 fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
656 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported netdevice in switchdev mode");
663 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
671 ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
673 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
674 struct ice_adv_rule_info rule_info = { 0 };
675 struct ice_rule_query_data rule_added;
676 struct ice_hw *hw = &vsi->back->hw;
677 struct ice_adv_lkup_elem *list;
678 u32 flags = fltr->flags;
683 if (!flags || (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT)) {
684 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
688 lkups_cnt = ice_tc_count_lkups(flags, headers, fltr);
689 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
693 i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
694 if (i != lkups_cnt) {
699 /* egress traffic is always redirect to uplink */
700 if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
701 fltr->dest_vsi = vsi->back->switchdev.uplink_vsi;
703 rule_info.sw_act.fltr_act = fltr->action.fltr_act;
704 if (fltr->action.fltr_act != ICE_DROP_PACKET)
705 rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
706 /* For now, making priority to be highest, and it also becomes
707 * the priority for recipe which will get created as a result of
708 * new extraction sequence based on input set.
709 * Priority '7' is max val for switch recipe, higher the number
710 * results into order of switch rule evaluation.
712 rule_info.priority = 7;
713 rule_info.flags_info.act_valid = true;
715 if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
716 rule_info.sw_act.flag |= ICE_FLTR_RX;
717 rule_info.sw_act.src = hw->pf_id;
718 rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
720 rule_info.sw_act.flag |= ICE_FLTR_TX;
721 rule_info.sw_act.src = vsi->idx;
722 rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
725 /* specify the cookie as filter_rule_id */
726 rule_info.fltr_rule_id = fltr->cookie;
728 ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
729 if (ret == -EEXIST) {
730 NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
734 NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
738 /* store the output params, which are needed later for removing
739 * advanced switch filter
741 fltr->rid = rule_added.rid;
742 fltr->rule_id = rule_added.rule_id;
743 fltr->dest_vsi_handle = rule_added.vsi_handle;
751 * ice_locate_vsi_using_queue - locate VSI using queue (forward to queue action)
752 * @vsi: Pointer to VSI
753 * @queue: Queue index
755 * Locate the VSI using specified "queue". When ADQ is not enabled,
756 * always return input VSI, otherwise locate corresponding
757 * VSI based on per channel "offset" and "qcount"
760 ice_locate_vsi_using_queue(struct ice_vsi *vsi, int queue)
764 /* if ADQ is not active, passed VSI is the candidate VSI */
765 if (!ice_is_adq_active(vsi->back))
768 /* Locate the VSI (it could still be main PF VSI or CHNL_VSI depending
771 num_tc = vsi->mqprio_qopt.qopt.num_tc;
773 for (tc = 0; tc < num_tc; tc++) {
774 int qcount = vsi->mqprio_qopt.qopt.count[tc];
775 int offset = vsi->mqprio_qopt.qopt.offset[tc];
777 if (queue >= offset && queue < offset + qcount) {
778 /* for non-ADQ TCs, passed VSI is the candidate VSI */
779 if (tc < ICE_CHNL_START_TC)
782 return vsi->tc_map_vsi[tc];
788 static struct ice_rx_ring *
789 ice_locate_rx_ring_using_queue(struct ice_vsi *vsi,
790 struct ice_tc_flower_fltr *tc_fltr)
792 u16 queue = tc_fltr->action.fwd.q.queue;
794 return queue < vsi->num_rxq ? vsi->rx_rings[queue] : NULL;
798 * ice_tc_forward_action - Determine destination VSI and queue for the action
799 * @vsi: Pointer to VSI
800 * @tc_fltr: Pointer to TC flower filter structure
802 * Validates the tc forward action and determines the destination VSI and queue
803 * for the forward action.
805 static struct ice_vsi *
806 ice_tc_forward_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *tc_fltr)
808 struct ice_rx_ring *ring = NULL;
809 struct ice_vsi *dest_vsi = NULL;
810 struct ice_pf *pf = vsi->back;
815 dev = ice_pf_to_dev(pf);
817 /* Get the destination VSI and/or destination queue and validate them */
818 switch (tc_fltr->action.fltr_act) {
820 tc_class = tc_fltr->action.fwd.tc.tc_class;
821 /* Select the destination VSI */
822 if (tc_class < ICE_CHNL_START_TC) {
823 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
824 "Unable to add filter because of unsupported destination");
825 return ERR_PTR(-EOPNOTSUPP);
827 /* Locate ADQ VSI depending on hw_tc number */
828 dest_vsi = vsi->tc_map_vsi[tc_class];
831 /* Locate the Rx queue */
832 ring = ice_locate_rx_ring_using_queue(vsi, tc_fltr);
835 "Unable to locate Rx queue for action fwd_to_queue: %u\n",
836 tc_fltr->action.fwd.q.queue);
837 return ERR_PTR(-EINVAL);
839 /* Determine destination VSI even though the action is
840 * FWD_TO_QUEUE, because QUEUE is associated with VSI
842 q = tc_fltr->action.fwd.q.queue;
843 dest_vsi = ice_locate_vsi_using_queue(vsi, q);
847 "Unable to add filter because of unsupported action %u (supported actions: fwd to tc, fwd to queue)\n",
848 tc_fltr->action.fltr_act);
849 return ERR_PTR(-EINVAL);
851 /* Must have valid dest_vsi (it could be main VSI or ADQ VSI) */
854 "Unable to add filter because specified destination VSI doesn't exist\n");
855 return ERR_PTR(-EINVAL);
861 * ice_add_tc_flower_adv_fltr - add appropriate filter rules
862 * @vsi: Pointer to VSI
863 * @tc_fltr: Pointer to TC flower filter structure
865 * based on filter parameters using Advance recipes supported
869 ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
870 struct ice_tc_flower_fltr *tc_fltr)
872 struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
873 struct ice_adv_rule_info rule_info = {0};
874 struct ice_rule_query_data rule_added;
875 struct ice_adv_lkup_elem *list;
876 struct ice_pf *pf = vsi->back;
877 struct ice_hw *hw = &pf->hw;
878 u32 flags = tc_fltr->flags;
879 struct ice_vsi *dest_vsi;
886 dev = ice_pf_to_dev(pf);
887 if (ice_is_safe_mode(pf)) {
888 NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
892 if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
893 ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
894 ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
895 ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
896 ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
897 NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
901 /* validate forwarding action VSI and queue */
902 if (ice_is_forward_action(tc_fltr->action.fltr_act)) {
903 dest_vsi = ice_tc_forward_action(vsi, tc_fltr);
904 if (IS_ERR(dest_vsi))
905 return PTR_ERR(dest_vsi);
908 lkups_cnt = ice_tc_count_lkups(flags, headers, tc_fltr);
909 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
913 i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
914 if (i != lkups_cnt) {
919 rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
920 /* specify the cookie as filter_rule_id */
921 rule_info.fltr_rule_id = tc_fltr->cookie;
923 switch (tc_fltr->action.fltr_act) {
925 rule_info.sw_act.vsi_handle = dest_vsi->idx;
926 rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
927 rule_info.sw_act.src = hw->pf_id;
928 dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
929 tc_fltr->action.fwd.tc.tc_class,
930 rule_info.sw_act.vsi_handle, lkups_cnt);
933 /* HW queue number in global space */
934 rule_info.sw_act.fwd_id.q_id = tc_fltr->action.fwd.q.hw_queue;
935 rule_info.sw_act.vsi_handle = dest_vsi->idx;
936 rule_info.priority = ICE_SWITCH_FLTR_PRIO_QUEUE;
937 rule_info.sw_act.src = hw->pf_id;
938 dev_dbg(dev, "add switch rule action to forward to queue:%u (HW queue %u), lkups_cnt:%u\n",
939 tc_fltr->action.fwd.q.queue,
940 tc_fltr->action.fwd.q.hw_queue, lkups_cnt);
942 case ICE_DROP_PACKET:
943 rule_info.sw_act.flag |= ICE_FLTR_RX;
944 rule_info.sw_act.src = hw->pf_id;
945 rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
952 ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
953 if (ret == -EEXIST) {
954 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
955 "Unable to add filter because it already exist");
959 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
960 "Unable to add filter due to error");
964 /* store the output params, which are needed later for removing
965 * advanced switch filter
967 tc_fltr->rid = rule_added.rid;
968 tc_fltr->rule_id = rule_added.rule_id;
969 tc_fltr->dest_vsi_handle = rule_added.vsi_handle;
970 if (tc_fltr->action.fltr_act == ICE_FWD_TO_VSI ||
971 tc_fltr->action.fltr_act == ICE_FWD_TO_Q) {
972 tc_fltr->dest_vsi = dest_vsi;
973 /* keep track of advanced switch filter for
976 dest_vsi->num_chnl_fltr++;
978 /* keeps track of channel filters for PF VSI */
979 if (vsi->type == ICE_VSI_PF &&
980 (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
981 ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
982 pf->num_dmac_chnl_fltrs++;
984 switch (tc_fltr->action.fltr_act) {
986 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",
988 tc_fltr->action.fwd.tc.tc_class, rule_added.rid,
989 rule_added.rule_id, rule_added.vsi_handle);
992 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",
993 lkups_cnt, flags, tc_fltr->action.fwd.q.queue,
994 tc_fltr->action.fwd.q.hw_queue, rule_added.rid,
997 case ICE_DROP_PACKET:
998 dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is drop, rid %u, rule_id %u\n",
999 lkups_cnt, flags, rule_added.rid, rule_added.rule_id);
1010 * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
1011 * @match: Pointer to flow match structure
1012 * @fltr: Pointer to filter structure
1013 * @headers: Pointer to outer header fields
1014 * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
1017 ice_tc_set_pppoe(struct flow_match_pppoe *match,
1018 struct ice_tc_flower_fltr *fltr,
1019 struct ice_tc_flower_lyr_2_4_hdrs *headers)
1021 if (match->mask->session_id) {
1022 fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
1023 headers->pppoe_hdr.session_id = match->key->session_id;
1026 if (match->mask->ppp_proto) {
1027 fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
1028 headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
1031 return be16_to_cpu(match->key->type);
1035 * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
1036 * @match: Pointer to flow match structure
1037 * @fltr: Pointer to filter structure
1038 * @headers: inner or outer header fields
1039 * @is_encap: set true for tunnel IPv4 address
1042 ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
1043 struct ice_tc_flower_fltr *fltr,
1044 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1046 if (match->key->dst) {
1048 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
1050 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
1051 headers->l3_key.dst_ipv4 = match->key->dst;
1052 headers->l3_mask.dst_ipv4 = match->mask->dst;
1054 if (match->key->src) {
1056 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
1058 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
1059 headers->l3_key.src_ipv4 = match->key->src;
1060 headers->l3_mask.src_ipv4 = match->mask->src;
1066 * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
1067 * @match: Pointer to flow match structure
1068 * @fltr: Pointer to filter structure
1069 * @headers: inner or outer header fields
1070 * @is_encap: set true for tunnel IPv6 address
1073 ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
1074 struct ice_tc_flower_fltr *fltr,
1075 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1077 struct ice_tc_l3_hdr *l3_key, *l3_mask;
1079 /* src and dest IPV6 address should not be LOOPBACK
1080 * (0:0:0:0:0:0:0:1), which can be represented as ::1
1082 if (ipv6_addr_loopback(&match->key->dst) ||
1083 ipv6_addr_loopback(&match->key->src)) {
1084 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
1087 /* if src/dest IPv6 address is *,* error */
1088 if (ipv6_addr_any(&match->mask->dst) &&
1089 ipv6_addr_any(&match->mask->src)) {
1090 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
1093 if (!ipv6_addr_any(&match->mask->dst)) {
1095 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
1097 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
1099 if (!ipv6_addr_any(&match->mask->src)) {
1101 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
1103 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
1106 l3_key = &headers->l3_key;
1107 l3_mask = &headers->l3_mask;
1109 if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
1110 ICE_TC_FLWR_FIELD_SRC_IPV6)) {
1111 memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
1112 sizeof(match->key->src.s6_addr));
1113 memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
1114 sizeof(match->mask->src.s6_addr));
1116 if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
1117 ICE_TC_FLWR_FIELD_DEST_IPV6)) {
1118 memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
1119 sizeof(match->key->dst.s6_addr));
1120 memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
1121 sizeof(match->mask->dst.s6_addr));
1128 * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
1129 * @match: Pointer to flow match structure
1130 * @fltr: Pointer to filter structure
1131 * @headers: inner or outer header fields
1132 * @is_encap: set true for tunnel
1135 ice_tc_set_tos_ttl(struct flow_match_ip *match,
1136 struct ice_tc_flower_fltr *fltr,
1137 struct ice_tc_flower_lyr_2_4_hdrs *headers,
1140 if (match->mask->tos) {
1142 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
1144 fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
1146 headers->l3_key.tos = match->key->tos;
1147 headers->l3_mask.tos = match->mask->tos;
1150 if (match->mask->ttl) {
1152 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
1154 fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
1156 headers->l3_key.ttl = match->key->ttl;
1157 headers->l3_mask.ttl = match->mask->ttl;
1162 * ice_tc_set_port - Parse ports from TC flower filter
1163 * @match: Flow match structure
1164 * @fltr: Pointer to filter structure
1165 * @headers: inner or outer header fields
1166 * @is_encap: set true for tunnel port
1169 ice_tc_set_port(struct flow_match_ports match,
1170 struct ice_tc_flower_fltr *fltr,
1171 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1173 if (match.key->dst) {
1175 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
1177 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
1179 headers->l4_key.dst_port = match.key->dst;
1180 headers->l4_mask.dst_port = match.mask->dst;
1182 if (match.key->src) {
1184 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
1186 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
1188 headers->l4_key.src_port = match.key->src;
1189 headers->l4_mask.src_port = match.mask->src;
1194 static struct net_device *
1195 ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
1197 struct flow_action_entry *act;
1200 if (ice_is_tunnel_supported(dev))
1203 flow_action_for_each(i, act, &rule->action) {
1204 if (act->id == FLOW_ACTION_REDIRECT &&
1205 ice_is_tunnel_supported(act->dev))
1213 * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
1214 * @match: Flow match structure
1215 * @fltr: Pointer to filter structure
1217 * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
1218 * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
1219 * therefore making GTP-U the default choice (when destination port number is
1223 ice_parse_gtp_type(struct flow_match_ports match,
1224 struct ice_tc_flower_fltr *fltr)
1228 if (match.key->dst) {
1229 dst_port = be16_to_cpu(match.key->dst);
1235 fltr->tunnel_type = TNL_GTPC;
1238 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
1247 ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
1248 struct ice_tc_flower_fltr *fltr)
1250 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1251 struct flow_match_control enc_control;
1253 fltr->tunnel_type = ice_tc_tun_get_type(dev);
1254 headers->l3_key.ip_proto = IPPROTO_UDP;
1256 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1257 struct flow_match_enc_keyid enc_keyid;
1259 flow_rule_match_enc_keyid(rule, &enc_keyid);
1261 if (!enc_keyid.mask->keyid ||
1262 enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
1265 fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
1266 fltr->tenant_id = enc_keyid.key->keyid;
1269 flow_rule_match_enc_control(rule, &enc_control);
1271 if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1272 struct flow_match_ipv4_addrs match;
1274 flow_rule_match_enc_ipv4_addrs(rule, &match);
1275 if (ice_tc_set_ipv4(&match, fltr, headers, true))
1277 } else if (enc_control.key->addr_type ==
1278 FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1279 struct flow_match_ipv6_addrs match;
1281 flow_rule_match_enc_ipv6_addrs(rule, &match);
1282 if (ice_tc_set_ipv6(&match, fltr, headers, true))
1286 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
1287 struct flow_match_ip match;
1289 flow_rule_match_enc_ip(rule, &match);
1290 ice_tc_set_tos_ttl(&match, fltr, headers, true);
1293 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
1294 fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
1295 struct flow_match_ports match;
1297 flow_rule_match_enc_ports(rule, &match);
1299 if (fltr->tunnel_type != TNL_GTPU) {
1300 if (ice_tc_set_port(match, fltr, headers, true))
1303 if (ice_parse_gtp_type(match, fltr))
1308 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
1309 struct flow_match_enc_opts match;
1311 flow_rule_match_enc_opts(rule, &match);
1313 memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
1314 sizeof(struct gtp_pdu_session_info));
1316 memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
1317 sizeof(struct gtp_pdu_session_info));
1319 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_OPTS;
1326 * ice_parse_cls_flower - Parse TC flower filters provided by kernel
1327 * @vsi: Pointer to the VSI
1328 * @filter_dev: Pointer to device on which filter is being added
1329 * @f: Pointer to struct flow_cls_offload
1330 * @fltr: Pointer to filter structure
1333 ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
1334 struct flow_cls_offload *f,
1335 struct ice_tc_flower_fltr *fltr)
1337 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1338 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1339 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
1340 struct flow_dissector *dissector;
1341 struct net_device *tunnel_dev;
1343 dissector = rule->match.dissector;
1345 if (dissector->used_keys &
1346 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1347 BIT(FLOW_DISSECTOR_KEY_BASIC) |
1348 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1349 BIT(FLOW_DISSECTOR_KEY_VLAN) |
1350 BIT(FLOW_DISSECTOR_KEY_CVLAN) |
1351 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1352 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1353 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1354 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1355 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1356 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1357 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1358 BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1359 BIT(FLOW_DISSECTOR_KEY_IP) |
1360 BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
1361 BIT(FLOW_DISSECTOR_KEY_PORTS) |
1362 BIT(FLOW_DISSECTOR_KEY_PPPOE) |
1363 BIT(FLOW_DISSECTOR_KEY_L2TPV3))) {
1364 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
1368 tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
1372 filter_dev = tunnel_dev;
1374 err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
1376 NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
1380 /* header pointers should point to the inner headers, outer
1381 * header were already set by ice_parse_tunnel_attr
1383 headers = &fltr->inner_headers;
1384 } else if (dissector->used_keys &
1385 (BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1386 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1387 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1388 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))) {
1389 NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
1392 fltr->tunnel_type = TNL_LAST;
1395 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1396 struct flow_match_basic match;
1398 flow_rule_match_basic(rule, &match);
1400 n_proto_key = ntohs(match.key->n_proto);
1401 n_proto_mask = ntohs(match.mask->n_proto);
1403 if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
1404 fltr->tunnel_type == TNL_GTPU ||
1405 fltr->tunnel_type == TNL_GTPC) {
1409 fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1412 headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1413 headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
1414 headers->l3_key.ip_proto = match.key->ip_proto;
1417 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1418 struct flow_match_eth_addrs match;
1420 flow_rule_match_eth_addrs(rule, &match);
1422 if (!is_zero_ether_addr(match.key->dst)) {
1423 ether_addr_copy(headers->l2_key.dst_mac,
1425 ether_addr_copy(headers->l2_mask.dst_mac,
1427 fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1430 if (!is_zero_ether_addr(match.key->src)) {
1431 ether_addr_copy(headers->l2_key.src_mac,
1433 ether_addr_copy(headers->l2_mask.src_mac,
1435 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
1439 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
1440 is_vlan_dev(filter_dev)) {
1441 struct flow_dissector_key_vlan mask;
1442 struct flow_dissector_key_vlan key;
1443 struct flow_match_vlan match;
1445 if (is_vlan_dev(filter_dev)) {
1447 match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
1448 match.key->vlan_priority = 0;
1450 memset(match.mask, 0xff, sizeof(*match.mask));
1451 match.mask->vlan_priority = 0;
1453 flow_rule_match_vlan(rule, &match);
1456 if (match.mask->vlan_id) {
1457 if (match.mask->vlan_id == VLAN_VID_MASK) {
1458 fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
1459 headers->vlan_hdr.vlan_id =
1460 cpu_to_be16(match.key->vlan_id &
1463 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
1468 if (match.mask->vlan_priority) {
1469 fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
1470 headers->vlan_hdr.vlan_prio =
1471 be16_encode_bits(match.key->vlan_priority,
1475 if (match.mask->vlan_tpid) {
1476 headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
1477 fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_TPID;
1481 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
1482 struct flow_match_vlan match;
1484 if (!ice_is_dvm_ena(&vsi->back->hw)) {
1485 NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
1489 flow_rule_match_cvlan(rule, &match);
1491 if (match.mask->vlan_id) {
1492 if (match.mask->vlan_id == VLAN_VID_MASK) {
1493 fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
1494 headers->cvlan_hdr.vlan_id =
1495 cpu_to_be16(match.key->vlan_id &
1498 NL_SET_ERR_MSG_MOD(fltr->extack,
1504 if (match.mask->vlan_priority) {
1505 fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
1506 headers->cvlan_hdr.vlan_prio =
1507 be16_encode_bits(match.key->vlan_priority,
1512 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
1513 struct flow_match_pppoe match;
1515 flow_rule_match_pppoe(rule, &match);
1516 n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
1518 /* If ethertype equals ETH_P_PPP_SES, n_proto might be
1519 * overwritten by encapsulated protocol (ppp_proto field) or set
1520 * to 0. To correct this, flow_match_pppoe provides the type
1521 * field, which contains the actual ethertype (ETH_P_PPP_SES).
1523 headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1524 headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
1525 fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1528 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1529 struct flow_match_control match;
1531 flow_rule_match_control(rule, &match);
1533 addr_type = match.key->addr_type;
1536 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1537 struct flow_match_ipv4_addrs match;
1539 flow_rule_match_ipv4_addrs(rule, &match);
1540 if (ice_tc_set_ipv4(&match, fltr, headers, false))
1544 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1545 struct flow_match_ipv6_addrs match;
1547 flow_rule_match_ipv6_addrs(rule, &match);
1548 if (ice_tc_set_ipv6(&match, fltr, headers, false))
1552 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
1553 struct flow_match_ip match;
1555 flow_rule_match_ip(rule, &match);
1556 ice_tc_set_tos_ttl(&match, fltr, headers, false);
1559 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
1560 struct flow_match_l2tpv3 match;
1562 flow_rule_match_l2tpv3(rule, &match);
1564 fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
1565 headers->l2tpv3_hdr.session_id = match.key->session_id;
1568 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
1569 struct flow_match_ports match;
1571 flow_rule_match_ports(rule, &match);
1572 if (ice_tc_set_port(match, fltr, headers, false))
1574 switch (headers->l3_key.ip_proto) {
1579 NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
1587 * ice_add_switch_fltr - Add TC flower filters
1588 * @vsi: Pointer to VSI
1589 * @fltr: Pointer to struct ice_tc_flower_fltr
1591 * Add filter in HW switch block
1594 ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1596 if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
1599 if (ice_is_eswitch_mode_switchdev(vsi->back))
1600 return ice_eswitch_add_tc_fltr(vsi, fltr);
1602 return ice_add_tc_flower_adv_fltr(vsi, fltr);
1606 * ice_prep_adq_filter - Prepare ADQ filter with the required additional headers
1607 * @vsi: Pointer to VSI
1608 * @fltr: Pointer to TC flower filter structure
1610 * Prepare ADQ filter with the required additional header fields
1613 ice_prep_adq_filter(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1615 if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
1616 (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1617 ICE_TC_FLWR_FIELD_SRC_MAC))) {
1618 NL_SET_ERR_MSG_MOD(fltr->extack,
1619 "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
1623 /* For ADQ, filter must include dest MAC address, otherwise unwanted
1624 * packets with unrelated MAC address get delivered to ADQ VSIs as long
1625 * as remaining filter criteria is satisfied such as dest IP address
1626 * and dest/src L4 port. Below code handles the following cases:
1627 * 1. For non-tunnel, if user specify MAC addresses, use them.
1628 * 2. For non-tunnel, if user didn't specify MAC address, add implicit
1629 * dest MAC to be lower netdev's active unicast MAC address
1630 * 3. For tunnel, as of now TC-filter through flower classifier doesn't
1631 * have provision for user to specify outer DMAC, hence driver to
1632 * implicitly add outer dest MAC to be lower netdev's active unicast
1635 if (fltr->tunnel_type != TNL_LAST &&
1636 !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
1637 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
1639 if (fltr->tunnel_type == TNL_LAST &&
1640 !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
1641 fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1643 if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1644 ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
1645 ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
1646 vsi->netdev->dev_addr);
1647 eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
1650 /* Make sure VLAN is already added to main VSI, before allowing ADQ to
1651 * add a VLAN based filter such as MAC + VLAN + L4 port.
1653 if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
1654 u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
1656 if (!ice_vlan_fltr_exist(&vsi->back->hw, vlan_id, vsi->idx)) {
1657 NL_SET_ERR_MSG_MOD(fltr->extack,
1658 "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
1666 * ice_handle_tclass_action - Support directing to a traffic class
1667 * @vsi: Pointer to VSI
1668 * @cls_flower: Pointer to TC flower offload structure
1669 * @fltr: Pointer to TC flower filter structure
1671 * Support directing traffic to a traffic class/queue-set
1674 ice_handle_tclass_action(struct ice_vsi *vsi,
1675 struct flow_cls_offload *cls_flower,
1676 struct ice_tc_flower_fltr *fltr)
1678 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
1680 /* user specified hw_tc (must be non-zero for ADQ TC), action is forward
1681 * to hw_tc (i.e. ADQ channel number)
1683 if (tc < ICE_CHNL_START_TC) {
1684 NL_SET_ERR_MSG_MOD(fltr->extack,
1685 "Unable to add filter because of unsupported destination");
1688 if (!(vsi->all_enatc & BIT(tc))) {
1689 NL_SET_ERR_MSG_MOD(fltr->extack,
1690 "Unable to add filter because of non-existence destination");
1693 fltr->action.fltr_act = ICE_FWD_TO_VSI;
1694 fltr->action.fwd.tc.tc_class = tc;
1696 return ice_prep_adq_filter(vsi, fltr);
1700 ice_tc_forward_to_queue(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1701 struct flow_action_entry *act)
1703 struct ice_vsi *ch_vsi = NULL;
1704 u16 queue = act->rx_queue;
1706 if (queue >= vsi->num_rxq) {
1707 NL_SET_ERR_MSG_MOD(fltr->extack,
1708 "Unable to add filter because specified queue is invalid");
1711 fltr->action.fltr_act = ICE_FWD_TO_Q;
1712 fltr->action.fwd.q.queue = queue;
1713 /* determine corresponding HW queue */
1714 fltr->action.fwd.q.hw_queue = vsi->rxq_map[queue];
1716 /* If ADQ is configured, and the queue belongs to ADQ VSI, then prepare
1719 ch_vsi = ice_locate_vsi_using_queue(vsi, fltr->action.fwd.q.queue);
1722 fltr->dest_vsi = ch_vsi;
1723 if (!ice_is_chnl_fltr(fltr))
1726 return ice_prep_adq_filter(vsi, fltr);
1730 ice_tc_parse_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1731 struct flow_action_entry *act)
1734 case FLOW_ACTION_RX_QUEUE_MAPPING:
1735 /* forward to queue */
1736 return ice_tc_forward_to_queue(vsi, fltr, act);
1737 case FLOW_ACTION_DROP:
1738 fltr->action.fltr_act = ICE_DROP_PACKET;
1741 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported TC action");
1747 * ice_parse_tc_flower_actions - Parse the actions for a TC filter
1748 * @vsi: Pointer to VSI
1749 * @cls_flower: Pointer to TC flower offload structure
1750 * @fltr: Pointer to TC flower filter structure
1752 * Parse the actions for a TC filter
1755 ice_parse_tc_flower_actions(struct ice_vsi *vsi,
1756 struct flow_cls_offload *cls_flower,
1757 struct ice_tc_flower_fltr *fltr)
1759 struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1760 struct flow_action *flow_action = &rule->action;
1761 struct flow_action_entry *act;
1764 if (cls_flower->classid)
1765 return ice_handle_tclass_action(vsi, cls_flower, fltr);
1767 if (!flow_action_has_entries(flow_action))
1770 flow_action_for_each(i, act, flow_action) {
1771 if (ice_is_eswitch_mode_switchdev(vsi->back))
1772 err = ice_eswitch_tc_parse_action(fltr, act);
1774 err = ice_tc_parse_action(vsi, fltr, act);
1783 * ice_del_tc_fltr - deletes a filter from HW table
1784 * @vsi: Pointer to VSI
1785 * @fltr: Pointer to struct ice_tc_flower_fltr
1787 * This function deletes a filter from HW table and manages book-keeping
1789 static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1791 struct ice_rule_query_data rule_rem;
1792 struct ice_pf *pf = vsi->back;
1795 rule_rem.rid = fltr->rid;
1796 rule_rem.rule_id = fltr->rule_id;
1797 rule_rem.vsi_handle = fltr->dest_vsi_handle;
1798 err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
1800 if (err == -ENOENT) {
1801 NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
1804 NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
1808 /* update advanced switch filter count for destination
1809 * VSI if filter destination was VSI
1811 if (fltr->dest_vsi) {
1812 if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
1813 fltr->dest_vsi->num_chnl_fltr--;
1815 /* keeps track of channel filters for PF VSI */
1816 if (vsi->type == ICE_VSI_PF &&
1817 (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1818 ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
1819 pf->num_dmac_chnl_fltrs--;
1826 * ice_add_tc_fltr - adds a TC flower filter
1827 * @netdev: Pointer to netdev
1828 * @vsi: Pointer to VSI
1829 * @f: Pointer to flower offload structure
1830 * @__fltr: Pointer to struct ice_tc_flower_fltr
1832 * This function parses TC-flower input fields, parses action,
1833 * and adds a filter.
1836 ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
1837 struct flow_cls_offload *f,
1838 struct ice_tc_flower_fltr **__fltr)
1840 struct ice_tc_flower_fltr *fltr;
1843 /* by default, set output to be INVALID */
1846 fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1850 fltr->cookie = f->cookie;
1851 fltr->extack = f->common.extack;
1852 fltr->src_vsi = vsi;
1853 INIT_HLIST_NODE(&fltr->tc_flower_node);
1855 err = ice_parse_cls_flower(netdev, vsi, f, fltr);
1859 err = ice_parse_tc_flower_actions(vsi, f, fltr);
1863 err = ice_add_switch_fltr(vsi, fltr);
1867 /* return the newly created filter */
1877 * ice_find_tc_flower_fltr - Find the TC flower filter in the list
1878 * @pf: Pointer to PF
1879 * @cookie: filter specific cookie
1881 static struct ice_tc_flower_fltr *
1882 ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
1884 struct ice_tc_flower_fltr *fltr;
1886 hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
1887 if (cookie == fltr->cookie)
1894 * ice_add_cls_flower - add TC flower filters
1895 * @netdev: Pointer to filter device
1896 * @vsi: Pointer to VSI
1897 * @cls_flower: Pointer to flower offload structure
1900 ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
1901 struct flow_cls_offload *cls_flower)
1903 struct netlink_ext_ack *extack = cls_flower->common.extack;
1904 struct net_device *vsi_netdev = vsi->netdev;
1905 struct ice_tc_flower_fltr *fltr;
1906 struct ice_pf *pf = vsi->back;
1909 if (ice_is_reset_in_progress(pf->state))
1911 if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
1914 if (ice_is_port_repr_netdev(netdev))
1915 vsi_netdev = netdev;
1917 if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
1918 !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
1919 /* Based on TC indirect notifications from kernel, all ice
1920 * devices get an instance of rule from higher level device.
1921 * Avoid triggering explicit error in this case.
1923 if (netdev == vsi_netdev)
1924 NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
1928 /* avoid duplicate entries, if exists - return error */
1929 fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1931 NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
1935 /* prep and add TC-flower filter in HW */
1936 err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr);
1940 /* add filter into an ordered list */
1941 hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
1946 * ice_del_cls_flower - delete TC flower filters
1947 * @vsi: Pointer to VSI
1948 * @cls_flower: Pointer to struct flow_cls_offload
1951 ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
1953 struct ice_tc_flower_fltr *fltr;
1954 struct ice_pf *pf = vsi->back;
1958 fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1960 if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
1961 hlist_empty(&pf->tc_flower_fltr_list))
1964 NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
1968 fltr->extack = cls_flower->common.extack;
1969 /* delete filter from HW */
1970 err = ice_del_tc_fltr(vsi, fltr);
1974 /* delete filter from an ordered list */
1975 hlist_del(&fltr->tc_flower_node);
1977 /* free the filter node */
1984 * ice_replay_tc_fltrs - replay TC filters
1985 * @pf: pointer to PF struct
1987 void ice_replay_tc_fltrs(struct ice_pf *pf)
1989 struct ice_tc_flower_fltr *fltr;
1990 struct hlist_node *node;
1992 hlist_for_each_entry_safe(fltr, node,
1993 &pf->tc_flower_fltr_list,
1995 fltr->extack = NULL;
1996 ice_add_switch_fltr(fltr->src_vsi, fltr);