net: fix 64 bit counters on 32 bit arches
[linux-3.10.git] / net / bridge / br_device.c
1 /*
2  *      Device handling code
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
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>
16 #include <linux/netpoll.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/list.h>
20 #include <linux/netfilter_bridge.h>
21
22 #include <asm/uaccess.h>
23 #include "br_private.h"
24
25 /* net device transmit always called with no BH (preempt_disabled) */
26 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
27 {
28         struct net_bridge *br = netdev_priv(dev);
29         const unsigned char *dest = skb->data;
30         struct net_bridge_fdb_entry *dst;
31         struct net_bridge_mdb_entry *mdst;
32         struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats);
33
34 #ifdef CONFIG_BRIDGE_NETFILTER
35         if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
36                 br_nf_pre_routing_finish_bridge_slow(skb);
37                 return NETDEV_TX_OK;
38         }
39 #endif
40
41         u64_stats_update_begin(&brstats->syncp);
42         brstats->tx_packets++;
43         brstats->tx_bytes += skb->len;
44         u64_stats_update_end(&brstats->syncp);
45
46         BR_INPUT_SKB_CB(skb)->brdev = dev;
47
48         skb_reset_mac_header(skb);
49         skb_pull(skb, ETH_HLEN);
50
51         if (is_multicast_ether_addr(dest)) {
52                 if (unlikely(netpoll_tx_running(dev))) {
53                         br_flood_deliver(br, skb);
54                         goto out;
55                 }
56                 if (br_multicast_rcv(br, NULL, skb))
57                         goto out;
58
59                 mdst = br_mdb_get(br, skb);
60                 if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb))
61                         br_multicast_deliver(mdst, skb);
62                 else
63                         br_flood_deliver(br, skb);
64         } else if ((dst = __br_fdb_get(br, dest)) != NULL)
65                 br_deliver(dst->dst, skb);
66         else
67                 br_flood_deliver(br, skb);
68
69 out:
70         return NETDEV_TX_OK;
71 }
72
73 static int br_dev_open(struct net_device *dev)
74 {
75         struct net_bridge *br = netdev_priv(dev);
76
77         br_features_recompute(br);
78         netif_start_queue(dev);
79         br_stp_enable_bridge(br);
80         br_multicast_open(br);
81
82         return 0;
83 }
84
85 static void br_dev_set_multicast_list(struct net_device *dev)
86 {
87 }
88
89 static int br_dev_stop(struct net_device *dev)
90 {
91         struct net_bridge *br = netdev_priv(dev);
92
93         br_stp_disable_bridge(br);
94         br_multicast_stop(br);
95
96         netif_stop_queue(dev);
97
98         return 0;
99 }
100
101 static struct rtnl_link_stats64 *br_get_stats64(struct net_device *dev,
102                                                 struct rtnl_link_stats64 *stats)
103 {
104         struct net_bridge *br = netdev_priv(dev);
105         struct br_cpu_netstats tmp, sum = { 0 };
106         unsigned int cpu;
107
108         for_each_possible_cpu(cpu) {
109                 unsigned int start;
110                 const struct br_cpu_netstats *bstats
111                         = per_cpu_ptr(br->stats, cpu);
112                 do {
113                         start = u64_stats_fetch_begin(&bstats->syncp);
114                         memcpy(&tmp, bstats, sizeof(tmp));
115                 } while (u64_stats_fetch_retry(&bstats->syncp, start));
116                 sum.tx_bytes   += tmp.tx_bytes;
117                 sum.tx_packets += tmp.tx_packets;
118                 sum.rx_bytes   += tmp.rx_bytes;
119                 sum.rx_packets += tmp.rx_packets;
120         }
121
122         stats->tx_bytes   = sum.tx_bytes;
123         stats->tx_packets = sum.tx_packets;
124         stats->rx_bytes   = sum.rx_bytes;
125         stats->rx_packets = sum.rx_packets;
126
127         return stats;
128 }
129
130 static int br_change_mtu(struct net_device *dev, int new_mtu)
131 {
132         struct net_bridge *br = netdev_priv(dev);
133         if (new_mtu < 68 || new_mtu > br_min_mtu(br))
134                 return -EINVAL;
135
136         dev->mtu = new_mtu;
137
138 #ifdef CONFIG_BRIDGE_NETFILTER
139         /* remember the MTU in the rtable for PMTU */
140         br->fake_rtable.dst.metrics[RTAX_MTU - 1] = new_mtu;
141 #endif
142
143         return 0;
144 }
145
146 /* Allow setting mac address to any valid ethernet address. */
147 static int br_set_mac_address(struct net_device *dev, void *p)
148 {
149         struct net_bridge *br = netdev_priv(dev);
150         struct sockaddr *addr = p;
151
152         if (!is_valid_ether_addr(addr->sa_data))
153                 return -EINVAL;
154
155         spin_lock_bh(&br->lock);
156         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
157         br_stp_change_bridge_id(br, addr->sa_data);
158         br->flags |= BR_SET_MAC_ADDR;
159         spin_unlock_bh(&br->lock);
160
161         return 0;
162 }
163
164 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
165 {
166         strcpy(info->driver, "bridge");
167         strcpy(info->version, BR_VERSION);
168         strcpy(info->fw_version, "N/A");
169         strcpy(info->bus_info, "N/A");
170 }
171
172 static int br_set_sg(struct net_device *dev, u32 data)
173 {
174         struct net_bridge *br = netdev_priv(dev);
175
176         if (data)
177                 br->feature_mask |= NETIF_F_SG;
178         else
179                 br->feature_mask &= ~NETIF_F_SG;
180
181         br_features_recompute(br);
182         return 0;
183 }
184
185 static int br_set_tso(struct net_device *dev, u32 data)
186 {
187         struct net_bridge *br = netdev_priv(dev);
188
189         if (data)
190                 br->feature_mask |= NETIF_F_TSO;
191         else
192                 br->feature_mask &= ~NETIF_F_TSO;
193
194         br_features_recompute(br);
195         return 0;
196 }
197
198 static int br_set_tx_csum(struct net_device *dev, u32 data)
199 {
200         struct net_bridge *br = netdev_priv(dev);
201
202         if (data)
203                 br->feature_mask |= NETIF_F_NO_CSUM;
204         else
205                 br->feature_mask &= ~NETIF_F_ALL_CSUM;
206
207         br_features_recompute(br);
208         return 0;
209 }
210
211 #ifdef CONFIG_NET_POLL_CONTROLLER
212 static void br_poll_controller(struct net_device *br_dev)
213 {
214 }
215
216 static void br_netpoll_cleanup(struct net_device *dev)
217 {
218         struct net_bridge *br = netdev_priv(dev);
219         struct net_bridge_port *p, *n;
220
221         list_for_each_entry_safe(p, n, &br->port_list, list) {
222                 br_netpoll_disable(p);
223         }
224 }
225
226 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
227 {
228         struct net_bridge *br = netdev_priv(dev);
229         struct net_bridge_port *p, *n;
230         int err = 0;
231
232         list_for_each_entry_safe(p, n, &br->port_list, list) {
233                 if (!p->dev)
234                         continue;
235
236                 err = br_netpoll_enable(p);
237                 if (err)
238                         goto fail;
239         }
240
241 out:
242         return err;
243
244 fail:
245         br_netpoll_cleanup(dev);
246         goto out;
247 }
248
249 int br_netpoll_enable(struct net_bridge_port *p)
250 {
251         struct netpoll *np;
252         int err = 0;
253
254         np = kzalloc(sizeof(*p->np), GFP_KERNEL);
255         err = -ENOMEM;
256         if (!np)
257                 goto out;
258
259         np->dev = p->dev;
260
261         err = __netpoll_setup(np);
262         if (err) {
263                 kfree(np);
264                 goto out;
265         }
266
267         p->np = np;
268
269 out:
270         return err;
271 }
272
273 void br_netpoll_disable(struct net_bridge_port *p)
274 {
275         struct netpoll *np = p->np;
276
277         if (!np)
278                 return;
279
280         p->np = NULL;
281
282         /* Wait for transmitting packets to finish before freeing. */
283         synchronize_rcu_bh();
284
285         __netpoll_cleanup(np);
286         kfree(np);
287 }
288
289 #endif
290
291 static const struct ethtool_ops br_ethtool_ops = {
292         .get_drvinfo    = br_getinfo,
293         .get_link       = ethtool_op_get_link,
294         .get_tx_csum    = ethtool_op_get_tx_csum,
295         .set_tx_csum    = br_set_tx_csum,
296         .get_sg         = ethtool_op_get_sg,
297         .set_sg         = br_set_sg,
298         .get_tso        = ethtool_op_get_tso,
299         .set_tso        = br_set_tso,
300         .get_ufo        = ethtool_op_get_ufo,
301         .set_ufo        = ethtool_op_set_ufo,
302         .get_flags      = ethtool_op_get_flags,
303 };
304
305 static const struct net_device_ops br_netdev_ops = {
306         .ndo_open                = br_dev_open,
307         .ndo_stop                = br_dev_stop,
308         .ndo_start_xmit          = br_dev_xmit,
309         .ndo_get_stats64         = br_get_stats64,
310         .ndo_set_mac_address     = br_set_mac_address,
311         .ndo_set_multicast_list  = br_dev_set_multicast_list,
312         .ndo_change_mtu          = br_change_mtu,
313         .ndo_do_ioctl            = br_dev_ioctl,
314 #ifdef CONFIG_NET_POLL_CONTROLLER
315         .ndo_netpoll_setup       = br_netpoll_setup,
316         .ndo_netpoll_cleanup     = br_netpoll_cleanup,
317         .ndo_poll_controller     = br_poll_controller,
318 #endif
319 };
320
321 static void br_dev_free(struct net_device *dev)
322 {
323         struct net_bridge *br = netdev_priv(dev);
324
325         free_percpu(br->stats);
326         free_netdev(dev);
327 }
328
329 void br_dev_setup(struct net_device *dev)
330 {
331         random_ether_addr(dev->dev_addr);
332         ether_setup(dev);
333
334         dev->netdev_ops = &br_netdev_ops;
335         dev->destructor = br_dev_free;
336         SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
337         dev->tx_queue_len = 0;
338         dev->priv_flags = IFF_EBRIDGE;
339
340         dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
341                         NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX |
342                         NETIF_F_NETNS_LOCAL | NETIF_F_GSO;
343 }