Loading...
  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
/*
 * Copyright (c) 2016-2024 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <sys/select.h>
#include <poll.h>
#include <time.h>
#include <sys/event.h>
#include "skywalk_test_driver.h"
#include "skywalk_test_common.h"
#include "skywalk_test_utils.h"

/************************************************************************
 * Utility code
 */

struct context {
	uuid_t nexus_uuid;
	int argc;
	char **argv;

	int test_selector;

	struct stage_ctx stage;
};

static void
skt_kqueue_init(void)
{
	struct sktc_nexus_attr attr = SKTC_NEXUS_ATTR_INIT();

	strncpy((char *)attr.name, "skywalk_test_kqueue_upipe",
	    sizeof(nexus_name_t) - 1);
	attr.type = NEXUS_TYPE_USER_PIPE;
	attr.ntxrings = 1;
	attr.nrxrings = 1;
	attr.ntxslots = 64;
	attr.nrxslots = 64;
	attr.anonymous = 1;

	sktc_setup_nexus(&attr);
}

static void
skt_kqueue_fini(void)
{
	sktc_cleanup_nexus();
}

enum kqueue_common_test_stage {
	SKT_KQUEUE_INIT=0,
};

static int
skt_kqueue_main(int argc, char *argv[], int test_selector,
    void *(rx_body)(void *), void *(tx_body)(void *))
{
	pthread_t rx_thread, tx_thread;
	struct context ctx;
	int error;

	test_stage_init(&ctx.stage, SKT_KQUEUE_INIT);
	ctx.argc = argc;
	ctx.argv = argv;
	ctx.test_selector = test_selector;

	error = uuid_parse(argv[3], ctx.nexus_uuid);
	SKTC_ASSERT_ERR(!error);

	error = pthread_create(&rx_thread, NULL, rx_body, &ctx);
	SKTC_ASSERT_ERR(!error);
	error = pthread_create(&tx_thread, NULL, tx_body, &ctx);
	SKTC_ASSERT_ERR(!error);

	pthread_join(rx_thread, NULL);
	pthread_join(tx_thread, NULL);

	test_stage_destroy(&ctx.stage);

	return 0;
}

/************************************************************************
 * Basic sweep test
 */

enum kqueue_basic_test_stage {
	SKT_KQUEUE_BASIC_RX_SWEEP_1=1,
	SKT_KQUEUE_BASIC_RX_SWEEP_2,
	SKT_KQUEUE_BASIC_TX_SWEEP_1,
	SKT_KQUEUE_BASIC_TX_SWEEP_2,
	SKT_KQUEUE_BASIC_TX_SWEEP_3,
};

static void *
skt_kqueue_basic_rx(void *ctx_)
{
	struct context *ctx = (struct context *)ctx_;
	channel_t channel;
	channel_ring_t rxring;
	channel_attr_t ch_attr;
	ring_dir_t ring_dir = CHANNEL_DIR_RX;
	ring_id_t ring_id = CHANNEL_RING_ID_ANY;
	uint32_t port = 0;
	int kq_fd;
	int error;
	int i;

	assert(ctx->stage.test_stage == SKT_KQUEUE_INIT);

	/* Initialize kqueue */
	kq_fd = kqueue();
	assert(kq_fd >= 0);

	/* Initialize channel */
	ch_attr = os_channel_attr_create();
	channel = sktu_channel_create_extended(
		ctx->nexus_uuid,
		port, ring_dir, ring_id,
		ch_attr,
		-1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
	assert(channel != NULL);

	ring_id = os_channel_ring_id(channel, CHANNEL_FIRST_RX_RING);
	rxring = os_channel_rx_ring(channel, ring_id);
	assert(rxring);

	/* Wait before any data and confirm that the ring is reported as empty */
	error = wait_on_fd(kq_fd, EVFILT_READ, channel, 0, TIMEOUT_EXPECT);
	SKTC_ASSERT_ERR(!error);

	/* Test basic RX data reporting, sweep from 0 to slots-1 to make sure
	 * the ring pointers wrap at some point */
	test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_1);

	int lim = ch_attr->cha_rx_slots - 1;
	for (i = 1; i <= lim; i++) {
		test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_2);
#ifndef NDEBUG
		int result =
#endif
		wait_on_fd(kq_fd, EVFILT_READ, channel, 0, TIMEOUT_FAIL);
		assert(result == i);

		chew_slots(rxring, 0);

		error = os_channel_sync(channel, CHANNEL_SYNC_RX);
		SKTC_ASSERT_ERR(!error);

		test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_1);
	}
	test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_2);

	/* Get ready for TX sweep part of the test */

	test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_1);

	/* Drain RX ring */
	test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_2);
	error = os_channel_sync(channel, CHANNEL_SYNC_RX);
	SKTC_ASSERT_ERR(!error);
	test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_1);

	/* Allow TX backlog to trickle in */
	for (i = 1; i <= lim; i++) {
		test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_2);

		chew_slots(rxring, 1);
		error = os_channel_sync(channel, CHANNEL_SYNC_RX);
		SKTC_ASSERT_ERR(!error);

		test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_1);
	}

	os_channel_attr_destroy(ch_attr);
	os_channel_destroy(channel);
	return 0;
}

static void *
skt_kqueue_basic_tx(void *ctx_)
{
	struct context *ctx = (struct context *)ctx_;
	channel_t channel;
	channel_ring_t txring;
	channel_attr_t ch_attr;
	ring_dir_t ring_dir = CHANNEL_DIR_TX;
	ring_id_t ring_id = CHANNEL_RING_ID_ANY;
	uint32_t port = 1;
	int error;
	int kq_fd;
	int i, j;

	kq_fd = kqueue();
	assert(kq_fd >= 0);

	ch_attr = os_channel_attr_create();
	channel = sktu_channel_create_extended(
		ctx->nexus_uuid,
		port, ring_dir, ring_id,
		ch_attr,
		-1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
	assert(channel != NULL);

	ring_id = os_channel_ring_id(channel, CHANNEL_FIRST_TX_RING);
	txring = os_channel_tx_ring(channel, ring_id);
	assert(txring);

	/* Wait for RX to initialize */
	test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_1);

	/* Test basic RX data reporting, sweep from 0 to slots-1 to make sure
	 * the ring pointers wrap at some point */
	int lim =  ch_attr->cha_tx_slots - 1;
	for (i = 1; i <= lim; i++) {
		while (os_channel_available_slot_count(txring) < i) {
			wait_on_fd(kq_fd, EVFILT_WRITE, channel, 0, TIMEOUT_DONT_CARE);

			/* Abort if we were woken up but there are no slots.
			 * This can happen if the channel is defuncted such as
			 * in the skywalk_shutdown tests and we'll get stuck.
			 */
			assert(os_channel_available_slot_count(txring));
		}

		for (j = 0; j < i; j++) {
			send_bytes(txring, i);
		}

		error = os_channel_sync(channel, CHANNEL_SYNC_TX);
		SKTC_ASSERT_ERR(!error);

		test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_2);
		test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_1);
	}
	test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_2);

	/* Test TX data reporting - start by filling the RX ring */
	test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_1);

	for (j = 0; j < lim; j++) {
		send_bytes(txring, j);
	}
	error = os_channel_sync(channel, CHANNEL_SYNC_TX);
	SKTC_ASSERT_ERR(!error);

	/* Send more packets and confirm TX backs up */
	for (i = 0; i < lim; i++) {
		send_bytes(txring, 8);
		error = os_channel_sync(channel, CHANNEL_SYNC_TX);
		SKTC_ASSERT_ERR(!error);
		assert(os_channel_available_slot_count(txring) == lim - i - 1);
	}

	/* Confirm we time out waiting for more slots */
	error = wait_on_fd(kq_fd, EVFILT_WRITE, channel, 0, TIMEOUT_EXPECT);
	SKTC_ASSERT_ERR(!error);

	/* Start draining the rings */
	test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_2);
	test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_1);
	error = os_channel_sync(channel, CHANNEL_SYNC_TX);
	SKTC_ASSERT_ERR(!error);

	/* Drain the rings and confirm reporting is accurate */
	for (i = 1; i <= lim; i++) {
		test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_2);
		test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_TX_SWEEP_1);
		error = wait_on_fd(kq_fd, EVFILT_WRITE, channel, 0, TIMEOUT_FAIL);
		SKTC_ASSERT_ERR(error == i);
		error = os_channel_sync(channel, CHANNEL_SYNC_TX);
		SKTC_ASSERT_ERR(!error);
	}

	os_channel_attr_destroy(ch_attr);
	os_channel_destroy(channel);
	return 0;
}

static int
skt_kqueue_basic_main(int argc, char *argv[])
{
	return skt_kqueue_main(argc, argv, 0,
	           &skt_kqueue_basic_rx,
	           &skt_kqueue_basic_tx);
}

/************************************************************************
 * Basic lowat test
 */

#define LOWAT_TYPE                      0x00000001
#define LOWAT_TYPE_CHAN         0x00000000
#define LOWAT_TYPE_NOTE         0x00000001

#define LOWAT_UNIT                      0x00000010
#define LOWAT_UNIT_SLOTS        0x00000000
#define LOWAT_UNIT_BYTES        0x00000010

enum kqueue_lowat_basic_test_stage {
	SKT_KQUEUE_LOWAT_BASIC_1=1,
	SKT_KQUEUE_LOWAT_BASIC_2,
	SKT_KQUEUE_LOWAT_BASIC_3
};

static void *
skt_kqueue_lowat_basic_rx(void *ctx_)
{
	struct context *ctx = (struct context *)ctx_;
	channel_t channel;
	channel_ring_t rxring;
	channel_attr_t ch_attr;
	ring_dir_t ring_dir = CHANNEL_DIR_RX;
	ring_id_t ring_id = CHANNEL_RING_ID_ANY;
	uint32_t port = 0;
	int kq_fd;
	channel_threshold_unit_t lowat_unit;
	int lowat_val;
	int error;
	int i;

	assert(ctx->stage.test_stage == SKT_KQUEUE_INIT);

	/* Initialize kqueue */
	kq_fd = kqueue();
	assert(kq_fd >= 0);

	/* Initialize channel */
	ch_attr = os_channel_attr_create();
	channel = sktu_channel_create_extended(
		ctx->nexus_uuid,
		port, ring_dir, ring_id,
		ch_attr,
		-1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
	assert(channel != NULL);

	ring_id = os_channel_ring_id(channel, CHANNEL_FIRST_RX_RING);
	rxring = os_channel_rx_ring(channel, ring_id);
	assert(rxring);

	/* Set up watermarks */
	if ((ctx->test_selector & LOWAT_UNIT) == LOWAT_UNIT_BYTES) {
		lowat_unit = CHANNEL_THRESHOLD_UNIT_BYTES;
	} else {
		lowat_unit = CHANNEL_THRESHOLD_UNIT_SLOTS;
	}
	if ((ctx->test_selector & LOWAT_TYPE) == LOWAT_TYPE_CHAN) {
		set_watermark(channel, false, lowat_unit, 10);
		set_watermark(channel, true, lowat_unit, 15);
		lowat_val = 0;
	} else {
		set_watermark(channel, false, lowat_unit, 1);
		set_watermark(channel, true, lowat_unit, 1);
		lowat_val = 10;
	}
	test_stage_change(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_1);

	/* The first N waits should time out (3 waits for slots, 1 wait for
	 * bytes) */
	int N;
	if (lowat_unit == CHANNEL_THRESHOLD_UNIT_SLOTS) {
		N = 3;
	} else {
		N = 1;
	}
	for (i = 0; i < N; i++) {
		error = wait_on_fd(kq_fd, EVFILT_READ, channel, lowat_val,
		    TIMEOUT_EXPECT);
		SKTC_ASSERT_ERR(error == 0);
		error = os_channel_sync(channel, CHANNEL_SYNC_RX);
		SKTC_ASSERT_ERR(error == 0);
		test_stage_wait(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_2);
		test_stage_change(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_1);
	}

	/* The next wait should trigger */
	error = wait_on_fd(kq_fd, EVFILT_READ, channel, lowat_val, TIMEOUT_FAIL);
	SKTC_ASSERT_ERR(error == 12);
	chew_slots(rxring, 0);
	error = os_channel_sync(channel, CHANNEL_SYNC_RX);
	SKTC_ASSERT_ERR(error == 0);
	test_stage_wait(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_2);
	test_stage_change(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_1);

	/* Do it all again, but make sure the TX thread transmits while
	 * we're waiting this time */
	/* (TODO: is there a better way to do this, without the TX thread
	 * sleeping an arbitrary amount of time?) */
	error = wait_on_fd(kq_fd, EVFILT_READ, channel, lowat_val, TIMEOUT_DISABLE);
	SKTC_ASSERT_ERR(error == 12);
	chew_slots(rxring, 0);
	error = os_channel_sync(channel, CHANNEL_SYNC_RX);
	SKTC_ASSERT_ERR(error == 0);
	test_stage_wait(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_2);
	test_stage_change(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_1);

	/* Let the TX thread fill our RX ring up, to test TX watermarks */
	test_stage_wait(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_2);
	error = os_channel_sync(channel, CHANNEL_SYNC_RX);
	SKTC_ASSERT_ERR(error == 0);

	/* Free up some slots for TX, enough to trigger its TX watermark but
	 * not its RX watermark */
	for (i = 0; i < 5; i++) {
		chew_slots(rxring, 1);
		error = os_channel_sync(channel, CHANNEL_SYNC_RX);
		SKTC_ASSERT_ERR(error == 0);
	}

	test_stage_change(&ctx->stage, SKT_KQUEUE_BASIC_RX_SWEEP_1);
	test_stage_wait(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_2);

	/* Chew more slots to wake TX up, but wait a bit first to make sure TX
	 * is really sleeping (TODO: better way to do this?) */
	usleep(50000);
	for (i = 0; i < 5; i++) {
		chew_slots(rxring, 1);
		error = os_channel_sync(channel, CHANNEL_SYNC_RX);
		SKTC_ASSERT_ERR(error == 0);
	}

	os_channel_attr_destroy(ch_attr);
	os_channel_destroy(channel);
	return 0;
}

static void *
skt_kqueue_lowat_basic_tx(void *ctx_)
{
	struct context *ctx = (struct context *)ctx_;
	channel_t channel;
	channel_ring_t txring;
	channel_attr_t ch_attr;
	ring_dir_t ring_dir = CHANNEL_DIR_TX;
	ring_id_t ring_id = CHANNEL_RING_ID_ANY;
	uint32_t port = 1;
	int kq_fd;
	channel_threshold_unit_t lowat_unit;
	int lowat_val;
	int note_lowat_val;
	int slot_size;
	int error;
	int i;

	kq_fd = kqueue();
	assert(kq_fd >= 0);

	ch_attr = os_channel_attr_create();
	channel = sktu_channel_create_extended(
		ctx->nexus_uuid,
		port, ring_dir, ring_id,
		ch_attr,
		-1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
	assert(channel != NULL);

	slot_size = channel->chd_info->cinfo_nxprov_params.nxp_buf_size;

	ring_id = os_channel_ring_id(channel, CHANNEL_FIRST_TX_RING);
	txring = os_channel_tx_ring(channel, ring_id);
	assert(txring);

	if ((ctx->test_selector & LOWAT_UNIT) == LOWAT_UNIT_BYTES) {
		lowat_unit = CHANNEL_THRESHOLD_UNIT_BYTES;
	} else {
		lowat_unit = CHANNEL_THRESHOLD_UNIT_SLOTS;
	}

	/* Wait for RX to initialize */
	test_stage_wait(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_1);

	/* Send packets in bursts of 3 to allow RX to confirm its watermark is
	 * being respected */
	int N;
	if (lowat_unit == CHANNEL_THRESHOLD_UNIT_SLOTS) {
		N = 4;
	} else {
		N = 2;
	}
	for (i = 0; i < N; i++) {
		send_bytes(txring, 2);
		send_bytes(txring, 2);
		send_bytes(txring, 2);
		error = os_channel_sync(channel, CHANNEL_SYNC_TX);
		SKTC_ASSERT_ERR(error == 0);
		test_stage_change(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_2);
		test_stage_wait(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_1);
	}

	/* Send packets in bursts of 3 again, but wait a bit to give the RX
	 * thread time to wait (TODO: is there a better way to do this, without
	 * sleeping an arbitrary amount of time?) */
	usleep(50000);
	for (i = 0; i < N; i++) {
		send_bytes(txring, 2);
		send_bytes(txring, 2);
		send_bytes(txring, 2);
		error = os_channel_sync(channel, CHANNEL_SYNC_TX);
		SKTC_ASSERT_ERR(error == 0);
	}

	test_stage_change(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_2);
	test_stage_wait(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_1);
	error = os_channel_sync(channel, CHANNEL_SYNC_TX);
	SKTC_ASSERT_ERR(error == 0);

	/* Test TX watermarks - start by filling the RX ring */
	int lim =  ch_attr->cha_tx_slots - 1;
	for (i = 0; i < lim; i++) {
		send_bytes(txring, 5);
	}
	error = os_channel_sync(channel, CHANNEL_SYNC_TX);
	SKTC_ASSERT_ERR(error == 0);

	/* Send more packets and confirm TX backs up _almost_ all the way */
	for (i = 0; i < lim - 5; i++) {
		send_bytes(txring, 8);
		error = os_channel_sync(channel, CHANNEL_SYNC_TX);
		SKTC_ASSERT_ERR(error == 0);
		assert(os_channel_available_slot_count(txring) == lim - i - 1);
	}

	/* Set up watermarks */
	lowat_val = 10;
	if (lowat_unit == CHANNEL_THRESHOLD_UNIT_BYTES) {
		lowat_val *= slot_size;
	}
	if ((ctx->test_selector & LOWAT_TYPE) == LOWAT_TYPE_CHAN) {
		set_watermark(channel, false, lowat_unit, lowat_val * 2);
		set_watermark(channel, true, lowat_unit, lowat_val);
		note_lowat_val = 0;
	} else {
		set_watermark(channel, false, lowat_unit, 1);
		set_watermark(channel, true, lowat_unit, 1);
		note_lowat_val = lowat_val;
	}

	/* Wait for TX slots, confirm that even though some are available
	 * there aren't enough to trigger the watermark */
	assert(os_channel_available_slot_count(txring) > 0);
	error = wait_on_fd(kq_fd, EVFILT_WRITE, channel, note_lowat_val,
	    TIMEOUT_EXPECT);
	SKTC_ASSERT_ERR(error == 0);

	/* Let the RX thread drain some slots and try again */
	test_stage_change(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_2);
	test_stage_wait(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_1);

	error = wait_on_fd(kq_fd, EVFILT_WRITE, channel, note_lowat_val,
	    TIMEOUT_FAIL);
	assert(error == lowat_val);

	/* Do it all again, but make sure RX triggers the watermark _while_
	 * we're sleeping this time */
	/* TODO: is there a better way to do this than having the RX
	 * thread sleep? */
	lowat_val = 15;
	if (lowat_unit == CHANNEL_THRESHOLD_UNIT_BYTES) {
		lowat_val *= slot_size;
	}
	if ((ctx->test_selector & LOWAT_TYPE) == LOWAT_TYPE_CHAN) {
		set_watermark(channel, false, lowat_unit, lowat_val * 2);
		set_watermark(channel, true, lowat_unit, lowat_val);
		note_lowat_val = 0;
	} else {
		set_watermark(channel, false, lowat_unit, 1);
		set_watermark(channel, true, lowat_unit, 1);
		note_lowat_val = lowat_val;
	}
	test_stage_change(&ctx->stage, SKT_KQUEUE_LOWAT_BASIC_2);
	error = wait_on_fd(kq_fd, EVFILT_WRITE, channel, note_lowat_val,
	    TIMEOUT_DISABLE);
	SKTC_ASSERT_ERR(error == lowat_val);

	os_channel_attr_destroy(ch_attr);
	os_channel_destroy(channel);
	return 0;
}

static int
skt_kqueue_lowat_chan_bytes_main(int argc, char *argv[])
{
	return skt_kqueue_main(argc, argv, LOWAT_TYPE_CHAN | LOWAT_UNIT_BYTES,
	           &skt_kqueue_lowat_basic_rx,
	           &skt_kqueue_lowat_basic_tx);
}

static int
skt_kqueue_lowat_chan_slots_main(int argc, char *argv[])
{
	return skt_kqueue_main(argc, argv, LOWAT_TYPE_CHAN | LOWAT_UNIT_SLOTS,
	           &skt_kqueue_lowat_basic_rx,
	           &skt_kqueue_lowat_basic_tx);
}

static int
skt_kqueue_lowat_note_bytes_main(int argc, char *argv[])
{
	return skt_kqueue_main(argc, argv, LOWAT_TYPE_NOTE | LOWAT_UNIT_BYTES,
	           &skt_kqueue_lowat_basic_rx,
	           &skt_kqueue_lowat_basic_tx);
}

static int
skt_kqueue_lowat_note_slots_main(int argc, char *argv[])
{
	return skt_kqueue_main(argc, argv, LOWAT_TYPE_NOTE | LOWAT_UNIT_SLOTS,
	           &skt_kqueue_lowat_basic_rx,
	           &skt_kqueue_lowat_basic_tx);
}

/****************************************************************
 * TODO: Tests to write:
 *	- kqueue_selwakeup - Test that wakeups associated with one channel
 *		descriptor don't spuriously wake up other threads waiting on other
 *		channels, or on channel descriptors that don't include the rings
 *		relevant to the waking event
 *	- lowat_edge - Test edge cases related to low watermarks: changing the
 *		unit while running, wakeups as a result of that unit change, issuing
 *              the watermark in the knote vs the channel, etc
 */

struct skywalk_test skt_kqueue_basic = {
	"kqueue_basic", "tests kqueue return values",
	SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
	skt_kqueue_basic_main, SKTC_GENERIC_UPIPE_ARGV,
	skt_kqueue_init, skt_kqueue_fini,
};

struct skywalk_test skt_kqueue_lowat_chan_bytes = {
	"kqueue_lowat_chan_bytes", "tests kqueue low watermark (byte watermark on channel)",
	SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
	skt_kqueue_lowat_chan_bytes_main, SKTC_GENERIC_UPIPE_ARGV,
	skt_kqueue_init, skt_kqueue_fini,
};

struct skywalk_test skt_kqueue_lowat_chan_slots = {
	"kqueue_lowat_chan_slots", "tests kqueue low watermark (slot watermark on channel)",
	SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
	skt_kqueue_lowat_chan_slots_main, SKTC_GENERIC_UPIPE_ARGV,
	skt_kqueue_init, skt_kqueue_fini,
};

struct skywalk_test skt_kqueue_lowat_note_bytes = {
	"kqueue_lowat_note_bytes", "tests kqueue low watermark (byte watermark on knote)",
	SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
	skt_kqueue_lowat_note_bytes_main, SKTC_GENERIC_UPIPE_ARGV,
	skt_kqueue_init, skt_kqueue_fini,
};

struct skywalk_test skt_kqueue_lowat_note_slots = {
	"kqueue_lowat_note_slots", "tests kqueue low watermark (slot watermark on knote)",
	SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
	skt_kqueue_lowat_note_slots_main, SKTC_GENERIC_UPIPE_ARGV,
	skt_kqueue_init, skt_kqueue_fini,
};

/****************************************************************/