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