blob: 625c6f49cfba9923e80dc0834d49a6a6b870f1b3 [file] [log] [blame]
Ian Campbellf942dc22011-03-15 00:06:18 +00001/*
2 * Network-device interface management.
3 *
4 * Copyright (c) 2004-2005, Keir Fraser
5 *
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:
11 *
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:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
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
28 * IN THE SOFTWARE.
29 */
30
31#include "common.h"
32
Wei Liub3f980b2013-08-26 12:59:38 +010033#include <linux/kthread.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000034#include <linux/ethtool.h>
35#include <linux/rtnetlink.h>
36#include <linux/if_vlan.h>
37
38#include <xen/events.h>
39#include <asm/xen/hypercall.h>
40
41#define XENVIF_QUEUE_LENGTH 32
Wei Liub3f980b2013-08-26 12:59:38 +010042#define XENVIF_NAPI_WEIGHT 64
Ian Campbellf942dc22011-03-15 00:06:18 +000043
44int xenvif_schedulable(struct xenvif *vif)
45{
46 return netif_running(vif->dev) && netif_carrier_ok(vif->dev);
47}
48
49static int xenvif_rx_schedulable(struct xenvif *vif)
50{
Wei Liu73764192013-08-26 12:59:39 +010051 return xenvif_schedulable(vif) && !xenvif_rx_ring_full(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +000052}
53
Wei Liue1f00a692013-05-22 06:34:45 +000054static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
Ian Campbellf942dc22011-03-15 00:06:18 +000055{
56 struct xenvif *vif = dev_id;
57
Wei Liub3f980b2013-08-26 12:59:38 +010058 if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx))
59 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +000060
Wei Liue1f00a692013-05-22 06:34:45 +000061 return IRQ_HANDLED;
62}
63
Wei Liub3f980b2013-08-26 12:59:38 +010064static int xenvif_poll(struct napi_struct *napi, int budget)
65{
66 struct xenvif *vif = container_of(napi, struct xenvif, napi);
67 int work_done;
68
Wei Liu73764192013-08-26 12:59:39 +010069 work_done = xenvif_tx_action(vif, budget);
Wei Liub3f980b2013-08-26 12:59:38 +010070
71 if (work_done < budget) {
72 int more_to_do = 0;
73 unsigned long flags;
74
75 /* It is necessary to disable IRQ before calling
76 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
77 * lose event from the frontend.
78 *
79 * Consider:
80 * RING_HAS_UNCONSUMED_REQUESTS
81 * <frontend generates event to trigger napi_schedule>
82 * __napi_complete
83 *
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.
89 */
90
91 local_irq_save(flags);
92
93 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
94 if (!more_to_do)
95 __napi_complete(napi);
96
97 local_irq_restore(flags);
98 }
99
100 return work_done;
101}
102
Wei Liue1f00a692013-05-22 06:34:45 +0000103static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
104{
105 struct xenvif *vif = dev_id;
106
Ian Campbellf942dc22011-03-15 00:06:18 +0000107 if (xenvif_rx_schedulable(vif))
108 netif_wake_queue(vif->dev);
109
110 return IRQ_HANDLED;
111}
112
Wei Liue1f00a692013-05-22 06:34:45 +0000113static irqreturn_t xenvif_interrupt(int irq, void *dev_id)
114{
115 xenvif_tx_interrupt(irq, dev_id);
116 xenvif_rx_interrupt(irq, dev_id);
117
118 return IRQ_HANDLED;
119}
120
Ian Campbellf942dc22011-03-15 00:06:18 +0000121static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
122{
123 struct xenvif *vif = netdev_priv(dev);
124
125 BUG_ON(skb->dev != dev);
126
Wei Liub3f980b2013-08-26 12:59:38 +0100127 /* Drop the packet if vif is not ready */
128 if (vif->task == NULL)
Ian Campbellf942dc22011-03-15 00:06:18 +0000129 goto drop;
130
131 /* Drop the packet if the target domain has no receive buffers. */
132 if (!xenvif_rx_schedulable(vif))
133 goto drop;
134
135 /* Reserve ring slots for the worst-case number of fragments. */
Wei Liu73764192013-08-26 12:59:39 +0100136 vif->rx_req_cons_peek += xenvif_count_skb_slots(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000137
Wei Liu73764192013-08-26 12:59:39 +0100138 if (vif->can_queue && xenvif_must_stop_queue(vif))
Ian Campbellf942dc22011-03-15 00:06:18 +0000139 netif_stop_queue(dev);
140
Wei Liu73764192013-08-26 12:59:39 +0100141 xenvif_queue_tx_skb(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000142
143 return NETDEV_TX_OK;
144
145 drop:
146 vif->dev->stats.tx_dropped++;
147 dev_kfree_skb(skb);
148 return NETDEV_TX_OK;
149}
150
Ian Campbellf942dc22011-03-15 00:06:18 +0000151void xenvif_notify_tx_completion(struct xenvif *vif)
152{
153 if (netif_queue_stopped(vif->dev) && xenvif_rx_schedulable(vif))
154 netif_wake_queue(vif->dev);
155}
156
157static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
158{
159 struct xenvif *vif = netdev_priv(dev);
160 return &vif->dev->stats;
161}
162
163static void xenvif_up(struct xenvif *vif)
164{
Wei Liub3f980b2013-08-26 12:59:38 +0100165 napi_enable(&vif->napi);
Wei Liue1f00a692013-05-22 06:34:45 +0000166 enable_irq(vif->tx_irq);
167 if (vif->tx_irq != vif->rx_irq)
168 enable_irq(vif->rx_irq);
Wei Liu73764192013-08-26 12:59:39 +0100169 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000170}
171
172static void xenvif_down(struct xenvif *vif)
173{
Wei Liub3f980b2013-08-26 12:59:38 +0100174 napi_disable(&vif->napi);
Wei Liue1f00a692013-05-22 06:34:45 +0000175 disable_irq(vif->tx_irq);
176 if (vif->tx_irq != vif->rx_irq)
177 disable_irq(vif->rx_irq);
David Vrabel3e55f8b2013-02-14 03:18:58 +0000178 del_timer_sync(&vif->credit_timeout);
Ian Campbellf942dc22011-03-15 00:06:18 +0000179}
180
181static int xenvif_open(struct net_device *dev)
182{
183 struct xenvif *vif = netdev_priv(dev);
184 if (netif_carrier_ok(dev))
185 xenvif_up(vif);
186 netif_start_queue(dev);
187 return 0;
188}
189
190static int xenvif_close(struct net_device *dev)
191{
192 struct xenvif *vif = netdev_priv(dev);
193 if (netif_carrier_ok(dev))
194 xenvif_down(vif);
195 netif_stop_queue(dev);
196 return 0;
197}
198
199static int xenvif_change_mtu(struct net_device *dev, int mtu)
200{
201 struct xenvif *vif = netdev_priv(dev);
202 int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
203
204 if (mtu > max)
205 return -EINVAL;
206 dev->mtu = mtu;
207 return 0;
208}
209
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000210static netdev_features_t xenvif_fix_features(struct net_device *dev,
211 netdev_features_t features)
Ian Campbellf942dc22011-03-15 00:06:18 +0000212{
213 struct xenvif *vif = netdev_priv(dev);
Ian Campbellf942dc22011-03-15 00:06:18 +0000214
Michał Mirosław47103042011-04-19 03:35:06 +0000215 if (!vif->can_sg)
216 features &= ~NETIF_F_SG;
217 if (!vif->gso && !vif->gso_prefix)
218 features &= ~NETIF_F_TSO;
219 if (!vif->csum)
220 features &= ~NETIF_F_IP_CSUM;
Ian Campbellf942dc22011-03-15 00:06:18 +0000221
Michał Mirosław47103042011-04-19 03:35:06 +0000222 return features;
Ian Campbellf942dc22011-03-15 00:06:18 +0000223}
224
225static const struct xenvif_stat {
226 char name[ETH_GSTRING_LEN];
227 u16 offset;
228} xenvif_stats[] = {
229 {
230 "rx_gso_checksum_fixup",
231 offsetof(struct xenvif, rx_gso_checksum_fixup)
232 },
233};
234
235static int xenvif_get_sset_count(struct net_device *dev, int string_set)
236{
237 switch (string_set) {
238 case ETH_SS_STATS:
239 return ARRAY_SIZE(xenvif_stats);
240 default:
241 return -EINVAL;
242 }
243}
244
245static void xenvif_get_ethtool_stats(struct net_device *dev,
246 struct ethtool_stats *stats, u64 * data)
247{
248 void *vif = netdev_priv(dev);
249 int i;
250
251 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
252 data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset);
253}
254
255static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
256{
257 int i;
258
259 switch (stringset) {
260 case ETH_SS_STATS:
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);
264 break;
265 }
266}
267
stephen hemminger813abbb2012-01-04 11:56:58 +0000268static const struct ethtool_ops xenvif_ethtool_ops = {
Ian Campbellf942dc22011-03-15 00:06:18 +0000269 .get_link = ethtool_op_get_link,
270
271 .get_sset_count = xenvif_get_sset_count,
272 .get_ethtool_stats = xenvif_get_ethtool_stats,
273 .get_strings = xenvif_get_strings,
274};
275
stephen hemminger813abbb2012-01-04 11:56:58 +0000276static const struct net_device_ops xenvif_netdev_ops = {
Ian Campbellf942dc22011-03-15 00:06:18 +0000277 .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,
Michał Mirosław47103042011-04-19 03:35:06 +0000282 .ndo_fix_features = xenvif_fix_features,
Matt Wilson4a633a62013-01-22 08:08:25 +0000283 .ndo_set_mac_address = eth_mac_addr,
284 .ndo_validate_addr = eth_validate_addr,
Ian Campbellf942dc22011-03-15 00:06:18 +0000285};
286
287struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
288 unsigned int handle)
289{
290 int err;
291 struct net_device *dev;
292 struct xenvif *vif;
293 char name[IFNAMSIZ] = {};
Wei Liub3f980b2013-08-26 12:59:38 +0100294 int i;
Ian Campbellf942dc22011-03-15 00:06:18 +0000295
296 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
297 dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup);
298 if (dev == NULL) {
Wei Liub3f980b2013-08-26 12:59:38 +0100299 pr_warn("Could not allocate netdev for %s\n", name);
Ian Campbellf942dc22011-03-15 00:06:18 +0000300 return ERR_PTR(-ENOMEM);
301 }
302
303 SET_NETDEV_DEV(dev, parent);
304
305 vif = netdev_priv(dev);
306 vif->domid = domid;
307 vif->handle = handle;
Ian Campbellf942dc22011-03-15 00:06:18 +0000308 vif->can_sg = 1;
309 vif->csum = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000310 vif->dev = dev;
Ian Campbellf942dc22011-03-15 00:06:18 +0000311
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;
317
318 dev->netdev_ops = &xenvif_netdev_ops;
Michał Mirosław47103042011-04-19 03:35:06 +0000319 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
320 dev->features = dev->hw_features;
Ian Campbellf942dc22011-03-15 00:06:18 +0000321 SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops);
322
323 dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
324
Wei Liub3f980b2013-08-26 12:59:38 +0100325 skb_queue_head_init(&vif->rx_queue);
326 skb_queue_head_init(&vif->tx_queue);
327
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;
334
Ian Campbellf942dc22011-03-15 00:06:18 +0000335 /*
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)
340 */
341 memset(dev->dev_addr, 0xFF, ETH_ALEN);
342 dev->dev_addr[0] &= ~0x01;
343
Wei Liub3f980b2013-08-26 12:59:38 +0100344 netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT);
345
Ian Campbellf942dc22011-03-15 00:06:18 +0000346 netif_carrier_off(dev);
347
348 err = register_netdev(dev);
349 if (err) {
350 netdev_warn(dev, "Could not register device: err=%d\n", err);
351 free_netdev(dev);
352 return ERR_PTR(err);
353 }
354
355 netdev_dbg(dev, "Successfully created xenvif\n");
356 return vif;
357}
358
359int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
Wei Liue1f00a692013-05-22 06:34:45 +0000360 unsigned long rx_ring_ref, unsigned int tx_evtchn,
361 unsigned int rx_evtchn)
Ian Campbellf942dc22011-03-15 00:06:18 +0000362{
363 int err = -ENOMEM;
364
365 /* Already connected through? */
Wei Liue1f00a692013-05-22 06:34:45 +0000366 if (vif->tx_irq)
Ian Campbellf942dc22011-03-15 00:06:18 +0000367 return 0;
368
Wei Liub103f352013-05-16 23:26:11 +0000369 __module_get(THIS_MODULE);
370
Wei Liu73764192013-08-26 12:59:39 +0100371 err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref);
Ian Campbellf942dc22011-03-15 00:06:18 +0000372 if (err < 0)
373 goto err;
374
Wei Liue1f00a692013-05-22 06:34:45 +0000375 if (tx_evtchn == rx_evtchn) {
376 /* feature-split-event-channels == 0 */
377 err = bind_interdomain_evtchn_to_irqhandler(
378 vif->domid, tx_evtchn, xenvif_interrupt, 0,
379 vif->dev->name, vif);
380 if (err < 0)
381 goto err_unmap;
382 vif->tx_irq = vif->rx_irq = err;
383 disable_irq(vif->tx_irq);
384 } else {
385 /* feature-split-event-channels == 1 */
386 snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name),
387 "%s-tx", vif->dev->name);
388 err = bind_interdomain_evtchn_to_irqhandler(
389 vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
390 vif->tx_irq_name, vif);
391 if (err < 0)
392 goto err_unmap;
393 vif->tx_irq = err;
394 disable_irq(vif->tx_irq);
395
396 snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name),
397 "%s-rx", vif->dev->name);
398 err = bind_interdomain_evtchn_to_irqhandler(
399 vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
400 vif->rx_irq_name, vif);
401 if (err < 0)
402 goto err_tx_unbind;
403 vif->rx_irq = err;
404 disable_irq(vif->rx_irq);
405 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000406
Wei Liub3f980b2013-08-26 12:59:38 +0100407 init_waitqueue_head(&vif->wq);
Wei Liu73764192013-08-26 12:59:39 +0100408 vif->task = kthread_create(xenvif_kthread,
Wei Liub3f980b2013-08-26 12:59:38 +0100409 (void *)vif, vif->dev->name);
410 if (IS_ERR(vif->task)) {
411 pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
412 err = PTR_ERR(vif->task);
413 goto err_rx_unbind;
414 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000415
416 rtnl_lock();
Michał Mirosław47103042011-04-19 03:35:06 +0000417 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
418 dev_set_mtu(vif->dev, ETH_DATA_LEN);
419 netdev_update_features(vif->dev);
420 netif_carrier_on(vif->dev);
David Vrabeld0e5d832011-09-30 06:37:51 +0000421 if (netif_running(vif->dev))
422 xenvif_up(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000423 rtnl_unlock();
424
Wei Liub3f980b2013-08-26 12:59:38 +0100425 wake_up_process(vif->task);
426
Ian Campbellf942dc22011-03-15 00:06:18 +0000427 return 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100428
429err_rx_unbind:
430 unbind_from_irqhandler(vif->rx_irq, vif);
431 vif->rx_irq = 0;
Wei Liue1f00a692013-05-22 06:34:45 +0000432err_tx_unbind:
433 unbind_from_irqhandler(vif->tx_irq, vif);
434 vif->tx_irq = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000435err_unmap:
Wei Liu73764192013-08-26 12:59:39 +0100436 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000437err:
Wei Liub103f352013-05-16 23:26:11 +0000438 module_put(THIS_MODULE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000439 return err;
440}
441
Ian Campbell488562862013-02-06 23:41:35 +0000442void xenvif_carrier_off(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000443{
444 struct net_device *dev = vif->dev;
Ian Campbell488562862013-02-06 23:41:35 +0000445
446 rtnl_lock();
447 netif_carrier_off(dev); /* discard queued packets */
448 if (netif_running(dev))
449 xenvif_down(vif);
450 rtnl_unlock();
Ian Campbell488562862013-02-06 23:41:35 +0000451}
452
453void xenvif_disconnect(struct xenvif *vif)
454{
Wei Liub103f352013-05-16 23:26:11 +0000455 /* Disconnect funtion might get called by generic framework
456 * even before vif connects, so we need to check if we really
457 * need to do a module_put.
458 */
459 int need_module_put = 0;
460
Ian Campbell488562862013-02-06 23:41:35 +0000461 if (netif_carrier_ok(vif->dev))
462 xenvif_carrier_off(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000463
Wei Liue1f00a692013-05-22 06:34:45 +0000464 if (vif->tx_irq) {
465 if (vif->tx_irq == vif->rx_irq)
466 unbind_from_irqhandler(vif->tx_irq, vif);
467 else {
468 unbind_from_irqhandler(vif->tx_irq, vif);
469 unbind_from_irqhandler(vif->rx_irq, vif);
470 }
Wei Liub103f352013-05-16 23:26:11 +0000471 /* vif->irq is valid, we had a module_get in
472 * xenvif_connect.
473 */
474 need_module_put = 1;
475 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000476
Wei Liub3f980b2013-08-26 12:59:38 +0100477 if (vif->task)
478 kthread_stop(vif->task);
479
480 netif_napi_del(&vif->napi);
481
Ian Campbellf942dc22011-03-15 00:06:18 +0000482 unregister_netdev(vif->dev);
483
Wei Liu73764192013-08-26 12:59:39 +0100484 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000485
486 free_netdev(vif->dev);
Wei Liub103f352013-05-16 23:26:11 +0000487
488 if (need_module_put)
489 module_put(THIS_MODULE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000490}