interface.c 18 KB

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