Introducing bgpls: watching a network backbone change with Swift

Hi everyone,

I started RouteObjects because I wanted to build more of the tools we use in network engineering with Swift. I’ve just released bgpls, an open-source BGP Link-State client. It’s both a command-line tool and a Swift library you can use in your own server application.

If BGP Link-State is new to you, think of it as a way for a router to share its map of the network and tell you when that map changes. In the included lab, you can bring down one backbone link between two virtual routers, p1 and p2. That link appears on the map twice, once in each direction. When it goes down, your application receives two withdrawals. Bring the link back up and both directions reappear.

On the Swift side, topology changes arrive as an AsyncStream. In my application, one task runs the BGP session while another watches for changes. Here’s the watcher:

let changes = await session.ribChanges()
for await change in changes {
    print(change.watchLine(showAttributes: true))
}

Download the 75-second bgpls demo from iCloud Drive. At 0:45, look at the terminal on the right to see the two withdrawals, p1 → p2 and p2 → p1. At 0:53, both directions reappear in the same BGP session.

The project uses SwiftNIO for the BGP session and NIO Extras for packet capture. The CLI uses Swift Argument Parser and the Unix signal support from Swift Service Lifecycle.

I also used eight value types from swift-cidr. They made bgpls easier to write and review because the types match the concepts a network engineer works with:

  • IPAddress and IPNetwork distinguish an interface address such as 192.0.2.7/24 from the network prefix 192.0.2.0/24. PrefixLength gives the /24 its own type and enforces the valid range for IPv4 or IPv6.
  • AnyIPAddress and AnyIPNetwork let the same API work with either address family while preserving the distinction between an address and a network.
  • IPEndpoint and Port describe where to open the TCP connection. AutonomousSystemNumber identifies the local and peer routing domains when bgpls establishes the BGP session.

If you’re curious about the network control plane, the hands-on lab walkthrough takes you from starting the virtual routers to watching that first link go down and come back up. I’d love to hear what you’d build on top of that stream of changes.

2 Likes