Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef SRC_BGP_ROUTING_INSTANCE_STATIC_ROUTE_H_ 6 : #define SRC_BGP_ROUTING_INSTANCE_STATIC_ROUTE_H_ 7 : 8 : #include <map> 9 : #include <set> 10 : #include <mutex> 11 : 12 : #include "bgp/routing-instance/istatic_route_mgr.h" 13 : 14 : #include "base/queue_task.h" 15 : #include "bgp/bgp_condition_listener.h" 16 : #include "bgp/bgp_config.h" 17 : #include "bgp/inet/inet_route.h" 18 : #include "bgp/inet6/inet6_route.h" 19 : 20 : class InetVpnRoute; 21 : class Inet6VpnRoute; 22 : class StaticRouteConfig; 23 : 24 : template <typename T> class StaticRoute; 25 : 26 : template <typename T1, typename T2, typename T3, typename T4> 27 : struct StaticRouteBase { 28 : typedef T1 RouteT; 29 : typedef T2 VpnRouteT; 30 : typedef T3 PrefixT; 31 : typedef T4 AddressT; 32 : }; 33 : 34 : class StaticRouteInet : public StaticRouteBase< 35 : InetRoute, InetVpnRoute, Ip4Prefix, Ip4Address> { 36 : }; 37 : 38 : class StaticRouteInet6 : public StaticRouteBase< 39 : Inet6Route, Inet6VpnRoute, Inet6Prefix, Ip6Address> { 40 : }; 41 : 42 : typedef ConditionMatchPtr StaticRoutePtr; 43 : 44 : struct StaticRouteRequest { 45 : enum RequestType { 46 : NEXTHOP_ADD_CHG, 47 : NEXTHOP_DELETE 48 : }; 49 : 50 356 : StaticRouteRequest(RequestType type, BgpTable *table, BgpRoute *route, 51 : StaticRoutePtr info) 52 356 : : type_(type), table_(table), rt_(route), info_(info) { 53 356 : } 54 : 55 : RequestType type_; 56 : BgpTable *table_; 57 : BgpRoute *rt_; 58 : StaticRoutePtr info_; 59 : 60 : private: 61 : DISALLOW_COPY_AND_ASSIGN(StaticRouteRequest); 62 : }; 63 : 64 : template <typename T> 65 : class StaticRouteMgr : public IStaticRouteMgr { 66 : public: 67 : typedef typename T::RouteT RouteT; 68 : typedef typename T::VpnRouteT VpnRouteT; 69 : typedef typename T::PrefixT PrefixT; 70 : typedef typename T::AddressT AddressT; 71 : typedef StaticRoute<T> StaticRouteT; 72 : 73 : struct RouteKey { 74 504 : RouteKey (const PrefixT &prefix, const IpAddress &nexthop) : 75 504 : prefix(prefix), nexthop(nexthop) { 76 504 : } 77 : 78 1758 : bool operator<(const RouteKey &rhs) const { 79 1758 : BOOL_KEY_COMPARE(prefix, rhs.prefix); 80 1220 : BOOL_KEY_COMPARE(nexthop, rhs.nexthop); 81 1146 : return false; 82 : } 83 : 84 : PrefixT prefix; 85 : IpAddress nexthop; 86 : }; 87 : 88 : // Map of Static Route prefix to the StaticRoute match object 89 : typedef std::map<RouteKey, StaticRoutePtr> StaticRouteMap; 90 : 91 : explicit StaticRouteMgr(RoutingInstance *instance); 92 : ~StaticRouteMgr(); 93 : 94 : // Config 95 : virtual void ProcessStaticRouteConfig(); 96 : virtual void UpdateStaticRouteConfig(); 97 : virtual void FlushStaticRouteConfig(); 98 : 99 : void EnqueueStaticRouteReq(StaticRouteRequest *req); 100 0 : const StaticRouteMap &static_route_map() const { return static_route_map_; } 101 : 102 : virtual void NotifyAllRoutes(); 103 : virtual void UpdateAllRoutes(); 104 : virtual uint32_t GetRouteCount() const; 105 : virtual uint32_t GetDownRouteCount() const; 106 : virtual bool FillStaticRouteInfo(RoutingInstance *rtinstance, 107 : StaticRouteEntriesInfo *info) const; 108 : 109 : Address::Family GetFamily() const; 110 : AddressT GetAddress(IpAddress addr) const; 111 : 112 : private: 113 : template <typename U> friend class StaticRouteTest; 114 : typedef std::set<StaticRoutePtr> StaticRouteProcessList; 115 : typedef BgpInstanceConfig::StaticRouteList StaticRouteConfigList; 116 : 117 : // All static route related actions are performed in the context 118 : // of this task. This task has exclusion with db::DBTable task. 119 : static int static_route_task_id_; 120 : 121 : int CompareStaticRoute(typename StaticRouteMap::iterator loc, 122 : StaticRouteConfigList::iterator it); 123 : void AddStaticRoute(StaticRouteConfigList::iterator it); 124 : void DelStaticRoute(typename StaticRouteMap::iterator loc); 125 : void UpdateStaticRoute(typename StaticRouteMap::iterator loc, 126 : StaticRouteConfigList::iterator it); 127 : 128 : void LocateStaticRoutePrefix(const StaticRouteConfig &config); 129 : void RemoveStaticRoutePrefix(const RouteKey &static_route); 130 : void StopStaticRouteDone(BgpTable *table, ConditionMatch *info); 131 : void UnregisterAndResolveStaticRoute(StaticRoutePtr entry); 132 : bool StaticRouteEventCallback(StaticRouteRequest *req); 133 : 134 : bool ProcessUnregisterList(); 135 : 136 : virtual void DisableUnregisterTrigger(); 137 : virtual void EnableUnregisterTrigger(); 138 : 139 12 : virtual void DisableQueue() { static_route_queue_->set_disable(true); } 140 12 : virtual void EnableQueue() { static_route_queue_->set_disable(false); } 141 12 : virtual bool IsQueueEmpty() { return static_route_queue_->IsQueueEmpty(); } 142 668 : RoutingInstance *routing_instance() { return rtinstance_; } 143 : 144 : RoutingInstance *rtinstance_; 145 : BgpConditionListener *listener_; 146 : StaticRouteMap static_route_map_; 147 : WorkQueue<StaticRouteRequest *> *static_route_queue_; 148 : std::mutex mutex_; 149 : StaticRouteProcessList unregister_static_route_list_; 150 : boost::scoped_ptr<TaskTrigger> unregister_list_trigger_; 151 : 152 : DISALLOW_COPY_AND_ASSIGN(StaticRouteMgr); 153 : }; 154 : 155 : typedef StaticRouteMgr<StaticRouteInet> StaticRouteMgrInet; 156 : typedef StaticRouteMgr<StaticRouteInet6> StaticRouteMgrInet6; 157 : 158 : #endif // SRC_BGP_ROUTING_INSTANCE_STATIC_ROUTE_H_