first public release
[dpdk.git] / examples / load_balancer / init.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <sys/types.h>
41 #include <string.h>
42 #include <sys/queue.h>
43 #include <stdarg.h>
44 #include <errno.h>
45 #include <getopt.h>
46
47 #include <rte_common.h>
48 #include <rte_byteorder.h>
49 #include <rte_log.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_tailq.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_cycles.h>
59 #include <rte_prefetch.h>
60 #include <rte_lcore.h>
61 #include <rte_per_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
64 #include <rte_pci.h>
65 #include <rte_random.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
69 #include <rte_ring.h>
70 #include <rte_mempool.h>
71 #include <rte_mbuf.h>
72 #include <rte_string_fns.h>
73 #include <rte_ip.h>
74 #include <rte_tcp.h>
75 #include <rte_lpm.h>
76
77 #include "main.h"
78
79 static struct rte_eth_conf port_conf = {
80         .rxmode = {
81                 .split_hdr_size = 0,
82                 .header_split   = 0, /**< Header Split disabled */
83                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
84                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
85                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
86                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
87         },
88         .rx_adv_conf = {
89                 .rss_conf = {
90                         .rss_key = NULL,
91                         .rss_hf = ETH_RSS_IPV4,
92                 },
93         },
94         .txmode = {
95         },
96 };
97
98 static struct rte_eth_rxconf rx_conf = {
99         .rx_thresh = {
100                 .pthresh = APP_DEFAULT_NIC_RX_PTHRESH,
101                 .hthresh = APP_DEFAULT_NIC_RX_HTHRESH,
102                 .wthresh = APP_DEFAULT_NIC_RX_WTHRESH,
103         },
104         .rx_free_thresh = APP_DEFAULT_NIC_RX_FREE_THRESH,
105 };
106
107 static struct rte_eth_txconf tx_conf = {
108         .tx_thresh = {
109                 .pthresh = APP_DEFAULT_NIC_TX_PTHRESH,
110                 .hthresh = APP_DEFAULT_NIC_TX_HTHRESH,
111                 .wthresh = APP_DEFAULT_NIC_TX_WTHRESH,
112         },
113         .tx_free_thresh = APP_DEFAULT_NIC_TX_FREE_THRESH,
114         .tx_rs_thresh = APP_DEFAULT_NIC_TX_RS_THRESH,
115 };
116
117 static void
118 app_assign_worker_ids(void)
119 {
120         uint32_t lcore, worker_id;
121
122         /* Assign ID for each worker */
123         worker_id = 0;
124         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
125                 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
126
127                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
128                         continue;
129                 }
130
131                 lp_worker->worker_id = worker_id;
132                 worker_id ++;
133         }
134 }
135
136 static void
137 app_init_mbuf_pools(void)
138 {
139         uint32_t socket, lcore;
140
141         /* Init the buffer pools */
142         for (socket = 0; socket < APP_MAX_SOCKETS; socket ++) {
143                 char name[32];
144                 if (app_is_socket_used(socket) == 0) {
145                         continue;
146                 }
147
148                 rte_snprintf(name, sizeof(name), "mbuf_pool_%u", socket);
149                 printf("Creating the mbuf pool for socket %u ...\n", socket);
150                 app.pools[socket] = rte_mempool_create(
151                         name,
152                         APP_DEFAULT_MEMPOOL_BUFFERS,
153                         APP_DEFAULT_MBUF_SIZE,
154                         APP_DEFAULT_MEMPOOL_CACHE_SIZE,
155                         sizeof(struct rte_pktmbuf_pool_private),
156                         rte_pktmbuf_pool_init, NULL,
157                         rte_pktmbuf_init, NULL,
158                         socket,
159                         0);
160                 if (app.pools[socket] == NULL) {
161                         rte_panic("Cannot create mbuf pool on socket %u\n", socket);
162                 }
163         }
164
165         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
166                 if (app.lcore_params[lcore].type == e_APP_LCORE_DISABLED) {
167                         continue;
168                 }
169
170                 socket = rte_lcore_to_socket_id(lcore);
171                 app.lcore_params[lcore].pool = app.pools[socket];
172         }
173 }
174
175 static void
176 app_init_lpm_tables(void)
177 {
178         uint32_t socket, lcore;
179
180         /* Init the LPM tables */
181         for (socket = 0; socket < APP_MAX_SOCKETS; socket ++) {
182                 char name[32];
183                 uint32_t rule;
184
185                 if (app_is_socket_used(socket) == 0) {
186                         continue;
187                 }
188
189                 rte_snprintf(name, sizeof(name), "lpm_table_%u", socket);
190                 printf("Creating the LPM table for socket %u ...\n", socket);
191                 app.lpm_tables[socket] = rte_lpm_create(
192                         name,
193                         socket,
194                         APP_MAX_LPM_RULES,
195                         RTE_LPM_MEMZONE);
196                 if (app.lpm_tables[socket] == NULL) {
197                         rte_panic("Unable to create LPM table on socket %u\n", socket);
198                 }
199
200                 for (rule = 0; rule < app.n_lpm_rules; rule ++) {
201                         int ret;
202
203                         ret = rte_lpm_add(app.lpm_tables[socket],
204                                 app.lpm_rules[rule].ip,
205                                 app.lpm_rules[rule].depth,
206                                 app.lpm_rules[rule].if_out);
207
208                         if (ret < 0) {
209                                 rte_panic("Unable to add entry %u (%x/%u => %u) to the LPM table on socket %u (%d)\n",
210                                         rule, app.lpm_rules[rule].ip,
211                                         (uint32_t) app.lpm_rules[rule].depth,
212                                         (uint32_t) app.lpm_rules[rule].if_out,
213                                         socket,
214                                         ret);
215                         }
216                 }
217
218         }
219
220         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
221                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
222                         continue;
223                 }
224
225                 socket = rte_lcore_to_socket_id(lcore);
226                 app.lcore_params[lcore].worker.lpm_table = app.lpm_tables[socket];
227         }
228 }
229
230 static void
231 app_init_rings_rx(void)
232 {
233         uint32_t lcore;
234
235         /* Initialize the rings for the RX side */
236         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
237                 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
238                 uint32_t socket_io, lcore_worker;
239
240                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
241                     (lp_io->rx.n_nic_queues == 0)) {
242                         continue;
243                 }
244
245                 socket_io = rte_lcore_to_socket_id(lcore);
246
247                 for (lcore_worker = 0; lcore_worker < APP_MAX_LCORES; lcore_worker ++) {
248                         char name[32];
249                         struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore_worker].worker;
250                         struct rte_ring *ring = NULL;
251
252                         if (app.lcore_params[lcore_worker].type != e_APP_LCORE_WORKER) {
253                                 continue;
254                         }
255
256                         printf("Creating ring to connect I/O lcore %u (socket %u) with worker lcore %u ...\n",
257                                 lcore,
258                                 socket_io,
259                                 lcore_worker);
260                         rte_snprintf(name, sizeof(name), "app_ring_rx_s%u_io%u_w%u",
261                                 socket_io,
262                                 lcore,
263                                 lcore_worker);
264                         ring = rte_ring_create(
265                                 name,
266                                 app.ring_rx_size,
267                                 socket_io,
268                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
269                         if (ring == NULL) {
270                                 rte_panic("Cannot create ring to connect I/O core %u with worker core %u\n",
271                                         lcore,
272                                         lcore_worker);
273                         }
274
275                         lp_io->rx.rings[lp_io->rx.n_rings] = ring;
276                         lp_io->rx.n_rings ++;
277
278                         lp_worker->rings_in[lp_worker->n_rings_in] = ring;
279                         lp_worker->n_rings_in ++;
280                 }
281         }
282
283         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
284                 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
285
286                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
287                     (lp_io->rx.n_nic_queues == 0)) {
288                         continue;
289                 }
290
291                 if (lp_io->rx.n_rings != app_get_lcores_worker()) {
292                         rte_panic("Algorithmic error (I/O RX rings)\n");
293                 }
294         }
295
296         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
297                 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
298
299                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
300                         continue;
301                 }
302
303                 if (lp_worker->n_rings_in != app_get_lcores_io_rx()) {
304                         rte_panic("Algorithmic error (worker input rings)\n");
305                 }
306         }
307 }
308
309 static void
310 app_init_rings_tx(void)
311 {
312         uint32_t lcore;
313
314         /* Initialize the rings for the TX side */
315         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
316                 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
317                 uint32_t port;
318
319                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
320                         continue;
321                 }
322
323                 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
324                         char name[32];
325                         struct app_lcore_params_io *lp_io = NULL;
326                         struct rte_ring *ring;
327                         uint32_t socket_io, lcore_io;
328
329                         if (app.nic_tx_port_mask[port] == 0) {
330                                 continue;
331                         }
332
333                         if (app_get_lcore_for_nic_tx((uint8_t) port, &lcore_io) < 0) {
334                                 rte_panic("Algorithmic error (no I/O core to handle TX of port %u)\n",
335                                         port);
336                         }
337
338                         lp_io = &app.lcore_params[lcore_io].io;
339                         socket_io = rte_lcore_to_socket_id(lcore_io);
340
341                         printf("Creating ring to connect worker lcore %u with TX port %u (through I/O lcore %u) (socket %u) ...\n",
342                                 lcore, port, lcore_io, socket_io);
343                         rte_snprintf(name, sizeof(name), "app_ring_tx_s%u_w%u_p%u", socket_io, lcore, port);
344                         ring = rte_ring_create(
345                                 name,
346                                 app.ring_tx_size,
347                                 socket_io,
348                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
349                         if (ring == NULL) {
350                                 rte_panic("Cannot create ring to connect worker core %u with TX port %u\n",
351                                         lcore,
352                                         port);
353                         }
354
355                         lp_worker->rings_out[port] = ring;
356                         lp_io->tx.rings[port][lp_worker->worker_id] = ring;
357                 }
358         }
359
360         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
361                 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
362                 uint32_t i;
363
364                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
365                     (lp_io->tx.n_nic_ports == 0)) {
366                         continue;
367                 }
368
369                 for (i = 0; i < lp_io->tx.n_nic_ports; i ++){
370                         uint32_t port, j;
371
372                         port = lp_io->tx.nic_ports[i];
373                         for (j = 0; j < app_get_lcores_worker(); j ++) {
374                                 if (lp_io->tx.rings[port][j] == NULL) {
375                                         rte_panic("Algorithmic error (I/O TX rings)\n");
376                                 }
377                         }
378                 }
379         }
380 }
381
382 static void
383 app_init_nics(void)
384 {
385         uint32_t socket, lcore;
386         uint8_t port, queue;
387         int ret;
388
389         /* Init driver */
390         printf("Initializing the PMD driver ...\n");
391 #ifdef RTE_LIBRTE_IGB_PMD
392         if (rte_igb_pmd_init() < 0) {
393                 rte_panic("Cannot init IGB PMD\n");
394         }
395 #endif
396 #ifdef RTE_LIBRTE_IXGBE_PMD
397         if (rte_ixgbe_pmd_init() < 0) {
398                 rte_panic("Cannot init IXGBE PMD\n");
399         }
400 #endif
401         if (rte_eal_pci_probe() < 0) {
402                 rte_panic("Cannot probe PCI\n");
403         }
404
405         /* Init NIC ports and queues, then start the ports */
406         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
407                 struct rte_eth_link link;
408                 struct rte_mempool *pool;
409                 uint32_t n_rx_queues, n_tx_queues;
410
411                 n_rx_queues = app_get_nic_rx_queues_per_port(port);
412                 n_tx_queues = app.nic_tx_port_mask[port];
413
414                 if ((n_rx_queues == 0) && (n_tx_queues == 0)) {
415                         continue;
416                 }
417
418                 /* Init port */
419                 printf("Initializing NIC port %u ...\n", (uint32_t) port);
420                 ret = rte_eth_dev_configure(
421                         port,
422                         (uint8_t) n_rx_queues,
423                         (uint8_t) n_tx_queues,
424                         &port_conf);
425                 if (ret < 0) {
426                         rte_panic("Cannot init NIC port %u (%d)\n", (uint32_t) port, ret);
427                 }
428                 rte_eth_promiscuous_enable(port);
429
430                 /* Init RX queues */
431                 for (queue = 0; queue < APP_MAX_RX_QUEUES_PER_NIC_PORT; queue ++) {
432                         if (app.nic_rx_queue_mask[port][queue] == 0) {
433                                 continue;
434                         }
435
436                         app_get_lcore_for_nic_rx(port, queue, &lcore);
437                         socket = rte_lcore_to_socket_id(lcore);
438                         pool = app.lcore_params[lcore].pool;
439
440                         printf("Initializing NIC port %u RX queue %u ...\n",
441                                 (uint32_t) port,
442                                 (uint32_t) queue);
443                         ret = rte_eth_rx_queue_setup(
444                                 port,
445                                 queue,
446                                 (uint16_t) app.nic_rx_ring_size,
447                                 socket,
448                                 &rx_conf,
449                                 pool);
450                         if (ret < 0) {
451                                 rte_panic("Cannot init RX queue %u for port %u (%d)\n",
452                                         (uint32_t) queue,
453                                         (uint32_t) port,
454                                         ret);
455                         }
456                 }
457
458                 /* Init TX queues */
459                 if (app.nic_tx_port_mask[port] == 1) {
460                         app_get_lcore_for_nic_tx(port, &lcore);
461                         socket = rte_lcore_to_socket_id(lcore);
462                         printf("Initializing NIC port %u TX queue 0 ...\n",
463                                 (uint32_t) port);
464                         ret = rte_eth_tx_queue_setup(
465                                 port,
466                                 0,
467                                 (uint16_t) app.nic_tx_ring_size,
468                                 socket,
469                                 &tx_conf);
470                         if (ret < 0) {
471                                 rte_panic("Cannot init TX queue 0 for port %d (%d)\n",
472                                         port,
473                                         ret);
474                         }
475                 }
476
477                 /* Start port */
478                 ret = rte_eth_dev_start(port);
479                 if (ret < 0) {
480                         rte_panic("Cannot start port %d (%d)\n", port, ret);
481                 }
482
483                 /* Get link status */
484                 rte_eth_link_get(port, &link);
485                 if (link.link_status) {
486                         printf("Port %u is UP (%u Mbps)\n",
487                                 (uint32_t) port,
488                                 (unsigned) link.link_speed);
489                 } else {
490                         printf("Port %u is DOWN\n",
491                                 (uint32_t) port);
492                 }
493         }
494 }
495
496 void
497 app_init(void)
498 {
499         app_assign_worker_ids();
500         app_init_mbuf_pools();
501         app_init_lpm_tables();
502         app_init_rings_rx();
503         app_init_rings_tx();
504         app_init_nics();
505
506         printf("Initialization completed.\n");
507 }