314 lines
11 KiB
C
314 lines
11 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <inttypes.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/time.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "esp_log.h"
|
|
#include "esp_err.h"
|
|
#include "esp_timer.h"
|
|
#include "nvs_flash.h"
|
|
#include "nvs.h"
|
|
#include "esp_event.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_wifi.h"
|
|
#include "iperf.h"
|
|
#include "status_led.h"
|
|
|
|
static const char *TAG = "iperf";
|
|
|
|
static EventGroupHandle_t s_iperf_event_group = NULL;
|
|
#define IPERF_IP_READY_BIT (1 << 0)
|
|
#define IPERF_STOP_REQ_BIT (1 << 1)
|
|
|
|
#define RATE_CHECK_INTERVAL_US 500000
|
|
#define MIN_PACING_INTERVAL_US 100
|
|
|
|
// --- REMOVED DUPLICATE STRUCT DEFINITION ---
|
|
// We rely on the definition in iperf.h
|
|
|
|
typedef struct {
|
|
iperf_cfg_t cfg;
|
|
bool finish;
|
|
uint32_t buffer_len;
|
|
uint8_t *buffer;
|
|
} iperf_ctrl_t;
|
|
|
|
static iperf_ctrl_t s_iperf_ctrl = {0};
|
|
static TaskHandle_t s_iperf_task_handle = NULL;
|
|
static esp_event_handler_instance_t instance_any_id;
|
|
static esp_event_handler_instance_t instance_got_ip;
|
|
|
|
static void iperf_network_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
|
if (s_iperf_event_group == NULL) return;
|
|
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
xEventGroupSetBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
|
}
|
|
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
xEventGroupClearBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
|
status_led_set_state(LED_STATE_NO_CONFIG);
|
|
}
|
|
}
|
|
|
|
static bool iperf_wait_for_ip(void) {
|
|
if (!s_iperf_event_group) s_iperf_event_group = xEventGroupCreate();
|
|
|
|
// Check if we already have IP
|
|
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
|
if (netif) {
|
|
esp_netif_ip_info_t ip_info;
|
|
if (esp_netif_get_ip_info(netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
|
|
xEventGroupSetBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
|
}
|
|
}
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &iperf_network_event_handler, NULL, &instance_any_id));
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &iperf_network_event_handler, NULL, &instance_got_ip));
|
|
|
|
EventBits_t bits = xEventGroupWaitBits(s_iperf_event_group, IPERF_IP_READY_BIT | IPERF_STOP_REQ_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
|
|
|
esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id);
|
|
esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip);
|
|
return !(bits & IPERF_STOP_REQ_BIT);
|
|
}
|
|
|
|
static void trim_whitespace(char *str) {
|
|
char *end = str + strlen(str) - 1;
|
|
while(end > str && isspace((unsigned char)*end)) end--;
|
|
*(end+1) = 0;
|
|
}
|
|
|
|
static void iperf_read_nvs_config(iperf_cfg_t *cfg) {
|
|
nvs_handle_t my_handle;
|
|
if (nvs_open("storage", NVS_READONLY, &my_handle) != ESP_OK) {
|
|
ESP_LOGW(TAG, "No NVS config found, using defaults");
|
|
return;
|
|
}
|
|
|
|
uint32_t val;
|
|
if (nvs_get_u32(my_handle, NVS_KEY_IPERF_PERIOD, &val) == ESP_OK) cfg->pacing_period_us = val;
|
|
if (nvs_get_u32(my_handle, NVS_KEY_IPERF_BURST, &val) == ESP_OK) cfg->burst_count = val;
|
|
if (nvs_get_u32(my_handle, NVS_KEY_IPERF_LEN, &val) == ESP_OK) cfg->send_len = val;
|
|
if (nvs_get_u32(my_handle, NVS_KEY_IPERF_PORT, &val) == ESP_OK) cfg->dport = (uint16_t)val;
|
|
|
|
// Read Role
|
|
size_t req;
|
|
char buf[16];
|
|
req = sizeof(buf);
|
|
if (nvs_get_str(my_handle, NVS_KEY_IPERF_ROLE, buf, &req) == ESP_OK) {
|
|
if (strcmp(buf, "SERVER") == 0) {
|
|
cfg->flag |= IPERF_FLAG_SERVER;
|
|
cfg->flag &= ~IPERF_FLAG_CLIENT;
|
|
} else {
|
|
cfg->flag |= IPERF_FLAG_CLIENT;
|
|
cfg->flag &= ~IPERF_FLAG_SERVER;
|
|
}
|
|
}
|
|
|
|
// Read Proto
|
|
req = sizeof(buf);
|
|
if (nvs_get_str(my_handle, NVS_KEY_IPERF_PROTO, buf, &req) == ESP_OK) {
|
|
if (strcmp(buf, "TCP") == 0) {
|
|
cfg->flag |= IPERF_FLAG_TCP;
|
|
cfg->flag &= ~IPERF_FLAG_UDP;
|
|
} else {
|
|
cfg->flag |= IPERF_FLAG_UDP;
|
|
cfg->flag &= ~IPERF_FLAG_TCP;
|
|
}
|
|
}
|
|
|
|
// Read Dest IP
|
|
if (nvs_get_str(my_handle, NVS_KEY_IPERF_DST_IP, NULL, &req) == ESP_OK) {
|
|
char *ip_str = malloc(req);
|
|
if (ip_str) {
|
|
nvs_get_str(my_handle, NVS_KEY_IPERF_DST_IP, ip_str, &req);
|
|
trim_whitespace(ip_str);
|
|
cfg->dip = inet_addr(ip_str);
|
|
free(ip_str);
|
|
}
|
|
}
|
|
nvs_close(my_handle);
|
|
}
|
|
|
|
void iperf_set_pps(uint32_t pps) {
|
|
if (pps == 0) pps = 1;
|
|
uint32_t period_us = 1000000 / pps;
|
|
if (period_us < MIN_PACING_INTERVAL_US) period_us = MIN_PACING_INTERVAL_US;
|
|
|
|
if (s_iperf_task_handle != NULL) {
|
|
s_iperf_ctrl.cfg.pacing_period_us = period_us;
|
|
ESP_LOGI(TAG, "Runtime pacing updated to %" PRIu32 " PPS", pps);
|
|
} else {
|
|
s_iperf_ctrl.cfg.pacing_period_us = period_us;
|
|
}
|
|
}
|
|
|
|
uint32_t iperf_get_pps(void) {
|
|
if (s_iperf_ctrl.cfg.pacing_period_us == 0) return 0;
|
|
return 1000000 / s_iperf_ctrl.cfg.pacing_period_us;
|
|
}
|
|
|
|
static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
|
if (!iperf_wait_for_ip()) return ESP_FAIL;
|
|
|
|
struct sockaddr_in addr;
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons(ctrl->cfg.dport > 0 ? ctrl->cfg.dport : 5001);
|
|
addr.sin_addr.s_addr = ctrl->cfg.dip;
|
|
|
|
// Log the actual destination we are about to use
|
|
char ip_str[32];
|
|
inet_ntop(AF_INET, &addr.sin_addr, ip_str, sizeof(ip_str));
|
|
ESP_LOGI(TAG, "Starting UDP Traffic to %s:%d (Period=%luus, Burst=%lu)",
|
|
ip_str, ntohs(addr.sin_port), ctrl->cfg.pacing_period_us, ctrl->cfg.burst_count);
|
|
|
|
int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
if (sockfd < 0) {
|
|
status_led_set_state(LED_STATE_FAILED);
|
|
ESP_LOGE(TAG, "Failed to create socket: errno %d", errno);
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
status_led_set_state(LED_STATE_TRANSMITTING_SLOW);
|
|
|
|
int64_t next_send_time = esp_timer_get_time();
|
|
int64_t end_time = (ctrl->cfg.time == 0) ? INT64_MAX : esp_timer_get_time() + (int64_t)ctrl->cfg.time * 1000000LL;
|
|
|
|
int64_t last_rate_check = esp_timer_get_time();
|
|
uint32_t packets_since_check = 0;
|
|
int64_t enomem_start_time = 0;
|
|
|
|
// --- Sequence Counter ---
|
|
int32_t packet_id = 0;
|
|
|
|
while (!ctrl->finish && esp_timer_get_time() < end_time) {
|
|
int64_t now = esp_timer_get_time();
|
|
int64_t wait = next_send_time - now;
|
|
|
|
if (wait > 2000) vTaskDelay(pdMS_TO_TICKS(wait / 1000));
|
|
else while (esp_timer_get_time() < next_send_time) taskYIELD();
|
|
|
|
for (int k = 0; k < ctrl->cfg.burst_count; k++) {
|
|
|
|
// --- Populate Iperf Header ---
|
|
udp_datagram *hdr = (udp_datagram *)ctrl->buffer;
|
|
|
|
// 1. Sequence ID (Big Endian)
|
|
hdr->id = htonl(packet_id++);
|
|
|
|
// 2. Timestamp (Big Endian)
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
hdr->tv_sec = htonl(tv.tv_sec);
|
|
hdr->tv_usec = htonl(tv.tv_usec);
|
|
|
|
// ----------------------------------
|
|
|
|
int sent = sendto(sockfd, ctrl->buffer, ctrl->cfg.send_len, 0, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
if (sent > 0) {
|
|
packets_since_check++;
|
|
enomem_start_time = 0;
|
|
|
|
if (now - last_rate_check > RATE_CHECK_INTERVAL_US) {
|
|
int64_t interval = now - last_rate_check;
|
|
double cycles = (double)interval / (double)ctrl->cfg.pacing_period_us;
|
|
uint32_t expected_pkts = (uint32_t)(cycles * ctrl->cfg.burst_count);
|
|
uint32_t threshold = (expected_pkts * 3) / 4;
|
|
|
|
led_state_t target = (packets_since_check >= threshold)
|
|
? LED_STATE_TRANSMITTING
|
|
: LED_STATE_TRANSMITTING_SLOW;
|
|
|
|
if (status_led_get_state() != target) status_led_set_state(target);
|
|
|
|
last_rate_check = now;
|
|
packets_since_check = 0;
|
|
}
|
|
} else {
|
|
if (errno == 12) { // ENOMEM
|
|
if (status_led_get_state() != LED_STATE_STALLED) status_led_set_state(LED_STATE_STALLED);
|
|
if (enomem_start_time == 0) enomem_start_time = now;
|
|
else if (now - enomem_start_time > 10000000) {
|
|
status_led_set_state(LED_STATE_FAILED); goto exit;
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
|
} else {
|
|
ESP_LOGE(TAG, "Send failed: errno %d", errno);
|
|
status_led_set_state(LED_STATE_FAILED); goto exit;
|
|
}
|
|
}
|
|
}
|
|
next_send_time += ctrl->cfg.pacing_period_us;
|
|
}
|
|
|
|
exit:
|
|
close(sockfd);
|
|
return ESP_OK;
|
|
}
|
|
|
|
static void iperf_task(void *arg) {
|
|
iperf_ctrl_t *ctrl = (iperf_ctrl_t *)arg;
|
|
|
|
// Simple logic dispatch
|
|
if (ctrl->cfg.flag & IPERF_FLAG_UDP) {
|
|
if (ctrl->cfg.flag & IPERF_FLAG_CLIENT) {
|
|
iperf_start_udp_client(ctrl);
|
|
} else {
|
|
ESP_LOGW(TAG, "UDP Server mode not implemented in this snippet");
|
|
}
|
|
} else {
|
|
ESP_LOGW(TAG, "TCP mode not implemented in this snippet");
|
|
}
|
|
|
|
free(ctrl->buffer);
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
void iperf_start(iperf_cfg_t *cfg) {
|
|
if (s_iperf_task_handle) return;
|
|
|
|
s_iperf_ctrl.cfg = *cfg;
|
|
|
|
// 1. Read Overrides from NVS
|
|
iperf_read_nvs_config(&s_iperf_ctrl.cfg);
|
|
|
|
// 2. Apply Defaults if NVS/Args were missing
|
|
if (s_iperf_ctrl.cfg.send_len == 0) s_iperf_ctrl.cfg.send_len = 1470;
|
|
if (s_iperf_ctrl.cfg.pacing_period_us == 0) s_iperf_ctrl.cfg.pacing_period_us = 10000;
|
|
if (s_iperf_ctrl.cfg.burst_count == 0) s_iperf_ctrl.cfg.burst_count = 1;
|
|
|
|
// 3. Log the Final Config
|
|
char ip_str[32] = "0.0.0.0";
|
|
struct in_addr ip_addr;
|
|
ip_addr.s_addr = s_iperf_ctrl.cfg.dip;
|
|
inet_ntop(AF_INET, &ip_addr, ip_str, sizeof(ip_str));
|
|
|
|
ESP_LOGI(TAG, "--- IPERF CONFIG ---");
|
|
ESP_LOGI(TAG, "Role: %s", (s_iperf_ctrl.cfg.flag & IPERF_FLAG_CLIENT) ? "CLIENT" : "SERVER");
|
|
ESP_LOGI(TAG, "Proto: %s", (s_iperf_ctrl.cfg.flag & IPERF_FLAG_UDP) ? "UDP" : "TCP");
|
|
ESP_LOGI(TAG, "Dest IP: %s", ip_str);
|
|
ESP_LOGI(TAG, "Dest Port: %d", s_iperf_ctrl.cfg.dport);
|
|
ESP_LOGI(TAG, "Period: %lu us", s_iperf_ctrl.cfg.pacing_period_us);
|
|
ESP_LOGI(TAG, "--------------------");
|
|
|
|
s_iperf_ctrl.finish = false;
|
|
s_iperf_ctrl.buffer_len = s_iperf_ctrl.cfg.send_len + 128;
|
|
s_iperf_ctrl.buffer = calloc(1, s_iperf_ctrl.buffer_len);
|
|
|
|
s_iperf_event_group = xEventGroupCreate();
|
|
xTaskCreate(iperf_task, "iperf", 4096, &s_iperf_ctrl, 5, &s_iperf_task_handle);
|
|
}
|
|
|
|
void iperf_stop(void) {
|
|
if (s_iperf_task_handle) {
|
|
s_iperf_ctrl.finish = true;
|
|
if (s_iperf_event_group) xEventGroupSetBits(s_iperf_event_group, IPERF_STOP_REQ_BIT);
|
|
}
|
|
}
|