| technology | networking - David Chapman
OSPF vs BGP: Which to Use?
Deciding between OSPF and BGP can be a difficult task, especially if you do not have to grapple with them on a daily basis. In order to pick the proper routing protocol, you have to understand their strengths, weaknesses, and the logic they employ. If so — most of the time — the decision makes itself.
With that though, many dynamic routing topologies tend to use both but in different segments of the network. The goal of this post is to help you understand the nature of each, when you would use them individually, and when you would use them together.
OSPF: Link-State Routing for Internal Use Only
OSPF is an Interior Gateway Protocol (IGP). As the name suggests, these are used for internal network routing. This would typically be between switches and routers in the same location. Sometimes OSPF is also used in Layer 2 connections between offices. OSPF is the most widely used but it is not the only choice. With that said, it is the most standardized IGP and that allows for optimal vendor interoperability. OSPF is primarily used for internal routing because it's a link-state routing protocol.
OSPF is based on Dijkstra's algorithm for finding shortest paths between two points. Basically, OSPF works on the concept of "areas." Most networks simply put all OSPF prefixes under an autonomous system into a single area numbered 0 for backbone, which means that each node has a full view of the topology. This allows each device to determine the best next hop for any particular destination. This full view does come with some downsides: greater memory and CPU resources being used. That's why OSPF is usually used for internal, rather than external, routing.
Being an IGP, the overhead of being a link-state protocol is minimized due to the smaller topology. Not to say they cannot get large but compared to the topology of internet routing, they are usually minimal. The benefit to being a link-state protocol is its ability to highly engineer traffic routing due to its in-depth understanding of the topology. Each router has a full view of how each router is connected. The downside is that many convergence scenarios require a full or partial table recalculation.
Today's routers and switches are usually insulated from this, but it does kick off a lot of work. Imagine if a full table needed to be calculated every time a new dynamic internet routing node was brought online. That scenario could cause some issues and is why OSPF is not suitable for internet routing.
Use BGP for Exterior Routing: Your Gateway to the Internet
BGP is an Exterior Gateway Protocol (EGP). Not to be confused with the "Exterior Gateway Protocol" which was the protocol used prior to BGP for internet routing. Protocols such as BGP are typically used for external network routing at the edge of your network. This may be internet edge routing or even MPLS/private network routing between facilities/organizations. Where your network interfaces with another provider is typically where an EGP gets implemented. BGP is the standardized EGP today.
BGP is sometimes classified as a distance vector protocol but, in reality, it is a path vector protocol. In contrast to link-state, which knows what the entire network looks like, distance vector learns paths. Each router learns paths by a neighbor exporting prefixes to it. Think of this as a traceroute. Because the distance vector learns this way, each router does not have a full view of the topology, only its perspective based on routes it has learned. Instead of using prefix information primarily, it uses AS numbers. One of the key metrics BGP uses is the shortest AS path from one location to another. The fundamentals of how it works are very similar otherwise.
Unlike OSPF, BGP does not use IP hops as its calculations. It uses the concept of Autonomous Systems, — simply a collection of subnets (or prefixes as they are usually called). Typically, these prefixes are all under control of one organization or operating unit. The originator of a prefix, or set of prefixes, notes its AS number on the prefixes. As each neighbor learns about those prefixes, it also appends its AS. At the end what you have is a path of AS numbers associated with a prefix.
With all other things equal, typically, the shortest AS path wins for externally learned routes. Because routing is based on an AS path, you will not see every BGP router on a BGP AS path. In more complex BGP implementations, configurations may be put into place to prefer certain override paths. Each vendor has different criteria for breaking a tie on routes. The alternative to breaking a tie would be to load balance or round robin between them — typically referred to as ECMP (Equal Cost Multipath). This is not always preferred at the edge and usually has to be manually enabled.
By default, BGP routers do not append the AS if it learned of the prefix through another BGP router with the same AS. The concept for determining this is internal BGP (iBGP) versus external BGP (eBGP). We will not get too deep on this but essentially iBGP routes are classified as prefixes learned from a neighbor BGP router with the same AS. Alternatively, eBGP prefixes are classified by learning them through a router with a different AS. BGP, as previously mentioned, is classified as a path-vector protocol because BGP attributes, including AS numbers, are stored instead of just IP/network hops. Other than that, it operates very similar to distance-vector protocols.
As an administrator, you typically hard code BGP peers into your configuration and have route maps to determine the prefixes you want to announce and in what manner. The lightweight nature of BGP does not carry the tax of having to know the entire topology nor the CPU tax of recalculating the topology every time a node is brought online. With that said, receiving full internet routes can reach limitations on some older hardware but holding a full topology view of every connected router would be even more burdensome.
BGP & OSPF
Now that we understand the basics of each protocol, how do we choose? The topology usually dictates the protocol but in some cases router licensing may as well. Some routers come with BGP in the base license while others may come with OSPF instead and require a premium license for the other. It then comes down to topology, assuming a proper budget. BGP vs. OSPF is kind of a misconception as they do not necessarily compete and have their own use cases. In many cases, they are used together.
In the datacenter, OSPF is usually the common protocol for internal routing between switches and/or routers. If it is not OSPF, sometimes you still see other IGPs like EIGRP. When that traffic needs to make its way to another organization or another AS, whether it be to an MPLS network with other sites or the internet, BGP usually comes into play.
Working Together: Route Redistribution
Some protocols will also announce directly connected routes to the topology as well as routes learned via the same protocol. When you have an OSPF topology internally but want to announce those routes to your MPLS and the provider supports BGP, the routes do not propagate by default. This is where route redistribution emerges.
On your edge device forming adjacency with the provider's BGP peer, you would configure OSPF and BGP. BGP would peer with the neighbor and OSPF would synchronize a full topology from its neighbors. You would then configure OSPF to redistribute routes learned from BGP into the OSPF topology and vice versa. Static routes typically need to be redistributed. Do not assume your local routing table, in entirety, makes it into the various routing protocols you've selected.