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