X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvcl%2Fldp.c;h=95c0edcff0ab4b6a4a1c4272d1b8181516996388;hb=b4e5e50fe;hp=1c038b0a10230419b725900af41a81675650155f;hpb=7baeb71f92826ee6ef02ab2c3d16484bb64d9301;p=vpp.git diff --git a/src/vcl/ldp.c b/src/vcl/ldp.c index 1c038b0a102..95c0edcff0a 100644 --- a/src/vcl/ldp.c +++ b/src/vcl/ldp.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Cisco and/or its affiliates. + * Copyright (c) 2016-2019 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -62,13 +62,12 @@ typedef struct ldp_worker_ctx_ clib_bitmap_t *rd_bitmap; clib_bitmap_t *wr_bitmap; clib_bitmap_t *ex_bitmap; - clib_bitmap_t *sid_rd_bitmap; - clib_bitmap_t *sid_wr_bitmap; - clib_bitmap_t *sid_ex_bitmap; + clib_bitmap_t *si_rd_bitmap; + clib_bitmap_t *si_wr_bitmap; + clib_bitmap_t *si_ex_bitmap; clib_bitmap_t *libc_rd_bitmap; clib_bitmap_t *libc_wr_bitmap; clib_bitmap_t *libc_ex_bitmap; - u8 select_vcl; /* * Poll state @@ -85,6 +84,13 @@ typedef struct ldp_worker_ctx_ } ldp_worker_ctx_t; +/* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure + * they are the same size */ +STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask), + "ldp bitmap size mismatch"); +STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask), + "ldp bitmap size mismatch"); + typedef struct { ldp_worker_ctx_t *workers; @@ -93,6 +99,7 @@ typedef struct u32 vlsh_bit_val; u32 vlsh_bit_mask; u32 debug; + u8 transparent_tls; /** vcl needs next epoll_create to go to libc_epoll */ u8 vcl_needs_real_epoll; @@ -102,12 +109,17 @@ typedef struct #define LDBG(_lvl, _fmt, _args...) \ if (ldp->debug > _lvl) \ - clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args) + { \ + int errno_saved = errno; \ + clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args); \ + errno = errno_saved; \ + } static ldp_main_t ldp_main = { .vlsh_bit_val = (1 << LDP_SID_BIT_MIN), .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1, .debug = LDP_DEBUG_INIT, + .transparent_tls = 0, }; static ldp_main_t *ldp = &ldp_main; @@ -124,11 +136,8 @@ ldp_worker_get_current (void) static inline void ldp_set_app_name (char *app_name) { - int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX, - "ldp-%d-%s", getpid (), app_name); - - if (rv >= LDP_APP_NAME_MAX) - app_name[LDP_APP_NAME_MAX - 1] = 0; + snprintf (ldp->app_name, LDP_APP_NAME_MAX, + "ldp-%d-%s", getpid (), app_name); } static inline char * @@ -155,6 +164,14 @@ ldp_fd_to_vlsh (int fd) return (fd - ldp->vlsh_bit_val); } +static void +ldp_alloc_workers (void) +{ + if (ldp->workers) + return; + pool_alloc (ldp->workers, LDP_MAX_NWORKERS); +} + static inline int ldp_init (void) { @@ -178,7 +195,7 @@ ldp_init (void) return rv; } ldp->vcl_needs_real_epoll = 0; - pool_alloc (ldp->workers, LDP_MAX_NWORKERS); + ldp_alloc_workers (); ldpw = ldp_worker_get_current (); char *env_var_str = getenv (LDP_ENV_DEBUG); @@ -211,32 +228,29 @@ ldp_init (void) u32 sb; if (sscanf (env_var_str, "%u", &sb) != 1) { - clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in" - " the env var " LDP_ENV_SID_BIT " (%s)! sid bit " - "value %d (0x%x)", getpid (), env_var_str, - ldp->vlsh_bit_val, ldp->vlsh_bit_val); + LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var " + LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str, + ldp->vlsh_bit_val, ldp->vlsh_bit_val); } else if (sb < LDP_SID_BIT_MIN) { ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN); ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1; - clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the" - " env var " LDP_ENV_SID_BIT " (%s) is too small. " - "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)", - getpid (), sb, env_var_str, LDP_SID_BIT_MIN, - ldp->vlsh_bit_val, ldp->vlsh_bit_val); + LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var " + LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN" + " (%d)! sid bit value %d (0x%x)", sb, env_var_str, + LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val); } else if (sb > LDP_SID_BIT_MAX) { ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX); ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1; - clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the" - " env var " LDP_ENV_SID_BIT " (%s) is too big. Using" - " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)", - getpid (), sb, env_var_str, LDP_SID_BIT_MAX, - ldp->vlsh_bit_val, ldp->vlsh_bit_val); + LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var " + LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX" + " (%d)! sid bit value %d (0x%x)", sb, env_var_str, + LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val); } else { @@ -247,9 +261,28 @@ ldp_init (void) LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb, ldp->vlsh_bit_val, ldp->vlsh_bit_val); } + + /* Make sure there are enough bits in the fd set for vcl sessions */ + if (ldp->vlsh_bit_val > FD_SETSIZE / 2) + { + LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!", + ldp->vlsh_bit_val, FD_SETSIZE / 2); + ldp->init = 0; + return -1; + } + } + env_var_str = getenv (LDP_ENV_TLS_TRANS); + if (env_var_str) + { + ldp->transparent_tls = 1; } - clib_time_init (&ldpw->clib_time); + /* *INDENT-OFF* */ + pool_foreach (ldpw, ldp->workers, ({ + clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time)); + })); + /* *INDENT-ON* */ + LDBG (0, "LDP initialization: done!"); return 0; @@ -347,24 +380,19 @@ readv (int fd, const struct iovec * iov, int iovcnt) vlsh = ldp_fd_to_vlsh (fd); if (vlsh != VLS_INVALID_HANDLE) { - do + for (i = 0; i < iovcnt; ++i) { - for (i = 0; i < iovcnt; ++i) + rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len); + if (rv <= 0) + break; + else { - rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len); - if (rv < 0) + total += rv; + if (rv < iov[i].iov_len) break; - else - { - total += rv; - if (rv < iov[i].iov_len) - break; - } } } - while ((rv >= 0) && (total == 0)); - - if (rv < 0) + if (rv < 0 && total == 0) { errno = -rv; size = -1; @@ -453,8 +481,13 @@ writev (int fd, const struct iovec * iov, int iovcnt) return size; } +#ifdef HAVE_FCNTL64 +int +fcntl64 (int fd, int cmd, ...) +#else int fcntl (int fd, int cmd, ...) +#endif { vls_handle_t vlsh; int rv = 0; @@ -502,7 +535,11 @@ fcntl (int fd, int cmd, ...) } else { +#ifdef HAVE_FCNTL64 + rv = libc_vfcntl64 (fd, cmd, ap); +#else rv = libc_vfcntl (fd, cmd, ap); +#endif } va_end (ap); @@ -563,6 +600,83 @@ ioctl (int fd, unsigned long int cmd, ...) return rv; } +always_inline void +ldp_select_init_maps (fd_set * __restrict original, + clib_bitmap_t ** resultb, clib_bitmap_t ** libcb, + clib_bitmap_t ** vclb, int nfds, u32 minbits, + u32 n_bytes, uword * si_bits, uword * libc_bits) +{ + uword si_bits_set, libc_bits_set; + vls_handle_t vlsh; + int fd; + + clib_bitmap_validate (*vclb, minbits); + clib_bitmap_validate (*libcb, minbits); + clib_bitmap_validate (*resultb, minbits); + clib_memcpy_fast (*resultb, original, n_bytes); + memset (original, 0, n_bytes); + + /* *INDENT-OFF* */ + clib_bitmap_foreach (fd, *resultb, ({ + if (fd > nfds) + break; + vlsh = ldp_fd_to_vlsh (fd); + if (vlsh == VLS_INVALID_HANDLE) + clib_bitmap_set_no_check (*libcb, fd, 1); + else + clib_bitmap_set_no_check (*vclb, vlsh_to_session_index (vlsh), 1); + })); + /* *INDENT-ON* */ + + si_bits_set = clib_bitmap_last_set (*vclb) + 1; + *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits; + + libc_bits_set = clib_bitmap_last_set (*libcb) + 1; + *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits; +} + +always_inline int +ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb) +{ + vls_handle_t vlsh; + uword si; + int fd; + + if (!libcb) + return 0; + + /* *INDENT-OFF* */ + clib_bitmap_foreach (si, vclb, ({ + vlsh = vls_session_index_to_vlsh (si); + ASSERT (vlsh != VLS_INVALID_HANDLE); + fd = ldp_vlsh_to_fd (vlsh); + if (PREDICT_FALSE (fd < 0)) + { + errno = EBADFD; + return -1; + } + FD_SET (fd, libcb); + })); + /* *INDENT-ON* */ + + return 0; +} + +always_inline void +ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb) +{ + uword fd; + + if (!libcb) + return; + + /* *INDENT-OFF* */ + clib_bitmap_foreach (fd, result, ({ + FD_SET ((int)fd, libcb); + })); + /* *INDENT-ON* */ +} + int ldp_pselect (int nfds, fd_set * __restrict readfds, fd_set * __restrict writefds, @@ -570,12 +684,12 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, const struct timespec *__restrict timeout, const __sigset_t * __restrict sigmask) { - uword sid_bits, sid_bits_set, libc_bits, libc_bits_set; + u32 minbits = clib_max (nfds, BITS (uword)), n_bytes; ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); - u32 minbits = clib_max (nfds, BITS (uword)), si; - vls_handle_t vlsh; - f64 time_out; - int rv, fd; + struct timespec libc_tspec = { 0 }; + f64 time_out, vcl_timeout = 0; + uword si_bits, libc_bits; + int rv, bits_set = 0; if (nfds < 0) { @@ -583,6 +697,9 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, return -1; } + if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) + clib_time_init (&ldpw->clib_time); + if (timeout) { time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ? @@ -605,7 +722,6 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, else time_out = -1; - if (nfds <= ldp->vlsh_bit_val) { rv = libc_pselect (nfds, readfds, writefds, exceptfds, @@ -613,218 +729,112 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, goto done; } - if (PREDICT_FALSE (ldp->vlsh_bit_val > FD_SETSIZE / 2)) - { - LDBG (0, "ERROR: LDP sid bit value %d > FD_SETSIZE/2 %d!", - ldp->vlsh_bit_val, FD_SETSIZE / 2); - errno = EOVERFLOW; - return -1; - } - - sid_bits = libc_bits = 0; - u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0); + si_bits = libc_bits = 0; + n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0); if (readfds) - { - 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, ldpw->rd_bitmap, ({ - if (fd > nfds) - break; - vlsh = ldp_fd_to_vlsh (fd); - if (vlsh == VLS_INVALID_HANDLE) - clib_bitmap_set_no_check (ldpw->libc_rd_bitmap, fd, 1); - else - clib_bitmap_set_no_check (ldpw->sid_rd_bitmap, - vlsh_to_session_index (vlsh), 1); - })); - /* *INDENT-ON* */ - - 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 (ldpw->libc_rd_bitmap) + 1; - libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits; - } + ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap, + &ldpw->si_rd_bitmap, nfds, minbits, n_bytes, + &si_bits, &libc_bits); if (writefds) - { - 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, ldpw->wr_bitmap, ({ - if (fd > nfds) - break; - vlsh = ldp_fd_to_vlsh (fd); - if (vlsh == VLS_INVALID_HANDLE) - clib_bitmap_set_no_check (ldpw->libc_wr_bitmap, fd, 1); - else - clib_bitmap_set_no_check (ldpw->sid_wr_bitmap, - vlsh_to_session_index (vlsh), 1); - })); - /* *INDENT-ON* */ - - 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 (ldpw->libc_wr_bitmap) + 1; - libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits; - - } + ldp_select_init_maps (writefds, &ldpw->wr_bitmap, + &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds, + minbits, n_bytes, &si_bits, &libc_bits); if (exceptfds) - { - 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, ldpw->ex_bitmap, ({ - if (fd > nfds) - break; - vlsh = ldp_fd_to_vlsh (fd); - if (vlsh == VLS_INVALID_HANDLE) - clib_bitmap_set_no_check (ldpw->libc_ex_bitmap, fd, 1); - else - clib_bitmap_set_no_check (ldpw->sid_ex_bitmap, - vlsh_to_session_index (vlsh), 1); - })); - /* *INDENT-ON* */ - - 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 (ldpw->libc_ex_bitmap) + 1; - libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits; - } + ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap, + &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds, + minbits, n_bytes, &si_bits, &libc_bits); - if (PREDICT_FALSE (!sid_bits && !libc_bits)) + if (PREDICT_FALSE (!si_bits && !libc_bits)) { errno = EINVAL; rv = -1; goto done; } + if (!si_bits) + libc_tspec = timeout ? *timeout : libc_tspec; + do { - if (sid_bits) + if (si_bits) { - if (!ldpw->select_vcl) + if (readfds) + clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap, + vec_len (ldpw->rd_bitmap) * + sizeof (clib_bitmap_t)); + if (writefds) + clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap, + vec_len (ldpw->wr_bitmap) * + sizeof (clib_bitmap_t)); + if (exceptfds) + clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap, + vec_len (ldpw->ex_bitmap) * + sizeof (clib_bitmap_t)); + + rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL, + writefds ? ldpw->wr_bitmap : NULL, + exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout); + if (rv < 0) { - if (readfds) - clib_memcpy_fast (ldpw->rd_bitmap, ldpw->sid_rd_bitmap, - vec_len (ldpw->rd_bitmap) * - sizeof (clib_bitmap_t)); - if (writefds) - clib_memcpy_fast (ldpw->wr_bitmap, ldpw->sid_wr_bitmap, - vec_len (ldpw->wr_bitmap) * - sizeof (clib_bitmap_t)); - if (exceptfds) - 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 ? (unsigned long *) ldpw->rd_bitmap - : NULL, - writefds ? (unsigned long *) ldpw->wr_bitmap - : NULL, - exceptfds ? (unsigned long *) - ldpw->ex_bitmap : NULL, 0); - if (rv < 0) + errno = -rv; + rv = -1; + } + else if (rv > 0) + { + if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds)) { - errno = -rv; rv = -1; + goto done; } - else if (rv > 0) + + if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds)) { - if (readfds) - { - /* *INDENT-OFF* */ - clib_bitmap_foreach (si, ldpw->rd_bitmap, ({ - vlsh = vls_session_index_to_vlsh (si); - fd = ldp_vlsh_to_fd (vlsh); - if (PREDICT_FALSE (fd < 0)) - { - errno = EBADFD; - rv = -1; - goto done; - } - FD_SET (fd, readfds); - })); - /* *INDENT-ON* */ - } - if (writefds) - { - /* *INDENT-OFF* */ - clib_bitmap_foreach (si, ldpw->wr_bitmap, ({ - vlsh = vls_session_index_to_vlsh (si); - fd = ldp_vlsh_to_fd (vlsh); - if (PREDICT_FALSE (fd < 0)) - { - errno = EBADFD; - rv = -1; - goto done; - } - FD_SET (fd, writefds); - })); - /* *INDENT-ON* */ - } - if (exceptfds) - { - /* *INDENT-OFF* */ - clib_bitmap_foreach (si, ldpw->ex_bitmap, ({ - vlsh = vls_session_index_to_vlsh (si); - fd = ldp_vlsh_to_fd (vlsh); - if (PREDICT_FALSE (fd < 0)) - { - errno = EBADFD; - rv = -1; - goto done; - } - FD_SET (fd, exceptfds); - })); - /* *INDENT-ON* */ - } - ldpw->select_vcl = 1; + rv = -1; goto done; } + + if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds)) + { + rv = -1; + goto done; + } + bits_set = rv; } - else - ldpw->select_vcl = 0; } if (libc_bits) { - struct timespec tspec; - if (readfds) - clib_memcpy_fast (readfds, ldpw->libc_rd_bitmap, - vec_len (ldpw->rd_bitmap) * + clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap, + vec_len (ldpw->libc_rd_bitmap) * sizeof (clib_bitmap_t)); if (writefds) - clib_memcpy_fast (writefds, ldpw->libc_wr_bitmap, - vec_len (ldpw->wr_bitmap) * + clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap, + vec_len (ldpw->libc_wr_bitmap) * sizeof (clib_bitmap_t)); if (exceptfds) - clib_memcpy_fast (exceptfds, ldpw->libc_ex_bitmap, - vec_len (ldpw->ex_bitmap) * + clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap, + vec_len (ldpw->libc_ex_bitmap) * sizeof (clib_bitmap_t)); - tspec.tv_sec = tspec.tv_nsec = 0; + rv = libc_pselect (libc_bits, - readfds ? readfds : NULL, - writefds ? writefds : NULL, - exceptfds ? exceptfds : NULL, &tspec, sigmask); - if (rv != 0) - goto done; + readfds ? (fd_set *) ldpw->rd_bitmap : NULL, + writefds ? (fd_set *) ldpw->wr_bitmap : NULL, + exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL, + &libc_tspec, sigmask); + if (rv > 0) + { + ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds); + ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds); + ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds); + bits_set += rv; + } + } + + if (bits_set) + { + rv = bits_set; + goto done; } } while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out)); @@ -833,13 +843,13 @@ ldp_pselect (int nfds, fd_set * __restrict readfds, done: /* TBD: set timeout to amount of time left */ clib_bitmap_zero (ldpw->rd_bitmap); - clib_bitmap_zero (ldpw->sid_rd_bitmap); + clib_bitmap_zero (ldpw->si_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->si_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->si_ex_bitmap); clib_bitmap_zero (ldpw->libc_ex_bitmap); return rv; @@ -873,6 +883,71 @@ pselect (int nfds, fd_set * __restrict readfds, } #endif +/* If transparent TLS mode is turned on, then ldp will load key and cert. + */ +static int +load_tls_cert (vls_handle_t vlsh) +{ + char *env_var_str = getenv (LDP_ENV_TLS_CERT); + char inbuf[4096]; + char *tls_cert; + int cert_size; + FILE *fp; + + if (env_var_str) + { + fp = fopen (env_var_str, "r"); + if (fp == NULL) + { + LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str); + return -1; + } + cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp); + tls_cert = inbuf; + vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert, + cert_size); + fclose (fp); + } + else + { + LDBG (0, "ERROR: failed to read LDP environment %s\n", + LDP_ENV_TLS_CERT); + return -1; + } + return 0; +} + +static int +load_tls_key (vls_handle_t vlsh) +{ + char *env_var_str = getenv (LDP_ENV_TLS_KEY); + char inbuf[4096]; + char *tls_key; + int key_size; + FILE *fp; + + if (env_var_str) + { + fp = fopen (env_var_str, "r"); + if (fp == NULL) + { + LDBG (0, "ERROR: failed to open key file %s \n", env_var_str); + return -1; + } + key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp); + tls_key = inbuf; + vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key, + key_size); + fclose (fp); + } + else + { + LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY); + return -1; + } + return 0; +} + int socket (int domain, int type, int protocol) { @@ -886,8 +961,14 @@ socket (int domain, int type, int protocol) if (((domain == AF_INET) || (domain == AF_INET6)) && ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM))) { - u8 proto = ((sock_type == SOCK_DGRAM) ? - VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP); + u8 proto; + if (ldp->transparent_tls) + { + proto = VPPCOM_PROTO_TLS; + } + else + proto = ((sock_type == SOCK_DGRAM) ? + VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP); LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u", proto, vppcom_proto_str (proto), is_nonblocking); @@ -900,6 +981,13 @@ socket (int domain, int type, int protocol) } else { + if (ldp->transparent_tls) + { + if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0) + { + return -1; + } + } rv = ldp_vlsh_to_fd (vlsh); } } @@ -1493,7 +1581,7 @@ recvfrom (int fd, void *__restrict buf, size_t n, int flags, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len) { vls_handle_t sid; - ssize_t size; + ssize_t size, rv; if ((errno = -ldp_init ())) return -1; @@ -1510,7 +1598,11 @@ recvfrom (int fd, void *__restrict buf, size_t n, int flags, size = vls_recvfrom (sid, buf, n, flags, &ep); if (size > 0) - size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep); + { + rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep); + if (rv < 0) + size = rv; + } } else size = vls_recvfrom (sid, buf, n, flags, NULL); @@ -1722,6 +1814,11 @@ getsockopt (int fd, int level, int optname, else rv = -EFAULT; break; + case TCP_CONGESTION: + strcpy (optval, "cubic"); + *optlen = strlen ("cubic"); + rv = 0; + break; default: LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, " "optname %d unsupported!", fd, vlsh, optname); @@ -1830,6 +1927,11 @@ setsockopt (int fd, int level, int optname, rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL, (void *) optval, &optlen); break; + case TCP_CONGESTION: + case TCP_CORK: + /* Ignore */ + rv = 0; + break; default: LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u" "optname %d unsupported!", fd, vlsh, optname); @@ -2038,6 +2140,12 @@ epoll_create1 (int flags) if (ldp->vcl_needs_real_epoll) { + /* Make sure workers have been allocated */ + if (!ldp->workers) + { + ldp_alloc_workers (); + ldpw = ldp_worker_get_current (); + } rv = libc_epoll_create1 (flags); ldp->vcl_needs_real_epoll = 0; ldpw->vcl_mq_epfd = rv; @@ -2155,7 +2263,7 @@ 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; + double time_to_wait = (double) 0, max_time; int libc_epfd, rv = 0; vls_handle_t ep_vlsh; @@ -2179,8 +2287,10 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, return -1; } + if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) + clib_time_init (&ldpw->clib_time); time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0); - time_out = clib_time_now (&ldpw->clib_time) + time_to_wait; + max_time = clib_time_now (&ldpw->clib_time) + time_to_wait; libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); if (PREDICT_FALSE (libc_epfd < 0)) @@ -2192,8 +2302,7 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, " "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh, - libc_epfd, events, maxevents, timeout, sigmask, time_to_wait, - time_out); + libc_epfd, events, maxevents, timeout, sigmask, time_to_wait); do { if (!ldpw->epoll_wait_vcl) @@ -2220,11 +2329,8 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, if (rv != 0) goto done; } - - if (timeout != -1) - now = clib_time_now (&ldpw->clib_time); } - while (now < time_out); + while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time)); done: return rv; @@ -2250,14 +2356,15 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout) int rv, i, n_revents = 0; vls_handle_t vlsh; vcl_poll_t *vp; - double wait_for_time; + double max_time; LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout); - if (timeout >= 0) - wait_for_time = (f64) timeout / 1000; - else - wait_for_time = -1; + if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) + clib_time_init (&ldpw->clib_time); + + max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0; + max_time += clib_time_now (&ldpw->clib_time); for (i = 0; i < nfds; i++) { @@ -2317,8 +2424,7 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout) goto done; } } - while ((wait_for_time == -1) || - (clib_time_now (&ldpw->clib_time) < wait_for_time)); + while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time)); rv = 0; done: @@ -2375,8 +2481,11 @@ ldp_constructor (void) { swrap_constructor (); if (ldp_init () != 0) - fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n", - getpid ()); + { + fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n", + getpid ()); + _exit (1); + } else if (LDP_DEBUG > 0) clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ()); } @@ -2387,16 +2496,18 @@ ldp_constructor (void) void ldp_destructor (void) { - swrap_destructor (); - if (ldp->init) - ldp->init = 0; + /* + swrap_destructor (); + if (ldp->init) + ldp->init = 0; + */ /* Don't use clib_warning() here because that calls writev() * which will call ldp_init(). */ if (LDP_DEBUG > 0) - printf ("%s:%d: LDP<%d>: LDP destructor: done!\n", - __func__, __LINE__, getpid ()); + fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n", + __func__, __LINE__, getpid ()); }