Skip to main content

NftablesDiff

Struct NftablesDiff 

Source
#[non_exhaustive]
pub struct NftablesDiff { pub tables_to_add: Vec<DeclaredTable>, pub tables_to_delete: Vec<(Family, String)>, pub chains_to_add: Vec<(String, Family, DeclaredChain)>, pub chains_to_delete: Vec<(String, Family, String)>, pub rules_to_add: Vec<DeclaredRule>, pub rules_to_delete: Vec<(String, Family, String, RuleHandle)>, pub rules_to_replace: Vec<(String, Family, String, RuleHandle, DeclaredRule)>, pub flowtables_to_add: Vec<DeclaredFlowtable>, pub flowtables_to_delete: Vec<(Family, String, String)>, }
Expand description

The result of comparing a declared NftablesConfig against the kernel’s current state. Apply via Self::apply.

is_empty() returns true when declared and current already agree (idempotent reapply).

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§tables_to_add: Vec<DeclaredTable>

Tables to create.

§tables_to_delete: Vec<(Family, String)>

Tables to delete (family, name).

§chains_to_add: Vec<(String, Family, DeclaredChain)>

Chains to create — (owning table, owning family, chain).

§chains_to_delete: Vec<(String, Family, String)>

Chains to delete — (table, family, name).

§rules_to_add: Vec<DeclaredRule>

Rules to add — paired with owning table/chain/family.

§rules_to_delete: Vec<(String, Family, String, RuleHandle)>

Rules to delete — (table, family, chain, kernel_handle). Chain is carried explicitly because the kernel rejects a NFT_MSG_DELRULE with an empty NFTA_RULE_CHAIN even when NFTA_RULE_HANDLE is supplied (returns ENOENT); the earlier (table, family, handle) shape relied on a kernel behavior that doesn’t actually hold. Plan 178 closeout.

§rules_to_replace: Vec<(String, Family, String, RuleHandle, DeclaredRule)>

Rules to replace in-place. Each entry is (table, family, chain, kernel_handle, replacement) — emits NFT_MSG_NEWRULE | NLM_F_REPLACE | NFTA_RULE_HANDLE so the kernel atomically swaps the rule body at that handle (preserves position, no flush).

Populated by NftablesConfig::diff when a declared keyed rule matches a kernel rule by NFTA_RULE_USERDATA comment but the expression bytes differ. Plan 157b v2.

§flowtables_to_add: Vec<DeclaredFlowtable>

Flowtables to add.

§flowtables_to_delete: Vec<(Family, String, String)>

Flowtables to delete — (family, table, name).

Implementations§

Source§

impl NftablesDiff

Source

pub async fn apply(&self, conn: &Connection<Nftables>) -> Result<usize>

Apply the diff to the kernel atomically.

Builds a single Transaction covering every change in the diff and commits it in one NFNL_MSG_BATCH_BEGIN ... BATCH_END round-trip. The kernel either accepts the whole batch (full diff visible to other readers in one step) or rejects the whole batch (kernel rolls back; no intermediate state observable).

Returns the diff’s change_count on success — a caller-visible “we did N things” signal useful for tracing::info!-style post-apply logging.

Source

pub async fn apply_reconcile( &self, conn: &Connection<Nftables>, opts: ReconcileOptions, ) -> Result<ReconcileReport>

Apply with bounded retry on transient kernel-busy errors (EBUSY / EAGAIN). Useful when another process may be mutating the same ruleset concurrently — e.g. systemd-resolved

  • a node firewall tool both calling nft simultaneously.

On EBUSY / EAGAIN, sleeps opts.backoff × 2^attempt and retries up to opts.max_retries times. Non-transient errors surface immediately (caller’s responsibility to handle).

Returns a ReconcileReport with the attempt count + the diff that was finally applied. Total wall time is bounded by Σ(opts.backoff × 2^i) for i in 0..max_retries.

§Example
use nlink::netlink::nftables::config::{NftablesConfig, ReconcileOptions};
use std::time::Duration;

let cfg = NftablesConfig::new() /* ... */;
let diff = cfg.diff(&conn).await?;
let report = diff
    .apply_reconcile(&conn, ReconcileOptions::default())
    .await?;
if report.attempts > 1 {
    tracing::warn!(retries = report.attempts - 1, "transient conflict");
}
Source§

impl NftablesDiff

Source

pub fn is_empty(&self) -> bool

true if declared state already matches kernel state.

Source

pub fn change_count(&self) -> usize

Total number of changes (sum of all add/delete counts).

Source

pub fn summary(&self) -> String

👎Deprecated since 0.19.0:

use Display via format!("{}") or diff.to_string() instead — Plan 188 §2.6

Render a one-line-per-change human summary. Useful for tracing::info! or CLI output.

Equivalent to format!("{self}") — Plan 183 (0.18) made the std::fmt::Display impl share the same renderer. Prefer the Display form (diff.to_string() / format!("{diff}")) for new code.

Trait Implementations§

Source§

impl Clone for NftablesDiff

Source§

fn clone(&self) -> NftablesDiff

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NftablesDiff

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NftablesDiff

Source§

fn default() -> NftablesDiff

Returns the “default value” for a type. Read more
Source§

impl Display for NftablesDiff

Display mirrors NftablesDiff::summary so callers can println!("{diff}") directly. Plan 183.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for NftablesDiff

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more