1 /* 2 * Copyright (C) 2017 THL A29 Limited, a Tencent company. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 */ 26 27 #include <rte_common.h> 28 #include <rte_memory.h> 29 #include <rte_config.h> 30 #include <rte_eal.h> 31 #include <rte_ring.h> 32 #include <rte_mempool.h> 33 #include <rte_malloc.h> 34 #include <unistd.h> 35 36 #include "ff_ipc.h" 37 38 static int inited; 39 40 static struct rte_mempool *message_pool; 41 42 uint16_t ff_proc_id = 0; 43 44 void 45 ff_set_proc_id(int pid) 46 { 47 if (pid < 0 || pid > 65535) { 48 printf("Invalid F-Stack proccess id\n"); 49 exit(1); 50 } 51 ff_proc_id = pid; 52 } 53 54 int 55 ff_ipc_init(void) 56 { 57 if (inited) { 58 return 0; 59 } 60 61 char *dpdk_argv[] = { 62 "ff-ipc", "-c1", "-n4", 63 "--proc-type=secondary", 64 /* RTE_LOG_WARNING */ 65 "--log-level=5", 66 }; 67 68 int ret = rte_eal_init(sizeof(dpdk_argv)/sizeof(dpdk_argv[0]), dpdk_argv); 69 if (ret < 0) { 70 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 71 } 72 73 message_pool = rte_mempool_lookup(FF_MSG_POOL); 74 if (message_pool == NULL) { 75 rte_exit(EXIT_FAILURE, "lookup message pool:%s failed!\n", FF_MSG_POOL); 76 } 77 78 inited = 1; 79 80 return 0; 81 } 82 83 struct ff_msg * 84 ff_ipc_msg_alloc(void) 85 { 86 if (inited == 0) { 87 int ret = ff_ipc_init(); 88 if (ret < 0) { 89 return NULL; 90 } 91 } 92 93 void *msg; 94 if (rte_mempool_get(message_pool, &msg) < 0) { 95 printf("get buffer from message pool failed.\n"); 96 return NULL; 97 } 98 99 return (struct ff_msg *)msg; 100 } 101 102 int 103 ff_ipc_msg_free(struct ff_msg *msg) 104 { 105 if (inited == 0) { 106 printf("ff ipc not inited\n"); 107 return -1; 108 } 109 110 rte_mempool_put(message_pool, msg); 111 112 return 0; 113 } 114 115 int 116 ff_ipc_send(const struct ff_msg *msg) 117 { 118 int ret; 119 120 if (inited == 0) { 121 printf("ff ipc not inited\n"); 122 return -1; 123 } 124 125 char name[RTE_RING_NAMESIZE]; 126 snprintf(name, RTE_RING_NAMESIZE, "%s%u", 127 FF_MSG_RING_IN, ff_proc_id); 128 struct rte_ring *ring = rte_ring_lookup(name); 129 if (ring == NULL) { 130 printf("lookup message ring:%s failed!\n", name); 131 return -1; 132 } 133 134 ret = rte_ring_enqueue(ring, (void *)msg); 135 if (ret < 0) { 136 printf("ff_ipc_send failed\n"); 137 return ret; 138 } 139 140 return 0; 141 } 142 143 int 144 ff_ipc_recv(struct ff_msg **msg) 145 { 146 int ret, i; 147 if (inited == 0) { 148 printf("ff ipc not inited\n"); 149 return -1; 150 } 151 152 char name[RTE_RING_NAMESIZE]; 153 snprintf(name, RTE_RING_NAMESIZE, "%s%u", 154 FF_MSG_RING_OUT, ff_proc_id); 155 struct rte_ring *ring = rte_ring_lookup(name); 156 if (ring == NULL) { 157 printf("lookup message ring:%s failed!\n", name); 158 return -1; 159 } 160 161 void *obj; 162 #define MAX_ATTEMPTS_NUM 1000 163 for (i = 0; i < MAX_ATTEMPTS_NUM; i++) { 164 ret = rte_ring_dequeue(ring, &obj); 165 if (ret == 0) { 166 *msg = (struct ff_msg *)obj; 167 break; 168 } 169 170 usleep(1000); 171 } 172 173 return ret; 174 } 175