12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799 |
- #include "../config-host.h"
- /* SPDX-License-Identifier: MIT */
- /*
- * Description: run various timeout tests
- *
- */
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/time.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include "helpers.h"
- #include "liburing.h"
- #include "../src/syscall.h"
- #define TIMEOUT_MSEC 200
- static int not_supported;
- static int no_modify;
- static int no_multishot;
- static void msec_to_ts(struct __kernel_timespec *ts, unsigned int msec)
- {
- ts->tv_sec = msec / 1000;
- ts->tv_nsec = (msec % 1000) * 1000000;
- }
- static unsigned long long mtime_since(const struct timeval *s,
- const struct timeval *e)
- {
- long long sec, usec;
- sec = e->tv_sec - s->tv_sec;
- usec = (e->tv_usec - s->tv_usec);
- if (sec > 0 && usec < 0) {
- sec--;
- usec += 1000000;
- }
- sec *= 1000;
- usec /= 1000;
- return sec + usec;
- }
- static unsigned long long mtime_since_now(struct timeval *tv)
- {
- struct timeval end;
- gettimeofday(&end, NULL);
- return mtime_since(tv, &end);
- }
- /*
- * Test that we return to userspace if a timeout triggers, even if we
- * don't satisfy the number of events asked for.
- */
- static int test_single_timeout_many(struct io_uring *ring)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- unsigned long long exp;
- struct __kernel_timespec ts;
- struct timeval tv;
- int ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, TIMEOUT_MSEC);
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- gettimeofday(&tv, NULL);
- ret = __sys_io_uring_enter(ring->ring_fd, 0, 4, IORING_ENTER_GETEVENTS,
- NULL);
- if (ret < 0) {
- fprintf(stderr, "%s: io_uring_enter %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- io_uring_cqe_seen(ring, cqe);
- if (ret == -EINVAL) {
- fprintf(stdout, "Timeout not supported, ignored\n");
- not_supported = 1;
- return 0;
- } else if (ret != -ETIME) {
- fprintf(stderr, "Timeout: %s\n", strerror(-ret));
- goto err;
- }
- exp = mtime_since_now(&tv);
- if (exp >= TIMEOUT_MSEC / 2 && exp <= (TIMEOUT_MSEC * 3) / 2)
- return 0;
- fprintf(stderr, "%s: Timeout seems wonky (got %llu)\n", __FUNCTION__, exp);
- err:
- return 1;
- }
- /*
- * Test numbered trigger of timeout
- */
- static int test_single_timeout_nr(struct io_uring *ring, int nr)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- int i, ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, TIMEOUT_MSEC);
- io_uring_prep_timeout(sqe, &ts, nr, 0);
- sqe = io_uring_get_sqe(ring);
- io_uring_prep_nop(sqe);
- io_uring_sqe_set_data(sqe, (void *) 1);
- sqe = io_uring_get_sqe(ring);
- io_uring_prep_nop(sqe);
- io_uring_sqe_set_data(sqe, (void *) 1);
- ret = io_uring_submit_and_wait(ring, 3);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- i = 0;
- while (i < 3) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- /*
- * NOP commands have user_data as 1. Check that we get the
- * at least 'nr' NOPs first, then the successfully removed timeout.
- */
- if (io_uring_cqe_get_data(cqe) == NULL) {
- if (i < nr) {
- fprintf(stderr, "%s: timeout received too early\n", __FUNCTION__);
- goto err;
- }
- if (ret) {
- fprintf(stderr, "%s: timeout triggered by passage of"
- " time, not by events completed\n", __FUNCTION__);
- goto err;
- }
- }
- io_uring_cqe_seen(ring, cqe);
- if (ret) {
- fprintf(stderr, "res: %d\n", ret);
- goto err;
- }
- i++;
- }
- return 0;
- err:
- return 1;
- }
- static int test_single_timeout_wait(struct io_uring *ring,
- struct io_uring_params *p)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- int i, ret;
- sqe = io_uring_get_sqe(ring);
- io_uring_prep_nop(sqe);
- io_uring_sqe_set_data(sqe, (void *) 1);
- sqe = io_uring_get_sqe(ring);
- io_uring_prep_nop(sqe);
- io_uring_sqe_set_data(sqe, (void *) 1);
- /* no implied submit for newer kernels */
- if (p->features & IORING_FEAT_EXT_ARG) {
- ret = io_uring_submit(ring);
- if (ret != 2) {
- fprintf(stderr, "%s: submit %d\n", __FUNCTION__, ret);
- return 1;
- }
- }
- msec_to_ts(&ts, 1000);
- i = 0;
- do {
- ret = io_uring_wait_cqes(ring, &cqe, 2, &ts, NULL);
- if (ret == -ETIME)
- break;
- if (ret < 0) {
- fprintf(stderr, "%s: wait timeout failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- io_uring_cqe_seen(ring, cqe);
- if (ret < 0) {
- fprintf(stderr, "res: %d\n", ret);
- goto err;
- }
- i++;
- } while (1);
- if (i != 2) {
- fprintf(stderr, "got %d completions\n", i);
- goto err;
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test single timeout waking us up
- */
- static int test_single_timeout(struct io_uring *ring)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- unsigned long long exp;
- struct __kernel_timespec ts;
- struct timeval tv;
- int ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, TIMEOUT_MSEC);
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- gettimeofday(&tv, NULL);
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- io_uring_cqe_seen(ring, cqe);
- if (ret == -EINVAL) {
- fprintf(stdout, "%s: Timeout not supported, ignored\n", __FUNCTION__);
- not_supported = 1;
- return 0;
- } else if (ret != -ETIME) {
- fprintf(stderr, "%s: Timeout: %s\n", __FUNCTION__, strerror(-ret));
- goto err;
- }
- exp = mtime_since_now(&tv);
- if (exp >= TIMEOUT_MSEC / 2 && exp <= (TIMEOUT_MSEC * 3) / 2)
- return 0;
- fprintf(stderr, "%s: Timeout seems wonky (got %llu)\n", __FUNCTION__, exp);
- err:
- return 1;
- }
- static int test_single_timeout_remove_notfound(struct io_uring *ring)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- int ret, i;
- if (no_modify)
- return 0;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, TIMEOUT_MSEC);
- io_uring_prep_timeout(sqe, &ts, 2, 0);
- sqe->user_data = 1;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout_remove(sqe, 2, 0);
- sqe->user_data = 2;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- /*
- * We should get two completions. One is our modify request, which should
- * complete with -ENOENT. The other is the timeout that will trigger after
- * TIMEOUT_MSEC.
- */
- for (i = 0; i < 2; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- if (cqe->user_data == 2) {
- if (cqe->res != -ENOENT) {
- fprintf(stderr, "%s: modify ret %d, wanted ENOENT\n", __FUNCTION__, cqe->res);
- break;
- }
- } else if (cqe->user_data == 1) {
- if (cqe->res != -ETIME) {
- fprintf(stderr, "%s: timeout ret %d, wanted -ETIME\n", __FUNCTION__, cqe->res);
- break;
- }
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- static int test_single_timeout_remove(struct io_uring *ring)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- int ret, i;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, TIMEOUT_MSEC);
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 1;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout_remove(sqe, 1, 0);
- sqe->user_data = 2;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- /*
- * We should have two completions ready. One is for the original timeout
- * request, user_data == 1, that should have a ret of -ECANCELED. The other
- * is for our modify request, user_data == 2, that should have a ret of 0.
- */
- for (i = 0; i < 2; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- if (no_modify)
- goto seen;
- if (cqe->res == -EINVAL && cqe->user_data == 2) {
- fprintf(stdout, "Timeout modify not supported, ignoring\n");
- no_modify = 1;
- goto seen;
- }
- if (cqe->user_data == 1) {
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "%s: timeout ret %d, wanted canceled\n", __FUNCTION__, cqe->res);
- break;
- }
- } else if (cqe->user_data == 2) {
- if (cqe->res) {
- fprintf(stderr, "%s: modify ret %d, wanted 0\n", __FUNCTION__, cqe->res);
- break;
- }
- }
- seen:
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test single absolute timeout waking us up
- */
- static int test_single_timeout_abs(struct io_uring *ring)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- unsigned long long exp;
- struct __kernel_timespec ts;
- struct timespec abs_ts;
- struct timeval tv;
- int ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- clock_gettime(CLOCK_MONOTONIC, &abs_ts);
- ts.tv_sec = abs_ts.tv_sec + 1;
- ts.tv_nsec = abs_ts.tv_nsec;
- io_uring_prep_timeout(sqe, &ts, 0, IORING_TIMEOUT_ABS);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- gettimeofday(&tv, NULL);
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- io_uring_cqe_seen(ring, cqe);
- if (ret == -EINVAL) {
- fprintf(stdout, "Absolute timeouts not supported, ignored\n");
- return 0;
- } else if (ret != -ETIME) {
- fprintf(stderr, "Timeout: %s\n", strerror(-ret));
- goto err;
- }
- exp = mtime_since_now(&tv);
- if (exp >= 1000 / 2 && exp <= (1000 * 3) / 2)
- return 0;
- fprintf(stderr, "%s: Timeout seems wonky (got %llu)\n", __FUNCTION__, exp);
- err:
- return 1;
- }
- /*
- * Test that timeout is canceled on exit
- */
- static int test_single_timeout_exit(struct io_uring *ring)
- {
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- int ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, 30000);
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- io_uring_queue_exit(ring);
- return 0;
- err:
- io_uring_queue_exit(ring);
- return 1;
- }
- /*
- * Test multi timeouts waking us up
- */
- static int test_multi_timeout(struct io_uring *ring)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts[2];
- unsigned int timeout[2];
- unsigned long long exp;
- struct timeval tv;
- int ret, i;
- /* req_1: timeout req, count = 1, time = (TIMEOUT_MSEC * 2) */
- timeout[0] = TIMEOUT_MSEC * 2;
- msec_to_ts(&ts[0], timeout[0]);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts[0], 1, 0);
- sqe->user_data = 1;
- /* req_2: timeout req, count = 1, time = TIMEOUT_MSEC */
- timeout[1] = TIMEOUT_MSEC;
- msec_to_ts(&ts[1], timeout[1]);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts[1], 1, 0);
- sqe->user_data = 2;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- gettimeofday(&tv, NULL);
- for (i = 0; i < 2; i++) {
- unsigned int time = 0;
- __u64 user_data = 0;
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- /*
- * Both of these two reqs should timeout, but req_2 should
- * return before req_1.
- */
- switch (i) {
- case 0:
- user_data = 2;
- time = timeout[1];
- break;
- case 1:
- user_data = 1;
- time = timeout[0];
- break;
- }
- if (cqe->user_data != user_data) {
- fprintf(stderr, "%s: unexpected timeout req %d sequence\n",
- __FUNCTION__, i+1);
- goto err;
- }
- if (cqe->res != -ETIME) {
- fprintf(stderr, "%s: Req %d timeout: %s\n",
- __FUNCTION__, i+1, strerror(cqe->res));
- goto err;
- }
- exp = mtime_since_now(&tv);
- if (exp < time / 2 || exp > (time * 3) / 2) {
- fprintf(stderr, "%s: Req %d timeout seems wonky (got %llu)\n",
- __FUNCTION__, i+1, exp);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test multi timeout req with different count
- */
- static int test_multi_timeout_nr(struct io_uring *ring)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts;
- int ret, i;
- msec_to_ts(&ts, TIMEOUT_MSEC);
- /* req_1: timeout req, count = 2 */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 2, 0);
- sqe->user_data = 1;
- /* req_2: timeout req, count = 1 */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 1, 0);
- sqe->user_data = 2;
- /* req_3: nop req */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_nop(sqe);
- io_uring_sqe_set_data(sqe, (void *) 1);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- /*
- * req_2 (count=1) should return without error and req_1 (count=2)
- * should timeout.
- */
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- switch (i) {
- case 0:
- /* Should be nop req */
- if (io_uring_cqe_get_data(cqe) != (void *) 1) {
- fprintf(stderr, "%s: nop not seen as 1 or 2\n", __FUNCTION__);
- goto err;
- }
- break;
- case 1:
- /* Should be timeout req_2 */
- if (cqe->user_data != 2) {
- fprintf(stderr, "%s: unexpected timeout req %d sequence\n",
- __FUNCTION__, i+1);
- goto err;
- }
- if (cqe->res < 0) {
- fprintf(stderr, "%s: Req %d res %d\n",
- __FUNCTION__, i+1, cqe->res);
- goto err;
- }
- break;
- case 2:
- /* Should be timeout req_1 */
- if (cqe->user_data != 1) {
- fprintf(stderr, "%s: unexpected timeout req %d sequence\n",
- __FUNCTION__, i+1);
- goto err;
- }
- if (cqe->res != -ETIME) {
- fprintf(stderr, "%s: Req %d timeout: %s\n",
- __FUNCTION__, i+1, strerror(cqe->res));
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test timeout <link> timeout <drain> timeout
- */
- static int test_timeout_flags1(struct io_uring *ring)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts;
- int ret, i;
- msec_to_ts(&ts, TIMEOUT_MSEC);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 1;
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 2;
- sqe->flags |= IOSQE_IO_DRAIN;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 3;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- if (cqe->res == -EINVAL) {
- if (!i)
- fprintf(stdout, "%s: timeout flags not supported\n",
- __FUNCTION__);
- io_uring_cqe_seen(ring, cqe);
- continue;
- }
- switch (cqe->user_data) {
- case 1:
- if (cqe->res != -ETIME) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res, -ETIME);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res,
- -ECANCELED);
- goto err;
- }
- break;
- case 3:
- if (cqe->res != -ETIME) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res, -ETIME);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test timeout <link> timeout <link> timeout
- */
- static int test_timeout_flags2(struct io_uring *ring)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts;
- int ret, i;
- msec_to_ts(&ts, TIMEOUT_MSEC);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 1;
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 2;
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 3;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- if (cqe->res == -EINVAL) {
- if (!i)
- fprintf(stdout, "%s: timeout flags not supported\n",
- __FUNCTION__);
- io_uring_cqe_seen(ring, cqe);
- continue;
- }
- switch (cqe->user_data) {
- case 1:
- if (cqe->res != -ETIME) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res, -ETIME);
- goto err;
- }
- break;
- case 2:
- case 3:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res,
- -ECANCELED);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test timeout <drain> timeout <link> timeout
- */
- static int test_timeout_flags3(struct io_uring *ring)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts;
- int ret, i;
- msec_to_ts(&ts, TIMEOUT_MSEC);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 1;
- sqe->flags |= IOSQE_IO_DRAIN;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 2;
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 3;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- if (cqe->res == -EINVAL) {
- if (!i)
- fprintf(stdout, "%s: timeout flags not supported\n",
- __FUNCTION__);
- io_uring_cqe_seen(ring, cqe);
- continue;
- }
- switch (cqe->user_data) {
- case 1:
- case 2:
- if (cqe->res != -ETIME) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res, -ETIME);
- goto err;
- }
- break;
- case 3:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res,
- -ECANCELED);
- goto err;
- }
- break;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- static int test_update_timeout(struct io_uring *ring, unsigned long ms,
- bool abs, bool async, bool linked)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts, ts_upd;
- unsigned long long exp_ms, base_ms = 10000;
- struct timeval tv;
- int ret, i, nr = 2;
- __u32 mode = abs ? IORING_TIMEOUT_ABS : 0;
- msec_to_ts(&ts_upd, ms);
- gettimeofday(&tv, NULL);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, base_ms);
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->user_data = 1;
- if (linked) {
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->user_data = 3;
- sqe->flags = IOSQE_IO_LINK;
- if (async)
- sqe->flags |= IOSQE_ASYNC;
- nr++;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout_update(sqe, &ts_upd, 1, mode);
- sqe->user_data = 2;
- if (async)
- sqe->flags |= IOSQE_ASYNC;
- ret = io_uring_submit(ring);
- if (ret != nr) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- for (i = 0; i < nr; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 1:
- if (cqe->res != -ETIME) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res, -ETIME);
- goto err;
- }
- break;
- case 2:
- if (cqe->res != 0) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res,
- 0);
- goto err;
- }
- break;
- case 3:
- if (cqe->res != 0) {
- fprintf(stderr, "nop failed\n");
- goto err;
- }
- break;
- default:
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- exp_ms = mtime_since_now(&tv);
- if (exp_ms >= base_ms / 2) {
- fprintf(stderr, "too long, timeout wasn't updated\n");
- goto err;
- }
- if (ms >= 1000 && !abs && exp_ms < ms / 2) {
- fprintf(stderr, "fired too early, potentially updated to 0 ms"
- "instead of %lu\n", ms);
- goto err;
- }
- return 0;
- err:
- return 1;
- }
- static int test_update_nonexistent_timeout(struct io_uring *ring)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts;
- int ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, 0);
- io_uring_prep_timeout_update(sqe, &ts, 42, 0);
- ret = io_uring_submit(ring);
- if (ret != 1) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- if (ret == -ENOENT)
- ret = 0;
- io_uring_cqe_seen(ring, cqe);
- return ret;
- err:
- return 1;
- }
- static int test_update_invalid_flags(struct io_uring *ring)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts;
- int ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout_remove(sqe, 0, IORING_TIMEOUT_ABS);
- ret = io_uring_submit(ring);
- if (ret != 1) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- if (cqe->res != -EINVAL) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res, -EINVAL);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, 0);
- io_uring_prep_timeout_update(sqe, &ts, 0, -1);
- ret = io_uring_submit(ring);
- if (ret != 1) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- if (cqe->res != -EINVAL) {
- fprintf(stderr, "%s: got %d, wanted %d\n",
- __FUNCTION__, cqe->res, -EINVAL);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- return 0;
- err:
- return 1;
- }
- static int fill_exec_target(char *dst, char *path)
- {
- struct stat sb;
- /*
- * Should either be ./exec-target.t or test/exec-target.t
- */
- sprintf(dst, "%s", path);
- return stat(dst, &sb);
- }
- static int test_timeout_link_cancel(void)
- {
- struct io_uring ring;
- struct io_uring_cqe *cqe;
- char prog_path[PATH_MAX];
- pid_t p;
- int ret, i, wstatus;
- if (fill_exec_target(prog_path, "./exec-target.t") &&
- fill_exec_target(prog_path, "test/exec-target.t")) {
- fprintf(stdout, "Can't find exec-target, skipping\n");
- return 0;
- }
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- fprintf(stderr, "ring create failed: %d\n", ret);
- return 1;
- }
- p = fork();
- if (p == -1) {
- fprintf(stderr, "fork() failed\n");
- return 1;
- }
- if (p == 0) {
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- msec_to_ts(&ts, 10000);
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe->user_data = 0;
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_nop(sqe);
- sqe->user_data = 1;
- ret = io_uring_submit(&ring);
- if (ret != 2) {
- fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
- exit(1);
- }
- /* trigger full cancellation */
- ret = execl(prog_path, prog_path, NULL);
- if (ret) {
- fprintf(stderr, "exec failed %i\n", errno);
- exit(1);
- }
- exit(0);
- }
- if (waitpid(p, &wstatus, 0) == (pid_t)-1) {
- perror("waitpid()");
- return 1;
- }
- if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) {
- fprintf(stderr, "child failed %i\n", WEXITSTATUS(wstatus));
- return 1;
- }
- for (i = 0; i < 2; ++i) {
- ret = io_uring_wait_cqe(&ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait_cqe=%d\n", ret);
- return 1;
- }
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "invalid result, user_data: %i res: %i\n",
- (int)cqe->user_data, cqe->res);
- return 1;
- }
- io_uring_cqe_seen(&ring, cqe);
- }
- io_uring_queue_exit(&ring);
- return 0;
- }
- static int test_not_failing_links(void)
- {
- struct io_uring ring;
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct __kernel_timespec ts;
- int ret;
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- fprintf(stderr, "ring create failed: %d\n", ret);
- return 1;
- }
- msec_to_ts(&ts, 1);
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_timeout(sqe, &ts, 0, IORING_TIMEOUT_ETIME_SUCCESS);
- sqe->user_data = 1;
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_nop(sqe);
- sqe->user_data = 2;
- ret = io_uring_submit(&ring);
- if (ret != 2) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- return 1;
- }
- ret = io_uring_wait_cqe(&ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- return 1;
- } else if (cqe->user_data == 1 && cqe->res == -EINVAL) {
- goto done;
- } else if (cqe->res != -ETIME || cqe->user_data != 1) {
- fprintf(stderr, "timeout failed %i %i\n", cqe->res,
- (int)cqe->user_data);
- return 1;
- }
- io_uring_cqe_seen(&ring, cqe);
- ret = io_uring_wait_cqe(&ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- return 1;
- } else if (cqe->res || cqe->user_data != 2) {
- fprintf(stderr, "nop failed %i %i\n", cqe->res,
- (int)cqe->user_data);
- return 1;
- }
- done:
- io_uring_cqe_seen(&ring, cqe);
- io_uring_queue_exit(&ring);
- return 0;
- }
- static int test_timeout_multishot(struct io_uring *ring)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- int ret;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, TIMEOUT_MSEC);
- io_uring_prep_timeout(sqe, &ts, 0, IORING_TIMEOUT_MULTISHOT);
- io_uring_sqe_set_data(sqe, (void *) 1);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- for (int i = 0; i < 2; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- if (ret == -EINVAL) {
- no_multishot = 1;
- return T_EXIT_SKIP;
- }
- if (!(cqe->flags & IORING_CQE_F_MORE)) {
- fprintf(stderr, "%s: flag not set in cqe\n", __FUNCTION__);
- goto err;
- }
- if (ret != -ETIME) {
- fprintf(stderr, "%s: Timeout: %s\n", __FUNCTION__, strerror(-ret));
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout_remove(sqe, 1, 0);
- io_uring_sqe_set_data(sqe, (void *) 2);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- if (ret < 0) {
- fprintf(stderr, "%s: remove failed: %s\n", __FUNCTION__, strerror(-ret));
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- if (ret != -ECANCELED) {
- fprintf(stderr, "%s: timeout canceled: %s %llu\n", __FUNCTION__, strerror(-ret), cqe->user_data);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- return 0;
- err:
- return 1;
- }
- static int test_timeout_multishot_nr(struct io_uring *ring)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- int ret;
- if (no_multishot)
- return T_EXIT_SKIP;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, TIMEOUT_MSEC);
- io_uring_prep_timeout(sqe, &ts, 3, IORING_TIMEOUT_MULTISHOT);
- io_uring_sqe_set_data(sqe, (void *) 1);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- for (int i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- if (i < 2 && !(cqe->flags & IORING_CQE_F_MORE)) {
- fprintf(stderr, "%s: flag not set in cqe\n", __FUNCTION__);
- goto err;
- }
- if (i == 3 && (cqe->flags & IORING_CQE_F_MORE)) {
- fprintf(stderr, "%s: flag set in cqe\n", __FUNCTION__);
- goto err;
- }
- ret = cqe->res;
- if (ret != -ETIME) {
- fprintf(stderr, "%s: Timeout: %s\n", __FUNCTION__, strerror(-ret));
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- msec_to_ts(&ts, 2 * TIMEOUT_MSEC);
- ret = io_uring_wait_cqe_timeout(ring, &cqe, &ts);
- if (ret != -ETIME) {
- fprintf(stderr, "%s: wait completion timeout %s\n", __FUNCTION__, strerror(-ret));
- goto err;
- }
- return 0;
- err:
- return 1;
- }
- static int test_timeout_multishot_overflow(struct io_uring *ring)
- {
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- struct __kernel_timespec ts;
- int ret;
- if (no_multishot)
- return T_EXIT_SKIP;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- msec_to_ts(&ts, 10);
- io_uring_prep_timeout(sqe, &ts, 0, IORING_TIMEOUT_MULTISHOT);
- io_uring_sqe_set_data(sqe, (void *) 1);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- if (ret != -ETIME) {
- fprintf(stderr, "%s: Timeout: %s\n", __FUNCTION__, strerror(-ret));
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- sleep(1);
- if (!((*ring->sq.kflags) & IORING_SQ_CQ_OVERFLOW)) {
- goto err;
- }
- /* multishot timer should be gone */
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
- goto err;
- }
- io_uring_prep_timeout_remove(sqe, 1, 0);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
- goto err;
- }
- ret = cqe->res;
- io_uring_cqe_seen(ring, cqe);
- if (ret != -ETIME) {
- fprintf(stderr, "%s: remove failed: %d %s\n", __FUNCTION__, ret, strerror(-ret));
- goto err;
- }
- return 0;
- err:
- return 1;
- }
- int main(int argc, char *argv[])
- {
- struct io_uring ring, sqpoll_ring;
- bool has_timeout_update, sqpoll;
- struct io_uring_params p = { };
- int ret;
- if (argc > 1)
- return 0;
- ret = io_uring_queue_init_params(8, &ring, &p);
- if (ret) {
- fprintf(stderr, "ring setup failed\n");
- return 1;
- }
- ret = io_uring_queue_init(8, &sqpoll_ring, IORING_SETUP_SQPOLL);
- sqpoll = !ret;
- ret = test_single_timeout(&ring);
- if (ret) {
- fprintf(stderr, "test_single_timeout failed\n");
- return ret;
- }
- if (not_supported)
- return 0;
- ret = test_multi_timeout(&ring);
- if (ret) {
- fprintf(stderr, "test_multi_timeout failed\n");
- return ret;
- }
- ret = test_single_timeout_abs(&ring);
- if (ret) {
- fprintf(stderr, "test_single_timeout_abs failed\n");
- return ret;
- }
- ret = test_single_timeout_remove(&ring);
- if (ret) {
- fprintf(stderr, "test_single_timeout_remove failed\n");
- return ret;
- }
- ret = test_single_timeout_remove_notfound(&ring);
- if (ret) {
- fprintf(stderr, "test_single_timeout_remove_notfound failed\n");
- return ret;
- }
- ret = test_single_timeout_many(&ring);
- if (ret) {
- fprintf(stderr, "test_single_timeout_many failed\n");
- return ret;
- }
- ret = test_single_timeout_nr(&ring, 1);
- if (ret) {
- fprintf(stderr, "test_single_timeout_nr(1) failed\n");
- return ret;
- }
- ret = test_single_timeout_nr(&ring, 2);
- if (ret) {
- fprintf(stderr, "test_single_timeout_nr(2) failed\n");
- return ret;
- }
- ret = test_multi_timeout_nr(&ring);
- if (ret) {
- fprintf(stderr, "test_multi_timeout_nr failed\n");
- return ret;
- }
- ret = test_timeout_flags1(&ring);
- if (ret) {
- fprintf(stderr, "test_timeout_flags1 failed\n");
- return ret;
- }
- ret = test_timeout_flags2(&ring);
- if (ret) {
- fprintf(stderr, "test_timeout_flags2 failed\n");
- return ret;
- }
- ret = test_timeout_flags3(&ring);
- if (ret) {
- fprintf(stderr, "test_timeout_flags3 failed\n");
- return ret;
- }
- ret = test_timeout_multishot(&ring);
- if (ret && ret != T_EXIT_SKIP) {
- fprintf(stderr, "test_timeout_multishot failed\n");
- return ret;
- }
- ret = test_timeout_multishot_nr(&ring);
- if (ret && ret != T_EXIT_SKIP) {
- fprintf(stderr, "test_timeout_multishot_nr failed\n");
- return ret;
- }
- /* io_uring_wait_cqe_timeout() may have left a timeout, reinit ring */
- io_uring_queue_exit(&ring);
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- fprintf(stderr, "ring setup failed\n");
- return 1;
- }
- ret = test_timeout_multishot_overflow(&ring);
- if (ret && ret != T_EXIT_SKIP) {
- fprintf(stderr, "test_timeout_multishot_overflow failed\n");
- return ret;
- }
- /* io_uring_wait_cqe_timeout() may have left a timeout, reinit ring */
- io_uring_queue_exit(&ring);
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- fprintf(stderr, "ring setup failed\n");
- return 1;
- }
- ret = test_single_timeout_wait(&ring, &p);
- if (ret) {
- fprintf(stderr, "test_single_timeout_wait failed\n");
- return ret;
- }
- /* io_uring_wait_cqes() may have left a timeout, reinit ring */
- io_uring_queue_exit(&ring);
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- fprintf(stderr, "ring setup failed\n");
- return 1;
- }
- ret = test_update_nonexistent_timeout(&ring);
- has_timeout_update = (ret != -EINVAL);
- if (has_timeout_update) {
- if (ret) {
- fprintf(stderr, "test_update_nonexistent_timeout failed\n");
- return ret;
- }
- ret = test_update_invalid_flags(&ring);
- if (ret) {
- fprintf(stderr, "test_update_invalid_flags failed\n");
- return ret;
- }
- ret = test_update_timeout(&ring, 0, false, false, false);
- if (ret) {
- fprintf(stderr, "test_update_timeout failed\n");
- return ret;
- }
- ret = test_update_timeout(&ring, 1, false, false, false);
- if (ret) {
- fprintf(stderr, "test_update_timeout 1ms failed\n");
- return ret;
- }
- ret = test_update_timeout(&ring, 1000, false, false, false);
- if (ret) {
- fprintf(stderr, "test_update_timeout 1s failed\n");
- return ret;
- }
- ret = test_update_timeout(&ring, 0, true, true, false);
- if (ret) {
- fprintf(stderr, "test_update_timeout abs failed\n");
- return ret;
- }
- ret = test_update_timeout(&ring, 0, false, true, false);
- if (ret) {
- fprintf(stderr, "test_update_timeout async failed\n");
- return ret;
- }
- ret = test_update_timeout(&ring, 0, false, false, true);
- if (ret) {
- fprintf(stderr, "test_update_timeout linked failed\n");
- return ret;
- }
- if (sqpoll) {
- ret = test_update_timeout(&sqpoll_ring, 0, false, false,
- false);
- if (ret) {
- fprintf(stderr, "test_update_timeout sqpoll"
- "failed\n");
- return ret;
- }
- }
- }
- /*
- * this test must go last, it kills the ring
- */
- ret = test_single_timeout_exit(&ring);
- if (ret) {
- fprintf(stderr, "test_single_timeout_exit failed\n");
- return ret;
- }
- ret = test_timeout_link_cancel();
- if (ret) {
- fprintf(stderr, "test_timeout_link_cancel failed\n");
- return ret;
- }
- ret = test_not_failing_links();
- if (ret) {
- fprintf(stderr, "test_not_failing_links failed\n");
- return ret;
- }
- if (sqpoll)
- io_uring_queue_exit(&sqpoll_ring);
- return 0;
- }
|