OpenSDN source code
bgp_origin_vn_path.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 
9 #include <algorithm>
10 #include <string>
11 
12 #include "bgp/bgp_proto.h"
14 
15 using std::string;
16 using std::vector;
17 
19  char repr[80];
20  snprintf(repr, sizeof(repr), "OriginVnPath <code: %d, flags: %02x> : %zu",
21  code, flags, origin_vns.size());
22  return string(repr);
23 }
24 
26  int ret = BgpAttribute::CompareTo(rhs);
27  if (ret != 0)
28  return ret;
30  static_cast<const OriginVnPathSpec &>(rhs).origin_vns);
31  return 0;
32 }
33 
35  attr->set_origin_vn_path(this);
36 }
37 
39  return origin_vns.size() * sizeof(uint64_t);
40 }
41 
43  const OriginVnPathSpec spec)
44  : ovnpath_db_(ovnpath_db) {
45  refcount_ = 0;
46  for (vector<uint64_t>::const_iterator it = spec.origin_vns.begin();
47  it < spec.origin_vns.end(); ++it) {
48  OriginVnValue value;
49  put_value(value.data(), value.size(), *it);
50  origin_vns_.push_back(value);
51  }
52 }
53 
55  ovnpath_db_->Delete(this);
56 }
57 
59  OriginVnList::iterator it = origin_vns_.begin();
60  origin_vns_.insert(it, value);
61 }
62 
63 bool OriginVnPath::Contains(as_t asn, uint32_t vn_index) const {
64  if (asn <= 0xffff) {
65  OriginVn origin_vn(asn, vn_index);
66  return Contains(origin_vn.GetExtCommunity());
67  }
68  OriginVn origin_vn4(asn, AS_TRANS);
69  OriginVn origin_vn(AS_TRANS, vn_index);
70  return (Contains(origin_vn.GetExtCommunity()) &&
71  Contains(origin_vn4.GetExtCommunity()));
72 }
73 
74 bool OriginVnPath::Contains(const OriginVnValue &val) const {
75  OriginVn in_origin_vn(val);
76  int in_vn_index = in_origin_vn.IsGlobal() ? in_origin_vn.vn_index() : 0;
77  for (OriginVnList::const_iterator it = origin_vns_.begin();
78  it != origin_vns_.end(); ++it) {
79  if (*it == val)
80  return true;
81  if (in_vn_index == 0)
82  continue;
83  OriginVn origin_vn(*it);
84  if (origin_vn.vn_index() == in_vn_index)
85  return true;
86  }
87  return false;
88 }
89 
90 int OriginVnPath::CompareTo(const OriginVnPath &rhs) const {
91  KEY_COMPARE(origin_vns_.size(), rhs.origin_vns_.size());
92 
93  OriginVnList::const_iterator it1, it2;
94  for (it1 = origin_vns_.begin(), it2 = rhs.origin_vns_.begin();
95  it1 < origin_vns_.end(); ++it1, ++it2) {
96  if (*it1 < *it2) {
97  return -1;
98  }
99  if (*it1 > *it2) {
100  return 1;
101  }
102  }
103  return 0;
104 }
105 
107 }
108 
110  const OriginVnPath::OriginVnValue &value) {
111  OriginVnPath *clone;
112  if (ovnpath) {
113  clone = new OriginVnPath(*ovnpath);
114  } else {
115  clone = new OriginVnPath(this);
116  }
117  clone->Prepend(value);
118  return Locate(clone);
119 }
#define AS_TRANS
Definition: bgp_common.h:24
uint32_t as_t
Definition: bgp_common.h:22
boost::intrusive_ptr< const OriginVnPath > OriginVnPathPtr
void set_origin_vn_path(OriginVnPathPtr ovnpath)
Definition: bgp_attr.cc:1058
uint8_t flags
Definition: bgp_attr_base.h:72
virtual int CompareTo(const BgpAttribute &rhs) const
uint8_t code
Definition: bgp_attr_base.h:70
void Delete(Type *attr)
OriginVnPathPtr PrependAndLocate(const OriginVnPath *ovnpath, const OriginVnPath::OriginVnValue &value)
OriginVnPathDB(BgpServer *server)
std::atomic< int > refcount_
virtual void Remove()
OriginVnList origin_vns_
boost::array< uint8_t, 8 > OriginVnValue
OriginVnPath(OriginVnPathDB *ovnpath_db)
int CompareTo(const OriginVnPath &rhs) const
bool Contains(const OriginVnValue &value) const
void Prepend(const OriginVnValue &value)
OriginVnPathDB * ovnpath_db_
bool IsGlobal() const
Definition: origin_vn.cc:135
int vn_index() const
Definition: origin_vn.cc:123
const bytes_type & GetExtCommunity() const
Definition: origin_vn.h:34
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:56
virtual int CompareTo(const BgpAttribute &rhs_attr) const
std::vector< uint64_t > origin_vns
virtual void ToCanonical(BgpAttr *attr)
virtual size_t EncodeLength() const
virtual std::string ToString() const
#define KEY_COMPARE(x, y)
Definition: util.h:71