- if (VPPCOM_DEBUG > 1)
- clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
- "sending disconnect...",
- getpid (), vpp_handle, session_index);
-
- vppcom_send_disconnect_session (vpp_handle, session_index);
- }
-
-done:
- return rv;
-}
-
-#define foreach_sock_msg \
-_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
-_(BIND_SOCK_REPLY, bind_sock_reply) \
-_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
-_(ACCEPT_SESSION, accept_session) \
-_(CONNECT_SESSION_REPLY, connect_session_reply) \
-_(DISCONNECT_SESSION, disconnect_session) \
-_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
-_(RESET_SESSION, reset_session) \
-_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
-_(APPLICATION_DETACH_REPLY, application_detach_reply) \
-_(MAP_ANOTHER_SEGMENT, map_another_segment) \
-_(UNMAP_SEGMENT, unmap_segment)
-
-static void
-vppcom_api_hookup (void)
-{
-#define _(N, n) \
- vl_msg_api_set_handlers(VL_API_##N, #n, \
- vl_api_##n##_t_handler, \
- vl_noop_handler, \
- vl_api_##n##_t_endian, \
- vl_api_##n##_t_print, \
- sizeof(vl_api_##n##_t), 1);
- foreach_sock_msg;
-#undef _
-}
-
-static void
-vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
-{
- ASSERT (vcl_cfg);
-
- vcl_cfg->heapsize = (256ULL << 20);
- vcl_cfg->vpp_api_q_length = 1024;
- vcl_cfg->segment_baseva = 0x200000000ULL;
- vcl_cfg->segment_size = (256 << 20);
- vcl_cfg->add_segment_size = (128 << 20);
- vcl_cfg->preallocated_fifo_pairs = 8;
- vcl_cfg->rx_fifo_size = (1 << 20);
- vcl_cfg->tx_fifo_size = (1 << 20);
- vcl_cfg->event_queue_size = 2048;
- vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
- vcl_cfg->app_timeout = 10 * 60.0;
- vcl_cfg->session_timeout = 10 * 60.0;
- vcl_cfg->accept_timeout = 60.0;
- vcl_cfg->event_ring_size = (128 << 10);
- vcl_cfg->event_log_path = "/dev/shm";
-}
-
-static void
-vppcom_cfg_heapsize (char *conf_fname)
-{
- vppcom_cfg_t *vcl_cfg = &vcm->cfg;
- FILE *fp;
- char inbuf[4096];
- int argc = 1;
- char **argv = NULL;
- char *arg = NULL;
- char *p;
- int i;
- u8 *sizep;
- u32 size;
- void *vcl_mem;
- void *heap;
-
- fp = fopen (conf_fname, "r");
- if (fp == NULL)
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
- getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
- goto defaulted;
- }
-
- argv = calloc (1, sizeof (char *));
- if (argv == NULL)
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: calloc failed, using default "
- "heapsize %lld (0x%llx)",
- getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
- goto defaulted;
- }
-
- while (1)
- {
- if (fgets (inbuf, 4096, fp) == 0)
- break;
- p = strtok (inbuf, " \t\n");
- while (p != NULL)
- {
- if (*p == '#')
- break;
- argc++;
- char **tmp = realloc (argv, argc * sizeof (char *));
- if (tmp == NULL)
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: realloc failed, "
- "using default heapsize %lld (0x%llx)",
- getpid (), vcl_cfg->heapsize,
- vcl_cfg->heapsize);
- goto defaulted;
- }
- argv = tmp;
- arg = strndup (p, 1024);
- if (arg == NULL)
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: strndup failed, "
- "using default heapsize %lld (0x%llx)",
- getpid (), vcl_cfg->heapsize,
- vcl_cfg->heapsize);
- goto defaulted;
- }
- argv[argc - 1] = arg;
- p = strtok (NULL, " \t\n");
- }
- }
-
- fclose (fp);
- fp = NULL;
-
- char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
- if (tmp == NULL)
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: realloc failed, "
- "using default heapsize %lld (0x%llx)",
- getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
- goto defaulted;
- }
- argv = tmp;
- argv[argc] = NULL;
-
- /*
- * Look for and parse the "heapsize" config parameter.
- * Manual since none of the clib infra has been bootstrapped yet.
- *
- * Format: heapsize <nn>[mM][gG]
- */
-
- for (i = 1; i < (argc - 1); i++)
- {
- if (!strncmp (argv[i], "heapsize", 8))
- {
- sizep = (u8 *) argv[i + 1];
- size = 0;
- while (*sizep >= '0' && *sizep <= '9')
- {
- size *= 10;
- size += *sizep++ - '0';
- }
- if (size == 0)
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: parse error '%s %s', "
- "using default heapsize %lld (0x%llx)",
- getpid (), argv[i], argv[i + 1],
- vcl_cfg->heapsize, vcl_cfg->heapsize);
- goto defaulted;
- }
-
- if (*sizep == 'g' || *sizep == 'G')
- vcl_cfg->heapsize = size << 30;
- else if (*sizep == 'm' || *sizep == 'M')
- vcl_cfg->heapsize = size << 20;
- else
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: parse error '%s %s', "
- "using default heapsize %lld (0x%llx)",
- getpid (), argv[i], argv[i + 1],
- vcl_cfg->heapsize, vcl_cfg->heapsize);
- goto defaulted;
- }
- }
- }
-
-defaulted:
- if (fp != NULL)
- fclose (fp);
- if (argv != NULL)
- free (argv);
-
- vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
- MAP_SHARED | MAP_ANONYMOUS, -1, 0);
- if (vcl_mem == MAP_FAILED)
- {
- clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
- "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
- "-1, 0) failed!",
- getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
- ASSERT (vcl_mem != MAP_FAILED);
- return;
- }
- heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
- if (!heap)
- {
- clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
- ASSERT (heap);
- return;
- }
- vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
- if (!vcl_mem)
- {
- clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
- ASSERT (vcl_mem);
- return;
- }
-
- clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
- vcm = vcl_mem;
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
- getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
-}
-
-static void
-vppcom_cfg_read (char *conf_fname)
-{
- vppcom_cfg_t *vcl_cfg = &vcm->cfg;
- int fd;
- unformat_input_t _input, *input = &_input;
- unformat_input_t _line_input, *line_input = &_line_input;
- u8 vc_cfg_input = 0;
- u8 *chroot_path;
- struct stat s;
- u32 uid, gid, q_len;
-
- fd = open (conf_fname, O_RDONLY);
- if (fd < 0)
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: using default configuration.",
- getpid (), conf_fname);
- goto file_done;
- }
-
- if (fstat (fd, &s) < 0)
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: failed to stat `%s', "
- "using default configuration", getpid (), conf_fname);
- goto file_done;
- }
-
- if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: not a regular file `%s', "
- "using default configuration", getpid (), conf_fname);
- goto file_done;
- }
-
- unformat_init_clib_file (input, fd);
-
- while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
- {
- (void) unformat_user (input, unformat_line_input, line_input);
- unformat_skip_white_space (line_input);
-
- if (unformat (line_input, "vcl {"))
- {
- vc_cfg_input = 1;
- continue;
- }
-
- if (vc_cfg_input)
- {
- if (unformat (line_input, "heapsize %s", &chroot_path))
- {
- vec_terminate_c_string (chroot_path);
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured heapsize %s, "
- "actual heapsize %lld (0x%llx)",
- getpid (), chroot_path, vcl_cfg->heapsize,
- vcl_cfg->heapsize);
- vec_free (chroot_path);
- }
- else if (unformat (line_input, "api-prefix %s", &chroot_path))
- {
- vec_terminate_c_string (chroot_path);
- if (vcl_cfg->vpp_api_filename)
- vec_free (vcl_cfg->vpp_api_filename);
- vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
- chroot_path, 0);
- vl_set_memory_root_path ((char *) chroot_path);
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured api-prefix (%s) and "
- "api filename (%s)", getpid (), chroot_path,
- vcl_cfg->vpp_api_filename);
- chroot_path = 0; /* Don't vec_free() it! */
- }
- else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
- {
- if (q_len < vcl_cfg->vpp_api_q_length)
- {
- clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
- "(%u) is too small! Using default: %u ",
- getpid (), q_len, vcl_cfg->vpp_api_q_length);
- }
- else
- {
- vcl_cfg->vpp_api_q_length = q_len;
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
- getpid (), vcl_cfg->vpp_api_q_length);
- }
- }
- else if (unformat (line_input, "uid %d", &uid))
- {
- vl_set_memory_uid (uid);
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
- }
- else if (unformat (line_input, "gid %d", &gid))
- {
- vl_set_memory_gid (gid);
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
- }
- else if (unformat (line_input, "segment-baseva 0x%lx",
- &vcl_cfg->segment_baseva))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
- getpid (), vcl_cfg->segment_baseva);
- }
- else if (unformat (line_input, "segment-size 0x%lx",
- &vcl_cfg->segment_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
- getpid (), vcl_cfg->segment_size,
- vcl_cfg->segment_size);
- }
- else if (unformat (line_input, "segment-size %ld",
- &vcl_cfg->segment_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
- getpid (), vcl_cfg->segment_size,
- vcl_cfg->segment_size);
- }
- else if (unformat (line_input, "add-segment-size 0x%lx",
- &vcl_cfg->add_segment_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning
- ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
- getpid (), vcl_cfg->add_segment_size,
- vcl_cfg->add_segment_size);
- }
- else if (unformat (line_input, "add-segment-size %ld",
- &vcl_cfg->add_segment_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning
- ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
- getpid (), vcl_cfg->add_segment_size,
- vcl_cfg->add_segment_size);
- }
- else if (unformat (line_input, "preallocated-fifo-pairs %d",
- &vcl_cfg->preallocated_fifo_pairs))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
- "%d (0x%x)", getpid (),
- vcl_cfg->preallocated_fifo_pairs,
- vcl_cfg->preallocated_fifo_pairs);
- }
- else if (unformat (line_input, "rx-fifo-size 0x%lx",
- &vcl_cfg->rx_fifo_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
- getpid (), vcl_cfg->rx_fifo_size,
- vcl_cfg->rx_fifo_size);
- }
- else if (unformat (line_input, "rx-fifo-size %ld",
- &vcl_cfg->rx_fifo_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
- getpid (), vcl_cfg->rx_fifo_size,
- vcl_cfg->rx_fifo_size);
- }
- else if (unformat (line_input, "tx-fifo-size 0x%lx",
- &vcl_cfg->tx_fifo_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
- getpid (), vcl_cfg->tx_fifo_size,
- vcl_cfg->tx_fifo_size);
- }
- else if (unformat (line_input, "tx-fifo-size %ld",
- &vcl_cfg->tx_fifo_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
- getpid (), vcl_cfg->tx_fifo_size,
- vcl_cfg->tx_fifo_size);
- }
- else if (unformat (line_input, "event-queue-size 0x%lx",
- &vcl_cfg->event_queue_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured event_queue_size "
- "0x%lx (%ld)",
- getpid (), vcl_cfg->event_queue_size,
- vcl_cfg->event_queue_size);
- }
- else if (unformat (line_input, "event-queue-size %ld",
- &vcl_cfg->event_queue_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured event_queue_size "
- "%ld (0x%lx)",
- getpid (), vcl_cfg->event_queue_size,
- vcl_cfg->event_queue_size);
- }
- else if (unformat (line_input, "listen-queue-size 0x%lx",
- &vcl_cfg->listen_queue_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured listen_queue_size "
- "0x%lx (%ld)",
- getpid (), vcl_cfg->listen_queue_size,
- vcl_cfg->listen_queue_size);
- }
- else if (unformat (line_input, "listen-queue-size %ld",
- &vcl_cfg->listen_queue_size))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured listen_queue_size "
- "%ld (0x%lx)",
- getpid (), vcl_cfg->listen_queue_size,
- vcl_cfg->listen_queue_size);
- }
- else if (unformat (line_input, "app-timeout %f",
- &vcl_cfg->app_timeout))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured app_timeout %f",
- getpid (), vcl_cfg->app_timeout);
- }
- else if (unformat (line_input, "session-timeout %f",
- &vcl_cfg->session_timeout))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured session_timeout %f",
- getpid (), vcl_cfg->session_timeout);
- }
- else if (unformat (line_input, "accept-timeout %f",
- &vcl_cfg->accept_timeout))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured accept_timeout %f",
- getpid (), vcl_cfg->accept_timeout);
- }
- else if (unformat (line_input, "app-proxy-transport-tcp"))
- {
- vcl_cfg->app_proxy_transport_tcp = 1;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured "
- "app_proxy_transport_tcp (%d)",
- getpid (), vcl_cfg->app_proxy_transport_tcp);
- }
- else if (unformat (line_input, "app-proxy-transport-udp"))
- {
- vcl_cfg->app_proxy_transport_udp = 1;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured "
- "app_proxy_transport_udp (%d)",
- getpid (), vcl_cfg->app_proxy_transport_udp);
- }
- else if (unformat (line_input, "app-scope-local"))
- {
- vcl_cfg->app_scope_local = 1;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured app_scope_local (%d)",
- getpid (), vcl_cfg->app_scope_local);
- }
- else if (unformat (line_input, "app-scope-global"))
- {
- vcl_cfg->app_scope_global = 1;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured app_scope_global (%d)",
- getpid (), vcl_cfg->app_scope_global);
- }
- else if (unformat (line_input, "namespace-secret %lu",
- &vcl_cfg->namespace_secret))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning
- ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
- getpid (), 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 ("VCL<%d>: configured namespace_id is "
- "too long, truncated to %d characters!",
- getpid (), max_nsid_vec_len);
- }
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured namespace_id %v",
- getpid (), vcl_cfg->namespace_id);
- }
- else if (unformat (line_input, "}"))
- {
- vc_cfg_input = 0;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: completed parsing vppcom config!",
- getpid ());
- goto input_done;
- }
- else
- {
- if (line_input->buffer[line_input->index] != '#')
- {
- clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
- getpid (), (char *)
- &line_input->buffer[line_input->index]);
- }
- }
- }
- }
-
-input_done:
- unformat_free (input);
-
-file_done:
- if (fd >= 0)
- close (fd);
-}
-
-/*
- * VPPCOM Public API functions
- */
-int
-vppcom_app_create (char *app_name)
-{
- vppcom_cfg_t *vcl_cfg = &vcm->cfg;
- u8 *heap;
- mheap_t *h;
- int rv;
-
- if (!vcm->init)
- {
- char *conf_fname;
- char *env_var_str;
-
- vcm->init = 1;
- vppcom_cfg_init (vcl_cfg);
- env_var_str = getenv (VPPCOM_ENV_DEBUG);
- if (env_var_str)
- {
- u32 tmp;
- if (sscanf (env_var_str, "%u", &tmp) != 1)
- clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
- "in the environment variable " VPPCOM_ENV_DEBUG
- " (%s)!\n", getpid (), env_var_str);
- else
- {
- vcm->debug = tmp;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
- VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
- }
- }
- conf_fname = getenv (VPPCOM_ENV_CONF);
- if (!conf_fname)
- conf_fname = VPPCOM_CONF_DEFAULT;
- vppcom_cfg_heapsize (conf_fname);
- vcl_cfg = &vcm->cfg;
- clib_spinlock_init (&vcm->session_fifo_lockp);
- clib_fifo_validate (vcm->client_session_index_fifo,
- vcm->cfg.listen_queue_size);
- vppcom_cfg_read (conf_fname);
-
- env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
- if (env_var_str)
- {
- if (vcl_cfg->vpp_api_filename)
- vec_free (vcl_cfg->vpp_api_filename);
- vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
- env_var_str, 0);
- vl_set_memory_root_path ((char *) env_var_str);
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured api prefix (%s) and "
- "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
- getpid (), env_var_str, vcl_cfg->vpp_api_filename);
- }
- 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 ("VCL<%d>: configured namespace_id (%v) from "
- VPPCOM_ENV_APP_NAMESPACE_ID
- "!", getpid (), 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 ("VCL<%d>: WARNING: Invalid namespace secret "
- "specified in the environment variable "
- VPPCOM_ENV_APP_NAMESPACE_SECRET
- " (%s)!\n", getpid (), env_var_str);
- else
- {
- vcm->cfg.namespace_secret = tmp;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured namespace secret "
- "(%lu) from "
- VPPCOM_ENV_APP_NAMESPACE_SECRET
- "!", getpid (), vcm->cfg.namespace_secret);
- }
- }
- if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
- {
- vcm->cfg.app_proxy_transport_tcp = 1;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
- "(%u) from "
- VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
- "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
- }
- if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
- {
- vcm->cfg.app_proxy_transport_udp = 1;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
- "(%u) from "
- VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
- "!", getpid (), vcm->cfg.app_proxy_transport_udp);
- }
- if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
- {
- vcm->cfg.app_scope_local = 1;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
- VPPCOM_ENV_APP_SCOPE_LOCAL
- "!", getpid (), vcm->cfg.app_scope_local);
- }
- if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
- {
- vcm->cfg.app_scope_global = 1;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
- VPPCOM_ENV_APP_SCOPE_GLOBAL
- "!", getpid (), vcm->cfg.app_scope_global);
- }
-
- vcm->main_cpu = os_get_thread_index ();
- heap = clib_mem_get_per_cpu_heap ();
- h = mheap_header (heap);
-
- /* make the main heap thread-safe */
- h->flags |= MHEAP_FLAG_THREAD_SAFE;
-
- vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
-
- clib_time_init (&vcm->clib_time);
- vppcom_init_error_string_table ();
- svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
- 20 /* timeout in secs */ );
- clib_spinlock_init (&vcm->sessions_lockp);
- }
-
- if (vcm->my_client_index == ~0)
- {
-
- /* API hookup and connect to VPP */
- vppcom_api_hookup ();
- vcm->app_state = STATE_APP_START;
- rv = vppcom_connect_to_vpp (app_name);
- if (rv)
- {
- clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
- getpid ());
- return rv;
- }
-
- /* State event handling thread */
-
- rv = vce_start_event_thread (&(vcm->event_thread), 20);
-
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: sending session enable", getpid ());
-
- rv = vppcom_app_session_enable ();
- if (rv)
- {
- clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
- "failed!", getpid ());
- return rv;
- }
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: sending app attach", getpid ());
-
- rv = vppcom_app_attach ();
- if (rv)
- {
- clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
- getpid ());
- return rv;
- }
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
- getpid (), app_name, vcm->my_client_index,
- vcm->my_client_index);
- }
-
- return VPPCOM_OK;
-}
-
-void
-vppcom_app_destroy (void)
-{
- int rv;
-
- if (vcm->my_client_index == ~0)
- return;
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
- getpid (), vcm->my_client_index, vcm->my_client_index);
-
- if (VPPCOM_DEBUG > 0)
- {
- /* *INDENT-OFF* */
- ELOG_TYPE_DECLARE (e) =
- {
- .format = "app_detach:C:%d",
- .format_args = "i4",
- };
-
- struct
- {
- u32 data;
- } *ed;
- ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
- ed->data = vcm->my_client_index;
- /* *INDENT-ON* */
- }
-
- vppcom_app_detach ();
- rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
- if (PREDICT_FALSE (rv))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: application detach timed out! "
- "returning %d (%s)",
- getpid (), rv, vppcom_retval_str (rv));
- }
-
- /* Finished with logging before client gets reset to ~0 */
- if (VPPCOM_DEBUG > 0)
- write_elog ();
-
- vl_client_disconnect_from_vlib ();
- vcm->my_client_index = ~0;
- vcm->app_state = STATE_APP_START;
-}
-
-int
-vppcom_session_create (u8 proto, u8 is_nonblocking)
-{
- session_t *session;
- u32 session_index;
- session_state_t state;
- elog_track_t session_elog_track;
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- pool_get (vcm->sessions, session);
- memset (session, 0, sizeof (*session));
- session_index = session - vcm->sessions;
-
- session->proto = proto;
- session->state = STATE_START;
- state = session->state;
- session->vpp_handle = ~0;
-
- if (is_nonblocking)
- VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
- else
- VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
-
- if (VPPCOM_DEBUG > 0)
- {
- session->elog_track.name =
- (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
- session_index, 0);
- elog_track_register (&vcm->elog_main, &session->elog_track);
- session_elog_track = session->elog_track;
- }
-
- clib_spinlock_unlock (&vcm->sessions_lockp);
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
-
- if (VPPCOM_DEBUG > 0)
- {
- /* *INDENT-OFF* */
- ELOG_TYPE_DECLARE (e) =
- {
- .format = "session_create:proto:%d state:%d is_nonblocking:%d",
- .format_args = "i4i4i4",
- };
-
- struct
- {
- u32 data[3];
- } *ed;
-
- ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
- ed->data[0] = proto;
- ed->data[1] = state;
- ed->data[2] = is_nonblocking;
- /* *INDENT-ON* */
- }
-
- return (int) session_index;
-}
-
-int
-vppcom_session_close (uint32_t session_index)
-{
- session_t *session = 0;
- int rv;
- u8 is_vep;
- u8 is_vep_session;
- u32 next_sid;
- u32 vep_idx;
- u64 vpp_handle;
- uword *p;
- session_state_t state;
- elog_track_t session_elog_track;
-
- VCL_LOCK_AND_GET_SESSION (session_index, &session);
- 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;
- vpp_handle = session->vpp_handle;
- clib_spinlock_unlock (&vcm->sessions_lockp);
-
- /*
- * Why two if(VPPCOM_DEBUG) checks?
- *
- * Eventually all clib_warnings need their own way of being
- * logged and signalled (like severity) where event logging
- * is a separate debugging tool. It will make the separation
- * easier. ... parting is such sweet sorrow ...
- */
- if (VPPCOM_DEBUG > 0)
- {
- session_elog_track = session->elog_track;
- }
-
- if (VPPCOM_DEBUG > 0)
- {
- if (is_vep)
- clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
- "closing epoll session...",
- getpid (), session_index, session_index);
- else
- clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
- "closing session...",
- getpid (), vpp_handle, session_index);
- }
-
- if (is_vep)
- {
- while (next_sid != ~0)
- {
- rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
- if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
- clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
- "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
- getpid (), vpp_handle, next_sid, vep_idx,
- rv, vppcom_retval_str (rv));
-
- VCL_LOCK_AND_GET_SESSION (session_index, &session);
- next_sid = session->vep.next_sid;
- clib_spinlock_unlock (&vcm->sessions_lockp);
- }
- }
- else
- {
- if (is_vep_session)