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