# Tap in Qemu not working

## betlor5

Hi,

I'm trying to get qemu and a tuntap device to communicate with each other so that I can use the internet in a normal way in the virtual maschine.

My qemu-ifup script looks like this:

```

#!/bin/sh

set -x

switch=br0

if [ -n "$1" ];then

        /usr/bin/tunctl -u `whoami` -t $1

        /bin/ip link set $1 up promisc on

        sleep 0.5s

        /sbin/brctl addif $switch $1

        exit 0

else

        echo "Error: no interface specified"

        exit 1

fi

```

the qemu-ifdown

```

/bin/sh

set -x

switch=br0

if [ -n "$1" ];then

        bin/ip link set $1 down

        sleep 0.5s

        /sbin/brctl delif $switch $1

        /bin/ip link delete dev $1

        exit 0

else

        echo "Error: no interface specified"

        exit 1

fi

```

my /etc/conf.d/net

```

config_enp2s0="null"

config_br0="144.76.153.170"

brctl_br0="setfd 0" "sethello 10" "stp on"

mac_br0="d4:3d:7e:db:00:da"

bridge_br0="enp2s0"

rc_need_br0="net.enp2s0"

```

Ifconfig returns

```

br0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1500

        inet 144.76.153.170  netmask 255.255.255.255  broadcast 0.0.0.0

        inet6 fe80::d63d:7eff:fedb:da  prefixlen 64  scopeid 0x20<link>

        ether d4:3d:7e:db:00:da  txqueuelen 0  (Ethernet)

        RX packets 313437  bytes 21291579 (20.3 MiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 466439  bytes 107791501 (102.7 MiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp2s0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1500

        inet 169.254.181.57  netmask 255.255.0.0  broadcast 169.254.255.255

        inet6 fe80::d63d:7eff:fedb:da  prefixlen 64  scopeid 0x20<link>

        ether d4:3d:7e:db:00:da  txqueuelen 1000  (Ethernet)

        RX packets 313903  bytes 25690677 (24.5 MiB)

        RX errors 0  dropped 388  overruns 0  frame 0

        TX packets 466828  bytes 108259769 (103.2 MiB)

        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 0  (Local Loopback)

        RX packets 34  bytes 2681 (2.6 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 34  bytes 2681 (2.6 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

sit0: flags=193<UP,RUNNING,NOARP>  mtu 1480

        inet6 ::127.0.0.1  prefixlen 96  scopeid 0x90<compat,host>

        inet6 ::144.76.153.170  prefixlen 96  scopeid 0x80<compat,global>

        sit  txqueuelen 0  (IPv6-in-IPv4)

        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 185  dropped 0 overruns 0  carrier 0  collisions 0

tap1: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1500

        inet 169.254.156.36  netmask 255.255.0.0  broadcast 169.254.255.255

        inet6 fe80::b813:50ff:fe0f:66eb  prefixlen 64  scopeid 0x20<link>

        ether ba:13:50:0f:66:eb  txqueuelen 500  (Ethernet)

        RX packets 20  bytes 3210 (3.1 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 21  bytes 3415 (3.3 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

```

when qemu is running.  I am also able to ping the tap1 device.

```

ping 169.254.156.36

PING 169.254.156.36 (169.254.156.36) 56(84) bytes of data.

64 bytes from 169.254.156.36: icmp_seq=1 ttl=64 time=0.017 ms

64 bytes from 169.254.156.36: icmp_seq=2 ttl=64 time=0.010 ms

```

In order to start the qemu-kvm device I'm using a start up script which looks like this:

 *Quote:*   

> 
> 
> #!/bin/sh
> 
> exec qemu-system-x86_64 \
> ...

 

the ifconfig of the vm says:

```

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet6 fe80::dcad:beff:feff:bcdc prefixlen 64  scopeid 0x20<link>

        ether d4:3d:7e:db:00:da  txqueuelen 1000  (Ethernet)

        RX packets 12 bytes 2002 (1.9 KiB)

        RX errors 0  dropped 388  overruns 0  frame 0

        TX packets 10 bytes 1352 (1.3KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

```

If i try to ping something in the vm it tells me 

```

ping: unkown host www.gentoo.org

```

If I'm using the user mode insted of tap it is working unfortunatlly this option is not sufficiant enough for my intention.

Before I forget:

 *Quote:*   

> 
> 
> brctl show
> 
> bridge name     bridge id               STP enabled     interfaces
> ...

 

```

ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

    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: enp2s0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000

    link/ether d4:3d:7e:db:00:da brd ff:ff:ff:ff:ff:ff

    inet 169.254.181.57/16 brd 169.254.255.255 scope global enp2s0

       valid_lft forever preferred_lft forever

    inet6 fe80::d63d:7eff:fedb:da/64 scope link

       valid_lft forever preferred_lft forever

3: sit0: <NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN

    link/sit 0.0.0.0 brd 0.0.0.0

    inet6 ::144.76.153.170/96 scope global

       valid_lft forever preferred_lft forever

    inet6 ::127.0.0.1/96 scope host

       valid_lft forever preferred_lft forever

4: br0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP

    link/ether d4:3d:7e:db:00:da brd ff:ff:ff:ff:ff:ff

    inet 144.76.153.170/32 scope global br0

       valid_lft forever preferred_lft forever

    inet 144.76.153.170/27 brd 144.76.153.191 scope global br0

       valid_lft forever preferred_lft forever

    inet6 fe80::d63d:7eff:fedb:da/64 scope link

       valid_lft forever preferred_lft forever

35: tap1: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 500

    link/ether ca:db:f9:6c:8b:49 brd ff:ff:ff:ff:ff:ff

    inet 169.254.156.36/16 brd 169.254.255.255 scope global tap1

       valid_lft forever preferred_lft forever

    inet6 fe80::c8db:f9ff:fe6c:8b49/64 scope link

       valid_lft forever preferred_lft forever

```

Regarding the Modeles I have checked several times. 

```

cat /usr/src/linux/.config | grep 802

# CONFIG_BRIDGE_EBT_802_3 is not set

CONFIG_VLAN_8021Q=y

CONFIG_VLAN_8021Q_GVRP=y

CONFIG_VLAN_8021Q_MVRP=y

```

```

cat /usr/src/linux/.config | grep KVM

CONFIG_HAVE_KVM=y

CONFIG_HAVE_KVM_IRQCHIP=y

CONFIG_HAVE_KVM_IRQ_ROUTING=y

CONFIG_HAVE_KVM_EVENTFD=y

CONFIG_KVM_APIC_ARCHITECTURE=y

CONFIG_KVM_MMIO=y

CONFIG_KVM_ASYNC_PF=y

CONFIG_HAVE_KVM_MSI=y

CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT=y

CONFIG_KVM=y

CONFIG_KVM_INTEL=y

# CONFIG_KVM_AMD is not set

# CONFIG_KVM_MMU_AUDIT is not set

CONFIG_KVM_DEVICE_ASSIGNMENT=y

```

By the way the cpu information:

```

lscpu

Architecture:          x86_64

CPU op-mode(s):        32-bit, 64-bit

Byte Order:            Little Endian

CPU(s):                8

On-line CPU(s) list:   0-7

Thread(s) per core:    2

Core(s) per socket:    4

Socket(s):             1

NUMA node(s):          1

Vendor ID:             GenuineIntel

CPU family:            6

Model:                 60

Stepping:              3

CPU MHz:               3400.000

BogoMIPS:              6800.19

Virtualization:        VT-x

L1d cache:             32K

L1i cache:             32K

L2 cache:              256K

L3 cache:              8192K

NUMA node0 CPU(s):     0-7

```

I hope you can help me to get tap and qemu up and running.

Betlor5

----------

## Logicien

Hello,

the root slash / is missing in bin/ip in the qemu-ifdown script. If you put tap1 in the bridge br0, tap1 should not have any IP address as a part of the bridge. So you can ping tap1 at 169.254.156.36, but for the host and the guest it have the same IP address than br0, 144.76.153.170. The same apply to the enp2s0 interface as a slave of the bridge br0.

It's look like if some DHCP request are made by the host on those interfaces without an answer by a server and the DHCP client give them the address they have. Be sure that the slave of the bridge are not use directly by the host but only from the bridge br0.

In Qemu, giving a unique IP address to the Ethernet interface in the same subnet than br0 and give 144.76.153.170 as the default route should give you Internet access with appropriates IP DNS. Than you should be able to ping the 144.76.153.170 address.

Is the command

```
brctl show
```

executed from the host after Qemu is started give the output you expect, tap1 and enp2s0 as slaves of br0? Do you have an Ethernet device in Qemu?

----------

## betlor5

The brctl show was execudet as qemu was running giving the output:

 *Quote:*   

> 
> 
> bridge name     bridge id               STP enabled     interfaces
> 
> br0             8000.d43d7edb00da       no              enp2s0
> ...

 

As far as I am aware I should have an Ethernet device in Qemu as it is started in

```

-device e1000,netdev=net1,mac=DE:AD:BE:EF:BC:DC\

```

and linkes in 

```

-netdev tap,id=net1,ifname=tap1,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown\ 

```

and having a look at http://www.linux-kvm.org/page/Networking it is the modern way of saying the same thing with 

 *Quote:*   

> 
> 
> -net nic ...
> 
> 

 

which after my understand should initalise the ethernet device. Next to the postest enp0s3 module I am alse getting a lo module when executing a ifconfig in the qemu.

 *Quote:*   

> 
> 
> It's look like if some DHCP request are made by the host on those interfaces without an answer by a server and the DHCP client give them the address they have. Be sure that the slave of the bridge are not use directly by the host but only from the bridge br0.
> 
> In Qemu, giving a unique IP address to the Ethernet interface in the same subnet than br0 and give 144.76.153.170 as the default route should give you Internet access with appropriates IP DNS. Than you should be able to ping the 144.76.153.170 address.
> ...

 

It would be nice if you could illustrate to me how I have to proceed in that regard.

----------

## Logicien

I am not sure I can help you the way you do because I do not use any start/stop script with Qemu, I do not set any parameter to the tuntap device, even not a user, my tap0 virtual network device is created by Openrc at boot time from net.tap0 as a symbolic link to net.lo in the /etc/init.d/ directory and added to the default runlevel with rc-update. The same method is use for net.br0. My /etc/conf.d/net for br0 and tap0 is

```
bridge_br0="tap0"

config_br0="xxx.xxx.xxx.xxx broadcast xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx"

modules_br0="!dhclient !dhcpcd !ifconfig iproute2 !pump !udhcpc !wireless"

rc_net_br0_need="net.tap0"

config_tap0="null"

modules_tap0="!dhclient !dhcpcd !ifconfig iproute2 !pump !udhcpc !wireless"

tuntap_tap0="tap"
```

Any module that can trigger a DHCP request is disabled because the IP configuration is manual.

A reason I see why your wire Ethernet device is not part of your bridge br0 is that

```
rc_need_br0="net.enp2s0" 
```

should be

```
rc_net_br0_need="net.enp2s0"
```

I start Qemu with the old method for the network configuration for the moment with this one line command

```
xterm -hold -e 'QEMU_AUDIO_DRV=alsa qemu-kvm -boot d -cdrom /mnt/garde/SystemRescueCd/*.iso -hda /mnt/garde/Qemu/hda.img -enable-kvm -fda /mnt/garde/Qemu/win98sc.img -m 2048 -net nic,vlan=0 -net tap,vlan=0,ifname=tap0,script=no -soundhw hda -usb -vga cirrus'
```

Here I boot in the cdrom using SystemRescueCd iso image. I use NetworkManager and configure the network manually because the host do not have any DHCP server for br0 clients request. With a Linux guest who do not use NetworkManager, I can configure the network in Qemu using several commands like ip, ifconfig and route. Here is an example

```
ifconfig eth0 xxx.xxx.xxx.xxx up

route add default gw xxx.xxx.xxx.xxx

echo 'nameserver 8.8.8.8' > /etc/resolv.conf
```

----------

## betlor5

I am a little bit confused. Maybe it is just a tippo, but just to clarify. You worte:

 *Quote:*   

> 
> 
> bridge_br0="tap0" 
> 
> 

 

With my level of understanding I thought it should be something like:

```

tuntap_tap0="tap"

config_tap0="null"

brctl_br0="setfd 0" "sethello 10" "stp on"

```

because otherwise I would miss the bridge to which the tap device should be enslaved or am I mistaken?

----------

## Logicien

You must dissociate the lines that are for br0 to the one who are for tap0 in /etc/conf.d/net

```
bridge_br0=""
```

say which network devices are part of the bridge as slaves. In your case, it is only enp2s0 if you use a Qemu startup script because tap1 will be added by it. Without a Qemu startup script it will be

```
bridge_br0="enp2s0 tap0"
```

I would use the second method. The bridge always have enp2s0 and tap1 as slaves even if Qemu is off and are accessible from the beginning of the start of Qemu. That's imply that net.enp2s0 and net.tap0 are configure in /etc/conf.d/net and have their symbolic links to net.lo in /etc/init.d/. Add them in the boot process with rc-update can be done too.

```
config_br0="xxx.xxx.xxx.xxx broadcast xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx"
```

allow you to give an IP address, netmask and broadcast to br0.

```
modules_br0="!dhclient !dhcpcd !ifconfig iproute2 !pump !udhcpc !wireless"
```

precise which modules can be use with br0 only or not (! mean disabled).

```
rc_net_br0_need="net.tap0"
```

mean that tap0 should be up and running before net.br0. In your case, it should be

```
rc_net_br0_need="net.enp2s0 net.tap0"
```

That's imply that net.enp2s0 and net.tap0 are configure in /etc/conf.d/net and have their symbolic links to net.lo in /etc/init.d/. Add them in the boot process with rc-update can be done too.

```
config_tap0="null"
```

say no IP configuration for tap0. You can change tap0 for tap1 if you like.

```
modules_tap0="!dhclient !dhcpcd !ifconfig iproute2 !pump !udhcpc !wireless"
```

say the same for tap0 than for br0.

```
tuntap_tap0="tap"
```

say the virtual device tap0 is a tap device instead of a tun one.

```
brctl_br0="setfd 0" "sethello 10" "stp on"
```

pass some options to the command brctl when it will create the bridge br0. I do not use any of those options, so this line do not exist in my /etc/conf.d/net but it could be. I do not force any MAC address for br0 and tap0 too.

----------

## betlor5

Thank you for your fast and detailed answer. I will try it tomorrow if it is still not working I will ask again   :Very Happy: 

betlor5

----------

