net: skb_shared_info optimization
[linux-2.6/verdex.git] / drivers / net / macvlan.c
blob021d9941c2926ceb8bf92bd0f61b963876dc984e
1 /*
2 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * The code this is based on carried the following copyright notice:
10 * ---
11 * (C) Copyright 2001-2006
12 * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
13 * Re-worked by Ben Greear <greearb@candelatech.com>
14 * ---
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/rculist.h>
24 #include <linux/notifier.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_link.h>
30 #include <linux/if_macvlan.h>
31 #include <net/rtnetlink.h>
33 #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
35 struct macvlan_port {
36 struct net_device *dev;
37 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
38 struct list_head vlans;
41 struct macvlan_dev {
42 struct net_device *dev;
43 struct list_head list;
44 struct hlist_node hlist;
45 struct macvlan_port *port;
46 struct net_device *lowerdev;
50 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
51 const unsigned char *addr)
53 struct macvlan_dev *vlan;
54 struct hlist_node *n;
56 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
57 if (!compare_ether_addr(vlan->dev->dev_addr, addr))
58 return vlan;
60 return NULL;
63 static void macvlan_hash_add(struct macvlan_dev *vlan)
65 struct macvlan_port *port = vlan->port;
66 const unsigned char *addr = vlan->dev->dev_addr;
68 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
71 static void macvlan_hash_del(struct macvlan_dev *vlan)
73 hlist_del_rcu(&vlan->hlist);
74 synchronize_rcu();
77 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
78 const unsigned char *addr)
80 macvlan_hash_del(vlan);
81 /* Now that we are unhashed it is safe to change the device
82 * address without confusing packet delivery.
84 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
85 macvlan_hash_add(vlan);
88 static int macvlan_addr_busy(const struct macvlan_port *port,
89 const unsigned char *addr)
91 /* Test to see if the specified multicast address is
92 * currently in use by the underlying device or
93 * another macvlan.
95 if (memcmp(port->dev->dev_addr, addr, ETH_ALEN) == 0)
96 return 1;
98 if (macvlan_hash_lookup(port, addr))
99 return 1;
101 return 0;
104 static void macvlan_broadcast(struct sk_buff *skb,
105 const struct macvlan_port *port)
107 const struct ethhdr *eth = eth_hdr(skb);
108 const struct macvlan_dev *vlan;
109 struct hlist_node *n;
110 struct net_device *dev;
111 struct sk_buff *nskb;
112 unsigned int i;
114 if (skb->protocol == htons(ETH_P_PAUSE))
115 return;
117 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
118 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
119 dev = vlan->dev;
121 nskb = skb_clone(skb, GFP_ATOMIC);
122 if (nskb == NULL) {
123 dev->stats.rx_errors++;
124 dev->stats.rx_dropped++;
125 continue;
128 dev->stats.rx_bytes += skb->len + ETH_HLEN;
129 dev->stats.rx_packets++;
130 dev->stats.multicast++;
132 nskb->dev = dev;
133 if (!compare_ether_addr(eth->h_dest, dev->broadcast))
134 nskb->pkt_type = PACKET_BROADCAST;
135 else
136 nskb->pkt_type = PACKET_MULTICAST;
138 netif_rx(nskb);
143 /* called under rcu_read_lock() from netif_receive_skb */
144 static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
146 const struct ethhdr *eth = eth_hdr(skb);
147 const struct macvlan_port *port;
148 const struct macvlan_dev *vlan;
149 struct net_device *dev;
151 port = rcu_dereference(skb->dev->macvlan_port);
152 if (port == NULL)
153 return skb;
155 if (is_multicast_ether_addr(eth->h_dest)) {
156 macvlan_broadcast(skb, port);
157 return skb;
160 vlan = macvlan_hash_lookup(port, eth->h_dest);
161 if (vlan == NULL)
162 return skb;
164 dev = vlan->dev;
165 if (unlikely(!(dev->flags & IFF_UP))) {
166 kfree_skb(skb);
167 return NULL;
170 skb = skb_share_check(skb, GFP_ATOMIC);
171 if (skb == NULL) {
172 dev->stats.rx_errors++;
173 dev->stats.rx_dropped++;
174 return NULL;
177 dev->stats.rx_bytes += skb->len + ETH_HLEN;
178 dev->stats.rx_packets++;
180 skb->dev = dev;
181 skb->pkt_type = PACKET_HOST;
183 netif_rx(skb);
184 return NULL;
187 static int macvlan_start_xmit(struct sk_buff *skb, struct net_device *dev)
189 const struct macvlan_dev *vlan = netdev_priv(dev);
190 unsigned int len = skb->len;
191 int ret;
193 skb->dev = vlan->lowerdev;
194 ret = dev_queue_xmit(skb);
196 if (likely(ret == NET_XMIT_SUCCESS)) {
197 dev->stats.tx_packets++;
198 dev->stats.tx_bytes += len;
199 } else {
200 dev->stats.tx_errors++;
201 dev->stats.tx_aborted_errors++;
203 return NETDEV_TX_OK;
206 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
207 unsigned short type, const void *daddr,
208 const void *saddr, unsigned len)
210 const struct macvlan_dev *vlan = netdev_priv(dev);
211 struct net_device *lowerdev = vlan->lowerdev;
213 return dev_hard_header(skb, lowerdev, type, daddr,
214 saddr ? : dev->dev_addr, len);
217 static const struct header_ops macvlan_hard_header_ops = {
218 .create = macvlan_hard_header,
219 .rebuild = eth_rebuild_header,
220 .parse = eth_header_parse,
221 .cache = eth_header_cache,
222 .cache_update = eth_header_cache_update,
225 static int macvlan_open(struct net_device *dev)
227 struct macvlan_dev *vlan = netdev_priv(dev);
228 struct net_device *lowerdev = vlan->lowerdev;
229 int err;
231 err = -EBUSY;
232 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
233 goto out;
235 err = dev_unicast_add(lowerdev, dev->dev_addr);
236 if (err < 0)
237 goto out;
238 if (dev->flags & IFF_ALLMULTI) {
239 err = dev_set_allmulti(lowerdev, 1);
240 if (err < 0)
241 goto del_unicast;
243 macvlan_hash_add(vlan);
244 return 0;
246 del_unicast:
247 dev_unicast_delete(lowerdev, dev->dev_addr);
248 out:
249 return err;
252 static int macvlan_stop(struct net_device *dev)
254 struct macvlan_dev *vlan = netdev_priv(dev);
255 struct net_device *lowerdev = vlan->lowerdev;
257 dev_mc_unsync(lowerdev, dev);
258 if (dev->flags & IFF_ALLMULTI)
259 dev_set_allmulti(lowerdev, -1);
261 dev_unicast_delete(lowerdev, dev->dev_addr);
263 macvlan_hash_del(vlan);
264 return 0;
267 static int macvlan_set_mac_address(struct net_device *dev, void *p)
269 struct macvlan_dev *vlan = netdev_priv(dev);
270 struct net_device *lowerdev = vlan->lowerdev;
271 struct sockaddr *addr = p;
272 int err;
274 if (!is_valid_ether_addr(addr->sa_data))
275 return -EADDRNOTAVAIL;
277 if (!(dev->flags & IFF_UP)) {
278 /* Just copy in the new address */
279 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
280 } else {
281 /* Rehash and update the device filters */
282 if (macvlan_addr_busy(vlan->port, addr->sa_data))
283 return -EBUSY;
285 err = dev_unicast_add(lowerdev, addr->sa_data);
286 if (err)
287 return err;
289 dev_unicast_delete(lowerdev, dev->dev_addr);
291 macvlan_hash_change_addr(vlan, addr->sa_data);
293 return 0;
296 static void macvlan_change_rx_flags(struct net_device *dev, int change)
298 struct macvlan_dev *vlan = netdev_priv(dev);
299 struct net_device *lowerdev = vlan->lowerdev;
301 if (change & IFF_ALLMULTI)
302 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
305 static void macvlan_set_multicast_list(struct net_device *dev)
307 struct macvlan_dev *vlan = netdev_priv(dev);
309 dev_mc_sync(vlan->lowerdev, dev);
312 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
314 struct macvlan_dev *vlan = netdev_priv(dev);
316 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
317 return -EINVAL;
318 dev->mtu = new_mtu;
319 return 0;
323 * macvlan network devices have devices nesting below it and are a special
324 * "super class" of normal network devices; split their locks off into a
325 * separate class since they always nest.
327 static struct lock_class_key macvlan_netdev_xmit_lock_key;
328 static struct lock_class_key macvlan_netdev_addr_lock_key;
330 #define MACVLAN_FEATURES \
331 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
332 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
333 NETIF_F_TSO_ECN | NETIF_F_TSO6)
335 #define MACVLAN_STATE_MASK \
336 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
338 static void macvlan_set_lockdep_class_one(struct net_device *dev,
339 struct netdev_queue *txq,
340 void *_unused)
342 lockdep_set_class(&txq->_xmit_lock,
343 &macvlan_netdev_xmit_lock_key);
346 static void macvlan_set_lockdep_class(struct net_device *dev)
348 lockdep_set_class(&dev->addr_list_lock,
349 &macvlan_netdev_addr_lock_key);
350 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
353 static int macvlan_init(struct net_device *dev)
355 struct macvlan_dev *vlan = netdev_priv(dev);
356 const struct net_device *lowerdev = vlan->lowerdev;
358 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
359 (lowerdev->state & MACVLAN_STATE_MASK);
360 dev->features = lowerdev->features & MACVLAN_FEATURES;
361 dev->iflink = lowerdev->ifindex;
363 macvlan_set_lockdep_class(dev);
365 return 0;
368 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
369 struct ethtool_drvinfo *drvinfo)
371 snprintf(drvinfo->driver, 32, "macvlan");
372 snprintf(drvinfo->version, 32, "0.1");
375 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
377 const struct macvlan_dev *vlan = netdev_priv(dev);
378 return dev_ethtool_get_rx_csum(vlan->lowerdev);
381 static int macvlan_ethtool_get_settings(struct net_device *dev,
382 struct ethtool_cmd *cmd)
384 const struct macvlan_dev *vlan = netdev_priv(dev);
385 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
388 static u32 macvlan_ethtool_get_flags(struct net_device *dev)
390 const struct macvlan_dev *vlan = netdev_priv(dev);
391 return dev_ethtool_get_flags(vlan->lowerdev);
394 static const struct ethtool_ops macvlan_ethtool_ops = {
395 .get_link = ethtool_op_get_link,
396 .get_settings = macvlan_ethtool_get_settings,
397 .get_rx_csum = macvlan_ethtool_get_rx_csum,
398 .get_drvinfo = macvlan_ethtool_get_drvinfo,
399 .get_flags = macvlan_ethtool_get_flags,
402 static const struct net_device_ops macvlan_netdev_ops = {
403 .ndo_init = macvlan_init,
404 .ndo_open = macvlan_open,
405 .ndo_stop = macvlan_stop,
406 .ndo_start_xmit = macvlan_start_xmit,
407 .ndo_change_mtu = macvlan_change_mtu,
408 .ndo_change_rx_flags = macvlan_change_rx_flags,
409 .ndo_set_mac_address = macvlan_set_mac_address,
410 .ndo_set_multicast_list = macvlan_set_multicast_list,
411 .ndo_validate_addr = eth_validate_addr,
414 static void macvlan_setup(struct net_device *dev)
416 ether_setup(dev);
418 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
419 dev->netdev_ops = &macvlan_netdev_ops;
420 dev->destructor = free_netdev;
421 dev->header_ops = &macvlan_hard_header_ops,
422 dev->ethtool_ops = &macvlan_ethtool_ops;
423 dev->tx_queue_len = 0;
426 static int macvlan_port_create(struct net_device *dev)
428 struct macvlan_port *port;
429 unsigned int i;
431 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
432 return -EINVAL;
434 port = kzalloc(sizeof(*port), GFP_KERNEL);
435 if (port == NULL)
436 return -ENOMEM;
438 port->dev = dev;
439 INIT_LIST_HEAD(&port->vlans);
440 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
441 INIT_HLIST_HEAD(&port->vlan_hash[i]);
442 rcu_assign_pointer(dev->macvlan_port, port);
443 return 0;
446 static void macvlan_port_destroy(struct net_device *dev)
448 struct macvlan_port *port = dev->macvlan_port;
450 rcu_assign_pointer(dev->macvlan_port, NULL);
451 synchronize_rcu();
452 kfree(port);
455 static void macvlan_transfer_operstate(struct net_device *dev)
457 struct macvlan_dev *vlan = netdev_priv(dev);
458 const struct net_device *lowerdev = vlan->lowerdev;
460 if (lowerdev->operstate == IF_OPER_DORMANT)
461 netif_dormant_on(dev);
462 else
463 netif_dormant_off(dev);
465 if (netif_carrier_ok(lowerdev)) {
466 if (!netif_carrier_ok(dev))
467 netif_carrier_on(dev);
468 } else {
469 if (netif_carrier_ok(dev))
470 netif_carrier_off(dev);
474 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
476 if (tb[IFLA_ADDRESS]) {
477 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
478 return -EINVAL;
479 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
480 return -EADDRNOTAVAIL;
482 return 0;
485 static int macvlan_newlink(struct net_device *dev,
486 struct nlattr *tb[], struct nlattr *data[])
488 struct macvlan_dev *vlan = netdev_priv(dev);
489 struct macvlan_port *port;
490 struct net_device *lowerdev;
491 int err;
493 if (!tb[IFLA_LINK])
494 return -EINVAL;
496 lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
497 if (lowerdev == NULL)
498 return -ENODEV;
500 /* When creating macvlans on top of other macvlans - use
501 * the real device as the lowerdev.
503 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
504 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
505 lowerdev = lowervlan->lowerdev;
508 if (!tb[IFLA_MTU])
509 dev->mtu = lowerdev->mtu;
510 else if (dev->mtu > lowerdev->mtu)
511 return -EINVAL;
513 if (!tb[IFLA_ADDRESS])
514 random_ether_addr(dev->dev_addr);
516 if (lowerdev->macvlan_port == NULL) {
517 err = macvlan_port_create(lowerdev);
518 if (err < 0)
519 return err;
521 port = lowerdev->macvlan_port;
523 vlan->lowerdev = lowerdev;
524 vlan->dev = dev;
525 vlan->port = port;
527 err = register_netdevice(dev);
528 if (err < 0)
529 return err;
531 list_add_tail(&vlan->list, &port->vlans);
532 macvlan_transfer_operstate(dev);
533 return 0;
536 static void macvlan_dellink(struct net_device *dev)
538 struct macvlan_dev *vlan = netdev_priv(dev);
539 struct macvlan_port *port = vlan->port;
541 list_del(&vlan->list);
542 unregister_netdevice(dev);
544 if (list_empty(&port->vlans))
545 macvlan_port_destroy(port->dev);
548 static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
549 .kind = "macvlan",
550 .priv_size = sizeof(struct macvlan_dev),
551 .setup = macvlan_setup,
552 .validate = macvlan_validate,
553 .newlink = macvlan_newlink,
554 .dellink = macvlan_dellink,
557 static int macvlan_device_event(struct notifier_block *unused,
558 unsigned long event, void *ptr)
560 struct net_device *dev = ptr;
561 struct macvlan_dev *vlan, *next;
562 struct macvlan_port *port;
564 port = dev->macvlan_port;
565 if (port == NULL)
566 return NOTIFY_DONE;
568 switch (event) {
569 case NETDEV_CHANGE:
570 list_for_each_entry(vlan, &port->vlans, list)
571 macvlan_transfer_operstate(vlan->dev);
572 break;
573 case NETDEV_FEAT_CHANGE:
574 list_for_each_entry(vlan, &port->vlans, list) {
575 vlan->dev->features = dev->features & MACVLAN_FEATURES;
576 netdev_features_change(vlan->dev);
578 break;
579 case NETDEV_UNREGISTER:
580 list_for_each_entry_safe(vlan, next, &port->vlans, list)
581 macvlan_dellink(vlan->dev);
582 break;
584 return NOTIFY_DONE;
587 static struct notifier_block macvlan_notifier_block __read_mostly = {
588 .notifier_call = macvlan_device_event,
591 static int __init macvlan_init_module(void)
593 int err;
595 register_netdevice_notifier(&macvlan_notifier_block);
596 macvlan_handle_frame_hook = macvlan_handle_frame;
598 err = rtnl_link_register(&macvlan_link_ops);
599 if (err < 0)
600 goto err1;
601 return 0;
602 err1:
603 macvlan_handle_frame_hook = NULL;
604 unregister_netdevice_notifier(&macvlan_notifier_block);
605 return err;
608 static void __exit macvlan_cleanup_module(void)
610 rtnl_link_unregister(&macvlan_link_ops);
611 macvlan_handle_frame_hook = NULL;
612 unregister_netdevice_notifier(&macvlan_notifier_block);
615 module_init(macvlan_init_module);
616 module_exit(macvlan_cleanup_module);
618 MODULE_LICENSE("GPL");
619 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
620 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
621 MODULE_ALIAS_RTNL_LINK("macvlan");