aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorDavid Ahern <dsahern@gmail.com>2017-08-31 18:05:46 -0400
committerDavid S. Miller <davem@davemloft.net>2017-09-01 01:05:15 -0400
commitfa38aa17bcf31a798638d911891cd478cdea40e0 (patch)
tree0f8740c0a28fe5df698e16ff7905aca62e7d815b /samples
parentae2cf1c466bea99e927899707aed022942505d27 (diff)
samples/bpf: Update sock test to allow setting mark and priority
Update sock test to set mark and priority on socket create. Signed-off-by: David Ahern <dsahern@gmail.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/test_cgrp2_sock.c134
-rwxr-xr-xsamples/bpf/test_cgrp2_sock.sh2
2 files changed, 119 insertions, 17 deletions
diff --git a/samples/bpf/test_cgrp2_sock.c b/samples/bpf/test_cgrp2_sock.c
index c3cfb23e23b5..681abbe6c85e 100644
--- a/samples/bpf/test_cgrp2_sock.c
+++ b/samples/bpf/test_cgrp2_sock.c
@@ -19,59 +19,161 @@
19#include <errno.h> 19#include <errno.h>
20#include <fcntl.h> 20#include <fcntl.h>
21#include <net/if.h> 21#include <net/if.h>
22#include <inttypes.h>
22#include <linux/bpf.h> 23#include <linux/bpf.h>
23 24
24#include "libbpf.h" 25#include "libbpf.h"
25 26
26char bpf_log_buf[BPF_LOG_BUF_SIZE]; 27char bpf_log_buf[BPF_LOG_BUF_SIZE];
27 28
28static int prog_load(int idx) 29static int prog_load(__u32 idx, __u32 mark, __u32 prio)
29{ 30{
30 struct bpf_insn prog[] = { 31 /* save pointer to context */
32 struct bpf_insn prog_start[] = {
31 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 33 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
34 };
35 struct bpf_insn prog_end[] = {
36 BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = verdict */
37 BPF_EXIT_INSN(),
38 };
39
40 /* set sk_bound_dev_if on socket */
41 struct bpf_insn prog_dev[] = {
32 BPF_MOV64_IMM(BPF_REG_3, idx), 42 BPF_MOV64_IMM(BPF_REG_3, idx),
33 BPF_MOV64_IMM(BPF_REG_2, offsetof(struct bpf_sock, bound_dev_if)), 43 BPF_MOV64_IMM(BPF_REG_2, offsetof(struct bpf_sock, bound_dev_if)),
34 BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, offsetof(struct bpf_sock, bound_dev_if)), 44 BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, offsetof(struct bpf_sock, bound_dev_if)),
35 BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = verdict */
36 BPF_EXIT_INSN(),
37 }; 45 };
38 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
39 46
40 return bpf_load_program(BPF_PROG_TYPE_CGROUP_SOCK, prog, insns_cnt, 47 /* set mark on socket */
48 struct bpf_insn prog_mark[] = {
49 BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
50 BPF_MOV64_IMM(BPF_REG_3, mark),
51 BPF_MOV64_IMM(BPF_REG_2, offsetof(struct bpf_sock, mark)),
52 BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, offsetof(struct bpf_sock, mark)),
53 };
54
55 /* set priority on socket */
56 struct bpf_insn prog_prio[] = {
57 BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
58 BPF_MOV64_IMM(BPF_REG_3, prio),
59 BPF_MOV64_IMM(BPF_REG_2, offsetof(struct bpf_sock, priority)),
60 BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, offsetof(struct bpf_sock, priority)),
61 };
62
63 struct bpf_insn *prog;
64 size_t insns_cnt;
65 void *p;
66 int ret;
67
68 insns_cnt = sizeof(prog_start) + sizeof(prog_end);
69 if (idx)
70 insns_cnt += sizeof(prog_dev);
71
72 if (mark)
73 insns_cnt += sizeof(prog_mark);
74
75 if (prio)
76 insns_cnt += sizeof(prog_prio);
77
78 p = prog = malloc(insns_cnt);
79 if (!prog) {
80 fprintf(stderr, "Failed to allocate memory for instructions\n");
81 return EXIT_FAILURE;
82 }
83
84 memcpy(p, prog_start, sizeof(prog_start));
85 p += sizeof(prog_start);
86
87 if (idx) {
88 memcpy(p, prog_dev, sizeof(prog_dev));
89 p += sizeof(prog_dev);
90 }
91
92 if (mark) {
93 memcpy(p, prog_mark, sizeof(prog_mark));
94 p += sizeof(prog_mark);
95 }
96
97 if (prio) {
98 memcpy(p, prog_prio, sizeof(prog_prio));
99 p += sizeof(prog_prio);
100 }
101
102 memcpy(p, prog_end, sizeof(prog_end));
103 p += sizeof(prog_end);
104
105 insns_cnt /= sizeof(struct bpf_insn);
106
107 ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SOCK, prog, insns_cnt,
41 "GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE); 108 "GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE);
109
110 free(prog);
111
112 return ret;
42} 113}
43 114
44static int usage(const char *argv0) 115static int usage(const char *argv0)
45{ 116{
46 printf("Usage: %s cg-path device-index\n", argv0); 117 printf("Usage: %s -b bind-to-dev -m mark -p prio cg-path\n", argv0);
47 return EXIT_FAILURE; 118 return EXIT_FAILURE;
48} 119}
49 120
50int main(int argc, char **argv) 121int main(int argc, char **argv)
51{ 122{
123 __u32 idx = 0, mark = 0, prio = 0;
124 const char *cgrp_path = NULL;
52 int cg_fd, prog_fd, ret; 125 int cg_fd, prog_fd, ret;
53 unsigned int idx; 126 int rc;
127
128 while ((rc = getopt(argc, argv, "b:m:p:")) != -1) {
129 switch (rc) {
130 case 'b':
131 idx = if_nametoindex(optarg);
132 if (!idx) {
133 idx = strtoumax(optarg, NULL, 0);
134 if (!idx) {
135 printf("Invalid device name\n");
136 return EXIT_FAILURE;
137 }
138 }
139 break;
140 case 'm':
141 mark = strtoumax(optarg, NULL, 0);
142 break;
143 case 'p':
144 prio = strtoumax(optarg, NULL, 0);
145 break;
146 default:
147 return usage(argv[0]);
148 }
149 }
54 150
55 if (argc < 2) 151 if (optind == argc)
56 return usage(argv[0]); 152 return usage(argv[0]);
57 153
58 idx = if_nametoindex(argv[2]); 154 cgrp_path = argv[optind];
59 if (!idx) { 155 if (!cgrp_path) {
60 printf("Invalid device name\n"); 156 fprintf(stderr, "cgroup path not given\n");
61 return EXIT_FAILURE; 157 return EXIT_FAILURE;
62 } 158 }
63 159
64 cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY); 160 if (!idx && !mark && !prio) {
161 fprintf(stderr,
162 "One of device, mark or priority must be given\n");
163 return EXIT_FAILURE;
164 }
165
166 cg_fd = open(cgrp_path, O_DIRECTORY | O_RDONLY);
65 if (cg_fd < 0) { 167 if (cg_fd < 0) {
66 printf("Failed to open cgroup path: '%s'\n", strerror(errno)); 168 printf("Failed to open cgroup path: '%s'\n", strerror(errno));
67 return EXIT_FAILURE; 169 return EXIT_FAILURE;
68 } 170 }
69 171
70 prog_fd = prog_load(idx); 172 prog_fd = prog_load(idx, mark, prio);
71 printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf);
72
73 if (prog_fd < 0) { 173 if (prog_fd < 0) {
74 printf("Failed to load prog: '%s'\n", strerror(errno)); 174 printf("Failed to load prog: '%s'\n", strerror(errno));
175 printf("Output from kernel verifier:\n%s\n-------\n",
176 bpf_log_buf);
75 return EXIT_FAILURE; 177 return EXIT_FAILURE;
76 } 178 }
77 179
diff --git a/samples/bpf/test_cgrp2_sock.sh b/samples/bpf/test_cgrp2_sock.sh
index 925fd467c7cc..1153c33e8964 100755
--- a/samples/bpf/test_cgrp2_sock.sh
+++ b/samples/bpf/test_cgrp2_sock.sh
@@ -20,7 +20,7 @@ function attach_bpf {
20 mkdir -p /tmp/cgroupv2 20 mkdir -p /tmp/cgroupv2