xenbus.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "common.h"
  21. #include <linux/vmalloc.h>
  22. #include <linux/rtnetlink.h>
  23. struct backend_info {
  24. struct xenbus_device *dev;
  25. struct xenvif *vif;
  26. /* This is the state that will be reflected in xenstore when any
  27. * active hotplug script completes.
  28. */
  29. enum xenbus_state state;
  30. enum xenbus_state frontend_state;
  31. struct xenbus_watch hotplug_status_watch;
  32. u8 have_hotplug_status_watch:1;
  33. const char *hotplug_script;
  34. };
  35. static int connect_rings(struct backend_info *be, struct xenvif_queue *queue);
  36. static void connect(struct backend_info *be);
  37. static int read_xenbus_vif_flags(struct backend_info *be);
  38. static int backend_create_xenvif(struct backend_info *be);
  39. static void unregister_hotplug_status_watch(struct backend_info *be);
  40. static void xen_unregister_watchers(struct xenvif *vif);
  41. static void set_backend_state(struct backend_info *be,
  42. enum xenbus_state state);
  43. #ifdef CONFIG_DEBUG_FS
  44. struct dentry *xen_netback_dbg_root = NULL;
  45. static int xenvif_read_io_ring(struct seq_file *m, void *v)
  46. {
  47. struct xenvif_queue *queue = m->private;
  48. struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
  49. struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
  50. struct netdev_queue *dev_queue;
  51. if (tx_ring->sring) {
  52. struct xen_netif_tx_sring *sring = tx_ring->sring;
  53. seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
  54. tx_ring->nr_ents);
  55. seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
  56. sring->req_prod,
  57. sring->req_prod - sring->rsp_prod,
  58. tx_ring->req_cons,
  59. tx_ring->req_cons - sring->rsp_prod,
  60. sring->req_event,
  61. sring->req_event - sring->rsp_prod);
  62. seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
  63. sring->rsp_prod,
  64. tx_ring->rsp_prod_pvt,
  65. tx_ring->rsp_prod_pvt - sring->rsp_prod,
  66. sring->rsp_event,
  67. sring->rsp_event - sring->rsp_prod);
  68. seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
  69. queue->pending_prod,
  70. queue->pending_cons,
  71. nr_pending_reqs(queue));
  72. seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
  73. queue->dealloc_prod,
  74. queue->dealloc_cons,
  75. queue->dealloc_prod - queue->dealloc_cons);
  76. }
  77. if (rx_ring->sring) {
  78. struct xen_netif_rx_sring *sring = rx_ring->sring;
  79. seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
  80. seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
  81. sring->req_prod,
  82. sring->req_prod - sring->rsp_prod,
  83. rx_ring->req_cons,
  84. rx_ring->req_cons - sring->rsp_prod,
  85. sring->req_event,
  86. sring->req_event - sring->rsp_prod);
  87. seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
  88. sring->rsp_prod,
  89. rx_ring->rsp_prod_pvt,
  90. rx_ring->rsp_prod_pvt - sring->rsp_prod,
  91. sring->rsp_event,
  92. sring->rsp_event - sring->rsp_prod);
  93. }
  94. seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
  95. "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
  96. "remaining: %lu, expires: %lu, now: %lu\n",
  97. queue->napi.state, queue->napi.weight,
  98. skb_queue_len(&queue->tx_queue),
  99. timer_pending(&queue->credit_timeout),
  100. queue->credit_bytes,
  101. queue->credit_usec,
  102. queue->remaining_credit,
  103. queue->credit_timeout.expires,
  104. jiffies);
  105. dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
  106. seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
  107. queue->rx_queue_len, queue->rx_queue_max,
  108. skb_queue_len(&queue->rx_queue),
  109. netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
  110. return 0;
  111. }
  112. #define XENVIF_KICK_STR "kick"
  113. #define BUFFER_SIZE 32
  114. static ssize_t
  115. xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
  116. loff_t *ppos)
  117. {
  118. struct xenvif_queue *queue =
  119. ((struct seq_file *)filp->private_data)->private;
  120. int len;
  121. char write[BUFFER_SIZE];
  122. /* don't allow partial writes and check the length */
  123. if (*ppos != 0)
  124. return 0;
  125. if (count >= sizeof(write))
  126. return -ENOSPC;
  127. len = simple_write_to_buffer(write,
  128. sizeof(write) - 1,
  129. ppos,
  130. buf,
  131. count);
  132. if (len < 0)
  133. return len;
  134. write[len] = '\0';
  135. if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
  136. xenvif_interrupt(0, (void *)queue);
  137. else {
  138. pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
  139. queue->id);
  140. count = -EINVAL;
  141. }
  142. return count;
  143. }
  144. static int xenvif_dump_open(struct inode *inode, struct file *filp)
  145. {
  146. int ret;
  147. void *queue = NULL;
  148. if (inode->i_private)
  149. queue = inode->i_private;
  150. ret = single_open(filp, xenvif_read_io_ring, queue);
  151. filp->f_mode |= FMODE_PWRITE;
  152. return ret;
  153. }
  154. static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
  155. .owner = THIS_MODULE,
  156. .open = xenvif_dump_open,
  157. .read = seq_read,
  158. .llseek = seq_lseek,
  159. .release = single_release,
  160. .write = xenvif_write_io_ring,
  161. };
  162. static void xenvif_debugfs_addif(struct xenvif *vif)
  163. {
  164. struct dentry *pfile;
  165. int i;
  166. if (IS_ERR_OR_NULL(xen_netback_dbg_root))
  167. return;
  168. vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
  169. xen_netback_dbg_root);
  170. if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
  171. for (i = 0; i < vif->num_queues; ++i) {
  172. char filename[sizeof("io_ring_q") + 4];
  173. snprintf(filename, sizeof(filename), "io_ring_q%d", i);
  174. pfile = debugfs_create_file(filename,
  175. S_IRUSR | S_IWUSR,
  176. vif->xenvif_dbg_root,
  177. &vif->queues[i],
  178. &xenvif_dbg_io_ring_ops_fops);
  179. if (IS_ERR_OR_NULL(pfile))
  180. pr_warn("Creation of io_ring file returned %ld!\n",
  181. PTR_ERR(pfile));
  182. }
  183. } else
  184. netdev_warn(vif->dev,
  185. "Creation of vif debugfs dir returned %ld!\n",
  186. PTR_ERR(vif->xenvif_dbg_root));
  187. }
  188. static void xenvif_debugfs_delif(struct xenvif *vif)
  189. {
  190. if (IS_ERR_OR_NULL(xen_netback_dbg_root))
  191. return;
  192. if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
  193. debugfs_remove_recursive(vif->xenvif_dbg_root);
  194. vif->xenvif_dbg_root = NULL;
  195. }
  196. #endif /* CONFIG_DEBUG_FS */
  197. static int netback_remove(struct xenbus_device *dev)
  198. {
  199. struct backend_info *be = dev_get_drvdata(&dev->dev);
  200. set_backend_state(be, XenbusStateClosed);
  201. unregister_hotplug_status_watch(be);
  202. if (be->vif) {
  203. kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
  204. xen_unregister_watchers(be->vif);
  205. xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
  206. xenvif_free(be->vif);
  207. be->vif = NULL;
  208. }
  209. kfree(be->hotplug_script);
  210. kfree(be);
  211. dev_set_drvdata(&dev->dev, NULL);
  212. return 0;
  213. }
  214. /**
  215. * Entry point to this code when a new device is created. Allocate the basic
  216. * structures and switch to InitWait.
  217. */
  218. static int netback_probe(struct xenbus_device *dev,
  219. const struct xenbus_device_id *id)
  220. {
  221. const char *message;
  222. struct xenbus_transaction xbt;
  223. int err;
  224. int sg;
  225. const char *script;
  226. struct backend_info *be = kzalloc(sizeof(struct backend_info),
  227. GFP_KERNEL);
  228. if (!be) {
  229. xenbus_dev_fatal(dev, -ENOMEM,
  230. "allocating backend structure");
  231. return -ENOMEM;
  232. }
  233. be->dev = dev;
  234. dev_set_drvdata(&dev->dev, be);
  235. sg = 1;
  236. do {
  237. err = xenbus_transaction_start(&xbt);
  238. if (err) {
  239. xenbus_dev_fatal(dev, err, "starting transaction");
  240. goto fail;
  241. }
  242. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
  243. if (err) {
  244. message = "writing feature-sg";
  245. goto abort_transaction;
  246. }
  247. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
  248. "%d", sg);
  249. if (err) {
  250. message = "writing feature-gso-tcpv4";
  251. goto abort_transaction;
  252. }
  253. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
  254. "%d", sg);
  255. if (err) {
  256. message = "writing feature-gso-tcpv6";
  257. goto abort_transaction;
  258. }
  259. /* We support partial checksum setup for IPv6 packets */
  260. err = xenbus_printf(xbt, dev->nodename,
  261. "feature-ipv6-csum-offload",
  262. "%d", 1);
  263. if (err) {
  264. message = "writing feature-ipv6-csum-offload";
  265. goto abort_transaction;
  266. }
  267. /* We support rx-copy path. */
  268. err = xenbus_printf(xbt, dev->nodename,
  269. "feature-rx-copy", "%d", 1);
  270. if (err) {
  271. message = "writing feature-rx-copy";
  272. goto abort_transaction;
  273. }
  274. /*
  275. * We don't support rx-flip path (except old guests who don't
  276. * grok this feature flag).
  277. */
  278. err = xenbus_printf(xbt, dev->nodename,
  279. "feature-rx-flip", "%d", 0);
  280. if (err) {
  281. message = "writing feature-rx-flip";
  282. goto abort_transaction;
  283. }
  284. /* We support dynamic multicast-control. */
  285. err = xenbus_printf(xbt, dev->nodename,
  286. "feature-multicast-control", "%d", 1);
  287. if (err) {
  288. message = "writing feature-multicast-control";
  289. goto abort_transaction;
  290. }
  291. err = xenbus_printf(xbt, dev->nodename,
  292. "feature-dynamic-multicast-control",
  293. "%d", 1);
  294. if (err) {
  295. message = "writing feature-dynamic-multicast-control";
  296. goto abort_transaction;
  297. }
  298. err = xenbus_transaction_end(xbt, 0);
  299. } while (err == -EAGAIN);
  300. if (err) {
  301. xenbus_dev_fatal(dev, err, "completing transaction");
  302. goto fail;
  303. }
  304. /*
  305. * Split event channels support, this is optional so it is not
  306. * put inside the above loop.
  307. */
  308. err = xenbus_printf(XBT_NIL, dev->nodename,
  309. "feature-split-event-channels",
  310. "%u", separate_tx_rx_irq);
  311. if (err)
  312. pr_debug("Error writing feature-split-event-channels\n");
  313. /* Multi-queue support: This is an optional feature. */
  314. err = xenbus_printf(XBT_NIL, dev->nodename,
  315. "multi-queue-max-queues", "%u", xenvif_max_queues);
  316. if (err)
  317. pr_debug("Error writing multi-queue-max-queues\n");
  318. script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
  319. if (IS_ERR(script)) {
  320. err = PTR_ERR(script);
  321. xenbus_dev_fatal(dev, err, "reading script");
  322. goto fail;
  323. }
  324. be->hotplug_script = script;
  325. err = xenbus_switch_state(dev, XenbusStateInitWait);
  326. if (err)
  327. goto fail;
  328. be->state = XenbusStateInitWait;
  329. /* This kicks hotplug scripts, so do it immediately. */
  330. err = backend_create_xenvif(be);
  331. if (err)
  332. goto fail;
  333. return 0;
  334. abort_transaction:
  335. xenbus_transaction_end(xbt, 1);
  336. xenbus_dev_fatal(dev, err, "%s", message);
  337. fail:
  338. pr_debug("failed\n");
  339. netback_remove(dev);
  340. return err;
  341. }
  342. /*
  343. * Handle the creation of the hotplug script environment. We add the script
  344. * and vif variables to the environment, for the benefit of the vif-* hotplug
  345. * scripts.
  346. */
  347. static int netback_uevent(struct xenbus_device *xdev,
  348. struct kobj_uevent_env *env)
  349. {
  350. struct backend_info *be = dev_get_drvdata(&xdev->dev);
  351. if (!be)
  352. return 0;
  353. if (add_uevent_var(env, "script=%s", be->hotplug_script))
  354. return -ENOMEM;
  355. if (!be->vif)
  356. return 0;
  357. return add_uevent_var(env, "vif=%s", be->vif->dev->name);
  358. }
  359. static int backend_create_xenvif(struct backend_info *be)
  360. {
  361. int err;
  362. long handle;
  363. struct xenbus_device *dev = be->dev;
  364. struct xenvif *vif;
  365. if (be->vif != NULL)
  366. return 0;
  367. err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
  368. if (err != 1) {
  369. xenbus_dev_fatal(dev, err, "reading handle");
  370. return (err < 0) ? err : -EINVAL;
  371. }
  372. vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
  373. if (IS_ERR(vif)) {
  374. err = PTR_ERR(vif);
  375. xenbus_dev_fatal(dev, err, "creating interface");
  376. return err;
  377. }
  378. be->vif = vif;
  379. kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
  380. return 0;
  381. }
  382. static void backend_disconnect(struct backend_info *be)
  383. {
  384. if (be->vif) {
  385. xen_unregister_watchers(be->vif);
  386. #ifdef CONFIG_DEBUG_FS
  387. xenvif_debugfs_delif(be->vif);
  388. #endif /* CONFIG_DEBUG_FS */
  389. xenvif_disconnect(be->vif);
  390. }
  391. }
  392. static void backend_connect(struct backend_info *be)
  393. {
  394. if (be->vif)
  395. connect(be);
  396. }
  397. static inline void backend_switch_state(struct backend_info *be,
  398. enum xenbus_state state)
  399. {
  400. struct xenbus_device *dev = be->dev;
  401. pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
  402. be->state = state;
  403. /* If we are waiting for a hotplug script then defer the
  404. * actual xenbus state change.
  405. */
  406. if (!be->have_hotplug_status_watch)
  407. xenbus_switch_state(dev, state);
  408. }
  409. /* Handle backend state transitions:
  410. *
  411. * The backend state starts in InitWait and the following transitions are
  412. * allowed.
  413. *
  414. * InitWait -> Connected
  415. *
  416. * ^ \ |
  417. * | \ |
  418. * | \ |
  419. * | \ |
  420. * | \ |
  421. * | \ |
  422. * | V V
  423. *
  424. * Closed <-> Closing
  425. *
  426. * The state argument specifies the eventual state of the backend and the
  427. * function transitions to that state via the shortest path.
  428. */
  429. static void set_backend_state(struct backend_info *be,
  430. enum xenbus_state state)
  431. {
  432. while (be->state != state) {
  433. switch (be->state) {
  434. case XenbusStateClosed:
  435. switch (state) {
  436. case XenbusStateInitWait:
  437. case XenbusStateConnected:
  438. backend_switch_state(be, XenbusStateInitWait);
  439. break;
  440. case XenbusStateClosing:
  441. backend_switch_state(be, XenbusStateClosing);
  442. break;
  443. default:
  444. BUG();
  445. }
  446. break;
  447. case XenbusStateInitWait:
  448. switch (state) {
  449. case XenbusStateConnected:
  450. backend_connect(be);
  451. backend_switch_state(be, XenbusStateConnected);
  452. break;
  453. case XenbusStateClosing:
  454. case XenbusStateClosed:
  455. backend_switch_state(be, XenbusStateClosing);
  456. break;
  457. default:
  458. BUG();
  459. }
  460. break;
  461. case XenbusStateConnected:
  462. switch (state) {
  463. case XenbusStateInitWait:
  464. case XenbusStateClosing:
  465. case XenbusStateClosed:
  466. backend_disconnect(be);
  467. backend_switch_state(be, XenbusStateClosing);
  468. break;
  469. default:
  470. BUG();
  471. }
  472. break;
  473. case XenbusStateClosing:
  474. switch (state) {
  475. case XenbusStateInitWait:
  476. case XenbusStateConnected:
  477. case XenbusStateClosed:
  478. backend_switch_state(be, XenbusStateClosed);
  479. break;
  480. default:
  481. BUG();
  482. }
  483. break;
  484. default:
  485. BUG();
  486. }
  487. }
  488. }
  489. /**
  490. * Callback received when the frontend's state changes.
  491. */
  492. static void frontend_changed(struct xenbus_device *dev,
  493. enum xenbus_state frontend_state)
  494. {
  495. struct backend_info *be = dev_get_drvdata(&dev->dev);
  496. pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
  497. be->frontend_state = frontend_state;
  498. switch (frontend_state) {
  499. case XenbusStateInitialising:
  500. set_backend_state(be, XenbusStateInitWait);
  501. break;
  502. case XenbusStateInitialised:
  503. break;
  504. case XenbusStateConnected:
  505. set_backend_state(be, XenbusStateConnected);
  506. break;
  507. case XenbusStateClosing:
  508. set_backend_state(be, XenbusStateClosing);
  509. break;
  510. case XenbusStateClosed:
  511. set_backend_state(be, XenbusStateClosed);
  512. if (xenbus_dev_is_online(dev))
  513. break;
  514. /* fall through if not online */
  515. case XenbusStateUnknown:
  516. set_backend_state(be, XenbusStateClosed);
  517. device_unregister(&dev->dev);
  518. break;
  519. default:
  520. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  521. frontend_state);
  522. break;
  523. }
  524. }
  525. static void xen_net_read_rate(struct xenbus_device *dev,
  526. unsigned long *bytes, unsigned long *usec)
  527. {
  528. char *s, *e;
  529. unsigned long b, u;
  530. char *ratestr;
  531. /* Default to unlimited bandwidth. */
  532. *bytes = ~0UL;
  533. *usec = 0;
  534. ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
  535. if (IS_ERR(ratestr))
  536. return;
  537. s = ratestr;
  538. b = simple_strtoul(s, &e, 10);
  539. if ((s == e) || (*e != ','))
  540. goto fail;
  541. s = e + 1;
  542. u = simple_strtoul(s, &e, 10);
  543. if ((s == e) || (*e != '\0'))
  544. goto fail;
  545. *bytes = b;
  546. *usec = u;
  547. kfree(ratestr);
  548. return;
  549. fail:
  550. pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
  551. kfree(ratestr);
  552. }
  553. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  554. {
  555. char *s, *e, *macstr;
  556. int i;
  557. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  558. if (IS_ERR(macstr))
  559. return PTR_ERR(macstr);
  560. for (i = 0; i < ETH_ALEN; i++) {
  561. mac[i] = simple_strtoul(s, &e, 16);
  562. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  563. kfree(macstr);
  564. return -ENOENT;
  565. }
  566. s = e+1;
  567. }
  568. kfree(macstr);
  569. return 0;
  570. }
  571. static void xen_net_rate_changed(struct xenbus_watch *watch,
  572. const char **vec, unsigned int len)
  573. {
  574. struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
  575. struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
  576. unsigned long credit_bytes;
  577. unsigned long credit_usec;
  578. unsigned int queue_index;
  579. xen_net_read_rate(dev, &credit_bytes, &credit_usec);
  580. for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
  581. struct xenvif_queue *queue = &vif->queues[queue_index];
  582. queue->credit_bytes = credit_bytes;
  583. queue->credit_usec = credit_usec;
  584. if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
  585. queue->remaining_credit > queue->credit_bytes) {
  586. queue->remaining_credit = queue->credit_bytes;
  587. }
  588. }
  589. }
  590. static int xen_register_credit_watch(struct xenbus_device *dev,
  591. struct xenvif *vif)
  592. {
  593. int err = 0;
  594. char *node;
  595. unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
  596. if (vif->credit_watch.node)
  597. return -EADDRINUSE;
  598. node = kmalloc(maxlen, GFP_KERNEL);
  599. if (!node)
  600. return -ENOMEM;
  601. snprintf(node, maxlen, "%s/rate", dev->nodename);
  602. vif->credit_watch.node = node;
  603. vif->credit_watch.callback = xen_net_rate_changed;
  604. err = register_xenbus_watch(&vif->credit_watch);
  605. if (err) {
  606. pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
  607. kfree(node);
  608. vif->credit_watch.node = NULL;
  609. vif->credit_watch.callback = NULL;
  610. }
  611. return err;
  612. }
  613. static void xen_unregister_credit_watch(struct xenvif *vif)
  614. {
  615. if (vif->credit_watch.node) {
  616. unregister_xenbus_watch(&vif->credit_watch);
  617. kfree(vif->credit_watch.node);
  618. vif->credit_watch.node = NULL;
  619. }
  620. }
  621. static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
  622. const char **vec, unsigned int len)
  623. {
  624. struct xenvif *vif = container_of(watch, struct xenvif,
  625. mcast_ctrl_watch);
  626. struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
  627. int val;
  628. if (xenbus_scanf(XBT_NIL, dev->otherend,
  629. "request-multicast-control", "%d", &val) < 0)
  630. val = 0;
  631. vif->multicast_control = !!val;
  632. }
  633. static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev,
  634. struct xenvif *vif)
  635. {
  636. int err = 0;
  637. char *node;
  638. unsigned maxlen = strlen(dev->otherend) +
  639. sizeof("/request-multicast-control");
  640. if (vif->mcast_ctrl_watch.node) {
  641. pr_err_ratelimited("Watch is already registered\n");
  642. return -EADDRINUSE;
  643. }
  644. node = kmalloc(maxlen, GFP_KERNEL);
  645. if (!node) {
  646. pr_err("Failed to allocate memory for watch\n");
  647. return -ENOMEM;
  648. }
  649. snprintf(node, maxlen, "%s/request-multicast-control",
  650. dev->otherend);
  651. vif->mcast_ctrl_watch.node = node;
  652. vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed;
  653. err = register_xenbus_watch(&vif->mcast_ctrl_watch);
  654. if (err) {
  655. pr_err("Failed to set watcher %s\n",
  656. vif->mcast_ctrl_watch.node);
  657. kfree(node);
  658. vif->mcast_ctrl_watch.node = NULL;
  659. vif->mcast_ctrl_watch.callback = NULL;
  660. }
  661. return err;
  662. }
  663. static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif)
  664. {
  665. if (vif->mcast_ctrl_watch.node) {
  666. unregister_xenbus_watch(&vif->mcast_ctrl_watch);
  667. kfree(vif->mcast_ctrl_watch.node);
  668. vif->mcast_ctrl_watch.node = NULL;
  669. }
  670. }
  671. static void xen_register_watchers(struct xenbus_device *dev,
  672. struct xenvif *vif)
  673. {
  674. xen_register_credit_watch(dev, vif);
  675. xen_register_mcast_ctrl_watch(dev, vif);
  676. }
  677. static void xen_unregister_watchers(struct xenvif *vif)
  678. {
  679. xen_unregister_mcast_ctrl_watch(vif);
  680. xen_unregister_credit_watch(vif);
  681. }
  682. static void unregister_hotplug_status_watch(struct backend_info *be)
  683. {
  684. if (be->have_hotplug_status_watch) {
  685. unregister_xenbus_watch(&be->hotplug_status_watch);
  686. kfree(be->hotplug_status_watch.node);
  687. }
  688. be->have_hotplug_status_watch = 0;
  689. }
  690. static void hotplug_status_changed(struct xenbus_watch *watch,
  691. const char **vec,
  692. unsigned int vec_size)
  693. {
  694. struct backend_info *be = container_of(watch,
  695. struct backend_info,
  696. hotplug_status_watch);
  697. char *str;
  698. unsigned int len;
  699. str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
  700. if (IS_ERR(str))
  701. return;
  702. if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
  703. /* Complete any pending state change */
  704. xenbus_switch_state(be->dev, be->state);
  705. /* Not interested in this watch anymore. */
  706. unregister_hotplug_status_watch(be);
  707. }
  708. kfree(str);
  709. }
  710. static void connect(struct backend_info *be)
  711. {
  712. int err;
  713. struct xenbus_device *dev = be->dev;
  714. unsigned long credit_bytes, credit_usec;
  715. unsigned int queue_index;
  716. unsigned int requested_num_queues;
  717. struct xenvif_queue *queue;
  718. /* Check whether the frontend requested multiple queues
  719. * and read the number requested.
  720. */
  721. err = xenbus_scanf(XBT_NIL, dev->otherend,
  722. "multi-queue-num-queues",
  723. "%u", &requested_num_queues);
  724. if (err < 0) {
  725. requested_num_queues = 1; /* Fall back to single queue */
  726. } else if (requested_num_queues > xenvif_max_queues) {
  727. /* buggy or malicious guest */
  728. xenbus_dev_fatal(dev, err,
  729. "guest requested %u queues, exceeding the maximum of %u.",
  730. requested_num_queues, xenvif_max_queues);
  731. return;
  732. }
  733. err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
  734. if (err) {
  735. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  736. return;
  737. }
  738. xen_net_read_rate(dev, &credit_bytes, &credit_usec);
  739. xen_unregister_watchers(be->vif);
  740. xen_register_watchers(dev, be->vif);
  741. read_xenbus_vif_flags(be);
  742. /* Use the number of queues requested by the frontend */
  743. be->vif->queues = vzalloc(requested_num_queues *
  744. sizeof(struct xenvif_queue));
  745. if (!be->vif->queues) {
  746. xenbus_dev_fatal(dev, -ENOMEM,
  747. "allocating queues");
  748. return;
  749. }
  750. be->vif->num_queues = requested_num_queues;
  751. be->vif->stalled_queues = requested_num_queues;
  752. for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
  753. queue = &be->vif->queues[queue_index];
  754. queue->vif = be->vif;
  755. queue->id = queue_index;
  756. snprintf(queue->name, sizeof(queue->name), "%s-q%u",
  757. be->vif->dev->name, queue->id);
  758. err = xenvif_init_queue(queue);
  759. if (err) {
  760. /* xenvif_init_queue() cleans up after itself on
  761. * failure, but we need to clean up any previously
  762. * initialised queues. Set num_queues to i so that
  763. * earlier queues can be destroyed using the regular
  764. * disconnect logic.
  765. */
  766. be->vif->num_queues = queue_index;
  767. goto err;
  768. }
  769. queue->credit_bytes = credit_bytes;
  770. queue->remaining_credit = credit_bytes;
  771. queue->credit_usec = credit_usec;
  772. err = connect_rings(be, queue);
  773. if (err) {
  774. /* connect_rings() cleans up after itself on failure,
  775. * but we need to clean up after xenvif_init_queue() here,
  776. * and also clean up any previously initialised queues.
  777. */
  778. xenvif_deinit_queue(queue);
  779. be->vif->num_queues = queue_index;
  780. goto err;
  781. }
  782. }
  783. #ifdef CONFIG_DEBUG_FS
  784. xenvif_debugfs_addif(be->vif);
  785. #endif /* CONFIG_DEBUG_FS */
  786. /* Initialisation completed, tell core driver the number of
  787. * active queues.
  788. */
  789. rtnl_lock();
  790. netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
  791. netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
  792. rtnl_unlock();
  793. xenvif_carrier_on(be->vif);
  794. unregister_hotplug_status_watch(be);
  795. err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
  796. hotplug_status_changed,
  797. "%s/%s", dev->nodename, "hotplug-status");
  798. if (!err)
  799. be->have_hotplug_status_watch = 1;
  800. netif_tx_wake_all_queues(be->vif->dev);
  801. return;
  802. err:
  803. if (be->vif->num_queues > 0)
  804. xenvif_disconnect(be->vif); /* Clean up existing queues */
  805. vfree(be->vif->queues);
  806. be->vif->queues = NULL;
  807. be->vif->num_queues = 0;
  808. return;
  809. }
  810. static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
  811. {
  812. struct xenbus_device *dev = be->dev;
  813. unsigned int num_queues = queue->vif->num_queues;
  814. unsigned long tx_ring_ref, rx_ring_ref;
  815. unsigned int tx_evtchn, rx_evtchn;
  816. int err;
  817. char *xspath;
  818. size_t xspathsize;
  819. const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
  820. /* If the frontend requested 1 queue, or we have fallen back
  821. * to single queue due to lack of frontend support for multi-
  822. * queue, expect the remaining XenStore keys in the toplevel
  823. * directory. Otherwise, expect them in a subdirectory called
  824. * queue-N.
  825. */
  826. if (num_queues == 1) {
  827. xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
  828. if (!xspath) {
  829. xenbus_dev_fatal(dev, -ENOMEM,
  830. "reading ring references");
  831. return -ENOMEM;
  832. }
  833. strcpy(xspath, dev->otherend);
  834. } else {
  835. xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
  836. xspath = kzalloc(xspathsize, GFP_KERNEL);
  837. if (!xspath) {
  838. xenbus_dev_fatal(dev, -ENOMEM,
  839. "reading ring references");
  840. return -ENOMEM;
  841. }
  842. snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
  843. queue->id);
  844. }
  845. err = xenbus_gather(XBT_NIL, xspath,
  846. "tx-ring-ref", "%lu", &tx_ring_ref,
  847. "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
  848. if (err) {
  849. xenbus_dev_fatal(dev, err,
  850. "reading %s/ring-ref",
  851. xspath);
  852. goto err;
  853. }
  854. /* Try split event channels first, then single event channel. */
  855. err = xenbus_gather(XBT_NIL, xspath,
  856. "event-channel-tx", "%u", &tx_evtchn,
  857. "event-channel-rx", "%u", &rx_evtchn, NULL);
  858. if (err < 0) {
  859. err = xenbus_scanf(XBT_NIL, xspath,
  860. "event-channel", "%u", &tx_evtchn);
  861. if (err < 0) {
  862. xenbus_dev_fatal(dev, err,
  863. "reading %s/event-channel(-tx/rx)",
  864. xspath);
  865. goto err;
  866. }
  867. rx_evtchn = tx_evtchn;
  868. }
  869. /* Map the shared frame, irq etc. */
  870. err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
  871. tx_evtchn, rx_evtchn);
  872. if (err) {
  873. xenbus_dev_fatal(dev, err,
  874. "mapping shared-frames %lu/%lu port tx %u rx %u",
  875. tx_ring_ref, rx_ring_ref,
  876. tx_evtchn, rx_evtchn);
  877. goto err;
  878. }
  879. err = 0;
  880. err: /* Regular return falls through with err == 0 */
  881. kfree(xspath);
  882. return err;
  883. }
  884. static int read_xenbus_vif_flags(struct backend_info *be)
  885. {
  886. struct xenvif *vif = be->vif;
  887. struct xenbus_device *dev = be->dev;
  888. unsigned int rx_copy;
  889. int err, val;
  890. err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
  891. &rx_copy);
  892. if (err == -ENOENT) {
  893. err = 0;
  894. rx_copy = 0;
  895. }
  896. if (err < 0) {
  897. xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
  898. dev->otherend);
  899. return err;
  900. }
  901. if (!rx_copy)
  902. return -EOPNOTSUPP;
  903. if (xenbus_scanf(XBT_NIL, dev->otherend,
  904. "feature-rx-notify", "%d", &val) < 0)
  905. val = 0;
  906. if (!val) {
  907. /* - Reduce drain timeout to poll more frequently for
  908. * Rx requests.
  909. * - Disable Rx stall detection.
  910. */
  911. be->vif->drain_timeout = msecs_to_jiffies(30);
  912. be->vif->stall_timeout = 0;
  913. }
  914. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
  915. "%d", &val) < 0)
  916. val = 0;
  917. vif->can_sg = !!val;
  918. vif->gso_mask = 0;
  919. vif->gso_prefix_mask = 0;
  920. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
  921. "%d", &val) < 0)
  922. val = 0;
  923. if (val)
  924. vif->gso_mask |= GSO_BIT(TCPV4);
  925. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
  926. "%d", &val) < 0)
  927. val = 0;
  928. if (val)
  929. vif->gso_prefix_mask |= GSO_BIT(TCPV4);
  930. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
  931. "%d", &val) < 0)
  932. val = 0;
  933. if (val)
  934. vif->gso_mask |= GSO_BIT(TCPV6);
  935. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
  936. "%d", &val) < 0)
  937. val = 0;
  938. if (val)
  939. vif->gso_prefix_mask |= GSO_BIT(TCPV6);
  940. if (vif->gso_mask & vif->gso_prefix_mask) {
  941. xenbus_dev_fatal(dev, err,
  942. "%s: gso and gso prefix flags are not "
  943. "mutually exclusive",
  944. dev->otherend);
  945. return -EOPNOTSUPP;
  946. }
  947. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
  948. "%d", &val) < 0)
  949. val = 0;
  950. vif->ip_csum = !val;
  951. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
  952. "%d", &val) < 0)
  953. val = 0;
  954. vif->ipv6_csum = !!val;
  955. return 0;
  956. }
  957. static const struct xenbus_device_id netback_ids[] = {
  958. { "vif" },
  959. { "" }
  960. };
  961. static struct xenbus_driver netback_driver = {
  962. .ids = netback_ids,
  963. .probe = netback_probe,
  964. .remove = netback_remove,
  965. .uevent = netback_uevent,
  966. .otherend_changed = frontend_changed,
  967. };
  968. int xenvif_xenbus_init(void)
  969. {
  970. return xenbus_register_backend(&netback_driver);
  971. }
  972. void xenvif_xenbus_fini(void)
  973. {
  974. return xenbus_unregister_driver(&netback_driver);
  975. }