Score:0

Is it possible to route data between 2 wireguard devices on the same machine?

de flag

I want to find out how much overhead wireguard creates on my machine in terms of latency. However, my server is remote, so I would like to test it solely on the machine to avoid measuring the whole network latency.

So for measuring the latency, I thought about the following setup.

I add two wireguard devices to the machine, wg0 and wg1. I then send a packet to wg0, which encrypts the packet and sends it to wg1. Wg1 then decrypts it and sends it to a local port to measure the time it takes between sending and receiving the packet.

My wg0 conf:

[Interface]
   Address = 10.0.0.1/24
   ListenPort = 51871

[Peer]
   AllowedIPs = 10.0.0.1/24, 10.0.1.1/24
   Endpoint = localhost:51872

My wg1 conf:

[Interface]
   Address = 10.0.1.1/24
   ListenPort = 51872

[Peer]
   AllowedIPs = 10.0.0.1/24, 10.0.1.1/24
   Endpoint = localhost:51871
I have tried using the following configuration but then I am unable to setup wg1 as the setup fails at

ip -4 route add 10.0.0.0/24 dev wg1
due to 
RTNETLINK answers: File exists

Which makes sense as because this is the IP range of the wg0 device

My wg0 conf:

[Interface]
   Address = 10.0.0.1/24
   ListenPort = 51871
   PostUp = route add -net 10.0.1.0/24 gw 10.0.0.1 
   PostDown = route delete -net 10.0.1.0/24 gw 10.0.0.1

[Peer]
   AllowedIPs = 10.0.0.1/24, 10.0.1.1/24
   Endpoint = localhost:51872

My wg1 conf:

[Interface]
   Address = 10.0.1.1/24
   ListenPort = 51872
   PostUp = route add -net 10.0.0.1/24 gw 10.0.1.1 
   PostDown = route delete -net 10.0.0.1/24 gw 10.0.1.1


[Peer]
   AllowedIPs = 10.0.0.1/24, 10.0.1.1/24
   Endpoint = localhost:51871

However, I can not get the routing set up so that my packet actually traverses the wg devices. The devices also do not perform a handshake atm. Is it even possible? Or can you recommend another setup?.

My OS is ubuntu 20.04 server. As a sidenote, I want to compare this to another measurement I took where one of the wireguard devices runs in a virtual machine. In this setup, I just ran wg0 on the host and wg1 in the VM. Basically, now I want to find out the overhead of running a wireguard gateway in a VM compared to running it natively on the machine.

Solution:

As suggested, I am now running the second wg device in a different namespace. The setup was straightforward because even if the wg device is moved to a different namespace, the listen-port stays in the original namespace. The RTT is about 2/3 compared to running wg1 in a Linux VM for anyone interested.

Thanks for the suggestions!

A.B avatar
cl flag
A.B
Use network namespaces instead (or containers).
user132645 avatar
de flag
Thanks a lot for the tip! Got it working with namespaces. I am pretty new to this.
A.B avatar
cl flag
A.B
You can answer your own question with the working solution then.
A.B avatar
cl flag
A.B
Sorry again to suggest something: you're not supposed to add the solution *inside* the question. You're supposed to add it as an answer to the question, even if it's your own question.
user132645 avatar
de flag
No worries, I am happy to learn ;)
Score:1
de flag

As suggested, I used network namespaces. Here is the setup I ended up with, although I didn't use config files:

Setup of the first wg device:

sudo ip link add dev wg0 type wireguard
sudo ip address add dev wg0 10.0.0.1/24
sudo wg set wg0 listen-port 51871 private-key ./wg0.key peer PEER1_PUBKEY allowed-ips 10.0.0.0/24 endpoint localhost:51872
sudo ip link set up dev wg0

Setup of the wg1 device:

sudo ip netns add container
sudo ip link add wg1 type wireguard
sudo ip link set wg1 netns container
sudo ip -n container addr add 10.0.0.2/24 dev wg1
sudo ip netns exec container wg set wg1 listen-port 51872 private-key ./wg1.key peer PEER0_PUBKEY allowed-ips 10.0.0.0/24 endpoint localhost:51871
sudo ip -n container link set wg1 up
sudo ip -n container route add default dev wg1

And then I simply run an echo client like this:

sudo ip netns exec container ./udp_echo_server
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.