History

bpf: Add link-based BPF program attachment to network namespace

Extend bpf() syscall subcommands that operate on bpf_link, that is LINK_CREATE, LINK_UPDATE, OBJ_GET_INFO, to accept attach types tied to network namespaces (only flow dissector at the moment). Link-based and prog-based attachment can be used interchangeably, but only one can exist at a time. Attempts to attach a link when a prog is already attached directly, and the other way around, will be met with -EEXIST. Attempts to detach a program when link exists result in -EINVAL. Attachment of multiple links of same attach type to one netns is not supported with the intention to lift the restriction when a use-case presents itself. Because of that link create returns -E2BIG when trying to create another netns link, when one already exists. Link-based attachments to netns don't keep a netns alive by holding a ref to it. Instead links get auto-detached from netns when the latter is being destroyed, using a pernet pre_exit callback. When auto-detached, link lives in defunct state as long there are open FDs for it. -ENOLINK is returned if a user tries to update a defunct link. Because bpf_link to netns doesn't hold a ref to struct net, special care is taken when releasing, updating, or filling link info. The netns might be getting torn down when any of these link operations are in progress. That is why auto-detach and update/release/fill_info are synchronized by the same mutex. Also, link ops have to always check if auto-detach has not happened yet and if netns is still alive (refcnt > 0).

Jakub Sitnicki committed 6 years ago

Commit 7f045a41 parent b27f7bb7 files · +267 −2Browse files

Showing the changes to include/uapi/linux (1 of 7 files). Show all

Unified · Split

include/uapi/linux/bpf.h+5−0
@@ -237,6 +237,7 @@ enum bpf_link_type { · 236 unmodified lines
237 BPF_LINK_TYPE_TRACING = 2,237 BPF_LINK_TYPE_TRACING = 2,
238 BPF_LINK_TYPE_CGROUP = 3,238 BPF_LINK_TYPE_CGROUP = 3,
239 BPF_LINK_TYPE_ITER = 4,239 BPF_LINK_TYPE_ITER = 4,
240 BPF_LINK_TYPE_NETNS = 5,
240 241
241 MAX_BPF_LINK_TYPE,242 MAX_BPF_LINK_TYPE,
242};243};
@@ -3839,6 +3840,10 @@ struct bpf_link_info { · 3596 unmodified lines
3839 __u64 cgroup_id;3840 __u64 cgroup_id;
3840 __u32 attach_type;3841 __u32 attach_type;
3841 } cgroup;3842 } cgroup;
3843 struct {
3844 __u32 netns_ino;
3845 __u32 attach_type;
3846 } netns;
3842 };3847 };
3843} __attribute__((aligned(8)));3848} __attribute__((aligned(8)));
3844 3849

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t