xorp

bgp_trie.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/bgp/bgp_trie.hh,v 1.22 2008/11/08 06:14:36 mjh Exp $
00021 
00022 #ifndef __BGP_BGP_TRIE_HH__
00023 #define __BGP_BGP_TRIE_HH__
00024 
00025 #include "subnet_route.hh"
00026 
00027 #include "libxorp/ref_trie.hh"
00028 
00029 template <class A>
00030 class Path_Att_Ptr_Cmp {
00031 public:
00032     bool operator() (const PAListRef<A> a,
00033              const PAListRef<A> b) const {
00034     return a < b;
00035     }
00036 };
00037 
00038 template<class A>
00039 class ChainedSubnetRoute : public SubnetRoute<A> {
00040 public:
00041     ChainedSubnetRoute(const IPNet<A> &net,
00042                const PAListRef<A> attributes) :
00043     SubnetRoute<A>(net, attributes), _prev(0), _next(0) {}
00044 
00045     ChainedSubnetRoute(const SubnetRoute<A>& route,
00046                const ChainedSubnetRoute<A>* prev);
00047 
00048     ChainedSubnetRoute(const ChainedSubnetRoute& csr);
00049 
00050     const ChainedSubnetRoute<A> *prev() const { return _prev; }
00051     const ChainedSubnetRoute<A> *next() const { return _next; }
00052 
00053     bool unchain() const;
00054 
00055 protected:
00056     void set_next(const ChainedSubnetRoute<A> *next) const { _next = next; }
00057 
00058     void set_prev(const ChainedSubnetRoute<A> *prev) const { _prev = prev; }
00059 
00060     ChainedSubnetRoute& operator=(const ChainedSubnetRoute& csr); // Not impl.
00061 
00062 private:
00063     //The destructor is private because it's not supposed to be called
00064     //directly. Instead, unref() should be used which will only delete
00065     //the SubnetRoute when the reference count reaches zero.
00066     friend class SubnetRoute<A>; //shut the compiler up.
00067     ~ChainedSubnetRoute() {}
00068 
00069     // it looks odd to have these be mutable and the methods to set
00070     // them be const, but that's because the chaining is really
00071     // conceptually part of the container, not the payload.  It these
00072     // aren't mutable we can't modify the chaining and have the payload
00073     // be const.
00074     mutable const ChainedSubnetRoute<A> *_prev;
00075     mutable const ChainedSubnetRoute<A> *_next;
00076 };
00077 
00083 template<>
00084 inline void
00085 RefTrieNode<IPv4, const ChainedSubnetRoute<IPv4> >::delete_payload(const ChainedSubnetRoute<IPv4>* p)
00086 {
00087     p->unref();
00088 }
00089 
00090 template<>
00091 inline void
00092 RefTrieNode<IPv6, const ChainedSubnetRoute<IPv6> >::delete_payload(const ChainedSubnetRoute<IPv6>* p)
00093 {
00094     p->unref();
00095 }
00096 
00112 template<class A>
00113 class BgpTrie : public RefTrie<A, const ChainedSubnetRoute<A> > {
00114 public:
00115     typedef ::IPNet<A> IPNet;
00116     typedef ::ChainedSubnetRoute<A> ChainedSubnetRoute;
00117     typedef map<const PAListRef<A>,
00118         const ChainedSubnetRoute*,
00119         Path_Att_Ptr_Cmp<A> > PathmapType;
00120     typedef RefTrie<A, const ChainedSubnetRoute> RouteTrie;
00121     typedef typename RouteTrie::iterator iterator;
00122 
00123     BgpTrie();
00124     ~BgpTrie();
00125 
00126     iterator insert(const IPNet& net, const SubnetRoute<A>& route);
00127 
00128     void erase(const IPNet& net);
00129 
00130     void delete_all_nodes();
00131 
00132     const PathmapType& pathmap() const { return _pathmap; }
00133 
00134 private:
00135     PathmapType _pathmap;
00136 };
00137 
00138 #endif // __BGP_BGP_TRIE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations