title: Multiple Linux network namespaces for single application
body: <p>I'm trying to use network namespaces to achieve VRF behavior (virtual routing and forwarding) for network isolation.  Essentially I have a server application (C/C++) running on a TCP port in the default namespace.  What I'd like to do is use network namespaces to create isolated VRF's using VLANs, and then have that application running in the default namespace be able to spawn a thread to each namespace to listen on that same port per namespace.</p>
  
  <p>I have the network side figured out, I just can't see how I can spawn a thread (prefer to use pthread's instead of clone() if possible), call setns() on one of those namespaces, and then bind to the same port inside the namespace.  Here's what I'm doing to create the namespaces and bridges (limiting to one namespace here for simplicity):</p>
  
  <pre><code># ip netns add ns_vlan100
  # ip link add link eno1 eno1.100 type vlan id 100
  
  # ip link add veth0 type veth peer name veth_vlan100
  # ip link set veth0 netns ns_vlan100
  
  # ip netns exec ns_vlan100 ip link set dev veth0 up
  # ip link set dev veth_vlan100 up
  
  # brctl addbr bridge_vlan100
  # brctl addif bridge_vlan100 eno1.100
  # brctl addif bridge_vlan100 veth_vlan100
  
  # ip link set dev bridge_vlan100 up
  # ip link set dev eno1.100 up
  
  # ip netns exec ns_vlan100 ifconfig veth0 10.10.10.1 netmask 255.255.255.0 up
  # ip netns exec ns_vlan100 ip route add default via 10.10.10.1
  </code></pre>
  
  <p>With this, I can create a VLAN on a peer machine (no containers) and ping 10.10.10.1 without issue.  So I know the links are good.  What I want to do is then have my existing application be able to spawn a thread in C or C++ (pthreads are heavily preferred), and that thread call setns() with something to put it into namespace ns_vlan100, so I can then bind to the same port for my application, just inside that namespace.</p>
  
  <p>I can't seem to figure out how to do this.  Any help is much appreciated.</p>