@@ -144,6 +144,29 @@ impl Drop for GenlMsg {
144144 }
145145}
146146
147+ /// Flags for a generic netlink family.
148+ struct FamilyFlags {
149+ /// Whether the family supports network namespaces.
150+ netnsok : bool ,
151+ /// Whether the family supports parallel operations.
152+ parallel_ops : bool ,
153+ }
154+
155+ impl FamilyFlags {
156+ /// Converts the flags to the bitfield representation used by `genl_family`.
157+ const fn into_bitfield ( self ) -> bindings:: __BindgenBitfieldUnit < [ u8 ; 1 ] > {
158+ let mut bits = 0 ;
159+ if self . netnsok {
160+ bits |= 1 << 0 ;
161+ }
162+ if self . parallel_ops {
163+ bits |= 1 << 1 ;
164+ }
165+ // SAFETY: This bitfield is represented as an u8.
166+ unsafe { core:: mem:: transmute :: < u8 , bindings:: __BindgenBitfieldUnit < [ u8 ; 1 ] > > ( bits) }
167+ }
168+ }
169+
147170/// A generic netlink family.
148171#[ repr( transparent) ]
149172pub struct Family {
@@ -167,10 +190,11 @@ impl Family {
167190 }
168191 let mut genl_family = bindings:: genl_family {
169192 version,
170- // SAFETY: This bitfield is represented as an u8.
171- _bitfield_1 : unsafe {
172- core:: mem:: transmute :: < u8 , bindings:: __BindgenBitfieldUnit < [ u8 ; 1 ] > > ( 0b11u8 )
173- } ,
193+ _bitfield_1 : FamilyFlags {
194+ netnsok : true ,
195+ parallel_ops : true ,
196+ }
197+ . into_bitfield ( ) ,
174198 module : module. as_ptr ( ) ,
175199 mcgrps : mcgrps. as_ptr ( ) . cast ( ) ,
176200 n_mcgrps,
@@ -242,7 +266,7 @@ impl MulticastGroup {
242266///
243267/// # Invariants
244268///
245- /// `self.family` always holds a valid reference to an initialized and registered [`genl_family `].
269+ /// `self.family` always holds a valid reference to an initialized and registered [`Family `].
246270pub struct Registration {
247271 family : & ' static Family ,
248272}
@@ -265,3 +289,37 @@ impl Drop for Registration {
265289 unsafe { bindings:: genl_unregister_family ( self . family . as_raw ( ) ) } ;
266290 }
267291}
292+
293+ #[ macros:: kunit_tests( rust_netlink) ]
294+ mod tests {
295+ use super :: * ;
296+
297+ #[ test]
298+ fn test_family_flags_bitfield ( ) {
299+ for netnsok in [ false , true ] {
300+ for parallel_ops in [ false , true ] {
301+ let mut b_fam = bindings:: genl_family {
302+ ..Default :: default ( )
303+ } ;
304+ b_fam. set_netnsok ( if netnsok { 1 } else { 0 } ) ;
305+ b_fam. set_parallel_ops ( if parallel_ops { 1 } else { 0 } ) ;
306+
307+ let c_bitfield = FamilyFlags {
308+ netnsok,
309+ parallel_ops,
310+ }
311+ . into_bitfield ( ) ;
312+
313+ // SAFETY: The bit field is stored as u8.
314+ let b_val: u8 = unsafe { core:: mem:: transmute ( b_fam. _bitfield_1 ) } ;
315+ // SAFETY: The bit field is stored as u8.
316+ let c_val: u8 = unsafe { core:: mem:: transmute ( c_bitfield) } ;
317+ assert_eq ! (
318+ b_val, c_val,
319+ "Failed for netnsok={} parallel_ops={}" ,
320+ netnsok, parallel_ops
321+ ) ;
322+ }
323+ }
324+ }
325+ }
0 commit comments