In BGP, a router receives prefix announcements via eBGP. If you are multi-homed or peer, you will receive announcements for one and the same prefix from multiple sources. Out of these multiple announcements, a router has to select one announcement as best.
The decision which prefix announcement is best has to meet the following criteria:
- Only one single path for each prefix is needed (and wanted)
- The decision has to be based on attributes of the BGP announcement
- The decision has to be deterministic (with the same parameters, the decision is always the same)
Below, you can find an example of the BGP routing algorithm according to RFC4271 that explains how your router chooses which prefix to take.
- Checks if next hop is reachable
- Chooses route with the highest Local Preference (this is where you prefer peering over upstream)
- Prefers the route with the shortest AS path
- Prefers the route with the lowest origin attribute
- Prefers the route with the lowest MED value
- Prefers routes received from eBGP over iBGP
- Prefers the nearest exit from your network (in terms of your internal routing protocol)
- Implementation dependent: Prefers older (= more stable) routes
- Prefers routes learned from the router with lower router ID
- Prefers routes learned from the router with lower IP address
*most important rules are bold; implementations are vendor-specific
The router starts selecting a best path by checking if the BGP announcement is valid. Only paths where the next hop is reachable are taken into consideration; if the next hop is not reachable, the announcement is discarded.