xref: /xnu-10002.1.13/bsd/skywalk/packet/packet_kern.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1 /*
2  * Copyright (c) 2016-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 #include <skywalk/os_skywalk_private.h>
30 #include <netinet/tcp_var.h>
31 
32 static int kern_packet_clone_internal(const kern_packet_t, kern_packet_t *,
33     uint32_t, kern_packet_copy_mode_t);
34 
35 #if (DEBUG || DEVELOPMENT)
36 __attribute__((noreturn))
37 void
pkt_subtype_assert_fail(const kern_packet_t ph,uint64_t type,uint64_t subtype)38 pkt_subtype_assert_fail(const kern_packet_t ph, uint64_t type, uint64_t subtype)
39 {
40 	panic("invalid packet handle 0x%llx (type %llu != %llu || "
41 	    "subtype %llu != %llu)", ph, SK_PTR_TYPE(ph), type,
42 	    SK_PTR_SUBTYPE(ph), subtype);
43 	/* NOTREACHED */
44 	__builtin_unreachable();
45 }
46 
47 __attribute__((noreturn))
48 void
pkt_type_assert_fail(const kern_packet_t ph,uint64_t type)49 pkt_type_assert_fail(const kern_packet_t ph, uint64_t type)
50 {
51 	panic("invalid packet handle 0x%llx (type %llu != %llu)",
52 	    ph, SK_PTR_TYPE(ph), type);
53 	/* NOTREACHED */
54 	__builtin_unreachable();
55 }
56 #endif /* DEBUG || DEVELOPMENT */
57 
58 errno_t
kern_packet_set_headroom(const kern_packet_t ph,const uint8_t headroom)59 kern_packet_set_headroom(const kern_packet_t ph, const uint8_t headroom)
60 {
61 	return __packet_set_headroom(ph, headroom);
62 }
63 
64 uint8_t
kern_packet_get_headroom(const kern_packet_t ph)65 kern_packet_get_headroom(const kern_packet_t ph)
66 {
67 	return __packet_get_headroom(ph);
68 }
69 
70 errno_t
kern_packet_set_link_header_offset(const kern_packet_t ph,const uint8_t off)71 kern_packet_set_link_header_offset(const kern_packet_t ph, const uint8_t off)
72 {
73 	return __packet_set_headroom(ph, off);
74 }
75 
76 uint16_t
kern_packet_get_link_header_offset(const kern_packet_t ph)77 kern_packet_get_link_header_offset(const kern_packet_t ph)
78 {
79 	return __packet_get_headroom(ph);
80 }
81 
82 errno_t
kern_packet_set_link_header_length(const kern_packet_t ph,const uint8_t off)83 kern_packet_set_link_header_length(const kern_packet_t ph, const uint8_t off)
84 {
85 	return __packet_set_link_header_length(ph, off);
86 }
87 
88 uint8_t
kern_packet_get_link_header_length(const kern_packet_t ph)89 kern_packet_get_link_header_length(const kern_packet_t ph)
90 {
91 	return __packet_get_link_header_length(ph);
92 }
93 
94 errno_t
kern_packet_set_link_broadcast(const kern_packet_t ph)95 kern_packet_set_link_broadcast(const kern_packet_t ph)
96 {
97 	return __packet_set_link_broadcast(ph);
98 }
99 
100 boolean_t
kern_packet_get_link_broadcast(const kern_packet_t ph)101 kern_packet_get_link_broadcast(const kern_packet_t ph)
102 {
103 	return __packet_get_link_broadcast(ph);
104 }
105 
106 errno_t
kern_packet_set_link_multicast(const kern_packet_t ph)107 kern_packet_set_link_multicast(const kern_packet_t ph)
108 {
109 	return __packet_set_link_multicast(ph);
110 }
111 
112 errno_t
kern_packet_set_link_ethfcs(const kern_packet_t ph)113 kern_packet_set_link_ethfcs(const kern_packet_t ph)
114 {
115 	return __packet_set_link_ethfcs(ph);
116 }
117 
118 boolean_t
kern_packet_get_link_multicast(const kern_packet_t ph)119 kern_packet_get_link_multicast(const kern_packet_t ph)
120 {
121 	return __packet_get_link_multicast(ph);
122 }
123 
124 boolean_t
kern_packet_get_link_ethfcs(const kern_packet_t ph)125 kern_packet_get_link_ethfcs(const kern_packet_t ph)
126 {
127 	return __packet_get_link_ethfcs(ph);
128 }
129 
130 /* deprecated -- no effect, use set_link_header_length instead  */
131 errno_t
kern_packet_set_network_header_offset(const kern_packet_t ph,const uint16_t off)132 kern_packet_set_network_header_offset(const kern_packet_t ph,
133     const uint16_t off)
134 {
135 #pragma unused(ph, off)
136 	return 0;
137 }
138 
139 /* deprecated -- use get_link_header_length instead  */
140 uint16_t
kern_packet_get_network_header_offset(const kern_packet_t ph)141 kern_packet_get_network_header_offset(const kern_packet_t ph)
142 {
143 	return (uint16_t)__packet_get_headroom(ph) +
144 	       (uint16_t)__packet_get_link_header_length(ph);
145 }
146 
147 /* deprecated */
148 errno_t
kern_packet_set_transport_header_offset(const kern_packet_t ph,const uint16_t off)149 kern_packet_set_transport_header_offset(const kern_packet_t ph,
150     const uint16_t off)
151 {
152 #pragma unused(ph, off)
153 	return 0;
154 }
155 
156 /* deprecated */
157 uint16_t
kern_packet_get_transport_header_offset(const kern_packet_t ph)158 kern_packet_get_transport_header_offset(const kern_packet_t ph)
159 {
160 #pragma unused(ph)
161 	return 0;
162 }
163 
164 boolean_t
kern_packet_get_transport_traffic_background(const kern_packet_t ph)165 kern_packet_get_transport_traffic_background(const kern_packet_t ph)
166 {
167 	return __packet_get_transport_traffic_background(ph);
168 }
169 
170 boolean_t
kern_packet_get_transport_traffic_realtime(const kern_packet_t ph)171 kern_packet_get_transport_traffic_realtime(const kern_packet_t ph)
172 {
173 	return __packet_get_transport_traffic_realtime(ph);
174 }
175 
176 boolean_t
kern_packet_get_transport_retransmit(const kern_packet_t ph)177 kern_packet_get_transport_retransmit(const kern_packet_t ph)
178 {
179 	return __packet_get_transport_retransmit(ph);
180 }
181 
182 boolean_t
kern_packet_get_transport_new_flow(const kern_packet_t ph)183 kern_packet_get_transport_new_flow(const kern_packet_t ph)
184 {
185 	return __packet_get_transport_new_flow(ph);
186 }
187 
188 boolean_t
kern_packet_get_transport_last_packet(const kern_packet_t ph)189 kern_packet_get_transport_last_packet(const kern_packet_t ph)
190 {
191 	return __packet_get_transport_last_packet(ph);
192 }
193 
194 int
kern_packet_set_service_class(const kern_packet_t ph,const kern_packet_svc_class_t sc)195 kern_packet_set_service_class(const kern_packet_t ph,
196     const kern_packet_svc_class_t sc)
197 {
198 	return __packet_set_service_class(ph, sc);
199 }
200 
201 kern_packet_svc_class_t
kern_packet_get_service_class(const kern_packet_t ph)202 kern_packet_get_service_class(const kern_packet_t ph)
203 {
204 	return __packet_get_service_class(ph);
205 }
206 
207 errno_t
kern_packet_set_compression_generation_count(const kern_packet_t ph,uint32_t gencnt)208 kern_packet_set_compression_generation_count(const kern_packet_t ph,
209     uint32_t gencnt)
210 {
211 	return __packet_set_comp_gencnt(ph, gencnt);
212 }
213 
214 errno_t
kern_packet_get_compression_generation_count(const kern_packet_t ph,uint32_t * pgencnt)215 kern_packet_get_compression_generation_count(const kern_packet_t ph, uint32_t *pgencnt)
216 {
217 	return __packet_get_comp_gencnt(ph, pgencnt);
218 }
219 
220 errno_t
kern_packet_get_service_class_index(const kern_packet_svc_class_t svc,uint32_t * index)221 kern_packet_get_service_class_index(const kern_packet_svc_class_t svc,
222     uint32_t *index)
223 {
224 	if (index == NULL || !KPKT_VALID_SVC(svc)) {
225 		return EINVAL;
226 	}
227 
228 	*index = KPKT_SVCIDX(svc);
229 	return 0;
230 }
231 
232 boolean_t
kern_packet_is_high_priority(const kern_packet_t ph)233 kern_packet_is_high_priority(const kern_packet_t ph)
234 {
235 	uint32_t sc;
236 	boolean_t is_hi_priority;
237 
238 	sc = __packet_get_service_class(ph);
239 
240 	switch (sc) {
241 	case PKT_SC_VI:
242 	case PKT_SC_SIG:
243 	case PKT_SC_VO:
244 	case PKT_SC_CTL:
245 		is_hi_priority = (PKT_ADDR(ph)->pkt_comp_gencnt == 0 ||
246 		    PKT_ADDR(ph)->pkt_comp_gencnt == TCP_ACK_COMPRESSION_DUMMY);
247 		break;
248 
249 	case PKT_SC_BK_SYS:
250 	case PKT_SC_BK:
251 	case PKT_SC_BE:
252 	case PKT_SC_RD:
253 	case PKT_SC_OAM:
254 	case PKT_SC_AV:
255 	case PKT_SC_RV:
256 	default:
257 		is_hi_priority = false;
258 	}
259 	return is_hi_priority;
260 }
261 
262 errno_t
kern_packet_set_traffic_class(const kern_packet_t ph,kern_packet_traffic_class_t tc)263 kern_packet_set_traffic_class(const kern_packet_t ph,
264     kern_packet_traffic_class_t tc)
265 {
266 	return __packet_set_traffic_class(ph, tc);
267 }
268 
269 kern_packet_traffic_class_t
kern_packet_get_traffic_class(const kern_packet_t ph)270 kern_packet_get_traffic_class(const kern_packet_t ph)
271 {
272 	return __packet_get_traffic_class(ph);
273 }
274 
275 errno_t
kern_packet_set_inet_checksum(const kern_packet_t ph,const packet_csum_flags_t flags,const uint16_t start,const uint16_t stuff,const boolean_t tx)276 kern_packet_set_inet_checksum(const kern_packet_t ph,
277     const packet_csum_flags_t flags, const uint16_t start,
278     const uint16_t stuff, const boolean_t tx)
279 {
280 	return __packet_set_inet_checksum(ph, flags, start, stuff, tx);
281 }
282 
283 packet_csum_flags_t
kern_packet_get_inet_checksum(const kern_packet_t ph,uint16_t * start,uint16_t * val,const boolean_t tx)284 kern_packet_get_inet_checksum(const kern_packet_t ph, uint16_t *start,
285     uint16_t *val, const boolean_t tx)
286 {
287 	return __packet_get_inet_checksum(ph, start, val, tx);
288 }
289 
290 void
kern_packet_set_flow_uuid(const kern_packet_t ph,const uuid_t flow_uuid)291 kern_packet_set_flow_uuid(const kern_packet_t ph, const uuid_t flow_uuid)
292 {
293 	__packet_set_flow_uuid(ph, flow_uuid);
294 }
295 
296 void
kern_packet_get_flow_uuid(const kern_packet_t ph,uuid_t * flow_uuid)297 kern_packet_get_flow_uuid(const kern_packet_t ph, uuid_t *flow_uuid)
298 {
299 	__packet_get_flow_uuid(ph, *flow_uuid);
300 }
301 
302 void
kern_packet_clear_flow_uuid(const kern_packet_t ph)303 kern_packet_clear_flow_uuid(const kern_packet_t ph)
304 {
305 	__packet_clear_flow_uuid(ph);
306 }
307 
308 void
kern_packet_get_euuid(const kern_packet_t ph,uuid_t euuid)309 kern_packet_get_euuid(const kern_packet_t ph, uuid_t euuid)
310 {
311 	if (__probable(SK_PTR_TYPE(ph) == NEXUS_META_TYPE_PACKET)) {
312 		uuid_copy(euuid, PKT_ADDR(ph)->pkt_policy_euuid);
313 	} else {
314 		uuid_clear(euuid);
315 	}
316 }
317 
318 void
kern_packet_set_policy_id(const kern_packet_t ph,uint32_t policy_id)319 kern_packet_set_policy_id(const kern_packet_t ph, uint32_t policy_id)
320 {
321 	if (__probable(SK_PTR_TYPE(ph) == NEXUS_META_TYPE_PACKET)) {
322 		PKT_ADDR(ph)->pkt_policy_id = policy_id;
323 	}
324 }
325 
326 uint32_t
kern_packet_get_policy_id(const kern_packet_t ph)327 kern_packet_get_policy_id(const kern_packet_t ph)
328 {
329 	if (__probable(SK_PTR_TYPE(ph) == NEXUS_META_TYPE_PACKET)) {
330 		return PKT_ADDR(ph)->pkt_policy_id;
331 	} else {
332 		return 0;
333 	}
334 }
335 
336 uint32_t
kern_packet_get_data_length(const kern_packet_t ph)337 kern_packet_get_data_length(const kern_packet_t ph)
338 {
339 	return __packet_get_data_length(ph);
340 }
341 
342 uint32_t
kern_packet_get_buflet_count(const kern_packet_t ph)343 kern_packet_get_buflet_count(const kern_packet_t ph)
344 {
345 	return __packet_get_buflet_count(ph);
346 }
347 
348 kern_buflet_t
kern_packet_get_next_buflet(const kern_packet_t ph,const kern_buflet_t bprev)349 kern_packet_get_next_buflet(const kern_packet_t ph, const kern_buflet_t bprev)
350 {
351 	return __packet_get_next_buflet(ph, bprev);
352 }
353 
354 errno_t
kern_packet_finalize(const kern_packet_t ph)355 kern_packet_finalize(const kern_packet_t ph)
356 {
357 	return __packet_finalize(ph);
358 }
359 
360 kern_packet_idx_t
kern_packet_get_object_index(const kern_packet_t ph)361 kern_packet_get_object_index(const kern_packet_t ph)
362 {
363 	return __packet_get_object_index(ph);
364 }
365 
366 errno_t
kern_packet_get_timestamp(const kern_packet_t ph,uint64_t * ts,boolean_t * valid)367 kern_packet_get_timestamp(const kern_packet_t ph, uint64_t *ts,
368     boolean_t *valid)
369 {
370 	return __packet_get_timestamp(ph, ts, valid);
371 }
372 
373 errno_t
kern_packet_set_timestamp(const kern_packet_t ph,uint64_t ts,boolean_t valid)374 kern_packet_set_timestamp(const kern_packet_t ph, uint64_t ts, boolean_t valid)
375 {
376 	return __packet_set_timestamp(ph, ts, valid);
377 }
378 
379 struct mbuf *
kern_packet_get_mbuf(const kern_packet_t pkt)380 kern_packet_get_mbuf(const kern_packet_t pkt)
381 {
382 	struct __kern_packet *kpkt = SK_PTR_ADDR_KPKT(pkt);
383 
384 	if ((kpkt->pkt_pflags & PKT_F_MBUF_DATA) != 0) {
385 		return kpkt->pkt_mbuf;
386 	}
387 	return NULL;
388 }
389 
390 errno_t
kern_packet_get_timestamp_requested(const kern_packet_t ph,boolean_t * requested)391 kern_packet_get_timestamp_requested(const kern_packet_t ph,
392     boolean_t *requested)
393 {
394 	return __packet_get_timestamp_requested(ph, requested);
395 }
396 
397 void
kern_packet_tx_completion(const kern_packet_t ph,ifnet_t ifp)398 kern_packet_tx_completion(const kern_packet_t ph, ifnet_t ifp)
399 {
400 	struct __kern_packet *kpkt = SK_PTR_ADDR_KPKT(ph);
401 
402 	PKT_TYPE_ASSERT(ph, NEXUS_META_TYPE_PACKET);
403 	/*
404 	 * handling of transmit completion events.
405 	 */
406 	(void) kern_channel_event_transmit_status_with_packet(ph, ifp);
407 
408 	/*
409 	 * handling of transmit completion timestamp request callbacks.
410 	 */
411 	if ((kpkt->pkt_pflags & PKT_F_TX_COMPL_TS_REQ) != 0) {
412 		__packet_perform_tx_completion_callbacks(ph, ifp);
413 	}
414 }
415 
416 errno_t
kern_packet_get_tx_completion_status(const kern_packet_t ph,kern_return_t * status)417 kern_packet_get_tx_completion_status(const kern_packet_t ph,
418     kern_return_t *status)
419 {
420 	return __packet_get_tx_completion_status(ph, status);
421 }
422 
423 errno_t
kern_packet_set_tx_completion_status(const kern_packet_t ph,kern_return_t status)424 kern_packet_set_tx_completion_status(const kern_packet_t ph,
425     kern_return_t status)
426 {
427 	return __packet_set_tx_completion_status(ph, status);
428 }
429 
430 void
kern_packet_set_group_start(const kern_packet_t ph)431 kern_packet_set_group_start(const kern_packet_t ph)
432 {
433 	(void) __packet_set_group_start(ph);
434 }
435 
436 boolean_t
kern_packet_get_group_start(const kern_packet_t ph)437 kern_packet_get_group_start(const kern_packet_t ph)
438 {
439 	return __packet_get_group_start(ph);
440 }
441 
442 void
kern_packet_set_group_end(const kern_packet_t ph)443 kern_packet_set_group_end(const kern_packet_t ph)
444 {
445 	(void) __packet_set_group_end(ph);
446 }
447 
448 boolean_t
kern_packet_get_group_end(const kern_packet_t ph)449 kern_packet_get_group_end(const kern_packet_t ph)
450 {
451 	return __packet_get_group_end(ph);
452 }
453 
454 errno_t
kern_packet_get_expire_time(const kern_packet_t ph,uint64_t * ts)455 kern_packet_get_expire_time(const kern_packet_t ph, uint64_t *ts)
456 {
457 	return __packet_get_expire_time(ph, ts);
458 }
459 
460 errno_t
kern_packet_set_expire_time(const kern_packet_t ph,const uint64_t ts)461 kern_packet_set_expire_time(const kern_packet_t ph, const uint64_t ts)
462 {
463 	return __packet_set_expire_time(ph, ts);
464 }
465 
466 errno_t
kern_packet_get_expiry_action(const kern_packet_t ph,packet_expiry_action_t * pea)467 kern_packet_get_expiry_action(const kern_packet_t ph, packet_expiry_action_t *pea)
468 {
469 	return __packet_get_expiry_action(ph, pea);
470 }
471 
472 errno_t
kern_packet_set_expiry_action(const kern_packet_t ph,packet_expiry_action_t pea)473 kern_packet_set_expiry_action(const kern_packet_t ph, packet_expiry_action_t pea)
474 {
475 	return __packet_set_expiry_action(ph, pea);
476 }
477 
478 errno_t
kern_packet_get_token(const kern_packet_t ph,void * token,uint16_t * len)479 kern_packet_get_token(const kern_packet_t ph, void *token, uint16_t *len)
480 {
481 	return __packet_get_token(ph, token, len);
482 }
483 
484 errno_t
kern_packet_set_token(const kern_packet_t ph,const void * token,const uint16_t len)485 kern_packet_set_token(const kern_packet_t ph, const void *token,
486     const uint16_t len)
487 {
488 	return __packet_set_token(ph, token, len);
489 }
490 
491 errno_t
kern_packet_get_packetid(const kern_packet_t ph,packet_id_t * pktid)492 kern_packet_get_packetid(const kern_packet_t ph, packet_id_t *pktid)
493 {
494 	return __packet_get_packetid(ph, pktid);
495 }
496 
497 errno_t
kern_packet_set_vlan_tag(const kern_packet_t ph,const uint16_t tag,const boolean_t tag_in_pkt)498 kern_packet_set_vlan_tag(const kern_packet_t ph, const uint16_t tag,
499     const boolean_t tag_in_pkt)
500 {
501 	return __packet_set_vlan_tag(ph, tag, tag_in_pkt);
502 }
503 
504 errno_t
kern_packet_get_vlan_tag(const kern_packet_t ph,uint16_t * tag,boolean_t * tag_in_pkt)505 kern_packet_get_vlan_tag(const kern_packet_t ph, uint16_t *tag,
506     boolean_t *tag_in_pkt)
507 {
508 	return __packet_get_vlan_tag(ph, tag, tag_in_pkt);
509 }
510 
511 uint16_t
kern_packet_get_vlan_id(const uint16_t tag)512 kern_packet_get_vlan_id(const uint16_t tag)
513 {
514 	return __packet_get_vlan_id(tag);
515 }
516 
517 uint8_t
kern_packet_get_vlan_priority(const uint16_t tag)518 kern_packet_get_vlan_priority(const uint16_t tag)
519 {
520 	return __packet_get_vlan_priority(tag);
521 }
522 
523 errno_t
kern_packet_get_app_metadata(const kern_packet_t ph,packet_app_metadata_type_t * app_type,uint8_t * app_metadata)524 kern_packet_get_app_metadata(const kern_packet_t ph,
525     packet_app_metadata_type_t *app_type, uint8_t *app_metadata)
526 {
527 	return __packet_get_app_metadata(ph, app_type, app_metadata);
528 }
529 
530 void
kern_packet_set_wake_flag(const kern_packet_t ph)531 kern_packet_set_wake_flag(const kern_packet_t ph)
532 {
533 	return __packet_set_wake_flag(ph);
534 }
535 
536 boolean_t
kern_packet_get_wake_flag(const kern_packet_t ph)537 kern_packet_get_wake_flag(const kern_packet_t ph)
538 {
539 	return __packet_get_wake_flag(ph);
540 }
541 
542 uint32_t
kern_inet_checksum(const void * data,uint32_t len,uint32_t sum0)543 kern_inet_checksum(const void *data, uint32_t len, uint32_t sum0)
544 {
545 	return __packet_cksum(data, len, sum0);
546 }
547 
548 uint32_t
kern_copy_and_inet_checksum(const void * src,void * dst,uint32_t len,uint32_t sum0)549 kern_copy_and_inet_checksum(const void *src, void *dst, uint32_t len,
550     uint32_t sum0)
551 {
552 	uint32_t sum = __packet_copy_and_sum(src, dst, len, sum0);
553 	return __packet_fold_sum_final(sum);
554 }
555 
556 /*
557  * Source packet must be finalized (not dropped); cloned packet does not
558  * inherit the finalized flag, or the classified flag, so caller is
559  * responsible for finalizing it and classifying it (as needed).
560  */
561 static int
kern_packet_clone_internal(const kern_packet_t ph1,kern_packet_t * ph2,uint32_t skmflag,kern_packet_copy_mode_t mode)562 kern_packet_clone_internal(const kern_packet_t ph1, kern_packet_t *ph2,
563     uint32_t skmflag, kern_packet_copy_mode_t mode)
564 {
565 	struct kern_pbufpool *pool;
566 	struct __kern_packet *p1 = SK_PTR_ADDR_KPKT(ph1);
567 	struct __kern_packet *p2 = NULL;
568 	struct __kern_buflet *p1_buf, *p2_buf;
569 	uint16_t bufs_cnt_alloc;
570 	int m_how;
571 	int err;
572 
573 	/* TODO: Add quantum support */
574 	VERIFY(SK_PTR_TYPE(ph1) == NEXUS_META_TYPE_PACKET);
575 
576 	/* Source needs to be finalized (not dropped) and with 1 buflet */
577 	if ((p1->pkt_qum.qum_qflags & QUM_F_DROPPED) != 0 ||
578 	    p1->pkt_bufs_cnt == 0) {
579 		return EINVAL;
580 	}
581 
582 	/* TODO: Add multi-buflet support */
583 	VERIFY(p1->pkt_bufs_cnt == 1);
584 
585 	switch (mode) {
586 	case KPKT_COPY_HEAVY:
587 		/*
588 		 * Allocate a packet with the same number of buffers as that
589 		 * of the source packet's; this cannot be 0 per check above.
590 		 */
591 		bufs_cnt_alloc = p1->pkt_bufs_cnt;
592 		break;
593 
594 	case KPKT_COPY_LIGHT:
595 		/*
596 		 * Allocate an "empty" packet with no buffers attached; this
597 		 * will work only on pools marked with "on-demand", which is
598 		 * the case today for device drivers needing shared buffers
599 		 * support.
600 		 *
601 		 * TODO: We could make this generic and applicable to regular
602 		 * pools, but it would involve detaching the buffer that comes
603 		 * attached to the constructed packet; this wouldn't be that
604 		 * lightweight in nature, but whatever.  In such a case the
605 		 * number of buffers requested during allocation is the same
606 		 * as the that of the source packet's.  For now, let it fail
607 		 * naturally on regular pools, as part of allocation below.
608 		 *
609 		 * XXX: This would also fail on quantums as we currently
610 		 * restrict quantums to have exactly one buffer.
611 		 */
612 		bufs_cnt_alloc = 0;
613 		break;
614 
615 	default:
616 		VERIFY(0);
617 		/* NOTREACHED */
618 		__builtin_unreachable();
619 	}
620 
621 	*ph2 = 0;
622 	pool = __DECONST(struct kern_pbufpool *, SK_PTR_ADDR_KQUM(ph1)->qum_pp);
623 	if (skmflag & SKMEM_NOSLEEP) {
624 		err = kern_pbufpool_alloc_nosleep(pool, bufs_cnt_alloc, ph2);
625 		m_how = M_NOWAIT;
626 	} else {
627 		err = kern_pbufpool_alloc(pool, bufs_cnt_alloc, ph2);
628 		ASSERT(err != ENOMEM);
629 		m_how = M_WAIT;
630 	}
631 	if (__improbable(err != 0)) {
632 		/* See comments above related to KPKT_COPY_{HEAVY,LIGHT} */
633 		goto error;
634 	}
635 	p2 = SK_PTR_ADDR_KPKT(*ph2);
636 
637 	/* Copy packet metadata */
638 	_QUM_COPY(&(p1)->pkt_qum, &(p2)->pkt_qum);
639 	_PKT_COPY(p1, p2);
640 	ASSERT(p2->pkt_mbuf == NULL);
641 	ASSERT(p2->pkt_bufs_max == p1->pkt_bufs_max);
642 
643 	/* clear trace id */
644 	p2->pkt_trace_id = 0;
645 	/* clear finalized and classified bits from clone */
646 	p2->pkt_qum.qum_qflags &= ~(QUM_F_FINALIZED | QUM_F_FLOW_CLASSIFIED);
647 
648 	switch (mode) {
649 	case KPKT_COPY_HEAVY:
650 		/*
651 		 * Heavy: Copy buffer contents and extra metadata.
652 		 */
653 		ASSERT(p2->pkt_bufs_cnt == p1->pkt_bufs_cnt);
654 		if (__probable(p1->pkt_bufs_cnt != 0)) {
655 			uint8_t *saddr, *daddr;
656 			uint32_t copy_len;
657 			/*
658 			 * TODO -- [email protected]
659 			 * Packets from compat driver could have dlen > dlim
660 			 * for flowswitch flow compatibility, cleanup when we
661 			 * make them consistent.
662 			 */
663 			PKT_GET_FIRST_BUFLET(p1, p1->pkt_bufs_cnt, p1_buf);
664 			PKT_GET_FIRST_BUFLET(p2, p2->pkt_bufs_cnt, p2_buf);
665 			saddr = (void *)p1_buf->buf_addr;
666 			daddr = (void *)p2_buf->buf_addr;
667 			copy_len = MIN(p1_buf->buf_dlen, p1_buf->buf_dlim);
668 			if (copy_len != 0) {
669 				bcopy(saddr, daddr, copy_len);
670 			}
671 			*__DECONST(uint32_t *, &p2_buf->buf_dlim) =
672 			    p1_buf->buf_dlim;
673 			p2_buf->buf_dlen = p1_buf->buf_dlen;
674 			p2_buf->buf_doff = p1_buf->buf_doff;
675 		}
676 
677 		/* Copy AQM metadata */
678 		p2->pkt_flowsrc_type = p1->pkt_flowsrc_type;
679 		p2->pkt_flowsrc_fidx = p1->pkt_flowsrc_fidx;
680 		_CASSERT((offsetof(struct __flow, flow_src_id) % 8) == 0);
681 		_UUID_COPY(p2->pkt_flowsrc_id, p1->pkt_flowsrc_id);
682 		_UUID_COPY(p2->pkt_policy_euuid, p1->pkt_policy_euuid);
683 		p2->pkt_policy_id = p1->pkt_policy_id;
684 
685 		p2->pkt_pflags = p1->pkt_pflags;
686 		if (p1->pkt_pflags & PKT_F_MBUF_DATA) {
687 			ASSERT(p1->pkt_mbuf != NULL);
688 			p2->pkt_mbuf = m_dup(p1->pkt_mbuf, m_how);
689 			if (p2->pkt_mbuf == NULL) {
690 				KPKT_CLEAR_MBUF_DATA(p2);
691 				err = ENOBUFS;
692 				goto error;
693 			}
694 		}
695 		break;
696 
697 	case KPKT_COPY_LIGHT:
698 		/*
699 		 * Lightweight: Duplicate buflet(s) and add refs.
700 		 */
701 		ASSERT(p1->pkt_mbuf == NULL);
702 		ASSERT(p2->pkt_bufs_cnt == 0);
703 		if (__probable(p1->pkt_bufs_cnt != 0)) {
704 			PKT_GET_FIRST_BUFLET(p1, p1->pkt_bufs_cnt, p1_buf);
705 			p2_buf = &p2->pkt_qum_buf;
706 			*__DECONST(uint16_t *, &p2->pkt_bufs_cnt) =
707 			    p1->pkt_bufs_cnt;
708 			_KBUF_COPY(p1_buf, p2_buf);
709 			ASSERT(p2_buf->buf_nbft_addr == 0);
710 			ASSERT(p2_buf->buf_nbft_idx == OBJ_IDX_NONE);
711 		}
712 		ASSERT(p2->pkt_bufs_cnt == p1->pkt_bufs_cnt);
713 		ASSERT(p2->pkt_bufs_max == p1->pkt_bufs_max);
714 		ASSERT(err == 0);
715 		break;
716 	}
717 
718 error:
719 	if (err != 0 && p2 != NULL) {
720 		uint32_t usecnt = 0;
721 
722 		ASSERT(p2->pkt_mbuf == NULL);
723 		if (__probable(mode == KPKT_COPY_LIGHT)) {
724 			/*
725 			 * This is undoing what _KBUF_COPY() did earlier,
726 			 * in case this routine is modified to handle regular
727 			 * pool (not on-demand), which also decrements the
728 			 * shared buffer's usecnt.  For regular pool, calling
729 			 * kern_pubfpool_free() will not yield a call to
730 			 * destroy the metadata.
731 			 */
732 			PKT_GET_FIRST_BUFLET(p2, p2->pkt_bufs_cnt, p2_buf);
733 			KBUF_DTOR(p2_buf, usecnt);
734 		}
735 		kern_pbufpool_free(pool, *ph2);
736 		*ph2 = 0;
737 	}
738 
739 	return err;
740 }
741 
742 errno_t
kern_packet_clone(const kern_packet_t ph1,kern_packet_t * ph2,kern_packet_copy_mode_t mode)743 kern_packet_clone(const kern_packet_t ph1, kern_packet_t *ph2,
744     kern_packet_copy_mode_t mode)
745 {
746 	return kern_packet_clone_internal(ph1, ph2, 0, mode);
747 }
748 
749 errno_t
kern_packet_clone_nosleep(const kern_packet_t ph1,kern_packet_t * ph2,kern_packet_copy_mode_t mode)750 kern_packet_clone_nosleep(const kern_packet_t ph1, kern_packet_t *ph2,
751     kern_packet_copy_mode_t mode)
752 {
753 	return kern_packet_clone_internal(ph1, ph2, SKMEM_NOSLEEP, mode);
754 }
755 
756 errno_t
kern_packet_add_buflet(const kern_packet_t ph,const kern_buflet_t bprev,const kern_buflet_t bnew)757 kern_packet_add_buflet(const kern_packet_t ph, const kern_buflet_t bprev,
758     const kern_buflet_t bnew)
759 {
760 	return __packet_add_buflet(ph, bprev, bnew);
761 }
762 
763 void
kern_packet_append(const kern_packet_t ph1,const kern_packet_t ph2)764 kern_packet_append(const kern_packet_t ph1, const kern_packet_t ph2)
765 {
766 	/*
767 	 * TODO:
768 	 * Add assert for non-zero ph2 here after changing IOSkywalkFamily
769 	 * to use kern_packet_set_next() for clearing the next pointer.
770 	 */
771 	kern_packet_set_next(ph1, ph2);
772 }
773 
774 kern_packet_t
kern_packet_get_next(const kern_packet_t ph)775 kern_packet_get_next(const kern_packet_t ph)
776 {
777 	struct __kern_packet *p, *next;
778 
779 	p = SK_PTR_ADDR_KPKT(ph);
780 	next = p->pkt_nextpkt;
781 	return next == NULL ? 0 : SK_PKT2PH(next);
782 }
783 
784 void
kern_packet_set_next(const kern_packet_t ph1,const kern_packet_t ph2)785 kern_packet_set_next(const kern_packet_t ph1, const kern_packet_t ph2)
786 {
787 	struct __kern_packet *p1, *p2;
788 
789 	ASSERT(ph1 != 0);
790 	p1 = SK_PTR_ADDR_KPKT(ph1);
791 	p2 = (ph2 == 0 ? NULL : SK_PTR_ADDR_KPKT(ph2));
792 	p1->pkt_nextpkt = p2;
793 }
794 
795 void
kern_packet_set_chain_counts(const kern_packet_t ph,uint32_t count,uint32_t bytes)796 kern_packet_set_chain_counts(const kern_packet_t ph, uint32_t count,
797     uint32_t bytes)
798 {
799 	struct __kern_packet *p;
800 
801 	p = SK_PTR_ADDR_KPKT(ph);
802 	p->pkt_chain_count = count;
803 	p->pkt_chain_bytes = bytes;
804 }
805 
806 void
kern_packet_get_chain_counts(const kern_packet_t ph,uint32_t * count,uint32_t * bytes)807 kern_packet_get_chain_counts(const kern_packet_t ph, uint32_t *count,
808     uint32_t *bytes)
809 {
810 	struct __kern_packet *p;
811 
812 	p = SK_PTR_ADDR_KPKT(ph);
813 	*count = p->pkt_chain_count;
814 	*bytes = p->pkt_chain_bytes;
815 }
816 
817 errno_t
kern_buflet_set_data_offset(const kern_buflet_t buf,const uint32_t doff)818 kern_buflet_set_data_offset(const kern_buflet_t buf, const uint32_t doff)
819 {
820 	return __buflet_set_data_offset(buf, doff);
821 }
822 
823 uint32_t
kern_buflet_get_data_offset(const kern_buflet_t buf)824 kern_buflet_get_data_offset(const kern_buflet_t buf)
825 {
826 	return __buflet_get_data_offset(buf);
827 }
828 
829 errno_t
kern_buflet_set_data_length(const kern_buflet_t buf,const uint32_t dlen)830 kern_buflet_set_data_length(const kern_buflet_t buf, const uint32_t dlen)
831 {
832 	return __buflet_set_data_length(buf, dlen);
833 }
834 
835 uint32_t
kern_buflet_get_data_length(const kern_buflet_t buf)836 kern_buflet_get_data_length(const kern_buflet_t buf)
837 {
838 	return __buflet_get_data_length(buf);
839 }
840 
841 void *
kern_buflet_get_object_address(const kern_buflet_t buf)842 kern_buflet_get_object_address(const kern_buflet_t buf)
843 {
844 	return __buflet_get_object_address(buf);
845 }
846 
847 uint32_t
kern_buflet_get_object_limit(const kern_buflet_t buf)848 kern_buflet_get_object_limit(const kern_buflet_t buf)
849 {
850 	return __buflet_get_object_limit(buf);
851 }
852 
853 void *
kern_buflet_get_data_address(const kern_buflet_t buf)854 kern_buflet_get_data_address(const kern_buflet_t buf)
855 {
856 	return __buflet_get_data_address(buf);
857 }
858 
859 errno_t
kern_buflet_set_data_address(const kern_buflet_t buf,const void * daddr)860 kern_buflet_set_data_address(const kern_buflet_t buf, const void *daddr)
861 {
862 	return __buflet_set_data_address(buf, daddr);
863 }
864 
865 errno_t
kern_buflet_set_buffer_offset(const kern_buflet_t buf,const uint32_t off)866 kern_buflet_set_buffer_offset(const kern_buflet_t buf, const uint32_t off)
867 {
868 	return __buflet_set_buffer_offset(buf, off);
869 }
870 
871 kern_segment_t
kern_buflet_get_object_segment(const kern_buflet_t buf,kern_obj_idx_seg_t * idx)872 kern_buflet_get_object_segment(const kern_buflet_t buf,
873     kern_obj_idx_seg_t *idx)
874 {
875 	return __buflet_get_object_segment(buf, idx);
876 }
877 
878 uint32_t
kern_buflet_get_data_limit(const kern_buflet_t buf)879 kern_buflet_get_data_limit(const kern_buflet_t buf)
880 {
881 	return __buflet_get_data_limit(buf);
882 }
883 
884 errno_t
kern_buflet_set_data_limit(const kern_buflet_t buf,const uint32_t dlim)885 kern_buflet_set_data_limit(const kern_buflet_t buf, const uint32_t dlim)
886 {
887 	return __buflet_set_data_limit(buf, dlim);
888 }
889 
890 packet_trace_id_t
kern_packet_get_trace_id(const kern_packet_t ph)891 kern_packet_get_trace_id(const kern_packet_t ph)
892 {
893 	return __packet_get_trace_id(ph);
894 }
895 
896 void
kern_packet_set_trace_id(const kern_packet_t ph,packet_trace_id_t trace_id)897 kern_packet_set_trace_id(const kern_packet_t ph, packet_trace_id_t trace_id)
898 {
899 	return __packet_set_trace_id(ph, trace_id);
900 }
901 
902 void
kern_packet_trace_event(const kern_packet_t ph,uint32_t event)903 kern_packet_trace_event(const kern_packet_t ph, uint32_t event)
904 {
905 	return __packet_trace_event(ph, event);
906 }
907 
908 errno_t
kern_packet_copy_bytes(kern_packet_t pkt,size_t off,size_t len,void * out_data)909 kern_packet_copy_bytes(kern_packet_t pkt, size_t off, size_t len, void* out_data)
910 {
911 	kern_buflet_t buflet = NULL;
912 	size_t count;
913 	uint8_t *addr;
914 	uint32_t buflet_len;
915 
916 	buflet = __packet_get_next_buflet(pkt, buflet);
917 	if (buflet == NULL) {
918 		return EINVAL;
919 	}
920 	buflet_len = __buflet_get_data_length(buflet);
921 	if (len > buflet_len) {
922 		return EINVAL;
923 	}
924 	if (off > buflet_len) {
925 		return EINVAL;
926 	}
927 	addr = __buflet_get_data_address(buflet);
928 	if (addr == NULL) {
929 		return EINVAL;
930 	}
931 	addr += __buflet_get_data_offset(buflet);
932 	addr += off;
933 	count = MIN(len, buflet_len - off);
934 	bcopy((void *) addr, out_data, count);
935 
936 	return 0;
937 }
938 
939 
940 errno_t
kern_packet_get_flowid(const kern_packet_t ph,packet_flowid_t * pflowid)941 kern_packet_get_flowid(const kern_packet_t ph, packet_flowid_t *pflowid)
942 {
943 	return __packet_get_flowid(ph, pflowid);
944 }
945 
946 void
kern_packet_set_trace_tag(const kern_packet_t ph,packet_trace_tag_t tag)947 kern_packet_set_trace_tag(const kern_packet_t ph, packet_trace_tag_t tag)
948 {
949 	__packet_set_trace_tag(ph, tag);
950 }
951 
952 packet_trace_tag_t
kern_packet_get_trace_tag(const kern_packet_t ph)953 kern_packet_get_trace_tag(const kern_packet_t ph)
954 {
955 	return __packet_get_trace_tag(ph);
956 }
957 
958 errno_t
kern_packet_get_tx_nexus_port_id(const kern_packet_t ph,uint32_t * nx_port_id)959 kern_packet_get_tx_nexus_port_id(const kern_packet_t ph, uint32_t *nx_port_id)
960 {
961 	return __packet_get_tx_nx_port_id(ph, nx_port_id);
962 }
963 
964 uint16_t
kern_packet_get_protocol_segment_size(const kern_packet_t ph)965 kern_packet_get_protocol_segment_size(const kern_packet_t ph)
966 {
967 	return __packet_get_protocol_segment_size(ph);
968 }
969 
970 void
kern_packet_set_segment_count(const kern_packet_t ph,uint8_t segcount)971 kern_packet_set_segment_count(const kern_packet_t ph, uint8_t segcount)
972 {
973 	__packet_set_segment_count(ph, segcount);
974 }
975 
976 void *
kern_packet_get_priv(const kern_packet_t ph)977 kern_packet_get_priv(const kern_packet_t ph)
978 {
979 	return __packet_get_priv(ph);
980 }
981 
982 void
kern_packet_set_priv(const kern_packet_t ph,void * priv)983 kern_packet_set_priv(const kern_packet_t ph, void *priv)
984 {
985 	return __packet_set_priv(ph, priv);
986 }
987 
988 void
kern_packet_get_tso_flags(const kern_packet_t ph,packet_tso_flags_t * flags)989 kern_packet_get_tso_flags(const kern_packet_t ph, packet_tso_flags_t *flags)
990 {
991 	return __packet_get_tso_flags(ph, flags);
992 }
993 
994 errno_t
kern_packet_check_for_expiry_and_notify(const kern_packet_t ph,ifnet_t ifp,uint16_t origin,uint16_t status)995 kern_packet_check_for_expiry_and_notify(
996 	const kern_packet_t ph, ifnet_t ifp, uint16_t origin, uint16_t status)
997 {
998 	errno_t err = 0;
999 	uint32_t nx_port_id = 0;
1000 	packet_expiry_action_t exp_action = PACKET_EXPIRY_ACTION_NONE;
1001 	os_channel_event_packet_transmit_expired_t exp_notif = {0};
1002 
1003 	if (__improbable(!ifp)) {
1004 		return EINVAL;
1005 	}
1006 
1007 	err = __packet_get_expire_time(ph, &exp_notif.packet_tx_expiration_deadline);
1008 	if (__probable(err)) {
1009 		if (err == ENOENT) {
1010 			/* Expiration time is not set; can not continue; not an error. */
1011 			return 0;
1012 		}
1013 		return err;
1014 	}
1015 
1016 	err = __packet_get_expiry_action(ph, &exp_action);
1017 	if (__probable(err)) {
1018 		if (err == ENOENT) {
1019 			/* Expiry action is not set; can not continue; not an error. */
1020 			return 0;
1021 		}
1022 		return err;
1023 	}
1024 
1025 	if (exp_action == PACKET_EXPIRY_ACTION_NONE) {
1026 		/* Expiry action is no-op; can not continue; not an error. */
1027 		return 0;
1028 	}
1029 
1030 	exp_notif.packet_tx_expiration_timestamp = mach_absolute_time();
1031 
1032 	/* Check whether the packet has expired */
1033 	if (exp_notif.packet_tx_expiration_timestamp < exp_notif.packet_tx_expiration_deadline) {
1034 		/* The packet hasn't expired yet; can not continue; not an error */
1035 		return 0;
1036 	}
1037 
1038 	/* The packet has expired and notification is requested */
1039 	err = __packet_get_packetid(ph, &exp_notif.packet_id);
1040 	if (__improbable(err)) {
1041 		return err;
1042 	}
1043 
1044 	err = __packet_get_tx_nx_port_id(ph, &nx_port_id);
1045 	if (__improbable(err)) {
1046 		return err;
1047 	}
1048 
1049 	exp_notif.packet_tx_expiration_status = status;
1050 	exp_notif.packet_tx_expiration_origin = origin;
1051 
1052 	/* Send the notification status */
1053 	err = kern_channel_event_transmit_expired(
1054 		ifp, &exp_notif, nx_port_id);
1055 
1056 	return err;
1057 }
1058