From af007ec1ff51269cd6135c3e6e2fa5c8b2c02039 Mon Sep 17 00:00:00 2001 From: Jens Goldberg Date: Mon, 19 Aug 2024 15:01:58 +0200 Subject: [PATCH 1/4] std.os.linux: Add support for AF_PACKET V3 --- lib/std/os/linux.zig | 133 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 54ad53fe06..9ccbb198b4 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -6816,6 +6816,139 @@ pub const ifreq = extern struct { }, }; +pub const PACKET = struct { + pub const HOST = 0; + pub const BROADCAST = 1; + pub const MULTICAST = 2; + pub const OTHERHOST = 3; + pub const OUTGOING = 4; + pub const LOOPBACK = 5; + pub const USER = 6; + pub const KERNEL = 7; + pub const FASTROUTE = 8; + + pub const ADD_MEMBERSHIP = 1; + pub const DROP_MEMBERSHIP = 2; + pub const RECV_OUTPUT = 3; + pub const RX_RING = 5; + pub const STATISTICS = 6; + pub const COPY_THRESH = 7; + pub const AUXDATA = 8; + pub const ORIGDEV = 9; + pub const VERSION = 10; + pub const HDRLEN = 11; + pub const RESERVE = 12; + pub const TX_RING = 13; + pub const LOSS = 14; + pub const VNET_HDR = 15; + pub const TX_TIMESTAMP = 16; + pub const TIMESTAMP = 17; + pub const FANOUT = 18; + pub const TX_HAS_OFF = 19; + pub const QDISC_BYPASS = 20; + pub const ROLLOVER_STATS = 21; + pub const FANOUT_DATA = 22; + pub const IGNORE_OUTGOING = 23; + pub const VNET_HDR_SZ = 24; + + pub const FANOUT_HASH = 0; + pub const FANOUT_LB = 1; + pub const FANOUT_CPU = 2; + pub const FANOUT_ROLLOVER = 3; + pub const FANOUT_RND = 4; + pub const FANOUT_QM = 5; + pub const FANOUT_CBPF = 6; + pub const FANOUT_EBPF = 7; + pub const FANOUT_FLAG_ROLLOVER = 0x1000; + pub const FANOUT_FLAG_UNIQUEID = 0x2000; + pub const FANOUT_FLAG_IGNORE_OUTGOING = 0x4000; + pub const FANOUT_FLAG_DEFRAG = 0x8000; +}; + +pub const tpacket_versions = enum(u32) { + V1 = 0, + V2 = 1, + V3 = 2, +}; + +pub const tpacket_req3 = extern struct { + block_size: c_uint, // Minimal size of contiguous block + block_nr: c_uint, // Number of blocks + frame_size: c_uint, // Size of frame + frame_nr: c_uint, // Total number of frames + retire_blk_tov: c_uint, // Timeout in msecs + sizeof_priv: c_uint, // Offset to private data area + feature_req_word: c_uint, +}; + +pub const tpacket_bd_ts = extern struct { + sec: c_uint, + frac: extern union { + usec: c_uint, + nsec: c_uint, + }, +}; + +pub const TP_STATUS = extern union { + rx: packed struct(u32) { + USER: bool, + COPY: bool, + LOSING: bool, + CSUMNOTREADY: bool, + VLAN_VALID: bool, + BLK_TMO: bool, + VLAN_TPID_VALID: bool, + CSUM_VALID: bool, + GSO_TCP: bool, + _: u20, + TS_SOFTWARE: bool, + TS_SYS_HARDWARE: bool, + TS_RAW_HARDWARE: bool, + }, + tx: packed struct(u32) { + SEND_REQUEST: bool, + SENDING: bool, + WRONG_FORMAT: bool, + _: u26, + TS_SOFTWARE: bool, + TS_SYS_HARDWARE: bool, + TS_RAW_HARDWARE: bool, + }, +}; + +pub const tpacket_block_desc = extern struct { + version: u32, + offset_to_priv: u32, + block_status: TP_STATUS, + num_pkts: u32, + offset_to_first_pkt: u32, + blk_len: u32, + seq_num: u64 align(8), + ts_first_pkt: tpacket_bd_ts, + ts_last_pkt: tpacket_bd_ts, +}; + +pub const tpacket3_hdr = extern struct { + next_offset: u32, + sec: u32, + nsec: u32, + snaplen: u32, + len: u32, + status: u32, + mac: u16, + net: u16, + rxhash: u32, + vlan_tci: u32, + vlan_tpid: u16, + padding: [10]u8, +}; + +pub const tpacket_stats_v3 = extern struct { + packets: c_uint, + drops: c_uint, + freeze_q_cnt: c_uint, +}; + // doc comments copied from musl pub const rlimit_resource = if (native_arch.isMIPS() or native_arch.isSPARC()) arch_bits.rlimit_resource From 1950e52c6cbc8321bdc986278bcb1cc510552b15 Mon Sep 17 00:00:00 2001 From: Jens Goldberg Date: Mon, 19 Aug 2024 16:19:05 +0200 Subject: [PATCH 2/4] Remove FASTROUTE; invisible to userspace, and collides with USER --- lib/std/os/linux.zig | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 9ccbb198b4..24ff2bda02 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -6825,7 +6825,6 @@ pub const PACKET = struct { pub const LOOPBACK = 5; pub const USER = 6; pub const KERNEL = 7; - pub const FASTROUTE = 8; pub const ADD_MEMBERSHIP = 1; pub const DROP_MEMBERSHIP = 2; From 6f24c4485ca76c7e8b1b573addd7effb2c7a9bf4 Mon Sep 17 00:00:00 2001 From: Jens Goldberg Date: Tue, 20 Aug 2024 10:10:14 +0200 Subject: [PATCH 3/4] Add tpacket_hdr and tpacket_block variant unions --- lib/std/os/linux.zig | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 24ff2bda02..fb295a4934 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -6915,9 +6915,7 @@ pub const TP_STATUS = extern union { }, }; -pub const tpacket_block_desc = extern struct { - version: u32, - offset_to_priv: u32, +pub const tpacket_hdr_v1 = extern struct { block_status: TP_STATUS, num_pkts: u32, offset_to_first_pkt: u32, @@ -6927,6 +6925,27 @@ pub const tpacket_block_desc = extern struct { ts_last_pkt: tpacket_bd_ts, }; +pub const tpacket_bd_header_u = extern union { + bh1: tpacket_hdr_v1, +}; + +pub const tpacket_block_desc = extern struct { + version: u32, + offset_to_priv: u32, + hdr: tpacket_bd_header_u, +}; + +pub const tpacket_hdr_variant1 = extern struct { + rxhash: u32, + vlan_tci: u32, + vlan_tpid: u16, + padding: u16, +}; + +pub const tpacket3_hdr_variants = extern union { + hv1: tpacket_hdr_variant1, +}; + pub const tpacket3_hdr = extern struct { next_offset: u32, sec: u32, @@ -6936,10 +6955,8 @@ pub const tpacket3_hdr = extern struct { status: u32, mac: u16, net: u16, - rxhash: u32, - vlan_tci: u32, - vlan_tpid: u16, - padding: [10]u8, + variant: tpacket3_hdr_variants, + padding: [8]u8, }; pub const tpacket_stats_v3 = extern struct { From c3214bcd966b6cb245e937a8599cb6f5b54eeef7 Mon Sep 17 00:00:00 2001 From: Jens Goldberg Date: Wed, 21 Aug 2024 10:10:19 +0200 Subject: [PATCH 4/4] Inline the variant union --- lib/std/os/linux.zig | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index fb295a4934..da05efc323 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -6942,10 +6942,6 @@ pub const tpacket_hdr_variant1 = extern struct { padding: u16, }; -pub const tpacket3_hdr_variants = extern union { - hv1: tpacket_hdr_variant1, -}; - pub const tpacket3_hdr = extern struct { next_offset: u32, sec: u32, @@ -6955,7 +6951,9 @@ pub const tpacket3_hdr = extern struct { status: u32, mac: u16, net: u16, - variant: tpacket3_hdr_variants, + variant: extern union { + hv1: tpacket_hdr_variant1, + }, padding: [8]u8, };