aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/reg-fd-only.c
blob: 1ff6ea250ead8965822a62e9668e79a26588eff1 (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
129
130
131
132
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Test io_uring_setup with IORING_SETUP_REGISTERED_FD_ONLY
 *
 */
#include <stdio.h>

#include "helpers.h"

#define NORMAL_PAGE_ENTRIES	8
#define HUGE_PAGE_ENTRIES	512

static int no_mmap;

static int test_nops(struct io_uring *ring, int sq_size, int nr_nops)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int i, ret;

	do {
		int todo = nr_nops;

		if (todo > sq_size)
			todo = sq_size;

		for (i = 0; i < todo; i++) {
			sqe = io_uring_get_sqe(ring);
			io_uring_prep_nop(sqe);
		}

		ret = io_uring_submit(ring);
		if (ret != todo) {
			fprintf(stderr, "short submit %d\n", ret);
			return T_EXIT_FAIL;
		}

		for (i = 0; i < todo; i++) {
			ret = io_uring_wait_cqe(ring, &cqe);
			if (ret) {
				fprintf(stderr, "wait err %d\n", ret);
				return T_EXIT_FAIL;
			}
			io_uring_cqe_seen(ring, cqe);
		}
		nr_nops -= todo;
	} while (nr_nops);

	return T_EXIT_PASS;
}

static int test(int nentries)
{
	struct io_uring ring;
	unsigned values[2];
	int ret;

	ret = io_uring_queue_init(nentries, &ring,
			IORING_SETUP_REGISTERED_FD_ONLY | IORING_SETUP_NO_MMAP);
	if (ret == -EINVAL) {
		no_mmap = 1;
		return T_EXIT_SKIP;
	} else if (ret == -ENOMEM) {
		fprintf(stdout, "Enable huge pages to test big rings\n");
		return T_EXIT_SKIP;
	} else if (ret) {
		fprintf(stderr, "ring setup failed\n");
		return T_EXIT_FAIL;
	}

	ret = io_uring_register_ring_fd(&ring);
	if (ret != -EEXIST) {
		fprintf(stderr, "registering already-registered ring fd should fail\n");
		goto err;
	}

	ret = io_uring_close_ring_fd(&ring);
	if (ret != -EBADF) {
		fprintf(stderr, "closing already-closed ring fd should fail\n");
		goto err;
	}

	/* Test a simple io_uring_register operation expected to work.
	 * io_uring_register_iowq_max_workers is arbitrary.
	 */
	values[0] = values[1] = 0;
	ret = io_uring_register_iowq_max_workers(&ring, values);
	if (ret || (values[0] == 0 && values[1] == 0)) {
		fprintf(stderr, "io_uring_register operation failed after closing ring fd\n");
		goto err;
	}

	ret = test_nops(&ring, nentries, nentries * 4);
	if (ret)
		goto err;

	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;

err:
	io_uring_queue_exit(&ring);
	return T_EXIT_FAIL;
}

int main(int argc, char *argv[])
{
	int ret;

	if (argc > 1)
		return T_EXIT_SKIP;

	/* test single normal page */
	ret = test(NORMAL_PAGE_ENTRIES);
	if (ret == T_EXIT_SKIP || no_mmap) {
		return T_EXIT_SKIP;
	} else if (ret != T_EXIT_PASS) {
		fprintf(stderr, "test 8 failed\n");
		return T_EXIT_FAIL;
	}

	/* test with entries requiring a huge page */
	ret = test(HUGE_PAGE_ENTRIES);
	if (ret == T_EXIT_SKIP) {
		return T_EXIT_SKIP;
	} else if (ret != T_EXIT_PASS) {
		fprintf(stderr, "test 512 failed\n");
		return T_EXIT_FAIL;
	}

	return T_EXIT_PASS;
}