-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bpftool-show-filenames-of-pinned-objects'
Prashant Bhole says: ==================== tools: bpftool: show filenames of pinned objects This patchset adds support to show pinned objects in object details. Patch1 adds a funtionality to open a path in bpf-fs regardless of its object type. Patch2 adds actual functionality by scanning the bpf-fs once and adding object information in hash table, with object id as a key. One object may be associated with multiple paths because an object can be pinned multiple times Patch3 adds command line option to enable this functionality. Making it optional because scanning bpf-fs can be costly. ==================== Acked-by: Jakub Kicinski <[email protected]>
- Loading branch information
Showing
7 changed files
with
187 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,9 @@ | |
/* Author: Jakub Kicinski <[email protected]> */ | ||
|
||
#include <errno.h> | ||
#include <fts.h> | ||
#include <libgen.h> | ||
#include <mntent.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
@@ -122,9 +124,8 @@ static int mnt_bpffs(const char *target, char *buff, size_t bufflen) | |
return 0; | ||
} | ||
|
||
int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type) | ||
int open_obj_pinned(char *path) | ||
{ | ||
enum bpf_obj_type type; | ||
int fd; | ||
|
||
fd = bpf_obj_get(path); | ||
|
@@ -136,6 +137,18 @@ int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type) | |
return -1; | ||
} | ||
|
||
return fd; | ||
} | ||
|
||
int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type) | ||
{ | ||
enum bpf_obj_type type; | ||
int fd; | ||
|
||
fd = open_obj_pinned(path); | ||
if (fd < 0) | ||
return -1; | ||
|
||
type = get_fd_type(fd); | ||
if (type < 0) { | ||
close(fd); | ||
|
@@ -310,3 +323,83 @@ void print_hex_data_json(uint8_t *data, size_t len) | |
jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]); | ||
jsonw_end_array(json_wtr); | ||
} | ||
|
||
int build_pinned_obj_table(struct pinned_obj_table *tab, | ||
enum bpf_obj_type type) | ||
{ | ||
struct bpf_prog_info pinned_info = {}; | ||
struct pinned_obj *obj_node = NULL; | ||
__u32 len = sizeof(pinned_info); | ||
struct mntent *mntent = NULL; | ||
enum bpf_obj_type objtype; | ||
FILE *mntfile = NULL; | ||
FTSENT *ftse = NULL; | ||
FTS *fts = NULL; | ||
int fd, err; | ||
|
||
mntfile = setmntent("/proc/mounts", "r"); | ||
if (!mntfile) | ||
return -1; | ||
|
||
while ((mntent = getmntent(mntfile))) { | ||
char *path[] = { mntent->mnt_dir, NULL }; | ||
|
||
if (strncmp(mntent->mnt_type, "bpf", 3) != 0) | ||
continue; | ||
|
||
fts = fts_open(path, 0, NULL); | ||
if (!fts) | ||
continue; | ||
|
||
while ((ftse = fts_read(fts))) { | ||
if (!(ftse->fts_info & FTS_F)) | ||
continue; | ||
fd = open_obj_pinned(ftse->fts_path); | ||
if (fd < 0) | ||
continue; | ||
|
||
objtype = get_fd_type(fd); | ||
if (objtype != type) { | ||
close(fd); | ||
continue; | ||
} | ||
memset(&pinned_info, 0, sizeof(pinned_info)); | ||
err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len); | ||
if (err) { | ||
close(fd); | ||
continue; | ||
} | ||
|
||
obj_node = malloc(sizeof(*obj_node)); | ||
if (!obj_node) { | ||
close(fd); | ||
fts_close(fts); | ||
fclose(mntfile); | ||
return -1; | ||
} | ||
|
||
memset(obj_node, 0, sizeof(*obj_node)); | ||
obj_node->id = pinned_info.id; | ||
obj_node->path = strdup(ftse->fts_path); | ||
hash_add(tab->table, &obj_node->hash, obj_node->id); | ||
|
||
close(fd); | ||
} | ||
fts_close(fts); | ||
} | ||
fclose(mntfile); | ||
return 0; | ||
} | ||
|
||
void delete_pinned_obj_table(struct pinned_obj_table *tab) | ||
{ | ||
struct pinned_obj *obj; | ||
struct hlist_node *tmp; | ||
unsigned int bkt; | ||
|
||
hash_for_each_safe(tab->table, bkt, tmp, obj, hash) { | ||
hash_del(&obj->hash); | ||
free(obj->path); | ||
free(obj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters