X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvcl%2Fvppcom.c;h=d3ed8e3c0cff88a153db951673bb4e69519fe174;hb=66cf6eb41c5a8bb833e94040dff94f6d8ce3814e;hp=1b1a08e4c060eca85d06e483d621b91c232c61d3;hpb=ac1f96d7cdfb845c08400d27ec566b991e32e24b;p=vpp.git diff --git a/src/vcl/vppcom.c b/src/vcl/vppcom.c index 1b1a08e4c06..d3ed8e3c0cf 100644 --- a/src/vcl/vppcom.c +++ b/src/vcl/vppcom.c @@ -127,6 +127,10 @@ typedef struct vppcom_cfg_t_ u32 tx_fifo_size; u32 event_queue_size; u32 listen_queue_size; + u8 session_scope_local; + u8 session_scope_global; + u8 *namespace_id; + u64 namespace_secret; f64 app_timeout; f64 session_timeout; f64 accept_timeout; @@ -455,6 +459,8 @@ vppcom_app_send_attach (void) { vppcom_main_t *vcm = &vppcom_main; vl_api_application_attach_t *bmp; + u8 nsid_len = vec_len (vcm->cfg.namespace_id); + bmp = vl_msg_api_alloc (sizeof (*bmp)); memset (bmp, 0, sizeof (*bmp)); @@ -463,11 +469,18 @@ vppcom_app_send_attach (void) bmp->context = htonl (0xfeedface); bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT | - APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE | APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; + (vcm->cfg.session_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) | + (vcm->cfg.session_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0); bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; + if (nsid_len) + { + bmp->namespace_id_len = nsid_len; + clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len); + bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret; + } vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); } @@ -813,33 +826,19 @@ vppcom_send_connect_sock (session_t * session, u32 session_index) vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp); } -static int -vppcom_send_disconnect (u32 session_index) +static inline void +vppcom_send_disconnect (session_t * session) { vppcom_main_t *vcm = &vppcom_main; vl_api_disconnect_session_t *dmp; - session_t *session = 0; - int rv; - - clib_spinlock_lock (&vcm->sessions_lockp); - rv = vppcom_session_at_index (session_index, &session); - if (PREDICT_FALSE (rv)) - { - clib_spinlock_unlock (&vcm->sessions_lockp); - if (VPPCOM_DEBUG > 1) - clib_warning ("[%d] invalid session, sid (%u) has been closed!", - vcm->my_pid, session_index); - return rv; - } + /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */ dmp = vl_msg_api_alloc (sizeof (*dmp)); memset (dmp, 0, sizeof (*dmp)); dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); dmp->client_index = vcm->my_client_index; dmp->handle = session->vpp_session_handle; - clib_spinlock_unlock (&vcm->sessions_lockp); vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp); - return VPPCOM_OK; } static void @@ -1283,25 +1282,38 @@ vppcom_session_unbind (u32 session_index) return VPPCOM_OK; } -static int +static inline int vppcom_session_disconnect (u32 session_index) { vppcom_main_t *vcm = &vppcom_main; int rv; + session_t *session; - rv = vppcom_send_disconnect (session_index); - if (PREDICT_FALSE (rv)) - return rv; - - rv = vppcom_wait_for_session_state_change (session_index, STATE_DISCONNECT, - vcm->cfg.session_timeout); + clib_spinlock_lock (&vcm->sessions_lockp); + rv = vppcom_session_at_index (session_index, &session); if (PREDICT_FALSE (rv)) { - if (VPPCOM_DEBUG > 0) - clib_warning ("[%d] client disconnect timed out, rv = %s (%d)", - vcm->my_pid, vppcom_retval_str (rv), rv); + clib_spinlock_unlock (&vcm->sessions_lockp); + if (VPPCOM_DEBUG > 1) + clib_warning ("[%d] invalid session, sid (%u) has been closed!", + vcm->my_pid, session_index); return rv; } + + if (!session->is_cut_thru) + { + vppcom_send_disconnect (session); + clib_spinlock_unlock (&vcm->sessions_lockp); + + rv = vppcom_wait_for_session_state_change (session_index, + STATE_DISCONNECT, 1.0); + if ((VPPCOM_DEBUG > 0) && (rv < 0)) + clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)", + vcm->my_pid, session_index, vppcom_retval_str (rv), rv); + } + else + clib_spinlock_unlock (&vcm->sessions_lockp); + return VPPCOM_OK; } @@ -1509,7 +1521,7 @@ vppcom_cfg_read (char *conf_fname) (void) unformat_user (input, unformat_line_input, line_input); unformat_skip_white_space (line_input); - if (unformat (line_input, "vppcom {")) + if (unformat (line_input, "vcl {")) { vc_cfg_input = 1; continue; @@ -1548,11 +1560,11 @@ vppcom_cfg_read (char *conf_fname) if (VPPCOM_DEBUG > 0) clib_warning ("[%d] configured gid %d", vcm->my_pid, gid); } - else if (unformat (line_input, "segment-baseva 0x%llx", + else if (unformat (line_input, "segment-baseva 0x%lx", &vcl_cfg->segment_baseva)) { if (VPPCOM_DEBUG > 0) - clib_warning ("[%d] configured segment_baseva 0x%llx", + clib_warning ("[%d] configured segment_baseva 0x%lx", vcm->my_pid, vcl_cfg->segment_baseva); } else if (unformat (line_input, "segment-size 0x%lx", @@ -1683,6 +1695,57 @@ vppcom_cfg_read (char *conf_fname) clib_warning ("[%d] configured accept_timeout %f", vcm->my_pid, vcl_cfg->accept_timeout); } + else if (unformat (line_input, "session-scope-local")) + { + vcl_cfg->session_scope_local = 1; + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] configured session_scope_local (%d)", + vcm->my_pid, vcl_cfg->session_scope_local); + } + else if (unformat (line_input, "session-scope-global")) + { + vcl_cfg->session_scope_global = 1; + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] configured session_scope_global (%d)", + vcm->my_pid, vcl_cfg->session_scope_global); + } + else if (unformat (line_input, "namespace-secret 0x%lx", + &vcl_cfg->namespace_secret)) + { + if (VPPCOM_DEBUG > 0) + clib_warning + ("[%d] configured namespace_secret 0x%lx (%lu)", + vcm->my_pid, vcl_cfg->namespace_secret, + vcl_cfg->namespace_secret); + } + else if (unformat (line_input, "namespace-secret %lu", + &vcl_cfg->namespace_secret)) + { + if (VPPCOM_DEBUG > 0) + clib_warning + ("[%d] configured namespace_secret %lu (0x%lx)", + vcm->my_pid, vcl_cfg->namespace_secret, + vcl_cfg->namespace_secret); + } + else if (unformat (line_input, "namespace-id %v", + &vcl_cfg->namespace_id)) + { + vl_api_application_attach_t *mp; + u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1; + u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id); + if (nsid_vec_len > max_nsid_vec_len) + { + _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len; + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] configured namespace_id is too long," + " truncated to %d characters!", vcm->my_pid, + max_nsid_vec_len); + } + + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] configured namespace_id %v", + vcm->my_pid, vcl_cfg->namespace_id); + } else if (unformat (line_input, "}")) { vc_cfg_input = 0; @@ -1726,22 +1789,72 @@ vppcom_app_create (char *app_name) if (!vcm->init) { char *conf_fname; + char *env_var_str; vcm->init = 1; vcm->my_pid = getpid (); clib_fifo_validate (vcm->client_session_index_fifo, vcm->cfg.listen_queue_size); vppcom_cfg_init (vcl_cfg); - conf_fname = getenv (VPPCOM_CONF_ENV); + conf_fname = getenv (VPPCOM_ENV_CONF); if (!conf_fname) { conf_fname = VPPCOM_CONF_DEFAULT; if (VPPCOM_DEBUG > 0) clib_warning ("[%d] getenv '%s' failed!", vcm->my_pid, - VPPCOM_CONF_ENV); + VPPCOM_ENV_CONF); } vppcom_cfg_heapsize (conf_fname); vppcom_cfg_read (conf_fname); + env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID); + if (env_var_str) + { + u32 ns_id_vec_len = strlen (env_var_str); + + vec_reset_length (vcm->cfg.namespace_id); + vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1); + clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len); + + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] configured namespace_id (%v) from " + VPPCOM_ENV_APP_NAMESPACE_ID "!", vcm->my_pid, + vcm->cfg.namespace_id); + } + env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET); + if (env_var_str) + { + u64 tmp; + if (sscanf (env_var_str, "%lu", &tmp) != 1) + clib_warning ("[%d] Invalid namespace secret specified in " + "the environment variable " + VPPCOM_ENV_APP_NAMESPACE_SECRET + " (%s)!\n", vcm->my_pid, env_var_str); + else + { + vcm->cfg.namespace_secret = tmp; + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] configured namespace secret (%lu) from " + VPPCOM_ENV_APP_NAMESPACE_ID "!", vcm->my_pid, + vcm->cfg.namespace_secret); + } + } + if (getenv (VPPCOM_ENV_SESSION_SCOPE_LOCAL)) + { + vcm->cfg.session_scope_local = 1; + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] configured session_scope_local (%u) from " + VPPCOM_ENV_SESSION_SCOPE_LOCAL "!", vcm->my_pid, + vcm->cfg.session_scope_local); + } + if (getenv (VPPCOM_ENV_SESSION_SCOPE_GLOBAL)) + { + vcm->cfg.session_scope_global = 1; + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] configured session_scope_global (%u) from " + VPPCOM_ENV_SESSION_SCOPE_GLOBAL "!", vcm->my_pid, + vcm->cfg.session_scope_global); + } + vcm->bind_session_index = ~0; vcm->main_cpu = os_get_thread_index (); heap = clib_mem_get_per_cpu_heap (); @@ -1790,12 +1903,12 @@ vppcom_app_create (char *app_name) clib_warning ("[%d] vppcom_app_attach() failed!", vcm->my_pid); return rv; } - } - if (VPPCOM_DEBUG > 0) - clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)", - vcm->my_pid, app_name, vcm->my_client_index, - vcm->my_client_index); + if (VPPCOM_DEBUG > 0) + clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)", + vcm->my_pid, app_name, vcm->my_client_index, + vcm->my_client_index); + } return VPPCOM_OK; } @@ -1856,6 +1969,14 @@ vppcom_session_close (uint32_t session_index) vppcom_main_t *vcm = &vppcom_main; session_t *session = 0; int rv; + u8 is_server; + u8 is_listen; + u8 is_cut_thru; + u8 is_vep; + u8 is_vep_session; + u32 next_sid; + u32 vep_idx; + session_state_t state; clib_spinlock_lock (&vcm->sessions_lockp); rv = vppcom_session_at_index (session_index, &session); @@ -1867,22 +1988,27 @@ vppcom_session_close (uint32_t session_index) clib_spinlock_unlock (&vcm->sessions_lockp); goto done; } + is_server = session->is_server; + is_listen = session->is_listen; + is_cut_thru = session->is_cut_thru; + is_vep = session->is_vep; + is_vep_session = session->is_vep_session; + next_sid = session->vep.next_sid; + vep_idx = session->vep.vep_idx; + state = session->state; clib_spinlock_unlock (&vcm->sessions_lockp); if (VPPCOM_DEBUG > 0) clib_warning ("[%d] sid %d", vcm->my_pid, session_index); - if (session->is_vep) + if (is_vep) { - u32 next_sid; - for (next_sid = session->vep.next_sid; next_sid != ~0; - next_sid = session->vep.next_sid) + while (next_sid != ~0) { rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0); if ((VPPCOM_DEBUG > 0) && (rv < 0)) clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, " - "rv = %s (%d)", session_index, next_sid, - vcm->my_pid, session_index, + "rv = %s (%d)", vcm->my_pid, vep_idx, next_sid, vppcom_retval_str (rv), rv); clib_spinlock_lock (&vcm->sessions_lockp); @@ -1896,37 +2022,22 @@ vppcom_session_close (uint32_t session_index) clib_spinlock_unlock (&vcm->sessions_lockp); goto done; } + next_sid = session->vep.next_sid; clib_spinlock_unlock (&vcm->sessions_lockp); } } else { - if (session->is_vep_session) + if (is_vep_session) { - u32 vep_idx = session->vep.vep_idx; rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0); if ((VPPCOM_DEBUG > 0) && (rv < 0)) clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, " - "rv = %s (%d)", vep_idx, session_index, - vcm->my_pid, session_index, + "rv = %s (%d)", vcm->my_pid, vep_idx, session_index, vppcom_retval_str (rv), rv); - - clib_spinlock_lock (&vcm->sessions_lockp); - rv = vppcom_session_at_index (session_index, &session); - if (PREDICT_FALSE (rv)) - { - if (VPPCOM_DEBUG > 0) - clib_warning - ("[%d] invalid session, sid (%u) has been closed!", - vcm->my_pid, session_index); - clib_spinlock_unlock (&vcm->sessions_lockp); - goto done; - } - clib_spinlock_unlock (&vcm->sessions_lockp); } - if (session->is_cut_thru && session->is_server && - (session->state == STATE_ACCEPT)) + if (is_cut_thru && is_server && (state == STATE_ACCEPT)) { rv = vppcom_session_unbind_cut_thru (session); if ((VPPCOM_DEBUG > 0) && (rv < 0)) @@ -1935,7 +2046,7 @@ vppcom_session_close (uint32_t session_index) vcm->my_pid, session_index, vppcom_retval_str (rv), rv); } - else if (session->is_server && session->is_listen) + else if (is_server && is_listen) { rv = vppcom_session_unbind (session_index); if ((VPPCOM_DEBUG > 0) && (rv < 0)) @@ -1943,16 +2054,13 @@ vppcom_session_close (uint32_t session_index) vcm->my_pid, session_index, vppcom_retval_str (rv), rv); } - else if (session->state == STATE_CONNECT) - { - rv = vppcom_session_disconnect (session_index); - if ((VPPCOM_DEBUG > 0) && (rv < 0)) - clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)", - vcm->my_pid, session_index, - vppcom_retval_str (rv), rv); - } + else if (state == STATE_CONNECT) + if (vppcom_session_disconnect (session_index)) + goto done; } + clib_spinlock_lock (&vcm->sessions_lockp); pool_put_index (vcm->sessions, session_index); + clib_spinlock_unlock (&vcm->sessions_lockp); done: return rv; } @@ -2236,8 +2344,9 @@ vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep) return VPPCOM_OK; } -int -vppcom_session_read (uint32_t session_index, void *buf, int n) +static inline int +vppcom_session_read_internal (uint32_t session_index, void *buf, int n, + u8 peek) { vppcom_main_t *vcm = &vppcom_main; session_t *session = 0; @@ -2287,7 +2396,10 @@ vppcom_session_read (uint32_t session_index, void *buf, int n) do { - n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf); + if (peek) + n_read = svm_fifo_peek (rx_fifo, 0, n, buf); + else + n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf); } while (!session->is_nonblocking && (n_read <= 0)); @@ -2305,6 +2417,18 @@ vppcom_session_read (uint32_t session_index, void *buf, int n) return (n_read <= 0) ? VPPCOM_EAGAIN : n_read; } +int +vppcom_session_read (uint32_t session_index, void *buf, int n) +{ + return (vppcom_session_read_internal (session_index, buf, n, 0)); +} + +static int +vppcom_session_peek (uint32_t session_index, void *buf, int n) +{ + return (vppcom_session_read_internal (session_index, buf, n, 1)); +} + static inline int vppcom_session_read_ready (session_t * session, u32 session_index) { @@ -3154,7 +3278,7 @@ vppcom_session_attr (uint32_t session_index, uint32_t op, { case VPPCOM_ATTR_GET_NREAD: rv = vppcom_session_read_ready (session, session_index); - if (VPPCOM_DEBUG > 0) + if (VPPCOM_DEBUG > 1) clib_warning ("VPPCOM_ATTR_GET_NREAD: nread = %d", rv); break; @@ -3168,7 +3292,7 @@ vppcom_session_attr (uint32_t session_index, uint32_t op, { *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0); *buflen = sizeof (*flags); - if (VPPCOM_DEBUG > 0) + if (VPPCOM_DEBUG > 1) clib_warning ("VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, " "is_nonblocking = %u", *flags, session->is_nonblocking); @@ -3181,7 +3305,7 @@ vppcom_session_attr (uint32_t session_index, uint32_t op, if (buffer && buflen && (*buflen >= sizeof (*flags))) { session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0; - if (VPPCOM_DEBUG > 0) + if (VPPCOM_DEBUG > 1) clib_warning ("VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, " "is_nonblocking = %u", *flags, session->is_nonblocking); @@ -3203,7 +3327,7 @@ vppcom_session_attr (uint32_t session_index, uint32_t op, clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6, sizeof (ip6_address_t)); *buflen = sizeof (*ep); - if (VPPCOM_DEBUG > 0) + if (VPPCOM_DEBUG > 1) clib_warning ("VPPCOM_ATTR_GET_PEER_ADDR: sid %u is_ip4 = %u, " "addr = %U, port %u", session_index, ep->is_ip4, format_ip46_address, @@ -3227,7 +3351,7 @@ vppcom_session_attr (uint32_t session_index, uint32_t op, clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6, sizeof (ip6_address_t)); *buflen = sizeof (*ep); - if (VPPCOM_DEBUG > 0) + if (VPPCOM_DEBUG > 1) clib_warning ("VPPCOM_ATTR_GET_LCL_ADDR: sid %u is_ip4 = %u, " "addr = %U port %d", session_index, ep->is_ip4, format_ip46_address, @@ -3284,7 +3408,9 @@ vppcom_session_recvfrom (uint32_t session_index, void *buffer, if (VPPCOM_DEBUG > 0) clib_warning ("[%d] invalid session, sid (%u) has been closed!", vcm->my_pid, session_index); - rv = VPPCOM_EINVAL; + rv = VPPCOM_EBADFD; + clib_spinlock_unlock (&vcm->sessions_lockp); + goto done; } ep->vrf = session->vrf; ep->is_ip4 = session->peer_addr.is_ip4; @@ -3296,22 +3422,19 @@ vppcom_session_recvfrom (uint32_t session_index, void *buffer, clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6, sizeof (ip6_address_t)); clib_spinlock_unlock (&vcm->sessions_lockp); - rv = vppcom_session_read (session_index, buffer, buflen); } - else if (flags == 0) + + if (flags == 0) rv = vppcom_session_read (session_index, buffer, buflen); else if (flags & MSG_PEEK) - { - rv = vppcom_session_attr (session_index, VPPCOM_ATTR_GET_NREAD, 0, 0); - if (rv > buflen) - rv = buflen; - } + rv = vppcom_session_peek (session_index, buffer, buflen); else { - clib_warning ("Unsupport flags for recvfro %d", flags); + clib_warning ("Unsupport flags for recvfrom %d", flags); rv = VPPCOM_EAFNOSUPPORT; } +done: return rv; } @@ -3319,16 +3442,26 @@ int vppcom_session_sendto (uint32_t session_index, void *buffer, uint32_t buflen, int flags, vppcom_endpt_t * ep) { + vppcom_main_t *vcm = &vppcom_main; + + if (!buffer) + return VPPCOM_EINVAL; + if (ep) - // TBD - return -1; - else if (flags == 0) - return (vppcom_session_write (session_index, buffer, buflen)); - else if (flags) - // TBD check the flags and do the right thing - return (vppcom_session_write (session_index, buffer, buflen)); - - return -1; + { + // TBD + return VPPCOM_EINVAL; + } + + if (flags) + { + // TBD check the flags and do the right thing + if (VPPCOM_DEBUG > 2) + clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.", + vcm->my_pid, flags, flags); + } + + return (vppcom_session_write (session_index, buffer, buflen)); } /*