123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785 |
- #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;
- }
- /*
- * 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;
- bool update_ealready = false;
- 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:
- /*
- * We should not be hitting this case, but for
- * a kernel with PREEMPT_RT, even an instant attempt
- * to remove a timer will return that the timer is
- * already running... Deal with it.
- */
- if (cqe->res == -EALREADY) {
- update_ealready = true;
- break;
- }
- 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 (!update_ealready && 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 cancelation */
- 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;
- }
|