afce5efe24a8af9e249482088f6741f043fe0121
[dpdk.git] / examples / qos_sched / app_thread.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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  */
34
35 #include <stdint.h>
36
37 #include <rte_log.h>
38 #include <rte_mbuf.h>
39 #include <rte_malloc.h>
40 #include <rte_cycles.h>
41 #include <rte_ethdev.h>
42 #include <rte_memcpy.h>
43 #include <rte_byteorder.h>
44 #include <rte_branch_prediction.h>
45 #include <rte_sched.h>
46
47 #include "main.h"
48
49 /*
50  * QoS parameters are encoded as follows:
51  *              Outer VLAN ID defines subport
52  *              Inner VLAN ID defines pipe
53  *              Destination IP 0.0.XXX.0 defines traffic class
54  *              Destination IP host (0.0.0.XXX) defines queue
55  * Values below define offset to each field from start of frame
56  */
57 #define SUBPORT_OFFSET  7
58 #define PIPE_OFFSET             9
59 #define TC_OFFSET               20
60 #define QUEUE_OFFSET    20
61 #define COLOR_OFFSET    19
62
63 static inline int
64 get_pkt_sched(struct rte_mbuf *m, uint32_t *subport, uint32_t *pipe,
65                         uint32_t *traffic_class, uint32_t *queue, uint32_t *color)
66 {
67         uint16_t *pdata = rte_pktmbuf_mtod(m, uint16_t *);
68
69         *subport = (rte_be_to_cpu_16(pdata[SUBPORT_OFFSET]) & 0x0FFF) &
70                         (port_params.n_subports_per_port - 1); /* Outer VLAN ID*/
71         *pipe = (rte_be_to_cpu_16(pdata[PIPE_OFFSET]) & 0x0FFF) &
72                         (port_params.n_pipes_per_subport - 1); /* Inner VLAN ID */
73         *traffic_class = (pdata[QUEUE_OFFSET] & 0x0F) &
74                         (RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE - 1); /* Destination IP */
75         *queue = ((pdata[QUEUE_OFFSET] >> 8) & 0x0F) &
76                         (RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS - 1) ; /* Destination IP */
77         *color = pdata[COLOR_OFFSET] & 0x03;    /* Destination IP */
78
79         return 0;
80 }
81
82 void
83 app_rx_thread(struct thread_conf **confs)
84 {
85         uint32_t i, nb_rx;
86         struct rte_mbuf *rx_mbufs[burst_conf.rx_burst] __rte_cache_aligned;
87         struct thread_conf *conf;
88         int conf_idx = 0;
89
90         uint32_t subport;
91         uint32_t pipe;
92         uint32_t traffic_class;
93         uint32_t queue;
94         uint32_t color;
95
96         while ((conf = confs[conf_idx])) {
97                 nb_rx = rte_eth_rx_burst(conf->rx_port, conf->rx_queue, rx_mbufs,
98                                 burst_conf.rx_burst);
99
100                 if (likely(nb_rx != 0)) {
101                         APP_STATS_ADD(conf->stat.nb_rx, nb_rx);
102
103                         for(i = 0; i < nb_rx; i++) {
104                                 get_pkt_sched(rx_mbufs[i],
105                                                 &subport, &pipe, &traffic_class, &queue, &color);
106                                 rte_sched_port_pkt_write(rx_mbufs[i], subport, pipe,
107                                                 traffic_class, queue, (enum rte_meter_color) color);
108                         }
109
110                         if (unlikely(rte_ring_sp_enqueue_bulk(conf->rx_ring,
111                                                                 (void **)rx_mbufs, nb_rx) != 0)) {
112                                 for(i = 0; i < nb_rx; i++) {
113                                         rte_pktmbuf_free(rx_mbufs[i]);
114
115                                         APP_STATS_ADD(conf->stat.nb_drop, 1);
116                                 }
117                         }
118                 }
119                 conf_idx++;
120                 if (confs[conf_idx] == NULL)
121                         conf_idx = 0;
122         }
123 }
124
125
126
127 /* Send the packet to an output interface
128  * For performance reason function returns number of packets dropped, not sent,
129  * so 0 means that all packets were sent successfully
130  */
131
132 static inline void
133 app_send_burst(struct thread_conf *qconf)
134 {
135         struct rte_mbuf **mbufs;
136         uint32_t n, ret;
137
138         mbufs = (struct rte_mbuf **)qconf->m_table;
139         n = qconf->n_mbufs;
140
141         do {
142                 ret = rte_eth_tx_burst(qconf->tx_port, qconf->tx_queue, mbufs, (uint16_t)n);
143                 if (unlikely(ret < n)) { /* we cannot drop the packets, so re-send */
144                         /* update number of packets to be sent */
145                         n -= ret;
146                         mbufs = (struct rte_mbuf **)&mbufs[ret];
147                         /* limit number of retries to avoid endless loop */
148                         /* reset retry counter if some packets were sent */
149                         if (likely(ret != 0)) {
150                                 continue;
151                         }
152                 }
153         } while (ret != n);
154 }
155
156
157 /* Send the packet to an output interface */
158 static void
159 app_send_packets(struct thread_conf *qconf, struct rte_mbuf **mbufs, uint32_t nb_pkt)
160 {
161         uint32_t i, len;
162
163         len = qconf->n_mbufs;
164         for(i = 0; i < nb_pkt; i++) {
165                 qconf->m_table[len] = mbufs[i];
166                 len++;
167                 /* enough pkts to be sent */
168                 if (unlikely(len == burst_conf.tx_burst)) {
169                         qconf->n_mbufs = len;
170                         app_send_burst(qconf);
171                         len = 0;
172                 }
173         }
174
175         qconf->n_mbufs = len;
176 }
177
178 void
179 app_tx_thread(struct thread_conf **confs)
180 {
181         struct rte_mbuf *mbufs[burst_conf.qos_dequeue];
182         struct thread_conf *conf;
183         int conf_idx = 0;
184         int retval;
185         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
186
187         while ((conf = confs[conf_idx])) {
188                 retval = rte_ring_sc_dequeue_bulk(conf->tx_ring, (void **)mbufs,
189                                         burst_conf.qos_dequeue);
190                 if (likely(retval == 0)) {
191                         app_send_packets(conf, mbufs, burst_conf.qos_dequeue);
192
193                         conf->counter = 0; /* reset empty read loop counter */
194                 }
195
196                 conf->counter++;
197
198                 /* drain ring and TX queues */
199                 if (unlikely(conf->counter > drain_tsc)) {
200                         /* now check is there any packets left to be transmitted */
201                         if (conf->n_mbufs != 0) {
202                                 app_send_burst(conf);
203
204                                 conf->n_mbufs = 0;
205                         }
206                         conf->counter = 0;
207                 }
208
209                 conf_idx++;
210                 if (confs[conf_idx] == NULL)
211                         conf_idx = 0;
212         }
213 }
214
215
216 void
217 app_worker_thread(struct thread_conf **confs)
218 {
219         struct rte_mbuf *mbufs[burst_conf.ring_burst];
220         struct thread_conf *conf;
221         int conf_idx = 0;
222
223         while ((conf = confs[conf_idx])) {
224                 uint32_t nb_pkt;
225                 int retval;
226
227                 /* Read packet from the ring */
228                 retval = rte_ring_sc_dequeue_bulk(conf->rx_ring, (void **)mbufs,
229                                         burst_conf.ring_burst);
230                 if (likely(retval == 0)) {
231                         int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs,
232                                         burst_conf.ring_burst);
233
234                         APP_STATS_ADD(conf->stat.nb_drop, burst_conf.ring_burst - nb_sent);
235                         APP_STATS_ADD(conf->stat.nb_rx, burst_conf.ring_burst);
236                 }
237
238                 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs,
239                                         burst_conf.qos_dequeue);
240                 if (likely(nb_pkt > 0))
241                         while (rte_ring_sp_enqueue_bulk(conf->tx_ring, (void **)mbufs, nb_pkt) != 0);
242
243                 conf_idx++;
244                 if (confs[conf_idx] == NULL)
245                         conf_idx = 0;
246         }
247 }
248
249
250 void
251 app_mixed_thread(struct thread_conf **confs)
252 {
253         struct rte_mbuf *mbufs[burst_conf.ring_burst];
254         struct thread_conf *conf;
255         int conf_idx = 0;
256         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
257
258         while ((conf = confs[conf_idx])) {
259                 uint32_t nb_pkt;
260                 int retval;
261
262                 /* Read packet from the ring */
263                 retval = rte_ring_sc_dequeue_bulk(conf->rx_ring, (void **)mbufs,
264                                         burst_conf.ring_burst);
265                 if (likely(retval == 0)) {
266                         int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs,
267                                         burst_conf.ring_burst);
268
269                         APP_STATS_ADD(conf->stat.nb_drop, burst_conf.ring_burst - nb_sent);
270                         APP_STATS_ADD(conf->stat.nb_rx, burst_conf.ring_burst);
271                 }
272
273
274                 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs,
275                                         burst_conf.qos_dequeue);
276                 if (likely(nb_pkt > 0)) {
277                         app_send_packets(conf, mbufs, nb_pkt);
278
279                         conf->counter = 0; /* reset empty read loop counter */
280                 }
281
282                 conf->counter++;
283
284                 /* drain ring and TX queues */
285                 if (unlikely(conf->counter > drain_tsc)) {
286
287                         /* now check is there any packets left to be transmitted */
288                         if (conf->n_mbufs != 0) {
289                                 app_send_burst(conf);
290
291                                 conf->n_mbufs = 0;
292                         }
293                         conf->counter = 0;
294                 }
295
296                 conf_idx++;
297                 if (confs[conf_idx] == NULL)
298                         conf_idx = 0;
299         }
300 }
301
302