OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
metadata_ip.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <oper/route_common.h>
8 #include <oper/vm_interface.h>
10 #include <oper/metadata_ip.h>
11 
13 
15  MetaDataIpType type, bool insert_metadata_ip, bool ipv4) :
16  allocator_(allocator), index_(-1), intf_(intf),
17  intf_label_(MplsTable::kInvalidLabel), service_ip_(), destination_ip_(),
18  active_(false), type_(type), insert_metadata_ip_(insert_metadata_ip),
19  ipv4_(ipv4) {
20  if (insert_metadata_ip_) {
22  intf->InsertMetaDataIpInfo(this);
23  }
24 }
25 
27  uint16_t index, bool ipv4) :
28  allocator_(allocator), index_(index), intf_(intf),
29  intf_label_(MplsTable::kInvalidLabel), service_ip_(), destination_ip_(),
30  active_(false), type_(LINKLOCAL), ipv4_(ipv4) {
33 }
34 
36  if (type_ == HEALTH_CHECK) {
37  if (insert_metadata_ip_) {
39  allocator_->ReleaseIndex(this);
40  }
41  } else {
43  allocator_->ReleaseIndex(this);
44  }
45  set_active(false);
46 }
47 
48 template<>
49 Ip4Address MetaDataIp::IndexToIpAddress<Ip4Address>(uint32_t idx) {
50  uint32_t ip = METADATA_IP_ADDR & 0xFFFF0000;
51  ip += (((uint32_t)idx) & 0xFFFF);
52  return Ip4Address(ip);
53 }
54 
55 template<>
56 Ip6Address MetaDataIp::IndexToIpAddress<Ip6Address>(uint32_t idx) {
57  Ip6Address::bytes_type ip_bytes =
58  {0xFE, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59  (uint32_t)idx & 0xFF00,
60  (uint32_t)idx & 0x00FF};
61  return Ip6Address(ip_bytes);
62 }
63 
64 template<>
65 uint32_t MetaDataIp::IpAddressToIndex<Ip4Address>(const Ip4Address& ip) {
66  uint32_t idx = ip.to_ulong();
67  if ((idx & 0xFFFF0000) != (METADATA_IP_ADDR & 0xFFFF0000))
68  return (0xFFFF + 1);
69  idx &= 0xFFFF;
70  return idx;
71 }
72 
73 template<>
74 uint32_t MetaDataIp::IpAddressToIndex<Ip6Address>(const Ip6Address& ip) {
75  uint32_t idx = ip.to_bytes()[15] + (ip.to_bytes()[14] << 8);
76  return idx;
77 }
78 
80  if (ipv4_) {
81  return GetLinkLocalIp4();
82  }
83  return GetLinkLocalIp6();
84 }
85 
87  return IndexToIpAddress<Ip4Address>(index_);
88 }
89 
91  return IndexToIpAddress<Ip6Address>(index_);
92 }
93 
95  // check if explicit configuration of service ip is present for
96  // this metadata ip
97  if (service_ip_ == kDefaultIp) {
99  if (type_ == HEALTH_CHECK) {
100  // for metadata IP type Health Check first verify
101  // if service health check ip is available on interface
102  service_ip = intf_->service_health_check_ip();
103  if (service_ip != kDefaultIp) {
104  return service_ip;
105  }
106  }
107  // check if service ip on the primary ip addr of interface
108  // is available
109  service_ip = intf_->GetServiceIp(intf_->primary_ip_addr());
110  if (service_ip != kDefaultIp) {
111  return service_ip;
112  }
113  // if service IP is not available fallback to MetaData IP
115  }
116  return service_ip_;
117 }
118 
120  if (destination_ip_.to_v4() == kDefaultIp) {
121  return intf_->primary_ip_addr();
122  }
123  return destination_ip_;
124 }
125 
127  destination_ip_ = dst_ip;
128 }
129 
130 void MetaDataIp::set_active(bool active) {
131  if (active_ == active) {
132  return;
133  }
134  active_ = active;
135  UpdateRoute();
136 }
137 
139  if (intf_label_ != intf_->label()) {
140  intf_label_ = intf_->label();
141  UpdateRoute();
142  }
143 }
144 
146  return intf_;
147 }
148 
151  intf_label_ = intf_->label();
152  allocator_->AddFabricRoute(this);
153  } else {
154  allocator_->DelFabricRoute(this);
156  }
157 }
158 
160  uint16_t end) :
161  agent_(agent), index_table_(), start_(start), end_(end) {
162 }
163 
165 }
166 
168  uint16_t index = end_ - id;
169  return index_table_.At(index);
170 }
171 
173  uint16_t index = index_table_.Insert(ip);
174  assert(index <= end_ && (end_ - index) >= start_);
175  return (end_ - index);
176 }
177 
179  assert(id <= end_ && id >= start_);
180  uint16_t index = end_ - id;
181  assert(index == index_table_.InsertAtIndex(index, ip));
182 }
183 
185  uint16_t index = end_ - ip->index_;
186  index_table_.Remove(index);
187 }
188 
190  if (ip->intf_->vmi_type() == VmInterface::VHOST ||
191  ip->intf_->vn() == NULL || ip->intf_->vrf() == NULL) {
192  return false;
193  }
194 
195  return true;
196 }
197 
199  if (!CanAddRoute(ip)) {
200  return;
201  }
202  PathPreference path_preference;
203  EcmpLoadBalance ecmp_load_balance;
204  ip->intf_->SetPathPreference(&path_preference, false, Ip4Address(0));
205 
206  VnListType vn_list;
207  vn_list.insert(ip->intf_->vn()->GetName());
208 
209  if (ip->ipv4_) {
211  static_cast<InetUnicastAgentRouteTable*>
213  agent_->fabric_vrf_name()));
214 
215  table->AddLocalVmRouteReq(
217  ip->GetLinkLocalIp(), 32, ip->intf_->GetUuid(),
218  vn_list, ip->intf_->label(), SecurityGroupList(),
219  TagList(), CommunityList(), true, path_preference,
220  Ip4Address(0), ecmp_load_balance, false, false, false,
221  ip->intf_->name());
222  } else {
224  static_cast<InetUnicastAgentRouteTable*>
226  agent_->fabric_vrf_name()));
227 
228  table->AddLocalVmRouteReq(
230  ip->GetLinkLocalIp(), 128, ip->intf_->GetUuid(),
231  vn_list, ip->intf_->label(), SecurityGroupList(),
232  TagList(), CommunityList(), true, path_preference,
233  Ip6Address(), ecmp_load_balance, false, false, false,
234  ip->intf_->name());
235  }
236 }
237 
239  if (ip->ipv4_) {
242  ip->GetLinkLocalIp(), 32);
243  } else {
246  ip->GetLinkLocalIp(), 128);
247  }
248 }
249 
void AddFabricRoute(MetaDataIp *ip)
Definition: metadata_ip.cc:198
EntryType At(size_t index) const
Definition: index_vector.h:32
void SetPathPreference(PathPreference *pref, bool ecmp, const IpAddress &dependent_ip) const
IpAddress service_ip_
Definition: metadata_ip.h:81
bool CanAddRoute(MetaDataIp *ip)
Definition: metadata_ip.cc:189
bool ipv4_
Definition: metadata_ip.h:86
void AddLocalVmRouteReq(const Peer *peer, const string &vm_vrf, const IpAddress &addr, uint8_t plen, LocalVmRoute *data)
const boost::uuids::uuid & GetUuid() const
Definition: interface.h:113
MetaDataIp * FindIndex(uint16_t id)
Definition: metadata_ip.cc:167
IpAddress destination_ip_
Definition: metadata_ip.h:82
boost::asio::ip::address IpAddress
Definition: address.h:13
MetaDataIpAllocator(Agent *agent, uint16_t start, uint16_t end)
Definition: metadata_ip.cc:159
IpAddress destination_ip() const
Definition: metadata_ip.cc:119
std::vector< int > SecurityGroupList
Definition: agent.h:201
IpAddress service_health_check_ip() const
IndexVector< MetaDataIp * > index_table_
Definition: metadata_ip.h:118
#define METADATA_IP_ADDR
Definition: agent.h:294
IpAddress service_ip() const
Definition: metadata_ip.cc:94
IpAddress GetServiceIp(const IpAddress &ip) const
VrfEntry * vrf() const
Definition: interface.h:115
const Interface * GetInterface() const
Definition: metadata_ip.cc:145
InetUnicastAgentRouteTable * GetInet4UnicastRouteTable(const std::string &vrf_name)
Definition: vrf.cc:898
uint32_t label() const
Definition: interface.h:127
void Remove(size_t index)
Definition: index_vector.h:78
const std::string & fabric_vrf_name() const
Definition: agent.h:903
size_t Insert(EntryType entry)
Definition: index_vector.h:40
MetaDataIp(MetaDataIpAllocator *allocator, VmInterface *intf, MetaDataIpType type, bool insert_metadata_ip, bool ipv4=true)
Definition: metadata_ip.cc:14
const Peer * link_local_peer() const
Definition: agent.h:1024
static void Delete(const Peer *peer, const string &vrf_name, const IpAddress &addr, uint8_t plen)
uint8_t type
Definition: load_balance.h:109
Ip6Address GetLinkLocalIp6() const
Definition: metadata_ip.cc:90
Definition: agent.h:358
void UpdateRoute()
Definition: metadata_ip.cc:149
VmInterface * intf_
Definition: metadata_ip.h:79
boost::asio::ip::address_v6 Ip6Address
Definition: address.h:15
uint32_t intf_label_
Definition: metadata_ip.h:80
void DelFabricRoute(MetaDataIp *ip)
Definition: metadata_ip.cc:238
IpAddress GetLinkLocalIp() const
Definition: metadata_ip.cc:79
uint16_t index_
Definition: metadata_ip.h:77
const VnEntry * vn() const
std::vector< std::string > CommunityList
Definition: bgp_config.h:347
std::set< std::string > VnListType
Definition: agent.h:212
void ReleaseIndex(MetaDataIp *ip)
Definition: metadata_ip.cc:184
uint16_t AllocateIndex(MetaDataIp *ip)
Definition: metadata_ip.cc:172
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
static const uint32_t kInvalidLabel
Definition: mpls.h:101
void DeleteMetaDataIpInfo(MetaDataIp *mip)
VrfTable * vrf_table() const
Definition: agent.h:485
const Ip4Address & primary_ip_addr() const
bool insert_metadata_ip_
Definition: metadata_ip.h:85
Definition: mpls.h:99
void set_destination_ip(const IpAddress &dst_ip)
Definition: metadata_ip.cc:126
void InsertMetaDataIpInfo(MetaDataIp *mip)
VmInterface::VmiType vmi_type() const
static const IpAddress kDefaultIp
Definition: metadata_ip.h:31
void set_active(bool active)
Definition: metadata_ip.cc:130
MetaDataIpType type_
Definition: metadata_ip.h:84
const std::string & name() const
Definition: interface.h:114
const string & GetName() const
Definition: vn.h:162
Ip4Address GetLinkLocalIp4() const
Definition: metadata_ip.cc:86
InetUnicastAgentRouteTable * GetInet6UnicastRouteTable(const std::string &vrf_name)
Definition: vrf.cc:922
bool active_
Definition: metadata_ip.h:83
void UpdateInterfaceCb()
Definition: metadata_ip.cc:138
MetaDataIpAllocator * allocator_
Definition: metadata_ip.h:75
size_t InsertAtIndex(uint32_t index, EntryType entry)
Definition: index_vector.h:54
std::vector< int > TagList
Definition: agent.h:202