Add support for vrf helper
Subsystems (3)
v2 cover Add support for vrf helper
This series adds support to iproute2 to run a command against a specific VRF. The user semnatics are similar to 'ip netns'.
The 'ip vrf' subcommand supports 3 usages:
1. Run a command against a given vrf:
ip vrf exec NAME CMD
Uses the recently committed cgroup/sock BPF option. vrf directory is added to cgroup2 mount. Individual vrfs are created under it. BPF filter is attached to vrf/NAME cgroup2 to set sk_bound_dev_if to the device index of the VRF. From there the current process (ip's pid) is addded to the cgroups.proc file and the given command is exected. In doing so all AF_INET/AF_INET6 (ipv4/ipv6) sockets are automatically bound to the VRF domain.
The association is inherited parent to child allowing the command to be a shell from which other commands are run relative to the VRF.
2. Show the VRF a process is bound to:
ip vrf id
This command essentially looks at /proc/pid/cgroup for a "::/vrf/" entry.
3. Show process ids bound to a VRF
ip vrf pids NAME
This command dumps the file MNT/vrf/NAME/cgroup.procs since that file shows the process ids in the particular vrf cgroup.
v2
- updated suject of patch 3 to avoid spam filters on vger
David Ahern (8):
lib bpf: Add support for BPF_PROG_ATTACH and BPF_PROG_DETACH
bpf: export bpf_prog_load
Add libbpf.h header with BPF_ macros
move cmd_exec to lib utils
Add filesystem APIs to lib
change name_is_vrf to return index
libnetlink: Add variant of rtnl_talk that does not display RTNETLINK
answers error
Introduce ip vrf command
include/bpf_util.h | 6 ++ include/libbpf.h | 184 ++++++++++++++++++++++++++++++++ include/libnetlink.h | 3 + include/utils.h | 4 + ip/Makefile | 3 +- ip/ip.c | 4 +- ip/ip_common.h | 4 +- ip/iplink_vrf.c | 29 ++++-- ip/ipnetns.c | 34 ------ ip/ipvrf.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/Makefile | 2 +- lib/bpf.c | 71 ++++++++----- lib/exec.c | 41 ++++++++ lib/fs.c | 143 +++++++++++++++++++++++++ lib/libnetlink.c | 20 +++- man/man8/ip-vrf.8 | 88 ++++++++++++++++ 16 files changed, 850 insertions(+), 75 deletions(-) create mode 100644 include/libbpf.h create mode 100644 ip/ipvrf.c create mode 100644 lib/exec.c create mode 100644 lib/fs.c create mode 100644 man/man8/ip-vrf.8
Signature · 1 line
2.1.4
PATCH v2 1/8 lib bpf: Add support for BPF_PROG_ATTACH and BPF_PROG_DETACH
For consistency with other bpf commands, the functions are named bpf_prog_attach and bpf_prog_detach. The existing bpf_prog_attach is renamed to bpf_prog_load_and_report since it calls bpf_prog_load and bpf_prog_report.
--- include/bpf_util.h | 3 +++ lib/bpf.c | 31 ++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-)
include/bpf_util.h +3 -0
@@ -75,6 +75,9 @@ int bpf_trace_pipe(void);void bpf_print_ops (FILE *f,struct rtattr *bpf_ops,__u16 len); +int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type); +int bpf_prog_detach (int target_fd,enum bpf_attach_type type); +#ifdef HAVE_ELF int bpf_send_map_fds (const char *path,const char *obj); int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,
lib/bpf.c +26 -5
@@ -850,6 +850,27 @@ int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv)return ret; } +int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type) +{ +union bpf_attr attr = { + .target_fd = target_fd, + .attach_bpf_fd = prog_fd, + .attach_type = type, + }; + +return bpf (BPF_PROG_ATTACH , &attr,sizeof (attr)); +} + +int bpf_prog_detach (int target_fd,enum bpf_attach_type type) +{ +union bpf_attr attr = { + .target_fd = target_fd, + .attach_type = type, + }; + +return bpf (BPF_PROG_DETACH , &attr,sizeof (attr)); +} + #ifdef HAVE_ELF struct bpf_elf_prog { enum bpf_prog_type type;
@@ -1262,9 +1283,9 @@ static void bpf_prog_report(int fd, const char *section,bpf_dump_error (ctx,"Verifier analysis:\n\n" ); } -static int bpf_prog_attach (const char *section, -const struct bpf_elf_prog *prog, -struct bpf_elf_ctx *ctx) +static int bpf_prog_load_and_report (const char *section, +const struct bpf_elf_prog *prog, +struct bpf_elf_ctx *ctx) {int tries =0 , fd; retry:
@@ -1656,7 +1677,7 @@ static int bpf_fetch_prog(struct bpf_elf_ctx *ctx, const char *section, prog.size = data.sec_data->d_size; prog.license = ctx->license; - fd =bpf_prog_attach (section, &prog, ctx); + fd =bpf_prog_load_and_report (section, &prog, ctx);if (fd <0 )return fd;
@@ -1755,7 +1776,7 @@ static int bpf_fetch_prog_relo(struct bpf_elf_ctx *ctx, const char *section, prog.size = data_insn.sec_data->d_size; prog.license = ctx->license; - fd =bpf_prog_attach (section, &prog, ctx); + fd =bpf_prog_load_and_report (section, &prog, ctx);if (fd <0 ) { *lderr = true;return fd;
Signature · 1 line
2.1.4
Re: [iproute2 net-next 1/8] lib bpf: Add support for BPF_PROG_ATTACH and BPF_PROG_DETACH
Quoted 30 lines · David Ahern
For consistency with other bpf commands, the functions are named
bpf_prog_attachandbpf_prog_detach. The existingbpf_prog_attachis renamed tobpf_prog_load_and_reportsince it callsbpf_prog_loadandbpf_prog_report.Signed-off-by David Ahern <[email protected]>--- include/bpf_util.h | 3 +++ lib/bpf.c | 31 ++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-)include/bpf_util.h +3
-0@@ -75,6 +75,9 @@ int bpf_trace_pipe(void);void bpf_print_ops (FILE *f,struct rtattr *bpf_ops,__u16 len); +int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type); +int bpf_prog_detach (int target_fd,enum bpf_attach_type type); +#ifdef HAVE_ELF int bpf_send_map_fds (const char *path,const char *obj); int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,lib/bpf.c +0
-0
lib/bpf.c int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv) · line 859
Quoted 10 lines
lib/bpf.c +7
-0@@ -850,6 +850,27 @@ int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv)return ret; } +int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type) +{ +union bpf_attr attr = { + .target_fd = target_fd, + .attach_bpf_fd = prog_fd, + .attach_type = type, + };
Please make this consistent with the other bpf(2) cmds we have in the current lib code. There were some gcc issues in the past, see:
F.e. bpf_map_create() currently looks like:
union bpf_attr attr = {};
attr.map_type = type;
attr.key_size = size_key;
attr.value_size = size_value;
attr.max_entries = max_elem;
attr.map_flags = flags;lib/bpf.c int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv) · line 869
Quoted 9 lines
lib/bpf.c +9
-0+return bpf (BPF_PROG_ATTACH , &attr,sizeof (attr)); +} + +int bpf_prog_detach (int target_fd,enum bpf_attach_type type) +{ +union bpf_attr attr = { + .target_fd = target_fd, + .attach_type = type, + };
Ditto.
Quoted 7 lines
lib/bpf.c +3
-0+return bpf (BPF_PROG_DETACH , &attr,sizeof (attr)); +} + #ifdef HAVE_ELF struct bpf_elf_prog { enum bpf_prog_type type;
lib/bpf.c static void bpf_prog_report(int fd, const char *section, · line 1289
Quoted 10 lines
lib/bpf.c +3
-3@@ -1262,9 +1283,9 @@ static void bpf_prog_report(int fd, const char *section,bpf_dump_error (ctx,"Verifier analysis:\n\n" ); } -static int bpf_prog_attach (const char *section, -const struct bpf_elf_prog *prog, -struct bpf_elf_ctx *ctx) +static int bpf_prog_load_and_report (const char *section, +const struct bpf_elf_prog *prog, +struct bpf_elf_ctx *ctx) {
Please name it bpf_prog_create() then, it would be consistent to bpf_map_create() and shorter as well.
Quoted 21 lines
lib/bpf.c +2
-2int tries =0 , fd; retry:@@ -1656,7 +1677,7 @@ static int bpf_fetch_prog(struct bpf_elf_ctx *ctx, const char *section, prog.size = data.sec_data->d_size; prog.license = ctx->license; - fd =bpf_prog_attach (section, &prog, ctx); + fd =bpf_prog_load_and_report (section, &prog, ctx);if (fd <0 )return fd;@@ -1755,7 +1776,7 @@ static int bpf_fetch_prog_relo(struct bpf_elf_ctx *ctx, const char *section, prog.size = data_insn.sec_data->d_size; prog.license = ctx->license; - fd =bpf_prog_attach (section, &prog, ctx); + fd =bpf_prog_load_and_report (section, &prog, ctx);if (fd <0 ) { *lderr = true;return fd;
Re: [iproute2 net-next 1/8] lib bpf: Add support for BPF_PROG_ATTACH and BPF_PROG_DETACH
Quoted 80 lines · Daniel Borkmann
Quoted 30 lines · David Ahern
For consistency with other bpf commands, the functions are named
bpf_prog_attachandbpf_prog_detach. The existingbpf_prog_attachis renamed tobpf_prog_load_and_reportsince it callsbpf_prog_loadandbpf_prog_report.Signed-off-by David Ahern <[email protected]>--- include/bpf_util.h | 3 +++ lib/bpf.c | 31 ++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-)include/bpf_util.h +3
-0@@ -75,6 +75,9 @@ int bpf_trace_pipe(void);void bpf_print_ops (FILE *f,struct rtattr *bpf_ops,__u16 len); +int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type); +int bpf_prog_detach (int target_fd,enum bpf_attach_type type); +#ifdef HAVE_ELF int bpf_send_map_fds (const char *path,const char *obj); int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,lib/bpf.c +0
-0lib/bpf.c int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv) · line 859
Quoted 10 lines
lib/bpf.c +7
-0@@ -850,6 +850,27 @@ int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv)return ret; } +int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type) +{ +union bpf_attr attr = { + .target_fd = target_fd, + .attach_bpf_fd = prog_fd, + .attach_type = type, + };Please make this consistent with the other bpf(2) cmds we have in the current lib code. There were some gcc issues in the past, see:
F.e.
bpf_map_create()currently looks like:union bpf_attr attr = {}; attr.map_type = type; attr.key_size = size_key; attr.value_size = size_value; attr.max_entries = max_elem; attr.map_flags = flags;lib/bpf.c int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv) · line 869
Quoted 9 lines
lib/bpf.c +9
-0+return bpf (BPF_PROG_ATTACH , &attr,sizeof (attr)); +} + +int bpf_prog_detach (int target_fd,enum bpf_attach_type type) +{ +union bpf_attr attr = { + .target_fd = target_fd, + .attach_type = type, + };Ditto.
Quoted 10 lines
lib/bpf.c +3
-0+return bpf (BPF_PROG_DETACH , &attr,sizeof (attr)); +} + #ifdef HAVE_ELF struct bpf_elf_prog { enum bpf_prog_type type;@@ -1262,9 +1283,9 @@ static void bpf_prog_report(int fd, const char *section,bpf_dump_error (ctx,"Verifier analysis:\n\n" ); }
Quoted 10 lines
lib/bpf.c static void bpf_prog_report(int fd, const char *section, · line 1289
Quoted 7 lines
lib/bpf.c +3
-3-static int bpf_prog_attach (const char *section, -const struct bpf_elf_prog *prog, -struct bpf_elf_ctx *ctx) +static int bpf_prog_load_and_report (const char *section, +const struct bpf_elf_prog *prog, +struct bpf_elf_ctx *ctx) {Please name it
bpf_prog_create()then, it would be consistent tobpf_map_create()and shorter as well.
Sorry, lack of coffee, scratch that.
Can't the current bpf_prog_attach() stay as is, and you name the above new functions bpf_prog_attach_fd() and bpf_prog_detach_fd()? I think that would be better.
Quoted 22 lines
Quoted 21 lines
lib/bpf.c +2
-2int tries =0 , fd; retry:@@ -1656,7 +1677,7 @@ static int bpf_fetch_prog(struct bpf_elf_ctx *ctx, const char *section, prog.size = data.sec_data->d_size; prog.license = ctx->license; - fd =bpf_prog_attach (section, &prog, ctx); + fd =bpf_prog_load_and_report (section, &prog, ctx);if (fd <0 )return fd;@@ -1755,7 +1776,7 @@ static int bpf_fetch_prog_relo(struct bpf_elf_ctx *ctx, const char *section, prog.size = data_insn.sec_data->d_size; prog.license = ctx->license; - fd =bpf_prog_attach (section, &prog, ctx); + fd =bpf_prog_load_and_report (section, &prog, ctx);if (fd <0 ) { *lderr = true;return fd;
Re: [iproute2 net-next 1/8] lib bpf: Add support for BPF_PROG_ATTACH and BPF_PROG_DETACH
Quoted 9 lines · Daniel Borkmann
Quoted 3 lines
Please name it
bpf_prog_create()then, it would be consistent tobpf_map_create()and shorter as well.Sorry, lack of coffee, scratch that.
Can't the current
bpf_prog_attach()stay as is, and you name the above new functionsbpf_prog_attach_fd()andbpf_prog_detach_fd()? I think that would be better.
ok. no concerns about consistency with libbpf in the kernel repo?
Seems like making iproute2 and the kernel version the same will allow samples and code to move between them much easier.
Re: [iproute2 net-next 1/8] lib bpf: Add support for BPF_PROG_ATTACH and BPF_PROG_DETACH
Quoted 7 lines · David Ahern
Quoted 6 lines · Daniel Borkmann
Quoted 3 lines
Please name it
bpf_prog_create()then, it would be consistent tobpf_map_create()and shorter as well.Sorry, lack of coffee, scratch that.
Quoted 7 lines
Quoted 3 lines
Can't the current
bpf_prog_attach()stay as is, and you name the above new functionsbpf_prog_attach_fd()andbpf_prog_detach_fd()? I think that would be better.ok. no concerns about consistency with libbpf in the kernel repo?
Seems like making iproute2 and the kernel version the same will allow samples and code to move between them much easier.
I think the lib/bpf.c code is quite different anyway, so I don't think it's much of a concern or even requirement to look exactly the same as the samples code (it was also never designed with such requirement). But besides that, it's also trivial enough from reading the code due to the BPF_PROG_ATTACH and BPF_PROG_DETACH anyway.
PATCH v2 2/8 bpf: export bpf_prog_load
In reply to David AhernCode move only; no functional change intended.
--- include/bpf_util.h | 3 +++ lib/bpf.c | 40 ++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 20 deletions(-)
include/bpf_util.h +3 -0
@@ -75,6 +75,9 @@ int bpf_trace_pipe(void);void bpf_print_ops (FILE *f,struct rtattr *bpf_ops,__u16 len); +int bpf_prog_load (enum bpf_prog_type type,const struct bpf_insn *insns, +size_t size_insns,const char *license,char *log, +size_t size_log);int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type);int bpf_prog_detach (int target_fd,enum bpf_attach_type type);
lib/bpf.c +20 -20
@@ -871,6 +871,26 @@ int bpf_prog_detach(int target_fd, enum bpf_attach_type type)return bpf (BPF_PROG_DETACH , &attr,sizeof (attr)); } +int bpf_prog_load (enum bpf_prog_type type,const struct bpf_insn *insns, +size_t size_insns,const char *license,char *log, +size_t size_log) +{ +union bpf_attr attr = {}; + + attr.prog_type = type; + attr.insns =bpf_ptr_to_u64 (insns); + attr.insn_cnt = size_insns /sizeof (struct bpf_insn ); + attr.license =bpf_ptr_to_u64 (license); + +if (size_log >0 ) { + attr.log_buf =bpf_ptr_to_u64 (log); + attr.log_size = size_log; + attr.log_level =1 ; + } + +return bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); +} + #ifdef HAVE_ELF struct bpf_elf_prog { enum bpf_prog_type type;
@@ -988,26 +1008,6 @@ static int bpf_map_create(enum bpf_map_type type, uint32_t size_key,return bpf (BPF_MAP_CREATE , &attr,sizeof (attr)); } -static int bpf_prog_load (enum bpf_prog_type type,const struct bpf_insn *insns, -size_t size_insns,const char *license,char *log, -size_t size_log) -{ -union bpf_attr attr = {}; - - attr.prog_type = type; - attr.insns =bpf_ptr_to_u64 (insns); - attr.insn_cnt = size_insns /sizeof (struct bpf_insn ); - attr.license =bpf_ptr_to_u64 (license); - -if (size_log >0 ) { - attr.log_buf =bpf_ptr_to_u64 (log); - attr.log_size = size_log; - attr.log_level =1 ; - } - -return bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); -} -static int bpf_obj_pin (int fd,const char *pathname) {union bpf_attr attr = {};
Signature · 1 line
2.1.4
Re: [iproute2 net-next 2/8] bpf: export bpf_prog_load
Quoted 13 lines · David Ahern
Code move only; no functional change intended.
Signed-off-by David Ahern <[email protected]>--- include/bpf_util.h | 3 +++ lib/bpf.c | 40 ++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 20 deletions(-)include/bpf_util.h +0
-0
include/bpf_util.h int bpf_trace_pipe(void); · line 80
Quoted 6 lines
include/bpf_util.h +3
-0@@ -75,6 +75,9 @@ int bpf_trace_pipe(void);void bpf_print_ops (FILE *f,struct rtattr *bpf_ops,__u16 len); +int bpf_prog_load (enum bpf_prog_type type,const struct bpf_insn *insns, +size_t size_insns,const char *license,char *log, +size_t size_log);
Just a really minor nit: please add a newline here.
Quoted 3 lines
include/bpf_util.h +0
-0int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type);int bpf_prog_detach (int target_fd,enum bpf_attach_type type);
PATCH v2 3/8 Add libbpf.h header with BPF_ macros
In reply to David AhernBased on version in kernel repo, samples/bpf/libbpf.h
--- include/libbpf.h | 184 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 include/libbpf.h
include/libbpf.h new file +184 -0
@@ -0,0 +1,184 @@ +/* eBPF mini library */ +#ifndef __LIBBPF_H +#define __LIBBPF_H + +/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ + +#define BPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_ALU32_REG (OP ,DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ + +#define BPF_ALU64_IMM (OP ,DST ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +#define BPF_ALU32_IMM (OP ,DST ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov, dst_reg = src_reg */ + +#define BPF_MOV64_REG (DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_MOV32_REG (DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* Short form of mov, dst_reg = imm32 */ + +#define BPF_MOV64_IMM (DST ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +#define BPF_MOV32_IMM (DST ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */ +#define BPF_LD_IMM64 (DST ,IMM ) \ + BPF_LD_IMM64_RAW(DST, 0, IMM) + +#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_LD | BPF_DW | BPF_IMM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = (__u32) (IMM) }), \ + ((struct bpf_insn) { \ + .code = 0, /* zero is reserved opcode */ \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = ((__u64) (IMM)) >> 32 }) + +#ifndef BPF_PSEUDO_MAP_FD +# define BPF_PSEUDO_MAP_FD 1 +#endif + +/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */ +#define BPF_LD_MAP_FD (DST ,MAP_FD ) \ + BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD) + + +/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ + +#define BPF_LD_ABS (SIZE ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Memory load, dst_reg = *(uint *) (src_reg + off16) */ + +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = src_reg */ + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = imm32 */ + +#define BPF_ST_MEM (SIZE ,DST ,OFF ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */ + +#define BPF_JMP_REG (OP ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ + +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Raw code statement block */ + +#define BPF_RAW_INSN (CODE ,DST ,SRC ,OFF ,IMM ) \ + ((struct bpf_insn) { \ + .code = CODE, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = IMM }) + +/* Program exit */ + +#define BPF_EXIT_INSN () \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +#endif
Signature · 1 line
2.1.4
Re: [iproute2 net-next 3/8] Add libbpf.h header with BPF_ macros
Quoted 14 lines · David Ahern
Based on version in kernel repo,
samples/bpf/libbpf.hSigned-off-by David Ahern <[email protected]>--- include/libbpf.h | 184 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+)create mode 100644
include/libbpf.hinclude/libbpf.h new file +0
-0
include/libbpf.h line 3
Quoted 3 lines
include/libbpf.h new file +3
-0@@ -0,0 +1,184 @@ +/* eBPF mini library */ +#ifndef __LIBBPF_H +#define __LIBBPF_H
Creating include/libbpf.h is a bit confusing, since all the function declarations of the current bpf lib code are located in include/bpf_util.h. Please add all this there as well instead of creating a new file.
PATCH v2 4/8 move cmd_exec to lib utils
In reply to David Ahern--- include/utils.h | 2 ++ ip/ipnetns.c | 34 ---------------------------------- lib/Makefile | 2 +- lib/exec.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 lib/exec.c
include/utils.h +2 -0
@@ -256,4 +256,6 @@ char *int_to_str(int val, char *buf);int get_guid (__u64 *guid,const char *arg);int get_real_family (int rtm_type,int rtm_family); +int cmd_exec (const char *cmd,char **argv,bool do_fork); +#endif /* __UTILS_H__ */
ip/ipnetns.c +0 -34
@@ -357,40 +357,6 @@ static int netns_list(int argc, char **argv)return 0 ; } -static int cmd_exec (const char *cmd,char **argv,bool do_fork) -{ -fflush (stdout); -if (do_fork) { -int status; -pid_t pid; - - pid =fork (); -if (pid <0 ) { -perror ("fork" ); -exit (1 ); - } - -if (pid !=0 ) { -/* Parent */ -if (waitpid (pid, &status,0 ) <0 ) { -perror ("waitpid" ); -exit (1 ); - } - -if (WIFEXITED (status)) { -return WEXITSTATUS (status); - } - -exit (1 ); - } - } - -if (execvp (cmd, argv) <0 ) -fprintf (stderr,"exec of \"%s\" failed: %s\n" , - cmd,strerror (errno)); -_exit (1 ); -} -static int on_netns_exec (char *nsname,void *arg) {char **argv = arg;
lib/Makefile +1 -1
@@ -8,7 +8,7 @@ CFLAGS += -fPIC UTILOBJ = utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o \ - names.o color.o bpf.o + names.o color.o bpf.o exec.o NLOBJ=libgenl.o ll_map.o libnetlink.o
lib/exec.c new file +41 -0
41 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH v2 5/8 Add filesystem APIs to lib
In reply to David AhernAdd make_path to recursively call mkdir as needed to create a given path with the given mode.
Add find_cgroup2_mount to lookup path where cgroup2 is mounted. If it is not already mounted, cgroup2 is mounted under /var/run/cgroup2 for use by iproute2.
--- include/utils.h | 2 + lib/Makefile | 2 +- lib/fs.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 lib/fs.c
include/utils.h +2 -0
@@ -257,5 +257,7 @@ int get_guid(__u64 *guid, const char *arg);int get_real_family (int rtm_type,int rtm_family);int cmd_exec (const char *cmd,char **argv,bool do_fork); +int make_path (const char *path,mode_t mode); +char *find_cgroup2_mount (void );#endif /* __UTILS_H__ */
lib/Makefile +1 -1
@@ -8,7 +8,7 @@ CFLAGS += -fPIC UTILOBJ = utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o \ - names.o color.o bpf.o exec.o + names.o color.o bpf.o exec.o fs.o NLOBJ=libgenl.o ll_map.o libnetlink.o
lib/fs.c new file +143 -0
143 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH v2 6/8 change name_is_vrf to return index
In reply to David Ahernindex of 0 means name is not a valid vrf.
--- ip/ip_common.h | 2 +- ip/iplink_vrf.c | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-)
ip/ip_common.h +1 -1
@@ -91,7 +91,7 @@ struct link_util *get_link_kind(const char *kind);void br_dump_bridge_id (const struct ifla_bridge_id *id,char *buf,size_t len);__u32 ipvrf_get_table (const char *name); -bool name_is_vrf (const char *name); +int name_is_vrf (const char *name);#ifndef INFINITY_LIFE_TIME #define INFINITY_LIFE_TIME 0xFFFFFFFFU
ip/iplink_vrf.c +9 -6
@@ -159,7 +159,7 @@ __u32 ipvrf_get_table(const char *name)return tb_id; } -bool name_is_vrf (const char *name) +int name_is_vrf (const char *name) {struct {struct nlmsghdr n;
@@ -187,24 +187,27 @@ bool name_is_vrf(const char *name)addattr_l (&req.n,sizeof (req),IFLA_IFNAME , name,strlen (name) +1 );if (rtnl_talk (&rth, &req.n, &answer.n,sizeof (answer)) <0 ) -return false; +return 0 ; ifi =NLMSG_DATA (&answer.n); len = answer.n.nlmsg_len -NLMSG_LENGTH (sizeof (*ifi));if (len <0 ) {fprintf (stderr,"BUG: Invalid response to link query.\n" ); -return false; +return 0 ; }parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len);if (!tb[IFLA_LINKINFO ]) -return false; +return 0 ;parse_rtattr_nested (li,IFLA_INFO_MAX , tb[IFLA_LINKINFO ]);if (!li[IFLA_INFO_KIND ]) -return false; +return 0 ; + +if (strcmp (RTA_DATA (li[IFLA_INFO_KIND ]),"vrf" )) +return 0 ; -return strcmp (RTA_DATA (li[IFLA_INFO_KIND ]),"vrf" ) ==0 ; +return ifi->ifi_index; }
Signature · 1 line
2.1.4
PATCH v2 7/8 libnetlink: Add variant of rtnl_talk that does not display RTNETLINK answers error
In reply to David Aherniplink_vrf has 2 functions used to validate a user given device name is a VRF device and to return the table id. If the user string is not a device name ip commands with a vrf keyword show a confusing error
message: "RTNETLINK answers: No such device".
Add a variant of rtnl_talk that does not display the "RTNETLINK answers" message and update iplink_vrf to use it.
--- include/libnetlink.h | 3 +++ ip/iplink_vrf.c | 14 +++++++++++--- lib/libnetlink.c | 20 +++++++++++++++++--- 3 files changed, 31 insertions(+), 6 deletions(-)
include/libnetlink.h +3 -0
@@ -81,6 +81,9 @@ int rtnl_dump_filter_nc(struct rtnl_handle *rth,int rtnl_talk (struct rtnl_handle *rtnl,struct nlmsghdr *n,struct nlmsghdr *answer,size_t len) __attribute__((warn_unused_result)); +int rtnl_talk_suppress_rtnl_errmsg (struct rtnl_handle *rtnl,struct nlmsghdr *n, +struct nlmsghdr *answer,size_t len) + __attribute__((warn_unused_result));int rtnl_send (struct rtnl_handle *rth,const void *buf,int ) __attribute__((warn_unused_result));int rtnl_send_check (struct rtnl_handle *rth,const void *buf,int )
ip/iplink_vrf.c +11 -3
@@ -13,6 +13,7 @@#include <string.h> #include <sys/socket.h> #include <linux/if_link.h> +#include <errno.h> #include "rt_names.h" #include "utils.h"
@@ -126,8 +127,14 @@ __u32 ipvrf_get_table(const char *name)addattr_l (&req.n,sizeof (req),IFLA_IFNAME , name,strlen (name) +1 ); -if (rtnl_talk (&rth, &req.n, &answer.n,sizeof (answer)) <0 ) -return 0 ; +if (rtnl_talk_suppress_rtnl_errmsg (&rth, &req.n, + &answer.n,sizeof (answer)) <0 ) { +/* special case "default" vrf to be the main table */ +if (errno ==ENODEV && !strcmp (name,"default" )) +rtnl_rttable_a2n (&tb_id,"main" ); + +return tb_id; + } ifi =NLMSG_DATA (&answer.n); len = answer.n.nlmsg_len -NLMSG_LENGTH (sizeof (*ifi));
@@ -186,7 +193,8 @@ int name_is_vrf(const char *name)addattr_l (&req.n,sizeof (req),IFLA_IFNAME , name,strlen (name) +1 ); -if (rtnl_talk (&rth, &req.n, &answer.n,sizeof (answer)) <0 ) +if (rtnl_talk_suppress_rtnl_errmsg (&rth, &req.n, + &answer.n,sizeof (answer)) <0 )return 0 ; ifi =NLMSG_DATA (&answer.n);
lib/libnetlink.c +17 -3
@@ -12,6 +12,7 @@#include <stdio.h> #include <stdlib.h> +#include <stdbool.h> #include <unistd.h> #include <syslog.h> #include <fcntl.h>
@@ -397,8 +398,9 @@ int rtnl_dump_filter_nc(struct rtnl_handle *rth,return rtnl_dump_filter_l (rth, a); } -int rtnl_talk (struct rtnl_handle *rtnl,struct nlmsghdr *n, -struct nlmsghdr *answer,size_t maxlen) +static int __rtnl_talk (struct rtnl_handle *rtnl,struct nlmsghdr *n, +struct nlmsghdr *answer,size_t maxlen, +bool show_rtnl_err) {int status;unsigned int seq;
@@ -485,7 +487,7 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,return 0 ; } -if (rtnl->proto !=NETLINK_SOCK_DIAG ) +if (rtnl->proto !=NETLINK_SOCK_DIAG && show_rtnl_err)fprintf (stderr,"RTNETLINK answers: %s\n" ,strerror (-err->error));
@@ -517,6 +519,18 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, } } +int rtnl_talk (struct rtnl_handle *rtnl,struct nlmsghdr *n, +struct nlmsghdr *answer,size_t maxlen) +{ +return __rtnl_talk (rtnl, n, answer, maxlen, true); +} + +int rtnl_talk_suppress_rtnl_errmsg (struct rtnl_handle *rtnl,struct nlmsghdr *n, +struct nlmsghdr *answer,size_t maxlen) +{ +return __rtnl_talk (rtnl, n, answer, maxlen, false); +} +int rtnl_listen_all_nsid (struct rtnl_handle *rth) {unsigned int on =1 ;
Signature · 1 line
2.1.4
PATCH v2 8/8 Introduce ip vrf command
In reply to David Ahern'ip vrf' follows the user semnatics established by 'ip netns'.
The 'ip vrf' subcommand supports 3 usages:
1. Run a command against a given vrf:
ip vrf exec NAME CMD
Uses the recently committed cgroup/sock BPF option. vrf directory is added to cgroup2 mount. Individual vrfs are created under it. BPF filter attached to vrf/NAME cgroup2 to set sk_bound_dev_if to the VRF device index. From there the current process (ip's pid) is addded to the cgroups.proc file and the given command is exected. In doing so all AF_INET/AF_INET6 (ipv4/ipv6) sockets are automatically bound to the VRF domain.
The association is inherited parent to child allowing the command to be a shell from which other commands are run relative to the VRF.
2. Show the VRF a process is bound to:
ip vrf id
This command essentially looks at /proc/pid/cgroup for a "::/vrf/" entry with the VRF name following.
3. Show process ids bound to a VRF
ip vrf pids NAME
This command dumps the file MNT/vrf/NAME/cgroup.procs since that file shows the process ids in the particular vrf cgroup.
--- ip/Makefile | 3 +- ip/ip.c | 4 +- ip/ip_common.h | 2 + ip/ipvrf.c | 289 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ man/man8/ip-vrf.8 | 88 +++++++++++++++++ 5 files changed, 384 insertions(+), 2 deletions(-) create mode 100644 ip/ipvrf.c create mode 100644 man/man8/ip-vrf.8
ip/Makefile +2 -1
@@ -7,7 +7,8 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \ iplink_vxlan.o tcp_metrics.o iplink_ipoib.o ipnetconf.o link_ip6tnl.o \ link_iptnl.o link_gre6.o iplink_bond.o iplink_bond_slave.o iplink_hsr.o \ iplink_bridge.o iplink_bridge_slave.o ipfou.o iplink_ipvlan.o \ - iplink_geneve.o iplink_vrf.o iproute_lwtunnel.o ipmacsec.o ipila.o + iplink_geneve.o iplink_vrf.o iproute_lwtunnel.o ipmacsec.o ipila.o \ + ipvrf.o RTMONOBJ=rtmon.o
ip/ip.c +3 -1
@@ -51,7 +51,8 @@ static void usage(void)" ip [ -force ] -batch filename\n" "where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |\n" " tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |\n" -" netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila }\n" +" netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |\n" +" vrf }\n" " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n" " -h[uman-readable] | -iec |\n" " -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |\n"
@@ -99,6 +100,7 @@ static const struct cmd { { "mrule", do_multirule }, { "netns", do_netns }, { "netconf", do_ipnetconf }, + { "vrf", do_ipvrf}, { "help", do_help }, { 0 } };
ip/ip_common.h +2 -0
@@ -57,6 +57,8 @@ extern int do_ipila(int argc, char **argv);int do_tcp_metrics (int argc,char **argv);int do_ipnetconf (int argc,char **argv);int do_iptoken (int argc,char **argv); +int do_ipvrf (int argc,char **argv); +int iplink_get (unsigned int flags,char *name,__u32 filt_mask);static inline int rtm_get_table (struct rtmsg *r,struct rtattr **tb)
ip/ipvrf.c new file +289 -0
289 lines not on this page · raw message
man/man8/ip-vrf.8 new file +88 -0
88 lines not on this page · raw message
Signature · 1 line
2.1.4
Quoted 57 lines · On Sat, 10 Dec 2016 12:32:06 -0800 David Ahern <[email protected]> wrote:
This series adds support to iproute2 to run a command against a specific VRF. The user semnatics are similar to 'ip netns'.
The 'ip vrf' subcommand supports 3 usages:
1. Run a command against a given vrf:
ip vrf exec NAME CMDUses the recently committed cgroup/sock BPF option. vrf directory is added to cgroup2 mount. Individual vrfs are created under it. BPF filter is attached to vrf/NAME cgroup2 to set
sk_bound_dev_ifto the device index of the VRF. From there the current process (ip's pid) is addded to the cgroups.proc file and the given command is exected. In doing so all AF_INET/AF_INET6 (ipv4/ipv6) sockets are automatically bound to the VRF domain.The association is inherited parent to child allowing the command to be a shell from which other commands are run relative to the VRF.
2. Show the VRF a process is bound to:
ip vrf id
This command essentially looks at/proc/pid/cgroupfor a "::/vrf/" entry.3. Show process ids bound to a VRF
ip vrf pids NAME
This command dumps the file MNT/vrf/NAME/cgroup.procs since that file shows the process ids in the particular vrf cgroup.v2
- updated suject of patch 3 to avoid spam filters on vgerDavid Ahern (8):
lib bpf: Add support forBPF_PROG_ATTACHandBPF_PROG_DETACH
bpf: exportbpf_prog_load
Addlibbpf.hheader withBPF_macros
movecmd_execto lib utils
Add filesystem APIs to lib
changename_is_vrfto return index
libnetlink: Add variant ofrtnl_talkthat does not display RTNETLINK
answers error
Introduce ip vrf commandinclude/bpf_util.h | 6 ++ include/libbpf.h | 184 ++++++++++++++++++++++++++++++++ include/libnetlink.h | 3 + include/utils.h | 4 + ip/Makefile | 3 +- ip/ip.c | 4 +- ip/ip_common.h | 4 +- ip/iplink_vrf.c | 29 ++++-- ip/ipnetns.c | 34 ------ ip/ipvrf.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/Makefile | 2 +- lib/bpf.c | 71 ++++++++----- lib/exec.c | 41 ++++++++ lib/fs.c | 143 +++++++++++++++++++++++++
Quoted 9 lines
lib/libnetlink.c | 20 +++- man/man8/ip-vrf.8 | 88 ++++++++++++++++ 16 files changed, 850 insertions(+), 75 deletions(-) create mode 100644 include/libbpf.h create mode 100644 ip/ipvrf.c create mode 100644 lib/exec.c create mode 100644 lib/fs.c create mode 100644 man/man8/ip-vrf.8
Please use tooling that puts v2 on all the updated patches. It makes it easier to spot them in patchwork