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