first public release
[dpdk.git] / examples / multi_process / simple_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  *  version: DPDK.L.1.2.3-3
34  */
35
36 /*
37  * This sample application is a simple multi-process application which
38  * demostrates sharing of queues and memory pools between processes, and
39  * using those queues/pools for communication between the processes.
40  *
41  * Application is designed to run with two processes, a primary and a
42  * secondary, and each accepts commands on the commandline, the most
43  * important of which is "send", which just sends a string to the other
44  * process.
45  */
46
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdint.h>
50 #include <inttypes.h>
51 #include <stdarg.h>
52 #include <errno.h>
53 #include <unistd.h>
54 #include <termios.h>
55 #include <sys/queue.h>
56
57 #include <rte_common.h>
58 #include <rte_memory.h>
59 #include <rte_memzone.h>
60 #include <rte_launch.h>
61 #include <rte_tailq.h>
62 #include <rte_eal.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_debug.h>
66 #include <rte_atomic.h>
67 #include <rte_branch_prediction.h>
68 #include <rte_ring.h>
69 #include <rte_log.h>
70 #include <rte_mempool.h>
71 #include <cmdline_rdline.h>
72 #include <cmdline_parse.h>
73 #include <cmdline_socket.h>
74 #include <cmdline.h>
75 #include "mp_commands.h"
76
77 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
78
79 #define SOCKET0 0
80
81 static const char *_MSG_POOL = "MSG_POOL";
82 static const char *_SEC_2_PRI = "SEC_2_PRI";
83 static const char *_PRI_2_SEC = "PRI_2_SEC";
84 const unsigned string_size = 64;
85
86 struct rte_ring *send_ring, *recv_ring;
87 struct rte_mempool *message_pool;
88 volatile int quit = 0;
89
90 static int
91 lcore_recv(__attribute__((unused)) void *arg)
92 {
93         unsigned lcore_id = rte_lcore_id();
94
95         printf("Starting core %u\n", lcore_id);
96         while (!quit){
97                 void *msg;
98                 if (rte_ring_dequeue(recv_ring, &msg) < 0){
99                         usleep(5);
100                         continue;
101                 }
102                 printf("core %u: Received '%s'\n", lcore_id, (char *)msg);
103                 rte_mempool_put(message_pool, msg);
104         }
105
106         return 0;
107 }
108
109 int
110 main(int argc, char **argv)
111 {
112         const unsigned flags = 0;
113         const unsigned ring_size = 64;
114         const unsigned pool_size = 1024;
115         const unsigned pool_cache = 32;
116         const unsigned priv_data_sz = 0;
117
118         int ret;
119         unsigned lcore_id;
120
121         ret = rte_eal_init(argc, argv);
122         if (ret < 0)
123                 rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
124
125         if (rte_eal_process_type() == RTE_PROC_PRIMARY){
126                 send_ring = rte_ring_create(_PRI_2_SEC, ring_size, SOCKET0, flags);
127                 recv_ring = rte_ring_create(_SEC_2_PRI, ring_size, SOCKET0, flags);
128                 message_pool = rte_mempool_create(_MSG_POOL, pool_size,
129                                 string_size, pool_cache, priv_data_sz,
130                                 NULL, NULL, NULL, NULL,
131                                 SOCKET0, flags);
132         } else {
133                 recv_ring = rte_ring_lookup(_PRI_2_SEC);
134                 send_ring = rte_ring_lookup(_SEC_2_PRI);
135                 message_pool = rte_mempool_lookup(_MSG_POOL);
136         }
137         if (send_ring == NULL)
138                 rte_exit(EXIT_FAILURE, "Problem getting sending ring\n");
139         if (recv_ring == NULL)
140                 rte_exit(EXIT_FAILURE, "Problem getting receiving ring\n");
141         if (message_pool == NULL)
142                 rte_exit(EXIT_FAILURE, "Problem getting message pool\n");
143
144         RTE_LOG(INFO, APP, "Finished Process Init.\n");
145
146         /* call lcore_recv() on every slave lcore */
147         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
148                 rte_eal_remote_launch(lcore_recv, NULL, lcore_id);
149         }
150
151         /* call cmd prompt on master lcore */
152         struct cmdline *cl = cmdline_stdin_new(simple_mp_ctx, "\nsimple_mp > ");
153         if (cl == NULL)
154                 rte_exit(EXIT_FAILURE, "Cannot create cmdline instance\n");
155         cmdline_interact(cl);
156         cmdline_stdin_exit(cl);
157
158         rte_eal_mp_wait_lcore();
159         return 0;
160 }