blob: dde5aff8030efc5b67cfa3afed86d1408e88e449 [file] [log] [blame]
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
Damjan Marion2cdc2bd2018-04-26 18:39:56 +020018#define _GNU_SOURCE
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020019#include <stdint.h>
20#include <net/if.h>
21#include <sys/types.h>
22#include <fcntl.h>
23#include <sys/ioctl.h>
24#include <sys/socket.h>
25#include <sys/un.h>
26#include <sys/uio.h>
27#include <sys/mman.h>
28#include <sys/prctl.h>
29#include <inttypes.h>
30#include <string.h>
31#include <stdio.h>
32#include <netdb.h>
33#include <linux/ip.h>
34#include <linux/icmp.h>
35#include <arpa/inet.h>
36#include <stdlib.h>
37#include <netinet/if_ether.h>
38#include <net/if_arp.h>
39#include <asm/byteorder.h>
40#include <byteswap.h>
41#include <string.h>
42#include <errno.h>
43#include <sys/stat.h>
44#include <sys/eventfd.h>
45#include <sys/timerfd.h>
46#include <sys/epoll.h>
47#include <signal.h>
Jakub Grajciar435f5002018-04-20 09:20:50 +020048#include <linux/memfd.h>
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020049
50/* memif protocol msg, ring and descriptor definitions */
51#include <memif.h>
52/* memif api */
53#include <libmemif.h>
54/* socket messaging functions */
55#include <socket.h>
56/* private structs and functions */
57#include <memif_private.h>
58
Jakub Grajciar568cc462018-09-05 12:11:35 +020059#define ERRLIST_LEN 40
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020060#define MAX_ERRBUF_LEN 256
61
62#if __x86_x64__
63#define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence ()
64#else
Milan Lenco0a47c992017-10-12 14:19:31 +020065#define MEMIF_MEMORY_BARRIER() __sync_synchronize ()
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020066#endif /* __x86_x64__ */
67
68libmemif_main_t libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020069
70static char memif_buf[MAX_ERRBUF_LEN];
71
72const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */
73 "Success.",
74 /* MEMIF_ERR_SYSCALL */
75 "Unspecified syscall error (build with -DMEMIF_DBG or make debug).",
Jakub Grajciar568cc462018-09-05 12:11:35 +020076 /* MEMIF_ERR_CONNREFUSED */
77 "Connection refused",
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020078 /* MEMIF_ERR_ACCES */
79 "Permission to resoure denied.",
80 /* MEMIF_ERR_NO_FILE */
81 "Socket file does not exist",
82 /* MEMIF_ERR_FILE_LIMIT */
83 "System limit on total numer of open files reached.",
84 /* MEMIF_ERR_PROC_FILE_LIMIT */
85 "Per-process limit on total number of open files reached.",
86 /* MEMIF_ERR_ALREADY */
87 "Connection already requested.",
88 /* MEMIF_ERR_AGAIN */
89 "File descriptor refers to file other than socket, or operation would block.",
90 /* MEMIF_ERR_BAD_FD */
91 "Bad file descriptor.",
92 /* MEMIF_ERR_NOMEM */
93 "Out of memory.",
94 /* MEMIF_ERR_INVAL_ARG */
95 "Invalid argument.",
96 /* MEMIF_ERR_NOCONN */
97 "Memif connection handle does not point to existing conenction",
98 /* MEMIF_ERR_CONN */
99 "Memif connection handle points to existing connection",
100 /* MEMIF_ERR_CB_FDUPDATE */
101 "Callback memif_control_fd_update_t returned error",
102 /* MEMIF_ERR_FILE_NOT_SOCK */
103 "File specified by socket filename exists and is not socket.",
104 /* MEMIF_ERR_NO_SHMFD */
105 "Missing shared memory file descriptor. (internal error)",
106 /* MEMIF_ERR_COOKIE */
107 "Invalid cookie on ring. (internal error)",
108 /* MEMIF_ERR_NOBUF_RING */
109 "Ring buffer full.",
110 /* MEMIF_ERR_NOBUF */
111 "Not enough memif buffers. There are unreceived data in shared memory.",
112 /* MEMIF_ERR_NOBUF_DET */
113 "Not enough space for memif details in suplied buffer. String data might be malformed.",
114 /* MEMIF_ERR_INT_WRITE */
115 "Send interrupt error.",
116 /* MEMIF_ERR_MFMSG */
117 "Malformed message received on control channel.",
118 /* MEMIF_ERR_QID */
119 "Invalid queue id",
120 /* MEMIF_ERR_PROTO */
121 "Incompatible memory interface protocol version.",
122 /* MEMIF_ERR_ID */
123 "Unmatched interface id.",
124 /* MEMIF_ERR_ACCSLAVE */
125 "Slave cannot accept connection reqest.",
126 /* MEMIF_ERR_ALRCONN */
127 "Interface is already connected.",
128 /* MEMIF_ERR_MODE */
129 "Mode mismatch.",
130 /* MEMIF_ERR_SECRET */
131 "Secret mismatch.",
132 /* MEMIF_ERR_NOSECRET */
133 "Secret required.",
134 /* MEMIF_ERR_MAXREG */
135 "Limit on total number of regions reached.",
136 /* MEMIF_ERR_MAXRING */
137 "Limit on total number of ring reached.",
138 /* MEMIF_ERR_NO_INTFD */
139 "Missing interrupt file descriptor. (internal error)",
140 /* MEMIF_ERR_DISCONNECT */
141 "Interface received disconnect request.",
142 /* MEMIF_ERR_DISCONNECTED */
143 "Interface is disconnected.",
144 /* MEMIF_ERR_UNKNOWN_MSG */
Milan Lenco0a47c992017-10-12 14:19:31 +0200145 "Unknown message type received on control channel. (internal error)",
146 /* MEMIF_ERR_POLL_CANCEL */
Damjan Marion6d56fa42017-11-03 12:24:37 +0100147 "Memif event polling was canceled.",
148 /* MEMIF_ERR_MAX_RING */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200149 "Maximum log2 ring size is 15",
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200150 /* MEMIF_ERR_PRIVHDR */
151 "Private headers not supported."
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200152};
153
154#define MEMIF_ERR_UNDEFINED "undefined error"
155
156char *
157memif_strerror (int err_code)
158{
159 if (err_code >= ERRLIST_LEN)
160 {
161 strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED));
162 memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0';
163 }
164 else
165 {
166 strncpy (memif_buf, memif_errlist[err_code],
167 strlen (memif_errlist[err_code]));
168 memif_buf[strlen (memif_errlist[err_code])] = '\0';
169 }
170 return memif_buf;
171}
172
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200173uint16_t
174memif_get_version ()
175{
176 return MEMIF_VERSION;
177}
178
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200179#define DBG_TX_BUF (0)
180#define DBG_RX_BUF (1)
181
182#ifdef MEMIF_DBG_SHM
183static void
184print_bytes (void *data, uint16_t len, uint8_t q)
185{
186 if (q == DBG_TX_BUF)
187 printf ("\nTX:\n\t");
188 else
189 printf ("\nRX:\n\t");
190 int i;
191 for (i = 0; i < len; i++)
192 {
193 if (i % 8 == 0)
194 printf ("\n%d:\t", i);
195 printf ("%02X ", ((uint8_t *) (data))[i]);
196 }
197 printf ("\n\n");
198}
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200199#endif /* MEMIF_DBG_SHM */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200200
201int
202memif_syscall_error_handler (int err_code)
203{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200204 DBG ("%s", strerror (err_code));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200205
206 if (err_code == 0)
207 return MEMIF_ERR_SUCCESS;
208 if (err_code == EACCES)
209 return MEMIF_ERR_ACCES;
210 if (err_code == ENFILE)
211 return MEMIF_ERR_FILE_LIMIT;
212 if (err_code == EMFILE)
213 return MEMIF_ERR_PROC_FILE_LIMIT;
214 if (err_code == ENOMEM)
215 return MEMIF_ERR_NOMEM;
Damjan Marion6d56fa42017-11-03 12:24:37 +0100216/* connection refused if master does not exist
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200217 this error would spam the user until master was created */
Jakub Grajciar568cc462018-09-05 12:11:35 +0200218/*
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200219 if (err_code == ECONNREFUSED)
220 return MEMIF_ERR_SUCCESS;
Jakub Grajciar568cc462018-09-05 12:11:35 +0200221*/
222 if (err_code == ECONNREFUSED)
223 return MEMIF_ERR_CONNREFUSED;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200224 if (err_code == EALREADY)
225 return MEMIF_ERR_ALREADY;
226 if (err_code == EAGAIN)
227 return MEMIF_ERR_AGAIN;
228 if (err_code == EBADF)
229 return MEMIF_ERR_BAD_FD;
230 if (err_code == ENOENT)
231 return MEMIF_ERR_NO_FILE;
232
233 /* other syscall errors */
234 return MEMIF_ERR_SYSCALL;
235}
236
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200237/* Always valid */
238libmemif_main_t *
239get_libmemif_main (memif_socket_t * ms)
240{
241 if (ms != NULL && ms->lm != NULL)
242 return ms->lm;
243 return &libmemif_main;
244}
245
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200246static int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200247memif_add_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200248{
249 if (fd < 0)
250 {
251 DBG ("invalid fd %d", fd);
252 return -1;
253 }
254 struct epoll_event evt;
255 memset (&evt, 0, sizeof (evt));
256 evt.events = events;
257 evt.data.fd = fd;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200258 if (epoll_ctl (lm->epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200259 {
260 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
261 return -1;
262 }
263 DBG ("fd %d added to epoll", fd);
264 return 0;
265}
266
267static int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200268memif_mod_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200269{
270 if (fd < 0)
271 {
272 DBG ("invalid fd %d", fd);
273 return -1;
274 }
275 struct epoll_event evt;
276 memset (&evt, 0, sizeof (evt));
277 evt.events = events;
278 evt.data.fd = fd;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200279 if (epoll_ctl (lm->epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200280 {
281 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
282 return -1;
283 }
284 DBG ("fd %d moddified on epoll", fd);
285 return 0;
286}
287
288static int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200289memif_del_epoll_fd (libmemif_main_t * lm, int fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200290{
291 if (fd < 0)
292 {
293 DBG ("invalid fd %d", fd);
294 return -1;
295 }
296 struct epoll_event evt;
297 memset (&evt, 0, sizeof (evt));
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200298 if (epoll_ctl (lm->epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200299 {
300 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
301 return -1;
302 }
303 DBG ("fd %d removed from epoll", fd);
304 return 0;
305}
306
307int
Jakub Grajciar12df4972019-07-01 14:24:48 +0200308memif_control_fd_update (int fd, uint8_t events, void *private_ctx)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200309{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200310 libmemif_main_t *lm;
311
312 if (private_ctx == NULL)
313 return MEMIF_ERR_INVAL_ARG;
314
315 lm = (libmemif_main_t *) private_ctx;
316
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200317 if (events & MEMIF_FD_EVENT_DEL)
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200318 return memif_del_epoll_fd (lm, fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200319
320 uint32_t evt = 0;
321 if (events & MEMIF_FD_EVENT_READ)
322 evt |= EPOLLIN;
323 if (events & MEMIF_FD_EVENT_WRITE)
324 evt |= EPOLLOUT;
325
326 if (events & MEMIF_FD_EVENT_MOD)
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200327 return memif_mod_epoll_fd (lm, fd, evt);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200328
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200329 return memif_add_epoll_fd (lm, fd, evt);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200330}
331
332int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200333add_list_elt (libmemif_main_t * lm, memif_list_elt_t * e,
334 memif_list_elt_t ** list, uint16_t * len)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200335{
Jakub Grajciar12df4972019-07-01 14:24:48 +0200336 memif_list_elt_t *tmp;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200337 int i;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200338
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200339 for (i = 0; i < *len; i++)
340 {
341 if ((*list)[i].data_struct == NULL)
342 {
343 (*list)[i].key = e->key;
344 (*list)[i].data_struct = e->data_struct;
345 return i;
346 }
347 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200348
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200349 tmp = lm->realloc (*list, sizeof (memif_list_elt_t) * *len * 2);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200350 if (tmp == NULL)
351 return -1;
352
353 for (i = *len; i < *len * 2; i++)
354 {
355 tmp[i].key = -1;
356 tmp[i].data_struct = NULL;
357 }
358
359 tmp[*len].key = e->key;
360 tmp[*len].data_struct = e->data_struct;
361 i = *len;
362 *len = *len * 2;
363 *list = tmp;
364
365 return i;
366}
367
368int
369get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len,
370 int key)
371{
Jakub Grajciar12df4972019-07-01 14:24:48 +0200372 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200373 if (key == -1)
374 {
375 *e = NULL;
376 return -1;
377 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200378
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200379 for (i = 0; i < len; i++)
380 {
381 if (list[i].key == key)
382 {
383 *e = &list[i];
384 return 0;
385 }
386 }
387 *e = NULL;
388 return -1;
389}
390
391/* does not free memory, only marks element as free */
392int
393free_list_elt (memif_list_elt_t * list, uint16_t len, int key)
394{
395 int i;
396 for (i = 0; i < len; i++)
397 {
398 if (list[i].key == key)
399 {
400 list[i].key = -1;
401 list[i].data_struct = NULL;
402 return 0;
403 }
404 }
405
406 return -1;
407}
408
409int
410free_list_elt_ctx (memif_list_elt_t * list, uint16_t len,
411 memif_connection_t * ctx)
412{
413 int i;
414 for (i = 0; i < len; i++)
415 {
416 if (list[i].key == -1)
417 {
418 if (list[i].data_struct == ctx)
419 {
420 list[i].data_struct = NULL;
421 return 0;
422 }
423 }
424 }
425
426 return -1;
427}
428
429static void
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200430memif_control_fd_update_register (libmemif_main_t * lm,
431 memif_control_fd_update_t * cb)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200432{
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200433 lm->control_fd_update = cb;
434}
435
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200436void
437memif_register_external_region (memif_add_external_region_t * ar,
438 memif_get_external_region_addr_t * gr,
439 memif_del_external_region_t * dr,
440 memif_get_external_buffer_offset_t * go)
441{
442 libmemif_main_t *lm = &libmemif_main;
443 lm->add_external_region = ar;
444 lm->get_external_region_addr = gr;
445 lm->del_external_region = dr;
446 lm->get_external_buffer_offset = go;
447}
448
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200449static void
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200450memif_alloc_register (libmemif_main_t * lm, memif_alloc_t * ma)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200451{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200452 lm->alloc = ma;
453}
454
455static void
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200456memif_realloc_register (libmemif_main_t * lm, memif_realloc_t * mr)
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200457{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200458 lm->realloc = mr;
459}
460
461static void
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200462memif_free_register (libmemif_main_t * lm, memif_free_t * mf)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200463{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200464 lm->free = mf;
465}
466
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200467int
Jakub Grajciar12df4972019-07-01 14:24:48 +0200468memif_set_connection_request_timer (struct itimerspec timer)
Jakub Grajciar84b83772019-03-04 12:42:19 +0100469{
470 libmemif_main_t *lm = &libmemif_main;
471 int err = MEMIF_ERR_SUCCESS;
472
473 lm->arm = timer;
474
475 /* overwrite timer, if already armed */
476 if (lm->disconn_slaves != 0)
477 {
478 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
479 {
480 err = memif_syscall_error_handler (errno);
481 }
482 }
483 return err;
484}
485
486int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200487memif_per_thread_set_connection_request_timer (memif_per_thread_main_handle_t
488 pt_main,
489 struct itimerspec timer)
490{
491 libmemif_main_t *lm = (libmemif_main_t *) pt_main;
492 int err = MEMIF_ERR_SUCCESS;
493
494 lm->arm = timer;
495
496 /* overwrite timer, if already armed */
497 if (lm->disconn_slaves != 0)
498 {
499 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
500 {
501 err = memif_syscall_error_handler (errno);
502 }
503 }
504 return err;
505}
506
507int
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200508memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200509 memif_alloc_t * memif_alloc, memif_realloc_t * memif_realloc,
510 memif_free_t * memif_free)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200511{
512 int err = MEMIF_ERR_SUCCESS; /* 0 */
513 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar19418712018-03-13 13:57:50 +0100514 memset (lm, 0, sizeof (libmemif_main_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200515
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200516 /* register custom memory management */
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200517 if (memif_alloc != NULL)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200518 {
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200519 memif_alloc_register (lm, memif_alloc);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200520 }
521 else
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200522 memif_alloc_register (lm, malloc);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200523
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200524 if (memif_realloc != NULL)
525 {
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200526 memif_realloc_register (lm, memif_realloc);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200527 }
528 else
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200529 memif_realloc_register (lm, realloc);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200530
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200531 if (memif_free != NULL)
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200532 memif_free_register (lm, memif_free);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200533 else
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200534 memif_free_register (lm, free);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200535
536 if (app_name != NULL)
537 {
538 uint8_t len = (strlen (app_name) > MEMIF_NAME_LEN)
539 ? strlen (app_name) : MEMIF_NAME_LEN;
540 strncpy ((char *) lm->app_name, app_name, len);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200541 }
542 else
543 {
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200544 strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
545 strlen (MEMIF_DEFAULT_APP_NAME));
546 }
547
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200548 lm->poll_cancel_fd = -1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200549 /* register control fd update callback */
550 if (on_control_fd_update != NULL)
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200551 memif_control_fd_update_register (lm, on_control_fd_update);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200552 else
553 {
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200554 lm->epfd = epoll_create (1);
555 memif_control_fd_update_register (lm, memif_control_fd_update);
556 if ((lm->poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
Milan Lenco0a47c992017-10-12 14:19:31 +0200557 {
558 err = errno;
559 DBG ("eventfd: %s", strerror (err));
560 return memif_syscall_error_handler (err);
561 }
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200562 lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ, NULL);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200563 DBG ("libmemif event polling initialized");
564 }
565
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200566 lm->control_list_len = 2;
567 lm->interrupt_list_len = 2;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200568 lm->socket_list_len = 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200569 lm->pending_list_len = 1;
570
571 lm->control_list =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200572 lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len);
573 if (lm->control_list == NULL)
574 {
575 err = MEMIF_ERR_NOMEM;
576 goto error;
577 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200578 lm->interrupt_list =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200579 lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
580 if (lm->interrupt_list == NULL)
581 {
582 err = MEMIF_ERR_NOMEM;
583 goto error;
584 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200585 lm->socket_list =
586 lm->alloc (sizeof (memif_list_elt_t) * lm->socket_list_len);
587 if (lm->socket_list == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200588 {
589 err = MEMIF_ERR_NOMEM;
590 goto error;
591 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200592 lm->pending_list =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200593 lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
594 if (lm->pending_list == NULL)
595 {
596 err = MEMIF_ERR_NOMEM;
597 goto error;
598 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200599
600 int i;
601 for (i = 0; i < lm->control_list_len; i++)
602 {
603 lm->control_list[i].key = -1;
604 lm->control_list[i].data_struct = NULL;
605 }
606 for (i = 0; i < lm->interrupt_list_len; i++)
607 {
608 lm->interrupt_list[i].key = -1;
609 lm->interrupt_list[i].data_struct = NULL;
610 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200611 for (i = 0; i < lm->socket_list_len; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200612 {
Jakub Grajciar12df4972019-07-01 14:24:48 +0200613 lm->socket_list[i].key = -1;
614 lm->socket_list[i].data_struct = NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200615 }
616 for (i = 0; i < lm->pending_list_len; i++)
617 {
618 lm->pending_list[i].key = -1;
619 lm->pending_list[i].data_struct = NULL;
620 }
621
622 lm->disconn_slaves = 0;
623
624 lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
625 if (lm->timerfd < 0)
626 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200627 err = memif_syscall_error_handler (errno);
628 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200629 }
630
Jakub Grajciar84b83772019-03-04 12:42:19 +0100631 lm->arm.it_value.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
632 lm->arm.it_value.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
633 lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
634 lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200635
Jakub Grajciar12df4972019-07-01 14:24:48 +0200636 if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ, NULL) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200637 {
638 DBG ("callback type memif_control_fd_update_t error!");
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200639 err = MEMIF_ERR_CB_FDUPDATE;
640 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200641 }
642
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200643 /* Create default socket */
644 err = memif_create_socket ((memif_socket_handle_t *) &
645 lm->default_socket,
646 MEMIF_DEFAULT_SOCKET_PATH, NULL);
647 if (err != MEMIF_ERR_SUCCESS)
648 goto error;
649
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200650 return err;
651
652error:
653 memif_cleanup ();
654 return err;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200655}
656
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200657int
658memif_per_thread_init (memif_per_thread_main_handle_t * pt_main,
659 void *private_ctx,
660 memif_control_fd_update_t * on_control_fd_update,
661 char *app_name, memif_alloc_t * memif_alloc,
662 memif_realloc_t * memif_realloc,
663 memif_free_t * memif_free)
664{
665 memif_err_t err = MEMIF_ERR_SUCCESS;
666 int i;
667 libmemif_main_t *lm;
668
669 /* Allocate unique libmemif main */
670 if (memif_alloc != NULL)
671 lm = memif_alloc (sizeof (libmemif_main_t));
672 else
673 lm = malloc (sizeof (libmemif_main_t));
674
675 if (lm == NULL)
676 return MEMIF_ERR_NOMEM;
677
678 memset (lm, 0, sizeof (libmemif_main_t));
679
680 /* register custom memory management */
681 if (memif_alloc != NULL)
682 {
683 memif_alloc_register (lm, memif_alloc);
684 }
685 else
686 memif_alloc_register (lm, malloc);
687
688 if (memif_realloc != NULL)
689 {
690 memif_realloc_register (lm, memif_realloc);
691 }
692 else
693 memif_realloc_register (lm, realloc);
694
695 if (memif_free != NULL)
696 memif_free_register (lm, memif_free);
697 else
698 memif_free_register (lm, free);
699
700 lm->private_ctx = private_ctx;
701
702 /* set app name */
703 if (app_name != NULL)
704 {
705 uint8_t len = (strlen (app_name) > MEMIF_NAME_LEN)
706 ? strlen (app_name) : MEMIF_NAME_LEN;
707 strncpy ((char *) lm->app_name, app_name, len);
708 }
709 else
710 {
711 strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
712 strlen (MEMIF_DEFAULT_APP_NAME));
713 }
714
715 lm->poll_cancel_fd = -1;
716 /* register control fd update callback */
717 if (on_control_fd_update != NULL)
718 memif_control_fd_update_register (lm, on_control_fd_update);
719 else
720 {
721 lm->epfd = epoll_create (1);
722 memif_control_fd_update_register (lm, memif_control_fd_update);
723 if ((lm->poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
724 {
725 err = errno;
726 DBG ("eventfd: %s", strerror (err));
727 return memif_syscall_error_handler (err);
728 }
729 lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ,
730 lm->private_ctx);
731 DBG ("libmemif event polling initialized");
732 }
733
734 /* Initialize lists */
735 lm->control_list_len = 2;
736 lm->interrupt_list_len = 2;
737 lm->socket_list_len = 1;
738 lm->pending_list_len = 1;
739
740 lm->control_list =
741 lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len);
742 if (lm->control_list == NULL)
743 {
744 err = MEMIF_ERR_NOMEM;
745 goto error;
746 }
747 lm->interrupt_list =
748 lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
749 if (lm->interrupt_list == NULL)
750 {
751 err = MEMIF_ERR_NOMEM;
752 goto error;
753 }
754 lm->socket_list =
755 lm->alloc (sizeof (memif_list_elt_t) * lm->socket_list_len);
756 if (lm->socket_list == NULL)
757 {
758 err = MEMIF_ERR_NOMEM;
759 goto error;
760 }
761 lm->pending_list =
762 lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
763 if (lm->pending_list == NULL)
764 {
765 err = MEMIF_ERR_NOMEM;
766 goto error;
767 }
768
769 for (i = 0; i < lm->control_list_len; i++)
770 {
771 lm->control_list[i].key = -1;
772 lm->control_list[i].data_struct = NULL;
773 }
774 for (i = 0; i < lm->interrupt_list_len; i++)
775 {
776 lm->interrupt_list[i].key = -1;
777 lm->interrupt_list[i].data_struct = NULL;
778 }
779 for (i = 0; i < lm->socket_list_len; i++)
780 {
781 lm->socket_list[i].key = -1;
782 lm->socket_list[i].data_struct = NULL;
783 }
784 for (i = 0; i < lm->pending_list_len; i++)
785 {
786 lm->pending_list[i].key = -1;
787 lm->pending_list[i].data_struct = NULL;
788 }
789
790 /* Initialize autoconnect */
791 lm->disconn_slaves = 0;
792
793 lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
794 if (lm->timerfd < 0)
795 {
796 err = memif_syscall_error_handler (errno);
797 goto error;
798 }
799
800 lm->arm.it_value.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
801 lm->arm.it_value.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
802 lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
803 lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
804
805 if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ,
806 lm->private_ctx) < 0)
807 {
808 DBG ("callback type memif_control_fd_update_t error!");
809 err = MEMIF_ERR_CB_FDUPDATE;
810 goto error;
811 }
812
813 *pt_main = lm;
814
815 return err;
816
817error:
818 *pt_main = lm;
819 memif_per_thread_cleanup (pt_main);
820 return err;
821}
822
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200823static inline memif_ring_t *
824memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
825 uint16_t ring_num)
826{
827 if (&conn->regions[0] == NULL)
828 return NULL;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200829 void *p = conn->regions[0].addr;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200830 int ring_size =
831 sizeof (memif_ring_t) +
832 sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size);
833 p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size;
834
835 return (memif_ring_t *) p;
836}
837
838int
839memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
840 uint16_t qid)
841{
842 memif_connection_t *conn = (memif_connection_t *) c;
843 if (conn == NULL)
844 return MEMIF_ERR_NOCONN;
845 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +0200846 (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->
847 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200848 if (qid >= num)
849 return MEMIF_ERR_QID;
850
851 conn->rx_queues[qid].ring->flags = rx_mode;
852 DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags);
853 return MEMIF_ERR_SUCCESS;
854}
855
Jakub Grajciar12df4972019-07-01 14:24:48 +0200856static int
857memif_socket_start_listening (memif_socket_t * ms)
858{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200859 libmemif_main_t *lm = get_libmemif_main (ms);
Jakub Grajciar12df4972019-07-01 14:24:48 +0200860 memif_list_elt_t elt;
861 struct stat file_stat;
862 struct sockaddr_un un = { 0 };
863 int on = 1;
864 int err = MEMIF_ERR_SUCCESS;
865
866 if (ms->type == MEMIF_SOCKET_TYPE_CLIENT)
867 return MEMIF_ERR_INVAL_ARG;
868
869 /* check if file exists */
870 if (stat ((char *) ms->filename, &file_stat) == 0)
871 {
872 if (S_ISSOCK (file_stat.st_mode))
873 unlink ((char *) ms->filename);
874 else
875 return memif_syscall_error_handler (errno);
876 }
877
878 ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
879 if (ms->fd < 0)
880 {
881 err = memif_syscall_error_handler (errno);
882 goto error;
883 }
884
885 DBG ("socket %d created", ms->fd);
886 un.sun_family = AF_UNIX;
887 strncpy ((char *) un.sun_path, (char *) ms->filename,
888 sizeof (un.sun_path) - 1);
889 if (setsockopt (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0)
890 {
891 err = memif_syscall_error_handler (errno);
892 goto error;
893 }
894 if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0)
895 {
896 err = memif_syscall_error_handler (errno);
897 goto error;
898 }
899 if (listen (ms->fd, 1) < 0)
900 {
901 err = memif_syscall_error_handler (errno);
902 goto error;
903 }
904 if (stat ((char *) ms->filename, &file_stat) < 0)
905 {
906 err = memif_syscall_error_handler (errno);
907 goto error;
908 }
909
910 /* add socket to libmemif main */
911 elt.key = ms->fd;
912 elt.data_struct = ms;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200913 add_list_elt (lm, &elt, &lm->socket_list, &lm->socket_list_len);
Jakub Grajciar12df4972019-07-01 14:24:48 +0200914 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ, ms->private_ctx);
915
916 ms->type = MEMIF_SOCKET_TYPE_LISTENER;
917
918 return err;
919
920error:
921 if (ms->fd > 0)
922 {
923 close (ms->fd);
924 ms->fd = -1;
925 }
926 return err;
927}
928
929int
930memif_create_socket (memif_socket_handle_t * sock, const char *filename,
931 void *private_ctx)
932{
933 libmemif_main_t *lm = &libmemif_main;
934 memif_socket_t *ms = (memif_socket_t *) * sock;
935 int i, err = MEMIF_ERR_SUCCESS;
936
937 for (i = 0; i < lm->socket_list_len; i++)
938 {
939 if ((ms = (memif_socket_t *) lm->socket_list[i].data_struct) != NULL)
940 {
941 if (strncmp ((char *) ms->filename, filename,
942 strlen ((char *) ms->filename)) == 0)
943 return MEMIF_ERR_INVAL_ARG;
944 }
945 }
946
947 /* allocate memif_socket_t */
948 ms = NULL;
949 ms = lm->alloc (sizeof (memif_socket_t));
950 if (ms == NULL)
951 {
952 err = MEMIF_ERR_NOMEM;
953 goto error;
954 }
955 memset (ms, 0, sizeof (memif_socket_t));
956 /* set filename */
957 ms->filename = lm->alloc (strlen (filename) + sizeof (char));
958 if (ms->filename == NULL)
959 {
960 err = MEMIF_ERR_NOMEM;
961 goto error;
962 }
963 memset (ms->filename, 0, strlen (filename) + sizeof (char));
964 strncpy ((char *) ms->filename, filename, strlen (filename));
965
966 ms->type = MEMIF_SOCKET_TYPE_NONE;
967
968 ms->interface_list_len = 1;
969 ms->interface_list =
970 lm->alloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
971 if (ms->interface_list == NULL)
972 {
973 err = MEMIF_ERR_NOMEM;
974 goto error;
975 }
976 ms->interface_list[0].key = -1;
977 ms->interface_list[0].data_struct = NULL;
978
979 *sock = ms;
980
981 return err;
982
983error:
984 if (ms != NULL)
985 {
986 if (ms->filename != NULL)
987 {
988 lm->free (ms->filename);
989 ms->filename = NULL;
990 }
991 if (ms->fd > 0)
992 {
993 close (ms->fd);
994 ms->fd = -1;
995 }
996 if (ms->interface_list != NULL)
997 {
998 lm->free (ms->interface_list);
999 ms->interface_list = NULL;
1000 ms->interface_list_len = 0;
1001 }
1002 lm->free (ms);
1003 *sock = ms = NULL;
1004 }
1005 return err;
1006}
1007
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001008int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001009memif_per_thread_create_socket (memif_per_thread_main_handle_t pt_main,
1010 memif_socket_handle_t * sock,
1011 const char *filename, void *private_ctx)
1012{
1013 libmemif_main_t *lm = (libmemif_main_t *) pt_main;
1014 memif_socket_t *ms = (memif_socket_t *) * sock;
1015 int i, err = MEMIF_ERR_SUCCESS;
1016
1017 if (lm == NULL)
1018 return MEMIF_ERR_INVAL_ARG;
1019
1020 for (i = 0; i < lm->socket_list_len; i++)
1021 {
1022 if ((ms = (memif_socket_t *) lm->socket_list[i].data_struct) != NULL)
1023 {
1024 if (strncmp ((char *) ms->filename, filename,
1025 strlen ((char *) ms->filename)) == 0)
1026 return MEMIF_ERR_INVAL_ARG;
1027 }
1028 }
1029
1030 /* allocate memif_socket_t */
1031 ms = NULL;
1032 ms = lm->alloc (sizeof (memif_socket_t));
1033 if (ms == NULL)
1034 {
1035 err = MEMIF_ERR_NOMEM;
1036 goto error;
1037 }
1038 memset (ms, 0, sizeof (memif_socket_t));
1039 ms->lm = lm;
1040 /* set filename */
1041 ms->filename = lm->alloc (strlen (filename) + sizeof (char));
1042 if (ms->filename == NULL)
1043 {
1044 err = MEMIF_ERR_NOMEM;
1045 goto error;
1046 }
1047 memset (ms->filename, 0, strlen (filename) + sizeof (char));
1048 strncpy ((char *) ms->filename, filename, strlen (filename));
1049
1050 ms->type = MEMIF_SOCKET_TYPE_NONE;
1051
1052 ms->interface_list_len = 1;
1053 ms->interface_list =
1054 lm->alloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
1055 if (ms->interface_list == NULL)
1056 {
1057 err = MEMIF_ERR_NOMEM;
1058 goto error;
1059 }
1060 ms->interface_list[0].key = -1;
1061 ms->interface_list[0].data_struct = NULL;
1062
1063 *sock = ms;
1064
1065 return err;
1066
1067error:
1068 if (ms != NULL)
1069 {
1070 if (ms->filename != NULL)
1071 {
1072 lm->free (ms->filename);
1073 ms->filename = NULL;
1074 }
1075 if (ms->fd > 0)
1076 {
1077 close (ms->fd);
1078 ms->fd = -1;
1079 }
1080 if (ms->interface_list != NULL)
1081 {
1082 lm->free (ms->interface_list);
1083 ms->interface_list = NULL;
1084 ms->interface_list_len = 0;
1085 }
1086 lm->free (ms);
1087 *sock = ms = NULL;
1088 }
1089 return err;
1090}
1091
1092int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001093memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
1094 memif_connection_update_t * on_connect,
1095 memif_connection_update_t * on_disconnect,
1096 memif_interrupt_t * on_interrupt, void *private_ctx)
1097{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001098 libmemif_main_t *lm = get_libmemif_main (args->socket);
Jakub Grajciar12df4972019-07-01 14:24:48 +02001099 int err, index = 0;
1100 memif_list_elt_t elt;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001101 memif_connection_t *conn = (memif_connection_t *) * c;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001102 memif_socket_t *ms;
1103
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001104 if (conn != NULL)
1105 {
1106 DBG ("This handle already points to existing memif.");
1107 return MEMIF_ERR_CONN;
1108 }
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001109
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001110 conn = (memif_connection_t *) lm->alloc (sizeof (memif_connection_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001111 if (conn == NULL)
1112 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001113 err = MEMIF_ERR_NOMEM;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001114 goto error;
1115 }
1116 memset (conn, 0, sizeof (memif_connection_t));
1117
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001118 conn->args.interface_id = args->interface_id;
1119
1120 if (args->log2_ring_size == 0)
1121 args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001122 else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE)
1123 {
1124 err = MEMIF_ERR_MAX_RING;
1125 goto error;
1126 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001127 if (args->buffer_size == 0)
1128 args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
1129 if (args->num_s2m_rings == 0)
1130 args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES;
1131 if (args->num_m2s_rings == 0)
1132 args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES;
1133
1134 conn->args.num_s2m_rings = args->num_s2m_rings;
1135 conn->args.num_m2s_rings = args->num_m2s_rings;
1136 conn->args.buffer_size = args->buffer_size;
1137 conn->args.log2_ring_size = args->log2_ring_size;
1138 conn->args.is_master = args->is_master;
1139 conn->args.mode = args->mode;
1140 conn->msg_queue = NULL;
1141 conn->regions = NULL;
1142 conn->tx_queues = NULL;
1143 conn->rx_queues = NULL;
1144 conn->fd = -1;
1145 conn->on_connect = on_connect;
1146 conn->on_disconnect = on_disconnect;
1147 conn->on_interrupt = on_interrupt;
1148 conn->private_ctx = private_ctx;
1149 memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t));
1150
1151 uint8_t l = strlen ((char *) args->interface_name);
1152 strncpy ((char *) conn->args.interface_name, (char *) args->interface_name,
1153 l);
1154
Jakub Grajciar12df4972019-07-01 14:24:48 +02001155 if ((l = strlen ((char *) args->secret)) > 0)
1156 strncpy ((char *) conn->args.secret, (char *) args->secret, l);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001157
Jakub Grajciar12df4972019-07-01 14:24:48 +02001158 if (args->socket != NULL)
1159 conn->args.socket = args->socket;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001160 else if (lm->default_socket != NULL)
1161 conn->args.socket = lm->default_socket;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001162 else
1163 {
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001164 err = MEMIF_ERR_INVAL_ARG;
1165 goto error;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001166 }
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001167
Jakub Grajciar12df4972019-07-01 14:24:48 +02001168 ms = (memif_socket_t *) conn->args.socket;
1169
1170 if ((conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_CLIENT) ||
1171 (!conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_LISTENER))
1172 {
1173 err = MEMIF_ERR_INVAL_ARG;
1174 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001175 }
1176
Jakub Grajciar12df4972019-07-01 14:24:48 +02001177 elt.key = conn->args.interface_id;
1178 elt.data_struct = conn;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001179 add_list_elt (lm, &elt, &ms->interface_list, &ms->interface_list_len);
Jakub Grajciar12df4972019-07-01 14:24:48 +02001180 ms->use_count++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001181
1182 if (conn->args.is_master)
1183 {
Jakub Grajciar12df4972019-07-01 14:24:48 +02001184 if (ms->type == MEMIF_SOCKET_TYPE_NONE)
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001185 {
1186 err = memif_socket_start_listening (ms);
1187 if (err != MEMIF_ERR_SUCCESS)
1188 goto error;
1189 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001190 }
1191 else
1192 {
Jakub Grajciar12df4972019-07-01 14:24:48 +02001193 elt.key = -1;
1194 elt.data_struct = conn;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001195 if ((index =
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001196 add_list_elt (lm, &elt, &lm->control_list,
1197 &lm->control_list_len)) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001198 {
1199 err = MEMIF_ERR_NOMEM;
1200 goto error;
1201 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001202
Jakub Grajciar84b83772019-03-04 12:42:19 +01001203 conn->index = index;
1204
1205 /* try connectiong to master */
Jakub Grajciar12df4972019-07-01 14:24:48 +02001206 err = memif_request_connection (conn);
Jakub Grajciar272e2e42019-07-24 10:33:11 +02001207 if ((err != MEMIF_ERR_SUCCESS) && (lm->disconn_slaves == 0))
Jakub Grajciar84b83772019-03-04 12:42:19 +01001208 {
1209 /* connection failed, arm reconnect timer (if not armed) */
1210 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
1211 {
1212 err = memif_syscall_error_handler (errno);
1213 goto error;
1214 }
1215 }
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001216 lm->disconn_slaves++;
Jakub Grajciar84b83772019-03-04 12:42:19 +01001217 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001218
Jakub Grajciar12df4972019-07-01 14:24:48 +02001219 *c = conn;
1220
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001221 return 0;
1222
1223error:
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001224 if (conn != NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001225 lm->free (conn);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001226 *c = conn = NULL;
1227 return err;
1228}
1229
1230int
Jakub Grajciar12df4972019-07-01 14:24:48 +02001231memif_request_connection (memif_conn_handle_t c)
Jakub Grajciar84b83772019-03-04 12:42:19 +01001232{
Jakub Grajciar84b83772019-03-04 12:42:19 +01001233 memif_connection_t *conn = (memif_connection_t *) c;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001234 libmemif_main_t *lm;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001235 memif_socket_t *ms;
Jakub Grajciar84b83772019-03-04 12:42:19 +01001236 int err = MEMIF_ERR_SUCCESS;
1237 int sockfd = -1;
1238 struct sockaddr_un sun;
1239
Jakub Grajciar12df4972019-07-01 14:24:48 +02001240 if (conn == NULL)
1241 return MEMIF_ERR_NOCONN;
1242
1243 ms = (memif_socket_t *) conn->args.socket;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001244 lm = get_libmemif_main (ms);
1245
Jakub Grajciar12df4972019-07-01 14:24:48 +02001246
1247 if (conn->args.is_master || ms->type == MEMIF_SOCKET_TYPE_LISTENER)
Jakub Grajciar84b83772019-03-04 12:42:19 +01001248 return MEMIF_ERR_INVAL_ARG;
1249 if (conn->fd > 0)
1250 return MEMIF_ERR_ALRCONN;
1251
1252 sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
1253 if (sockfd < 0)
1254 {
1255 err = memif_syscall_error_handler (errno);
1256 goto error;
1257 }
1258
1259 sun.sun_family = AF_UNIX;
1260
Jakub Grajciar12df4972019-07-01 14:24:48 +02001261 strncpy (sun.sun_path, (char *) ms->filename, sizeof (sun.sun_path) - 1);
Jakub Grajciar84b83772019-03-04 12:42:19 +01001262
1263 if (connect (sockfd, (struct sockaddr *) &sun,
Jakub Grajciar12df4972019-07-01 14:24:48 +02001264 sizeof (struct sockaddr_un)) == 0)
Jakub Grajciar84b83772019-03-04 12:42:19 +01001265 {
1266 conn->fd = sockfd;
1267 conn->read_fn = memif_conn_fd_read_ready;
1268 conn->write_fn = memif_conn_fd_write_ready;
1269 conn->error_fn = memif_conn_fd_error;
1270
1271 lm->control_list[conn->index].key = conn->fd;
1272
1273 lm->control_fd_update (sockfd,
Jakub Grajciar12df4972019-07-01 14:24:48 +02001274 MEMIF_FD_EVENT_READ |
1275 MEMIF_FD_EVENT_WRITE, conn->private_ctx);
Jakub Grajciar84b83772019-03-04 12:42:19 +01001276
1277 lm->disconn_slaves--;
1278 if (lm->disconn_slaves == 0)
1279 {
1280 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
1281 {
1282 err = memif_syscall_error_handler (errno);
1283 return err;
1284 }
1285 }
1286 }
1287 else
1288 {
Jakub Grajciar272e2e42019-07-24 10:33:11 +02001289 err = memif_syscall_error_handler (errno);
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001290 strcpy ((char *) conn->remote_disconnect_string, memif_strerror (err));
Jakub Grajciar84b83772019-03-04 12:42:19 +01001291 goto error;
1292 }
1293
Jakub Grajciar12df4972019-07-01 14:24:48 +02001294 ms->type = MEMIF_SOCKET_TYPE_CLIENT;
1295
1296 return err;
Jakub Grajciar84b83772019-03-04 12:42:19 +01001297
1298error:
1299 if (sockfd > 0)
1300 close (sockfd);
1301 sockfd = -1;
1302 return err;
1303}
1304
1305int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001306memif_control_fd_handler (int fd, uint8_t events)
1307{
Jakub Grajciar84b83772019-03-04 12:42:19 +01001308 int i, err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001309 uint16_t num;
1310 memif_list_elt_t *e = NULL;
1311 memif_connection_t *conn;
1312 libmemif_main_t *lm = &libmemif_main;
1313 if (fd == lm->timerfd)
1314 {
1315 uint64_t b;
1316 ssize_t size;
1317 size = read (fd, &b, sizeof (b));
msardara8f554b72018-12-11 18:36:55 +01001318
1319 if (size == -1)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001320 goto error;
msardara8f554b72018-12-11 18:36:55 +01001321
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001322 for (i = 0; i < lm->control_list_len; i++)
1323 {
1324 if ((lm->control_list[i].key < 0)
1325 && (lm->control_list[i].data_struct != NULL))
1326 {
1327 conn = lm->control_list[i].data_struct;
1328 if (conn->args.is_master)
1329 continue;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001330 err = memif_request_connection (conn);
1331 if (err != MEMIF_ERR_SUCCESS)
1332 DBG ("memif_request_connection: %s", memif_strerror (err));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001333 }
1334 }
1335 }
1336 else
1337 {
1338 get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
1339 if (e != NULL)
1340 {
1341 if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
1342 {
1343 num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001344 (((memif_connection_t *) e->data_struct)->
1345 args.is_master) ? ((memif_connection_t *) e->
1346 data_struct)->run_args.
1347 num_s2m_rings : ((memif_connection_t *) e->data_struct)->
1348 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001349 for (i = 0; i < num; i++)
1350 {
Jakub Grajciar568cc462018-09-05 12:11:35 +02001351 if (((memif_connection_t *) e->data_struct)->
1352 rx_queues[i].int_fd == fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001353 {
Jakub Grajciar568cc462018-09-05 12:11:35 +02001354 ((memif_connection_t *) e->data_struct)->
1355 on_interrupt ((void *) e->data_struct,
1356 ((memif_connection_t *) e->
1357 data_struct)->private_ctx, i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001358 return MEMIF_ERR_SUCCESS;
1359 }
1360 }
1361 }
1362 return MEMIF_ERR_SUCCESS;
1363 }
Jakub Grajciar12df4972019-07-01 14:24:48 +02001364 get_list_elt (&e, lm->socket_list, lm->socket_list_len, fd);
1365 if (e != NULL
1366 && ((memif_socket_t *) e->data_struct)->type ==
1367 MEMIF_SOCKET_TYPE_LISTENER)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001368 {
Jakub Grajciar12df4972019-07-01 14:24:48 +02001369 err =
1370 memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
Jakub Grajciar84b83772019-03-04 12:42:19 +01001371 return err;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001372 }
1373
1374 get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
1375 if (e != NULL)
1376 {
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001377 err = memif_read_ready (lm, fd);
1378 return err;
1379 }
1380
1381 get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
1382 if (e != NULL)
1383 {
1384 if (events & MEMIF_FD_EVENT_READ)
1385 {
1386 err =
1387 ((memif_connection_t *) e->data_struct)->
1388 read_fn (e->data_struct);
1389 if (err != MEMIF_ERR_SUCCESS)
1390 return err;
1391 }
1392 if (events & MEMIF_FD_EVENT_WRITE)
1393 {
1394 err =
1395 ((memif_connection_t *) e->data_struct)->
1396 write_fn (e->data_struct);
1397 if (err != MEMIF_ERR_SUCCESS)
1398 return err;
1399 }
1400 if (events & MEMIF_FD_EVENT_ERROR)
1401 {
1402 err =
1403 ((memif_connection_t *) e->data_struct)->
1404 error_fn (e->data_struct);
1405 if (err != MEMIF_ERR_SUCCESS)
1406 return err;
1407 }
1408 }
1409 }
1410
1411 return MEMIF_ERR_SUCCESS; /* 0 */
1412
1413error:
1414 return err;
1415}
1416
1417int
1418memif_per_thread_control_fd_handler (memif_per_thread_main_handle_t pt_main,
1419 int fd, uint8_t events)
1420{
1421 int i, err = MEMIF_ERR_SUCCESS; /* 0 */
1422 uint16_t num;
1423 memif_list_elt_t *e = NULL;
1424 memif_connection_t *conn;
1425 libmemif_main_t *lm = (libmemif_main_t *) pt_main;
1426
1427 if (fd == lm->timerfd)
1428 {
1429 uint64_t b;
1430 ssize_t size;
1431 size = read (fd, &b, sizeof (b));
1432
1433 if (size == -1)
1434 goto error;
1435
1436 for (i = 0; i < lm->control_list_len; i++)
1437 {
1438 if ((lm->control_list[i].key < 0)
1439 && (lm->control_list[i].data_struct != NULL))
1440 {
1441 conn = lm->control_list[i].data_struct;
1442 if (conn->args.is_master)
1443 continue;
1444 err = memif_request_connection (conn);
1445 if (err != MEMIF_ERR_SUCCESS)
1446 DBG ("memif_request_connection: %s", memif_strerror (err));
1447 }
1448 }
1449 }
1450 else
1451 {
1452 get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
1453 if (e != NULL)
1454 {
1455 if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
1456 {
1457 num =
1458 (((memif_connection_t *) e->data_struct)->
1459 args.is_master) ? ((memif_connection_t *) e->
1460 data_struct)->run_args.
1461 num_s2m_rings : ((memif_connection_t *) e->data_struct)->
1462 run_args.num_m2s_rings;
1463 for (i = 0; i < num; i++)
1464 {
1465 if (((memif_connection_t *) e->data_struct)->
1466 rx_queues[i].int_fd == fd)
1467 {
1468 ((memif_connection_t *) e->data_struct)->
1469 on_interrupt ((void *) e->data_struct,
1470 ((memif_connection_t *) e->
1471 data_struct)->private_ctx, i);
1472 return MEMIF_ERR_SUCCESS;
1473 }
1474 }
1475 }
1476 return MEMIF_ERR_SUCCESS;
1477 }
1478 get_list_elt (&e, lm->socket_list, lm->socket_list_len, fd);
1479 if (e != NULL
1480 && ((memif_socket_t *) e->data_struct)->type ==
1481 MEMIF_SOCKET_TYPE_LISTENER)
1482 {
1483 err =
1484 memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
1485 return err;
1486 }
1487
1488 get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
1489 if (e != NULL)
1490 {
1491 err = memif_read_ready (lm, fd);
Jakub Grajciar84b83772019-03-04 12:42:19 +01001492 return err;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001493 }
1494
1495 get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
1496 if (e != NULL)
1497 {
1498 if (events & MEMIF_FD_EVENT_READ)
1499 {
1500 err =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001501 ((memif_connection_t *) e->data_struct)->
1502 read_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001503 if (err != MEMIF_ERR_SUCCESS)
1504 return err;
1505 }
1506 if (events & MEMIF_FD_EVENT_WRITE)
1507 {
1508 err =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001509 ((memif_connection_t *) e->data_struct)->
1510 write_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001511 if (err != MEMIF_ERR_SUCCESS)
1512 return err;
1513 }
1514 if (events & MEMIF_FD_EVENT_ERROR)
1515 {
1516 err =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001517 ((memif_connection_t *) e->data_struct)->
1518 error_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001519 if (err != MEMIF_ERR_SUCCESS)
1520 return err;
1521 }
1522 }
1523 }
1524
1525 return MEMIF_ERR_SUCCESS; /* 0 */
1526
1527error:
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001528 return err;
1529}
1530
1531int
1532memif_poll_event (int timeout)
1533{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001534 libmemif_main_t *lm = &libmemif_main;
msardara8f554b72018-12-11 18:36:55 +01001535 struct epoll_event evt;
1536 int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001537 uint32_t events = 0;
Milan Lenco0a47c992017-10-12 14:19:31 +02001538 uint64_t counter = 0;
1539 ssize_t r = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001540 memset (&evt, 0, sizeof (evt));
1541 evt.events = EPOLLIN | EPOLLOUT;
1542 sigset_t sigset;
1543 sigemptyset (&sigset);
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001544 en = epoll_pwait (lm->epfd, &evt, 1, timeout, &sigset);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001545 if (en < 0)
1546 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001547 err = errno;
1548 DBG ("epoll_pwait: %s", strerror (err));
1549 return memif_syscall_error_handler (err);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001550 }
1551 if (en > 0)
1552 {
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001553 if (evt.data.fd == lm->poll_cancel_fd)
1554 {
1555 r = read (evt.data.fd, &counter, sizeof (counter));
1556 if (r == -1)
1557 return MEMIF_ERR_DISCONNECTED;
1558
1559 return MEMIF_ERR_POLL_CANCEL;
1560 }
1561 if (evt.events & EPOLLIN)
1562 events |= MEMIF_FD_EVENT_READ;
1563 if (evt.events & EPOLLOUT)
1564 events |= MEMIF_FD_EVENT_WRITE;
1565 if (evt.events & EPOLLERR)
1566 events |= MEMIF_FD_EVENT_ERROR;
1567 err = memif_control_fd_handler (evt.data.fd, events);
1568 return err;
1569 }
1570 return 0;
1571}
1572
1573int
1574memif_per_thread_poll_event (memif_per_thread_main_handle_t pt_main,
1575 int timeout)
1576{
1577 libmemif_main_t *lm = (libmemif_main_t *) pt_main;
1578 struct epoll_event evt;
1579 int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */
1580 uint32_t events = 0;
1581 uint64_t counter = 0;
1582 ssize_t r = 0;
1583 memset (&evt, 0, sizeof (evt));
1584 evt.events = EPOLLIN | EPOLLOUT;
1585 sigset_t sigset;
1586 sigemptyset (&sigset);
1587 en = epoll_pwait (lm->epfd, &evt, 1, timeout, &sigset);
1588 if (en < 0)
1589 {
1590 err = errno;
1591 DBG ("epoll_pwait: %s", strerror (err));
1592 return memif_syscall_error_handler (err);
1593 }
1594 if (en > 0)
1595 {
1596 if (evt.data.fd == lm->poll_cancel_fd)
Milan Lenco0a47c992017-10-12 14:19:31 +02001597 {
1598 r = read (evt.data.fd, &counter, sizeof (counter));
msardara8f554b72018-12-11 18:36:55 +01001599 if (r == -1)
1600 return MEMIF_ERR_DISCONNECTED;
1601
Milan Lenco0a47c992017-10-12 14:19:31 +02001602 return MEMIF_ERR_POLL_CANCEL;
1603 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001604 if (evt.events & EPOLLIN)
1605 events |= MEMIF_FD_EVENT_READ;
1606 if (evt.events & EPOLLOUT)
1607 events |= MEMIF_FD_EVENT_WRITE;
1608 if (evt.events & EPOLLERR)
1609 events |= MEMIF_FD_EVENT_ERROR;
1610 err = memif_control_fd_handler (evt.data.fd, events);
1611 return err;
1612 }
1613 return 0;
1614}
1615
Milan Lenco0a47c992017-10-12 14:19:31 +02001616int
1617memif_cancel_poll_event ()
1618{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001619 libmemif_main_t *lm = &libmemif_main;
Milan Lenco0a47c992017-10-12 14:19:31 +02001620 uint64_t counter = 1;
1621 ssize_t w = 0;
1622
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001623 if (lm->poll_cancel_fd == -1)
Milan Lenco0a47c992017-10-12 14:19:31 +02001624 return 0;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001625 w = write (lm->poll_cancel_fd, &counter, sizeof (counter));
1626 if (w < sizeof (counter))
1627 return MEMIF_ERR_INT_WRITE;
1628
1629 return 0;
1630}
1631
1632int
1633memif_per_thread_cancel_poll_event (memif_per_thread_main_handle_t pt_main)
1634{
1635 libmemif_main_t *lm = (libmemif_main_t *) pt_main;
1636 uint64_t counter = 1;
1637 ssize_t w = 0;
1638
1639 if (lm == NULL)
1640 return MEMIF_ERR_INVAL_ARG;
1641
1642 if (lm->poll_cancel_fd == -1)
1643 return 0;
1644 w = write (lm->poll_cancel_fd, &counter, sizeof (counter));
Milan Lenco0a47c992017-10-12 14:19:31 +02001645 if (w < sizeof (counter))
1646 return MEMIF_ERR_INT_WRITE;
1647
1648 return 0;
1649}
1650
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001651static void
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001652memif_msg_queue_free (libmemif_main_t * lm, memif_msg_queue_elt_t ** e)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001653{
1654 if (*e == NULL)
1655 return;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001656 memif_msg_queue_free (lm, &(*e)->next);
1657 lm->free (*e);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001658 *e = NULL;
1659 return;
1660}
1661
1662/* send disconnect msg and close interface */
1663int
1664memif_disconnect_internal (memif_connection_t * c)
1665{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001666 int err = MEMIF_ERR_SUCCESS, i; /* 0 */
1667 memif_queue_t *mq;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001668 libmemif_main_t *lm;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001669 memif_list_elt_t *e;
1670
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001671 if (c == NULL)
1672 {
1673 DBG ("no connection");
1674 return MEMIF_ERR_NOCONN;
1675 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001676
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001677 lm = get_libmemif_main (c->args.socket);
1678
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001679 c->on_disconnect ((void *) c, c->private_ctx);
1680
1681 if (c->fd > 0)
1682 {
Jakub Grajciar5e1f69a2018-04-12 13:56:29 +02001683 memif_msg_send_disconnect (c->fd, (uint8_t *) "interface deleted", 0);
Jakub Grajciar12df4972019-07-01 14:24:48 +02001684 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL, c->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001685 close (c->fd);
1686 }
1687 get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd);
1688 if (e != NULL)
1689 {
1690 if (c->args.is_master)
1691 free_list_elt (lm->control_list, lm->control_list_len, c->fd);
1692 e->key = c->fd = -1;
1693 }
1694
1695 if (c->tx_queues != NULL)
1696 {
Jakub Grajciar718a19b2019-11-05 10:58:00 +01001697 for (i = 0; i < c->tx_queues_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001698 {
1699 mq = &c->tx_queues[i];
1700 if (mq != NULL)
1701 {
1702 if (mq->int_fd > 0)
1703 close (mq->int_fd);
1704 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1705 mq->int_fd);
1706 mq->int_fd = -1;
1707 }
1708 }
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001709 lm->free (c->tx_queues);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001710 c->tx_queues = NULL;
1711 }
Jakub Grajciar718a19b2019-11-05 10:58:00 +01001712 c->tx_queues_num = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001713
1714 if (c->rx_queues != NULL)
1715 {
Jakub Grajciar718a19b2019-11-05 10:58:00 +01001716 for (i = 0; i < c->rx_queues_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001717 {
1718 mq = &c->rx_queues[i];
1719 if (mq != NULL)
1720 {
1721 if (mq->int_fd > 0)
1722 {
1723 if (c->on_interrupt != NULL)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001724 lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL,
1725 c->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001726 close (mq->int_fd);
1727 }
1728 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1729 mq->int_fd);
1730 mq->int_fd = -1;
1731 }
1732 }
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001733 lm->free (c->rx_queues);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001734 c->rx_queues = NULL;
1735 }
Jakub Grajciar718a19b2019-11-05 10:58:00 +01001736 c->rx_queues_num = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001737
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001738 for (i = 0; i < c->regions_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001739 {
Jakub Grajciar412e7b62018-09-27 10:26:35 +02001740 if (&c->regions[i] == NULL)
1741 continue;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001742 if (c->regions[i].is_external != 0)
1743 {
1744 lm->del_external_region (c->regions[i].addr,
1745 c->regions[i].region_size,
1746 c->regions[i].fd, c->private_ctx);
1747 }
1748 else
1749 {
1750 if (munmap (c->regions[i].addr, c->regions[i].region_size) < 0)
1751 return memif_syscall_error_handler (errno);
1752 if (c->regions[i].fd > 0)
1753 close (c->regions[i].fd);
1754 c->regions[i].fd = -1;
1755 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001756 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001757 lm->free (c->regions);
1758 c->regions = NULL;
1759 c->regions_num = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001760
1761 memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
1762
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001763 memif_msg_queue_free (lm, &c->msg_queue);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001764
1765 if (!(c->args.is_master))
1766 {
1767 if (lm->disconn_slaves == 0)
1768 {
1769 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
1770 {
1771 err = memif_syscall_error_handler (errno);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001772 DBG ("timerfd_settime: arm");
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001773 }
1774 }
1775 lm->disconn_slaves++;
1776 }
1777
1778 return err;
1779}
1780
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001781const char *
1782memif_get_socket_filename (memif_socket_handle_t sock)
1783{
1784 memif_socket_t *ms = (memif_socket_t *) sock;
1785
1786 if (ms == NULL)
1787 return NULL;
1788
1789 return (char *) ms->filename;
1790}
1791
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001792int
Jakub Grajciar12df4972019-07-01 14:24:48 +02001793memif_delete_socket (memif_socket_handle_t * sock)
1794{
1795 memif_socket_t *ms = (memif_socket_t *) * sock;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001796 libmemif_main_t *lm;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001797
1798 /* check if socket is in use */
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001799 if (ms == NULL || ms->use_count > 0)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001800 return MEMIF_ERR_INVAL_ARG;
1801
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001802 lm = get_libmemif_main (ms);
1803
Jakub Grajciar12df4972019-07-01 14:24:48 +02001804 lm->free (ms->interface_list);
1805 ms->interface_list = NULL;
1806 lm->free (ms->filename);
1807 ms->filename = NULL;
1808 lm->free (ms);
1809 *sock = ms = NULL;
1810
1811 return MEMIF_ERR_SUCCESS;
1812}
1813
1814int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001815memif_delete (memif_conn_handle_t * conn)
1816{
1817 memif_connection_t *c = (memif_connection_t *) * conn;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001818 libmemif_main_t *lm;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001819 memif_socket_t *ms = NULL;
1820 int err = MEMIF_ERR_SUCCESS;
1821
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001822 if (c == NULL)
1823 {
1824 DBG ("no connection");
1825 return MEMIF_ERR_NOCONN;
1826 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001827
1828 if (c->fd > 0)
1829 {
1830 DBG ("DISCONNECTING");
1831 err = memif_disconnect_internal (c);
1832 if (err == MEMIF_ERR_NOCONN)
1833 return err;
1834 }
1835
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001836 lm = get_libmemif_main (c->args.socket);
1837
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001838 free_list_elt_ctx (lm->control_list, lm->control_list_len, c);
1839
Jakub Grajciar12df4972019-07-01 14:24:48 +02001840 ms = (memif_socket_t *) c->args.socket;
1841 ms->use_count--;
1842 free_list_elt (ms->interface_list, ms->interface_list_len,
1843 c->args.interface_id);
1844 if (ms->use_count <= 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001845 {
Jakub Grajciar12df4972019-07-01 14:24:48 +02001846 /* stop listening on this socket */
1847 if (ms->type == MEMIF_SOCKET_TYPE_LISTENER)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001848 {
Jakub Grajciar12df4972019-07-01 14:24:48 +02001849 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_DEL, ms->private_ctx);
1850 free_list_elt (lm->socket_list, lm->socket_list_len, ms->fd);
1851 close (ms->fd);
1852 ms->fd = -1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001853 }
Jakub Grajciar12df4972019-07-01 14:24:48 +02001854 /* socket not in use */
1855 ms->type = MEMIF_SOCKET_TYPE_NONE;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001856 }
Jakub Grajciar12df4972019-07-01 14:24:48 +02001857
1858 if (!c->args.is_master)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001859 {
1860 lm->disconn_slaves--;
1861 if (lm->disconn_slaves <= 0)
1862 {
1863 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
1864 {
1865 err = memif_syscall_error_handler (errno);
1866 DBG ("timerfd_settime: disarm");
1867 }
1868 }
1869 }
1870
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001871 lm->free (c);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001872 c = NULL;
1873
1874 *conn = c;
1875 return err;
1876}
1877
1878int
1879memif_connect1 (memif_connection_t * c)
1880{
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001881 libmemif_main_t *lm;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001882 memif_region_t *mr;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001883 memif_queue_t *mq;
1884 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001885
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001886 if (c == NULL)
1887 return MEMIF_ERR_INVAL_ARG;
1888
1889 lm = get_libmemif_main (c->args.socket);
1890
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001891 for (i = 0; i < c->regions_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001892 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001893 mr = &c->regions[i];
1894 if (mr != NULL)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001895 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001896 if (!mr->addr)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001897 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001898 if (mr->is_external)
1899 {
1900 if (lm->get_external_region_addr == NULL)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001901 return MEMIF_ERR_INVAL_ARG;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001902 mr->addr =
1903 lm->get_external_region_addr (mr->region_size, mr->fd,
1904 c->private_ctx);
1905 }
1906 else
1907 {
1908 if (mr->fd < 0)
1909 return MEMIF_ERR_NO_SHMFD;
1910
1911 if ((mr->addr =
1912 mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
1913 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
1914 {
1915 return memif_syscall_error_handler (errno);
1916 }
1917 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001918 }
1919 }
1920 }
1921
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001922 for (i = 0; i < c->rx_queues_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001923 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001924 mq = &c->rx_queues[i];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001925 if (mq != NULL)
1926 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001927 mq->ring = c->regions[mq->region].addr + mq->offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001928 if (mq->ring->cookie != MEMIF_COOKIE)
1929 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001930 DBG ("wrong cookie on rx ring %u", i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001931 return MEMIF_ERR_COOKIE;
1932 }
1933 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1934 0;
1935 }
1936 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001937
1938 for (i = 0; i < c->tx_queues_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001939 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001940 mq = &c->tx_queues[i];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001941 if (mq != NULL)
1942 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001943 mq->ring = c->regions[mq->region].addr + mq->offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001944 if (mq->ring->cookie != MEMIF_COOKIE)
1945 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001946 DBG ("wrong cookie on tx ring %u", i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001947 return MEMIF_ERR_COOKIE;
1948 }
1949 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1950 0;
1951 }
1952 }
1953
Jakub Grajciar12df4972019-07-01 14:24:48 +02001954 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD,
1955 c->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001956
1957 return 0;
1958}
1959
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001960static inline int
1961memif_add_region (libmemif_main_t * lm, memif_connection_t * conn,
1962 uint8_t has_buffers)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001963{
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001964 memif_region_t *r;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001965
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001966 r =
1967 lm->realloc (conn->regions,
1968 sizeof (memif_region_t) * ++conn->regions_num);
1969 if (r == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001970 return MEMIF_ERR_NOMEM;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001971
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001972 conn->regions = r;
1973 r = &conn->regions[conn->regions_num - 1];
1974 memset (r, 0, sizeof (memif_region_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001975
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001976 if (has_buffers != 0)
1977 {
1978 r->buffer_offset = 0;
1979 }
1980 else
1981 {
1982 r->buffer_offset =
1983 (conn->run_args.num_s2m_rings +
1984 conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
1985 sizeof (memif_desc_t) *
Jakub Grajciar568cc462018-09-05 12:11:35 +02001986 (1 << conn->
1987 run_args.log2_ring_size));
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001988 }
1989
1990 r->region_size = (has_buffers == 0) ? r->buffer_offset : r->buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001991 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1992 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1993
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001994 if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001995 return memif_syscall_error_handler (errno);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001996
1997 if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
1998 return memif_syscall_error_handler (errno);
1999
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002000 if ((ftruncate (r->fd, r->region_size)) == -1)
2001 return memif_syscall_error_handler (errno);
2002
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002003 if ((r->addr = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
2004 MAP_SHARED, r->fd, 0)) == MAP_FAILED)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002005 return memif_syscall_error_handler (errno);
2006
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002007 return MEMIF_ERR_SUCCESS;
2008}
2009
2010static inline int
2011memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn)
2012{
2013 int i, j;
2014 memif_ring_t *ring;
2015
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002016 for (i = 0; i < conn->run_args.num_s2m_rings; i++)
2017 {
2018 ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
2019 DBG ("RING: %p I: %d", ring, i);
2020 ring->head = ring->tail = 0;
2021 ring->cookie = MEMIF_COOKIE;
2022 ring->flags = 0;
2023 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
2024 {
2025 uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002026 ring->desc[j].region = 1;
2027 ring->desc[j].offset =
2028 conn->regions[1].buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002029 (uint32_t) (slot * conn->run_args.buffer_size);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002030 ring->desc[j].length = conn->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002031 }
2032 }
2033 for (i = 0; i < conn->run_args.num_m2s_rings; i++)
2034 {
2035 ring = memif_get_ring (conn, MEMIF_RING_M2S, i);
2036 DBG ("RING: %p I: %d", ring, i);
2037 ring->head = ring->tail = 0;
2038 ring->cookie = MEMIF_COOKIE;
2039 ring->flags = 0;
2040 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
2041 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002042 uint16_t slot = (i + conn->run_args.num_s2m_rings) *
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002043 (1 << conn->run_args.log2_ring_size) + j;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002044 ring->desc[j].region = 1;
2045 ring->desc[j].offset =
2046 conn->regions[1].buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002047 (uint32_t) (slot * conn->run_args.buffer_size);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002048 ring->desc[j].length = conn->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002049 }
2050 }
2051 memif_queue_t *mq;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002052 DBG ("alloc: %p", lm->alloc);
2053 DBG ("size: %lu", sizeof (memif_queue_t) * conn->run_args.num_s2m_rings);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002054 mq =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002055 (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
2056 conn->run_args.num_s2m_rings);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002057 if (mq == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002058 return MEMIF_ERR_NOMEM;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002059
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002060 int x;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002061 memif_list_elt_t e;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002062 for (x = 0; x < conn->run_args.num_s2m_rings; x++)
2063 {
2064 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
2065 return memif_syscall_error_handler (errno);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002066 e.key = mq[x].int_fd;
2067 e.data_struct = conn;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02002068 add_list_elt (lm, &e, &lm->interrupt_list, &lm->interrupt_list_len);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002069
2070 mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
2071 DBG ("RING: %p I: %d", mq[x].ring, x);
2072 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
2073 mq[x].region = 0;
2074 mq[x].offset =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002075 (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
2076 mq[x].last_head = mq[x].last_tail = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002077 mq[x].alloc_bufs = 0;
2078 }
2079 conn->tx_queues = mq;
Jakub Grajciar718a19b2019-11-05 10:58:00 +01002080 conn->tx_queues_num = conn->run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002081
2082 mq =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002083 (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
2084 conn->run_args.num_m2s_rings);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002085 if (mq == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002086 return MEMIF_ERR_NOMEM;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002087
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002088 for (x = 0; x < conn->run_args.num_m2s_rings; x++)
2089 {
2090 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
2091 return memif_syscall_error_handler (errno);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002092 e.key = mq[x].int_fd;
2093 e.data_struct = conn;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02002094 add_list_elt (lm, &e, &lm->interrupt_list, &lm->interrupt_list_len);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002095
2096 mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
2097 DBG ("RING: %p I: %d", mq[x].ring, x);
2098 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
2099 mq[x].region = 0;
2100 mq[x].offset =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002101 (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
2102 mq[x].last_head = mq[x].last_tail = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002103 mq[x].alloc_bufs = 0;
2104 }
2105 conn->rx_queues = mq;
Jakub Grajciar718a19b2019-11-05 10:58:00 +01002106 conn->rx_queues_num = conn->run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002107
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002108 return MEMIF_ERR_SUCCESS;
2109}
2110
2111int
2112memif_init_regions_and_queues (memif_connection_t * conn)
2113{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002114 memif_region_t *r;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02002115 libmemif_main_t *lm;
2116
2117 if (conn == NULL)
2118 return MEMIF_ERR_INVAL_ARG;
2119
2120 lm = get_libmemif_main (conn->args.socket);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002121
2122 /* region 0. rings */
2123 memif_add_region (lm, conn, /* has_buffers */ 0);
2124
2125 /* region 1. buffers */
2126 if (lm->add_external_region)
2127 {
2128 r =
2129 (memif_region_t *) lm->realloc (conn->regions,
2130 sizeof (memif_region_t) *
2131 ++conn->regions_num);
2132 if (r == NULL)
2133 return MEMIF_ERR_NOMEM;
2134 conn->regions = r;
2135
2136 conn->regions[1].region_size =
2137 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
2138 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
2139 conn->regions[1].buffer_offset = 0;
2140 lm->add_external_region (&conn->regions[1].addr,
2141 conn->regions[1].region_size,
2142 &conn->regions[1].fd, conn->private_ctx);
2143 conn->regions[1].is_external = 1;
2144 }
2145 else
2146 {
2147 memif_add_region (lm, conn, 1);
2148 }
2149
2150 memif_init_queues (lm, conn);
2151
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002152 return 0;
2153}
2154
2155int
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002156memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
2157 memif_buffer_t * bufs, uint16_t count,
2158 uint16_t * count_out)
2159{
2160 memif_connection_t *c = (memif_connection_t *) conn;
2161 if (EXPECT_FALSE (c == NULL))
2162 return MEMIF_ERR_NOCONN;
2163 if (EXPECT_FALSE (c->fd < 0))
2164 return MEMIF_ERR_DISCONNECTED;
2165 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002166 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2167 run_args.num_s2m_rings;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002168 if (EXPECT_FALSE (qid >= num))
2169 return MEMIF_ERR_QID;
2170 if (EXPECT_FALSE (!count_out))
2171 return MEMIF_ERR_INVAL_ARG;
2172 if (EXPECT_FALSE (c->args.is_master))
2173 return MEMIF_ERR_INVAL_ARG;
2174
2175 memif_queue_t *mq = &c->tx_queues[qid];
2176 memif_ring_t *ring = mq->ring;
2177 memif_buffer_t *b0;
2178 uint16_t mask = (1 << mq->log2_ring_size) - 1;
2179 uint16_t ring_size;
2180 uint16_t slot, ns;
msardara8f554b72018-12-11 18:36:55 +01002181 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002182 *count_out = 0;
2183
2184 ring_size = (1 << mq->log2_ring_size);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002185 slot = (c->args.is_master) ? ring->tail : ring->head;
2186 slot += mq->alloc_bufs;
2187
2188 /* can only be called by slave */
Milan Lencof8b43e52018-06-26 15:16:15 +02002189 ns = ring_size - (ring->head + mq->alloc_bufs) + ring->tail;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002190
2191 b0 = bufs;
2192
2193 while (count && ns)
2194 {
2195 if (EXPECT_FALSE ((b0->flags & MEMIF_BUFFER_FLAG_RX) == 0))
2196 {
2197 /* not a valid buffer */
2198 count--;
2199 continue;
2200 }
2201 b0->flags &= ~MEMIF_BUFFER_FLAG_RX;
2202
2203 ((memif_ring_t *) b0->ring)->desc[b0->desc_index & mask].offset = ring->desc[slot & mask].offset; /* put free buffer on rx ring */
2204
2205 ring->desc[slot & mask].offset =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002206 (uint32_t) (b0->data -
2207 c->regions[ring->desc[slot & mask].region].addr);
Jakub Grajciarcbd179c2019-07-24 14:49:04 +02002208 ring->desc[slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002209 ring->desc[slot & mask].flags |=
2210 (b0->flags & MEMIF_BUFFER_FLAG_NEXT) ? MEMIF_DESC_FLAG_NEXT : 0;
2211
2212 b0->desc_index = slot;
2213
2214 mq->alloc_bufs++;
2215 slot++;
2216
2217 count--;
2218 ns--;
2219 b0++;
2220 *count_out += 1;
2221 }
2222
2223 DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
2224 mq->alloc_bufs);
2225
2226 if (count)
2227 {
2228 DBG ("ring buffer full! qid: %u", qid);
2229 err = MEMIF_ERR_NOBUF_RING;
2230 }
2231
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002232 return err;
2233}
2234
2235int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002236memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
2237 memif_buffer_t * bufs, uint16_t count,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02002238 uint16_t * count_out, uint16_t size)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002239{
2240 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002241 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002242 return MEMIF_ERR_NOCONN;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002243 if (EXPECT_FALSE (c->fd < 0))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002244 return MEMIF_ERR_DISCONNECTED;
2245 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002246 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2247 run_args.num_s2m_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002248 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002249 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002250 if (EXPECT_FALSE (!count_out))
2251 return MEMIF_ERR_INVAL_ARG;
2252
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02002253 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002254 memif_queue_t *mq = &c->tx_queues[qid];
2255 memif_ring_t *ring = mq->ring;
msardara8f554b72018-12-11 18:36:55 +01002256 memif_buffer_t *b0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002257 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002258 uint32_t offset_mask = c->run_args.buffer_size - 1;
Chun Li5dca5462018-02-07 09:51:35 +08002259 uint16_t ring_size;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002260 uint16_t slot, ns;
msardara8f554b72018-12-11 18:36:55 +01002261 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002262 uint16_t dst_left, src_left;
2263 uint16_t saved_count;
2264 memif_buffer_t *saved_b;
2265 *count_out = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002266
Chun Li5dca5462018-02-07 09:51:35 +08002267 ring_size = (1 << mq->log2_ring_size);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002268 slot = (c->args.is_master) ? ring->tail : ring->head;
2269 slot += mq->alloc_bufs;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002270
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002271 if (c->args.is_master)
Milan Lencof8b43e52018-06-26 15:16:15 +02002272 ns = ring->head - (ring->tail + mq->alloc_bufs);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002273 else
Milan Lencof8b43e52018-06-26 15:16:15 +02002274 ns = ring_size - (ring->head + mq->alloc_bufs) + ring->tail;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01002275
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002276 while (count && ns)
2277 {
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002278 b0 = (bufs + *count_out);
2279
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002280 saved_b = b0;
2281 saved_count = count;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02002282
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002283 b0->desc_index = slot;
2284 ring->desc[slot & mask].flags = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002285
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002286 /* slave can produce buffer with original length */
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002287 dst_left = (c->args.is_master) ? ring->desc[slot & mask].length :
2288 c->run_args.buffer_size;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002289 src_left = size;
2290
2291 while (src_left)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02002292 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002293 if (EXPECT_FALSE (dst_left == 0))
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002294 {
2295 if (count && ns)
2296 {
2297 slot++;
2298 *count_out += 1;
2299 mq->alloc_bufs++;
2300 ns--;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002301
2302 ring->desc[b0->desc_index & mask].flags |=
2303 MEMIF_DESC_FLAG_NEXT;
2304 b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
2305
2306 b0 = (bufs + *count_out);
2307 b0->desc_index = slot;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002308 dst_left =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002309 (c->args.is_master) ? ring->desc[slot & mask].
2310 length : c->run_args.buffer_size;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002311 ring->desc[slot & mask].flags = 0;
2312 }
2313 else
2314 {
2315 /* rollback allocated chain buffers */
2316 memset (saved_b, 0, sizeof (memif_buffer_t)
2317 * (saved_count - count + 1));
2318 *count_out -= saved_count - count;
2319 mq->alloc_bufs = saved_count - count;
2320 goto no_ns;
2321 }
2322 }
2323 b0->len = memif_min (dst_left, src_left);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002324
2325 /* slave resets buffer offset */
2326 if (c->args.is_master == 0)
2327 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002328 memif_desc_t *d = &ring->desc[slot & mask];
2329 if (lm->get_external_buffer_offset)
2330 d->offset = lm->get_external_buffer_offset (c->private_ctx);
2331 else
2332 d->offset = d->offset - (d->offset & offset_mask);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002333 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002334 b0->data = memif_get_buffer (c, ring, slot & mask);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002335
2336 src_left -= b0->len;
2337 dst_left -= b0->len;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02002338 }
2339
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002340 slot++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002341 *count_out += 1;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002342 mq->alloc_bufs++;
2343 ns--;
2344 count--;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002345 }
2346
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002347no_ns:
2348
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002349 DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
2350 mq->alloc_bufs);
2351
2352 if (count)
2353 {
2354 DBG ("ring buffer full! qid: %u", qid);
2355 err = MEMIF_ERR_NOBUF_RING;
2356 }
2357
2358 return err;
2359}
2360
2361int
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002362memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
2363 uint16_t headroom)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002364{
2365 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002366 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002367 return MEMIF_ERR_NOCONN;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002368 if (EXPECT_FALSE (c->fd < 0))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002369 return MEMIF_ERR_DISCONNECTED;
2370 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002371 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2372 run_args.num_m2s_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002373 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002374 return MEMIF_ERR_QID;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02002375 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002376 memif_queue_t *mq = &c->rx_queues[qid];
2377 memif_ring_t *ring = mq->ring;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002378 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002379 uint32_t offset_mask = c->run_args.buffer_size - 1;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002380 uint16_t slot;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002381
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002382 if (c->args.is_master)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002383 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002384 MEMIF_MEMORY_BARRIER ();
2385 ring->tail =
2386 (ring->tail + count <=
2387 mq->last_head) ? ring->tail + count : mq->last_head;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002388 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002389 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002390
2391 uint16_t head = ring->head;
2392 uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail;
2393 head += (count < ns) ? count : ns;
2394
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002395 slot = ring->head;
2396 memif_desc_t *d;
2397 while (slot < head)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002398 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002399 d = &ring->desc[slot & mask];
2400 d->region = 1;
2401 d->length = c->run_args.buffer_size - headroom;
2402 if (lm->get_external_buffer_offset)
2403 d->offset = lm->get_external_buffer_offset (c->private_ctx);
2404 else
2405 d->offset = d->offset - (d->offset & offset_mask) + headroom;
2406 slot++;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002407 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002408
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002409 MEMIF_MEMORY_BARRIER ();
2410 ring->head = head;
2411
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002412 return MEMIF_ERR_SUCCESS; /* 0 */
2413}
2414
2415int
2416memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
2417 memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
2418{
2419 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002420 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002421 return MEMIF_ERR_NOCONN;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002422 if (EXPECT_FALSE (c->fd < 0))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002423 return MEMIF_ERR_DISCONNECTED;
2424 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002425 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2426 run_args.num_s2m_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002427 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002428 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002429 if (EXPECT_FALSE (!tx))
2430 return MEMIF_ERR_INVAL_ARG;
2431
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002432 memif_queue_t *mq = &c->tx_queues[qid];
2433 memif_ring_t *ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002434 uint16_t mask = (1 << mq->log2_ring_size) - 1;
2435 memif_buffer_t *b0;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002436 *tx = 0;
2437
2438 if (count > mq->alloc_bufs)
2439 count = mq->alloc_bufs;
2440
2441 if (EXPECT_FALSE (count == 0))
2442 return MEMIF_ERR_SUCCESS;
2443
2444 while (count)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002445 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002446 b0 = (bufs + *tx);
2447 ring->desc[b0->desc_index & mask].length = b0->len;
2448
2449#ifdef MEMIF_DBG_SHM
2450 printf ("offset: %-6d\n", ring->desc[b0->desc_index & mask].offset);
2451 printf ("data: %p\n",
2452 memif_get_buffer (c, ring, b0->desc_index & mask));
2453 printf ("index: %u\n", b0->desc_index);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002454 print_bytes (memif_get_buffer (c, ring, b0->desc_index & mask),
2455 ring->desc[b0->desc_index & mask].length, DBG_TX_BUF);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002456#endif /* MEMIF_DBG_SHM */
2457
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002458 *tx += 1;
2459 count--;
2460 }
2461
2462
Milan Lenco0a47c992017-10-12 14:19:31 +02002463 MEMIF_MEMORY_BARRIER ();
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002464 if (c->args.is_master)
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002465 ring->tail = b0->desc_index + 1;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002466 else
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002467 ring->head = b0->desc_index + 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002468
2469 mq->alloc_bufs -= *tx;
2470
2471 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
2472 {
2473 uint64_t a = 1;
2474 int r = write (mq->int_fd, &a, sizeof (a));
2475 if (r < 0)
2476 return MEMIF_ERR_INT_WRITE;
2477 }
2478
2479 return MEMIF_ERR_SUCCESS; /* 0 */
2480}
2481
2482int
2483memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
2484 memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
2485{
2486 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002487 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002488 return MEMIF_ERR_NOCONN;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002489 if (EXPECT_FALSE (c->fd < 0))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002490 return MEMIF_ERR_DISCONNECTED;
2491 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002492 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2493 run_args.num_m2s_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002494 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002495 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002496 if (EXPECT_FALSE (!rx))
2497 return MEMIF_ERR_INVAL_ARG;
2498
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002499 memif_queue_t *mq = &c->rx_queues[qid];
2500 memif_ring_t *ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002501 uint16_t cur_slot, last_slot;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002502 uint16_t ns;
2503 uint16_t mask = (1 << mq->log2_ring_size) - 1;
msardara8f554b72018-12-11 18:36:55 +01002504 memif_buffer_t *b0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002505 *rx = 0;
2506
2507 uint64_t b;
2508 ssize_t r = read (mq->int_fd, &b, sizeof (b));
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002509 if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002510 return memif_syscall_error_handler (errno);
2511
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002512 cur_slot = (c->args.is_master) ? mq->last_head : mq->last_tail;
2513 last_slot = (c->args.is_master) ? ring->head : ring->tail;
2514 if (cur_slot == last_slot)
2515 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002516
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002517 ns = last_slot - cur_slot;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002518
2519 while (ns && count)
2520 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002521 b0 = (bufs + *rx);
2522
2523 b0->desc_index = cur_slot;
2524 b0->data = memif_get_buffer (c, ring, cur_slot & mask);
2525 b0->len = ring->desc[cur_slot & mask].length;
2526 /* slave resets buffer length */
2527 if (c->args.is_master == 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002528 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002529 ring->desc[cur_slot & mask].length = c->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002530 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002531
2532 b0->flags = MEMIF_BUFFER_FLAG_RX;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002533 if (ring->desc[cur_slot & mask].flags & MEMIF_DESC_FLAG_NEXT)
2534 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002535 b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002536 ring->desc[cur_slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
2537 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002538/* b0->offset = ring->desc[cur_slot & mask].offset;*/
2539 b0->ring = ring;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02002540#ifdef MEMIF_DBG_SHM
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002541 printf ("data: %p\n", b0->data);
2542 printf ("index: %u\n", b0->desc_index);
2543 printf ("ring: %p\n", b0->ring);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002544 print_bytes (b0->data, b0->len, DBG_RX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02002545#endif /* MEMIF_DBG_SHM */
2546 ns--;
2547 *rx += 1;
2548
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002549 count--;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002550 cur_slot++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002551 }
2552
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002553 if (c->args.is_master)
2554 mq->last_head = cur_slot;
2555 else
2556 mq->last_tail = cur_slot;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02002557
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002558 if (ns)
2559 {
2560 DBG ("not enough buffers!");
2561 return MEMIF_ERR_NOBUF;
2562 }
2563
2564 return MEMIF_ERR_SUCCESS; /* 0 */
2565}
2566
2567int
2568memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
2569 char *buf, ssize_t buflen)
2570{
2571 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02002572 libmemif_main_t *lm;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002573 memif_socket_t *ms;
2574 int err = MEMIF_ERR_SUCCESS, i;
2575 ssize_t l0 = 0, l1;
2576
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002577 if (c == NULL)
2578 return MEMIF_ERR_NOCONN;
2579
Jakub Grajciar12df4972019-07-01 14:24:48 +02002580 ms = (memif_socket_t *) c->args.socket;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02002581 lm = get_libmemif_main (ms);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002582
2583 l1 = strlen ((char *) c->args.interface_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02002584 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002585 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002586 md->if_name =
2587 (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002588 l0 += l1 + 1;
2589 }
2590 else
2591 err = MEMIF_ERR_NOBUF_DET;
2592
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002593 l1 = strlen ((char *) lm->app_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02002594 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002595 {
Jakub Grajciar5e1f69a2018-04-12 13:56:29 +02002596 md->inst_name = (uint8_t *) strcpy (buf + l0, (char *) lm->app_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002597 l0 += l1 + 1;
2598 }
2599 else
2600 err = MEMIF_ERR_NOBUF_DET;
2601
2602 l1 = strlen ((char *) c->remote_if_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02002603 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002604 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002605 md->remote_if_name =
2606 (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002607 l0 += l1 + 1;
2608 }
2609 else
2610 err = MEMIF_ERR_NOBUF_DET;
2611
2612 l1 = strlen ((char *) c->remote_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02002613 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002614 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002615 md->remote_inst_name =
2616 (uint8_t *) strcpy (buf + l0, (char *) c->remote_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002617 l0 += l1 + 1;
2618 }
2619 else
2620 err = MEMIF_ERR_NOBUF_DET;
2621
2622 md->id = c->args.interface_id;
2623
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002624 if (strlen ((char *) c->args.secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002625 {
2626 l1 = strlen ((char *) c->args.secret);
Milan Lenco0a47c992017-10-12 14:19:31 +02002627 if (l0 + l1 < buflen)
2628 {
Jakub Grajciar5e1f69a2018-04-12 13:56:29 +02002629 md->secret = (uint8_t *) strcpy (buf + l0, (char *) c->args.secret);
Milan Lenco0a47c992017-10-12 14:19:31 +02002630 l0 += l1 + 1;
2631 }
2632 else
2633 err = MEMIF_ERR_NOBUF_DET;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002634 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002635
2636 md->role = (c->args.is_master) ? 0 : 1;
2637 md->mode = c->args.mode;
2638
Jakub Grajciar12df4972019-07-01 14:24:48 +02002639 l1 = strlen ((char *) ms->filename);
Milan Lenco0a47c992017-10-12 14:19:31 +02002640 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002641 {
2642 md->socket_filename =
Jakub Grajciar12df4972019-07-01 14:24:48 +02002643 (uint8_t *) strcpy (buf + l0, (char *) ms->filename);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002644 l0 += l1 + 1;
2645 }
2646 else
2647 err = MEMIF_ERR_NOBUF_DET;
2648
Jakub Grajciar568cc462018-09-05 12:11:35 +02002649 l1 = strlen ((char *) c->remote_disconnect_string);
2650 if (l0 + l1 < buflen)
2651 {
2652 md->error =
2653 (uint8_t *) strcpy (buf + l0, (char *) c->remote_disconnect_string);
2654 l0 += l1 + 1;
2655 }
2656 else
2657 err = MEMIF_ERR_NOBUF_DET;
2658
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002659 md->regions_num = c->regions_num;
2660 l1 = sizeof (memif_region_details_t) * md->regions_num;
2661 if (l0 + l1 <= buflen)
2662 {
Koichiro Den33331ed2019-01-03 00:22:24 +09002663 md->regions = (memif_region_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09002664 for (i = 0; i < md->regions_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02002665 {
2666 md->regions[i].index = i;
2667 md->regions[i].addr = c->regions[i].addr;
2668 md->regions[i].size = c->regions[i].region_size;
2669 md->regions[i].fd = c->regions[i].fd;
2670 md->regions[i].is_external = c->regions[i].is_external;
2671 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002672 l0 += l1;
2673 }
2674 else
2675 err = MEMIF_ERR_NOBUF_DET;
2676
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002677 md->rx_queues_num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002678 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2679 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002680
2681 l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
2682 if (l0 + l1 <= buflen)
2683 {
Koichiro Den33331ed2019-01-03 00:22:24 +09002684 md->rx_queues = (memif_queue_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09002685 for (i = 0; i < md->rx_queues_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02002686 {
2687 md->rx_queues[i].region = c->rx_queues[i].region;
2688 md->rx_queues[i].qid = i;
2689 md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
2690 md->rx_queues[i].flags = c->rx_queues[i].ring->flags;
2691 md->rx_queues[i].head = c->rx_queues[i].ring->head;
2692 md->rx_queues[i].tail = c->rx_queues[i].ring->tail;
2693 md->rx_queues[i].buffer_size = c->run_args.buffer_size;
2694 }
Milan Lenco0a47c992017-10-12 14:19:31 +02002695 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002696 }
2697 else
2698 err = MEMIF_ERR_NOBUF_DET;
2699
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002700 md->tx_queues_num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002701 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2702 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002703
2704 l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
2705 if (l0 + l1 <= buflen)
2706 {
Koichiro Den33331ed2019-01-03 00:22:24 +09002707 md->tx_queues = (memif_queue_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09002708 for (i = 0; i < md->tx_queues_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02002709 {
2710 md->tx_queues[i].region = c->tx_queues[i].region;
2711 md->tx_queues[i].qid = i;
2712 md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
2713 md->tx_queues[i].flags = c->tx_queues[i].ring->flags;
2714 md->tx_queues[i].head = c->tx_queues[i].ring->head;
2715 md->tx_queues[i].tail = c->tx_queues[i].ring->tail;
2716 md->tx_queues[i].buffer_size = c->run_args.buffer_size;
2717 }
Milan Lenco0a47c992017-10-12 14:19:31 +02002718 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002719 }
2720 else
2721 err = MEMIF_ERR_NOBUF_DET;
2722
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002723 md->link_up_down = (c->fd > 0) ? 1 : 0;
2724
2725 return err; /* 0 */
2726}
2727
2728int
2729memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
2730{
2731 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002732 uint8_t num;
2733
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002734 *efd = -1;
2735 if (c == NULL)
2736 return MEMIF_ERR_NOCONN;
2737 if (c->fd < 0)
2738 return MEMIF_ERR_DISCONNECTED;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002739
2740 num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002741 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2742 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002743 if (qid >= num)
2744 return MEMIF_ERR_QID;
2745
2746 *efd = c->rx_queues[qid].int_fd;
2747
2748 return MEMIF_ERR_SUCCESS;
2749}
2750
2751int
2752memif_cleanup ()
2753{
2754 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02002755 int err;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002756
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02002757 err = memif_delete_socket ((memif_socket_handle_t *) & lm->default_socket);
2758 if (err != MEMIF_ERR_SUCCESS)
2759 return err;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002760
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002761 if (lm->control_list)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002762 lm->free (lm->control_list);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002763 lm->control_list = NULL;
2764 if (lm->interrupt_list)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002765 lm->free (lm->interrupt_list);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002766 lm->interrupt_list = NULL;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002767 if (lm->socket_list)
2768 lm->free (lm->socket_list);
2769 lm->socket_list = NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002770 if (lm->pending_list)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002771 lm->free (lm->pending_list);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002772 lm->pending_list = NULL;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02002773 if (lm->poll_cancel_fd != -1)
2774 close (lm->poll_cancel_fd);
2775
2776 return MEMIF_ERR_SUCCESS; /* 0 */
2777}
2778
2779int
2780memif_per_thread_cleanup (memif_per_thread_main_handle_t * pt_main)
2781{
2782 libmemif_main_t *lm = (libmemif_main_t *) * pt_main;
2783
2784 if (lm == NULL)
2785 return MEMIF_ERR_INVAL_ARG;
2786
2787 /* No default socket in case of per thread */
2788
2789 if (lm->control_list)
2790 lm->free (lm->control_list);
2791 lm->control_list = NULL;
2792 if (lm->interrupt_list)
2793 lm->free (lm->interrupt_list);
2794 lm->interrupt_list = NULL;
2795 if (lm->socket_list)
2796 lm->free (lm->socket_list);
2797 lm->socket_list = NULL;
2798 if (lm->pending_list)
2799 lm->free (lm->pending_list);
2800 lm->pending_list = NULL;
2801 if (lm->poll_cancel_fd != -1)
2802 close (lm->poll_cancel_fd);
2803
2804 lm->free (lm);
2805
2806 *pt_main = NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002807
2808 return MEMIF_ERR_SUCCESS; /* 0 */
2809}