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