Skip to content

Commit 836b05e

Browse files
committed
Disabled loopback by default in config. Addressed review comments.
1 parent acfc27a commit 836b05e

6 files changed

Lines changed: 53 additions & 19 deletions

File tree

.github/workflows/linux.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ jobs:
9090
set -euo pipefail
9191
timeout --preserve-status 2m sudo LD_PRELOAD=$PWD/libwolfip.so ping -c 5 10.10.10.1
9292
93+
- name: Rebuild libwolfip.so with loopback enabled
94+
run: |
95+
set -euo pipefail
96+
rm -f libwolfip.so build/pie/wolfip.o
97+
make libwolfip.so EXTRA_CFLAGS="-DWOLFIP_ENABLE_LOOPBACK=1"
98+
9399
- name: Testing ICMP loopback smoke (no TUN/TAP)
94100
timeout-minutes: 2
95101
run: |

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CC?=gcc
22
CFLAGS:=-Wall -Werror -Wextra -I. -D_GNU_SOURCE
33
CFLAGS+=-g -ggdb -Wdeclaration-after-statement
4+
CFLAGS+=$(EXTRA_CFLAGS)
45
LDFLAGS+=-pthread
56
# additional debug flags:
67
# CFLAGS+=-DDEBUG_TAP

config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#endif
4141

4242
#ifndef WOLFIP_ENABLE_LOOPBACK
43-
#define WOLFIP_ENABLE_LOOPBACK 1
43+
#define WOLFIP_ENABLE_LOOPBACK 0
4444
#endif
4545

4646
/* Enable HTTP server for POSIX builds */

src/test/unit/unit_esp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#undef WOLFIP_MAX_INTERFACES
2929
#define WOLFIP_MAX_INTERFACES 1
3030
#undef WOLFIP_ENABLE_LOOPBACK
31+
#define WOLFIP_ENABLE_LOOPBACK 0
3132

3233
#include "check.h"
3334
#include "../../../config.h"

src/test/unit/unit_tests_proto.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,7 +2998,12 @@ START_TEST(test_wolfip_loopback_send_paths)
29982998

29992999
ck_assert_int_eq(wolfIP_loopback_send(NULL, frame, sizeof(frame)), -1);
30003000
ck_assert_int_eq(wolfIP_loopback_send(loop, NULL, sizeof(frame)), -1);
3001-
ck_assert_int_eq(wolfIP_loopback_send(loop, frame, sizeof(frame)), (int)sizeof(frame));
3001+
/* Fill the queue, each slot enqueue returns the byte count. */
3002+
for (unsigned int i = 0; i < WOLFIP_LOOPBACK_QUEUE_DEPTH; i++) {
3003+
ck_assert_int_eq(wolfIP_loopback_send(loop, frame, sizeof(frame)),
3004+
(int)sizeof(frame));
3005+
}
3006+
/* Next send must be dropped because the queue is full. */
30023007
ck_assert_int_eq(wolfIP_loopback_send(loop, frame, sizeof(frame)), 0);
30033008
}
30043009
END_TEST
@@ -3018,6 +3023,7 @@ START_TEST(test_wolfip_loopback_poll_paths)
30183023
memset(rx, 0, sizeof(rx));
30193024

30203025
ck_assert_int_eq(wolfIP_loopback_poll(NULL, rx, sizeof(rx)), 0);
3026+
ck_assert_int_eq(wolfIP_loopback_poll(loop, NULL, sizeof(rx)), 0);
30213027
ck_assert_int_eq(wolfIP_loopback_poll(loop, rx, sizeof(rx)), 0);
30223028

30233029
ck_assert_int_eq(wolfIP_loopback_send(loop, tx, sizeof(tx)), (int)sizeof(tx));
@@ -4890,10 +4896,12 @@ START_TEST(test_regression_loopback_pure_ack_uses_deferred_buffer_until_poll)
48904896
(uint32_t)sizeof(seg)), 0);
48914897
ck_assert_uint_eq(ts->sock.tcp.last_ack, ts->sock.tcp.ack);
48924898
ck_assert_uint_eq(last_frame_sent_size, 0U);
4893-
ck_assert_uint_eq(s.loopback_pending_len, expected_pending_len);
4899+
ck_assert_uint_eq(s.loopback_count, 1U);
4900+
ck_assert_uint_eq(s.loopback_pending_len[s.loopback_head],
4901+
expected_pending_len);
48944902

48954903
(void)wolfIP_poll(&s, 200);
4896-
ck_assert_uint_eq(s.loopback_pending_len, 0U);
4904+
ck_assert_uint_eq(s.loopback_count, 0U);
48974905
}
48984906
END_TEST
48994907

@@ -4934,13 +4942,17 @@ START_TEST(test_regression_loopback_pure_ack_drain_allows_next_send_cycle)
49344942
seg.flags = TCP_FLAG_ACK;
49354943

49364944
ck_assert_int_eq(tcp_send_empty_immediate(ts, &seg, (uint32_t)sizeof(seg)), 0);
4937-
ck_assert_uint_eq(s.loopback_pending_len, expected_pending_len);
4945+
ck_assert_uint_eq(s.loopback_count, 1U);
4946+
ck_assert_uint_eq(s.loopback_pending_len[s.loopback_head],
4947+
expected_pending_len);
49384948

49394949
ck_assert_int_eq(loop->poll(loop, rx, sizeof(rx)), (int)expected_pending_len);
4940-
ck_assert_uint_eq(s.loopback_pending_len, 0U);
4950+
ck_assert_uint_eq(s.loopback_count, 0U);
49414951

49424952
ck_assert_int_eq(tcp_send_empty_immediate(ts, &seg, (uint32_t)sizeof(seg)), 0);
4943-
ck_assert_uint_eq(s.loopback_pending_len, expected_pending_len);
4953+
ck_assert_uint_eq(s.loopback_count, 1U);
4954+
ck_assert_uint_eq(s.loopback_pending_len[s.loopback_head],
4955+
expected_pending_len);
49444956
}
49454957
END_TEST
49464958

src/wolfip.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,8 +1260,14 @@ struct wolfIP {
12601260
struct arp_pending_entry arp_pending[WOLFIP_ARP_PENDING_MAX];
12611261
#endif
12621262
#if WOLFIP_ENABLE_LOOPBACK
1263-
uint8_t loopback_buf[IP_MTU_MAX];
1264-
uint32_t loopback_pending_len;
1263+
#ifndef WOLFIP_LOOPBACK_QUEUE_DEPTH
1264+
#define WOLFIP_LOOPBACK_QUEUE_DEPTH 4
1265+
#endif
1266+
uint8_t loopback_buf[WOLFIP_LOOPBACK_QUEUE_DEPTH][IP_MTU_MAX];
1267+
uint32_t loopback_pending_len[WOLFIP_LOOPBACK_QUEUE_DEPTH];
1268+
uint8_t loopback_head;
1269+
uint8_t loopback_tail;
1270+
uint8_t loopback_count;
12651271
#endif
12661272
};
12671273

@@ -1369,39 +1375,47 @@ static inline uint32_t tcp_tx_payload_cap(const struct tsocket *t)
13691375
static int wolfIP_loopback_send(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
13701376
{
13711377
struct wolfIP *s;
1378+
uint8_t slot;
13721379
if (!ll || !buf)
13731380
return -1;
13741381
s = WOLFIP_CONTAINER_OF(ll, struct wolfIP, ll_dev);
13751382
if (!s)
13761383
return -1;
1377-
if (len > IP_MTU_MAX)
1384+
if (len == 0 || len > IP_MTU_MAX)
13781385
return 0;
1379-
if (s->loopback_pending_len > 0)
1380-
return 0; /* buffer busy, drop */
1386+
if (s->loopback_count >= WOLFIP_LOOPBACK_QUEUE_DEPTH)
1387+
return 0; /* queue full, drop */
13811388
/* buf is the IP payload (ETH header already stripped by
13821389
* wolfIP_ll_send_frame for non-ethernet devices).
13831390
* Store as-is; wolfIP_poll will re-add the ETH prefix. */
1384-
memcpy(s->loopback_buf, buf, len);
1385-
s->loopback_pending_len = len;
1391+
slot = s->loopback_tail;
1392+
memcpy(s->loopback_buf[slot], buf, len);
1393+
s->loopback_pending_len[slot] = len;
1394+
s->loopback_tail = (uint8_t)((slot + 1U) % WOLFIP_LOOPBACK_QUEUE_DEPTH);
1395+
s->loopback_count++;
13861396
return (int)len;
13871397
}
13881398

13891399
static int wolfIP_loopback_poll(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
13901400
{
13911401
struct wolfIP *s;
1402+
uint8_t slot;
13921403
uint32_t pending;
1393-
if (!ll)
1404+
if (!ll || !buf)
13941405
return 0;
13951406
s = WOLFIP_CONTAINER_OF(ll, struct wolfIP, ll_dev);
13961407
if (!s)
13971408
return 0;
1398-
pending = s->loopback_pending_len;
1399-
if (pending == 0)
1409+
if (s->loopback_count == 0)
14001410
return 0;
1411+
slot = s->loopback_head;
1412+
pending = s->loopback_pending_len[slot];
14011413
if (pending > len)
14021414
return 0;
1403-
s->loopback_pending_len = 0;
1404-
memcpy(buf, s->loopback_buf, pending);
1415+
memcpy(buf, s->loopback_buf[slot], pending);
1416+
s->loopback_pending_len[slot] = 0;
1417+
s->loopback_head = (uint8_t)((slot + 1U) % WOLFIP_LOOPBACK_QUEUE_DEPTH);
1418+
s->loopback_count--;
14051419
return (int)pending;
14061420
}
14071421
#endif

0 commit comments

Comments
 (0)