---
# System prepended metadata

title: virtual network interface
tags: [linux, network, ip]

---

# virtual network interface

###### tags: `linux` `ip` `network`

Examples to create some type of virtual network interface.

#### macvlan
The macvlan interface create a new mac address and bind with physical interface.
It has 5 mode types, "private", "vepa", "bridge", "passthru", "source". Each of them has different traffic flow constrain.

```
root@worker4:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:54:92:f8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.14/24 brd 192.168.122.255 scope global dynamic ens3
       valid_lft 2845sec preferred_lft 2845sec    
```

```
root@worker4:~# ip netns add NS0
root@worker4:~# ip link add macvlan0 link ens3 type macvlan mode bridge
root@worker4:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:54:92:f8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.14/24 brd 192.168.122.255 scope global dynamic ens3
       valid_lft 2583sec preferred_lft 2583sec
3: macvlan0@ens3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 06:98:0a:f7:55:a6 brd ff:ff:ff:ff:ff:ff
root@worker4:~# ip link set macvlan0 netns NS0
root@worker4:~# ip netns exec NS0 ip link set macvlan0 up
root@worker4:~# 
root@worker4:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:54:92:f8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.14/24 brd 192.168.122.255 scope global dynamic ens3
       valid_lft 2528sec preferred_lft 2528sec
root@worker4:~# 
root@worker4:~# ip netns exec NS0 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: macvlan0@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 06:98:0a:f7:55:a6 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::498:aff:fef7:55a6/64 scope link 
       valid_lft forever preferred_lft forever
```

```
root@worker4:~# ip netns exec NS0 ip addr add 192.168.122.222/24 dev macvlan0
root@worker4:~# ip netns exec NS0 bash
root@worker4:~# ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: macvlan0@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 06:98:0a:f7:55:a6 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.122.222/24 scope global macvlan0
       valid_lft forever preferred_lft forever
    inet6 fe80::498:aff:fef7:55a6/64 scope link 
       valid_lft forever preferred_lft forever

root@worker4:~# ping 192.168.122.1 -c 3
PING 192.168.122.1 (192.168.122.1) 56(84) bytes of data.
64 bytes from 192.168.122.1: icmp_seq=1 ttl=64 time=0.178 ms
64 bytes from 192.168.122.1: icmp_seq=2 ttl=64 time=0.195 ms
64 bytes from 192.168.122.1: icmp_seq=3 ttl=64 time=0.213 ms

--- 192.168.122.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2038ms
rtt min/avg/max/mdev = 0.178/0.195/0.213/0.014 ms
```

#### vlan
with vlan interface, mac address can be specified.
```
root@worker4:~# ip netns add NS1

root@worker4:~# ip link add link ens3 name eth0 address 52:54:00:54:80:20 type vlan id 10
root@worker4:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:54:92:f8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.14/24 brd 192.168.122.255 scope global dynamic ens3
       valid_lft 2403sec preferred_lft 2403sec
5: eth0@ens3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 52:54:00:54:80:20 brd ff:ff:ff:ff:ff:ff

root@worker4:~# ip link set eth0 netns NS1
root@worker4:~# ip netns exec NS1 ip link set eth0 up
root@worker4:~# ip netns exec NS1 ip addr add 192.168.122.223/24 dev eth0
root@worker4:~# ip netns exec NS1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
5: eth0@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:54:80:20 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.122.223/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe54:8020/64 scope link 
       valid_lft forever preferred_lft forever
```

> captured traffic on ens3 has vlan id 10.
```
root@worker4:~# ip netns exec NS1 ping 192.168.122.1 -c 3
PING 192.168.122.1 (192.168.122.1) 56(84) bytes of data.

--- 192.168.122.1 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2046ms
```
```
root@worker4:~# tcpdump -i ens3 -n -e  not tcp port 22 and vlan
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens3, link-type EN10MB (Ethernet), capture size 262144 bytes
00:21:44.451524 52:54:00:54:80:20 > ff:ff:ff:ff:ff:ff, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype ARP, Request who-has 192.168.122.1 tell 192.168.122.223, length 28
00:21:45.474651 52:54:00:54:80:20 > ff:ff:ff:ff:ff:ff, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype ARP, Request who-has 192.168.122.1 tell 192.168.122.223, length 28
00:21:46.498654 52:54:00:54:80:20 > ff:ff:ff:ff:ff:ff, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype ARP, Request who-has 192.168.122.1 tell 192.168.122.223, length 28
```
#### dummy
```
root@worker4:~# ip link add dummy1 type dummy
root@worker4:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:54:92:f8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.14/24 brd 192.168.122.255 scope global dynamic ens3
       valid_lft 2879sec preferred_lft 2879sec
6: dummy1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 5a:0c:f6:e1:29:86 brd ff:ff:ff:ff:ff:ff
root@worker4:~# ip addr add 192.168.122.224/23 dev dummy1
root@worker4:~# ip link set dummy1 up
root@worker4:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:54:92:f8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.14/24 brd 192.168.122.255 scope global dynamic ens3
       valid_lft 2851sec preferred_lft 2851sec
6: dummy1: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether 5a:0c:f6:e1:29:86 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.224/23 scope global dummy1
       valid_lft forever preferred_lft forever
    inet6 fe80::580c:f6ff:fee1:2986/64 scope link 
       valid_lft forever preferred_lft forever

```

#### reference:
```
https://developers.redhat.com/blog/2018/10/22/introduction-to-linux-interfaces-for-virtual-networking
https://man7.org/linux/man-pages/man8/ip-link.8.html
https://hicu.be/docker-networking-macvlan-vlan-configuration
```