← All posts

Learn BGP and Anycast with a Lab You Can Break

How BGP and anycast decide where Internet traffic goes, how one change in an announcement moves it, and a lab that you can run on your laptop, break, and rebuild in a minute.

When you type a URL, your request goes to a server. But which server? Big services like CDNs, DNS resolvers and cloud load balancers use one IP address in many locations at the same time. Your packets go to the nearest location, and you never notice. You did not configure anything. Somewhere, a group of routers quietly made this decision for you.

I knew the theory of BGP and anycast. But theory did not show me the details: how an operator or service provider actually controls where traffic goes, and what happens when something breaks. Articles helped, but nobody learns to swim from a book. I wanted to see the routes, change them and, of course, break them.

So I built a lab, and it is on GitHub: github.com/antonu17/lab-network-bgp.

In this post, I explain the basics of BGP and anycast, show how a small change in a BGP announcement can move Internet traffic, and share the lab. You can run the lab on your laptop, break it, and start again from zero in one minute. No change requests, no angry users.

The Internet is a network of networks

The Internet is not one network. It is a large set of independent networks that connect to each other. Each network has its own operator and its own routing policy. Such a network is called an Autonomous System (AS). Each AS has a unique number, the ASN.

The networks must tell each other which IP addresses they can deliver traffic to. They use the Border Gateway Protocol (BGP) for this. Each AS announces its IP prefixes (address ranges in CIDR notation, like 192.0.2.0/24) to its neighbors. The neighbors forward these announcements to their own neighbors, and so on. Each AS adds its ASN to the announcement when it sends the announcement on. The result is the AS path: a list of all the networks between you and the destination. So BGP is basically gossip, but very well organized: “Pssst! I heard that 192.0.2.0/24 is over there, behind these three networks.”

For a longer introduction, read the “Meet BGP” section of the Cloudflare blog post about the Facebook outage of October 2021. The outage also shows how important BGP is. Facebook withdrew the BGP routes to its own DNS servers. After that, no network on the Internet knew how to reach them, and Facebook, Instagram and WhatsApp were unavailable for about six hours. A few BGP announcements fewer, a few billion unhappy users more.

Anycast: one address, many locations

Usually, one IP address identifies one host. This is unicast. With anycast, many hosts in different locations have the same IP address. Each location announces the same prefix with BGP.

Now each network on the Internet receives many paths to the same prefix. BGP selects the best path, and traffic goes to the nearest location. “Nearest” here means “nearest from the point of view of BGP”, not the shortest distance on a map. BGP has never seen a map. If one location fails, its announcement disappears. Then BGP selects the next best path automatically, and traffic goes to another location.

Anycast gives three important properties:

  • Low latency. Users connect to a location near them.
  • High availability. When a location fails, traffic moves to the other locations.
  • Distribution of load and attacks. A DDoS attack does not hit one location. Each location receives only the part of the attack that comes from its area.

To learn more, read A Brief Anycast Primer on the Cloudflare blog.

Who speaks BGP: routing daemons

BGP is only a protocol, a set of rules for the messages between two routers. Something must run it. On network hardware from vendors like Cisco or Juniper, the vendor’s operating system runs BGP. On Linux, a normal process runs it: a routing daemon.

If you work with Linux servers, you already know half of the setup. The Linux kernel forwards packets with its routing table, the table that ip route shows. The kernel does not decide which BGP routes go into this table. A routing daemon does this:

  1. It opens BGP sessions (TCP port 179) to its neighbors, which are other routers.
  2. It receives the neighbors’ routes and announces its own routes to them.
  3. It selects the best path for each prefix.
  4. It writes the best paths into the kernel routing table.

Network engineers call the daemon the control plane and the kernel the data plane. The daemon decides, and the kernel forwards. A daemon is like any other service that you operate: it has a configuration file, a log, a CLI and a systemd unit. And, like any other service, it can fail at 3 a.m.

The two most popular open-source routing daemons are FRR and BIRD:

  • FRR (Free Range Routing) is a Linux Foundation project. It supports BGP and many other routing protocols, for example OSPF and IS-IS. Its CLI, vtysh, looks like the CLI of a Cisco router. Network engineers feel at home, and everybody else learns to type show a lot.
  • BIRD (BIRD Internet Routing Daemon) comes from CZ.NIC, the Czech domain registry. Its configuration is a small programming language with powerful route filters. Yes, your router configuration can have if-statements now. Many Internet exchange points use BIRD for their route servers.

Both daemons implement the same protocol, so an FRR router and a BIRD router can be BGP neighbors without problems. Only their configuration and their CLI are different.

How BGP selects a path

A router often receives many paths to the same prefix: one from each neighbor that knows a way to it. The routing daemon must select one best path. It compares the attributes of the paths step by step. The first step that finds a difference selects the winner.

The full list has about a dozen steps, and it is a little different in each BGP implementation. You do not need all of them to start. These four explain everything in this post:

  1. Highest local preference. A value that all routers inside one AS share. With it, an operator selects the exit from their network.
  2. Shortest AS path. Fewer networks in the path are better.
  3. eBGP over iBGP. A BGP session between two different ASes is eBGP (external). A session between two routers in the same AS is iBGP (internal). A path from an eBGP neighbor is better.
  4. Tie-breakers. If all of the above are equal, the router uses simple rules, for example the oldest path or the lowest router ID. The router ID is a number that identifies each BGP router.

The other steps, such as weight, origin and MED, are for special cases. When you need them, the FRR documentation has the full list.

Outbound and inbound traffic

Steps 1 and 2 are the most important for traffic engineering. Imagine that you operate an AS, and your AS has BGP sessions with several other ASes. You can control the paths of your traffic in two directions.

Outbound traffic is the traffic that leaves your AS. This direction is easy. Your own routers select the exit, so you decide. You set a higher local preference on the routes from the peer that you prefer, and all your routers send traffic through that peer. If that peer fails, the routes from the other peers are still available, and traffic moves to them.

Inbound traffic is the traffic that comes into your AS. This is where the fun starts. For example, you have two peers, and you want all traffic to one of your address ranges to come through peer A. Peer B must carry it only as a backup. But the routers that make this decision are not yours. They are in other ASes, maybe on the other side of the world, and their operators apply their own policies. You cannot set their local preference. You can send them an email, of course. Good luck with that.

The only thing that you control is your BGP announcements: what you tell each peer about your address ranges. So you must change your announcements in a way that makes the other ASes select peer A themselves.

“Fake” hops: AS path prepending

Remember step 2: the shortest AS path wins. So you make the path through peer B longer on purpose. In your announcements to peer B, you add your own ASN two or three more times. This is called AS path prepending.

For example, a data center in AS 1443 normally announces this path:

1443

With two prepends toward peer B, peer B receives:

1443 1443 1443

This is the same network, but now the path looks two hops longer. It is a small lie, and the whole Internet accepts it. Other networks now usually prefer the path through peer A. The link to peer B becomes a backup link: in most cases, it carries traffic only when the other paths fail.

This is a simple tool, but the results are not always easy to predict. The decision is made in networks that you do not control, and each network applies its own policy first. One prepend can move all traffic. Two prepends can cause a tie, and then a tie-breaker like the age of the path decides. Three prepends can move traffic to a location that you did not expect. You must see it to understand it.

Containerlab: a network on your laptop

To experiment with routing, you need routers, links and hosts. Physical routers are expensive and loud, and they do not look good in a living room. Virtual machines are slow and use a lot of memory. Containerlab solves this problem.

With Containerlab, you describe a network topology in one YAML file: the nodes, their images, and the links between them. Each node is a container. Each link is a virtual Ethernet pair between two containers. One command creates the full network, and one command removes it:

clab deploy
clab destroy

Containers start in seconds and use very little memory. So you can run a topology with 20 routers and servers on a laptop. Your laptop fan may have an opinion about it, but it works. Containerlab runs on Linux. On macOS, you can use a Linux VM (for example, with Lima) or Docker Desktop.

The lab: what I built to learn

Nothing teaches better than a good hands-on experiment. So I designed a small Internet. It is much smaller than the real one, but it has all the parts of an anycast setup.

Detailed topology of the lab. Data Center #1 and Data Center #2 are in AS 1443. Each has three servers in a private AS, two top-of-rack routers in another private AS, and one edge router. The three transit providers AS 101, AS 102 and AS 103 connect them, and each provider has one user host. Every link shows its interface names and its IPv4 and IPv6 addresses, and each server lists the anycast prefixes that it announces.
The lab in detail: every node, link, interface and address. Select the diagram to open it at full size.

The lab has:

  • Two data centers. Both data centers are in AS 1443. Each data center has three servers, two top-of-rack routers, and one edge router that connects to the Internet.
  • Three transit providers. AS 101, AS 102 and AS 103 form the backbone. DC1 connects to AS 101 and AS 102. DC2 connects to AS 102 and AS 103. AS 102 has three routers and also connects to AS 101 and AS 103.
  • Eyeball hosts. One “user” in each transit provider. These hosts send requests to the anycast addresses. (Network engineers really call users “eyeballs”. Do not take it personally.)

The lab uses both routing daemons from above. The servers and the top-of-rack routers run BIRD. The edge routers and the transit routers run FRR. So you learn both CLIs, and you see that they work together without problems.

The servers also run BGP. Each server announces its anycast addresses to the two top-of-rack routers above it. This is a common pattern in data centers: a server that stops BGP or fails also stops its announcements, and the network then stops sending traffic to it.

All servers announce two types of anycast prefixes, for IPv4 and IPv6:

  • Global anycast. All six servers in both data centers announce 1.1.1.0/24, 2.2.2.0/24 and fc00:aa::/48. The two IPv4 prefixes are real public addresses. The lab borrows them only inside its own network. Never announce them on a real network.
  • DC-local anycast. Two servers in one data center announce a prefix, and the prefix stays inside the data center.

Each server has a web server that returns its own hostname. So a simple curl shows which server answered your request.

Prepending in action

The lab already uses the trick from above. Both data centers add two prepends when they announce their prefixes to AS 102. This makes the direct links to AS 102 backup links.

Look at what router2.as102 receives for 1.1.1.0/24:

$ docker exec -it clab-bgp-router2.as102 vtysh -c "show ip bgp 1.1.1.0/24 longer-prefixes"
BGP table version is 73, local router ID is 10.102.254.2, vrf id 0
Default local pref 100, local AS 102
Status codes:  s suppressed, d damped, h history, u unsorted, * valid, > best, = multipath,
               i internal, r RIB-failure, S Stale, R Removed
Nexthop codes: @NNN nexthop's vrf id, < announce-nh-self
Origin codes:  i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
 *>  1.1.1.0/24       10.102.7.2                             0 103 1443 65200 65201 i
 * i                  10.102.3.1                    100      0 101 1443 65100 65101 i
 *                    10.102.6.2                             0 1443 1443 1443 65200 65201 i

Displayed 1 routes and 80 total paths

The next hops are the neighbors of router2.as102:

Next hop Neighbor Path
10.102.7.2 router1.as103 Through AS 103. This is the best path: it wins at step 3, eBGP over iBGP.
10.102.3.1 router1.as102 Through AS 101, learned from router1.as102 over iBGP. Its AS path has the same length as the path through AS 103, so it loses at step 3.
10.102.6.2 router1.dc2 Direct from Data Center #2, with two prepends. Its AS path is longer, so it loses at step 2.

On paper, the direct link is the best one. BGP disagrees, because its path is longer. So AS 102 sends its traffic to the data centers through AS 101 and AS 103. The third router in AS 102 receives two paths with the same length and uses both. This is ECMP (Equal-Cost Multi-Path): the router spreads connections across paths that are equally good. So the users of AS 102 go to both data centers. You can see this with curl:

$ docker exec clab-bgp-eyeball.as102 curl -s http://2.2.2.2/
server3.dc1
$ docker exec clab-bgp-eyeball.as102 curl -s http://2.2.2.2/
server1.dc2

One more detail in the router output above: each AS path ends with 65100 65101 or 65200 65201. These are private ASNs. Inside each data center, the servers and the top-of-rack routers use them for their BGP sessions, and the edge router passes them on to the transit providers. Is it correct to show them to the Internet? That is one of the tasks.

Share it: a lab that you can break

I learned a lot when I built this lab. I want to share that experience, not only the result. So the lab is on GitHub at github.com/antonu17/lab-network-bgp. It is reproducible and disposable:

git clone https://github.com/antonu17/lab-network-bgp.git
cd lab-network-bgp
docker build --target base --tag node-base .
docker build --target bird --tag node-bird .
docker build --target frr --tag node-frr .
sudo clab deploy

After a few seconds, you have 20 nodes and all BGP sessions are up. A script pings all addresses from all nodes, so you can make sure that the network is fully operational. If you break something, destroy the lab and deploy it again.

The lab has two sets of tasks.

Configuration tasks. You get a requirement and you change the routing to satisfy it. For example:

  • Peering partners complain about too many prefixes. Make sure that the data centers do not announce prefixes longer than /24 (IPv4) or /48 (IPv6), without loss of connectivity.
  • Move all requests from AS 102 users to 2.2.2.0/24 to Data Center #2. But you cannot change the configuration of the transit providers. Users from AS 101 must still use Data Center #1.
  • Make the traffic to one anycast prefix enter the servers through one top-of-rack router, and the traffic to the other prefix through the other router.

Troubleshooting tasks. The tshoot branch has a set of problems that I put in on purpose. This is the final boss of the lab: no hints, no health bar, and the only checkpoint is sudo clab deploy. A server does not receive anycast traffic. Requests always go to the same server. Traffic goes to the far data center, not to the near one. You must find the cause with birdc, vtysh, ip, ping and traceroute, and fix it. Do not just compare the configuration files. It works, but then you learn how to use diff, not BGP.

The tasks do not need a lot of configuration. Usually, the solution is a few lines. The difficult part is to understand why the network behaves the way it does.

Conclusion

BGP and anycast are not only for network engineers at large ISPs. They are the foundation of the services that people use every day:

  • CDNs like Cloudflare use anycast to send each user to the nearest location and to absorb DDoS attacks.
  • Public DNS resolvers like Cloudflare’s 1.1.1.1 and Google’s 8.8.8.8 are anycast addresses. You use the same address everywhere in the world, and the nearest location answers. Yes, the lab borrows 1.1.1.0/24 for its global anycast prefix.
  • Cloud providers use anycast for global load balancers. Google Cloud’s global load balancing and AWS Global Accelerator give you one IP address that works in many regions.

And when BGP goes wrong, large parts of the Internet stop. The Facebook outage is the best example, and also a good reason to make your mistakes in a lab first.

You do not need access to a real network to learn this. You need a laptop, a few containers, and a desire to break things. Clone the lab, deploy it, and try the first task. Then try to break it in a way that I did not think of, and tell me about it.

Have fun!