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_branch_prediction.h> 34 #include <rte_ring.h> 35 #include <rte_log.h> 36 #include <rte_mempool.h> 37 #include <cmdline_rdline.h> 38 #include <cmdline_parse.h> 39 #include <cmdline_parse_string.h> 40 #include <cmdline_socket.h> 41 #include <cmdline.h> 42 #include "mp_commands.h" 43 44 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1 45 46 static const char *_MSG_POOL = "MSG_POOL"; 47 static const char *_SEC_2_PRI = "SEC_2_PRI"; 48 static const char *_PRI_2_SEC = "PRI_2_SEC"; 49 50 struct rte_ring *send_ring, *recv_ring; 51 struct rte_mempool *message_pool; 52 volatile int quit = 0; 53 54 static int 55 lcore_recv(__rte_unused void *arg) 56 { 57 unsigned lcore_id = rte_lcore_id(); 58 59 printf("Starting core %u\n", lcore_id); 60 while (!quit){ 61 void *msg; 62 if (rte_ring_dequeue(recv_ring, &msg) < 0){ 63 usleep(5); 64 continue; 65 } 66 printf("core %u: Received '%s'\n", lcore_id, (char *)msg); 67 rte_mempool_put(message_pool, msg); 68 } 69 70 return 0; 71 } 72 73 int 74 main(int argc, char **argv) 75 { 76 const unsigned flags = 0; 77 const unsigned ring_size = 64; 78 const unsigned pool_size = 1024; 79 const unsigned pool_cache = 32; 80 const unsigned priv_data_sz = 0; 81 82 int ret; 83 unsigned lcore_id; 84 85 ret = rte_eal_init(argc, argv); 86 if (ret < 0) 87 rte_exit(EXIT_FAILURE, "Cannot init EAL\n"); 88 89 /* Start of ring structure. 8< */ 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 /* >8 End of ring structure. */ 103 if (send_ring == NULL) 104 rte_exit(EXIT_FAILURE, "Problem getting sending ring\n"); 105 if (recv_ring == NULL) 106 rte_exit(EXIT_FAILURE, "Problem getting receiving ring\n"); 107 if (message_pool == NULL) 108 rte_exit(EXIT_FAILURE, "Problem getting message pool\n"); 109 110 RTE_LOG(INFO, APP, "Finished Process Init.\n"); 111 112 /* call lcore_recv() on every worker lcore */ 113 RTE_LCORE_FOREACH_WORKER(lcore_id) { 114 rte_eal_remote_launch(lcore_recv, NULL, lcore_id); 115 } 116 117 /* call cmd prompt on main lcore */ 118 struct cmdline *cl = cmdline_stdin_new(simple_mp_ctx, "\nsimple_mp > "); 119 if (cl == NULL) 120 rte_exit(EXIT_FAILURE, "Cannot create cmdline instance\n"); 121 cmdline_interact(cl); 122 cmdline_stdin_exit(cl); 123 124 rte_eal_mp_wait_lcore(); 125 126 /* clean up the EAL */ 127 rte_eal_cleanup(); 128 129 return 0; 130 } 131