ia64/linux-2.6.18-xen.hg

view drivers/xen/netback/interface.c @ 792:db9857bb0320

netback: add ethtool stat to track copied skbs.

Copied skbs should be rare but we have no way of verifying that.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Feb 05 15:24:46 2009 +0000 (2009-02-05)
parents 51decc39e5e7
children 3aa9b8a7876b
line source
1 /******************************************************************************
2 * arch/xen/drivers/netif/backend/interface.c
3 *
4 * Network-device interface management.
5 *
6 * Copyright (c) 2004-2005, Keir Fraser
7 *
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:
13 *
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:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
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
30 * IN THE SOFTWARE.
31 */
33 #include "common.h"
34 #include <linux/ethtool.h>
35 #include <linux/rtnetlink.h>
37 /*
38 * Module parameter 'queue_length':
39 *
40 * Enables queuing in the network stack when a client has run out of receive
41 * descriptors. Although this feature can improve receive bandwidth by avoiding
42 * packet loss, it can also result in packets sitting in the 'tx_queue' for
43 * unbounded time. This is bad if those packets hold onto foreign resources.
44 * For example, consider a packet that holds onto resources belonging to the
45 * guest for which it is queued (e.g., packet received on vif1.0, destined for
46 * vif1.1 which is not activated in the guest): in this situation the guest
47 * will never be destroyed, unless vif1.1 is taken down. To avoid this, we
48 * run a timer (tx_queue_timeout) to drain the queue when the interface is
49 * blocked.
50 */
51 static unsigned long netbk_queue_length = 32;
52 module_param_named(queue_length, netbk_queue_length, ulong, 0);
54 static void __netif_up(netif_t *netif)
55 {
56 enable_irq(netif->irq);
57 netif_schedule_work(netif);
58 }
60 static void __netif_down(netif_t *netif)
61 {
62 disable_irq(netif->irq);
63 netif_deschedule_work(netif);
64 }
66 static int net_open(struct net_device *dev)
67 {
68 netif_t *netif = netdev_priv(dev);
69 if (netback_carrier_ok(netif)) {
70 __netif_up(netif);
71 netif_start_queue(dev);
72 }
73 return 0;
74 }
76 static int net_close(struct net_device *dev)
77 {
78 netif_t *netif = netdev_priv(dev);
79 if (netback_carrier_ok(netif))
80 __netif_down(netif);
81 netif_stop_queue(dev);
82 return 0;
83 }
85 static int netbk_change_mtu(struct net_device *dev, int mtu)
86 {
87 int max = netbk_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
89 if (mtu > max)
90 return -EINVAL;
91 dev->mtu = mtu;
92 return 0;
93 }
95 static int netbk_set_sg(struct net_device *dev, u32 data)
96 {
97 if (data) {
98 netif_t *netif = netdev_priv(dev);
100 if (!(netif->features & NETIF_F_SG))
101 return -ENOSYS;
102 }
104 return ethtool_op_set_sg(dev, data);
105 }
107 static int netbk_set_tso(struct net_device *dev, u32 data)
108 {
109 if (data) {
110 netif_t *netif = netdev_priv(dev);
112 if (!(netif->features & NETIF_F_TSO))
113 return -ENOSYS;
114 }
116 return ethtool_op_set_tso(dev, data);
117 }
119 static void netbk_get_drvinfo(struct net_device *dev,
120 struct ethtool_drvinfo *info)
121 {
122 strcpy(info->driver, "netbk");
123 }
125 static const struct netif_stat {
126 char name[ETH_GSTRING_LEN];
127 u16 offset;
128 } netbk_stats[] = {
129 { "copied_skbs", offsetof(netif_t, nr_copied_skbs) },
130 };
132 static int netbk_get_stats_count(struct net_device *dev)
133 {
134 return ARRAY_SIZE(netbk_stats);
135 }
137 static void netbk_get_ethtool_stats(struct net_device *dev,
138 struct ethtool_stats *stats, u64 * data)
139 {
140 void *netif = netdev_priv(dev);
141 int i;
143 for (i = 0; i < ARRAY_SIZE(netbk_stats); i++)
144 data[i] = *(int *)(netif + netbk_stats[i].offset);
145 }
147 static void netbk_get_strings(struct net_device *dev, u32 stringset, u8 * data)
148 {
149 int i;
151 switch (stringset) {
152 case ETH_SS_STATS:
153 for (i = 0; i < ARRAY_SIZE(netbk_stats); i++)
154 memcpy(data + i * ETH_GSTRING_LEN,
155 netbk_stats[i].name, ETH_GSTRING_LEN);
156 break;
157 }
158 }
160 static struct ethtool_ops network_ethtool_ops =
161 {
162 .get_drvinfo = netbk_get_drvinfo,
164 .get_tx_csum = ethtool_op_get_tx_csum,
165 .set_tx_csum = ethtool_op_set_tx_csum,
166 .get_sg = ethtool_op_get_sg,
167 .set_sg = netbk_set_sg,
168 .get_tso = ethtool_op_get_tso,
169 .set_tso = netbk_set_tso,
170 .get_link = ethtool_op_get_link,
172 .get_stats_count = netbk_get_stats_count,
173 .get_ethtool_stats = netbk_get_ethtool_stats,
174 .get_strings = netbk_get_strings,
175 };
177 netif_t *netif_alloc(domid_t domid, unsigned int handle)
178 {
179 int err = 0;
180 struct net_device *dev;
181 netif_t *netif;
182 char name[IFNAMSIZ] = {};
184 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
185 dev = alloc_netdev(sizeof(netif_t), name, ether_setup);
186 if (dev == NULL) {
187 DPRINTK("Could not create netif: out of memory\n");
188 return ERR_PTR(-ENOMEM);
189 }
191 netif = netdev_priv(dev);
192 memset(netif, 0, sizeof(*netif));
193 netif->domid = domid;
194 netif->handle = handle;
195 atomic_set(&netif->refcnt, 1);
196 init_waitqueue_head(&netif->waiting_to_free);
197 netif->dev = dev;
199 netback_carrier_off(netif);
201 netif->credit_bytes = netif->remaining_credit = ~0UL;
202 netif->credit_usec = 0UL;
203 init_timer(&netif->credit_timeout);
204 /* Initialize 'expires' now: it's used to track the credit window. */
205 netif->credit_timeout.expires = jiffies;
207 init_timer(&netif->tx_queue_timeout);
209 dev->hard_start_xmit = netif_be_start_xmit;
210 dev->get_stats = netif_be_get_stats;
211 dev->open = net_open;
212 dev->stop = net_close;
213 dev->change_mtu = netbk_change_mtu;
214 dev->features = NETIF_F_IP_CSUM;
216 SET_ETHTOOL_OPS(dev, &network_ethtool_ops);
218 dev->tx_queue_len = netbk_queue_length;
220 /*
221 * Initialise a dummy MAC address. We choose the numerically
222 * largest non-broadcast address to prevent the address getting
223 * stolen by an Ethernet bridge for STP purposes.
224 * (FE:FF:FF:FF:FF:FF)
225 */
226 memset(dev->dev_addr, 0xFF, ETH_ALEN);
227 dev->dev_addr[0] &= ~0x01;
229 rtnl_lock();
230 err = register_netdevice(dev);
231 rtnl_unlock();
232 if (err) {
233 DPRINTK("Could not register new net device %s: err=%d\n",
234 dev->name, err);
235 free_netdev(dev);
236 return ERR_PTR(err);
237 }
239 DPRINTK("Successfully created netif\n");
240 return netif;
241 }
243 static int map_frontend_pages(
244 netif_t *netif, grant_ref_t tx_ring_ref, grant_ref_t rx_ring_ref)
245 {
246 struct gnttab_map_grant_ref op;
248 gnttab_set_map_op(&op, (unsigned long)netif->tx_comms_area->addr,
249 GNTMAP_host_map, tx_ring_ref, netif->domid);
251 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
252 BUG();
254 if (op.status) {
255 DPRINTK(" Gnttab failure mapping tx_ring_ref!\n");
256 return op.status;
257 }
259 netif->tx_shmem_ref = tx_ring_ref;
260 netif->tx_shmem_handle = op.handle;
262 gnttab_set_map_op(&op, (unsigned long)netif->rx_comms_area->addr,
263 GNTMAP_host_map, rx_ring_ref, netif->domid);
265 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
266 BUG();
268 if (op.status) {
269 struct gnttab_unmap_grant_ref unop;
271 gnttab_set_unmap_op(&unop,
272 (unsigned long)netif->tx_comms_area->addr,
273 GNTMAP_host_map, netif->tx_shmem_handle);
274 VOID(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
275 &unop, 1));
276 DPRINTK(" Gnttab failure mapping rx_ring_ref!\n");
277 return op.status;
278 }
280 netif->rx_shmem_ref = rx_ring_ref;
281 netif->rx_shmem_handle = op.handle;
283 return 0;
284 }
286 static void unmap_frontend_pages(netif_t *netif)
287 {
288 struct gnttab_unmap_grant_ref op;
290 gnttab_set_unmap_op(&op, (unsigned long)netif->tx_comms_area->addr,
291 GNTMAP_host_map, netif->tx_shmem_handle);
293 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
294 BUG();
296 gnttab_set_unmap_op(&op, (unsigned long)netif->rx_comms_area->addr,
297 GNTMAP_host_map, netif->rx_shmem_handle);
299 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
300 BUG();
301 }
303 int netif_map(netif_t *netif, unsigned long tx_ring_ref,
304 unsigned long rx_ring_ref, unsigned int evtchn)
305 {
306 int err = -ENOMEM;
307 netif_tx_sring_t *txs;
308 netif_rx_sring_t *rxs;
310 /* Already connected through? */
311 if (netif->irq)
312 return 0;
314 netif->tx_comms_area = alloc_vm_area(PAGE_SIZE);
315 if (netif->tx_comms_area == NULL)
316 return -ENOMEM;
317 netif->rx_comms_area = alloc_vm_area(PAGE_SIZE);
318 if (netif->rx_comms_area == NULL)
319 goto err_rx;
321 err = map_frontend_pages(netif, tx_ring_ref, rx_ring_ref);
322 if (err)
323 goto err_map;
325 err = bind_interdomain_evtchn_to_irqhandler(
326 netif->domid, evtchn, netif_be_int, 0,
327 netif->dev->name, netif);
328 if (err < 0)
329 goto err_hypervisor;
330 netif->irq = err;
331 disable_irq(netif->irq);
333 txs = (netif_tx_sring_t *)netif->tx_comms_area->addr;
334 BACK_RING_INIT(&netif->tx, txs, PAGE_SIZE);
336 rxs = (netif_rx_sring_t *)
337 ((char *)netif->rx_comms_area->addr);
338 BACK_RING_INIT(&netif->rx, rxs, PAGE_SIZE);
340 netif->rx_req_cons_peek = 0;
342 netif_get(netif);
344 rtnl_lock();
345 netback_carrier_on(netif);
346 if (netif_running(netif->dev))
347 __netif_up(netif);
348 rtnl_unlock();
350 return 0;
351 err_hypervisor:
352 unmap_frontend_pages(netif);
353 err_map:
354 free_vm_area(netif->rx_comms_area);
355 err_rx:
356 free_vm_area(netif->tx_comms_area);
357 return err;
358 }
360 void netif_disconnect(netif_t *netif)
361 {
362 if (netback_carrier_ok(netif)) {
363 rtnl_lock();
364 netback_carrier_off(netif);
365 netif_carrier_off(netif->dev); /* discard queued packets */
366 if (netif_running(netif->dev))
367 __netif_down(netif);
368 rtnl_unlock();
369 netif_put(netif);
370 }
372 atomic_dec(&netif->refcnt);
373 wait_event(netif->waiting_to_free, atomic_read(&netif->refcnt) == 0);
375 del_timer_sync(&netif->credit_timeout);
376 del_timer_sync(&netif->tx_queue_timeout);
378 if (netif->irq)
379 unbind_from_irqhandler(netif->irq, netif);
381 unregister_netdev(netif->dev);
383 if (netif->tx.sring) {
384 unmap_frontend_pages(netif);
385 free_vm_area(netif->tx_comms_area);
386 free_vm_area(netif->rx_comms_area);
387 }
389 free_netdev(netif->dev);
390 }