aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/xen-netback/xenbus.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/xen-netback/xenbus.c')
-rw-r--r--drivers/net/xen-netback/xenbus.c152
1 files changed, 122 insertions, 30 deletions
diff --git a/drivers/net/xen-netback/xenbus.c b/drivers/net/xen-netback/xenbus.c
index a53782ef1540..1b08d8798372 100644
--- a/drivers/net/xen-netback/xenbus.c
+++ b/drivers/net/xen-netback/xenbus.c
@@ -24,6 +24,12 @@
24struct backend_info { 24struct backend_info {
25 struct xenbus_device *dev; 25 struct xenbus_device *dev;
26 struct xenvif *vif; 26 struct xenvif *vif;
27
28 /* This is the state that will be reflected in xenstore when any
29 * active hotplug script completes.
30 */
31 enum xenbus_state state;
32
27 enum xenbus_state frontend_state; 33 enum xenbus_state frontend_state;
28 struct xenbus_watch hotplug_status_watch; 34 struct xenbus_watch hotplug_status_watch;
29 u8 have_hotplug_status_watch:1; 35 u8 have_hotplug_status_watch:1;
@@ -33,11 +39,15 @@ static int connect_rings(struct backend_info *);
33static void connect(struct backend_info *); 39static void connect(struct backend_info *);
34static void backend_create_xenvif(struct backend_info *be); 40static void backend_create_xenvif(struct backend_info *be);
35static void unregister_hotplug_status_watch(struct backend_info *be); 41static void unregister_hotplug_status_watch(struct backend_info *be);
42static void set_backend_state(struct backend_info *be,
43 enum xenbus_state state);
36 44
37static int netback_remove(struct xenbus_device *dev) 45static int netback_remove(struct xenbus_device *dev)
38{ 46{
39 struct backend_info *be = dev_get_drvdata(&dev->dev); 47 struct backend_info *be = dev_get_drvdata(&dev->dev);
40 48
49 set_backend_state(be, XenbusStateClosed);
50
41 unregister_hotplug_status_watch(be); 51 unregister_hotplug_status_watch(be);
42 if (be->vif) { 52 if (be->vif) {
43 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE); 53 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
@@ -136,6 +146,8 @@ static int netback_probe(struct xenbus_device *dev,
136 if (err) 146 if (err)
137 goto fail; 147 goto fail;
138 148
149 be->state = XenbusStateInitWait;
150
139 /* This kicks hotplug scripts, so do it immediately. */ 151 /* This kicks hotplug scripts, so do it immediately. */
140 backend_create_xenvif(be); 152 backend_create_xenvif(be);
141 153
@@ -208,24 +220,113 @@ static void backend_create_xenvif(struct backend_info *be)
208 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE); 220 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
209} 221}
210 222
211 223static void backend_disconnect(struct backend_info *be)
212static void disconnect_backend(struct xenbus_device *dev)
213{ 224{
214 struct backend_info *be = dev_get_drvdata(&dev->dev);
215
216 if (be->vif) 225 if (be->vif)
217 xenvif_disconnect(be->vif); 226 xenvif_disconnect(be->vif);
218} 227}
219 228
220static void destroy_backend(struct xenbus_device *dev) 229static void backend_connect(struct backend_info *be)
221{ 230{
222 struct backend_info *be = dev_get_drvdata(&dev->dev); 231 if (be->vif)
232 connect(be);
233}
223 234
224 if (be->vif) { 235static inline void backend_switch_state(struct backend_info *be,
225 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE); 236 enum xenbus_state state)
226 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status"); 237{
227 xenvif_free(be->vif); 238 struct xenbus_device *dev = be->dev;
228 be->vif = NULL; 239
240 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
241 be->state = state;
242
243 /* If we are waiting for a hotplug script then defer the
244 * actual xenbus state change.
245 */
246 if (!be->have_hotplug_status_watch)
247 xenbus_switch_state(dev, state);
248}
249
250/* Handle backend state transitions:
251 *
252 * The backend state starts in InitWait and the following transitions are
253 * allowed.
254 *
255 * InitWait -> Connected
256 *
257 * ^ \ |
258 * | \ |
259 * | \ |
260 * | \ |
261 * | \ |
262 * | \ |
263 * | V V
264 *
265 * Closed <-> Closing
266 *
267 * The state argument specifies the eventual state of the backend and the
268 * function transitions to that state via the shortest path.
269 */
270static void set_backend_state(struct backend_info *be,
271 enum xenbus_state state)
272{
273 while (be->state != state) {
274 switch (be->state) {
275 case XenbusStateClosed:
276 switch (state) {
277 case XenbusStateInitWait:
278 case XenbusStateConnected:
279 pr_info("%s: prepare for reconnect\n",
280 be->dev->nodename);
281 backend_switch_state(be, XenbusStateInitWait);
282 break;
283 case XenbusStateClosing:
284 backend_switch_state(be, XenbusStateClosing);
285 break;
286 default:
287 BUG();
288 }
289 break;
290 case XenbusStateInitWait:
291 switch (state) {
292 case XenbusStateConnected:
293 backend_connect(be);
294 backend_switch_state(be, XenbusStateConnected);
295 break;
296 case XenbusStateClosing:
297 case XenbusStateClosed:
298 backend_switch_state(be, XenbusStateClosing);
299 break;
300 default:
301 BUG();
302 }
303 break;
304 case XenbusStateConnected:
305 switch (state) {
306 case XenbusStateInitWait:
307 case XenbusStateClosing:
308 case XenbusStateClosed:
309 backend_disconnect(be);
310 backend_switch_state(be, XenbusStateClosing);
311 break;
312 default:
313 BUG();
314 }
315 break;
316 case XenbusStateClosing:
317 switch (state) {
318 case XenbusStateInitWait:
319 case XenbusStateConnected:
320 case XenbusStateClosed:
321 backend_switch_state(be, XenbusStateClosed);
322 break;
323 default:
324 BUG();
325 }
326 break;
327 default:
328 BUG();
329 }
229 } 330 }
230} 331}
231 332
@@ -237,40 +338,33 @@ static void frontend_changed(struct xenbus_device *dev,
237{ 338{
238 struct backend_info *be = dev_get_drvdata(&dev->dev); 339 struct backend_info *be = dev_get_drvdata(&dev->dev);
239 340
240 pr_debug("frontend state %s\n", xenbus_strstate(frontend_state)); 341 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
241 342
242 be->frontend_state = frontend_state; 343 be->frontend_state = frontend_state;
243 344
244 switch (frontend_state) { 345 switch (frontend_state) {
245 case XenbusStateInitialising: 346 case XenbusStateInitialising:
246 if (dev->state == XenbusStateClosed) { 347 set_backend_state(be, XenbusStateInitWait);
247 pr_info("%s: prepare for reconnect\n", dev->nodename);
248 xenbus_switch_state(dev, XenbusStateInitWait);
249 }
250 break; 348 break;
251 349
252 case XenbusStateInitialised: 350 case XenbusStateInitialised:
253 break; 351 break;
254 352
255 case XenbusStateConnected: 353 case XenbusStateConnected:
256 if (dev->state == XenbusStateConnected) 354 set_backend_state(be, XenbusStateConnected);
257 break;
258 if (be->vif)
259 connect(be);
260 break; 355 break;
261 356
262 case XenbusStateClosing: 357 case XenbusStateClosing:
263 disconnect_backend(dev); 358 set_backend_state(be, XenbusStateClosing);
264 xenbus_switch_state(dev, XenbusStateClosing);
265 break; 359 break;
266 360
267 case XenbusStateClosed: 361 case XenbusStateClosed:
268 xenbus_switch_state(dev, XenbusStateClosed); 362 set_backend_state(be, XenbusStateClosed);
269 if (xenbus_dev_is_online(dev)) 363 if (xenbus_dev_is_online(dev))
270 break; 364 break;
271 destroy_backend(dev);
272 /* fall through if not online */ 365 /* fall through if not online */
273 case XenbusStateUnknown: 366 case XenbusStateUnknown:
367 set_backend_state(be, XenbusStateClosed);
274 device_unregister(&dev->dev); 368 device_unregister(&dev->dev);
275 break; 369 break;
276 370
@@ -363,7 +457,9 @@ static void hotplug_status_changed(struct xenbus_watch *watch,
363 if (IS_ERR(str)) 457 if (IS_ERR(str))
364 return; 458 return;
365 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) { 459 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
366 xenbus_switch_state(be->dev, XenbusStateConnected); 460 /* Complete any pending state change */
461 xenbus_switch_state(be->dev, be->state);
462
367 /* Not interested in this watch anymore. */ 463 /* Not interested in this watch anymore. */
368 unregister_hotplug_status_watch(be); 464 unregister_hotplug_status_watch(be);
369 } 465 }
@@ -393,12 +489,8 @@ static void connect(struct backend_info *be)
393 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch, 489 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
394 hotplug_status_changed, 490 hotplug_status_changed,
395 "%s/%s", dev->nodename, "hotplug-status"); 491 "%s/%s", dev->nodename, "hotplug-status");
396 if (err) { 492 if (!err)
397 /* Switch now, since we can't do a watch. */
398 xenbus_switch_state(dev, XenbusStateConnected);
399 } else {
400 be->have_hotplug_status_watch = 1; 493 be->have_hotplug_status_watch = 1;
401 }
402 494
403 netif_wake_queue(be->vif->dev); 495 netif_wake_queue(be->vif->dev);
404} 496}