|
| 1 | +/* |
| 2 | + * ch_hostdev.h: Cloud Hypervisor hotplug support |
| 3 | + * |
| 4 | + * Copyright Intel Corp. 2020 |
| 5 | + * |
| 6 | + * This library is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 2.1 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this library. If not, see |
| 18 | + * <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "ch_cgroup.h" |
| 22 | +#include "ch_domain.h" |
| 23 | +#include "ch_hotplug.h" |
| 24 | +#include "ch_monitor.h" |
| 25 | +#include "ch_process.h" |
| 26 | +#include "domain_audit.h" |
| 27 | +#include "virerror.h" |
| 28 | +#include "virlog.h" |
| 29 | + |
| 30 | +#define VIR_FROM_THIS VIR_FROM_CH |
| 31 | + |
| 32 | +VIR_LOG_INIT("ch.ch_hotplug"); |
| 33 | + |
| 34 | +/** |
| 35 | + * virCHDomainSelectHotplugVcpuEntities: |
| 36 | + * |
| 37 | + * @def: domain definition |
| 38 | + * @nvcpus: target vcpu count |
| 39 | + * |
| 40 | + * Validate vcpu entities will be able to be enabled/disabled. |
| 41 | + * |
| 42 | + * Returns the true if the nvcpus can be set, false if not. |
| 43 | + */ |
| 44 | +static bool |
| 45 | +virCHDomainValidateHotplugVcpuEntities(virDomainDefPtr def, |
| 46 | + unsigned int nvcpus) |
| 47 | +{ |
| 48 | + unsigned int maxvcpus = virDomainDefGetVcpusMax(def); |
| 49 | + |
| 50 | + if (nvcpus < 1 || nvcpus > maxvcpus) { |
| 51 | + virReportError(VIR_ERR_INVALID_ARG, |
| 52 | + _("requested vcpus is greater than max allowable" |
| 53 | + " vcpus for the live domain: %u > %u"), |
| 54 | + nvcpus, maxvcpus); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +static int |
| 62 | +virCHDomainHotplugSetVcpus(virDomainObjPtr vm, |
| 63 | + unsigned int nvcpus) |
| 64 | +{ |
| 65 | + unsigned int maxvcpus = virDomainDefGetVcpusMax(vm->def); |
| 66 | + int oldvcpus = virDomainDefGetVcpus(vm->def); |
| 67 | + virCHDomainObjPrivatePtr priv = vm->privateData; |
| 68 | + virCHMonitorPtr mon = priv->monitor; |
| 69 | + int ret = -1; |
| 70 | + |
| 71 | + VIR_DEBUG("Setting cpu count to %d", nvcpus); |
| 72 | + ret = virCHMonitorResizeCPU(mon, nvcpus); |
| 73 | + |
| 74 | + virDomainAuditVcpu(vm, oldvcpus, nvcpus, "update", ret == 0); |
| 75 | + |
| 76 | + if (ret < 0) |
| 77 | + goto error; |
| 78 | + |
| 79 | + /* Set cpu status for validation (after the monitor sees the change) */ |
| 80 | + for (int i = 0; i < nvcpus; i++) { |
| 81 | + virDomainVcpuDefPtr vcpuinfo = virDomainDefGetVcpu(vm->def, i); |
| 82 | + vcpuinfo->online = true; |
| 83 | + } |
| 84 | + for (int i = nvcpus; i < maxvcpus; i++) { |
| 85 | + virDomainVcpuDefPtr vcpuinfo = virDomainDefGetVcpu(vm->def, i); |
| 86 | + vcpuinfo->online = false; |
| 87 | + } |
| 88 | + |
| 89 | + ret = 0; |
| 90 | + |
| 91 | + error: |
| 92 | + return ret; |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +static int |
| 97 | +virCHDomainSetVcpusLive(virDomainObjPtr vm, |
| 98 | + unsigned int nvcpus) |
| 99 | +{ |
| 100 | + virCHDomainObjPrivatePtr priv = vm->privateData; |
| 101 | + chCgroupEmulatorAllNodesDataPtr emulatorCgroup = NULL; |
| 102 | + int ret = -1; |
| 103 | + |
| 104 | + if (chCgroupEmulatorAllNodesAllow(priv->cgroup, &emulatorCgroup) < 0) |
| 105 | + goto cleanup; |
| 106 | + |
| 107 | + if (virCHDomainHotplugSetVcpus(vm, nvcpus) < 0) |
| 108 | + goto cleanup; |
| 109 | + |
| 110 | + ret = 0; |
| 111 | + |
| 112 | + cleanup: |
| 113 | + chCgroupEmulatorAllNodesRestore(emulatorCgroup); |
| 114 | + |
| 115 | + return ret; |
| 116 | +} |
| 117 | + |
| 118 | +int |
| 119 | +virCHDomainSetVcpusInternal(virDomainObjPtr vm, |
| 120 | + virDomainDefPtr def, |
| 121 | + unsigned int nvcpus) |
| 122 | +{ |
| 123 | + int ret = -1; |
| 124 | + |
| 125 | + if (def) { |
| 126 | + if (!virCHDomainValidateHotplugVcpuEntities(def, nvcpus)) |
| 127 | + goto error; |
| 128 | + |
| 129 | + if (virCHDomainSetVcpusLive(vm, nvcpus) < 0) |
| 130 | + goto error; |
| 131 | + } |
| 132 | + |
| 133 | + ret = 0; |
| 134 | + |
| 135 | + error: |
| 136 | + return ret; |
| 137 | +} |
0 commit comments