net/mlx5: fix packet length assert in MPRQ
[dpdk.git] / drivers / net / iavf / iavf_hash.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdarg.h>
12
13 #include <rte_debug.h>
14 #include <rte_ether.h>
15 #include <rte_ethdev_driver.h>
16 #include <rte_log.h>
17 #include <rte_malloc.h>
18 #include <rte_eth_ctrl.h>
19 #include <rte_tailq.h>
20 #include <rte_flow_driver.h>
21
22 #include "iavf_log.h"
23 #include "iavf.h"
24 #include "iavf_generic_flow.h"
25
26 enum iavf_pattern_hint_type {
27         IAVF_PATTERN_HINT_NONE,
28         IAVF_PATTERN_HINT_IPV4,
29         IAVF_PATTERN_HINT_IPV4_UDP,
30         IAVF_PATTERN_HINT_IPV4_TCP,
31         IAVF_PATTERN_HINT_IPV4_SCTP,
32         IAVF_PATTERN_HINT_IPV6,
33         IAVF_PATTERN_HINT_IPV6_UDP,
34         IAVF_PATTERN_HINT_IPV6_TCP,
35         IAVF_PATTERN_HINT_IPV6_SCTP,
36 };
37
38 struct iavf_pattern_match_type {
39         enum iavf_pattern_hint_type phint_type;
40 };
41
42 struct iavf_hash_match_type {
43         enum iavf_pattern_hint_type phint_type;
44         uint64_t hash_type;
45         struct virtchnl_proto_hdrs *proto_hdrs;
46 };
47
48 struct iavf_rss_meta {
49         struct virtchnl_proto_hdrs *proto_hdrs;
50         uint32_t hash_function;
51 };
52
53 struct iavf_hash_flow_cfg {
54         struct virtchnl_rss_cfg *rss_cfg;
55         bool simple_xor;
56 };
57
58 static int
59 iavf_hash_init(struct iavf_adapter *ad);
60 static int
61 iavf_hash_create(struct iavf_adapter *ad, struct rte_flow *flow, void *meta,
62                  struct rte_flow_error *error);
63 static int
64 iavf_hash_destroy(struct iavf_adapter *ad, struct rte_flow *flow,
65                   struct rte_flow_error *error);
66 static void
67 iavf_hash_uninit(struct iavf_adapter *ad);
68 static void
69 iavf_hash_free(struct rte_flow *flow);
70 static int
71 iavf_hash_parse_pattern_action(struct iavf_adapter *ad,
72                                struct iavf_pattern_match_item *array,
73                                uint32_t array_len,
74                                const struct rte_flow_item pattern[],
75                                const struct rte_flow_action actions[],
76                                void **meta,
77                                struct rte_flow_error *error);
78
79 struct iavf_pattern_match_type phint_empty = {
80         IAVF_PATTERN_HINT_NONE};
81 struct iavf_pattern_match_type phint_eth_ipv4 = {
82         IAVF_PATTERN_HINT_IPV4};
83 struct iavf_pattern_match_type phint_eth_ipv4_udp = {
84         IAVF_PATTERN_HINT_IPV4_UDP};
85 struct iavf_pattern_match_type phint_eth_ipv4_tcp = {
86         IAVF_PATTERN_HINT_IPV4_TCP};
87 struct iavf_pattern_match_type phint_eth_ipv4_sctp = {
88         IAVF_PATTERN_HINT_IPV4_SCTP};
89 struct iavf_pattern_match_type phint_eth_ipv4_gtpu_eh = {
90         IAVF_PATTERN_HINT_IPV4_UDP};
91 struct iavf_pattern_match_type phint_eth_ipv4_esp = {
92         IAVF_PATTERN_HINT_IPV4};
93 struct iavf_pattern_match_type phint_eth_ipv4_ah = {
94         IAVF_PATTERN_HINT_IPV4};
95 struct iavf_pattern_match_type phint_eth_ipv4_l2tpv3 = {
96         IAVF_PATTERN_HINT_IPV4};
97 struct iavf_pattern_match_type phint_eth_ipv4_pfcp = {
98         IAVF_PATTERN_HINT_IPV4_UDP};
99 struct iavf_pattern_match_type phint_eth_ipv6 = {
100         IAVF_PATTERN_HINT_IPV6};
101 struct iavf_pattern_match_type phint_eth_ipv6_udp = {
102         IAVF_PATTERN_HINT_IPV6_UDP};
103 struct iavf_pattern_match_type phint_eth_ipv6_tcp = {
104         IAVF_PATTERN_HINT_IPV6_TCP};
105 struct iavf_pattern_match_type phint_eth_ipv6_sctp = {
106         IAVF_PATTERN_HINT_IPV6_SCTP};
107 struct iavf_pattern_match_type phint_eth_ipv6_esp = {
108         IAVF_PATTERN_HINT_IPV6};
109 struct iavf_pattern_match_type phint_eth_ipv6_ah = {
110         IAVF_PATTERN_HINT_IPV6};
111 struct iavf_pattern_match_type phint_eth_ipv6_l2tpv3 = {
112         IAVF_PATTERN_HINT_IPV6};
113 struct iavf_pattern_match_type phint_eth_ipv6_pfcp = {
114         IAVF_PATTERN_HINT_IPV6_UDP};
115
116 /**
117  * Supported pattern for hash.
118  * The first member is pattern item type,
119  * the second member is input set mask,
120  * the third member is pattern hint for hash.
121  */
122 static struct iavf_pattern_match_item iavf_hash_pattern_list[] = {
123         {iavf_pattern_eth_ipv4, IAVF_INSET_NONE, &phint_eth_ipv4},
124         {iavf_pattern_eth_ipv4_udp, IAVF_INSET_NONE, &phint_eth_ipv4_udp},
125         {iavf_pattern_eth_ipv4_tcp, IAVF_INSET_NONE, &phint_eth_ipv4_tcp},
126         {iavf_pattern_eth_ipv4_sctp, IAVF_INSET_NONE, &phint_eth_ipv4_sctp},
127         {iavf_pattern_eth_ipv6, IAVF_INSET_NONE, &phint_eth_ipv6},
128         {iavf_pattern_eth_ipv4_gtpu_eh_ipv4, IAVF_INSET_NONE,
129                                                 &phint_eth_ipv4_gtpu_eh},
130         {iavf_pattern_eth_ipv4_gtpu_eh_ipv4_udp, IAVF_INSET_NONE,
131                                                 &phint_eth_ipv4_gtpu_eh},
132         {iavf_pattern_eth_ipv4_gtpu_eh_ipv4_tcp, IAVF_INSET_NONE,
133                                                 &phint_eth_ipv4_gtpu_eh},
134         {iavf_pattern_eth_ipv4_esp, IAVF_INSET_NONE, &phint_eth_ipv4_esp},
135         {iavf_pattern_eth_ipv4_ah, IAVF_INSET_NONE, &phint_eth_ipv4_ah},
136         {iavf_pattern_eth_ipv4_l2tpv3, IAVF_INSET_NONE,
137                                                 &phint_eth_ipv4_l2tpv3},
138         {iavf_pattern_eth_ipv4_pfcp, IAVF_INSET_NONE, &phint_eth_ipv4_pfcp},
139         {iavf_pattern_eth_ipv6_udp, IAVF_INSET_NONE, &phint_eth_ipv6_udp},
140         {iavf_pattern_eth_ipv6_tcp, IAVF_INSET_NONE, &phint_eth_ipv6_tcp},
141         {iavf_pattern_eth_ipv6_sctp, IAVF_INSET_NONE, &phint_eth_ipv6_sctp},
142         {iavf_pattern_eth_ipv6_esp, IAVF_INSET_NONE, &phint_eth_ipv6_esp},
143         {iavf_pattern_eth_ipv6_ah, IAVF_INSET_NONE, &phint_eth_ipv6_ah},
144         {iavf_pattern_eth_ipv6_l2tpv3, IAVF_INSET_NONE,
145                                                 &phint_eth_ipv6_l2tpv3},
146         {iavf_pattern_eth_ipv6_pfcp, IAVF_INSET_NONE, &phint_eth_ipv6_pfcp},
147         {iavf_pattern_empty, IAVF_INSET_NONE, &phint_empty},
148 };
149
150 #define GTP_EH_PDU_LINK_UP              1
151 #define GTP_EH_PDU_LINK_DWN             0
152
153 #define TUNNEL_LEVEL_OUTER              0
154 #define TUNNEL_LEVEL_FIRST_INNER        1
155
156 #define PROTO_COUNT_ONE                 1
157 #define PROTO_COUNT_TWO                 2
158 #define PROTO_COUNT_THREE               3
159
160 #define BUFF_NOUSED                     0
161 #define FIELD_FOR_PROTO_ONLY            0
162
163 #define proto_hint_eth_src { \
164         VIRTCHNL_PROTO_HDR_ETH, VIRTCHNL_PROTO_HDR_ETH_SRC, {BUFF_NOUSED } }
165
166 #define proto_hint_eth_dst { \
167         VIRTCHNL_PROTO_HDR_ETH, VIRTCHNL_PROTO_HDR_ETH_DST, {BUFF_NOUSED } }
168
169 #define proto_hint_eth_only { \
170         VIRTCHNL_PROTO_HDR_ETH, FIELD_FOR_PROTO_ONLY, {BUFF_NOUSED } }
171
172 #define proto_hint_eth { \
173         VIRTCHNL_PROTO_HDR_ETH, \
174         VIRTCHNL_PROTO_HDR_ETH_SRC | VIRTCHNL_PROTO_HDR_ETH_DST, \
175         {BUFF_NOUSED } }
176
177 #define proto_hint_svlan { \
178         VIRTCHNL_PROTO_HDR_S_VLAN, VIRTCHNL_PROTO_HDR_S_VLAN_ID, \
179         {BUFF_NOUSED } }
180
181 #define proto_hint_cvlan { \
182         VIRTCHNL_PROTO_HDR_C_VLAN, VIRTCHNL_PROTO_HDR_C_VLAN_ID, \
183         {BUFF_NOUSED } }
184
185 #define proto_hint_ipv4_src { \
186         VIRTCHNL_PROTO_HDR_IPV4, VIRTCHNL_PROTO_HDR_IPV4_SRC, {BUFF_NOUSED } }
187
188 #define proto_hint_ipv4_dst { \
189         VIRTCHNL_PROTO_HDR_IPV4, VIRTCHNL_PROTO_HDR_IPV4_DST, {BUFF_NOUSED } }
190
191 #define proto_hint_ipv4_only { \
192         VIRTCHNL_PROTO_HDR_IPV4, FIELD_FOR_PROTO_ONLY, {BUFF_NOUSED } }
193
194 #define proto_hint_ipv4 { \
195         VIRTCHNL_PROTO_HDR_IPV4, \
196         VIRTCHNL_PROTO_HDR_IPV4_SRC | VIRTCHNL_PROTO_HDR_IPV4_DST, \
197         {BUFF_NOUSED } }
198
199 #define proto_hint_udp_src_port { \
200         VIRTCHNL_PROTO_HDR_UDP, VIRTCHNL_PROTO_HDR_UDP_SRC_PORT, \
201         {BUFF_NOUSED } }
202
203 #define proto_hint_udp_dst_port { \
204         VIRTCHNL_PROTO_HDR_UDP, VIRTCHNL_PROTO_HDR_UDP_DST_PORT, \
205         {BUFF_NOUSED } }
206
207 #define proto_hint_udp_only { \
208         VIRTCHNL_PROTO_HDR_UDP, FIELD_FOR_PROTO_ONLY, {BUFF_NOUSED } }
209
210 #define proto_hint_udp { \
211         VIRTCHNL_PROTO_HDR_UDP, \
212         VIRTCHNL_PROTO_HDR_UDP_SRC_PORT | VIRTCHNL_PROTO_HDR_UDP_DST_PORT, \
213         {BUFF_NOUSED } }
214
215 #define proto_hint_tcp_src_port { \
216         VIRTCHNL_PROTO_HDR_TCP, VIRTCHNL_PROTO_HDR_TCP_SRC_PORT, \
217         {BUFF_NOUSED } }
218
219 #define proto_hint_tcp_dst_port { \
220         VIRTCHNL_PROTO_HDR_TCP, VIRTCHNL_PROTO_HDR_TCP_DST_PORT, \
221         {BUFF_NOUSED } }
222
223 #define proto_hint_tcp_only { \
224         VIRTCHNL_PROTO_HDR_TCP, FIELD_FOR_PROTO_ONLY, {BUFF_NOUSED } }
225
226 #define proto_hint_tcp { \
227         VIRTCHNL_PROTO_HDR_TCP, \
228         VIRTCHNL_PROTO_HDR_TCP_SRC_PORT | VIRTCHNL_PROTO_HDR_TCP_DST_PORT, \
229         {BUFF_NOUSED } }
230
231 #define proto_hint_sctp_src_port { \
232         VIRTCHNL_PROTO_HDR_SCTP, VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT, \
233         {BUFF_NOUSED } }
234
235 #define proto_hint_sctp_dst_port { \
236         VIRTCHNL_PROTO_HDR_SCTP, VIRTCHNL_PROTO_HDR_SCTP_DST_PORT, \
237         {BUFF_NOUSED } }
238
239 #define proto_hint_sctp_only { \
240         VIRTCHNL_PROTO_HDR_SCTP, FIELD_FOR_PROTO_ONLY, {BUFF_NOUSED } }
241
242 #define proto_hint_sctp { \
243         VIRTCHNL_PROTO_HDR_SCTP, \
244         VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT | VIRTCHNL_PROTO_HDR_SCTP_DST_PORT, \
245         {BUFF_NOUSED } }
246
247 #define proto_hint_ipv6_src { \
248         VIRTCHNL_PROTO_HDR_IPV6, VIRTCHNL_PROTO_HDR_IPV6_SRC, {BUFF_NOUSED } }
249
250 #define proto_hint_ipv6_dst { \
251         VIRTCHNL_PROTO_HDR_IPV6, VIRTCHNL_PROTO_HDR_IPV6_DST, {BUFF_NOUSED } }
252
253 #define proto_hint_ipv6_only { \
254         VIRTCHNL_PROTO_HDR_IPV6, FIELD_FOR_PROTO_ONLY, {BUFF_NOUSED } }
255
256 #define proto_hint_ipv6 { \
257         VIRTCHNL_PROTO_HDR_IPV6, \
258         VIRTCHNL_PROTO_HDR_IPV6_SRC | VIRTCHNL_PROTO_HDR_IPV6_DST, \
259         {BUFF_NOUSED } }
260
261 #define proto_hint_gtpu_up_only { \
262         VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP, \
263         FIELD_FOR_PROTO_ONLY, {BUFF_NOUSED } }
264
265 #define proto_hint_gtpu_dwn_only { \
266         VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN, \
267         FIELD_FOR_PROTO_ONLY, {BUFF_NOUSED } }
268
269 #define proto_hint_esp { \
270         VIRTCHNL_PROTO_HDR_ESP, \
271         VIRTCHNL_PROTO_HDR_ESP_SPI, {BUFF_NOUSED } }
272
273 #define proto_hint_ah { \
274         VIRTCHNL_PROTO_HDR_AH, \
275         VIRTCHNL_PROTO_HDR_AH_SPI, {BUFF_NOUSED } }
276
277 #define proto_hint_l2tpv3 { \
278         VIRTCHNL_PROTO_HDR_L2TPV3, \
279         VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID, {BUFF_NOUSED } }
280
281 #define proto_hint_pfcp { \
282         VIRTCHNL_PROTO_HDR_PFCP, VIRTCHNL_PROTO_HDR_PFCP_SEID, {BUFF_NOUSED } }
283
284 struct virtchnl_proto_hdrs hdrs_hint_eth_src = {
285         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_eth_src }
286 };
287
288 struct virtchnl_proto_hdrs hdrs_hint_eth_dst = {
289         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_eth_dst }
290 };
291
292 struct virtchnl_proto_hdrs hdrs_hint_eth = {
293         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_eth }
294 };
295
296 struct virtchnl_proto_hdrs hdrs_hint_svlan = {
297         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_svlan }
298 };
299
300 struct virtchnl_proto_hdrs hdrs_hint_cvlan = {
301         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_cvlan }
302 };
303
304 struct virtchnl_proto_hdrs hdrs_hint_ipv4_src = {
305         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_ipv4_src }
306 };
307
308 struct virtchnl_proto_hdrs hdrs_hint_ipv4_dst = {
309         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_ipv4_dst }
310 };
311
312 struct virtchnl_proto_hdrs hdrs_hint_ipv4_src_gtpu_up = {
313         TUNNEL_LEVEL_FIRST_INNER, PROTO_COUNT_TWO, {proto_hint_gtpu_up_only,
314         proto_hint_ipv4_src }
315 };
316
317 struct virtchnl_proto_hdrs hdrs_hint_ipv4_dst_gtpu_dwn = {
318         TUNNEL_LEVEL_FIRST_INNER, PROTO_COUNT_TWO, {proto_hint_gtpu_dwn_only,
319         proto_hint_ipv4_dst }
320 };
321
322 struct virtchnl_proto_hdrs hdrs_hint_ipv4_esp = {
323         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
324         proto_hint_esp }
325 };
326
327 struct virtchnl_proto_hdrs hdrs_hint_ipv4_ah = {
328         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
329         proto_hint_ah }
330 };
331
332 struct virtchnl_proto_hdrs hdrs_hint_ipv4_l2tpv3 = {
333         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
334         proto_hint_l2tpv3 }
335 };
336
337 struct virtchnl_proto_hdrs hdrs_hint_ipv4_pfcp = {
338         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
339         proto_hint_pfcp }
340 };
341
342 struct virtchnl_proto_hdrs hdrs_hint_ipv4 = {
343         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_ipv4 }
344 };
345
346 struct virtchnl_proto_hdrs hdrs_hint_ipv4_src_udp_src_port = {
347         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_src,
348         proto_hint_udp_src_port }
349 };
350
351 struct virtchnl_proto_hdrs hdrs_hint_ipv4_src_udp_dst_port = {
352         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_src,
353         proto_hint_udp_dst_port }
354 };
355
356 struct virtchnl_proto_hdrs hdrs_hint_ipv4_dst_udp_src_port = {
357         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_dst,
358         proto_hint_udp_src_port }
359 };
360
361 struct virtchnl_proto_hdrs hdrs_hint_ipv4_dst_udp_dst_port = {
362         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_dst,
363         proto_hint_udp_dst_port }
364 };
365
366 struct virtchnl_proto_hdrs hdrs_hint_ipv4_udp_src_port = {
367         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
368         proto_hint_udp_src_port }
369 };
370
371 struct virtchnl_proto_hdrs hdrs_hint_ipv4_udp_dst_port = {
372         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
373         proto_hint_udp_dst_port }
374 };
375
376 struct virtchnl_proto_hdrs hdrs_hint_ipv4_udp = {
377         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4,
378         proto_hint_udp }
379 };
380
381 struct virtchnl_proto_hdrs hdrs_hint_ipv4_src_tcp_src_port = {
382         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_src,
383         proto_hint_tcp_src_port }
384 };
385
386 struct virtchnl_proto_hdrs hdrs_hint_ipv4_src_tcp_dst_port = {
387         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_src,
388         proto_hint_tcp_dst_port }
389 };
390
391 struct virtchnl_proto_hdrs hdrs_hint_ipv4_dst_tcp_src_port = {
392         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_dst,
393         proto_hint_tcp_src_port }
394 };
395
396 struct virtchnl_proto_hdrs hdrs_hint_ipv4_dst_tcp_dst_port = {
397         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_dst,
398         proto_hint_tcp_dst_port }
399 };
400
401 struct virtchnl_proto_hdrs hdrs_hint_ipv4_tcp_src_port = {
402         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
403         proto_hint_tcp_src_port }
404 };
405
406 struct virtchnl_proto_hdrs hdrs_hint_ipv4_tcp_dst_port = {
407         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
408         proto_hint_tcp_dst_port }
409 };
410
411 struct virtchnl_proto_hdrs hdrs_hint_ipv4_tcp = {
412         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4,
413         proto_hint_tcp }
414 };
415
416 struct virtchnl_proto_hdrs hdrs_hint_ipv4_src_sctp_src_port = {
417         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_src,
418         proto_hint_sctp_src_port }
419 };
420
421 struct virtchnl_proto_hdrs hdrs_hint_ipv4_src_sctp_dst_port = {
422         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_src,
423         proto_hint_sctp_dst_port }
424 };
425
426 struct virtchnl_proto_hdrs hdrs_hint_ipv4_dst_sctp_src_port = {
427         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_dst,
428         proto_hint_sctp_src_port }
429 };
430
431 struct virtchnl_proto_hdrs hdrs_hint_ipv4_dst_sctp_dst_port = {
432         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_dst,
433         proto_hint_sctp_dst_port }
434 };
435
436 struct virtchnl_proto_hdrs hdrs_hint_ipv4_sctp_src_port = {
437         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
438         proto_hint_sctp_src_port }
439 };
440
441 struct virtchnl_proto_hdrs hdrs_hint_ipv4_sctp_dst_port = {
442         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4_only,
443         proto_hint_sctp_dst_port }
444 };
445
446 struct virtchnl_proto_hdrs hdrs_hint_ipv4_sctp = {
447         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv4,
448         proto_hint_sctp }
449 };
450
451 struct virtchnl_proto_hdrs hdrs_hint_ipv6_src = {
452         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_ipv6_src }
453 };
454
455 struct virtchnl_proto_hdrs hdrs_hint_ipv6_dst = {
456         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_ipv6_dst }
457 };
458
459 struct virtchnl_proto_hdrs hdrs_hint_ipv6_esp = {
460         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_only,
461         proto_hint_esp }
462 };
463
464 struct virtchnl_proto_hdrs hdrs_hint_ipv6_ah = {
465         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_only,
466         proto_hint_ah }
467 };
468
469 struct virtchnl_proto_hdrs hdrs_hint_ipv6_l2tpv3 = {
470         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_only,
471         proto_hint_l2tpv3 }
472 };
473
474 struct virtchnl_proto_hdrs hdrs_hint_ipv6_pfcp = {
475         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_only,
476         proto_hint_pfcp }
477 };
478
479 struct virtchnl_proto_hdrs hdrs_hint_ipv6 = {
480         TUNNEL_LEVEL_OUTER, PROTO_COUNT_ONE, {proto_hint_ipv6 }
481 };
482
483 struct virtchnl_proto_hdrs hdrs_hint_ipv6_src_udp_src_port = {
484         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_src,
485         proto_hint_udp_src_port }
486 };
487
488 struct virtchnl_proto_hdrs hdrs_hint_ipv6_src_udp_dst_port = {
489         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_src,
490         proto_hint_udp_dst_port }
491 };
492
493 struct virtchnl_proto_hdrs hdrs_hint_ipv6_dst_udp_src_port = {
494         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_dst,
495         proto_hint_udp_src_port }
496 };
497
498 struct virtchnl_proto_hdrs hdrs_hint_ipv6_dst_udp_dst_port = {
499         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_dst,
500         proto_hint_udp_dst_port }
501 };
502
503 struct virtchnl_proto_hdrs hdrs_hint_ipv6_udp_src_port = {
504         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_udp_only,
505         proto_hint_udp_src_port }
506 };
507
508 struct virtchnl_proto_hdrs hdrs_hint_ipv6_udp_dst_port = {
509         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_udp_only,
510         proto_hint_udp_dst_port }
511 };
512
513 struct virtchnl_proto_hdrs hdrs_hint_ipv6_udp = {
514         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6,
515         proto_hint_udp }
516 };
517
518 struct virtchnl_proto_hdrs hdrs_hint_ipv6_src_tcp_src_port = {
519         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_src,
520         proto_hint_tcp_src_port }
521 };
522
523 struct virtchnl_proto_hdrs hdrs_hint_ipv6_src_tcp_dst_port = {
524         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_src,
525         proto_hint_tcp_dst_port }
526 };
527
528 struct virtchnl_proto_hdrs hdrs_hint_ipv6_dst_tcp_src_port = {
529         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_dst,
530         proto_hint_tcp_src_port }
531 };
532
533 struct virtchnl_proto_hdrs hdrs_hint_ipv6_dst_tcp_dst_port = {
534         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_dst,
535         proto_hint_tcp_dst_port }
536 };
537
538 struct virtchnl_proto_hdrs hdrs_hint_ipv6_tcp_src_port = {
539         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_udp_only,
540         proto_hint_tcp_src_port }
541 };
542
543 struct virtchnl_proto_hdrs hdrs_hint_ipv6_tcp_dst_port = {
544         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_udp_only,
545         proto_hint_tcp_dst_port }
546 };
547
548 struct virtchnl_proto_hdrs hdrs_hint_ipv6_tcp = {
549         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6,
550         proto_hint_tcp }
551 };
552
553 struct virtchnl_proto_hdrs hdrs_hint_ipv6_src_sctp_src_port = {
554         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_src,
555         proto_hint_sctp_src_port }
556 };
557
558 struct virtchnl_proto_hdrs hdrs_hint_ipv6_src_sctp_dst_port = {
559         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_src,
560         proto_hint_sctp_dst_port }
561 };
562
563 struct virtchnl_proto_hdrs hdrs_hint_ipv6_dst_sctp_src_port = {
564         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_dst,
565         proto_hint_sctp_src_port }
566 };
567
568 struct virtchnl_proto_hdrs hdrs_hint_ipv6_dst_sctp_dst_port = {
569         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6_dst,
570         proto_hint_sctp_dst_port }
571 };
572
573 struct virtchnl_proto_hdrs hdrs_hint_ipv6_sctp_src_port = {
574         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_udp_only,
575         proto_hint_sctp_src_port }
576 };
577
578 struct virtchnl_proto_hdrs hdrs_hint_ipv6_sctp_dst_port = {
579         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_udp_only,
580         proto_hint_sctp_dst_port }
581 };
582
583 struct virtchnl_proto_hdrs hdrs_hint_ipv6_sctp = {
584         TUNNEL_LEVEL_OUTER, PROTO_COUNT_TWO, {proto_hint_ipv6,
585         proto_hint_sctp }
586 };
587
588 /**
589  * The first member is pattern hint type,
590  * the second member is hash type,
591  * the third member is virtchnl protocol hdrs.
592  */
593 struct iavf_hash_match_type iavf_hash_type_list[] = {
594         /* IPV4 */
595         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_L2_SRC_ONLY, &hdrs_hint_eth_src},
596         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_L2_DST_ONLY, &hdrs_hint_eth_dst},
597         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_ETH, &hdrs_hint_eth},
598         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_S_VLAN, &hdrs_hint_svlan},
599         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_C_VLAN, &hdrs_hint_cvlan},
600         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_L3_SRC_ONLY, &hdrs_hint_ipv4_src},
601         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_L3_DST_ONLY, &hdrs_hint_ipv4_dst},
602         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_ESP, &hdrs_hint_ipv4_esp},
603         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_AH, &hdrs_hint_ipv4_ah},
604         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_L2TPV3, &hdrs_hint_ipv4_l2tpv3},
605         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_IPV4, &hdrs_hint_ipv4},
606         /* IPV4 UDP */
607         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_SRC_ONLY,
608                                         &hdrs_hint_ipv4_src_udp_src_port},
609         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_DST_ONLY,
610                                         &hdrs_hint_ipv4_src_udp_dst_port},
611         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_GTPU,
612                                         &hdrs_hint_ipv4_src_gtpu_up},
613         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L3_SRC_ONLY,
614                                         &hdrs_hint_ipv4_src},
615         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_SRC_ONLY,
616                                         &hdrs_hint_ipv4_dst_udp_src_port},
617         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_DST_ONLY,
618                                         &hdrs_hint_ipv4_dst_udp_dst_port},
619         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L3_DST_ONLY | ETH_RSS_GTPU,
620                                         &hdrs_hint_ipv4_dst_gtpu_dwn},
621         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L3_DST_ONLY,
622                                         &hdrs_hint_ipv4_dst},
623         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L4_SRC_ONLY,
624                                         &hdrs_hint_ipv4_udp_src_port},
625         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_L4_DST_ONLY,
626                                         &hdrs_hint_ipv4_udp_dst_port},
627         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_PFCP,
628                                         &hdrs_hint_ipv4_pfcp},
629         {IAVF_PATTERN_HINT_IPV4_UDP, ETH_RSS_NONFRAG_IPV4_UDP,
630                                         &hdrs_hint_ipv4_udp},
631         /* IPV4 TCP */
632         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_SRC_ONLY,
633                                         &hdrs_hint_ipv4_src_tcp_src_port},
634         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_DST_ONLY,
635                                         &hdrs_hint_ipv4_src_tcp_dst_port},
636         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_L3_SRC_ONLY,
637                                         &hdrs_hint_ipv4_src},
638         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_SRC_ONLY,
639                                         &hdrs_hint_ipv4_dst_tcp_src_port},
640         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_DST_ONLY,
641                                         &hdrs_hint_ipv4_dst_tcp_dst_port},
642         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_L3_DST_ONLY,
643                                         &hdrs_hint_ipv4_dst},
644         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_L4_SRC_ONLY,
645                                         &hdrs_hint_ipv4_tcp_src_port},
646         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_L4_DST_ONLY,
647                                         &hdrs_hint_ipv4_tcp_dst_port},
648         {IAVF_PATTERN_HINT_IPV4_TCP, ETH_RSS_NONFRAG_IPV4_TCP,
649                                         &hdrs_hint_ipv4_tcp},
650         /* IPV4 SCTP */
651         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_SRC_ONLY,
652                                         &hdrs_hint_ipv4_src_sctp_src_port},
653         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_DST_ONLY,
654                                         &hdrs_hint_ipv4_src_sctp_dst_port},
655         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_L3_SRC_ONLY,
656                                         &hdrs_hint_ipv4_src},
657         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_SRC_ONLY,
658                                         &hdrs_hint_ipv4_dst_sctp_src_port},
659         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_DST_ONLY,
660                                         &hdrs_hint_ipv4_dst_sctp_dst_port},
661         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_L3_DST_ONLY,
662                                         &hdrs_hint_ipv4_dst},
663         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_L4_SRC_ONLY,
664                                         &hdrs_hint_ipv4_sctp_src_port},
665         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_L4_DST_ONLY,
666                                         &hdrs_hint_ipv4_sctp_dst_port},
667         {IAVF_PATTERN_HINT_IPV4_SCTP, ETH_RSS_NONFRAG_IPV4_SCTP,
668                                         &hdrs_hint_ipv4_sctp},
669         /* IPV6 */
670         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_L2_SRC_ONLY, &hdrs_hint_eth_src},
671         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_L2_DST_ONLY, &hdrs_hint_eth_dst},
672         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_ETH, &hdrs_hint_eth},
673         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_S_VLAN, &hdrs_hint_svlan},
674         {IAVF_PATTERN_HINT_IPV4, ETH_RSS_C_VLAN, &hdrs_hint_cvlan},
675         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_L3_SRC_ONLY, &hdrs_hint_ipv6_src},
676         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_L3_DST_ONLY, &hdrs_hint_ipv6_dst},
677         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_ESP, &hdrs_hint_ipv6_esp},
678         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_AH, &hdrs_hint_ipv6_ah},
679         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_L2TPV3, &hdrs_hint_ipv6_l2tpv3},
680         {IAVF_PATTERN_HINT_IPV6, ETH_RSS_IPV6, &hdrs_hint_ipv6},
681         /* IPV6 UDP */
682         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_SRC_ONLY,
683                                         &hdrs_hint_ipv6_src_udp_src_port},
684         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_DST_ONLY,
685                                         &hdrs_hint_ipv6_src_udp_dst_port},
686         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_L3_SRC_ONLY,
687                                         &hdrs_hint_ipv6_src},
688         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_SRC_ONLY,
689                                         &hdrs_hint_ipv6_dst_udp_src_port},
690         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_DST_ONLY,
691                                         &hdrs_hint_ipv6_dst_udp_dst_port},
692         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_L3_DST_ONLY,
693                                         &hdrs_hint_ipv6_dst},
694         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_L4_SRC_ONLY,
695                                         &hdrs_hint_ipv6_udp_src_port},
696         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_L4_DST_ONLY,
697                                         &hdrs_hint_ipv6_udp_dst_port},
698         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_PFCP,
699                                         &hdrs_hint_ipv6_pfcp},
700         {IAVF_PATTERN_HINT_IPV6_UDP, ETH_RSS_NONFRAG_IPV6_UDP,
701                                         &hdrs_hint_ipv6_udp},
702         /* IPV6 TCP */
703         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_SRC_ONLY,
704                                         &hdrs_hint_ipv6_src_tcp_src_port},
705         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_DST_ONLY,
706                                         &hdrs_hint_ipv6_src_tcp_dst_port},
707         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_L3_SRC_ONLY,
708                                         &hdrs_hint_ipv6_src},
709         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_SRC_ONLY,
710                                         &hdrs_hint_ipv6_dst_tcp_src_port},
711         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_DST_ONLY,
712                                         &hdrs_hint_ipv6_dst_tcp_dst_port},
713         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_L3_DST_ONLY,
714                                         &hdrs_hint_ipv6_dst},
715         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_L4_SRC_ONLY,
716                                         &hdrs_hint_ipv6_tcp_src_port},
717         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_L4_DST_ONLY,
718                                         &hdrs_hint_ipv6_tcp_dst_port},
719         {IAVF_PATTERN_HINT_IPV6_TCP, ETH_RSS_NONFRAG_IPV6_TCP,
720                                         &hdrs_hint_ipv6_tcp},
721         /* IPV6 SCTP */
722         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_SRC_ONLY,
723                                         &hdrs_hint_ipv6_src_sctp_src_port},
724         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_L3_SRC_ONLY | ETH_RSS_L4_DST_ONLY,
725                                         &hdrs_hint_ipv6_src_sctp_dst_port},
726         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_L3_SRC_ONLY,
727                                         &hdrs_hint_ipv6_src},
728         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_SRC_ONLY,
729                                         &hdrs_hint_ipv6_dst_sctp_src_port},
730         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_L3_DST_ONLY | ETH_RSS_L4_DST_ONLY,
731                                         &hdrs_hint_ipv6_dst_sctp_dst_port},
732         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_L3_DST_ONLY,
733                                         &hdrs_hint_ipv6_dst},
734         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_L4_SRC_ONLY,
735                                         &hdrs_hint_ipv6_sctp_src_port},
736         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_L4_DST_ONLY,
737                                         &hdrs_hint_ipv6_sctp_dst_port},
738         {IAVF_PATTERN_HINT_IPV6_SCTP, ETH_RSS_NONFRAG_IPV6_SCTP,
739                                         &hdrs_hint_ipv6_sctp},
740 };
741
742 static struct iavf_flow_engine iavf_hash_engine = {
743         .init = iavf_hash_init,
744         .create = iavf_hash_create,
745         .destroy = iavf_hash_destroy,
746         .uninit = iavf_hash_uninit,
747         .free = iavf_hash_free,
748         .type = IAVF_FLOW_ENGINE_HASH,
749 };
750
751 /* Register parser for comms package. */
752 static struct iavf_flow_parser iavf_hash_parser = {
753         .engine = &iavf_hash_engine,
754         .array = iavf_hash_pattern_list,
755         .array_len = RTE_DIM(iavf_hash_pattern_list),
756         .parse_pattern_action = iavf_hash_parse_pattern_action,
757         .stage = IAVF_FLOW_STAGE_RSS,
758 };
759
760 RTE_INIT(iavf_hash_engine_init)
761 {
762         struct iavf_flow_engine *engine = &iavf_hash_engine;
763
764         iavf_register_flow_engine(engine);
765 }
766
767 static int
768 iavf_hash_init(struct iavf_adapter *ad)
769 {
770         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(ad);
771         struct iavf_flow_parser *parser;
772
773         if (!vf->vf_res)
774                 return -EINVAL;
775
776         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF))
777                 return -ENOTSUP;
778
779         parser = &iavf_hash_parser;
780
781         return iavf_register_parser(parser, ad);
782 }
783
784 static int
785 iavf_hash_check_inset(const struct rte_flow_item pattern[],
786                       struct rte_flow_error *error)
787 {
788         const struct rte_flow_item *item = pattern;
789
790         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
791                 if (item->last) {
792                         rte_flow_error_set(error, EINVAL,
793                                            RTE_FLOW_ERROR_TYPE_ITEM, item,
794                                            "Not support range");
795                         return -rte_errno;
796                 }
797         }
798
799         return 0;
800 }
801
802 static uint64_t
803 iavf_hash_refine_type(uint64_t rss_type, const struct rte_flow_item pattern[])
804 {
805         const struct rte_flow_item *item;
806
807         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
808                 if (item->type == RTE_FLOW_ITEM_TYPE_GTP_PSC) {
809                         const struct rte_flow_item_gtp_psc *psc = item->spec;
810
811                         if (psc && (psc->pdu_type == GTP_EH_PDU_LINK_UP ||
812                                     psc->pdu_type == GTP_EH_PDU_LINK_DWN)) {
813                                 rss_type |= ETH_RSS_GTPU;
814                         }
815                 }
816         }
817
818         return rss_type;
819 }
820
821 static int
822 iavf_hash_parse_action(struct iavf_pattern_match_item *pattern_match_item,
823                        const struct rte_flow_item pattern[],
824                        const struct rte_flow_action actions[],
825                        void **meta, struct rte_flow_error *error)
826 {
827         struct iavf_rss_meta *rss_meta = (struct iavf_rss_meta *)*meta;
828         uint32_t type_list_len = RTE_DIM(iavf_hash_type_list);
829         struct iavf_hash_match_type *type_match_item;
830         enum rte_flow_action_type action_type;
831         const struct rte_flow_action_rss *rss;
832         const struct rte_flow_action *action;
833         bool item_found = false;
834         uint64_t rss_type;
835         uint16_t i;
836
837         struct iavf_pattern_match_type *tt = (struct iavf_pattern_match_type *)
838                 (pattern_match_item->meta);
839
840         /* Supported action is RSS. */
841         for (action = actions; action->type !=
842                 RTE_FLOW_ACTION_TYPE_END; action++) {
843                 action_type = action->type;
844                 switch (action_type) {
845                 case RTE_FLOW_ACTION_TYPE_RSS:
846                         rss = action->conf;
847                         rss_type = rss->types;
848
849                         /**
850                          * Check simultaneous use of SRC_ONLY and DST_ONLY
851                          * of the same level.
852                          */
853                         rss_type = rte_eth_rss_hf_refine(rss_type);
854
855                         /**
856                          * Refine the hash type base on some specific item of
857                          * the pattern, such as identify the gtpu hash.
858                          */
859                         rss_type = iavf_hash_refine_type(rss_type, pattern);
860
861                         /* Check if pattern is empty. */
862                         if (pattern_match_item->pattern_list !=
863                                 iavf_pattern_empty && rss->func ==
864                                 RTE_ETH_HASH_FUNCTION_SIMPLE_XOR)
865                                 return rte_flow_error_set(error, ENOTSUP,
866                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
867                                         "Not supported flow");
868
869                         if (rss->level)
870                                 return rte_flow_error_set(error, ENOTSUP,
871                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
872                                         "a nonzero RSS encapsulation level is not supported");
873
874                         if (rss->key_len)
875                                 return rte_flow_error_set(error, ENOTSUP,
876                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
877                                         "a nonzero RSS key_len is not supported");
878
879                         if (rss->queue_num)
880                                 return rte_flow_error_set(error, ENOTSUP,
881                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
882                                         "a non-NULL RSS queue is not supported");
883
884                         /* Check hash function and save it to rss_meta. */
885                         if (rss->func == RTE_ETH_HASH_FUNCTION_SIMPLE_XOR)
886                                 rss_meta->hash_function =
887                                 RTE_ETH_HASH_FUNCTION_SIMPLE_XOR;
888
889                         if (rss->func ==
890                             RTE_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ)
891                                 rss_meta->hash_function =
892                                 RTE_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ;
893
894                         type_match_item =
895                         rte_zmalloc("iavf_type_match_item",
896                                     sizeof(struct iavf_hash_match_type), 0);
897                         if (!type_match_item) {
898                                 rte_flow_error_set(error, EINVAL,
899                                                    RTE_FLOW_ERROR_TYPE_HANDLE,
900                                                    NULL,
901                                                    "No memory for type_match_item");
902                                 return -ENOMEM;
903                         }
904
905                         /* Find matched proto hdrs according to hash type. */
906                         for (i = 0; i < type_list_len; i++) {
907                                 struct iavf_hash_match_type *ht_map =
908                                         &iavf_hash_type_list[i];
909                                 if (rss_type == ht_map->hash_type &&
910                                     tt->phint_type == ht_map->phint_type) {
911                                         type_match_item->hash_type =
912                                                 ht_map->hash_type;
913                                         type_match_item->proto_hdrs =
914                                                 ht_map->proto_hdrs;
915                                         rss_meta->proto_hdrs =
916                                                 type_match_item->proto_hdrs;
917                                         item_found = true;
918                                 }
919                         }
920
921                         rte_free(type_match_item);
922
923                         if (!item_found)
924                                 return rte_flow_error_set(error, ENOTSUP,
925                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
926                                         "Not supported flow");
927                         break;
928
929                 case RTE_FLOW_ACTION_TYPE_END:
930                         break;
931
932                 default:
933                         rte_flow_error_set(error, EINVAL,
934                                            RTE_FLOW_ERROR_TYPE_ACTION, action,
935                                            "Invalid action.");
936                         return -rte_errno;
937                 }
938         }
939
940         return 0;
941 }
942
943 static int
944 iavf_hash_parse_pattern_action(__rte_unused struct iavf_adapter *ad,
945                                struct iavf_pattern_match_item *array,
946                                uint32_t array_len,
947                                const struct rte_flow_item pattern[],
948                                const struct rte_flow_action actions[],
949                                void **meta,
950                                struct rte_flow_error *error)
951 {
952         struct iavf_pattern_match_item *pattern_match_item;
953         struct iavf_rss_meta *rss_meta_ptr;
954         int ret = 0;
955
956         rss_meta_ptr = rte_zmalloc(NULL, sizeof(*rss_meta_ptr), 0);
957         if (!rss_meta_ptr) {
958                 rte_flow_error_set(error, EINVAL,
959                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
960                                    "No memory for rss_meta_ptr");
961                 return -ENOMEM;
962         }
963
964         /* Check rss supported pattern and find matched pattern. */
965         pattern_match_item =
966                 iavf_search_pattern_match_item(pattern, array, array_len,
967                                                error);
968         if (!pattern_match_item) {
969                 ret = -rte_errno;
970                 goto error;
971         }
972
973         ret = iavf_hash_check_inset(pattern, error);
974         if (ret)
975                 goto error;
976
977         /* Check rss action. */
978         ret = iavf_hash_parse_action(pattern_match_item, pattern, actions,
979                                      (void **)&rss_meta_ptr, error);
980
981 error:
982         if (!ret && meta)
983                 *meta = rss_meta_ptr;
984         else
985                 rte_free(rss_meta_ptr);
986
987         rte_free(pattern_match_item);
988
989         return ret;
990 }
991
992 static int
993 iavf_hash_create(__rte_unused struct iavf_adapter *ad,
994                  __rte_unused struct rte_flow *flow, void *meta,
995                  __rte_unused struct rte_flow_error *error)
996 {
997         struct iavf_rss_meta *rss_meta = (struct iavf_rss_meta *)meta;
998         struct virtchnl_rss_cfg *rss_cfg;
999         int ret = 0;
1000
1001         rss_cfg = rte_zmalloc("iavf rss rule",
1002                               sizeof(struct virtchnl_rss_cfg), 0);
1003         if (!rss_cfg) {
1004                 rte_flow_error_set(error, EINVAL,
1005                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1006                                    "No memory for rss rule");
1007                 return -ENOMEM;
1008         }
1009
1010         rss_cfg->proto_hdrs = *rss_meta->proto_hdrs;
1011         rss_cfg->rss_algorithm = rss_meta->hash_function;
1012
1013         ret = iavf_add_del_rss_cfg(ad, rss_cfg, true);
1014         if (!ret) {
1015                 flow->rule = rss_cfg;
1016         } else {
1017                 PMD_DRV_LOG(ERR, "fail to add RSS configure");
1018                 rte_free(rss_cfg);
1019         }
1020
1021         rte_free(meta);
1022
1023         return ret;
1024 }
1025
1026 static int
1027 iavf_hash_destroy(__rte_unused struct iavf_adapter *ad,
1028                   struct rte_flow *flow,
1029                   __rte_unused struct rte_flow_error *error)
1030 {
1031         struct virtchnl_rss_cfg *rss_cfg;
1032         int ret = 0;
1033
1034         rss_cfg = (struct virtchnl_rss_cfg *)flow->rule;
1035
1036         ret = iavf_add_del_rss_cfg(ad, rss_cfg, false);
1037         if (ret)
1038                 PMD_DRV_LOG(ERR, "fail to del RSS configure");
1039
1040         return ret;
1041 }
1042
1043 static void
1044 iavf_hash_uninit(struct iavf_adapter *ad)
1045 {
1046         iavf_unregister_parser(&iavf_hash_parser, ad);
1047 }
1048
1049 static void
1050 iavf_hash_free(struct rte_flow *flow)
1051 {
1052         rte_free(flow->rule);
1053 }