interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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/ethtool.h>
  32. #include <linux/rtnetlink.h>
  33. #include <linux/if_vlan.h>
  34. #include <xen/events.h>
  35. #include <asm/xen/hypercall.h>
  36. #define XENVIF_QUEUE_LENGTH 32
  37. void xenvif_get(struct xenvif *vif)
  38. {
  39. atomic_inc(&vif->refcnt);
  40. }
  41. void xenvif_put(struct xenvif *vif)
  42. {
  43. if (atomic_dec_and_test(&vif->refcnt))
  44. wake_up(&vif->waiting_to_free);
  45. }
  46. int xenvif_schedulable(struct xenvif *vif)
  47. {
  48. return netif_running(vif->dev) && netif_carrier_ok(vif->dev);
  49. }
  50. static int xenvif_rx_schedulable(struct xenvif *vif)
  51. {
  52. return xenvif_schedulable(vif) && !xen_netbk_rx_ring_full(vif);
  53. }
  54. static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
  55. {
  56. struct xenvif *vif = dev_id;
  57. if (vif->netbk == NULL)
  58. return IRQ_HANDLED;
  59. xen_netbk_schedule_xenvif(vif);
  60. return IRQ_HANDLED;
  61. }
  62. static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
  63. {
  64. struct xenvif *vif = dev_id;
  65. if (vif->netbk == NULL)
  66. return IRQ_HANDLED;
  67. if (xenvif_rx_schedulable(vif))
  68. netif_wake_queue(vif->dev);
  69. return IRQ_HANDLED;
  70. }
  71. static irqreturn_t xenvif_interrupt(int irq, void *dev_id)
  72. {
  73. xenvif_tx_interrupt(irq, dev_id);
  74. xenvif_rx_interrupt(irq, dev_id);
  75. return IRQ_HANDLED;
  76. }
  77. static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  78. {
  79. struct xenvif *vif = netdev_priv(dev);
  80. BUG_ON(skb->dev != dev);
  81. if (vif->netbk == NULL)
  82. goto drop;
  83. /* Drop the packet if the target domain has no receive buffers. */
  84. if (!xenvif_rx_schedulable(vif))
  85. goto drop;
  86. /* Reserve ring slots for the worst-case number of fragments. */
  87. vif->rx_req_cons_peek += xen_netbk_count_skb_slots(vif, skb);
  88. xenvif_get(vif);
  89. if (vif->can_queue && xen_netbk_must_stop_queue(vif))
  90. netif_stop_queue(dev);
  91. xen_netbk_queue_tx_skb(vif, skb);
  92. return NETDEV_TX_OK;
  93. drop:
  94. vif->dev->stats.tx_dropped++;
  95. dev_kfree_skb(skb);
  96. return NETDEV_TX_OK;
  97. }
  98. void xenvif_receive_skb(struct xenvif *vif, struct sk_buff *skb)
  99. {
  100. netif_rx_ni(skb);
  101. }
  102. void xenvif_notify_tx_completion(struct xenvif *vif)
  103. {
  104. if (netif_queue_stopped(vif->dev) && xenvif_rx_schedulable(vif))
  105. netif_wake_queue(vif->dev);
  106. }
  107. static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
  108. {
  109. struct xenvif *vif = netdev_priv(dev);
  110. return &vif->dev->stats;
  111. }
  112. static void xenvif_up(struct xenvif *vif)
  113. {
  114. xen_netbk_add_xenvif(vif);
  115. enable_irq(vif->tx_irq);
  116. if (vif->tx_irq != vif->rx_irq)
  117. enable_irq(vif->rx_irq);
  118. xen_netbk_check_rx_xenvif(vif);
  119. }
  120. static void xenvif_down(struct xenvif *vif)
  121. {
  122. disable_irq(vif->tx_irq);
  123. if (vif->tx_irq != vif->rx_irq)
  124. disable_irq(vif->rx_irq);
  125. del_timer_sync(&vif->credit_timeout);
  126. xen_netbk_deschedule_xenvif(vif);
  127. xen_netbk_remove_xenvif(vif);
  128. }
  129. static int xenvif_open(struct net_device *dev)
  130. {
  131. struct xenvif *vif = netdev_priv(dev);
  132. if (netif_carrier_ok(dev))
  133. xenvif_up(vif);
  134. netif_start_queue(dev);
  135. return 0;
  136. }
  137. static int xenvif_close(struct net_device *dev)
  138. {
  139. struct xenvif *vif = netdev_priv(dev);
  140. if (netif_carrier_ok(dev))
  141. xenvif_down(vif);
  142. netif_stop_queue(dev);
  143. return 0;
  144. }
  145. static int xenvif_change_mtu(struct net_device *dev, int mtu)
  146. {
  147. struct xenvif *vif = netdev_priv(dev);
  148. int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
  149. if (mtu > max)
  150. return -EINVAL;
  151. dev->mtu = mtu;
  152. return 0;
  153. }
  154. static netdev_features_t xenvif_fix_features(struct net_device *dev,
  155. netdev_features_t features)
  156. {
  157. struct xenvif *vif = netdev_priv(dev);
  158. if (!vif->can_sg)
  159. features &= ~NETIF_F_SG;
  160. if (!vif->gso && !vif->gso_prefix)
  161. features &= ~NETIF_F_TSO;
  162. if (!vif->csum)
  163. features &= ~NETIF_F_IP_CSUM;
  164. return features;
  165. }
  166. static const struct xenvif_stat {
  167. char name[ETH_GSTRING_LEN];
  168. u16 offset;
  169. } xenvif_stats[] = {
  170. {
  171. "rx_gso_checksum_fixup",
  172. offsetof(struct xenvif, rx_gso_checksum_fixup)
  173. },
  174. };
  175. static int xenvif_get_sset_count(struct net_device *dev, int string_set)
  176. {
  177. switch (string_set) {
  178. case ETH_SS_STATS:
  179. return ARRAY_SIZE(xenvif_stats);
  180. default:
  181. return -EINVAL;
  182. }
  183. }
  184. static void xenvif_get_ethtool_stats(struct net_device *dev,
  185. struct ethtool_stats *stats, u64 * data)
  186. {
  187. void *vif = netdev_priv(dev);
  188. int i;
  189. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
  190. data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset);
  191. }
  192. static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  193. {
  194. int i;
  195. switch (stringset) {
  196. case ETH_SS_STATS:
  197. for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
  198. memcpy(data + i * ETH_GSTRING_LEN,
  199. xenvif_stats[i].name, ETH_GSTRING_LEN);
  200. break;
  201. }
  202. }
  203. static const struct ethtool_ops xenvif_ethtool_ops = {
  204. .get_link = ethtool_op_get_link,
  205. .get_sset_count = xenvif_get_sset_count,
  206. .get_ethtool_stats = xenvif_get_ethtool_stats,
  207. .get_strings = xenvif_get_strings,
  208. };
  209. static const struct net_device_ops xenvif_netdev_ops = {
  210. .ndo_start_xmit = xenvif_start_xmit,
  211. .ndo_get_stats = xenvif_get_stats,
  212. .ndo_open = xenvif_open,
  213. .ndo_stop = xenvif_close,
  214. .ndo_change_mtu = xenvif_change_mtu,
  215. .ndo_fix_features = xenvif_fix_features,
  216. .ndo_set_mac_address = eth_mac_addr,
  217. .ndo_validate_addr = eth_validate_addr,
  218. };
  219. struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
  220. unsigned int handle)
  221. {
  222. int err;
  223. struct net_device *dev;
  224. struct xenvif *vif;
  225. char name[IFNAMSIZ] = {};
  226. snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
  227. dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup);
  228. if (dev == NULL) {
  229. pr_warn("Could not allocate netdev\n");
  230. return ERR_PTR(-ENOMEM);
  231. }
  232. SET_NETDEV_DEV(dev, parent);
  233. vif = netdev_priv(dev);
  234. vif->domid = domid;
  235. vif->handle = handle;
  236. vif->netbk = NULL;
  237. vif->can_sg = 1;
  238. vif->csum = 1;
  239. atomic_set(&vif->refcnt, 1);
  240. init_waitqueue_head(&vif->waiting_to_free);
  241. vif->dev = dev;
  242. INIT_LIST_HEAD(&vif->schedule_list);
  243. INIT_LIST_HEAD(&vif->notify_list);
  244. vif->credit_bytes = vif->remaining_credit = ~0UL;
  245. vif->credit_usec = 0UL;
  246. init_timer(&vif->credit_timeout);
  247. /* Initialize 'expires' now: it's used to track the credit window. */
  248. vif->credit_timeout.expires = jiffies;
  249. dev->netdev_ops = &xenvif_netdev_ops;
  250. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
  251. dev->features = dev->hw_features;
  252. SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops);
  253. dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
  254. /*
  255. * Initialise a dummy MAC address. We choose the numerically
  256. * largest non-broadcast address to prevent the address getting
  257. * stolen by an Ethernet bridge for STP purposes.
  258. * (FE:FF:FF:FF:FF:FF)
  259. */
  260. memset(dev->dev_addr, 0xFF, ETH_ALEN);
  261. dev->dev_addr[0] &= ~0x01;
  262. netif_carrier_off(dev);
  263. err = register_netdev(dev);
  264. if (err) {
  265. netdev_warn(dev, "Could not register device: err=%d\n", err);
  266. free_netdev(dev);
  267. return ERR_PTR(err);
  268. }
  269. netdev_dbg(dev, "Successfully created xenvif\n");
  270. return vif;
  271. }
  272. int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
  273. unsigned long rx_ring_ref, unsigned int tx_evtchn,
  274. unsigned int rx_evtchn)
  275. {
  276. int err = -ENOMEM;
  277. /* Already connected through? */
  278. if (vif->tx_irq)
  279. return 0;
  280. __module_get(THIS_MODULE);
  281. err = xen_netbk_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref);
  282. if (err < 0)
  283. goto err;
  284. if (tx_evtchn == rx_evtchn) {
  285. /* feature-split-event-channels == 0 */
  286. err = bind_interdomain_evtchn_to_irqhandler(
  287. vif->domid, tx_evtchn, xenvif_interrupt, 0,
  288. vif->dev->name, vif);
  289. if (err < 0)
  290. goto err_unmap;
  291. vif->tx_irq = vif->rx_irq = err;
  292. disable_irq(vif->tx_irq);
  293. } else {
  294. /* feature-split-event-channels == 1 */
  295. snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name),
  296. "%s-tx", vif->dev->name);
  297. err = bind_interdomain_evtchn_to_irqhandler(
  298. vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
  299. vif->tx_irq_name, vif);
  300. if (err < 0)
  301. goto err_unmap;
  302. vif->tx_irq = err;
  303. disable_irq(vif->tx_irq);
  304. snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name),
  305. "%s-rx", vif->dev->name);
  306. err = bind_interdomain_evtchn_to_irqhandler(
  307. vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
  308. vif->rx_irq_name, vif);
  309. if (err < 0)
  310. goto err_tx_unbind;
  311. vif->rx_irq = err;
  312. disable_irq(vif->rx_irq);
  313. }
  314. xenvif_get(vif);
  315. rtnl_lock();
  316. if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
  317. dev_set_mtu(vif->dev, ETH_DATA_LEN);
  318. netdev_update_features(vif->dev);
  319. netif_carrier_on(vif->dev);
  320. if (netif_running(vif->dev))
  321. xenvif_up(vif);
  322. rtnl_unlock();
  323. return 0;
  324. err_tx_unbind:
  325. unbind_from_irqhandler(vif->tx_irq, vif);
  326. vif->tx_irq = 0;
  327. err_unmap:
  328. xen_netbk_unmap_frontend_rings(vif);
  329. err:
  330. module_put(THIS_MODULE);
  331. return err;
  332. }
  333. void xenvif_carrier_off(struct xenvif *vif)
  334. {
  335. struct net_device *dev = vif->dev;
  336. rtnl_lock();
  337. netif_carrier_off(dev); /* discard queued packets */
  338. if (netif_running(dev))
  339. xenvif_down(vif);
  340. rtnl_unlock();
  341. xenvif_put(vif);
  342. }
  343. void xenvif_disconnect(struct xenvif *vif)
  344. {
  345. /* Disconnect funtion might get called by generic framework
  346. * even before vif connects, so we need to check if we really
  347. * need to do a module_put.
  348. */
  349. int need_module_put = 0;
  350. if (netif_carrier_ok(vif->dev))
  351. xenvif_carrier_off(vif);
  352. atomic_dec(&vif->refcnt);
  353. wait_event(vif->waiting_to_free, atomic_read(&vif->refcnt) == 0);
  354. if (vif->tx_irq) {
  355. if (vif->tx_irq == vif->rx_irq)
  356. unbind_from_irqhandler(vif->tx_irq, vif);
  357. else {
  358. unbind_from_irqhandler(vif->tx_irq, vif);
  359. unbind_from_irqhandler(vif->rx_irq, vif);
  360. }
  361. /* vif->irq is valid, we had a module_get in
  362. * xenvif_connect.
  363. */
  364. need_module_put = 1;
  365. }
  366. unregister_netdev(vif->dev);
  367. xen_netbk_unmap_frontend_rings(vif);
  368. free_netdev(vif->dev);
  369. if (need_module_put)
  370. module_put(THIS_MODULE);
  371. }