Commit ea732dff authored by Paul Durrant's avatar Paul Durrant Committed by David S. Miller

xen-netback: Handle backend state transitions in a more robust way

When the frontend state changes netback now specifies its desired state to
a new function, set_backend_state(), which transitions through any
necessary intermediate states.
This fixes an issue observed with some old Windows frontend drivers where
they failed to transition through the Closing state and netback would not
behave correctly.
Signed-off-by: default avatarPaul Durrant <paul.durrant@citrix.com>
Cc: Ian Campbell <ian.campbell@citrix.com>
Cc: Wei Liu <wei.liu2@citrix.com>
Cc: David Vrabel <david.vrabel@citrix.com>
Acked-by: default avatarIan Campbell <ian.campbell@citrix.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c9d55d5b
...@@ -24,6 +24,12 @@ ...@@ -24,6 +24,12 @@
struct backend_info { struct backend_info {
struct xenbus_device *dev; struct xenbus_device *dev;
struct xenvif *vif; struct xenvif *vif;
/* This is the state that will be reflected in xenstore when any
* active hotplug script completes.
*/
enum xenbus_state state;
enum xenbus_state frontend_state; enum xenbus_state frontend_state;
struct xenbus_watch hotplug_status_watch; struct xenbus_watch hotplug_status_watch;
u8 have_hotplug_status_watch:1; u8 have_hotplug_status_watch:1;
...@@ -136,6 +142,8 @@ static int netback_probe(struct xenbus_device *dev, ...@@ -136,6 +142,8 @@ static int netback_probe(struct xenbus_device *dev,
if (err) if (err)
goto fail; goto fail;
be->state = XenbusStateInitWait;
/* This kicks hotplug scripts, so do it immediately. */ /* This kicks hotplug scripts, so do it immediately. */
backend_create_xenvif(be); backend_create_xenvif(be);
...@@ -208,24 +216,113 @@ static void backend_create_xenvif(struct backend_info *be) ...@@ -208,24 +216,113 @@ static void backend_create_xenvif(struct backend_info *be)
kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE); kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
} }
static void backend_disconnect(struct backend_info *be)
static void disconnect_backend(struct xenbus_device *dev)
{ {
struct backend_info *be = dev_get_drvdata(&dev->dev);
if (be->vif) if (be->vif)
xenvif_disconnect(be->vif); xenvif_disconnect(be->vif);
} }
static void destroy_backend(struct xenbus_device *dev) static void backend_connect(struct backend_info *be)
{ {
struct backend_info *be = dev_get_drvdata(&dev->dev); if (be->vif)
connect(be);
}
if (be->vif) { static inline void backend_switch_state(struct backend_info *be,
kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE); enum xenbus_state state)
xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status"); {
xenvif_free(be->vif); struct xenbus_device *dev = be->dev;
be->vif = NULL;
pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
be->state = state;
/* If we are waiting for a hotplug script then defer the
* actual xenbus state change.
*/
if (!be->have_hotplug_status_watch)
xenbus_switch_state(dev, state);
}
/* Handle backend state transitions:
*
* The backend state starts in InitWait and the following transitions are
* allowed.
*
* InitWait -> Connected
*
* ^ \ |
* | \ |
* | \ |
* | \ |
* | \ |
* | \ |
* | V V
*
* Closed <-> Closing
*
* The state argument specifies the eventual state of the backend and the
* function transitions to that state via the shortest path.
*/
static void set_backend_state(struct backend_info *be,
enum xenbus_state state)
{
while (be->state != state) {
switch (be->state) {
case XenbusStateClosed:
switch (state) {
case XenbusStateInitWait:
case XenbusStateConnected:
pr_info("%s: prepare for reconnect\n",
be->dev->nodename);
backend_switch_state(be, XenbusStateInitWait);
break;
case XenbusStateClosing:
backend_switch_state(be, XenbusStateClosing);
break;
default:
BUG();
}
break;
case XenbusStateInitWait:
switch (state) {
case XenbusStateConnected:
backend_connect(be);
backend_switch_state(be, XenbusStateConnected);
break;
case XenbusStateClosing:
case XenbusStateClosed:
backend_switch_state(be, XenbusStateClosing);
break;
default:
BUG();
}
break;
case XenbusStateConnected:
switch (state) {
case XenbusStateInitWait:
case XenbusStateClosing:
case XenbusStateClosed:
backend_disconnect(be);
backend_switch_state(be, XenbusStateClosing);
break;
default:
BUG();
}
break;
case XenbusStateClosing:
switch (state) {
case XenbusStateInitWait:
case XenbusStateConnected:
case XenbusStateClosed:
backend_switch_state(be, XenbusStateClosed);
break;
default:
BUG();
}
break;
default:
BUG();
}
} }
} }
...@@ -237,40 +334,33 @@ static void frontend_changed(struct xenbus_device *dev, ...@@ -237,40 +334,33 @@ static void frontend_changed(struct xenbus_device *dev,
{ {
struct backend_info *be = dev_get_drvdata(&dev->dev); struct backend_info *be = dev_get_drvdata(&dev->dev);
pr_debug("frontend state %s\n", xenbus_strstate(frontend_state)); pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
be->frontend_state = frontend_state; be->frontend_state = frontend_state;
switch (frontend_state) { switch (frontend_state) {
case XenbusStateInitialising: case XenbusStateInitialising:
if (dev->state == XenbusStateClosed) { set_backend_state(be, XenbusStateInitWait);
pr_info("%s: prepare for reconnect\n", dev->nodename);
xenbus_switch_state(dev, XenbusStateInitWait);
}
break; break;
case XenbusStateInitialised: case XenbusStateInitialised:
break; break;
case XenbusStateConnected: case XenbusStateConnected:
if (dev->state == XenbusStateConnected) set_backend_state(be, XenbusStateConnected);
break;
if (be->vif)
connect(be);
break; break;
case XenbusStateClosing: case XenbusStateClosing:
disconnect_backend(dev); set_backend_state(be, XenbusStateClosing);
xenbus_switch_state(dev, XenbusStateClosing);
break; break;
case XenbusStateClosed: case XenbusStateClosed:
xenbus_switch_state(dev, XenbusStateClosed); set_backend_state(be, XenbusStateClosed);
if (xenbus_dev_is_online(dev)) if (xenbus_dev_is_online(dev))
break; break;
destroy_backend(dev);
/* fall through if not online */ /* fall through if not online */
case XenbusStateUnknown: case XenbusStateUnknown:
set_backend_state(be, XenbusStateClosed);
device_unregister(&dev->dev); device_unregister(&dev->dev);
break; break;
...@@ -363,7 +453,9 @@ static void hotplug_status_changed(struct xenbus_watch *watch, ...@@ -363,7 +453,9 @@ static void hotplug_status_changed(struct xenbus_watch *watch,
if (IS_ERR(str)) if (IS_ERR(str))
return; return;
if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) { if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
xenbus_switch_state(be->dev, XenbusStateConnected); /* Complete any pending state change */
xenbus_switch_state(be->dev, be->state);
/* Not interested in this watch anymore. */ /* Not interested in this watch anymore. */
unregister_hotplug_status_watch(be); unregister_hotplug_status_watch(be);
} }
...@@ -393,12 +485,8 @@ static void connect(struct backend_info *be) ...@@ -393,12 +485,8 @@ static void connect(struct backend_info *be)
err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch, err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
hotplug_status_changed, hotplug_status_changed,
"%s/%s", dev->nodename, "hotplug-status"); "%s/%s", dev->nodename, "hotplug-status");
if (err) { if (!err)
/* Switch now, since we can't do a watch. */
xenbus_switch_state(dev, XenbusStateConnected);
} else {
be->have_hotplug_status_watch = 1; be->have_hotplug_status_watch = 1;
}
netif_wake_queue(be->vif->dev); netif_wake_queue(be->vif->dev);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment