23/07/11 Fix Lwip TCP Rx Speed.

This commit is contained in:
涂煜洋
2023-07-11 17:04:40 +08:00
parent 42e3b261ab
commit 1086321a63
17 changed files with 901 additions and 594 deletions
@@ -1,3 +1,4 @@
SRC_FILES := ping.c lwip_ping_demo.c lwip_tcp_demo.c lwip_udp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c iperf.c
SRC_FILES := ping.c lwip_ping_demo.c lwip_tcp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c iperf.c
# SRC_FILES := ping.c lwip_ping_demo.c lwip_tcp_demo.c lwip_udp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c iperf.c
include $(KERNEL_ROOT)/compiler.mk
@@ -18,6 +18,7 @@
#include <sys_arch.h>
#include "lwip/sockets.h"
#include <netdb.h>
#include <lwiperf.h>
#define IPERF_PORT 5001
#define IPERF_BUFSZ (4 * 1024)
@@ -33,9 +34,9 @@ typedef struct{
} IPERF_PARAM;
static IPERF_PARAM param = {IPERF_MODE_STOP, NULL, IPERF_PORT};
char tcp_iperf_ip[] = {192, 168, 131, 77};
char tcp_iperf_ip[] = {192, 168, 130, 77};
char tcp_iperf_mask[] = {255, 255, 254, 0};
char tcp_iperf_gw[] = {192, 168, 131, 1};
char tcp_iperf_gw[] = {192, 168, 130, 1};
static void iperf_udp_client(void *thread_param)
{
@@ -226,15 +227,14 @@ static void iperf_client(void *thread_param)
while (param.mode != IPERF_MODE_STOP){
tick2 = CurrentTicksGain();
if (tick2 - tick1 >= TICK_PER_SECOND * 5){
long data;
int integer, decimal;
double speed;
// int integer, decimal;
KTaskDescriptorType tid;
tid = GetKTaskDescriptor();
data = sentlen * TICK_PER_SECOND / 125 / (tick2 - tick1);
integer = data/1000;
decimal = data%1000;
KPrintf("%s: %d.%03d0 Mbps!\n", tid->task_base_info.name, integer, decimal);
speed = (double)(sentlen * TICK_PER_SECOND / 125 / (tick2 - tick1));
speed = speed / 1000.0f;
printf("%s: %2.4f Mbps!\n", tid->task_base_info.name, speed);
tick1 = tick2;
sentlen = 0;
}
@@ -301,14 +301,15 @@ void iperf_server(void *thread_param)
FD_ZERO(&readset);
FD_SET(sock, &readset);
if (select(sock + 1, &readset, NULL, NULL, &timeout) == 0)
if (select(sock + 1, &readset, NULL, NULL, &timeout) == 0) {
continue;
}
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
KPrintf("new client connected from (%s, %d)",
printf("new client connected from (%s, %d)\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
int flag = 1;
@@ -318,33 +319,36 @@ void iperf_server(void *thread_param)
(void *) &flag, /* the cast is historical cruft */
sizeof(int)); /* length of option value */
printf(" \n"); //BUG
recvlen = 0;
tick1 = CurrentTicksGain();
while (param.mode != IPERF_MODE_STOP){
bytes_received = recv(connected, recv_data, IPERF_BUFSZ, 0);
if (bytes_received <= 0) break;
if (bytes_received == 0) {
KPrintf("client disconnected (%s, %d)\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
break;
} else if (bytes_received < 0) {
KPrintf("recv error, client: (%s, %d)\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
break;
}
recvlen += bytes_received;
tick2 = CurrentTicksGain();
if (tick2 - tick1 >= TICK_PER_SECOND * 5){
long data;
int integer, decimal;
if (tick2 - tick1 >= TICK_PER_SECOND * 5) {
double speed;
// int integer, decimal;
KTaskDescriptorType tid;
tid = GetKTaskDescriptor();
data = recvlen * TICK_PER_SECOND / 125 / (tick2 - tick1);
integer = data/1000;
decimal = data%1000;
KPrintf("%s: %d.%03d0 Mbps!\n", tid->task_base_info.name, integer, decimal);
speed = (double)(recvlen * TICK_PER_SECOND / (125 * (tick2 - tick1)));
speed = speed / 1000.0f;
printf("%s: %2.4f Mbps!\n", tid->task_base_info.name, speed);
tick1 = tick2;
recvlen = 0;
}
}
KPrintf("client disconnected (%s, %d)\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
if (connected >= 0) closesocket(connected);
connected = -1;
}
@@ -399,6 +403,7 @@ int iperf(int argc, char **argv)
{
/* stop iperf */
param.mode = IPERF_MODE_STOP;
printf("iperf stop.\n");
return 0;
}
else if (strcmp(argv[index], "-s") == 0)
@@ -506,5 +511,39 @@ __usage:
return 0;
}
#if LWIP_TCP
static void
lwiperf_report(void *arg, enum lwiperf_report_type report_type,
const ip_addr_t* local_addr, u16_t local_port, const ip_addr_t* remote_addr, u16_t remote_port,
u32_t bytes_transferred, u32_t ms_duration, u32_t bandwidth_kbitpsec)
{
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(local_addr);
LWIP_UNUSED_ARG(local_port);
printf("IPERF report: type=%d, remote: %s:%d, total bytes: %"U32_F", duration in ms: %"U32_F", kbits/s: %"U32_F"\n",
(int)report_type, ipaddr_ntoa(remote_addr), (int)remote_port, bytes_transferred, ms_duration, bandwidth_kbitpsec);
}
#endif /* LWIP_TCP */
void
lwiperf_example_init(void)
{
#if LWIP_TCP
ip4_addr_t ipaddr;
lwiperf_start_tcp_server_default(lwiperf_report, NULL);
// IP4_ADDR(&ipaddr,192,168,0,181);
// lwiperf_start_tcp_client_default(&ipaddr, lwiperf_report, NULL);
#endif
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(8),
iperf, iperf, netutils iperf);
extern void *lwiperf_start_tcp_server_default(lwiperf_report_fn report_fn, void *report_arg);
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(8),
lwiperf_tcp_server, lwiperf_example_init, netutils lwipperf);
@@ -30,7 +30,7 @@ static void LwipSetIPTask(void *param)
{
uint8_t enet_port = *(uint8_t *)param; ///< test enet port
printf("lw: [%s] config netport id[%d]\n", __func__, enet_port);
lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
lwip_config_tcp(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
}
void LwipSetIPTest(int argc, char *argv[])
@@ -64,7 +64,8 @@ void LwipSetIPTest(int argc, char *argv[])
sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]);
memcpy(lwip_eth0_ipaddr, lwip_ipaddr, strlen(lwip_ipaddr));
}
sys_thread_new("SET ip address", LwipSetIPTask, &enet_id, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
// sys_thread_new("SET ip address", LwipSetIPTask, &enet_id, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
LwipSetIPTask(&enet_id);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5),
@@ -19,13 +19,13 @@
*/
#include "board.h"
#include "lwip_demo.h"
#include "sys_arch.h"
#include "lwip/sockets.h"
#include "tcpecho_raw.h"
#include <shell.h>
#include <sys.h>
#include <xizi.h>
#include "lwip_demo.h"
#include "lwip/sockets.h"
#include "tcpecho_raw.h"
char tcp_demo_msg[LWIP_TEST_MSG_SIZE] = { 0 };
char tcp_demo_ip[] = {192, 168, 250, 252};
u16_t tcp_demo_port = LWIP_TARGET_PORT;
@@ -211,7 +211,9 @@ ping_recv(int s)
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
ip_addr_debug_print_val(PING_DEBUG, fromaddr);
#ifdef LWIP_DEBUG
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
#endif
/* todo: support ICMP6 echo */
#if LWIP_IPV4
@@ -235,7 +237,9 @@ ping_recv(int s)
}
if (len == 0) {
#ifdef LWIP_DEBUG
LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time)));
#endif
}
/* do some ping result processing */
@@ -524,8 +528,10 @@ int get_url_ip(char* url)
#endif /* LWIP_DEBUG */
if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
{
#ifdef LWIP_DEBUG
lw_notice("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", recv_len, inet_ntoa(ina), cnt,
ttl, sys_now() - ping_time);
#endif
}
else
{