Skip to content

Commit 83d8c39

Browse files
committed
Auto merge of #2080 - jbit:freebsd-bpf, r=JohnTitor
Add FreeBSD BPF structures This PR adds BPF structures that are common between [FreeBSD 11](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/net/bpf.h), [FreeBSD 12](https://github.com/freebsd/freebsd-src/blob/release/12.0.0/sys/net/bpf.h), [FreeBSD mainline/13](https://github.com/freebsd/freebsd-src/blob/main/sys/net/bpf.h) and [DragonFlyBSD 5.9](https://github.com/DragonFlyBSD/DragonFlyBSD/blob/v5.9.0/sys/net/bpf.h). It also fixes the definition of `BPF_ALIGNMENT`, which should be equal to `sizeof(long)`, which is `4` on 32bit FreeBSD. https://www.freebsd.org/cgi/man.cgi?query=arch&sektion=7 > All supported ABIs can be divided into two groups: > * ILP32: `int`, `long`, `void *` types machine representations all have 4-byte size. > * LP64: `int` type machine representation uses 4 bytes, while `long` and `void *` are 8 bytes. I introduced the private `SIZEOF_LONG` const, since I didn't want to introduce a dependency on rust 1.24+ by depending on `libc_const_size_of`. These changes allow my experimental crate to build on FreeBSD: https://github.com/jbit/powerline
2 parents 8a88b7d + 4b55c87 commit 83d8c39

File tree

1 file changed

+45
-2
lines changed
  • src/unix/bsd/freebsdlike

1 file changed

+45
-2
lines changed

src/unix/bsd/freebsdlike/mod.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,42 @@ s! {
272272
pub piod_len: ::size_t,
273273
}
274274

275+
// bpf.h
276+
277+
pub struct bpf_program {
278+
pub bf_len: ::c_uint,
279+
pub bf_insns: *mut bpf_insn,
280+
}
281+
282+
pub struct bpf_stat {
283+
pub bs_recv: ::c_uint,
284+
pub bs_drop: ::c_uint,
285+
}
286+
287+
pub struct bpf_version {
288+
pub bv_major: ::c_ushort,
289+
pub bv_minor: ::c_ushort,
290+
}
291+
292+
pub struct bpf_hdr {
293+
pub bh_tstamp: ::timeval,
294+
pub bh_caplen: u32,
295+
pub bh_datalen: u32,
296+
pub bh_hdrlen: ::c_ushort,
297+
}
298+
299+
pub struct bpf_insn {
300+
pub code: ::c_ushort,
301+
pub jt: ::c_uchar,
302+
pub jf: ::c_uchar,
303+
pub k: u32,
304+
}
305+
306+
pub struct bpf_dltlist {
307+
bfl_len: ::c_uint,
308+
bfl_list: *mut ::c_uint,
309+
}
310+
275311
// elf.h
276312

277313
pub struct Elf32_Phdr {
@@ -359,6 +395,14 @@ cfg_if! {
359395
}
360396
}
361397

398+
// Non-public helper constant
399+
#[cfg(all(not(libc_const_size_of), target_pointer_width = "32"))]
400+
const SIZEOF_LONG: usize = 4;
401+
#[cfg(all(not(libc_const_size_of), target_pointer_width = "64"))]
402+
const SIZEOF_LONG: usize = 8;
403+
#[cfg(libc_const_size_of)]
404+
const SIZEOF_LONG: usize = ::mem::size_of::<::c_long>();
405+
362406
#[deprecated(
363407
since = "0.2.64",
364408
note = "Can vary at runtime. Use sysconf(3) instead"
@@ -1216,8 +1260,7 @@ pub const ONLRET: ::tcflag_t = 0x40;
12161260
pub const CMGROUP_MAX: usize = 16;
12171261

12181262
// https://github.com/freebsd/freebsd/blob/master/sys/net/bpf.h
1219-
// sizeof(long)
1220-
pub const BPF_ALIGNMENT: ::c_int = 8;
1263+
pub const BPF_ALIGNMENT: usize = SIZEOF_LONG;
12211264

12221265
// Values for rtprio struct (prio field) and syscall (function argument)
12231266
pub const RTP_PRIO_MIN: ::c_ushort = 0;

0 commit comments

Comments
 (0)