/* Function which parses command options; returns true if it ate an option */ static int parse(int c, char **argv, int invert, unsigned int *flags, const struct ipt_entry *entry, unsigned int *nfcache, struct ipt_entry_match **match) { struct ipt_mac_info *macinfo = (struct ipt_mac_info *)(*match)->data; switch (c) { case '1': check_inverse(optarg, &invert, &optind, 0); parse_mac(argv[optind-1], macinfo, IPTMAC_SRC); if (invert) macinfo->invert |= IPTMAC_SRC; macinfo->flag |= IPTMAC_SRC; *flags |= IPTMAC_SRC; break; case '2': check_inverse(optarg, &invert, &optind, 0); parse_mac(argv[optind-1], macinfo, IPTMAC_DST); if (invert) macinfo->invert |= IPTMAC_DST; macinfo->flag |= IPTMAC_DST; *flags |= IPTMAC_DST; break; default: return 0; } return 1; }
int cmd_macstatic(struct cli_context *ctx, int argc, char **argv, struct menu_node **nodev) { int sock_fd, status; int ifindex, vlan; int delmac_flag = 0; unsigned char mac[ETH_ALEN]; if (!strcmp(nodev[0]->name, "no")) { delmac_flag = 1; SHIFT_ARG(argc, argv, nodev); } if ((status = parse_mac(argv[2], mac))) { EX_STATUS_REASON(ctx, "Error parsing mac address %s: %s", argv[2], strerror(status)); return CLI_EX_REJECTED; } SW_SOCK_OPEN(ctx, sock_fd); vlan = atoi(argv[4]); SHIFT_ARG(argc, argv, nodev, 6); if_args_to_ifindex(ctx, argv, nodev, sock_fd, ifindex, status); if (delmac_flag) status = sw_ops->vlan_del_mac_static(sw_ops, ifindex, vlan, mac); else status = sw_ops->vlan_set_mac_static(sw_ops, ifindex, vlan, mac); SW_SOCK_CLOSE(ctx, sock_fd); if (status) { EX_STATUS_REASON_IOCTL(ctx, errno); return CLI_EX_REJECTED; } return CLI_EX_OK; }
int add_end_x (int argc, char **argv) { int ret = -1; struct in6_addr next_hop; if (argc > 8) { printf ("Too many parameters. Please try \"srconf localsid help\" \n"); goto end; } if (argc < 8) { printf ("Command line is not complete.\n"); goto end; } if (strcmp (argv[5], "ip") != 0 && strcmp (argv[5], "mac") != 0) { printf (" invalid token \"%s\"\n", argv[5]); goto end; } if (if_nametoindex (argv[7]) == 0) { printf ("Error: interface \"%s\" doesn't exist .\n", argv[7]); goto end; } params.oif = argv[7]; if (strcmp (argv[5], "mac") == 0) { ret = parse_mac (argv[6]); if (!ret) goto send_add; goto end; } if (inet_pton (AF_INET6, argv[6], &next_hop) != 1) { printf ("Error: inet6 prefix is expected rather than \"%s\".\n", argv[6]); goto end; } params.next = argv[6]; send_add: ret = send_add_command (); end: return ret; }
static int mac_parse(int c, char **argv, int invert, unsigned int *flags, const void *entry, struct xt_entry_match **match) { struct xt_mac_info *macinfo = (struct xt_mac_info *)(*match)->data; switch (c) { case '1': if (*flags & MAC_SRC) xtables_error(PARAMETER_PROBLEM, "mac match: Only use --mac-source ONCE!"); *flags |= MAC_SRC; macinfo->flags |= MAC_SRC; xtables_check_inverse(optarg, &invert, &optind, 0); if (invert) { macinfo->flags |= MAC_SRC_INV; } parse_mac(optarg, &macinfo->srcaddr); break; case '2': if (*flags & MAC_DST) xtables_error(PARAMETER_PROBLEM, "mac match: Only use --mac-destination ONCE!"); *flags |= MAC_DST; macinfo->flags |= MAC_DST; xtables_check_inverse(optarg, &invert, &optind, 0); if (invert){ macinfo->flags |= MAC_DST_INV; } parse_mac(optarg, &macinfo->dstaddr); break; default: return 0; } return 1; }
int send_wol (char *target, char *mac) { int sock; int optval = 1; int i, j, rc; char msg[1024]; int msglen = 0; struct sockaddr_in bcast; struct hostent *he; struct in_addr inaddr; unsigned char macaddr[6]; short bport = htons(32767); if (!parse_mac(macaddr, mac)) { printf ("Illegal MAC address '%s'\n", mac); exit (1); } if (!inet_aton(target, &inaddr)) { he = gethostbyname(target); inaddr = *(struct in_addr *)he->h_addr_list[0]; } for (i = 0; i < 6; i++) { msg[msglen++] = 0xff; } for (i = 0; i < 16; i++) { for (j = 0; j < sizeof(macaddr); j++) { msg[msglen++] = macaddr[j]; } } memset(&bcast, 0, sizeof(bcast)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inaddr.s_addr; bcast.sin_port = bport; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { printf ("Can't allocate socket\n"); return -1; } if ((rc=setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval))) < 0) { printf ("Can't socket option SO_BROADCAST: rc = %d, errno=%s(%d)\n", rc, strerror(errno), errno); return -1; } sendto(sock, &msg, msglen, 0, (struct sockaddr *)&bcast, sizeof(bcast)); return 0; }
/* Add, del, test parser */ static ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data) { struct ip_set_req_macipmap *mydata = (struct ip_set_req_macipmap *) data; char *saved = ipset_strdup(optarg); char *ptr, *tmp = saved; DP("macipmap: %p %p", optarg, data); ptr = strsep(&tmp, ":%"); parse_ip(ptr, &mydata->ip); if (tmp) parse_mac(tmp, mydata->ethernet); else memset(mydata->ethernet, 0, ETH_ALEN); free(saved); return 1; }
static int mac_parse(int c, char **argv, int invert, unsigned int *flags, const void *entry, struct xt_entry_match **match) { struct xt_mac_info *macinfo = (struct xt_mac_info *)(*match)->data; switch (c) { case '1': xtables_check_inverse(optarg, &invert, &optind, 0, argv); parse_mac(optarg, macinfo); if (invert) macinfo->invert = 1; *flags = 1; break; default: return 0; } return 1; }
static int parse(int c, char **argv, int invert, unsigned int *flags, const struct ip6t_entry *entry, unsigned int *nfcache, struct ip6t_entry_match **match) { struct ip6t_mac_info *macinfo = (struct ip6t_mac_info *)(*match)->data; switch (c) { case '1': check_inverse(optarg, &invert, &optind, 0); parse_mac(argv[optind-1], macinfo); if (invert) macinfo->invert = 1; *flags = 1; break; default: return 0; } return 1; }
// callback function - called for each name/value pair by ini parsing library static int openavbTLCfgCallback(void *user, const char *tlSection, const char *name, const char *value) { AVB_TRACE_ENTRY(AVB_TRACE_TL); parse_ini_data_t *pParseIniData = (parse_ini_data_t *)user; openavb_tl_cfg_t *pCfg = pParseIniData->pCfg; openavb_tl_cfg_name_value_t *pNVCfg = pParseIniData->pNVCfg; tl_state_t *pTLState = pParseIniData->pTLState; AVB_LOGF_DEBUG("name=[%s] value=[%s]", name, value); bool valOK = FALSE; char *pEnd; int i; if (MATCH(name, "role")) { if (MATCH(value, "talker")) { pCfg->role = AVB_ROLE_TALKER; valOK = TRUE; } else if (MATCH(value, "listener")) { pCfg->role = AVB_ROLE_LISTENER; valOK = TRUE; } } else if (MATCH(name, "dest_addr")) { valOK = parse_mac(value, &pCfg->dest_addr); } else if (MATCH(name, "stream_addr")) { valOK = parse_mac(value, &pCfg->stream_addr); } else if (MATCH(name, "stream_uid")) { errno = 0; pCfg->stream_uid = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->stream_uid <= UINT16_MAX) valOK = TRUE; } else if (MATCH(name, "max_interval_frames")) { errno = 0; pCfg->max_interval_frames = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_interval_frames <= UINT16_MAX) valOK = TRUE; } else if (MATCH(name, "max_frame_size")) { errno = 0; pCfg->max_frame_size = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_interval_frames <= UINT16_MAX) valOK = TRUE; } else if (MATCH(name, "sr_class")) { if (strlen(value) == 1) { if (tolower(value[0]) == 'a') { pCfg->sr_class = SR_CLASS_A; valOK = TRUE; } else if (tolower(value[0]) == 'b') { pCfg->sr_class = SR_CLASS_B; valOK = TRUE; } } } else if (MATCH(name, "sr_rank")) { if (strlen(value) == 1) { if (value[0] == '1') { pCfg->sr_rank = SR_RANK_REGULAR; valOK = TRUE; } else if (value[0] == '0') { pCfg->sr_rank = SR_RANK_EMERGENCY; valOK = TRUE; } } } else if (MATCH(name, "max_transit_usec")) { errno = 0; pCfg->max_transit_usec = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_transit_usec <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "max_transmit_deficit_usec")) { errno = 0; pCfg->max_transmit_deficit_usec = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_transmit_deficit_usec <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "internal_latency")) { errno = 0; pCfg->internal_latency = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->internal_latency <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "batch_factor")) { errno = 0; pCfg->batch_factor = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->batch_factor > 0 && pCfg->batch_factor <= INT32_MAX) valOK = TRUE; } else if (MATCH(name, "max_stale")) { errno = 0; pCfg->max_stale = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_stale >= 0 && pCfg->max_stale <= INT32_MAX) valOK = TRUE; } else if (MATCH(name, "raw_tx_buffers")) { errno = 0; pCfg->raw_tx_buffers = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->raw_tx_buffers <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "raw_rx_buffers")) { errno = 0; pCfg->raw_rx_buffers = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->raw_rx_buffers <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "report_seconds")) { errno = 0; pCfg->report_seconds = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && (int)pCfg->report_seconds >= 0 && pCfg->report_seconds <= INT32_MAX) valOK = TRUE; } else if (MATCH(name, "start_paused")) { // ignore this item - tl_host doesn't use it because // it pauses before reading any of its streams. errno = 0; long tmp; tmp = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && tmp >= 0 && tmp <= 1) { pCfg->start_paused = (tmp == 1); valOK = TRUE; } } else if (MATCH(name, "ifname")) { if_info_t ifinfo; if (openavbCheckInterface(value, &ifinfo)) { strncpy(pCfg->ifname, value, IFNAMSIZ - 1); valOK = TRUE; } } else if (MATCH(name, "vlan_id")) { errno = 0; long tmp; tmp = strtol(value, &pEnd, 0); // vlanID is 12 bit field if (*pEnd == '\0' && errno == 0 && tmp >= 0x0 && tmp <= 0xFFF) { pCfg->vlan_id = tmp; valOK = TRUE; } } else if (MATCH(name, "map_lib")) { if (pTLState->mapLib.libName) free(pTLState->mapLib.libName); pTLState->mapLib.libName = strdup(value); valOK = TRUE; } else if (MATCH(name, "map_fn")) { if (pTLState->mapLib.funcName) free(pTLState->mapLib.funcName); pTLState->mapLib.funcName = strdup(value); valOK = TRUE; } else if (MATCH(name, "intf_lib")) { if (pTLState->intfLib.libName) free(pTLState->intfLib.libName); pTLState->intfLib.libName = strdup(value); valOK = TRUE; } else if (MATCH(name, "intf_fn")) { if (pTLState->intfLib.funcName) free(pTLState->intfLib.funcName); pTLState->intfLib.funcName = strdup(value); valOK = TRUE; } else if (MATCH_LEFT(name, "intf_nv_", 8) || MATCH_LEFT(name, "map_nv_", 7)) { // Need to save the interface and mapping module configuration // until later (after those libraries are loaded.) // check if this setting replaces an earlier one for (i = 0; i < pNVCfg->nLibCfgItems; i++) { if (MATCH(name, pNVCfg->libCfgNames[i])) { if (pNVCfg->libCfgValues[i]) free(pNVCfg->libCfgValues[i]); pNVCfg->libCfgValues[i] = strdup(value); valOK = TRUE; } } if (i >= pNVCfg->nLibCfgItems) { // is a new name/value if (i >= MAX_LIB_CFG_ITEMS) { AVB_LOG_ERROR("Too many INI settings for interface/mapping modules"); } else { pNVCfg->libCfgNames[i] = strdup(name); pNVCfg->libCfgValues[i] = strdup(value); pNVCfg->nLibCfgItems++; valOK = TRUE; } } } else { // unmatched item, fail AVB_LOGF_ERROR("Unrecognized configuration item: name=%s", name); return 0; } if (!valOK) { // bad value AVB_LOGF_ERROR("Invalid value: name=%s, value=%s", name, value); return 0; } AVB_TRACE_EXIT(AVB_TRACE_TL); return 1; // OK }
int main(int argc, char **argv) { /* * chilli_query [ -s <unix-socket> ] <command> [<argument>] * (or maybe this should get the unix-socket from the config file) */ #ifdef HAVE_GLOB char *cmdsock = DEFSTATEDIR"/chilli*.sock"; glob_t globbuf; int i; #else char *cmdsock = DEFSTATEDIR"/chilli.sock"; #endif int s, len; struct sockaddr_un remote; char line[1024], *cmd; int argidx = 1; struct itimerval itval; #ifdef ENABLE_CLUSTER int peerid = -1; #endif int query_timeout = QUERY_TIMEOUT; if (getenv("QUERY_TIMEOUT")) { query_timeout = atoi(getenv("QUERY_TIMEOUT")); } set_signal(SIGALRM, timeout_alarm); memset(&itval, 0, sizeof(itval)); itval.it_interval.tv_sec = query_timeout; itval.it_interval.tv_usec = 0; itval.it_value.tv_sec = query_timeout; itval.it_value.tv_usec = 0; if (setitimer(ITIMER_REAL, &itval, NULL)) { log_err(errno, "setitimer() failed!"); } if (argc < 2) return usage(argv[0]); if (*argv[argidx] == '/') { /* for backward support, ignore a unix-socket given as first arg */ if (argc < 3) return usage(argv[0]); cmdsock = argv[argidx++]; } memset(&request,0,sizeof(request)); while (argidx < argc && *argv[argidx] == '-') { if (!strcmp(argv[argidx], "-s")) { argidx++; if (argidx >= argc) return usage(argv[0]); cmdsock = argv[argidx++]; #ifdef ENABLE_CLUSTER } else if (!strcmp(argv[argidx], "-p")) { argidx++; if (argidx >= argc) return usage(argv[0]); peerid = atoi(argv[argidx++]); #endif } else if (!strcmp(argv[argidx], "-json")) { request.options |= CMDSOCK_OPT_JSON; argidx++; } } if (argidx >= argc) return usage(argv[0]); cmd = argv[argidx++]; for (s = 0; commands[s].command; s++) { if (!strcmp(cmd, commands[s].command)) { request.type = commands[s].type; switch(request.type) { #ifdef ENABLE_INSPECT case CMDSOCK_INSPECT: #endif case CMDSOCK_LOGIN: case CMDSOCK_LOGOUT: case CMDSOCK_UPDATE: case CMDSOCK_AUTHORIZE: case CMDSOCK_ADD_GARDEN: case CMDSOCK_REM_GARDEN: argidx = process_args(argc, argv, argidx); if (request.type != CMDSOCK_LOGOUT || argidx >= argc) break; /* else, drop through */ case CMDSOCK_DHCP_DROP: case CMDSOCK_DHCP_RELEASE: case CMDSOCK_ENTRY_FOR_MAC: { if (argc < argidx+1) { fprintf(stderr, "%s requires a MAC address argument\n", cmd); return usage(argv[0]); } if (parse_mac(request.mac, argv[argidx])) return usage(argv[0]); /* do another switch to pick up param configs for authorize */ } break; case CMDSOCK_ENTRY_FOR_IP: { struct in_addr ip; /* Test for a valid ip argument. */ if (argc < argidx+1) { fprintf(stderr, "%s requires an IP address argument\n", cmd); return usage(argv[0]); } if (!inet_aton(argv[argidx], &ip)) { fprintf(stderr, "Invalid IP Address: %s\n", argv[argidx]); return usage(argv[0]); } request.ip.s_addr = ip.s_addr; } break; #ifdef ENABLE_MULTIROUTE case CMDSOCK_ROUTE: case CMDSOCK_ROUTE_GW: { unsigned int temp[PKT_ETH_ALEN]; char macstr[RADIUS_ATTR_VLEN]; int macstrlen; int i; if (argc < argidx + 2) { break; } if ((macstrlen = strlen(argv[argidx])) >= (RADIUS_ATTR_VLEN-1)) { fprintf(stderr, "%s: bad MAC address\n", argv[argidx]); break; } memcpy(macstr, argv[argidx], macstrlen); macstr[macstrlen] = 0; for (i=0; i<macstrlen; i++) if (!isxdigit((int) macstr[i])) macstr[i] = 0x20; if (sscanf(macstr, "%2x %2x %2x %2x %2x %2x", &temp[0], &temp[1], &temp[2], &temp[3], &temp[4], &temp[5]) != 6) { fprintf(stderr, "%s: bad MAC address\n", argv[argidx]); break; } for (i = 0; i < PKT_ETH_ALEN; i++) request.mac[i] = temp[i]; argidx++; request.d.sess.params.routeidx = atoi(argv[argidx]); if (request.type != CMDSOCK_ROUTE_GW) request.type = CMDSOCK_ROUTE_SET; /* do another switch to pick up param configs for authorize */ } break; #endif } break; } } if (!commands[s].command) { fprintf(stderr,"unknown command: %s\n",cmd); exit(1); } #ifdef ENABLE_CLUSTER if (peerid > -1) { struct sockaddr_in s; int blen = sizeof(struct pkt_chillihdr_t); uint8_t b[blen]; int fd = socket(AF_INET, SOCK_DGRAM, 0); printf("blen %d\n", blen); if (fd < 0) { log_err(errno,"socket() failed"); exit(1); } memset(&s, 0, sizeof(struct sockaddr_in)); s.sin_family = AF_INET; s.sin_port = htons(10203); s.sin_addr.s_addr = htonl(INADDR_BROADCAST); (void) safe_sendto(fd, b, blen, 0, (struct sockaddr *)&s, sizeof(struct sockaddr_in)); } else { #endif #ifdef HAVE_GLOB globbuf.gl_offs = 0; glob(cmdsock, GLOB_DOOFFS, NULL, &globbuf); if (!globbuf.gl_pathc) { fprintf(stderr,"no cmdsock sockets: %s\n",cmdsock); exit(1); } for (i=0 ; i < globbuf.gl_pathc; i++) { cmdsock = globbuf.gl_pathv[i]; if (globbuf.gl_pathc>1) { char header[256]; int headerlen; safe_snprintf(header, sizeof(header), "\nQuerying socket %s\n", cmdsock); headerlen=strlen(header); if (write(1, header, strlen(header))!=headerlen) { perror("write"); exit(1); } } #endif if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } remote.sun_family = AF_UNIX; strcpy(remote.sun_path, cmdsock); #if defined (__FreeBSD__) || defined (__APPLE__) || defined (__OpenBSD__) remote.sun_len = strlen(remote.sun_path) + 1; #endif len = offsetof(struct sockaddr_un, sun_path) + strlen(remote.sun_path); if (connect(s, (struct sockaddr *)&remote, len) == -1) { perror("connect"); exit(1); } if (safe_write(s, &request, sizeof(request)) != sizeof(request)) { perror("write"); exit(1); } while((len = safe_read(s, line, sizeof(line)-1)) > 0) { if (write(1, line, len) != len) { perror("write"); exit(1); } } if (len < 0) { perror("read"); exit(1); } shutdown(s, 2); close(s); #ifdef HAVE_GLOB } globfree(&globbuf); #endif #ifdef ENABLE_CLUSTER } #endif return 0; }
static int process_args(int argc, char *argv[], int argidx) { int c = argc - argidx; char is_data = 0; while(c > 0) { int i; for (i=0; i < count; i++) { if (!strcmp(argv[argidx], args[i].name)) { if (args[i].flag) { *(args[i].flag) |= args[i].flagbit; } else if (args[i].flagbit == 1) { if (is_data == 0) is_data = 1; else { fprintf(stderr, "data can only be once and by itself\n"); return argidx; } } else { if (i > 1) { if (is_data == 1) { fprintf(stderr, "data can only be once and by itself\n"); return argidx; } is_data = -1; } } if (c == 1 && args[i].length) { fprintf(stderr, "Argument %s requires a value\n", argv[argidx]); return usage(argv[0]); } switch(args[i].type) { case CMDSOCK_FIELD_NONE: break; case CMDSOCK_FIELD_MAC: parse_mac(((uint8_t *)args[i].field), argv[argidx+1]); break; case CMDSOCK_FIELD_STRING: safe_strncpy(((char *)args[i].field), argv[argidx+1], args[i].length); break; case CMDSOCK_FIELD_INTEGER: switch(args[i].length) { case 1: *((uint8_t *)args[i].field) |= (uint8_t)atoi(argv[argidx+1]); break; case 2: *((uint16_t *)args[i].field) |= (uint16_t)atoi(argv[argidx+1]); break; case 4: *((uint32_t *)args[i].field) |= (uint32_t)atol(argv[argidx+1]); break; case 8: *((uint64_t *)args[i].field) |= (uint64_t)atol(argv[argidx+1]); break; } break; case CMDSOCK_FIELD_IPV4: { struct in_addr ip; if (!inet_aton(argv[argidx+1], &ip)) { fprintf(stderr, "Invalid IP Address: %s\n", argv[argidx+1]); return usage(argv[0]); } ((struct in_addr *)args[i].field)->s_addr = ip.s_addr; break; } } break; } } if (i == count) { if (request.type == CMDSOCK_LOGOUT) break; fprintf(stderr, "Unknown argument: %s\n", argv[argidx]); return usage(argv[0]); } if (args[i].length) { c -= 2; argidx += 2; } else { c --; argidx ++; } } return argidx; }
int main(int argc, char* argv[]) { const char* interface; test_t* t; int c; printf("# ef_vi_version_str: %s\n", ef_vi_version_str()); while( (c = getopt (argc, argv, "n:s:wfbvVpta:A:")) != -1 ) switch( c ) { case 'n': cfg_iter = atoi(optarg); break; case 's': cfg_payload_len = atoi(optarg); break; case 'w': cfg_eventq_wait = 1; break; case 'f': cfg_fd_wait = 1; break; case 'v': cfg_use_vf = 1; break; case 'V': cfg_use_vport = 1; break; case 'p': cfg_phys_mode = 1; break; case 't': cfg_disable_tx_push = 1; break; case 'a': cfg_tx_align = atoi(optarg); break; case 'A': cfg_rx_align = atoi(optarg); break; case '?': usage(); default: TEST(0); } argc -= optind; argv += optind; if( argc != 7 ) usage(); interface = argv[1]; CL_CHK(parse_host(argv[2], &sa_local.sin_addr)); sa_local.sin_port = htons(atoi(argv[3])); CL_CHK(parse_mac(argv[4], remote_mac)); CL_CHK(parse_host(argv[5], &sa_remote.sin_addr)); sa_remote.sin_port = htons(atoi(argv[6])); if( cfg_payload_len > MAX_UDP_PAYLEN ) { fprintf(stderr, "WARNING: UDP payload length %d is larged than standard " "MTU\n", cfg_payload_len); } for( t = the_tests; t != the_tests + NUM_TESTS; ++t ) if( ! strcmp(argv[0], t->name) ) break; if( t == the_tests + NUM_TESTS ) usage(); printf("# udp payload len: %d\n", cfg_payload_len); printf("# iterations: %d\n", cfg_iter); do_init(interface); printf("# frame len: %d\n", tx_frame_len); printf("# rx align: %d\n", cfg_rx_align); printf("# tx align: %d\n", cfg_tx_align); t->fn(); /* Free all ef_vi resources we allocated above. This isn't * necessary as the process is about to exit which will free up all * resources. It is just to serve as an example of how to free up * ef_vi resources without exiting the process. */ do_free(); return 0; }
static int parse(int c, char **argv, int invert, unsigned int *flags, const struct ipt_entry *entry, struct ipt_entry_target **target) { struct ipt_clusterip_tgt_info *cipinfo = (struct ipt_clusterip_tgt_info *)(*target)->data; switch (c) { unsigned int num; case '1': cipinfo->flags |= CLUSTERIP_FLAG_NEW; if (*flags & PARAM_NEW) exit_error(PARAMETER_PROBLEM, "Can only specify `--new' once\n"); *flags |= PARAM_NEW; break; case '2': if (!(*flags & PARAM_NEW)) exit_error(PARAMETER_PROBLEM, "Can only specify hashmode combined with `--new'\n"); if (*flags & PARAM_HMODE) exit_error(PARAMETER_PROBLEM, "Can only specify hashmode once\n"); if (!strcmp(optarg, "sourceip")) cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP; else if (!strcmp(optarg, "sourceip-sourceport")) cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT; else if (!strcmp(optarg, "sourceip-sourceport-destport")) cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT_DPT; else exit_error(PARAMETER_PROBLEM, "Unknown hashmode `%s'\n", optarg); *flags |= PARAM_HMODE; break; case '3': if (!(*flags & PARAM_NEW)) exit_error(PARAMETER_PROBLEM, "Can only specify MAC combined with `--new'\n"); if (*flags & PARAM_MAC) exit_error(PARAMETER_PROBLEM, "Can only specify MAC once\n"); parse_mac(optarg, cipinfo->clustermac); if (!(cipinfo->clustermac[0] & 0x01)) exit_error(PARAMETER_PROBLEM, "MAC has to be a multicast ethernet address\n"); *flags |= PARAM_MAC; break; case '4': if (!(*flags & PARAM_NEW)) exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n"); if (*flags & PARAM_TOTALNODE) exit_error(PARAMETER_PROBLEM, "Can only specify total node number once\n"); if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0) exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg); cipinfo->num_total_nodes = (u_int16_t)num; *flags |= PARAM_TOTALNODE; break; case '5': if (!(*flags & PARAM_NEW)) exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n"); if (*flags & PARAM_LOCALNODE) exit_error(PARAMETER_PROBLEM, "Can only specify local node number once\n"); if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0) exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg); cipinfo->num_local_nodes = 1; cipinfo->local_nodes[0] = (u_int16_t)num; *flags |= PARAM_LOCALNODE; break; case '6': if (!(*flags & PARAM_NEW)) exit_error(PARAMETER_PROBLEM, "Can only specify hash init value combined with `--new'\n"); if (*flags & PARAM_HASHINIT) exit_error(PARAMETER_PROBLEM, "Can specify hash init value only once\n"); if (string_to_number(optarg, 0, UINT_MAX, &num) < 0) exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg); cipinfo->hash_initval = num; *flags |= PARAM_HASHINIT; break; default: return 0; } return 1; }
static void parse_cmdline(int argc, char **argp) { unsigned int i = 1; if(argc < 2) { show_usage(1); } if (!strcmp(argp[i], "-h")) { show_usage(0); } else if (!strcmp(argp[i], "-srq")) { command = COM_SRQ; i++; if(argc != 5) show_usage(1); parse_port(argp[i++]); parse_q(argp[i++]); parse_pt(argp[i++]); } else if (!strcmp(argp[i], "-sq")) { command = COM_SQ; i++; if(argc != 6) show_usage(1); parse_port(argp[i++]); parse_q(argp[i++]); parse_mac(argp[i++], mac); } else if (!strcmp(argp[i], "-srp")) { command = COM_SRP; i++; if(argc != 4) show_usage(1); parse_port(argp[i++]); parse_policy(argp[i++]); } else if (!strcmp(argp[i], "-srqw")) { command = COM_SRQW; i++; if(argc != 5) show_usage(1); parse_port(argp[i++]); parse_q(argp[i++]); parse_hex_val(argp[i++], &weight); } else if (!strcmp(argp[i], "-stp")) { command = COM_STP; i++; if(argc != 6) show_usage(1); parse_port(argp[i++]); parse_q(argp[i++]); parse_hex_val(argp[i++], &weight); parse_policy(argp[i++]); } else if (!strcmp(argp[i], "-fprs")) { command = COM_IP_RULE_SET; i++; if(argc != 8) show_usage(1); parse_dec_val(argp[i++], &inport); parse_dec_val(argp[i++], &outport); parse_ip(argp[i++], &dip); parse_ip(argp[i++], &sip); parse_mac(argp[i++], da); parse_mac(argp[i++], sa); } else if (!strcmp(argp[i], "-fprd")) { command = COM_IP_RULE_DEL; i++; if(argc != 4) show_usage(1); parse_ip(argp[i++], &dip); parse_ip(argp[i++], &sip); } else if (!strcmp(argp[i], "-fp_st")) { command = COM_NFP_STATUS; if(argc != 2) show_usage(1); } else if (!strcmp(argp[i], "-fp_print")) { command = COM_NFP_PRINT; i++; if (argc != 3) show_usage(1); parse_db_name(argp[i++]); } else if (!strcmp(argp[i], "-txdone")) { command = COM_TXDONE_Q; i++; if(argc != 3) show_usage(1); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-txen")) { command = COM_TX_EN; i++; if(argc != 4) show_usage(1); parse_port(argp[i++]); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-lro")) { command = COM_LRO; i++; if(argc != 4) show_usage(1); parse_port(argp[i++]); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-lro_desc")) { command = COM_LRO_DESC; i++; if(argc != 4) show_usage(1); parse_port(argp[i++]); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-reuse")) { command = COM_SKB_REUSE; i++; if(argc != 3) show_usage(1); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-recycle")) { command = COM_SKB_RECYCLE; i++; if(argc != 3) show_usage(1); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-nfp")) { command = COM_NFP; i++; if(argc != 3) show_usage(1); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-rxcoal")) { command = COM_RX_COAL; i++; if(argc != 4) show_usage(1); parse_port(argp[i++]); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-txcoal")) { command = COM_TX_COAL; i++; if(argc != 4) show_usage(1); parse_port(argp[i++]); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-ejp")) { command = COM_EJP_MODE; i++; if(argc != 4) show_usage(1); parse_port(argp[i++]); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-tos")) { command = COM_TOS_MAP; i++; if(argc != 5) show_usage(1); parse_port(argp[i++]); parse_q(argp[i++]); parse_hex_val(argp[i++], &value); } else if (!strcmp(argp[i], "-tx_noq")) { command = COM_TX_NOQUEUE; i++; if(argc != 4) show_usage(1); parse_port(argp[i++]); parse_dec_val(argp[i++], &value); } else if (!strcmp(argp[i], "-St")) { command = COM_STS; i++; if(argc < 4) show_usage(1); parse_status(argp[i++]); if( status == STS_PORT_Q ) { if(argc != 5) show_usage(1); parse_q(argp[i++]); } else if(argc != 4) show_usage(1); parse_port(argp[i++]); } else { show_usage(i++); } }
static int set_general(struct sc_stream* stream, char* stream_str) { int rc = 0; uint8_t mac[6]; char* key; char* next_field = stream_str; /* General format is series of key=value pairs, separated by ",". */ while( next_field && (rc == 0) ) { char* value; char* field; field = strsep(&next_field, ","); /* Split key and value */ value = field; key = strsep(&value, "="); if( !value ) { /* Handle some key-only magic values */ if( !strcmp(key, "all") ) rc = sc_stream_all(stream); /* The following needs a strncmp because we pass the stream as * 'sniff [0,1]' */ else if( !strncmp(key, "sniff", strlen("sniff")) ) rc = sc_stream_sniff(stream, key); else if( !strcmp(key, "ip") ) rc = set_eth_type(stream, key); else if( !strcmp(key, "udp") || !strcmp(key, "tcp") ) rc = set_protocol(stream, key); else { fprintf(stderr, "%s: ERROR: No value for key %s\n", __func__, key); return -EINVAL; } } else { if( !strcmp(key, "dmac") ) { if( parse_mac(value, mac) < 0 ) { fprintf(stderr, "%s: ERROR: Failed to parse mac \"%s\"\n", __func__, key); return -EINVAL; } rc = sc_stream_eth_dhost(stream, mac); } else if( !strcmp(key, "smac") ) { if( parse_mac(value, mac) < 0 ) fprintf(stderr, "%s: ERROR: Failed to parse mac \"%s\"\n", __func__, key); return -EINVAL; rc = sc_stream_eth_shost(stream, mac); } else if( !strcmp(key, "vid") ) { rc = set_vlan_id(stream, value); } else if( !strcmp(key, "eth_type") ) { rc = set_eth_type(stream, value); } else if( !strcmp(key, "shost") ) { rc = sc_stream_ip_source_host(stream, value); } else if( !strcmp(key, "dhost") ) { rc = sc_stream_ip_dest_host(stream, value); } else if( !strcmp(key, "ip_protocol") ) { rc = set_protocol(stream, value); } else if( !strcmp(key, "sport") ) { rc = sc_stream_ip_source_port(stream, value); } else if( !strcmp(key, "dport") ) { rc = sc_stream_ip_dest_port(stream, value); } else { fprintf(stderr, "%s: ERROR: Unrecognised key \"%s\"\n", __func__, key); return -EINVAL; } } } return rc; }
void parse_args(int argc, char *argv[], address_pool *pool) { int c; opterr = 0; while ((c = getopt (argc, argv, "a:d:o:p:s:")) != -1) switch (c) { case 'a': // parse IP address pool { char *opt = strdup(optarg); char *sfirst = opt; char *slast = strchr(opt, ','); if (slast == NULL) usage("error: comma not present in option -a.", 1); *slast = '\0'; slast++; uint32_t *first, *last; if (parse_ip(sfirst, (void **)&first) != 4) usage("error: invalid first ip in address pool.", 1); if (parse_ip(slast, (void **)&last) != 4) usage("error: invalid last ip in address pool.", 1); pool->indexes.first = *first; pool->indexes.last = *last; pool->indexes.current = *first; free(first); free(last); free(opt); break; } case 'd': // network device to use { strncpy(pool->device, optarg, sizeof(pool->device)); break; } case 'o': // parse dhcp option { uint8_t id; char *opt = strdup(optarg); char *name = opt; char *value = strchr(opt, ','); if (value == NULL) usage("error: comma not present in option -o.", 1); *value = '\0'; value++; dhcp_option *option = calloc(1, sizeof(*option)); if((id = parse_option(option, name, value)) == 0) { char msg[128]; snprintf(msg, sizeof(msg), "error: invalid dhcp option specified: %s,%s", name, value); usage(msg, 1); } append_option(&pool->options, option); if(option->id == IP_ADDRESS_LEASE_TIME) pool->lease_time = ntohl(*((uint32_t *)option->data)); free(option); free(opt); break; } case 'p': // parse pending time { time_t *t; if(parse_long(optarg, (void **)&t) != 4) usage("error: invalid pending time.", 1); pool->pending_time = *t; free(t); break; } case 's': // static binding { char *opt = strdup(optarg); char *shw = opt; char *sip = strchr(opt, ','); if (sip == NULL) usage("error: comma not present in option -s.", 1); *sip = '\0'; sip++; uint32_t *ip; uint8_t *hw; if (parse_mac(shw, (void **)&hw) != 6) usage("error: invalid mac address in static binding.", 1); if (parse_ip(sip, (void **)&ip) != 4) usage("error: invalid ip in static binding.", 1); add_binding(&pool->bindings, *ip, hw, 6, 1); free(ip); free(hw); free(opt); } case '?': usage(NULL, 1); default: usage(NULL, 1); } if(optind >= argc) usage("error: server address not provided.", 1); uint32_t *ip; if (parse_ip(argv[optind], (void **)&ip) != 4) usage("error: invalid server address.", 1); pool->server_id = *ip; free(ip); }
// callback function - called for each name/value pair by ini parsing library static int openavbIniCfgCallback(void *user, const char *tlSection, const char *name, const char *value) { AVB_TRACE_ENTRY(AVB_TRACE_AVDECC); openavb_tl_data_cfg_t *pCfg = (openavb_tl_data_cfg_t *)user; AVB_LOGF_DEBUG("name=[%s] value=[%s]", name, value); bool valOK = FALSE; char *pEnd; if (MATCH(name, "role")) { if (MATCH(value, "talker")) { pCfg->role = AVB_ROLE_TALKER; valOK = TRUE; } else if (MATCH(value, "listener")) { pCfg->role = AVB_ROLE_LISTENER; valOK = TRUE; } } else if (MATCH(name, "initial_state")) { if (MATCH(value, "running")) { pCfg->initial_state = TL_INIT_STATE_RUNNING; valOK = TRUE; } else if (MATCH(value, "stopped")) { pCfg->initial_state = TL_INIT_STATE_STOPPED; valOK = TRUE; } } else if (MATCH(name, "dest_addr")) { valOK = parse_mac(value, &pCfg->dest_addr); } else if (MATCH(name, "stream_addr")) { valOK = parse_mac(value, &pCfg->stream_addr); } else if (MATCH(name, "stream_uid")) { errno = 0; pCfg->stream_uid = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->stream_uid <= UINT16_MAX) valOK = TRUE; } else if (MATCH(name, "max_interval_frames")) { errno = 0; pCfg->max_interval_frames = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_interval_frames <= UINT16_MAX) valOK = TRUE; } else if (MATCH(name, "max_frame_size")) { errno = 0; pCfg->max_frame_size = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_interval_frames <= UINT16_MAX) valOK = TRUE; } else if (MATCH(name, "sr_class")) { if (strlen(value) == 1) { if (tolower(value[0]) == 'a') { pCfg->sr_class = SR_CLASS_A; valOK = TRUE; } else if (tolower(value[0]) == 'b') { pCfg->sr_class = SR_CLASS_B; valOK = TRUE; } } } else if (MATCH(name, "sr_rank")) { if (strlen(value) == 1) { if (value[0] == '1') { pCfg->sr_rank = SR_RANK_REGULAR; valOK = TRUE; } else if (value[0] == '0') { pCfg->sr_rank = SR_RANK_EMERGENCY; valOK = TRUE; } } } else if (MATCH(name, "max_transit_usec")) { errno = 0; pCfg->max_transit_usec = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_transit_usec <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "max_transmit_deficit_usec")) { errno = 0; pCfg->max_transmit_deficit_usec = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_transmit_deficit_usec <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "internal_latency")) { errno = 0; pCfg->internal_latency = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->internal_latency <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "batch_factor")) { errno = 0; pCfg->batch_factor = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->batch_factor > 0 && pCfg->batch_factor <= INT32_MAX) valOK = TRUE; } else if (MATCH(name, "max_stale")) { errno = 0; pCfg->max_stale = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->max_stale >= 0 && pCfg->max_stale <= INT32_MAX) valOK = TRUE; } else if (MATCH(name, "raw_tx_buffers")) { errno = 0; pCfg->raw_tx_buffers = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->raw_tx_buffers <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "raw_rx_buffers")) { errno = 0; pCfg->raw_rx_buffers = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->raw_rx_buffers <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "report_seconds")) { errno = 0; pCfg->report_seconds = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && (int)pCfg->report_seconds >= 0 && pCfg->report_seconds <= INT32_MAX) valOK = TRUE; } else if (MATCH(name, "start_paused")) { // ignore this item - tl_host doesn't use it because // it pauses before reading any of its streams. errno = 0; long tmp; tmp = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && tmp >= 0 && tmp <= 1) { pCfg->start_paused = (tmp == 1); valOK = TRUE; } } else if (MATCH(name, "vlan_id")) { errno = 0; long tmp; tmp = strtol(value, &pEnd, 0); // vlanID is 12 bit field if (*pEnd == '\0' && errno == 0 && tmp >= 0x0 && tmp <= 0xFFF) { pCfg->vlan_id = tmp; valOK = TRUE; } } else if (MATCH(name, "fixed_timestamp")) { errno = 0; long tmp; tmp = strtol(value, &pEnd, 0); if (*pEnd == '\0' && errno == 0) { pCfg->fixed_timestamp = tmp; valOK = TRUE; } } else if (MATCH(name, "spin_wait")) { errno = 0; long tmp; tmp = strtol(value, &pEnd, 0); if (*pEnd == '\0' && errno == 0) { pCfg->spin_wait = (tmp == 1); valOK = TRUE; } } else if (MATCH(name, "tx_blocking_in_intf")) { errno = 0; long tmp; tmp = strtol(value, &pEnd, 0); if (*pEnd == '\0' && errno == 0) { pCfg->tx_blocking_in_intf = tmp; valOK = TRUE; } } else if (MATCH(name, "thread_rt_priority")) { errno = 0; long tmp; tmp = strtol(value, &pEnd, 0); if (*pEnd == '\0' && errno == 0) { pCfg->thread_rt_priority = tmp; valOK = TRUE; } } else if (MATCH(name, "thread_affinity")) { errno = 0; unsigned long tmp; tmp = strtoul(value, &pEnd, 0); if (*pEnd == '\0' && errno == 0) { pCfg->thread_affinity = tmp; valOK = TRUE; } } else if (MATCH(name, "friendly_name")) { strncpy(pCfg->friendly_name, value, FRIENDLY_NAME_SIZE - 1); valOK = TRUE; } else if (MATCH(name, "current_sampling_rate")) { errno = 0; pCfg->current_sampling_rate = strtol(value, &pEnd, 10); if (*pEnd == '\0' && errno == 0 && pCfg->current_sampling_rate <= UINT32_MAX) valOK = TRUE; } else if (MATCH(name, "sampling_rates")) { errno = 0; memset(pCfg->sampling_rates,0,sizeof(pCfg->sampling_rates)); char *rate, *copy; copy = strdup(value); rate = strtok(copy,","); int i = 0,break_flag = 0; while (rate != NULL ) { pCfg->sampling_rates[i] = strtol(rate,&pEnd, 10); if (*pEnd != '\0' || errno != 0 || pCfg->sampling_rates[i] > UINT32_MAX) { break_flag = 1; break; } rate = strtok(NULL,","); i++; } if (break_flag != 1) { valOK = TRUE; pCfg->sampling_rates_count = i; } } else if (strcmp(name, "intf_nv_audio_rate") == 0) { long int val = strtol(value, &pEnd, 10); if (val >= AVB_AUDIO_RATE_8KHZ && val <= AVB_AUDIO_RATE_192KHZ) { pCfg->audioRate = val; valOK = TRUE; } else { AVB_LOG_ERROR("Invalid audio rate configured for intf_nv_audio_rate."); pCfg->audioRate = AVB_AUDIO_RATE_44_1KHZ; } } else if (strcmp(name, "intf_nv_audio_bit_depth") == 0) { long int val = strtol(value, &pEnd, 10); if (val >= AVB_AUDIO_BIT_DEPTH_1BIT && val <= AVB_AUDIO_BIT_DEPTH_64BIT) { pCfg->audioBitDepth = val; valOK = TRUE; } else { AVB_LOG_ERROR("Invalid audio type configured for intf_nv_audio_bits."); pCfg->audioBitDepth = AVB_AUDIO_BIT_DEPTH_24BIT; } } else if (strcmp(name, "intf_nv_audio_channels") == 0) { long int val = strtol(value, &pEnd, 10); if (val >= AVB_AUDIO_CHANNELS_1 && val <= AVB_AUDIO_CHANNELS_8) { pCfg->audioChannels = val; valOK = TRUE; } else { AVB_LOG_ERROR("Invalid audio channels configured for intf_nv_audio_channels."); pCfg->audioChannels = AVB_AUDIO_CHANNELS_2; } } else if (MATCH(name, "map_fn")) { errno = 0; memset(pCfg->map_fn,0,sizeof(pCfg->map_fn)); strncpy(pCfg->map_fn,value,sizeof(pCfg->map_fn)-1); valOK = TRUE; } else { // Ignored item. AVB_LOGF_DEBUG("Unhandled configuration item: name=%s, value=%s", name, value); // Don't abort for this item. valOK = TRUE; } if (!valOK) { // bad value AVB_LOGF_ERROR("Invalid value: name=%s, value=%s", name, value); return 0; } AVB_TRACE_EXIT(AVB_TRACE_AVDECC); return 1; // OK }
int main(int argc, char* argv[]) { int ifindex; test_t* t; int c; printf("# ef_vi_version_str: %s\n", ef_vi_version_str()); while( (c = getopt (argc, argv, "n:s:wbvptcd")) != -1 ) switch( c ) { case 'n': cfg_iter = atoi(optarg); break; case 's': cfg_payload_len = atoi(optarg); break; case 'w': cfg_wait = 1; break; case 'v': cfg_use_vf = 1; break; case 'p': cfg_phys_mode = 1; break; case 't': cfg_disable_tx_push = 1; break; case 'c': cfg_precopy = 0; break; case '?': usage(); default: TEST(0); } argc -= optind; argv += optind; if( argc != 7 ) usage(); CL_CHK(parse_interface(argv[1], &ifindex)); CL_CHK(parse_host(argv[2], &sa_local.sin_addr)); sa_local.sin_port = htons(atoi(argv[3])); CL_CHK(parse_mac(argv[4], remote_mac)); CL_CHK(parse_host(argv[5], &sa_remote.sin_addr)); sa_remote.sin_port = htons(atoi(argv[6])); if( cfg_payload_len > MAX_UDP_PAYLEN ) { fprintf(stderr, "WARNING: UDP payload length %d is larged than standard " "MTU\n", cfg_payload_len); } for( t = the_tests; t != the_tests + NUM_TESTS; ++t ) if( ! strcmp(argv[0], t->name) ) break; if( t == the_tests + NUM_TESTS ) usage(); do_init(ifindex); printf("# udp payload len: %d\n", cfg_payload_len); printf("# iterations: %d\n", cfg_iter); printf("# frame len: %d\n", tx_frame_len); if( ! cfg_precopy ) printf("# copy on critical path\n"); t->fn(&vi); return 0; }
int main(int argc, char **argv) { struct eeprom_dev *dev; int ch; int writing = 0; char *tmp; struct novena_eeprom_data_v2 newrom; int newdata = 0; int update_mac = 0; int update_features = 0; int update_serial = 0; int update_oops_start = 0; int update_oops_length = 0; int update_page_size = 0; int update_total_size = 0; int update_lvds1 = 0; int update_lvds2 = 0; int update_hdmi = 0; dev = eeprom_open(I2C_BUS, EEPROM_ADDRESS); if (!dev) return 1; while ((ch = getopt(argc, argv, "hm:s:f:wo:p:l:1:2:d:e:i:")) != -1) { switch(ch) { /* MAC address */ case 'm': if (parse_mac(optarg, newrom.mac)) return 1; update_mac = 1; break; /* Serial number */ case 's': newrom.serial = strtoul(optarg, NULL, 0); update_serial = 1; break; /* Featuresset */ case 'f': newrom.features = parse_features(optarg); if (newrom.features == -1) return 1; update_features = 1; break; case 'o': newrom.eepromoops_offset = strtoul(optarg, &tmp, 0); update_oops_start = 1; if (tmp && *tmp) { newrom.eepromoops_length = strtoul(tmp + 1, NULL, 0); update_oops_length = 1; } break; case 'p': newrom.page_size = strtoul(optarg, NULL, 0); update_page_size = 1; break; case 'l': newrom.eeprom_size = strtoul(optarg, NULL, 0); update_total_size = 1; break; case '1': if (parse_modesetting(&newrom.lvds1, optarg)) return 1; update_lvds1 = 1; break; case '2': if (parse_modesetting(&newrom.lvds2, optarg)) return 1; update_lvds2 = 1; break; case 'd': if (parse_modesetting(&newrom.hdmi, optarg)) return 1; update_hdmi = 1; break; case 'e': return eeprom_export(dev, optarg); case 'i': if (eeprom_import(dev, optarg)) return 1; newdata = 1; break; /* Write data */ case 'w': writing = 1; break; case 'h': print_usage(argv[0]); return 1; default: printf("Unrecognized option: %c\n", ch); print_usage(argv[0]); return 1; } } argc -= optind; argv += optind; if (update_mac || update_serial || update_features || update_oops_start || update_oops_length || update_page_size || update_total_size || update_lvds1 || update_lvds2 || update_hdmi) newdata = 1; if (argc) print_usage(argv[0]); else if (!writing) { if (newdata) printf("Not writing data, as -w was not specified\n"); printf("Current EEPROM settings:\n"); print_eeprom_data(dev); } else { int ret; ret = eeprom_read(dev); if (ret) return 1; if (dev->data.v1.version == 1) { printf("Updating v1 EEPROM to v2...\n"); eeprom_upgrade_v1_to_v2(dev); } else if (dev->data.v1.version == 2) { /* Ignore v2 */; } else { if (memcmp(dev->data.v2.signature, NOVENA_SIGNATURE, sizeof(dev->data.v2.signature))) printf("Blank EEPROM found, " "setting defaults...\n"); else fprintf(stderr, "Unrecognized EEPROM version found " "(v%d), overwriting with v2\n", dev->data.v1.version); eeprom_get_defaults(dev); } if (update_mac) memcpy(&dev->data.v2.mac, newrom.mac, sizeof(newrom.mac)); if (update_serial) dev->data.v2.serial = newrom.serial; if (update_features) dev->data.v2.features = newrom.features; if (update_oops_start) dev->data.v2.eepromoops_offset = newrom.eepromoops_offset; if (update_oops_length) dev->data.v2.eepromoops_length = newrom.eepromoops_length; if (update_page_size) dev->data.v2.page_size = newrom.page_size; if (update_total_size) dev->data.v2.eeprom_size = newrom.eeprom_size; if (update_lvds1) memcpy(&dev->data.v2.lvds1, &newrom.lvds1, sizeof(dev->data.v2.lvds1)); if (update_lvds2) memcpy(&dev->data.v2.lvds2, &newrom.lvds2, sizeof(dev->data.v2.lvds2)); if (update_hdmi) memcpy(&dev->data.v2.hdmi, &newrom.hdmi, sizeof(dev->data.v2.hdmi)); memcpy(&dev->data.v2.signature, NOVENA_SIGNATURE, sizeof(dev->data.v2.signature)); dev->data.v2.version = 2; ret = eeprom_write(dev); if (ret) { printf("EEPROM write failed\n"); return 1; } printf("Updated EEPROM. New values:\n"); print_eeprom_data(dev); } eeprom_close(&dev); return 0; }
int main (int argc, char *argv[]) { int sock; int optval = 1; int version =0; int i, j, c, rc; char msg[1024]; int msglen = 0; struct sockaddr_in bcast; struct hostent *he; struct in_addr inaddr; unsigned char macaddr[6]; char *mac = DEFAULTMAC; char *target = DEFAULTTARGET; short bport = htons(32767); while ((c = getopt(argc, argv, "hvp:m:b:")) != EOF) { switch (c) { case 'b': target = optarg; break; case 'm': mac = optarg; break; case 'p': bport = htons(atoi(optarg)); break; case 'v': version++; break; case 'h': case '?': usage(argv[0]); } } if (version) { printf ("Version: %s\n", versionid); if (version > 1) { printf (" RCSID: %s\n", rcsid); } exit (0); } if (argv[optind] != NULL) { mac = argv[optind++]; } if (argv[optind] != NULL) { target = argv[optind++]; } if (argv[optind] != NULL) { bport = htons(atoi(argv[optind++])); } if (argv[optind] != NULL) { usage(argv[0]); } if (!parse_mac(macaddr, mac)) { printf ("Illegal MAC address '%s'\n", mac); exit (1); } if (!inet_aton(target, &inaddr)) { he = gethostbyname(target); inaddr = *(struct in_addr *)he->h_addr_list[0]; } for (i = 0; i < 6; i++) { msg[msglen++] = 0xff; } for (i = 0; i < 16; i++) { for (j = 0; j < sizeof(macaddr); j++) { msg[msglen++] = macaddr[j]; } } memset(&bcast, 0, sizeof(bcast)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inaddr.s_addr; bcast.sin_port = bport; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { printf ("Can't allocate socket\n"); exit (1); } if ((rc=setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval))) < 0) { printf ("Can't socket option SO_BROADCAST: rc = %d, errno=%s(%d)\n", rc, strerror(errno), errno); exit (1); } sendto(sock, &msg, msglen, 0, (struct sockaddr *)&bcast, sizeof(bcast)); return 0; }
void netfe_init(void) { int index = 0; netfe_t **link = &net_front_ends; while (1) { int n; char xs_key[256]; snprintf(xs_key, sizeof(xs_key), "device/vif/%d/backend-id", index); int rs = xenstore_read_int(&n, xs_key); if (rs != 0) break; // FE/(index) is present domid_t backend_id = (domid_t)n; netfe_t *fe = (netfe_t *)mm_alloc_pages(PSIZE(sizeof(netfe_t))); memset(fe, 0, sizeof(*fe)); // setup shared rings fe->rxs = (netif_rx_sring_t *)mm_alloc_page(); assert(fe->rxs != 0); fe->txs = (netif_tx_sring_t *)mm_alloc_page(); assert(fe->txs != 0); SHARED_RING_INIT(fe->rxs); SHARED_RING_INIT(fe->txs); FRONT_RING_INIT(&fe->rx_ring, fe->rxs, PAGE_SIZE); FRONT_RING_INIT(&fe->tx_ring, fe->txs, PAGE_SIZE); grants_allow_access(&fe->rx_ref, backend_id, virt_to_mfn(fe->rxs)); grants_allow_access(&fe->tx_ref, backend_id, virt_to_mfn(fe->txs)); // set up receive buffers for (int i = 0; i < NR_RX_BUFFERS; i++) { fe->rx_buffers[i] = mm_alloc_page(); assert(fe->rx_buffers[i] != 0); unsigned long mfn = virt_to_mfn(fe->rx_buffers[i]); grants_allow_access(&fe->rx_buf_refs[i], backend_id, mfn); } // set up send buffers fe->free_tx_head = NO_TX_BUFFER; for (int i = 0; i < NR_TX_BUFFERS; i++) { fe->tx_buffers[i] = mm_alloc_page(); assert(fe->tx_buffers[i] != 0); unsigned long mfn = virt_to_mfn(fe->tx_buffers[i]); grants_allow_access(&fe->tx_buf_refs[i], backend_id, mfn); fe->free_tx_bufs[i] = fe->free_tx_head; fe->free_tx_head = i; } // set up interrupt fe->evtchn = event_alloc_unbound(backend_id); event_bind(fe->evtchn, netfe_int, (void *)fe); snprintf(xs_key, sizeof(xs_key), "device/vif/%d/rx-ring-ref", index); rs = xenstore_write_uint(xs_key, fe->rx_ref); assert(rs == 0); snprintf(xs_key, sizeof(xs_key), "device/vif/%d/tx-ring-ref", index); rs = xenstore_write_uint(xs_key, fe->tx_ref); assert(rs == 0); snprintf(xs_key, sizeof(xs_key), "device/vif/%d/event-channel", index); rs = xenstore_write_uint(xs_key, fe->evtchn); assert(rs == 0); snprintf(xs_key, sizeof(xs_key), "device/vif/%d/request-rx-copy", index); rs = xenstore_write(xs_key, "1"); assert(rs == 0); snprintf(xs_key, sizeof(xs_key), "device/vif/%d/feature-no-csum-offload", index); rs = xenstore_write(xs_key, "1"); assert(rs == 0); snprintf(xs_key, sizeof(xs_key), "device/vif/%d/feature-rx-notify", index); rs = xenstore_write(xs_key, "1"); assert(rs == 0); snprintf(xs_key, sizeof(xs_key), "device/vif/%d/state", index); rs = xenstore_write(xs_key, "4"); // XenbusStateConnected assert(rs == 0); // read MAC address char buf[64]; snprintf(xs_key, sizeof(xs_key), "device/vif/%d/mac", index); rs = xenstore_read(xs_key, buf, sizeof(buf)); assert(rs == 0); rs = parse_mac(buf, fe->mac); assert(rs == 0); fe->mac_len = ETH_ALEN; printk("\reth%d: MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n", index, fe->mac[0], fe->mac[1], fe->mac[2], fe->mac[3], fe->mac[4], fe->mac[5]); // // Publish EXT_RX_BUFFERS requests only and replenish then to this number // during each interrupt handler invocation. // for (int i = 0; i < EXT_RX_BUFFERS; i++) { netif_rx_request_t *req = RING_GET_REQUEST(&fe->rx_ring, fe->rx_ring.req_prod_pvt); req->id = i; //rx_id++; req->gref = fe->rx_buf_refs[i]; fe->rx_ring.req_prod_pvt++; } RING_PUSH_REQUESTS(&fe->rx_ring); event_kick(fe->evtchn); fe->index = index++; //fe->next = 0; //fe->attached_lwip_netif = 0; //fe->attached_outlet = 0; // add to net_front_ends list *link = fe; link = &fe->next; } num_net_front_ends = index; }