@@ -47,17 +47,60 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
47
47
return err ;
48
48
}
49
49
50
- static int do_detach (int idx , const char * name )
50
+ static int do_detach (int ifindex , const char * ifname , const char * app_name )
51
51
{
52
- int err ;
52
+ LIBBPF_OPTS (bpf_xdp_attach_opts , opts );
53
+ struct bpf_prog_info prog_info = {};
54
+ char prog_name [BPF_OBJ_NAME_LEN ];
55
+ __u32 info_len , curr_prog_id ;
56
+ int prog_fd ;
57
+ int err = 1 ;
58
+
59
+ if (bpf_xdp_query_id (ifindex , xdp_flags , & curr_prog_id )) {
60
+ printf ("ERROR: bpf_xdp_query_id failed (%s)\n" ,
61
+ strerror (errno ));
62
+ return err ;
63
+ }
53
64
54
- err = bpf_xdp_detach (idx , xdp_flags , NULL );
55
- if (err < 0 )
56
- printf ("ERROR: failed to detach program from %s\n" , name );
65
+ if (!curr_prog_id ) {
66
+ printf ("ERROR: flags(0x%x) xdp prog is not attached to %s\n" ,
67
+ xdp_flags , ifname );
68
+ return err ;
69
+ }
57
70
71
+ info_len = sizeof (prog_info );
72
+ prog_fd = bpf_prog_get_fd_by_id (curr_prog_id );
73
+ if (prog_fd < 0 ) {
74
+ printf ("ERROR: bpf_prog_get_fd_by_id failed (%s)\n" ,
75
+ strerror (errno ));
76
+ return prog_fd ;
77
+ }
78
+
79
+ err = bpf_obj_get_info_by_fd (prog_fd , & prog_info , & info_len );
80
+ if (err ) {
81
+ printf ("ERROR: bpf_obj_get_info_by_fd failed (%s)\n" ,
82
+ strerror (errno ));
83
+ goto close_out ;
84
+ }
85
+ snprintf (prog_name , sizeof (prog_name ), "%s_prog" , app_name );
86
+ prog_name [BPF_OBJ_NAME_LEN - 1 ] = '\0' ;
87
+
88
+ if (strcmp (prog_info .name , prog_name )) {
89
+ printf ("ERROR: %s isn't attached to %s\n" , app_name , ifname );
90
+ err = 1 ;
91
+ goto close_out ;
92
+ }
93
+
94
+ opts .old_prog_fd = prog_fd ;
95
+ err = bpf_xdp_detach (ifindex , xdp_flags , & opts );
96
+ if (err < 0 )
97
+ printf ("ERROR: failed to detach program from %s (%s)\n" ,
98
+ ifname , strerror (errno ));
58
99
/* TODO: Remember to cleanup map, when adding use of shared map
59
100
* bpf_map_delete_elem((map_fd, &idx);
60
101
*/
102
+ close_out :
103
+ close (prog_fd );
61
104
return err ;
62
105
}
63
106
@@ -169,7 +212,7 @@ int main(int argc, char **argv)
169
212
return 1 ;
170
213
}
171
214
if (!attach ) {
172
- err = do_detach (idx , argv [i ]);
215
+ err = do_detach (idx , argv [i ], prog_name );
173
216
if (err )
174
217
ret = err ;
175
218
} else {
0 commit comments