@@ -22,11 +22,7 @@ void testcall() {
2222#define DATA_PIN_PATH "/sys/fs/bpf/data"
2323
2424/* detach programs and cleanup pinned maps */
25- void cleanup (struct bpf_link * link1 , struct bpf_link * link2 ) {
26- if (link1 )
27- bpf_link__destroy (link1 );
28- if (link2 )
29- bpf_link__destroy (link2 );
25+ void cleanup () {
3026 unlink (META_PIN_PATH );
3127 unlink (DATA_PIN_PATH );
3228}
@@ -101,9 +97,10 @@ int reuse_pinned_maps(struct bpf_object *obj) {
10197}
10298
10399int main () {
104- cleanup (NULL , NULL );
105- struct bpf_link * link1 = NULL , * link2 = NULL ;
100+ cleanup ();
101+ struct bpf_link * link1 = NULL , * link2 = NULL , * link3 = NULL ;
106102 struct bpf_object * obj2 = NULL ;
103+ struct bpf_object * obj3 = NULL ;
107104
108105 /* Step 1: Load first program and attach */
109106 if (attach_prog ("init_ll.o" , "init_ll" , & link1 ) != 0 )
@@ -112,9 +109,9 @@ int main() {
112109
113110 /* Optional: run program briefly */
114111 sleep (1 );
115- testcall ();
116- testcall ();
117- testcall ();
112+ for ( int i = 0 ; i < 16 ; ++ i ){
113+ testcall ();
114+ }
118115 sleep (1 );
119116
120117 /* Step 3: Detach first program */
@@ -126,20 +123,20 @@ int main() {
126123 obj2 = bpf_object__open_file ("lookup_ll.o" , NULL );
127124 if (libbpf_get_error (obj2 )) {
128125 fprintf (stderr , "Failed to open lookup_ll.o\n" );
129- cleanup (NULL , NULL );
126+ cleanup ();
130127 return 1 ;
131128 }
132129 /* Step 5: Reuse pinned maps */
133130 if (reuse_pinned_maps (obj2 ) != 0 ) {
134131 fprintf (stderr , "Failed to reuse pinned maps\n" );
135- cleanup (NULL , NULL );
132+ cleanup ();
136133 return 1 ;
137134 }
138135 printf ("lookup_ll will use existing maps\n" );
139136
140137 if (bpf_object__load (obj2 )) {
141138 fprintf (stderr , "Failed to load lookup_ll.o\n" );
142- cleanup (NULL , NULL );
139+ cleanup ();
143140 return 1 ;
144141 }
145142
@@ -149,17 +146,18 @@ int main() {
149146 bpf_object__find_program_by_name (obj2 , "lookup_ll" );
150147 if (!prog2 ) {
151148 fprintf (stderr , "lookup_ll program not found\n" );
152- cleanup (NULL , NULL );
149+ cleanup ();
153150 return 1 ;
154151 }
155152
156153 link2 = bpf_program__attach_tracepoint (prog2 , "syscalls" , "sys_enter_mount" );
157154 if (libbpf_get_error (link2 )) {
158155 fprintf (stderr , "Failed to attach lookup_ll\n" );
159- cleanup (NULL , NULL );
156+ cleanup ();
160157 return 1 ;
161158 }
162159 printf ("lookup_ll attached\n" );
160+ bpf_object__close (obj2 );
163161
164162 /* Run for some time */
165163 sleep (1 );
@@ -168,11 +166,58 @@ int main() {
168166 }
169167 sleep (1 );
170168
169+ bpf_link__destroy (link2 );
170+
171+ obj3 = bpf_object__open_file ("delete_ll.o" , NULL );
172+ if (libbpf_get_error (obj3 )) {
173+ fprintf (stderr , "Failed to open delete_ll.o\n" );
174+ cleanup ();
175+ return 1 ;
176+ }
177+ /* Step 5: Reuse pinned maps */
178+ if (reuse_pinned_maps (obj3 ) != 0 ) {
179+ fprintf (stderr , "Failed to reuse pinned maps\n" );
180+ cleanup ();
181+ return 1 ;
182+ }
183+ printf ("lookup_ll will use existing maps\n" );
184+
185+ if (bpf_object__load (obj3 )) {
186+ fprintf (stderr , "Failed to load delete_ll.o\n" );
187+ cleanup ();
188+ return 1 ;
189+ }
190+
191+
192+ /* Step 6: Attach second program */
193+ struct bpf_program * prog3 =
194+ bpf_object__find_program_by_name (obj3 , "delete_ll" );
195+ if (!prog3 ) {
196+ fprintf (stderr , "delete_ll program not found\n" );
197+ cleanup ();
198+ return 1 ;
199+ }
200+
201+ link3 = bpf_program__attach_tracepoint (prog3 , "syscalls" , "sys_enter_mount" );
202+ if (libbpf_get_error (link3 )) {
203+ fprintf (stderr , "Failed to attach delete_ll\n" );
204+ cleanup ();
205+ return 1 ;
206+ }
207+ printf ("delete_ll attached\n" );
208+ bpf_object__close (obj3 );
209+
210+ /* Run for some time */
211+ sleep (1 );
212+ for (int i = 0 ; i < 32 ;++ i ){
213+ testcall ();
214+ }
215+ sleep (1 );
216+
171217 /* Step 7: Detach second program and cleanup maps */
172- cleanup (NULL , link2 );
218+ cleanup ();
173219 printf ("lookup_ll detached, pinned maps removed\n" );
174220
175- bpf_object__close (obj2 );
176221
177222 return 0 ;
178223}
0 commit comments