int vlib_unix_main (int argc, char *argv[]) { vlib_main_t *vm = &vlib_global_main; /* one and only time for this! */ unformat_input_t input; clib_error_t *e; int i; vm->argv = (u8 **) argv; vm->name = argv[0]; vm->heap_base = clib_mem_get_heap (); vm->heap_aligned_base = (void *) (((uword) vm->heap_base) & ~(VLIB_FRAME_ALIGN - 1)); ASSERT (vm->heap_base); unformat_init_command_line (&input, (char **) vm->argv); if ((e = vlib_plugin_config (vm, &input))) { clib_error_report (e); return 1; } unformat_free (&input); i = vlib_plugin_early_init (vm); if (i) return i; unformat_init_command_line (&input, (char **) vm->argv); if (vm->init_functions_called == 0) vm->init_functions_called = hash_create (0, /* value bytes */ 0); e = vlib_call_all_config_functions (vm, &input, 1 /* early */ ); if (e != 0) { clib_error_report (e); return 1; } unformat_free (&input); /* always load symbols, for signal handler and mheap memory get/put backtrace */ clib_elf_main_init (vm->name); vec_validate (vlib_thread_stacks, 0); vlib_thread_stack_init (0); __os_thread_index = 0; vm->thread_index = 0; i = clib_calljmp (thread0, (uword) vm, (void *) (vlib_thread_stacks[0] + VLIB_THREAD_STACK_SIZE)); return i; }
u8 * vlib_sysfs_link_to_name (char *link) { char *p, buffer[64]; unformat_input_t in; u8 *s = 0; int r; r = readlink (link, buffer, sizeof (buffer) - 1); if (r < 0) return 0; buffer[r] = 0; p = strrchr (buffer, '/'); if (!p) return 0; unformat_init_string (&in, p + 1, strlen (p + 1)); unformat (&in, "%s", &s); unformat_free (&in); return s; }
static clib_error_t * mrvl_pp2_create_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; mrvl_pp2_create_if_args_t args = { 0 }; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "name %s", &args.name)) ; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } unformat_free (line_input); mrvl_pp2_create_if (&args); vec_free (args.name); return args.error; }
static clib_error_t * netmap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, * line_input = &_line_input; u8 * host_if_name = NULL; /* Get a line of input. */ if (! unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "name %s", &host_if_name)) ; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } unformat_free (line_input); if (host_if_name == NULL) return clib_error_return (0, "missing host interface name"); netmap_delete_if(vm, host_if_name); return 0; }
static clib_error_t * virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; virtio_pci_create_if_args_t args; u64 feature_mask = (u64) ~ (0ULL); /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; memset (&args, 0, sizeof (args)); while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr)) ; else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask)) args.features = feature_mask; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } unformat_free (line_input); virtio_pci_create_if (vm, &args); return args.error; }
static clib_error_t * abf_policy_cmd (vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; u32 acl_index, policy_id; fib_route_path_t *rpaths = NULL, rpath; u32 is_del; is_del = 0; acl_index = INDEX_INVALID; policy_id = INDEX_INVALID; /* Get a line of input. */ if (!unformat_user (main_input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "acl %d", &acl_index)) ; else if (unformat (line_input, "id %d", &policy_id)) ; else if (unformat (line_input, "del")) is_del = 1; else if (unformat (line_input, "add")) is_del = 0; else if (unformat (line_input, "via %U", unformat_fib_route_path, &rpath)) vec_add1 (rpaths, rpath); else return (clib_error_return (0, "unknown input '%U'", format_unformat_error, line_input)); } if (INDEX_INVALID == policy_id) { vlib_cli_output (vm, "Specify a Policy ID"); return 0; } if (!is_del) { if (INDEX_INVALID == acl_index) { vlib_cli_output (vm, "ACL index must be set"); return 0; } abf_policy_update (policy_id, acl_index, rpaths); } else { abf_policy_delete (policy_id, rpaths); } unformat_free (line_input); return (NULL); }
int main (int argc, char * argv []) { unformat_input_t i; int r; verbose = (argc > 1); unformat_init_command_line (&i, argv); r = test_socket_main (&i); unformat_free (&i); return r; }
int main (int argc, char * argv[]) { unformat_input_t i; int ret; unformat_init_command_line (&i, argv); ret = test_macros_main (&i); unformat_free (&i); return ret; }
int main (int argc, char * argv[]) { unformat_input_t i; int ret; clib_mem_init (0, 3ULL<<30); unformat_init_command_line (&i, argv); ret = test_random_main (&i); unformat_free (&i); return ret; }
static uword thread0 (uword arg) { vlib_main_t *vm = (vlib_main_t *) arg; unformat_input_t input; int i; unformat_init_command_line (&input, (char **) vm->argv); i = vlib_main (vm, &input); unformat_free (&input); return i; }
static clib_error_t * netmap_create_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, * line_input = &_line_input; u8 * host_if_name = NULL; u8 hwaddr [6]; u8 * hw_addr_ptr = 0; int r; u8 is_pipe = 0; u8 is_master = 0; u32 sw_if_index = ~0; /* Get a line of input. */ if (! unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "name %s", &host_if_name)) ; else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr)) hw_addr_ptr = hwaddr; else if (unformat (line_input, "pipe")) is_pipe = 1; else if (unformat (line_input, "master")) is_master = 1; else if (unformat (line_input, "slave")) is_master = 0; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } unformat_free (line_input); if (host_if_name == NULL) return clib_error_return (0, "missing host interface name"); r = netmap_create_if(vm, host_if_name, hw_addr_ptr, is_pipe, is_master, &sw_if_index); if (r == VNET_API_ERROR_SYSCALL_ERROR_1) return clib_error_return(0, "%s (errno %d)", strerror (errno), errno); if (r == VNET_API_ERROR_INVALID_INTERFACE) return clib_error_return(0, "Invalid interface name"); if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS) return clib_error_return(0, "Interface already exists"); vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index); return 0; }
static clib_error_t * virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; u32 sw_if_index = ~0; vnet_hw_interface_t *hw; virtio_main_t *vim = &virtio_main; virtio_if_t *vif; vnet_main_t *vnm = vnet_get_main (); /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "sw_if_index %d", &sw_if_index)) ; else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) ; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } unformat_free (line_input); if (sw_if_index == ~0) return clib_error_return (0, "please specify interface name or sw_if_index"); hw = vnet_get_sup_hw_interface (vnm, sw_if_index); if (hw == NULL || virtio_device_class.index != hw->dev_class_index) return clib_error_return (0, "not a virtio interface"); vif = pool_elt_at_index (vim->interfaces, hw->dev_instance); if (virtio_pci_delete_if (vm, vif) < 0) return clib_error_return (0, "not a virtio pci interface"); return 0; }
static clib_error_t * cj_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { int is_enable = -1; int is_dump = -1; unformat_input_t _line_input, *line_input = &_line_input; clib_error_t *error = NULL; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return clib_error_return (0, "expected enable | disable | dump"); while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "enable") || unformat (line_input, "on")) is_enable = 1; else if (unformat (line_input, "disable") || unformat (line_input, "off")) is_enable = 0; else if (unformat (line_input, "dump")) is_dump = 1; else { error = clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); goto done; } } if (is_enable >= 0) cj_enable_disable (is_enable); if (is_dump > 0) cj_dump (); done: unformat_free (line_input); return error; }
static clib_error_t * rdma_create_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; rdma_create_if_args_t args; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; clib_memset (&args, 0, sizeof (rdma_create_if_args_t)); while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "host-if %s", &args.ifname)) ; else if (unformat (line_input, "name %s", &args.name)) ; else if (unformat (line_input, "rx-queue-size %u", &args.rxq_size)) ; else if (unformat (line_input, "tx-queue-size %u", &args.txq_size)) ; else if (unformat (line_input, "num-rx-queues %u", &args.rxq_num)) ; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } unformat_free (line_input); rdma_create_if (vm, &args); vec_free (args.ifname); vec_free (args.name); return args.error; }
clib_error_t * vlib_call_all_config_functions (vlib_main_t * vm, unformat_input_t * input, int is_early) { clib_error_t *error = 0; vlib_config_function_runtime_t *c, **all; uword *hash = 0, *p; uword i; hash = hash_create_string (0, sizeof (uword)); all = 0; c = vm->config_function_registrations; while (c) { hash_set_mem (hash, c->name, vec_len (all)); vec_add1 (all, c); unformat_init (&c->input, 0, 0); c = c->next_registration; } while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { u8 *s, *v; if (!unformat (input, "%s %v", &s, &v) || !(p = hash_get_mem (hash, s))) { error = clib_error_create ("unknown input `%s %v'", s, v); goto done; } c = all[p[0]]; if (vec_len (c->input.buffer) > 0) vec_add1 (c->input.buffer, ' '); vec_add (c->input.buffer, v, vec_len (v)); vec_free (v); vec_free (s); } for (i = 0; i < vec_len (all); i++) { c = all[i]; /* Is this an early config? Are we doing early configs? */ if (is_early ^ c->is_early) continue; /* Already called? */ if (hash_get (vm->init_functions_called, c->function)) continue; hash_set1 (vm->init_functions_called, c->function); error = c->function (vm, &c->input); if (error) goto done; } done: for (i = 0; i < vec_len (all); i++) { c = all[i]; unformat_free (&c->input); } vec_free (all); hash_free (hash); return error; }
static clib_error_t * lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, * line_input = &_line_input; u8 is_add = 1; ip_address_t lloc, rloc, *llocs = 0, *rlocs = 0; clib_error_t * error = 0; gid_address_t _reid, * reid = &_reid, _leid, * leid = &_leid; u8 reid_set = 0, leid_set = 0, is_negative = 0, vrf_set = 0, vni_set = 0; u32 vni, vrf, action = ~0; int rv; /* Get a line of input. */ if (! unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "del")) is_add = 0; else if (unformat (line_input, "add")) is_add = 1; else if (unformat (line_input, "leid %U", unformat_gid_address, leid)) { leid_set = 1; } else if (unformat (line_input, "reid %U", unformat_gid_address, reid)) { reid_set = 1; } else if (unformat (line_input, "vni %u", &vni)) { gid_address_vni (leid) = vni; gid_address_vni (reid) = vni; vni_set = 1; } else if (unformat (line_input, "vrf %u", &vrf)) { vrf_set = 1; } else if (unformat (line_input, "negative action %U", unformat_negative_mapping_action, &action)) { is_negative = 1; } else if (unformat (line_input, "lloc %U rloc %U", unformat_ip_address, &lloc, unformat_ip_address, &rloc)) { /* TODO: support p and w */ vec_add1 (llocs, lloc); vec_add1 (rlocs, rloc); } else { error = unformat_parse_error (line_input); goto done; } } unformat_free (line_input); if (!vni_set || !vrf_set) { error = clib_error_return(0, "vni and vrf must be set!"); goto done; } if (!reid_set) { error = clib_error_return(0, "remote eid must be set!"); goto done; } if (is_negative) { if (~0 == action) { error = clib_error_return(0, "no action set for negative tunnel!"); goto done; } } else { if (vec_len (llocs) == 0) { error = clib_error_return (0, "expected ip4/ip6 locators."); goto done; } if (vec_len (llocs) != 1) { error = clib_error_return (0, "multihoming not supported for now!"); goto done; } } if (!leid_set) { /* if leid not set, make sure it's the same AFI like reid */ gid_address_type(leid) = gid_address_type(reid); if (GID_ADDR_IP_PREFIX == gid_address_type (reid)) gid_address_ip_version(leid) = gid_address_ip_version(reid); } /* add fwd entry */ vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a; memset (a, 0, sizeof(a[0])); a->is_add = is_add; a->vni = vni; a->table_id = vrf; gid_address_copy(&a->seid, leid); gid_address_copy(&a->deid, reid); if (!is_negative) { a->slocator = llocs[0]; a->dlocator = rlocs[0]; } rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0); if (0 != rv) { error = clib_error_return(0, "failed to %s gpe tunnel!", is_add ? "add" : "delete"); } done: vec_free(llocs); vec_free(rlocs); return error; }
/* * enable or disable the output_feature */ static clib_error_t * nsim_output_feature_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { nsim_main_t *nsm = &nsim_main; unformat_input_t _line_input, *line_input = &_line_input; u32 sw_if_index = ~0; int enable_disable = 1; int rv; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "disable")) enable_disable = 0; else if (unformat (line_input, "%U", unformat_vnet_sw_interface, nsm->vnet_main, &sw_if_index)) ; else { clib_error_t *error = clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); unformat_free (line_input); return error; } } unformat_free (line_input); if (sw_if_index == ~0) return clib_error_return (0, "Please specify one interface..."); rv = nsim_output_feature_enable_disable (nsm, sw_if_index, enable_disable); switch (rv) { case 0: break; case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE: return clib_error_return (0, "Not configured, please 'set nsim' first"); case VNET_API_ERROR_INVALID_SW_IF_INDEX: return clib_error_return (0, "Invalid interface, only works on physical ports"); break; case VNET_API_ERROR_UNIMPLEMENTED: return clib_error_return (0, "Device driver doesn't support redirection"); break; default: return clib_error_return (0, "nsim_output_feature_enable_disable returned %d", rv); } return 0; }
int test_elog_main (unformat_input_t * input) { clib_error_t * error = 0; u32 i, n_iter, seed, max_events; elog_main_t _em, * em = &_em; u32 verbose; f64 min_sample_time; char * dump_file, * load_file, * merge_file, ** merge_files; u8 * tag, ** tags; n_iter = 100; max_events = 100000; seed = 1; verbose = 0; dump_file = 0; load_file = 0; merge_files = 0; tags = 0; min_sample_time = 2; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "iter %d", &n_iter)) ; else if (unformat (input, "seed %d", &seed)) ; else if (unformat (input, "dump %s", &dump_file)) ; else if (unformat (input, "load %s", &load_file)) ; else if (unformat (input, "tag %s", &tag)) vec_add1 (tags, tag); else if (unformat (input, "merge %s", &merge_file)) vec_add1 (merge_files, merge_file); else if (unformat (input, "verbose %=", &verbose, 1)) ; else if (unformat (input, "max-events %d", &max_events)) ; else if (unformat (input, "sample-time %f", &min_sample_time)) ; else { error = clib_error_create ("unknown input `%U'\n", format_unformat_error, input); goto done; } } #ifdef CLIB_UNIX if (load_file) { if ((error = elog_read_file (em, load_file))) goto done; } else if (merge_files) { uword i; elog_main_t * ems; vec_clone (ems, merge_files); elog_init (em, max_events); for (i = 0; i < vec_len (ems); i++) { if ((error = elog_read_file (i == 0 ? em : &ems[i], merge_files[i]))) goto done; if (i > 0) { elog_merge (em, tags[0], &ems[i], tags[i]); tags[0] = 0; } } } else #endif /* CLIB_UNIX */ { f64 t[2]; elog_init (em, max_events); elog_enable_disable (em, 1); t[0] = unix_time_now (); for (i = 0; i < n_iter; i++) { u32 j, n, sum; n = 1 + (random_u32 (&seed) % 128); sum = 0; for (j = 0; j < n; j++) sum += random_u32 (&seed); { ELOG_TYPE_XF (e); ELOG (em, e, sum); } { ELOG_TYPE_XF (e); ELOG (em, e, sum + 1); } { struct { u32 string_index; f32 f; } * d; ELOG_TYPE_DECLARE (e) = { .format = "fumble %s %.9f", .format_args = "t4f4", .n_enum_strings = 4, .enum_strings = { "string0", "string1", "string2", "string3", }, }; d = ELOG_DATA (em, e); d->string_index = sum & 3; d->f = (sum & 0xff) / 128.; } { ELOG_TYPE_DECLARE (e) = { .format = "bar %d.%d.%d.%d", .format_args = "i1i1i1i1", }; ELOG_TRACK (my_track); u8 * d = ELOG_TRACK_DATA (em, e, my_track); d[0] = i + 0; d[1] = i + 1; d[2] = i + 2; d[3] = i + 3; } { ELOG_TYPE_DECLARE (e) = { .format = "bar `%s'", .format_args = "s20", }; struct { char s[20]; } * d; u8 * v; d = ELOG_DATA (em, e); v = format (0, "foo %d%c", i, 0); memcpy (d->s, v, clib_min (vec_len (v), sizeof (d->s))); } { ELOG_TYPE_DECLARE (e) = { .format = "bar `%s'", .format_args = "T4", }; struct { u32 offset; } * d; d = ELOG_DATA (em, e); d->offset = elog_string (em, "string table %d", i); } } do { t[1] = unix_time_now (); } while (t[1] - t[0] < min_sample_time); } #ifdef CLIB_UNIX if (dump_file) { if ((error = elog_write_file (em, dump_file))) goto done; } #endif if (verbose) { elog_event_t * e, * es; es = elog_get_events (em); vec_foreach (e, es) { clib_warning ("%18.9f: %12U %U\n", e->time, format_elog_track, em, e, format_elog_event, em, e); } } done: if (error) clib_error_report (error); return 0; } #ifdef CLIB_UNIX int main (int argc, char * argv []) { unformat_input_t i; int r; unformat_init_command_line (&i, argv); r = test_elog_main (&i); unformat_free (&i); return r; }
static uword startup_config_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) { unix_main_t *um = &unix_main; u8 *buf = 0; uword l, n = 1; vlib_process_suspend (vm, 2.0); while (um->unix_config_complete == 0) vlib_process_suspend (vm, 0.1); if (um->startup_config_filename) { unformat_input_t sub_input; int fd; struct stat s; char *fn = (char *) um->startup_config_filename; fd = open (fn, O_RDONLY); if (fd < 0) { clib_warning ("failed to open `%s'", fn); return 0; } if (fstat (fd, &s) < 0) { clib_warning ("failed to stat `%s'", fn); bail: close (fd); return 0; } if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) { clib_warning ("not a regular file: `%s'", fn); goto bail; } while (n > 0) { l = vec_len (buf); vec_resize (buf, 4096); n = read (fd, buf + l, 4096); if (n > 0) { _vec_len (buf) = l + n; if (n < 4096) break; } else break; } if (um->log_fd && vec_len (buf)) { u8 *lv = 0; lv = format (lv, "%U: ***** Startup Config *****\n%v", format_timeval, 0 /* current bat-time */ , 0 /* current bat-format */ , buf); { int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv)); } vec_reset_length (lv); lv = format (lv, "%U: ***** End Startup Config *****\n", format_timeval, 0 /* current bat-time */ , 0 /* current bat-format */ ); { int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv)); } vec_free (lv); } if (vec_len (buf)) { unformat_init_vector (&sub_input, buf); vlib_cli_input (vm, &sub_input, 0, 0); /* frees buf for us */ unformat_free (&sub_input); } close (fd); } return 0; }