Mapping Network Topology with lldpd on Linux: Switch Port, VLAN, and Neighbor Discovery

Networking tutorial - IT technology blog
Networking tutorial - IT technology blog

The 2:14 AM Outage: Lost in a Sea of DAC Cables

At 2:14 AM on a Tuesday, PagerDuty blasted my phone with urgent alerts. Database replica db-prod-04 had abruptly dropped out of its Galera cluster after packet loss surged past 60%. SSH access over the primary bonded interface was dead. That left an IPMI serial-over-LAN console as my only way in.

I dialed into the bridge and flagged the on-call network engineer. He skipped pleasantries: “Which top-of-rack switch is eth0 plugged into, and on which physical port? I need to check interface drop counters.”

Silence. The datacenter was sixty miles away in Ashburn. Worse, our NetBox rack diagram had not been touched in four months. Tracing twinaxial DAC cables through a packed 42U rack at 2 AM was out of the question. That night drove home an unvarnished reality: never rely on human-maintained spreadsheets during a Sev-1. You must know how to make Linux query the physical network fabric directly.

Why Manual Cable Audits Crumble Under Pressure

Standard Linux network stacks operate blind at Layer 2. When you configure an interface with iproute2, NetworkManager, or systemd-networkd, the kernel manages ARP tables, applies routing rules, and pushes frames through MAC addresses. What it never does out of the box is identify physical neighbors.

That blind spot triggers costly delays during active outages:

  • Documentation Drift: Field technicians swap faulty SFP28 optics or re-seat patch cables during emergency maintenance. They fully intend to update the CMDB on Monday morning. They rarely do.
  • Silent Discards: Enterprise switches broadcast Layer 2 advertisement frames every 30 seconds. A vanilla Linux kernel quietly drops these frames because no application holds an open raw socket for that ethertype.
  • Asymmetric Bond Cabling: In dual-ToR setups using LACP (802.3ad) across MLAG or Cisco vPC pairs, cross-cabling is catastrophic. A single cable accidentally patched into a management switch or an access port on the wrong native VLAN will destabilize the entire bond.

Evaluating Discovery Options

When an interface begins dropping packets or VLAN tags fail silently, engineers generally lean on three approaches:

1. Remote-Hands Inspection

Asking an on-site technician to trace cables with a flashlight takes 45 to 90 minutes. In a cramped hot aisle, tired hands also risk snagging an adjacent fiber jumper or bumping a power whip.

2. Sniffing Multicast Frames with tcpdump

You can capture raw advertisement frames directly off the wire:

sudo tcpdump -nn -v -i eth0 -s 1500 -c 1 'ether proto 0x88cc'

This works if you are in a pinch, but it demands patience. Standard LLDP frames broadcast only once every 30 seconds. If packet loss is already flapping the link, sitting on a blocking packet capture slows triage down. Decoding raw TLVs over a laggy 9600-baud IPMI serial console is painful and error-prone.

3. Specialized Daemons: open-lldp vs. lldpd

Linux hosts typically run one of two Layer 2 discovery daemons:

  • open-lldp (lldpad): Designed primarily for Data Center Bridging (DCB) and Fibre Channel over Ethernet (FCoE). It is monolithic, difficult to script, and overly complex for general host discovery.
  • lldpd: Small, reliable, and active. It consumes under 12 MB of resident memory and supports IEEE 802.1AB LLDP alongside Cisco CDP (v1/v2), Extreme EDP, Foundry FDP, and Nortel SONMP. It features a fast interactive CLI and outputs clean JSON.

Automating Neighbor Discovery with lldpd

Running lldpd across your fleet removes guesswork. It runs as a lightweight user-space daemon, continuously indexes switch metadata, and answers the network team’s questions in less than two seconds.

Step 1: Install lldpd

Deploy the daemon using your distribution’s package manager:

# Debian, Ubuntu
sudo apt update && sudo apt install -y lldpd

# Rocky Linux, AlmaLinux, RHEL 9
sudo dnf install -y epel-release
sudo dnf install -y lldpd

Step 2: Configure Protocols and Start the Service

Stock installations listen only for basic LLDP. Enable Cisco Discovery Protocol (CDP) and Extreme Discovery Protocol (EDP) to handle heterogeneous network hardware seamlessly:

# On Debian/Ubuntu:
echo 'DAEMON_ARGS="-c -e"' | sudo tee /etc/default/lldpd

# On RHEL/Rocky Linux:
echo 'LLDPD_OPTIONS="-c -e"' | sudo tee /etc/sysconfig/lldpd

# Launch and enable the daemon
sudo systemctl enable --now lldpd

Step 3: Query Neighbors with lldpcli

After letting the daemon listen through one 30-second broadcast window, query the local interface table:

sudo lldpcli show neighbors

The output provides immediate physical context:

-------------------------------------------------------------------------------
LLDP neighbors:
-------------------------------------------------------------------------------
Interface:    eth0, via: LLDP, RID: 1, Time: 0 day, 00:01:22
  Chassis:
    ChassisID:    mac 00:50:56:bb:12:34
    SysName:      sw-leaf-01a.dc1.internal
    SysDescr:     Cisco Nexus Operating System (NX-OS) Software 9.3(8)
    Capability:   Bridge, on
    Capability:   Router, on
  Port:
    PortID:       ifname Ethernet1/14
    PortDescr:    db-prod-04:eth0 [Trunk to Prod Compute]
    TTL:          120
    MFS:          9216
  VLAN:
    VLAN:         100, pvid: yes
-------------------------------------------------------------------------------

Within seconds, you have confirmed four critical parameters:

  • Switch Chassis: sw-leaf-01a.dc1.internal (a Cisco Nexus 9000-series switch)
  • Physical Switch Port: Ethernet1/14
  • Port Native VLAN (PVID): 100
  • Max Frame Size (MFS): 9216 (verifying jumbo frame support along the link)

Step 4: Inspect Trunk and VLAN Tagging Details

If you suspect an unconfigured VLAN tag is dropping storage or cluster traffic, query extended port attributes:

sudo lldpcli show neighbors details

This command lists all VLAN IDs permitted on the trunk, Link Aggregation Control Protocol (LACP) partner states, and PoE power levels. If the server expects tagged frames on VLAN 250 but the upstream switch port only permits VLAN 100 untagged, you have pinned down the fault without logging into a single switch CLI.

Step 5: Exporting JSON to CMDB and Monitoring Pipelines

Manual queries resolve immediate crises, but automated data collection prevents them altogether. Use lldpcli with native JSON output for automation:

sudo lldpcli -f json show neighbors

Pipe that structured payload directly into jq to extract interface-to-switch mappings:

sudo lldpcli -f json show neighbors | jq '[
  .lldp[0].interface[]? // .lldp.interface[]? | {
    local_interface: (.name // keys[0]),
    switch_chassis: (.. | .SysName?.value? // empty),
    switch_port: (.. | .port?.id?.value? // empty)
  }
]'

The filter produces clean key-value records:

[
  {
    "local_interface": "eth0",
    "switch_chassis": "sw-leaf-01a.dc1.internal",
    "switch_port": "Ethernet1/14"
  },
  {
    "local_interface": "eth1",
    "switch_chassis": "sw-leaf-01b.dc1.internal",
    "switch_port": "Ethernet1/14"
  }
]

Shipping this payload to NetBox, AWX, or an internal inventory API turns passive switch advertisements into an automated, self-healing source of truth.

Production Caveats to Keep in Mind

  • Switch Transmit Policies: Enterprise switches frequently have LLDP disabled by default. On Cisco IOS, the switch requires lldp run globally. On Cisco NX-OS, it requires feature lldp. If the switch receives frames but never transmits them, your local neighbor table remains blank.
  • Hardened Read-Only Mode: In multi-tenant environments or DMZs, servers should not announce their hostnames or IP addresses to the entire Layer 2 broadcast domain. Pass -r in your daemon arguments to enforce listen-only mode. The host will ingest switch neighbor data without leaking its own identity onto the wire.
Share: