OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bgp_origin_vn_path.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef SRC_BGP_BGP_ORIGIN_VN_PATH_H_
6 #define SRC_BGP_BGP_ORIGIN_VN_PATH_H_
7 
8 #include <boost/array.hpp>
9 #include <boost/intrusive_ptr.hpp>
10 #include <tbb/atomic.h>
11 
12 #include <set>
13 #include <string>
14 #include <vector>
15 
16 #include "base/parse_object.h"
17 #include "base/util.h"
18 #include "bgp/bgp_attr_base.h"
19 #include "bgp/bgp_common.h"
20 
21 class BgpAttr;
22 class OriginVnPathDB;
23 class BgpServer;
24 
25 struct OriginVnPathSpec : public BgpAttribute {
26  static const int kSize = -1;
27  static const uint8_t kFlags = Optional | Transitive;
29  explicit OriginVnPathSpec(const BgpAttribute &rhs) : BgpAttribute(rhs) { }
30  std::vector<uint64_t> origin_vns;
31  virtual int CompareTo(const BgpAttribute &rhs_attr) const;
32  virtual void ToCanonical(BgpAttr *attr);
33  virtual std::string ToString() const;
34  virtual size_t EncodeLength() const;
35 };
36 
37 class OriginVnPath {
38 public:
39  typedef boost::array<uint8_t, 8> OriginVnValue;
40  typedef std::vector<OriginVnValue> OriginVnList;
41 
42  explicit OriginVnPath(OriginVnPathDB *ovnpath_db)
43  : ovnpath_db_(ovnpath_db) {
44  refcount_ = 0;
45  }
46  explicit OriginVnPath(const OriginVnPath &rhs)
47  : ovnpath_db_(rhs.ovnpath_db_),
49  refcount_ = 0;
50  }
51  explicit OriginVnPath(OriginVnPathDB *ovnpath_db,
52  const OriginVnPathSpec spec);
53  virtual ~OriginVnPath() { }
54  virtual void Remove();
55 
56  bool Contains(const OriginVnValue &value) const;
57  bool Contains(as_t asn, uint32_t vn_index) const;
58  int CompareTo(const OriginVnPath &rhs) const;
59 
60  const OriginVnList &origin_vns() const { return origin_vns_; }
61 
62  friend std::size_t hash_value(const OriginVnPath &ovnpath) {
63  size_t hash = 0;
64  for (OriginVnList::const_iterator it = ovnpath.origin_vns_.begin();
65  it != ovnpath.origin_vns_.end(); ++it) {
66  boost::hash_range(hash, it->begin(), it->end());
67  }
68  return hash;
69  }
70 
71 private:
72  friend int intrusive_ptr_add_ref(const OriginVnPath *covnpath);
73  friend int intrusive_ptr_del_ref(const OriginVnPath *covnpath);
74  friend void intrusive_ptr_release(const OriginVnPath *covnpath);
75  friend class OriginVnPathDB;
76  friend class BgpAttrTest;
77 
78  void Prepend(const OriginVnValue &value);
79 
80  mutable tbb::atomic<int> refcount_;
83 };
84 
85 inline int intrusive_ptr_add_ref(const OriginVnPath *covnpath) {
86  return covnpath->refcount_.fetch_and_increment();
87 }
88 
89 inline int intrusive_ptr_del_ref(const OriginVnPath *covnpath) {
90  return covnpath->refcount_.fetch_and_decrement();
91 }
92 
93 inline void intrusive_ptr_release(const OriginVnPath *covnpath) {
94  int prev = covnpath->refcount_.fetch_and_decrement();
95  if (prev == 1) {
96  OriginVnPath *ovnpath = const_cast<OriginVnPath *>(covnpath);
97  ovnpath->Remove();
98  assert(ovnpath->refcount_ == 0);
99  delete ovnpath;
100  }
101 }
102 
103 typedef boost::intrusive_ptr<const OriginVnPath> OriginVnPathPtr;
104 
106  bool operator()(const OriginVnPath *lhs, const OriginVnPath *rhs) {
107  return lhs->CompareTo(*rhs) < 0;
108  }
109 };
110 
111 class OriginVnPathDB : public BgpPathAttributeDB<OriginVnPath, OriginVnPathPtr,
112  OriginVnPathSpec,
113  OriginVnPathCompare,
114  OriginVnPathDB> {
115 public:
116  explicit OriginVnPathDB(BgpServer *server);
118  const OriginVnPath::OriginVnValue &value);
119 
120 private:
122 };
123 
124 #endif // SRC_BGP_BGP_ORIGIN_VN_PATH_H_
int intrusive_ptr_add_ref(const AsPath *cpath)
Definition: bgp_aspath.h:147
std::vector< OriginVnValue > OriginVnList
int CompareTo(const OriginVnPath &rhs) const
DISALLOW_COPY_AND_ASSIGN(OriginVnPathDB)
virtual void ToCanonical(BgpAttr *attr)
OriginVnPathPtr PrependAndLocate(const OriginVnPath *ovnpath, const OriginVnPath::OriginVnValue &value)
virtual size_t EncodeLength() const
uint32_t as_t
Definition: bgp_common.h:21
friend class BgpAttrTest
boost::intrusive_ptr< const OriginVnPath > OriginVnPathPtr
friend std::size_t hash_value(const OriginVnPath &ovnpath)
friend void intrusive_ptr_release(const OriginVnPath *covnpath)
virtual ~OriginVnPath()
friend int intrusive_ptr_add_ref(const OriginVnPath *covnpath)
static const uint8_t kFlags
std::vector< uint64_t > origin_vns
virtual void Remove()
int intrusive_ptr_del_ref(const AsPath *cpath)
Definition: bgp_aspath.h:151
OriginVnPathDB(BgpServer *server)
OriginVnPath(OriginVnPathDB *ovnpath_db)
virtual int CompareTo(const BgpAttribute &rhs_attr) const
const OriginVnList & origin_vns() const
OriginVnPathSpec(const BgpAttribute &rhs)
virtual std::string ToString() const
bool Contains(const OriginVnValue &value) const
void intrusive_ptr_release(const AsPath *cpath)
Definition: bgp_aspath.h:155
OriginVnList origin_vns_
OriginVnPathDB * ovnpath_db_
friend int intrusive_ptr_del_ref(const OriginVnPath *covnpath)
void Prepend(const OriginVnValue &value)
OriginVnPath(const OriginVnPath &rhs)
bool operator()(const OriginVnPath *lhs, const OriginVnPath *rhs)
boost::array< uint8_t, 8 > OriginVnValue
tbb::atomic< int > refcount_
static const int kSize