#[nfnetlink_struct]
Expand description
nfnetlink_struct
is a macro wrapping structures that describe nftables objects.
It allows serializing and deserializing these objects to the corresponding nfnetlink
attributes.
It automatically generates getter and setter functions for each netlink properties.
§Parameters
The macro have multiple parameters:
nested
(defaults tofalse
): the structure is nested (in the netlink sense) inside its parent structure. This is the case of most structures outside of the main nftables objects (batches, sets, rules, chains and tables), which are the outermost structures, and as such cannot be nested.derive_decoder
(defaults totrue
): derive a [rustables::nlmsg::AttributeDecoder
] implementation for the structurederive_deserialize
(defaults totrue
): derive a [rustables::nlmsg::NfNetlinkDeserializable
] implementation for the structure
§Example use
ⓘ
#[nfnetlink_struct(derive_deserialize = false)]
#[derive(PartialEq, Eq, Default, Debug)]
pub struct Chain {
family: ProtocolFamily,
#[field(NFTA_CHAIN_TABLE)]
table: String,
#[field(NFTA_CHAIN_TYPE, name_in_functions = "type")]
chain_type: ChainType,
#[field(optional = true, crate::sys::NFTA_CHAIN_USERDATA)]
userdata: Vec<u8>,
...
}
§Type of fields
This contrived example show the two possible type of fields:
- A field that is not converted to a netlink attribute (
family
) because it is not annotated in#[field]
attribute. When deserialized, this field will take the value it is given in the Default implementation of the struct. - A field that is annotated with the
#[field]
attribute. That attribute takes parameters (there are none here), and the netlink attribute type. When annotated with that attribute, the macro will generateget_<name>
,set_<name>
andwith_<name>
methods to manipulate the attribute (e.g.get_table
,set_table
andwith_table
). It will also replace the field type (hereString
) with an Option (Option<String>
) so the struct may represent objects where that attribute is not set.
§#[field]
parameters
The #[field]
attribute can be parametrized through two options:
optional
(defaults tofalse
): if the netlink attribute type (hereNFTA_CHAIN_USERDATA
) does not exist, do not generate methods and ignore this attribute if encountered while deserializing a nftables object. This is useful for attributes added recently to the kernel, which may not be supported on older kernels. Support for an attribute is detected according to the existence of that attribute in the kernel headers.name_in_functions
(not defined by default): overwrite the<name
> in the name of the methodsget_<name>
,set_<name>
andwith_<name>
. Here, this means that even though the field is calledchain_type
, users can query it with the methodget_type
instead ofget_chain_type
.