Skip to contentSkip to conversation

tools/xenstored: add support for watch depth feature

PATCH Superseded Juergen Gross 2 participants 24 messages Mar 13 – Apr 21

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover tools/xenstored: add support for watch depth feature

In order to reduce excessive number of watch events the watch depth feature has been defined, allowing the user to limit the number of node levels below the watched node to cause events.

This series is implementing this feature for xenstored (including the PV and PVH stubdom variants).

Possible users are:

- xl/libxl: use domid information for the special @introduceDomain and @releaseDomain watches, which will be available when using depth = 1

- xenconsoled, like xl/libxl

- any PV-device backends watching directories for added devices (no watch events when any device specific node is modified)

- any PV-device frontend and backend watching the "state" node of the other side, in case the other side plays dirty games by building large sub-trees beneath the "state" node

This series has been tested with a modified xenstore-watch client. The modification will be sent as soon as the pending series of the xs-clients has been applied.

Note that the watch depth series depends on the still pending series "tools: add support for per-domain xenstore quota".

Juergen Gross (8):
tools/xenstored: allow domU to get own features
docs: clarify Xenstore watch depth feature tools/libs/store: add support to use watches with a depth parameter tools/xenstored: add depth information to watches tools/xenstored: add depth handling to XS_WATCH tools/xenstored: replace the fire_watches() exact parameter tools/xenstored: expand special watch handling with depth feature tools/xenstored: make XENSTORE_SERVER_FEATURE_WATCHDEPTH available

 docs/man/xl.cfg.5.pod.in         |   6 ++
 docs/misc/xenstore.txt           |  14 ++-
 tools/include/xenstore.h         |  16 +++
 tools/libs/store/libxenstore.map |   2 +
 tools/libs/store/xs.c            | 116 ++++++++++++++++++----
 tools/xenstored/core.c           |  15 ++-
 tools/xenstored/domain.c         |  64 +++++++++---
 tools/xenstored/domain.h         |   2 +
 tools/xenstored/lu.c             |   3 +
 tools/xenstored/transaction.c    |  14 +--
 tools/xenstored/transaction.h    |   4 +-
 tools/xenstored/watch.c          | 161 ++++++++++++++++++++++---------
 tools/xenstored/watch.h          |  10 +-
 xen/include/public/io/xs_wire.h  |   2 +
 14 files changed, 328 insertions(+), 101 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/8 tools/xenstored: allow domU to get own features

Expand the XS_GET_FEATURE command to allow an unprivileged domain to read its own available features. While this information is available via the related field in the shared page used for communication with Xenstore, user land components of the domU would need special support in the related kernel driver to obtain that data. With supporting the XS_GET_FEATURE for that purpose, only an up to date libxenstore is needed.

---
 docs/misc/xenstore.txt   |  9 +++++----
 tools/xenstored/core.c   |  3 +--
 tools/xenstored/domain.c | 16 +++++++++++-----
 3 files changed, 17 insertions(+), 11 deletions(-)
docs/misc/xenstore.txt +5 -4
index 4eccbc2f7f..8a2c19d116 100644
@@ -427,13 +427,14 @@ SET_FEATURE		<domid>|<value>|
 	to set a bit for a feature not being supported by the running
 	Xenstore will be denied. Providing no <domid> with the
 	GET_FEATURE command will return the features which are supported
-	by Xenstore.
+	by Xenstore for the domain issuing the command.
 
 	SET_FEATURE for a domain will be rejected after the INTRODUCE
-	command for this domain has been sent to xenstored.
+	command for this domain has been sent to xenstored. This has the
+	effect that dom0 will always have all supported features enabled.
 
-	xenstored prevents the use of GET_FEATURE and SET_FEATURE other
-	than by dom0.
+	xenstored prevents the use of GET_FEATURE with a domid specified
+	and SET_FEATURE other than by dom0.
 
 GET_QUOTA		[[<domid>|]<quota>|]	<value>|
 SET_QUOTA		[<domid>|]<quota>|<value>|
tools/xenstored/core.c +1 -2
index e283d47184..09f1390f14 100644
@@ -2030,8 +2030,7 @@ static struct {
 	    { "SET_TARGET",    do_set_target,   XS_FLAG_PRIV },
 	[XS_RESET_WATCHES]     = { "RESET_WATCHES",     do_reset_watches },
 	[XS_DIRECTORY_PART]    = { "DIRECTORY_PART",    send_directory_part },
-	[XS_GET_FEATURE]       =
-	    { "GET_FEATURE",   do_get_feature,  XS_FLAG_PRIV },
+	[XS_GET_FEATURE]       = { "GET_FEATURE",       do_get_feature },
 	[XS_SET_FEATURE]       =
 	    { "SET_FEATURE",   do_set_feature,  XS_FLAG_PRIV },
 	[XS_GET_QUOTA]         =
tools/xenstored/domain.c +11 -5
index c0bc8a3eb7..ebeced3228 100644
@@ -1293,17 +1293,23 @@ int do_get_feature(const void *ctx, struct connection *conn,
 	char *result;
 
 	n_args = get_strings(in, vec, ARRAY_SIZE(vec));
-	if (n_args > 1)
-		return EINVAL;
 
-	if (n_args == 1) {
+	if (!n_args) {
+		features = conn->domain ? conn->domain->features
+					: XENSTORE_FEATURES;
+	} else {
+		if (domain_is_unprivileged(conn))
+			return EACCES;
+
+		if (n_args > 1)
+			return EINVAL;
+
 		domid = atoi(vec[0]);
 		domain = find_or_alloc_existing_domain(domid);
 		if (!domain)
 			return ENOENT;
 		features = domain->features;
-	} else
-		features = XENSTORE_FEATURES;
+	}
 
 	result = talloc_asprintf(ctx, "%u", features);
 	if (!result)
Signature · 1 line
2.53.0
Reviewed-by on v1 1/8
#2 lore raw

Re: [PATCH 1/8] tools/xenstored: allow domU to get own features

Quoted 20 lines · Juergen Gross

Expand the XS_GET_FEATURE command to allow an unprivileged domain to read its own available features. While this information is available via the related field in the shared page used for communication with Xenstore, user land components of the domU would need special support in the related kernel driver to obtain that data. With supporting the XS_GET_FEATURE for that purpose, only an up to date libxenstore is needed.

---
  docs/misc/xenstore.txt   |  9 +++++----
  tools/xenstored/core.c   |  3 +--
  tools/xenstored/domain.c | 16 +++++++++++-----
  3 files changed, 17 insertions(+), 11 deletions(-)
docs/misc/xenstore.txt +0 -0
index 4eccbc2f7f..8a2c19d116 100644

docs/misc/xenstore.txt SET_FEATURE <domid>|<value>| · line 434

Quoted 10 lines
docs/misc/xenstore.txt +3 -2
@@ -427,13 +427,14 @@ SET_FEATURE		<domid>|<value>|
  	to set a bit for a feature not being supported by the running
  	Xenstore will be denied. Providing no <domid> with the
  	GET_FEATURE command will return the features which are supported
-	by Xenstore.
+	by Xenstore for the domain issuing the command.
 
  	SET_FEATURE for a domain will be rejected after the INTRODUCE
-	command for this domain has been sent to xenstored.
+	command for this domain has been sent to xenstored. This has the
+	effect that dom0 will always have all supported features enabled.

This statement...

tools/xenstored/domain.c int do_get_feature(const void *ctx, struct connection *conn, · line 1299

Quoted 13 lines
tools/xenstored/domain.c +3 -3
@@ -1293,17 +1293,23 @@ int do_get_feature(const void *ctx, struct connection *conn,
  	char *result;
 
  	n_args = get_strings(in, vec, ARRAY_SIZE(vec));
-	if (n_args > 1)
-		return EINVAL;
 
-	if (n_args == 1) {
+	if (!n_args) {
+		features = conn->domain ? conn->domain->features
+					: XENSTORE_FEATURES;

... cover this, yes? With a stubdom, dom0 will have conn->domain. conn->domain->features is initialized to XENSTORE_FEATURES and cannot change. Provided that is correct:

Thanks,
Jason

#3 lore raw

Re: [PATCH 1/8] tools/xenstored: allow domU to get own features

Quoted 47 lines · Jason Andryuk
Quoted 21 lines · Juergen Gross

Expand the XS_GET_FEATURE command to allow an unprivileged domain to read its own available features. While this information is available via the related field in the shared page used for communication with Xenstore, user land components of the domU would need special support in the related kernel driver to obtain that data. With supporting the XS_GET_FEATURE for that purpose, only an up to date libxenstore is needed.

---
  docs/misc/xenstore.txt   |  9 +++++----
  tools/xenstored/core.c   |  3 +--
  tools/xenstored/domain.c | 16 +++++++++++-----

  3 files changed, 17 insertions(+), 11 deletions(-)
docs/misc/xenstore.txt +0 -0
index 4eccbc2f7f..8a2c19d116 100644
@@ -427,13 +427,14 @@ SET_FEATURE        <domid>|<value>|

      to set a bit for a feature not being supported by the running

Quoted 8 lines

      Xenstore will be denied. Providing no <domid> with the GET_FEATURE command will return the features which are supported -    by Xenstore.
+    by Xenstore for the domain issuing the command. SET_FEATURE for a domain will be rejected after the INTRODUCE -    command for this domain has been sent to xenstored.
+    command for this domain has been sent to xenstored. This has the +    effect that dom0 will always have all supported features enabled.

This statement...

tools/xenstored/domain.c int do_get_feature(const void *ctx, struct connection · line 1298

Quoted 12 lines
tools/xenstored/domain.c +3 -3
@@ -1293,17 +1293,23 @@ int do_get_feature(const void *ctx, struct connection 
 *conn,
       char *result;
       n_args = get_strings(in, vec, ARRAY_SIZE(vec));
-    if (n_args > 1)
-        return EINVAL;
-    if (n_args == 1) {
+    if (!n_args) {
+        features = conn->domain ? conn->domain->features
+                    : XENSTORE_FEATURES;

... cover this, yes?  With a stubdom, dom0 will have conn->domain. conn->domain-

Quoted 2 lines
Quoted 1 line

features is initialized to XENSTORE_FEATURES and cannot change.  Provided that

is correct:

This is correct.

Quoted 2 lines

Thanks,

Juergen

#4 lore raw

PATCH 2/8 docs: clarify Xenstore watch depth feature

In reply to Juergen Gross

Make it explicit that for watching the @releaseDomain/<domid> special node, the XENSTORE_SERVER_FEATURE_WATCHDEPTH is needed.

---
 docs/misc/xenstore.txt | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
docs/misc/xenstore.txt +4 -1
index 8a2c19d116..dba6471387 100644
@@ -266,7 +266,8 @@ WATCH			<wpath>|<token>|[<depth>|]?
 	decimal value of 0 or larger): it denotes the directory levels
 	below <wpath> to consider for a match ("0" would not match for
 	a child of <wpath>, "1" would match only for a direct child,
-	etc.).
+	etc.).  The depth specification is possible only, if the
+	XENSTORE_SERVER_FEATURE_WATCHDEPTH feature is available.
 
 	<wpath> can be a <path> to watch or @<wspecial>.  In the
 	latter case <wspecial> may have any syntax but it matches
@@ -284,6 +285,8 @@ WATCH			<wpath>|<token>|[<depth>|]?
 	values are not supported.
 	For @releaseDomain it is possible to watch only for a specific
 	domain by specifying @releaseDomain/<domid> for the path.
+	This requires the XENSTORE_SERVER_FEATURE_WATCHDEPTH to be
+	available.
 
 	When a watch is first set up it is triggered once straight
 	away, with <path> equal to <wpath>.  Watches may be triggered
Signature · 1 line
2.53.0
#6 lore raw

PATCH 3/8 tools/libs/store: add support to use watches with a depth parameter

In reply to Juergen Gross

Add a new xs_watch_depth() function to libxenstore allowing to limit the scope of a Xenstore watch. It can be used only in case Xenstore is supporting the XENSTORE_SERVER_FEATURE_WATCHDEPTH feature.

For convenience add a xs_watch_try_depth() wrapper, which will call xs_watch_depth() if supported and xs_watch() otherwise.

Cache the supported features of Xenstore in order not having to get them from Xenstore for each call of one of the new functions.

---
 docs/man/xl.cfg.5.pod.in         |   6 ++
 tools/include/xenstore.h         |  16 +++++
 tools/libs/store/libxenstore.map |   2 +
 tools/libs/store/xs.c            | 116 ++++++++++++++++++++++++++-----
 xen/include/public/io/xs_wire.h  |   2 +
 5 files changed, 123 insertions(+), 19 deletions(-)
docs/man/xl.cfg.5.pod.in +6 -0
index 3aac0bc4fb..2f77016ecf 100644
@@ -740,6 +740,12 @@ Xenstore is capable to reconnect to a guest.
 Xenstore will present an error value in case it disconnects due to an error
 condition.
 
+=item B<0x00000004>
+
+Xenstore supports to set watches with a limited depth (depth 0 matches
+only the watched node, depth 1 matches the node and its direct children,
+etc.).
+
 =back
 
 The features supported by the running Xenstore instance can be retrieved
tools/include/xenstore.h +16 -0
index 6b661e5895..8a6afd97f5 100644
@@ -177,6 +177,22 @@ bool xs_set_permissions(struct xs_handle *h, xs_transaction_t t,
  */
 bool xs_watch(struct xs_handle *h, const char *path, const char *token);
 
+/* Same as xs_watch(), but with limiting the matching for modified
+ * children to a specified depth (depth 0 only matches the node itself,
+ * depth 1 will additionally match direct children of the node, etc.).
+ * Only supported if the XENSTORE_SERVER_FEATURE_WATCHDEPTH (4) is set
+ * in the returned features of xs_get_features_supported().
+ */
+bool xs_watch_depth(struct xs_handle *h, const char *path, const char *token,
+		    unsigned int depth);
+
+/* If supported, same as xs_watch_depth(), use xs_watch() otherwise.
+ * As a result watches might trigger for nodes below the watched path, too.
+ * Not to be used for special watches!
+ */
+bool xs_watch_try_depth(struct xs_handle *h, const char *path,
+			const char *token, unsigned int depth);
+
 /* Return the FD to poll on to see if a watch has fired. */
 int xs_fileno(struct xs_handle *h);
 
tools/libs/store/libxenstore.map +2 -0
index a08ddd549f..7067068998 100644
@@ -52,4 +52,6 @@ VERS_4.2 {
 		xs_set_global_quota;
 		xs_get_domain_quota;
 		xs_set_domain_quota;
+		xs_watch_depth;
+		xs_watch_try_depth;
 } VERS_4.1;
tools/libs/store/xs.c +97 -19

163 lines not on this page · raw message

xen/include/public/io/xs_wire.h +2 -0
index d2e2b8b9eb..2e763bc877 100644
@@ -124,6 +124,8 @@ struct xenstore_domain_interface {
 #define XENSTORE_SERVER_FEATURE_RECONNECTION 1
 /* The presence of the "error" field in the ring page */
 #define XENSTORE_SERVER_FEATURE_ERROR        2
+/* The XS_WATCH command can be used with a <depth> parameter */
+#define XENSTORE_SERVER_FEATURE_WATCHDEPTH   4
 
 /* Valid values for the connection field */
 #define XENSTORE_CONNECTED 0 /* the steady-state */
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH 3/8] tools/libs/store: add support to use watches with a depth parameter

Quoted 46 lines · Juergen Gross

Add a new xs_watch_depth() function to libxenstore allowing to limit the scope of a Xenstore watch. It can be used only in case Xenstore is supporting the XENSTORE_SERVER_FEATURE_WATCHDEPTH feature.

For convenience add a xs_watch_try_depth() wrapper, which will call xs_watch_depth() if supported and xs_watch() otherwise.

Cache the supported features of Xenstore in order not having to get them from Xenstore for each call of one of the new functions.

tools/libs/store/xs.c +10 -11
@@ -984,37 +984,29 @@ bool xs_restrict(struct xs_handle *h, unsigned domid)
  	return false;
  }
 
-/* Watch a node for changes (poll on fd to detect, or call read_watch()).
- * When the node (or any child) changes, fd will become readable.
- * Token is returned when watch is read, to allow matching.
- * Returns false on failure.
- */
-bool xs_watch(struct xs_handle *h, const char *path, const char *token)
+static bool xs_watch_helper(struct xs_handle *h)
  {
-	struct xsd_sockmsg msg = { .type = XS_WATCH };
-	struct iovec iov[3];
-
  #ifdef USE_PTHREAD
+	sigset_t set, old_set;
+	pthread_attr_t attr;
+	static size_t stack_size;
+#ifdef USE_DLSYM
+	size_t (*getsz)(pthread_attr_t *attr);
+#endif
+
  #define DEFAULT_THREAD_STACKSIZE (16 * 1024)
  /* NetBSD doesn't have PTHREAD_STACK_MIN. */
  #ifndef PTHREAD_STACK_MIN
  # define PTHREAD_STACK_MIN 0
  #endif
 
-#define READ_THREAD_STACKSIZE 					\
-	((DEFAULT_THREAD_STACKSIZE < PTHREAD_STACK_MIN) ? 	\
+#define READ_THREAD_STACKSIZE					\
+	((DEFAULT_THREAD_STACKSIZE < PTHREAD_STACK_MIN) ?	\
  	 PTHREAD_STACK_MIN : DEFAULT_THREAD_STACKSIZE)

tools/libs/store/xs.c bool xs_restrict(struct xs_handle *h, unsigned domid) · line 1016

Quoted 10 lines
tools/libs/store/xs.c +0 -6
 
  	/* We dynamically create a reader thread on demand. */
  	mutex_lock(&h->request_mutex);
  	if (!h->read_thr_exists) {
-		sigset_t set, old_set;
-		pthread_attr_t attr;
-		static size_t stack_size;
-#ifdef USE_DLSYM
-		size_t (*getsz)(pthread_attr_t *attr);
-#endif

AFAICT, there is no particular reason to change the scope of these variables. Am I missing something?

Everything else looks okay.

Thanks,
Jason

#8 lore raw

Re: [PATCH 3/8] tools/libs/store: add support to use watches with a depth parameter

Quoted 48 lines · Jason Andryuk
Quoted 47 lines · Juergen Gross

Add a new xs_watch_depth() function to libxenstore allowing to limit the scope of a Xenstore watch. It can be used only in case Xenstore is supporting the XENSTORE_SERVER_FEATURE_WATCHDEPTH feature.

For convenience add a xs_watch_try_depth() wrapper, which will call xs_watch_depth() if supported and xs_watch() otherwise.

Cache the supported features of Xenstore in order not having to get them from Xenstore for each call of one of the new functions.

tools/libs/store/xs.c +10 -11
@@ -984,37 +984,29 @@ bool xs_restrict(struct xs_handle *h, unsigned domid)
       return false;
   }
-/* Watch a node for changes (poll on fd to detect, or call read_watch()).
- * When the node (or any child) changes, fd will become readable.
- * Token is returned when watch is read, to allow matching.
- * Returns false on failure.
- */
-bool xs_watch(struct xs_handle *h, const char *path, const char *token)
+static bool xs_watch_helper(struct xs_handle *h)
   {
-    struct xsd_sockmsg msg = { .type = XS_WATCH };
-    struct iovec iov[3];
-
   #ifdef USE_PTHREAD
+    sigset_t set, old_set;
+    pthread_attr_t attr;
+    static size_t stack_size;
+#ifdef USE_DLSYM
+    size_t (*getsz)(pthread_attr_t *attr);
+#endif
+
   #define DEFAULT_THREAD_STACKSIZE (16 * 1024)
   /* NetBSD doesn't have PTHREAD_STACK_MIN. */
   #ifndef PTHREAD_STACK_MIN
   # define PTHREAD_STACK_MIN 0
   #endif
-#define READ_THREAD_STACKSIZE                     \
-    ((DEFAULT_THREAD_STACKSIZE < PTHREAD_STACK_MIN) ?     \
+#define READ_THREAD_STACKSIZE                    \
+    ((DEFAULT_THREAD_STACKSIZE < PTHREAD_STACK_MIN) ?    \
        PTHREAD_STACK_MIN : DEFAULT_THREAD_STACKSIZE)
       /* We dynamically create a reader thread on demand. */
       mutex_lock(&h->request_mutex);
       if (!h->read_thr_exists) {
Quoted 8 lines

tools/libs/store/xs.c bool xs_restrict(struct xs_handle *h, unsigned domid) · line 1016

Quoted 6 lines
tools/libs/store/xs.c +0 -6
-        sigset_t set, old_set;
-        pthread_attr_t attr;
-        static size_t stack_size;
-#ifdef USE_DLSYM
-        size_t (*getsz)(pthread_attr_t *attr);
-#endif

AFAICT, there is no particular reason to change the scope of these variables. Am I missing something?

That wasn't on purpose.

Seems to be an artifact of creating the new xs_watch_helper() function.

I'll fix that and send a V2.

Quoted 2 lines

Everything else looks okay.

Thanks,

Juergen

#9 lore raw

PATCH 4/8 tools/xenstored: add depth information to watches

In reply to Juergen Gross

Add the depth for matching subdirectories of a watch to the watch handling. A depth value of -1 is used for the current model of no limit.

---
 tools/xenstored/lu.c    |   3 ++
 tools/xenstored/watch.c | 110 ++++++++++++++++++++++++++++------------
 tools/xenstored/watch.h |   1 +
 3 files changed, 81 insertions(+), 33 deletions(-)
tools/xenstored/lu.c +3 -0
index eaffdbc69e..7d9f086a59 100644
@@ -183,6 +183,9 @@ void lu_read_state(void)
 		case XS_STATE_TYPE_WATCH:
 			read_state_watch(ctx, state.buf);
 			break;
+		case XS_STATE_TYPE_WATCH_EXT:
+			read_state_watch_ext(ctx, state.buf);
+			break;
 		case XS_STATE_TYPE_TA:
 			xprintf("live-update: ignore transaction record\n");
 			break;
tools/xenstored/watch.c +77 -33

191 lines not on this page · raw message

tools/xenstored/watch.h +1 -0
index d9ac6a334a..afdfdc6b2f 100644
@@ -37,5 +37,6 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 			       unsigned int conn_id);
 
 void read_state_watch(const void *ctx, const void *state);
+void read_state_watch_ext(const void *ctx, const void *state);
 
 #endif /* _XENSTORED_WATCH_H */
Signature · 1 line
2.53.0
#11 lore raw

PATCH 5/8 tools/xenstored: add depth handling to XS_WATCH

In reply to Juergen Gross

In case XENSTORE_SERVER_FEATURE_WATCHDEPTH is set, allow XS_WATCH to have "depth" as 3rd parameter.

---
 tools/xenstored/domain.c |  8 ++++++++
 tools/xenstored/domain.h |  2 ++
 tools/xenstored/watch.c  | 11 +++++++++--
 3 files changed, 19 insertions(+), 2 deletions(-)
tools/xenstored/domain.c +8 -0
index ebeced3228..866c0aa908 100644
@@ -387,6 +387,14 @@ void wrl_apply_debit_trans_commit(struct connection *conn)
 	wrl_apply_debit_actual(conn->domain);
 }
 
+bool feature_available(const struct connection *conn, unsigned int feature)
+{
+	if (!conn->domain)
+		return XENSTORE_FEATURES & feature;
+
+	return conn->domain->features & feature;
+}
+
 static unsigned int domain_get_soft_quota(struct domain *d, enum accitem what)
 {
 	if (d && d->acc[what].val[Q_IDX_SOFT] != Q_VAL_DISABLED)
tools/xenstored/domain.h +2 -0
index 6a06b0d1af..15d8f1a5b2 100644
@@ -192,4 +192,6 @@ struct hashtable *domain_check_acc_init(void);
 void domain_check_acc_add(const struct node *node, struct hashtable *domains);
 void domain_check_acc(struct hashtable *domains);
 
+bool feature_available(const struct connection *conn, unsigned int feature);
+
 #endif /* _XENSTORED_DOMAIN_H */
tools/xenstored/watch.c +9 -2
index 3f1049911b..5de386370d 100644
@@ -213,11 +213,18 @@ static struct watch *add_watch(struct connection *conn, const char *path,
 int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in)
 {
 	struct watch *watch;
-	const char *vec[2];
+	const char *vec[3];
+	unsigned int n_pars;
 	int depth = -1;
 	bool relative;
 
-	if (get_strings(in, vec, ARRAY_SIZE(vec)) != ARRAY_SIZE(vec))
+	n_pars = get_strings(in, vec, ARRAY_SIZE(vec));
+	if (n_pars == 3 &&
+	    feature_available(conn, XENSTORE_SERVER_FEATURE_WATCHDEPTH)) {
+		depth = atoi(vec[2]);
+		if (depth < 0)
+			return EINVAL;
+	} else if (n_pars != 2)
 		return EINVAL;
 
 	errno = check_watch_path(conn, ctx, &(vec[0]), &relative);
Signature · 1 line
2.53.0
#13 lore raw

PATCH 6/8 tools/xenstored: replace the fire_watches() exact parameter

In reply to Juergen Gross

Today fire_watches() has the boolean "exact" parameter specifying how the matching of the modified node with registered watches is to be handled (only the exact node name is matching or all nodes being in the subtree beneath the watched node).

For the handling of <special-watch>/<domid> watch events 2 additional matching possibility needs to be added, as those events should only be delivered to clients having requested such events, either by watching <special-watch> with the depth=1 parameter, or by directly watching for <special-watch>/<domid>, while the traditional special watch events should NOT delivered to those watchers.

For this purpose rename the "exact" parameter to "match" and make it an enum with four possible values.

---
 tools/xenstored/core.c        | 12 +++++-----
 tools/xenstored/transaction.c | 14 +++++++-----
 tools/xenstored/transaction.h |  4 +++-
 tools/xenstored/watch.c       | 42 +++++++++++++++++++++++++----------
 tools/xenstored/watch.h       |  9 +++++++-
 5 files changed, 55 insertions(+), 26 deletions(-)
tools/xenstored/core.c +6 -6
index 09f1390f14..109e319f8c 100644
@@ -1612,7 +1612,7 @@ static int do_write(const void *ctx, struct connection *conn,
 			return errno;
 	}
 
-	fire_watches(conn, ctx, name, node, false, NULL);
+	fire_watches(conn, ctx, name, node, MATCH_SUBTREE, NULL);
 	send_ack(conn, XS_WRITE);
 
 	return 0;
@@ -1636,7 +1636,7 @@ static int do_mkdir(const void *ctx, struct connection *conn,
 		node = create_node(conn, ctx, name, NULL, 0);
 		if (!node)
 			return errno;
-		fire_watches(conn, ctx, name, node, false, NULL);
+		fire_watches(conn, ctx, name, node, MATCH_SUBTREE, NULL);
 	}
 	send_ack(conn, XS_MKDIR);
 
@@ -1682,7 +1682,7 @@ static int delnode_sub(const void *ctx, struct connection *conn,
 		       struct node *node, void *arg)
 {
 	const char *root = arg;
-	bool watch_exact;
+	enum watch_match watch_match;
 	int ret;
 	const char *db_name;
 
@@ -1702,8 +1702,8 @@ static int delnode_sub(const void *ctx, struct connection *conn,
 	 * This fine as we are single threaded and the next possible read will
 	 * be handled only after the node has been really removed.
 	*/
-	watch_exact = strcmp(root, node->name);
-	fire_watches(conn, ctx, node->name, node, watch_exact, NULL);
+	watch_match = strcmp(root, node->name) ? MATCH_EXACT : MATCH_SUBTREE;
+	fire_watches(conn, ctx, node->name, node, watch_match, NULL);
 
 	return WALK_TREE_RM_CHILDENTRY;
 }
@@ -1857,7 +1857,7 @@ static int do_set_perms(const void *ctx, struct connection *conn,
 	if (write_node(conn, node, NODE_MODIFY, false))
 		return errno;
 
-	fire_watches(conn, ctx, name, node, false, &old_perms);
+	fire_watches(conn, ctx, name, node, MATCH_SUBTREE, &old_perms);
 	send_ack(conn, XS_SET_PERMS);
 
 	return 0;
tools/xenstored/transaction.c +8 -6
index ccf93a1132..7e15aa8a2c 100644
@@ -134,7 +134,7 @@ struct accessed_node
 
 	/* Watch event flags. */
 	bool fire_watch;
-	bool watch_exact;
+	enum watch_match watch_match;
 };
 
 struct transaction
@@ -327,8 +327,10 @@ err:
  * A watch event should be fired for a node modified inside a transaction.
  * Set the corresponding information. A non-exact event is replacing an exact
  * one, but not the other way round.
+ * No special watch handling needed here, so MATCH_DEPTH is no issue.
  */
-void queue_watches(struct connection *conn, const char *name, bool watch_exact)
+void queue_watches(struct connection *conn, const char *name,
+		   enum watch_match watch_match)
 {
 	struct accessed_node *i;
 
@@ -340,9 +342,9 @@ void queue_watches(struct connection *conn, const char *name, bool watch_exact)
 
 	if (!i->fire_watch) {
 		i->fire_watch = true;
-		i->watch_exact = watch_exact;
-	} else if (!watch_exact) {
-		i->watch_exact = false;
+		i->watch_match = watch_match;
+	} else if (watch_match == MATCH_SUBTREE) {
+		i->watch_match = MATCH_SUBTREE;
 	}
 }
 
@@ -419,7 +421,7 @@ static int finalize_transaction(struct connection *conn,
 				db_delete(conn, i->node, NULL);
 		}
 		if (i->fire_watch)
-			fire_watches(conn, trans, i->node, NULL, i->watch_exact,
+			fire_watches(conn, trans, i->node, NULL, i->watch_match,
 				     i->perms.p ? &i->perms : NULL);
 
 		list_del(&i->list);
tools/xenstored/transaction.h +3 -1
index 90435b4fc9..b4f5f757e2 100644
@@ -18,6 +18,7 @@
 #ifndef _XENSTORED_TRANSACTION_H
 #define _XENSTORED_TRANSACTION_H
 #include "core.h"
+#include "watch.h"
 
 enum node_access_type {
     NODE_ACCESS_READ,
@@ -44,7 +45,8 @@ int __must_check access_node(struct connection *conn, struct node *node,
                              enum node_access_type type, const char **db_name);
 
 /* Queue watches for a modified node. */
-void queue_watches(struct connection *conn, const char *name, bool watch_exact);
+void queue_watches(struct connection *conn, const char *name,
+		   enum watch_match watch_match);
 
 /* Prepend the transaction to name if appropriate. */
 const char *transaction_prepend(struct connection *conn, const char *name);
tools/xenstored/watch.c +30 -12

61 lines not on this page · raw message

tools/xenstored/watch.h +8 -1
index afdfdc6b2f..692343389f 100644
@@ -27,8 +27,15 @@ int do_unwatch(const void *ctx, struct connection *conn,
 	       struct buffered_data *in);
 
 /* Fire all watches: !exact means all the children are affected (ie. rm). */
+enum watch_match {
+	MATCH_EXACT,
+	MATCH_SUBTREE,
+	MATCH_DEPTH,	/* watches with depth > 0, MATCH_SUBTREE semantics */
+	MATCH_NODEPTH	/* watches with no depth, MATCH_EXACT semantics */
+};
+
 void fire_watches(struct connection *conn, const void *tmp, const char *name,
-		  const struct node *node, bool exact,
+		  const struct node *node, enum watch_match match,
 		  struct node_perms *perms);
 
 void conn_delete_all_watches(struct connection *conn);
Signature · 1 line
2.53.0
Reviewed-by on v1 6/8
#14 lore raw

Re: [PATCH 6/8] tools/xenstored: replace the fire_watches() exact parameter

Quoted 12 lines · Juergen Gross

Today fire_watches() has the boolean "exact" parameter specifying how the matching of the modified node with registered watches is to be handled (only the exact node name is matching or all nodes being in the subtree beneath the watched node).

For the handling of <special-watch>/<domid> watch events 2 additional matching possibility needs to be added, as those events should only be delivered to clients having requested such events, either by watching <special-watch> with the depth=1 parameter, or by directly watching for <special-watch>/<domid>, while the traditional special watch events should NOT delivered to those watchers.

Quoted 4 lines

For this purpose rename the "exact" parameter to "match" and make it an enum with four possible values.

#15 lore raw

PATCH 7/8 tools/xenstored: expand special watch handling with depth feature

In reply to Juergen Gross

With XENSTORE_SERVER_FEATURE_WATCHDEPTH available, special watch handling needs to be extended:

- when a special watch is set with depth = 1, the domid is added to the watch event

- it is possible to watch for @releaseDomain/<domid>

---
 tools/xenstored/domain.c | 37 ++++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 9 deletions(-)
tools/xenstored/domain.c +28 -9
index 866c0aa908..1fe8abbea9 100644
@@ -613,7 +613,12 @@ static void domain_tree_remove(struct domain *domain)
 	walk_node_tree(domain, NULL, "@introduceDomain", &walkfuncs, domain);
 }
 
-static void fire_special_watches(const char *name)
+#define WATCH_NODOM	1	/* Fire watches without <domid> extension. */
+#define WATCH_DOM	2	/* Fire watches with <domid> extension. */
+#define WATCH_BOTH	(WATCH_NODOM | WATCH_DOM)
+
+static void fire_special_watches(const char *name, unsigned int domid,
+				 unsigned int watchdom_flag)
 {
 	void *ctx = talloc_new(NULL);
 	const struct node *node;
@@ -623,9 +628,19 @@ static void fire_special_watches(const char *name)
 
 	node = read_node_const(NULL, ctx, name);
 
-	if (node)
-		fire_watches(NULL, ctx, name, node, true, NULL);
-	else
+	if (node) {
+		if (watchdom_flag & WATCH_NODOM)
+			fire_watches(NULL, ctx, name, node, MATCH_NODEPTH,
+				     NULL);
+		if (watchdom_flag & WATCH_DOM) {
+			char name_dom[24]; /* max. "@introduceDomain/domid" */
+
+			snprintf(name_dom, sizeof(name_dom),
+				 "%s/%u", name, domid);
+			fire_watches(NULL, ctx, name_dom, node, MATCH_DEPTH,
+				     NULL);
+		}
+	} else
 		log("special node %s not found\n", name);
 
 	talloc_free(ctx);
@@ -650,7 +665,7 @@ static int destroy_domain(void *_domain)
 	if (domain->interface)
 		unmap_interface(domain->domid, domain->interface);
 
-	fire_special_watches("@releaseDomain");
+	fire_special_watches("@releaseDomain", domain->domid, WATCH_BOTH);
 
 	wrl_domain_destroy(domain);
 
@@ -678,10 +693,13 @@ static int do_check_domain(struct domain *domain, bool *notify,
 		if ((state & XENMANAGE_GETDOMSTATE_STATE_SHUTDOWN)
 		    && !domain->shutdown) {
 			domain->shutdown = true;
+			fire_special_watches("@releaseDomain", domain->domid,
+					     WATCH_DOM);
 			*notify = true;
 		}
-		if (!(state & XENMANAGE_GETDOMSTATE_STATE_DEAD))
+		if (!(state & XENMANAGE_GETDOMSTATE_STATE_DEAD)) {
 			return 0;
+		}
 	}
 	if (domain->conn) {
 		/* domain is a talloc child of domain->conn. */
@@ -720,7 +738,7 @@ void check_domains(void)
 		;
 
 	if (notify)
-		fire_special_watches("@releaseDomain");
+		fire_special_watches("@releaseDomain", 0, WATCH_NODOM);
 }
 
 static struct domain *find_domain_struct(unsigned int domid)
@@ -744,7 +762,7 @@ static void do_check_domains(void)
 	}
 
 	if (notify)
-		fire_special_watches("@releaseDomain");
+		fire_special_watches("@releaseDomain", 0, WATCH_NODOM);
 }
 
 /* We scan all domains rather than use the information given here. */
@@ -1097,7 +1115,8 @@ static struct domain *introduce_domain(const void *ctx,
 		talloc_steal(domain->conn, domain);
 
 		if (!is_priv_domain && !restore)
-			fire_special_watches("@introduceDomain");
+			fire_special_watches("@introduceDomain", domid,
+					     WATCH_BOTH);
 	} else {
 		/* Use XS_INTRODUCE for recreating the xenbus event-channel. */
 		if (domain->port)
Signature · 1 line
2.53.0
Reviewed-by on v1 7/8
#16 lore raw

Re: [PATCH 7/8] tools/xenstored: expand special watch handling with depth feature

Quoted 12 lines · Juergen Gross

With XENSTORE_SERVER_FEATURE_WATCHDEPTH available, special watch handling needs to be extended:

- when a special watch is set with depth = 1, the domid is added to the watch event

- it is possible to watch for @releaseDomain/<domid>

tools/xenstored/domain.c +0 -0
@@ -678,10 +693,13 @@ static int do_check_domain(struct domain *domain, bool *notify,
  		if ((state & XENMANAGE_GETDOMSTATE_STATE_SHUTDOWN)

tools/xenstored/domain.c static int do_check_domain(struct domain *domain, bool *notify, · line 702

Quoted 10 lines
tools/xenstored/domain.c +4 -1
  		    && !domain->shutdown) {
  			domain->shutdown = true;
+			fire_special_watches("@releaseDomain", domain->domid,
+					     WATCH_DOM);
  			*notify = true;
  		}
-		if (!(state & XENMANAGE_GETDOMSTATE_STATE_DEAD))
+		if (!(state & XENMANAGE_GETDOMSTATE_STATE_DEAD)) {
  			return 0;
+		}

Stray {} addition?

Quoted 3 lines
tools/xenstored/domain.c +0 -0
  	}
  	if (domain->conn) {
  		/* domain is a talloc child of domain->conn. */
#17 lore raw

Re: [PATCH 7/8] tools/xenstored: expand special watch handling with depth feature

Quoted 11 lines · Jason Andryuk
Quoted 9 lines · Juergen Gross

With XENSTORE_SERVER_FEATURE_WATCHDEPTH available, special watch handling needs to be extended:

- when a special watch is set with depth = 1, the domid is added to the watch event

- it is possible to watch for @releaseDomain/<domid>

Quoted 15 lines

tools/xenstored/domain.c static int do_check_domain(struct domain *domain, bool · line 703

Quoted 13 lines
tools/xenstored/domain.c +4 -1
@@ -678,10 +693,13 @@ static int do_check_domain(struct domain *domain, bool 
 *notify,
           if ((state & XENMANAGE_GETDOMSTATE_STATE_SHUTDOWN)
               && !domain->shutdown) {
               domain->shutdown = true;
+            fire_special_watches("@releaseDomain", domain->domid,
+                         WATCH_DOM);
               *notify = true;
           }
-        if (!(state & XENMANAGE_GETDOMSTATE_STATE_DEAD))
+        if (!(state & XENMANAGE_GETDOMSTATE_STATE_DEAD)) {
               return 0;
+        }

Stray  {} addition?

Yes. Another artifact from the evolving series.

Quoted 2 lines

Thanks,

Juergen

#18 lore raw

PATCH 8/8 tools/xenstored: make XENSTORE_SERVER_FEATURE_WATCHDEPTH available

In reply to Juergen Gross

Add XENSTORE_SERVER_FEATURE_WATCHDEPTH to the available features now that all needed support has been added.

---
 tools/xenstored/domain.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/xenstored/domain.c +2 -1
index 1fe8abbea9..d02ac247c5 100644
@@ -43,7 +43,8 @@
 #include <mini-os/xenbus.h>
 #endif
 
-#define XENSTORE_FEATURES	XENSTORE_SERVER_FEATURE_ERROR
+#define XENSTORE_FEATURES	(XENSTORE_SERVER_FEATURE_ERROR |	\
+				 XENSTORE_SERVER_FEATURE_WATCHDEPTH)
 
 static xenmanage_handle *xm_handle;
 xengnttab_handle **xgt_handle;
Signature · 1 line
2.53.0
#20 lore raw

[PATCH 9/8] tools/xs-clients: support depth with xenstore-watch

In reply to Juergen Gross

Add a "-d <depth>" parameter to xenstore-watch, allowing to specify the depth parameter of the XS_WATCH command.

Using "-d" without support of depth by xenstored will refuse to set the watch(es).

---
 tools/xs-clients/xenstore_client.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
tools/xs-clients/xenstore_client.c +15 -4
index 9a25704b91..4a14cb334f 100644
@@ -445,7 +445,7 @@ do_watch(struct xs_handle *xsh, int max_events)
 static int
 perform(enum mode mode, int optind, int argc, char **argv, struct xs_handle *xsh,
         xs_transaction_t xth, int prefix, int tidy, int upto, int recurse, int nr_watches,
-        int raw)
+        int raw, int depth)
 {
     switch (mode) {
     case MODE_ls:
@@ -627,8 +627,11 @@ perform(enum mode mode, int optind, int argc, char **argv, struct xs_handle *xsh
             for (; argv[optind]; optind++) {
                 const char *w = argv[optind];
 
-                if (!xs_watch(xsh, w, w))
+                if (depth < 0 && !xs_watch(xsh, w, w))
                     errx(1, "Unable to add watch on %s\n", w);
+                if (depth >= 0 && !xs_watch_depth(xsh, w, w, depth))
+                    errx(1, "Unable to add watch on %s with depth %d\n", w,
+                         depth);
             }
             do_watch(xsh, nr_watches);
         }
@@ -676,6 +679,7 @@ main(int argc, char **argv)
     int nr_watches = -1;
     int transaction;
     int raw = 0;
+    int depth = -1;
     struct winsize ws;
     enum mode mode;
 
@@ -709,10 +713,11 @@ main(int argc, char **argv)
 	    {"recurse", 0, 0, 'r'}, /* MODE_chmod */
 	    {"number",  1, 0, 'n'}, /* MODE_watch */
 	    {"raw",     0, 0, 'R'}, /* MODE_read || MODE_write */
+	    {"depth",   1, 0, 'd'}, /* MODE_watch */
 	    {0, 0, 0, 0}
 	};
 
-	c = getopt_long(argc - switch_argv, argv + switch_argv, "hfspturn:R",
+	c = getopt_long(argc - switch_argv, argv + switch_argv, "hfspturn:Rd:",
 			long_options, &index);
 	if (c == -1)
 	    break;
@@ -766,6 +771,12 @@ main(int argc, char **argv)
 	    else
 		usage(1, mode, switch_argv, argv[0]);
 	    break;
+	case 'd':
+	    if ( mode == MODE_watch )
+		depth = atoi(optarg);
+	    else
+		usage(1, mode, switch_argv, argv[0]);
+	    break;
 	}
     }
 
@@ -819,7 +830,7 @@ again:
 	    errx(1, "couldn't start transaction");
     }
 
-    ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse, nr_watches, raw);
+    ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse, nr_watches, raw, depth);
 
     if (transaction && !xs_transaction_end(xsh, xth, ret)) {
 	if (ret == 0 && errno == EAGAIN) {
Signature · 1 line
2.53.0
Reviewed-by
#21 lore raw

Re: [PATCH 9/8] tools/xs-clients: support depth with xenstore-watch

Quoted 7 lines · Juergen Gross

Add a "-d <depth>" parameter to xenstore-watch, allowing to specify the depth parameter of the XS_WATCH command.

Using "-d" without support of depth by xenstored will refuse to set the watch(es).

For bonus points, you can add it to the man page :)

Regards,
Jason

#22 lore raw

Re: [PATCH 9/8] tools/xs-clients: support depth with xenstore-watch

Quoted 4 lines · Jason Andryuk
Quoted 3 lines · Juergen Gross

Add a "-d <depth>" parameter to xenstore-watch, allowing to specify the depth parameter of the XS_WATCH command.

Quoted 8 lines
Quoted 4 lines

Using "-d" without support of depth by xenstored will refuse to set the watch(es).

For bonus points, you can add it to the man page :)

I'll do that.

Thanks,

Juergen

#23 lore raw
In reply to Juergen Gross

Regarding the upcoming feature freeze, would it be possible to get some feedback on this series?

I would really appreciate if at least the first 3 patches could make it into 4.22, as this will avoid the need to bump the libxenstore minor version in 4.23 again.

Juergen

Quoted 54 lines · Juergen Gross

In order to reduce excessive number of watch events the watch depth feature has been defined, allowing the user to limit the number of node levels below the watched node to cause events.

This series is implementing this feature for xenstored (including the PV and PVH stubdom variants).

Possible users are:

- xl/libxl: use domid information for the special @introduceDomain and @releaseDomain watches, which will be available when using depth = 1

- xenconsoled, like xl/libxl

- any PV-device backends watching directories for added devices (no watch events when any device specific node is modified)

- any PV-device frontend and backend watching the "state" node of the other side, in case the other side plays dirty games by building large sub-trees beneath the "state" node

This series has been tested with a modified xenstore-watch client. The modification will be sent as soon as the pending series of the xs-clients has been applied.

Note that the watch depth series depends on the still pending series "tools: add support for per-domain xenstore quota".

Juergen Gross (8):
tools/xenstored: allow domU to get own features
docs: clarify Xenstore watch depth feature tools/libs/store: add support to use watches with a depth parameter tools/xenstored: add depth information to watches tools/xenstored: add depth handling to XS_WATCH tools/xenstored: replace the fire_watches() exact parameter tools/xenstored: expand special watch handling with depth feature tools/xenstored: make XENSTORE_SERVER_FEATURE_WATCHDEPTH available

  docs/man/xl.cfg.5.pod.in         |   6 ++
  docs/misc/xenstore.txt           |  14 ++-
  tools/include/xenstore.h         |  16 +++
  tools/libs/store/libxenstore.map |   2 +
  tools/libs/store/xs.c            | 116 ++++++++++++++++++----
  tools/xenstored/core.c           |  15 ++-
  tools/xenstored/domain.c         |  64 +++++++++---
  tools/xenstored/domain.h         |   2 +
  tools/xenstored/lu.c             |   3 +
  tools/xenstored/transaction.c    |  14 +--
  tools/xenstored/transaction.h    |   4 +-
  tools/xenstored/watch.c          | 161 ++++++++++++++++++++++---------
  tools/xenstored/watch.h          |  10 +-
  xen/include/public/io/xs_wire.h  |   2 +
  14 files changed, 328 insertions(+), 101 deletions(-)