0

Let's create two macvlan's and attach network namespaces to them:

$ sudo ip link add macvlan1 link eth0 type macvlan mode bridge
$ sudo ip link add macvlan2 link eth0 type macvlan mode bridge
$ sudo ip netns add net1
$ sudo ip netns add net2
$ sudo ip link set macvlan1 netns net1 # attach net1 to macvlan1
$ sudo ip link set macvlan2 netns net2 # attach net2 to macvlan2

Next, let's start a simple tcp server in one of the namespaces, bring it up and ensure that it's working:

# let's bring loopback up
$ sudo ip netns exec net1 ip link set lo up

# start a server listening on port 5000
$ sudo ip netns exec net1 /bin/bash -c 'while (sleep 1); do echo "hello from net1"; done | nc -lk 5000 &'

# Now, from inside the net1 namespace, we can connect to the server
$ sudo ip netns exec net1 nc localhost 5000
hello from net1
hello from net1
hello from net1
hello from net1
hello from net1
hello from net1

Now, obviously I can't connect to this server from the host or from macvlan2:

# from the host
$ sudo nc localhost 5000
Ncat: Connection refused.

# from mcvlan2
$ sudo ip netns exec net2 nc localhost 5000
Ncat: Network is unreachable.

Question

My question is, how do I make that simple tcp server available to the host and to mcvlan2 (net2)? For context, assume I need dozens of such network namespaces / macvlans.

EDIT

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.15.87  netmask 255.255.240.0  broadcast 172.31.15.255
        inet6 fe80::413:87ff:fe9c:f21b  prefixlen 64  scopeid 0x20<link>
        ether 06:13:87:9c:f2:1b  txqueuelen 1000  (Ethernet)
        RX packets 767  bytes 76228 (74.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 713  bytes 107200 (104.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 06:13:87:9c:f2:1b brd ff:ff:ff:ff:ff:ff

$ sudo ip netns exec net1 ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 18  bytes 1552 (1.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 1552 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ sudo ip netns exec net1 ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: macvlan1@if2: <BROADCAST,MULTICAST> mtu 9001 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:d3:ae:64:24:99 brd ff:ff:ff:ff:ff:ff link-netnsid 0

SOLUTION ATTEMPT #1

# change the tcp server to listen on all hosts
$ sudo ip netns exec net1 /bin/bash -c 'while (sleep 1); do echo "hello from net1"; done | nc -lk 0.0.0.0 5000'

# add ip addr to macvlan1 and bring it up
$ sudo ip netns exec net1 ifconfig macvlan1 192.168.2.1/32 up

# add default route to net1
$ sudo ip netns exec net1 ip route add default via 192.168.2.1

# attempt to connect to tcp server from host
$ sudo nc 192.168.2.1 5000
Ncat: Connection timed out.
4
  • 2
    ifconfig, ip link show macvlan1&2 tell what ? as I dont see see sudo ifconfig macvlan1 up & sudo ifconfig macvlan2 up command done
    – yagmoth555
    Sep 17 '19 at 18:09
  • @yagmoth555 thanks for the help. I edited my question to show the commands you suggest. And good point, I am not bringing the macvlan's up nor am I giving them an ip addr (i.e. $ sudo ip netns exec net1 ifconfig macvlan1 10.1.1.1/24 up) Sep 17 '19 at 18:25
  • 1
    related on UL SE: How to make reachable macvlan aliases in a different namespaces?
    – A.B
    Sep 18 '19 at 11:58
  • OP's problem isn't really about macvlan. It's about adding weird address netmasks and routes that won't give a route in the end. One dosn't set the default route's gateway to oneself, it's the same as not setting a gateway and only relying on ARP. Then the other namespace didn't get a compatible configuration etc. Not forgetting the first attempt from net2 to itself where its own lo interface isn't up. Looks like OP doesn't understand that lo is per namespace, as well as other settings.
    – A.B
    Dec 19 '20 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.