aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/io_uring_setup.c
blob: 3d5a6c4bcaaadb7f4068bd5813575f14352315c2 (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
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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * io_uring_setup.c
 *
 * Description: Unit tests for the io_uring_setup system call.
 *
 * Copyright 2019, Red Hat, Inc.
 * Author: Jeff Moyer <jmoyer@redhat.com>
 */
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/sysinfo.h>
#include "liburing.h"
#include "helpers.h"

#include "../syscall.h"

char *features_string(struct io_uring_params *p)
{
	static char flagstr[64];

	if (!p || !p->features)
		return "none";

	if (p->features & ~IORING_FEAT_SINGLE_MMAP) {
		snprintf(flagstr, 64, "0x%.8x", p->features);
		return flagstr;
	}

	if (p->features & IORING_FEAT_SINGLE_MMAP)
		strncat(flagstr, "IORING_FEAT_SINGLE_MMAP", 64 - strlen(flagstr));

	return flagstr;
}

/*
 * Attempt the call with the given args.  Return 0 when expect matches
 * the return value of the system call, 1 otherwise.
 */
char *
flags_string(struct io_uring_params *p)
{
	static char flagstr[64];
	int add_pipe = 0;

	memset(flagstr, 0, sizeof(flagstr));

	if (!p || p->flags == 0)
		return "none";

	/*
	 * If unsupported flags are present, just print the bitmask.
	 */
	if (p->flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
			 IORING_SETUP_SQ_AFF)) {
		snprintf(flagstr, 64, "0x%.8x", p->flags);
		return flagstr;
	}

	if (p->flags & IORING_SETUP_IOPOLL) {
		strncat(flagstr, "IORING_SETUP_IOPOLL", 64 - strlen(flagstr));
		add_pipe = 1;
	}
	if (p->flags & IORING_SETUP_SQPOLL) {
		if (add_pipe)
			strncat(flagstr, "|", 64 - strlen(flagstr));
		else
			add_pipe = 1;
		strncat(flagstr, "IORING_SETUP_SQPOLL", 64 - strlen(flagstr));
	}
	if (p->flags & IORING_SETUP_SQ_AFF) {
		if (add_pipe)
			strncat(flagstr, "|", 64 - strlen(flagstr));
		strncat(flagstr, "IORING_SETUP_SQ_AFF", 64 - strlen(flagstr));
	}

	return flagstr;
}

char *
dump_resv(struct io_uring_params *p)
{
	static char resvstr[4096];

	if (!p)
		return "";

	sprintf(resvstr, "0x%.8x 0x%.8x 0x%.8x", p->resv[0],
		p->resv[1], p->resv[2]);

	return resvstr;
}

/* bogus: setup returns a valid fd on success... expect can't predict the
   fd we'll get, so this really only takes 1 parameter: error */
int
try_io_uring_setup(unsigned entries, struct io_uring_params *p, int expect)
{
	int ret;

	ret = io_uring_setup(entries, p);
	if (ret != expect) {
		fprintf(stderr, "expected %d, got %d\n", expect, ret);
		/* if we got a valid uring, close it */
		if (ret > 0)
			close(ret);
		return 1;
	}

	if (expect < 0 && expect != ret) {
		if (ret == -EPERM && geteuid() != 0) {
			printf("Needs root, not flagging as an error\n");
			return 0;
		}
		fprintf(stderr, "expected errno %d, got %d\n", expect, ret);
		return 1;
	}

	return 0;
}

int
main(int argc, char **argv)
{
	int fd;
	unsigned int status = 0;
	struct io_uring_params p;

	if (argc > 1)
		return T_EXIT_SKIP;

	memset(&p, 0, sizeof(p));
	status |= try_io_uring_setup(0, &p, -EINVAL);
	status |= try_io_uring_setup(1, NULL, -EFAULT);

	/* resv array is non-zero */
	memset(&p, 0, sizeof(p));
	p.resv[0] = p.resv[1] = p.resv[2] = 1;
	status |= try_io_uring_setup(1, &p, -EINVAL);

	/* invalid flags */
	memset(&p, 0, sizeof(p));
	p.flags = ~0U;
	status |= try_io_uring_setup(1, &p, -EINVAL);

	/* IORING_SETUP_SQ_AFF set but not IORING_SETUP_SQPOLL */
	memset(&p, 0, sizeof(p));
	p.flags = IORING_SETUP_SQ_AFF;
	status |= try_io_uring_setup(1, &p, -EINVAL);

	/* attempt to bind to invalid cpu */
	memset(&p, 0, sizeof(p));
	p.flags = IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF;
	p.sq_thread_cpu = get_nprocs_conf();
	status |= try_io_uring_setup(1, &p, -EINVAL);

	/* I think we can limit a process to a set of cpus.  I assume
	 * we shouldn't be able to setup a kernel thread outside of that.
	 * try to do that. (task->cpus_allowed) */

	/* read/write on io_uring_fd */
	memset(&p, 0, sizeof(p));
	fd = io_uring_setup(1, &p);
	if (fd < 0) {
		fprintf(stderr, "io_uring_setup failed with %d, expected success\n",
		       -fd);
		status = 1;
	} else {
		char buf[4096];
		int ret;
		ret = read(fd, buf, 4096);
		if (ret >= 0) {
			fprintf(stderr, "read from io_uring fd succeeded.  expected fail\n");
			status = 1;
		}
	}

	if (!status)
		return T_EXIT_PASS;

	fprintf(stderr, "FAIL\n");
	return T_EXIT_FAIL;
}