1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Reference program for verifying XDP metadata on real HW. Functional test 4 * only, doesn't test the performance. 5 * 6 * RX: 7 * - UDP 9091 packets are diverted into AF_XDP 8 * - Metadata verified: 9 * - rx_timestamp 10 * - rx_hash 11 * 12 * TX: 13 * - TBD 14 */ 15 16 #include <test_progs.h> 17 #include <network_helpers.h> 18 #include "xdp_hw_metadata.skel.h" 19 #include "xsk.h" 20 21 #include <error.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> 27 #include <linux/net_tstamp.h> 28 #include <sys/mman.h> 29 #include <net/if.h> 30 #include <poll.h> 31 32 #include "xdp_metadata.h" 33 34 #define UMEM_NUM 16 35 #define UMEM_FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE 36 #define UMEM_SIZE (UMEM_FRAME_SIZE * UMEM_NUM) 37 #define XDP_FLAGS (XDP_FLAGS_DRV_MODE | XDP_FLAGS_REPLACE) 38 39 struct xsk { 40 void *umem_area; 41 struct xsk_umem *umem; 42 struct xsk_ring_prod fill; 43 struct xsk_ring_cons comp; 44 struct xsk_ring_prod tx; 45 struct xsk_ring_cons rx; 46 struct xsk_socket *socket; 47 }; 48 49 struct xdp_hw_metadata *bpf_obj; 50 struct xsk *rx_xsk; 51 const char *ifname; 52 int ifindex; 53 int rxq; 54 55 void test__fail(void) { /* for network_helpers.c */ } 56 57 static int open_xsk(int ifindex, struct xsk *xsk, __u32 queue_id) 58 { 59 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; 60 const struct xsk_socket_config socket_config = { 61 .rx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, 62 .tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, 63 .bind_flags = XDP_COPY, 64 }; 65 const struct xsk_umem_config umem_config = { 66 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, 67 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS, 68 .frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE, 69 .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG, 70 }; 71 __u32 idx; 72 u64 addr; 73 int ret; 74 int i; 75 76 xsk->umem_area = mmap(NULL, UMEM_SIZE, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); 77 if (xsk->umem_area == MAP_FAILED) 78 return -ENOMEM; 79 80 ret = xsk_umem__create(&xsk->umem, 81 xsk->umem_area, UMEM_SIZE, 82 &xsk->fill, 83 &xsk->comp, 84 &umem_config); 85 if (ret) 86 return ret; 87 88 ret = xsk_socket__create(&xsk->socket, ifindex, queue_id, 89 xsk->umem, 90 &xsk->rx, 91 &xsk->tx, 92 &socket_config); 93 if (ret) 94 return ret; 95 96 /* First half of umem is for TX. This way address matches 1-to-1 97 * to the completion queue index. 98 */ 99 100 for (i = 0; i < UMEM_NUM / 2; i++) { 101 addr = i * UMEM_FRAME_SIZE; 102 printf("%p: tx_desc[%d] -> %lx\n", xsk, i, addr); 103 } 104 105 /* Second half of umem is for RX. */ 106 107 ret = xsk_ring_prod__reserve(&xsk->fill, UMEM_NUM / 2, &idx); 108 for (i = 0; i < UMEM_NUM / 2; i++) { 109 addr = (UMEM_NUM / 2 + i) * UMEM_FRAME_SIZE; 110 printf("%p: rx_desc[%d] -> %lx\n", xsk, i, addr); 111 *xsk_ring_prod__fill_addr(&xsk->fill, i) = addr; 112 } 113 xsk_ring_prod__submit(&xsk->fill, ret); 114 115 return 0; 116 } 117 118 static void close_xsk(struct xsk *xsk) 119 { 120 if (xsk->umem) 121 xsk_umem__delete(xsk->umem); 122 if (xsk->socket) 123 xsk_socket__delete(xsk->socket); 124 munmap(xsk->umem, UMEM_SIZE); 125 } 126 127 static void refill_rx(struct xsk *xsk, __u64 addr) 128 { 129 __u32 idx; 130 131 if (xsk_ring_prod__reserve(&xsk->fill, 1, &idx) == 1) { 132 printf("%p: complete idx=%u addr=%llx\n", xsk, idx, addr); 133 *xsk_ring_prod__fill_addr(&xsk->fill, idx) = addr; 134 xsk_ring_prod__submit(&xsk->fill, 1); 135 } 136 } 137 138 static void verify_xdp_metadata(void *data) 139 { 140 struct xdp_meta *meta; 141 142 meta = data - sizeof(*meta); 143 144 printf("rx_timestamp: %llu\n", meta->rx_timestamp); 145 printf("rx_hash: %u\n", meta->rx_hash); 146 } 147 148 static void verify_skb_metadata(int fd) 149 { 150 char cmsg_buf[1024]; 151 char packet_buf[128]; 152 153 struct scm_timestamping *ts; 154 struct iovec packet_iov; 155 struct cmsghdr *cmsg; 156 struct msghdr hdr; 157 158 memset(&hdr, 0, sizeof(hdr)); 159 hdr.msg_iov = &packet_iov; 160 hdr.msg_iovlen = 1; 161 packet_iov.iov_base = packet_buf; 162 packet_iov.iov_len = sizeof(packet_buf); 163 164 hdr.msg_control = cmsg_buf; 165 hdr.msg_controllen = sizeof(cmsg_buf); 166 167 if (recvmsg(fd, &hdr, 0) < 0) 168 error(-1, errno, "recvmsg"); 169 170 for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL; 171 cmsg = CMSG_NXTHDR(&hdr, cmsg)) { 172 173 if (cmsg->cmsg_level != SOL_SOCKET) 174 continue; 175 176 switch (cmsg->cmsg_type) { 177 case SCM_TIMESTAMPING: 178 ts = (struct scm_timestamping *)CMSG_DATA(cmsg); 179 if (ts->ts[2].tv_sec || ts->ts[2].tv_nsec) { 180 printf("found skb hwtstamp = %lu.%lu\n", 181 ts->ts[2].tv_sec, ts->ts[2].tv_nsec); 182 return; 183 } 184 break; 185 default: 186 break; 187 } 188 } 189 190 printf("skb hwtstamp is not found!\n"); 191 } 192 193 static int verify_metadata(struct xsk *rx_xsk, int rxq, int server_fd) 194 { 195 const struct xdp_desc *rx_desc; 196 struct pollfd fds[rxq + 1]; 197 __u64 comp_addr; 198 __u64 addr; 199 __u32 idx; 200 int ret; 201 int i; 202 203 for (i = 0; i < rxq; i++) { 204 fds[i].fd = xsk_socket__fd(rx_xsk[i].socket); 205 fds[i].events = POLLIN; 206 fds[i].revents = 0; 207 } 208 209 fds[rxq].fd = server_fd; 210 fds[rxq].events = POLLIN; 211 fds[rxq].revents = 0; 212 213 while (true) { 214 errno = 0; 215 ret = poll(fds, rxq + 1, 1000); 216 printf("poll: %d (%d)\n", ret, errno); 217 if (ret < 0) 218 break; 219 if (ret == 0) 220 continue; 221 222 if (fds[rxq].revents) 223 verify_skb_metadata(server_fd); 224 225 for (i = 0; i < rxq; i++) { 226 if (fds[i].revents == 0) 227 continue; 228 229 struct xsk *xsk = &rx_xsk[i]; 230 231 ret = xsk_ring_cons__peek(&xsk->rx, 1, &idx); 232 printf("xsk_ring_cons__peek: %d\n", ret); 233 if (ret != 1) 234 continue; 235 236 rx_desc = xsk_ring_cons__rx_desc(&xsk->rx, idx); 237 comp_addr = xsk_umem__extract_addr(rx_desc->addr); 238 addr = xsk_umem__add_offset_to_addr(rx_desc->addr); 239 printf("%p: rx_desc[%u]->addr=%llx addr=%llx comp_addr=%llx\n", 240 xsk, idx, rx_desc->addr, addr, comp_addr); 241 verify_xdp_metadata(xsk_umem__get_data(xsk->umem_area, addr)); 242 xsk_ring_cons__release(&xsk->rx, 1); 243 refill_rx(xsk, comp_addr); 244 } 245 } 246 247 return 0; 248 } 249 250 struct ethtool_channels { 251 __u32 cmd; 252 __u32 max_rx; 253 __u32 max_tx; 254 __u32 max_other; 255 __u32 max_combined; 256 __u32 rx_count; 257 __u32 tx_count; 258 __u32 other_count; 259 __u32 combined_count; 260 }; 261 262 #define ETHTOOL_GCHANNELS 0x0000003c /* Get no of channels */ 263 264 static int rxq_num(const char *ifname) 265 { 266 struct ethtool_channels ch = { 267 .cmd = ETHTOOL_GCHANNELS, 268 }; 269 270 struct ifreq ifr = { 271 .ifr_data = (void *)&ch, 272 }; 273 strcpy(ifr.ifr_name, ifname); 274 int fd, ret; 275 276 fd = socket(AF_UNIX, SOCK_DGRAM, 0); 277 if (fd < 0) 278 error(-1, errno, "socket"); 279 280 ret = ioctl(fd, SIOCETHTOOL, &ifr); 281 if (ret < 0) 282 error(-1, errno, "ioctl(SIOCETHTOOL)"); 283 284 close(fd); 285 286 return ch.rx_count + ch.combined_count; 287 } 288 289 static void hwtstamp_ioctl(int op, const char *ifname, struct hwtstamp_config *cfg) 290 { 291 struct ifreq ifr = { 292 .ifr_data = (void *)cfg, 293 }; 294 strcpy(ifr.ifr_name, ifname); 295 int fd, ret; 296 297 fd = socket(AF_UNIX, SOCK_DGRAM, 0); 298 if (fd < 0) 299 error(-1, errno, "socket"); 300 301 ret = ioctl(fd, op, &ifr); 302 if (ret < 0) 303 error(-1, errno, "ioctl(%d)", op); 304 305 close(fd); 306 } 307 308 static struct hwtstamp_config saved_hwtstamp_cfg; 309 static const char *saved_hwtstamp_ifname; 310 311 static void hwtstamp_restore(void) 312 { 313 hwtstamp_ioctl(SIOCSHWTSTAMP, saved_hwtstamp_ifname, &saved_hwtstamp_cfg); 314 } 315 316 static void hwtstamp_enable(const char *ifname) 317 { 318 struct hwtstamp_config cfg = { 319 .rx_filter = HWTSTAMP_FILTER_ALL, 320 }; 321 322 hwtstamp_ioctl(SIOCGHWTSTAMP, ifname, &saved_hwtstamp_cfg); 323 saved_hwtstamp_ifname = strdup(ifname); 324 atexit(hwtstamp_restore); 325 326 hwtstamp_ioctl(SIOCSHWTSTAMP, ifname, &cfg); 327 } 328 329 static void cleanup(void) 330 { 331 LIBBPF_OPTS(bpf_xdp_attach_opts, opts); 332 int ret; 333 int i; 334 335 if (bpf_obj) { 336 opts.old_prog_fd = bpf_program__fd(bpf_obj->progs.rx); 337 if (opts.old_prog_fd >= 0) { 338 printf("detaching bpf program....\n"); 339 ret = bpf_xdp_detach(ifindex, XDP_FLAGS, &opts); 340 if (ret) 341 printf("failed to detach XDP program: %d\n", ret); 342 } 343 } 344 345 for (i = 0; i < rxq; i++) 346 close_xsk(&rx_xsk[i]); 347 348 if (bpf_obj) 349 xdp_hw_metadata__destroy(bpf_obj); 350 } 351 352 static void handle_signal(int sig) 353 { 354 /* interrupting poll() is all we need */ 355 } 356 357 static void timestamping_enable(int fd, int val) 358 { 359 int ret; 360 361 ret = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val)); 362 if (ret < 0) 363 error(-1, errno, "setsockopt(SO_TIMESTAMPING)"); 364 } 365 366 int main(int argc, char *argv[]) 367 { 368 int server_fd = -1; 369 int ret; 370 int i; 371 372 struct bpf_program *prog; 373 374 if (argc != 2) { 375 fprintf(stderr, "pass device name\n"); 376 return -1; 377 } 378 379 ifname = argv[1]; 380 ifindex = if_nametoindex(ifname); 381 rxq = rxq_num(ifname); 382 383 printf("rxq: %d\n", rxq); 384 385 hwtstamp_enable(ifname); 386 387 rx_xsk = malloc(sizeof(struct xsk) * rxq); 388 if (!rx_xsk) 389 error(-1, ENOMEM, "malloc"); 390 391 for (i = 0; i < rxq; i++) { 392 printf("open_xsk(%s, %p, %d)\n", ifname, &rx_xsk[i], i); 393 ret = open_xsk(ifindex, &rx_xsk[i], i); 394 if (ret) 395 error(-1, -ret, "open_xsk"); 396 397 printf("xsk_socket__fd() -> %d\n", xsk_socket__fd(rx_xsk[i].socket)); 398 } 399 400 printf("open bpf program...\n"); 401 bpf_obj = xdp_hw_metadata__open(); 402 if (libbpf_get_error(bpf_obj)) 403 error(-1, libbpf_get_error(bpf_obj), "xdp_hw_metadata__open"); 404 405 prog = bpf_object__find_program_by_name(bpf_obj->obj, "rx"); 406 bpf_program__set_ifindex(prog, ifindex); 407 bpf_program__set_flags(prog, BPF_F_XDP_DEV_BOUND_ONLY); 408 409 printf("load bpf program...\n"); 410 ret = xdp_hw_metadata__load(bpf_obj); 411 if (ret) 412 error(-1, -ret, "xdp_hw_metadata__load"); 413 414 printf("prepare skb endpoint...\n"); 415 server_fd = start_server(AF_INET6, SOCK_DGRAM, NULL, 9092, 1000); 416 if (server_fd < 0) 417 error(-1, errno, "start_server"); 418 timestamping_enable(server_fd, 419 SOF_TIMESTAMPING_SOFTWARE | 420 SOF_TIMESTAMPING_RAW_HARDWARE); 421 422 printf("prepare xsk map...\n"); 423 for (i = 0; i < rxq; i++) { 424 int sock_fd = xsk_socket__fd(rx_xsk[i].socket); 425 __u32 queue_id = i; 426 427 printf("map[%d] = %d\n", queue_id, sock_fd); 428 ret = bpf_map_update_elem(bpf_map__fd(bpf_obj->maps.xsk), &queue_id, &sock_fd, 0); 429 if (ret) 430 error(-1, -ret, "bpf_map_update_elem"); 431 } 432 433 printf("attach bpf program...\n"); 434 ret = bpf_xdp_attach(ifindex, 435 bpf_program__fd(bpf_obj->progs.rx), 436 XDP_FLAGS, NULL); 437 if (ret) 438 error(-1, -ret, "bpf_xdp_attach"); 439 440 signal(SIGINT, handle_signal); 441 ret = verify_metadata(rx_xsk, rxq, server_fd); 442 close(server_fd); 443 cleanup(); 444 if (ret) 445 error(-1, -ret, "verify_metadata"); 446 } 447