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