interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. #define XENVIF_QUEUE_LENGTH 32
  39. #define XENVIF_NAPI_WEIGHT 64
  40. int xenvif_schedulable(struct xenvif *vif)
  41. {
  42. return netif_running(vif->dev) && netif_carrier_ok(vif->dev);
  43. }
  44. static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
  45. {
  46. struct xenvif *vif = dev_id;
  47. if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx))
  48. napi_schedule(&vif->napi);
  49. return IRQ_HANDLED;
  50. }
  51. static int xenvif_poll(struct napi_struct *napi, int budget)
  52. {
  53. struct xenvif *vif = container_of(napi, struct xenvif, napi);
  54. int work_done;
  55. work_done = xenvif_tx_action(vif, budget);
  56. if (work_done < budget) {
  57. int more_to_do = 0;
  58. unsigned long flags;
  59. /* It is necessary to disable IRQ before calling
  60. * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
  61. * lose event from the frontend.
  62. *
  63. * Consider:
  64. * RING_HAS_UNCONSUMED_REQUESTS
  65. * <frontend generates event to trigger napi_schedule>
  66. * __napi_complete
  67. *
  68. * This handler is still in scheduled state so the
  69. * event has no effect at all. After __napi_complete
  70. * this handler is descheduled and cannot get
  71. * scheduled again. We lose event in this case and the ring
  72. * will be completely stalled.
  73. */
  74. local_irq_save(flags);
  75. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
  76. if (!more_to_do)
  77. __napi_complete(napi);
  78. local_irq_restore(flags);
  79. }
  80. return work_done;
  81. }
  82. static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
  83. {
  84. struct xenvif *vif = dev_id;
  85. xenvif_kick_thread(vif);
  86. return IRQ_HANDLED;
  87. }
  88. static irqreturn_t xenvif_interrupt(int irq, void *dev_id)
  89. {
  90. xenvif_tx_interrupt(irq, dev_id);
  91. xenvif_rx_interrupt(irq, dev_id);
  92. return IRQ_HANDLED;
  93. }
  94. static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  95. {
  96. struct xenvif *vif = netdev_priv(dev);
  97. int min_slots_needed;
  98. BUG_ON(skb->dev != dev);
  99. /* Drop the packet if vif is not ready */
  100. if (vif->task == NULL || !xenvif_schedulable(vif))
  101. goto drop;
  102. /* At best we'll need one slot for the header and one for each
  103. * frag.
  104. */
  105. min_slots_needed = 1 + skb_shinfo(skb)->nr_frags;
  106. /* If the skb is GSO then we'll also need an extra slot for the
  107. * metadata.
  108. */
  109. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
  110. skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  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. xenvif_stop_queue(vif);
  118. skb_queue_tail(&vif->rx_queue, skb);
  119. xenvif_kick_thread(vif);
  120. return NETDEV_TX_OK;
  121. drop:
  122. vif->dev->stats.tx_dropped++;
  123. dev_kfree_skb(skb);
  124. return NETDEV_TX_OK;
  125. }
  126. static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
  127. {
  128. struct xenvif *vif = netdev_priv(dev);
  129. return &vif->dev->stats;
  130. }
  131. static void xenvif_up(struct xenvif *vif)
  132. {
  133. napi_enable(&vif->napi);
  134. enable_irq(vif->tx_irq);
  135. if (vif->tx_irq != vif->rx_irq)
  136. enable_irq(vif->rx_irq);
  137. xenvif_check_rx_xenvif(vif);
  138. }
  139. static void xenvif_down(struct xenvif *vif)
  140. {
  141. napi_disable(&vif->napi);
  142. disable_irq(vif->tx_irq);
  143. if (vif->tx_irq != vif->rx_irq)
  144. disable_irq(vif->rx_irq);
  145. del_timer_sync(&vif->credit_timeout);
  146. }
  147. static int xenvif_open(struct net_device *dev)
  148. {
  149. struct xenvif *vif = netdev_priv(dev);
  150. if (netif_carrier_ok(dev))
  151. xenvif_up(vif);
  152. netif_start_queue(dev);
  153. return 0;
  154. }
  155. static int xenvif_close(struct net_device *dev)
  156. {
  157. struct xenvif *vif = netdev_priv(dev);
  158. if (netif_carrier_ok(dev))
  159. xenvif_down(vif);
  160. netif_stop_queue(dev);
  161. return 0;
  162. }
  163. static int xenvif_change_mtu(struct net_device *dev, int mtu)
  164. {
  165. struct xenvif *vif = netdev_priv(dev);
  166. int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
  167. if (mtu > max)
  168. return -EINVAL;
  169. dev->mtu = mtu;
  170. return 0;
  171. }
  172. static netdev_features_t xenvif_fix_features(struct net_device *dev,
  173. netdev_features_t features)
  174. {
  175. struct xenvif *vif = netdev_priv(dev);
  176. if (!vif->can_sg)
  177. features &= ~NETIF_F_SG;
  178. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4))
  179. features &= ~NETIF_F_TSO;
  180. if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6))
  181. features &= ~NETIF_F_TSO6;
  182. if (!vif->ip_csum)
  183. features &= ~NETIF_F_IP_CSUM;
  184. if (!vif->ipv6_csum)
  185. features &= ~NETIF_F_IPV6_CSUM;
  186. return features;
  187. }
  188. static const struct xenvif_stat {
  189. char name[ETH_GSTRING_LEN];
  190. u16 offset;
  191. } xenvif_stats[] = {
  192. {
  193. "rx_gso_checksum_fixup",
  194. offsetof(struct xenvif, rx_gso_checksum_fixup)
  195. },
  196. };
  197. static int xenvif_get_sset_count(struct net_device *dev, int string_set)
  198. {
  199. switch (string_set) {
  200. case ETH_SS_STATS:
  201. return ARRAY_SIZE(xenvif_stats);
  202. default:
  203. return -EINVAL;
  204. }
  205. }
  206. static void xenvif_get_ethtool_stats(struct net_device *dev,
  207. struct ethtool_stats *stats, u64 * data)
  208. {
  209. void *vif = netdev_priv(dev);
  210. int i;
  211. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
  212. data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset);
  213. }
  214. static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  215. {
  216. int i;
  217. switch (stringset) {
  218. case ETH_SS_STATS:
  219. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
  220. memcpy(data + i * ETH_GSTRING_LEN,
  221. xenvif_stats[i].name, ETH_GSTRING_LEN);
  222. break;
  223. }
  224. }
  225. static const struct ethtool_ops xenvif_ethtool_ops = {
  226. .get_link = ethtool_op_get_link,
  227. .get_sset_count = xenvif_get_sset_count,
  228. .get_ethtool_stats = xenvif_get_ethtool_stats,
  229. .get_strings = xenvif_get_strings,
  230. };
  231. static const struct net_device_ops xenvif_netdev_ops = {
  232. .ndo_start_xmit = xenvif_start_xmit,
  233. .ndo_get_stats = xenvif_get_stats,
  234. .ndo_open = xenvif_open,
  235. .ndo_stop = xenvif_close,
  236. .ndo_change_mtu = xenvif_change_mtu,
  237. .ndo_fix_features = xenvif_fix_features,
  238. .ndo_set_mac_address = eth_mac_addr,
  239. .ndo_validate_addr = eth_validate_addr,
  240. };
  241. struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
  242. unsigned int handle)
  243. {
  244. int err;
  245. struct net_device *dev;
  246. struct xenvif *vif;
  247. char name[IFNAMSIZ] = {};
  248. int i;
  249. snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
  250. dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup);
  251. if (dev == NULL) {
  252. pr_warn("Could not allocate netdev for %s\n", name);
  253. return ERR_PTR(-ENOMEM);
  254. }
  255. SET_NETDEV_DEV(dev, parent);
  256. vif = netdev_priv(dev);
  257. vif->grant_copy_op = vmalloc(sizeof(struct gnttab_copy) *
  258. MAX_GRANT_COPY_OPS);
  259. if (vif->grant_copy_op == NULL) {
  260. pr_warn("Could not allocate grant copy space for %s\n", name);
  261. free_netdev(dev);
  262. return ERR_PTR(-ENOMEM);
  263. }
  264. vif->domid = domid;
  265. vif->handle = handle;
  266. vif->can_sg = 1;
  267. vif->ip_csum = 1;
  268. vif->dev = dev;
  269. vif->credit_bytes = vif->remaining_credit = ~0UL;
  270. vif->credit_usec = 0UL;
  271. init_timer(&vif->credit_timeout);
  272. vif->credit_window_start = get_jiffies_64();
  273. dev->netdev_ops = &xenvif_netdev_ops;
  274. dev->hw_features = NETIF_F_SG |
  275. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  276. NETIF_F_TSO | NETIF_F_TSO6;
  277. dev->features = dev->hw_features | NETIF_F_RXCSUM;
  278. SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops);
  279. dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
  280. skb_queue_head_init(&vif->rx_queue);
  281. skb_queue_head_init(&vif->tx_queue);
  282. vif->pending_cons = 0;
  283. vif->pending_prod = MAX_PENDING_REQS;
  284. for (i = 0; i < MAX_PENDING_REQS; i++)
  285. vif->pending_ring[i] = i;
  286. for (i = 0; i < MAX_PENDING_REQS; i++)
  287. vif->mmap_pages[i] = NULL;
  288. /*
  289. * Initialise a dummy MAC address. We choose the numerically
  290. * largest non-broadcast address to prevent the address getting
  291. * stolen by an Ethernet bridge for STP purposes.
  292. * (FE:FF:FF:FF:FF:FF)
  293. */
  294. memset(dev->dev_addr, 0xFF, ETH_ALEN);
  295. dev->dev_addr[0] &= ~0x01;
  296. netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT);
  297. netif_carrier_off(dev);
  298. err = register_netdev(dev);
  299. if (err) {
  300. netdev_warn(dev, "Could not register device: err=%d\n", err);
  301. free_netdev(dev);
  302. return ERR_PTR(err);
  303. }
  304. netdev_dbg(dev, "Successfully created xenvif\n");
  305. __module_get(THIS_MODULE);
  306. return vif;
  307. }
  308. int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
  309. unsigned long rx_ring_ref, unsigned int tx_evtchn,
  310. unsigned int rx_evtchn)
  311. {
  312. struct task_struct *task;
  313. int err = -ENOMEM;
  314. BUG_ON(vif->tx_irq);
  315. BUG_ON(vif->task);
  316. err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref);
  317. if (err < 0)
  318. goto err;
  319. init_waitqueue_head(&vif->wq);
  320. if (tx_evtchn == rx_evtchn) {
  321. /* feature-split-event-channels == 0 */
  322. err = bind_interdomain_evtchn_to_irqhandler(
  323. vif->domid, tx_evtchn, xenvif_interrupt, 0,
  324. vif->dev->name, vif);
  325. if (err < 0)
  326. goto err_unmap;
  327. vif->tx_irq = vif->rx_irq = err;
  328. disable_irq(vif->tx_irq);
  329. } else {
  330. /* feature-split-event-channels == 1 */
  331. snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name),
  332. "%s-tx", vif->dev->name);
  333. err = bind_interdomain_evtchn_to_irqhandler(
  334. vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
  335. vif->tx_irq_name, vif);
  336. if (err < 0)
  337. goto err_unmap;
  338. vif->tx_irq = err;
  339. disable_irq(vif->tx_irq);
  340. snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name),
  341. "%s-rx", vif->dev->name);
  342. err = bind_interdomain_evtchn_to_irqhandler(
  343. vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
  344. vif->rx_irq_name, vif);
  345. if (err < 0)
  346. goto err_tx_unbind;
  347. vif->rx_irq = err;
  348. disable_irq(vif->rx_irq);
  349. }
  350. task = kthread_create(xenvif_kthread_guest_rx,
  351. (void *)vif, "%s-guest-rx", vif->dev->name);
  352. if (IS_ERR(task)) {
  353. pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
  354. err = PTR_ERR(task);
  355. goto err_rx_unbind;
  356. }
  357. vif->task = task;
  358. rtnl_lock();
  359. if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
  360. dev_set_mtu(vif->dev, ETH_DATA_LEN);
  361. netdev_update_features(vif->dev);
  362. netif_carrier_on(vif->dev);
  363. if (netif_running(vif->dev))
  364. xenvif_up(vif);
  365. rtnl_unlock();
  366. wake_up_process(vif->task);
  367. return 0;
  368. err_rx_unbind:
  369. unbind_from_irqhandler(vif->rx_irq, vif);
  370. vif->rx_irq = 0;
  371. err_tx_unbind:
  372. unbind_from_irqhandler(vif->tx_irq, vif);
  373. vif->tx_irq = 0;
  374. err_unmap:
  375. xenvif_unmap_frontend_rings(vif);
  376. err:
  377. module_put(THIS_MODULE);
  378. return err;
  379. }
  380. void xenvif_carrier_off(struct xenvif *vif)
  381. {
  382. struct net_device *dev = vif->dev;
  383. rtnl_lock();
  384. netif_carrier_off(dev); /* discard queued packets */
  385. if (netif_running(dev))
  386. xenvif_down(vif);
  387. rtnl_unlock();
  388. }
  389. void xenvif_disconnect(struct xenvif *vif)
  390. {
  391. if (netif_carrier_ok(vif->dev))
  392. xenvif_carrier_off(vif);
  393. if (vif->task) {
  394. kthread_stop(vif->task);
  395. vif->task = NULL;
  396. }
  397. if (vif->tx_irq) {
  398. if (vif->tx_irq == vif->rx_irq)
  399. unbind_from_irqhandler(vif->tx_irq, vif);
  400. else {
  401. unbind_from_irqhandler(vif->tx_irq, vif);
  402. unbind_from_irqhandler(vif->rx_irq, vif);
  403. }
  404. vif->tx_irq = 0;
  405. }
  406. xenvif_unmap_frontend_rings(vif);
  407. }
  408. void xenvif_free(struct xenvif *vif)
  409. {
  410. netif_napi_del(&vif->napi);
  411. unregister_netdev(vif->dev);
  412. vfree(vif->grant_copy_op);
  413. free_netdev(vif->dev);
  414. module_put(THIS_MODULE);
  415. }