summaryrefslogtreecommitdiff
path: root/test/io_uring_setup.c
blob: 2b76402f04a11cb11b02f9dcc8638d9279143b21 (plain)
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
/*
 * 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 "../src/liburing.h"

/*
 * 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));
		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 0x%.8x 0x%.8x", p->resv[0],
		p->resv[1], p->resv[2], p->resv[3], p->resv[4]);

	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 error)
{
	int ret;

	printf("io_uring_setup(%u, %p), flags: %s, resv: %s, sq_thread_cpu: %u\n",
	       entries, p, flags_string(p), dump_resv(p),
	       p ? p->sq_thread_cpu : 0);

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

	return 0;
}

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

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

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

	/* invalid flags */
	memset(&p, 0, sizeof(p));
	p.flags = ~0U;
	status |= try_io_uring_setup(1, &p, -1, 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, -1, 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, -1, 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) {
		printf("io_uring_setup failed with %d, expected success\n",
		       errno);
		status = 1;
	} else {
		char buf[4096];
		int ret;
		ret = read(fd, buf, 4096);
		if (ret >= 0) {
			printf("read from io_uring fd succeeded.  expected fail\n");
			status = 1;
		}
	}

	if (!status) {
		printf("PASS\n");
		return 0;
	}

	printf("FAIL\n");
	return -1;
}