remove version in all files
[dpdk.git] / examples / multi_process / symmetric_mp / main.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  */
34
35 /*
36  * Sample application demostrating how to do packet I/O in a multi-process
37  * environment. The same code can be run as a primary process and as a
38  * secondary process, just with a different proc-id parameter in each case
39  * (apart from the EAL flag to indicate a secondary process).
40  *
41  * Each process will read from the same ports, given by the port-mask
42  * parameter, which should be the same in each case, just using a different
43  * queue per port as determined by the proc-id parameter.
44  */
45
46 #include <stdio.h>
47 #include <string.h>
48 #include <stdint.h>
49 #include <stdlib.h>
50 #include <stdarg.h>
51 #include <errno.h>
52 #include <sys/queue.h>
53 #include <getopt.h>
54 #include <signal.h>
55 #include <inttypes.h>
56
57 #include <rte_common.h>
58 #include <rte_log.h>
59 #include <rte_memory.h>
60 #include <rte_memzone.h>
61 #include <rte_launch.h>
62 #include <rte_tailq.h>
63 #include <rte_eal.h>
64 #include <rte_per_lcore.h>
65 #include <rte_lcore.h>
66 #include <rte_debug.h>
67 #include <rte_atomic.h>
68 #include <rte_branch_prediction.h>
69 #include <rte_ring.h>
70 #include <rte_debug.h>
71 #include <rte_interrupts.h>
72 #include <rte_pci.h>
73 #include <rte_ether.h>
74 #include <rte_ethdev.h>
75 #include <rte_mempool.h>
76 #include <rte_memcpy.h>
77 #include <rte_mbuf.h>
78 #include <rte_string_fns.h>
79
80 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
81
82 #define SOCKET0 0
83
84 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
85 #define NB_MBUFS 64*1024 /* use 64k mbufs */
86 #define MBUF_CACHE_SIZE 256
87 #define PKT_BURST 32
88 #define RX_RING_SIZE 128
89 #define TX_RING_SIZE 512
90
91 #define PARAM_PROC_ID "proc-id"
92 #define PARAM_NUM_PROCS "num-procs"
93
94 /*
95  * RX and TX Prefetch, Host, and Write-back threshold values should be
96  * carefully set for optimal performance. Consult the network
97  * controller's datasheet and supporting DPDK documentation for guidance
98  * on how these parameters should be set.
99  */
100 /* Default configuration for rx and tx thresholds etc. */
101 static const struct rte_eth_rxconf rx_conf_default = {
102         .rx_thresh = {
103                 .pthresh = 8,
104                 .hthresh = 8,
105                 .wthresh = 4,
106         },
107 };
108
109 /*
110  * These default values are optimized for use with the Intel(R) 82599 10 GbE
111  * Controller and the DPDK ixgbe PMD. Consider using other values for other
112  * network controllers and/or network drivers.
113  */
114 static const struct rte_eth_txconf tx_conf_default = {
115         .tx_thresh = {
116                 .pthresh = 36,
117                 .hthresh = 0,
118                 .wthresh = 0,
119         },
120         .tx_free_thresh = 0, /* Use PMD default values */
121         .tx_rs_thresh = 0, /* Use PMD default values */
122 };
123
124 /* for each lcore, record the elements of the ports array to use */
125 struct lcore_ports{
126         unsigned start_port;
127         unsigned num_ports;
128 };
129
130 /* structure to record the rx and tx packets. Put two per cache line as ports
131  * used in pairs */
132 struct port_stats{
133         unsigned rx;
134         unsigned tx;
135         unsigned drop;
136 } __attribute__((aligned(CACHE_LINE_SIZE / 2)));
137
138 static int proc_id = -1;
139 static unsigned num_procs = 0;
140
141 static uint8_t ports[RTE_MAX_ETHPORTS];
142 static unsigned num_ports = 0;
143
144 static struct lcore_ports lcore_ports[RTE_MAX_LCORE];
145 static struct port_stats pstats[RTE_MAX_ETHPORTS];
146
147 /* prints the usage statement and quits with an error message */
148 static void
149 smp_usage(const char *prgname, const char *errmsg)
150 {
151         printf("\nError: %s\n",errmsg);
152         printf("\n%s [EAL options] -- -p <port mask> "
153                         "--"PARAM_NUM_PROCS" <n>"
154                         " --"PARAM_PROC_ID" <id>\n"
155                         "-p         : a hex bitmask indicating what ports are to be used\n"
156                         "--num-procs: the number of processes which will be used\n"
157                         "--proc-id  : the id of the current process (id < num-procs)\n"
158                         "\n",
159                         prgname);
160         exit(1);
161 }
162
163
164 /* signal handler configured for SIGTERM and SIGINT to print stats on exit */
165 static void
166 print_stats(int signum)
167 {
168         unsigned i;
169         printf("\nExiting on signal %d\n\n", signum);
170         for (i = 0; i < num_ports; i++){
171                 const uint8_t p_num = ports[i];
172                 printf("Port %u: RX - %u, TX - %u, Drop - %u\n", (unsigned)p_num,
173                                 pstats[p_num].rx, pstats[p_num].tx, pstats[p_num].drop);
174         }
175         exit(0);
176 }
177
178 /* Parse the argument given in the command line of the application */
179 static int
180 smp_parse_args(int argc, char **argv)
181 {
182         int opt, ret;
183         char **argvopt;
184         int option_index;
185         unsigned i, port_mask = 0;
186         char *prgname = argv[0];
187         static struct option lgopts[] = {
188                         {PARAM_NUM_PROCS, 1, 0, 0},
189                         {PARAM_PROC_ID, 1, 0, 0},
190                         {NULL, 0, 0, 0}
191         };
192
193         argvopt = argv;
194
195         while ((opt = getopt_long(argc, argvopt, "p:", \
196                         lgopts, &option_index)) != EOF) {
197
198                 switch (opt) {
199                 case 'p':
200                         port_mask = strtoull(optarg, NULL, 16);
201                         break;
202                         /* long options */
203                 case 0:
204                         if (strncmp(lgopts[option_index].name, PARAM_NUM_PROCS, 8) == 0)
205                                 num_procs = atoi(optarg);
206                         else if (strncmp(lgopts[option_index].name, PARAM_PROC_ID, 7) == 0)
207                                 proc_id = atoi(optarg);
208                         break;
209
210                 default:
211                         smp_usage(prgname, "Cannot parse all command-line arguments\n");
212                 }
213         }
214
215         if (optind >= 0)
216                 argv[optind-1] = prgname;
217
218         if (proc_id < 0)
219                 smp_usage(prgname, "Invalid or missing proc-id parameter\n");
220         if (rte_eal_process_type() == RTE_PROC_PRIMARY && num_procs == 0)
221                 smp_usage(prgname, "Invalid or missing num-procs parameter\n");
222         if (port_mask == 0)
223                 smp_usage(prgname, "Invalid or missing port mask\n");
224
225         /* get the port numbers from the port mask */
226         for(i = 0; i < rte_eth_dev_count(); i++)
227                 if(port_mask & (1 << i))
228                         ports[num_ports++] = (uint8_t)i;
229
230         ret = optind-1;
231         optind = 0; /* reset getopt lib */
232
233         return (ret);
234 }
235
236 /* Queries the link status of a port and prints it to screen */
237 static void
238 report_link_status(uint8_t port)
239 {
240         /* get link status */
241         struct rte_eth_link link;
242         rte_eth_link_get(port, &link);
243         if (link.link_status)
244                 printf("Port %u: Link Up - %u Gbps - %s\n", (unsigned)port,
245                                 (unsigned) link.link_speed / 1000,
246                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
247                                                 ("full-duplex") : ("half-duplex\n"));
248         else
249                 printf("Port %u: Link Down\n", (unsigned)port);
250 }
251
252 /*
253  * Initialises a given port using global settings and with the rx buffers
254  * coming from the mbuf_pool passed as parameter
255  */
256 static inline int
257 smp_port_init(uint8_t port, struct rte_mempool *mbuf_pool, uint16_t num_queues)
258 {
259         struct rte_eth_conf port_conf = {
260                         .rxmode = {
261                                 .mq_mode = ETH_RSS,
262                                 .split_hdr_size = 0,
263                                 .header_split   = 0, /**< Header Split disabled */
264                                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
265                                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
266                                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
267                                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
268                         },
269                         .rx_adv_conf = {
270                                 .rss_conf = {
271                                         .rss_key = NULL,
272                                         .rss_hf = ETH_RSS_IPV4,
273                                 },
274                         },
275                         .txmode = {
276                         }
277         };
278         const uint16_t rx_rings = num_queues, tx_rings = num_queues;
279         int retval;
280         uint16_t q;
281
282         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
283                 return 0;
284
285         if (port >= rte_eth_dev_count())
286                 return -1;
287
288         printf("# Initialising port %u... ", (unsigned)port);
289         fflush(stdout);
290
291         retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
292         if (retval < 0)
293                 return retval;
294
295         for (q = 0; q < rx_rings; q ++) {
296                 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
297                                 SOCKET0, &rx_conf_default,
298                                 mbuf_pool);
299                 if (retval < 0)
300                         return retval;
301         }
302
303         for (q = 0; q < tx_rings; q ++) {
304                 retval = rte_eth_tx_queue_setup(port, q, RX_RING_SIZE,
305                                 SOCKET0, &tx_conf_default);
306                 if (retval < 0)
307                         return retval;
308         }
309
310         rte_eth_promiscuous_enable(port);
311
312         retval  = rte_eth_dev_start(port);
313         if (retval < 0)
314                 return retval;
315
316         return 0;
317 }
318
319 /* Goes through each of the lcores and calculates what ports should
320  * be used by that core. Fills in the global lcore_ports[] array.
321  */
322 static void
323 assign_ports_to_cores(void)
324 {
325
326         const unsigned lcores = rte_eal_get_configuration()->lcore_count;
327         const unsigned port_pairs = num_ports / 2;
328         const unsigned pairs_per_lcore = port_pairs / lcores;
329         unsigned extra_pairs = port_pairs % lcores;
330         unsigned ports_assigned = 0;
331         unsigned i;
332
333         RTE_LCORE_FOREACH(i) {
334                 lcore_ports[i].start_port = ports_assigned;
335                 lcore_ports[i].num_ports = pairs_per_lcore * 2;
336                 if (extra_pairs > 0) {
337                         lcore_ports[i].num_ports += 2;
338                         extra_pairs--;
339                 }
340                 ports_assigned += lcore_ports[i].num_ports;
341         }
342 }
343
344 /* Main function used by the processing threads.
345  * Prints out some configuration details for the thread and then begins
346  * performing packet RX and TX.
347  */
348 static int
349 lcore_main(void *arg __rte_unused)
350 {
351         const unsigned id = rte_lcore_id();
352         const unsigned start_port = lcore_ports[id].start_port;
353         const unsigned end_port = start_port + lcore_ports[id].num_ports;
354         const uint16_t q_id = (uint16_t)proc_id;
355         unsigned p, i;
356         char msgbuf[256];
357         int msgbufpos = 0;
358
359         if (start_port == end_port){
360                 printf("Lcore %u has nothing to do\n", id);
361                 return 0;
362         }
363
364         /* build up message in msgbuf before printing to decrease likelihood
365          * of multi-core message interleaving.
366          */
367         msgbufpos += rte_snprintf(msgbuf, sizeof(msgbuf) - msgbufpos,
368                         "Lcore %u using ports ", id);
369         for (p = start_port; p < end_port; p++){
370                 msgbufpos += rte_snprintf(msgbuf + msgbufpos, sizeof(msgbuf) - msgbufpos,
371                                 "%u ", (unsigned)ports[p]);
372         }
373         printf("%s\n", msgbuf);
374         printf("lcore %u using queue %u of each port\n", id, (unsigned)q_id);
375
376         /* handle packet I/O from the ports, reading and writing to the
377          * queue number corresponding to our process number (not lcore id)
378          */
379
380         for (;;) {
381                 struct rte_mbuf *buf[PKT_BURST];
382
383                 for (p = start_port; p < end_port; p++) {
384                         const uint8_t src = ports[p];
385                         const uint8_t dst = ports[p ^ 1]; /* 0 <-> 1, 2 <-> 3 etc */
386                         const uint16_t rx_c = rte_eth_rx_burst(src, q_id, buf, PKT_BURST);
387                         if (rx_c == 0)
388                                 continue;
389                         pstats[src].rx += rx_c;
390
391                         const uint16_t tx_c = rte_eth_tx_burst(dst, q_id, buf, rx_c);
392                         pstats[dst].tx += tx_c;
393                         if (tx_c != rx_c) {
394                                 pstats[dst].drop += (rx_c - tx_c);
395                                 for (i = tx_c; i < rx_c; i++)
396                                         rte_pktmbuf_free(buf[i]);
397                         }
398                 }
399         }
400 }
401
402 /* Main function.
403  * Performs initialisation and then calls the lcore_main on each core
404  * to do the packet-processing work.
405  */
406 int
407 main(int argc, char **argv)
408 {
409         static const char *_SMP_MBUF_POOL = "SMP_MBUF_POOL";
410         int ret;
411         unsigned i;
412         enum rte_proc_type_t proc_type;
413         struct rte_mempool *mp;
414
415         /* set up signal handlers to print stats on exit */
416         signal(SIGINT, print_stats);
417         signal(SIGTERM, print_stats);
418
419         /* initialise the EAL for all */
420         ret = rte_eal_init(argc, argv);
421         if (ret < 0)
422                 rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
423         argc -= ret;
424         argv += ret;
425
426         /* probe to determine the NIC devices available */
427         proc_type = rte_eal_process_type();
428 #ifdef RTE_LIBRTE_IGB_PMD
429         if (rte_igb_pmd_init() < 0)
430                 rte_exit(EXIT_FAILURE, "Cannot init igb pmd\n");
431 #endif
432 #ifdef RTE_LIBRTE_IXGBE_PMD
433         if (rte_ixgbe_pmd_init() < 0)
434                 rte_exit(EXIT_FAILURE, "Cannot init ixgbe pmd\n");
435 #endif
436         if (rte_eal_pci_probe() < 0)
437                 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
438         if (rte_eth_dev_count() == 0)
439                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
440
441         /* parse application arguments (those after the EAL ones) */
442         smp_parse_args(argc, argv);
443
444         mp = (proc_type == RTE_PROC_SECONDARY) ?
445                         rte_mempool_lookup(_SMP_MBUF_POOL) :
446                         rte_mempool_create(_SMP_MBUF_POOL, NB_MBUFS, MBUF_SIZE,
447                                         MBUF_CACHE_SIZE, sizeof(struct rte_pktmbuf_pool_private),
448                                         rte_pktmbuf_pool_init, NULL,
449                                         rte_pktmbuf_init, NULL,
450                                         SOCKET0, 0);
451         if (mp == NULL)
452                 rte_exit(EXIT_FAILURE, "Cannot get memory pool for buffers\n");
453
454         if (num_ports & 1)
455                 rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
456         for(i = 0; i < num_ports; i++){
457                 if(proc_type == RTE_PROC_PRIMARY)
458                         if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
459                                 rte_exit(EXIT_FAILURE, "Error initialising ports\n");
460                 report_link_status(ports[i]);
461         }
462
463         assign_ports_to_cores();
464
465         RTE_LOG(INFO, APP, "Finished Process Init.\n");
466
467         rte_eal_mp_remote_launch(lcore_main, NULL, CALL_MASTER);
468
469         return 0;
470 }