xref: /xnu-11215.41.3/tests/skywalk/skt_debug_verify.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2016-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <uuid/uuid.h>
35 #include <sys/sysctl.h>
36 #include <sys/event.h>
37 #include <darwintest.h>
38 #include "skywalk_test_driver.h"
39 #include "skywalk_test_common.h"
40 #include "skywalk_test_utils.h"
41 
42 static int old_debug_value;
43 
44 static int
skt_debug_verify_main(int argc,char * argv[])45 skt_debug_verify_main(int argc, char *argv[])
46 {
47 	int error;
48 	channel_t channel;
49 	uuid_t channel_uuid;
50 	ring_id_t ringid;
51 	channel_ring_t rxring, txring;
52 	channel_slot_t slot;
53 	uint32_t avail;
54 	slot_prop_t prop;
55 	int channelfd;
56 	int kq;
57 	struct kevent kev;
58 	const char msg[] = "Time flies like an arrow;  fruit flies like a banana.";
59 
60 	error = uuid_parse(argv[3], channel_uuid);
61 	SKTC_ASSERT_ERR(!error);
62 
63 	channel = sktu_channel_create_extended(channel_uuid, 0,
64 	    CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL,
65 	    -1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
66 	assert(channel);
67 
68 	channelfd = os_channel_get_fd(channel);
69 	assert(channelfd != -1);
70 
71 	kq = kqueue();
72 	assert(kq != -1);
73 	EV_SET(&kev, channelfd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, NULL);
74 	error = kevent(kq, &kev, 1, &kev, 1, NULL);
75 	SKTC_ASSERT_ERR(error != -1);
76 	SKTC_ASSERT_ERR(error == 1);
77 	assert(kev.filter == EVFILT_WRITE);
78 	assert(kev.ident == channelfd);
79 	assert(kev.udata == NULL);
80 	close(kq);
81 
82 	ringid = os_channel_ring_id(channel, CHANNEL_FIRST_TX_RING);
83 	txring = os_channel_tx_ring(channel, ringid);
84 	assert(txring);
85 
86 	avail = os_channel_available_slot_count(txring);
87 	assert(avail);
88 
89 	slot = os_channel_get_next_slot(txring, NULL, &prop);
90 	assert(slot);
91 
92 	assert(prop.sp_buf_ptr);
93 	assert(prop.sp_len);
94 
95 	memcpy((void *)prop.sp_buf_ptr, msg, sizeof(msg));
96 	prop.sp_len = sizeof(msg);
97 	os_channel_set_slot_properties(txring, slot, &prop);
98 
99 	error = os_channel_advance_slot(txring, slot);
100 	SKTC_ASSERT_ERR(!error);
101 
102 	error = os_channel_sync(channel, CHANNEL_SYNC_TX);
103 	SKTC_ASSERT_ERR(!error);
104 
105 	kq = kqueue();
106 	assert(kq != -1);
107 	EV_SET(&kev, channelfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, NULL);
108 	error = kevent(kq, &kev, 1, &kev, 1, NULL);
109 	SKTC_ASSERT_ERR(error != -1);
110 	SKTC_ASSERT_ERR(error == 1);
111 	assert(kev.filter == EVFILT_READ);
112 	assert(kev.ident == channelfd);
113 	assert(kev.udata == NULL);
114 	close(kq);
115 
116 	ringid = os_channel_ring_id(channel, CHANNEL_FIRST_RX_RING);
117 	rxring = os_channel_rx_ring(channel, ringid);
118 	assert(rxring);
119 
120 	avail = os_channel_available_slot_count(rxring);
121 	assert(avail);
122 
123 	slot = os_channel_get_next_slot(rxring, NULL, &prop);
124 	assert(slot);
125 
126 	assert(prop.sp_buf_ptr);
127 	assert(!memcmp((void *)prop.sp_buf_ptr, msg, sizeof(msg)));
128 	assert(prop.sp_len == sizeof(msg));
129 
130 	/* Finish up */
131 
132 	error = os_channel_advance_slot(rxring, slot);
133 	SKTC_ASSERT_ERR(!error);
134 
135 	error = os_channel_sync(channel, CHANNEL_SYNC_RX);
136 	SKTC_ASSERT_ERR(!error);
137 
138 	os_channel_destroy(channel);
139 
140 	return 0;
141 }
142 
143 /*
144  * TODO: Right now, the flag macros for the skywalk.debug register are in a
145  * private kernel header. Ideally we would specify SKF_VERIFY instead of the
146  * direct value '1' here, and at some point should fix things up so we can.
147  * See: <rdar://problem/26142697> Allow Skywalk unit tests to specify both
148  * development and debug kernel compatibility
149  */
150 void
skt_debug_verify_u_init(void)151 skt_debug_verify_u_init(void)
152 {
153 	int error;
154 
155 	int new_debug_value = 1;
156 	size_t old_size = sizeof(int);
157 	error = sysctlbyname("kern.skywalk.debug", &old_debug_value, &old_size,
158 	    &new_debug_value, sizeof(int));
159 	if (error) {
160 		T_LOG("%s: warning sysctl(\"kern.skywalk.debug\" returned %d: %s",
161 		    __func__, error, strerror(errno));
162 	}
163 
164 	sktc_generic_upipe_echo_init();
165 }
166 
167 void
skt_debug_verify_u_fini(void)168 skt_debug_verify_u_fini(void)
169 {
170 	int error;
171 
172 	sktc_generic_upipe_fini();
173 
174 	error = sysctlbyname("kern.skywalk.debug", NULL, 0, &old_debug_value,
175 	    sizeof(int));
176 	if (error) {
177 		T_LOG("%s: warning sysctl(\"kern.skywalk.debug\" returned %d: %s",
178 		    __func__, error, strerror(errno));
179 	}
180 }
181 
182 void
skt_debug_verify_k_init(void)183 skt_debug_verify_k_init(void)
184 {
185 	int error;
186 
187 	int new_debug_value = 1;
188 	size_t old_size = sizeof(int);
189 	error = sysctlbyname("kern.skywalk.debug", &old_debug_value, &old_size,
190 	    &new_debug_value, sizeof(int));
191 	if (error) {
192 		T_LOG("%s: warning sysctl(\"kern.skywalk.debug\" returned %d: %s",
193 		    __func__, error, strerror(errno));
194 	}
195 
196 	sktc_generic_kpipe_init();
197 }
198 
199 void
skt_debug_verify_k_fini(void)200 skt_debug_verify_k_fini(void)
201 {
202 	int error;
203 
204 	sktc_generic_kpipe_fini();
205 
206 	error = sysctlbyname("kern.skywalk.debug", NULL, 0, &old_debug_value,
207 	    sizeof(int));
208 	if (error) {
209 		T_LOG("%s: warning sysctl(\"kern.skywalk.debug\" returned %d: %s",
210 		    __func__, error, strerror(errno));
211 	}
212 }
213 
214 struct skywalk_test skt_debug_verify_u = {
215 	"debug_verify_u", "test confirms that skywalk is storing checksums of slots received on a upipe when in SKF_VERIFY debug mode",
216 	SK_FEATURE_SKYWALK | SK_FEATURE_DEV_OR_DEBUG | SK_FEATURE_NEXUS_USER_PIPE,
217 	skt_debug_verify_main, SKTC_GENERIC_UPIPE_ARGV,
218 	skt_debug_verify_u_init, skt_debug_verify_u_fini,
219 };
220 
221 struct skywalk_test skt_debug_verify_k = {
222 	"debug_verify_k", "test confirms that skywalk is storing checksums of slots received on a kpipe when in SKF_VERIFY debug mode",
223 	SK_FEATURE_SKYWALK | SK_FEATURE_DEV_OR_DEBUG | SK_FEATURE_NEXUS_KERNEL_PIPE |
224 	SK_FEATURE_NEXUS_KERNEL_PIPE_LOOPBACK,
225 	skt_debug_verify_main, SKTC_GENERIC_KPIPE_ARGV,
226 	skt_debug_verify_k_init, skt_debug_verify_k_fini,
227 };
228 
229 
230 /****************************************************************/
231