interface.c 22 KB

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