xref: /cloud-hypervisor/docs/hotplug.md (revision 42e9632c53d14cd0040db4952d40ba806c4b6ee9)
16444e29bSRob Bradford# Cloud Hypervisor Hot Plug
26444e29bSRob Bradford
3859f72b5SLiang ZhouCurrently Cloud Hypervisor supports hot plugging of CPUs devices (x86 only), PCI devices and memory resizing.
46444e29bSRob Bradford
56444e29bSRob Bradford## Kernel support
66444e29bSRob Bradford
76444e29bSRob BradfordFor hotplug on Cloud Hypervisor ACPI GED support is needed. This can either be achieved by turning on `CONFIG_ACPI_REDUCED_HARDWARE_ONLY`
8a3342bdbSSebastien Boeufor by using this kernel patch (available in 5.5-rc1 and later): https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/patch/drivers/acpi/Makefile?id=ac36d37e943635fc072e9d4f47e40a48fbcdb3f0
96444e29bSRob Bradford
106444e29bSRob Bradford## CPU Hot Plug
116444e29bSRob Bradford
1280bbc471SZiye YangExtra vCPUs can be added and removed from a running `cloud-hypervisor` instance. This is controlled by two mechanisms:
136444e29bSRob Bradford
146444e29bSRob Bradford1. Specifying a number of maximum potential vCPUs that is greater than the number of default (boot) vCPUs.
1514041e97SRob Bradford2. Making a HTTP API request to the VMM to ask for the additional vCPUs to be added.
166444e29bSRob Bradford
176444e29bSRob BradfordTo use CPU hotplug start the VM with the number of max vCPUs greater than the number of boot vCPUs, e.g.
186444e29bSRob Bradford
196444e29bSRob Bradford```shell
206444e29bSRob Bradford$ pushd $CLOUDH
216444e29bSRob Bradford$ sudo setcap cap_net_admin+ep ./cloud-hypervisor/target/release/cloud-hypervisor
226444e29bSRob Bradford$ ./cloud-hypervisor/target/release/cloud-hypervisor \
23a3342bdbSSebastien Boeuf	--kernel custom-vmlinux.bin \
24a3342bdbSSebastien Boeuf	--cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \
25a3342bdbSSebastien Boeuf	--disk path=focal-server-cloudimg-amd64.raw \
266444e29bSRob Bradford	--cpus boot=4,max=8 \
276444e29bSRob Bradford	--memory size=1024M \
286444e29bSRob Bradford	--net "tap=,mac=,ip=,mask=" \
296444e29bSRob Bradford	--rng \
30fa22cb0bSRavi kumar Veeramally	--api-socket=/tmp/ch-socket
316444e29bSRob Bradford$ popd
326444e29bSRob Bradford```
336444e29bSRob Bradford
34fa22cb0bSRavi kumar VeeramallyNotice the addition of `--api-socket=/tmp/ch-socket` and a `max` parameter on `--cpus boot=4,max=8`.
356444e29bSRob Bradford
366444e29bSRob BradfordTo ask the VMM to add additional vCPUs then use the resize API:
376444e29bSRob Bradford
386444e29bSRob Bradford```shell
39fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket resize --cpus 8
406444e29bSRob Bradford```
416444e29bSRob Bradford
4232506dadSRob BradfordThe extra vCPU threads will be created and advertised to the running kernel. The kernel does not bring up the CPUs immediately and instead the user must "online" them from inside the VM:
436444e29bSRob Bradford
446444e29bSRob Bradford```shell
456444e29bSRob Bradfordroot@ch-guest ~ # lscpu | grep list:
466444e29bSRob BradfordOn-line CPU(s) list:             0-3
476444e29bSRob BradfordOff-line CPU(s) list:            4-7
486444e29bSRob Bradfordroot@ch-guest ~ # echo 1 | tee /sys/devices/system/cpu/cpu[4,5,6,7]/online
496444e29bSRob Bradford1
506444e29bSRob Bradfordroot@ch-guest ~ # lscpu | grep list:
516444e29bSRob BradfordOn-line CPU(s) list:             0-7
526444e29bSRob Bradford```
536444e29bSRob Bradford
546444e29bSRob BradfordAfter a reboot the added CPUs will remain.
556444e29bSRob Bradford
56*42e9632cSJosh SorefRemoving CPUs works similarly by reducing the number in the "desired_vcpus" field of the resize API. The CPUs will be automatically offlined inside the guest so there is no need to run any commands inside the guest:
5732506dadSRob Bradford
5832506dadSRob Bradford```shell
59fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket resize --cpus 2
6032506dadSRob Bradford```
6132506dadSRob Bradford
6232506dadSRob BradfordAs per adding CPUs to the guest, after a reboot the VM will be running with the reduced number of vCPUs.
6314041e97SRob Bradford
6414041e97SRob Bradford## Memory Hot Plug
6514041e97SRob Bradford
6655814861SSebastien Boeuf### ACPI method
6755814861SSebastien Boeuf
6880bbc471SZiye YangExtra memory can be added from a running `cloud-hypervisor` instance. This is controlled by two mechanisms:
6914041e97SRob Bradford
7014041e97SRob Bradford1. Allocating some of the guest physical address space for hotplug memory.
7114041e97SRob Bradford2. Making a HTTP API request to the VMM to ask for a new amount of RAM to be assigned to the VM. In the case of expanding the memory for the VM the new memory will be hotplugged into the running VM, if reducing the size of the memory then change will take effect after the next reboot.
7214041e97SRob Bradford
7355814861SSebastien BoeufTo use memory hotplug start the VM specifying some size RAM in the `hotplug_size` parameter to the memory configuration. Not all the memory specified in this parameter will be available to hotplug as there are spacing and alignment requirements so it is recommended to make it larger than the hotplug RAM needed.
7455814861SSebastien Boeuf
7555814861SSebastien BoeufBecause the ACPI method is the default, there is no need to add the extra option `hotplug_method=acpi`.
7614041e97SRob Bradford
7714041e97SRob Bradford```shell
7814041e97SRob Bradford$ pushd $CLOUDH
7914041e97SRob Bradford$ sudo setcap cap_net_admin+ep ./cloud-hypervisor/target/release/cloud-hypervisor
8014041e97SRob Bradford$ ./cloud-hypervisor/target/release/cloud-hypervisor \
81a3342bdbSSebastien Boeuf	--kernel custom-vmlinux.bin \
82a3342bdbSSebastien Boeuf	--cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \
83a3342bdbSSebastien Boeuf	--disk path=focal-server-cloudimg-amd64.raw \
8414041e97SRob Bradford	--cpus boot=4,max=8 \
8514041e97SRob Bradford	--memory size=1024M,hotplug_size=8192M \
8614041e97SRob Bradford	--net "tap=,mac=,ip=,mask=" \
8714041e97SRob Bradford	--rng \
88fa22cb0bSRavi kumar Veeramally	--api-socket=/tmp/ch-socket
8914041e97SRob Bradford$ popd
9014041e97SRob Bradford```
9114041e97SRob Bradford
9214041e97SRob BradfordBefore issuing the API request it is necessary to run the following command inside the VM to make it automatically online the added memory:
9314041e97SRob Bradford
9414041e97SRob Bradford```shell
9514041e97SRob Bradfordroot@ch-guest ~ # echo online | sudo tee /sys/devices/system/memory/auto_online_blocks
9614041e97SRob Bradford```
9714041e97SRob Bradford
981cd186c8SSebastien BoeufTo ask the VMM to expand the RAM for the VM:
9914041e97SRob Bradford
10014041e97SRob Bradford```shell
101fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket resize --memory 3G
10214041e97SRob Bradford```
10314041e97SRob Bradford
10414041e97SRob BradfordThe new memory is now available to use inside the VM:
10514041e97SRob Bradford
10614041e97SRob Bradford```shell
10714041e97SRob Bradfordfree -h
10814041e97SRob Bradford              total        used        free      shared  buff/cache   available
10914041e97SRob BradfordMem:          3.0Gi        71Mi       2.8Gi       0.0Ki        47Mi       2.8Gi
11014041e97SRob BradfordSwap:          32Mi          0B        32Mi
11114041e97SRob Bradford```
11214041e97SRob Bradford
11314041e97SRob BradfordDue to guest OS limitations is is necessary to ensure that amount of memory added (between currently assigned RAM and that which is desired) is a multiple of 128MiB.
11414041e97SRob Bradford
11514041e97SRob BradfordThe same API can also be used to reduce the desired RAM for a VM but the change will not be applied until the VM is rebooted.
11614041e97SRob Bradford
11714041e97SRob BradfordMemory and CPU resizing can be combined together into the same HTTP API request.
1183e3c1632SSebastien Boeuf
11955814861SSebastien Boeuf### virtio-mem method
12055814861SSebastien Boeuf
12155814861SSebastien BoeufExtra memory can be added and removed from a running Cloud Hypervisor instance. This is controlled by two mechanisms:
12255814861SSebastien Boeuf
12355814861SSebastien Boeuf1. Allocating some of the guest physical address space for hotplug memory.
12455814861SSebastien Boeuf2. Making a HTTP API request to the VMM to ask for a new amount of RAM to be assigned to the VM.
12555814861SSebastien Boeuf
12655814861SSebastien BoeufTo use memory hotplug start the VM specifying some size RAM in the `hotplug_size` parameter along with `hotplug_method=virtio-mem` to the memory configuration.
12755814861SSebastien Boeuf
12855814861SSebastien Boeuf```shell
12955814861SSebastien Boeuf$ pushd $CLOUDH
13055814861SSebastien Boeuf$ sudo setcap cap_net_admin+ep ./cloud-hypervisor/target/release/cloud-hypervisor
13155814861SSebastien Boeuf$ ./cloud-hypervisor/target/release/cloud-hypervisor \
13255814861SSebastien Boeuf	--kernel custom-vmlinux.bin \
13355814861SSebastien Boeuf	--cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \
13455814861SSebastien Boeuf	--disk path=focal-server-cloudimg-amd64.raw \
13555814861SSebastien Boeuf	--memory size=1024M,hotplug_size=8192M,hotplug_method=virtio-mem \
13655814861SSebastien Boeuf	--net "tap=,mac=,ip=,mask=" \
137fa22cb0bSRavi kumar Veeramally	--api-socket=/tmp/ch-socket
13855814861SSebastien Boeuf$ popd
13955814861SSebastien Boeuf```
14055814861SSebastien Boeuf
14155814861SSebastien BoeufTo ask the VMM to expand the RAM for the VM (request is in bytes):
14255814861SSebastien Boeuf
14355814861SSebastien Boeuf```shell
144fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket resize --memory 3G
14555814861SSebastien Boeuf```
14655814861SSebastien Boeuf
14755814861SSebastien BoeufThe new memory is now available to use inside the VM:
14855814861SSebastien Boeuf
14955814861SSebastien Boeuf```shell
15055814861SSebastien Boeuffree -h
15155814861SSebastien Boeuf              total        used        free      shared  buff/cache   available
15255814861SSebastien BoeufMem:          3.0Gi        71Mi       2.8Gi       0.0Ki        47Mi       2.8Gi
15355814861SSebastien BoeufSwap:          32Mi          0B        32Mi
15455814861SSebastien Boeuf```
15555814861SSebastien Boeuf
15655814861SSebastien BoeufThe same API can also be used to reduce the desired RAM for a VM. It is important to note that reducing RAM size might only partially work, as the guest might be using some of it.
15755814861SSebastien Boeuf
1583e3c1632SSebastien Boeuf## PCI Device Hot Plug
1593e3c1632SSebastien Boeuf
16080bbc471SZiye YangExtra PCI devices can be added and removed from a running `cloud-hypervisor` instance. This is controlled by making a HTTP API request to the VMM to ask for the additional device to be added, or for the existing device to be removed.
1613e3c1632SSebastien Boeuf
1623377e0e9SBo ChenNote: On AArch64 platform, PCI device hotplug can only be achieved using ACPI. Please refer to the [documentation](uefi.md#building-uefi-firmware-for-aarch64) for more information.
163dd6f07daSHenry Wang
1643e3c1632SSebastien BoeufTo use PCI device hotplug start the VM with the HTTP server.
1653e3c1632SSebastien Boeuf
1663e3c1632SSebastien Boeuf```shell
1673e3c1632SSebastien Boeuf$ sudo setcap cap_net_admin+ep ./cloud-hypervisor/target/release/cloud-hypervisor
1683e3c1632SSebastien Boeuf$ ./cloud-hypervisor/target/release/cloud-hypervisor \
1693e3c1632SSebastien Boeuf	--kernel custom-vmlinux.bin \
1703e3c1632SSebastien Boeuf	--cmdline "console=ttyS0 console=hvc0 root=/dev/vda1 rw" \
1713e3c1632SSebastien Boeuf	--disk path=focal-server-cloudimg-amd64.raw \
1723e3c1632SSebastien Boeuf	--cpus boot=4 \
1733e3c1632SSebastien Boeuf	--memory size=1024M \
1743e3c1632SSebastien Boeuf	--net "tap=,mac=,ip=,mask=" \
175fa22cb0bSRavi kumar Veeramally	--api-socket=/tmp/ch-socket
1763e3c1632SSebastien Boeuf```
1773e3c1632SSebastien Boeuf
178fa22cb0bSRavi kumar VeeramallyNotice the addition of `--api-socket=/tmp/ch-socket`.
1793e3c1632SSebastien Boeuf
1803e3c1632SSebastien Boeuf### Add VFIO Device
1813e3c1632SSebastien Boeuf
1823e3c1632SSebastien BoeufTo ask the VMM to add additional VFIO device then use the `add-device` API.
1833e3c1632SSebastien Boeuf
1843e3c1632SSebastien Boeuf```shell
185fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket add-device path=/sys/bus/pci/devices/0000:01:00.0/
1863e3c1632SSebastien Boeuf```
1873e3c1632SSebastien Boeuf
1883e3c1632SSebastien Boeuf### Add Disk Device
1893e3c1632SSebastien Boeuf
1903e3c1632SSebastien BoeufTo ask the VMM to add additional disk device then use the `add-disk` API.
1913e3c1632SSebastien Boeuf
1923e3c1632SSebastien Boeuf```shell
193fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket add-disk path=/foo/bar/cloud.img
1943e3c1632SSebastien Boeuf```
1953e3c1632SSebastien Boeuf
1963e3c1632SSebastien Boeuf### Add Fs Device
1973e3c1632SSebastien Boeuf
1983e3c1632SSebastien BoeufTo ask the VMM to add additional fs device then use the `add-fs` API.
1993e3c1632SSebastien Boeuf
2003e3c1632SSebastien Boeuf```shell
201fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket add-fs tag=myfs,socket=/foo/bar/virtiofs.sock
2023e3c1632SSebastien Boeuf```
2033e3c1632SSebastien Boeuf
2043e3c1632SSebastien Boeuf### Add Net Device
2053e3c1632SSebastien Boeuf
2063e3c1632SSebastien BoeufTo ask the VMM to add additional network device then use the `add-net` API.
2073e3c1632SSebastien Boeuf
2083e3c1632SSebastien Boeuf```shell
209fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket add-net tap=chtap0
2103e3c1632SSebastien Boeuf```
2113e3c1632SSebastien Boeuf
2123e3c1632SSebastien Boeuf### Add Pmem Device
2133e3c1632SSebastien Boeuf
2143e3c1632SSebastien BoeufTo ask the VMM to add additional PMEM device then use the `add-pmem` API.
2153e3c1632SSebastien Boeuf
2163e3c1632SSebastien Boeuf```shell
217fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket add-pmem file=/foo/bar.cloud.img
2183e3c1632SSebastien Boeuf```
2193e3c1632SSebastien Boeuf
2203e3c1632SSebastien Boeuf### Add Vsock Device
2213e3c1632SSebastien Boeuf
2223e3c1632SSebastien BoeufTo ask the VMM to add additional vsock device then use the `add-vsock` API.
2233e3c1632SSebastien Boeuf
2243e3c1632SSebastien Boeuf```shell
225fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket add-vsock cid=3,socket=/foo/bar/vsock.sock
2263e3c1632SSebastien Boeuf```
2273e3c1632SSebastien Boeuf
2283e3c1632SSebastien Boeuf### Common Across All PCI Devices
2293e3c1632SSebastien Boeuf
2303e3c1632SSebastien BoeufThe extra PCI device will be created and advertised to the running kernel. The new device can be found by checking the list of PCI devices.
2313e3c1632SSebastien Boeuf
2323e3c1632SSebastien Boeuf```shell
2333e3c1632SSebastien Boeufroot@ch-guest ~ # lspci
2343e3c1632SSebastien Boeuf00:00.0 Host bridge: Intel Corporation Device 0d57
2353e3c1632SSebastien Boeuf00:01.0 Unassigned class [ffff]: Red Hat, Inc. Virtio console (rev 01)
2363e3c1632SSebastien Boeuf00:02.0 Mass storage controller: Red Hat, Inc. Virtio block device (rev 01)
2373e3c1632SSebastien Boeuf00:03.0 Unassigned class [ffff]: Red Hat, Inc. Virtio RNG (rev 01)
2383e3c1632SSebastien Boeuf```
2393e3c1632SSebastien Boeuf
2403e3c1632SSebastien BoeufAfter a reboot the added PCI device will remain.
2413e3c1632SSebastien Boeuf
2423e3c1632SSebastien Boeuf### Remove PCI device
2433e3c1632SSebastien Boeuf
2443e3c1632SSebastien BoeufRemoving a PCI device works the same way for all kind of PCI devices. The unique identifier related to the device must be provided. This identifier can be provided by the user when adding the new device, or by default Cloud Hypervisor will assign one.
2453e3c1632SSebastien Boeuf
2463e3c1632SSebastien Boeuf```shell
247fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/ch-socket remove-device _disk0
2483e3c1632SSebastien Boeuf```
2493e3c1632SSebastien Boeuf
2503e3c1632SSebastien BoeufAs per adding a PCI device to the guest, after a reboot the VM will be running without the removed PCI device.
251