Revision 6663a195 hw/virtio-serial-bus.c

b/hw/virtio-serial-bus.c
66 66
    return NULL;
67 67
}
68 68

  
69
static bool use_multiport(VirtIOSerial *vser)
70
{
71
    return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
72
}
73

  
69 74
static size_t write_to_port(VirtIOSerialPort *port,
70 75
                            const uint8_t *buf, size_t size)
71 76
{
......
139 144
/* Functions for use inside qemu to open and read from/write to ports */
140 145
int virtio_serial_open(VirtIOSerialPort *port)
141 146
{
147
    /* Don't allow opening an already-open port */
148
    if (port->host_connected) {
149
        return 0;
150
    }
151
    /* Send port open notification to the guest */
152
    port->host_connected = true;
153
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
154

  
142 155
    return 0;
143 156
}
144 157

  
145 158
int virtio_serial_close(VirtIOSerialPort *port)
146 159
{
160
    port->host_connected = false;
161
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
162

  
147 163
    return 0;
148 164
}
149 165

  
......
151 167
ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
152 168
                            size_t size)
153 169
{
170
    if (!port || !port->host_connected || !port->guest_connected) {
171
        return 0;
172
    }
154 173
    return write_to_port(port, buf, size);
155 174
}
156 175

  
......
167 186
        virtio_queue_empty(vq)) {
168 187
        return 0;
169 188
    }
189
    if (use_multiport(port->vser) && !port->guest_connected) {
190
        return 0;
191
    }
170 192

  
171 193
    if (virtqueue_avail_bytes(vq, 4096, 0)) {
172 194
        return 4096;
......
203 225
        if (port->is_console) {
204 226
            send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
205 227
        }
228

  
229
        if (port->host_connected) {
230
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
231
        }
232

  
206 233
        /*
207 234
         * When the guest has asked us for this information it means
208 235
         * the guest is all setup and has its virtqueues
......
213 240
            port->info->guest_ready(port);
214 241
        }
215 242
        break;
243

  
244
    case VIRTIO_CONSOLE_PORT_OPEN:
245
        port->guest_connected = cpkt.value;
246
        if (cpkt.value && port->info->guest_open) {
247
            /* Send the guest opened notification if an app is interested */
248
            port->info->guest_open(port);
249
        }
250

  
251
        if (!cpkt.value && port->info->guest_close) {
252
            /* Send the guest closed notification if an app is interested */
253
            port->info->guest_close(port);
254
        }
255
        break;
216 256
    }
217 257
}
218 258

  
......
302 342
static void virtio_serial_save(QEMUFile *f, void *opaque)
303 343
{
304 344
    VirtIOSerial *s = opaque;
345
    VirtIOSerialPort *port;
346
    uint32_t nr_active_ports;
305 347

  
306 348
    /* The virtio device */
307 349
    virtio_save(&s->vdev, f);
......
310 352
    qemu_put_be16s(f, &s->config.cols);
311 353
    qemu_put_be16s(f, &s->config.rows);
312 354
    qemu_put_be32s(f, &s->config.nr_ports);
355

  
356
    /* Items in struct VirtIOSerial */
357

  
358
    /* Do this because we might have hot-unplugged some ports */
359
    nr_active_ports = 0;
360
    QTAILQ_FOREACH(port, &s->ports, next)
361
        nr_active_ports++;
362

  
363
    qemu_put_be32s(f, &nr_active_ports);
364

  
365
    /*
366
     * Items in struct VirtIOSerialPort.
367
     */
368
    QTAILQ_FOREACH(port, &s->ports, next) {
369
        /*
370
         * We put the port number because we may not have an active
371
         * port at id 0 that's reserved for a console port, or in case
372
         * of ports that might have gotten unplugged
373
         */
374
        qemu_put_be32s(f, &port->id);
375
        qemu_put_byte(f, port->guest_connected);
376
    }
313 377
}
314 378

  
315 379
static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
316 380
{
317 381
    VirtIOSerial *s = opaque;
382
    VirtIOSerialPort *port;
383
    uint32_t nr_active_ports;
384
    unsigned int i;
318 385

  
319 386
    if (version_id > 2) {
320 387
        return -EINVAL;
321 388
    }
389

  
322 390
    /* The virtio device */
323 391
    virtio_load(&s->vdev, f);
324 392

  
......
331 399
    qemu_get_be16s(f, &s->config.rows);
332 400
    s->config.nr_ports = qemu_get_be32(f);
333 401

  
402
    /* Items in struct VirtIOSerial */
403

  
404
    qemu_get_be32s(f, &nr_active_ports);
405

  
406
    /* Items in struct VirtIOSerialPort */
407
    for (i = 0; i < nr_active_ports; i++) {
408
        uint32_t id;
409

  
410
        id = qemu_get_be32(f);
411
        port = find_port_by_id(s, id);
412

  
413
        port->guest_connected = qemu_get_byte(f);
414
    }
415

  
334 416
    return 0;
335 417
}
336 418

  
......
360 442

  
361 443
    monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
362 444
                   indent, "", port->id);
445
    monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
446
                   indent, "", port->guest_connected);
447
    monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
448
                   indent, "", port->host_connected);
363 449
}
364 450

  
365 451
static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
......
393 479

  
394 480
    port->id = plugging_port0 ? 0 : port->vser->config.nr_ports++;
395 481

  
482
    if (!use_multiport(port->vser)) {
483
        /*
484
         * Allow writes to guest in this case; we have no way of
485
         * knowing if a guest port is connected.
486
         */
487
        port->guest_connected = true;
488
    }
489

  
396 490
    QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
397 491
    port->ivq = port->vser->ivqs[port->id];
398 492
    port->ovq = port->vser->ovqs[port->id];

Also available in: Unified diff