Skip to content

Commit 4e6843e

Browse files
authored
Merge pull request #22 from danielinux/fix-fifo-aligned-empty
Fix resetting fifo alignment when datagrams are consumed
2 parents 1ba7e03 + b8002dc commit 4e6843e

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

src/test/unit/unit.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,83 @@ START_TEST(test_fifo_pop_no_wrap_when_space_available)
605605
}
606606
END_TEST
607607

608+
START_TEST(test_fifo_peek_empty_unaligned_tail)
609+
{
610+
struct fifo f;
611+
uint8_t data[64];
612+
struct pkt_desc *desc;
613+
614+
fifo_init(&f, data, sizeof(data));
615+
f.head = 3;
616+
f.tail = 3;
617+
f.h_wrap = 0;
618+
619+
desc = fifo_peek(&f);
620+
ck_assert_ptr_eq(desc, NULL);
621+
ck_assert_uint_eq(f.tail, 3);
622+
ck_assert_uint_eq(f.h_wrap, 0);
623+
}
624+
END_TEST
625+
626+
START_TEST(test_fifo_len_empty_unaligned_tail)
627+
{
628+
struct fifo f;
629+
uint8_t data[64];
630+
631+
fifo_init(&f, data, sizeof(data));
632+
f.head = 3;
633+
f.tail = 3;
634+
f.h_wrap = 0;
635+
636+
ck_assert_uint_eq(fifo_len(&f), 0);
637+
ck_assert_uint_eq(f.tail, 3);
638+
}
639+
END_TEST
640+
641+
START_TEST(test_fifo_pop_empty_unaligned_tail)
642+
{
643+
struct fifo f;
644+
uint8_t data[64];
645+
646+
fifo_init(&f, data, sizeof(data));
647+
f.head = 3;
648+
f.tail = 3;
649+
f.h_wrap = 0;
650+
651+
ck_assert_ptr_eq(fifo_pop(&f), NULL);
652+
ck_assert_uint_eq(f.tail, 3);
653+
}
654+
END_TEST
655+
656+
START_TEST(test_fifo_push_pop_odd_sizes_drains_cleanly)
657+
{
658+
struct fifo f;
659+
uint8_t data[256];
660+
struct pkt_desc *desc;
661+
uint8_t p1[3] = {0x01, 0x02, 0x03};
662+
uint8_t p2[5] = {0x11, 0x12, 0x13, 0x14, 0x15};
663+
uint8_t p3[7] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27};
664+
665+
fifo_init(&f, data, sizeof(data));
666+
ck_assert_int_eq(fifo_push(&f, p1, sizeof(p1)), 0);
667+
ck_assert_int_eq(fifo_push(&f, p2, sizeof(p2)), 0);
668+
ck_assert_int_eq(fifo_push(&f, p3, sizeof(p3)), 0);
669+
670+
desc = fifo_pop(&f);
671+
ck_assert_ptr_nonnull(desc);
672+
ck_assert_mem_eq((const uint8_t *)f.data + desc->pos + sizeof(struct pkt_desc), p1, sizeof(p1));
673+
desc = fifo_pop(&f);
674+
ck_assert_ptr_nonnull(desc);
675+
ck_assert_mem_eq((const uint8_t *)f.data + desc->pos + sizeof(struct pkt_desc), p2, sizeof(p2));
676+
desc = fifo_pop(&f);
677+
ck_assert_ptr_nonnull(desc);
678+
ck_assert_mem_eq((const uint8_t *)f.data + desc->pos + sizeof(struct pkt_desc), p3, sizeof(p3));
679+
680+
ck_assert_uint_eq(fifo_len(&f), 0);
681+
ck_assert_ptr_eq(fifo_peek(&f), NULL);
682+
}
683+
END_TEST
684+
608685
START_TEST(test_queue_insert_len_gt_space)
609686
{
610687
struct queue q;
@@ -4510,6 +4587,56 @@ START_TEST(test_sock_recvfrom_icmp_short_addrlen)
45104587
}
45114588
END_TEST
45124589

4590+
START_TEST(test_sock_recvfrom_udp_fifo_alignment)
4591+
{
4592+
struct wolfIP s;
4593+
int udp_sd;
4594+
struct tsocket *ts;
4595+
struct wolfIP_sockaddr_in from;
4596+
socklen_t from_len;
4597+
uint8_t buf[16];
4598+
uint8_t payload1[3] = {0xA1, 0xA2, 0xA3};
4599+
uint8_t payload2[5] = {0xB1, 0xB2, 0xB3, 0xB4, 0xB5};
4600+
uint8_t payload3[7] = {0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7};
4601+
int ret;
4602+
4603+
wolfIP_init(&s);
4604+
mock_link_init(&s);
4605+
4606+
udp_sd = wolfIP_sock_socket(&s, AF_INET, IPSTACK_SOCK_DGRAM, WI_IPPROTO_UDP);
4607+
ck_assert_int_gt(udp_sd, 0);
4608+
ts = &s.udpsockets[SOCKET_UNMARK(udp_sd)];
4609+
ts->src_port = 1234;
4610+
4611+
enqueue_udp_rx(ts, payload1, sizeof(payload1), 1111);
4612+
enqueue_udp_rx(ts, payload2, sizeof(payload2), 2222);
4613+
enqueue_udp_rx(ts, payload3, sizeof(payload3), 3333);
4614+
4615+
memset(buf, 0, sizeof(buf));
4616+
ret = wolfIP_sock_recv(&s, udp_sd, buf, sizeof(buf), 0);
4617+
ck_assert_int_eq(ret, (int)sizeof(payload1));
4618+
ck_assert_mem_eq(buf, payload1, sizeof(payload1));
4619+
4620+
memset(&from, 0, sizeof(from));
4621+
from_len = sizeof(from);
4622+
memset(buf, 0, sizeof(buf));
4623+
ret = wolfIP_sock_recvfrom(&s, udp_sd, buf, sizeof(buf), 0,
4624+
(struct wolfIP_sockaddr *)&from, &from_len);
4625+
ck_assert_int_eq(ret, (int)sizeof(payload2));
4626+
ck_assert_mem_eq(buf, payload2, sizeof(payload2));
4627+
ck_assert_uint_eq(from.sin_port, ee16(2222));
4628+
ck_assert_uint_eq(from_len, sizeof(from));
4629+
4630+
memset(buf, 0, sizeof(buf));
4631+
ret = wolfIP_sock_recv(&s, udp_sd, buf, sizeof(buf), 0);
4632+
ck_assert_int_eq(ret, (int)sizeof(payload3));
4633+
ck_assert_mem_eq(buf, payload3, sizeof(payload3));
4634+
4635+
ret = wolfIP_sock_recv(&s, udp_sd, buf, sizeof(buf), 0);
4636+
ck_assert_int_eq(ret, -WOLFIP_EAGAIN);
4637+
}
4638+
END_TEST
4639+
45134640
START_TEST(test_sock_recvfrom_udp_payload_too_long)
45144641
{
45154642
struct wolfIP s;
@@ -11513,6 +11640,14 @@ Suite *wolf_suite(void)
1151311640
suite_add_tcase(s, tc_core);
1151411641
tcase_add_test(tc_core, test_fifo_pop_no_wrap_when_space_available);
1151511642
suite_add_tcase(s, tc_core);
11643+
tcase_add_test(tc_core, test_fifo_peek_empty_unaligned_tail);
11644+
suite_add_tcase(s, tc_core);
11645+
tcase_add_test(tc_core, test_fifo_len_empty_unaligned_tail);
11646+
suite_add_tcase(s, tc_core);
11647+
tcase_add_test(tc_core, test_fifo_pop_empty_unaligned_tail);
11648+
suite_add_tcase(s, tc_core);
11649+
tcase_add_test(tc_core, test_fifo_push_pop_odd_sizes_drains_cleanly);
11650+
suite_add_tcase(s, tc_core);
1151611651
suite_add_tcase(s, tc_utils);
1151711652
suite_add_tcase(s, tc_proto);
1151811653

@@ -11705,6 +11840,8 @@ Suite *wolf_suite(void)
1170511840
suite_add_tcase(s, tc_utils);
1170611841
tcase_add_test(tc_utils, test_sock_recvfrom_icmp_short_addrlen);
1170711842
suite_add_tcase(s, tc_utils);
11843+
tcase_add_test(tc_utils, test_sock_recvfrom_udp_fifo_alignment);
11844+
suite_add_tcase(s, tc_utils);
1170811845
tcase_add_test(tc_utils, test_sock_bind_non_local_ip_fails);
1170911846
suite_add_tcase(s, tc_utils);
1171011847
tcase_add_test(tc_utils, test_sock_connect_bad_addrlen);

src/wolfip.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ static inline uint32_t fifo_align_head_pos(uint32_t head, uint32_t size)
142142

143143
static inline void fifo_align_tail(struct fifo *f)
144144
{
145+
if (f->tail == f->head)
146+
return;
145147
if (f->tail % 4)
146148
f->tail += 4 - (f->tail % 4);
147149
if (f->h_wrap && f->tail >= f->h_wrap) {

0 commit comments

Comments
 (0)