FD.io VPP  v18.01-8-g0eacf49
Vector Packet Processing
socket.c
Go to the documentation of this file.
1 /*
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 
36 /* sends msg to socket */
37 static_fn int
38 memif_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 */
70 static_fn int
72 {
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));
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 
99 static_fn int
101 {
103  memif_msg_t msg = { 0 };
104  memif_msg_hello_t *h = &msg.hello;
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 */
121 static_fn int
123 {
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 
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 */
162 static_fn int
164 {
165  /* maybe check if region is valid? */
166  memif_region_t *mr = &c->regions[region_index];
167 
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));
175 
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 */
199 static_fn int
200 memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
201 {
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));
209 
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 */
244 static_fn int
246 {
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 
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 */
278 static_fn int
280 {
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));
288 
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) */
314 int
315 memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
316 {
317  memif_msg_t msg = { 0 };
319 
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 
333 static_fn int
335 {
336  memif_msg_hello_t *h = &msg->hello;
337 
338  if (msg->hello.min_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)) */
359 static_fn int
361 {
362  memif_msg_init_t *i = &msg->init;
363  memif_list_elt_t *elt = NULL;
364  memif_list_elt_t elt2;
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,
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,
439  err = MEMIF_ERR_SECRET;
440  goto error;
441  }
442  }
443  else
444  {
445  DBG ("MEMIF_NOSECRET_ERR");
446  strncpy ((char *) err_string,
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);
462 
463  return err;
464 
465 error:
466  memif_msg_send_disconnect (fd, err_string, 0);
469  close (fd);
470  fd = -1;
471  return err;
472 }
473 
474 /* receive region information and add new region to connection (if possible) */
475 static_fn int
477  int fd)
478 {
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) */
502 static_fn int
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 
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));
522  if (mq == NULL)
523  return memif_syscall_error_handler (errno);
524  c->rx_queues = mq;
525  c->rx_queues[ar->index].int_fd = fd;
526  c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
527  c->rx_queues[ar->index].region = ar->region;
528  c->rx_queues[ar->index].offset = ar->offset;
529  c->run_args.num_s2m_rings++;
530  }
531  else
532  {
533  if (ar->index > MEMIF_MAX_M2S_RING)
534  return MEMIF_ERR_MAXRING;
535  if (ar->index >= c->args.num_m2s_rings)
536  return MEMIF_ERR_MAXRING;
537 
538  mq =
539  (memif_queue_t *) realloc (c->tx_queues,
540  sizeof (memif_queue_t) * (ar->index + 1));
541  if (mq == NULL)
542  return memif_syscall_error_handler (errno);
543  c->tx_queues = mq;
544  c->tx_queues[ar->index].int_fd = fd;
545  c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
546  c->tx_queues[ar->index].region = ar->region;
547  c->tx_queues[ar->index].offset = ar->offset;
548  c->run_args.num_m2s_rings++;
549  }
550 
551  return MEMIF_ERR_SUCCESS; /* 0 */
552 }
553 
554 /* slave -> master */
555 static_fn int
557 {
558  memif_msg_connect_t *cm = &msg->connect;
560  memif_list_elt_t elt;
561 
562  int err;
563  err = memif_connect1 (c);
564  if (err != MEMIF_ERR_SUCCESS)
565  return err;
566 
567  strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
568  strlen ((char *) cm->if_name));
569 
570  int i;
571  if (c->on_interrupt != NULL)
572  {
573  for (i = 0; i < c->run_args.num_m2s_rings; i++)
574  {
575  elt.key = c->rx_queues[i].int_fd;
576  elt.data_struct = c;
578 
579  lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
580  }
581 
582  }
583 
584  c->on_connect ((void *) c, c->private_ctx);
585 
586  return err;
587 }
588 
589 /* master -> slave */
590 static_fn int
592 {
593  memif_msg_connect_t *cm = &msg->connect;
595 
596  int err;
597  err = memif_connect1 (c);
598  if (err != MEMIF_ERR_SUCCESS)
599  return err;
600 
601  strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
602  strlen ((char *) cm->if_name));
603 
604  int i;
605  if (c->on_interrupt != NULL)
606  {
607  for (i = 0; i < c->run_args.num_s2m_rings; i++)
608  lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
609  }
610 
611  c->on_connect ((void *) c, c->private_ctx);
612 
613  return err;
614 }
615 
616 static_fn int
618 {
620 
621  memset (c->remote_disconnect_string, 0,
622  sizeof (c->remote_disconnect_string));
623  strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
624  strlen ((char *) d->string));
625 
626  /* on returning error, handle function will call memif_disconnect () */
627  DBG ("disconnect received: %s, mode: %d",
628  c->remote_disconnect_string, c->args.mode);
629  return MEMIF_ERR_DISCONNECT;
630 }
631 
632 static_fn int
634 {
635  char ctl[CMSG_SPACE (sizeof (int)) +
636  CMSG_SPACE (sizeof (struct ucred))] = { 0 };
637  struct msghdr mh = { 0 };
638  struct iovec iov[1];
639  memif_msg_t msg = { 0 };
640  ssize_t size;
641  int err = MEMIF_ERR_SUCCESS; /* 0 */
642  int fd = -1;
643  int i;
646  memif_socket_t *ms = NULL;
647  memif_list_elt_t *elt = NULL;
648 
649  iov[0].iov_base = (void *) &msg;
650  iov[0].iov_len = sizeof (memif_msg_t);
651  mh.msg_iov = iov;
652  mh.msg_iovlen = 1;
653  mh.msg_control = ctl;
654  mh.msg_controllen = sizeof (ctl);
655 
656  DBG ("recvmsg fd %d", ifd);
657  size = recvmsg (ifd, &mh, 0);
658  DBG ("done");
659  if (size != sizeof (memif_msg_t))
660  {
661  if (size == 0)
662  return MEMIF_ERR_DISCONNECTED;
663  else
664  return MEMIF_ERR_MFMSG;
665  }
666 
667  struct ucred *cr = 0;
668  struct cmsghdr *cmsg;
669 
670  cmsg = CMSG_FIRSTHDR (&mh);
671  while (cmsg)
672  {
673  if (cmsg->cmsg_level == SOL_SOCKET)
674  {
675  if (cmsg->cmsg_type == SCM_CREDENTIALS)
676  {
677  cr = (struct ucred *) CMSG_DATA (cmsg);
678  }
679  else if (cmsg->cmsg_type == SCM_RIGHTS)
680  {
681  int *fdp = (int *) CMSG_DATA (cmsg);
682  fd = *fdp;
683  }
684  }
685  cmsg = CMSG_NXTHDR (&mh, cmsg);
686  }
687 
688  DBG ("Message type %u received", msg.type);
689 
690  get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
691  if (elt != NULL)
692  c = (memif_connection_t *) elt->data_struct;
693 
694  switch (msg.type)
695  {
696  case MEMIF_MSG_TYPE_ACK:
697  break;
698 
700  if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
701  return err;
703  return err;
704  if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
705  return err;
706  if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS)
707  return err;
708  for (i = 0; i < c->run_args.num_s2m_rings; i++)
709  {
710  if ((err =
713  return err;
714  }
715  for (i = 0; i < c->run_args.num_m2s_rings; i++)
716  {
717  if ((err =
720  return err;
721  }
722  if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
723  return err;
724  break;
725 
726  case MEMIF_MSG_TYPE_INIT:
727  get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
728  if (elt == NULL)
729  return -1;
730  ms = (memif_socket_t *) elt->data_struct;
731  if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
732  return err;
733  /* c->remote_pid = cr->pid */
734  /* c->remote_uid = cr->uid */
735  /* c->remote_gid = cr->gid */
736  get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
737  if (elt == NULL)
738  return -1;
739  c = (memif_connection_t *) elt->data_struct;
740  if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
741  return err;
742  break;
743 
745  if ((err =
747  return err;
748  if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
749  return err;
750  break;
751 
753  if ((err =
755  return err;
756  if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
757  return err;
758  break;
759 
761  if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
762  return err;
763  if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
764  return err;
765  break;
766 
768  if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
769  return err;
770  break;
771 
773  if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
774  return err;
775  break;
776 
777  default:
778  return MEMIF_ERR_UNKNOWN_MSG;;
779  break;
780  }
781 
782  if (c != NULL)
783  c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
784 /* libmemif_main_t *lm = &libmemif_main;
785  lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
786  return MEMIF_ERR_SUCCESS; /* 0 */
787 }
788 
789 int
791 {
792  DBG ("connection fd error");
793  strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
794  int err = memif_disconnect_internal (c);
795  return err;
796 }
797 
798 /* calls memif_msg_receive to handle pending messages on socket */
799 int
801 {
802  int err;
803  err = memif_msg_receive (c->fd);
804  if (err != 0)
805  {
806  err = memif_disconnect_internal (c);
807  }
808  return err;
809 }
810 
811 /* get msg from msg queue buffer and send it to socket */
812 int
814 {
815  int err = MEMIF_ERR_SUCCESS; /* 0 */
816 
817 
818  if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
819  goto done;
820 
821  memif_msg_queue_elt_t *e = c->msg_queue;
822  if (e == NULL)
823  goto done;
824 
825  c->msg_queue = c->msg_queue->next;
826 
827  c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
828 /*
829  libmemif_main_t *lm = &libmemif_main;
830 
831  lm->control_fd_update (c->fd,
832  MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
833 */
834  err = memif_msg_send (c->fd, &e->msg, e->fd);
835  free (e);
836  goto done;
837 
838 done:
839  return err;
840 }
841 
842 int
844 {
845  int addr_len;
846  struct sockaddr_un client;
847  int conn_fd;
849 
850  DBG ("accept called");
851 
852  addr_len = sizeof (client);
853  conn_fd =
854  accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
855 
856  if (conn_fd < 0)
857  {
858  return memif_syscall_error_handler (errno);
859  }
860  DBG ("accept fd %d", ms->fd);
861  DBG ("conn fd %d", conn_fd);
862 
863  memif_list_elt_t elt;
864  elt.key = conn_fd;
865  elt.data_struct = ms;
866 
867  add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
869 
870  return memif_msg_send_hello (conn_fd);
871 }
872 
873 int
875 {
876  int err;
877  DBG ("call recv");
878  err = memif_msg_receive (fd);
879  DBG ("recv finished");
880  return err;
881 }
static_fn int memif_msg_receive_disconnect(memif_connection_t *c, memif_msg_t *msg)
Definition: socket.c:617
memif_version_t max_version
Definition: memif.h:76
memif_interface_mode_t mode
Definition: memif.h:87
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
int memif_conn_fd_write_ready(memif_connection_t *c)
Definition: socket.c:813
clib_error_t * memif_conn_fd_accept_ready(clib_file_t *uf)
Definition: socket.c:628
memif_msg_add_region_t add_region
Definition: memif.h:131
#define MEMIF_FD_EVENT_READ
user needs to set events that occured on fd and pass them to memif_control_fd_handler ...
Definition: libmemif.h:88
#define MEMIF_SECRET_ERR
Definition: socket.h:29
uint8_t string[96]
Definition: memif.h:121
#define MEMIF_SLAVE_ERR
Definition: socket.h:26
#define NULL
Definition: clib.h:55
memif_msg_add_ring_t add_ring
Definition: memif.h:132
#define MEMIF_MSG_ADD_RING_FLAG_S2M
Definition: memif.h:101
#define MEMIF_MAX_REGION
Definition: private.h:28
memif_list_elt_t * pending_list
int memif_conn_fd_read_ready(memif_connection_t *c)
Definition: socket.c:800
static_fn int memif_msg_send(int fd, memif_msg_t *msg, int afd)
Definition: socket.c:38
int memif_disconnect_internal(memif_connection_t *c)
Definition: main.c:1004
#define MEMIF_CONNECTION_FLAG_WRITE
memif_control_fd_update_t * control_fd_update
memif_region_index_t region
Definition: memif.h:103
memif_version_t min_version
Definition: memif.h:75
uint8_t secret[24]
Definition: memif.h:88
memif_list_elt_t * interface_list
memif_interface_id_t id
Definition: memif.h:86
uint8_t name[32]
Definition: memif.h:74
#define static_fn
Definition: socket.h:86
memif_ring_index_t max_m2s_ring
Definition: memif.h:78
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
Definition: memif.c:266
#define MEMIF_ID_ERR
Definition: socket.h:25
#define MEMIF_NOSECRET_ERR
Definition: socket.h:30
memif_region_offset_t offset
Definition: private.h:99
#define MEMIF_MAX_M2S_RING
Definition: private.h:26
memif_region_offset_t offset
Definition: memif.h:104
static_fn int memif_msg_receive_connect(memif_connection_t *c, memif_msg_t *msg)
Definition: socket.c:556
#define MEMIF_MAX_LOG2_RING_SIZE
Definition: private.h:29
static_fn int memif_msg_receive_init(memif_socket_t *ms, int fd, memif_msg_t *msg)
Definition: socket.c:360
memif_msg_init_t init
Definition: memif.h:130
int memif_syscall_error_handler(int err_code)
Definition: main.c:192
memif_ring_index_t index
Definition: memif.h:102
memif_list_elt_t * interrupt_list
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: socket.c:189
#define MEMIF_CONN_ERR
Definition: socket.h:27
static_fn int memif_msg_enq_connected(memif_connection_t *c)
Definition: socket.c:279
static_fn int memif_msg_enq_init(memif_connection_t *c)
Definition: socket.c:122
#define MEMIF_FD_EVENT_DEL
if set, informs that fd is going to be closed (user may want to stop watching for events on this fd) ...
Definition: libmemif.h:93
uint8_t * app_name
memif_msg_disconnect_t disconnect
Definition: memif.h:135
struct memif_msg_queue_elt * next
static_fn int memif_msg_enq_ack(memif_connection_t *c)
Definition: socket.c:71
static_fn int memif_msg_send_hello(int fd)
Definition: socket.c:100
memif_msg_hello_t hello
Definition: memif.h:129
memif_log2_ring_size_t max_log2_ring_size
Definition: memif.h:80
int add_list_elt(memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len)
Definition: main.c:303
uint8_t if_name[32]
Definition: memif.h:110
uint8_t name[32]
Definition: memif.h:89
svmdb_client_t * c
vec_header_t h
Definition: buffer.c:282
uint8_t if_name[32]
Definition: memif.h:115
int get_list_elt(memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:338
#define MEMIF_MODE_ERR
Definition: socket.h:28
#define DBG(...)
memif_region_index_t index
Definition: memif.h:94
int memif_read_ready(int fd)
Definition: socket.c:874
memif_log2_ring_size_t log2_ring_size
Definition: memif.h:105
memif_version_t version
Definition: memif.h:85
#define memif_min(a, b)
Definition: memif_private.h:47
memif_ring_index_t max_s2m_ring
Definition: memif.h:79
static_fn int memif_msg_enq_add_region(memif_connection_t *c, uint8_t region_index)
Definition: socket.c:163
memif_msg_connect_t connect
Definition: memif.h:133
#define MEMIF_VERSION
Definition: memif.h:28
u64 size
Definition: vhost-user.h:76
uint16_t interrupt_list_len
static_fn int memif_msg_receive_add_ring(memif_connection_t *c, memif_msg_t *msg, int fd)
Definition: socket.c:503
static_fn int memif_msg_receive(int ifd)
Definition: socket.c:633
uint16_t flags
Definition: memif.h:100
static_fn int memif_msg_receive_add_region(memif_connection_t *c, memif_msg_t *msg, int fd)
Definition: socket.c:476
#define MEMIF_MAX_S2M_RING
Definition: private.h:27
memif_region_size_t size
Definition: memif.h:95
memif_region_index_t max_region
Definition: memif.h:77
memif_msg_type_t type
Definition: memif.h:126
uint16_t pending_list_len
memif_list_elt_t * control_list
static_fn int memif_msg_enq_connect(memif_connection_t *c)
Definition: socket.c:245
memif_log2_ring_size_t log2_ring_size
Definition: private.h:97
static_fn int memif_msg_receive_hello(memif_connection_t *c, memif_msg_t *msg)
Definition: socket.c:334
memif_msg_connected_t connected
Definition: memif.h:134
uint16_t control_list_len
#define MEMIF_VER_ERR
Definition: socket.h:24
static_fn int memif_msg_receive_connected(memif_connection_t *c, memif_msg_t *msg)
Definition: socket.c:591
int memif_conn_fd_error(memif_connection_t *c)
Definition: socket.c:790
int memif_connect1(memif_connection_t *c)
Definition: main.c:1186
libmemif_main_t libmemif_main
Definition: main.c:66
int free_list_elt(memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:361
#define MEMIF_FD_EVENT_WRITE
Definition: libmemif.h:89
memif_region_index_t region
Definition: private.h:98
static_fn int memif_msg_enq_add_ring(memif_connection_t *c, uint8_t index, uint8_t dir)
Definition: socket.c:200
uint16_t interface_list_len
memif_region_size_t region_size
Definition: private.h:83