1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include <rte_malloc.h>
10 #include <rte_cycles.h>
11 #include <rte_ethdev.h>
12 #include <rte_memcpy.h>
13 #include <rte_byteorder.h>
14 #include <rte_branch_prediction.h>
15 #include <rte_sched.h>
20 * QoS parameters are encoded as follows:
21 * Outer VLAN ID defines subport
22 * Inner VLAN ID defines pipe
23 * Destination IP 0.0.XXX.0 defines traffic class
24 * Destination IP host (0.0.0.XXX) defines queue
25 * Values below define offset to each field from start of frame
27 #define SUBPORT_OFFSET 7
30 #define QUEUE_OFFSET 20
31 #define COLOR_OFFSET 19
34 get_pkt_sched(struct rte_mbuf *m, uint32_t *subport, uint32_t *pipe,
35 uint32_t *traffic_class, uint32_t *queue, uint32_t *color)
37 uint16_t *pdata = rte_pktmbuf_mtod(m, uint16_t *);
39 *subport = (rte_be_to_cpu_16(pdata[SUBPORT_OFFSET]) & 0x0FFF) &
40 (port_params.n_subports_per_port - 1); /* Outer VLAN ID*/
41 *pipe = (rte_be_to_cpu_16(pdata[PIPE_OFFSET]) & 0x0FFF) &
42 (port_params.n_pipes_per_subport - 1); /* Inner VLAN ID */
43 *traffic_class = (pdata[QUEUE_OFFSET] & 0x0F) &
44 (RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE - 1); /* Destination IP */
45 *queue = ((pdata[QUEUE_OFFSET] >> 8) & 0x0F) &
46 (RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS - 1) ; /* Destination IP */
47 *color = pdata[COLOR_OFFSET] & 0x03; /* Destination IP */
53 app_rx_thread(struct thread_conf **confs)
56 struct rte_mbuf *rx_mbufs[burst_conf.rx_burst] __rte_cache_aligned;
57 struct thread_conf *conf;
62 uint32_t traffic_class;
66 while ((conf = confs[conf_idx])) {
67 nb_rx = rte_eth_rx_burst(conf->rx_port, conf->rx_queue, rx_mbufs,
70 if (likely(nb_rx != 0)) {
71 APP_STATS_ADD(conf->stat.nb_rx, nb_rx);
73 for(i = 0; i < nb_rx; i++) {
74 get_pkt_sched(rx_mbufs[i],
75 &subport, &pipe, &traffic_class, &queue, &color);
76 rte_sched_port_pkt_write(conf->sched_port,
80 (enum rte_meter_color) color);
83 if (unlikely(rte_ring_sp_enqueue_bulk(conf->rx_ring,
84 (void **)rx_mbufs, nb_rx, NULL) == 0)) {
85 for(i = 0; i < nb_rx; i++) {
86 rte_pktmbuf_free(rx_mbufs[i]);
88 APP_STATS_ADD(conf->stat.nb_drop, 1);
93 if (confs[conf_idx] == NULL)
100 /* Send the packet to an output interface
101 * For performance reason function returns number of packets dropped, not sent,
102 * so 0 means that all packets were sent successfully
106 app_send_burst(struct thread_conf *qconf)
108 struct rte_mbuf **mbufs;
111 mbufs = (struct rte_mbuf **)qconf->m_table;
115 ret = rte_eth_tx_burst(qconf->tx_port, qconf->tx_queue, mbufs, (uint16_t)n);
116 /* we cannot drop the packets, so re-send */
117 /* update number of packets to be sent */
119 mbufs = (struct rte_mbuf **)&mbufs[ret];
124 /* Send the packet to an output interface */
126 app_send_packets(struct thread_conf *qconf, struct rte_mbuf **mbufs, uint32_t nb_pkt)
130 len = qconf->n_mbufs;
131 for(i = 0; i < nb_pkt; i++) {
132 qconf->m_table[len] = mbufs[i];
134 /* enough pkts to be sent */
135 if (unlikely(len == burst_conf.tx_burst)) {
136 qconf->n_mbufs = len;
137 app_send_burst(qconf);
142 qconf->n_mbufs = len;
146 app_tx_thread(struct thread_conf **confs)
148 struct rte_mbuf *mbufs[burst_conf.qos_dequeue];
149 struct thread_conf *conf;
152 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
154 while ((conf = confs[conf_idx])) {
155 retval = rte_ring_sc_dequeue_bulk(conf->tx_ring, (void **)mbufs,
156 burst_conf.qos_dequeue, NULL);
157 if (likely(retval != 0)) {
158 app_send_packets(conf, mbufs, burst_conf.qos_dequeue);
160 conf->counter = 0; /* reset empty read loop counter */
165 /* drain ring and TX queues */
166 if (unlikely(conf->counter > drain_tsc)) {
167 /* now check is there any packets left to be transmitted */
168 if (conf->n_mbufs != 0) {
169 app_send_burst(conf);
177 if (confs[conf_idx] == NULL)
184 app_worker_thread(struct thread_conf **confs)
186 struct rte_mbuf *mbufs[burst_conf.ring_burst];
187 struct thread_conf *conf;
190 while ((conf = confs[conf_idx])) {
193 /* Read packet from the ring */
194 nb_pkt = rte_ring_sc_dequeue_burst(conf->rx_ring, (void **)mbufs,
195 burst_conf.ring_burst, NULL);
196 if (likely(nb_pkt)) {
197 int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs,
200 APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent);
201 APP_STATS_ADD(conf->stat.nb_rx, nb_pkt);
204 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs,
205 burst_conf.qos_dequeue);
206 if (likely(nb_pkt > 0))
207 while (rte_ring_sp_enqueue_bulk(conf->tx_ring,
208 (void **)mbufs, nb_pkt, NULL) == 0)
212 if (confs[conf_idx] == NULL)
219 app_mixed_thread(struct thread_conf **confs)
221 struct rte_mbuf *mbufs[burst_conf.ring_burst];
222 struct thread_conf *conf;
224 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
226 while ((conf = confs[conf_idx])) {
229 /* Read packet from the ring */
230 nb_pkt = rte_ring_sc_dequeue_burst(conf->rx_ring, (void **)mbufs,
231 burst_conf.ring_burst, NULL);
232 if (likely(nb_pkt)) {
233 int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs,
236 APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent);
237 APP_STATS_ADD(conf->stat.nb_rx, nb_pkt);
241 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs,
242 burst_conf.qos_dequeue);
243 if (likely(nb_pkt > 0)) {
244 app_send_packets(conf, mbufs, nb_pkt);
246 conf->counter = 0; /* reset empty read loop counter */
251 /* drain ring and TX queues */
252 if (unlikely(conf->counter > drain_tsc)) {
254 /* now check is there any packets left to be transmitted */
255 if (conf->n_mbufs != 0) {
256 app_send_burst(conf);
264 if (confs[conf_idx] == NULL)