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 | /* * 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 <unistd.h> #include <uuid/uuid.h> #include <sys/select.h> #include <poll.h> #include <sys/event.h> #include <sys/sysctl.h> #include <darwintest.h> #include "skywalk_test_driver.h" #include "skywalk_test_common.h" #include "skywalk_test_utils.h" static int skt_oneslot_common(int argc, char *argv[], int method, bool defunct) { int error; channel_t channel; uuid_t channel_uuid; ring_id_t ringid; channel_ring_t ring; channel_slot_t slot; uint32_t avail; slot_prop_t prop; int channelfd; fd_set rfdset, wfdset, efdset; struct pollfd fds; int kq; struct kevent kev; const char msg[] = "Time flies like an arrow; fruit flies like a banana."; error = uuid_parse(argv[3], channel_uuid); SKTC_ASSERT_ERR(!error); channel = sktu_channel_create_extended(channel_uuid, 0, CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1); assert(channel); channelfd = os_channel_get_fd(channel); assert(channelfd != -1); if (defunct) { /* * In this test case the other end of the user-pipe is not * eligible for defunct, so this defunct call should have no * impact on this channel and the data path should still work. */ error = pid_shutdown_sockets(getpid(), SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL); SKTC_ASSERT_ERR(!error); } switch (method) { case 0: FD_ZERO(&rfdset); FD_ZERO(&wfdset); FD_ZERO(&efdset); FD_SET(channelfd, &rfdset); FD_SET(channelfd, &wfdset); FD_SET(channelfd, &efdset); error = select(channelfd + 1, &rfdset, &wfdset, &efdset, NULL); SKTC_ASSERT_ERR(error != -1); assert(!FD_ISSET(channelfd, &rfdset)); assert(FD_ISSET(channelfd, &wfdset)); assert(!FD_ISSET(channelfd, &efdset)); SKTC_ASSERT_ERR(error == 1); break; case 1: fds.fd = channelfd; fds.events = POLLWRNORM; fds.revents = 0; error = poll(&fds, 1, -1); SKTC_ASSERT_ERR(error != -1); SKTC_ASSERT_ERR(error == 1); assert(fds.fd == channelfd); assert(fds.events == POLLWRNORM); assert(fds.revents == POLLWRNORM); break; case 2: kq = kqueue(); assert(kq != -1); EV_SET(&kev, channelfd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, NULL); error = kevent(kq, &kev, 1, &kev, 1, NULL); SKTC_ASSERT_ERR(error != -1); SKTC_ASSERT_ERR(error == 1); assert(kev.filter == EVFILT_WRITE); assert(kev.ident == channelfd); assert(kev.udata == NULL); assert((kev.flags & EV_ERROR) == 0); close(kq); break; default: abort(); } ringid = os_channel_ring_id(channel, CHANNEL_FIRST_TX_RING); ring = os_channel_tx_ring(channel, ringid); assert(ring); avail = os_channel_available_slot_count(ring); assert(avail); slot = os_channel_get_next_slot(ring, NULL, &prop); assert(slot); assert(prop.sp_buf_ptr); assert(prop.sp_len); memcpy((void *)prop.sp_buf_ptr, msg, sizeof(msg)); prop.sp_len = sizeof(msg); os_channel_set_slot_properties(ring, slot, &prop); error = os_channel_advance_slot(ring, slot); SKTC_ASSERT_ERR(!error); error = os_channel_sync(channel, CHANNEL_SYNC_TX); SKTC_ASSERT_ERR(!error); switch (method) { case 0: FD_ZERO(&rfdset); FD_ZERO(&efdset); FD_SET(channelfd, &rfdset); FD_SET(channelfd, &efdset); error = select(channelfd + 1, &rfdset, NULL, &efdset, NULL); SKTC_ASSERT_ERR(error != -1); assert(!FD_ISSET(channelfd, &efdset)); assert(FD_ISSET(channelfd, &wfdset)); SKTC_ASSERT_ERR(error == 1); break; case 1: fds.fd = channelfd; fds.events = POLLRDNORM; fds.revents = 0; error = poll(&fds, 1, -1); SKTC_ASSERT_ERR(error != -1); SKTC_ASSERT_ERR(error == 1); assert(fds.fd == channelfd); assert(fds.events == POLLRDNORM); assert(fds.revents == POLLRDNORM); break; case 2: kq = kqueue(); assert(kq != -1); EV_SET(&kev, channelfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, NULL); error = kevent(kq, &kev, 1, &kev, 1, NULL); SKTC_ASSERT_ERR(error != -1); SKTC_ASSERT_ERR(error == 1); assert(kev.filter == EVFILT_READ); assert(kev.ident == channelfd); assert(kev.udata == NULL); assert((kev.flags & EV_ERROR) == 0); close(kq); break; default: abort(); } ringid = os_channel_ring_id(channel, CHANNEL_FIRST_RX_RING); ring = os_channel_rx_ring(channel, ringid); assert(ring); avail = os_channel_available_slot_count(ring); assert(avail); slot = os_channel_get_next_slot(ring, NULL, &prop); assert(slot); assert(prop.sp_buf_ptr); assert(!memcmp((void *)prop.sp_buf_ptr, msg, sizeof(msg))); assert(prop.sp_len == sizeof(msg)); //T_LOG("Got message \"%s\" len %d\n", (char *)prop.sp_buf_ptr, prop.sp_len); // XXX test this? //prop.sp_len = 0; //os_channel_set_slot_properties(ring, slot, &prop); error = os_channel_advance_slot(ring, slot); SKTC_ASSERT_ERR(!error); error = os_channel_sync(channel, CHANNEL_SYNC_RX); SKTC_ASSERT_ERR(!error); os_channel_destroy(channel); return 0; } static int skt_oneslot_select_main(int argc, char *argv[]) { return skt_oneslot_common(argc, argv, 0, false); } static int skt_oneslot_poll_main(int argc, char *argv[]) { return skt_oneslot_common(argc, argv, 1, false); } static int skt_oneslot_kqueue_main(int argc, char *argv[]) { return skt_oneslot_common(argc, argv, 2, false); } static int skt_oneslot_kqueue_defunct_main(int argc, char *argv[]) { return skt_oneslot_common(argc, argv, 2, true); } void skt_oneslot_upipe_echo_defunct_init(void) { sktc_generic_upipe_nexus_init(); sktc_setup_channel_worker(sktc_instance_uuid, 1, CHANNEL_RING_ID_ANY, NULL, 0, true, false); } struct skywalk_test skt_oneslotus = { "oneslotus", "test sends one slot of data on user pipe loopback using select", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, skt_oneslot_select_main, SKTC_GENERIC_UPIPE_ARGV, sktc_generic_upipe_echo_init, sktc_generic_upipe_fini, }; struct skywalk_test skt_oneslotks = { "oneslotks", "test sends one slot of data on kpipe loopback using select", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_KERNEL_PIPE | SK_FEATURE_NEXUS_KERNEL_PIPE_LOOPBACK, skt_oneslot_select_main, SKTC_GENERIC_KPIPE_ARGV, sktc_generic_kpipe_init, sktc_generic_kpipe_fini, }; struct skywalk_test skt_oneslotup = { "oneslotup", "test sends one slot of data on user pipe loopback using poll", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, skt_oneslot_poll_main, SKTC_GENERIC_UPIPE_ARGV, sktc_generic_upipe_echo_init, sktc_generic_upipe_fini, }; struct skywalk_test skt_oneslotkp = { "oneslotkp", "test sends one slot of data on kpipe loopback using poll", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_KERNEL_PIPE | SK_FEATURE_NEXUS_KERNEL_PIPE_LOOPBACK, skt_oneslot_poll_main, SKTC_GENERIC_KPIPE_ARGV, sktc_generic_kpipe_init, sktc_generic_kpipe_fini, }; struct skywalk_test skt_oneslotuk = { "oneslotuk", "test sends one slot of data on user pipe loopback using kqueue", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, skt_oneslot_kqueue_main, SKTC_GENERIC_UPIPE_ARGV, sktc_generic_upipe_echo_init, sktc_generic_upipe_fini, }; struct skywalk_test skt_oneslotkk = { "oneslotkk", "test sends one slot of data on kpipe loopback using kqueue", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_KERNEL_PIPE | SK_FEATURE_NEXUS_KERNEL_PIPE_LOOPBACK, skt_oneslot_kqueue_main, SKTC_GENERIC_KPIPE_ARGV, sktc_generic_kpipe_init, sktc_generic_kpipe_fini, }; struct skywalk_test skt_oneslotuk_defunct = { "oneslotukd", "test sends one slot of data on user pipe loopback using kqueue with one end of the pipe defuncted", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, skt_oneslot_kqueue_defunct_main, SKTC_GENERIC_UPIPE_ARGV, skt_oneslot_upipe_echo_defunct_init, sktc_generic_upipe_fini, }; |