interface.c 19 KB

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