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 | /* * 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 <errno.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <uuid/uuid.h> #include <sys/select.h> #include <sys/mman.h> #include <mach/vm_map.h> #include <darwintest.h> #include "skywalk_test_driver.h" #include "skywalk_test_common.h" #include "skywalk_test_utils.h" static int skt_writeif_main(int argc, char *argv[]) { int error; channel_t channel; uuid_t channel_uuid; 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); assert(channel); *(char *)channel->chd_schema->csm_kern_name = 'X'; return 1; // shouldn't be reached } struct skywalk_test skt_writeif = { .skt_testname = "writeif", .skt_testdesc = "writes to the read only channel if", .skt_required_features = SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, .skt_main = skt_writeif_main, .skt_argv = SKTC_GENERIC_UPIPE_ARGV, .skt_init = sktc_generic_upipe_null_init, .skt_fini = sktc_generic_upipe_fini, .skt_expected_exception_code = 0xa100002, .skt_expected_exception_code_ignore = 0, }; static int skt_writering_main(int argc, char *argv[]) { int error; channel_t channel; uuid_t channel_uuid; 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); assert(channel); ring_id_t ringid; channel_ring_t ring; ringid = os_channel_ring_id(channel, CHANNEL_FIRST_TX_RING); ring = os_channel_tx_ring(channel, ringid); assert(ring); assert((intptr_t)channel->chd_schema + channel->chd_schema->csm_ring_ofs[0].ring_off == (intptr_t)ring->chrd_ring); /* Write garbage to ring descriptors */ memset((void *)ring->chrd_ring, 0x5a, sizeof(*ring->chrd_ring)); error = os_channel_sync(channel, CHANNEL_SYNC_TX); /* Expect failure (and a process crash) here */ /* * Sanity checks to localize failures, in case the crash doesn't * succeed: */ SKTC_ASSERT_ERR(error); SKTC_ASSERT_ERR(errno == EFAULT); return 1; } struct skywalk_test skt_writering = { .skt_testname = "writering", .skt_testdesc = "writes to the writeable ring", .skt_required_features = SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, .skt_main = skt_writering_main, .skt_argv = SKTC_GENERIC_UPIPE_ARGV, .skt_init = sktc_generic_upipe_null_init, .skt_fini = sktc_generic_upipe_fini, .skt_expected_exception_code = SIGABRT << 24, .skt_expected_exception_code_ignore = 0, }; static int skt_readsmap_main(int argc, char *argv[]) { int error; channel_t channel; uuid_t channel_uuid; uint8_t byte; 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); assert(channel); //T_LOG("ch_if 0x%p offset 0x%llx pointer 0x%p\n", // channel->chd_schema, channel->chd_schema->csm_ring_ofs[0].sd_off, // (char *)channel->chd_schema+channel->chd_schema->csm_ring_ofs[0].sd_off); // Verify we can read it memcpy(&byte, (void *)channel->chd_schema + channel->chd_schema->csm_ring_ofs[0].sd_off, 1); return 0; } struct skywalk_test skt_readsmap = { .skt_testname = "readsmap", .skt_testdesc = "reads from the read only smap", .skt_required_features = SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, .skt_main = skt_readsmap_main, .skt_argv = SKTC_GENERIC_UPIPE_ARGV, .skt_init = sktc_generic_upipe_null_init, .skt_fini = sktc_generic_upipe_fini, }; static int skt_writesmap_main(int argc, char *argv[]) { int error; channel_t channel; uuid_t channel_uuid; uint8_t byte; 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); assert(channel); //T_LOG("ch_if 0x%p offset 0x%llx pointer 0x%p\n", // channel->chd_schema, channel->chd_schema->csm_ring_ofs[0].sd_off, // (char *)channel->chd_schema+channel->chd_schema->csm_ring_ofs[0].sd_off); // Verify we can read it memcpy(&byte, (void *)channel->chd_schema + channel->chd_schema->csm_ring_ofs[0].sd_off, 1); // Now try to write it memcpy((void *)channel->chd_schema + channel->chd_schema->csm_ring_ofs[0].sd_off, &byte, 1); return 1; // shouldn't be reached } struct skywalk_test skt_writesmap = { .skt_testname = "writesmap", .skt_testdesc = "writes to the read only smap", .skt_required_features = SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, .skt_main = skt_writesmap_main, .skt_argv = SKTC_GENERIC_UPIPE_ARGV, .skt_init = sktc_generic_upipe_null_init, .skt_fini = sktc_generic_upipe_fini, .skt_expected_exception_code = 0xa100002, .skt_expected_exception_code_ignore = 0, }; /****************************************************************/ /* * Nexus region verification tests. */ struct sktc_nexus_handles ms_fsw_handle; static uuid_string_t ms_fsw_uuid_string; #define SKT_VERIFY_NXADV_REGION 1 #define SKT_VERIFY_NXADV_REGION_STR "1" static void skt_ms_fsw_init(void) { uuid_t flow_id; int error; sktc_ifnet_feth0_create(); sktc_create_flowswitch(&ms_fsw_handle, 0); uuid_unparse(ms_fsw_handle.fsw_nx_uuid, ms_fsw_uuid_string); /* * flowswitch doesn't allow opening a channel without a flow bound * to the port. */ uuid_generate(flow_id); error = sktc_bind_tcp4_flow(ms_fsw_handle.controller, ms_fsw_handle.fsw_nx_uuid, 0, NEXUS_PORT_FLOW_SWITCH_CLIENT, flow_id); /* Don't assert as this is running in the test driver */ if (error) { T_LOG("func %s sktc_bind_tcp4_flow returned error %d " "errno %d: %s\n", __func__, error, errno, strerror(errno)); } } static void skt_ms_fsw_fini(void) { sktc_cleanup_flowswitch(&ms_fsw_handle); sktc_ifnet_feth0_destroy(); } static void skt_verify_nxadv_region(channel_t channel) { struct sk_nexusadv *region; uint64_t region_size; channel_attr_t attr; int error; attr = os_channel_attr_create(); assert(attr != NULL); error = os_channel_read_attr(channel, attr); SKTC_ASSERT_ERR(!error); /* verify size of the region */ os_channel_attr_get(attr, CHANNEL_ATTR_NEXUS_ADV_SIZE, ®ion_size); assert(region_size == sizeof(*region)); /* verify version number */ region = os_channel_get_advisory_region(channel); assert(region != NULL); assert(region->nxadv_ver == NX_ADVISORY_CURRENT_VERSION); /* write should fault as the region is mapped read-only */ region->nxadv_ver = 0; /* shouldn't be reached */ error = 1; SKTC_ASSERT_ERR(!error); } int skt_nxregion_verify_main(int argc, char *argv[]) { int error; int test_id; uuid_t fsw_uuid; channel_t channel; error = uuid_parse(argv[3], fsw_uuid); SKTC_ASSERT_ERR(error == 0); test_id = atoi(argv[4]); T_LOG("opening channel %d on fsw %s\n", NEXUS_PORT_FLOW_SWITCH_CLIENT, argv[3]); /* must fail without user packet pool set (flow switch) */ assert(sktu_channel_create_extended(fsw_uuid, NEXUS_PORT_FLOW_SWITCH_CLIENT, CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL, -1, -1, -1, -1, -1, -1, 1, -1, -1) == NULL); channel = sktu_channel_create_extended(fsw_uuid, NEXUS_PORT_FLOW_SWITCH_CLIENT, CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL, -1, -1, -1, -1, -1, 1, 1, -1, -1); assert(channel); switch (test_id) { case SKT_VERIFY_NXADV_REGION: skt_verify_nxadv_region(channel); break; default: assert(0); } os_channel_destroy(channel); return 0; } struct skywalk_test skt_verifynxadv = { .skt_testname = "verifynadv", .skt_testdesc = "verifies nexus advisory region", .skt_required_features = SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_NETIF | SK_FEATURE_NEXUS_FLOWSWITCH | SK_FEATURE_NETNS, .skt_main = skt_nxregion_verify_main, .skt_argv = { NULL, NULL, NULL, ms_fsw_uuid_string, SKT_VERIFY_NXADV_REGION_STR}, .skt_init = skt_ms_fsw_init, .skt_fini = skt_ms_fsw_fini, .skt_expected_exception_code = 0xa100002, .skt_expected_exception_code_ignore = 0, }; |