interface.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Network-device interface management.
  3. *
  4. * Copyright (c) 2004-2005, Keir Fraser
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation; or, when distributed
  9. * separately from the Linux kernel or incorporated into other
  10. * software packages, subject to the following license:
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this source file (the "Software"), to deal in the Software without
  14. * restriction, including without limitation the rights to use, copy, modify,
  15. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  16. * and to permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  28. * IN THE SOFTWARE.
  29. */
  30. #include "common.h"
  31. #include <linux/kthread.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/if_vlan.h>
  35. #include <linux/vmalloc.h>
  36. #include <xen/events.h>
  37. #include <asm/xen/hypercall.h>
  38. #include <xen/balloon.h>
  39. #define XENVIF_QUEUE_LENGTH 32
  40. #define XENVIF_NAPI_WEIGHT 64
  41. static inline void xenvif_stop_queue(struct xenvif_queue *queue)
  42. {
  43. struct net_device *dev = queue->vif->dev;
  44. if (!queue->vif->can_queue)
  45. return;
  46. netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
  47. }
  48. int xenvif_schedulable(struct xenvif *vif)
  49. {
  50. return netif_running(vif->dev) &&
  51. test_bit(VIF_STATUS_CONNECTED, &vif->status);
  52. }
  53. static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
  54. {
  55. struct xenvif_queue *queue = dev_id;
  56. if (RING_HAS_UNCONSUMED_REQUESTS(&queue->tx))
  57. napi_schedule(&queue->napi);
  58. return IRQ_HANDLED;
  59. }
  60. int xenvif_poll(struct napi_struct *napi, int budget)
  61. {
  62. struct xenvif_queue *queue =
  63. container_of(napi, struct xenvif_queue, napi);
  64. int work_done;
  65. /* This vif is rogue, we pretend we've there is nothing to do
  66. * for this vif to deschedule it from NAPI. But this interface
  67. * will be turned off in thread context later.
  68. */
  69. if (unlikely(queue->vif->disabled)) {
  70. napi_complete(napi);
  71. return 0;
  72. }
  73. work_done = xenvif_tx_action(queue, budget);
  74. if (work_done < budget) {
  75. napi_complete(napi);
  76. xenvif_napi_schedule_or_enable_events(queue);
  77. }
  78. return work_done;
  79. }
  80. static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
  81. {
  82. struct xenvif_queue *queue = dev_id;
  83. xenvif_kick_thread(queue);
  84. return IRQ_HANDLED;
  85. }
  86. irqreturn_t xenvif_interrupt(int irq, void *dev_id)
  87. {
  88. xenvif_tx_interrupt(irq, dev_id);
  89. xenvif_rx_interrupt(irq, dev_id);
  90. return IRQ_HANDLED;
  91. }
  92. int xenvif_queue_stopped(struct xenvif_queue *queue)
  93. {
  94. struct net_device *dev = queue->vif->dev;
  95. unsigned int id = queue->id;
  96. return netif_tx_queue_stopped(netdev_get_tx_queue(dev, id));
  97. }
  98. void xenvif_wake_queue(struct xenvif_queue *queue)
  99. {
  100. struct net_device *dev = queue->vif->dev;
  101. unsigned int id = queue->id;
  102. netif_tx_wake_queue(netdev_get_tx_queue(dev, id));
  103. }
  104. /* Callback to wake the queue and drain it on timeout */
  105. static void xenvif_wake_queue_callback(unsigned long data)
  106. {
  107. struct xenvif_queue *queue = (struct xenvif_queue *)data;
  108. if (xenvif_queue_stopped(queue)) {
  109. netdev_err(queue->vif->dev, "draining TX queue\n");
  110. queue->rx_queue_purge = true;
  111. xenvif_kick_thread(queue);
  112. xenvif_wake_queue(queue);
  113. }
  114. }
  115. static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  116. {
  117. struct xenvif *vif = netdev_priv(dev);
  118. struct xenvif_queue *queue = NULL;
  119. unsigned int num_queues = vif->num_queues;
  120. u16 index;
  121. int min_slots_needed;
  122. BUG_ON(skb->dev != dev);
  123. /* Drop the packet if queues are not set up */
  124. if (num_queues < 1)
  125. goto drop;
  126. /* Obtain the queue to be used to transmit this packet */
  127. index = skb_get_queue_mapping(skb);
  128. if (index >= num_queues) {
  129. pr_warn_ratelimited("Invalid queue %hu for packet on interface %s\n.",
  130. index, vif->dev->name);
  131. index %= num_queues;
  132. }
  133. queue = &vif->queues[index];
  134. /* Drop the packet if queue is not ready */
  135. if (queue->task == NULL ||
  136. queue->dealloc_task == NULL ||
  137. !xenvif_schedulable(vif))
  138. goto drop;
  139. /* At best we'll need one slot for the header and one for each
  140. * frag.
  141. */
  142. min_slots_needed = 1 + skb_shinfo(skb)->nr_frags;
  143. /* If the skb is GSO then we'll also need an extra slot for the
  144. * metadata.
  145. */
  146. if (skb_is_gso(skb))
  147. min_slots_needed++;
  148. /* If the skb can't possibly fit in the remaining slots
  149. * then turn off the queue to give the ring a chance to
  150. * drain.
  151. */
  152. if (!xenvif_rx_ring_slots_available(queue, min_slots_needed)) {
  153. queue->wake_queue.function = xenvif_wake_queue_callback;
  154. queue->wake_queue.data = (unsigned long)queue;
  155. xenvif_stop_queue(queue);
  156. mod_timer(&queue->wake_queue,
  157. jiffies + rx_drain_timeout_jiffies);
  158. }
  159. skb_queue_tail(&queue->rx_queue, skb);
  160. xenvif_kick_thread(queue);
  161. return NETDEV_TX_OK;
  162. drop:
  163. vif->dev->stats.tx_dropped++;
  164. dev_kfree_skb(skb);
  165. return NETDEV_TX_OK;
  166. }
  167. static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
  168. {
  169. struct xenvif *vif = netdev_priv(dev);
  170. struct xenvif_queue *queue = NULL;
  171. unsigned int num_queues = vif->num_queues;
  172. unsigned long rx_bytes = 0;
  173. unsigned long rx_packets = 0;
  174. unsigned long tx_bytes = 0;
  175. unsigned long tx_packets = 0;
  176. unsigned int index;
  177. if (vif->queues == NULL)
  178. goto out;
  179. /* Aggregate tx and rx stats from each queue */
  180. for (index = 0; index < num_queues; ++index) {
  181. queue = &vif->queues[index];
  182. rx_bytes += queue->stats.rx_bytes;
  183. rx_packets += queue->stats.rx_packets;
  184. tx_bytes += queue->stats.tx_bytes;
  185. tx_packets += queue->stats.tx_packets;
  186. }
  187. out:
  188. vif->dev->stats.rx_bytes = rx_bytes;
  189. vif->dev->stats.rx_packets = rx_packets;
  190. vif->dev->stats.tx_bytes = tx_bytes;
  191. vif->dev->stats.tx_packets = tx_packets;
  192. return &vif->dev->stats;
  193. }
  194. static void xenvif_up(struct xenvif *vif)
  195. {
  196. struct xenvif_queue *queue = NULL;
  197. unsigned int num_queues = vif->num_queues;
  198. unsigned int queue_index;
  199. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  200. queue = &vif->queues[queue_index];
  201. napi_enable(&queue->napi);
  202. enable_irq(queue->tx_irq);
  203. if (queue->tx_irq != queue->rx_irq)
  204. enable_irq(queue->rx_irq);
  205. xenvif_napi_schedule_or_enable_events(queue);
  206. }
  207. }
  208. static void xenvif_down(struct xenvif *vif)
  209. {
  210. struct xenvif_queue *queue = NULL;
  211. unsigned int num_queues = vif->num_queues;
  212. unsigned int queue_index;
  213. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  214. queue = &vif->queues[queue_index];
  215. napi_disable(&queue->napi);
  216. disable_irq(queue->tx_irq);
  217. if (queue->tx_irq != queue->rx_irq)
  218. disable_irq(queue->rx_irq);
  219. del_timer_sync(&queue->credit_timeout);
  220. }
  221. }
  222. static int xenvif_open(struct net_device *dev)
  223. {
  224. struct xenvif *vif = netdev_priv(dev);
  225. if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
  226. xenvif_up(vif);
  227. netif_tx_start_all_queues(dev);
  228. return 0;
  229. }
  230. static int xenvif_close(struct net_device *dev)
  231. {
  232. struct xenvif *vif = netdev_priv(dev);
  233. if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
  234. xenvif_down(vif);
  235. netif_tx_stop_all_queues(dev);
  236. return 0;
  237. }
  238. static int xenvif_change_mtu(struct net_device *dev, int mtu)
  239. {
  240. struct xenvif *vif = netdev_priv(dev);
  241. int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
  242. if (mtu > max)
  243. return -EINVAL;
  244. dev->mtu = mtu;
  245. return 0;
  246. }
  247. static netdev_features_t xenvif_fix_features(struct net_device *dev,
  248. netdev_features_t features)
  249. {
  250. struct xenvif *vif = netdev_priv(dev);
  251. if (!vif->can_sg)
  252. features &= ~NETIF_F_SG;
  253. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4))
  254. features &= ~NETIF_F_TSO;
  255. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6))
  256. features &= ~NETIF_F_TSO6;
  257. if (!vif->ip_csum)
  258. features &= ~NETIF_F_IP_CSUM;
  259. if (!vif->ipv6_csum)
  260. features &= ~NETIF_F_IPV6_CSUM;
  261. return features;
  262. }
  263. static const struct xenvif_stat {
  264. char name[ETH_GSTRING_LEN];
  265. u16 offset;
  266. } xenvif_stats[] = {
  267. {
  268. "rx_gso_checksum_fixup",
  269. offsetof(struct xenvif_stats, rx_gso_checksum_fixup)
  270. },
  271. /* If (sent != success + fail), there are probably packets never
  272. * freed up properly!
  273. */
  274. {
  275. "tx_zerocopy_sent",
  276. offsetof(struct xenvif_stats, tx_zerocopy_sent),
  277. },
  278. {
  279. "tx_zerocopy_success",
  280. offsetof(struct xenvif_stats, tx_zerocopy_success),
  281. },
  282. {
  283. "tx_zerocopy_fail",
  284. offsetof(struct xenvif_stats, tx_zerocopy_fail)
  285. },
  286. /* Number of packets exceeding MAX_SKB_FRAG slots. You should use
  287. * a guest with the same MAX_SKB_FRAG
  288. */
  289. {
  290. "tx_frag_overflow",
  291. offsetof(struct xenvif_stats, tx_frag_overflow)
  292. },
  293. };
  294. static int xenvif_get_sset_count(struct net_device *dev, int string_set)
  295. {
  296. switch (string_set) {
  297. case ETH_SS_STATS:
  298. return ARRAY_SIZE(xenvif_stats);
  299. default:
  300. return -EINVAL;
  301. }
  302. }
  303. static void xenvif_get_ethtool_stats(struct net_device *dev,
  304. struct ethtool_stats *stats, u64 * data)
  305. {
  306. struct xenvif *vif = netdev_priv(dev);
  307. unsigned int num_queues = vif->num_queues;
  308. int i;
  309. unsigned int queue_index;
  310. struct xenvif_stats *vif_stats;
  311. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
  312. unsigned long accum = 0;
  313. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  314. vif_stats = &vif->queues[queue_index].stats;
  315. accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
  316. }
  317. data[i] = accum;
  318. }
  319. }
  320. static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  321. {
  322. int i;
  323. switch (stringset) {
  324. case ETH_SS_STATS:
  325. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
  326. memcpy(data + i * ETH_GSTRING_LEN,
  327. xenvif_stats[i].name, ETH_GSTRING_LEN);
  328. break;
  329. }
  330. }
  331. static const struct ethtool_ops xenvif_ethtool_ops = {
  332. .get_link = ethtool_op_get_link,
  333. .get_sset_count = xenvif_get_sset_count,
  334. .get_ethtool_stats = xenvif_get_ethtool_stats,
  335. .get_strings = xenvif_get_strings,
  336. };
  337. static const struct net_device_ops xenvif_netdev_ops = {
  338. .ndo_start_xmit = xenvif_start_xmit,
  339. .ndo_get_stats = xenvif_get_stats,
  340. .ndo_open = xenvif_open,
  341. .ndo_stop = xenvif_close,
  342. .ndo_change_mtu = xenvif_change_mtu,
  343. .ndo_fix_features = xenvif_fix_features,
  344. .ndo_set_mac_address = eth_mac_addr,
  345. .ndo_validate_addr = eth_validate_addr,
  346. };
  347. struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
  348. unsigned int handle)
  349. {
  350. int err;
  351. struct net_device *dev;
  352. struct xenvif *vif;
  353. char name[IFNAMSIZ] = {};
  354. snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
  355. /* Allocate a netdev with the max. supported number of queues.
  356. * When the guest selects the desired number, it will be updated
  357. * via netif_set_real_num_*_queues().
  358. */
  359. dev = alloc_netdev_mq(sizeof(struct xenvif), name, NET_NAME_UNKNOWN,
  360. ether_setup, xenvif_max_queues);
  361. if (dev == NULL) {
  362. pr_warn("Could not allocate netdev for %s\n", name);
  363. return ERR_PTR(-ENOMEM);
  364. }
  365. SET_NETDEV_DEV(dev, parent);
  366. vif = netdev_priv(dev);
  367. vif->domid = domid;
  368. vif->handle = handle;
  369. vif->can_sg = 1;
  370. vif->ip_csum = 1;
  371. vif->dev = dev;
  372. vif->disabled = false;
  373. /* Start out with no queues. */
  374. vif->queues = NULL;
  375. vif->num_queues = 0;
  376. dev->netdev_ops = &xenvif_netdev_ops;
  377. dev->hw_features = NETIF_F_SG |
  378. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  379. NETIF_F_TSO | NETIF_F_TSO6;
  380. dev->features = dev->hw_features | NETIF_F_RXCSUM;
  381. dev->ethtool_ops = &xenvif_ethtool_ops;
  382. dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
  383. /*
  384. * Initialise a dummy MAC address. We choose the numerically
  385. * largest non-broadcast address to prevent the address getting
  386. * stolen by an Ethernet bridge for STP purposes.
  387. * (FE:FF:FF:FF:FF:FF)
  388. */
  389. memset(dev->dev_addr, 0xFF, ETH_ALEN);
  390. dev->dev_addr[0] &= ~0x01;
  391. netif_carrier_off(dev);
  392. err = register_netdev(dev);
  393. if (err) {
  394. netdev_warn(dev, "Could not register device: err=%d\n", err);
  395. free_netdev(dev);
  396. return ERR_PTR(err);
  397. }
  398. netdev_dbg(dev, "Successfully created xenvif\n");
  399. __module_get(THIS_MODULE);
  400. return vif;
  401. }
  402. int xenvif_init_queue(struct xenvif_queue *queue)
  403. {
  404. int err, i;
  405. queue->credit_bytes = queue->remaining_credit = ~0UL;
  406. queue->credit_usec = 0UL;
  407. init_timer(&queue->credit_timeout);
  408. queue->credit_window_start = get_jiffies_64();
  409. skb_queue_head_init(&queue->rx_queue);
  410. skb_queue_head_init(&queue->tx_queue);
  411. queue->pending_cons = 0;
  412. queue->pending_prod = MAX_PENDING_REQS;
  413. for (i = 0; i < MAX_PENDING_REQS; ++i)
  414. queue->pending_ring[i] = i;
  415. spin_lock_init(&queue->callback_lock);
  416. spin_lock_init(&queue->response_lock);
  417. /* If ballooning is disabled, this will consume real memory, so you
  418. * better enable it. The long term solution would be to use just a
  419. * bunch of valid page descriptors, without dependency on ballooning
  420. */
  421. err = alloc_xenballooned_pages(MAX_PENDING_REQS,
  422. queue->mmap_pages,
  423. false);
  424. if (err) {
  425. netdev_err(queue->vif->dev, "Could not reserve mmap_pages\n");
  426. return -ENOMEM;
  427. }
  428. for (i = 0; i < MAX_PENDING_REQS; i++) {
  429. queue->pending_tx_info[i].callback_struct = (struct ubuf_info)
  430. { .callback = xenvif_zerocopy_callback,
  431. .ctx = NULL,
  432. .desc = i };
  433. queue->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
  434. }
  435. init_timer(&queue->wake_queue);
  436. netif_napi_add(queue->vif->dev, &queue->napi, xenvif_poll,
  437. XENVIF_NAPI_WEIGHT);
  438. return 0;
  439. }
  440. void xenvif_carrier_on(struct xenvif *vif)
  441. {
  442. rtnl_lock();
  443. if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
  444. dev_set_mtu(vif->dev, ETH_DATA_LEN);
  445. netdev_update_features(vif->dev);
  446. set_bit(VIF_STATUS_CONNECTED, &vif->status);
  447. netif_carrier_on(vif->dev);
  448. if (netif_running(vif->dev))
  449. xenvif_up(vif);
  450. rtnl_unlock();
  451. }
  452. int xenvif_connect(struct xenvif_queue *queue, unsigned long tx_ring_ref,
  453. unsigned long rx_ring_ref, unsigned int tx_evtchn,
  454. unsigned int rx_evtchn)
  455. {
  456. struct task_struct *task;
  457. int err = -ENOMEM;
  458. BUG_ON(queue->tx_irq);
  459. BUG_ON(queue->task);
  460. BUG_ON(queue->dealloc_task);
  461. err = xenvif_map_frontend_rings(queue, tx_ring_ref, rx_ring_ref);
  462. if (err < 0)
  463. goto err;
  464. init_waitqueue_head(&queue->wq);
  465. init_waitqueue_head(&queue->dealloc_wq);
  466. if (tx_evtchn == rx_evtchn) {
  467. /* feature-split-event-channels == 0 */
  468. err = bind_interdomain_evtchn_to_irqhandler(
  469. queue->vif->domid, tx_evtchn, xenvif_interrupt, 0,
  470. queue->name, queue);
  471. if (err < 0)
  472. goto err_unmap;
  473. queue->tx_irq = queue->rx_irq = err;
  474. disable_irq(queue->tx_irq);
  475. } else {
  476. /* feature-split-event-channels == 1 */
  477. snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
  478. "%s-tx", queue->name);
  479. err = bind_interdomain_evtchn_to_irqhandler(
  480. queue->vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
  481. queue->tx_irq_name, queue);
  482. if (err < 0)
  483. goto err_unmap;
  484. queue->tx_irq = err;
  485. disable_irq(queue->tx_irq);
  486. snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
  487. "%s-rx", queue->name);
  488. err = bind_interdomain_evtchn_to_irqhandler(
  489. queue->vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
  490. queue->rx_irq_name, queue);
  491. if (err < 0)
  492. goto err_tx_unbind;
  493. queue->rx_irq = err;
  494. disable_irq(queue->rx_irq);
  495. }
  496. task = kthread_create(xenvif_kthread_guest_rx,
  497. (void *)queue, "%s-guest-rx", queue->name);
  498. if (IS_ERR(task)) {
  499. pr_warn("Could not allocate kthread for %s\n", queue->name);
  500. err = PTR_ERR(task);
  501. goto err_rx_unbind;
  502. }
  503. queue->task = task;
  504. task = kthread_create(xenvif_dealloc_kthread,
  505. (void *)queue, "%s-dealloc", queue->name);
  506. if (IS_ERR(task)) {
  507. pr_warn("Could not allocate kthread for %s\n", queue->name);
  508. err = PTR_ERR(task);
  509. goto err_rx_unbind;
  510. }
  511. queue->dealloc_task = task;
  512. wake_up_process(queue->task);
  513. wake_up_process(queue->dealloc_task);
  514. return 0;
  515. err_rx_unbind:
  516. unbind_from_irqhandler(queue->rx_irq, queue);
  517. queue->rx_irq = 0;
  518. err_tx_unbind:
  519. unbind_from_irqhandler(queue->tx_irq, queue);
  520. queue->tx_irq = 0;
  521. err_unmap:
  522. xenvif_unmap_frontend_rings(queue);
  523. err:
  524. module_put(THIS_MODULE);
  525. return err;
  526. }
  527. void xenvif_carrier_off(struct xenvif *vif)
  528. {
  529. struct net_device *dev = vif->dev;
  530. rtnl_lock();
  531. if (test_and_clear_bit(VIF_STATUS_CONNECTED, &vif->status)) {
  532. netif_carrier_off(dev); /* discard queued packets */
  533. if (netif_running(dev))
  534. xenvif_down(vif);
  535. }
  536. rtnl_unlock();
  537. }
  538. static void xenvif_wait_unmap_timeout(struct xenvif_queue *queue,
  539. unsigned int worst_case_skb_lifetime)
  540. {
  541. int i, unmap_timeout = 0;
  542. for (i = 0; i < MAX_PENDING_REQS; ++i) {
  543. if (queue->grant_tx_handle[i] != NETBACK_INVALID_HANDLE) {
  544. unmap_timeout++;
  545. schedule_timeout(msecs_to_jiffies(1000));
  546. if (unmap_timeout > worst_case_skb_lifetime &&
  547. net_ratelimit())
  548. netdev_err(queue->vif->dev,
  549. "Page still granted! Index: %x\n",
  550. i);
  551. i = -1;
  552. }
  553. }
  554. }
  555. void xenvif_disconnect(struct xenvif *vif)
  556. {
  557. struct xenvif_queue *queue = NULL;
  558. unsigned int num_queues = vif->num_queues;
  559. unsigned int queue_index;
  560. xenvif_carrier_off(vif);
  561. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  562. queue = &vif->queues[queue_index];
  563. if (queue->task) {
  564. del_timer_sync(&queue->wake_queue);
  565. kthread_stop(queue->task);
  566. queue->task = NULL;
  567. }
  568. if (queue->dealloc_task) {
  569. kthread_stop(queue->dealloc_task);
  570. queue->dealloc_task = NULL;
  571. }
  572. if (queue->tx_irq) {
  573. if (queue->tx_irq == queue->rx_irq)
  574. unbind_from_irqhandler(queue->tx_irq, queue);
  575. else {
  576. unbind_from_irqhandler(queue->tx_irq, queue);
  577. unbind_from_irqhandler(queue->rx_irq, queue);
  578. }
  579. queue->tx_irq = 0;
  580. }
  581. xenvif_unmap_frontend_rings(queue);
  582. }
  583. }
  584. /* Reverse the relevant parts of xenvif_init_queue().
  585. * Used for queue teardown from xenvif_free(), and on the
  586. * error handling paths in xenbus.c:connect().
  587. */
  588. void xenvif_deinit_queue(struct xenvif_queue *queue)
  589. {
  590. free_xenballooned_pages(MAX_PENDING_REQS, queue->mmap_pages);
  591. netif_napi_del(&queue->napi);
  592. }
  593. void xenvif_free(struct xenvif *vif)
  594. {
  595. struct xenvif_queue *queue = NULL;
  596. unsigned int num_queues = vif->num_queues;
  597. unsigned int queue_index;
  598. /* Here we want to avoid timeout messages if an skb can be legitimately
  599. * stuck somewhere else. Realistically this could be an another vif's
  600. * internal or QDisc queue. That another vif also has this
  601. * rx_drain_timeout_msecs timeout, but the timer only ditches the
  602. * internal queue. After that, the QDisc queue can put in worst case
  603. * XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS skbs into that another vif's
  604. * internal queue, so we need several rounds of such timeouts until we
  605. * can be sure that no another vif should have skb's from us. We are
  606. * not sending more skb's, so newly stuck packets are not interesting
  607. * for us here.
  608. */
  609. unsigned int worst_case_skb_lifetime = (rx_drain_timeout_msecs/1000) *
  610. DIV_ROUND_UP(XENVIF_QUEUE_LENGTH, (XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS));
  611. unregister_netdev(vif->dev);
  612. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  613. queue = &vif->queues[queue_index];
  614. xenvif_wait_unmap_timeout(queue, worst_case_skb_lifetime);
  615. xenvif_deinit_queue(queue);
  616. }
  617. vfree(vif->queues);
  618. vif->queues = NULL;
  619. vif->num_queues = 0;
  620. free_netdev(vif->dev);
  621. module_put(THIS_MODULE);
  622. }