contrib: add vhost-user-sim
RFC 1/1 contrib: add vhost-user-sim
From: Johannes Berg <[email protected]>
This implements
* the UM protocol to access the simulation calendar,
* the underlying simulation calendar, and
* a very trivial simulated network as a vhost-user virtio_net device.
Currently the code is a bit rough and mostly an example, things such as the network propagation delay and the number of clients expected to connect before the whole simulation starts are both hardcoded; nevertheless, it serves as an example of how to build such a simulation.
There are also some bugs in the calendar code that are hard to fix using glib - all the file descriptors should be handled recursively at all times. The code does some of that, but then may deadlock in the calendar.
--- .gitignore | 1 + Makefile | 3 + Makefile.objs | 1 + contrib/vhost-user-sim/Makefile.objs | 1 + contrib/vhost-user-sim/cal.c | 288 ++++++++++++++++++ contrib/vhost-user-sim/cal.h | 45 +++ contrib/vhost-user-sim/main.c | 122 ++++++++ contrib/vhost-user-sim/main.h | 22 ++ contrib/vhost-user-sim/net.c | 246 +++++++++++++++ contrib/vhost-user-sim/simtime.c | 267 ++++++++++++++++ .../standard-headers/linux/um_timetravel.h | 107 +++++++ 11 files changed, 1103 insertions(+) create mode 100644 contrib/vhost-user-sim/Makefile.objs create mode 100644 contrib/vhost-user-sim/cal.c create mode 100644 contrib/vhost-user-sim/cal.h create mode 100644 contrib/vhost-user-sim/main.c create mode 100644 contrib/vhost-user-sim/main.h create mode 100644 contrib/vhost-user-sim/net.c create mode 100644 contrib/vhost-user-sim/simtime.c create mode 100644 include/standard-headers/linux/um_timetravel.h
.gitignore +1 -0
@@ -67,6 +67,7 @@ /vhost-user-blk /vhost-user-gpu /vhost-user-input +/vhost-user-sim /fsdev/virtfs-proxy-helper *.tmp *.[1-9]
Makefile +3 -0
@@ -418,6 +418,7 @@ dummy := $(call unnest-vars,, \ vhost-user-blk-obj-y \ vhost-user-input-obj-y \ vhost-user-gpu-obj-y \ + vhost-user-sim-obj-y \ qga-vss-dll-obj-y \ block-obj-y \ block-obj-m \
@@ -638,6 +639,8 @@ vhost-user-scsi$(EXESUF): $(vhost-user-scsi-obj-y) libvhost-user.a $(call LINK, $^) vhost-user-blk$(EXESUF): $(vhost-user-blk-obj-y) libvhost-user.a $(call LINK, $^) +vhost-user-sim$(EXESUF): $(vhost-user-sim-obj-y) libvhost-user.a + $(call LINK, $^) rdmacm-mux$(EXESUF): LIBS += "-libumad" rdmacm-mux$(EXESUF): $(rdmacm-mux-obj-y) $(COMMON_LDADDS)
Makefile.objs +1 -0
@@ -122,6 +122,7 @@ vhost-user-scsi.o-cflags := $(LIBISCSI_CFLAGS) vhost-user-scsi.o-libs := $(LIBISCSI_LIBS) vhost-user-scsi-obj-y = contrib/vhost-user-scsi/ vhost-user-blk-obj-y = contrib/vhost-user-blk/ +vhost-user-sim-obj-y = contrib/vhost-user-sim/ rdmacm-mux-obj-y = contrib/rdmacm-mux/ vhost-user-input-obj-y = contrib/vhost-user-input/ vhost-user-gpu-obj-y = contrib/vhost-user-gpu/
contrib/vhost-user-sim/Makefile.objs new file +1 -0
@@ -0,0 +1 @@ +vhost-user-sim-obj-y = cal.o simtime.o net.o main.o
contrib/vhost-user-sim/cal.c new file +288 -0
288 lines not on this page · raw message
contrib/vhost-user-sim/cal.h new file +45 -0
45 lines not on this page · raw message
contrib/vhost-user-sim/main.c new file +122 -0
122 lines not on this page · raw message
contrib/vhost-user-sim/main.h new file +22 -0
@@ -0,0 +1,22 @@ +/* +* vhost-user sim main application header file +* +* Copyright (c) 2019 Intel Corporation. All rights reserved. +* +* Author: +* Johannes Berg <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 only. +* See the COPYING file in the top-level directory. +*/ +#ifndef _SIM_MAIN_H +#define _SIM_MAIN_H + +gboolean simtime_client_connected (GIOChannel *src, +GIOCondition cond, +gpointer data); +gboolean vu_net_client_connected (GIOChannel *src, +GIOCondition cond, +gpointer data); + +#endif /* _SIM_MAIN_H */
contrib/vhost-user-sim/net.c new file +246 -0
246 lines not on this page · raw message
contrib/vhost-user-sim/simtime.c new file +267 -0
267 lines not on this page · raw message
include/standard-headers/linux/um_timetravel.h new file +107 -0
107 lines not on this page · raw message
Signature · 1 line
2.20.1
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Subject: [Qemu-devel] [RFC] contrib: add vhost-user-sim
Message-id: [email protected]
Type: series
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
e897496 contrib: add vhost-user-sim
=== OUTPUT BEGIN ===
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #73:
new file mode 100644
WARNING: line over 80 characters
#254: FILE: contrib/vhost-user-sim/cal.c:170:
+ if (running_entry && entry != running_entry && running_entry->update_until) {
ERROR: line over 90 characters
#257: FILE: contrib/vhost-user-sim/cal.c:173:
+ DPRINT("update %s to be free until %lld (due to add unless of %s)\n", running_entry->name, nexttime, entry->name);
ERROR: line over 90 characters
#260: FILE: contrib/vhost-user-sim/cal.c:176:
+ DPRINT("no update for running entry %s\n", running_entry ? running_entry->name : "<none>");
ERROR: line over 90 characters
#278: FILE: contrib/vhost-user-sim/cal.c:194:
+ DPRINT("update %s to be free until %lld (due to add of %s)\n", running_entry->name, nexttime, entry->name);
ERROR: line over 90 characters
#281: FILE: contrib/vhost-user-sim/cal.c:197:
+ DPRINT("no update for running entry %s\n", running_entry ? running_entry->name : "<none>");
WARNING: line over 80 characters
#525: FILE: contrib/vhost-user-sim/main.c:96:
+ printf("Usage: %s -s time-device-socket [-n net-device-socket] | [ -h ]\n",
ERROR: braces {} are necessary for all arms of this statement #547: FILE: contrib/vhost-user-sim/main.c:118:
+ if (net_socket)
[...]
ERROR: line over 90 characters
#741: FILE: contrib/vhost-user-sim/net.c:156:
+ fprintf(stderr, "insert IRQ from %s at %lld\n", ndev->entry.name, calendar_get_time());
WARNING: line over 80 characters
#951: FILE: contrib/vhost-user-sim/simtime.c:114:
+ DPRINT(" %d | calendar entry added for %lld\n", conn->idx, msg->time);
WARNING: line over 80 characters
#954: FILE: contrib/vhost-user-sim/simtime.c:117:
+ DPRINT(" %d | calendar entry time updated for %lld\n", conn->idx, msg->time);
ERROR: line over 90 characters
#980: FILE: contrib/vhost-user-sim/simtime.c:143:
+ g_assert(full_write(g_io_channel_unix_get_fd(conn->chan), &resp, sizeof(resp)) == sizeof(resp));
ERROR: line over 90 characters
#981: FILE: contrib/vhost-user-sim/simtime.c:144:
+ DPRINT(" %d | sent ACK for message %s (%lld, time %lld)\n", conn->idx, simtime_op_str(msg->op), msg->op, msg->time);
ERROR: line over 90 characters
#991: FILE: contrib/vhost-user-sim/simtime.c:154:
+ g_assert(full_write(g_io_channel_unix_get_fd(conn->chan), msg, sizeof(*msg)) == sizeof(*msg));
ERROR: line over 90 characters
#993: FILE: contrib/vhost-user-sim/simtime.c:156:
+ g_assert(full_read(g_io_channel_unix_get_fd(conn->chan), msg, sizeof(*msg)) == sizeof(*msg));
ERROR: braces {} are necessary for all arms of this statement #996: FILE: contrib/vhost-user-sim/simtime.c:159:
+ if (msg->op == UM_TIMETRAVEL_ACK)
[...]
ERROR: line over 90 characters
#1043: FILE: contrib/vhost-user-sim/simtime.c:206:
+ printf("client disconnected, made %lld requests and waited %lld times, sent %lld updates\n",
ERROR: do not use C99 // comments
#1048: FILE: contrib/vhost-user-sim/simtime.c:211:
+ // leak for now ... source is still attached
ERROR: do not use C99 // comments
#1049: FILE: contrib/vhost-user-sim/simtime.c:212:
+ //g_free(conn);
total: 14 errors, 5 warnings, 1127 lines checked
Commit e89749692cba (contrib: add vhost-user-sim) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series failed the asan build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.
=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-fedora V=1 NETWORK=1
time make docker-test-debug@fedora TARGET_LIST=x86_64-softmmu J=14 NETWORK=1 === TEST SCRIPT END ===
./tests/docker/docker.py --engine auto build qemu:fedora tests/docker/dockerfiles/fedora.docker --add-current-user
Image is up to date.
LD [email protected]
cc: fatal error: no input files
compilation terminated.
make: *** [[email protected]] Error 4
The full log is available at
http://patchew.org/logs/[email protected]/testing.asan/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series failed the docker-mingw@fedora build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.
=== TEST SCRIPT BEGIN ===
#! /bin/bash
make docker-image-fedora V=1 NETWORK=1
time make docker-test-mingw@fedora J=14 NETWORK=1
=== TEST SCRIPT END ===
./tests/docker/docker.py --engine auto build qemu:fedora tests/docker/dockerfiles/fedora.docker --add-current-user
Image is up to date.
LD [email protected]
cc: fatal error: no input files
compilation terminated.
make: *** [[email protected]] Error 4
The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-mingw@fedora/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Quoted 8 lines · Johannes Berg
contrib/vhost-user-sim/main.c new file +8
-0+static int unix_sock_new (const char *unix_fn) +{ +int sock; +struct sockaddr_un un; +size_t len; + +g_assert (unix_fn); +
contrib/vhost-user-sim/main.c line 38
Quoted 10 lines
contrib/vhost-user-sim/main.c new file +10
-0+ sock =socket (AF_UNIX ,SOCK_STREAM ,0 ); +if (sock <=0 ) { +perror ("socket" ); +g_assert (0 ); +return -1 ; + } + + un.sun_family =AF_UNIX ; + (void )snprintf (un.sun_path,sizeof (un.sun_path),"%s" , unix_fn); + len =sizeof (un.sun_family) +strlen (un.sun_path);
According to unix(7):
The addrlen argument that describes the enclosing sockaddr_un structure should have a value of at least:
offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
or, more simply, addrlen can be specified as sizeof(struct sockaddr_un).
Please either increase len by 1 or use sizeof(struct sockaddr_un).
Quoted 5 lines
contrib/vhost-user-sim/net.c new file +5
-0+gboolean vu_net_client_connected (GIOChannel *src, +GIOCondition cond, +gpointer data) +{ +int lsock =g_io_channel_unix_get_fd (src);
contrib/vhost-user-sim/net.c line 229
Quoted 10 lines
contrib/vhost-user-sim/net.c new file +10
-0+int csock =accept (lsock,NULL ,NULL ); +VuNetDev *ndev; + +if (csock <0 ) { +fprintf (stderr,"Accept error %s\n" ,strerror (errno)); +return TRUE; + } + + ndev =g_new0 (VuNetDev,1 ); +if (!ndev) {
g_new0() cannot return NULL, please remove this if statement.
Quoted 5 lines
contrib/vhost-user-sim/simtime.c new file +5
-0+static int full_read (int fd,void *_buf,size_t len) +{ +unsigned char *buf = _buf; +ssize_t ret; +
contrib/vhost-user-sim/simtime.c line 73
Quoted 10 lines
contrib/vhost-user-sim/simtime.c new file +10
-0+ do { + ret = read(fd, buf, len); + if (ret > 0) { + buf += ret; + len -= ret; + } else if (ret == 0) { + return 0; + } else { + return -errno; + }
Want to loop on EINTR?
Quoted 9 lines
contrib/vhost-user-sim/simtime.c new file +9
-0+ }while (len >0 ); + +return buf - (unsigned char *)_buf; +} + +static int full_write (int fd,const void *_buf,size_t len) +{ +const unsigned char *buf = _buf; +ssize_t ret;
contrib/vhost-user-sim/simtime.c line 92
Quoted 10 lines
contrib/vhost-user-sim/simtime.c new file +10
-0+ + do { + ret = write(fd, buf, len); + if (ret > 0) { + buf += ret; + len -= ret; + } else if (ret == 0) { + return 0; + } else { + return -errno;
EINTR?
Quoted 16 lines
contrib/vhost-user-sim/simtime.c new file +16
-0+ } + }while (len >0 ); + +return buf - (const unsigned char *)_buf; +} + +static void simtime_handle_message (SimTimeConnection *conn, +struct um_timetravel_msg *msg) +{ +struct um_timetravel_msg resp = { + .op =UM_TIMETRAVEL_ACK , + }; + +DPRINT (" %d | message %s (%lld, time=%lld)\n" , + conn->idx,simtime_op_str (msg->op), msg->op, msg->time); +
contrib/vhost-user-sim/simtime.c line 118
Quoted 10 lines
contrib/vhost-user-sim/simtime.c new file +10
-0+switch (msg->op) { +case UM_TIMETRAVEL_REQUEST : +if (calendar_entry_remove (&conn->entry)) { + conn->entry.time = conn->offset + msg->time; +calendar_entry_add (&conn->entry); +DPRINT (" %d | calendar entry added for %lld\n" , conn->idx, msg->time); + }else { + conn->entry.time = conn->offset + msg->time; +DPRINT (" %d | calendar entry time updated for %lld\n" , conn->idx, msg->time); + }
Just checking the expected semantics:
If the entry was already added, then we update the time and it stays scheduled. If the entry was not already added then we just stash away the time but don't schedule it?
Also, are the DPRINT() messages swapped in the if ... else ... bodies? They seem to be talking about the other case.
contrib/vhost-user-sim/simtime.c line 241
Quoted 4 lines
contrib/vhost-user-sim/simtime.c new file +4
-0+ conn =g_new0 (SimTimeConnection,1 ); +if (!conn) { +return TRUE; + }
g_new0() does not fail. If it did, then csock would be leaked here. Please drop the if statement.
include/standard-headers/linux/um_timetravel.h
Quoted 6 lines
include/standard-headers/linux/um_timetravel.h new file +0
-0@@ -0,0 +1,107 @@
Please use scripts/update-linux-headers.sh to import this header file with the necessary conversions (e.g. #include <linux/types.h> -> #include "standard-headers/linux/types.h", __u64 -> uint64_t).
Quoted 11 lines · Stefan Hajnoczi
According to unix(7):
The addrlen argument that describes the enclosing
sockaddr_unstructure should have a value of at least:
offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1or, more simply, addrlen can be specified as
sizeof(struct sockaddr_un).Please either increase len by 1 or use
sizeof(struct sockaddr_un).
Good catch, I actually realized this later as I was porting it elsewhere but forgot to update this code.
Quoted 1 line
g_new0()cannot return NULL, please remove this if statement.
:-)
Shows I don't work with glib much ...
Quoted 9 lines
Quoted 9 lines
contrib/vhost-user-sim/simtime.c new file +9
-0+static int full_read (int fd,void *_buf,size_t len) +{ +unsigned char *buf = _buf; +ssize_t ret; + + do { + ret = read(fd, buf, len); + if (ret > 0) { + buf += ret;
Quoted 8 lines
contrib/vhost-user-sim/simtime.c line 73
Quoted 6 lines
contrib/vhost-user-sim/simtime.c new file +6
-0+ len -= ret; + }else if (ret ==0 ) { +return 0 ; + }else { +return -errno; + }Want to loop on EINTR?
Can we even get EINTR? We're not really expecting to deal with any signals in this code, do we?
But I guess we can.
Quoted 11 lines
contrib/vhost-user-sim/simtime.c line 118
Quoted 10 lines
contrib/vhost-user-sim/simtime.c new file +10
-0+switch (msg->op) { +case UM_TIMETRAVEL_REQUEST : +if (calendar_entry_remove (&conn->entry)) { + conn->entry.time = conn->offset + msg->time; +calendar_entry_add (&conn->entry); +DPRINT (" %d | calendar entry added for %lld\n" , conn->idx, msg->time); + }else { + conn->entry.time = conn->offset + msg->time; +DPRINT (" %d | calendar entry time updated for %lld\n" , conn->idx, msg->time); + }
Quoted 5 lines
Just checking the expected semantics:
If the entry was already added, then we update the time and it stays scheduled. If the entry was not already added then we just stash away the time but don't schedule it?
Right.
The first case is the "normally running" case, where the request is coming in after UM_TIMETRAVEL_WAIT but before we sent it UM_TIMETRAVEL_RUN, i.e. when the sender of the message got an interrupt from elsewhere and needs to change the next runtime.
The second case is when it's requesting before it went into UM_TIMETRAVEL_WAIT, in which case we just want the entry to the calendar to be added for when we actually get _WAIT.
Quoted 2 lines
Also, are the
DPRINT()messages swapped in the if ... else ... bodies? They seem to be talking about the other case.
Or better rephrased I guess :)
Quoted 10 lines
include/standard-headers/linux/um_timetravel.h
Quoted 6 lines
include/standard-headers/linux/um_timetravel.h new file +0
-0@@ -0,0 +1,107 @@Please use
scripts/update-linux-headers.shto import this header file with the necessary conversions (e.g. #include <linux/types.h> -> #include "standard-headers/linux/types.h",__u64->uint64_t).
Hah, sure, wasn't aware of that.
Note, I'm not happy with this code at all, it deadlocks all the time. Unfortunately I haven't really been able to figure out how to make glib do what I wanted.
The first issue is that sometimes glib actually seems to reports an FD as readable when it's not, so I even put them into non-blocking mode :(
The second is that I can't seem to understand how to do recursive mainloops.
To really do this *well*, it should remain a single-threaded application, but would need a hook like
run_mainloop_until_fd_readable(vdev->call_fd)
inside the libvhost-user.c code, and that's a bit ugly ... if I even could figure out how to implement that in glib.
johannes
Quoted 7 lines · Johannes Berg
On Mon, 2019-09-23 at 10:25 +0100, Stefan Hajnoczi wrote:
Note, I'm not happy with this code at all, it deadlocks all the time. Unfortunately I haven't really been able to figure out how to make glib do what I wanted.The first issue is that sometimes glib actually seems to reports an FD as readable when it's not, so I even put them into non-blocking mode :(
Strange. Spurious wakeups are possible in general. I think when using fd monitoring (select(), poll(), etc) the fds should be in non-blocking mode.
But if you're seeing this often it makes me wonder if something else is unintentionally reading available bytes...
Quoted 10 lines
The second is that I can't seem to understand how to do recursive mainloops.
To really do this *well*, it should remain a single-threaded application, but would need a hook like
run_mainloop_until_fd_readable(vdev->call_fd)inside the
libvhost-user.ccode, and that's a bit ugly ... if I even could figure out how to implement that in glib.
Recursive mainloops are tricky since usually event loop code isn't written to be re-entrant. It opens up a whole new dimension that existing code usually wasn't designed for. In this case you are writing the code from scratch so maybe you can get it to work, but it makes me wonder why the recursive mainloop is necessary.
Stefan
Quoted 6 lines · Stefan Hajnoczi
Quoted 2 lines
The first issue is that sometimes glib actually seems to reports an FD as readable when it's not, so I even put them into non-blocking mode :(
Strange. Spurious wakeups are possible in general. I think when using fd monitoring (
select(),poll(), etc) the fds should be in non-blocking mode.
Are they? I reimplemented all this on top of my (internal) testing framework and use blocking FDs there, and had no issues whatsoever.
Quoted 2 lines
But if you're seeing this often it makes me wonder if something else is unintentionally reading available bytes...
I have no idea, it'd be strange though, what would even get to the FD?
Quoted 8 lines
Quoted 8 lines
The second is that I can't seem to understand how to do recursive mainloops.
To really do this *well*, it should remain a single-threaded application, but would need a hook like
run_mainloop_until_fd_readable(vdev->call_fd)
Quoted 8 lines
Quoted 2 lines
inside the
libvhost-user.ccode, and that's a bit ugly ... if I even could figure out how to implement that in glib.Recursive mainloops are tricky since usually event loop code isn't written to be re-entrant. It opens up a whole new dimension that existing code usually wasn't designed for. In this case you are writing the code from scratch so maybe you can get it to work, but it makes me wonder why the recursive mainloop is necessary.
It's needed to handle different clients / connections.
For example, consider this flow
VM net device & simtime
<-------------- VHOST_USER_SLAVE_CALL_VRING --------------> request for time handling <-------------- response for time handling --------------> REPLY_ACK for VHOST_USER_SLAVE_CALL_VRING
I sort of solved it with different threads for the different entities (netdev & simtime) but that also just causes deadlocks with the locking that needs to happen.
Like I said, I had reimplemented this in the meantime with a (hand- written) recursive mainloop as a single thread, and it works way better. One of these days I need to sit down and port that to be independent of my testing framework, so I can show the code ...
johannes