set memory ranges in N_NORMAL_MEMORY when onlined
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bridge / br_fdb.c
blob900431c69ed4929a76137c4a22515379f71ad698
1 /*
2 * Forwarding database
3 * Linux ethernet bridge
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
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.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/rculist.h>
17 #include <linux/spinlock.h>
18 #include <linux/times.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/jhash.h>
22 #include <linux/random.h>
23 #include <asm/atomic.h>
24 #include <asm/unaligned.h>
25 #include "br_private.h"
27 static struct kmem_cache *br_fdb_cache __read_mostly;
28 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
29 const unsigned char *addr);
31 static u32 fdb_salt __read_mostly;
33 int __init br_fdb_init(void)
35 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
36 sizeof(struct net_bridge_fdb_entry),
38 SLAB_HWCACHE_ALIGN, NULL);
39 if (!br_fdb_cache)
40 return -ENOMEM;
42 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
43 return 0;
46 void br_fdb_fini(void)
48 kmem_cache_destroy(br_fdb_cache);
52 /* if topology_changing then use forward_delay (default 15 sec)
53 * otherwise keep longer (default 5 minutes)
55 static inline unsigned long hold_time(const struct net_bridge *br)
57 return br->topology_change ? br->forward_delay : br->ageing_time;
60 static inline int has_expired(const struct net_bridge *br,
61 const struct net_bridge_fdb_entry *fdb)
63 return !fdb->is_static &&
64 time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
67 static inline int br_mac_hash(const unsigned char *mac)
69 /* use 1 byte of OUI cnd 3 bytes of NIC */
70 u32 key = get_unaligned((u32 *)(mac + 2));
71 return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
74 static void fdb_rcu_free(struct rcu_head *head)
76 struct net_bridge_fdb_entry *ent
77 = container_of(head, struct net_bridge_fdb_entry, rcu);
78 kmem_cache_free(br_fdb_cache, ent);
81 static inline void fdb_delete(struct net_bridge_fdb_entry *f)
83 hlist_del_rcu(&f->hlist);
84 call_rcu(&f->rcu, fdb_rcu_free);
87 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
89 struct net_bridge *br = p->br;
90 int i;
92 spin_lock_bh(&br->hash_lock);
94 /* Search all chains since old address/hash is unknown */
95 for (i = 0; i < BR_HASH_SIZE; i++) {
96 struct hlist_node *h;
97 hlist_for_each(h, &br->hash[i]) {
98 struct net_bridge_fdb_entry *f;
100 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
101 if (f->dst == p && f->is_local) {
102 /* maybe another port has same hw addr? */
103 struct net_bridge_port *op;
104 list_for_each_entry(op, &br->port_list, list) {
105 if (op != p &&
106 !compare_ether_addr(op->dev->dev_addr,
107 f->addr.addr)) {
108 f->dst = op;
109 goto insert;
113 /* delete old one */
114 fdb_delete(f);
115 goto insert;
119 insert:
120 /* insert new address, may fail if invalid address or dup. */
121 fdb_insert(br, p, newaddr);
123 spin_unlock_bh(&br->hash_lock);
126 void br_fdb_cleanup(unsigned long _data)
128 struct net_bridge *br = (struct net_bridge *)_data;
129 unsigned long delay = hold_time(br);
130 unsigned long next_timer = jiffies + br->ageing_time;
131 int i;
133 spin_lock_bh(&br->hash_lock);
134 for (i = 0; i < BR_HASH_SIZE; i++) {
135 struct net_bridge_fdb_entry *f;
136 struct hlist_node *h, *n;
138 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
139 unsigned long this_timer;
140 if (f->is_static)
141 continue;
142 this_timer = f->ageing_timer + delay;
143 if (time_before_eq(this_timer, jiffies))
144 fdb_delete(f);
145 else if (time_before(this_timer, next_timer))
146 next_timer = this_timer;
149 spin_unlock_bh(&br->hash_lock);
151 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
154 /* Completely flush all dynamic entries in forwarding database.*/
155 void br_fdb_flush(struct net_bridge *br)
157 int i;
159 spin_lock_bh(&br->hash_lock);
160 for (i = 0; i < BR_HASH_SIZE; i++) {
161 struct net_bridge_fdb_entry *f;
162 struct hlist_node *h, *n;
163 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
164 if (!f->is_static)
165 fdb_delete(f);
168 spin_unlock_bh(&br->hash_lock);
171 /* Flush all entries refering to a specific port.
172 * if do_all is set also flush static entries
174 void br_fdb_delete_by_port(struct net_bridge *br,
175 const struct net_bridge_port *p,
176 int do_all)
178 int i;
180 spin_lock_bh(&br->hash_lock);
181 for (i = 0; i < BR_HASH_SIZE; i++) {
182 struct hlist_node *h, *g;
184 hlist_for_each_safe(h, g, &br->hash[i]) {
185 struct net_bridge_fdb_entry *f
186 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
187 if (f->dst != p)
188 continue;
190 if (f->is_static && !do_all)
191 continue;
193 * if multiple ports all have the same device address
194 * then when one port is deleted, assign
195 * the local entry to other port
197 if (f->is_local) {
198 struct net_bridge_port *op;
199 list_for_each_entry(op, &br->port_list, list) {
200 if (op != p &&
201 !compare_ether_addr(op->dev->dev_addr,
202 f->addr.addr)) {
203 f->dst = op;
204 goto skip_delete;
209 fdb_delete(f);
210 skip_delete: ;
213 spin_unlock_bh(&br->hash_lock);
216 /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
217 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
218 const unsigned char *addr)
220 struct hlist_node *h;
221 struct net_bridge_fdb_entry *fdb;
223 hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
224 if (!compare_ether_addr(fdb->addr.addr, addr)) {
225 if (unlikely(has_expired(br, fdb)))
226 break;
227 return fdb;
231 return NULL;
234 #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
235 /* Interface used by ATM LANE hook to test
236 * if an addr is on some other bridge port */
237 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
239 struct net_bridge_fdb_entry *fdb;
240 int ret;
242 if (!dev->br_port)
243 return 0;
245 rcu_read_lock();
246 fdb = __br_fdb_get(dev->br_port->br, addr);
247 ret = fdb && fdb->dst->dev != dev &&
248 fdb->dst->state == BR_STATE_FORWARDING;
249 rcu_read_unlock();
251 return ret;
253 #endif /* CONFIG_ATM_LANE */
256 * Fill buffer with forwarding table records in
257 * the API format.
259 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
260 unsigned long maxnum, unsigned long skip)
262 struct __fdb_entry *fe = buf;
263 int i, num = 0;
264 struct hlist_node *h;
265 struct net_bridge_fdb_entry *f;
267 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
269 rcu_read_lock();
270 for (i = 0; i < BR_HASH_SIZE; i++) {
271 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
272 if (num >= maxnum)
273 goto out;
275 if (has_expired(br, f))
276 continue;
278 if (skip) {
279 --skip;
280 continue;
283 /* convert from internal format to API */
284 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
286 /* due to ABI compat need to split into hi/lo */
287 fe->port_no = f->dst->port_no;
288 fe->port_hi = f->dst->port_no >> 8;
290 fe->is_local = f->is_local;
291 if (!f->is_static)
292 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
293 ++fe;
294 ++num;
298 out:
299 rcu_read_unlock();
301 return num;
304 static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
305 const unsigned char *addr)
307 struct hlist_node *h;
308 struct net_bridge_fdb_entry *fdb;
310 hlist_for_each_entry_rcu(fdb, h, head, hlist) {
311 if (!compare_ether_addr(fdb->addr.addr, addr))
312 return fdb;
314 return NULL;
317 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
318 struct net_bridge_port *source,
319 const unsigned char *addr,
320 int is_local)
322 struct net_bridge_fdb_entry *fdb;
324 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
325 if (fdb) {
326 memcpy(fdb->addr.addr, addr, ETH_ALEN);
327 hlist_add_head_rcu(&fdb->hlist, head);
329 fdb->dst = source;
330 fdb->is_local = is_local;
331 fdb->is_static = is_local;
332 fdb->ageing_timer = jiffies;
334 return fdb;
337 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
338 const unsigned char *addr)
340 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
341 struct net_bridge_fdb_entry *fdb;
343 if (!is_valid_ether_addr(addr))
344 return -EINVAL;
346 fdb = fdb_find(head, addr);
347 if (fdb) {
348 /* it is okay to have multiple ports with same
349 * address, just use the first one.
351 if (fdb->is_local)
352 return 0;
354 printk(KERN_WARNING "%s adding interface with same address "
355 "as a received packet\n",
356 source->dev->name);
357 fdb_delete(fdb);
360 if (!fdb_create(head, source, addr, 1))
361 return -ENOMEM;
363 return 0;
366 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
367 const unsigned char *addr)
369 int ret;
371 spin_lock_bh(&br->hash_lock);
372 ret = fdb_insert(br, source, addr);
373 spin_unlock_bh(&br->hash_lock);
374 return ret;
377 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
378 const unsigned char *addr)
380 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
381 struct net_bridge_fdb_entry *fdb;
383 /* some users want to always flood. */
384 if (hold_time(br) == 0)
385 return;
387 /* ignore packets unless we are using this port */
388 if (!(source->state == BR_STATE_LEARNING ||
389 source->state == BR_STATE_FORWARDING))
390 return;
392 fdb = fdb_find(head, addr);
393 if (likely(fdb)) {
394 /* attempt to update an entry for a local interface */
395 if (unlikely(fdb->is_local)) {
396 if (net_ratelimit())
397 printk(KERN_WARNING "%s: received packet with "
398 "own address as source address\n",
399 source->dev->name);
400 } else {
401 /* fastpath: update of existing entry */
402 fdb->dst = source;
403 fdb->ageing_timer = jiffies;
405 } else {
406 spin_lock(&br->hash_lock);
407 if (!fdb_find(head, addr))
408 fdb_create(head, source, addr, 0);
409 /* else we lose race and someone else inserts
410 * it first, don't bother updating
412 spin_unlock(&br->hash_lock);