Skip to content

Commit

Permalink
libbpf: introduce metdata flag in attributes
Browse files Browse the repository at this point in the history
This flag is needed to inform driver that metadata is used in xdp
program.

First approach (code in this patch): libbpf search for metadata casting
line in xdp program and based on this set metada flag in load attr.
This isn't clean solution. XDP program is checked line by line and only
exact match with (structure xdp_meta_generic *) will result in setting
metadata flags. It is hard to parse all different (but valid for
compiler) casting case. For example with more space between names etc.

Also there is a problem with passing the flag to ndo_bpf call. Searching
for casting can happend in loading bpf program path, but ndo_bpf call is
done in creating link on interface.

Signed-off-by: Michal Swiatkowski <michal.swiatkowski@intel.com>
  • Loading branch information
mswiatko committed Jul 21, 2021
1 parent 8ac91e6 commit a4f32ba
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/uapi/linux/bpf.h
Expand Up @@ -1308,6 +1308,8 @@ union bpf_attr {
__aligned_u64 line_info; /* line info */
__u32 line_info_cnt; /* number of bpf_line_info records */
__u32 attach_btf_id; /* in-kernel BTF type id to attach to */

__u32 metadata; /* flag to inform that metadata is used in program, related only to XDP type programs */
union {
/* valid prog_fd to attach to bpf prog */
__u32 attach_prog_fd;
Expand Down
2 changes: 2 additions & 0 deletions tools/include/uapi/linux/bpf.h
Expand Up @@ -1308,6 +1308,8 @@ union bpf_attr {
__aligned_u64 line_info; /* line info */
__u32 line_info_cnt; /* number of bpf_line_info records */
__u32 attach_btf_id; /* in-kernel BTF type id to attach to */

__u32 metadata; /* flag to inform that metadata is used in program, related only to XDP type programs */
union {
/* valid prog_fd to attach to bpf prog */
__u32 attach_prog_fd;
Expand Down
2 changes: 2 additions & 0 deletions tools/lib/bpf/bpf.c
Expand Up @@ -261,6 +261,8 @@ int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr)
attr.line_info_cnt = load_attr->line_info_cnt;
attr.line_info = ptr_to_u64(load_attr->line_info);

attr.metadata = load_attr->metadata;

if (load_attr->name)
memcpy(attr.prog_name, load_attr->name,
min(strlen(load_attr->name), (size_t)BPF_OBJ_NAME_LEN - 1));
Expand Down
25 changes: 25 additions & 0 deletions tools/lib/bpf/libbpf.c
Expand Up @@ -7063,6 +7063,28 @@ static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program
return 0;
}

/* Assume that if line have (struct xdp_meta_generic *) program for
* sure uses hints. This automatic search doesn't look good, maybe better
* solution is to allow user to decide if hints offload should be use.
* This can be done by exposing attr load bpf program flag.
* */
static bool
libbpf_is_metadata_used(struct bpf_program *prog, int btf_fd)
{
struct bpf_line_info *linfo = (struct bpf_line_info *)prog->line_info;
struct btf *btf = btf_get_from_fd(btf_fd, NULL);
const char meta_marker[] = "(struct xdp_meta_generic *)";
int i;

for (i = 0; i < prog->line_info_cnt; i++) {
if (strstr(btf__name_by_offset(btf, linfo[i].line_off),
meta_marker))
return true;
}

return false;
}

static int
load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
char *license, __u32 kern_version, int *pfd)
Expand Down Expand Up @@ -7121,6 +7143,9 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
load_attr.log_level = prog->log_level;
load_attr.prog_flags = prog->prog_flags;

if (prog->type == BPF_PROG_TYPE_XDP)
load_attr.metadata = libbpf_is_metadata_used(prog, btf_fd);

retry_load:
if (log_buf_size) {
log_buf = malloc(log_buf_size);
Expand Down
2 changes: 2 additions & 0 deletions tools/lib/bpf/libbpf_internal.h
Expand Up @@ -249,6 +249,8 @@ struct bpf_prog_load_params {
__u32 log_level;
char *log_buf;
size_t log_buf_sz;

__u32 metadata;
};

int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr);
Expand Down

0 comments on commit a4f32ba

Please sign in to comment.