[BRIDGE]: limited ethtool support
[linux-2.6.git] / net / bridge / br_if.c
1 /*
2  *      Userspace interface
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
8  *      $Id: br_if.c,v 1.7 2001/12/24 00:59:55 davem Exp $
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/if_arp.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/rtnetlink.h>
23 #include <net/sock.h>
24
25 #include "br_private.h"
26
27 /*
28  * Determine initial path cost based on speed.
29  * using recommendations from 802.1d standard
30  *
31  * Need to simulate user ioctl because not all device's that support
32  * ethtool, use ethtool_ops.  Also, since driver might sleep need to
33  * not be holding any locks.
34  */
35 static int port_cost(struct net_device *dev)
36 {
37         struct ethtool_cmd ecmd = { ETHTOOL_GSET };
38         struct ifreq ifr;
39         mm_segment_t old_fs;
40         int err;
41
42         strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
43         ifr.ifr_data = (void __user *) &ecmd;
44
45         old_fs = get_fs();
46         set_fs(KERNEL_DS);
47         err = dev_ethtool(&ifr);
48         set_fs(old_fs);
49         
50         if (!err) {
51                 switch(ecmd.speed) {
52                 case SPEED_100:
53                         return 19;
54                 case SPEED_1000:
55                         return 4;
56                 case SPEED_10000:
57                         return 2;
58                 case SPEED_10:
59                         return 100;
60                 }
61         }
62
63         /* Old silly heuristics based on name */
64         if (!strncmp(dev->name, "lec", 3))
65                 return 7;
66
67         if (!strncmp(dev->name, "plip", 4))
68                 return 2500;
69
70         return 100;     /* assume old 10Mbps */
71 }
72
73
74 /*
75  * Check for port carrier transistions.
76  * Called from work queue to allow for calling functions that
77  * might sleep (such as speed check), and to debounce.
78  */
79 static void port_carrier_check(void *arg)
80 {
81         struct net_bridge_port *p = arg;
82
83         rtnl_lock();
84         if (netif_carrier_ok(p->dev)) {
85                 u32 cost = port_cost(p->dev);
86
87                 spin_lock_bh(&p->br->lock);
88                 if (p->state == BR_STATE_DISABLED) {
89                         p->path_cost = cost;
90                         br_stp_enable_port(p);
91                 }
92                 spin_unlock_bh(&p->br->lock);
93         } else {
94                 spin_lock_bh(&p->br->lock);
95                 if (p->state != BR_STATE_DISABLED)
96                         br_stp_disable_port(p);
97                 spin_unlock_bh(&p->br->lock);
98         }
99         rtnl_unlock();
100 }
101
102 static void destroy_nbp(struct net_bridge_port *p)
103 {
104         struct net_device *dev = p->dev;
105
106         p->br = NULL;
107         p->dev = NULL;
108         dev_put(dev);
109
110         br_sysfs_freeif(p);
111 }
112
113 static void destroy_nbp_rcu(struct rcu_head *head)
114 {
115         struct net_bridge_port *p =
116                         container_of(head, struct net_bridge_port, rcu);
117         destroy_nbp(p);
118 }
119
120 /* called with RTNL */
121 static void del_nbp(struct net_bridge_port *p)
122 {
123         struct net_bridge *br = p->br;
124         struct net_device *dev = p->dev;
125
126         dev->br_port = NULL;
127         dev_set_promiscuity(dev, -1);
128
129         cancel_delayed_work(&p->carrier_check);
130         flush_scheduled_work();
131
132         spin_lock_bh(&br->lock);
133         br_stp_disable_port(p);
134         spin_unlock_bh(&br->lock);
135
136         br_fdb_delete_by_port(br, p);
137
138         list_del_rcu(&p->list);
139
140         del_timer_sync(&p->message_age_timer);
141         del_timer_sync(&p->forward_delay_timer);
142         del_timer_sync(&p->hold_timer);
143         
144         call_rcu(&p->rcu, destroy_nbp_rcu);
145 }
146
147 /* called with RTNL */
148 static void del_br(struct net_bridge *br)
149 {
150         struct net_bridge_port *p, *n;
151
152         list_for_each_entry_safe(p, n, &br->port_list, list) {
153                 br_sysfs_removeif(p);
154                 del_nbp(p);
155         }
156
157         del_timer_sync(&br->gc_timer);
158
159         br_sysfs_delbr(br->dev);
160         unregister_netdevice(br->dev);
161 }
162
163 static struct net_device *new_bridge_dev(const char *name)
164 {
165         struct net_bridge *br;
166         struct net_device *dev;
167
168         dev = alloc_netdev(sizeof(struct net_bridge), name,
169                            br_dev_setup);
170         
171         if (!dev)
172                 return NULL;
173
174         br = netdev_priv(dev);
175         br->dev = dev;
176
177         spin_lock_init(&br->lock);
178         INIT_LIST_HEAD(&br->port_list);
179         spin_lock_init(&br->hash_lock);
180
181         br->bridge_id.prio[0] = 0x80;
182         br->bridge_id.prio[1] = 0x00;
183         memset(br->bridge_id.addr, 0, ETH_ALEN);
184
185         br->feature_mask = dev->features;
186         br->stp_enabled = 0;
187         br->designated_root = br->bridge_id;
188         br->root_path_cost = 0;
189         br->root_port = 0;
190         br->bridge_max_age = br->max_age = 20 * HZ;
191         br->bridge_hello_time = br->hello_time = 2 * HZ;
192         br->bridge_forward_delay = br->forward_delay = 15 * HZ;
193         br->topology_change = 0;
194         br->topology_change_detected = 0;
195         br->ageing_time = 300 * HZ;
196         INIT_LIST_HEAD(&br->age_list);
197
198         br_stp_timer_init(br);
199
200         return dev;
201 }
202
203 /* find an available port number */
204 static int find_portno(struct net_bridge *br)
205 {
206         int index;
207         struct net_bridge_port *p;
208         unsigned long *inuse;
209
210         inuse = kmalloc(BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long),
211                         GFP_KERNEL);
212         if (!inuse)
213                 return -ENOMEM;
214
215         memset(inuse, 0, BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long));
216         set_bit(0, inuse);      /* zero is reserved */
217         list_for_each_entry(p, &br->port_list, list) {
218                 set_bit(p->port_no, inuse);
219         }
220         index = find_first_zero_bit(inuse, BR_MAX_PORTS);
221         kfree(inuse);
222
223         return (index >= BR_MAX_PORTS) ? -EXFULL : index;
224 }
225
226 /* called with RTNL but without bridge lock */
227 static struct net_bridge_port *new_nbp(struct net_bridge *br, 
228                                        struct net_device *dev)
229 {
230         int index;
231         struct net_bridge_port *p;
232         
233         index = find_portno(br);
234         if (index < 0)
235                 return ERR_PTR(index);
236
237         p = kmalloc(sizeof(*p), GFP_KERNEL);
238         if (p == NULL)
239                 return ERR_PTR(-ENOMEM);
240
241         memset(p, 0, sizeof(*p));
242         p->br = br;
243         dev_hold(dev);
244         p->dev = dev;
245         p->path_cost = port_cost(dev);
246         p->priority = 0x8000 >> BR_PORT_BITS;
247         dev->br_port = p;
248         p->port_no = index;
249         br_init_port(p);
250         p->state = BR_STATE_DISABLED;
251         INIT_WORK(&p->carrier_check, port_carrier_check, p);
252         kobject_init(&p->kobj);
253
254         return p;
255 }
256
257 int br_add_bridge(const char *name)
258 {
259         struct net_device *dev;
260         int ret;
261
262         dev = new_bridge_dev(name);
263         if (!dev) 
264                 return -ENOMEM;
265
266         rtnl_lock();
267         if (strchr(dev->name, '%')) {
268                 ret = dev_alloc_name(dev, dev->name);
269                 if (ret < 0)
270                         goto err1;
271         }
272
273         ret = register_netdevice(dev);
274         if (ret)
275                 goto err2;
276
277         /* network device kobject is not setup until
278          * after rtnl_unlock does it's hotplug magic.
279          * so hold reference to avoid race.
280          */
281         dev_hold(dev);
282         rtnl_unlock();
283
284         ret = br_sysfs_addbr(dev);
285         dev_put(dev);
286
287         if (ret) 
288                 unregister_netdev(dev);
289  out:
290         return ret;
291
292  err2:
293         free_netdev(dev);
294  err1:
295         rtnl_unlock();
296         goto out;
297 }
298
299 int br_del_bridge(const char *name)
300 {
301         struct net_device *dev;
302         int ret = 0;
303
304         rtnl_lock();
305         dev = __dev_get_by_name(name);
306         if (dev == NULL) 
307                 ret =  -ENXIO;  /* Could not find device */
308
309         else if (!(dev->priv_flags & IFF_EBRIDGE)) {
310                 /* Attempt to delete non bridge device! */
311                 ret = -EPERM;
312         }
313
314         else if (dev->flags & IFF_UP) {
315                 /* Not shutdown yet. */
316                 ret = -EBUSY;
317         } 
318
319         else 
320                 del_br(netdev_priv(dev));
321
322         rtnl_unlock();
323         return ret;
324 }
325
326 /* Mtu of the bridge pseudo-device 1500 or the minimum of the ports */
327 int br_min_mtu(const struct net_bridge *br)
328 {
329         const struct net_bridge_port *p;
330         int mtu = 0;
331
332         ASSERT_RTNL();
333
334         if (list_empty(&br->port_list))
335                 mtu = 1500;
336         else {
337                 list_for_each_entry(p, &br->port_list, list) {
338                         if (!mtu  || p->dev->mtu < mtu)
339                                 mtu = p->dev->mtu;
340                 }
341         }
342         return mtu;
343 }
344
345 /*
346  * Recomputes features using slave's features
347  */
348 void br_features_recompute(struct net_bridge *br)
349 {
350         struct net_bridge_port *p;
351         unsigned long features, checksum;
352
353         features = br->feature_mask &~ NETIF_F_IP_CSUM;
354         checksum = br->feature_mask & NETIF_F_IP_CSUM;
355
356         list_for_each_entry(p, &br->port_list, list) {
357                 if (!(p->dev->features 
358                       & (NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM)))
359                         checksum = 0;
360                 features &= p->dev->features;
361         }
362
363         br->dev->features = features | checksum | NETIF_F_LLTX;
364 }
365
366 /* called with RTNL */
367 int br_add_if(struct net_bridge *br, struct net_device *dev)
368 {
369         struct net_bridge_port *p;
370         int err = 0;
371
372         if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
373                 return -EINVAL;
374
375         if (dev->hard_start_xmit == br_dev_xmit)
376                 return -ELOOP;
377
378         if (dev->br_port != NULL)
379                 return -EBUSY;
380
381         if (IS_ERR(p = new_nbp(br, dev)))
382                 return PTR_ERR(p);
383
384         if ((err = br_fdb_insert(br, p, dev->dev_addr)))
385                 destroy_nbp(p);
386  
387         else if ((err = br_sysfs_addif(p)))
388                 del_nbp(p);
389         else {
390                 dev_set_promiscuity(dev, 1);
391
392                 list_add_rcu(&p->list, &br->port_list);
393
394                 spin_lock_bh(&br->lock);
395                 br_stp_recalculate_bridge_id(br);
396                 br_features_recompute(br);
397                 if ((br->dev->flags & IFF_UP) 
398                     && (dev->flags & IFF_UP) && netif_carrier_ok(dev))
399                         br_stp_enable_port(p);
400                 spin_unlock_bh(&br->lock);
401
402                 dev_set_mtu(br->dev, br_min_mtu(br));
403         }
404
405         return err;
406 }
407
408 /* called with RTNL */
409 int br_del_if(struct net_bridge *br, struct net_device *dev)
410 {
411         struct net_bridge_port *p = dev->br_port;
412         
413         if (!p || p->br != br) 
414                 return -EINVAL;
415
416         br_sysfs_removeif(p);
417         del_nbp(p);
418
419         spin_lock_bh(&br->lock);
420         br_stp_recalculate_bridge_id(br);
421         br_features_recompute(br);
422         spin_unlock_bh(&br->lock);
423
424         return 0;
425 }
426
427 void __exit br_cleanup_bridges(void)
428 {
429         struct net_device *dev, *nxt;
430
431         rtnl_lock();
432         for (dev = dev_base; dev; dev = nxt) {
433                 nxt = dev->next;
434                 if (dev->priv_flags & IFF_EBRIDGE)
435                         del_br(dev->priv);
436         }
437         rtnl_unlock();
438
439 }