blob: 3385a934278a310132119aeab529e97a74041587 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP routing table
2 Copyright (C) 1998, 2001 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "prefix.h"
24#include "memory.h"
25#include "sockunion.h"
26#include "vty.h"
27
28#include "bgpd/bgpd.h"
29#include "bgpd/bgp_table.h"
30
Paul Jakmab608d5b2008-07-02 02:12:07 +000031static void bgp_node_delete (struct bgp_node *);
32static void bgp_table_free (struct bgp_table *);
paul718e3742002-12-13 20:15:29 +000033
34struct bgp_table *
Paul Jakma64e580a2006-02-21 01:09:01 +000035bgp_table_init (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +000036{
37 struct bgp_table *rt;
38
Stephen Hemminger393deb92008-08-18 14:13:29 -070039 rt = XCALLOC (MTYPE_BGP_TABLE, sizeof (struct bgp_table));
paulfee0f4c2004-09-13 05:12:46 +000040
Chris Caputo228da422009-07-18 05:44:03 +000041 bgp_table_lock(rt);
paulfee0f4c2004-09-13 05:12:46 +000042 rt->type = BGP_TABLE_MAIN;
Paul Jakma64e580a2006-02-21 01:09:01 +000043 rt->afi = afi;
44 rt->safi = safi;
45
paul718e3742002-12-13 20:15:29 +000046 return rt;
47}
48
49void
Chris Caputo228da422009-07-18 05:44:03 +000050bgp_table_lock (struct bgp_table *rt)
51{
52 rt->lock++;
53}
54
55void
56bgp_table_unlock (struct bgp_table *rt)
57{
58 assert (rt->lock > 0);
59 rt->lock--;
60
61 if (rt->lock == 0)
62 bgp_table_free (rt);
63}
64
65void
Paul Jakmab608d5b2008-07-02 02:12:07 +000066bgp_table_finish (struct bgp_table **rt)
paul718e3742002-12-13 20:15:29 +000067{
Chris Caputo228da422009-07-18 05:44:03 +000068 if (*rt != NULL)
69 {
70 bgp_table_unlock(*rt);
71 *rt = NULL;
72 }
paul718e3742002-12-13 20:15:29 +000073}
74
paul94f2b392005-06-28 12:44:16 +000075static struct bgp_node *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080076bgp_node_create (void)
paul718e3742002-12-13 20:15:29 +000077{
Stephen Hemminger393deb92008-08-18 14:13:29 -070078 return XCALLOC (MTYPE_BGP_NODE, sizeof (struct bgp_node));
paul718e3742002-12-13 20:15:29 +000079}
80
81/* Allocate new route node with prefix set. */
paul94f2b392005-06-28 12:44:16 +000082static struct bgp_node *
paul718e3742002-12-13 20:15:29 +000083bgp_node_set (struct bgp_table *table, struct prefix *prefix)
84{
85 struct bgp_node *node;
86
87 node = bgp_node_create ();
88
89 prefix_copy (&node->p, prefix);
90 node->table = table;
91
92 return node;
93}
94
95/* Free route node. */
paul94f2b392005-06-28 12:44:16 +000096static void
paul718e3742002-12-13 20:15:29 +000097bgp_node_free (struct bgp_node *node)
98{
99 XFREE (MTYPE_BGP_NODE, node);
100}
101
102/* Free route table. */
Paul Jakmab608d5b2008-07-02 02:12:07 +0000103static void
paul718e3742002-12-13 20:15:29 +0000104bgp_table_free (struct bgp_table *rt)
105{
106 struct bgp_node *tmp_node;
107 struct bgp_node *node;
108
109 if (rt == NULL)
110 return;
111
112 node = rt->top;
113
Chris Caputo228da422009-07-18 05:44:03 +0000114 /* Bulk deletion of nodes remaining in this table. This function is not
115 called until workers have completed their dependency on this table.
116 A final bgp_unlock_node() will not be called for these nodes. */
paul718e3742002-12-13 20:15:29 +0000117 while (node)
118 {
119 if (node->l_left)
120 {
121 node = node->l_left;
122 continue;
123 }
124
125 if (node->l_right)
126 {
127 node = node->l_right;
128 continue;
129 }
130
131 tmp_node = node;
132 node = node->parent;
133
Chris Caputo228da422009-07-18 05:44:03 +0000134 tmp_node->table->count--;
135 tmp_node->lock = 0; /* to cause assert if unlocked after this */
136 bgp_node_free (tmp_node);
137
paul718e3742002-12-13 20:15:29 +0000138 if (node != NULL)
139 {
140 if (node->l_left == tmp_node)
141 node->l_left = NULL;
142 else
143 node->l_right = NULL;
paul718e3742002-12-13 20:15:29 +0000144 }
145 else
146 {
paul718e3742002-12-13 20:15:29 +0000147 break;
148 }
149 }
150
Chris Caputo228da422009-07-18 05:44:03 +0000151 assert (rt->count == 0);
152
153 if (rt->owner)
154 {
155 peer_unlock (rt->owner);
156 rt->owner = NULL;
157 }
158
paul718e3742002-12-13 20:15:29 +0000159 XFREE (MTYPE_BGP_TABLE, rt);
160 return;
161}
162
163/* Utility mask array. */
164static u_char maskbit[] =
165{
166 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
167};
168
169/* Common prefix route genaration. */
170static void
171route_common (struct prefix *n, struct prefix *p, struct prefix *new)
172{
173 int i;
174 u_char diff;
175 u_char mask;
176
177 u_char *np = (u_char *)&n->u.prefix;
178 u_char *pp = (u_char *)&p->u.prefix;
179 u_char *newp = (u_char *)&new->u.prefix;
180
181 for (i = 0; i < p->prefixlen / 8; i++)
182 {
183 if (np[i] == pp[i])
184 newp[i] = np[i];
185 else
186 break;
187 }
188
189 new->prefixlen = i * 8;
190
191 if (new->prefixlen != p->prefixlen)
192 {
193 diff = np[i] ^ pp[i];
194 mask = 0x80;
195 while (new->prefixlen < p->prefixlen && !(mask & diff))
196 {
197 mask >>= 1;
198 new->prefixlen++;
199 }
200 newp[i] = np[i] & maskbit[new->prefixlen % 8];
201 }
202}
203
paul718e3742002-12-13 20:15:29 +0000204static void
205set_link (struct bgp_node *node, struct bgp_node *new)
206{
Stephen Hemminger1352ef32009-12-09 14:43:17 +0300207 unsigned int bit = prefix_bit (&new->p.u.prefix, node->p.prefixlen);
paul718e3742002-12-13 20:15:29 +0000208
209 node->link[bit] = new;
210 new->parent = node;
211}
212
213/* Lock node. */
214struct bgp_node *
215bgp_lock_node (struct bgp_node *node)
216{
217 node->lock++;
218 return node;
219}
220
221/* Unlock node. */
222void
223bgp_unlock_node (struct bgp_node *node)
224{
Chris Caputo228da422009-07-18 05:44:03 +0000225 assert (node->lock > 0);
paul718e3742002-12-13 20:15:29 +0000226 node->lock--;
227
228 if (node->lock == 0)
229 bgp_node_delete (node);
230}
231
232/* Find matched prefix. */
233struct bgp_node *
Paul Jakma851a1a52008-07-22 19:56:56 +0000234bgp_node_match (const struct bgp_table *table, struct prefix *p)
paul718e3742002-12-13 20:15:29 +0000235{
236 struct bgp_node *node;
237 struct bgp_node *matched;
238
239 matched = NULL;
240 node = table->top;
241
242 /* Walk down tree. If there is matched route then store it to
243 matched. */
244 while (node && node->p.prefixlen <= p->prefixlen &&
245 prefix_match (&node->p, p))
246 {
247 if (node->info)
248 matched = node;
Stephen Hemminger1352ef32009-12-09 14:43:17 +0300249 node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
paul718e3742002-12-13 20:15:29 +0000250 }
251
252 /* If matched route found, return it. */
253 if (matched)
254 return bgp_lock_node (matched);
255
256 return NULL;
257}
258
259struct bgp_node *
Paul Jakma851a1a52008-07-22 19:56:56 +0000260bgp_node_match_ipv4 (const struct bgp_table *table, struct in_addr *addr)
paul718e3742002-12-13 20:15:29 +0000261{
262 struct prefix_ipv4 p;
263
264 memset (&p, 0, sizeof (struct prefix_ipv4));
265 p.family = AF_INET;
266 p.prefixlen = IPV4_MAX_PREFIXLEN;
267 p.prefix = *addr;
268
269 return bgp_node_match (table, (struct prefix *) &p);
270}
271
272#ifdef HAVE_IPV6
273struct bgp_node *
Paul Jakma851a1a52008-07-22 19:56:56 +0000274bgp_node_match_ipv6 (const struct bgp_table *table, struct in6_addr *addr)
paul718e3742002-12-13 20:15:29 +0000275{
276 struct prefix_ipv6 p;
277
278 memset (&p, 0, sizeof (struct prefix_ipv6));
279 p.family = AF_INET6;
280 p.prefixlen = IPV6_MAX_PREFIXLEN;
281 p.prefix = *addr;
282
283 return bgp_node_match (table, (struct prefix *) &p);
284}
285#endif /* HAVE_IPV6 */
286
287/* Lookup same prefix node. Return NULL when we can't find route. */
288struct bgp_node *
Paul Jakma851a1a52008-07-22 19:56:56 +0000289bgp_node_lookup (const struct bgp_table *table, struct prefix *p)
paul718e3742002-12-13 20:15:29 +0000290{
291 struct bgp_node *node;
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000292 u_char prefixlen = p->prefixlen;
293 const u_char *prefix = &p->u.prefix;
paul718e3742002-12-13 20:15:29 +0000294
295 node = table->top;
296
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000297 while (node && node->p.prefixlen <= prefixlen &&
paul718e3742002-12-13 20:15:29 +0000298 prefix_match (&node->p, p))
299 {
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000300 if (node->p.prefixlen == prefixlen && node->info)
paul718e3742002-12-13 20:15:29 +0000301 return bgp_lock_node (node);
302
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000303 node = node->link[prefix_bit(prefix, node->p.prefixlen)];
paul718e3742002-12-13 20:15:29 +0000304 }
305
306 return NULL;
307}
308
309/* Add node to routing table. */
310struct bgp_node *
Paul Jakma851a1a52008-07-22 19:56:56 +0000311bgp_node_get (struct bgp_table *const table, struct prefix *p)
paul718e3742002-12-13 20:15:29 +0000312{
313 struct bgp_node *new;
314 struct bgp_node *node;
315 struct bgp_node *match;
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000316 u_char prefixlen = p->prefixlen;
317 const u_char *prefix = &p->u.prefix;
paul718e3742002-12-13 20:15:29 +0000318
319 match = NULL;
320 node = table->top;
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000321 while (node && node->p.prefixlen <= prefixlen &&
paul718e3742002-12-13 20:15:29 +0000322 prefix_match (&node->p, p))
323 {
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000324 if (node->p.prefixlen == prefixlen)
paul718e3742002-12-13 20:15:29 +0000325 {
326 bgp_lock_node (node);
327 return node;
328 }
329 match = node;
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000330 node = node->link[prefix_bit(prefix, node->p.prefixlen)];
paul718e3742002-12-13 20:15:29 +0000331 }
332
333 if (node == NULL)
334 {
335 new = bgp_node_set (table, p);
336 if (match)
337 set_link (match, new);
338 else
339 table->top = new;
340 }
341 else
342 {
343 new = bgp_node_create ();
344 route_common (&node->p, p, &new->p);
345 new->p.family = p->family;
346 new->table = table;
347 set_link (new, node);
348
349 if (match)
350 set_link (match, new);
351 else
352 table->top = new;
353
Jorge Boncompte [DTI2]47d3b602012-05-07 16:53:11 +0000354 if (new->p.prefixlen != prefixlen)
paul718e3742002-12-13 20:15:29 +0000355 {
356 match = new;
357 new = bgp_node_set (table, p);
358 set_link (match, new);
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000359 table->count++;
paul718e3742002-12-13 20:15:29 +0000360 }
361 }
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000362 table->count++;
paul718e3742002-12-13 20:15:29 +0000363 bgp_lock_node (new);
364
365 return new;
366}
367
368/* Delete node from the routing table. */
Paul Jakmab608d5b2008-07-02 02:12:07 +0000369static void
paul718e3742002-12-13 20:15:29 +0000370bgp_node_delete (struct bgp_node *node)
371{
372 struct bgp_node *child;
373 struct bgp_node *parent;
374
375 assert (node->lock == 0);
376 assert (node->info == NULL);
377
378 if (node->l_left && node->l_right)
379 return;
380
381 if (node->l_left)
382 child = node->l_left;
383 else
384 child = node->l_right;
385
386 parent = node->parent;
387
388 if (child)
389 child->parent = parent;
390
391 if (parent)
392 {
393 if (parent->l_left == node)
394 parent->l_left = child;
395 else
396 parent->l_right = child;
397 }
398 else
399 node->table->top = child;
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000400
401 node->table->count--;
402
paul718e3742002-12-13 20:15:29 +0000403 bgp_node_free (node);
404
405 /* If parent node is stub then delete it also. */
406 if (parent && parent->lock == 0)
407 bgp_node_delete (parent);
408}
409
410/* Get fist node and lock it. This function is useful when one want
411 to lookup all the node exist in the routing table. */
412struct bgp_node *
Paul Jakma851a1a52008-07-22 19:56:56 +0000413bgp_table_top (const struct bgp_table *const table)
paul718e3742002-12-13 20:15:29 +0000414{
415 /* If there is no node in the routing table return NULL. */
416 if (table->top == NULL)
417 return NULL;
418
419 /* Lock the top node and return it. */
420 bgp_lock_node (table->top);
421 return table->top;
422}
423
424/* Unlock current node and lock next node then return it. */
425struct bgp_node *
426bgp_route_next (struct bgp_node *node)
427{
428 struct bgp_node *next;
429 struct bgp_node *start;
430
431 /* Node may be deleted from bgp_unlock_node so we have to preserve
432 next node's pointer. */
433
434 if (node->l_left)
435 {
436 next = node->l_left;
437 bgp_lock_node (next);
438 bgp_unlock_node (node);
439 return next;
440 }
441 if (node->l_right)
442 {
443 next = node->l_right;
444 bgp_lock_node (next);
445 bgp_unlock_node (node);
446 return next;
447 }
448
449 start = node;
450 while (node->parent)
451 {
452 if (node->parent->l_left == node && node->parent->l_right)
453 {
454 next = node->parent->l_right;
455 bgp_lock_node (next);
456 bgp_unlock_node (start);
457 return next;
458 }
459 node = node->parent;
460 }
461 bgp_unlock_node (start);
462 return NULL;
463}
464
465/* Unlock current node and lock next node until limit. */
466struct bgp_node *
467bgp_route_next_until (struct bgp_node *node, struct bgp_node *limit)
468{
469 struct bgp_node *next;
470 struct bgp_node *start;
471
472 /* Node may be deleted from bgp_unlock_node so we have to preserve
473 next node's pointer. */
474
475 if (node->l_left)
476 {
477 next = node->l_left;
478 bgp_lock_node (next);
479 bgp_unlock_node (node);
480 return next;
481 }
482 if (node->l_right)
483 {
484 next = node->l_right;
485 bgp_lock_node (next);
486 bgp_unlock_node (node);
487 return next;
488 }
489
490 start = node;
491 while (node->parent && node != limit)
492 {
493 if (node->parent->l_left == node && node->parent->l_right)
494 {
495 next = node->parent->l_right;
496 bgp_lock_node (next);
497 bgp_unlock_node (start);
498 return next;
499 }
500 node = node->parent;
501 }
502 bgp_unlock_node (start);
503 return NULL;
504}
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000505
506unsigned long
Paul Jakma851a1a52008-07-22 19:56:56 +0000507bgp_table_count (const struct bgp_table *table)
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000508{
509 return table->count;
510}