xref: /xnu-8020.101.4/bsd/skywalk/packet/os_packet.h (revision e7776783b89a353188416a9a346c6cdb4928faad)
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_PARTIAL_CHECKSUM(_p) \
196 	((_p)->pkt_csum_flags & (PACKET_CSUM_PARTIAL))
197 
198 #define PACKET_CSUM_RX_FLAGS                                    \
199 	(PACKET_CSUM_IP_CHECKED | PACKET_CSUM_IP_VALID |        \
200 	PACKET_CSUM_DATA_VALID | PACKET_CSUM_PSEUDO_HDR |       \
201 	PACKET_CSUM_PARTIAL)
202 
203 /*
204  * TODO: [email protected] -- these are temporary and should be removed later.
205  */
206 #define OS_PACKET_HAS_CHECKSUM_API      1
207 #define OS_PACKET_HAS_SEGMENT_COUNT     1
208 #define OS_PACKET_HAS_TRACING_API       1
209 
210 /*
211  * Valid values for pkt_aggr_type.
212  */
213 #define PKT_AGGR_NONE                0x00 /* no aggregation */
214 #define PKT_AGGR_IP_CHAIN            0x01 /* buflet chain of discrete IP packets containing contiguous L4 frames */
215 #define PKT_AGGR_SINGLE_IP           0x02 /* buflet chain representing single IP packet */
216 #define PKT_AGGR_SINGLE_IP_PACKED    0x03 /* buflet chain representing single IP packet in packed format */
217 
218 /*
219  * packet_id is a per packet metadata which can be set on a packet by the
220  * application. It can be used to identify either an individual or a group
221  * of packets within the networking stack and driver.
222  */
223 typedef struct packet_id {
224 	/*
225 	 * version of this structure.
226 	 */
227 	uint8_t     pktid_version;
228 #define OS_PACKET_PKTID_VERSION_1          1
229 #define OS_PACKET_PKTID_VERSION_CURRENT    OS_PACKET_PKTID_VERSION_1
230 	/*
231 	 * payload type of the packet, opaque to the network stack.
232 	 */
233 	uint8_t     pktid_payload_type;
234 	/*
235 	 * packet sequence number, monotonically increasing.
236 	 */
237 	uint16_t    pktid_sequence_number;
238 	/*
239 	 * packet timestamp, monotonically increasing and opaque to the
240 	 * network stack. Sample rate of the timestamp clock is determined
241 	 * by the application.
242 	 */
243 	uint32_t    pktid_timestamp;
244 	/*
245 	 * Identifier for streams defined by the application.
246 	 */
247 	uint32_t    pktid_stream_identifier;
248 	/*
249 	 * reserved for future use.
250 	 */
251 	uint32_t    _reserved;
252 } packet_id_t;
253 
254 /*
255  * Packet Trace code
256  * Used with os_packet_trace_* functions.
257  * Total of 12bit (0xABC) code space available, current sub-code allocation is:
258  *     0x00C code space for FSW Rx path.
259  *     0x01C code space for FSW Tx path.
260  *
261  * More sub-code can be added for other packet data path, e.g. uPipe, BSD, etc.
262  *
263  * Note:
264  *     1. Needs to include <sys/kdebug.h> to use the values.
265  *     2. When making changes to sub-code/value, update static assertions in
266  *        pp_init and probe list ariadne-plists/skywalk-tracepoints.plist.
267  *
268  */
269 /* Rx Group */
270 #define PKT_TRACE_RX_DRV_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x001) | DBG_FUNC_START)
271 #define PKT_TRACE_RX_DRV_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x001) | DBG_FUNC_END)
272 
273 #define PKT_TRACE_RX_FSW_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x002) | DBG_FUNC_START)
274 #define PKT_TRACE_RX_FSW_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x002) | DBG_FUNC_END)
275 
276 #define PKT_TRACE_RX_CHN_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x003) | DBG_FUNC_START)
277 #define PKT_TRACE_RX_CHN_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x003) | DBG_FUNC_END)
278 
279 
280 /* Tx Group */
281 #define PKT_TRACE_TX_FSW_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x010) | DBG_FUNC_START)
282 #define PKT_TRACE_TX_FSW_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x010) | DBG_FUNC_END)
283 
284 #define PKT_TRACE_TX_AQM_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x011) | DBG_FUNC_START)
285 #define PKT_TRACE_TX_AQM_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x011) | DBG_FUNC_END)
286 
287 #define PKT_TRACE_TX_DRV_START      (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x012) | DBG_FUNC_START)
288 #define PKT_TRACE_TX_DRV_END        (SKYWALKDBG_CODE(DBG_SKYWALK_PACKET, 0x012) | DBG_FUNC_END)
289 
290 
291 #ifndef KERNEL
292 /*
293  * User APIs.
294  */
295 
296 /*
297  * Opaque handles.
298  */
299 struct __user_buflet;
300 typedef uint64_t                        packet_t;
301 typedef struct __user_buflet            *buflet_t;
302 
303 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
304 /*
305  * Packets specific.
306  */
307 extern int os_packet_set_headroom(const packet_t, const uint8_t);
308 extern uint8_t os_packet_get_headroom(const packet_t);
309 extern int os_packet_set_link_header_length(const packet_t, const uint8_t);
310 extern uint8_t os_packet_get_link_header_length(const packet_t);
311 extern int os_packet_set_link_broadcast(const packet_t);
312 extern boolean_t os_packet_get_link_broadcast(const packet_t);
313 extern int os_packet_set_link_multicast(const packet_t);
314 extern boolean_t os_packet_get_link_multicast(const packet_t);
315 extern int os_packet_set_link_ethfcs(const packet_t);
316 extern boolean_t os_packet_get_link_ethfcs(const packet_t);
317 extern int os_packet_set_transport_traffic_background(const packet_t);
318 extern boolean_t os_packet_get_transport_traffic_background(const packet_t);
319 extern int os_packet_set_transport_traffic_realtime(const packet_t);
320 extern boolean_t os_packet_get_transport_traffic_realtime(const packet_t);
321 extern int os_packet_set_transport_retransmit(const packet_t);
322 extern boolean_t os_packet_get_transport_retransmit(const packet_t);
323 extern int os_packet_set_transport_last_packet(const packet_t);
324 extern int os_packet_set_service_class(const packet_t,
325     const packet_svc_class_t);
326 extern packet_svc_class_t os_packet_get_service_class(const packet_t);
327 extern int os_packet_set_compression_generation_count(const packet_t, const uint32_t);
328 extern uint32_t os_packet_get_compression_generation_count(const packet_t);
329 extern int os_packet_set_traffic_class(const packet_t, packet_traffic_class_t);
330 extern packet_traffic_class_t os_packet_get_traffic_class(const packet_t);
331 extern int os_packet_set_inet_checksum(const packet_t,
332     const packet_csum_flags_t, const uint16_t, const uint16_t);
333 extern packet_csum_flags_t os_packet_get_inet_checksum(const packet_t,
334     uint16_t *, uint16_t *);
335 extern void os_packet_set_group_start(const packet_t);
336 extern boolean_t os_packet_get_group_start(const packet_t);
337 extern void os_packet_set_group_end(const packet_t);
338 extern boolean_t os_packet_get_group_end(const packet_t);
339 extern int os_packet_set_expire_time(const packet_t, const uint64_t);
340 extern int os_packet_get_expire_time(const packet_t, uint64_t *);
341 extern int os_packet_set_token(const packet_t, const void *, const uint16_t);
342 extern int os_packet_get_packetid(const packet_t, packet_id_t *);
343 extern int os_packet_set_packetid(const packet_t, packet_id_t *);
344 extern int os_packet_set_vlan_tag(const packet_t, const uint16_t,
345     const boolean_t);
346 extern int os_packet_get_vlan_tag(const packet_t, uint16_t *, boolean_t *);
347 extern uint16_t os_packet_get_vlan_id(const uint16_t);
348 extern uint8_t os_packet_get_vlan_priority(const uint16_t);
349 #define HAS_OS_PACKET_GET_WAKE_FLAG 1
350 extern boolean_t os_packet_get_wake_flag(const packet_t);
351 #define HAS_OS_PACKET_KEEP_ALIVE 1
352 extern boolean_t os_packet_get_keep_alive(const packet_t);
353 extern void os_packet_set_keep_alive(const packet_t, const boolean_t);
354 extern boolean_t os_packet_get_truncated(const packet_t);
355 extern uint8_t os_packet_get_aggregation_type(const packet_t ph);
356 
357 /*
358  * Quantum & Packets.
359  */
360 extern void os_packet_set_flow_uuid(const packet_t, const uuid_t flow_uuid);
361 extern void os_packet_get_flow_uuid(const packet_t, uuid_t *flow_uuid);
362 extern void os_packet_clear_flow_uuid(const packet_t);
363 extern uint32_t os_packet_get_data_length(const packet_t);
364 extern uint32_t os_packet_get_buflet_count(const packet_t);
365 extern buflet_t os_packet_get_next_buflet(const packet_t, const buflet_t);
366 extern uint32_t os_packet_get_segment_count(const packet_t ph);
367 extern int os_packet_finalize(const packet_t);
368 extern int os_packet_add_buflet(const packet_t ph, const buflet_t bprev,
369     const buflet_t bnew);
370 /* increment use count on packet */
371 extern int os_packet_increment_use_count(const packet_t ph);
372 /* decrement use count on packet and retrieve new value  */
373 extern int os_packet_decrement_use_count(const packet_t ph, uint16_t *use_cnt);
374 
375 extern packet_trace_id_t os_packet_get_trace_id(const packet_t ph);
376 extern void os_packet_set_trace_id(const packet_t ph, packet_trace_id_t);
377 extern void os_packet_trace_event(const packet_t ph, uint32_t);
378 
379 /*
380  * Misc.
381  */
382 extern uint32_t os_inet_checksum(const void *, uint32_t, uint32_t);
383 extern uint32_t os_copy_and_inet_checksum(const void *, void *,
384     uint32_t, uint32_t);
385 
386 /*
387  * Buflets.
388  */
389 extern int os_buflet_set_data_offset(const buflet_t, const uint16_t);
390 extern uint16_t os_buflet_get_data_offset(const buflet_t);
391 extern int os_buflet_set_data_length(const buflet_t, const uint16_t);
392 extern uint16_t os_buflet_get_data_length(const buflet_t);
393 extern void *os_buflet_get_object_address(const buflet_t);
394 extern uint32_t os_buflet_get_object_limit(const buflet_t);
395 extern void *os_buflet_get_data_address(const buflet_t);
396 extern uint16_t os_buflet_get_data_limit(const buflet_t);
397 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
398 #else /* KERNEL */
399 /*
400  * Kernel APIs.
401  */
402 
403 /*
404  * Opaque handles.
405  */
406 struct __kern_buflet;
407 struct kern_pbufpool;
408 struct sksegment;
409 
410 typedef struct kern_pbufpool            *kern_pbufpool_t;
411 typedef uint64_t                        kern_packet_t;
412 typedef uint32_t                        kern_packet_idx_t;
413 typedef struct __kern_buflet            *kern_buflet_t;
414 typedef uint32_t                        kern_obj_idx_seg_t;
415 typedef struct sksegment                *kern_segment_t;
416 typedef uint32_t                        kern_segment_idx_t;
417 
418 /*
419  * @typedef pbuf_seg_ctor_fn_t
420  * @abstract Buffer segment constructor callback.
421  * @param buf_seg Buffer segment handle.
422  * @param buf_desc IOSKMemoryDescriptor handle for buffer segment.
423  * @discussion This callback is invoked when a segment has been activated.
424  *      If applicable, the owner should wire/prepare the memory and insert
425  *      it into the memory mapper (IOMMU/DART) prior to returning.
426  */
427 typedef void (*pbuf_seg_ctor_fn_t)(const kern_pbufpool_t,
428     const kern_segment_t buf_seg, const IOSKMemoryDescriptor buf_desc);
429 
430 /*
431  * @typedef pbuf_seg_dtor_fn_t
432  * @abstract Buffer segment destructor callback.
433  * @param buf_seg Buffer segment handle.
434  * @param buf_desc IOSKMemoryDescriptor handle for buffer segment.
435  * @discussion This callback is invoked when a segment is about to be
436  *      freed.  The owner should reverse what had been done earlier
437  *      at pbuf_seg_ctor_fn_t() constructor time.  If applicable,
438  *      it should remove the memory from mapper (IOMMU/DART), and
439  *      unwire/complete it prior to returning.
440  */
441 typedef void (*pbuf_seg_dtor_fn_t)(const kern_pbufpool_t,
442     const kern_segment_t buf_seg, const IOSKMemoryDescriptor buf_desc);
443 
444 typedef void (*pbuf_ctx_retain_fn_t)(void *const ctx);
445 typedef void (*pbuf_ctx_release_fn_t)(void *const ctx);
446 
447 typedef uint8_t pbufpool_name_t[64];
448 
449 /*
450  * Kernel packet buffer pool init.
451  */
452 struct kern_pbufpool_init {
453 	uint32_t                kbi_version;    /* current version */
454 	pbufpool_name_t         kbi_name;       /* optional */
455 	uint32_t                kbi_flags;      /* see KBI_* */
456 	uint32_t                kbi_packets;    /* required */
457 	uint32_t                kbi_max_frags;  /* max buflets per packet */
458 	uint32_t                kbi_buflets;    /* >= packets (optional) */
459 	uint32_t                kbi_bufsize;    /* required */
460 	uint32_t                kbi_buf_seg_size; /* optional */
461 	pbuf_seg_ctor_fn_t      kbi_buf_seg_ctor; /* optional */
462 	pbuf_seg_dtor_fn_t      kbi_buf_seg_dtor; /* optional */
463 	void *                  kbi_ctx;         /* optional */
464 	pbuf_ctx_retain_fn_t    kbi_ctx_retain;  /* optional */
465 	pbuf_ctx_release_fn_t   kbi_ctx_release; /* optional */
466 };
467 
468 #define KBIF_QUANTUM            0x1     /* simple packet (non-networking) */
469 #define KBIF_PERSISTENT         0x2     /* persistent memory (wired) */
470 #define KBIF_MONOLITHIC         0x4     /* single segment mode */
471 #define KBIF_BUFFER_ON_DEMAND   0x8     /* attach/detach buffers on demand */
472 #define KBIF_INHIBIT_CACHE      0x10    /* caching-inhibited */
473 #define KBIF_USER_ACCESS        0x20    /* allow userspace access */
474 #define KBIF_VIRTUAL_DEVICE     0x40    /* device is virtual (no DMA) */
475 #define KBIF_PHYS_CONTIGUOUS    0x80    /* physically-contiguous segment(s) */
476 #define KBIF_IODIR_IN           0x100   /* io direction in (device to host) */
477 #define KBIF_IODIR_OUT          0x200   /* io direction out (host to device) */
478 #define KBIF_KERNEL_READONLY    0x400   /* kernel read-only */
479 #define KBIF_NO_MAGAZINES       0x800   /* disable per-CPU magazines layer */
480 
481 #define KERN_PBUFPOOL_VERSION_1         1
482 #define KERN_PBUFPOOL_VERSION_2         2       /* added ctx retain/release */
483 #define KERN_PBUFPOOL_CURRENT_VERSION   KERN_PBUFPOOL_VERSION_2
484 
485 /*
486  * Kernel packet buffer pool memory info.
487  */
488 struct kern_pbufpool_memory_info {
489 	uint32_t                kpm_flags;      /* see KPMF_* */
490 	uint32_t                kpm_packets;    /* number of packets */
491 	uint32_t                kpm_max_frags;  /* max buflets per packet */
492 	uint32_t                kpm_buflets;    /* number of buflets */
493 	uint32_t                kpm_bufsize;    /* size of each buffer */
494 	uint32_t                kpm_bufsegs;    /* number of buffer segments */
495 	uint32_t                kpm_buf_seg_size; /* size of a buffer segment */
496 } __attribute__((aligned(64)));
497 
498 #define KPMF_EXTERNAL           0x1             /* externally configured */
499 
500 /*
501  * Kernel packet representation of packet_svc_class_t.
502  *
503  * We have a separate enum to separate the namespace just in case we need it.
504  */
505 typedef enum {
506 #ifdef BSD_KERNEL_PRIVATE
507 	KPKT_SC_UNSPEC  = -1, /* Internal: not specified */
508 #endif /* BSD_KERNEL_PRIVATE */
509 	KPKT_SC_BK_SYS  = PKT_SC_BK_SYS, /* lowest class */
510 	KPKT_SC_BK      = PKT_SC_BK,
511 
512 	KPKT_SC_BE      = PKT_SC_BE,
513 	KPKT_SC_RD      = PKT_SC_RD,
514 	KPKT_SC_OAM     = PKT_SC_OAM,
515 
516 	KPKT_SC_AV      = PKT_SC_AV,
517 	KPKT_SC_RV      = PKT_SC_RV,
518 	KPKT_SC_VI      = PKT_SC_VI,
519 	KPKT_SC_SIG     = PKT_SC_SIG,
520 
521 	KPKT_SC_VO      = PKT_SC_VO,
522 	KPKT_SC_CTL     = PKT_SC_CTL, /* highest class */
523 } kern_packet_svc_class_t;
524 
525 /* Maximum number of KPKT_SC values (excluding KPKT_SC_UNSPEC) */
526 #define KPKT_SC_MAX_CLASSES     10
527 
528 #define KPKT_VALID_SVC(c)                                               \
529 	(c == KPKT_SC_BK_SYS || c == KPKT_SC_BK || c == KPKT_SC_BE ||   \
530 	c == KPKT_SC_RD || c == KPKT_SC_OAM || c == KPKT_SC_AV ||       \
531 	c == KPKT_SC_RV || c == KPKT_SC_VI || c == KPKT_SC_SIG ||       \
532 	c == KPKT_SC_VO || c == KPKT_SC_CTL)
533 
534 #define KPKT_SVCIDX(c)          ((((c) >> 16) & 0xff) >> 3)
535 
536 /*
537  * Kernel packet representation of packet_traffic_class_t.
538  *
539  * We have a separate enum to separate the namespace just in case we need it.
540  */
541 typedef enum {
542 #ifdef BSD_KERNEL_PRIVATE
543 	KPKT_TC_UNSPEC  = -1,           /* Internal: not specified */
544 #endif /* BSD_KERNEL_PRIVATE */
545 	KPKT_TC_BE      = PKT_TC_BE,
546 	KPKT_TC_BK      = PKT_TC_BK,
547 	KPKT_TC_VI      = PKT_TC_VI,
548 	KPKT_TC_VO      = PKT_TC_VO,
549 #ifdef BSD_KERNEL_PRIVATE
550 	KPKT_TC_MAX     = 4,            /* Internal: traffic class count */
551 #endif /* BSD_KERNEL_PRIVATE */
552 } kern_packet_traffic_class_t;
553 
554 /*
555  * Modes for cloning a kernel packet.
556  *
557  * The "heavy" mode copies most of the metadata (except those pertaining
558  * to linkages to other objects), allocates new buffer(s) for the
559  * cloned packet, and copies old buffer(s) to new one(s).
560  *
561  * The "light" mode is to be used on a packet that's recently allocated,
562  * as the cloning process involves copying minimal metadata information,
563  * as well as adding reference(s) to the buffer(s) rather than copying.
564  */
565 typedef enum {
566 	KPKT_COPY_HEAVY = 0,   /* copy everything including buffers */
567 	KPKT_COPY_LIGHT        /* minimal copy, adding refs to buffers */
568 } kern_packet_copy_mode_t;
569 
570 __BEGIN_DECLS
571 /*
572  * Packets specific.
573  */
574 extern errno_t kern_packet_set_headroom(const kern_packet_t, const uint8_t);
575 extern uint8_t kern_packet_get_headroom(const kern_packet_t);
576 /* deprecated -- use kern_packet_set_headroom instead */
577 extern errno_t kern_packet_set_link_header_offset(const kern_packet_t,
578     const uint8_t);
579 /* deprecated -- use kern_packet_get_headroom instead */
580 extern uint16_t kern_packet_get_link_header_offset(const kern_packet_t);
581 extern errno_t kern_packet_set_link_header_length(const kern_packet_t,
582     const uint8_t);
583 extern uint8_t kern_packet_get_link_header_length(const kern_packet_t);
584 extern errno_t kern_packet_set_link_broadcast(const kern_packet_t);
585 extern boolean_t kern_packet_get_link_broadcast(const kern_packet_t);
586 extern errno_t kern_packet_set_link_multicast(const kern_packet_t);
587 extern boolean_t kern_packet_get_link_multicast(const kern_packet_t);
588 extern errno_t kern_packet_set_link_ethfcs(const kern_packet_t);
589 extern boolean_t kern_packet_get_link_ethfcs(const kern_packet_t);
590 /* deprecated -- use kern_packet_set_link_header_length instead */
591 extern errno_t kern_packet_set_network_header_offset(const kern_packet_t,
592     const uint16_t);
593 /* deprecated -- use kern_packet_get_link_header_length instead */
594 extern uint16_t kern_packet_get_network_header_offset(const kern_packet_t);
595 extern errno_t kern_packet_set_transport_traffic_background(
596 	const kern_packet_t);
597 extern boolean_t kern_packet_get_transport_traffic_background(
598 	const kern_packet_t);
599 extern errno_t kern_packet_set_transport_traffic_realtime(const kern_packet_t);
600 extern boolean_t kern_packet_get_transport_traffic_realtime(
601 	const kern_packet_t);
602 /* deprecated */
603 extern errno_t kern_packet_set_transport_header_offset(const kern_packet_t,
604     const uint16_t);
605 /* deprecated */
606 extern uint16_t kern_packet_get_transport_header_offset(const kern_packet_t);
607 extern errno_t kern_packet_set_transport_retransmit(const kern_packet_t);
608 extern boolean_t kern_packet_get_transport_retransmit(const kern_packet_t);
609 extern boolean_t kern_packet_get_transport_new_flow(const kern_packet_t);
610 extern boolean_t kern_packet_get_transport_last_packet(const kern_packet_t);
611 extern errno_t kern_packet_set_service_class(const kern_packet_t,
612     const kern_packet_svc_class_t);
613 extern kern_packet_svc_class_t kern_packet_get_service_class(
614 	const kern_packet_t);
615 extern errno_t kern_packet_get_service_class_index(
616 	const kern_packet_svc_class_t, uint32_t *);
617 extern boolean_t kern_packet_is_high_priority(
618 	const kern_packet_t);
619 extern errno_t kern_packet_set_traffic_class(const kern_packet_t,
620     kern_packet_traffic_class_t);
621 extern kern_packet_traffic_class_t kern_packet_get_traffic_class(
622 	const kern_packet_t);
623 extern errno_t kern_packet_set_inet_checksum(const kern_packet_t,
624     const packet_csum_flags_t, const uint16_t, const uint16_t);
625 extern packet_csum_flags_t kern_packet_get_inet_checksum(const kern_packet_t,
626     uint16_t *, uint16_t *);
627 extern errno_t kern_packet_get_timestamp(const kern_packet_t, uint64_t *,
628     boolean_t *);
629 extern errno_t kern_packet_set_timestamp(const kern_packet_t, uint64_t,
630     boolean_t);
631 extern errno_t kern_packet_get_timestamp_requested(const kern_packet_t,
632     boolean_t *);
633 extern errno_t kern_packet_get_tx_completion_status(const kern_packet_t,
634     kern_return_t *);
635 extern errno_t kern_packet_set_tx_completion_status(const kern_packet_t,
636     kern_return_t);
637 extern void kern_packet_tx_completion(const kern_packet_t, ifnet_t);
638 extern void kern_packet_set_group_start(const kern_packet_t);
639 extern boolean_t kern_packet_get_group_start(const kern_packet_t);
640 extern void kern_packet_set_group_end(const kern_packet_t);
641 extern boolean_t kern_packet_get_group_end(const kern_packet_t);
642 extern errno_t kern_packet_set_expire_time(const kern_packet_t, const uint64_t);
643 extern errno_t kern_packet_get_expire_time(const kern_packet_t, uint64_t *);
644 extern errno_t kern_packet_set_token(const kern_packet_t, const void *,
645     const uint16_t);
646 extern errno_t kern_packet_get_token(const kern_packet_t, void *, uint16_t *);
647 extern errno_t kern_packet_get_packetid(const kern_packet_t, packet_id_t *);
648 extern errno_t kern_packet_set_vlan_tag(const kern_packet_t, const uint16_t,
649     const boolean_t);
650 extern errno_t kern_packet_get_vlan_tag(const kern_packet_t, uint16_t *,
651     boolean_t *);
652 extern uint16_t kern_packet_get_vlan_id(const uint16_t);
653 extern uint8_t kern_packet_get_vlan_priority(const uint16_t);
654 extern void kern_packet_set_wake_flag(const kern_packet_t);
655 extern boolean_t kern_packet_get_wake_flag(const kern_packet_t);
656 
657 /*
658  * Quantum & Packets.
659  */
660 extern void kern_packet_set_flow_uuid(const kern_packet_t, const uuid_t);
661 extern void kern_packet_get_flow_uuid(const kern_packet_t, uuid_t *);
662 extern void kern_packet_clear_flow_uuid(const kern_packet_t);
663 extern void kern_packet_get_euuid(const kern_packet_t, uuid_t);
664 extern void kern_packet_set_policy_id(const kern_packet_t, uint32_t);
665 extern uint32_t kern_packet_get_policy_id(const kern_packet_t);
666 extern kern_packet_idx_t kern_packet_get_object_index(const kern_packet_t);
667 extern uint32_t kern_packet_get_data_length(const kern_packet_t);
668 extern uint32_t kern_packet_get_buflet_count(const kern_packet_t);
669 extern errno_t kern_packet_set_buflet_count(const kern_packet_t, uint32_t);
670 extern kern_buflet_t kern_packet_get_next_buflet(const kern_packet_t,
671     const kern_buflet_t);
672 extern errno_t kern_packet_finalize(const kern_packet_t);
673 extern errno_t kern_packet_clone(const kern_packet_t, kern_packet_t *,
674     kern_packet_copy_mode_t);
675 extern errno_t kern_packet_clone_nosleep(const kern_packet_t, kern_packet_t *,
676     kern_packet_copy_mode_t);
677 extern errno_t kern_packet_add_buflet(const kern_packet_t ph,
678     const kern_buflet_t bprev, const kern_buflet_t bnew);
679 extern void kern_packet_append(const kern_packet_t, const kern_packet_t);
680 extern kern_packet_t kern_packet_get_next(const kern_packet_t);
681 extern void kern_packet_set_next(const kern_packet_t, const kern_packet_t);
682 extern void kern_packet_set_chain_counts(const kern_packet_t, uint32_t,
683     uint32_t);
684 extern void kern_packet_get_chain_counts(const kern_packet_t, uint32_t *,
685     uint32_t *);
686 
687 /*
688  * Misc.
689  */
690 extern uint32_t kern_inet_checksum(const void *, uint32_t, uint32_t);
691 extern uint32_t kern_copy_and_inet_checksum(const void *, void *,
692     uint32_t, uint32_t);
693 extern void kern_packet_set_trace_id(const kern_packet_t, packet_trace_id_t);
694 extern packet_trace_id_t kern_packet_get_trace_id(const kern_packet_t);
695 extern void kern_packet_trace_event(const kern_packet_t, uint32_t);
696 extern errno_t kern_packet_copy_bytes(const kern_packet_t, size_t, size_t,
697     void*);
698 
699 /*
700  * Buflets.
701  */
702 extern errno_t kern_buflet_set_data_address(const kern_buflet_t, const void *);
703 extern void *kern_buflet_get_data_address(const kern_buflet_t);
704 extern errno_t kern_buflet_set_data_offset(const kern_buflet_t, const uint16_t);
705 extern uint16_t kern_buflet_get_data_offset(const kern_buflet_t);
706 extern errno_t kern_buflet_set_data_length(const kern_buflet_t, const uint16_t);
707 extern uint16_t kern_buflet_get_data_length(const kern_buflet_t);
708 extern void *kern_buflet_get_object_address(const kern_buflet_t);
709 extern uint32_t kern_buflet_get_object_limit(const kern_buflet_t);
710 extern kern_segment_t kern_buflet_get_object_segment(const kern_buflet_t,
711     kern_obj_idx_seg_t *);
712 extern errno_t kern_buflet_set_data_limit(const kern_buflet_t, const uint16_t);
713 extern uint16_t kern_buflet_get_data_limit(const kern_buflet_t);
714 
715 /*
716  * Packet buffer pool.
717  */
718 typedef void (*alloc_cb_func_t)(kern_packet_t packet, uint32_t pkt_index,
719     const void *ctx);
720 extern errno_t kern_pbufpool_create(const struct kern_pbufpool_init *,
721     kern_pbufpool_t *, struct kern_pbufpool_memory_info *);
722 extern void *kern_pbufpool_get_context(const kern_pbufpool_t pbufpool);
723 extern errno_t kern_pbufpool_get_memory_info(const kern_pbufpool_t pbufpool,
724     struct kern_pbufpool_memory_info *pbufpool_mem_ref);
725 extern errno_t kern_pbufpool_alloc(const kern_pbufpool_t pbufpool,
726     const uint32_t bufcnt, kern_packet_t *packet);
727 extern errno_t kern_pbufpool_alloc_batch(const kern_pbufpool_t pbufpool,
728     const uint32_t bufcnt, kern_packet_t *array, uint32_t *size);
729 extern errno_t kern_pbufpool_alloc_batch_callback(
730 	const kern_pbufpool_t pbufpool, const uint32_t bufcnt, kern_packet_t *array,
731 	uint32_t *size, alloc_cb_func_t cb, const void *ctx);
732 extern errno_t kern_pbufpool_alloc_nosleep(const kern_pbufpool_t pbufpool,
733     const uint32_t bufcnt, kern_packet_t *packet);
734 extern errno_t kern_pbufpool_alloc_batch_nosleep(const kern_pbufpool_t pbufpool,
735     const uint32_t bufcnt, kern_packet_t *array, uint32_t *size);
736 extern errno_t kern_pbufpool_alloc_batch_nosleep_callback(
737 	const kern_pbufpool_t pbufpool, const uint32_t bufcnt,
738 	kern_packet_t *array, uint32_t *size, alloc_cb_func_t cb, const void *ctx);
739 extern void kern_pbufpool_free(const kern_pbufpool_t pbufpool, kern_packet_t);
740 extern void kern_pbufpool_free_batch(const kern_pbufpool_t pbufpool,
741     kern_packet_t *array, uint32_t size);
742 extern void kern_pbufpool_free_chain(const kern_pbufpool_t pbufpool,
743     kern_packet_t chain);
744 extern errno_t kern_pbufpool_alloc_buffer(const kern_pbufpool_t pbufpool,
745     mach_vm_address_t *buffer, kern_segment_t *sg, kern_obj_idx_seg_t *sg_idx);
746 extern errno_t kern_pbufpool_alloc_buffer_nosleep(const kern_pbufpool_t
747     pbufpool, mach_vm_address_t *buffer, kern_segment_t *sg,
748     kern_obj_idx_seg_t *sg_idx);
749 extern void kern_pbufpool_free_buffer(const kern_pbufpool_t pbufpool,
750     mach_vm_address_t baddr);
751 extern errno_t kern_pbufpool_alloc_buflet(const kern_pbufpool_t,
752     kern_buflet_t *);
753 extern errno_t kern_pbufpool_alloc_buflet_nosleep(const kern_pbufpool_t,
754     kern_buflet_t *);
755 extern void kern_pbufpool_destroy(kern_pbufpool_t);
756 extern kern_segment_idx_t kern_segment_get_index(const kern_segment_t);
757 __END_DECLS
758 #endif /* KERNEL */
759 #endif /* PRIVATE */
760 #endif /* !_SKYWALK_OS_PACKET_H_ */
761