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