]>
Commit | Line | Data |
---|---|---|
f942dc25 IC |
1 | /* |
2 | * Xenbus code for netif backend | |
3 | * | |
4 | * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au> | |
5 | * Copyright (C) 2005 XenSource Ltd | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #include "common.h" | |
23 | ||
24 | struct backend_info { | |
25 | struct xenbus_device *dev; | |
26 | struct xenvif *vif; | |
27 | enum xenbus_state frontend_state; | |
28 | struct xenbus_watch hotplug_status_watch; | |
17938a69 | 29 | u8 have_hotplug_status_watch:1; |
f942dc25 IC |
30 | }; |
31 | ||
32 | static int connect_rings(struct backend_info *); | |
33 | static void connect(struct backend_info *); | |
34 | static void backend_create_xenvif(struct backend_info *be); | |
35 | static void unregister_hotplug_status_watch(struct backend_info *be); | |
36 | ||
37 | static int netback_remove(struct xenbus_device *dev) | |
38 | { | |
39 | struct backend_info *be = dev_get_drvdata(&dev->dev); | |
40 | ||
41 | unregister_hotplug_status_watch(be); | |
42 | if (be->vif) { | |
43 | kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE); | |
44 | xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status"); | |
45 | xenvif_disconnect(be->vif); | |
46 | be->vif = NULL; | |
47 | } | |
48 | kfree(be); | |
49 | dev_set_drvdata(&dev->dev, NULL); | |
50 | return 0; | |
51 | } | |
52 | ||
53 | ||
54 | /** | |
55 | * Entry point to this code when a new device is created. Allocate the basic | |
56 | * structures and switch to InitWait. | |
57 | */ | |
58 | static int netback_probe(struct xenbus_device *dev, | |
59 | const struct xenbus_device_id *id) | |
60 | { | |
61 | const char *message; | |
62 | struct xenbus_transaction xbt; | |
63 | int err; | |
64 | int sg; | |
65 | struct backend_info *be = kzalloc(sizeof(struct backend_info), | |
66 | GFP_KERNEL); | |
67 | if (!be) { | |
68 | xenbus_dev_fatal(dev, -ENOMEM, | |
69 | "allocating backend structure"); | |
70 | return -ENOMEM; | |
71 | } | |
72 | ||
73 | be->dev = dev; | |
74 | dev_set_drvdata(&dev->dev, be); | |
75 | ||
76 | sg = 1; | |
77 | ||
78 | do { | |
79 | err = xenbus_transaction_start(&xbt); | |
80 | if (err) { | |
81 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
82 | goto fail; | |
83 | } | |
84 | ||
85 | err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg); | |
86 | if (err) { | |
87 | message = "writing feature-sg"; | |
88 | goto abort_transaction; | |
89 | } | |
90 | ||
91 | err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", | |
92 | "%d", sg); | |
93 | if (err) { | |
94 | message = "writing feature-gso-tcpv4"; | |
95 | goto abort_transaction; | |
96 | } | |
97 | ||
98 | /* We support rx-copy path. */ | |
99 | err = xenbus_printf(xbt, dev->nodename, | |
100 | "feature-rx-copy", "%d", 1); | |
101 | if (err) { | |
102 | message = "writing feature-rx-copy"; | |
103 | goto abort_transaction; | |
104 | } | |
105 | ||
106 | /* | |
107 | * We don't support rx-flip path (except old guests who don't | |
108 | * grok this feature flag). | |
109 | */ | |
110 | err = xenbus_printf(xbt, dev->nodename, | |
111 | "feature-rx-flip", "%d", 0); | |
112 | if (err) { | |
113 | message = "writing feature-rx-flip"; | |
114 | goto abort_transaction; | |
115 | } | |
116 | ||
117 | err = xenbus_transaction_end(xbt, 0); | |
118 | } while (err == -EAGAIN); | |
119 | ||
120 | if (err) { | |
121 | xenbus_dev_fatal(dev, err, "completing transaction"); | |
122 | goto fail; | |
123 | } | |
124 | ||
e1f00a69 WL |
125 | /* |
126 | * Split event channels support, this is optional so it is not | |
127 | * put inside the above loop. | |
128 | */ | |
129 | err = xenbus_printf(XBT_NIL, dev->nodename, | |
130 | "feature-split-event-channels", | |
131 | "%u", separate_tx_rx_irq); | |
132 | if (err) | |
133 | pr_debug("Error writing feature-split-event-channels"); | |
134 | ||
f942dc25 IC |
135 | err = xenbus_switch_state(dev, XenbusStateInitWait); |
136 | if (err) | |
137 | goto fail; | |
138 | ||
139 | /* This kicks hotplug scripts, so do it immediately. */ | |
140 | backend_create_xenvif(be); | |
141 | ||
142 | return 0; | |
143 | ||
144 | abort_transaction: | |
145 | xenbus_transaction_end(xbt, 1); | |
146 | xenbus_dev_fatal(dev, err, "%s", message); | |
147 | fail: | |
148 | pr_debug("failed"); | |
149 | netback_remove(dev); | |
150 | return err; | |
151 | } | |
152 | ||
153 | ||
154 | /* | |
155 | * Handle the creation of the hotplug script environment. We add the script | |
156 | * and vif variables to the environment, for the benefit of the vif-* hotplug | |
157 | * scripts. | |
158 | */ | |
159 | static int netback_uevent(struct xenbus_device *xdev, | |
160 | struct kobj_uevent_env *env) | |
161 | { | |
162 | struct backend_info *be = dev_get_drvdata(&xdev->dev); | |
163 | char *val; | |
164 | ||
165 | val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL); | |
166 | if (IS_ERR(val)) { | |
167 | int err = PTR_ERR(val); | |
168 | xenbus_dev_fatal(xdev, err, "reading script"); | |
169 | return err; | |
170 | } else { | |
171 | if (add_uevent_var(env, "script=%s", val)) { | |
172 | kfree(val); | |
173 | return -ENOMEM; | |
174 | } | |
175 | kfree(val); | |
176 | } | |
177 | ||
178 | if (!be || !be->vif) | |
179 | return 0; | |
180 | ||
181 | return add_uevent_var(env, "vif=%s", be->vif->dev->name); | |
182 | } | |
183 | ||
184 | ||
185 | static void backend_create_xenvif(struct backend_info *be) | |
186 | { | |
187 | int err; | |
188 | long handle; | |
189 | struct xenbus_device *dev = be->dev; | |
190 | ||
191 | if (be->vif != NULL) | |
192 | return; | |
193 | ||
194 | err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle); | |
195 | if (err != 1) { | |
196 | xenbus_dev_fatal(dev, err, "reading handle"); | |
197 | return; | |
198 | } | |
199 | ||
200 | be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle); | |
201 | if (IS_ERR(be->vif)) { | |
202 | err = PTR_ERR(be->vif); | |
203 | be->vif = NULL; | |
204 | xenbus_dev_fatal(dev, err, "creating interface"); | |
205 | return; | |
206 | } | |
207 | ||
208 | kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE); | |
209 | } | |
210 | ||
211 | ||
212 | static void disconnect_backend(struct xenbus_device *dev) | |
213 | { | |
214 | struct backend_info *be = dev_get_drvdata(&dev->dev); | |
215 | ||
216 | if (be->vif) { | |
217 | xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status"); | |
218 | xenvif_disconnect(be->vif); | |
219 | be->vif = NULL; | |
220 | } | |
221 | } | |
222 | ||
223 | /** | |
224 | * Callback received when the frontend's state changes. | |
225 | */ | |
226 | static void frontend_changed(struct xenbus_device *dev, | |
227 | enum xenbus_state frontend_state) | |
228 | { | |
229 | struct backend_info *be = dev_get_drvdata(&dev->dev); | |
230 | ||
231 | pr_debug("frontend state %s", xenbus_strstate(frontend_state)); | |
232 | ||
233 | be->frontend_state = frontend_state; | |
234 | ||
235 | switch (frontend_state) { | |
236 | case XenbusStateInitialising: | |
237 | if (dev->state == XenbusStateClosed) { | |
238 | printk(KERN_INFO "%s: %s: prepare for reconnect\n", | |
239 | __func__, dev->nodename); | |
240 | xenbus_switch_state(dev, XenbusStateInitWait); | |
241 | } | |
242 | break; | |
243 | ||
244 | case XenbusStateInitialised: | |
245 | break; | |
246 | ||
247 | case XenbusStateConnected: | |
248 | if (dev->state == XenbusStateConnected) | |
249 | break; | |
250 | backend_create_xenvif(be); | |
251 | if (be->vif) | |
252 | connect(be); | |
253 | break; | |
254 | ||
255 | case XenbusStateClosing: | |
256 | if (be->vif) | |
257 | kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE); | |
258 | disconnect_backend(dev); | |
259 | xenbus_switch_state(dev, XenbusStateClosing); | |
260 | break; | |
261 | ||
262 | case XenbusStateClosed: | |
263 | xenbus_switch_state(dev, XenbusStateClosed); | |
264 | if (xenbus_dev_is_online(dev)) | |
265 | break; | |
266 | /* fall through if not online */ | |
267 | case XenbusStateUnknown: | |
268 | device_unregister(&dev->dev); | |
269 | break; | |
270 | ||
271 | default: | |
272 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", | |
273 | frontend_state); | |
274 | break; | |
275 | } | |
276 | } | |
277 | ||
278 | ||
279 | static void xen_net_read_rate(struct xenbus_device *dev, | |
280 | unsigned long *bytes, unsigned long *usec) | |
281 | { | |
282 | char *s, *e; | |
283 | unsigned long b, u; | |
284 | char *ratestr; | |
285 | ||
286 | /* Default to unlimited bandwidth. */ | |
287 | *bytes = ~0UL; | |
288 | *usec = 0; | |
289 | ||
290 | ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL); | |
291 | if (IS_ERR(ratestr)) | |
292 | return; | |
293 | ||
294 | s = ratestr; | |
295 | b = simple_strtoul(s, &e, 10); | |
296 | if ((s == e) || (*e != ',')) | |
297 | goto fail; | |
298 | ||
299 | s = e + 1; | |
300 | u = simple_strtoul(s, &e, 10); | |
301 | if ((s == e) || (*e != '\0')) | |
302 | goto fail; | |
303 | ||
304 | *bytes = b; | |
305 | *usec = u; | |
306 | ||
307 | kfree(ratestr); | |
308 | return; | |
309 | ||
310 | fail: | |
311 | pr_warn("Failed to parse network rate limit. Traffic unlimited.\n"); | |
312 | kfree(ratestr); | |
313 | } | |
314 | ||
315 | static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[]) | |
316 | { | |
317 | char *s, *e, *macstr; | |
318 | int i; | |
319 | ||
320 | macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL); | |
321 | if (IS_ERR(macstr)) | |
322 | return PTR_ERR(macstr); | |
323 | ||
324 | for (i = 0; i < ETH_ALEN; i++) { | |
325 | mac[i] = simple_strtoul(s, &e, 16); | |
326 | if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) { | |
327 | kfree(macstr); | |
328 | return -ENOENT; | |
329 | } | |
330 | s = e+1; | |
331 | } | |
332 | ||
333 | kfree(macstr); | |
334 | return 0; | |
335 | } | |
336 | ||
337 | static void unregister_hotplug_status_watch(struct backend_info *be) | |
338 | { | |
339 | if (be->have_hotplug_status_watch) { | |
340 | unregister_xenbus_watch(&be->hotplug_status_watch); | |
341 | kfree(be->hotplug_status_watch.node); | |
342 | } | |
343 | be->have_hotplug_status_watch = 0; | |
344 | } | |
345 | ||
346 | static void hotplug_status_changed(struct xenbus_watch *watch, | |
347 | const char **vec, | |
348 | unsigned int vec_size) | |
349 | { | |
350 | struct backend_info *be = container_of(watch, | |
351 | struct backend_info, | |
352 | hotplug_status_watch); | |
353 | char *str; | |
354 | unsigned int len; | |
355 | ||
356 | str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len); | |
357 | if (IS_ERR(str)) | |
358 | return; | |
359 | if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) { | |
360 | xenbus_switch_state(be->dev, XenbusStateConnected); | |
361 | /* Not interested in this watch anymore. */ | |
362 | unregister_hotplug_status_watch(be); | |
363 | } | |
364 | kfree(str); | |
365 | } | |
366 | ||
367 | static void connect(struct backend_info *be) | |
368 | { | |
369 | int err; | |
370 | struct xenbus_device *dev = be->dev; | |
371 | ||
372 | err = connect_rings(be); | |
373 | if (err) | |
374 | return; | |
375 | ||
376 | err = xen_net_read_mac(dev, be->vif->fe_dev_addr); | |
377 | if (err) { | |
378 | xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename); | |
379 | return; | |
380 | } | |
381 | ||
382 | xen_net_read_rate(dev, &be->vif->credit_bytes, | |
383 | &be->vif->credit_usec); | |
384 | be->vif->remaining_credit = be->vif->credit_bytes; | |
385 | ||
386 | unregister_hotplug_status_watch(be); | |
387 | err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch, | |
388 | hotplug_status_changed, | |
389 | "%s/%s", dev->nodename, "hotplug-status"); | |
390 | if (err) { | |
391 | /* Switch now, since we can't do a watch. */ | |
392 | xenbus_switch_state(dev, XenbusStateConnected); | |
393 | } else { | |
394 | be->have_hotplug_status_watch = 1; | |
395 | } | |
396 | ||
397 | netif_wake_queue(be->vif->dev); | |
398 | } | |
399 | ||
400 | ||
401 | static int connect_rings(struct backend_info *be) | |
402 | { | |
403 | struct xenvif *vif = be->vif; | |
404 | struct xenbus_device *dev = be->dev; | |
405 | unsigned long tx_ring_ref, rx_ring_ref; | |
e1f00a69 | 406 | unsigned int tx_evtchn, rx_evtchn, rx_copy; |
f942dc25 IC |
407 | int err; |
408 | int val; | |
409 | ||
410 | err = xenbus_gather(XBT_NIL, dev->otherend, | |
411 | "tx-ring-ref", "%lu", &tx_ring_ref, | |
e1f00a69 | 412 | "rx-ring-ref", "%lu", &rx_ring_ref, NULL); |
f942dc25 IC |
413 | if (err) { |
414 | xenbus_dev_fatal(dev, err, | |
e1f00a69 | 415 | "reading %s/ring-ref", |
f942dc25 IC |
416 | dev->otherend); |
417 | return err; | |
418 | } | |
419 | ||
e1f00a69 WL |
420 | /* Try split event channels first, then single event channel. */ |
421 | err = xenbus_gather(XBT_NIL, dev->otherend, | |
422 | "event-channel-tx", "%u", &tx_evtchn, | |
423 | "event-channel-rx", "%u", &rx_evtchn, NULL); | |
424 | if (err < 0) { | |
425 | err = xenbus_scanf(XBT_NIL, dev->otherend, | |
426 | "event-channel", "%u", &tx_evtchn); | |
427 | if (err < 0) { | |
428 | xenbus_dev_fatal(dev, err, | |
429 | "reading %s/event-channel(-tx/rx)", | |
430 | dev->otherend); | |
431 | return err; | |
432 | } | |
433 | rx_evtchn = tx_evtchn; | |
434 | } | |
435 | ||
f942dc25 IC |
436 | err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u", |
437 | &rx_copy); | |
438 | if (err == -ENOENT) { | |
439 | err = 0; | |
440 | rx_copy = 0; | |
441 | } | |
442 | if (err < 0) { | |
443 | xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy", | |
444 | dev->otherend); | |
445 | return err; | |
446 | } | |
447 | if (!rx_copy) | |
448 | return -EOPNOTSUPP; | |
449 | ||
450 | if (vif->dev->tx_queue_len != 0) { | |
451 | if (xenbus_scanf(XBT_NIL, dev->otherend, | |
452 | "feature-rx-notify", "%d", &val) < 0) | |
453 | val = 0; | |
454 | if (val) | |
455 | vif->can_queue = 1; | |
456 | else | |
457 | /* Must be non-zero for pfifo_fast to work. */ | |
458 | vif->dev->tx_queue_len = 1; | |
459 | } | |
460 | ||
461 | if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", | |
462 | "%d", &val) < 0) | |
463 | val = 0; | |
464 | vif->can_sg = !!val; | |
465 | ||
466 | if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", | |
467 | "%d", &val) < 0) | |
468 | val = 0; | |
469 | vif->gso = !!val; | |
470 | ||
471 | if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix", | |
472 | "%d", &val) < 0) | |
473 | val = 0; | |
474 | vif->gso_prefix = !!val; | |
475 | ||
476 | if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload", | |
477 | "%d", &val) < 0) | |
478 | val = 0; | |
479 | vif->csum = !val; | |
480 | ||
481 | /* Map the shared frame, irq etc. */ | |
e1f00a69 WL |
482 | err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref, |
483 | tx_evtchn, rx_evtchn); | |
f942dc25 IC |
484 | if (err) { |
485 | xenbus_dev_fatal(dev, err, | |
e1f00a69 WL |
486 | "mapping shared-frames %lu/%lu port tx %u rx %u", |
487 | tx_ring_ref, rx_ring_ref, | |
488 | tx_evtchn, rx_evtchn); | |
f942dc25 IC |
489 | return err; |
490 | } | |
491 | return 0; | |
492 | } | |
493 | ||
494 | ||
495 | /* ** Driver Registration ** */ | |
496 | ||
497 | ||
498 | static const struct xenbus_device_id netback_ids[] = { | |
499 | { "vif" }, | |
500 | { "" } | |
501 | }; | |
502 | ||
503 | ||
73db144b | 504 | static DEFINE_XENBUS_DRIVER(netback, , |
f942dc25 IC |
505 | .probe = netback_probe, |
506 | .remove = netback_remove, | |
507 | .uevent = netback_uevent, | |
508 | .otherend_changed = frontend_changed, | |
73db144b | 509 | ); |
f942dc25 IC |
510 | |
511 | int xenvif_xenbus_init(void) | |
512 | { | |
73db144b | 513 | return xenbus_register_backend(&netback_driver); |
f942dc25 | 514 | } |
b103f358 WL |
515 | |
516 | void xenvif_xenbus_fini(void) | |
517 | { | |
518 | return xenbus_unregister_driver(&netback_driver); | |
519 | } |