Skip to contentSkip to conversation

tools: remove xenstore entries on vchan server closure

PATCH In review [email protected] 4 participants 12 messages Jul 13, 2022 – Sep 27, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 tools: remove xenstore entries on vchan server closure

From: Oleksandr Andrushchenko <[email protected]>

vchan server creates XenStore entries to advertise its event channel and ring, but those are not removed after the server quits.
Add additional cleanup step, so those are removed, so clients do not try to connect to a non-existing server.

---
 tools/include/libxenvchan.h |  5 +++++
 tools/libs/vchan/init.c     | 24 ++++++++++++++++++++++++
 tools/libs/vchan/io.c       |  4 ++++
 tools/libs/vchan/vchan.h    | 31 +++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+)
 create mode 100644 tools/libs/vchan/vchan.h
tools/include/libxenvchan.h +5 -0
index d6010b145d..30cc73cf97 100644
@@ -86,6 +86,11 @@ struct libxenvchan {
 	int blocking:1;
 	/* communication rings */
 	struct libxenvchan_ring read, write;
+	/**
+	 * Base xenstore path for storing ring/event data used by the server
+	 * during cleanup.
+	 * */
+	char *xs_path;
 };
 
 /**
tools/libs/vchan/init.c +24 -0
index c8510e6ce9..9195bd3b98 100644
@@ -46,6 +46,8 @@
 #include <xen/sys/gntdev.h>
 #include <libxenvchan.h>
 
+#include "vchan.h"
+
 #ifndef PAGE_SHIFT
 #define PAGE_SHIFT 12
 #endif
@@ -251,6 +253,12 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
 	char ref[16];
 	char* domid_str = NULL;
 	xs_transaction_t xs_trans = XBT_NULL;
+
+	/* store the base path so we can clean up on server closure */
+	ctrl->xs_path = strdup(xs_base);
+	if (!ctrl->xs_path)
+		return -1; 
+
 	xs = xs_open(0);
 	if (!xs)
 		goto fail;
@@ -298,6 +306,22 @@ retry_transaction:
 	return ret;
 }
 
+void close_xs_srv(struct libxenvchan *ctrl)
+{
+	struct xs_handle *xs;
+
+	if (!ctrl->xs_path)
+		return;
+
+	xs = xs_open(0);
+	if (xs) {
+		xs_rm(xs, XBT_NULL, ctrl->xs_path);
+		xs_close(xs);
+	}
+
+	free(ctrl->xs_path);
+}
+
 static int min_order(size_t siz)
 {
 	int rv = PAGE_SHIFT;
tools/libs/vchan/io.c +4 -0
index da303fbc01..1f201ad554 100644
@@ -40,6 +40,8 @@
 #include <xenctrl.h>
 #include <libxenvchan.h>
 
+#include "vchan.h"
+
 #ifndef PAGE_SHIFT
 #define PAGE_SHIFT 12
 #endif
@@ -384,5 +386,7 @@ void libxenvchan_close(struct libxenvchan *ctrl)
 		if (ctrl->gnttab)
 			xengnttab_close(ctrl->gnttab);
 	}
+	if (ctrl->is_server)
+		close_xs_srv(ctrl);
 	free(ctrl);
 }
tools/libs/vchan/vchan.h new file +31 -0
new file mode 100644
index 0000000000..621016ef42
@@ -0,0 +1,31 @@
+/**
+ * @file
+ * @section AUTHORS
+ *
+ * Copyright (C) 2021 EPAM Systems Inc.
+ *
+ * @section LICENSE
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @section DESCRIPTION
+ *
+ *  This file contains common libxenvchan declarations.
+ */
+#ifndef LIBVCHAN_H
+#define LIBVCHAN_H
+
+void close_xs_srv(struct libxenvchan *ctrl);
+
+#endif /* LIBVCHAN_H */
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 2/4 tools: allow vchan XenStore paths more then 64 bytes long

From: Oleksandr Andrushchenko <[email protected]>

Current vchan implementation, while dealing with XenStore paths, allocates 64 bytes buffer on the stack which may not be enough for some use-cases. Make the buffer longer to respect maximum allowed XenStore path of XENSTORE_ABS_PATH_MAX.

---
 tools/libs/vchan/init.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
tools/libs/vchan/init.c +22 -6
index 9195bd3b98..38658f30af 100644
@@ -249,7 +249,7 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
 	int ret = -1;
 	struct xs_handle *xs;
 	struct xs_permissions perms[2];
-	char buf[64];
+	char *buf;
 	char ref[16];
 	char* domid_str = NULL;
 	xs_transaction_t xs_trans = XBT_NULL;
@@ -259,6 +259,12 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
 	if (!ctrl->xs_path)
 		return -1; 
 
+	buf = malloc(XENSTORE_ABS_PATH_MAX);
+	if (!buf) {
+		free(ctrl);
+		return 0;
+	}
+
 	xs = xs_open(0);
 	if (!xs)
 		goto fail;
@@ -280,14 +286,14 @@ retry_transaction:
 		goto fail_xs_open;
 
 	snprintf(ref, sizeof ref, "%d", ring_ref);
-	snprintf(buf, sizeof buf, "%s/ring-ref", xs_base);
+	snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_base);
 	if (!xs_write(xs, xs_trans, buf, ref, strlen(ref)))
 		goto fail_xs_open;
 	if (!xs_set_permissions(xs, xs_trans, buf, perms, 2))
 		goto fail_xs_open;
 
 	snprintf(ref, sizeof ref, "%d", ctrl->event_port);
-	snprintf(buf, sizeof buf, "%s/event-channel", xs_base);
+	snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_base);
 	if (!xs_write(xs, xs_trans, buf, ref, strlen(ref)))
 		goto fail_xs_open;
 	if (!xs_set_permissions(xs, xs_trans, buf, perms, 2))
@@ -303,6 +309,7 @@ retry_transaction:
 	free(domid_str);
 	xs_close(xs);
  fail:
+	free(buf);
 	return ret;
 }
 
@@ -419,13 +426,20 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
 {
 	struct libxenvchan *ctrl = malloc(sizeof(struct libxenvchan));
 	struct xs_handle *xs = NULL;
-	char buf[64];
+	char *buf;
 	char *ref;
 	int ring_ref;
 	unsigned int len;
 
 	if (!ctrl)
 		return 0;
+
+	buf = malloc(XENSTORE_ABS_PATH_MAX);
+	if (!buf) {
+		free(ctrl);
+		return 0;
+	}
+
 	ctrl->ring = NULL;
 	ctrl->event = NULL;
 	ctrl->gnttab = NULL;
@@ -436,8 +450,9 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
 	if (!xs)
 		goto fail;
 
+
 // find xenstore entry
-	snprintf(buf, sizeof buf, "%s/ring-ref", xs_path);
+	snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_path);
 	ref = xs_read(xs, 0, buf, &len);
 	if (!ref)
 		goto fail;
@@ -445,7 +460,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
 	free(ref);
 	if (!ring_ref)
 		goto fail;
-	snprintf(buf, sizeof buf, "%s/event-channel", xs_path);
+	snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_path);
 	ref = xs_read(xs, 0, buf, &len);
 	if (!ref)
 		goto fail;
@@ -475,6 +490,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
  out:
 	if (xs)
 		xs_close(xs);
+	free(buf);
 	return ctrl;
  fail:
 	libxenvchan_close(ctrl);
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 2/4] tools: allow vchan XenStore paths more then 64 bytes long

Quoted 107 lines · On 13.07.22 17:03, [email protected] wrote:

From: Oleksandr Andrushchenko <[email protected]>

Current vchan implementation, while dealing with XenStore paths, allocates 64 bytes buffer on the stack which may not be enough for some use-cases. Make the buffer longer to respect maximum allowed XenStore path of XENSTORE_ABS_PATH_MAX.

---
  tools/libs/vchan/init.c | 28 ++++++++++++++++++++++------
  1 file changed, 22 insertions(+), 6 deletions(-)
tools/libs/vchan/init.c +21 -6
index 9195bd3b98..38658f30af 100644
@@ -249,7 +249,7 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
  	int ret = -1;
  	struct xs_handle *xs;
  	struct xs_permissions perms[2];
-	char buf[64];
+	char *buf;
  	char ref[16];
  	char* domid_str = NULL;
  	xs_transaction_t xs_trans = XBT_NULL;
@@ -259,6 +259,12 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
  	if (!ctrl->xs_path)
  		return -1;
 
+	buf = malloc(XENSTORE_ABS_PATH_MAX);
+	if (!buf) {
+		free(ctrl);
+		return 0;
+	}
+
  	xs = xs_open(0);
  	if (!xs)
  		goto fail;
@@ -280,14 +286,14 @@ retry_transaction:
  		goto fail_xs_open;
 
  	snprintf(ref, sizeof ref, "%d", ring_ref);
-	snprintf(buf, sizeof buf, "%s/ring-ref", xs_base);
+	snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_base);
  	if (!xs_write(xs, xs_trans, buf, ref, strlen(ref)))
  		goto fail_xs_open;
  	if (!xs_set_permissions(xs, xs_trans, buf, perms, 2))
  		goto fail_xs_open;
 
  	snprintf(ref, sizeof ref, "%d", ctrl->event_port);
-	snprintf(buf, sizeof buf, "%s/event-channel", xs_base);
+	snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_base);
  	if (!xs_write(xs, xs_trans, buf, ref, strlen(ref)))
  		goto fail_xs_open;
  	if (!xs_set_permissions(xs, xs_trans, buf, perms, 2))
@@ -303,6 +309,7 @@ retry_transaction:
  	free(domid_str);
  	xs_close(xs);
   fail:
+	free(buf);
  	return ret;
  }
 
@@ -419,13 +426,20 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
  {
  	struct libxenvchan *ctrl = malloc(sizeof(struct libxenvchan));
  	struct xs_handle *xs = NULL;
-	char buf[64];
+	char *buf;
  	char *ref;
  	int ring_ref;
  	unsigned int len;
 
  	if (!ctrl)
  		return 0;
+
+	buf = malloc(XENSTORE_ABS_PATH_MAX);
+	if (!buf) {
+		free(ctrl);
+		return 0;
+	}
+
  	ctrl->ring = NULL;
  	ctrl->event = NULL;
  	ctrl->gnttab = NULL;
@@ -436,8 +450,9 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
  	if (!xs)
  		goto fail;
 
+
  // find xenstore entry
-	snprintf(buf, sizeof buf, "%s/ring-ref", xs_path);
+	snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_path);
  	ref = xs_read(xs, 0, buf, &len);
  	if (!ref)
  		goto fail;
@@ -445,7 +460,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
  	free(ref);
  	if (!ring_ref)
  		goto fail;
-	snprintf(buf, sizeof buf, "%s/event-channel", xs_path);
+	snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_path);
  	ref = xs_read(xs, 0, buf, &len);
  	if (!ref)
  		goto fail;

tools/libs/vchan/init.c struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger, · line 496

Quoted 7 lines
tools/libs/vchan/init.c +1 -0
@@ -475,6 +490,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
   out:
  	if (xs)
  		xs_close(xs);
+	free(buf);
  	return ctrl;
   fail:
  	libxenvchan_close(ctrl);

I think you are leaking buf in case of "goto fail".

Juergen

#3 lore raw

Re: [PATCH v2 2/4] tools: allow vchan XenStore paths more then 64 bytes long

пн, 1 авг. 2022 г. в 11:59, Juergen Gross <[email protected]>:

Quoted 110 lines · On 13.07.22 17:03, [email protected] wrote:
Quoted 108 lines

From: Oleksandr Andrushchenko <[email protected]>

Current vchan implementation, while dealing with XenStore paths, allocates 64 bytes buffer on the stack which may not be enough for some use-cases. Make the buffer longer to respect maximum allowed XenStore path of XENSTORE_ABS_PATH_MAX.

---
  tools/libs/vchan/init.c | 28 ++++++++++++++++++++++------
  1 file changed, 22 insertions(+), 6 deletions(-)
tools/libs/vchan/init.c +21 -6
index 9195bd3b98..38658f30af 100644
@@ -249,7 +249,7 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
      int ret = -1;
      struct xs_handle *xs;
      struct xs_permissions perms[2];
-     char buf[64];
+     char *buf;
      char ref[16];
      char* domid_str = NULL;
      xs_transaction_t xs_trans = XBT_NULL;
@@ -259,6 +259,12 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
      if (!ctrl->xs_path)
              return -1;
 
+     buf = malloc(XENSTORE_ABS_PATH_MAX);
+     if (!buf) {
+             free(ctrl);
+             return 0;
+     }
+
      xs = xs_open(0);
      if (!xs)
              goto fail;
@@ -280,14 +286,14 @@ retry_transaction:
              goto fail_xs_open;
 
      snprintf(ref, sizeof ref, "%d", ring_ref);
-     snprintf(buf, sizeof buf, "%s/ring-ref", xs_base);
+     snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_base);
      if (!xs_write(xs, xs_trans, buf, ref, strlen(ref)))
              goto fail_xs_open;
      if (!xs_set_permissions(xs, xs_trans, buf, perms, 2))
              goto fail_xs_open;
 
      snprintf(ref, sizeof ref, "%d", ctrl->event_port);
-     snprintf(buf, sizeof buf, "%s/event-channel", xs_base);
+     snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_base);
      if (!xs_write(xs, xs_trans, buf, ref, strlen(ref)))
              goto fail_xs_open;
      if (!xs_set_permissions(xs, xs_trans, buf, perms, 2))
@@ -303,6 +309,7 @@ retry_transaction:
      free(domid_str);
      xs_close(xs);
   fail:
+     free(buf);
      return ret;
  }
 
@@ -419,13 +426,20 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
  {
      struct libxenvchan *ctrl = malloc(sizeof(struct libxenvchan));
      struct xs_handle *xs = NULL;
-     char buf[64];
+     char *buf;
      char *ref;
      int ring_ref;
      unsigned int len;
 
      if (!ctrl)
              return 0;
+
+     buf = malloc(XENSTORE_ABS_PATH_MAX);
+     if (!buf) {
+             free(ctrl);
+             return 0;
+     }
+
      ctrl->ring = NULL;
      ctrl->event = NULL;
      ctrl->gnttab = NULL;
@@ -436,8 +450,9 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
      if (!xs)
              goto fail;
 
+
  // find xenstore entry
-     snprintf(buf, sizeof buf, "%s/ring-ref", xs_path);
+     snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_path);
      ref = xs_read(xs, 0, buf, &len);
      if (!ref)
              goto fail;
@@ -445,7 +460,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
      free(ref);
      if (!ring_ref)
              goto fail;
-     snprintf(buf, sizeof buf, "%s/event-channel", xs_path);
+     snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_path);
      ref = xs_read(xs, 0, buf, &len);
      if (!ref)
              goto fail;
@@ -475,6 +490,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
   out:
Quoted 8 lines

tools/libs/vchan/init.c struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger, · line 496

Quoted 6 lines
tools/libs/vchan/init.c +1 -0
      if (xs)
              xs_close(xs);
+     free(buf);
      return ctrl;
   fail:
      libxenvchan_close(ctrl);

I think you are leaking buf in case of "goto fail".

No. File with patch doesn't have follows lines:

    ctrl = NULL;
    goto out;
}
Quoted 3 lines

Juergen

Reviewed-by
#4 lore raw

Re: [PATCH v2 2/4] tools: allow vchan XenStore paths more then 64 bytes long

Quoted 119 lines · Dmytro Semenets

пн, 1 авг. 2022 г. в 11:59, Juergen Gross <[email protected]>:

Quoted 110 lines · On 13.07.22 17:03, [email protected] wrote:
Quoted 108 lines

From: Oleksandr Andrushchenko <[email protected]>

Current vchan implementation, while dealing with XenStore paths, allocates 64 bytes buffer on the stack which may not be enough for some use-cases. Make the buffer longer to respect maximum allowed XenStore path of XENSTORE_ABS_PATH_MAX.

---
   tools/libs/vchan/init.c | 28 ++++++++++++++++++++++------
   1 file changed, 22 insertions(+), 6 deletions(-)
tools/libs/vchan/init.c +21 -6
index 9195bd3b98..38658f30af 100644
@@ -249,7 +249,7 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
       int ret = -1;
       struct xs_handle *xs;
       struct xs_permissions perms[2];
-     char buf[64];
+     char *buf;
       char ref[16];
       char* domid_str = NULL;
       xs_transaction_t xs_trans = XBT_NULL;
@@ -259,6 +259,12 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
       if (!ctrl->xs_path)
               return -1;
 
+     buf = malloc(XENSTORE_ABS_PATH_MAX);
+     if (!buf) {
+             free(ctrl);
+             return 0;
+     }
+
       xs = xs_open(0);
       if (!xs)
               goto fail;
@@ -280,14 +286,14 @@ retry_transaction:
               goto fail_xs_open;
 
       snprintf(ref, sizeof ref, "%d", ring_ref);
-     snprintf(buf, sizeof buf, "%s/ring-ref", xs_base);
+     snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_base);
       if (!xs_write(xs, xs_trans, buf, ref, strlen(ref)))
               goto fail_xs_open;
       if (!xs_set_permissions(xs, xs_trans, buf, perms, 2))
               goto fail_xs_open;
 
       snprintf(ref, sizeof ref, "%d", ctrl->event_port);
-     snprintf(buf, sizeof buf, "%s/event-channel", xs_base);
+     snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_base);
       if (!xs_write(xs, xs_trans, buf, ref, strlen(ref)))
               goto fail_xs_open;
       if (!xs_set_permissions(xs, xs_trans, buf, perms, 2))
@@ -303,6 +309,7 @@ retry_transaction:
       free(domid_str);
       xs_close(xs);
    fail:
+     free(buf);
       return ret;
   }
 
@@ -419,13 +426,20 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
   {
       struct libxenvchan *ctrl = malloc(sizeof(struct libxenvchan));
       struct xs_handle *xs = NULL;
-     char buf[64];
+     char *buf;
       char *ref;
       int ring_ref;
       unsigned int len;
 
       if (!ctrl)
               return 0;
+
+     buf = malloc(XENSTORE_ABS_PATH_MAX);
+     if (!buf) {
+             free(ctrl);
+             return 0;
+     }
+
       ctrl->ring = NULL;
       ctrl->event = NULL;
       ctrl->gnttab = NULL;
@@ -436,8 +450,9 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
       if (!xs)
               goto fail;
 
+
   // find xenstore entry
-     snprintf(buf, sizeof buf, "%s/ring-ref", xs_path);
+     snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_path);
       ref = xs_read(xs, 0, buf, &len);
       if (!ref)
               goto fail;
@@ -445,7 +460,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
       free(ref);
       if (!ring_ref)
               goto fail;
-     snprintf(buf, sizeof buf, "%s/event-channel", xs_path);
+     snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_path);
       ref = xs_read(xs, 0, buf, &len);
       if (!ref)
               goto fail;
@@ -475,6 +490,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
    out:
Quoted 8 lines

tools/libs/vchan/init.c struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger, · line 496

Quoted 6 lines
tools/libs/vchan/init.c +1 -0
       if (xs)
               xs_close(xs);
+     free(buf);
       return ctrl;
    fail:
       libxenvchan_close(ctrl);

I think you are leaking buf in case of "goto fail".

Quoted 4 lines

No. File with patch doesn't have follows lines:

     ctrl = NULL;
     goto out;
}

Oh, what a nasty control flow!

You are right, sorry for the noise.

Juergen

#5 lore raw

Re: [PATCH v2 2/4] tools: allow vchan XenStore paths more then 64 bytes long

In reply to [email protected]

Hi Dmitry,

tools/libs/vchan/init.c static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base · line 265

Quoted 12 lines · On Wed, Jul 13, 2022 at 06:03:09PM +0300, [email protected] wrote:
tools/libs/vchan/init.c +4 -0
index 9195bd3b98..38658f30af 100644
@@ -259,6 +259,12 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base
 	if (!ctrl->xs_path)
 		return -1; 
 
+	buf = malloc(XENSTORE_ABS_PATH_MAX);
+	if (!buf) {
+		free(ctrl);
+		return 0;

I don't understand what you are trying to achieve here. If we can't allocate buf, we should return an error, right?
Also, ctrl isn't allocated in this function but by the caller, so I don't think we need to free it here. Also, if it's free here, the caller is going to continue to use the pointer, after free.

Quoted 12 lines
tools/libs/vchan/init.c +3 -1
+	}
+
 	xs = xs_open(0);
 	if (!xs)
 		goto fail;
@@ -419,13 +426,20 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger,
 {
 	struct libxenvchan *ctrl = malloc(sizeof(struct libxenvchan));
 	struct xs_handle *xs = NULL;
-	char buf[64];
+	char *buf;
 	char *ref;

tools/libs/vchan/init.c struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger, · line 440

Quoted 10 lines
tools/libs/vchan/init.c +5 -0
 	int ring_ref;
 	unsigned int len;
 
 	if (!ctrl)
 		return 0;
+
+	buf = malloc(XENSTORE_ABS_PATH_MAX);
+	if (!buf) {
+		free(ctrl);
+		return 0;

Nit: could you write NULL instead of 0 here? It would makes it much easier to understand that we return a pointer.

Thanks,

Signature · 1 line
Anthony PERARD
#6 lore raw

PATCH v2 3/4 tools/libs/light: Add vchan support to libxl

In reply to [email protected]

From: Oleksandr Andrushchenko <[email protected]>

Add possibility to send commands from libxl and execute them on server side. Libxl vchan adds support for JSON messages processing.

The using of libxl vchan is preventing the client from libxl from reading and writing from / to the local sysfs directly.To do this, the libxl vchan was added - instead of working with the read and write functions from / to the sysfs, functionality allows to send requests to the server, then receive and process the response.

---
 tools/helpers/Makefile         |   4 +-
 tools/libs/light/Makefile      |   7 +-
 tools/libs/light/libxl_vchan.c | 488 +++++++++++++++++++++++++++++++++
 tools/libs/light/libxl_vchan.h |  92 +++++++
 tools/xl/Makefile              |   3 +-
 5 files changed, 588 insertions(+), 6 deletions(-)
 create mode 100644 tools/libs/light/libxl_vchan.c
 create mode 100644 tools/libs/light/libxl_vchan.h
tools/helpers/Makefile +2 -2
index 7f6c422440..3240ef2b6e 100644
@@ -30,10 +30,10 @@ $(INIT_XENSTORE_DOMAIN_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h
 all: $(PROGS)
 
 xen-init-dom0: $(XEN_INIT_DOM0_OBJS)
-	$(CC) $(LDFLAGS) -o $@ $(XEN_INIT_DOM0_OBJS) $(LDLIBS_libxenctrl) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) $(LDLIBS_libxenlight) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $(XEN_INIT_DOM0_OBJS) $(LDLIBS_libxenctrl) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) $(LDLIBS_libxenlight) $(LDLIBS_libxenvchan) $(APPEND_LDFLAGS)
 
 init-xenstore-domain: $(INIT_XENSTORE_DOMAIN_OBJS)
-	$(CC) $(LDFLAGS) -o $@ $(INIT_XENSTORE_DOMAIN_OBJS) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenlight) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $(INIT_XENSTORE_DOMAIN_OBJS) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenlight) $(LDLIBS_libxenvchan) $(APPEND_LDFLAGS)
 
 .PHONY: install
 install: all
tools/libs/light/Makefile +4 -3
index 453bea0067..ea8994af6d 100644
@@ -77,6 +77,7 @@ SRCS-y += libxl.c
 SRCS-y += libxl_create.c
 SRCS-y += libxl_dm.c
 SRCS-y += libxl_pci.c
+SRCS-y += libxl_vchan.c
 SRCS-y += libxl_dom.c
 SRCS-y += libxl_exec.c
 SRCS-y += libxl_xshelp.c
@@ -175,7 +176,7 @@ LDUSELIBS-y += $(PTHREAD_LIBS)
 LDUSELIBS-y += -lyajl
 LDUSELIBS += $(LDUSELIBS-y)
 
-$(LIB_OBJS) $(PIC_OBJS) $(LIBXL_TEST_OBJS): CFLAGS += $(CFLAGS_LIBXL) -include $(XEN_ROOT)/tools/config.h
+$(LIB_OBJS) $(PIC_OBJS) $(LIBXL_TEST_OBJS): CFLAGS += $(CFLAGS_LIBXL) $(CFLAGS_libxenevtchn) -include $(XEN_ROOT)/tools/config.h
 $(ACPI_OBJS) $(ACPI_PIC_OBJS): CFLAGS += -I. -DLIBACPI_STDUTILS=\"$(CURDIR)/libxl_x86_acpi.h\"
 $(TEST_PROG_OBJS) _libxl.api-for-check: CFLAGS += $(CFLAGS_libxentoollog) $(CFLAGS_libxentoolcore)
 libxl_dom.o libxl_dom.opic: CFLAGS += -I$(XEN_ROOT)/tools  # include libacpi/x86.h
@@ -231,13 +232,13 @@ libxenlight_test.so: $(PIC_OBJS) $(LIBXL_TEST_OBJS)
 	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenlight.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDUSELIBS) $(APPEND_LDFLAGS)
 
 test_%: test_%.o test_common.o libxenlight_test.so
-	$(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) -lyajl $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS)
 
 libxl-save-helper: $(SAVE_HELPER_OBJS) libxenlight.so
 	$(CC) $(LDFLAGS) -o $@ $(SAVE_HELPER_OBJS) $(LDLIBS_libxentoollog) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxentoolcore) $(APPEND_LDFLAGS)
 
 testidl: testidl.o libxenlight.so
-	$(CC) $(LDFLAGS) -o $@ testidl.o $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ testidl.o $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(LDLIBS_libxenvchan) $(APPEND_LDFLAGS)
 
 install: installlocal $(LIBHEADERS)
 
tools/libs/light/libxl_vchan.c new file +488 -0

488 lines not on this page · raw message

tools/libs/light/libxl_vchan.h new file +92 -0

92 lines not on this page · raw message

tools/xl/Makefile +2 -1
index b7f439121a..5dfff7fc6f 100644
@@ -15,6 +15,7 @@ LDFLAGS += $(PTHREAD_LDFLAGS)
 CFLAGS_XL += $(CFLAGS_libxenlight)
 CFLAGS_XL += $(CFLAGS_libxenutil)
 CFLAGS_XL += -Wshadow
+CFLAGS_XL += $(CFLAGS_libxenvchan)
 
 XL_OBJS-$(CONFIG_X86) = xl_psr.o
 XL_OBJS = xl.o xl_cmdtable.o xl_sxp.o xl_utils.o $(XL_OBJS-y)
@@ -33,7 +34,7 @@ $(XL_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs i
 all: xl
 
 xl: $(XL_OBJS)
-	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) -lyajl $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS)
 
 .PHONY: install
 install: all
Signature · 1 line
2.25.1
#7 lore raw

PATCH v2 4/4 tools/xl: Add pcid daemon to xl

In reply to [email protected]

From: Oleksandr Andrushchenko <[email protected]>

Add pcid daemon (based on vchan-node2) implements pcid protocol. Protocol is OS independed and should work on ane supported OS.

Add essential functionality to handle pcid protocol:
- define required constants
- prepare for handling remote requests
- prepare and send an error packet

pcid server used if domain has passthrough PCI controller and we wants assign some device to other domain.
pcid server should be launched in domain owns the PCI controller and process request from other domains.

Message exchange imnplementation based on JSON via libvchan. Supported messages:
- make_assignable
- revert_assignable
- is_device_assigned
- resource_list
- reset_device
- write_bdf

---
 docs/man/xl-pci-configuration.5.pod           |   14 +
 tools/configure                               |    8 +-
 tools/configure.ac                            |    1 +
 tools/hotplug/FreeBSD/rc.d/xlpcid.in          |   75 ++
 tools/hotplug/Linux/init.d/xlpcid.in          |   76 ++
 tools/hotplug/Linux/systemd/Makefile          |    1 +
 .../hotplug/Linux/systemd/xenpcid.service.in  |   10 +
 tools/hotplug/NetBSD/rc.d/xlpcid.in           |   75 ++
 tools/include/libxl.h                         |    2 +-
 tools/include/pcid.h                          |  228 ++++
 tools/libs/light/Makefile                     |    1 +
 tools/libs/light/libxl_pci.c                  |  701 +++++------
 tools/libs/light/libxl_pcid.c                 | 1095 +++++++++++++++++
 tools/libs/light/libxl_types.idl              |    2 +
 tools/libs/light/libxl_vchan.c                |   64 +-
 tools/libs/light/libxl_vchan.h                |    7 +-
 tools/libs/util/libxlu_pci.c                  |    6 +-
 tools/xl/Makefile                             |    4 +-
 tools/xl/xl.h                                 |    1 +
 tools/xl/xl_cmdtable.c                        |    7 +
 tools/xl/xl_parse.c                           |    1 +
 tools/xl/xl_pci.c                             |    4 +-
 tools/xl/xl_pcid.c                            |   81 ++
 23 files changed, 2070 insertions(+), 394 deletions(-)
 create mode 100644 tools/hotplug/FreeBSD/rc.d/xlpcid.in
 create mode 100644 tools/hotplug/Linux/init.d/xlpcid.in
 create mode 100644 tools/hotplug/Linux/systemd/xenpcid.service.in
 create mode 100644 tools/hotplug/NetBSD/rc.d/xlpcid.in
 create mode 100644 tools/include/pcid.h
 create mode 100644 tools/libs/light/libxl_pcid.c
 create mode 100644 tools/xl/xl_pcid.c
docs/man/xl-pci-configuration.5.pod +14 -0
index db3360307c..aacbd52c86 100644
@@ -215,4 +215,18 @@ required to look up the B<BDF> in the list of assignable devices.
 
 =back
 
+=item B<domain>=I<STRING>
+
+=over 4
+
+=item Description
+
+This is domain name with pcid daemon running
+
+=item Default Value
+
+None. Domain name with pcid daemon selects automatically
+
+=back
+
 =back
tools/configure +6 -2
index 45db3a5d5c..6124b2ab47 100755
@@ -2452,7 +2452,7 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
 
 
 
-ac_config_files="$ac_config_files ../config/Tools.mk hotplug/FreeBSD/rc.d/xencommons hotplug/FreeBSD/rc.d/xendriverdomain hotplug/Linux/init.d/sysconfig.xencommons hotplug/Linux/init.d/sysconfig.xendomains hotplug/Linux/init.d/xen-watchdog hotplug/Linux/init.d/xencommons hotplug/Linux/init.d/xendomains hotplug/Linux/init.d/xendriverdomain hotplug/Linux/launch-xenstore hotplug/Linux/vif-setup hotplug/Linux/xen-hotplug-common.sh hotplug/Linux/xendomains hotplug/NetBSD/rc.d/xencommons hotplug/NetBSD/rc.d/xendriverdomain ocaml/xenstored/oxenstored.conf"
+ac_config_files="$ac_config_files ../config/Tools.mk hotplug/FreeBSD/rc.d/xencommons hotplug/FreeBSD/rc.d/xendriverdomain hotplug/FreeBSD/rc.d/xlpcid hotplug/Linux/init.d/sysconfig.xencommons hotplug/Linux/init.d/sysconfig.xendomains hotplug/Linux/init.d/xlpcid hotplug/Linux/init.d/xen-watchdog hotplug/Linux/init.d/xencommons hotplug/Linux/init.d/xendomains hotplug/Linux/init.d/xendriverdomain hotplug/Linux/launch-xenstore hotplug/Linux/vif-setup hotplug/Linux/xen-hotplug-common.sh hotplug/Linux/xendomains hotplug/NetBSD/rc.d/xencommons hotplug/NetBSD/rc.d/xendriverdomain hotplug/NetBSD/rc.d/xlpcid ocaml/xenstored/oxenstored.conf"
 
 ac_config_headers="$ac_config_headers config.h"
 
@@ -10053,7 +10053,7 @@ fi
 
 if test "x$systemd" = "xy"; then :
 
-    ac_config_files="$ac_config_files hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/var-lib-xenstored.mount hotplug/Linux/systemd/xen-init-dom0.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xen-watchdog.service hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xendriverdomain.service hotplug/Linux/systemd/xenstored.service"
+    ac_config_files="$ac_config_files hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/var-lib-xenstored.mount hotplug/Linux/systemd/xen-init-dom0.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xen-watchdog.service hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xendriverdomain.service hotplug/Linux/systemd/xenstored.service hotplug/Linux/systemd/xenpcid.service"
 
 
 fi
@@ -10917,8 +10917,10 @@ do
     "../config/Tools.mk") CONFIG_FILES="$CONFIG_FILES ../config/Tools.mk" ;;
     "hotplug/FreeBSD/rc.d/xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/FreeBSD/rc.d/xencommons" ;;
     "hotplug/FreeBSD/rc.d/xendriverdomain") CONFIG_FILES="$CONFIG_FILES hotplug/FreeBSD/rc.d/xendriverdomain" ;;
+    "hotplug/FreeBSD/rc.d/xlpcid") CONFIG_FILES="$CONFIG_FILES hotplug/FreeBSD/rc.d/xlpcid" ;;
     "hotplug/Linux/init.d/sysconfig.xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/sysconfig.xencommons" ;;
     "hotplug/Linux/init.d/sysconfig.xendomains") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/sysconfig.xendomains" ;;
+    "hotplug/Linux/init.d/xlpcid") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xlpcid" ;;
     "hotplug/Linux/init.d/xen-watchdog") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xen-watchdog" ;;
     "hotplug/Linux/init.d/xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xencommons" ;;
     "hotplug/Linux/init.d/xendomains") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xendomains" ;;
@@ -10929,6 +10931,7 @@ do
     "hotplug/Linux/xendomains") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/xendomains" ;;
     "hotplug/NetBSD/rc.d/xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/NetBSD/rc.d/xencommons" ;;
     "hotplug/NetBSD/rc.d/xendriverdomain") CONFIG_FILES="$CONFIG_FILES hotplug/NetBSD/rc.d/xendriverdomain" ;;
+    "hotplug/NetBSD/rc.d/xlpcid") CONFIG_FILES="$CONFIG_FILES hotplug/NetBSD/rc.d/xlpcid" ;;
     "ocaml/xenstored/oxenstored.conf") CONFIG_FILES="$CONFIG_FILES ocaml/xenstored/oxenstored.conf" ;;
     "config.h") CONFIG_HEADERS="$CONFIG_HEADERS config.h" ;;
     "hotplug/Linux/systemd/proc-xen.mount") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/proc-xen.mount" ;;
@@ -10940,6 +10943,7 @@ do
     "hotplug/Linux/systemd/xendomains.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xendomains.service" ;;
     "hotplug/Linux/systemd/xendriverdomain.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xendriverdomain.service" ;;
     "hotplug/Linux/systemd/xenstored.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xenstored.service" ;;
+    "hotplug/Linux/systemd/xenpcid.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xenpcid.service" ;;
 
   *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
   esac
tools/configure.ac +1 -0
index f29c319b42..a874e96608 100644
@@ -477,6 +477,7 @@ AS_IF([test "x$systemd" = "xy"], [
     hotplug/Linux/systemd/xendomains.service
     hotplug/Linux/systemd/xendriverdomain.service
     hotplug/Linux/systemd/xenstored.service
+    hotplug/Linux/systemd/xenpcid.service
     ])
 ])
 
tools/hotplug/FreeBSD/rc.d/xlpcid.in new file +75 -0

75 lines not on this page · raw message

tools/hotplug/Linux/init.d/xlpcid.in new file +76 -0

76 lines not on this page · raw message

tools/hotplug/Linux/systemd/Makefile +1 -0
index a5d41d86ef..a3820e5f0a 100644
@@ -13,6 +13,7 @@ XEN_SYSTEMD_SERVICE += xendomains.service
 XEN_SYSTEMD_SERVICE += xen-watchdog.service
 XEN_SYSTEMD_SERVICE += xen-init-dom0.service
 XEN_SYSTEMD_SERVICE += xendriverdomain.service
+XEN_SYSTEMD_SERVICE += xenpcid.service
 
 ALL_XEN_SYSTEMD =	$(XEN_SYSTEMD_MODULES)  \
 			$(XEN_SYSTEMD_MOUNT)	\
tools/hotplug/Linux/systemd/xenpcid.service.in new file +10 -0
new file mode 100644
index 0000000000..49c57f635a
@@ -0,0 +1,10 @@
+[Unit]
+Description=Xen PCI host daemon
+ConditionVirtualization=xen
+
+[Service]
+Type=forking
+ExecStart=@sbindir@/xl pcid
+
+[Install]
+WantedBy=multi-user.target
tools/hotplug/NetBSD/rc.d/xlpcid.in new file +75 -0

75 lines not on this page · raw message

tools/include/libxl.h +1 -1
index 310a3468a1..67ba46c78f 100644
@@ -2465,7 +2465,7 @@ int libxl_device_events_handler(libxl_ctx *ctx,
  */
 int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_device_pci *pci, int rebind);
 int libxl_device_pci_assignable_remove(libxl_ctx *ctx, libxl_device_pci *pci, int rebind);
-libxl_device_pci *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *num);
+libxl_device_pci *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *num, libxl_domid backend_domid);
 void libxl_device_pci_assignable_list_free(libxl_device_pci *list, int num);
 
 /* CPUID handling */
tools/include/pcid.h new file +228 -0

228 lines not on this page · raw message

tools/libs/light/Makefile +1 -0
index ea8994af6d..b64658e72f 100644
@@ -77,6 +77,7 @@ SRCS-y += libxl.c
 SRCS-y += libxl_create.c
 SRCS-y += libxl_dm.c
 SRCS-y += libxl_pci.c
+SRCS-y += libxl_pcid.c
 SRCS-y += libxl_vchan.c
 SRCS-y += libxl_dom.c
 SRCS-y += libxl_exec.c
tools/libs/light/libxl_pci.c +341 -360

910 lines not on this page · raw message

tools/libs/light/libxl_pcid.c new file +1095 -0

1,095 lines not on this page · raw message

tools/libs/light/libxl_types.idl +2 -0
index f387d7b8b2..458acc0748 100644
@@ -802,6 +802,8 @@ libxl_device_pci = Struct("device_pci", [
     ("seize", bool),
     ("rdm_policy", libxl_rdm_reserve_policy),
     ("name", string),
+    ("backend", string),
+    ("backend_domid", libxl_domid),
     ])
 
 libxl_device_rdm = Struct("device_rdm", [
tools/libs/light/libxl_vchan.c +39 -25

106 lines not on this page · raw message

tools/libs/light/libxl_vchan.h +5 -2
index 0968875298..e16db7a6ed 100644
@@ -45,6 +45,7 @@ struct vchan_info {
     /* Buffer info. */
     size_t receive_buf_size;
     size_t max_buf_size;
+    bool initialized;
 };
 
 int libxl__vchan_field_add_string(libxl__gc *gc, yajl_gen hand,
@@ -58,7 +59,9 @@ static inline libxl__json_object *libxl__vchan_start_args(libxl__gc *gc)
 void libxl__vchan_arg_add_string(libxl__gc *gc, libxl__json_object *args,
                                  char *key, char *val);
 void libxl__vchan_arg_add_bool(libxl__gc *gc, libxl__json_object *args,
-                               char *key, bool val);
+                                 char *key, bool val);
+void libxl__vchan_arg_add_integer(libxl__gc *gc, libxl__json_object *args,
+                                 char *key,  int val);
 
 libxl__json_object *vchan_send_command(libxl__gc *gc, struct vchan_info *vchan,
                                        char *cmd, libxl__json_object *args);
@@ -72,7 +75,7 @@ char *vchan_get_server_xs_path(libxl__gc *gc, libxl_domid domid, char *srv_name)
 struct vchan_state *vchan_init_new_state(libxl__gc *gc, libxl_domid domid,
                                          char *vchan_xs_path, bool is_server);
 
-struct vchan_state *vchan_new_client(libxl__gc *gc, char *srv_name);
+struct vchan_state *vchan_new_client(libxl__gc *gc, char *srv_name, libxl_domid domid);
 
 void vchan_fini_one(libxl__gc *gc, struct vchan_state *state);
 
tools/libs/util/libxlu_pci.c +5 -1
index 551d8e3aed..7e2e8c6a6f 100644
@@ -1,6 +1,8 @@
 #define _GNU_SOURCE
 
 #include <ctype.h>
+#include <stdint.h>
+#include <xen/xen.h>
 
 #include "libxlu_internal.h"
 #include "libxlu_disk_l.h"
@@ -195,8 +197,10 @@ int xlu_pci_parse_spec_string(XLU_Config *cfg, libxl_device_pci *pci,
             name_present = true;
             pci->name = strdup(val);
             if (!pci->name) ret = ERROR_NOMEM;
+        } else if (!strcmp(key, "backend")) {
+            pci->backend = strdup(val);
         } else {
-            XLU__PCI_ERR(cfg, "Unknown PCI_SPEC_STRING option: %s", key);
+            XLU__PCI_ERR(cfg, "Unknown PCI_SPEC_STRING option: '%s'\n", key);
             ret = ERROR_INVAL;
         }
 
tools/xl/Makefile +2 -2
index 5dfff7fc6f..0dbbbde17f 100644
@@ -22,7 +22,7 @@ XL_OBJS = xl.o xl_cmdtable.o xl_sxp.o xl_utils.o $(XL_OBJS-y)
 XL_OBJS += xl_parse.o xl_cpupool.o xl_flask.o
 XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o
 XL_OBJS += xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o
-XL_OBJS += xl_info.o xl_console.o xl_misc.o
+XL_OBJS += xl_info.o xl_console.o xl_misc.o xl_pcid.o
 XL_OBJS += xl_vmcontrol.o xl_saverestore.o xl_migrate.o
 XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o
 
@@ -34,7 +34,7 @@ $(XL_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs i
 all: xl
 
 xl: $(XL_OBJS)
-	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenstore) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS)
 
 .PHONY: install
 install: all
tools/xl/xl.h +1 -0
index c5c4bedbdd..1419a8c1e4 100644
@@ -202,6 +202,7 @@ int main_loadpolicy(int argc, char **argv);
 int main_remus(int argc, char **argv);
 #endif
 int main_devd(int argc, char **argv);
+int main_pcid(int argc, char **argv);
 #if defined(__i386__) || defined(__x86_64__)
 int main_psr_hwinfo(int argc, char **argv);
 int main_psr_cmt_attach(int argc, char **argv);
tools/xl/xl_cmdtable.c +7 -0
index 661323d488..5fbbe25a43 100644
@@ -545,6 +545,13 @@ const struct cmd_spec cmd_table[] = {
       "-F                      Run in the foreground.\n"
       "-p, --pidfile [FILE]    Write PID to pidfile when daemonizing.",
     },
+    { "pcid",
+      &main_pcid, 0, 1,
+      "Daemon that acts as a server for the client in the libxl PCI",
+      "[options]",
+      "-f                      Run in the foreground.\n"
+      "-p, --pidfile [FILE]    Write PID to pidfile when daemonizing.",
+    },
 #if defined(__i386__) || defined(__x86_64__)
     { "psr-hwinfo",
       &main_psr_hwinfo, 0, 1,
tools/xl/xl_parse.c +1 -0
index c8d2b794e5..5052ad9fe4 100644
@@ -1491,6 +1491,7 @@ void parse_config_data(const char *config_source,
             pci->power_mgmt = pci_power_mgmt;
             pci->permissive = pci_permissive;
             pci->seize = pci_seize;
+            pci->backend = NULL;
             /*
              * Like other pci option, the per-device policy always follows
              * the global policy by default.
tools/xl/xl_pci.c +3 -1
index b1c3ae2a72..a1c0bc1e17 100644
@@ -13,7 +13,9 @@
  */
 
 #include <stdlib.h>
+#include <stdint.h>
 
+#include <xen/xen.h>
 #include <libxl.h>
 #include <libxl_utils.h>
 #include <libxlutil.h>
@@ -156,7 +158,7 @@ static void pciassignable_list(bool show_names)
     libxl_device_pci *pcis;
     int num, i;
 
-    pcis = libxl_device_pci_assignable_list(ctx, &num);
+    pcis = libxl_device_pci_assignable_list(ctx, &num, DOMID_INVALID);
 
     if ( pcis == NULL )
         return;
tools/xl/xl_pcid.c new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.25.1
#8 lore raw

Re: [PATCH v2 4/4] tools/xl: Add pcid daemon to xl

Quoted 24 lines · On Wed, Jul 13, 2022 at 06:03:11PM +0300, [email protected] wrote:

From: Oleksandr Andrushchenko <[email protected]>

Add pcid daemon (based on vchan-node2) implements pcid protocol. Protocol is OS independed and should work on ane supported OS.

Add essential functionality to handle pcid protocol:
- define required constants
- prepare for handling remote requests
- prepare and send an error packet

pcid server used if domain has passthrough PCI controller and we wants assign some device to other domain.
pcid server should be launched in domain owns the PCI controller and process request from other domains.

Message exchange imnplementation based on JSON via libvchan. Supported messages:
- make_assignable
- revert_assignable
- is_device_assigned
- resource_list
- reset_device
- write_bdf

Quoted 4 lines

Hi Dmitry,

I don't think libxl is the right place to implement a daemon, and xl isn't the place to start a new daemon either. Could you look into creating a new binary which implements only "pcid"?

I understand that making use of the facilities in libxl makes writing this daemon a bit easier, but still I don't think the code belong to libxl. Maybe some of libxl's code could be moved to libxlu (utils) if that help, but I'm not sure.

Also, this patch is way to big, makes too many changes, including changes to existing libxl's API which we try to avoid. (Adding new functions is fine, changing the prototype of existing one is what we try to avoid.)

As for the protocol, it might be better to have the description somewhere in "docs/" instead of within a C header. I'm not sure which sub-directory as we have protocols in different one, like in "misc" or "design" or "features", but "misc" might be the one. Also, the document should say somewhere that the protocol is based on JSON, as that's missing in pcid.h.

Thanks,

Signature · 1 line
Anthony PERARD
Reviewed-by on v2 1/4
#9 lore raw
In reply to [email protected]
Quoted 9 lines · On 13.07.22 17:03, [email protected] wrote:

From: Oleksandr Andrushchenko <[email protected]>

vchan server creates XenStore entries to advertise its event channel and ring, but those are not removed after the server quits.
Add additional cleanup step, so those are removed, so clients do not try to connect to a non-existing server.

Juergen

#10 lore raw
Quoted 8 lines · Juergen Gross
Quoted 7 lines · On 13.07.22 17:03, [email protected] wrote:

From: Oleksandr Andrushchenko <[email protected]>

vchan server creates XenStore entries to advertise its event channel and ring, but those are not removed after the server quits.
Add additional cleanup step, so those are removed, so clients do not try to connect to a non-existing server.

Quoted 4 lines
Quoted 2 lines

Anthony - any chance of getting an ack for at least this first patch? The others are more like feature additions, so perhaps aren't to go in anymore before 4.18 opens ...

Jan

#11 lore raw
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Juergen Gross
Quoted 7 lines · On 13.07.22 17:03, [email protected] wrote:

From: Oleksandr Andrushchenko <[email protected]>

vchan server creates XenStore entries to advertise its event channel and ring, but those are not removed after the server quits.
Add additional cleanup step, so those are removed, so clients do not try to connect to a non-existing server.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Anthony - any chance of getting an ack for at least this first patch? The others are more like feature additions, so perhaps aren't to go in anymore before 4.18 opens ...

Thanks,

Signature · 1 line
Anthony PERARD