Line data Source code
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 : 11 : #include <set> 12 : #include <string> 13 : #include <vector> 14 : #include <atomic> 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; 28 5722 : OriginVnPathSpec() : BgpAttribute(OriginVnPath, kFlags) { } 29 3234 : 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 16628 : explicit OriginVnPath(OriginVnPathDB *ovnpath_db) 43 16628 : : ovnpath_db_(ovnpath_db) { 44 16628 : refcount_ = 0; 45 16628 : } 46 899 : explicit OriginVnPath(const OriginVnPath &rhs) 47 899 : : ovnpath_db_(rhs.ovnpath_db_), 48 899 : origin_vns_(rhs.origin_vns_) { 49 899 : refcount_ = 0; 50 899 : } 51 : explicit OriginVnPath(OriginVnPathDB *ovnpath_db, 52 : const OriginVnPathSpec spec); 53 37496 : 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 3368 : const OriginVnList &origin_vns() const { return origin_vns_; } 61 : 62 0 : friend std::size_t hash_value(const OriginVnPath &ovnpath) { 63 0 : size_t hash = 0; 64 0 : for (OriginVnList::const_iterator it = ovnpath.origin_vns_.begin(); 65 0 : it != ovnpath.origin_vns_.end(); ++it) { 66 0 : boost::hash_range(hash, it->begin(), it->end()); 67 : } 68 0 : 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 std::atomic<int> refcount_; 81 : OriginVnPathDB *ovnpath_db_; 82 : OriginVnList origin_vns_; 83 : }; 84 : 85 360496 : inline int intrusive_ptr_add_ref(const OriginVnPath *covnpath) { 86 720992 : return covnpath->refcount_.fetch_add(1); 87 : } 88 : 89 188035 : inline int intrusive_ptr_del_ref(const OriginVnPath *covnpath) { 90 376070 : return covnpath->refcount_.fetch_sub(1); 91 : } 92 : 93 172479 : inline void intrusive_ptr_release(const OriginVnPath *covnpath) { 94 172479 : int prev = covnpath->refcount_.fetch_sub(1); 95 172479 : if (prev == 1) { 96 1844 : OriginVnPath *ovnpath = const_cast<OriginVnPath *>(covnpath); 97 1844 : ovnpath->Remove(); 98 1844 : assert(ovnpath->refcount_ == 0); 99 1844 : delete ovnpath; 100 : } 101 172479 : } 102 : 103 : typedef boost::intrusive_ptr<const OriginVnPath> OriginVnPathPtr; 104 : 105 : struct OriginVnPathCompare { 106 379459 : bool operator()(const OriginVnPath *lhs, const OriginVnPath *rhs) const { 107 379459 : 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); 117 : OriginVnPathPtr PrependAndLocate(const OriginVnPath *ovnpath, 118 : const OriginVnPath::OriginVnValue &value); 119 : 120 : private: 121 : DISALLOW_COPY_AND_ASSIGN(OriginVnPathDB); 122 : }; 123 : 124 : #endif // SRC_BGP_BGP_ORIGIN_VN_PATH_H_