xref: /xnu-8020.121.3/bsd/skywalk/packet/os_packet.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * Copyright (c) 2016-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 #ifndef _SKYWALK_OS_PACKET_H_
30 #define _SKYWALK_OS_PACKET_H_
31 
32 #ifdef PRIVATE
33 
34 #include <stdint.h>
35 #include <sys/types.h>
36 #include <sys/cdefs.h>
37 #include <uuid/uuid.h>
38 #include <mach/boolean.h>
39 #include <mach/vm_types.h>
40 #include <skywalk/os_nexus.h>
41 
42 /*
43  * @enum packet_svc_class_t
44  * @abstract Service class of a packet
45  * @discussion Property that represents the category of service
46  *      of a packet. This information may be used by the driver
47  *      and at the link level.
48  * @constant PKT_SC_BK_SYS "Background System-Initiated", high delay
49  *      tolerant, high loss tolerant, elastic flow, variable size &
50  *      long-lived.
51  * @constant PKT_SC_BK "Background", user-initiated, high delay tolerant,
52  *      high loss tolerant, elastic flow, variable size.  This level
53  *      corresponds to WMM access class "BG".
54  * @constant PKT_SC_BE "Best Effort", unclassified/standard.  This is
55  *      the default service class; pretty much a mix of everything.
56  *      This level corresponds to WMM access class "BE".
57  * @constant PKT_SC_RD
58  *      "Responsive Data", a notch higher than "Best Effort", medium
59  *      delay tolerant, medium loss tolerant, elastic flow, bursty,
60  *      long-lived.
61  * @constant PKT_SC_OAM "Operations, Administration, and Management",
62  *      medium delay tolerant, low-medium loss tolerant, elastic &
63  *      inelastic flows, variable size.
64  * @constant PKT_SC_AV "Multimedia Audio/Video Streaming", medium delay
65  *      tolerant, low-medium loss tolerant, elastic flow, constant
66  *      packet interval, variable rate & size.
67  * @constant PKT_SC_RV "Responsive Multimedia Audio/Video", low delay
68  *      tolerant, low-medium loss tolerant, elastic flow, variable
69  *      packet interval, rate and size.
70  * @constant PKT_SC_VI "Interactive Video", low delay tolerant, low-
71  *      medium loss tolerant, elastic flow, constant packet interval,
72  *      variable rate & size.  This level corresponds to WMM access
73  *      class "VI".
74  * @constant PKT_SC_SIG "Signaling", low delay tolerant, low loss
75  *      tolerant, inelastic flow, jitter tolerant, rate is bursty but
76  *      short, variable size. e.g. SIP.  This level corresponds to WMM
77  *      access class "VI".
78  * @constant PKT_SC_VO "Interactive Voice", low delay tolerant, low loss
79  *      tolerant, inelastic flow, constant packet rate, somewhat fixed
80  *      size.  This level corresponds to WMM access class "VO" or
81  *      PKT_TC_VO.
82  * @constant PKT_SC_CTL "Network Control", low delay tolerant, low loss
83  *      tolerant, inelastic flow, rate is short & burst, variable size.
84  */
85 typedef enum {
86 	PKT_SC_BK_SYS   = 0x00080090, /* lowest class */
87 	PKT_SC_BK       = 0x00100080,
88 
89 	PKT_SC_BE       = 0x00000000,
90 	PKT_SC_RD       = 0x00180010,
91 	PKT_SC_OAM      = 0x00200020,
92 
93 	PKT_SC_AV       = 0x00280120,
94 	PKT_SC_RV       = 0x00300110,
95 	PKT_SC_VI       = 0x00380100,
96 	PKT_SC_SIG      = 0x00380130,
97 
98 	PKT_SC_VO       = 0x00400180,
99 	PKT_SC_CTL      = 0x00480190, /* highest class */
100 } packet_svc_class_t;
101 
102 /* CSTYLED */
103 /*!
104  * @enum packet_traffic_class_t
105  * @abstract Traffic class of a packet
106  * @discussion Property that represent the category of traffic of a packet.
107  *	This information may be used by the driver and at the link level.
108  * @constant PKT_TC_BE Best effort, normal class.
109  * @constant PKT_TC_BK Background, low priority or bulk traffic.
110  * @constant PKT_TC_VI Interactive video, constant bit rate, low latency.
111  * @constant PKT_TC_VO Interactive voice, constant bit rate, lowest latency.
112  */
113 typedef enum {
114 	PKT_TC_BE       = 0,
115 	PKT_TC_BK       = 1,
116 	PKT_TC_VI       = 2,
117 	PKT_TC_VO       = 3,
118 } packet_traffic_class_t;
119 
120 /*
121  * These conversion macros rely on the corresponding PKT_SC and
122  * PKT_TC values in order to establish the following mapping:
123  *
124  *	PKT_SC_BK_SYS	] ==>	PKT_TC_BK
125  *	PKT_SC_BK	]
126  *
127  *	PKT_SC_BE	] ==>	PKT_TC_BE
128  *	PKT_SC_RD	]
129  *	PKT_SC_OAM	]
130  *
131  *	PKT_SC_AV	] ==>	PKT_TC_VI
132  *	PKT_SC_RV	]
133  *	PKT_SC_VI	]
134  *	PKT_SC_SIG	]
135  *
136  *	PKT_SC_VO	] ==>	PKT_TC_VO
137  *	PKT_SC_CTL	]
138  *
139  * The values assigned to each service class allows for a fast mapping to
140  * the corresponding PKT_TC traffic class values, as well as to retrieve the
141  * assigned index; therefore care must be taken when comparing against these
142  * values.  Use the corresponding class and index macros to retrieve the
143  * corresponding portion, and never assume that a higher class corresponds
144  * to a higher index.
145  */
146 #define PKT_SCVAL(x)            ((x) & 0xffff)
147 #define PKT_SC2TC(_sc)          (PKT_SCVAL(_sc) >> 7)
148 #define PKT_TC2SCVAL(_tc)       ((_tc) << 7)
149 
150 #define PKT_SCVAL_BK_SYS        PKT_SCVAL(PKT_SC_BK_SYS)
151 #define PKT_SCVAL_BK            PKT_SCVAL(PKT_SC_BK)
152 #define PKT_SCVAL_BE            PKT_SCVAL(PKT_SC_BE)
153 #define PKT_SCVAL_RD            PKT_SCVAL(PKT_SC_RD)
154 #define PKT_SCVAL_OAM           PKT_SCVAL(PKT_SC_OAM)
155 #define PKT_SCVAL_AV            PKT_SCVAL(PKT_SC_AV)
156 #define PKT_SCVAL_RV            PKT_SCVAL(PKT_SC_RV)
157 #define PKT_SCVAL_VI            PKT_SCVAL(PKT_SC_VI)
158 #define PKT_SCVAL_SIG           PKT_SCVAL(PKT_SC_SIG)
159 #define PKT_SCVAL_VO            PKT_SCVAL(PKT_SC_VO)
160 #define PKT_SCVAL_CTL           PKT_SCVAL(PKT_SC_CTL)
161 
162 /*
163  * Packet checksum offload flags.
164  */
165 typedef uint32_t packet_csum_flags_t;
166 typedef uint32_t packet_trace_id_t;
167 
168 /*
169  * PACKET_CSUM_PARTIAL indicates the following:
170  *
171  * On transmit, the start and stuff offsets are significant, and that the
172  * module setting this information is requesting that the layer below it
173  * compute 16-bit 1's complement sum from the location marked by the start
174  * offset to the end of the packet, and store the resulted sum at the
175  * location marked by the stuff offset.  If PACKET_CSUM_ZERO_INVERT is set,
176  * and if the resulted sum is 0, it will be converted to -0 (0xffff).
177  *
178  * On receive, the start offset and checksum value are significant, and
179  * that the module computing the 16-bit 1's complement and setting this
180  * information is requesting that the layer above it perform any necessary
181  * adjustments to exclude/include data span that's not applicable, as well
182  * as to validate the checksum value.
183  */
184 #define PACKET_CSUM_PARTIAL     0x01     /* partial one's complement */
185 #define PACKET_CSUM_ZERO_INVERT 0x02     /* invert resulted 0 to 0xffff */
186 
187 /*
188  * Below flags are for RX.
189  */
190 #define PACKET_CSUM_IP_CHECKED  0x0100    /* did IP checksum */
191 #define PACKET_CSUM_IP_VALID    0x0200    /* and the IP checksum is valid */
192 #define PACKET_CSUM_DATA_VALID  0x0400    /* csum_rx_val is valid */
193 #define PACKET_CSUM_PSEUDO_HDR  0x0800    /* csum_rx_val includes pseudo hdr */
194 
195 #define PACKET_HAS_VALID_IP_CSUM(_p) \
196     (((_p)->pkt_csum_flags & (PACKET_CSUM_IP_CHECKED | PACKET_CSUM_IP_VALID)) \
197      == (PACKET_CSUM_IP_CHECKED | PACKET_CSUM_IP_VALID))
198 
199 #define PACKET_HAS_PARTIAL_CHECKSUM(_p) \
200 	((_p)->pkt_csum_flags & (PACKET_CSUM_PARTIAL))
201 
202 #define PACKET_CSUM_RX_FULL_FLAGS \
203 	(PACKET_CSUM_IP_CHECKED | PACKET_CSUM_IP_VALID | \
204 	PACKET_CSUM_DATA_VALID | PACKET_CSUM_PSEUDO_HDR)
205 
206 #define PACKET_CSUM_RX_FLAGS \
207 	(PACKET_CSUM_RX_FULL_FLAGS | PACKET_CSUM_PARTIAL)
208 
209 /*
210  * TODO: [email protected] -- these are temporary and should be removed later.
211  */
212 #define OS_PACKET_HAS_CHECKSUM_API      1
213 #define OS_PACKET_HAS_SEGMENT_COUNT     1
214 #define OS_PACKET_HAS_TRACING_API       1
215 
216 /*
217  * Valid values for pkt_aggr_type.
218  */
219 #define PKT_AGGR_NONE                0x00 /* no aggregation */
220 #define PKT_AGGR_IP_CHAIN            0x01 /* buflet chain of discrete IP packets containing contiguous L4 frames */
221 #define PKT_AGGR_SINGLE_IP           0x02 /* buflet chain representing single IP packet */
222 #define PKT_AGGR_SINGLE_IP_PACKED    0x03 /* buflet chain representing single IP packet in packed format */
223 
224 /*
225  * packet_id is a per packet metadata which can be set on a packet by the
226  * application. It can be used to identify either an individual or a group
227  * of packets within the networking stack and driver.
228  */
229 typedef struct packet_id {
230 	/*
231 	 * version of this structure.
232 	 */
233 	uint8_t     pktid_version;
234 #define OS_PACKET_PKTID_VERSION_1          1
235 #define OS_PACKET_PKTID_VERSION_CURRENT    OS_PACKET_PKTID_VERSION_1
236 	/*
237 	 * payload type of the packet, opaque to the network stack.
238 	 */
239 	uint8_t     pktid_payload_type;
240 	/*
241 	 * packet sequence number, monotonically increasing.
242 	 */
243 	uint16_t    pktid_sequence_number;
244 	/*
245 	 * packet timestamp, monotonically increasing and opaque to the
246 	 * network stack. Sample rate of the timestamp clock is determined
247 	 * by the application.
248 	 */
249 	uint32_t    pktid_timestamp;
250 	/*
251 	 * Identifier for streams defined by the application.
252 	 */
253 	uint32_t    pktid_stream_identifier;
254 	/*
255 	 * reserved for future use.
256 	 */
257 	uint32_t    _reserved;
258 } packet_id_t;
259 
260 /*
261  * Packet Trace code
262  * Used with os_packet_trace_* functions.
263  * Total of 12bit (0xABC) code space available, current sub-code allocation is:
264  *     0x00C code space for FSW Rx path.
265  *     0x01C code space for FSW Tx path.
266  *
267  * More sub-code can be added for other packet data path, e.g. uPipe, BSD, etc.
268  *
269  * Note:
270  *     1. Needs to include <sys/kdebug.h> to use the values.
271  *     2. When making changes to sub-code/value, update static assertions in
272  *        pp_init and probe list ariadne-plists/skywalk-tracepoints.plist.
273  *
274  */
275 /* Rx Group */
276 #define PKT_TRACE_RX_DRV_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x001) | DBG_FUNC_START)
277 #define PKT_TRACE_RX_DRV_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x001) | DBG_FUNC_END)
278 
279 #define PKT_TRACE_RX_FSW_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x002) | DBG_FUNC_START)
280 #define PKT_TRACE_RX_FSW_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x002) | DBG_FUNC_END)
281 
282 #define PKT_TRACE_RX_CHN_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x003) | DBG_FUNC_START)
283 #define PKT_TRACE_RX_CHN_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x003) | DBG_FUNC_END)
284 
285 
286 /* Tx Group */
287 #define PKT_TRACE_TX_FSW_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x010) | DBG_FUNC_START)
288 #define PKT_TRACE_TX_FSW_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x010) | DBG_FUNC_END)
289 
290 #define PKT_TRACE_TX_AQM_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x011) | DBG_FUNC_START)
291 #define PKT_TRACE_TX_AQM_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x011) | DBG_FUNC_END)
292 
293 #define PKT_TRACE_TX_DRV_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x012) | DBG_FUNC_START)
294 #define PKT_TRACE_TX_DRV_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x012) | DBG_FUNC_END)
295 
296 
297 #ifndef KERNEL
298 /*
299  * User APIs.
300  */
301 
302 /*
303  * Opaque handles.
304  */
305 struct __user_buflet;
306 typedef uint64_t                        packet_t;
307 typedef struct __user_buflet            *buflet_t;
308 
309 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
310 /*
311  * Packets specific.
312  */
313 extern int os_packet_set_headroom(const packet_t, const uint8_t);
314 extern uint8_t os_packet_get_headroom(const packet_t);
315 extern int os_packet_set_link_header_length(const packet_t, const uint8_t);
316 extern uint8_t os_packet_get_link_header_length(const packet_t);
317 extern int os_packet_set_link_broadcast(const packet_t);
318 extern boolean_t os_packet_get_link_broadcast(const packet_t);
319 extern int os_packet_set_link_multicast(const packet_t);
320 extern boolean_t os_packet_get_link_multicast(const packet_t);
321 extern int os_packet_set_link_ethfcs(const packet_t);
322 extern boolean_t os_packet_get_link_ethfcs(const packet_t);
323 extern int os_packet_set_transport_traffic_background(const packet_t);
324 extern boolean_t os_packet_get_transport_traffic_background(const packet_t);
325 extern int os_packet_set_transport_traffic_realtime(const packet_t);
326 extern boolean_t os_packet_get_transport_traffic_realtime(const packet_t);
327 extern int os_packet_set_transport_retransmit(const packet_t);
328 extern boolean_t os_packet_get_transport_retransmit(const packet_t);
329 extern int os_packet_set_transport_last_packet(const packet_t);
330 extern int os_packet_set_service_class(const packet_t,
331     const packet_svc_class_t);
332 extern packet_svc_class_t os_packet_get_service_class(const packet_t);
333 extern int os_packet_set_compression_generation_count(const packet_t, const uint32_t);
334 extern uint32_t os_packet_get_compression_generation_count(const packet_t);
335 extern int os_packet_set_traffic_class(const packet_t, packet_traffic_class_t);
336 extern packet_traffic_class_t os_packet_get_traffic_class(const packet_t);
337 extern int os_packet_set_inet_checksum(const packet_t,
338     const packet_csum_flags_t, const uint16_t, const uint16_t);
339 extern packet_csum_flags_t os_packet_get_inet_checksum(const packet_t,
340     uint16_t *, uint16_t *);
341 extern void os_packet_set_group_start(const packet_t);
342 extern boolean_t os_packet_get_group_start(const packet_t);
343 extern void os_packet_set_group_end(const packet_t);
344 extern boolean_t os_packet_get_group_end(const packet_t);
345 extern int os_packet_set_expire_time(const packet_t, const uint64_t);
346 extern int os_packet_get_expire_time(const packet_t, uint64_t *);
347 extern int os_packet_set_token(const packet_t, const void *, const uint16_t);
348 extern int os_packet_get_packetid(const packet_t, packet_id_t *);
349 extern int os_packet_set_packetid(const packet_t, packet_id_t *);
350 extern int os_packet_set_vlan_tag(const packet_t, const uint16_t,
351     const boolean_t);
352 extern int os_packet_get_vlan_tag(const packet_t, uint16_t *, boolean_t *);
353 extern uint16_t os_packet_get_vlan_id(const uint16_t);
354 extern uint8_t os_packet_get_vlan_priority(const uint16_t);
355 #define HAS_OS_PACKET_GET_WAKE_FLAG 1
356 extern boolean_t os_packet_get_wake_flag(const packet_t);
357 #define HAS_OS_PACKET_KEEP_ALIVE 1
358 extern boolean_t os_packet_get_keep_alive(const packet_t);
359 extern void os_packet_set_keep_alive(const packet_t, const boolean_t);
360 extern boolean_t os_packet_get_truncated(const packet_t);
361 extern uint8_t os_packet_get_aggregation_type(const packet_t ph);
362 
363 /*
364  * Quantum & Packets.
365  */
366 extern void os_packet_set_flow_uuid(const packet_t, const uuid_t flow_uuid);
367 extern void os_packet_get_flow_uuid(const packet_t, uuid_t *flow_uuid);
368 extern void os_packet_clear_flow_uuid(const packet_t);
369 extern uint32_t os_packet_get_data_length(const packet_t);
370 extern uint32_t os_packet_get_buflet_count(const packet_t);
371 extern buflet_t os_packet_get_next_buflet(const packet_t, const buflet_t);
372 extern uint32_t os_packet_get_segment_count(const packet_t ph);
373 extern int os_packet_finalize(const packet_t);
374 extern int os_packet_add_buflet(const packet_t ph, const buflet_t bprev,
375     const buflet_t bnew);
376 /* increment use count on packet */
377 extern int os_packet_increment_use_count(const packet_t ph);
378 /* decrement use count on packet and retrieve new value  */
379 extern int os_packet_decrement_use_count(const packet_t ph, uint16_t *use_cnt);
380 
381 extern packet_trace_id_t os_packet_get_trace_id(const packet_t ph);
382 extern void os_packet_set_trace_id(const packet_t ph, packet_trace_id_t);
383 extern void os_packet_trace_event(const packet_t ph, uint32_t);
384 
385 /*
386  * Misc.
387  */
388 extern uint32_t os_inet_checksum(const void *, uint32_t, uint32_t);
389 extern uint32_t os_copy_and_inet_checksum(const void *, void *,
390     uint32_t, uint32_t);
391 
392 /*
393  * Buflets.
394  */
395 extern int os_buflet_set_data_offset(const buflet_t, const uint16_t);
396 extern uint16_t os_buflet_get_data_offset(const buflet_t);
397 extern int os_buflet_set_data_length(const buflet_t, const uint16_t);
398 extern uint16_t os_buflet_get_data_length(const buflet_t);
399 extern void *os_buflet_get_object_address(const buflet_t);
400 extern uint32_t os_buflet_get_object_limit(const buflet_t);
401 extern void *os_buflet_get_data_address(const buflet_t);
402 extern uint16_t os_buflet_get_data_limit(const buflet_t);
403 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
404 #else /* KERNEL */
405 /*
406  * Kernel APIs.
407  */
408 
409 /*
410  * Opaque handles.
411  */
412 struct __kern_buflet;
413 struct kern_pbufpool;
414 struct sksegment;
415 
416 typedef struct kern_pbufpool            *kern_pbufpool_t;
417 typedef uint64_t                        kern_packet_t;
418 typedef uint32_t                        kern_packet_idx_t;
419 typedef struct __kern_buflet            *kern_buflet_t;
420 typedef uint32_t                        kern_obj_idx_seg_t;
421 typedef struct sksegment                *kern_segment_t;
422 typedef uint32_t                        kern_segment_idx_t;
423 
424 /*
425  * @typedef pbuf_seg_ctor_fn_t
426  * @abstract Buffer segment constructor callback.
427  * @param buf_seg Buffer segment handle.
428  * @param buf_desc IOSKMemoryDescriptor handle for buffer segment.
429  * @discussion This callback is invoked when a segment has been activated.
430  *      If applicable, the owner should wire/prepare the memory and insert
431  *      it into the memory mapper (IOMMU/DART) prior to returning.
432  */
433 typedef void (*pbuf_seg_ctor_fn_t)(const kern_pbufpool_t,
434     const kern_segment_t buf_seg, const IOSKMemoryDescriptor buf_desc);
435 
436 /*
437  * @typedef pbuf_seg_dtor_fn_t
438  * @abstract Buffer segment destructor callback.
439  * @param buf_seg Buffer segment handle.
440  * @param buf_desc IOSKMemoryDescriptor handle for buffer segment.
441  * @discussion This callback is invoked when a segment is about to be
442  *      freed.  The owner should reverse what had been done earlier
443  *      at pbuf_seg_ctor_fn_t() constructor time.  If applicable,
444  *      it should remove the memory from mapper (IOMMU/DART), and
445  *      unwire/complete it prior to returning.
446  */
447 typedef void (*pbuf_seg_dtor_fn_t)(const kern_pbufpool_t,
448     const kern_segment_t buf_seg, const IOSKMemoryDescriptor buf_desc);
449 
450 typedef void (*pbuf_ctx_retain_fn_t)(void *const ctx);
451 typedef void (*pbuf_ctx_release_fn_t)(void *const ctx);
452 
453 typedef uint8_t pbufpool_name_t[64];
454 
455 /*
456  * Kernel packet buffer pool init.
457  */
458 struct kern_pbufpool_init {
459 	uint32_t                kbi_version;    /* current version */
460 	pbufpool_name_t         kbi_name;       /* optional */
461 	uint32_t                kbi_flags;      /* see KBI_* */
462 	uint32_t                kbi_packets;    /* required */
463 	uint32_t                kbi_max_frags;  /* max buflets per packet */
464 	uint32_t                kbi_buflets;    /* >= packets (optional) */
465 	uint32_t                kbi_bufsize;    /* required */
466 	uint32_t                kbi_buf_seg_size; /* optional */
467 	pbuf_seg_ctor_fn_t      kbi_buf_seg_ctor; /* optional */
468 	pbuf_seg_dtor_fn_t      kbi_buf_seg_dtor; /* optional */
469 	void *                  kbi_ctx;         /* optional */
470 	pbuf_ctx_retain_fn_t    kbi_ctx_retain;  /* optional */
471 	pbuf_ctx_release_fn_t   kbi_ctx_release; /* optional */
472 };
473 
474 #define KBIF_QUANTUM            0x1     /* simple packet (non-networking) */
475 #define KBIF_PERSISTENT         0x2     /* persistent memory (wired) */
476 #define KBIF_MONOLITHIC         0x4     /* single segment mode */
477 #define KBIF_BUFFER_ON_DEMAND   0x8     /* attach/detach buffers on demand */
478 #define KBIF_INHIBIT_CACHE      0x10    /* caching-inhibited */
479 #define KBIF_USER_ACCESS        0x20    /* allow userspace access */
480 #define KBIF_VIRTUAL_DEVICE     0x40    /* device is virtual (no DMA) */
481 #define KBIF_PHYS_CONTIGUOUS    0x80    /* physically-contiguous segment(s) */
482 #define KBIF_IODIR_IN           0x100   /* io direction in (device to host) */
483 #define KBIF_IODIR_OUT          0x200   /* io direction out (host to device) */
484 #define KBIF_KERNEL_READONLY    0x400   /* kernel read-only */
485 #define KBIF_NO_MAGAZINES       0x800   /* disable per-CPU magazines layer */
486 
487 #define KERN_PBUFPOOL_VERSION_1         1
488 #define KERN_PBUFPOOL_VERSION_2         2       /* added ctx retain/release */
489 #define KERN_PBUFPOOL_CURRENT_VERSION   KERN_PBUFPOOL_VERSION_2
490 
491 /*
492  * Kernel packet buffer pool memory info.
493  */
494 struct kern_pbufpool_memory_info {
495 	uint32_t                kpm_flags;      /* see KPMF_* */
496 	uint32_t                kpm_packets;    /* number of packets */
497 	uint32_t                kpm_max_frags;  /* max buflets per packet */
498 	uint32_t                kpm_buflets;    /* number of buflets */
499 	uint32_t                kpm_bufsize;    /* size of each buffer */
500 	uint32_t                kpm_bufsegs;    /* number of buffer segments */
501 	uint32_t                kpm_buf_seg_size; /* size of a buffer segment */
502 } __attribute__((aligned(64)));
503 
504 #define KPMF_EXTERNAL           0x1             /* externally configured */
505 
506 /*
507  * Kernel packet representation of packet_svc_class_t.
508  *
509  * We have a separate enum to separate the namespace just in case we need it.
510  */
511 typedef enum {
512 #ifdef BSD_KERNEL_PRIVATE
513 	KPKT_SC_UNSPEC  = -1, /* Internal: not specified */
514 #endif /* BSD_KERNEL_PRIVATE */
515 	KPKT_SC_BK_SYS  = PKT_SC_BK_SYS, /* lowest class */
516 	KPKT_SC_BK      = PKT_SC_BK,
517 
518 	KPKT_SC_BE      = PKT_SC_BE,
519 	KPKT_SC_RD      = PKT_SC_RD,
520 	KPKT_SC_OAM     = PKT_SC_OAM,
521 
522 	KPKT_SC_AV      = PKT_SC_AV,
523 	KPKT_SC_RV      = PKT_SC_RV,
524 	KPKT_SC_VI      = PKT_SC_VI,
525 	KPKT_SC_SIG     = PKT_SC_SIG,
526 
527 	KPKT_SC_VO      = PKT_SC_VO,
528 	KPKT_SC_CTL     = PKT_SC_CTL, /* highest class */
529 } kern_packet_svc_class_t;
530 
531 /* Maximum number of KPKT_SC values (excluding KPKT_SC_UNSPEC) */
532 #define KPKT_SC_MAX_CLASSES     10
533 
534 #define KPKT_VALID_SVC(c)                                               \
535 	(c == KPKT_SC_BK_SYS || c == KPKT_SC_BK || c == KPKT_SC_BE ||   \
536 	c == KPKT_SC_RD || c == KPKT_SC_OAM || c == KPKT_SC_AV ||       \
537 	c == KPKT_SC_RV || c == KPKT_SC_VI || c == KPKT_SC_SIG ||       \
538 	c == KPKT_SC_VO || c == KPKT_SC_CTL)
539 
540 #define KPKT_SVCIDX(c)          ((((c) >> 16) & 0xff) >> 3)
541 
542 /*
543  * Kernel packet representation of packet_traffic_class_t.
544  *
545  * We have a separate enum to separate the namespace just in case we need it.
546  */
547 typedef enum {
548 #ifdef BSD_KERNEL_PRIVATE
549 	KPKT_TC_UNSPEC  = -1,           /* Internal: not specified */
550 #endif /* BSD_KERNEL_PRIVATE */
551 	KPKT_TC_BE      = PKT_TC_BE,
552 	KPKT_TC_BK      = PKT_TC_BK,
553 	KPKT_TC_VI      = PKT_TC_VI,
554 	KPKT_TC_VO      = PKT_TC_VO,
555 #ifdef BSD_KERNEL_PRIVATE
556 	KPKT_TC_MAX     = 4,            /* Internal: traffic class count */
557 #endif /* BSD_KERNEL_PRIVATE */
558 } kern_packet_traffic_class_t;
559 
560 /*
561  * Modes for cloning a kernel packet.
562  *
563  * The "heavy" mode copies most of the metadata (except those pertaining
564  * to linkages to other objects), allocates new buffer(s) for the
565  * cloned packet, and copies old buffer(s) to new one(s).
566  *
567  * The "light" mode is to be used on a packet that's recently allocated,
568  * as the cloning process involves copying minimal metadata information,
569  * as well as adding reference(s) to the buffer(s) rather than copying.
570  */
571 typedef enum {
572 	KPKT_COPY_HEAVY = 0,   /* copy everything including buffers */
573 	KPKT_COPY_LIGHT        /* minimal copy, adding refs to buffers */
574 } kern_packet_copy_mode_t;
575 
576 __BEGIN_DECLS
577 /*
578  * Packets specific.
579  */
580 extern errno_t kern_packet_set_headroom(const kern_packet_t, const uint8_t);
581 extern uint8_t kern_packet_get_headroom(const kern_packet_t);
582 /* deprecated -- use kern_packet_set_headroom instead */
583 extern errno_t kern_packet_set_link_header_offset(const kern_packet_t,
584     const uint8_t);
585 /* deprecated -- use kern_packet_get_headroom instead */
586 extern uint16_t kern_packet_get_link_header_offset(const kern_packet_t);
587 extern errno_t kern_packet_set_link_header_length(const kern_packet_t,
588     const uint8_t);
589 extern uint8_t kern_packet_get_link_header_length(const kern_packet_t);
590 extern errno_t kern_packet_set_link_broadcast(const kern_packet_t);
591 extern boolean_t kern_packet_get_link_broadcast(const kern_packet_t);
592 extern errno_t kern_packet_set_link_multicast(const kern_packet_t);
593 extern boolean_t kern_packet_get_link_multicast(const kern_packet_t);
594 extern errno_t kern_packet_set_link_ethfcs(const kern_packet_t);
595 extern boolean_t kern_packet_get_link_ethfcs(const kern_packet_t);
596 /* deprecated -- use kern_packet_set_link_header_length instead */
597 extern errno_t kern_packet_set_network_header_offset(const kern_packet_t,
598     const uint16_t);
599 /* deprecated -- use kern_packet_get_link_header_length instead */
600 extern uint16_t kern_packet_get_network_header_offset(const kern_packet_t);
601 extern errno_t kern_packet_set_transport_traffic_background(
602 	const kern_packet_t);
603 extern boolean_t kern_packet_get_transport_traffic_background(
604 	const kern_packet_t);
605 extern errno_t kern_packet_set_transport_traffic_realtime(const kern_packet_t);
606 extern boolean_t kern_packet_get_transport_traffic_realtime(
607 	const kern_packet_t);
608 /* deprecated */
609 extern errno_t kern_packet_set_transport_header_offset(const kern_packet_t,
610     const uint16_t);
611 /* deprecated */
612 extern uint16_t kern_packet_get_transport_header_offset(const kern_packet_t);
613 extern errno_t kern_packet_set_transport_retransmit(const kern_packet_t);
614 extern boolean_t kern_packet_get_transport_retransmit(const kern_packet_t);
615 extern boolean_t kern_packet_get_transport_new_flow(const kern_packet_t);
616 extern boolean_t kern_packet_get_transport_last_packet(const kern_packet_t);
617 extern errno_t kern_packet_set_service_class(const kern_packet_t,
618     const kern_packet_svc_class_t);
619 extern kern_packet_svc_class_t kern_packet_get_service_class(
620 	const kern_packet_t);
621 extern errno_t kern_packet_get_service_class_index(
622 	const kern_packet_svc_class_t, uint32_t *);
623 extern boolean_t kern_packet_is_high_priority(
624 	const kern_packet_t);
625 extern errno_t kern_packet_set_traffic_class(const kern_packet_t,
626     kern_packet_traffic_class_t);
627 extern kern_packet_traffic_class_t kern_packet_get_traffic_class(
628 	const kern_packet_t);
629 extern errno_t kern_packet_set_inet_checksum(const kern_packet_t,
630     const packet_csum_flags_t, const uint16_t, const uint16_t);
631 extern packet_csum_flags_t kern_packet_get_inet_checksum(const kern_packet_t,
632     uint16_t *, uint16_t *);
633 extern errno_t kern_packet_get_timestamp(const kern_packet_t, uint64_t *,
634     boolean_t *);
635 extern errno_t kern_packet_set_timestamp(const kern_packet_t, uint64_t,
636     boolean_t);
637 extern errno_t kern_packet_get_timestamp_requested(const kern_packet_t,
638     boolean_t *);
639 extern errno_t kern_packet_get_tx_completion_status(const kern_packet_t,
640     kern_return_t *);
641 extern errno_t kern_packet_set_tx_completion_status(const kern_packet_t,
642     kern_return_t);
643 extern void kern_packet_tx_completion(const kern_packet_t, ifnet_t);
644 extern void kern_packet_set_group_start(const kern_packet_t);
645 extern boolean_t kern_packet_get_group_start(const kern_packet_t);
646 extern void kern_packet_set_group_end(const kern_packet_t);
647 extern boolean_t kern_packet_get_group_end(const kern_packet_t);
648 extern errno_t kern_packet_set_expire_time(const kern_packet_t, const uint64_t);
649 extern errno_t kern_packet_get_expire_time(const kern_packet_t, uint64_t *);
650 extern errno_t kern_packet_set_token(const kern_packet_t, const void *,
651     const uint16_t);
652 extern errno_t kern_packet_get_token(const kern_packet_t, void *, uint16_t *);
653 extern errno_t kern_packet_get_packetid(const kern_packet_t, packet_id_t *);
654 extern errno_t kern_packet_set_vlan_tag(const kern_packet_t, const uint16_t,
655     const boolean_t);
656 extern errno_t kern_packet_get_vlan_tag(const kern_packet_t, uint16_t *,
657     boolean_t *);
658 extern uint16_t kern_packet_get_vlan_id(const uint16_t);
659 extern uint8_t kern_packet_get_vlan_priority(const uint16_t);
660 extern void kern_packet_set_wake_flag(const kern_packet_t);
661 extern boolean_t kern_packet_get_wake_flag(const kern_packet_t);
662 
663 /*
664  * Quantum & Packets.
665  */
666 extern void kern_packet_set_flow_uuid(const kern_packet_t, const uuid_t);
667 extern void kern_packet_get_flow_uuid(const kern_packet_t, uuid_t *);
668 extern void kern_packet_clear_flow_uuid(const kern_packet_t);
669 extern void kern_packet_get_euuid(const kern_packet_t, uuid_t);
670 extern void kern_packet_set_policy_id(const kern_packet_t, uint32_t);
671 extern uint32_t kern_packet_get_policy_id(const kern_packet_t);
672 extern kern_packet_idx_t kern_packet_get_object_index(const kern_packet_t);
673 extern uint32_t kern_packet_get_data_length(const kern_packet_t);
674 extern uint32_t kern_packet_get_buflet_count(const kern_packet_t);
675 extern errno_t kern_packet_set_buflet_count(const kern_packet_t, uint32_t);
676 extern kern_buflet_t kern_packet_get_next_buflet(const kern_packet_t,
677     const kern_buflet_t);
678 extern errno_t kern_packet_finalize(const kern_packet_t);
679 extern errno_t kern_packet_clone(const kern_packet_t, kern_packet_t *,
680     kern_packet_copy_mode_t);
681 extern errno_t kern_packet_clone_nosleep(const kern_packet_t, kern_packet_t *,
682     kern_packet_copy_mode_t);
683 extern errno_t kern_packet_add_buflet(const kern_packet_t ph,
684     const kern_buflet_t bprev, const kern_buflet_t bnew);
685 extern void kern_packet_append(const kern_packet_t, const kern_packet_t);
686 extern kern_packet_t kern_packet_get_next(const kern_packet_t);
687 extern void kern_packet_set_next(const kern_packet_t, const kern_packet_t);
688 extern void kern_packet_set_chain_counts(const kern_packet_t, uint32_t,
689     uint32_t);
690 extern void kern_packet_get_chain_counts(const kern_packet_t, uint32_t *,
691     uint32_t *);
692 
693 /*
694  * Misc.
695  */
696 extern uint32_t kern_inet_checksum(const void *, uint32_t, uint32_t);
697 extern uint32_t kern_copy_and_inet_checksum(const void *, void *,
698     uint32_t, uint32_t);
699 extern void kern_packet_set_trace_id(const kern_packet_t, packet_trace_id_t);
700 extern packet_trace_id_t kern_packet_get_trace_id(const kern_packet_t);
701 extern void kern_packet_trace_event(const kern_packet_t, uint32_t);
702 extern errno_t kern_packet_copy_bytes(const kern_packet_t, size_t, size_t,
703     void*);
704 
705 /*
706  * Buflets.
707  */
708 extern errno_t kern_buflet_set_data_address(const kern_buflet_t, const void *);
709 extern void *kern_buflet_get_data_address(const kern_buflet_t);
710 extern errno_t kern_buflet_set_data_offset(const kern_buflet_t, const uint16_t);
711 extern uint16_t kern_buflet_get_data_offset(const kern_buflet_t);
712 extern errno_t kern_buflet_set_data_length(const kern_buflet_t, const uint16_t);
713 extern uint16_t kern_buflet_get_data_length(const kern_buflet_t);
714 extern void *kern_buflet_get_object_address(const kern_buflet_t);
715 extern uint32_t kern_buflet_get_object_limit(const kern_buflet_t);
716 extern kern_segment_t kern_buflet_get_object_segment(const kern_buflet_t,
717     kern_obj_idx_seg_t *);
718 extern errno_t kern_buflet_set_data_limit(const kern_buflet_t, const uint16_t);
719 extern uint16_t kern_buflet_get_data_limit(const kern_buflet_t);
720 
721 /*
722  * Packet buffer pool.
723  */
724 typedef void (*alloc_cb_func_t)(kern_packet_t packet, uint32_t pkt_index,
725     const void *ctx);
726 extern errno_t kern_pbufpool_create(const struct kern_pbufpool_init *,
727     kern_pbufpool_t *, struct kern_pbufpool_memory_info *);
728 extern void *kern_pbufpool_get_context(const kern_pbufpool_t pbufpool);
729 extern errno_t kern_pbufpool_get_memory_info(const kern_pbufpool_t pbufpool,
730     struct kern_pbufpool_memory_info *pbufpool_mem_ref);
731 extern errno_t kern_pbufpool_alloc(const kern_pbufpool_t pbufpool,
732     const uint32_t bufcnt, kern_packet_t *packet);
733 extern errno_t kern_pbufpool_alloc_batch(const kern_pbufpool_t pbufpool,
734     const uint32_t bufcnt, kern_packet_t *array, uint32_t *size);
735 extern errno_t kern_pbufpool_alloc_batch_callback(
736 	const kern_pbufpool_t pbufpool, const uint32_t bufcnt, kern_packet_t *array,
737 	uint32_t *size, alloc_cb_func_t cb, const void *ctx);
738 extern errno_t kern_pbufpool_alloc_nosleep(const kern_pbufpool_t pbufpool,
739     const uint32_t bufcnt, kern_packet_t *packet);
740 extern errno_t kern_pbufpool_alloc_batch_nosleep(const kern_pbufpool_t pbufpool,
741     const uint32_t bufcnt, kern_packet_t *array, uint32_t *size);
742 extern errno_t kern_pbufpool_alloc_batch_nosleep_callback(
743 	const kern_pbufpool_t pbufpool, const uint32_t bufcnt,
744 	kern_packet_t *array, uint32_t *size, alloc_cb_func_t cb, const void *ctx);
745 extern void kern_pbufpool_free(const kern_pbufpool_t pbufpool, kern_packet_t);
746 extern void kern_pbufpool_free_batch(const kern_pbufpool_t pbufpool,
747     kern_packet_t *array, uint32_t size);
748 extern void kern_pbufpool_free_chain(const kern_pbufpool_t pbufpool,
749     kern_packet_t chain);
750 extern errno_t kern_pbufpool_alloc_buffer(const kern_pbufpool_t pbufpool,
751     mach_vm_address_t *buffer, kern_segment_t *sg, kern_obj_idx_seg_t *sg_idx);
752 extern errno_t kern_pbufpool_alloc_buffer_nosleep(const kern_pbufpool_t
753     pbufpool, mach_vm_address_t *buffer, kern_segment_t *sg,
754     kern_obj_idx_seg_t *sg_idx);
755 extern void kern_pbufpool_free_buffer(const kern_pbufpool_t pbufpool,
756     mach_vm_address_t baddr);
757 extern errno_t kern_pbufpool_alloc_buflet(const kern_pbufpool_t,
758     kern_buflet_t *);
759 extern errno_t kern_pbufpool_alloc_buflet_nosleep(const kern_pbufpool_t,
760     kern_buflet_t *);
761 extern void kern_pbufpool_destroy(kern_pbufpool_t);
762 extern kern_segment_idx_t kern_segment_get_index(const kern_segment_t);
763 __END_DECLS
764 #endif /* KERNEL */
765 #endif /* PRIVATE */
766 #endif /* !_SKYWALK_OS_PACKET_H_ */
767