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
));
41 rt
->type
= BGP_TABLE_MAIN
;
49 bgp_table_finish (struct bgp_table
**rt
)
55 static struct bgp_node
*
56 bgp_node_create (void)
58 return XCALLOC (MTYPE_BGP_NODE
, sizeof (struct bgp_node
));
61 /* Allocate new route node with prefix set. */
62 static struct bgp_node
*
63 bgp_node_set (struct bgp_table
*table
, struct prefix
*prefix
)
65 struct bgp_node
*node
;
67 node
= bgp_node_create ();
69 prefix_copy (&node
->p
, prefix
);
75 /* Free route node. */
77 bgp_node_free (struct bgp_node
*node
)
79 XFREE (MTYPE_BGP_NODE
, node
);
82 /* Free route table. */
84 bgp_table_free (struct bgp_table
*rt
)
86 struct bgp_node
*tmp_node
;
87 struct bgp_node
*node
;
104 node
= node
->l_right
;
113 if (node
->l_left
== tmp_node
)
116 node
->l_right
= NULL
;
118 bgp_node_free (tmp_node
);
122 bgp_node_free (tmp_node
);
127 XFREE (MTYPE_BGP_TABLE
, rt
);
131 /* Utility mask array. */
132 static u_char maskbit
[] =
134 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
137 /* Common prefix route genaration. */
139 route_common (struct prefix
*n
, struct prefix
*p
, struct prefix
*new)
145 u_char
*np
= (u_char
*)&n
->u
.prefix
;
146 u_char
*pp
= (u_char
*)&p
->u
.prefix
;
147 u_char
*newp
= (u_char
*)&new->u
.prefix
;
149 for (i
= 0; i
< p
->prefixlen
/ 8; i
++)
157 new->prefixlen
= i
* 8;
159 if (new->prefixlen
!= p
->prefixlen
)
161 diff
= np
[i
] ^ pp
[i
];
163 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
))
168 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
172 /* Macro version of check_bit (). */
173 #define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
175 /* Check bit of the prefix. */
177 check_bit (u_char
*prefix
, u_char prefixlen
)
181 u_char
*p
= (u_char
*)prefix
;
183 assert (prefixlen
<= 128);
185 offset
= prefixlen
/ 8;
186 shift
= 7 - (prefixlen
% 8);
188 return (p
[offset
] >> shift
& 1);
191 /* Macro version of set_link (). */
192 #define SET_LINK(X,Y) (X)->link[CHECK_BIT(&(Y)->prefix,(X)->prefixlen)] = (Y);\
196 set_link (struct bgp_node
*node
, struct bgp_node
*new)
200 bit
= check_bit (&new->p
.u
.prefix
, node
->p
.prefixlen
);
202 assert (bit
== 0 || bit
== 1);
204 node
->link
[bit
] = new;
210 bgp_lock_node (struct bgp_node
*node
)
218 bgp_unlock_node (struct bgp_node
*node
)
223 bgp_node_delete (node
);
226 /* Find matched prefix. */
228 bgp_node_match (const struct bgp_table
*table
, struct prefix
*p
)
230 struct bgp_node
*node
;
231 struct bgp_node
*matched
;
236 /* Walk down tree. If there is matched route then store it to
238 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
239 prefix_match (&node
->p
, p
))
243 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
246 /* If matched route found, return it. */
248 return bgp_lock_node (matched
);
254 bgp_node_match_ipv4 (const struct bgp_table
*table
, struct in_addr
*addr
)
256 struct prefix_ipv4 p
;
258 memset (&p
, 0, sizeof (struct prefix_ipv4
));
260 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
263 return bgp_node_match (table
, (struct prefix
*) &p
);
268 bgp_node_match_ipv6 (const struct bgp_table
*table
, struct in6_addr
*addr
)
270 struct prefix_ipv6 p
;
272 memset (&p
, 0, sizeof (struct prefix_ipv6
));
274 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
277 return bgp_node_match (table
, (struct prefix
*) &p
);
279 #endif /* HAVE_IPV6 */
281 /* Lookup same prefix node. Return NULL when we can't find route. */
283 bgp_node_lookup (const struct bgp_table
*table
, struct prefix
*p
)
285 struct bgp_node
*node
;
289 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
290 prefix_match (&node
->p
, p
))
292 if (node
->p
.prefixlen
== p
->prefixlen
&& node
->info
)
293 return bgp_lock_node (node
);
295 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
301 /* Add node to routing table. */
303 bgp_node_get (struct bgp_table
*const table
, struct prefix
*p
)
305 struct bgp_node
*new;
306 struct bgp_node
*node
;
307 struct bgp_node
*match
;
311 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
312 prefix_match (&node
->p
, p
))
314 if (node
->p
.prefixlen
== p
->prefixlen
)
316 bgp_lock_node (node
);
320 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
325 new = bgp_node_set (table
, p
);
327 set_link (match
, new);
333 new = bgp_node_create ();
334 route_common (&node
->p
, p
, &new->p
);
335 new->p
.family
= p
->family
;
337 set_link (new, node
);
340 set_link (match
, new);
344 if (new->p
.prefixlen
!= p
->prefixlen
)
347 new = bgp_node_set (table
, p
);
348 set_link (match
, new);
358 /* Delete node from the routing table. */
360 bgp_node_delete (struct bgp_node
*node
)
362 struct bgp_node
*child
;
363 struct bgp_node
*parent
;
365 assert (node
->lock
== 0);
366 assert (node
->info
== NULL
);
368 if (node
->l_left
&& node
->l_right
)
372 child
= node
->l_left
;
374 child
= node
->l_right
;
376 parent
= node
->parent
;
379 child
->parent
= parent
;
383 if (parent
->l_left
== node
)
384 parent
->l_left
= child
;
386 parent
->l_right
= child
;
389 node
->table
->top
= child
;
391 node
->table
->count
--;
393 bgp_node_free (node
);
395 /* If parent node is stub then delete it also. */
396 if (parent
&& parent
->lock
== 0)
397 bgp_node_delete (parent
);
400 /* Get fist node and lock it. This function is useful when one want
401 to lookup all the node exist in the routing table. */
403 bgp_table_top (const struct bgp_table
*const table
)
405 /* If there is no node in the routing table return NULL. */
406 if (table
->top
== NULL
)
409 /* Lock the top node and return it. */
410 bgp_lock_node (table
->top
);
414 /* Unlock current node and lock next node then return it. */
416 bgp_route_next (struct bgp_node
*node
)
418 struct bgp_node
*next
;
419 struct bgp_node
*start
;
421 /* Node may be deleted from bgp_unlock_node so we have to preserve
422 next node's pointer. */
427 bgp_lock_node (next
);
428 bgp_unlock_node (node
);
433 next
= node
->l_right
;
434 bgp_lock_node (next
);
435 bgp_unlock_node (node
);
442 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
444 next
= node
->parent
->l_right
;
445 bgp_lock_node (next
);
446 bgp_unlock_node (start
);
451 bgp_unlock_node (start
);
455 /* Unlock current node and lock next node until limit. */
457 bgp_route_next_until (struct bgp_node
*node
, struct bgp_node
*limit
)
459 struct bgp_node
*next
;
460 struct bgp_node
*start
;
462 /* Node may be deleted from bgp_unlock_node so we have to preserve
463 next node's pointer. */
468 bgp_lock_node (next
);
469 bgp_unlock_node (node
);
474 next
= node
->l_right
;
475 bgp_lock_node (next
);
476 bgp_unlock_node (node
);
481 while (node
->parent
&& node
!= limit
)
483 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
485 next
= node
->parent
->l_right
;
486 bgp_lock_node (next
);
487 bgp_unlock_node (start
);
492 bgp_unlock_node (start
);
497 bgp_table_count (const struct bgp_table
*table
)