net/sfc: make TSO a datapath-dependent feature
[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, NULL) == 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                 /* 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         } while (n);
147 }
148
149
150 /* Send the packet to an output interface */
151 static void
152 app_send_packets(struct thread_conf *qconf, struct rte_mbuf **mbufs, uint32_t nb_pkt)
153 {
154         uint32_t i, len;
155
156         len = qconf->n_mbufs;
157         for(i = 0; i < nb_pkt; i++) {
158                 qconf->m_table[len] = mbufs[i];
159                 len++;
160                 /* enough pkts to be sent */
161                 if (unlikely(len == burst_conf.tx_burst)) {
162                         qconf->n_mbufs = len;
163                         app_send_burst(qconf);
164                         len = 0;
165                 }
166         }
167
168         qconf->n_mbufs = len;
169 }
170
171 void
172 app_tx_thread(struct thread_conf **confs)
173 {
174         struct rte_mbuf *mbufs[burst_conf.qos_dequeue];
175         struct thread_conf *conf;
176         int conf_idx = 0;
177         int retval;
178         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
179
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);
185
186                         conf->counter = 0; /* reset empty read loop counter */
187                 }
188
189                 conf->counter++;
190
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);
196
197                                 conf->n_mbufs = 0;
198                         }
199                         conf->counter = 0;
200                 }
201
202                 conf_idx++;
203                 if (confs[conf_idx] == NULL)
204                         conf_idx = 0;
205         }
206 }
207
208
209 void
210 app_worker_thread(struct thread_conf **confs)
211 {
212         struct rte_mbuf *mbufs[burst_conf.ring_burst];
213         struct thread_conf *conf;
214         int conf_idx = 0;
215
216         while ((conf = confs[conf_idx])) {
217                 uint32_t nb_pkt;
218
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,
224                                         nb_pkt);
225
226                         APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent);
227                         APP_STATS_ADD(conf->stat.nb_rx, nb_pkt);
228                 }
229
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)
235                                 ; /* empty body */
236
237                 conf_idx++;
238                 if (confs[conf_idx] == NULL)
239                         conf_idx = 0;
240         }
241 }
242
243
244 void
245 app_mixed_thread(struct thread_conf **confs)
246 {
247         struct rte_mbuf *mbufs[burst_conf.ring_burst];
248         struct thread_conf *conf;
249         int conf_idx = 0;
250         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
251
252         while ((conf = confs[conf_idx])) {
253                 uint32_t nb_pkt;
254
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,
260                                         nb_pkt);
261
262                         APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent);
263                         APP_STATS_ADD(conf->stat.nb_rx, nb_pkt);
264                 }
265
266
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);
271
272                         conf->counter = 0; /* reset empty read loop counter */
273                 }
274
275                 conf->counter++;
276
277                 /* drain ring and TX queues */
278                 if (unlikely(conf->counter > drain_tsc)) {
279
280                         /* now check is there any packets left to be transmitted */
281                         if (conf->n_mbufs != 0) {
282                                 app_send_burst(conf);
283
284                                 conf->n_mbufs = 0;
285                         }
286                         conf->counter = 0;
287                 }
288
289                 conf_idx++;
290                 if (confs[conf_idx] == NULL)
291                         conf_idx = 0;
292         }
293 }