Skip to content

Commit 51a093d

Browse files
committed
lwip - Added windowing to tcp packet pressure tests
Despite being able to buffer an arbitrary stream of data, TCP send is still limited by the available buffer space in the network stack. Errors from TCP send are perfectly reasonable and should be handled by reducing the buffer that is attempted.
1 parent 2da71a7 commit 51a093d

File tree

2 files changed

+36
-8
lines changed
  • features/FEATURE_LWIP/TESTS/mbedmicro-net

2 files changed

+36
-8
lines changed

features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure/main.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,40 @@ int main() {
156156
RandSeq rx_seq;
157157
size_t rx_count = 0;
158158
size_t tx_count = 0;
159+
size_t window = buffer_size;
159160

160161
while (tx_count < size || rx_count < size) {
161162
// Send out data
162163
if (tx_count < size) {
163164
size_t chunk_size = size - tx_count;
164-
if (chunk_size > buffer_size) {
165-
chunk_size = buffer_size;
165+
if (chunk_size > window) {
166+
chunk_size = window;
166167
}
167168

168169
tx_seq.buffer(buffer, chunk_size);
169170
int td = sock.send(buffer, chunk_size);
170-
TEST_ASSERT(td > 0 || td == NSAPI_ERROR_WOULD_BLOCK);
171+
171172
if (td > 0) {
172173
if (MBED_CFG_TCP_CLIENT_PACKET_PRESSURE_DEBUG) {
173174
printf("TCP: tx -> %d\r\n", td);
174175
}
175176
tx_seq.skip(td);
176177
tx_count += td;
178+
} else if (td != NSAPI_ERROR_WOULD_BLOCK) {
179+
// We may fail to send because of buffering issues,
180+
// cut buffer in half
181+
if (window > MBED_CFG_TCP_CLIENT_PACKET_PRESSURE_MIN) {
182+
window /= 2;
183+
}
184+
185+
if (MBED_CFG_TCP_CLIENT_PACKET_PRESSURE_DEBUG) {
186+
printf("TCP: Not sent (%d), window = %d\r\n", td, window);
187+
}
177188
}
178189
}
179190

180191
// Verify recieved data
181-
if (rx_count < size) {
192+
while (rx_count < size) {
182193
int rd = sock.recv(buffer, buffer_size);
183194
TEST_ASSERT(rd > 0 || rd == NSAPI_ERROR_WOULD_BLOCK);
184195
if (rd > 0) {
@@ -189,6 +200,8 @@ int main() {
189200
TEST_ASSERT_EQUAL(0, diff);
190201
rx_seq.skip(rd);
191202
rx_count += rd;
203+
} else if (rd == NSAPI_ERROR_WOULD_BLOCK) {
204+
break;
192205
}
193206
}
194207
}

features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure_parallel/main.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,19 @@ class PressureTest {
153153
RandSeq rx_seq;
154154
size_t rx_count = 0;
155155
size_t tx_count = 0;
156+
size_t window = buffer_size;
156157

157158
while (tx_count < size || rx_count < size) {
158159
// Send out data
159160
if (tx_count < size) {
160161
size_t chunk_size = size - tx_count;
161-
if (chunk_size > buffer_size) {
162-
chunk_size = buffer_size;
162+
if (chunk_size > window) {
163+
chunk_size = window;
163164
}
164165

165166
tx_seq.buffer(buffer, chunk_size);
166167
int td = sock.send(buffer, chunk_size);
167-
TEST_ASSERT(td > 0 || td == NSAPI_ERROR_WOULD_BLOCK);
168+
168169
if (td > 0) {
169170
if (MBED_CFG_TCP_CLIENT_PACKET_PRESSURE_DEBUG) {
170171
iomutex.lock();
@@ -173,11 +174,23 @@ class PressureTest {
173174
}
174175
tx_seq.skip(td);
175176
tx_count += td;
177+
} else if (td != NSAPI_ERROR_WOULD_BLOCK) {
178+
// We may fail to send because of buffering issues,
179+
// cut buffer in half
180+
if (window > MBED_CFG_TCP_CLIENT_PACKET_PRESSURE_MIN) {
181+
window /= 2;
182+
}
183+
184+
if (MBED_CFG_TCP_CLIENT_PACKET_PRESSURE_DEBUG) {
185+
iomutex.lock();
186+
printf("TCP: Not sent (%d), window = %d\r\n", td, window);
187+
iomutex.unlock();
188+
}
176189
}
177190
}
178191

179192
// Verify recieved data
180-
if (rx_count < size) {
193+
while (rx_count < size) {
181194
int rd = sock.recv(buffer, buffer_size);
182195
TEST_ASSERT(rd > 0 || rd == NSAPI_ERROR_WOULD_BLOCK);
183196
if (rd > 0) {
@@ -190,6 +203,8 @@ class PressureTest {
190203
TEST_ASSERT_EQUAL(0, diff);
191204
rx_seq.skip(rd);
192205
rx_count += rd;
206+
} else if (rd == NSAPI_ERROR_WOULD_BLOCK) {
207+
break;
193208
}
194209
}
195210
}

0 commit comments

Comments
 (0)