57df8efdb6417df754a27256b9bd8ae49e859c8d
[dpdk.git] / examples / quota_watermark / qw / main.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 <rte_eal.h>
35
36 #include <rte_common.h>
37 #include <rte_debug.h>
38 #include <rte_errno.h>
39 #include <rte_ethdev.h>
40 #include <rte_launch.h>
41 #include <rte_lcore.h>
42 #include <rte_log.h>
43 #include <rte_mbuf.h>
44 #include <rte_ring.h>
45
46 #include <rte_byteorder.h>
47
48 #include "args.h"
49 #include "main.h"
50 #include "init.h"
51 #include "../include/conf.h"
52
53
54 #ifdef QW_SOFTWARE_FC
55 #define SEND_PAUSE_FRAME(port_id, duration) send_pause_frame(port_id, duration)
56 #else
57 #define SEND_PAUSE_FRAME(port_id, duration) do { } while(0)
58 #endif
59
60 #define ETHER_TYPE_FLOW_CONTROL 0x8808
61
62 struct ether_fc_frame {
63         uint16_t opcode;
64         uint16_t param;
65 } __attribute__((__packed__));
66
67
68 int *quota;
69 unsigned int *low_watermark;
70
71 uint8_t port_pairs[RTE_MAX_ETHPORTS];
72
73 struct rte_ring *rings[RTE_MAX_LCORE][RTE_MAX_ETHPORTS];
74 struct rte_mempool *mbuf_pool;
75
76
77 static void send_pause_frame(uint8_t port_id, uint16_t duration)
78 {
79         struct rte_mbuf *mbuf;
80         struct ether_fc_frame *pause_frame;
81         struct ether_hdr *hdr;
82         struct ether_addr mac_addr;
83
84         RTE_LOG_DP(DEBUG, USER1,
85                         "Sending PAUSE frame (duration=%d) on port %d\n",
86                         duration, port_id);
87
88         /* Get a mbuf from the pool */
89         mbuf = rte_pktmbuf_alloc(mbuf_pool);
90         if (unlikely(mbuf == NULL))
91                 return;
92
93         /* Prepare a PAUSE frame */
94         hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
95         pause_frame = (struct ether_fc_frame *) &hdr[1];
96
97         rte_eth_macaddr_get(port_id, &mac_addr);
98         ether_addr_copy(&mac_addr, &hdr->s_addr);
99
100         void *tmp = &hdr->d_addr.addr_bytes[0];
101         *((uint64_t *)tmp) = 0x010000C28001ULL;
102
103         hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_FLOW_CONTROL);
104
105         pause_frame->opcode = rte_cpu_to_be_16(0x0001);
106         pause_frame->param  = rte_cpu_to_be_16(duration);
107
108         mbuf->pkt_len  = 60;
109         mbuf->data_len = 60;
110
111         rte_eth_tx_burst(port_id, 0, &mbuf, 1);
112 }
113
114 /**
115  * Get the previous enabled lcore ID
116  *
117  * @param lcore_id
118  *   The current lcore ID.
119  * @return
120  *   The previous enabled lcore_id or -1 if not found.
121  */
122 static unsigned int
123 get_previous_lcore_id(unsigned int lcore_id)
124 {
125         int i;
126
127         for (i = lcore_id - 1; i >= 0; i--)
128                 if (rte_lcore_is_enabled(i))
129                         return i;
130
131         return -1;
132 }
133
134 /**
135  * Get the last enabled lcore ID
136  *
137  * @return
138  *   The last enabled lcore_id.
139  */
140 static unsigned int
141 get_last_lcore_id(void)
142 {
143         int i;
144
145         for (i = RTE_MAX_LCORE; i >= 0; i--)
146                 if (rte_lcore_is_enabled(i))
147                         return i;
148
149         return 0;
150 }
151
152 static void
153 receive_stage(__attribute__((unused)) void *args)
154 {
155         int i, ret;
156
157         uint8_t port_id;
158         uint16_t nb_rx_pkts;
159
160         unsigned int lcore_id;
161
162         struct rte_mbuf *pkts[MAX_PKT_QUOTA];
163         struct rte_ring *ring;
164         enum ring_state ring_state[RTE_MAX_ETHPORTS] = { RING_READY };
165
166         lcore_id = rte_lcore_id();
167
168         RTE_LOG(INFO, USER1,
169                         "%s() started on core %u\n", __func__, lcore_id);
170
171         while (1) {
172
173                 /* Process each port round robin style */
174                 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
175
176                         if (!is_bit_set(port_id, portmask))
177                                 continue;
178
179                         ring = rings[lcore_id][port_id];
180
181                         if (ring_state[port_id] != RING_READY) {
182                                 if (rte_ring_count(ring) > *low_watermark)
183                                         continue;
184                                 else
185                                         ring_state[port_id] = RING_READY;
186                         }
187
188                         /* Enqueue received packets on the RX ring */
189                         nb_rx_pkts = rte_eth_rx_burst(port_id, 0, pkts,
190                                         (uint16_t) *quota);
191                         ret = rte_ring_enqueue_bulk(ring, (void *) pkts,
192                                         nb_rx_pkts);
193                         if (ret == -EDQUOT) {
194                                 ring_state[port_id] = RING_OVERLOADED;
195                                 send_pause_frame(port_id, 1337);
196                         }
197
198                         else if (ret == -ENOBUFS) {
199
200                                 /*
201                                  * Return  mbufs to the pool,
202                                  * effectively dropping packets
203                                  */
204                                 for (i = 0; i < nb_rx_pkts; i++)
205                                         rte_pktmbuf_free(pkts[i]);
206                         }
207                 }
208         }
209 }
210
211 static void
212 pipeline_stage(__attribute__((unused)) void *args)
213 {
214         int i, ret;
215         int nb_dq_pkts;
216
217         uint8_t port_id;
218
219         unsigned int lcore_id, previous_lcore_id;
220
221         void *pkts[MAX_PKT_QUOTA];
222         struct rte_ring *rx, *tx;
223         enum ring_state ring_state[RTE_MAX_ETHPORTS] = { RING_READY };
224
225         lcore_id = rte_lcore_id();
226         previous_lcore_id = get_previous_lcore_id(lcore_id);
227
228         RTE_LOG(INFO, USER1,
229                         "%s() started on core %u - processing packets from core %u\n",
230                         __func__, lcore_id, previous_lcore_id);
231
232         while (1) {
233
234                 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
235
236                         if (!is_bit_set(port_id, portmask))
237                                 continue;
238
239                         tx = rings[lcore_id][port_id];
240                         rx = rings[previous_lcore_id][port_id];
241
242                         if (ring_state[port_id] != RING_READY) {
243                                 if (rte_ring_count(tx) > *low_watermark)
244                                         continue;
245                                 else
246                                         ring_state[port_id] = RING_READY;
247                         }
248
249                         /* Dequeue up to quota mbuf from rx */
250                         nb_dq_pkts = rte_ring_dequeue_burst(rx, pkts, *quota);
251                         if (unlikely(nb_dq_pkts < 0))
252                                 continue;
253
254                         /* Enqueue them on tx */
255                         ret = rte_ring_enqueue_bulk(tx, pkts, nb_dq_pkts);
256                         if (ret == -EDQUOT)
257                                 ring_state[port_id] = RING_OVERLOADED;
258
259                         else if (ret == -ENOBUFS) {
260
261                                 /*
262                                  * Return  mbufs to the pool,
263                                  * effectively dropping packets
264                                  */
265                                 for (i = 0; i < nb_dq_pkts; i++)
266                                         rte_pktmbuf_free(pkts[i]);
267                         }
268                 }
269         }
270 }
271
272 static void
273 send_stage(__attribute__((unused)) void *args)
274 {
275         uint16_t nb_dq_pkts;
276
277         uint8_t port_id;
278         uint8_t dest_port_id;
279
280         unsigned int lcore_id, previous_lcore_id;
281
282         struct rte_ring *tx;
283         struct rte_mbuf *tx_pkts[MAX_PKT_QUOTA];
284
285         lcore_id = rte_lcore_id();
286         previous_lcore_id = get_previous_lcore_id(lcore_id);
287
288         RTE_LOG(INFO, USER1,
289                         "%s() started on core %u - processing packets from core %u\n",
290                         __func__, lcore_id, previous_lcore_id);
291
292         while (1) {
293
294                 /* Process each ring round robin style */
295                 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
296
297                         if (!is_bit_set(port_id, portmask))
298                                 continue;
299
300                         dest_port_id = port_pairs[port_id];
301                         tx = rings[previous_lcore_id][port_id];
302
303                         if (rte_ring_empty(tx))
304                                 continue;
305
306                         /* Dequeue packets from tx and send them */
307                         nb_dq_pkts = (uint16_t) rte_ring_dequeue_burst(tx,
308                                         (void *) tx_pkts, *quota);
309                         rte_eth_tx_burst(dest_port_id, 0, tx_pkts, nb_dq_pkts);
310
311                         /* TODO: Check if nb_dq_pkts == nb_tx_pkts? */
312                 }
313         }
314 }
315
316 int
317 main(int argc, char **argv)
318 {
319         int ret;
320         unsigned int lcore_id, master_lcore_id, last_lcore_id;
321
322         uint8_t port_id;
323
324         rte_set_log_level(RTE_LOG_INFO);
325
326         ret = rte_eal_init(argc, argv);
327         if (ret < 0)
328                 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
329
330         argc -= ret;
331         argv += ret;
332
333         init_dpdk();
334         setup_shared_variables();
335
336         *quota = 32;
337         *low_watermark = 60 * RING_SIZE / 100;
338
339         last_lcore_id   = get_last_lcore_id();
340         master_lcore_id = rte_get_master_lcore();
341
342         /* Parse the application's arguments */
343         ret = parse_qw_args(argc, argv);
344         if (ret < 0)
345                 rte_exit(EXIT_FAILURE, "Invalid quota/watermark argument(s)\n");
346
347         /* Create a pool of mbuf to store packets */
348         mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, 32, 0,
349                         MBUF_DATA_SIZE, rte_socket_id());
350         if (mbuf_pool == NULL)
351                 rte_panic("%s\n", rte_strerror(rte_errno));
352
353         for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
354                 if (is_bit_set(port_id, portmask)) {
355                         configure_eth_port(port_id);
356                         init_ring(master_lcore_id, port_id);
357                 }
358
359         pair_ports();
360
361         /*
362          * Start pipeline_connect() on all the available slave lcores
363          * but the last
364          */
365         for (lcore_id = 0 ; lcore_id < last_lcore_id; lcore_id++) {
366                 if (rte_lcore_is_enabled(lcore_id) &&
367                                 lcore_id != master_lcore_id) {
368
369                         for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
370                                 if (is_bit_set(port_id, portmask))
371                                         init_ring(lcore_id, port_id);
372
373                         /* typecast is a workaround for GCC 4.3 bug */
374                         rte_eal_remote_launch((int (*)(void *))pipeline_stage,
375                                         NULL, lcore_id);
376                 }
377         }
378
379         /* Start send_stage() on the last slave core */
380         /* typecast is a workaround for GCC 4.3 bug */
381         rte_eal_remote_launch((int (*)(void *))send_stage, NULL, last_lcore_id);
382
383         /* Start receive_stage() on the master core */
384         receive_stage(NULL);
385
386         return 0;
387 }