interface.c 20 KB

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