Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <libelf.h> |
| 6 | #include <gelf.h> |
| 7 | #include <errno.h> |
| 8 | #include <unistd.h> |
| 9 | #include <string.h> |
| 10 | #include <stdbool.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 11 | #include <stdlib.h> |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 12 | #include <linux/bpf.h> |
| 13 | #include <linux/filter.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 14 | #include <linux/perf_event.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 15 | #include <linux/netlink.h> |
| 16 | #include <linux/rtnetlink.h> |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame] | 17 | #include <linux/types.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 18 | #include <sys/types.h> |
| 19 | #include <sys/socket.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 20 | #include <sys/syscall.h> |
| 21 | #include <sys/ioctl.h> |
| 22 | #include <sys/mman.h> |
| 23 | #include <poll.h> |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 24 | #include <ctype.h> |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 25 | #include <assert.h> |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 26 | #include "libbpf.h" |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 27 | #include "bpf_load.h" |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 28 | #include "perf-sys.h" |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 29 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 30 | #define DEBUGFS "/sys/kernel/debug/tracing/" |
| 31 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 32 | static char license[128]; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 33 | static int kern_version; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 34 | static bool processed_sec[128]; |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 35 | char bpf_log_buf[BPF_LOG_BUF_SIZE]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 36 | int map_fd[MAX_MAPS]; |
| 37 | int prog_fd[MAX_PROGS]; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 38 | int event_fd[MAX_PROGS]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 39 | int prog_cnt; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 40 | int prog_array_fd = -1; |
| 41 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 42 | struct bpf_map_data map_data[MAX_MAPS]; |
| 43 | int map_data_count = 0; |
| 44 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 45 | static int populate_prog_array(const char *event, int prog_fd) |
| 46 | { |
| 47 | int ind = atoi(event), err; |
| 48 | |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 49 | err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 50 | if (err < 0) { |
| 51 | printf("failed to store prog_fd in prog_array\n"); |
| 52 | return -1; |
| 53 | } |
| 54 | return 0; |
| 55 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 56 | |
| 57 | static int load_and_attach(const char *event, struct bpf_insn *prog, int size) |
| 58 | { |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 59 | bool is_socket = strncmp(event, "socket", 6) == 0; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 60 | bool is_kprobe = strncmp(event, "kprobe/", 7) == 0; |
| 61 | bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0; |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 62 | bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 63 | bool is_xdp = strncmp(event, "xdp", 3) == 0; |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 64 | bool is_perf_event = strncmp(event, "perf_event", 10) == 0; |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 65 | bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0; |
| 66 | bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 67 | bool is_sockops = strncmp(event, "sockops", 7) == 0; |
Joe Stringer | 43371c8 | 2016-12-14 14:43:39 -0800 | [diff] [blame] | 68 | size_t insns_cnt = size / sizeof(struct bpf_insn); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 69 | enum bpf_prog_type prog_type; |
| 70 | char buf[256]; |
| 71 | int fd, efd, err, id; |
| 72 | struct perf_event_attr attr = {}; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 73 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 74 | attr.type = PERF_TYPE_TRACEPOINT; |
| 75 | attr.sample_type = PERF_SAMPLE_RAW; |
| 76 | attr.sample_period = 1; |
| 77 | attr.wakeup_events = 1; |
| 78 | |
| 79 | if (is_socket) { |
| 80 | prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
| 81 | } else if (is_kprobe || is_kretprobe) { |
| 82 | prog_type = BPF_PROG_TYPE_KPROBE; |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 83 | } else if (is_tracepoint) { |
| 84 | prog_type = BPF_PROG_TYPE_TRACEPOINT; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 85 | } else if (is_xdp) { |
| 86 | prog_type = BPF_PROG_TYPE_XDP; |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 87 | } else if (is_perf_event) { |
| 88 | prog_type = BPF_PROG_TYPE_PERF_EVENT; |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 89 | } else if (is_cgroup_skb) { |
| 90 | prog_type = BPF_PROG_TYPE_CGROUP_SKB; |
| 91 | } else if (is_cgroup_sk) { |
| 92 | prog_type = BPF_PROG_TYPE_CGROUP_SOCK; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 93 | } else if (is_sockops) { |
| 94 | prog_type = BPF_PROG_TYPE_SOCK_OPS; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 95 | } else { |
| 96 | printf("Unknown event '%s'\n", event); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 97 | return -1; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 98 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 99 | |
Joe Stringer | 43371c8 | 2016-12-14 14:43:39 -0800 | [diff] [blame] | 100 | fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version, |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 101 | bpf_log_buf, BPF_LOG_BUF_SIZE); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 102 | if (fd < 0) { |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 103 | printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | prog_fd[prog_cnt++] = fd; |
| 108 | |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 109 | if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 112 | if (is_socket || is_sockops) { |
| 113 | if (is_socket) |
| 114 | event += 6; |
| 115 | else |
| 116 | event += 7; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 117 | if (*event != '/') |
| 118 | return 0; |
| 119 | event++; |
| 120 | if (!isdigit(*event)) { |
| 121 | printf("invalid prog number\n"); |
| 122 | return -1; |
| 123 | } |
| 124 | return populate_prog_array(event, fd); |
| 125 | } |
| 126 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 127 | if (is_kprobe || is_kretprobe) { |
| 128 | if (is_kprobe) |
| 129 | event += 7; |
| 130 | else |
| 131 | event += 10; |
| 132 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 133 | if (*event == 0) { |
| 134 | printf("event name cannot be empty\n"); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | if (isdigit(*event)) |
| 139 | return populate_prog_array(event, fd); |
| 140 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 141 | snprintf(buf, sizeof(buf), |
| 142 | "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events", |
| 143 | is_kprobe ? 'p' : 'r', event, event); |
| 144 | err = system(buf); |
| 145 | if (err < 0) { |
| 146 | printf("failed to create kprobe '%s' error '%s'\n", |
| 147 | event, strerror(errno)); |
| 148 | return -1; |
| 149 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 150 | |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 151 | strcpy(buf, DEBUGFS); |
| 152 | strcat(buf, "events/kprobes/"); |
| 153 | strcat(buf, event); |
| 154 | strcat(buf, "/id"); |
| 155 | } else if (is_tracepoint) { |
| 156 | event += 11; |
| 157 | |
| 158 | if (*event == 0) { |
| 159 | printf("event name cannot be empty\n"); |
| 160 | return -1; |
| 161 | } |
| 162 | strcpy(buf, DEBUGFS); |
| 163 | strcat(buf, "events/"); |
| 164 | strcat(buf, event); |
| 165 | strcat(buf, "/id"); |
| 166 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 167 | |
| 168 | efd = open(buf, O_RDONLY, 0); |
| 169 | if (efd < 0) { |
| 170 | printf("failed to open event %s\n", event); |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | err = read(efd, buf, sizeof(buf)); |
| 175 | if (err < 0 || err >= sizeof(buf)) { |
| 176 | printf("read from '%s' failed '%s'\n", event, strerror(errno)); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | close(efd); |
| 181 | |
| 182 | buf[err] = 0; |
| 183 | id = atoi(buf); |
| 184 | attr.config = id; |
| 185 | |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 186 | efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 187 | if (efd < 0) { |
| 188 | printf("event %d fd %d err %s\n", id, efd, strerror(errno)); |
| 189 | return -1; |
| 190 | } |
| 191 | event_fd[prog_cnt - 1] = efd; |
| 192 | ioctl(efd, PERF_EVENT_IOC_ENABLE, 0); |
| 193 | ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd); |
| 194 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 198 | static int load_maps(struct bpf_map_data *maps, int nr_maps, |
| 199 | fixup_map_cb fixup_map) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 200 | { |
| 201 | int i; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 202 | |
Jesper Dangaard Brouer | 5010e94 | 2017-04-28 16:25:04 +0200 | [diff] [blame] | 203 | for (i = 0; i < nr_maps; i++) { |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 204 | if (fixup_map) { |
| 205 | fixup_map(&maps[i], i); |
| 206 | /* Allow userspace to assign map FD prior to creation */ |
| 207 | if (maps[i].fd != -1) { |
| 208 | map_fd[i] = maps[i].fd; |
| 209 | continue; |
| 210 | } |
| 211 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 212 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 213 | if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
| 214 | maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 215 | int inner_map_fd = map_fd[maps[i].def.inner_map_idx]; |
Martin KaFai Lau | fb30d4b | 2017-03-22 10:00:35 -0700 | [diff] [blame] | 216 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 217 | map_fd[i] = bpf_create_map_in_map(maps[i].def.type, |
| 218 | maps[i].def.key_size, |
| 219 | inner_map_fd, |
| 220 | maps[i].def.max_entries, |
| 221 | maps[i].def.map_flags); |
Martin KaFai Lau | fb30d4b | 2017-03-22 10:00:35 -0700 | [diff] [blame] | 222 | } else { |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 223 | map_fd[i] = bpf_create_map(maps[i].def.type, |
| 224 | maps[i].def.key_size, |
| 225 | maps[i].def.value_size, |
| 226 | maps[i].def.max_entries, |
| 227 | maps[i].def.map_flags); |
Martin KaFai Lau | fb30d4b | 2017-03-22 10:00:35 -0700 | [diff] [blame] | 228 | } |
Alexei Starovoitov | 618ec9a7 | 2016-03-07 21:57:18 -0800 | [diff] [blame] | 229 | if (map_fd[i] < 0) { |
| 230 | printf("failed to create a map: %d %s\n", |
| 231 | errno, strerror(errno)); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 232 | return 1; |
Alexei Starovoitov | 618ec9a7 | 2016-03-07 21:57:18 -0800 | [diff] [blame] | 233 | } |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 234 | maps[i].fd = map_fd[i]; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 235 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 236 | if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY) |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 237 | prog_array_fd = map_fd[i]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 238 | } |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname, |
| 243 | GElf_Shdr *shdr, Elf_Data **data) |
| 244 | { |
| 245 | Elf_Scn *scn; |
| 246 | |
| 247 | scn = elf_getscn(elf, i); |
| 248 | if (!scn) |
| 249 | return 1; |
| 250 | |
| 251 | if (gelf_getshdr(scn, shdr) != shdr) |
| 252 | return 2; |
| 253 | |
| 254 | *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name); |
| 255 | if (!*shname || !shdr->sh_size) |
| 256 | return 3; |
| 257 | |
| 258 | *data = elf_getdata(scn, 0); |
| 259 | if (!*data || elf_getdata(scn, *data) != NULL) |
| 260 | return 4; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols, |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 266 | GElf_Shdr *shdr, struct bpf_insn *insn, |
| 267 | struct bpf_map_data *maps, int nr_maps) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 268 | { |
| 269 | int i, nrels; |
| 270 | |
| 271 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 272 | |
| 273 | for (i = 0; i < nrels; i++) { |
| 274 | GElf_Sym sym; |
| 275 | GElf_Rel rel; |
| 276 | unsigned int insn_idx; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 277 | bool match = false; |
| 278 | int j, map_idx; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 279 | |
| 280 | gelf_getrel(data, i, &rel); |
| 281 | |
| 282 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
| 283 | |
| 284 | gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym); |
| 285 | |
| 286 | if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { |
| 287 | printf("invalid relo for insn[%d].code 0x%x\n", |
| 288 | insn_idx, insn[insn_idx].code); |
| 289 | return 1; |
| 290 | } |
| 291 | insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 292 | |
| 293 | /* Match FD relocation against recorded map_data[] offset */ |
| 294 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
| 295 | if (maps[map_idx].elf_offset == sym.st_value) { |
| 296 | match = true; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | if (match) { |
| 301 | insn[insn_idx].imm = maps[map_idx].fd; |
| 302 | } else { |
| 303 | printf("invalid relo for insn[%d] no map_data match\n", |
| 304 | insn_idx); |
| 305 | return 1; |
| 306 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 312 | static int cmp_symbols(const void *l, const void *r) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 313 | { |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 314 | const GElf_Sym *lsym = (const GElf_Sym *)l; |
| 315 | const GElf_Sym *rsym = (const GElf_Sym *)r; |
| 316 | |
| 317 | if (lsym->st_value < rsym->st_value) |
| 318 | return -1; |
| 319 | else if (lsym->st_value > rsym->st_value) |
| 320 | return 1; |
| 321 | else |
| 322 | return 0; |
| 323 | } |
| 324 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 325 | static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx, |
| 326 | Elf *elf, Elf_Data *symbols, int strtabidx) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 327 | { |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 328 | int map_sz_elf, map_sz_copy; |
| 329 | bool validate_zero = false; |
| 330 | Elf_Data *data_maps; |
| 331 | int i, nr_maps; |
| 332 | GElf_Sym *sym; |
| 333 | Elf_Scn *scn; |
| 334 | int copy_sz; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 335 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 336 | if (maps_shndx < 0) |
| 337 | return -EINVAL; |
| 338 | if (!symbols) |
| 339 | return -EINVAL; |
| 340 | |
| 341 | /* Get data for maps section via elf index */ |
| 342 | scn = elf_getscn(elf, maps_shndx); |
| 343 | if (scn) |
| 344 | data_maps = elf_getdata(scn, NULL); |
| 345 | if (!scn || !data_maps) { |
| 346 | printf("Failed to get Elf_Data from maps section %d\n", |
| 347 | maps_shndx); |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | /* For each map get corrosponding symbol table entry */ |
| 352 | sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym)); |
| 353 | for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { |
| 354 | assert(nr_maps < MAX_MAPS+1); |
| 355 | if (!gelf_getsym(symbols, i, &sym[nr_maps])) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 356 | continue; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 357 | if (sym[nr_maps].st_shndx != maps_shndx) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 358 | continue; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 359 | /* Only increment iif maps section */ |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 360 | nr_maps++; |
| 361 | } |
| 362 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 363 | /* Align to map_fd[] order, via sort on offset in sym.st_value */ |
| 364 | qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 365 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 366 | /* Keeping compatible with ELF maps section changes |
| 367 | * ------------------------------------------------ |
| 368 | * The program size of struct bpf_map_def is known by loader |
| 369 | * code, but struct stored in ELF file can be different. |
| 370 | * |
| 371 | * Unfortunately sym[i].st_size is zero. To calculate the |
| 372 | * struct size stored in the ELF file, assume all struct have |
| 373 | * the same size, and simply divide with number of map |
| 374 | * symbols. |
| 375 | */ |
| 376 | map_sz_elf = data_maps->d_size / nr_maps; |
| 377 | map_sz_copy = sizeof(struct bpf_map_def); |
| 378 | if (map_sz_elf < map_sz_copy) { |
| 379 | /* |
| 380 | * Backward compat, loading older ELF file with |
| 381 | * smaller struct, keeping remaining bytes zero. |
| 382 | */ |
| 383 | map_sz_copy = map_sz_elf; |
| 384 | } else if (map_sz_elf > map_sz_copy) { |
| 385 | /* |
| 386 | * Forward compat, loading newer ELF file with larger |
| 387 | * struct with unknown features. Assume zero means |
| 388 | * feature not used. Thus, validate rest of struct |
| 389 | * data is zero. |
| 390 | */ |
| 391 | validate_zero = true; |
| 392 | } |
| 393 | |
| 394 | /* Memcpy relevant part of ELF maps data to loader maps */ |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 395 | for (i = 0; i < nr_maps; i++) { |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 396 | unsigned char *addr, *end; |
| 397 | struct bpf_map_def *def; |
| 398 | const char *map_name; |
| 399 | size_t offset; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 400 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 401 | map_name = elf_strptr(elf, strtabidx, sym[i].st_name); |
| 402 | maps[i].name = strdup(map_name); |
| 403 | if (!maps[i].name) { |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 404 | printf("strdup(%s): %s(%d)\n", map_name, |
| 405 | strerror(errno), errno); |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 406 | free(sym); |
| 407 | return -errno; |
| 408 | } |
| 409 | |
| 410 | /* Symbol value is offset into ELF maps section data area */ |
| 411 | offset = sym[i].st_value; |
| 412 | def = (struct bpf_map_def *)(data_maps->d_buf + offset); |
| 413 | maps[i].elf_offset = offset; |
| 414 | memset(&maps[i].def, 0, sizeof(struct bpf_map_def)); |
| 415 | memcpy(&maps[i].def, def, map_sz_copy); |
| 416 | |
| 417 | /* Verify no newer features were requested */ |
| 418 | if (validate_zero) { |
| 419 | addr = (unsigned char*) def + map_sz_copy; |
| 420 | end = (unsigned char*) def + map_sz_elf; |
| 421 | for (; addr < end; addr++) { |
| 422 | if (*addr != 0) { |
| 423 | free(sym); |
| 424 | return -EFBIG; |
| 425 | } |
| 426 | } |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 430 | free(sym); |
Jesper Dangaard Brouer | 5010e94 | 2017-04-28 16:25:04 +0200 | [diff] [blame] | 431 | return nr_maps; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map) |
| 435 | { |
| 436 | int fd, i, ret, maps_shndx = -1, strtabidx = -1; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 437 | Elf *elf; |
| 438 | GElf_Ehdr ehdr; |
| 439 | GElf_Shdr shdr, shdr_prog; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 440 | Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 441 | char *shname, *shname_prog; |
| 442 | int nr_maps = 0; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 443 | |
Mickaël Salaün | a734fb5 | 2017-02-08 21:27:43 +0100 | [diff] [blame] | 444 | /* reset global variables */ |
| 445 | kern_version = 0; |
| 446 | memset(license, 0, sizeof(license)); |
| 447 | memset(processed_sec, 0, sizeof(processed_sec)); |
| 448 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 449 | if (elf_version(EV_CURRENT) == EV_NONE) |
| 450 | return 1; |
| 451 | |
| 452 | fd = open(path, O_RDONLY, 0); |
| 453 | if (fd < 0) |
| 454 | return 1; |
| 455 | |
| 456 | elf = elf_begin(fd, ELF_C_READ, NULL); |
| 457 | |
| 458 | if (!elf) |
| 459 | return 1; |
| 460 | |
| 461 | if (gelf_getehdr(elf, &ehdr) != &ehdr) |
| 462 | return 1; |
| 463 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 464 | /* clear all kprobes */ |
| 465 | i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events"); |
| 466 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 467 | /* scan over all elf sections to get license and map info */ |
| 468 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 469 | |
| 470 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 471 | continue; |
| 472 | |
| 473 | if (0) /* helpful for llvm debugging */ |
| 474 | printf("section %d:%s data %p size %zd link %d flags %d\n", |
| 475 | i, shname, data->d_buf, data->d_size, |
| 476 | shdr.sh_link, (int) shdr.sh_flags); |
| 477 | |
| 478 | if (strcmp(shname, "license") == 0) { |
| 479 | processed_sec[i] = true; |
| 480 | memcpy(license, data->d_buf, data->d_size); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 481 | } else if (strcmp(shname, "version") == 0) { |
| 482 | processed_sec[i] = true; |
| 483 | if (data->d_size != sizeof(int)) { |
| 484 | printf("invalid size of version section %zd\n", |
| 485 | data->d_size); |
| 486 | return 1; |
| 487 | } |
| 488 | memcpy(&kern_version, data->d_buf, sizeof(int)); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 489 | } else if (strcmp(shname, "maps") == 0) { |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 490 | int j; |
| 491 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 492 | maps_shndx = i; |
| 493 | data_maps = data; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 494 | for (j = 0; j < MAX_MAPS; j++) |
| 495 | map_data[j].fd = -1; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 496 | } else if (shdr.sh_type == SHT_SYMTAB) { |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 497 | strtabidx = shdr.sh_link; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 498 | symbols = data; |
| 499 | } |
| 500 | } |
| 501 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 502 | ret = 1; |
| 503 | |
| 504 | if (!symbols) { |
| 505 | printf("missing SHT_SYMTAB section\n"); |
| 506 | goto done; |
| 507 | } |
| 508 | |
| 509 | if (data_maps) { |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 510 | nr_maps = load_elf_maps_section(map_data, maps_shndx, |
| 511 | elf, symbols, strtabidx); |
| 512 | if (nr_maps < 0) { |
| 513 | printf("Error: Failed loading ELF maps (errno:%d):%s\n", |
| 514 | nr_maps, strerror(-nr_maps)); |
Jesper Dangaard Brouer | 5010e94 | 2017-04-28 16:25:04 +0200 | [diff] [blame] | 515 | ret = 1; |
| 516 | goto done; |
| 517 | } |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 518 | if (load_maps(map_data, nr_maps, fixup_map)) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 519 | goto done; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 520 | map_data_count = nr_maps; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 521 | |
| 522 | processed_sec[maps_shndx] = true; |
| 523 | } |
| 524 | |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 525 | /* process all relo sections, and rewrite bpf insns for maps */ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 526 | for (i = 1; i < ehdr.e_shnum; i++) { |
Mickaël Salaün | 16ad132 | 2017-02-08 21:27:42 +0100 | [diff] [blame] | 527 | if (processed_sec[i]) |
| 528 | continue; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 529 | |
| 530 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 531 | continue; |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 532 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 533 | if (shdr.sh_type == SHT_REL) { |
| 534 | struct bpf_insn *insns; |
| 535 | |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 536 | /* locate prog sec that need map fixup (relocations) */ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 537 | if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog, |
| 538 | &shdr_prog, &data_prog)) |
| 539 | continue; |
| 540 | |
Alexei Starovoitov | db6a71d | 2016-11-22 16:52:09 -0800 | [diff] [blame] | 541 | if (shdr_prog.sh_type != SHT_PROGBITS || |
| 542 | !(shdr_prog.sh_flags & SHF_EXECINSTR)) |
| 543 | continue; |
| 544 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 545 | insns = (struct bpf_insn *) data_prog->d_buf; |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 546 | processed_sec[i] = true; /* relo section */ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 547 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 548 | if (parse_relo_and_apply(data, symbols, &shdr, insns, |
| 549 | map_data, nr_maps)) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 550 | continue; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 554 | /* load programs */ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 555 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 556 | |
| 557 | if (processed_sec[i]) |
| 558 | continue; |
| 559 | |
| 560 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 561 | continue; |
| 562 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 563 | if (memcmp(shname, "kprobe/", 7) == 0 || |
| 564 | memcmp(shname, "kretprobe/", 10) == 0 || |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 565 | memcmp(shname, "tracepoint/", 11) == 0 || |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 566 | memcmp(shname, "xdp", 3) == 0 || |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 567 | memcmp(shname, "perf_event", 10) == 0 || |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 568 | memcmp(shname, "socket", 6) == 0 || |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 569 | memcmp(shname, "cgroup/", 7) == 0 || |
Lawrence Brakmo | f856e46 | 2017-07-04 15:57:50 -0700 | [diff] [blame^] | 570 | memcmp(shname, "sockops", 7) == 0) { |
| 571 | ret = load_and_attach(shname, data->d_buf, |
| 572 | data->d_size); |
| 573 | if (ret != 0) |
| 574 | goto done; |
| 575 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 578 | ret = 0; |
| 579 | done: |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 580 | close(fd); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | int load_bpf_file(char *path) |
| 585 | { |
| 586 | return do_load_bpf_file(path, NULL); |
| 587 | } |
| 588 | |
| 589 | int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map) |
| 590 | { |
| 591 | return do_load_bpf_file(path, fixup_map); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 592 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 593 | |
| 594 | void read_trace_pipe(void) |
| 595 | { |
| 596 | int trace_fd; |
| 597 | |
| 598 | trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0); |
| 599 | if (trace_fd < 0) |
| 600 | return; |
| 601 | |
| 602 | while (1) { |
| 603 | static char buf[4096]; |
| 604 | ssize_t sz; |
| 605 | |
| 606 | sz = read(trace_fd, buf, sizeof(buf)); |
| 607 | if (sz > 0) { |
| 608 | buf[sz] = 0; |
| 609 | puts(buf); |
| 610 | } |
| 611 | } |
| 612 | } |
Alexei Starovoitov | 3622e7e | 2016-03-07 21:57:19 -0800 | [diff] [blame] | 613 | |
| 614 | #define MAX_SYMS 300000 |
| 615 | static struct ksym syms[MAX_SYMS]; |
| 616 | static int sym_cnt; |
| 617 | |
| 618 | static int ksym_cmp(const void *p1, const void *p2) |
| 619 | { |
| 620 | return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr; |
| 621 | } |
| 622 | |
| 623 | int load_kallsyms(void) |
| 624 | { |
| 625 | FILE *f = fopen("/proc/kallsyms", "r"); |
| 626 | char func[256], buf[256]; |
| 627 | char symbol; |
| 628 | void *addr; |
| 629 | int i = 0; |
| 630 | |
| 631 | if (!f) |
| 632 | return -ENOENT; |
| 633 | |
| 634 | while (!feof(f)) { |
| 635 | if (!fgets(buf, sizeof(buf), f)) |
| 636 | break; |
| 637 | if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3) |
| 638 | break; |
| 639 | if (!addr) |
| 640 | continue; |
| 641 | syms[i].addr = (long) addr; |
| 642 | syms[i].name = strdup(func); |
| 643 | i++; |
| 644 | } |
| 645 | sym_cnt = i; |
| 646 | qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | struct ksym *ksym_search(long key) |
| 651 | { |
| 652 | int start = 0, end = sym_cnt; |
| 653 | int result; |
| 654 | |
| 655 | while (start < end) { |
| 656 | size_t mid = start + (end - start) / 2; |
| 657 | |
| 658 | result = key - syms[mid].addr; |
| 659 | if (result < 0) |
| 660 | end = mid; |
| 661 | else if (result > 0) |
| 662 | start = mid + 1; |
| 663 | else |
| 664 | return &syms[mid]; |
| 665 | } |
| 666 | |
| 667 | if (start >= 1 && syms[start - 1].addr < key && |
| 668 | key < syms[start].addr) |
| 669 | /* valid ksym */ |
| 670 | return &syms[start - 1]; |
| 671 | |
| 672 | /* out of range. return _stext */ |
| 673 | return &syms[0]; |
| 674 | } |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 675 | |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame] | 676 | int set_link_xdp_fd(int ifindex, int fd, __u32 flags) |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 677 | { |
| 678 | struct sockaddr_nl sa; |
| 679 | int sock, seq = 0, len, ret = -1; |
| 680 | char buf[4096]; |
| 681 | struct nlattr *nla, *nla_xdp; |
| 682 | struct { |
| 683 | struct nlmsghdr nh; |
| 684 | struct ifinfomsg ifinfo; |
| 685 | char attrbuf[64]; |
| 686 | } req; |
| 687 | struct nlmsghdr *nh; |
| 688 | struct nlmsgerr *err; |
| 689 | |
| 690 | memset(&sa, 0, sizeof(sa)); |
| 691 | sa.nl_family = AF_NETLINK; |
| 692 | |
| 693 | sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 694 | if (sock < 0) { |
| 695 | printf("open netlink socket: %s\n", strerror(errno)); |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) { |
| 700 | printf("bind to netlink: %s\n", strerror(errno)); |
| 701 | goto cleanup; |
| 702 | } |
| 703 | |
| 704 | memset(&req, 0, sizeof(req)); |
| 705 | req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
| 706 | req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; |
| 707 | req.nh.nlmsg_type = RTM_SETLINK; |
| 708 | req.nh.nlmsg_pid = 0; |
| 709 | req.nh.nlmsg_seq = ++seq; |
| 710 | req.ifinfo.ifi_family = AF_UNSPEC; |
| 711 | req.ifinfo.ifi_index = ifindex; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 712 | |
| 713 | /* started nested attribute for XDP */ |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 714 | nla = (struct nlattr *)(((char *)&req) |
| 715 | + NLMSG_ALIGN(req.nh.nlmsg_len)); |
| 716 | nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 717 | nla->nla_len = NLA_HDRLEN; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 718 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 719 | /* add XDP fd */ |
| 720 | nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 721 | nla_xdp->nla_type = 1/*IFLA_XDP_FD*/; |
| 722 | nla_xdp->nla_len = NLA_HDRLEN + sizeof(int); |
| 723 | memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd)); |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 724 | nla->nla_len += nla_xdp->nla_len; |
| 725 | |
| 726 | /* if user passed in any flags, add those too */ |
| 727 | if (flags) { |
| 728 | nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); |
| 729 | nla_xdp->nla_type = 3/*IFLA_XDP_FLAGS*/; |
| 730 | nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags); |
| 731 | memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags)); |
| 732 | nla->nla_len += nla_xdp->nla_len; |
| 733 | } |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 734 | |
| 735 | req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len); |
| 736 | |
| 737 | if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) { |
| 738 | printf("send to netlink: %s\n", strerror(errno)); |
| 739 | goto cleanup; |
| 740 | } |
| 741 | |
| 742 | len = recv(sock, buf, sizeof(buf), 0); |
| 743 | if (len < 0) { |
| 744 | printf("recv from netlink: %s\n", strerror(errno)); |
| 745 | goto cleanup; |
| 746 | } |
| 747 | |
| 748 | for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); |
| 749 | nh = NLMSG_NEXT(nh, len)) { |
| 750 | if (nh->nlmsg_pid != getpid()) { |
| 751 | printf("Wrong pid %d, expected %d\n", |
| 752 | nh->nlmsg_pid, getpid()); |
| 753 | goto cleanup; |
| 754 | } |
| 755 | if (nh->nlmsg_seq != seq) { |
| 756 | printf("Wrong seq %d, expected %d\n", |
| 757 | nh->nlmsg_seq, seq); |
| 758 | goto cleanup; |
| 759 | } |
| 760 | switch (nh->nlmsg_type) { |
| 761 | case NLMSG_ERROR: |
| 762 | err = (struct nlmsgerr *)NLMSG_DATA(nh); |
| 763 | if (!err->error) |
| 764 | continue; |
| 765 | printf("nlmsg error %s\n", strerror(-err->error)); |
| 766 | goto cleanup; |
| 767 | case NLMSG_DONE: |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | ret = 0; |
| 773 | |
| 774 | cleanup: |
| 775 | close(sock); |
| 776 | return ret; |
| 777 | } |