Skip to content

Commit 6bec161

Browse files
committed
examples/zcrx: multi connection
Signed-off-by: Pavel Begunkov <[email protected]>
1 parent 5f34c4f commit 6bec161

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

examples/zcrx.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ static __u32 zcrx_id;
9898
static int dmabuf_fd;
9999
static int memfd;
100100

101+
static int listen_fd;
102+
101103
static struct zc_conn *get_connection(__u64 user_data)
102104
{
103105
user_data &= ~REQ_TYPE_MASK;
@@ -292,18 +294,24 @@ static void process_accept(struct io_uring *ring, struct io_uring_cqe *cqe)
292294
{
293295
struct zc_conn *conn;
294296

297+
if (cqe->res < 0) {
298+
printf("Accept failed %i, terminate\n", cqe->res);
299+
stop = false;
300+
return;
301+
}
302+
295303
conn = aligned_alloc(64, sizeof(*conn));
296304
if (!conn)
297305
t_error(1, 0, "can't allocate conn structure");
298-
if (cqe->res < 0)
299-
t_error(1, 0, "accept()");
300306
if (conn->sockfd)
301307
t_error(1, 0, "Unexpected second connection");
302308

303309
memset(conn, 0, sizeof(*conn));
304310
conn->sockfd = cqe->res;
305311
print_socket_info(conn->sockfd);
306312
add_recvzc(ring, conn, cfg_size);
313+
314+
add_accept(ring, listen_fd);
307315
}
308316

309317
static void verify_data(__u8 *data, size_t size, unsigned long seq)
@@ -385,7 +393,6 @@ static void process_recvzc_error(struct io_uring *ring,
385393

386394
close(conn->sockfd);
387395
free(conn);
388-
stop = true;
389396
}
390397

391398
static void process_recvzc(struct io_uring *ring,
@@ -444,25 +451,24 @@ static void run_server(void)
444451
{
445452
struct io_uring_params p;
446453
struct io_uring ring;
447-
int fd, enable, ret;
454+
int enable, ret;
448455

449-
fd = socket(AF_INET6, SOCK_STREAM, 0);
450-
if (fd == -1)
456+
listen_fd = socket(AF_INET6, SOCK_STREAM, 0);
457+
if (listen_fd == -1)
451458
t_error(1, 0, "socket()");
452459

453460
enable = 1;
454-
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
461+
ret = setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
455462
if (ret < 0)
456463
t_error(1, 0, "setsockopt(SO_REUSEADDR)");
457464

458-
ret = bind(fd, (struct sockaddr *)&cfg_addr, sizeof(cfg_addr));
465+
ret = bind(listen_fd, (struct sockaddr *)&cfg_addr, sizeof(cfg_addr));
459466
if (ret < 0)
460467
t_error(1, 0, "bind()");
461468

462-
if (listen(fd, 1024) < 0)
469+
if (listen(listen_fd, 1024) < 0)
463470
t_error(1, 0, "listen()");
464471

465-
466472
memset(&p, 0, sizeof(p));
467473
p.flags |= IORING_SETUP_COOP_TASKRUN;
468474
p.flags |= IORING_SETUP_SINGLE_ISSUER;
@@ -477,12 +483,12 @@ static void run_server(void)
477483
t_error(1, ret, "ring init failed");
478484

479485
setup_zcrx(&ring);
480-
add_accept(&ring, fd);
486+
add_accept(&ring, listen_fd);
481487

482488
while (!stop)
483489
server_loop(&ring);
484490

485-
close(fd);
491+
close(listen_fd);
486492
}
487493

488494
static void usage(const char *filepath)

0 commit comments

Comments
 (0)