2 * iplink_xdp.c XDP program loader
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.
9 * Authors: Daniel Borkmann <daniel@iogearbox.net>
15 #include <linux/bpf.h>
23 struct iplink_req
*req
;
27 static void xdp_ebpf_cb(void *raw
, int fd
, const char *annotation
)
29 struct xdp_req
*xdp
= raw
;
30 struct iplink_req
*req
= xdp
->req
;
31 struct rtattr
*xdp_attr
;
33 xdp_attr
= addattr_nest(&req
->n
, sizeof(*req
), IFLA_XDP
);
34 addattr32(&req
->n
, sizeof(*req
), IFLA_XDP_FD
, fd
);
36 addattr32(&req
->n
, sizeof(*req
), IFLA_XDP_FLAGS
, xdp
->flags
);
37 addattr_nest_end(&req
->n
, xdp_attr
);
40 static const struct bpf_cfg_ops bpf_cb_ops
= {
41 .ebpf_cb
= xdp_ebpf_cb
,
44 static int xdp_delete(struct xdp_req
*xdp
)
46 xdp_ebpf_cb(xdp
, -1, NULL
);
50 int xdp_parse(int *argc
, char ***argv
, struct iplink_req
*req
, bool generic
,
51 bool drv
, bool offload
)
53 struct bpf_cfg_in cfg
= {
57 struct xdp_req xdp
= {
62 xdp
.flags
|= XDP_FLAGS_UPDATE_IF_NOEXIST
;
64 xdp
.flags
|= XDP_FLAGS_SKB_MODE
;
66 xdp
.flags
|= XDP_FLAGS_DRV_MODE
;
68 xdp
.flags
|= XDP_FLAGS_HW_MODE
;
71 if (strcmp(**argv
, "none") == 0 ||
72 strcmp(**argv
, "off") == 0)
73 return xdp_delete(&xdp
);
76 if (bpf_parse_common(BPF_PROG_TYPE_XDP
, &cfg
, &bpf_cb_ops
, &xdp
))
84 void xdp_dump(FILE *fp
, struct rtattr
*xdp
)
86 struct rtattr
*tb
[IFLA_XDP_MAX
+ 1];
89 parse_rtattr_nested(tb
, IFLA_XDP_MAX
, xdp
);
91 if (!tb
[IFLA_XDP_ATTACHED
])
94 mode
= rta_getattr_u8(tb
[IFLA_XDP_ATTACHED
]);
95 if (mode
== XDP_ATTACHED_NONE
)
97 else if (mode
== XDP_ATTACHED_DRV
)
99 else if (mode
== XDP_ATTACHED_SKB
)
100 fprintf(fp
, "xdpgeneric");
101 else if (mode
== XDP_ATTACHED_HW
)
102 fprintf(fp
, "xdpoffload");
104 fprintf(fp
, "xdp[%u]", mode
);
106 if (tb
[IFLA_XDP_PROG_ID
])
107 fprintf(fp
, "/id:%u",
108 rta_getattr_u32(tb
[IFLA_XDP_PROG_ID
]));