iperf: handle NO_MEM error in OpenThread iperf

* simplify iperf send/recv loop
This commit is contained in:
zhangwenxu 2021-10-25 14:47:05 +08:00 committed by bot
parent 9044374032
commit 0fe32adb58
3 changed files with 30 additions and 60 deletions

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed 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
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "esp_netif.h"
@ -55,10 +47,15 @@ static err_t openthread_output_ip6(struct netif *netif, struct pbuf *p, const st
pbuf_free(q);
}
/* Check error */
if (unlikely(ret != ESP_OK)) {
return ERR_ABRT;
} else {
switch(ret) {
case ESP_ERR_NO_MEM:
return ERR_MEM;
case ESP_OK:
return ERR_OK;
default:
return ERR_ABRT;
}
}

View File

@ -9,6 +9,7 @@
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <sys/socket.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@ -111,36 +112,25 @@ static esp_err_t iperf_start_report(void)
static void socket_recv(int recv_socket, struct sockaddr_storage listen_addr, uint8_t type)
{
bool udp_recv_start = true;
bool iperf_recv_start = true;
uint8_t *buffer;
int want_recv = 0;
int actual_recv = 0;
socklen_t addr_len = sizeof(struct sockaddr);
socklen_t socklen = (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
const char *error_log = (type == IPERF_TRANS_TYPE_TCP) ? "tcp server recv" : "udp server recv";
buffer = s_iperf_ctrl.buffer;
want_recv = s_iperf_ctrl.buffer_len;
while (!s_iperf_ctrl.finish) {
if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) {
addr_len = sizeof(struct sockaddr_in6);
actual_recv = recvfrom(recv_socket, buffer, want_recv, 0, (struct sockaddr *)&listen_addr, &addr_len);
} else if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4) {
addr_len = sizeof(struct sockaddr_in);
actual_recv = recvfrom(recv_socket, buffer, want_recv, 0, (struct sockaddr *)&listen_addr, &addr_len);
}
actual_recv = recvfrom(recv_socket, buffer, want_recv, 0, (struct sockaddr *)&listen_addr, &socklen);
if (actual_recv < 0) {
if (type == IPERF_TRANS_TYPE_TCP) {
iperf_show_socket_error_reason("tcp server recv", recv_socket);
}
if (type == IPERF_TRANS_TYPE_UDP) {
iperf_show_socket_error_reason("udp server recv", recv_socket);
}
iperf_show_socket_error_reason(error_log, recv_socket);
s_iperf_ctrl.finish = true;
break;
} else {
if (udp_recv_start) {
if (iperf_recv_start) {
iperf_start_report();
udp_recv_start = false;
iperf_recv_start = false;
}
s_iperf_ctrl.actual_len += actual_recv;
}
@ -149,51 +139,35 @@ static void socket_recv(int recv_socket, struct sockaddr_storage listen_addr, ui
static void socket_send(int send_socket, struct sockaddr_storage dest_addr, uint8_t type)
{
bool retry = false;
uint8_t *buffer;
uint8_t delay = 0;
uint8_t delay = 1;
int actual_send = 0;
int want_send = 0;
int err = 0;
const socklen_t socklen = (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
const char *error_log = (type == IPERF_TRANS_TYPE_TCP) ? "tcp client send" : "udp client send";
buffer = s_iperf_ctrl.buffer;
want_send = s_iperf_ctrl.buffer_len;
iperf_start_report();
while (!s_iperf_ctrl.finish) {
if (type == IPERF_TRANS_TYPE_UDP) {
if (false == retry) {
delay = 1;
}
retry = false;
}
if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) {
actual_send = sendto(send_socket, buffer, want_send, 0, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));
} else if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4) {
actual_send = sendto(send_socket, buffer, want_send, 0, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in));
}
actual_send = sendto(send_socket, buffer, want_send, 0, (struct sockaddr *)&dest_addr, socklen);
if (actual_send != want_send) {
if (type == IPERF_TRANS_TYPE_UDP) {
err = iperf_get_socket_error_code(send_socket);
if (err == ENOMEM) {
vTaskDelay(delay);
if (delay < IPERF_MAX_DELAY) {
delay <<= 1;
}
retry = true;
continue;
delay = MIN(delay << 1, IPERF_MAX_DELAY);
} else {
if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4) {
ESP_LOGE(TAG, "udp client send abort: err=%d", err);
}
iperf_show_socket_error_reason(error_log, send_socket);
}
}
if (type == IPERF_TRANS_TYPE_TCP) {
iperf_show_socket_error_reason("tcp client send", send_socket);
ESP_LOGI(TAG, "tcp client send error\n");
} else if (type == IPERF_TRANS_TYPE_TCP) {
iperf_show_socket_error_reason(error_log, send_socket);
break;
}
} else {
delay = 1;
s_iperf_ctrl.actual_len += actual_send;
}
}

View File

@ -1450,7 +1450,6 @@ components/lwip/port/esp32/include/sntp/sntp_get_set_time.h
components/lwip/port/esp32/include/sys/socket.h
components/lwip/port/esp32/netif/dhcp_state.c
components/lwip/port/esp32/netif/ethernetif.c
components/lwip/port/esp32/netif/openthreadif.c
components/lwip/port/esp32/netif/wlanif.c
components/lwip/port/esp32/no_vfs_syscalls.c
components/lwip/port/esp32/vfs_lwip.c