|
xorp
|
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/rip/route_entry.hh,v 1.23 2008/10/02 21:58:17 bms Exp $ 00022 00023 #ifndef __RIP_ROUTE_ENTRY_HH__ 00024 #define __RIP_ROUTE_ENTRY_HH__ 00025 00026 #include "libxorp/xorp.h" 00027 #include "libxorp/ipnet.hh" 00028 #include "libxorp/timer.hh" 00029 00030 00031 00032 #include "policy/backend/policytags.hh" 00033 00034 template<typename A> class RouteEntryOrigin; 00035 template<typename A> class RouteEntryRef; 00036 00044 template<typename A> 00045 class RouteEntry : 00046 public NONCOPYABLE 00047 { 00048 public: 00049 typedef A Addr; 00050 typedef IPNet<A> Net; 00051 typedef RouteEntryOrigin<A> Origin; 00052 00053 public: 00061 RouteEntry(const Net& n, 00062 const Addr& nh, 00063 const string& ifname, 00064 const string& vifname, 00065 uint16_t cost, 00066 Origin*& o, 00067 uint16_t tag); 00068 00069 RouteEntry(const Net& n, 00070 const Addr& nh, 00071 const string& ifname, 00072 const string& vifname, 00073 uint16_t cost, 00074 Origin*& o, 00075 uint16_t tag, 00076 const PolicyTags& policytags); 00084 ~RouteEntry(); 00085 00089 const IPNet<A>& net() const { return _net; } 00090 00098 bool set_nexthop(const A& nh); 00099 00105 const A& nexthop() const { return _nh; } 00106 00112 const string& ifname() const { return _ifname; } 00113 00120 bool set_ifname(const string& ifname); 00121 00127 const string& vifname() const { return _vifname; } 00128 00135 bool set_vifname(const string& vifname); 00136 00144 bool set_cost(uint16_t cost); 00145 00151 uint16_t cost() const { return _cost; } 00152 00162 bool set_origin(Origin* origin); 00163 00169 const Origin* origin() const { return _origin; } 00170 00176 Origin* origin() { return _origin; } 00177 00185 bool set_tag(uint16_t tag); 00186 00192 uint16_t tag() const { return _tag; } 00193 00197 void set_timer(const XorpTimer& t) { _timer = t; } 00198 00202 const XorpTimer& timer() const { return _timer; } 00203 00207 const PolicyTags& policytags() const { return _policytags; } 00208 00209 PolicyTags& policytags() { return _policytags; } 00210 00216 bool set_policytags(const PolicyTags& tags); 00217 00221 bool filtered() const { return _filtered; } 00227 void set_filtered(bool v) { _filtered = v; } 00228 00229 private: 00230 friend class RouteEntryRef<A>; 00231 void ref() { _ref_cnt++; } 00232 uint16_t unref() { return --_ref_cnt; } 00233 uint16_t ref_cnt() const { return _ref_cnt; } 00234 00235 protected: 00236 void dissociate(); 00237 void associate(Origin* o); 00238 00239 protected: 00240 Net _net; 00241 Addr _nh; 00242 string _ifname; 00243 string _vifname; 00244 uint16_t _cost; 00245 Origin* _origin; 00246 uint16_t _tag; 00247 uint16_t _ref_cnt; 00248 00249 XorpTimer _timer; 00250 00251 PolicyTags _policytags; 00252 bool _filtered; 00253 }; 00254 00255 00264 template <typename A> 00265 class RouteEntryRef { 00266 private: 00267 RouteEntry<A>* _rt; 00268 00269 protected: 00270 void release() { 00271 if (_rt && _rt->unref() == 0) delete _rt; 00272 } 00273 00274 public: 00275 RouteEntryRef(RouteEntry<A>* r) : _rt(r) { _rt->ref(); } 00276 00277 RouteEntryRef() : _rt(0) {} 00278 00279 ~RouteEntryRef() { release(); } 00280 00281 RouteEntryRef(const RouteEntryRef& o) : _rt(o._rt) 00282 { 00283 if (_rt) _rt->ref(); 00284 } 00285 00286 RouteEntryRef& operator=(const RouteEntryRef& o) { 00287 release(); 00288 _rt = o._rt; 00289 if (_rt) _rt->ref(); 00290 return *this; 00291 } 00292 00293 RouteEntry<A>* get() const { return _rt; } 00294 00295 RouteEntry<A>* operator->() const { return _rt; } 00296 00297 bool operator==(const RouteEntryRef& o) const { return _rt == o._rt; } 00298 }; 00299 00300 00308 template <typename A> 00309 class RouteEntryOrigin : 00310 public NONCOPYABLE 00311 { 00312 public: 00313 typedef RouteEntry<A> Route; 00314 typedef IPNet<A> Net; 00315 struct RouteEntryStore; 00316 00317 public: 00318 RouteEntryOrigin(bool is_rib_origin); 00319 virtual ~RouteEntryOrigin(); 00320 00326 bool is_rib_origin() const { return _is_rib_origin; } 00327 00333 bool associate(Route* r); 00334 00340 bool dissociate(Route* r); 00341 00347 Route* find_route(const Net& n) const; 00348 00352 uint32_t route_count() const; 00353 00357 void clear(); 00358 00362 void dump_routes(vector<const Route*>& routes) const; 00363 00369 virtual uint32_t expiry_secs() const = 0; 00370 00375 virtual uint32_t deletion_secs() const = 0; 00376 00377 protected: 00378 struct RouteEntryStore* _rtstore; 00379 00380 private: 00381 bool _is_rib_origin; // True if the origin is RIB 00382 }; 00383 00384 00385 00386 #endif // __RIP_ROUTE_ENTRY_HH__