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 static 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->vpp_api_q_length = 1024;
38 vcl_cfg->segment_baseva = HIGH_SEGMENT_BASEVA;
39 vcl_cfg->segment_size = (256 << 20);
40 vcl_cfg->add_segment_size = (128 << 20);
41 vcl_cfg->preallocated_fifo_pairs = 8;
42 vcl_cfg->rx_fifo_size = (1 << 20);
43 vcl_cfg->tx_fifo_size = (1 << 20);
44 vcl_cfg->event_queue_size = 2048;
45 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
46 vcl_cfg->app_timeout = 10 * 60.0;
47 vcl_cfg->session_timeout = 10 * 60.0;
48 vcl_cfg->accept_timeout = 60.0;
49 vcl_cfg->event_ring_size = (128 << 10);
50 vcl_cfg->event_log_path = "/dev/shm";
53 #define VCFG_DBG(_lvl, _fmt, _args...) \
55 if (vcm->debug > _lvl) \
56 fprintf (stderr, _fmt "\n", ##_args); \
59 vppcom_cfg_heapsize (char *conf_fname)
61 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
74 fp = fopen (conf_fname, "r");
77 VCFG_DBG (0, "VCL<%d>: using default heapsize %lu (0x%lx)",
78 getpid (), (unsigned long) vcl_cfg->heapsize,
79 (unsigned long) 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 (), (unsigned long) vcl_cfg->heapsize,
88 (unsigned long) vcl_cfg->heapsize);
94 if (fgets (inbuf, 4096, fp) == 0)
96 p = strtok (inbuf, " \t\n");
102 char **tmp = realloc (argv, argc * sizeof (char *));
105 VCFG_DBG (0, "VCL<%d>: realloc failed, using default "
106 "heapsize %lu (0x%lx)", getpid (),
107 (unsigned long) vcl_cfg->heapsize,
108 (unsigned long) vcl_cfg->heapsize);
112 arg = strndup (p, 1024);
115 VCFG_DBG (0, "VCL<%d>: strndup failed, using default "
116 "heapsize %lu (0x%lx)", getpid (),
117 (unsigned long) vcl_cfg->heapsize,
118 (unsigned long) vcl_cfg->heapsize);
121 argv[argc - 1] = arg;
122 p = strtok (NULL, " \t\n");
129 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
132 VCFG_DBG (0, "VCL<%d>: realloc failed, using default heapsize %lu "
133 "(0x%lx)", getpid (), (unsigned long) vcl_cfg->heapsize,
134 (unsigned long) vcl_cfg->heapsize);
141 * Look for and parse the "heapsize" config parameter.
142 * Manual since none of the clib infra has been bootstrapped yet.
144 * Format: heapsize <nn>[mM][gG]
147 for (i = 1; i < (argc - 1); i++)
149 if (!strncmp (argv[i], "heapsize", 8))
151 sizep = (u8 *) argv[i + 1];
153 while (*sizep >= '0' && *sizep <= '9')
156 size += *sizep++ - '0';
160 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
161 "heapsize %lu (0x%lx)", getpid (), argv[i],
162 argv[i + 1], (unsigned long) vcl_cfg->heapsize,
163 (unsigned long) vcl_cfg->heapsize);
167 if (*sizep == 'g' || *sizep == 'G')
168 vcl_cfg->heapsize = size << 30;
169 else if (*sizep == 'm' || *sizep == 'M')
170 vcl_cfg->heapsize = size << 20;
173 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
174 "heapsize %lu (0x%lx)", getpid (), argv[i],
175 argv[i + 1], (unsigned long) vcl_cfg->heapsize,
176 (unsigned long) vcl_cfg->heapsize);
189 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
190 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
191 if (vcl_mem == MAP_FAILED)
193 VCFG_DBG (0, "VCL<%d>: ERROR: mmap(0, %lu == 0x%lx, "
194 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
195 "-1, 0) failed!", getpid (),
196 (unsigned long) vcl_cfg->heapsize,
197 (unsigned long) vcl_cfg->heapsize);
198 ASSERT (vcl_mem != MAP_FAILED);
201 heap = clib_mem_init_thread_safe (vcl_mem, vcl_cfg->heapsize);
204 fprintf (stderr, "VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
208 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
211 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
216 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
219 VCFG_DBG (0, "VCL<%d>: allocated VCL heap = %p, size %lu (0x%lx)",
220 getpid (), heap, (unsigned long) vcl_cfg->heapsize,
221 (unsigned long) vcl_cfg->heapsize);
225 vppcom_cfg_read_file (char *conf_fname)
227 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
229 unformat_input_t _input, *input = &_input;
230 unformat_input_t _line_input, *line_input = &_line_input;
231 u8 vc_cfg_input = 0, *chroot_path;
235 fd = open (conf_fname, O_RDONLY);
238 VCFG_DBG (0, "VCL<%d>: using default configuration.", getpid ());
242 if (fstat (fd, &s) < 0)
244 VCFG_DBG (0, "VCL<%d>: failed to stat `%s' using default configuration",
245 getpid (), conf_fname);
249 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
251 VCFG_DBG (0, "VCL<%d>: not a regular file `%s', using default "
252 "configuration", getpid (), conf_fname);
256 unformat_init_clib_file (input, fd);
258 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
260 (void) unformat_user (input, unformat_line_input, line_input);
261 unformat_skip_white_space (line_input);
263 if (unformat (line_input, "vcl {"))
266 unformat_free (line_input);
272 if (unformat (line_input, "heapsize %U", unformat_memory_size,
275 VCFG_DBG (0, "VCL<%d>: configured heapsize %lu", getpid (),
276 (unsigned long) vcl_cfg->heapsize);
280 (line_input, "max-workers %u", &vcl_cfg->max_workers))
282 VCFG_DBG (0, "VCL<%d>: configured max-workers %u", getpid (),
283 vcl_cfg->max_workers);
285 else if (unformat (line_input, "api-prefix %s", &chroot_path))
287 vec_terminate_c_string (chroot_path);
288 if (vcl_cfg->vpp_api_filename)
289 vec_free (vcl_cfg->vpp_api_filename);
290 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
292 vl_set_memory_root_path ((char *) chroot_path);
294 VCFG_DBG (0, "VCL<%d>: configured api-prefix (%s) and api "
295 "filename (%s)", getpid (), chroot_path,
296 vcl_cfg->vpp_api_filename);
297 chroot_path = 0; /* Don't vec_free() it! */
299 else if (unformat (line_input, "api-socket-name %s",
300 &vcl_cfg->vpp_api_socket_name))
302 vec_terminate_c_string (vcl_cfg->vpp_api_socket_name);
303 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)",
304 getpid (), vcl_cfg->vpp_api_socket_name);
306 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
308 if (q_len < vcl_cfg->vpp_api_q_length)
311 "VCL<%d>: ERROR: configured vpp-api-q-length "
312 "(%u) is too small! Using default: %u ", getpid (),
313 q_len, vcl_cfg->vpp_api_q_length);
317 vcl_cfg->vpp_api_q_length = q_len;
319 VCFG_DBG (0, "VCL<%d>: configured vpp-api-q-length %u",
320 getpid (), vcl_cfg->vpp_api_q_length);
323 else if (unformat (line_input, "uid %d", &uid))
325 vl_set_memory_uid (uid);
326 VCFG_DBG (0, "VCL<%d>: configured uid %d", getpid (), uid);
328 else if (unformat (line_input, "gid %d", &gid))
330 vl_set_memory_gid (gid);
331 VCFG_DBG (0, "VCL<%d>: configured gid %d", getpid (), gid);
333 else if (unformat (line_input, "segment-baseva 0x%x",
334 &vcl_cfg->segment_baseva))
336 VCFG_DBG (0, "VCL<%d>: configured segment_baseva 0x%lx",
337 getpid (), (unsigned long) vcl_cfg->segment_baseva);
339 else if (unformat (line_input, "segment-size 0x%x",
340 &vcl_cfg->segment_size))
342 VCFG_DBG (0, "VCL<%d>: configured segment_size 0x%x (%d)",
343 getpid (), vcl_cfg->segment_size,
344 vcl_cfg->segment_size);
346 else if (unformat (line_input, "segment-size %u",
347 &vcl_cfg->segment_size))
349 VCFG_DBG (0, "VCL<%d>: configured segment_size %u (0x%x)",
350 getpid (), vcl_cfg->segment_size,
351 vcl_cfg->segment_size);
353 else if (unformat (line_input, "add-segment-size 0x%x",
354 &vcl_cfg->add_segment_size))
356 VCFG_DBG (0, "VCL<%d>: configured add_segment_size 0x%x (%u)",
357 getpid (), vcl_cfg->add_segment_size,
358 vcl_cfg->add_segment_size);
360 else if (unformat (line_input, "add-segment-size %u",
361 &vcl_cfg->add_segment_size))
363 VCFG_DBG (0, "VCL<%d>: configured add_segment_size %u (0x%x)",
364 getpid (), vcl_cfg->add_segment_size,
365 vcl_cfg->add_segment_size);
367 else if (unformat (line_input, "preallocated-fifo-pairs %u",
368 &vcl_cfg->preallocated_fifo_pairs))
370 VCFG_DBG (0, "VCL<%d>: configured preallocated_fifo_pairs %u "
371 "(0x%x)", getpid (), vcl_cfg->preallocated_fifo_pairs,
372 vcl_cfg->preallocated_fifo_pairs);
374 else if (unformat (line_input, "rx-fifo-size 0x%x",
375 &vcl_cfg->rx_fifo_size))
377 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size 0x%x (%u)",
378 getpid (), vcl_cfg->rx_fifo_size,
379 vcl_cfg->rx_fifo_size);
381 else if (unformat (line_input, "rx-fifo-size %u",
382 &vcl_cfg->rx_fifo_size))
384 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size %u (0x%x)",
385 getpid (), vcl_cfg->rx_fifo_size,
386 vcl_cfg->rx_fifo_size);
388 else if (unformat (line_input, "tx-fifo-size 0x%x",
389 &vcl_cfg->tx_fifo_size))
391 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size 0x%x (%u)",
392 getpid (), vcl_cfg->tx_fifo_size,
393 vcl_cfg->tx_fifo_size);
395 else if (unformat (line_input, "tx-fifo-size %u",
396 &vcl_cfg->tx_fifo_size))
398 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size %u (0x%x)",
399 getpid (), vcl_cfg->tx_fifo_size,
400 vcl_cfg->tx_fifo_size);
402 else if (unformat (line_input, "event-queue-size 0x%x",
403 &vcl_cfg->event_queue_size))
405 VCFG_DBG (0, "VCL<%d>: configured event_queue_size 0x%x (%u)",
406 getpid (), vcl_cfg->event_queue_size,
407 vcl_cfg->event_queue_size);
409 else if (unformat (line_input, "event-queue-size %u",
410 &vcl_cfg->event_queue_size))
412 VCFG_DBG (0, "VCL<%d>: configured event_queue_size %u (0x%x)",
413 getpid (), vcl_cfg->event_queue_size,
414 vcl_cfg->event_queue_size);
416 else if (unformat (line_input, "listen-queue-size 0x%x",
417 &vcl_cfg->listen_queue_size))
419 VCFG_DBG (0, "VCL<%d>: configured listen_queue_size 0x%x (%u)",
420 getpid (), vcl_cfg->listen_queue_size,
421 vcl_cfg->listen_queue_size);
423 else if (unformat (line_input, "listen-queue-size %u",
424 &vcl_cfg->listen_queue_size))
426 VCFG_DBG (0, "VCL<%d>: configured listen_queue_size %u (0x%x)",
427 getpid (), vcl_cfg->listen_queue_size,
428 vcl_cfg->listen_queue_size);
430 else if (unformat (line_input, "app-timeout %f",
431 &vcl_cfg->app_timeout))
433 VCFG_DBG (0, "VCL<%d>: configured app_timeout %f",
434 getpid (), vcl_cfg->app_timeout);
436 else if (unformat (line_input, "session-timeout %f",
437 &vcl_cfg->session_timeout))
439 VCFG_DBG (0, "VCL<%d>: configured session_timeout %f",
440 getpid (), vcl_cfg->session_timeout);
442 else if (unformat (line_input, "accept-timeout %f",
443 &vcl_cfg->accept_timeout))
445 VCFG_DBG (0, "VCL<%d>: configured accept_timeout %f",
446 getpid (), vcl_cfg->accept_timeout);
448 else if (unformat (line_input, "app-proxy-transport-tcp"))
450 vcl_cfg->app_proxy_transport_tcp = 1;
451 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%d)",
452 getpid (), vcl_cfg->app_proxy_transport_tcp);
454 else if (unformat (line_input, "app-proxy-transport-udp"))
456 vcl_cfg->app_proxy_transport_udp = 1;
457 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%d)",
458 getpid (), vcl_cfg->app_proxy_transport_udp);
460 else if (unformat (line_input, "app-scope-local"))
462 vcl_cfg->app_scope_local = 1;
463 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%d)",
464 getpid (), vcl_cfg->app_scope_local);
466 else if (unformat (line_input, "app-scope-global"))
468 vcl_cfg->app_scope_global = 1;
469 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%d)",
470 getpid (), vcl_cfg->app_scope_global);
472 else if (unformat (line_input, "namespace-secret %lu",
473 &vcl_cfg->namespace_secret))
475 VCFG_DBG (0, "VCL<%d>: configured namespace_secret %llu "
476 "(0x%llx)", getpid (),
477 (unsigned long long) vcl_cfg->namespace_secret,
478 (unsigned long long) vcl_cfg->namespace_secret);
480 else if (unformat (line_input, "namespace-id %v",
481 &vcl_cfg->namespace_id))
483 u32 max_nsid_vec_len = vcl_max_nsid_len ();
484 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
485 if (nsid_vec_len > max_nsid_vec_len)
487 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
488 VCFG_DBG (0, "VCL<%d>: configured namespace_id is too long,"
489 " truncated to %d characters!",
490 getpid (), max_nsid_vec_len);
493 VCFG_DBG (0, "VCL<%d>: configured namespace_id %s",
494 getpid (), (char *) vcl_cfg->namespace_id);
496 else if (unformat (line_input, "use-mq-eventfd"))
498 vcl_cfg->use_mq_eventfd = 1;
499 VCFG_DBG (0, "VCL<%d>: configured with mq with eventfd",
502 else if (unformat (line_input, "tls-engine %u",
503 &vcl_cfg->tls_engine))
505 VCFG_DBG (0, "VCL<%d>: configured tls-engine %u (0x%x)",
506 getpid (), vcl_cfg->tls_engine, vcl_cfg->tls_engine);
508 else if (unformat (line_input, "}"))
511 VCFG_DBG (0, "VCL<%d>: completed parsing vppcom config!",
513 unformat_free (line_input);
518 if (line_input->buffer[line_input->index] != '#')
520 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
522 &line_input->buffer[line_input->index]);
525 unformat_free (line_input);
530 unformat_free (input);
538 vppcom_cfg (vppcom_cfg_t * vcl_cfg)
540 char *conf_fname, *env_var_str;
542 vppcom_cfg_init (vcl_cfg);
543 env_var_str = getenv (VPPCOM_ENV_DEBUG);
547 if (sscanf (env_var_str, "%u", &tmp) != 1)
549 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid debug level specified "
550 "in the environment variable " VPPCOM_ENV_DEBUG
551 " (%s)!\n", getpid (), env_var_str);
556 VCFG_DBG (0, "VCL<%d>: configured VCL debug level (%u) from "
557 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
560 conf_fname = getenv (VPPCOM_ENV_CONF);
562 conf_fname = VPPCOM_CONF_DEFAULT;
563 vppcom_cfg_heapsize (conf_fname);
564 vppcom_cfg_read_file (conf_fname);
566 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
569 if (vcl_cfg->vpp_api_filename)
570 vec_free (vcl_cfg->vpp_api_filename);
571 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c", env_var_str, 0);
572 vl_set_memory_root_path ((char *) env_var_str);
574 VCFG_DBG (0, "VCL<%d>: configured api prefix (%s) and filename (%s) "
575 "from " VPPCOM_ENV_API_PREFIX "!", getpid (), env_var_str,
576 vcl_cfg->vpp_api_filename);
578 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
581 u32 ns_id_vec_len = strlen (env_var_str);
583 vec_reset_length (vcm->cfg.namespace_id);
584 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
585 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
587 VCFG_DBG (0, "VCL<%d>: configured namespace_id (%s) from "
588 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (), env_var_str);
590 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
594 if (sscanf (env_var_str, "%llu", (unsigned long long *) &tmp) != 1)
596 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid namespace secret specified"
597 " in the environment variable "
598 VPPCOM_ENV_APP_NAMESPACE_SECRET " (%s)!\n", getpid (),
603 vcm->cfg.namespace_secret = tmp;
604 VCFG_DBG (0, "VCL<%d>: configured namespace secret (%llu) from "
605 VPPCOM_ENV_APP_NAMESPACE_SECRET "!", getpid (),
606 (unsigned long long) vcm->cfg.namespace_secret);
609 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
611 vcm->cfg.app_proxy_transport_tcp = 1;
612 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%u) from "
613 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
614 vcm->cfg.app_proxy_transport_tcp);
616 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
618 vcm->cfg.app_proxy_transport_udp = 1;
619 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%u) from "
620 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
621 vcm->cfg.app_proxy_transport_udp);
623 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
625 vcm->cfg.app_scope_local = 1;
626 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%u) from "
627 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
628 vcm->cfg.app_scope_local);
630 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
632 vcm->cfg.app_scope_global = 1;
633 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%u) from "
634 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
635 vcm->cfg.app_scope_global);
637 env_var_str = getenv (VPPCOM_ENV_VPP_API_SOCKET);
640 vcm->cfg.vpp_api_socket_name = format (0, "%s%c", env_var_str, 0);
641 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)", getpid (),
642 vcl_cfg->vpp_api_socket_name);
647 * fd.io coding-style-patch-verification: ON
650 * eval: (c-set-style "gnu")