net/mlx5: add Altivec Rx
[dpdk.git] / drivers / net / hns3 / hns3_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #include <stdbool.h>
6 #include <sys/queue.h>
7 #include <rte_flow_driver.h>
8 #include <rte_io.h>
9 #include <rte_malloc.h>
10
11 #include "hns3_ethdev.h"
12 #include "hns3_logs.h"
13
14 /* Default default keys */
15 static uint8_t hns3_hash_key[] = {
16         0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
17         0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
18         0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
19         0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
20         0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA
21 };
22
23 static const uint8_t full_mask[VNI_OR_TNI_LEN] = { 0xFF, 0xFF, 0xFF };
24 static const uint8_t zero_mask[VNI_OR_TNI_LEN] = { 0x00, 0x00, 0x00 };
25
26 /* Special Filter id for non-specific packet flagging. Don't change value */
27 #define HNS3_MAX_FILTER_ID      0x0FFF
28
29 #define ETHER_TYPE_MASK         0xFFFF
30 #define IPPROTO_MASK            0xFF
31 #define TUNNEL_TYPE_MASK        0xFFFF
32
33 #define HNS3_TUNNEL_TYPE_VXLAN          0x12B5
34 #define HNS3_TUNNEL_TYPE_VXLAN_GPE      0x12B6
35 #define HNS3_TUNNEL_TYPE_GENEVE         0x17C1
36 #define HNS3_TUNNEL_TYPE_NVGRE          0x6558
37
38 static enum rte_flow_item_type first_items[] = {
39         RTE_FLOW_ITEM_TYPE_ETH,
40         RTE_FLOW_ITEM_TYPE_IPV4,
41         RTE_FLOW_ITEM_TYPE_IPV6,
42         RTE_FLOW_ITEM_TYPE_TCP,
43         RTE_FLOW_ITEM_TYPE_UDP,
44         RTE_FLOW_ITEM_TYPE_SCTP,
45         RTE_FLOW_ITEM_TYPE_ICMP,
46         RTE_FLOW_ITEM_TYPE_NVGRE,
47         RTE_FLOW_ITEM_TYPE_VXLAN,
48         RTE_FLOW_ITEM_TYPE_GENEVE,
49         RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
50         RTE_FLOW_ITEM_TYPE_MPLS
51 };
52
53 static enum rte_flow_item_type L2_next_items[] = {
54         RTE_FLOW_ITEM_TYPE_VLAN,
55         RTE_FLOW_ITEM_TYPE_IPV4,
56         RTE_FLOW_ITEM_TYPE_IPV6
57 };
58
59 static enum rte_flow_item_type L3_next_items[] = {
60         RTE_FLOW_ITEM_TYPE_TCP,
61         RTE_FLOW_ITEM_TYPE_UDP,
62         RTE_FLOW_ITEM_TYPE_SCTP,
63         RTE_FLOW_ITEM_TYPE_NVGRE,
64         RTE_FLOW_ITEM_TYPE_ICMP
65 };
66
67 static enum rte_flow_item_type L4_next_items[] = {
68         RTE_FLOW_ITEM_TYPE_VXLAN,
69         RTE_FLOW_ITEM_TYPE_GENEVE,
70         RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
71         RTE_FLOW_ITEM_TYPE_MPLS
72 };
73
74 static enum rte_flow_item_type tunnel_next_items[] = {
75         RTE_FLOW_ITEM_TYPE_ETH,
76         RTE_FLOW_ITEM_TYPE_VLAN
77 };
78
79 struct items_step_mngr {
80         enum rte_flow_item_type *items;
81         int count;
82 };
83
84 static inline void
85 net_addr_to_host(uint32_t *dst, const rte_be32_t *src, size_t len)
86 {
87         size_t i;
88
89         for (i = 0; i < len; i++)
90                 dst[i] = rte_be_to_cpu_32(src[i]);
91 }
92
93 static inline const struct rte_flow_action *
94 find_rss_action(const struct rte_flow_action actions[])
95 {
96         const struct rte_flow_action *next = &actions[0];
97
98         for (; next->type != RTE_FLOW_ACTION_TYPE_END; next++) {
99                 if (next->type == RTE_FLOW_ACTION_TYPE_RSS)
100                         return next;
101         }
102         return NULL;
103 }
104
105 static inline struct hns3_flow_counter *
106 hns3_counter_lookup(struct rte_eth_dev *dev, uint32_t id)
107 {
108         struct hns3_adapter *hns = dev->data->dev_private;
109         struct hns3_pf *pf = &hns->pf;
110         struct hns3_flow_counter *cnt;
111
112         LIST_FOREACH(cnt, &pf->flow_counters, next) {
113                 if (cnt->id == id)
114                         return cnt;
115         }
116         return NULL;
117 }
118
119 static int
120 hns3_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id,
121                  struct rte_flow_error *error)
122 {
123         struct hns3_adapter *hns = dev->data->dev_private;
124         struct hns3_pf *pf = &hns->pf;
125         struct hns3_flow_counter *cnt;
126
127         cnt = hns3_counter_lookup(dev, id);
128         if (cnt) {
129                 if (!cnt->shared || cnt->shared != shared)
130                         return rte_flow_error_set(error, ENOTSUP,
131                                                   RTE_FLOW_ERROR_TYPE_ACTION,
132                                                   cnt,
133                                                   "Counter id is used,shared flag not match");
134                 cnt->ref_cnt++;
135                 return 0;
136         }
137
138         cnt = rte_zmalloc("hns3 counter", sizeof(*cnt), 0);
139         if (cnt == NULL)
140                 return rte_flow_error_set(error, ENOMEM,
141                                           RTE_FLOW_ERROR_TYPE_ACTION, cnt,
142                                           "Alloc mem for counter failed");
143         cnt->id = id;
144         cnt->shared = shared;
145         cnt->ref_cnt = 1;
146         cnt->hits = 0;
147         LIST_INSERT_HEAD(&pf->flow_counters, cnt, next);
148         return 0;
149 }
150
151 static int
152 hns3_counter_query(struct rte_eth_dev *dev, struct rte_flow *flow,
153                    struct rte_flow_query_count *qc,
154                    struct rte_flow_error *error)
155 {
156         struct hns3_adapter *hns = dev->data->dev_private;
157         struct hns3_flow_counter *cnt;
158         uint64_t value;
159         int ret;
160
161         /* FDIR is available only in PF driver */
162         if (hns->is_vf)
163                 return rte_flow_error_set(error, ENOTSUP,
164                                           RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
165                                           "Fdir is not supported in VF");
166         cnt = hns3_counter_lookup(dev, flow->counter_id);
167         if (cnt == NULL)
168                 return rte_flow_error_set(error, EINVAL,
169                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
170                                           "Can't find counter id");
171
172         ret = hns3_get_count(&hns->hw, flow->counter_id, &value);
173         if (ret) {
174                 rte_flow_error_set(error, -ret,
175                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
176                                    NULL, "Read counter fail.");
177                 return ret;
178         }
179         qc->hits_set = 1;
180         qc->hits = value;
181
182         return 0;
183 }
184
185 static int
186 hns3_counter_release(struct rte_eth_dev *dev, uint32_t id)
187 {
188         struct hns3_adapter *hns = dev->data->dev_private;
189         struct hns3_hw *hw = &hns->hw;
190         struct hns3_flow_counter *cnt;
191
192         cnt = hns3_counter_lookup(dev, id);
193         if (cnt == NULL) {
194                 hns3_err(hw, "Can't find available counter to release");
195                 return -EINVAL;
196         }
197         cnt->ref_cnt--;
198         if (cnt->ref_cnt == 0) {
199                 LIST_REMOVE(cnt, next);
200                 rte_free(cnt);
201         }
202         return 0;
203 }
204
205 static void
206 hns3_counter_flush(struct rte_eth_dev *dev)
207 {
208         struct hns3_adapter *hns = dev->data->dev_private;
209         struct hns3_pf *pf = &hns->pf;
210         struct hns3_flow_counter *cnt_ptr;
211
212         cnt_ptr = LIST_FIRST(&pf->flow_counters);
213         while (cnt_ptr) {
214                 LIST_REMOVE(cnt_ptr, next);
215                 rte_free(cnt_ptr);
216                 cnt_ptr = LIST_FIRST(&pf->flow_counters);
217         }
218 }
219
220 static int
221 hns3_handle_action_queue(struct rte_eth_dev *dev,
222                          const struct rte_flow_action *action,
223                          struct hns3_fdir_rule *rule,
224                          struct rte_flow_error *error)
225 {
226         struct hns3_adapter *hns = dev->data->dev_private;
227         struct hns3_hw *hw = &hns->hw;
228         const struct rte_flow_action_queue *queue;
229
230         queue = (const struct rte_flow_action_queue *)action->conf;
231         if (queue->index >= hw->data->nb_rx_queues)
232                 return rte_flow_error_set(error, EINVAL,
233                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
234                                           "Invalid queue ID in PF");
235         rule->queue_id = queue->index;
236         rule->action = HNS3_FD_ACTION_ACCEPT_PACKET;
237         return 0;
238 }
239
240 /*
241  * Parse actions structure from the provided pattern.
242  * The pattern is validated as the items are copied.
243  *
244  * @param actions[in]
245  * @param rule[out]
246  *   NIC specfilc actions derived from the actions.
247  * @param error[out]
248  */
249 static int
250 hns3_handle_actions(struct rte_eth_dev *dev,
251                     const struct rte_flow_action actions[],
252                     struct hns3_fdir_rule *rule, struct rte_flow_error *error)
253 {
254         struct hns3_adapter *hns = dev->data->dev_private;
255         const struct rte_flow_action_count *act_count;
256         const struct rte_flow_action_mark *mark;
257         struct hns3_pf *pf = &hns->pf;
258         uint32_t counter_num;
259         int ret;
260
261         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
262                 switch (actions->type) {
263                 case RTE_FLOW_ACTION_TYPE_QUEUE:
264                         ret = hns3_handle_action_queue(dev, actions, rule,
265                                                        error);
266                         if (ret)
267                                 return ret;
268                         break;
269                 case RTE_FLOW_ACTION_TYPE_DROP:
270                         rule->action = HNS3_FD_ACTION_DROP_PACKET;
271                         break;
272                 case RTE_FLOW_ACTION_TYPE_MARK:
273                         mark =
274                             (const struct rte_flow_action_mark *)actions->conf;
275                         if (mark->id >= HNS3_MAX_FILTER_ID)
276                                 return rte_flow_error_set(error, EINVAL,
277                                                      RTE_FLOW_ERROR_TYPE_ACTION,
278                                                      actions,
279                                                      "Invalid Mark ID");
280                         rule->fd_id = mark->id;
281                         rule->flags |= HNS3_RULE_FLAG_FDID;
282                         break;
283                 case RTE_FLOW_ACTION_TYPE_FLAG:
284                         rule->fd_id = HNS3_MAX_FILTER_ID;
285                         rule->flags |= HNS3_RULE_FLAG_FDID;
286                         break;
287                 case RTE_FLOW_ACTION_TYPE_COUNT:
288                         act_count =
289                             (const struct rte_flow_action_count *)actions->conf;
290                         counter_num = pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1];
291                         if (act_count->id >= counter_num)
292                                 return rte_flow_error_set(error, EINVAL,
293                                                      RTE_FLOW_ERROR_TYPE_ACTION,
294                                                      actions,
295                                                      "Invalid counter id");
296                         rule->act_cnt = *act_count;
297                         rule->flags |= HNS3_RULE_FLAG_COUNTER;
298                         break;
299                 case RTE_FLOW_ACTION_TYPE_VOID:
300                         break;
301                 default:
302                         return rte_flow_error_set(error, ENOTSUP,
303                                                   RTE_FLOW_ERROR_TYPE_ACTION,
304                                                   NULL, "Unsupported action");
305                 }
306         }
307
308         return 0;
309 }
310
311 /* Parse to get the attr and action info of flow director rule. */
312 static int
313 hns3_check_attr(const struct rte_flow_attr *attr, struct rte_flow_error *error)
314 {
315         if (!attr->ingress)
316                 return rte_flow_error_set(error, EINVAL,
317                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
318                                           attr, "Ingress can't be zero");
319         if (attr->egress)
320                 return rte_flow_error_set(error, ENOTSUP,
321                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
322                                           attr, "Not support egress");
323         if (attr->transfer)
324                 return rte_flow_error_set(error, ENOTSUP,
325                                           RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
326                                           attr, "No support for transfer");
327         if (attr->priority)
328                 return rte_flow_error_set(error, ENOTSUP,
329                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
330                                           attr, "Not support priority");
331         if (attr->group)
332                 return rte_flow_error_set(error, ENOTSUP,
333                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
334                                           attr, "Not support group");
335         return 0;
336 }
337
338 static int
339 hns3_parse_eth(const struct rte_flow_item *item,
340                    struct hns3_fdir_rule *rule, struct rte_flow_error *error)
341 {
342         const struct rte_flow_item_eth *eth_spec;
343         const struct rte_flow_item_eth *eth_mask;
344
345         if (item->spec == NULL && item->mask)
346                 return rte_flow_error_set(error, EINVAL,
347                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
348                                           "Can't configure FDIR with mask but without spec");
349
350         /* Only used to describe the protocol stack. */
351         if (item->spec == NULL && item->mask == NULL)
352                 return 0;
353
354         if (item->mask) {
355                 eth_mask = item->mask;
356                 if (eth_mask->type) {
357                         hns3_set_bit(rule->input_set, INNER_ETH_TYPE, 1);
358                         rule->key_conf.mask.ether_type =
359                             rte_be_to_cpu_16(eth_mask->type);
360                 }
361                 if (!rte_is_zero_ether_addr(&eth_mask->src)) {
362                         hns3_set_bit(rule->input_set, INNER_SRC_MAC, 1);
363                         memcpy(rule->key_conf.mask.src_mac,
364                                eth_mask->src.addr_bytes, RTE_ETHER_ADDR_LEN);
365                 }
366                 if (!rte_is_zero_ether_addr(&eth_mask->dst)) {
367                         hns3_set_bit(rule->input_set, INNER_DST_MAC, 1);
368                         memcpy(rule->key_conf.mask.dst_mac,
369                                eth_mask->dst.addr_bytes, RTE_ETHER_ADDR_LEN);
370                 }
371         }
372
373         eth_spec = item->spec;
374         rule->key_conf.spec.ether_type = rte_be_to_cpu_16(eth_spec->type);
375         memcpy(rule->key_conf.spec.src_mac, eth_spec->src.addr_bytes,
376                RTE_ETHER_ADDR_LEN);
377         memcpy(rule->key_conf.spec.dst_mac, eth_spec->dst.addr_bytes,
378                RTE_ETHER_ADDR_LEN);
379         return 0;
380 }
381
382 static int
383 hns3_parse_vlan(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
384                 struct rte_flow_error *error)
385 {
386         const struct rte_flow_item_vlan *vlan_spec;
387         const struct rte_flow_item_vlan *vlan_mask;
388
389         if (item->spec == NULL && item->mask)
390                 return rte_flow_error_set(error, EINVAL,
391                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
392                                           "Can't configure FDIR with mask but without spec");
393
394         rule->key_conf.vlan_num++;
395         if (rule->key_conf.vlan_num > VLAN_TAG_NUM_MAX)
396                 return rte_flow_error_set(error, EINVAL,
397                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
398                                           "Vlan_num is more than 2");
399
400         /* Only used to describe the protocol stack. */
401         if (item->spec == NULL && item->mask == NULL)
402                 return 0;
403
404         if (item->mask) {
405                 vlan_mask = item->mask;
406                 if (vlan_mask->tci) {
407                         if (rule->key_conf.vlan_num == 1) {
408                                 hns3_set_bit(rule->input_set, INNER_VLAN_TAG1,
409                                              1);
410                                 rule->key_conf.mask.vlan_tag1 =
411                                     rte_be_to_cpu_16(vlan_mask->tci);
412                         } else {
413                                 hns3_set_bit(rule->input_set, INNER_VLAN_TAG2,
414                                              1);
415                                 rule->key_conf.mask.vlan_tag2 =
416                                     rte_be_to_cpu_16(vlan_mask->tci);
417                         }
418                 }
419         }
420
421         vlan_spec = item->spec;
422         if (rule->key_conf.vlan_num == 1)
423                 rule->key_conf.spec.vlan_tag1 =
424                     rte_be_to_cpu_16(vlan_spec->tci);
425         else
426                 rule->key_conf.spec.vlan_tag2 =
427                     rte_be_to_cpu_16(vlan_spec->tci);
428         return 0;
429 }
430
431 static int
432 hns3_parse_ipv4(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
433                 struct rte_flow_error *error)
434 {
435         const struct rte_flow_item_ipv4 *ipv4_spec;
436         const struct rte_flow_item_ipv4 *ipv4_mask;
437
438         if (item->spec == NULL && item->mask)
439                 return rte_flow_error_set(error, EINVAL,
440                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
441                                           "Can't configure FDIR with mask but without spec");
442
443         hns3_set_bit(rule->input_set, INNER_ETH_TYPE, 1);
444         rule->key_conf.spec.ether_type = RTE_ETHER_TYPE_IPV4;
445         rule->key_conf.mask.ether_type = ETHER_TYPE_MASK;
446         /* Only used to describe the protocol stack. */
447         if (item->spec == NULL && item->mask == NULL)
448                 return 0;
449
450         if (item->mask) {
451                 ipv4_mask = item->mask;
452
453                 if (ipv4_mask->hdr.total_length ||
454                     ipv4_mask->hdr.packet_id ||
455                     ipv4_mask->hdr.fragment_offset ||
456                     ipv4_mask->hdr.time_to_live ||
457                     ipv4_mask->hdr.hdr_checksum) {
458                         return rte_flow_error_set(error, EINVAL,
459                                                   RTE_FLOW_ERROR_TYPE_ITEM,
460                                                   item,
461                                                   "Only support src & dst ip,tos,proto in IPV4");
462                 }
463
464                 if (ipv4_mask->hdr.src_addr) {
465                         hns3_set_bit(rule->input_set, INNER_SRC_IP, 1);
466                         rule->key_conf.mask.src_ip[IP_ADDR_KEY_ID] =
467                             rte_be_to_cpu_32(ipv4_mask->hdr.src_addr);
468                 }
469
470                 if (ipv4_mask->hdr.dst_addr) {
471                         hns3_set_bit(rule->input_set, INNER_DST_IP, 1);
472                         rule->key_conf.mask.dst_ip[IP_ADDR_KEY_ID] =
473                             rte_be_to_cpu_32(ipv4_mask->hdr.dst_addr);
474                 }
475
476                 if (ipv4_mask->hdr.type_of_service) {
477                         hns3_set_bit(rule->input_set, INNER_IP_TOS, 1);
478                         rule->key_conf.mask.ip_tos =
479                             ipv4_mask->hdr.type_of_service;
480                 }
481
482                 if (ipv4_mask->hdr.next_proto_id) {
483                         hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
484                         rule->key_conf.mask.ip_proto =
485                             ipv4_mask->hdr.next_proto_id;
486                 }
487         }
488
489         ipv4_spec = item->spec;
490         rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID] =
491             rte_be_to_cpu_32(ipv4_spec->hdr.src_addr);
492         rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID] =
493             rte_be_to_cpu_32(ipv4_spec->hdr.dst_addr);
494         rule->key_conf.spec.ip_tos = ipv4_spec->hdr.type_of_service;
495         rule->key_conf.spec.ip_proto = ipv4_spec->hdr.next_proto_id;
496         return 0;
497 }
498
499 static int
500 hns3_parse_ipv6(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
501                 struct rte_flow_error *error)
502 {
503         const struct rte_flow_item_ipv6 *ipv6_spec;
504         const struct rte_flow_item_ipv6 *ipv6_mask;
505
506         if (item->spec == NULL && item->mask)
507                 return rte_flow_error_set(error, EINVAL,
508                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
509                                           "Can't configure FDIR with mask but without spec");
510
511         hns3_set_bit(rule->input_set, INNER_ETH_TYPE, 1);
512         rule->key_conf.spec.ether_type = RTE_ETHER_TYPE_IPV6;
513         rule->key_conf.mask.ether_type = ETHER_TYPE_MASK;
514
515         /* Only used to describe the protocol stack. */
516         if (item->spec == NULL && item->mask == NULL)
517                 return 0;
518
519         if (item->mask) {
520                 ipv6_mask = item->mask;
521                 if (ipv6_mask->hdr.vtc_flow ||
522                     ipv6_mask->hdr.payload_len || ipv6_mask->hdr.hop_limits) {
523                         return rte_flow_error_set(error, EINVAL,
524                                                   RTE_FLOW_ERROR_TYPE_ITEM,
525                                                   item,
526                                                   "Only support src & dst ip,proto in IPV6");
527                 }
528                 net_addr_to_host(rule->key_conf.mask.src_ip,
529                                  (const rte_be32_t *)ipv6_mask->hdr.src_addr,
530                                  IP_ADDR_LEN);
531                 net_addr_to_host(rule->key_conf.mask.dst_ip,
532                                  (const rte_be32_t *)ipv6_mask->hdr.dst_addr,
533                                  IP_ADDR_LEN);
534                 rule->key_conf.mask.ip_proto = ipv6_mask->hdr.proto;
535                 if (rule->key_conf.mask.src_ip[IP_ADDR_KEY_ID])
536                         hns3_set_bit(rule->input_set, INNER_SRC_IP, 1);
537                 if (rule->key_conf.mask.dst_ip[IP_ADDR_KEY_ID])
538                         hns3_set_bit(rule->input_set, INNER_DST_IP, 1);
539                 if (ipv6_mask->hdr.proto)
540                         hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
541         }
542
543         ipv6_spec = item->spec;
544         net_addr_to_host(rule->key_conf.spec.src_ip,
545                          (const rte_be32_t *)ipv6_spec->hdr.src_addr,
546                          IP_ADDR_LEN);
547         net_addr_to_host(rule->key_conf.spec.dst_ip,
548                          (const rte_be32_t *)ipv6_spec->hdr.dst_addr,
549                          IP_ADDR_LEN);
550         rule->key_conf.spec.ip_proto = ipv6_spec->hdr.proto;
551
552         return 0;
553 }
554
555 static int
556 hns3_parse_tcp(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
557                struct rte_flow_error *error)
558 {
559         const struct rte_flow_item_tcp *tcp_spec;
560         const struct rte_flow_item_tcp *tcp_mask;
561
562         if (item->spec == NULL && item->mask)
563                 return rte_flow_error_set(error, EINVAL,
564                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
565                                           "Can't configure FDIR with mask but without spec");
566
567         hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
568         rule->key_conf.spec.ip_proto = IPPROTO_TCP;
569         rule->key_conf.mask.ip_proto = IPPROTO_MASK;
570
571         /* Only used to describe the protocol stack. */
572         if (item->spec == NULL && item->mask == NULL)
573                 return 0;
574
575         if (item->mask) {
576                 tcp_mask = item->mask;
577                 if (tcp_mask->hdr.sent_seq ||
578                     tcp_mask->hdr.recv_ack ||
579                     tcp_mask->hdr.data_off ||
580                     tcp_mask->hdr.tcp_flags ||
581                     tcp_mask->hdr.rx_win ||
582                     tcp_mask->hdr.cksum || tcp_mask->hdr.tcp_urp) {
583                         return rte_flow_error_set(error, EINVAL,
584                                                   RTE_FLOW_ERROR_TYPE_ITEM,
585                                                   item,
586                                                   "Only support src & dst port in TCP");
587                 }
588
589                 if (tcp_mask->hdr.src_port) {
590                         hns3_set_bit(rule->input_set, INNER_SRC_PORT, 1);
591                         rule->key_conf.mask.src_port =
592                             rte_be_to_cpu_16(tcp_mask->hdr.src_port);
593                 }
594                 if (tcp_mask->hdr.dst_port) {
595                         hns3_set_bit(rule->input_set, INNER_DST_PORT, 1);
596                         rule->key_conf.mask.dst_port =
597                             rte_be_to_cpu_16(tcp_mask->hdr.dst_port);
598                 }
599         }
600
601         tcp_spec = item->spec;
602         rule->key_conf.spec.src_port = rte_be_to_cpu_16(tcp_spec->hdr.src_port);
603         rule->key_conf.spec.dst_port = rte_be_to_cpu_16(tcp_spec->hdr.dst_port);
604
605         return 0;
606 }
607
608 static int
609 hns3_parse_udp(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
610                struct rte_flow_error *error)
611 {
612         const struct rte_flow_item_udp *udp_spec;
613         const struct rte_flow_item_udp *udp_mask;
614
615         if (item->spec == NULL && item->mask)
616                 return rte_flow_error_set(error, EINVAL,
617                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
618                                           "Can't configure FDIR with mask but without spec");
619
620         hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
621         rule->key_conf.spec.ip_proto = IPPROTO_UDP;
622         rule->key_conf.mask.ip_proto = IPPROTO_MASK;
623         /* Only used to describe the protocol stack. */
624         if (item->spec == NULL && item->mask == NULL)
625                 return 0;
626
627         if (item->mask) {
628                 udp_mask = item->mask;
629                 if (udp_mask->hdr.dgram_len || udp_mask->hdr.dgram_cksum) {
630                         return rte_flow_error_set(error, EINVAL,
631                                                   RTE_FLOW_ERROR_TYPE_ITEM,
632                                                   item,
633                                                   "Only support src & dst port in UDP");
634                 }
635                 if (udp_mask->hdr.src_port) {
636                         hns3_set_bit(rule->input_set, INNER_SRC_PORT, 1);
637                         rule->key_conf.mask.src_port =
638                             rte_be_to_cpu_16(udp_mask->hdr.src_port);
639                 }
640                 if (udp_mask->hdr.dst_port) {
641                         hns3_set_bit(rule->input_set, INNER_DST_PORT, 1);
642                         rule->key_conf.mask.dst_port =
643                             rte_be_to_cpu_16(udp_mask->hdr.dst_port);
644                 }
645         }
646
647         udp_spec = item->spec;
648         rule->key_conf.spec.src_port = rte_be_to_cpu_16(udp_spec->hdr.src_port);
649         rule->key_conf.spec.dst_port = rte_be_to_cpu_16(udp_spec->hdr.dst_port);
650
651         return 0;
652 }
653
654 static int
655 hns3_parse_sctp(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
656                 struct rte_flow_error *error)
657 {
658         const struct rte_flow_item_sctp *sctp_spec;
659         const struct rte_flow_item_sctp *sctp_mask;
660
661         if (item->spec == NULL && item->mask)
662                 return rte_flow_error_set(error, EINVAL,
663                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
664                                           "Can't configure FDIR with mask but without spec");
665
666         hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
667         rule->key_conf.spec.ip_proto = IPPROTO_SCTP;
668         rule->key_conf.mask.ip_proto = IPPROTO_MASK;
669
670         /* Only used to describe the protocol stack. */
671         if (item->spec == NULL && item->mask == NULL)
672                 return 0;
673
674         if (item->mask) {
675                 sctp_mask = item->mask;
676                 if (sctp_mask->hdr.cksum)
677                         return rte_flow_error_set(error, EINVAL,
678                                                   RTE_FLOW_ERROR_TYPE_ITEM,
679                                                   item,
680                                                   "Only support src & dst port in SCTP");
681
682                 if (sctp_mask->hdr.src_port) {
683                         hns3_set_bit(rule->input_set, INNER_SRC_PORT, 1);
684                         rule->key_conf.mask.src_port =
685                             rte_be_to_cpu_16(sctp_mask->hdr.src_port);
686                 }
687                 if (sctp_mask->hdr.dst_port) {
688                         hns3_set_bit(rule->input_set, INNER_DST_PORT, 1);
689                         rule->key_conf.mask.dst_port =
690                             rte_be_to_cpu_16(sctp_mask->hdr.dst_port);
691                 }
692                 if (sctp_mask->hdr.tag) {
693                         hns3_set_bit(rule->input_set, INNER_SCTP_TAG, 1);
694                         rule->key_conf.mask.sctp_tag =
695                             rte_be_to_cpu_32(sctp_mask->hdr.tag);
696                 }
697         }
698
699         sctp_spec = item->spec;
700         rule->key_conf.spec.src_port =
701             rte_be_to_cpu_16(sctp_spec->hdr.src_port);
702         rule->key_conf.spec.dst_port =
703             rte_be_to_cpu_16(sctp_spec->hdr.dst_port);
704         rule->key_conf.spec.sctp_tag = rte_be_to_cpu_32(sctp_spec->hdr.tag);
705
706         return 0;
707 }
708
709 /*
710  * Check items before tunnel, save inner configs to outer configs,and clear
711  * inner configs.
712  * The key consists of two parts: meta_data and tuple keys.
713  * Meta data uses 15 bits, including vlan_num(2bit), des_port(12bit) and tunnel
714  * packet(1bit).
715  * Tuple keys uses 384bit, including ot_dst-mac(48bit), ot_dst-port(16bit),
716  * ot_tun_vni(24bit), ot_flow_id(8bit), src-mac(48bit), dst-mac(48bit),
717  * src-ip(32/128bit), dst-ip(32/128bit), src-port(16bit), dst-port(16bit),
718  * tos(8bit), ether-proto(16bit), ip-proto(8bit), vlantag1(16bit),
719  * Vlantag2(16bit) and sctp-tag(32bit).
720  */
721 static int
722 hns3_handle_tunnel(const struct rte_flow_item *item,
723                    struct hns3_fdir_rule *rule, struct rte_flow_error *error)
724 {
725         /* check eth config */
726         if (rule->input_set & (BIT(INNER_SRC_MAC) | BIT(INNER_DST_MAC)))
727                 return rte_flow_error_set(error, EINVAL,
728                                           RTE_FLOW_ERROR_TYPE_ITEM,
729                                           item, "Outer eth mac is unsupported");
730         if (rule->input_set & BIT(INNER_ETH_TYPE)) {
731                 hns3_set_bit(rule->input_set, OUTER_ETH_TYPE, 1);
732                 rule->key_conf.spec.outer_ether_type =
733                     rule->key_conf.spec.ether_type;
734                 rule->key_conf.mask.outer_ether_type =
735                     rule->key_conf.mask.ether_type;
736                 hns3_set_bit(rule->input_set, INNER_ETH_TYPE, 0);
737                 rule->key_conf.spec.ether_type = 0;
738                 rule->key_conf.mask.ether_type = 0;
739         }
740
741         /* check vlan config */
742         if (rule->input_set & (BIT(INNER_VLAN_TAG1) | BIT(INNER_VLAN_TAG2)))
743                 return rte_flow_error_set(error, EINVAL,
744                                           RTE_FLOW_ERROR_TYPE_ITEM,
745                                           item,
746                                           "Outer vlan tags is unsupported");
747
748         /* clear vlan_num for inner vlan select */
749         rule->key_conf.outer_vlan_num = rule->key_conf.vlan_num;
750         rule->key_conf.vlan_num = 0;
751
752         /* check L3 config */
753         if (rule->input_set &
754             (BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) | BIT(INNER_IP_TOS)))
755                 return rte_flow_error_set(error, EINVAL,
756                                           RTE_FLOW_ERROR_TYPE_ITEM,
757                                           item, "Outer ip is unsupported");
758         if (rule->input_set & BIT(INNER_IP_PROTO)) {
759                 hns3_set_bit(rule->input_set, OUTER_IP_PROTO, 1);
760                 rule->key_conf.spec.outer_proto = rule->key_conf.spec.ip_proto;
761                 rule->key_conf.mask.outer_proto = rule->key_conf.mask.ip_proto;
762                 hns3_set_bit(rule->input_set, INNER_IP_PROTO, 0);
763                 rule->key_conf.spec.ip_proto = 0;
764                 rule->key_conf.mask.ip_proto = 0;
765         }
766
767         /* check L4 config */
768         if (rule->input_set & BIT(INNER_SCTP_TAG))
769                 return rte_flow_error_set(error, EINVAL,
770                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
771                                           "Outer sctp tag is unsupported");
772
773         if (rule->input_set & BIT(INNER_SRC_PORT)) {
774                 hns3_set_bit(rule->input_set, OUTER_SRC_PORT, 1);
775                 rule->key_conf.spec.outer_src_port =
776                     rule->key_conf.spec.src_port;
777                 rule->key_conf.mask.outer_src_port =
778                     rule->key_conf.mask.src_port;
779                 hns3_set_bit(rule->input_set, INNER_SRC_PORT, 0);
780                 rule->key_conf.spec.src_port = 0;
781                 rule->key_conf.mask.src_port = 0;
782         }
783         if (rule->input_set & BIT(INNER_DST_PORT)) {
784                 hns3_set_bit(rule->input_set, INNER_DST_PORT, 0);
785                 rule->key_conf.spec.dst_port = 0;
786                 rule->key_conf.mask.dst_port = 0;
787         }
788         return 0;
789 }
790
791 static int
792 hns3_parse_vxlan(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
793                  struct rte_flow_error *error)
794 {
795         const struct rte_flow_item_vxlan *vxlan_spec;
796         const struct rte_flow_item_vxlan *vxlan_mask;
797
798         if (item->spec == NULL && item->mask)
799                 return rte_flow_error_set(error, EINVAL,
800                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
801                                           "Can't configure FDIR with mask but without spec");
802         else if (item->spec && (item->mask == NULL))
803                 return rte_flow_error_set(error, EINVAL,
804                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
805                                           "Tunnel packets must configure with mask");
806
807         hns3_set_bit(rule->input_set, OUTER_DST_PORT, 1);
808         rule->key_conf.mask.tunnel_type = TUNNEL_TYPE_MASK;
809         if (item->type == RTE_FLOW_ITEM_TYPE_VXLAN)
810                 rule->key_conf.spec.tunnel_type = HNS3_TUNNEL_TYPE_VXLAN;
811         else
812                 rule->key_conf.spec.tunnel_type = HNS3_TUNNEL_TYPE_VXLAN_GPE;
813
814         /* Only used to describe the protocol stack. */
815         if (item->spec == NULL && item->mask == NULL)
816                 return 0;
817
818         vxlan_mask = item->mask;
819         vxlan_spec = item->spec;
820
821         if (vxlan_mask->flags)
822                 return rte_flow_error_set(error, EINVAL,
823                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
824                                           "Flags is not supported in VxLAN");
825
826         /* VNI must be totally masked or not. */
827         if (memcmp(vxlan_mask->vni, full_mask, VNI_OR_TNI_LEN) &&
828             memcmp(vxlan_mask->vni, zero_mask, VNI_OR_TNI_LEN))
829                 return rte_flow_error_set(error, EINVAL,
830                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
831                                           "VNI must be totally masked or not in VxLAN");
832         if (vxlan_mask->vni[0]) {
833                 hns3_set_bit(rule->input_set, OUTER_TUN_VNI, 1);
834                 memcpy(rule->key_conf.mask.outer_tun_vni, vxlan_mask->vni,
835                            VNI_OR_TNI_LEN);
836         }
837         memcpy(rule->key_conf.spec.outer_tun_vni, vxlan_spec->vni,
838                    VNI_OR_TNI_LEN);
839         return 0;
840 }
841
842 static int
843 hns3_parse_nvgre(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
844                  struct rte_flow_error *error)
845 {
846         const struct rte_flow_item_nvgre *nvgre_spec;
847         const struct rte_flow_item_nvgre *nvgre_mask;
848
849         if (item->spec == NULL && item->mask)
850                 return rte_flow_error_set(error, EINVAL,
851                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
852                                           "Can't configure FDIR with mask but without spec");
853         else if (item->spec && (item->mask == NULL))
854                 return rte_flow_error_set(error, EINVAL,
855                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
856                                           "Tunnel packets must configure with mask");
857
858         hns3_set_bit(rule->input_set, OUTER_IP_PROTO, 1);
859         rule->key_conf.spec.outer_proto = IPPROTO_GRE;
860         rule->key_conf.mask.outer_proto = IPPROTO_MASK;
861
862         hns3_set_bit(rule->input_set, OUTER_DST_PORT, 1);
863         rule->key_conf.spec.tunnel_type = HNS3_TUNNEL_TYPE_NVGRE;
864         rule->key_conf.mask.tunnel_type = ~HNS3_TUNNEL_TYPE_NVGRE;
865         /* Only used to describe the protocol stack. */
866         if (item->spec == NULL && item->mask == NULL)
867                 return 0;
868
869         nvgre_mask = item->mask;
870         nvgre_spec = item->spec;
871
872         if (nvgre_mask->protocol || nvgre_mask->c_k_s_rsvd0_ver)
873                 return rte_flow_error_set(error, EINVAL,
874                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
875                                           "Ver/protocal is not supported in NVGRE");
876
877         /* TNI must be totally masked or not. */
878         if (memcmp(nvgre_mask->tni, full_mask, VNI_OR_TNI_LEN) &&
879             memcmp(nvgre_mask->tni, zero_mask, VNI_OR_TNI_LEN))
880                 return rte_flow_error_set(error, EINVAL,
881                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
882                                           "TNI must be totally masked or not in NVGRE");
883
884         if (nvgre_mask->tni[0]) {
885                 hns3_set_bit(rule->input_set, OUTER_TUN_VNI, 1);
886                 memcpy(rule->key_conf.mask.outer_tun_vni, nvgre_mask->tni,
887                            VNI_OR_TNI_LEN);
888         }
889         memcpy(rule->key_conf.spec.outer_tun_vni, nvgre_spec->tni,
890                    VNI_OR_TNI_LEN);
891
892         if (nvgre_mask->flow_id) {
893                 hns3_set_bit(rule->input_set, OUTER_TUN_FLOW_ID, 1);
894                 rule->key_conf.mask.outer_tun_flow_id = nvgre_mask->flow_id;
895         }
896         rule->key_conf.spec.outer_tun_flow_id = nvgre_spec->flow_id;
897         return 0;
898 }
899
900 static int
901 hns3_parse_geneve(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
902                   struct rte_flow_error *error)
903 {
904         const struct rte_flow_item_geneve *geneve_spec;
905         const struct rte_flow_item_geneve *geneve_mask;
906
907         if (item->spec == NULL && item->mask)
908                 return rte_flow_error_set(error, EINVAL,
909                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
910                                           "Can't configure FDIR with mask but without spec");
911         else if (item->spec && (item->mask == NULL))
912                 return rte_flow_error_set(error, EINVAL,
913                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
914                                           "Tunnel packets must configure with mask");
915
916         hns3_set_bit(rule->input_set, OUTER_DST_PORT, 1);
917         rule->key_conf.spec.tunnel_type = HNS3_TUNNEL_TYPE_GENEVE;
918         rule->key_conf.mask.tunnel_type = TUNNEL_TYPE_MASK;
919         /* Only used to describe the protocol stack. */
920         if (item->spec == NULL && item->mask == NULL)
921                 return 0;
922
923         geneve_mask = item->mask;
924         geneve_spec = item->spec;
925
926         if (geneve_mask->ver_opt_len_o_c_rsvd0 || geneve_mask->protocol)
927                 return rte_flow_error_set(error, EINVAL,
928                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
929                                           "Ver/protocal is not supported in GENEVE");
930         /* VNI must be totally masked or not. */
931         if (memcmp(geneve_mask->vni, full_mask, VNI_OR_TNI_LEN) &&
932             memcmp(geneve_mask->vni, zero_mask, VNI_OR_TNI_LEN))
933                 return rte_flow_error_set(error, EINVAL,
934                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
935                                           "VNI must be totally masked or not in GENEVE");
936         if (geneve_mask->vni[0]) {
937                 hns3_set_bit(rule->input_set, OUTER_TUN_VNI, 1);
938                 memcpy(rule->key_conf.mask.outer_tun_vni, geneve_mask->vni,
939                            VNI_OR_TNI_LEN);
940         }
941         memcpy(rule->key_conf.spec.outer_tun_vni, geneve_spec->vni,
942                    VNI_OR_TNI_LEN);
943         return 0;
944 }
945
946 static int
947 hns3_parse_tunnel(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
948                   struct rte_flow_error *error)
949 {
950         int ret;
951
952         switch (item->type) {
953         case RTE_FLOW_ITEM_TYPE_VXLAN:
954         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
955                 ret = hns3_parse_vxlan(item, rule, error);
956                 break;
957         case RTE_FLOW_ITEM_TYPE_NVGRE:
958                 ret = hns3_parse_nvgre(item, rule, error);
959                 break;
960         case RTE_FLOW_ITEM_TYPE_GENEVE:
961                 ret = hns3_parse_geneve(item, rule, error);
962                 break;
963         default:
964                 return rte_flow_error_set(error, ENOTSUP,
965                                           RTE_FLOW_ERROR_TYPE_HANDLE,
966                                           NULL, "Unsupported tunnel type!");
967         }
968         if (ret)
969                 return ret;
970         return hns3_handle_tunnel(item, rule, error);
971 }
972
973 static int
974 hns3_parse_normal(const struct rte_flow_item *item,
975                   struct hns3_fdir_rule *rule,
976                   struct items_step_mngr *step_mngr,
977                   struct rte_flow_error *error)
978 {
979         int ret;
980
981         switch (item->type) {
982         case RTE_FLOW_ITEM_TYPE_ETH:
983                 ret = hns3_parse_eth(item, rule, error);
984                 step_mngr->items = L2_next_items;
985                 step_mngr->count = ARRAY_SIZE(L2_next_items);
986                 break;
987         case RTE_FLOW_ITEM_TYPE_VLAN:
988                 ret = hns3_parse_vlan(item, rule, error);
989                 step_mngr->items = L2_next_items;
990                 step_mngr->count = ARRAY_SIZE(L2_next_items);
991                 break;
992         case RTE_FLOW_ITEM_TYPE_IPV4:
993                 ret = hns3_parse_ipv4(item, rule, error);
994                 step_mngr->items = L3_next_items;
995                 step_mngr->count = ARRAY_SIZE(L3_next_items);
996                 break;
997         case RTE_FLOW_ITEM_TYPE_IPV6:
998                 ret = hns3_parse_ipv6(item, rule, error);
999                 step_mngr->items = L3_next_items;
1000                 step_mngr->count = ARRAY_SIZE(L3_next_items);
1001                 break;
1002         case RTE_FLOW_ITEM_TYPE_TCP:
1003                 ret = hns3_parse_tcp(item, rule, error);
1004                 step_mngr->items = L4_next_items;
1005                 step_mngr->count = ARRAY_SIZE(L4_next_items);
1006                 break;
1007         case RTE_FLOW_ITEM_TYPE_UDP:
1008                 ret = hns3_parse_udp(item, rule, error);
1009                 step_mngr->items = L4_next_items;
1010                 step_mngr->count = ARRAY_SIZE(L4_next_items);
1011                 break;
1012         case RTE_FLOW_ITEM_TYPE_SCTP:
1013                 ret = hns3_parse_sctp(item, rule, error);
1014                 step_mngr->items = L4_next_items;
1015                 step_mngr->count = ARRAY_SIZE(L4_next_items);
1016                 break;
1017         default:
1018                 return rte_flow_error_set(error, ENOTSUP,
1019                                           RTE_FLOW_ERROR_TYPE_HANDLE,
1020                                           NULL, "Unsupported normal type!");
1021         }
1022
1023         return ret;
1024 }
1025
1026 static int
1027 hns3_validate_item(const struct rte_flow_item *item,
1028                    struct items_step_mngr step_mngr,
1029                    struct rte_flow_error *error)
1030 {
1031         int i;
1032
1033         if (item->last)
1034                 return rte_flow_error_set(error, ENOTSUP,
1035                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, item,
1036                                           "Not supported last point for range");
1037
1038         for (i = 0; i < step_mngr.count; i++) {
1039                 if (item->type == step_mngr.items[i])
1040                         break;
1041         }
1042
1043         if (i == step_mngr.count) {
1044                 return rte_flow_error_set(error, EINVAL,
1045                                           RTE_FLOW_ERROR_TYPE_ITEM,
1046                                           item, "Inval or missing item");
1047         }
1048         return 0;
1049 }
1050
1051 static inline bool
1052 is_tunnel_packet(enum rte_flow_item_type type)
1053 {
1054         if (type == RTE_FLOW_ITEM_TYPE_VXLAN_GPE ||
1055             type == RTE_FLOW_ITEM_TYPE_VXLAN ||
1056             type == RTE_FLOW_ITEM_TYPE_NVGRE ||
1057             type == RTE_FLOW_ITEM_TYPE_GENEVE ||
1058             type == RTE_FLOW_ITEM_TYPE_MPLS)
1059                 return true;
1060         return false;
1061 }
1062
1063 /*
1064  * Parse the rule to see if it is a IP or MAC VLAN flow director rule.
1065  * And get the flow director filter info BTW.
1066  * UDP/TCP/SCTP PATTERN:
1067  * The first not void item can be ETH or IPV4 or IPV6
1068  * The second not void item must be IPV4 or IPV6 if the first one is ETH.
1069  * The next not void item could be UDP or TCP or SCTP (optional)
1070  * The next not void item could be RAW (for flexbyte, optional)
1071  * The next not void item must be END.
1072  * A Fuzzy Match pattern can appear at any place before END.
1073  * Fuzzy Match is optional for IPV4 but is required for IPV6
1074  * MAC VLAN PATTERN:
1075  * The first not void item must be ETH.
1076  * The second not void item must be MAC VLAN.
1077  * The next not void item must be END.
1078  * ACTION:
1079  * The first not void action should be QUEUE or DROP.
1080  * The second not void optional action should be MARK,
1081  * mark_id is a uint32_t number.
1082  * The next not void action should be END.
1083  * UDP/TCP/SCTP pattern example:
1084  * ITEM         Spec                    Mask
1085  * ETH          NULL                    NULL
1086  * IPV4         src_addr 192.168.1.20   0xFFFFFFFF
1087  *              dst_addr 192.167.3.50   0xFFFFFFFF
1088  * UDP/TCP/SCTP src_port        80      0xFFFF
1089  *              dst_port        80      0xFFFF
1090  * END
1091  * MAC VLAN pattern example:
1092  * ITEM         Spec                    Mask
1093  * ETH          dst_addr
1094                 {0xAC, 0x7B, 0xA1,      {0xFF, 0xFF, 0xFF,
1095                 0x2C, 0x6D, 0x36}       0xFF, 0xFF, 0xFF}
1096  * MAC VLAN     tci     0x2016          0xEFFF
1097  * END
1098  * Other members in mask and spec should set to 0x00.
1099  * Item->last should be NULL.
1100  */
1101 static int
1102 hns3_parse_fdir_filter(struct rte_eth_dev *dev,
1103                        const struct rte_flow_item pattern[],
1104                        const struct rte_flow_action actions[],
1105                        struct hns3_fdir_rule *rule,
1106                        struct rte_flow_error *error)
1107 {
1108         struct hns3_adapter *hns = dev->data->dev_private;
1109         const struct rte_flow_item *item;
1110         struct items_step_mngr step_mngr;
1111         int ret;
1112
1113         /* FDIR is available only in PF driver */
1114         if (hns->is_vf)
1115                 return rte_flow_error_set(error, ENOTSUP,
1116                                           RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1117                                           "Fdir not supported in VF");
1118
1119         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT)
1120                 return rte_flow_error_set(error, ENOTSUP,
1121                                           RTE_FLOW_ERROR_TYPE_ITEM_NUM, NULL,
1122                                           "fdir_conf.mode isn't perfect");
1123
1124         step_mngr.items = first_items;
1125         step_mngr.count = ARRAY_SIZE(first_items);
1126         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1127                 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
1128                         continue;
1129
1130                 ret = hns3_validate_item(item, step_mngr, error);
1131                 if (ret)
1132                         return ret;
1133
1134                 if (is_tunnel_packet(item->type)) {
1135                         ret = hns3_parse_tunnel(item, rule, error);
1136                         if (ret)
1137                                 return ret;
1138                         step_mngr.items = tunnel_next_items;
1139                         step_mngr.count = ARRAY_SIZE(tunnel_next_items);
1140                 } else {
1141                         ret = hns3_parse_normal(item, rule, &step_mngr, error);
1142                         if (ret)
1143                                 return ret;
1144                 }
1145         }
1146
1147         return hns3_handle_actions(dev, actions, rule, error);
1148 }
1149
1150 void
1151 hns3_filterlist_init(struct rte_eth_dev *dev)
1152 {
1153         struct hns3_process_private *process_list = dev->process_private;
1154
1155         TAILQ_INIT(&process_list->fdir_list);
1156         TAILQ_INIT(&process_list->filter_rss_list);
1157         TAILQ_INIT(&process_list->flow_list);
1158 }
1159
1160 static void
1161 hns3_filterlist_flush(struct rte_eth_dev *dev)
1162 {
1163         struct hns3_process_private *process_list = dev->process_private;
1164         struct hns3_fdir_rule_ele *fdir_rule_ptr;
1165         struct hns3_rss_conf_ele *rss_filter_ptr;
1166         struct hns3_flow_mem *flow_node;
1167
1168         fdir_rule_ptr = TAILQ_FIRST(&process_list->fdir_list);
1169         while (fdir_rule_ptr) {
1170                 TAILQ_REMOVE(&process_list->fdir_list, fdir_rule_ptr, entries);
1171                 rte_free(fdir_rule_ptr);
1172                 fdir_rule_ptr = TAILQ_FIRST(&process_list->fdir_list);
1173         }
1174
1175         rss_filter_ptr = TAILQ_FIRST(&process_list->filter_rss_list);
1176         while (rss_filter_ptr) {
1177                 TAILQ_REMOVE(&process_list->filter_rss_list, rss_filter_ptr,
1178                              entries);
1179                 rte_free(rss_filter_ptr);
1180                 rss_filter_ptr = TAILQ_FIRST(&process_list->filter_rss_list);
1181         }
1182
1183         flow_node = TAILQ_FIRST(&process_list->flow_list);
1184         while (flow_node) {
1185                 TAILQ_REMOVE(&process_list->flow_list, flow_node, entries);
1186                 rte_free(flow_node->flow);
1187                 rte_free(flow_node);
1188                 flow_node = TAILQ_FIRST(&process_list->flow_list);
1189         }
1190 }
1191
1192 static bool
1193 hns3_action_rss_same(const struct rte_flow_action_rss *comp,
1194                      const struct rte_flow_action_rss *with)
1195 {
1196         return (comp->func == with->func &&
1197                 comp->level == with->level &&
1198                 comp->types == with->types &&
1199                 comp->key_len == with->key_len &&
1200                 comp->queue_num == with->queue_num &&
1201                 !memcmp(comp->key, with->key, with->key_len) &&
1202                 !memcmp(comp->queue, with->queue,
1203                         sizeof(*with->queue) * with->queue_num));
1204 }
1205
1206 static int
1207 hns3_rss_conf_copy(struct hns3_rss_conf *out,
1208                    const struct rte_flow_action_rss *in)
1209 {
1210         if (in->key_len > RTE_DIM(out->key) ||
1211             in->queue_num > RTE_DIM(out->queue))
1212                 return -EINVAL;
1213         if (in->key == NULL && in->key_len)
1214                 return -EINVAL;
1215         out->conf = (struct rte_flow_action_rss) {
1216                 .func = in->func,
1217                 .level = in->level,
1218                 .types = in->types,
1219                 .key_len = in->key_len,
1220                 .queue_num = in->queue_num,
1221         };
1222         out->conf.queue =
1223                 memcpy(out->queue, in->queue,
1224                        sizeof(*in->queue) * in->queue_num);
1225         if (in->key)
1226                 out->conf.key = memcpy(out->key, in->key, in->key_len);
1227
1228         return 0;
1229 }
1230
1231 /*
1232  * This function is used to parse rss action validatation.
1233  */
1234 static int
1235 hns3_parse_rss_filter(struct rte_eth_dev *dev,
1236                       const struct rte_flow_action *actions,
1237                       struct rte_flow_error *error)
1238 {
1239         struct hns3_adapter *hns = dev->data->dev_private;
1240         struct hns3_hw *hw = &hns->hw;
1241         struct hns3_rss_conf *rss_conf = &hw->rss_info;
1242         const struct rte_flow_action_rss *rss;
1243         const struct rte_flow_action *act;
1244         uint32_t act_index = 0;
1245         uint64_t flow_types;
1246         uint16_t n;
1247
1248         NEXT_ITEM_OF_ACTION(act, actions, act_index);
1249         /* Get configuration args from APP cmdline input */
1250         rss = act->conf;
1251
1252         if (rss == NULL || rss->queue_num == 0) {
1253                 return rte_flow_error_set(error, EINVAL,
1254                                           RTE_FLOW_ERROR_TYPE_ACTION,
1255                                           act, "no valid queues");
1256         }
1257
1258         for (n = 0; n < rss->queue_num; n++) {
1259                 if (rss->queue[n] < dev->data->nb_rx_queues)
1260                         continue;
1261                 return rte_flow_error_set(error, EINVAL,
1262                                           RTE_FLOW_ERROR_TYPE_ACTION,
1263                                           act,
1264                                           "queue id > max number of queues");
1265         }
1266
1267         /* Parse flow types of RSS */
1268         if (!(rss->types & HNS3_ETH_RSS_SUPPORT) && rss->types)
1269                 return rte_flow_error_set(error, EINVAL,
1270                                           RTE_FLOW_ERROR_TYPE_ACTION,
1271                                           act,
1272                                           "Flow types is unsupported by "
1273                                           "hns3's RSS");
1274
1275         flow_types = rss->types & HNS3_ETH_RSS_SUPPORT;
1276         if (flow_types != rss->types)
1277                 hns3_warn(hw, "RSS flow types(%" PRIx64 ") include unsupported "
1278                           "flow types", rss->types);
1279
1280         /* Parse RSS related parameters from RSS configuration */
1281         switch (rss->func) {
1282         case RTE_ETH_HASH_FUNCTION_DEFAULT:
1283         case RTE_ETH_HASH_FUNCTION_TOEPLITZ:
1284         case RTE_ETH_HASH_FUNCTION_SIMPLE_XOR:
1285                 break;
1286         default:
1287                 return rte_flow_error_set(error, ENOTSUP,
1288                                           RTE_FLOW_ERROR_TYPE_ACTION, act,
1289                                           "input RSS hash functions are not supported");
1290         }
1291
1292         if (rss->level)
1293                 return rte_flow_error_set(error, ENOTSUP,
1294                                           RTE_FLOW_ERROR_TYPE_ACTION, act,
1295                                           "a nonzero RSS encapsulation level is not supported");
1296         if (rss->key_len && rss->key_len != RTE_DIM(rss_conf->key))
1297                 return rte_flow_error_set(error, ENOTSUP,
1298                                           RTE_FLOW_ERROR_TYPE_ACTION, act,
1299                                           "RSS hash key must be exactly 40 bytes");
1300         if (rss->queue_num > RTE_DIM(rss_conf->queue))
1301                 return rte_flow_error_set(error, ENOTSUP,
1302                                           RTE_FLOW_ERROR_TYPE_ACTION, act,
1303                                           "too many queues for RSS context");
1304
1305         act_index++;
1306
1307         /* Check if the next not void action is END */
1308         NEXT_ITEM_OF_ACTION(act, actions, act_index);
1309         if (act->type != RTE_FLOW_ACTION_TYPE_END) {
1310                 memset(rss_conf, 0, sizeof(struct hns3_rss_conf));
1311                 return rte_flow_error_set(error, EINVAL,
1312                                           RTE_FLOW_ERROR_TYPE_ACTION,
1313                                           act, "Not supported action.");
1314         }
1315
1316         return 0;
1317 }
1318
1319 static int
1320 hns3_disable_rss(struct hns3_hw *hw)
1321 {
1322         int ret;
1323
1324         /* Redirected the redirection table to queue 0 */
1325         ret = hns3_rss_reset_indir_table(hw);
1326         if (ret)
1327                 return ret;
1328
1329         /* Disable RSS */
1330         hw->rss_info.conf.types = 0;
1331
1332         return 0;
1333 }
1334
1335 static void
1336 hns3_parse_rss_key(struct hns3_hw *hw, struct rte_flow_action_rss *rss_conf)
1337 {
1338         if (rss_conf->key == NULL ||
1339             rss_conf->key_len < HNS3_RSS_KEY_SIZE) {
1340                 hns3_info(hw, "Default RSS hash key to be set");
1341                 rss_conf->key = hns3_hash_key;
1342                 rss_conf->key_len = HNS3_RSS_KEY_SIZE;
1343         }
1344 }
1345
1346 static int
1347 hns3_parse_rss_algorithm(struct hns3_hw *hw, enum rte_eth_hash_function *func,
1348                          uint8_t *hash_algo)
1349 {
1350         enum rte_eth_hash_function algo_func = *func;
1351         switch (algo_func) {
1352         case RTE_ETH_HASH_FUNCTION_DEFAULT:
1353                 /* Keep *hash_algo as what it used to be */
1354                 algo_func = hw->rss_info.conf.func;
1355                 break;
1356         case RTE_ETH_HASH_FUNCTION_TOEPLITZ:
1357                 *hash_algo = HNS3_RSS_HASH_ALGO_TOEPLITZ;
1358                 break;
1359         case RTE_ETH_HASH_FUNCTION_SIMPLE_XOR:
1360                 *hash_algo = HNS3_RSS_HASH_ALGO_SIMPLE;
1361                 break;
1362         default:
1363                 hns3_err(hw, "Invalid RSS algorithm configuration(%u)",
1364                          algo_func);
1365                 return -EINVAL;
1366         }
1367         *func = algo_func;
1368
1369         return 0;
1370 }
1371
1372 static int
1373 hns3_hw_rss_hash_set(struct hns3_hw *hw, struct rte_flow_action_rss *rss_config)
1374 {
1375         uint8_t hash_algo =
1376                 (hw->rss_info.conf.func == RTE_ETH_HASH_FUNCTION_TOEPLITZ ?
1377                  HNS3_RSS_HASH_ALGO_TOEPLITZ : HNS3_RSS_HASH_ALGO_SIMPLE);
1378         struct hns3_rss_tuple_cfg *tuple;
1379         int ret;
1380
1381         /* Parse hash key */
1382         hns3_parse_rss_key(hw, rss_config);
1383
1384         /* Parse hash algorithm */
1385         ret = hns3_parse_rss_algorithm(hw, &rss_config->func, &hash_algo);
1386         if (ret)
1387                 return ret;
1388
1389         ret = hns3_set_rss_algo_key(hw, hash_algo, rss_config->key);
1390         if (ret)
1391                 return ret;
1392
1393         /* Update algorithm of hw */
1394         hw->rss_info.conf.func = rss_config->func;
1395
1396         /* Set flow type supported */
1397         tuple = &hw->rss_info.rss_tuple_sets;
1398         ret = hns3_set_rss_tuple_by_rss_hf(hw, tuple, rss_config->types);
1399         if (ret)
1400                 hns3_err(hw, "Update RSS tuples by rss hf failed %d", ret);
1401
1402         return ret;
1403 }
1404
1405 static int
1406 hns3_update_indir_table(struct rte_eth_dev *dev,
1407                         const struct rte_flow_action_rss *conf, uint16_t num)
1408 {
1409         struct hns3_adapter *hns = dev->data->dev_private;
1410         struct hns3_hw *hw = &hns->hw;
1411         uint8_t indir_tbl[HNS3_RSS_IND_TBL_SIZE];
1412         uint16_t j, allow_rss_queues;
1413         uint8_t queue_id;
1414         uint32_t i;
1415
1416         if (num == 0) {
1417                 hns3_err(hw, "No PF queues are configured to enable RSS");
1418                 return -ENOTSUP;
1419         }
1420
1421         allow_rss_queues = RTE_MIN(dev->data->nb_rx_queues, hw->rss_size_max);
1422         /* Fill in redirection table */
1423         memcpy(indir_tbl, hw->rss_info.rss_indirection_tbl,
1424                HNS3_RSS_IND_TBL_SIZE);
1425         for (i = 0, j = 0; i < HNS3_RSS_IND_TBL_SIZE; i++, j++) {
1426                 j %= num;
1427                 if (conf->queue[j] >= allow_rss_queues) {
1428                         hns3_err(hw, "Invalid queue id(%u) to be set in "
1429                                      "redirection table, max number of rss "
1430                                      "queues: %u", conf->queue[j],
1431                                  allow_rss_queues);
1432                         return -EINVAL;
1433                 }
1434                 queue_id = conf->queue[j];
1435                 indir_tbl[i] = queue_id;
1436         }
1437
1438         return hns3_set_rss_indir_table(hw, indir_tbl, HNS3_RSS_IND_TBL_SIZE);
1439 }
1440
1441 static int
1442 hns3_config_rss_filter(struct rte_eth_dev *dev,
1443                        const struct hns3_rss_conf *conf, bool add)
1444 {
1445         struct hns3_adapter *hns = dev->data->dev_private;
1446         struct hns3_hw *hw = &hns->hw;
1447         struct hns3_rss_conf *rss_info;
1448         uint64_t flow_types;
1449         uint16_t num;
1450         int ret;
1451
1452         struct rte_flow_action_rss rss_flow_conf = {
1453                 .func = conf->conf.func,
1454                 .level = conf->conf.level,
1455                 .types = conf->conf.types,
1456                 .key_len = conf->conf.key_len,
1457                 .queue_num = conf->conf.queue_num,
1458                 .key = conf->conf.key_len ?
1459                     (void *)(uintptr_t)conf->conf.key : NULL,
1460                 .queue = conf->conf.queue,
1461         };
1462
1463         /* The types is Unsupported by hns3' RSS */
1464         if (!(rss_flow_conf.types & HNS3_ETH_RSS_SUPPORT) &&
1465             rss_flow_conf.types) {
1466                 hns3_err(hw,
1467                          "Flow types(%" PRIx64 ") is unsupported by hns3's RSS",
1468                          rss_flow_conf.types);
1469                 return -EINVAL;
1470         }
1471
1472         /* Filter the unsupported flow types */
1473         flow_types = rss_flow_conf.types & HNS3_ETH_RSS_SUPPORT;
1474         if (flow_types != rss_flow_conf.types)
1475                 hns3_warn(hw, "modified RSS types based on hardware support, "
1476                               "requested:%" PRIx64 " configured:%" PRIx64,
1477                           rss_flow_conf.types, flow_types);
1478         /* Update the useful flow types */
1479         rss_flow_conf.types = flow_types;
1480
1481         if ((rss_flow_conf.types & ETH_RSS_PROTO_MASK) == 0)
1482                 return hns3_disable_rss(hw);
1483
1484         rss_info = &hw->rss_info;
1485         if (!add) {
1486                 if (hns3_action_rss_same(&rss_info->conf, &rss_flow_conf)) {
1487                         ret = hns3_disable_rss(hw);
1488                         if (ret) {
1489                                 hns3_err(hw, "RSS disable failed(%d)", ret);
1490                                 return ret;
1491                         }
1492                         memset(rss_info, 0, sizeof(struct hns3_rss_conf));
1493                         return 0;
1494                 }
1495                 return -EINVAL;
1496         }
1497
1498         /* Get rx queues num */
1499         num = dev->data->nb_rx_queues;
1500
1501         /* Set rx queues to use */
1502         num = RTE_MIN(num, rss_flow_conf.queue_num);
1503         if (rss_flow_conf.queue_num > num)
1504                 hns3_warn(hw, "Config queue numbers %u are beyond the scope of truncated",
1505                           rss_flow_conf.queue_num);
1506         hns3_info(hw, "Max of contiguous %u PF queues are configured", num);
1507
1508         rte_spinlock_lock(&hw->lock);
1509         /* Update redirection talbe of rss */
1510         ret = hns3_update_indir_table(dev, &rss_flow_conf, num);
1511         if (ret)
1512                 goto rss_config_err;
1513
1514         /* Set hash algorithm and flow types by the user's config */
1515         ret = hns3_hw_rss_hash_set(hw, &rss_flow_conf);
1516         if (ret)
1517                 goto rss_config_err;
1518
1519         ret = hns3_rss_conf_copy(rss_info, &rss_flow_conf);
1520         if (ret) {
1521                 hns3_err(hw, "RSS config init fail(%d)", ret);
1522                 goto rss_config_err;
1523         }
1524
1525 rss_config_err:
1526         rte_spinlock_unlock(&hw->lock);
1527
1528         return ret;
1529 }
1530
1531 /* Remove the rss filter */
1532 static int
1533 hns3_clear_rss_filter(struct rte_eth_dev *dev)
1534 {
1535         struct hns3_adapter *hns = dev->data->dev_private;
1536         struct hns3_hw *hw = &hns->hw;
1537
1538         if (hw->rss_info.conf.queue_num == 0)
1539                 return 0;
1540
1541         return hns3_config_rss_filter(dev, &hw->rss_info, false);
1542 }
1543
1544 static int
1545 hns3_flow_parse_rss(struct rte_eth_dev *dev,
1546                     const struct hns3_rss_conf *conf, bool add)
1547 {
1548         struct hns3_adapter *hns = dev->data->dev_private;
1549         struct hns3_hw *hw = &hns->hw;
1550         bool ret;
1551
1552         /* Action rss same */
1553         ret = hns3_action_rss_same(&hw->rss_info.conf, &conf->conf);
1554         if (ret) {
1555                 hns3_err(hw, "Enter duplicate RSS configuration : %d", ret);
1556                 return -EINVAL;
1557         }
1558
1559         return hns3_config_rss_filter(dev, conf, add);
1560 }
1561
1562 static int
1563 hns3_flow_args_check(const struct rte_flow_attr *attr,
1564                      const struct rte_flow_item pattern[],
1565                      const struct rte_flow_action actions[],
1566                      struct rte_flow_error *error)
1567 {
1568         if (pattern == NULL)
1569                 return rte_flow_error_set(error, EINVAL,
1570                                           RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1571                                           NULL, "NULL pattern.");
1572
1573         if (actions == NULL)
1574                 return rte_flow_error_set(error, EINVAL,
1575                                           RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1576                                           NULL, "NULL action.");
1577
1578         if (attr == NULL)
1579                 return rte_flow_error_set(error, EINVAL,
1580                                           RTE_FLOW_ERROR_TYPE_ATTR,
1581                                           NULL, "NULL attribute.");
1582
1583         return hns3_check_attr(attr, error);
1584 }
1585
1586 /*
1587  * Check if the flow rule is supported by hns3.
1588  * It only checkes the format. Don't guarantee the rule can be programmed into
1589  * the HW. Because there can be no enough room for the rule.
1590  */
1591 static int
1592 hns3_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
1593                    const struct rte_flow_item pattern[],
1594                    const struct rte_flow_action actions[],
1595                    struct rte_flow_error *error)
1596 {
1597         struct hns3_fdir_rule fdir_rule;
1598         int ret;
1599
1600         ret = hns3_flow_args_check(attr, pattern, actions, error);
1601         if (ret)
1602                 return ret;
1603
1604         if (find_rss_action(actions))
1605                 return hns3_parse_rss_filter(dev, actions, error);
1606
1607         memset(&fdir_rule, 0, sizeof(struct hns3_fdir_rule));
1608         return hns3_parse_fdir_filter(dev, pattern, actions, &fdir_rule, error);
1609 }
1610
1611 /*
1612  * Create or destroy a flow rule.
1613  * Theorically one rule can match more than one filters.
1614  * We will let it use the filter which it hitt first.
1615  * So, the sequence matters.
1616  */
1617 static struct rte_flow *
1618 hns3_flow_create(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
1619                  const struct rte_flow_item pattern[],
1620                  const struct rte_flow_action actions[],
1621                  struct rte_flow_error *error)
1622 {
1623         struct hns3_process_private *process_list = dev->process_private;
1624         struct hns3_adapter *hns = dev->data->dev_private;
1625         struct hns3_hw *hw = &hns->hw;
1626         const struct hns3_rss_conf *rss_conf;
1627         struct hns3_fdir_rule_ele *fdir_rule_ptr;
1628         struct hns3_rss_conf_ele *rss_filter_ptr;
1629         struct hns3_flow_mem *flow_node;
1630         const struct rte_flow_action *act;
1631         struct rte_flow *flow;
1632         struct hns3_fdir_rule fdir_rule;
1633         int ret;
1634
1635         ret = hns3_flow_args_check(attr, pattern, actions, error);
1636         if (ret)
1637                 return NULL;
1638
1639         flow = rte_zmalloc("hns3 flow", sizeof(struct rte_flow), 0);
1640         if (flow == NULL) {
1641                 rte_flow_error_set(error, ENOMEM,
1642                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1643                                    "Failed to allocate flow memory");
1644                 return NULL;
1645         }
1646         flow_node = rte_zmalloc("hns3 flow node",
1647                                 sizeof(struct hns3_flow_mem), 0);
1648         if (flow_node == NULL) {
1649                 rte_flow_error_set(error, ENOMEM,
1650                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1651                                    "Failed to allocate flow list memory");
1652                 rte_free(flow);
1653                 return NULL;
1654         }
1655
1656         flow_node->flow = flow;
1657         TAILQ_INSERT_TAIL(&process_list->flow_list, flow_node, entries);
1658
1659         act = find_rss_action(actions);
1660         if (act) {
1661                 rss_conf = act->conf;
1662
1663                 ret = hns3_flow_parse_rss(dev, rss_conf, true);
1664                 if (ret)
1665                         goto err;
1666
1667                 rss_filter_ptr = rte_zmalloc("hns3 rss filter",
1668                                              sizeof(struct hns3_rss_conf_ele),
1669                                              0);
1670                 if (rss_filter_ptr == NULL) {
1671                         hns3_err(hw,
1672                                     "Failed to allocate hns3_rss_filter memory");
1673                         ret = -ENOMEM;
1674                         goto err;
1675                 }
1676                 memcpy(&rss_filter_ptr->filter_info, rss_conf,
1677                         sizeof(struct hns3_rss_conf));
1678                 TAILQ_INSERT_TAIL(&process_list->filter_rss_list,
1679                                   rss_filter_ptr, entries);
1680
1681                 flow->rule = rss_filter_ptr;
1682                 flow->filter_type = RTE_ETH_FILTER_HASH;
1683                 return flow;
1684         }
1685
1686         memset(&fdir_rule, 0, sizeof(struct hns3_fdir_rule));
1687         ret = hns3_parse_fdir_filter(dev, pattern, actions, &fdir_rule, error);
1688         if (ret)
1689                 goto out;
1690
1691         if (fdir_rule.flags & HNS3_RULE_FLAG_COUNTER) {
1692                 ret = hns3_counter_new(dev, fdir_rule.act_cnt.shared,
1693                                        fdir_rule.act_cnt.id, error);
1694                 if (ret)
1695                         goto out;
1696
1697                 flow->counter_id = fdir_rule.act_cnt.id;
1698         }
1699         ret = hns3_fdir_filter_program(hns, &fdir_rule, false);
1700         if (!ret) {
1701                 fdir_rule_ptr = rte_zmalloc("hns3 fdir rule",
1702                                             sizeof(struct hns3_fdir_rule_ele),
1703                                             0);
1704                 if (fdir_rule_ptr == NULL) {
1705                         hns3_err(hw, "Failed to allocate fdir_rule memory");
1706                         ret = -ENOMEM;
1707                         goto err_fdir;
1708                 }
1709                 memcpy(&fdir_rule_ptr->fdir_conf, &fdir_rule,
1710                         sizeof(struct hns3_fdir_rule));
1711                 TAILQ_INSERT_TAIL(&process_list->fdir_list,
1712                                   fdir_rule_ptr, entries);
1713                 flow->rule = fdir_rule_ptr;
1714                 flow->filter_type = RTE_ETH_FILTER_FDIR;
1715
1716                 return flow;
1717         }
1718
1719 err_fdir:
1720         if (fdir_rule.flags & HNS3_RULE_FLAG_COUNTER)
1721                 hns3_counter_release(dev, fdir_rule.act_cnt.id);
1722
1723 err:
1724         rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1725                            "Failed to create flow");
1726 out:
1727         TAILQ_REMOVE(&process_list->flow_list, flow_node, entries);
1728         rte_free(flow_node);
1729         rte_free(flow);
1730         return NULL;
1731 }
1732
1733 /* Destroy a flow rule on hns3. */
1734 static int
1735 hns3_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1736                   struct rte_flow_error *error)
1737 {
1738         struct hns3_process_private *process_list = dev->process_private;
1739         struct hns3_adapter *hns = dev->data->dev_private;
1740         struct hns3_fdir_rule_ele *fdir_rule_ptr;
1741         struct hns3_rss_conf_ele *rss_filter_ptr;
1742         struct hns3_flow_mem *flow_node;
1743         struct hns3_hw *hw = &hns->hw;
1744         enum rte_filter_type filter_type;
1745         struct hns3_fdir_rule fdir_rule;
1746         int ret;
1747
1748         if (flow == NULL)
1749                 return rte_flow_error_set(error, EINVAL,
1750                                           RTE_FLOW_ERROR_TYPE_HANDLE,
1751                                           flow, "Flow is NULL");
1752         filter_type = flow->filter_type;
1753         switch (filter_type) {
1754         case RTE_ETH_FILTER_FDIR:
1755                 fdir_rule_ptr = (struct hns3_fdir_rule_ele *)flow->rule;
1756                 memcpy(&fdir_rule, &fdir_rule_ptr->fdir_conf,
1757                            sizeof(struct hns3_fdir_rule));
1758
1759                 ret = hns3_fdir_filter_program(hns, &fdir_rule, true);
1760                 if (ret)
1761                         return rte_flow_error_set(error, EIO,
1762                                                   RTE_FLOW_ERROR_TYPE_HANDLE,
1763                                                   flow,
1764                                                   "Destroy FDIR fail.Try again");
1765                 if (fdir_rule.flags & HNS3_RULE_FLAG_COUNTER)
1766                         hns3_counter_release(dev, fdir_rule.act_cnt.id);
1767                 TAILQ_REMOVE(&process_list->fdir_list, fdir_rule_ptr, entries);
1768                 rte_free(fdir_rule_ptr);
1769                 fdir_rule_ptr = NULL;
1770                 break;
1771         case RTE_ETH_FILTER_HASH:
1772                 rss_filter_ptr = (struct hns3_rss_conf_ele *)flow->rule;
1773                 ret = hns3_config_rss_filter(dev, &hw->rss_info, false);
1774                 if (ret)
1775                         return rte_flow_error_set(error, EIO,
1776                                                   RTE_FLOW_ERROR_TYPE_HANDLE,
1777                                                   flow,
1778                                                   "Destroy RSS fail.Try again");
1779                 TAILQ_REMOVE(&process_list->filter_rss_list, rss_filter_ptr,
1780                              entries);
1781                 rte_free(rss_filter_ptr);
1782                 rss_filter_ptr = NULL;
1783                 break;
1784         default:
1785                 return rte_flow_error_set(error, EINVAL,
1786                                           RTE_FLOW_ERROR_TYPE_HANDLE, flow,
1787                                           "Unsupported filter type");
1788         }
1789
1790         TAILQ_FOREACH(flow_node, &process_list->flow_list, entries) {
1791                 if (flow_node->flow == flow) {
1792                         TAILQ_REMOVE(&process_list->flow_list, flow_node,
1793                                      entries);
1794                         rte_free(flow_node);
1795                         flow_node = NULL;
1796                         break;
1797                 }
1798         }
1799         rte_free(flow);
1800         flow = NULL;
1801
1802         return 0;
1803 }
1804
1805 /*  Destroy all flow rules associated with a port on hns3. */
1806 static int
1807 hns3_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1808 {
1809         struct hns3_adapter *hns = dev->data->dev_private;
1810         int ret;
1811
1812         /* FDIR is available only in PF driver */
1813         if (!hns->is_vf) {
1814                 ret = hns3_clear_all_fdir_filter(hns);
1815                 if (ret) {
1816                         rte_flow_error_set(error, ret,
1817                                            RTE_FLOW_ERROR_TYPE_HANDLE,
1818                                            NULL, "Failed to flush rule");
1819                         return ret;
1820                 }
1821                 hns3_counter_flush(dev);
1822         }
1823
1824         ret = hns3_clear_rss_filter(dev);
1825         if (ret)
1826                 return ret;
1827
1828         hns3_filterlist_flush(dev);
1829
1830         return 0;
1831 }
1832
1833 /* Query an existing flow rule. */
1834 static int
1835 hns3_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
1836                 const struct rte_flow_action *actions, void *data,
1837                 struct rte_flow_error *error)
1838 {
1839         struct rte_flow_query_count *qc;
1840         int ret;
1841
1842         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1843                 switch (actions->type) {
1844                 case RTE_FLOW_ACTION_TYPE_VOID:
1845                         break;
1846                 case RTE_FLOW_ACTION_TYPE_COUNT:
1847                         qc = (struct rte_flow_query_count *)data;
1848                         ret = hns3_counter_query(dev, flow, qc, error);
1849                         if (ret)
1850                                 return ret;
1851                         break;
1852                 default:
1853                         return rte_flow_error_set(error, ENOTSUP,
1854                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1855                                                   actions,
1856                                                   "Query action only support count");
1857                 }
1858         }
1859         return 0;
1860 }
1861
1862 static const struct rte_flow_ops hns3_flow_ops = {
1863         .validate = hns3_flow_validate,
1864         .create = hns3_flow_create,
1865         .destroy = hns3_flow_destroy,
1866         .flush = hns3_flow_flush,
1867         .query = hns3_flow_query,
1868         .isolate = NULL,
1869 };
1870
1871 /*
1872  * The entry of flow API.
1873  * @param dev
1874  *   Pointer to Ethernet device.
1875  * @return
1876  *   0 on success, a negative errno value otherwise is set.
1877  */
1878 int
1879 hns3_dev_filter_ctrl(struct rte_eth_dev *dev, enum rte_filter_type filter_type,
1880                      enum rte_filter_op filter_op, void *arg)
1881 {
1882         struct hns3_hw *hw;
1883         int ret = 0;
1884
1885         if (dev == NULL)
1886                 return -EINVAL;
1887         hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1888         switch (filter_type) {
1889         case RTE_ETH_FILTER_GENERIC:
1890                 if (filter_op != RTE_ETH_FILTER_GET)
1891                         return -EINVAL;
1892                 if (hw->adapter_state >= HNS3_NIC_CLOSED)
1893                         return -ENODEV;
1894                 *(const void **)arg = &hns3_flow_ops;
1895                 break;
1896         default:
1897                 hns3_err(hw, "Filter type (%d) not supported", filter_type);
1898                 ret = -EOPNOTSUPP;
1899                 break;
1900         }
1901
1902         return ret;
1903 }