1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <rte_malloc.h>
9 #include "rte_port_sched.h"
14 #ifdef RTE_PORT_STATS_COLLECT
16 #define RTE_PORT_SCHED_READER_PKTS_IN_ADD(port, val) \
17 port->stats.n_pkts_in += val
18 #define RTE_PORT_SCHED_READER_PKTS_DROP_ADD(port, val) \
19 port->stats.n_pkts_drop += val
23 #define RTE_PORT_SCHED_READER_PKTS_IN_ADD(port, val)
24 #define RTE_PORT_SCHED_READER_PKTS_DROP_ADD(port, val)
28 struct rte_port_sched_reader {
29 struct rte_port_in_stats stats;
31 struct rte_sched_port *sched;
35 rte_port_sched_reader_create(void *params, int socket_id)
37 struct rte_port_sched_reader_params *conf =
39 struct rte_port_sched_reader *port;
41 /* Check input parameters */
43 (conf->sched == NULL)) {
44 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
48 /* Memory allocation */
49 port = rte_zmalloc_socket("PORT", sizeof(*port),
50 RTE_CACHE_LINE_SIZE, socket_id);
52 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
57 port->sched = conf->sched;
63 rte_port_sched_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
65 struct rte_port_sched_reader *p = port;
68 nb_rx = rte_sched_port_dequeue(p->sched, pkts, n_pkts);
69 RTE_PORT_SCHED_READER_PKTS_IN_ADD(p, nb_rx);
75 rte_port_sched_reader_free(void *port)
78 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
88 rte_port_sched_reader_stats_read(void *port,
89 struct rte_port_in_stats *stats, int clear)
91 struct rte_port_sched_reader *p =
95 memcpy(stats, &p->stats, sizeof(p->stats));
98 memset(&p->stats, 0, sizeof(p->stats));
106 #ifdef RTE_PORT_STATS_COLLECT
108 #define RTE_PORT_SCHED_WRITER_STATS_PKTS_IN_ADD(port, val) \
109 port->stats.n_pkts_in += val
110 #define RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(port, val) \
111 port->stats.n_pkts_drop += val
115 #define RTE_PORT_SCHED_WRITER_STATS_PKTS_IN_ADD(port, val)
116 #define RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(port, val)
120 struct rte_port_sched_writer {
121 struct rte_port_out_stats stats;
123 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
124 struct rte_sched_port *sched;
125 uint32_t tx_burst_sz;
126 uint32_t tx_buf_count;
131 rte_port_sched_writer_create(void *params, int socket_id)
133 struct rte_port_sched_writer_params *conf =
135 struct rte_port_sched_writer *port;
137 /* Check input parameters */
138 if ((conf == NULL) ||
139 (conf->sched == NULL) ||
140 (conf->tx_burst_sz == 0) ||
141 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
142 (!rte_is_power_of_2(conf->tx_burst_sz))) {
143 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
147 /* Memory allocation */
148 port = rte_zmalloc_socket("PORT", sizeof(*port),
149 RTE_CACHE_LINE_SIZE, socket_id);
151 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
156 port->sched = conf->sched;
157 port->tx_burst_sz = conf->tx_burst_sz;
158 port->tx_buf_count = 0;
159 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
165 rte_port_sched_writer_tx(void *port, struct rte_mbuf *pkt)
167 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
169 p->tx_buf[p->tx_buf_count++] = pkt;
170 RTE_PORT_SCHED_WRITER_STATS_PKTS_IN_ADD(p, 1);
171 if (p->tx_buf_count >= p->tx_burst_sz) {
172 __rte_unused uint32_t nb_tx;
174 nb_tx = rte_sched_port_enqueue(p->sched, p->tx_buf, p->tx_buf_count);
175 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
183 rte_port_sched_writer_tx_bulk(void *port,
184 struct rte_mbuf **pkts,
187 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
188 uint64_t bsz_mask = p->bsz_mask;
189 uint32_t tx_buf_count = p->tx_buf_count;
190 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
191 ((pkts_mask & bsz_mask) ^ bsz_mask);
194 __rte_unused uint32_t nb_tx;
195 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
198 nb_tx = rte_sched_port_enqueue(p->sched, p->tx_buf,
200 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, tx_buf_count - nb_tx);
204 nb_tx = rte_sched_port_enqueue(p->sched, pkts, n_pkts);
205 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, n_pkts - nb_tx);
207 for ( ; pkts_mask; ) {
208 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
209 uint64_t pkt_mask = 1LLU << pkt_index;
210 struct rte_mbuf *pkt = pkts[pkt_index];
212 p->tx_buf[tx_buf_count++] = pkt;
213 RTE_PORT_SCHED_WRITER_STATS_PKTS_IN_ADD(p, 1);
214 pkts_mask &= ~pkt_mask;
216 p->tx_buf_count = tx_buf_count;
218 if (tx_buf_count >= p->tx_burst_sz) {
219 __rte_unused uint32_t nb_tx;
221 nb_tx = rte_sched_port_enqueue(p->sched, p->tx_buf,
223 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, tx_buf_count - nb_tx);
232 rte_port_sched_writer_flush(void *port)
234 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
236 if (p->tx_buf_count) {
237 __rte_unused uint32_t nb_tx;
239 nb_tx = rte_sched_port_enqueue(p->sched, p->tx_buf, p->tx_buf_count);
240 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
248 rte_port_sched_writer_free(void *port)
251 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
255 rte_port_sched_writer_flush(port);
262 rte_port_sched_writer_stats_read(void *port,
263 struct rte_port_out_stats *stats, int clear)
265 struct rte_port_sched_writer *p =
269 memcpy(stats, &p->stats, sizeof(p->stats));
272 memset(&p->stats, 0, sizeof(p->stats));
278 * Summary of port operations
280 struct rte_port_in_ops rte_port_sched_reader_ops = {
281 .f_create = rte_port_sched_reader_create,
282 .f_free = rte_port_sched_reader_free,
283 .f_rx = rte_port_sched_reader_rx,
284 .f_stats = rte_port_sched_reader_stats_read,
287 struct rte_port_out_ops rte_port_sched_writer_ops = {
288 .f_create = rte_port_sched_writer_create,
289 .f_free = rte_port_sched_writer_free,
290 .f_tx = rte_port_sched_writer_tx,
291 .f_tx_bulk = rte_port_sched_writer_tx_bulk,
292 .f_flush = rte_port_sched_writer_flush,
293 .f_stats = rte_port_sched_writer_stats_read,