blob: 8f18d892139994227ad4042fe62fa7f80d31157c [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
18#define _GNU_SOURCE
19#include <sys/socket.h>
20#include <sys/types.h>
21#include <sys/un.h>
22#include <string.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <net/if.h>
26#include <sys/ioctl.h>
27#include <sys/uio.h>
28#include <sys/mman.h>
29#include <sys/prctl.h>
30#include <fcntl.h>
31#include <errno.h>
32
33#include <socket.h>
34#include <memif.h>
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020035#include <memif_private.h>
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020036
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020037/* sends msg to socket */
38static_fn int
39memif_msg_send (int fd, memif_msg_t * msg, int afd)
40{
41 struct msghdr mh = { 0 };
42 struct iovec iov[1];
43 char ctl[CMSG_SPACE (sizeof (int))];
44 int rv, err = MEMIF_ERR_SUCCESS; /* 0 */
45
46 iov[0].iov_base = (void *) msg;
47 iov[0].iov_len = sizeof (memif_msg_t);
48 mh.msg_iov = iov;
49 mh.msg_iovlen = 1;
50
51 if (afd > 0)
52 {
53 struct cmsghdr *cmsg;
54 memset (&ctl, 0, sizeof (ctl));
55 mh.msg_control = ctl;
56 mh.msg_controllen = sizeof (ctl);
57 cmsg = CMSG_FIRSTHDR (&mh);
58 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
59 cmsg->cmsg_level = SOL_SOCKET;
60 cmsg->cmsg_type = SCM_RIGHTS;
61 memcpy (CMSG_DATA (cmsg), &afd, sizeof (int));
62 }
63 rv = sendmsg (fd, &mh, 0);
64 if (rv < 0)
65 err = memif_syscall_error_handler (errno);
66 DBG ("Message type %u sent", msg->type);
67 return err;
68}
69
70/* response from memif master - master is ready to handle next message */
71static_fn int
72memif_msg_enq_ack (memif_connection_t * c)
73{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020074 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020075 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020076 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020077 if (e == NULL)
78 return memif_syscall_error_handler (errno);
79
80 memset (&e->msg, 0, sizeof (e->msg));
81 e->msg.type = MEMIF_MSG_TYPE_ACK;
82 e->fd = -1;
83
84 e->next = NULL;
85 if (c->msg_queue == NULL)
86 {
87 c->msg_queue = e;
88 return MEMIF_ERR_SUCCESS; /* 0 */
89 }
90
91 memif_msg_queue_elt_t *cur = c->msg_queue;
92 while (cur->next != NULL)
93 {
94 cur = cur->next;
95 }
96 cur->next = e;
97
98 return MEMIF_ERR_SUCCESS; /* 0 */
99}
100
101static_fn int
102memif_msg_send_hello (int fd)
103{
104 libmemif_main_t *lm = &libmemif_main;
105 memif_msg_t msg = { 0 };
106 memif_msg_hello_t *h = &msg.hello;
107 msg.type = MEMIF_MSG_TYPE_HELLO;
108 h->min_version = MEMIF_VERSION;
109 h->max_version = MEMIF_VERSION;
110 h->max_s2m_ring = MEMIF_MAX_M2S_RING;
111 h->max_m2s_ring = MEMIF_MAX_M2S_RING;
112 h->max_region = MEMIF_MAX_REGION;
113 h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
114
115 strncpy ((char *) h->name, lm->app_name, strlen (lm->app_name));
116
117 /* msg hello is not enqueued but sent directly,
118 because it is the first msg to be sent */
119 return memif_msg_send (fd, &msg, -1);
120}
121
122/* send id and secret (optional) for interface identification */
123static_fn int
124memif_msg_enq_init (memif_connection_t * c)
125{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200126 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200127 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200128 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200129 if (e == NULL)
130 return memif_syscall_error_handler (errno);
131 memset (e, 0, sizeof (memif_msg_queue_elt_t));
132
133 memset (&e->msg, 0, sizeof (e->msg));
134 memif_msg_init_t *i = &e->msg.init;
135
136 e->msg.type = MEMIF_MSG_TYPE_INIT;
137 e->fd = -1;
138 i->version = MEMIF_VERSION;
139 i->id = c->args.interface_id;
140 i->mode = c->args.mode;
141
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200142 strncpy ((char *) i->name, (char *) lm->app_name,
143 strlen ((char *) lm->app_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200144 if (c->args.secret)
145 strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
146
147 e->next = NULL;
148 if (c->msg_queue == NULL)
149 {
150 c->msg_queue = e;
151 return MEMIF_ERR_SUCCESS; /* 0 */
152 }
153
154 memif_msg_queue_elt_t *cur = c->msg_queue;
155 while (cur->next != NULL)
156 {
157 cur = cur->next;
158 }
159 cur->next = e;
160
161 return MEMIF_ERR_SUCCESS; /* 0 */
162}
163
164/* send information about region specified by region_index */
165static_fn int
166memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
167{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200168 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200169 /* maybe check if region is valid? */
170 memif_region_t *mr = &c->regions[region_index];
171
172 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200173 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200174 if (e == NULL)
175 return memif_syscall_error_handler (errno);
176
177 memset (&e->msg, 0, sizeof (e->msg));
178 memif_msg_add_region_t *ar = &e->msg.add_region;
179
180 e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
181 e->fd = mr->fd;
182 ar->index = region_index;
183 ar->size = mr->region_size;
184
185 e->next = NULL;
186 if (c->msg_queue == NULL)
187 {
188 c->msg_queue = e;
189 return MEMIF_ERR_SUCCESS; /* 0 */
190 }
191
192 memif_msg_queue_elt_t *cur = c->msg_queue;
193 while (cur->next != NULL)
194 {
195 cur = cur->next;
196 }
197 cur->next = e;
198
199 return MEMIF_ERR_SUCCESS; /* 0 */
200}
201
202/* send information about ring specified by direction (S2M | M2S) and index */
203static_fn int
204memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
205{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200206 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200207 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200208 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200209 if (e == NULL)
210 return memif_syscall_error_handler (errno);
211
212 memset (&e->msg, 0, sizeof (e->msg));
213 memif_msg_add_ring_t *ar = &e->msg.add_ring;
214
215 e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
216
217 /* TODO: support multiple rings */
218 memif_queue_t *mq;
219 if (dir == MEMIF_RING_M2S)
220 mq = &c->rx_queues[index];
221 else
222 mq = &c->tx_queues[index];
223
224 e->fd = mq->int_fd;
225 ar->index = index;
226 ar->offset = mq->offset;
227 ar->region = mq->region;
228 ar->log2_ring_size = mq->log2_ring_size;
229 ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
230
231 e->next = NULL;
232 if (c->msg_queue == NULL)
233 {
234 c->msg_queue = e;
235 return MEMIF_ERR_SUCCESS; /* 0 */
236 }
237
238 memif_msg_queue_elt_t *cur = c->msg_queue;
239 while (cur->next != NULL)
240 {
241 cur = cur->next;
242 }
243 cur->next = e;
244
245 return MEMIF_ERR_SUCCESS; /* 0 */
246}
247
248/* used as connection request from slave */
249static_fn int
250memif_msg_enq_connect (memif_connection_t * c)
251{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200252 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200253 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200254 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200255 if (e == NULL)
256 return memif_syscall_error_handler (errno);
257
258 memset (&e->msg, 0, sizeof (e->msg));
259 memif_msg_connect_t *cm = &e->msg.connect;
260
261 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
262 e->fd = -1;
263 strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
264 strlen ((char *) c->args.interface_name));
265
266 e->next = NULL;
267 if (c->msg_queue == NULL)
268 {
269 c->msg_queue = e;
270 return MEMIF_ERR_SUCCESS; /* 0 */
271 }
272
273 memif_msg_queue_elt_t *cur = c->msg_queue;
274 while (cur->next != NULL)
275 {
276 cur = cur->next;
277 }
278 cur->next = e;
279
280 return MEMIF_ERR_SUCCESS; /* 0 */
281}
282
283/* used as confirmation of connection by master */
284static_fn int
285memif_msg_enq_connected (memif_connection_t * c)
286{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200287 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200288 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200289 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200290 if (e == NULL)
291 return memif_syscall_error_handler (errno);
292
293 memset (&e->msg, 0, sizeof (e->msg));
294 memif_msg_connected_t *cm = &e->msg.connected;
295
296 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
297 e->fd = -1;
298 strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
299 strlen ((char *) c->args.interface_name));
300
301 e->next = NULL;
302 if (c->msg_queue == NULL)
303 {
304 c->msg_queue = e;
305 return MEMIF_ERR_SUCCESS; /* 0 */
306 }
307
308 memif_msg_queue_elt_t *cur = c->msg_queue;
309 while (cur->next != NULL)
310 {
311 cur = cur->next;
312 }
313 cur->next = e;
314
315 return MEMIF_ERR_SUCCESS; /* 0 */
316}
317
318/* immediately send disconnect msg */
319 /* specifie protocol for disconnect msg err_code
320 so that it will be compatible with VPP? (header/doc) */
321int
322memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
323{
324 memif_msg_t msg = { 0 };
325 memif_msg_disconnect_t *d = &msg.disconnect;
326
327 msg.type = MEMIF_MSG_TYPE_DISCONNECT;
328 d->code = err_code;
329 uint16_t l = strlen ((char *) err_string);
330 if (l > 96)
331 {
332 DBG ("Disconnect string too long. Sending first 96 characters.");
333 l = 96;
334 }
335 strncpy ((char *) d->string, (char *) err_string, l);
336
337 return memif_msg_send (fd, &msg, -1);
338}
339
340static_fn int
341memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
342{
343 memif_msg_hello_t *h = &msg->hello;
344
345 if (msg->hello.min_version > MEMIF_VERSION ||
346 msg->hello.max_version < MEMIF_VERSION)
347 {
348 DBG ("incompatible protocol version");
349 return MEMIF_ERR_PROTO;
350 }
351
352 c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
353 c->args.num_s2m_rings);
354 c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
355 c->args.num_m2s_rings);
356 c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
357 c->args.log2_ring_size);
358 c->run_args.buffer_size = c->args.buffer_size;
359 strncpy ((char *) c->remote_name, (char *) h->name,
360 strlen ((char *) h->name));
361
362 return MEMIF_ERR_SUCCESS; /* 0 */
363}
364
365/* handle interface identification (id, secret (optional)) */
366static_fn int
367memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
368{
369 memif_msg_init_t *i = &msg->init;
370 memif_list_elt_t *elt = NULL;
371 memif_list_elt_t elt2;
372 memif_connection_t *c = NULL;
373 libmemif_main_t *lm = &libmemif_main;
374 uint8_t err_string[96];
375 memset (err_string, 0, sizeof (char) * 96);
376 int err = MEMIF_ERR_SUCCESS; /* 0 */
377 int err_disc;
378 if (i->version != MEMIF_VERSION)
379 {
380 DBG ("MEMIF_VER_ERR");
381 strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
382 err = MEMIF_ERR_PROTO;
383 goto error;
384 }
385
386 get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
387 if (elt == NULL)
388 {
389 DBG ("MEMIF_ID_ERR");
390 strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
391 err = MEMIF_ERR_ID;
392 goto error;
393 }
394
395 c = (memif_connection_t *) elt->data_struct;
396
397 if (!(c->args.is_master))
398 {
399 DBG ("MEMIF_SLAVE_ERR");
400 strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
401 strlen (MEMIF_SLAVE_ERR));
402 err = MEMIF_ERR_ACCSLAVE;
403 goto error;
404 }
405 if (c->fd != -1)
406 {
407 DBG ("MEMIF_CONN_ERR");
408 strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
409 err = MEMIF_ERR_ALRCONN;
410 goto error;
411 }
412
413 c->fd = fd;
414
415 if (i->mode != c->args.mode)
416 {
417 DBG ("MEMIF_MODE_ERR");
418 strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
419 err = MEMIF_ERR_MODE;
420 goto error;
421 }
422
423 strncpy ((char *) c->remote_name, (char *) i->name,
424 strlen ((char *) i->name));
425
426 if (c->args.secret)
427 {
428 int r;
429 if (i->secret)
430 {
431 if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
432 {
433 DBG ("MEMIF_SECRET_ERR");
434 strncpy ((char *) err_string,
435 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
436 err = MEMIF_ERR_SECRET;
437 goto error;
438 }
439 r = strncmp ((char *) i->secret, (char *) c->args.secret,
440 strlen ((char *) c->args.secret));
441 if (r != 0)
442 {
443 DBG ("MEMIF_SECRET_ERR");
444 strncpy ((char *) err_string,
445 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
446 err = MEMIF_ERR_SECRET;
447 goto error;
448 }
449 }
450 else
451 {
452 DBG ("MEMIF_NOSECRET_ERR");
453 strncpy ((char *) err_string,
454 MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR));
455 err = MEMIF_ERR_NOSECRET;
456 goto error;
457 }
458 }
459
460 c->read_fn = memif_conn_fd_read_ready;
461 c->write_fn = memif_conn_fd_write_ready;
462 c->error_fn = memif_conn_fd_error;
463
464 elt2.key = c->fd;
465 elt2.data_struct = c;
466
467 add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
468 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
469
470 return err;
471
472error:
473 memif_msg_send_disconnect (fd, err_string, 0);
474 lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL);
475 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
476 close (fd);
477 fd = -1;
478 return err;
479}
480
481/* receive region information and add new region to connection (if possible) */
482static_fn int
483memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
484 int fd)
485{
486 memif_msg_add_region_t *ar = &msg->add_region;
487 memif_region_t *mr;
488 if (fd < 0)
489 return MEMIF_ERR_NO_SHMFD;
490
491 if (ar->index > MEMIF_MAX_REGION)
492 return MEMIF_ERR_MAXREG;
493
494 mr =
495 (memif_region_t *) realloc (c->regions,
496 sizeof (memif_region_t) * (ar->index + 1));
497 if (mr == NULL)
498 return memif_syscall_error_handler (errno);
499 c->regions = mr;
500 c->regions[ar->index].fd = fd;
501 c->regions[ar->index].region_size = ar->size;
502 c->regions[ar->index].shm = NULL;
503
504 return MEMIF_ERR_SUCCESS; /* 0 */
505}
506
507/* receive ring information and add new ring to connection queue
508 (based on direction S2M | M2S) */
509static_fn int
510memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
511{
512 memif_msg_add_ring_t *ar = &msg->add_ring;
513
514 memif_queue_t *mq;
515
516 if (fd < 0)
517 return MEMIF_ERR_NO_INTFD;
518
519 if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
520 {
521 if (ar->index > MEMIF_MAX_S2M_RING)
522 return MEMIF_ERR_MAXRING;
523 if (ar->index >= c->args.num_s2m_rings)
524 return MEMIF_ERR_MAXRING;
525
526 mq =
527 (memif_queue_t *) realloc (c->rx_queues,
528 sizeof (memif_queue_t) * (ar->index + 1));
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200529 memset (mq, 0, sizeof (memif_queue_t) * (ar->index + 1));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200530 if (mq == NULL)
531 return memif_syscall_error_handler (errno);
532 c->rx_queues = mq;
533 c->rx_queues[ar->index].int_fd = fd;
534 c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
535 c->rx_queues[ar->index].region = ar->region;
536 c->rx_queues[ar->index].offset = ar->offset;
537 c->run_args.num_s2m_rings++;
538 }
539 else
540 {
541 if (ar->index > MEMIF_MAX_M2S_RING)
542 return MEMIF_ERR_MAXRING;
543 if (ar->index >= c->args.num_m2s_rings)
544 return MEMIF_ERR_MAXRING;
545
546 mq =
547 (memif_queue_t *) realloc (c->tx_queues,
548 sizeof (memif_queue_t) * (ar->index + 1));
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200549 memset (mq, 0, sizeof (memif_queue_t) * (ar->index + 1));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200550 if (mq == NULL)
551 return memif_syscall_error_handler (errno);
552 c->tx_queues = mq;
553 c->tx_queues[ar->index].int_fd = fd;
554 c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
555 c->tx_queues[ar->index].region = ar->region;
556 c->tx_queues[ar->index].offset = ar->offset;
557 c->run_args.num_m2s_rings++;
558 }
559
560 return MEMIF_ERR_SUCCESS; /* 0 */
561}
562
563/* slave -> master */
564static_fn int
565memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
566{
567 memif_msg_connect_t *cm = &msg->connect;
568 libmemif_main_t *lm = &libmemif_main;
569 memif_list_elt_t elt;
570
571 int err;
572 err = memif_connect1 (c);
573 if (err != MEMIF_ERR_SUCCESS)
574 return err;
575
576 strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
577 strlen ((char *) cm->if_name));
578
579 int i;
580 if (c->on_interrupt != NULL)
581 {
582 for (i = 0; i < c->run_args.num_m2s_rings; i++)
583 {
584 elt.key = c->rx_queues[i].int_fd;
585 elt.data_struct = c;
586 add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
587
588 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200589
590 /* refill ring buffers */
591 memif_refill_queue ((void *) c, i, -1);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200592 }
593
594 }
595
596 c->on_connect ((void *) c, c->private_ctx);
597
598 return err;
599}
600
601/* master -> slave */
602static_fn int
603memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
604{
605 memif_msg_connect_t *cm = &msg->connect;
606 libmemif_main_t *lm = &libmemif_main;
607
608 int err;
609 err = memif_connect1 (c);
610 if (err != MEMIF_ERR_SUCCESS)
611 return err;
612
613 strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
614 strlen ((char *) cm->if_name));
615
616 int i;
617 if (c->on_interrupt != NULL)
618 {
619 for (i = 0; i < c->run_args.num_s2m_rings; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200620 {
621 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
622
623 /* refill ring buffers */
624 memif_refill_queue ((void *) c, i, -1);
625 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200626 }
627
628 c->on_connect ((void *) c, c->private_ctx);
629
630 return err;
631}
632
633static_fn int
634memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
635{
636 memif_msg_disconnect_t *d = &msg->disconnect;
637
638 memset (c->remote_disconnect_string, 0,
639 sizeof (c->remote_disconnect_string));
640 strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
641 strlen ((char *) d->string));
642
643 /* on returning error, handle function will call memif_disconnect () */
644 DBG ("disconnect received: %s, mode: %d",
645 c->remote_disconnect_string, c->args.mode);
646 return MEMIF_ERR_DISCONNECT;
647}
648
649static_fn int
650memif_msg_receive (int ifd)
651{
652 char ctl[CMSG_SPACE (sizeof (int)) +
653 CMSG_SPACE (sizeof (struct ucred))] = { 0 };
654 struct msghdr mh = { 0 };
655 struct iovec iov[1];
656 memif_msg_t msg = { 0 };
657 ssize_t size;
658 int err = MEMIF_ERR_SUCCESS; /* 0 */
659 int fd = -1;
660 int i;
661 libmemif_main_t *lm = &libmemif_main;
662 memif_connection_t *c = NULL;
663 memif_socket_t *ms = NULL;
664 memif_list_elt_t *elt = NULL;
665
666 iov[0].iov_base = (void *) &msg;
667 iov[0].iov_len = sizeof (memif_msg_t);
668 mh.msg_iov = iov;
669 mh.msg_iovlen = 1;
670 mh.msg_control = ctl;
671 mh.msg_controllen = sizeof (ctl);
672
673 DBG ("recvmsg fd %d", ifd);
674 size = recvmsg (ifd, &mh, 0);
675 DBG ("done");
676 if (size != sizeof (memif_msg_t))
677 {
678 if (size == 0)
679 return MEMIF_ERR_DISCONNECTED;
680 else
681 return MEMIF_ERR_MFMSG;
682 }
683
684 struct ucred *cr = 0;
685 struct cmsghdr *cmsg;
686
687 cmsg = CMSG_FIRSTHDR (&mh);
688 while (cmsg)
689 {
690 if (cmsg->cmsg_level == SOL_SOCKET)
691 {
692 if (cmsg->cmsg_type == SCM_CREDENTIALS)
693 {
694 cr = (struct ucred *) CMSG_DATA (cmsg);
695 }
696 else if (cmsg->cmsg_type == SCM_RIGHTS)
697 {
698 int *fdp = (int *) CMSG_DATA (cmsg);
699 fd = *fdp;
700 }
701 }
702 cmsg = CMSG_NXTHDR (&mh, cmsg);
703 }
704
705 DBG ("Message type %u received", msg.type);
706
707 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
708 if (elt != NULL)
709 c = (memif_connection_t *) elt->data_struct;
710
711 switch (msg.type)
712 {
713 case MEMIF_MSG_TYPE_ACK:
714 break;
715
716 case MEMIF_MSG_TYPE_HELLO:
717 if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
718 return err;
719 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
720 return err;
721 if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
722 return err;
723 if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS)
724 return err;
725 for (i = 0; i < c->run_args.num_s2m_rings; i++)
726 {
727 if ((err =
728 memif_msg_enq_add_ring (c, i,
729 MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS)
730 return err;
731 }
732 for (i = 0; i < c->run_args.num_m2s_rings; i++)
733 {
734 if ((err =
735 memif_msg_enq_add_ring (c, i,
736 MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS)
737 return err;
738 }
739 if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
740 return err;
741 break;
742
743 case MEMIF_MSG_TYPE_INIT:
744 get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
745 if (elt == NULL)
746 return -1;
747 ms = (memif_socket_t *) elt->data_struct;
748 if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
749 return err;
750 /* c->remote_pid = cr->pid */
751 /* c->remote_uid = cr->uid */
752 /* c->remote_gid = cr->gid */
753 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
754 if (elt == NULL)
755 return -1;
756 c = (memif_connection_t *) elt->data_struct;
757 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
758 return err;
759 break;
760
761 case MEMIF_MSG_TYPE_ADD_REGION:
762 if ((err =
763 memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
764 return err;
765 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
766 return err;
767 break;
768
769 case MEMIF_MSG_TYPE_ADD_RING:
770 if ((err =
771 memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
772 return err;
773 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
774 return err;
775 break;
776
777 case MEMIF_MSG_TYPE_CONNECT:
778 if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
779 return err;
780 if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
781 return err;
782 break;
783
784 case MEMIF_MSG_TYPE_CONNECTED:
785 if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
786 return err;
787 break;
788
789 case MEMIF_MSG_TYPE_DISCONNECT:
790 if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
791 return err;
792 break;
793
794 default:
795 return MEMIF_ERR_UNKNOWN_MSG;;
796 break;
797 }
798
799 if (c != NULL)
800 c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
801/* libmemif_main_t *lm = &libmemif_main;
802 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
803 return MEMIF_ERR_SUCCESS; /* 0 */
804}
805
806int
807memif_conn_fd_error (memif_connection_t * c)
808{
809 DBG ("connection fd error");
810 strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
811 int err = memif_disconnect_internal (c);
812 return err;
813}
814
815/* calls memif_msg_receive to handle pending messages on socket */
816int
817memif_conn_fd_read_ready (memif_connection_t * c)
818{
819 int err;
820 err = memif_msg_receive (c->fd);
821 if (err != 0)
822 {
823 err = memif_disconnect_internal (c);
824 }
825 return err;
826}
827
828/* get msg from msg queue buffer and send it to socket */
829int
830memif_conn_fd_write_ready (memif_connection_t * c)
831{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200832 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200833 int err = MEMIF_ERR_SUCCESS; /* 0 */
834
835
836 if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
837 goto done;
838
839 memif_msg_queue_elt_t *e = c->msg_queue;
840 if (e == NULL)
841 goto done;
842
843 c->msg_queue = c->msg_queue->next;
844
845 c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
846/*
847 libmemif_main_t *lm = &libmemif_main;
848
849 lm->control_fd_update (c->fd,
850 MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
851*/
852 err = memif_msg_send (c->fd, &e->msg, e->fd);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200853 lm->free (e);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200854 goto done;
855
856done:
857 return err;
858}
859
860int
861memif_conn_fd_accept_ready (memif_socket_t * ms)
862{
863 int addr_len;
864 struct sockaddr_un client;
865 int conn_fd;
866 libmemif_main_t *lm = &libmemif_main;
867
868 DBG ("accept called");
869
870 addr_len = sizeof (client);
871 conn_fd =
872 accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
873
874 if (conn_fd < 0)
875 {
876 return memif_syscall_error_handler (errno);
877 }
878 DBG ("accept fd %d", ms->fd);
879 DBG ("conn fd %d", conn_fd);
880
881 memif_list_elt_t elt;
882 elt.key = conn_fd;
883 elt.data_struct = ms;
884
885 add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
886 lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE);
887
888 return memif_msg_send_hello (conn_fd);
889}
890
891int
892memif_read_ready (int fd)
893{
894 int err;
895 DBG ("call recv");
896 err = memif_msg_receive (fd);
897 DBG ("recv finished");
898 return err;
899}