Skip to content

Commit 1795ab5

Browse files
msuneaanm
authored andcommitted
bpf/test: more cleanups on IPv6 NDP test
Create an auxiliary function that encapsulates the return code checks, reducing drastically the number of lines. Signed-off-by: Marc Suñé <marc.sune@isovalent.com>
1 parent e5e568a commit 1795ab5

1 file changed

Lines changed: 28 additions & 96 deletions

File tree

bpf/tests/ipv6_ndp_from_netdev_test.c

Lines changed: 28 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ struct test_args {
4949
} __packed icmp_opt;
5050
};
5151

52+
/*
53+
* Generic
54+
*/
55+
static __always_inline
56+
bool __check_ret_code(const struct __ctx_buff *ctx, const __u32 exp_rc)
57+
{
58+
void *data;
59+
void *data_end;
60+
__u32 *status_code;
61+
62+
data = (void *)(long)ctx->data;
63+
data_end = (void *)(long)ctx->data_end;
64+
65+
if (data + sizeof(*status_code) > data_end)
66+
return false;
67+
68+
status_code = data;
69+
return *status_code == exp_rc;
70+
}
71+
5272
/*
5373
* These tests make sure that ND packets directed to a Pod IP are answered
5474
* directly from BPF.
@@ -86,20 +106,9 @@ int ipv6_from_netdev_ns_pod_setup(struct __ctx_buff *ctx)
86106
CHECK("tc", "011_ipv6_from_netdev_ns_pod")
87107
int ipv6_from_netdev_ns_pod_check(const struct __ctx_buff *ctx)
88108
{
89-
void *data;
90-
void *data_end;
91-
__u32 *status_code;
92-
93109
test_init();
94110

95-
data = (void *)(long)ctx->data;
96-
data_end = (void *)(long)ctx->data_end;
97-
98-
if (data + sizeof(*status_code) > data_end)
99-
test_fatal("status code out of bounds");
100-
101-
status_code = data;
102-
assert(*status_code == CTX_ACT_REDIRECT);
111+
assert(__check_ret_code(ctx, CTX_ACT_REDIRECT));
103112

104113
BUF_DECL(V6_NDP_POD_NA_LLOPT, v6_ndp_pod_na_llopt);
105114

@@ -135,20 +144,9 @@ int ipv6_from_netdev_ns_pod_setup_noopt(struct __ctx_buff *ctx)
135144
CHECK("tc", "011_ipv6_from_netdev_ns_pod_noopt")
136145
int ipv6_from_netdev_ns_pod_check_noopt(const struct __ctx_buff *ctx)
137146
{
138-
void *data;
139-
void *data_end;
140-
__u32 *status_code;
141-
142147
test_init();
143148

144-
data = (void *)(long)ctx->data;
145-
data_end = (void *)(long)ctx->data_end;
146-
147-
if (data + sizeof(*status_code) > data_end)
148-
test_fatal("status code out of bounds");
149-
150-
status_code = data;
151-
assert(*status_code == CTX_ACT_REDIRECT);
149+
assert(__check_ret_code(ctx, CTX_ACT_REDIRECT));
152150

153151
/* Note we always return NA with llopt */
154152
BUF_DECL(V6_NDP_POD_NA_LLOPT_NS_NOOPT, v6_ndp_pod_na_llopt);
@@ -195,20 +193,9 @@ int ipv6_from_netdev_ns_pod_setup_mcast(struct __ctx_buff *ctx)
195193
CHECK("tc", "012_ipv6_from_netdev_ns_pod_mcast")
196194
int ipv6_from_netdev_ns_pod_check_mcast(const struct __ctx_buff *ctx)
197195
{
198-
void *data;
199-
void *data_end;
200-
__u32 *status_code;
201-
202196
test_init();
203197

204-
data = (void *)(long)ctx->data;
205-
data_end = (void *)(long)ctx->data_end;
206-
207-
if (data + sizeof(*status_code) > data_end)
208-
test_fatal("status code out of bounds");
209-
210-
status_code = data;
211-
assert(*status_code == CTX_ACT_REDIRECT);
198+
assert(__check_ret_code(ctx, CTX_ACT_REDIRECT));
212199

213200
/* Note we always return NA with llopt */
214201
BUF_DECL(V6_NDP_POD_NA_MCAST_NS_NOOPT, v6_ndp_pod_na_llopt);
@@ -244,20 +231,9 @@ int ipv6_from_netdev_ns_pod_setup_mcast_noopt(struct __ctx_buff *ctx)
244231
CHECK("tc", "012_ipv6_from_netdev_ns_pod_mcast_noopt")
245232
int ipv6_from_netdev_ns_pod_check_mcast_noopt(const struct __ctx_buff *ctx)
246233
{
247-
void *data;
248-
void *data_end;
249-
__u32 *status_code;
250-
251234
test_init();
252235

253-
data = (void *)(long)ctx->data;
254-
data_end = (void *)(long)ctx->data_end;
255-
256-
if (data + sizeof(*status_code) > data_end)
257-
test_fatal("status code out of bounds");
258-
259-
status_code = data;
260-
assert(*status_code == CTX_ACT_REDIRECT);
236+
assert(__check_ret_code(ctx, CTX_ACT_REDIRECT));
261237

262238
/* Note we always return NA with llopt */
263239
BUF_DECL(V6_NDP_POD_NA_MCAST_LLOPT, v6_ndp_pod_na_llopt);
@@ -310,20 +286,9 @@ int ipv6_from_netdev_ns_node_ip_setup(struct __ctx_buff *ctx)
310286
CHECK("tc", "0211_ipv6_from_netdev_ns_node_ip")
311287
int ipv6_from_netdev_ns_node_ip_check(const struct __ctx_buff *ctx)
312288
{
313-
void *data;
314-
void *data_end;
315-
__u32 *status_code;
316-
317289
test_init();
318290

319-
data = (void *)(long)ctx->data;
320-
data_end = (void *)(long)ctx->data_end;
321-
322-
if (data + sizeof(*status_code) > data_end)
323-
test_fatal("status code out of bounds");
324-
325-
status_code = data;
326-
assert(*status_code == CTX_ACT_OK);
291+
assert(__check_ret_code(ctx, CTX_ACT_OK));
327292

328293
/* Packet should not be modified */
329294
BUF_DECL(V6_NDP_NODE_NS_LLOPT_PASS, v6_ndp_node_ns_llopt);
@@ -361,20 +326,9 @@ int ipv6_from_netdev_ns_node_ip_setup_noopt(struct __ctx_buff *ctx)
361326
CHECK("tc", "0212_ipv6_from_netdev_ns_node_ip_noopt")
362327
int ipv6_from_netdev_ns_node_ip_check_noopt(const struct __ctx_buff *ctx)
363328
{
364-
void *data;
365-
void *data_end;
366-
__u32 *status_code;
367-
368329
test_init();
369330

370-
data = (void *)(long)ctx->data;
371-
data_end = (void *)(long)ctx->data_end;
372-
373-
if (data + sizeof(*status_code) > data_end)
374-
test_fatal("status code out of bounds");
375-
376-
status_code = data;
377-
assert(*status_code == CTX_ACT_OK);
331+
assert(__check_ret_code(ctx, CTX_ACT_OK));
378332

379333
/* Packet should not be modified */
380334
BUF_DECL(V6_NDP_NODE_NS_PASS, v6_ndp_node_ns);
@@ -422,20 +376,9 @@ int ipv6_from_netdev_ns_node_ip_setup_mcast(struct __ctx_buff *ctx)
422376
CHECK("tc", "022_ipv6_from_netdev_ns_node_ip_mcast")
423377
int ipv6_from_netdev_ns_node_ip_check_mcast(const struct __ctx_buff *ctx)
424378
{
425-
void *data;
426-
void *data_end;
427-
__u32 *status_code;
428-
429379
test_init();
430380

431-
data = (void *)(long)ctx->data;
432-
data_end = (void *)(long)ctx->data_end;
433-
434-
if (data + sizeof(*status_code) > data_end)
435-
test_fatal("status code out of bounds");
436-
437-
status_code = data;
438-
assert(*status_code == CTX_ACT_OK);
381+
assert(__check_ret_code(ctx, CTX_ACT_OK));
439382

440383
/* Packet should not be modified */
441384
BUF_DECL(V6_NDP_NODE_NS_MCAST_LLOPT_PASS, v6_ndp_node_ns_mcast_llopt);
@@ -472,20 +415,9 @@ int ipv6_from_netdev_ns_node_ip_setup_mcast_noopt(struct __ctx_buff *ctx)
472415
CHECK("tc", "022_ipv6_from_netdev_ns_node_ip_mcast_noopt")
473416
int ipv6_from_netdev_ns_node_ip_check_mcast_noopt(const struct __ctx_buff *ctx)
474417
{
475-
void *data;
476-
void *data_end;
477-
__u32 *status_code;
478-
479418
test_init();
480419

481-
data = (void *)(long)ctx->data;
482-
data_end = (void *)(long)ctx->data_end;
483-
484-
if (data + sizeof(*status_code) > data_end)
485-
test_fatal("status code out of bounds");
486-
487-
status_code = data;
488-
assert(*status_code == CTX_ACT_OK);
420+
assert(__check_ret_code(ctx, CTX_ACT_OK));
489421

490422
/* Packet should not be modified */
491423
BUF_DECL(V6_NDP_NODE_NS_MCAST_PASS, v6_ndp_node_ns_mcast);

0 commit comments

Comments
 (0)