xref: /qemu/docs/system/devices/net.rst (revision 94a9471ceb656ad45e86aa07e98d8d0c3b8afe82)
1d6359e15SPeter Maydell.. _Network_Emulation:
2324b2298SPaolo Bonzini
3324b2298SPaolo BonziniNetwork emulation
4324b2298SPaolo Bonzini-----------------
5324b2298SPaolo Bonzini
6324b2298SPaolo BonziniQEMU can simulate several network cards (e.g. PCI or ISA cards on the PC
7324b2298SPaolo Bonzinitarget) and can connect them to a network backend on the host or an
8324b2298SPaolo Bonziniemulated hub. The various host network backends can either be used to
9324b2298SPaolo Bonziniconnect the NIC of the guest to a real network (e.g. by using a TAP
10324b2298SPaolo Bonzinidevices or the non-privileged user mode network stack), or to other
11324b2298SPaolo Bonziniguest instances running in another QEMU process (e.g. by using the
12324b2298SPaolo Bonzinisocket host network backend).
13324b2298SPaolo Bonzini
14324b2298SPaolo BonziniUsing TAP network interfaces
15324b2298SPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16324b2298SPaolo Bonzini
17324b2298SPaolo BonziniThis is the standard way to connect QEMU to a real network. QEMU adds a
18324b2298SPaolo Bonzinivirtual network device on your host (called ``tapN``), and you can then
19324b2298SPaolo Bonziniconfigure it as if it was a real ethernet card.
20324b2298SPaolo Bonzini
21324b2298SPaolo BonziniLinux host
22324b2298SPaolo Bonzini^^^^^^^^^^
23324b2298SPaolo Bonzini
24324b2298SPaolo BonziniAs an example, you can download the ``linux-test-xxx.tar.gz`` archive
25324b2298SPaolo Bonziniand copy the script ``qemu-ifup`` in ``/etc`` and configure properly
26324b2298SPaolo Bonzini``sudo`` so that the command ``ifconfig`` contained in ``qemu-ifup`` can
27324b2298SPaolo Bonzinibe executed as root. You must verify that your host kernel supports the
28324b2298SPaolo BonziniTAP network interfaces: the device ``/dev/net/tun`` must be present.
29324b2298SPaolo Bonzini
30324b2298SPaolo BonziniSee :ref:`sec_005finvocation` to have examples of command
31324b2298SPaolo Bonzinilines using the TAP network interfaces.
32324b2298SPaolo Bonzini
33324b2298SPaolo BonziniWindows host
34324b2298SPaolo Bonzini^^^^^^^^^^^^
35324b2298SPaolo Bonzini
36324b2298SPaolo BonziniThere is a virtual ethernet driver for Windows 2000/XP systems, called
37324b2298SPaolo BonziniTAP-Win32. But it is not included in standard QEMU for Windows, so you
38324b2298SPaolo Bonziniwill need to get it separately. It is part of OpenVPN package, so
39324b2298SPaolo Bonzinidownload OpenVPN from : https://openvpn.net/.
40324b2298SPaolo Bonzini
41324b2298SPaolo BonziniUsing the user mode network stack
42324b2298SPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43324b2298SPaolo Bonzini
44324b2298SPaolo BonziniBy using the option ``-net user`` (default configuration if no ``-net``
45324b2298SPaolo Bonzinioption is specified), QEMU uses a completely user mode network stack
46324b2298SPaolo Bonzini(you don't need root privilege to use the virtual network). The virtual
47324b2298SPaolo Bonzininetwork configuration is the following::
48324b2298SPaolo Bonzini
49324b2298SPaolo Bonzini        guest (10.0.2.15)  <------>  Firewall/DHCP server <-----> Internet
50324b2298SPaolo Bonzini                              |          (10.0.2.2)
51324b2298SPaolo Bonzini                              |
52324b2298SPaolo Bonzini                              ---->  DNS server (10.0.2.3)
53324b2298SPaolo Bonzini                              |
54324b2298SPaolo Bonzini                              ---->  SMB server (10.0.2.4)
55324b2298SPaolo Bonzini
56324b2298SPaolo BonziniThe QEMU VM behaves as if it was behind a firewall which blocks all
57324b2298SPaolo Bonziniincoming connections. You can use a DHCP client to automatically
58324b2298SPaolo Bonziniconfigure the network in the QEMU VM. The DHCP server assign addresses
59324b2298SPaolo Bonzinito the hosts starting from 10.0.2.15.
60324b2298SPaolo Bonzini
61324b2298SPaolo BonziniIn order to check that the user mode network is working, you can ping
62324b2298SPaolo Bonzinithe address 10.0.2.2 and verify that you got an address in the range
63324b2298SPaolo Bonzini10.0.2.x from the QEMU virtual DHCP server.
64324b2298SPaolo Bonzini
65324b2298SPaolo BonziniNote that ICMP traffic in general does not work with user mode
66324b2298SPaolo Bonzininetworking. ``ping``, aka. ICMP echo, to the local router (10.0.2.2)
67324b2298SPaolo Bonzinishall work, however. If you're using QEMU on Linux >= 3.0, it can use
68324b2298SPaolo Bonziniunprivileged ICMP ping sockets to allow ``ping`` to the Internet. The
69324b2298SPaolo Bonzinihost admin has to set the ping_group_range in order to grant access to
70324b2298SPaolo Bonzinithose sockets. To allow ping for GID 100 (usually users group)::
71324b2298SPaolo Bonzini
72324b2298SPaolo Bonzini   echo 100 100 > /proc/sys/net/ipv4/ping_group_range
73324b2298SPaolo Bonzini
74324b2298SPaolo BonziniWhen using the built-in TFTP server, the router is also the TFTP server.
75324b2298SPaolo Bonzini
76324b2298SPaolo BonziniWhen using the ``'-netdev user,hostfwd=...'`` option, TCP or UDP
77324b2298SPaolo Bonziniconnections can be redirected from the host to the guest. It allows for
78324b2298SPaolo Bonziniexample to redirect X11, telnet or SSH connections.
79324b2298SPaolo Bonzini
80*94a9471cSLaurent VivierUsing passt as the user mode network stack
81*94a9471cSLaurent Vivier~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82*94a9471cSLaurent Vivier
83*94a9471cSLaurent Vivierpasst_ can be used as a simple replacement for SLIRP (``-net user``).
84*94a9471cSLaurent Vivierpasst doesn't require any capability or privilege. passt has
85*94a9471cSLaurent Vivierbetter performance than ``-net user``, full IPv6 support and better security
86*94a9471cSLaurent Vivieras it's a daemon that is not executed in QEMU context.
87*94a9471cSLaurent Vivier
88*94a9471cSLaurent Vivierpasst can be connected to QEMU either by using a socket
89*94a9471cSLaurent Vivier(``-netdev stream``) or using the vhost-user interface (``-netdev vhost-user``).
90*94a9471cSLaurent VivierSee `passt(1)`_ for more details on passt.
91*94a9471cSLaurent Vivier
92*94a9471cSLaurent Vivier.. _passt: https://passt.top/
93*94a9471cSLaurent Vivier.. _passt(1): https://passt.top/builds/latest/web/passt.1.html
94*94a9471cSLaurent Vivier
95*94a9471cSLaurent VivierTo use socket based passt interface:
96*94a9471cSLaurent Vivier^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97*94a9471cSLaurent Vivier
98*94a9471cSLaurent VivierStart passt as a daemon::
99*94a9471cSLaurent Vivier
100*94a9471cSLaurent Vivier   passt --socket ~/passt.socket
101*94a9471cSLaurent Vivier
102*94a9471cSLaurent VivierIf ``--socket`` is not provided, passt will print the path of the UNIX domain socket QEMU can connect to (``/tmp/passt_1.socket``, ``/tmp/passt_2.socket``,
103*94a9471cSLaurent Vivier...). Then you can connect your QEMU instance to passt:
104*94a9471cSLaurent Vivier
105*94a9471cSLaurent Vivier.. parsed-literal::
106*94a9471cSLaurent Vivier   |qemu_system| [...OPTIONS...] -device virtio-net-pci,netdev=netdev0 -netdev stream,id=netdev0,server=off,addr.type=unix,addr.path=~/passt.socket
107*94a9471cSLaurent Vivier
108*94a9471cSLaurent VivierWhere ``~/passt.socket`` is the UNIX socket created by passt to
109*94a9471cSLaurent Viviercommunicate with QEMU.
110*94a9471cSLaurent Vivier
111*94a9471cSLaurent VivierTo use vhost-based interface:
112*94a9471cSLaurent Vivier^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
113*94a9471cSLaurent Vivier
114*94a9471cSLaurent VivierStart passt with ``--vhost-user``::
115*94a9471cSLaurent Vivier
116*94a9471cSLaurent Vivier   passt --vhost-user --socket ~/passt.socket
117*94a9471cSLaurent Vivier
118*94a9471cSLaurent VivierThen to connect QEMU:
119*94a9471cSLaurent Vivier
120*94a9471cSLaurent Vivier.. parsed-literal::
121*94a9471cSLaurent Vivier   |qemu_system| [...OPTIONS...] -m $RAMSIZE -chardev socket,id=chr0,path=~/passt.socket -netdev vhost-user,id=netdev0,chardev=chr0 -device virtio-net,netdev=netdev0 -object memory-backend-memfd,id=memfd0,share=on,size=$RAMSIZE -numa node,memdev=memfd0
122*94a9471cSLaurent Vivier
123*94a9471cSLaurent VivierWhere ``$RAMSIZE`` is the memory size of your VM ``-m`` and ``-object memory-backend-memfd,size=`` must match.
124*94a9471cSLaurent Vivier
125*94a9471cSLaurent VivierMigration of passt:
126*94a9471cSLaurent Vivier^^^^^^^^^^^^^^^^^^^
127*94a9471cSLaurent Vivier
128*94a9471cSLaurent VivierWhen passt is connected to QEMU using the vhost-user interface it can
129*94a9471cSLaurent Vivierbe migrated with QEMU and the network connections are not interrupted.
130*94a9471cSLaurent Vivier
131*94a9471cSLaurent VivierAs passt runs with no privileges, it relies on passt-repair to save and
132*94a9471cSLaurent Vivierload the TCP connections state, using the TCP_REPAIR socket option.
133*94a9471cSLaurent VivierThe passt-repair helper needs to have the CAP_NET_ADMIN capability, or run as root. If passt-repair is not available, TCP connections will not be preserved.
134*94a9471cSLaurent Vivier
135*94a9471cSLaurent VivierExample of migration of a guest on the same host
136*94a9471cSLaurent Vivier________________________________________________
137*94a9471cSLaurent Vivier
138*94a9471cSLaurent VivierBefore being able to run passt-repair, the CAP_NET_ADMIN capability must be set
139*94a9471cSLaurent Vivieron the file, run as root::
140*94a9471cSLaurent Vivier
141*94a9471cSLaurent Vivier   setcap cap_net_admin+eip ./passt-repair
142*94a9471cSLaurent Vivier
143*94a9471cSLaurent VivierStart passt for the source side::
144*94a9471cSLaurent Vivier
145*94a9471cSLaurent Vivier   passt --vhost-user --socket ~/passt_src.socket --repair-path ~/passt-repair_src.socket
146*94a9471cSLaurent Vivier
147*94a9471cSLaurent VivierWhere ``~/passt-repair_src.socket`` is the UNIX socket created by passt to
148*94a9471cSLaurent Viviercommunicate with passt-repair. The default value is the ``--socket`` path
149*94a9471cSLaurent Vivierappended with ``.repair``.
150*94a9471cSLaurent Vivier
151*94a9471cSLaurent VivierStart passt-repair::
152*94a9471cSLaurent Vivier
153*94a9471cSLaurent Vivier   passt-repair ~/passt-repair_src.socket
154*94a9471cSLaurent Vivier
155*94a9471cSLaurent VivierStart source side QEMU with a monitor to be able to send the migrate command:
156*94a9471cSLaurent Vivier
157*94a9471cSLaurent Vivier.. parsed-literal::
158*94a9471cSLaurent Vivier   |qemu_system| [...OPTIONS...] [...VHOST USER OPTIONS...] -monitor stdio
159*94a9471cSLaurent Vivier
160*94a9471cSLaurent VivierStart passt for the destination side::
161*94a9471cSLaurent Vivier
162*94a9471cSLaurent Vivier   passt --vhost-user --socket ~/passt_dst.socket --repair-path ~/passt-repair_dst.socket
163*94a9471cSLaurent Vivier
164*94a9471cSLaurent VivierStart passt-repair::
165*94a9471cSLaurent Vivier
166*94a9471cSLaurent Vivier   passt-repair ~/passt-repair_dst.socket
167*94a9471cSLaurent Vivier
168*94a9471cSLaurent VivierStart QEMU with the ``-incoming`` parameter:
169*94a9471cSLaurent Vivier
170*94a9471cSLaurent Vivier.. parsed-literal::
171*94a9471cSLaurent Vivier   |qemu_system| [...OPTIONS...] [...VHOST USER OPTIONS...] -incoming tcp:localhost:4444
172*94a9471cSLaurent Vivier
173*94a9471cSLaurent VivierThen in the source guest monitor the migration can be started::
174*94a9471cSLaurent Vivier
175*94a9471cSLaurent Vivier   (qemu) migrate tcp:localhost:4444
176*94a9471cSLaurent Vivier
177*94a9471cSLaurent VivierA separate passt-repair instance must be started for every migration. In the case of a failed migration, passt-repair also needs to be restarted before trying
178*94a9471cSLaurent Vivieragain.
179*94a9471cSLaurent Vivier
180324b2298SPaolo BonziniHubs
181324b2298SPaolo Bonzini~~~~
182324b2298SPaolo Bonzini
183324b2298SPaolo BonziniQEMU can simulate several hubs. A hub can be thought of as a virtual
184324b2298SPaolo Bonziniconnection between several network devices. These devices can be for
185324b2298SPaolo Bonziniexample QEMU virtual ethernet cards or virtual Host ethernet devices
186324b2298SPaolo Bonzini(TAP devices). You can connect guest NICs or host network backends to
187324b2298SPaolo Bonzinisuch a hub using the ``-netdev
188324b2298SPaolo Bonzinihubport`` or ``-nic hubport`` options. The legacy ``-net`` option also
189324b2298SPaolo Bonziniconnects the given device to the emulated hub with ID 0 (i.e. the
190324b2298SPaolo Bonzinidefault hub) unless you specify a netdev with ``-net nic,netdev=xxx``
191324b2298SPaolo Bonzinihere.
192324b2298SPaolo Bonzini
193324b2298SPaolo BonziniConnecting emulated networks between QEMU instances
194324b2298SPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
195324b2298SPaolo Bonzini
196324b2298SPaolo BonziniUsing the ``-netdev socket`` (or ``-nic socket`` or ``-net socket``)
197324b2298SPaolo Bonzinioption, it is possible to create emulated networks that span several
198324b2298SPaolo BonziniQEMU instances. See the description of the ``-netdev socket`` option in
199324b2298SPaolo Bonzini:ref:`sec_005finvocation` to have a basic
200324b2298SPaolo Bonziniexample.
201