1 /******************************************************************************
2 * arch/xen/drivers/netif/backend/interface.c
4 * Network-device interface management.
6 * Copyright (c) 2004-2005, Keir Fraser
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #include <linux/ethtool.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/delay.h>
37 #include <linux/vmalloc.h>
38 #include <xen/evtchn.h>
41 * Module parameter 'queue_length':
43 * Enables queuing in the network stack when a client has run out of receive
44 * descriptors. Although this feature can improve receive bandwidth by avoiding
45 * packet loss, it can also result in packets sitting in the 'tx_queue' for
46 * unbounded time. This is bad if those packets hold onto foreign resources.
47 * For example, consider a packet that holds onto resources belonging to the
48 * guest for which it is queued (e.g., packet received on vif1.0, destined for
49 * vif1.1 which is not activated in the guest): in this situation the guest
50 * will never be destroyed, unless vif1.1 is taken down. To avoid this, we
51 * run a timer (tx_queue_timeout) to drain the queue when the interface is
54 static unsigned long netbk_queue_length = 32;
55 module_param_named(queue_length, netbk_queue_length, ulong, 0644);
57 static void __netif_up(netif_t *netif)
59 unsigned int group = 0;
60 unsigned int min_groups = atomic_read(&xen_netbk[0].nr_groups);
63 /* Find the list which contains least number of domains. */
64 for (i = 1; i < netbk_nr_groups; i++) {
65 unsigned int nr_groups = atomic_read(&xen_netbk[i].nr_groups);
67 if (nr_groups < min_groups) {
69 min_groups = nr_groups;
73 atomic_inc(&xen_netbk[group].nr_groups);
76 enable_irq(netif->irq);
77 netif_schedule_work(netif);
80 static void __netif_down(netif_t *netif)
82 struct xen_netbk *netbk = xen_netbk + netif->group;
84 disable_irq(netif->irq);
85 netif_deschedule_work(netif);
87 netif->group = UINT_MAX;
88 atomic_dec(&netbk->nr_groups);
91 static int net_open(struct net_device *dev)
93 netif_t *netif = netdev_priv(dev);
94 if (netback_carrier_ok(netif)) {
96 netif_start_queue(dev);
101 static int net_close(struct net_device *dev)
103 netif_t *netif = netdev_priv(dev);
104 if (netback_carrier_ok(netif))
106 netif_stop_queue(dev);
110 static int netbk_change_mtu(struct net_device *dev, int mtu)
112 int max = netbk_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
120 static netdev_features_t netbk_fix_features(struct net_device *dev,
121 netdev_features_t features)
123 netif_t *netif = netdev_priv(dev);
126 features &= ~NETIF_F_SG;
128 features &= ~NETIF_F_TSO;
130 features &= ~NETIF_F_IP_CSUM;
135 static void netbk_get_drvinfo(struct net_device *dev,
136 struct ethtool_drvinfo *info)
138 strcpy(info->driver, "netbk");
139 strlcpy(info->bus_info, dev_name(dev->dev.parent),
140 ARRAY_SIZE(info->bus_info));
143 static const struct netif_stat {
144 char name[ETH_GSTRING_LEN];
147 { "copied_skbs", offsetof(netif_t, nr_copied_skbs) / sizeof(long) },
148 { "rx_gso_csum_fixups", offsetof(netif_t, rx_gso_csum_fixups) / sizeof(long) },
151 static int netbk_get_sset_count(struct net_device *dev, int sset)
155 return ARRAY_SIZE(netbk_stats);
160 static void netbk_get_ethtool_stats(struct net_device *dev,
161 struct ethtool_stats *stats, u64 * data)
163 unsigned long *np = netdev_priv(dev);
166 for (i = 0; i < ARRAY_SIZE(netbk_stats); i++)
167 data[i] = np[netbk_stats[i].offset];
170 static void netbk_get_strings(struct net_device *dev, u32 stringset, u8 * data)
176 for (i = 0; i < ARRAY_SIZE(netbk_stats); i++)
177 memcpy(data + i * ETH_GSTRING_LEN,
178 netbk_stats[i].name, ETH_GSTRING_LEN);
183 static const struct ethtool_ops network_ethtool_ops =
185 .get_drvinfo = netbk_get_drvinfo,
186 .get_link = ethtool_op_get_link,
188 .get_sset_count = netbk_get_sset_count,
189 .get_ethtool_stats = netbk_get_ethtool_stats,
190 .get_strings = netbk_get_strings,
193 static const struct net_device_ops netif_be_netdev_ops = {
194 .ndo_open = net_open,
195 .ndo_stop = net_close,
196 .ndo_start_xmit = netif_be_start_xmit,
197 .ndo_change_mtu = netbk_change_mtu,
198 .ndo_fix_features = netbk_fix_features,
199 .ndo_set_mac_address = eth_mac_addr,
200 .ndo_validate_addr = eth_validate_addr,
203 netif_t *netif_alloc(struct device *parent, domid_t domid, unsigned int handle)
206 struct net_device *dev;
208 char name[IFNAMSIZ] = {};
210 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
211 dev = alloc_netdev(sizeof(netif_t), name, ether_setup);
213 DPRINTK("Could not create netif: out of memory\n");
214 return ERR_PTR(-ENOMEM);
217 SET_NETDEV_DEV(dev, parent);
219 netif = netdev_priv(dev);
220 netif->domid = domid;
221 netif->group = UINT_MAX;
222 netif->handle = handle;
225 atomic_set(&netif->refcnt, 1);
226 init_waitqueue_head(&netif->waiting_to_free);
229 netback_carrier_off(netif);
231 netif->credit_bytes = netif->remaining_credit = ~0UL;
232 netif->credit_usec = 0UL;
233 init_timer(&netif->credit_timeout);
234 /* Initialize 'expires' now: it's used to track the credit window. */
235 netif->credit_timeout.expires = jiffies;
237 init_timer(&netif->tx_queue_timeout);
239 dev->netdev_ops = &netif_be_netdev_ops;
241 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
242 dev->features = dev->hw_features;
244 SET_ETHTOOL_OPS(dev, &network_ethtool_ops);
246 dev->tx_queue_len = netbk_queue_length;
249 * Initialise a dummy MAC address. We choose the numerically
250 * largest non-broadcast address to prevent the address getting
251 * stolen by an Ethernet bridge for STP purposes.
252 * (FE:FF:FF:FF:FF:FF)
254 memset(dev->dev_addr, 0xFF, ETH_ALEN);
255 dev->dev_addr[0] &= ~0x01;
258 err = register_netdevice(dev);
261 DPRINTK("Could not register new net device %s: err=%d\n",
267 DPRINTK("Successfully created netif\n");
271 int netif_map(struct backend_info *be, grant_ref_t tx_ring_ref,
272 grant_ref_t rx_ring_ref, evtchn_port_t evtchn)
274 netif_t *netif = be->netif;
275 struct vm_struct *area;
277 netif_tx_sring_t *txs;
278 netif_rx_sring_t *rxs;
280 /* Already connected through? */
284 area = xenbus_map_ring_valloc(be->dev, tx_ring_ref);
286 return PTR_ERR(area);
287 netif->tx_comms_area = area;
288 area = xenbus_map_ring_valloc(be->dev, rx_ring_ref);
293 netif->rx_comms_area = area;
295 err = bind_interdomain_evtchn_to_irqhandler(
296 netif->domid, evtchn, netif_be_int, 0,
297 netif->dev->name, netif);
300 BUG_ON(err < DYNIRQ_BASE || err >= DYNIRQ_BASE + NR_DYNIRQS);
302 disable_irq(netif->irq);
304 txs = (netif_tx_sring_t *)netif->tx_comms_area->addr;
305 BACK_RING_INIT(&netif->tx, txs, PAGE_SIZE);
307 rxs = (netif_rx_sring_t *)
308 ((char *)netif->rx_comms_area->addr);
309 BACK_RING_INIT(&netif->rx, rxs, PAGE_SIZE);
311 netif->rx_req_cons_peek = 0;
316 if (!netif->can_sg && netif->dev->mtu > ETH_DATA_LEN)
317 dev_set_mtu(netif->dev, ETH_DATA_LEN);
318 netdev_update_features(netif->dev);
319 netback_carrier_on(netif);
320 if (netif_running(netif->dev))
326 xenbus_unmap_ring_vfree(be->dev, netif->rx_comms_area);
328 xenbus_unmap_ring_vfree(be->dev, netif->tx_comms_area);
332 void netif_disconnect(struct backend_info *be)
334 netif_t *netif = be->netif;
336 if (netback_carrier_ok(netif)) {
338 netback_carrier_off(netif);
339 netif_carrier_off(netif->dev); /* discard queued packets */
340 if (netif_running(netif->dev))
346 atomic_dec(&netif->refcnt);
347 wait_event(netif->waiting_to_free, atomic_read(&netif->refcnt) == 0);
349 del_timer_sync(&netif->credit_timeout);
350 del_timer_sync(&netif->tx_queue_timeout);
353 unbind_from_irqhandler(netif->irq, netif);
355 unregister_netdev(netif->dev);
357 if (netif->tx.sring) {
358 xenbus_unmap_ring_vfree(be->dev, netif->tx_comms_area);
359 xenbus_unmap_ring_vfree(be->dev, netif->rx_comms_area);
362 free_netdev(netif->dev);