OpenSDN source code
service_chaining.h
Go to the documentation of this file.
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_SERVICE_CHAINING_H_
7 #define SRC_BGP_ROUTING_INSTANCE_SERVICE_CHAINING_H_
8 
9 #include <boost/ptr_container/ptr_map.hpp>
10 #include <boost/shared_ptr.hpp>
11 
12 #include <list>
13 #include <map>
14 #include <set>
15 #include <string>
16 #include <vector>
17 #include <mutex>
18 
19 #include "base/lifetime.h"
20 #include "base/queue_task.h"
22 #include "bgp/inet/inet_route.h"
23 #include "bgp/inet6/inet6_route.h"
24 #include "bgp/evpn/evpn_route.h"
26 
27 class BgpRoute;
28 class BgpTable;
29 class RoutingInstance;
30 class BgpTable;
31 class BgpServer;
32 class InetVpnRoute;
33 class Inet6VpnRoute;
34 class SandeshResponse;
35 class ServiceChainConfig;
36 class ServiceChainGroup;
37 class ShowServicechainInfo;
38 
39 template <typename T> class ServiceChainMgr;
40 
41 template <typename T1, typename T2, typename T3, typename T4>
43  typedef T1 RouteT;
44  typedef T2 VpnRouteT;
45  typedef T3 PrefixT;
46  typedef T4 AddressT;
47 };
48 
50  InetRoute, InetVpnRoute, Ip4Prefix, Ip4Address> {
51 };
52 
54  Inet6Route, Inet6VpnRoute, Inet6Prefix, Ip6Address> {
55 };
56 
58  EvpnRoute, InetVpnRoute, EvpnPrefix, Ip4Address> {
59 };
60 
62  EvpnRoute, Inet6VpnRoute, EvpnPrefix, Ip6Address> {
63 };
64 
66 
68 public:
70  }
71  ServiceChainPtr info() { return info_; }
72 
73 private:
76 };
77 
78 template <typename T>
80 public:
81  typedef typename T::PrefixT PrefixT;
82 
83  enum RequestType {
93  };
94 
96  PrefixT aggregate_match, ServiceChainPtr info)
97  : type_(type),
98  table_(table),
99  rt_(route),
100  aggregate_match_(aggregate_match),
101  info_(info),
102  snh_resp_(NULL) {
103  }
104 
106  : type_(type),
107  table_(NULL),
108  rt_(NULL),
109  snh_resp_(resp) {
110  }
111 
118 
119 private:
121 };
122 
123 template <typename T>
124 class ServiceChain : public ConditionMatch {
125 public:
126  typedef typename T::RouteT RouteT;
127  typedef typename T::VpnRouteT VpnRouteT;
128  typedef typename T::PrefixT PrefixT;
129  typedef typename T::AddressT AddressT;
132 
133  // List of more specific routes resulted in Aggregate route
134  typedef std::set<BgpRoute *> RouteList;
135 
136  // Map of Virtual Network subnet prefix to List of More Specific routes
137  typedef std::map<PrefixT, RouteList> PrefixToRouteListMap;
138 
139  // Map of External Connecting route to Service Chain Route
140  typedef std::set<BgpRoute *> ExtConnectRouteList;
141 
142  // List of path ids for the connected route
143  typedef std::set<uint32_t> ConnectedPathIdList;
144 
146  RoutingInstance *src, RoutingInstance *dest, RoutingInstance *connected,
147  const std::vector<std::string> &subnets, AddressT addr, bool head,
148  bool retain_as_path);
151  return manager_->GetConnectedFamily();
152  }
154 
155  // Delete is triggered from configuration, not via LifetimeManager.
156  void ManagedDelete() { }
157 
158  bool CompareServiceChainConfig(const ServiceChainConfig &config);
159  void RemoveMatchState(BgpRoute *route, ServiceChainState *state);
160 
161  void SetConnectedRoute(BgpRoute *connected);
162  bool IsConnectedRouteValid() const;
164  return connected_path_ids_;
165  }
166 
172  void UpdateServiceChainRoute(PrefixT prefix, const RouteT *orig_route,
173  const ConnectedPathIdList &old_path_ids, bool aggregate);
174  void DeleteServiceChainRoute(PrefixT prefix, bool aggregate);
175 
176  bool AddMoreSpecific(PrefixT aggregate, BgpRoute *more_specific);
177  bool DeleteMoreSpecific(PrefixT aggregate, BgpRoute *more_specific);
178 
179  BgpTable *src_table() const;
180  BgpTable *connected_table() const;
181  BgpTable *dest_table() const;
182 
183  ServiceChainGroup *group() const { return group_; }
184  void clear_group() { group_ = NULL; }
185 
187  return &prefix_to_routelist_map_;
188  }
190  return &prefix_to_routelist_map_;
191  }
192 
193  virtual bool Match(BgpServer *server, BgpTable *table, BgpRoute *route,
194  bool deleted);
195  virtual std::string ToString() const;
196 
197  void FillServiceChainInfo(ShowServicechainInfo *info) const;
198 
201  }
204  }
205  bool dest_table_unregistered() const {
207  }
210  }
211  bool unregistered() const {
213  }
214 
216  return ext_connect_routes_;
217  }
219  return &ext_connect_routes_;
220  }
221 
222  bool aggregate_enable() const { return aggregate_; }
223  bool is_sc_head() const { return sc_head_; }
224  bool retain_as_path() const { return retain_as_path_; }
225  void set_aggregate_enable() { aggregate_ = true; }
226  bool group_oper_state_up() const { return group_oper_state_up_; }
228 
229 private:
243  bool aggregate_; // Whether the host route needs to be aggregated
244  bool sc_head_; // Whether this SI is at the head of the chain
249 
250  // Helper function to match
251  bool IsMoreSpecific(BgpRoute *route, PrefixT *aggregate_match) const;
252  bool IsAggregate(BgpRoute *route) const;
253  bool IsConnectedRoute(BgpRoute *route, bool is_conn_table=false) const;
254  bool IsEvpnType5Route(BgpRoute *route) const;
256  BgpRoute *&route, BgpTable *&table, PrefixT prefix, bool create);
257  void ProcessServiceChainPath(uint32_t path_id, BgpPath *path,
258  BgpAttrPtr attr, BgpRoute *&route, DBTablePartition *&partition,
259  bool aggregate, BgpTable *bgptable);
260  void UpdateServiceChainRouteInternal(const RouteT *orig_route,
261  const ConnectedPathIdList &old_path_ids, BgpRoute *sc_route,
262  DBTablePartition *partition, BgpTable *bgptable, bool aggregate);
263  void DeleteServiceChainRouteInternal(BgpRoute *service_chain_route,
264  DBTablePartition *partition,
265  BgpTable *bgptable, bool aggregate);
266 
268 };
269 
270 //
271 // This represents a service chain group within a ServiceChainMgr. All the
272 // individual ServiceChains that are part of the same logical service chain
273 // in the configuration are part of a given ServiceChainGroup.
274 //
275 // A ServiceChainGroup maintains operational state for itself based on the
276 // operational state of the individual ServiceChains that are part of the
277 // group. This is used to force fate sharing for all the ServiceChains in
278 // the group. If the operational status of the group is down, re-originated
279 // routes for all ServiceChainTs are deleted/withdrawn.
280 //
281 // A ServiceChainGroup gets created when the ServiceChainMgr processes a
282 // ServiceChainConfig with a non-empty service_chain_id. It gets deleted
283 // when there are no more ServiceChainTs or pending chains that belong to
284 // the ServiceChainGroup.
285 //
286 // The set of member chains is tracked using RoutingInstance pointers (as
287 // opposed to ServiceChainT pointers) so that the state of pending chains
288 // can also be tracked. Note that ServiceChainT objects are not allocated
289 // for pending chains.
290 //
291 // The membership of RoutingInstances in a ServiceChainGroup is updated as
292 // the group in the ServiceChainConfig for the RoutingInstances is updated.
293 //
294 // The operational state of the group is updated whenever a RoutingInstance
295 // is added or deleted to/from the ServiceChainGroup and when the connected
296 // route for the ServiceChainT is added/updated/deleted.
297 //
299 public:
300  ServiceChainGroup(IServiceChainMgr *manager, const std::string &name);
302 
303  void AddRoutingInstance(RoutingInstance *rtinstance);
304  void DeleteRoutingInstance(RoutingInstance *rtinstance);
305  void UpdateOperState();
306  std::string name() const { return name_; }
307  bool empty() const { return chain_set_.empty(); }
308  bool oper_state_up() const { return oper_state_up_; }
309 
310 private:
311  typedef std::set<RoutingInstance *> ServiceChainSet;
312 
314  std::string name_;
317 };
318 
319 template <typename T>
321 public:
322  typedef typename T::RouteT RouteT;
323  typedef typename T::PrefixT PrefixT;
324  typedef typename T::AddressT AddressT;
327 
328  explicit ServiceChainMgr(BgpServer *server);
329  virtual ~ServiceChainMgr();
330 
331  void Terminate();
332  void ManagedDelete();
333  bool MayDelete() const;
334  void RetryDelete();
335 
336  // Creates a new service chain between two Virtual network
337  // If the two routing instance is already connected, it updates the
338  // connected route address for existing service chain
339  virtual bool LocateServiceChain(RoutingInstance *rtinstance,
340  const ServiceChainConfig &config);
341 
342  // Remove the existing service chain between from routing instance
343  virtual void StopServiceChain(RoutingInstance *rtinstance);
344  virtual void UpdateServiceChain(RoutingInstance *rtinstance,
345  bool group_oper_state_up);
347 
348  virtual size_t PendingQueueSize() const { return pending_chains_.size(); }
349  virtual size_t ResolvedQueueSize() const { return chain_set_.size(); }
350  virtual uint32_t GetDownServiceChainCount() const;
351  virtual bool IsQueueEmpty() const { return process_queue_->IsQueueEmpty(); }
352  virtual bool ServiceChainIsPending(RoutingInstance *rtinstance,
353  std::string *reason = NULL) const;
354  virtual bool ServiceChainIsUp(RoutingInstance *rtinstance) const;
355 
360  virtual bool FillServiceChainInfo(RoutingInstance *rtinstance,
361  ShowServicechainInfo *info) const;
363 private:
364  template <typename U> friend class ServiceChainIntegrationTest;
365  template <typename U> friend class ServiceChainTest;
366  class DeleteActor;
367 
368  // All service chain related actions are performed in the context
369  // of this task. This task has exclusion with db::DBTable task.
371 
374  }
376  : group(group), reason(reason) {
377  }
379  std::string reason;
380  };
381 
382  // Set of service chains created in the system
383  typedef std::map<RoutingInstance *, ServiceChainPtr> ServiceChainMap;
384 
385  // At the time of processing, service chain request, all required info
386  // may not be available (e.g. dest routing instance may not be created,
387  // or marked deleted etc). Create a list of pending service chains that
388  // are waiting to get created and maintain a reason string for why the
389  // service chain is on the pending list.
390  typedef std::map<RoutingInstance *, PendingChainState> PendingChainList;
391 
392  typedef boost::ptr_map<std::string, ServiceChainGroup> GroupMap;
393  typedef std::set<ServiceChainGroup *> GroupSet;
394 
396  ServiceChainGroup *FindServiceChainGroup(const std::string &group_name);
397  ServiceChainGroup *LocateServiceChainGroup(const std::string &group_name);
399 
401  void StopServiceChainDone(BgpTable *table, ConditionMatch *info);
402  ServiceChainT *FindServiceChain(const std::string &instance) const;
403  ServiceChainT *FindServiceChain(RoutingInstance *rtinstance) const;
404 
406  ServiceChainGroup *group, std::string reason) {
407  PendingChainState state(group, reason);
408  pending_chains_.insert(std::make_pair(rtinstance, state));
409  }
411  pending_chains_.erase(rtinstance);
412  }
414  typename PendingChainList::const_iterator loc =
415  pending_chains_.find(rtinstance);
416  if (loc != pending_chains_.end()) {
417  return loc->second;
418  } else {
419  return PendingChainState();
420  }
421  }
422 
424  const typename ServiceChainT::ConnectedPathIdList &old_path_ids);
426 
427  void StartResolve();
429  void RoutingInstanceCallback(std::string name, int op);
430  void PeerRegistrationCallback(IPeer *peer, BgpTable *table,
431  bool unregister);
432 
434  virtual void set_aggregate_host_route(bool value) {
435  aggregate_host_route_ = value;
436  }
437 
438  virtual void DisableResolveTrigger();
439  virtual void EnableResolveTrigger();
440  virtual void DisableGroupTrigger();
441  virtual void EnableGroupTrigger();
442 
443  // Work Queue to handle requests posted from Match function, called
444  // in the context of db::DBTable task.
445  // The actions are performed in the bgp::ServiceChain task context.
446  virtual void DisableQueue() { process_queue_->set_disable(true); }
447  virtual void EnableQueue() { process_queue_->set_disable(false); }
448 
449  // Mutex is used to serialize access from multiple bgp::ConfigHelper tasks.
451  std::mutex mutex_;
453  boost::scoped_ptr<TaskTrigger> resolve_trigger_;
454  boost::scoped_ptr<WorkQueue<ServiceChainRequestT *> > process_queue_;
457  boost::scoped_ptr<TaskTrigger> group_trigger_;
461  int id_;
463  boost::scoped_ptr<DeleteActor> deleter_;
465 
467 };
468 
473 
474 #endif // SRC_BGP_ROUTING_INSTANCE_SERVICE_CHAINING_H_
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
Definition: bgp_attr.h:999
boost::intrusive_ptr< ConditionMatch > ConditionMatchPtr
Family
Definition: address.h:25
Definition: ipeer.h:187
bool oper_state_up() const
std::set< RoutingInstance * > ServiceChainSet
ServiceChainGroup(IServiceChainMgr *manager, const std::string &name)
void DeleteRoutingInstance(RoutingInstance *rtinstance)
void AddRoutingInstance(RoutingInstance *rtinstance)
ServiceChainSet chain_set_
IServiceChainMgr * manager_
std::string name() const
virtual bool IsQueueEmpty() const
BgpServer * server_
PendingChainList pending_chains_
void UpdateServiceChainGroup(ServiceChainGroup *group)
virtual BgpConditionListener * GetListener()
void UpdateServiceChainRoutes(ServiceChainT *chain, const typename ServiceChainT::ConnectedPathIdList &old_path_ids)
boost::scoped_ptr< TaskTrigger > resolve_trigger_
void DeleteServiceChainRoutes(ServiceChainT *chain)
boost::scoped_ptr< DeleteActor > deleter_
bool aggregate_host_route() const
ServiceChainGroup * LocateServiceChainGroup(const std::string &group_name)
Address::Family GetConnectedFamily() const
std::map< RoutingInstance *, ServiceChainPtr > ServiceChainMap
SCAddress::Family GetSCFamily() const
PendingChainState GetPendingServiceChain(RoutingInstance *rtinstance)
virtual void set_aggregate_host_route(bool value)
virtual uint32_t GetDownServiceChainCount() const
std::set< ServiceChainGroup * > GroupSet
virtual void StopServiceChain(RoutingInstance *rtinstance)
bool ProcessServiceChainGroups()
virtual void DisableGroupTrigger()
ServiceChainT * FindServiceChain(const std::string &instance) const
friend class ServiceChainTest
virtual void EnableGroupTrigger()
virtual void EnableResolveTrigger()
virtual bool LocateServiceChain(RoutingInstance *rtinstance, const ServiceChainConfig &config)
std::map< RoutingInstance *, PendingChainState > PendingChainList
boost::ptr_map< std::string, ServiceChainGroup > GroupMap
ServiceChainRequest< T > ServiceChainRequestT
ServiceChainGroup * FindServiceChainGroup(const std::string &group_name)
LifetimeRef< ServiceChainMgr > server_delete_ref_
virtual ~ServiceChainMgr()
T::AddressT AddressT
ServiceChainGroup * FindServiceChainGroup(RoutingInstance *rtinstance)
void StopServiceChainDone(BgpTable *table, ConditionMatch *info)
ServiceChainMgr(BgpServer *server)
ServiceChain< T > ServiceChainT
static int service_chain_task_id_
BgpConditionListener * listener_
DISALLOW_COPY_AND_ASSIGN(ServiceChainMgr)
virtual size_t ResolvedQueueSize() const
bool ResolvePendingServiceChain()
friend class ServiceChainIntegrationTest
void RoutingInstanceCallback(std::string name, int op)
virtual void DisableQueue()
void Enqueue(ServiceChainRequestT *req)
ServiceChainMap chain_set_
virtual void EnableQueue()
void AddPendingServiceChain(RoutingInstance *rtinstance, ServiceChainGroup *group, std::string reason)
bool RequestHandler(ServiceChainRequestT *req)
virtual void DisableResolveTrigger()
virtual size_t PendingQueueSize() const
void PeerRegistrationCallback(IPeer *peer, BgpTable *table, bool unregister)
virtual void UpdateServiceChain(RoutingInstance *rtinstance, bool group_oper_state_up)
void DeletePendingServiceChain(RoutingInstance *rtinstance)
boost::scoped_ptr< TaskTrigger > group_trigger_
virtual bool FillServiceChainInfo(RoutingInstance *rtinstance, ShowServicechainInfo *info) const
boost::scoped_ptr< WorkQueue< ServiceChainRequestT * > > process_queue_
virtual bool ServiceChainIsPending(RoutingInstance *rtinstance, std::string *reason=NULL) const
virtual bool ServiceChainIsUp(RoutingInstance *rtinstance) const
Address::Family GetFamily() const
bool MayDelete() const
SandeshResponse * snh_resp_
ServiceChainPtr info_
ServiceChainRequest(RequestType type, BgpTable *table, BgpRoute *route, PrefixT aggregate_match, ServiceChainPtr info)
DISALLOW_COPY_AND_ASSIGN(ServiceChainRequest)
ServiceChainRequest(RequestType type, SandeshResponse *resp)
ServiceChainState(ServiceChainPtr info)
ServiceChainPtr info_
ServiceChainPtr info()
DISALLOW_COPY_AND_ASSIGN(ServiceChainState)
bool group_oper_state_up() const
const ConnectedPathIdList & GetConnectedPathIds()
SCAddress::Family GetSCFamily() const
AddressT service_chain_addr_
bool dest_table_unregistered() const
bool dest_table_unregistered_
bool IsEvpnType5Route(BgpRoute *route) const
bool aggregate_enable() const
virtual std::string ToString() const
void set_dest_table_unregistered()
PrefixToRouteListMap * prefix_to_route_list_map()
std::set< uint32_t > ConnectedPathIdList
bool AddMoreSpecific(PrefixT aggregate, BgpRoute *more_specific)
ServiceChainMgrT * manager_
bool CompareServiceChainConfig(const ServiceChainConfig &config)
RoutingInstance * connected_routing_instance() const
void DeleteServiceChainRouteInternal(BgpRoute *service_chain_route, DBTablePartition *partition, BgpTable *bgptable, bool aggregate)
BgpRoute * connected_route_
void UpdateServiceChainRouteInternal(const RouteT *orig_route, const ConnectedPathIdList &old_path_ids, BgpRoute *sc_route, DBTablePartition *partition, BgpTable *bgptable, bool aggregate)
BgpRoute * connected_route() const
T::PrefixT PrefixT
void RemoveMatchState(BgpRoute *route, ServiceChainState *state)
T::RouteT RouteT
DISALLOW_COPY_AND_ASSIGN(ServiceChain)
void UpdateServiceChainRoute(PrefixT prefix, const RouteT *orig_route, const ConnectedPathIdList &old_path_ids, bool aggregate)
ServiceChainMgr< T > ServiceChainMgrT
PrefixToRouteListMap prefix_to_routelist_map_
bool connected_table_unregistered_
bool unregistered() const
LifetimeRef< ServiceChain > dest_table_delete_ref_
RoutingInstance * src_
ServiceChainGroup * group_
virtual bool Match(BgpServer *server, BgpTable *table, BgpRoute *route, bool deleted)
std::map< PrefixT, RouteList > PrefixToRouteListMap
void SetConnectedRoute(BgpRoute *connected)
T::AddressT AddressT
void FillServiceChainInfo(ShowServicechainInfo *info) const
ServiceChainRequest< T > ServiceChainRequestT
bool DeleteMoreSpecific(PrefixT aggregate, BgpRoute *more_specific)
const ExtConnectRouteList & ext_connecting_routes() const
bool is_sc_head() const
void ProcessServiceChainPath(uint32_t path_id, BgpPath *path, BgpAttrPtr attr, BgpRoute *&route, DBTablePartition *&partition, bool aggregate, BgpTable *bgptable)
BgpTable * dest_table() const
void set_group_oper_state_up(bool up)
void GetReplicationFamilyInfo(DBTablePartition *&partition, BgpRoute *&route, BgpTable *&table, PrefixT prefix, bool create)
LifetimeRef< ServiceChain > src_table_delete_ref_
bool retain_as_path() const
BgpTable * connected_table() const
ConnectedPathIdList connected_path_ids_
bool IsConnectedRoute(BgpRoute *route, bool is_conn_table=false) const
RoutingInstance * connected_
RoutingInstance * dest_
bool IsMoreSpecific(BgpRoute *route, PrefixT *aggregate_match) const
RoutingInstance * src_routing_instance() const
void set_connected_table_unregistered()
BgpTable * src_table() const
const PrefixToRouteListMap * prefix_to_route_list_map() const
ExtConnectRouteList ext_connect_routes_
LifetimeRef< ServiceChain > connected_table_delete_ref_
bool IsConnectedRouteValid() const
std::set< BgpRoute * > ExtConnectRouteList
ExtConnectRouteList * ext_connecting_routes()
std::set< BgpRoute * > RouteList
ServiceChain(ServiceChainMgrT *manager, ServiceChainGroup *group, RoutingInstance *src, RoutingInstance *dest, RoutingInstance *connected, const std::vector< std::string > &subnets, AddressT addr, bool head, bool retain_as_path)
T::VpnRouteT VpnRouteT
Address::Family GetConnectedFamily() const
bool IsAggregate(BgpRoute *route) const
RoutingInstance * dest_routing_instance() const
ServiceChainGroup * group() const
Address::Family GetFamily() const
bool connected_table_unregistered() const
void DeleteServiceChainRoute(PrefixT prefix, bool aggregate)
void set_aggregate_enable()
const AddressT & service_chain_addr() const
uint8_t type
Definition: load_balance.h:2
ServiceChainMgr< ServiceChainInet > ServiceChainMgrInet
ServiceChainMgr< ServiceChainEvpn6 > ServiceChainMgrEvpn6
ServiceChainMgr< ServiceChainEvpn > ServiceChainMgrEvpn
ServiceChainMgr< ServiceChainInet6 > ServiceChainMgrInet6
ConditionMatchPtr ServiceChainPtr
PendingChainState(ServiceChainGroup *group, std::string reason)