1 /*
2 * Copyright (c) 2015-2021 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
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <skywalk/os_skywalk_private.h>
37
38 #ifndef LIBSYSCALL_INTERFACE
39 #error "LIBSYSCALL_INTERFACE not defined"
40 #endif /* !LIBSYSCALL_INTERFACE */
41
42 #if (DEBUG || DEVELOPMENT)
43 __attribute__((noreturn))
44 void
pkt_subtype_assert_fail(const packet_t ph,uint64_t type __unused,uint64_t subtype __unused)45 pkt_subtype_assert_fail(const packet_t ph, uint64_t type __unused,
46 uint64_t subtype __unused)
47 {
48 SK_ABORT_WITH_CAUSE("invalid packet subtype", ph);
49 /* NOTREACHED */
50 __builtin_unreachable();
51 }
52
53 __attribute__((noreturn))
54 void
pkt_type_assert_fail(const packet_t ph,uint64_t type __unused)55 pkt_type_assert_fail(const packet_t ph, uint64_t type __unused)
56 {
57 SK_ABORT_WITH_CAUSE("invalid packet type", ph);
58 /* NOTREACHED */
59 __builtin_unreachable();
60 }
61 #endif /* DEBUG || DEVELOPMENT */
62
63 int
os_packet_set_headroom(const packet_t ph,const uint8_t headroom)64 os_packet_set_headroom(const packet_t ph, const uint8_t headroom)
65 {
66 return __packet_set_headroom(ph, headroom);
67 }
68
69 uint8_t
os_packet_get_headroom(const packet_t ph)70 os_packet_get_headroom(const packet_t ph)
71 {
72 return __packet_get_headroom(ph);
73 }
74
75 int
os_packet_set_link_header_length(const packet_t ph,const uint8_t off)76 os_packet_set_link_header_length(const packet_t ph, const uint8_t off)
77 {
78 return __packet_set_link_header_length(ph, off);
79 }
80
81 uint8_t
os_packet_get_link_header_length(const packet_t ph)82 os_packet_get_link_header_length(const packet_t ph)
83 {
84 return __packet_get_link_header_length(ph);
85 }
86
87 int
os_packet_set_link_broadcast(const packet_t ph)88 os_packet_set_link_broadcast(const packet_t ph)
89 {
90 return __packet_set_link_broadcast(ph);
91 }
92
93 boolean_t
os_packet_get_link_broadcast(const packet_t ph)94 os_packet_get_link_broadcast(const packet_t ph)
95 {
96 return __packet_get_link_broadcast(ph);
97 }
98
99 int
os_packet_set_link_multicast(const packet_t ph)100 os_packet_set_link_multicast(const packet_t ph)
101 {
102 return __packet_set_link_multicast(ph);
103 }
104
105 boolean_t
os_packet_get_link_multicast(const packet_t ph)106 os_packet_get_link_multicast(const packet_t ph)
107 {
108 return __packet_get_link_multicast(ph);
109 }
110
111 int
os_packet_set_link_ethfcs(const packet_t ph)112 os_packet_set_link_ethfcs(const packet_t ph)
113 {
114 return __packet_set_link_ethfcs(ph);
115 }
116
117 boolean_t
os_packet_get_link_ethfcs(const packet_t ph)118 os_packet_get_link_ethfcs(const packet_t ph)
119 {
120 return __packet_get_link_ethfcs(ph);
121 }
122
123 int
os_packet_set_transport_traffic_background(const packet_t ph)124 os_packet_set_transport_traffic_background(const packet_t ph)
125 {
126 return __packet_set_transport_traffic_background(ph);
127 }
128
129 boolean_t
os_packet_get_transport_traffic_background(const packet_t ph)130 os_packet_get_transport_traffic_background(const packet_t ph)
131 {
132 return __packet_get_transport_traffic_background(ph);
133 }
134
135 int
os_packet_set_transport_traffic_realtime(const packet_t ph)136 os_packet_set_transport_traffic_realtime(const packet_t ph)
137 {
138 return __packet_set_transport_traffic_realtime(ph);
139 }
140
141 boolean_t
os_packet_get_transport_traffic_realtime(const packet_t ph)142 os_packet_get_transport_traffic_realtime(const packet_t ph)
143 {
144 return __packet_get_transport_traffic_realtime(ph);
145 }
146
147 int
os_packet_set_transport_retransmit(const packet_t ph)148 os_packet_set_transport_retransmit(const packet_t ph)
149 {
150 return __packet_set_transport_retransmit(ph);
151 }
152
153 boolean_t
os_packet_get_transport_retransmit(const packet_t ph)154 os_packet_get_transport_retransmit(const packet_t ph)
155 {
156 return __packet_get_transport_retransmit(ph);
157 }
158
159 int
os_packet_set_transport_last_packet(const packet_t ph)160 os_packet_set_transport_last_packet(const packet_t ph)
161 {
162 return __packet_set_transport_last_packet(ph);
163 }
164
165 int
os_packet_set_service_class(const packet_t ph,const packet_svc_class_t sc)166 os_packet_set_service_class(const packet_t ph, const packet_svc_class_t sc)
167 {
168 return __packet_set_service_class(ph, sc);
169 }
170
171 packet_svc_class_t
os_packet_get_service_class(const packet_t ph)172 os_packet_get_service_class(const packet_t ph)
173 {
174 return __packet_get_service_class(ph);
175 }
176
177 int
os_packet_set_compression_generation_count(const packet_t ph,const uint32_t gencnt)178 os_packet_set_compression_generation_count(const packet_t ph, const uint32_t gencnt)
179 {
180 __packet_set_comp_gencnt(ph, gencnt);
181
182 return 0;
183 }
184
185 uint32_t
os_packet_get_compression_generation_count(const packet_t ph)186 os_packet_get_compression_generation_count(const packet_t ph)
187 {
188 return __packet_get_comp_gencnt(ph);
189 }
190
191 int
os_packet_set_traffic_class(const packet_t ph,packet_traffic_class_t tc)192 os_packet_set_traffic_class(const packet_t ph, packet_traffic_class_t tc)
193 {
194 return __packet_set_traffic_class(ph, tc);
195 }
196
197 packet_traffic_class_t
os_packet_get_traffic_class(const packet_t ph)198 os_packet_get_traffic_class(const packet_t ph)
199 {
200 return __packet_get_traffic_class(ph);
201 }
202
203 int
os_packet_set_inet_checksum(const packet_t ph,const packet_csum_flags_t flags,const uint16_t start,const uint16_t stuff)204 os_packet_set_inet_checksum(const packet_t ph, const packet_csum_flags_t flags,
205 const uint16_t start, const uint16_t stuff)
206 {
207 return __packet_set_inet_checksum(ph, flags, start, stuff, TRUE);
208 }
209
210 packet_csum_flags_t
os_packet_get_inet_checksum(const packet_t ph,uint16_t * start,uint16_t * val)211 os_packet_get_inet_checksum(const packet_t ph, uint16_t *start, uint16_t *val)
212 {
213 return __packet_get_inet_checksum(ph, start, val, FALSE);
214 }
215
216 void
os_packet_get_flow_uuid(const packet_t ph,uuid_t * flow_uuid)217 os_packet_get_flow_uuid(const packet_t ph, uuid_t *flow_uuid)
218 {
219 __packet_get_flow_uuid(ph, *flow_uuid);
220 }
221
222 void
os_packet_set_flow_uuid(const packet_t ph,const uuid_t flow_uuid)223 os_packet_set_flow_uuid(const packet_t ph, const uuid_t flow_uuid)
224 {
225 __packet_set_flow_uuid(ph, flow_uuid);
226 }
227
228 void
os_packet_clear_flow_uuid(const packet_t ph)229 os_packet_clear_flow_uuid(const packet_t ph)
230 {
231 __packet_clear_flow_uuid(ph);
232 }
233
234 void
os_packet_set_group_start(const packet_t ph)235 os_packet_set_group_start(const packet_t ph)
236 {
237 (void) __packet_set_group_start(ph);
238 }
239
240 boolean_t
os_packet_get_group_start(const packet_t ph)241 os_packet_get_group_start(const packet_t ph)
242 {
243 return __packet_get_group_start(ph);
244 }
245
246 void
os_packet_set_group_end(const packet_t ph)247 os_packet_set_group_end(const packet_t ph)
248 {
249 (void) __packet_set_group_end(ph);
250 }
251
252 boolean_t
os_packet_get_group_end(const packet_t ph)253 os_packet_get_group_end(const packet_t ph)
254 {
255 return __packet_get_group_end(ph);
256 }
257
258 int
os_packet_get_expire_time(const packet_t ph,uint64_t * ts)259 os_packet_get_expire_time(const packet_t ph, uint64_t *ts)
260 {
261 return __packet_get_expire_time(ph, ts);
262 }
263
264 int
os_packet_set_expire_time(const packet_t ph,const uint64_t ts)265 os_packet_set_expire_time(const packet_t ph, const uint64_t ts)
266 {
267 return __packet_set_expire_time(ph, ts);
268 }
269
270 int
os_packet_get_token(const packet_t ph,void * token,uint16_t * len)271 os_packet_get_token(const packet_t ph, void *token, uint16_t *len)
272 {
273 return __packet_get_token(ph, token, len);
274 }
275
276 int
os_packet_set_token(const packet_t ph,const void * token,const uint16_t len)277 os_packet_set_token(const packet_t ph, const void *token,
278 const uint16_t len)
279 {
280 return __packet_set_token(ph, token, len);
281 }
282
283 int
os_packet_get_packetid(const packet_t ph,packet_id_t * pktid)284 os_packet_get_packetid(const packet_t ph, packet_id_t *pktid)
285 {
286 return __packet_get_packetid(ph, pktid);
287 }
288
289 int
os_packet_set_packetid(const packet_t ph,packet_id_t * pktid)290 os_packet_set_packetid(const packet_t ph, packet_id_t *pktid)
291 {
292 return __packet_set_packetid(ph, pktid);
293 }
294
295 int
os_packet_set_vlan_tag(const packet_t ph,const uint16_t tag,const boolean_t tag_in_pkt)296 os_packet_set_vlan_tag(const packet_t ph, const uint16_t tag,
297 const boolean_t tag_in_pkt)
298 {
299 return __packet_set_vlan_tag(ph, tag, tag_in_pkt);
300 }
301
302 int
os_packet_get_vlan_tag(const packet_t ph,uint16_t * tag,boolean_t * tag_in_pkt)303 os_packet_get_vlan_tag(const packet_t ph, uint16_t *tag, boolean_t *tag_in_pkt)
304 {
305 return __packet_get_vlan_tag(ph, tag, tag_in_pkt);
306 }
307
308 uint16_t
os_packet_get_vlan_id(const uint16_t tag)309 os_packet_get_vlan_id(const uint16_t tag)
310 {
311 return __packet_get_vlan_id(tag);
312 }
313
314 uint8_t
os_packet_get_vlan_priority(const uint16_t tag)315 os_packet_get_vlan_priority(const uint16_t tag)
316 {
317 return __packet_get_vlan_priority(tag);
318 }
319
320 boolean_t
os_packet_get_wake_flag(const packet_t ph)321 os_packet_get_wake_flag(const packet_t ph)
322 {
323 return __packet_get_wake_flag(ph);
324 }
325
326 boolean_t
os_packet_get_keep_alive(const packet_t ph)327 os_packet_get_keep_alive(const packet_t ph)
328 {
329 return __packet_get_keep_alive(ph);
330 }
331
332 void
os_packet_set_keep_alive(const packet_t ph,const boolean_t is_keep_alive)333 os_packet_set_keep_alive(const packet_t ph, const boolean_t is_keep_alive)
334 {
335 __packet_set_keep_alive(ph, is_keep_alive);
336 }
337
338 boolean_t
os_packet_get_truncated(const packet_t ph)339 os_packet_get_truncated(const packet_t ph)
340 {
341 return __packet_get_truncated(ph);
342 }
343
344 uint32_t
os_packet_get_data_length(const packet_t ph)345 os_packet_get_data_length(const packet_t ph)
346 {
347 return __packet_get_data_length(ph);
348 }
349
350 uint32_t
os_packet_get_buflet_count(const packet_t ph)351 os_packet_get_buflet_count(const packet_t ph)
352 {
353 return __packet_get_buflet_count(ph);
354 }
355
356 buflet_t
os_packet_get_next_buflet(const packet_t ph,const buflet_t bprev)357 os_packet_get_next_buflet(const packet_t ph, const buflet_t bprev)
358 {
359 return __packet_get_next_buflet(ph, bprev);
360 }
361
362 uint32_t
os_packet_get_segment_count(const packet_t ph)363 os_packet_get_segment_count(const packet_t ph)
364 {
365 return __packet_get_segment_count(ph);
366 }
367
368 int
os_packet_finalize(const packet_t ph)369 os_packet_finalize(const packet_t ph)
370 {
371 return __packet_finalize(ph);
372 }
373
374 int
os_packet_add_buflet(const packet_t ph,const buflet_t bprev,const buflet_t bnew)375 os_packet_add_buflet(const packet_t ph, const buflet_t bprev,
376 const buflet_t bnew)
377 {
378 return __packet_add_buflet(ph, bprev, bnew);
379 }
380
381 int
os_packet_increment_use_count(const packet_t ph)382 os_packet_increment_use_count(const packet_t ph)
383 {
384 if (__improbable(QUM_ADDR(ph)->qum_usecnt == 0xFFFF)) {
385 return ERANGE;
386 }
387 QUM_ADDR(ph)->qum_usecnt++;
388 return 0;
389 }
390
391 int
os_packet_decrement_use_count(const packet_t ph,uint16_t * use_cnt)392 os_packet_decrement_use_count(const packet_t ph, uint16_t *use_cnt)
393 {
394 if (__improbable(QUM_ADDR(ph)->qum_usecnt == 0)) {
395 return ERANGE;
396 }
397 *use_cnt = --QUM_ADDR(ph)->qum_usecnt;
398 return 0;
399 }
400
401 uint8_t
os_packet_get_aggregation_type(const packet_t ph)402 os_packet_get_aggregation_type(const packet_t ph)
403 {
404 return __packet_get_aggregation_type(ph);
405 }
406
407 uint32_t
os_inet_checksum(const void * data,uint32_t len,uint32_t sum0)408 os_inet_checksum(const void *data, uint32_t len, uint32_t sum0)
409 {
410 return __packet_cksum(data, len, sum0);
411 }
412
413 uint32_t
os_copy_and_inet_checksum(const void * src,void * dst,uint32_t len,uint32_t sum0)414 os_copy_and_inet_checksum(const void *src, void *dst, uint32_t len,
415 uint32_t sum0)
416 {
417 uint32_t sum = __packet_copy_and_sum(src, dst, len, sum0);
418 return __packet_fold_sum_final(sum);
419 }
420
421 uint16_t
os_buflet_get_data_offset(const buflet_t buf)422 os_buflet_get_data_offset(const buflet_t buf)
423 {
424 return __buflet_get_data_offset(buf);
425 }
426
427 uint16_t
os_buflet_get_data_length(const buflet_t buf)428 os_buflet_get_data_length(const buflet_t buf)
429 {
430 return __buflet_get_data_length(buf);
431 }
432
433 int
os_buflet_set_data_offset(const buflet_t buf,const uint16_t doff)434 os_buflet_set_data_offset(const buflet_t buf, const uint16_t doff)
435 {
436 return __buflet_set_data_offset(buf, doff);
437 }
438
439 int
os_buflet_set_data_length(const buflet_t buf,const uint16_t dlen)440 os_buflet_set_data_length(const buflet_t buf, const uint16_t dlen)
441 {
442 return __buflet_set_data_length(buf, dlen);
443 }
444
445 void *
os_buflet_get_object_address(const buflet_t buf)446 os_buflet_get_object_address(const buflet_t buf)
447 {
448 return __buflet_get_object_address(buf);
449 }
450
451 uint32_t
os_buflet_get_object_limit(const buflet_t buf)452 os_buflet_get_object_limit(const buflet_t buf)
453 {
454 return __buflet_get_object_limit(buf);
455 }
456
457 void *
os_buflet_get_data_address(const buflet_t buf)458 os_buflet_get_data_address(const buflet_t buf)
459 {
460 return __buflet_get_data_address(buf);
461 }
462
463 uint16_t
os_buflet_get_data_limit(const buflet_t buf)464 os_buflet_get_data_limit(const buflet_t buf)
465 {
466 return __buflet_get_data_limit(buf);
467 }
468
469 packet_trace_id_t
os_packet_get_trace_id(const packet_t ph)470 os_packet_get_trace_id(const packet_t ph)
471 {
472 return __packet_get_trace_id(ph);
473 }
474
475 void
os_packet_set_trace_id(const packet_t ph,packet_trace_id_t trace_id)476 os_packet_set_trace_id(const packet_t ph, packet_trace_id_t trace_id)
477 {
478 __packet_set_trace_id(ph, trace_id);
479 }
480
481 void
os_packet_trace_event(const packet_t ph,uint32_t event)482 os_packet_trace_event(const packet_t ph, uint32_t event)
483 {
484 return __packet_trace_event(ph, event);
485 }
486