Skip to content

Commit 5105b1d

Browse files
David Marchandelmarco
authored andcommitted
ivshmem: add check on protocol version in QEMU
Send a protocol version as the first message from server, clients must close communication if they don't support this protocol version. Older QEMUs should be fine with this change in the protocol since they overrides their own vm_id on reception of an id associated to no eventfd. Signed-off-by: David Marchand <[email protected]> Signed-off-by: Marc-André Lureau <[email protected]> [use fifo_update_and_get()] Reviewed-by: Claudio Fontana <[email protected]>
1 parent 8c4ef20 commit 5105b1d

File tree

7 files changed

+81
-8
lines changed

7 files changed

+81
-8
lines changed

contrib/ivshmem-client/ivshmem-client.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,17 @@ ivshmem_client_connect(IvshmemClient *client)
206206
goto err_close;
207207
}
208208

209-
/* first, we expect our index + a fd == -1 */
209+
/* first, we expect a protocol version */
210+
if (ivshmem_client_read_one_msg(client, &tmp, &fd) < 0 ||
211+
(tmp != IVSHMEM_PROTOCOL_VERSION) || fd != -1) {
212+
IVSHMEM_CLIENT_DEBUG(client, "cannot read from server\n");
213+
goto err_close;
214+
}
215+
216+
/* then, we expect our index + a fd == -1 */
210217
if (ivshmem_client_read_one_msg(client, &client->local.id, &fd) < 0 ||
211218
client->local.id < 0 || fd != -1) {
212-
IVSHMEM_CLIENT_DEBUG(client, "cannot read from server\n");
219+
IVSHMEM_CLIENT_DEBUG(client, "cannot read from server (2)\n");
213220
goto err_close;
214221
}
215222
IVSHMEM_CLIENT_DEBUG(client, "our_id=%ld\n", client->local.id);
@@ -221,7 +228,7 @@ ivshmem_client_connect(IvshmemClient *client)
221228
if (fd >= 0) {
222229
close(fd);
223230
}
224-
IVSHMEM_CLIENT_DEBUG(client, "cannot read from server (2)\n");
231+
IVSHMEM_CLIENT_DEBUG(client, "cannot read from server (3)\n");
225232
goto err_close;
226233
}
227234
client->shm_fd = fd;

contrib/ivshmem-client/ivshmem-client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <sys/select.h>
2424

2525
#include "qemu/queue.h"
26+
#include "hw/misc/ivshmem.h"
2627

2728
/**
2829
* Maximum number of notification vectors supported by the client

contrib/ivshmem-server/ivshmem-server.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ ivshmem_server_send_initial_info(IvshmemServer *server, IvshmemServerPeer *peer)
101101
{
102102
int ret;
103103

104+
/* send our protocol version first */
105+
ret = ivshmem_server_send_one_msg(peer->sock_fd, IVSHMEM_PROTOCOL_VERSION,
106+
-1);
107+
if (ret < 0) {
108+
IVSHMEM_SERVER_DEBUG(server, "cannot send version: %s\n",
109+
strerror(errno));
110+
return -1;
111+
}
112+
104113
/* send the peer id to the client */
105114
ret = ivshmem_server_send_one_msg(peer->sock_fd, peer->id, -1);
106115
if (ret < 0) {

contrib/ivshmem-server/ivshmem-server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "qemu/event_notifier.h"
3535
#include "qemu/queue.h"
36+
#include "hw/misc/ivshmem.h"
3637

3738
/**
3839
* Maximum number of notification vectors supported by the server

docs/specs/ivshmem_device_spec.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ It creates a shared memory object then waits for clients to connect on a unix
6464
socket.
6565

6666
For each client (QEMU process) that connects to the server:
67+
- the server sends a protocol version, if client does not support it, the client
68+
closes the communication,
6769
- the server assigns an ID for this client and sends this ID to him as the first
6870
message,
6971
- the server sends a fd to the shared memory object to this client,
@@ -86,9 +88,10 @@ been provided in qemu.git/contrib/ivshmem-client for debug.
8688

8789
*QEMU as an ivshmem client*
8890

89-
At initialisation, when creating the ivshmem device, QEMU gets its ID from the
90-
server then makes it available through BAR0 IVPosition register for the VM to
91-
use (see 'PCI device registers' subsection).
91+
At initialisation, when creating the ivshmem device, QEMU first receives a
92+
protocol version and closes communication with server if it does not match.
93+
Then, QEMU gets its ID from the server then makes it available through BAR0
94+
IVPosition register for the VM to use (see 'PCI device registers' subsection).
9295
QEMU then uses the fd to the shared memory to map it to BAR2.
9396
eventfds for all other clients received from the server are stored to implement
9497
BAR0 Doorbell register (see 'PCI device registers' subsection).

hw/misc/ivshmem.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "qemu/fifo8.h"
2828
#include "sysemu/char.h"
2929

30+
#include "hw/misc/ivshmem.h"
31+
3032
#include <sys/mman.h>
3133
#include <sys/types.h>
3234
#include <limits.h>
@@ -596,6 +598,31 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
596598
}
597599
}
598600

601+
static void ivshmem_check_version(void *opaque, const uint8_t * buf, int size)
602+
{
603+
IVShmemState *s = opaque;
604+
int tmp;
605+
long version;
606+
607+
if (!fifo_update_and_get(s, buf, size,
608+
&version, sizeof(version))) {
609+
return;
610+
}
611+
612+
tmp = qemu_chr_fe_get_msgfd(s->server_chr);
613+
if (tmp != -1 || version != IVSHMEM_PROTOCOL_VERSION) {
614+
fprintf(stderr, "incompatible version, you are connecting to a ivshmem-"
615+
"server using a different protocol please check your setup\n");
616+
qemu_chr_delete(s->server_chr);
617+
s->server_chr = NULL;
618+
return;
619+
}
620+
621+
IVSHMEM_DPRINTF("version check ok, switch to real chardev handler\n");
622+
qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
623+
ivshmem_event, s);
624+
}
625+
599626
/* Select the MSI-X vectors used by device.
600627
* ivshmem maps events to vectors statically, so
601628
* we just enable all vectors on init and after reset. */
@@ -769,8 +796,8 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
769796

770797
s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
771798

772-
qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
773-
ivshmem_event, s);
799+
qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive,
800+
ivshmem_check_version, ivshmem_event, s);
774801
} else {
775802
/* just map the file immediately, we're not using a server */
776803
int fd;

include/hw/misc/ivshmem.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
/*
3+
* Inter-VM Shared Memory PCI device.
4+
*
5+
* Author:
6+
* Cam Macdonell <[email protected]>
7+
*
8+
* Based On: cirrus_vga.c
9+
* Copyright (c) 2004 Fabrice Bellard
10+
* Copyright (c) 2004 Makoto Suzuki (suzu)
11+
*
12+
* and rtl8139.c
13+
* Copyright (c) 2006 Igor Kovalenko
14+
*
15+
* This code is licensed under the GNU GPL v2.
16+
*
17+
* Contributions after 2012-01-13 are licensed under the terms of the
18+
* GNU GPL, version 2 or (at your option) any later version.
19+
*/
20+
#ifndef IVSHMEM_H
21+
#define IVSHMEM_H
22+
23+
#define IVSHMEM_PROTOCOL_VERSION 0
24+
25+
#endif /* IVSHMEM_H */

0 commit comments

Comments
 (0)