18 #include "../suricata-common.h"
19 #include "../util-unittest.h"
25 static struct netmap_ring *NetmapTestRingNew(uint32_t num_slots, uint32_t buf_size)
28 offsetof(
struct netmap_ring, slot) + (size_t)num_slots *
sizeof(
struct netmap_slot);
29 size_t total = slots_end + (size_t)num_slots * buf_size;
31 struct netmap_ring *ring =
SCCalloc(1, total);
35 *(int64_t *)(uintptr_t)&ring->buf_ofs = (int64_t)slots_end;
36 *(uint32_t *)(uintptr_t)&ring->num_slots = num_slots;
37 *(uint32_t *)(uintptr_t)&ring->nr_buf_size = buf_size;
38 ring->head = ring->cur = 0;
40 ring->tail = num_slots ? num_slots - 1 : 0;
46 static void NetmapTestSetSlot(
struct netmap_ring *ring, uint32_t slot_idx, uint32_t buf_idx,
47 uint16_t
len, uint16_t
flags, uint8_t fill)
49 ring->slot[slot_idx].buf_idx = buf_idx;
50 ring->slot[slot_idx].len =
len;
51 ring->slot[slot_idx].flags =
flags;
52 memset(NETMAP_BUF(ring, buf_idx), fill,
len);
55 static void NetmapTestNtvInit(NetmapThreadVars *ntv, uint32_t coalesce_size)
57 memset(ntv, 0,
sizeof(*ntv));
58 ntv->coalesce_size = coalesce_size;
59 ntv->coalesce_buf =
SCCalloc(1, coalesce_size);
60 if (ntv->coalesce_buf == NULL)
66 static int NetmapReadSingleSlotTest(
void)
68 struct netmap_ring *ring = NetmapTestRingNew(8, 2048);
74 NetmapTestSetSlot(ring, 0, 0, 100, 0, 0xAA);
77 bool force_copy =
true;
78 FAIL_IF_NOT(NetmapReadOnePacket(ring, &ntv, &hdr, &force_copy));
81 FAIL_IF_NOT(hdr.buf == (u_char *)NETMAP_BUF(ring, 0));
91 static int NetmapReadTwoSlotAdjacentTest(
void)
93 struct netmap_ring *ring = NetmapTestRingNew(8, 2048);
99 NetmapTestSetSlot(ring, 0, 0, 60, NS_MOREFRAG, 0x11);
100 NetmapTestSetSlot(ring, 1, 1, 40, 0, 0x22);
102 struct nm_pkthdr hdr;
103 bool force_copy =
false;
104 FAIL_IF_NOT(NetmapReadOnePacket(ring, &ntv, &hdr, &force_copy));
108 for (uint32_t i = 0; i < 60; i++)
110 for (uint32_t i = 60; i < 100; i++)
121 static int NetmapReadTwoSlotNonAdjacentTest(
void)
123 struct netmap_ring *ring = NetmapTestRingNew(8, 2048);
125 NetmapThreadVars ntv;
129 NetmapTestSetSlot(ring, 0, 5, 60, NS_MOREFRAG, 0x33);
130 NetmapTestSetSlot(ring, 1, 2, 40, 0, 0x44);
132 struct nm_pkthdr hdr;
133 bool force_copy =
false;
134 FAIL_IF_NOT(NetmapReadOnePacket(ring, &ntv, &hdr, &force_copy));
138 for (uint32_t i = 0; i < 60; i++)
140 for (uint32_t i = 60; i < 100; i++)
151 static int NetmapReadRingWrapTest(
void)
153 struct netmap_ring *ring = NetmapTestRingNew(4, 2048);
155 NetmapThreadVars ntv;
159 NetmapTestSetSlot(ring, 3, 3, 60, NS_MOREFRAG, 0x55);
160 NetmapTestSetSlot(ring, 0, 0, 40, 0, 0x66);
161 ring->head = ring->cur = 3;
165 struct nm_pkthdr hdr;
166 bool force_copy =
false;
167 FAIL_IF_NOT(NetmapReadOnePacket(ring, &ntv, &hdr, &force_copy));
171 for (uint32_t i = 0; i < 60; i++)
173 for (uint32_t i = 60; i < 100; i++)
183 static int NetmapReadZeroLenFragTest(
void)
185 struct netmap_ring *ring = NetmapTestRingNew(8, 2048);
187 NetmapThreadVars ntv;
191 NetmapTestSetSlot(ring, 0, 0, 0, NS_MOREFRAG, 0x00);
192 NetmapTestSetSlot(ring, 1, 1, 50, 0, 0x77);
194 struct nm_pkthdr hdr;
195 bool force_copy =
false;
196 FAIL_IF_NOT(NetmapReadOnePacket(ring, &ntv, &hdr, &force_copy));
199 for (uint32_t i = 0; i < 50; i++)
209 static int NetmapReadOverSizeTest(
void)
211 struct netmap_ring *ring = NetmapTestRingNew(8, 2048);
213 NetmapThreadVars ntv;
214 NetmapTestNtvInit(&ntv, 100);
217 NetmapTestSetSlot(ring, 0, 0, 60, NS_MOREFRAG, 0x11);
218 NetmapTestSetSlot(ring, 1, 1, 60, 0, 0x22);
220 struct nm_pkthdr hdr;
221 bool force_copy =
false;
222 FAIL_IF(NetmapReadOnePacket(ring, &ntv, &hdr, &force_copy));
233 static int NetmapReadOverSizeTrailingFragTest(
void)
235 struct netmap_ring *ring = NetmapTestRingNew(8, 2048);
237 NetmapThreadVars ntv;
238 NetmapTestNtvInit(&ntv, 100);
241 NetmapTestSetSlot(ring, 0, 0, 60, NS_MOREFRAG, 0x11);
242 NetmapTestSetSlot(ring, 1, 1, 60, NS_MOREFRAG, 0x22);
243 NetmapTestSetSlot(ring, 2, 2, 20, 0, 0x33);
245 struct nm_pkthdr hdr;
246 bool force_copy =
false;
247 FAIL_IF(NetmapReadOnePacket(ring, &ntv, &hdr, &force_copy));
258 static int NetmapReadTooManyFragsTest(
void)
260 struct netmap_ring *ring = NetmapTestRingNew(8, 64);
262 NetmapThreadVars ntv;
266 for (uint32_t k = 0; k < 5; k++)
267 NetmapTestSetSlot(ring, k, k, 10, NS_MOREFRAG, (uint8_t)(0x10 + k));
268 ring->head = ring->cur = 0;
271 struct nm_pkthdr hdr;
272 bool force_copy =
false;
273 FAIL_IF(NetmapReadOnePacket(ring, &ntv, &hdr, &force_copy));
282 static void NetmapRegisterTests(
void)
284 UtRegisterTest(
"NetmapReadSingleSlotTest", NetmapReadSingleSlotTest);
285 UtRegisterTest(
"NetmapReadTwoSlotAdjacentTest", NetmapReadTwoSlotAdjacentTest);
286 UtRegisterTest(
"NetmapReadTwoSlotNonAdjacentTest", NetmapReadTwoSlotNonAdjacentTest);
288 UtRegisterTest(
"NetmapReadZeroLenFragTest", NetmapReadZeroLenFragTest);
290 UtRegisterTest(
"NetmapReadOverSizeTrailingFragTest", NetmapReadOverSizeTrailingFragTest);
291 UtRegisterTest(
"NetmapReadTooManyFragsTest", NetmapReadTooManyFragsTest);