Note AES PMDs enablement in changelog
[deb_dpdk.git] / lib / librte_vhost / fd_man.c
index 8a075da..38347ab 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
  */
 
 #include <stdint.h>
@@ -45,6 +16,9 @@
 
 #include "fd_man.h"
 
+
+#define RTE_LOGTYPE_VHOST_FDMAN RTE_LOGTYPE_USER1
+
 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
 
 static int
@@ -65,17 +39,12 @@ fdset_move(struct fdset *pfdset, int dst, int src)
        pfdset->rwfds[dst] = pfdset->rwfds[src];
 }
 
-/*
- * Find deleted fd entries and remove them
- */
 static void
-fdset_shrink(struct fdset *pfdset)
+fdset_shrink_nolock(struct fdset *pfdset)
 {
        int i;
        int last_valid_idx = get_last_valid_idx(pfdset, pfdset->num - 1);
 
-       pthread_mutex_lock(&pfdset->fd_mutex);
-
        for (i = 0; i < last_valid_idx; i++) {
                if (pfdset->fd[i].fd != -1)
                        continue;
@@ -84,7 +53,16 @@ fdset_shrink(struct fdset *pfdset)
                last_valid_idx = get_last_valid_idx(pfdset, last_valid_idx - 1);
        }
        pfdset->num = last_valid_idx + 1;
+}
 
+/*
+ * Find deleted fd entries and remove them
+ */
+static void
+fdset_shrink(struct fdset *pfdset)
+{
+       pthread_mutex_lock(&pfdset->fd_mutex);
+       fdset_shrink_nolock(pfdset);
        pthread_mutex_unlock(&pfdset->fd_mutex);
 }
 
@@ -151,8 +129,12 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
        pthread_mutex_lock(&pfdset->fd_mutex);
        i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
        if (i == -1) {
-               pthread_mutex_unlock(&pfdset->fd_mutex);
-               return -2;
+               fdset_shrink_nolock(pfdset);
+               i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
+               if (i == -1) {
+                       pthread_mutex_unlock(&pfdset->fd_mutex);
+                       return -2;
+               }
        }
 
        fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
@@ -192,6 +174,38 @@ fdset_del(struct fdset *pfdset, int fd)
        return dat;
 }
 
+/**
+ *  Unregister the fd from the fdset.
+ *
+ *  If parameters are invalid, return directly -2.
+ *  And check whether fd is busy, if yes, return -1.
+ *  Otherwise, try to delete the fd from fdset and
+ *  return true.
+ */
+int
+fdset_try_del(struct fdset *pfdset, int fd)
+{
+       int i;
+
+       if (pfdset == NULL || fd == -1)
+               return -2;
+
+       pthread_mutex_lock(&pfdset->fd_mutex);
+       i = fdset_find_fd(pfdset, fd);
+       if (i != -1 && pfdset->fd[i].busy) {
+               pthread_mutex_unlock(&pfdset->fd_mutex);
+               return -1;
+       }
+
+       if (i != -1) {
+               pfdset->fd[i].fd = -1;
+               pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
+               pfdset->fd[i].dat = NULL;
+       }
+
+       pthread_mutex_unlock(&pfdset->fd_mutex);
+       return 0;
+}
 
 /**
  * This functions runs in infinite blocking loop until there is no fd in
@@ -202,8 +216,8 @@ fdset_del(struct fdset *pfdset, int fd)
  * will wait until the flag is reset to zero(which indicates the callback is
  * finished), then it could free the context after fdset_del.
  */
-void
-fdset_event_dispatch(struct fdset *pfdset)
+void *
+fdset_event_dispatch(void *arg)
 {
        int i;
        struct pollfd *pfd;
@@ -213,9 +227,11 @@ fdset_event_dispatch(struct fdset *pfdset)
        int fd, numfds;
        int remove1, remove2;
        int need_shrink;
+       struct fdset *pfdset = arg;
+       int val;
 
        if (pfdset == NULL)
-               return;
+               return NULL;
 
        while (1) {
 
@@ -230,7 +246,9 @@ fdset_event_dispatch(struct fdset *pfdset)
                numfds = pfdset->num;
                pthread_mutex_unlock(&pfdset->fd_mutex);
 
-               poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
+               val = poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
+               if (val < 0)
+                       continue;
 
                need_shrink = 0;
                for (i = 0; i < numfds; i++) {
@@ -275,7 +293,7 @@ fdset_event_dispatch(struct fdset *pfdset)
                         * because the fd is closed in the cb,
                         * the old fd val could be reused by when creates new
                         * listen fd in another thread, we couldn't call
-                        * fd_set_del.
+                        * fdset_del.
                         */
                        if (remove1 || remove2) {
                                pfdentry->fd = -1;
@@ -286,4 +304,67 @@ fdset_event_dispatch(struct fdset *pfdset)
                if (need_shrink)
                        fdset_shrink(pfdset);
        }
+
+       return NULL;
+}
+
+static void
+fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
+                  int *remove __rte_unused)
+{
+       char charbuf[16];
+       int r = read(readfd, charbuf, sizeof(charbuf));
+       /*
+        * Just an optimization, we don't care if read() failed
+        * so ignore explicitly its return value to make the
+        * compiler happy
+        */
+       RTE_SET_USED(r);
+}
+
+void
+fdset_pipe_uninit(struct fdset *fdset)
+{
+       fdset_del(fdset, fdset->u.readfd);
+       close(fdset->u.readfd);
+       close(fdset->u.writefd);
+}
+
+int
+fdset_pipe_init(struct fdset *fdset)
+{
+       int ret;
+
+       if (pipe(fdset->u.pipefd) < 0) {
+               RTE_LOG(ERR, VHOST_FDMAN,
+                       "failed to create pipe for vhost fdset\n");
+               return -1;
+       }
+
+       ret = fdset_add(fdset, fdset->u.readfd,
+                       fdset_pipe_read_cb, NULL, NULL);
+
+       if (ret < 0) {
+               RTE_LOG(ERR, VHOST_FDMAN,
+                       "failed to add pipe readfd %d into vhost server fdset\n",
+                       fdset->u.readfd);
+
+               fdset_pipe_uninit(fdset);
+               return -1;
+       }
+
+       return 0;
+}
+
+void
+fdset_pipe_notify(struct fdset *fdset)
+{
+       int r = write(fdset->u.writefd, "1", 1);
+       /*
+        * Just an optimization, we don't care if write() failed
+        * so ignore explicitly its return value to make the
+        * compiler happy
+        */
+       RTE_SET_USED(r);
+
 }