interface.c 22 KB

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