Skip to content

Commit a3ce278

Browse files
borkmannKernel Patches Daemon
authored andcommitted
libbpf: Add link-based API for netkit
This adds bpf_program__attach_netkit() API to libbpf. Overall it is very similar to tcx. The API looks as following: LIBBPF_API struct bpf_link * bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex, const struct bpf_netkit_opts *opts); The struct bpf_netkit_opts is done in similar way as struct bpf_tcx_opts for supporting bpf_mprog control parameters. The attach location for the primary and peer device is derived from the program section "netkit/primary" and "netkit/peer", respectively. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent f12860a commit a3ce278

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,22 @@ int bpf_link_create(int prog_fd, int target_fd,
810810
if (!OPTS_ZEROED(opts, tcx))
811811
return libbpf_err(-EINVAL);
812812
break;
813+
case BPF_NETKIT_PRIMARY:
814+
case BPF_NETKIT_PEER:
815+
relative_fd = OPTS_GET(opts, netkit.relative_fd, 0);
816+
relative_id = OPTS_GET(opts, netkit.relative_id, 0);
817+
if (relative_fd && relative_id)
818+
return libbpf_err(-EINVAL);
819+
if (relative_id) {
820+
attr.link_create.netkit.relative_id = relative_id;
821+
attr.link_create.flags |= BPF_F_ID;
822+
} else {
823+
attr.link_create.netkit.relative_fd = relative_fd;
824+
}
825+
attr.link_create.netkit.expected_revision = OPTS_GET(opts, netkit.expected_revision, 0);
826+
if (!OPTS_ZEROED(opts, netkit))
827+
return libbpf_err(-EINVAL);
828+
break;
813829
default:
814830
if (!OPTS_ZEROED(opts, flags))
815831
return libbpf_err(-EINVAL);

tools/lib/bpf/bpf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ struct bpf_link_create_opts {
415415
__u32 relative_id;
416416
__u64 expected_revision;
417417
} tcx;
418+
struct {
419+
__u32 relative_fd;
420+
__u32 relative_id;
421+
__u64 expected_revision;
422+
} netkit;
418423
};
419424
size_t :0;
420425
};

tools/lib/bpf/libbpf.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ static const char * const attach_type_name[] = {
126126
[BPF_TCX_INGRESS] = "tcx_ingress",
127127
[BPF_TCX_EGRESS] = "tcx_egress",
128128
[BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi",
129+
[BPF_NETKIT_PRIMARY] = "netkit_primary",
130+
[BPF_NETKIT_PEER] = "netkit_peer",
129131
};
130132

131133
static const char * const link_type_name[] = {
@@ -142,6 +144,7 @@ static const char * const link_type_name[] = {
142144
[BPF_LINK_TYPE_NETFILTER] = "netfilter",
143145
[BPF_LINK_TYPE_TCX] = "tcx",
144146
[BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi",
147+
[BPF_LINK_TYPE_NETKIT] = "netkit",
145148
};
146149

147150
static const char * const map_type_name[] = {
@@ -8915,6 +8918,8 @@ static const struct bpf_sec_def section_defs[] = {
89158918
SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */
89168919
SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */
89178920
SEC_DEF("action", SCHED_ACT, 0, SEC_NONE), /* deprecated / legacy, use tcx */
8921+
SEC_DEF("netkit/primary", SCHED_CLS, BPF_NETKIT_PRIMARY, SEC_NONE),
8922+
SEC_DEF("netkit/peer", SCHED_CLS, BPF_NETKIT_PEER, SEC_NONE),
89188923
SEC_DEF("tracepoint+", TRACEPOINT, 0, SEC_NONE, attach_tp),
89198924
SEC_DEF("tp+", TRACEPOINT, 0, SEC_NONE, attach_tp),
89208925
SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
@@ -12126,6 +12131,40 @@ bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
1212612131
return bpf_program_attach_fd(prog, ifindex, "tcx", &link_create_opts);
1212712132
}
1212812133

12134+
struct bpf_link *
12135+
bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
12136+
const struct bpf_netkit_opts *opts)
12137+
{
12138+
LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12139+
__u32 relative_id;
12140+
int relative_fd;
12141+
12142+
if (!OPTS_VALID(opts, bpf_netkit_opts))
12143+
return libbpf_err_ptr(-EINVAL);
12144+
12145+
relative_id = OPTS_GET(opts, relative_id, 0);
12146+
relative_fd = OPTS_GET(opts, relative_fd, 0);
12147+
12148+
/* validate we don't have unexpected combinations of non-zero fields */
12149+
if (!ifindex) {
12150+
pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12151+
prog->name);
12152+
return libbpf_err_ptr(-EINVAL);
12153+
}
12154+
if (relative_fd && relative_id) {
12155+
pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12156+
prog->name);
12157+
return libbpf_err_ptr(-EINVAL);
12158+
}
12159+
12160+
link_create_opts.netkit.expected_revision = OPTS_GET(opts, expected_revision, 0);
12161+
link_create_opts.netkit.relative_fd = relative_fd;
12162+
link_create_opts.netkit.relative_id = relative_id;
12163+
link_create_opts.flags = OPTS_GET(opts, flags, 0);
12164+
12165+
return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts);
12166+
}
12167+
1212912168
struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
1213012169
int target_fd,
1213112170
const char *attach_func_name)

tools/lib/bpf/libbpf.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,21 @@ LIBBPF_API struct bpf_link *
800800
bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
801801
const struct bpf_tcx_opts *opts);
802802

803+
struct bpf_netkit_opts {
804+
/* size of this struct, for forward/backward compatibility */
805+
size_t sz;
806+
__u32 relative_fd;
807+
__u32 relative_id;
808+
__u64 expected_revision;
809+
__u32 flags;
810+
size_t :0;
811+
};
812+
#define bpf_netkit_opts__last_field flags
813+
814+
LIBBPF_API struct bpf_link *
815+
bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
816+
const struct bpf_netkit_opts *opts);
817+
803818
struct bpf_map;
804819

805820
LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ LIBBPF_1.3.0 {
398398
bpf_object__unpin;
399399
bpf_prog_detach_opts;
400400
bpf_program__attach_netfilter;
401+
bpf_program__attach_netkit;
401402
bpf_program__attach_tcx;
402403
bpf_program__attach_uprobe_multi;
403404
ring__avail_data_size;

0 commit comments

Comments
 (0)