xref: /f-stack/tools/compat/ff_ipc.c (revision ebf5cedb)
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 void
84 ff_ipc_exit(void)
85 {
86 	rte_eal_cleanup();
87 	return;
88 }
89 
90 struct ff_msg *
91 ff_ipc_msg_alloc(void)
92 {
93     if (inited == 0) {
94         int ret = ff_ipc_init();
95         if (ret < 0) {
96             return NULL;
97         }
98     }
99 
100     void *msg;
101     if (rte_mempool_get(message_pool, &msg) < 0) {
102         printf("get buffer from message pool failed.\n");
103         return NULL;
104     }
105 
106     return (struct ff_msg *)msg;
107 }
108 
109 int
110 ff_ipc_msg_free(struct ff_msg *msg)
111 {
112     if (inited == 0) {
113         printf("ff ipc not inited\n");
114         return -1;
115     }
116 
117     rte_mempool_put(message_pool, msg);
118 
119     return 0;
120 }
121 
122 int
123 ff_ipc_send(const struct ff_msg *msg)
124 {
125     int ret;
126 
127     if (inited == 0) {
128         printf("ff ipc not inited\n");
129         return -1;
130     }
131 
132     char name[RTE_RING_NAMESIZE];
133     snprintf(name, RTE_RING_NAMESIZE, "%s%u",
134         FF_MSG_RING_IN, ff_proc_id);
135     struct rte_ring *ring = rte_ring_lookup(name);
136     if (ring == NULL) {
137         printf("lookup message ring:%s failed!\n", name);
138         return -1;
139     }
140 
141     ret = rte_ring_enqueue(ring, (void *)msg);
142     if (ret < 0) {
143         printf("ff_ipc_send failed\n");
144         return ret;
145     }
146 
147     return 0;
148 }
149 
150 int
151 ff_ipc_recv(struct ff_msg **msg, enum FF_MSG_TYPE msg_type)
152 {
153     int ret, i;
154     if (inited == 0) {
155         printf("ff ipc not inited\n");
156         return -1;
157     }
158 
159     char name[RTE_RING_NAMESIZE];
160     snprintf(name, RTE_RING_NAMESIZE, "%s%u_%u",
161         FF_MSG_RING_OUT, ff_proc_id, msg_type);
162     struct rte_ring *ring = rte_ring_lookup(name);
163     if (ring == NULL) {
164         printf("lookup message ring:%s failed!\n", name);
165         return -1;
166     }
167 
168     void *obj;
169     #define MAX_ATTEMPTS_NUM 1000
170     for (i = 0; i < MAX_ATTEMPTS_NUM; i++) {
171         ret = rte_ring_dequeue(ring, &obj);
172         if (ret == 0) {
173             *msg = (struct ff_msg *)obj;
174             break;
175         }
176 
177         usleep(1000);
178     }
179 
180     return ret;
181 }
182