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> | |
37 | ||
38 | #include <xen/events.h> | |
39 | #include <asm/xen/hypercall.h> | |
40 | ||
41 | #define XENVIF_QUEUE_LENGTH 32 | |
b3f980bd | 42 | #define XENVIF_NAPI_WEIGHT 64 |
f942dc25 IC |
43 | |
44 | int xenvif_schedulable(struct xenvif *vif) | |
45 | { | |
46 | return netif_running(vif->dev) && netif_carrier_ok(vif->dev); | |
47 | } | |
48 | ||
49 | static int xenvif_rx_schedulable(struct xenvif *vif) | |
50 | { | |
7376419a | 51 | return xenvif_schedulable(vif) && !xenvif_rx_ring_full(vif); |
f942dc25 IC |
52 | } |
53 | ||
e1f00a69 | 54 | static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id) |
f942dc25 IC |
55 | { |
56 | struct xenvif *vif = dev_id; | |
57 | ||
b3f980bd WL |
58 | if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) |
59 | napi_schedule(&vif->napi); | |
f942dc25 | 60 | |
e1f00a69 WL |
61 | return IRQ_HANDLED; |
62 | } | |
63 | ||
b3f980bd WL |
64 | static 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 | ||
7376419a | 69 | work_done = xenvif_tx_action(vif, budget); |
b3f980bd WL |
70 | |
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 | ||
e1f00a69 WL |
103 | static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id) |
104 | { | |
105 | struct xenvif *vif = dev_id; | |
106 | ||
f942dc25 IC |
107 | if (xenvif_rx_schedulable(vif)) |
108 | netif_wake_queue(vif->dev); | |
109 | ||
110 | return IRQ_HANDLED; | |
111 | } | |
112 | ||
e1f00a69 WL |
113 | static 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 | ||
f942dc25 IC |
121 | static 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 | ||
b3f980bd WL |
127 | /* Drop the packet if vif is not ready */ |
128 | if (vif->task == NULL) | |
f942dc25 IC |
129 | 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. */ | |
7376419a | 136 | vif->rx_req_cons_peek += xenvif_count_skb_slots(vif, skb); |
f942dc25 | 137 | |
7376419a | 138 | if (vif->can_queue && xenvif_must_stop_queue(vif)) |
f942dc25 IC |
139 | netif_stop_queue(dev); |
140 | ||
7376419a | 141 | xenvif_queue_tx_skb(vif, skb); |
f942dc25 IC |
142 | |
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 | ||
f942dc25 IC |
151 | void 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 | ||
157 | static 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 | ||
163 | static void xenvif_up(struct xenvif *vif) | |
164 | { | |
b3f980bd | 165 | napi_enable(&vif->napi); |
e1f00a69 WL |
166 | enable_irq(vif->tx_irq); |
167 | if (vif->tx_irq != vif->rx_irq) | |
168 | enable_irq(vif->rx_irq); | |
7376419a | 169 | xenvif_check_rx_xenvif(vif); |
f942dc25 IC |
170 | } |
171 | ||
172 | static void xenvif_down(struct xenvif *vif) | |
173 | { | |
b3f980bd | 174 | napi_disable(&vif->napi); |
e1f00a69 WL |
175 | disable_irq(vif->tx_irq); |
176 | if (vif->tx_irq != vif->rx_irq) | |
177 | disable_irq(vif->rx_irq); | |
3e55f8b3 | 178 | del_timer_sync(&vif->credit_timeout); |
f942dc25 IC |
179 | } |
180 | ||
181 | static 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 | ||
190 | static 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 | ||
199 | static 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 | ||
c8f44aff MM |
210 | static netdev_features_t xenvif_fix_features(struct net_device *dev, |
211 | netdev_features_t features) | |
f942dc25 IC |
212 | { |
213 | struct xenvif *vif = netdev_priv(dev); | |
f942dc25 | 214 | |
47103041 MM |
215 | if (!vif->can_sg) |
216 | features &= ~NETIF_F_SG; | |
217 | if (!vif->gso && !vif->gso_prefix) | |
218 | features &= ~NETIF_F_TSO; | |
146c8a77 | 219 | if (!vif->ip_csum) |
47103041 | 220 | features &= ~NETIF_F_IP_CSUM; |
146c8a77 PD |
221 | if (!vif->ipv6_csum) |
222 | features &= ~NETIF_F_IPV6_CSUM; | |
f942dc25 | 223 | |
47103041 | 224 | return features; |
f942dc25 IC |
225 | } |
226 | ||
227 | static const struct xenvif_stat { | |
228 | char name[ETH_GSTRING_LEN]; | |
229 | u16 offset; | |
230 | } xenvif_stats[] = { | |
231 | { | |
232 | "rx_gso_checksum_fixup", | |
233 | offsetof(struct xenvif, rx_gso_checksum_fixup) | |
234 | }, | |
235 | }; | |
236 | ||
237 | static int xenvif_get_sset_count(struct net_device *dev, int string_set) | |
238 | { | |
239 | switch (string_set) { | |
240 | case ETH_SS_STATS: | |
241 | return ARRAY_SIZE(xenvif_stats); | |
242 | default: | |
243 | return -EINVAL; | |
244 | } | |
245 | } | |
246 | ||
247 | static void xenvif_get_ethtool_stats(struct net_device *dev, | |
248 | struct ethtool_stats *stats, u64 * data) | |
249 | { | |
250 | void *vif = netdev_priv(dev); | |
251 | int i; | |
252 | ||
253 | for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) | |
254 | data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset); | |
255 | } | |
256 | ||
257 | static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data) | |
258 | { | |
259 | int i; | |
260 | ||
261 | switch (stringset) { | |
262 | case ETH_SS_STATS: | |
263 | for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) | |
264 | memcpy(data + i * ETH_GSTRING_LEN, | |
265 | xenvif_stats[i].name, ETH_GSTRING_LEN); | |
266 | break; | |
267 | } | |
268 | } | |
269 | ||
813abbba | 270 | static const struct ethtool_ops xenvif_ethtool_ops = { |
f942dc25 IC |
271 | .get_link = ethtool_op_get_link, |
272 | ||
273 | .get_sset_count = xenvif_get_sset_count, | |
274 | .get_ethtool_stats = xenvif_get_ethtool_stats, | |
275 | .get_strings = xenvif_get_strings, | |
276 | }; | |
277 | ||
813abbba | 278 | static const struct net_device_ops xenvif_netdev_ops = { |
f942dc25 IC |
279 | .ndo_start_xmit = xenvif_start_xmit, |
280 | .ndo_get_stats = xenvif_get_stats, | |
281 | .ndo_open = xenvif_open, | |
282 | .ndo_stop = xenvif_close, | |
283 | .ndo_change_mtu = xenvif_change_mtu, | |
47103041 | 284 | .ndo_fix_features = xenvif_fix_features, |
4a633a60 MW |
285 | .ndo_set_mac_address = eth_mac_addr, |
286 | .ndo_validate_addr = eth_validate_addr, | |
f942dc25 IC |
287 | }; |
288 | ||
289 | struct xenvif *xenvif_alloc(struct device *parent, domid_t domid, | |
290 | unsigned int handle) | |
291 | { | |
292 | int err; | |
293 | struct net_device *dev; | |
294 | struct xenvif *vif; | |
295 | char name[IFNAMSIZ] = {}; | |
b3f980bd | 296 | int i; |
f942dc25 IC |
297 | |
298 | snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle); | |
299 | dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup); | |
300 | if (dev == NULL) { | |
b3f980bd | 301 | pr_warn("Could not allocate netdev for %s\n", name); |
f942dc25 IC |
302 | return ERR_PTR(-ENOMEM); |
303 | } | |
304 | ||
305 | SET_NETDEV_DEV(dev, parent); | |
306 | ||
307 | vif = netdev_priv(dev); | |
308 | vif->domid = domid; | |
309 | vif->handle = handle; | |
f942dc25 | 310 | vif->can_sg = 1; |
146c8a77 | 311 | vif->ip_csum = 1; |
f942dc25 | 312 | vif->dev = dev; |
f942dc25 IC |
313 | |
314 | vif->credit_bytes = vif->remaining_credit = ~0UL; | |
315 | vif->credit_usec = 0UL; | |
316 | init_timer(&vif->credit_timeout); | |
317 | /* Initialize 'expires' now: it's used to track the credit window. */ | |
318 | vif->credit_timeout.expires = jiffies; | |
319 | ||
320 | dev->netdev_ops = &xenvif_netdev_ops; | |
146c8a77 PD |
321 | dev->hw_features = NETIF_F_SG | |
322 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | | |
323 | NETIF_F_TSO; | |
47103041 | 324 | dev->features = dev->hw_features; |
f942dc25 IC |
325 | SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops); |
326 | ||
327 | dev->tx_queue_len = XENVIF_QUEUE_LENGTH; | |
328 | ||
b3f980bd WL |
329 | skb_queue_head_init(&vif->rx_queue); |
330 | skb_queue_head_init(&vif->tx_queue); | |
331 | ||
332 | vif->pending_cons = 0; | |
333 | vif->pending_prod = MAX_PENDING_REQS; | |
334 | for (i = 0; i < MAX_PENDING_REQS; i++) | |
335 | vif->pending_ring[i] = i; | |
336 | for (i = 0; i < MAX_PENDING_REQS; i++) | |
337 | vif->mmap_pages[i] = NULL; | |
338 | ||
f942dc25 IC |
339 | /* |
340 | * Initialise a dummy MAC address. We choose the numerically | |
341 | * largest non-broadcast address to prevent the address getting | |
342 | * stolen by an Ethernet bridge for STP purposes. | |
343 | * (FE:FF:FF:FF:FF:FF) | |
344 | */ | |
345 | memset(dev->dev_addr, 0xFF, ETH_ALEN); | |
346 | dev->dev_addr[0] &= ~0x01; | |
347 | ||
b3f980bd WL |
348 | netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT); |
349 | ||
f942dc25 IC |
350 | netif_carrier_off(dev); |
351 | ||
352 | err = register_netdev(dev); | |
353 | if (err) { | |
354 | netdev_warn(dev, "Could not register device: err=%d\n", err); | |
355 | free_netdev(dev); | |
356 | return ERR_PTR(err); | |
357 | } | |
358 | ||
359 | netdev_dbg(dev, "Successfully created xenvif\n"); | |
279f438e PD |
360 | |
361 | __module_get(THIS_MODULE); | |
362 | ||
f942dc25 IC |
363 | return vif; |
364 | } | |
365 | ||
366 | int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref, | |
e1f00a69 WL |
367 | unsigned long rx_ring_ref, unsigned int tx_evtchn, |
368 | unsigned int rx_evtchn) | |
f942dc25 IC |
369 | { |
370 | int err = -ENOMEM; | |
371 | ||
372 | /* Already connected through? */ | |
e1f00a69 | 373 | if (vif->tx_irq) |
f942dc25 IC |
374 | return 0; |
375 | ||
7376419a | 376 | err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref); |
f942dc25 IC |
377 | if (err < 0) |
378 | goto err; | |
379 | ||
e1f00a69 WL |
380 | if (tx_evtchn == rx_evtchn) { |
381 | /* feature-split-event-channels == 0 */ | |
382 | err = bind_interdomain_evtchn_to_irqhandler( | |
383 | vif->domid, tx_evtchn, xenvif_interrupt, 0, | |
384 | vif->dev->name, vif); | |
385 | if (err < 0) | |
386 | goto err_unmap; | |
387 | vif->tx_irq = vif->rx_irq = err; | |
388 | disable_irq(vif->tx_irq); | |
389 | } else { | |
390 | /* feature-split-event-channels == 1 */ | |
391 | snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name), | |
392 | "%s-tx", vif->dev->name); | |
393 | err = bind_interdomain_evtchn_to_irqhandler( | |
394 | vif->domid, tx_evtchn, xenvif_tx_interrupt, 0, | |
395 | vif->tx_irq_name, vif); | |
396 | if (err < 0) | |
397 | goto err_unmap; | |
398 | vif->tx_irq = err; | |
399 | disable_irq(vif->tx_irq); | |
400 | ||
401 | snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name), | |
402 | "%s-rx", vif->dev->name); | |
403 | err = bind_interdomain_evtchn_to_irqhandler( | |
404 | vif->domid, rx_evtchn, xenvif_rx_interrupt, 0, | |
405 | vif->rx_irq_name, vif); | |
406 | if (err < 0) | |
407 | goto err_tx_unbind; | |
408 | vif->rx_irq = err; | |
409 | disable_irq(vif->rx_irq); | |
410 | } | |
f942dc25 | 411 | |
b3f980bd | 412 | init_waitqueue_head(&vif->wq); |
7376419a | 413 | vif->task = kthread_create(xenvif_kthread, |
a9677bc0 | 414 | (void *)vif, "%s", vif->dev->name); |
b3f980bd WL |
415 | if (IS_ERR(vif->task)) { |
416 | pr_warn("Could not allocate kthread for %s\n", vif->dev->name); | |
417 | err = PTR_ERR(vif->task); | |
418 | goto err_rx_unbind; | |
419 | } | |
f942dc25 IC |
420 | |
421 | rtnl_lock(); | |
47103041 MM |
422 | if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN) |
423 | dev_set_mtu(vif->dev, ETH_DATA_LEN); | |
424 | netdev_update_features(vif->dev); | |
425 | netif_carrier_on(vif->dev); | |
d0e5d832 DV |
426 | if (netif_running(vif->dev)) |
427 | xenvif_up(vif); | |
f942dc25 IC |
428 | rtnl_unlock(); |
429 | ||
b3f980bd WL |
430 | wake_up_process(vif->task); |
431 | ||
f942dc25 | 432 | return 0; |
b3f980bd WL |
433 | |
434 | err_rx_unbind: | |
435 | unbind_from_irqhandler(vif->rx_irq, vif); | |
436 | vif->rx_irq = 0; | |
e1f00a69 WL |
437 | err_tx_unbind: |
438 | unbind_from_irqhandler(vif->tx_irq, vif); | |
439 | vif->tx_irq = 0; | |
f942dc25 | 440 | err_unmap: |
7376419a | 441 | xenvif_unmap_frontend_rings(vif); |
f942dc25 | 442 | err: |
b103f358 | 443 | module_put(THIS_MODULE); |
f942dc25 IC |
444 | return err; |
445 | } | |
446 | ||
48856286 | 447 | void xenvif_carrier_off(struct xenvif *vif) |
f942dc25 IC |
448 | { |
449 | struct net_device *dev = vif->dev; | |
48856286 IC |
450 | |
451 | rtnl_lock(); | |
452 | netif_carrier_off(dev); /* discard queued packets */ | |
453 | if (netif_running(dev)) | |
454 | xenvif_down(vif); | |
455 | rtnl_unlock(); | |
48856286 IC |
456 | } |
457 | ||
458 | void xenvif_disconnect(struct xenvif *vif) | |
459 | { | |
460 | if (netif_carrier_ok(vif->dev)) | |
461 | xenvif_carrier_off(vif); | |
f942dc25 | 462 | |
e1f00a69 WL |
463 | if (vif->tx_irq) { |
464 | if (vif->tx_irq == vif->rx_irq) | |
465 | unbind_from_irqhandler(vif->tx_irq, vif); | |
466 | else { | |
467 | unbind_from_irqhandler(vif->tx_irq, vif); | |
468 | unbind_from_irqhandler(vif->rx_irq, vif); | |
469 | } | |
279f438e | 470 | vif->tx_irq = 0; |
b103f358 | 471 | } |
f942dc25 | 472 | |
b3f980bd WL |
473 | if (vif->task) |
474 | kthread_stop(vif->task); | |
475 | ||
279f438e PD |
476 | xenvif_unmap_frontend_rings(vif); |
477 | } | |
478 | ||
479 | void xenvif_free(struct xenvif *vif) | |
480 | { | |
b3f980bd WL |
481 | netif_napi_del(&vif->napi); |
482 | ||
f942dc25 IC |
483 | unregister_netdev(vif->dev); |
484 | ||
f942dc25 | 485 | free_netdev(vif->dev); |
b103f358 | 486 | |
279f438e | 487 | module_put(THIS_MODULE); |
f942dc25 | 488 | } |