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,
27 vppcom_main_t *vcm = &_vppcom_main;
30 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
34 vcl_cfg->heapsize = (256ULL << 20);
35 vcl_cfg->vpp_api_q_length = 1024;
36 vcl_cfg->segment_baseva = 0x200000000ULL;
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->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
44 vcl_cfg->app_timeout = 10 * 60.0;
45 vcl_cfg->session_timeout = 10 * 60.0;
46 vcl_cfg->accept_timeout = 60.0;
47 vcl_cfg->event_ring_size = (128 << 10);
48 vcl_cfg->event_log_path = "/dev/shm";
51 #define VCL_CFG_DBG(_lvl, _fmt, _args...) \
52 if (vcm->debug > _lvl) \
53 fprintf (stderr, _fmt, ##_args)
55 vppcom_cfg_heapsize (char *conf_fname)
57 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
70 fp = fopen (conf_fname, "r");
73 VCL_CFG_DBG (0, "VCL<%d>: using default heapsize %lu (0x%lx)",
74 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
78 argv = calloc (1, sizeof (char *));
81 VCL_CFG_DBG (0, "VCL<%d>: calloc failed, using default heapsize %lu"
82 " (0x%lx)", getpid (), 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 VCL_CFG_DBG (0, "VCL<%d>: realloc failed, using default "
101 "heapsize %lu (0x%lx)", getpid (),
102 vcl_cfg->heapsize, vcl_cfg->heapsize);
106 arg = strndup (p, 1024);
109 VCL_CFG_DBG (0, "VCL<%d>: strndup failed, using default "
110 "heapsize %ld (0x%lx)", getpid (),
111 vcl_cfg->heapsize, vcl_cfg->heapsize);
114 argv[argc - 1] = arg;
115 p = strtok (NULL, " \t\n");
122 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
125 VCL_CFG_DBG (0, "VCL<%d>: realloc failed, using default heapsize %ld "
126 "(0x%lx)", getpid (), vcl_cfg->heapsize,
134 * Look for and parse the "heapsize" config parameter.
135 * Manual since none of the clib infra has been bootstrapped yet.
137 * Format: heapsize <nn>[mM][gG]
140 for (i = 1; i < (argc - 1); i++)
142 if (!strncmp (argv[i], "heapsize", 8))
144 sizep = (u8 *) argv[i + 1];
146 while (*sizep >= '0' && *sizep <= '9')
149 size += *sizep++ - '0';
153 VCL_CFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
154 "heapsize %ld (0x%lx)", getpid (), argv[i],
155 argv[i + 1], vcl_cfg->heapsize, vcl_cfg->heapsize);
159 if (*sizep == 'g' || *sizep == 'G')
160 vcl_cfg->heapsize = size << 30;
161 else if (*sizep == 'm' || *sizep == 'M')
162 vcl_cfg->heapsize = size << 20;
165 VCL_CFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
166 "heapsize %ld (0x%lx)", getpid (), argv[i],
167 argv[i + 1], vcl_cfg->heapsize, vcl_cfg->heapsize);
179 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
180 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
181 if (vcl_mem == MAP_FAILED)
183 VCL_CFG_DBG (0, "VCL<%d>: ERROR: mmap(0, %ld == 0x%lx, "
184 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
185 "-1, 0) failed!", getpid (), vcl_cfg->heapsize,
187 ASSERT (vcl_mem != MAP_FAILED);
190 heap = clib_mem_init_thread_safe (vcl_mem, vcl_cfg->heapsize);
193 fprintf (stderr, "VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
197 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
200 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
205 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
208 VDBG (0, "VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)", getpid (),
209 heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
213 vppcom_cfg_read_file (char *conf_fname)
215 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
217 unformat_input_t _input, *input = &_input;
218 unformat_input_t _line_input, *line_input = &_line_input;
224 fd = open (conf_fname, O_RDONLY);
227 VDBG (0, "VCL<%d>: using default configuration.", getpid (),
232 if (fstat (fd, &s) < 0)
234 VDBG (0, "VCL<%d>: failed to stat `%s', using default configuration",
235 getpid (), conf_fname);
239 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
241 VDBG (0, "VCL<%d>: not a regular file `%s', using default "
242 "configuration", getpid (), conf_fname);
246 unformat_init_clib_file (input, fd);
248 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
250 (void) unformat_user (input, unformat_line_input, line_input);
251 unformat_skip_white_space (line_input);
253 if (unformat (line_input, "vcl {"))
261 if (unformat (line_input, "heapsize %s", &chroot_path))
263 vec_terminate_c_string (chroot_path);
264 VDBG (0, "VCL<%d>: configured heapsize %s, actual heapsize %lld"
265 " (0x%llx)", getpid (), chroot_path, vcl_cfg->heapsize,
267 vec_free (chroot_path);
269 else if (unformat (line_input, "api-prefix %s", &chroot_path))
271 vec_terminate_c_string (chroot_path);
272 if (vcl_cfg->vpp_api_filename)
273 vec_free (vcl_cfg->vpp_api_filename);
274 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
276 vl_set_memory_root_path ((char *) chroot_path);
278 VDBG (0, "VCL<%d>: configured api-prefix (%s) and api filename"
279 " (%s)", getpid (), chroot_path,
280 vcl_cfg->vpp_api_filename);
281 chroot_path = 0; /* Don't vec_free() it! */
283 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
285 if (q_len < vcl_cfg->vpp_api_q_length)
287 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
288 "(%u) is too small! Using default: %u ",
289 getpid (), q_len, vcl_cfg->vpp_api_q_length);
293 vcl_cfg->vpp_api_q_length = q_len;
295 VDBG (0, "VCL<%d>: configured vpp-api-q-length %u",
296 getpid (), vcl_cfg->vpp_api_q_length);
299 else if (unformat (line_input, "uid %d", &uid))
301 vl_set_memory_uid (uid);
302 VDBG (0, "VCL<%d>: configured uid %d", getpid (), uid);
304 else if (unformat (line_input, "gid %d", &gid))
306 vl_set_memory_gid (gid);
307 VDBG (0, "VCL<%d>: configured gid %d", getpid (), gid);
309 else if (unformat (line_input, "segment-baseva 0x%lx",
310 &vcl_cfg->segment_baseva))
312 VDBG (0, "VCL<%d>: configured segment_baseva 0x%lx", getpid (),
313 vcl_cfg->segment_baseva);
315 else if (unformat (line_input, "segment-size 0x%lx",
316 &vcl_cfg->segment_size))
318 VDBG (0, "VCL<%d>: configured segment_size 0x%lx (%ld)",
319 getpid (), vcl_cfg->segment_size, vcl_cfg->segment_size);
321 else if (unformat (line_input, "segment-size %ld",
322 &vcl_cfg->segment_size))
324 VDBG (0, "VCL<%d>: configured segment_size %ld (0x%lx)",
325 getpid (), vcl_cfg->segment_size, vcl_cfg->segment_size);
327 else if (unformat (line_input, "add-segment-size 0x%lx",
328 &vcl_cfg->add_segment_size))
330 VDBG (0, "VCL<%d>: configured add_segment_size 0x%lx (%ld)",
331 getpid (), vcl_cfg->add_segment_size,
332 vcl_cfg->add_segment_size);
334 else if (unformat (line_input, "add-segment-size %ld",
335 &vcl_cfg->add_segment_size))
337 VDBG (0, "VCL<%d>: configured add_segment_size %ld (0x%lx)",
338 getpid (), vcl_cfg->add_segment_size,
339 vcl_cfg->add_segment_size);
341 else if (unformat (line_input, "preallocated-fifo-pairs %d",
342 &vcl_cfg->preallocated_fifo_pairs))
344 VDBG (0, "VCL<%d>: configured preallocated_fifo_pairs %d "
345 "(0x%x)", getpid (), vcl_cfg->preallocated_fifo_pairs,
346 vcl_cfg->preallocated_fifo_pairs);
348 else if (unformat (line_input, "rx-fifo-size 0x%lx",
349 &vcl_cfg->rx_fifo_size))
351 VDBG (0, "VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
352 getpid (), vcl_cfg->rx_fifo_size, vcl_cfg->rx_fifo_size);
354 else if (unformat (line_input, "rx-fifo-size %ld",
355 &vcl_cfg->rx_fifo_size))
357 VDBG (0, "VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
358 getpid (), vcl_cfg->rx_fifo_size, vcl_cfg->rx_fifo_size);
360 else if (unformat (line_input, "tx-fifo-size 0x%lx",
361 &vcl_cfg->tx_fifo_size))
363 VDBG (0, "VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
364 getpid (), vcl_cfg->tx_fifo_size, vcl_cfg->tx_fifo_size);
366 else if (unformat (line_input, "tx-fifo-size %ld",
367 &vcl_cfg->tx_fifo_size))
369 VDBG (0, "VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
370 getpid (), vcl_cfg->tx_fifo_size, vcl_cfg->tx_fifo_size);
372 else if (unformat (line_input, "event-queue-size 0x%lx",
373 &vcl_cfg->event_queue_size))
375 VDBG (0, "VCL<%d>: configured event_queue_size 0x%lx (%ld)",
376 getpid (), vcl_cfg->event_queue_size,
377 vcl_cfg->event_queue_size);
379 else if (unformat (line_input, "event-queue-size %ld",
380 &vcl_cfg->event_queue_size))
382 VDBG (0, "VCL<%d>: configured event_queue_size %ld (0x%lx)",
383 getpid (), vcl_cfg->event_queue_size,
384 vcl_cfg->event_queue_size);
386 else if (unformat (line_input, "listen-queue-size 0x%lx",
387 &vcl_cfg->listen_queue_size))
389 VDBG (0, "VCL<%d>: configured listen_queue_size 0x%lx (%ld)",
390 getpid (), vcl_cfg->listen_queue_size,
391 vcl_cfg->listen_queue_size);
393 else if (unformat (line_input, "listen-queue-size %ld",
394 &vcl_cfg->listen_queue_size))
396 VDBG (0, "VCL<%d>: configured listen_queue_size %ld (0x%lx)",
397 getpid (), vcl_cfg->listen_queue_size,
398 vcl_cfg->listen_queue_size);
400 else if (unformat (line_input, "app-timeout %f",
401 &vcl_cfg->app_timeout))
403 VDBG (0, "VCL<%d>: configured app_timeout %f",
404 getpid (), vcl_cfg->app_timeout);
406 else if (unformat (line_input, "session-timeout %f",
407 &vcl_cfg->session_timeout))
409 VDBG (0, "VCL<%d>: configured session_timeout %f",
410 getpid (), vcl_cfg->session_timeout);
412 else if (unformat (line_input, "accept-timeout %f",
413 &vcl_cfg->accept_timeout))
415 VDBG (0, "VCL<%d>: configured accept_timeout %f",
416 getpid (), vcl_cfg->accept_timeout);
418 else if (unformat (line_input, "app-proxy-transport-tcp"))
420 vcl_cfg->app_proxy_transport_tcp = 1;
421 VDBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%d)",
422 getpid (), vcl_cfg->app_proxy_transport_tcp);
424 else if (unformat (line_input, "app-proxy-transport-udp"))
426 vcl_cfg->app_proxy_transport_udp = 1;
427 VDBG (0, "VCL<%d>: configured app_proxy_transport_udp (%d)",
428 getpid (), vcl_cfg->app_proxy_transport_udp);
430 else if (unformat (line_input, "app-scope-local"))
432 vcl_cfg->app_scope_local = 1;
433 VDBG (0, "VCL<%d>: configured app_scope_local (%d)",
434 getpid (), vcl_cfg->app_scope_local);
436 else if (unformat (line_input, "app-scope-global"))
438 vcl_cfg->app_scope_global = 1;
439 VDBG (0, "VCL<%d>: configured app_scope_global (%d)",
440 getpid (), vcl_cfg->app_scope_global);
442 else if (unformat (line_input, "namespace-secret %lu",
443 &vcl_cfg->namespace_secret))
445 VDBG (0, "VCL<%d>: configured namespace_secret %lu (0x%lx)",
446 getpid (), vcl_cfg->namespace_secret,
447 vcl_cfg->namespace_secret);
449 else if (unformat (line_input, "namespace-id %v",
450 &vcl_cfg->namespace_id))
452 u32 max_nsid_vec_len = vcl_max_nsid_len ();
453 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
454 if (nsid_vec_len > max_nsid_vec_len)
456 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
457 VDBG (0, "VCL<%d>: configured namespace_id is too long,"
458 " truncated to %d characters!",
459 getpid (), max_nsid_vec_len);
462 VDBG (0, "VCL<%d>: configured namespace_id %v",
463 getpid (), vcl_cfg->namespace_id);
465 else if (unformat (line_input, "}"))
468 VDBG (0, "VCL<%d>: completed parsing vppcom config!",
474 if (line_input->buffer[line_input->index] != '#')
476 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
478 &line_input->buffer[line_input->index]);
485 unformat_free (input);
493 vppcom_cfg (vppcom_cfg_t * vcl_cfg)
495 char *conf_fname, *env_var_str;
497 vppcom_cfg_init (vcl_cfg);
498 env_var_str = getenv (VPPCOM_ENV_DEBUG);
502 if (sscanf (env_var_str, "%u", &tmp) != 1)
503 clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
504 "in the environment variable " VPPCOM_ENV_DEBUG
505 " (%s)!\n", getpid (), env_var_str);
509 VDBG (0, "VCL<%d>: configured VCL debug level (%u) from "
510 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
513 conf_fname = getenv (VPPCOM_ENV_CONF);
515 conf_fname = VPPCOM_CONF_DEFAULT;
516 vppcom_cfg_heapsize (conf_fname);
517 vppcom_cfg_read_file (conf_fname);
519 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
522 if (vcl_cfg->vpp_api_filename)
523 vec_free (vcl_cfg->vpp_api_filename);
524 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c", env_var_str, 0);
525 vl_set_memory_root_path ((char *) env_var_str);
527 VDBG (0, "VCL<%d>: configured api prefix (%s) and filename (%s) "
528 "from " VPPCOM_ENV_API_PREFIX "!",
529 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
531 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
534 u32 ns_id_vec_len = strlen (env_var_str);
536 vec_reset_length (vcm->cfg.namespace_id);
537 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
538 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
540 VDBG (0, "VCL<%d>: configured namespace_id (%v) from "
541 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
542 vcm->cfg.namespace_id);
544 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
548 if (sscanf (env_var_str, "%lu", &tmp) != 1)
549 clib_warning ("VCL<%d>: WARNING: Invalid namespace secret "
550 "specified in the environment variable "
551 VPPCOM_ENV_APP_NAMESPACE_SECRET
552 " (%s)!\n", getpid (), env_var_str);
555 vcm->cfg.namespace_secret = tmp;
556 VDBG (0, "VCL<%d>: configured namespace secret (%lu) from "
557 VPPCOM_ENV_APP_NAMESPACE_SECRET "!", getpid (),
558 vcm->cfg.namespace_secret);
561 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
563 vcm->cfg.app_proxy_transport_tcp = 1;
564 VDBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%u) from "
565 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
566 vcm->cfg.app_proxy_transport_tcp);
568 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
570 vcm->cfg.app_proxy_transport_udp = 1;
571 VDBG (0, "VCL<%d>: configured app_proxy_transport_udp (%u) from "
572 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
573 vcm->cfg.app_proxy_transport_udp);
575 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
577 vcm->cfg.app_scope_local = 1;
578 VDBG (0, "VCL<%d>: configured app_scope_local (%u) from "
579 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
580 vcm->cfg.app_scope_local);
582 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
584 vcm->cfg.app_scope_global = 1;
585 VDBG (0, "VCL<%d>: configured app_scope_global (%u) from "
586 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
587 vcm->cfg.app_scope_global);
592 * fd.io coding-style-patch-verification: ON
595 * eval: (c-set-style "gnu")