Skip to content

ElementaryLesson 9 of 9

Project: a single site ​

You build a router and a switch for a home or a small office. You start from Circuit's single-site example, a working project of exactly this network, and make it your own.

The site ​

  • Three VLANs: mgmt (10) for logging in to the devices, home (20) for your own devices, and guests (30) for visitors, kept off the other two.
  • The switch has a port for each device on the site, each in its VLAN, and a trunk to the router that carries all three.
  • The router has the internet connection on its port 1g-1. For each VLAN it is the gateway and the DHCP server. Its firewall keeps guests in their place, and NAT shares its one public address.

The switch only moves frames inside each VLAN. Everything that crosses from one VLAN to another, or to the internet, goes through the router. So the addresses, the firewall, and NAT are on the router. That is Layers at work: the switch works at layer 2, the router at layer 3.

Start the project ​

Install Circuit, then run circuit new my-network. It asks a few questions and writes an empty project. Copy the example's files into it:

text
my-network/
  circuit.ts             the network: its sites, and the checks
  sites/home/site.ts     what both devices share
  sites/home/router.ts
  sites/home/switch.ts
  lib/checks.ts          a check of your own
  .circuit/secrets       the devices' passwords, encrypted

Each device is one file, and the file is the whole truth, as in Writing a network down. Read them in this order.

The site file: site.ts ​

ts
// The site's facts that both devices write. Change them to your own.

export const home = literal({
    timezone: "Asia/Jakarta",
    addresses: {
        /** The management network. Only addresses in it may log in to the devices. */
        management: "192.168.10.0/24",
        router: "192.168.10.1/24",
        switch: "192.168.10.2/24",
    },
    vlans: {
        mgmt: { id: 10, description: "Management" },
        home: { id: 20, description: "Home" },
        guests: { id: 30, description: "Guests" },
    },
});

The VLANs are written once, by name, and both devices take the same ones, so the switch and the router can never disagree about which number guests is (VLANs). management is the one network allowed to log in, and router and switch are each device's address on it (Keeping the keys). Change these to your own first.

The switch: switch.ts ​

ts
// The switch: a port for each device on the site, in the VLAN it belongs to.

import { as64500 } from "../../circuit";
import { home } from "./site";

export default as64500.device({
    name: "switch",
    site: "home",
    role: "Switch",
    platform: "vrp",
    model: "CE6855-48S6Q-HI",
    connection: { host: "192.168.10.2", user: "operator" },

    system: {
        timezone: home.timezone,
        ntp: { servers: ["192.168.10.1"] },
        users: { operator: { role: "admin", password: secret("SWITCH_PASSWORD") } },
    },
    management: { allow: [home.addresses.management], ssh: {} },

    interfaces: {
        ethernet: {
            "10g-1": {
                description: "router sfp28-1",
                trunk_vlans: ["mgmt", "home", "guests"],
                link: { device: "router", port: "25g-1" },
            },
            "10g-2": { description: "Desk", access_vlan: "home", stp: { edge: true } },
            "10g-3": { description: "Living room", access_vlan: "home", stp: { edge: true } },
            "10g-4": { description: "Guest access point", access_vlan: "guests", stp: { edge: true } },
        },
        vlan_interface: {
            mgmt: { vlan: "mgmt", addresses: [home.addresses.switch] },
        },
    },
    switching: {
        vlans: home.vlans,
        stp: {},
        lldp: true,
    },

    routing: {
        static_routes: [{ prefix: presets.default_routes.ipv4, next_hop: "192.168.10.1" }],
    },
});
  • 10g-1 is the trunk to the router. It carries all three VLANs, and link says which port is at the other end of the cable. So validate can check that both ends carry the same ones (Trunks). Its description gives the far end as the router prints it.
  • 10g-2 to 10g-4 are access ports, each in one VLAN. A device plugged into one of them does not know VLANs exist.
  • The switch has one address of its own, on mgmt, so you can log in to it, and a default route to the router, its gateway.
  • lldp: true lets the switch tell its neighbors who it is. stp: { edge: true } marks a port with a single device on it, so the port comes up at once instead of waiting. Staying up explains why a switch waits.

The router: router.ts ​

ts
// The router: the internet connection, a gateway and DHCP for each VLAN, and the firewall.

import { as64500 } from "../../circuit";
import { home } from "./site";

export default as64500.device({
    name: "router",
    site: "home",
    role: "Router",
    platform: "routeros",
    model: "CCR2216-1G-12XS-2XQ",
    connection: { host: "192.168.10.1", user: "operator" },

    system: {
        timezone: home.timezone,
        dns: { servers: ["1.1.1.1", "1.0.0.1"] },
        ntp: { servers: ["pool.ntp.org"] },
        users: { operator: { role: "admin", password: secret("ROUTER_PASSWORD") } },
    },
    management: { allow: [home.addresses.management], ssh: {} },

    interfaces: {
        ethernet: {
            "1g-1": { description: "Internet provider", addresses: ["203.0.113.2/30"] },
            "25g-1": {
                description: "switch 10GE1/0/1",
                speed: "10g",
                trunk_vlans: ["mgmt", "home", "guests"],
                link: { device: "switch", port: "10g-1" },
            },
        },
        // The gateway of each VLAN.
        vlan_interface: {
            mgmt: { vlan: "mgmt", addresses: [home.addresses.router] },
            home: { vlan: "home", addresses: ["192.168.20.1/24"] },
            guests: { vlan: "guests", addresses: ["192.168.30.1/24"] },
        },
    },
    switching: {
        vlans: home.vlans,
    },

    services: {
        dns_server: true,
        ntp_server: true,
        dhcp_server: {
            home: {
                interface: "home",
                network: "192.168.20.0/24",
                gateway: "192.168.20.1",
                dns: ["192.168.20.1"],
                pool: ["192.168.20.100", "192.168.20.200"],
            },
            guests: {
                interface: "guests",
                network: "192.168.30.0/24",
                gateway: "192.168.30.1",
                dns: ["192.168.30.1"],
                pool: ["192.168.30.100", "192.168.30.200"],
                lease_time: 3600,
            },
        },
    },

    routing: {
        static_routes: [{ prefix: presets.default_routes.ipv4, next_hop: "203.0.113.1", description: "Internet provider" }],
    },

    firewall: {
        filter: {
            // Traffic to the router itself.
            input: {
                default_action: "drop",
                rules: [
                    { description: "Replies", if: { connection_state: ["established", "related"] }, then: "accept" },
                    { description: "Ping", if: { protocol: "icmp" }, then: "accept" },
                    { description: "Management", if: { src: home.addresses.management }, then: "accept" },
                    { description: "DNS from home", if: { in_interface: "home", protocol: "udp", dst_port: 53 }, then: "accept" },
                    { description: "DNS from guests", if: { in_interface: "guests", protocol: "udp", dst_port: 53 }, then: "accept" },
                ],
            },
            // Traffic through the router: home and guests reach the internet, and nothing else gets through.
            forward: {
                default_action: "drop",
                rules: [
                    { description: "Replies", if: { connection_state: ["established", "related"] }, then: "accept", fast_path: true },
                    { description: "Home to the internet", if: { in_interface: "home", out_interface: "1g-1" }, then: "accept" },
                    { description: "Guests to the internet", if: { in_interface: "guests", out_interface: "1g-1" }, then: "accept" },
                ],
            },
        },
        nat: {
            source: [{ description: "Share the public address", if: { out_interface: "1g-1" }, then: "masquerade" }],
        },
    },
});
  • 25g-1 is the other end of the trunk, with the same three VLANs and a link back (Trunks).
  • vlan_interface gives the router an address on each VLAN, the gateway for that VLAN. dhcp_server hands out addresses, the gateway, and the DNS server to every device that joins (DHCP).
  • 1g-1 has the public address from your provider, and the static route sends everything that is not at home to the provider. Change both to what your provider gave you.
  • dns_server: true lets the router answer DNS questions, which is why the input firewall accepts DNS from home and guests.
  • firewall.filter decides what may pass, to the router and through it (firewall). firewall.nat shares the public address (NAT).
  • system.users, management, and secret keep the keys (Keeping the keys).

The network: circuit.ts and lib/checks.ts ​

ts
// The network: one site, a home or a small office, with a router and a switch. Circuit reads this file first, then every device file under sites/.

import { managementStaysOnTrunks } from "./lib/checks";

export const as64500 = circuit({
    asn: 64500,
    sites: {
        home: { description: "A home or a small office: one router and one switch." },
    },
    checks: {
        same_vlan_ids: true,
        management_stays_on_trunks: managementStaysOnTrunks,
    },
});
ts
// This network's own checks, beside Circuit's built-in ones. circuit.ts turns them on in `checks`.

import type { Check } from "circuit";

/** Someone who plugs into a wall socket must not land on the management network: only a trunk between devices carries it. */
export const managementStaysOnTrunks: Check = (network) =>
    network.devices.flatMap((device) =>
        Object.entries(device.interfaces?.ethernet ?? {})
            .filter(([, port]) => port?.access_vlan === "mgmt")
            .map(([name]) => ({
                device: device.name,
                path: `interfaces.ethernet.${name}`,
                message: "gives management to whatever plugs in. Put the port in another VLAN.",
            }))
    );

circuit.ts is the network: its one site, and the checks. same_vlan_ids is one of Circuit's own: one VLAN name has one number on every device. management_stays_on_trunks is the project's own, in lib/checks.ts: no access port gives the management VLAN to whatever plugs in. A network without BGP needs nothing more.

Fix what validate finds ​

circuit validate checks every file, the two ends of every cable, and your checks. Your editor catches some mistakes before that, such as a VLAN name that does not exist. Here are five mistakes that are easy to make while you change the example to your own.

Try itFix the files

Mistake 1 of 5: A VLAN missing on one end of the trunk

sites/home/switch.ts
"10g-1": {
    description: "router sfp28-1",
    trunk_vlans: ["mgmt", "home"],
    link: { device: "router", port: "25g-1" },
},

Find what is wrong in the file, then run validate and read what it says.

Each message points at its cause: the device, the field, and often the file and line. Your own check speaks in the words you gave it in lib/checks.ts.

An error stops diff and apply, so none of these can reach a device. A warning, like the cable described in your own words, is printed and stops nothing.

Checkvalidate prints one warning and no errors. You run circuit diff switch. What happens?
CheckWhich part of the files keeps a guest phone from reaching the printer on the home VLAN?

From the files to the devices ​

Try itFrom the files to a change
Terminal
$ circuit secrets set ROUTER_PASSWORD
✔ Set ROUTER_PASSWORD in .circuit/secrets.
$ circuit secrets set SWITCH_PASSWORD
✔ Set SWITCH_PASSWORD in .circuit/secrets.
$ circuit secrets list
┌─────────────────┬───────────┐
│ Name            │ Stored as │
├─────────────────┼───────────┤
│ ROUTER_PASSWORD │ encrypted │
│ SWITCH_PASSWORD │ encrypted │
└─────────────────┴───────────┘

Step 1 of 6

Set the passwords

secrets set keeps each password encrypted in .circuit/secrets. secrets list checks that every password the files use is there.

The first plan is long

A device that was set up by hand runs many things your files do not describe yet. The first plan removes all of them, because the file is the whole truth. Read it line by line, and add to the file whatever must stay, until the plan has only changes you want.

CheckYesterday you ran diff. This morning a colleague changed the switch by hand. You run circuit apply switch. What does its plan compare your files with?

Discussion

Stuck, or found a mistake? Ask here. You sign in with GitHub.

Released under the Business Source License 1.1.