interface.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. irqreturn_t xenvif_ctrl_interrupt(int irq, void *dev_id)
  109. {
  110. struct xenvif *vif = dev_id;
  111. wake_up(&vif->ctrl_wq);
  112. return IRQ_HANDLED;
  113. }
  114. int xenvif_queue_stopped(struct xenvif_queue *queue)
  115. {
  116. struct net_device *dev = queue->vif->dev;
  117. unsigned int id = queue->id;
  118. return netif_tx_queue_stopped(netdev_get_tx_queue(dev, id));
  119. }
  120. void xenvif_wake_queue(struct xenvif_queue *queue)
  121. {
  122. struct net_device *dev = queue->vif->dev;
  123. unsigned int id = queue->id;
  124. netif_tx_wake_queue(netdev_get_tx_queue(dev, id));
  125. }
  126. static u16 xenvif_select_queue(struct net_device *dev, struct sk_buff *skb,
  127. void *accel_priv,
  128. select_queue_fallback_t fallback)
  129. {
  130. struct xenvif *vif = netdev_priv(dev);
  131. unsigned int size = vif->hash.size;
  132. if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE) {
  133. u16 index = fallback(dev, skb) % dev->real_num_tx_queues;
  134. /* Make sure there is no hash information in the socket
  135. * buffer otherwise it would be incorrectly forwarded
  136. * to the frontend.
  137. */
  138. skb_clear_hash(skb);
  139. return index;
  140. }
  141. xenvif_set_skb_hash(vif, skb);
  142. if (size == 0)
  143. return skb_get_hash_raw(skb) % dev->real_num_tx_queues;
  144. return vif->hash.mapping[skb_get_hash_raw(skb) % size];
  145. }
  146. static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  147. {
  148. struct xenvif *vif = netdev_priv(dev);
  149. struct xenvif_queue *queue = NULL;
  150. unsigned int num_queues = vif->num_queues;
  151. u16 index;
  152. struct xenvif_rx_cb *cb;
  153. BUG_ON(skb->dev != dev);
  154. /* Drop the packet if queues are not set up */
  155. if (num_queues < 1)
  156. goto drop;
  157. /* Obtain the queue to be used to transmit this packet */
  158. index = skb_get_queue_mapping(skb);
  159. if (index >= num_queues) {
  160. pr_warn_ratelimited("Invalid queue %hu for packet on interface %s\n.",
  161. index, vif->dev->name);
  162. index %= num_queues;
  163. }
  164. queue = &vif->queues[index];
  165. /* Drop the packet if queue is not ready */
  166. if (queue->task == NULL ||
  167. queue->dealloc_task == NULL ||
  168. !xenvif_schedulable(vif))
  169. goto drop;
  170. if (vif->multicast_control && skb->pkt_type == PACKET_MULTICAST) {
  171. struct ethhdr *eth = (struct ethhdr *)skb->data;
  172. if (!xenvif_mcast_match(vif, eth->h_dest))
  173. goto drop;
  174. }
  175. cb = XENVIF_RX_CB(skb);
  176. cb->expires = jiffies + vif->drain_timeout;
  177. xenvif_rx_queue_tail(queue, skb);
  178. xenvif_kick_thread(queue);
  179. return NETDEV_TX_OK;
  180. drop:
  181. vif->dev->stats.tx_dropped++;
  182. dev_kfree_skb(skb);
  183. return NETDEV_TX_OK;
  184. }
  185. static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
  186. {
  187. struct xenvif *vif = netdev_priv(dev);
  188. struct xenvif_queue *queue = NULL;
  189. unsigned int num_queues = vif->num_queues;
  190. unsigned long rx_bytes = 0;
  191. unsigned long rx_packets = 0;
  192. unsigned long tx_bytes = 0;
  193. unsigned long tx_packets = 0;
  194. unsigned int index;
  195. if (vif->queues == NULL)
  196. goto out;
  197. /* Aggregate tx and rx stats from each queue */
  198. for (index = 0; index < num_queues; ++index) {
  199. queue = &vif->queues[index];
  200. rx_bytes += queue->stats.rx_bytes;
  201. rx_packets += queue->stats.rx_packets;
  202. tx_bytes += queue->stats.tx_bytes;
  203. tx_packets += queue->stats.tx_packets;
  204. }
  205. out:
  206. vif->dev->stats.rx_bytes = rx_bytes;
  207. vif->dev->stats.rx_packets = rx_packets;
  208. vif->dev->stats.tx_bytes = tx_bytes;
  209. vif->dev->stats.tx_packets = tx_packets;
  210. return &vif->dev->stats;
  211. }
  212. static void xenvif_up(struct xenvif *vif)
  213. {
  214. struct xenvif_queue *queue = NULL;
  215. unsigned int num_queues = vif->num_queues;
  216. unsigned int queue_index;
  217. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  218. queue = &vif->queues[queue_index];
  219. napi_enable(&queue->napi);
  220. enable_irq(queue->tx_irq);
  221. if (queue->tx_irq != queue->rx_irq)
  222. enable_irq(queue->rx_irq);
  223. xenvif_napi_schedule_or_enable_events(queue);
  224. }
  225. }
  226. static void xenvif_down(struct xenvif *vif)
  227. {
  228. struct xenvif_queue *queue = NULL;
  229. unsigned int num_queues = vif->num_queues;
  230. unsigned int queue_index;
  231. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  232. queue = &vif->queues[queue_index];
  233. disable_irq(queue->tx_irq);
  234. if (queue->tx_irq != queue->rx_irq)
  235. disable_irq(queue->rx_irq);
  236. napi_disable(&queue->napi);
  237. del_timer_sync(&queue->credit_timeout);
  238. }
  239. }
  240. static int xenvif_open(struct net_device *dev)
  241. {
  242. struct xenvif *vif = netdev_priv(dev);
  243. if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
  244. xenvif_up(vif);
  245. netif_tx_start_all_queues(dev);
  246. return 0;
  247. }
  248. static int xenvif_close(struct net_device *dev)
  249. {
  250. struct xenvif *vif = netdev_priv(dev);
  251. if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
  252. xenvif_down(vif);
  253. netif_tx_stop_all_queues(dev);
  254. return 0;
  255. }
  256. static int xenvif_change_mtu(struct net_device *dev, int mtu)
  257. {
  258. struct xenvif *vif = netdev_priv(dev);
  259. int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
  260. if (mtu > max)
  261. return -EINVAL;
  262. dev->mtu = mtu;
  263. return 0;
  264. }
  265. static netdev_features_t xenvif_fix_features(struct net_device *dev,
  266. netdev_features_t features)
  267. {
  268. struct xenvif *vif = netdev_priv(dev);
  269. if (!vif->can_sg)
  270. features &= ~NETIF_F_SG;
  271. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4))
  272. features &= ~NETIF_F_TSO;
  273. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6))
  274. features &= ~NETIF_F_TSO6;
  275. if (!vif->ip_csum)
  276. features &= ~NETIF_F_IP_CSUM;
  277. if (!vif->ipv6_csum)
  278. features &= ~NETIF_F_IPV6_CSUM;
  279. return features;
  280. }
  281. static const struct xenvif_stat {
  282. char name[ETH_GSTRING_LEN];
  283. u16 offset;
  284. } xenvif_stats[] = {
  285. {
  286. "rx_gso_checksum_fixup",
  287. offsetof(struct xenvif_stats, rx_gso_checksum_fixup)
  288. },
  289. /* If (sent != success + fail), there are probably packets never
  290. * freed up properly!
  291. */
  292. {
  293. "tx_zerocopy_sent",
  294. offsetof(struct xenvif_stats, tx_zerocopy_sent),
  295. },
  296. {
  297. "tx_zerocopy_success",
  298. offsetof(struct xenvif_stats, tx_zerocopy_success),
  299. },
  300. {
  301. "tx_zerocopy_fail",
  302. offsetof(struct xenvif_stats, tx_zerocopy_fail)
  303. },
  304. /* Number of packets exceeding MAX_SKB_FRAG slots. You should use
  305. * a guest with the same MAX_SKB_FRAG
  306. */
  307. {
  308. "tx_frag_overflow",
  309. offsetof(struct xenvif_stats, tx_frag_overflow)
  310. },
  311. };
  312. static int xenvif_get_sset_count(struct net_device *dev, int string_set)
  313. {
  314. switch (string_set) {
  315. case ETH_SS_STATS:
  316. return ARRAY_SIZE(xenvif_stats);
  317. default:
  318. return -EINVAL;
  319. }
  320. }
  321. static void xenvif_get_ethtool_stats(struct net_device *dev,
  322. struct ethtool_stats *stats, u64 * data)
  323. {
  324. struct xenvif *vif = netdev_priv(dev);
  325. unsigned int num_queues = vif->num_queues;
  326. int i;
  327. unsigned int queue_index;
  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. void *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_select_queue = xenvif_select_queue,
  356. .ndo_start_xmit = xenvif_start_xmit,
  357. .ndo_get_stats = xenvif_get_stats,
  358. .ndo_open = xenvif_open,
  359. .ndo_stop = xenvif_close,
  360. .ndo_change_mtu = xenvif_change_mtu,
  361. .ndo_fix_features = xenvif_fix_features,
  362. .ndo_set_mac_address = eth_mac_addr,
  363. .ndo_validate_addr = eth_validate_addr,
  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_*_queues().
  376. */
  377. dev = alloc_netdev_mq(sizeof(struct xenvif), name, NET_NAME_UNKNOWN,
  378. ether_setup, 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. vif->drain_timeout = msecs_to_jiffies(rx_drain_timeout_msecs);
  392. vif->stall_timeout = msecs_to_jiffies(rx_stall_timeout_msecs);
  393. /* Start out with no queues. */
  394. vif->queues = NULL;
  395. vif->num_queues = 0;
  396. spin_lock_init(&vif->lock);
  397. INIT_LIST_HEAD(&vif->fe_mcast_addr);
  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. eth_broadcast_addr(dev->dev_addr);
  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_timeout.function = xenvif_tx_credit_callback;
  431. queue->credit_window_start = get_jiffies_64();
  432. queue->rx_queue_max = XENVIF_RX_QUEUE_BYTES;
  433. skb_queue_head_init(&queue->rx_queue);
  434. skb_queue_head_init(&queue->tx_queue);
  435. queue->pending_cons = 0;
  436. queue->pending_prod = MAX_PENDING_REQS;
  437. for (i = 0; i < MAX_PENDING_REQS; ++i)
  438. queue->pending_ring[i] = i;
  439. spin_lock_init(&queue->callback_lock);
  440. spin_lock_init(&queue->response_lock);
  441. /* If ballooning is disabled, this will consume real memory, so you
  442. * better enable it. The long term solution would be to use just a
  443. * bunch of valid page descriptors, without dependency on ballooning
  444. */
  445. err = gnttab_alloc_pages(MAX_PENDING_REQS,
  446. queue->mmap_pages);
  447. if (err) {
  448. netdev_err(queue->vif->dev, "Could not reserve mmap_pages\n");
  449. return -ENOMEM;
  450. }
  451. for (i = 0; i < MAX_PENDING_REQS; i++) {
  452. queue->pending_tx_info[i].callback_struct = (struct ubuf_info)
  453. { .callback = xenvif_zerocopy_callback,
  454. .ctx = NULL,
  455. .desc = i };
  456. queue->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
  457. }
  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. if (netif_running(vif->dev))
  468. xenvif_up(vif);
  469. rtnl_unlock();
  470. }
  471. int xenvif_connect_ctrl(struct xenvif *vif, grant_ref_t ring_ref,
  472. unsigned int evtchn)
  473. {
  474. struct net_device *dev = vif->dev;
  475. void *addr;
  476. struct xen_netif_ctrl_sring *shared;
  477. struct task_struct *task;
  478. int err = -ENOMEM;
  479. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  480. &ring_ref, 1, &addr);
  481. if (err)
  482. goto err;
  483. shared = (struct xen_netif_ctrl_sring *)addr;
  484. BACK_RING_INIT(&vif->ctrl, shared, XEN_PAGE_SIZE);
  485. init_waitqueue_head(&vif->ctrl_wq);
  486. err = bind_interdomain_evtchn_to_irqhandler(vif->domid, evtchn,
  487. xenvif_ctrl_interrupt,
  488. 0, dev->name, vif);
  489. if (err < 0)
  490. goto err_unmap;
  491. vif->ctrl_irq = err;
  492. xenvif_init_hash(vif);
  493. task = kthread_create(xenvif_ctrl_kthread, (void *)vif,
  494. "%s-control", dev->name);
  495. if (IS_ERR(task)) {
  496. pr_warn("Could not allocate kthread for %s\n", dev->name);
  497. err = PTR_ERR(task);
  498. goto err_deinit;
  499. }
  500. get_task_struct(task);
  501. vif->ctrl_task = task;
  502. wake_up_process(vif->ctrl_task);
  503. return 0;
  504. err_deinit:
  505. xenvif_deinit_hash(vif);
  506. unbind_from_irqhandler(vif->ctrl_irq, vif);
  507. vif->ctrl_irq = 0;
  508. err_unmap:
  509. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  510. vif->ctrl.sring);
  511. vif->ctrl.sring = NULL;
  512. err:
  513. return err;
  514. }
  515. int xenvif_connect_data(struct xenvif_queue *queue,
  516. unsigned long tx_ring_ref,
  517. unsigned long rx_ring_ref,
  518. unsigned int tx_evtchn,
  519. unsigned int rx_evtchn)
  520. {
  521. struct task_struct *task;
  522. int err = -ENOMEM;
  523. BUG_ON(queue->tx_irq);
  524. BUG_ON(queue->task);
  525. BUG_ON(queue->dealloc_task);
  526. err = xenvif_map_frontend_data_rings(queue, tx_ring_ref,
  527. rx_ring_ref);
  528. if (err < 0)
  529. goto err;
  530. init_waitqueue_head(&queue->wq);
  531. init_waitqueue_head(&queue->dealloc_wq);
  532. atomic_set(&queue->inflight_packets, 0);
  533. netif_napi_add(queue->vif->dev, &queue->napi, xenvif_poll,
  534. XENVIF_NAPI_WEIGHT);
  535. if (tx_evtchn == rx_evtchn) {
  536. /* feature-split-event-channels == 0 */
  537. err = bind_interdomain_evtchn_to_irqhandler(
  538. queue->vif->domid, tx_evtchn, xenvif_interrupt, 0,
  539. queue->name, queue);
  540. if (err < 0)
  541. goto err_unmap;
  542. queue->tx_irq = queue->rx_irq = err;
  543. disable_irq(queue->tx_irq);
  544. } else {
  545. /* feature-split-event-channels == 1 */
  546. snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
  547. "%s-tx", queue->name);
  548. err = bind_interdomain_evtchn_to_irqhandler(
  549. queue->vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
  550. queue->tx_irq_name, queue);
  551. if (err < 0)
  552. goto err_unmap;
  553. queue->tx_irq = err;
  554. disable_irq(queue->tx_irq);
  555. snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
  556. "%s-rx", queue->name);
  557. err = bind_interdomain_evtchn_to_irqhandler(
  558. queue->vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
  559. queue->rx_irq_name, queue);
  560. if (err < 0)
  561. goto err_tx_unbind;
  562. queue->rx_irq = err;
  563. disable_irq(queue->rx_irq);
  564. }
  565. queue->stalled = true;
  566. task = kthread_create(xenvif_kthread_guest_rx,
  567. (void *)queue, "%s-guest-rx", queue->name);
  568. if (IS_ERR(task)) {
  569. pr_warn("Could not allocate kthread for %s\n", queue->name);
  570. err = PTR_ERR(task);
  571. goto err_rx_unbind;
  572. }
  573. queue->task = task;
  574. get_task_struct(task);
  575. task = kthread_create(xenvif_dealloc_kthread,
  576. (void *)queue, "%s-dealloc", queue->name);
  577. if (IS_ERR(task)) {
  578. pr_warn("Could not allocate kthread for %s\n", queue->name);
  579. err = PTR_ERR(task);
  580. goto err_rx_unbind;
  581. }
  582. queue->dealloc_task = task;
  583. wake_up_process(queue->task);
  584. wake_up_process(queue->dealloc_task);
  585. return 0;
  586. err_rx_unbind:
  587. unbind_from_irqhandler(queue->rx_irq, queue);
  588. queue->rx_irq = 0;
  589. err_tx_unbind:
  590. unbind_from_irqhandler(queue->tx_irq, queue);
  591. queue->tx_irq = 0;
  592. err_unmap:
  593. xenvif_unmap_frontend_data_rings(queue);
  594. netif_napi_del(&queue->napi);
  595. err:
  596. module_put(THIS_MODULE);
  597. return err;
  598. }
  599. void xenvif_carrier_off(struct xenvif *vif)
  600. {
  601. struct net_device *dev = vif->dev;
  602. rtnl_lock();
  603. if (test_and_clear_bit(VIF_STATUS_CONNECTED, &vif->status)) {
  604. netif_carrier_off(dev); /* discard queued packets */
  605. if (netif_running(dev))
  606. xenvif_down(vif);
  607. }
  608. rtnl_unlock();
  609. }
  610. void xenvif_disconnect_data(struct xenvif *vif)
  611. {
  612. struct xenvif_queue *queue = NULL;
  613. unsigned int num_queues = vif->num_queues;
  614. unsigned int queue_index;
  615. xenvif_carrier_off(vif);
  616. for (queue_index = 0; queue_index < num_queues; ++queue_index) {
  617. queue = &vif->queues[queue_index];
  618. netif_napi_del(&queue->napi);
  619. if (queue->task) {
  620. kthread_stop(queue->task);
  621. put_task_struct(queue->task);
  622. queue->task = NULL;
  623. }
  624. if (queue->dealloc_task) {
  625. kthread_stop(queue->dealloc_task);
  626. queue->dealloc_task = NULL;
  627. }
  628. if (queue->tx_irq) {
  629. if (queue->tx_irq == queue->rx_irq)
  630. unbind_from_irqhandler(queue->tx_irq, queue);
  631. else {
  632. unbind_from_irqhandler(queue->tx_irq, queue);
  633. unbind_from_irqhandler(queue->rx_irq, queue);
  634. }
  635. queue->tx_irq = 0;
  636. }
  637. xenvif_unmap_frontend_data_rings(queue);
  638. }
  639. xenvif_mcast_addr_list_free(vif);
  640. }
  641. void xenvif_disconnect_ctrl(struct xenvif *vif)
  642. {
  643. if (vif->ctrl_task) {
  644. kthread_stop(vif->ctrl_task);
  645. put_task_struct(vif->ctrl_task);
  646. vif->ctrl_task = NULL;
  647. }
  648. xenvif_deinit_hash(vif);
  649. if (vif->ctrl_irq) {
  650. unbind_from_irqhandler(vif->ctrl_irq, vif);
  651. vif->ctrl_irq = 0;
  652. }
  653. if (vif->ctrl.sring) {
  654. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  655. vif->ctrl.sring);
  656. vif->ctrl.sring = NULL;
  657. }
  658. }
  659. /* Reverse the relevant parts of xenvif_init_queue().
  660. * Used for queue teardown from xenvif_free(), and on the
  661. * error handling paths in xenbus.c:connect().
  662. */
  663. void xenvif_deinit_queue(struct xenvif_queue *queue)
  664. {
  665. gnttab_free_pages(MAX_PENDING_REQS, queue->mmap_pages);
  666. }
  667. void xenvif_free(struct xenvif *vif)
  668. {
  669. struct xenvif_queue *queues = vif->queues;
  670. unsigned int num_queues = vif->num_queues;
  671. unsigned int queue_index;
  672. unregister_netdev(vif->dev);
  673. free_netdev(vif->dev);
  674. for (queue_index = 0; queue_index < num_queues; ++queue_index)
  675. xenvif_deinit_queue(&queues[queue_index]);
  676. vfree(queues);
  677. module_put(THIS_MODULE);
  678. }