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 5733 : OriginVnPathSpec() : BgpAttribute(OriginVnPath, kFlags) { } 29 3254 : 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 16680 : explicit OriginVnPath(OriginVnPathDB *ovnpath_db) 43 16680 : : ovnpath_db_(ovnpath_db) { 44 16680 : refcount_ = 0; 45 16680 : } 46 922 : explicit OriginVnPath(const OriginVnPath &rhs) 47 922 : : ovnpath_db_(rhs.ovnpath_db_), 48 922 : origin_vns_(rhs.origin_vns_) { 49 922 : refcount_ = 0; 50 922 : } 51 : explicit OriginVnPath(OriginVnPathDB *ovnpath_db, 52 : const OriginVnPathSpec spec); 53 37688 : 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 3390 : 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 465741 : inline int intrusive_ptr_add_ref(const OriginVnPath *covnpath) { 86 931482 : return covnpath->refcount_.fetch_add(1); 87 : } 88 : 89 292268 : inline int intrusive_ptr_del_ref(const OriginVnPath *covnpath) { 90 584536 : return covnpath->refcount_.fetch_sub(1); 91 : } 92 : 93 173491 : inline void intrusive_ptr_release(const OriginVnPath *covnpath) { 94 173491 : int prev = covnpath->refcount_.fetch_sub(1); 95 173491 : if (prev == 1) { 96 1838 : OriginVnPath *ovnpath = const_cast<OriginVnPath *>(covnpath); 97 1838 : ovnpath->Remove(); 98 1838 : assert(ovnpath->refcount_ == 0); 99 1838 : delete ovnpath; 100 : } 101 173491 : } 102 : 103 : typedef boost::intrusive_ptr<const OriginVnPath> OriginVnPathPtr; 104 : 105 : struct OriginVnPathCompare { 106 588111 : bool operator()(const OriginVnPath *lhs, const OriginVnPath *rhs) const { 107 588111 : 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_