blob: 87972dc22bff6ac3c95149d8c7ecbd874f847dc5 [file] [log] [blame]
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001/*
2 * Sigma Control API DUT (station/AP)
3 * Copyright (c) 2010, Atheros Communications, Inc.
Jouni Malinen9d7e31d2017-12-22 18:55:04 +02004 * Copyright (c) 2011-2013, 2016-2017 Qualcomm Atheros, Inc.
Jouni Malinena3042042019-02-19 13:01:13 +02005 * Copyright (c) 2018-2019, The Linux Foundation
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006 * All Rights Reserved.
7 * Licensed under the Clear BSD license. See README for more details.
8 */
9
10#include "sigma_dut.h"
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <signal.h>
15#include <ctype.h>
16
Pradeep Reddy POTTETIeeb2ccb2016-08-30 12:41:03 +053017#include "wpa_helpers.h"
18
Jouni Malinencd4e3c32015-10-29 12:39:56 +020019#ifdef ANDROID
20#define SHELL "/system/bin/sh"
21#else /* ANDROID */
22#define SHELL "/bin/sh"
23#endif /* ANDROID */
24
25
Jouni Malinena3042042019-02-19 13:01:13 +020026static enum sigma_cmd_result cmd_traffic_send_ping(struct sigma_dut *dut,
27 struct sigma_conn *conn,
28 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020029{
30 const char *dst, *val;
31 int size, dur, pkts;
32 int id;
33 char resp[100];
34 float interval;
35 double rate;
36 FILE *f;
37 char buf[100];
38 int type = 1;
39 int dscp = 0, use_dscp = 0;
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +020040 char extra[100], int_arg[100], intf_arg[100], ip_dst[100], ping[100];
Jouni Malinencd4e3c32015-10-29 12:39:56 +020041
42 val = get_param(cmd, "Type");
43 if (!val)
44 val = get_param(cmd, "IPType");
45 if (val)
46 type = atoi(val);
47 if (type != 1 && type != 2) {
48 send_resp(dut, conn, SIGMA_ERROR,
49 "ErrorCode,Unsupported address type");
Jouni Malinena3042042019-02-19 13:01:13 +020050 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020051 }
52
53 dst = get_param(cmd, "destination");
54 if (dst == NULL || (type == 1 && !is_ip_addr(dst)) ||
55 (type == 2 && !is_ipv6_addr(dst)))
Jouni Malinena3042042019-02-19 13:01:13 +020056 return INVALID_SEND_STATUS;
Peng Xu1165f172018-10-05 17:44:22 -070057 if (dut->ndp_enable && type == 2) {
58 snprintf(ip_dst, sizeof(ip_dst), "%s%%nan0", dst);
59 dst = ip_dst;
60 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +020061
62 val = get_param(cmd, "frameSize");
63 if (val == NULL)
Jouni Malinena3042042019-02-19 13:01:13 +020064 return INVALID_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020065 size = atoi(val);
66
67 val = get_param(cmd, "frameRate");
68 if (val == NULL)
Jouni Malinena3042042019-02-19 13:01:13 +020069 return INVALID_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020070 rate = atof(val);
71 if (rate <= 0)
Jouni Malinena3042042019-02-19 13:01:13 +020072 return INVALID_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020073
74 val = get_param(cmd, "duration");
75 if (val == NULL)
Jouni Malinena3042042019-02-19 13:01:13 +020076 return INVALID_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020077 dur = atoi(val);
Pradeep Reddy POTTETI95093ac2016-10-13 17:22:03 +053078 if (dur <= 0 || dur > 3600)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020079 dur = 3600;
80
81 pkts = dur * rate;
82 interval = (float) 1 / rate;
83 if (interval > 100000)
Jouni Malinena3042042019-02-19 13:01:13 +020084 return INVALID_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020085
86 val = get_param(cmd, "DSCP");
87 if (val) {
88 dscp = atoi(val);
89 if (dscp < 0 || dscp > 63) {
90 send_resp(dut, conn, SIGMA_ERROR,
91 "ErrorCode,Invalid DSCP value");
Jouni Malinena3042042019-02-19 13:01:13 +020092 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020093 }
94 use_dscp = 1;
95 }
96
97 id = dut->next_streamid++;
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +020098 snprintf(buf, sizeof(buf), "%s/sigma_dut-ping.%d",
99 dut->sigma_tmpdir, id);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200100 unlink(buf);
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200101 snprintf(buf, sizeof(buf), "%s/sigma_dut-ping-pid.%d",
102 dut->sigma_tmpdir, id);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200103 unlink(buf);
104
105 sigma_dut_print(dut, DUT_MSG_DEBUG, "Send ping: pkts=%d interval=%f "
106 "streamid=%d",
107 pkts, interval, id);
108
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200109 f = fopen(concat_sigma_tmpdir(dut, "/sigma_dut-ping.sh", ping,
110 sizeof(ping)), "w");
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200111 if (f == NULL)
Jouni Malinena3042042019-02-19 13:01:13 +0200112 return ERROR_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200113
114 extra[0] = '\0';
115 if (use_dscp) {
116 snprintf(extra, sizeof(extra), " -Q 0x%02x",
117 dscp << 2);
118 }
119
120 int_arg[0] = '\0';
121 if (rate != 1)
122 snprintf(int_arg, sizeof(int_arg), " -i %f", interval);
Peng Xu1165f172018-10-05 17:44:22 -0700123 if (!dut->ndp_enable && type == 2)
Purushottam Kushwahaa11683a2017-12-11 12:36:43 +0530124 snprintf(intf_arg, sizeof(intf_arg), " -I %s",
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200125 get_station_ifname(dut));
Peng Xu1165f172018-10-05 17:44:22 -0700126 else
127 intf_arg[0] = '\0';
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200128 fprintf(f, "#!" SHELL "\n"
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200129 "ping%s -c %d%s -s %d%s -q%s %s > %s"
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200130 "/sigma_dut-ping.%d &\n"
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200131 "echo $! > %s/sigma_dut-ping-pid.%d\n",
Pradeep Reddy POTTETIeeb2ccb2016-08-30 12:41:03 +0530132 type == 2 ? "6" : "", pkts, int_arg, size, extra,
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200133 intf_arg, dst, dut->sigma_tmpdir, id, dut->sigma_tmpdir, id);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200134
135 fclose(f);
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200136 if (chmod(concat_sigma_tmpdir(dut, "/sigma_dut-ping.sh", ping,
137 sizeof(ping)),
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200138 S_IRUSR | S_IWUSR | S_IXUSR) < 0)
Jouni Malinena3042042019-02-19 13:01:13 +0200139 return ERROR_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200140
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200141 if (system(concat_sigma_tmpdir(dut, "/sigma_dut-ping.sh", ping,
142 sizeof(ping))) != 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200143 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to start ping");
Jouni Malinena3042042019-02-19 13:01:13 +0200144 return ERROR_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200145 }
146
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200147 unlink(concat_sigma_tmpdir(dut, "/sigma_dut-ping.sh", ping,
148 sizeof(ping)));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200149
150 snprintf(resp, sizeof(resp), "streamID,%d", id);
151 send_resp(dut, conn, SIGMA_COMPLETE, resp);
Jouni Malinena3042042019-02-19 13:01:13 +0200152 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200153}
154
155
Jouni Malinena3042042019-02-19 13:01:13 +0200156static enum sigma_cmd_result cmd_traffic_stop_ping(struct sigma_dut *dut,
157 struct sigma_conn *conn,
158 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200159{
160 const char *val;
161 int id, pid;
162 FILE *f;
163 char buf[100];
164 int res_found = 0, sent = 0, received = 0;
165
166 val = get_param(cmd, "streamID");
167 if (val == NULL)
Jouni Malinena3042042019-02-19 13:01:13 +0200168 return INVALID_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200169 id = atoi(val);
170
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200171 snprintf(buf, sizeof(buf), "%s/sigma_dut-ping-pid.%d",
172 dut->sigma_tmpdir, id);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200173 f = fopen(buf, "r");
174 if (f == NULL) {
175 send_resp(dut, conn, SIGMA_ERROR,
176 "ErrorCode,Unknown streamID");
Jouni Malinena3042042019-02-19 13:01:13 +0200177 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200178 }
179 if (fscanf(f, "%d", &pid) != 1 || pid <= 0) {
180 sigma_dut_print(dut, DUT_MSG_ERROR, "No PID for ping process");
181 fclose(f);
182 unlink(buf);
Jouni Malinena3042042019-02-19 13:01:13 +0200183 return ERROR_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200184 }
185
186 fclose(f);
187 unlink(buf);
188
189 sigma_dut_print(dut, DUT_MSG_DEBUG, "Ping process pid %d", pid);
190 if (kill(pid, SIGINT) < 0 && errno != ESRCH) {
191 sigma_dut_print(dut, DUT_MSG_DEBUG, "kill failed: %s",
192 strerror(errno));
193 }
194 usleep(250000);
195
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200196 snprintf(buf, sizeof(buf), "%s/sigma_dut-ping.%d",
197 dut->sigma_tmpdir, id);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200198 f = fopen(buf, "r");
199 if (f == NULL) {
200 sigma_dut_print(dut, DUT_MSG_DEBUG,
201 "No ping result file found");
202 send_resp(dut, conn, SIGMA_COMPLETE, "sent,0,replies,0");
Jouni Malinena3042042019-02-19 13:01:13 +0200203 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200204 }
205
206 while (fgets(buf, sizeof(buf), f)) {
207 char *pos;
208
209 pos = strstr(buf, " packets transmitted");
210 if (pos) {
211 pos--;
212 while (pos > buf && isdigit(pos[-1]))
213 pos--;
214 sent = atoi(pos);
215 res_found = 1;
216 }
217
218 pos = strstr(buf, " packets received");
219 if (pos == NULL)
220 pos = strstr(buf, " received");
221 if (pos) {
222 pos--;
223 while (pos > buf && isdigit(pos[-1]))
224 pos--;
225 received = atoi(pos);
226 res_found = 1;
227 }
228 }
229 fclose(f);
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200230 snprintf(buf, sizeof(buf), "%s/sigma_dut-ping.%d",
231 dut->sigma_tmpdir, id);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200232 unlink(buf);
233
234 if (!res_found) {
235 sigma_dut_print(dut, DUT_MSG_DEBUG,
236 "No ping results found");
237 send_resp(dut, conn, SIGMA_COMPLETE, "sent,0,replies,0");
Jouni Malinena3042042019-02-19 13:01:13 +0200238 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200239 }
240
241 snprintf(buf, sizeof(buf), "sent,%d,replies,%d", sent, received);
242 send_resp(dut, conn, SIGMA_COMPLETE, buf);
Jouni Malinena3042042019-02-19 13:01:13 +0200243 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200244}
245
246
Jouni Malinena3042042019-02-19 13:01:13 +0200247static enum sigma_cmd_result cmd_traffic_start_iperf(struct sigma_dut *dut,
248 struct sigma_conn *conn,
249 struct sigma_cmd *cmd)
Peng Xu57784e02018-06-26 17:20:43 -0700250{
251 const char *val, *dst;
252 const char *iptype;
253 int port, duration;
254 const char *proto;
255 char buf[256];
256 const char *ifname;
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200257 char port_str[20], iperf[100];
Peng Xu57784e02018-06-26 17:20:43 -0700258 FILE *f;
259 int server, ipv6 = 0;
260
261 val = get_param(cmd, "mode");
262 if (!val) {
263 send_resp(dut, conn, SIGMA_ERROR,
264 "errorCode,Missing mode parameter");
Jouni Malinena3042042019-02-19 13:01:13 +0200265 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700266 }
267 server = strcasecmp(val, "server") == 0;
268
269 iptype = "";
270 val = get_param(cmd, "iptype");
271 if (val) {
272 if (strcasecmp(val, "ipv6") == 0) {
273 iptype = "-6";
274 ipv6 = 1;
275 } else {
276 iptype = "-4";
277 ipv6 = 0;
278 }
279 }
280
281 port_str[0] = '\0';
282 val = get_param(cmd, "port");
283 if (val) {
284 port = atoi(val);
285 snprintf(port_str, sizeof(port_str), "-p %d", port);
286 }
287
288 proto = "";
289 val = get_param(cmd, "transproto");
290 if (val && strcasecmp(val, "udp") == 0)
291 proto = "-u";
292
293 dst = get_param(cmd, "destination");
294 if (!server && (!dst || (!is_ip_addr(dst) && !is_ipv6_addr(dst)))) {
295 send_resp(dut, conn, SIGMA_ERROR,
296 "errorCode,Invalid destination address");
Jouni Malinena3042042019-02-19 13:01:13 +0200297 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700298 }
299
300 if (dut->ndpe)
301 ifname = "nan0";
302 else
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200303 ifname = get_station_ifname(dut);
Peng Xu57784e02018-06-26 17:20:43 -0700304
305 val = get_param(cmd, "duration");
306 if (val)
307 duration = atoi(val);
308 else
309 duration = 0;
310
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200311 unlink(concat_sigma_tmpdir(dut, "/sigma_dut-iperf", iperf,
312 sizeof(iperf)));
313 unlink(concat_sigma_tmpdir(dut, "/sigma_dut-iperf-pid", iperf,
314 sizeof(iperf)));
Peng Xu57784e02018-06-26 17:20:43 -0700315
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200316 f = fopen(concat_sigma_tmpdir(dut, "/sigma_dut-iperf.sh", iperf,
317 sizeof(iperf)), "w");
Peng Xu57784e02018-06-26 17:20:43 -0700318 if (!f) {
319 send_resp(dut, conn, SIGMA_ERROR,
320 "errorCode,Can not write sigma_dut-iperf.sh");
Jouni Malinena3042042019-02-19 13:01:13 +0200321 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700322 }
323
324 if (server) {
325 /* write server side command to shell file */
326 fprintf(f, "#!" SHELL "\n"
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200327 "iperf3 -s %s %s > %s"
Peng Xu57784e02018-06-26 17:20:43 -0700328 "/sigma_dut-iperf &\n"
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200329 "echo $! > %s/sigma_dut-iperf-pid\n",
330 port_str, iptype, dut->sigma_tmpdir, dut->sigma_tmpdir);
Peng Xu57784e02018-06-26 17:20:43 -0700331 } else {
332 /* write client side command to shell file */
333 if (!dst)
Jouni Malinena3042042019-02-19 13:01:13 +0200334 return INVALID_SEND_STATUS;
Peng Xu57784e02018-06-26 17:20:43 -0700335 if (ipv6)
336 snprintf(buf, sizeof(buf), "%s%%%s", dst, ifname);
337 else
338 snprintf(buf, sizeof(buf), "%s", dst);
339 fprintf(f, "#!" SHELL "\n"
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200340 "iperf3 -c %s -t %d %s %s %s > %s"
Peng Xu57784e02018-06-26 17:20:43 -0700341 "/sigma_dut-iperf &\n"
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200342 "echo $! > %s/sigma_dut-iperf-pid\n",
343 buf, duration, iptype, proto, port_str,
344 dut->sigma_tmpdir, dut->sigma_tmpdir);
Peng Xu57784e02018-06-26 17:20:43 -0700345 }
346
347 fclose(f);
348
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200349 if (chmod(concat_sigma_tmpdir(dut, "/sigma_dut-iperf.sh", iperf,
350 sizeof(iperf)),
Peng Xu57784e02018-06-26 17:20:43 -0700351 S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
352 send_resp(dut, conn, SIGMA_ERROR,
353 "errorCode,Can not chmod sigma_dut-iperf.sh");
Jouni Malinena3042042019-02-19 13:01:13 +0200354 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700355 }
356
357 sigma_dut_print(dut, DUT_MSG_DEBUG, "Starting iperf");
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200358 if (system(concat_sigma_tmpdir(dut, "/sigma_dut-iperf.sh", iperf,
359 sizeof(iperf))) != 0) {
Peng Xu57784e02018-06-26 17:20:43 -0700360 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to start iperf");
361 send_resp(dut, conn, SIGMA_ERROR,
362 "errorCode,Failed to run sigma_dut-iperf.sh");
Jouni Malinena3042042019-02-19 13:01:13 +0200363 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700364 }
365
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200366 unlink(concat_sigma_tmpdir(dut, "/sigma_dut-iperf.sh", iperf,
367 sizeof(iperf)));
Jouni Malinena3042042019-02-19 13:01:13 +0200368 return SUCCESS_SEND_STATUS;
Peng Xu57784e02018-06-26 17:20:43 -0700369}
370
371
Jouni Malinena3042042019-02-19 13:01:13 +0200372static enum sigma_cmd_result cmd_traffic_stop_iperf(struct sigma_dut *dut,
373 struct sigma_conn *conn,
374 struct sigma_cmd *cmd)
Peng Xu57784e02018-06-26 17:20:43 -0700375{
376 int pid;
377 FILE *f;
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200378 char buf[100], summary_buf[100], iperf[100];
Peng Xu57784e02018-06-26 17:20:43 -0700379 float bandwidth, totalbytes, factor;
380 char *pos;
381 long l_bandwidth, l_totalbytes;
382
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200383 f = fopen(concat_sigma_tmpdir(dut, "/sigma_dut-iperf-pid", iperf,
384 sizeof(iperf)), "r");
Peng Xu57784e02018-06-26 17:20:43 -0700385 if (!f) {
386 send_resp(dut, conn, SIGMA_ERROR,
387 "errorCode,PID file does not exist");
Jouni Malinena3042042019-02-19 13:01:13 +0200388 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700389 }
390 if (fscanf(f, "%d", &pid) != 1 || pid <= 0) {
391 sigma_dut_print(dut, DUT_MSG_ERROR, "No PID for iperf process");
392 fclose(f);
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200393 unlink(concat_sigma_tmpdir(dut, "/sigma_dut-iperf-pid", iperf,
394 sizeof(iperf)));
Jouni Malinena3042042019-02-19 13:01:13 +0200395 return ERROR_SEND_STATUS;
Peng Xu57784e02018-06-26 17:20:43 -0700396 }
397
398 fclose(f);
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200399 unlink(concat_sigma_tmpdir(dut, "/sigma_dut-iperf-pid", iperf,
400 sizeof(iperf)));
Peng Xu57784e02018-06-26 17:20:43 -0700401
402 if (kill(pid, SIGINT) < 0 && errno != ESRCH) {
403 sigma_dut_print(dut, DUT_MSG_DEBUG, "kill failed: %s",
404 strerror(errno));
405 }
406 usleep(250000);
407
408 /* parse iperf output which is stored in sigma_dut-iperf */
Peng Xu3886d5e2018-10-25 17:10:26 -0700409 summary_buf[0] = '\0';
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200410 f = fopen(concat_sigma_tmpdir(dut, "/sigma_dut-iperf", iperf,
411 sizeof(iperf)), "r");
Peng Xu57784e02018-06-26 17:20:43 -0700412 if (!f) {
413 sigma_dut_print(dut, DUT_MSG_DEBUG,
414 "No iperf result file found");
415 send_resp(dut, conn, SIGMA_COMPLETE,
416 "bandwidth,0,totalbytes,0");
Jouni Malinena3042042019-02-19 13:01:13 +0200417 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700418 }
419
420 /* find the last line which has the received bytes summary */
421 while (fgets(buf, sizeof(buf), f)) {
422 char *pos;
423
424 pos = strchr(buf, '\n');
425 if (pos)
426 *pos = '\0';
427 sigma_dut_print(dut, DUT_MSG_DEBUG, "iperf: %s", buf);
428 pos = strstr(buf, " sec ");
429 if (pos)
430 strlcpy(summary_buf, buf, sizeof(summary_buf));
431 }
432
433 fclose(f);
Alexei Avshalom Lazarc9bc15d2020-01-29 10:52:13 +0200434 unlink(concat_sigma_tmpdir(dut, "/sigma_dut-iperf", iperf,
435 sizeof(iperf)));
Peng Xu57784e02018-06-26 17:20:43 -0700436
437 pos = strstr(summary_buf, "Bytes");
438 if (!pos || pos == summary_buf) {
439 sigma_dut_print(dut, DUT_MSG_DEBUG,
440 "Can not parse iperf results");
441 send_resp(dut, conn, SIGMA_COMPLETE,
442 "bandwidth,0,totalbytes,0");
Jouni Malinena3042042019-02-19 13:01:13 +0200443 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700444 }
445
446 if (pos[-1] == 'G')
447 factor = 1024 * 1024 * 1024;
448 else if (pos[-1] == 'M')
449 factor = 1024 * 1024;
450 else if (pos[-1] == 'K')
451 factor = 1024;
452 else
453 factor = 1;
454
455 if (pos) {
456 pos -= 2;
457 while (pos > summary_buf && (pos[-1] != ' '))
458 pos--;
459 totalbytes = atof(pos);
460 } else
461 totalbytes = 0;
462 l_totalbytes = totalbytes * factor;
463
464 pos = strstr(summary_buf, "bits/sec");
465 if (!pos || pos == summary_buf) {
466 sigma_dut_print(dut, DUT_MSG_DEBUG,
467 "Can not parse iperf results");
468 send_resp(dut, conn, SIGMA_COMPLETE,
469 "bandwidth,0,totalbytes,0");
Jouni Malinena3042042019-02-19 13:01:13 +0200470 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700471 }
472
473 if (pos[-1] == 'G')
474 factor = 1024 * 1024 * 1024 / 8;
475 else if (pos[-1] == 'M')
476 factor = 1024 * 1024 / 8;
477 else if (pos[-1] == 'K')
478 factor = 1024 / 8;
479 else
480 factor = 1 / 8;
481
482 if (pos && pos - summary_buf > 2) {
483 pos -= 2;
484 while (pos > summary_buf && (pos[-1] != ' '))
485 pos--;
486 bandwidth = atof(pos);
487 } else
488 bandwidth = 0;
489 l_bandwidth = bandwidth * factor;
490
491 snprintf(buf, sizeof(buf), "bandwidth,%lu,totalbytes,%lu",
492 l_bandwidth, l_totalbytes);
493 send_resp(dut, conn, SIGMA_COMPLETE, buf);
Jouni Malinena3042042019-02-19 13:01:13 +0200494 return STATUS_SENT;
Peng Xu57784e02018-06-26 17:20:43 -0700495}
496
497
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200498void traffic_register_cmds(void)
499{
500 sigma_dut_reg_cmd("traffic_send_ping", NULL, cmd_traffic_send_ping);
501 sigma_dut_reg_cmd("traffic_stop_ping", NULL, cmd_traffic_stop_ping);
Peng Xu57784e02018-06-26 17:20:43 -0700502 sigma_dut_reg_cmd("traffic_start_iperf", NULL, cmd_traffic_start_iperf);
503 sigma_dut_reg_cmd("traffic_stop_iperf", NULL, cmd_traffic_stop_iperf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200504}