Commit | Line | Data |
---|---|---|
f942dc25 IC |
1 | /* |
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 | ||
b3f980bd | 33 | #include <linux/kthread.h> |
f942dc25 IC |
34 | #include <linux/ethtool.h> |
35 | #include <linux/rtnetlink.h> | |
36 | #include <linux/if_vlan.h> | |
f35f76ee | 37 | #include <linux/vmalloc.h> |
f942dc25 IC |
38 | |
39 | #include <xen/events.h> | |
40 | #include <asm/xen/hypercall.h> | |
41 | ||
42 | #define XENVIF_QUEUE_LENGTH 32 | |
b3f980bd | 43 | #define XENVIF_NAPI_WEIGHT 64 |
f942dc25 IC |
44 | |
45 | int xenvif_schedulable(struct xenvif *vif) | |
46 | { | |
47 | return netif_running(vif->dev) && netif_carrier_ok(vif->dev); | |
48 | } | |
49 | ||
e1f00a69 | 50 | static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id) |
f942dc25 IC |
51 | { |
52 | struct xenvif *vif = dev_id; | |
53 | ||
b3f980bd WL |
54 | if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) |
55 | napi_schedule(&vif->napi); | |
f942dc25 | 56 | |
e1f00a69 WL |
57 | return IRQ_HANDLED; |
58 | } | |
59 | ||
b3f980bd WL |
60 | static int xenvif_poll(struct napi_struct *napi, int budget) |
61 | { | |
62 | struct xenvif *vif = container_of(napi, struct xenvif, napi); | |
63 | int work_done; | |
64 | ||
7376419a | 65 | work_done = xenvif_tx_action(vif, budget); |
b3f980bd WL |
66 | |
67 | if (work_done < budget) { | |
68 | int more_to_do = 0; | |
69 | unsigned long flags; | |
70 | ||
71 | /* It is necessary to disable IRQ before calling | |
72 | * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might | |
73 | * lose event from the frontend. | |
74 | * | |
75 | * Consider: | |
76 | * RING_HAS_UNCONSUMED_REQUESTS | |
77 | * <frontend generates event to trigger napi_schedule> | |
78 | * __napi_complete | |
79 | * | |
80 | * This handler is still in scheduled state so the | |
81 | * event has no effect at all. After __napi_complete | |
82 | * this handler is descheduled and cannot get | |
83 | * scheduled again. We lose event in this case and the ring | |
84 | * will be completely stalled. | |
85 | */ | |
86 | ||
87 | local_irq_save(flags); | |
88 | ||
89 | RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do); | |
90 | if (!more_to_do) | |
91 | __napi_complete(napi); | |
92 | ||
93 | local_irq_restore(flags); | |
94 | } | |
95 | ||
96 | return work_done; | |
97 | } | |
98 | ||
e1f00a69 WL |
99 | static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id) |
100 | { | |
101 | struct xenvif *vif = dev_id; | |
102 | ||
ca2f09f2 | 103 | xenvif_kick_thread(vif); |
f942dc25 IC |
104 | |
105 | return IRQ_HANDLED; | |
106 | } | |
107 | ||
e1f00a69 WL |
108 | static irqreturn_t xenvif_interrupt(int irq, void *dev_id) |
109 | { | |
110 | xenvif_tx_interrupt(irq, dev_id); | |
111 | xenvif_rx_interrupt(irq, dev_id); | |
112 | ||
113 | return IRQ_HANDLED; | |
114 | } | |
115 | ||
f942dc25 IC |
116 | static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev) |
117 | { | |
118 | struct xenvif *vif = netdev_priv(dev); | |
ca2f09f2 | 119 | int min_slots_needed; |
f942dc25 IC |
120 | |
121 | BUG_ON(skb->dev != dev); | |
122 | ||
b3f980bd | 123 | /* Drop the packet if vif is not ready */ |
ca2f09f2 | 124 | if (vif->task == NULL || !xenvif_schedulable(vif)) |
f942dc25 IC |
125 | goto drop; |
126 | ||
ca2f09f2 PD |
127 | /* At best we'll need one slot for the header and one for each |
128 | * frag. | |
129 | */ | |
130 | min_slots_needed = 1 + skb_shinfo(skb)->nr_frags; | |
f942dc25 | 131 | |
ca2f09f2 PD |
132 | /* If the skb is GSO then we'll also need an extra slot for the |
133 | * metadata. | |
134 | */ | |
836fbaf4 | 135 | if (skb_is_gso(skb)) |
ca2f09f2 | 136 | min_slots_needed++; |
f942dc25 | 137 | |
ca2f09f2 PD |
138 | /* If the skb can't possibly fit in the remaining slots |
139 | * then turn off the queue to give the ring a chance to | |
140 | * drain. | |
141 | */ | |
142 | if (!xenvif_rx_ring_slots_available(vif, min_slots_needed)) | |
143 | xenvif_stop_queue(vif); | |
f942dc25 | 144 | |
ca2f09f2 PD |
145 | skb_queue_tail(&vif->rx_queue, skb); |
146 | xenvif_kick_thread(vif); | |
f942dc25 IC |
147 | |
148 | return NETDEV_TX_OK; | |
149 | ||
150 | drop: | |
151 | vif->dev->stats.tx_dropped++; | |
152 | dev_kfree_skb(skb); | |
153 | return NETDEV_TX_OK; | |
154 | } | |
155 | ||
f942dc25 IC |
156 | static struct net_device_stats *xenvif_get_stats(struct net_device *dev) |
157 | { | |
158 | struct xenvif *vif = netdev_priv(dev); | |
159 | return &vif->dev->stats; | |
160 | } | |
161 | ||
162 | static void xenvif_up(struct xenvif *vif) | |
163 | { | |
b3f980bd | 164 | napi_enable(&vif->napi); |
e1f00a69 WL |
165 | enable_irq(vif->tx_irq); |
166 | if (vif->tx_irq != vif->rx_irq) | |
167 | enable_irq(vif->rx_irq); | |
7376419a | 168 | xenvif_check_rx_xenvif(vif); |
f942dc25 IC |
169 | } |
170 | ||
171 | static void xenvif_down(struct xenvif *vif) | |
172 | { | |
b3f980bd | 173 | napi_disable(&vif->napi); |
e1f00a69 WL |
174 | disable_irq(vif->tx_irq); |
175 | if (vif->tx_irq != vif->rx_irq) | |
176 | disable_irq(vif->rx_irq); | |
3e55f8b3 | 177 | del_timer_sync(&vif->credit_timeout); |
f942dc25 IC |
178 | } |
179 | ||
180 | static int xenvif_open(struct net_device *dev) | |
181 | { | |
182 | struct xenvif *vif = netdev_priv(dev); | |
183 | if (netif_carrier_ok(dev)) | |
184 | xenvif_up(vif); | |
185 | netif_start_queue(dev); | |
186 | return 0; | |
187 | } | |
188 | ||
189 | static int xenvif_close(struct net_device *dev) | |
190 | { | |
191 | struct xenvif *vif = netdev_priv(dev); | |
192 | if (netif_carrier_ok(dev)) | |
193 | xenvif_down(vif); | |
194 | netif_stop_queue(dev); | |
195 | return 0; | |
196 | } | |
197 | ||
198 | static int xenvif_change_mtu(struct net_device *dev, int mtu) | |
199 | { | |
200 | struct xenvif *vif = netdev_priv(dev); | |
201 | int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN; | |
202 | ||
203 | if (mtu > max) | |
204 | return -EINVAL; | |
205 | dev->mtu = mtu; | |
206 | return 0; | |
207 | } | |
208 | ||
c8f44aff MM |
209 | static netdev_features_t xenvif_fix_features(struct net_device *dev, |
210 | netdev_features_t features) | |
f942dc25 IC |
211 | { |
212 | struct xenvif *vif = netdev_priv(dev); | |
f942dc25 | 213 | |
47103041 MM |
214 | if (!vif->can_sg) |
215 | features &= ~NETIF_F_SG; | |
82cada22 | 216 | if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4)) |
47103041 | 217 | features &= ~NETIF_F_TSO; |
82cada22 PD |
218 | if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6)) |
219 | features &= ~NETIF_F_TSO6; | |
146c8a77 | 220 | if (!vif->ip_csum) |
47103041 | 221 | features &= ~NETIF_F_IP_CSUM; |
146c8a77 PD |
222 | if (!vif->ipv6_csum) |
223 | features &= ~NETIF_F_IPV6_CSUM; | |
f942dc25 | 224 | |
47103041 | 225 | return features; |
f942dc25 IC |
226 | } |
227 | ||
228 | static const struct xenvif_stat { | |
229 | char name[ETH_GSTRING_LEN]; | |
230 | u16 offset; | |
231 | } xenvif_stats[] = { | |
232 | { | |
233 | "rx_gso_checksum_fixup", | |
234 | offsetof(struct xenvif, rx_gso_checksum_fixup) | |
235 | }, | |
236 | }; | |
237 | ||
238 | static int xenvif_get_sset_count(struct net_device *dev, int string_set) | |
239 | { | |
240 | switch (string_set) { | |
241 | case ETH_SS_STATS: | |
242 | return ARRAY_SIZE(xenvif_stats); | |
243 | default: | |
244 | return -EINVAL; | |
245 | } | |
246 | } | |
247 | ||
248 | static void xenvif_get_ethtool_stats(struct net_device *dev, | |
249 | struct ethtool_stats *stats, u64 * data) | |
250 | { | |
251 | void *vif = netdev_priv(dev); | |
252 | int i; | |
253 | ||
254 | for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) | |
255 | data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset); | |
256 | } | |
257 | ||
258 | static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data) | |
259 | { | |
260 | int i; | |
261 | ||
262 | switch (stringset) { | |
263 | case ETH_SS_STATS: | |
264 | for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) | |
265 | memcpy(data + i * ETH_GSTRING_LEN, | |
266 | xenvif_stats[i].name, ETH_GSTRING_LEN); | |
267 | break; | |
268 | } | |
269 | } | |
270 | ||
813abbba | 271 | static const struct ethtool_ops xenvif_ethtool_ops = { |
f942dc25 IC |
272 | .get_link = ethtool_op_get_link, |
273 | ||
274 | .get_sset_count = xenvif_get_sset_count, | |
275 | .get_ethtool_stats = xenvif_get_ethtool_stats, | |
276 | .get_strings = xenvif_get_strings, | |
277 | }; | |
278 | ||
813abbba | 279 | static const struct net_device_ops xenvif_netdev_ops = { |
f942dc25 IC |
280 | .ndo_start_xmit = xenvif_start_xmit, |
281 | .ndo_get_stats = xenvif_get_stats, | |
282 | .ndo_open = xenvif_open, | |
283 | .ndo_stop = xenvif_close, | |
284 | .ndo_change_mtu = xenvif_change_mtu, | |
47103041 | 285 | .ndo_fix_features = xenvif_fix_features, |
4a633a60 MW |
286 | .ndo_set_mac_address = eth_mac_addr, |
287 | .ndo_validate_addr = eth_validate_addr, | |
f942dc25 IC |
288 | }; |
289 | ||
290 | struct xenvif *xenvif_alloc(struct device *parent, domid_t domid, | |
291 | unsigned int handle) | |
292 | { | |
293 | int err; | |
294 | struct net_device *dev; | |
295 | struct xenvif *vif; | |
296 | char name[IFNAMSIZ] = {}; | |
b3f980bd | 297 | int i; |
f942dc25 IC |
298 | |
299 | snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle); | |
300 | dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup); | |
301 | if (dev == NULL) { | |
b3f980bd | 302 | pr_warn("Could not allocate netdev for %s\n", name); |
f942dc25 IC |
303 | return ERR_PTR(-ENOMEM); |
304 | } | |
305 | ||
306 | SET_NETDEV_DEV(dev, parent); | |
307 | ||
308 | vif = netdev_priv(dev); | |
ac3d5ac2 PD |
309 | |
310 | vif->grant_copy_op = vmalloc(sizeof(struct gnttab_copy) * | |
311 | MAX_GRANT_COPY_OPS); | |
312 | if (vif->grant_copy_op == NULL) { | |
313 | pr_warn("Could not allocate grant copy space for %s\n", name); | |
314 | free_netdev(dev); | |
315 | return ERR_PTR(-ENOMEM); | |
316 | } | |
317 | ||
f942dc25 IC |
318 | vif->domid = domid; |
319 | vif->handle = handle; | |
f942dc25 | 320 | vif->can_sg = 1; |
146c8a77 | 321 | vif->ip_csum = 1; |
f942dc25 | 322 | vif->dev = dev; |
f942dc25 IC |
323 | |
324 | vif->credit_bytes = vif->remaining_credit = ~0UL; | |
325 | vif->credit_usec = 0UL; | |
326 | init_timer(&vif->credit_timeout); | |
059dfa6a | 327 | vif->credit_window_start = get_jiffies_64(); |
f942dc25 IC |
328 | |
329 | dev->netdev_ops = &xenvif_netdev_ops; | |
146c8a77 PD |
330 | dev->hw_features = NETIF_F_SG | |
331 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | | |
82cada22 | 332 | NETIF_F_TSO | NETIF_F_TSO6; |
7365bcfa | 333 | dev->features = dev->hw_features | NETIF_F_RXCSUM; |
f942dc25 IC |
334 | SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops); |
335 | ||
336 | dev->tx_queue_len = XENVIF_QUEUE_LENGTH; | |
337 | ||
b3f980bd WL |
338 | skb_queue_head_init(&vif->rx_queue); |
339 | skb_queue_head_init(&vif->tx_queue); | |
340 | ||
341 | vif->pending_cons = 0; | |
342 | vif->pending_prod = MAX_PENDING_REQS; | |
343 | for (i = 0; i < MAX_PENDING_REQS; i++) | |
344 | vif->pending_ring[i] = i; | |
345 | for (i = 0; i < MAX_PENDING_REQS; i++) | |
346 | vif->mmap_pages[i] = NULL; | |
347 | ||
f942dc25 IC |
348 | /* |
349 | * Initialise a dummy MAC address. We choose the numerically | |
350 | * largest non-broadcast address to prevent the address getting | |
351 | * stolen by an Ethernet bridge for STP purposes. | |
352 | * (FE:FF:FF:FF:FF:FF) | |
353 | */ | |
354 | memset(dev->dev_addr, 0xFF, ETH_ALEN); | |
355 | dev->dev_addr[0] &= ~0x01; | |
356 | ||
b3f980bd WL |
357 | netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT); |
358 | ||
f942dc25 IC |
359 | netif_carrier_off(dev); |
360 | ||
361 | err = register_netdev(dev); | |
362 | if (err) { | |
363 | netdev_warn(dev, "Could not register device: err=%d\n", err); | |
364 | free_netdev(dev); | |
365 | return ERR_PTR(err); | |
366 | } | |
367 | ||
368 | netdev_dbg(dev, "Successfully created xenvif\n"); | |
279f438e PD |
369 | |
370 | __module_get(THIS_MODULE); | |
371 | ||
f942dc25 IC |
372 | return vif; |
373 | } | |
374 | ||
375 | int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref, | |
e1f00a69 WL |
376 | unsigned long rx_ring_ref, unsigned int tx_evtchn, |
377 | unsigned int rx_evtchn) | |
f942dc25 | 378 | { |
67fa3660 | 379 | struct task_struct *task; |
f942dc25 IC |
380 | int err = -ENOMEM; |
381 | ||
67fa3660 PD |
382 | BUG_ON(vif->tx_irq); |
383 | BUG_ON(vif->task); | |
f942dc25 | 384 | |
7376419a | 385 | err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref); |
f942dc25 IC |
386 | if (err < 0) |
387 | goto err; | |
388 | ||
ca2f09f2 PD |
389 | init_waitqueue_head(&vif->wq); |
390 | ||
e1f00a69 WL |
391 | if (tx_evtchn == rx_evtchn) { |
392 | /* feature-split-event-channels == 0 */ | |
393 | err = bind_interdomain_evtchn_to_irqhandler( | |
394 | vif->domid, tx_evtchn, xenvif_interrupt, 0, | |
395 | vif->dev->name, vif); | |
396 | if (err < 0) | |
397 | goto err_unmap; | |
398 | vif->tx_irq = vif->rx_irq = err; | |
399 | disable_irq(vif->tx_irq); | |
400 | } else { | |
401 | /* feature-split-event-channels == 1 */ | |
402 | snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name), | |
403 | "%s-tx", vif->dev->name); | |
404 | err = bind_interdomain_evtchn_to_irqhandler( | |
405 | vif->domid, tx_evtchn, xenvif_tx_interrupt, 0, | |
406 | vif->tx_irq_name, vif); | |
407 | if (err < 0) | |
408 | goto err_unmap; | |
409 | vif->tx_irq = err; | |
410 | disable_irq(vif->tx_irq); | |
411 | ||
412 | snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name), | |
413 | "%s-rx", vif->dev->name); | |
414 | err = bind_interdomain_evtchn_to_irqhandler( | |
415 | vif->domid, rx_evtchn, xenvif_rx_interrupt, 0, | |
416 | vif->rx_irq_name, vif); | |
417 | if (err < 0) | |
418 | goto err_tx_unbind; | |
419 | vif->rx_irq = err; | |
420 | disable_irq(vif->rx_irq); | |
421 | } | |
f942dc25 | 422 | |
67fa3660 PD |
423 | task = kthread_create(xenvif_kthread, |
424 | (void *)vif, "%s", vif->dev->name); | |
425 | if (IS_ERR(task)) { | |
b3f980bd | 426 | pr_warn("Could not allocate kthread for %s\n", vif->dev->name); |
67fa3660 | 427 | err = PTR_ERR(task); |
b3f980bd WL |
428 | goto err_rx_unbind; |
429 | } | |
f942dc25 | 430 | |
67fa3660 PD |
431 | vif->task = task; |
432 | ||
f942dc25 | 433 | rtnl_lock(); |
47103041 MM |
434 | if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN) |
435 | dev_set_mtu(vif->dev, ETH_DATA_LEN); | |
436 | netdev_update_features(vif->dev); | |
437 | netif_carrier_on(vif->dev); | |
d0e5d832 DV |
438 | if (netif_running(vif->dev)) |
439 | xenvif_up(vif); | |
f942dc25 IC |
440 | rtnl_unlock(); |
441 | ||
b3f980bd WL |
442 | wake_up_process(vif->task); |
443 | ||
f942dc25 | 444 | return 0; |
b3f980bd WL |
445 | |
446 | err_rx_unbind: | |
447 | unbind_from_irqhandler(vif->rx_irq, vif); | |
448 | vif->rx_irq = 0; | |
e1f00a69 WL |
449 | err_tx_unbind: |
450 | unbind_from_irqhandler(vif->tx_irq, vif); | |
451 | vif->tx_irq = 0; | |
f942dc25 | 452 | err_unmap: |
7376419a | 453 | xenvif_unmap_frontend_rings(vif); |
f942dc25 | 454 | err: |
b103f358 | 455 | module_put(THIS_MODULE); |
f942dc25 IC |
456 | return err; |
457 | } | |
458 | ||
48856286 | 459 | void xenvif_carrier_off(struct xenvif *vif) |
f942dc25 IC |
460 | { |
461 | struct net_device *dev = vif->dev; | |
48856286 IC |
462 | |
463 | rtnl_lock(); | |
464 | netif_carrier_off(dev); /* discard queued packets */ | |
465 | if (netif_running(dev)) | |
466 | xenvif_down(vif); | |
467 | rtnl_unlock(); | |
48856286 IC |
468 | } |
469 | ||
470 | void xenvif_disconnect(struct xenvif *vif) | |
471 | { | |
472 | if (netif_carrier_ok(vif->dev)) | |
473 | xenvif_carrier_off(vif); | |
f942dc25 | 474 | |
67fa3660 | 475 | if (vif->task) { |
db739ef3 | 476 | kthread_stop(vif->task); |
67fa3660 PD |
477 | vif->task = NULL; |
478 | } | |
db739ef3 | 479 | |
e1f00a69 WL |
480 | if (vif->tx_irq) { |
481 | if (vif->tx_irq == vif->rx_irq) | |
482 | unbind_from_irqhandler(vif->tx_irq, vif); | |
483 | else { | |
484 | unbind_from_irqhandler(vif->tx_irq, vif); | |
485 | unbind_from_irqhandler(vif->rx_irq, vif); | |
486 | } | |
279f438e | 487 | vif->tx_irq = 0; |
b103f358 | 488 | } |
f942dc25 | 489 | |
279f438e PD |
490 | xenvif_unmap_frontend_rings(vif); |
491 | } | |
492 | ||
493 | void xenvif_free(struct xenvif *vif) | |
494 | { | |
b3f980bd WL |
495 | netif_napi_del(&vif->napi); |
496 | ||
f942dc25 IC |
497 | unregister_netdev(vif->dev); |
498 | ||
ac3d5ac2 | 499 | vfree(vif->grant_copy_op); |
f942dc25 | 500 | free_netdev(vif->dev); |
b103f358 | 501 | |
279f438e | 502 | module_put(THIS_MODULE); |
f942dc25 | 503 | } |