v2: Fixes, new way of discovering ports
cover v2: Fixes, new way of discovering ports
Hello,
These patches rework the way ports are announced to the guests. A control message is used to let the guest know a new port is added. Initial port discovery and port hot-plug work via this way now.
This was done to have the host and guest port numbering in sync to avoid surprises after several hotplug/unplug operations and migrations.
The ability to assign a particular port number to ports is also added so that management software can control the placement of ports.
The other patches to handle scatter/gather for guest data and migration fixes remain the same.
Please review.
Amit Shah (9):
virtio-serial-bus: save/load: Ensure target has enough ports
virtio-serial-bus: save/load: Ensure nr_ports on src and dest are
same.
virtio-serial: Remove redundant check for 0-sized write request
virtio-serial: Update copyright year to 2010
virtio-serial: save/load: Ensure we have hot-plugged ports
instantiated
virtio-serial-bus: Use control messages to notify guest of new ports
virtio-serial-bus: Let the guest know of host connection changes
after migration
virtio-serial: Handle scatter-gather buffers for control messages
virtio-serial: Handle scatter/gather input from the guest
hw/virtio-console.c | 4 +- hw/virtio-serial-bus.c | 245 +++++++++++++++++++++++++++++++++++++----------- hw/virtio-serial.h | 19 ++-- 3 files changed, 205 insertions(+), 63 deletions(-)
PATCH 1/9 virtio-serial-bus: save/load: Ensure target has enough ports
The target could be started with max_nr_ports for a virtio-serial device lesser than what was available on the source machine. Fail the migration in such a case.
--- hw/virtio-serial-bus.c | 10 +++++++++- 1 files changed, 9 insertions(+), 1 deletions(-)
hw/virtio-serial-bus.c +9 -1
@@ -374,6 +374,8 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)/* Items in struct VirtIOSerial */ +qemu_put_be32s (f, &s->bus->max_nr_ports); +/* Do this because we might have hot-unplugged some ports */ nr_active_ports =0 ;QTAILQ_FOREACH (port, &s->ports, next)
@@ -399,7 +401,7 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id) {VirtIOSerial *s = opaque;VirtIOSerialPort *port; -uint32_t nr_active_ports; +uint32_t max_nr_ports, nr_active_ports;unsigned int i;if (version_id >2 ) {
@@ -420,6 +422,12 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)/* Items in struct VirtIOSerial */ +qemu_get_be32s (f, &max_nr_ports); +if (max_nr_ports > s->bus->max_nr_ports) { +/* Source could have more ports than us. Fail migration. */ +return -EINVAL ; + } +qemu_get_be32s (f, &nr_active_ports);/* Items in struct VirtIOSerialPort */
Signature · 1 line
1.6.2.5
PATCH 2/9 virtio-serial-bus: save/load: Ensure nr_ports on src and dest are same.
The number of ports on the source as well as the destination machines should match. If they don't, it means some ports that got hotplugged on the source aren't instantiated on the destination. Or that ports that were hot-unplugged on the source are created on the destination.
--- hw/virtio-serial-bus.c | 18 ++++++++++++++++-- 1 files changed, 16 insertions(+), 2 deletions(-)
hw/virtio-serial-bus.c +16 -2
@@ -401,7 +401,7 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id) {VirtIOSerial *s = opaque;VirtIOSerialPort *port; -uint32_t max_nr_ports, nr_active_ports; +uint32_t max_nr_ports, nr_active_ports, nr_ports;unsigned int i;if (version_id >2 ) {
@@ -418,7 +418,21 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)/* The config space */ qemu_get_be16s (f, &s->config.cols);qemu_get_be16s (f, &s->config.rows); - s->config.nr_ports =qemu_get_be32 (f); + nr_ports =qemu_get_be32 (f); + +if (nr_ports != s->config.nr_ports) { +/* +* Source hot-plugged/unplugged ports and we don't have all of +* them here. +* +* Note: This condition cannot check for all hotplug/unplug +* events: eg, if one port was hot-plugged and one was +* unplugged, the nr_ports remains the same but the port id's +* would have changed and we won't catch it here. A later +* check for !find_port_by_id() will confirm if this happened. +*/ +return -EINVAL ; + }/* Items in struct VirtIOSerial */
Signature · 1 line
1.6.2.5
PATCH 3/9 virtio-serial: Remove redundant check for 0-sized write request
The check for a 0-sized write request to a guest port is not necessary; the while loop below won't be executed in this case and all will be fine.
--- hw/virtio-serial-bus.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)
hw/virtio-serial-bus.c +0 -3
@@ -83,9 +83,6 @@ static size_t write_to_port(VirtIOSerialPort *port,if (!virtio_queue_ready (vq)) {return 0 ; } -if (!size) { -return 0 ; - }while (offset < size) {int i;
Signature · 1 line
1.6.2.5
PATCH 4/9 virtio-serial: Update copyright year to 2010
--- hw/virtio-console.c | 2 +- hw/virtio-serial-bus.c | 2 +- hw/virtio-serial.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
hw/virtio-console.c +1 -1
@@ -1,7 +1,7 @@/* * Virtio Console and Generic Serial Port Devices * -* Copyright Red Hat, Inc. 2009 +* Copyright Red Hat, Inc. 2009, 2010 * * Authors: * Amit Shah <[email protected]>
hw/virtio-serial-bus.c +1 -1
@@ -1,7 +1,7 @@/* * A bus for connecting virtio serial and console ports * -* Copyright (C) 2009 Red Hat, Inc. +* Copyright (C) 2009, 2010 Red Hat, Inc. * * Author(s): * Amit Shah <[email protected]>
hw/virtio-serial.h +1 -1
@@ -2,7 +2,7 @@* Virtio Serial / Console Support * * Copyright IBM, Corp. 2008 -* Copyright Red Hat, Inc. 2009 +* Copyright Red Hat, Inc. 2009, 2010 * * Authors: * Christian Ehrhardt <[email protected]>
Signature · 1 line
1.6.2.5
PATCH 5/9 virtio-serial: save/load: Ensure we have hot-plugged ports instantiated
If some ports that were hot-plugged on the source are not available on the destination, fail migration instead of trying to deref a NULL pointer.
--- hw/virtio-serial-bus.c | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +7 -0
@@ -447,6 +447,13 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id) id =qemu_get_be32 (f); port =find_port_by_id (s, id); +if (!port) { +/* +* The requested port was hot-plugged on the source but we +* don't have it +*/ +return -EINVAL ; + } port->guest_connected =qemu_get_byte (f); }
Signature · 1 line
1.6.2.5
PATCH 6/9 virtio-serial-bus: Use control messages to notify guest of new ports
Allow the port 'id's to be set by a user on the command line. This is needed by management apps that will want a stable port numbering scheme for hot-plug/unplug and migration.
Since the port numbers are shared with the guest (to identify ports in control messages), we just send a control message to the guest indicating addition of new ports (hot-plug) or notifying the guest of the available ports when the guest sends us a DEVICE_READY control message.
--- hw/virtio-console.c | 2 + hw/virtio-serial-bus.c | 182 +++++++++++++++++++++++++++++++----------------- hw/virtio-serial.h | 17 +++-- 3 files changed, 130 insertions(+), 71 deletions(-)
hw/virtio-console.c +2 -0
@@ -99,6 +99,7 @@ static VirtIOSerialPortInfo virtconsole_info = { .exit = virtconsole_exitfn, .qdev.props = (Property []) {DEFINE_PROP_UINT8 ("is_console" , VirtConsole, port.is_console,1 ), +DEFINE_PROP_UINT32 ("nr" , VirtConsole, port.id,VIRTIO_CONSOLE_BAD_ID ),DEFINE_PROP_CHR ("chardev" , VirtConsole, chr),DEFINE_PROP_STRING ("name" , VirtConsole, port.name),DEFINE_PROP_END_OF_LIST (),
@@ -133,6 +134,7 @@ static VirtIOSerialPortInfo virtserialport_info = { .init = virtserialport_initfn, .exit = virtconsole_exitfn, .qdev.props = (Property []) { +DEFINE_PROP_UINT32 ("nr" , VirtConsole, port.id,VIRTIO_CONSOLE_BAD_ID ),DEFINE_PROP_CHR ("chardev" , VirtConsole, chr),DEFINE_PROP_STRING ("name" , VirtConsole, port.name),DEFINE_PROP_END_OF_LIST (),
hw/virtio-serial-bus.c +118 -64
292 lines not on this page · raw message
hw/virtio-serial.h +10 -7
@@ -27,6 +27,8 @@/* Features supported */ #define VIRTIO_CONSOLE_F_MULTIPORT 1 +#define VIRTIO_CONSOLE_BAD_ID (~(uint32_t)0) +struct virtio_console_config {/* * These two fields are used by VIRTIO_CONSOLE_F_SIZE which
@@ -36,7 +38,6 @@ struct virtio_console_config {uint16_t rows;uint32_t max_nr_ports; -uint32_t nr_ports; } __attribute__((packed));struct virtio_console_control {
@@ -46,12 +47,14 @@ struct virtio_console_control { };/* Some events for the internal messages (control packets) */ -#define VIRTIO_CONSOLE_PORT_READY 0 -#define VIRTIO_CONSOLE_CONSOLE_PORT 1 -#define VIRTIO_CONSOLE_RESIZE 2 -#define VIRTIO_CONSOLE_PORT_OPEN 3 -#define VIRTIO_CONSOLE_PORT_NAME 4 -#define VIRTIO_CONSOLE_PORT_REMOVE 5 +#define VIRTIO_CONSOLE_DEVICE_READY 0 +#define VIRTIO_CONSOLE_PORT_ADD 1 +#define VIRTIO_CONSOLE_PORT_REMOVE 2 +#define VIRTIO_CONSOLE_PORT_READY 3 +#define VIRTIO_CONSOLE_CONSOLE_PORT 4 +#define VIRTIO_CONSOLE_RESIZE 5 +#define VIRTIO_CONSOLE_PORT_OPEN 6 +#define VIRTIO_CONSOLE_PORT_NAME 7/* == In-qemu interface == */
Signature · 1 line
1.6.2.5
PATCH 7/9 virtio-serial-bus: Let the guest know of host connection changes after migration
If the host connection to a port is closed on the destination machine after migration, when the connection was open on the source, the host has to be informed of that.
Similar for a host connection open on the destination.
--- hw/virtio-serial-bus.c | 11 +++++++++++ 1 files changed, 11 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +11 -0
@@ -408,6 +408,7 @@ static void virtio_serial_save(QEMUFile *f, void *opaque) QTAILQ_FOREACH(port, &s->ports, next) {qemu_put_be32s (f, &port->id);qemu_put_byte (f, port->guest_connected); +qemu_put_byte (f, port->host_connected); } }
@@ -461,11 +462,21 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)/* Items in struct VirtIOSerialPort */ for (i =0 ; i < nr_active_ports; i++) {uint32_t id; +bool host_connected; id =qemu_get_be32 (f); port =find_port_by_id (s, id); port->guest_connected =qemu_get_byte (f); + host_connected =qemu_get_byte (f); +if (host_connected != port->host_connected) { +/* +* We have to let the guest know of the host connection +* status change +*/ +send_control_event (port,VIRTIO_CONSOLE_PORT_OPEN , + port->host_connected); + } }return 0 ; }
Signature · 1 line
1.6.2.5
PATCH 8/9 virtio-serial: Handle scatter-gather buffers for control messages
Current control messages are small enough to not be split into multiple buffers but we could run into such a situation in the future or a malicious guest could cause such a situation.
So handle the entire iov request for control messages.
Also ensure the size of the control request is >= what we expect otherwise we risk accessing memory that we don't own.
--- hw/virtio-serial-bus.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 41 insertions(+), 3 deletions(-)
hw/virtio-serial-bus.c +41 -3
@@ -205,7 +205,7 @@ size_t virtio_serial_guest_ready(VirtIOSerialPort *port) }/* Guest wants to notify us of some event */ -static void handle_control_message (VirtIOSerial *vser,void *buf) +static void handle_control_message (VirtIOSerial *vser,void *buf,size_t len) {struct VirtIOSerialPort *port;struct virtio_console_control cpkt, *gcpkt;
@@ -214,6 +214,11 @@ static void handle_control_message(VirtIOSerial *vser, void *buf) gcpkt = buf; +if (len <sizeof (cpkt)) { +/* The guest sent an invalid control packet */ +return ; + } + cpkt.event =lduw_p (&gcpkt->event); cpkt.value =lduw_p (&gcpkt->value);
@@ -297,12 +302,45 @@ static void control_out(VirtIODevice *vdev, VirtQueue *vq) {VirtQueueElement elem;VirtIOSerial *vser; +uint8_t *buf; +size_t len; vser =DO_UPCAST (VirtIOSerial, vdev, vdev); + len =0 ; + buf =NULL ;while (virtqueue_pop (vq, &elem)) { -handle_control_message (vser, elem.out_sg[0 ].iov_base); -virtqueue_push (vq, &elem, elem.out_sg[0 ].iov_len); +unsigned int i; +size_t cur_len, offset; + + cur_len =0 ; +for (i =0 ; i < elem.out_num; i++) { + cur_len += elem.out_sg[i].iov_len; + } +/* +* Allocate a new buf only if we didn't have one previously or +* if the size of the buf differs +*/ +if (cur_len != len) { +if (len) { +qemu_free (buf); + } + buf =qemu_malloc (cur_len); + } + + offset =0 ; +for (i =0 ; i < elem.out_num; i++) { +memcpy (buf + offset, elem.out_sg[i].iov_base, + elem.out_sg[i].iov_len); + offset += elem.out_sg[i].iov_len; + } + len = cur_len; + +handle_control_message (vser, buf, len); +virtqueue_push (vq, &elem, len); + } +if (len) { +qemu_free (buf); }virtio_notify (vdev, vq); }
Signature · 1 line
1.6.2.5
PATCH 9/9 virtio-serial: Handle scatter/gather input from the guest
Current guests don't send more than one iov but it can change later. Ensure we handle that case.
--- hw/virtio-serial-bus.c | 22 +++++++++++++++------- 1 files changed, 15 insertions(+), 7 deletions(-)
hw/virtio-serial-bus.c +15 -7
@@ -355,11 +355,12 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)while (virtqueue_pop (vq, &elem)) {VirtIOSerialPort *port; -size_t ret; +size_t len; +unsigned int i; + len =0 ; port =find_port_by_vq (vser, vq);if (!port) { - ret =0 ; goto next_buf; }
@@ -369,16 +370,23 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)* with it. Just ignore the data in that case. */ if (!port->info->have_data) { - ret =0 ; goto next_buf; } -/* The guest always sends only one sg */ - ret = port->info->have_data (port, elem.out_sg[0 ].iov_base, - elem.out_sg[0 ].iov_len); +for (i =0 ; i < elem.out_num; i++) { +size_t ret; + + ret = port->info->have_data (port, elem.out_sg[0 ].iov_base, + elem.out_sg[0 ].iov_len); +if (ret < elem.out_sg[0 ].iov_len) { +/* We couldn't write the entire iov; stop processing now */ +break ; + } + len += ret; + } next_buf: -virtqueue_push (vq, &elem, ret); +virtqueue_push (vq, &elem, len); }virtio_notify (vdev, vq); }
Signature · 1 line
1.6.2.5
[Qemu-devel] Re: [PATCH 9/9] virtio-serial: Handle scatter/gather input from the guest
Quoted 9 lines · Amit Shah
hw/virtio-serial-bus.c +0
-2@@ -369,16 +370,23 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)* with it. Just ignore the data in that case. */ if (!port->info->have_data) { - ret =0 ; goto next_buf; } -/* The guest always sends only one sg */
hw/virtio-serial-bus.c static void handle_output(VirtIODevice *vdev, VirtQueue *vq) · line 383
Quoted 10 lines
hw/virtio-serial-bus.c +8
-2- ret = port->info->have_data (port, elem.out_sg[0 ].iov_base, - elem.out_sg[0 ].iov_len); +for (i =0 ; i < elem.out_num; i++) { +size_t ret; + + ret = port->info->have_data (port, elem.out_sg[0 ].iov_base, + elem.out_sg[0 ].iov_len); +if (ret < elem.out_sg[0 ].iov_len) { +/* We couldn't write the entire iov; stop processing now */ +break ;
We should increment len here if ret > 0.
I'll post a followup patch that does this.
Quoted 13 lines
hw/virtio-serial-bus.c +4
-1+ } + len += ret; + } next_buf: -virtqueue_push (vq, &elem, ret); +virtqueue_push (vq, &elem, len); }virtio_notify (vdev, vq); }Signature · 1 line
1.6.2.5
Amit
Quoted 10 lines · Amit Shah
Hello,
These patches rework the way ports are announced to the guests. A control message is used to let the guest know a new port is added. Initial port discovery and port hot-plug work via this way now.
This was done to have the host and guest port numbering in sync to avoid surprises after several hotplug/unplug operations and migrations.
Quoted 7 lines
The ability to assign a particular port number to ports is also added so that management software can control the placement of ports.
The other patches to handle scatter/gather for guest data and migration fixes remain the same.
Please review.
it fixes the migration troubles/questions that I raised. From possible NULL dereference to checking maximum numbers of ports.
Thanks, Juan.