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_size = (256 << 20);
38 vcl_cfg->add_segment_size = (128 << 20);
39 vcl_cfg->preallocated_fifo_pairs = 8;
40 vcl_cfg->rx_fifo_size = (1 << 20);
41 vcl_cfg->tx_fifo_size = (1 << 20);
42 vcl_cfg->event_queue_size = 2048;
43 vcl_cfg->app_timeout = 10 * 60.0;
44 vcl_cfg->session_timeout = 10 * 60.0;
45 vcl_cfg->event_log_path = "/dev/shm";
48 #define VCFG_DBG(_lvl, _fmt, _args...) \
50 if (vcm->debug > _lvl) \
51 fprintf (stderr, _fmt "\n", ##_args); \
54 vppcom_cfg_heapsize (char *conf_fname)
56 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
69 fp = fopen (conf_fname, "r");
72 VCFG_DBG (0, "VCL<%d>: using default heapsize %lu (0x%lx)",
73 getpid (), (unsigned long) vcl_cfg->heapsize,
74 (unsigned long) vcl_cfg->heapsize);
78 argv = calloc (1, sizeof (char *));
81 VCFG_DBG (0, "VCL<%d>: calloc failed, using default heapsize %lu"
82 " (0x%lx)", getpid (), (unsigned long) vcl_cfg->heapsize,
83 (unsigned long) vcl_cfg->heapsize);
89 if (fgets (inbuf, 4096, fp) == 0)
91 p = strtok (inbuf, " \t\n");
97 char **tmp = realloc (argv, argc * sizeof (char *));
100 VCFG_DBG (0, "VCL<%d>: realloc failed, using default "
101 "heapsize %lu (0x%lx)", getpid (),
102 (unsigned long) vcl_cfg->heapsize,
103 (unsigned long) vcl_cfg->heapsize);
107 arg = strndup (p, 1024);
110 VCFG_DBG (0, "VCL<%d>: strndup failed, using default "
111 "heapsize %lu (0x%lx)", getpid (),
112 (unsigned long) vcl_cfg->heapsize,
113 (unsigned long) vcl_cfg->heapsize);
116 argv[argc - 1] = arg;
117 p = strtok (NULL, " \t\n");
124 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
127 VCFG_DBG (0, "VCL<%d>: realloc failed, using default heapsize %lu "
128 "(0x%lx)", getpid (), (unsigned long) vcl_cfg->heapsize,
129 (unsigned long) vcl_cfg->heapsize);
136 * Look for and parse the "heapsize" config parameter.
137 * Manual since none of the clib infra has been bootstrapped yet.
139 * Format: heapsize <nn>[mM][gG]
142 for (i = 1; i < (argc - 1); i++)
144 if (!strncmp (argv[i], "heapsize", 8))
146 sizep = (u8 *) argv[i + 1];
148 while (*sizep >= '0' && *sizep <= '9')
151 size += *sizep++ - '0';
155 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
156 "heapsize %lu (0x%lx)", getpid (), argv[i],
157 argv[i + 1], (unsigned long) vcl_cfg->heapsize,
158 (unsigned long) 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 %lu (0x%lx)", getpid (), argv[i],
170 argv[i + 1], (unsigned long) vcl_cfg->heapsize,
171 (unsigned long) vcl_cfg->heapsize);
184 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
185 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
186 if (vcl_mem == MAP_FAILED)
188 VCFG_DBG (0, "VCL<%d>: ERROR: mmap(0, %lu == 0x%lx, "
189 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
190 "-1, 0) failed!", getpid (),
191 (unsigned long) vcl_cfg->heapsize,
192 (unsigned long) vcl_cfg->heapsize);
193 ASSERT (vcl_mem != MAP_FAILED);
196 heap = clib_mem_init_thread_safe (vcl_mem, vcl_cfg->heapsize);
199 fprintf (stderr, "VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
203 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
206 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
211 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
214 VCFG_DBG (0, "VCL<%d>: allocated VCL heap = %p, size %lu (0x%lx)",
215 getpid (), heap, (unsigned long) vcl_cfg->heapsize,
216 (unsigned long) vcl_cfg->heapsize);
220 vppcom_cfg_read_file (char *conf_fname)
222 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
224 unformat_input_t _input, *input = &_input;
225 unformat_input_t _line_input, *line_input = &_line_input;
230 fd = open (conf_fname, O_RDONLY);
233 VCFG_DBG (0, "VCL<%d>: using default configuration.", getpid ());
237 if (fstat (fd, &s) < 0)
239 VCFG_DBG (0, "VCL<%d>: failed to stat `%s' using default configuration",
240 getpid (), conf_fname);
244 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
246 VCFG_DBG (0, "VCL<%d>: not a regular file `%s', using default "
247 "configuration", getpid (), conf_fname);
251 unformat_init_clib_file (input, fd);
253 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
255 (void) unformat_user (input, unformat_line_input, line_input);
256 unformat_skip_white_space (line_input);
258 if (unformat (line_input, "vcl {"))
261 unformat_free (line_input);
267 if (unformat (line_input, "heapsize %U", unformat_memory_size,
270 VCFG_DBG (0, "VCL<%d>: configured heapsize %lu", getpid (),
271 (unsigned long) vcl_cfg->heapsize);
275 (line_input, "max-workers %u", &vcl_cfg->max_workers))
277 VCFG_DBG (0, "VCL<%d>: configured max-workers %u", getpid (),
278 vcl_cfg->max_workers);
280 else if (unformat (line_input, "api-socket-name %s",
281 &vcl_cfg->vpp_bapi_socket_name))
283 vec_terminate_c_string (vcl_cfg->vpp_bapi_socket_name);
284 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)",
285 getpid (), vcl_cfg->vpp_bapi_socket_name);
287 else if (unformat (line_input, "app-socket-api %s",
288 &vcl_cfg->vpp_app_socket_api))
290 vec_terminate_c_string (vcl_cfg->vpp_app_socket_api);
291 VCFG_DBG (0, "VCL<%d>: configured app-socket-api (%s)",
292 getpid (), vcl_cfg->vpp_app_socket_api);
294 else if (unformat (line_input, "uid %d", &uid))
296 vl_set_memory_uid (uid);
297 VCFG_DBG (0, "VCL<%d>: configured uid %d", getpid (), uid);
299 else if (unformat (line_input, "gid %d", &gid))
301 vl_set_memory_gid (gid);
302 VCFG_DBG (0, "VCL<%d>: configured gid %d", getpid (), gid);
304 else if (unformat (line_input, "segment-size 0x%lx",
305 &vcl_cfg->segment_size))
307 VCFG_DBG (0, "VCL<%d>: configured segment_size 0x%lx (%lu)",
308 getpid (), vcl_cfg->segment_size,
309 vcl_cfg->segment_size);
311 else if (unformat (line_input, "segment-size %lu",
312 &vcl_cfg->segment_size))
314 VCFG_DBG (0, "VCL<%d>: configured segment_size %lu (0x%lx)",
315 getpid (), vcl_cfg->segment_size,
316 vcl_cfg->segment_size);
318 else if (unformat (line_input, "add-segment-size 0x%lx",
319 &vcl_cfg->add_segment_size))
321 VCFG_DBG (0, "VCL<%d>: configured add_segment_size 0x%lx (%lu)",
322 getpid (), vcl_cfg->add_segment_size,
323 vcl_cfg->add_segment_size);
325 else if (unformat (line_input, "add-segment-size %lu",
326 &vcl_cfg->add_segment_size))
328 VCFG_DBG (0, "VCL<%d>: configured add_segment_size %lu (0x%lx)",
329 getpid (), vcl_cfg->add_segment_size,
330 vcl_cfg->add_segment_size);
332 else if (unformat (line_input, "preallocated-fifo-pairs %u",
333 &vcl_cfg->preallocated_fifo_pairs))
335 VCFG_DBG (0, "VCL<%d>: configured preallocated_fifo_pairs %u "
336 "(0x%x)", getpid (), vcl_cfg->preallocated_fifo_pairs,
337 vcl_cfg->preallocated_fifo_pairs);
339 else if (unformat (line_input, "rx-fifo-size 0x%x",
340 &vcl_cfg->rx_fifo_size))
342 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size 0x%x (%u)",
343 getpid (), vcl_cfg->rx_fifo_size,
344 vcl_cfg->rx_fifo_size);
346 else if (unformat (line_input, "rx-fifo-size %u",
347 &vcl_cfg->rx_fifo_size))
349 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size %u (0x%x)",
350 getpid (), vcl_cfg->rx_fifo_size,
351 vcl_cfg->rx_fifo_size);
353 else if (unformat (line_input, "tx-fifo-size 0x%x",
354 &vcl_cfg->tx_fifo_size))
356 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size 0x%x (%u)",
357 getpid (), vcl_cfg->tx_fifo_size,
358 vcl_cfg->tx_fifo_size);
360 else if (unformat (line_input, "tx-fifo-size %u",
361 &vcl_cfg->tx_fifo_size))
363 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size %u (0x%x)",
364 getpid (), vcl_cfg->tx_fifo_size,
365 vcl_cfg->tx_fifo_size);
367 else if (unformat (line_input, "event-queue-size 0x%x",
368 &vcl_cfg->event_queue_size))
370 VCFG_DBG (0, "VCL<%d>: configured event_queue_size 0x%x (%u)",
371 getpid (), vcl_cfg->event_queue_size,
372 vcl_cfg->event_queue_size);
374 else if (unformat (line_input, "event-queue-size %u",
375 &vcl_cfg->event_queue_size))
377 VCFG_DBG (0, "VCL<%d>: configured event_queue_size %u (0x%x)",
378 getpid (), vcl_cfg->event_queue_size,
379 vcl_cfg->event_queue_size);
381 else if (unformat (line_input, "app-timeout %f",
382 &vcl_cfg->app_timeout))
384 VCFG_DBG (0, "VCL<%d>: configured app_timeout %f",
385 getpid (), vcl_cfg->app_timeout);
387 else if (unformat (line_input, "session-timeout %f",
388 &vcl_cfg->session_timeout))
390 VCFG_DBG (0, "VCL<%d>: configured session_timeout %f",
391 getpid (), vcl_cfg->session_timeout);
393 else if (unformat (line_input, "app-proxy-transport-tcp"))
395 vcl_cfg->app_proxy_transport_tcp = 1;
396 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%d)",
397 getpid (), vcl_cfg->app_proxy_transport_tcp);
399 else if (unformat (line_input, "app-proxy-transport-udp"))
401 vcl_cfg->app_proxy_transport_udp = 1;
402 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%d)",
403 getpid (), vcl_cfg->app_proxy_transport_udp);
405 else if (unformat (line_input, "app-scope-local"))
407 vcl_cfg->app_scope_local = 1;
408 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%d)",
409 getpid (), vcl_cfg->app_scope_local);
411 else if (unformat (line_input, "app-scope-global"))
413 vcl_cfg->app_scope_global = 1;
414 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%d)",
415 getpid (), vcl_cfg->app_scope_global);
417 else if (unformat (line_input, "namespace-secret %lu",
418 &vcl_cfg->namespace_secret))
420 VCFG_DBG (0, "VCL<%d>: configured namespace_secret %llu "
421 "(0x%llx)", getpid (),
422 (unsigned long long) vcl_cfg->namespace_secret,
423 (unsigned long long) vcl_cfg->namespace_secret);
425 else if (unformat (line_input, "namespace-id %v",
426 &vcl_cfg->namespace_id))
428 u32 max_nsid_vec_len = vcl_bapi_max_nsid_len ();
429 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
430 if (nsid_vec_len > max_nsid_vec_len)
432 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
433 VCFG_DBG (0, "VCL<%d>: configured namespace_id is too long,"
434 " truncated to %d characters!",
435 getpid (), max_nsid_vec_len);
438 VCFG_DBG (0, "VCL<%d>: configured namespace_id %s",
439 getpid (), (char *) vcl_cfg->namespace_id);
441 else if (unformat (line_input, "use-mq-eventfd"))
443 vcl_cfg->use_mq_eventfd = 1;
444 VCFG_DBG (0, "VCL<%d>: configured with mq with eventfd",
447 else if (unformat (line_input, "tls-engine %u",
448 &vcl_cfg->tls_engine))
450 VCFG_DBG (0, "VCL<%d>: configured tls-engine %u (0x%x)",
451 getpid (), vcl_cfg->tls_engine, vcl_cfg->tls_engine);
453 else if (unformat (line_input, "multi-thread-workers"))
455 vcl_cfg->mt_wrk_supported = 1;
456 VCFG_DBG (0, "VCL<%d>: configured with multithread workers",
459 else if (unformat (line_input, "}"))
462 VCFG_DBG (0, "VCL<%d>: completed parsing vppcom config!",
464 unformat_free (line_input);
469 if (line_input->buffer[line_input->index] != '#')
471 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
473 &line_input->buffer[line_input->index]);
476 unformat_free (line_input);
481 unformat_free (input);
489 vppcom_cfg (vppcom_cfg_t * vcl_cfg)
491 char *conf_fname, *env_var_str;
493 vppcom_cfg_init (vcl_cfg);
494 env_var_str = getenv (VPPCOM_ENV_DEBUG);
498 if (sscanf (env_var_str, "%u", &tmp) != 1)
500 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid debug level specified "
501 "in the environment variable " VPPCOM_ENV_DEBUG
502 " (%s)!\n", getpid (), env_var_str);
507 VCFG_DBG (0, "VCL<%d>: configured VCL debug level (%u) from "
508 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
511 conf_fname = getenv (VPPCOM_ENV_CONF);
513 conf_fname = VPPCOM_CONF_DEFAULT;
514 vppcom_cfg_heapsize (conf_fname);
515 vppcom_cfg_read_file (conf_fname);
517 /* Regrab cfg after heap initialization */
519 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
522 u32 ns_id_vec_len = strlen (env_var_str);
524 vec_reset_length (vcm->cfg.namespace_id);
525 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
526 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
528 VCFG_DBG (0, "VCL<%d>: configured namespace_id (%s) from "
529 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (), env_var_str);
531 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
535 if (sscanf (env_var_str, "%llu", (unsigned long long *) &tmp) != 1)
537 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid namespace secret specified"
538 " in the environment variable "
539 VPPCOM_ENV_APP_NAMESPACE_SECRET " (%s)!\n", getpid (),
544 vcm->cfg.namespace_secret = tmp;
545 VCFG_DBG (0, "VCL<%d>: configured namespace secret (%llu) from "
546 VPPCOM_ENV_APP_NAMESPACE_SECRET "!", getpid (),
547 (unsigned long long) vcm->cfg.namespace_secret);
550 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
552 vcm->cfg.app_proxy_transport_tcp = 1;
553 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%u) from "
554 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
555 vcm->cfg.app_proxy_transport_tcp);
557 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
559 vcm->cfg.app_proxy_transport_udp = 1;
560 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%u) from "
561 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
562 vcm->cfg.app_proxy_transport_udp);
564 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
566 vcm->cfg.app_scope_local = 1;
567 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%u) from "
568 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
569 vcm->cfg.app_scope_local);
571 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
573 vcm->cfg.app_scope_global = 1;
574 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%u) from "
575 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
576 vcm->cfg.app_scope_global);
578 env_var_str = getenv (VPPCOM_ENV_VPP_API_SOCKET);
581 vcm->cfg.vpp_bapi_socket_name = format (0, "%s%c", env_var_str, 0);
582 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)", getpid (),
583 vcl_cfg->vpp_bapi_socket_name);
585 env_var_str = getenv (VPPCOM_ENV_VPP_SAPI_SOCKET);
588 vcm->cfg.vpp_app_socket_api = format (0, "%s%c", env_var_str, 0);
589 VCFG_DBG (0, "VCL<%d>: configured app-socket-api (%s)", getpid (),
590 vcl_cfg->vpp_app_socket_api);
595 * fd.io coding-style-patch-verification: ON
598 * eval: (c-set-style "gnu")