61d71c3ef785dbb6ac7af054afc8edfffe58d7e2
[dpdk.git] / examples / ip_pipeline / init.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 <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_memzone.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_atomic.h>
55 #include <rte_cycles.h>
56 #include <rte_prefetch.h>
57 #include <rte_lcore.h>
58 #include <rte_per_lcore.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_interrupts.h>
61 #include <rte_pci.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_ring.h>
67 #include <rte_mempool.h>
68 #include <rte_malloc.h>
69 #include <rte_mbuf.h>
70 #include <rte_string_fns.h>
71 #include <rte_ip.h>
72 #include <rte_tcp.h>
73 #include <rte_lpm.h>
74 #include <rte_lpm6.h>
75
76 #include "main.h"
77
78 #define NA                             APP_SWQ_INVALID
79
80 struct app_params app = {
81         /* CPU cores */
82         .cores = {
83         {0, APP_CORE_MASTER, {15, 16, 17, NA, NA, NA, NA, NA},
84                 {12, 13, 14, NA, NA, NA, NA, NA} },
85         {0, APP_CORE_RX,     {NA, NA, NA, NA, NA, NA, NA, 12},
86                 { 0,  1,  2,  3, NA, NA, NA, 15} },
87         {0, APP_CORE_FC,     { 0,  1,  2,  3, NA, NA, NA, 13},
88                 { 4,  5,  6,  7, NA, NA, NA, 16} },
89         {0, APP_CORE_RT,     { 4,  5,  6,  7, NA, NA, NA, 14},
90                 { 8,  9, 10, 11, NA, NA, NA, 17} },
91         {0, APP_CORE_TX,     { 8,  9, 10, 11, NA, NA, NA, NA},
92                 {NA, NA, NA, NA, NA, NA, NA, NA} },
93         },
94
95         /* Ports*/
96         .n_ports = APP_MAX_PORTS,
97         .rsz_hwq_rx = 128,
98         .rsz_hwq_tx = 512,
99         .bsz_hwq_rd = 64,
100         .bsz_hwq_wr = 64,
101
102         .port_conf = {
103                 .rxmode = {
104                         .split_hdr_size = 0,
105                         .header_split   = 0, /* Header Split disabled */
106                         .hw_ip_checksum = 1, /* IP checksum offload enabled */
107                         .hw_vlan_filter = 0, /* VLAN filtering disabled */
108                         .jumbo_frame    = 1, /* Jumbo Frame Support enabled */
109                         .max_rx_pkt_len = 9000, /* Jumbo Frame MAC pkt length */
110                         .hw_strip_crc   = 0, /* CRC stripped by hardware */
111                 },
112                 .rx_adv_conf = {
113                         .rss_conf = {
114                                 .rss_key = NULL,
115                                 .rss_hf = ETH_RSS_IP,
116                         },
117                 },
118                 .txmode = {
119                         .mq_mode = ETH_MQ_TX_NONE,
120                 },
121         },
122
123         .rx_conf = {
124                 .rx_thresh = {
125                         .pthresh = 8,
126                         .hthresh = 8,
127                         .wthresh = 4,
128                 },
129                 .rx_free_thresh = 64,
130                 .rx_drop_en = 0,
131         },
132
133         .tx_conf = {
134                 .tx_thresh = {
135                         .pthresh = 36,
136                         .hthresh = 0,
137                         .wthresh = 0,
138                 },
139                 .tx_free_thresh = 0,
140                 .tx_rs_thresh = 0,
141         },
142
143         /* SWQs */
144         .rsz_swq = 128,
145         .bsz_swq_rd = 64,
146         .bsz_swq_wr = 64,
147
148         /* Buffer pool */
149         .pool_buffer_size = 2048 + sizeof(struct rte_mbuf) +
150                 RTE_PKTMBUF_HEADROOM,
151         .pool_size = 32 * 1024,
152         .pool_cache_size = 256,
153
154         /* Message buffer pool */
155         .msg_pool_buffer_size = 256,
156         .msg_pool_size = 1024,
157         .msg_pool_cache_size = 64,
158
159         /* Rule tables */
160         .max_arp_rules = 1 << 10,
161         .max_firewall_rules = 1 << 5,
162         .max_routing_rules = 1 << 24,
163         .max_flow_rules = 1 << 24,
164
165         /* Application processing */
166         .ether_hdr_pop_push = 0,
167 };
168
169 struct app_core_params *
170 app_get_core_params(uint32_t core_id)
171 {
172         uint32_t i;
173
174         for (i = 0; i < RTE_MAX_LCORE; i++) {
175                 struct app_core_params *p = &app.cores[i];
176
177                 if (p->core_id != core_id)
178                         continue;
179
180                 return p;
181         }
182
183         return NULL;
184 }
185
186 static uint32_t
187 app_get_n_swq_in(void)
188 {
189         uint32_t max_swq_id = 0, i, j;
190
191         for (i = 0; i < RTE_MAX_LCORE; i++) {
192                 struct app_core_params *p = &app.cores[i];
193
194                 if (p->core_type == APP_CORE_NONE)
195                         continue;
196
197                 for (j = 0; j < APP_MAX_SWQ_PER_CORE; j++) {
198                         uint32_t swq_id = p->swq_in[j];
199
200                         if ((swq_id != APP_SWQ_INVALID) &&
201                                 (swq_id > max_swq_id))
202                                 max_swq_id = swq_id;
203                 }
204         }
205
206         return (1 + max_swq_id);
207 }
208
209 static uint32_t
210 app_get_n_swq_out(void)
211 {
212         uint32_t max_swq_id = 0, i, j;
213
214         for (i = 0; i < RTE_MAX_LCORE; i++) {
215                 struct app_core_params *p = &app.cores[i];
216
217                 if (p->core_type == APP_CORE_NONE)
218                         continue;
219
220                 for (j = 0; j < APP_MAX_SWQ_PER_CORE; j++) {
221                         uint32_t swq_id = p->swq_out[j];
222
223                         if ((swq_id != APP_SWQ_INVALID) &&
224                                 (swq_id > max_swq_id))
225                                 max_swq_id = swq_id;
226                 }
227         }
228
229         return (1 + max_swq_id);
230 }
231
232 static uint32_t
233 app_get_swq_in_count(uint32_t swq_id)
234 {
235         uint32_t n, i;
236
237         for (n = 0, i = 0; i < RTE_MAX_LCORE; i++) {
238                 struct app_core_params *p = &app.cores[i];
239                 uint32_t j;
240
241                 if (p->core_type == APP_CORE_NONE)
242                         continue;
243
244                 for (j = 0; j < APP_MAX_SWQ_PER_CORE; j++)
245                         if (p->swq_in[j] == swq_id)
246                                 n++;
247         }
248
249         return n;
250 }
251
252 static uint32_t
253 app_get_swq_out_count(uint32_t swq_id)
254 {
255         uint32_t n, i;
256
257         for (n = 0, i = 0; i < RTE_MAX_LCORE; i++) {
258                 struct app_core_params *p = &app.cores[i];
259                 uint32_t j;
260
261                 if (p->core_type == APP_CORE_NONE)
262                         continue;
263
264                 for (j = 0; j < APP_MAX_SWQ_PER_CORE; j++)
265                         if (p->swq_out[j] == swq_id)
266                                 n++;
267         }
268
269         return n;
270 }
271
272 void
273 app_check_core_params(void)
274 {
275         uint32_t n_swq_in = app_get_n_swq_in();
276         uint32_t n_swq_out = app_get_n_swq_out();
277         uint32_t i;
278
279         /* Check that range of SW queues is contiguous and each SW queue has
280            exactly one reader and one writer */
281         if (n_swq_in != n_swq_out)
282                 rte_panic("Number of input SW queues is not equal to the "
283                         "number of output SW queues\n");
284
285         for (i = 0; i < n_swq_in; i++) {
286                 uint32_t n = app_get_swq_in_count(i);
287
288                 if (n == 0)
289                         rte_panic("SW queue %u has no reader\n", i);
290
291                 if (n > 1)
292                         rte_panic("SW queue %u has more than one reader\n", i);
293         }
294
295         for (i = 0; i < n_swq_out; i++) {
296                 uint32_t n = app_get_swq_out_count(i);
297
298                 if (n == 0)
299                         rte_panic("SW queue %u has no writer\n", i);
300
301                 if (n > 1)
302                         rte_panic("SW queue %u has more than one writer\n", i);
303         }
304
305         /* Check the request and response queues are valid */
306         for (i = 0; i < RTE_MAX_LCORE; i++) {
307                 struct app_core_params *p = &app.cores[i];
308                 uint32_t ring_id_req, ring_id_resp;
309
310                 if ((p->core_type != APP_CORE_FC) &&
311                     (p->core_type != APP_CORE_FW) &&
312                         (p->core_type != APP_CORE_RT)) {
313                         continue;
314                 }
315
316                 ring_id_req = p->swq_in[APP_SWQ_IN_REQ];
317                 if (ring_id_req == APP_SWQ_INVALID)
318                         rte_panic("Core %u of type %u has invalid request "
319                                 "queue ID\n", p->core_id, p->core_type);
320
321                 ring_id_resp = p->swq_out[APP_SWQ_OUT_RESP];
322                 if (ring_id_resp == APP_SWQ_INVALID)
323                         rte_panic("Core %u of type %u has invalid response "
324                                 "queue ID\n", p->core_id, p->core_type);
325         }
326
327         return;
328 }
329
330 uint32_t
331 app_get_first_core_id(enum app_core_type core_type)
332 {
333         uint32_t i;
334
335         for (i = 0; i < RTE_MAX_LCORE; i++) {
336                 struct app_core_params *p = &app.cores[i];
337
338                 if (p->core_type == core_type)
339                         return p->core_id;
340         }
341
342         return RTE_MAX_LCORE;
343 }
344
345 struct rte_ring *
346 app_get_ring_req(uint32_t core_id)
347 {
348         struct app_core_params *p = app_get_core_params(core_id);
349         uint32_t ring_req_id = p->swq_in[APP_SWQ_IN_REQ];
350
351         return app.rings[ring_req_id];
352 }
353
354 struct rte_ring *
355 app_get_ring_resp(uint32_t core_id)
356 {
357         struct app_core_params *p = app_get_core_params(core_id);
358         uint32_t ring_resp_id = p->swq_out[APP_SWQ_OUT_RESP];
359
360         return app.rings[ring_resp_id];
361 }
362
363 static void
364 app_init_mbuf_pools(void)
365 {
366         struct rte_pktmbuf_pool_private indirect_mbp_priv;
367
368         /* Init the buffer pool */
369         RTE_LOG(INFO, USER1, "Creating the mbuf pool ...\n");
370         app.pool = rte_mempool_create(
371                 "mempool",
372                 app.pool_size,
373                 app.pool_buffer_size,
374                 app.pool_cache_size,
375                 sizeof(struct rte_pktmbuf_pool_private),
376                 rte_pktmbuf_pool_init, NULL,
377                 rte_pktmbuf_init, NULL,
378                 rte_socket_id(),
379                 0);
380         if (app.pool == NULL)
381                 rte_panic("Cannot create mbuf pool\n");
382
383         /* Init the indirect buffer pool */
384         RTE_LOG(INFO, USER1, "Creating the indirect mbuf pool ...\n");
385         indirect_mbp_priv.mbuf_data_room_size = 0;
386         indirect_mbp_priv.mbuf_priv_size = sizeof(struct app_pkt_metadata);
387         app.indirect_pool = rte_mempool_create(
388                 "indirect mempool",
389                 app.pool_size,
390                 sizeof(struct rte_mbuf) + sizeof(struct app_pkt_metadata),
391                 app.pool_cache_size,
392                 sizeof(struct rte_pktmbuf_pool_private),
393                 rte_pktmbuf_pool_init, &indirect_mbp_priv,
394                 rte_pktmbuf_init, NULL,
395                 rte_socket_id(),
396                 0);
397         if (app.indirect_pool == NULL)
398                 rte_panic("Cannot create mbuf pool\n");
399
400         /* Init the message buffer pool */
401         RTE_LOG(INFO, USER1, "Creating the message pool ...\n");
402         app.msg_pool = rte_mempool_create(
403                 "mempool msg",
404                 app.msg_pool_size,
405                 app.msg_pool_buffer_size,
406                 app.msg_pool_cache_size,
407                 0,
408                 NULL, NULL,
409                 rte_ctrlmbuf_init, NULL,
410                 rte_socket_id(),
411                 0);
412         if (app.msg_pool == NULL)
413                 rte_panic("Cannot create message pool\n");
414 }
415
416 static void
417 app_init_rings(void)
418 {
419         uint32_t n_swq, i;
420
421         n_swq = app_get_n_swq_in();
422         RTE_LOG(INFO, USER1, "Initializing %u SW rings ...\n", n_swq);
423
424         app.rings = rte_malloc_socket(NULL, n_swq * sizeof(struct rte_ring *),
425                 RTE_CACHE_LINE_SIZE, rte_socket_id());
426         if (app.rings == NULL)
427                 rte_panic("Cannot allocate memory to store ring pointers\n");
428
429         for (i = 0; i < n_swq; i++) {
430                 struct rte_ring *ring;
431                 char name[32];
432
433                 snprintf(name, sizeof(name), "app_ring_%u", i);
434
435                 ring = rte_ring_create(
436                         name,
437                         app.rsz_swq,
438                         rte_socket_id(),
439                         RING_F_SP_ENQ | RING_F_SC_DEQ);
440
441                 if (ring == NULL)
442                         rte_panic("Cannot create ring %u\n", i);
443
444                 app.rings[i] = ring;
445         }
446 }
447
448 static void
449 app_ports_check_link(void)
450 {
451         uint32_t all_ports_up, i;
452
453         all_ports_up = 1;
454
455         for (i = 0; i < app.n_ports; i++) {
456                 struct rte_eth_link link;
457                 uint32_t port;
458
459                 port = app.ports[i];
460                 memset(&link, 0, sizeof(link));
461                 rte_eth_link_get_nowait(port, &link);
462                 RTE_LOG(INFO, USER1, "Port %u (%u Gbps) %s\n",
463                         port,
464                         link.link_speed / 1000,
465                         link.link_status ? "UP" : "DOWN");
466
467                 if (link.link_status == 0)
468                         all_ports_up = 0;
469         }
470
471         if (all_ports_up == 0)
472                 rte_panic("Some NIC ports are DOWN\n");
473 }
474
475 static void
476 app_init_ports(void)
477 {
478         uint32_t i;
479
480         /* Init NIC ports, then start the ports */
481         for (i = 0; i < app.n_ports; i++) {
482                 uint32_t port;
483                 int ret;
484
485                 port = app.ports[i];
486                 RTE_LOG(INFO, USER1, "Initializing NIC port %u ...\n", port);
487
488                 /* Init port */
489                 ret = rte_eth_dev_configure(
490                         port,
491                         1,
492                         1,
493                         &app.port_conf);
494                 if (ret < 0)
495                         rte_panic("Cannot init NIC port %u (%d)\n", port, ret);
496                 rte_eth_promiscuous_enable(port);
497
498                 /* Init RX queues */
499                 ret = rte_eth_rx_queue_setup(
500                         port,
501                         0,
502                         app.rsz_hwq_rx,
503                         rte_eth_dev_socket_id(port),
504                         &app.rx_conf,
505                         app.pool);
506                 if (ret < 0)
507                         rte_panic("Cannot init RX for port %u (%d)\n",
508                                 (uint32_t) port, ret);
509
510                 /* Init TX queues */
511                 ret = rte_eth_tx_queue_setup(
512                         port,
513                         0,
514                         app.rsz_hwq_tx,
515                         rte_eth_dev_socket_id(port),
516                         &app.tx_conf);
517                 if (ret < 0)
518                         rte_panic("Cannot init TX for port %u (%d)\n", port,
519                                 ret);
520
521                 /* Start port */
522                 ret = rte_eth_dev_start(port);
523                 if (ret < 0)
524                         rte_panic("Cannot start port %u (%d)\n", port, ret);
525         }
526
527         app_ports_check_link();
528 }
529
530 #define APP_PING_TIMEOUT_SEC                               5
531
532 void
533 app_ping(void)
534 {
535         unsigned i;
536         uint64_t timestamp, diff_tsc;
537
538         const uint64_t timeout = rte_get_tsc_hz() * APP_PING_TIMEOUT_SEC;
539
540         for (i = 0; i < RTE_MAX_LCORE; i++) {
541                 struct app_core_params *p = &app.cores[i];
542                 struct rte_ring *ring_req, *ring_resp;
543                 void *msg;
544                 struct app_msg_req *req;
545                 int status;
546
547                 if ((p->core_type != APP_CORE_FC) &&
548                     (p->core_type != APP_CORE_FW) &&
549                         (p->core_type != APP_CORE_RT) &&
550                         (p->core_type != APP_CORE_RX))
551                         continue;
552
553                 ring_req = app_get_ring_req(p->core_id);
554                 ring_resp = app_get_ring_resp(p->core_id);
555
556                 /* Fill request message */
557                 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
558                 if (msg == NULL)
559                         rte_panic("Unable to allocate new message\n");
560
561                 req = (struct app_msg_req *)
562                                 rte_ctrlmbuf_data((struct rte_mbuf *)msg);
563                 req->type = APP_MSG_REQ_PING;
564
565                 /* Send request */
566                 do {
567                         status = rte_ring_sp_enqueue(ring_req, msg);
568                 } while (status == -ENOBUFS);
569
570                 /* Wait for response */
571                 timestamp = rte_rdtsc();
572                 do {
573                         status = rte_ring_sc_dequeue(ring_resp, &msg);
574                         diff_tsc = rte_rdtsc() - timestamp;
575
576                         if (unlikely(diff_tsc > timeout))
577                                 rte_panic("Core %u of type %d does not respond "
578                                         "to requests\n", p->core_id,
579                                         p->core_type);
580                 } while (status != 0);
581
582                 /* Free message buffer */
583                 rte_ctrlmbuf_free(msg);
584         }
585 }
586
587 static void
588 app_init_etc(void)
589 {
590         if ((app_get_first_core_id(APP_CORE_IPV4_FRAG) != RTE_MAX_LCORE) ||
591                 (app_get_first_core_id(APP_CORE_IPV4_RAS) != RTE_MAX_LCORE)) {
592                 RTE_LOG(INFO, USER1,
593                         "Activating the Ethernet header pop/push ...\n");
594                 app.ether_hdr_pop_push = 1;
595         }
596 }
597
598 void
599 app_init(void)
600 {
601         if ((sizeof(struct app_pkt_metadata) % RTE_CACHE_LINE_SIZE) != 0)
602                 rte_panic("Application pkt meta-data size mismatch\n");
603
604         app_check_core_params();
605
606         app_init_mbuf_pools();
607         app_init_rings();
608         app_init_ports();
609         app_init_etc();
610
611         RTE_LOG(INFO, USER1, "Initialization completed\n");
612 }