xenbus.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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 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_transaction_end(xbt, 0);
  292. } while (err == -EAGAIN);
  293. if (err) {
  294. xenbus_dev_fatal(dev, err, "completing transaction");
  295. goto fail;
  296. }
  297. /*
  298. * Split event channels support, this is optional so it is not
  299. * put inside the above loop.
  300. */
  301. err = xenbus_printf(XBT_NIL, dev->nodename,
  302. "feature-split-event-channels",
  303. "%u", separate_tx_rx_irq);
  304. if (err)
  305. pr_debug("Error writing feature-split-event-channels\n");
  306. /* Multi-queue support: This is an optional feature. */
  307. err = xenbus_printf(XBT_NIL, dev->nodename,
  308. "multi-queue-max-queues", "%u", xenvif_max_queues);
  309. if (err)
  310. pr_debug("Error writing multi-queue-max-queues\n");
  311. script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
  312. if (IS_ERR(script)) {
  313. err = PTR_ERR(script);
  314. xenbus_dev_fatal(dev, err, "reading script");
  315. goto fail;
  316. }
  317. be->hotplug_script = script;
  318. err = xenbus_switch_state(dev, XenbusStateInitWait);
  319. if (err)
  320. goto fail;
  321. be->state = XenbusStateInitWait;
  322. /* This kicks hotplug scripts, so do it immediately. */
  323. err = backend_create_xenvif(be);
  324. if (err)
  325. goto fail;
  326. return 0;
  327. abort_transaction:
  328. xenbus_transaction_end(xbt, 1);
  329. xenbus_dev_fatal(dev, err, "%s", message);
  330. fail:
  331. pr_debug("failed\n");
  332. netback_remove(dev);
  333. return err;
  334. }
  335. /*
  336. * Handle the creation of the hotplug script environment. We add the script
  337. * and vif variables to the environment, for the benefit of the vif-* hotplug
  338. * scripts.
  339. */
  340. static int netback_uevent(struct xenbus_device *xdev,
  341. struct kobj_uevent_env *env)
  342. {
  343. struct backend_info *be = dev_get_drvdata(&xdev->dev);
  344. if (!be)
  345. return 0;
  346. if (add_uevent_var(env, "script=%s", be->hotplug_script))
  347. return -ENOMEM;
  348. if (!be->vif)
  349. return 0;
  350. return add_uevent_var(env, "vif=%s", be->vif->dev->name);
  351. }
  352. static int backend_create_xenvif(struct backend_info *be)
  353. {
  354. int err;
  355. long handle;
  356. struct xenbus_device *dev = be->dev;
  357. struct xenvif *vif;
  358. if (be->vif != NULL)
  359. return 0;
  360. err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
  361. if (err != 1) {
  362. xenbus_dev_fatal(dev, err, "reading handle");
  363. return (err < 0) ? err : -EINVAL;
  364. }
  365. vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
  366. if (IS_ERR(vif)) {
  367. err = PTR_ERR(vif);
  368. xenbus_dev_fatal(dev, err, "creating interface");
  369. return err;
  370. }
  371. be->vif = vif;
  372. kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
  373. return 0;
  374. }
  375. static void backend_disconnect(struct backend_info *be)
  376. {
  377. if (be->vif) {
  378. xen_unregister_watchers(be->vif);
  379. #ifdef CONFIG_DEBUG_FS
  380. xenvif_debugfs_delif(be->vif);
  381. #endif /* CONFIG_DEBUG_FS */
  382. xenvif_disconnect(be->vif);
  383. }
  384. }
  385. static void backend_connect(struct backend_info *be)
  386. {
  387. if (be->vif)
  388. connect(be);
  389. }
  390. static inline void backend_switch_state(struct backend_info *be,
  391. enum xenbus_state state)
  392. {
  393. struct xenbus_device *dev = be->dev;
  394. pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
  395. be->state = state;
  396. /* If we are waiting for a hotplug script then defer the
  397. * actual xenbus state change.
  398. */
  399. if (!be->have_hotplug_status_watch)
  400. xenbus_switch_state(dev, state);
  401. }
  402. /* Handle backend state transitions:
  403. *
  404. * The backend state starts in InitWait and the following transitions are
  405. * allowed.
  406. *
  407. * InitWait -> Connected
  408. *
  409. * ^ \ |
  410. * | \ |
  411. * | \ |
  412. * | \ |
  413. * | \ |
  414. * | \ |
  415. * | V V
  416. *
  417. * Closed <-> Closing
  418. *
  419. * The state argument specifies the eventual state of the backend and the
  420. * function transitions to that state via the shortest path.
  421. */
  422. static void set_backend_state(struct backend_info *be,
  423. enum xenbus_state state)
  424. {
  425. while (be->state != state) {
  426. switch (be->state) {
  427. case XenbusStateClosed:
  428. switch (state) {
  429. case XenbusStateInitWait:
  430. case XenbusStateConnected:
  431. pr_info("%s: prepare for reconnect\n",
  432. be->dev->nodename);
  433. backend_switch_state(be, XenbusStateInitWait);
  434. break;
  435. case XenbusStateClosing:
  436. backend_switch_state(be, XenbusStateClosing);
  437. break;
  438. default:
  439. BUG();
  440. }
  441. break;
  442. case XenbusStateInitWait:
  443. switch (state) {
  444. case XenbusStateConnected:
  445. backend_connect(be);
  446. backend_switch_state(be, XenbusStateConnected);
  447. break;
  448. case XenbusStateClosing:
  449. case XenbusStateClosed:
  450. backend_switch_state(be, XenbusStateClosing);
  451. break;
  452. default:
  453. BUG();
  454. }
  455. break;
  456. case XenbusStateConnected:
  457. switch (state) {
  458. case XenbusStateInitWait:
  459. case XenbusStateClosing:
  460. case XenbusStateClosed:
  461. backend_disconnect(be);
  462. backend_switch_state(be, XenbusStateClosing);
  463. break;
  464. default:
  465. BUG();
  466. }
  467. break;
  468. case XenbusStateClosing:
  469. switch (state) {
  470. case XenbusStateInitWait:
  471. case XenbusStateConnected:
  472. case XenbusStateClosed:
  473. backend_switch_state(be, XenbusStateClosed);
  474. break;
  475. default:
  476. BUG();
  477. }
  478. break;
  479. default:
  480. BUG();
  481. }
  482. }
  483. }
  484. /**
  485. * Callback received when the frontend's state changes.
  486. */
  487. static void frontend_changed(struct xenbus_device *dev,
  488. enum xenbus_state frontend_state)
  489. {
  490. struct backend_info *be = dev_get_drvdata(&dev->dev);
  491. pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
  492. be->frontend_state = frontend_state;
  493. switch (frontend_state) {
  494. case XenbusStateInitialising:
  495. set_backend_state(be, XenbusStateInitWait);
  496. break;
  497. case XenbusStateInitialised:
  498. break;
  499. case XenbusStateConnected:
  500. set_backend_state(be, XenbusStateConnected);
  501. break;
  502. case XenbusStateClosing:
  503. set_backend_state(be, XenbusStateClosing);
  504. break;
  505. case XenbusStateClosed:
  506. set_backend_state(be, XenbusStateClosed);
  507. if (xenbus_dev_is_online(dev))
  508. break;
  509. /* fall through if not online */
  510. case XenbusStateUnknown:
  511. set_backend_state(be, XenbusStateClosed);
  512. device_unregister(&dev->dev);
  513. break;
  514. default:
  515. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  516. frontend_state);
  517. break;
  518. }
  519. }
  520. static void xen_net_read_rate(struct xenbus_device *dev,
  521. unsigned long *bytes, unsigned long *usec)
  522. {
  523. char *s, *e;
  524. unsigned long b, u;
  525. char *ratestr;
  526. /* Default to unlimited bandwidth. */
  527. *bytes = ~0UL;
  528. *usec = 0;
  529. ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
  530. if (IS_ERR(ratestr))
  531. return;
  532. s = ratestr;
  533. b = simple_strtoul(s, &e, 10);
  534. if ((s == e) || (*e != ','))
  535. goto fail;
  536. s = e + 1;
  537. u = simple_strtoul(s, &e, 10);
  538. if ((s == e) || (*e != '\0'))
  539. goto fail;
  540. *bytes = b;
  541. *usec = u;
  542. kfree(ratestr);
  543. return;
  544. fail:
  545. pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
  546. kfree(ratestr);
  547. }
  548. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  549. {
  550. char *s, *e, *macstr;
  551. int i;
  552. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  553. if (IS_ERR(macstr))
  554. return PTR_ERR(macstr);
  555. for (i = 0; i < ETH_ALEN; i++) {
  556. mac[i] = simple_strtoul(s, &e, 16);
  557. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  558. kfree(macstr);
  559. return -ENOENT;
  560. }
  561. s = e+1;
  562. }
  563. kfree(macstr);
  564. return 0;
  565. }
  566. static void xen_net_rate_changed(struct xenbus_watch *watch,
  567. const char **vec, unsigned int len)
  568. {
  569. struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
  570. struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
  571. unsigned long credit_bytes;
  572. unsigned long credit_usec;
  573. unsigned int queue_index;
  574. xen_net_read_rate(dev, &credit_bytes, &credit_usec);
  575. for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
  576. struct xenvif_queue *queue = &vif->queues[queue_index];
  577. queue->credit_bytes = credit_bytes;
  578. queue->credit_usec = credit_usec;
  579. if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
  580. queue->remaining_credit > queue->credit_bytes) {
  581. queue->remaining_credit = queue->credit_bytes;
  582. }
  583. }
  584. }
  585. static int xen_register_watchers(struct xenbus_device *dev, struct xenvif *vif)
  586. {
  587. int err = 0;
  588. char *node;
  589. unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
  590. if (vif->credit_watch.node)
  591. return -EADDRINUSE;
  592. node = kmalloc(maxlen, GFP_KERNEL);
  593. if (!node)
  594. return -ENOMEM;
  595. snprintf(node, maxlen, "%s/rate", dev->nodename);
  596. vif->credit_watch.node = node;
  597. vif->credit_watch.callback = xen_net_rate_changed;
  598. err = register_xenbus_watch(&vif->credit_watch);
  599. if (err) {
  600. pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
  601. kfree(node);
  602. vif->credit_watch.node = NULL;
  603. vif->credit_watch.callback = NULL;
  604. }
  605. return err;
  606. }
  607. static void xen_unregister_watchers(struct xenvif *vif)
  608. {
  609. if (vif->credit_watch.node) {
  610. unregister_xenbus_watch(&vif->credit_watch);
  611. kfree(vif->credit_watch.node);
  612. vif->credit_watch.node = NULL;
  613. }
  614. }
  615. static void unregister_hotplug_status_watch(struct backend_info *be)
  616. {
  617. if (be->have_hotplug_status_watch) {
  618. unregister_xenbus_watch(&be->hotplug_status_watch);
  619. kfree(be->hotplug_status_watch.node);
  620. }
  621. be->have_hotplug_status_watch = 0;
  622. }
  623. static void hotplug_status_changed(struct xenbus_watch *watch,
  624. const char **vec,
  625. unsigned int vec_size)
  626. {
  627. struct backend_info *be = container_of(watch,
  628. struct backend_info,
  629. hotplug_status_watch);
  630. char *str;
  631. unsigned int len;
  632. str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
  633. if (IS_ERR(str))
  634. return;
  635. if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
  636. /* Complete any pending state change */
  637. xenbus_switch_state(be->dev, be->state);
  638. /* Not interested in this watch anymore. */
  639. unregister_hotplug_status_watch(be);
  640. }
  641. kfree(str);
  642. }
  643. static void connect(struct backend_info *be)
  644. {
  645. int err;
  646. struct xenbus_device *dev = be->dev;
  647. unsigned long credit_bytes, credit_usec;
  648. unsigned int queue_index;
  649. unsigned int requested_num_queues;
  650. struct xenvif_queue *queue;
  651. /* Check whether the frontend requested multiple queues
  652. * and read the number requested.
  653. */
  654. err = xenbus_scanf(XBT_NIL, dev->otherend,
  655. "multi-queue-num-queues",
  656. "%u", &requested_num_queues);
  657. if (err < 0) {
  658. requested_num_queues = 1; /* Fall back to single queue */
  659. } else if (requested_num_queues > xenvif_max_queues) {
  660. /* buggy or malicious guest */
  661. xenbus_dev_fatal(dev, err,
  662. "guest requested %u queues, exceeding the maximum of %u.",
  663. requested_num_queues, xenvif_max_queues);
  664. return;
  665. }
  666. err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
  667. if (err) {
  668. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  669. return;
  670. }
  671. xen_net_read_rate(dev, &credit_bytes, &credit_usec);
  672. xen_unregister_watchers(be->vif);
  673. xen_register_watchers(dev, be->vif);
  674. read_xenbus_vif_flags(be);
  675. /* Use the number of queues requested by the frontend */
  676. be->vif->queues = vzalloc(requested_num_queues *
  677. sizeof(struct xenvif_queue));
  678. if (!be->vif->queues) {
  679. xenbus_dev_fatal(dev, -ENOMEM,
  680. "allocating queues");
  681. return;
  682. }
  683. be->vif->num_queues = requested_num_queues;
  684. be->vif->stalled_queues = requested_num_queues;
  685. for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
  686. queue = &be->vif->queues[queue_index];
  687. queue->vif = be->vif;
  688. queue->id = queue_index;
  689. snprintf(queue->name, sizeof(queue->name), "%s-q%u",
  690. be->vif->dev->name, queue->id);
  691. err = xenvif_init_queue(queue);
  692. if (err) {
  693. /* xenvif_init_queue() cleans up after itself on
  694. * failure, but we need to clean up any previously
  695. * initialised queues. Set num_queues to i so that
  696. * earlier queues can be destroyed using the regular
  697. * disconnect logic.
  698. */
  699. be->vif->num_queues = queue_index;
  700. goto err;
  701. }
  702. queue->credit_bytes = credit_bytes;
  703. queue->remaining_credit = credit_bytes;
  704. queue->credit_usec = credit_usec;
  705. err = connect_rings(be, queue);
  706. if (err) {
  707. /* connect_rings() cleans up after itself on failure,
  708. * but we need to clean up after xenvif_init_queue() here,
  709. * and also clean up any previously initialised queues.
  710. */
  711. xenvif_deinit_queue(queue);
  712. be->vif->num_queues = queue_index;
  713. goto err;
  714. }
  715. }
  716. #ifdef CONFIG_DEBUG_FS
  717. xenvif_debugfs_addif(be->vif);
  718. #endif /* CONFIG_DEBUG_FS */
  719. /* Initialisation completed, tell core driver the number of
  720. * active queues.
  721. */
  722. rtnl_lock();
  723. netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
  724. netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
  725. rtnl_unlock();
  726. xenvif_carrier_on(be->vif);
  727. unregister_hotplug_status_watch(be);
  728. err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
  729. hotplug_status_changed,
  730. "%s/%s", dev->nodename, "hotplug-status");
  731. if (!err)
  732. be->have_hotplug_status_watch = 1;
  733. netif_tx_wake_all_queues(be->vif->dev);
  734. return;
  735. err:
  736. if (be->vif->num_queues > 0)
  737. xenvif_disconnect(be->vif); /* Clean up existing queues */
  738. vfree(be->vif->queues);
  739. be->vif->queues = NULL;
  740. be->vif->num_queues = 0;
  741. return;
  742. }
  743. static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
  744. {
  745. struct xenbus_device *dev = be->dev;
  746. unsigned int num_queues = queue->vif->num_queues;
  747. unsigned long tx_ring_ref, rx_ring_ref;
  748. unsigned int tx_evtchn, rx_evtchn;
  749. int err;
  750. char *xspath;
  751. size_t xspathsize;
  752. const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
  753. /* If the frontend requested 1 queue, or we have fallen back
  754. * to single queue due to lack of frontend support for multi-
  755. * queue, expect the remaining XenStore keys in the toplevel
  756. * directory. Otherwise, expect them in a subdirectory called
  757. * queue-N.
  758. */
  759. if (num_queues == 1) {
  760. xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
  761. if (!xspath) {
  762. xenbus_dev_fatal(dev, -ENOMEM,
  763. "reading ring references");
  764. return -ENOMEM;
  765. }
  766. strcpy(xspath, dev->otherend);
  767. } else {
  768. xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
  769. xspath = kzalloc(xspathsize, GFP_KERNEL);
  770. if (!xspath) {
  771. xenbus_dev_fatal(dev, -ENOMEM,
  772. "reading ring references");
  773. return -ENOMEM;
  774. }
  775. snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
  776. queue->id);
  777. }
  778. err = xenbus_gather(XBT_NIL, xspath,
  779. "tx-ring-ref", "%lu", &tx_ring_ref,
  780. "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
  781. if (err) {
  782. xenbus_dev_fatal(dev, err,
  783. "reading %s/ring-ref",
  784. xspath);
  785. goto err;
  786. }
  787. /* Try split event channels first, then single event channel. */
  788. err = xenbus_gather(XBT_NIL, xspath,
  789. "event-channel-tx", "%u", &tx_evtchn,
  790. "event-channel-rx", "%u", &rx_evtchn, NULL);
  791. if (err < 0) {
  792. err = xenbus_scanf(XBT_NIL, xspath,
  793. "event-channel", "%u", &tx_evtchn);
  794. if (err < 0) {
  795. xenbus_dev_fatal(dev, err,
  796. "reading %s/event-channel(-tx/rx)",
  797. xspath);
  798. goto err;
  799. }
  800. rx_evtchn = tx_evtchn;
  801. }
  802. /* Map the shared frame, irq etc. */
  803. err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
  804. tx_evtchn, rx_evtchn);
  805. if (err) {
  806. xenbus_dev_fatal(dev, err,
  807. "mapping shared-frames %lu/%lu port tx %u rx %u",
  808. tx_ring_ref, rx_ring_ref,
  809. tx_evtchn, rx_evtchn);
  810. goto err;
  811. }
  812. err = 0;
  813. err: /* Regular return falls through with err == 0 */
  814. kfree(xspath);
  815. return err;
  816. }
  817. static int read_xenbus_vif_flags(struct backend_info *be)
  818. {
  819. struct xenvif *vif = be->vif;
  820. struct xenbus_device *dev = be->dev;
  821. unsigned int rx_copy;
  822. int err, val;
  823. err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
  824. &rx_copy);
  825. if (err == -ENOENT) {
  826. err = 0;
  827. rx_copy = 0;
  828. }
  829. if (err < 0) {
  830. xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
  831. dev->otherend);
  832. return err;
  833. }
  834. if (!rx_copy)
  835. return -EOPNOTSUPP;
  836. if (xenbus_scanf(XBT_NIL, dev->otherend,
  837. "feature-rx-notify", "%d", &val) < 0)
  838. val = 0;
  839. if (!val) {
  840. /* - Reduce drain timeout to poll more frequently for
  841. * Rx requests.
  842. * - Disable Rx stall detection.
  843. */
  844. be->vif->drain_timeout = msecs_to_jiffies(30);
  845. be->vif->stall_timeout = 0;
  846. }
  847. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
  848. "%d", &val) < 0)
  849. val = 0;
  850. vif->can_sg = !!val;
  851. vif->gso_mask = 0;
  852. vif->gso_prefix_mask = 0;
  853. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
  854. "%d", &val) < 0)
  855. val = 0;
  856. if (val)
  857. vif->gso_mask |= GSO_BIT(TCPV4);
  858. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
  859. "%d", &val) < 0)
  860. val = 0;
  861. if (val)
  862. vif->gso_prefix_mask |= GSO_BIT(TCPV4);
  863. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
  864. "%d", &val) < 0)
  865. val = 0;
  866. if (val)
  867. vif->gso_mask |= GSO_BIT(TCPV6);
  868. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
  869. "%d", &val) < 0)
  870. val = 0;
  871. if (val)
  872. vif->gso_prefix_mask |= GSO_BIT(TCPV6);
  873. if (vif->gso_mask & vif->gso_prefix_mask) {
  874. xenbus_dev_fatal(dev, err,
  875. "%s: gso and gso prefix flags are not "
  876. "mutually exclusive",
  877. dev->otherend);
  878. return -EOPNOTSUPP;
  879. }
  880. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
  881. "%d", &val) < 0)
  882. val = 0;
  883. vif->ip_csum = !val;
  884. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
  885. "%d", &val) < 0)
  886. val = 0;
  887. vif->ipv6_csum = !!val;
  888. if (xenbus_scanf(XBT_NIL, dev->otherend, "request-multicast-control",
  889. "%d", &val) < 0)
  890. val = 0;
  891. vif->multicast_control = !!val;
  892. return 0;
  893. }
  894. static const struct xenbus_device_id netback_ids[] = {
  895. { "vif" },
  896. { "" }
  897. };
  898. static struct xenbus_driver netback_driver = {
  899. .ids = netback_ids,
  900. .probe = netback_probe,
  901. .remove = netback_remove,
  902. .uevent = netback_uevent,
  903. .otherend_changed = frontend_changed,
  904. };
  905. int xenvif_xenbus_init(void)
  906. {
  907. return xenbus_register_backend(&netback_driver);
  908. }
  909. void xenvif_xenbus_fini(void)
  910. {
  911. return xenbus_unregister_driver(&netback_driver);
  912. }