paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP routing table |
| 2 | Copyright (C) 1998, 2001 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 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 |
| 9 | later version. |
| 10 | |
| 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. |
| 15 | |
| 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 |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "prefix.h" |
| 24 | #include "memory.h" |
| 25 | #include "sockunion.h" |
| 26 | #include "vty.h" |
Donald Sharp | 0490729 | 2016-01-07 10:03:01 -0500 | [diff] [blame] | 27 | #include "filter.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | |
| 29 | #include "bgpd/bgpd.h" |
| 30 | #include "bgpd/bgp_table.h" |
| 31 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 32 | void |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 33 | bgp_table_lock (struct bgp_table *rt) |
| 34 | { |
| 35 | rt->lock++; |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | bgp_table_unlock (struct bgp_table *rt) |
| 40 | { |
| 41 | assert (rt->lock > 0); |
| 42 | rt->lock--; |
| 43 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 44 | if (rt->lock != 0) |
| 45 | { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | route_table_finish (rt->route_table); |
| 50 | rt->route_table = NULL; |
| 51 | |
| 52 | if (rt->owner) |
| 53 | { |
| 54 | peer_unlock (rt->owner); |
| 55 | rt->owner = NULL; |
| 56 | } |
| 57 | |
| 58 | XFREE (MTYPE_BGP_TABLE, rt); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
Paul Jakma | b608d5b | 2008-07-02 02:12:07 +0000 | [diff] [blame] | 62 | bgp_table_finish (struct bgp_table **rt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | { |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 64 | if (*rt != NULL) |
| 65 | { |
| 66 | bgp_table_unlock(*rt); |
| 67 | *rt = NULL; |
| 68 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 71 | /* |
| 72 | * bgp_node_create |
| 73 | */ |
| 74 | static struct route_node * |
| 75 | bgp_node_create (route_table_delegate_t *delegate, struct route_table *table) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | { |
| 77 | struct bgp_node *node; |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 78 | node = XCALLOC (MTYPE_BGP_NODE, sizeof (struct bgp_node)); |
| 79 | return bgp_node_to_rnode (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 82 | /* |
| 83 | * bgp_node_destroy |
| 84 | */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 85 | static void |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 86 | bgp_node_destroy (route_table_delegate_t *delegate, |
| 87 | struct route_table *table, struct route_node *node) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | { |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 89 | struct bgp_node *bgp_node; |
| 90 | bgp_node = bgp_node_from_rnode (node); |
| 91 | XFREE (MTYPE_BGP_NODE, bgp_node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 94 | /* |
| 95 | * Function vector to customize the behavior of the route table |
| 96 | * library for BGP route tables. |
| 97 | */ |
| 98 | route_table_delegate_t bgp_table_delegate = { |
| 99 | .create_node = bgp_node_create, |
| 100 | .destroy_node = bgp_node_destroy |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 103 | /* |
| 104 | * bgp_table_init |
| 105 | */ |
| 106 | struct bgp_table * |
| 107 | bgp_table_init (afi_t afi, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | { |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 109 | struct bgp_table *rt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 110 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 111 | rt = XCALLOC (MTYPE_BGP_TABLE, sizeof (struct bgp_table)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 112 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 113 | rt->route_table = route_table_init_with_delegate (&bgp_table_delegate); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 115 | /* |
| 116 | * Set up back pointer to bgp_table. |
| 117 | */ |
| 118 | rt->route_table->info = rt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 120 | bgp_table_lock (rt); |
| 121 | rt->type = BGP_TABLE_MAIN; |
| 122 | rt->afi = afi; |
| 123 | rt->safi = safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | |
Avneesh Sachdev | 6717404 | 2012-08-17 08:19:49 -0700 | [diff] [blame] | 125 | return rt; |
Paul Jakma | cbdfbaa | 2006-03-30 13:20:48 +0000 | [diff] [blame] | 126 | } |