summaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/iopoll-sync.c
blob: b6c2832553e6d91fcfc9f577d17efb8f3815c8fa (plain) (blame)
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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: test uring_cmds for files that don't support iopoll
 * on IORING_SETUP_IOPOLL rings
 */

#include <liburing.h>
#include <stdio.h>
#include <sys/socket.h>

#include "helpers.h"

int main(void)
{
	int sockfd;
	int level = SOL_SOCKET;
	int optname = SO_REUSEADDR;
	int optval1, optval2, optval3;
	struct io_uring ring;
	int ret;
	struct io_uring_sqe *sqe;
	struct io_uring_cqe_iter cqe_iter;
	struct io_uring_cqe *cqe;

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) {
		fprintf(stderr, "socket() failed: %m\n");
		return T_EXIT_SKIP;
	}

	optval1 = 0;
	if (setsockopt(sockfd, level, optname, &optval1, sizeof(optval1)) < 0) {
		fprintf(stderr, "setsockopt() failed: %m\n");
		return T_EXIT_SKIP;
	}

	ret = t_create_ring(3, &ring, IORING_SETUP_IOPOLL);
	if (ret == T_SETUP_SKIP) {
		fprintf(stderr, "IORING_SETUP_IOPOLL not supported\n");
		return T_EXIT_SKIP;
	}
	if (ret)
		return T_EXIT_FAIL;

	optval1 = 123;
	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_cmd_sock(sqe, SOCKET_URING_OP_GETSOCKOPT, sockfd,
			       level, optname, &optval1, sizeof(optval1));
	sqe->flags |= IOSQE_IO_LINK;
	sqe->user_data = 1;

	optval2 = 1;
	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_cmd_sock(sqe, SOCKET_URING_OP_SETSOCKOPT, sockfd,
			       level, optname, &optval2, sizeof(optval2));
	sqe->flags |= IOSQE_IO_LINK;
	sqe->user_data = 2;

	optval3 = 123;
	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_cmd_sock(sqe, SOCKET_URING_OP_GETSOCKOPT, sockfd,
			       level, optname, &optval3, sizeof(optval3));
	sqe->user_data = 3;

	ret = io_uring_submit(&ring);
	if (ret != 3) {
		fprintf(stderr, "io_uring_submit() returned %d\n", ret);
		return T_EXIT_FAIL;
	}

	cqe_iter = io_uring_cqe_iter_init(&ring);
	if (!io_uring_cqe_iter_next(&cqe_iter, &cqe)) {
		fprintf(stderr, "No CQE available\n");
		return T_EXIT_FAIL;
	}
	if (cqe->user_data != 1) {
		fprintf(stderr, "No CQE for user_data 1\n");
		return T_EXIT_FAIL;
	}
	if (cqe->res == -EOPNOTSUPP || cqe->res == -EINVAL)
		return T_EXIT_SKIP;
	if (cqe->res != sizeof(optval1)) {
		fprintf(stderr, "GETSOCKOPT returned %d\n", cqe->res);
		return T_EXIT_FAIL;
	}
	if (optval1 != 0) {
		fprintf(stderr, "optval %d != 0\n", optval1);
		return T_EXIT_FAIL;
	}

	if (!io_uring_cqe_iter_next(&cqe_iter, &cqe)) {
		fprintf(stderr, "Only 1 CQE available\n");
		return T_EXIT_FAIL;
	}
	if (cqe->user_data != 2) {
		fprintf(stderr, "No CQE for user_data 2\n");
		return T_EXIT_FAIL;
	}
	if (cqe->res) {
		fprintf(stderr, "SETSOCKOPT returned %d\n", cqe->res);
		return T_EXIT_FAIL;
	}

	if (!io_uring_cqe_iter_next(&cqe_iter, &cqe)) {
		fprintf(stderr, "Only 2 CQEs available\n");
		return T_EXIT_FAIL;
	}
	if (cqe->user_data != 3) {
		fprintf(stderr, "No CQE for user_data 3\n");
		return T_EXIT_FAIL;
	}
	if (cqe->res != sizeof(optval3)) {
		fprintf(stderr, "GETSOCKOPT returned %d\n", cqe->res);
		return T_EXIT_FAIL;
	}
	if (optval3 != 1) {
		fprintf(stderr, "optval %d != 1\n", optval3);
		return T_EXIT_FAIL;
	}

	if (io_uring_cqe_iter_next(&cqe_iter, &cqe)) {
		fprintf(stderr, "More than 3 CQEs available");
		return T_EXIT_FAIL;
	}

	return T_EXIT_PASS;
}