Skip to contentSkip to conversation

libxl: call hotplug scripts from libxl

PATCH Superseded Roger Pau Monne 2 participants 22 messages Apr 17, 2012 – Apr 20, 2012 Arm/actions semi architecture

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 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. This series should be applied after Ian Jackson event fork and Ian Campbell support for named vifs.

#1 lore raw

PATCH v2 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 59992b6..16ebef3 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 fdeddc6..a09b38d 100644
@@ -991,7 +991,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 d486af2..2181774 100644
@@ -949,8 +949,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

PATCH v2 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.

---
 tools/libxl/libxl_device.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
tools/libxl/libxl_device.c +21 -0
index c7e057d..fe4bcc6 100644
@@ -356,6 +356,27 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,
     return -1;
 }
 
+static 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;
+}
 
 typedef struct {
     libxl__ao *ao;
Signature · 1 line
1.7.7.5 (Apple Git-26)
#3 lore raw

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

Quoted 3 lines · 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.

Can you put this in libxl_xshelp.c and a prototype in libxl_internal.h, please. Also it should include a comment in the header describing the function and what it does.

Quoted 38 lines
---
 tools/libxl/libxl_device.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
tools/libxl/libxl_device.c +21 -0
index c7e057d..fe4bcc6 100644
@@ -356,6 +356,27 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,
     return -1;
 }
 
+static 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;
+}
 
 typedef struct {
     libxl__ao *ao;
#4 lore raw

PATCH v2 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 device backend, 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 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                       |  107 +++++++++++++++++++++----
 tools/libxl/libxl.h                       |    7 +-
 tools/libxl/libxl_create.c                |    4 +-
 tools/libxl/libxl_device.c                |  124 ++++++++++++++++++++++++++---
 tools/libxl/libxl_dm.c                    |    4 +-
 tools/libxl/libxl_hotplug.c               |   67 ++++++++++++++++
 tools/libxl/libxl_internal.h              |   43 ++++++++++-
 tools/libxl/libxl_linux.c                 |  114 ++++++++++++++++++++++++++
 tools/libxl/xl_cmdimpl.c                  |   14 ++--
 tools/python/xen/lowlevel/xl/xl.c         |    5 +-
 13 files changed, 458 insertions(+), 46 deletions(-)
 create mode 100644 tools/libxl/libxl_hotplug.c
tools/hotplug/Linux/xen-backend.rules +3 -3
index 19bd0fa..ef8ef8e 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}="$XENBUS_PATH/disable_udev", RUN+="/etc/xen/scripts/blktap $env{ACTION}"
+SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{HOTPLUG_GATE}="$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}="$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 b30d11f..ac8b810 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 +91 -16

207 lines not on this page · raw message

tools/libxl/libxl.h +5 -2
index 50bae2f..b7347be 100644
@@ -394,7 +394,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 */
@@ -523,7 +524,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 +114 -10

198 lines not on this page · raw message

tools/libxl/libxl_dm.c +2 -2
index a09b38d..4779a78 100644
@@ -790,7 +790,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;
     }
@@ -1044,7 +1044,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 +42 -1

67 lines not on this page · raw message

tools/libxl/libxl_linux.c +114 -0

117 lines not on this page · raw message

tools/libxl/xl_cmdimpl.c +7 -7
index 08815a3..01ff363 100644
@@ -1301,7 +1301,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:
@@ -1862,7 +1862,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)
@@ -2339,7 +2339,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); }
 }
 
@@ -2613,7 +2613,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);
 }
@@ -2846,7 +2846,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);
 
@@ -2964,7 +2964,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);
@@ -5011,7 +5011,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 +3 -2
index bbbf2a9..a41a720 100644
@@ -444,9 +444,10 @@ static PyObject *pyxl_domain_reboot(XlObject *self, PyObject *args)
 static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args)
 {
     int domid;
-    if ( !PyArg_ParseTuple(args, "i", &domid) )
+    const libxl_asyncop_how *ao_how;
+    if ( !PyArg_ParseTuple(args, "ii", &domid, &ao_how) )
         return NULL;
-    if ( libxl_domain_destroy(self->ctx, domid) ) {
+    if ( libxl_domain_destroy(self->ctx, domid, ao_how) ) {
         PyErr_SetString(xl_error_obj, "cannot destroy domain");
         return NULL;
     }
Signature · 1 line
1.7.7.5 (Apple Git-26)
Acked-by on v2 3/5
#5 lore raw

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

Quoted 5 lines · Roger Pau Monne
tools/python/xen/lowlevel/xl/xl.c +0 -0
index bbbf2a9..a41a720 100644

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

Quoted 9 lines
tools/python/xen/lowlevel/xl/xl.c +3 -2
@@ -444,9 +444,10 @@ static PyObject *pyxl_domain_reboot(XlObject *self, PyObject *args)
 static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args)
 {
     int domid;
-    if ( !PyArg_ParseTuple(args, "i", &domid) )
+    const libxl_asyncop_how *ao_how;
+    if ( !PyArg_ParseTuple(args, "ii", &domid, &ao_how) )
         return NULL;
-    if ( libxl_domain_destroy(self->ctx, domid) ) {
+    if ( libxl_domain_destroy(self->ctx, domid, ao_how) ) {

Without a bunch more infrastructure to allow python to create and managed ao_how and callbacks etc there's not really much point in this and you might as well just declare that the python bindings as synchronous (i.e. pass NULL here).

Otherwise the poatch looks good:

Quoted 11 lines
tools/python/xen/lowlevel/xl/xl.c +0 -1
         PyErr_SetString(xl_error_obj, "cannot destroy domain");
         return NULL;
     }
--

1.7.7.5 (Apple Git-26)

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

#6 lore raw

PATCH v2 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 mantain the simmetry.

libxl__initiate_device_remove has been changed a little, to nuke the frontend xenstore path before trying to perform device teardown, this allows for unitialized 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 gobal 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 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/hotplug/Linux/xen-backend.rules |    6 +-
 tools/libxl/libxl.c                   |   90 +++++++-------------
 tools/libxl/libxl.h                   |    3 +-
 tools/libxl/libxl_create.c            |   22 +++++-
 tools/libxl/libxl_device.c            |   77 +++++++++++++++--
 tools/libxl/libxl_dm.c                |    2 +-
 tools/libxl/libxl_internal.h          |    6 ++
 tools/libxl/libxl_linux.c             |  150 ++++++++++++++++++++++++++++++++-
 tools/libxl/libxl_types.idl           |    1 +
 tools/libxl/xl.c                      |    4 +
 tools/libxl/xl.h                      |    1 +
 tools/libxl/xl_cmdimpl.c              |   15 +++-
 13 files changed, 308 insertions(+), 76 deletions(-)
docs/man/xl.conf.pod.5 +7 -0
index 8bd45ea..cf2c477 100644
@@ -55,6 +55,13 @@ default.
 
 Default: C<1>
 
+=item B<disable_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/hotplug/Linux/xen-backend.rules +3 -3
index ef8ef8e..35c4e13 100644
@@ -2,8 +2,8 @@ SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{HOTPLUG_GATE}="$XENBUS_PATH/disabl
 SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{HOTPLUG_GATE}="$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}="$XENBUS_PATH/disable_udev", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif"
+SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{HOTPLUG_GATE}="$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}="$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=="xentap*", ACTION=="add", ENV{HOTPLUG_GATE}="$XENBUS_PATH/disable_udev", RUN+="/etc/xen/scripts/vif-setup $env{ACTION} type_if=tap"
tools/libxl/libxl.c +30 -60

126 lines not on this page · raw message

tools/libxl/libxl.h +2 -1
index b7347be..6da6107 100644
@@ -551,7 +551,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..a1f5731 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_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 +70 -7

112 lines not on this page · raw message

tools/libxl/libxl_dm.c +1 -1
index 4779a78..04f76c2 100644
@@ -795,7 +795,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 +6 -0
index a39346c..3787217 100644
@@ -756,6 +756,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,
tools/libxl/libxl_linux.c +148 -2

184 lines not on this page · raw message

tools/libxl/libxl_types.idl +1 -0
index 09089b2..ac3e79f 100644
@@ -343,6 +343,7 @@ libxl_device_nic = Struct("device_nic", [
     ("ifname", string),
     ("script", string),
     ("nictype", libxl_nic_type),
+    ("disable_vif_script", integer),
     ])
 
 libxl_device_pci = Struct("device_pci", [
tools/libxl/xl.c +4 -0
index a6ffd25..2a705b8 100644
@@ -35,6 +35,7 @@
 xentoollog_logger_stdiostream *logger;
 int dryrun_only;
 int autoballoon = 1;
+int disable_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_vif_scripts", &l, 0))
+        disable_vif_scripts = l;
+
     if (!xlu_cfg_get_string (config, "lockfile", &buf, 0))
         lockfile = strdup(buf);
     else {
tools/libxl/xl.h +1 -0
index 7e258d5..13619e7 100644
@@ -109,6 +109,7 @@ void postfork(void);
 
 /* global options */
 extern int autoballoon;
+extern int disable_vif_scripts;
 extern int dryrun_only;
 extern char *lockfile;
 extern char *default_vifscript;
tools/libxl/xl_cmdimpl.c +14 -1
index 01ff363..41230a6 100644
@@ -846,6 +846,19 @@ static void parse_config_data(const char *configfile_filename_report,
                 nic->script = strdup(default_vifscript);
             }
 
+            /* Set default nic type for PV guests correctly */
+            if (b_info->type == LIBXL_DOMAIN_TYPE_PV) {
+                nic->nictype = LIBXL_NIC_TYPE_VIF;
+            }
+
+            /*
+             * Disable nic network script calling, to allow the user
+             * to attach the nic backend from a different domain.
+             */
+            if (disable_vif_scripts) {
+                nic->disable_vif_script = 1;
+            }
+
 	    if (default_bridge) {
 		free(nic->bridge);
 		nic->bridge = strdup(default_bridge);
@@ -4901,7 +4914,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)
#7 lore raw

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

Quoted 15 lines · 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.

Quoted 3 lines

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 mantain the simmetry.

"maintain" and "symmetry"

These are pure code motion or did the code actually change too?

Quoted 3 lines

libxl__initiate_device_remove has been changed a little, to nuke the frontend xenstore path before trying to perform device teardown, this allows for unitialized devices to be closed gracefully, specially vif interfaces added to

uninitialized (or -ised)

Quoted 6 lines

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 gobal option, called disable_vif_scripts has been added to allow the user

global

Quoted 2 lines

decide if he wants to execute the hotplug scripts from xl or from udev. This has been documented in the xl.conf man page.

Did you do this for block too?

Quoted 32 lines

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/hotplug/Linux/xen-backend.rules |    6 +-
 tools/libxl/libxl.c                   |   90 +++++++-------------
 tools/libxl/libxl.h                   |    3 +-
 tools/libxl/libxl_create.c            |   22 +++++-
 tools/libxl/libxl_device.c            |   77 +++++++++++++++--
 tools/libxl/libxl_dm.c                |    2 +-
 tools/libxl/libxl_internal.h          |    6 ++
 tools/libxl/libxl_linux.c             |  150 ++++++++++++++++++++++++++++++++-
 tools/libxl/libxl_types.idl           |    1 +
 tools/libxl/xl.c                      |    4 +
 tools/libxl/xl.h                      |    1 +
 tools/libxl/xl_cmdimpl.c              |   15 +++-
 13 files changed, 308 insertions(+), 76 deletions(-)
docs/man/xl.conf.pod.5 +0 -0
index 8bd45ea..cf2c477 100644

docs/man/xl.conf.pod.5 default. · line 63

Quoted 9 lines
docs/man/xl.conf.pod.5 +6 -0
@@ -55,6 +55,13 @@ default.
 
 Default: C<1>
 
+=item B<disable_vif_scripts=BOOLEAN>
+
+If enabled xl will not execute nic hotplug scripts itself, and instead
+relegate this task to udev.
+
+Default: C<0>

Please can you also add a commented out version
to ./tools/examples/xl.conf, with the value of the default.

This doesn't actually disable the scripts entirely, but rather only from udev, disable_udev_vif_scripts perhaps?

Quoted 4 lines
docs/man/xl.conf.pod.5 +0 -0
 =item B<lockfile="PATH">
 
 Sets the path to the lock file used by xl to serialise certain

docs/man/xl.conf.pod.5 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic) · line 1891

Quoted 10 lines
docs/man/xl.conf.pod.5 +6 -1
@@ -1929,14 +1883,30 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
                              libxl__xs_kvs_of_flexarray(gc, back, back->count),
                              libxl__xs_kvs_of_flexarray(gc, front, front->count));
 
-    /* FIXME: wait for plug */
+    switch(nic->nictype) {
+    case LIBXL_NIC_TYPE_VIF:
+        if (!nic->disable_vif_script) {
+            rc = libxl__initiate_device_add(egc, ao, &device);
+            if (rc) goto out_free;
+        }

You don't need an ao_complete in the else case?

docs/man/xl.conf.pod.5 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic) · line 1897

Quoted 6 lines
docs/man/xl.conf.pod.5 +6 -0
+        break;
+    case LIBXL_NIC_TYPE_IOEMU:
+        /*
+         * Don't execute hotplug scripts for IOEMU interfaces yet,
+         * we need to launch the device model first.
+        */

It'd be useful to add a reference to where these are run, at first I thought this comment (and the patch to xen-backend.rules) meant they weren't being run at all.

Quoted 19 lines
tools/libxl/libxl_create.c +3 -1
index de598ad..a1f5731 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

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

Quoted 10 lines
tools/libxl/libxl_create.c +10 -0
+         * 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_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);

I should have asked this in 3/5 but does this function not need to be async, since the script might take a while and we need to wait for it to complete before continuing?

Quoted 11 lines
tools/libxl/libxl_create.c +8 -0
+                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++)

[...]

Quoted 8 lines
tools/libxl/libxl_create.c +1 -1
@@ -450,22 +504,29 @@ int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao,
 {
     AO_GC;
     libxl_ctx *ctx = libxl__gc_owner(gc);
-    xs_transaction_t t;
+    xs_transaction_t t = 0;
     char *be_path = libxl__device_backend_path(gc, dev);
     char *state_path = libxl__sprintf(gc, "%s/state", be_path);

tools/libxl/libxl_create.c int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao, · line 518

Quoted 10 lines
tools/libxl/libxl_create.c +6 -1
-    char *state = libxl__xs_read(gc, XBT_NULL, state_path);
+    char *state;
     int rc = 0;
     libxl__ao_device_remove *aorm = 0;
 
+    /*
+     * Nuke frontend to force backend teardown
+     * It's not pretty, but it's the only way that seems to work for all
+     * types of backends
+     */

I'm still not happy with this. The _remove functions are supposed to be a graceful + co-operative remove, that means prodding the front and backend into doing the controlled teardown dance.

This may well take some time and may even fail after some timeout depending on the device type, guest configuration and co-operation etc, but that is expected and should be handled.

Forcing things in this way is appropriate for device_destroy only IMHO since that is the function which is provided for use when the guest is not co-operating.

Quoted 45 lines
tools/libxl/libxl_create.c +9 -5
+    libxl__xs_path_cleanup(gc, libxl__device_frontend_path(gc, dev));
+
+retry_transaction:
+    t = xs_transaction_start(ctx->xsh);
+    state = libxl__xs_read(gc, t, state_path);
     if (!state)
         goto out_ok;
-    if (atoi(state) != 4) {
+    if (atoi(state) == XenbusStateClosed)
         libxl__device_destroy_tapdisk(gc, be_path);
         goto out_ok;
-    }
 
-retry_transaction:
-    t = xs_transaction_start(ctx->xsh);
     xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/online", be_path), "0", strlen("0"));
     xs_write(ctx->xsh, t, state_path, "5", strlen("5"));
     if (!xs_transaction_end(ctx->xsh, t, 0)) {
@@ -476,6 +537,8 @@ retry_transaction:
             goto out_fail;
         }
     }
+    /* mark transaction as ended, to prevent double closing it on out_ok */
+    t = 0;
 
     libxl__device_destroy_tapdisk(gc, be_path);
 
@@ -497,8 +560,8 @@ retry_transaction:
     return rc;
 
  out_ok:
+    if (t) xs_transaction_end(ctx->xsh, t, 0);
     rc = libxl__device_hotplug(gc, dev, DISCONNECT);
-    libxl__xs_path_cleanup(gc, libxl__device_frontend_path(gc, dev));
     libxl__xs_path_cleanup(gc, be_path);
     return 0;
 }
tools/libxl/libxl_linux.c +0 -0
index c5583af..1ef282f 100644
@@ -27,11 +27,30 @@ int libxl__try_phy_backend(mode_t st_mode)
 }
 
 /* Hotplug scripts helpers */

tools/libxl/libxl_linux.c int libxl__try_phy_backend(mode_t st_mode) · line 39

Quoted 10 lines
tools/libxl/libxl_linux.c +10 -0
+
+static libxl_nic_type get_nic_type(libxl__gc *gc, libxl__device *dev)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    char *snictype, *be_path;
+
+    be_path = libxl__device_backend_path(gc, dev);
+
+    snictype = libxl__xs_read(gc, XBT_NULL,
+                            libxl__sprintf(gc, "%s/%s", be_path, "type"));

This really ought to be under some private libxl path relating to the device rather than under the backend/frontend visible dirs.

Actually, now I think of it, that is also true of the udev_disable key?

You could also use the handy enum to/from_strings functions to leave one less magic number in xenstore.

Quoted 17 lines
tools/libxl/libxl_linux.c +13 -0
+    if (!snictype) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable to read nictype from %s",
+                                          be_path);
+        return -1;
+    }
+
+    return atoi(snictype);
+}
+
@@ -62,6 +81,35 @@ static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev)
                   dev->domid, dev->devid));
     flexarray_set(f_env, nr++, "XENBUS_BASE_PATH");
     flexarray_set(f_env, nr++, "backend");
+    if (dev->backend_kind == LIBXL__DEVICE_KIND_VIF) {
+        ifname = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/%s",
+                                                                 be_path,
+                                                                 "vifname"));

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 97

Quoted 10 lines
tools/libxl/libxl_linux.c +10 -0
+        switch (get_nic_type(gc, dev)) {
+        case LIBXL_NIC_TYPE_IOEMU:
+            flexarray_set(f_env, nr++, "INTERFACE");
+            if (ifname) {
+                flexarray_set(f_env, nr++, libxl__sprintf(gc, "xentap-%s",
+                                                              ifname));
+            } else {
+                flexarray_set(f_env, nr++,
+                              libxl__sprintf(gc, "xentap%u.%d",
+                              dev->domid, dev->devid));

This is cut-n-paste of some other code, perhaps create a function to get the tap name from the vif name and or dom/devid?

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 107

Quoted 10 lines
tools/libxl/libxl_linux.c +10 -0
+            }
+        case LIBXL_NIC_TYPE_VIF:
+            flexarray_set(f_env, nr++, "vif");
+            if (ifname) {
+                flexarray_set(f_env, nr++, ifname);
+            } else {
+                flexarray_set(f_env, nr++,
+                              libxl__sprintf(gc, "vif%u.%d",
+                              dev->domid, dev->devid));
+            }

Likewise

Quoted 13 lines
tools/libxl/libxl_linux.c +5 -0
+            break;
+        default:
+            return NULL;
+        }
+    }
 
     flexarray_set(f_env, nr++, NULL);
 
tools/libxl/xl_cmdimpl.c +0 -0
index 01ff363..41230a6 100644

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char *configfile_filename_report, · line 852

Quoted 7 lines
tools/libxl/xl_cmdimpl.c +4 -0
@@ -846,6 +846,19 @@ static void parse_config_data(const char *configfile_filename_report,
                 nic->script = strdup(default_vifscript);
             }
 
+            /* Set default nic type for PV guests correctly */
+            if (b_info->type == LIBXL_DOMAIN_TYPE_PV) {
+                nic->nictype = LIBXL_NIC_TYPE_VIF;
+            }

Hrm, really the lib ought to be taking care of that for us...

libxl__device_nic_setdefault has a domid so it should be able to query the domain type with libxl__domain_type.

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char *configfile_filename_report, · line 860

Quoted 7 lines
tools/libxl/xl_cmdimpl.c +7 -0
+            /*
+             * Disable nic network script calling, to allow the user
+             * to attach the nic backend from a different domain.
+             */
+            if (disable_vif_scripts) {
+                nic->disable_vif_script = 1;
+            }

This is equivalent to :

		nic->disable_vif_script = disable_vif_scripts
Quoted 21 lines
tools/libxl/xl_cmdimpl.c +2 -2
+
            if (default_bridge) {
                free(nic->bridge);
                nic->bridge = strdup(default_bridge);
@@ -4901,7 +4914,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;
     }
--

1.7.7.5 (Apple Git-26)

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

#8 lore raw

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

Quoted 16 lines · Ian Campbell
Quoted 15 lines · 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.

Quoted 7 lines
Quoted 3 lines

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 mantain the simmetry.

"maintain" and "symmetry"

These are pure code motion or did the code actually change too?

The code didn't change, I just moved it around.

Quoted 12 lines
Quoted 4 lines

libxl__initiate_device_remove has been changed a little, to nuke the frontend xenstore path before trying to perform device teardown, this allows for unitialized devices to be closed gracefully, specially vif interfaces added to

uninitialized (or -ised)

Quoted 5 lines

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.

Quoted 8 lines
Quoted 1 line

A new gobal option, called disable_vif_scripts has been added to allow the user

global

Quoted 2 lines

decide if he wants to execute the hotplug scripts from xl or from udev. This has been documented in the xl.conf man page.

Did you do this for block too?

Nope, only for vif interfaces, that are the only ones that have some kind of limited support for the driver domains thing when called from udev.

Quoted 47 lines
Quoted 40 lines

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/hotplug/Linux/xen-backend.rules |    6 +-
tools/libxl/libxl.c                   |   90 +++++++-------------
tools/libxl/libxl.h                   |    3 +-
tools/libxl/libxl_create.c            |   22 +++++-
tools/libxl/libxl_device.c            |   77 +++++++++++++++--
tools/libxl/libxl_dm.c                |    2 +-
tools/libxl/libxl_internal.h          |    6 ++
tools/libxl/libxl_linux.c             |  150 
++++++++++++++++++++++++++++++++-
tools/libxl/libxl_types.idl           |    1 +
tools/libxl/xl.c                      |    4 +
tools/libxl/xl.h                      |    1 +
tools/libxl/xl_cmdimpl.c              |   15 +++-
13 files changed, 308 insertions(+), 76 deletions(-)
docs/man/xl.conf.pod.5 +0 -0
index 8bd45ea..cf2c477 100644
@@ -55,6 +55,13 @@ default.
 

Default: C<1>

+=item B<disable_vif_scripts=BOOLEAN>

docs/man/xl.conf.pod.5 +2 -0
+
+If enabled xl will not execute nic hotplug scripts itself, and 

instead

docs/man/xl.conf.pod.5 default. · line 63

Quoted 3 lines
docs/man/xl.conf.pod.5 +3 -0
+relegate this task to udev.
+
+Default: C<0>

Please can you also add a commented out version
to ./tools/examples/xl.conf, with the value of the default.

Quoted 2 lines

This doesn't actually disable the scripts entirely, but rather only from udev, disable_udev_vif_scripts perhaps?

It actually disables script calling from libxl, so I think it might be best to call it disable_xl_vif_scripts?

Quoted 11 lines
Quoted 11 lines

=item B<lockfile="PATH">

Sets the path to the lock file used by xl to serialise certain

docs/man/xl.conf.pod.5 +0 -0
@@ -1929,14 +1883,30 @@ int libxl_device_nic_add(libxl_ctx *ctx, 
 uint32_t domid, libxl_device_nic *nic)
                         libxl__xs_kvs_of_flexarray(gc, back, 
 back->count),
                         libxl__xs_kvs_of_flexarray(gc, front, 
 front->count));
 
Quoted 9 lines

docs/man/xl.conf.pod.5 int libxl_device_nic_add(libxl_ctx *ctx, · line 1894

Quoted 7 lines
docs/man/xl.conf.pod.5 +6 -1
-    /* FIXME: wait for plug */
+    switch(nic->nictype) {
+    case LIBXL_NIC_TYPE_VIF:
+        if (!nic->disable_vif_script) {
+            rc = libxl__initiate_device_add(egc, ao, &device);
+            if (rc) goto out_free;
+        }

You don't need an ao_complete in the else case?

Sure, good one, I think this kind of mechanism is prone to errors… AO_INPROGRESS should call ao_complete if no events have been added.

Quoted 10 lines

docs/man/xl.conf.pod.5 int libxl_device_nic_add(libxl_ctx *ctx, · line 1900

Quoted 6 lines
docs/man/xl.conf.pod.5 +6 -0
+        break;
+    case LIBXL_NIC_TYPE_IOEMU:
+        /*
+         * Don't execute hotplug scripts for IOEMU interfaces yet,
+         * we need to launch the device model first.
+        */

It'd be useful to add a reference to where these are run, at first I thought this comment (and the patch to xen-backend.rules) meant they weren't being run at all.

Done I've added a small note saying where they are called.

Quoted 31 lines
Quoted 31 lines
tools/libxl/libxl_create.c +1 -1
index de598ad..a1f5731 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);
tools/libxl/libxl_create.c +2 -0
@@ -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

tools/libxl/libxl_create.c +7 -0
+         * 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_vif_script) {
+                libxl__device device;
+                ret = libxl__device_from_nic(gc, domid, 
Quoted 8 lines

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

Quoted 4 lines

&d_config->vifs[i],

tools/libxl/libxl_create.c +3 -0
+                                             &device);
+                if (ret < 0) goto error_out;
+                ret = libxl__device_hotplug(gc, &device, CONNECT);

I should have asked this in 3/5 but does this function not need to be async, since the script might take a while and we need to wait for it to complete before continuing?

Are you sure we need to wait for it to finish? There wasn't any kind of synchronization in the past when we called the scripts from udev, so I guess we don't have to wait either for them to finish.

Quoted 39 lines
Quoted 13 lines
tools/libxl/libxl_create.c +3 -0
+                if (ret < 0) {
+                    LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                               "unable to launch hotplug script for 

device: "

tools/libxl/libxl_create.c +5 -0
+                               "%"PRIu32, device.devid);
+                    goto error_out;
+                }
+            }
+        }

}

for (i = 0; i < d_config->num_pcidevs; i++)

[...]

Quoted 18 lines
tools/libxl/libxl_create.c +5 -2
@@ -450,22 +504,29 @@ int libxl__initiate_device_remove(libxl__egc 
 *egc, libxl__ao *ao,
 {
 AO_GC;
 libxl_ctx *ctx = libxl__gc_owner(gc);
-    xs_transaction_t t;
+    xs_transaction_t t = 0;
 char *be_path = libxl__device_backend_path(gc, dev);
 char *state_path = libxl__sprintf(gc, "%s/state", be_path);
-    char *state = libxl__xs_read(gc, XBT_NULL, state_path);
+    char *state;
 int rc = 0;
 libxl__ao_device_remove *aorm = 0;
 
+    /*
+     * Nuke frontend to force backend teardown
+     * It's not pretty, but it's the only way that seems to work for 

all

tools/libxl/libxl_create.c

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+     * types of backends
+     */

I'm still not happy with this. The _remove functions are supposed to be a graceful + co-operative remove, that means prodding the front and backend into doing the controlled teardown dance.

Quoted 7 lines

This may well take some time and may even fail after some timeout depending on the device type, guest configuration and co-operation etc, but that is expected and should be handled.

Forcing things in this way is appropriate for device_destroy only IMHO since that is the function which is provided for use when the guest is not co-operating.

Before this patch, we used to just nuke both front and back xenstore directories (because we always called libxl__device_destroy), so I think this is quite and improvement in term of co-operation than what we had before. We only used this kind of negotiation when detaching a block from a live guest.

Quoted 48 lines
Quoted 48 lines

+ libxl__xs_path_cleanup(gc, libxl__device_frontend_path(gc,
dev));

tools/libxl/libxl_create.c +4 -0
+
+retry_transaction:
+    t = xs_transaction_start(ctx->xsh);
+    state = libxl__xs_read(gc, t, state_path);

if (!state)

    goto out_ok;
tools/libxl/libxl_create.c +1 -4
-    if (atoi(state) != 4) {
+    if (atoi(state) == XenbusStateClosed)
    libxl__device_destroy_tapdisk(gc, be_path);
    goto out_ok;
-    }
 
-retry_transaction:
-    t = xs_transaction_start(ctx->xsh);

xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/online", be_path), "0", strlen("0")); xs_write(ctx->xsh, t, state_path, "5", strlen("5"));
if (!xs_transaction_end(ctx->xsh, t, 0)) {

tools/libxl/libxl_create.c +1 -0
@@ -476,6 +537,8 @@ retry_transaction:
        goto out_fail;
    }
 }
+    /* mark transaction as ended, to prevent double closing it on 

out_ok */
+ t = 0;

libxl__device_destroy_tapdisk(gc, be_path);

tools/libxl/libxl_create.c +0 -0
@@ -497,8 +560,8 @@ retry_transaction:
 return rc;
 

out_ok:
+ if (t) xs_transaction_end(ctx->xsh, t, 0);
rc = libxl__device_hotplug(gc, dev, DISCONNECT);
- libxl__xs_path_cleanup(gc, libxl__device_frontend_path(gc,
dev));
libxl__xs_path_cleanup(gc, be_path);
return 0;
}

tools/libxl/libxl_linux.c +0 -0
index c5583af..1ef282f 100644
@@ -27,11 +27,30 @@ int libxl__try_phy_backend(mode_t st_mode)
 }
 
Quoted 16 lines

tools/libxl/libxl_linux.c int libxl__try_phy_backend(mode_t st_mode)

Quoted 13 lines
tools/libxl/libxl_linux.c +10 -0
 /* Hotplug scripts helpers */
+
+static libxl_nic_type get_nic_type(libxl__gc *gc, libxl__device 
 *dev)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    char *snictype, *be_path;
+
+    be_path = libxl__device_backend_path(gc, dev);
+
+    snictype = libxl__xs_read(gc, XBT_NULL,
+                            libxl__sprintf(gc, "%s/%s", be_path, 
 "type"));

This really ought to be under some private libxl path relating to the device rather than under the backend/frontend visible dirs.

Well, this is currently only used by libxl, but the type of the backend used I think should be inside the backend device directory, since other applications might also want to know this.

Quoted 1 line

Actually, now I think of it, that is also true of the udev_disable key?

This is probably true for udev_disable, something like /libxl/devices/<domid>/<devid>/udev_disable? I will have to modify libxl__device_generic_add to do this, because it should be done on the same transaction when the device is added. I'm not sure if we need to add so much overhead for just one xenstore entry, that will go away in the next release probably.

Quoted 2 lines

You could also use the handy enum to/from_strings functions to leave one less magic number in xenstore.

Yes.

Quoted 30 lines
Quoted 30 lines
tools/libxl/libxl_linux.c +13 -0
+    if (!snictype) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable to read nictype 
 from %s",
+                                          be_path);
+        return -1;
+    }
+
+    return atoi(snictype);
+}
+
@@ -62,6 +81,35 @@ static char **get_hotplug_env(libxl__gc *gc, 
 libxl__device *dev)
              dev->domid, dev->devid));
 flexarray_set(f_env, nr++, "XENBUS_BASE_PATH");
 flexarray_set(f_env, nr++, "backend");
+    if (dev->backend_kind == LIBXL__DEVICE_KIND_VIF) {
+        ifname = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, 
 "%s/%s",
+                                                                 
 be_path,
+                                                                 

"vifname"));

tools/libxl/libxl_linux.c +5 -0
+        switch (get_nic_type(gc, dev)) {
+        case LIBXL_NIC_TYPE_IOEMU:
+            flexarray_set(f_env, nr++, "INTERFACE");
+            if (ifname) {
+                flexarray_set(f_env, nr++, libxl__sprintf(gc, 

"xentap-%s",
+

Quoted 8 lines

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 97

Quoted 5 lines

ifname));

tools/libxl/libxl_linux.c +4 -0
+            } else {
+                flexarray_set(f_env, nr++,
+                              libxl__sprintf(gc, "xentap%u.%d",
+                              dev->domid, dev->devid));

This is cut-n-paste of some other code, perhaps create a function to get the tap name from the vif name and or dom/devid?

Are you going to add it to your patch or I should add it to mine? Something like:

char *libxl__device_tap_name(libxl__gc *gc, libxl__device *dev)

Quoted 11 lines

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 107

Quoted 10 lines
tools/libxl/libxl_linux.c +10 -0
+            }
+        case LIBXL_NIC_TYPE_VIF:
+            flexarray_set(f_env, nr++, "vif");
+            if (ifname) {
+                flexarray_set(f_env, nr++, ifname);
+            } else {
+                flexarray_set(f_env, nr++,
+                              libxl__sprintf(gc, "vif%u.%d",
+                              dev->domid, dev->devid));
+            }
Quoted 1 line

Likewise

char *libxl__device_vif_name(libxl__gc *gc, libxl__device *dev)

Both this functions should return the correct vifname if one is set, or the default one otherwise.

Quoted 24 lines
Quoted 13 lines
tools/libxl/libxl_linux.c +5 -0
+            break;
+        default:
+            return NULL;
+        }
+    }

flexarray_set(f_env, nr++, NULL);

tools/libxl/xl_cmdimpl.c +0 -0
index 01ff363..41230a6 100644

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char · line 853

Quoted 8 lines
tools/libxl/xl_cmdimpl.c +4 -0
@@ -846,6 +846,19 @@ static void parse_config_data(const char 
 *configfile_filename_report,
            nic->script = strdup(default_vifscript);
        }
 
+            /* Set default nic type for PV guests correctly */
+            if (b_info->type == LIBXL_DOMAIN_TYPE_PV) {
+                nic->nictype = LIBXL_NIC_TYPE_VIF;
+            }

Hrm, really the lib ought to be taking care of that for us...

Quoted 2 lines

libxl__device_nic_setdefault has a domid so it should be able to query the domain type with libxl__domain_type.

Yes.

Quoted 10 lines

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char *configfile_filename_report, · line 860

Quoted 7 lines
tools/libxl/xl_cmdimpl.c +7 -0
+            /*
+             * Disable nic network script calling, to allow the user
+             * to attach the nic backend from a different domain.
+             */
+            if (disable_vif_scripts) {
+                nic->disable_vif_script = 1;
+            }

This is equivalent to :

           nic->disable_vif_script = disable_vif_scripts

Yes, and it's one line less.

Quoted 18 lines
Quoted 18 lines
tools/libxl/xl_cmdimpl.c +2 -2
+
       if (default_bridge) {
           free(nic->bridge);
           nic->bridge = strdup(default_bridge);
@@ -4901,7 +4914,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;
 }
--

1.7.7.5 (Apple Git-26)

Quoted 4 lines
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

#9 lore raw

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

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

decide if he wants to execute the hotplug scripts from xl or from udev. This has
been documented in the xl.conf man page.

Did you do this for block too?

Nope, only for vif interfaces, that are the only ones that have some kind of limited support for the driver domains thing when called from udev.

OK, makes sense.

Quoted 15 lines
Quoted 15 lines

docs/man/xl.conf.pod.5 default. · line 63

Quoted 11 lines
(quoted diff) +0 -0
@@ -55,6 +55,13 @@ default.
 

Default: C<1>

+=item B<disable_vif_scripts=BOOLEAN>

(quoted diff) +2 -0
+
+If enabled xl will not execute nic hotplug scripts itself, and

instead

docs/man/xl.conf.pod.5 +3 -0
+relegate this task to udev.
+
+Default: C<0>

Please can you also add a commented out version
to ./tools/examples/xl.conf, with the value of the default.

Quoted 5 lines
Quoted 2 lines

This doesn't actually disable the scripts entirely, but rather only from > udev, disable_udev_vif_scripts perhaps?

It actually disables script calling from libxl, so I think it might be best to call it disable_xl_vif_scripts?

Oh, I was confusing it with the xenstore key used by the scripts! I think your existing name is fine then.

Quoted 41 lines
Quoted 32 lines
Quoted 32 lines
tools/libxl/libxl_create.c +1 -1
index de598ad..a1f5731 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);
tools/libxl/libxl_create.c +2 -0
@@ -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

tools/libxl/libxl_create.c +7 -0
+         * 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_vif_script) {
+                libxl__device device;
+                ret = libxl__device_from_nic(gc, domid,

&d_config->vifs[i],

Quoted 8 lines

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

Quoted 3 lines
tools/libxl/libxl_create.c +3 -0
+                                             &device);
+                if (ret < 0) goto error_out;
+                ret = libxl__device_hotplug(gc, &device, CONNECT);

I should have asked this in 3/5 but does this function not need to be async, since the script might take a while and we need to wait for it to
complete before continuing?

Quoted 1 line

Are you sure we need to wait for it to finish?

Not at all..

Quoted 3 lines

There wasn't any kind of
synchronization in the past when we called the scripts from udev, so I guess we don't have to wait either for them to finish.

You are right, I don't think xl currently waits.

xend used to (IIRC) wait for the hotplug-status node to be written and used this to do some basic error handling, waiting doe that node is roughly equivalent to waiting for the script to finish.

Given that xl today doesn't wait I guess I'd be happy to defer that to the future.

Quoted 31 lines
Quoted 31 lines
Quoted 18 lines
tools/libxl/libxl_create.c +5 -2
@@ -450,22 +504,29 @@ int libxl__initiate_device_remove(libxl__egc
 *egc, libxl__ao *ao,
 {
 AO_GC;
 libxl_ctx *ctx = libxl__gc_owner(gc);
-    xs_transaction_t t;
+    xs_transaction_t t = 0;
 char *be_path = libxl__device_backend_path(gc, dev);
 char *state_path = libxl__sprintf(gc, "%s/state", be_path);
-    char *state = libxl__xs_read(gc, XBT_NULL, state_path);
+    char *state;
 int rc = 0;
 libxl__ao_device_remove *aorm = 0;
 
+    /*
+     * Nuke frontend to force backend teardown
+     * It's not pretty, but it's the only way that seems to work for

all

tools/libxl/libxl_create.c

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+     * types of backends
+     */

I'm still not happy with this. The _remove functions are supposed to be
a graceful + co-operative remove, that means prodding the front and backend into doing the controlled teardown dance.

This may well take some time and may even fail after some timeout depending on the device type, guest configuration and co-operation etc,
but that is expected and should be handled.

Quoted 6 lines
Quoted 3 lines

Forcing things in this way is appropriate for device_destroy only IMHO since that is the function which is provided for use when the guest is not co-operating.

Before this patch, we used to just nuke both front and back xenstore directories (because we always called libxl__device_destroy),

Not always, the call to destroy depended on the current state.

In the case where the guest is alive and responsive we have to do the hot remove in the graceful way. If the guest is alive but not responsive then the correct approach is to try the graceful method with a timeout which triggers the big hammer approach.

Quoted 71 lines

so I think
this is quite and improvement in term of co-operation than what we had before. We only used this kind of negotiation when detaching a block from a live guest.

Quoted 49 lines
Quoted 49 lines

+ libxl__xs_path_cleanup(gc, libxl__device_frontend_path(gc,
dev));

tools/libxl/libxl_create.c +4 -0
+
+retry_transaction:
+    t = xs_transaction_start(ctx->xsh);
+    state = libxl__xs_read(gc, t, state_path);

if (!state)

    goto out_ok;
tools/libxl/libxl_create.c +1 -4
-    if (atoi(state) != 4) {
+    if (atoi(state) == XenbusStateClosed)
    libxl__device_destroy_tapdisk(gc, be_path);
    goto out_ok;
-    }
 
-retry_transaction:
-    t = xs_transaction_start(ctx->xsh);

xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/online", be_path), "0", strlen("0"));
xs_write(ctx->xsh, t, state_path, "5", strlen("5"));
if (!xs_transaction_end(ctx->xsh, t, 0)) {

tools/libxl/libxl_create.c +1 -0
@@ -476,6 +537,8 @@ retry_transaction:
        goto out_fail;
    }
 }
+    /* mark transaction as ended, to prevent double closing it on

out_ok */
+ t = 0;

libxl__device_destroy_tapdisk(gc, be_path);

tools/libxl/libxl_create.c +0 -0
@@ -497,8 +560,8 @@ retry_transaction:
 return rc;
 

out_ok:
+ if (t) xs_transaction_end(ctx->xsh, t, 0);
rc = libxl__device_hotplug(gc, dev, DISCONNECT);
- libxl__xs_path_cleanup(gc, libxl__device_frontend_path(gc,
dev));
libxl__xs_path_cleanup(gc, be_path);
return 0;
}

tools/libxl/libxl_linux.c +0 -0
index c5583af..1ef282f 100644
@@ -27,11 +27,30 @@ int libxl__try_phy_backend(mode_t st_mode)
 }
 
Quoted 16 lines

tools/libxl/libxl_linux.c int libxl__try_phy_backend(mode_t st_mode)

Quoted 13 lines
tools/libxl/libxl_linux.c +10 -0
 /* Hotplug scripts helpers */
+
+static libxl_nic_type get_nic_type(libxl__gc *gc, libxl__device
 *dev)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    char *snictype, *be_path;
+
+    be_path = libxl__device_backend_path(gc, dev);
+
+    snictype = libxl__xs_read(gc, XBT_NULL,
+                            libxl__sprintf(gc, "%s/%s", be_path,
 "type"));

This really ought to be under some private libxl path relating to the device rather than under the backend/frontend visible dirs.

Quoted 3 lines

Well, this is currently only used by libxl, but the type of the backend used I think should be inside the backend device directory, since other applications might also want to know this.

Hang on, can't you infer the type from the backend path, one should contains vif and the other something else (tap)? Or is this because of the stupid sharing of the vif dir for both vif and tap from the hotplug scripts point of view?

It's probably too late in the 4.2 cycle to direct the tap hotplug script to a different backend dir so I think the best thing to do for now is to put this key somewhere else so that it doesn't become a guest visible API (which is what happens with where you have put it). The same place as udev_disable would work fine.

Quoted 9 lines
Quoted 2 lines

Actually, now I think of it, that is also true of the udev_disable key?

This is probably true for udev_disable, something like /libxl/devices/<domid>/<devid>/udev_disable? I will have to modify libxl__device_generic_add to do this, because it should be done on the same transaction when the device is added. I'm not sure if we need to add so much overhead for just one xenstore entry, that will go away in the next release probably.

I think this will be around for more than one release (these deprecations always take time) so it is worth doing right.

Quoted 41 lines
Quoted 31 lines
Quoted 31 lines
tools/libxl/libxl_linux.c +13 -0
+    if (!snictype) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable to read nictype
 from %s",
+                                          be_path);
+        return -1;
+    }
+
+    return atoi(snictype);
+}
+
@@ -62,6 +81,35 @@ static char **get_hotplug_env(libxl__gc *gc,
 libxl__device *dev)
              dev->domid, dev->devid));
 flexarray_set(f_env, nr++, "XENBUS_BASE_PATH");
 flexarray_set(f_env, nr++, "backend");
+    if (dev->backend_kind == LIBXL__DEVICE_KIND_VIF) {
+        ifname = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc,
 "%s/%s",
+
 be_path,
+

"vifname"));

tools/libxl/libxl_linux.c +5 -0
+        switch (get_nic_type(gc, dev)) {
+        case LIBXL_NIC_TYPE_IOEMU:
+            flexarray_set(f_env, nr++, "INTERFACE");
+            if (ifname) {
+                flexarray_set(f_env, nr++, libxl__sprintf(gc,

"xentap-%s",
+
ifname));

Quoted 8 lines

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 97

Quoted 4 lines
tools/libxl/libxl_linux.c +4 -0
+            } else {
+                flexarray_set(f_env, nr++,
+                              libxl__sprintf(gc, "xentap%u.%d",
+                              dev->domid, dev->devid));

This is cut-n-paste of some other code, perhaps create a function to get
the tap name from the vif name and or dom/devid?

Quoted 1 line

Are you going to add it to your patch or I should add it to mine?

I'll let you do it if that's alright.

Quoted 3 lines

Something like:

char *libxl__device_tap_name(libxl__gc *gc, libxl__device *dev)

[...]

Quoted 4 lines

char *libxl__device_vif_name(libxl__gc *gc, libxl__device *dev)

Both this functions should return the correct vifname if one is set, or the default one otherwise.

Yep.

Ian.

#10 lore raw

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

Quoted 15 lines · Ian Campbell
Quoted 15 lines
Quoted 15 lines

docs/man/xl.conf.pod.5 default. · line 63

Quoted 11 lines
(quoted diff) +0 -0
@@ -55,6 +55,13 @@ default.
 

Default: C<1>

+=item B<disable_vif_scripts=BOOLEAN>

(quoted diff) +2 -0
+
+If enabled xl will not execute nic hotplug scripts itself, and

instead

docs/man/xl.conf.pod.5 +3 -0
+relegate this task to udev.
+
+Default: C<0>

Please can you also add a commented out version
to ./tools/examples/xl.conf, with the value of the default.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This doesn't actually disable the scripts entirely, but rather only from > udev, disable_udev_vif_scripts perhaps?

It actually disables script calling from libxl, so I think it might be best to call it disable_xl_vif_scripts?

Oh, I was confusing it with the xenstore key used by the scripts! I think your existing name is fine then.

I've changed it to disable_xl_vif_scripts, which I think is even more clear.

Quoted 39 lines
Quoted 32 lines
Quoted 32 lines
Quoted 19 lines
tools/libxl/libxl_device.c +5 -2
@@ -450,22 +504,29 @@ int libxl__initiate_device_remove(libxl__egc
 *egc, libxl__ao *ao,
 {
 AO_GC;
 libxl_ctx *ctx = libxl__gc_owner(gc);
-    xs_transaction_t t;
+    xs_transaction_t t = 0;
 char *be_path = libxl__device_backend_path(gc, dev);
 char *state_path = libxl__sprintf(gc, "%s/state", be_path);
-    char *state = libxl__xs_read(gc, XBT_NULL, state_path);
+    char *state;
 int rc = 0;
 libxl__ao_device_remove *aorm = 0;
 
+    /*
+     * Nuke frontend to force backend teardown
+     * It's not pretty, but it's the only way that seems to work 

for
all

tools/libxl/libxl_device.c int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao, · line 518

Quoted 2 lines
tools/libxl/libxl_device.c +2 -0
+     * types of backends
+     */

I'm still not happy with this. The _remove functions are supposed to be
a graceful + co-operative remove, that means prodding the front and backend into doing the controlled teardown dance.

This may well take some time and may even fail after some timeout depending on the device type, guest configuration and co-operation etc,
but that is expected and should be handled.

Quoted 6 lines
Quoted 3 lines

Forcing things in this way is appropriate for device_destroy only IMHO since that is the function which is provided for use when the guest is not co-operating.

Before this patch, we used to just nuke both front and back xenstore directories (because we always called libxl__device_destroy),

Quoted 6 lines

Not always, the call to destroy depended on the current state.

In the case where the guest is alive and responsive we have to do the hot remove in the graceful way. If the guest is alive but not responsive then the correct approach is to try the graceful method with a timeout which triggers the big hammer approach.

Ok, I will add and extra parameter to libxl__initiate_device_remove that turns on or off the destruction of the front xenstore entries. We will first call it without removing the fronted, and if that fails we will call libxl__initiate_device_remove from the callback this time forcing the removal of the frontend.

Quoted 72 lines
Quoted 72 lines

so I think
this is quite and improvement in term of co-operation than what we had before. We only used this kind of negotiation when detaching a block from a live guest.

Quoted 65 lines
Quoted 52 lines

+ libxl__xs_path_cleanup(gc, libxl__device_frontend_path(gc,
dev));

tools/libxl/libxl_device.c +4 -0
+
+retry_transaction:
+    t = xs_transaction_start(ctx->xsh);
+    state = libxl__xs_read(gc, t, state_path);

if (!state)
goto out_ok;

tools/libxl/libxl_device.c +1 -4
-    if (atoi(state) != 4) {
+    if (atoi(state) == XenbusStateClosed)
 libxl__device_destroy_tapdisk(gc, be_path);
 goto out_ok;
-    }
 
-retry_transaction:
-    t = xs_transaction_start(ctx->xsh);

xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/online", be_path), "0", strlen("0"));
xs_write(ctx->xsh, t, state_path, "5", strlen("5"));
if (!xs_transaction_end(ctx->xsh, t, 0)) {

tools/libxl/libxl_device.c +1 -0
@@ -476,6 +537,8 @@ retry_transaction:
     goto out_fail;
 }
 }
+    /* mark transaction as ended, to prevent double closing it on

out_ok */
+ t = 0;

libxl__device_destroy_tapdisk(gc, be_path);

(quoted diff) +0 -0
@@ -497,8 +560,8 @@ retry_transaction:
 return rc;
 

out_ok:
+ if (t) xs_transaction_end(ctx->xsh, t, 0);
rc = libxl__device_hotplug(gc, dev, DISCONNECT);
- libxl__xs_path_cleanup(gc, libxl__device_frontend_path(gc,
dev));
libxl__xs_path_cleanup(gc, be_path);
return 0;
}

tools/libxl/libxl_linux.c +2 -0
index c5583af..1ef282f 100644
@@ -27,11 +27,30 @@ int libxl__try_phy_backend(mode_t st_mode)
 }
 
 /* Hotplug scripts helpers */
+
+static libxl_nic_type get_nic_type(libxl__gc *gc, libxl__device

tools/libxl/libxl_linux.c int libxl__try_phy_backend(mode_t st_mode) · line 41

Quoted 10 lines
tools/libxl/libxl_linux.c +8 -0
 *dev)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    char *snictype, *be_path;
+
+    be_path = libxl__device_backend_path(gc, dev);
+
+    snictype = libxl__xs_read(gc, XBT_NULL,
+                            libxl__sprintf(gc, "%s/%s", be_path,
 "type"));

This really ought to be under some private libxl path relating to the device rather than under the backend/frontend visible dirs.

Quoted 8 lines
Quoted 3 lines

Well, this is currently only used by libxl, but the type of the backend used I think should be inside the backend device directory, since other applications might also want to know this.

Hang on, can't you infer the type from the backend path, one should contains vif and the other something else (tap)? Or is this because of the stupid sharing of the vif dir for both vif and tap from the hotplug scripts point of view?

Nope, tap doesn't have a backend xenstore entry, only vifs have, so this is kind of a hack because I was marking a vifs path as tap...

Quoted 5 lines

It's probably too late in the 4.2 cycle to direct the tap hotplug script to a different backend dir so I think the best thing to do for now is to put this key somewhere else so that it doesn't become a guest visible API (which is what happens with where you have put it). The same place as udev_disable would work fine.

Does this paths sound ok:

/libxl/devices/<domid>/nic/<devid>/udev_disable
/libxl/devices/<domid>/nic/<devid>/type

Quoted 69 lines
Quoted 9 lines
Quoted 2 lines

Actually, now I think of it, that is also true of the udev_disable key?

This is probably true for udev_disable, something like /libxl/devices/<domid>/<devid>/udev_disable? I will have to modify libxl__device_generic_add to do this, because it should be done on the same transaction when the device is added. I'm not sure if we need to add so much overhead for just one xenstore entry, that will go away in the next release probably.

I think this will be around for more than one release (these deprecations always take time) so it is worth doing right.

Quoted 41 lines
Quoted 31 lines
Quoted 31 lines
tools/libxl/libxl_linux.c +13 -0
+    if (!snictype) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unable to read nictype
 from %s",
+                                          be_path);
+        return -1;
+    }
+
+    return atoi(snictype);
+}
+
@@ -62,6 +81,35 @@ static char **get_hotplug_env(libxl__gc *gc,
 libxl__device *dev)
           dev->domid, dev->devid));
 flexarray_set(f_env, nr++, "XENBUS_BASE_PATH");
 flexarray_set(f_env, nr++, "backend");
+    if (dev->backend_kind == LIBXL__DEVICE_KIND_VIF) {
+        ifname = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc,
 "%s/%s",
+
 be_path,
+

"vifname"));

tools/libxl/libxl_linux.c +5 -0
+        switch (get_nic_type(gc, dev)) {
+        case LIBXL_NIC_TYPE_IOEMU:
+            flexarray_set(f_env, nr++, "INTERFACE");
+            if (ifname) {
+                flexarray_set(f_env, nr++, libxl__sprintf(gc,

"xentap-%s",
+
ifname));

Quoted 8 lines

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 97

Quoted 4 lines
tools/libxl/libxl_linux.c +4 -0
+            } else {
+                flexarray_set(f_env, nr++,
+                              libxl__sprintf(gc, "xentap%u.%d",
+                              dev->domid, dev->devid));

This is cut-n-paste of some other code, perhaps create a function to get
the tap name from the vif name and or dom/devid?

Quoted 1 line

Are you going to add it to your patch or I should add it to mine?

I'll let you do it if that's alright.

Quoted 3 lines

Something like:

char *libxl__device_tap_name(libxl__gc *gc, libxl__device *dev)

[...]

Quoted 4 lines

char *libxl__device_vif_name(libxl__gc *gc, libxl__device *dev)

Both this functions should return the correct vifname if one is set, or the default one otherwise.

Yep.

Ian.

#11 lore raw

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

Quoted 6 lines

Ok, I will add and extra parameter to libxl__initiate_device_remove that turns on or off the destruction of the front xenstore entries. We will first call it without removing the fronted, and if that fails we will call libxl__initiate_device_remove from the callback this time forcing the removal of the frontend.

If libxl__initiate_device_remove fails then you should be calling libxl__initiate_device_destroy, I think, so no need for a param to _remove?

Quoted 8 lines
Quoted 5 lines

Hang on, can't you infer the type from the backend path, one should contains vif and the other something else (tap)? Or is this because of the stupid sharing of the vif dir for both vif and tap from the hotplug
scripts point of view?

Nope, tap doesn't have a backend xenstore entry, only vifs have, so this is kind of a hack because I was marking a vifs path as tap...

Right, that's the stupid sharing I was referring too (which IIRC I added :-/)

Quoted 9 lines
Quoted 8 lines

It's probably too late in the 4.2 cycle to direct the tap hotplug script
to a different backend dir so I think the best thing to do for now is to
put this key somewhere else so that it doesn't become a guest visible API (which is what happens with where you have put it). The same place as udev_disable would work fine.

Quoted 4 lines

Does this paths sound ok:

/libxl/devices/<domid>/nic/<devid>/udev_disable
/libxl/devices/<domid>/nic/<devid>/type

Works for me.

Ian.

#12 lore raw

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

Quoted 9 lines · Ian Campbell
Quoted 5 lines

Ok, I will add and extra parameter to libxl__initiate_device_remove that turns on or off the destruction of the front xenstore entries. We will first call it without removing the fronted, and if that fails we will call libxl__initiate_device_remove from the callback this time forcing the removal of the frontend.

If libxl__initiate_device_remove fails then you should be calling libxl__initiate_device_destroy, I think, so no need for a param to _remove?

Not really… That's how I think the removal path should be:

1- Wait for backend to turn to state 6 -----> if ok then execute hotplug, and remove front/backend 2- If timeout: nuke frontend and wait for backend state 6 -----> if ok then execute hotplug and remove front/back 3- If timeout: nuke front/backend

Quoted 21 lines
Quoted 8 lines
Quoted 5 lines

Hang on, can't you infer the type from the backend path, one should contains vif and the other something else (tap)? Or is this because of the stupid sharing of the vif dir for both vif and tap from the hotplug
scripts point of view?

Nope, tap doesn't have a backend xenstore entry, only vifs have, so this is kind of a hack because I was marking a vifs path as tap...

Right, that's the stupid sharing I was referring too (which IIRC I added :-/)

Quoted 9 lines
Quoted 8 lines

It's probably too late in the 4.2 cycle to direct the tap hotplug script
to a different backend dir so I think the best thing to do for now is to
put this key somewhere else so that it doesn't become a guest visible API (which is what happens with where you have put it). The same place as udev_disable would work fine.

Quoted 8 lines
Quoted 4 lines

Does this paths sound ok:

/libxl/devices/<domid>/nic/<devid>/udev_disable
/libxl/devices/<domid>/nic/<devid>/type

Works for me.

Ian.

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

#13 lore raw

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

Quoted 15 lines · Roger Pau Monne
Quoted 9 lines · Ian Campbell
Quoted 8 lines

Ok, I will add and extra parameter to libxl__initiate_device_remove that
turns on or off the destruction of the front xenstore entries. We will
first call it without removing the fronted, and if that fails we will call libxl__initiate_device_remove from the callback this time forcing
the removal of the frontend.

Quoted 3 lines

If libxl__initiate_device_remove fails then you should be calling libxl__initiate_device_destroy, I think, so no need for a param to _remove?

Quoted 7 lines

Not really… That's how I think the removal path should be:

1- Wait for backend to turn to state 6 -----> if ok then execute hotplug, and remove front/backend
2- If timeout: nuke frontend and wait for backend state 6 -----> if ok then execute hotplug and remove front/back
3- If timeout: nuke front/backend

That could work, but what does doing step 2 buy you? Why not skip straight to step 3?

Ian.

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

#14 lore raw

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

Quoted 18 lines · Ian Campbell
Quoted 17 lines · Roger Pau Monne
Quoted 9 lines · Ian Campbell
Quoted 8 lines

Ok, I will add and extra parameter to libxl__initiate_device_remove that
turns on or off the destruction of the front xenstore entries. We will
first call it without removing the fronted, and if that fails we will call libxl__initiate_device_remove from the callback this time forcing
the removal of the frontend.

Quoted 3 lines

If libxl__initiate_device_remove fails then you should be calling libxl__initiate_device_destroy, I think, so no need for a param to _remove?

Not really… That's how I think the removal path should be:

Quoted 8 lines
Quoted 5 lines

1- Wait for backend to turn to state 6 -----> if ok then execute hotplug, and remove front/backend
2- If timeout: nuke frontend and wait for backend state 6 -----> if ok then execute hotplug and remove front/back
3- If timeout: nuke front/backend

That could work, but what does doing step 2 buy you? Why not skip straight to step 3?

hotplug scripts usually require the device to be on state 6 for them to work, nuking the frontend forces the backend disconnection, and gets us a state 6.

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

#15 lore raw

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

Quoted 28 lines · Roger Pau Monne
Quoted 26 lines · Ian Campbell
Quoted 24 lines · Roger Pau Monne
Quoted 10 lines · Ian Campbell
Quoted 9 lines

Ok, I will add and extra parameter to libxl__initiate_device_remove that
turns on or off the destruction of the front xenstore entries. We will
first call it without removing the fronted, and if that fails we will
call libxl__initiate_device_remove from the callback this time forcing
the removal of the frontend.

Quoted 3 lines

If libxl__initiate_device_remove fails then you should be calling libxl__initiate_device_destroy, I think, so no need for a param to _remove?

Not really… That's how I think the removal path should be:

1- Wait for backend to turn to state 6 -----> if ok then execute hotplug, and remove front/backend
2- If timeout: nuke frontend and wait for backend state 6 -----> if ok
then execute hotplug and remove front/back
3- If timeout: nuke front/backend

Quoted 6 lines
Quoted 2 lines

That could work, but what does doing step 2 buy you? Why not skip straight to step 3?

hotplug scripts usually require the device to be on state 6 for them to work, nuking the frontend forces the backend disconnection, and gets us a state 6.

OK, that makes sense. You'd need to have two levels of timeout/callbacks though?

Ian.

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

#16 lore raw

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

In reply to Ian Campbell
Quoted 8 lines · Ian Campbell
Quoted 8 lines
Quoted 7 lines

It's probably too late in the 4.2 cycle to direct the tap hotplug script
to a different backend dir so I think the best thing to do for now is to
put this key somewhere else so that it doesn't become a guest visible API (which is what happens with where you have put it). The same place as udev_disable would work fine.

Quoted 6 lines
Quoted 4 lines

Does this paths sound ok:

/libxl/devices/<domid>/nic/<devid>/udev_disable
/libxl/devices/<domid>/nic/<devid>/type

Works for me.

I've just realized that it's not possible to use exactly this paths, because then I won't be able to set the HOTPLUG_GATE env from the udev rules file, so I think something like:

/libxl/backend/vif/7/0

Is one of the only options left. Here are the env variable available when calling a hotplug script from udev:

UDEV_LOG=3
ACTION=remove
DEVPATH=/devices/vif-7-0
SUBSYSTEM=xen-backend
XENBUS_TYPE=vif
XENBUS_PATH=backend/vif/7/0
XENBUS_BASE_PATH=backend
SEQNUM=1382

So I would set ENV{HOTPLUG_GATE} as /libxl/$XENBUS_PATH

#17 lore raw

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

Quoted 38 lines · Roger Pau Monne
Quoted 11 lines · Ian Campbell
Quoted 11 lines
Quoted 10 lines

It's probably too late in the 4.2 cycle to direct the tap hotplug script
to a different backend dir so I think the best thing to do for now is
to
put this key somewhere else so that it doesn't become a guest visible
API (which is what happens with where you have put it). The same place
as udev_disable would work fine.

Quoted 6 lines
Quoted 4 lines

Does this paths sound ok:

/libxl/devices/<domid>/nic/<devid>/udev_disable
/libxl/devices/<domid>/nic/<devid>/type

Works for me.

I've just realized that it's not possible to use exactly this paths, because then I won't be able to set the HOTPLUG_GATE env from the udev rules file, so I think something like:

/libxl/backend/vif/7/0

Is one of the only options left. Here are the env variable available when calling a hotplug script from udev:

UDEV_LOG=3
ACTION=remove
DEVPATH=/devices/vif-7-0
SUBSYSTEM=xen-backend
XENBUS_TYPE=vif
XENBUS_PATH=backend/vif/7/0
XENBUS_BASE_PATH=backend
SEQNUM=1382

Quoted 1 line

So I would set ENV{HOTPLUG_GATE} as /libxl/$XENBUS_PATH

That seems reasonable to me.

Ian.

#18 lore raw

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

In reply to Ian Campbell
Quoted 87 lines · Ian Campbell
Quoted 15 lines · 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.

Quoted 3 lines

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 mantain the simmetry.

"maintain" and "symmetry"

These are pure code motion or did the code actually change too?

Quoted 3 lines

libxl__initiate_device_remove has been changed a little, to nuke the frontend xenstore path before trying to perform device teardown, this allows for unitialized devices to be closed gracefully, specially vif interfaces added to

uninitialized (or -ised)

Quoted 6 lines

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 gobal option, called disable_vif_scripts has been added to allow the user

global

Quoted 2 lines

decide if he wants to execute the hotplug scripts from xl or from udev. This has been documented in the xl.conf man page.

Did you do this for block too?

Quoted 32 lines

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/hotplug/Linux/xen-backend.rules |    6 +-
tools/libxl/libxl.c                   |   90 +++++++-------------
tools/libxl/libxl.h                   |    3 +-
tools/libxl/libxl_create.c            |   22 +++++-
tools/libxl/libxl_device.c            |   77 +++++++++++++++--
tools/libxl/libxl_dm.c                |    2 +-
tools/libxl/libxl_internal.h          |    6 ++
tools/libxl/libxl_linux.c             |  150 
++++++++++++++++++++++++++++++++-
tools/libxl/libxl_types.idl           |    1 +
tools/libxl/xl.c                      |    4 +
tools/libxl/xl.h                      |    1 +
tools/libxl/xl_cmdimpl.c              |   15 +++-
13 files changed, 308 insertions(+), 76 deletions(-)
tools/libxl/xl_cmdimpl.c +0 -0
index 01ff363..41230a6 100644

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char · line 853

Quoted 8 lines
tools/libxl/xl_cmdimpl.c +4 -0
@@ -846,6 +846,19 @@ static void parse_config_data(const char 
 *configfile_filename_report,
             nic->script = strdup(default_vifscript);
         }
 
+            /* Set default nic type for PV guests correctly */
+            if (b_info->type == LIBXL_DOMAIN_TYPE_PV) {
+                nic->nictype = LIBXL_NIC_TYPE_VIF;
+            }

Hrm, really the lib ought to be taking care of that for us...

Quoted 2 lines

libxl__device_nic_setdefault has a domid so it should be able to query the domain type with libxl__domain_type.

int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic) is the prototype, and libxl_device_nic doesn't have a domid, so I'm not sure where should I get it from.

Quoted 7 lines
Quoted 7 lines
tools/libxl/xl_cmdimpl.c +7 -0
+            /*
+             * Disable nic network script calling, to allow the user
+             * to attach the nic backend from a different domain.
+             */
+            if (disable_vif_scripts) {
+                nic->disable_vif_script = 1;
+            }
#19 lore raw

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

Quoted 16 lines
Quoted 16 lines

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char · line 853

Quoted 13 lines
tools/libxl/xl_cmdimpl.c +4 -0
index 01ff363..41230a6 100644
@@ -846,6 +846,19 @@ static void parse_config_data(const char 
 *configfile_filename_report,
             nic->script = strdup(default_vifscript);
         }
 
+            /* Set default nic type for PV guests correctly */
+            if (b_info->type == LIBXL_DOMAIN_TYPE_PV) {
+                nic->nictype = LIBXL_NIC_TYPE_VIF;
+            }

Hrm, really the lib ought to be taking care of that for us...

Quoted 6 lines
Quoted 2 lines

libxl__device_nic_setdefault has a domid so it should be able to query the domain type with libxl__domain_type.

int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic) is the prototype, and libxl_device_nic doesn't have a domid, so I'm not sure where should I get it from.

Urk, oh dear.

I think it would be ok to add parameters which are necessary for setdefault to make its decision.

Other wise perhaps make the default be LIBXL_NIC_TYPE_VIF unconditionally? That is the option which has a chance of working regardless of guest type, TYPE_IOEMU is a bit of an odd choice for the default.

Ian.

#20 lore raw

PATCH v2 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 04f76c2..d9c1786 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++;
             }
         }
@@ -461,10 +468,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)
Acked-by on v2 5/5
#21 lore raw

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

Quoted 5 lines · 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:

Quoted 7 lines

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

Changes since v1:

* Indentation fixes.

Quoted 49 lines
---
 tools/libxl/libxl_dm.c |   27 ++++++++++++++++++---------
 1 files changed, 18 insertions(+), 9 deletions(-)
tools/libxl/libxl_dm.c +18 -9
index 04f76c2..d9c1786 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++;
             }
         }
@@ -461,10 +468,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++;
             }
         }