eal: add uuid API
[dpdk.git] / drivers / net / softnic / rte_eth_softnic_swq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <rte_string_fns.h>
9
10 #include "rte_eth_softnic_internals.h"
11
12 int
13 softnic_swq_init(struct pmd_internals *p)
14 {
15         TAILQ_INIT(&p->swq_list);
16
17         return 0;
18 }
19
20 void
21 softnic_swq_free(struct pmd_internals *p)
22 {
23         for ( ; ; ) {
24                 struct softnic_swq *swq;
25
26                 swq = TAILQ_FIRST(&p->swq_list);
27                 if (swq == NULL)
28                         break;
29
30                 TAILQ_REMOVE(&p->swq_list, swq, node);
31                 rte_ring_free(swq->r);
32                 free(swq);
33         }
34 }
35
36 void
37 softnic_softnic_swq_free_keep_rxq_txq(struct pmd_internals *p)
38 {
39         struct softnic_swq *swq;
40
41         TAILQ_FOREACH(swq, &p->swq_list, node) {
42                 if ((strncmp(swq->name, "RXQ", strlen("RXQ")) == 0) ||
43                         (strncmp(swq->name, "TXQ", strlen("TXQ")) == 0))
44                         continue;
45
46                 TAILQ_REMOVE(&p->swq_list, swq, node);
47                 rte_ring_free(swq->r);
48                 free(swq);
49         }
50 }
51
52 struct softnic_swq *
53 softnic_swq_find(struct pmd_internals *p,
54         const char *name)
55 {
56         struct softnic_swq *swq;
57
58         if (name == NULL)
59                 return NULL;
60
61         TAILQ_FOREACH(swq, &p->swq_list, node)
62                 if (strcmp(swq->name, name) == 0)
63                         return swq;
64
65         return NULL;
66 }
67
68 struct softnic_swq *
69 softnic_swq_create(struct pmd_internals *p,
70         const char *name,
71         struct softnic_swq_params *params)
72 {
73         char ring_name[NAME_SIZE];
74         struct softnic_swq *swq;
75         struct rte_ring *r;
76         unsigned int flags = RING_F_SP_ENQ | RING_F_SC_DEQ;
77
78         /* Check input params */
79         if (name == NULL ||
80                 softnic_swq_find(p, name) ||
81                 params == NULL ||
82                 params->size == 0)
83                 return NULL;
84
85         /* Resource create */
86         snprintf(ring_name, sizeof(ring_name), "%s_%s",
87                 p->params.name,
88                 name);
89
90         r = rte_ring_create(ring_name,
91                 params->size,
92                 p->params.cpu_id,
93                 flags);
94
95         if (r == NULL)
96                 return NULL;
97
98         /* Node allocation */
99         swq = calloc(1, sizeof(struct softnic_swq));
100         if (swq == NULL) {
101                 rte_ring_free(r);
102                 return NULL;
103         }
104
105         /* Node fill in */
106         strlcpy(swq->name, name, sizeof(swq->name));
107         swq->r = r;
108
109         /* Node add to list */
110         TAILQ_INSERT_TAIL(&p->swq_list, swq, node);
111
112         return swq;
113 }