github.com/cilium/cilium@v1.16.2/bpf/tests/drop_notify_test.c (about) 1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright Authors of Cilium */ 3 4 /* 5 * sample unit test program for some functions in drop.h 6 * It contains function definitions for testing "send_drop_notify" and "__send_drop_notify". 7 * It is used to perform unit test on the above two functions to demonstrate how 8 * to handle tailcalls. 9 * There is a tailcall at the end of function send_drop_notif which actually 10 * calls function __send_drop_notify. We can stub the tailcall and actually call 11 * the function with callback. 12 * If other functions in drop.h need to be tested, please add the function definitions at the bottom. 13 */ 14 15 #include "common.h" 16 17 #define DROP_NOTIFY 18 19 /* Include node config */ 20 #include "bpf/ctx/skb.h" 21 #include "node_config.h" 22 23 /* Include lib/metrics.h which contains the definition of tail_call_internal first to */ 24 /* avoid it to be included again in lib/drop.h. */ 25 #include "lib/metrics.h" 26 27 /* Forward declare the mock func */ 28 int mock_tail_call(void *ctx, const void *map, __u32 index); 29 30 /* Define macros like the following to make sure the original tailcall is redirected */ 31 /* to the mock tailcall function, the last 0 does not matter because we do not */ 32 /* actually use the arguments. */ 33 #define tail_call_internal(a, b, c) mock_tail_call(a, NULL, 0) 34 35 /* The file containing the functions to be tested must be included after */ 36 /* defining the above macros. */ 37 #include "lib/drop.h" 38 39 /* Undefine tail_call_internal to stop redirecting to the mock. It is not necessary */ 40 /* unless you would like to include something else that might conflict with the */ 41 /* redirection. */ 42 #undef tail_call_internal 43 44 static int __send_drop_notify_res; 45 46 /* This is the function we use as the callback when stubbing the tailcall. */ 47 int mock_tail_call(void *ctx, __maybe_unused const void *map, __maybe_unused __u32 index) 48 { 49 /* We can even unit-test the function which is actually called by the tailcall */ 50 /* within the callback. */ 51 __send_drop_notify_res = __send_drop_notify(ctx); 52 return 0; 53 } 54 55 /* A sample test for function send_drop_notify */ 56 /* It is a demo to show how we handle tailcalls. */ 57 CHECK("tc", "send_drop_notify") 58 int test_send_drop_notify(struct __ctx_buff ctx) 59 { 60 test_init(); 61 62 assert(!send_drop_notify(&ctx, 0, 0, 0, 0, 0, 0)); 63 assert(!__send_drop_notify_res); 64 65 test_finish(); 66 } 67 68 BPF_LICENSE("Dual BSD/GPL");