Skip to contentSkip to conversation

hotplug/unplug support and bug fixes for network accelerators

PATCH New Kieran Mansley 1 participant 1 message Dec 18, 2007 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hotplug/unplug support and bug fixes for network accelerators

Adding support to the network accelerators for hot plugging and unplugging the NIC revealed some new and exciting ways in which the network acceleration plugins could be loaded and unloaded. This patch makes sure that this all works properly. While this is motivated by what is arguably a new feature (a xenstore watch in domU to change the accelerator when the configuration changes) most of the changes are in fact bug fixes that this (and the rest of our 3.2.0-rc1 testing) turned up, and so I would recommend the patch be applied to 3.2.0

In detail, the changes are as follows:

netback:
- module_get/put not called consistently on one code path, now fixed
- strcmp used where !strcmp was intended on disconnect
- backend accelerator removal while active interfaces present now
supported
- code cleanup (better code reuse, more consistent function names,
comments, etc)
- bump protocol version to reflect these changes

netfront:
- add watch on accel configuration in xenstore, and respond to changes
- correct handling of errors from accelerator probe (could in theory
lead to BUG() before when using accelerators)
- simplify plugin API (remove need for netfront_accelerator_ready(),
and netfront_load_accelerator() now internal only) and less "accelerator" code in netfront.c
- correct locking for removal of frontend accelerator (could lead to
spin lock deadlock before when using accelerators)
- bump protocol version to reflect these changes

Allow network accelerators to deal with hot-plug/unplug on physical devices Add watch for acceleration configuration in frontend

drivers/xen/netback/accel.c +64 -52
diff -r fc406f9e9a0a drivers/xen/netback/accel.c
@@ -76,12 +76,27 @@ static int match_accelerator(struct xenb
 	}
 }
 
-/*
- * Notify all suitable backends that a new accelerator is available
- * and connected.  This will also notify the accelerator plugin module
+
+static void do_probe(struct backend_info *be, 
+		     struct netback_accelerator *accelerator,
+		     struct xenbus_device *xendev) 
+{
+	be->accelerator = accelerator;
+	atomic_inc(&be->accelerator->use_count);
+	if (be->accelerator->hooks->probe(xendev) != 0) {
+		atomic_dec(&be->accelerator->use_count);
+		module_put(be->accelerator->hooks->owner);
+		be->accelerator = NULL;
+	}
+}
+
+
+/*
+ * Notify suitable backends that a new accelerator is available and
+ * connected.  This will also notify the accelerator plugin module
  * that it is being used for a device through the probe hook.
  */
-static int netback_accelerator_tell_backend(struct device *dev, void *arg)
+static int netback_accelerator_probe_backend(struct device *dev, void *arg)
 {
 	struct netback_accelerator *accelerator = 
 		(struct netback_accelerator *)arg;
@@ -90,14 +105,37 @@ static int netback_accelerator_tell_back
 	if (!strcmp("vif", xendev->devicetype)) {
 		struct backend_info *be = xendev->dev.driver_data;
 
-		if (match_accelerator(xendev, be, accelerator)) {
-			be->accelerator = accelerator;
-			atomic_inc(&be->accelerator->use_count);
-			be->accelerator->hooks->probe(xendev);
-		}
-	}
-	return 0;
-}
+		if (match_accelerator(xendev, be, accelerator) &&
+		    try_module_get(accelerator->hooks->owner)) {
+			do_probe(be, accelerator, xendev);
+		}
+	}
+	return 0;
+}
+
+
+/*
+ * Notify suitable backends that an accelerator is unavailable.
+ */
+static int netback_accelerator_remove_backend(struct device *dev, void *arg)
+{
+	struct xenbus_device *xendev = to_xenbus_device(dev);
+	struct netback_accelerator *accelerator = 
+		(struct netback_accelerator *)arg;
+	
+	if (!strcmp("vif", xendev->devicetype)) {
+		struct backend_info *be = xendev->dev.driver_data;
+
+		if (be->accelerator == accelerator) {
+			be->accelerator->hooks->remove(xendev);
+			atomic_dec(&be->accelerator->use_count);
+			module_put(be->accelerator->hooks->owner);
+			be->accelerator = NULL;
+		}
+	}
+	return 0;
+}
+
 
 
 /*
@@ -133,7 +171,6 @@ int netback_connect_accelerator(unsigned
 		return -ENOMEM;
 	}
 
-
 	new_accelerator->id = id;
 	
 	eth_name_len = strlen(eth_name)+1;
@@ -152,44 +189,22 @@ int netback_connect_accelerator(unsigned
 	
 	mutex_lock(&accelerators_mutex);
 	list_add(&new_accelerator->link, &accelerators_list);
-	mutex_unlock(&accelerators_mutex);
 	
 	/* tell existing backends about new plugin */
 	xenbus_for_each_backend(new_accelerator, 
-				netback_accelerator_tell_backend);
+				netback_accelerator_probe_backend);
+
+	mutex_unlock(&accelerators_mutex);
 
 	return 0;
 
 }
 EXPORT_SYMBOL_GPL(netback_connect_accelerator);
-
-
-/* 
- * Remove the link from backend state to a particular accelerator
- */ 
-static int netback_accelerator_cleanup_backend(struct device *dev, void *arg)
-{
-	struct netback_accelerator *accelerator = 
-		(struct netback_accelerator *)arg;
-	struct xenbus_device *xendev = to_xenbus_device(dev);
-
-	if (!strcmp("vif", xendev->devicetype)) {
-		struct backend_info *be = xendev->dev.driver_data;
-		if (be->accelerator == accelerator)
-			be->accelerator = NULL;
-	}
-	return 0;
-}
 
 
 /* 
  * Disconnect an accelerator plugin module that has previously been
  * connected.
- *
- * This should only be allowed when there are no remaining users -
- * i.e. it is not necessary to go through and clear all the hooks, as
- * they should have already been removed.  This is enforced by taking
- * a module reference to the plugin while the interfaces are in use
  */
 void netback_disconnect_accelerator(int id, const char *eth_name)
 {
@@ -197,17 +212,14 @@ void netback_disconnect_accelerator(int 
 
 	mutex_lock(&accelerators_mutex);
 	list_for_each_entry_safe(accelerator, next, &accelerators_list, link) {
-		if (strcmp(eth_name, accelerator->eth_name)) {
+		if (!strcmp(eth_name, accelerator->eth_name)) {
+			xenbus_for_each_backend
+				(accelerator, netback_accelerator_remove_backend);
 			BUG_ON(atomic_read(&accelerator->use_count) != 0);
-			list_del(&accelerator->link);
-			mutex_unlock(&accelerators_mutex);
-
-			xenbus_for_each_backend(accelerator, 
-						netback_accelerator_cleanup_backend);
-				
+			list_del(&accelerator->link);				
 			kfree(accelerator->eth_name);
 			kfree(accelerator);
-			return;
+			break;
 		}
 	}
 	mutex_unlock(&accelerators_mutex);
@@ -228,9 +240,7 @@ void netback_probe_accelerators(struct b
 	list_for_each_entry(accelerator, &accelerators_list, link) { 
 		if (match_accelerator(dev, be, accelerator) &&
 		    try_module_get(accelerator->hooks->owner)) {
-			be->accelerator = accelerator;
-			atomic_inc(&be->accelerator->use_count);
-			be->accelerator->hooks->probe(dev);
+			do_probe(be, accelerator, dev);
 			break;
 		}
 	}
@@ -241,13 +251,15 @@ void netback_remove_accelerators(struct 
 void netback_remove_accelerators(struct backend_info *be,
 				 struct xenbus_device *dev)
 {
+	mutex_lock(&accelerators_mutex);
 	/* Notify the accelerator (if any) of this device's removal */
-	if (be->accelerator) {
+	if (be->accelerator != NULL) {
 		be->accelerator->hooks->remove(dev);
 		atomic_dec(&be->accelerator->use_count);
 		module_put(be->accelerator->hooks->owner);
-	}
-	be->accelerator = NULL;
+		be->accelerator = NULL;
+	}
+	mutex_unlock(&accelerators_mutex);
 }
 
 
drivers/xen/netback/common.h +1 -1
diff -r fc406f9e9a0a drivers/xen/netback/common.h
@@ -150,7 +150,7 @@ struct backend_info {
 	struct netback_accelerator *accelerator;
 };
 
-#define NETBACK_ACCEL_VERSION 0x00010000
+#define NETBACK_ACCEL_VERSION 0x00010001
 
 /* 
  * Connect an accelerator plugin module to netback.  Returns zero on
drivers/xen/netfront/accel.c +191 -99

427 lines not on this page · raw message

drivers/xen/netfront/netfront.c +6 -14
diff -r fc406f9e9a0a drivers/xen/netfront/netfront.c
@@ -378,6 +378,10 @@ static int talk_to_backend(struct xenbus
 	if (err)
 		goto out;
 
+	/* This will load an accelerator if one is configured when the
+	 * watch fires */
+	netfront_accelerator_add_watch(info);
+
 again:
 	err = xenbus_transaction_start(&xbt);
 	if (err) {
@@ -452,6 +456,7 @@ again:
 	xenbus_transaction_end(xbt, 1);
 	xenbus_dev_fatal(dev, err, "%s", message);
  destroy_ring:
+	netfront_accelerator_call_remove(info, dev);
 	netif_disconnect_backend(info);
  out:
 	return err;
@@ -1746,9 +1751,7 @@ static int network_connect(struct net_de
 	struct sk_buff *skb;
 	grant_ref_t ref;
 	netif_rx_request_t *req;
-	unsigned int feature_rx_copy, feature_rx_flip, feature_accel;
-	char *accel_frontend;
-	int accel_len;
+	unsigned int feature_rx_copy, feature_rx_flip;
 
 	err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
 			   "feature-rx-copy", "%u", &feature_rx_copy);
@@ -1759,12 +1762,6 @@ static int network_connect(struct net_de
 	if (err != 1)
 		feature_rx_flip = 1;
 
-	feature_accel = 1;
-	accel_frontend = xenbus_read(XBT_NIL, np->xbdev->otherend, 
-				     "accel-frontend", &accel_len);
-	if (IS_ERR(accel_frontend)) 
-		feature_accel = 0;
-
 	/*
 	 * Copy packets on receive path if:
 	 *  (a) This was requested by user, and the backend supports it; or
@@ -1776,11 +1773,6 @@ static int network_connect(struct net_de
 	err = talk_to_backend(np->xbdev, np);
 	if (err)
 		return err;
-
-	if (feature_accel) {
-		netfront_load_accelerator(np, np->xbdev, accel_frontend);
-		kfree(accel_frontend);
-	}
 
 	xennet_set_features(dev);
 
drivers/xen/netfront/netfront.h +9 -17
diff -r fc406f9e9a0a drivers/xen/netfront/netfront.h
@@ -95,7 +95,7 @@ struct netfront_accel_hooks {
 
 /* Version of API/protocol for communication between netfront and
    acceleration plugin supported */
-#define NETFRONT_ACCEL_VERSION 0x00010002
+#define NETFRONT_ACCEL_VERSION 0x00010003
 
 /* 
  * Per-netfront device state for the accelerator.  This is used to
@@ -108,6 +108,13 @@ struct netfront_accel_vif_state {
 	struct xenbus_device *dev;
 	struct netfront_info *np;
 	struct netfront_accel_hooks *hooks;
+
+	/* Watch on the accelerator configuration value */
+	struct xenbus_watch accel_watch;
+	/* Work item to process change in accelerator */
+	struct work_struct accel_work;
+	/* The string from xenbus last time accel_watch fired */
+	char *accel_frontend;
 }; 
 
 /* 
@@ -210,18 +217,6 @@ extern int netfront_accelerator_loaded(i
 				       struct netfront_accel_hooks *hooks);
 
 /* 
- * Called when an accelerator plugin is ready to accelerate a device *
- * that has been passed to it from netfront using the "new_device"
- * hook.
- *
- * frontend: the string describing the accelerator. Must match the
- * one passed to netfront_accelerator_loaded()
- * dev: the xenbus device the plugin was asked to accelerate
- */
-extern void netfront_accelerator_ready(const char *frontend,
-				       struct xenbus_device *dev);
-
-/* 
  * Called by an accelerator plugin module when it is about to unload.
  *
  * frontend: the string describing the accelerator.  Must match the
@@ -235,7 +230,6 @@ extern void netfront_accelerator_stop(co
  * wake, false if still busy 
  */
 extern int netfront_check_queue_ready(struct net_device *net_dev);
-
 
 
 /* Internal-to-netfront Functions */
@@ -267,9 +261,7 @@ int netfront_accelerator_call_get_stats(
 int netfront_accelerator_call_get_stats(struct netfront_info *np,
 					struct net_device *dev);
 extern
-int netfront_load_accelerator(struct netfront_info *np, 
-			      struct xenbus_device *dev, 
-			      const char *frontend);
+void netfront_accelerator_add_watch(struct netfront_info *np);
 
 extern
 void netif_init_accel(void);

Allow network accelerators to deal with hot-plug/unplug on physical devices Add watch for acceleration configuration in frontend

drivers/xen/netback/accel.c +64 -52

180 lines not on this page · raw message

drivers/xen/netback/common.h +1 -1
diff -r fc406f9e9a0a drivers/xen/netback/common.h
@@ -150,7 +150,7 @@ struct backend_info {
 	struct netback_accelerator *accelerator;
 };
 
-#define NETBACK_ACCEL_VERSION 0x00010000
+#define NETBACK_ACCEL_VERSION 0x00010001
 
 /* 
  * Connect an accelerator plugin module to netback.  Returns zero on
drivers/xen/netfront/accel.c +191 -99

427 lines not on this page · raw message

drivers/xen/netfront/netfront.c +6 -14
diff -r fc406f9e9a0a drivers/xen/netfront/netfront.c
@@ -378,6 +378,10 @@ static int talk_to_backend(struct xenbus
 	if (err)
 		goto out;
 
+	/* This will load an accelerator if one is configured when the
+	 * watch fires */
+	netfront_accelerator_add_watch(info);
+
 again:
 	err = xenbus_transaction_start(&xbt);
 	if (err) {
@@ -452,6 +456,7 @@ again:
 	xenbus_transaction_end(xbt, 1);
 	xenbus_dev_fatal(dev, err, "%s", message);
  destroy_ring:
+	netfront_accelerator_call_remove(info, dev);
 	netif_disconnect_backend(info);
  out:
 	return err;
@@ -1746,9 +1751,7 @@ static int network_connect(struct net_de
 	struct sk_buff *skb;
 	grant_ref_t ref;
 	netif_rx_request_t *req;
-	unsigned int feature_rx_copy, feature_rx_flip, feature_accel;
-	char *accel_frontend;
-	int accel_len;
+	unsigned int feature_rx_copy, feature_rx_flip;
 
 	err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
 			   "feature-rx-copy", "%u", &feature_rx_copy);
@@ -1759,12 +1762,6 @@ static int network_connect(struct net_de
 	if (err != 1)
 		feature_rx_flip = 1;
 
-	feature_accel = 1;
-	accel_frontend = xenbus_read(XBT_NIL, np->xbdev->otherend, 
-				     "accel-frontend", &accel_len);
-	if (IS_ERR(accel_frontend)) 
-		feature_accel = 0;
-
 	/*
 	 * Copy packets on receive path if:
 	 *  (a) This was requested by user, and the backend supports it; or
@@ -1776,11 +1773,6 @@ static int network_connect(struct net_de
 	err = talk_to_backend(np->xbdev, np);
 	if (err)
 		return err;
-
-	if (feature_accel) {
-		netfront_load_accelerator(np, np->xbdev, accel_frontend);
-		kfree(accel_frontend);
-	}
 
 	xennet_set_features(dev);
 
drivers/xen/netfront/netfront.h +9 -17
diff -r fc406f9e9a0a drivers/xen/netfront/netfront.h
@@ -95,7 +95,7 @@ struct netfront_accel_hooks {
 
 /* Version of API/protocol for communication between netfront and
    acceleration plugin supported */
-#define NETFRONT_ACCEL_VERSION 0x00010002
+#define NETFRONT_ACCEL_VERSION 0x00010003
 
 /* 
  * Per-netfront device state for the accelerator.  This is used to
@@ -108,6 +108,13 @@ struct netfront_accel_vif_state {
 	struct xenbus_device *dev;
 	struct netfront_info *np;
 	struct netfront_accel_hooks *hooks;
+
+	/* Watch on the accelerator configuration value */
+	struct xenbus_watch accel_watch;
+	/* Work item to process change in accelerator */
+	struct work_struct accel_work;
+	/* The string from xenbus last time accel_watch fired */
+	char *accel_frontend;
 }; 
 
 /* 
@@ -210,18 +217,6 @@ extern int netfront_accelerator_loaded(i
 				       struct netfront_accel_hooks *hooks);
 
 /* 
- * Called when an accelerator plugin is ready to accelerate a device *
- * that has been passed to it from netfront using the "new_device"
- * hook.
- *
- * frontend: the string describing the accelerator. Must match the
- * one passed to netfront_accelerator_loaded()
- * dev: the xenbus device the plugin was asked to accelerate
- */
-extern void netfront_accelerator_ready(const char *frontend,
-				       struct xenbus_device *dev);
-
-/* 
  * Called by an accelerator plugin module when it is about to unload.
  *
  * frontend: the string describing the accelerator.  Must match the
@@ -235,7 +230,6 @@ extern void netfront_accelerator_stop(co
  * wake, false if still busy 
  */
 extern int netfront_check_queue_ready(struct net_device *net_dev);
-
 
 
 /* Internal-to-netfront Functions */
@@ -267,9 +261,7 @@ int netfront_accelerator_call_get_stats(
 int netfront_accelerator_call_get_stats(struct netfront_info *np,
 					struct net_device *dev);
 extern
-int netfront_load_accelerator(struct netfront_info *np, 
-			      struct xenbus_device *dev, 
-			      const char *frontend);
+void netfront_accelerator_add_watch(struct netfront_info *np);
 
 extern
 void netif_init_accel(void);

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