1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
// SPDX-License-Identifier: GPL-2.0
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#include <arpa/inet.h>
#include <linux/errqueue.h>
#include <linux/if_packet.h>
#include <linux/ipv6.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <linux/mman.h>
#include <linux/memfd.h>
#include <linux/dma-buf.h>
#include <linux/udmabuf.h>
#include "liburing.h"
#include "helpers.h"
enum {
RQ_ALLOC_USER,
RQ_ALLOC_KERNEL,
__RQ_ALLOC_MAX,
};
static long page_size;
#define AREA_SIZE (8192 * page_size)
#define REQ_TYPE_SHIFT 3
#define REQ_TYPE_MASK ((1UL << REQ_TYPE_SHIFT) - 1)
enum {
AREA_TYPE_NORMAL,
AREA_TYPE_HUGE_PAGES,
AREA_TYPE_DMABUF,
__AREA_TYPE_MAX,
};
enum {
REQ_TYPE_ACCEPT = 1,
REQ_TYPE_RX = 2,
};
static int cfg_port = 8000;
static const char *cfg_ifname;
static int cfg_queue_id = -1;
static bool cfg_verify_data = false;
static size_t cfg_size = 0;
static unsigned cfg_rq_alloc_mode = RQ_ALLOC_USER;
static unsigned cfg_area_type = AREA_TYPE_NORMAL;
static struct sockaddr_in6 cfg_addr;
static void *area_ptr;
static void *ring_ptr;
static size_t ring_size;
static struct io_uring_zcrx_rq rq_ring;
static unsigned long area_token;
static int connfd;
static bool stop;
static size_t received;
static __u32 zcrx_id;
static int dmabuf_fd;
static int memfd;
static inline size_t get_refill_ring_size(unsigned int rq_entries)
{
ring_size = rq_entries * sizeof(struct io_uring_zcrx_rqe);
/* add space for the header (head/tail/etc.) */
ring_size += page_size;
return T_ALIGN_UP(ring_size, page_size);
}
static void zcrx_populate_area_udmabuf(struct io_uring_zcrx_area_reg *area_reg)
{
struct udmabuf_create create;
int ret, devfd;
devfd = open("/dev/udmabuf", O_RDWR);
if (devfd < 0)
t_error(1, devfd, "Failed to open udmabuf dev");
memfd = memfd_create("udmabuf-test", MFD_ALLOW_SEALING);
if (memfd < 0)
t_error(1, memfd, "Failed to open udmabuf dev");
ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK);
if (ret < 0)
t_error(1, 0, "Failed to set seals");
ret = ftruncate(memfd, AREA_SIZE);
if (ret == -1)
t_error(1, 0, "Failed to resize udmabuf");
memset(&create, 0, sizeof(create));
create.memfd = memfd;
create.offset = 0;
create.size = AREA_SIZE;
dmabuf_fd = ioctl(devfd, UDMABUF_CREATE, &create);
if (dmabuf_fd < 0)
t_error(1, dmabuf_fd, "Failed to create udmabuf");
area_ptr = mmap(NULL, AREA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
dmabuf_fd, 0);
if (area_ptr == MAP_FAILED)
t_error(1, 0, "Failed to mmap udmabuf");
memset(area_reg, 0, sizeof(*area_reg));
area_reg->addr = 0; /* offset into dmabuf */
area_reg->len = AREA_SIZE;
area_reg->flags |= IORING_ZCRX_AREA_DMABUF;
area_reg->dmabuf_fd = dmabuf_fd;
close(devfd);
}
static void zcrx_populate_area(struct io_uring_zcrx_area_reg *area_reg)
{
unsigned flags = MAP_PRIVATE | MAP_ANONYMOUS;
unsigned prot = PROT_READ | PROT_WRITE;
if (cfg_area_type == AREA_TYPE_DMABUF) {
zcrx_populate_area_udmabuf(area_reg);
return;
}
if (cfg_area_type == AREA_TYPE_NORMAL) {
area_ptr = mmap(NULL, AREA_SIZE, prot,
flags, 0, 0);
} else if (cfg_area_type == AREA_TYPE_HUGE_PAGES) {
area_ptr = mmap(NULL, AREA_SIZE, prot,
flags | MAP_HUGETLB | MAP_HUGE_2MB, -1, 0);
}
if (area_ptr == MAP_FAILED)
t_error(1, 0, "mmap(): area allocation failed");
memset(area_reg, 0, sizeof(*area_reg));
area_reg->addr = uring_ptr_to_u64(area_ptr);
area_reg->len = AREA_SIZE;
area_reg->flags = 0;
}
static void setup_zcrx(struct io_uring *ring)
{
struct io_uring_zcrx_area_reg area_reg;
unsigned int ifindex;
unsigned int rq_entries = 4096;
unsigned rq_flags = 0;
int ret;
ifindex = if_nametoindex(cfg_ifname);
if (!ifindex)
t_error(1, 0, "bad interface name: %s", cfg_ifname);
ring_size = get_refill_ring_size(rq_entries);
ring_ptr = NULL;
if (cfg_rq_alloc_mode == RQ_ALLOC_USER) {
ring_ptr = mmap(NULL, ring_size,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
0, 0);
if (ring_ptr == MAP_FAILED)
t_error(1, 0, "mmap(): refill ring");
rq_flags |= IORING_MEM_REGION_TYPE_USER;
}
struct io_uring_region_desc region_reg = {
.size = ring_size,
.user_addr = uring_ptr_to_u64(ring_ptr),
.flags = rq_flags,
};
zcrx_populate_area(&area_reg);
struct io_uring_zcrx_ifq_reg reg = {
.if_idx = ifindex,
.if_rxq = cfg_queue_id,
.rq_entries = rq_entries,
.area_ptr = uring_ptr_to_u64(&area_reg),
.region_ptr = uring_ptr_to_u64(®ion_reg),
};
ret = io_uring_register_ifq(ring, ®);
if (ret)
t_error(1, 0, "io_uring_register_ifq(): %d", ret);
if (cfg_rq_alloc_mode == RQ_ALLOC_KERNEL) {
ring_ptr = mmap(NULL, ring_size,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE,
ring->ring_fd, region_reg.mmap_offset);
if (ring_ptr == MAP_FAILED)
t_error(1, 0, "mmap(): refill ring");
}
rq_ring.khead = (unsigned int *)((char *)ring_ptr + reg.offsets.head);
rq_ring.ktail = (unsigned int *)((char *)ring_ptr + reg.offsets.tail);
rq_ring.rqes = (struct io_uring_zcrx_rqe *)((char *)ring_ptr + reg.offsets.rqes);
rq_ring.rq_tail = 0;
rq_ring.ring_entries = reg.rq_entries;
zcrx_id = reg.zcrx_id;
area_token = area_reg.rq_area_token;
}
static void add_accept(struct io_uring *ring, int sockfd)
{
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
io_uring_prep_accept(sqe, sockfd, NULL, NULL, 0);
sqe->user_data = REQ_TYPE_ACCEPT;
}
static void add_recvzc(struct io_uring *ring, int sockfd, size_t len)
{
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
io_uring_prep_rw(IORING_OP_RECV_ZC, sqe, sockfd, NULL, len, 0);
sqe->ioprio |= IORING_RECV_MULTISHOT;
sqe->zcrx_ifq_idx = zcrx_id;
sqe->user_data = REQ_TYPE_RX;
}
static void process_accept(struct io_uring *ring, struct io_uring_cqe *cqe)
{
if (cqe->res < 0)
t_error(1, 0, "accept()");
if (connfd)
t_error(1, 0, "Unexpected second connection");
connfd = cqe->res;
add_recvzc(ring, connfd, cfg_size);
}
static void verify_data(char *data, size_t size, unsigned long seq)
{
int i;
if (!cfg_verify_data)
return;
for (i = 0; i < size; i++) {
char expected = 'a' + (seq + i) % 26;
if (data[i] != expected)
t_error(1, 0, "payload mismatch at %i: expected %i vs got %i, seq %li",
i, expected, data[i], seq);
}
}
static void process_recvzc(struct io_uring __attribute__((unused)) *ring,
struct io_uring_cqe *cqe)
{
unsigned rq_mask = rq_ring.ring_entries - 1;
struct io_uring_zcrx_cqe *rcqe;
struct io_uring_zcrx_rqe *rqe;
uint64_t mask;
char *data;
if (!(cqe->flags & IORING_CQE_F_MORE)) {
if (!cfg_size || cqe->res != 0)
t_error(1, 0, "invalid final recvzc ret %i", cqe->res);
if (received != cfg_size)
t_error(1, 0, "total receive size mismatch %lu / %lu",
received, cfg_size);
stop = true;
return;
}
if (cqe->res < 0)
t_error(1, 0, "recvzc(): %d", cqe->res);
rcqe = (struct io_uring_zcrx_cqe *)(cqe + 1);
mask = (1ULL << IORING_ZCRX_AREA_SHIFT) - 1;
data = (char *)area_ptr + (rcqe->off & mask);
verify_data(data, cqe->res, received);
received += cqe->res;
/* processed, return back to the kernel */
rqe = &rq_ring.rqes[rq_ring.rq_tail & rq_mask];
rqe->off = (rcqe->off & ~IORING_ZCRX_AREA_MASK) | area_token;
rqe->len = cqe->res;
io_uring_smp_store_release(rq_ring.ktail, ++rq_ring.rq_tail);
}
static void server_loop(struct io_uring *ring)
{
struct io_uring_cqe *cqe;
unsigned int head, count = 0;
io_uring_submit_and_wait(ring, 1);
io_uring_for_each_cqe(ring, head, cqe) {
switch (cqe->user_data & REQ_TYPE_MASK) {
case REQ_TYPE_ACCEPT:
process_accept(ring, cqe);
break;
case REQ_TYPE_RX:
process_recvzc(ring, cqe);
break;
default:
t_error(1, 0, "unknown cqe");
}
count++;
}
io_uring_cq_advance(ring, count);
}
static void run_server(void)
{
unsigned int flags = 0;
struct io_uring ring;
int fd, enable, ret;
fd = socket(AF_INET6, SOCK_STREAM, 0);
if (fd == -1)
t_error(1, 0, "socket()");
enable = 1;
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
if (ret < 0)
t_error(1, 0, "setsockopt(SO_REUSEADDR)");
ret = bind(fd, (struct sockaddr *)&cfg_addr, sizeof(cfg_addr));
if (ret < 0)
t_error(1, 0, "bind()");
if (listen(fd, 1024) < 0)
t_error(1, 0, "listen()");
flags |= IORING_SETUP_COOP_TASKRUN;
flags |= IORING_SETUP_SINGLE_ISSUER;
flags |= IORING_SETUP_DEFER_TASKRUN;
flags |= IORING_SETUP_SUBMIT_ALL;
flags |= IORING_SETUP_CQE32;
ret = io_uring_queue_init(512, &ring, flags);
if (ret)
t_error(1, ret, "ring init failed");
setup_zcrx(&ring);
add_accept(&ring, fd);
while (!stop)
server_loop(&ring);
}
static void usage(const char *filepath)
{
t_error(1, 0, "Usage: %s (-4|-6) -p<port> -i<ifname> -q<rxq_id>", filepath);
}
static void parse_opts(int argc, char **argv)
{
struct sockaddr_in6 *addr6 = (void *) &cfg_addr;
int c;
if (argc <= 1)
usage(argv[0]);
while ((c = getopt(argc, argv, "vp:i:q:s:r:A:")) != -1) {
switch (c) {
case 'p':
cfg_port = strtoul(optarg, NULL, 0);
break;
case 'i':
cfg_ifname = optarg;
break;
case 's':
cfg_size = strtoul(optarg, NULL, 0);
break;
case 'q':
cfg_queue_id = strtoul(optarg, NULL, 0);
break;
case 'v':
cfg_verify_data = true;
break;
case 'r':
cfg_rq_alloc_mode = strtoul(optarg, NULL, 0);
if (cfg_rq_alloc_mode >= __RQ_ALLOC_MAX)
t_error(1, 0, "invalid RQ allocation mode");
break;
case 'A':
cfg_area_type = strtoul(optarg, NULL, 0);
if (cfg_area_type >= __AREA_TYPE_MAX)
t_error(1, 0, "Invalid area type");
break;
}
}
memset(addr6, 0, sizeof(*addr6));
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(cfg_port);
addr6->sin6_addr = in6addr_any;
}
int main(int argc, char **argv)
{
page_size = sysconf(_SC_PAGESIZE);
if (page_size < 0) {
perror("sysconf(_SC_PAGESIZE)");
return 1;
}
parse_opts(argc, argv);
run_server();
return 0;
}
|