interface.c 12 KB

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