Skip to contentSkip to conversation

PATCH RFC: possible implementation of a low-bandwidth private 'channel'

RFC PATCH Superseded David Scott 2 participants 10 messages Jun 11, 2014 – Jun 12, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

Several libvirt applications (e.g. oVirt, CloudStack) make use of 'channels':
low-bandwidth private host <-> guest communication links which resemble serial ports. Typical uses include:

* initial VM configuration without using the network: read the config data directly from the channel on boot

* controlling a guest agent: signal shutdown reqests, exchange clipboard data, trigger resolution changes

This patch set re-uses the existing PV console protocol implemented by qemu to provide this service.

If you declare a channel in your .xl file as follows:

channel = [ "type=socket,path=/tmp/mysock,name=guest-agent" ]

then an extra PV console will be added to your guest. This console has the extra key in the frontend

name = guest-agent

which allows udev scripts in the VM to create a named device in a well-known location (e.g. /dev/xen-channels/guest-agent, similar to the KVM /dev/vports). The qemu process in the backend domain will proxy the data to/from the named Unix domain socket (in this case /tmp/mysock).

Note this mechanism is intended for low-bandwidth communication only. If an application requires a high-bandwith connection then it should use a direct vchan connection (and not proxy it via a qemu).

#1 lore raw

RFC PATCH 1/4 libxl: add a list of abstract 'channels' to the domain config

A 'channel' is a low-bandwidth private communication channel that resembles a physical serial port. Example uses include:

* providing initial VM configuration without having to use the network
* signalling a guest agent

Every channel has a string 'name' which the VM can use to find the appropriate handler. Each channel has an implementation 'type' which currently includes:

* NONE: reads will block, writes will be thrown away
* PTY: the I/O surfaces as a pty in the backend domain
* PATH: writes are appended to a log file in the backend domain
* SOCKET: a listening Unix domain socket accepts a connection in the backend domain and proxies

---
 tools/libxl/libxl_types.idl |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tools/libxl/libxl_types.idl +21 -0
index 52f1aa9..4df8d42 100644
@@ -51,6 +51,17 @@ libxl_domain_type = Enumeration("domain_type", [
     (2, "PV"),
     ], init_val = -1)
 
+libxl_channel_type = Enumeration("channel_type", [
+    # Connected to nothing:
+    (0, "NONE"),
+    # Connect to a pty in the backend domain:
+    (1, "PTY"),
+    # Spool output to a file in the backend domain:
+    (2, "PATH"),
+    # Listen on a Unix domain socket in the backend domain:
+    (3, "SOCKET"),
+    ], init_val = 0)
+
 libxl_device_model_version = Enumeration("device_model_version", [
     (0, "UNKNOWN"),
     (1, "QEMU_XEN_TRADITIONAL"), # Historical qemu-xen device model (qemu-dm)
@@ -457,6 +468,15 @@ libxl_device_vtpm = Struct("device_vtpm", [
     ("uuid",             libxl_uuid),
 ])
 
+libxl_device_channel = Struct("device_channel", [
+    ("backend_domid", libxl_domid),
+    ("backend_domname", string),
+    ("devid", libxl_devid),
+    ("name", string),
+    ("type", libxl_channel_type),
+    ("path", string),
+])
+
 libxl_domain_config = Struct("domain_config", [
     ("c_info", libxl_domain_create_info),
     ("b_info", libxl_domain_build_info),
@@ -467,6 +487,7 @@ libxl_domain_config = Struct("domain_config", [
     ("vfbs", Array(libxl_device_vfb, "num_vfbs")),
     ("vkbs", Array(libxl_device_vkb, "num_vkbs")),
     ("vtpms", Array(libxl_device_vtpm, "num_vtpms")),
+    ("channels", Array(libxl_device_channel, "num_channels")),
 
     ("on_poweroff", libxl_action_on_shutdown),
     ("on_reboot", libxl_action_on_shutdown),
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH RFC 1/4] libxl: add a list of abstract 'channels' to the domain config

Quoted 31 lines · David Scott

A 'channel' is a low-bandwidth private communication channel that resembles a physical serial port. Example uses include:

* providing initial VM configuration without having to use the network
* signalling a guest agent

Every channel has a string 'name' which the VM can use to find the appropriate handler. Each channel has an implementation 'type' which currently includes:

* NONE: reads will block, writes will be thrown away
* PTY: the I/O surfaces as a pty in the backend domain
* PATH: writes are appended to a log file in the backend domain
* SOCKET: a listening Unix domain socket accepts a connection in the backend domain and proxies

---
 tools/libxl/libxl_types.idl |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tools/libxl/libxl_types.idl +1 -0
index 52f1aa9..4df8d42 100644
@@ -51,6 +51,17 @@ libxl_domain_type = Enumeration("domain_type", [
     (2, "PV"),
     ], init_val = -1)
 
+libxl_channel_type = Enumeration("channel_type", [

tools/libxl/libxl_types.idl libxl_domain_type = Enumeration("domain_type", [ · line 64

Quoted 10 lines
tools/libxl/libxl_types.idl +10 -0
+    # Connected to nothing:
+    (0, "NONE"),
+    # Connect to a pty in the backend domain:
+    (1, "PTY"),
+    # Spool output to a file in the backend domain:
+    (2, "PATH"),
+    # Listen on a Unix domain socket in the backend domain:
+    (3, "SOCKET"),
+    ], init_val = 0)
+

You can omit init_val if it is 0.

Wei.

#3 lore raw

RFC PATCH 2/4 xl: add support for channels

In reply to David Scott

This adds support for channel declarations of the form:

channel = [ "name=...,type=...[,path=...][,backend=...]" ]

where 'name' is a label to identify the channel to the frontend.

If 'type = none' then the channel is connected to /dev/null If 'type = pty' then the channel is connected to a pty in the
backend domain
If 'type = path' then data is read from the channel and written
to the file given by 'path = ...' in the backend domain.
If 'type = socket' then the channel is connected to a Unix domain
socket given by 'path = ...' in the backend domain.

---
 tools/libxl/xl_cmdimpl.c |   62 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 61 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +61 -1
index 5195914..2657b5d 100644
@@ -736,7 +736,7 @@ static void parse_config_data(const char *config_source,
     long l;
     XLU_Config *config;
     XLU_ConfigList *cpus, *vbds, *nics, *pcis, *cvfbs, *cpuids, *vtpms;
-    XLU_ConfigList *ioports, *irqs, *iomem;
+    XLU_ConfigList *channels, *ioports, *irqs, *iomem;
     int num_ioports, num_irqs, num_iomem;
     int pci_power_mgmt = 0;
     int pci_msitranslate = 0;
@@ -1289,6 +1289,66 @@ static void parse_config_data(const char *config_source,
         }
     }
 
+    if (!xlu_cfg_get_list (config, "channel", &channels, 0, 0)) {
+        d_config->num_channels = 0;
+        d_config->channels = NULL;
+        while ((buf = xlu_cfg_get_listitem (channels, d_config->num_channels)) != NULL) {
+            libxl_device_channel *channel;
+            char *buf2 = strdup(buf);
+            char *p, *p2;
+            d_config->channels = (libxl_device_channel *) realloc(d_config->channels, sizeof (libxl_device_channel) * (d_config->num_channels + 1));
+            channel = d_config->channels + d_config->num_channels;
+            libxl_device_channel_init(channel);
+            channel->devid = d_config->num_channels;
+
+            p = strtok(buf2, ",");
+            if (!p)
+                goto skip_channel;
+            do {
+                while (*p == ' ')
+                    p++;
+                if ((p2 = strchr(p, '=')) == NULL)
+                    break;
+                *p2 = '\0';
+                if (!strcmp(p, "backend")) {
+                    free(channel->backend_domname);
+                    channel->backend_domname = strdup(p2 + 1);
+                } else if (!strcmp(p, "name")) {
+                    free(channel->name);
+                    channel->name = strdup(p2 + 1);
+                } else if (!strcmp(p, "type")) {
+                    if (channel->type != LIBXL_CHANNEL_TYPE_NONE) {
+                        fprintf(stderr, "a channel may have only one output,"
+                                        " skipping device\n");
+                        goto skip_channel;
+                    }
+                    if (!strcmp(p2 + 1, "none")) {
+                        channel->type = LIBXL_CHANNEL_TYPE_NONE;
+                    } else if (!strcmp(p2 + 1, "pty")) {
+                        channel->type = LIBXL_CHANNEL_TYPE_PTY;
+                    } else if (!strcmp(p2 + 1, "path")) {
+                        channel->type = LIBXL_CHANNEL_TYPE_PATH;
+                    } else if (!strcmp(p2 + 1, "socket")) {
+                        channel->type = LIBXL_CHANNEL_TYPE_SOCKET;
+                    } else {
+                        fprintf(stderr, "unknown channel type '%s',"
+                                        " skipping device\n", p2 + 1);
+                        goto skip_channel;
+                    }
+                } else if (!strcmp(p, "path")) {
+                    free(channel->path);
+                    channel->path = strdup(p2 + 1);
+                } else {
+                    fprintf(stderr, "unknown channel parameter '%s',"
+                                    " ignoring\n", p);
+                }
+            } while ((p = strtok(NULL, ",")) != NULL);
+skip_channel:
+            free(buf2);
+            d_config->num_channels++;
+        }
+    }
+
     if (!xlu_cfg_get_list (config, "vif", &nics, 0, 0)) {
         d_config->num_nics = 0;
         d_config->nics = NULL;
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [PATCH RFC 2/4] xl: add support for channels

Quoted 14 lines · David Scott

This adds support for channel declarations of the form:

channel = [ "name=...,type=...[,path=...][,backend=...]" ]

where 'name' is a label to identify the channel to the frontend.

If 'type = none' then the channel is connected to /dev/null If 'type = pty' then the channel is connected to a pty in the
backend domain
If 'type = path' then data is read from the channel and written
to the file given by 'path = ...' in the backend domain.
If 'type = socket' then the channel is connected to a Unix domain
socket given by 'path = ...' in the backend domain.

Quoted 5 lines
---
 tools/libxl/xl_cmdimpl.c |   62 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 61 insertions(+), 1 deletion(-)

You also need to document this new config option in manpage.

And possibly the spec of defining channel should be documented too.

Quoted 14 lines
tools/libxl/xl_cmdimpl.c +1 -1
index 5195914..2657b5d 100644
@@ -736,7 +736,7 @@ static void parse_config_data(const char *config_source,
     long l;
     XLU_Config *config;
     XLU_ConfigList *cpus, *vbds, *nics, *pcis, *cvfbs, *cpuids, *vtpms;
-    XLU_ConfigList *ioports, *irqs, *iomem;
+    XLU_ConfigList *channels, *ioports, *irqs, *iomem;
     int num_ioports, num_irqs, num_iomem;
     int pci_power_mgmt = 0;
     int pci_msitranslate = 0;

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char *config_source, · line 1295

Quoted 7 lines
tools/libxl/xl_cmdimpl.c +4 -0
@@ -1289,6 +1289,66 @@ static void parse_config_data(const char *config_source,
         }
     }
 
+    if (!xlu_cfg_get_list (config, "channel", &channels, 0, 0)) {
+        d_config->num_channels = 0;
+        d_config->channels = NULL;
+        while ((buf = xlu_cfg_get_listitem (channels, d_config->num_channels)) != NULL) {

Line too long.

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char *config_source, · line 1299

Quoted 4 lines
tools/libxl/xl_cmdimpl.c +4 -0
+            libxl_device_channel *channel;
+            char *buf2 = strdup(buf);
+            char *p, *p2;
+            d_config->channels = (libxl_device_channel *) realloc(d_config->channels, sizeof (libxl_device_channel) * (d_config->num_channels + 1));

Ditto.

And please use xrealloc instead, which handles allocation failure for you.

Wei.

#5 lore raw

RFC PATCH 3/4 libxl: implement channels via PV console rings attached to qemu

In reply to David Scott

We extend the (internal) console type with a 'name' (string) which isn't used by the default built-in console 0.

For every channel we create a console, starting at index 1, which is handled by the qemu 'chardev' mechanism (ie has 'output=chardev:libxl-channel%d' in xenstore)

---
 tools/libxl/libxl_create.c           |   60 ++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_types_internal.idl |    1 +
 2 files changed, 61 insertions(+)
tools/libxl/libxl_create.c +60 -0
index d015cf4..13a2a27 100644
@@ -361,11 +361,60 @@ static int init_console_info(libxl__device_console *console, int dev_num)
     console->devid = dev_num;
     console->consback = LIBXL__CONSOLE_BACKEND_XENCONSOLED;
     console->output = strdup("pty");
+    /* console->name is NULL on normal consoles. Only 'channels' when mapped
+       to consoles have a string name. */
     if (!console->output)
         return ERROR_NOMEM;
     return 0;
 }
 
+static int init_console_from_channel(libxl__gc *gc,
+                                     libxl__device_console *console,
+                                     int dev_num,
+                                     libxl_device_channel *channel)
+{
+    const char *chardev;
+    memset(console, 0x00, sizeof(libxl__device_console));
+    console->devid = dev_num;
+    console->consback = LIBXL__CONSOLE_BACKEND_IOEMU;
+    if (!channel->name){
+        LIBXL__LOG(CTX, LIBXL__LOG_ERROR,
+                   "channel %d has no name", channel->devid);
+        return ERROR_INVAL;
+    }
+    console->name = strdup(channel->name);
+    console->backend_domid = channel->backend_domid;
+
+    switch (channel->type) {
+        case LIBXL_CHANNEL_TYPE_NONE:
+        case LIBXL_CHANNEL_TYPE_PTY:
+            /* No path is needed */
+            break;
+        case LIBXL_CHANNEL_TYPE_PATH:
+        case LIBXL_CHANNEL_TYPE_SOCKET:
+            if (!channel->path) {
+                LIBXL__LOG(CTX, LIBXL__LOG_ERROR,
+                           "channel %d has no path", channel->devid);
+                return ERROR_INVAL;
+            }
+            break;
+        default:
+            /* We've forgotten to add the clause */
+            LOG(ERROR, "%s: unknown channel type %d", __func__, channel->type);
+            return ERROR_INVAL;
+    }
+
+    /* Use qemu chardev for every channel */
+    chardev = libxl__sprintf(gc, "chardev:libxl-channel%d",
+                                 channel->devid);
+    if (!chardev) return ERROR_NOMEM;
+    console->output = strdup(chardev);
+    if (!console->output) return ERROR_NOMEM;
+
+    return 0;
+}
+
+
 int libxl__domain_build(libxl__gc *gc,
                         libxl_domain_config *d_config,
                         uint32_t domid,
@@ -1110,6 +1159,17 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
         }
     }
 
+    /* For both HVM and PV the 0th console is a regular console. We
+       map channels to IOEMU consoles starting at 1 */
+    for (i = 0; i < d_config->num_channels; i++) {
+        libxl__device_console console;
+        ret = init_console_from_channel(gc, &console, i + 1, &d_config->channels[i]);
+        if ( ret )
+            goto error_out;
+        libxl__device_console_add(gc, domid, &console, NULL);
+        libxl__device_console_dispose(&console);
+    }
+
     switch (d_config->c_info.type) {
     case LIBXL_DOMAIN_TYPE_HVM:
     {
tools/libxl/libxl_types_internal.idl +1 -0
index cb9444f..2a509a9 100644
@@ -32,6 +32,7 @@ libxl__device_console = Struct("device_console", [
     ("devid", integer),
     ("consback", libxl__console_backend),
     ("output", string),
+    ("name", string),
     ])
 
 libxl__device_action = Enumeration("device_action", [
Signature · 1 line
1.7.10.4
#6 lore raw

Re: [PATCH RFC 3/4] libxl: implement channels via PV console rings attached to qemu

Quoted 25 lines · David Scott

We extend the (internal) console type with a 'name' (string) which isn't used by the default built-in console 0.

For every channel we create a console, starting at index 1, which is handled by the qemu 'chardev' mechanism (ie has 'output=chardev:libxl-channel%d' in xenstore)

---
 tools/libxl/libxl_create.c           |   60 ++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_types_internal.idl |    1 +
 2 files changed, 61 insertions(+)
tools/libxl/libxl_create.c +2 -0
index d015cf4..13a2a27 100644
@@ -361,11 +361,60 @@ static int init_console_info(libxl__device_console *console, int dev_num)
     console->devid = dev_num;
     console->consback = LIBXL__CONSOLE_BACKEND_XENCONSOLED;
     console->output = strdup("pty");
+    /* console->name is NULL on normal consoles. Only 'channels' when mapped
+       to consoles have a string name. */
     if (!console->output)
         return ERROR_NOMEM;

tools/libxl/libxl_create.c static int init_console_info(libxl__device_console *console, int dev_num) · line 377

Quoted 10 lines
tools/libxl/libxl_create.c +7 -0
     return 0;
 }
 
+static int init_console_from_channel(libxl__gc *gc,
+                                     libxl__device_console *console,
+                                     int dev_num,
+                                     libxl_device_channel *channel)
+{
+    const char *chardev;
+    memset(console, 0x00, sizeof(libxl__device_console));

I suppose you should use libxl__device_console_init instead?

tools/libxl/libxl_create.c static int init_console_info(libxl__device_console *console, int dev_num) · line 385

Quoted 8 lines
tools/libxl/libxl_create.c +8 -0
+    console->devid = dev_num;
+    console->consback = LIBXL__CONSOLE_BACKEND_IOEMU;
+    if (!channel->name){
+        LIBXL__LOG(CTX, LIBXL__LOG_ERROR,
+                   "channel %d has no name", channel->devid);
+        return ERROR_INVAL;
+    }
+    console->name = strdup(channel->name);

libxl__strdup, use NOGC if you don't want console->name gets freed.

Quoted 16 lines
tools/libxl/libxl_create.c +16 -0
+    console->backend_domid = channel->backend_domid;
+
+    switch (channel->type) {
+        case LIBXL_CHANNEL_TYPE_NONE:
+        case LIBXL_CHANNEL_TYPE_PTY:
+            /* No path is needed */
+            break;
+        case LIBXL_CHANNEL_TYPE_PATH:
+        case LIBXL_CHANNEL_TYPE_SOCKET:
+            if (!channel->path) {
+                LIBXL__LOG(CTX, LIBXL__LOG_ERROR,
+                           "channel %d has no path", channel->devid);
+                return ERROR_INVAL;
+            }
+            break;
+        default:

tools/libxl/libxl_create.c static int init_console_info(libxl__device_console *console, int dev_num) · line 411

Quoted 10 lines
tools/libxl/libxl_create.c +10 -0
+            /* We've forgotten to add the clause */
+            LOG(ERROR, "%s: unknown channel type %d", __func__, channel->type);
+            return ERROR_INVAL;
+    }
+
+    /* Use qemu chardev for every channel */
+    chardev = libxl__sprintf(gc, "chardev:libxl-channel%d",
+                                 channel->devid);
+    if (!chardev) return ERROR_NOMEM;
+    console->output = strdup(chardev);

libxl__strdup might be a better choice, so that you can ...

tools/libxl/libxl_create.c static int init_console_info(libxl__device_console *console, int dev_num) · line 412

Quoted 1 line
tools/libxl/libxl_create.c +1 -0
+    if (!console->output) return ERROR_NOMEM;

... get rid of this line.

Wei.

#7 lore raw

RFC PATCH 4/4 libxl: spawn a qemu to implement channels

In reply to David Scott

Since every channel is mapped to a console device in xenstore with 'output=chardev:libxl-channel%d', we need to tell qemu to create the appropriate chardevs.

---
 tools/libxl/libxl_create.c   |    3 ++-
 tools/libxl/libxl_dm.c       |   44 ++++++++++++++++++++++++++++++++++++++++--
 tools/libxl/libxl_internal.h |    3 ++-
 3 files changed, 46 insertions(+), 4 deletions(-)
tools/libxl/libxl_create.c +2 -1
index 13a2a27..2d2fb55 100644
@@ -1210,7 +1210,8 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
 
         need_qemu = libxl__need_xenpv_qemu(gc, 1, &console,
                 d_config->num_vfbs, d_config->vfbs,
-                d_config->num_disks, &d_config->disks[0]);
+                d_config->num_disks, &d_config->disks[0],
+                d_config->num_channels);
 
         console.backend_domid = state->console_domid;
         libxl__device_console_add(gc, domid, &console, state);
tools/libxl/libxl_dm.c +42 -2

69 lines not on this page · raw message

tools/libxl/libxl_internal.h +2 -1
index 082749e..a3b1f57 100644
@@ -1450,7 +1450,8 @@ _hidden const char *libxl__domain_device_model(libxl__gc *gc,
 _hidden int libxl__need_xenpv_qemu(libxl__gc *gc,
         int nr_consoles, libxl__device_console *consoles,
         int nr_vfbs, libxl_device_vfb *vfbs,
-        int nr_disks, libxl_device_disk *disks);
+        int nr_disks, libxl_device_disk *disks,
+        int nr_channels);
 
 /*
  * This function will cause the whole libxl process to hang
Signature · 1 line
1.7.10.4
#8 lore raw

Re: [PATCH RFC 4/4] libxl: spawn a qemu to implement channels

On Wed, Jun 11, 2014 at 09:24:29PM +0100, David Scott wrote:
[...]

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 424

Quoted 9 lines
tools/libxl/libxl_dm.c +9 -0
+    for (i = 0; i < guest_config->num_channels; i++) {
+        flexarray_append(dm_args, "-chardev");
+        type = guest_config->channels[i].type;
+        path = guest_config->channels[i].path;
+        switch (type) {
+            case LIBXL_CHANNEL_TYPE_NONE:
+                flexarray_append(dm_args,
+                                 libxl__sprintf(gc, "null,id=libxl-channel%d",
+                                                    i));

Wouldn't it be more appropriate to use channels[i].devid?

And the indentation here seems wrong.

There's also a macro called GCSPRINTF that you can use.

Wei.

author
#9 lore raw

Re: [PATCH RFC 4/4] libxl: spawn a qemu to implement channels

Hi Wei,

Quoted 12 lines · Wei Liu

On Wed, Jun 11, 2014 at 09:24:29PM +0100, David Scott wrote:
[...]

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 424

Quoted 9 lines
tools/libxl/libxl_dm.c +9 -0
+    for (i = 0; i < guest_config->num_channels; i++) {
+        flexarray_append(dm_args, "-chardev");
+        type = guest_config->channels[i].type;
+        path = guest_config->channels[i].path;
+        switch (type) {
+            case LIBXL_CHANNEL_TYPE_NONE:
+                flexarray_append(dm_args,
+                                 libxl__sprintf(gc, "null,id=libxl-channel%d",
+                                                    i));
Quoted 5 lines

Wouldn't it be more appropriate to use channels[i].devid?

And the indentation here seems wrong.

There's also a macro called GCSPRINTF that you can use.

Thanks for taking a look -- I’ll make a v2 which fixes these problems.

Thanks,
Dave