Patchwork [4/8] virtio-serial-bus: Add a port 'name' property for port discovery in guests

login
register
mail settings
Submitter Amit Shah
Date Dec. 23, 2009, 7:52 p.m.
Message ID <1261597948-24293-5-git-send-email-amit.shah@redhat.com>
Download mbox | patch
Permalink /patch/41695/
State New
Headers show

Comments

Amit Shah - Dec. 23, 2009, 7:52 p.m.
The port 'id' or number is internal state between the guest kernel and
our bus implementation. This is invocation-dependent and isn't part of
the guest-host ABI.

To correcly enumerate and map ports between the host and the guest, the
'name' property is used.

Example:

    -device virtserialport,name=org.qemu.port.0

This invocation will get us a char device in the guest at:

    /dev/virtio-ports/org.qemu.port.0

which can be a symlink to

    /dev/vport0p3

This 'name' property is exposed by the guest kernel in a sysfs
attribute:

    /sys/kernel/virtio-ports/vport0p3/name

A simple udev script can pick up this name and create the symlink
mentioned above.

Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
 hw/virtio-serial-bus.c |   16 ++++++++++++++++
 hw/virtio-serial.c     |    1 +
 hw/virtio-serial.h     |    8 ++++++++
 3 files changed, 25 insertions(+), 0 deletions(-)
Anthony Liguori - Dec. 23, 2009, 11:15 p.m.
On 12/23/2009 01:52 PM, Amit Shah wrote:
> The port 'id' or number is internal state between the guest kernel and
> our bus implementation. This is invocation-dependent and isn't part of
> the guest-host ABI.
>
> To correcly enumerate and map ports between the host and the guest, the
> 'name' property is used.
>
> Example:
>
>      -device virtserialport,name=org.qemu.port.0
>
> This invocation will get us a char device in the guest at:
>
>      /dev/virtio-ports/org.qemu.port.0
>
> which can be a symlink to
>
>      /dev/vport0p3
>
> This 'name' property is exposed by the guest kernel in a sysfs
> attribute:
>
>      /sys/kernel/virtio-ports/vport0p3/name
>
> A simple udev script can pick up this name and create the symlink
> mentioned above.
>
> Signed-off-by: Amit Shah<amit.shah@redhat.com>
> ---
>   hw/virtio-serial-bus.c |   16 ++++++++++++++++
>   hw/virtio-serial.c     |    1 +
>   hw/virtio-serial.h     |    8 ++++++++
>   3 files changed, 25 insertions(+), 0 deletions(-)
>
> diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c
> index 27120da..b683109 100644
> --- a/hw/virtio-serial-bus.c
> +++ b/hw/virtio-serial-bus.c
> @@ -236,6 +236,8 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
>   {
>       struct VirtIOSerialPort *port;
>       struct virtio_console_control *cpkt;
> +    uint8_t *buffer;
> +    size_t buffer_len;
>
>       cpkt = buf;
>       port = find_port_by_id(vser, cpkt->id);
> @@ -254,6 +256,20 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
>           if (port->is_console) {
>               send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
>           }
> +        if (port->name) {
> +            cpkt->event = VIRTIO_CONSOLE_PORT_NAME;
> +            cpkt->value = 1;

This isn't endian-safe.  The guest may have a different endianness than 
the host.

Regards,

Anthony Liguori

Patch

diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c
index 27120da..b683109 100644
--- a/hw/virtio-serial-bus.c
+++ b/hw/virtio-serial-bus.c
@@ -236,6 +236,8 @@  static void handle_control_message(VirtIOSerial *vser, void *buf)
 {
     struct VirtIOSerialPort *port;
     struct virtio_console_control *cpkt;
+    uint8_t *buffer;
+    size_t buffer_len;
 
     cpkt = buf;
     port = find_port_by_id(vser, cpkt->id);
@@ -254,6 +256,20 @@  static void handle_control_message(VirtIOSerial *vser, void *buf)
         if (port->is_console) {
             send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
         }
+        if (port->name) {
+            cpkt->event = VIRTIO_CONSOLE_PORT_NAME;
+            cpkt->value = 1;
+
+            buffer_len = sizeof(*cpkt) + strlen(port->name) + 1;
+            buffer = qemu_malloc(buffer_len);
+
+            memcpy(buffer, cpkt, sizeof(*cpkt));
+            memcpy(buffer + sizeof(*cpkt), port->name, strlen(port->name));
+            buffer[buffer_len - 1] = 0;
+
+            send_control_msg(port, buffer, buffer_len);
+            qemu_free(buffer);
+        }
         if (port->host_connected) {
             send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
         }
diff --git a/hw/virtio-serial.c b/hw/virtio-serial.c
index 654a259..2cc51c5 100644
--- a/hw/virtio-serial.c
+++ b/hw/virtio-serial.c
@@ -103,6 +103,7 @@  static VirtIOSerialPortInfo virtconsole_info = {
     .qdev.props = (Property[]) {
         DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1),
         DEFINE_PROP_CHR("chardev", VirtConsole, chr),
+        DEFINE_PROP_STRING("name", VirtConsole, port.name),
         DEFINE_PROP_END_OF_LIST(),
     },
 };
diff --git a/hw/virtio-serial.h b/hw/virtio-serial.h
index b855375..5505841 100644
--- a/hw/virtio-serial.h
+++ b/hw/virtio-serial.h
@@ -54,6 +54,7 @@  struct virtio_console_header {
 #define VIRTIO_CONSOLE_CONSOLE_PORT	1
 #define VIRTIO_CONSOLE_RESIZE		2
 #define VIRTIO_CONSOLE_PORT_OPEN	3
+#define VIRTIO_CONSOLE_PORT_NAME	4
 
 /* == In-qemu interface == */
 
@@ -88,6 +89,13 @@  struct VirtIOSerialPort {
     VirtQueue *ivq, *ovq;
 
     /*
+     * This name is sent to the guest and exported via sysfs.
+     * The guest could create symlinks based on this information.
+     * The name is in the reverse fqdn format, like org.qemu.console.0
+     */
+    char *name;
+
+    /*
      * This id helps identify ports between the guest and the host.
      * The guest sends a "header" with this id with each data packet
      * that it sends and the host can then find out which associated