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