2 Copyright (C) 1998, 2001 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GNU Zebra is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include "sockunion.h"
28 #include "bgpd/bgpd.h"
29 #include "bgpd/bgp_table.h"
31 static void bgp_node_delete (struct bgp_node
*);
32 static void bgp_table_free (struct bgp_table
*);
35 bgp_table_init (afi_t afi
, safi_t safi
)
39 rt
= XCALLOC (MTYPE_BGP_TABLE
, sizeof (struct bgp_table
));
42 rt
->type
= BGP_TABLE_MAIN
;
50 bgp_table_lock (struct bgp_table
*rt
)
56 bgp_table_unlock (struct bgp_table
*rt
)
58 assert (rt
->lock
> 0);
66 bgp_table_finish (struct bgp_table
**rt
)
70 bgp_table_unlock(*rt
);
75 static struct bgp_node
*
76 bgp_node_create (void)
78 return XCALLOC (MTYPE_BGP_NODE
, sizeof (struct bgp_node
));
81 /* Allocate new route node with prefix set. */
82 static struct bgp_node
*
83 bgp_node_set (struct bgp_table
*table
, struct prefix
*prefix
)
85 struct bgp_node
*node
;
87 node
= bgp_node_create ();
89 prefix_copy (&node
->p
, prefix
);
95 /* Free route node. */
97 bgp_node_free (struct bgp_node
*node
)
99 XFREE (MTYPE_BGP_NODE
, node
);
102 /* Free route table. */
104 bgp_table_free (struct bgp_table
*rt
)
106 struct bgp_node
*tmp_node
;
107 struct bgp_node
*node
;
114 /* 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. */
127 node
= node
->l_right
;
134 tmp_node
->table
->count
--;
135 tmp_node
->lock
= 0; /* to cause assert if unlocked after this */
136 bgp_node_free (tmp_node
);
140 if (node
->l_left
== tmp_node
)
143 node
->l_right
= NULL
;
151 assert (rt
->count
== 0);
155 peer_unlock (rt
->owner
);
159 XFREE (MTYPE_BGP_TABLE
, rt
);
163 /* Utility mask array. */
164 static u_char maskbit
[] =
166 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
169 /* Common prefix route genaration. */
171 route_common (struct prefix
*n
, struct prefix
*p
, struct prefix
*new)
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
;
181 for (i
= 0; i
< p
->prefixlen
/ 8; i
++)
189 new->prefixlen
= i
* 8;
191 if (new->prefixlen
!= p
->prefixlen
)
193 diff
= np
[i
] ^ pp
[i
];
195 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
))
200 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
204 /* Macro version of check_bit (). */
205 #define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
207 /* Check bit of the prefix. */
209 check_bit (u_char
*prefix
, u_char prefixlen
)
213 u_char
*p
= (u_char
*)prefix
;
215 assert (prefixlen
<= 128);
217 offset
= prefixlen
/ 8;
218 shift
= 7 - (prefixlen
% 8);
220 return (p
[offset
] >> shift
& 1);
223 /* Macro version of set_link (). */
224 #define SET_LINK(X,Y) (X)->link[CHECK_BIT(&(Y)->prefix,(X)->prefixlen)] = (Y);\
228 set_link (struct bgp_node
*node
, struct bgp_node
*new)
232 bit
= check_bit (&new->p
.u
.prefix
, node
->p
.prefixlen
);
234 assert (bit
== 0 || bit
== 1);
236 node
->link
[bit
] = new;
242 bgp_lock_node (struct bgp_node
*node
)
250 bgp_unlock_node (struct bgp_node
*node
)
252 assert (node
->lock
> 0);
256 bgp_node_delete (node
);
259 /* Find matched prefix. */
261 bgp_node_match (const struct bgp_table
*table
, struct prefix
*p
)
263 struct bgp_node
*node
;
264 struct bgp_node
*matched
;
269 /* Walk down tree. If there is matched route then store it to
271 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
272 prefix_match (&node
->p
, p
))
276 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
279 /* If matched route found, return it. */
281 return bgp_lock_node (matched
);
287 bgp_node_match_ipv4 (const struct bgp_table
*table
, struct in_addr
*addr
)
289 struct prefix_ipv4 p
;
291 memset (&p
, 0, sizeof (struct prefix_ipv4
));
293 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
296 return bgp_node_match (table
, (struct prefix
*) &p
);
301 bgp_node_match_ipv6 (const struct bgp_table
*table
, struct in6_addr
*addr
)
303 struct prefix_ipv6 p
;
305 memset (&p
, 0, sizeof (struct prefix_ipv6
));
307 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
310 return bgp_node_match (table
, (struct prefix
*) &p
);
312 #endif /* HAVE_IPV6 */
314 /* Lookup same prefix node. Return NULL when we can't find route. */
316 bgp_node_lookup (const struct bgp_table
*table
, struct prefix
*p
)
318 struct bgp_node
*node
;
322 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
323 prefix_match (&node
->p
, p
))
325 if (node
->p
.prefixlen
== p
->prefixlen
&& node
->info
)
326 return bgp_lock_node (node
);
328 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
334 /* Add node to routing table. */
336 bgp_node_get (struct bgp_table
*const table
, struct prefix
*p
)
338 struct bgp_node
*new;
339 struct bgp_node
*node
;
340 struct bgp_node
*match
;
344 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
345 prefix_match (&node
->p
, p
))
347 if (node
->p
.prefixlen
== p
->prefixlen
)
349 bgp_lock_node (node
);
353 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
358 new = bgp_node_set (table
, p
);
360 set_link (match
, new);
366 new = bgp_node_create ();
367 route_common (&node
->p
, p
, &new->p
);
368 new->p
.family
= p
->family
;
370 set_link (new, node
);
373 set_link (match
, new);
377 if (new->p
.prefixlen
!= p
->prefixlen
)
380 bgp_lock_node (match
);
381 new = bgp_node_set (table
, p
);
382 set_link (match
, new);
392 /* Delete node from the routing table. */
394 bgp_node_delete (struct bgp_node
*node
)
396 struct bgp_node
*child
;
397 struct bgp_node
*parent
;
399 assert (node
->lock
== 0);
400 assert (node
->info
== NULL
);
402 if (node
->l_left
&& node
->l_right
)
406 child
= node
->l_left
;
408 child
= node
->l_right
;
410 parent
= node
->parent
;
413 child
->parent
= parent
;
417 if (parent
->l_left
== node
)
418 parent
->l_left
= child
;
420 parent
->l_right
= child
;
423 node
->table
->top
= child
;
425 node
->table
->count
--;
427 bgp_node_free (node
);
429 /* If parent node is stub then delete it also. */
430 if (parent
&& parent
->lock
== 0)
431 bgp_node_delete (parent
);
434 /* Get fist node and lock it. This function is useful when one want
435 to lookup all the node exist in the routing table. */
437 bgp_table_top (const struct bgp_table
*const table
)
439 /* If there is no node in the routing table return NULL. */
440 if (table
->top
== NULL
)
443 /* Lock the top node and return it. */
444 bgp_lock_node (table
->top
);
448 /* Unlock current node and lock next node then return it. */
450 bgp_route_next (struct bgp_node
*node
)
452 struct bgp_node
*next
;
453 struct bgp_node
*start
;
455 /* Node may be deleted from bgp_unlock_node so we have to preserve
456 next node's pointer. */
461 bgp_lock_node (next
);
462 bgp_unlock_node (node
);
467 next
= node
->l_right
;
468 bgp_lock_node (next
);
469 bgp_unlock_node (node
);
476 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
478 next
= node
->parent
->l_right
;
479 bgp_lock_node (next
);
480 bgp_unlock_node (start
);
485 bgp_unlock_node (start
);
489 /* Unlock current node and lock next node until limit. */
491 bgp_route_next_until (struct bgp_node
*node
, struct bgp_node
*limit
)
493 struct bgp_node
*next
;
494 struct bgp_node
*start
;
496 /* Node may be deleted from bgp_unlock_node so we have to preserve
497 next node's pointer. */
502 bgp_lock_node (next
);
503 bgp_unlock_node (node
);
508 next
= node
->l_right
;
509 bgp_lock_node (next
);
510 bgp_unlock_node (node
);
515 while (node
->parent
&& node
!= limit
)
517 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
519 next
= node
->parent
->l_right
;
520 bgp_lock_node (next
);
521 bgp_unlock_node (start
);
526 bgp_unlock_node (start
);
531 bgp_table_count (const struct bgp_table
*table
)