summaryrefslogtreecommitdiff
path: root/uring.c
blob: dc095c8bc0b6b377aed7954ba820034a51cf3a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#include <liburing.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include "main.h"
#include "uring.h"
#include "config.h"

struct uring_ev {
	struct io_uring uring;
	struct io_uring_params uring_params;
	struct cfg *cfg;
	struct uring_task task;
};

enum cqe_type {
	CQE_TYPE_NORMAL		= 0x0,
	CQE_TYPE_CANCEL		= 0x1,
	CQE_TYPE_CLOSE		= 0x2,
	CQE_TYPE_POLL_CANCEL	= 0x3
};

#define CQE_TYPE_PTR_MASK 0x3

uint64_t sqe_count = 0;
uint64_t cqe_count = 0;

static struct io_uring_sqe *
get_sqe(struct cfg *cfg, struct uring_task *task)
{
	struct io_uring_sqe *sqe;
	
	if (!cfg || !task)
		die("invalid parameters");

	sqe = io_uring_get_sqe(&cfg->uev->uring);
	if (!sqe) {
		io_uring_submit(&cfg->uev->uring);
		sqe = io_uring_get_sqe(&cfg->uev->uring);
		if (!sqe)
			perrordie("failed to get an sqe!");
	}

	sqe_count++;
	uring_task_get(cfg, task);
	return sqe;
}

void
uring_task_refdump(struct uring_task *task)
{
	char buf[4096];
	struct uring_task *tmp;

	buf[0] = '\0';
	for (tmp = task; tmp; tmp = tmp->parent) {
		size_t prefix;
		char *dst;

		if (tmp->parent)
			prefix = strlen("->") + strlen(tmp->name);
		else
			prefix = strlen(tmp->name);

		memmove(&buf[prefix], &buf[0], strlen(buf) + 1);

		dst = &buf[0];
		if (tmp->parent) {
			*dst++ = '-';
			*dst++ = '>';
		}

		memcpy(dst, tmp->name, strlen(tmp->name));
	}

	fprintf(stderr, "%s (0x%p parent 0x%p free 0x%p fd %i ref %u)\n",
		buf, task, task->parent, task->free, task->fd,
		task->refcount);
}

/*
 * Similar to uring_task_put, but can be called from other tasks
 * while the task is active.
 */
void
uring_task_destroy(struct cfg *cfg, struct uring_task *task)
{
	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	if (!task) {
		error("called with no task\n");
		return;
	}

	if (task->fd >= 0) {
		struct io_uring_sqe *sqe;
	
		sqe = get_sqe(cfg, task);
		io_uring_prep_cancel(sqe, task, 0);
		io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CANCEL));
	}

	task->dead = true;
	uring_task_put(cfg, task);
}

void
uring_task_put(struct cfg *cfg, struct uring_task *task)
{
	struct uring_task *parent = task->parent;

	debug(DBG_REF, "task %s (%p), refcount %u\n",
	      task->name, task, task->refcount);

	task->refcount--;

	if (task->refcount > 0)
		return;

	if (task->refcount < 0)
		error("Negative refcount!\n");

	if (task->fd >= 0) {
		uring_task_close_fd(cfg, task);
		/* We'll be called again once the fd is closed */
		return;
	}

	if (parent)
		debug(DBG_REF, "putting parent %s (%p)\n",
		      task->parent->name, task->parent);

	if (task->free)
		task->free(task);

	if (parent)
		uring_task_put(cfg, parent);
}

void
uring_task_get(struct cfg *cfg, struct uring_task *task)
{
	debug(DBG_REF, "task %s (%p), refcount %u\n",
	      task->name, task, task->refcount);

	if (task->refcount < 0)
		error("Negative refcount!\n");

	task->refcount++;
}

void
uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf)
{
	debug(DBG_UR, "task %s (%p), buf %p, refcount %u\n",
	      task->name, task, tbuf, task->refcount);

	if (tbuf) {
		/* iov_len and msg_namelen are set at send/receive time */
		tbuf->iov.iov_base = tbuf->buf;
		tbuf->msg.msg_name = &task->addr.storage;
		tbuf->msg.msg_iov = &tbuf->iov;
		tbuf->msg.msg_iovlen = 1;
		tbuf->msg.msg_control = NULL;
		tbuf->msg.msg_controllen = 0;
		tbuf->msg.msg_flags = 0;
	}
	task->tbuf = tbuf;
}

void
uring_task_set_fd(struct uring_task *task, int fd)
{
	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, fd, task->refcount);

	task->fd = fd;
}

void
uring_task_close_fd(struct cfg *cfg, struct uring_task *task)
{
	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	if (task->fd < 0)
		return;

	uring_close(cfg, task, task->fd);
	task->fd = -1;
}

struct uring_task *
uring_parent(struct cfg *cfg)
{
	if (!cfg)
		die("called with null cfg");

	if (!cfg->uev)
		die("called with uninitialized uring");

	return &cfg->uev->task;
}

void
uring_task_init(struct uring_task *task, const char *name,
		struct uring_task *parent, void (*free)(struct uring_task *))
{
	static bool first = true;

	if (first)
		first = false;
	else if (!parent)
		die("called without a parent");

	if (!free)
		die("called without destructor");

	task->refcount = 1;
	task->fd = -1;
	task->parent = parent;
	task->free = free;
	task->dead = false;
	task->name = name;
	task->tbuf = NULL;

	if (task->parent) {
		debug(DBG_REF, "task %s (%p), refcount %u, "
		      "getting parent %s (%p), refcount %u\n",
		      task->name, task, task->refcount,
		      task->parent->name, task->parent, task->parent->refcount);
		uring_task_get(NULL, task->parent);
	}
}

void
uring_close(struct cfg *cfg, struct uring_task *task, int fd)
{
	struct io_uring_sqe *sqe;

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	if (!task || fd < 0) {
		error("invalid parameters (task: %p (%s), fd: %i)\n", task, task->name, fd);
		return;
	}

       	sqe = get_sqe(cfg, task);
	io_uring_prep_close(sqe, fd);
	io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CLOSE));
}

static void
uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res)
{
	int r;

	if (!task || !task->tbuf || !task->final_callback)
		die("missing parameters");

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	if (res < 0) {
		r = res;
		goto finished;
	}

	/* We wrote some more data */
	task->tbuf->done += res;
	if (task->tbuf->done >= task->tbuf->len || res == 0) {
		r = task->tbuf->len;
		goto finished;
	}

	uring_write(cfg, task, task->tbuf->buf + task->tbuf->done,
		    task->tbuf->len - task->tbuf->done,
		    uring_tbuf_write_cb);
	return;

finished:
	task->final_callback(cfg, task, r);
	return;
}

void
uring_tbuf_write(struct cfg *cfg, struct uring_task *task, callback_t callback)
{
	if (!task || task->fd < 0 || !task->tbuf || task->tbuf->len < 0) {
		error("invalid parameters\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	task->tbuf->done = 0;
	task->final_callback = callback;
	uring_write(cfg, task, &task->tbuf->buf, task->tbuf->len, uring_tbuf_write_cb);
}

void
uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, callback_t callback)
{
	struct io_uring_sqe *sqe;

	if (task->fd < 0) {
		error("no fd set\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

       	sqe = get_sqe(cfg, task);
	task->callback = callback;
	io_uring_prep_write(sqe, task->fd, buf, len, 0);
	io_uring_sqe_set_data(sqe, task);
}

static void
uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res)
{
	int r;

	if (!task || !task->tbuf || !task->final_callback || !task->complete_callback) {
		error("invalid parameters\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	if (res < 0) {
		r = res;
		goto finished;
	}

	task->tbuf->len += res;
	r = task->complete_callback(cfg, task, res);
	if (r < 0) {
		r = res;
		goto finished;
	} else if (r > 0) {
		r = task->tbuf->len;
		goto finished;
	}

	/* Need to read more */
	if (task->tbuf->len >= sizeof(task->tbuf->buf)) {
		r = E2BIG;
		goto finished;
	}

	uring_read_offset(cfg, task, task->tbuf->buf + task->tbuf->len,
			  sizeof(task->tbuf->buf) - task->tbuf->len,
			  task->tbuf->len, uring_tbuf_read_until_cb);
	return;

finished:
	task->final_callback(cfg, task, r);
	return;
}

void
uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task,
		      rcallback_t complete, callback_t callback)
{
	if (!task || task->fd < 0 || !task->tbuf || !complete) {
		error("%s: invalid parameters\n", __func__);
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	task->tbuf->len = 0;
	task->complete_callback = complete;
	task->final_callback = callback;
	uring_read(cfg, task, &task->tbuf->buf, sizeof(task->tbuf->buf), uring_tbuf_read_until_cb);
}

static int
uring_tbuf_eof(struct cfg *cfg, struct uring_task *task, int res)
{
	if (task->tbuf->len + 1 >= sizeof(task->tbuf->buf))
		return -E2BIG;

	if (res > 0)
		return 0;

	task->tbuf->buf[task->tbuf->len] = '\0';
	task->tbuf->len++;
	return 1;
}

void
uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, callback_t callback)
{
	uring_tbuf_read_until(cfg, task, uring_tbuf_eof, callback);
}

static int
uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res)
{
	if (res < 0)
		return res;
	else
		return 1;
}

void
uring_tbuf_read(struct cfg *cfg, struct uring_task *task, callback_t callback)
{
	uring_tbuf_read_until(cfg, task, uring_tbuf_have_data, callback);
}

void
uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, off_t offset, callback_t callback)
{
	struct io_uring_sqe *sqe;

	if (task->fd < 0) {
		error("uring_read called with no fd set\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	sqe = get_sqe(cfg, task);
	task->callback = callback;
	io_uring_prep_read(sqe, task->fd, buf, len, offset);
	io_uring_sqe_set_data(sqe, task);
}

void
uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callback_t callback)
{
	struct io_uring_sqe *sqe;
       
	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	sqe = get_sqe(cfg, task);
	task->callback = callback;
	io_uring_prep_openat(sqe, AT_FDCWD, path, O_RDONLY | O_CLOEXEC, 0);
	io_uring_sqe_set_data(sqe, task);
}

void
uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, callback_t callback)
{
	struct io_uring_sqe *sqe;

	if (!task->tbuf) {
		error("called with no tbuf set\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	sqe = get_sqe(cfg, task);
	task->tbuf->done = 0;
	task->tbuf->len = 0;
	task->tbuf->iov.iov_len = sizeof(task->tbuf->buf);
	task->tbuf->msg.msg_namelen = task->addr.addrlen;
	task->callback = callback;
	io_uring_prep_recvmsg(sqe, task->fd, &task->tbuf->msg, 0);
	io_uring_sqe_set_data(sqe, task);
}

void
uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback)
{
	struct io_uring_sqe *sqe;

	if (!task->tbuf) {
		error("%s: called with no tbuf set\n", __func__);
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	sqe = get_sqe(cfg, task);
	task->tbuf->done = 0;
	task->tbuf->iov.iov_len = task->tbuf->len;
	task->tbuf->msg.msg_namelen = task->addr.addrlen;
	task->callback = callback;
	io_uring_prep_sendmsg(sqe, task->fd, &task->tbuf->msg, 0);
	io_uring_sqe_set_data(sqe, task);
}

void
uring_connect(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *addr, callback_t callback)
{
	struct io_uring_sqe *sqe;

	if (task->fd < 0) {
		error("fd set\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	sqe = get_sqe(cfg, task);
	task->callback = callback;
	io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&addr->storage, addr->addrlen);
	io_uring_sqe_set_data(sqe, task);
}

void
uring_accept(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *addr, callback_t callback)
{
	struct io_uring_sqe *sqe;

	if (task->fd < 0) {
		error("no fd set\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	sqe = get_sqe(cfg, task);
	addr->addrlen = sizeof(addr->storage);
	task->callback = callback;
	io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&addr->storage, &addr->addrlen, SOCK_CLOEXEC);
	io_uring_sqe_set_data(sqe, task);
}

void
uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, callback_t callback)
{
	struct io_uring_sqe *sqe;
	
	if (task->fd < 0) {
		error("uring_poll called with no fd set\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	sqe = get_sqe(cfg, task);
	task->callback = callback;
	io_uring_prep_poll_add(sqe, task->fd, poll_mask);
	io_uring_sqe_set_data(sqe, task);
}

void
uring_poll_cancel(struct cfg *cfg, struct uring_task *task)
{
	struct io_uring_sqe *sqe;
	
	if (task->fd < 0) {
		fprintf(stderr, "uring_poll_cancel called with no fd set\n");
		return;
	}

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	sqe = get_sqe(cfg, task);
	task->dead = true;
	io_uring_prep_poll_remove(sqe, task);
	io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_POLL_CANCEL));
}

static void
uring_free(struct uring_task *task)
{
	struct uring_ev *uev = container_of(task, struct uring_ev, task);

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	io_uring_queue_exit(&uev->uring);
	uev->cfg->uev = NULL;
	xfree(uev);
}

void
uring_refdump(struct uring_ev *uev)
{
	uring_task_refdump(&uev->task);
}

void
uring_delete(struct cfg *cfg)
{
	struct uring_task *task = &cfg->uev->task;

	debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n",
	      task->name, task, task->fd, task->refcount);

	uring_task_put(cfg, task);
}

void
uring_init(struct cfg *cfg)
{
	struct uring_ev *uev;

	uev = zmalloc(sizeof(*uev));
	if (!uev)
		perrordie("malloc");

	if (io_uring_queue_init_params(4096, &uev->uring, &uev->uring_params) < 0)
		perrordie("io_uring_queue_init_params");

	verbose("uring initialized, features: 0x%08x\n", uev->uring_params.features);

	uring_task_init(&uev->task, "uev", &cfg->task, uring_free);
	cfg->uev = uev;
	uev->cfg = cfg;
}

static inline void
uring_print_cqe(struct cfg *cfg, const char *type, struct uring_task *task,
		struct io_uring_cqe *cqe)
{
	if (!debug_enabled(DBG_UR))
		return;

	error("got CQE "
	      "(type: %s, res: %i (%s), task: %s (%p), fd: %i, cb: %p)\n",
	      type,
	      cqe->res,
	      cqe->res < 0 ? strerror(-cqe->res) : "ok",
	      task->name ? task->name : "<none>",
	      task,
	      task->fd,
	      task->callback);
}

void
uring_event_loop(struct cfg *cfg)
{
	while (true) {
		struct io_uring_cqe *cqe;
		unsigned nr, head;
		int r;

		io_uring_submit(&cfg->uev->uring);
		r = io_uring_wait_cqe(&cfg->uev->uring, &cqe);
		if (r < 0) {
			if (errno == EINTR)
				continue;
			else
				perrordie("io_uring_wait_cqe");
		}

		nr = 0;
		io_uring_for_each_cqe(&cfg->uev->uring, head, cqe) {
			struct uring_task *task = io_uring_cqe_get_data(cqe);
			bool do_callback;
			enum cqe_type cqe_type;

			cqe_count++;

			cqe_type = ((uintptr_t)task & CQE_TYPE_PTR_MASK);
			task = (void *)((uintptr_t)task & ~CQE_TYPE_PTR_MASK);

			if (!task)
				die("null task");

			switch (cqe_type) {
			case CQE_TYPE_CANCEL:
				uring_print_cqe(cfg, "cancel", task, cqe);
				do_callback = false;
				break;

			case CQE_TYPE_CLOSE:
				uring_print_cqe(cfg, "close", task, cqe);
				do_callback = false;
				break;

			case CQE_TYPE_POLL_CANCEL:
				uring_print_cqe(cfg, "poll_cancel", task, cqe);
				do_callback = false;
				break;

			case CQE_TYPE_NORMAL:
				uring_print_cqe(cfg, "standard", task, cqe);
				do_callback = true;
				break;

			default:
				die("unknown CQE type");
			}

			if (do_callback && task->callback)
				task->callback(cfg, task, cqe->res);

			uring_task_put(cfg, task);

			if (exiting)
				return;

			nr++;
		}

		io_uring_cq_advance(&cfg->uev->uring, nr);
	}
}