tailq: remove unneeded inclusions
[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         /* Init the buffer pool */
367         RTE_LOG(INFO, USER1, "Creating the mbuf pool ...\n");
368         app.pool = rte_mempool_create(
369                 "mempool",
370                 app.pool_size,
371                 app.pool_buffer_size,
372                 app.pool_cache_size,
373                 sizeof(struct rte_pktmbuf_pool_private),
374                 rte_pktmbuf_pool_init, NULL,
375                 rte_pktmbuf_init, NULL,
376                 rte_socket_id(),
377                 0);
378         if (app.pool == NULL)
379                 rte_panic("Cannot create mbuf pool\n");
380
381         /* Init the indirect buffer pool */
382         RTE_LOG(INFO, USER1, "Creating the indirect mbuf pool ...\n");
383         app.indirect_pool = rte_mempool_create(
384                 "indirect mempool",
385                 app.pool_size,
386                 sizeof(struct rte_mbuf) + sizeof(struct app_pkt_metadata),
387                 app.pool_cache_size,
388                 0,
389                 NULL, NULL,
390                 rte_pktmbuf_init, NULL,
391                 rte_socket_id(),
392                 0);
393         if (app.indirect_pool == NULL)
394                 rte_panic("Cannot create mbuf pool\n");
395
396         /* Init the message buffer pool */
397         RTE_LOG(INFO, USER1, "Creating the message pool ...\n");
398         app.msg_pool = rte_mempool_create(
399                 "mempool msg",
400                 app.msg_pool_size,
401                 app.msg_pool_buffer_size,
402                 app.msg_pool_cache_size,
403                 0,
404                 NULL, NULL,
405                 rte_ctrlmbuf_init, NULL,
406                 rte_socket_id(),
407                 0);
408         if (app.msg_pool == NULL)
409                 rte_panic("Cannot create message pool\n");
410 }
411
412 static void
413 app_init_rings(void)
414 {
415         uint32_t n_swq, i;
416
417         n_swq = app_get_n_swq_in();
418         RTE_LOG(INFO, USER1, "Initializing %u SW rings ...\n", n_swq);
419
420         app.rings = rte_malloc_socket(NULL, n_swq * sizeof(struct rte_ring *),
421                 RTE_CACHE_LINE_SIZE, rte_socket_id());
422         if (app.rings == NULL)
423                 rte_panic("Cannot allocate memory to store ring pointers\n");
424
425         for (i = 0; i < n_swq; i++) {
426                 struct rte_ring *ring;
427                 char name[32];
428
429                 snprintf(name, sizeof(name), "app_ring_%u", i);
430
431                 ring = rte_ring_create(
432                         name,
433                         app.rsz_swq,
434                         rte_socket_id(),
435                         RING_F_SP_ENQ | RING_F_SC_DEQ);
436
437                 if (ring == NULL)
438                         rte_panic("Cannot create ring %u\n", i);
439
440                 app.rings[i] = ring;
441         }
442 }
443
444 static void
445 app_ports_check_link(void)
446 {
447         uint32_t all_ports_up, i;
448
449         all_ports_up = 1;
450
451         for (i = 0; i < app.n_ports; i++) {
452                 struct rte_eth_link link;
453                 uint32_t port;
454
455                 port = app.ports[i];
456                 memset(&link, 0, sizeof(link));
457                 rte_eth_link_get_nowait(port, &link);
458                 RTE_LOG(INFO, USER1, "Port %u (%u Gbps) %s\n",
459                         port,
460                         link.link_speed / 1000,
461                         link.link_status ? "UP" : "DOWN");
462
463                 if (link.link_status == 0)
464                         all_ports_up = 0;
465         }
466
467         if (all_ports_up == 0)
468                 rte_panic("Some NIC ports are DOWN\n");
469 }
470
471 static void
472 app_init_ports(void)
473 {
474         uint32_t i;
475
476         /* Init NIC ports, then start the ports */
477         for (i = 0; i < app.n_ports; i++) {
478                 uint32_t port;
479                 int ret;
480
481                 port = app.ports[i];
482                 RTE_LOG(INFO, USER1, "Initializing NIC port %u ...\n", port);
483
484                 /* Init port */
485                 ret = rte_eth_dev_configure(
486                         port,
487                         1,
488                         1,
489                         &app.port_conf);
490                 if (ret < 0)
491                         rte_panic("Cannot init NIC port %u (%d)\n", port, ret);
492                 rte_eth_promiscuous_enable(port);
493
494                 /* Init RX queues */
495                 ret = rte_eth_rx_queue_setup(
496                         port,
497                         0,
498                         app.rsz_hwq_rx,
499                         rte_eth_dev_socket_id(port),
500                         &app.rx_conf,
501                         app.pool);
502                 if (ret < 0)
503                         rte_panic("Cannot init RX for port %u (%d)\n",
504                                 (uint32_t) port, ret);
505
506                 /* Init TX queues */
507                 ret = rte_eth_tx_queue_setup(
508                         port,
509                         0,
510                         app.rsz_hwq_tx,
511                         rte_eth_dev_socket_id(port),
512                         &app.tx_conf);
513                 if (ret < 0)
514                         rte_panic("Cannot init TX for port %u (%d)\n", port,
515                                 ret);
516
517                 /* Start port */
518                 ret = rte_eth_dev_start(port);
519                 if (ret < 0)
520                         rte_panic("Cannot start port %u (%d)\n", port, ret);
521         }
522
523         app_ports_check_link();
524 }
525
526 #define APP_PING_TIMEOUT_SEC                               5
527
528 void
529 app_ping(void)
530 {
531         unsigned i;
532         uint64_t timestamp, diff_tsc;
533
534         const uint64_t timeout = rte_get_tsc_hz() * APP_PING_TIMEOUT_SEC;
535
536         for (i = 0; i < RTE_MAX_LCORE; i++) {
537                 struct app_core_params *p = &app.cores[i];
538                 struct rte_ring *ring_req, *ring_resp;
539                 void *msg;
540                 struct app_msg_req *req;
541                 int status;
542
543                 if ((p->core_type != APP_CORE_FC) &&
544                     (p->core_type != APP_CORE_FW) &&
545                         (p->core_type != APP_CORE_RT) &&
546                         (p->core_type != APP_CORE_RX))
547                         continue;
548
549                 ring_req = app_get_ring_req(p->core_id);
550                 ring_resp = app_get_ring_resp(p->core_id);
551
552                 /* Fill request message */
553                 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
554                 if (msg == NULL)
555                         rte_panic("Unable to allocate new message\n");
556
557                 req = (struct app_msg_req *)
558                                 rte_ctrlmbuf_data((struct rte_mbuf *)msg);
559                 req->type = APP_MSG_REQ_PING;
560
561                 /* Send request */
562                 do {
563                         status = rte_ring_sp_enqueue(ring_req, msg);
564                 } while (status == -ENOBUFS);
565
566                 /* Wait for response */
567                 timestamp = rte_rdtsc();
568                 do {
569                         status = rte_ring_sc_dequeue(ring_resp, &msg);
570                         diff_tsc = rte_rdtsc() - timestamp;
571
572                         if (unlikely(diff_tsc > timeout))
573                                 rte_panic("Core %u of type %d does not respond "
574                                         "to requests\n", p->core_id,
575                                         p->core_type);
576                 } while (status != 0);
577
578                 /* Free message buffer */
579                 rte_ctrlmbuf_free(msg);
580         }
581 }
582
583 static void
584 app_init_etc(void)
585 {
586         if ((app_get_first_core_id(APP_CORE_IPV4_FRAG) != RTE_MAX_LCORE) ||
587                 (app_get_first_core_id(APP_CORE_IPV4_RAS) != RTE_MAX_LCORE)) {
588                 RTE_LOG(INFO, USER1,
589                         "Activating the Ethernet header pop/push ...\n");
590                 app.ether_hdr_pop_push = 1;
591         }
592 }
593
594 void
595 app_init(void)
596 {
597         if ((sizeof(struct app_pkt_metadata) % RTE_CACHE_LINE_SIZE) != 0)
598                 rte_panic("Application pkt meta-data size mismatch\n");
599
600         app_check_core_params();
601
602         app_init_mbuf_pools();
603         app_init_rings();
604         app_init_ports();
605         app_init_etc();
606
607         RTE_LOG(INFO, USER1, "Initialization completed\n");
608 }