12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109 |
- #include "../config-host.h"
- /* SPDX-License-Identifier: MIT */
- /*
- * Description: run various linked timeout cases
- *
- */
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <poll.h>
- #include "liburing.h"
- #include "helpers.h"
- static int test_fail_lone_link_timeouts(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_link_timeout(sqe, &ts, 0);
- ts.tv_sec = 1;
- ts.tv_nsec = 0;
- sqe->user_data = 1;
- sqe->flags |= IOSQE_IO_LINK;
- ret = io_uring_submit(ring);
- if (ret != 1) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- if (cqe->user_data != 1) {
- fprintf(stderr, "invalid user data %d\n", cqe->res);
- goto err;
- }
- if (cqe->res != -EINVAL) {
- fprintf(stderr, "got %d, wanted -EINVAL\n", cqe->res);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- return 0;
- err:
- return 1;
- }
- static int test_fail_two_link_timeouts(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int ret, i, nr_wait;
- ts.tv_sec = 1;
- ts.tv_nsec = 0;
- /*
- * sqe_1: write destined to fail
- * use buf=NULL, to do that during the issuing stage
- */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_writev(sqe, 0, NULL, 1, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- /* sqe_2: valid linked timeout */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->user_data = 2;
- sqe->flags |= IOSQE_IO_LINK;
- /* sqe_3: invalid linked timeout */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 3;
- /* sqe_4: invalid linked timeout */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 4;
- ret = io_uring_submit(ring);
- if (ret < 3) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- nr_wait = ret;
- for (i = 0; i < nr_wait; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- if (cqe->res != -EFAULT && cqe->res != -ECANCELED) {
- fprintf(stderr, "write got %d, wanted -EFAULT "
- "or -ECANCELED\n", cqe->res);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Link timeout got %d, wanted -ECACNCELED\n", cqe->res);
- goto err;
- }
- break;
- case 3:
- /* fall through */
- case 4:
- if (cqe->res != -ECANCELED && cqe->res != -EINVAL) {
- fprintf(stderr, "Invalid link timeout got %d"
- ", wanted -ECACNCELED || -EINVAL\n", cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test linked timeout with timeout (timeoutception)
- */
- static int test_single_link_timeout_ception(struct io_uring *ring)
- {
- struct __kernel_timespec ts1, ts2;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int ret, i;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts1.tv_sec = 1;
- ts1.tv_nsec = 0;
- io_uring_prep_timeout(sqe, &ts1, -1U, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts2.tv_sec = 2;
- ts2.tv_nsec = 0;
- io_uring_prep_link_timeout(sqe, &ts2, 0);
- sqe->user_data = 2;
- ret = io_uring_submit(ring);
- if (ret != 2) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 2; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- /* newer kernels allow timeout links */
- if (cqe->res != -EINVAL && cqe->res != -ETIME) {
- fprintf(stderr, "Timeout got %d, wanted "
- "-EINVAL or -ETIME\n", cqe->res);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Link timeout got %d, wanted -ECANCELED\n", cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test linked timeout with NOP
- */
- static int test_single_link_timeout_nop(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int ret, i;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 1;
- ts.tv_nsec = 0;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->user_data = 2;
- ret = io_uring_submit(ring);
- if (ret != 2) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 2; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- if (cqe->res) {
- fprintf(stderr, "NOP got %d, wanted 0\n", cqe->res);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Link timeout got %d, wanted -ECACNCELED\n", cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test read that will not complete, with a linked timeout behind it that
- * has errors in the SQE
- */
- static int test_single_link_timeout_error(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int fds[2], ret, i;
- struct iovec iov;
- char buffer[128];
- if (pipe(fds)) {
- perror("pipe");
- return 1;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- iov.iov_base = buffer;
- iov.iov_len = sizeof(buffer);
- io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 1;
- ts.tv_nsec = 0;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- /* set invalid field, it'll get failed */
- sqe->ioprio = 89;
- sqe->user_data = 2;
- ret = io_uring_submit(ring);
- if (ret != 2) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 2; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Read got %d, wanted -ECANCELED\n",
- cqe->res);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -EINVAL) {
- fprintf(stderr, "Link timeout got %d, wanted -EINVAL\n", cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test read that will complete, with a linked timeout behind it
- */
- static int test_single_link_no_timeout(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int fds[2], ret, i;
- struct iovec iov;
- char buffer[128];
- if (pipe(fds)) {
- perror("pipe");
- return 1;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- iov.iov_base = buffer;
- iov.iov_len = sizeof(buffer);
- io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 1;
- ts.tv_nsec = 0;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- iov.iov_base = buffer;
- iov.iov_len = sizeof(buffer);
- io_uring_prep_writev(sqe, fds[1], &iov, 1, 0);
- sqe->user_data = 3;
- ret = io_uring_submit(ring);
- if (ret != 3) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- case 3:
- if (cqe->res != sizeof(buffer)) {
- fprintf(stderr, "R/W got %d, wanted %d\n", cqe->res,
- (int) sizeof(buffer));
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Link timeout %d, wanted -ECANCELED\n",
- cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test read that will not complete, with a linked timeout behind it
- */
- static int test_single_link_timeout(struct io_uring *ring, unsigned nsec)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int fds[2], ret, i;
- struct iovec iov;
- char buffer[128];
- if (pipe(fds)) {
- perror("pipe");
- return 1;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- iov.iov_base = buffer;
- iov.iov_len = sizeof(buffer);
- io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 0;
- ts.tv_nsec = nsec;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->user_data = 2;
- ret = io_uring_submit(ring);
- if (ret != 2) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 2; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- if (cqe->res != -EINTR && cqe->res != -ECANCELED) {
- fprintf(stderr, "Read got %d\n", cqe->res);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -EALREADY && cqe->res != -ETIME &&
- cqe->res != 0) {
- fprintf(stderr, "Link timeout got %d\n", cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- close(fds[0]);
- close(fds[1]);
- return 0;
- err:
- return 1;
- }
- static int test_timeout_link_chain1(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int fds[2], ret, i;
- struct iovec iov;
- char buffer[128];
- if (pipe(fds)) {
- perror("pipe");
- return 1;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- iov.iov_base = buffer;
- iov.iov_len = sizeof(buffer);
- io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 0;
- ts.tv_nsec = 1000000;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->user_data = 3;
- ret = io_uring_submit(ring);
- if (ret != 3) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- if (cqe->res != -EINTR && cqe->res != -ECANCELED) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- case 2:
- /* FASTPOLL kernels can cancel successfully */
- if (cqe->res != -EALREADY && cqe->res != -ETIME) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- case 3:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- close(fds[0]);
- close(fds[1]);
- return 0;
- err:
- return 1;
- }
- static int test_timeout_link_chain2(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int fds[2], ret, i;
- if (pipe(fds)) {
- perror("pipe");
- return 1;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_poll_add(sqe, fds[0], POLLIN);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 0;
- ts.tv_nsec = 1000000;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 3;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->user_data = 4;
- ret = io_uring_submit(ring);
- if (ret != 4) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 4; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- /* poll cancel really should return -ECANCEL... */
- case 1:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -ETIME) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- case 3:
- case 4:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- close(fds[0]);
- close(fds[1]);
- return 0;
- err:
- return 1;
- }
- static int test_timeout_link_chain3(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int fds[2], ret, i;
- if (pipe(fds)) {
- perror("pipe");
- return 1;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_poll_add(sqe, fds[0], POLLIN);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 0;
- ts.tv_nsec = 1000000;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 3;
- /* POLL -> TIMEOUT -> NOP */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_poll_add(sqe, fds[0], POLLIN);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 4;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 0;
- ts.tv_nsec = 1000000;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->user_data = 5;
- /* poll on pipe + timeout */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->user_data = 6;
- /* nop */
- ret = io_uring_submit(ring);
- if (ret != 6) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 6; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 2:
- if (cqe->res != -ETIME) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- case 1:
- case 3:
- case 4:
- case 5:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- case 6:
- if (cqe->res) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- close(fds[0]);
- close(fds[1]);
- return 0;
- err:
- return 1;
- }
- static int test_timeout_link_chain4(struct io_uring *ring)
- {
- struct __kernel_timespec ts;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int fds[2], ret, i;
- if (pipe(fds)) {
- perror("pipe");
- return 1;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_poll_add(sqe, fds[0], POLLIN);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 0;
- ts.tv_nsec = 1000000;
- io_uring_prep_link_timeout(sqe, &ts, 0);
- sqe->user_data = 3;
- ret = io_uring_submit(ring);
- if (ret != 3) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- /* poll cancel really should return -ECANCEL... */
- case 1:
- if (cqe->res) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- case 3:
- if (cqe->res != -ETIME) {
- fprintf(stderr, "Req %" PRIu64 " got %d\n", (uint64_t) cqe->user_data,
- cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- close(fds[0]);
- close(fds[1]);
- return 0;
- err:
- return 1;
- }
- static int test_timeout_link_chain5(struct io_uring *ring)
- {
- struct __kernel_timespec ts1, ts2;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int ret, i;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts1.tv_sec = 1;
- ts1.tv_nsec = 0;
- io_uring_prep_link_timeout(sqe, &ts1, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- ts2.tv_sec = 2;
- ts2.tv_nsec = 0;
- io_uring_prep_link_timeout(sqe, &ts2, 0);
- sqe->user_data = 3;
- ret = io_uring_submit(ring);
- if (ret != 3) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- case 2:
- if (cqe->res && cqe->res != -ECANCELED) {
- fprintf(stderr, "Request got %d, wanted -EINVAL "
- "or -ECANCELED\n",
- cqe->res);
- goto err;
- }
- break;
- case 3:
- if (cqe->res != -ECANCELED && cqe->res != -EINVAL) {
- fprintf(stderr, "Link timeout got %d, wanted -ECANCELED\n", cqe->res);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- int main(int argc, char *argv[])
- {
- struct io_uring ring;
- int ret;
- if (argc > 1)
- return T_EXIT_SKIP;
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- printf("ring setup failed\n");
- return T_EXIT_FAIL;
- }
- ret = test_timeout_link_chain1(&ring);
- if (ret) {
- printf("test_single_link_chain1 failed\n");
- return ret;
- }
- ret = test_timeout_link_chain2(&ring);
- if (ret) {
- printf("test_single_link_chain2 failed\n");
- return ret;
- }
- ret = test_timeout_link_chain3(&ring);
- if (ret) {
- printf("test_single_link_chain3 failed\n");
- return ret;
- }
- ret = test_timeout_link_chain4(&ring);
- if (ret) {
- printf("test_single_link_chain4 failed\n");
- return ret;
- }
- ret = test_timeout_link_chain5(&ring);
- if (ret) {
- printf("test_single_link_chain5 failed\n");
- return ret;
- }
- ret = test_single_link_timeout(&ring, 10);
- if (ret) {
- printf("test_single_link_timeout 10 failed\n");
- return ret;
- }
- ret = test_single_link_timeout(&ring, 100000ULL);
- if (ret) {
- printf("test_single_link_timeout 100000 failed\n");
- return ret;
- }
- ret = test_single_link_timeout(&ring, 500000000ULL);
- if (ret) {
- printf("test_single_link_timeout 500000000 failed\n");
- return ret;
- }
- ret = test_single_link_no_timeout(&ring);
- if (ret) {
- printf("test_single_link_no_timeout failed\n");
- return ret;
- }
- ret = test_single_link_timeout_error(&ring);
- if (ret) {
- printf("test_single_link_timeout_error failed\n");
- return ret;
- }
- ret = test_single_link_timeout_nop(&ring);
- if (ret) {
- printf("test_single_link_timeout_nop failed\n");
- return ret;
- }
- ret = test_single_link_timeout_ception(&ring);
- if (ret) {
- printf("test_single_link_timeout_ception failed\n");
- return ret;
- }
- ret = test_fail_lone_link_timeouts(&ring);
- if (ret) {
- printf("test_fail_lone_link_timeouts failed\n");
- return ret;
- }
- ret = test_fail_two_link_timeouts(&ring);
- if (ret) {
- printf("test_fail_two_link_timeouts failed\n");
- return ret;
- }
- return T_EXIT_PASS;
- }
|