2 * Copyright (c) 2018-2019 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 vppcom_main_t _vppcom_main = {
23 .debug = VPPCOM_DEBUG_INIT,
28 vppcom_main_t *vcm = &_vppcom_main;
31 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
35 vcl_cfg->heapsize = (256ULL << 20);
36 vcl_cfg->max_workers = 16;
37 vcl_cfg->segment_baseva = HIGH_SEGMENT_BASEVA;
38 vcl_cfg->segment_size = (256 << 20);
39 vcl_cfg->add_segment_size = (128 << 20);
40 vcl_cfg->preallocated_fifo_pairs = 8;
41 vcl_cfg->rx_fifo_size = (1 << 20);
42 vcl_cfg->tx_fifo_size = (1 << 20);
43 vcl_cfg->event_queue_size = 2048;
44 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
45 vcl_cfg->app_timeout = 10 * 60.0;
46 vcl_cfg->session_timeout = 10 * 60.0;
47 vcl_cfg->accept_timeout = 60.0;
48 vcl_cfg->event_ring_size = (128 << 10);
49 vcl_cfg->event_log_path = "/dev/shm";
52 #define VCFG_DBG(_lvl, _fmt, _args...) \
54 if (vcm->debug > _lvl) \
55 fprintf (stderr, _fmt "\n", ##_args); \
58 vppcom_cfg_heapsize (char *conf_fname)
60 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
73 fp = fopen (conf_fname, "r");
76 VCFG_DBG (0, "VCL<%d>: using default heapsize %lu (0x%lx)",
77 getpid (), (unsigned long) vcl_cfg->heapsize,
78 (unsigned long) vcl_cfg->heapsize);
82 argv = calloc (1, sizeof (char *));
85 VCFG_DBG (0, "VCL<%d>: calloc failed, using default heapsize %lu"
86 " (0x%lx)", getpid (), (unsigned long) vcl_cfg->heapsize,
87 (unsigned long) 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 (unsigned long) vcl_cfg->heapsize,
107 (unsigned long) vcl_cfg->heapsize);
111 arg = strndup (p, 1024);
114 VCFG_DBG (0, "VCL<%d>: strndup failed, using default "
115 "heapsize %lu (0x%lx)", getpid (),
116 (unsigned long) vcl_cfg->heapsize,
117 (unsigned long) vcl_cfg->heapsize);
120 argv[argc - 1] = arg;
121 p = strtok (NULL, " \t\n");
128 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
131 VCFG_DBG (0, "VCL<%d>: realloc failed, using default heapsize %lu "
132 "(0x%lx)", getpid (), (unsigned long) vcl_cfg->heapsize,
133 (unsigned long) vcl_cfg->heapsize);
140 * Look for and parse the "heapsize" config parameter.
141 * Manual since none of the clib infra has been bootstrapped yet.
143 * Format: heapsize <nn>[mM][gG]
146 for (i = 1; i < (argc - 1); i++)
148 if (!strncmp (argv[i], "heapsize", 8))
150 sizep = (u8 *) argv[i + 1];
152 while (*sizep >= '0' && *sizep <= '9')
155 size += *sizep++ - '0';
159 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
160 "heapsize %lu (0x%lx)", getpid (), argv[i],
161 argv[i + 1], (unsigned long) vcl_cfg->heapsize,
162 (unsigned long) vcl_cfg->heapsize);
166 if (*sizep == 'g' || *sizep == 'G')
167 vcl_cfg->heapsize = size << 30;
168 else if (*sizep == 'm' || *sizep == 'M')
169 vcl_cfg->heapsize = size << 20;
172 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
173 "heapsize %lu (0x%lx)", getpid (), argv[i],
174 argv[i + 1], (unsigned long) vcl_cfg->heapsize,
175 (unsigned long) vcl_cfg->heapsize);
188 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
189 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
190 if (vcl_mem == MAP_FAILED)
192 VCFG_DBG (0, "VCL<%d>: ERROR: mmap(0, %lu == 0x%lx, "
193 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
194 "-1, 0) failed!", getpid (),
195 (unsigned long) vcl_cfg->heapsize,
196 (unsigned long) vcl_cfg->heapsize);
197 ASSERT (vcl_mem != MAP_FAILED);
200 heap = clib_mem_init_thread_safe (vcl_mem, vcl_cfg->heapsize);
203 fprintf (stderr, "VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
207 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
210 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
215 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
218 VCFG_DBG (0, "VCL<%d>: allocated VCL heap = %p, size %lu (0x%lx)",
219 getpid (), heap, (unsigned long) vcl_cfg->heapsize,
220 (unsigned long) vcl_cfg->heapsize);
224 vppcom_cfg_read_file (char *conf_fname)
226 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
228 unformat_input_t _input, *input = &_input;
229 unformat_input_t _line_input, *line_input = &_line_input;
234 fd = open (conf_fname, O_RDONLY);
237 VCFG_DBG (0, "VCL<%d>: using default configuration.", getpid ());
241 if (fstat (fd, &s) < 0)
243 VCFG_DBG (0, "VCL<%d>: failed to stat `%s' using default configuration",
244 getpid (), conf_fname);
248 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
250 VCFG_DBG (0, "VCL<%d>: not a regular file `%s', using default "
251 "configuration", getpid (), conf_fname);
255 unformat_init_clib_file (input, fd);
257 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
259 (void) unformat_user (input, unformat_line_input, line_input);
260 unformat_skip_white_space (line_input);
262 if (unformat (line_input, "vcl {"))
265 unformat_free (line_input);
271 if (unformat (line_input, "heapsize %U", unformat_memory_size,
274 VCFG_DBG (0, "VCL<%d>: configured heapsize %lu", getpid (),
275 (unsigned long) vcl_cfg->heapsize);
279 (line_input, "max-workers %u", &vcl_cfg->max_workers))
281 VCFG_DBG (0, "VCL<%d>: configured max-workers %u", getpid (),
282 vcl_cfg->max_workers);
284 else if (unformat (line_input, "api-socket-name %s",
285 &vcl_cfg->vpp_bapi_socket_name))
287 vec_terminate_c_string (vcl_cfg->vpp_bapi_socket_name);
288 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)",
289 getpid (), vcl_cfg->vpp_bapi_socket_name);
291 else if (unformat (line_input, "app-socket-api %s",
292 &vcl_cfg->vpp_app_socket_api))
294 vec_terminate_c_string (vcl_cfg->vpp_app_socket_api);
295 VCFG_DBG (0, "VCL<%d>: configured app-socket-api (%s)",
296 getpid (), vcl_cfg->vpp_app_socket_api);
298 else if (unformat (line_input, "uid %d", &uid))
300 vl_set_memory_uid (uid);
301 VCFG_DBG (0, "VCL<%d>: configured uid %d", getpid (), uid);
303 else if (unformat (line_input, "gid %d", &gid))
305 vl_set_memory_gid (gid);
306 VCFG_DBG (0, "VCL<%d>: configured gid %d", getpid (), gid);
308 else if (unformat (line_input, "segment-baseva 0x%lx",
309 &vcl_cfg->segment_baseva))
311 VCFG_DBG (0, "VCL<%d>: configured segment_baseva 0x%lx",
312 getpid (), (unsigned long) vcl_cfg->segment_baseva);
314 else if (unformat (line_input, "segment-size 0x%lx",
315 &vcl_cfg->segment_size))
317 VCFG_DBG (0, "VCL<%d>: configured segment_size 0x%lx (%lu)",
318 getpid (), vcl_cfg->segment_size,
319 vcl_cfg->segment_size);
321 else if (unformat (line_input, "segment-size %lu",
322 &vcl_cfg->segment_size))
324 VCFG_DBG (0, "VCL<%d>: configured segment_size %lu (0x%lx)",
325 getpid (), vcl_cfg->segment_size,
326 vcl_cfg->segment_size);
328 else if (unformat (line_input, "add-segment-size 0x%lx",
329 &vcl_cfg->add_segment_size))
331 VCFG_DBG (0, "VCL<%d>: configured add_segment_size 0x%lx (%lu)",
332 getpid (), vcl_cfg->add_segment_size,
333 vcl_cfg->add_segment_size);
335 else if (unformat (line_input, "add-segment-size %lu",
336 &vcl_cfg->add_segment_size))
338 VCFG_DBG (0, "VCL<%d>: configured add_segment_size %lu (0x%lx)",
339 getpid (), vcl_cfg->add_segment_size,
340 vcl_cfg->add_segment_size);
342 else if (unformat (line_input, "preallocated-fifo-pairs %u",
343 &vcl_cfg->preallocated_fifo_pairs))
345 VCFG_DBG (0, "VCL<%d>: configured preallocated_fifo_pairs %u "
346 "(0x%x)", getpid (), vcl_cfg->preallocated_fifo_pairs,
347 vcl_cfg->preallocated_fifo_pairs);
349 else if (unformat (line_input, "rx-fifo-size 0x%x",
350 &vcl_cfg->rx_fifo_size))
352 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size 0x%x (%u)",
353 getpid (), vcl_cfg->rx_fifo_size,
354 vcl_cfg->rx_fifo_size);
356 else if (unformat (line_input, "rx-fifo-size %u",
357 &vcl_cfg->rx_fifo_size))
359 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size %u (0x%x)",
360 getpid (), vcl_cfg->rx_fifo_size,
361 vcl_cfg->rx_fifo_size);
363 else if (unformat (line_input, "tx-fifo-size 0x%x",
364 &vcl_cfg->tx_fifo_size))
366 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size 0x%x (%u)",
367 getpid (), vcl_cfg->tx_fifo_size,
368 vcl_cfg->tx_fifo_size);
370 else if (unformat (line_input, "tx-fifo-size %u",
371 &vcl_cfg->tx_fifo_size))
373 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size %u (0x%x)",
374 getpid (), vcl_cfg->tx_fifo_size,
375 vcl_cfg->tx_fifo_size);
377 else if (unformat (line_input, "event-queue-size 0x%x",
378 &vcl_cfg->event_queue_size))
380 VCFG_DBG (0, "VCL<%d>: configured event_queue_size 0x%x (%u)",
381 getpid (), vcl_cfg->event_queue_size,
382 vcl_cfg->event_queue_size);
384 else if (unformat (line_input, "event-queue-size %u",
385 &vcl_cfg->event_queue_size))
387 VCFG_DBG (0, "VCL<%d>: configured event_queue_size %u (0x%x)",
388 getpid (), vcl_cfg->event_queue_size,
389 vcl_cfg->event_queue_size);
391 else if (unformat (line_input, "listen-queue-size 0x%x",
392 &vcl_cfg->listen_queue_size))
394 VCFG_DBG (0, "VCL<%d>: configured listen_queue_size 0x%x (%u)",
395 getpid (), vcl_cfg->listen_queue_size,
396 vcl_cfg->listen_queue_size);
398 else if (unformat (line_input, "listen-queue-size %u",
399 &vcl_cfg->listen_queue_size))
401 VCFG_DBG (0, "VCL<%d>: configured listen_queue_size %u (0x%x)",
402 getpid (), vcl_cfg->listen_queue_size,
403 vcl_cfg->listen_queue_size);
405 else if (unformat (line_input, "app-timeout %f",
406 &vcl_cfg->app_timeout))
408 VCFG_DBG (0, "VCL<%d>: configured app_timeout %f",
409 getpid (), vcl_cfg->app_timeout);
411 else if (unformat (line_input, "session-timeout %f",
412 &vcl_cfg->session_timeout))
414 VCFG_DBG (0, "VCL<%d>: configured session_timeout %f",
415 getpid (), vcl_cfg->session_timeout);
417 else if (unformat (line_input, "accept-timeout %f",
418 &vcl_cfg->accept_timeout))
420 VCFG_DBG (0, "VCL<%d>: configured accept_timeout %f",
421 getpid (), vcl_cfg->accept_timeout);
423 else if (unformat (line_input, "app-proxy-transport-tcp"))
425 vcl_cfg->app_proxy_transport_tcp = 1;
426 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%d)",
427 getpid (), vcl_cfg->app_proxy_transport_tcp);
429 else if (unformat (line_input, "app-proxy-transport-udp"))
431 vcl_cfg->app_proxy_transport_udp = 1;
432 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%d)",
433 getpid (), vcl_cfg->app_proxy_transport_udp);
435 else if (unformat (line_input, "app-scope-local"))
437 vcl_cfg->app_scope_local = 1;
438 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%d)",
439 getpid (), vcl_cfg->app_scope_local);
441 else if (unformat (line_input, "app-scope-global"))
443 vcl_cfg->app_scope_global = 1;
444 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%d)",
445 getpid (), vcl_cfg->app_scope_global);
447 else if (unformat (line_input, "namespace-secret %lu",
448 &vcl_cfg->namespace_secret))
450 VCFG_DBG (0, "VCL<%d>: configured namespace_secret %llu "
451 "(0x%llx)", getpid (),
452 (unsigned long long) vcl_cfg->namespace_secret,
453 (unsigned long long) vcl_cfg->namespace_secret);
455 else if (unformat (line_input, "namespace-id %v",
456 &vcl_cfg->namespace_id))
458 u32 max_nsid_vec_len = vcl_bapi_max_nsid_len ();
459 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
460 if (nsid_vec_len > max_nsid_vec_len)
462 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
463 VCFG_DBG (0, "VCL<%d>: configured namespace_id is too long,"
464 " truncated to %d characters!",
465 getpid (), max_nsid_vec_len);
468 VCFG_DBG (0, "VCL<%d>: configured namespace_id %s",
469 getpid (), (char *) vcl_cfg->namespace_id);
471 else if (unformat (line_input, "use-mq-eventfd"))
473 vcl_cfg->use_mq_eventfd = 1;
474 VCFG_DBG (0, "VCL<%d>: configured with mq with eventfd",
477 else if (unformat (line_input, "tls-engine %u",
478 &vcl_cfg->tls_engine))
480 VCFG_DBG (0, "VCL<%d>: configured tls-engine %u (0x%x)",
481 getpid (), vcl_cfg->tls_engine, vcl_cfg->tls_engine);
483 else if (unformat (line_input, "multi-thread-workers"))
485 vcl_cfg->mt_wrk_supported = 1;
486 VCFG_DBG (0, "VCL<%d>: configured with multithread workers",
489 else if (unformat (line_input, "}"))
492 VCFG_DBG (0, "VCL<%d>: completed parsing vppcom config!",
494 unformat_free (line_input);
499 if (line_input->buffer[line_input->index] != '#')
501 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
503 &line_input->buffer[line_input->index]);
506 unformat_free (line_input);
511 unformat_free (input);
519 vppcom_cfg (vppcom_cfg_t * vcl_cfg)
521 char *conf_fname, *env_var_str;
523 vppcom_cfg_init (vcl_cfg);
524 env_var_str = getenv (VPPCOM_ENV_DEBUG);
528 if (sscanf (env_var_str, "%u", &tmp) != 1)
530 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid debug level specified "
531 "in the environment variable " VPPCOM_ENV_DEBUG
532 " (%s)!\n", getpid (), env_var_str);
537 VCFG_DBG (0, "VCL<%d>: configured VCL debug level (%u) from "
538 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
541 conf_fname = getenv (VPPCOM_ENV_CONF);
543 conf_fname = VPPCOM_CONF_DEFAULT;
544 vppcom_cfg_heapsize (conf_fname);
545 vppcom_cfg_read_file (conf_fname);
547 /* Regrab cfg after heap initialization */
549 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
552 u32 ns_id_vec_len = strlen (env_var_str);
554 vec_reset_length (vcm->cfg.namespace_id);
555 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
556 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
558 VCFG_DBG (0, "VCL<%d>: configured namespace_id (%s) from "
559 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (), env_var_str);
561 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
565 if (sscanf (env_var_str, "%llu", (unsigned long long *) &tmp) != 1)
567 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid namespace secret specified"
568 " in the environment variable "
569 VPPCOM_ENV_APP_NAMESPACE_SECRET " (%s)!\n", getpid (),
574 vcm->cfg.namespace_secret = tmp;
575 VCFG_DBG (0, "VCL<%d>: configured namespace secret (%llu) from "
576 VPPCOM_ENV_APP_NAMESPACE_SECRET "!", getpid (),
577 (unsigned long long) vcm->cfg.namespace_secret);
580 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
582 vcm->cfg.app_proxy_transport_tcp = 1;
583 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%u) from "
584 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
585 vcm->cfg.app_proxy_transport_tcp);
587 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
589 vcm->cfg.app_proxy_transport_udp = 1;
590 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%u) from "
591 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
592 vcm->cfg.app_proxy_transport_udp);
594 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
596 vcm->cfg.app_scope_local = 1;
597 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%u) from "
598 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
599 vcm->cfg.app_scope_local);
601 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
603 vcm->cfg.app_scope_global = 1;
604 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%u) from "
605 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
606 vcm->cfg.app_scope_global);
608 env_var_str = getenv (VPPCOM_ENV_VPP_API_SOCKET);
611 vcm->cfg.vpp_bapi_socket_name = format (0, "%s%c", env_var_str, 0);
612 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)", getpid (),
613 vcl_cfg->vpp_bapi_socket_name);
615 env_var_str = getenv (VPPCOM_ENV_VPP_SAPI_SOCKET);
618 vcm->cfg.vpp_app_socket_api = format (0, "%s%c", env_var_str, 0);
619 VCFG_DBG (0, "VCL<%d>: configured app-socket-api (%s)", getpid (),
620 vcl_cfg->vpp_app_socket_api);
625 * fd.io coding-style-patch-verification: ON
628 * eval: (c-set-style "gnu")