Skip to contentSkip to conversation

libxl: call hotplug scripts from libxl

PATCH Superseded Roger Pau Monne 5 participants 41 messages Apr 20, 2012 – May 11, 2012 1 message not in the archive Arm/actions semi architecture

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover libxl: call hotplug scripts from libxl

This series removes the use of udev rules to call hotplug scripts when using libxl. Scripts are directly called from the toolstack at the necessary points, making use of the new event library and it's fork support.

Fixed Ian Campbell reviews and added support for named vifs using a custom constant as prefix (to be merged/mixed with Ian Campbell named interfaces patch). This series should be applied after Ian Jackson event fork and Ian Campbell support for named vifs.

#1 lore raw

PATCH v3 1/5 libxl: allow libxl__exec to take a parameter containing the env variables

Add another parameter to libxl__exec call that contains the environment variables to use when performing the execvp call.

---
 tools/libxl/libxl.c            |    2 +-
 tools/libxl/libxl_bootloader.c |    4 ++--
 tools/libxl/libxl_dm.c         |    2 +-
 tools/libxl/libxl_exec.c       |    7 ++++++-
 tools/libxl/libxl_internal.h   |   13 ++++++++++++-
 5 files changed, 22 insertions(+), 6 deletions(-)
tools/libxl/libxl.c +1 -1
index 42ac89f..f0372d0 100644
@@ -1253,7 +1253,7 @@ int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
         args[2] = "-autopass";
     }
 
-    libxl__exec(autopass_fd, -1, -1, args[0], args);
+    libxl__exec(autopass_fd, -1, -1, args[0], args, NULL);
     abort();
 
  x_fail:
tools/libxl/libxl_bootloader.c +2 -2
index 2774062..7120dad 100644
@@ -113,12 +113,12 @@ static int open_xenconsoled_pty(int *master, int *slave, char *slave_path, size_
 static pid_t fork_exec_bootloader(int *master, const char *arg0, char **args)
 {
     struct termios termattr;
+    char *env[] = {"TERM", "vt100", NULL};
     pid_t pid = forkpty(master, NULL, NULL, NULL);
     if (pid == -1)
         return -1;
     else if (pid == 0) {
-        setenv("TERM", "vt100", 1);
-        libxl__exec(-1, -1, -1, arg0, args);
+        libxl__exec(-1, -1, -1, arg0, args, env);
         return -1;
     }
 
tools/libxl/libxl_dm.c +1 -1
index e25a767..15e24b9 100644
@@ -994,7 +994,7 @@ retry_transaction:
         goto out_close;
     if (!rc) { /* inner child */
         setsid();
-        libxl__exec(null, logfile_w, logfile_w, dm, args);
+        libxl__exec(null, logfile_w, logfile_w, dm, args, NULL);
     }
 
     rc = 0;
tools/libxl/libxl_exec.c +6 -1
index b10e79f..be9e407 100644
@@ -72,7 +72,7 @@ static void check_open_fds(const char *what)
 }
 
 void libxl__exec(int stdinfd, int stdoutfd, int stderrfd, const char *arg0,
-                char **args)
+                char **args, char **env)
      /* call this in the child */
 {
     if (stdinfd != -1)
@@ -95,6 +95,11 @@ void libxl__exec(int stdinfd, int stdoutfd, int stderrfd, const char *arg0,
     /* in case our caller set it to IGN.  subprocesses are entitled
      * to assume they got DFL. */
 
+    if (env != NULL) {
+        for (int i = 0; env[i] != NULL && env[i+1] != NULL; i += 2) {
+            setenv(env[i], env[i+1], 1);
+        }
+    }
     execvp(arg0, args);
 
     fprintf(stderr, "libxl: cannot execute %s: %s\n", arg0, strerror(errno));
tools/libxl/libxl_internal.h +12 -1
index d524945..9e15f95 100644
@@ -952,8 +952,19 @@ _hidden int libxl__spawn_check(libxl__gc *gc,
 
  /* low-level stuff, for synchronous subprocesses etc. */
 
+/*
+ * env should be passed using the following format,
+ *
+ * env[0]: name of env variable
+ * env[1]: value of env variable
+ *
+ * So it efectively becomes something like:
+ * export env[0]=env[1]
+ *
+ * The last entry of the array always has to be NULL.
+ */
 _hidden void libxl__exec(int stdinfd, int stdoutfd, int stderrfd,
-               const char *arg0, char **args); // logs errors, never returns
+               const char *arg0, char **args, char **env); // logs errors, never returns
 
 /* from xl_create */
 _hidden int libxl__domain_make(libxl__gc *gc,
Signature · 1 line
1.7.7.5 (Apple Git-26)
#2 lore raw

Re: [PATCH v3 1/5] libxl: allow libxl__exec to take a parameter containing the env variables

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 1/5] libxl: allow libxl__exec to take a parameter containing the env variables"):

Quoted 2 lines

Add another parameter to libxl__exec call that contains the environment variables to use when performing the execvp call.

This looks OK to me. However, shouldn't this be const char** ?

Quoted 2 lines
     struct termios termattr;

+ char *env[] = {"TERM", "vt100", NULL};

We don't compile with -Wwrite-strings but if we did this would trigger. Also it needs spaces inside { } I think.

tools/libxl/libxl_exec.c void libxl__exec(int stdinfd, int stdoutfd, int stderrfd, const char *arg0, · line 100

Quoted 3 lines
tools/libxl/libxl_exec.c +3 -0
+    if (env != NULL) {
+        for (int i = 0; env[i] != NULL && env[i+1] != NULL; i += 2) {
+            setenv(env[i], env[i+1], 1);

setenv can fail. If it does you should probably check errno and probably call libxl__alloc_failed.

Quoted 5 lines

+/*
+ * env should be passed using the following format,
+ *
+ * env[0]: name of env variable
+ * env[1]: value of env variable

You need to mention that it may have more than one setting!

Ian.

#3 lore raw

PATCH v3 2/5 libxl: add libxl__xs_path_cleanup

In reply to Roger Pau Monne

Add a function which behaves like "xenstore-rm -t", and which will be used to clean xenstore after unplug since we will be no longer executing xen-hotplug-cleanup script, that used to do that for us.

Changes since v2:

* Moved the function to libxl_xshelp.c and added the prototype to libxl_internal.h.

---
 tools/libxl/libxl_device.c   |    1 -
 tools/libxl/libxl_internal.h |    6 ++++++
 tools/libxl/libxl_xshelp.c   |   22 ++++++++++++++++++++++
 3 files changed, 28 insertions(+), 1 deletions(-)
tools/libxl/libxl_device.c +0 -1
index c7e057d..36d58cd 100644
@@ -356,7 +356,6 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,
     return -1;
 }
 
-
 typedef struct {
     libxl__ao *ao;
     libxl__ev_devstate ds;
tools/libxl/libxl_internal.h +6 -0
index 9e15f95..020810a 100644
@@ -458,6 +458,12 @@ _hidden bool libxl__xs_mkdir(libxl__gc *gc, xs_transaction_t t,
 
 _hidden char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid);
 
+/*
+ * Perfrom recursive cleanup of xenstore path, from top to bottom
+ * just like xenstore-rm -t
+ */
+_hidden int libxl__xs_path_cleanup(libxl__gc *gc, char *path);
+
 
 /*
  * Event generation functions provided by the libxl event core to the
tools/libxl/libxl_xshelp.c +22 -0
index 3ea8d08..0b1b844 100644
@@ -135,6 +135,28 @@ char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
     return s;
 }
 
+int libxl__xs_path_cleanup(libxl__gc *gc, char *path)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    unsigned int nb = 0;
+    char *last;
+
+    if (!path)
+        return 0;
+
+    xs_rm(ctx->xsh, XBT_NULL, path);
+
+    for (last = strrchr(path, '/'); last != NULL; last = strrchr(path, '/')) {
+        *last = '\0';
+        if (!libxl__xs_directory(gc, XBT_NULL, path, &nb))
+            continue;
+        if (nb == 0) {
+            xs_rm(ctx->xsh, XBT_NULL, path);
+        }
+    }
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
Signature · 1 line
1.7.7.5 (Apple Git-26)
#4 lore raw

Re: [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup"):

Quoted 3 lines

Add a function which behaves like "xenstore-rm -t", and which will be used to clean xenstore after unplug since we will be no longer executing xen-hotplug-cleanup script, that used to do that for us.

This is all rather odd. I hadn't previously noticed the existence of xenstore-rm -t.

With the C xenstored, the RM command will delete a whole directory tree, regardless of its contents. This is documented in docs/misc/xenstore.txt.

The comment in xs.c near xs_rm, which says that directories must be empty, seems to be wrong.

What does oxenstored do ? I'm tempted to say that it should follow the C xenstored behaviour.

Ian.

#5 lore raw

Re: [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup

Quoted 11 lines · Ian Jackson

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup"):

Quoted 3 lines

Add a function which behaves like "xenstore-rm -t", and which will be used to clean xenstore after unplug since we will be no longer executing xen-hotplug-cleanup script, that used to do that for us.

This is all rather odd. I hadn't previously noticed the existence of xenstore-rm -t.

With the C xenstored, the RM command will delete a whole directory tree, regardless of its contents. This is documented in docs/misc/xenstore.txt.

This is a recursive delete, from top to bottom, let me put an example which will make this clear, since probably the title is wrong. Imagine you have the following xenstore entry:

/foo/bar/baz = 123

If you do a:

xenstore-rm /foo/bar/baz

the following will remain in xenstore:

/foo/bar

What this function does is clean empty folders that contained the deleted entry, so using this function on /foo/bar/baz would have cleaned the whole directory.

Quoted 7 lines

The comment in xs.c near xs_rm, which says that directories must be empty, seems to be wrong.

What does oxenstored do ? I'm tempted to say that it should follow the C xenstored behaviour.

Ian.

#6 lore raw

Re: [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup

Roger Pau Monne writes ("Re: [Xen-devel] [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup"):

Quoted 15 lines
Quoted 3 lines · Ian Jackson

With the C xenstored, the RM command will delete a whole directory tree, regardless of its contents. This is documented in docs/misc/xenstore.txt.

This is a recursive delete, from top to bottom, let me put an example which will make this clear, since probably the title is wrong. Imagine you have the following xenstore entry:

/foo/bar/baz = 123

If you do a:

xenstore-rm /foo/bar/baz

Quoted 7 lines

the following will remain in xenstore:

/foo/bar

What this function does is clean empty folders that contained the deleted entry, so using this function on /foo/bar/baz would have cleaned the whole directory.

Oh! This was quite unclear to me and I didn't read your code closely enough to spot this. I'll go back and read your patch again.

Ian.

#7 lore raw

Re: [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup

In reply to Roger Pau Monne

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup"):

tools/libxl/libxl_device.c int libxl__device_disk_dev_number(const char *virtpath, int *pdisk, · line 359

Quoted 9 lines
tools/libxl/libxl_device.c +0 -1
index c7e057d..36d58cd 100644
@@ -356,7 +356,6 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,
     return -1;
 }
 
-

Unrelated whitespace change.

tools/libxl/libxl_device.c int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,

Quoted 5 lines
tools/libxl/libxl_device.c +5 -0
+/*
+ * Perfrom recursive cleanup of xenstore path, from top to bottom
+ * just like xenstore-rm -t
+ */
+_hidden int libxl__xs_path_cleanup(libxl__gc *gc, char *path);

I think, following my confusion, that this needs some better documentation comment.

Quoted 10 lines
tools/libxl/libxl_xshelp.c +2 -0
index 3ea8d08..0b1b844 100644
@@ -135,6 +135,28 @@ char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
     return s;
 }
 
+int libxl__xs_path_cleanup(libxl__gc *gc, char *path)
+{

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 149

Quoted 10 lines
tools/libxl/libxl_xshelp.c +10 -0
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    unsigned int nb = 0;
+    char *last;
+
+    if (!path)
+        return 0;
+
+    xs_rm(ctx->xsh, XBT_NULL, path);
+
+    for (last = strrchr(path, '/'); last != NULL; last = strrchr(path, '/')) {

If the path is relative, this won't work correctly.

Also this whole thing needs to take place in a transaction, or it is racy. Probably a transaction supplied by the caller, in which case you should assert it.

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 152

Quoted 3 lines
tools/libxl/libxl_xshelp.c +3 -0
+        *last = '\0';
+        if (!libxl__xs_directory(gc, XBT_NULL, path, &nb))
+            continue;

If this fails, it should be a fatal error; we should not just blunder on.

Ian.

#8 lore raw

Re: [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup

Quoted 9 lines · Ian Jackson

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup"):

Quoted 8 lines
tools/libxl/libxl_device.c +0 -0
index c7e057d..36d58cd 100644
@@ -356,7 +356,6 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,
      return -1;
  }
 
Quoted 3 lines

tools/libxl/libxl_device.c int libxl__device_disk_dev_number(const char *virtpath, int *pdisk, · line 359

Quoted 1 line
tools/libxl/libxl_device.c +0 -1
-

Unrelated whitespace change.

Done.

Quoted 8 lines

tools/libxl/libxl_device.c int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,

Quoted 5 lines
tools/libxl/libxl_device.c +5 -0
+/*
+ * Perfrom recursive cleanup of xenstore path, from top to bottom
+ * just like xenstore-rm -t
+ */
+_hidden int libxl__xs_path_cleanup(libxl__gc *gc, char *path);

I think, following my confusion, that this needs some better documentation comment.

Ok, I've tried to change to something more self-explaining.

Quoted 8 lines
Quoted 8 lines
tools/libxl/libxl_xshelp.c +0 -0
index 3ea8d08..0b1b844 100644
@@ -135,6 +135,28 @@ char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
      return s;
  }
 
Quoted 14 lines

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 149

Quoted 12 lines
tools/libxl/libxl_xshelp.c +12 -0
+int libxl__xs_path_cleanup(libxl__gc *gc, char *path)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    unsigned int nb = 0;
+    char *last;
+
+    if (!path)
+        return 0;
+
+    xs_rm(ctx->xsh, XBT_NULL, path);
+
+    for (last = strrchr(path, '/'); last != NULL; last = strrchr(path, '/')) {

If the path is relative, this won't work correctly.

Are you sure? From what I've tested it seems to work ok.

Quoted 4 lines

Also this whole thing needs to take place in a transaction, or it is racy. Probably a transaction supplied by the caller, in which case you should assert it.

Ok, will add another lock.

Quoted 8 lines

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 152

Quoted 3 lines
tools/libxl/libxl_xshelp.c +3 -0
+        *last = '\0';
+        if (!libxl__xs_directory(gc, XBT_NULL, path,&nb))
+            continue;

If this fails, it should be a fatal error; we should not just blunder on.

Ian.

#9 lore raw

Re: [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup

In reply to Ian Jackson
Quoted 45 lines · Ian Jackson

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 2/5] libxl: add libxl__xs_path_cleanup"):

tools/libxl/libxl_device.c int libxl__device_disk_dev_number(const char *virtpath, int *pdisk, · line 359

Quoted 9 lines
tools/libxl/libxl_device.c +0 -1
index c7e057d..36d58cd 100644
@@ -356,7 +356,6 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,
      return -1;
  }
 
-

Unrelated whitespace change.

tools/libxl/libxl_device.c int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,

Quoted 5 lines
tools/libxl/libxl_device.c +5 -0
+/*
+ * Perfrom recursive cleanup of xenstore path, from top to bottom
+ * just like xenstore-rm -t
+ */
+_hidden int libxl__xs_path_cleanup(libxl__gc *gc, char *path);

I think, following my confusion, that this needs some better documentation comment.

Quoted 10 lines
tools/libxl/libxl_xshelp.c +2 -0
index 3ea8d08..0b1b844 100644
@@ -135,6 +135,28 @@ char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
      return s;
  }
 
+int libxl__xs_path_cleanup(libxl__gc *gc, char *path)
+{

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 149

Quoted 10 lines
tools/libxl/libxl_xshelp.c +10 -0
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    unsigned int nb = 0;
+    char *last;
+
+    if (!path)
+        return 0;
+
+    xs_rm(ctx->xsh, XBT_NULL, path);
+
+    for (last = strrchr(path, '/'); last != NULL; last = strrchr(path, '/')) {

If the path is relative, this won't work correctly.

Quoted 3 lines

Also this whole thing needs to take place in a transaction, or it is racy. Probably a transaction supplied by the caller, in which case you should assert it.

This cannot be done inside of a transaction, because we cannot check that the directory is empty if the remove has not actually taken place, and checking that there are zero or one elements (the one we 'had' removed) can lead to unexpected results, as someone might be deleting elements on our back and we might actually delete a directory that still has valid entries.

Quoted 9 lines

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 152

Quoted 4 lines
tools/libxl/libxl_xshelp.c +3 -0
+        *last = '\0';
+        if (!libxl__xs_directory(gc, XBT_NULL, path,&nb))
+            continue;

If this fails, it should be a fatal error; we should not just blunder on.

Ian.

#10 lore raw

PATCH v3 3/5 libxl: call hotplug scripts from libxl for vbd

In reply to Roger Pau Monne

This is a rather big change, that adds the necessary machinery to perform hotplug script calling from libxl for Linux. This patch launches the necessary scripts to attach and detach PHY and TAP backend types (Qemu doesn't use hotplug scripts).

Here's a list of the major changes introduced:

* libxl_device_disk_add makes use of the new event library and takes the optional parameter libxl_asyncop_how, to choose how the operation has to be issued (sync or async).

* libxl_device_disk_add waits for backend to switch to state 2 (XenbusInitWait) and then launches the hotplug script.

* libxl__devices_destroy no longer calls libxl__device_destroy, and instead calls libxl__initiate_device_remove, so we can disconnect the device and execute the necessary hotplug scripts instead of just deleting the backend entries. So libxl__devices_destroy now uses the event library, and so does libxl_domain_destroy.

* Since libxl__devices_destroy calls multiple times libxl__initiate_device_remove, this function now returns a different value regarding the actions taken (if an event was added or not). The user has to call libxl__ao_complete after using this function if necessary.

* The internal API for hotplug scripts has been added, which consists of one function; libxl__device_hotplug that takes the device and the action to execute.

* Linux hotplug scripts are called by setting the necessary env variables to emulate udev rules, so there's no need to change them (although a rework to pass this as parameters insted of env variables would be suitable, so both NetBSD and Linux hotplug scripts take the same parameters).

* Added a check in xen-hotplug-common.sh, so scripts are only executed from udev when using xend. xl adds a disable_udev=y to xenstore private directory and with the modification of the udev rules every call from udev gets HOTPLUG_GATE passed, that points to disable_udev. If HOTPLUG_GATE is passed and points to a value, the hotplug script is not executed.

I've also modified the python binding for pyxl_domain_destroy, that now take the ao_how parameter, but I'm not really sure if it's done correctly, let's just say that it compiles.

Changes since v3:

* disable_udev is now stored in /libxl/backend/vbd/<domid>/<devid>/.

* Passed NULL as ao_how in Python bindings.

Changes since v1:

* Replaced the hotplug snippet that prevents execution from udev when necessary, and now we set the env var from udev rules instead of libxl.

* Replaced the libxl_device_disk_add "if" with a "switch".

* Removed libxl__xs_path_cleanup (replaced with xs_rm).

* Fixed several spelling mistakes.

---
 tools/hotplug/Linux/xen-backend.rules     |    6 +-
 tools/hotplug/Linux/xen-hotplug-common.sh |    6 +
 tools/libxl/Makefile                      |    3 +-
 tools/libxl/libxl.c                       |  144 ++++++++++++++++++++++-----
 tools/libxl/libxl.h                       |    7 +-
 tools/libxl/libxl_create.c                |    4 +-
 tools/libxl/libxl_device.c                |  152 ++++++++++++++++++++++++++---
 tools/libxl/libxl_dm.c                    |    4 +-
 tools/libxl/libxl_hotplug.c               |   67 +++++++++++++
 tools/libxl/libxl_internal.h              |   46 ++++++++-
 tools/libxl/libxl_linux.c                 |  117 ++++++++++++++++++++++
 tools/libxl/libxl_pci.c                   |    5 +-
 tools/libxl/xl_cmdimpl.c                  |   14 ++--
 tools/python/xen/lowlevel/xl/xl.c         |    2 +-
 14 files changed, 516 insertions(+), 61 deletions(-)
 create mode 100644 tools/libxl/libxl_hotplug.c
tools/hotplug/Linux/xen-backend.rules +3 -3
index 19bd0fa..19f3d27 100644
@@ -1,11 +1,11 @@
-SUBSYSTEM=="xen-backend", KERNEL=="tap*", RUN+="/etc/xen/scripts/blktap $env{ACTION}"
-SUBSYSTEM=="xen-backend", KERNEL=="vbd*", RUN+="/etc/xen/scripts/block $env{ACTION}"
+SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/blktap $env{ACTION}"
+SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/block $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vif2-*", RUN+="/etc/xen/scripts/vif2 $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif"
 SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif"
 SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}"
-SUBSYSTEM=="xen-backend", ACTION=="remove", RUN+="/etc/xen/scripts/xen-hotplug-cleanup"
+SUBSYSTEM=="xen-backend", ACTION=="remove", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/xen-hotplug-cleanup"
 KERNEL=="evtchn", NAME="xen/%k"
 SUBSYSTEM=="xen", KERNEL=="blktap[0-9]*", NAME="xen/%k", MODE="0600"
 SUBSYSTEM=="blktap2", KERNEL=="blktap[0-9]*", NAME="xen/blktap-2/%k", MODE="0600"
tools/hotplug/Linux/xen-hotplug-common.sh +6 -0
index 8f6557d..d2aac3a 100644
@@ -15,6 +15,12 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 
+# Hack to prevent the execution of hotplug scripts from udev if the domain
+# has been launched from libxl
+if [ -n "${HOTPLUG_GATE}" ] && \
+   `xenstore-read "$HOTPLUG_GATE" >/dev/null 2>&1`; then
+    exit 0
+fi
 
 dir=$(dirname "$0")
 . "$dir/hotplugpath.sh"
tools/libxl/Makefile +2 -1
index e5ea867..0ac43bd 100644
@@ -53,7 +53,8 @@ LIBXL_LIBS += -lyajl
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
 			libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
-			libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y)
+			libxl_qmp.o libxl_event.o libxl_fork.o libxl_hotplug.o \
+			$(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl) -include $(XEN_ROOT)/tools/config.h
tools/libxl/libxl.c +117 -27

268 lines not on this page · raw message

tools/libxl/libxl.h +5 -2
index edbca53..6687e2e 100644
@@ -472,7 +472,8 @@ int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
 int libxl_domain_resume(libxl_ctx *ctx, uint32_t domid);
 int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid);
 int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid);
-int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid);
+int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid,
+                         const libxl_asyncop_how *ao_how);
 int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid, libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid);
 
 /* get max. number of cpus supported by hypervisor */
@@ -601,7 +602,9 @@ void libxl_vminfo_list_free(libxl_vminfo *list, int nr);
  */
 
 /* Disks */
-int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk);
+int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid,
+                          libxl_device_disk *disk,
+                          const libxl_asyncop_how *ao_how);
 int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,
                              libxl_device_disk *disk,
                              const libxl_asyncop_how *ao_how);
tools/libxl/libxl_create.c +2 -2
index 3675afe..de598ad 100644
@@ -598,7 +598,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
     store_libxl_entry(gc, domid, &d_config->b_info);
 
     for (i = 0; i < d_config->num_disks; i++) {
-        ret = libxl_device_disk_add(ctx, domid, &d_config->disks[i]);
+        ret = libxl_device_disk_add(ctx, domid, &d_config->disks[i], 0);
         if (ret) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
                        "cannot add disk %d to domain: %d", i, ret);
@@ -721,7 +721,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
 
 error_out:
     if (domid)
-        libxl_domain_destroy(ctx, domid);
+        libxl_domain_destroy(ctx, domid, 0);
 
     return ret;
 }
tools/libxl/libxl_device.c +140 -12

258 lines not on this page · raw message

tools/libxl/libxl_dm.c +2 -2
index 15e24b9..4ce58f6 100644
@@ -789,7 +789,7 @@ retry_transaction:
             goto retry_transaction;
 
     for (i = 0; i < dm_config.num_disks; i++) {
-        ret = libxl_device_disk_add(ctx, dm_domid, &dm_config.disks[i]);
+        ret = libxl_device_disk_add(ctx, dm_domid, &dm_config.disks[i], 0);
         if (ret)
             goto out_free;
     }
@@ -1047,7 +1047,7 @@ int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid)
             goto out;
         }
         LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device model is a stubdom, domid=%d", stubdomid);
-        ret = libxl_domain_destroy(ctx, stubdomid);
+        ret = libxl_domain_destroy(ctx, stubdomid, 0);
         if (ret)
             goto out;
 
tools/libxl/libxl_hotplug.c new file +67 -0

67 lines not on this page · raw message

tools/libxl/libxl_internal.h +44 -2

75 lines not on this page · raw message

tools/libxl/libxl_linux.c +117 -0

120 lines not on this page · raw message

tools/libxl/libxl_pci.c +3 -2
index e8b8839..fd79d1d 100644
@@ -103,8 +103,9 @@ int libxl__create_pci_backend(libxl__gc *gc, uint32_t domid,
     flexarray_append_pair(front, "state", libxl__sprintf(gc, "%d", 1));
 
     libxl__device_generic_add(gc, &device,
-                              libxl__xs_kvs_of_flexarray(gc, back, back->count),
-                              libxl__xs_kvs_of_flexarray(gc, front, front->count));
+                            libxl__xs_kvs_of_flexarray(gc, back, back->count),
+                            libxl__xs_kvs_of_flexarray(gc, front, front->count),
+                            NULL);
 
 out:
     if (back)
tools/libxl/xl_cmdimpl.c +7 -7
index c9e9943..0bc3ac2 100644
@@ -1306,7 +1306,7 @@ static int handle_domain_death(libxl_ctx *ctx, uint32_t domid,
         /* fall-through */
     case LIBXL_ACTION_ON_SHUTDOWN_DESTROY:
         LOG("Domain %d needs to be cleaned up: destroying the domain", domid);
-        libxl_domain_destroy(ctx, domid);
+        libxl_domain_destroy(ctx, domid, 0);
         break;
 
     case LIBXL_ACTION_ON_SHUTDOWN_COREDUMP_DESTROY:
@@ -1867,7 +1867,7 @@ start:
 error_out:
     release_lock();
     if (libxl_domid_valid_guest(domid))
-        libxl_domain_destroy(ctx, domid);
+        libxl_domain_destroy(ctx, domid, 0);
 
 out:
     if (logfile != 2)
@@ -2354,7 +2354,7 @@ static void destroy_domain(const char *p)
         fprintf(stderr, "Cannot destroy privileged domain 0.\n\n");
         exit(-1);
     }
-    rc = libxl_domain_destroy(ctx, domid);
+    rc = libxl_domain_destroy(ctx, domid, 0);
     if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n",rc); exit(-1); }
 }
 
@@ -2628,7 +2628,7 @@ static int save_domain(const char *p, const char *filename, int checkpoint,
     if (checkpoint)
         libxl_domain_unpause(ctx, domid);
     else
-        libxl_domain_destroy(ctx, domid);
+        libxl_domain_destroy(ctx, domid, 0);
 
     exit(0);
 }
@@ -2861,7 +2861,7 @@ static void migrate_domain(const char *domain_spec, const char *rune,
     }
 
     fprintf(stderr, "migration sender: Target reports successful startup.\n");
-    libxl_domain_destroy(ctx, domid); /* bang! */
+    libxl_domain_destroy(ctx, domid, 0); /* bang! */
     fprintf(stderr, "Migration successful.\n");
     exit(0);
 
@@ -2979,7 +2979,7 @@ static void migrate_receive(int debug, int daemonize, int monitor)
     if (rc) {
         fprintf(stderr, "migration target: Failure, destroying our copy.\n");
 
-        rc2 = libxl_domain_destroy(ctx, domid);
+        rc2 = libxl_domain_destroy(ctx, domid, 0);
         if (rc2) {
             fprintf(stderr, "migration target: Failed to destroy our copy"
                     " (code %d).\n", rc2);
@@ -5026,7 +5026,7 @@ int main_blockattach(int argc, char **argv)
         return 0;
     }
 
-    if (libxl_device_disk_add(ctx, fe_domid, &disk)) {
+    if (libxl_device_disk_add(ctx, fe_domid, &disk, 0)) {
         fprintf(stderr, "libxl_device_disk_add failed.\n");
     }
     return 0;
tools/python/xen/lowlevel/xl/xl.c +1 -1
index c4f7c52..ce7a2b2 100644
@@ -447,7 +447,7 @@ static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args)
     int domid;
     if ( !PyArg_ParseTuple(args, "i", &domid) )
         return NULL;
-    if ( libxl_domain_destroy(self->ctx, domid) ) {
+    if ( libxl_domain_destroy(self->ctx, domid, NULL) ) {
         PyErr_SetString(xl_error_obj, "cannot destroy domain");
         return NULL;
     }
Signature · 1 line
1.7.7.5 (Apple Git-26)
#11 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Thanks for this mammoth patch. My comments are below.

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 6 lines

This is a rather big change, that adds the necessary machinery to perform hotplug script calling from libxl for Linux. This patch launches the necessary scripts to attach and detach PHY and TAP backend types (Qemu doesn't use hotplug scripts).

Here's a list of the major changes introduced:

Can you please wrap your commit messages to no more than 75 characters ? Some vcs's indent them. And of course they get indented when we reply leading to wrap damage.

Quoted 5 lines

* libxl__devices_destroy no longer calls libxl__device_destroy, and instead calls libxl__initiate_device_remove, so we can disconnect the device and execute the necessary hotplug scripts instead of just deleting the backend entries. So libxl__devices_destroy now uses the event library, and so does libxl_domain_destroy.

So we no longer have any function which will synchronously and immediately destroy a domain and throw away everything to do with it. Is it actually the case that you think such a function cannot be provided ?

Quoted 5 lines

* Added a check in xen-hotplug-common.sh, so scripts are only executed from udev when using xend. xl adds a disable_udev=y to xenstore private directory and with the modification of the udev rules every call from udev gets HOTPLUG_GATE passed, that points to disable_udev. If HOTPLUG_GATE is passed and points to a value, the hotplug script is not executed.

WDYM "points to a value"? Did you just mean "is set to a nonempty value" ? I'm not convinced by the name of this variable. Shouldn't it have Xen in the name, and specify its own sense ? Eg, XEN_HOTPLUG_DISABLE or something ?

Quoted 1 line

+SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/blktap $env{ACTION}"

...

tools/hotplug/Linux/xen-hotplug-common.sh line 23

Quoted 6 lines
tools/hotplug/Linux/xen-hotplug-common.sh +6 -0
+# Hack to prevent the execution of hotplug scripts from udev if the domain
+# has been launched from libxl
+if [ -n "${HOTPLUG_GATE}" ] && \
+   `xenstore-read "$HOTPLUG_GATE" >/dev/null 2>&1`; then
+    exit 0
+fi

Oh I see. Hmm. Why should this string not be fixed ? I'm not sure I like the idea of being able to pass some random other xenstore path.

Also: this xenstore path should be a relative path, ie one relative to the xenstore home of domain running this part of the tools. That way the scripts can be easily and automatically disabled for dom0 and enabled in driver domains.

tools/libxl/libxl.c int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis · line 1443

Quoted 3 lines
tools/libxl/libxl.c +3 -0
+    /* compatibility addon to keep udev rules */
+    flexarray_append(private, "disable_udev");
+    flexarray_append(private, "y");

We would normally use "1" for true in xenstore.

Quoted 8 lines

libxl__device_generic_add(gc, &device,

tools/libxl/libxl.c +5 -2
-                             libxl__xs_kvs_of_flexarray(gc, back, back->count),
-                             libxl__xs_kvs_of_flexarray(gc, front, front->count));
+                    libxl__xs_kvs_of_flexarray(gc, back, back->count),
+                    libxl__xs_kvs_of_flexarray(gc, front, front->count),
+                    libxl__xs_kvs_of_flexarray(gc, private, private->count));
+
+    switch(disk->backend) {

tools/libxl/libxl.c int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis · line 1460

Quoted 10 lines
tools/libxl/libxl.c +10 -0
+    case LIBXL_DISK_BACKEND_PHY:
+    case LIBXL_DISK_BACKEND_TAP:
+        rc = libxl__initiate_device_add(egc, ao, &device);
+        if (rc) goto out_free;
+        break;
+    case LIBXL_DISK_BACKEND_QDISK:
+    default:
+        libxl__ao_complete(egc, ao, 0);
+        break;
+    }

Does this really need no confirmation from qemu ?

tools/libxl/libxl.c int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis · line 1469

Quoted 6 lines

out_free:

tools/libxl/libxl.c +3 -0
+    flexarray_free(private);
+out_free_b:
     flexarray_free(back);
+out_free_f:
     flexarray_free(front);

It would be much better to allocate these from the gc somehow. Failing that, perhaps we could initialise them to NULL and free them iff necessary on the exit path:

out:

      ...
      if (back)  flexarray_free(back);
      if (front) flexarray_free(front);

etc.

tools/libxl/libxl.c int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid, · line 1489

Quoted 9 lines

int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,

tools/libxl/libxl.c +3 -1
@@ -1442,7 +1484,18 @@ int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,
     if (rc != 0) goto out;
 
     rc = libxl__initiate_device_remove(egc, ao, &device);
-    if (rc) goto out;
+    switch(rc) {
+    case 1:
+        /* event added */

I think this terminology "event added" is confusingly wrong. What you mean is "event queued", I think. You should change this everywhere. But before you do that, please see what I write below about your approach to libxl__initiate_device_remove.

tools/libxl/libxl.c int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk) · line 1722

Quoted 6 lines
tools/libxl/libxl.c +1 -1
@@ -1666,11 +1719,11 @@ int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
     ret = 0;
 
     libxl_device_disk_remove(ctx, domid, disks + i, 0);
-    libxl_device_disk_add(ctx, domid, disk);
+    libxl_device_disk_add(ctx, domid, disk, 0);

This needs a /* fixme-ao */ because inside-libxl callers are not allowed to invent an ao_how*, even NULL. You need to mark every occurrence of these that way.

tools/libxl/libxl.c int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic) · line 1942

Quoted 9 lines
tools/libxl/libxl.c +3 -2
@@ -1884,8 +1937,9 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
     flexarray_append(front, libxl__sprintf(gc,
                                     LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
     libxl__device_generic_add(gc, &device,
-                             libxl__xs_kvs_of_flexarray(gc, back, back->count),
-                             libxl__xs_kvs_of_flexarray(gc, front, front->count));
+                            libxl__xs_kvs_of_flexarray(gc, back, back->count),
+                            libxl__xs_kvs_of_flexarray(gc, front, front->count),
+                            NULL);

Likewise. Also unintentional indentation change ? (In several more places, too.)

tools/libxl/libxl_create.c static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config, · line 601

Quoted 10 lines
tools/libxl/libxl_create.c +1 -1
index 3675afe..de598ad 100644
@@ -598,7 +598,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
     store_libxl_entry(gc, domid, &d_config->b_info);
 
     for (i = 0; i < d_config->num_disks; i++) {
-        ret = libxl_device_disk_add(ctx, domid, &d_config->disks[i]);
+        ret = libxl_device_disk_add(ctx, domid, &d_config->disks[i], 0);

Here in xl simply passing 0 is correct.

tools/libxl/libxl_create.c static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,

Quoted 6 lines
tools/libxl/libxl_create.c +6 -0
+char *libxl__device_private_path(libxl__gc *gc, libxl__device *device)
+{
+    return libxl__sprintf(gc, "/libxl/backend/%s/%u/%d",
+                          libxl__device_kind_to_string(device->backend_kind),
+                          device->domid, device->devid);
+}

Did you want to use GCSPRINTF ?

Quoted 1 line

+ rc = libxl__device_hotplug(gc, aorm->dev, DISCONNECT);

This is not correct. You need to do something more with the exit status of the hotplug script than simply throwing it away as you do in hotplug_exited. libxl__device_hotplug needs to take a callback from the caller so that it can notify the caller when the hotplug script has exited.

You need to not allow the ao to complete until the hotplug script has exited. Otherwise you will enter hotplug_exited after the libxl__hotplug_state has been destroyed with the ao.

If it's too slow and you need to time out, you need to send the hotplug script a signal, and then wait for it to exit. If necessary that signal could be SIGKILL; SIGKILL can be relied on to cause the hotplug script to actually terminate and thus the hotplug_exited callback to occur.

Quoted 4 lines

+/*
+ * Return codes:
+ * 1 Success and event(s) HAVE BEEN added
+ * 0 Success and NO events were added

Quoted 8 lines

+ * < 0 Error
+ *
+ * Since this function can be called several times, and several events
+ * can be added to the same context, the user has to call
+ * libxl__ao_complete when necessary (if no events are added).
+ */
int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao,
libxl__device *dev)

This comment should be in the header file near the declaration. And indeed if you had put it there you would have discovered another comment already there describing the old behaviour, which you have now left behind as an erroneous comment.

And I think the new interface is entirely wrong. How does the caller know when to complete the ao if libxl__initiate_device_remove never calls back ?

You need to split this into two functions: one with the current interface which is a simple wrapper, used for all the existing call sites, and one which never completes any ao but which always makes a callback when the operation is complete. That callback should be used by the caller of libxl__initiate_device_remove to do whatever it needs to, which might include completing the ao.

At the moment, AFAICT from reading your code, the caller's ao will be completed by the first nontrivial device remove to complete, ie a bug.

Quoted 3 lines
tools/libxl/libxl_create.c +2 -1
-int libxl__device_destroy(libxl__gc *gc, libxl__device *dev)
+int libxl__initiate_device_add(libxl__egc *egc, libxl__ao *ao,
+                               libxl__device *dev)

...

tools/libxl/libxl_create.c

Quoted 7 lines
tools/libxl/libxl_create.c +7 -0
+    rc = libxl__ev_devstate_wait(gc, &aorm->ds, device_add_callback,
+                                 state_path, XenbusStateInitWait,
+                                 LIBXL_INIT_TIMEOUT * 1000);
+    if (rc) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable to initialize device %"
+                                          PRIu32, dev->devid);
+        libxl__ev_devstate_cancel(gc, &aorm->ds);

This cancel is not necessary, because device_add_cleanup will do this. (Or at least it should do; I haven't checked.)

tools/libxl/libxl_create.c

Quoted 10 lines
tools/libxl/libxl_create.c +10 -0
+        goto out_fail;
+    }
+
+    return 0;
+
+out_fail:
+    assert(rc);
+    device_add_cleanup(gc, aorm);
+    return rc;
+out_ok:

^ blank line needed after the return.

tools/libxl/libxl_create.c

Quoted 4 lines
tools/libxl/libxl_create.c +4 -0
+    rc = libxl__device_hotplug(gc, dev, CONNECT);
+    libxl__ao_complete(egc, ao, 0);
+    return rc;
+}

Some of my comments about initiate_device_remove's callback probably apply here too. In particular, I have found it is often better to make a callback-queueing function always call the callback right away, recursively, on the error path. The result is that the function can return void, which simplifies callers considerably.

Whichever way you do it you do need to document your choices about reentrancy.

tools/libxl/libxl_create.c

Quoted 11 lines
tools/libxl/libxl_create.c +11 -0
+static void hotplug_exited(libxl__egc *egc, libxl__ev_child *child,
+                           pid_t pid, int status)
+{
+    libxl__hotplug_state *hs = CONTAINER_OF(child, *hs, child);
+    EGC_GC;
+
+    if (status) {
+        libxl_report_child_exitstatus(CTX, hs->rc ? LIBXL__LOG_ERROR
+                                                  : LIBXL__LOG_WARNING,
+                                      "hotplug child", pid, status);
+    }

As I say, this needs to make a callback.

tools/libxl/libxl_create.c

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+int libxl__hotplug_exec(libxl__gc *gc, char *arg0, char **args, char **env)
+{

It would be better IMO to call this function "launch" or something, since it doesn't actually call exec in the parent (which is what I think a function called "exec" should do).

tools/libxl/libxl_create.c

Quoted 3 lines
tools/libxl/libxl_create.c +3 -0
+    libxl__hotplug_state *hs;
+
+    hs = libxl__zalloc(gc, sizeof(*hs));

How about
GCNEW(hs)
?

tools/libxl/libxl_create.c

Quoted 3 lines
tools/libxl/libxl_create.c +3 -0
+    if (!pid) {
+        /* child */
+        signal(SIGCHLD, SIG_DFL);

What is this for ? Is the problem that children of libxl otherwise inherit a weird SIGCHLD disposition ? If so you need to fix this in libxl__exec, probably in a separate patch - and you probably need to sort out sigprocmask too in case the libxl caller has set up something weird. This is something that ought to go in a new patch.

tools/libxl/libxl_create.c

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+    if (libxl__ev_child_inuse(&hs->child)) {
+        hs->rc = rc;

Under what circumstances will rc!=0 here ? Surely that is the success path? It might be easier simply to have "out:" be only the error path. The success path always has _inuse and the failure path never does.

tools/libxl/libxl_create.c

Quoted 5 lines
tools/libxl/libxl_create.c +5 -0
+/* Action to pass to hotplug caller functions */
+typedef enum {
+    CONNECT = 1,
+    DISCONNECT = 2
+} libxl__hotplug_action;

These names are rather too generic, I think.

tools/libxl/libxl_create.c

Quoted 8 lines
tools/libxl/libxl_create.c +8 -0
+typedef struct libxl__hotplug_state libxl__hotplug_state;
+
+struct libxl__hotplug_state {
+    /* public, result, caller may only read in callback */
+    int rc;
+    /* private for implementation */
+    libxl__ev_child child;
+};

And this is where the callback member ought to go, in the public part, with a note saying it should be set by the caller.

Is there any provision for timeout here ? Would here be a good place to do the timeout, rather than higher up the stack ?

Also you might find it better to move the args and env and so forth into the hotplug_state. That way, for example, you can actually produce a useful message when the script fails.

tools/libxl/libxl_create.c

Quoted 9 lines
tools/libxl/libxl_create.c +9 -0
+/*
+ * libxl__hotplug_exec is an internal function that should not be used
+ * directly, all hotplug script calls have to implement and use
+ * libxl__device_hotplug.
+ */
+_hidden int libxl__device_hotplug(libxl__gc *gc, libxl__device *dev,
+                                  libxl__hotplug_action action);
+_hidden int libxl__hotplug_exec(libxl__gc *gc, char *arg0, char **args,
+                                char **env);

Why does libxl__hotplug_exec need to be declared here at all then ? Could it be static in libxl_hotplug.c ?

tools/libxl/libxl_create.c

Quoted 4 lines
tools/libxl/libxl_create.c +4 -0
+/* Hotplug scripts helpers */
+static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);

Why not use CTX ? For that matter, if you use my new LOG macros you don't need to refer to CTX at all.

tools/libxl/libxl_create.c

Quoted 5 lines
tools/libxl/libxl_create.c +5 -0
+    script = libxl__xs_read(gc, XBT_NULL,
+                            libxl__sprintf(gc, "%s/%s", be_path, "script"));
+    if (!script) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable to read script from %s",
+                                          be_path);

You need to log the errno.

tools/libxl/libxl_create.c

Quoted 6 lines
tools/libxl/libxl_create.c +6 -0
+    f_env = flexarray_make(9, 1);
+    if (!f_env) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "unable to create environment array");
+        return NULL;
+    }

Isn't this an allocation failure which should be dealt with by libxl__alloc_failed ?

tools/libxl/libxl_create.c

Quoted 3 lines
tools/libxl/libxl_create.c +3 -0
+    flexarray_set(f_env, nr++, "XENBUS_TYPE");
+    flexarray_set(f_env, nr++, (char *)
+                  libxl__device_kind_to_string(dev->backend_kind));

Why is the cast needed ?

tools/libxl/libxl_create.c

Quoted 5 lines
tools/libxl/libxl_create.c +5 -0
+    flexarray_set(f_env, nr++, "XENBUS_PATH");
+    flexarray_set(f_env, nr++,
+                  libxl__sprintf(gc, "backend/%s/%u/%d",
+                  libxl__device_kind_to_string(dev->backend_kind),
+                  dev->domid, dev->devid));

GCSPRINTF might help shorten this ?

For that matter, you've called libxl__device_kind_to_string twice. Calling it only once would make this easier to read.

I won't comment any more on places where I think GCSPRINTF, LOG/LOGE/LOGEV and CTX might usefully replace what you have written.

tools/libxl/libxl_create.c

Quoted 3 lines
tools/libxl/libxl_create.c +3 -0
+    env = get_hotplug_env(gc, dev);
+    if (!env)
+        return -1;

Surely this should never fail as it just results in allocation failure ?

This function seems to be supposed to return an rc value, so return -1 is wrong.

Quoted 1 line

+ args = (char **) flexarray_contents(f_args);

Why is the cast needed ?

tools/libxl/libxl_create.c

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+    what = libxl__sprintf(gc, "%s %s", args[0],
+                          action == CONNECT ? "connect" : "disconnect");

Surely

action == CONNECT ? "connect" :
action == DISCONNECT ? "disconnect" : abort()

?

tools/libxl/libxl_create.c

Quoted 4 lines
tools/libxl/libxl_create.c +4 -0
+    rc = libxl__hotplug_exec(gc, args[0], args, env);
+    if (rc) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable execute hotplug scripts for "
+                                          "device %"PRIu32, dev->devid);

libxl__hotplug_exec ought to do all the logging. That means that all the information needed to do so should be passed to it, including the domid (which you don't currently log) and the device id. That should probably be filled in by its caller in the libxl__hotplug_state struct.

tools/python/xen/lowlevel/xl/xl.c static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args) · line 450

Quoted 10 lines
tools/python/xen/lowlevel/xl/xl.c +1 -1
index c4f7c52..ce7a2b2 100644
@@ -447,7 +447,7 @@ static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args)
     int domid;
     if ( !PyArg_ParseTuple(args, "i", &domid) )
         return NULL;
-    if ( libxl_domain_destroy(self->ctx, domid) ) {
+    if ( libxl_domain_destroy(self->ctx, domid, NULL) ) {

I think this is correct. Or, at least, we don't currently provide any asynchronous capability in the Python code and I don't mind not doing so.

Ian.

#12 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 10 lines · Ian Jackson
Quoted 5 lines

* libxl__devices_destroy no longer calls libxl__device_destroy, and instead calls libxl__initiate_device_remove, so we can disconnect the device and execute the necessary hotplug scripts instead of just deleting the backend entries. So libxl__devices_destroy now uses the event library, and so does libxl_domain_destroy.

So we no longer have any function which will synchronously and immediately destroy a domain and throw away everything to do with it. Is it actually the case that you think such a function cannot be provided ?

The difference between remove and destroy is documented in libxl.h and this patch does not change those definitions:

* libxl_device_<type>_remove(ctx, domid, device):
*

 *   Removes the given device from the specified domain by performing
 *   an orderly unplug with guest co-operation. This requires that the
 *   guest is running.
 *
 *   This method is currently synchronous and therefore can block
 *   while interacting with the guest.
 *
 * libxl_device_<type>_destroy(ctx, domid, device):
 *
 *   Removes the given device from the specified domain without guest
 *   co-operation. It is guest specific what affect this will have on
 *   a running guest.

*
* This function does not interact with the guest and therefore
* cannot block on the guest.

These descriptions must be updated to refer to the new async behaviour.

Does the new implementation of destroy meet these requirements (modulo now being asynchronous)?

The documentation does not currently mention blocking on the backend domain, could you please add a comment which describes what the new requirements are in this respect. Likewise hotplug scripts are not mentioned in these docs.

In principal I think I am happy with the possibility to block temporarily with a timeout on a backend domain but ultimately we need a timeout and a fallback to the "just kill it" mode.

What happens here in 4.3 when we split the hotplug state out according to whatever becomes of the "Driver domains communication protocol proposal" thread? At that point the hotplug script state will be separate from the backend state and we can go back to the "just nuke it" mode, can't we? Would there be a regression vs. xend for us to stick with the nuke it mode for 4.2 and fix this properly in 4.3?

I think it is important in the context of this patch to be clear about what is the desired long term behaviour compared with the short term behaviour being implemented for 4.2 is. We should also be clear about what is being done now in order to address xl regressions vs xend. We should also be clear about what is just papering over an issue for 4.2 vs what the proper fix in 4.3 will be. We also need to know what is actually new functionality or behaviour (i.e. not fixing an xl vs xend regression). IOW we need to have clear descriptions of the reasons for the changes not just what the changes.

I think all the above need to be written down explicitly in either the commit message or the introductory email, otherwise the review of this series is just going to continue to go round in circles -- the reasoning behind these changes is just too complex for a reviewer (even one who is familiar with all this stuff already) to hold in their head.

Quoted 12 lines
Quoted 1 line

+SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/blktap $env{ACTION}"

...

tools/hotplug/Linux/xen-hotplug-common.sh line 23

Quoted 6 lines
tools/hotplug/Linux/xen-hotplug-common.sh +6 -0
+# Hack to prevent the execution of hotplug scripts from udev if the domain
+# has been launched from libxl
+if [ -n "${HOTPLUG_GATE}" ] && \
+   `xenstore-read "$HOTPLUG_GATE" >/dev/null 2>&1`; then
+    exit 0
+fi

Oh I see. Hmm. Why should this string not be fixed ? I'm not sure I like the idea of being able to pass some random other xenstore path.

Quoted 4 lines

Also: this xenstore path should be a relative path, ie one relative to the xenstore home of domain running this part of the tools. That way the scripts can be easily and automatically disabled for dom0 and enabled in driver domains.

XENBUS_PATH contains elements for both the back- and frontend domains as well as the specific device.

Or do you think the key should be global per-(backend-domain rather than per-device?

Quoted 7 lines

tools/libxl/libxl_internal.h _hidden char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid); · line 479

Quoted 5 lines
tools/libxl/libxl_internal.h +5 -0
+/* Action to pass to hotplug caller functions */
+typedef enum {
+    CONNECT = 1,
+    DISCONNECT = 2
+} libxl__hotplug_action;

These names are rather too generic, I think.

enums should also be declared in lixl_types_internal.idl

Ian.

#13 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 9 lines

I think it is important in the context of this patch to be clear about what is the desired long term behaviour compared with the short term behaviour being implemented for 4.2 is. We should also be clear about what is being done now in order to address xl regressions vs xend. We should also be clear about what is just papering over an issue for 4.2 vs what the proper fix in 4.3 will be. We also need to know what is actually new functionality or behaviour (i.e. not fixing an xl vs xend regression). IOW we need to have clear descriptions of the reasons for the changes not just what the changes.

Yes.

Quoted 5 lines

I think all the above need to be written down explicitly in either the commit message or the introductory email, otherwise the review of this series is just going to continue to go round in circles -- the reasoning behind these changes is just too complex for a reviewer (even one who is familiar with all this stuff already) to hold in their head.

Quite...

Quoted 10 lines
Quoted 4 lines

Also: this xenstore path should be a relative path, ie one relative to the xenstore home of domain running this part of the tools. That way the scripts can be easily and automatically disabled for dom0 and enabled in driver domains.

XENBUS_PATH contains elements for both the back- and frontend domains as well as the specific device.

Or do you think the key should be global per-(backend-domain rather than per-device?

The latter.

Quoted 3 lines
Quoted 1 line

These names are rather too generic, I think.

enums should also be declared in lixl_types_internal.idl

Surely an enum which doesn't escape from inside libxl and which never needs to be printed can just be an enum ?

Ian.

#14 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 7 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 5 lines
Quoted 4 lines

Also: this xenstore path should be a relative path, ie one relative to the xenstore home of domain running this part of the tools. That way the scripts can be easily and automatically disabled for dom0 and enabled in driver domains.

Quoted 7 lines
Quoted 5 lines

XENBUS_PATH contains elements for both the back- and frontend domains as well as the specific device.

Or do you think the key should be global per-(backend-domain rather than per-device?

The latter.

So /libxl/$XENBUS_PATH is ok then?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

These names are rather too generic, I think.

enums should also be declared in lixl_types_internal.idl

Surely an enum which doesn't escape from inside libxl and which never needs to be printed can just be an enum ?

Sure.

#15 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 6 lines · Ian Jackson
Quoted 5 lines

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 3 lines

XENBUS_PATH contains elements for both the back- and frontend domains as well as the specific device.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Or do you think the key should be global per-(backend-domain rather than per-device?

The latter.

So /libxl/$XENBUS_PATH is ok then?

?

I was suggesting that the key should be global, and XENBUS_PATH contains too much.

Ian.

#16 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 10 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 9 lines · Ian Jackson
Quoted 8 lines

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 5 lines

XENBUS_PATH contains elements for both the back- and frontend domains as well as the specific device.

Or do you think the key should be global per-(backend-domain rather than per-device?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The latter.

So /libxl/$XENBUS_PATH is ok then?

?

I was suggesting that the key should be global, and XENBUS_PATH contains too much.

Ah, I saw "latter" and read the last bit of my sentence "per-device", my mistake.

Ian.

#17 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 6 lines · Ian Jackson
Quoted 2 lines

I was suggesting that the key should be global, and XENBUS_PATH contains too much.

Ah, I saw "latter" and read the last bit of my sentence "per-device", my mistake.

Right.

The reason I think it should be global is that this is a transitional bodge to make driver domains continue to work pending a proper sorting-out in 4.3. So there is no need to make it fully general.

Ian.

#18 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 7 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 5 lines · Ian Jackson
Quoted 2 lines

I was suggesting that the key should be global, and XENBUS_PATH contains too much.

Ah, I saw "latter" and read the last bit of my sentence "per-device", my mistake.

Quoted 7 lines

Right.

The reason I think it should be global is that this is a transitional bodge to make driver domains continue to work pending a proper sorting-out in 4.3. So there is no need to make it fully general.

Ian.

So the new variable should be global, even between devices, so that we either enable hotplug script calling from xl for all, or for none. Something like:

/libxl/run_hotplug_scripts

Thanks.

#19 lore raw

Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 14 lines · Roger Pau Monne
Quoted 13 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 5 lines · Ian Jackson
Quoted 2 lines

I was suggesting that the key should be global, and XENBUS_PATH contains too much.

Ah, I saw "latter" and read the last bit of my sentence "per-device", my mistake.

Right.

The reason I think it should be global is that this is a transitional bodge to make driver domains continue to work pending a proper sorting-out in 4.3. So there is no need to make it fully general.

Quoted 7 lines
Quoted 1 line

Ian.

So the new variable should be global, even between devices, so that we either enable hotplug script calling from xl for all, or for none. Something like:

/libxl/run_hotplug_scripts

It still needs to be per-backend domain, doesn't it?

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#21 lore raw

Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 20 lines · Ian Jackson

Thanks for this mammoth patch. My comments are below.

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 6 lines

This is a rather big change, that adds the necessary machinery to perform hotplug script calling from libxl for Linux. This patch launches the necessary scripts to attach and detach PHY and TAP backend types (Qemu doesn't use hotplug scripts).

Here's a list of the major changes introduced:

Can you please wrap your commit messages to no more than 75 characters ? Some vcs's indent them. And of course they get indented when we reply leading to wrap damage.

Quoted 5 lines

* libxl__devices_destroy no longer calls libxl__device_destroy, and instead calls libxl__initiate_device_remove, so we can disconnect the device and execute the necessary hotplug scripts instead of just deleting the backend entries. So libxl__devices_destroy now uses the event library, and so does libxl_domain_destroy.

Quoted 4 lines

So we no longer have any function which will synchronously and immediately destroy a domain and throw away everything to do with it. Is it actually the case that you think such a function cannot be provided ?

It can be provided, but we should create another command or add an option to "destroy" (like -f), that doesn't wait for backend disconnection and just nukes both frontend/backend xenstore entries. This will also prevent us from executing hotplug scripts, and could lead to unexpected results.

With this series we wait for the backend to disconnect for 10s, and if it doesn't respond we nuke the frontend and wait for another 10s. If after that it fails to disconnect we nuke the remaining backend xenstore entries.

Quoted 10 lines
Quoted 5 lines

* Added a check in xen-hotplug-common.sh, so scripts are only executed from udev when using xend. xl adds a disable_udev=y to xenstore private directory and with the modification of the udev rules every call from udev gets HOTPLUG_GATE passed, that points to disable_udev. If HOTPLUG_GATE is passed and points to a value, the hotplug script is not executed.

WDYM "points to a value"? Did you just mean "is set to a nonempty value" ? I'm not convinced by the name of this variable. Shouldn't it have Xen in the name, and specify its own sense ? Eg, XEN_HOTPLUG_DISABLE or something ?

This was decided with Ian Campbell, but I have no strong feelings one
way or another, so I don't mind changing it, please read my comment below regarding the naming.

Quoted 11 lines
Quoted 1 line

+SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/blktap $env{ACTION}"

...

tools/hotplug/Linux/xen-hotplug-common.sh line 23

Quoted 6 lines
tools/hotplug/Linux/xen-hotplug-common.sh +6 -0
+# Hack to prevent the execution of hotplug scripts from udev if the domain
+# has been launched from libxl
+if [ -n "${HOTPLUG_GATE}" ]&&  \
+   `xenstore-read "$HOTPLUG_GATE">/dev/null 2>&1`; then
+    exit 0
+fi

Oh I see. Hmm. Why should this string not be fixed ? I'm not sure I like the idea of being able to pass some random other xenstore path.

Anyway, I will have to pass an environmental variable when calling the script from udev, I can rename this to UDEV_CALL=1 if that sounds better.

Quoted 4 lines

Also: this xenstore path should be a relative path, ie one relative to the xenstore home of domain running this part of the tools. That way the scripts can be easily and automatically disabled for dom0 and enabled in driver domains.

Something like:

/libxl/<domid>/disable_udev?

So that it embraces the whole domain instead of just applying to a single device?

Quoted 26 lines

tools/libxl/libxl.c int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis · line 1443

Quoted 4 lines
tools/libxl/libxl.c +3 -0
+    /* compatibility addon to keep udev rules */
+    flexarray_append(private, "disable_udev");
+    flexarray_append(private, "y");

We would normally use "1" for true in xenstore.

Quoted 8 lines

libxl__device_generic_add(gc,&device,

tools/libxl/libxl.c +5 -2
-                             libxl__xs_kvs_of_flexarray(gc, back, back->count),
-                             libxl__xs_kvs_of_flexarray(gc, front, front->count));
+                    libxl__xs_kvs_of_flexarray(gc, back, back->count),
+                    libxl__xs_kvs_of_flexarray(gc, front, front->count),
+                    libxl__xs_kvs_of_flexarray(gc, private, private->count));
+
+    switch(disk->backend) {

tools/libxl/libxl.c int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis · line 1460

Quoted 10 lines
tools/libxl/libxl.c +10 -0
+    case LIBXL_DISK_BACKEND_PHY:
+    case LIBXL_DISK_BACKEND_TAP:
+        rc = libxl__initiate_device_add(egc, ao,&device);
+        if (rc) goto out_free;
+        break;
+    case LIBXL_DISK_BACKEND_QDISK:
+    default:
+        libxl__ao_complete(egc, ao, 0);
+        break;
+    }
Quoted 1 line

Does this really need no confirmation from qemu ?

Qemu is not even started here, and it doesn't use any kind of hotplug scripts, so I think I can safely say yes.

Quoted 11 lines

tools/libxl/libxl.c int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis · line 1469

Quoted 7 lines

out_free:

tools/libxl/libxl.c +3 -0
+    flexarray_free(private);
+out_free_b:
      flexarray_free(back);
+out_free_f:
      flexarray_free(front);

It would be much better to allocate these from the gc somehow. Failing that, perhaps we could initialise them to NULL and free them iff necessary on the exit path:

Ok.

Quoted 10 lines
   out:
       ...
       if (back)  flexarray_free(back);
       if (front) flexarray_free(front);

etc.

Quoted 4 lines

int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,

(quoted diff) +0 -0
@@ -1442,7 +1484,18 @@ int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,
      if (rc != 0) goto out;
 
Quoted 10 lines

tools/libxl/libxl.c int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid, · line 1489

Quoted 5 lines
tools/libxl/libxl.c +3 -1
      rc = libxl__initiate_device_remove(egc, ao,&device);
-    if (rc) goto out;
+    switch(rc) {
+    case 1:
+        /* event added */

I think this terminology "event added" is confusingly wrong. What you mean is "event queued", I think. You should change this everywhere. But before you do that, please see what I write below about your approach to libxl__initiate_device_remove.

Ok.

Quoted 10 lines

tools/libxl/libxl.c int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk) · line 1722

Quoted 6 lines
tools/libxl/libxl.c +1 -1
@@ -1666,11 +1719,11 @@ int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
      ret = 0;
 
      libxl_device_disk_remove(ctx, domid, disks + i, 0);
-    libxl_device_disk_add(ctx, domid, disk);
+    libxl_device_disk_add(ctx, domid, disk, 0);

This needs a /* fixme-ao */ because inside-libxl callers are not allowed to invent an ao_how*, even NULL. You need to mark every occurrence of these that way.

Ok.

Quoted 4 lines
Quoted 4 lines
tools/libxl/libxl.c +0 -0
@@ -1884,8 +1937,9 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
      flexarray_append(front, libxl__sprintf(gc,
                                      LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
      libxl__device_generic_add(gc,&device,
Quoted 8 lines

tools/libxl/libxl.c int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic) · line 1942

Quoted 5 lines
tools/libxl/libxl.c +3 -2
-                             libxl__xs_kvs_of_flexarray(gc, back, back->count),
-                             libxl__xs_kvs_of_flexarray(gc, front, front->count));
+                            libxl__xs_kvs_of_flexarray(gc, back, back->count),
+                            libxl__xs_kvs_of_flexarray(gc, front, front->count),
+                            NULL);

Likewise. Also unintentional indentation change ? (In several more places, too.)

Ok, I will try to spot these.

Quoted 13 lines

tools/libxl/libxl_create.c static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config, · line 601

Quoted 10 lines
tools/libxl/libxl_create.c +1 -1
index 3675afe..de598ad 100644
@@ -598,7 +598,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
      store_libxl_entry(gc, domid,&d_config->b_info);
 
      for (i = 0; i<  d_config->num_disks; i++) {
-        ret = libxl_device_disk_add(ctx, domid,&d_config->disks[i]);
+        ret = libxl_device_disk_add(ctx, domid,&d_config->disks[i], 0);

Here in xl simply passing 0 is correct.

Quoted 8 lines

tools/libxl/libxl_create.c static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,

Quoted 6 lines
tools/libxl/libxl_create.c +6 -0
+char *libxl__device_private_path(libxl__gc *gc, libxl__device *device)
+{
+    return libxl__sprintf(gc, "/libxl/backend/%s/%u/%d",
+                          libxl__device_kind_to_string(device->backend_kind),
+                          device->domid, device->devid);
+}

Did you want to use GCSPRINTF ?

Changed, thanks.

Quoted 9 lines
Quoted 2 lines

+ rc = libxl__device_hotplug(gc, aorm->dev, DISCONNECT);

This is not correct. You need to do something more with the exit status of the hotplug script than simply throwing it away as you do in hotplug_exited. libxl__device_hotplug needs to take a callback from the caller so that it can notify the caller when the hotplug script has exited.

Quoted 3 lines

You need to not allow the ao to complete until the hotplug script has exited. Otherwise you will enter hotplug_exited after the libxl__hotplug_state has been destroyed with the ao.

Ok, I think I've changed most of this stuff, and unified device addition/destruction on the same event, since it's just a matter of wait -> execute hotplug.

Quoted 6 lines

If it's too slow and you need to time out, you need to send the hotplug script a signal, and then wait for it to exit. If necessary that signal could be SIGKILL; SIGKILL can be relied on to cause the hotplug script to actually terminate and thus the hotplug_exited callback to occur.

Where is the best place to handle this? From what I see, we have no helper for doing this, so I guess I will have to use waitpid or something similar?

Quoted 13 lines
Quoted 4 lines

+/*
+ * Return codes:
+ * 1 Success and event(s) HAVE BEEN added
+ * 0 Success and NO events were added

Quoted 8 lines

+ *< 0 Error
+ *
+ * Since this function can be called several times, and several events
+ * can be added to the same context, the user has to call
+ * libxl__ao_complete when necessary (if no events are added).
+ */
int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao,
libxl__device *dev)

Quoted 4 lines

This comment should be in the header file near the declaration. And indeed if you had put it there you would have discovered another comment already there describing the old behaviour, which you have now left behind as an erroneous comment.

Done.

Quoted 10 lines

And I think the new interface is entirely wrong. How does the caller know when to complete the ao if libxl__initiate_device_remove never calls back ?

You need to split this into two functions: one with the current interface which is a simple wrapper, used for all the existing call sites, and one which never completes any ao but which always makes a callback when the operation is complete. That callback should be used by the caller of libxl__initiate_device_remove to do whatever it needs to, which might include completing the ao.

I understand what you are saying, but I have trouble thinking of a correct way to do this, since multiple events can be added to the same ao, and the libxl__initiate_device_remove "callback" will be called more than once, how do I know when I have to call ao_complete?

Quoted 15 lines

At the moment, AFAICT from reading your code, the caller's ao will be completed by the first nontrivial device remove to complete, ie a bug.

Quoted 3 lines
tools/libxl/libxl_create.c +2 -1
-int libxl__device_destroy(libxl__gc *gc, libxl__device *dev)
+int libxl__initiate_device_add(libxl__egc *egc, libxl__ao *ao,
+                               libxl__device *dev)

...

tools/libxl/libxl_create.c

Quoted 7 lines
tools/libxl/libxl_create.c +7 -0
+    rc = libxl__ev_devstate_wait(gc,&aorm->ds, device_add_callback,
+                                 state_path, XenbusStateInitWait,
+                                 LIBXL_INIT_TIMEOUT * 1000);
+    if (rc) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable to initialize device %"
+                                          PRIu32, dev->devid);
+        libxl__ev_devstate_cancel(gc,&aorm->ds);
Quoted 2 lines

This cancel is not necessary, because device_add_cleanup will do this. (Or at least it should do; I haven't checked.)

Removed.

Quoted 27 lines

tools/libxl/libxl_create.c

Quoted 10 lines
tools/libxl/libxl_create.c +10 -0
+        goto out_fail;
+    }
+
+    return 0;
+
+out_fail:
+    assert(rc);
+    device_add_cleanup(gc, aorm);
+    return rc;
+out_ok:

^ blank line needed after the return.

tools/libxl/libxl_create.c

Quoted 4 lines
tools/libxl/libxl_create.c +4 -0
+    rc = libxl__device_hotplug(gc, dev, CONNECT);
+    libxl__ao_complete(egc, ao, 0);
+    return rc;
+}

Some of my comments about initiate_device_remove's callback probably apply here too. In particular, I have found it is often better to make a callback-queueing function always call the callback right away, recursively, on the error path. The result is that the function can return void, which simplifies callers considerably.

Whichever way you do it you do need to document your choices about reentrancy.

Quoted 13 lines

tools/libxl/libxl_create.c

Quoted 11 lines
tools/libxl/libxl_create.c +11 -0
+static void hotplug_exited(libxl__egc *egc, libxl__ev_child *child,
+                           pid_t pid, int status)
+{
+    libxl__hotplug_state *hs = CONTAINER_OF(child, *hs, child);
+    EGC_GC;
+
+    if (status) {
+        libxl_report_child_exitstatus(CTX, hs->rc ? LIBXL__LOG_ERROR
+                                                  : LIBXL__LOG_WARNING,
+                                      "hotplug child", pid, status);
+    }

As I say, this needs to make a callback.

Sure.

Quoted 6 lines

tools/libxl/libxl_create.c

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+int libxl__hotplug_exec(libxl__gc *gc, char *arg0, char **args, char **env)
+{

It would be better IMO to call this function "launch" or something, since it doesn't actually call exec in the parent (which is what I think a function called "exec" should do).

libxl__hotplug_launch it is.

Quoted 26 lines

tools/libxl/libxl_create.c

Quoted 3 lines
tools/libxl/libxl_create.c +3 -0
+    libxl__hotplug_state *hs;
+
+    hs = libxl__zalloc(gc, sizeof(*hs));

How about
GCNEW(hs)
?

tools/libxl/libxl_create.c

Quoted 3 lines
tools/libxl/libxl_create.c +3 -0
+    if (!pid) {
+        /* child */
+        signal(SIGCHLD, SIG_DFL);

What is this for ? Is the problem that children of libxl otherwise inherit a weird SIGCHLD disposition ? If so you need to fix this in libxl__exec, probably in a separate patch - and you probably need to sort out sigprocmask too in case the libxl caller has set up something weird. This is something that ought to go in a new patch.

tools/libxl/libxl_create.c

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+    if (libxl__ev_child_inuse(&hs->child)) {
+        hs->rc = rc;

Under what circumstances will rc!=0 here ? Surely that is the success path? It might be easier simply to have "out:" be only the error path. The success path always has _inuse and the failure path never does.

Quoted 7 lines

tools/libxl/libxl_create.c

Quoted 5 lines
tools/libxl/libxl_create.c +5 -0
+/* Action to pass to hotplug caller functions */
+typedef enum {
+    CONNECT = 1,
+    DISCONNECT = 2
+} libxl__hotplug_action;

These names are rather too generic, I think.

I've changed them to HOTPLUG_CONNECT and HOTPLUG_DISCONNECT.

Quoted 19 lines

tools/libxl/libxl_create.c

Quoted 8 lines
tools/libxl/libxl_create.c +8 -0
+typedef struct libxl__hotplug_state libxl__hotplug_state;
+
+struct libxl__hotplug_state {
+    /* public, result, caller may only read in callback */
+    int rc;
+    /* private for implementation */
+    libxl__ev_child child;
+};

And this is where the callback member ought to go, in the public part, with a note saying it should be set by the caller.

Is there any provision for timeout here ? Would here be a good place to do the timeout, rather than higher up the stack ?

Also you might find it better to move the args and env and so forth into the hotplug_state. That way, for example, you can actually produce a useful message when the script fails.

Quoted 12 lines

tools/libxl/libxl_create.c

Quoted 9 lines
tools/libxl/libxl_create.c +9 -0
+/*
+ * libxl__hotplug_exec is an internal function that should not be used
+ * directly, all hotplug script calls have to implement and use
+ * libxl__device_hotplug.
+ */
+_hidden int libxl__device_hotplug(libxl__gc *gc, libxl__device *dev,
+                                  libxl__hotplug_action action);
+_hidden int libxl__hotplug_exec(libxl__gc *gc, char *arg0, char **args,
+                                char **env);

Why does libxl__hotplug_exec need to be declared here at all then ? Could it be static in libxl_hotplug.c ?

No, because libxl_linux uses libxl_hotplug_launch, which is supposed to be a common launcher for both Linux and NetBSD.

Quoted 24 lines

tools/libxl/libxl_create.c

Quoted 5 lines
tools/libxl/libxl_create.c +4 -0
+/* Hotplug scripts helpers */
+static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);

Why not use CTX ? For that matter, if you use my new LOG macros you don't need to refer to CTX at all.

tools/libxl/libxl_create.c

Quoted 5 lines
tools/libxl/libxl_create.c +5 -0
+    script = libxl__xs_read(gc, XBT_NULL,
+                            libxl__sprintf(gc, "%s/%s", be_path, "script"));
+    if (!script) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable to read script from %s",
+                                          be_path);

You need to log the errno.

tools/libxl/libxl_create.c

Quoted 6 lines
tools/libxl/libxl_create.c +6 -0
+    f_env = flexarray_make(9, 1);
+    if (!f_env) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "unable to create environment array");
+        return NULL;
+    }
Quoted 2 lines

Isn't this an allocation failure which should be dealt with by libxl__alloc_failed ?

Done, I think we should set a macro for

libxl__alloc_failed(ctx, __func__, 0,0);

Because I'm using that on more than one place.

Quoted 6 lines

tools/libxl/libxl_create.c

Quoted 4 lines
tools/libxl/libxl_create.c +3 -0
+    flexarray_set(f_env, nr++, "XENBUS_TYPE");
+    flexarray_set(f_env, nr++, (char *)
+                  libxl__device_kind_to_string(dev->backend_kind));

Why is the cast needed ?

Not sure, probably slipped from some other place, it's removed now.

Quoted 8 lines

tools/libxl/libxl_create.c

Quoted 6 lines
tools/libxl/libxl_create.c +5 -0
+    flexarray_set(f_env, nr++, "XENBUS_PATH");
+    flexarray_set(f_env, nr++,
+                  libxl__sprintf(gc, "backend/%s/%u/%d",
+                  libxl__device_kind_to_string(dev->backend_kind),
+                  dev->domid, dev->devid));

GCSPRINTF might help shorten this ?

Done.

Quoted 6 lines

For that matter, you've called libxl__device_kind_to_string twice. Calling it only once would make this easier to read.

I won't comment any more on places where I think GCSPRINTF, LOG/LOGE/LOGEV and CTX might usefully replace what you have written.

I'm trying to spot most of them, but it's quite difficult. Are we going to do a massive replace of this at some point?

Quoted 17 lines

tools/libxl/libxl_create.c

Quoted 4 lines
tools/libxl/libxl_create.c +3 -0
+    env = get_hotplug_env(gc, dev);
+    if (!env)
+        return -1;

Surely this should never fail as it just results in allocation failure ?

This function seems to be supposed to return an rc value, so return -1 is wrong.

Quoted 1 line

+ args = (char **) flexarray_contents(f_args);

Why is the cast needed ?

tools/libxl/libxl_create.c

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+    what = libxl__sprintf(gc, "%s %s", args[0],
+                          action == CONNECT ? "connect" : "disconnect");
Quoted 6 lines

Surely

   action == CONNECT ? "connect" :
   action == DISCONNECT ? "disconnect" : abort()

?

Done!

Quoted 26 lines

tools/libxl/libxl_create.c

Quoted 4 lines
tools/libxl/libxl_create.c +4 -0
+    rc = libxl__hotplug_exec(gc, args[0], args, env);
+    if (rc) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable execute hotplug scripts for "
+                                          "device %"PRIu32, dev->devid);

libxl__hotplug_exec ought to do all the logging. That means that all the information needed to do so should be passed to it, including the domid (which you don't currently log) and the device id. That should probably be filled in by its caller in the libxl__hotplug_state struct.

tools/python/xen/lowlevel/xl/xl.c static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args) · line 450

Quoted 10 lines
tools/python/xen/lowlevel/xl/xl.c +1 -1
index c4f7c52..ce7a2b2 100644
@@ -447,7 +447,7 @@ static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args)
      int domid;
      if ( !PyArg_ParseTuple(args, "i",&domid) )
          return NULL;
-    if ( libxl_domain_destroy(self->ctx, domid) ) {
+    if ( libxl_domain_destroy(self->ctx, domid, NULL) ) {

I think this is correct. Or, at least, we don't currently provide any asynchronous capability in the Python code and I don't mind not doing so.

Quoted 1 line

Ian.

Thanks for the review!

#22 lore raw

Re: Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 26 lines · Roger Pau Monne
Quoted 20 lines · Ian Jackson

Thanks for this mammoth patch. My comments are below.

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 6 lines

This is a rather big change, that adds the necessary machinery to perform hotplug script calling from libxl for Linux. This patch launches the necessary scripts to attach and detach PHY and TAP backend types (Qemu doesn't use hotplug scripts).

Here's a list of the major changes introduced:

Can you please wrap your commit messages to no more than 75 characters ? Some vcs's indent them. And of course they get indented when we reply leading to wrap damage.

Quoted 5 lines

* libxl__devices_destroy no longer calls libxl__device_destroy, and instead calls libxl__initiate_device_remove, so we can disconnect the device and execute the necessary hotplug scripts instead of just deleting the backend entries. So libxl__devices_destroy now uses the event library, and so does libxl_domain_destroy.

Quoted 4 lines

So we no longer have any function which will synchronously and immediately destroy a domain and throw away everything to do with it. Is it actually the case that you think such a function cannot be provided ?

Quoted 5 lines

It can be provided, but we should create another command or add an option to "destroy" (like -f), that doesn't wait for backend disconnection and just nukes both frontend/backend xenstore entries. This will also prevent us from executing hotplug scripts, and could lead to unexpected results.

We have a plan of action to fix this properly in 4.3 (via the Driver Dom protocol, which separates the backend from the hotplug info).

It would IMHO be acceptable for 4.2 to keep on simply nuking the backend, and accepting the downsides which this entails. AFAIK this is not a regression vs Xend -- xend also just nukes the backend (please correct me if I am wrong about this).

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

* Added a check in xen-hotplug-common.sh, so scripts are only executed from udev when using xend. xl adds a disable_udev=y to xenstore private directory and with the modification of the udev rules every call from udev gets HOTPLUG_GATE passed, that points to disable_udev. If HOTPLUG_GATE is passed and points to a value, the hotplug script is not executed.

Quoted 6 lines
Quoted 4 lines

WDYM "points to a value"? Did you just mean "is set to a nonempty value" ? I'm not convinced by the name of this variable. Shouldn't it have Xen in the name, and specify its own sense ? Eg, XEN_HOTPLUG_DISABLE or something ?

This was decided with Ian Campbell,

Please do feel free to take my half-baked suggestions and make them sensible ;-)

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#23 lore raw

Re: Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 30 lines · Ian Campbell
Quoted 23 lines · Roger Pau Monne
Quoted 18 lines · Ian Jackson

Thanks for this mammoth patch. My comments are below.

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 6 lines

This is a rather big change, that adds the necessary machinery to perform hotplug script calling from libxl for Linux. This patch launches the necessary scripts to attach and detach PHY and TAP backend types (Qemu doesn't use hotplug scripts).

Here's a list of the major changes introduced:

Can you please wrap your commit messages to no more than 75 characters ? Some vcs's indent them. And of course they get indented when we reply leading to wrap damage.

Quoted 5 lines

* libxl__devices_destroy no longer calls libxl__device_destroy, and instead calls libxl__initiate_device_remove, so we can disconnect the device and execute the necessary hotplug scripts instead of just deleting the backend entries. So libxl__devices_destroy now uses the event library, and so does libxl_domain_destroy.

Quoted 4 lines

So we no longer have any function which will synchronously and immediately destroy a domain and throw away everything to do with it. Is it actually the case that you think such a function cannot be provided ?

Quoted 5 lines

It can be provided, but we should create another command or add an option to "destroy" (like -f), that doesn't wait for backend disconnection and just nukes both frontend/backend xenstore entries. This will also prevent us from executing hotplug scripts, and could lead to unexpected results.

Quoted 7 lines

We have a plan of action to fix this properly in 4.3 (via the Driver Dom protocol, which separates the backend from the hotplug info).

It would IMHO be acceptable for 4.2 to keep on simply nuking the backend, and accepting the downsides which this entails. AFAIK this is not a regression vs Xend -- xend also just nukes the backend (please correct me if I am wrong about this).

This is ok if we execute hotplug scripts with udev, because they are executed when the "physical" device is destroyed, but if we have to launch the scripts from the toolstack, the only way to know that the physical device is no longer in use is by watching xenstore backend entries, that's why we cannot nuke them unless it is our last option.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

* Added a check in xen-hotplug-common.sh, so scripts are only executed from udev when using xend. xl adds a disable_udev=y to xenstore private directory and with the modification of the udev rules every call from udev gets HOTPLUG_GATE passed, that points to disable_udev. If HOTPLUG_GATE is passed and points to a value, the hotplug script is not executed.

WDYM "points to a value"? Did you just mean "is set to a nonempty value" ? I'm not convinced by the name of this variable. Shouldn't it have Xen in the name, and specify its own sense ? Eg, XEN_HOTPLUG_DISABLE or something ?

Quoted 8 lines
Quoted 1 line

This was decided with Ian Campbell,

Please do feel free to take my half-baked suggestions and make them sensible ;-)

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#24 lore raw

Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

In reply to Roger Pau Monne

Thanks for your reply. I see you didn't reply to all of my comments. Do you plan to address the others later ?

Roger Pau Monne writes ("Fwd: Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 11 lines
Quoted 4 lines · Ian Jackson

So we no longer have any function which will synchronously and immediately destroy a domain and throw away everything to do with it. Is it actually the case that you think such a function cannot be provided ?

It can be provided, but we should create another command or add an option to "destroy" (like -f), that doesn't wait for backend disconnection and just nukes both frontend/backend xenstore entries. This will also prevent us from executing hotplug scripts, and could lead to unexpected results.

Quite so. But it is important to be able to disregard a malfunctioning driver domain. That would make it possible to shut down all the guests using it, destroy the driver domain itself, and restart it in a hopefully good state, for example.

Quoted 4 lines

With this series we wait for the backend to disconnect for 10s, and if it doesn't respond we nuke the frontend and wait for another 10s. If after that it fails to disconnect we nuke the remaining backend xenstore entries.

I think we need to think about these timeouts. At the very least they're policy which should probably not be hardcoded in libxl; and arguably people might want to be able to specify infinite ("I trust my guest or driver domain and never want to pull the rug out from under its feet") or zero ("this guest or driver domain has gone wrong and I want it killed, now").

Quoted 10 lines
Quoted 9 lines

tools/hotplug/Linux/xen-hotplug-common.sh line 23

Quoted 6 lines
tools/hotplug/Linux/xen-hotplug-common.sh +6 -0
+# Hack to prevent the execution of hotplug scripts from udev if the domain
+# has been launched from libxl
+if [ -n "${HOTPLUG_GATE}" ]&&  \
+   `xenstore-read "$HOTPLUG_GATE">/dev/null 2>&1`; then
+    exit 0
+fi

Oh I see. Hmm. Why should this string not be fixed ? I'm not sure I like the idea of being able to pass some random other xenstore path.

Quoted 2 lines

Anyway, I will have to pass an environmental variable when calling the script from udev, I can rename this to UDEV_CALL=1 if that sounds better.

Perhaps it would be better to do things the other way around, and have an env var for the case where we're _not_ calling the script from udev ? After all, udev config is configuration (at least on my distros) which the user may not update when the software is updated.

Quoted 11 lines
Quoted 4 lines

Also: this xenstore path should be a relative path, ie one relative to the xenstore home of domain running this part of the tools. That way the scripts can be easily and automatically disabled for dom0 and enabled in driver domains.

Something like:

/libxl/<domid>/disable_udev?

So that it embraces the whole domain instead of just applying to a single device?

Yes, except you want
/local/domain/<domid>/libxl/disable_udev
or some such so that <domid> can access it via the relative path
libxl/disable_udev
without needing to know its own domid.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
tools/libxl/libxl.c +11 -0
+    switch(disk->backend) {
+    case LIBXL_DISK_BACKEND_PHY:
+    case LIBXL_DISK_BACKEND_TAP:
+        rc = libxl__initiate_device_add(egc, ao,&device);
+        if (rc) goto out_free;
+        break;
+    case LIBXL_DISK_BACKEND_QDISK:
+    default:
+        libxl__ao_complete(egc, ao, 0);
+        break;
+    }
Quoted 4 lines
Quoted 1 line

Does this really need no confirmation from qemu ?

Qemu is not even started here, and it doesn't use any kind of hotplug scripts, so I think I can safely say yes.

Uh, what, LIBXL_DISK_BACKEND_QDISK but qemu is not started ? How is that possible ?

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

+ rc = libxl__device_hotplug(gc, aorm->dev, DISCONNECT);

This is not correct. You need to do something more with the exit status of the hotplug script than simply throwing it away as you do in hotplug_exited. libxl__device_hotplug needs to take a callback from the caller so that it can notify the caller when the hotplug script has exited.

Quoted 7 lines
Quoted 3 lines

You need to not allow the ao to complete until the hotplug script has exited. Otherwise you will enter hotplug_exited after the libxl__hotplug_state has been destroyed with the ao.

Ok, I think I've changed most of this stuff, and unified device addition/destruction on the same event, since it's just a matter of wait -> execute hotplug.

I'm not sure I quite understand your response, but OK :-).

Quoted 9 lines
Quoted 5 lines

If it's too slow and you need to time out, you need to send the hotplug script a signal, and then wait for it to exit. If necessary that signal could be SIGKILL; SIGKILL can be relied on to cause the hotplug script to actually terminate and thus the hotplug_exited callback to occur.

Where is the best place to handle this? From what I see, we have no helper for doing this, so I guess I will have to use waitpid or something similar?

You must not use waitpid yourself. libxl__ev_child_fork will do all of that and simply call yo back when the child exits.

You can call kill on the pid at any time (provided that 1. you have the ctx lock held and 2. the libxl__ev_child state struct is still "inuse" ie still has the pid in it).

Quoted 11 lines
Quoted 10 lines

And I think the new interface is entirely wrong. How does the caller know when to complete the ao if libxl__initiate_device_remove never calls back ?

You need to split this into two functions: one with the current interface which is a simple wrapper, used for all the existing call sites, and one which never completes any ao but which always makes a callback when the operation is complete. That callback should be used by the caller of libxl__initiate_device_remove to do whatever it needs to, which might include completing the ao.

Quoted 4 lines

I understand what you are saying, but I have trouble thinking of a correct way to do this, since multiple events can be added to the same ao, and the libxl__initiate_device_remove "callback" will be called more than once, how do I know when I have to call ao_complete?

You have to maintain a data structure of some kind that tells you whether you're expecting more callbacks and what they are.

Quoted 9 lines
Quoted 7 lines

tools/libxl/libxl_internal.h _hidden char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid); · line 479

Quoted 5 lines
tools/libxl/libxl_internal.h +5 -0
+/* Action to pass to hotplug caller functions */
+typedef enum {
+    CONNECT = 1,
+    DISCONNECT = 2
+} libxl__hotplug_action;

These names are rather too generic, I think.

I've changed them to HOTPLUG_CONNECT and HOTPLUG_DISCONNECT.

Sounds good.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines

+/*
+ * libxl__hotplug_exec is an internal function that should not be used
+ * directly, all hotplug script calls have to implement and use
+ * libxl__device_hotplug.

Quoted 8 lines

tools/libxl/libxl_internal.h _hidden char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid); · line 498

Quoted 5 lines

+ */

tools/libxl/libxl_internal.h +4 -0
+_hidden int libxl__device_hotplug(libxl__gc *gc, libxl__device *dev,
+                                  libxl__hotplug_action action);
+_hidden int libxl__hotplug_exec(libxl__gc *gc, char *arg0, char **args,
+                                char **env);

Why does libxl__hotplug_exec need to be declared here at all then ? Could it be static in libxl_hotplug.c ?

Quoted 2 lines

No, because libxl_linux uses libxl_hotplug_launch, which is supposed to be a common launcher for both Linux and NetBSD.

Ah right.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
tools/libxl/libxl_linux.c +6 -0
+    f_env = flexarray_make(9, 1);
+    if (!f_env) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "unable to create environment array");
+        return NULL;
+    }
Quoted 8 lines
Quoted 2 lines

Isn't this an allocation failure which should be dealt with by libxl__alloc_failed ?

Done, I think we should set a macro for

libxl__alloc_failed(ctx, __func__, 0,0);

Because I'm using that on more than one place.

Sure. A helper macro for that would be fine.

Quoted 8 lines
Quoted 5 lines

For that matter, you've called libxl__device_kind_to_string twice. Calling it only once would make this easier to read.

I won't comment any more on places where I think GCSPRINTF, LOG/LOGE/LOGEV and CTX might usefully replace what you have written.

I'm trying to spot most of them, but it's quite difficult. Are we going to do a massive replace of this at some point?

I'm not sure. I don't currently have any plans to do so before 4.2.

But in new code it would be better to use the shorter convenience macros simply to reduce the amount of nearly- meaningless repetition.

Thanks,
Ian.

#25 lore raw

Re: Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 26 lines · Ian Jackson

Thanks for your reply. I see you didn't reply to all of my comments. Do you plan to address the others later ?

Roger Pau Monne writes ("Fwd: Re: [Xen-devel] [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 11 lines
Quoted 4 lines · Ian Jackson

So we no longer have any function which will synchronously and immediately destroy a domain and throw away everything to do with it. Is it actually the case that you think such a function cannot be provided ?

It can be provided, but we should create another command or add an option to "destroy" (like -f), that doesn't wait for backend disconnection and just nukes both frontend/backend xenstore entries. This will also prevent us from executing hotplug scripts, and could lead to unexpected results.

Quite so. But it is important to be able to disregard a malfunctioning driver domain. That would make it possible to shut down all the guests using it, destroy the driver domain itself, and restart it in a hopefully good state, for example.

Quoted 4 lines

With this series we wait for the backend to disconnect for 10s, and if it doesn't respond we nuke the frontend and wait for another 10s. If after that it fails to disconnect we nuke the remaining backend xenstore entries.

Quoted 6 lines

I think we need to think about these timeouts. At the very least they're policy which should probably not be hardcoded in libxl; and arguably people might want to be able to specify infinite ("I trust my guest or driver domain and never want to pull the rug out from under its feet") or zero ("this guest or driver domain has gone wrong and I want it killed, now").

Unless the same is going to be true of the eventual solution (which I suppose it may well be?) we should be wary of over engineering the interim solution for 4.2.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

tools/hotplug/Linux/xen-hotplug-common.sh line 23

Quoted 6 lines
tools/hotplug/Linux/xen-hotplug-common.sh +6 -0
+# Hack to prevent the execution of hotplug scripts from udev if the domain
+# has been launched from libxl
+if [ -n "${HOTPLUG_GATE}" ]&&  \
+   `xenstore-read "$HOTPLUG_GATE">/dev/null 2>&1`; then
+    exit 0
+fi

Oh I see. Hmm. Why should this string not be fixed ? I'm not sure I like the idea of being able to pass some random other xenstore path.

Quoted 7 lines
Quoted 2 lines

Anyway, I will have to pass an environmental variable when calling the script from udev, I can rename this to UDEV_CALL=1 if that sounds better.

Perhaps it would be better to do things the other way around, and have an env var for the case where we're _not_ calling the script from udev ? After all, udev config is configuration (at least on my distros) which the user may not update when the software is updated.

It was my suggestion to do it this way so that we don't end up with a vestigial env var which must always be set for no real reason after we've removed the udev path altogether.

I don't really mind though, we could live with that vestigial thing, or have another, easier, transition a few releases down the line.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#26 lore raw

Re: Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Ian Campbell writes ("Re: [Xen-devel] Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 11 lines · Ian Jackson
Quoted 6 lines

I think we need to think about these timeouts. At the very least they're policy which should probably not be hardcoded in libxl; and arguably people might want to be able to specify infinite ("I trust my guest or driver domain and never want to pull the rug out from under its feet") or zero ("this guest or driver domain has gone wrong and I want it killed, now").

Unless the same is going to be true of the eventual solution (which I suppose it may well be?) we should be wary of over engineering the interim solution for 4.2.

I guess. Also xend's poor behaviour in this area (failing hotplug timeouts) provides something of an excuse...

Quoted 8 lines
Quoted 4 lines

Perhaps it would be better to do things the other way around, and have an env var for the case where we're _not_ calling the script from udev ? After all, udev config is configuration (at least on my distros) which the user may not update when the software is updated.

It was my suggestion to do it this way so that we don't end up with a vestigial env var which must always be set for no real reason after we've removed the udev path altogether.

When we have "removed the udev path altogether" we will still need to have something to prevent trouble if the udev rules remain for some reason.

Quoted 2 lines

I don't really mind though, we could live with that vestigial thing, or have another, easier, transition a few releases down the line.

The vestigial thing can indeed eventually go away.

Ian.

#27 lore raw

Re: Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd

Quoted 25 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] Fwd: Re: [PATCH v3 3/5] libxl: call hotplug scripts from libxl for vbd"):

Quoted 11 lines · Ian Jackson
Quoted 6 lines

I think we need to think about these timeouts. At the very least they're policy which should probably not be hardcoded in libxl; and arguably people might want to be able to specify infinite ("I trust my guest or driver domain and never want to pull the rug out from under its feet") or zero ("this guest or driver domain has gone wrong and I want it killed, now").

Unless the same is going to be true of the eventual solution (which I suppose it may well be?) we should be wary of over engineering the interim solution for 4.2.

I guess. Also xend's poor behaviour in this area (failing hotplug timeouts) provides something of an excuse...

Quoted 8 lines
Quoted 4 lines

Perhaps it would be better to do things the other way around, and have an env var for the case where we're _not_ calling the script from udev ? After all, udev config is configuration (at least on my distros) which the user may not update when the software is updated.

It was my suggestion to do it this way so that we don't end up with a vestigial env var which must always be set for no real reason after we've removed the udev path altogether.

Quoted 8 lines

When we have "removed the udev path altogether" we will still need to have something to prevent trouble if the udev rules remain for some reason.

Quoted 2 lines

I don't really mind though, we could live with that vestigial thing, or have another, easier, transition a few releases down the line.

The vestigial thing can indeed eventually go away.

I'm happy for Roger and you to agree whichever sense you think best for the flag.

Ian.

#28 lore raw

PATCH v3 4/5 libxl: call hotplug scripts from libxl for vif

In reply to Roger Pau Monne

As the previous change already introduces most of needed machinery to call hotplug scripts from libxl, this only adds the necessary bits to call this scripts for vif interfaces also.

libxl_device_nic_add has been changed to make use of the new event functionality, and the necessary vif hotplug code has been added. No changes where needed in the teardown part, since it uses exactly the same code introduced for vbd.

An exception has been introduced for tap devices, since hotplug scripts should be called after the device model has been created, so the hotplug call for those is done in do_domain_create after confirmation of the device model startup. On the other hand, tap devices don't use teardown scripts, so add another exception for that.

libxl__device_from_nic was moved to libxl_device.c, so it can be used in libxl_create.c, and libxl__device_from_disk was also moved to maintain the symmetry.

libxl__initiate_device_remove has been changed a little, to nuke the frontend xenstore path before trying to perform device teardown, this allows for uninitialized devices to be closed gracefully, specially vif interfaces added to HVM machines and not used.

PV nic devices are set to LIBXL_NIC_TYPE_VIF, since the default value is LIBXL_NIC_TYPE_IOEMU regardless of the guest type.

A new global option, called disable_vif_scripts has been added to allow the user decide if he wants to execute the hotplug scripts from xl or from udev. This has been documented in the xl.conf man page.

Changes since v2:

* Added fancy functions to fetch tap and vif names, now the prefix of the tap device has been saved in a constant, called TAP_DEVICE_PREFIX.

* Wait for timeout before nuking frontend xenstore entries.

* Changed disable_vif_scripts to disable_xl_vif_scripts, it's easier to understand.

* Default nic type set by libxl__device_nic_setdefault is VIF now (was IOEMU).

* Save nic type and udev script exection inside /libxl/devices/<domid>/nic/<devid>/ instead of saving it in the backend path.

Changes since v1:

* Propagated changes from previous patch (disable_udev and libxl_device_nic_add switch).

* Modified udev rules to add the new env variable.

* Added support for named interfaces.

---
 docs/man/xl.conf.pod.5                |    7 ++
 tools/examples/xl.conf                |    3 +
 tools/hotplug/Linux/xen-backend.rules |    6 +-
 tools/libxl/libxl.c                   |  125 +++++++++++++-----------------
 tools/libxl/libxl.h                   |    3 +-
 tools/libxl/libxl_create.c            |   22 +++++-
 tools/libxl/libxl_device.c            |  122 +++++++++++++++++++++++++++--
 tools/libxl/libxl_dm.c                |    2 +-
 tools/libxl/libxl_internal.h          |   18 ++++-
 tools/libxl/libxl_linux.c             |  137 ++++++++++++++++++++++++++++++++-
 tools/libxl/libxl_types.idl           |    1 +
 tools/libxl/xl.c                      |    4 +
 tools/libxl/xl.h                      |    1 +
 tools/libxl/xl_cmdimpl.c              |    8 ++-
 14 files changed, 369 insertions(+), 90 deletions(-)
docs/man/xl.conf.pod.5 +7 -0
index 8bd45ea..da8f16f 100644
@@ -55,6 +55,13 @@ default.
 
 Default: C<1>
 
+=item B<disable_xl_vif_scripts=BOOLEAN>
+
+If enabled xl will not execute nic hotplug scripts itself, and instead
+relegate this task to udev.
+
+Default: C<0>
+
 =item B<lockfile="PATH">
 
 Sets the path to the lock file used by xl to serialise certain
tools/examples/xl.conf +3 -0
index 56d3b3b..172ff80 100644
@@ -12,3 +12,6 @@
 
 # default output format used by "xl list -l"
 #output_format="json"
+
+# default caller of hotplug scripts for vif interfaces
+#disable_xl_vif_scripts=0
tools/hotplug/Linux/xen-backend.rules +3 -3
index 19f3d27..5dca574 100644
@@ -2,8 +2,8 @@ SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_
 SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/block $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vif2-*", RUN+="/etc/xen/scripts/vif2 $env{ACTION}"
-SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif"
-SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif"
+SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif"
+SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif"
 SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}"
 SUBSYSTEM=="xen-backend", ACTION=="remove", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/xen-hotplug-cleanup"
 KERNEL=="evtchn", NAME="xen/%k"
@@ -13,4 +13,4 @@ KERNEL=="blktap-control", NAME="xen/blktap-2/control", MODE="0600"
 KERNEL=="gntdev", NAME="xen/%k", MODE="0600"
 KERNEL=="pci_iomul", NAME="xen/%k", MODE="0600"
 KERNEL=="tapdev[a-z]*", NAME="xen/blktap-2/tapdev%m", MODE="0600"
-SUBSYSTEM=="net", KERNEL=="xentap*", ACTION=="add", RUN+="/etc/xen/scripts/vif-setup $env{ACTION} type_if=tap"
+SUBSYSTEM=="net", KERNEL=="emu*", ACTION=="add", ENV{HOTPLUG_GATE}="/libxl/$env{XENBUS_PATH}/disable_udev", RUN+="/etc/xen/scripts/vif-setup $env{ACTION} type_if=tap"
tools/libxl/libxl.c +56 -69

188 lines not on this page · raw message

tools/libxl/libxl.h +2 -1
index 6687e2e..723c3aa 100644
@@ -629,7 +629,8 @@ char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk);
 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk);
 
 /* Network Interfaces */
-int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
+int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic,
+                         const libxl_asyncop_how *ao_how);
 int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid,
                             libxl_device_nic *nic,
                             const libxl_asyncop_how *ao_how);
tools/libxl/libxl_create.c +21 -1
index de598ad..023159b 100644
@@ -607,7 +607,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         }
     }
     for (i = 0; i < d_config->num_vifs; i++) {
-        ret = libxl_device_nic_add(ctx, domid, &d_config->vifs[i]);
+        ret = libxl_device_nic_add(ctx, domid, &d_config->vifs[i], 0);
         if (ret) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
                        "cannot add nic %d to domain: %d", i, ret);
@@ -685,6 +685,26 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
                        "device model did not start: %d", ret);
             goto error_out;
         }
+        /* 
+         * Execute hotplug scripts for tap devices, this has to be done
+         * after the domain model has been started.
+        */
+        for (i = 0; i < d_config->num_vifs; i++) {
+            if (d_config->vifs[i].nictype == LIBXL_NIC_TYPE_IOEMU &&
+                !d_config->vifs[i].disable_xl_vif_script) {
+                libxl__device device;
+                ret = libxl__device_from_nic(gc, domid, &d_config->vifs[i],
+                                             &device);
+                if (ret < 0) goto error_out;
+                ret = libxl__device_hotplug(gc, &device, CONNECT);
+                if (ret < 0) {
+                    LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                               "unable to launch hotplug script for device: "
+                               "%"PRIu32, device.devid);
+                    goto error_out;
+                }
+            }
+        }
     }
 
     for (i = 0; i < d_config->num_pcidevs; i++)
tools/libxl/libxl_device.c +113 -9

190 lines not on this page · raw message

tools/libxl/libxl_dm.c +1 -1
index 4ce58f6..8cf9d9d 100644
@@ -794,7 +794,7 @@ retry_transaction:
             goto out_free;
     }
     for (i = 0; i < dm_config.num_vifs; i++) {
-        ret = libxl_device_nic_add(ctx, dm_domid, &dm_config.vifs[i]);
+        ret = libxl_device_nic_add(ctx, dm_domid, &dm_config.vifs[i], 0);
         if (ret)
             goto out_free;
     }
tools/libxl/libxl_internal.h +14 -4
index 95d5c40..68c7514 100644
@@ -84,6 +84,7 @@
 #define STUBDOM_CONSOLE_RESTORE 2
 #define STUBDOM_CONSOLE_SERIAL 3
 #define STUBDOM_SPECIAL_CONSOLES 3
+#define TAP_DEVICE_PREFIX "emu"
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
@@ -765,6 +766,12 @@ _hidden int libxl__domain_pvcontrol_write(libxl__gc *gc, xs_transaction_t t,
 _hidden char *libxl__device_disk_string_of_backend(libxl_disk_backend backend);
 _hidden char *libxl__device_disk_string_of_format(libxl_disk_format format);
 _hidden int libxl__device_disk_set_backend(libxl__gc*, libxl_device_disk*);
+_hidden int libxl__device_from_nic(libxl__gc *gc, uint32_t domid,
+                                   libxl_device_nic *nic,
+                                   libxl__device *device);
+_hidden int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
+                                    libxl_device_disk *disk,
+                                    libxl__device *device);
 
 _hidden int libxl__device_physdisk_major_minor(const char *physpath, int *major, int *minor);
 _hidden int libxl__device_disk_dev_number(const char *virtpath,
@@ -775,10 +782,13 @@ _hidden int libxl__device_console_add(libxl__gc *gc, uint32_t domid,
                                       libxl__domain_build_state *state);
 
 _hidden int libxl__device_generic_add(libxl__gc *gc, libxl__device *device,
-                             char **bents, char **fents, char **private);
+                                      char **bents, char **fents,
+                                      char **private);
 _hidden char *libxl__device_backend_path(libxl__gc *gc, libxl__device *device);
-_hidden char *libxl__device_private_path(libxl__gc *gc, libxl__device *device);
 _hidden char *libxl__device_frontend_path(libxl__gc *gc, libxl__device *device);
+_hidden char *libxl__device_private_path(libxl__gc *gc, libxl__device *device);
+_hidden char *libxl__device_vif_name(libxl__gc *gc, libxl__device *dev);
+_hidden char *libxl__device_tap_name(libxl__gc *gc, libxl__device *dev);
 _hidden int libxl__parse_backend_path(libxl__gc *gc, const char *path,
                                       libxl__device *dev);
 _hidden int libxl__device_destroy(libxl__gc *gc, libxl__device *dev);
@@ -803,8 +813,8 @@ _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state);
  * this is done, the ao will be completed.  An error
  * return from libxl__initiate_device_remove means that the ao
  * will _not_ be completed and the caller must do so. */
-_hidden int libxl__initiate_device_remove(libxl__egc*, libxl__ao*,
-                                          libxl__device *dev);
+_hidden int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao,
+                                          libxl__device *dev, int force);
 
 /* Arranges that dev will be added to the guest, and the
  * hotplug scripts will be executed (if necessary). When
tools/libxl/libxl_linux.c +136 -1

167 lines not on this page · raw message

tools/libxl/libxl_types.idl +1 -0
index 5cf9708..0b2c832 100644
@@ -343,6 +343,7 @@ libxl_device_nic = Struct("device_nic", [
     ("ifname", string),
     ("script", string),
     ("nictype", libxl_nic_type),
+    ("disable_xl_vif_script", integer),
     ])
 
 libxl_device_pci = Struct("device_pci", [
tools/libxl/xl.c +4 -0
index a6ffd25..2f96468 100644
@@ -35,6 +35,7 @@
 xentoollog_logger_stdiostream *logger;
 int dryrun_only;
 int autoballoon = 1;
+int disable_xl_vif_scripts = 0;
 char *lockfile;
 char *default_vifscript = NULL;
 char *default_bridge = NULL;
@@ -66,6 +67,9 @@ static void parse_global_config(const char *configfile,
     if (!xlu_cfg_get_long (config, "autoballoon", &l, 0))
         autoballoon = l;
 
+    if (!xlu_cfg_get_long (config, "disable_xl_vif_scripts", &l, 0))
+        disable_xl_vif_scripts = l;
+
     if (!xlu_cfg_get_string (config, "lockfile", &buf, 0))
         lockfile = strdup(buf);
     else {
tools/libxl/xl.h +1 -0
index 7e258d5..b60cd12 100644
@@ -109,6 +109,7 @@ void postfork(void);
 
 /* global options */
 extern int autoballoon;
+extern int disable_xl_vif_scripts;
 extern int dryrun_only;
 extern char *lockfile;
 extern char *default_vifscript;
tools/libxl/xl_cmdimpl.c +7 -1
index 0bc3ac2..704d938 100644
@@ -847,6 +847,12 @@ static void parse_config_data(const char *configfile_filename_report,
                 nic->script = strdup(default_vifscript);
             }
 
+            /*
+             * Disable nic network script calling, to allow the user
+             * to attach the nic backend from a different domain.
+             */
+            nic->disable_xl_vif_script = disable_xl_vif_scripts;
+
 	    if (default_bridge) {
 		free(nic->bridge);
 		nic->bridge = strdup(default_bridge);
@@ -4916,7 +4922,7 @@ int main_networkattach(int argc, char **argv)
             return 1;
         }
     }
-    if (libxl_device_nic_add(ctx, domid, &nic)) {
+    if (libxl_device_nic_add(ctx, domid, &nic, 0)) {
         fprintf(stderr, "libxl_device_nic_add failed.\n");
         return 1;
     }
Signature · 1 line
1.7.7.5 (Apple Git-26)
#29 lore raw

Re: [PATCH v3 4/5] libxl: call hotplug scripts from libxl for vif

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 4/5] libxl: call hotplug scripts from libxl for vif"):

Quoted 3 lines

As the previous change already introduces most of needed machinery to call hotplug scripts from libxl, this only adds the necessary bits to call this scripts for vif interfaces also.

I have read this only cursorily, given my comments on the previous patch, but:

tools/libxl/libxl_device.c char *libxl__device_private_path(libxl__gc *gc, libxl__device *device) · line 55

Quoted 3 lines
tools/libxl/libxl_device.c +3 -0
+    char *ifname = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/%s",
+                                                                   be_path,
+                                                                   "vifname"));

Probably all of these functions need to take a xenstore transaction to deal with concurrency properly.

You need to avoid assuming that NULL from libxl__xs_* means what you think it means. It might be better to invent a new wrapper which you could use like this

    rc = libxl__xs_read_that_actually_works(gc, trans, path, &result);
    if (rc) goto out;
    if (!result) {
        /* code for if thing doesn't exist */
    }

GCSPRINTF might avoid the weird wrapping.

tools/libxl/libxl_types.idl libxl_device_nic = Struct("device_nic", [ · line 347

Quoted 10 lines
tools/libxl/libxl_types.idl +1 -0
index 5cf9708..0b2c832 100644
@@ -343,6 +343,7 @@ libxl_device_nic = Struct("device_nic", [
     ("ifname", string),
     ("script", string),
     ("nictype", libxl_nic_type),
+    ("disable_xl_vif_script", integer),
     ])

We shouldn't have config options of the form "disable_...". They should all be phrased as "enable" or in this case "run". And I'm not convinced by "xl", but since there isn't any patch to the xl domain configuration doc for this new option I'm not sure. Of course documentation is needed...

Ian.

#30 lore raw

Re: [PATCH v3 4/5] libxl: call hotplug scripts from libxl for vif

Quoted 8 lines · Ian Jackson
Quoted 8 lines
tools/libxl/libxl_types.idl +0 -0
index 5cf9708..0b2c832 100644
@@ -343,6 +343,7 @@ libxl_device_nic = Struct("device_nic", [
     ("ifname", string),
     ("script", string),
     ("nictype", libxl_nic_type),
Quoted 8 lines

tools/libxl/libxl_types.idl libxl_device_nic = Struct("device_nic", [ · line 347

Quoted 2 lines
tools/libxl/libxl_types.idl +1 -0
+    ("disable_xl_vif_script", integer),
     ])

We shouldn't have config options of the form "disable_...". They should all be phrased as "enable" or in this case "run". And I'm not convinced by "xl", but since there isn't any patch to the xl domain configuration doc for this new option I'm not sure. Of course documentation is needed...

Also this should either be a bool, or perhaps a libxl_defbool if we want to do auto detection.

Ian.

#31 lore raw

Re: [PATCH v3 4/5] libxl: call hotplug scripts from libxl for vif

In reply to Roger Pau Monne
Quoted 4 lines · Roger Pau Monne

* Added fancy functions to fetch tap and vif names, now the prefix of
the tap
device has been saved in a constant, called TAP_DEVICE_PREFIX.

You missed the device name construction in
libxl__build_device_model_args_*.

Ian.

#32 lore raw

Re: [PATCH v3 4/5] libxl: call hotplug scripts from libxl for vif

Quoted 7 lines · Ian Campbell
Quoted 3 lines · Roger Pau Monne

* Added fancy functions to fetch tap and vif names, now the prefix of
the tap
device has been saved in a constant, called TAP_DEVICE_PREFIX.

You missed the device name construction in
libxl__build_device_model_args_*.

Didn't you have that on your patch? Anyway, please take this functions and merge them with your own series/patch, this is a mess.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#33 lore raw

PATCH v3 5/5 libxl: add "downscript=no" to Qemu call

In reply to Roger Pau Monne

Currently we only pass script=no to Qemu, to avoid calling any scripts when attaching a tap interface, but we should also pass downscript=no to avoid Qemu trying to execute a script when disconnecting the interface. This prevents the following harmless error message:

/etc/qemu-ifdown: could not launch network script

Changes since v1:

* Indentation fixes.

---
 tools/libxl/libxl_dm.c |   27 ++++++++++++++++++---------
 1 files changed, 18 insertions(+), 9 deletions(-)
tools/libxl/libxl_dm.c +18 -9
index 8cf9d9d..0f6c4cf 100644
@@ -216,11 +216,18 @@ static char ** libxl__build_device_model_args_old(libxl__gc *gc,
                 else
                     ifname = libxl__sprintf(gc, "xentap-%s", vifs[i].ifname);
                 flexarray_vappend(dm_args,
-                                "-net", libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s",
-                                                       vifs[i].devid, smac, vifs[i].model),
-                                "-net", libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,bridge=%s,script=%s",
-                                                       vifs[i].devid, ifname, vifs[i].bridge, libxl_tapif_script(gc)),
-                                NULL);
+                                  "-net",
+                                  libxl__sprintf(gc,
+                                      "nic,vlan=%d,macaddr=%s,model=%s",
+                                      vifs[i].devid, smac, vifs[i].model),
+                                  "-net",
+                                  libxl__sprintf(gc,
+                                      "tap,vlan=%d,ifname=%s,bridge=%s,"
+                                      "script=%s,downscript=%s",
+                                      vifs[i].devid, ifname, vifs[i].bridge,
+                                      libxl_tapif_script(gc),
+                                      libxl_tapif_script(gc)),
+                                  NULL);
                 ioemu_vifs++;
             }
         }
@@ -462,10 +469,12 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                                                 vifs[i].model, vifs[i].devid,
                                                 vifs[i].devid, smac));
                 flexarray_append(dm_args, "-netdev");
-                flexarray_append(dm_args,
-                   libxl__sprintf(gc, "type=tap,id=net%d,ifname=%s,script=%s",
-                                                vifs[i].devid, ifname,
-                                                libxl_tapif_script(gc)));
+                flexarray_append(dm_args, libxl__sprintf(gc, 
+                                          "type=tap,id=net%d,ifname=%s,"
+                                          "script=%s,downscript=%s",
+                                          vifs[i].devid, ifname,
+                                          libxl_tapif_script(gc),
+                                          libxl_tapif_script(gc)));
                 ioemu_vifs++;
             }
         }
Signature · 1 line
1.7.7.5 (Apple Git-26)
#34 lore raw

Re: [PATCH v3 5/5] libxl: add "downscript=no" to Qemu call

Roger Pau Monne writes ("[Xen-devel] [PATCH v3 5/5] libxl: add "downscript=no" to Qemu call"):

Quoted 10 lines

Currently we only pass script=no to Qemu, to avoid calling any scripts when attaching a tap interface, but we should also pass downscript=no to avoid Qemu trying to execute a script when disconnecting the interface. This prevents the following harmless error message:

/etc/qemu-ifdown: could not launch network script

Changes since v1:

* Indentation fixes.

This looks plausible but can you please split out the indentation change from the functional change ?

Thanks,
Ian.

#35 lore raw
In reply to Roger Pau Monne
Quoted 3 lines · Roger Pau Monne

This series removes the use of udev rules to call hotplug scripts when using libxl. Scripts are directly called from the toolstack at the necessary points, making use of the new event library and it's fork support.

What about non-dom0 backends? There will be no simple way to execute script there by libxl without help from udev...

In Qubes-OS we heavily use network backend in domU: dom0 have no network at all, all NICs are attached (as PCI device) to some domU - called NetVM, where network backend resides.

Also vbd backend in domU is used - eg to boot HVM from iso, which is stored in some domU.

Signature · 3 lines
Best Regards / Pozdrawiam,
Marek Marczykowski
Invisible Things Lab

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#36 lore raw
Quoted 7 lines · Marek Marczykowski
Quoted 3 lines · Roger Pau Monne

This series removes the use of udev rules to call hotplug scripts when using libxl. Scripts are directly called from the toolstack at the necessary points, making use of the new event library and it's fork support.

What about non-dom0 backends? There will be no simple way to execute script there by libxl without help from udev...

A new config option has been added on this series (disable_xl_vif_scripts) that allows the user to keep executing vif scripts from udev, so this functionality is not lost.

Quoted 3 lines

In Qubes-OS we heavily use network backend in domU: dom0 have no network at all, all NICs are attached (as PCI device) to some domU - called NetVM, where network backend resides.

There should be no problem with that, you will just need to use the new option.

Quoted 2 lines

Also vbd backend in domU is used - eg to boot HVM from iso, which is stored in some domU.

I didn't know you where able to use vbd from driver domains with xl, if so I will have to add a similar option for vbd devices (disable_xl_vbd_scripts).

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#37 lore raw
Quoted 17 lines · Roger Pau Monne
Quoted 7 lines · Marek Marczykowski
Quoted 3 lines · Roger Pau Monne

This series removes the use of udev rules to call hotplug scripts when using libxl. Scripts are directly called from the toolstack at the necessary points, making use of the new event library and it's fork support.

What about non-dom0 backends? There will be no simple way to execute script there by libxl without help from udev...

A new config option has been added on this series (disable_xl_vif_scripts) that allows the user to keep executing vif scripts from udev, so this functionality is not lost.

Quoted 3 lines

In Qubes-OS we heavily use network backend in domU: dom0 have no network at all, all NICs are attached (as PCI device) to some domU - called NetVM, where network backend resides.

Quoted 7 lines

There should be no problem with that, you will just need to use the new option.

Quoted 2 lines

Also vbd backend in domU is used - eg to boot HVM from iso, which is stored in some domU.

I didn't know you where able to use vbd from driver domains with xl, if so I will have to add a similar option for vbd devices (disable_xl_vbd_scripts).

When starting domU using xl create, I needed to slightly modify disk config syntax in xl_cmdimpl.c to add backend field (still using xen 4.1, backend added as the end of disk spec). But everything else worked fine. Especially xl block-attach, which allow to specify backend domain.
So disable_xl_vbd_scripts option will be helpful.

Signature · 3 lines
Best Regards / Pozdrawiam,
Marek Marczykowski
Invisible Things Lab

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#38 lore raw

Non-dom0 block backends (was: Re: [PATCH v3 0/5] libxl: call hotplug scripts from libxl)

On 04/23/12 15:47, Marek Marczykowski wrote:
/.../

Quoted 10 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Also vbd backend in domU is used - eg to boot HVM from iso, which is stored in some domU.

I didn't know you where able to use vbd from driver domains with xl, if so I will have to add a similar option for vbd devices (disable_xl_vbd_scripts).

When starting domU using xl create, I needed to slightly modify disk config syntax in xl_cmdimpl.c to add backend field (still using xen 4.1, backend added as the end of disk spec). But everything else worked fine. Especially xl block-attach, which allow to specify backend domain.
So disable_xl_vbd_scripts option will be helpful.

On a side note: some cool applications of this:

1) We can have a UsbVM, which has assigned all the USB controllers (pci attach), which greatly minimizes threats from various USB attacks [1] on the overall system. Now, if one plugs a USB disk, those disks can be made available to other domains, without the need for Dom0 to plug them (so no need to parse their, untrusted, partition tables, or other fs metadata).

2) We can store various installation ISOs, e.g. that cool new "hacker" Linux distro ISO, and pass it to an HVM domain (for installation) directly from the VM where we downloaded it (e.g. "untrusted-internet-browsing-vm") without the need to store it first on the Dom0 fs.

joanna.

[1]
http://theinvisiblethings.blogspot.com/2011/06/usb-security-challenges.html

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#39 lore raw
In reply to Roger Pau Monne
Quoted 6 lines · Roger Pau Monne
Quoted 5 lines · Marek Marczykowski
Quoted 3 lines · Roger Pau Monne

This series removes the use of udev rules to call hotplug scripts when using libxl. Scripts are directly called from the toolstack at the necessary points, making use of the new event library and it's fork support.

Quoted 6 lines
Quoted 2 lines

What about non-dom0 backends? There will be no simple way to execute script there by libxl without help from udev...

A new config option has been added on this series (disable_xl_vif_scripts) that allows the user to keep executing vif scripts from udev, so this functionality is not lost.

In the long term (e.g. for 4.3) we intend to overhaul this in a way which makes driver domains work without udev at all, see "Driver domains communication protocol proposal" posted by Ian Jackson several weeks ago -- it would be good to confirm that the scheme proposed there works well for Qubes-OS too.

In the short term (i.e. 4.2) we felt it was too late to be making these sorts of changes (e.g. implementing that complete protocol) and therefore the compromise is that xl will execute the scripts only in the case that dom0 is also the backend domain while for driver domains we retain the pre-4.2 behaviour of executing the hotplug scripts via udev inside the driver domain. This was necessary in order to fix things such as teardown of disks on NetBSD and teardown of NICs on openvswitch (currently both are broken even with backend = dom0 due to short comings in the previous approach) while not regressing the driver domain use case.

By default do we write the xenstore key to suppress udev running the scripts regardless of which domain the backend is in or only for backend=0? Or is it necessary to use the override config option for driver domain?

Ian.

Quoted 15 lines
Quoted 3 lines

In Qubes-OS we heavily use network backend in domU: dom0 have no network at all, all NICs are attached (as PCI device) to some domU - called NetVM, where network backend resides.

There should be no problem with that, you will just need to use the new option.

Quoted 2 lines

Also vbd backend in domU is used - eg to boot HVM from iso, which is stored in some domU.

I didn't know you where able to use vbd from driver domains with xl, if so I will have to add a similar option for vbd devices (disable_xl_vbd_scripts).

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#40 lore raw
Quoted 29 lines · Ian Campbell
Quoted 10 lines · Roger Pau Monne
Quoted 6 lines · Marek Marczykowski
Quoted 3 lines · Roger Pau Monne

This series removes the use of udev rules to call hotplug scripts when using libxl. Scripts are directly called from the toolstack at the necessary points, making use of the new event library and it's fork support.

What about non-dom0 backends? There will be no simple way to execute script there by libxl without help from udev...

A new config option has been added on this series (disable_xl_vif_scripts) that allows the user to keep executing vif scripts from udev, so this functionality is not lost.

In the long term (e.g. for 4.3) we intend to overhaul this in a way which makes driver domains work without udev at all, see "Driver domains communication protocol proposal" posted by Ian Jackson several weeks ago -- it would be good to confirm that the scheme proposed there works well for Qubes-OS too.

In the short term (i.e. 4.2) we felt it was too late to be making these sorts of changes (e.g. implementing that complete protocol) and therefore the compromise is that xl will execute the scripts only in the case that dom0 is also the backend domain while for driver domains we retain the pre-4.2 behaviour of executing the hotplug scripts via udev inside the driver domain. This was necessary in order to fix things such as teardown of disks on NetBSD and teardown of NICs on openvswitch (currently both are broken even with backend = dom0 due to short comings in the previous approach) while not regressing the driver domain use case.

Quoted 3 lines

By default do we write the xenstore key to suppress udev running the scripts regardless of which domain the backend is in or only for backend=0?

For vbd devices we write it everytime, for vifs devices we write it if disable_xl_vif_scripts is not set.

Quoted 2 lines

Or is it necessary to use the override config option for driver domain?

So the new proposal is to add a disable_xl_vbd_scripts and to detect if the device backend is different than dom0, if it is in fact different than dom0 don't execute hotplug scripts from xl (this will only work for vifs, because there's no support for setting the device domain for vbd devices yet).

Quoted 21 lines

Ian.

Quoted 17 lines
Quoted 3 lines

In Qubes-OS we heavily use network backend in domU: dom0 have no network at all, all NICs are attached (as PCI device) to some domU - called NetVM, where network backend resides.

There should be no problem with that, you will just need to use the new option.

Quoted 2 lines

Also vbd backend in domU is used - eg to boot HVM from iso, which is stored in some domU.

I didn't know you where able to use vbd from driver domains with xl, if so I will have to add a similar option for vbd devices (disable_xl_vbd_scripts).

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#41 lore raw
Quoted 37 lines · Roger Pau Monne
Quoted 29 lines · Ian Campbell
Quoted 10 lines · Roger Pau Monne
Quoted 6 lines · Marek Marczykowski
Quoted 3 lines · Roger Pau Monne

This series removes the use of udev rules to call hotplug scripts when using libxl. Scripts are directly called from the toolstack at the necessary points, making use of the new event library and it's fork support.

What about non-dom0 backends? There will be no simple way to execute script there by libxl without help from udev...

A new config option has been added on this series (disable_xl_vif_scripts) that allows the user to keep executing vif scripts from udev, so this functionality is not lost.

In the long term (e.g. for 4.3) we intend to overhaul this in a way which makes driver domains work without udev at all, see "Driver domains communication protocol proposal" posted by Ian Jackson several weeks ago -- it would be good to confirm that the scheme proposed there works well for Qubes-OS too.

In the short term (i.e. 4.2) we felt it was too late to be making these sorts of changes (e.g. implementing that complete protocol) and therefore the compromise is that xl will execute the scripts only in the case that dom0 is also the backend domain while for driver domains we retain the pre-4.2 behaviour of executing the hotplug scripts via udev inside the driver domain. This was necessary in order to fix things such as teardown of disks on NetBSD and teardown of NICs on openvswitch (currently both are broken even with backend = dom0 due to short comings in the previous approach) while not regressing the driver domain use case.

Quoted 3 lines

By default do we write the xenstore key to suppress udev running the scripts regardless of which domain the backend is in or only for backend=0?

For vbd devices we write it everytime, for vifs devices we write it if disable_xl_vif_scripts is not set.

Quoted 8 lines
Quoted 2 lines

Or is it necessary to use the override config option for driver domain?

So the new proposal is to add a disable_xl_vbd_scripts and to detect if the device backend is different than dom0, if it is in fact different than dom0 don't execute hotplug scripts from xl (this will only work for vifs, because there's no support for setting the device domain for vbd devices yet).

If we can detect I don't see why we shouldn't, unless it turns out to add complexity which we want to defer until 4.3.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel