/* iperf Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include #include #include "esp_log.h" #include "esp_err.h" #include "esp_console.h" #include "argtable3/argtable3.h" #include "protocol_examples_common.h" #include "iperf.h" static const char *TAG = "cmd"; /* "iperf" command */ static struct { struct arg_str *ip; struct arg_lit *server; struct arg_lit *udp; struct arg_lit *version; struct arg_int *port; struct arg_int *length; struct arg_int *interval; struct arg_int *time; struct arg_int *bw_limit; struct arg_lit *abort; struct arg_end *end; } iperf_args; static int eth_cmd_iperf(int argc, char **argv) { int nerrors = arg_parse(argc, argv, (void **)&iperf_args); iperf_cfg_t cfg; if (nerrors != 0) { arg_print_errors(stderr, iperf_args.end, argv[0]); return 0; } memset(&cfg, 0, sizeof(cfg)); // ethernet iperf only support IPV4 address cfg.type = IPERF_IP_TYPE_IPV4; /* iperf -a */ if (iperf_args.abort->count != 0) { iperf_stop(); return 0; } if (((iperf_args.ip->count == 0) && (iperf_args.server->count == 0)) || ((iperf_args.ip->count != 0) && (iperf_args.server->count != 0))) { ESP_LOGI(TAG,"Wrong mode! ESP32 should run in client or server mode"); return 0; } /* iperf -s */ if (iperf_args.ip->count == 0) { cfg.flag |= IPERF_FLAG_SERVER; } /* iperf -c SERVER_ADDRESS */ else { cfg.destination_ip4 = esp_ip4addr_aton(iperf_args.ip->sval[0]); cfg.flag |= IPERF_FLAG_CLIENT; } if (iperf_args.length->count == 0) { cfg.len_send_buf = 0; } else { cfg.len_send_buf = iperf_args.length->ival[0]; } /* acquiring for ip, could blocked here */ #if CONFIG_EXAMPLE_CONNECT_ETHERNET esp_netif_t *eth_netif = get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH); #elif CONFIG_EXAMPLE_CONNECT_WIFI esp_netif_t *eth_netif = get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA); #endif esp_netif_ip_info_t ip; if(eth_netif == NULL) { //ESP_LOGE(__func__, "Wrong mode! ESP32 should run in client or server mode"); ESP_LOGI(TAG,"not found any ethernet interface information\n"); return 0; } esp_netif_get_ip_info(eth_netif, &ip); cfg.source_ip4 = ip.ip.addr; if (cfg.source_ip4 == 0) { return 0; } /* iperf -u */ if (iperf_args.udp->count == 0) { cfg.flag |= IPERF_FLAG_TCP; } else { cfg.flag |= IPERF_FLAG_UDP; } /* iperf -p */ if (iperf_args.port->count == 0) { cfg.sport = IPERF_DEFAULT_PORT; cfg.dport = IPERF_DEFAULT_PORT; } else { if (cfg.flag & IPERF_FLAG_SERVER) { cfg.sport = iperf_args.port->ival[0]; cfg.dport = IPERF_DEFAULT_PORT; } else { cfg.sport = IPERF_DEFAULT_PORT; cfg.dport = iperf_args.port->ival[0]; } } /* iperf -i */ if (iperf_args.interval->count == 0) { cfg.interval = IPERF_DEFAULT_INTERVAL; } else { cfg.interval = iperf_args.interval->ival[0]; if (cfg.interval <= 0) { cfg.interval = IPERF_DEFAULT_INTERVAL; } } /* iperf -t */ if (iperf_args.time->count == 0) { cfg.time = IPERF_DEFAULT_TIME; } else { cfg.time = iperf_args.time->ival[0]; if (cfg.time <= cfg.interval) { cfg.time = cfg.interval; } } /* iperf -b */ if (iperf_args.bw_limit->count == 0) { cfg.bw_lim = IPERF_DEFAULT_NO_BW_LIMIT; } else { cfg.bw_lim = iperf_args.bw_limit->ival[0]; if (cfg.bw_lim <= 0) { cfg.bw_lim = IPERF_DEFAULT_NO_BW_LIMIT; } } ESP_LOGI(TAG,"mode=%s-%s sip=%ld.%ld.%ld.%ld:%d, dip=%ld.%ld.%ld.%ld:%d, interval=%ld, time=%ld", cfg.flag & IPERF_FLAG_TCP ? "tcp" : "udp", cfg.flag & IPERF_FLAG_SERVER ? "server" : "client", cfg.source_ip4 & 0xFF, (cfg.source_ip4 >> 8) & 0xFF, (cfg.source_ip4 >> 16) & 0xFF, (cfg.source_ip4 >> 24) & 0xFF, cfg.sport, cfg.destination_ip4 & 0xFF, (cfg.destination_ip4 >> 8) & 0xFF, (cfg.destination_ip4 >> 16) & 0xFF, (cfg.destination_ip4 >> 24) & 0xFF, cfg.dport, cfg.interval, cfg.time); iperf_start(&cfg); return 0; } void register_iperf_cmd(void){ iperf_args.ip = arg_str0("c", "client", "", "run in client mode, connecting to "); iperf_args.server = arg_lit0("s", "server", "run in server mode"); iperf_args.udp = arg_lit0("u", "udp", "use UDP rather than TCP"); iperf_args.version = arg_lit0("V", "ipv6_domain", "use IPV6 address rather than IPV4"); iperf_args.port = arg_int0("p", "port", "", "server port to listen on/connect to"); iperf_args.length = arg_int0("l", "len", "", "set read/write buffer size"); iperf_args.interval = arg_int0("i", "interval", "", "seconds between periodic bandwidth reports"); iperf_args.time = arg_int0("t", "time", "