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