]>
Commit | Line | Data |
---|---|---|
c7272ca7 DB |
1 | /* |
2 | * iplink_xdp.c XDP program loader | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Daniel Borkmann <daniel@iogearbox.net> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | ||
15 | #include <linux/bpf.h> | |
16 | ||
bc2d4d83 | 17 | #include "json_print.h" |
c7272ca7 DB |
18 | #include "xdp.h" |
19 | #include "bpf_util.h" | |
20 | ||
21 | extern int force; | |
22 | ||
a872b870 DB |
23 | struct xdp_req { |
24 | struct iplink_req *req; | |
25 | __u32 flags; | |
26 | }; | |
27 | ||
c7272ca7 DB |
28 | static void xdp_ebpf_cb(void *raw, int fd, const char *annotation) |
29 | { | |
a872b870 DB |
30 | struct xdp_req *xdp = raw; |
31 | struct iplink_req *req = xdp->req; | |
32 | struct rtattr *xdp_attr; | |
c7272ca7 | 33 | |
a872b870 | 34 | xdp_attr = addattr_nest(&req->n, sizeof(*req), IFLA_XDP); |
c7272ca7 | 35 | addattr32(&req->n, sizeof(*req), IFLA_XDP_FD, fd); |
a872b870 DB |
36 | if (xdp->flags) |
37 | addattr32(&req->n, sizeof(*req), IFLA_XDP_FLAGS, xdp->flags); | |
38 | addattr_nest_end(&req->n, xdp_attr); | |
c7272ca7 DB |
39 | } |
40 | ||
41 | static const struct bpf_cfg_ops bpf_cb_ops = { | |
42 | .ebpf_cb = xdp_ebpf_cb, | |
43 | }; | |
44 | ||
a872b870 | 45 | static int xdp_delete(struct xdp_req *xdp) |
c7272ca7 | 46 | { |
a872b870 | 47 | xdp_ebpf_cb(xdp, -1, NULL); |
c7272ca7 DB |
48 | return 0; |
49 | } | |
50 | ||
14683814 | 51 | int xdp_parse(int *argc, char ***argv, struct iplink_req *req, bool generic, |
1b5e8094 | 52 | bool drv, bool offload) |
c7272ca7 DB |
53 | { |
54 | struct bpf_cfg_in cfg = { | |
658cfebc | 55 | .type = BPF_PROG_TYPE_XDP, |
c7272ca7 DB |
56 | .argc = *argc, |
57 | .argv = *argv, | |
58 | }; | |
a872b870 DB |
59 | struct xdp_req xdp = { |
60 | .req = req, | |
61 | }; | |
62 | ||
63 | if (!force) | |
64 | xdp.flags |= XDP_FLAGS_UPDATE_IF_NOEXIST; | |
65 | if (generic) | |
66 | xdp.flags |= XDP_FLAGS_SKB_MODE; | |
14683814 JK |
67 | if (drv) |
68 | xdp.flags |= XDP_FLAGS_DRV_MODE; | |
1b5e8094 JK |
69 | if (offload) |
70 | xdp.flags |= XDP_FLAGS_HW_MODE; | |
c7272ca7 DB |
71 | |
72 | if (*argc == 1) { | |
73 | if (strcmp(**argv, "none") == 0 || | |
74 | strcmp(**argv, "off") == 0) | |
a872b870 | 75 | return xdp_delete(&xdp); |
c7272ca7 | 76 | } |
a872b870 | 77 | |
399db839 | 78 | if (bpf_parse_and_load_common(&cfg, &bpf_cb_ops, &xdp)) |
c7272ca7 DB |
79 | return -1; |
80 | ||
81 | *argc = cfg.argc; | |
82 | *argv = cfg.argv; | |
83 | return 0; | |
84 | } | |
85 | ||
bc2d4d83 DB |
86 | static void xdp_dump_json(struct rtattr *tb[IFLA_XDP_MAX + 1]) |
87 | { | |
88 | __u32 prog_id = 0; | |
89 | __u8 mode; | |
90 | ||
91 | mode = rta_getattr_u8(tb[IFLA_XDP_ATTACHED]); | |
92 | if (tb[IFLA_XDP_PROG_ID]) | |
93 | prog_id = rta_getattr_u32(tb[IFLA_XDP_PROG_ID]); | |
94 | ||
95 | open_json_object("xdp"); | |
96 | print_uint(PRINT_JSON, "mode", NULL, mode); | |
97 | if (prog_id) | |
98 | bpf_dump_prog_info(NULL, prog_id); | |
99 | close_json_object(); | |
100 | } | |
101 | ||
a0b5b7cf | 102 | void xdp_dump(FILE *fp, struct rtattr *xdp, bool link, bool details) |
c7272ca7 DB |
103 | { |
104 | struct rtattr *tb[IFLA_XDP_MAX + 1]; | |
a0b5b7cf | 105 | __u32 prog_id = 0; |
077bb180 | 106 | __u8 mode; |
c7272ca7 DB |
107 | |
108 | parse_rtattr_nested(tb, IFLA_XDP_MAX, xdp); | |
a872b870 | 109 | |
077bb180 | 110 | if (!tb[IFLA_XDP_ATTACHED]) |
c7272ca7 DB |
111 | return; |
112 | ||
077bb180 | 113 | mode = rta_getattr_u8(tb[IFLA_XDP_ATTACHED]); |
bc2d4d83 DB |
114 | if (mode == XDP_ATTACHED_NONE) |
115 | return; | |
116 | else if (is_json_context()) | |
117 | return details ? (void)0 : xdp_dump_json(tb); | |
118 | else if (details && link) | |
119 | fprintf(fp, "%s prog/xdp", _SL_); | |
120 | else if (mode == XDP_ATTACHED_DRV) | |
121 | fprintf(fp, "xdp"); | |
122 | else if (mode == XDP_ATTACHED_SKB) | |
123 | fprintf(fp, "xdpgeneric"); | |
124 | else if (mode == XDP_ATTACHED_HW) | |
125 | fprintf(fp, "xdpoffload"); | |
126 | else | |
127 | fprintf(fp, "xdp[%u]", mode); | |
128 | ||
129 | if (tb[IFLA_XDP_PROG_ID]) | |
130 | prog_id = rta_getattr_u32(tb[IFLA_XDP_PROG_ID]); | |
131 | if (!details) { | |
132 | if (prog_id && !link) | |
133 | fprintf(fp, "/id:%u", prog_id); | |
134 | fprintf(fp, " "); | |
135 | return; | |
136 | } | |
137 | ||
138 | if (prog_id) { | |
139 | fprintf(fp, " "); | |
140 | bpf_dump_prog_info(fp, prog_id); | |
a0b5b7cf | 141 | } |
c7272ca7 | 142 | } |