blob: 7a6c675dc28c17ca5f7a11fc95dfcbe13eb87aea [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
paul718e3742002-12-13 20:15:29 +000031void
Chris Caputo228da422009-07-18 05:44:03 +000032bgp_table_lock (struct bgp_table *rt)
33{
34 rt->lock++;
35}
36
37void
38bgp_table_unlock (struct bgp_table *rt)
39{
40 assert (rt->lock > 0);
41 rt->lock--;
42
Avneesh Sachdev67174042012-08-17 08:19:49 -070043 if (rt->lock != 0)
44 {
45 return;
46 }
47
48 route_table_finish (rt->route_table);
49 rt->route_table = NULL;
50
51 if (rt->owner)
52 {
53 peer_unlock (rt->owner);
54 rt->owner = NULL;
55 }
56
57 XFREE (MTYPE_BGP_TABLE, rt);
Chris Caputo228da422009-07-18 05:44:03 +000058}
59
60void
Paul Jakmab608d5b2008-07-02 02:12:07 +000061bgp_table_finish (struct bgp_table **rt)
paul718e3742002-12-13 20:15:29 +000062{
Chris Caputo228da422009-07-18 05:44:03 +000063 if (*rt != NULL)
64 {
65 bgp_table_unlock(*rt);
66 *rt = NULL;
67 }
paul718e3742002-12-13 20:15:29 +000068}
69
Avneesh Sachdev67174042012-08-17 08:19:49 -070070/*
71 * bgp_node_create
72 */
73static struct route_node *
74bgp_node_create (route_table_delegate_t *delegate, struct route_table *table)
paul718e3742002-12-13 20:15:29 +000075{
76 struct bgp_node *node;
Avneesh Sachdev67174042012-08-17 08:19:49 -070077 node = XCALLOC (MTYPE_BGP_NODE, sizeof (struct bgp_node));
78 return bgp_node_to_rnode (node);
paul718e3742002-12-13 20:15:29 +000079}
80
Avneesh Sachdev67174042012-08-17 08:19:49 -070081/*
82 * bgp_node_destroy
83 */
paul94f2b392005-06-28 12:44:16 +000084static void
Avneesh Sachdev67174042012-08-17 08:19:49 -070085bgp_node_destroy (route_table_delegate_t *delegate,
86 struct route_table *table, struct route_node *node)
paul718e3742002-12-13 20:15:29 +000087{
Avneesh Sachdev67174042012-08-17 08:19:49 -070088 struct bgp_node *bgp_node;
89 bgp_node = bgp_node_from_rnode (node);
90 XFREE (MTYPE_BGP_NODE, bgp_node);
paul718e3742002-12-13 20:15:29 +000091}
92
Avneesh Sachdev67174042012-08-17 08:19:49 -070093/*
94 * Function vector to customize the behavior of the route table
95 * library for BGP route tables.
96 */
97route_table_delegate_t bgp_table_delegate = {
98 .create_node = bgp_node_create,
99 .destroy_node = bgp_node_destroy
paul718e3742002-12-13 20:15:29 +0000100};
101
Avneesh Sachdev67174042012-08-17 08:19:49 -0700102/*
103 * bgp_table_init
104 */
105struct bgp_table *
106bgp_table_init (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000107{
Avneesh Sachdev67174042012-08-17 08:19:49 -0700108 struct bgp_table *rt;
paul718e3742002-12-13 20:15:29 +0000109
Avneesh Sachdev67174042012-08-17 08:19:49 -0700110 rt = XCALLOC (MTYPE_BGP_TABLE, sizeof (struct bgp_table));
paul718e3742002-12-13 20:15:29 +0000111
Avneesh Sachdev67174042012-08-17 08:19:49 -0700112 rt->route_table = route_table_init_with_delegate (&bgp_table_delegate);
paul718e3742002-12-13 20:15:29 +0000113
Avneesh Sachdev67174042012-08-17 08:19:49 -0700114 /*
115 * Set up back pointer to bgp_table.
116 */
117 rt->route_table->info = rt;
paul718e3742002-12-13 20:15:29 +0000118
Avneesh Sachdev67174042012-08-17 08:19:49 -0700119 bgp_table_lock (rt);
120 rt->type = BGP_TABLE_MAIN;
121 rt->afi = afi;
122 rt->safi = safi;
paul718e3742002-12-13 20:15:29 +0000123
Avneesh Sachdev67174042012-08-17 08:19:49 -0700124 return rt;
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000125}