netpoll: add generic support for bridge and bonding devices
[linux-block.git] / net / bridge / br_device.c
CommitLineData
1da177e4
LT
1/*
2 * Device handling code
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
1da177e4
LT
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/netdevice.h>
4505a3ef 16#include <linux/etherdevice.h>
edb5e46f 17#include <linux/ethtool.h>
4505a3ef 18
1da177e4
LT
19#include <asm/uaccess.h>
20#include "br_private.h"
21
f8ae737d 22/* net device transmit always called with no BH (preempt_disabled) */
6fef4c0c 23netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
24{
25 struct net_bridge *br = netdev_priv(dev);
26 const unsigned char *dest = skb->data;
27 struct net_bridge_fdb_entry *dst;
c4fcb78c 28 struct net_bridge_mdb_entry *mdst;
14bb4789 29 struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats);
1da177e4 30
14bb4789 31 brstats->tx_packets++;
32 brstats->tx_bytes += skb->len;
6088a539 33
14bb4789 34 BR_INPUT_SKB_CB(skb)->brdev = dev;
1da177e4 35
459a98ed 36 skb_reset_mac_header(skb);
1da177e4
LT
37 skb_pull(skb, ETH_HLEN);
38
7180f775 39 if (is_multicast_ether_addr(dest)) {
c4fcb78c
HX
40 if (br_multicast_rcv(br, NULL, skb))
41 goto out;
42
43 mdst = br_mdb_get(br, skb);
32dec5dd 44 if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb))
c4fcb78c
HX
45 br_multicast_deliver(mdst, skb);
46 else
47 br_flood_deliver(br, skb);
48 } else if ((dst = __br_fdb_get(br, dest)) != NULL)
1da177e4
LT
49 br_deliver(dst->dst, skb);
50 else
e081e1e3 51 br_flood_deliver(br, skb);
1da177e4 52
c4fcb78c 53out:
6ed10654 54 return NETDEV_TX_OK;
1da177e4
LT
55}
56
57static int br_dev_open(struct net_device *dev)
58{
81d35307 59 struct net_bridge *br = netdev_priv(dev);
1da177e4 60
81d35307
SH
61 br_features_recompute(br);
62 netif_start_queue(dev);
63 br_stp_enable_bridge(br);
3fe2d7c7 64 br_multicast_open(br);
1da177e4
LT
65
66 return 0;
67}
68
69static void br_dev_set_multicast_list(struct net_device *dev)
70{
71}
72
73static int br_dev_stop(struct net_device *dev)
74{
3fe2d7c7
HX
75 struct net_bridge *br = netdev_priv(dev);
76
77 br_stp_disable_bridge(br);
78 br_multicast_stop(br);
1da177e4
LT
79
80 netif_stop_queue(dev);
81
82 return 0;
83}
84
14bb4789 85static struct net_device_stats *br_get_stats(struct net_device *dev)
86{
87 struct net_bridge *br = netdev_priv(dev);
88 struct net_device_stats *stats = &dev->stats;
89 struct br_cpu_netstats sum = { 0 };
90 unsigned int cpu;
91
92 for_each_possible_cpu(cpu) {
93 const struct br_cpu_netstats *bstats
94 = per_cpu_ptr(br->stats, cpu);
95
96 sum.tx_bytes += bstats->tx_bytes;
97 sum.tx_packets += bstats->tx_packets;
98 sum.rx_bytes += bstats->rx_bytes;
99 sum.rx_packets += bstats->rx_packets;
100 }
101
102 stats->tx_bytes = sum.tx_bytes;
103 stats->tx_packets = sum.tx_packets;
104 stats->rx_bytes = sum.rx_bytes;
105 stats->rx_packets = sum.rx_packets;
106
107 return stats;
108}
109
1da177e4
LT
110static int br_change_mtu(struct net_device *dev, int new_mtu)
111{
4adf0af6
SW
112 struct net_bridge *br = netdev_priv(dev);
113 if (new_mtu < 68 || new_mtu > br_min_mtu(br))
1da177e4
LT
114 return -EINVAL;
115
116 dev->mtu = new_mtu;
4adf0af6
SW
117
118#ifdef CONFIG_BRIDGE_NETFILTER
119 /* remember the MTU in the rtable for PMTU */
120 br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu;
121#endif
122
1da177e4
LT
123 return 0;
124}
125
ffe1d49c 126/* Allow setting mac address to any valid ethernet address. */
4505a3ef
SH
127static int br_set_mac_address(struct net_device *dev, void *p)
128{
129 struct net_bridge *br = netdev_priv(dev);
130 struct sockaddr *addr = p;
ffe1d49c
SH
131
132 if (!is_valid_ether_addr(addr->sa_data))
133 return -EINVAL;
4505a3ef
SH
134
135 spin_lock_bh(&br->lock);
ffe1d49c
SH
136 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
137 br_stp_change_bridge_id(br, addr->sa_data);
92c0574f 138 br->flags |= BR_SET_MAC_ADDR;
4505a3ef
SH
139 spin_unlock_bh(&br->lock);
140
ffe1d49c 141 return 0;
4505a3ef
SH
142}
143
edb5e46f
SH
144static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
145{
146 strcpy(info->driver, "bridge");
147 strcpy(info->version, BR_VERSION);
148 strcpy(info->fw_version, "N/A");
149 strcpy(info->bus_info, "N/A");
150}
151
152static int br_set_sg(struct net_device *dev, u32 data)
153{
154 struct net_bridge *br = netdev_priv(dev);
155
156 if (data)
157 br->feature_mask |= NETIF_F_SG;
158 else
159 br->feature_mask &= ~NETIF_F_SG;
160
161 br_features_recompute(br);
162 return 0;
163}
164
165static int br_set_tso(struct net_device *dev, u32 data)
166{
167 struct net_bridge *br = netdev_priv(dev);
168
169 if (data)
170 br->feature_mask |= NETIF_F_TSO;
171 else
172 br->feature_mask &= ~NETIF_F_TSO;
173
174 br_features_recompute(br);
175 return 0;
176}
177
178static int br_set_tx_csum(struct net_device *dev, u32 data)
179{
180 struct net_bridge *br = netdev_priv(dev);
181
182 if (data)
2c6cc0d8 183 br->feature_mask |= NETIF_F_NO_CSUM;
edb5e46f 184 else
2c6cc0d8 185 br->feature_mask &= ~NETIF_F_ALL_CSUM;
edb5e46f
SH
186
187 br_features_recompute(br);
188 return 0;
189}
190
a2dbb882 191static const struct ethtool_ops br_ethtool_ops = {
e4119a43
SH
192 .get_drvinfo = br_getinfo,
193 .get_link = ethtool_op_get_link,
194 .get_tx_csum = ethtool_op_get_tx_csum,
195 .set_tx_csum = br_set_tx_csum,
196 .get_sg = ethtool_op_get_sg,
197 .set_sg = br_set_sg,
198 .get_tso = ethtool_op_get_tso,
199 .set_tso = br_set_tso,
200 .get_ufo = ethtool_op_get_ufo,
72dad218 201 .set_ufo = ethtool_op_set_ufo,
e4119a43 202 .get_flags = ethtool_op_get_flags,
edb5e46f
SH
203};
204
a2dbb882
SH
205static const struct net_device_ops br_netdev_ops = {
206 .ndo_open = br_dev_open,
207 .ndo_stop = br_dev_stop,
00829823 208 .ndo_start_xmit = br_dev_xmit,
14bb4789 209 .ndo_get_stats = br_get_stats,
00829823
SH
210 .ndo_set_mac_address = br_set_mac_address,
211 .ndo_set_multicast_list = br_dev_set_multicast_list,
212 .ndo_change_mtu = br_change_mtu,
213 .ndo_do_ioctl = br_dev_ioctl,
a2dbb882
SH
214};
215
14bb4789 216static void br_dev_free(struct net_device *dev)
217{
218 struct net_bridge *br = netdev_priv(dev);
219
220 free_percpu(br->stats);
221 free_netdev(dev);
222}
223
1da177e4
LT
224void br_dev_setup(struct net_device *dev)
225{
3ae41254 226 random_ether_addr(dev->dev_addr);
1da177e4
LT
227 ether_setup(dev);
228
a2dbb882 229 dev->netdev_ops = &br_netdev_ops;
14bb4789 230 dev->destructor = br_dev_free;
9d6f229f 231 SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
1da177e4 232 dev->tx_queue_len = 0;
1da177e4 233 dev->priv_flags = IFF_EBRIDGE;
edb5e46f 234
9d6f229f 235 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
4aa678ba 236 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX |
b63365a2 237 NETIF_F_NETNS_LOCAL | NETIF_F_GSO;
1da177e4 238}