xorp

rib_ipc_handler.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/bgp/rib_ipc_handler.hh,v 1.51 2008/11/08 06:14:37 mjh Exp $
00022 
00023 #ifndef __BGP_RIB_IPC_HANDLER_HH__
00024 #define __BGP_RIB_IPC_HANDLER_HH__
00025 
00026 #include <deque>
00027 
00028 #include "peer_handler.hh"
00029 #include "plumbing.hh"
00030 #include "libxorp/eventloop.hh"
00031 #include "libxorp/timer.hh"
00032 #include "libxipc/xrl_std_router.hh"
00033 
00034 #include "policy/backend/policytags.hh"
00035 
00036 class RibIpcHandler;
00037 class EventLoop;
00038 
00039 template <class A>
00040 class XrlQueue {
00041 public:
00042     XrlQueue(RibIpcHandler &rib_ipc_handler, XrlStdRouter &xrl_router,
00043          BGPMain &_bgp);
00044 
00045     void queue_add_route(string ribname, bool ibgp, Safi, const IPNet<A>& net,
00046              const A& nexthop, const PolicyTags& policytags);
00047 
00048     void queue_delete_route(string ribname, bool ibgp, Safi,
00049                 const IPNet<A>& net);
00050 
00051     bool busy();
00052 private:
00053     static const size_t XRL_HIWAT = 100;    // Maximum number of XRLs
00054                         // allowed in flight.
00055     static const size_t XRL_LOWAT = 10;     // Low watermark for XRL
00056                         // in-flight flow control
00057                         // hysteresis.
00058 
00059     RibIpcHandler &_rib_ipc_handler;
00060     XrlStdRouter &_xrl_router;
00061     BGPMain &_bgp;
00062 
00063     struct Queued {
00064     bool add;
00065     string ribname;
00066     bool ibgp;
00067     Safi safi;
00068     IPNet<A> net;
00069     A nexthop;
00070     string comment;
00071     PolicyTags policytags;
00072     };
00073 
00074     deque <Queued> _xrl_queue;
00075     size_t _flying; //XRLs currently in flight
00076     bool _flow_controlled;
00077 
00081     bool flow_controlled() {
00082     if (_flying >= XRL_HIWAT)
00083         _flow_controlled = true;
00084     else if (_flying <= XRL_LOWAT)
00085         _flow_controlled = false;
00086     return _flow_controlled;
00087     }
00088 
00092     void start();
00093 
00101     bool sendit_spec(Queued& q, const char *bgp);
00102 
00103     EventLoop& eventloop() const;
00104 
00105     void route_command_done(const XrlError& error, const string comment);
00106 };
00107 
00108 /*
00109  * RibIpcHandler's job is to convert to and from XRLs, and to handle the
00110  * XRL state machine for talking to the RIB process 
00111  */
00112 
00113 class RibIpcHandler : public PeerHandler {
00114 public:
00115     RibIpcHandler(XrlStdRouter& xrl_router, BGPMain& bgp);
00116 
00117     ~RibIpcHandler();
00118 
00123     bool register_ribname(const string& r);
00124     int start_packet();
00125     /* add_route and delete_route are called to propagate a route *to*
00126        the RIB. */
00127     int add_route(const SubnetRoute<IPv4> &rt, 
00128           FPAList4Ref& pa_list,   
00129           bool ibgp, Safi safi);
00130     int replace_route(const SubnetRoute<IPv4> &old_rt, bool old_ibgp, 
00131               const SubnetRoute<IPv4> &new_rt, bool new_ibgp, 
00132               FPAList4Ref& pa_list,   
00133               Safi safi);
00134     int delete_route(const SubnetRoute<IPv4> &rt, 
00135              FPAList4Ref& pa_list,   
00136              bool ibgp, Safi safi);
00137     void rib_command_done(const XrlError& error, const char *comment);
00138     PeerOutputState push_packet();
00139 
00140     void set_plumbing(BGPPlumbing *plumbing_unicast,
00141               BGPPlumbing *plumbing_multicast) {
00142     _plumbing_unicast = plumbing_unicast;
00143     _plumbing_multicast = plumbing_multicast;
00144     }
00145 
00149     bool busy() {
00150     return (_v4_queue.busy()
00151 #ifdef HAVE_IPV6
00152         || _v6_queue.busy()
00153 #endif
00154         );
00155     }
00156 
00170     bool originate_route(const OriginType origin,
00171              const ASPath& aspath,
00172              const IPv4Net& nlri,
00173              const IPv4& next_hop,
00174              const bool& unicast,
00175              const bool& multicast,
00176              const PolicyTags& policytags);
00177 
00187     bool withdraw_route(const IPv4Net&  nlri,
00188             const bool& unicast,
00189             const bool& multicast);
00190 
00191     virtual uint32_t get_unique_id() const  { return _fake_unique_id; }
00192 
00193     //fake a zero IP address so the RIB IPC handler gets listed first
00194     //in the Fanout Table.
00195     const IPv4& id() const      { return _fake_id; }
00196 
00197     virtual PeerType get_peer_type() const {
00198     return PEER_TYPE_INTERNAL;
00199     }                                                                           
00205     virtual bool originate_route_handler() const {return true;}
00206 
00210     virtual EventLoop& eventloop() const { return _xrl_router.eventloop();}
00211 
00212 
00214 #ifdef HAVE_IPV6
00215     int add_route(const SubnetRoute<IPv6> &rt, 
00216           FPAList6Ref& pa_list,   
00217           bool ibgp, Safi safi);
00218     int replace_route(const SubnetRoute<IPv6> &old_rt, bool old_ibgp, 
00219               const SubnetRoute<IPv6> &new_rt, bool new_ibgp, 
00220               FPAList6Ref& pa_list,   
00221               Safi safi);
00222     int delete_route(const SubnetRoute<IPv6> &rt, 
00223              FPAList6Ref& pa_list,   
00224              bool ibgp, Safi safi);
00225 
00239     bool originate_route(const OriginType origin,
00240              const ASPath& aspath,
00241              const IPv6Net& nlri,
00242              const IPv6& next_hop,
00243              const bool& unicast,
00244              const bool& multicast,
00245              const PolicyTags& policytags);
00246 
00252     bool withdraw_route(const IPv6Net&  nlri,
00253             const bool& unicast,
00254             const bool& multicast);
00255 #endif // ipv6
00256 
00257 private:
00258     bool unregister_rib(string ribname);
00259 
00260     string _ribname;
00261     XrlStdRouter& _xrl_router;
00262 
00263     XrlQueue<IPv4> _v4_queue;
00264 #ifdef HAVE_IPV6
00265     XrlQueue<IPv6> _v6_queue;
00266 #endif
00267     const uint32_t _fake_unique_id;
00268     IPv4 _fake_id;
00269 };
00270 
00271 #endif // __BGP_RIB_IPC_HANDLER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations