Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Virtual network driver for conversing with remote driver backends. |
| 3 | * |
| 4 | * Copyright (c) 2002-2005, K A Fraser |
| 5 | * Copyright (c) 2005, XenSource Ltd |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version 2 |
| 9 | * as published by the Free Software Foundation; or, when distributed |
| 10 | * separately from the Linux kernel or incorporated into other |
| 11 | * software packages, subject to the following license: |
| 12 | * |
| 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | * of this source file (the "Software"), to deal in the Software without |
| 15 | * restriction, including without limitation the rights to use, copy, modify, |
| 16 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 17 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 18 | * the following conditions: |
| 19 | * |
| 20 | * The above copyright notice and this permission notice shall be included in |
| 21 | * all copies or substantial portions of the Software. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 29 | * IN THE SOFTWARE. |
| 30 | */ |
| 31 | |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 33 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 34 | #include <linux/module.h> |
| 35 | #include <linux/kernel.h> |
| 36 | #include <linux/netdevice.h> |
| 37 | #include <linux/etherdevice.h> |
| 38 | #include <linux/skbuff.h> |
| 39 | #include <linux/ethtool.h> |
| 40 | #include <linux/if_ether.h> |
Wei Liu | 9ecd1a7 | 2013-04-22 02:20:41 +0000 | [diff] [blame] | 41 | #include <net/tcp.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 42 | #include <linux/udp.h> |
| 43 | #include <linux/moduleparam.h> |
| 44 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 45 | #include <linux/slab.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 46 | #include <net/ip.h> |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 47 | #include <linux/bpf.h> |
| 48 | #include <net/page_pool.h> |
| 49 | #include <linux/bpf_trace.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 50 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 51 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 52 | #include <xen/xenbus.h> |
| 53 | #include <xen/events.h> |
| 54 | #include <xen/page.h> |
Igor Mammedov | b9136d2 | 2012-03-21 15:08:38 +0100 | [diff] [blame] | 55 | #include <xen/platform_pci.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 56 | #include <xen/grant_table.h> |
| 57 | |
| 58 | #include <xen/interface/io/netif.h> |
| 59 | #include <xen/interface/memory.h> |
| 60 | #include <xen/interface/grant_table.h> |
| 61 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 62 | /* Module parameters */ |
Juergen Gross | 034702a | 2017-01-10 14:32:51 +0100 | [diff] [blame] | 63 | #define MAX_QUEUES_DEFAULT 8 |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 64 | static unsigned int xennet_max_queues; |
| 65 | module_param_named(max_queues, xennet_max_queues, uint, 0644); |
| 66 | MODULE_PARM_DESC(max_queues, |
| 67 | "Maximum number of queues per virtual interface"); |
| 68 | |
Andrea Righi | c2c6331 | 2020-07-24 10:59:10 +0200 | [diff] [blame] | 69 | #define XENNET_TIMEOUT (5 * HZ) |
| 70 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 71 | static const struct ethtool_ops xennet_ethtool_ops; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 72 | |
| 73 | struct netfront_cb { |
Ian Campbell | 3683243 | 2012-08-22 00:26:47 +0000 | [diff] [blame] | 74 | int pull_to; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb)) |
| 78 | |
| 79 | #define RX_COPY_THRESHOLD 256 |
| 80 | |
| 81 | #define GRANT_INVALID_REF 0 |
| 82 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 83 | #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE) |
| 84 | #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE) |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 85 | |
| 86 | /* Minimum number of Rx slots (includes slot for GSO metadata). */ |
| 87 | #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 88 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 89 | /* Queue name is interface name with "-qNNN" appended */ |
| 90 | #define QUEUE_NAME_SIZE (IFNAMSIZ + 6) |
| 91 | |
| 92 | /* IRQ name is queue name with "-tx" or "-rx" appended */ |
| 93 | #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3) |
| 94 | |
Juergen Gross | 8edfe2e | 2018-09-07 14:21:30 +0200 | [diff] [blame] | 95 | static DECLARE_WAIT_QUEUE_HEAD(module_wq); |
Eduardo Otubo | 5b5971d | 2017-11-23 15:18:35 +0100 | [diff] [blame] | 96 | |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 97 | struct netfront_stats { |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 98 | u64 packets; |
| 99 | u64 bytes; |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 100 | struct u64_stats_sync syncp; |
| 101 | }; |
| 102 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 103 | struct netfront_info; |
| 104 | |
| 105 | struct netfront_queue { |
| 106 | unsigned int id; /* Queue ID, 0-based */ |
| 107 | char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */ |
| 108 | struct netfront_info *info; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 109 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 110 | struct bpf_prog __rcu *xdp_prog; |
| 111 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 112 | struct napi_struct napi; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 113 | |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 114 | /* Split event channels support, tx_* == rx_* when using |
| 115 | * single event channel. |
| 116 | */ |
| 117 | unsigned int tx_evtchn, rx_evtchn; |
| 118 | unsigned int tx_irq, rx_irq; |
| 119 | /* Only used when split event channels support is enabled */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 120 | char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */ |
| 121 | char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 122 | |
| 123 | spinlock_t tx_lock; |
Jeremy Fitzhardinge | 84284d3 | 2007-10-15 12:59:53 -0700 | [diff] [blame] | 124 | struct xen_netif_tx_front_ring tx; |
| 125 | int tx_ring_ref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 129 | * are linked from tx_skb_freelist through tx_link. |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 130 | */ |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 131 | struct sk_buff *tx_skbs[NET_TX_RING_SIZE]; |
| 132 | unsigned short tx_link[NET_TX_RING_SIZE]; |
| 133 | #define TX_LINK_NONE 0xffff |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 134 | #define TX_PENDING 0xfffe |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 135 | grant_ref_t gref_tx_head; |
| 136 | grant_ref_t grant_tx_ref[NET_TX_RING_SIZE]; |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 137 | struct page *grant_tx_page[NET_TX_RING_SIZE]; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 138 | unsigned tx_skb_freelist; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 139 | unsigned int tx_pend_queue; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 140 | |
Jeremy Fitzhardinge | 84284d3 | 2007-10-15 12:59:53 -0700 | [diff] [blame] | 141 | spinlock_t rx_lock ____cacheline_aligned_in_smp; |
| 142 | struct xen_netif_rx_front_ring rx; |
| 143 | int rx_ring_ref; |
| 144 | |
Jeremy Fitzhardinge | 84284d3 | 2007-10-15 12:59:53 -0700 | [diff] [blame] | 145 | struct timer_list rx_refill_timer; |
| 146 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 147 | struct sk_buff *rx_skbs[NET_RX_RING_SIZE]; |
| 148 | grant_ref_t gref_rx_head; |
| 149 | grant_ref_t grant_rx_ref[NET_RX_RING_SIZE]; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 150 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 151 | unsigned int rx_rsp_unconsumed; |
| 152 | spinlock_t rx_cons_lock; |
| 153 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 154 | struct page_pool *page_pool; |
| 155 | struct xdp_rxq_info xdp_rxq; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | struct netfront_info { |
| 159 | struct list_head list; |
| 160 | struct net_device *netdev; |
| 161 | |
| 162 | struct xenbus_device *xbdev; |
| 163 | |
| 164 | /* Multi-queue support */ |
| 165 | struct netfront_queue *queues; |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 166 | |
| 167 | /* Statistics */ |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 168 | struct netfront_stats __percpu *rx_stats; |
| 169 | struct netfront_stats __percpu *tx_stats; |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 170 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 171 | /* XDP state */ |
| 172 | bool netback_has_xdp_headroom; |
| 173 | bool netfront_xdp_enabled; |
| 174 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 175 | /* Is device behaving sane? */ |
| 176 | bool broken; |
| 177 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 178 | atomic_t rx_gso_checksum_fixup; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | struct netfront_rx_info { |
| 182 | struct xen_netif_rx_response rx; |
| 183 | struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; |
| 184 | }; |
| 185 | |
| 186 | /* |
| 187 | * Access macros for acquiring freeing slots in tx_skbs[]. |
| 188 | */ |
| 189 | |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 190 | static void add_id_to_list(unsigned *head, unsigned short *list, |
| 191 | unsigned short id) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 192 | { |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 193 | list[id] = *head; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 194 | *head = id; |
| 195 | } |
| 196 | |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 197 | static unsigned short get_id_from_list(unsigned *head, unsigned short *list) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 198 | { |
| 199 | unsigned int id = *head; |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 200 | |
| 201 | if (id != TX_LINK_NONE) { |
| 202 | *head = list[id]; |
| 203 | list[id] = TX_LINK_NONE; |
| 204 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 205 | return id; |
| 206 | } |
| 207 | |
| 208 | static int xennet_rxidx(RING_IDX idx) |
| 209 | { |
| 210 | return idx & (NET_RX_RING_SIZE - 1); |
| 211 | } |
| 212 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 213 | static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 214 | RING_IDX ri) |
| 215 | { |
| 216 | int i = xennet_rxidx(ri); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 217 | struct sk_buff *skb = queue->rx_skbs[i]; |
| 218 | queue->rx_skbs[i] = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 219 | return skb; |
| 220 | } |
| 221 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 222 | static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 223 | RING_IDX ri) |
| 224 | { |
| 225 | int i = xennet_rxidx(ri); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 226 | grant_ref_t ref = queue->grant_rx_ref[i]; |
| 227 | queue->grant_rx_ref[i] = GRANT_INVALID_REF; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 228 | return ref; |
| 229 | } |
| 230 | |
| 231 | #ifdef CONFIG_SYSFS |
Takashi Iwai | 27b917e | 2015-02-04 14:38:55 +0100 | [diff] [blame] | 232 | static const struct attribute_group xennet_dev_group; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 233 | #endif |
| 234 | |
Michał Mirosław | 3ad9b35 | 2011-11-16 14:05:33 +0000 | [diff] [blame] | 235 | static bool xennet_can_sg(struct net_device *dev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 236 | { |
Michał Mirosław | 3ad9b35 | 2011-11-16 14:05:33 +0000 | [diff] [blame] | 237 | return dev->features & NETIF_F_SG; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 241 | static void rx_refill_timeout(struct timer_list *t) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 242 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 243 | struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 244 | napi_schedule(&queue->napi); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 247 | static int netfront_tx_slot_available(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 248 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 249 | return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) < |
Juergen Gross | 57f230a | 2018-06-12 08:57:53 +0200 | [diff] [blame] | 250 | (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 253 | static void xennet_maybe_wake_tx(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 254 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 255 | struct net_device *dev = queue->info->netdev; |
| 256 | struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 257 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 258 | if (unlikely(netif_tx_queue_stopped(dev_queue)) && |
| 259 | netfront_tx_slot_available(queue) && |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 260 | likely(netif_running(dev))) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 261 | netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 262 | } |
| 263 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 264 | |
| 265 | static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 266 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 267 | struct sk_buff *skb; |
| 268 | struct page *page; |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 269 | |
| 270 | skb = __netdev_alloc_skb(queue->info->netdev, |
| 271 | RX_COPY_THRESHOLD + NET_IP_ALIGN, |
| 272 | GFP_ATOMIC | __GFP_NOWARN); |
| 273 | if (unlikely(!skb)) |
| 274 | return NULL; |
| 275 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 276 | page = page_pool_dev_alloc_pages(queue->page_pool); |
| 277 | if (unlikely(!page)) { |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 278 | kfree_skb(skb); |
| 279 | return NULL; |
| 280 | } |
| 281 | skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE); |
| 282 | |
| 283 | /* Align ip header to a 16 bytes boundary */ |
| 284 | skb_reserve(skb, NET_IP_ALIGN); |
| 285 | skb->dev = queue->info->netdev; |
| 286 | |
| 287 | return skb; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | static void xennet_alloc_rx_buffers(struct netfront_queue *queue) |
| 292 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 293 | RING_IDX req_prod = queue->rx.req_prod_pvt; |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 294 | int notify; |
Vineeth Remanan Pillai | 538d929 | 2017-02-07 18:59:01 +0000 | [diff] [blame] | 295 | int err = 0; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 296 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 297 | if (unlikely(!netif_carrier_ok(queue->info->netdev))) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 298 | return; |
| 299 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 300 | for (req_prod = queue->rx.req_prod_pvt; |
| 301 | req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE; |
| 302 | req_prod++) { |
| 303 | struct sk_buff *skb; |
| 304 | unsigned short id; |
| 305 | grant_ref_t ref; |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 306 | struct page *page; |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 307 | struct xen_netif_rx_request *req; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 308 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 309 | skb = xennet_alloc_one_rx_buffer(queue); |
Vineeth Remanan Pillai | 538d929 | 2017-02-07 18:59:01 +0000 | [diff] [blame] | 310 | if (!skb) { |
| 311 | err = -ENOMEM; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 312 | break; |
Vineeth Remanan Pillai | 538d929 | 2017-02-07 18:59:01 +0000 | [diff] [blame] | 313 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 314 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 315 | id = xennet_rxidx(req_prod); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 316 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 317 | BUG_ON(queue->rx_skbs[id]); |
| 318 | queue->rx_skbs[id] = skb; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 319 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 320 | ref = gnttab_claim_grant_reference(&queue->gref_rx_head); |
Dongli Zhang | 269ebce | 2016-11-02 09:04:33 +0800 | [diff] [blame] | 321 | WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref)); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 322 | queue->grant_rx_ref[id] = ref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 323 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 324 | page = skb_frag_page(&skb_shinfo(skb)->frags[0]); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 325 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 326 | req = RING_GET_REQUEST(&queue->rx, req_prod); |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 327 | gnttab_page_grant_foreign_access_ref_one(ref, |
| 328 | queue->info->xbdev->otherend_id, |
| 329 | page, |
| 330 | 0); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 331 | req->id = id; |
| 332 | req->gref = ref; |
| 333 | } |
| 334 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 335 | queue->rx.req_prod_pvt = req_prod; |
| 336 | |
Vineeth Remanan Pillai | 538d929 | 2017-02-07 18:59:01 +0000 | [diff] [blame] | 337 | /* Try again later if there are not enough requests or skb allocation |
| 338 | * failed. |
| 339 | * Enough requests is quantified as the sum of newly created slots and |
| 340 | * the unconsumed slots at the backend. |
| 341 | */ |
| 342 | if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN || |
| 343 | unlikely(err)) { |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 344 | mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10)); |
| 345 | return; |
| 346 | } |
| 347 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 348 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 349 | if (notify) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 350 | notify_remote_via_irq(queue->rx_irq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static int xennet_open(struct net_device *dev) |
| 354 | { |
| 355 | struct netfront_info *np = netdev_priv(dev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 356 | unsigned int num_queues = dev->real_num_tx_queues; |
| 357 | unsigned int i = 0; |
| 358 | struct netfront_queue *queue = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 359 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 360 | if (!np->queues || np->broken) |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 361 | return -ENODEV; |
| 362 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 363 | for (i = 0; i < num_queues; ++i) { |
| 364 | queue = &np->queues[i]; |
| 365 | napi_enable(&queue->napi); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 366 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 367 | spin_lock_bh(&queue->rx_lock); |
| 368 | if (netif_carrier_ok(dev)) { |
| 369 | xennet_alloc_rx_buffers(queue); |
| 370 | queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1; |
| 371 | if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)) |
| 372 | napi_schedule(&queue->napi); |
| 373 | } |
| 374 | spin_unlock_bh(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 375 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 376 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 377 | netif_tx_start_all_queues(dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 382 | static bool xennet_tx_buf_gc(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 383 | { |
| 384 | RING_IDX cons, prod; |
| 385 | unsigned short id; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 386 | struct sk_buff *skb; |
Malcolm Crossley | 7d0105b | 2016-01-26 17:12:44 +0000 | [diff] [blame] | 387 | bool more_to_do; |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 388 | bool work_done = false; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 389 | const struct device *dev = &queue->info->netdev->dev; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 390 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 391 | BUG_ON(!netif_carrier_ok(queue->info->netdev)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 392 | |
| 393 | do { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 394 | prod = queue->tx.sring->rsp_prod; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 395 | if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) { |
| 396 | dev_alert(dev, "Illegal number of responses %u\n", |
| 397 | prod - queue->tx.rsp_cons); |
| 398 | goto err; |
| 399 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 400 | rmb(); /* Ensure we see responses up to 'rp'. */ |
| 401 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 402 | for (cons = queue->tx.rsp_cons; cons != prod; cons++) { |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 403 | struct xen_netif_tx_response txrsp; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 404 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 405 | work_done = true; |
| 406 | |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 407 | RING_COPY_RESPONSE(&queue->tx, cons, &txrsp); |
| 408 | if (txrsp.status == XEN_NETIF_RSP_NULL) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 409 | continue; |
| 410 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 411 | id = txrsp.id; |
| 412 | if (id >= RING_SIZE(&queue->tx)) { |
| 413 | dev_alert(dev, |
| 414 | "Response has incorrect id (%u)\n", |
| 415 | id); |
| 416 | goto err; |
| 417 | } |
| 418 | if (queue->tx_link[id] != TX_PENDING) { |
| 419 | dev_alert(dev, |
| 420 | "Response for inactive request\n"); |
| 421 | goto err; |
| 422 | } |
| 423 | |
| 424 | queue->tx_link[id] = TX_LINK_NONE; |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 425 | skb = queue->tx_skbs[id]; |
| 426 | queue->tx_skbs[id] = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 427 | if (unlikely(gnttab_query_foreign_access( |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 428 | queue->grant_tx_ref[id]) != 0)) { |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 429 | dev_alert(dev, |
| 430 | "Grant still in use by backend domain\n"); |
| 431 | goto err; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 432 | } |
| 433 | gnttab_end_foreign_access_ref( |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 434 | queue->grant_tx_ref[id], GNTMAP_readonly); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 435 | gnttab_release_grant_reference( |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 436 | &queue->gref_tx_head, queue->grant_tx_ref[id]); |
| 437 | queue->grant_tx_ref[id] = GRANT_INVALID_REF; |
| 438 | queue->grant_tx_page[id] = NULL; |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 439 | add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 440 | dev_kfree_skb_irq(skb); |
| 441 | } |
| 442 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 443 | queue->tx.rsp_cons = prod; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 444 | |
Malcolm Crossley | 7d0105b | 2016-01-26 17:12:44 +0000 | [diff] [blame] | 445 | RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do); |
| 446 | } while (more_to_do); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 447 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 448 | xennet_maybe_wake_tx(queue); |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 449 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 450 | return work_done; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 451 | |
| 452 | err: |
| 453 | queue->info->broken = true; |
| 454 | dev_alert(dev, "Disabled for further use\n"); |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 455 | |
| 456 | return work_done; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 459 | struct xennet_gnttab_make_txreq { |
| 460 | struct netfront_queue *queue; |
| 461 | struct sk_buff *skb; |
| 462 | struct page *page; |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 463 | struct xen_netif_tx_request *tx; /* Last request on ring page */ |
| 464 | struct xen_netif_tx_request tx_local; /* Last request local copy*/ |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 465 | unsigned int size; |
| 466 | }; |
| 467 | |
| 468 | static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset, |
| 469 | unsigned int len, void *data) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 470 | { |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 471 | struct xennet_gnttab_make_txreq *info = data; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 472 | unsigned int id; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 473 | struct xen_netif_tx_request *tx; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 474 | grant_ref_t ref; |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 475 | /* convenient aliases */ |
| 476 | struct page *page = info->page; |
| 477 | struct netfront_queue *queue = info->queue; |
| 478 | struct sk_buff *skb = info->skb; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 479 | |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 480 | id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link); |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 481 | tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); |
| 482 | ref = gnttab_claim_grant_reference(&queue->gref_tx_head); |
Dongli Zhang | 269ebce | 2016-11-02 09:04:33 +0800 | [diff] [blame] | 483 | WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref)); |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 484 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 485 | gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id, |
| 486 | gfn, GNTMAP_readonly); |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 487 | |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 488 | queue->tx_skbs[id] = skb; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 489 | queue->grant_tx_page[id] = page; |
| 490 | queue->grant_tx_ref[id] = ref; |
| 491 | |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 492 | info->tx_local.id = id; |
| 493 | info->tx_local.gref = ref; |
| 494 | info->tx_local.offset = offset; |
| 495 | info->tx_local.size = len; |
| 496 | info->tx_local.flags = 0; |
| 497 | |
| 498 | *tx = info->tx_local; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 499 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 500 | /* |
| 501 | * Put the request in the pending queue, it will be set to be pending |
| 502 | * when the producer index is about to be raised. |
| 503 | */ |
| 504 | add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id); |
| 505 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 506 | info->tx = tx; |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 507 | info->size += info->tx_local.size; |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static struct xen_netif_tx_request *xennet_make_first_txreq( |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 511 | struct xennet_gnttab_make_txreq *info, |
| 512 | unsigned int offset, unsigned int len) |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 513 | { |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 514 | info->size = 0; |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 515 | |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 516 | gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info); |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 517 | |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 518 | return info->tx; |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset, |
| 522 | unsigned int len, void *data) |
| 523 | { |
| 524 | struct xennet_gnttab_make_txreq *info = data; |
| 525 | |
| 526 | info->tx->flags |= XEN_NETTXF_more_data; |
| 527 | skb_get(info->skb); |
| 528 | xennet_tx_setup_grant(gfn, offset, len, data); |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 531 | static void xennet_make_txreqs( |
| 532 | struct xennet_gnttab_make_txreq *info, |
| 533 | struct page *page, |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 534 | unsigned int offset, unsigned int len) |
| 535 | { |
| 536 | /* Skip unused frames from start of page */ |
| 537 | page += offset >> PAGE_SHIFT; |
| 538 | offset &= ~PAGE_MASK; |
| 539 | |
| 540 | while (len) { |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 541 | info->page = page; |
| 542 | info->size = 0; |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 543 | |
| 544 | gnttab_foreach_grant_in_range(page, offset, len, |
| 545 | xennet_make_one_txreq, |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 546 | info); |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 547 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 548 | page++; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 549 | offset = 0; |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 550 | len -= info->size; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 551 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 554 | /* |
David Vrabel | e84448d | 2015-01-13 17:16:43 +0000 | [diff] [blame] | 555 | * Count how many ring slots are required to send this skb. Each frag |
| 556 | * might be a compound page. |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 557 | */ |
David Vrabel | e84448d | 2015-01-13 17:16:43 +0000 | [diff] [blame] | 558 | static int xennet_count_skb_slots(struct sk_buff *skb) |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 559 | { |
| 560 | int i, frags = skb_shinfo(skb)->nr_frags; |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 561 | int slots; |
David Vrabel | e84448d | 2015-01-13 17:16:43 +0000 | [diff] [blame] | 562 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 563 | slots = gnttab_count_grant(offset_in_page(skb->data), |
| 564 | skb_headlen(skb)); |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 565 | |
| 566 | for (i = 0; i < frags; i++) { |
| 567 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; |
| 568 | unsigned long size = skb_frag_size(frag); |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 569 | unsigned long offset = skb_frag_off(frag); |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 570 | |
| 571 | /* Skip unused frames from start of page */ |
| 572 | offset &= ~PAGE_MASK; |
| 573 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 574 | slots += gnttab_count_grant(offset, size); |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 577 | return slots; |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 580 | static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb, |
Paolo Abeni | a350ecc | 2019-03-20 11:02:06 +0100 | [diff] [blame] | 581 | struct net_device *sb_dev) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 582 | { |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 583 | unsigned int num_queues = dev->real_num_tx_queues; |
| 584 | u32 hash; |
| 585 | u16 queue_idx; |
| 586 | |
| 587 | /* First, check if there is only one queue */ |
| 588 | if (num_queues == 1) { |
| 589 | queue_idx = 0; |
| 590 | } else { |
| 591 | hash = skb_get_hash(skb); |
| 592 | queue_idx = hash % num_queues; |
| 593 | } |
| 594 | |
| 595 | return queue_idx; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 598 | static void xennet_mark_tx_pending(struct netfront_queue *queue) |
| 599 | { |
| 600 | unsigned int i; |
| 601 | |
| 602 | while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) != |
| 603 | TX_LINK_NONE) |
| 604 | queue->tx_link[i] = TX_PENDING; |
| 605 | } |
| 606 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 607 | static int xennet_xdp_xmit_one(struct net_device *dev, |
| 608 | struct netfront_queue *queue, |
| 609 | struct xdp_frame *xdpf) |
| 610 | { |
| 611 | struct netfront_info *np = netdev_priv(dev); |
| 612 | struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats); |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 613 | struct xennet_gnttab_make_txreq info = { |
| 614 | .queue = queue, |
| 615 | .skb = NULL, |
| 616 | .page = virt_to_page(xdpf->data), |
| 617 | }; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 618 | int notify; |
| 619 | |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 620 | xennet_make_first_txreq(&info, |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 621 | offset_in_page(xdpf->data), |
| 622 | xdpf->len); |
| 623 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 624 | xennet_mark_tx_pending(queue); |
| 625 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 626 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); |
| 627 | if (notify) |
| 628 | notify_remote_via_irq(queue->tx_irq); |
| 629 | |
| 630 | u64_stats_update_begin(&tx_stats->syncp); |
| 631 | tx_stats->bytes += xdpf->len; |
| 632 | tx_stats->packets++; |
| 633 | u64_stats_update_end(&tx_stats->syncp); |
| 634 | |
| 635 | xennet_tx_buf_gc(queue); |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int xennet_xdp_xmit(struct net_device *dev, int n, |
| 641 | struct xdp_frame **frames, u32 flags) |
| 642 | { |
| 643 | unsigned int num_queues = dev->real_num_tx_queues; |
| 644 | struct netfront_info *np = netdev_priv(dev); |
| 645 | struct netfront_queue *queue = NULL; |
| 646 | unsigned long irq_flags; |
Lorenzo Bianconi | fdc1397 | 2021-03-08 12:06:58 +0100 | [diff] [blame] | 647 | int nxmit = 0; |
| 648 | int i; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 649 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 650 | if (unlikely(np->broken)) |
| 651 | return -ENODEV; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 652 | if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) |
| 653 | return -EINVAL; |
| 654 | |
| 655 | queue = &np->queues[smp_processor_id() % num_queues]; |
| 656 | |
| 657 | spin_lock_irqsave(&queue->tx_lock, irq_flags); |
| 658 | for (i = 0; i < n; i++) { |
| 659 | struct xdp_frame *xdpf = frames[i]; |
| 660 | |
| 661 | if (!xdpf) |
| 662 | continue; |
Lorenzo Bianconi | fdc1397 | 2021-03-08 12:06:58 +0100 | [diff] [blame] | 663 | if (xennet_xdp_xmit_one(dev, queue, xdpf)) |
| 664 | break; |
| 665 | nxmit++; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 666 | } |
| 667 | spin_unlock_irqrestore(&queue->tx_lock, irq_flags); |
| 668 | |
Lorenzo Bianconi | fdc1397 | 2021-03-08 12:06:58 +0100 | [diff] [blame] | 669 | return nxmit; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 673 | #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1) |
| 674 | |
Luc Van Oostenryck | 24a94b3 | 2018-04-24 15:18:14 +0200 | [diff] [blame] | 675 | static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 676 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 677 | struct netfront_info *np = netdev_priv(dev); |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 678 | struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats); |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 679 | struct xen_netif_tx_request *first_tx; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 680 | unsigned int i; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 681 | int notify; |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 682 | int slots; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 683 | struct page *page; |
| 684 | unsigned int offset; |
| 685 | unsigned int len; |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 686 | unsigned long flags; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 687 | struct netfront_queue *queue = NULL; |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 688 | struct xennet_gnttab_make_txreq info = { }; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 689 | unsigned int num_queues = dev->real_num_tx_queues; |
| 690 | u16 queue_index; |
Vitaly Kuznetsov | fd07160 | 2016-09-19 12:53:40 +0200 | [diff] [blame] | 691 | struct sk_buff *nskb; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 692 | |
| 693 | /* Drop the packet if no queues are set up */ |
| 694 | if (num_queues < 1) |
| 695 | goto drop; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 696 | if (unlikely(np->broken)) |
| 697 | goto drop; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 698 | /* Determine which queue to transmit this SKB on */ |
| 699 | queue_index = skb_get_queue_mapping(skb); |
| 700 | queue = &np->queues[queue_index]; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 701 | |
Wei Liu | 9ecd1a7 | 2013-04-22 02:20:41 +0000 | [diff] [blame] | 702 | /* If skb->len is too big for wire format, drop skb and alert |
| 703 | * user about misconfiguration. |
| 704 | */ |
| 705 | if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) { |
| 706 | net_alert_ratelimited( |
| 707 | "xennet: skb->len = %u, too big for wire format\n", |
| 708 | skb->len); |
| 709 | goto drop; |
| 710 | } |
| 711 | |
David Vrabel | e84448d | 2015-01-13 17:16:43 +0000 | [diff] [blame] | 712 | slots = xennet_count_skb_slots(skb); |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 713 | if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) { |
Zoltan Kiss | 97a6d1b | 2014-08-11 18:32:23 +0100 | [diff] [blame] | 714 | net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n", |
| 715 | slots, skb->len); |
| 716 | if (skb_linearize(skb)) |
| 717 | goto drop; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 718 | } |
| 719 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 720 | page = virt_to_page(skb->data); |
| 721 | offset = offset_in_page(skb->data); |
Vitaly Kuznetsov | fd07160 | 2016-09-19 12:53:40 +0200 | [diff] [blame] | 722 | |
| 723 | /* The first req should be at least ETH_HLEN size or the packet will be |
| 724 | * dropped by netback. |
| 725 | */ |
| 726 | if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) { |
| 727 | nskb = skb_copy(skb, GFP_ATOMIC); |
| 728 | if (!nskb) |
| 729 | goto drop; |
Eric Dumazet | 62f3250 | 2017-08-30 10:32:58 -0700 | [diff] [blame] | 730 | dev_consume_skb_any(skb); |
Vitaly Kuznetsov | fd07160 | 2016-09-19 12:53:40 +0200 | [diff] [blame] | 731 | skb = nskb; |
| 732 | page = virt_to_page(skb->data); |
| 733 | offset = offset_in_page(skb->data); |
| 734 | } |
| 735 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 736 | len = skb_headlen(skb); |
| 737 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 738 | spin_lock_irqsave(&queue->tx_lock, flags); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 739 | |
| 740 | if (unlikely(!netif_carrier_ok(dev) || |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 741 | (slots > 1 && !xennet_can_sg(dev)) || |
Johannes Berg | 8b86a61 | 2015-04-17 15:45:04 +0200 | [diff] [blame] | 742 | netif_needs_gso(skb, netif_skb_features(skb)))) { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 743 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 744 | goto drop; |
| 745 | } |
| 746 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 747 | /* First request for the linear area. */ |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 748 | info.queue = queue; |
| 749 | info.skb = skb; |
| 750 | info.page = page; |
| 751 | first_tx = xennet_make_first_txreq(&info, offset, len); |
| 752 | offset += info.tx_local.size; |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 753 | if (offset == PAGE_SIZE) { |
| 754 | page++; |
| 755 | offset = 0; |
| 756 | } |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 757 | len -= info.tx_local.size; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 758 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 759 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 760 | /* local packet? */ |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 761 | first_tx->flags |= XEN_NETTXF_csum_blank | |
| 762 | XEN_NETTXF_data_validated; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 763 | else if (skb->ip_summed == CHECKSUM_UNNECESSARY) |
| 764 | /* remote but checksummed. */ |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 765 | first_tx->flags |= XEN_NETTXF_data_validated; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 766 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 767 | /* Optional extra info after the first request. */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 768 | if (skb_shinfo(skb)->gso_size) { |
| 769 | struct xen_netif_extra_info *gso; |
| 770 | |
| 771 | gso = (struct xen_netif_extra_info *) |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 772 | RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 773 | |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 774 | first_tx->flags |= XEN_NETTXF_extra_info; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 775 | |
| 776 | gso->u.gso.size = skb_shinfo(skb)->gso_size; |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 777 | gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ? |
| 778 | XEN_NETIF_GSO_TYPE_TCPV6 : |
| 779 | XEN_NETIF_GSO_TYPE_TCPV4; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 780 | gso->u.gso.pad = 0; |
| 781 | gso->u.gso.features = 0; |
| 782 | |
| 783 | gso->type = XEN_NETIF_EXTRA_TYPE_GSO; |
| 784 | gso->flags = 0; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 785 | } |
| 786 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 787 | /* Requests for the rest of the linear area. */ |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 788 | xennet_make_txreqs(&info, page, offset, len); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 789 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 790 | /* Requests for all the frags. */ |
| 791 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 792 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Juergen Gross | 162081ec | 2021-08-24 12:28:07 +0200 | [diff] [blame] | 793 | xennet_make_txreqs(&info, skb_frag_page(frag), |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 794 | skb_frag_off(frag), |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 795 | skb_frag_size(frag)); |
| 796 | } |
| 797 | |
| 798 | /* First request has the packet length. */ |
| 799 | first_tx->size = skb->len; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 800 | |
Daniel Drown | 91ffb9d | 2020-07-03 01:22:34 -0500 | [diff] [blame] | 801 | /* timestamp packet in software */ |
| 802 | skb_tx_timestamp(skb); |
| 803 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 804 | xennet_mark_tx_pending(queue); |
| 805 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 806 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 807 | if (notify) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 808 | notify_remote_via_irq(queue->tx_irq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 809 | |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 810 | u64_stats_update_begin(&tx_stats->syncp); |
| 811 | tx_stats->bytes += skb->len; |
| 812 | tx_stats->packets++; |
| 813 | u64_stats_update_end(&tx_stats->syncp); |
Jeremy Fitzhardinge | 10a273a | 2007-08-13 12:54:37 -0700 | [diff] [blame] | 814 | |
| 815 | /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 816 | xennet_tx_buf_gc(queue); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 817 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 818 | if (!netfront_tx_slot_available(queue)) |
| 819 | netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 820 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 821 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 822 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 823 | return NETDEV_TX_OK; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 824 | |
| 825 | drop: |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 826 | dev->stats.tx_dropped++; |
Eric W. Biederman | 979de8a | 2014-03-15 18:33:04 -0700 | [diff] [blame] | 827 | dev_kfree_skb_any(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 828 | return NETDEV_TX_OK; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | static int xennet_close(struct net_device *dev) |
| 832 | { |
| 833 | struct netfront_info *np = netdev_priv(dev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 834 | unsigned int num_queues = dev->real_num_tx_queues; |
| 835 | unsigned int i; |
| 836 | struct netfront_queue *queue; |
| 837 | netif_tx_stop_all_queues(np->netdev); |
| 838 | for (i = 0; i < num_queues; ++i) { |
| 839 | queue = &np->queues[i]; |
| 840 | napi_disable(&queue->napi); |
| 841 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 845 | static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val) |
| 846 | { |
| 847 | unsigned long flags; |
| 848 | |
| 849 | spin_lock_irqsave(&queue->rx_cons_lock, flags); |
| 850 | queue->rx.rsp_cons = val; |
| 851 | queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx); |
| 852 | spin_unlock_irqrestore(&queue->rx_cons_lock, flags); |
| 853 | } |
| 854 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 855 | static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 856 | grant_ref_t ref) |
| 857 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 858 | int new = xennet_rxidx(queue->rx.req_prod_pvt); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 859 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 860 | BUG_ON(queue->rx_skbs[new]); |
| 861 | queue->rx_skbs[new] = skb; |
| 862 | queue->grant_rx_ref[new] = ref; |
| 863 | RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new; |
| 864 | RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref; |
| 865 | queue->rx.req_prod_pvt++; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 868 | static int xennet_get_extras(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 869 | struct xen_netif_extra_info *extras, |
| 870 | RING_IDX rp) |
| 871 | |
| 872 | { |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 873 | struct xen_netif_extra_info extra; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 874 | struct device *dev = &queue->info->netdev->dev; |
| 875 | RING_IDX cons = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 876 | int err = 0; |
| 877 | |
| 878 | do { |
| 879 | struct sk_buff *skb; |
| 880 | grant_ref_t ref; |
| 881 | |
| 882 | if (unlikely(cons + 1 == rp)) { |
| 883 | if (net_ratelimit()) |
| 884 | dev_warn(dev, "Missing extra info\n"); |
| 885 | err = -EBADR; |
| 886 | break; |
| 887 | } |
| 888 | |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 889 | RING_COPY_RESPONSE(&queue->rx, ++cons, &extra); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 890 | |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 891 | if (unlikely(!extra.type || |
| 892 | extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 893 | if (net_ratelimit()) |
| 894 | dev_warn(dev, "Invalid extra type: %d\n", |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 895 | extra.type); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 896 | err = -EINVAL; |
| 897 | } else { |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 898 | extras[extra.type - 1] = extra; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 901 | skb = xennet_get_rx_skb(queue, cons); |
| 902 | ref = xennet_get_rx_ref(queue, cons); |
| 903 | xennet_move_rx_slot(queue, skb, ref); |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 904 | } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 905 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 906 | xennet_set_rx_rsp_cons(queue, cons); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 907 | return err; |
| 908 | } |
| 909 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 910 | static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata, |
| 911 | struct xen_netif_rx_response *rx, struct bpf_prog *prog, |
| 912 | struct xdp_buff *xdp, bool *need_xdp_flush) |
| 913 | { |
| 914 | struct xdp_frame *xdpf; |
| 915 | u32 len = rx->status; |
Colin Ian King | e44f65f | 2020-07-02 15:22:23 +0100 | [diff] [blame] | 916 | u32 act; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 917 | int err; |
| 918 | |
Lorenzo Bianconi | 43b5169 | 2020-12-22 22:09:28 +0100 | [diff] [blame] | 919 | xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM, |
| 920 | &queue->xdp_rxq); |
Lorenzo Bianconi | be9df4a | 2020-12-22 22:09:29 +0100 | [diff] [blame] | 921 | xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM, |
| 922 | len, false); |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 923 | |
| 924 | act = bpf_prog_run_xdp(prog, xdp); |
| 925 | switch (act) { |
| 926 | case XDP_TX: |
| 927 | get_page(pdata); |
| 928 | xdpf = xdp_convert_buff_to_frame(xdp); |
| 929 | err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0); |
Lorenzo Bianconi | fdc1397 | 2021-03-08 12:06:58 +0100 | [diff] [blame] | 930 | if (unlikely(!err)) |
| 931 | xdp_return_frame_rx_napi(xdpf); |
| 932 | else if (unlikely(err < 0)) |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 933 | trace_xdp_exception(queue->info->netdev, prog, act); |
| 934 | break; |
| 935 | case XDP_REDIRECT: |
| 936 | get_page(pdata); |
| 937 | err = xdp_do_redirect(queue->info->netdev, xdp, prog); |
| 938 | *need_xdp_flush = true; |
| 939 | if (unlikely(err)) |
| 940 | trace_xdp_exception(queue->info->netdev, prog, act); |
| 941 | break; |
| 942 | case XDP_PASS: |
| 943 | case XDP_DROP: |
| 944 | break; |
| 945 | |
| 946 | case XDP_ABORTED: |
| 947 | trace_xdp_exception(queue->info->netdev, prog, act); |
| 948 | break; |
| 949 | |
| 950 | default: |
Paolo Abeni | c8064e5 | 2021-11-30 11:08:07 +0100 | [diff] [blame] | 951 | bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act); |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | return act; |
| 955 | } |
| 956 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 957 | static int xennet_get_responses(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 958 | struct netfront_rx_info *rinfo, RING_IDX rp, |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 959 | struct sk_buff_head *list, |
| 960 | bool *need_xdp_flush) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 961 | { |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 962 | struct xen_netif_rx_response *rx = &rinfo->rx, rx_local; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 963 | int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 964 | RING_IDX cons = queue->rx.rsp_cons; |
| 965 | struct sk_buff *skb = xennet_get_rx_skb(queue, cons); |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 966 | struct xen_netif_extra_info *extras = rinfo->extras; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 967 | grant_ref_t ref = xennet_get_rx_ref(queue, cons); |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 968 | struct device *dev = &queue->info->netdev->dev; |
| 969 | struct bpf_prog *xdp_prog; |
| 970 | struct xdp_buff xdp; |
| 971 | unsigned long ret; |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 972 | int slots = 1; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 973 | int err = 0; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 974 | u32 verdict; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 975 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 976 | if (rx->flags & XEN_NETRXF_extra_info) { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 977 | err = xennet_get_extras(queue, extras, rp); |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 978 | if (!err) { |
| 979 | if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) { |
| 980 | struct xen_netif_extra_info *xdp; |
| 981 | |
| 982 | xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1]; |
| 983 | rx->offset = xdp->u.xdp.headroom; |
| 984 | } |
| 985 | } |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 986 | cons = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | for (;;) { |
| 990 | if (unlikely(rx->status < 0 || |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 991 | rx->offset + rx->status > XEN_PAGE_SIZE)) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 992 | if (net_ratelimit()) |
Julien Grall | 6c10127 | 2015-06-16 20:10:46 +0100 | [diff] [blame] | 993 | dev_warn(dev, "rx->offset: %u, size: %d\n", |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 994 | rx->offset, rx->status); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 995 | xennet_move_rx_slot(queue, skb, ref); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 996 | err = -EINVAL; |
| 997 | goto next; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * This definitely indicates a bug, either in this driver or in |
| 1002 | * the backend driver. In future this should flag the bad |
Wei Liu | 697089d | 2013-04-22 02:20:40 +0000 | [diff] [blame] | 1003 | * situation to the system controller to reboot the backend. |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1004 | */ |
| 1005 | if (ref == GRANT_INVALID_REF) { |
| 1006 | if (net_ratelimit()) |
| 1007 | dev_warn(dev, "Bad rx response id %d.\n", |
| 1008 | rx->id); |
| 1009 | err = -EINVAL; |
| 1010 | goto next; |
| 1011 | } |
| 1012 | |
| 1013 | ret = gnttab_end_foreign_access_ref(ref, 0); |
| 1014 | BUG_ON(!ret); |
| 1015 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1016 | gnttab_release_grant_reference(&queue->gref_rx_head, ref); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1017 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1018 | rcu_read_lock(); |
| 1019 | xdp_prog = rcu_dereference(queue->xdp_prog); |
| 1020 | if (xdp_prog) { |
| 1021 | if (!(rx->flags & XEN_NETRXF_more_data)) { |
| 1022 | /* currently only a single page contains data */ |
| 1023 | verdict = xennet_run_xdp(queue, |
| 1024 | skb_frag_page(&skb_shinfo(skb)->frags[0]), |
| 1025 | rx, xdp_prog, &xdp, need_xdp_flush); |
| 1026 | if (verdict != XDP_PASS) |
| 1027 | err = -EINVAL; |
| 1028 | } else { |
| 1029 | /* drop the frame */ |
| 1030 | err = -EINVAL; |
| 1031 | } |
| 1032 | } |
| 1033 | rcu_read_unlock(); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1034 | next: |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1035 | __skb_queue_tail(list, skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1036 | if (!(rx->flags & XEN_NETRXF_more_data)) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1037 | break; |
| 1038 | |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 1039 | if (cons + slots == rp) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1040 | if (net_ratelimit()) |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 1041 | dev_warn(dev, "Need more slots\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1042 | err = -ENOENT; |
| 1043 | break; |
| 1044 | } |
| 1045 | |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 1046 | RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local); |
| 1047 | rx = &rx_local; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1048 | skb = xennet_get_rx_skb(queue, cons + slots); |
| 1049 | ref = xennet_get_rx_ref(queue, cons + slots); |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 1050 | slots++; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 1053 | if (unlikely(slots > max)) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1054 | if (net_ratelimit()) |
Wei Liu | 697089d | 2013-04-22 02:20:40 +0000 | [diff] [blame] | 1055 | dev_warn(dev, "Too many slots\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1056 | err = -E2BIG; |
| 1057 | } |
| 1058 | |
| 1059 | if (unlikely(err)) |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1060 | xennet_set_rx_rsp_cons(queue, cons + slots); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1061 | |
| 1062 | return err; |
| 1063 | } |
| 1064 | |
| 1065 | static int xennet_set_skb_gso(struct sk_buff *skb, |
| 1066 | struct xen_netif_extra_info *gso) |
| 1067 | { |
| 1068 | if (!gso->u.gso.size) { |
| 1069 | if (net_ratelimit()) |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 1070 | pr_warn("GSO size must not be zero\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1071 | return -EINVAL; |
| 1072 | } |
| 1073 | |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1074 | if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 && |
| 1075 | gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1076 | if (net_ratelimit()) |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 1077 | pr_warn("Bad GSO type %d\n", gso->u.gso.type); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1078 | return -EINVAL; |
| 1079 | } |
| 1080 | |
| 1081 | skb_shinfo(skb)->gso_size = gso->u.gso.size; |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1082 | skb_shinfo(skb)->gso_type = |
| 1083 | (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ? |
| 1084 | SKB_GSO_TCPV4 : |
| 1085 | SKB_GSO_TCPV6; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1086 | |
| 1087 | /* Header must be checked, and gso_segs computed. */ |
| 1088 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 1089 | skb_shinfo(skb)->gso_segs = 0; |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Dongli Zhang | a761129 | 2019-10-01 21:56:41 +0800 | [diff] [blame] | 1094 | static int xennet_fill_frags(struct netfront_queue *queue, |
| 1095 | struct sk_buff *skb, |
| 1096 | struct sk_buff_head *list) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1097 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1098 | RING_IDX cons = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1099 | struct sk_buff *nskb; |
| 1100 | |
| 1101 | while ((nskb = __skb_dequeue(list))) { |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 1102 | struct xen_netif_rx_response rx; |
Ian Campbell | 01c6802 | 2011-10-05 00:28:47 +0000 | [diff] [blame] | 1103 | skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0]; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1104 | |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 1105 | RING_COPY_RESPONSE(&queue->rx, ++cons, &rx); |
| 1106 | |
Juergen Gross | d472b3a | 2018-08-09 16:42:16 +0200 | [diff] [blame] | 1107 | if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) { |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 1108 | unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1109 | |
Juergen Gross | d81c505 | 2018-12-18 16:06:19 +0100 | [diff] [blame] | 1110 | BUG_ON(pull_to < skb_headlen(skb)); |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 1111 | __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); |
| 1112 | } |
Juergen Gross | ad4f15d | 2018-09-11 09:04:48 +0200 | [diff] [blame] | 1113 | if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) { |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1114 | xennet_set_rx_rsp_cons(queue, |
| 1115 | ++cons + skb_queue_len(list)); |
Juergen Gross | ad4f15d | 2018-09-11 09:04:48 +0200 | [diff] [blame] | 1116 | kfree_skb(nskb); |
Dongli Zhang | a761129 | 2019-10-01 21:56:41 +0800 | [diff] [blame] | 1117 | return -ENOENT; |
Juergen Gross | ad4f15d | 2018-09-11 09:04:48 +0200 | [diff] [blame] | 1118 | } |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 1119 | |
Juergen Gross | d472b3a | 2018-08-09 16:42:16 +0200 | [diff] [blame] | 1120 | skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, |
| 1121 | skb_frag_page(nfrag), |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 1122 | rx.offset, rx.status, PAGE_SIZE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1123 | |
| 1124 | skb_shinfo(nskb)->nr_frags = 0; |
| 1125 | kfree_skb(nskb); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1128 | xennet_set_rx_rsp_cons(queue, cons); |
Dongli Zhang | a761129 | 2019-10-01 21:56:41 +0800 | [diff] [blame] | 1129 | |
| 1130 | return 0; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 1133 | static int checksum_setup(struct net_device *dev, struct sk_buff *skb) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1134 | { |
Paul Durrant | b5cf66c | 2014-01-09 10:02:48 +0000 | [diff] [blame] | 1135 | bool recalculate_partial_csum = false; |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* |
| 1138 | * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy |
| 1139 | * peers can fail to set NETRXF_csum_blank when sending a GSO |
| 1140 | * frame. In this case force the SKB to CHECKSUM_PARTIAL and |
| 1141 | * recalculate the partial checksum. |
| 1142 | */ |
| 1143 | if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) { |
| 1144 | struct netfront_info *np = netdev_priv(dev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1145 | atomic_inc(&np->rx_gso_checksum_fixup); |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 1146 | skb->ip_summed = CHECKSUM_PARTIAL; |
Paul Durrant | b5cf66c | 2014-01-09 10:02:48 +0000 | [diff] [blame] | 1147 | recalculate_partial_csum = true; |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ |
| 1151 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 1152 | return 0; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1153 | |
Paul Durrant | b5cf66c | 2014-01-09 10:02:48 +0000 | [diff] [blame] | 1154 | return skb_checksum_setup(skb, recalculate_partial_csum); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1157 | static int handle_incoming_queue(struct netfront_queue *queue, |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1158 | struct sk_buff_head *rxq) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1159 | { |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1160 | struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1161 | int packets_dropped = 0; |
| 1162 | struct sk_buff *skb; |
| 1163 | |
| 1164 | while ((skb = __skb_dequeue(rxq)) != NULL) { |
Ian Campbell | 3683243 | 2012-08-22 00:26:47 +0000 | [diff] [blame] | 1165 | int pull_to = NETFRONT_SKB_CB(skb)->pull_to; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1166 | |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 1167 | if (pull_to > skb_headlen(skb)) |
| 1168 | __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1169 | |
| 1170 | /* Ethernet work: Delayed to here as it peeks the header. */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1171 | skb->protocol = eth_type_trans(skb, queue->info->netdev); |
Wei Liu | d554f73 | 2014-02-19 18:48:34 +0000 | [diff] [blame] | 1172 | skb_reset_network_header(skb); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1173 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1174 | if (checksum_setup(queue->info->netdev, skb)) { |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 1175 | kfree_skb(skb); |
| 1176 | packets_dropped++; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1177 | queue->info->netdev->stats.rx_errors++; |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 1178 | continue; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1181 | u64_stats_update_begin(&rx_stats->syncp); |
| 1182 | rx_stats->packets++; |
| 1183 | rx_stats->bytes += skb->len; |
| 1184 | u64_stats_update_end(&rx_stats->syncp); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1185 | |
| 1186 | /* Pass it up. */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1187 | napi_gro_receive(&queue->napi, skb); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | return packets_dropped; |
| 1191 | } |
| 1192 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1193 | static int xennet_poll(struct napi_struct *napi, int budget) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1194 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1195 | struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi); |
| 1196 | struct net_device *dev = queue->info->netdev; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1197 | struct sk_buff *skb; |
| 1198 | struct netfront_rx_info rinfo; |
| 1199 | struct xen_netif_rx_response *rx = &rinfo.rx; |
| 1200 | struct xen_netif_extra_info *extras = rinfo.extras; |
| 1201 | RING_IDX i, rp; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1202 | int work_done; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1203 | struct sk_buff_head rxq; |
| 1204 | struct sk_buff_head errq; |
| 1205 | struct sk_buff_head tmpq; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1206 | int err; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1207 | bool need_xdp_flush = false; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1208 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1209 | spin_lock(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1210 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1211 | skb_queue_head_init(&rxq); |
| 1212 | skb_queue_head_init(&errq); |
| 1213 | skb_queue_head_init(&tmpq); |
| 1214 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1215 | rp = queue->rx.sring->rsp_prod; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 1216 | if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) { |
| 1217 | dev_alert(&dev->dev, "Illegal number of responses %u\n", |
| 1218 | rp - queue->rx.rsp_cons); |
| 1219 | queue->info->broken = true; |
| 1220 | spin_unlock(&queue->rx_lock); |
| 1221 | return 0; |
| 1222 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1223 | rmb(); /* Ensure we see queued responses up to 'rp'. */ |
| 1224 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1225 | i = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1226 | work_done = 0; |
| 1227 | while ((i != rp) && (work_done < budget)) { |
Juergen Gross | 8446066 | 2021-08-24 12:28:06 +0200 | [diff] [blame] | 1228 | RING_COPY_RESPONSE(&queue->rx, i, rx); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1229 | memset(extras, 0, sizeof(rinfo.extras)); |
| 1230 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1231 | err = xennet_get_responses(queue, &rinfo, rp, &tmpq, |
| 1232 | &need_xdp_flush); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1233 | |
| 1234 | if (unlikely(err)) { |
| 1235 | err: |
| 1236 | while ((skb = __skb_dequeue(&tmpq))) |
| 1237 | __skb_queue_tail(&errq, skb); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1238 | dev->stats.rx_errors++; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1239 | i = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1240 | continue; |
| 1241 | } |
| 1242 | |
| 1243 | skb = __skb_dequeue(&tmpq); |
| 1244 | |
| 1245 | if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) { |
| 1246 | struct xen_netif_extra_info *gso; |
| 1247 | gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1]; |
| 1248 | |
| 1249 | if (unlikely(xennet_set_skb_gso(skb, gso))) { |
| 1250 | __skb_queue_head(&tmpq, skb); |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1251 | xennet_set_rx_rsp_cons(queue, |
| 1252 | queue->rx.rsp_cons + |
| 1253 | skb_queue_len(&tmpq)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1254 | goto err; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Ian Campbell | 3683243 | 2012-08-22 00:26:47 +0000 | [diff] [blame] | 1258 | NETFRONT_SKB_CB(skb)->pull_to = rx->status; |
| 1259 | if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD) |
| 1260 | NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1261 | |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 1262 | skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset); |
Ian Campbell | 3683243 | 2012-08-22 00:26:47 +0000 | [diff] [blame] | 1263 | skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status); |
| 1264 | skb->data_len = rx->status; |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 1265 | skb->len += rx->status; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1266 | |
Dongli Zhang | a761129 | 2019-10-01 21:56:41 +0800 | [diff] [blame] | 1267 | if (unlikely(xennet_fill_frags(queue, skb, &tmpq))) |
Juergen Gross | ad4f15d | 2018-09-11 09:04:48 +0200 | [diff] [blame] | 1268 | goto err; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1269 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1270 | if (rx->flags & XEN_NETRXF_csum_blank) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1271 | skb->ip_summed = CHECKSUM_PARTIAL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1272 | else if (rx->flags & XEN_NETRXF_data_validated) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1273 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1274 | |
| 1275 | __skb_queue_tail(&rxq, skb); |
| 1276 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1277 | i = queue->rx.rsp_cons + 1; |
| 1278 | xennet_set_rx_rsp_cons(queue, i); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1279 | work_done++; |
| 1280 | } |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1281 | if (need_xdp_flush) |
| 1282 | xdp_do_flush(); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1283 | |
Wang Chen | 56cfe5d | 2008-05-22 18:09:06 +0800 | [diff] [blame] | 1284 | __skb_queue_purge(&errq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1285 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1286 | work_done -= handle_incoming_queue(queue, &rxq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1287 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1288 | xennet_alloc_rx_buffers(queue); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1289 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1290 | if (work_done < budget) { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1291 | int more_to_do = 0; |
| 1292 | |
Eric Dumazet | 6ad2016 | 2017-01-30 08:22:01 -0800 | [diff] [blame] | 1293 | napi_complete_done(napi, work_done); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1294 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1295 | RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do); |
David Vrabel | 6a6dc08 | 2014-12-16 18:59:46 +0000 | [diff] [blame] | 1296 | if (more_to_do) |
| 1297 | napi_schedule(napi); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1300 | spin_unlock(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1301 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1302 | return work_done; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | static int xennet_change_mtu(struct net_device *dev, int mtu) |
| 1306 | { |
Jonathan Davies | 0c36820 | 2015-03-31 11:05:15 +0100 | [diff] [blame] | 1307 | int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1308 | |
| 1309 | if (mtu > max) |
| 1310 | return -EINVAL; |
| 1311 | dev->mtu = mtu; |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 1315 | static void xennet_get_stats64(struct net_device *dev, |
| 1316 | struct rtnl_link_stats64 *tot) |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1317 | { |
| 1318 | struct netfront_info *np = netdev_priv(dev); |
| 1319 | int cpu; |
| 1320 | |
| 1321 | for_each_possible_cpu(cpu) { |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1322 | struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu); |
| 1323 | struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu); |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1324 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
| 1325 | unsigned int start; |
| 1326 | |
| 1327 | do { |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1328 | start = u64_stats_fetch_begin_irq(&tx_stats->syncp); |
| 1329 | tx_packets = tx_stats->packets; |
| 1330 | tx_bytes = tx_stats->bytes; |
| 1331 | } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start)); |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1332 | |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1333 | do { |
| 1334 | start = u64_stats_fetch_begin_irq(&rx_stats->syncp); |
| 1335 | rx_packets = rx_stats->packets; |
| 1336 | rx_bytes = rx_stats->bytes; |
| 1337 | } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start)); |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1338 | |
| 1339 | tot->rx_packets += rx_packets; |
| 1340 | tot->tx_packets += tx_packets; |
| 1341 | tot->rx_bytes += rx_bytes; |
| 1342 | tot->tx_bytes += tx_bytes; |
| 1343 | } |
| 1344 | |
| 1345 | tot->rx_errors = dev->stats.rx_errors; |
| 1346 | tot->tx_dropped = dev->stats.tx_dropped; |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1349 | static void xennet_release_tx_bufs(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1350 | { |
| 1351 | struct sk_buff *skb; |
| 1352 | int i; |
| 1353 | |
| 1354 | for (i = 0; i < NET_TX_RING_SIZE; i++) { |
| 1355 | /* Skip over entries which are actually freelist references */ |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 1356 | if (!queue->tx_skbs[i]) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1357 | continue; |
| 1358 | |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 1359 | skb = queue->tx_skbs[i]; |
| 1360 | queue->tx_skbs[i] = NULL; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1361 | get_page(queue->grant_tx_page[i]); |
| 1362 | gnttab_end_foreign_access(queue->grant_tx_ref[i], |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1363 | GNTMAP_readonly, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1364 | (unsigned long)page_address(queue->grant_tx_page[i])); |
| 1365 | queue->grant_tx_page[i] = NULL; |
| 1366 | queue->grant_tx_ref[i] = GRANT_INVALID_REF; |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 1367 | add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1368 | dev_kfree_skb_irq(skb); |
| 1369 | } |
| 1370 | } |
| 1371 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1372 | static void xennet_release_rx_bufs(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1373 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1374 | int id, ref; |
| 1375 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1376 | spin_lock_bh(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1377 | |
| 1378 | for (id = 0; id < NET_RX_RING_SIZE; id++) { |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1379 | struct sk_buff *skb; |
| 1380 | struct page *page; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1381 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1382 | skb = queue->rx_skbs[id]; |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1383 | if (!skb) |
| 1384 | continue; |
| 1385 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1386 | ref = queue->grant_rx_ref[id]; |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1387 | if (ref == GRANT_INVALID_REF) |
| 1388 | continue; |
| 1389 | |
| 1390 | page = skb_frag_page(&skb_shinfo(skb)->frags[0]); |
| 1391 | |
| 1392 | /* gnttab_end_foreign_access() needs a page ref until |
| 1393 | * foreign access is ended (which may be deferred). |
| 1394 | */ |
| 1395 | get_page(page); |
| 1396 | gnttab_end_foreign_access(ref, 0, |
| 1397 | (unsigned long)page_address(page)); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1398 | queue->grant_rx_ref[id] = GRANT_INVALID_REF; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1399 | |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1400 | kfree_skb(skb); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1403 | spin_unlock_bh(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1406 | static netdev_features_t xennet_fix_features(struct net_device *dev, |
| 1407 | netdev_features_t features) |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1408 | { |
| 1409 | struct netfront_info *np = netdev_priv(dev); |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1410 | |
Juergen Gross | 2890ea5 | 2016-10-31 14:58:41 +0100 | [diff] [blame] | 1411 | if (features & NETIF_F_SG && |
| 1412 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0)) |
| 1413 | features &= ~NETIF_F_SG; |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1414 | |
Juergen Gross | 2890ea5 | 2016-10-31 14:58:41 +0100 | [diff] [blame] | 1415 | if (features & NETIF_F_IPV6_CSUM && |
| 1416 | !xenbus_read_unsigned(np->xbdev->otherend, |
| 1417 | "feature-ipv6-csum-offload", 0)) |
| 1418 | features &= ~NETIF_F_IPV6_CSUM; |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1419 | |
Juergen Gross | 2890ea5 | 2016-10-31 14:58:41 +0100 | [diff] [blame] | 1420 | if (features & NETIF_F_TSO && |
| 1421 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0)) |
| 1422 | features &= ~NETIF_F_TSO; |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1423 | |
Juergen Gross | 2890ea5 | 2016-10-31 14:58:41 +0100 | [diff] [blame] | 1424 | if (features & NETIF_F_TSO6 && |
| 1425 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0)) |
| 1426 | features &= ~NETIF_F_TSO6; |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1427 | |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1428 | return features; |
| 1429 | } |
| 1430 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1431 | static int xennet_set_features(struct net_device *dev, |
| 1432 | netdev_features_t features) |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1433 | { |
| 1434 | if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) { |
| 1435 | netdev_info(dev, "Reducing MTU because no SG offload"); |
| 1436 | dev->mtu = ETH_DATA_LEN; |
| 1437 | } |
| 1438 | |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1442 | static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi) |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1443 | { |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1444 | unsigned long flags; |
| 1445 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1446 | if (unlikely(queue->info->broken)) |
| 1447 | return false; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 1448 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1449 | spin_lock_irqsave(&queue->tx_lock, flags); |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1450 | if (xennet_tx_buf_gc(queue)) |
| 1451 | *eoi = 0; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1452 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1453 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1454 | return true; |
| 1455 | } |
| 1456 | |
| 1457 | static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id) |
| 1458 | { |
| 1459 | unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; |
| 1460 | |
| 1461 | if (likely(xennet_handle_tx(dev_id, &eoiflag))) |
| 1462 | xen_irq_lateeoi(irq, eoiflag); |
| 1463 | |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1464 | return IRQ_HANDLED; |
| 1465 | } |
| 1466 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1467 | static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi) |
| 1468 | { |
| 1469 | unsigned int work_queued; |
| 1470 | unsigned long flags; |
| 1471 | |
| 1472 | if (unlikely(queue->info->broken)) |
| 1473 | return false; |
| 1474 | |
| 1475 | spin_lock_irqsave(&queue->rx_cons_lock, flags); |
| 1476 | work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx); |
| 1477 | if (work_queued > queue->rx_rsp_unconsumed) { |
| 1478 | queue->rx_rsp_unconsumed = work_queued; |
| 1479 | *eoi = 0; |
| 1480 | } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) { |
| 1481 | const struct device *dev = &queue->info->netdev->dev; |
| 1482 | |
| 1483 | spin_unlock_irqrestore(&queue->rx_cons_lock, flags); |
| 1484 | dev_alert(dev, "RX producer index going backwards\n"); |
| 1485 | dev_alert(dev, "Disabled for further use\n"); |
| 1486 | queue->info->broken = true; |
| 1487 | return false; |
| 1488 | } |
| 1489 | spin_unlock_irqrestore(&queue->rx_cons_lock, flags); |
| 1490 | |
| 1491 | if (likely(netif_carrier_ok(queue->info->netdev) && work_queued)) |
| 1492 | napi_schedule(&queue->napi); |
| 1493 | |
| 1494 | return true; |
| 1495 | } |
| 1496 | |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1497 | static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id) |
| 1498 | { |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1499 | unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1500 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1501 | if (likely(xennet_handle_rx(dev_id, &eoiflag))) |
| 1502 | xen_irq_lateeoi(irq, eoiflag); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1503 | |
| 1504 | return IRQ_HANDLED; |
| 1505 | } |
| 1506 | |
| 1507 | static irqreturn_t xennet_interrupt(int irq, void *dev_id) |
| 1508 | { |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1509 | unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; |
| 1510 | |
| 1511 | if (xennet_handle_tx(dev_id, &eoiflag) && |
| 1512 | xennet_handle_rx(dev_id, &eoiflag)) |
| 1513 | xen_irq_lateeoi(irq, eoiflag); |
| 1514 | |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1515 | return IRQ_HANDLED; |
| 1516 | } |
| 1517 | |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1518 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1519 | static void xennet_poll_controller(struct net_device *dev) |
| 1520 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1521 | /* Poll each queue */ |
| 1522 | struct netfront_info *info = netdev_priv(dev); |
| 1523 | unsigned int num_queues = dev->real_num_tx_queues; |
| 1524 | unsigned int i; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 1525 | |
| 1526 | if (info->broken) |
| 1527 | return; |
| 1528 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1529 | for (i = 0; i < num_queues; ++i) |
| 1530 | xennet_interrupt(0, &info->queues[i]); |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1531 | } |
| 1532 | #endif |
| 1533 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1534 | #define NETBACK_XDP_HEADROOM_DISABLE 0 |
| 1535 | #define NETBACK_XDP_HEADROOM_ENABLE 1 |
| 1536 | |
| 1537 | static int talk_to_netback_xdp(struct netfront_info *np, int xdp) |
| 1538 | { |
| 1539 | int err; |
| 1540 | unsigned short headroom; |
| 1541 | |
| 1542 | headroom = xdp ? XDP_PACKET_HEADROOM : 0; |
| 1543 | err = xenbus_printf(XBT_NIL, np->xbdev->nodename, |
| 1544 | "xdp-headroom", "%hu", |
| 1545 | headroom); |
| 1546 | if (err) |
| 1547 | pr_warn("Error writing xdp-headroom\n"); |
| 1548 | |
| 1549 | return err; |
| 1550 | } |
| 1551 | |
| 1552 | static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog, |
| 1553 | struct netlink_ext_ack *extack) |
| 1554 | { |
| 1555 | unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM; |
| 1556 | struct netfront_info *np = netdev_priv(dev); |
| 1557 | struct bpf_prog *old_prog; |
| 1558 | unsigned int i, err; |
| 1559 | |
| 1560 | if (dev->mtu > max_mtu) { |
| 1561 | netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu); |
| 1562 | return -EINVAL; |
| 1563 | } |
| 1564 | |
| 1565 | if (!np->netback_has_xdp_headroom) |
| 1566 | return 0; |
| 1567 | |
| 1568 | xenbus_switch_state(np->xbdev, XenbusStateReconfiguring); |
| 1569 | |
| 1570 | err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE : |
| 1571 | NETBACK_XDP_HEADROOM_DISABLE); |
| 1572 | if (err) |
| 1573 | return err; |
| 1574 | |
| 1575 | /* avoid the race with XDP headroom adjustment */ |
| 1576 | wait_event(module_wq, |
| 1577 | xenbus_read_driver_state(np->xbdev->otherend) == |
| 1578 | XenbusStateReconfigured); |
| 1579 | np->netfront_xdp_enabled = true; |
| 1580 | |
| 1581 | old_prog = rtnl_dereference(np->queues[0].xdp_prog); |
| 1582 | |
| 1583 | if (prog) |
| 1584 | bpf_prog_add(prog, dev->real_num_tx_queues); |
| 1585 | |
| 1586 | for (i = 0; i < dev->real_num_tx_queues; ++i) |
| 1587 | rcu_assign_pointer(np->queues[i].xdp_prog, prog); |
| 1588 | |
| 1589 | if (old_prog) |
| 1590 | for (i = 0; i < dev->real_num_tx_queues; ++i) |
| 1591 | bpf_prog_put(old_prog); |
| 1592 | |
| 1593 | xenbus_switch_state(np->xbdev, XenbusStateConnected); |
| 1594 | |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1598 | static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp) |
| 1599 | { |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 1600 | struct netfront_info *np = netdev_priv(dev); |
| 1601 | |
| 1602 | if (np->broken) |
| 1603 | return -ENODEV; |
| 1604 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1605 | switch (xdp->command) { |
| 1606 | case XDP_SETUP_PROG: |
| 1607 | return xennet_xdp_set(dev, xdp->prog, xdp->extack); |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1608 | default: |
| 1609 | return -EINVAL; |
| 1610 | } |
| 1611 | } |
| 1612 | |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1613 | static const struct net_device_ops xennet_netdev_ops = { |
| 1614 | .ndo_open = xennet_open, |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1615 | .ndo_stop = xennet_close, |
| 1616 | .ndo_start_xmit = xennet_start_xmit, |
| 1617 | .ndo_change_mtu = xennet_change_mtu, |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1618 | .ndo_get_stats64 = xennet_get_stats64, |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1619 | .ndo_set_mac_address = eth_mac_addr, |
| 1620 | .ndo_validate_addr = eth_validate_addr, |
Michał Mirosław | fb50793 | 2011-03-31 01:01:35 +0000 | [diff] [blame] | 1621 | .ndo_fix_features = xennet_fix_features, |
| 1622 | .ndo_set_features = xennet_set_features, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1623 | .ndo_select_queue = xennet_select_queue, |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1624 | .ndo_bpf = xennet_xdp, |
| 1625 | .ndo_xdp_xmit = xennet_xdp_xmit, |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1626 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1627 | .ndo_poll_controller = xennet_poll_controller, |
| 1628 | #endif |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1629 | }; |
| 1630 | |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1631 | static void xennet_free_netdev(struct net_device *netdev) |
| 1632 | { |
| 1633 | struct netfront_info *np = netdev_priv(netdev); |
| 1634 | |
| 1635 | free_percpu(np->rx_stats); |
| 1636 | free_percpu(np->tx_stats); |
| 1637 | free_netdev(netdev); |
| 1638 | } |
| 1639 | |
Bill Pemberton | 8e0e46b | 2012-12-03 09:24:22 -0500 | [diff] [blame] | 1640 | static struct net_device *xennet_create_dev(struct xenbus_device *dev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1641 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1642 | int err; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1643 | struct net_device *netdev; |
| 1644 | struct netfront_info *np; |
| 1645 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1646 | netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues); |
Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 1647 | if (!netdev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1648 | return ERR_PTR(-ENOMEM); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1649 | |
| 1650 | np = netdev_priv(netdev); |
| 1651 | np->xbdev = dev; |
| 1652 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1653 | np->queues = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1654 | |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1655 | err = -ENOMEM; |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1656 | np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats); |
| 1657 | if (np->rx_stats == NULL) |
| 1658 | goto exit; |
| 1659 | np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats); |
| 1660 | if (np->tx_stats == NULL) |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1661 | goto exit; |
| 1662 | |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1663 | netdev->netdev_ops = &xennet_netdev_ops; |
| 1664 | |
Michał Mirosław | fb50793 | 2011-03-31 01:01:35 +0000 | [diff] [blame] | 1665 | netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM | |
| 1666 | NETIF_F_GSO_ROBUST; |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1667 | netdev->hw_features = NETIF_F_SG | |
| 1668 | NETIF_F_IPV6_CSUM | |
| 1669 | NETIF_F_TSO | NETIF_F_TSO6; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1670 | |
Ian Campbell | fc3e594 | 2011-04-04 11:07:57 -0700 | [diff] [blame] | 1671 | /* |
| 1672 | * Assume that all hw features are available for now. This set |
| 1673 | * will be adjusted by the call to netdev_update_features() in |
| 1674 | * xennet_connect() which is the earliest point where we can |
| 1675 | * negotiate with the backend regarding supported features. |
| 1676 | */ |
| 1677 | netdev->features |= netdev->hw_features; |
| 1678 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1679 | netdev->ethtool_ops = &xennet_ethtool_ops; |
Mohammed Gamal | e1043a4 | 2017-10-16 15:20:32 +0200 | [diff] [blame] | 1680 | netdev->min_mtu = ETH_MIN_MTU; |
Jarod Wilson | d0c2c99 | 2016-10-20 13:55:21 -0400 | [diff] [blame] | 1681 | netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1682 | SET_NETDEV_DEV(netdev, &dev->dev); |
| 1683 | |
| 1684 | np->netdev = netdev; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1685 | np->netfront_xdp_enabled = false; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1686 | |
| 1687 | netif_carrier_off(netdev); |
| 1688 | |
Andrea Righi | c2c6331 | 2020-07-24 10:59:10 +0200 | [diff] [blame] | 1689 | do { |
| 1690 | xenbus_switch_state(dev, XenbusStateInitialising); |
| 1691 | err = wait_event_timeout(module_wq, |
| 1692 | xenbus_read_driver_state(dev->otherend) != |
| 1693 | XenbusStateClosed && |
| 1694 | xenbus_read_driver_state(dev->otherend) != |
| 1695 | XenbusStateUnknown, XENNET_TIMEOUT); |
| 1696 | } while (!err); |
| 1697 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1698 | return netdev; |
| 1699 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1700 | exit: |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1701 | xennet_free_netdev(netdev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1702 | return ERR_PTR(err); |
| 1703 | } |
| 1704 | |
Lee Jones | 8070860 | 2021-01-15 20:09:03 +0000 | [diff] [blame] | 1705 | /* |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1706 | * Entry point to this code when a new device is created. Allocate the basic |
| 1707 | * structures and the ring buffers for communication with the backend, and |
| 1708 | * inform the backend of the appropriate details for those. |
| 1709 | */ |
Bill Pemberton | 8e0e46b | 2012-12-03 09:24:22 -0500 | [diff] [blame] | 1710 | static int netfront_probe(struct xenbus_device *dev, |
Greg Kroah-Hartman | 1dd06ae | 2012-12-06 14:30:56 +0000 | [diff] [blame] | 1711 | const struct xenbus_device_id *id) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1712 | { |
| 1713 | int err; |
| 1714 | struct net_device *netdev; |
| 1715 | struct netfront_info *info; |
| 1716 | |
| 1717 | netdev = xennet_create_dev(dev); |
| 1718 | if (IS_ERR(netdev)) { |
| 1719 | err = PTR_ERR(netdev); |
| 1720 | xenbus_dev_fatal(dev, err, "creating netdev"); |
| 1721 | return err; |
| 1722 | } |
| 1723 | |
| 1724 | info = netdev_priv(netdev); |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1725 | dev_set_drvdata(&dev->dev, info); |
Takashi Iwai | 27b917e | 2015-02-04 14:38:55 +0100 | [diff] [blame] | 1726 | #ifdef CONFIG_SYSFS |
| 1727 | info->netdev->sysfs_groups[0] = &xennet_dev_group; |
| 1728 | #endif |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1729 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1730 | return 0; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | static void xennet_end_access(int ref, void *page) |
| 1734 | { |
| 1735 | /* This frees the page as a side-effect */ |
| 1736 | if (ref != GRANT_INVALID_REF) |
| 1737 | gnttab_end_foreign_access(ref, 0, (unsigned long)page); |
| 1738 | } |
| 1739 | |
| 1740 | static void xennet_disconnect_backend(struct netfront_info *info) |
| 1741 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1742 | unsigned int i = 0; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1743 | unsigned int num_queues = info->netdev->real_num_tx_queues; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1744 | |
David Vrabel | f9feb1e | 2014-07-02 16:09:15 +0100 | [diff] [blame] | 1745 | netif_carrier_off(info->netdev); |
| 1746 | |
Chas Williams | 9a873c7 | 2015-08-19 19:14:20 -0400 | [diff] [blame] | 1747 | for (i = 0; i < num_queues && info->queues; ++i) { |
David Vrabel | 7654186 | 2014-06-18 10:47:27 +0100 | [diff] [blame] | 1748 | struct netfront_queue *queue = &info->queues[i]; |
| 1749 | |
Boris Ostrovsky | 7447095 | 2017-01-30 12:45:46 -0500 | [diff] [blame] | 1750 | del_timer_sync(&queue->rx_refill_timer); |
| 1751 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1752 | if (queue->tx_irq && (queue->tx_irq == queue->rx_irq)) |
| 1753 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 1754 | if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) { |
| 1755 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 1756 | unbind_from_irqhandler(queue->rx_irq, queue); |
| 1757 | } |
| 1758 | queue->tx_evtchn = queue->rx_evtchn = 0; |
| 1759 | queue->tx_irq = queue->rx_irq = 0; |
| 1760 | |
Chas Williams | 274b045 | 2015-08-27 12:28:46 -0400 | [diff] [blame] | 1761 | if (netif_running(info->netdev)) |
| 1762 | napi_synchronize(&queue->napi); |
David Vrabel | f9feb1e | 2014-07-02 16:09:15 +0100 | [diff] [blame] | 1763 | |
David Vrabel | a5b5dc3 | 2014-07-31 17:38:23 +0100 | [diff] [blame] | 1764 | xennet_release_tx_bufs(queue); |
| 1765 | xennet_release_rx_bufs(queue); |
| 1766 | gnttab_free_grant_references(queue->gref_tx_head); |
| 1767 | gnttab_free_grant_references(queue->gref_rx_head); |
| 1768 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1769 | /* End access and free the pages */ |
| 1770 | xennet_end_access(queue->tx_ring_ref, queue->tx.sring); |
| 1771 | xennet_end_access(queue->rx_ring_ref, queue->rx.sring); |
| 1772 | |
| 1773 | queue->tx_ring_ref = GRANT_INVALID_REF; |
| 1774 | queue->rx_ring_ref = GRANT_INVALID_REF; |
| 1775 | queue->tx.sring = NULL; |
| 1776 | queue->rx.sring = NULL; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 1777 | |
| 1778 | page_pool_destroy(queue->page_pool); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1779 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Lee Jones | 8070860 | 2021-01-15 20:09:03 +0000 | [diff] [blame] | 1782 | /* |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1783 | * We are reconnecting to the backend, due to a suspend/resume, or a backend |
| 1784 | * driver restart. We tear down our netif structure and recreate it, but |
| 1785 | * leave the device-layer structures intact so that this is transparent to the |
| 1786 | * rest of the kernel. |
| 1787 | */ |
| 1788 | static int netfront_resume(struct xenbus_device *dev) |
| 1789 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1790 | struct netfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1791 | |
| 1792 | dev_dbg(&dev->dev, "%s\n", dev->nodename); |
| 1793 | |
Dongli Zhang | 042b204 | 2021-10-22 16:31:39 -0700 | [diff] [blame] | 1794 | netif_tx_lock_bh(info->netdev); |
| 1795 | netif_device_detach(info->netdev); |
| 1796 | netif_tx_unlock_bh(info->netdev); |
| 1797 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1798 | xennet_disconnect_backend(info); |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[]) |
| 1803 | { |
| 1804 | char *s, *e, *macstr; |
| 1805 | int i; |
| 1806 | |
| 1807 | macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL); |
| 1808 | if (IS_ERR(macstr)) |
| 1809 | return PTR_ERR(macstr); |
| 1810 | |
| 1811 | for (i = 0; i < ETH_ALEN; i++) { |
| 1812 | mac[i] = simple_strtoul(s, &e, 16); |
| 1813 | if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) { |
| 1814 | kfree(macstr); |
| 1815 | return -ENOENT; |
| 1816 | } |
| 1817 | s = e+1; |
| 1818 | } |
| 1819 | |
| 1820 | kfree(macstr); |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1824 | static int setup_netfront_single(struct netfront_queue *queue) |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1825 | { |
| 1826 | int err; |
| 1827 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1828 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1829 | if (err < 0) |
| 1830 | goto fail; |
| 1831 | |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1832 | err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn, |
| 1833 | xennet_interrupt, 0, |
| 1834 | queue->info->netdev->name, |
| 1835 | queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1836 | if (err < 0) |
| 1837 | goto bind_fail; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1838 | queue->rx_evtchn = queue->tx_evtchn; |
| 1839 | queue->rx_irq = queue->tx_irq = err; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1840 | |
| 1841 | return 0; |
| 1842 | |
| 1843 | bind_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1844 | xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); |
| 1845 | queue->tx_evtchn = 0; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1846 | fail: |
| 1847 | return err; |
| 1848 | } |
| 1849 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1850 | static int setup_netfront_split(struct netfront_queue *queue) |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1851 | { |
| 1852 | int err; |
| 1853 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1854 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1855 | if (err < 0) |
| 1856 | goto fail; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1857 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1858 | if (err < 0) |
| 1859 | goto alloc_rx_evtchn_fail; |
| 1860 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1861 | snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name), |
| 1862 | "%s-tx", queue->name); |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1863 | err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn, |
| 1864 | xennet_tx_interrupt, 0, |
| 1865 | queue->tx_irq_name, queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1866 | if (err < 0) |
| 1867 | goto bind_tx_fail; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1868 | queue->tx_irq = err; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1869 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1870 | snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name), |
| 1871 | "%s-rx", queue->name); |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1872 | err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn, |
| 1873 | xennet_rx_interrupt, 0, |
| 1874 | queue->rx_irq_name, queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1875 | if (err < 0) |
| 1876 | goto bind_rx_fail; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1877 | queue->rx_irq = err; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1878 | |
| 1879 | return 0; |
| 1880 | |
| 1881 | bind_rx_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1882 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 1883 | queue->tx_irq = 0; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1884 | bind_tx_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1885 | xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn); |
| 1886 | queue->rx_evtchn = 0; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1887 | alloc_rx_evtchn_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1888 | xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); |
| 1889 | queue->tx_evtchn = 0; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1890 | fail: |
| 1891 | return err; |
| 1892 | } |
| 1893 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1894 | static int setup_netfront(struct xenbus_device *dev, |
| 1895 | struct netfront_queue *queue, unsigned int feature_split_evtchn) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1896 | { |
| 1897 | struct xen_netif_tx_sring *txs; |
| 1898 | struct xen_netif_rx_sring *rxs; |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1899 | grant_ref_t gref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1900 | int err; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1901 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1902 | queue->tx_ring_ref = GRANT_INVALID_REF; |
| 1903 | queue->rx_ring_ref = GRANT_INVALID_REF; |
| 1904 | queue->rx.sring = NULL; |
| 1905 | queue->tx.sring = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1906 | |
Ian Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 1907 | txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1908 | if (!txs) { |
| 1909 | err = -ENOMEM; |
| 1910 | xenbus_dev_fatal(dev, err, "allocating tx ring page"); |
| 1911 | goto fail; |
| 1912 | } |
| 1913 | SHARED_RING_INIT(txs); |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 1914 | FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1915 | |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1916 | err = xenbus_grant_ring(dev, txs, 1, &gref); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1917 | if (err < 0) |
| 1918 | goto grant_tx_ring_fail; |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1919 | queue->tx_ring_ref = gref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1920 | |
Ian Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 1921 | rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1922 | if (!rxs) { |
| 1923 | err = -ENOMEM; |
| 1924 | xenbus_dev_fatal(dev, err, "allocating rx ring page"); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1925 | goto alloc_rx_ring_fail; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1926 | } |
| 1927 | SHARED_RING_INIT(rxs); |
Julien Grall | 30c5d7f | 2015-04-10 14:42:21 +0100 | [diff] [blame] | 1928 | FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1929 | |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1930 | err = xenbus_grant_ring(dev, rxs, 1, &gref); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1931 | if (err < 0) |
| 1932 | goto grant_rx_ring_fail; |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1933 | queue->rx_ring_ref = gref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1934 | |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1935 | if (feature_split_evtchn) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1936 | err = setup_netfront_split(queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1937 | /* setup single event channel if |
| 1938 | * a) feature-split-event-channels == 0 |
| 1939 | * b) feature-split-event-channels == 1 but failed to setup |
| 1940 | */ |
Jiapeng Chong | e93fac3 | 2021-02-02 18:17:49 +0800 | [diff] [blame] | 1941 | if (!feature_split_evtchn || err) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1942 | err = setup_netfront_single(queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1943 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1944 | if (err) |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1945 | goto alloc_evtchn_fail; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1946 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1947 | return 0; |
| 1948 | |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1949 | /* If we fail to setup netfront, it is safe to just revoke access to |
| 1950 | * granted pages because backend is not accessing it at this point. |
| 1951 | */ |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1952 | alloc_evtchn_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1953 | gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1954 | grant_rx_ring_fail: |
| 1955 | free_page((unsigned long)rxs); |
| 1956 | alloc_rx_ring_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1957 | gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1958 | grant_tx_ring_fail: |
| 1959 | free_page((unsigned long)txs); |
| 1960 | fail: |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1961 | return err; |
| 1962 | } |
| 1963 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1964 | /* Queue-specific initialisation |
| 1965 | * This used to be done in xennet_create_dev() but must now |
| 1966 | * be run per-queue. |
| 1967 | */ |
| 1968 | static int xennet_init_queue(struct netfront_queue *queue) |
| 1969 | { |
| 1970 | unsigned short i; |
| 1971 | int err = 0; |
Xiao Liang | 21f2706 | 2018-08-14 23:21:28 +0800 | [diff] [blame] | 1972 | char *devid; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1973 | |
| 1974 | spin_lock_init(&queue->tx_lock); |
| 1975 | spin_lock_init(&queue->rx_lock); |
Juergen Gross | b27d479 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1976 | spin_lock_init(&queue->rx_cons_lock); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1977 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1978 | timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1979 | |
Xiao Liang | 21f2706 | 2018-08-14 23:21:28 +0800 | [diff] [blame] | 1980 | devid = strrchr(queue->info->xbdev->nodename, '/') + 1; |
| 1981 | snprintf(queue->name, sizeof(queue->name), "vif%s-q%u", |
| 1982 | devid, queue->id); |
Wei Liu | 8b71501 | 2014-06-04 10:30:47 +0100 | [diff] [blame] | 1983 | |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 1984 | /* Initialise tx_skb_freelist as a free chain containing every entry. */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1985 | queue->tx_skb_freelist = 0; |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 1986 | queue->tx_pend_queue = TX_LINK_NONE; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1987 | for (i = 0; i < NET_TX_RING_SIZE; i++) { |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 1988 | queue->tx_link[i] = i + 1; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1989 | queue->grant_tx_ref[i] = GRANT_INVALID_REF; |
| 1990 | queue->grant_tx_page[i] = NULL; |
| 1991 | } |
Juergen Gross | 21631d2 | 2021-08-24 12:28:08 +0200 | [diff] [blame] | 1992 | queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1993 | |
| 1994 | /* Clear out rx_skbs */ |
| 1995 | for (i = 0; i < NET_RX_RING_SIZE; i++) { |
| 1996 | queue->rx_skbs[i] = NULL; |
| 1997 | queue->grant_rx_ref[i] = GRANT_INVALID_REF; |
| 1998 | } |
| 1999 | |
| 2000 | /* A grant for every tx ring slot */ |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2001 | if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2002 | &queue->gref_tx_head) < 0) { |
| 2003 | pr_alert("can't alloc tx grant refs\n"); |
| 2004 | err = -ENOMEM; |
| 2005 | goto exit; |
| 2006 | } |
| 2007 | |
| 2008 | /* A grant for every rx ring slot */ |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2009 | if (gnttab_alloc_grant_references(NET_RX_RING_SIZE, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2010 | &queue->gref_rx_head) < 0) { |
| 2011 | pr_alert("can't alloc rx grant refs\n"); |
| 2012 | err = -ENOMEM; |
| 2013 | goto exit_free_tx; |
| 2014 | } |
| 2015 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2016 | return 0; |
| 2017 | |
| 2018 | exit_free_tx: |
| 2019 | gnttab_free_grant_references(queue->gref_tx_head); |
| 2020 | exit: |
| 2021 | return err; |
| 2022 | } |
| 2023 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2024 | static int write_queue_xenstore_keys(struct netfront_queue *queue, |
| 2025 | struct xenbus_transaction *xbt, int write_hierarchical) |
| 2026 | { |
| 2027 | /* Write the queue-specific keys into XenStore in the traditional |
| 2028 | * way for a single queue, or in a queue subkeys for multiple |
| 2029 | * queues. |
| 2030 | */ |
| 2031 | struct xenbus_device *dev = queue->info->xbdev; |
| 2032 | int err; |
| 2033 | const char *message; |
| 2034 | char *path; |
| 2035 | size_t pathsize; |
| 2036 | |
| 2037 | /* Choose the correct place to write the keys */ |
| 2038 | if (write_hierarchical) { |
| 2039 | pathsize = strlen(dev->nodename) + 10; |
| 2040 | path = kzalloc(pathsize, GFP_KERNEL); |
| 2041 | if (!path) { |
| 2042 | err = -ENOMEM; |
| 2043 | message = "out of memory while writing ring references"; |
| 2044 | goto error; |
| 2045 | } |
| 2046 | snprintf(path, pathsize, "%s/queue-%u", |
| 2047 | dev->nodename, queue->id); |
| 2048 | } else { |
| 2049 | path = (char *)dev->nodename; |
| 2050 | } |
| 2051 | |
| 2052 | /* Write ring references */ |
| 2053 | err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u", |
| 2054 | queue->tx_ring_ref); |
| 2055 | if (err) { |
| 2056 | message = "writing tx-ring-ref"; |
| 2057 | goto error; |
| 2058 | } |
| 2059 | |
| 2060 | err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u", |
| 2061 | queue->rx_ring_ref); |
| 2062 | if (err) { |
| 2063 | message = "writing rx-ring-ref"; |
| 2064 | goto error; |
| 2065 | } |
| 2066 | |
| 2067 | /* Write event channels; taking into account both shared |
| 2068 | * and split event channel scenarios. |
| 2069 | */ |
| 2070 | if (queue->tx_evtchn == queue->rx_evtchn) { |
| 2071 | /* Shared event channel */ |
| 2072 | err = xenbus_printf(*xbt, path, |
| 2073 | "event-channel", "%u", queue->tx_evtchn); |
| 2074 | if (err) { |
| 2075 | message = "writing event-channel"; |
| 2076 | goto error; |
| 2077 | } |
| 2078 | } else { |
| 2079 | /* Split event channels */ |
| 2080 | err = xenbus_printf(*xbt, path, |
| 2081 | "event-channel-tx", "%u", queue->tx_evtchn); |
| 2082 | if (err) { |
| 2083 | message = "writing event-channel-tx"; |
| 2084 | goto error; |
| 2085 | } |
| 2086 | |
| 2087 | err = xenbus_printf(*xbt, path, |
| 2088 | "event-channel-rx", "%u", queue->rx_evtchn); |
| 2089 | if (err) { |
| 2090 | message = "writing event-channel-rx"; |
| 2091 | goto error; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | if (write_hierarchical) |
| 2096 | kfree(path); |
| 2097 | return 0; |
| 2098 | |
| 2099 | error: |
| 2100 | if (write_hierarchical) |
| 2101 | kfree(path); |
| 2102 | xenbus_dev_fatal(dev, err, "%s", message); |
| 2103 | return err; |
| 2104 | } |
| 2105 | |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2106 | static void xennet_destroy_queues(struct netfront_info *info) |
| 2107 | { |
| 2108 | unsigned int i; |
| 2109 | |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2110 | for (i = 0; i < info->netdev->real_num_tx_queues; i++) { |
| 2111 | struct netfront_queue *queue = &info->queues[i]; |
| 2112 | |
| 2113 | if (netif_running(info->netdev)) |
| 2114 | napi_disable(&queue->napi); |
| 2115 | netif_napi_del(&queue->napi); |
| 2116 | } |
| 2117 | |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2118 | kfree(info->queues); |
| 2119 | info->queues = NULL; |
| 2120 | } |
| 2121 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 2122 | |
| 2123 | |
| 2124 | static int xennet_create_page_pool(struct netfront_queue *queue) |
| 2125 | { |
| 2126 | int err; |
| 2127 | struct page_pool_params pp_params = { |
| 2128 | .order = 0, |
| 2129 | .flags = 0, |
| 2130 | .pool_size = NET_RX_RING_SIZE, |
| 2131 | .nid = NUMA_NO_NODE, |
| 2132 | .dev = &queue->info->netdev->dev, |
| 2133 | .offset = XDP_PACKET_HEADROOM, |
| 2134 | .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM, |
| 2135 | }; |
| 2136 | |
| 2137 | queue->page_pool = page_pool_create(&pp_params); |
| 2138 | if (IS_ERR(queue->page_pool)) { |
| 2139 | err = PTR_ERR(queue->page_pool); |
| 2140 | queue->page_pool = NULL; |
| 2141 | return err; |
| 2142 | } |
| 2143 | |
| 2144 | err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev, |
Björn Töpel | b02e5a0 | 2020-11-30 19:52:01 +0100 | [diff] [blame] | 2145 | queue->id, 0); |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 2146 | if (err) { |
| 2147 | netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n"); |
| 2148 | goto err_free_pp; |
| 2149 | } |
| 2150 | |
| 2151 | err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq, |
| 2152 | MEM_TYPE_PAGE_POOL, queue->page_pool); |
| 2153 | if (err) { |
| 2154 | netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n"); |
| 2155 | goto err_unregister_rxq; |
| 2156 | } |
| 2157 | return 0; |
| 2158 | |
| 2159 | err_unregister_rxq: |
| 2160 | xdp_rxq_info_unreg(&queue->xdp_rxq); |
| 2161 | err_free_pp: |
| 2162 | page_pool_destroy(queue->page_pool); |
| 2163 | queue->page_pool = NULL; |
| 2164 | return err; |
| 2165 | } |
| 2166 | |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2167 | static int xennet_create_queues(struct netfront_info *info, |
Joe Jin | ca88ea1 | 2015-10-19 13:37:17 +0800 | [diff] [blame] | 2168 | unsigned int *num_queues) |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2169 | { |
| 2170 | unsigned int i; |
| 2171 | int ret; |
| 2172 | |
Joe Jin | ca88ea1 | 2015-10-19 13:37:17 +0800 | [diff] [blame] | 2173 | info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue), |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2174 | GFP_KERNEL); |
| 2175 | if (!info->queues) |
| 2176 | return -ENOMEM; |
| 2177 | |
Joe Jin | ca88ea1 | 2015-10-19 13:37:17 +0800 | [diff] [blame] | 2178 | for (i = 0; i < *num_queues; i++) { |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2179 | struct netfront_queue *queue = &info->queues[i]; |
| 2180 | |
| 2181 | queue->id = i; |
| 2182 | queue->info = info; |
| 2183 | |
| 2184 | ret = xennet_init_queue(queue); |
| 2185 | if (ret < 0) { |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2186 | dev_warn(&info->xbdev->dev, |
David Vrabel | 69cb852 | 2014-07-31 17:38:24 +0100 | [diff] [blame] | 2187 | "only created %d queues\n", i); |
Joe Jin | ca88ea1 | 2015-10-19 13:37:17 +0800 | [diff] [blame] | 2188 | *num_queues = i; |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2189 | break; |
| 2190 | } |
| 2191 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 2192 | /* use page pool recycling instead of buddy allocator */ |
| 2193 | ret = xennet_create_page_pool(queue); |
| 2194 | if (ret < 0) { |
| 2195 | dev_err(&info->xbdev->dev, "can't allocate page pool\n"); |
| 2196 | *num_queues = i; |
| 2197 | return ret; |
| 2198 | } |
| 2199 | |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2200 | netif_napi_add(queue->info->netdev, &queue->napi, |
| 2201 | xennet_poll, 64); |
| 2202 | if (netif_running(info->netdev)) |
| 2203 | napi_enable(&queue->napi); |
| 2204 | } |
| 2205 | |
Joe Jin | ca88ea1 | 2015-10-19 13:37:17 +0800 | [diff] [blame] | 2206 | netif_set_real_num_tx_queues(info->netdev, *num_queues); |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2207 | |
Joe Jin | ca88ea1 | 2015-10-19 13:37:17 +0800 | [diff] [blame] | 2208 | if (*num_queues == 0) { |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2209 | dev_err(&info->xbdev->dev, "no queues\n"); |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2210 | return -EINVAL; |
| 2211 | } |
| 2212 | return 0; |
| 2213 | } |
| 2214 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2215 | /* Common code used when first setting up, and when resuming. */ |
Ian Campbell | f502bf2 | 2010-08-18 23:27:49 +0000 | [diff] [blame] | 2216 | static int talk_to_netback(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2217 | struct netfront_info *info) |
| 2218 | { |
| 2219 | const char *message; |
| 2220 | struct xenbus_transaction xbt; |
| 2221 | int err; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2222 | unsigned int feature_split_evtchn; |
| 2223 | unsigned int i = 0; |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2224 | unsigned int max_queues = 0; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2225 | struct netfront_queue *queue = NULL; |
| 2226 | unsigned int num_queues = 1; |
Jakub Kicinski | 9377211 | 2021-10-21 06:12:03 -0700 | [diff] [blame] | 2227 | u8 addr[ETH_ALEN]; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2228 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2229 | info->netdev->irq = 0; |
| 2230 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2231 | /* Check if backend supports multiple queues */ |
Juergen Gross | 2890ea5 | 2016-10-31 14:58:41 +0100 | [diff] [blame] | 2232 | max_queues = xenbus_read_unsigned(info->xbdev->otherend, |
| 2233 | "multi-queue-max-queues", 1); |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2234 | num_queues = min(max_queues, xennet_max_queues); |
| 2235 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2236 | /* Check feature-split-event-channels */ |
Juergen Gross | 2890ea5 | 2016-10-31 14:58:41 +0100 | [diff] [blame] | 2237 | feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend, |
| 2238 | "feature-split-event-channels", 0); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2239 | |
| 2240 | /* Read mac addr. */ |
Jakub Kicinski | 9377211 | 2021-10-21 06:12:03 -0700 | [diff] [blame] | 2241 | err = xen_net_read_mac(dev, addr); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2242 | if (err) { |
| 2243 | xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename); |
Ross Lagerwall | cb25778 | 2018-06-21 14:00:20 +0100 | [diff] [blame] | 2244 | goto out_unlocked; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2245 | } |
Jakub Kicinski | 9377211 | 2021-10-21 06:12:03 -0700 | [diff] [blame] | 2246 | eth_hw_addr_set(info->netdev, addr); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2247 | |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 2248 | info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend, |
| 2249 | "feature-xdp-headroom", 0); |
| 2250 | if (info->netback_has_xdp_headroom) { |
| 2251 | /* set the current xen-netfront xdp state */ |
| 2252 | err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ? |
| 2253 | NETBACK_XDP_HEADROOM_ENABLE : |
| 2254 | NETBACK_XDP_HEADROOM_DISABLE); |
| 2255 | if (err) |
| 2256 | goto out_unlocked; |
| 2257 | } |
| 2258 | |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2259 | rtnl_lock(); |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 2260 | if (info->queues) |
| 2261 | xennet_destroy_queues(info); |
| 2262 | |
Juergen Gross | a884daa | 2021-08-24 12:28:09 +0200 | [diff] [blame] | 2263 | /* For the case of a reconnect reset the "broken" indicator. */ |
| 2264 | info->broken = false; |
| 2265 | |
Joe Jin | ca88ea1 | 2015-10-19 13:37:17 +0800 | [diff] [blame] | 2266 | err = xennet_create_queues(info, &num_queues); |
Ross Lagerwall | e2e004a | 2017-02-08 10:57:37 +0000 | [diff] [blame] | 2267 | if (err < 0) { |
| 2268 | xenbus_dev_fatal(dev, err, "creating queues"); |
| 2269 | kfree(info->queues); |
| 2270 | info->queues = NULL; |
| 2271 | goto out; |
| 2272 | } |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2273 | rtnl_unlock(); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2274 | |
| 2275 | /* Create shared ring, alloc event channel -- for each queue */ |
| 2276 | for (i = 0; i < num_queues; ++i) { |
| 2277 | queue = &info->queues[i]; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2278 | err = setup_netfront(dev, queue, feature_split_evtchn); |
Ross Lagerwall | e2e004a | 2017-02-08 10:57:37 +0000 | [diff] [blame] | 2279 | if (err) |
| 2280 | goto destroy_ring; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2281 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2282 | |
| 2283 | again: |
| 2284 | err = xenbus_transaction_start(&xbt); |
| 2285 | if (err) { |
| 2286 | xenbus_dev_fatal(dev, err, "starting transaction"); |
| 2287 | goto destroy_ring; |
| 2288 | } |
| 2289 | |
chas williams | 812494d | 2015-09-16 16:28:25 -0400 | [diff] [blame] | 2290 | if (xenbus_exists(XBT_NIL, |
| 2291 | info->xbdev->otherend, "multi-queue-max-queues")) { |
| 2292 | /* Write the number of queues */ |
| 2293 | err = xenbus_printf(xbt, dev->nodename, |
| 2294 | "multi-queue-num-queues", "%u", num_queues); |
| 2295 | if (err) { |
| 2296 | message = "writing multi-queue-num-queues"; |
| 2297 | goto abort_transaction_no_dev_fatal; |
| 2298 | } |
| 2299 | } |
| 2300 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2301 | if (num_queues == 1) { |
| 2302 | err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */ |
| 2303 | if (err) |
| 2304 | goto abort_transaction_no_dev_fatal; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 2305 | } else { |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2306 | /* Write the keys for each queue */ |
| 2307 | for (i = 0; i < num_queues; ++i) { |
| 2308 | queue = &info->queues[i]; |
| 2309 | err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */ |
| 2310 | if (err) |
| 2311 | goto abort_transaction_no_dev_fatal; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 2312 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2315 | /* The remaining keys are not queue-specific */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2316 | err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u", |
| 2317 | 1); |
| 2318 | if (err) { |
| 2319 | message = "writing request-rx-copy"; |
| 2320 | goto abort_transaction; |
| 2321 | } |
| 2322 | |
| 2323 | err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1); |
| 2324 | if (err) { |
| 2325 | message = "writing feature-rx-notify"; |
| 2326 | goto abort_transaction; |
| 2327 | } |
| 2328 | |
| 2329 | err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1); |
| 2330 | if (err) { |
| 2331 | message = "writing feature-sg"; |
| 2332 | goto abort_transaction; |
| 2333 | } |
| 2334 | |
| 2335 | err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1); |
| 2336 | if (err) { |
| 2337 | message = "writing feature-gso-tcpv4"; |
| 2338 | goto abort_transaction; |
| 2339 | } |
| 2340 | |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 2341 | err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1"); |
| 2342 | if (err) { |
| 2343 | message = "writing feature-gso-tcpv6"; |
| 2344 | goto abort_transaction; |
| 2345 | } |
| 2346 | |
| 2347 | err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload", |
| 2348 | "1"); |
| 2349 | if (err) { |
| 2350 | message = "writing feature-ipv6-csum-offload"; |
| 2351 | goto abort_transaction; |
| 2352 | } |
| 2353 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2354 | err = xenbus_transaction_end(xbt, 0); |
| 2355 | if (err) { |
| 2356 | if (err == -EAGAIN) |
| 2357 | goto again; |
| 2358 | xenbus_dev_fatal(dev, err, "completing transaction"); |
| 2359 | goto destroy_ring; |
| 2360 | } |
| 2361 | |
| 2362 | return 0; |
| 2363 | |
| 2364 | abort_transaction: |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2365 | xenbus_dev_fatal(dev, err, "%s", message); |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2366 | abort_transaction_no_dev_fatal: |
| 2367 | xenbus_transaction_end(xbt, 1); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2368 | destroy_ring: |
| 2369 | xennet_disconnect_backend(info); |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2370 | rtnl_lock(); |
Ross Lagerwall | e2e004a | 2017-02-08 10:57:37 +0000 | [diff] [blame] | 2371 | xennet_destroy_queues(info); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2372 | out: |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2373 | rtnl_unlock(); |
Ross Lagerwall | cb25778 | 2018-06-21 14:00:20 +0100 | [diff] [blame] | 2374 | out_unlocked: |
Vitaly Kuznetsov | d86b567 | 2017-05-11 13:58:06 +0200 | [diff] [blame] | 2375 | device_unregister(&dev->dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2376 | return err; |
| 2377 | } |
| 2378 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2379 | static int xennet_connect(struct net_device *dev) |
| 2380 | { |
| 2381 | struct netfront_info *np = netdev_priv(dev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2382 | unsigned int num_queues = 0; |
David Vrabel | a5b5dc3 | 2014-07-31 17:38:23 +0100 | [diff] [blame] | 2383 | int err; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2384 | unsigned int j = 0; |
| 2385 | struct netfront_queue *queue = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2386 | |
Juergen Gross | 2890ea5 | 2016-10-31 14:58:41 +0100 | [diff] [blame] | 2387 | if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2388 | dev_info(&dev->dev, |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 2389 | "backend does not support copying receive path\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2390 | return -ENODEV; |
| 2391 | } |
| 2392 | |
Ian Campbell | f502bf2 | 2010-08-18 23:27:49 +0000 | [diff] [blame] | 2393 | err = talk_to_netback(np->xbdev, np); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2394 | if (err) |
| 2395 | return err; |
Denis Kirjanov | 6c5aa6f | 2020-06-29 16:13:28 +0300 | [diff] [blame] | 2396 | if (np->netback_has_xdp_headroom) |
| 2397 | pr_info("backend supports XDP headroom\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2398 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2399 | /* talk_to_netback() sets the correct number of queues */ |
| 2400 | num_queues = dev->real_num_tx_queues; |
| 2401 | |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2402 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
| 2403 | err = register_netdev(dev); |
| 2404 | if (err) { |
| 2405 | pr_warn("%s: register_netdev err=%d\n", __func__, err); |
| 2406 | device_unregister(&np->xbdev->dev); |
| 2407 | return err; |
| 2408 | } |
| 2409 | } |
| 2410 | |
Ross Lagerwall | 45c8184 | 2018-06-21 14:00:21 +0100 | [diff] [blame] | 2411 | rtnl_lock(); |
| 2412 | netdev_update_features(dev); |
| 2413 | rtnl_unlock(); |
| 2414 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2415 | /* |
David Vrabel | a5b5dc3 | 2014-07-31 17:38:23 +0100 | [diff] [blame] | 2416 | * All public and private state should now be sane. Get |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2417 | * ready to start sending and receiving packets and give the driver |
| 2418 | * domain a kick because we've probably just requeued some |
| 2419 | * packets. |
| 2420 | */ |
Dongli Zhang | 042b204 | 2021-10-22 16:31:39 -0700 | [diff] [blame] | 2421 | netif_tx_lock_bh(np->netdev); |
| 2422 | netif_device_attach(np->netdev); |
| 2423 | netif_tx_unlock_bh(np->netdev); |
| 2424 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2425 | netif_carrier_on(np->netdev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2426 | for (j = 0; j < num_queues; ++j) { |
| 2427 | queue = &np->queues[j]; |
David Vrabel | f50b407 | 2014-07-02 16:09:14 +0100 | [diff] [blame] | 2428 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2429 | notify_remote_via_irq(queue->tx_irq); |
| 2430 | if (queue->tx_irq != queue->rx_irq) |
| 2431 | notify_remote_via_irq(queue->rx_irq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2432 | |
David Vrabel | f50b407 | 2014-07-02 16:09:14 +0100 | [diff] [blame] | 2433 | spin_lock_irq(&queue->tx_lock); |
| 2434 | xennet_tx_buf_gc(queue); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2435 | spin_unlock_irq(&queue->tx_lock); |
David Vrabel | f50b407 | 2014-07-02 16:09:14 +0100 | [diff] [blame] | 2436 | |
| 2437 | spin_lock_bh(&queue->rx_lock); |
| 2438 | xennet_alloc_rx_buffers(queue); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2439 | spin_unlock_bh(&queue->rx_lock); |
| 2440 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2441 | |
| 2442 | return 0; |
| 2443 | } |
| 2444 | |
Lee Jones | 8070860 | 2021-01-15 20:09:03 +0000 | [diff] [blame] | 2445 | /* |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2446 | * Callback received when the backend's state changes. |
| 2447 | */ |
Ian Campbell | f502bf2 | 2010-08-18 23:27:49 +0000 | [diff] [blame] | 2448 | static void netback_changed(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2449 | enum xenbus_state backend_state) |
| 2450 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 2451 | struct netfront_info *np = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2452 | struct net_device *netdev = np->netdev; |
| 2453 | |
| 2454 | dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state)); |
| 2455 | |
Juergen Gross | 8edfe2e | 2018-09-07 14:21:30 +0200 | [diff] [blame] | 2456 | wake_up_all(&module_wq); |
| 2457 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2458 | switch (backend_state) { |
| 2459 | case XenbusStateInitialising: |
| 2460 | case XenbusStateInitialised: |
Noboru Iwamatsu | b78c951 | 2009-10-13 17:22:29 -0400 | [diff] [blame] | 2461 | case XenbusStateReconfiguring: |
| 2462 | case XenbusStateReconfigured: |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2463 | case XenbusStateUnknown: |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2464 | break; |
| 2465 | |
| 2466 | case XenbusStateInitWait: |
| 2467 | if (dev->state != XenbusStateInitialising) |
| 2468 | break; |
| 2469 | if (xennet_connect(netdev) != 0) |
| 2470 | break; |
| 2471 | xenbus_switch_state(dev, XenbusStateConnected); |
Laszlo Ersek | 08e34eb | 2011-12-11 01:48:59 +0000 | [diff] [blame] | 2472 | break; |
| 2473 | |
| 2474 | case XenbusStateConnected: |
Amerigo Wang | ee89bab | 2012-08-09 22:14:56 +0000 | [diff] [blame] | 2475 | netdev_notify_peers(netdev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2476 | break; |
| 2477 | |
David Vrabel | bce3ea8 | 2014-02-04 18:50:26 +0000 | [diff] [blame] | 2478 | case XenbusStateClosed: |
| 2479 | if (dev->state == XenbusStateClosed) |
| 2480 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2481 | fallthrough; /* Missed the backend's CLOSING state */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2482 | case XenbusStateClosing: |
| 2483 | xenbus_frontend_closed(dev); |
| 2484 | break; |
| 2485 | } |
| 2486 | } |
| 2487 | |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 2488 | static const struct xennet_stat { |
| 2489 | char name[ETH_GSTRING_LEN]; |
| 2490 | u16 offset; |
| 2491 | } xennet_stats[] = { |
| 2492 | { |
| 2493 | "rx_gso_checksum_fixup", |
| 2494 | offsetof(struct netfront_info, rx_gso_checksum_fixup) |
| 2495 | }, |
| 2496 | }; |
| 2497 | |
| 2498 | static int xennet_get_sset_count(struct net_device *dev, int string_set) |
| 2499 | { |
| 2500 | switch (string_set) { |
| 2501 | case ETH_SS_STATS: |
| 2502 | return ARRAY_SIZE(xennet_stats); |
| 2503 | default: |
| 2504 | return -EINVAL; |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | static void xennet_get_ethtool_stats(struct net_device *dev, |
| 2509 | struct ethtool_stats *stats, u64 * data) |
| 2510 | { |
| 2511 | void *np = netdev_priv(dev); |
| 2512 | int i; |
| 2513 | |
| 2514 | for (i = 0; i < ARRAY_SIZE(xennet_stats); i++) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2515 | data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset)); |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data) |
| 2519 | { |
| 2520 | int i; |
| 2521 | |
| 2522 | switch (stringset) { |
| 2523 | case ETH_SS_STATS: |
| 2524 | for (i = 0; i < ARRAY_SIZE(xennet_stats); i++) |
| 2525 | memcpy(data + i * ETH_GSTRING_LEN, |
| 2526 | xennet_stats[i].name, ETH_GSTRING_LEN); |
| 2527 | break; |
| 2528 | } |
| 2529 | } |
| 2530 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 2531 | static const struct ethtool_ops xennet_ethtool_ops = |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2532 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2533 | .get_link = ethtool_op_get_link, |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 2534 | |
| 2535 | .get_sset_count = xennet_get_sset_count, |
| 2536 | .get_ethtool_stats = xennet_get_ethtool_stats, |
| 2537 | .get_strings = xennet_get_strings, |
Daniel Drown | 91ffb9d | 2020-07-03 01:22:34 -0500 | [diff] [blame] | 2538 | .get_ts_info = ethtool_op_get_ts_info, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2539 | }; |
| 2540 | |
| 2541 | #ifdef CONFIG_SYSFS |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2542 | static ssize_t show_rxbuf(struct device *dev, |
| 2543 | struct device_attribute *attr, char *buf) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2544 | { |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2545 | return sprintf(buf, "%lu\n", NET_RX_RING_SIZE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2546 | } |
| 2547 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2548 | static ssize_t store_rxbuf(struct device *dev, |
| 2549 | struct device_attribute *attr, |
| 2550 | const char *buf, size_t len) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2551 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2552 | char *endp; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2553 | |
| 2554 | if (!capable(CAP_NET_ADMIN)) |
| 2555 | return -EPERM; |
| 2556 | |
Andrew Lunn | 8ed7ec1 | 2020-10-31 19:04:35 +0100 | [diff] [blame] | 2557 | simple_strtoul(buf, &endp, 0); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2558 | if (endp == buf) |
| 2559 | return -EBADMSG; |
| 2560 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2561 | /* rxbuf_min and rxbuf_max are no longer configurable. */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2562 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2563 | return len; |
| 2564 | } |
| 2565 | |
Joe Perches | d61e403 | 2018-03-23 15:54:39 -0700 | [diff] [blame] | 2566 | static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf); |
| 2567 | static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf); |
| 2568 | static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL); |
Takashi Iwai | 27b917e | 2015-02-04 14:38:55 +0100 | [diff] [blame] | 2569 | |
| 2570 | static struct attribute *xennet_dev_attrs[] = { |
| 2571 | &dev_attr_rxbuf_min.attr, |
| 2572 | &dev_attr_rxbuf_max.attr, |
| 2573 | &dev_attr_rxbuf_cur.attr, |
| 2574 | NULL |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2575 | }; |
| 2576 | |
Takashi Iwai | 27b917e | 2015-02-04 14:38:55 +0100 | [diff] [blame] | 2577 | static const struct attribute_group xennet_dev_group = { |
| 2578 | .attrs = xennet_dev_attrs |
| 2579 | }; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2580 | #endif /* CONFIG_SYSFS */ |
| 2581 | |
Andrea Righi | c2c6331 | 2020-07-24 10:59:10 +0200 | [diff] [blame] | 2582 | static void xennet_bus_close(struct xenbus_device *dev) |
| 2583 | { |
| 2584 | int ret; |
| 2585 | |
| 2586 | if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed) |
| 2587 | return; |
| 2588 | do { |
| 2589 | xenbus_switch_state(dev, XenbusStateClosing); |
| 2590 | ret = wait_event_timeout(module_wq, |
| 2591 | xenbus_read_driver_state(dev->otherend) == |
| 2592 | XenbusStateClosing || |
| 2593 | xenbus_read_driver_state(dev->otherend) == |
| 2594 | XenbusStateClosed || |
| 2595 | xenbus_read_driver_state(dev->otherend) == |
| 2596 | XenbusStateUnknown, |
| 2597 | XENNET_TIMEOUT); |
| 2598 | } while (!ret); |
| 2599 | |
| 2600 | if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed) |
| 2601 | return; |
| 2602 | |
| 2603 | do { |
| 2604 | xenbus_switch_state(dev, XenbusStateClosed); |
| 2605 | ret = wait_event_timeout(module_wq, |
| 2606 | xenbus_read_driver_state(dev->otherend) == |
| 2607 | XenbusStateClosed || |
| 2608 | xenbus_read_driver_state(dev->otherend) == |
| 2609 | XenbusStateUnknown, |
| 2610 | XENNET_TIMEOUT); |
| 2611 | } while (!ret); |
| 2612 | } |
| 2613 | |
Bill Pemberton | 8e0e46b | 2012-12-03 09:24:22 -0500 | [diff] [blame] | 2614 | static int xennet_remove(struct xenbus_device *dev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2615 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 2616 | struct netfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2617 | |
Andrea Righi | c2c6331 | 2020-07-24 10:59:10 +0200 | [diff] [blame] | 2618 | xennet_bus_close(dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2619 | xennet_disconnect_backend(info); |
| 2620 | |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2621 | if (info->netdev->reg_state == NETREG_REGISTERED) |
| 2622 | unregister_netdev(info->netdev); |
Ian Campbell | 6bc96d0 | 2012-06-25 22:48:41 +0000 | [diff] [blame] | 2623 | |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2624 | if (info->queues) { |
| 2625 | rtnl_lock(); |
Chas Williams | 9a873c7 | 2015-08-19 19:14:20 -0400 | [diff] [blame] | 2626 | xennet_destroy_queues(info); |
Ross Lagerwall | f599c64 | 2018-01-11 09:36:38 +0000 | [diff] [blame] | 2627 | rtnl_unlock(); |
| 2628 | } |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 2629 | xennet_free_netdev(info->netdev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2630 | |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 2634 | static const struct xenbus_device_id netfront_ids[] = { |
| 2635 | { "vif" }, |
| 2636 | { "" } |
| 2637 | }; |
| 2638 | |
| 2639 | static struct xenbus_driver netfront_driver = { |
| 2640 | .ids = netfront_ids, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2641 | .probe = netfront_probe, |
Bill Pemberton | 8e0e46b | 2012-12-03 09:24:22 -0500 | [diff] [blame] | 2642 | .remove = xennet_remove, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2643 | .resume = netfront_resume, |
Ian Campbell | f502bf2 | 2010-08-18 23:27:49 +0000 | [diff] [blame] | 2644 | .otherend_changed = netback_changed, |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 2645 | }; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2646 | |
| 2647 | static int __init netif_init(void) |
| 2648 | { |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 2649 | if (!xen_domain()) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2650 | return -ENODEV; |
| 2651 | |
Konrad Rzeszutek Wilk | 51c71a3 | 2013-11-26 15:05:40 -0500 | [diff] [blame] | 2652 | if (!xen_has_pv_nic_devices()) |
Igor Mammedov | b9136d2 | 2012-03-21 15:08:38 +0100 | [diff] [blame] | 2653 | return -ENODEV; |
| 2654 | |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 2655 | pr_info("Initialising Xen virtual ethernet driver\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2656 | |
Juergen Gross | 034702a | 2017-01-10 14:32:51 +0100 | [diff] [blame] | 2657 | /* Allow as many queues as there are CPUs inut max. 8 if user has not |
Wei Liu | 32a8440 | 2015-09-10 11:18:58 +0100 | [diff] [blame] | 2658 | * specified a value. |
| 2659 | */ |
| 2660 | if (xennet_max_queues == 0) |
Juergen Gross | 034702a | 2017-01-10 14:32:51 +0100 | [diff] [blame] | 2661 | xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT, |
| 2662 | num_online_cpus()); |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2663 | |
Al Viro | ffb78a2 | 2008-11-22 17:38:14 +0000 | [diff] [blame] | 2664 | return xenbus_register_frontend(&netfront_driver); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2665 | } |
| 2666 | module_init(netif_init); |
| 2667 | |
| 2668 | |
| 2669 | static void __exit netif_exit(void) |
| 2670 | { |
Al Viro | ffb78a2 | 2008-11-22 17:38:14 +0000 | [diff] [blame] | 2671 | xenbus_unregister_driver(&netfront_driver); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2672 | } |
| 2673 | module_exit(netif_exit); |
| 2674 | |
| 2675 | MODULE_DESCRIPTION("Xen virtual network device frontend"); |
| 2676 | MODULE_LICENSE("GPL"); |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 2677 | MODULE_ALIAS("xen:vif"); |
Mark McLoughlin | 4f93f09b | 2008-04-02 10:54:06 -0700 | [diff] [blame] | 2678 | MODULE_ALIAS("xennet"); |