nlink 0.15.1

Async netlink library for Linux network configuration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Bridge Forwarding Database (FDB) management.
//!
//! This module provides typed builders for managing bridge FDB entries,
//! which are used for MAC address learning and forwarding in Linux bridges.
//!
//! # Example
//!
//! ```ignore
//! use nlink::netlink::{Connection, Route};
//! use nlink::netlink::fdb::FdbEntryBuilder;
//!
//! let conn = Connection::<Route>::new()?;
//!
//! // List FDB entries for a bridge
//! let entries = conn.get_fdb("br0").await?;
//! for entry in &entries {
//!     println!("{} vlan={:?}", entry.mac_str(), entry.vlan);
//! }
//!
//! // Add a static FDB entry
//! let mac = FdbEntryBuilder::parse_mac("aa:bb:cc:dd:ee:ff")?;
//! conn.add_fdb(
//!     FdbEntryBuilder::new(mac)
//!         .dev("veth0")
//!         .master("br0")
//!         .permanent()
//! ).await?;
//!
//! // Add VXLAN FDB entry (remote VTEP)
//! use std::net::Ipv4Addr;
//! conn.add_fdb(
//!     FdbEntryBuilder::new([0x00; 6])  // all-zeros for BUM traffic
//!         .dev("vxlan0")
//!         .dst(Ipv4Addr::new(192, 168, 1, 100).into())
//! ).await?;
//!
//! // Delete an entry
//! conn.del_fdb("veth0", mac, None).await?;
//! ```

use std::net::IpAddr;

use super::{
    builder::MessageBuilder,
    connection::Connection,
    error::{Error, Result},
    interface_ref::InterfaceRef,
    message::{NLM_F_ACK, NLM_F_DUMP, NLM_F_REQUEST, NlMsgType},
    messages::NeighborMessage,
    protocol::Route,
    types::neigh::{NdMsg, NdaAttr, NeighborState},
};

/// NLM_F_CREATE flag
const NLM_F_CREATE: u16 = 0x400;
/// NLM_F_EXCL flag - fail if entry exists
const NLM_F_EXCL: u16 = 0x200;
/// NLM_F_REPLACE flag - replace existing entry
const NLM_F_REPLACE: u16 = 0x100;

/// AF_BRIDGE constant
const AF_BRIDGE: u8 = 7;

/// Neighbor flags for FDB entries.
mod ntf {
    /// Entry for the interface itself
    pub const SELF: u8 = 0x02;
    /// Entry for the master bridge
    pub const MASTER: u8 = 0x04;
    /// Externally learned entry
    pub const EXT_LEARNED: u8 = 0x10;
}

/// Neighbor states
mod nud {
    /// Permanent (static) entry
    pub const PERMANENT: u16 = 0x80;
    /// Reachable (dynamic) entry
    pub const REACHABLE: u16 = 0x02;
}

/// FDB entry information.
///
/// Represents a bridge forwarding database entry, containing MAC address
/// to port mappings, optional VLAN information, and VXLAN remote endpoint
/// data.
#[derive(Debug, Clone)]
pub struct FdbEntry {
    /// Interface index (bridge port)
    pub ifindex: u32,
    /// MAC address (6 bytes)
    pub mac: [u8; 6],
    /// VLAN ID (if VLAN filtering is enabled)
    pub vlan: Option<u16>,
    /// Destination IP (for VXLAN remote VTEP)
    pub dst: Option<IpAddr>,
    /// VNI (for VXLAN)
    pub vni: Option<u32>,
    /// Entry state (permanent, reachable, etc.)
    pub state: NeighborState,
    /// Entry flags (NTF_SELF, NTF_MASTER, etc.)
    pub flags: u8,
    /// Master device index (bridge interface)
    pub master: Option<u32>,
}

impl FdbEntry {
    /// Create from a NeighborMessage.
    ///
    /// Returns `None` if the message doesn't have a valid MAC address.
    pub fn from_neighbor(msg: &NeighborMessage) -> Option<Self> {
        let lladdr = msg.lladdr()?;
        if lladdr.len() != 6 {
            return None;
        }

        let mut mac = [0u8; 6];
        mac.copy_from_slice(lladdr);

        Some(Self {
            ifindex: msg.ifindex(),
            mac,
            vlan: msg.vlan(),
            dst: msg.destination().cloned(),
            vni: msg.vni(),
            state: msg.state(),
            flags: msg.flags(),
            master: msg.master(),
        })
    }

    /// Check if this is a permanent (static) entry.
    pub fn is_permanent(&self) -> bool {
        self.state == NeighborState::Permanent
    }

    /// Check if this is a dynamic (learned) entry.
    pub fn is_dynamic(&self) -> bool {
        !self.is_permanent()
    }

    /// Check if entry is for the interface itself (NTF_SELF).
    pub fn is_self(&self) -> bool {
        self.flags & ntf::SELF != 0
    }

    /// Check if entry is for the master bridge (NTF_MASTER).
    pub fn is_master(&self) -> bool {
        self.flags & ntf::MASTER != 0
    }

    /// Check if entry was externally learned (NTF_EXT_LEARNED).
    pub fn is_extern_learn(&self) -> bool {
        self.flags & ntf::EXT_LEARNED != 0
    }

    /// Format MAC address as a colon-separated hex string.
    pub fn mac_str(&self) -> String {
        format!(
            "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
            self.mac[0], self.mac[1], self.mac[2], self.mac[3], self.mac[4], self.mac[5]
        )
    }
}

/// Builder for FDB entries.
///
/// # Example
///
/// ```ignore
/// use nlink::netlink::fdb::FdbEntryBuilder;
/// use std::net::Ipv4Addr;
///
/// // Static entry on a bridge port
/// let entry = FdbEntryBuilder::new([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])
///     .dev("veth0")
///     .master("br0")
///     .vlan(100)
///     .permanent();
///
/// // VXLAN remote VTEP entry
/// let vxlan_entry = FdbEntryBuilder::new([0x00; 6])
///     .dev("vxlan0")
///     .dst(Ipv4Addr::new(192, 168, 1, 100).into());
/// ```
#[derive(Debug, Clone, Default)]
#[must_use = "builders do nothing unless used"]
pub struct FdbEntryBuilder {
    mac: [u8; 6],
    dev: Option<InterfaceRef>,
    vlan: Option<u16>,
    dst: Option<IpAddr>,
    vni: Option<u32>,
    master: Option<InterfaceRef>,
    permanent: bool,
    self_flag: bool,
}

impl FdbEntryBuilder {
    /// Create a new FDB entry builder with the given MAC address.
    ///
    /// By default, the entry is marked as permanent (static).
    pub fn new(mac: [u8; 6]) -> Self {
        Self {
            mac,
            permanent: true,
            ..Default::default()
        }
    }

    /// Parse a MAC address from a colon-separated hex string.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let mac = FdbEntryBuilder::parse_mac("aa:bb:cc:dd:ee:ff")?;
    /// ```
    pub fn parse_mac(mac_str: &str) -> Result<[u8; 6]> {
        crate::util::addr::parse_mac(mac_str)
            .map_err(|e| Error::InvalidMessage(format!("invalid MAC: {}", e)))
    }

    /// Set the device name (bridge port interface).
    pub fn dev(mut self, dev: impl Into<String>) -> Self {
        self.dev = Some(InterfaceRef::Name(dev.into()));
        self
    }

    /// Set the interface index directly (namespace-safe).
    ///
    /// Use this instead of `dev()` when operating in a network namespace
    /// to avoid reading `/sys/class/net/` from the wrong namespace.
    pub fn ifindex(mut self, ifindex: u32) -> Self {
        self.dev = Some(InterfaceRef::Index(ifindex));
        self
    }

    /// Get the device reference.
    pub fn device_ref(&self) -> Option<&InterfaceRef> {
        self.dev.as_ref()
    }

    /// Set the VLAN ID.
    ///
    /// Only relevant for bridges with VLAN filtering enabled.
    pub fn vlan(mut self, vlan: u16) -> Self {
        self.vlan = Some(vlan);
        self
    }

    /// Set the destination IP address (for VXLAN FDB entries).
    ///
    /// This specifies the remote VTEP IP address for VXLAN tunneling.
    pub fn dst(mut self, dst: IpAddr) -> Self {
        self.dst = Some(dst);
        self
    }

    /// Set the VNI (for VXLAN FDB entries).
    pub fn vni(mut self, vni: u32) -> Self {
        self.vni = Some(vni);
        self
    }

    /// Set the master bridge device by name.
    pub fn master(mut self, master: impl Into<String>) -> Self {
        self.master = Some(InterfaceRef::Name(master.into()));
        self
    }

    /// Set the master bridge device by interface index (namespace-safe).
    pub fn master_ifindex(mut self, ifindex: u32) -> Self {
        self.master = Some(InterfaceRef::Index(ifindex));
        self
    }

    /// Get the master device reference.
    pub fn master_ref(&self) -> Option<&InterfaceRef> {
        self.master.as_ref()
    }

    /// Mark entry as permanent (static). This is the default.
    pub fn permanent(mut self) -> Self {
        self.permanent = true;
        self
    }

    /// Mark entry as dynamic (will age out).
    pub fn dynamic(mut self) -> Self {
        self.permanent = false;
        self
    }

    /// Add to interface's own FDB (sets NTF_SELF flag).
    ///
    /// This is typically used for entries on the bridge port itself
    /// rather than entries forwarded to the master bridge.
    pub fn self_(mut self) -> Self {
        self.self_flag = true;
        self
    }

    /// Write the add message to the builder with resolved interface indices.
    pub(crate) fn write_add(
        &self,
        builder: &mut MessageBuilder,
        ifindex: u32,
        master_idx: Option<u32>,
    ) {
        let state = if self.permanent {
            nud::PERMANENT
        } else {
            nud::REACHABLE
        };

        let mut ntf_flags: u8 = 0;
        if self.self_flag {
            ntf_flags |= ntf::SELF;
        }

        let ndmsg = NdMsg::new()
            .with_family(AF_BRIDGE)
            .with_ifindex(ifindex as i32)
            .with_state(state)
            .with_flags(ntf_flags);

        builder.append(&ndmsg);

        // NDA_LLADDR - MAC address (required)
        builder.append_attr(NdaAttr::Lladdr as u16, &self.mac);

        // NDA_MASTER - bridge interface
        if let Some(master) = master_idx {
            builder.append_attr_u32(NdaAttr::Master as u16, master);
        }

        // NDA_VLAN
        if let Some(vlan) = self.vlan {
            builder.append_attr_u16(NdaAttr::Vlan as u16, vlan);
        }

        // NDA_DST - remote IP for VXLAN
        if let Some(ref dst) = self.dst {
            match dst {
                IpAddr::V4(v4) => {
                    builder.append_attr(NdaAttr::Dst as u16, &v4.octets());
                }
                IpAddr::V6(v6) => {
                    builder.append_attr(NdaAttr::Dst as u16, &v6.octets());
                }
            }
        }

        // NDA_VNI
        if let Some(vni) = self.vni {
            builder.append_attr_u32(NdaAttr::Vni as u16, vni);
        }
    }

    /// Write the delete message to the builder with resolved interface index.
    pub(crate) fn write_delete(&self, builder: &mut MessageBuilder, ifindex: u32) {
        let ndmsg = NdMsg::new()
            .with_family(AF_BRIDGE)
            .with_ifindex(ifindex as i32);

        builder.append(&ndmsg);

        // NDA_LLADDR - MAC address (required for delete)
        builder.append_attr(NdaAttr::Lladdr as u16, &self.mac);

        // NDA_VLAN - needed if VLAN filtering is enabled
        if let Some(vlan) = self.vlan {
            builder.append_attr_u16(NdaAttr::Vlan as u16, vlan);
        }
    }
}

// ============================================================================
// Connection Methods
// ============================================================================

impl Connection<Route> {
    /// Get all FDB entries for a bridge.
    ///
    /// Returns entries where the master device matches the specified bridge,
    /// or entries directly on the bridge interface itself.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let entries = conn.get_fdb("br0").await?;
    /// for entry in &entries {
    ///     println!("{} on ifindex {} vlan={:?}",
    ///         entry.mac_str(), entry.ifindex, entry.vlan);
    /// }
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "get_fdb"))]
    pub async fn get_fdb(&self, bridge: impl Into<InterfaceRef>) -> Result<Vec<FdbEntry>> {
        let bridge_idx = self.resolve_interface(&bridge.into()).await?;
        self.get_fdb_by_index(bridge_idx).await
    }

    /// Get all FDB entries for a bridge by interface index.
    ///
    /// Use this method when operating in a network namespace to avoid
    /// reading `/sys/class/net/` from the wrong namespace.
    #[tracing::instrument(level = "debug", skip_all, fields(method = "get_fdb_by_index"))]
    pub async fn get_fdb_by_index(&self, bridge_idx: u32) -> Result<Vec<FdbEntry>> {
        // Query neighbors with AF_BRIDGE family to get FDB entries
        let neighbors = self.get_bridge_neighbors().await?;

        Ok(neighbors
            .iter()
            .filter(|n| n.master() == Some(bridge_idx) || n.ifindex() == bridge_idx)
            .filter_map(FdbEntry::from_neighbor)
            .collect())
    }

    /// Get all bridge neighbor entries (AF_BRIDGE FDB dump).
    async fn get_bridge_neighbors(&self) -> Result<Vec<NeighborMessage>> {
        use super::{message::NLMSG_HDRLEN, parse::FromNetlink};

        let ndmsg = NdMsg::new().with_family(AF_BRIDGE);
        let mut builder = MessageBuilder::new(NlMsgType::RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP);
        builder.append(&ndmsg);

        let responses = self.send_dump(builder).await?;

        let mut parsed = Vec::new();
        for response in responses {
            if response.len() < NLMSG_HDRLEN {
                continue;
            }
            let payload = &response[NLMSG_HDRLEN..];
            if let Ok(msg) = NeighborMessage::from_bytes(payload) {
                parsed.push(msg);
            }
        }
        Ok(parsed)
    }

    /// Get FDB entries for a specific bridge port.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let entries = conn.get_fdb_for_port("br0", "veth0").await?;
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "get_fdb_for_port"))]
    pub async fn get_fdb_for_port(
        &self,
        bridge: impl Into<InterfaceRef>,
        port: impl Into<InterfaceRef>,
    ) -> Result<Vec<FdbEntry>> {
        let bridge_idx = self.resolve_interface(&bridge.into()).await?;
        let port_idx = self.resolve_interface(&port.into()).await?;

        let neighbors = self.get_bridge_neighbors().await?;

        Ok(neighbors
            .iter()
            .filter(|n| n.ifindex() == port_idx)
            .filter(|n| n.master() == Some(bridge_idx))
            .filter_map(FdbEntry::from_neighbor)
            .collect())
    }

    /// Resolve FDB entry interface references.
    async fn resolve_fdb_interfaces(&self, entry: &FdbEntryBuilder) -> Result<(u32, Option<u32>)> {
        let ifindex = match entry.device_ref() {
            Some(iface) => self.resolve_interface(iface).await?,
            None => {
                return Err(Error::InvalidMessage(
                    "device name or ifindex required".into(),
                ));
            }
        };

        let master_idx = match entry.master_ref() {
            Some(iface) => Some(self.resolve_interface(iface).await?),
            None => None,
        };

        Ok((ifindex, master_idx))
    }

    /// Add an FDB entry.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::fdb::FdbEntryBuilder;
    ///
    /// let mac = FdbEntryBuilder::parse_mac("aa:bb:cc:dd:ee:ff")?;
    /// conn.add_fdb(
    ///     FdbEntryBuilder::new(mac)
    ///         .dev("veth0")
    ///         .master("br0")
    ///         .vlan(100)
    /// ).await?;
    ///
    /// // Namespace-safe version using interface index
    /// conn.add_fdb(
    ///     FdbEntryBuilder::new(mac)
    ///         .ifindex(5)
    ///         .master_ifindex(3)
    /// ).await?;
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "add_fdb"))]
    pub async fn add_fdb(&self, entry: FdbEntryBuilder) -> Result<()> {
        let (ifindex, master_idx) = self.resolve_fdb_interfaces(&entry).await?;
        let mut builder = MessageBuilder::new(
            NlMsgType::RTM_NEWNEIGH,
            NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL,
        );
        entry.write_add(&mut builder, ifindex, master_idx);
        self.send_ack(builder)
            .await
            .map_err(|e| e.with_context("add_fdb"))
    }

    /// Replace an FDB entry (add or update).
    ///
    /// If the entry exists, it will be updated. Otherwise, it will be created.
    #[tracing::instrument(level = "debug", skip_all, fields(method = "replace_fdb"))]
    pub async fn replace_fdb(&self, entry: FdbEntryBuilder) -> Result<()> {
        let (ifindex, master_idx) = self.resolve_fdb_interfaces(&entry).await?;
        let mut builder = MessageBuilder::new(
            NlMsgType::RTM_NEWNEIGH,
            NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_REPLACE,
        );
        entry.write_add(&mut builder, ifindex, master_idx);
        self.send_ack(builder)
            .await
            .map_err(|e| e.with_context("replace_fdb"))
    }

    /// Delete an FDB entry by device name, MAC address, and optional VLAN.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Delete entry without VLAN
    /// conn.del_fdb("veth0", [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff], None).await?;
    ///
    /// // Delete entry with specific VLAN
    /// conn.del_fdb("veth0", [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff], Some(100)).await?;
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "del_fdb"))]
    pub async fn del_fdb(
        &self,
        dev: impl Into<InterfaceRef>,
        mac: [u8; 6],
        vlan: Option<u16>,
    ) -> Result<()> {
        let ifindex = self.resolve_interface(&dev.into()).await?;
        self.del_fdb_by_index(ifindex, mac, vlan).await
    }

    /// Delete an FDB entry by interface index, MAC address, and optional VLAN.
    ///
    /// Use this method when operating in a network namespace.
    #[tracing::instrument(level = "debug", skip_all, fields(method = "del_fdb_by_index"))]
    pub async fn del_fdb_by_index(
        &self,
        ifindex: u32,
        mac: [u8; 6],
        vlan: Option<u16>,
    ) -> Result<()> {
        let mut entry = FdbEntryBuilder::new(mac).ifindex(ifindex);
        if let Some(v) = vlan {
            entry = entry.vlan(v);
        }
        let mut builder = MessageBuilder::new(NlMsgType::RTM_DELNEIGH, NLM_F_REQUEST | NLM_F_ACK);
        entry.write_delete(&mut builder, ifindex);
        self.send_ack(builder)
            .await
            .map_err(|e| e.with_context("del_fdb"))
    }

    /// Flush all dynamic FDB entries for a bridge.
    ///
    /// Permanent (static) entries are not removed.
    ///
    /// # Example
    ///
    /// ```ignore
    /// conn.flush_fdb("br0").await?;
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "flush_fdb"))]
    pub async fn flush_fdb(&self, bridge: impl Into<InterfaceRef>) -> Result<()> {
        let entries = self.get_fdb(bridge).await?;

        for entry in entries {
            // Only flush dynamic entries
            if entry.is_dynamic()
                && let Err(e) = self
                    .del_fdb_by_index(entry.ifindex, entry.mac, entry.vlan)
                    .await
            {
                // Ignore "not found" errors (race condition with aging)
                if !e.is_not_found() {
                    return Err(e);
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_mac() {
        let mac = FdbEntryBuilder::parse_mac("aa:bb:cc:dd:ee:ff").unwrap();
        assert_eq!(mac, [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    }

    #[test]
    fn test_parse_mac_uppercase() {
        let mac = FdbEntryBuilder::parse_mac("AA:BB:CC:DD:EE:FF").unwrap();
        assert_eq!(mac, [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    }

    #[test]
    fn test_parse_mac_invalid() {
        assert!(FdbEntryBuilder::parse_mac("invalid").is_err());
        assert!(FdbEntryBuilder::parse_mac("aa:bb:cc:dd:ee").is_err());
        assert!(FdbEntryBuilder::parse_mac("aa:bb:cc:dd:ee:ff:gg").is_err());
    }

    #[test]
    fn test_fdb_entry_mac_str() {
        let entry = FdbEntry {
            ifindex: 1,
            mac: [0x00, 0x11, 0x22, 0x33, 0x44, 0x55],
            vlan: None,
            dst: None,
            vni: None,
            state: NeighborState::Permanent,
            flags: 0,
            master: None,
        };
        assert_eq!(entry.mac_str(), "00:11:22:33:44:55");
    }

    #[test]
    fn test_fdb_entry_flags() {
        let entry = FdbEntry {
            ifindex: 1,
            mac: [0; 6],
            vlan: None,
            dst: None,
            vni: None,
            state: NeighborState::Permanent,
            flags: ntf::SELF | ntf::MASTER,
            master: None,
        };
        assert!(entry.is_self());
        assert!(entry.is_master());
        assert!(!entry.is_extern_learn());
    }

    #[test]
    fn test_fdb_entry_permanent() {
        let permanent = FdbEntry {
            ifindex: 1,
            mac: [0; 6],
            vlan: None,
            dst: None,
            vni: None,
            state: NeighborState::Permanent,
            flags: 0,
            master: None,
        };
        assert!(permanent.is_permanent());
        assert!(!permanent.is_dynamic());

        let dynamic = FdbEntry {
            ifindex: 1,
            mac: [0; 6],
            vlan: None,
            dst: None,
            vni: None,
            state: NeighborState::Reachable,
            flags: 0,
            master: None,
        };
        assert!(!dynamic.is_permanent());
        assert!(dynamic.is_dynamic());
    }

    #[test]
    fn test_builder_default() {
        let builder = FdbEntryBuilder::new([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
        assert!(builder.permanent); // default is permanent
        assert!(!builder.self_flag);
    }

    #[test]
    fn test_builder_chain() {
        let builder = FdbEntryBuilder::new([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])
            .dev("veth0")
            .master("br0")
            .vlan(100)
            .dynamic()
            .self_();

        assert_eq!(builder.dev, Some(InterfaceRef::Name("veth0".to_string())));
        assert_eq!(builder.master, Some(InterfaceRef::Name("br0".to_string())));
        assert_eq!(builder.vlan, Some(100));
        assert!(!builder.permanent);
        assert!(builder.self_flag);
    }

    #[test]
    fn test_builder_ifindex() {
        let builder = FdbEntryBuilder::new([0; 6]).ifindex(5).master_ifindex(3);

        assert_eq!(builder.dev, Some(InterfaceRef::Index(5)));
        assert_eq!(builder.master, Some(InterfaceRef::Index(3)));
    }
}