devices: tap API cleanup
[vpp.git] / extras / libmemif / src / socket.c
index e568079..2ea6fab 100644 (file)
@@ -71,7 +71,7 @@ memif_msg_send (int fd, memif_msg_t * msg, int afd)
 static_fn int
 memif_msg_enq_ack (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -99,20 +99,20 @@ memif_msg_enq_ack (memif_connection_t * c)
 }
 
 static_fn int
-memif_msg_send_hello (int fd)
+memif_msg_send_hello (libmemif_main_t * lm, int fd)
 {
-  libmemif_main_t *lm = &libmemif_main;
   memif_msg_t msg = { 0 };
   memif_msg_hello_t *h = &msg.hello;
   msg.type = MEMIF_MSG_TYPE_HELLO;
   h->min_version = MEMIF_VERSION;
   h->max_version = MEMIF_VERSION;
-  h->max_s2m_ring = MEMIF_MAX_M2S_RING;
+  h->max_s2m_ring = MEMIF_MAX_S2M_RING;
   h->max_m2s_ring = MEMIF_MAX_M2S_RING;
   h->max_region = MEMIF_MAX_REGION;
   h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
 
-  strncpy ((char *) h->name, lm->app_name, strlen (lm->app_name));
+  strncpy ((char *) h->name, (char *) lm->app_name,
+          strlen ((char *) lm->app_name));
 
   /* msg hello is not enqueued but sent directly,
      because it is the first msg to be sent */
@@ -123,7 +123,7 @@ memif_msg_send_hello (int fd)
 static_fn int
 memif_msg_enq_init (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -141,7 +141,7 @@ memif_msg_enq_init (memif_connection_t * c)
 
   strncpy ((char *) i->name, (char *) lm->app_name,
           strlen ((char *) lm->app_name));
-  if (c->args.secret)
+  if (strlen ((char *) c->args.secret) > 0)
     strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
 
   e->next = NULL;
@@ -165,8 +165,7 @@ memif_msg_enq_init (memif_connection_t * c)
 static_fn int
 memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
 {
-  libmemif_main_t *lm = &libmemif_main;
-  /* maybe check if region is valid? */
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_region_t *mr = &c->regions[region_index];
 
   memif_msg_queue_elt_t *e =
@@ -203,7 +202,7 @@ memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
 static_fn int
 memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -250,7 +249,7 @@ memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
 static_fn int
 memif_msg_enq_connect (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -285,7 +284,7 @@ memif_msg_enq_connect (memif_connection_t * c)
 static_fn int
 memif_msg_enq_connected (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -371,11 +370,11 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
   memif_list_elt_t *elt = NULL;
   memif_list_elt_t elt2;
   memif_connection_t *c = NULL;
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (ms);
   uint8_t err_string[96];
   memset (err_string, 0, sizeof (char) * 96);
   int err = MEMIF_ERR_SUCCESS; /* 0 */
-  int err_disc;
+
   if (i->version != MEMIF_VERSION)
     {
       DBG ("MEMIF_VER_ERR");
@@ -424,10 +423,10 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
   strncpy ((char *) c->remote_name, (char *) i->name,
           strlen ((char *) i->name));
 
-  if (c->args.secret)
+  if (strlen ((char *) c->args.secret) > 0)
     {
       int r;
-      if (i->secret)
+      if (strlen ((char *) i->secret) > 0)
        {
          if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
            {
@@ -465,14 +464,14 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
   elt2.key = c->fd;
   elt2.data_struct = c;
 
-  add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
+  add_list_elt (lm, &elt2, &lm->control_list, &lm->control_list_len);
   free_list_elt (lm->pending_list, lm->pending_list_len, fd);
 
   return err;
 
 error:
   memif_msg_send_disconnect (fd, err_string, 0);
-  lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL);
+  lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL, c->private_ctx);
   free_list_elt (lm->pending_list, lm->pending_list_len, fd);
   close (fd);
   fd = -1;
@@ -484,6 +483,8 @@ static_fn int
 memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
                              int fd)
 {
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
+
   memif_msg_add_region_t *ar = &msg->add_region;
   memif_region_t *mr;
   if (fd < 0)
@@ -493,14 +494,20 @@ memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
     return MEMIF_ERR_MAXREG;
 
   mr =
-    (memif_region_t *) realloc (c->regions,
-                               sizeof (memif_region_t) * (ar->index + 1));
+    (memif_region_t *) lm->realloc (c->regions,
+                                   sizeof (memif_region_t) *
+                                   (++c->regions_num));
   if (mr == NULL)
     return memif_syscall_error_handler (errno);
+  memset (mr + ar->index, 0, sizeof (memif_region_t));
   c->regions = mr;
   c->regions[ar->index].fd = fd;
   c->regions[ar->index].region_size = ar->size;
-  c->regions[ar->index].shm = NULL;
+  c->regions[ar->index].addr = NULL;
+
+  /* region 0 is never external */
+  if (lm->get_external_region_addr && (ar->index != 0))
+    c->regions[ar->index].is_external = 1;
 
   return MEMIF_ERR_SUCCESS;    /* 0 */
 }
@@ -510,6 +517,8 @@ memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
 static_fn int
 memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
 {
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
+
   memif_msg_add_ring_t *ar = &msg->add_ring;
 
   memif_queue_t *mq;
@@ -528,9 +537,10 @@ memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
        return MEMIF_ERR_MAXRING;
 
       mq =
-       (memif_queue_t *) realloc (c->rx_queues,
-                                  sizeof (memif_queue_t) * (ar->index + 1));
-      memset (mq, 0, sizeof (memif_queue_t) * (ar->index + 1));
+       (memif_queue_t *) lm->realloc (c->rx_queues,
+                                      sizeof (memif_queue_t) *
+                                      (++c->rx_queues_num));
+      memset (mq + ar->index, 0, sizeof (memif_queue_t));
       if (mq == NULL)
        return memif_syscall_error_handler (errno);
       c->rx_queues = mq;
@@ -548,9 +558,10 @@ memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
        return MEMIF_ERR_MAXRING;
 
       mq =
-       (memif_queue_t *) realloc (c->tx_queues,
-                                  sizeof (memif_queue_t) * (ar->index + 1));
-      memset (mq, 0, sizeof (memif_queue_t) * (ar->index + 1));
+       (memif_queue_t *) lm->realloc (c->tx_queues,
+                                      sizeof (memif_queue_t) *
+                                      (++c->tx_queues_num));
+      memset (mq + ar->index, 0, sizeof (memif_queue_t));
       if (mq == NULL)
        return memif_syscall_error_handler (errno);
       c->tx_queues = mq;
@@ -569,7 +580,7 @@ static_fn int
 memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
 {
   memif_msg_connect_t *cm = &msg->connect;
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_list_elt_t elt;
 
   int err;
@@ -587,9 +598,11 @@ memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
        {
          elt.key = c->rx_queues[i].int_fd;
          elt.data_struct = c;
-         add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
+         add_list_elt (lm, &elt, &lm->interrupt_list,
+                       &lm->interrupt_list_len);
 
-         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
+         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
+                                c->private_ctx);
        }
 
     }
@@ -604,7 +617,7 @@ static_fn int
 memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
 {
   memif_msg_connect_t *cm = &msg->connect;
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
 
   int err;
   err = memif_connect1 (c);
@@ -619,7 +632,8 @@ memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
     {
       for (i = 0; i < c->run_args.num_s2m_rings; i++)
        {
-         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
+         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
+                                c->private_ctx);
        }
     }
 
@@ -645,7 +659,7 @@ memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
 }
 
 static_fn int
-memif_msg_receive (int ifd)
+memif_msg_receive (libmemif_main_t * lm, int ifd)
 {
   char ctl[CMSG_SPACE (sizeof (int)) +
           CMSG_SPACE (sizeof (struct ucred))] = { 0 };
@@ -656,7 +670,6 @@ memif_msg_receive (int ifd)
   int err = MEMIF_ERR_SUCCESS; /* 0 */
   int fd = -1;
   int i;
-  libmemif_main_t *lm = &libmemif_main;
   memif_connection_t *c = NULL;
   memif_socket_t *ms = NULL;
   memif_list_elt_t *elt = NULL;
@@ -670,7 +683,6 @@ memif_msg_receive (int ifd)
 
   DBG ("recvmsg fd %d", ifd);
   size = recvmsg (ifd, &mh, 0);
-  DBG ("done");
   if (size != sizeof (memif_msg_t))
     {
       if (size == 0)
@@ -679,7 +691,6 @@ memif_msg_receive (int ifd)
        return MEMIF_ERR_MFMSG;
     }
 
-  struct ucred *cr = 0;
   struct cmsghdr *cmsg;
 
   cmsg = CMSG_FIRSTHDR (&mh);
@@ -689,7 +700,7 @@ memif_msg_receive (int ifd)
        {
          if (cmsg->cmsg_type == SCM_CREDENTIALS)
            {
-             cr = (struct ucred *) CMSG_DATA (cmsg);
+             /* Do nothing */ ;
            }
          else if (cmsg->cmsg_type == SCM_RIGHTS)
            {
@@ -718,8 +729,11 @@ memif_msg_receive (int ifd)
        return err;
       if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
        return err;
-      if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS)
-       return err;
+      for (i = 0; i < c->regions_num; i++)
+       {
+         if ((err = memif_msg_enq_add_region (c, i)) != MEMIF_ERR_SUCCESS)
+           return err;
+       }
       for (i = 0; i < c->run_args.num_s2m_rings; i++)
        {
          if ((err =
@@ -796,8 +810,7 @@ memif_msg_receive (int ifd)
 
   if (c != NULL)
     c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
-/*    libmemif_main_t *lm = &libmemif_main;
-    lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
+
   return MEMIF_ERR_SUCCESS;    /* 0 */
 }
 
@@ -814,8 +827,10 @@ memif_conn_fd_error (memif_connection_t * c)
 int
 memif_conn_fd_read_ready (memif_connection_t * c)
 {
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   int err;
-  err = memif_msg_receive (c->fd);
+
+  err = memif_msg_receive (lm, c->fd);
   if (err != 0)
     {
       err = memif_disconnect_internal (c);
@@ -827,7 +842,7 @@ memif_conn_fd_read_ready (memif_connection_t * c)
 int
 memif_conn_fd_write_ready (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   int err = MEMIF_ERR_SUCCESS; /* 0 */
 
 
@@ -841,12 +856,7 @@ memif_conn_fd_write_ready (memif_connection_t * c)
   c->msg_queue = c->msg_queue->next;
 
   c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
-/*
-    libmemif_main_t *lm = &libmemif_main;
 
-    lm->control_fd_update (c->fd,
-        MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
-*/
   err = memif_msg_send (c->fd, &e->msg, e->fd);
   lm->free (e);
   goto done;
@@ -861,7 +871,7 @@ memif_conn_fd_accept_ready (memif_socket_t * ms)
   int addr_len;
   struct sockaddr_un client;
   int conn_fd;
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (ms);
 
   DBG ("accept called");
 
@@ -880,18 +890,19 @@ memif_conn_fd_accept_ready (memif_socket_t * ms)
   elt.key = conn_fd;
   elt.data_struct = ms;
 
-  add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
-  lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE);
+  add_list_elt (lm, &elt, &lm->pending_list, &lm->pending_list_len);
+  lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE,
+                        ms->private_ctx);
 
-  return memif_msg_send_hello (conn_fd);
+  return memif_msg_send_hello (lm, conn_fd);
 }
 
 int
-memif_read_ready (int fd)
+memif_read_ready (libmemif_main_t * lm, int fd)
 {
   int err;
-  DBG ("call recv");
-  err = memif_msg_receive (fd);
-  DBG ("recv finished");
+
+  err = memif_msg_receive (lm, fd);
+
   return err;
 }