OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
flow_mgmt_key.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef __AGENT_PKT_FLOW_MGMT_KEY_H__
6 #define __AGENT_PKT_FLOW_MGMT_KEY_H__
7 
8 #include <db/db_entry.h>
9 #include <pkt/flow_event.h>
10 
11 class FlowMgmtKey {
12 public:
13  enum Type {
16  ACL,
18  VN,
19  VM,
23  NH,
24  VRF,
27  };
28 
30  type_(type), db_entry_(db_entry) {
31  }
32 
33  virtual ~FlowMgmtKey() { }
34 
35  // Clone the key
36  virtual FlowMgmtKey *Clone() = 0;
37 
38  // Convert from FlowMgmtKey to FlowEvent
39  virtual void KeyToFlowRequest(FlowEvent *req) {
40  req->set_db_entry(db_entry_);
41  }
42 
43  // Is DBEntry used as key. Routes dont use DBEntry as key
44  virtual bool UseDBEntry() const { return true; }
45 
46  // Comparator
47  virtual bool Compare(const FlowMgmtKey *rhs) const {
48  return false;
49  }
50 
51  bool IsLess(const FlowMgmtKey *rhs) const {
52  if (type_ != rhs->type_)
53  return type_ < rhs->type_;
54 
55  if (UseDBEntry()) {
56  if (db_entry_ != rhs->db_entry_)
57  return db_entry_ < rhs->db_entry_;
58  }
59 
60  return Compare(rhs);
61  }
62 
64  Type type() const { return type_; }
65  const DBEntry *db_entry() const { return db_entry_; }
67 
68 protected:
70  mutable const DBEntry *db_entry_;
71 
72 private:
74 };
75 
77  bool operator()(const FlowMgmtKey *l, const FlowMgmtKey *r) const {
78  return l->IsLess(r);
79  }
80 };
81 
82 class AclFlowMgmtKey : public FlowMgmtKey {
83 public:
85  FlowMgmtKey(FlowMgmtKey::ACL, acl) {
86  if (ace_id_list) {
88  }
89  }
90 
91  virtual ~AclFlowMgmtKey() { }
92 
93  virtual FlowMgmtKey *Clone() {
94  return new AclFlowMgmtKey(static_cast<const AclDBEntry *>(db_entry()),
95  &ace_id_list_);
96  }
97 
98  const AclEntryIDList *ace_id_list() const { return &ace_id_list_; }
99 
100  void set_ace_id_list(const AclEntryIDList *list) {
101  ace_id_list_ = *list;
102  }
103 
104 private:
107 };
108 
109 class VnFlowMgmtKey : public FlowMgmtKey {
110 public:
112  virtual ~VnFlowMgmtKey() { }
113 
114  virtual FlowMgmtKey *Clone() {
115  return new VnFlowMgmtKey(static_cast<const VnEntry *>(db_entry()));
116  }
117 
118 private:
120 };
121 
123 public:
126  }
127 
128  virtual ~InterfaceFlowMgmtKey() { }
129 
130  virtual FlowMgmtKey *Clone() {
131  return new InterfaceFlowMgmtKey(static_cast<const Interface *>(db_entry()));
132  }
133 
134 private:
136 };
137 
138 class NhFlowMgmtKey : public FlowMgmtKey {
139 public:
141  virtual ~NhFlowMgmtKey() { }
142 
143  virtual FlowMgmtKey *Clone() {
144  return new NhFlowMgmtKey(static_cast<const NextHop *>(db_entry()));
145  }
146 
147 private:
149 };
150 
152 public:
153  static Type AddrToType(const IpAddress &addr) {
154  if (addr.is_v4())
155  return INET4;
156 
157  if (addr.is_v6())
158  return INET6;
159 
160  assert(0);
161  return INVALID;
162  }
163 
164  static Type RouteToType(const AgentRoute *rt) {
165  const InetUnicastRouteEntry *inet_rt =
166  dynamic_cast<const InetUnicastRouteEntry *>(rt);
167  if (inet_rt) {
168  return AddrToType(inet_rt->prefix_address());
169  }
170 
171  if (dynamic_cast<const BridgeRouteEntry *>(rt))
172  return BRIDGE;
173 
174  assert(0);
175  }
176 
178  FlowMgmtKey(type, NULL), vrf_id_(vrf_id) {
179  }
180 
181  RouteFlowMgmtKey(Type type, const AgentRoute *rt, uint32_t vrf_id) :
182  FlowMgmtKey(type, rt), vrf_id_(vrf_id) {
183  }
184 
185  virtual ~RouteFlowMgmtKey() { }
186 
187  virtual bool UseDBEntry() const { return false; }
188  uint32_t vrf_id() const { return vrf_id_; }
189 
190 protected:
191  uint32_t vrf_id_;
192 
193 private:
195 };
196 
198 public:
200  RouteFlowMgmtKey(AddrToType(route->prefix_address()), route, route->vrf_id()),
201  ip_(route->prefix_address()), plen_(route->prefix_length()) {
202  }
203 
204  InetRouteFlowMgmtKey(uint32_t vrf_id, const IpAddress &ip, uint8_t plen) :
205  RouteFlowMgmtKey(AddrToType(ip), vrf_id), ip_(ip), plen_(plen) {
206  }
207 
208  virtual ~InetRouteFlowMgmtKey() { }
209 
210  virtual bool Compare(const FlowMgmtKey *rhs) const {
211  const InetRouteFlowMgmtKey *rhs_key =
212  static_cast<const InetRouteFlowMgmtKey *>(rhs);
213  if (vrf_id_ != rhs_key->vrf_id_)
214  return vrf_id_ < rhs_key->vrf_id_;
215 
216  if (ip_ != rhs_key->ip_)
217  return ip_ < rhs_key->ip_;
218 
219  return plen_ < rhs_key->plen_;
220  }
221 
222  class KeyCmp {
223  public:
224  static std::size_t BitLength(const InetRouteFlowMgmtKey *rt) {
225  return (((sizeof(rt->vrf_id_) + sizeof(rt->type_)) << 3) +
226  rt->plen_);
227  }
228 
229  static char ByteValue(const InetRouteFlowMgmtKey *rt, std::size_t idx) {
230  const char *ch;
231  std::size_t i = idx;
232  if (i < sizeof(rt->vrf_id_)) {
233  ch = (const char *)&rt->vrf_id_;
234  return ch[sizeof(rt->vrf_id_) - i - 1];
235  }
236  i -= sizeof(rt->vrf_id_);
237  if (i < sizeof(rt->type_)) {
238  ch = (const char *)&rt->type_;
239  return ch[sizeof(rt->type_) - i - 1];
240  }
241  i -= sizeof(rt->type_);
242  if (rt->type_ == INET4) {
243  return rt->ip_.to_v4().to_bytes()[i];
244  } else if (rt->type_ == INET6) {
245  return rt->ip_.to_v6().to_bytes()[i];
246  } else {
247  assert(0);
248  return 0;
249  }
250  }
251  };
252 
254  if (ip_.is_v4()) {
255  return new InetRouteFlowMgmtKey(vrf_id_, ip_.to_v4(), plen_);
256  }
257 
258  if (ip_.is_v6()) {
259  return new InetRouteFlowMgmtKey(vrf_id_, ip_.to_v6(), plen_);
260  }
261 
262  return NULL;
263  }
264 
265  bool Match(const IpAddress &match_ip) const {
266  if (ip_.is_v4()) {
267  return (Address::GetIp4SubnetAddress(ip_.to_v4(), plen_) ==
268  Address::GetIp4SubnetAddress(match_ip.to_v4(), plen_));
269  } else if (ip_.is_v6()) {
270  return (Address::GetIp6SubnetAddress(ip_.to_v6(), plen_) ==
271  Address::GetIp6SubnetAddress(match_ip.to_v6(), plen_));
272  }
273 
274  assert(0);
275  return false;
276  }
277 
278  bool NeedsReCompute(const FlowEntry *flow);
279  IpAddress ip() const { return ip_; }
280  uint8_t plen() const { return plen_; }
281 
282 private:
283  friend class InetRouteFlowMgmtTree;
284 
286  uint8_t plen_;
289 };
290 
292 public:
294  RouteFlowMgmtKey(BRIDGE, rt, rt->vrf_id()), mac_(rt->prefix_address()) {
295  }
296 
297  BridgeRouteFlowMgmtKey(uint32_t vrf_id, const MacAddress &mac) :
298  RouteFlowMgmtKey(BRIDGE, vrf_id), mac_(mac) {
299  }
300 
302 
303  virtual bool Compare(const FlowMgmtKey *rhs) const {
304  const BridgeRouteFlowMgmtKey *rhs_key =
305  static_cast<const BridgeRouteFlowMgmtKey *>(rhs);
306  if (vrf_id_ != rhs_key->vrf_id_)
307  return vrf_id_ < rhs_key->vrf_id_;
308 
309  return mac_ < rhs_key->mac_;
310  }
311 
313  return new BridgeRouteFlowMgmtKey(vrf_id(), mac_);
314  }
315 
316 private:
318 
321 };
322 
323 class VrfFlowMgmtKey : public FlowMgmtKey {
324 public:
325  VrfFlowMgmtKey(const VrfEntry *vrf) :
326  FlowMgmtKey(FlowMgmtKey::VRF, vrf) {
327  }
328 
329  virtual ~VrfFlowMgmtKey() { }
330 
331  virtual FlowMgmtKey *Clone() {
332  return new VrfFlowMgmtKey(static_cast<const VrfEntry *>(db_entry()));
333  }
334 
335 private:
337 };
338 
340 public:
342  uint32_t source_port,
343  uint8_t cn_index,
344  HealthCheckInstanceBase *hc_instance,
345  HealthCheckService *hc_service) :
347  source_port_(source_port), cn_index_(cn_index),
348  bgp_health_check_instance_(hc_instance),
349  bgp_health_check_service_(hc_service) { }
350 
352 
353  virtual FlowMgmtKey *Clone() {
357  }
358 
359  virtual bool UseDBEntry() const { return false; }
360 
361  virtual bool Compare(const FlowMgmtKey *rhs) const {
362  const BgpAsAServiceFlowMgmtKey *rhs_key =
363  static_cast<const BgpAsAServiceFlowMgmtKey *>(rhs);
364  if (uuid_ != rhs_key->uuid_)
365  return uuid_ < rhs_key->uuid_;
366  if (cn_index_ != rhs_key->cn_index_)
367  return cn_index_< rhs_key->cn_index_;
368  return source_port_ < rhs_key->source_port_;
369  }
370 
371  const boost::uuids::uuid &uuid() const { return uuid_; }
372  uint32_t source_port() const { return source_port_; }
373  uint8_t cn_index() const { return cn_index_; }
374 
377  }
378 
379  void StartHealthCheck(Agent *agent, FlowEntry *flow,
380  const boost::uuids::uuid &hc_uuid);
381  void StopHealthCheck(FlowEntry *flow);
382 
383 private:
385  uint32_t source_port_;
386  uint8_t cn_index_; //Control node index
387  // By adding the health check instance here, we ensure one BFD session
388  // per <VMI-SRC-IP, BGP-DEST-IP, VMI>
392 };
393 
394 #endif // __AGENT_PKT_FLOW_MGMT_KEY_H__
virtual ~VrfFlowMgmtKey()
HealthCheckService * bgp_health_check_service_
virtual ~AclFlowMgmtKey()
Definition: flow_mgmt_key.h:91
virtual ~BridgeRouteFlowMgmtKey()
Definition: vrf.h:86
virtual bool UseDBEntry() const
Definition: flow_mgmt_key.h:44
static Type RouteToType(const AgentRoute *rt)
const DBEntry * db_entry() const
Definition: flow_mgmt_key.h:65
DISALLOW_COPY_AND_ASSIGN(BridgeRouteFlowMgmtKey)
Type type() const
Definition: flow_mgmt_key.h:64
virtual ~FlowMgmtKey()
Definition: flow_mgmt_key.h:33
DISALLOW_COPY_AND_ASSIGN(NhFlowMgmtKey)
boost::asio::ip::address IpAddress
Definition: address.h:13
virtual bool Compare(const FlowMgmtKey *rhs) const
AclEntryIDList ace_id_list_
virtual FlowMgmtKey * Clone()
virtual FlowMgmtKey * Clone()
Definition: flow_mgmt_key.h:93
VrfFlowMgmtKey(const VrfEntry *vrf)
RouteFlowMgmtKey(Type type, const AgentRoute *rt, uint32_t vrf_id)
virtual ~BgpAsAServiceFlowMgmtKey()
std::vector< AclEntryID > AclEntryIDList
Definition: acl_entry.h:85
BgpAsAServiceFlowMgmtKey(const boost::uuids::uuid &uuid, uint32_t source_port, uint8_t cn_index, HealthCheckInstanceBase *hc_instance, HealthCheckService *hc_service)
boost::uuids::uuid uuid
HealthCheckInstanceBase * bgp_health_check_instance() const
DISALLOW_COPY_AND_ASSIGN(RouteFlowMgmtKey)
virtual FlowMgmtKey * Clone()
virtual void KeyToFlowRequest(FlowEvent *req)
Definition: flow_mgmt_key.h:39
static Ip4Address GetIp4SubnetAddress(const Ip4Address &prefix, uint16_t plen)
Definition: address.cc:179
Base class for all Route entries in agent.
Definition: agent_route.h:224
boost::uuids::uuid uuid_
static Ip6Address GetIp6SubnetAddress(const Ip6Address &prefix, uint16_t plen)
Definition: address.cc:200
const AclEntryIDList * ace_id_list() const
Definition: flow_mgmt_key.h:98
bool NeedsReCompute(const FlowEntry *flow)
void set_db_entry(const DBEntry *db_entry)
Definition: flow_mgmt_key.h:66
virtual ~RouteFlowMgmtKey()
DISALLOW_COPY_AND_ASSIGN(VrfFlowMgmtKey)
void StopHealthCheck(FlowEntry *flow)
Definition: agent.h:358
DISALLOW_COPY_AND_ASSIGN(BgpAsAServiceFlowMgmtKey)
FlowMgmtKey * Clone()
static std::size_t BitLength(const InetRouteFlowMgmtKey *rt)
BridgeRouteFlowMgmtKey(uint32_t vrf_id, const MacAddress &mac)
virtual ~VnFlowMgmtKey()
RouteFlowMgmtKey(Type type, uint32_t vrf_id)
bool operator()(const FlowMgmtKey *l, const FlowMgmtKey *r) const
Definition: flow_mgmt_key.h:77
virtual FlowMgmtKey * Clone()=0
DISALLOW_COPY_AND_ASSIGN(FlowMgmtKey)
uint8_t cn_index() const
uint32_t vrf_id() const
uint32_t source_port() const
virtual const PrefixType & prefix_address() const
Returns the value of a stored prefix address (IPv4, IPv6 or MAC address)
Definition: agent_route.h:375
IpAddress ip() const
Definition: vn.h:151
virtual bool Compare(const FlowMgmtKey *rhs) const
bool IsLess(const FlowMgmtKey *rhs) const
Definition: flow_mgmt_key.h:51
static Type AddrToType(const IpAddress &addr)
uint8_t plen() const
DISALLOW_COPY_AND_ASSIGN(InterfaceFlowMgmtKey)
InterfaceFlowMgmtKey(const Interface *intf)
NhFlowMgmtKey(const NextHop *nh)
virtual ~InetRouteFlowMgmtKey()
BridgeRouteFlowMgmtKey(const BridgeRouteEntry *rt)
virtual FlowMgmtKey * Clone()
static char ByteValue(const InetRouteFlowMgmtKey *rt, std::size_t idx)
FlowMgmtKey(Type type, const DBEntry *db_entry)
Definition: flow_mgmt_key.h:29
AclFlowMgmtKey(const AclDBEntry *acl, const AclEntryIDList *ace_id_list)
Definition: flow_mgmt_key.h:84
FlowEvent::Event FreeDBEntryEvent() const
Definition: flow_mgmt_key.cc:8
virtual ~InterfaceFlowMgmtKey()
void set_db_entry(const DBEntry *db_entry)
Definition: flow_event.h:151
virtual ~NhFlowMgmtKey()
DISALLOW_COPY_AND_ASSIGN(AclFlowMgmtKey)
FlowMgmtKey * Clone()
virtual FlowMgmtKey * Clone()
virtual FlowMgmtKey * Clone()
DISALLOW_COPY_AND_ASSIGN(InetRouteFlowMgmtKey)
virtual bool UseDBEntry() const
virtual bool Compare(const FlowMgmtKey *rhs) const
const boost::uuids::uuid & uuid() const
const DBEntry * db_entry_
Definition: flow_mgmt_key.h:70
Patricia::Node node_
DISALLOW_COPY_AND_ASSIGN(VnFlowMgmtKey)
HealthCheckInstanceBase * bgp_health_check_instance_
void StartHealthCheck(Agent *agent, FlowEntry *flow, const boost::uuids::uuid &hc_uuid)
InetRouteFlowMgmtKey(const InetUnicastRouteEntry *route)
virtual bool Compare(const FlowMgmtKey *rhs) const
Definition: flow_mgmt_key.h:47
VnFlowMgmtKey(const VnEntry *vn)
Definition: acl.h:92
void set_ace_id_list(const AclEntryIDList *list)
InetRouteFlowMgmtKey(uint32_t vrf_id, const IpAddress &ip, uint8_t plen)
virtual bool UseDBEntry() const
bool Match(const IpAddress &match_ip) const