interface.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. int xenvif_schedulable(struct xenvif *vif)
  42. {
  43. return netif_running(vif->dev) && netif_carrier_ok(vif->dev);
  44. }
  45. static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
  46. {
  47. struct xenvif *vif = dev_id;
  48. if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx))
  49. napi_schedule(&vif->napi);
  50. return IRQ_HANDLED;
  51. }
  52. static int xenvif_poll(struct napi_struct *napi, int budget)
  53. {
  54. struct xenvif *vif = container_of(napi, struct xenvif, napi);
  55. int work_done;
  56. /* This vif is rogue, we pretend we've there is nothing to do
  57. * for this vif to deschedule it from NAPI. But this interface
  58. * will be turned off in thread context later.
  59. */
  60. if (unlikely(vif->disabled)) {
  61. napi_complete(napi);
  62. return 0;
  63. }
  64. work_done = xenvif_tx_action(vif, budget);
  65. if (work_done < budget) {
  66. napi_complete(napi);
  67. xenvif_napi_schedule_or_enable_events(vif);
  68. }
  69. return work_done;
  70. }
  71. static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
  72. {
  73. struct xenvif *vif = dev_id;
  74. xenvif_kick_thread(vif);
  75. return IRQ_HANDLED;
  76. }
  77. static irqreturn_t xenvif_interrupt(int irq, void *dev_id)
  78. {
  79. xenvif_tx_interrupt(irq, dev_id);
  80. xenvif_rx_interrupt(irq, dev_id);
  81. return IRQ_HANDLED;
  82. }
  83. static void xenvif_wake_queue(unsigned long data)
  84. {
  85. struct xenvif *vif = (struct xenvif *)data;
  86. if (netif_queue_stopped(vif->dev)) {
  87. netdev_err(vif->dev, "draining TX queue\n");
  88. vif->rx_queue_purge = true;
  89. xenvif_kick_thread(vif);
  90. netif_wake_queue(vif->dev);
  91. }
  92. }
  93. static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  94. {
  95. struct xenvif *vif = netdev_priv(dev);
  96. int min_slots_needed;
  97. BUG_ON(skb->dev != dev);
  98. /* Drop the packet if vif is not ready */
  99. if (vif->task == NULL ||
  100. vif->dealloc_task == NULL ||
  101. !xenvif_schedulable(vif))
  102. goto drop;
  103. /* At best we'll need one slot for the header and one for each
  104. * frag.
  105. */
  106. min_slots_needed = 1 + skb_shinfo(skb)->nr_frags;
  107. /* If the skb is GSO then we'll also need an extra slot for the
  108. * metadata.
  109. */
  110. if (skb_is_gso(skb))
  111. min_slots_needed++;
  112. /* If the skb can't possibly fit in the remaining slots
  113. * then turn off the queue to give the ring a chance to
  114. * drain.
  115. */
  116. if (!xenvif_rx_ring_slots_available(vif, min_slots_needed)) {
  117. vif->wake_queue.function = xenvif_wake_queue;
  118. vif->wake_queue.data = (unsigned long)vif;
  119. xenvif_stop_queue(vif);
  120. mod_timer(&vif->wake_queue,
  121. jiffies + rx_drain_timeout_jiffies);
  122. }
  123. skb_queue_tail(&vif->rx_queue, skb);
  124. xenvif_kick_thread(vif);
  125. return NETDEV_TX_OK;
  126. drop:
  127. vif->dev->stats.tx_dropped++;
  128. dev_kfree_skb(skb);
  129. return NETDEV_TX_OK;
  130. }
  131. static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
  132. {
  133. struct xenvif *vif = netdev_priv(dev);
  134. return &vif->dev->stats;
  135. }
  136. static void xenvif_up(struct xenvif *vif)
  137. {
  138. napi_enable(&vif->napi);
  139. enable_irq(vif->tx_irq);
  140. if (vif->tx_irq != vif->rx_irq)
  141. enable_irq(vif->rx_irq);
  142. xenvif_napi_schedule_or_enable_events(vif);
  143. }
  144. static void xenvif_down(struct xenvif *vif)
  145. {
  146. napi_disable(&vif->napi);
  147. disable_irq(vif->tx_irq);
  148. if (vif->tx_irq != vif->rx_irq)
  149. disable_irq(vif->rx_irq);
  150. del_timer_sync(&vif->credit_timeout);
  151. }
  152. static int xenvif_open(struct net_device *dev)
  153. {
  154. struct xenvif *vif = netdev_priv(dev);
  155. if (netif_carrier_ok(dev))
  156. xenvif_up(vif);
  157. netif_start_queue(dev);
  158. return 0;
  159. }
  160. static int xenvif_close(struct net_device *dev)
  161. {
  162. struct xenvif *vif = netdev_priv(dev);
  163. if (netif_carrier_ok(dev))
  164. xenvif_down(vif);
  165. netif_stop_queue(dev);
  166. return 0;
  167. }
  168. static int xenvif_change_mtu(struct net_device *dev, int mtu)
  169. {
  170. struct xenvif *vif = netdev_priv(dev);
  171. int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
  172. if (mtu > max)
  173. return -EINVAL;
  174. dev->mtu = mtu;
  175. return 0;
  176. }
  177. static netdev_features_t xenvif_fix_features(struct net_device *dev,
  178. netdev_features_t features)
  179. {
  180. struct xenvif *vif = netdev_priv(dev);
  181. if (!vif->can_sg)
  182. features &= ~NETIF_F_SG;
  183. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4))
  184. features &= ~NETIF_F_TSO;
  185. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6))
  186. features &= ~NETIF_F_TSO6;
  187. if (!vif->ip_csum)
  188. features &= ~NETIF_F_IP_CSUM;
  189. if (!vif->ipv6_csum)
  190. features &= ~NETIF_F_IPV6_CSUM;
  191. return features;
  192. }
  193. static const struct xenvif_stat {
  194. char name[ETH_GSTRING_LEN];
  195. u16 offset;
  196. } xenvif_stats[] = {
  197. {
  198. "rx_gso_checksum_fixup",
  199. offsetof(struct xenvif, rx_gso_checksum_fixup)
  200. },
  201. /* If (sent != success + fail), there are probably packets never
  202. * freed up properly!
  203. */
  204. {
  205. "tx_zerocopy_sent",
  206. offsetof(struct xenvif, tx_zerocopy_sent),
  207. },
  208. {
  209. "tx_zerocopy_success",
  210. offsetof(struct xenvif, tx_zerocopy_success),
  211. },
  212. {
  213. "tx_zerocopy_fail",
  214. offsetof(struct xenvif, tx_zerocopy_fail)
  215. },
  216. /* Number of packets exceeding MAX_SKB_FRAG slots. You should use
  217. * a guest with the same MAX_SKB_FRAG
  218. */
  219. {
  220. "tx_frag_overflow",
  221. offsetof(struct xenvif, tx_frag_overflow)
  222. },
  223. };
  224. static int xenvif_get_sset_count(struct net_device *dev, int string_set)
  225. {
  226. switch (string_set) {
  227. case ETH_SS_STATS:
  228. return ARRAY_SIZE(xenvif_stats);
  229. default:
  230. return -EINVAL;
  231. }
  232. }
  233. static void xenvif_get_ethtool_stats(struct net_device *dev,
  234. struct ethtool_stats *stats, u64 * data)
  235. {
  236. void *vif = netdev_priv(dev);
  237. int i;
  238. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
  239. data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset);
  240. }
  241. static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  242. {
  243. int i;
  244. switch (stringset) {
  245. case ETH_SS_STATS:
  246. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
  247. memcpy(data + i * ETH_GSTRING_LEN,
  248. xenvif_stats[i].name, ETH_GSTRING_LEN);
  249. break;
  250. }
  251. }
  252. static const struct ethtool_ops xenvif_ethtool_ops = {
  253. .get_link = ethtool_op_get_link,
  254. .get_sset_count = xenvif_get_sset_count,
  255. .get_ethtool_stats = xenvif_get_ethtool_stats,
  256. .get_strings = xenvif_get_strings,
  257. };
  258. static const struct net_device_ops xenvif_netdev_ops = {
  259. .ndo_start_xmit = xenvif_start_xmit,
  260. .ndo_get_stats = xenvif_get_stats,
  261. .ndo_open = xenvif_open,
  262. .ndo_stop = xenvif_close,
  263. .ndo_change_mtu = xenvif_change_mtu,
  264. .ndo_fix_features = xenvif_fix_features,
  265. .ndo_set_mac_address = eth_mac_addr,
  266. .ndo_validate_addr = eth_validate_addr,
  267. };
  268. struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
  269. unsigned int handle)
  270. {
  271. int err;
  272. struct net_device *dev;
  273. struct xenvif *vif;
  274. char name[IFNAMSIZ] = {};
  275. int i;
  276. snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
  277. dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup);
  278. if (dev == NULL) {
  279. pr_warn("Could not allocate netdev for %s\n", name);
  280. return ERR_PTR(-ENOMEM);
  281. }
  282. SET_NETDEV_DEV(dev, parent);
  283. vif = netdev_priv(dev);
  284. vif->grant_copy_op = vmalloc(sizeof(struct gnttab_copy) *
  285. MAX_GRANT_COPY_OPS);
  286. if (vif->grant_copy_op == NULL) {
  287. pr_warn("Could not allocate grant copy space for %s\n", name);
  288. free_netdev(dev);
  289. return ERR_PTR(-ENOMEM);
  290. }
  291. vif->domid = domid;
  292. vif->handle = handle;
  293. vif->can_sg = 1;
  294. vif->ip_csum = 1;
  295. vif->dev = dev;
  296. vif->disabled = false;
  297. vif->credit_bytes = vif->remaining_credit = ~0UL;
  298. vif->credit_usec = 0UL;
  299. init_timer(&vif->credit_timeout);
  300. vif->credit_window_start = get_jiffies_64();
  301. init_timer(&vif->wake_queue);
  302. dev->netdev_ops = &xenvif_netdev_ops;
  303. dev->hw_features = NETIF_F_SG |
  304. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  305. NETIF_F_TSO | NETIF_F_TSO6;
  306. dev->features = dev->hw_features | NETIF_F_RXCSUM;
  307. SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops);
  308. dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
  309. skb_queue_head_init(&vif->rx_queue);
  310. skb_queue_head_init(&vif->tx_queue);
  311. vif->pending_cons = 0;
  312. vif->pending_prod = MAX_PENDING_REQS;
  313. for (i = 0; i < MAX_PENDING_REQS; i++)
  314. vif->pending_ring[i] = i;
  315. spin_lock_init(&vif->callback_lock);
  316. spin_lock_init(&vif->response_lock);
  317. /* If ballooning is disabled, this will consume real memory, so you
  318. * better enable it. The long term solution would be to use just a
  319. * bunch of valid page descriptors, without dependency on ballooning
  320. */
  321. err = alloc_xenballooned_pages(MAX_PENDING_REQS,
  322. vif->mmap_pages,
  323. false);
  324. if (err) {
  325. netdev_err(dev, "Could not reserve mmap_pages\n");
  326. return ERR_PTR(-ENOMEM);
  327. }
  328. for (i = 0; i < MAX_PENDING_REQS; i++) {
  329. vif->pending_tx_info[i].callback_struct = (struct ubuf_info)
  330. { .callback = xenvif_zerocopy_callback,
  331. .ctx = NULL,
  332. .desc = i };
  333. vif->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
  334. }
  335. /*
  336. * Initialise a dummy MAC address. We choose the numerically
  337. * largest non-broadcast address to prevent the address getting
  338. * stolen by an Ethernet bridge for STP purposes.
  339. * (FE:FF:FF:FF:FF:FF)
  340. */
  341. memset(dev->dev_addr, 0xFF, ETH_ALEN);
  342. dev->dev_addr[0] &= ~0x01;
  343. netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT);
  344. netif_carrier_off(dev);
  345. err = register_netdev(dev);
  346. if (err) {
  347. netdev_warn(dev, "Could not register device: err=%d\n", err);
  348. free_netdev(dev);
  349. return ERR_PTR(err);
  350. }
  351. netdev_dbg(dev, "Successfully created xenvif\n");
  352. __module_get(THIS_MODULE);
  353. return vif;
  354. }
  355. int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
  356. unsigned long rx_ring_ref, unsigned int tx_evtchn,
  357. unsigned int rx_evtchn)
  358. {
  359. struct task_struct *task;
  360. int err = -ENOMEM;
  361. BUG_ON(vif->tx_irq);
  362. BUG_ON(vif->task);
  363. BUG_ON(vif->dealloc_task);
  364. err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref);
  365. if (err < 0)
  366. goto err;
  367. init_waitqueue_head(&vif->wq);
  368. init_waitqueue_head(&vif->dealloc_wq);
  369. if (tx_evtchn == rx_evtchn) {
  370. /* feature-split-event-channels == 0 */
  371. err = bind_interdomain_evtchn_to_irqhandler(
  372. vif->domid, tx_evtchn, xenvif_interrupt, 0,
  373. vif->dev->name, vif);
  374. if (err < 0)
  375. goto err_unmap;
  376. vif->tx_irq = vif->rx_irq = err;
  377. disable_irq(vif->tx_irq);
  378. } else {
  379. /* feature-split-event-channels == 1 */
  380. snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name),
  381. "%s-tx", vif->dev->name);
  382. err = bind_interdomain_evtchn_to_irqhandler(
  383. vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
  384. vif->tx_irq_name, vif);
  385. if (err < 0)
  386. goto err_unmap;
  387. vif->tx_irq = err;
  388. disable_irq(vif->tx_irq);
  389. snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name),
  390. "%s-rx", vif->dev->name);
  391. err = bind_interdomain_evtchn_to_irqhandler(
  392. vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
  393. vif->rx_irq_name, vif);
  394. if (err < 0)
  395. goto err_tx_unbind;
  396. vif->rx_irq = err;
  397. disable_irq(vif->rx_irq);
  398. }
  399. task = kthread_create(xenvif_kthread_guest_rx,
  400. (void *)vif, "%s-guest-rx", vif->dev->name);
  401. if (IS_ERR(task)) {
  402. pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
  403. err = PTR_ERR(task);
  404. goto err_rx_unbind;
  405. }
  406. vif->task = task;
  407. task = kthread_create(xenvif_dealloc_kthread,
  408. (void *)vif, "%s-dealloc", vif->dev->name);
  409. if (IS_ERR(task)) {
  410. pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
  411. err = PTR_ERR(task);
  412. goto err_rx_unbind;
  413. }
  414. vif->dealloc_task = task;
  415. rtnl_lock();
  416. if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
  417. dev_set_mtu(vif->dev, ETH_DATA_LEN);
  418. netdev_update_features(vif->dev);
  419. netif_carrier_on(vif->dev);
  420. if (netif_running(vif->dev))
  421. xenvif_up(vif);
  422. rtnl_unlock();
  423. wake_up_process(vif->task);
  424. wake_up_process(vif->dealloc_task);
  425. return 0;
  426. err_rx_unbind:
  427. unbind_from_irqhandler(vif->rx_irq, vif);
  428. vif->rx_irq = 0;
  429. err_tx_unbind:
  430. unbind_from_irqhandler(vif->tx_irq, vif);
  431. vif->tx_irq = 0;
  432. err_unmap:
  433. xenvif_unmap_frontend_rings(vif);
  434. err:
  435. module_put(THIS_MODULE);
  436. return err;
  437. }
  438. void xenvif_carrier_off(struct xenvif *vif)
  439. {
  440. struct net_device *dev = vif->dev;
  441. rtnl_lock();
  442. netif_carrier_off(dev); /* discard queued packets */
  443. if (netif_running(dev))
  444. xenvif_down(vif);
  445. rtnl_unlock();
  446. }
  447. void xenvif_disconnect(struct xenvif *vif)
  448. {
  449. if (netif_carrier_ok(vif->dev))
  450. xenvif_carrier_off(vif);
  451. if (vif->task) {
  452. del_timer_sync(&vif->wake_queue);
  453. kthread_stop(vif->task);
  454. vif->task = NULL;
  455. }
  456. if (vif->dealloc_task) {
  457. kthread_stop(vif->dealloc_task);
  458. vif->dealloc_task = NULL;
  459. }
  460. if (vif->tx_irq) {
  461. if (vif->tx_irq == vif->rx_irq)
  462. unbind_from_irqhandler(vif->tx_irq, vif);
  463. else {
  464. unbind_from_irqhandler(vif->tx_irq, vif);
  465. unbind_from_irqhandler(vif->rx_irq, vif);
  466. }
  467. vif->tx_irq = 0;
  468. }
  469. xenvif_unmap_frontend_rings(vif);
  470. }
  471. void xenvif_free(struct xenvif *vif)
  472. {
  473. int i, unmap_timeout = 0;
  474. /* Here we want to avoid timeout messages if an skb can be legitimately
  475. * stuck somewhere else. Realistically this could be an another vif's
  476. * internal or QDisc queue. That another vif also has this
  477. * rx_drain_timeout_msecs timeout, but the timer only ditches the
  478. * internal queue. After that, the QDisc queue can put in worst case
  479. * XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS skbs into that another vif's
  480. * internal queue, so we need several rounds of such timeouts until we
  481. * can be sure that no another vif should have skb's from us. We are
  482. * not sending more skb's, so newly stuck packets are not interesting
  483. * for us here.
  484. */
  485. unsigned int worst_case_skb_lifetime = (rx_drain_timeout_msecs/1000) *
  486. DIV_ROUND_UP(XENVIF_QUEUE_LENGTH, (XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS));
  487. for (i = 0; i < MAX_PENDING_REQS; ++i) {
  488. if (vif->grant_tx_handle[i] != NETBACK_INVALID_HANDLE) {
  489. unmap_timeout++;
  490. schedule_timeout(msecs_to_jiffies(1000));
  491. if (unmap_timeout > worst_case_skb_lifetime &&
  492. net_ratelimit())
  493. netdev_err(vif->dev,
  494. "Page still granted! Index: %x\n",
  495. i);
  496. /* If there are still unmapped pages, reset the loop to
  497. * start checking again. We shouldn't exit here until
  498. * dealloc thread and NAPI instance release all the
  499. * pages. If a kernel bug causes the skbs to stall
  500. * somewhere, the interface cannot be brought down
  501. * properly.
  502. */
  503. i = -1;
  504. }
  505. }
  506. free_xenballooned_pages(MAX_PENDING_REQS, vif->mmap_pages);
  507. netif_napi_del(&vif->napi);
  508. unregister_netdev(vif->dev);
  509. vfree(vif->grant_copy_op);
  510. free_netdev(vif->dev);
  511. module_put(THIS_MODULE);
  512. }