3 * LWIP iperf server implementation
5 * This is simple "Iperf" server to check your bandwith using Iperf on a PC as client.
6 * It is currently a minimal implementation providing an IPv4 TCP server only.
13 * Copyright (c) 2014 Simon Goldschmidt
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without modification,
17 * are permitted provided that the following conditions are met:
19 * 1. Redistributions of source code must retain the above copyright notice,
20 * this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
24 * 3. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
30 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
32 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 * This file is part of the lwIP TCP/IP stack.
40 * Author: Simon Goldschmidt
43 #include "lwip/apps/lwiperf.h"
50 /* Currently, only TCP-over-IPv4 is implemented (does iperf support IPv6 anyway?) */
51 #if LWIP_IPV4 && LWIP_TCP
53 /** Specify the idle timeout (in seconds) after that the test fails */
54 #ifndef LWIPERF_TCP_MAX_IDLE_SEC
55 #define LWIPERF_TCP_MAX_IDLE_SEC 10U
57 #if LWIPERF_TCP_MAX_IDLE_SEC > 255
58 #error LWIPERF_TCP_MAX_IDLE_SEC must fit into an u8_t
61 /* File internal memory allocation (struct lwiperf_*): this defaults to
64 #define LWIPERF_ALLOC(type) mem_malloc(sizeof(type))
65 #define LWIPERF_FREE(type, item) mem_free(item)
68 /** If this is 1, check that received data has the correct format */
69 #ifndef LWIPERF_CHECK_RX_DATA
70 #define LWIPERF_CHECK_RX_DATA 0
73 /** This is the Iperf settings struct sent from the client */
74 typedef struct _lwiperf_settings {
75 #define LWIPERF_FLAGS_ANSWER_TEST 0x80000000
76 #define LWIPERF_FLAGS_ANSWER_NOW 0x00000001
78 u32_t num_threads; /* unused for now */
80 u32_t buffer_len; /* unused for now */
81 u32_t win_band; /* TCP window / UDP rate: unused for now */
82 u32_t amount; /* pos. value: bytes?; neg. values: time (unit is 10ms: 1/100 second) */
85 /** Basic connection handle */
86 struct _lwiperf_state_base;
87 typedef struct _lwiperf_state_base lwiperf_state_base_t;
88 typedef struct _lwiperf_state_base {
91 /* 1=server, 0=client */
93 lwiperf_state_base_t* next;
94 lwiperf_state_base_t* related_server_state;
95 } lwiperf_state_base_t;
97 /** Connection handle for a TCP iperf session */
98 typedef struct _lwiperf_state_tcp {
99 lwiperf_state_base_t base;
100 struct tcp_pcb* server_pcb;
101 struct tcp_pcb* conn_pcb;
103 lwiperf_report_fn report_fn;
107 u32_t bytes_transferred;
108 lwiperf_settings_t settings;
109 u8_t have_settings_buf;
110 } lwiperf_state_tcp_t;
112 /** List of active iperf sessions */
113 static lwiperf_state_base_t* lwiperf_all_connections;
114 /** A const buffer to send from: we want to measure sending, not copying! */
115 const u8_t lwiperf_txbuf_const[1600] = {
116 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
117 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
118 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
119 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
120 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
121 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
122 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
123 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
124 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
125 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
126 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
127 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
128 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
129 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
130 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
131 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
132 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
133 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
134 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
135 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
136 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
137 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
138 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
139 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
140 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
141 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
142 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
143 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
144 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
145 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
146 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
147 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
148 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
149 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
150 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
151 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
152 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
153 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
154 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
155 '0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9',
158 static err_t lwiperf_tcp_poll(void *arg, struct tcp_pcb *tpcb);
159 static void lwiperf_tcp_err(void *arg, err_t err);
161 /** Add an iperf session to the 'active' list */
163 lwiperf_list_add(lwiperf_state_base_t* item)
165 if (lwiperf_all_connections == NULL) {
166 lwiperf_all_connections = item;
168 item = lwiperf_all_connections;
172 /** Remove an iperf session from the 'active' list */
174 lwiperf_list_remove(lwiperf_state_base_t* item)
176 lwiperf_state_base_t* prev = NULL;
177 lwiperf_state_base_t* iter;
178 for (iter = lwiperf_all_connections; iter != NULL; prev = iter, iter = iter->next) {
181 lwiperf_all_connections = iter->next;
185 /* @debug: ensure this item is listed only once */
186 for (iter = iter->next; iter != NULL; iter = iter->next) {
187 LWIP_ASSERT("duplicate entry", iter != item);
194 /** Call the report function of an iperf tcp session */
196 lwip_tcp_conn_report(lwiperf_state_tcp_t* conn, enum lwiperf_report_type report_type)
198 if ((conn != NULL) && (conn->report_fn != NULL)) {
199 u32_t now, duration_ms, bandwidth_kbitpsec;
201 duration_ms = now - conn->time_started;
202 if (duration_ms == 0) {
203 bandwidth_kbitpsec = 0;
205 bandwidth_kbitpsec = (conn->bytes_transferred / duration_ms) * 8U;
207 conn->report_fn(conn->report_arg, report_type,
208 &conn->conn_pcb->local_ip, conn->conn_pcb->local_port,
209 &conn->conn_pcb->remote_ip, conn->conn_pcb->remote_port,
210 conn->bytes_transferred, duration_ms, bandwidth_kbitpsec);
214 /** Close an iperf tcp session */
216 lwiperf_tcp_close(lwiperf_state_tcp_t* conn, enum lwiperf_report_type report_type)
220 lwip_tcp_conn_report(conn, report_type);
221 lwiperf_list_remove(&conn->base);
222 if (conn->conn_pcb != NULL) {
223 tcp_arg(conn->conn_pcb, NULL);
224 tcp_poll(conn->conn_pcb, NULL, 0);
225 tcp_sent(conn->conn_pcb, NULL);
226 tcp_recv(conn->conn_pcb, NULL);
227 tcp_err(conn->conn_pcb, NULL);
228 err = tcp_close(conn->conn_pcb);
230 /* don't want to wait for free memory here... */
231 tcp_abort(conn->conn_pcb);
234 /* no conn pcb, this is the server pcb */
235 err = tcp_close(conn->server_pcb);
236 LWIP_ASSERT("error", err != ERR_OK);
238 LWIPERF_FREE(lwiperf_state_tcp_t, conn);
241 /** Try to send more data on an iperf tcp session */
243 lwiperf_tcp_client_send_more(lwiperf_state_tcp_t* conn)
252 LWIP_ASSERT("conn invalid", (conn != NULL) && conn->base.tcp && (conn->base.server == 0));
256 if (conn->settings.amount & PP_HTONL(0x80000000)) {
257 /* this session is time-limited */
258 u32_t now = sys_now();
259 u32_t diff_ms = now - conn->time_started;
260 u32_t time = (u32_t)-(s32_t)htonl(conn->settings.amount);
261 u32_t time_ms = time * 10;
262 if (diff_ms >= time_ms) {
263 /* time specified by the client is over -> close the connection */
264 lwiperf_tcp_close(conn, LWIPERF_TCP_DONE_CLIENT);
268 /* this session is byte-limited */
269 u32_t amount_bytes = htonl(conn->settings.amount);
270 /* @todo: this can send up to 1*MSS more than requested... */
271 if (amount_bytes >= conn->bytes_transferred) {
272 /* all requested bytes transferred -> close the connection */
273 lwiperf_tcp_close(conn, LWIPERF_TCP_DONE_CLIENT);
278 if (conn->bytes_transferred < 24) {
279 /* transmit the settings a first time */
280 txptr = &((u8_t*)&conn->settings)[conn->bytes_transferred];
281 txlen_max = (u16_t)(24 - conn->bytes_transferred);
282 apiflags = TCP_WRITE_FLAG_COPY;
283 } else if (conn->bytes_transferred < 48) {
284 /* transmit the settings a second time */
285 txptr = &((u8_t*)&conn->settings)[conn->bytes_transferred - 24];
286 txlen_max = (u16_t)(48 - conn->bytes_transferred);
287 apiflags = TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE;
291 /* @todo: every x bytes, transmit the settings again */
292 txptr = (void*)(size_t)&lwiperf_txbuf_const[conn->bytes_transferred % 10];
294 if (conn->bytes_transferred == 48) { /* @todo: fix this for intermediate settings, too */
295 txlen_max = TCP_MSS - 24;
297 apiflags = 0; /* no copying needed */
302 err = tcp_write(conn->conn_pcb, txptr, txlen, apiflags);
303 if (err == ERR_MEM) {
306 } while ((err == ERR_MEM) && (txlen >= (TCP_MSS/2)));
309 conn->bytes_transferred += txlen;
315 tcp_output(conn->conn_pcb);
319 /** TCP sent callback, try to send more data */
321 lwiperf_tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
323 lwiperf_state_tcp_t* conn = (lwiperf_state_tcp_t*)arg;
324 /* @todo: check 'len' (e.g. to time ACK of all data)? for now, we just send more... */
325 LWIP_ASSERT("invalid conn", conn->conn_pcb == tpcb);
326 LWIP_UNUSED_ARG(tpcb);
327 LWIP_UNUSED_ARG(len);
329 conn->poll_count = 0;
331 return lwiperf_tcp_client_send_more(conn);
334 /** TCP connected callback (active connection), send data now */
336 lwiperf_tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
338 lwiperf_state_tcp_t* conn = (lwiperf_state_tcp_t*)arg;
339 LWIP_ASSERT("invalid conn", conn->conn_pcb == tpcb);
340 LWIP_UNUSED_ARG(tpcb);
342 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_REMOTE);
345 conn->poll_count = 0;
346 conn->time_started = sys_now();
347 return lwiperf_tcp_client_send_more(conn);
350 /** Start TCP connection back to the client (either parallel or after the
351 * receive test has finished.
354 lwiperf_tx_start(lwiperf_state_tcp_t* conn)
357 lwiperf_state_tcp_t* client_conn;
358 struct tcp_pcb* newpcb;
359 ip_addr_t remote_addr;
362 client_conn = (lwiperf_state_tcp_t*)LWIPERF_ALLOC(lwiperf_state_tcp_t);
363 if (client_conn == NULL) {
367 if (newpcb == NULL) {
368 LWIPERF_FREE(lwiperf_state_tcp_t, client_conn);
372 memcpy(client_conn, conn, sizeof(lwiperf_state_tcp_t));
373 client_conn->base.server = 0;
374 client_conn->server_pcb = NULL;
375 client_conn->conn_pcb = newpcb;
376 client_conn->time_started = sys_now(); /* TODO: set this again on 'connected' */
377 client_conn->poll_count = 0;
378 client_conn->next_num = 4; /* initial nr is '4' since the header has 24 byte */
379 client_conn->bytes_transferred = 0;
380 client_conn->settings.flags = 0; /* prevent the remote side starting back as client again */
382 tcp_arg(newpcb, client_conn);
383 tcp_sent(newpcb, lwiperf_tcp_client_sent);
384 tcp_poll(newpcb, lwiperf_tcp_poll, 2U);
385 tcp_err(newpcb, lwiperf_tcp_err);
387 ip_addr_copy(remote_addr, conn->conn_pcb->remote_ip);
388 remote_port = (u16_t)htonl(client_conn->settings.remote_port);
390 err = tcp_connect(newpcb, &remote_addr, remote_port, lwiperf_tcp_client_connected);
392 lwiperf_tcp_close(client_conn, LWIPERF_TCP_ABORTED_LOCAL);
395 lwiperf_list_add(&client_conn->base);
399 /** Receive data on an iperf tcp session */
401 lwiperf_tcp_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
406 lwiperf_state_tcp_t* conn = (lwiperf_state_tcp_t*)arg;
408 LWIP_ASSERT("pcb mismatch", conn->conn_pcb == tpcb);
409 LWIP_UNUSED_ARG(tpcb);
412 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_REMOTE);
416 /* connection closed -> test done */
417 if ((conn->settings.flags & PP_HTONL(LWIPERF_FLAGS_ANSWER_TEST|LWIPERF_FLAGS_ANSWER_NOW)) ==
418 PP_HTONL(LWIPERF_FLAGS_ANSWER_TEST)) {
419 /* client requested transmission after end of test */
420 lwiperf_tx_start(conn);
422 lwiperf_tcp_close(conn, LWIPERF_TCP_DONE_SERVER);
425 tot_len = p->tot_len;
427 conn->poll_count = 0;
429 if ((!conn->have_settings_buf) || ((conn->bytes_transferred -24) % (1024*128) == 0)) {
430 /* wait for 24-byte header */
431 if (p->tot_len < sizeof(lwiperf_settings_t)) {
432 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_LOCAL_DATAERROR);
436 if (!conn->have_settings_buf) {
437 if (pbuf_copy_partial(p, &conn->settings, sizeof(lwiperf_settings_t), 0) != sizeof(lwiperf_settings_t)) {
438 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_LOCAL);
442 conn->have_settings_buf = 1;
443 if ((conn->settings.flags & PP_HTONL(LWIPERF_FLAGS_ANSWER_TEST|LWIPERF_FLAGS_ANSWER_NOW)) ==
444 PP_HTONL(LWIPERF_FLAGS_ANSWER_TEST|LWIPERF_FLAGS_ANSWER_NOW)) {
445 /* client requested parallel transmission test */
446 err_t err2 = lwiperf_tx_start(conn);
447 if (err2 != ERR_OK) {
448 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_LOCAL_TXERROR);
454 if (pbuf_memcmp(p, 0, &conn->settings, sizeof(lwiperf_settings_t)) != 0) {
455 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_LOCAL_DATAERROR);
460 conn->bytes_transferred += sizeof(lwiperf_settings_t);
461 if(conn->bytes_transferred <= 24) {
462 conn->time_started = sys_now();
463 tcp_recved(tpcb, p->tot_len);
467 conn->next_num = 4; /* 24 bytes received... */
468 err = pbuf_header(p, -24);
469 LWIP_ASSERT("pbuf_header failed", err == ERR_OK);
473 for (q = p; q != NULL; q = q->next) {
474 #if LWIPERF_CHECK_RX_DATA
475 const u8_t* payload = (const u8_t*)q->payload;
477 for (i = 0; i < q->len; i++) {
478 u8_t val = payload[i];
479 u8_t num = val - '0';
480 if (num == conn->next_num) {
482 if (conn->next_num == 10) {
486 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_LOCAL_DATAERROR);
493 packet_idx += q->len;
496 LWIP_ASSERT("count mismatch", packet_idx == p->tot_len);
497 conn->bytes_transferred += packet_idx;
498 tcp_recved(tpcb, tot_len);
503 /** Error callback, iperf tcp session aborted */
505 lwiperf_tcp_err(void *arg, err_t err)
507 lwiperf_state_tcp_t* conn = (lwiperf_state_tcp_t*)arg;
508 LWIP_UNUSED_ARG(err);
509 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_REMOTE);
512 /** TCP poll callback, try to send more data */
514 lwiperf_tcp_poll(void *arg, struct tcp_pcb *tpcb)
516 lwiperf_state_tcp_t* conn = (lwiperf_state_tcp_t*)arg;
517 LWIP_ASSERT("pcb mismatch", conn->conn_pcb == tpcb);
518 LWIP_UNUSED_ARG(tpcb);
519 if (++conn->poll_count >= LWIPERF_TCP_MAX_IDLE_SEC) {
520 lwiperf_tcp_close(conn, LWIPERF_TCP_ABORTED_LOCAL);
523 if (!conn->base.server) {
524 lwiperf_tcp_client_send_more(conn);
530 /** This is called when a new client connects for an iperf tcp session */
532 lwiperf_tcp_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
534 lwiperf_state_tcp_t *s, *conn;
535 if ((err != ERR_OK) || (newpcb == NULL) || (arg == NULL)) {
539 s = (lwiperf_state_tcp_t*)arg;
540 conn = (lwiperf_state_tcp_t*)LWIPERF_ALLOC(lwiperf_state_tcp_t);
544 memset(conn, 0, sizeof(lwiperf_state_tcp_t));
546 conn->base.server = 1;
547 conn->base.related_server_state = &s->base;
548 conn->server_pcb = s->server_pcb;
549 conn->conn_pcb = newpcb;
550 conn->time_started = sys_now();
551 conn->report_fn = s->report_fn;
552 conn->report_arg = s->report_arg;
554 /* setup the tcp rx connection */
555 tcp_arg(newpcb, conn);
556 tcp_recv(newpcb, lwiperf_tcp_recv);
557 tcp_poll(newpcb, lwiperf_tcp_poll, 2U);
558 tcp_err(conn->conn_pcb, lwiperf_tcp_err);
560 lwiperf_list_add(&conn->base);
564 /** Start a TCP iperf server on the default TCP port (5001) and listen for
565 * incoming connections from iperf clients.
567 * @returns a connection handle that can be used to abort the server
568 * by calling @ref lwiperf_abort()
571 lwiperf_start_tcp_server_default(lwiperf_report_fn report_fn, void* report_arg)
573 return lwiperf_start_tcp_server(IP_ADDR_ANY, LWIPERF_TCP_PORT_DEFAULT,
574 report_fn, report_arg);
577 /** Start a TCP iperf server on a specific IP address and port and listen for
578 * incoming connections from iperf clients.
580 * @returns a connection handle that can be used to abort the server
581 * by calling @ref lwiperf_abort()
584 lwiperf_start_tcp_server(const ip_addr_t* local_addr, u16_t local_port,
585 lwiperf_report_fn report_fn, void* report_arg)
589 lwiperf_state_tcp_t* s = (lwiperf_state_tcp_t*)LWIPERF_ALLOC(lwiperf_state_tcp_t);
593 memset(s, 0, sizeof(lwiperf_state_tcp_t));
596 s->report_fn = report_fn;
597 s->report_arg = report_arg;
601 err = tcp_bind(pcb, local_addr, local_port);
603 s->server_pcb = tcp_listen_with_backlog(pcb, 1);
606 if (s->server_pcb == NULL) {
610 LWIPERF_FREE(lwiperf_state_tcp_t, s);
615 tcp_arg(s->server_pcb, s);
616 tcp_accept(s->server_pcb, lwiperf_tcp_accept);
618 lwiperf_list_add(&s->base);
622 /** Abort an iperf session (handle returned by lwiperf_start_tcp_server*()) */
624 lwiperf_abort(void* lwiperf_session)
626 lwiperf_state_base_t* i, *dealloc, *last = NULL;
628 for (i = lwiperf_all_connections; i != NULL; ) {
629 if ((i == lwiperf_session) || (i->related_server_state == lwiperf_session)) {
635 LWIPERF_FREE(lwiperf_state_tcp_t, dealloc); /* TODO: type? */
643 #endif /* LWIP_IPV4 && LWIP_TCP */