iperf_cli.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <string.h>
  2. #include <stdint.h>
  3. #include <utils_getopt.h>
  4. #include <iperf.h>
  5. #include <lwip/ip_addr.h>
  6. #define NL "\r\n"
  7. static void iperf_cmd(int argc, char **argv)
  8. {
  9. int opt;
  10. getopt_env_t opt_env;
  11. int o_c = 0, o_s = 0, o_u = 0, o_a = 0;
  12. int o_p = IPERF_DEFAULT_PORT, o_l = 0, o_i = IPERF_DEFAULT_INTERVAL, o_t = IPERF_DEFAULT_TIME, o_b = IPERF_DEFAULT_NO_BW_LIMIT, o_S = 0, o_n = 0;
  13. int o_d = 0;
  14. int o_P = IPERF_TRAFFIC_TASK_PRIORITY;
  15. uint32_t dst_addr = 0;
  16. iperf_cfg_t cfg;
  17. utils_getopt_init(&opt_env, 0);
  18. while ((opt = utils_getopt(&opt_env, argc, argv, ":c:sup:l:i:t:b:S:n:P:ad")) != -1) {
  19. #define ARG_READ(v) v = atoi(opt_env.optarg)
  20. switch (opt) {
  21. case 'c':
  22. ++o_c;
  23. dst_addr = ipaddr_addr(opt_env.optarg);
  24. break;
  25. case 's': ++o_s; break;
  26. case 'u': ++o_u; break;
  27. case 'p': ARG_READ(o_p); break;
  28. case 'l': ARG_READ(o_l); break;
  29. case 'i': ARG_READ(o_i); break;
  30. case 't': ARG_READ(o_t); break;
  31. case 'b': ARG_READ(o_b); break;
  32. case 'S': ARG_READ(o_S); break;
  33. case 'n': ARG_READ(o_n); break;
  34. case 'P': ARG_READ(o_P); break;
  35. case 'd': ++o_d; break;
  36. case 'a': ++o_a; break;
  37. }
  38. #undef ARG_READ
  39. }
  40. memset(&cfg, 0, sizeof(cfg));
  41. cfg.type = IPERF_IP_TYPE_IPV4;
  42. if (o_a) {
  43. iperf_stop();
  44. return;
  45. }
  46. if (!((o_c && !o_s) || (!o_c && o_s))) {
  47. printf("client/server required" NL);
  48. return;
  49. }
  50. if (o_c) {
  51. cfg.destination_ip4 = dst_addr;
  52. cfg.flag |= IPERF_FLAG_CLIENT;
  53. } else {
  54. cfg.flag |= IPERF_FLAG_SERVER;
  55. }
  56. if (o_u) {
  57. cfg.flag |= IPERF_FLAG_UDP;
  58. } else {
  59. cfg.flag |= IPERF_FLAG_TCP;
  60. }
  61. if (o_c && !o_u && o_d) {
  62. cfg.flag |= IPERF_FLAG_DUAL;
  63. }
  64. cfg.len_buf = o_l;
  65. cfg.sport = o_p;
  66. cfg.dport = o_p;
  67. cfg.interval = o_i;
  68. cfg.time = o_t;
  69. if (cfg.time < cfg.interval) {
  70. cfg.time = cfg.interval;
  71. }
  72. cfg.bw_lim = o_b;
  73. cfg.tos = o_S;
  74. cfg.num_bytes = o_n * 1000 * 1000;
  75. if (cfg.bw_lim <= 0) {
  76. cfg.bw_lim = IPERF_DEFAULT_NO_BW_LIMIT;
  77. }
  78. cfg.traffic_task_priority = o_P;
  79. iperf_start(&cfg);
  80. }
  81. #include <shell.h>
  82. #define ML(s) s NL
  83. #define IPERF_USAGE \
  84. ML("iperf") \
  85. ML(" -c server_addr: run in client mode") \
  86. ML(" -s: run in server mode") \
  87. ML(" -u: UDP") \
  88. ML(" -p port: specify port") \
  89. ML(" -l length: set read/write buffer size") \
  90. ML(" -i interval: seconds between bandwidth reports") \
  91. ML(" -t time: time in seconds to run") \
  92. ML(" -b bandwith: bandwidth to send in Mbps") \
  93. ML(" -S tos: TOS") \
  94. ML(" -n MB: number of MB to send/recv") \
  95. ML(" -P priority: traffic task priority") \
  96. ML(" -d: dual mode") \
  97. ML(" -a: abort running iperf") \
  98. #if 0
  99. const static struct cli_command iperf_cmds[] STATIC_CLI_CMD_ATTRIBUTE = {
  100. {"iperf", IPERF_USAGE, iperf_cmd},
  101. };
  102. #endif
  103. CSH_CMD_EXPORT_ALIAS(iperf_cmd, iperf, iperf command);