X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvcl%2Fldp.c;h=504155696fb3980b2a30865a36450f2fe4a473e6;hb=a7a1a22673e029d59f52422263076aaaab81a046;hp=beaa988185d8d9021c4e87c97543a4f86f3afa5a;hpb=173bae3d178d96cd14e0cc1b191a575c4aaff299;p=vpp.git diff --git a/src/vcl/ldp.c b/src/vcl/ldp.c index beaa988185d..504155696fb 100644 --- a/src/vcl/ldp.c +++ b/src/vcl/ldp.c @@ -29,6 +29,9 @@ #include #include #include +#include +#include +#include #define HAVE_CONSTRUCTOR_ATTRIBUTE #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE @@ -46,15 +49,27 @@ #define DESTRUCTOR_ATTRIBUTE #endif -typedef struct +#define LDP_MAX_NWORKERS 32 + +#define LDP_F_SHUT_RD (1 << 0) +#define LDP_F_SHUT_WR (1 << 1) + +typedef struct ldp_fd_entry_ +{ + u32 session_index; + u32 fd; + u32 fd_index; + u32 flags; +} ldp_fd_entry_t; + +typedef struct ldp_worker_ctx_ { - int init; - char app_name[LDP_APP_NAME_MAX]; - u32 sid_bit_val; - u32 sid_bit_mask; - u32 debug; u8 *io_buffer; clib_time_t clib_time; + + /* + * Select state + */ clib_bitmap_t *rd_bitmap; clib_bitmap_t *wr_bitmap; clib_bitmap_t *ex_bitmap; @@ -64,21 +79,44 @@ typedef struct clib_bitmap_t *libc_rd_bitmap; clib_bitmap_t *libc_wr_bitmap; clib_bitmap_t *libc_ex_bitmap; + u8 select_vcl; + + /* + * Poll state + */ vcl_poll_t *vcl_poll; struct pollfd *libc_poll; u16 *libc_poll_idxs; - u8 select_vcl; + + /* + * Epoll state + */ u8 epoll_wait_vcl; - u8 vcl_needs_real_epoll; /*< vcl needs next epoll_create to - go to libc_epoll */ int vcl_mq_epfd; +} ldp_worker_ctx_t; + +typedef struct +{ + ldp_worker_ctx_t *workers; + int init; + char app_name[LDP_APP_NAME_MAX]; + u32 sid_bit_val; + u32 sid_bit_mask; + u32 debug; + ldp_fd_entry_t *fd_pool; + clib_rwlock_t fd_table_lock; + uword *session_index_to_fd_table; + + /** vcl needs next epoll_create to go to libc_epoll */ + u8 vcl_needs_real_epoll; } ldp_main_t; + #define LDP_DEBUG ldp->debug #define LDBG(_lvl, _fmt, _args...) \ if (ldp->debug > _lvl) \ - clib_warning (_fmt, ##_args) + clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args) static ldp_main_t ldp_main = { .sid_bit_val = (1 << LDP_SID_BIT_MIN), @@ -88,6 +126,12 @@ static ldp_main_t ldp_main = { static ldp_main_t *ldp = &ldp_main; +static inline ldp_worker_ctx_t * +ldp_worker_get_current (void) +{ + return (ldp->workers + vppcom_worker_index ()); +} + /* * RETURN: 0 on success or -1 on error. * */ @@ -110,31 +154,104 @@ ldp_get_app_name () return ldp->app_name; } +static int +ldp_fd_alloc (u32 sid) +{ + ldp_fd_entry_t *fde; + + clib_rwlock_writer_lock (&ldp->fd_table_lock); + if (pool_elts (ldp->fd_pool) >= (1ULL << 32) - ldp->sid_bit_val) + { + clib_rwlock_writer_unlock (&ldp->fd_table_lock); + return -1; + } + pool_get (ldp->fd_pool, fde); + fde->session_index = vppcom_session_index (sid); + fde->fd_index = fde - ldp->fd_pool; + fde->fd = fde->fd_index + ldp->sid_bit_val; + hash_set (ldp->session_index_to_fd_table, fde->session_index, fde->fd); + clib_rwlock_writer_unlock (&ldp->fd_table_lock); + return fde->fd; +} + +static ldp_fd_entry_t * +ldp_fd_entry_get_w_lock (u32 fd_index) +{ + clib_rwlock_reader_lock (&ldp->fd_table_lock); + if (pool_is_free_index (ldp->fd_pool, fd_index)) + return 0; + + return pool_elt_at_index (ldp->fd_pool, fd_index); +} + static inline int ldp_fd_from_sid (u32 sid) { - if (PREDICT_FALSE (sid >= ldp->sid_bit_val)) - return -EMFILE; - else - return (sid | ldp->sid_bit_val); + uword *fdp; + int fd; + + clib_rwlock_reader_lock (&ldp->fd_table_lock); + fdp = hash_get (ldp->session_index_to_fd_table, vppcom_session_index (sid)); + fd = fdp ? *fdp : -EMFILE; + clib_rwlock_reader_unlock (&ldp->fd_table_lock); + + return fd; } static inline int ldp_fd_is_sid (int fd) { - return ((u32) fd & ldp->sid_bit_val) ? 1 : 0; + return fd >= ldp->sid_bit_val; } static inline u32 ldp_sid_from_fd (int fd) { - return (ldp_fd_is_sid (fd) ? ((u32) fd & ldp->sid_bit_mask) : - INVALID_SESSION_ID); + u32 fd_index, session_index; + ldp_fd_entry_t *fde; + + if (!ldp_fd_is_sid (fd)) + return INVALID_SESSION_ID; + + fd_index = fd - ldp->sid_bit_val; + fde = ldp_fd_entry_get_w_lock (fd_index); + if (!fde) + { + LDBG (0, "unknown fd %d", fd); + clib_rwlock_reader_unlock (&ldp->fd_table_lock); + return INVALID_SESSION_ID; + } + session_index = fde->session_index; + clib_rwlock_reader_unlock (&ldp->fd_table_lock); + + return vppcom_session_handle (session_index); +} + +static void +ldp_fd_free_w_sid (u32 sid) +{ + ldp_fd_entry_t *fde; + u32 fd_index; + int fd; + + fd = ldp_fd_from_sid (sid); + if (!fd) + return; + + fd_index = fd - ldp->sid_bit_val; + fde = ldp_fd_entry_get_w_lock (fd_index); + if (fde) + { + hash_unset (ldp->session_index_to_fd_table, fde->session_index); + pool_put (ldp->fd_pool, fde); + } + clib_rwlock_writer_unlock (&ldp->fd_table_lock); } static inline int ldp_init (void) { + ldp_worker_ctx_t *ldpw; int rv; if (PREDICT_TRUE (ldp->init)) @@ -145,13 +262,17 @@ ldp_init (void) rv = vppcom_app_create (ldp_get_app_name ()); if (rv != VPPCOM_OK) { - fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()" - " failed! rv = %d (%s)\n", - getpid (), rv, vppcom_retval_str (rv)); + ldp->vcl_needs_real_epoll = 0; + if (rv == VPPCOM_EEXIST) + return 0; + LDBG (2, "\nERROR: ldp_init: vppcom_app_create()" + " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv)); ldp->init = 0; return rv; } ldp->vcl_needs_real_epoll = 0; + pool_alloc (ldp->workers, LDP_MAX_NWORKERS); + ldpw = ldp_worker_get_current (); char *env_var_str = getenv (LDP_ENV_DEBUG); if (env_var_str) @@ -164,8 +285,8 @@ ldp_init (void) else { ldp->debug = tmp; - LDBG (0, "LDP<%d>: configured LDP debug level (%u) from env var " - LDP_ENV_DEBUG "!", getpid (), ldp->debug); + LDBG (0, "configured LDP debug level (%u) from env var " + LDP_ENV_DEBUG "!", ldp->debug); } } @@ -173,8 +294,8 @@ ldp_init (void) if (env_var_str) { ldp_set_app_name (env_var_str); - LDBG (0, "LDP<%d>: configured LDP app name (%s) from the env var " - LDP_ENV_APP_NAME "!", getpid (), ldp->app_name); + LDBG (0, "configured LDP app name (%s) from the env var " + LDP_ENV_APP_NAME "!", ldp->app_name); } env_var_str = getenv (LDP_ENV_SID_BIT); @@ -215,14 +336,15 @@ ldp_init (void) ldp->sid_bit_val = (1 << sb); ldp->sid_bit_mask = ldp->sid_bit_val - 1; - LDBG (0, "LDP<%d>: configured LDP sid bit (%u) from " - LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", getpid (), sb, + LDBG (0, "configured LDP sid bit (%u) from " + LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb, ldp->sid_bit_val, ldp->sid_bit_val); } } - clib_time_init (&ldp->clib_time); - LDBG (0, "LDP<%d>: LDP initialization: done!", getpid ()); + clib_time_init (&ldpw->clib_time); + clib_rwlock_init (&ldp->fd_table_lock); + LDBG (0, "LDP initialization: done!"); return 0; } @@ -230,8 +352,7 @@ ldp_init (void) int close (int fd) { - int rv; - const char *func_str; + int rv, refcnt; u32 sid = ldp_sid_from_fd (fd); if ((errno = -ldp_init ())) @@ -241,14 +362,11 @@ close (int fd) { int epfd; - func_str = "vppcom_session_attr[GET_LIBC_EPFD]"; epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); if (epfd > 0) { - func_str = "libc_close"; - - LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): epfd %u (0x%x)", - getpid (), fd, fd, func_str, epfd, epfd); + LDBG (0, "fd %d (0x%x): calling libc_close: epfd %u (0x%x)", + fd, fd, epfd, epfd); rv = libc_close (epfd); if (rv < 0) @@ -267,44 +385,28 @@ close (int fd) goto done; } - func_str = "vppcom_session_close"; - - LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)", - getpid (), fd, fd, func_str, sid, sid); + LDBG (0, "fd %d (0x%x): calling vppcom_session_close: sid %u (0x%x)", + fd, fd, sid, sid); + refcnt = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REFCNT, 0, 0); rv = vppcom_session_close (sid); if (rv != VPPCOM_OK) { errno = -rv; rv = -1; } + if (refcnt <= 1) + ldp_fd_free_w_sid (sid); } else { - func_str = "libc_close"; - - LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s()", getpid (), fd, fd, - func_str); - + LDBG (0, "fd %d (0x%x): calling libc_close", fd, fd); rv = libc_close (fd); } done: - if (LDP_DEBUG > 0) - { - if (rv < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, rv, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, rv, rv); - } + + LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv); return rv; } @@ -312,7 +414,6 @@ ssize_t read (int fd, void *buf, size_t nbytes) { ssize_t size; - const char *func_str; u32 sid = ldp_sid_from_fd (fd); if ((errno = -ldp_init ())) @@ -320,12 +421,8 @@ read (int fd, void *buf, size_t nbytes) if (sid != INVALID_SESSION_ID) { - func_str = "vppcom_session_read"; - - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "sid %u (0x%x), buf %p, nbytes %u", getpid (), - fd, fd, func_str, sid, sid, buf, nbytes); + LDBG (2, "fd %d (0x%x): calling vppcom_session_read(): sid %u (0x%x)," + " buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes); size = vppcom_session_read (sid, buf, nbytes); if (size < 0) @@ -336,38 +433,19 @@ read (int fd, void *buf, size_t nbytes) } else { - func_str = "libc_read"; - - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "buf %p, nbytes %u", getpid (), - fd, fd, func_str, buf, nbytes); + LDBG (2, "fd %d (0x%x): calling libc_read(): buf %p, nbytes %u", + fd, fd, buf, nbytes); size = libc_read (fd, buf, nbytes); } - if (LDP_DEBUG > 2) - { - if (size < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, size, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, size, size); - } + LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size); return size; } ssize_t readv (int fd, const struct iovec * iov, int iovcnt) { - const char *func_str; ssize_t size = 0; u32 sid = ldp_sid_from_fd (fd); int rv = 0, i, total = 0; @@ -377,16 +455,13 @@ readv (int fd, const struct iovec * iov, int iovcnt) if (sid != INVALID_SESSION_ID) { - func_str = "vppcom_session_read"; do { for (i = 0; i < iovcnt; ++i) { - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s() [%d]: " - "sid %u (0x%x), iov %p, iovcnt %d, total %d", - getpid (), fd, fd, func_str, i, sid, sid, - iov, iovcnt, total); + LDBG (2, "fd %d (0x%x): calling vppcom_session_read() [%d]:" + " sid %u (0x%x), iov %p, iovcnt %d, total %d", fd, fd, i, + sid, sid, iov, iovcnt, total); rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len); if (rv < 0) @@ -396,11 +471,8 @@ readv (int fd, const struct iovec * iov, int iovcnt) total += rv; if (rv < iov[i].iov_len) { - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): " - "rv (%d) < iov[%d].iov_len (%d)", - getpid (), fd, fd, rv, i, - iov[i].iov_len); + LDBG (2, "fd %d (0x%x): rv (%d) < iov[%d].iov_len (%d)", + fd, fd, rv, i, iov[i].iov_len); break; } } @@ -418,37 +490,20 @@ readv (int fd, const struct iovec * iov, int iovcnt) } else { - func_str = "libc_readv"; - - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt); + LDBG (2, "fd %d (0x%x): calling libc_readv(): iov %p, iovcnt %d", fd, + fd, iov, iovcnt); size = libc_readv (fd, iov, iovcnt); } - if (LDP_DEBUG > 2) - { - if (size < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, size, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, size, size); - } + + LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size); return size; } ssize_t write (int fd, const void *buf, size_t nbytes) { - const char *func_str; ssize_t size = 0; u32 sid = ldp_sid_from_fd (fd); @@ -457,14 +512,10 @@ write (int fd, const void *buf, size_t nbytes) if (sid != INVALID_SESSION_ID) { - func_str = "vppcom_session_write"; + LDBG (2, "fd %d (0x%x): calling vppcom_session_write(): sid %u (0x%x), " + "buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes); - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "sid %u (0x%x), buf %p, nbytes %u", getpid (), - fd, fd, func_str, sid, sid, buf, nbytes); - - size = vppcom_session_write (sid, (void *) buf, nbytes); + size = vppcom_session_write_msg (sid, (void *) buf, nbytes); if (size < 0) { errno = -size; @@ -473,38 +524,19 @@ write (int fd, const void *buf, size_t nbytes) } else { - func_str = "libc_write"; - - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "buf %p, nbytes %u", getpid (), - fd, fd, func_str, buf, nbytes); + LDBG (2, "fd %d (0x%x): calling libc_write(): buf %p, nbytes %u", + fd, fd, buf, nbytes); size = libc_write (fd, buf, nbytes); } - if (LDP_DEBUG > 2) - { - if (size < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, size, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, size, size); - } + LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size); return size; } ssize_t writev (int fd, const struct iovec * iov, int iovcnt) { - const char *func_str; ssize_t size = 0, total = 0; u32 sid = ldp_sid_from_fd (fd); int i, rv = 0; @@ -518,33 +550,19 @@ writev (int fd, const struct iovec * iov, int iovcnt) if (sid != INVALID_SESSION_ID) { - func_str = "vppcom_session_write"; do { for (i = 0; i < iovcnt; ++i) { - if (LDP_DEBUG > 4) - printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: " - "sid %u (0x%x), buf %p, nbytes %ld, total %ld", - __func__, __LINE__, getpid (), fd, fd, func_str, - i, sid, sid, iov[i].iov_base, iov[i].iov_len, total); - - rv = vppcom_session_write (sid, iov[i].iov_base, - iov[i].iov_len); + rv = vppcom_session_write_msg (sid, iov[i].iov_base, + iov[i].iov_len); if (rv < 0) break; else { total += rv; if (rv < iov[i].iov_len) - { - if (LDP_DEBUG > 4) - printf ("%s:%d: LDP<%d>: fd %d (0x%x): " - "rv (%d) < iov[%d].iov_len (%ld)", - __func__, __LINE__, getpid (), fd, fd, - rv, i, iov[i].iov_len); - break; - } + break; } } } @@ -560,32 +578,9 @@ writev (int fd, const struct iovec * iov, int iovcnt) } else { - func_str = "libc_writev"; - - if (LDP_DEBUG > 4) - printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): " - "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (), - fd, fd, func_str, iov, iovcnt); - size = libc_writev (fd, iov, iovcnt); } - if (LDP_DEBUG > 4) - { - if (size < 0) - { - int errno_val = errno; - perror (func_str); - fprintf (stderr, - "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd, - fd, func_str, size, errno_val); - errno = errno_val; - } - else - printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n", - __func__, __LINE__, getpid (), fd, fd, size); - } return size; } @@ -612,8 +607,8 @@ fcntl (int fd, int cmd, ...) { case F_SETFL: func_str = "vppcom_session_attr[SET_FLAGS]"; - LDBG (2, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) " - "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid, + LDBG (2, "fd %d (0x%x): calling %s(): sid %u (0x%x) " + "flags %d (0x%x), size %d", fd, fd, func_str, sid, sid, flags, flags, size); rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags, @@ -622,16 +617,16 @@ fcntl (int fd, int cmd, ...) case F_GETFL: func_str = "vppcom_session_attr[GET_FLAGS]"; - LDBG (2, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), " - "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid, + LDBG (2, "fd %d (0x%x): calling %s(): sid %u (0x%x), " + "flags %d (0x%x), size %d", fd, fd, func_str, sid, sid, flags, flags, size); rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags, &size); if (rv == VPPCOM_OK) { - LDBG (2, "LDP<%d>: fd %d (0x%x), cmd %d (F_GETFL): %s() " - "returned flags %d (0x%x)", getpid (), fd, fd, cmd, + LDBG (2, "fd %d (0x%x), cmd %d (F_GETFL): %s() " + "returned flags %d (0x%x)", fd, fd, cmd, func_str, flags, flags); rv = flags; } @@ -655,9 +650,7 @@ fcntl (int fd, int cmd, ...) { func_str = "libc_vfcntl"; - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d", - getpid (), fd, fd, func_str, cmd); + LDBG (2, "fd %d (0x%x): calling %s(): cmd %d", fd, fd, func_str, cmd); rv = libc_vfcntl (fd, cmd, ap); } @@ -776,13 +769,12 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, const struct timespec *__restrict timeout, const __sigset_t * __restrict sigmask) { - int rv; + uword sid_bits, sid_bits_set, libc_bits, libc_bits_set; + ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); + u32 minbits = clib_max (nfds, BITS (uword)), sid; char *func_str = "##"; f64 time_out; - int fd; - uword sid_bits, sid_bits_set, libc_bits, libc_bits_set; - u32 minbits = clib_max (nfds, BITS (uword)); - u32 sid; + int rv, fd; if (nfds < 0) { @@ -799,11 +791,10 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, /* select as fine grained sleep */ if (!nfds) { - LDBG (3, "LDP<%d>: sleeping for %.02f seconds", getpid (), - time_out); + LDBG (3, "sleeping for %.02f seconds", time_out); - time_out += clib_time_now (&ldp->clib_time); - while (clib_time_now (&ldp->clib_time) < time_out) + time_out += clib_time_now (&ldpw->clib_time); + while (clib_time_now (&ldpw->clib_time) < time_out) ; return 0; } @@ -821,8 +812,8 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, { func_str = "libc_pselect"; - LDBG (3, "LDP<%d>: calling %s(): nfds %d, readfds %p, writefds %p, " - "exceptfds %p, timeout %p, sigmask %p", getpid (), func_str, nfds, + LDBG (3, "calling %s(): nfds %d, readfds %p, writefds %p, " + "exceptfds %p, timeout %p, sigmask %p", func_str, nfds, readfds, writefds, exceptfds, timeout, sigmask); rv = libc_pselect (nfds, readfds, writefds, exceptfds, @@ -844,98 +835,98 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0); if (readfds) { - clib_bitmap_validate (ldp->sid_rd_bitmap, minbits); - clib_bitmap_validate (ldp->libc_rd_bitmap, minbits); - clib_bitmap_validate (ldp->rd_bitmap, minbits); - clib_memcpy_fast (ldp->rd_bitmap, readfds, n_bytes); + clib_bitmap_validate (ldpw->sid_rd_bitmap, minbits); + clib_bitmap_validate (ldpw->libc_rd_bitmap, minbits); + clib_bitmap_validate (ldpw->rd_bitmap, minbits); + clib_memcpy_fast (ldpw->rd_bitmap, readfds, n_bytes); memset (readfds, 0, n_bytes); /* *INDENT-OFF* */ - clib_bitmap_foreach (fd, ldp->rd_bitmap, ({ + clib_bitmap_foreach (fd, ldpw->rd_bitmap, ({ if (fd > nfds) break; sid = ldp_sid_from_fd (fd); - LDBG (3, "LDP<%d>: readfds: fd %d (0x%x), sid %u (0x%x)", - getpid (), fd, fd, sid, sid); + LDBG (3, "readfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid); if (sid == INVALID_SESSION_ID) - clib_bitmap_set_no_check (ldp->libc_rd_bitmap, fd, 1); + clib_bitmap_set_no_check (ldpw->libc_rd_bitmap, fd, 1); else - clib_bitmap_set_no_check (ldp->sid_rd_bitmap, sid, 1); + clib_bitmap_set_no_check (ldpw->sid_rd_bitmap, + vppcom_session_index (sid), 1); })); /* *INDENT-ON* */ - sid_bits_set = clib_bitmap_last_set (ldp->sid_rd_bitmap) + 1; + sid_bits_set = clib_bitmap_last_set (ldpw->sid_rd_bitmap) + 1; sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits; - libc_bits_set = clib_bitmap_last_set (ldp->libc_rd_bitmap) + 1; + libc_bits_set = clib_bitmap_last_set (ldpw->libc_rd_bitmap) + 1; libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits; - LDBG (3, "LDP<%d>: readfds: sid_bits_set %d, sid_bits %d, " - "libc_bits_set %d, libc_bits %d", getpid (), sid_bits_set, + LDBG (3, "readfds: sid_bits_set %d, sid_bits %d, " + "libc_bits_set %d, libc_bits %d", sid_bits_set, sid_bits, libc_bits_set, libc_bits); } if (writefds) { - clib_bitmap_validate (ldp->sid_wr_bitmap, minbits); - clib_bitmap_validate (ldp->libc_wr_bitmap, minbits); - clib_bitmap_validate (ldp->wr_bitmap, minbits); - clib_memcpy_fast (ldp->wr_bitmap, writefds, n_bytes); + clib_bitmap_validate (ldpw->sid_wr_bitmap, minbits); + clib_bitmap_validate (ldpw->libc_wr_bitmap, minbits); + clib_bitmap_validate (ldpw->wr_bitmap, minbits); + clib_memcpy_fast (ldpw->wr_bitmap, writefds, n_bytes); memset (writefds, 0, n_bytes); /* *INDENT-OFF* */ - clib_bitmap_foreach (fd, ldp->wr_bitmap, ({ + clib_bitmap_foreach (fd, ldpw->wr_bitmap, ({ if (fd > nfds) break; sid = ldp_sid_from_fd (fd); - LDBG (3, "LDP<%d>: writefds: fd %d (0x%x), sid %u (0x%x)", - getpid (), fd, fd, sid, sid); + LDBG (3, "writefds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid); if (sid == INVALID_SESSION_ID) - clib_bitmap_set_no_check (ldp->libc_wr_bitmap, fd, 1); + clib_bitmap_set_no_check (ldpw->libc_wr_bitmap, fd, 1); else - clib_bitmap_set_no_check (ldp->sid_wr_bitmap, sid, 1); + clib_bitmap_set_no_check (ldpw->sid_wr_bitmap, + vppcom_session_index (sid), 1); })); /* *INDENT-ON* */ - sid_bits_set = clib_bitmap_last_set (ldp->sid_wr_bitmap) + 1; + sid_bits_set = clib_bitmap_last_set (ldpw->sid_wr_bitmap) + 1; sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits; - libc_bits_set = clib_bitmap_last_set (ldp->libc_wr_bitmap) + 1; + libc_bits_set = clib_bitmap_last_set (ldpw->libc_wr_bitmap) + 1; libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits; - LDBG (3, "LDP<%d>: writefds: sid_bits_set %d, sid_bits %d, " - "libc_bits_set %d, libc_bits %d", getpid (), + LDBG (3, "writefds: sid_bits_set %d, sid_bits %d, " + "libc_bits_set %d, libc_bits %d", sid_bits_set, sid_bits, libc_bits_set, libc_bits); } if (exceptfds) { - clib_bitmap_validate (ldp->sid_ex_bitmap, minbits); - clib_bitmap_validate (ldp->libc_ex_bitmap, minbits); - clib_bitmap_validate (ldp->ex_bitmap, minbits); - clib_memcpy_fast (ldp->ex_bitmap, exceptfds, n_bytes); + clib_bitmap_validate (ldpw->sid_ex_bitmap, minbits); + clib_bitmap_validate (ldpw->libc_ex_bitmap, minbits); + clib_bitmap_validate (ldpw->ex_bitmap, minbits); + clib_memcpy_fast (ldpw->ex_bitmap, exceptfds, n_bytes); memset (exceptfds, 0, n_bytes); /* *INDENT-OFF* */ - clib_bitmap_foreach (fd, ldp->ex_bitmap, ({ + clib_bitmap_foreach (fd, ldpw->ex_bitmap, ({ if (fd > nfds) break; sid = ldp_sid_from_fd (fd); - LDBG (3, "LDP<%d>: exceptfds: fd %d (0x%x), sid %u (0x%x)", - getpid (), fd, fd, sid, sid); + LDBG (3, "exceptfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid); if (sid == INVALID_SESSION_ID) - clib_bitmap_set_no_check (ldp->libc_ex_bitmap, fd, 1); + clib_bitmap_set_no_check (ldpw->libc_ex_bitmap, fd, 1); else - clib_bitmap_set_no_check (ldp->sid_ex_bitmap, sid, 1); + clib_bitmap_set_no_check (ldpw->sid_ex_bitmap, + vppcom_session_index (sid), 1); })); /* *INDENT-ON* */ - sid_bits_set = clib_bitmap_last_set (ldp->sid_ex_bitmap) + 1; + sid_bits_set = clib_bitmap_last_set (ldpw->sid_ex_bitmap) + 1; sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits; - libc_bits_set = clib_bitmap_last_set (ldp->libc_ex_bitmap) + 1; + libc_bits_set = clib_bitmap_last_set (ldpw->libc_ex_bitmap) + 1; libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits; - LDBG (3, "LDP<%d>: exceptfds: sid_bits_set %d, sid_bits %d, " - "libc_bits_set %d, libc_bits %d", getpid (), + LDBG (3, "exceptfds: sid_bits_set %d, sid_bits %d, " + "libc_bits_set %d, libc_bits %d", sid_bits_set, sid_bits, libc_bits_set, libc_bits); } @@ -950,27 +941,27 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, { if (sid_bits) { - if (!ldp->select_vcl) + if (!ldpw->select_vcl) { func_str = "vppcom_select"; if (readfds) - clib_memcpy_fast (ldp->rd_bitmap, ldp->sid_rd_bitmap, - vec_len (ldp->rd_bitmap) * + clib_memcpy_fast (ldpw->rd_bitmap, ldpw->sid_rd_bitmap, + vec_len (ldpw->rd_bitmap) * sizeof (clib_bitmap_t)); if (writefds) - clib_memcpy_fast (ldp->wr_bitmap, ldp->sid_wr_bitmap, - vec_len (ldp->wr_bitmap) * + clib_memcpy_fast (ldpw->wr_bitmap, ldpw->sid_wr_bitmap, + vec_len (ldpw->wr_bitmap) * sizeof (clib_bitmap_t)); if (exceptfds) - clib_memcpy_fast (ldp->ex_bitmap, ldp->sid_ex_bitmap, - vec_len (ldp->ex_bitmap) * + clib_memcpy_fast (ldpw->ex_bitmap, ldpw->sid_ex_bitmap, + vec_len (ldpw->ex_bitmap) * sizeof (clib_bitmap_t)); rv = vppcom_select (sid_bits, - readfds ? ldp->rd_bitmap : NULL, - writefds ? ldp->wr_bitmap : NULL, - exceptfds ? ldp->ex_bitmap : NULL, 0); + readfds ? ldpw->rd_bitmap : NULL, + writefds ? ldpw->wr_bitmap : NULL, + exceptfds ? ldpw->ex_bitmap : NULL, 0); if (rv < 0) { errno = -rv; @@ -981,9 +972,9 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, if (readfds) { /* *INDENT-OFF* */ - clib_bitmap_foreach (sid, ldp->rd_bitmap, + clib_bitmap_foreach (sid, ldpw->rd_bitmap, ({ - fd = ldp_fd_from_sid (sid); + fd = ldp_fd_from_sid (vppcom_session_handle (sid)); if (PREDICT_FALSE (fd < 0)) { errno = EBADFD; @@ -997,9 +988,9 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, if (writefds) { /* *INDENT-OFF* */ - clib_bitmap_foreach (sid, ldp->wr_bitmap, + clib_bitmap_foreach (sid, ldpw->wr_bitmap, ({ - fd = ldp_fd_from_sid (sid); + fd = ldp_fd_from_sid (vppcom_session_handle (sid)); if (PREDICT_FALSE (fd < 0)) { errno = EBADFD; @@ -1013,9 +1004,9 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, if (exceptfds) { /* *INDENT-OFF* */ - clib_bitmap_foreach (sid, ldp->ex_bitmap, + clib_bitmap_foreach (sid, ldpw->ex_bitmap, ({ - fd = ldp_fd_from_sid (sid); + fd = ldp_fd_from_sid (vppcom_session_handle (sid)); if (PREDICT_FALSE (fd < 0)) { errno = EBADFD; @@ -1026,12 +1017,12 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, })); /* *INDENT-ON* */ } - ldp->select_vcl = 1; + ldpw->select_vcl = 1; goto done; } } else - ldp->select_vcl = 0; + ldpw->select_vcl = 0; } if (libc_bits) { @@ -1040,16 +1031,16 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, func_str = "libc_pselect"; if (readfds) - clib_memcpy_fast (readfds, ldp->libc_rd_bitmap, - vec_len (ldp->rd_bitmap) * + clib_memcpy_fast (readfds, ldpw->libc_rd_bitmap, + vec_len (ldpw->rd_bitmap) * sizeof (clib_bitmap_t)); if (writefds) - clib_memcpy_fast (writefds, ldp->libc_wr_bitmap, - vec_len (ldp->wr_bitmap) * + clib_memcpy_fast (writefds, ldpw->libc_wr_bitmap, + vec_len (ldpw->wr_bitmap) * sizeof (clib_bitmap_t)); if (exceptfds) - clib_memcpy_fast (exceptfds, ldp->libc_ex_bitmap, - vec_len (ldp->ex_bitmap) * + clib_memcpy_fast (exceptfds, ldpw->libc_ex_bitmap, + vec_len (ldpw->ex_bitmap) * sizeof (clib_bitmap_t)); tspec.tv_sec = tspec.tv_nsec = 0; rv = libc_pselect (libc_bits, @@ -1060,20 +1051,20 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, goto done; } } - while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out)); + while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out)); rv = 0; done: /* TBD: set timeout to amount of time left */ - clib_bitmap_zero (ldp->rd_bitmap); - clib_bitmap_zero (ldp->sid_rd_bitmap); - clib_bitmap_zero (ldp->libc_rd_bitmap); - clib_bitmap_zero (ldp->wr_bitmap); - clib_bitmap_zero (ldp->sid_wr_bitmap); - clib_bitmap_zero (ldp->libc_wr_bitmap); - clib_bitmap_zero (ldp->ex_bitmap); - clib_bitmap_zero (ldp->sid_ex_bitmap); - clib_bitmap_zero (ldp->libc_ex_bitmap); + clib_bitmap_zero (ldpw->rd_bitmap); + clib_bitmap_zero (ldpw->sid_rd_bitmap); + clib_bitmap_zero (ldpw->libc_rd_bitmap); + clib_bitmap_zero (ldpw->wr_bitmap); + clib_bitmap_zero (ldpw->sid_wr_bitmap); + clib_bitmap_zero (ldpw->libc_wr_bitmap); + clib_bitmap_zero (ldpw->ex_bitmap); + clib_bitmap_zero (ldpw->sid_ex_bitmap); + clib_bitmap_zero (ldpw->libc_ex_bitmap); if (LDP_DEBUG > 3) { @@ -1140,9 +1131,8 @@ socket (int domain, int type, int protocol) func_str = "vppcom_session_create"; - LDBG (0, "LDP<%d>: : calling %s(): proto %u (%s), is_nonblocking %u", - getpid (), func_str, proto, vppcom_proto_str (proto), - is_nonblocking); + LDBG (0, "calling %s(): proto %u (%s), is_nonblocking %u", + func_str, proto, vppcom_proto_str (proto), is_nonblocking); sid = vppcom_session_create (proto, is_nonblocking); if (sid < 0) @@ -1153,7 +1143,7 @@ socket (int domain, int type, int protocol) else { func_str = "ldp_fd_from_sid"; - rv = ldp_fd_from_sid (sid); + rv = ldp_fd_alloc (sid); if (rv < 0) { (void) vppcom_session_close (sid); @@ -1166,7 +1156,7 @@ socket (int domain, int type, int protocol) { func_str = "libc_socket"; - LDBG (0, "LDP<%d>: : calling %s()", getpid (), func_str); + LDBG (0, "calling %s()", func_str); rv = libc_socket (domain, type, protocol); } @@ -1183,7 +1173,7 @@ socket (int domain, int type, int protocol) errno = errno_val; } else - clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv); + clib_warning ("returning fd %d (0x%x)", getpid (), rv, rv); } return rv; } @@ -1218,7 +1208,7 @@ socketpair (int domain, int type, int protocol, int fds[2]) { func_str = "libc_socket"; - LDBG (1, "LDP<%d>: : calling %s()", getpid (), func_str); + LDBG (1, "calling %s()", func_str); rv = libc_socketpair (domain, type, protocol, fds); } @@ -1244,7 +1234,6 @@ int bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) { int rv; - const char *func_str; u32 sid = ldp_sid_from_fd (fd); if ((errno = -ldp_init ())) @@ -1254,8 +1243,6 @@ bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) { vppcom_endpt_t ep; - func_str = "vppcom_session_bind"; - switch (addr->sa_family) { case AF_INET: @@ -1296,10 +1283,8 @@ bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) rv = -1; goto done; } - if (LDP_DEBUG > 0) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), " - "addr %p, len %u", - getpid (), fd, fd, func_str, sid, sid, addr, len); + LDBG (0, "fd %d (0x%x): calling vppcom_session_bind(): " + "sid %u (0x%x), addr %p, len %u", fd, fd, sid, sid, addr, len); rv = vppcom_session_bind (sid, &ep); if (rv != VPPCOM_OK) @@ -1310,32 +1295,15 @@ bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) } else { - func_str = "libc_bind"; - - if (LDP_DEBUG > 0) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "addr %p, len %u", - getpid (), fd, fd, func_str, addr, len); + LDBG (0, "fd %d (0x%x): calling libc_bind(): addr %p, len %u", + fd, fd, addr, len); rv = libc_bind (fd, addr, len); } done: - if (LDP_DEBUG > 0) - { - if (rv < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, rv, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, rv, rv); - } + LDBG (1, "fd %d (0x%x): returning %d", fd, fd, rv); + return rv; } @@ -1459,7 +1427,6 @@ int connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) { int rv; - const char *func_str = __func__; u32 sid = ldp_sid_from_fd (fd); if ((errno = -ldp_init ())) @@ -1478,8 +1445,6 @@ connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) { vppcom_endpt_t ep; - func_str = "vppcom_session_connect"; - switch (addr->sa_family) { case AF_INET: @@ -1520,10 +1485,8 @@ connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) rv = -1; goto done; } - if (LDP_DEBUG > 0) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) " - "addr %p len %u", - getpid (), fd, fd, func_str, sid, sid, addr, len); + LDBG (0, "fd %d (0x%x): calling vppcom_session_connect(): sid %u (0x%x)" + " addr %p len %u", fd, fd, sid, sid, addr, len); rv = vppcom_session_connect (sid, &ep); if (rv != VPPCOM_OK) @@ -1534,32 +1497,14 @@ connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) } else { - func_str = "libc_connect"; - - if (LDP_DEBUG > 0) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "addr %p, len %u", - getpid (), fd, fd, func_str, addr, len); + LDBG (0, "fd %d (0x%x): calling libc_connect(): addr %p, len %u", + fd, fd, addr, len); rv = libc_connect (fd, addr, len); } done: - if (LDP_DEBUG > 0) - { - if (rv < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, rv, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, rv, rv); - } + LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv); return rv; } @@ -1693,6 +1638,7 @@ send (int fd, const void *buf, size_t n, int flags) ssize_t sendfile (int out_fd, int in_fd, off_t * offset, size_t len) { + ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); ssize_t size = 0; const char *func_str; u32 sid = ldp_sid_from_fd (out_fd); @@ -1721,7 +1667,7 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len) out_fd, out_fd, func_str, sid, sid, rv, vppcom_retval_str (rv)); - vec_reset_length (ldp->io_buffer); + vec_reset_length (ldpw->io_buffer); errno = -rv; size = -1; goto done; @@ -1757,7 +1703,7 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len) ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), " "returned %d (%s)!", getpid (), out_fd, out_fd, func_str, sid, sid, size, vppcom_retval_str (size)); - vec_reset_length (ldp->io_buffer); + vec_reset_length (ldpw->io_buffer); errno = -size; size = -1; goto done; @@ -1789,8 +1735,8 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len) continue; } bytes_to_read = clib_min (n_bytes_left, bytes_to_read); - vec_validate (ldp->io_buffer, bytes_to_read); - nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read); + vec_validate (ldpw->io_buffer, bytes_to_read); + nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read); if (nbytes < 0) { func_str = "libc_read"; @@ -1798,13 +1744,13 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len) clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), " "io_buffer %p, bytes_to_read %lu, rv %d, " "errno %d", getpid (), out_fd, out_fd, func_str, - in_fd, ldp->io_buffer, bytes_to_read, nbytes, + in_fd, ldpw->io_buffer, bytes_to_read, nbytes, errno_val); errno = errno_val; if (results == 0) { - vec_reset_length (ldp->io_buffer); + vec_reset_length (ldpw->io_buffer); size = -1; goto done; } @@ -1815,10 +1761,10 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len) clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), " "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (), - out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes, + out_fd, out_fd, func_str, sid, sid, ldpw->io_buffer, nbytes, results, n_bytes_left); - size = vppcom_session_write (sid, ldp->io_buffer, nbytes); + size = vppcom_session_write (sid, ldpw->io_buffer, nbytes); if (size < 0) { if (size == VPPCOM_EAGAIN) @@ -1844,12 +1790,12 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len) "sid %u, io_buffer %p, nbytes %u " "returned %d (%s)", getpid (), out_fd, out_fd, func_str, - sid, ldp->io_buffer, nbytes, + sid, ldpw->io_buffer, nbytes, size, vppcom_retval_str (size)); } if (results == 0) { - vec_reset_length (ldp->io_buffer); + vec_reset_length (ldpw->io_buffer); errno = -size; size = -1; goto done; @@ -1864,7 +1810,7 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len) while (n_bytes_left > 0); update_offset: - vec_reset_length (ldp->io_buffer); + vec_reset_length (ldpw->io_buffer); if (offset) { off_t off = lseek (in_fd, *offset, SEEK_SET); @@ -1934,55 +1880,29 @@ ssize_t recv (int fd, void *buf, size_t n, int flags) { ssize_t size; - const char *func_str; - u32 sid = ldp_sid_from_fd (fd); + u32 sid; if ((errno = -ldp_init ())) return -1; + sid = ldp_sid_from_fd (fd); if (sid != INVALID_SESSION_ID) { - func_str = "vppcom_session_recvfrom"; - - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (), - fd, fd, func_str, sid, sid, buf, n, flags); + LDBG (2, "fd %d (0x%x): calling vcl recvfrom: sid %u (0x%x), buf %p," + " n %u, flags 0x%x", fd, fd, sid, sid, buf, n, flags); size = vppcom_session_recvfrom (sid, buf, n, flags, NULL); if (size < 0) - { - errno = -size; - size = -1; - } + errno = -size; } else { - func_str = "libc_recv"; - - if (LDP_DEBUG > 2) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " - "buf %p, n %u, flags 0x%x", getpid (), - fd, fd, func_str, buf, n, flags); + LDBG (2, "fd %d (0x%x): calling libc_recvfrom(): buf %p, n %u, " + "flags 0x%x", fd, fd, buf, n, flags); size = libc_recv (fd, buf, n, flags); } - if (LDP_DEBUG > 2) - { - if (size < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, size, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, size, size); - } return size; } @@ -2740,7 +2660,6 @@ int listen (int fd, int n) { int rv; - const char *func_str; u32 sid = ldp_sid_from_fd (fd); if ((errno = -ldp_init ())) @@ -2748,10 +2667,8 @@ listen (int fd, int n) if (sid != INVALID_SESSION_ID) { - func_str = "vppcom_session_listen"; - - LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d", - getpid (), fd, fd, func_str, sid, sid, n); + LDBG (0, "fd %d (0x%x): calling vppcom_session_listen():" + " sid %u (0x%x), n %d", fd, fd, sid, sid, n); rv = vppcom_session_listen (sid, n); if (rv != VPPCOM_OK) @@ -2762,29 +2679,12 @@ listen (int fd, int n) } else { - func_str = "libc_listen"; - - LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): n %d", getpid (), fd, - fd, func_str, n); + LDBG (0, "fd %d (0x%x): calling libc_listen(): n %d", fd, fd, n); rv = libc_listen (fd, n); } - if (LDP_DEBUG > 0) - { - if (rv < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, rv, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, rv, rv); - } + LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv); return rv; } @@ -2793,7 +2693,6 @@ ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len, int flags) { int rv; - const char *func_str; u32 listen_sid = ldp_sid_from_fd (listen_fd); int accept_sid; @@ -2807,13 +2706,9 @@ ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr, memset (&ep, 0, sizeof (ep)); ep.ip = src_addr; - func_str = "vppcom_session_accept"; - - if (LDP_DEBUG > 0) - clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): " - "listen sid %u (0x%x), ep %p, flags 0x%x", - getpid (), listen_fd, listen_fd, func_str, - listen_sid, listen_sid, ep, flags); + LDBG (0, "listen fd %d (0x%x): calling vppcom_session_accept:" + " listen sid %u (0x%x), ep %p, flags 0x%x", listen_fd, + listen_fd, listen_sid, listen_sid, ep, flags); accept_sid = vppcom_session_accept (listen_sid, &ep, flags); if (accept_sid < 0) @@ -2832,13 +2727,7 @@ ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr, } else { - func_str = "ldp_fd_from_sid"; - if (LDP_DEBUG > 0) - clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): " - "accept sid %u (0x%x), ep %p, flags 0x%x", - getpid (), listen_fd, listen_fd, - func_str, accept_sid, accept_sid, ep, flags); - rv = ldp_fd_from_sid ((u32) accept_sid); + rv = ldp_fd_alloc ((u32) accept_sid); if (rv < 0) { (void) vppcom_session_close ((u32) accept_sid); @@ -2850,32 +2739,16 @@ ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr, } else { - func_str = "libc_accept4"; - - if (LDP_DEBUG > 0) - clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): " - "addr %p, addr_len %p, flags 0x%x", - getpid (), listen_fd, listen_fd, func_str, - addr, addr_len, flags); + LDBG (0, "listen fd %d (0x%x): calling libc_accept4(): " + "addr %p, addr_len %p, flags 0x%x", listen_fd, + listen_fd, addr, addr_len, flags); rv = libc_accept4 (listen_fd, addr, addr_len, flags); } - if (LDP_DEBUG > 0) - { - if (rv < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), listen_fd, - listen_fd, func_str, rv, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)", - getpid (), listen_fd, listen_fd, rv, rv); - } + LDBG (1, "listen fd %d (0x%x): returning %d (0x%x)", listen_fd, listen_fd, + rv, rv); + return rv; } @@ -2895,50 +2768,50 @@ accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len) int shutdown (int fd, int how) { - int rv; - const char *func_str; - u32 sid = ldp_sid_from_fd (fd); + int rv = 0; if ((errno = -ldp_init ())) return -1; - if (sid != INVALID_SESSION_ID) + if (ldp_fd_is_sid (fd)) { - func_str = "vppcom_session_close[TODO]"; - rv = close (fd); + u32 fd_index = fd - ldp->sid_bit_val; + ldp_fd_entry_t *fde; + + fde = ldp_fd_entry_get_w_lock (fd_index); + if (!fde) + { + clib_rwlock_reader_unlock (&ldp->fd_table_lock); + errno = ENOTCONN; + return -1; + } + + if (how == SHUT_RD) + fde->flags |= LDP_F_SHUT_RD; + else if (how == SHUT_WR) + fde->flags |= LDP_F_SHUT_WR; + else if (how == SHUT_RDWR) + fde->flags |= (LDP_F_SHUT_RD | LDP_F_SHUT_WR); + + if ((fde->flags & LDP_F_SHUT_RD) && (fde->flags & LDP_F_SHUT_WR)) + rv = close (fd); + + clib_rwlock_reader_unlock (&ldp->fd_table_lock); + LDBG (0, "fd %d (0x%x): calling vcl shutdown: how %d", fd, fd, how); } else { - func_str = "libc_shutdown"; - - if (LDP_DEBUG > 1) - clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d", - getpid (), fd, fd, func_str, how); - + LDBG (1, "fd %d (0x%x): calling libc_shutdown: how %d", fd, fd, how); rv = libc_shutdown (fd, how); } - if (LDP_DEBUG > 1) - { - if (rv < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), fd, fd, - func_str, rv, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", - getpid (), fd, fd, rv, rv); - } return rv; } int epoll_create1 (int flags) { + ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); const char *func_str; int rv; @@ -2949,13 +2822,13 @@ epoll_create1 (int flags) { rv = libc_epoll_create1 (flags); ldp->vcl_needs_real_epoll = 0; - ldp->vcl_mq_epfd = rv; - LDBG (0, "LDP<%d>: created vcl epfd %u", getpid (), rv); + ldpw->vcl_mq_epfd = rv; + LDBG (0, "created vcl epfd %u", rv); return rv; } func_str = "vppcom_epoll_create"; - LDBG (1, "LDP<%d>: calling %s()", getpid (), func_str); + LDBG (1, "calling %s()", func_str); rv = vppcom_epoll_create (); @@ -2965,7 +2838,7 @@ epoll_create1 (int flags) rv = -1; } else - rv = ldp_fd_from_sid ((u32) rv); + rv = ldp_fd_alloc ((u32) rv); if (LDP_DEBUG > 1) { @@ -3009,8 +2882,8 @@ epoll_ctl (int epfd, int op, int fd, struct epoll_event *event) */ func_str = "libc_epoll_ctl"; - LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x)," - " event %p", getpid (), epfd, epfd, func_str, op, fd, fd, event); + LDBG (1, "epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x)," + " event %p", epfd, epfd, func_str, op, fd, fd, event); rv = libc_epoll_ctl (epfd, op, fd, event); goto done; @@ -3018,15 +2891,15 @@ epoll_ctl (int epfd, int op, int fd, struct epoll_event *event) sid = ldp_sid_from_fd (fd); - LDBG (0, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)", - getpid (), epfd, epfd, vep_idx, vep_idx, sid, sid); + LDBG (0, "epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)", + epfd, epfd, vep_idx, vep_idx, sid, sid); if (sid != INVALID_SESSION_ID) { func_str = "vppcom_epoll_ctl"; - LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x)," - " op %d, sid %u (0x%x), event %p", getpid (), epfd, epfd, + LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x)," + " op %d, sid %u (0x%x), event %p", epfd, epfd, func_str, vep_idx, vep_idx, sid, sid, event); rv = vppcom_epoll_ctl (vep_idx, op, sid, event); @@ -3044,16 +2917,16 @@ epoll_ctl (int epfd, int op, int fd, struct epoll_event *event) func_str = "vppcom_session_attr[GET_LIBC_EPFD]"; libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); - LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): %s() " - "returned libc_epfd %d (0x%x)", getpid (), epfd, epfd, + LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): %s() " + "returned libc_epfd %d (0x%x)", epfd, epfd, vep_idx, vep_idx, func_str, libc_epfd, libc_epfd); if (!libc_epfd) { func_str = "libc_epoll_create1"; - LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): " - "calling %s(): EPOLL_CLOEXEC", getpid (), epfd, epfd, + LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): " + "calling %s(): EPOLL_CLOEXEC", epfd, epfd, vep_idx, vep_idx, func_str); libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC); @@ -3064,9 +2937,9 @@ epoll_ctl (int epfd, int op, int fd, struct epoll_event *event) } func_str = "vppcom_session_attr[SET_LIBC_EPFD]"; - LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x)," + LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x)," " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d", - getpid (), epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd, + epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd, libc_epfd, size); rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD, @@ -3087,8 +2960,8 @@ epoll_ctl (int epfd, int op, int fd, struct epoll_event *event) func_str = "libc_epoll_ctl"; - LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), " - "op %d, fd %d (0x%x), event %p", getpid (), epfd, epfd, func_str, + LDBG (1, "epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), " + "op %d, fd %d (0x%x), event %p", epfd, epfd, func_str, libc_epfd, libc_epfd, op, fd, fd, event); rv = libc_epoll_ctl (libc_epfd, op, fd, event); @@ -3117,10 +2990,10 @@ static inline int ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t * sigmask) { + ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); double time_to_wait = (double) 0, time_out, now = 0; u32 vep_idx = ldp_sid_from_fd (epfd); int libc_epfd, rv = 0; - const char *func_str; if ((errno = -ldp_init ())) return -1; @@ -3131,21 +3004,20 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, return -1; } - if (epfd == ldp->vcl_mq_epfd) + if (epfd == ldpw->vcl_mq_epfd) return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask); if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID)) { - clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!", - getpid (), epfd, epfd, vep_idx, vep_idx); + LDBG (0, "epfd %d (0x%x): bad vep_idx %d (0x%x)!", epfd, epfd, vep_idx, + vep_idx); errno = EBADFD; return -1; } - time_to_wait = ((timeout >= 0) ? (double) timeout : 0); - time_out = clib_time_now (&ldp->clib_time) + time_to_wait; + time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0); + time_out = clib_time_now (&ldpw->clib_time) + time_to_wait; - func_str = "vppcom_session_attr[GET_LIBC_EPFD]"; libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); if (PREDICT_FALSE (libc_epfd < 0)) { @@ -3154,24 +3026,22 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, goto done; } - LDBG (2, "LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), " + LDBG (2, "epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), " "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f", - getpid (), epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events, + epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events, maxevents, timeout, sigmask, time_to_wait, time_out); do { - if (!ldp->epoll_wait_vcl) + if (!ldpw->epoll_wait_vcl) { - func_str = "vppcom_epoll_wait"; - - LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x)," - " events %p, maxevents %d", getpid (), epfd, epfd, func_str, - vep_idx, vep_idx, events, maxevents); + LDBG (3, "epfd %d (0x%x): calling vcl_epoll_wait: vep_idx %d (0x%x)" + " events %p, maxevents %d", epfd, epfd, vep_idx, vep_idx, + events, maxevents); rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0); if (rv > 0) { - ldp->epoll_wait_vcl = 1; + ldpw->epoll_wait_vcl = 1; goto done; } else if (rv < 0) @@ -3182,45 +3052,25 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, } } else - ldp->epoll_wait_vcl = 0; + ldpw->epoll_wait_vcl = 0; if (libc_epfd > 0) { - func_str = "libc_epoll_pwait"; - - LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d " - "(0x%x), events %p, maxevents %d, sigmask %p", getpid (), - epfd, epfd, func_str, libc_epfd, libc_epfd, events, - maxevents, sigmask); + LDBG (3, "epfd %d (0x%x): calling libc_epoll_wait: libc_epfd %d " + "(0x%x), events %p, maxevents %d, sigmask %p", epfd, epfd, + libc_epfd, libc_epfd, events, maxevents, sigmask); - rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask); + rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask); if (rv != 0) goto done; } if (timeout != -1) - now = clib_time_now (&ldp->clib_time); + now = clib_time_now (&ldpw->clib_time); } while (now < time_out); done: - if (LDP_DEBUG > 3) - { - if (libc_epfd > 0) - epfd = libc_epfd; - if (rv < 0) - { - int errno_val = errno; - perror (func_str); - clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! " - "rv %d, errno = %d", getpid (), epfd, epfd, - func_str, rv, errno_val); - errno = errno_val; - } - else - clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)", - getpid (), epfd, epfd, rv, rv); - } return rv; } @@ -3240,14 +3090,14 @@ epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout) int poll (struct pollfd *fds, nfds_t nfds, int timeout) { + ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); const char *func_str = __func__; int rv, i, n_revents = 0; u32 sid; vcl_poll_t *vp; double wait_for_time; - LDBG (3, "LDP<%d>: fds %p, nfds %d, timeout %d", getpid (), fds, nfds, - timeout); + LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout); if (timeout >= 0) wait_for_time = (f64) timeout / 1000; @@ -3259,15 +3109,14 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout) if (fds[i].fd < 0) continue; - LDBG (3, "LDP<%d>: fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x", - getpid (), i, fds[i].fd, fds[i].fd, fds[i].events, - fds[i].revents); + LDBG (3, "fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x", + i, fds[i].fd, fds[i].fd, fds[i].events, fds[i].revents); sid = ldp_sid_from_fd (fds[i].fd); if (sid != INVALID_SESSION_ID) { fds[i].fd = -fds[i].fd; - vec_add2 (ldp->vcl_poll, vp, 1); + vec_add2 (ldpw->vcl_poll, vp, 1); vp->fds_ndx = i; vp->sid = sid; vp->events = fds[i].events; @@ -3281,23 +3130,23 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout) } else { - vec_add1 (ldp->libc_poll, fds[i]); - vec_add1 (ldp->libc_poll_idxs, i); + vec_add1 (ldpw->libc_poll, fds[i]); + vec_add1 (ldpw->libc_poll_idxs, i); } } do { - if (vec_len (ldp->vcl_poll)) + if (vec_len (ldpw->vcl_poll)) { func_str = "vppcom_poll"; - LDBG (3, "LDP<%d>: calling %s(): vcl_poll %p, n_sids %u (0x%x): " - "n_libc_fds %u", getpid (), func_str, ldp->vcl_poll, - vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll), - vec_len (ldp->libc_poll)); + LDBG (3, "calling %s(): vcl_poll %p, n_sids %u (0x%x): " + "n_libc_fds %u", func_str, ldpw->vcl_poll, + vec_len (ldpw->vcl_poll), vec_len (ldpw->vcl_poll), + vec_len (ldpw->libc_poll)); - rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0); + rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0); if (rv < 0) { errno = -rv; @@ -3308,14 +3157,14 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout) n_revents += rv; } - if (vec_len (ldp->libc_poll)) + if (vec_len (ldpw->libc_poll)) { func_str = "libc_poll"; - LDBG (3, "LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u", - getpid (), fds, nfds, vec_len (ldp->vcl_poll)); + LDBG (3, "calling %s(): fds %p, nfds %u: n_sids %u", + fds, nfds, vec_len (ldpw->vcl_poll)); - rv = libc_poll (ldp->libc_poll, vec_len (ldp->libc_poll), 0); + rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0); if (rv < 0) goto done; else @@ -3329,11 +3178,11 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout) } } while ((wait_for_time == -1) || - (clib_time_now (&ldp->clib_time) < wait_for_time)); + (clib_time_now (&ldpw->clib_time) < wait_for_time)); rv = 0; done: - vec_foreach (vp, ldp->vcl_poll) + vec_foreach (vp, ldpw->vcl_poll) { fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd; fds[vp->fds_ndx].revents = vp->revents; @@ -3346,14 +3195,14 @@ done: fds[vp->fds_ndx].revents |= POLLWRNORM; #endif } - vec_reset_length (ldp->vcl_poll); + vec_reset_length (ldpw->vcl_poll); - for (i = 0; i < vec_len (ldp->libc_poll); i++) + for (i = 0; i < vec_len (ldpw->libc_poll); i++) { - fds[ldp->libc_poll_idxs[i]].revents = ldp->libc_poll[i].revents; + fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents; } - vec_reset_length (ldp->libc_poll_idxs); - vec_reset_length (ldp->libc_poll); + vec_reset_length (ldpw->libc_poll_idxs); + vec_reset_length (ldpw->libc_poll); if (LDP_DEBUG > 3) { @@ -3370,7 +3219,7 @@ done: { clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, " "n_libc_fds %d", getpid (), rv, rv, - vec_len (ldp->vcl_poll), vec_len (ldp->libc_poll)); + vec_len (ldpw->vcl_poll), vec_len (ldpw->libc_poll)); for (i = 0; i < nfds; i++) { @@ -3431,10 +3280,7 @@ ldp_destructor (void) { swrap_destructor (); if (ldp->init) - { - vppcom_app_destroy (); - ldp->init = 0; - } + ldp->init = 0; /* Don't use clib_warning() here because that calls writev() * which will call ldp_init().