virnodesuspend.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * virnodesuspend.c: Support for suspending a node (host machine)
 *
 * Copyright (C) 2011 Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

#include <config.h>
#include "virnodesuspend.h"

#include "command.h"
#include "threads.h"
#include "datatypes.h"

#include "memory.h"
#include "logging.h"
#include "virterror_internal.h"

#define VIR_FROM_THIS VIR_FROM_NONE

#define SUSPEND_DELAY 10 /* in seconds */

/* Give sufficient time for performing the suspend operation on the host */
#define MIN_TIME_REQ_FOR_SUSPEND 60 /* in seconds */

/*
 * Bitmask to hold the Power Management features supported by the host,
 * such as Suspend-to-RAM, Suspend-to-Disk, Hybrid-Suspend etc.
 */
44 45
static unsigned int nodeSuspendTargetMask;
static bool nodeSuspendTargetMaskInit;
46

47
static virMutex virNodeSuspendMutex;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

static bool aboutToSuspend;

static void virNodeSuspendLock(void)
{
    virMutexLock(&virNodeSuspendMutex);
}

static void virNodeSuspendUnlock(void)
{
    virMutexUnlock(&virNodeSuspendMutex);
}


/**
 * virNodeSuspendInit:
 *
 * Get the system-wide sleep states supported by the host, such as
 * Suspend-to-RAM, Suspend-to-Disk, or Hybrid-Suspend, so that a request
 * to suspend/hibernate the host can be handled appropriately based on
 * this information.
 *
 * Returns 0 if successful, and -1 in case of error.
 */
int virNodeSuspendInit(void)
{
    if (virMutexInit(&virNodeSuspendMutex) < 0)
        return -1;

    return 0;
}


/**
 * virNodeSuspendSetNodeWakeup:
 * @alarmTime: time in seconds from now, at which the RTC alarm has to be set.
 *
 * Set up the RTC alarm to the specified time.
 * Return 0 on success, -1 on failure.
 */
static int virNodeSuspendSetNodeWakeup(unsigned long long alarmTime)
{
    virCommandPtr setAlarmCmd;
    int ret = -1;

    if (alarmTime <= MIN_TIME_REQ_FOR_SUSPEND) {
94
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("Suspend duration is too short"));
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        return -1;
    }

    setAlarmCmd = virCommandNewArgList("rtcwake", "-m", "no", "-s", NULL);
    virCommandAddArgFormat(setAlarmCmd, "%lld", alarmTime);

    if (virCommandRun(setAlarmCmd, NULL) < 0)
        goto cleanup;

    ret = 0;

cleanup:
    virCommandFree(setAlarmCmd);
    return ret;
}

/**
 * virNodeSuspend:
 * @cmdString: pointer to the command string this thread has to execute.
 *
 * Actually perform the suspend operation by invoking the command.
 * Give a short delay before executing the command so as to give a chance
 * to virNodeSuspendForDuration() to return the status to the caller.
 * If we don't give this delay, that function will not be able to return
 * the status, since the suspend operation would have begun and hence no
 * data can be sent through the connection to the caller. However, with
 * this delay added, the status return is best-effort only.
 */
static void virNodeSuspend(void *cmdString)
{
125
    virCommandPtr suspendCmd = virCommandNew((const char *)cmdString);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    /*
     * Delay for sometime so that the function nodeSuspendForDuration()
     * can return the status to the caller.
     */
    sleep(SUSPEND_DELAY);
    if (virCommandRun(suspendCmd, NULL) < 0)
        VIR_WARN("Failed to suspend the host");

    virCommandFree(suspendCmd);

    /*
     * Now that we have resumed from suspend or the suspend failed,
     * reset 'aboutToSuspend' flag.
     */
    virNodeSuspendLock();
    aboutToSuspend = false;
    virNodeSuspendUnlock();
}

/**
 * nodeSuspendForDuration:
 * @conn: pointer to the hypervisor connection
 * @target: the state to which the host must be suspended to -
 *         VIR_NODE_SUSPEND_TARGET_MEM       (Suspend-to-RAM),
 *         VIR_NODE_SUSPEND_TARGET_DISK      (Suspend-to-Disk),
 *         VIR_NODE_SUSPEND_TARGET_HYBRID    (Hybrid-Suspend)
 * @duration: the time duration in seconds, for which the host must be
 *            suspended
 * @flags: any flag values that might need to be passed; currently unused.
 *
 * Suspend the node (host machine) for the given duration of time in the
 * specified state (such as Mem, Disk or Hybrid-Suspend). Attempt to resume
 * the node after the time duration is complete.
 *
 * First, an RTC alarm is set appropriately to wake up the node from its
 * sleep state. Then the actual node suspend operation is carried out
 * asynchronously in another thread, after a short time delay so as to
 * give enough time for this function to return status to its caller
 * through the connection. However this returning of status is best-effort
 * only, and should generally happen due to the short delay but might be
 * missed if the machine suspends first.
 *
 * Returns 0 in case the node is going to be suspended after a short delay,
 * -1 if suspending the node is not supported, or if a previous suspend
 * operation is still in progress.
 */
int nodeSuspendForDuration(virConnectPtr conn ATTRIBUTE_UNUSED,
                           unsigned int target,
                           unsigned long long duration,
                           unsigned int flags)
{
    static virThread thread;
179
    const char *cmdString = NULL;
180 181
    int ret = -1;
    unsigned int supported;
182 183 184

    virCheckFlags(0, -1);

185 186 187
    if (virNodeSuspendGetTargetMask(&supported) < 0)
        return -1;

188 189 190 191 192 193 194 195
    /*
     * Ensure that we are the only ones trying to suspend.
     * Fail if somebody has already initiated a suspend.
     */
    virNodeSuspendLock();

    if (aboutToSuspend) {
        /* A suspend operation is already in progress */
196 197
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Suspend operation already in progress"));
198
        goto cleanup;
199 200 201 202 203
    }

    /* Check if the host supports the requested suspend target */
    switch (target) {
    case VIR_NODE_SUSPEND_TARGET_MEM:
204
        if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_MEM))) {
205
            virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-RAM"));
206
            goto cleanup;
207
        }
208 209
        cmdString = "pm-suspend";
        break;
210 211

    case VIR_NODE_SUSPEND_TARGET_DISK:
212
        if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_DISK))) {
213
            virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-Disk"));
214
            goto cleanup;
215
        }
216 217
        cmdString = "pm-hibernate";
        break;
218 219

    case VIR_NODE_SUSPEND_TARGET_HYBRID:
220
        if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_HYBRID))) {
221
            virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Hybrid-Suspend"));
222
            goto cleanup;
223
        }
224 225
        cmdString = "pm-suspend-hybrid";
        break;
226 227

    default:
228
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid suspend target"));
229 230 231 232 233 234 235 236
        goto cleanup;
    }

    /* Just set the RTC alarm. Don't suspend yet. */
    if (virNodeSuspendSetNodeWakeup(duration) < 0)
        goto cleanup;

    if (virThreadCreate(&thread, false, virNodeSuspend, (void *)cmdString) < 0) {
237 238
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failed to create thread to suspend the host"));
239 240 241
        goto cleanup;
    }

242
    aboutToSuspend = true;
243
    ret = 0;
244
cleanup:
245 246
    virNodeSuspendUnlock();
    return ret;
247
}
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316


/**
 * virNodeSuspendSupportsTarget:
 * @target: The power management target to check whether it is supported
 *           by the host. Values could be:
 *           VIR_NODE_SUSPEND_TARGET_MEM
 *           VIR_NODE_SUSPEND_TARGET_DISK
 *           VIR_NODE_SUSPEND_TARGET_HYBRID
 * @supported: set to true if supported, false otherwise
 *
 * Run the script 'pm-is-supported' (from the pm-utils package)
 * to find out if @target is supported by the host.
 *
 * Returns 0 if the query was successful, -1 on failure.
 */
static int
virNodeSuspendSupportsTarget(unsigned int target, bool *supported)
{
    virCommandPtr cmd;
    int status;
    int ret = -1;

    *supported = false;

    switch (target) {
    case VIR_NODE_SUSPEND_TARGET_MEM:
        cmd = virCommandNewArgList("pm-is-supported", "--suspend", NULL);
        break;
    case VIR_NODE_SUSPEND_TARGET_DISK:
        cmd = virCommandNewArgList("pm-is-supported", "--hibernate", NULL);
        break;
    case VIR_NODE_SUSPEND_TARGET_HYBRID:
        cmd = virCommandNewArgList("pm-is-supported", "--suspend-hybrid", NULL);
        break;
    default:
        return ret;
    }

    if (virCommandRun(cmd, &status) < 0)
        goto cleanup;

   /*
    * Check return code of command == 0 for success
    * (i.e., the PM capability is supported)
    */
    *supported = (status == 0);
    ret = 0;

cleanup:
    virCommandFree(cmd);
    return ret;
}

/**
 * virNodeSuspendGetTargetMask:
 *
 * Get the Power Management Capabilities that the host system supports,
 * such as Suspend-to-RAM (S3), Suspend-to-Disk (S4) and Hybrid-Suspend
 * (a combination of S3 and S4).
 *
 * @bitmask: Pointer to the bitmask which will be set appropriately to
 *           indicate all the supported host power management targets.
 *
 * Returns 0 if the query was successful, -1 on failure.
 */
int
virNodeSuspendGetTargetMask(unsigned int *bitmask)
{
317
    int ret = -1;
318 319 320

    *bitmask = 0;

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    virNodeSuspendLock();
    /* Get the power management capabilities supported by the host */
    if (!nodeSuspendTargetMaskInit) {
        bool supported;
        nodeSuspendTargetMask = 0;

        /* Check support for Suspend-to-RAM (S3) */
        if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_MEM, &supported) < 0)
            goto cleanup;
        if (supported)
            nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_MEM);

        /* Check support for Suspend-to-Disk (S4) */
        if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_DISK, &supported) < 0)
            goto cleanup;
        if (supported)
            nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_DISK);

        /* Check support for Hybrid-Suspend */
        if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_HYBRID, &supported) < 0)
            goto cleanup;
        if (supported)
            nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_HYBRID);

        nodeSuspendTargetMaskInit = true;
    }
347

348 349 350 351 352
    *bitmask = nodeSuspendTargetMask;
    ret = 0;
cleanup:
    virNodeSuspendUnlock();
    return ret;
353
}