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