socket api: multiple fds in one msg 50/13950/3
authorFlorin Coras <fcoras@cisco.com>
Fri, 3 Aug 2018 09:50:43 +0000 (02:50 -0700)
committerDave Barach <openvpp@barachs.net>
Sat, 4 Aug 2018 16:00:32 +0000 (16:00 +0000)
Change-Id: I77a6e092a42290eed7201ad4a62e0d00ef997d2b
Signed-off-by: Florin Coras <fcoras@cisco.com>
src/tests/vnet/session/tcp_echo.c
src/vlibmemory/api.h
src/vlibmemory/socket_api.c
src/vlibmemory/socket_api.h
src/vlibmemory/socket_client.c
src/vlibmemory/socket_client.h
src/vnet/session/session_api.c

index 626ef84..6a5d8c4 100644 (file)
@@ -353,7 +353,7 @@ memfd_segment_attach (void)
   clib_error_t *error;
   int rv;
 
-  if ((error = vl_socket_client_recv_fd_msg (&ssvm->fd, 5)))
+  if ((error = vl_socket_client_recv_fd_msg (&ssvm->fd, 1, 5)))
     {
       clib_error_report (error);
       return -1;
@@ -379,7 +379,7 @@ fifo_segment_attach (char *name, u32 size, ssvm_segment_type_t type)
 
   if (type == SSVM_SEGMENT_MEMFD)
     {
-      if ((error = vl_socket_client_recv_fd_msg (&a->memfd_fd, 5)))
+      if ((error = vl_socket_client_recv_fd_msg (&a->memfd_fd, 1, 5)))
        {
          clib_error_report (error);
          return -1;
index 8d2b191..2146b16 100644 (file)
@@ -82,20 +82,21 @@ vl_api_registration_del_file (vl_api_registration_t * reg)
 }
 
 always_inline clib_error_t *
-vl_api_send_fd_msg (vl_api_registration_t * reg, int fd_to_send)
+vl_api_send_fd_msg (vl_api_registration_t * reg, int fds[], int n_fds)
 {
   clib_file_t *cf = vl_api_registration_file (reg);
   if (cf)
-    return vl_sock_api_send_fd_msg (cf->file_descriptor, fd_to_send);
+    return vl_sock_api_send_fd_msg (cf->file_descriptor, fds, n_fds);
   return 0;
 }
 
 always_inline clib_error_t *
-vl_api_recv_fd_msg (vl_api_registration_t * reg, int *fd_to_recv, u32 wait)
+vl_api_recv_fd_msg (vl_api_registration_t * reg, int fds[], int n_fds,
+                   u32 wait)
 {
   clib_file_t *cf = vl_api_registration_file (reg);
   if (cf)
-    return vl_sock_api_recv_fd_msg (cf->file_descriptor, fd_to_recv, wait);
+    return vl_sock_api_recv_fd_msg (cf->file_descriptor, fds, n_fds, wait);
   return 0;
 }
 
index 7d27127..5587611 100644 (file)
@@ -449,12 +449,13 @@ vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp)
 }
 
 clib_error_t *
-vl_sock_api_send_fd_msg (int socket_fd, int fd_to_share)
+vl_sock_api_send_fd_msg (int socket_fd, int fds[], int n_fds)
 {
   struct msghdr mh = { 0 };
   struct iovec iov[1];
-  char ctl[CMSG_SPACE (sizeof (int))];
-  char *msg = "memfd";
+  char ctl[CMSG_SPACE (sizeof (int)) * n_fds];
+  struct cmsghdr *cmsg;
+  char *msg = "fdmsg";
   int rv;
 
   iov[0].iov_base = msg;
@@ -462,15 +463,14 @@ vl_sock_api_send_fd_msg (int socket_fd, int fd_to_share)
   mh.msg_iov = iov;
   mh.msg_iovlen = 1;
 
-  struct cmsghdr *cmsg;
   memset (&ctl, 0, sizeof (ctl));
   mh.msg_control = ctl;
   mh.msg_controllen = sizeof (ctl);
   cmsg = CMSG_FIRSTHDR (&mh);
-  cmsg->cmsg_len = CMSG_LEN (sizeof (int));
+  cmsg->cmsg_len = CMSG_LEN (sizeof (int) * n_fds);
   cmsg->cmsg_level = SOL_SOCKET;
   cmsg->cmsg_type = SCM_RIGHTS;
-  memcpy (CMSG_DATA (cmsg), &fd_to_share, sizeof (int));
+  memcpy (CMSG_DATA (cmsg), fds, sizeof (int) * n_fds);
 
   rv = sendmsg (socket_fd, &mh, 0);
   if (rv < 0)
@@ -629,7 +629,7 @@ reply:
   cf->write_function (cf);
 
   /* Send the magic "here's your sign (aka fd)" socket message */
-  vl_sock_api_send_fd_msg (cf->file_descriptor, memfd->fd);
+  vl_sock_api_send_fd_msg (cf->file_descriptor, &memfd->fd, 1);
 }
 
 #define foreach_vlib_api_msg                           \
index d12b427..86fd600 100644 (file)
@@ -77,8 +77,8 @@ void vl_socket_process_api_msg (clib_file_t * uf, vl_api_registration_t * rp,
                                i8 * input_v);
 void vl_sock_api_dump_clients (vlib_main_t * vm, api_main_t * am);
 clib_error_t *vl_sock_api_init (vlib_main_t * vm);
-clib_error_t *vl_sock_api_send_fd_msg (int socket_fd, int fd_to_share);
-clib_error_t *vl_sock_api_recv_fd_msg (int socket_fd, int *fd_to_share,
+clib_error_t *vl_sock_api_send_fd_msg (int socket_fd, int fds[], int n_fds);
+clib_error_t *vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds,
                                       u32 wait);
 
 #endif /* SRC_VLIBMEMORY_SOCKET_API_H_ */
index a49d335..3d933de 100644 (file)
@@ -177,11 +177,12 @@ vl_socket_client_enable_disable (int enable)
 }
 
 clib_error_t *
-vl_sock_api_recv_fd_msg (int socket_fd, int *my_fd, u32 wait)
+vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
 {
   socket_client_main_t *scm = &socket_client_main;
   char msgbuf[16];
-  char ctl[CMSG_SPACE (sizeof (int)) + CMSG_SPACE (sizeof (struct ucred))];
+  char ctl[CMSG_SPACE (sizeof (int) * n_fds)
+          + CMSG_SPACE (sizeof (struct ucred))];
   struct msghdr mh = { 0 };
   struct iovec iov[1];
   ssize_t size = 0;
@@ -231,7 +232,7 @@ vl_sock_api_recv_fd_msg (int socket_fd, int *my_fd, u32 wait)
            }
          else if (cmsg->cmsg_type == SCM_RIGHTS)
            {
-             clib_memcpy (my_fd, CMSG_DATA (cmsg), sizeof (int));
+             clib_memcpy (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds);
            }
        }
       cmsg = CMSG_NXTHDR (&mh, cmsg);
@@ -259,7 +260,7 @@ static void vl_api_sock_init_shm_reply_t_handler
   /*
    * Check the socket for the magic fd
    */
-  error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 5);
+  error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5);
   if (error)
     {
       clib_error_report (error);
@@ -409,12 +410,12 @@ vl_socket_client_init_shm (vl_api_shm_elem_config_t * config)
 }
 
 clib_error_t *
-vl_socket_client_recv_fd_msg (int *fd_to_recv, u32 wait)
+vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait)
 {
   socket_client_main_t *scm = &socket_client_main;
   if (!scm->socket_fd)
     return clib_error_return (0, "no socket");
-  return vl_sock_api_recv_fd_msg (scm->client_socket.fd, fd_to_recv, wait);
+  return vl_sock_api_recv_fd_msg (scm->client_socket.fd, fds, n_fds, wait);
 }
 
 /*
index 74be5a9..5612a98 100644 (file)
@@ -53,7 +53,7 @@ int vl_socket_client_write (void);
 void vl_socket_client_enable_disable (int enable);
 void *vl_socket_client_msg_alloc (int nbytes);
 int vl_socket_client_init_shm (vl_api_shm_elem_config_t * config);
-clib_error_t *vl_socket_client_recv_fd_msg (int *fd_to_recv, u32 wait);
+clib_error_t *vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait);
 
 #endif /* SRC_VLIBMEMORY_SOCKET_CLIENT_H_ */
 
index 8585b57..76c1021 100755 (executable)
@@ -63,12 +63,13 @@ static int
 session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
 {
   clib_error_t *error;
+  int fd = sp->fd;
   if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
     {
       clib_warning ("can't send memfd fd");
       return -1;
     }
-  error = vl_api_send_fd_msg (reg, sp->fd);
+  error = vl_api_send_fd_msg (reg, &fd, 1);
   if (error)
     {
       clib_error_report (error);