Skip to content

Commit 77b90a7

Browse files
author
Xun Gu
committed
basic04-pinning-maps: Implementation of unloading program and unpinning map
Signed-off-by: Xun Gu <xugu@redhat.com>
1 parent 004feab commit 77b90a7

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

basic-solutions/xdp_loader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int pin_maps_in_bpf_object(struct bpf_object *bpf_obj, struct config *cfg)
8484
}
8585

8686
/* Existing/previous XDP prog might not have cleaned up */
87-
if (access(map_filename, F_OK ) != -1 ) {
87+
if (access(map_filename, F_OK) != -1 ) {
8888
if (verbose)
8989
printf(" - Unpinning (remove) prev maps in %s/\n",
9090
cfg->pin_dir);

basic04-pinning-maps/xdp_loader.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,41 @@ int main(int argc, char **argv)
134134
return EXIT_FAIL_OPTION;
135135
}
136136
if (cfg.do_unload) {
137-
/* TODO: Miss unpin of maps on unload */
138-
/* return xdp_link_detach(cfg.ifindex, cfg.xdp_flags, 0); */
137+
/* unpin the maps */
138+
char map_filename[PATH_MAX];
139+
int len;
140+
141+
len = snprintf(map_filename, PATH_MAX, "%s/%s/%s", pin_basedir,
142+
cfg.ifname, map_name);
143+
if (len < 0) {
144+
fprintf(stderr, "ERR: creating map filename for unload\n");
145+
return EXIT_FAIL_OPTION;
146+
}
147+
148+
/* Check if the map file exists and unpin it */
149+
if (access(map_filename, F_OK) == 0) {
150+
if (verbose)
151+
printf(" - Unpinning map %s\n", map_filename);
152+
153+
/* Use unlink to remove the pinned map file */
154+
err = unlink(map_filename);
155+
if (err) {
156+
fprintf(stderr, "ERR: Failed to unpin map %s: %s\n",
157+
map_filename, strerror(errno));
158+
}
159+
}
160+
161+
/* unload the program */
162+
err = do_unload(&cfg);
163+
if (err) {
164+
char errmsg[1024];
165+
libxdp_strerror(err, errmsg, sizeof(errmsg));
166+
fprintf(stderr, "Couldn't unload XDP program: %s\n", errmsg);
167+
return err;
168+
}
169+
170+
printf("Success: Unloaded XDP program\n");
171+
return EXIT_OK;
139172
}
140173

141174
program = load_bpf_and_xdp_attach(&cfg);

0 commit comments

Comments
 (0)