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