2013-10-04 16:52:03 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2013-10-04 16:52:03 +00:00
|
|
|
* Copyright (c) 2013 Peter Grehan <grehan@freebsd.org>
|
|
|
|
* All rights reserved.
|
2020-04-23 19:20:58 +00:00
|
|
|
* Copyright 2020 Joyent, Inc.
|
2013-10-04 16:52:03 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <sys/capsicum.h>
|
|
|
|
#endif
|
2013-10-04 16:52:03 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2019-01-16 00:39:23 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <capsicum_helpers.h>
|
|
|
|
#endif
|
2016-05-27 11:46:54 +00:00
|
|
|
#include <err.h>
|
2013-10-04 16:52:03 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <pthread_np.h>
|
2014-11-04 01:06:33 +00:00
|
|
|
#include <signal.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#include <sysexits.h>
|
2013-10-04 16:52:03 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
#include <machine/atomic.h>
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
#include <machine/vmm_snapshot.h>
|
2014-11-04 01:06:33 +00:00
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
#include "bhyverun.h"
|
2019-06-26 20:30:41 +00:00
|
|
|
#include "config.h"
|
2020-01-08 22:55:22 +00:00
|
|
|
#include "debug.h"
|
2014-11-04 01:06:33 +00:00
|
|
|
#include "mevent.h"
|
2019-06-26 20:30:41 +00:00
|
|
|
#include "pci_emul.h"
|
2013-10-04 16:52:03 +00:00
|
|
|
#include "block_if.h"
|
|
|
|
|
|
|
|
#define BLOCKIF_SIG 0xb109b109
|
|
|
|
|
2015-03-14 21:15:45 +00:00
|
|
|
#define BLOCKIF_NUMTHR 8
|
2019-05-02 22:46:37 +00:00
|
|
|
#define BLOCKIF_MAXREQ (BLOCKIF_RING_MAX + BLOCKIF_NUMTHR)
|
2013-10-04 16:52:03 +00:00
|
|
|
|
|
|
|
enum blockop {
|
|
|
|
BOP_READ,
|
|
|
|
BOP_WRITE,
|
2015-03-13 16:43:52 +00:00
|
|
|
BOP_FLUSH,
|
|
|
|
BOP_DELETE
|
2013-10-04 16:52:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum blockstat {
|
|
|
|
BST_FREE,
|
2015-03-14 21:15:45 +00:00
|
|
|
BST_BLOCK,
|
2014-11-04 01:06:33 +00:00
|
|
|
BST_PEND,
|
|
|
|
BST_BUSY,
|
|
|
|
BST_DONE
|
2013-10-04 16:52:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct blockif_elem {
|
|
|
|
TAILQ_ENTRY(blockif_elem) be_link;
|
|
|
|
struct blockif_req *be_req;
|
|
|
|
enum blockop be_op;
|
|
|
|
enum blockstat be_status;
|
2014-11-04 01:06:33 +00:00
|
|
|
pthread_t be_tid;
|
2015-03-14 21:15:45 +00:00
|
|
|
off_t be_block;
|
2013-10-04 16:52:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct blockif_ctxt {
|
|
|
|
int bc_magic;
|
|
|
|
int bc_fd;
|
2015-03-05 15:29:18 +00:00
|
|
|
int bc_ischr;
|
2015-04-18 20:10:19 +00:00
|
|
|
int bc_isgeom;
|
2015-03-13 16:43:52 +00:00
|
|
|
int bc_candelete;
|
2013-10-04 16:52:03 +00:00
|
|
|
int bc_rdonly;
|
|
|
|
off_t bc_size;
|
|
|
|
int bc_sectsz;
|
2015-03-05 12:21:12 +00:00
|
|
|
int bc_psectsz;
|
|
|
|
int bc_psectoff;
|
2015-03-14 21:15:45 +00:00
|
|
|
int bc_closing;
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
int bc_paused;
|
|
|
|
int bc_work_count;
|
2015-03-14 21:15:45 +00:00
|
|
|
pthread_t bc_btid[BLOCKIF_NUMTHR];
|
2018-06-14 01:34:53 +00:00
|
|
|
pthread_mutex_t bc_mtx;
|
|
|
|
pthread_cond_t bc_cond;
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
pthread_cond_t bc_paused_cond;
|
|
|
|
pthread_cond_t bc_work_done_cond;
|
2021-06-12 00:59:25 +00:00
|
|
|
blockif_resize_cb *bc_resize_cb;
|
|
|
|
void *bc_resize_cb_arg;
|
|
|
|
struct mevent *bc_resize_event;
|
2013-10-04 16:52:03 +00:00
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
/* Request elements and free/pending/busy queues */
|
2013-10-04 16:52:03 +00:00
|
|
|
TAILQ_HEAD(, blockif_elem) bc_freeq;
|
2014-11-04 01:06:33 +00:00
|
|
|
TAILQ_HEAD(, blockif_elem) bc_pendq;
|
|
|
|
TAILQ_HEAD(, blockif_elem) bc_busyq;
|
2013-10-04 16:52:03 +00:00
|
|
|
struct blockif_elem bc_reqs[BLOCKIF_MAXREQ];
|
|
|
|
};
|
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
static pthread_once_t blockif_once = PTHREAD_ONCE_INIT;
|
|
|
|
|
|
|
|
struct blockif_sig_elem {
|
|
|
|
pthread_mutex_t bse_mtx;
|
|
|
|
pthread_cond_t bse_cond;
|
|
|
|
int bse_pending;
|
|
|
|
struct blockif_sig_elem *bse_next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct blockif_sig_elem *blockif_bse_head;
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
static int
|
|
|
|
blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
|
|
|
|
enum blockop op)
|
|
|
|
{
|
2015-03-14 21:15:45 +00:00
|
|
|
struct blockif_elem *be, *tbe;
|
|
|
|
off_t off;
|
|
|
|
int i;
|
2013-10-04 16:52:03 +00:00
|
|
|
|
|
|
|
be = TAILQ_FIRST(&bc->bc_freeq);
|
|
|
|
assert(be != NULL);
|
|
|
|
assert(be->be_status == BST_FREE);
|
|
|
|
TAILQ_REMOVE(&bc->bc_freeq, be, be_link);
|
|
|
|
be->be_req = breq;
|
|
|
|
be->be_op = op;
|
2015-03-14 21:15:45 +00:00
|
|
|
switch (op) {
|
|
|
|
case BOP_READ:
|
|
|
|
case BOP_WRITE:
|
|
|
|
case BOP_DELETE:
|
|
|
|
off = breq->br_offset;
|
|
|
|
for (i = 0; i < breq->br_iovcnt; i++)
|
|
|
|
off += breq->br_iov[i].iov_len;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
off = OFF_MAX;
|
|
|
|
}
|
|
|
|
be->be_block = off;
|
|
|
|
TAILQ_FOREACH(tbe, &bc->bc_pendq, be_link) {
|
|
|
|
if (tbe->be_block == breq->br_offset)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tbe == NULL) {
|
|
|
|
TAILQ_FOREACH(tbe, &bc->bc_busyq, be_link) {
|
|
|
|
if (tbe->be_block == breq->br_offset)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tbe == NULL)
|
|
|
|
be->be_status = BST_PEND;
|
|
|
|
else
|
|
|
|
be->be_status = BST_BLOCK;
|
2014-11-04 01:06:33 +00:00
|
|
|
TAILQ_INSERT_TAIL(&bc->bc_pendq, be, be_link);
|
2015-03-14 21:15:45 +00:00
|
|
|
return (be->be_status == BST_PEND);
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-14 21:15:45 +00:00
|
|
|
blockif_dequeue(struct blockif_ctxt *bc, pthread_t t, struct blockif_elem **bep)
|
2013-10-04 16:52:03 +00:00
|
|
|
{
|
|
|
|
struct blockif_elem *be;
|
|
|
|
|
2015-03-14 21:15:45 +00:00
|
|
|
TAILQ_FOREACH(be, &bc->bc_pendq, be_link) {
|
|
|
|
if (be->be_status == BST_PEND)
|
|
|
|
break;
|
|
|
|
assert(be->be_status == BST_BLOCK);
|
|
|
|
}
|
|
|
|
if (be == NULL)
|
|
|
|
return (0);
|
2014-11-04 01:06:33 +00:00
|
|
|
TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
|
|
|
|
be->be_status = BST_BUSY;
|
2015-03-14 21:15:45 +00:00
|
|
|
be->be_tid = t;
|
2014-11-04 01:06:33 +00:00
|
|
|
TAILQ_INSERT_TAIL(&bc->bc_busyq, be, be_link);
|
|
|
|
*bep = be;
|
2015-03-14 21:15:45 +00:00
|
|
|
return (1);
|
2014-11-04 01:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blockif_complete(struct blockif_ctxt *bc, struct blockif_elem *be)
|
|
|
|
{
|
2015-03-14 21:15:45 +00:00
|
|
|
struct blockif_elem *tbe;
|
2013-10-04 16:52:03 +00:00
|
|
|
|
2015-03-14 21:15:45 +00:00
|
|
|
if (be->be_status == BST_DONE || be->be_status == BST_BUSY)
|
|
|
|
TAILQ_REMOVE(&bc->bc_busyq, be, be_link);
|
|
|
|
else
|
|
|
|
TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
|
|
|
|
TAILQ_FOREACH(tbe, &bc->bc_pendq, be_link) {
|
|
|
|
if (tbe->be_req->br_offset == be->be_block)
|
|
|
|
tbe->be_status = BST_PEND;
|
|
|
|
}
|
2014-11-04 01:06:33 +00:00
|
|
|
be->be_tid = 0;
|
2013-10-04 16:52:03 +00:00
|
|
|
be->be_status = BST_FREE;
|
|
|
|
be->be_req = NULL;
|
|
|
|
TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
|
|
|
|
}
|
|
|
|
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
static int
|
|
|
|
blockif_flush_bc(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
if (bc->bc_ischr) {
|
|
|
|
if (ioctl(bc->bc_fd, DIOCGFLUSH))
|
|
|
|
return (errno);
|
|
|
|
} else if (fsync(bc->bc_fd))
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
static void
|
2015-04-18 20:10:19 +00:00
|
|
|
blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf)
|
2013-10-04 16:52:03 +00:00
|
|
|
{
|
|
|
|
struct blockif_req *br;
|
2015-03-13 16:43:52 +00:00
|
|
|
off_t arg[2];
|
2015-04-18 20:10:19 +00:00
|
|
|
ssize_t clen, len, off, boff, voff;
|
|
|
|
int i, err;
|
2021-08-07 09:10:30 +00:00
|
|
|
struct spacectl_range range;
|
2013-10-04 16:52:03 +00:00
|
|
|
|
|
|
|
br = be->be_req;
|
2015-04-18 20:10:19 +00:00
|
|
|
if (br->br_iovcnt <= 1)
|
|
|
|
buf = NULL;
|
2013-10-04 16:52:03 +00:00
|
|
|
err = 0;
|
|
|
|
switch (be->be_op) {
|
|
|
|
case BOP_READ:
|
2015-04-18 20:10:19 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
if ((len = preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
|
|
|
|
br->br_offset)) < 0)
|
|
|
|
err = errno;
|
|
|
|
else
|
|
|
|
br->br_resid -= len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
off = voff = 0;
|
|
|
|
while (br->br_resid > 0) {
|
|
|
|
len = MIN(br->br_resid, MAXPHYS);
|
|
|
|
if (pread(bc->bc_fd, buf, len, br->br_offset +
|
|
|
|
off) < 0) {
|
|
|
|
err = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
boff = 0;
|
|
|
|
do {
|
|
|
|
clen = MIN(len - boff, br->br_iov[i].iov_len -
|
|
|
|
voff);
|
|
|
|
memcpy(br->br_iov[i].iov_base + voff,
|
|
|
|
buf + boff, clen);
|
|
|
|
if (clen < br->br_iov[i].iov_len - voff)
|
|
|
|
voff += clen;
|
|
|
|
else {
|
|
|
|
i++;
|
|
|
|
voff = 0;
|
|
|
|
}
|
|
|
|
boff += clen;
|
|
|
|
} while (boff < len);
|
|
|
|
off += len;
|
|
|
|
br->br_resid -= len;
|
|
|
|
}
|
2013-10-04 16:52:03 +00:00
|
|
|
break;
|
|
|
|
case BOP_WRITE:
|
2015-04-18 20:10:19 +00:00
|
|
|
if (bc->bc_rdonly) {
|
2013-10-04 16:52:03 +00:00
|
|
|
err = EROFS;
|
2015-04-18 20:10:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (buf == NULL) {
|
|
|
|
if ((len = pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
|
|
|
|
br->br_offset)) < 0)
|
|
|
|
err = errno;
|
|
|
|
else
|
|
|
|
br->br_resid -= len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
off = voff = 0;
|
|
|
|
while (br->br_resid > 0) {
|
|
|
|
len = MIN(br->br_resid, MAXPHYS);
|
|
|
|
boff = 0;
|
|
|
|
do {
|
|
|
|
clen = MIN(len - boff, br->br_iov[i].iov_len -
|
|
|
|
voff);
|
|
|
|
memcpy(buf + boff,
|
|
|
|
br->br_iov[i].iov_base + voff, clen);
|
|
|
|
if (clen < br->br_iov[i].iov_len - voff)
|
|
|
|
voff += clen;
|
|
|
|
else {
|
|
|
|
i++;
|
|
|
|
voff = 0;
|
|
|
|
}
|
|
|
|
boff += clen;
|
|
|
|
} while (boff < len);
|
|
|
|
if (pwrite(bc->bc_fd, buf, len, br->br_offset +
|
|
|
|
off) < 0) {
|
|
|
|
err = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off += len;
|
|
|
|
br->br_resid -= len;
|
|
|
|
}
|
2013-10-04 16:52:03 +00:00
|
|
|
break;
|
|
|
|
case BOP_FLUSH:
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
err = blockif_flush_bc(bc);
|
2013-10-04 16:52:03 +00:00
|
|
|
break;
|
2015-03-13 16:43:52 +00:00
|
|
|
case BOP_DELETE:
|
|
|
|
if (!bc->bc_candelete)
|
|
|
|
err = EOPNOTSUPP;
|
2015-03-15 08:09:56 +00:00
|
|
|
else if (bc->bc_rdonly)
|
|
|
|
err = EROFS;
|
2015-03-13 16:43:52 +00:00
|
|
|
else if (bc->bc_ischr) {
|
|
|
|
arg[0] = br->br_offset;
|
2015-04-18 20:10:19 +00:00
|
|
|
arg[1] = br->br_resid;
|
2015-03-13 16:43:52 +00:00
|
|
|
if (ioctl(bc->bc_fd, DIOCGDELETE, arg))
|
|
|
|
err = errno;
|
2015-04-18 20:10:19 +00:00
|
|
|
else
|
|
|
|
br->br_resid = 0;
|
2021-08-07 09:10:30 +00:00
|
|
|
} else {
|
|
|
|
range.r_offset = br->br_offset;
|
|
|
|
range.r_len = br->br_resid;
|
|
|
|
|
|
|
|
while (range.r_len > 0) {
|
|
|
|
if (fspacectl(bc->bc_fd, SPACECTL_DEALLOC,
|
|
|
|
&range, 0, &range) != 0) {
|
|
|
|
err = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err == 0)
|
|
|
|
br->br_resid = 0;
|
|
|
|
}
|
2015-03-13 16:43:52 +00:00
|
|
|
break;
|
2013-10-04 16:52:03 +00:00
|
|
|
default:
|
|
|
|
err = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
be->be_status = BST_DONE;
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
(*br->br_callback)(br, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
blockif_thr(void *arg)
|
|
|
|
{
|
|
|
|
struct blockif_ctxt *bc;
|
2014-11-04 01:06:33 +00:00
|
|
|
struct blockif_elem *be;
|
2015-03-14 21:15:45 +00:00
|
|
|
pthread_t t;
|
2015-04-18 20:10:19 +00:00
|
|
|
uint8_t *buf;
|
2013-10-04 16:52:03 +00:00
|
|
|
|
|
|
|
bc = arg;
|
2015-04-18 20:10:19 +00:00
|
|
|
if (bc->bc_isgeom)
|
|
|
|
buf = malloc(MAXPHYS);
|
|
|
|
else
|
|
|
|
buf = NULL;
|
2015-03-14 21:15:45 +00:00
|
|
|
t = pthread_self();
|
2013-10-04 16:52:03 +00:00
|
|
|
|
2015-03-14 21:15:45 +00:00
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
2013-10-04 16:52:03 +00:00
|
|
|
for (;;) {
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
bc->bc_work_count++;
|
|
|
|
|
|
|
|
/* We cannot process work if the interface is paused */
|
|
|
|
while (!bc->bc_paused && blockif_dequeue(bc, t, &be)) {
|
2013-10-04 16:52:03 +00:00
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
2015-04-18 20:10:19 +00:00
|
|
|
blockif_proc(bc, be, buf);
|
2013-10-04 16:52:03 +00:00
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
2014-11-04 01:06:33 +00:00
|
|
|
blockif_complete(bc, be);
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
|
|
|
|
bc->bc_work_count--;
|
|
|
|
|
|
|
|
/* If none of the workers are busy, notify the main thread */
|
|
|
|
if (bc->bc_work_count == 0)
|
|
|
|
pthread_cond_broadcast(&bc->bc_work_done_cond);
|
|
|
|
|
2015-03-14 21:15:45 +00:00
|
|
|
/* Check ctxt status here to see if exit requested */
|
2013-10-04 16:52:03 +00:00
|
|
|
if (bc->bc_closing)
|
2015-03-14 21:15:45 +00:00
|
|
|
break;
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
|
|
|
|
/* Make all worker threads wait here if the device is paused */
|
|
|
|
while (bc->bc_paused)
|
|
|
|
pthread_cond_wait(&bc->bc_paused_cond, &bc->bc_mtx);
|
|
|
|
|
2015-03-14 21:15:45 +00:00
|
|
|
pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx);
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
2015-03-14 21:15:45 +00:00
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
2013-10-04 16:52:03 +00:00
|
|
|
|
2015-04-18 20:10:19 +00:00
|
|
|
if (buf)
|
|
|
|
free(buf);
|
2015-03-14 21:15:45 +00:00
|
|
|
pthread_exit(NULL);
|
2013-10-04 16:52:03 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
static void
|
|
|
|
blockif_sigcont_handler(int signal, enum ev_type type, void *arg)
|
|
|
|
{
|
|
|
|
struct blockif_sig_elem *bse;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* Process the entire list even if not intended for
|
|
|
|
* this thread.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
bse = blockif_bse_head;
|
|
|
|
if (bse == NULL)
|
|
|
|
return;
|
|
|
|
} while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
|
|
|
|
(uintptr_t)bse,
|
|
|
|
(uintptr_t)bse->bse_next));
|
|
|
|
|
|
|
|
pthread_mutex_lock(&bse->bse_mtx);
|
|
|
|
bse->bse_pending = 0;
|
|
|
|
pthread_cond_signal(&bse->bse_cond);
|
|
|
|
pthread_mutex_unlock(&bse->bse_mtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blockif_init(void)
|
|
|
|
{
|
|
|
|
mevent_add(SIGCONT, EVF_SIGNAL, blockif_sigcont_handler, NULL);
|
|
|
|
(void) signal(SIGCONT, SIG_IGN);
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
int
|
|
|
|
blockif_legacy_config(nvlist_t *nvl, const char *opts)
|
|
|
|
{
|
|
|
|
char *cp, *path;
|
|
|
|
|
|
|
|
if (opts == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
cp = strchr(opts, ',');
|
|
|
|
if (cp == NULL) {
|
|
|
|
set_config_value_node(nvl, "path", opts);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
path = strndup(opts, cp - opts);
|
|
|
|
set_config_value_node(nvl, "path", path);
|
|
|
|
free(path);
|
|
|
|
return (pci_parse_legacy_config(nvl, cp + 1));
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
struct blockif_ctxt *
|
2019-06-26 20:30:41 +00:00
|
|
|
blockif_open(nvlist_t *nvl, const char *ident)
|
2013-10-04 16:52:03 +00:00
|
|
|
{
|
|
|
|
char tname[MAXCOMLEN + 1];
|
2015-04-18 20:10:19 +00:00
|
|
|
char name[MAXPATHLEN];
|
2019-06-26 20:30:41 +00:00
|
|
|
const char *path, *pssval, *ssval;
|
|
|
|
char *cp;
|
2013-10-04 16:52:03 +00:00
|
|
|
struct blockif_ctxt *bc;
|
|
|
|
struct stat sbuf;
|
2015-03-13 16:43:52 +00:00
|
|
|
struct diocgattr_arg arg;
|
2015-03-05 12:21:12 +00:00
|
|
|
off_t size, psectsz, psectoff;
|
2013-10-04 16:52:03 +00:00
|
|
|
int extra, fd, i, sectsz;
|
2019-06-26 20:30:41 +00:00
|
|
|
int ro, candelete, geom, ssopt, pssopt;
|
2020-04-23 19:20:58 +00:00
|
|
|
int nodelete;
|
|
|
|
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
cap_rights_t rights;
|
|
|
|
cap_ioctl_t cmds[] = { DIOCGFLUSH, DIOCGDELETE };
|
|
|
|
#endif
|
2013-10-04 16:52:03 +00:00
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
pthread_once(&blockif_once, blockif_init);
|
|
|
|
|
2015-05-12 00:30:39 +00:00
|
|
|
fd = -1;
|
2019-06-26 20:30:41 +00:00
|
|
|
extra = 0;
|
2015-05-12 00:30:39 +00:00
|
|
|
ssopt = 0;
|
2013-10-04 16:52:03 +00:00
|
|
|
ro = 0;
|
2020-04-23 19:20:58 +00:00
|
|
|
nodelete = 0;
|
2013-10-04 16:52:03 +00:00
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
if (get_config_bool_node_default(nvl, "nocache", false))
|
|
|
|
extra |= O_DIRECT;
|
|
|
|
if (get_config_bool_node_default(nvl, "nodelete", false))
|
|
|
|
nodelete = 1;
|
|
|
|
if (get_config_bool_node_default(nvl, "sync", false) ||
|
|
|
|
get_config_bool_node_default(nvl, "direct", false))
|
|
|
|
extra |= O_SYNC;
|
|
|
|
if (get_config_bool_node_default(nvl, "ro", false))
|
|
|
|
ro = 1;
|
|
|
|
ssval = get_config_value_node(nvl, "sectorsize");
|
|
|
|
if (ssval != NULL) {
|
|
|
|
ssopt = strtol(ssval, &cp, 10);
|
|
|
|
if (cp == ssval) {
|
|
|
|
EPRINTLN("Invalid sector size \"%s\"", ssval);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (*cp == '\0') {
|
2015-05-12 00:30:39 +00:00
|
|
|
pssopt = ssopt;
|
2019-06-26 20:30:41 +00:00
|
|
|
} else if (*cp == '/') {
|
|
|
|
pssval = cp + 1;
|
|
|
|
pssopt = strtol(pssval, &cp, 10);
|
|
|
|
if (cp == pssval || *cp != '\0') {
|
|
|
|
EPRINTLN("Invalid sector size \"%s\"", ssval);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EPRINTLN("Invalid sector size \"%s\"", ssval);
|
2015-05-12 00:30:39 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
path = get_config_value_node(nvl, "path");
|
|
|
|
if (path == NULL) {
|
|
|
|
EPRINTLN("Missing \"path\" for block device.");
|
|
|
|
goto err;
|
|
|
|
}
|
2013-10-04 16:52:03 +00:00
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
fd = open(path, (ro ? O_RDONLY : O_RDWR) | extra);
|
2013-10-04 16:52:03 +00:00
|
|
|
if (fd < 0 && !ro) {
|
|
|
|
/* Attempt a r/w fail with a r/o open */
|
2019-06-26 20:30:41 +00:00
|
|
|
fd = open(path, O_RDONLY | extra);
|
2013-10-04 16:52:03 +00:00
|
|
|
ro = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd < 0) {
|
2019-06-26 20:30:41 +00:00
|
|
|
warn("Could not open backing file: %s", path);
|
2015-05-12 00:30:39 +00:00
|
|
|
goto err;
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &sbuf) < 0) {
|
2019-06-26 20:30:41 +00:00
|
|
|
warn("Could not stat backing file %s", path);
|
2015-05-12 00:30:39 +00:00
|
|
|
goto err;
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
cap_rights_init(&rights, CAP_FSYNC, CAP_IOCTL, CAP_READ, CAP_SEEK,
|
2021-06-12 00:59:25 +00:00
|
|
|
CAP_WRITE, CAP_FSTAT, CAP_EVENT);
|
2017-02-14 13:35:59 +00:00
|
|
|
if (ro)
|
|
|
|
cap_rights_clear(&rights, CAP_FSYNC, CAP_WRITE);
|
|
|
|
|
2019-01-16 00:39:23 +00:00
|
|
|
if (caph_rights_limit(fd, &rights) == -1)
|
2017-02-14 13:35:59 +00:00
|
|
|
errx(EX_OSERR, "Unable to apply rights for sandbox");
|
|
|
|
#endif
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
/*
|
|
|
|
* Deal with raw devices
|
|
|
|
*/
|
|
|
|
size = sbuf.st_size;
|
|
|
|
sectsz = DEV_BSIZE;
|
2015-03-05 12:21:12 +00:00
|
|
|
psectsz = psectoff = 0;
|
2015-04-18 20:10:19 +00:00
|
|
|
candelete = geom = 0;
|
2013-10-04 16:52:03 +00:00
|
|
|
if (S_ISCHR(sbuf.st_mode)) {
|
|
|
|
if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
|
|
|
|
ioctl(fd, DIOCGSECTORSIZE, §sz)) {
|
|
|
|
perror("Could not fetch dev blk/sector size");
|
2015-05-12 00:30:39 +00:00
|
|
|
goto err;
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
assert(size != 0);
|
|
|
|
assert(sectsz != 0);
|
2015-03-05 12:21:12 +00:00
|
|
|
if (ioctl(fd, DIOCGSTRIPESIZE, &psectsz) == 0 && psectsz > 0)
|
|
|
|
ioctl(fd, DIOCGSTRIPEOFFSET, &psectoff);
|
2015-03-13 16:43:52 +00:00
|
|
|
strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
|
|
|
|
arg.len = sizeof(arg.value.i);
|
2020-04-23 19:20:58 +00:00
|
|
|
if (nodelete == 0 && ioctl(fd, DIOCGATTR, &arg) == 0)
|
2015-03-13 16:43:52 +00:00
|
|
|
candelete = arg.value.i;
|
2015-04-18 20:10:19 +00:00
|
|
|
if (ioctl(fd, DIOCGPROVIDERNAME, name) == 0)
|
|
|
|
geom = 1;
|
2021-08-07 09:10:30 +00:00
|
|
|
} else {
|
2015-03-05 12:21:12 +00:00
|
|
|
psectsz = sbuf.st_blksize;
|
2021-08-07 09:10:30 +00:00
|
|
|
/* Avoid fallback implementation */
|
|
|
|
candelete = fpathconf(fd, _PC_DEALLOC_PRESENT) == 1;
|
|
|
|
}
|
2013-10-04 16:52:03 +00:00
|
|
|
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
2019-01-16 00:39:23 +00:00
|
|
|
if (caph_ioctls_limit(fd, cmds, nitems(cmds)) == -1)
|
2017-02-14 13:35:59 +00:00
|
|
|
errx(EX_OSERR, "Unable to apply rights for sandbox");
|
|
|
|
#endif
|
|
|
|
|
2015-05-12 00:30:39 +00:00
|
|
|
if (ssopt != 0) {
|
|
|
|
if (!powerof2(ssopt) || !powerof2(pssopt) || ssopt < 512 ||
|
|
|
|
ssopt > pssopt) {
|
2020-01-08 22:55:22 +00:00
|
|
|
EPRINTLN("Invalid sector size %d/%d",
|
2015-05-12 00:30:39 +00:00
|
|
|
ssopt, pssopt);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some backend drivers (e.g. cd0, ada0) require that the I/O
|
|
|
|
* size be a multiple of the device's sector size.
|
|
|
|
*
|
|
|
|
* Validate that the emulated sector size complies with this
|
|
|
|
* requirement.
|
|
|
|
*/
|
|
|
|
if (S_ISCHR(sbuf.st_mode)) {
|
|
|
|
if (ssopt < sectsz || (ssopt % sectsz) != 0) {
|
2020-01-08 22:55:22 +00:00
|
|
|
EPRINTLN("Sector size %d incompatible "
|
|
|
|
"with underlying device sector size %d",
|
2015-05-12 00:30:39 +00:00
|
|
|
ssopt, sectsz);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sectsz = ssopt;
|
|
|
|
psectsz = pssopt;
|
|
|
|
psectoff = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-22 18:55:21 +00:00
|
|
|
bc = calloc(1, sizeof(struct blockif_ctxt));
|
2013-10-04 16:52:03 +00:00
|
|
|
if (bc == NULL) {
|
2015-05-12 00:30:39 +00:00
|
|
|
perror("calloc");
|
|
|
|
goto err;
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bc->bc_magic = BLOCKIF_SIG;
|
|
|
|
bc->bc_fd = fd;
|
2015-03-05 15:29:18 +00:00
|
|
|
bc->bc_ischr = S_ISCHR(sbuf.st_mode);
|
2015-04-18 20:10:19 +00:00
|
|
|
bc->bc_isgeom = geom;
|
2015-03-13 16:43:52 +00:00
|
|
|
bc->bc_candelete = candelete;
|
2014-09-11 21:15:20 +00:00
|
|
|
bc->bc_rdonly = ro;
|
2013-10-04 16:52:03 +00:00
|
|
|
bc->bc_size = size;
|
|
|
|
bc->bc_sectsz = sectsz;
|
2015-03-05 12:21:12 +00:00
|
|
|
bc->bc_psectsz = psectsz;
|
|
|
|
bc->bc_psectoff = psectoff;
|
2013-10-04 16:52:03 +00:00
|
|
|
pthread_mutex_init(&bc->bc_mtx, NULL);
|
|
|
|
pthread_cond_init(&bc->bc_cond, NULL);
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
bc->bc_paused = 0;
|
|
|
|
bc->bc_work_count = 0;
|
|
|
|
pthread_cond_init(&bc->bc_paused_cond, NULL);
|
|
|
|
pthread_cond_init(&bc->bc_work_done_cond, NULL);
|
2013-10-04 16:52:03 +00:00
|
|
|
TAILQ_INIT(&bc->bc_freeq);
|
2014-11-04 01:06:33 +00:00
|
|
|
TAILQ_INIT(&bc->bc_pendq);
|
|
|
|
TAILQ_INIT(&bc->bc_busyq);
|
2013-10-04 16:52:03 +00:00
|
|
|
for (i = 0; i < BLOCKIF_MAXREQ; i++) {
|
|
|
|
bc->bc_reqs[i].be_status = BST_FREE;
|
|
|
|
TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link);
|
|
|
|
}
|
|
|
|
|
2015-03-14 21:15:45 +00:00
|
|
|
for (i = 0; i < BLOCKIF_NUMTHR; i++) {
|
|
|
|
pthread_create(&bc->bc_btid[i], NULL, blockif_thr, bc);
|
|
|
|
snprintf(tname, sizeof(tname), "blk-%s-%d", ident, i);
|
|
|
|
pthread_set_name_np(bc->bc_btid[i], tname);
|
|
|
|
}
|
2013-10-04 16:52:03 +00:00
|
|
|
|
|
|
|
return (bc);
|
2015-05-12 00:30:39 +00:00
|
|
|
err:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
return (NULL);
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 00:59:25 +00:00
|
|
|
static void
|
|
|
|
blockif_resized(int fd, enum ev_type type, void *arg)
|
|
|
|
{
|
|
|
|
struct blockif_ctxt *bc;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (fstat(fd, &sb) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bc = arg;
|
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
|
|
|
if (sb.st_size != bc->bc_size) {
|
|
|
|
bc->bc_size = sb.st_size;
|
|
|
|
bc->bc_resize_cb(bc, bc->bc_resize_cb_arg, bc->bc_size);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_register_resize_callback(struct blockif_ctxt *bc, blockif_resize_cb *cb,
|
|
|
|
void *cb_arg)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (cb == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
|
|
|
if (bc->bc_resize_cb != NULL) {
|
|
|
|
err = EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(bc->bc_closing == 0);
|
|
|
|
|
|
|
|
if (fstat(bc->bc_fd, &sb) != 0) {
|
|
|
|
err = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bc->bc_resize_event = mevent_add_flags(bc->bc_fd, EVF_VNODE,
|
|
|
|
EVFF_ATTRIB, blockif_resized, bc);
|
|
|
|
if (bc->bc_resize_event == NULL) {
|
|
|
|
err = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bc->bc_resize_cb = cb;
|
|
|
|
bc->bc_resize_cb_arg = cb_arg;
|
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
static int
|
|
|
|
blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
|
|
|
|
enum blockop op)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
2015-03-14 21:15:45 +00:00
|
|
|
if (!TAILQ_EMPTY(&bc->bc_freeq)) {
|
2013-10-04 16:52:03 +00:00
|
|
|
/*
|
|
|
|
* Enqueue and inform the block i/o thread
|
|
|
|
* that there is work available
|
|
|
|
*/
|
2015-03-14 21:15:45 +00:00
|
|
|
if (blockif_enqueue(bc, breq, op))
|
|
|
|
pthread_cond_signal(&bc->bc_cond);
|
2013-10-04 16:52:03 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Callers are not allowed to enqueue more than
|
|
|
|
* the specified blockif queue limit. Return an
|
|
|
|
* error to indicate that the queue length has been
|
|
|
|
* exceeded.
|
|
|
|
*/
|
|
|
|
err = E2BIG;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
return (blockif_request(bc, breq, BOP_READ));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
return (blockif_request(bc, breq, BOP_WRITE));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
return (blockif_request(bc, breq, BOP_FLUSH));
|
|
|
|
}
|
|
|
|
|
2015-03-13 16:43:52 +00:00
|
|
|
int
|
|
|
|
blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
return (blockif_request(bc, breq, BOP_DELETE));
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
int
|
|
|
|
blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
|
|
|
|
{
|
2014-10-17 11:37:50 +00:00
|
|
|
struct blockif_elem *be;
|
2013-10-04 16:52:03 +00:00
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
2014-10-17 11:37:50 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
/* XXX: not waiting while paused */
|
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
/*
|
|
|
|
* Check pending requests.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(be, &bc->bc_pendq, be_link) {
|
|
|
|
if (be->be_req == breq)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (be != NULL) {
|
|
|
|
/*
|
|
|
|
* Found it.
|
|
|
|
*/
|
2015-03-14 21:15:45 +00:00
|
|
|
blockif_complete(bc, be);
|
2014-11-04 01:06:33 +00:00
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check in-flight requests.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(be, &bc->bc_busyq, be_link) {
|
2014-10-17 11:37:50 +00:00
|
|
|
if (be->be_req == breq)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (be == NULL) {
|
2014-11-04 01:06:33 +00:00
|
|
|
/*
|
|
|
|
* Didn't find it.
|
|
|
|
*/
|
2014-10-17 11:37:50 +00:00
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
/*
|
|
|
|
* Interrupt the processing thread to force it return
|
|
|
|
* prematurely via it's normal callback path.
|
|
|
|
*/
|
|
|
|
while (be->be_status == BST_BUSY) {
|
|
|
|
struct blockif_sig_elem bse, *old_head;
|
|
|
|
|
|
|
|
pthread_mutex_init(&bse.bse_mtx, NULL);
|
|
|
|
pthread_cond_init(&bse.bse_cond, NULL);
|
|
|
|
|
|
|
|
bse.bse_pending = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
old_head = blockif_bse_head;
|
|
|
|
bse.bse_next = old_head;
|
|
|
|
} while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
|
|
|
|
(uintptr_t)old_head,
|
|
|
|
(uintptr_t)&bse));
|
|
|
|
|
|
|
|
pthread_kill(be->be_tid, SIGCONT);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&bse.bse_mtx);
|
|
|
|
while (bse.bse_pending)
|
|
|
|
pthread_cond_wait(&bse.bse_cond, &bse.bse_mtx);
|
|
|
|
pthread_mutex_unlock(&bse.bse_mtx);
|
|
|
|
}
|
|
|
|
|
2014-10-17 11:37:50 +00:00
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
|
2014-11-04 01:06:33 +00:00
|
|
|
/*
|
|
|
|
* The processing thread has been interrupted. Since it's not
|
|
|
|
* clear if the callback has been invoked yet, return EBUSY.
|
|
|
|
*/
|
|
|
|
return (EBUSY);
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_close(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
void *jval;
|
2016-01-26 07:17:21 +00:00
|
|
|
int i;
|
2013-10-04 16:52:03 +00:00
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop the block i/o thread
|
|
|
|
*/
|
2015-03-15 16:18:03 +00:00
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
2013-10-04 16:52:03 +00:00
|
|
|
bc->bc_closing = 1;
|
2021-06-12 00:59:25 +00:00
|
|
|
if (bc->bc_resize_event != NULL)
|
|
|
|
mevent_disable(bc->bc_resize_event);
|
2015-03-15 16:18:03 +00:00
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
2015-03-14 21:15:45 +00:00
|
|
|
pthread_cond_broadcast(&bc->bc_cond);
|
|
|
|
for (i = 0; i < BLOCKIF_NUMTHR; i++)
|
|
|
|
pthread_join(bc->bc_btid[i], &jval);
|
2013-10-04 16:52:03 +00:00
|
|
|
|
|
|
|
/* XXX Cancel queued i/o's ??? */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release resources
|
|
|
|
*/
|
|
|
|
bc->bc_magic = 0;
|
|
|
|
close(bc->bc_fd);
|
|
|
|
free(bc);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-07-15 00:25:54 +00:00
|
|
|
/*
|
|
|
|
* Return virtual C/H/S values for a given block. Use the algorithm
|
|
|
|
* outlined in the VHD specification to calculate values.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
|
|
|
|
{
|
|
|
|
off_t sectors; /* total sectors of the block dev */
|
|
|
|
off_t hcyl; /* cylinders times heads */
|
|
|
|
uint16_t secpt; /* sectors per track */
|
|
|
|
uint8_t heads;
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
|
|
|
|
sectors = bc->bc_size / bc->bc_sectsz;
|
|
|
|
|
|
|
|
/* Clamp the size to the largest possible with CHS */
|
|
|
|
if (sectors > 65535UL*16*255)
|
|
|
|
sectors = 65535UL*16*255;
|
|
|
|
|
|
|
|
if (sectors >= 65536UL*16*63) {
|
|
|
|
secpt = 255;
|
|
|
|
heads = 16;
|
|
|
|
hcyl = sectors / secpt;
|
|
|
|
} else {
|
|
|
|
secpt = 17;
|
|
|
|
hcyl = sectors / secpt;
|
|
|
|
heads = (hcyl + 1023) / 1024;
|
|
|
|
|
|
|
|
if (heads < 4)
|
|
|
|
heads = 4;
|
|
|
|
|
|
|
|
if (hcyl >= (heads * 1024) || heads > 16) {
|
|
|
|
secpt = 31;
|
|
|
|
heads = 16;
|
|
|
|
hcyl = sectors / secpt;
|
|
|
|
}
|
|
|
|
if (hcyl >= (heads * 1024)) {
|
|
|
|
secpt = 63;
|
|
|
|
heads = 16;
|
|
|
|
hcyl = sectors / secpt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*c = hcyl / heads;
|
|
|
|
*h = heads;
|
|
|
|
*s = secpt;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
/*
|
|
|
|
* Accessors
|
|
|
|
*/
|
|
|
|
off_t
|
|
|
|
blockif_size(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
return (bc->bc_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_sectsz(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
return (bc->bc_sectsz);
|
|
|
|
}
|
|
|
|
|
2015-03-05 12:21:12 +00:00
|
|
|
void
|
|
|
|
blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
*size = bc->bc_psectsz;
|
|
|
|
*off = bc->bc_psectoff;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:52:03 +00:00
|
|
|
int
|
|
|
|
blockif_queuesz(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
2014-11-09 21:08:52 +00:00
|
|
|
return (BLOCKIF_MAXREQ - 1);
|
2013-10-04 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_is_ro(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
return (bc->bc_rdonly);
|
|
|
|
}
|
2015-03-13 16:43:52 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
blockif_candelete(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
return (bc->bc_candelete);
|
|
|
|
}
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
|
|
|
|
#ifdef BHYVE_SNAPSHOT
|
|
|
|
void
|
|
|
|
blockif_pause(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
assert(bc != NULL);
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
|
|
|
bc->bc_paused = 1;
|
|
|
|
|
|
|
|
/* The interface is paused. Wait for workers to finish their work */
|
|
|
|
while (bc->bc_work_count)
|
|
|
|
pthread_cond_wait(&bc->bc_work_done_cond, &bc->bc_mtx);
|
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
|
|
|
|
if (blockif_flush_bc(bc))
|
|
|
|
fprintf(stderr, "%s: [WARN] failed to flush backing file.\r\n",
|
|
|
|
__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
blockif_resume(struct blockif_ctxt *bc)
|
|
|
|
{
|
|
|
|
assert(bc != NULL);
|
|
|
|
assert(bc->bc_magic == BLOCKIF_SIG);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
|
|
|
bc->bc_paused = 0;
|
|
|
|
/* resume the threads waiting for paused */
|
|
|
|
pthread_cond_broadcast(&bc->bc_paused_cond);
|
|
|
|
/* kick the threads after restore */
|
|
|
|
pthread_cond_broadcast(&bc->bc_cond);
|
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_snapshot_req(struct blockif_req *br, struct vm_snapshot_meta *meta)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct iovec *iov;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(br->br_iovcnt, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(br->br_offset, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(br->br_resid, meta, ret, done);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: The callback and parameter must be filled by the virtualized
|
|
|
|
* device that uses the interface, during its init; we're not touching
|
|
|
|
* them here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Snapshot the iovecs. */
|
|
|
|
for (i = 0; i < br->br_iovcnt; i++) {
|
|
|
|
iov = &br->br_iov[i];
|
|
|
|
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(iov->iov_len, meta, ret, done);
|
|
|
|
|
|
|
|
/* We assume the iov is a guest-mapped address. */
|
|
|
|
SNAPSHOT_GUEST2HOST_ADDR_OR_LEAVE(iov->iov_base, iov->iov_len,
|
|
|
|
false, meta, ret, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
blockif_snapshot(struct blockif_ctxt *bc, struct vm_snapshot_meta *meta)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (bc->bc_paused == 0) {
|
|
|
|
fprintf(stderr, "%s: Snapshot failed: "
|
|
|
|
"interface not paused.\r\n", __func__);
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&bc->bc_mtx);
|
|
|
|
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_magic, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_ischr, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_isgeom, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_candelete, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_rdonly, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_size, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_sectsz, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_psectsz, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_psectoff, meta, ret, done);
|
|
|
|
SNAPSHOT_VAR_OR_LEAVE(bc->bc_closing, meta, ret, done);
|
|
|
|
|
|
|
|
done:
|
|
|
|
pthread_mutex_unlock(&bc->bc_mtx);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
#endif
|