Virtual Routing and Forwarding (VRF)

This example attaches two physical interfaces to a VRF called vrf-blue:

  • configure eth0 in the default vrf
  • create the vrf vrf-blue
  • add eth1 and eth2 to the vrf vrf-blue
  • configure ip routing
interfaces:
  # default vrf
  eth0:
    addresses:
    - 198.51.100.2/31
    link:
      state: up
      kind: physical

  # "blue" vrf
  vrf-blue:
    link:
      state: up
      kind: vrf
      vrf_table: 10

  eth1:
    addresses:
    - 192.0.2.2/25
    link:
      state: up
      kind: physical
      master: vrf-blue

  eth2:
    addresses:
    - 192.0.2.129/25
    link:
      state: up
      kind: physical
      master: vrf-blue

routing:
  routes:
    # default route in default vrf
    - to: 0.0.0.0/0
      via: 198.51.100.1

    # default route in "blue" vrf
    - to: 0.0.0.0/0
      via: 192.0.2.1
      table: 10
{
  networking.ifstate = {
    enable = true;
    settings = {
      interfaces = {
        # default vrf
        eth0 = {
          addresses = [ "198.51.100.2/31" ];
          link = {
            state = "up";
            kind = "physical";
          };
        };

        # "blue" vrf
        vrf-blue = {
          link = {
            state = "up";
            kind = "vrf";
            vrf_table = 10;
          };
        };
        eth1 = {
          addresses = [ "192.0.2.2/25" ];
          link = {
            state = "up";
            kind = "physical";
            master = "vrf-blue";
          };
        };
        eth2 = {
          addresses = [ "192.0.2.129/25" ];
          link = {
            state = "up";
            kind = "physical";
            master = "vrf-blue";
          };
        };
      };
      routing = {
        routes = [
          # default route in default vrf
          {
            to = "0.0.0.0/0";
            via = "198.51.100.1";
          }

          # default route in "blue" vrf
          {
            to = "0.0.0.0/0";
            via = "192.0.2.1";
            table = 10;
          }
        ];
      };
    };
  };
}
ip address add 198.51.100.2/31 dev eth0
ip link set dev eth0 up
ip route add default via 198.51.100.1
ip link add name vrf-blue type vrf table 10
ip link set vrf-blue up
ip link set eth1 master vrf-blue up
ip address add 192.0.2.2/25 dev eth1
ip link set eth2 master vrf-blue up
ip address add 192.0.2.129/25 dev eth2
ip route add default via 192.0.2.1 table 10