Policy-based routing

A router normally forwards on the destination alone; policy-based routing lets it weigh other things, such as a packet's source or length. PBR is a route map applied to packets as they arrive on an interface, so the routing table never shows it. Packets the router makes itself need local PBR, and a policy next hop is used only while it exists.

In Daveth Pro (Network)12 min video · lab: Separate paths

By , founder of Daveth · Updated

Opening frame of the video walkthrough of Policy-based routing

Watch this lesson

12 min · Video walkthrough

In Daveth Pro (Network)

The full 12 min walkthrough is in Daveth Pro (Network). See what the plan opens

Upcoming practical lab

Lab R9: Separate paths

What the lab covers

What this page answers

How do you configure policy-based routing (PBR)?

Four steps: create a route map, match the packets with an ACL or by length, set the next hop, and apply the map to the inbound interface with ip policy route-map. The routing table does not change. Read it in “Building it: four steps and a route map you already know”

Does policy-based routing apply to packets the router sends itself?

No. PBR examines packets as they are received on the interface it is applied to; packets the router originates need local PBR, ip local policy route-map in global configuration. Read it in “The router's own packets”

What happens when a PBR next hop is down?

PBR checks that the next-hop address exists before forwarding to it. With several next hops, the next one is tried; if none exists, the packet is routed normally by the routing table. Read it in “When the policy's road is closed, and when it waits for the table”

One application, a different road

Can one application take a different road from everyone else? Yes.

Start with what a router normally does. It makes its forwarding decision on the packet's destination address. That's the whole question it asks: where is this going? Some situations want a router to weigh other things, such as a packet's length or its source address. Policy-based routing, PBR, is the tool for that: it forwards packets conditionally, on characteristics other than the destination IP address.

What that buys you. You can route by protocol type, so ICMP, TCP and UDP can take different paths. You can route by source address, destination address, or both. And you can assign different paths to the same destination manually, depending on how much latency, link speed or utilisation a specific kind of transient traffic can put up with.

It isn't free. Every one of those decisions is a hand-written exception, and PBR has three costs. An administrative burden as the network grows. A lack of network intelligence: the policy doesn't react the way a routing protocol does; it does what you wrote. And troubleshooting complexity, which is most of this lesson.

Here's the network. Five routers running OSPF. R1 has 10.1.1.0/24 behind it and R5 has 10.5.5.0/24. Between them, R2 and R4 are joined directly by 10.24.1.0/24, and R3 sits above them on a detour: R2 to R3 over 10.23.1.0/24, R3 to R4 over 10.34.1.0/24. R1 meets R2 on 10.12.1.0/24, and R4 meets R5 on 10.45.1.0/24. Traffic between R2 and R5 crosses 10.24.1.0/24, because going through R3 adds the cost of a second link. A traceroute from 10.1.1.1 to 10.5.5.5 proves it: 10.12.1.2, 10.24.1.4, 10.45.1.5.

Network drawing: OSPF on every router, 10.1.1.0/24, 10.12.1.0/24, 10.24.1.0/24, 10.45.1.0/24, 10.5.5.0/24, 10.23.1.0/24, 10.34.1.0/24, LAN, R1, R2, R3, R4, R5, Gi0/1, Gi0/2, Gi0/3

The short road is R2 to R4 direct; the detour through R3 crosses one more link, so OSPF never picks it.

The requirement: traffic from 10.1.1.0/24 to 10.5.5.0/24 must go through R3, on R2's decision. Everybody else keeps the short road.

Building it: four steps and a route map you already know

You've met the tool already. In the route-map lesson, a route map was a stack of sequences, each permit or deny, tried in order; the first sequence whose match lines all hold decides, and there's an implicit deny under the last one. PBR uses exactly that structure. What's new is who feeds it: packets instead of routes.

PBR's configuration is a route map with match and set statements, attached to the inbound interface. Four steps:

  1. Create the route map: route-map route-map-name [permit | deny] [sequence-number].
  2. Say which packets. By packet length, match length minimum-length maximum-length, or by the packet's IP address fields through an ACL, match ip address {access-list-number | acl-name}.
  3. Say where they go: set ip [default] next-hop ip-address [... ip-address], one next hop or several. Hold the default keyword for later.
  4. Attach it to the inbound interface with ip policy route-map route-map-name.

Look at step 2's ACL. When an ACL was pointed at routes, its fields had to be bent to fit. Here it's back at its original job, classifying packets, so an extended ACL's source and destination mean the packet's source and destination.

Here's the whole thing on R2:

R2
ip access-list extended ACL-PBR
 permit ip 10.1.1.0 0.0.0.255 10.5.5.0 0.0.0.255
!
route-map PBR-TRANSIT permit 10
 match ip address ACL-PBR
 set ip next-hop 10.23.1.3
!
interface GigabitEthernet0/1
 ip address 10.12.1.2 255.255.255.0
 ip policy route-map PBR-TRANSIT

One sequence. Nothing for the traffic that should keep using the routing table, and none is needed. Set that beside route maps for routes, where a map with one permit sequence throws away everything it doesn't match. For packets, the implicit deny throws nothing away: a packet no sequence claims is simply routed normally.

The trace from 10.1.1.1 to 10.5.5.5 after the policy: 10.12.1.2, 10.23.1.3, 10.34.1.4, 10.45.1.5. It goes through R3, the longer way, and leaves 10.24.1.0/24 alone.

The table that didn't change

Here's R2's routing table for 10.5.5.5, after the policy is applied:

R2# show ip route 10.5.5.5
Routing entry for 10.5.5.0/24
 Known via "ospf 1", distance 110, metric 3, type intra area
 Last update from 10.24.1.4 on GigabitEthernet0/2, 00:12:37 ago
 Routing Descriptor Blocks:
 * 10.24.1.4, from 10.45.1.5, 00:12:37 ago, via GigabitEthernet0/2
     Route metric is 3, traffic share count is 1

10.24.1.4. The short road. The trace went through 10.23.1.3, and the routing table still says 10.24.1.4. Applying PBR doesn't modify the routing table; conditional forwarding happens outside the RIB's view and doesn't appear in show ip route.

The routing table isn't lying. Nobody asked it.

Why it stays out: a policy isn't universal for all packets, so it doesn't go into the RIB. Every packet to 10.5.5.0/24 that the policy doesn't claim still uses 10.24.1.4, and the table is right about those. The cost lands on you: the routing table shows the next hop the routing protocol learned and never the one the policy uses for its traffic, and that often complicates troubleshooting. So PBR doesn't modify a router's routing table: it happens to traffic received on a specific interface, which is then conditionally forwarded.

The forwarding picture from the start of the course still holds: the router looks for the longest match in its routing table, and CEF's FIB is built from that table. PBR sits beside both. So the proof that a policy works is the traceroute, not a table; show ip cef exact-route, which shows the interface and next hop for one source and destination pair, may or may not reflect a policy, and we haven't confirmed which.

You've seen a hint of this before. In the OSPF tickets, R3 was forwarding packets for 192.168.1.0/24 to R2, so there had to be an entry in its routing table "or policy-based routing" at work. That "or" was this lesson.

Which packets meet the policy: the door

Here's the rule that matters most. Packets are examined for PBR as they're received on the router's interface. The policy is attached to the inbound interface, and a packet meets it by coming in through that interface.

So a policy works like a checkpoint at one door of the building: packets coming in through that door meet it. Where the picture stops: a checkpoint can turn people away; a packet this policy doesn't claim isn't stopped. It goes on to the routing table.

Network drawing: A packet meets a policy only through the door it came in by. The routing table's answer is shown under the result, and the policy never changes it.
10.1.1.1 to 10.5.5.5 arrives on Gi0/1, where R2's policy applies: sequence 10 claims it, and the packet goes to 10.23.1.3. The routing table still says 10.24.1.4; the policy never changes it.

Only the first takes the R3 road. The second meets the checkpoint and isn't claimed: ACL-PBR wants a source in 10.1.1.0/24, so the routing table sends it to 10.24.1.4. The third never meets the checkpoint at all. It came in through Gi0/2, where there's no policy, so R2's table sends it on to R1, 10.12.1.1. R4 and R5 chose its path to R2, not R2's policy.

That third one surprises people. The policy bent one direction of the conversation. The replies take the routing table's path, and if the requirement was "this traffic uses R3", half of it doesn't. Covering the return direction means a policy on the router where the replies arrive, at the door they arrive through.

The router's own packets

That leaves one kind of packet the door can't see: one that didn't come in through any interface, because the router made it.

Packets the router originates aren't policy routed. There's a separate feature for that, local PBR, applied to the whole router in global configuration with ip local policy route-map route-map-name. So: packets are examined as they arrive on an interface, and local policies can also pick out traffic that starts on the router. Back on R2, a ping that R2 itself sends to 10.5.5.5 ignores Gi0/1's policy and takes 10.24.1.4, and it would even if its source were one ACL-PBR permits.

A second network for this. R1 is managed through an interface kept for out-of-band management, on 172.16.14.0/24. OSPF runs on every router and interface except R1's Gi0/2, the management one. ACLs throughout the network make sure traffic for 172.16.14.0/24 only crosses 172.16.14.0/24, and management traffic must never use 10.12.1.0/24 or 10.23.1.0/24. The layout: R1's Gi0/1 to R2 on 10.12.1.0/24, R2 to R3 on 10.23.1.0/24, R3's Gi0/1 to the network management server on 10.33.33.0/24; and the management ring, R1's Gi0/2 to R4 on 172.16.14.0/24, R4 to R5 on 172.16.45.0/24, R5 to R6 on 172.16.56.0/24, R6 to R3's Gi0/3 on 172.16.36.0/24. R1's table has every remote network, 10.33.33.0/24 included, via 10.12.1.2 on Gi0/1. R2 has an ACL called LOG: permit OSPF, deny anything from or to 172.16.14.0/24 with logging, permit the rest with logging.

Network drawing: OSPF everywhere except R1 Gi0/2, 10.12.1.0/24, 10.23.1.0/24, 10.33.33.0/24, 10.33.33.3, ACL LOG on R2, 172.16.14.0/24, .1, .4, 172.16.36.0/24, management ring, out of band, 172.16.45.0/24, 172.16.56.0/24, R1, R2, R3, NMS, R4, R5, R6, Gi0/1, Gi0/2, Gi0/3

R1 has two ways out: Gi0/1 into the OSPF network, and Gi0/2 into the management ring, where it runs no OSPF.

Because R1 doesn't advertise 172.16.14.0/24 into OSPF and R4 does, everyone else reaches that network through R4, on the right interface. R1's own traffic is the problem.

Before you look at the next output: R1 runs a traceroute to the management server, 10.33.33.3, sourced from its management address, 172.16.14.1. Which interface does it leave by?

R1# traceroute 10.33.33.3 source 172.16.14.1
Type escape sequence to abort.
Tracing the route to 10.33.33.3
VRF info: (vrf in name/id, vrf out name/id)
 1 10.12.1.2 !A  *  !A
R2# 04:40:16.194: %SEC-6-IPACCESSLOGP: list LOG denied udp
172.16.14.1(0) > 10.33.33.3(0), 1 packet

Gi0/1. The first hop is R2, 10.12.1.2, and R2's ACL denies it and logs the denial: the !A is R2 refusing it, as the log line shows. A source address doesn't choose an exit; the routing table does. R1 learns every route through Gi0/1 and none through Gi0/2, so even traffic sourced from Gi0/2 leaves by Gi0/1. A static route wouldn't fix it cleanly: it could pull transit traffic across the out-of-band networks, 172.16.0.0/16, which nobody wants.

The fix is a local policy on R1 that changes the next hop to 172.16.14.4, R4, and only for traffic R1 sources from 172.16.14.0/24:

R1
ip access-list extended ACL-MANAGEMENT-LOCAL-PBR
 permit permit ip 172.16.14.0 0.0.0.255 any
!
route-map LOCAL-PBR permit 10
 match ip address ACL-LOCAL-PBR
 set ip next-hop 172.16.14.4
!
ip local policy route-map LOCAL-PBR

Read it carefully, because it has two slips. permit is printed twice, and the ACL is defined as ACL-MANAGEMENT-LOCAL-PBR while the route map matches ACL-LOCAL-PBR. As printed, the route map calls a list that doesn't exist, and what IOS does then we haven't confirmed. The result below is what one ACL named the same in both places would give, permit ip 172.16.14.0 0.0.0.255 any. Type it with one name, and don't rely on what IOS does with the other.

With the local policy in place, the trace from 172.16.14.1 to 10.33.33.3 goes 172.16.14.4, 172.16.45.5, 172.16.56.6, 172.16.36.3: the out-of-band networks all the way.

Proof of the decision itself comes from debug ip policy, which shows policy routing's decisions as traffic that matches the policy passes. R1 pings the server from 172.16.14.1:

R1# debug ip policy
Policy routing debugging is on
R1# ping  10.33.33.3 source 172.16.14.1
! Output omitted for brevity
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.33.33.3, timeout is 2 seconds:
Packet sent with a source address of 172.16.14.1
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 7/7/7 ms
R1#
01:47:14.986: IP: s=172.16.14.1 (local), d=10.33.33.3, len 100, policy match
01:47:14.987: IP: route map LOCAL-PBR, item 10, permit
01:47:14.987: IP: s=172.16.14.1 (local), d=10.33.33.3 (GigabitEthernet0/2), len 100, policy routed
01:47:14.988: IP: local to GigabitEthernet0/2 172.16.14.4

Read the ping first. Five echoes, each 100 bytes, sourced from 172.16.14.1, and all five came back: Success rate is 100 percent (5/5).

Now the debug. Look at the word local beside the source: we read it as the router telling you the packet started here, so it met the local policy and no interface's. len 100 is the length of the ping's 100-byte echo, so these lines describe the very packets the ping just sent. Read top to bottom: matched, which sequence and its action, routed by policy, and out which interface to which next hop.

When the policy's road is closed, and when it waits for the table

A policy names a next hop. What if that next hop isn't there?

PBR checks that the next-hop address exists, and only then forwards the packet to it. You can give more than one: if the first next hop isn't in the RIB, the next one can be used. And if none of them exists in the routing table, the packets aren't conditionally forwarded. They go by the routing table, 10.24.1.4 here, and arrive.

Nothing in that rule warns you. If the requirement was "never the short road", that's a policy breach nobody was told about; if the requirement was "prefer R3", it's exactly what you wanted.

What counts as "existing" isn't spelled out beyond "in the RIB" and "in the routing table". Whether a next hop reachable only through a remote route counts, or only one on a connected subnet, we haven't confirmed. Notice what the check doesn't do, either way: a next hop whose subnet is in the table may still be a dead neighbor, and nothing in this check would notice.

Now the keyword you held. Add default and the order flips: the route map's next hop is used only if the destination doesn't exist in the RIB; if a viable route exists there, that route's next hop is used. Without default, the policy goes first and the table is the fallback. With it, the table goes first and the policy is the fallback. One word in the configuration, opposite priorities.

One open question. The rule says "if the destination address does not exist in the RIB", and a later output says "no specific route". Neither says whether a default route counts as one; "specific" leans toward no, and we haven't confirmed it.