ba1fdcb265488d9636b379aff979f3201ac904e6
[dpdk.git] / lib / librte_port / rte_port_ring.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <string.h>
34
35 #include <rte_mbuf.h>
36 #include <rte_ring.h>
37 #include <rte_malloc.h>
38
39 #include "rte_port_ring.h"
40
41 /*
42  * Port RING Reader
43  */
44 struct rte_port_ring_reader {
45         struct rte_ring *ring;
46 };
47
48 static void *
49 rte_port_ring_reader_create(void *params, int socket_id)
50 {
51         struct rte_port_ring_reader_params *conf =
52                         (struct rte_port_ring_reader_params *) params;
53         struct rte_port_ring_reader *port;
54
55         /* Check input parameters */
56         if (conf == NULL) {
57                 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
58                 return NULL;
59         }
60
61         /* Memory allocation */
62         port = rte_zmalloc_socket("PORT", sizeof(*port),
63                         RTE_CACHE_LINE_SIZE, socket_id);
64         if (port == NULL) {
65                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
66                 return NULL;
67         }
68
69         /* Initialization */
70         port->ring = conf->ring;
71
72         return port;
73 }
74
75 static int
76 rte_port_ring_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
77 {
78         struct rte_port_ring_reader *p = (struct rte_port_ring_reader *) port;
79
80         return rte_ring_sc_dequeue_burst(p->ring, (void **) pkts, n_pkts);
81 }
82
83 static int
84 rte_port_ring_reader_free(void *port)
85 {
86         if (port == NULL) {
87                 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
88                 return -EINVAL;
89         }
90
91         rte_free(port);
92
93         return 0;
94 }
95
96 /*
97  * Port RING Writer
98  */
99 struct rte_port_ring_writer {
100         struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
101         struct rte_ring *ring;
102         uint32_t tx_burst_sz;
103         uint32_t tx_buf_count;
104         uint64_t bsz_mask;
105 };
106
107 static void *
108 rte_port_ring_writer_create(void *params, int socket_id)
109 {
110         struct rte_port_ring_writer_params *conf =
111                         (struct rte_port_ring_writer_params *) params;
112         struct rte_port_ring_writer *port;
113
114         /* Check input parameters */
115         if ((conf == NULL) ||
116             (conf->ring == NULL) ||
117                 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX)) {
118                 RTE_LOG(ERR, PORT, "%s: Invalid Parameters\n", __func__);
119                 return NULL;
120         }
121
122         /* Memory allocation */
123         port = rte_zmalloc_socket("PORT", sizeof(*port),
124                         RTE_CACHE_LINE_SIZE, socket_id);
125         if (port == NULL) {
126                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
127                 return NULL;
128         }
129
130         /* Initialization */
131         port->ring = conf->ring;
132         port->tx_burst_sz = conf->tx_burst_sz;
133         port->tx_buf_count = 0;
134         port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
135
136         return port;
137 }
138
139 static inline void
140 send_burst(struct rte_port_ring_writer *p)
141 {
142         uint32_t nb_tx;
143
144         nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
145                         p->tx_buf_count);
146
147         for ( ; nb_tx < p->tx_buf_count; nb_tx++)
148                 rte_pktmbuf_free(p->tx_buf[nb_tx]);
149
150         p->tx_buf_count = 0;
151 }
152
153 static int
154 rte_port_ring_writer_tx(void *port, struct rte_mbuf *pkt)
155 {
156         struct rte_port_ring_writer *p = (struct rte_port_ring_writer *) port;
157
158         p->tx_buf[p->tx_buf_count++] = pkt;
159         if (p->tx_buf_count >= p->tx_burst_sz)
160                 send_burst(p);
161
162         return 0;
163 }
164
165 static int
166 rte_port_ring_writer_tx_bulk(void *port,
167                 struct rte_mbuf **pkts,
168                 uint64_t pkts_mask)
169 {
170         struct rte_port_ring_writer *p =
171                 (struct rte_port_ring_writer *) port;
172
173         uint32_t bsz_mask = p->bsz_mask;
174         uint32_t tx_buf_count = p->tx_buf_count;
175         uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
176                         ((pkts_mask & bsz_mask) ^ bsz_mask);
177
178         if (expr == 0) {
179                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
180                 uint32_t n_pkts_ok;
181
182                 if (tx_buf_count)
183                         send_burst(p);
184
185                 n_pkts_ok = rte_ring_sp_enqueue_burst(p->ring, (void **)pkts, n_pkts);
186
187                 for ( ; n_pkts_ok < n_pkts; n_pkts_ok++) {
188                         struct rte_mbuf *pkt = pkts[n_pkts_ok];
189
190                         rte_pktmbuf_free(pkt);
191                 }
192         } else {
193                 for ( ; pkts_mask; ) {
194                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
195                         uint64_t pkt_mask = 1LLU << pkt_index;
196                         struct rte_mbuf *pkt = pkts[pkt_index];
197
198                         p->tx_buf[tx_buf_count++] = pkt;
199                         pkts_mask &= ~pkt_mask;
200                 }
201
202                 p->tx_buf_count = tx_buf_count;
203                 if (tx_buf_count >= p->tx_burst_sz)
204                         send_burst(p);
205         }
206
207         return 0;
208 }
209
210 static int
211 rte_port_ring_writer_flush(void *port)
212 {
213         struct rte_port_ring_writer *p = (struct rte_port_ring_writer *) port;
214
215         if (p->tx_buf_count > 0)
216                 send_burst(p);
217
218         return 0;
219 }
220
221 static int
222 rte_port_ring_writer_free(void *port)
223 {
224         if (port == NULL) {
225                 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
226                 return -EINVAL;
227         }
228
229         rte_port_ring_writer_flush(port);
230         rte_free(port);
231
232         return 0;
233 }
234
235 /*
236  * Summary of port operations
237  */
238 struct rte_port_in_ops rte_port_ring_reader_ops = {
239         .f_create = rte_port_ring_reader_create,
240         .f_free = rte_port_ring_reader_free,
241         .f_rx = rte_port_ring_reader_rx,
242 };
243
244 struct rte_port_out_ops rte_port_ring_writer_ops = {
245         .f_create = rte_port_ring_writer_create,
246         .f_free = rte_port_ring_writer_free,
247         .f_tx = rte_port_ring_writer_tx,
248         .f_tx_bulk = rte_port_ring_writer_tx_bulk,
249         .f_flush = rte_port_ring_writer_flush,
250 };