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