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 CVLAN specified? */
58 if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO))
61 /* are PPPoE options specified? */
62 if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
63 ICE_TC_FLWR_FIELD_PPP_PROTO))
66 /* are IPv[4|6] fields specified? */
67 if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 | ICE_TC_FLWR_FIELD_SRC_IPV4 |
68 ICE_TC_FLWR_FIELD_DEST_IPV6 | ICE_TC_FLWR_FIELD_SRC_IPV6))
71 if (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))
74 /* are L2TPv3 options specified? */
75 if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID)
78 /* is L4 (TCP/UDP/any other L4 protocol fields) specified? */
79 if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
80 ICE_TC_FLWR_FIELD_SRC_L4_PORT))
86 static enum ice_protocol_type ice_proto_type_from_mac(bool inner)
88 return inner ? ICE_MAC_IL : ICE_MAC_OFOS;
91 static enum ice_protocol_type ice_proto_type_from_etype(bool inner)
93 return inner ? ICE_ETYPE_IL : ICE_ETYPE_OL;
96 static enum ice_protocol_type ice_proto_type_from_ipv4(bool inner)
98 return inner ? ICE_IPV4_IL : ICE_IPV4_OFOS;
101 static enum ice_protocol_type ice_proto_type_from_ipv6(bool inner)
103 return inner ? ICE_IPV6_IL : ICE_IPV6_OFOS;
106 static enum ice_protocol_type ice_proto_type_from_l4_port(u16 ip_proto)
118 static enum ice_protocol_type
119 ice_proto_type_from_tunnel(enum ice_tunnel_type type)
129 /* NO_PAY profiles will not work with GTP-U */
132 return ICE_GTP_NO_PAY;
138 static enum ice_sw_tunnel_type
139 ice_sw_type_from_tunnel(enum ice_tunnel_type type)
143 return ICE_SW_TUN_VXLAN;
145 return ICE_SW_TUN_GENEVE;
147 return ICE_SW_TUN_NVGRE;
149 return ICE_SW_TUN_GTPU;
151 return ICE_SW_TUN_GTPC;
157 static u16 ice_check_supported_vlan_tpid(u16 vlan_tpid)
170 ice_tc_fill_tunnel_outer(u32 flags, struct ice_tc_flower_fltr *fltr,
171 struct ice_adv_lkup_elem *list)
173 struct ice_tc_flower_lyr_2_4_hdrs *hdr = &fltr->outer_headers;
176 if (flags & ICE_TC_FLWR_FIELD_TENANT_ID) {
179 list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
180 switch (fltr->tunnel_type) {
183 tenant_id = be32_to_cpu(fltr->tenant_id) << 8;
184 list[i].h_u.tnl_hdr.vni = cpu_to_be32(tenant_id);
185 memcpy(&list[i].m_u.tnl_hdr.vni, "\xff\xff\xff\x00", 4);
189 list[i].h_u.nvgre_hdr.tni_flow = fltr->tenant_id;
190 memcpy(&list[i].m_u.nvgre_hdr.tni_flow,
191 "\xff\xff\xff\xff", 4);
196 list[i].h_u.gtp_hdr.teid = fltr->tenant_id;
197 memcpy(&list[i].m_u.gtp_hdr.teid,
198 "\xff\xff\xff\xff", 4);
206 if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC) {
207 list[i].type = ice_proto_type_from_mac(false);
208 ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
209 hdr->l2_key.dst_mac);
210 ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
211 hdr->l2_mask.dst_mac);
215 if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS &&
216 (fltr->tunnel_type == TNL_GTPU || fltr->tunnel_type == TNL_GTPC)) {
217 list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
219 if (fltr->gtp_pdu_info_masks.pdu_type) {
220 list[i].h_u.gtp_hdr.pdu_type =
221 fltr->gtp_pdu_info_keys.pdu_type << 4;
222 memcpy(&list[i].m_u.gtp_hdr.pdu_type, "\xf0", 1);
225 if (fltr->gtp_pdu_info_masks.qfi) {
226 list[i].h_u.gtp_hdr.qfi = fltr->gtp_pdu_info_keys.qfi;
227 memcpy(&list[i].m_u.gtp_hdr.qfi, "\x3f", 1);
233 if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
234 ICE_TC_FLWR_FIELD_ENC_DEST_IPV4)) {
235 list[i].type = ice_proto_type_from_ipv4(false);
237 if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV4) {
238 list[i].h_u.ipv4_hdr.src_addr = hdr->l3_key.src_ipv4;
239 list[i].m_u.ipv4_hdr.src_addr = hdr->l3_mask.src_ipv4;
241 if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV4) {
242 list[i].h_u.ipv4_hdr.dst_addr = hdr->l3_key.dst_ipv4;
243 list[i].m_u.ipv4_hdr.dst_addr = hdr->l3_mask.dst_ipv4;
248 if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
249 ICE_TC_FLWR_FIELD_ENC_DEST_IPV6)) {
250 list[i].type = ice_proto_type_from_ipv6(false);
252 if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV6) {
253 memcpy(&list[i].h_u.ipv6_hdr.src_addr,
254 &hdr->l3_key.src_ipv6_addr,
255 sizeof(hdr->l3_key.src_ipv6_addr));
256 memcpy(&list[i].m_u.ipv6_hdr.src_addr,
257 &hdr->l3_mask.src_ipv6_addr,
258 sizeof(hdr->l3_mask.src_ipv6_addr));
260 if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV6) {
261 memcpy(&list[i].h_u.ipv6_hdr.dst_addr,
262 &hdr->l3_key.dst_ipv6_addr,
263 sizeof(hdr->l3_key.dst_ipv6_addr));
264 memcpy(&list[i].m_u.ipv6_hdr.dst_addr,
265 &hdr->l3_mask.dst_ipv6_addr,
266 sizeof(hdr->l3_mask.dst_ipv6_addr));
271 if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IP) &&
272 (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
273 ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
274 list[i].type = ice_proto_type_from_ipv4(false);
276 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
277 list[i].h_u.ipv4_hdr.tos = hdr->l3_key.tos;
278 list[i].m_u.ipv4_hdr.tos = hdr->l3_mask.tos;
281 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
282 list[i].h_u.ipv4_hdr.time_to_live = hdr->l3_key.ttl;
283 list[i].m_u.ipv4_hdr.time_to_live = hdr->l3_mask.ttl;
289 if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IPV6) &&
290 (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
291 ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
292 struct ice_ipv6_hdr *hdr_h, *hdr_m;
294 hdr_h = &list[i].h_u.ipv6_hdr;
295 hdr_m = &list[i].m_u.ipv6_hdr;
296 list[i].type = ice_proto_type_from_ipv6(false);
298 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
299 be32p_replace_bits(&hdr_h->be_ver_tc_flow,
301 ICE_IPV6_HDR_TC_MASK);
302 be32p_replace_bits(&hdr_m->be_ver_tc_flow,
304 ICE_IPV6_HDR_TC_MASK);
307 if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
308 hdr_h->hop_limit = hdr->l3_key.ttl;
309 hdr_m->hop_limit = hdr->l3_mask.ttl;
315 if ((flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT) &&
316 hdr->l3_key.ip_proto == IPPROTO_UDP) {
317 list[i].type = ICE_UDP_OF;
318 list[i].h_u.l4_hdr.dst_port = hdr->l4_key.dst_port;
319 list[i].m_u.l4_hdr.dst_port = hdr->l4_mask.dst_port;
327 * ice_tc_fill_rules - fill filter rules based on TC fltr
328 * @hw: pointer to HW structure
329 * @flags: tc flower field flags
330 * @tc_fltr: pointer to TC flower filter
331 * @list: list of advance rule elements
332 * @rule_info: pointer to information about rule
333 * @l4_proto: pointer to information such as L4 proto type
335 * Fill ice_adv_lkup_elem list based on TC flower flags and
336 * TC flower headers. This list should be used to add
337 * advance filter in hardware.
340 ice_tc_fill_rules(struct ice_hw *hw, u32 flags,
341 struct ice_tc_flower_fltr *tc_fltr,
342 struct ice_adv_lkup_elem *list,
343 struct ice_adv_rule_info *rule_info,
346 struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
351 rule_info->vlan_type = vlan_tpid;
353 rule_info->tun_type = ice_sw_type_from_tunnel(tc_fltr->tunnel_type);
354 if (tc_fltr->tunnel_type != TNL_LAST) {
355 i = ice_tc_fill_tunnel_outer(flags, tc_fltr, list);
357 headers = &tc_fltr->inner_headers;
361 if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID) {
362 list[i].type = ice_proto_type_from_etype(inner);
363 list[i].h_u.ethertype.ethtype_id = headers->l2_key.n_proto;
364 list[i].m_u.ethertype.ethtype_id = headers->l2_mask.n_proto;
368 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
369 ICE_TC_FLWR_FIELD_SRC_MAC)) {
370 struct ice_tc_l2_hdr *l2_key, *l2_mask;
372 l2_key = &headers->l2_key;
373 l2_mask = &headers->l2_mask;
375 list[i].type = ice_proto_type_from_mac(inner);
376 if (flags & ICE_TC_FLWR_FIELD_DST_MAC) {
377 ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
379 ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
382 if (flags & ICE_TC_FLWR_FIELD_SRC_MAC) {
383 ether_addr_copy(list[i].h_u.eth_hdr.src_addr,
385 ether_addr_copy(list[i].m_u.eth_hdr.src_addr,
392 if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO)) {
393 vlan_tpid = be16_to_cpu(headers->vlan_hdr.vlan_tpid);
394 rule_info->vlan_type =
395 ice_check_supported_vlan_tpid(vlan_tpid);
397 if (flags & ICE_TC_FLWR_FIELD_CVLAN)
398 list[i].type = ICE_VLAN_EX;
400 list[i].type = ICE_VLAN_OFOS;
402 if (flags & ICE_TC_FLWR_FIELD_VLAN) {
403 list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
404 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
407 if (flags & ICE_TC_FLWR_FIELD_VLAN_PRIO) {
408 if (flags & ICE_TC_FLWR_FIELD_VLAN) {
409 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
411 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
412 list[i].h_u.vlan_hdr.vlan = 0;
414 list[i].h_u.vlan_hdr.vlan |=
415 headers->vlan_hdr.vlan_prio;
421 if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO)) {
422 list[i].type = ICE_VLAN_IN;
424 if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
425 list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
426 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
429 if (flags & ICE_TC_FLWR_FIELD_CVLAN_PRIO) {
430 if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
431 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
433 list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
434 list[i].h_u.vlan_hdr.vlan = 0;
436 list[i].h_u.vlan_hdr.vlan |=
437 headers->cvlan_hdr.vlan_prio;
443 if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
444 ICE_TC_FLWR_FIELD_PPP_PROTO)) {
445 struct ice_pppoe_hdr *vals, *masks;
447 vals = &list[i].h_u.pppoe_hdr;
448 masks = &list[i].m_u.pppoe_hdr;
450 list[i].type = ICE_PPPOE;
452 if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
453 vals->session_id = headers->pppoe_hdr.session_id;
454 masks->session_id = cpu_to_be16(0xFFFF);
457 if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
458 vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
459 masks->ppp_prot_id = cpu_to_be16(0xFFFF);
465 /* copy L3 (IPv[4|6]: src, dest) address */
466 if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
467 ICE_TC_FLWR_FIELD_SRC_IPV4)) {
468 struct ice_tc_l3_hdr *l3_key, *l3_mask;
470 list[i].type = ice_proto_type_from_ipv4(inner);
471 l3_key = &headers->l3_key;
472 l3_mask = &headers->l3_mask;
473 if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
474 list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
475 list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
477 if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
478 list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
479 list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
482 } else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
483 ICE_TC_FLWR_FIELD_SRC_IPV6)) {
484 struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
485 struct ice_tc_l3_hdr *l3_key, *l3_mask;
487 list[i].type = ice_proto_type_from_ipv6(inner);
488 ipv6_hdr = &list[i].h_u.ipv6_hdr;
489 ipv6_mask = &list[i].m_u.ipv6_hdr;
490 l3_key = &headers->l3_key;
491 l3_mask = &headers->l3_mask;
493 if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
494 memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
495 sizeof(l3_key->dst_ipv6_addr));
496 memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
497 sizeof(l3_mask->dst_ipv6_addr));
499 if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
500 memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
501 sizeof(l3_key->src_ipv6_addr));
502 memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
503 sizeof(l3_mask->src_ipv6_addr));
508 if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
509 (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
510 list[i].type = ice_proto_type_from_ipv4(inner);
512 if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
513 list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
514 list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
517 if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
518 list[i].h_u.ipv4_hdr.time_to_live =
520 list[i].m_u.ipv4_hdr.time_to_live =
521 headers->l3_mask.ttl;
527 if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
528 (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
529 struct ice_ipv6_hdr *hdr_h, *hdr_m;
531 hdr_h = &list[i].h_u.ipv6_hdr;
532 hdr_m = &list[i].m_u.ipv6_hdr;
533 list[i].type = ice_proto_type_from_ipv6(inner);
535 if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
536 be32p_replace_bits(&hdr_h->be_ver_tc_flow,
538 ICE_IPV6_HDR_TC_MASK);
539 be32p_replace_bits(&hdr_m->be_ver_tc_flow,
540 headers->l3_mask.tos,
541 ICE_IPV6_HDR_TC_MASK);
544 if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
545 hdr_h->hop_limit = headers->l3_key.ttl;
546 hdr_m->hop_limit = headers->l3_mask.ttl;
552 if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
553 list[i].type = ICE_L2TPV3;
555 list[i].h_u.l2tpv3_sess_hdr.session_id =
556 headers->l2tpv3_hdr.session_id;
557 list[i].m_u.l2tpv3_sess_hdr.session_id =
558 cpu_to_be32(0xFFFFFFFF);
563 /* copy L4 (src, dest) port */
564 if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
565 ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
566 struct ice_tc_l4_hdr *l4_key, *l4_mask;
568 list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
569 l4_key = &headers->l4_key;
570 l4_mask = &headers->l4_mask;
572 if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
573 list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
574 list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
576 if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
577 list[i].h_u.l4_hdr.src_port = l4_key->src_port;
578 list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
587 * ice_tc_tun_get_type - get the tunnel type
588 * @tunnel_dev: ptr to tunnel device
590 * This function detects appropriate tunnel_type if specified device is
591 * tunnel device such as VXLAN/Geneve
593 static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
595 if (netif_is_vxlan(tunnel_dev))
597 if (netif_is_geneve(tunnel_dev))
599 if (netif_is_gretap(tunnel_dev) ||
600 netif_is_ip6gretap(tunnel_dev))
603 /* Assume GTP-U by default in case of GTP netdev.
604 * GTP-C may be selected later, based on enc_dst_port.
606 if (netif_is_gtp(tunnel_dev))
611 bool ice_is_tunnel_supported(struct net_device *dev)
613 return ice_tc_tun_get_type(dev) != TNL_LAST;
617 ice_eswitch_tc_parse_action(struct ice_tc_flower_fltr *fltr,
618 struct flow_action_entry *act)
620 struct ice_repr *repr;
623 case FLOW_ACTION_DROP:
624 fltr->action.fltr_act = ICE_DROP_PACKET;
627 case FLOW_ACTION_REDIRECT:
628 fltr->action.fltr_act = ICE_FWD_TO_VSI;
630 if (ice_is_port_repr_netdev(act->dev)) {
631 repr = ice_netdev_to_repr(act->dev);
633 fltr->dest_vsi = repr->src_vsi;
634 fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
635 } else if (netif_is_ice(act->dev) ||
636 ice_is_tunnel_supported(act->dev)) {
637 fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
639 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported netdevice in switchdev mode");
646 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
654 ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
656 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
657 struct ice_adv_rule_info rule_info = { 0 };
658 struct ice_rule_query_data rule_added;
659 struct ice_hw *hw = &vsi->back->hw;
660 struct ice_adv_lkup_elem *list;
661 u32 flags = fltr->flags;
666 if (!flags || (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT)) {
667 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
671 lkups_cnt = ice_tc_count_lkups(flags, headers, fltr);
672 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
676 i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
677 if (i != lkups_cnt) {
682 /* egress traffic is always redirect to uplink */
683 if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
684 fltr->dest_vsi = vsi->back->switchdev.uplink_vsi;
686 rule_info.sw_act.fltr_act = fltr->action.fltr_act;
687 if (fltr->action.fltr_act != ICE_DROP_PACKET)
688 rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
689 /* For now, making priority to be highest, and it also becomes
690 * the priority for recipe which will get created as a result of
691 * new extraction sequence based on input set.
692 * Priority '7' is max val for switch recipe, higher the number
693 * results into order of switch rule evaluation.
695 rule_info.priority = 7;
697 if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
698 rule_info.sw_act.flag |= ICE_FLTR_RX;
699 rule_info.sw_act.src = hw->pf_id;
702 rule_info.sw_act.flag |= ICE_FLTR_TX;
703 rule_info.sw_act.src = vsi->idx;
704 rule_info.rx = false;
705 rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
706 rule_info.flags_info.act_valid = true;
709 /* specify the cookie as filter_rule_id */
710 rule_info.fltr_rule_id = fltr->cookie;
712 ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
713 if (ret == -EEXIST) {
714 NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
718 NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
722 /* store the output params, which are needed later for removing
723 * advanced switch filter
725 fltr->rid = rule_added.rid;
726 fltr->rule_id = rule_added.rule_id;
727 fltr->dest_vsi_handle = rule_added.vsi_handle;
735 * ice_locate_vsi_using_queue - locate VSI using queue (forward to queue action)
736 * @vsi: Pointer to VSI
737 * @tc_fltr: Pointer to tc_flower_filter
739 * Locate the VSI using specified queue. When ADQ is not enabled, always
740 * return input VSI, otherwise locate corresponding VSI based on per channel
743 static struct ice_vsi *
744 ice_locate_vsi_using_queue(struct ice_vsi *vsi,
745 struct ice_tc_flower_fltr *tc_fltr)
747 int num_tc, tc, queue;
749 /* if ADQ is not active, passed VSI is the candidate VSI */
750 if (!ice_is_adq_active(vsi->back))
753 /* Locate the VSI (it could still be main PF VSI or CHNL_VSI depending
756 num_tc = vsi->mqprio_qopt.qopt.num_tc;
757 queue = tc_fltr->action.fwd.q.queue;
759 for (tc = 0; tc < num_tc; tc++) {
760 int qcount = vsi->mqprio_qopt.qopt.count[tc];
761 int offset = vsi->mqprio_qopt.qopt.offset[tc];
763 if (queue >= offset && queue < offset + qcount) {
764 /* for non-ADQ TCs, passed VSI is the candidate VSI */
765 if (tc < ICE_CHNL_START_TC)
768 return vsi->tc_map_vsi[tc];
774 static struct ice_rx_ring *
775 ice_locate_rx_ring_using_queue(struct ice_vsi *vsi,
776 struct ice_tc_flower_fltr *tc_fltr)
778 u16 queue = tc_fltr->action.fwd.q.queue;
780 return queue < vsi->num_rxq ? vsi->rx_rings[queue] : NULL;
784 * ice_tc_forward_action - Determine destination VSI and queue for the action
785 * @vsi: Pointer to VSI
786 * @tc_fltr: Pointer to TC flower filter structure
788 * Validates the tc forward action and determines the destination VSI and queue
789 * for the forward action.
791 static struct ice_vsi *
792 ice_tc_forward_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *tc_fltr)
794 struct ice_rx_ring *ring = NULL;
795 struct ice_vsi *dest_vsi = NULL;
796 struct ice_pf *pf = vsi->back;
800 dev = ice_pf_to_dev(pf);
802 /* Get the destination VSI and/or destination queue and validate them */
803 switch (tc_fltr->action.fltr_act) {
805 tc_class = tc_fltr->action.fwd.tc.tc_class;
806 /* Select the destination VSI */
807 if (tc_class < ICE_CHNL_START_TC) {
808 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
809 "Unable to add filter because of unsupported destination");
810 return ERR_PTR(-EOPNOTSUPP);
812 /* Locate ADQ VSI depending on hw_tc number */
813 dest_vsi = vsi->tc_map_vsi[tc_class];
816 /* Locate the Rx queue */
817 ring = ice_locate_rx_ring_using_queue(vsi, tc_fltr);
820 "Unable to locate Rx queue for action fwd_to_queue: %u\n",
821 tc_fltr->action.fwd.q.queue);
822 return ERR_PTR(-EINVAL);
824 /* Determine destination VSI even though the action is
825 * FWD_TO_QUEUE, because QUEUE is associated with VSI
827 dest_vsi = tc_fltr->dest_vsi;
831 "Unable to add filter because of unsupported action %u (supported actions: fwd to tc, fwd to queue)\n",
832 tc_fltr->action.fltr_act);
833 return ERR_PTR(-EINVAL);
835 /* Must have valid dest_vsi (it could be main VSI or ADQ VSI) */
838 "Unable to add filter because specified destination VSI doesn't exist\n");
839 return ERR_PTR(-EINVAL);
845 * ice_add_tc_flower_adv_fltr - add appropriate filter rules
846 * @vsi: Pointer to VSI
847 * @tc_fltr: Pointer to TC flower filter structure
849 * based on filter parameters using Advance recipes supported
853 ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
854 struct ice_tc_flower_fltr *tc_fltr)
856 struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
857 struct ice_adv_rule_info rule_info = {0};
858 struct ice_rule_query_data rule_added;
859 struct ice_adv_lkup_elem *list;
860 struct ice_pf *pf = vsi->back;
861 struct ice_hw *hw = &pf->hw;
862 u32 flags = tc_fltr->flags;
863 struct ice_vsi *dest_vsi;
870 dev = ice_pf_to_dev(pf);
871 if (ice_is_safe_mode(pf)) {
872 NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
876 if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
877 ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
878 ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
879 ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
880 ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
881 NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
885 /* validate forwarding action VSI and queue */
886 if (ice_is_forward_action(tc_fltr->action.fltr_act)) {
887 dest_vsi = ice_tc_forward_action(vsi, tc_fltr);
888 if (IS_ERR(dest_vsi))
889 return PTR_ERR(dest_vsi);
892 lkups_cnt = ice_tc_count_lkups(flags, headers, tc_fltr);
893 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
897 i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
898 if (i != lkups_cnt) {
903 rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
904 /* specify the cookie as filter_rule_id */
905 rule_info.fltr_rule_id = tc_fltr->cookie;
907 switch (tc_fltr->action.fltr_act) {
909 rule_info.sw_act.vsi_handle = dest_vsi->idx;
910 rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
911 rule_info.sw_act.src = hw->pf_id;
913 dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
914 tc_fltr->action.fwd.tc.tc_class,
915 rule_info.sw_act.vsi_handle, lkups_cnt);
918 /* HW queue number in global space */
919 rule_info.sw_act.fwd_id.q_id = tc_fltr->action.fwd.q.hw_queue;
920 rule_info.sw_act.vsi_handle = dest_vsi->idx;
921 rule_info.priority = ICE_SWITCH_FLTR_PRIO_QUEUE;
922 rule_info.sw_act.src = hw->pf_id;
924 dev_dbg(dev, "add switch rule action to forward to queue:%u (HW queue %u), lkups_cnt:%u\n",
925 tc_fltr->action.fwd.q.queue,
926 tc_fltr->action.fwd.q.hw_queue, lkups_cnt);
928 case ICE_DROP_PACKET:
929 rule_info.sw_act.flag |= ICE_FLTR_RX;
930 rule_info.sw_act.src = hw->pf_id;
932 rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
939 ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
940 if (ret == -EEXIST) {
941 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
942 "Unable to add filter because it already exist");
946 NL_SET_ERR_MSG_MOD(tc_fltr->extack,
947 "Unable to add filter due to error");
951 /* store the output params, which are needed later for removing
952 * advanced switch filter
954 tc_fltr->rid = rule_added.rid;
955 tc_fltr->rule_id = rule_added.rule_id;
956 tc_fltr->dest_vsi_handle = rule_added.vsi_handle;
957 if (tc_fltr->action.fltr_act == ICE_FWD_TO_VSI ||
958 tc_fltr->action.fltr_act == ICE_FWD_TO_Q) {
959 tc_fltr->dest_vsi = dest_vsi;
960 /* keep track of advanced switch filter for
963 dest_vsi->num_chnl_fltr++;
965 /* keeps track of channel filters for PF VSI */
966 if (vsi->type == ICE_VSI_PF &&
967 (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
968 ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
969 pf->num_dmac_chnl_fltrs++;
971 switch (tc_fltr->action.fltr_act) {
973 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",
975 tc_fltr->action.fwd.tc.tc_class, rule_added.rid,
976 rule_added.rule_id, rule_added.vsi_handle);
979 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",
980 lkups_cnt, flags, tc_fltr->action.fwd.q.queue,
981 tc_fltr->action.fwd.q.hw_queue, rule_added.rid,
984 case ICE_DROP_PACKET:
985 dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is drop, rid %u, rule_id %u\n",
986 lkups_cnt, flags, rule_added.rid, rule_added.rule_id);
997 * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
998 * @match: Pointer to flow match structure
999 * @fltr: Pointer to filter structure
1000 * @headers: Pointer to outer header fields
1001 * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
1004 ice_tc_set_pppoe(struct flow_match_pppoe *match,
1005 struct ice_tc_flower_fltr *fltr,
1006 struct ice_tc_flower_lyr_2_4_hdrs *headers)
1008 if (match->mask->session_id) {
1009 fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
1010 headers->pppoe_hdr.session_id = match->key->session_id;
1013 if (match->mask->ppp_proto) {
1014 fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
1015 headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
1018 return be16_to_cpu(match->key->type);
1022 * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
1023 * @match: Pointer to flow match structure
1024 * @fltr: Pointer to filter structure
1025 * @headers: inner or outer header fields
1026 * @is_encap: set true for tunnel IPv4 address
1029 ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
1030 struct ice_tc_flower_fltr *fltr,
1031 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1033 if (match->key->dst) {
1035 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
1037 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
1038 headers->l3_key.dst_ipv4 = match->key->dst;
1039 headers->l3_mask.dst_ipv4 = match->mask->dst;
1041 if (match->key->src) {
1043 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
1045 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
1046 headers->l3_key.src_ipv4 = match->key->src;
1047 headers->l3_mask.src_ipv4 = match->mask->src;
1053 * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
1054 * @match: Pointer to flow match structure
1055 * @fltr: Pointer to filter structure
1056 * @headers: inner or outer header fields
1057 * @is_encap: set true for tunnel IPv6 address
1060 ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
1061 struct ice_tc_flower_fltr *fltr,
1062 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1064 struct ice_tc_l3_hdr *l3_key, *l3_mask;
1066 /* src and dest IPV6 address should not be LOOPBACK
1067 * (0:0:0:0:0:0:0:1), which can be represented as ::1
1069 if (ipv6_addr_loopback(&match->key->dst) ||
1070 ipv6_addr_loopback(&match->key->src)) {
1071 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
1074 /* if src/dest IPv6 address is *,* error */
1075 if (ipv6_addr_any(&match->mask->dst) &&
1076 ipv6_addr_any(&match->mask->src)) {
1077 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
1080 if (!ipv6_addr_any(&match->mask->dst)) {
1082 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
1084 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
1086 if (!ipv6_addr_any(&match->mask->src)) {
1088 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
1090 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
1093 l3_key = &headers->l3_key;
1094 l3_mask = &headers->l3_mask;
1096 if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
1097 ICE_TC_FLWR_FIELD_SRC_IPV6)) {
1098 memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
1099 sizeof(match->key->src.s6_addr));
1100 memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
1101 sizeof(match->mask->src.s6_addr));
1103 if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
1104 ICE_TC_FLWR_FIELD_DEST_IPV6)) {
1105 memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
1106 sizeof(match->key->dst.s6_addr));
1107 memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
1108 sizeof(match->mask->dst.s6_addr));
1115 * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
1116 * @match: Pointer to flow match structure
1117 * @fltr: Pointer to filter structure
1118 * @headers: inner or outer header fields
1119 * @is_encap: set true for tunnel
1122 ice_tc_set_tos_ttl(struct flow_match_ip *match,
1123 struct ice_tc_flower_fltr *fltr,
1124 struct ice_tc_flower_lyr_2_4_hdrs *headers,
1127 if (match->mask->tos) {
1129 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
1131 fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
1133 headers->l3_key.tos = match->key->tos;
1134 headers->l3_mask.tos = match->mask->tos;
1137 if (match->mask->ttl) {
1139 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
1141 fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
1143 headers->l3_key.ttl = match->key->ttl;
1144 headers->l3_mask.ttl = match->mask->ttl;
1149 * ice_tc_set_port - Parse ports from TC flower filter
1150 * @match: Flow match structure
1151 * @fltr: Pointer to filter structure
1152 * @headers: inner or outer header fields
1153 * @is_encap: set true for tunnel port
1156 ice_tc_set_port(struct flow_match_ports match,
1157 struct ice_tc_flower_fltr *fltr,
1158 struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1160 if (match.key->dst) {
1162 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
1164 fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
1166 headers->l4_key.dst_port = match.key->dst;
1167 headers->l4_mask.dst_port = match.mask->dst;
1169 if (match.key->src) {
1171 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
1173 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
1175 headers->l4_key.src_port = match.key->src;
1176 headers->l4_mask.src_port = match.mask->src;
1181 static struct net_device *
1182 ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
1184 struct flow_action_entry *act;
1187 if (ice_is_tunnel_supported(dev))
1190 flow_action_for_each(i, act, &rule->action) {
1191 if (act->id == FLOW_ACTION_REDIRECT &&
1192 ice_is_tunnel_supported(act->dev))
1200 * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
1201 * @match: Flow match structure
1202 * @fltr: Pointer to filter structure
1204 * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
1205 * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
1206 * therefore making GTP-U the default choice (when destination port number is
1210 ice_parse_gtp_type(struct flow_match_ports match,
1211 struct ice_tc_flower_fltr *fltr)
1215 if (match.key->dst) {
1216 dst_port = be16_to_cpu(match.key->dst);
1222 fltr->tunnel_type = TNL_GTPC;
1225 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
1234 ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
1235 struct ice_tc_flower_fltr *fltr)
1237 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1238 struct flow_match_control enc_control;
1240 fltr->tunnel_type = ice_tc_tun_get_type(dev);
1241 headers->l3_key.ip_proto = IPPROTO_UDP;
1243 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1244 struct flow_match_enc_keyid enc_keyid;
1246 flow_rule_match_enc_keyid(rule, &enc_keyid);
1248 if (!enc_keyid.mask->keyid ||
1249 enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
1252 fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
1253 fltr->tenant_id = enc_keyid.key->keyid;
1256 flow_rule_match_enc_control(rule, &enc_control);
1258 if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1259 struct flow_match_ipv4_addrs match;
1261 flow_rule_match_enc_ipv4_addrs(rule, &match);
1262 if (ice_tc_set_ipv4(&match, fltr, headers, true))
1264 } else if (enc_control.key->addr_type ==
1265 FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1266 struct flow_match_ipv6_addrs match;
1268 flow_rule_match_enc_ipv6_addrs(rule, &match);
1269 if (ice_tc_set_ipv6(&match, fltr, headers, true))
1273 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
1274 struct flow_match_ip match;
1276 flow_rule_match_enc_ip(rule, &match);
1277 ice_tc_set_tos_ttl(&match, fltr, headers, true);
1280 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
1281 fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
1282 struct flow_match_ports match;
1284 flow_rule_match_enc_ports(rule, &match);
1286 if (fltr->tunnel_type != TNL_GTPU) {
1287 if (ice_tc_set_port(match, fltr, headers, true))
1290 if (ice_parse_gtp_type(match, fltr))
1295 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
1296 struct flow_match_enc_opts match;
1298 flow_rule_match_enc_opts(rule, &match);
1300 memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
1301 sizeof(struct gtp_pdu_session_info));
1303 memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
1304 sizeof(struct gtp_pdu_session_info));
1306 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_OPTS;
1313 * ice_parse_cls_flower - Parse TC flower filters provided by kernel
1314 * @vsi: Pointer to the VSI
1315 * @filter_dev: Pointer to device on which filter is being added
1316 * @f: Pointer to struct flow_cls_offload
1317 * @fltr: Pointer to filter structure
1320 ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
1321 struct flow_cls_offload *f,
1322 struct ice_tc_flower_fltr *fltr)
1324 struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1325 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1326 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
1327 struct flow_dissector *dissector;
1328 struct net_device *tunnel_dev;
1330 dissector = rule->match.dissector;
1332 if (dissector->used_keys &
1333 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1334 BIT(FLOW_DISSECTOR_KEY_BASIC) |
1335 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1336 BIT(FLOW_DISSECTOR_KEY_VLAN) |
1337 BIT(FLOW_DISSECTOR_KEY_CVLAN) |
1338 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1339 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1340 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1341 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1342 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1343 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1344 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1345 BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1346 BIT(FLOW_DISSECTOR_KEY_IP) |
1347 BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
1348 BIT(FLOW_DISSECTOR_KEY_PORTS) |
1349 BIT(FLOW_DISSECTOR_KEY_PPPOE) |
1350 BIT(FLOW_DISSECTOR_KEY_L2TPV3))) {
1351 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
1355 tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
1359 filter_dev = tunnel_dev;
1361 err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
1363 NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
1367 /* header pointers should point to the inner headers, outer
1368 * header were already set by ice_parse_tunnel_attr
1370 headers = &fltr->inner_headers;
1371 } else if (dissector->used_keys &
1372 (BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1373 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1374 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1375 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))) {
1376 NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
1379 fltr->tunnel_type = TNL_LAST;
1382 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1383 struct flow_match_basic match;
1385 flow_rule_match_basic(rule, &match);
1387 n_proto_key = ntohs(match.key->n_proto);
1388 n_proto_mask = ntohs(match.mask->n_proto);
1390 if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
1391 fltr->tunnel_type == TNL_GTPU ||
1392 fltr->tunnel_type == TNL_GTPC) {
1396 fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1399 headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1400 headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
1401 headers->l3_key.ip_proto = match.key->ip_proto;
1404 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1405 struct flow_match_eth_addrs match;
1407 flow_rule_match_eth_addrs(rule, &match);
1409 if (!is_zero_ether_addr(match.key->dst)) {
1410 ether_addr_copy(headers->l2_key.dst_mac,
1412 ether_addr_copy(headers->l2_mask.dst_mac,
1414 fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1417 if (!is_zero_ether_addr(match.key->src)) {
1418 ether_addr_copy(headers->l2_key.src_mac,
1420 ether_addr_copy(headers->l2_mask.src_mac,
1422 fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
1426 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
1427 is_vlan_dev(filter_dev)) {
1428 struct flow_dissector_key_vlan mask;
1429 struct flow_dissector_key_vlan key;
1430 struct flow_match_vlan match;
1432 if (is_vlan_dev(filter_dev)) {
1434 match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
1435 match.key->vlan_priority = 0;
1437 memset(match.mask, 0xff, sizeof(*match.mask));
1438 match.mask->vlan_priority = 0;
1440 flow_rule_match_vlan(rule, &match);
1443 if (match.mask->vlan_id) {
1444 if (match.mask->vlan_id == VLAN_VID_MASK) {
1445 fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
1446 headers->vlan_hdr.vlan_id =
1447 cpu_to_be16(match.key->vlan_id &
1450 NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
1455 if (match.mask->vlan_priority) {
1456 fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
1457 headers->vlan_hdr.vlan_prio =
1458 be16_encode_bits(match.key->vlan_priority,
1462 if (match.mask->vlan_tpid)
1463 headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
1466 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
1467 struct flow_match_vlan match;
1469 if (!ice_is_dvm_ena(&vsi->back->hw)) {
1470 NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
1474 flow_rule_match_cvlan(rule, &match);
1476 if (match.mask->vlan_id) {
1477 if (match.mask->vlan_id == VLAN_VID_MASK) {
1478 fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
1479 headers->cvlan_hdr.vlan_id =
1480 cpu_to_be16(match.key->vlan_id &
1483 NL_SET_ERR_MSG_MOD(fltr->extack,
1489 if (match.mask->vlan_priority) {
1490 fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
1491 headers->cvlan_hdr.vlan_prio =
1492 be16_encode_bits(match.key->vlan_priority,
1497 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
1498 struct flow_match_pppoe match;
1500 flow_rule_match_pppoe(rule, &match);
1501 n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
1503 /* If ethertype equals ETH_P_PPP_SES, n_proto might be
1504 * overwritten by encapsulated protocol (ppp_proto field) or set
1505 * to 0. To correct this, flow_match_pppoe provides the type
1506 * field, which contains the actual ethertype (ETH_P_PPP_SES).
1508 headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1509 headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
1510 fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1513 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1514 struct flow_match_control match;
1516 flow_rule_match_control(rule, &match);
1518 addr_type = match.key->addr_type;
1521 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1522 struct flow_match_ipv4_addrs match;
1524 flow_rule_match_ipv4_addrs(rule, &match);
1525 if (ice_tc_set_ipv4(&match, fltr, headers, false))
1529 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1530 struct flow_match_ipv6_addrs match;
1532 flow_rule_match_ipv6_addrs(rule, &match);
1533 if (ice_tc_set_ipv6(&match, fltr, headers, false))
1537 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
1538 struct flow_match_ip match;
1540 flow_rule_match_ip(rule, &match);
1541 ice_tc_set_tos_ttl(&match, fltr, headers, false);
1544 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
1545 struct flow_match_l2tpv3 match;
1547 flow_rule_match_l2tpv3(rule, &match);
1549 fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
1550 headers->l2tpv3_hdr.session_id = match.key->session_id;
1553 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
1554 struct flow_match_ports match;
1556 flow_rule_match_ports(rule, &match);
1557 if (ice_tc_set_port(match, fltr, headers, false))
1559 switch (headers->l3_key.ip_proto) {
1564 NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
1572 * ice_add_switch_fltr - Add TC flower filters
1573 * @vsi: Pointer to VSI
1574 * @fltr: Pointer to struct ice_tc_flower_fltr
1576 * Add filter in HW switch block
1579 ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1581 if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
1584 if (ice_is_eswitch_mode_switchdev(vsi->back))
1585 return ice_eswitch_add_tc_fltr(vsi, fltr);
1587 return ice_add_tc_flower_adv_fltr(vsi, fltr);
1591 * ice_prep_adq_filter - Prepare ADQ filter with the required additional headers
1592 * @vsi: Pointer to VSI
1593 * @fltr: Pointer to TC flower filter structure
1595 * Prepare ADQ filter with the required additional header fields
1598 ice_prep_adq_filter(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1600 if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
1601 (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1602 ICE_TC_FLWR_FIELD_SRC_MAC))) {
1603 NL_SET_ERR_MSG_MOD(fltr->extack,
1604 "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
1608 /* For ADQ, filter must include dest MAC address, otherwise unwanted
1609 * packets with unrelated MAC address get delivered to ADQ VSIs as long
1610 * as remaining filter criteria is satisfied such as dest IP address
1611 * and dest/src L4 port. Below code handles the following cases:
1612 * 1. For non-tunnel, if user specify MAC addresses, use them.
1613 * 2. For non-tunnel, if user didn't specify MAC address, add implicit
1614 * dest MAC to be lower netdev's active unicast MAC address
1615 * 3. For tunnel, as of now TC-filter through flower classifier doesn't
1616 * have provision for user to specify outer DMAC, hence driver to
1617 * implicitly add outer dest MAC to be lower netdev's active unicast
1620 if (fltr->tunnel_type != TNL_LAST &&
1621 !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
1622 fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
1624 if (fltr->tunnel_type == TNL_LAST &&
1625 !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
1626 fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1628 if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1629 ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
1630 ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
1631 vsi->netdev->dev_addr);
1632 eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
1635 /* Make sure VLAN is already added to main VSI, before allowing ADQ to
1636 * add a VLAN based filter such as MAC + VLAN + L4 port.
1638 if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
1639 u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
1641 if (!ice_vlan_fltr_exist(&vsi->back->hw, vlan_id, vsi->idx)) {
1642 NL_SET_ERR_MSG_MOD(fltr->extack,
1643 "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
1651 * ice_handle_tclass_action - Support directing to a traffic class
1652 * @vsi: Pointer to VSI
1653 * @cls_flower: Pointer to TC flower offload structure
1654 * @fltr: Pointer to TC flower filter structure
1656 * Support directing traffic to a traffic class/queue-set
1659 ice_handle_tclass_action(struct ice_vsi *vsi,
1660 struct flow_cls_offload *cls_flower,
1661 struct ice_tc_flower_fltr *fltr)
1663 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
1665 /* user specified hw_tc (must be non-zero for ADQ TC), action is forward
1666 * to hw_tc (i.e. ADQ channel number)
1668 if (tc < ICE_CHNL_START_TC) {
1669 NL_SET_ERR_MSG_MOD(fltr->extack,
1670 "Unable to add filter because of unsupported destination");
1673 if (!(vsi->all_enatc & BIT(tc))) {
1674 NL_SET_ERR_MSG_MOD(fltr->extack,
1675 "Unable to add filter because of non-existence destination");
1678 fltr->action.fltr_act = ICE_FWD_TO_VSI;
1679 fltr->action.fwd.tc.tc_class = tc;
1681 return ice_prep_adq_filter(vsi, fltr);
1685 ice_tc_forward_to_queue(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1686 struct flow_action_entry *act)
1688 struct ice_vsi *ch_vsi = NULL;
1689 u16 queue = act->rx_queue;
1691 if (queue >= vsi->num_rxq) {
1692 NL_SET_ERR_MSG_MOD(fltr->extack,
1693 "Unable to add filter because specified queue is invalid");
1696 fltr->action.fltr_act = ICE_FWD_TO_Q;
1697 fltr->action.fwd.q.queue = queue;
1698 /* determine corresponding HW queue */
1699 fltr->action.fwd.q.hw_queue = vsi->rxq_map[queue];
1701 /* If ADQ is configured, and the queue belongs to ADQ VSI, then prepare
1704 ch_vsi = ice_locate_vsi_using_queue(vsi, fltr);
1707 fltr->dest_vsi = ch_vsi;
1708 if (!ice_is_chnl_fltr(fltr))
1711 return ice_prep_adq_filter(vsi, fltr);
1715 ice_tc_parse_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1716 struct flow_action_entry *act)
1719 case FLOW_ACTION_RX_QUEUE_MAPPING:
1720 /* forward to queue */
1721 return ice_tc_forward_to_queue(vsi, fltr, act);
1722 case FLOW_ACTION_DROP:
1723 fltr->action.fltr_act = ICE_DROP_PACKET;
1726 NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported TC action");
1732 * ice_parse_tc_flower_actions - Parse the actions for a TC filter
1733 * @vsi: Pointer to VSI
1734 * @cls_flower: Pointer to TC flower offload structure
1735 * @fltr: Pointer to TC flower filter structure
1737 * Parse the actions for a TC filter
1740 ice_parse_tc_flower_actions(struct ice_vsi *vsi,
1741 struct flow_cls_offload *cls_flower,
1742 struct ice_tc_flower_fltr *fltr)
1744 struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1745 struct flow_action *flow_action = &rule->action;
1746 struct flow_action_entry *act;
1749 if (cls_flower->classid)
1750 return ice_handle_tclass_action(vsi, cls_flower, fltr);
1752 if (!flow_action_has_entries(flow_action))
1755 flow_action_for_each(i, act, flow_action) {
1756 if (ice_is_eswitch_mode_switchdev(vsi->back))
1757 err = ice_eswitch_tc_parse_action(fltr, act);
1759 err = ice_tc_parse_action(vsi, fltr, act);
1768 * ice_del_tc_fltr - deletes a filter from HW table
1769 * @vsi: Pointer to VSI
1770 * @fltr: Pointer to struct ice_tc_flower_fltr
1772 * This function deletes a filter from HW table and manages book-keeping
1774 static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1776 struct ice_rule_query_data rule_rem;
1777 struct ice_pf *pf = vsi->back;
1780 rule_rem.rid = fltr->rid;
1781 rule_rem.rule_id = fltr->rule_id;
1782 rule_rem.vsi_handle = fltr->dest_vsi_handle;
1783 err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
1785 if (err == -ENOENT) {
1786 NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
1789 NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
1793 /* update advanced switch filter count for destination
1794 * VSI if filter destination was VSI
1796 if (fltr->dest_vsi) {
1797 if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
1798 fltr->dest_vsi->num_chnl_fltr--;
1800 /* keeps track of channel filters for PF VSI */
1801 if (vsi->type == ICE_VSI_PF &&
1802 (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1803 ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
1804 pf->num_dmac_chnl_fltrs--;
1811 * ice_add_tc_fltr - adds a TC flower filter
1812 * @netdev: Pointer to netdev
1813 * @vsi: Pointer to VSI
1814 * @f: Pointer to flower offload structure
1815 * @__fltr: Pointer to struct ice_tc_flower_fltr
1817 * This function parses TC-flower input fields, parses action,
1818 * and adds a filter.
1821 ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
1822 struct flow_cls_offload *f,
1823 struct ice_tc_flower_fltr **__fltr)
1825 struct ice_tc_flower_fltr *fltr;
1828 /* by default, set output to be INVALID */
1831 fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1835 fltr->cookie = f->cookie;
1836 fltr->extack = f->common.extack;
1837 fltr->src_vsi = vsi;
1838 INIT_HLIST_NODE(&fltr->tc_flower_node);
1840 err = ice_parse_cls_flower(netdev, vsi, f, fltr);
1844 err = ice_parse_tc_flower_actions(vsi, f, fltr);
1848 err = ice_add_switch_fltr(vsi, fltr);
1852 /* return the newly created filter */
1862 * ice_find_tc_flower_fltr - Find the TC flower filter in the list
1863 * @pf: Pointer to PF
1864 * @cookie: filter specific cookie
1866 static struct ice_tc_flower_fltr *
1867 ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
1869 struct ice_tc_flower_fltr *fltr;
1871 hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
1872 if (cookie == fltr->cookie)
1879 * ice_add_cls_flower - add TC flower filters
1880 * @netdev: Pointer to filter device
1881 * @vsi: Pointer to VSI
1882 * @cls_flower: Pointer to flower offload structure
1885 ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
1886 struct flow_cls_offload *cls_flower)
1888 struct netlink_ext_ack *extack = cls_flower->common.extack;
1889 struct net_device *vsi_netdev = vsi->netdev;
1890 struct ice_tc_flower_fltr *fltr;
1891 struct ice_pf *pf = vsi->back;
1894 if (ice_is_reset_in_progress(pf->state))
1896 if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
1899 if (ice_is_port_repr_netdev(netdev))
1900 vsi_netdev = netdev;
1902 if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
1903 !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
1904 /* Based on TC indirect notifications from kernel, all ice
1905 * devices get an instance of rule from higher level device.
1906 * Avoid triggering explicit error in this case.
1908 if (netdev == vsi_netdev)
1909 NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
1913 /* avoid duplicate entries, if exists - return error */
1914 fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1916 NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
1920 /* prep and add TC-flower filter in HW */
1921 err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr);
1925 /* add filter into an ordered list */
1926 hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
1931 * ice_del_cls_flower - delete TC flower filters
1932 * @vsi: Pointer to VSI
1933 * @cls_flower: Pointer to struct flow_cls_offload
1936 ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
1938 struct ice_tc_flower_fltr *fltr;
1939 struct ice_pf *pf = vsi->back;
1943 fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1945 if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
1946 hlist_empty(&pf->tc_flower_fltr_list))
1949 NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
1953 fltr->extack = cls_flower->common.extack;
1954 /* delete filter from HW */
1955 err = ice_del_tc_fltr(vsi, fltr);
1959 /* delete filter from an ordered list */
1960 hlist_del(&fltr->tc_flower_node);
1962 /* free the filter node */
1969 * ice_replay_tc_fltrs - replay TC filters
1970 * @pf: pointer to PF struct
1972 void ice_replay_tc_fltrs(struct ice_pf *pf)
1974 struct ice_tc_flower_fltr *fltr;
1975 struct hlist_node *node;
1977 hlist_for_each_entry_safe(fltr, node,
1978 &pf->tc_flower_fltr_list,
1980 fltr->extack = NULL;
1981 ice_add_switch_fltr(fltr->src_vsi, fltr);