interface.c 20 KB

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