interface.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. * Also, if a guest doesn't post enough slots to receive data on one of
  69. * its queues, the carrier goes down and NAPI is descheduled here so
  70. * the guest can't send more packets until it's ready to receive.
  71. */
  72. if (unlikely(queue->vif->disabled ||
  73. !netif_carrier_ok(queue->vif->dev))) {
  74. napi_complete(napi);
  75. return 0;
  76. }
  77. work_done = xenvif_tx_action(queue, budget);
  78. if (work_done < budget) {
  79. napi_complete(napi);
  80. xenvif_napi_schedule_or_enable_events(queue);
  81. }
  82. return work_done;
  83. }
  84. static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
  85. {
  86. struct xenvif_queue *queue = dev_id;
  87. struct netdev_queue *net_queue =
  88. netdev_get_tx_queue(queue->vif->dev, queue->id);
  89. /* QUEUE_STATUS_RX_PURGE_EVENT is only set if either QDisc was off OR
  90. * the carrier went down and this queue was previously blocked
  91. */
  92. if (unlikely(netif_tx_queue_stopped(net_queue) ||
  93. (!netif_carrier_ok(queue->vif->dev) &&
  94. test_bit(QUEUE_STATUS_RX_STALLED, &queue->status))))
  95. set_bit(QUEUE_STATUS_RX_PURGE_EVENT, &queue->status);
  96. xenvif_kick_thread(queue);
  97. return IRQ_HANDLED;
  98. }
  99. irqreturn_t xenvif_interrupt(int irq, void *dev_id)
  100. {
  101. xenvif_tx_interrupt(irq, dev_id);
  102. xenvif_rx_interrupt(irq, dev_id);
  103. return IRQ_HANDLED;
  104. }
  105. int xenvif_queue_stopped(struct xenvif_queue *queue)
  106. {
  107. struct net_device *dev = queue->vif->dev;
  108. unsigned int id = queue->id;
  109. return netif_tx_queue_stopped(netdev_get_tx_queue(dev, id));
  110. }
  111. void xenvif_wake_queue(struct xenvif_queue *queue)
  112. {
  113. struct net_device *dev = queue->vif->dev;
  114. unsigned int id = queue->id;
  115. netif_tx_wake_queue(netdev_get_tx_queue(dev, id));
  116. }
  117. /* Callback to wake the queue's thread and turn the carrier off on timeout */
  118. static void xenvif_rx_stalled(unsigned long data)
  119. {
  120. struct xenvif_queue *queue = (struct xenvif_queue *)data;
  121. if (xenvif_queue_stopped(queue)) {
  122. set_bit(QUEUE_STATUS_RX_PURGE_EVENT, &queue->status);
  123. xenvif_kick_thread(queue);
  124. }
  125. }
  126. static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  127. {
  128. struct xenvif *vif = netdev_priv(dev);
  129. struct xenvif_queue *queue = NULL;
  130. unsigned int num_queues = vif->num_queues;
  131. u16 index;
  132. int min_slots_needed;
  133. BUG_ON(skb->dev != dev);
  134. /* Drop the packet if queues are not set up */
  135. if (num_queues < 1)
  136. goto drop;
  137. /* Obtain the queue to be used to transmit this packet */
  138. index = skb_get_queue_mapping(skb);
  139. if (index >= num_queues) {
  140. pr_warn_ratelimited("Invalid queue %hu for packet on interface %s\n.",
  141. index, vif->dev->name);
  142. index %= num_queues;
  143. }
  144. queue = &vif->queues[index];
  145. /* Drop the packet if queue is not ready */
  146. if (queue->task == NULL ||
  147. queue->dealloc_task == NULL ||
  148. !xenvif_schedulable(vif))
  149. goto drop;
  150. /* At best we'll need one slot for the header and one for each
  151. * frag.
  152. */
  153. min_slots_needed = 1 + skb_shinfo(skb)->nr_frags;
  154. /* If the skb is GSO then we'll also need an extra slot for the
  155. * metadata.
  156. */
  157. if (skb_is_gso(skb))
  158. min_slots_needed++;
  159. /* If the skb can't possibly fit in the remaining slots
  160. * then turn off the queue to give the ring a chance to
  161. * drain.
  162. */
  163. if (!xenvif_rx_ring_slots_available(queue, min_slots_needed)) {
  164. queue->rx_stalled.function = xenvif_rx_stalled;
  165. queue->rx_stalled.data = (unsigned long)queue;
  166. xenvif_stop_queue(queue);
  167. mod_timer(&queue->rx_stalled,
  168. jiffies + rx_drain_timeout_jiffies);
  169. }
  170. skb_queue_tail(&queue->rx_queue, skb);
  171. xenvif_kick_thread(queue);
  172. return NETDEV_TX_OK;
  173. drop:
  174. vif->dev->stats.tx_dropped++;
  175. dev_kfree_skb(skb);
  176. return NETDEV_TX_OK;
  177. }
  178. static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
  179. {
  180. struct xenvif *vif = netdev_priv(dev);
  181. struct xenvif_queue *queue = NULL;
  182. unsigned int num_queues = vif->num_queues;
  183. unsigned long rx_bytes = 0;
  184. unsigned long rx_packets = 0;
  185. unsigned long tx_bytes = 0;
  186. unsigned long tx_packets = 0;
  187. unsigned int index;
  188. if (vif->queues == NULL)
  189. goto out;
  190. /* Aggregate tx and rx stats from each queue */
  191. for (index = 0; index < num_queues; ++index) {
  192. queue = &vif->queues[index];
  193. rx_bytes += queue->stats.rx_bytes;
  194. rx_packets += queue->stats.rx_packets;
  195. tx_bytes += queue->stats.tx_bytes;
  196. tx_packets += queue->stats.tx_packets;
  197. }
  198. out:
  199. vif->dev->stats.rx_bytes = rx_bytes;
  200. vif->dev->stats.rx_packets = rx_packets;
  201. vif->dev->stats.tx_bytes = tx_bytes;
  202. vif->dev->stats.tx_packets = tx_packets;
  203. return &vif->dev->stats;
  204. }
  205. static void xenvif_up(struct xenvif *vif)
  206. {
  207. struct xenvif_queue *queue = NULL;
  208. unsigned int num_queues = vif->num_queues;
  209. unsigned int queue_index;
  210. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  211. queue = &vif->queues[queue_index];
  212. napi_enable(&queue->napi);
  213. enable_irq(queue->tx_irq);
  214. if (queue->tx_irq != queue->rx_irq)
  215. enable_irq(queue->rx_irq);
  216. xenvif_napi_schedule_or_enable_events(queue);
  217. }
  218. }
  219. static void xenvif_down(struct xenvif *vif)
  220. {
  221. struct xenvif_queue *queue = NULL;
  222. unsigned int num_queues = vif->num_queues;
  223. unsigned int queue_index;
  224. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  225. queue = &vif->queues[queue_index];
  226. napi_disable(&queue->napi);
  227. disable_irq(queue->tx_irq);
  228. if (queue->tx_irq != queue->rx_irq)
  229. disable_irq(queue->rx_irq);
  230. del_timer_sync(&queue->credit_timeout);
  231. }
  232. }
  233. static int xenvif_open(struct net_device *dev)
  234. {
  235. struct xenvif *vif = netdev_priv(dev);
  236. if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
  237. xenvif_up(vif);
  238. netif_tx_start_all_queues(dev);
  239. return 0;
  240. }
  241. static int xenvif_close(struct net_device *dev)
  242. {
  243. struct xenvif *vif = netdev_priv(dev);
  244. if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
  245. xenvif_down(vif);
  246. netif_tx_stop_all_queues(dev);
  247. return 0;
  248. }
  249. static int xenvif_change_mtu(struct net_device *dev, int mtu)
  250. {
  251. struct xenvif *vif = netdev_priv(dev);
  252. int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
  253. if (mtu > max)
  254. return -EINVAL;
  255. dev->mtu = mtu;
  256. return 0;
  257. }
  258. static netdev_features_t xenvif_fix_features(struct net_device *dev,
  259. netdev_features_t features)
  260. {
  261. struct xenvif *vif = netdev_priv(dev);
  262. if (!vif->can_sg)
  263. features &= ~NETIF_F_SG;
  264. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4))
  265. features &= ~NETIF_F_TSO;
  266. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6))
  267. features &= ~NETIF_F_TSO6;
  268. if (!vif->ip_csum)
  269. features &= ~NETIF_F_IP_CSUM;
  270. if (!vif->ipv6_csum)
  271. features &= ~NETIF_F_IPV6_CSUM;
  272. return features;
  273. }
  274. static const struct xenvif_stat {
  275. char name[ETH_GSTRING_LEN];
  276. u16 offset;
  277. } xenvif_stats[] = {
  278. {
  279. "rx_gso_checksum_fixup",
  280. offsetof(struct xenvif_stats, rx_gso_checksum_fixup)
  281. },
  282. /* If (sent != success + fail), there are probably packets never
  283. * freed up properly!
  284. */
  285. {
  286. "tx_zerocopy_sent",
  287. offsetof(struct xenvif_stats, tx_zerocopy_sent),
  288. },
  289. {
  290. "tx_zerocopy_success",
  291. offsetof(struct xenvif_stats, tx_zerocopy_success),
  292. },
  293. {
  294. "tx_zerocopy_fail",
  295. offsetof(struct xenvif_stats, tx_zerocopy_fail)
  296. },
  297. /* Number of packets exceeding MAX_SKB_FRAG slots. You should use
  298. * a guest with the same MAX_SKB_FRAG
  299. */
  300. {
  301. "tx_frag_overflow",
  302. offsetof(struct xenvif_stats, tx_frag_overflow)
  303. },
  304. };
  305. static int xenvif_get_sset_count(struct net_device *dev, int string_set)
  306. {
  307. switch (string_set) {
  308. case ETH_SS_STATS:
  309. return ARRAY_SIZE(xenvif_stats);
  310. default:
  311. return -EINVAL;
  312. }
  313. }
  314. static void xenvif_get_ethtool_stats(struct net_device *dev,
  315. struct ethtool_stats *stats, u64 * data)
  316. {
  317. struct xenvif *vif = netdev_priv(dev);
  318. unsigned int num_queues = vif->num_queues;
  319. int i;
  320. unsigned int queue_index;
  321. struct xenvif_stats *vif_stats;
  322. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
  323. unsigned long accum = 0;
  324. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  325. vif_stats = &vif->queues[queue_index].stats;
  326. accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
  327. }
  328. data[i] = accum;
  329. }
  330. }
  331. static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  332. {
  333. int i;
  334. switch (stringset) {
  335. case ETH_SS_STATS:
  336. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
  337. memcpy(data + i * ETH_GSTRING_LEN,
  338. xenvif_stats[i].name, ETH_GSTRING_LEN);
  339. break;
  340. }
  341. }
  342. static const struct ethtool_ops xenvif_ethtool_ops = {
  343. .get_link = ethtool_op_get_link,
  344. .get_sset_count = xenvif_get_sset_count,
  345. .get_ethtool_stats = xenvif_get_ethtool_stats,
  346. .get_strings = xenvif_get_strings,
  347. };
  348. static const struct net_device_ops xenvif_netdev_ops = {
  349. .ndo_start_xmit = xenvif_start_xmit,
  350. .ndo_get_stats = xenvif_get_stats,
  351. .ndo_open = xenvif_open,
  352. .ndo_stop = xenvif_close,
  353. .ndo_change_mtu = xenvif_change_mtu,
  354. .ndo_fix_features = xenvif_fix_features,
  355. .ndo_set_mac_address = eth_mac_addr,
  356. .ndo_validate_addr = eth_validate_addr,
  357. };
  358. struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
  359. unsigned int handle)
  360. {
  361. int err;
  362. struct net_device *dev;
  363. struct xenvif *vif;
  364. char name[IFNAMSIZ] = {};
  365. snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
  366. /* Allocate a netdev with the max. supported number of queues.
  367. * When the guest selects the desired number, it will be updated
  368. * via netif_set_real_num_*_queues().
  369. */
  370. dev = alloc_netdev_mq(sizeof(struct xenvif), name, NET_NAME_UNKNOWN,
  371. ether_setup, xenvif_max_queues);
  372. if (dev == NULL) {
  373. pr_warn("Could not allocate netdev for %s\n", name);
  374. return ERR_PTR(-ENOMEM);
  375. }
  376. SET_NETDEV_DEV(dev, parent);
  377. vif = netdev_priv(dev);
  378. vif->domid = domid;
  379. vif->handle = handle;
  380. vif->can_sg = 1;
  381. vif->ip_csum = 1;
  382. vif->dev = dev;
  383. vif->disabled = false;
  384. /* Start out with no queues. */
  385. vif->queues = NULL;
  386. vif->num_queues = 0;
  387. dev->netdev_ops = &xenvif_netdev_ops;
  388. dev->hw_features = NETIF_F_SG |
  389. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  390. NETIF_F_TSO | NETIF_F_TSO6;
  391. dev->features = dev->hw_features | NETIF_F_RXCSUM;
  392. dev->ethtool_ops = &xenvif_ethtool_ops;
  393. dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
  394. /*
  395. * Initialise a dummy MAC address. We choose the numerically
  396. * largest non-broadcast address to prevent the address getting
  397. * stolen by an Ethernet bridge for STP purposes.
  398. * (FE:FF:FF:FF:FF:FF)
  399. */
  400. memset(dev->dev_addr, 0xFF, ETH_ALEN);
  401. dev->dev_addr[0] &= ~0x01;
  402. netif_carrier_off(dev);
  403. err = register_netdev(dev);
  404. if (err) {
  405. netdev_warn(dev, "Could not register device: err=%d\n", err);
  406. free_netdev(dev);
  407. return ERR_PTR(err);
  408. }
  409. netdev_dbg(dev, "Successfully created xenvif\n");
  410. __module_get(THIS_MODULE);
  411. return vif;
  412. }
  413. int xenvif_init_queue(struct xenvif_queue *queue)
  414. {
  415. int err, i;
  416. queue->credit_bytes = queue->remaining_credit = ~0UL;
  417. queue->credit_usec = 0UL;
  418. init_timer(&queue->credit_timeout);
  419. queue->credit_window_start = get_jiffies_64();
  420. skb_queue_head_init(&queue->rx_queue);
  421. skb_queue_head_init(&queue->tx_queue);
  422. queue->pending_cons = 0;
  423. queue->pending_prod = MAX_PENDING_REQS;
  424. for (i = 0; i < MAX_PENDING_REQS; ++i)
  425. queue->pending_ring[i] = i;
  426. spin_lock_init(&queue->callback_lock);
  427. spin_lock_init(&queue->response_lock);
  428. /* If ballooning is disabled, this will consume real memory, so you
  429. * better enable it. The long term solution would be to use just a
  430. * bunch of valid page descriptors, without dependency on ballooning
  431. */
  432. err = alloc_xenballooned_pages(MAX_PENDING_REQS,
  433. queue->mmap_pages,
  434. false);
  435. if (err) {
  436. netdev_err(queue->vif->dev, "Could not reserve mmap_pages\n");
  437. return -ENOMEM;
  438. }
  439. for (i = 0; i < MAX_PENDING_REQS; i++) {
  440. queue->pending_tx_info[i].callback_struct = (struct ubuf_info)
  441. { .callback = xenvif_zerocopy_callback,
  442. .ctx = NULL,
  443. .desc = i };
  444. queue->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
  445. }
  446. init_timer(&queue->rx_stalled);
  447. netif_napi_add(queue->vif->dev, &queue->napi, xenvif_poll,
  448. XENVIF_NAPI_WEIGHT);
  449. return 0;
  450. }
  451. void xenvif_carrier_on(struct xenvif *vif)
  452. {
  453. rtnl_lock();
  454. if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
  455. dev_set_mtu(vif->dev, ETH_DATA_LEN);
  456. netdev_update_features(vif->dev);
  457. set_bit(VIF_STATUS_CONNECTED, &vif->status);
  458. netif_carrier_on(vif->dev);
  459. if (netif_running(vif->dev))
  460. xenvif_up(vif);
  461. rtnl_unlock();
  462. }
  463. int xenvif_connect(struct xenvif_queue *queue, unsigned long tx_ring_ref,
  464. unsigned long rx_ring_ref, unsigned int tx_evtchn,
  465. unsigned int rx_evtchn)
  466. {
  467. struct task_struct *task;
  468. int err = -ENOMEM;
  469. BUG_ON(queue->tx_irq);
  470. BUG_ON(queue->task);
  471. BUG_ON(queue->dealloc_task);
  472. err = xenvif_map_frontend_rings(queue, tx_ring_ref, rx_ring_ref);
  473. if (err < 0)
  474. goto err;
  475. init_waitqueue_head(&queue->wq);
  476. init_waitqueue_head(&queue->dealloc_wq);
  477. if (tx_evtchn == rx_evtchn) {
  478. /* feature-split-event-channels == 0 */
  479. err = bind_interdomain_evtchn_to_irqhandler(
  480. queue->vif->domid, tx_evtchn, xenvif_interrupt, 0,
  481. queue->name, queue);
  482. if (err < 0)
  483. goto err_unmap;
  484. queue->tx_irq = queue->rx_irq = err;
  485. disable_irq(queue->tx_irq);
  486. } else {
  487. /* feature-split-event-channels == 1 */
  488. snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
  489. "%s-tx", queue->name);
  490. err = bind_interdomain_evtchn_to_irqhandler(
  491. queue->vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
  492. queue->tx_irq_name, queue);
  493. if (err < 0)
  494. goto err_unmap;
  495. queue->tx_irq = err;
  496. disable_irq(queue->tx_irq);
  497. snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
  498. "%s-rx", queue->name);
  499. err = bind_interdomain_evtchn_to_irqhandler(
  500. queue->vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
  501. queue->rx_irq_name, queue);
  502. if (err < 0)
  503. goto err_tx_unbind;
  504. queue->rx_irq = err;
  505. disable_irq(queue->rx_irq);
  506. }
  507. task = kthread_create(xenvif_kthread_guest_rx,
  508. (void *)queue, "%s-guest-rx", queue->name);
  509. if (IS_ERR(task)) {
  510. pr_warn("Could not allocate kthread for %s\n", queue->name);
  511. err = PTR_ERR(task);
  512. goto err_rx_unbind;
  513. }
  514. queue->task = task;
  515. task = kthread_create(xenvif_dealloc_kthread,
  516. (void *)queue, "%s-dealloc", queue->name);
  517. if (IS_ERR(task)) {
  518. pr_warn("Could not allocate kthread for %s\n", queue->name);
  519. err = PTR_ERR(task);
  520. goto err_rx_unbind;
  521. }
  522. queue->dealloc_task = task;
  523. wake_up_process(queue->task);
  524. wake_up_process(queue->dealloc_task);
  525. return 0;
  526. err_rx_unbind:
  527. unbind_from_irqhandler(queue->rx_irq, queue);
  528. queue->rx_irq = 0;
  529. err_tx_unbind:
  530. unbind_from_irqhandler(queue->tx_irq, queue);
  531. queue->tx_irq = 0;
  532. err_unmap:
  533. xenvif_unmap_frontend_rings(queue);
  534. err:
  535. module_put(THIS_MODULE);
  536. return err;
  537. }
  538. void xenvif_carrier_off(struct xenvif *vif)
  539. {
  540. struct net_device *dev = vif->dev;
  541. rtnl_lock();
  542. if (test_and_clear_bit(VIF_STATUS_CONNECTED, &vif->status)) {
  543. netif_carrier_off(dev); /* discard queued packets */
  544. if (netif_running(dev))
  545. xenvif_down(vif);
  546. }
  547. rtnl_unlock();
  548. }
  549. static void xenvif_wait_unmap_timeout(struct xenvif_queue *queue,
  550. unsigned int worst_case_skb_lifetime)
  551. {
  552. int i, unmap_timeout = 0;
  553. for (i = 0; i < MAX_PENDING_REQS; ++i) {
  554. if (queue->grant_tx_handle[i] != NETBACK_INVALID_HANDLE) {
  555. unmap_timeout++;
  556. schedule_timeout(msecs_to_jiffies(1000));
  557. if (unmap_timeout > worst_case_skb_lifetime &&
  558. net_ratelimit())
  559. netdev_err(queue->vif->dev,
  560. "Page still granted! Index: %x\n",
  561. i);
  562. i = -1;
  563. }
  564. }
  565. }
  566. void xenvif_disconnect(struct xenvif *vif)
  567. {
  568. struct xenvif_queue *queue = NULL;
  569. unsigned int num_queues = vif->num_queues;
  570. unsigned int queue_index;
  571. xenvif_carrier_off(vif);
  572. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  573. queue = &vif->queues[queue_index];
  574. if (queue->task) {
  575. del_timer_sync(&queue->rx_stalled);
  576. kthread_stop(queue->task);
  577. queue->task = NULL;
  578. }
  579. if (queue->dealloc_task) {
  580. kthread_stop(queue->dealloc_task);
  581. queue->dealloc_task = NULL;
  582. }
  583. if (queue->tx_irq) {
  584. if (queue->tx_irq == queue->rx_irq)
  585. unbind_from_irqhandler(queue->tx_irq, queue);
  586. else {
  587. unbind_from_irqhandler(queue->tx_irq, queue);
  588. unbind_from_irqhandler(queue->rx_irq, queue);
  589. }
  590. queue->tx_irq = 0;
  591. }
  592. xenvif_unmap_frontend_rings(queue);
  593. }
  594. }
  595. /* Reverse the relevant parts of xenvif_init_queue().
  596. * Used for queue teardown from xenvif_free(), and on the
  597. * error handling paths in xenbus.c:connect().
  598. */
  599. void xenvif_deinit_queue(struct xenvif_queue *queue)
  600. {
  601. free_xenballooned_pages(MAX_PENDING_REQS, queue->mmap_pages);
  602. netif_napi_del(&queue->napi);
  603. }
  604. void xenvif_free(struct xenvif *vif)
  605. {
  606. struct xenvif_queue *queue = NULL;
  607. unsigned int num_queues = vif->num_queues;
  608. unsigned int queue_index;
  609. /* Here we want to avoid timeout messages if an skb can be legitimately
  610. * stuck somewhere else. Realistically this could be an another vif's
  611. * internal or QDisc queue. That another vif also has this
  612. * rx_drain_timeout_msecs timeout, so give it time to drain out.
  613. * Although if that other guest wakes up just before its timeout happens
  614. * and takes only one skb from QDisc, it can hold onto other skbs for a
  615. * longer period.
  616. */
  617. unsigned int worst_case_skb_lifetime = (rx_drain_timeout_msecs/1000);
  618. unregister_netdev(vif->dev);
  619. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  620. queue = &vif->queues[queue_index];
  621. xenvif_wait_unmap_timeout(queue, worst_case_skb_lifetime);
  622. xenvif_deinit_queue(queue);
  623. }
  624. vfree(vif->queues);
  625. vif->queues = NULL;
  626. vif->num_queues = 0;
  627. free_netdev(vif->dev);
  628. module_put(THIS_MODULE);
  629. }