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.
38 #include <rte_malloc.h>
39 #include <rte_cycles.h>
40 #include <rte_ethdev.h>
41 #include <rte_memcpy.h>
42 #include <rte_byteorder.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_sched.h>
49 * QoS parameters are encoded as follows:
50 * Outer VLAN ID defines subport
51 * Inner VLAN ID defines pipe
52 * Destination IP 0.0.XXX.0 defines traffic class
53 * Destination IP host (0.0.0.XXX) defines queue
54 * Values below define offset to each field from start of frame
56 #define SUBPORT_OFFSET 7
59 #define QUEUE_OFFSET 20
60 #define COLOR_OFFSET 19
63 get_pkt_sched(struct rte_mbuf *m, uint32_t *subport, uint32_t *pipe,
64 uint32_t *traffic_class, uint32_t *queue, uint32_t *color)
66 uint16_t *pdata = rte_pktmbuf_mtod(m, uint16_t *);
68 *subport = (rte_be_to_cpu_16(pdata[SUBPORT_OFFSET]) & 0x0FFF) &
69 (port_params.n_subports_per_port - 1); /* Outer VLAN ID*/
70 *pipe = (rte_be_to_cpu_16(pdata[PIPE_OFFSET]) & 0x0FFF) &
71 (port_params.n_pipes_per_subport - 1); /* Inner VLAN ID */
72 *traffic_class = (pdata[QUEUE_OFFSET] & 0x0F) &
73 (RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE - 1); /* Destination IP */
74 *queue = ((pdata[QUEUE_OFFSET] >> 8) & 0x0F) &
75 (RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS - 1) ; /* Destination IP */
76 *color = pdata[COLOR_OFFSET] & 0x03; /* Destination IP */
82 app_rx_thread(struct thread_conf **confs)
85 struct rte_mbuf *rx_mbufs[burst_conf.rx_burst] __rte_cache_aligned;
86 struct thread_conf *conf;
91 uint32_t traffic_class;
95 while ((conf = confs[conf_idx])) {
96 nb_rx = rte_eth_rx_burst(conf->rx_port, conf->rx_queue, rx_mbufs,
99 if (likely(nb_rx != 0)) {
100 APP_STATS_ADD(conf->stat.nb_rx, nb_rx);
102 for(i = 0; i < nb_rx; i++) {
103 get_pkt_sched(rx_mbufs[i],
104 &subport, &pipe, &traffic_class, &queue, &color);
105 rte_sched_port_pkt_write(rx_mbufs[i], subport, pipe,
106 traffic_class, queue, (enum rte_meter_color) color);
109 if (unlikely(rte_ring_sp_enqueue_bulk(conf->rx_ring,
110 (void **)rx_mbufs, nb_rx, NULL) == 0)) {
111 for(i = 0; i < nb_rx; i++) {
112 rte_pktmbuf_free(rx_mbufs[i]);
114 APP_STATS_ADD(conf->stat.nb_drop, 1);
119 if (confs[conf_idx] == NULL)
126 /* Send the packet to an output interface
127 * For performance reason function returns number of packets dropped, not sent,
128 * so 0 means that all packets were sent successfully
132 app_send_burst(struct thread_conf *qconf)
134 struct rte_mbuf **mbufs;
137 mbufs = (struct rte_mbuf **)qconf->m_table;
141 ret = rte_eth_tx_burst(qconf->tx_port, qconf->tx_queue, mbufs, (uint16_t)n);
142 /* we cannot drop the packets, so re-send */
143 /* update number of packets to be sent */
145 mbufs = (struct rte_mbuf **)&mbufs[ret];
150 /* Send the packet to an output interface */
152 app_send_packets(struct thread_conf *qconf, struct rte_mbuf **mbufs, uint32_t nb_pkt)
156 len = qconf->n_mbufs;
157 for(i = 0; i < nb_pkt; i++) {
158 qconf->m_table[len] = mbufs[i];
160 /* enough pkts to be sent */
161 if (unlikely(len == burst_conf.tx_burst)) {
162 qconf->n_mbufs = len;
163 app_send_burst(qconf);
168 qconf->n_mbufs = len;
172 app_tx_thread(struct thread_conf **confs)
174 struct rte_mbuf *mbufs[burst_conf.qos_dequeue];
175 struct thread_conf *conf;
178 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
180 while ((conf = confs[conf_idx])) {
181 retval = rte_ring_sc_dequeue_bulk(conf->tx_ring, (void **)mbufs,
182 burst_conf.qos_dequeue, NULL);
183 if (likely(retval != 0)) {
184 app_send_packets(conf, mbufs, burst_conf.qos_dequeue);
186 conf->counter = 0; /* reset empty read loop counter */
191 /* drain ring and TX queues */
192 if (unlikely(conf->counter > drain_tsc)) {
193 /* now check is there any packets left to be transmitted */
194 if (conf->n_mbufs != 0) {
195 app_send_burst(conf);
203 if (confs[conf_idx] == NULL)
210 app_worker_thread(struct thread_conf **confs)
212 struct rte_mbuf *mbufs[burst_conf.ring_burst];
213 struct thread_conf *conf;
216 while ((conf = confs[conf_idx])) {
219 /* Read packet from the ring */
220 nb_pkt = rte_ring_sc_dequeue_burst(conf->rx_ring, (void **)mbufs,
221 burst_conf.ring_burst, NULL);
222 if (likely(nb_pkt)) {
223 int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs,
226 APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent);
227 APP_STATS_ADD(conf->stat.nb_rx, nb_pkt);
230 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs,
231 burst_conf.qos_dequeue);
232 if (likely(nb_pkt > 0))
233 while (rte_ring_sp_enqueue_bulk(conf->tx_ring,
234 (void **)mbufs, nb_pkt, NULL) == 0)
238 if (confs[conf_idx] == NULL)
245 app_mixed_thread(struct thread_conf **confs)
247 struct rte_mbuf *mbufs[burst_conf.ring_burst];
248 struct thread_conf *conf;
250 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
252 while ((conf = confs[conf_idx])) {
255 /* Read packet from the ring */
256 nb_pkt = rte_ring_sc_dequeue_burst(conf->rx_ring, (void **)mbufs,
257 burst_conf.ring_burst, NULL);
258 if (likely(nb_pkt)) {
259 int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs,
262 APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent);
263 APP_STATS_ADD(conf->stat.nb_rx, nb_pkt);
267 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs,
268 burst_conf.qos_dequeue);
269 if (likely(nb_pkt > 0)) {
270 app_send_packets(conf, mbufs, nb_pkt);
272 conf->counter = 0; /* reset empty read loop counter */
277 /* drain ring and TX queues */
278 if (unlikely(conf->counter > drain_tsc)) {
280 /* now check is there any packets left to be transmitted */
281 if (conf->n_mbufs != 0) {
282 app_send_burst(conf);
290 if (confs[conf_idx] == NULL)