1 // SPDX-License-Identifier: GPL-2.0
3 /* Reference program for verifying XDP metadata on real HW. Functional test
4 * only, doesn't test the performance.
7 * - UDP 9091 packets are diverted into AF_XDP
16 #include <test_progs.h>
17 #include <network_helpers.h>
18 #include "xdp_hw_metadata.skel.h"
22 #include <linux/errqueue.h>
23 #include <linux/if_link.h>
24 #include <linux/net_tstamp.h>
25 #include <linux/udp.h>
26 #include <linux/sockios.h>
31 #include "xdp_metadata.h"
34 #define UMEM_FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE
35 #define UMEM_SIZE (UMEM_FRAME_SIZE * UMEM_NUM)
36 #define XDP_FLAGS (XDP_FLAGS_DRV_MODE | XDP_FLAGS_REPLACE)
40 struct xsk_umem *umem;
41 struct xsk_ring_prod fill;
42 struct xsk_ring_cons comp;
43 struct xsk_ring_prod tx;
44 struct xsk_ring_cons rx;
45 struct xsk_socket *socket;
48 struct xdp_hw_metadata *bpf_obj;
54 void test__fail(void) { /* for network_helpers.c */ }
56 static int open_xsk(int ifindex, struct xsk *xsk, __u32 queue_id)
58 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
59 const struct xsk_socket_config socket_config = {
60 .rx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
61 .tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
62 .bind_flags = XDP_COPY,
64 const struct xsk_umem_config umem_config = {
65 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
66 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
67 .frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE,
68 .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG,
75 xsk->umem_area = mmap(NULL, UMEM_SIZE, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
76 if (xsk->umem_area == MAP_FAILED)
79 ret = xsk_umem__create(&xsk->umem,
80 xsk->umem_area, UMEM_SIZE,
87 ret = xsk_socket__create(&xsk->socket, ifindex, queue_id,
95 /* First half of umem is for TX. This way address matches 1-to-1
96 * to the completion queue index.
99 for (i = 0; i < UMEM_NUM / 2; i++) {
100 addr = i * UMEM_FRAME_SIZE;
101 printf("%p: tx_desc[%d] -> %lx\n", xsk, i, addr);
104 /* Second half of umem is for RX. */
106 ret = xsk_ring_prod__reserve(&xsk->fill, UMEM_NUM / 2, &idx);
107 for (i = 0; i < UMEM_NUM / 2; i++) {
108 addr = (UMEM_NUM / 2 + i) * UMEM_FRAME_SIZE;
109 printf("%p: rx_desc[%d] -> %lx\n", xsk, i, addr);
110 *xsk_ring_prod__fill_addr(&xsk->fill, i) = addr;
112 xsk_ring_prod__submit(&xsk->fill, ret);
117 static void close_xsk(struct xsk *xsk)
120 xsk_umem__delete(xsk->umem);
122 xsk_socket__delete(xsk->socket);
123 munmap(xsk->umem_area, UMEM_SIZE);
126 static void refill_rx(struct xsk *xsk, __u64 addr)
130 if (xsk_ring_prod__reserve(&xsk->fill, 1, &idx) == 1) {
131 printf("%p: complete idx=%u addr=%llx\n", xsk, idx, addr);
132 *xsk_ring_prod__fill_addr(&xsk->fill, idx) = addr;
133 xsk_ring_prod__submit(&xsk->fill, 1);
137 static void verify_xdp_metadata(void *data)
139 struct xdp_meta *meta;
141 meta = data - sizeof(*meta);
143 printf("rx_timestamp: %llu\n", meta->rx_timestamp);
144 printf("rx_hash: %u\n", meta->rx_hash);
147 static void verify_skb_metadata(int fd)
150 char packet_buf[128];
152 struct scm_timestamping *ts;
153 struct iovec packet_iov;
154 struct cmsghdr *cmsg;
157 memset(&hdr, 0, sizeof(hdr));
158 hdr.msg_iov = &packet_iov;
160 packet_iov.iov_base = packet_buf;
161 packet_iov.iov_len = sizeof(packet_buf);
163 hdr.msg_control = cmsg_buf;
164 hdr.msg_controllen = sizeof(cmsg_buf);
166 if (recvmsg(fd, &hdr, 0) < 0)
167 error(1, errno, "recvmsg");
169 for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
170 cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
172 if (cmsg->cmsg_level != SOL_SOCKET)
175 switch (cmsg->cmsg_type) {
176 case SCM_TIMESTAMPING:
177 ts = (struct scm_timestamping *)CMSG_DATA(cmsg);
178 if (ts->ts[2].tv_sec || ts->ts[2].tv_nsec) {
179 printf("found skb hwtstamp = %lu.%lu\n",
180 ts->ts[2].tv_sec, ts->ts[2].tv_nsec);
189 printf("skb hwtstamp is not found!\n");
192 static int verify_metadata(struct xsk *rx_xsk, int rxq, int server_fd)
194 const struct xdp_desc *rx_desc;
195 struct pollfd fds[rxq + 1];
202 for (i = 0; i < rxq; i++) {
203 fds[i].fd = xsk_socket__fd(rx_xsk[i].socket);
204 fds[i].events = POLLIN;
208 fds[rxq].fd = server_fd;
209 fds[rxq].events = POLLIN;
210 fds[rxq].revents = 0;
214 ret = poll(fds, rxq + 1, 1000);
215 printf("poll: %d (%d)\n", ret, errno);
221 if (fds[rxq].revents)
222 verify_skb_metadata(server_fd);
224 for (i = 0; i < rxq; i++) {
225 if (fds[i].revents == 0)
228 struct xsk *xsk = &rx_xsk[i];
230 ret = xsk_ring_cons__peek(&xsk->rx, 1, &idx);
231 printf("xsk_ring_cons__peek: %d\n", ret);
235 rx_desc = xsk_ring_cons__rx_desc(&xsk->rx, idx);
236 comp_addr = xsk_umem__extract_addr(rx_desc->addr);
237 addr = xsk_umem__add_offset_to_addr(rx_desc->addr);
238 printf("%p: rx_desc[%u]->addr=%llx addr=%llx comp_addr=%llx\n",
239 xsk, idx, rx_desc->addr, addr, comp_addr);
240 verify_xdp_metadata(xsk_umem__get_data(xsk->umem_area, addr));
241 xsk_ring_cons__release(&xsk->rx, 1);
242 refill_rx(xsk, comp_addr);
249 struct ethtool_channels {
258 __u32 combined_count;
261 #define ETHTOOL_GCHANNELS 0x0000003c /* Get no of channels */
263 static int rxq_num(const char *ifname)
265 struct ethtool_channels ch = {
266 .cmd = ETHTOOL_GCHANNELS,
270 .ifr_data = (void *)&ch,
272 strncpy(ifr.ifr_name, ifname, IF_NAMESIZE - 1);
275 fd = socket(AF_UNIX, SOCK_DGRAM, 0);
277 error(1, errno, "socket");
279 ret = ioctl(fd, SIOCETHTOOL, &ifr);
281 error(1, errno, "ioctl(SIOCETHTOOL)");
285 return ch.rx_count + ch.combined_count;
288 static void hwtstamp_ioctl(int op, const char *ifname, struct hwtstamp_config *cfg)
291 .ifr_data = (void *)cfg,
293 strncpy(ifr.ifr_name, ifname, IF_NAMESIZE - 1);
296 fd = socket(AF_UNIX, SOCK_DGRAM, 0);
298 error(1, errno, "socket");
300 ret = ioctl(fd, op, &ifr);
302 error(1, errno, "ioctl(%d)", op);
307 static struct hwtstamp_config saved_hwtstamp_cfg;
308 static const char *saved_hwtstamp_ifname;
310 static void hwtstamp_restore(void)
312 hwtstamp_ioctl(SIOCSHWTSTAMP, saved_hwtstamp_ifname, &saved_hwtstamp_cfg);
315 static void hwtstamp_enable(const char *ifname)
317 struct hwtstamp_config cfg = {
318 .rx_filter = HWTSTAMP_FILTER_ALL,
321 hwtstamp_ioctl(SIOCGHWTSTAMP, ifname, &saved_hwtstamp_cfg);
322 saved_hwtstamp_ifname = strdup(ifname);
323 atexit(hwtstamp_restore);
325 hwtstamp_ioctl(SIOCSHWTSTAMP, ifname, &cfg);
328 static void cleanup(void)
330 LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
335 opts.old_prog_fd = bpf_program__fd(bpf_obj->progs.rx);
336 if (opts.old_prog_fd >= 0) {
337 printf("detaching bpf program....\n");
338 ret = bpf_xdp_detach(ifindex, XDP_FLAGS, &opts);
340 printf("failed to detach XDP program: %d\n", ret);
344 for (i = 0; i < rxq; i++)
345 close_xsk(&rx_xsk[i]);
348 xdp_hw_metadata__destroy(bpf_obj);
351 static void handle_signal(int sig)
353 /* interrupting poll() is all we need */
356 static void timestamping_enable(int fd, int val)
360 ret = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val));
362 error(1, errno, "setsockopt(SO_TIMESTAMPING)");
365 int main(int argc, char *argv[])
371 struct bpf_program *prog;
374 fprintf(stderr, "pass device name\n");
379 ifindex = if_nametoindex(ifname);
380 rxq = rxq_num(ifname);
382 printf("rxq: %d\n", rxq);
384 hwtstamp_enable(ifname);
386 rx_xsk = malloc(sizeof(struct xsk) * rxq);
388 error(1, ENOMEM, "malloc");
390 for (i = 0; i < rxq; i++) {
391 printf("open_xsk(%s, %p, %d)\n", ifname, &rx_xsk[i], i);
392 ret = open_xsk(ifindex, &rx_xsk[i], i);
394 error(1, -ret, "open_xsk");
396 printf("xsk_socket__fd() -> %d\n", xsk_socket__fd(rx_xsk[i].socket));
399 printf("open bpf program...\n");
400 bpf_obj = xdp_hw_metadata__open();
401 if (libbpf_get_error(bpf_obj))
402 error(1, libbpf_get_error(bpf_obj), "xdp_hw_metadata__open");
404 prog = bpf_object__find_program_by_name(bpf_obj->obj, "rx");
405 bpf_program__set_ifindex(prog, ifindex);
406 bpf_program__set_flags(prog, BPF_F_XDP_DEV_BOUND_ONLY);
408 printf("load bpf program...\n");
409 ret = xdp_hw_metadata__load(bpf_obj);
411 error(1, -ret, "xdp_hw_metadata__load");
413 printf("prepare skb endpoint...\n");
414 server_fd = start_server(AF_INET6, SOCK_DGRAM, NULL, 9092, 1000);
416 error(1, errno, "start_server");
417 timestamping_enable(server_fd,
418 SOF_TIMESTAMPING_SOFTWARE |
419 SOF_TIMESTAMPING_RAW_HARDWARE);
421 printf("prepare xsk map...\n");
422 for (i = 0; i < rxq; i++) {
423 int sock_fd = xsk_socket__fd(rx_xsk[i].socket);
426 printf("map[%d] = %d\n", queue_id, sock_fd);
427 ret = bpf_map_update_elem(bpf_map__fd(bpf_obj->maps.xsk), &queue_id, &sock_fd, 0);
429 error(1, -ret, "bpf_map_update_elem");
432 printf("attach bpf program...\n");
433 ret = bpf_xdp_attach(ifindex,
434 bpf_program__fd(bpf_obj->progs.rx),
437 error(1, -ret, "bpf_xdp_attach");
439 signal(SIGINT, handle_signal);
440 ret = verify_metadata(rx_xsk, rxq, server_fd);
444 error(1, -ret, "verify_metadata");