4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <rte_malloc.h>
38 #include "rte_port_sched.h"
43 struct rte_port_sched_reader {
44 struct rte_sched_port *sched;
48 rte_port_sched_reader_create(void *params, int socket_id)
50 struct rte_port_sched_reader_params *conf =
51 (struct rte_port_sched_reader_params *) params;
52 struct rte_port_sched_reader *port;
54 /* Check input parameters */
56 (conf->sched == NULL)) {
57 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
61 /* Memory allocation */
62 port = rte_zmalloc_socket("PORT", sizeof(*port),
63 RTE_CACHE_LINE_SIZE, socket_id);
65 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
70 port->sched = conf->sched;
76 rte_port_sched_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
78 struct rte_port_sched_reader *p = (struct rte_port_sched_reader *) port;
80 return rte_sched_port_dequeue(p->sched, pkts, n_pkts);
84 rte_port_sched_reader_free(void *port)
87 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
99 struct rte_port_sched_writer {
100 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
101 struct rte_sched_port *sched;
102 uint32_t tx_burst_sz;
103 uint32_t tx_buf_count;
108 rte_port_sched_writer_create(void *params, int socket_id)
110 struct rte_port_sched_writer_params *conf =
111 (struct rte_port_sched_writer_params *) params;
112 struct rte_port_sched_writer *port;
114 /* Check input parameters */
115 if ((conf == NULL) ||
116 (conf->sched == NULL) ||
117 (conf->tx_burst_sz == 0) ||
118 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
119 (!rte_is_power_of_2(conf->tx_burst_sz))) {
120 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
124 /* Memory allocation */
125 port = rte_zmalloc_socket("PORT", sizeof(*port),
126 RTE_CACHE_LINE_SIZE, socket_id);
128 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
133 port->sched = conf->sched;
134 port->tx_burst_sz = conf->tx_burst_sz;
135 port->tx_buf_count = 0;
136 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
142 rte_port_sched_writer_tx(void *port, struct rte_mbuf *pkt)
144 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
146 p->tx_buf[p->tx_buf_count++] = pkt;
147 if (p->tx_buf_count >= p->tx_burst_sz) {
148 rte_sched_port_enqueue(p->sched, p->tx_buf, p->tx_buf_count);
156 rte_port_sched_writer_tx_bulk(void *port,
157 struct rte_mbuf **pkts,
160 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
161 uint32_t bsz_mask = p->bsz_mask;
162 uint32_t tx_buf_count = p->tx_buf_count;
163 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
164 ((pkts_mask & bsz_mask) ^ bsz_mask);
167 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
170 rte_sched_port_enqueue(p->sched, p->tx_buf,
175 rte_sched_port_enqueue(p->sched, pkts, n_pkts);
177 for ( ; pkts_mask; ) {
178 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
179 uint64_t pkt_mask = 1LLU << pkt_index;
180 struct rte_mbuf *pkt = pkts[pkt_index];
182 p->tx_buf[tx_buf_count++] = pkt;
183 pkts_mask &= ~pkt_mask;
185 p->tx_buf_count = tx_buf_count;
187 if (tx_buf_count >= p->tx_burst_sz) {
188 rte_sched_port_enqueue(p->sched, p->tx_buf,
198 rte_port_sched_writer_flush(void *port)
200 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
202 if (p->tx_buf_count) {
203 rte_sched_port_enqueue(p->sched, p->tx_buf, p->tx_buf_count);
211 rte_port_sched_writer_free(void *port)
214 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
218 rte_port_sched_writer_flush(port);
225 * Summary of port operations
227 struct rte_port_in_ops rte_port_sched_reader_ops = {
228 .f_create = rte_port_sched_reader_create,
229 .f_free = rte_port_sched_reader_free,
230 .f_rx = rte_port_sched_reader_rx,
233 struct rte_port_out_ops rte_port_sched_writer_ops = {
234 .f_create = rte_port_sched_writer_create,
235 .f_free = rte_port_sched_writer_free,
236 .f_tx = rte_port_sched_writer_tx,
237 .f_tx_bulk = rte_port_sched_writer_tx_bulk,
238 .f_flush = rte_port_sched_writer_flush,