session: use session error type instead of vnet error
[vpp.git] / extras / libmemif / src / main.c
index 6a8e8b9..e735ee3 100644 (file)
@@ -409,6 +409,16 @@ memif_connect_handler (memif_fd_event_type_t type, void *private_ctx)
   memif_socket_t *ms = (memif_socket_t *) private_ctx;
   memif_connection_t *c;
 
+  if (ms->timer_fd >= 0)
+    {
+      uint64_t u64;
+      /*
+       Have to read the timer fd else it stays read-ready
+       and makes epoll_pwait() return without sleeping
+      */
+      read (ms->timer_fd, &u64, sizeof (u64));
+    }
+
   /* loop ms->slave_interfaces and request connection for disconnected ones */
   TAILQ_FOREACH (c, &ms->slave_interfaces, next)
   {
@@ -500,9 +510,6 @@ memif_create_socket (memif_socket_handle_t *sock, memif_socket_args_t *args,
 
   /* copy arguments to internal struct */
   memcpy (&ms->args, args, sizeof (*args));
-  /* Handle abstract socket by converting '@' -> '\0' */
-  if (ms->args.path[0] == '@')
-    ms->args.path[0] = '\0';
   ms->private_ctx = private_ctx;
 
   if (ms->args.alloc == NULL)
@@ -723,6 +730,12 @@ error:
   return err;
 }
 
+static inline int
+memif_path_is_abstract (const char *filename)
+{
+  return (filename[0] == '@');
+}
+
 int
 memif_request_connection (memif_conn_handle_t c)
 {
@@ -736,6 +749,7 @@ memif_request_connection (memif_conn_handle_t c)
   memif_control_channel_t *cc = NULL;
   memif_fd_event_t fde;
   memif_fd_event_data_t *fdata = NULL;
+  int sunlen = sizeof (un);
   void *ctx;
 
   if (conn == NULL)
@@ -773,6 +787,16 @@ memif_request_connection (memif_conn_handle_t c)
       goto error;
     }
 
+  if (memif_path_is_abstract (ms->args.path))
+    {
+      /* Ensure the string is NULL terminated */
+      un.sun_path[sizeof (un.sun_path) - 1] = '\0';
+      /* sunlen is strlen(un.sun_path) + sizeof(un.sun_family) */
+      sunlen = strlen (un.sun_path) + (sizeof (un) - sizeof (un.sun_path));
+      /* Handle abstract socket by converting '@' -> '\0' */
+      un.sun_path[0] = '\0';
+    }
+
   if (conn->args.is_master != 0)
     {
       /* Configure socket optins */
@@ -781,7 +805,7 @@ memif_request_connection (memif_conn_handle_t c)
          err = memif_syscall_error_handler (errno);
          goto error;
        }
-      if (bind (sockfd, (struct sockaddr *) &un, sizeof (un)) < 0)
+      if (bind (sockfd, (struct sockaddr *) &un, sunlen) < 0)
        {
          err = memif_syscall_error_handler (errno);
          goto error;
@@ -791,7 +815,7 @@ memif_request_connection (memif_conn_handle_t c)
          err = memif_syscall_error_handler (errno);
          goto error;
        }
-      if (ms->args.path[0] != '\0')
+      if (!memif_path_is_abstract (ms->args.path))
        {
          /* Verify that the socket was created */
          if (stat ((char *) ms->args.path, &file_stat) < 0)
@@ -815,8 +839,7 @@ memif_request_connection (memif_conn_handle_t c)
          err = MEMIF_ERR_NOMEM;
          goto error;
        }
-      if (connect (sockfd, (struct sockaddr *) &un,
-                  sizeof (struct sockaddr_un)) != 0)
+      if (connect (sockfd, (struct sockaddr *) &un, sunlen) != 0)
        {
          err = MEMIF_ERR_CONNREFUSED;
          goto error;
@@ -1522,7 +1545,6 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
   memif_ring_t *ring = mq->ring;
   memif_buffer_t *b0;
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
-  uint32_t offset_mask = c->run_args.buffer_size - 1;
   uint16_t ring_size;
   uint16_t ns;
   int err = MEMIF_ERR_SUCCESS; /* 0 */
@@ -1550,6 +1572,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
 
       b0->desc_index = mq->next_buf;
       ring->desc[mq->next_buf & mask].flags = 0;
+      b0->flags = 0;
 
       /* slave can produce buffer with original length */
       dst_left = (c->args.is_master) ? ring->desc[mq->next_buf & mask].length :
@@ -1596,7 +1619,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
              if (ms->get_external_buffer_offset)
                d->offset = ms->get_external_buffer_offset (c->private_ctx);
              else
-               d->offset = d->offset - (d->offset & offset_mask);
+               d->offset = d->offset - (d->offset % c->run_args.buffer_size);
            }
          b0->data = memif_get_buffer (c, ring, mq->next_buf & mask);
 
@@ -1642,7 +1665,6 @@ memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
   memif_queue_t *mq = &c->rx_queues[qid];
   memif_ring_t *ring = mq->ring;
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
-  uint32_t offset_mask = c->run_args.buffer_size - 1;
   uint16_t slot, counter = 0;
 
   if (c->args.is_master)
@@ -1668,7 +1690,8 @@ memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
       if (ms->get_external_buffer_offset)
        d->offset = ms->get_external_buffer_offset (c->private_ctx);
       else
-       d->offset = d->offset - (d->offset & offset_mask) + headroom;
+       d->offset =
+         d->offset - (d->offset % c->run_args.buffer_size) + headroom;
       slot++;
       counter++;
     }
@@ -1699,7 +1722,6 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
   memif_queue_t *mq = &c->tx_queues[qid];
   memif_ring_t *ring = mq->ring;
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
-  uint32_t offset_mask = c->run_args.buffer_size - 1;
   memif_buffer_t *b0;
   memif_desc_t *d;
   int64_t data_offset;
@@ -1727,10 +1749,12 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
        }
       d = &ring->desc[b0->desc_index & mask];
       d->length = b0->len;
+      d->flags =
+       ((b0->flags & MEMIF_BUFFER_FLAG_NEXT) == 1) ? MEMIF_DESC_FLAG_NEXT : 0;
       if (!c->args.is_master)
        {
          // reset headroom
-         d->offset = d->offset - (d->offset & offset_mask);
+         d->offset = d->offset - (d->offset % c->run_args.buffer_size);
          // calculate offset from user data
          data_offset = b0->data - (d->offset + c->regions[d->region].addr);
          if (data_offset != 0)
@@ -1739,7 +1763,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
              if ((data_offset < 0) ||
                  ((data_offset + b0->len) > c->run_args.buffer_size))
                {
-                 DBG ("slot: %d, data_offset: %d, length: %d",
+                 DBG ("slot: %d, data_offset: %ld, length: %d",
                       b0->desc_index & mask, data_offset, b0->len);
                  err = MEMIF_ERR_INVAL_ARG;
                  goto done;
@@ -1828,6 +1852,7 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
       b0->desc_index = cur_slot;
       b0->data = memif_get_buffer (c, ring, cur_slot & mask);
       b0->len = ring->desc[cur_slot & mask].length;
+      b0->flags = 0;
       /* slave resets buffer length */
       if (c->args.is_master == 0)
        {
@@ -1947,10 +1972,6 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
   if (l0 + l1 < buflen)
     {
       md->socket_path = (uint8_t *) memcpy (buf + l0, ms->args.path, 108);
-      if (md->socket_path[0] == '\0')
-       {
-         md->socket_path[0] = '@';
-       }
       l0 += l1;
     }
   else