xorp

route_db.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/rip/route_db.hh,v 1.31 2008/10/29 21:59:39 andrewma Exp $
00022 
00023 #ifndef __RIP_ROUTE_DB_HH__
00024 #define __RIP_ROUTE_DB_HH__
00025 
00026 #include "libxorp/xorp.h"
00027 #include "libxorp/ref_ptr.hh"
00028 
00029 
00030 
00031 
00032 #include "policy/backend/policy_filters.hh"
00033 
00034 #include "route_entry.hh"
00035 #include "trace.hh"
00036 
00037 
00038 class EventLoop;
00039 
00040 template <typename A>
00041 class UpdateQueue;
00042 
00043 template <typename A>
00044 class PacketRouteEntry;
00045 
00046 template <typename A>
00047 class RouteWalker;
00048 
00049 template <typename A>
00050 class Peer;
00051 
00056 template <typename A>
00057 struct NetCmp {
00058     typedef IPNet<A> Net;
00059     bool operator() (const Net& l, const Net& r) const;
00060 };
00061 
00072 template <typename A>
00073 class RouteDB :
00074     public NONCOPYABLE
00075 {
00076 public:
00077     typedef A                   Addr;
00078     typedef IPNet<A>                Net;
00079     typedef RouteEntry<A>           Route;
00080     typedef RouteEntryOrigin<A>         RouteOrigin;
00081     typedef RouteEntryRef<A>            DBRouteEntry;
00082     typedef RouteEntryRef<A>            ConstDBRouteEntry;
00083     typedef PacketRouteEntry<A>         PacketizedRoute;
00084     typedef map<Net, DBRouteEntry, NetCmp<A> >  RouteContainer;
00085     typedef map<Net, Route*, NetCmp<A> >        RouteContainerNoRef;
00086 
00087 public:
00088     RouteDB(EventLoop& e, PolicyFilters& pfs);
00089     ~RouteDB();
00090 
00097     bool insert_peer(Peer<A>* peer);
00098 
00106     bool erase_peer(Peer<A>* peer);
00107 
00127     bool update_route(const Net&    net,
00128               const Addr&   nexthop,
00129               const string& ifname,
00130               const string& vifname,
00131               uint32_t      cost,
00132               uint32_t      tag,
00133               RouteOrigin*  origin,
00134               const PolicyTags& policytags,
00135               bool      is_policy_push);
00136 
00152     void add_rib_route(const Net&   net,
00153                const Addr&  nexthop,
00154                const string&    ifname,
00155                const string&    vifname,
00156                uint32_t     cost,
00157                uint32_t     tag,
00158                RouteOrigin* origin,
00159                const PolicyTags& policytags);
00160     
00167     void delete_rib_route(const Net& net);
00168 
00174     void dump_routes(vector<ConstDBRouteEntry>& routes);
00175 
00179     void flush_routes();
00180 
00184     uint32_t route_count() const;
00185 
00189     const Route* find_route(const Net& n) const;
00190 
00195     UpdateQueue<A>& update_queue();
00196 
00201     const UpdateQueue<A>& update_queue() const;
00202 
00203     EventLoop& eventloop()          { return _eventloop; }
00204 
00208     void push_routes();
00209 
00216     bool do_filtering(Route* r);
00217 
00218     Trace& trace() { return _trace; }
00219 
00220 protected:
00221     void expire_route(Route* r);
00222     void set_expiry_timer(Route* r);
00223 
00224     void delete_route(Route* r);
00225     void set_deletion_timer(Route* r);
00226 
00227 protected:
00228     RouteContainer& routes();
00229 
00230 protected:
00231     EventLoop&      _eventloop;
00232     RouteContainer  _routes;
00233     UpdateQueue<A>* _uq;
00234     PolicyFilters&  _policy_filters;
00235     set<Peer<A>* >  _peers;
00236 
00237 
00238     //
00239     // RIB routes are not "readvertised", so consider if a rib route loses,
00240     // and then the winning route expires... we will have no route for that
00241     // destination... while we should.
00242     //
00243     // Also need to be able to re-filter original routes
00244     RouteContainerNoRef _rib_routes;
00245     RouteOrigin*    _rib_origin;
00246     
00247     friend class RouteWalker<A>;
00248 
00249 private:
00250     Trace _trace;       // Trace variable
00251 };
00252 
00264 template <typename A>
00265 class RouteWalker :
00266     public NONCOPYABLE
00267 {
00268 public:
00269     typedef A                   Addr;
00270     typedef IPNet<A>                Net;
00271     typedef typename RouteDB<A>::RouteContainer RouteContainer;
00272     typedef typename RouteDB<A>::Route      Route;
00273 
00274     enum State { STATE_RUNNING, STATE_PAUSED };
00275 
00276 public:
00277     RouteWalker(RouteDB<A>& route_db);
00278 
00279     ~RouteWalker();
00280 
00284     State state() const         { return _state; }
00285 
00292     const Route* next_route();
00293 
00300     const Route* current_route();
00301 
00314     void pause(uint32_t pause_ms);
00315 
00322     void resume();
00323 
00328     void reset();
00329 
00330 private:
00331     static const Net NO_NET;
00332 
00333 private:
00334     RouteDB<A>& _route_db;  // RouteDB to be walked.
00335     State   _state;     // Current state (STATE_RUNNING/STATE_PAUSED).
00336     Net     _last_visited;  // Last route output before entering
00337                 // STATE_PAUSED.
00338                     // If set to RouteWalker::no_net there was
00339                     // no valid route when paused.
00340     typename RouteContainer::iterator _pos; // Current route when the
00341                         // state is STATE_RUNNING.
00342 };
00343 
00344 #endif // __RIP_ROUTE_DB_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations