2020-12-04 09:16:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/netutils/iperf/iperf.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2021-06-17 06:37:00 +02:00
|
|
|
#include <assert.h>
|
2023-03-06 22:17:52 +01:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
2021-06-17 06:37:00 +02:00
|
|
|
#include <stdbool.h>
|
2023-03-06 22:17:52 +01:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
2023-04-07 10:46:19 +02:00
|
|
|
#include <sys/un.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-12-04 09:16:12 +01:00
|
|
|
#include "iperf.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define IPERF_TRAFFIC_TASK_NAME "iperf_traffic"
|
|
|
|
#define IPERF_TRAFFIC_TASK_PRIORITY 100
|
|
|
|
#define IPERF_TRAFFIC_TASK_STACK 4096
|
|
|
|
#define IPERF_REPORT_TASK_NAME "iperf_report"
|
|
|
|
#define IPERF_REPORT_TASK_PRIORITY 100
|
|
|
|
#define IPERF_REPORT_TASK_STACK 4096
|
|
|
|
|
|
|
|
#define IPERF_UDP_TX_LEN (1472)
|
|
|
|
#define IPERF_UDP_RX_LEN (16 << 10)
|
|
|
|
#define IPERF_TCP_TX_LEN (16 << 10)
|
|
|
|
#define IPERF_TCP_RX_LEN (16 << 10)
|
|
|
|
|
|
|
|
#define IPERF_MAX_DELAY 64
|
|
|
|
#define IPERF_SOCKET_RX_TIMEOUT 10
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct iperf_ctrl_t
|
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR struct iperf_ctrl_t *flink;
|
2020-12-04 09:16:12 +01:00
|
|
|
struct iperf_cfg_t cfg;
|
|
|
|
bool finish;
|
2021-06-10 06:23:14 +02:00
|
|
|
uintmax_t total_len;
|
2020-12-04 09:16:12 +01:00
|
|
|
uint32_t buffer_len;
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR uint8_t *buffer;
|
2020-12-04 09:16:12 +01:00
|
|
|
uint32_t sockfd;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct iperf_udp_pkt_t
|
|
|
|
{
|
|
|
|
int32_t id;
|
|
|
|
uint32_t sec;
|
|
|
|
uint32_t usec;
|
|
|
|
};
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
typedef CODE int (*iperf_client_func_t)(FAR struct iperf_ctrl_t *ctrl,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
socklen_t addrlen);
|
|
|
|
typedef CODE int (*iperf_server_func_t)(FAR struct iperf_ctrl_t *ctrl,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
socklen_t addrlen,
|
|
|
|
FAR struct sockaddr *remote_addr);
|
|
|
|
|
2020-12-04 09:16:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
static pthread_mutex_t g_iperf_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static sq_queue_t g_iperf_ctrl_list;
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
inline static bool iperf_is_udp_client(FAR struct iperf_ctrl_t *ctrl);
|
|
|
|
inline static bool iperf_is_udp_server(FAR struct iperf_ctrl_t *ctrl);
|
|
|
|
inline static bool iperf_is_tcp_client(FAR struct iperf_ctrl_t *ctrl);
|
|
|
|
inline static bool iperf_is_tcp_server(FAR struct iperf_ctrl_t *ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
static int iperf_get_socket_error_code(int sockfd);
|
2022-11-20 11:23:45 +01:00
|
|
|
static int iperf_show_socket_error_reason(FAR const char *str, int sockfd);
|
|
|
|
static void iperf_report_task(FAR void *arg);
|
|
|
|
static int iperf_start_report(FAR struct iperf_ctrl_t *ctrl);
|
|
|
|
static int iperf_run_tcp_server(FAR struct iperf_ctrl_t *ctrl);
|
|
|
|
static int iperf_run_udp_server(FAR struct iperf_ctrl_t *ctrl);
|
|
|
|
static int iperf_run_udp_client(FAR struct iperf_ctrl_t *ctrl);
|
|
|
|
static int iperf_run_tcp_client(FAR struct iperf_ctrl_t *ctrl);
|
|
|
|
static void iperf_task_traffic(FAR void *arg);
|
|
|
|
static uint32_t iperf_get_buffer_len(FAR struct iperf_ctrl_t *ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_is_udp_client
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if it is a udp client
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
inline static bool iperf_is_udp_client(FAR struct iperf_ctrl_t *ctrl)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
return ((ctrl->cfg.flag & IPERF_FLAG_CLIENT)
|
|
|
|
&& (ctrl->cfg.flag & IPERF_FLAG_UDP));
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_is_udp_server
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if it is a udp server
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
inline static bool iperf_is_udp_server(FAR struct iperf_ctrl_t *ctrl)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
return ((ctrl->cfg.flag & IPERF_FLAG_SERVER)
|
|
|
|
&& (ctrl->cfg.flag & IPERF_FLAG_UDP));
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_is_tcp_client
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if it is a tcp client
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
inline static bool iperf_is_tcp_client(FAR struct iperf_ctrl_t *ctrl)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
return ((ctrl->cfg.flag & IPERF_FLAG_CLIENT)
|
|
|
|
&& (ctrl->cfg.flag & IPERF_FLAG_TCP));
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_is_tcp_server
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if it is a tcp server
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
inline static bool iperf_is_tcp_server(FAR struct iperf_ctrl_t *ctrl)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
return ((ctrl->cfg.flag & IPERF_FLAG_SERVER)
|
|
|
|
&& (ctrl->cfg.flag & IPERF_FLAG_TCP));
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_get_socket_error_code
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get reason of socket error.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iperf_get_socket_error_code(int sockfd)
|
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_show_socket_error_reason
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Show reason of socket error.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
static int iperf_show_socket_error_reason(FAR const char *str, int sockfd)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
int err = errno;
|
|
|
|
if (err != 0)
|
|
|
|
{
|
2022-11-07 03:02:42 +01:00
|
|
|
printf("%s error, error code: %d, reason: %s\n",
|
2020-12-04 09:16:12 +01:00
|
|
|
str, err, strerror(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_print_addr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print addr info
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void iperf_print_addr(FAR const char *str, FAR struct sockaddr *addr)
|
|
|
|
{
|
|
|
|
switch (addr->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in *inaddr = (FAR struct sockaddr_in *)addr;
|
|
|
|
printf("%s: %s:%d\n", str,
|
|
|
|
inet_ntoa(inaddr->sin_addr), htons(inaddr->sin_port));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-07 10:46:19 +02:00
|
|
|
case AF_LOCAL:
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)addr;
|
|
|
|
printf("%s: path=%s\n", str, unaddr->sun_path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
default:
|
|
|
|
assert(false); /* shouldn't happen */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 06:23:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ts_sec
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a timespec to a double.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static double ts_sec(const struct timespec *ts)
|
|
|
|
{
|
|
|
|
return (double)ts->tv_sec + (double)ts->tv_nsec / 1e9;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ts_diff
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the diff of two timespecs in second.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static double ts_diff(const struct timespec *a, const struct timespec *b)
|
|
|
|
{
|
|
|
|
return ts_sec(a) - ts_sec(b);
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:16:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_report_task
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start iperf report task
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
static void iperf_report_task(FAR void *arg)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR struct iperf_ctrl_t *ctrl = arg;
|
|
|
|
uint32_t interval = ctrl->cfg.interval;
|
|
|
|
uint32_t time = ctrl->cfg.time;
|
2021-06-10 06:23:14 +02:00
|
|
|
struct timespec now;
|
|
|
|
struct timespec start;
|
|
|
|
uintmax_t now_len;
|
|
|
|
int ret;
|
|
|
|
|
2022-11-20 10:35:58 +01:00
|
|
|
prctl(PR_SET_NAME, IPERF_REPORT_TASK_NAME);
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
now_len = ctrl->total_len;
|
2022-02-15 09:16:54 +01:00
|
|
|
ret = clock_gettime(CLOCK_MONOTONIC, &now);
|
2021-06-10 06:23:14 +02:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "clock_gettime failed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-12-04 09:16:12 +01:00
|
|
|
|
2021-06-10 06:23:14 +02:00
|
|
|
start = now;
|
2021-06-10 06:28:51 +02:00
|
|
|
printf("\n%19s %16s %18s\n", "Interval", "Transfer", "Bandwidth\n");
|
2022-11-20 11:23:45 +01:00
|
|
|
while (!ctrl->finish)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2021-06-10 06:23:14 +02:00
|
|
|
uintmax_t last_len;
|
|
|
|
struct timespec last;
|
|
|
|
|
2020-12-04 09:16:12 +01:00
|
|
|
sleep(interval);
|
2021-06-10 06:23:14 +02:00
|
|
|
last_len = now_len;
|
|
|
|
last = now;
|
2022-11-20 11:23:45 +01:00
|
|
|
now_len = ctrl->total_len;
|
2022-02-15 09:16:54 +01:00
|
|
|
ret = clock_gettime(CLOCK_MONOTONIC, &now);
|
2021-06-10 06:23:14 +02:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "clock_gettime failed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2021-06-10 06:28:51 +02:00
|
|
|
printf("%7.2lf-%7.2lf sec %10ju Bytes %7.2f Mbits/sec\n",
|
2021-06-10 06:23:14 +02:00
|
|
|
ts_diff(&last, &start),
|
|
|
|
ts_diff(&now, &start),
|
2022-12-12 06:30:40 +01:00
|
|
|
now_len -last_len,
|
2023-03-22 08:05:58 +01:00
|
|
|
((double)((now_len - last_len) * 8) / 1000000) /
|
2022-12-12 06:30:40 +01:00
|
|
|
(double)ts_diff(&now, &last)
|
|
|
|
);
|
2021-06-10 06:34:46 +02:00
|
|
|
if (time != 0 && ts_diff(&now, &start) >= time)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 06:23:14 +02:00
|
|
|
if (ts_diff(&now, &start) > 0)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2021-06-10 06:28:51 +02:00
|
|
|
printf("%7.2lf-%7.2lf sec %10ju Bytes %7.2f Mbits/sec\n",
|
2021-06-10 06:23:14 +02:00
|
|
|
ts_diff(&start, &start),
|
|
|
|
ts_diff(&now, &start),
|
2021-06-10 06:28:51 +02:00
|
|
|
now_len,
|
2023-03-22 08:05:58 +01:00
|
|
|
((double)(now_len * 8) / 1000000) /
|
2022-12-12 06:30:40 +01:00
|
|
|
(double)ts_diff(&now, &start)
|
|
|
|
);
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->finish = true;
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_start_report
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start iperf report
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
static int iperf_start_report(FAR struct iperf_ctrl_t *ctrl)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
struct sched_param param;
|
|
|
|
pthread_attr_t attr;
|
2022-11-20 11:23:45 +01:00
|
|
|
pthread_t thread;
|
|
|
|
int ret;
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
param.sched_priority = IPERF_REPORT_TASK_PRIORITY;
|
|
|
|
pthread_attr_setschedparam(&attr, ¶m);
|
|
|
|
pthread_attr_setstacksize(&attr, IPERF_REPORT_TASK_STACK);
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
ret = pthread_create(&thread, &attr, (FAR void *)iperf_report_task,
|
|
|
|
ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("iperf_thread: pthread_create failed: %d, %s\n",
|
|
|
|
ret, IPERF_REPORT_TASK_NAME);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-03-16 07:01:11 +01:00
|
|
|
pthread_detach(thread);
|
|
|
|
|
2020-12-04 09:16:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-04-06 12:01:14 +02:00
|
|
|
* Name: iperf_run_server
|
2020-12-04 09:16:12 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-04-06 12:01:14 +02:00
|
|
|
* Start a server
|
2020-12-04 09:16:12 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
static int iperf_run_server(FAR struct iperf_ctrl_t *ctrl,
|
|
|
|
iperf_server_func_t server_func)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2023-04-07 10:46:19 +02:00
|
|
|
if (ctrl->cfg.flag & IPERF_FLAG_LOCAL)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
struct sockaddr_un remote_addr;
|
|
|
|
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
strlcpy(addr.sun_path, ctrl->cfg.path, sizeof(addr.sun_path));
|
|
|
|
|
|
|
|
return server_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr),
|
|
|
|
(FAR struct sockaddr *)&remote_addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct sockaddr_in remote_addr;
|
2023-04-06 12:01:14 +02:00
|
|
|
|
2023-04-07 10:46:19 +02:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(ctrl->cfg.sport);
|
|
|
|
addr.sin_addr.s_addr = ctrl->cfg.sip;
|
2023-04-06 12:01:14 +02:00
|
|
|
|
2023-04-07 10:46:19 +02:00
|
|
|
return server_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr),
|
|
|
|
(FAR struct sockaddr *)&remote_addr);
|
|
|
|
}
|
2023-04-06 12:01:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_run_client
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start a client
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iperf_run_client(FAR struct iperf_ctrl_t *ctrl,
|
|
|
|
iperf_client_func_t client_func)
|
|
|
|
{
|
2023-04-07 10:46:19 +02:00
|
|
|
if (ctrl->cfg.flag & IPERF_FLAG_LOCAL)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
strlcpy(addr.sun_path, ctrl->cfg.path, sizeof(addr.sun_path));
|
2023-04-06 12:01:14 +02:00
|
|
|
|
2023-04-07 10:46:19 +02:00
|
|
|
return client_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
2023-04-06 12:01:14 +02:00
|
|
|
|
2023-04-07 10:46:19 +02:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(ctrl->cfg.dport);
|
|
|
|
addr.sin_addr.s_addr = ctrl->cfg.dip;
|
|
|
|
|
|
|
|
return client_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
}
|
2023-04-06 12:01:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_tcp_server
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The main tcp server logic
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iperf_tcp_server(FAR struct iperf_ctrl_t *ctrl,
|
|
|
|
FAR struct sockaddr *addr, socklen_t addrlen,
|
|
|
|
FAR struct sockaddr *remote_addr)
|
|
|
|
{
|
2020-12-04 09:16:12 +01:00
|
|
|
int actual_recv = 0;
|
|
|
|
int want_recv = 0;
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR uint8_t *buffer;
|
2020-12-04 09:16:12 +01:00
|
|
|
int listen_socket;
|
|
|
|
struct timeval t;
|
|
|
|
int sockfd;
|
|
|
|
int opt;
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
listen_socket = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
|
2020-12-04 09:16:12 +01:00
|
|
|
if (listen_socket < 0)
|
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("tcp server create", listen_socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
2023-04-06 12:01:14 +02:00
|
|
|
if (bind(listen_socket, addr, addrlen) != 0)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("tcp server bind", listen_socket);
|
|
|
|
close(listen_socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(listen_socket, 5) < 0)
|
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("tcp server listen", listen_socket);
|
|
|
|
close(listen_socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
buffer = ctrl->buffer;
|
|
|
|
want_recv = ctrl->buffer_len;
|
|
|
|
while (!ctrl->finish)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
/* TODO need to change to non-block mode */
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
sockfd = accept(listen_socket, remote_addr, &addrlen);
|
2020-12-04 09:16:12 +01:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("tcp server listen", listen_socket);
|
|
|
|
close(listen_socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-06 12:01:14 +02:00
|
|
|
iperf_print_addr("accept", remote_addr);
|
2022-11-20 11:23:45 +01:00
|
|
|
iperf_start_report(ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
t.tv_sec = IPERF_SOCKET_RX_TIMEOUT;
|
2022-02-28 14:22:25 +01:00
|
|
|
t.tv_usec = 0;
|
2020-12-04 09:16:12 +01:00
|
|
|
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t));
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
while (!ctrl->finish)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
actual_recv = recv(sockfd, buffer, want_recv, 0);
|
2021-06-04 06:43:01 +02:00
|
|
|
if (actual_recv == 0)
|
|
|
|
{
|
2023-04-06 12:01:14 +02:00
|
|
|
iperf_print_addr("closed by the peer", remote_addr);
|
2021-06-04 06:43:01 +02:00
|
|
|
|
|
|
|
/* Note: unlike the original iperf, this implementation
|
|
|
|
* exits after finishing a single connection.
|
|
|
|
*/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->finish = true;
|
2021-06-04 06:43:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (actual_recv < 0)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("tcp server recv",
|
|
|
|
listen_socket);
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->finish = true;
|
2020-12-04 09:16:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->total_len += actual_recv;
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->finish = true;
|
2020-12-04 09:16:12 +01:00
|
|
|
close(listen_socket);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-04-06 12:01:14 +02:00
|
|
|
* Name: iperf_run_tcp_server
|
2020-12-04 09:16:12 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-04-06 12:01:14 +02:00
|
|
|
* Start tcp server
|
2020-12-04 09:16:12 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
static int iperf_run_tcp_server(FAR struct iperf_ctrl_t *ctrl)
|
|
|
|
{
|
|
|
|
return iperf_run_server(ctrl, iperf_tcp_server);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_udp_server
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The main udp server logic
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iperf_udp_server(FAR struct iperf_ctrl_t *ctrl,
|
|
|
|
FAR struct sockaddr *addr, socklen_t addrlen,
|
|
|
|
FAR struct sockaddr *remote_addr)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
int actual_recv = 0;
|
|
|
|
struct timeval t;
|
|
|
|
int want_recv = 0;
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR uint8_t *buffer;
|
2020-12-04 09:16:12 +01:00
|
|
|
int sockfd;
|
|
|
|
int opt;
|
|
|
|
bool udp_recv_start = true;
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
sockfd = socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
|
2020-12-04 09:16:12 +01:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("udp server create", sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
if (bind(sockfd, addr, addrlen) != 0)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("udp server bind", sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
buffer = ctrl->buffer;
|
|
|
|
want_recv = ctrl->buffer_len;
|
2022-11-07 03:02:42 +01:00
|
|
|
printf("want recv=%d\n", want_recv);
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
t.tv_sec = IPERF_SOCKET_RX_TIMEOUT;
|
2022-02-28 14:22:25 +01:00
|
|
|
t.tv_usec = 0;
|
2020-12-04 09:16:12 +01:00
|
|
|
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t));
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
while (!ctrl->finish)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
actual_recv = recvfrom(sockfd, buffer, want_recv, 0,
|
2023-04-06 12:01:14 +02:00
|
|
|
remote_addr, &addrlen);
|
2020-12-04 09:16:12 +01:00
|
|
|
if (actual_recv < 0)
|
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("udp server recv", sockfd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (udp_recv_start == true)
|
|
|
|
{
|
2023-04-06 12:01:14 +02:00
|
|
|
iperf_print_addr("accept", remote_addr);
|
2022-11-20 11:23:45 +01:00
|
|
|
iperf_start_report(ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
udp_recv_start = false;
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->total_len += actual_recv;
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->finish = true;
|
2020-12-04 09:16:12 +01:00
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-04-06 12:01:14 +02:00
|
|
|
* Name: iperf_run_udp_server
|
2020-12-04 09:16:12 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-04-06 12:01:14 +02:00
|
|
|
* Start udp server
|
2020-12-04 09:16:12 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
static int iperf_run_udp_server(FAR struct iperf_ctrl_t *ctrl)
|
|
|
|
{
|
|
|
|
return iperf_run_server(ctrl, iperf_udp_server);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_udp_client
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The main udp client logic
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iperf_udp_client(FAR struct iperf_ctrl_t *ctrl,
|
|
|
|
FAR struct sockaddr *addr, socklen_t addrlen)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR struct iperf_udp_pkt_t *udp;
|
2020-12-04 09:16:12 +01:00
|
|
|
int actual_send = 0;
|
|
|
|
bool retry = false;
|
|
|
|
uint32_t delay = 1;
|
|
|
|
int want_send = 0;
|
|
|
|
uint8_t *buffer;
|
|
|
|
int sockfd;
|
|
|
|
int opt;
|
|
|
|
int err;
|
|
|
|
int id;
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
sockfd = socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
|
2020-12-04 09:16:12 +01:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("udp client create", sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
iperf_start_report(ctrl);
|
|
|
|
buffer = ctrl->buffer;
|
|
|
|
udp = (FAR struct iperf_udp_pkt_t *)buffer;
|
|
|
|
want_send = ctrl->buffer_len;
|
2020-12-04 09:16:12 +01:00
|
|
|
id = 0;
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
while (!ctrl->finish)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
if (false == retry)
|
|
|
|
{
|
|
|
|
id++;
|
|
|
|
udp->id = htonl(id);
|
|
|
|
delay = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
retry = false;
|
2023-04-06 12:01:14 +02:00
|
|
|
actual_send = sendto(sockfd, buffer, want_send, 0, addr, addrlen);
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
if (actual_send != want_send)
|
|
|
|
{
|
|
|
|
err = iperf_get_socket_error_code(sockfd);
|
|
|
|
if (err == ENOMEM)
|
|
|
|
{
|
|
|
|
usleep(delay * 10000);
|
|
|
|
if (delay < IPERF_MAX_DELAY)
|
|
|
|
{
|
|
|
|
delay <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
retry = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-07 03:02:42 +01:00
|
|
|
printf("udp client send abort: err=%d\n", err);
|
2020-12-04 09:16:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->total_len += actual_send;
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->finish = true;
|
2020-12-04 09:16:12 +01:00
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-04-06 12:01:14 +02:00
|
|
|
* Name: iperf_run_udp_client
|
2020-12-04 09:16:12 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-04-06 12:01:14 +02:00
|
|
|
* Start udp client
|
2020-12-04 09:16:12 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
static int iperf_run_udp_client(FAR struct iperf_ctrl_t *ctrl)
|
|
|
|
{
|
|
|
|
return iperf_run_client(ctrl, iperf_udp_client);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_tcp_client
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The main tcp client logic
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iperf_tcp_client(FAR struct iperf_ctrl_t *ctrl,
|
|
|
|
FAR struct sockaddr *addr, socklen_t addrlen)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR uint8_t *buffer;
|
2020-12-04 09:16:12 +01:00
|
|
|
int actual_send = 0;
|
|
|
|
int want_send = 0;
|
|
|
|
int sockfd;
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
sockfd = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
|
2020-12-04 09:16:12 +01:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("tcp client create", sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
if (connect(sockfd, addr, addrlen) < 0)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("tcp client connect", sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
iperf_start_report(ctrl);
|
|
|
|
buffer = ctrl->buffer;
|
|
|
|
want_send = ctrl->buffer_len;
|
2020-12-04 09:16:12 +01:00
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
while (!ctrl->finish)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
actual_send = send(sockfd, buffer, want_send, 0);
|
|
|
|
if (actual_send <= 0)
|
|
|
|
{
|
|
|
|
iperf_show_socket_error_reason("tcp client send", sockfd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->total_len += actual_send;
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl->finish = true;
|
2020-12-04 09:16:12 +01:00
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:01:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_run_tcp_client
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start tcp client
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iperf_run_tcp_client(FAR struct iperf_ctrl_t *ctrl)
|
|
|
|
{
|
|
|
|
return iperf_run_client(ctrl, iperf_tcp_client);
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:16:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_task_traffic
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Select to run tcp or udp.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
static void iperf_task_traffic(FAR void *arg)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR struct iperf_ctrl_t *ctrl = arg;
|
|
|
|
|
2022-11-20 10:35:58 +01:00
|
|
|
prctl(PR_SET_NAME, IPERF_TRAFFIC_TASK_NAME);
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
if (iperf_is_udp_client(ctrl))
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
iperf_run_udp_client(ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
2022-11-20 11:23:45 +01:00
|
|
|
else if (iperf_is_udp_server(ctrl))
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
iperf_run_udp_server(ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
2022-11-20 11:23:45 +01:00
|
|
|
else if (iperf_is_tcp_client(ctrl))
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
iperf_run_tcp_client(ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
2022-11-20 11:23:45 +01:00
|
|
|
else if (iperf_is_tcp_server(ctrl))
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
iperf_run_tcp_server(ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
2021-06-17 06:37:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* shouldn't happen */
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
}
|
2020-12-04 09:16:12 +01:00
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
if (ctrl->buffer)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
free(ctrl->buffer);
|
|
|
|
ctrl->buffer = NULL;
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-07 03:02:42 +01:00
|
|
|
printf("iperf exit\n");
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
static uint32_t iperf_get_buffer_len(FAR struct iperf_ctrl_t *ctrl)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
if (iperf_is_udp_client(ctrl))
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
return IPERF_UDP_TX_LEN;
|
|
|
|
}
|
2022-11-20 11:23:45 +01:00
|
|
|
else if (iperf_is_udp_server(ctrl))
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
return IPERF_UDP_RX_LEN;
|
|
|
|
}
|
2022-11-20 11:23:45 +01:00
|
|
|
else if (iperf_is_tcp_client(ctrl))
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
return IPERF_TCP_TX_LEN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return IPERF_TCP_RX_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_start
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start iperf task.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
int iperf_start(FAR struct iperf_cfg_t *cfg)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
struct iperf_ctrl_t ctrl;
|
2020-12-04 09:16:12 +01:00
|
|
|
struct sched_param param;
|
|
|
|
pthread_attr_t attr;
|
2022-11-20 11:23:45 +01:00
|
|
|
pthread_t thread;
|
|
|
|
FAR void *retval;
|
|
|
|
int ret;
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
if (!cfg)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
memset(&ctrl, 0, sizeof(ctrl));
|
|
|
|
memcpy(&ctrl.cfg, cfg, sizeof(*cfg));
|
|
|
|
ctrl.finish = false;
|
|
|
|
ctrl.buffer_len = iperf_get_buffer_len(&ctrl);
|
|
|
|
ctrl.buffer = (FAR uint8_t *)malloc(ctrl.buffer_len);
|
|
|
|
if (ctrl.buffer == NULL)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
|
|
|
printf("create buffer: not enough memory\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
memset(ctrl.buffer, 0, ctrl.buffer_len);
|
2020-12-04 09:16:12 +01:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
param.sched_priority = IPERF_TRAFFIC_TASK_PRIORITY;
|
|
|
|
pthread_attr_setschedparam(&attr, ¶m);
|
|
|
|
pthread_attr_setstacksize(&attr, IPERF_TRAFFIC_TASK_STACK);
|
2022-11-20 11:23:45 +01:00
|
|
|
ret = pthread_create(&thread, &attr, (FAR void *)iperf_task_traffic,
|
|
|
|
&ctrl);
|
2020-12-04 09:16:12 +01:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("iperf_task_traffic: create task failed: %d\n", ret);
|
2022-11-20 11:23:45 +01:00
|
|
|
free(ctrl.buffer);
|
|
|
|
ctrl.buffer = NULL;
|
2020-12-04 09:16:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
pthread_mutex_lock(&g_iperf_ctrl_mutex);
|
|
|
|
sq_addlast((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list);
|
|
|
|
pthread_mutex_unlock(&g_iperf_ctrl_mutex);
|
|
|
|
|
2021-02-02 01:06:02 +01:00
|
|
|
pthread_join(thread, &retval);
|
2022-11-20 11:23:45 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&g_iperf_ctrl_mutex);
|
|
|
|
sq_rem((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list);
|
|
|
|
pthread_mutex_unlock(&g_iperf_ctrl_mutex);
|
|
|
|
|
2020-12-04 09:16:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iperf_stop
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stop iperf task.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int iperf_stop(void)
|
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
FAR struct iperf_ctrl_t *ctrl;
|
|
|
|
FAR sq_entry_t *tmp;
|
|
|
|
FAR sq_entry_t *p;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_iperf_ctrl_mutex);
|
2020-12-04 09:16:12 +01:00
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
sq_for_every_safe(&g_iperf_ctrl_list, p, tmp)
|
2020-12-04 09:16:12 +01:00
|
|
|
{
|
2022-11-20 11:23:45 +01:00
|
|
|
ctrl = (FAR struct iperf_ctrl_t *)p;
|
|
|
|
ctrl->finish = true;
|
|
|
|
sq_rem(p, &g_iperf_ctrl_list);
|
2020-12-04 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 11:23:45 +01:00
|
|
|
pthread_mutex_unlock(&g_iperf_ctrl_mutex);
|
|
|
|
|
2020-12-04 09:16:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|