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,
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->vpp_api_q_length = 1024;
37 vcl_cfg->segment_baseva = 0x200000000ULL;
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 (), vcl_cfg->heapsize, vcl_cfg->heapsize);
81 argv = calloc (1, sizeof (char *));
84 VCFG_DBG (0, "VCL<%d>: calloc failed, using default heapsize %lu"
85 " (0x%lx)", getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
91 if (fgets (inbuf, 4096, fp) == 0)
93 p = strtok (inbuf, " \t\n");
99 char **tmp = realloc (argv, argc * sizeof (char *));
102 VCFG_DBG (0, "VCL<%d>: realloc failed, using default "
103 "heapsize %lu (0x%lx)", getpid (),
104 vcl_cfg->heapsize, vcl_cfg->heapsize);
108 arg = strndup (p, 1024);
111 VCFG_DBG (0, "VCL<%d>: strndup failed, using default "
112 "heapsize %ld (0x%lx)", getpid (),
113 vcl_cfg->heapsize, 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 %ld "
128 "(0x%lx)", getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
135 * Look for and parse the "heapsize" config parameter.
136 * Manual since none of the clib infra has been bootstrapped yet.
138 * Format: heapsize <nn>[mM][gG]
141 for (i = 1; i < (argc - 1); i++)
143 if (!strncmp (argv[i], "heapsize", 8))
145 sizep = (u8 *) argv[i + 1];
147 while (*sizep >= '0' && *sizep <= '9')
150 size += *sizep++ - '0';
154 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
155 "heapsize %ld (0x%lx)", getpid (), argv[i],
156 argv[i + 1], vcl_cfg->heapsize, vcl_cfg->heapsize);
160 if (*sizep == 'g' || *sizep == 'G')
161 vcl_cfg->heapsize = size << 30;
162 else if (*sizep == 'm' || *sizep == 'M')
163 vcl_cfg->heapsize = size << 20;
166 VCFG_DBG (0, "VCL<%d>: parse error '%s %s', using default "
167 "heapsize %ld (0x%lx)", getpid (), argv[i],
168 argv[i + 1], vcl_cfg->heapsize, vcl_cfg->heapsize);
180 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
181 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
182 if (vcl_mem == MAP_FAILED)
184 VCFG_DBG (0, "VCL<%d>: ERROR: mmap(0, %ld == 0x%lx, "
185 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
186 "-1, 0) failed!", getpid (), vcl_cfg->heapsize,
188 ASSERT (vcl_mem != MAP_FAILED);
191 heap = clib_mem_init_thread_safe (vcl_mem, vcl_cfg->heapsize);
194 fprintf (stderr, "VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
198 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
201 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
206 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
209 VCFG_DBG (0, "VCL<%d>: allocated VCL heap = %p, size %ld (0x%lx)",
210 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
214 vppcom_cfg_read_file (char *conf_fname)
216 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
218 unformat_input_t _input, *input = &_input;
219 unformat_input_t _line_input, *line_input = &_line_input;
220 u8 vc_cfg_input = 0, *chroot_path;
224 fd = open (conf_fname, O_RDONLY);
227 VCFG_DBG (0, "VCL<%d>: using default configuration.", getpid ());
231 if (fstat (fd, &s) < 0)
234 "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 VCFG_DBG (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);
265 "VCL<%d>: configured heapsize %s, actual heapsize %ld"
266 " (0x%lx)", getpid (), chroot_path, vcl_cfg->heapsize,
268 vec_free (chroot_path);
270 else if (unformat (line_input, "api-prefix %s", &chroot_path))
272 vec_terminate_c_string (chroot_path);
273 if (vcl_cfg->vpp_api_filename)
274 vec_free (vcl_cfg->vpp_api_filename);
275 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
277 vl_set_memory_root_path ((char *) chroot_path);
280 "VCL<%d>: configured api-prefix (%s) and api filename"
281 " (%s)", getpid (), chroot_path,
282 vcl_cfg->vpp_api_filename);
283 chroot_path = 0; /* Don't vec_free() it! */
285 else if (unformat (line_input, "api-socket-name %s",
286 &vcl_cfg->vpp_api_socket_name))
288 vec_terminate_c_string (vcl_cfg->vpp_api_socket_name);
289 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)",
290 getpid (), vcl_cfg->vpp_api_socket_name);
292 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
294 if (q_len < vcl_cfg->vpp_api_q_length)
297 "VCL<%d>: ERROR: configured vpp-api-q-length "
298 "(%u) is too small! Using default: %u ", getpid (),
299 q_len, vcl_cfg->vpp_api_q_length);
303 vcl_cfg->vpp_api_q_length = q_len;
305 VCFG_DBG (0, "VCL<%d>: configured vpp-api-q-length %u",
306 getpid (), vcl_cfg->vpp_api_q_length);
309 else if (unformat (line_input, "uid %d", &uid))
311 vl_set_memory_uid (uid);
312 VCFG_DBG (0, "VCL<%d>: configured uid %d", getpid (), uid);
314 else if (unformat (line_input, "gid %d", &gid))
316 vl_set_memory_gid (gid);
317 VCFG_DBG (0, "VCL<%d>: configured gid %d", getpid (), gid);
319 else if (unformat (line_input, "segment-baseva 0x%x",
320 &vcl_cfg->segment_baseva))
322 VCFG_DBG (0, "VCL<%d>: configured segment_baseva 0x%lx",
323 getpid (), vcl_cfg->segment_baseva);
325 else if (unformat (line_input, "segment-size 0x%x",
326 &vcl_cfg->segment_size))
328 VCFG_DBG (0, "VCL<%d>: configured segment_size 0x%x (%d)",
329 getpid (), vcl_cfg->segment_size,
330 vcl_cfg->segment_size);
332 else if (unformat (line_input, "segment-size %d",
333 &vcl_cfg->segment_size))
335 VCFG_DBG (0, "VCL<%d>: configured segment_size %d (0x%x)",
336 getpid (), vcl_cfg->segment_size,
337 vcl_cfg->segment_size);
339 else if (unformat (line_input, "add-segment-size 0x%x",
340 &vcl_cfg->add_segment_size))
342 VCFG_DBG (0, "VCL<%d>: configured add_segment_size 0x%x (%d)",
343 getpid (), vcl_cfg->add_segment_size,
344 vcl_cfg->add_segment_size);
346 else if (unformat (line_input, "add-segment-size %d",
347 &vcl_cfg->add_segment_size))
349 VCFG_DBG (0, "VCL<%d>: configured add_segment_size %d (0x%x)",
350 getpid (), vcl_cfg->add_segment_size,
351 vcl_cfg->add_segment_size);
353 else if (unformat (line_input, "preallocated-fifo-pairs %d",
354 &vcl_cfg->preallocated_fifo_pairs))
356 VCFG_DBG (0, "VCL<%d>: configured preallocated_fifo_pairs %d "
357 "(0x%x)", getpid (), vcl_cfg->preallocated_fifo_pairs,
358 vcl_cfg->preallocated_fifo_pairs);
360 else if (unformat (line_input, "rx-fifo-size 0x%lx",
361 &vcl_cfg->rx_fifo_size))
363 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size 0x%x (%d)",
364 getpid (), vcl_cfg->rx_fifo_size,
365 vcl_cfg->rx_fifo_size);
367 else if (unformat (line_input, "rx-fifo-size %d",
368 &vcl_cfg->rx_fifo_size))
370 VCFG_DBG (0, "VCL<%d>: configured rx_fifo_size %d (0x%x)",
371 getpid (), vcl_cfg->rx_fifo_size,
372 vcl_cfg->rx_fifo_size);
374 else if (unformat (line_input, "tx-fifo-size 0x%lx",
375 &vcl_cfg->tx_fifo_size))
377 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size 0x%x (%d)",
378 getpid (), vcl_cfg->tx_fifo_size,
379 vcl_cfg->tx_fifo_size);
381 else if (unformat (line_input, "tx-fifo-size %ld",
382 &vcl_cfg->tx_fifo_size))
384 VCFG_DBG (0, "VCL<%d>: configured tx_fifo_size %d (0x%x)",
385 getpid (), vcl_cfg->tx_fifo_size,
386 vcl_cfg->tx_fifo_size);
388 else if (unformat (line_input, "event-queue-size 0x%lx",
389 &vcl_cfg->event_queue_size))
391 VCFG_DBG (0, "VCL<%d>: configured event_queue_size 0x%x (%d)",
392 getpid (), vcl_cfg->event_queue_size,
393 vcl_cfg->event_queue_size);
395 else if (unformat (line_input, "event-queue-size %ld",
396 &vcl_cfg->event_queue_size))
398 VCFG_DBG (0, "VCL<%d>: configured event_queue_size %d (0x%x)",
399 getpid (), vcl_cfg->event_queue_size,
400 vcl_cfg->event_queue_size);
402 else if (unformat (line_input, "listen-queue-size 0x%lx",
403 &vcl_cfg->listen_queue_size))
405 VCFG_DBG (0, "VCL<%d>: configured listen_queue_size 0x%x (%u)",
406 getpid (), vcl_cfg->listen_queue_size,
407 vcl_cfg->listen_queue_size);
409 else if (unformat (line_input, "listen-queue-size %ld",
410 &vcl_cfg->listen_queue_size))
412 VCFG_DBG (0, "VCL<%d>: configured listen_queue_size %u (0x%x)",
413 getpid (), vcl_cfg->listen_queue_size,
414 vcl_cfg->listen_queue_size);
416 else if (unformat (line_input, "app-timeout %f",
417 &vcl_cfg->app_timeout))
419 VCFG_DBG (0, "VCL<%d>: configured app_timeout %f",
420 getpid (), vcl_cfg->app_timeout);
422 else if (unformat (line_input, "session-timeout %f",
423 &vcl_cfg->session_timeout))
425 VCFG_DBG (0, "VCL<%d>: configured session_timeout %f",
426 getpid (), vcl_cfg->session_timeout);
428 else if (unformat (line_input, "accept-timeout %f",
429 &vcl_cfg->accept_timeout))
431 VCFG_DBG (0, "VCL<%d>: configured accept_timeout %f",
432 getpid (), vcl_cfg->accept_timeout);
434 else if (unformat (line_input, "app-proxy-transport-tcp"))
436 vcl_cfg->app_proxy_transport_tcp = 1;
437 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%d)",
438 getpid (), vcl_cfg->app_proxy_transport_tcp);
440 else if (unformat (line_input, "app-proxy-transport-udp"))
442 vcl_cfg->app_proxy_transport_udp = 1;
443 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%d)",
444 getpid (), vcl_cfg->app_proxy_transport_udp);
446 else if (unformat (line_input, "app-scope-local"))
448 vcl_cfg->app_scope_local = 1;
449 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%d)",
450 getpid (), vcl_cfg->app_scope_local);
452 else if (unformat (line_input, "app-scope-global"))
454 vcl_cfg->app_scope_global = 1;
455 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%d)",
456 getpid (), vcl_cfg->app_scope_global);
458 else if (unformat (line_input, "namespace-secret %lu",
459 &vcl_cfg->namespace_secret))
461 VCFG_DBG (0, "VCL<%d>: configured namespace_secret %lu (0x%lx)",
462 getpid (), vcl_cfg->namespace_secret,
463 vcl_cfg->namespace_secret);
465 else if (unformat (line_input, "namespace-id %v",
466 &vcl_cfg->namespace_id))
468 u32 max_nsid_vec_len = vcl_max_nsid_len ();
469 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
470 if (nsid_vec_len > max_nsid_vec_len)
472 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
473 VCFG_DBG (0, "VCL<%d>: configured namespace_id is too long,"
474 " truncated to %d characters!",
475 getpid (), max_nsid_vec_len);
478 VCFG_DBG (0, "VCL<%d>: configured namespace_id %s",
479 getpid (), (char *) vcl_cfg->namespace_id);
481 else if (unformat (line_input, "use-mq-eventfd"))
483 vcl_cfg->use_mq_eventfd = 1;
484 VCFG_DBG (0, "VCL<%d>: configured with mq with eventfd",
487 else if (unformat (line_input, "}"))
490 VCFG_DBG (0, "VCL<%d>: completed parsing vppcom config!",
496 if (line_input->buffer[line_input->index] != '#')
498 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
500 &line_input->buffer[line_input->index]);
507 unformat_free (input);
515 vppcom_cfg (vppcom_cfg_t * vcl_cfg)
517 char *conf_fname, *env_var_str;
519 vppcom_cfg_init (vcl_cfg);
520 env_var_str = getenv (VPPCOM_ENV_DEBUG);
524 if (sscanf (env_var_str, "%u", &tmp) != 1)
526 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid debug level specified "
527 "in the environment variable " VPPCOM_ENV_DEBUG
528 " (%s)!\n", getpid (), env_var_str);
533 VCFG_DBG (0, "VCL<%d>: configured VCL debug level (%u) from "
534 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
537 conf_fname = getenv (VPPCOM_ENV_CONF);
539 conf_fname = VPPCOM_CONF_DEFAULT;
540 vppcom_cfg_heapsize (conf_fname);
541 vppcom_cfg_read_file (conf_fname);
543 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
546 if (vcl_cfg->vpp_api_filename)
547 vec_free (vcl_cfg->vpp_api_filename);
548 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c", env_var_str, 0);
549 vl_set_memory_root_path ((char *) env_var_str);
551 VCFG_DBG (0, "VCL<%d>: configured api prefix (%s) and filename (%s) "
552 "from " VPPCOM_ENV_API_PREFIX "!", getpid (), env_var_str,
553 vcl_cfg->vpp_api_filename);
555 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
558 u32 ns_id_vec_len = strlen (env_var_str);
560 vec_reset_length (vcm->cfg.namespace_id);
561 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
562 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
564 VCFG_DBG (0, "VCL<%d>: configured namespace_id (%s) from "
565 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
566 (char *) vcm->cfg.namespace_id);
568 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
572 if (sscanf (env_var_str, "%lu", &tmp) != 1)
574 VCFG_DBG (0, "VCL<%d>: WARNING: Invalid namespace secret specified"
575 " in the environment variable "
576 VPPCOM_ENV_APP_NAMESPACE_SECRET " (%s)!\n", getpid (),
581 vcm->cfg.namespace_secret = tmp;
582 VCFG_DBG (0, "VCL<%d>: configured namespace secret (%lu) from "
583 VPPCOM_ENV_APP_NAMESPACE_SECRET "!", getpid (),
584 vcm->cfg.namespace_secret);
587 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
589 vcm->cfg.app_proxy_transport_tcp = 1;
590 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%u) from "
591 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
592 vcm->cfg.app_proxy_transport_tcp);
594 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
596 vcm->cfg.app_proxy_transport_udp = 1;
597 VCFG_DBG (0, "VCL<%d>: configured app_proxy_transport_udp (%u) from "
598 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
599 vcm->cfg.app_proxy_transport_udp);
601 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
603 vcm->cfg.app_scope_local = 1;
604 VCFG_DBG (0, "VCL<%d>: configured app_scope_local (%u) from "
605 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
606 vcm->cfg.app_scope_local);
608 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
610 vcm->cfg.app_scope_global = 1;
611 VCFG_DBG (0, "VCL<%d>: configured app_scope_global (%u) from "
612 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
613 vcm->cfg.app_scope_global);
615 env_var_str = getenv (VPPCOM_ENV_VPP_API_SOCKET);
618 vcm->cfg.vpp_api_socket_name = format (0, "%s%c", env_var_str, 0);
619 VCFG_DBG (0, "VCL<%d>: configured api-socket-name (%s)", getpid (),
620 vcl_cfg->vpp_api_socket_name);
625 * fd.io coding-style-patch-verification: ON
628 * eval: (c-set-style "gnu")