xorp

fib2mrib_node.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/fib2mrib/fib2mrib_node.hh,v 1.25 2008/10/02 21:57:14 bms Exp $
00021 
00022 #ifndef __FIB2MRIB_FIB2MRIB_NODE_HH__
00023 #define __FIB2MRIB_FIB2MRIB_NODE_HH__
00024 
00025 
00026 //
00027 // Fib2mrib node definition.
00028 //
00029 
00030 
00031 
00032 #include "libxorp/service.hh"
00033 #include "libxorp/status_codes.h"
00034 
00035 #include "libfeaclient/ifmgr_xrl_mirror.hh"
00036 
00037 #include "policy/backend/policytags.hh"
00038 #include "policy/backend/policy_filters.hh"
00039 
00040 class EventLoop;
00041 
00047 class Fib2mribRoute {
00048 public:
00065     Fib2mribRoute(const IPv4Net& network, const IPv4& nexthop,
00066           const string& ifname, const string& vifname,
00067           uint32_t metric, uint32_t admin_distance,
00068           const string& protocol_origin, bool xorp_route)
00069     : _network(network), _nexthop(nexthop),
00070       _ifname(ifname), _vifname(vifname),
00071       _metric(metric), _admin_distance(admin_distance),
00072       _protocol_origin(protocol_origin), _xorp_route(xorp_route),
00073       _route_type(IDLE_ROUTE), _is_ignored(false),
00074       _is_filtered(false), _is_accepted_by_nexthop(false) {}
00075 
00092     Fib2mribRoute(const IPv6Net& network, const IPv6& nexthop,
00093           const string& ifname, const string& vifname,
00094           uint32_t metric, uint32_t admin_distance,
00095           const string& protocol_origin, bool xorp_route)
00096     : _network(network), _nexthop(nexthop),
00097       _ifname(ifname), _vifname(vifname),
00098       _metric(metric), _admin_distance(admin_distance),
00099       _protocol_origin(protocol_origin), _xorp_route(xorp_route),
00100       _route_type(IDLE_ROUTE), _is_ignored(false),
00101       _is_filtered(false), _is_accepted_by_nexthop(false) {}
00102 
00103 #ifdef XORP_USE_USTL
00104     Fib2mribRoute() { }
00105 #endif
00106 
00114     bool operator==(const Fib2mribRoute& other) const {
00115     return ((_network == other.network())
00116         && (_nexthop == other.nexthop())
00117         && (_ifname == other.ifname())
00118         && (_vifname == other.vifname())
00119         && (_metric == other.metric())
00120         && (_route_type == other._route_type)
00121         && (_policytags == other._policytags));
00122     }
00123 
00129     bool is_ipv4() const { return _network.is_ipv4(); }
00130 
00136     bool is_ipv6() const { return _network.is_ipv6(); }
00137 
00143     const IPvXNet& network() const { return _network; }
00144 
00150     const IPvX& nexthop() const { return _nexthop; }
00151 
00157     void set_nexthop(const IPvX& v) { _nexthop = v; }
00158 
00164     const string& ifname() const { return _ifname; }
00165 
00171     void set_ifname(const string& v) { _ifname = v; }
00172 
00178     const string& vifname() const { return _vifname; }
00179 
00185     void set_vifname(const string& v) { _vifname = v; }
00186 
00192     uint32_t admin_distance() const { return _admin_distance; }
00193 
00199     uint32_t metric() const { return _metric; }
00200 
00206     const string& protocol_origin() const { return _protocol_origin; }
00207 
00213     bool xorp_route() const { return _xorp_route; }
00214 
00220     bool is_add_route() const { return (_route_type == ADD_ROUTE); }
00221 
00227     bool is_replace_route() const { return (_route_type == REPLACE_ROUTE); }
00228 
00234     bool is_delete_route() const { return (_route_type == DELETE_ROUTE); }
00235 
00239     void set_add_route() { _route_type = ADD_ROUTE; }
00240 
00244     void set_replace_route() { _route_type = REPLACE_ROUTE; }
00245 
00249     void set_delete_route() { _route_type = DELETE_ROUTE; }
00250 
00257     bool is_interface_route() const { return ! (_ifname.empty()
00258                         && _vifname.empty()); }
00259 
00266     bool is_valid_entry(string& error_msg) const;
00267 
00276     bool is_ignored() const { return _is_ignored; }
00277 
00286     void set_ignored(bool v) { _is_ignored = v; }
00287 
00291     PolicyTags& policytags() { return _policytags; }
00292     
00299     bool is_filtered() const { return _is_filtered; }
00300 
00308     void set_filtered(bool v) { _is_filtered = v; }
00309 
00316     bool is_accepted_by_nexthop() const { return _is_accepted_by_nexthop; }
00317 
00325     void set_accepted_by_nexthop(bool v) { _is_accepted_by_nexthop = v; }
00326 
00333     bool is_accepted_by_rib() const;
00334 
00335 private:
00336     IPvXNet _network;
00337     IPvX    _nexthop;
00338     string  _ifname;
00339     string  _vifname;
00340     uint32_t    _metric;
00341     uint32_t    _admin_distance;
00342     string  _protocol_origin;
00343     bool    _xorp_route;
00344     enum RouteType { IDLE_ROUTE, ADD_ROUTE, REPLACE_ROUTE, DELETE_ROUTE };
00345     RouteType   _route_type;
00346     bool    _is_ignored;    // True if the route is to be ignored
00347     bool    _is_filtered;   // True if rejected by a policy filter
00348     bool    _is_accepted_by_nexthop; // True if the route is accepted based on its next-hop information
00349     PolicyTags  _policytags;
00350 };
00351 
00352 
00358 class Fib2mribNode : public IfMgrHintObserver,
00359              public ServiceBase,
00360              public ServiceChangeObserverBase {
00361 public:
00367     Fib2mribNode(EventLoop& eventloop);
00368 
00372     virtual ~Fib2mribNode();
00373 
00379     EventLoop&  eventloop() { return _eventloop; }
00380 
00386     const string& protocol_name() const { return _protocol_name; }
00387 
00393     int startup();
00394 
00400     int shutdown();
00401 
00409     ProcessStatus   node_status(string& reason_msg);
00410 
00416     bool    is_done() const { return (_node_status == PROC_DONE); }
00417 
00423     bool    is_enabled() const { return _is_enabled; }
00424 
00434     void    set_enabled(bool enable);
00435 
00454     int add_route4(const IPv4Net& network, const IPv4& nexthop,
00455            const string& ifname, const string& vifname,
00456            uint32_t metric, uint32_t admin_distance,
00457            const string& protocol_origin, bool xorp_route,
00458            string& error_msg);
00459 
00478     int add_route6(const IPv6Net& network, const IPv6& nexthop,
00479            const string& ifname, const string& vifname,
00480            uint32_t metric, uint32_t admin_distance,
00481            const string& protocol_origin, bool xorp_route,
00482            string& error_msg);
00483 
00502     int replace_route4(const IPv4Net& network, const IPv4& nexthop,
00503                const string& ifname, const string& vifname,
00504                uint32_t metric, uint32_t admin_distance,
00505                const string& protocol_origin, bool xorp_route,
00506                string& error_msg);
00507 
00526     int replace_route6(const IPv6Net& network, const IPv6& nexthop,
00527                const string& ifname, const string& vifname,
00528                uint32_t metric, uint32_t admin_distance,
00529                const string& protocol_origin, bool xorp_route,
00530                string& error_msg);
00531 
00543     int delete_route4(const IPv4Net& network, const string& ifname,
00544               const string& vifname, string& error_msg);
00545 
00557     int delete_route6(const IPv6Net& network, const string& ifname,
00558               const string& vifname, string& error_msg);
00559 
00560     //
00561     // Debug-related methods
00562     //
00563     
00571     bool    is_log_trace() const { return (_is_log_trace); }
00572     
00580     void    set_log_trace(bool is_enabled) { _is_log_trace = is_enabled; }
00581 
00592     void configure_filter(const uint32_t& filter, const string& conf);
00593 
00599     void reset_filter(const uint32_t& filter);
00600 
00604     void push_routes();
00605 
00611     void push_pull_rib_routes(bool is_push);
00612 
00613 
00614 protected:
00615     //
00616     // IfMgrHintObserver methods
00617     //
00618     void tree_complete();
00619     void updates_made();
00620 
00621     void incr_startup_requests_n();
00622     void decr_startup_requests_n();
00623     void incr_shutdown_requests_n();
00624     void decr_shutdown_requests_n();
00625     void update_status();
00626 
00627 private:
00635     void status_change(ServiceBase*  service,
00636                ServiceStatus old_status,
00637                ServiceStatus new_status);
00638 
00647     virtual const ServiceBase* ifmgr_mirror_service_base() const = 0;
00648 
00657     virtual const IfMgrIfTree&  ifmgr_iftree() const = 0;
00658 
00665     virtual void fea_register_startup() = 0;
00666 
00673     virtual void fea_register_shutdown() = 0;
00674 
00681     virtual void rib_register_startup() = 0;
00682 
00689     virtual void rib_register_shutdown() = 0;
00690 
00699     int add_route(const Fib2mribRoute& fib2mrib_route, string& error_msg);
00700 
00709     int replace_route(const Fib2mribRoute& fib2mrib_route, string& error_msg);
00710 
00719     int delete_route(const Fib2mribRoute& fib2mrib_route, string& error_msg);
00720 
00730     void prepare_route_for_transmission(Fib2mribRoute& orig_route,
00731                     Fib2mribRoute& copy_route);
00732 
00741     virtual void inform_rib_route_change(const Fib2mribRoute& fib2mrib_route) = 0;
00742 
00751     virtual void cancel_rib_route_change(const Fib2mribRoute& fib2mrib_route) = 0;
00752 
00776     bool update_route(const IfMgrIfTree& iftree, Fib2mribRoute& route);
00777 
00784     bool do_filtering(Fib2mribRoute& route);
00785 
00793     bool is_accepted_by_nexthop(const Fib2mribRoute& route) const;
00794     
00800     void inform_rib(const Fib2mribRoute& r);
00801 
00807     void set_node_status(ProcessStatus v) { _node_status = v; }
00808 
00809     EventLoop&      _eventloop;     // The event loop
00810     ProcessStatus   _node_status;       // The node/process status
00811     const string    _protocol_name;     // The protocol name
00812     bool        _is_enabled;        // Flag whether node is enabled
00813 
00814     //
00815     // The routes are stored in a multimap, because we allow more than one
00816     // route for same subnet destination, but with different interface/vif
00817     // name.
00818     // E.g., in case of IPv6 we may have the same (link-local) subnet
00819     // address pre interface.
00820     //
00821     multimap<IPvXNet, Fib2mribRoute>    _fib2mrib_routes;
00822 
00823     //
00824     // Status-related state
00825     //
00826     size_t      _startup_requests_n;
00827     size_t      _shutdown_requests_n;
00828 
00829     //
00830     // A local copy with the interface state information
00831     //
00832     IfMgrIfTree     _iftree;
00833 
00834     //
00835     // Debug and test-related state
00836     //
00837     bool    _is_log_trace;      // If true, enable XLOG_TRACE()
00838 
00839     PolicyFilters   _policy_filters;    // Only one instance of this!
00840 };
00841 
00842 #endif // __FIB2MRIB_FIB2MRIB_NODE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations