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