New upstream version 18.02
[deb_dpdk.git] / examples / multi_process / simple_mp / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 /*
6  * This sample application is a simple multi-process application which
7  * demostrates sharing of queues and memory pools between processes, and
8  * using those queues/pools for communication between the processes.
9  *
10  * Application is designed to run with two processes, a primary and a
11  * secondary, and each accepts commands on the commandline, the most
12  * important of which is "send", which just sends a string to the other
13  * process.
14  */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdint.h>
19 #include <inttypes.h>
20 #include <stdarg.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <termios.h>
24 #include <sys/queue.h>
25
26 #include <rte_common.h>
27 #include <rte_memory.h>
28 #include <rte_launch.h>
29 #include <rte_eal.h>
30 #include <rte_per_lcore.h>
31 #include <rte_lcore.h>
32 #include <rte_debug.h>
33 #include <rte_atomic.h>
34 #include <rte_branch_prediction.h>
35 #include <rte_ring.h>
36 #include <rte_log.h>
37 #include <rte_mempool.h>
38 #include <cmdline_rdline.h>
39 #include <cmdline_parse.h>
40 #include <cmdline_parse_string.h>
41 #include <cmdline_socket.h>
42 #include <cmdline.h>
43 #include "mp_commands.h"
44
45 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
46
47 static const char *_MSG_POOL = "MSG_POOL";
48 static const char *_SEC_2_PRI = "SEC_2_PRI";
49 static const char *_PRI_2_SEC = "PRI_2_SEC";
50
51 struct rte_ring *send_ring, *recv_ring;
52 struct rte_mempool *message_pool;
53 volatile int quit = 0;
54
55 static int
56 lcore_recv(__attribute__((unused)) void *arg)
57 {
58         unsigned lcore_id = rte_lcore_id();
59
60         printf("Starting core %u\n", lcore_id);
61         while (!quit){
62                 void *msg;
63                 if (rte_ring_dequeue(recv_ring, &msg) < 0){
64                         usleep(5);
65                         continue;
66                 }
67                 printf("core %u: Received '%s'\n", lcore_id, (char *)msg);
68                 rte_mempool_put(message_pool, msg);
69         }
70
71         return 0;
72 }
73
74 int
75 main(int argc, char **argv)
76 {
77         const unsigned flags = 0;
78         const unsigned ring_size = 64;
79         const unsigned pool_size = 1024;
80         const unsigned pool_cache = 32;
81         const unsigned priv_data_sz = 0;
82
83         int ret;
84         unsigned lcore_id;
85
86         ret = rte_eal_init(argc, argv);
87         if (ret < 0)
88                 rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
89
90         if (rte_eal_process_type() == RTE_PROC_PRIMARY){
91                 send_ring = rte_ring_create(_PRI_2_SEC, ring_size, rte_socket_id(), flags);
92                 recv_ring = rte_ring_create(_SEC_2_PRI, ring_size, rte_socket_id(), flags);
93                 message_pool = rte_mempool_create(_MSG_POOL, pool_size,
94                                 STR_TOKEN_SIZE, pool_cache, priv_data_sz,
95                                 NULL, NULL, NULL, NULL,
96                                 rte_socket_id(), flags);
97         } else {
98                 recv_ring = rte_ring_lookup(_PRI_2_SEC);
99                 send_ring = rte_ring_lookup(_SEC_2_PRI);
100                 message_pool = rte_mempool_lookup(_MSG_POOL);
101         }
102         if (send_ring == NULL)
103                 rte_exit(EXIT_FAILURE, "Problem getting sending ring\n");
104         if (recv_ring == NULL)
105                 rte_exit(EXIT_FAILURE, "Problem getting receiving ring\n");
106         if (message_pool == NULL)
107                 rte_exit(EXIT_FAILURE, "Problem getting message pool\n");
108
109         RTE_LOG(INFO, APP, "Finished Process Init.\n");
110
111         /* call lcore_recv() on every slave lcore */
112         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
113                 rte_eal_remote_launch(lcore_recv, NULL, lcore_id);
114         }
115
116         /* call cmd prompt on master lcore */
117         struct cmdline *cl = cmdline_stdin_new(simple_mp_ctx, "\nsimple_mp > ");
118         if (cl == NULL)
119                 rte_exit(EXIT_FAILURE, "Cannot create cmdline instance\n");
120         cmdline_interact(cl);
121         cmdline_stdin_exit(cl);
122
123         rte_eal_mp_wait_lcore();
124         return 0;
125 }