1 /*
2 * Copyright (c) 2015-2022 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 return __packet_set_comp_gencnt(ph, gencnt);
181 }
182
183 int
os_packet_get_compression_generation_count(const packet_t ph,uint32_t * pgencnt)184 os_packet_get_compression_generation_count(const packet_t ph, uint32_t *pgencnt)
185 {
186 return __packet_get_comp_gencnt(ph, pgencnt);
187 }
188
189 int
os_packet_set_traffic_class(const packet_t ph,packet_traffic_class_t tc)190 os_packet_set_traffic_class(const packet_t ph, packet_traffic_class_t tc)
191 {
192 return __packet_set_traffic_class(ph, tc);
193 }
194
195 packet_traffic_class_t
os_packet_get_traffic_class(const packet_t ph)196 os_packet_get_traffic_class(const packet_t ph)
197 {
198 return __packet_get_traffic_class(ph);
199 }
200
201 int
os_packet_set_inet_checksum(const packet_t ph,const packet_csum_flags_t flags,const uint16_t start,const uint16_t stuff)202 os_packet_set_inet_checksum(const packet_t ph, const packet_csum_flags_t flags,
203 const uint16_t start, const uint16_t stuff)
204 {
205 return __packet_set_inet_checksum(ph, flags, start, stuff, TRUE);
206 }
207
208 void
os_packet_add_inet_csum_flags(const packet_t ph,const packet_csum_flags_t flags)209 os_packet_add_inet_csum_flags(const packet_t ph, const packet_csum_flags_t flags)
210 {
211 __packet_add_inet_csum_flags(ph, flags);
212 }
213
214 packet_csum_flags_t
os_packet_get_inet_checksum(const packet_t ph,uint16_t * start,uint16_t * val)215 os_packet_get_inet_checksum(const packet_t ph, uint16_t *start, uint16_t *val)
216 {
217 return __packet_get_inet_checksum(ph, start, val, FALSE);
218 }
219
220 void
os_packet_get_flow_uuid(const packet_t ph,uuid_t * flow_uuid)221 os_packet_get_flow_uuid(const packet_t ph, uuid_t *flow_uuid)
222 {
223 __packet_get_flow_uuid(ph, *flow_uuid);
224 }
225
226 void
os_packet_set_flow_uuid(const packet_t ph,const uuid_t flow_uuid)227 os_packet_set_flow_uuid(const packet_t ph, const uuid_t flow_uuid)
228 {
229 __packet_set_flow_uuid(ph, flow_uuid);
230 }
231
232 void
os_packet_clear_flow_uuid(const packet_t ph)233 os_packet_clear_flow_uuid(const packet_t ph)
234 {
235 __packet_clear_flow_uuid(ph);
236 }
237
238 void
os_packet_set_group_start(const packet_t ph)239 os_packet_set_group_start(const packet_t ph)
240 {
241 (void) __packet_set_group_start(ph);
242 }
243
244 boolean_t
os_packet_get_group_start(const packet_t ph)245 os_packet_get_group_start(const packet_t ph)
246 {
247 return __packet_get_group_start(ph);
248 }
249
250 void
os_packet_set_group_end(const packet_t ph)251 os_packet_set_group_end(const packet_t ph)
252 {
253 (void) __packet_set_group_end(ph);
254 }
255
256 boolean_t
os_packet_get_group_end(const packet_t ph)257 os_packet_get_group_end(const packet_t ph)
258 {
259 return __packet_get_group_end(ph);
260 }
261
262 int
os_packet_get_expire_time(const packet_t ph,uint64_t * ts)263 os_packet_get_expire_time(const packet_t ph, uint64_t *ts)
264 {
265 return __packet_get_expire_time(ph, ts);
266 }
267
268 int
os_packet_set_expire_time(const packet_t ph,const uint64_t ts)269 os_packet_set_expire_time(const packet_t ph, const uint64_t ts)
270 {
271 return __packet_set_expire_time(ph, ts);
272 }
273
274 int
os_packet_get_expiry_action(const packet_t ph,packet_expiry_action_t * pea)275 os_packet_get_expiry_action(const packet_t ph, packet_expiry_action_t *pea)
276 {
277 return __packet_get_expiry_action(ph, pea);
278 }
279
280 int
os_packet_set_expiry_action(const packet_t ph,const packet_expiry_action_t pea)281 os_packet_set_expiry_action(const packet_t ph, const packet_expiry_action_t pea)
282 {
283 return __packet_set_expiry_action(ph, pea);
284 }
285
286 int
os_packet_get_token(const packet_t ph,void * token,uint16_t * len)287 os_packet_get_token(const packet_t ph, void *token, uint16_t *len)
288 {
289 return __packet_get_token(ph, token, len);
290 }
291
292 int
os_packet_set_token(const packet_t ph,const void * token,const uint16_t len)293 os_packet_set_token(const packet_t ph, const void *token,
294 const uint16_t len)
295 {
296 return __packet_set_token(ph, token, len);
297 }
298
299 int
os_packet_get_packetid(const packet_t ph,packet_id_t * pktid)300 os_packet_get_packetid(const packet_t ph, packet_id_t *pktid)
301 {
302 return __packet_get_packetid(ph, pktid);
303 }
304
305 int
os_packet_set_packetid(const packet_t ph,packet_id_t * pktid)306 os_packet_set_packetid(const packet_t ph, packet_id_t *pktid)
307 {
308 return __packet_set_packetid(ph, pktid);
309 }
310
311 int
os_packet_set_vlan_tag(const packet_t ph,const uint16_t tag,const boolean_t tag_in_pkt)312 os_packet_set_vlan_tag(const packet_t ph, const uint16_t tag,
313 const boolean_t tag_in_pkt)
314 {
315 return __packet_set_vlan_tag(ph, tag, tag_in_pkt);
316 }
317
318 int
os_packet_get_vlan_tag(const packet_t ph,uint16_t * tag,boolean_t * tag_in_pkt)319 os_packet_get_vlan_tag(const packet_t ph, uint16_t *tag, boolean_t *tag_in_pkt)
320 {
321 return __packet_get_vlan_tag(ph, tag, tag_in_pkt);
322 }
323
324 uint16_t
os_packet_get_vlan_id(const uint16_t tag)325 os_packet_get_vlan_id(const uint16_t tag)
326 {
327 return __packet_get_vlan_id(tag);
328 }
329
330 uint8_t
os_packet_get_vlan_priority(const uint16_t tag)331 os_packet_get_vlan_priority(const uint16_t tag)
332 {
333 return __packet_get_vlan_priority(tag);
334 }
335
336 int
os_packet_set_app_metadata(const packet_t ph,const packet_app_metadata_type_t app_type,const uint8_t app_metadata)337 os_packet_set_app_metadata(const packet_t ph,
338 const packet_app_metadata_type_t app_type, const uint8_t app_metadata)
339 {
340 return __packet_set_app_metadata(ph, app_type, app_metadata);
341 }
342
343 boolean_t
os_packet_get_wake_flag(const packet_t ph)344 os_packet_get_wake_flag(const packet_t ph)
345 {
346 return __packet_get_wake_flag(ph);
347 }
348
349 boolean_t
os_packet_get_keep_alive(const packet_t ph)350 os_packet_get_keep_alive(const packet_t ph)
351 {
352 return __packet_get_keep_alive(ph);
353 }
354
355 void
os_packet_set_keep_alive(const packet_t ph,const boolean_t is_keep_alive)356 os_packet_set_keep_alive(const packet_t ph, const boolean_t is_keep_alive)
357 {
358 __packet_set_keep_alive(ph, is_keep_alive);
359 }
360
361 boolean_t
os_packet_get_truncated(const packet_t ph)362 os_packet_get_truncated(const packet_t ph)
363 {
364 return __packet_get_truncated(ph);
365 }
366
367 void
os_packet_set_l4s_flag(const packet_t ph)368 os_packet_set_l4s_flag(const packet_t ph)
369 {
370 __packet_set_l4s_flag(ph);
371 }
372
373 uint32_t
os_packet_get_data_length(const packet_t ph)374 os_packet_get_data_length(const packet_t ph)
375 {
376 return __packet_get_data_length(ph);
377 }
378
379 uint32_t
os_packet_get_buflet_count(const packet_t ph)380 os_packet_get_buflet_count(const packet_t ph)
381 {
382 return __packet_get_buflet_count(ph);
383 }
384
385 buflet_t
os_packet_get_next_buflet(const packet_t ph,const buflet_t bprev)386 os_packet_get_next_buflet(const packet_t ph, const buflet_t bprev)
387 {
388 return __packet_get_next_buflet(ph, bprev);
389 }
390
391 uint32_t
os_packet_get_segment_count(const packet_t ph)392 os_packet_get_segment_count(const packet_t ph)
393 {
394 return __packet_get_segment_count(ph);
395 }
396
397 int
os_packet_finalize(const packet_t ph)398 os_packet_finalize(const packet_t ph)
399 {
400 return __packet_finalize(ph);
401 }
402
403 int
os_packet_add_buflet(const packet_t ph,const buflet_t bprev,const buflet_t bnew)404 os_packet_add_buflet(const packet_t ph, const buflet_t bprev,
405 const buflet_t bnew)
406 {
407 return __packet_add_buflet(ph, bprev, bnew);
408 }
409
410 int
os_packet_increment_use_count(const packet_t ph)411 os_packet_increment_use_count(const packet_t ph)
412 {
413 if (__improbable(QUM_ADDR(ph)->qum_usecnt == 0xFFFF)) {
414 return ERANGE;
415 }
416 QUM_ADDR(ph)->qum_usecnt++;
417 return 0;
418 }
419
420 int
os_packet_decrement_use_count(const packet_t ph,uint16_t * use_cnt)421 os_packet_decrement_use_count(const packet_t ph, uint16_t *use_cnt)
422 {
423 if (__improbable(QUM_ADDR(ph)->qum_usecnt == 0)) {
424 return ERANGE;
425 }
426 *use_cnt = --QUM_ADDR(ph)->qum_usecnt;
427 return 0;
428 }
429
430 uint8_t
os_packet_get_aggregation_type(const packet_t ph)431 os_packet_get_aggregation_type(const packet_t ph)
432 {
433 return __packet_get_aggregation_type(ph);
434 }
435
436 void
os_packet_set_tx_timestamp(const packet_t ph,const uint64_t ts)437 os_packet_set_tx_timestamp(const packet_t ph, const uint64_t ts)
438 {
439 __packet_set_tx_timestamp(ph, ts);
440 }
441
442 uint32_t
os_inet_checksum(const void * data,uint32_t len,uint32_t sum0)443 os_inet_checksum(const void *data, uint32_t len, uint32_t sum0)
444 {
445 return __packet_cksum(data, len, sum0);
446 }
447
448 uint32_t
os_copy_and_inet_checksum(const void * src,void * dst,uint32_t len,uint32_t sum0)449 os_copy_and_inet_checksum(const void *src, void *dst, uint32_t len,
450 uint32_t sum0)
451 {
452 uint32_t sum = __packet_copy_and_sum(src, dst, len, sum0);
453 return __packet_fold_sum_final(sum);
454 }
455
456 uint32_t
os_buflet_get_data_offset(const buflet_t buf)457 os_buflet_get_data_offset(const buflet_t buf)
458 {
459 return __buflet_get_data_offset(buf);
460 }
461
462 uint32_t
os_buflet_get_data_length(const buflet_t buf)463 os_buflet_get_data_length(const buflet_t buf)
464 {
465 return __buflet_get_data_length(buf);
466 }
467
468 int
os_buflet_set_data_offset(const buflet_t buf,const uint32_t doff)469 os_buflet_set_data_offset(const buflet_t buf, const uint32_t doff)
470 {
471 return __buflet_set_data_offset(buf, doff);
472 }
473
474 int
os_buflet_set_data_length(const buflet_t buf,const uint32_t dlen)475 os_buflet_set_data_length(const buflet_t buf, const uint32_t dlen)
476 {
477 return __buflet_set_data_length(buf, dlen);
478 }
479
480 void *
os_buflet_get_object_address(const buflet_t buf)481 os_buflet_get_object_address(const buflet_t buf)
482 {
483 return __buflet_get_object_address(buf);
484 }
485
486 uint32_t
os_buflet_get_object_limit(const buflet_t buf)487 os_buflet_get_object_limit(const buflet_t buf)
488 {
489 return __buflet_get_object_limit(buf);
490 }
491
492 void *
os_buflet_get_data_address(const buflet_t buf)493 os_buflet_get_data_address(const buflet_t buf)
494 {
495 return __buflet_get_data_address(buf);
496 }
497
498 uint32_t
os_buflet_get_data_limit(const buflet_t buf)499 os_buflet_get_data_limit(const buflet_t buf)
500 {
501 return __buflet_get_data_limit(buf);
502 }
503
504 packet_trace_id_t
os_packet_get_trace_id(const packet_t ph)505 os_packet_get_trace_id(const packet_t ph)
506 {
507 return __packet_get_trace_id(ph);
508 }
509
510 void
os_packet_set_trace_id(const packet_t ph,packet_trace_id_t trace_id)511 os_packet_set_trace_id(const packet_t ph, packet_trace_id_t trace_id)
512 {
513 __packet_set_trace_id(ph, trace_id);
514 }
515
516 void
os_packet_trace_event(const packet_t ph,uint32_t event)517 os_packet_trace_event(const packet_t ph, uint32_t event)
518 {
519 return __packet_trace_event(ph, event);
520 }
521
522 int
os_packet_set_protocol_segment_size(const packet_t ph,uint16_t proto_seg_sz)523 os_packet_set_protocol_segment_size(const packet_t ph, uint16_t proto_seg_sz)
524 {
525 return __packet_set_protocol_segment_size(ph, proto_seg_sz);
526 }
527
528 void
os_packet_set_tso_flags(const packet_t ph,packet_tso_flags_t flags)529 os_packet_set_tso_flags(const packet_t ph, packet_tso_flags_t flags)
530 {
531 return __packet_set_tso_flags(ph, flags);
532 }
533