2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vcl/vcl_private.h>
18 /* NOTE: _vppcom_main is only used until the heap is allocated.
19 * Do not access it directly -- use vcm which will point to
20 * the heap allocated copy after init.
22 static vppcom_main_t _vppcom_main = {
23 .debug = VPPCOM_DEBUG_INIT,
29 vppcom_main_t *vcm = &_vppcom_main;
32 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
36 vcl_cfg->heapsize = (256ULL << 20);
37 vcl_cfg->max_workers = 16;
38 vcl_cfg->vpp_api_q_length = 1024;
39 vcl_cfg->segment_baseva = 0x200000000ULL;
40 vcl_cfg->segment_size = (256 << 20);
41 vcl_cfg->add_segment_size = (128 << 20);
42 vcl_cfg->preallocated_fifo_pairs = 8;
43 vcl_cfg->rx_fifo_size = (1 << 20);
44 vcl_cfg->tx_fifo_size = (1 << 20);
45 vcl_cfg->event_queue_size = 2048;
46 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
47 vcl_cfg->app_timeout = 10 * 60.0;
48 vcl_cfg->session_timeout = 10 * 60.0;
49 vcl_cfg->accept_timeout = 60.0;
50 vcl_cfg->event_ring_size = (128 << 10);
51 vcl_cfg->event_log_path = "/dev/shm";
54 #define VCFG_DBG(_lvl, _fmt, _args...) \
56 if (vcm->debug > _lvl) \
57 fprintf (stderr, _fmt "\n", ##_args); \
60 vppcom_cfg_heapsize (char *conf_fname)
62 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
75 fp = fopen (conf_fname, "r");
78 VCFG_DBG (0, "VCL<%d>: using default heapsize %lu (0x%lx)",
79 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
83 argv = calloc (1, sizeof (char *));
86 VCFG_DBG (0, "VCL<%d>: calloc failed, using default heapsize %lu"
87 " (0x%lx)", getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
93 if (fgets (inbuf, 4096, fp) == 0)
95 p = strtok (inbuf, " \t\n");
101 char **tmp = realloc (argv, argc * sizeof (char *));
104 VCFG_DBG (0, "VCL<%d>: realloc failed, using default "
105 "heapsize %lu (0x%lx)", getpid (),
106 vcl_cfg->heapsize, vcl_cfg->heapsize);
110 arg = strndup (p, 1024);
113 VCFG_DBG (0, "VCL<%d>: strndup failed, using default "
114 "heapsize %ld (0x%lx)", getpid (),
115 vcl_cfg->heapsize, vcl_cfg->heapsize);
118 argv[argc - 1] = arg;
119 p = strtok (NULL, " \t\n");
126 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
129 VCFG_DBG (0, "VCL<%d>: realloc failed, using default heapsize %ld "
130 "(0x%lx)", getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
137 * Look for and parse the "heapsize" config parameter.
138 * Manual since none of the clib infra has been bootstrapped yet.
140 * Format: heapsize <nn>[mM][gG]
143 for (i = 1; i < (argc - 1); i++)
145 if (!strncmp (argv[i], "heapsize", 8))
147 sizep = (u8 *) argv[i + 1];
149 while (*sizep >= '0' && *sizep <= '9')
152 size += *sizep++ - '0';
156 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
157 "heapsize %ld (0x%lx)", getpid (), argv[i],
158 argv[i + 1], vcl_cfg->heapsize, vcl_cfg->heapsize);
162 if (*sizep == 'g' || *sizep == 'G')
163 vcl_cfg->heapsize = size << 30;
164 else if (*sizep == 'm' || *sizep == 'M')
165 vcl_cfg->heapsize = size << 20;
168 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
169 "heapsize %ld (0x%lx)", getpid (), argv[i],
170 argv[i + 1], vcl_cfg->heapsize, vcl_cfg->heapsize);
183 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
184 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
185 if (vcl_mem == MAP_FAILED)
187 VCFG_DBG (0, "VCL<%d>: ERROR: mmap(0, %ld == 0x%lx, "
188 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
189 "-1, 0) failed!", getpid (), vcl_cfg->heapsize,
191 ASSERT (vcl_mem != MAP_FAILED);
194 heap = clib_mem_init_thread_safe (vcl_mem, vcl_cfg->heapsize);
197 fprintf (stderr, "VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
201 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
204 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
209 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
212 VCFG_DBG (0, "VCL<%d>: allocated VCL heap = %p, size %ld (0x%lx)",
213 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
217 vppcom_cfg_read_file (char *conf_fname)
219 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
221 unformat_input_t _input, *input = &_input;
222 unformat_input_t _line_input, *line_input = &_line_input;
223 u8 vc_cfg_input = 0, *chroot_path;
227 fd = open (conf_fname, O_RDONLY);
230 VCFG_DBG (0, "VCL<%d>: using default configuration.", getpid ());
234 if (fstat (fd, &s) < 0)
236 VCFG_DBG (0, "VCL<%d>: failed to stat `%s' using default configuration",
237 getpid (), conf_fname);
241 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
243 VCFG_DBG (0, "VCL<%d>: not a regular file `%s', using default "
244 "configuration", getpid (), conf_fname);
248 unformat_init_clib_file (input, fd);
250 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
252 (void) unformat_user (input, unformat_line_input, line_input);
253 unformat_skip_white_space (line_input);
255 if (unformat (line_input, "vcl {"))
258 unformat_free (line_input);
264 if (unformat (line_input, "heapsize %U", unformat_memory_size,
267 VCFG_DBG (0, "VCL<%d>: configured heapsize %lu", getpid (),
272 (line_input, "max-workers %u", &vcl_cfg->max_workers))
274 VCFG_DBG (0, "VCL<%d>: configured max-workers %u", getpid (),
275 vcl_cfg->max_workers);
277 else if (unformat (line_input, "api-prefix %s", &chroot_path))
279 vec_terminate_c_string (chroot_path);
280 if (vcl_cfg->vpp_api_filename)
281 vec_free (vcl_cfg->vpp_api_filename);
282 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
284 vl_set_memory_root_path ((char *) chroot_path);
286 VCFG_DBG (0, "VCL<%d>: configured api-prefix (%s) and api "
287 "filename (%s)", getpid (), chroot_path,
288 vcl_cfg->vpp_api_filename);
289 chroot_path = 0; /* Don't vec_free() it! */
291 else if (unformat (line_input, "api-socket-name %s",
292 &vcl_cfg->vpp_api_socket_name))
294 vec_terminate_c_string (vcl_cfg->vpp_api_socket_name);
295 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)",
296 getpid (), vcl_cfg->vpp_api_socket_name);
298 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
300 if (q_len < vcl_cfg->vpp_api_q_length)
303 "VCL<%d>: ERROR: configured vpp-api-q-length "
304 "(%u) is too small! Using default: %u ", getpid (),
305 q_len, vcl_cfg->vpp_api_q_length);
309 vcl_cfg->vpp_api_q_length = q_len;
311 VCFG_DBG (0, "VCL<%d>: configured vpp-api-q-length %u",
312 getpid (), vcl_cfg->vpp_api_q_length);
315 else if (unformat (line_input, "uid %d", &uid))
317 vl_set_memory_uid (uid);
318 VCFG_DBG (0, "VCL<%d>: configured uid %d", getpid (), uid);
320 else if (unformat (line_input, "gid %d", &gid))
322 vl_set_memory_gid (gid);
323 VCFG_DBG (0, "VCL<%d>: configured gid %d", getpid (), gid);
325 else if (unformat (line_input, "segment-baseva 0x%x",
326 &vcl_cfg->segment_baseva))
328 VCFG_DBG (0, "VCL<%d>: configured segment_baseva 0x%lx",
329 getpid (), vcl_cfg->segment_baseva);
331 else if (unformat (line_input, "segment-size 0x%x",
332 &vcl_cfg->segment_size))
334 VCFG_DBG (0, "VCL<%d>: configured segment_size 0x%x (%d)",
335 getpid (), vcl_cfg->segment_size,
336 vcl_cfg->segment_size);
338 else if (unformat (line_input, "segment-size %d",
339 &vcl_cfg->segment_size))
341 VCFG_DBG (0, "VCL<%d>: configured segment_size %d (0x%x)",
342 getpid (), vcl_cfg->segment_size,
343 vcl_cfg->segment_size);
345 else if (unformat (line_input, "add-segment-size 0x%x",
346 &vcl_cfg->add_segment_size))
348 VCFG_DBG (0, "VCL<%d>: configured add_segment_size 0x%x (%d)",
349 getpid (), vcl_cfg->add_segment_size,
350 vcl_cfg->add_segment_size);
352 else if (unformat (line_input, "add-segment-size %d",
353 &vcl_cfg->add_segment_size))
355 VCFG_DBG (0, "VCL<%d>: configured add_segment_size %d (0x%x)",
356 getpid (), vcl_cfg->add_segment_size,
357 vcl_cfg->add_segment_size);
359 else if (unformat (line_input, "preallocated-fifo-pairs %d",
360 &vcl_cfg->preallocated_fifo_pairs))
362 VCFG_DBG (0, "VCL<%d>: configured preallocated_fifo_pairs %d "
363 "(0x%x)", getpid (), vcl_cfg->preallocated_fifo_pairs,
364 vcl_cfg->preallocated_fifo_pairs);
366 else if (unformat (line_input, "rx-fifo-size 0x%lx",
367 &vcl_cfg->rx_fifo_size))
369 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size 0x%x (%d)",
370 getpid (), vcl_cfg->rx_fifo_size,
371 vcl_cfg->rx_fifo_size);
373 else if (unformat (line_input, "rx-fifo-size %d",
374 &vcl_cfg->rx_fifo_size))
376 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size %d (0x%x)",
377 getpid (), vcl_cfg->rx_fifo_size,
378 vcl_cfg->rx_fifo_size);
380 else if (unformat (line_input, "tx-fifo-size 0x%lx",
381 &vcl_cfg->tx_fifo_size))
383 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size 0x%x (%d)",
384 getpid (), vcl_cfg->tx_fifo_size,
385 vcl_cfg->tx_fifo_size);
387 else if (unformat (line_input, "tx-fifo-size %ld",
388 &vcl_cfg->tx_fifo_size))
390 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size %d (0x%x)",
391 getpid (), vcl_cfg->tx_fifo_size,
392 vcl_cfg->tx_fifo_size);
394 else if (unformat (line_input, "event-queue-size 0x%lx",
395 &vcl_cfg->event_queue_size))
397 VCFG_DBG (0, "VCL<%d>: configured event_queue_size 0x%x (%d)",
398 getpid (), vcl_cfg->event_queue_size,
399 vcl_cfg->event_queue_size);
401 else if (unformat (line_input, "event-queue-size %ld",
402 &vcl_cfg->event_queue_size))
404 VCFG_DBG (0, "VCL<%d>: configured event_queue_size %d (0x%x)",
405 getpid (), vcl_cfg->event_queue_size,
406 vcl_cfg->event_queue_size);
408 else if (unformat (line_input, "listen-queue-size 0x%lx",
409 &vcl_cfg->listen_queue_size))
411 VCFG_DBG (0, "VCL<%d>: configured listen_queue_size 0x%x (%u)",
412 getpid (), vcl_cfg->listen_queue_size,
413 vcl_cfg->listen_queue_size);
415 else if (unformat (line_input, "listen-queue-size %ld",
416 &vcl_cfg->listen_queue_size))
418 VCFG_DBG (0, "VCL<%d>: configured listen_queue_size %u (0x%x)",
419 getpid (), vcl_cfg->listen_queue_size,
420 vcl_cfg->listen_queue_size);
422 else if (unformat (line_input, "app-timeout %f",
423 &vcl_cfg->app_timeout))
425 VCFG_DBG (0, "VCL<%d>: configured app_timeout %f",
426 getpid (), vcl_cfg->app_timeout);
428 else if (unformat (line_input, "session-timeout %f",
429 &vcl_cfg->session_timeout))
431 VCFG_DBG (0, "VCL<%d>: configured session_timeout %f",
432 getpid (), vcl_cfg->session_timeout);
434 else if (unformat (line_input, "accept-timeout %f",
435 &vcl_cfg->accept_timeout))
437 VCFG_DBG (0, "VCL<%d>: configured accept_timeout %f",
438 getpid (), vcl_cfg->accept_timeout);
440 else if (unformat (line_input, "app-proxy-transport-tcp"))
442 vcl_cfg->app_proxy_transport_tcp = 1;
443 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%d)",
444 getpid (), vcl_cfg->app_proxy_transport_tcp);
446 else if (unformat (line_input, "app-proxy-transport-udp"))
448 vcl_cfg->app_proxy_transport_udp = 1;
449 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%d)",
450 getpid (), vcl_cfg->app_proxy_transport_udp);
452 else if (unformat (line_input, "app-scope-local"))
454 vcl_cfg->app_scope_local = 1;
455 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%d)",
456 getpid (), vcl_cfg->app_scope_local);
458 else if (unformat (line_input, "app-scope-global"))
460 vcl_cfg->app_scope_global = 1;
461 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%d)",
462 getpid (), vcl_cfg->app_scope_global);
464 else if (unformat (line_input, "namespace-secret %lu",
465 &vcl_cfg->namespace_secret))
467 VCFG_DBG (0, "VCL<%d>: configured namespace_secret %lu (0x%lx)",
468 getpid (), vcl_cfg->namespace_secret,
469 vcl_cfg->namespace_secret);
471 else if (unformat (line_input, "namespace-id %v",
472 &vcl_cfg->namespace_id))
474 u32 max_nsid_vec_len = vcl_max_nsid_len ();
475 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
476 if (nsid_vec_len > max_nsid_vec_len)
478 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
479 VCFG_DBG (0, "VCL<%d>: configured namespace_id is too long,"
480 " truncated to %d characters!",
481 getpid (), max_nsid_vec_len);
484 VCFG_DBG (0, "VCL<%d>: configured namespace_id %s",
485 getpid (), (char *) vcl_cfg->namespace_id);
487 else if (unformat (line_input, "use-mq-eventfd"))
489 vcl_cfg->use_mq_eventfd = 1;
490 VCFG_DBG (0, "VCL<%d>: configured with mq with eventfd",
493 else if (unformat (line_input, "}"))
496 VCFG_DBG (0, "VCL<%d>: completed parsing vppcom config!",
498 unformat_free (line_input);
503 if (line_input->buffer[line_input->index] != '#')
505 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
507 &line_input->buffer[line_input->index]);
510 unformat_free (line_input);
515 unformat_free (input);
523 vppcom_cfg (vppcom_cfg_t * vcl_cfg)
525 char *conf_fname, *env_var_str;
527 vppcom_cfg_init (vcl_cfg);
528 env_var_str = getenv (VPPCOM_ENV_DEBUG);
532 if (sscanf (env_var_str, "%u", &tmp) != 1)
534 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid debug level specified "
535 "in the environment variable " VPPCOM_ENV_DEBUG
536 " (%s)!\n", getpid (), env_var_str);
541 VCFG_DBG (0, "VCL<%d>: configured VCL debug level (%u) from "
542 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
545 conf_fname = getenv (VPPCOM_ENV_CONF);
547 conf_fname = VPPCOM_CONF_DEFAULT;
548 vppcom_cfg_heapsize (conf_fname);
549 vppcom_cfg_read_file (conf_fname);
551 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
554 if (vcl_cfg->vpp_api_filename)
555 vec_free (vcl_cfg->vpp_api_filename);
556 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c", env_var_str, 0);
557 vl_set_memory_root_path ((char *) env_var_str);
559 VCFG_DBG (0, "VCL<%d>: configured api prefix (%s) and filename (%s) "
560 "from " VPPCOM_ENV_API_PREFIX "!", getpid (), env_var_str,
561 vcl_cfg->vpp_api_filename);
563 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
566 u32 ns_id_vec_len = strlen (env_var_str);
568 vec_reset_length (vcm->cfg.namespace_id);
569 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
570 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
572 VCFG_DBG (0, "VCL<%d>: configured namespace_id (%s) from "
573 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
574 (char *) vcm->cfg.namespace_id);
576 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
580 if (sscanf (env_var_str, "%lu", &tmp) != 1)
582 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid namespace secret specified"
583 " in the environment variable "
584 VPPCOM_ENV_APP_NAMESPACE_SECRET " (%s)!\n", getpid (),
589 vcm->cfg.namespace_secret = tmp;
590 VCFG_DBG (0, "VCL<%d>: configured namespace secret (%lu) from "
591 VPPCOM_ENV_APP_NAMESPACE_SECRET "!", getpid (),
592 vcm->cfg.namespace_secret);
595 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
597 vcm->cfg.app_proxy_transport_tcp = 1;
598 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%u) from "
599 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
600 vcm->cfg.app_proxy_transport_tcp);
602 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
604 vcm->cfg.app_proxy_transport_udp = 1;
605 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%u) from "
606 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
607 vcm->cfg.app_proxy_transport_udp);
609 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
611 vcm->cfg.app_scope_local = 1;
612 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%u) from "
613 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
614 vcm->cfg.app_scope_local);
616 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
618 vcm->cfg.app_scope_global = 1;
619 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%u) from "
620 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
621 vcm->cfg.app_scope_global);
623 env_var_str = getenv (VPPCOM_ENV_VPP_API_SOCKET);
626 vcm->cfg.vpp_api_socket_name = format (0, "%s%c", env_var_str, 0);
627 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)", getpid (),
628 vcl_cfg->vpp_api_socket_name);
633 * fd.io coding-style-patch-verification: ON
636 * eval: (c-set-style "gnu")