xenbus.c 29 KB

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