interface.c 21 KB

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