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_PRIVATE_H_ 30 #define _SKYWALK_OS_PACKET_PRIVATE_H_ 31 32 #if defined(PRIVATE) || defined(BSD_KERNEL_PRIVATE) 33 #include <skywalk/os_packet.h> 34 #include <skywalk/os_nexus_private.h> 35 #include <skywalk/os_channel_private.h> 36 #include <libkern/OSByteOrder.h> 37 #include <netinet/in.h> 38 #include <net/ethernet.h> 39 40 #if defined(BSD_KERNEL_PRIVATE) 41 /* 42 * Flow (currently for kernel, potentially for userland one day). 43 * 44 * XXX: When we expose this to userland, we need to be make sure to NOT 45 * expose kernel pointer/address values embedded within. 46 * 47 * Values in flow_{l2,l3,l4} are stored in network byte order. Pointers 48 * are defined using mach_vm_address_t because it's stable across user 49 * and kernel, and therefore keeps the structure size the same. 50 * 51 * Because this structure might be initialized on a per-packet allocation 52 * basis, it as well as some of its member sub-subtructures are allocated 53 * on a 16-bytes address boundary to allow 128-bit operations on platforms 54 * that support them. 55 * 56 * XXX: when adding new fields, try to leverage __pad ones first. 57 * 58 * TODO: we should consider embedding a flow_key structure here and 59 * use that to store the tuples. That way we can leverage that for 60 * flow lookups without having to copy things back-and-forth. 61 */ 62 struct __flow { 63 union { 64 /* 65 * The following is always zeroed out on each alloc. 66 */ 67 struct __flow_init { 68 /* 69 * Layer 3 70 */ 71 struct __flow_l3 { 72 union { 73 struct __flow_l3_ipv4_addrs { 74 struct in_addr _src; 75 struct in_addr _dst; 76 } _l3_ipv4; 77 struct __flow_l3_ipv6_addrs { 78 struct in6_addr _src; 79 struct in6_addr _dst; 80 } _l3_ipv6; 81 }; 82 uint8_t _l3_ip_ver; 83 uint8_t _l3_proto; 84 uint8_t _l3_hlen; 85 unsigned _l3_is_frag : 1; 86 unsigned _l3_is_first_frag : 1; 87 unsigned _l3_reserved_flags : 6; 88 uint32_t _l3_frag_id; 89 mach_vm_address_t _l3_ptr; 90 } __l3; 91 /* 92 * AQM 93 */ 94 struct __flow_classq { 95 uint32_t _fcq_hash; /* classq-specific hash */ 96 uint32_t _fcq_flags; /* classq-specific flags */ 97 } __classq; 98 /* 99 * Misc. 100 */ 101 uint32_t __ulen; /* user data length */ 102 uint8_t __ulp_encap; /* e.g. IPPROTO_QUIC */ 103 uint8_t __pad[3]; 104 uint64_t __pad64[2]; 105 /* 106 * Flow Source. 107 */ 108 struct __flow_source { 109 union { 110 /* source identifier */ 111 uint64_t _fsrc_id_64[2]; 112 uint32_t _fsrc_id_32[4]; 113 uuid_t _fsrc_id; 114 } __attribute__((aligned(sizeof(uint64_t)))); 115 flowadv_idx_t _fsrc_fidx; /* flow adv. index */ 116 uint8_t _fsrc_type; /* FLOWSRC_* mbuf.h */ 117 uint8_t _fsrc_pad[3]; 118 } __source; 119 /* 120 * Policy. 121 */ 122 struct __flow_policy { 123 uint32_t _fpc_id; /* policy id of pkt sender */ 124 uint32_t _fpc_pad; 125 union { 126 /* process identifier */ 127 uint64_t _fpc_euuid_64[2]; 128 uint32_t _fpc_euuid_32[4]; 129 uuid_t _fpc_euuid; 130 } __attribute__((aligned(sizeof(uint64_t)))); 131 } __policy; 132 } flow_init; 133 uint64_t flow_init_data[16]; 134 } __attribute((aligned(16))); 135 #define flow_l3 flow_init.__l3 136 #define flow_classq flow_init.__classq 137 #define flow_ulen flow_init.__ulen 138 #define flow_ulp_encap flow_init.__ulp_encap 139 #define flow_source flow_init.__source 140 #define flow_policy flow_init.__policy 141 142 #define flow_ipv4_addrs flow_l3._l3_ipv4 143 #define flow_ipv4_src flow_l3._l3_ipv4._src 144 #define flow_ipv4_dst flow_l3._l3_ipv4._dst 145 #define flow_ipv6_addrs flow_l3._l3_ipv6 146 #define flow_ipv6_src flow_l3._l3_ipv6._src 147 #define flow_ipv6_dst flow_l3._l3_ipv6._dst 148 #define flow_ip_ver flow_l3._l3_ip_ver 149 #define flow_ip_proto flow_l3._l3_proto 150 #define flow_ip_hlen flow_l3._l3_hlen 151 #define flow_ip_hdr flow_l3._l3_ptr 152 #define flow_ip_frag_id flow_l3._l3_frag_id 153 #define flow_ip_is_frag flow_l3._l3_is_frag 154 #define flow_ip_is_first_frag flow_l3._l3_is_first_frag 155 156 #define flow_classq_hash flow_classq._fcq_hash 157 #define flow_classq_flags flow_classq._fcq_flags 158 159 #define flow_src_token flow_source._fsrc_id_32[0] 160 #define flow_src_id flow_source._fsrc_id 161 #define flow_src_fidx flow_source._fsrc_fidx 162 #define flow_src_type flow_source._fsrc_type 163 164 #define flow_policy_id flow_policy._fpc_id 165 #define flow_policy_euuid flow_policy._fpc_euuid 166 167 /* 168 * Layer 4. 169 */ 170 union { 171 struct __flow_l4 { 172 union { 173 struct __flow_l4_tcp { 174 in_port_t _src; 175 in_port_t _dst; 176 uint32_t _seq; 177 uint32_t _ack; 178 union { 179 struct { 180 #if BYTE_ORDER == LITTLE_ENDIAN 181 uint8_t _tcp_res:4; 182 uint8_t _off:4; 183 #else /* BYTE_ORDER == BIG_ENDIAN */ 184 uint8_t _off:4; 185 uint8_t _tcp_res:4; 186 #endif /* BYTE_ORDER == BIG_ENDIAN */ 187 uint8_t _flags; 188 uint16_t _win; 189 }; 190 uint32_t _ofw; 191 }; 192 } _l4_tcp; 193 struct __flow_l4_udp { 194 in_port_t _src; 195 in_port_t _dst; 196 uint32_t _ls; 197 } _l4_udp; 198 struct __flow_l4_esp { 199 uint32_t _spi; 200 } _l4_esp; 201 }; 202 uint8_t _l4_hlen; 203 uint8_t _l4_agg_fast; 204 uint8_t _l4_pad[6]; 205 mach_vm_address_t _l4_ptr; 206 } flow_l4; 207 uint64_t flow_l4_data[4]; 208 } __attribute((aligned(sizeof(uint64_t)))); 209 #define flow_tcp flow_l4._l4_tcp 210 #define flow_tcp_src flow_l4._l4_tcp._src 211 #define flow_tcp_dst flow_l4._l4_tcp._dst 212 #define flow_tcp_seq flow_l4._l4_tcp._seq 213 #define flow_tcp_ack flow_l4._l4_tcp._ack 214 #define flow_tcp_off flow_l4._l4_tcp._off 215 #define flow_tcp_flags flow_l4._l4_tcp._flags 216 #define flow_tcp_win flow_l4._l4_tcp._win 217 #define flow_tcp_hlen flow_l4._l4_hlen 218 #define flow_tcp_hdr flow_l4._l4_ptr 219 #define flow_tcp_agg_fast flow_l4._l4_agg_fast 220 #define flow_udp flow_l4._l4_udp 221 #define flow_udp_src flow_l4._l4_udp._src 222 #define flow_udp_dst flow_l4._l4_udp._dst 223 #define flow_udp_hlen flow_l4._l4_hlen 224 #define flow_udp_hdr flow_l4._l4_ptr 225 #define flow_esp_spi flow_l4._l4_esp._spi 226 } __attribute((aligned(16))); 227 #endif /* BSD_KERNEL_PRIVATE */ 228 229 /* 230 * Maximum size of L2, L3 & L4 headers combined. 231 */ 232 #define PKT_MAX_PROTO_HEADER_SIZE 256 233 234 /* based on 2KB buflet size */ 235 #define BUFLETS_MIN 1 /* Ethernet MTU (default) */ 236 #define BUFLETS_9K_JUMBO 5 /* 9000 bytes MTU */ 237 #define BUFLETS_GSO 46 /* 64KB GSO, Ethernet MTU */ 238 239 /* 240 * Common buflet structure shared by {__user,__kern}_buflet. 241 */ 242 struct __buflet { 243 union { 244 /* for skmem batch alloc/free */ 245 uint64_t __buflet_next; 246 /* address of next buflet in chain */ 247 const mach_vm_address_t __nbft_addr; 248 }; 249 /* buffer data address */ 250 const mach_vm_address_t __baddr; 251 /* index of buflet object in the owning buflet region */ 252 const obj_idx_t __bft_idx; 253 /* buffer object index in buffer region */ 254 const obj_idx_t __bidx; 255 /* object index in buflet region of next buflet(for buflet chaining) */ 256 const obj_idx_t __nbft_idx; 257 const uint16_t __dlim; /* maximum length */ 258 uint16_t __dlen; /* length of data in buflet */ 259 uint16_t __doff; /* offset of data in buflet */ 260 const uint16_t __flag; 261 #define BUFLET_FLAG_EXTERNAL 0x0001 262 } __attribute((packed)); 263 264 /* 265 * A buflet represents the smallest buffer fragment representing 266 * part of the packet. The index refers to the position of the buflet 267 * in the pool, and the data length represents the actual payload 268 * size -- not the buflet size itself as it is fixed for all objects 269 * in the pool. 270 */ 271 struct __user_buflet { 272 /* 273 * Common area between user and kernel variants. 274 */ 275 struct __buflet buf_com; 276 #define buf_addr buf_com.__baddr 277 #define buf_nbft_addr buf_com.__nbft_addr 278 #define buf_idx buf_com.__bidx 279 #define buf_nbft_idx buf_com.__nbft_idx 280 #define buf_dlim buf_com.__dlim 281 #define buf_dlen buf_com.__dlen 282 #define buf_doff buf_com.__doff 283 #define buf_flag buf_com.__flag 284 #define buf_bft_idx_reg buf_com.__bft_idx 285 }; 286 287 #define BUF_BADDR(_buf, _addr) \ 288 *__DECONST(mach_vm_address_t *, &(_buf)->buf_addr) = \ 289 (mach_vm_address_t)(_addr) 290 291 #define BUF_BIDX(_buf, _idx) \ 292 *__DECONST(obj_idx_t *, &(_buf)->buf_idx) = (obj_idx_t)(_idx) 293 294 #define BUF_NBFT_ADDR(_buf, _addr) \ 295 *__DECONST(mach_vm_address_t *, &(_buf)->buf_nbft_addr) = \ 296 (mach_vm_address_t)(_addr) 297 298 #define BUF_NBFT_IDX(_buf, _idx) \ 299 *__DECONST(obj_idx_t *, &(_buf)->buf_nbft_idx) = (obj_idx_t)(_idx) 300 301 #define BUF_BFT_IDX_REG(_buf, _idx) \ 302 *__DECONST(obj_idx_t *, &(_buf)->buf_bft_idx_reg) = (_idx) 303 304 #define UBUF_LINK(_pubft, _ubft) do { \ 305 ASSERT((_ubft) != NULL); \ 306 BUF_NBFT_ADDR(_pubft, _ubft); \ 307 BUF_NBFT_IDX(_pubft, (_ubft)->buf_bft_idx_reg); \ 308 } while (0) 309 310 #ifdef KERNEL 311 #define BUF_CTOR(_buf, _baddr, _bidx, _dlim, _dlen, _doff, _nbaddr, _nbidx, _bflag) do { \ 312 _CASSERT(sizeof ((_buf)->buf_addr) == sizeof (mach_vm_address_t)); \ 313 _CASSERT(sizeof ((_buf)->buf_idx) == sizeof (obj_idx_t)); \ 314 _CASSERT(sizeof ((_buf)->buf_dlim) == sizeof (uint16_t)); \ 315 BUF_BADDR(_buf, _baddr); \ 316 BUF_NBFT_ADDR(_buf, _nbaddr); \ 317 BUF_BIDX(_buf, _bidx); \ 318 BUF_NBFT_IDX(_buf, _nbidx); \ 319 *(uint16_t *)(uintptr_t)&(_buf)->buf_dlim = (_dlim); \ 320 (_buf)->buf_dlen = (_dlen); \ 321 (_buf)->buf_doff = (_doff); \ 322 *(uint16_t *)(uintptr_t)&(_buf)->buf_flag = (_bflag); \ 323 } while (0) 324 325 #define BUF_INIT(_buf, _dlen, _doff) do { \ 326 (_buf)->buf_dlen = (_dlen); \ 327 (_buf)->buf_doff = (_doff); \ 328 } while (0) 329 330 #endif /* KERNEL */ 331 332 #ifdef KERNEL 333 #define BUF_IN_RANGE(_buf) \ 334 ((_buf)->buf_addr >= (mach_vm_address_t)(_buf)->buf_objaddr && \ 335 ((uintptr_t)(_buf)->buf_addr + (_buf)->buf_dlim) <= \ 336 ((uintptr_t)(_buf)->buf_objaddr + (_buf)->buf_objlim) && \ 337 ((_buf)->buf_doff + (_buf)->buf_dlen) <= (_buf)->buf_dlim) 338 #else /* !KERNEL */ 339 #define BUF_IN_RANGE(_buf) \ 340 (((_buf)->buf_doff + (_buf)->buf_dlen) <= (_buf)->buf_dlim) 341 #endif /* !KERNEL */ 342 343 /* 344 * Metadata preamble. This structure is placed at begining of each 345 * __{user,kern}_{quantum,packet} object. Each user metadata object has a 346 * unique red zone pattern, which is an XOR of the redzone cookie and 347 * offset of the metadata object in the object's region. Due to the use 348 * of tagged pointer, we need the structure size to be multiples of 16. 349 * See SK_PTR_TAG() definition for details. 350 */ 351 struct __metadata_preamble { 352 union { 353 uint64_t _mdp_next; /* for batch alloc/free (K) */ 354 uint64_t mdp_redzone; /* red zone cookie (U) */ 355 }; 356 const obj_idx_t mdp_idx; /* index within region (UK) */ 357 uint16_t mdp_type; /* nexus_meta_type_t (UK) */ 358 uint16_t mdp_subtype; /* nexus_meta_subtype_t (UK) */ 359 }; 360 361 #define METADATA_PREAMBLE_SZ (sizeof (struct __metadata_preamble)) 362 363 #define METADATA_PREAMBLE(_md) \ 364 ((struct __metadata_preamble *) \ 365 ((mach_vm_address_t)(_md) - METADATA_PREAMBLE_SZ)) 366 367 #define METADATA_IDX(_md) \ 368 (METADATA_PREAMBLE(_md)->mdp_idx) 369 370 #define METADATA_TYPE(_md) \ 371 (METADATA_PREAMBLE(_md)->mdp_type) 372 373 #define METADATA_SUBTYPE(_md) \ 374 (METADATA_PREAMBLE(_md)->mdp_subtype) 375 376 /* 377 * Common packet structure shared by {__user,__kern}_quantum. 378 */ 379 struct __quantum { 380 union { 381 uuid_t __uuid; /* flow UUID */ 382 uint8_t __val8[16]; 383 uint16_t __val16[8]; 384 uint32_t __val32[4]; 385 uint64_t __val64[2]; 386 } __flow_id_u; 387 #define __q_flow_id __flow_id_u.__uuid 388 #define __q_flow_id_val8 __flow_id_u.__val8 389 #define __q_flow_id_val16 __flow_id_u.__val16 390 #define __q_flow_id_val32 __flow_id_u.__val32 391 #define __q_flow_id_val64 __flow_id_u.__val64 392 393 uint32_t __q_len; 394 395 /* QoS service class, see packet_svc_class_t */ 396 uint32_t __q_svc_class; /* PKT_SC_* values */ 397 398 /* 399 * See notes on _QUM_{INTERNALIZE,EXTERNALIZE}() regarding 400 * portion of this structure above __flags that gets copied. 401 * Adding more user-mutable fields after __flags would also 402 * require adjusting those macros as well. 403 */ 404 volatile uint16_t __q_flags; /* QUMF_* flags */ 405 uint16_t __q_pad[3]; 406 } __attribute((aligned(sizeof(uint64_t)))); 407 408 /* 409 * Quantum. 410 * 411 * This structure is aligned for efficient copy and accesses. 412 * It is the user version of the __kernel_quantum structure. 413 * 414 * XXX: Do NOT store kernel pointer/address values here. 415 */ 416 struct __user_quantum { 417 /* 418 * Common area between user and kernel variants. 419 */ 420 struct __quantum qum_com; 421 #define qum_flow_id qum_com.__q_flow_id 422 #define qum_flow_id_val8 qum_com.__q_flow_id_val8 423 #define qum_flow_id_val16 qum_com.__q_flow_id_val16 424 #define qum_flow_id_val32 qum_com.__q_flow_id_val32 425 #define qum_flow_id_val64 qum_com.__q_flow_id_val64 426 #define qum_len qum_com.__q_len 427 #define qum_qflags qum_com.__q_flags 428 #define qum_svc_class qum_com.__q_svc_class 429 430 /* 431 * Userland specific. 432 */ 433 struct __user_buflet qum_buf[1]; /* 1 buflet */ 434 /* 435 * use count for packet. 436 */ 437 uint16_t qum_usecnt; 438 } __attribute((aligned(sizeof(uint64_t)))); 439 440 /* 441 * Valid values for (16-bit) qum_qflags. 442 */ 443 #define QUM_F_FINALIZED 0x0001 /* has been finalized */ 444 #define QUM_F_DROPPED 0x0002 /* has been dropped */ 445 #define QUM_F_FLOW_CLASSIFIED 0x0010 /* flow has been classified */ 446 #ifdef KERNEL 447 #define QUM_F_INTERNALIZED 0x1000 /* has been internalized */ 448 #define QUM_F_KERNEL_ONLY 0x8000 /* kernel only; no user counterpart */ 449 450 /* invariant flags we want to keep */ 451 #define QUM_F_SAVE_MASK (QUM_F_KERNEL_ONLY) 452 /* kernel-only flags that's never externalized */ 453 #define QUM_F_KERNEL_FLAGS (QUM_F_INTERNALIZED|QUM_F_KERNEL_ONLY) 454 #endif /* KERNEL */ 455 456 #ifdef KERNEL 457 #define _KQUM_CTOR(_kqum, _flags, _len, _baddr, _bidx, _dlim, _qidx) do { \ 458 (_kqum)->qum_flow_id_val64[0] = 0; \ 459 (_kqum)->qum_flow_id_val64[1] = 0; \ 460 (_kqum)->qum_qflags = (_flags); \ 461 (_kqum)->qum_len = (_len); \ 462 _CASSERT(sizeof(METADATA_IDX(_kqum)) == sizeof(obj_idx_t)); \ 463 *(obj_idx_t *)(uintptr_t)&METADATA_IDX(_kqum) = (_qidx); \ 464 BUF_CTOR(&(_kqum)->qum_buf[0], (_baddr), (_bidx), (_dlim), 0, 0, 0, \ 465 OBJ_IDX_NONE, 0); \ 466 } while (0) 467 468 #define _KQUM_INIT(_kqum, _flags, _len, _qidx) do { \ 469 (_kqum)->qum_flow_id_val64[0] = 0; \ 470 (_kqum)->qum_flow_id_val64[1] = 0; \ 471 (_kqum)->qum_qflags = (_flags); \ 472 (_kqum)->qum_len = (_len); \ 473 BUF_INIT(&(_kqum)->qum_buf[0], 0, 0); \ 474 } while (0) 475 #endif /* KERNEL */ 476 477 /* 478 * Common packet structure shared by {__user,__kern}_packet. 479 */ 480 struct __packet_com { 481 /* Link layer (offset relevant to first buflet) */ 482 uint16_t __link_flags; /* PKT_LINKF_* flags */ 483 484 /* 485 * Headroom/protocol header length 486 * 487 * Since the security model of Skywalk nexus is that we doesn't trust 488 * packets either from above (userspace) or below (driver/firmware), 489 * the only metadata field that nexus makes use of from external is the 490 * headroom. Based on headroom, the flowswitch starts demux routine on 491 * l2 header, if any. The l2_len is stored in this step. Then the flow 492 * extraction (l3+l4 flow) begins parsing from (headroom + l2_len). 493 * 494 * __headroom is the empty buffer space before any packet data, 495 * it is also the equivalent to the first header offset. 496 * 497 * __l2_len is l2 (link layer) protocol header length, if any. 498 */ 499 uint8_t __headroom; 500 uint8_t __l2_len; 501 502 /* 503 * Checksum offload. 504 * 505 * Partial checksum does not require any header parsing and is 506 * therefore simpler to implement both in software and hardware. 507 * 508 * On transmit, PKT_CSUMF_PARTIAL indicates that a partial one's 509 * complement checksum to be computed on the span starting from 510 * pkt_csum_tx_start_off to the end of the packet, and have the 511 * resulted checksum value written at the location specified by 512 * pkt_csum_tx_stuff_off. 513 * 514 * The PKT_CSUMF_ZERO_INVERT flag is used on transmit to indicate 515 * that the value 0xffff (negative 0 in one's complement) must be 516 * substituted for the value of 0. This is set for UDP packets, 517 * since otherwise the receiver may not validate the checksum 518 * (UDP/IPv4), or drop the packet altogether (UDP/IPv6). 519 * 520 * On receive, PKT_CSUMF_PARTIAL indicates that a partial one's 521 * complement checksum has been computed on the span beginning at 522 * pkt_csum_rx_start_off to the end of the packet, and that the 523 * computed value is now stored in pkt_csum_rx_value. 524 * 525 * All offsets are relative to the base of the first buflet. 526 */ 527 uint32_t __csum_flags; /* PKT_CSUMF_* flags */ 528 union { 529 struct { 530 uint16_t __csum_start_off; /* start offset */ 531 uint16_t __csum_value; /* checksum value */ 532 } __csum_rx; 533 struct { 534 uint16_t __csum_start_off; /* start offset */ 535 uint16_t __csum_stuff_off; /* stuff offset */ 536 } __csum_tx; 537 uint32_t __csum_data; 538 }; 539 540 /* Compression generation count */ 541 uint32_t __comp_gencnt; 542 543 /* 544 * Trace ID for each sampled packet. 545 * Non-zero ID indicates that the packet is being actively traced. 546 */ 547 packet_trace_id_t __trace_id; 548 549 /* Aggregation type */ 550 uint8_t __aggr_type; /* PKT_AGGR_* values */ 551 uint8_t __seg_cnt; /* Number of LRO-packets */ 552 553 uint8_t __padding[2]; 554 555 /* 556 * See notes on _PKT_{INTERNALIZE,EXTERNALIZE}() regarding portion 557 * of this structure above __p_flags that gets copied. Adding 558 * more user-mutable fields after __p_flags would also require 559 * adjusting those macros as well. 560 */ 561 union { 562 volatile uint32_t __flags32[2]; 563 volatile uint64_t __flags; /* PKT_F_* flags */ 564 }; 565 } __attribute((aligned(sizeof(uint64_t)))); 566 567 struct __packet { 568 union { 569 uint64_t __pkt_data[4]; 570 struct __packet_com __pkt_com; 571 }; 572 #define __p_link_flags __pkt_com.__link_flags 573 #define __p_headroom __pkt_com.__headroom 574 #define __p_l2_len __pkt_com.__l2_len 575 #define __p_csum_flags __pkt_com.__csum_flags 576 #define __p_csum_rx __pkt_com.__csum_rx 577 #define __p_csum_tx __pkt_com.__csum_tx 578 #define __p_csum_data __pkt_com.__csum_data 579 #define __p_comp_gencnt __pkt_com.__comp_gencnt 580 #define __p_aggr_type __pkt_com.__aggr_type 581 #define __p_seg_cnt __pkt_com.__seg_cnt 582 #define __p_trace_id __pkt_com.__trace_id 583 #define __p_flags32 __pkt_com.__flags32 584 #define __p_flags __pkt_com.__flags 585 }; 586 587 /* optional packet token types */ 588 #define PKT_OPT_TOKEN_TYPE_OPAQUE 1 /* token has opaque data */ 589 #define PKT_OPT_TOKEN_TYPE_PACKET_ID 2 /* token has packet_id */ 590 591 /* maximum token size */ 592 #define PKT_OPT_MAX_TOKEN_SIZE 16 593 594 struct __packet_opt_com { 595 uint32_t __token_type; 596 uint16_t __token_len; 597 uint16_t __vlan_tag; 598 union { 599 uint64_t __token_data[2]; 600 uint8_t __token[PKT_OPT_MAX_TOKEN_SIZE]; 601 }; 602 uint64_t __expire_ts; 603 } __attribute((aligned(sizeof(uint64_t)))); 604 605 struct __packet_opt { 606 union { 607 uint64_t __pkt_opt_data[4]; 608 struct __packet_opt_com __pkt_opt_com; 609 }; 610 #define __po_token_type __pkt_opt_com.__token_type 611 #define __po_token_len __pkt_opt_com.__token_len 612 #define __po_vlan_tag __pkt_opt_com.__vlan_tag 613 #define __po_token_data __pkt_opt_com.__token_data 614 #define __po_token __pkt_opt_com.__token 615 #define __po_expire_ts __pkt_opt_com.__expire_ts 616 }; 617 618 /* 619 * Packet. 620 * 621 * This structure is aligned for efficient copy and accesses. 622 * It is the user version of the __kern_packet structure. 623 * 624 * XXX: Do NOT store kernel pointer/address values here. 625 */ 626 struct __user_packet { 627 struct __user_quantum pkt_qum; 628 #define pkt_flow_id pkt_qum.qum_flow_id 629 #define pkt_flow_id_64 pkt_qum.qum_flow_id_val64 630 #define pkt_qum_qflags pkt_qum.qum_qflags 631 #define pkt_length pkt_qum.qum_len 632 #define pkt_qum_buf pkt_qum.qum_buf[0] 633 #define pkt_svc_class pkt_qum.qum_svc_class 634 #ifdef KERNEL 635 /* 636 * We are using the first 4 bytes of flow_id as the AQM flow identifier. 637 * flow identifier should be a psuedo random number. flow_id should be 638 * generated using uuid_generate() where the first 4 bytes are random. 639 */ 640 #define pkt_flow_token pkt_qum.qum_flow_id_val32[0] 641 #endif /* KERNEL */ 642 643 /* 644 * Common area between user and kernel variants. 645 */ 646 struct __packet pkt_com; 647 #define pkt_link_flags pkt_com.__p_link_flags 648 #define pkt_headroom pkt_com.__p_headroom 649 #define pkt_l2_len pkt_com.__p_l2_len 650 #define pkt_csum_flags pkt_com.__p_csum_flags 651 #define pkt_csum_rx_start_off pkt_com.__p_csum_rx.__csum_start_off 652 #define pkt_csum_rx_value pkt_com.__p_csum_rx.__csum_value 653 #define pkt_csum_tx_start_off pkt_com.__p_csum_tx.__csum_start_off 654 #define pkt_csum_tx_stuff_off pkt_com.__p_csum_tx.__csum_stuff_off 655 #define pkt_csum_data pkt_com.__p_csum_data 656 #define pkt_comp_gencnt pkt_com.__p_comp_gencnt 657 #define pkt_aggr_type pkt_com.__p_aggr_type 658 #define pkt_seg_cnt pkt_com.__p_seg_cnt 659 #define pkt_trace_id pkt_com.__p_trace_id 660 #if BYTE_ORDER == LITTLE_ENDIAN 661 #define pkt_pflags32 pkt_com.__p_flags32[0] 662 #else /* BYTE_ORDER != LITTLE_ENDIAN */ 663 #define pkt_pflags32 pkt_com.__p_flags32[1] 664 #endif /* BYTE_ORDER != LITTLE_ENDIAN */ 665 #define pkt_pflags pkt_com.__p_flags 666 667 /* 668 * Optional common metadata. 669 */ 670 struct __packet_opt pkt_com_opt; 671 672 /* 673 * Userland specific. 674 */ 675 676 /* 677 * pkt_{bufs,max} aren't part of the common area, on purpose, 678 * since we selectively update them on internalize/externalize. 679 */ 680 const uint16_t pkt_bufs_max; /* maximum size of buflet chain */ 681 const uint16_t pkt_bufs_cnt; /* buflet chain size */ 682 } __attribute((aligned(sizeof(uint64_t)))); 683 684 /* the size of __user_packet structure for n total buflets */ 685 #define _USER_PACKET_SIZE(n) sizeof(struct __user_packet) 686 687 /* 688 * Valid values for pkt_link_flags. 689 */ 690 #define PKT_LINKF_BCAST 0x0001 /* send/received as link-level bcast */ 691 #define PKT_LINKF_MCAST 0x0002 /* send/received as link-level mcast */ 692 #define PKT_LINKF_ETHFCS 0x0004 /* has Ethernet FCS */ 693 694 /* 695 * XXX IMPORTANT - READ THIS XXX 696 * 697 * Valid values for (64-bit) pkt_pflags. 698 * 699 * The lower 32-bit values are equivalent to PKTF_* flags used by mbufs, 700 * hence the unused values are reserved. Do not use define any of these 701 * values unless they correspond to PKTF_* flags. Make sure to do the 702 * following when adding a value in the lower 32-bit range: 703 * 704 * a. If the flag is kernel-only, prefix it with 2 underscore characters, 705 * then add a PKT_F_* alias under the KERNEL block conditional. This 706 * will help ensure that the libsyscall code doesn't mistakenly use it. 707 * 708 * b. In pp_init(), add compile-time assertion to ensure that the PKT_F_* 709 * value matches the corresponding PKTF_* as defined in <sys/mbuf.h>. 710 * 711 * c. Add the new flag to PKT_F_USER_MASK depending on whether it's allowed 712 * to be user by userland. Flags not added to this mask will only be 713 * used by the kernel. We only internalize and externalize flags listed 714 * in PKT_F_USER_MASK. 715 * 716 * d. Add the new flag to PKT_F_COMMON_MASK. 717 * 718 * When adding an upper 32-bit value, ensure (a) and (c) above are done. 719 * 720 * Legend: 721 * 722 * (K) - Kernel-only 723 * (U+K) - User and kernel 724 * (reserved) - Only to be used for mapping with mbuf PKTF_* flags 725 */ 726 #define __PKT_F_FLOW_ID 0x0000000000000001ULL /* (K) */ 727 #define __PKT_F_FLOW_ADV 0x0000000000000002ULL /* (K) */ 728 /* 0x0000000000000004ULL (reserved) */ 729 /* 0x0000000000000008ULL (reserved) */ 730 /* 0x0000000000000010ULL (reserved) */ 731 /* 0x0000000000000020ULL (reserved) */ 732 /* 0x0000000000000040ULL (reserved) */ 733 /* 0x0000000000000080ULL (reserved) */ 734 /* 0x0000000000000100ULL (reserved) */ 735 /* 0x0000000000000200ULL (reserved) */ 736 #define PKT_F_WAKE_PKT 0x0000000000000400ULL /* (U+K) */ 737 /* 0x0000000000000800ULL (reserved) */ 738 /* 0x0000000000001000ULL (reserved) */ 739 /* 0x0000000000002000ULL (reserved) */ 740 /* 0x0000000000004000ULL (reserved) */ 741 #define PKT_F_BACKGROUND 0x0000000000008000ULL /* (U+K) */ 742 /* 0x0000000000010000ULL (reserved) */ 743 /* 0x0000000000020000ULL (reserved) */ 744 #define PKT_F_KEEPALIVE 0x0000000000040000ULL /* (U+K) */ 745 #define PKT_F_REALTIME 0x0000000000080000ULL /* (U+K) */ 746 /* 0x0000000000100000ULL (reserved) */ 747 #define PKT_F_REXMT 0x0000000000200000ULL /* (U+K) */ 748 /* 0x0000000000400000ULL (reserved) */ 749 #define __PKT_F_TX_COMPL_TS_REQ 0x0000000000800000ULL /* (K) */ 750 #define __PKT_F_TS_VALID 0x0000000001000000ULL /* (K) */ 751 /* 0x0000000002000000ULL (reserved) */ 752 #define __PKT_F_NEW_FLOW 0x0000000004000000ULL /* (K) */ 753 #define __PKT_F_START_SEQ 0x0000000008000000ULL /* (K) */ 754 #define PKT_F_LAST_PKT 0x0000000010000000ULL /* (U+K) */ 755 /* 0x0000000020000000ULL (reserved) */ 756 /* 0x0000000040000000ULL (reserved) */ 757 /* 0x0000000080000000ULL (reserved) */ 758 /* --------------------- upper 32-bit below */ 759 #define PKT_F_OPT_GROUP_START 0x0000000100000000ULL /* (U+K) */ 760 #define PKT_F_OPT_GROUP_END 0x0000000200000000ULL /* (U+K) */ 761 #define PKT_F_OPT_EXPIRE_TS 0x0000000400000000ULL /* (U+K) */ 762 #define PKT_F_OPT_TOKEN 0x0000000800000000ULL /* (U+K) */ 763 #define __PKT_F_FLOW_DATA 0x0000001000000000ULL /* (K) */ 764 #define __PKT_F_TX_COMPL_DATA 0x0000002000000000ULL /* (K) */ 765 #define __PKT_F_MBUF_DATA 0x0000004000000000ULL /* (K) */ 766 #define PKT_F_TRUNCATED 0x0000008000000000ULL /* (U+K) */ 767 #define __PKT_F_PKT_DATA 0x0000010000000000ULL /* (K) */ 768 #define PKT_F_PROMISC 0x0000020000000000ULL /* (U+K) */ 769 #define PKT_F_OPT_VLTAG 0x0000040000000000ULL /* (U+K) */ 770 #define PKT_F_OPT_VLTAG_IN_PKT 0x0000080000000000ULL /* (U+K) */ 771 /* 0x0000100000000000ULL */ 772 /* 0x0000200000000000ULL */ 773 /* 0x0000400000000000ULL */ 774 /* 0x0000800000000000ULL */ 775 /* 0x0001000000000000ULL */ 776 /* 0x0002000000000000ULL */ 777 /* 0x0004000000000000ULL */ 778 /* 0x0008000000000000ULL */ 779 /* 0x0010000000000000ULL */ 780 /* 0x0020000000000000ULL */ 781 /* 0x0040000000000000ULL */ 782 /* 0x0080000000000000ULL */ 783 #define __PKT_F_OPT_ALLOC 0x0100000000000000ULL /* (K) */ 784 #define __PKT_F_FLOW_ALLOC 0x0200000000000000ULL /* (K) */ 785 #define __PKT_F_TX_COMPL_ALLOC 0x0400000000000000ULL /* (K) */ 786 /* 0x0800000000000000ULL */ 787 /* 0x1000000000000000ULL */ 788 /* 0x2000000000000000ULL */ 789 /* 0x4000000000000000ULL */ 790 /* 0x8000000000000000ULL */ 791 792 /* 793 * Packet option flags. 794 */ 795 #define PKT_F_OPT_DATA \ 796 (PKT_F_OPT_GROUP_START | PKT_F_OPT_GROUP_END | \ 797 PKT_F_OPT_EXPIRE_TS | PKT_F_OPT_TOKEN | \ 798 PKT_F_OPT_VLTAG | PKT_F_OPT_VLTAG_IN_PKT) 799 800 #ifdef KERNEL 801 /* 802 * Flags exposed to user (and kernel). See notes above. 803 */ 804 #define PKT_F_USER_MASK \ 805 (PKT_F_BACKGROUND | PKT_F_REALTIME | PKT_F_REXMT | \ 806 PKT_F_LAST_PKT | PKT_F_OPT_DATA | PKT_F_PROMISC | \ 807 PKT_F_TRUNCATED | PKT_F_WAKE_PKT) 808 809 /* 810 * Aliases for kernel-only flags. See notes above. The ones marked 811 * with (common) have corresponding PKTF_* definitions and are also 812 * included in PKF_F_COMMON_MASK below. 813 */ 814 #define PKT_F_FLOW_ID __PKT_F_FLOW_ID /* (common) */ 815 #define PKT_F_FLOW_ADV __PKT_F_FLOW_ADV /* (common) */ 816 #define PKT_F_TX_COMPL_TS_REQ __PKT_F_TX_COMPL_TS_REQ /* (common) */ 817 #define PKT_F_TS_VALID __PKT_F_TS_VALID /* (common) */ 818 #define PKT_F_NEW_FLOW __PKT_F_NEW_FLOW /* (common) */ 819 #define PKT_F_START_SEQ __PKT_F_START_SEQ /* (common) */ 820 #define PKT_F_FLOW_DATA __PKT_F_FLOW_DATA 821 #define PKT_F_TX_COMPL_DATA __PKT_F_TX_COMPL_DATA 822 #define PKT_F_MBUF_DATA __PKT_F_MBUF_DATA 823 #define PKT_F_PKT_DATA __PKT_F_PKT_DATA 824 #define PKT_F_OPT_ALLOC __PKT_F_OPT_ALLOC 825 #define PKT_F_FLOW_ALLOC __PKT_F_FLOW_ALLOC 826 #define PKT_F_TX_COMPL_ALLOC __PKT_F_TX_COMPL_ALLOC 827 828 /* 829 * Flags related to mbuf attached to the packet. 830 */ 831 #define PKT_F_MBUF_MASK (PKT_F_MBUF_DATA | PKT_F_TRUNCATED) 832 833 /* 834 * Flags related to packet attached to the packet. 835 */ 836 #define PKT_F_PKT_MASK (PKT_F_PKT_DATA | PKT_F_TRUNCATED) 837 838 /* 839 * Invariant flags kept during _PKT_COPY(). At the moment we keep 840 * all except those related to the attached mbuf. 841 */ 842 #define PKT_F_COPY_MASK (~(PKT_F_MBUF_MASK | PKT_F_PKT_MASK)) 843 844 /* 845 * Lower 32-bit flags common to mbuf and __kern_packet. See notes above. 846 * DO NOT add flags to this mask unless they have equivalent PKTF_* flags 847 * defined in <sys/mbuf.h> 848 */ 849 #define PKT_F_COMMON_MASK \ 850 (PKT_F_BACKGROUND | PKT_F_REALTIME | PKT_F_REXMT | \ 851 PKT_F_LAST_PKT | PKT_F_FLOW_ID | PKT_F_FLOW_ADV | \ 852 PKT_F_TX_COMPL_TS_REQ | PKT_F_TS_VALID | PKT_F_NEW_FLOW | \ 853 PKT_F_START_SEQ | PKT_F_KEEPALIVE | PKT_F_WAKE_PKT) 854 855 /* 856 * Flags retained across alloc/free. 857 */ 858 #define PKT_F_INIT_MASK \ 859 (PKT_F_OPT_ALLOC | PKT_F_FLOW_ALLOC | PKT_F_TX_COMPL_ALLOC) 860 #endif /* KERNEL */ 861 862 /* 863 * 64-bit tagged pointer (limit tag to least significant byte). 864 * We use 2 bits to encode type, and another 2 bits for subtype. 865 */ 866 #define SK_PTR_TYPE_MASK ((uint64_t)0x3) /* 00 11 */ 867 #define SK_PTR_SUBTYPE_MASK ((uint64_t)0xc) /* 11 00 */ 868 #define SK_PTR_TAG_MASK ((uint64_t)0xf) /* 11 11 */ 869 870 #define SK_PTR_TAG(_p) ((uint64_t)(_p) & SK_PTR_TAG_MASK) 871 #define SK_PTR_ADDR_MASK (~SK_PTR_TAG_MASK) 872 873 #define SK_PTR_TYPE(_p) ((uint64_t)(_p) & SK_PTR_TYPE_MASK) 874 #define SK_PTR_TYPE_ENC(_t) ((uint64_t)(_t) & SK_PTR_TYPE_MASK) 875 876 #define SK_PTR_SUBTYPE(_p) (((uint64_t)(_p) & SK_PTR_SUBTYPE_MASK) >> 2) 877 #define SK_PTR_SUBTYPE_ENC(_s) (((uint64_t)(_s) << 2) & SK_PTR_SUBTYPE_MASK) 878 879 #define SK_PTR_ADDR(_p) ((uint64_t)(_p) & SK_PTR_ADDR_MASK) 880 #define SK_PTR_ADDR_ENC(_p) ((uint64_t)(_p) & SK_PTR_ADDR_MASK) 881 882 #define SK_PTR_ENCODE(_p, _t, _s) \ 883 (SK_PTR_ADDR_ENC(_p) | SK_PTR_TYPE_ENC(_t) | SK_PTR_SUBTYPE_ENC(_s)) 884 885 #define SK_PTR_ADDR_UQUM(_ph) ((struct __user_quantum *)SK_PTR_ADDR(_ph)) 886 #define SK_PTR_ADDR_UPKT(_ph) ((struct __user_packet *)SK_PTR_ADDR(_ph)) 887 888 #ifdef KERNEL 889 __BEGIN_DECLS 890 /* 891 * Packets. 892 */ 893 extern struct mbuf *kern_packet_get_mbuf(const kern_packet_t); 894 __END_DECLS 895 #else /* !KERNEL */ 896 #if defined(LIBSYSCALL_INTERFACE) 897 __BEGIN_DECLS 898 extern void pkt_subtype_assert_fail(const packet_t, uint64_t, uint64_t); 899 extern void pkt_type_assert_fail(const packet_t, uint64_t); 900 __END_DECLS 901 #endif /* LIBSYSCALL_INTERFACE */ 902 #endif /* !KERNEL */ 903 #if defined(LIBSYSCALL_INTERFACE) || defined(BSD_KERNEL_PRIVATE) 904 #include <skywalk/packet_common.h> 905 #endif /* LIBSYSCALL_INTERFACE || BSD_KERNEL_PRIVATE */ 906 #endif /* PRIVATE || BSD_KERNEL_PRIVATE */ 907 #endif /* !_SKYWALK_OS_PACKET_PRIVATE_H_ */ 908