ethdev: do not rely on detachable flag in detach
[dpdk.git] / lib / librte_flow_classify / rte_flow_classify.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_flow_classify.h>
35 #include "rte_flow_classify_parse.h"
36 #include <rte_flow_driver.h>
37 #include <rte_table_acl.h>
38 #include <stdbool.h>
39
40 int librte_flow_classify_logtype;
41
42 static struct rte_eth_ntuple_filter ntuple_filter;
43 static uint32_t unique_id = 1;
44
45
46 struct rte_flow_classify_table_entry {
47         /* meta-data for classify rule */
48         uint32_t rule_id;
49 };
50
51 struct rte_table {
52         /* Input parameters */
53         struct rte_table_ops ops;
54         uint32_t entry_size;
55         enum rte_flow_classify_table_type type;
56
57         /* Handle to the low-level table object */
58         void *h_table;
59 };
60
61 #define RTE_FLOW_CLASSIFIER_MAX_NAME_SZ 256
62
63 struct rte_flow_classifier {
64         /* Input parameters */
65         char name[RTE_FLOW_CLASSIFIER_MAX_NAME_SZ];
66         int socket_id;
67         enum rte_flow_classify_table_type type;
68
69         /* Internal tables */
70         struct rte_table tables[RTE_FLOW_CLASSIFY_TABLE_MAX];
71         uint32_t num_tables;
72         uint16_t nb_pkts;
73         struct rte_flow_classify_table_entry
74                 *entries[RTE_PORT_IN_BURST_SIZE_MAX];
75 } __rte_cache_aligned;
76
77 enum {
78         PROTO_FIELD_IPV4,
79         SRC_FIELD_IPV4,
80         DST_FIELD_IPV4,
81         SRCP_FIELD_IPV4,
82         DSTP_FIELD_IPV4,
83         NUM_FIELDS_IPV4
84 };
85
86 struct acl_keys {
87         struct rte_table_acl_rule_add_params key_add; /* add key */
88         struct rte_table_acl_rule_delete_params key_del; /* delete key */
89 };
90
91 struct classify_rules {
92         enum rte_flow_classify_rule_type type;
93         union {
94                 struct rte_flow_classify_ipv4_5tuple ipv4_5tuple;
95         } u;
96 };
97
98 struct rte_flow_classify_rule {
99         uint32_t id; /* unique ID of classify rule */
100         struct rte_flow_action action; /* action when match found */
101         struct classify_rules rules; /* union of rules */
102         union {
103                 struct acl_keys key;
104         } u;
105         int key_found;   /* rule key found in table */
106         void *entry;     /* pointer to buffer to hold rule meta data */
107         void *entry_ptr; /* handle to the table entry for rule meta data */
108 };
109
110 static int
111 flow_classify_parse_flow(
112                    const struct rte_flow_attr *attr,
113                    const struct rte_flow_item pattern[],
114                    const struct rte_flow_action actions[],
115                    struct rte_flow_error *error)
116 {
117         struct rte_flow_item *items;
118         parse_filter_t parse_filter;
119         uint32_t item_num = 0;
120         uint32_t i = 0;
121         int ret;
122
123         memset(&ntuple_filter, 0, sizeof(ntuple_filter));
124
125         /* Get the non-void item number of pattern */
126         while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
127                 if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
128                         item_num++;
129                 i++;
130         }
131         item_num++;
132
133         items = malloc(item_num * sizeof(struct rte_flow_item));
134         if (!items) {
135                 rte_flow_error_set(error, ENOMEM,
136                                 RTE_FLOW_ERROR_TYPE_ITEM_NUM,
137                                 NULL, "No memory for pattern items.");
138                 return -ENOMEM;
139         }
140
141         memset(items, 0, item_num * sizeof(struct rte_flow_item));
142         classify_pattern_skip_void_item(items, pattern);
143
144         parse_filter = classify_find_parse_filter_func(items);
145         if (!parse_filter) {
146                 rte_flow_error_set(error, EINVAL,
147                                 RTE_FLOW_ERROR_TYPE_ITEM,
148                                 pattern, "Unsupported pattern");
149                 free(items);
150                 return -EINVAL;
151         }
152
153         ret = parse_filter(attr, items, actions, &ntuple_filter, error);
154         free(items);
155         return ret;
156 }
157
158 #ifdef RTE_LIBRTE_CLASSIFY_DEBUG
159 #define uint32_t_to_char(ip, a, b, c, d) do {\
160                 *a = (unsigned char)(ip >> 24 & 0xff);\
161                 *b = (unsigned char)(ip >> 16 & 0xff);\
162                 *c = (unsigned char)(ip >> 8 & 0xff);\
163                 *d = (unsigned char)(ip & 0xff);\
164         } while (0)
165
166 static inline void
167 print_acl_ipv4_key_add(struct rte_table_acl_rule_add_params *key)
168 {
169         unsigned char a, b, c, d;
170
171         printf("%s:    0x%02hhx/0x%hhx ", __func__,
172                 key->field_value[PROTO_FIELD_IPV4].value.u8,
173                 key->field_value[PROTO_FIELD_IPV4].mask_range.u8);
174
175         uint32_t_to_char(key->field_value[SRC_FIELD_IPV4].value.u32,
176                         &a, &b, &c, &d);
177         printf(" %hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
178                         key->field_value[SRC_FIELD_IPV4].mask_range.u32);
179
180         uint32_t_to_char(key->field_value[DST_FIELD_IPV4].value.u32,
181                         &a, &b, &c, &d);
182         printf("%hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
183                         key->field_value[DST_FIELD_IPV4].mask_range.u32);
184
185         printf("%hu : 0x%x %hu : 0x%x",
186                 key->field_value[SRCP_FIELD_IPV4].value.u16,
187                 key->field_value[SRCP_FIELD_IPV4].mask_range.u16,
188                 key->field_value[DSTP_FIELD_IPV4].value.u16,
189                 key->field_value[DSTP_FIELD_IPV4].mask_range.u16);
190
191         printf(" priority: 0x%x\n", key->priority);
192 }
193
194 static inline void
195 print_acl_ipv4_key_delete(struct rte_table_acl_rule_delete_params *key)
196 {
197         unsigned char a, b, c, d;
198
199         printf("%s: 0x%02hhx/0x%hhx ", __func__,
200                 key->field_value[PROTO_FIELD_IPV4].value.u8,
201                 key->field_value[PROTO_FIELD_IPV4].mask_range.u8);
202
203         uint32_t_to_char(key->field_value[SRC_FIELD_IPV4].value.u32,
204                         &a, &b, &c, &d);
205         printf(" %hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
206                         key->field_value[SRC_FIELD_IPV4].mask_range.u32);
207
208         uint32_t_to_char(key->field_value[DST_FIELD_IPV4].value.u32,
209                         &a, &b, &c, &d);
210         printf("%hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
211                         key->field_value[DST_FIELD_IPV4].mask_range.u32);
212
213         printf("%hu : 0x%x %hu : 0x%x\n",
214                 key->field_value[SRCP_FIELD_IPV4].value.u16,
215                 key->field_value[SRCP_FIELD_IPV4].mask_range.u16,
216                 key->field_value[DSTP_FIELD_IPV4].value.u16,
217                 key->field_value[DSTP_FIELD_IPV4].mask_range.u16);
218 }
219 #endif
220
221 static int
222 rte_flow_classifier_check_params(struct rte_flow_classifier_params *params)
223 {
224         if (params == NULL) {
225                 RTE_FLOW_CLASSIFY_LOG(ERR,
226                         "%s: Incorrect value for parameter params\n", __func__);
227                 return -EINVAL;
228         }
229
230         /* name */
231         if (params->name == NULL) {
232                 RTE_FLOW_CLASSIFY_LOG(ERR,
233                         "%s: Incorrect value for parameter name\n", __func__);
234                 return -EINVAL;
235         }
236
237         /* socket */
238         if ((params->socket_id < 0) ||
239             (params->socket_id >= RTE_MAX_NUMA_NODES)) {
240                 RTE_FLOW_CLASSIFY_LOG(ERR,
241                         "%s: Incorrect value for parameter socket_id\n",
242                         __func__);
243                 return -EINVAL;
244         }
245
246         return 0;
247 }
248
249 struct rte_flow_classifier *
250 rte_flow_classifier_create(struct rte_flow_classifier_params *params)
251 {
252         struct rte_flow_classifier *cls;
253         int ret;
254
255         /* Check input parameters */
256         ret = rte_flow_classifier_check_params(params);
257         if (ret != 0) {
258                 RTE_FLOW_CLASSIFY_LOG(ERR,
259                         "%s: flow classifier params check failed (%d)\n",
260                         __func__, ret);
261                 return NULL;
262         }
263
264         /* Allocate memory for the flow classifier */
265         cls = rte_zmalloc_socket("FLOW_CLASSIFIER",
266                         sizeof(struct rte_flow_classifier),
267                         RTE_CACHE_LINE_SIZE, params->socket_id);
268
269         if (cls == NULL) {
270                 RTE_FLOW_CLASSIFY_LOG(ERR,
271                         "%s: flow classifier memory allocation failed\n",
272                         __func__);
273                 return NULL;
274         }
275
276         /* Save input parameters */
277         snprintf(cls->name, RTE_FLOW_CLASSIFIER_MAX_NAME_SZ, "%s",
278                         params->name);
279         cls->socket_id = params->socket_id;
280         cls->type = params->type;
281
282         /* Initialize flow classifier internal data structure */
283         cls->num_tables = 0;
284
285         return cls;
286 }
287
288 static void
289 rte_flow_classify_table_free(struct rte_table *table)
290 {
291         if (table->ops.f_free != NULL)
292                 table->ops.f_free(table->h_table);
293 }
294
295 int
296 rte_flow_classifier_free(struct rte_flow_classifier *cls)
297 {
298         uint32_t i;
299
300         /* Check input parameters */
301         if (cls == NULL) {
302                 RTE_FLOW_CLASSIFY_LOG(ERR,
303                         "%s: rte_flow_classifier parameter is NULL\n",
304                         __func__);
305                 return -EINVAL;
306         }
307
308         /* Free tables */
309         for (i = 0; i < cls->num_tables; i++) {
310                 struct rte_table *table = &cls->tables[i];
311
312                 rte_flow_classify_table_free(table);
313         }
314
315         /* Free flow classifier memory */
316         rte_free(cls);
317
318         return 0;
319 }
320
321 static int
322 rte_table_check_params(struct rte_flow_classifier *cls,
323                 struct rte_flow_classify_table_params *params,
324                 uint32_t *table_id)
325 {
326         if (cls == NULL) {
327                 RTE_FLOW_CLASSIFY_LOG(ERR,
328                         "%s: flow classifier parameter is NULL\n",
329                         __func__);
330                 return -EINVAL;
331         }
332         if (params == NULL) {
333                 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: params parameter is NULL\n",
334                         __func__);
335                 return -EINVAL;
336         }
337         if (table_id == NULL) {
338                 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: table_id parameter is NULL\n",
339                         __func__);
340                 return -EINVAL;
341         }
342
343         /* ops */
344         if (params->ops == NULL) {
345                 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: params->ops is NULL\n",
346                         __func__);
347                 return -EINVAL;
348         }
349
350         if (params->ops->f_create == NULL) {
351                 RTE_FLOW_CLASSIFY_LOG(ERR,
352                         "%s: f_create function pointer is NULL\n", __func__);
353                 return -EINVAL;
354         }
355
356         if (params->ops->f_lookup == NULL) {
357                 RTE_FLOW_CLASSIFY_LOG(ERR,
358                         "%s: f_lookup function pointer is NULL\n", __func__);
359                 return -EINVAL;
360         }
361
362         /* De we have room for one more table? */
363         if (cls->num_tables == RTE_FLOW_CLASSIFY_TABLE_MAX) {
364                 RTE_FLOW_CLASSIFY_LOG(ERR,
365                         "%s: Incorrect value for num_tables parameter\n",
366                         __func__);
367                 return -EINVAL;
368         }
369
370         return 0;
371 }
372
373 int
374 rte_flow_classify_table_create(struct rte_flow_classifier *cls,
375         struct rte_flow_classify_table_params *params,
376         uint32_t *table_id)
377 {
378         struct rte_table *table;
379         void *h_table;
380         uint32_t entry_size, id;
381         int ret;
382
383         /* Check input arguments */
384         ret = rte_table_check_params(cls, params, table_id);
385         if (ret != 0)
386                 return ret;
387
388         id = cls->num_tables;
389         table = &cls->tables[id];
390
391         /* calculate table entry size */
392         entry_size = sizeof(struct rte_flow_classify_table_entry);
393
394         /* Create the table */
395         h_table = params->ops->f_create(params->arg_create, cls->socket_id,
396                 entry_size);
397         if (h_table == NULL) {
398                 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: Table creation failed\n",
399                         __func__);
400                 return -EINVAL;
401         }
402
403         /* Commit current table to the classifier */
404         cls->num_tables++;
405         *table_id = id;
406
407         /* Save input parameters */
408         memcpy(&table->ops, params->ops, sizeof(struct rte_table_ops));
409
410         /* Initialize table internal data structure */
411         table->entry_size = entry_size;
412         table->h_table = h_table;
413
414         return 0;
415 }
416
417 static struct rte_flow_classify_rule *
418 allocate_acl_ipv4_5tuple_rule(void)
419 {
420         struct rte_flow_classify_rule *rule;
421
422         rule = malloc(sizeof(struct rte_flow_classify_rule));
423         if (!rule)
424                 return rule;
425
426         memset(rule, 0, sizeof(struct rte_flow_classify_rule));
427         rule->id = unique_id++;
428         rule->rules.type = RTE_FLOW_CLASSIFY_RULE_TYPE_IPV4_5TUPLE;
429
430         memcpy(&rule->action, classify_get_flow_action(),
431                sizeof(struct rte_flow_action));
432
433         /* key add values */
434         rule->u.key.key_add.priority = ntuple_filter.priority;
435         rule->u.key.key_add.field_value[PROTO_FIELD_IPV4].mask_range.u8 =
436                         ntuple_filter.proto_mask;
437         rule->u.key.key_add.field_value[PROTO_FIELD_IPV4].value.u8 =
438                         ntuple_filter.proto;
439         rule->rules.u.ipv4_5tuple.proto = ntuple_filter.proto;
440         rule->rules.u.ipv4_5tuple.proto_mask = ntuple_filter.proto_mask;
441
442         rule->u.key.key_add.field_value[SRC_FIELD_IPV4].mask_range.u32 =
443                         ntuple_filter.src_ip_mask;
444         rule->u.key.key_add.field_value[SRC_FIELD_IPV4].value.u32 =
445                         ntuple_filter.src_ip;
446         rule->rules.u.ipv4_5tuple.src_ip_mask = ntuple_filter.src_ip_mask;
447         rule->rules.u.ipv4_5tuple.src_ip = ntuple_filter.src_ip;
448
449         rule->u.key.key_add.field_value[DST_FIELD_IPV4].mask_range.u32 =
450                         ntuple_filter.dst_ip_mask;
451         rule->u.key.key_add.field_value[DST_FIELD_IPV4].value.u32 =
452                         ntuple_filter.dst_ip;
453         rule->rules.u.ipv4_5tuple.dst_ip_mask = ntuple_filter.dst_ip_mask;
454         rule->rules.u.ipv4_5tuple.dst_ip = ntuple_filter.dst_ip;
455
456         rule->u.key.key_add.field_value[SRCP_FIELD_IPV4].mask_range.u16 =
457                         ntuple_filter.src_port_mask;
458         rule->u.key.key_add.field_value[SRCP_FIELD_IPV4].value.u16 =
459                         ntuple_filter.src_port;
460         rule->rules.u.ipv4_5tuple.src_port_mask = ntuple_filter.src_port_mask;
461         rule->rules.u.ipv4_5tuple.src_port = ntuple_filter.src_port;
462
463         rule->u.key.key_add.field_value[DSTP_FIELD_IPV4].mask_range.u16 =
464                         ntuple_filter.dst_port_mask;
465         rule->u.key.key_add.field_value[DSTP_FIELD_IPV4].value.u16 =
466                         ntuple_filter.dst_port;
467         rule->rules.u.ipv4_5tuple.dst_port_mask = ntuple_filter.dst_port_mask;
468         rule->rules.u.ipv4_5tuple.dst_port = ntuple_filter.dst_port;
469
470 #ifdef RTE_LIBRTE_CLASSIFY_DEBUG
471         print_acl_ipv4_key_add(&rule->u.key.key_add);
472 #endif
473
474         /* key delete values */
475         memcpy(&rule->u.key.key_del.field_value[PROTO_FIELD_IPV4],
476                &rule->u.key.key_add.field_value[PROTO_FIELD_IPV4],
477                NUM_FIELDS_IPV4 * sizeof(struct rte_acl_field));
478
479 #ifdef RTE_LIBRTE_CLASSIFY_DEBUG
480         print_acl_ipv4_key_delete(&rule->u.key.key_del);
481 #endif
482         return rule;
483 }
484
485 struct rte_flow_classify_rule *
486 rte_flow_classify_table_entry_add(struct rte_flow_classifier *cls,
487                 uint32_t table_id,
488                 int *key_found,
489                 const struct rte_flow_attr *attr,
490                 const struct rte_flow_item pattern[],
491                 const struct rte_flow_action actions[],
492                 struct rte_flow_error *error)
493 {
494         struct rte_flow_classify_rule *rule;
495         struct rte_flow_classify_table_entry *table_entry;
496         int ret;
497
498         if (!error)
499                 return NULL;
500
501         if (!cls) {
502                 rte_flow_error_set(error, EINVAL,
503                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
504                                 NULL, "NULL classifier.");
505                 return NULL;
506         }
507
508         if (table_id >= cls->num_tables) {
509                 rte_flow_error_set(error, EINVAL,
510                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
511                                 NULL, "invalid table_id.");
512                 return NULL;
513         }
514
515         if (key_found == NULL) {
516                 rte_flow_error_set(error, EINVAL,
517                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
518                                 NULL, "NULL key_found.");
519                 return NULL;
520         }
521
522         if (!pattern) {
523                 rte_flow_error_set(error, EINVAL,
524                                 RTE_FLOW_ERROR_TYPE_ITEM_NUM,
525                                 NULL, "NULL pattern.");
526                 return NULL;
527         }
528
529         if (!actions) {
530                 rte_flow_error_set(error, EINVAL,
531                                 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
532                                 NULL, "NULL action.");
533                 return NULL;
534         }
535
536         if (!attr) {
537                 rte_flow_error_set(error, EINVAL,
538                                 RTE_FLOW_ERROR_TYPE_ATTR,
539                                 NULL, "NULL attribute.");
540                 return NULL;
541         }
542
543         /* parse attr, pattern and actions */
544         ret = flow_classify_parse_flow(attr, pattern, actions, error);
545         if (ret < 0)
546                 return NULL;
547
548         switch (cls->type) {
549         case RTE_FLOW_CLASSIFY_TABLE_TYPE_ACL:
550                 rule = allocate_acl_ipv4_5tuple_rule();
551                 if (!rule)
552                         return NULL;
553                 break;
554         default:
555                 return NULL;
556         }
557
558         rule->entry = malloc(sizeof(struct rte_flow_classify_table_entry));
559         if (!rule->entry) {
560                 free(rule);
561                 return NULL;
562         }
563
564         table_entry = rule->entry;
565         table_entry->rule_id = rule->id;
566
567         if (cls->tables[table_id].ops.f_add != NULL) {
568                 ret = cls->tables[table_id].ops.f_add(
569                         cls->tables[table_id].h_table,
570                         &rule->u.key.key_add,
571                         rule->entry,
572                         &rule->key_found,
573                         &rule->entry_ptr);
574                 if (ret) {
575                         free(rule->entry);
576                         free(rule);
577                         return NULL;
578                 }
579                 *key_found = rule->key_found;
580         }
581         return rule;
582 }
583
584 int
585 rte_flow_classify_table_entry_delete(struct rte_flow_classifier *cls,
586                 uint32_t table_id,
587                 struct rte_flow_classify_rule *rule)
588 {
589         int ret = -EINVAL;
590
591         if (!cls || !rule || table_id >= cls->num_tables)
592                 return ret;
593
594         if (cls->tables[table_id].ops.f_delete != NULL)
595                 ret = cls->tables[table_id].ops.f_delete(
596                         cls->tables[table_id].h_table,
597                         &rule->u.key.key_del,
598                         &rule->key_found,
599                         &rule->entry);
600
601         return ret;
602 }
603
604 static int
605 flow_classifier_lookup(struct rte_flow_classifier *cls,
606                 uint32_t table_id,
607                 struct rte_mbuf **pkts,
608                 const uint16_t nb_pkts)
609 {
610         int ret = -EINVAL;
611         uint64_t pkts_mask;
612         uint64_t lookup_hit_mask;
613
614         pkts_mask = RTE_LEN2MASK(nb_pkts, uint64_t);
615         ret = cls->tables[table_id].ops.f_lookup(
616                 cls->tables[table_id].h_table,
617                 pkts, pkts_mask, &lookup_hit_mask,
618                 (void **)cls->entries);
619
620         if (!ret && lookup_hit_mask)
621                 cls->nb_pkts = nb_pkts;
622         else
623                 cls->nb_pkts = 0;
624
625         return ret;
626 }
627
628 static int
629 action_apply(struct rte_flow_classifier *cls,
630                 struct rte_flow_classify_rule *rule,
631                 struct rte_flow_classify_stats *stats)
632 {
633         struct rte_flow_classify_ipv4_5tuple_stats *ntuple_stats;
634         uint64_t count = 0;
635         int i;
636         int ret = -EINVAL;
637
638         switch (rule->action.type) {
639         case RTE_FLOW_ACTION_TYPE_COUNT:
640                 for (i = 0; i < cls->nb_pkts; i++) {
641                         if (rule->id == cls->entries[i]->rule_id)
642                                 count++;
643                 }
644                 if (count) {
645                         ret = 0;
646                         ntuple_stats =
647                                 (struct rte_flow_classify_ipv4_5tuple_stats *)
648                                 stats->stats;
649                         ntuple_stats->counter1 = count;
650                         ntuple_stats->ipv4_5tuple = rule->rules.u.ipv4_5tuple;
651                 }
652                 break;
653         default:
654                 ret = -ENOTSUP;
655                 break;
656         }
657
658         return ret;
659 }
660
661 int
662 rte_flow_classifier_query(struct rte_flow_classifier *cls,
663                 uint32_t table_id,
664                 struct rte_mbuf **pkts,
665                 const uint16_t nb_pkts,
666                 struct rte_flow_classify_rule *rule,
667                 struct rte_flow_classify_stats *stats)
668 {
669         int ret = -EINVAL;
670
671         if (!cls || !rule || !stats || !pkts  || nb_pkts == 0 ||
672                 table_id >= cls->num_tables)
673                 return ret;
674
675         ret = flow_classifier_lookup(cls, table_id, pkts, nb_pkts);
676         if (!ret)
677                 ret = action_apply(cls, rule, stats);
678         return ret;
679 }
680
681 RTE_INIT(librte_flow_classify_init_log);
682
683 static void
684 librte_flow_classify_init_log(void)
685 {
686         librte_flow_classify_logtype =
687                 rte_log_register("librte.flow_classify");
688         if (librte_flow_classify_logtype >= 0)
689                 rte_log_set_level(librte_flow_classify_logtype, RTE_LOG_DEBUG);
690 }