1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Network-device interface management.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2004-2005, Keir Fraser
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or
7*4882a593Smuzhiyun * modify it under the terms of the GNU General Public License version 2
8*4882a593Smuzhiyun * as published by the Free Software Foundation; or, when distributed
9*4882a593Smuzhiyun * separately from the Linux kernel or incorporated into other
10*4882a593Smuzhiyun * software packages, subject to the following license:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a copy
13*4882a593Smuzhiyun * of this source file (the "Software"), to deal in the Software without
14*4882a593Smuzhiyun * restriction, including without limitation the rights to use, copy, modify,
15*4882a593Smuzhiyun * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16*4882a593Smuzhiyun * and to permit persons to whom the Software is furnished to do so, subject to
17*4882a593Smuzhiyun * the following conditions:
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
20*4882a593Smuzhiyun * all copies or substantial portions of the Software.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25*4882a593Smuzhiyun * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28*4882a593Smuzhiyun * IN THE SOFTWARE.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include "common.h"
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #include <linux/kthread.h>
34*4882a593Smuzhiyun #include <linux/sched/task.h>
35*4882a593Smuzhiyun #include <linux/ethtool.h>
36*4882a593Smuzhiyun #include <linux/rtnetlink.h>
37*4882a593Smuzhiyun #include <linux/if_vlan.h>
38*4882a593Smuzhiyun #include <linux/vmalloc.h>
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #include <xen/events.h>
41*4882a593Smuzhiyun #include <asm/xen/hypercall.h>
42*4882a593Smuzhiyun #include <xen/balloon.h>
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #define XENVIF_QUEUE_LENGTH 32
45*4882a593Smuzhiyun #define XENVIF_NAPI_WEIGHT 64
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* Number of bytes allowed on the internal guest Rx queue. */
48*4882a593Smuzhiyun #define XENVIF_RX_QUEUE_BYTES (XEN_NETIF_RX_RING_SIZE/2 * PAGE_SIZE)
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* This function is used to set SKBTX_DEV_ZEROCOPY as well as
51*4882a593Smuzhiyun * increasing the inflight counter. We need to increase the inflight
52*4882a593Smuzhiyun * counter because core driver calls into xenvif_zerocopy_callback
53*4882a593Smuzhiyun * which calls xenvif_skb_zerocopy_complete.
54*4882a593Smuzhiyun */
xenvif_skb_zerocopy_prepare(struct xenvif_queue * queue,struct sk_buff * skb)55*4882a593Smuzhiyun void xenvif_skb_zerocopy_prepare(struct xenvif_queue *queue,
56*4882a593Smuzhiyun struct sk_buff *skb)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
59*4882a593Smuzhiyun atomic_inc(&queue->inflight_packets);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
xenvif_skb_zerocopy_complete(struct xenvif_queue * queue)62*4882a593Smuzhiyun void xenvif_skb_zerocopy_complete(struct xenvif_queue *queue)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun atomic_dec(&queue->inflight_packets);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* Wake the dealloc thread _after_ decrementing inflight_packets so
67*4882a593Smuzhiyun * that if kthread_stop() has already been called, the dealloc thread
68*4882a593Smuzhiyun * does not wait forever with nothing to wake it.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun wake_up(&queue->dealloc_wq);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
xenvif_schedulable(struct xenvif * vif)73*4882a593Smuzhiyun static int xenvif_schedulable(struct xenvif *vif)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun return netif_running(vif->dev) &&
76*4882a593Smuzhiyun test_bit(VIF_STATUS_CONNECTED, &vif->status) &&
77*4882a593Smuzhiyun !vif->disabled;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
xenvif_handle_tx_interrupt(struct xenvif_queue * queue)80*4882a593Smuzhiyun static bool xenvif_handle_tx_interrupt(struct xenvif_queue *queue)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun bool rc;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun rc = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
85*4882a593Smuzhiyun if (rc)
86*4882a593Smuzhiyun napi_schedule(&queue->napi);
87*4882a593Smuzhiyun return rc;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
xenvif_tx_interrupt(int irq,void * dev_id)90*4882a593Smuzhiyun static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun struct xenvif_queue *queue = dev_id;
93*4882a593Smuzhiyun int old;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun old = atomic_fetch_or(NETBK_TX_EOI, &queue->eoi_pending);
96*4882a593Smuzhiyun WARN(old & NETBK_TX_EOI, "Interrupt while EOI pending\n");
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun if (!xenvif_handle_tx_interrupt(queue)) {
99*4882a593Smuzhiyun atomic_andnot(NETBK_TX_EOI, &queue->eoi_pending);
100*4882a593Smuzhiyun xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun return IRQ_HANDLED;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
xenvif_poll(struct napi_struct * napi,int budget)106*4882a593Smuzhiyun static int xenvif_poll(struct napi_struct *napi, int budget)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun struct xenvif_queue *queue =
109*4882a593Smuzhiyun container_of(napi, struct xenvif_queue, napi);
110*4882a593Smuzhiyun int work_done;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /* This vif is rogue, we pretend we've there is nothing to do
113*4882a593Smuzhiyun * for this vif to deschedule it from NAPI. But this interface
114*4882a593Smuzhiyun * will be turned off in thread context later.
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun if (unlikely(queue->vif->disabled)) {
117*4882a593Smuzhiyun napi_complete(napi);
118*4882a593Smuzhiyun return 0;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun work_done = xenvif_tx_action(queue, budget);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun if (work_done < budget) {
124*4882a593Smuzhiyun napi_complete_done(napi, work_done);
125*4882a593Smuzhiyun /* If the queue is rate-limited, it shall be
126*4882a593Smuzhiyun * rescheduled in the timer callback.
127*4882a593Smuzhiyun */
128*4882a593Smuzhiyun if (likely(!queue->rate_limited))
129*4882a593Smuzhiyun xenvif_napi_schedule_or_enable_events(queue);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun return work_done;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
xenvif_handle_rx_interrupt(struct xenvif_queue * queue)135*4882a593Smuzhiyun static bool xenvif_handle_rx_interrupt(struct xenvif_queue *queue)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun bool rc;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun rc = xenvif_have_rx_work(queue, false);
140*4882a593Smuzhiyun if (rc)
141*4882a593Smuzhiyun xenvif_kick_thread(queue);
142*4882a593Smuzhiyun return rc;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
xenvif_rx_interrupt(int irq,void * dev_id)145*4882a593Smuzhiyun static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun struct xenvif_queue *queue = dev_id;
148*4882a593Smuzhiyun int old;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun old = atomic_fetch_or(NETBK_RX_EOI, &queue->eoi_pending);
151*4882a593Smuzhiyun WARN(old & NETBK_RX_EOI, "Interrupt while EOI pending\n");
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (!xenvif_handle_rx_interrupt(queue)) {
154*4882a593Smuzhiyun atomic_andnot(NETBK_RX_EOI, &queue->eoi_pending);
155*4882a593Smuzhiyun xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun return IRQ_HANDLED;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
xenvif_interrupt(int irq,void * dev_id)161*4882a593Smuzhiyun irqreturn_t xenvif_interrupt(int irq, void *dev_id)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun struct xenvif_queue *queue = dev_id;
164*4882a593Smuzhiyun int old;
165*4882a593Smuzhiyun bool has_rx, has_tx;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun old = atomic_fetch_or(NETBK_COMMON_EOI, &queue->eoi_pending);
168*4882a593Smuzhiyun WARN(old, "Interrupt while EOI pending\n");
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun has_tx = xenvif_handle_tx_interrupt(queue);
171*4882a593Smuzhiyun has_rx = xenvif_handle_rx_interrupt(queue);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (!has_rx && !has_tx) {
174*4882a593Smuzhiyun atomic_andnot(NETBK_COMMON_EOI, &queue->eoi_pending);
175*4882a593Smuzhiyun xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun return IRQ_HANDLED;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
xenvif_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)181*4882a593Smuzhiyun static u16 xenvif_select_queue(struct net_device *dev, struct sk_buff *skb,
182*4882a593Smuzhiyun struct net_device *sb_dev)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun struct xenvif *vif = netdev_priv(dev);
185*4882a593Smuzhiyun unsigned int size = vif->hash.size;
186*4882a593Smuzhiyun unsigned int num_queues;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* If queues are not set up internally - always return 0
189*4882a593Smuzhiyun * as the packet going to be dropped anyway */
190*4882a593Smuzhiyun num_queues = READ_ONCE(vif->num_queues);
191*4882a593Smuzhiyun if (num_queues < 1)
192*4882a593Smuzhiyun return 0;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE)
195*4882a593Smuzhiyun return netdev_pick_tx(dev, skb, NULL) %
196*4882a593Smuzhiyun dev->real_num_tx_queues;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun xenvif_set_skb_hash(vif, skb);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun if (size == 0)
201*4882a593Smuzhiyun return skb_get_hash_raw(skb) % dev->real_num_tx_queues;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun return vif->hash.mapping[vif->hash.mapping_sel]
204*4882a593Smuzhiyun [skb_get_hash_raw(skb) % size];
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun static netdev_tx_t
xenvif_start_xmit(struct sk_buff * skb,struct net_device * dev)208*4882a593Smuzhiyun xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun struct xenvif *vif = netdev_priv(dev);
211*4882a593Smuzhiyun struct xenvif_queue *queue = NULL;
212*4882a593Smuzhiyun unsigned int num_queues;
213*4882a593Smuzhiyun u16 index;
214*4882a593Smuzhiyun struct xenvif_rx_cb *cb;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun BUG_ON(skb->dev != dev);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /* Drop the packet if queues are not set up.
219*4882a593Smuzhiyun * This handler should be called inside an RCU read section
220*4882a593Smuzhiyun * so we don't need to enter it here explicitly.
221*4882a593Smuzhiyun */
222*4882a593Smuzhiyun num_queues = READ_ONCE(vif->num_queues);
223*4882a593Smuzhiyun if (num_queues < 1)
224*4882a593Smuzhiyun goto drop;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /* Obtain the queue to be used to transmit this packet */
227*4882a593Smuzhiyun index = skb_get_queue_mapping(skb);
228*4882a593Smuzhiyun if (index >= num_queues) {
229*4882a593Smuzhiyun pr_warn_ratelimited("Invalid queue %hu for packet on interface %s\n",
230*4882a593Smuzhiyun index, vif->dev->name);
231*4882a593Smuzhiyun index %= num_queues;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun queue = &vif->queues[index];
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /* Drop the packet if queue is not ready */
236*4882a593Smuzhiyun if (queue->task == NULL ||
237*4882a593Smuzhiyun queue->dealloc_task == NULL ||
238*4882a593Smuzhiyun !xenvif_schedulable(vif))
239*4882a593Smuzhiyun goto drop;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun if (vif->multicast_control && skb->pkt_type == PACKET_MULTICAST) {
242*4882a593Smuzhiyun struct ethhdr *eth = (struct ethhdr *)skb->data;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun if (!xenvif_mcast_match(vif, eth->h_dest))
245*4882a593Smuzhiyun goto drop;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun cb = XENVIF_RX_CB(skb);
249*4882a593Smuzhiyun cb->expires = jiffies + vif->drain_timeout;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* If there is no hash algorithm configured then make sure there
252*4882a593Smuzhiyun * is no hash information in the socket buffer otherwise it
253*4882a593Smuzhiyun * would be incorrectly forwarded to the frontend.
254*4882a593Smuzhiyun */
255*4882a593Smuzhiyun if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE)
256*4882a593Smuzhiyun skb_clear_hash(skb);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun if (!xenvif_rx_queue_tail(queue, skb))
259*4882a593Smuzhiyun goto drop;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun xenvif_kick_thread(queue);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun return NETDEV_TX_OK;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun drop:
266*4882a593Smuzhiyun vif->dev->stats.tx_dropped++;
267*4882a593Smuzhiyun dev_kfree_skb_any(skb);
268*4882a593Smuzhiyun return NETDEV_TX_OK;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
xenvif_get_stats(struct net_device * dev)271*4882a593Smuzhiyun static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun struct xenvif *vif = netdev_priv(dev);
274*4882a593Smuzhiyun struct xenvif_queue *queue = NULL;
275*4882a593Smuzhiyun unsigned int num_queues;
276*4882a593Smuzhiyun u64 rx_bytes = 0;
277*4882a593Smuzhiyun u64 rx_packets = 0;
278*4882a593Smuzhiyun u64 tx_bytes = 0;
279*4882a593Smuzhiyun u64 tx_packets = 0;
280*4882a593Smuzhiyun unsigned int index;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun rcu_read_lock();
283*4882a593Smuzhiyun num_queues = READ_ONCE(vif->num_queues);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /* Aggregate tx and rx stats from each queue */
286*4882a593Smuzhiyun for (index = 0; index < num_queues; ++index) {
287*4882a593Smuzhiyun queue = &vif->queues[index];
288*4882a593Smuzhiyun rx_bytes += queue->stats.rx_bytes;
289*4882a593Smuzhiyun rx_packets += queue->stats.rx_packets;
290*4882a593Smuzhiyun tx_bytes += queue->stats.tx_bytes;
291*4882a593Smuzhiyun tx_packets += queue->stats.tx_packets;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun rcu_read_unlock();
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun vif->dev->stats.rx_bytes = rx_bytes;
297*4882a593Smuzhiyun vif->dev->stats.rx_packets = rx_packets;
298*4882a593Smuzhiyun vif->dev->stats.tx_bytes = tx_bytes;
299*4882a593Smuzhiyun vif->dev->stats.tx_packets = tx_packets;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun return &vif->dev->stats;
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun
xenvif_up(struct xenvif * vif)304*4882a593Smuzhiyun static void xenvif_up(struct xenvif *vif)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun struct xenvif_queue *queue = NULL;
307*4882a593Smuzhiyun unsigned int num_queues = vif->num_queues;
308*4882a593Smuzhiyun unsigned int queue_index;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun for (queue_index = 0; queue_index < num_queues; ++queue_index) {
311*4882a593Smuzhiyun queue = &vif->queues[queue_index];
312*4882a593Smuzhiyun napi_enable(&queue->napi);
313*4882a593Smuzhiyun enable_irq(queue->tx_irq);
314*4882a593Smuzhiyun if (queue->tx_irq != queue->rx_irq)
315*4882a593Smuzhiyun enable_irq(queue->rx_irq);
316*4882a593Smuzhiyun xenvif_napi_schedule_or_enable_events(queue);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
xenvif_down(struct xenvif * vif)320*4882a593Smuzhiyun static void xenvif_down(struct xenvif *vif)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun struct xenvif_queue *queue = NULL;
323*4882a593Smuzhiyun unsigned int num_queues = vif->num_queues;
324*4882a593Smuzhiyun unsigned int queue_index;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun for (queue_index = 0; queue_index < num_queues; ++queue_index) {
327*4882a593Smuzhiyun queue = &vif->queues[queue_index];
328*4882a593Smuzhiyun disable_irq(queue->tx_irq);
329*4882a593Smuzhiyun if (queue->tx_irq != queue->rx_irq)
330*4882a593Smuzhiyun disable_irq(queue->rx_irq);
331*4882a593Smuzhiyun napi_disable(&queue->napi);
332*4882a593Smuzhiyun del_timer_sync(&queue->credit_timeout);
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
xenvif_open(struct net_device * dev)336*4882a593Smuzhiyun static int xenvif_open(struct net_device *dev)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun struct xenvif *vif = netdev_priv(dev);
339*4882a593Smuzhiyun if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
340*4882a593Smuzhiyun xenvif_up(vif);
341*4882a593Smuzhiyun netif_tx_start_all_queues(dev);
342*4882a593Smuzhiyun return 0;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun
xenvif_close(struct net_device * dev)345*4882a593Smuzhiyun static int xenvif_close(struct net_device *dev)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun struct xenvif *vif = netdev_priv(dev);
348*4882a593Smuzhiyun if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
349*4882a593Smuzhiyun xenvif_down(vif);
350*4882a593Smuzhiyun netif_tx_stop_all_queues(dev);
351*4882a593Smuzhiyun return 0;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
xenvif_change_mtu(struct net_device * dev,int mtu)354*4882a593Smuzhiyun static int xenvif_change_mtu(struct net_device *dev, int mtu)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun struct xenvif *vif = netdev_priv(dev);
357*4882a593Smuzhiyun int max = vif->can_sg ? ETH_MAX_MTU - VLAN_ETH_HLEN : ETH_DATA_LEN;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun if (mtu > max)
360*4882a593Smuzhiyun return -EINVAL;
361*4882a593Smuzhiyun dev->mtu = mtu;
362*4882a593Smuzhiyun return 0;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
xenvif_fix_features(struct net_device * dev,netdev_features_t features)365*4882a593Smuzhiyun static netdev_features_t xenvif_fix_features(struct net_device *dev,
366*4882a593Smuzhiyun netdev_features_t features)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun struct xenvif *vif = netdev_priv(dev);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun if (!vif->can_sg)
371*4882a593Smuzhiyun features &= ~NETIF_F_SG;
372*4882a593Smuzhiyun if (~(vif->gso_mask) & GSO_BIT(TCPV4))
373*4882a593Smuzhiyun features &= ~NETIF_F_TSO;
374*4882a593Smuzhiyun if (~(vif->gso_mask) & GSO_BIT(TCPV6))
375*4882a593Smuzhiyun features &= ~NETIF_F_TSO6;
376*4882a593Smuzhiyun if (!vif->ip_csum)
377*4882a593Smuzhiyun features &= ~NETIF_F_IP_CSUM;
378*4882a593Smuzhiyun if (!vif->ipv6_csum)
379*4882a593Smuzhiyun features &= ~NETIF_F_IPV6_CSUM;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun return features;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun static const struct xenvif_stat {
385*4882a593Smuzhiyun char name[ETH_GSTRING_LEN];
386*4882a593Smuzhiyun u16 offset;
387*4882a593Smuzhiyun } xenvif_stats[] = {
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun "rx_gso_checksum_fixup",
390*4882a593Smuzhiyun offsetof(struct xenvif_stats, rx_gso_checksum_fixup)
391*4882a593Smuzhiyun },
392*4882a593Smuzhiyun /* If (sent != success + fail), there are probably packets never
393*4882a593Smuzhiyun * freed up properly!
394*4882a593Smuzhiyun */
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun "tx_zerocopy_sent",
397*4882a593Smuzhiyun offsetof(struct xenvif_stats, tx_zerocopy_sent),
398*4882a593Smuzhiyun },
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun "tx_zerocopy_success",
401*4882a593Smuzhiyun offsetof(struct xenvif_stats, tx_zerocopy_success),
402*4882a593Smuzhiyun },
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun "tx_zerocopy_fail",
405*4882a593Smuzhiyun offsetof(struct xenvif_stats, tx_zerocopy_fail)
406*4882a593Smuzhiyun },
407*4882a593Smuzhiyun /* Number of packets exceeding MAX_SKB_FRAG slots. You should use
408*4882a593Smuzhiyun * a guest with the same MAX_SKB_FRAG
409*4882a593Smuzhiyun */
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun "tx_frag_overflow",
412*4882a593Smuzhiyun offsetof(struct xenvif_stats, tx_frag_overflow)
413*4882a593Smuzhiyun },
414*4882a593Smuzhiyun };
415*4882a593Smuzhiyun
xenvif_get_sset_count(struct net_device * dev,int string_set)416*4882a593Smuzhiyun static int xenvif_get_sset_count(struct net_device *dev, int string_set)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun switch (string_set) {
419*4882a593Smuzhiyun case ETH_SS_STATS:
420*4882a593Smuzhiyun return ARRAY_SIZE(xenvif_stats);
421*4882a593Smuzhiyun default:
422*4882a593Smuzhiyun return -EINVAL;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
xenvif_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)426*4882a593Smuzhiyun static void xenvif_get_ethtool_stats(struct net_device *dev,
427*4882a593Smuzhiyun struct ethtool_stats *stats, u64 * data)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun struct xenvif *vif = netdev_priv(dev);
430*4882a593Smuzhiyun unsigned int num_queues;
431*4882a593Smuzhiyun int i;
432*4882a593Smuzhiyun unsigned int queue_index;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun rcu_read_lock();
435*4882a593Smuzhiyun num_queues = READ_ONCE(vif->num_queues);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
438*4882a593Smuzhiyun unsigned long accum = 0;
439*4882a593Smuzhiyun for (queue_index = 0; queue_index < num_queues; ++queue_index) {
440*4882a593Smuzhiyun void *vif_stats = &vif->queues[queue_index].stats;
441*4882a593Smuzhiyun accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun data[i] = accum;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun rcu_read_unlock();
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun
xenvif_get_strings(struct net_device * dev,u32 stringset,u8 * data)449*4882a593Smuzhiyun static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun int i;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun switch (stringset) {
454*4882a593Smuzhiyun case ETH_SS_STATS:
455*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
456*4882a593Smuzhiyun memcpy(data + i * ETH_GSTRING_LEN,
457*4882a593Smuzhiyun xenvif_stats[i].name, ETH_GSTRING_LEN);
458*4882a593Smuzhiyun break;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun static const struct ethtool_ops xenvif_ethtool_ops = {
463*4882a593Smuzhiyun .get_link = ethtool_op_get_link,
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun .get_sset_count = xenvif_get_sset_count,
466*4882a593Smuzhiyun .get_ethtool_stats = xenvif_get_ethtool_stats,
467*4882a593Smuzhiyun .get_strings = xenvif_get_strings,
468*4882a593Smuzhiyun };
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun static const struct net_device_ops xenvif_netdev_ops = {
471*4882a593Smuzhiyun .ndo_select_queue = xenvif_select_queue,
472*4882a593Smuzhiyun .ndo_start_xmit = xenvif_start_xmit,
473*4882a593Smuzhiyun .ndo_get_stats = xenvif_get_stats,
474*4882a593Smuzhiyun .ndo_open = xenvif_open,
475*4882a593Smuzhiyun .ndo_stop = xenvif_close,
476*4882a593Smuzhiyun .ndo_change_mtu = xenvif_change_mtu,
477*4882a593Smuzhiyun .ndo_fix_features = xenvif_fix_features,
478*4882a593Smuzhiyun .ndo_set_mac_address = eth_mac_addr,
479*4882a593Smuzhiyun .ndo_validate_addr = eth_validate_addr,
480*4882a593Smuzhiyun };
481*4882a593Smuzhiyun
xenvif_alloc(struct device * parent,domid_t domid,unsigned int handle)482*4882a593Smuzhiyun struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
483*4882a593Smuzhiyun unsigned int handle)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun int err;
486*4882a593Smuzhiyun struct net_device *dev;
487*4882a593Smuzhiyun struct xenvif *vif;
488*4882a593Smuzhiyun char name[IFNAMSIZ] = {};
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
491*4882a593Smuzhiyun /* Allocate a netdev with the max. supported number of queues.
492*4882a593Smuzhiyun * When the guest selects the desired number, it will be updated
493*4882a593Smuzhiyun * via netif_set_real_num_*_queues().
494*4882a593Smuzhiyun */
495*4882a593Smuzhiyun dev = alloc_netdev_mq(sizeof(struct xenvif), name, NET_NAME_UNKNOWN,
496*4882a593Smuzhiyun ether_setup, xenvif_max_queues);
497*4882a593Smuzhiyun if (dev == NULL) {
498*4882a593Smuzhiyun pr_warn("Could not allocate netdev for %s\n", name);
499*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun SET_NETDEV_DEV(dev, parent);
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun vif = netdev_priv(dev);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun vif->domid = domid;
507*4882a593Smuzhiyun vif->handle = handle;
508*4882a593Smuzhiyun vif->can_sg = 1;
509*4882a593Smuzhiyun vif->ip_csum = 1;
510*4882a593Smuzhiyun vif->dev = dev;
511*4882a593Smuzhiyun vif->disabled = false;
512*4882a593Smuzhiyun vif->drain_timeout = msecs_to_jiffies(rx_drain_timeout_msecs);
513*4882a593Smuzhiyun vif->stall_timeout = msecs_to_jiffies(rx_stall_timeout_msecs);
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /* Start out with no queues. */
516*4882a593Smuzhiyun vif->queues = NULL;
517*4882a593Smuzhiyun vif->num_queues = 0;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun vif->xdp_headroom = 0;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun spin_lock_init(&vif->lock);
522*4882a593Smuzhiyun INIT_LIST_HEAD(&vif->fe_mcast_addr);
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun dev->netdev_ops = &xenvif_netdev_ops;
525*4882a593Smuzhiyun dev->hw_features = NETIF_F_SG |
526*4882a593Smuzhiyun NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
527*4882a593Smuzhiyun NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_FRAGLIST;
528*4882a593Smuzhiyun dev->features = dev->hw_features | NETIF_F_RXCSUM;
529*4882a593Smuzhiyun dev->ethtool_ops = &xenvif_ethtool_ops;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun dev->min_mtu = ETH_MIN_MTU;
534*4882a593Smuzhiyun dev->max_mtu = ETH_MAX_MTU - VLAN_ETH_HLEN;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun /*
537*4882a593Smuzhiyun * Initialise a dummy MAC address. We choose the numerically
538*4882a593Smuzhiyun * largest non-broadcast address to prevent the address getting
539*4882a593Smuzhiyun * stolen by an Ethernet bridge for STP purposes.
540*4882a593Smuzhiyun * (FE:FF:FF:FF:FF:FF)
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun eth_broadcast_addr(dev->dev_addr);
543*4882a593Smuzhiyun dev->dev_addr[0] &= ~0x01;
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun netif_carrier_off(dev);
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun err = register_netdev(dev);
548*4882a593Smuzhiyun if (err) {
549*4882a593Smuzhiyun netdev_warn(dev, "Could not register device: err=%d\n", err);
550*4882a593Smuzhiyun free_netdev(dev);
551*4882a593Smuzhiyun return ERR_PTR(err);
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun netdev_dbg(dev, "Successfully created xenvif\n");
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun __module_get(THIS_MODULE);
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun return vif;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun
xenvif_init_queue(struct xenvif_queue * queue)561*4882a593Smuzhiyun int xenvif_init_queue(struct xenvif_queue *queue)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun int err, i;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun queue->credit_bytes = queue->remaining_credit = ~0UL;
566*4882a593Smuzhiyun queue->credit_usec = 0UL;
567*4882a593Smuzhiyun timer_setup(&queue->credit_timeout, xenvif_tx_credit_callback, 0);
568*4882a593Smuzhiyun queue->credit_window_start = get_jiffies_64();
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun queue->rx_queue_max = XENVIF_RX_QUEUE_BYTES;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun skb_queue_head_init(&queue->rx_queue);
573*4882a593Smuzhiyun skb_queue_head_init(&queue->tx_queue);
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun queue->pending_cons = 0;
576*4882a593Smuzhiyun queue->pending_prod = MAX_PENDING_REQS;
577*4882a593Smuzhiyun for (i = 0; i < MAX_PENDING_REQS; ++i)
578*4882a593Smuzhiyun queue->pending_ring[i] = i;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun spin_lock_init(&queue->callback_lock);
581*4882a593Smuzhiyun spin_lock_init(&queue->response_lock);
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun /* If ballooning is disabled, this will consume real memory, so you
584*4882a593Smuzhiyun * better enable it. The long term solution would be to use just a
585*4882a593Smuzhiyun * bunch of valid page descriptors, without dependency on ballooning
586*4882a593Smuzhiyun */
587*4882a593Smuzhiyun err = gnttab_alloc_pages(MAX_PENDING_REQS,
588*4882a593Smuzhiyun queue->mmap_pages);
589*4882a593Smuzhiyun if (err) {
590*4882a593Smuzhiyun netdev_err(queue->vif->dev, "Could not reserve mmap_pages\n");
591*4882a593Smuzhiyun return -ENOMEM;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun for (i = 0; i < MAX_PENDING_REQS; i++) {
595*4882a593Smuzhiyun queue->pending_tx_info[i].callback_struct = (struct ubuf_info)
596*4882a593Smuzhiyun { .callback = xenvif_zerocopy_callback,
597*4882a593Smuzhiyun { { .ctx = NULL,
598*4882a593Smuzhiyun .desc = i } } };
599*4882a593Smuzhiyun queue->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun return 0;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
xenvif_carrier_on(struct xenvif * vif)605*4882a593Smuzhiyun void xenvif_carrier_on(struct xenvif *vif)
606*4882a593Smuzhiyun {
607*4882a593Smuzhiyun rtnl_lock();
608*4882a593Smuzhiyun if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
609*4882a593Smuzhiyun dev_set_mtu(vif->dev, ETH_DATA_LEN);
610*4882a593Smuzhiyun netdev_update_features(vif->dev);
611*4882a593Smuzhiyun set_bit(VIF_STATUS_CONNECTED, &vif->status);
612*4882a593Smuzhiyun if (netif_running(vif->dev))
613*4882a593Smuzhiyun xenvif_up(vif);
614*4882a593Smuzhiyun rtnl_unlock();
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun
xenvif_connect_ctrl(struct xenvif * vif,grant_ref_t ring_ref,unsigned int evtchn)617*4882a593Smuzhiyun int xenvif_connect_ctrl(struct xenvif *vif, grant_ref_t ring_ref,
618*4882a593Smuzhiyun unsigned int evtchn)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun struct net_device *dev = vif->dev;
621*4882a593Smuzhiyun void *addr;
622*4882a593Smuzhiyun struct xen_netif_ctrl_sring *shared;
623*4882a593Smuzhiyun RING_IDX rsp_prod, req_prod;
624*4882a593Smuzhiyun int err;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
627*4882a593Smuzhiyun &ring_ref, 1, &addr);
628*4882a593Smuzhiyun if (err)
629*4882a593Smuzhiyun goto err;
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun shared = (struct xen_netif_ctrl_sring *)addr;
632*4882a593Smuzhiyun rsp_prod = READ_ONCE(shared->rsp_prod);
633*4882a593Smuzhiyun req_prod = READ_ONCE(shared->req_prod);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun BACK_RING_ATTACH(&vif->ctrl, shared, rsp_prod, XEN_PAGE_SIZE);
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun err = -EIO;
638*4882a593Smuzhiyun if (req_prod - rsp_prod > RING_SIZE(&vif->ctrl))
639*4882a593Smuzhiyun goto err_unmap;
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun err = bind_interdomain_evtchn_to_irq_lateeoi(vif->domid, evtchn);
642*4882a593Smuzhiyun if (err < 0)
643*4882a593Smuzhiyun goto err_unmap;
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun vif->ctrl_irq = err;
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun xenvif_init_hash(vif);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun err = request_threaded_irq(vif->ctrl_irq, NULL, xenvif_ctrl_irq_fn,
650*4882a593Smuzhiyun IRQF_ONESHOT, "xen-netback-ctrl", vif);
651*4882a593Smuzhiyun if (err) {
652*4882a593Smuzhiyun pr_warn("Could not setup irq handler for %s\n", dev->name);
653*4882a593Smuzhiyun goto err_deinit;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun return 0;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun err_deinit:
659*4882a593Smuzhiyun xenvif_deinit_hash(vif);
660*4882a593Smuzhiyun unbind_from_irqhandler(vif->ctrl_irq, vif);
661*4882a593Smuzhiyun vif->ctrl_irq = 0;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun err_unmap:
664*4882a593Smuzhiyun xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
665*4882a593Smuzhiyun vif->ctrl.sring);
666*4882a593Smuzhiyun vif->ctrl.sring = NULL;
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun err:
669*4882a593Smuzhiyun return err;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun
xenvif_disconnect_queue(struct xenvif_queue * queue)672*4882a593Smuzhiyun static void xenvif_disconnect_queue(struct xenvif_queue *queue)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun if (queue->task) {
675*4882a593Smuzhiyun kthread_stop(queue->task);
676*4882a593Smuzhiyun put_task_struct(queue->task);
677*4882a593Smuzhiyun queue->task = NULL;
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun if (queue->dealloc_task) {
681*4882a593Smuzhiyun kthread_stop(queue->dealloc_task);
682*4882a593Smuzhiyun queue->dealloc_task = NULL;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun if (queue->napi.poll) {
686*4882a593Smuzhiyun netif_napi_del(&queue->napi);
687*4882a593Smuzhiyun queue->napi.poll = NULL;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun if (queue->tx_irq) {
691*4882a593Smuzhiyun unbind_from_irqhandler(queue->tx_irq, queue);
692*4882a593Smuzhiyun if (queue->tx_irq == queue->rx_irq)
693*4882a593Smuzhiyun queue->rx_irq = 0;
694*4882a593Smuzhiyun queue->tx_irq = 0;
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun if (queue->rx_irq) {
698*4882a593Smuzhiyun unbind_from_irqhandler(queue->rx_irq, queue);
699*4882a593Smuzhiyun queue->rx_irq = 0;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun xenvif_unmap_frontend_data_rings(queue);
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
xenvif_connect_data(struct xenvif_queue * queue,unsigned long tx_ring_ref,unsigned long rx_ring_ref,unsigned int tx_evtchn,unsigned int rx_evtchn)705*4882a593Smuzhiyun int xenvif_connect_data(struct xenvif_queue *queue,
706*4882a593Smuzhiyun unsigned long tx_ring_ref,
707*4882a593Smuzhiyun unsigned long rx_ring_ref,
708*4882a593Smuzhiyun unsigned int tx_evtchn,
709*4882a593Smuzhiyun unsigned int rx_evtchn)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun struct task_struct *task;
712*4882a593Smuzhiyun int err;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun BUG_ON(queue->tx_irq);
715*4882a593Smuzhiyun BUG_ON(queue->task);
716*4882a593Smuzhiyun BUG_ON(queue->dealloc_task);
717*4882a593Smuzhiyun
718*4882a593Smuzhiyun err = xenvif_map_frontend_data_rings(queue, tx_ring_ref,
719*4882a593Smuzhiyun rx_ring_ref);
720*4882a593Smuzhiyun if (err < 0)
721*4882a593Smuzhiyun goto err;
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun init_waitqueue_head(&queue->wq);
724*4882a593Smuzhiyun init_waitqueue_head(&queue->dealloc_wq);
725*4882a593Smuzhiyun atomic_set(&queue->inflight_packets, 0);
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun netif_napi_add(queue->vif->dev, &queue->napi, xenvif_poll,
728*4882a593Smuzhiyun XENVIF_NAPI_WEIGHT);
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun queue->stalled = true;
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun task = kthread_run(xenvif_kthread_guest_rx, queue,
733*4882a593Smuzhiyun "%s-guest-rx", queue->name);
734*4882a593Smuzhiyun if (IS_ERR(task))
735*4882a593Smuzhiyun goto kthread_err;
736*4882a593Smuzhiyun queue->task = task;
737*4882a593Smuzhiyun /*
738*4882a593Smuzhiyun * Take a reference to the task in order to prevent it from being freed
739*4882a593Smuzhiyun * if the thread function returns before kthread_stop is called.
740*4882a593Smuzhiyun */
741*4882a593Smuzhiyun get_task_struct(task);
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun task = kthread_run(xenvif_dealloc_kthread, queue,
744*4882a593Smuzhiyun "%s-dealloc", queue->name);
745*4882a593Smuzhiyun if (IS_ERR(task))
746*4882a593Smuzhiyun goto kthread_err;
747*4882a593Smuzhiyun queue->dealloc_task = task;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun if (tx_evtchn == rx_evtchn) {
750*4882a593Smuzhiyun /* feature-split-event-channels == 0 */
751*4882a593Smuzhiyun err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
752*4882a593Smuzhiyun queue->vif->domid, tx_evtchn, xenvif_interrupt, 0,
753*4882a593Smuzhiyun queue->name, queue);
754*4882a593Smuzhiyun if (err < 0)
755*4882a593Smuzhiyun goto err;
756*4882a593Smuzhiyun queue->tx_irq = queue->rx_irq = err;
757*4882a593Smuzhiyun disable_irq(queue->tx_irq);
758*4882a593Smuzhiyun } else {
759*4882a593Smuzhiyun /* feature-split-event-channels == 1 */
760*4882a593Smuzhiyun snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
761*4882a593Smuzhiyun "%s-tx", queue->name);
762*4882a593Smuzhiyun err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
763*4882a593Smuzhiyun queue->vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
764*4882a593Smuzhiyun queue->tx_irq_name, queue);
765*4882a593Smuzhiyun if (err < 0)
766*4882a593Smuzhiyun goto err;
767*4882a593Smuzhiyun queue->tx_irq = err;
768*4882a593Smuzhiyun disable_irq(queue->tx_irq);
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
771*4882a593Smuzhiyun "%s-rx", queue->name);
772*4882a593Smuzhiyun err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
773*4882a593Smuzhiyun queue->vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
774*4882a593Smuzhiyun queue->rx_irq_name, queue);
775*4882a593Smuzhiyun if (err < 0)
776*4882a593Smuzhiyun goto err;
777*4882a593Smuzhiyun queue->rx_irq = err;
778*4882a593Smuzhiyun disable_irq(queue->rx_irq);
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun return 0;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun kthread_err:
784*4882a593Smuzhiyun pr_warn("Could not allocate kthread for %s\n", queue->name);
785*4882a593Smuzhiyun err = PTR_ERR(task);
786*4882a593Smuzhiyun err:
787*4882a593Smuzhiyun xenvif_disconnect_queue(queue);
788*4882a593Smuzhiyun return err;
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun
xenvif_carrier_off(struct xenvif * vif)791*4882a593Smuzhiyun void xenvif_carrier_off(struct xenvif *vif)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun struct net_device *dev = vif->dev;
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun rtnl_lock();
796*4882a593Smuzhiyun if (test_and_clear_bit(VIF_STATUS_CONNECTED, &vif->status)) {
797*4882a593Smuzhiyun netif_carrier_off(dev); /* discard queued packets */
798*4882a593Smuzhiyun if (netif_running(dev))
799*4882a593Smuzhiyun xenvif_down(vif);
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun rtnl_unlock();
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun
xenvif_disconnect_data(struct xenvif * vif)804*4882a593Smuzhiyun void xenvif_disconnect_data(struct xenvif *vif)
805*4882a593Smuzhiyun {
806*4882a593Smuzhiyun struct xenvif_queue *queue = NULL;
807*4882a593Smuzhiyun unsigned int num_queues = vif->num_queues;
808*4882a593Smuzhiyun unsigned int queue_index;
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun xenvif_carrier_off(vif);
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun for (queue_index = 0; queue_index < num_queues; ++queue_index) {
813*4882a593Smuzhiyun queue = &vif->queues[queue_index];
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun xenvif_disconnect_queue(queue);
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun xenvif_mcast_addr_list_free(vif);
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun
xenvif_disconnect_ctrl(struct xenvif * vif)821*4882a593Smuzhiyun void xenvif_disconnect_ctrl(struct xenvif *vif)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun if (vif->ctrl_irq) {
824*4882a593Smuzhiyun xenvif_deinit_hash(vif);
825*4882a593Smuzhiyun unbind_from_irqhandler(vif->ctrl_irq, vif);
826*4882a593Smuzhiyun vif->ctrl_irq = 0;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun if (vif->ctrl.sring) {
830*4882a593Smuzhiyun xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
831*4882a593Smuzhiyun vif->ctrl.sring);
832*4882a593Smuzhiyun vif->ctrl.sring = NULL;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun /* Reverse the relevant parts of xenvif_init_queue().
837*4882a593Smuzhiyun * Used for queue teardown from xenvif_free(), and on the
838*4882a593Smuzhiyun * error handling paths in xenbus.c:connect().
839*4882a593Smuzhiyun */
xenvif_deinit_queue(struct xenvif_queue * queue)840*4882a593Smuzhiyun void xenvif_deinit_queue(struct xenvif_queue *queue)
841*4882a593Smuzhiyun {
842*4882a593Smuzhiyun gnttab_free_pages(MAX_PENDING_REQS, queue->mmap_pages);
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun
xenvif_free(struct xenvif * vif)845*4882a593Smuzhiyun void xenvif_free(struct xenvif *vif)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun struct xenvif_queue *queues = vif->queues;
848*4882a593Smuzhiyun unsigned int num_queues = vif->num_queues;
849*4882a593Smuzhiyun unsigned int queue_index;
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun unregister_netdev(vif->dev);
852*4882a593Smuzhiyun free_netdev(vif->dev);
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun for (queue_index = 0; queue_index < num_queues; ++queue_index)
855*4882a593Smuzhiyun xenvif_deinit_queue(&queues[queue_index]);
856*4882a593Smuzhiyun vfree(queues);
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun module_put(THIS_MODULE);
859*4882a593Smuzhiyun }
860