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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/* SPDX-License-Identifier: MIT */
/*
* Check that CMD operations on sockets are consistent.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include "liburing.h"
#include "helpers.h"
#define USERDATA 0x1234
#define MSG "foobarbaz"
static int no_io_cmd;
struct fds {
int tx;
int rx;
};
/* Create 2 sockets (tx, rx) given the socket type */
static struct fds create_sockets(bool stream)
{
struct fds retval;
int fd[2];
t_create_socket_pair(fd, stream);
retval.tx = fd[0];
retval.rx = fd[1];
return retval;
}
static int create_sqe_and_submit(struct io_uring *ring, int32_t fd, int op)
{
struct io_uring_sqe *sqe;
int ret;
assert(fd > 0);
sqe = io_uring_get_sqe(ring);
assert(sqe != NULL);
io_uring_prep_cmd_sock(sqe, op, fd, 0, 0, NULL, 0);
sqe->user_data = USERDATA;
/* Submitting SQE */
ret = io_uring_submit_and_wait(ring, 1);
if (ret <= 0)
return ret;
return 0;
}
static int receive_cqe(struct io_uring *ring)
{
struct io_uring_cqe *cqe;
int err;
err = io_uring_wait_cqe(ring, &cqe);
assert(err == 0);
assert(cqe->user_data == USERDATA);
err = cqe->res;
io_uring_cqe_seen(ring, cqe);
/* Return the result of the operation */
return err;
}
static ssize_t send_data(struct fds *s, char *str)
{
size_t written_bytes;
written_bytes = write(s->tx, str, strlen(str));
assert(written_bytes == strlen(MSG));
return written_bytes;
}
static int run_test(bool stream)
{
struct fds sockfds;
ssize_t bytes_in, bytes_out;
struct io_uring ring;
size_t written_bytes;
int error;
/* Create three sockets */
sockfds = create_sockets(stream);
assert(sockfds.tx > 0);
assert(sockfds.rx > 0);
/* Send data sing the sockfds->send */
written_bytes = send_data(&sockfds, MSG);
/* Simply io_uring ring creation */
error = t_create_ring(1, &ring, 0);
if (error == T_SETUP_SKIP)
return error;
else if (error != T_SETUP_OK)
return T_EXIT_FAIL;
error = create_sqe_and_submit(&ring, sockfds.rx,
SOCKET_URING_OP_SIOCINQ);
if (error)
return T_EXIT_FAIL;
bytes_in = receive_cqe(&ring);
if (bytes_in < 0) {
if (bytes_in == -EINVAL || bytes_in == -EOPNOTSUPP) {
no_io_cmd = 1;
return T_EXIT_SKIP;
}
fprintf(stderr, "Bad return value %ld\n", (long) bytes_in);
return T_EXIT_FAIL;
}
error = create_sqe_and_submit(&ring, sockfds.tx,
SOCKET_URING_OP_SIOCOUTQ);
if (error)
return T_EXIT_FAIL;
bytes_out = receive_cqe(&ring);
if (bytes_in == -ENOTSUP || bytes_out == -ENOTSUP) {
fprintf(stderr, "Skipping tests. -ENOTSUP returned\n");
return T_EXIT_SKIP;
}
/*
* Assert the number of written bytes are either in the socket buffer
* or on the receive side
*/
if (bytes_in + bytes_out != written_bytes) {
fprintf(stderr, "values does not match: %zu+%zu != %zu\n",
bytes_in, bytes_out, written_bytes);
return T_EXIT_FAIL;
}
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
}
/*
* Make sure that siocoutq and siocinq returns the same value
* using ioctl(2) and uring commands for raw sockets
*/
static int run_test_raw(void)
{
int ioctl_siocoutq, ioctl_siocinq;
int uring_siocoutq, uring_siocinq;
struct io_uring ring;
int retry = 0, sock, error;
sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
if (sock == -1) {
/* You need root to create raw socket */
perror("Not able to create a raw socket");
return T_EXIT_SKIP;
}
/* Get the same operation using uring cmd */
error = t_create_ring(1, &ring, 0);
if (error == T_SETUP_SKIP)
return error;
else if (error != T_SETUP_OK)
return T_EXIT_FAIL;
again:
/* Simple SIOCOUTQ using ioctl */
error = ioctl(sock, SIOCOUTQ, &ioctl_siocoutq);
if (error < 0) {
fprintf(stderr, "Failed to run ioctl(SIOCOUTQ): %d\n", error);
return T_EXIT_FAIL;
}
error = ioctl(sock, SIOCINQ, &ioctl_siocinq);
if (error < 0) {
fprintf(stderr, "Failed to run ioctl(SIOCINQ): %d\n", error);
return T_EXIT_FAIL;
}
create_sqe_and_submit(&ring, sock, SOCKET_URING_OP_SIOCOUTQ);
uring_siocoutq = receive_cqe(&ring);
create_sqe_and_submit(&ring, sock, SOCKET_URING_OP_SIOCINQ);
uring_siocinq = receive_cqe(&ring);
/* Compare that both values (ioctl and uring CMD) should be similar */
if (uring_siocoutq != ioctl_siocoutq) {
if (!retry) {
retry = 1;
goto again;
}
fprintf(stderr, "values does not match: %d != %d\n",
uring_siocoutq, ioctl_siocoutq);
return T_EXIT_FAIL;
}
if (uring_siocinq != ioctl_siocinq) {
if (!retry) {
retry = 1;
goto again;
}
fprintf(stderr, "values does not match: %d != %d\n",
uring_siocinq, ioctl_siocinq);
return T_EXIT_FAIL;
}
return T_EXIT_PASS;
}
int main(int argc, char *argv[])
{
int err;
if (argc > 1)
return 0;
/* Test SOCK_STREAM */
err = run_test(true);
if (err)
return err;
if (no_io_cmd)
return T_EXIT_SKIP;
/* Test SOCK_DGRAM */
err = run_test(false);
if (err)
return err;
/* Test raw sockets */
return run_test_raw();
}
|