2 * Network-device interface management.
4 * Copyright (c) 2004-2005, Keir Fraser
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation; or, when distributed
9 * separately from the Linux kernel or incorporated into other
10 * software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #include <linux/kthread.h>
34 #include <linux/ethtool.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/if_vlan.h>
38 #include <xen/events.h>
39 #include <asm/xen/hypercall.h>
41 #define XENVIF_QUEUE_LENGTH 32
42 #define XENVIF_NAPI_WEIGHT 64
44 int xenvif_schedulable(struct xenvif
*vif
)
46 return netif_running(vif
->dev
) && netif_carrier_ok(vif
->dev
);
49 static int xenvif_rx_schedulable(struct xenvif
*vif
)
51 return xenvif_schedulable(vif
) && !xenvif_rx_ring_full(vif
);
54 static irqreturn_t
xenvif_tx_interrupt(int irq
, void *dev_id
)
56 struct xenvif
*vif
= dev_id
;
58 if (RING_HAS_UNCONSUMED_REQUESTS(&vif
->tx
))
59 napi_schedule(&vif
->napi
);
64 static int xenvif_poll(struct napi_struct
*napi
, int budget
)
66 struct xenvif
*vif
= container_of(napi
, struct xenvif
, napi
);
69 work_done
= xenvif_tx_action(vif
, budget
);
71 if (work_done
< budget
) {
75 /* It is necessary to disable IRQ before calling
76 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
77 * lose event from the frontend.
80 * RING_HAS_UNCONSUMED_REQUESTS
81 * <frontend generates event to trigger napi_schedule>
84 * This handler is still in scheduled state so the
85 * event has no effect at all. After __napi_complete
86 * this handler is descheduled and cannot get
87 * scheduled again. We lose event in this case and the ring
88 * will be completely stalled.
91 local_irq_save(flags
);
93 RING_FINAL_CHECK_FOR_REQUESTS(&vif
->tx
, more_to_do
);
95 __napi_complete(napi
);
97 local_irq_restore(flags
);
103 static irqreturn_t
xenvif_rx_interrupt(int irq
, void *dev_id
)
105 struct xenvif
*vif
= dev_id
;
107 if (xenvif_rx_schedulable(vif
))
108 netif_wake_queue(vif
->dev
);
113 static irqreturn_t
xenvif_interrupt(int irq
, void *dev_id
)
115 xenvif_tx_interrupt(irq
, dev_id
);
116 xenvif_rx_interrupt(irq
, dev_id
);
121 static int xenvif_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
123 struct xenvif
*vif
= netdev_priv(dev
);
125 BUG_ON(skb
->dev
!= dev
);
127 /* Drop the packet if vif is not ready */
128 if (vif
->task
== NULL
)
131 /* Drop the packet if the target domain has no receive buffers. */
132 if (!xenvif_rx_schedulable(vif
))
135 /* Reserve ring slots for the worst-case number of fragments. */
136 vif
->rx_req_cons_peek
+= xenvif_count_skb_slots(vif
, skb
);
138 if (vif
->can_queue
&& xenvif_must_stop_queue(vif
))
139 netif_stop_queue(dev
);
141 xenvif_queue_tx_skb(vif
, skb
);
146 vif
->dev
->stats
.tx_dropped
++;
151 void xenvif_notify_tx_completion(struct xenvif
*vif
)
153 if (netif_queue_stopped(vif
->dev
) && xenvif_rx_schedulable(vif
))
154 netif_wake_queue(vif
->dev
);
157 static struct net_device_stats
*xenvif_get_stats(struct net_device
*dev
)
159 struct xenvif
*vif
= netdev_priv(dev
);
160 return &vif
->dev
->stats
;
163 static void xenvif_up(struct xenvif
*vif
)
165 napi_enable(&vif
->napi
);
166 enable_irq(vif
->tx_irq
);
167 if (vif
->tx_irq
!= vif
->rx_irq
)
168 enable_irq(vif
->rx_irq
);
169 xenvif_check_rx_xenvif(vif
);
172 static void xenvif_down(struct xenvif
*vif
)
174 napi_disable(&vif
->napi
);
175 disable_irq(vif
->tx_irq
);
176 if (vif
->tx_irq
!= vif
->rx_irq
)
177 disable_irq(vif
->rx_irq
);
178 del_timer_sync(&vif
->credit_timeout
);
181 static int xenvif_open(struct net_device
*dev
)
183 struct xenvif
*vif
= netdev_priv(dev
);
184 if (netif_carrier_ok(dev
))
186 netif_start_queue(dev
);
190 static int xenvif_close(struct net_device
*dev
)
192 struct xenvif
*vif
= netdev_priv(dev
);
193 if (netif_carrier_ok(dev
))
195 netif_stop_queue(dev
);
199 static int xenvif_change_mtu(struct net_device
*dev
, int mtu
)
201 struct xenvif
*vif
= netdev_priv(dev
);
202 int max
= vif
->can_sg
? 65535 - VLAN_ETH_HLEN
: ETH_DATA_LEN
;
210 static netdev_features_t
xenvif_fix_features(struct net_device
*dev
,
211 netdev_features_t features
)
213 struct xenvif
*vif
= netdev_priv(dev
);
216 features
&= ~NETIF_F_SG
;
217 if (!vif
->gso
&& !vif
->gso_prefix
)
218 features
&= ~NETIF_F_TSO
;
220 features
&= ~NETIF_F_IP_CSUM
;
225 static const struct xenvif_stat
{
226 char name
[ETH_GSTRING_LEN
];
230 "rx_gso_checksum_fixup",
231 offsetof(struct xenvif
, rx_gso_checksum_fixup
)
235 static int xenvif_get_sset_count(struct net_device
*dev
, int string_set
)
237 switch (string_set
) {
239 return ARRAY_SIZE(xenvif_stats
);
245 static void xenvif_get_ethtool_stats(struct net_device
*dev
,
246 struct ethtool_stats
*stats
, u64
* data
)
248 void *vif
= netdev_priv(dev
);
251 for (i
= 0; i
< ARRAY_SIZE(xenvif_stats
); i
++)
252 data
[i
] = *(unsigned long *)(vif
+ xenvif_stats
[i
].offset
);
255 static void xenvif_get_strings(struct net_device
*dev
, u32 stringset
, u8
* data
)
261 for (i
= 0; i
< ARRAY_SIZE(xenvif_stats
); i
++)
262 memcpy(data
+ i
* ETH_GSTRING_LEN
,
263 xenvif_stats
[i
].name
, ETH_GSTRING_LEN
);
268 static const struct ethtool_ops xenvif_ethtool_ops
= {
269 .get_link
= ethtool_op_get_link
,
271 .get_sset_count
= xenvif_get_sset_count
,
272 .get_ethtool_stats
= xenvif_get_ethtool_stats
,
273 .get_strings
= xenvif_get_strings
,
276 static const struct net_device_ops xenvif_netdev_ops
= {
277 .ndo_start_xmit
= xenvif_start_xmit
,
278 .ndo_get_stats
= xenvif_get_stats
,
279 .ndo_open
= xenvif_open
,
280 .ndo_stop
= xenvif_close
,
281 .ndo_change_mtu
= xenvif_change_mtu
,
282 .ndo_fix_features
= xenvif_fix_features
,
283 .ndo_set_mac_address
= eth_mac_addr
,
284 .ndo_validate_addr
= eth_validate_addr
,
287 struct xenvif
*xenvif_alloc(struct device
*parent
, domid_t domid
,
291 struct net_device
*dev
;
293 char name
[IFNAMSIZ
] = {};
296 snprintf(name
, IFNAMSIZ
- 1, "vif%u.%u", domid
, handle
);
297 dev
= alloc_netdev(sizeof(struct xenvif
), name
, ether_setup
);
299 pr_warn("Could not allocate netdev for %s\n", name
);
300 return ERR_PTR(-ENOMEM
);
303 SET_NETDEV_DEV(dev
, parent
);
305 vif
= netdev_priv(dev
);
307 vif
->handle
= handle
;
312 vif
->credit_bytes
= vif
->remaining_credit
= ~0UL;
313 vif
->credit_usec
= 0UL;
314 init_timer(&vif
->credit_timeout
);
315 /* Initialize 'expires' now: it's used to track the credit window. */
316 vif
->credit_timeout
.expires
= jiffies
;
318 dev
->netdev_ops
= &xenvif_netdev_ops
;
319 dev
->hw_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_TSO
;
320 dev
->features
= dev
->hw_features
;
321 SET_ETHTOOL_OPS(dev
, &xenvif_ethtool_ops
);
323 dev
->tx_queue_len
= XENVIF_QUEUE_LENGTH
;
325 skb_queue_head_init(&vif
->rx_queue
);
326 skb_queue_head_init(&vif
->tx_queue
);
328 vif
->pending_cons
= 0;
329 vif
->pending_prod
= MAX_PENDING_REQS
;
330 for (i
= 0; i
< MAX_PENDING_REQS
; i
++)
331 vif
->pending_ring
[i
] = i
;
332 for (i
= 0; i
< MAX_PENDING_REQS
; i
++)
333 vif
->mmap_pages
[i
] = NULL
;
336 * Initialise a dummy MAC address. We choose the numerically
337 * largest non-broadcast address to prevent the address getting
338 * stolen by an Ethernet bridge for STP purposes.
339 * (FE:FF:FF:FF:FF:FF)
341 memset(dev
->dev_addr
, 0xFF, ETH_ALEN
);
342 dev
->dev_addr
[0] &= ~0x01;
344 netif_napi_add(dev
, &vif
->napi
, xenvif_poll
, XENVIF_NAPI_WEIGHT
);
346 netif_carrier_off(dev
);
348 err
= register_netdev(dev
);
350 netdev_warn(dev
, "Could not register device: err=%d\n", err
);
355 netdev_dbg(dev
, "Successfully created xenvif\n");
357 __module_get(THIS_MODULE
);
362 int xenvif_connect(struct xenvif
*vif
, unsigned long tx_ring_ref
,
363 unsigned long rx_ring_ref
, unsigned int tx_evtchn
,
364 unsigned int rx_evtchn
)
368 /* Already connected through? */
372 err
= xenvif_map_frontend_rings(vif
, tx_ring_ref
, rx_ring_ref
);
376 if (tx_evtchn
== rx_evtchn
) {
377 /* feature-split-event-channels == 0 */
378 err
= bind_interdomain_evtchn_to_irqhandler(
379 vif
->domid
, tx_evtchn
, xenvif_interrupt
, 0,
380 vif
->dev
->name
, vif
);
383 vif
->tx_irq
= vif
->rx_irq
= err
;
384 disable_irq(vif
->tx_irq
);
386 /* feature-split-event-channels == 1 */
387 snprintf(vif
->tx_irq_name
, sizeof(vif
->tx_irq_name
),
388 "%s-tx", vif
->dev
->name
);
389 err
= bind_interdomain_evtchn_to_irqhandler(
390 vif
->domid
, tx_evtchn
, xenvif_tx_interrupt
, 0,
391 vif
->tx_irq_name
, vif
);
395 disable_irq(vif
->tx_irq
);
397 snprintf(vif
->rx_irq_name
, sizeof(vif
->rx_irq_name
),
398 "%s-rx", vif
->dev
->name
);
399 err
= bind_interdomain_evtchn_to_irqhandler(
400 vif
->domid
, rx_evtchn
, xenvif_rx_interrupt
, 0,
401 vif
->rx_irq_name
, vif
);
405 disable_irq(vif
->rx_irq
);
408 init_waitqueue_head(&vif
->wq
);
409 vif
->task
= kthread_create(xenvif_kthread
,
410 (void *)vif
, "%s", vif
->dev
->name
);
411 if (IS_ERR(vif
->task
)) {
412 pr_warn("Could not allocate kthread for %s\n", vif
->dev
->name
);
413 err
= PTR_ERR(vif
->task
);
418 if (!vif
->can_sg
&& vif
->dev
->mtu
> ETH_DATA_LEN
)
419 dev_set_mtu(vif
->dev
, ETH_DATA_LEN
);
420 netdev_update_features(vif
->dev
);
421 netif_carrier_on(vif
->dev
);
422 if (netif_running(vif
->dev
))
426 wake_up_process(vif
->task
);
431 unbind_from_irqhandler(vif
->rx_irq
, vif
);
434 unbind_from_irqhandler(vif
->tx_irq
, vif
);
437 xenvif_unmap_frontend_rings(vif
);
439 module_put(THIS_MODULE
);
443 void xenvif_carrier_off(struct xenvif
*vif
)
445 struct net_device
*dev
= vif
->dev
;
448 netif_carrier_off(dev
); /* discard queued packets */
449 if (netif_running(dev
))
454 void xenvif_disconnect(struct xenvif
*vif
)
456 if (netif_carrier_ok(vif
->dev
))
457 xenvif_carrier_off(vif
);
460 if (vif
->tx_irq
== vif
->rx_irq
)
461 unbind_from_irqhandler(vif
->tx_irq
, vif
);
463 unbind_from_irqhandler(vif
->tx_irq
, vif
);
464 unbind_from_irqhandler(vif
->rx_irq
, vif
);
470 kthread_stop(vif
->task
);
472 xenvif_unmap_frontend_rings(vif
);
475 void xenvif_free(struct xenvif
*vif
)
477 netif_napi_del(&vif
->napi
);
479 unregister_netdev(vif
->dev
);
481 free_netdev(vif
->dev
);
483 module_put(THIS_MODULE
);