xenbus.c 30 KB

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