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