xorp

route.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 // 
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/rib/route.hh,v 1.29 2008/10/02 21:58:12 bms Exp $
00022 
00023 #ifndef __RIB_ROUTE_HH__
00024 #define __RIB_ROUTE_HH__
00025 
00026 
00027 
00028 
00029 #include "libxorp/xorp.h"
00030 #include "libxorp/ipv4net.hh"
00031 #include "libxorp/ipv6net.hh"
00032 #include "libxorp/vif.hh"
00033 #include "libxorp/nexthop.hh"
00034 
00035 #include "policy/backend/policytags.hh"
00036 
00037 #include "protocol.hh"
00038 
00039 class RibVif;
00040 
00047 class RouteEntry {
00048 public:
00059     RouteEntry(RibVif* vif, NextHop* nexthop, const Protocol& protocol,
00060            uint32_t metric);
00061 
00065     virtual ~RouteEntry();
00066 
00073     RibVif* vif() const { return _vif; }
00074 
00081     NextHop* nexthop() const { return _nexthop; }
00082 
00088     void set_nexthop(NextHop* v) { _nexthop = v; }
00089 
00099     uint16_t admin_distance() const { return _admin_distance; }
00100 
00106     void set_admin_distance(uint16_t ad) { _admin_distance = ad; }
00107 
00114     const Protocol& protocol() const { return _protocol; }
00115 
00119     virtual string str() const = 0;
00120 
00126     void set_metric(uint32_t metric) { _metric = metric; }
00127 
00133     uint32_t metric() const { return _metric; }
00134 
00135 protected:
00136     RibVif*     _vif;
00137     NextHop*        _nexthop;       // The next-hop router
00138     // The routing protocol that instantiated this route
00139     const Protocol& _protocol;
00140     uint16_t        _admin_distance;    // Lower is better
00141     uint32_t        _metric;        // Lower is better
00142 };
00143 
00151 template <class A>
00152 class IPRouteEntry : public RouteEntry {
00153 public:
00165     IPRouteEntry(const IPNet<A>& net, RibVif* vif, NextHop* nexthop,
00166          const Protocol& protocol, uint32_t metric)
00167     : RouteEntry(vif, nexthop, protocol, metric), _net(net) {}
00168 
00181     IPRouteEntry(const IPNet<A>& net, RibVif* vif, NextHop* nexthop,
00182          const Protocol& protocol, uint32_t metric,
00183          const PolicyTags& policytags)
00184     : RouteEntry(vif, nexthop, protocol, metric), _net(net),
00185       _policytags(policytags) {}
00186 
00190     ~IPRouteEntry() {}
00191 
00197     const IPNet<A>& net() const { return _net; }
00198 
00204     size_t prefix_len() const { return _net.prefix_len(); }
00205 
00212     const A& nexthop_addr() const {
00213     IPNextHop<A>* nh = reinterpret_cast<IPNextHop<A>* >(nexthop());
00214     if (nh != NULL)
00215         return nh->addr();
00216     else {
00217         return A::ZERO();
00218     }
00219     }
00220 
00226     PolicyTags& policytags() { return _policytags; }
00227     const PolicyTags& policytags() const { return _policytags; }
00228 
00234     string str() const;
00235 
00236 protected:
00237     IPNet<A> _net;      // The route entry's subnet address
00238     PolicyTags _policytags; // Tags used for policy route redistribution
00239 };
00240 
00241 typedef IPRouteEntry<IPv4> IPv4RouteEntry;
00242 typedef IPRouteEntry<IPv6> IPv6RouteEntry;
00243 
00258 template <class A>
00259 class ResolvedIPRouteEntry : public IPRouteEntry<A> {
00260 public:
00261     typedef multimap<const IPRouteEntry<A>* , ResolvedIPRouteEntry<A>* > RouteBackLink;
00262 
00263 public:
00278     ResolvedIPRouteEntry(const IPNet<A>& net, RibVif* vif, NextHop* nexthop,
00279              const Protocol& protocol, uint32_t metric,
00280              const IPRouteEntry<A>* igp_parent,
00281              const IPRouteEntry<A>* egp_parent)
00282     : IPRouteEntry<A>(net, vif, nexthop, protocol, metric, PolicyTags()),
00283     _igp_parent(igp_parent),
00284     _egp_parent(egp_parent) { }
00285 
00292     const IPRouteEntry<A>* igp_parent() const { return _igp_parent; }
00293 
00300     const IPRouteEntry<A>* egp_parent() const { return _egp_parent; }
00301 
00313     void set_backlink(typename RouteBackLink::iterator v);
00314 
00321     typename RouteBackLink::iterator backlink() const;
00322 
00323 private:
00324     mutable const IPRouteEntry<A>* _igp_parent;
00325     mutable const IPRouteEntry<A>* _egp_parent;
00326 
00327     // _backlink is used for removing the corresponding entry from the
00328     // RouteTable's map that is indexed by igp_parent.  Without it,
00329     // route deletion would be expensive.
00330     typename RouteBackLink::iterator _backlink;
00331 };
00332 
00333 template <typename A>
00334 inline void
00335 ResolvedIPRouteEntry<A>::set_backlink(typename RouteBackLink::iterator v)
00336 {
00337     _backlink = v;
00338 }
00339 
00340 template <typename A>
00341 inline typename ResolvedIPRouteEntry<A>::RouteBackLink::iterator
00342 ResolvedIPRouteEntry<A>::backlink() const
00343 {
00344     return _backlink;
00345 }
00346 
00347 typedef ResolvedIPRouteEntry<IPv4> ResolvedIPv4RouteEntry;
00348 typedef ResolvedIPRouteEntry<IPv6> ResolvedIPv6RouteEntry;
00349 
00362 template <class A>
00363 class UnresolvedIPRouteEntry {
00364 public:
00365     typedef multimap<A, UnresolvedIPRouteEntry<A>* > RouteBackLink;
00366 
00367 public:
00373     UnresolvedIPRouteEntry(const IPRouteEntry<A>* route)
00374     : _route(route) {}
00375 
00381     const IPRouteEntry<A>* route() const { return _route; }
00382 
00394     void set_backlink(typename RouteBackLink::iterator v);
00395 
00402     typename RouteBackLink::iterator backlink() const;
00403 
00404 private:
00405     //
00406     // _backlink is used for removing the corresponding entry from the
00407     // RouteTable's map that is indexed by the unresolved nexthop.
00408     // Without it, route deletion would be expensive.
00409     //
00410     typename RouteBackLink::iterator    _backlink;
00411     const IPRouteEntry<A>*      _route;
00412 };
00413 
00414 template <typename A>
00415 inline void
00416 UnresolvedIPRouteEntry<A>::set_backlink(typename RouteBackLink::iterator v)
00417 {
00418     _backlink = v;
00419 }
00420 
00421 template <typename A>
00422 inline typename UnresolvedIPRouteEntry<A>::RouteBackLink::iterator
00423 UnresolvedIPRouteEntry<A>::backlink() const
00424 {
00425     return _backlink;
00426 }
00427 
00428 typedef UnresolvedIPRouteEntry<IPv4> UnresolvedIPv4RouteEntry;
00429 typedef UnresolvedIPRouteEntry<IPv6> UnresolvedIPv6RouteEntry;
00430 
00431 #endif // __RIB_ROUTE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations