Let's switch again#
Sun, 29 Apr 2018 13:47:07 +0000
Achieving anything new this week has been rather hampered by (1) my decision to try out XMonad, and then (2) the kids all picked up some kind of vomiting bug. I do not intend you to infer any connection.
(XMonad: dunno yet, haven't really tried using it for long enough. The mouse pointer is impossibly small and I'm going to have to fix that sooner or later, but I only need it for gui apps and all I've really tried using thus far is emacs and rxvt)
But let's see if I can explain why I've been hung up on switches
lately. If you've ever wanted to know why your OpenWRT router has
network interfaces with names like eth0.1
(no, it's not a misguided
decision to do semantic versioning on them)
maybe this is for you.
First, you need to know about VLANs. A VLAN is a "virtual LAN": a way to multiplex traffic for multiple independent LANs onto the same cable. In the picture on your right, VLAN 5 connects ports 1 and 3 on switch A with ports 2 and 4 on switch B, and VLAN 6 connects port 2 on A with 3 on B.
The switches do this by "tagging" the packets (frames) according to
per-port rules. If alice
sends to fred
, her frames will be tagged
with VLAN ID 5 when they enter switch A, sent (with the tag intact) to
switch B, and then untagged again as they are sent out of port 3 to
fred
. Long story short -
- frames from end-user devices get tagged
- frames to end-user devices get detagged
- frames between VLAN-aware devices (usually, one switch and another) have had tags applied already and are transmitted without change
This is super useful, I have no doubt, if you're running an enterprise network and need to keep devices separated without having to run multiple sets of cables to every desk. But how is it relevant to *WRT? Because the hardware you're running it on, despite any impressions you might have had from its inclusion of two or five or eight RJ45 sockets, quite likely only has one ethernet device that Linux can see. This device is connected (internally, inside the SoC) to a builtin switch, which is also connected to all the sockets you can see. So if you want to address them separately - for example, you want to connect your upstream connection to one of them without giving it full access to your LAN and vice versa - you do it with VLAN configuration.
Make the switch
Set the upstream port to tag VLAN 1 and the LAN ports to tag VLAN 2,
and the "CPU port" (the one connected to the eth0
device that Linux
sees) to allow VLANs 1 and 2 but not to tag/untag either - i.e. it
receives tagged packets and particapates in the VLAN as if it were
another switch.
We would do this with swconfig
but we haven't yet because this turns
out to be the default configuration anyway, at least on the MT300A.
(I have no idea whether it's hardware or u-boot or the devicetree
config that makes it be this way - at some point I dare say I will
find out though)
Configure Linux
So we have a switch on the SoC which is sending VLAN tagged frames to the ethernet interface - we need to tell Linux to expect them. (You might say it needs configuring to serve VLAN).
You do this with the ip
command found in
iproute2
ip link add link eth0 name eth0.1 type vlan id 1 ip link add link eth0 name eth0.2 type vlan id 2 ip addr add 192.168.0.251/24 dev eth0.2 ip link set dev eth0 up
and now for most purposes you can treat eth0.1
and eth0.2
as
though you have two network interfaces.
I'd like to close by saying "Simples!" but when I googled that term I learned of the existence of contemporary mereology as a field of philosophical study, and I am not sure anything can ever be simple again.