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 void bgp_node_delete (struct bgp_node
*);
32 void bgp_table_free (struct bgp_table
*);
35 bgp_table_init (afi_t afi
, safi_t safi
)
39 rt
= XMALLOC (MTYPE_BGP_TABLE
, sizeof (struct bgp_table
));
40 memset (rt
, 0, sizeof (struct bgp_table
));
42 rt
->type
= BGP_TABLE_MAIN
;
50 bgp_table_finish (struct bgp_table
*rt
)
55 static struct bgp_node
*
60 rn
= (struct bgp_node
*) XMALLOC (MTYPE_BGP_NODE
, sizeof (struct bgp_node
));
61 memset (rn
, 0, sizeof (struct bgp_node
));
65 /* Allocate new route node with prefix set. */
66 static struct bgp_node
*
67 bgp_node_set (struct bgp_table
*table
, struct prefix
*prefix
)
69 struct bgp_node
*node
;
71 node
= bgp_node_create ();
73 prefix_copy (&node
->p
, prefix
);
79 /* Free route node. */
81 bgp_node_free (struct bgp_node
*node
)
83 XFREE (MTYPE_BGP_NODE
, node
);
86 /* Free route table. */
88 bgp_table_free (struct bgp_table
*rt
)
90 struct bgp_node
*tmp_node
;
91 struct bgp_node
*node
;
108 node
= node
->l_right
;
117 if (node
->l_left
== tmp_node
)
120 node
->l_right
= NULL
;
122 bgp_node_free (tmp_node
);
126 bgp_node_free (tmp_node
);
131 XFREE (MTYPE_BGP_TABLE
, rt
);
135 /* Utility mask array. */
136 static u_char maskbit
[] =
138 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
141 /* Common prefix route genaration. */
143 route_common (struct prefix
*n
, struct prefix
*p
, struct prefix
*new)
149 u_char
*np
= (u_char
*)&n
->u
.prefix
;
150 u_char
*pp
= (u_char
*)&p
->u
.prefix
;
151 u_char
*newp
= (u_char
*)&new->u
.prefix
;
153 for (i
= 0; i
< p
->prefixlen
/ 8; i
++)
161 new->prefixlen
= i
* 8;
163 if (new->prefixlen
!= p
->prefixlen
)
165 diff
= np
[i
] ^ pp
[i
];
167 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
))
172 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
176 /* Macro version of check_bit (). */
177 #define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
179 /* Check bit of the prefix. */
181 check_bit (u_char
*prefix
, u_char prefixlen
)
185 u_char
*p
= (u_char
*)prefix
;
187 assert (prefixlen
<= 128);
189 offset
= prefixlen
/ 8;
190 shift
= 7 - (prefixlen
% 8);
192 return (p
[offset
] >> shift
& 1);
195 /* Macro version of set_link (). */
196 #define SET_LINK(X,Y) (X)->link[CHECK_BIT(&(Y)->prefix,(X)->prefixlen)] = (Y);\
200 set_link (struct bgp_node
*node
, struct bgp_node
*new)
204 bit
= check_bit (&new->p
.u
.prefix
, node
->p
.prefixlen
);
206 assert (bit
== 0 || bit
== 1);
208 node
->link
[bit
] = new;
214 bgp_lock_node (struct bgp_node
*node
)
222 bgp_unlock_node (struct bgp_node
*node
)
227 bgp_node_delete (node
);
230 /* Find matched prefix. */
232 bgp_node_match (struct bgp_table
*table
, struct prefix
*p
)
234 struct bgp_node
*node
;
235 struct bgp_node
*matched
;
240 /* Walk down tree. If there is matched route then store it to
242 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
243 prefix_match (&node
->p
, p
))
247 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
250 /* If matched route found, return it. */
252 return bgp_lock_node (matched
);
258 bgp_node_match_ipv4 (struct bgp_table
*table
, struct in_addr
*addr
)
260 struct prefix_ipv4 p
;
262 memset (&p
, 0, sizeof (struct prefix_ipv4
));
264 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
267 return bgp_node_match (table
, (struct prefix
*) &p
);
272 bgp_node_match_ipv6 (struct bgp_table
*table
, struct in6_addr
*addr
)
274 struct prefix_ipv6 p
;
276 memset (&p
, 0, sizeof (struct prefix_ipv6
));
278 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
281 return bgp_node_match (table
, (struct prefix
*) &p
);
283 #endif /* HAVE_IPV6 */
285 /* Lookup same prefix node. Return NULL when we can't find route. */
287 bgp_node_lookup (struct bgp_table
*table
, struct prefix
*p
)
289 struct bgp_node
*node
;
293 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
294 prefix_match (&node
->p
, p
))
296 if (node
->p
.prefixlen
== p
->prefixlen
&& node
->info
)
297 return bgp_lock_node (node
);
299 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
305 /* Add node to routing table. */
307 bgp_node_get (struct bgp_table
*table
, struct prefix
*p
)
309 struct bgp_node
*new;
310 struct bgp_node
*node
;
311 struct bgp_node
*match
;
315 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
316 prefix_match (&node
->p
, p
))
318 if (node
->p
.prefixlen
== p
->prefixlen
)
320 bgp_lock_node (node
);
324 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
329 new = bgp_node_set (table
, p
);
331 set_link (match
, new);
337 new = bgp_node_create ();
338 route_common (&node
->p
, p
, &new->p
);
339 new->p
.family
= p
->family
;
341 set_link (new, node
);
344 set_link (match
, new);
348 if (new->p
.prefixlen
!= p
->prefixlen
)
351 new = bgp_node_set (table
, p
);
352 set_link (match
, new);
362 /* Delete node from the routing table. */
364 bgp_node_delete (struct bgp_node
*node
)
366 struct bgp_node
*child
;
367 struct bgp_node
*parent
;
369 assert (node
->lock
== 0);
370 assert (node
->info
== NULL
);
372 if (node
->l_left
&& node
->l_right
)
376 child
= node
->l_left
;
378 child
= node
->l_right
;
380 parent
= node
->parent
;
383 child
->parent
= parent
;
387 if (parent
->l_left
== node
)
388 parent
->l_left
= child
;
390 parent
->l_right
= child
;
393 node
->table
->top
= child
;
395 node
->table
->count
--;
397 bgp_node_free (node
);
399 /* If parent node is stub then delete it also. */
400 if (parent
&& parent
->lock
== 0)
401 bgp_node_delete (parent
);
404 /* Get fist node and lock it. This function is useful when one want
405 to lookup all the node exist in the routing table. */
407 bgp_table_top (struct bgp_table
*table
)
409 /* If there is no node in the routing table return NULL. */
410 if (table
->top
== NULL
)
413 /* Lock the top node and return it. */
414 bgp_lock_node (table
->top
);
418 /* Unlock current node and lock next node then return it. */
420 bgp_route_next (struct bgp_node
*node
)
422 struct bgp_node
*next
;
423 struct bgp_node
*start
;
425 /* Node may be deleted from bgp_unlock_node so we have to preserve
426 next node's pointer. */
431 bgp_lock_node (next
);
432 bgp_unlock_node (node
);
437 next
= node
->l_right
;
438 bgp_lock_node (next
);
439 bgp_unlock_node (node
);
446 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
448 next
= node
->parent
->l_right
;
449 bgp_lock_node (next
);
450 bgp_unlock_node (start
);
455 bgp_unlock_node (start
);
459 /* Unlock current node and lock next node until limit. */
461 bgp_route_next_until (struct bgp_node
*node
, struct bgp_node
*limit
)
463 struct bgp_node
*next
;
464 struct bgp_node
*start
;
466 /* Node may be deleted from bgp_unlock_node so we have to preserve
467 next node's pointer. */
472 bgp_lock_node (next
);
473 bgp_unlock_node (node
);
478 next
= node
->l_right
;
479 bgp_lock_node (next
);
480 bgp_unlock_node (node
);
485 while (node
->parent
&& node
!= limit
)
487 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
489 next
= node
->parent
->l_right
;
490 bgp_lock_node (next
);
491 bgp_unlock_node (start
);
496 bgp_unlock_node (start
);
501 bgp_table_count (struct bgp_table
*table
)