blob: a471e0ba943c070c05c3265e486c799c65d0b706 [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;
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200230 ar->private_hdr_size = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200231
232 e->next = NULL;
233 if (c->msg_queue == NULL)
234 {
235 c->msg_queue = e;
236 return MEMIF_ERR_SUCCESS; /* 0 */
237 }
238
239 memif_msg_queue_elt_t *cur = c->msg_queue;
240 while (cur->next != NULL)
241 {
242 cur = cur->next;
243 }
244 cur->next = e;
245
246 return MEMIF_ERR_SUCCESS; /* 0 */
247}
248
249/* used as connection request from slave */
250static_fn int
251memif_msg_enq_connect (memif_connection_t * c)
252{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200253 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200254 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200255 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200256 if (e == NULL)
257 return memif_syscall_error_handler (errno);
258
259 memset (&e->msg, 0, sizeof (e->msg));
260 memif_msg_connect_t *cm = &e->msg.connect;
261
262 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
263 e->fd = -1;
264 strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
265 strlen ((char *) c->args.interface_name));
266
267 e->next = NULL;
268 if (c->msg_queue == NULL)
269 {
270 c->msg_queue = e;
271 return MEMIF_ERR_SUCCESS; /* 0 */
272 }
273
274 memif_msg_queue_elt_t *cur = c->msg_queue;
275 while (cur->next != NULL)
276 {
277 cur = cur->next;
278 }
279 cur->next = e;
280
281 return MEMIF_ERR_SUCCESS; /* 0 */
282}
283
284/* used as confirmation of connection by master */
285static_fn int
286memif_msg_enq_connected (memif_connection_t * c)
287{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200288 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200289 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200290 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200291 if (e == NULL)
292 return memif_syscall_error_handler (errno);
293
294 memset (&e->msg, 0, sizeof (e->msg));
295 memif_msg_connected_t *cm = &e->msg.connected;
296
297 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
298 e->fd = -1;
299 strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
300 strlen ((char *) c->args.interface_name));
301
302 e->next = NULL;
303 if (c->msg_queue == NULL)
304 {
305 c->msg_queue = e;
306 return MEMIF_ERR_SUCCESS; /* 0 */
307 }
308
309 memif_msg_queue_elt_t *cur = c->msg_queue;
310 while (cur->next != NULL)
311 {
312 cur = cur->next;
313 }
314 cur->next = e;
315
316 return MEMIF_ERR_SUCCESS; /* 0 */
317}
318
319/* immediately send disconnect msg */
320 /* specifie protocol for disconnect msg err_code
321 so that it will be compatible with VPP? (header/doc) */
322int
323memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
324{
325 memif_msg_t msg = { 0 };
326 memif_msg_disconnect_t *d = &msg.disconnect;
327
328 msg.type = MEMIF_MSG_TYPE_DISCONNECT;
329 d->code = err_code;
330 uint16_t l = strlen ((char *) err_string);
331 if (l > 96)
332 {
333 DBG ("Disconnect string too long. Sending first 96 characters.");
334 l = 96;
335 }
336 strncpy ((char *) d->string, (char *) err_string, l);
337
338 return memif_msg_send (fd, &msg, -1);
339}
340
341static_fn int
342memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
343{
344 memif_msg_hello_t *h = &msg->hello;
345
346 if (msg->hello.min_version > MEMIF_VERSION ||
347 msg->hello.max_version < MEMIF_VERSION)
348 {
349 DBG ("incompatible protocol version");
350 return MEMIF_ERR_PROTO;
351 }
352
353 c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
354 c->args.num_s2m_rings);
355 c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
356 c->args.num_m2s_rings);
357 c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
358 c->args.log2_ring_size);
359 c->run_args.buffer_size = c->args.buffer_size;
360 strncpy ((char *) c->remote_name, (char *) h->name,
361 strlen ((char *) h->name));
362
363 return MEMIF_ERR_SUCCESS; /* 0 */
364}
365
366/* handle interface identification (id, secret (optional)) */
367static_fn int
368memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
369{
370 memif_msg_init_t *i = &msg->init;
371 memif_list_elt_t *elt = NULL;
372 memif_list_elt_t elt2;
373 memif_connection_t *c = NULL;
374 libmemif_main_t *lm = &libmemif_main;
375 uint8_t err_string[96];
376 memset (err_string, 0, sizeof (char) * 96);
377 int err = MEMIF_ERR_SUCCESS; /* 0 */
378 int err_disc;
379 if (i->version != MEMIF_VERSION)
380 {
381 DBG ("MEMIF_VER_ERR");
382 strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
383 err = MEMIF_ERR_PROTO;
384 goto error;
385 }
386
387 get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
388 if (elt == NULL)
389 {
390 DBG ("MEMIF_ID_ERR");
391 strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
392 err = MEMIF_ERR_ID;
393 goto error;
394 }
395
396 c = (memif_connection_t *) elt->data_struct;
397
398 if (!(c->args.is_master))
399 {
400 DBG ("MEMIF_SLAVE_ERR");
401 strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
402 strlen (MEMIF_SLAVE_ERR));
403 err = MEMIF_ERR_ACCSLAVE;
404 goto error;
405 }
406 if (c->fd != -1)
407 {
408 DBG ("MEMIF_CONN_ERR");
409 strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
410 err = MEMIF_ERR_ALRCONN;
411 goto error;
412 }
413
414 c->fd = fd;
415
416 if (i->mode != c->args.mode)
417 {
418 DBG ("MEMIF_MODE_ERR");
419 strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
420 err = MEMIF_ERR_MODE;
421 goto error;
422 }
423
424 strncpy ((char *) c->remote_name, (char *) i->name,
425 strlen ((char *) i->name));
426
427 if (c->args.secret)
428 {
429 int r;
430 if (i->secret)
431 {
432 if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
433 {
434 DBG ("MEMIF_SECRET_ERR");
435 strncpy ((char *) err_string,
436 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
437 err = MEMIF_ERR_SECRET;
438 goto error;
439 }
440 r = strncmp ((char *) i->secret, (char *) c->args.secret,
441 strlen ((char *) c->args.secret));
442 if (r != 0)
443 {
444 DBG ("MEMIF_SECRET_ERR");
445 strncpy ((char *) err_string,
446 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
447 err = MEMIF_ERR_SECRET;
448 goto error;
449 }
450 }
451 else
452 {
453 DBG ("MEMIF_NOSECRET_ERR");
454 strncpy ((char *) err_string,
455 MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR));
456 err = MEMIF_ERR_NOSECRET;
457 goto error;
458 }
459 }
460
461 c->read_fn = memif_conn_fd_read_ready;
462 c->write_fn = memif_conn_fd_write_ready;
463 c->error_fn = memif_conn_fd_error;
464
465 elt2.key = c->fd;
466 elt2.data_struct = c;
467
468 add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
469 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
470
471 return err;
472
473error:
474 memif_msg_send_disconnect (fd, err_string, 0);
475 lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL);
476 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
477 close (fd);
478 fd = -1;
479 return err;
480}
481
482/* receive region information and add new region to connection (if possible) */
483static_fn int
484memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
485 int fd)
486{
487 memif_msg_add_region_t *ar = &msg->add_region;
488 memif_region_t *mr;
489 if (fd < 0)
490 return MEMIF_ERR_NO_SHMFD;
491
492 if (ar->index > MEMIF_MAX_REGION)
493 return MEMIF_ERR_MAXREG;
494
495 mr =
496 (memif_region_t *) realloc (c->regions,
497 sizeof (memif_region_t) * (ar->index + 1));
498 if (mr == NULL)
499 return memif_syscall_error_handler (errno);
500 c->regions = mr;
501 c->regions[ar->index].fd = fd;
502 c->regions[ar->index].region_size = ar->size;
503 c->regions[ar->index].shm = NULL;
504
505 return MEMIF_ERR_SUCCESS; /* 0 */
506}
507
508/* receive ring information and add new ring to connection queue
509 (based on direction S2M | M2S) */
510static_fn int
511memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
512{
513 memif_msg_add_ring_t *ar = &msg->add_ring;
514
515 memif_queue_t *mq;
516
517 if (fd < 0)
518 return MEMIF_ERR_NO_INTFD;
519
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200520 if (ar->private_hdr_size != 0)
521 return MEMIF_ERR_PRIVHDR;
522
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200523 if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
524 {
525 if (ar->index > MEMIF_MAX_S2M_RING)
526 return MEMIF_ERR_MAXRING;
527 if (ar->index >= c->args.num_s2m_rings)
528 return MEMIF_ERR_MAXRING;
529
530 mq =
531 (memif_queue_t *) realloc (c->rx_queues,
532 sizeof (memif_queue_t) * (ar->index + 1));
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200533 memset (mq, 0, sizeof (memif_queue_t) * (ar->index + 1));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200534 if (mq == NULL)
535 return memif_syscall_error_handler (errno);
536 c->rx_queues = mq;
537 c->rx_queues[ar->index].int_fd = fd;
538 c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
539 c->rx_queues[ar->index].region = ar->region;
540 c->rx_queues[ar->index].offset = ar->offset;
541 c->run_args.num_s2m_rings++;
542 }
543 else
544 {
545 if (ar->index > MEMIF_MAX_M2S_RING)
546 return MEMIF_ERR_MAXRING;
547 if (ar->index >= c->args.num_m2s_rings)
548 return MEMIF_ERR_MAXRING;
549
550 mq =
551 (memif_queue_t *) realloc (c->tx_queues,
552 sizeof (memif_queue_t) * (ar->index + 1));
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200553 memset (mq, 0, sizeof (memif_queue_t) * (ar->index + 1));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200554 if (mq == NULL)
555 return memif_syscall_error_handler (errno);
556 c->tx_queues = mq;
557 c->tx_queues[ar->index].int_fd = fd;
558 c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
559 c->tx_queues[ar->index].region = ar->region;
560 c->tx_queues[ar->index].offset = ar->offset;
561 c->run_args.num_m2s_rings++;
562 }
563
564 return MEMIF_ERR_SUCCESS; /* 0 */
565}
566
567/* slave -> master */
568static_fn int
569memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
570{
571 memif_msg_connect_t *cm = &msg->connect;
572 libmemif_main_t *lm = &libmemif_main;
573 memif_list_elt_t elt;
574
575 int err;
576 err = memif_connect1 (c);
577 if (err != MEMIF_ERR_SUCCESS)
578 return err;
579
580 strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
581 strlen ((char *) cm->if_name));
582
583 int i;
584 if (c->on_interrupt != NULL)
585 {
586 for (i = 0; i < c->run_args.num_m2s_rings; i++)
587 {
588 elt.key = c->rx_queues[i].int_fd;
589 elt.data_struct = c;
590 add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
591
592 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200593
594 /* refill ring buffers */
595 memif_refill_queue ((void *) c, i, -1);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200596 }
597
598 }
599
600 c->on_connect ((void *) c, c->private_ctx);
601
602 return err;
603}
604
605/* master -> slave */
606static_fn int
607memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
608{
609 memif_msg_connect_t *cm = &msg->connect;
610 libmemif_main_t *lm = &libmemif_main;
611
612 int err;
613 err = memif_connect1 (c);
614 if (err != MEMIF_ERR_SUCCESS)
615 return err;
616
617 strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
618 strlen ((char *) cm->if_name));
619
620 int i;
621 if (c->on_interrupt != NULL)
622 {
623 for (i = 0; i < c->run_args.num_s2m_rings; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200624 {
625 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
626
627 /* refill ring buffers */
628 memif_refill_queue ((void *) c, i, -1);
629 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200630 }
631
632 c->on_connect ((void *) c, c->private_ctx);
633
634 return err;
635}
636
637static_fn int
638memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
639{
640 memif_msg_disconnect_t *d = &msg->disconnect;
641
642 memset (c->remote_disconnect_string, 0,
643 sizeof (c->remote_disconnect_string));
644 strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
645 strlen ((char *) d->string));
646
647 /* on returning error, handle function will call memif_disconnect () */
648 DBG ("disconnect received: %s, mode: %d",
649 c->remote_disconnect_string, c->args.mode);
650 return MEMIF_ERR_DISCONNECT;
651}
652
653static_fn int
654memif_msg_receive (int ifd)
655{
656 char ctl[CMSG_SPACE (sizeof (int)) +
657 CMSG_SPACE (sizeof (struct ucred))] = { 0 };
658 struct msghdr mh = { 0 };
659 struct iovec iov[1];
660 memif_msg_t msg = { 0 };
661 ssize_t size;
662 int err = MEMIF_ERR_SUCCESS; /* 0 */
663 int fd = -1;
664 int i;
665 libmemif_main_t *lm = &libmemif_main;
666 memif_connection_t *c = NULL;
667 memif_socket_t *ms = NULL;
668 memif_list_elt_t *elt = NULL;
669
670 iov[0].iov_base = (void *) &msg;
671 iov[0].iov_len = sizeof (memif_msg_t);
672 mh.msg_iov = iov;
673 mh.msg_iovlen = 1;
674 mh.msg_control = ctl;
675 mh.msg_controllen = sizeof (ctl);
676
677 DBG ("recvmsg fd %d", ifd);
678 size = recvmsg (ifd, &mh, 0);
679 DBG ("done");
680 if (size != sizeof (memif_msg_t))
681 {
682 if (size == 0)
683 return MEMIF_ERR_DISCONNECTED;
684 else
685 return MEMIF_ERR_MFMSG;
686 }
687
688 struct ucred *cr = 0;
689 struct cmsghdr *cmsg;
690
691 cmsg = CMSG_FIRSTHDR (&mh);
692 while (cmsg)
693 {
694 if (cmsg->cmsg_level == SOL_SOCKET)
695 {
696 if (cmsg->cmsg_type == SCM_CREDENTIALS)
697 {
698 cr = (struct ucred *) CMSG_DATA (cmsg);
699 }
700 else if (cmsg->cmsg_type == SCM_RIGHTS)
701 {
702 int *fdp = (int *) CMSG_DATA (cmsg);
703 fd = *fdp;
704 }
705 }
706 cmsg = CMSG_NXTHDR (&mh, cmsg);
707 }
708
709 DBG ("Message type %u received", msg.type);
710
711 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
712 if (elt != NULL)
713 c = (memif_connection_t *) elt->data_struct;
714
715 switch (msg.type)
716 {
717 case MEMIF_MSG_TYPE_ACK:
718 break;
719
720 case MEMIF_MSG_TYPE_HELLO:
721 if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
722 return err;
723 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
724 return err;
725 if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
726 return err;
727 if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS)
728 return err;
729 for (i = 0; i < c->run_args.num_s2m_rings; i++)
730 {
731 if ((err =
732 memif_msg_enq_add_ring (c, i,
733 MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS)
734 return err;
735 }
736 for (i = 0; i < c->run_args.num_m2s_rings; i++)
737 {
738 if ((err =
739 memif_msg_enq_add_ring (c, i,
740 MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS)
741 return err;
742 }
743 if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
744 return err;
745 break;
746
747 case MEMIF_MSG_TYPE_INIT:
748 get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
749 if (elt == NULL)
750 return -1;
751 ms = (memif_socket_t *) elt->data_struct;
752 if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
753 return err;
754 /* c->remote_pid = cr->pid */
755 /* c->remote_uid = cr->uid */
756 /* c->remote_gid = cr->gid */
757 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
758 if (elt == NULL)
759 return -1;
760 c = (memif_connection_t *) elt->data_struct;
761 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
762 return err;
763 break;
764
765 case MEMIF_MSG_TYPE_ADD_REGION:
766 if ((err =
767 memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
768 return err;
769 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
770 return err;
771 break;
772
773 case MEMIF_MSG_TYPE_ADD_RING:
774 if ((err =
775 memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
776 return err;
777 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
778 return err;
779 break;
780
781 case MEMIF_MSG_TYPE_CONNECT:
782 if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
783 return err;
784 if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
785 return err;
786 break;
787
788 case MEMIF_MSG_TYPE_CONNECTED:
789 if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
790 return err;
791 break;
792
793 case MEMIF_MSG_TYPE_DISCONNECT:
794 if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
795 return err;
796 break;
797
798 default:
799 return MEMIF_ERR_UNKNOWN_MSG;;
800 break;
801 }
802
803 if (c != NULL)
804 c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
805/* libmemif_main_t *lm = &libmemif_main;
806 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
807 return MEMIF_ERR_SUCCESS; /* 0 */
808}
809
810int
811memif_conn_fd_error (memif_connection_t * c)
812{
813 DBG ("connection fd error");
814 strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
815 int err = memif_disconnect_internal (c);
816 return err;
817}
818
819/* calls memif_msg_receive to handle pending messages on socket */
820int
821memif_conn_fd_read_ready (memif_connection_t * c)
822{
823 int err;
824 err = memif_msg_receive (c->fd);
825 if (err != 0)
826 {
827 err = memif_disconnect_internal (c);
828 }
829 return err;
830}
831
832/* get msg from msg queue buffer and send it to socket */
833int
834memif_conn_fd_write_ready (memif_connection_t * c)
835{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200836 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200837 int err = MEMIF_ERR_SUCCESS; /* 0 */
838
839
840 if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
841 goto done;
842
843 memif_msg_queue_elt_t *e = c->msg_queue;
844 if (e == NULL)
845 goto done;
846
847 c->msg_queue = c->msg_queue->next;
848
849 c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
850/*
851 libmemif_main_t *lm = &libmemif_main;
852
853 lm->control_fd_update (c->fd,
854 MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
855*/
856 err = memif_msg_send (c->fd, &e->msg, e->fd);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200857 lm->free (e);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200858 goto done;
859
860done:
861 return err;
862}
863
864int
865memif_conn_fd_accept_ready (memif_socket_t * ms)
866{
867 int addr_len;
868 struct sockaddr_un client;
869 int conn_fd;
870 libmemif_main_t *lm = &libmemif_main;
871
872 DBG ("accept called");
873
874 addr_len = sizeof (client);
875 conn_fd =
876 accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
877
878 if (conn_fd < 0)
879 {
880 return memif_syscall_error_handler (errno);
881 }
882 DBG ("accept fd %d", ms->fd);
883 DBG ("conn fd %d", conn_fd);
884
885 memif_list_elt_t elt;
886 elt.key = conn_fd;
887 elt.data_struct = ms;
888
889 add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
890 lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE);
891
892 return memif_msg_send_hello (conn_fd);
893}
894
895int
896memif_read_ready (int fd)
897{
898 int err;
899 DBG ("call recv");
900 err = memif_msg_receive (fd);
901 DBG ("recv finished");
902 return err;
903}