Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "sandesh/sandesh_types.h"
7 : #include "sandesh/sandesh.h"
8 : #include "base/address_util.h"
9 : #include "init/agent_init.h"
10 : #include "oper/nexthop.h"
11 : #include "oper/tunnel_nh.h"
12 : #include "oper/mirror_table.h"
13 : #include "oper/route_common.h"
14 : #include "pkt/pkt_init.h"
15 : #include "services/arp_proto.h"
16 : #include "services/services_sandesh.h"
17 : #include "services_init.h"
18 : #include "mac_learning/mac_learning_proto.h"
19 :
20 0 : ArpProto::ArpProto(Agent *agent, boost::asio::io_context &io,
21 0 : bool run_with_vrouter) :
22 : Proto(agent, "Agent::Services", PktHandler::ARP, io),
23 0 : run_with_vrouter_(run_with_vrouter), ip_fabric_interface_index_(-1),
24 0 : ip_fabric_interface_(NULL), max_retries_(kMaxRetries),
25 0 : retry_timeout_(kRetryTimeout), aging_timeout_(kAgingTimeout) {
26 : // limit the number of entries in the workqueue
27 0 : work_queue_.SetSize(agent->params()->services_queue_limit());
28 0 : work_queue_.SetBounded(true);
29 :
30 0 : vrf_table_listener_id_ = agent->vrf_table()->Register(
31 : boost::bind(&ArpProto::VrfNotify, this, _1, _2));
32 0 : interface_table_listener_id_ = agent->interface_table()->Register(
33 : boost::bind(&ArpProto::InterfaceNotify,
34 : this, _2));
35 0 : nexthop_table_listener_id_ = agent->nexthop_table()->Register(
36 : boost::bind(&ArpProto::NextHopNotify, this, _2));
37 0 : }
38 :
39 0 : ArpProto::~ArpProto() {
40 0 : }
41 :
42 0 : void ArpProto::Shutdown() {
43 : // we may have arp entries in arp cache without ArpNH, empty them
44 0 : for (ArpIterator it = arp_cache_.begin(); it != arp_cache_.end(); ) {
45 0 : it = DeleteArpEntry(it);
46 : }
47 :
48 0 : for (GratuitousArpIterator it = gratuitous_arp_cache_.begin();
49 0 : it != gratuitous_arp_cache_.end(); it++) {
50 0 : for (ArpEntrySet::iterator sit = it->second.begin();
51 0 : sit != it->second.end();) {
52 0 : ArpEntry *entry = *sit;
53 0 : it->second.erase(sit++);
54 0 : delete entry;
55 : }
56 : }
57 0 : gratuitous_arp_cache_.clear();
58 0 : agent_->vrf_table()->Unregister(vrf_table_listener_id_);
59 0 : agent_->interface_table()->Unregister(interface_table_listener_id_);
60 0 : agent_->nexthop_table()->Unregister(nexthop_table_listener_id_);
61 0 : }
62 :
63 0 : ProtoHandler *ArpProto::AllocProtoHandler(boost::shared_ptr<PktInfo> info,
64 : boost::asio::io_context &io) {
65 0 : return new ArpHandler(agent(), info, io);
66 : }
67 :
68 0 : void ArpProto::VrfNotify(DBTablePartBase *part, DBEntryBase *entry) {
69 0 : VrfEntry *vrf = static_cast<VrfEntry *>(entry);
70 : ArpVrfState *state;
71 :
72 0 : state = static_cast<ArpVrfState *>(entry->GetState(part->parent(),
73 : vrf_table_listener_id_));
74 0 : if (entry->IsDeleted()) {
75 0 : if (state) {
76 0 : for (ArpProto::ArpIterator it = arp_cache_.begin();
77 0 : it != arp_cache_.end();) {
78 0 : ArpEntry *arp_entry = it->second;
79 0 : if (arp_entry->key().vrf == vrf && arp_entry->DeleteArpRoute()) {
80 0 : it = DeleteArpEntry(it);
81 : } else
82 0 : it++;
83 : }
84 0 : for (GratuitousArpIterator it = gratuitous_arp_cache_.begin();
85 0 : it != gratuitous_arp_cache_.end(); ) {
86 0 : ArpKey key = it->first;
87 0 : if (key.vrf == vrf) {
88 0 : for (ArpEntrySet::iterator sit = it->second.begin();
89 0 : sit != it->second.end();) {
90 0 : ArpEntry *entry = *sit;
91 0 : it->second.erase(sit++);
92 0 : delete entry;
93 : }
94 0 : gratuitous_arp_cache_.erase(it++);
95 : }else {
96 0 : it++;
97 : }
98 : }
99 0 : state->Delete();
100 : }
101 0 : return;
102 : }
103 :
104 0 : if (!state){
105 0 : state = new ArpVrfState(agent_, this, vrf,
106 0 : vrf->GetInet4UnicastRouteTable(),
107 0 : vrf->GetEvpnRouteTable());
108 0 : state->route_table_listener_id = vrf->
109 0 : GetInet4UnicastRouteTable()->
110 0 : Register(boost::bind(&ArpVrfState::RouteUpdate, state, _1, _2));
111 0 : state->evpn_route_table_listener_id = vrf->GetEvpnRouteTable()->
112 0 : Register(boost::bind(&ArpVrfState::EvpnRouteUpdate, state, _1, _2));
113 0 : entry->SetState(part->parent(), vrf_table_listener_id_, state);
114 : }
115 : }
116 :
117 0 : void intrusive_ptr_add_ref(ArpPathPreferenceState *aps) {
118 0 : aps->refcount_++;
119 0 : }
120 :
121 0 : void intrusive_ptr_release(ArpPathPreferenceState *aps) {
122 0 : ArpVrfState *state = aps->vrf_state();
123 0 : int prev = aps->refcount_.fetch_sub(1);
124 0 : if (prev == 1) {
125 0 : state->Erase(aps->ip(), aps->plen());
126 0 : delete aps;
127 : }
128 0 : }
129 :
130 0 : ArpPathPreferenceState::ArpPathPreferenceState(ArpVrfState *state,
131 : uint32_t vrf_id,
132 : const IpAddress &ip,
133 0 : uint8_t plen):
134 0 : vrf_state_(state), arp_req_timer_(NULL), vrf_id_(vrf_id),
135 0 : vm_ip_(ip), plen_(plen) {
136 0 : refcount_ = 0;
137 0 : }
138 :
139 0 : ArpPathPreferenceState::~ArpPathPreferenceState() {
140 0 : if (arp_req_timer_) {
141 0 : arp_req_timer_->Cancel();
142 0 : TimerManager::DeleteTimer(arp_req_timer_);
143 : }
144 0 : assert(refcount_ == 0);
145 0 : }
146 :
147 0 : void ArpPathPreferenceState::StartTimer() {
148 0 : if (arp_req_timer_ == NULL) {
149 0 : arp_req_timer_ = TimerManager::CreateTimer(
150 0 : *(vrf_state_->agent->event_manager()->io_service()),
151 : "Arp Entry timer for VM",
152 : TaskScheduler::GetInstance()->
153 : GetTaskId("Agent::Services"), PktHandler::ARP);
154 : }
155 0 : arp_req_timer_->Start(kTimeout,
156 : boost::bind(&ArpPathPreferenceState::SendArpRequest,
157 : this));
158 0 : }
159 :
160 0 : bool ArpPathPreferenceState::SendArpRequest(WaitForTrafficIntfMap
161 : &wait_for_traffic_map,
162 : ArpTransmittedIntfMap
163 : &arp_transmitted_map) {
164 0 : bool ret = false;
165 0 : boost::shared_ptr<PktInfo> pkt(new PktInfo(vrf_state_->agent,
166 : ARP_TX_BUFF_LEN,
167 0 : PktHandler::ARP, 0));
168 0 : ArpHandler arp_handler(vrf_state_->agent, pkt,
169 0 : *(vrf_state_->agent->event_manager()->io_service()));
170 :
171 0 : WaitForTrafficIntfMap::iterator it = wait_for_traffic_map.begin();
172 0 : for (;it != wait_for_traffic_map.end(); it++) {
173 : const VmInterface *vm_intf = static_cast<const VmInterface *>(
174 0 : vrf_state_->agent->interface_table()->FindInterface(it->first));
175 0 : if (!vm_intf) {
176 0 : continue;
177 : }
178 :
179 0 : InterfaceArpPathPreferenceInfo &data = it->second;
180 0 : bool inserted = arp_transmitted_map.insert(it->first).second;
181 0 : ++data.arp_retry_count;
182 0 : if (inserted == false) {
183 : //ARP request already sent due to IP route
184 0 : continue;
185 : }
186 0 : if ((plen_ != 32) && (!data.prev_responded_ip.is_unspecified())) {
187 0 : if ((data.arp_send_count >= 1) && (data.arp_reply_count == 0)) {
188 0 : ++data.arp_failure_count;
189 : }
190 0 : if (data.arp_failure_count >= ArpProto::kMaxFailures) {
191 0 : data.prev_responded_ip = Ip4Address(0);
192 0 : data.arp_failure_count = 0;
193 0 : data.arp_reply_count = 0;
194 0 : data.arp_send_count = 0;
195 : }
196 : }
197 0 : ++data.arp_send_count;
198 :
199 : // Post message to delete MIL entry.
200 0 : MacAddress mil_mac = vrf_state_->agent->mac_learning_proto()->
201 : GetMacIpLearningTable()->GetPairedMacAddress(
202 0 : vm_intf->vrf_id(), ip());
203 0 : if (mil_mac != MacAddress()) {
204 0 : ++data.arp_try_count;
205 0 : if (data.arp_try_count == kArpTryCount) {
206 0 : IpAddress ip = vm_ip_;
207 0 : MacAddress mac = mil_mac;
208 0 : vrf_state_->agent->mac_learning_proto()->
209 0 : GetMacIpLearningTable()->MacIpEntryUnreachable(
210 : vm_intf->vrf_id(), ip, mac);
211 0 : return true;
212 : }
213 : }
214 :
215 0 : MacAddress smac = vm_intf->GetVifMac(vrf_state_->agent);
216 0 : arp_handler.SendArpRequestByPlen(vm_intf, smac, this,
217 0 : data.prev_responded_ip);
218 :
219 : // reduce the frequency of ARP requests after some tries
220 0 : if (data.arp_send_count >= kMaxRetry) {
221 0 : if ((mac() != MacAddress()) && (mac() != vm_intf->vm_mac())) {
222 0 : arp_req_timer_->Reschedule(5000);
223 0 : } else if (vm_intf->vmi_type() != VmInterface::REMOTE_VM) {
224 : // change frequency only if not in gateway mode with remote VMIs
225 0 : arp_req_timer_->Reschedule(kTimeout * 5);
226 : }
227 : }
228 :
229 0 : ret = true;
230 : }
231 0 : return ret;
232 0 : }
233 :
234 0 : bool ArpPathPreferenceState::SendArpRequest() {
235 0 : if (l3_wait_for_traffic_map_.size() == 0 &&
236 0 : evpn_wait_for_traffic_map_.size() == 0) {
237 0 : return false;
238 : }
239 :
240 0 : bool ret = false;
241 0 : ArpTransmittedIntfMap arp_transmitted_map;
242 0 : if (SendArpRequest(l3_wait_for_traffic_map_, arp_transmitted_map)) {
243 0 : ret = true;
244 : }
245 :
246 0 : if (SendArpRequest(evpn_wait_for_traffic_map_, arp_transmitted_map)) {
247 0 : ret = true;
248 : }
249 :
250 0 : return ret;
251 0 : }
252 :
253 : //Send ARP request on interface in Active-BackUp mode
254 : //So that preference of route can be incremented if the VM replies to ARP
255 0 : void ArpPathPreferenceState::SendArpRequestForAllIntf(const
256 : AgentRoute *route) {
257 0 : WaitForTrafficIntfMap new_wait_for_traffic_map;
258 0 : WaitForTrafficIntfMap wait_for_traffic_map = evpn_wait_for_traffic_map_;
259 0 : if (dynamic_cast<const InetUnicastRouteEntry *>(route)) {
260 0 : wait_for_traffic_map = l3_wait_for_traffic_map_;
261 : }
262 :
263 0 : for (Route::PathList::const_iterator it = route->GetPathList().begin();
264 0 : it != route->GetPathList().end(); it++) {
265 0 : const AgentPath *path = static_cast<const AgentPath *>(it.operator->());
266 0 : if (path->peer() &&
267 0 : path->peer()->GetType() == Peer::LOCAL_VM_PORT_PEER) {
268 0 : const NextHop *nh = path->ComputeNextHop(vrf_state_->agent);
269 0 : if (nh->GetType() != NextHop::INTERFACE) {
270 0 : continue;
271 : }
272 0 : if (path->is_health_check_service()) {
273 : // skip sending ARP request for Health Check Service IP
274 0 : continue;
275 : }
276 :
277 0 : const InterfaceNH *intf_nh =
278 : static_cast<const InterfaceNH *>(nh);
279 : const Interface *intf =
280 0 : static_cast<const Interface *>(intf_nh->GetInterface());
281 0 : if (intf->type() != Interface::VM_INTERFACE) {
282 : //Ignore non vm interface nexthop
283 0 : continue;
284 : }
285 0 : if (path->subnet_service_ip().is_v4() == false) {
286 0 : continue;
287 : }
288 0 : if (path->path_preference().IsDependentRt() == true) {
289 0 : continue;
290 : }
291 0 : uint32_t intf_id = intf->id();
292 : WaitForTrafficIntfMap::const_iterator wait_for_traffic_it =
293 0 : wait_for_traffic_map.find(intf_id);
294 0 : if (wait_for_traffic_it == wait_for_traffic_map.end()) {
295 0 : InterfaceArpPathPreferenceInfo data;
296 0 : new_wait_for_traffic_map.insert(WaitForTrafficIntfPair(intf_id,
297 : data));
298 : } else {
299 0 : new_wait_for_traffic_map.insert(WaitForTrafficIntfPair(intf_id,
300 0 : wait_for_traffic_it->second));
301 : }
302 : }
303 : }
304 :
305 0 : if (dynamic_cast<const InetUnicastRouteEntry *>(route)) {
306 0 : l3_wait_for_traffic_map_ = new_wait_for_traffic_map;
307 : } else {
308 0 : evpn_wait_for_traffic_map_ = new_wait_for_traffic_map;
309 : }
310 0 : if (new_wait_for_traffic_map.size() > 0) {
311 0 : SendArpRequest();
312 0 : StartTimer();
313 : }
314 0 : }
315 :
316 0 : ArpDBState::ArpDBState(ArpVrfState *vrf_state, uint32_t vrf_id, IpAddress ip,
317 0 : uint8_t plen) : vrf_state_(vrf_state),
318 0 : sg_list_(), tag_list_(), policy_(false), resolve_route_(false) {
319 0 : if (ip != Ip4Address(0)) {
320 0 : arp_path_preference_state_.reset(vrf_state->Locate(ip, plen));
321 : }
322 0 : }
323 :
324 0 : ArpDBState::~ArpDBState() {
325 0 : }
326 :
327 0 : void ArpDBState::UpdateMac(const InterfaceNH *nh) {
328 :
329 0 : if (nh && arp_path_preference_state_.get()) {
330 0 : arp_path_preference_state_.get()->set_mac(nh->GetDMac());
331 : }
332 :
333 :
334 0 : return;
335 : }
336 :
337 0 : void ArpDBState::UpdateArpRoutes(const InetUnicastRouteEntry *rt) {
338 0 : int plen = rt->prefix_length();
339 0 : uint32_t start_ip = rt->prefix_address().to_v4().to_ulong();
340 0 : ArpKey start_key(start_ip, rt->vrf());
341 :
342 : ArpProto::ArpIterator start_iter =
343 0 : vrf_state_->arp_proto->FindUpperBoundArpEntry(start_key);
344 :
345 :
346 0 : while (start_iter != vrf_state_->arp_proto->arp_cache().end() &&
347 0 : start_iter->first.vrf == rt->vrf() &&
348 0 : IsIp4SubnetMember(Ip4Address(start_iter->first.ip),
349 0 : rt->prefix_address().to_v4(), plen)) {
350 0 : start_iter->second->Resync(policy_, vn_list_, sg_list_, tag_list_);
351 0 : start_iter++;
352 : }
353 0 : }
354 :
355 0 : void ArpDBState::Delete(const InetUnicastRouteEntry *rt) {
356 0 : int plen = rt->prefix_length();
357 0 : uint32_t start_ip = rt->prefix_address().to_v4().to_ulong();
358 :
359 0 : ArpKey start_key(start_ip, rt->vrf());
360 :
361 : ArpProto::ArpIterator start_iter =
362 0 : vrf_state_->arp_proto->FindUpperBoundArpEntry(start_key);
363 :
364 0 : while (start_iter != vrf_state_->arp_proto->arp_cache().end() &&
365 0 : start_iter->first.vrf == rt->vrf() &&
366 0 : IsIp4SubnetMember(Ip4Address(start_iter->first.ip),
367 0 : rt->prefix_address().to_v4(), plen)) {
368 0 : ArpProto::ArpIterator tmp = start_iter++;
369 0 : if (tmp->second->DeleteArpRoute()) {
370 0 : vrf_state_->arp_proto->DeleteArpEntry(tmp->second);
371 : }
372 : }
373 0 : }
374 :
375 0 : void ArpDBState::Update(const AgentRoute *rt) {
376 0 : if (arp_path_preference_state_) {
377 0 : arp_path_preference_state_->SendArpRequestForAllIntf(rt);
378 : }
379 :
380 : const InetUnicastRouteEntry *ip_rt =
381 0 : dynamic_cast<const InetUnicastRouteEntry *>(rt);
382 0 : if (ip_rt == NULL) {
383 0 : return;
384 : }
385 0 : const NextHop *anh =ip_rt->GetActiveNextHop();
386 0 : if (anh == NULL) {
387 0 : return;
388 : }
389 :
390 0 : if (anh->GetType() == NextHop::RESOLVE) {
391 0 : resolve_route_ = true;
392 : }
393 :
394 0 : bool policy = anh->PolicyEnabled();
395 0 : const SecurityGroupList sg = ip_rt->GetActivePath()->sg_list();
396 0 : const TagList tag = ip_rt->GetActivePath()->tag_list();
397 :
398 :
399 0 : if (policy_ != policy || sg != sg_list_ || tag != tag_list_ ||
400 0 : vn_list_ != ip_rt->GetActivePath()->dest_vn_list()) {
401 0 : policy_ = policy;
402 0 : sg_list_ = sg;
403 0 : tag_list_ = tag;
404 0 : vn_list_ = ip_rt->GetActivePath()->dest_vn_list();
405 0 : if (resolve_route_) {
406 0 : UpdateArpRoutes(ip_rt);
407 : }
408 : }
409 0 : }
410 :
411 0 : void ArpVrfState::EvpnRouteUpdate(DBTablePartBase *part, DBEntryBase *entry) {
412 0 : EvpnRouteEntry *route = static_cast<EvpnRouteEntry *>(entry);
413 : /* Ignore route updates for Non-IPv4 addresses */
414 0 : if (!route->prefix_address().is_v4()) {
415 0 : return;
416 : }
417 :
418 0 : ArpDBState *state = static_cast<ArpDBState *>(entry->GetState(part->parent(),
419 : evpn_route_table_listener_id));
420 :
421 0 : if (entry->IsDeleted() || deleted) {
422 0 : if (state) {
423 0 : entry->ClearState(part->parent(), evpn_route_table_listener_id);
424 0 : delete state;
425 : }
426 0 : return;
427 : }
428 :
429 0 : if (state == NULL) {
430 0 : state = new ArpDBState(this, route->vrf_id(), route->prefix_address(),
431 0 : route->prefix_length());
432 0 : entry->SetState(part->parent(), evpn_route_table_listener_id, state);
433 : }
434 :
435 0 : state->Update(route);
436 : }
437 :
438 0 : void ArpVrfState::RouteUpdate(DBTablePartBase *part, DBEntryBase *entry) {
439 0 : InetUnicastRouteEntry *route = static_cast<InetUnicastRouteEntry *>(entry);
440 :
441 : ArpDBState *state =
442 : static_cast<ArpDBState *>
443 0 : (entry->GetState(part->parent(), route_table_listener_id));
444 :
445 0 : const InterfaceNH *intf_nh = dynamic_cast<const InterfaceNH *>(
446 0 : route->GetActiveNextHop());
447 0 : const Interface *intf = (intf_nh) ?
448 0 : static_cast<const Interface *>(intf_nh->GetInterface()) : NULL;
449 :
450 0 : ArpKey key(route->prefix_address().to_v4().to_ulong(), route->vrf());
451 0 : ArpEntry *arpentry = arp_proto->GratuitousArpEntry(key, intf);
452 0 : if (entry->IsDeleted() || deleted) {
453 0 : if (state) {
454 0 : arp_proto->DeleteGratuitousArpEntry(arpentry);
455 0 : entry->ClearState(part->parent(), route_table_listener_id);
456 0 : state->Delete(route);
457 0 : delete state;
458 : }
459 0 : return;
460 : }
461 :
462 0 : if (!state) {
463 0 : state = new ArpDBState(this, route->vrf_id(), route->prefix_address(),
464 0 : route->prefix_length());
465 0 : entry->SetState(part->parent(), route_table_listener_id, state);
466 : }
467 :
468 0 : state->UpdateMac(intf_nh);
469 :
470 0 : if (route->vrf()->GetName() == agent->fabric_vrf_name() && intf_nh &&
471 0 : route->GetActiveNextHop()->GetType() == NextHop::RECEIVE &&
472 0 : arp_proto->agent()->router_id() == route->prefix_address().to_v4()) {
473 : //Send Grat ARP
474 0 : arp_proto->AddGratuitousArpEntry(key);
475 0 : arp_proto->SendArpIpc(ArpProto::ARP_SEND_GRATUITOUS,
476 0 : route->prefix_address().to_v4().to_ulong(), route->vrf(),
477 0 : arp_proto->ip_fabric_interface());
478 : } else {
479 0 : if (intf_nh) {
480 0 : if (intf->type() == Interface::VM_INTERFACE &&
481 0 : static_cast<const VmInterface*>(intf)->IsActive()) {
482 0 : ArpKey intf_key(route->prefix_address().to_v4().to_ulong(), route->vrf());
483 0 : arp_proto->AddGratuitousArpEntry(intf_key);
484 0 : arp_proto->SendArpIpc(ArpProto::ARP_SEND_GRATUITOUS,
485 0 : route->prefix_address().to_v4().to_ulong(), intf->vrf(), intf);
486 : }
487 : }
488 : }
489 :
490 : //Check if there is a local VM path, if yes send a
491 : //ARP request, to trigger route preference state machine
492 0 : if (state && route->vrf()->GetName() != agent->fabric_vrf_name()) {
493 0 : state->Update(route);
494 : }
495 : }
496 :
497 0 : bool ArpVrfState::DeleteRouteState(DBTablePartBase *part, DBEntryBase *entry) {
498 0 : RouteUpdate(part, entry);
499 0 : return true;
500 : }
501 :
502 0 : bool ArpVrfState::DeleteEvpnRouteState(DBTablePartBase *part,
503 : DBEntryBase *entry) {
504 0 : EvpnRouteUpdate(part, entry);
505 0 : return true;
506 : }
507 :
508 :
509 0 : void ArpVrfState::Delete() {
510 0 : if (managed_delete_walk_ref.get() == NULL)
511 0 : return;
512 :
513 0 : rt_table->WalkAgain(managed_delete_walk_ref);
514 0 : if (evpn_walk_ref_.get())
515 0 : evpn_rt_table->WalkAgain(evpn_walk_ref_);
516 0 : deleted = true;
517 : }
518 :
519 0 : void ArpVrfState::WalkDone(DBTableBase *partition, ArpVrfState *state) {
520 0 : if (partition == state->rt_table) {
521 0 : state->rt_table->ReleaseWalker(state->managed_delete_walk_ref);
522 0 : state->managed_delete_walk_ref = NULL;
523 0 : state->l3_walk_completed_ = true;
524 : } else {
525 : //Get rt table from partition
526 0 : state->evpn_rt_table->ReleaseWalker(state->evpn_walk_ref_);
527 0 : state->evpn_walk_ref_ = NULL;
528 0 : state->evpn_walk_completed_ = true;
529 : }
530 0 : if (state->PreWalkDone(partition)) {
531 0 : delete state;
532 : }
533 0 : }
534 :
535 0 : bool ArpVrfState::PreWalkDone(DBTableBase *partition) {
536 0 : if (arp_proto->ValidateAndClearVrfState(vrf, this) == false) {
537 0 : return false;
538 : }
539 :
540 0 : rt_table->Unregister(route_table_listener_id);
541 0 : table_delete_ref.Reset(NULL);
542 :
543 0 : evpn_rt_table->Unregister(evpn_route_table_listener_id);
544 0 : evpn_table_delete_ref.Reset(NULL);
545 0 : return true;
546 : }
547 :
548 0 : ArpPathPreferenceState* ArpVrfState::Locate(const IpAddress &ip, uint8_t plen) {
549 0 : ArpPathPreferenceStateKey key(ip, plen);
550 0 : ArpPathPreferenceState* ptr = NULL;
551 0 : ArpPathPreferenceStateMap::iterator it = arp_path_preference_map_.find(key);
552 0 : if (it == arp_path_preference_map_.end()) {
553 0 : ptr = new ArpPathPreferenceState(this, vrf->vrf_id(), ip, plen);
554 0 : arp_path_preference_map_.insert(ArpPathPreferenceStatePair(key, ptr));
555 : } else {
556 0 : ptr = it->second;
557 : }
558 0 : return ptr;
559 : }
560 :
561 0 : void ArpVrfState::Erase(const IpAddress &ip, uint8_t plen) {
562 0 : ArpPathPreferenceStateKey key(ip, plen);
563 0 : ArpPathPreferenceStateMap::iterator it = arp_path_preference_map_.find(key);
564 0 : if (it != arp_path_preference_map_.end()) {
565 0 : arp_path_preference_map_.erase(it);
566 : }
567 0 : }
568 :
569 :
570 0 : ArpPathPreferenceState* ArpVrfState::Get(const IpAddress ip, uint8_t plen) {
571 0 : ArpPathPreferenceStateKey key(ip, plen);
572 0 : ArpPathPreferenceStateMap::iterator it = arp_path_preference_map_.find(key);
573 0 : if (it != arp_path_preference_map_.end()) {
574 0 : return it->second;
575 : }
576 0 : return NULL;
577 : }
578 :
579 0 : ArpVrfState::ArpVrfState(Agent *agent_ptr, ArpProto *proto, VrfEntry *vrf_entry,
580 0 : AgentRouteTable *table, AgentRouteTable *evpn_table):
581 0 : agent(agent_ptr), arp_proto(proto), vrf(vrf_entry), rt_table(table),
582 0 : evpn_rt_table(evpn_table), route_table_listener_id(DBTableBase::kInvalidId),
583 0 : evpn_route_table_listener_id(DBTableBase::kInvalidId),
584 0 : table_delete_ref(this, table->deleter()),
585 0 : evpn_table_delete_ref(this, evpn_table->deleter()),
586 0 : deleted(false),
587 0 : l3_walk_completed_(false), evpn_walk_completed_(false) {
588 0 : evpn_walk_ref_ = evpn_rt_table->AllocWalker(
589 : boost::bind(&ArpVrfState::DeleteEvpnRouteState, this, _1, _2),
590 0 : boost::bind(&ArpVrfState::WalkDone, _2, this));
591 0 : managed_delete_walk_ref = rt_table->AllocWalker(
592 : boost::bind(&ArpVrfState::DeleteRouteState, this, _1, _2),
593 0 : boost::bind(&ArpVrfState::WalkDone, _2, this));
594 0 : }
595 :
596 0 : ArpVrfState::~ArpVrfState() {
597 0 : assert(arp_path_preference_map_.size() == 0);
598 0 : }
599 :
600 0 : void ArpProto::InterfaceNotify(DBEntryBase *entry) {
601 0 : Interface *intf = static_cast<Interface *>(entry);
602 : ArpInterfaceState *state = static_cast<ArpInterfaceState *>
603 0 : (entry->GetState(entry->get_table_partition()->parent(),
604 : interface_table_listener_id_));
605 :
606 : /* On phy intf down, delete its associated arp route and set state to resolving */
607 0 : if (agent_->is_l3mh() && intf->type() == Interface::PHYSICAL && intf->os_oper_state() == false) {
608 0 : InterfaceArpInfo &intf_entry = ArpMapIndexToEntry(intf->id());
609 0 : ArpKeySet::iterator key_it = intf_entry.arp_key_list.begin();
610 0 : while (key_it != intf_entry.arp_key_list.end()) {
611 0 : ArpKey key = *key_it;
612 0 : ++key_it;
613 0 : ArpEntry *arp_entry = FindArpEntry(key);
614 0 : if (arp_entry) {
615 0 : arp_entry->SetState(ArpEntry::RESOLVING);
616 0 : arp_entry->DeleteArpRoute();
617 : }
618 : }
619 0 : return;
620 : }
621 :
622 0 : Interface *itf = static_cast<Interface *>(entry);
623 0 : if (entry->IsDeleted()) {
624 0 : if (state) {
625 0 : intf->ClearState(intf->get_table_partition()->parent(),
626 : interface_table_listener_id_);
627 0 : delete state;
628 : }
629 :
630 0 : InterfaceArpMap::iterator it = interface_arp_map_.find(itf->id());
631 0 : if (it != interface_arp_map_.end()) {
632 0 : InterfaceArpInfo &intf_entry = it->second;
633 0 : ArpKeySet::iterator key_it = intf_entry.arp_key_list.begin();
634 0 : while (key_it != intf_entry.arp_key_list.end()) {
635 0 : ArpKey key = *key_it;
636 0 : ++key_it;
637 0 : ArpIterator arp_it = arp_cache_.find(key);
638 0 : if (arp_it != arp_cache_.end()) {
639 0 : ArpEntry *arp_entry = arp_it->second;
640 0 : if (arp_entry->DeleteArpRoute()) {
641 0 : DeleteArpEntry(arp_it);
642 : }
643 : }
644 : }
645 0 : intf_entry.arp_key_list.clear();
646 0 : interface_arp_map_.erase(it);
647 : }
648 :
649 0 : if (itf->type() == Interface::VM_INTERFACE) {
650 : ArpKey key(static_cast<VmInterface *>
651 0 : (itf)->primary_ip_addr().to_ulong(), itf->vrf());
652 0 : ArpEntry *arpentry = GratuitousArpEntry(key, itf);
653 0 : if (arpentry) {
654 0 : DeleteGratuitousArpEntry(arpentry);
655 : }
656 : }
657 :
658 0 : if (itf->type() == Interface::PHYSICAL &&
659 0 : itf->name() == agent_->fabric_interface_name()) {
660 0 : set_ip_fabric_interface(NULL);
661 0 : set_ip_fabric_interface_index(-1);
662 : }
663 : } else {
664 0 : if (state == NULL) {
665 0 : state = new ArpInterfaceState(intf);
666 0 : intf->SetState(entry->get_table_partition()->parent(),
667 : interface_table_listener_id_, state);
668 : }
669 :
670 0 : if (itf->type() == Interface::PHYSICAL &&
671 0 : itf->name() == agent_->fabric_interface_name()) {
672 0 : set_ip_fabric_interface(itf);
673 0 : set_ip_fabric_interface_index(itf->id());
674 0 : if (run_with_vrouter_) {
675 0 : set_ip_fabric_interface_mac(itf->mac());
676 : } else {
677 0 : set_ip_fabric_interface_mac(MacAddress());
678 : }
679 : }
680 :
681 0 : if (itf->type() == Interface::VM_INTERFACE) {
682 0 : const VmInterface *vm_intf =
683 : static_cast<const VmInterface *>(itf);
684 0 : VrfEntry *forwarding_vrf = NULL;
685 0 : if (vm_intf->vrf()) {
686 0 : forwarding_vrf = vm_intf->vrf()->forwarding_vrf();
687 : }
688 0 : state->SetVrf(vm_intf->vrf(), forwarding_vrf);
689 : }
690 : }
691 : }
692 :
693 0 : ArpProto::InterfaceArpInfo& ArpProto::ArpMapIndexToEntry(uint32_t idx) {
694 0 : InterfaceArpMap::iterator it = interface_arp_map_.find(idx);
695 0 : if (it == interface_arp_map_.end()) {
696 0 : InterfaceArpInfo entry;
697 0 : std::pair<InterfaceArpMap::iterator, bool> ret;
698 0 : ret = interface_arp_map_.insert(InterfaceArpPair(idx, entry));
699 0 : return ret.first->second;
700 0 : } else {
701 0 : return it->second;
702 : }
703 : }
704 :
705 0 : void ArpProto::IncrementStatsArpRequest(uint32_t idx) {
706 0 : InterfaceArpInfo &entry = ArpMapIndexToEntry(idx);
707 0 : entry.stats.arp_req++;
708 0 : }
709 :
710 0 : void ArpProto::IncrementStatsArpReply(uint32_t idx) {
711 0 : InterfaceArpInfo &entry = ArpMapIndexToEntry(idx);
712 0 : entry.stats.arp_replies++;
713 0 : }
714 :
715 0 : void ArpProto::IncrementStatsResolved(uint32_t idx) {
716 0 : InterfaceArpInfo &entry = ArpMapIndexToEntry(idx);
717 0 : entry.stats.resolved++;
718 0 : }
719 :
720 0 : uint32_t ArpProto::ArpRequestStatsCounter(uint32_t idx) {
721 0 : InterfaceArpInfo &entry = ArpMapIndexToEntry(idx);
722 0 : return entry.stats.arp_req;
723 : }
724 :
725 0 : uint32_t ArpProto::ArpReplyStatsCounter(uint32_t idx) {
726 0 : InterfaceArpInfo &entry = ArpMapIndexToEntry(idx);
727 0 : return entry.stats.arp_replies;
728 : }
729 :
730 0 : uint32_t ArpProto::ArpResolvedStatsCounter(uint32_t idx) {
731 0 : InterfaceArpInfo &entry = ArpMapIndexToEntry(idx);
732 0 : return entry.stats.resolved;
733 : }
734 :
735 0 : void ArpProto::ClearInterfaceArpStats(uint32_t idx) {
736 0 : InterfaceArpInfo &entry = ArpMapIndexToEntry(idx);
737 0 : entry.stats.Reset();
738 0 : }
739 :
740 0 : void ArpProto::NextHopNotify(DBEntryBase *entry) {
741 0 : NextHop *nh = static_cast<NextHop *>(entry);
742 :
743 0 : switch(nh->GetType()) {
744 0 : case NextHop::ARP: {
745 0 : ArpNH *arp_nh = (static_cast<ArpNH *>(nh));
746 0 : if (arp_nh->IsDeleted()) {
747 0 : SendArpIpc(ArpProto::ARP_DELETE, arp_nh->GetIp()->to_ulong(),
748 : arp_nh->GetVrf(), arp_nh->GetInterface());
749 0 : } else if (arp_nh->IsValid() == false && arp_nh->GetInterface()) {
750 0 : SendArpIpc(ArpProto::ARP_RESOLVE, arp_nh->GetIp()->to_ulong(),
751 : arp_nh->GetVrf(), arp_nh->GetInterface());
752 : }
753 0 : break;
754 : }
755 :
756 0 : default:
757 0 : break;
758 : }
759 0 : }
760 :
761 0 : bool ArpProto::TimerExpiry(ArpKey &key, uint32_t timer_type,
762 : const Interface* itf) {
763 0 : if (arp_cache_.find(key) != arp_cache_.end() ||
764 0 : gratuitous_arp_cache_.find(key) != gratuitous_arp_cache_.end()) {
765 0 : if (itf) {
766 0 : SendArpIpc((ArpProto::ArpMsgType)timer_type, key, itf);
767 : }
768 : }
769 0 : return false;
770 : }
771 :
772 0 : void ArpProto::AddGratuitousArpEntry(ArpKey &key) {
773 0 : ArpEntrySet empty_set;
774 0 : gratuitous_arp_cache_.insert(GratuitousArpCachePair(key, empty_set));
775 0 : }
776 :
777 0 : void ArpProto::DeleteGratuitousArpEntry(ArpEntry *entry) {
778 0 : if (!entry)
779 0 : return ;
780 :
781 0 : ArpProto::GratuitousArpIterator iter = gratuitous_arp_cache_.find(entry->key());
782 0 : if (iter == gratuitous_arp_cache_.end()) {
783 0 : return;
784 : }
785 :
786 0 : iter->second.erase(entry);
787 0 : delete entry;
788 0 : if (iter->second.empty()) {
789 0 : gratuitous_arp_cache_.erase(iter);
790 : }
791 : }
792 :
793 : ArpEntry *
794 0 : ArpProto::GratuitousArpEntry(const ArpKey &key, const Interface *intf) {
795 0 : ArpProto::GratuitousArpIterator it = gratuitous_arp_cache_.find(key);
796 0 : if (it == gratuitous_arp_cache_.end())
797 0 : return NULL;
798 :
799 0 : for (ArpEntrySet::iterator sit = it->second.begin();
800 0 : sit != it->second.end(); sit++) {
801 0 : ArpEntry *entry = *sit;
802 0 : if (entry->get_interface() == intf)
803 0 : return *sit;
804 : }
805 :
806 0 : return NULL;
807 : }
808 :
809 : ArpProto::GratuitousArpIterator
810 0 : ArpProto::GratuitousArpEntryIterator(const ArpKey &key, bool *key_valid) {
811 0 : ArpProto::GratuitousArpIterator it = gratuitous_arp_cache_.find(key);
812 0 : if (it == gratuitous_arp_cache_.end())
813 0 : return it;
814 0 : const VrfEntry *vrf = key.vrf;
815 0 : if (!vrf)
816 0 : return it;
817 : const ArpVrfState *state = static_cast<const ArpVrfState *>
818 0 : (vrf->GetState(vrf->get_table_partition()->parent(),
819 : vrf_table_listener_id_));
820 : // If VRF is delete marked, do not add ARP entries to cache
821 0 : if (state == NULL || state->deleted == true)
822 0 : return it;
823 0 : *key_valid = true;
824 0 : return it;
825 : }
826 :
827 0 : void ArpProto::SendArpIpc(ArpProto::ArpMsgType type, in_addr_t ip,
828 : const VrfEntry *vrf, InterfaceConstRef itf) {
829 0 : ArpIpc *ipc = new ArpIpc(type, ip, vrf, itf);
830 0 : agent_->pkt()->pkt_handler()->SendMessage(PktHandler::ARP, ipc);
831 0 : }
832 :
833 0 : void ArpProto::SendArpIpc(ArpProto::ArpMsgType type, ArpKey &key,
834 : InterfaceConstRef itf) {
835 0 : ArpIpc *ipc = new ArpIpc(type, key, itf);
836 0 : agent_->pkt()->pkt_handler()->SendMessage(PktHandler::ARP, ipc);
837 0 : }
838 :
839 0 : bool ArpProto::AddArpEntry(ArpEntry *entry) {
840 0 : const VrfEntry *vrf = entry->key().vrf;
841 : const ArpVrfState *state = static_cast<const ArpVrfState *>
842 0 : (vrf->GetState(vrf->get_table_partition()->parent(),
843 : vrf_table_listener_id_));
844 : // If VRF is delete marked, do not add ARP entries to cache
845 0 : if (state == NULL || state->deleted == true)
846 0 : return false;
847 :
848 0 : bool ret = arp_cache_.insert(ArpCachePair(entry->key(), entry)).second;
849 0 : uint32_t intf_id = entry->get_interface()->id();
850 0 : InterfaceArpMap::iterator it = interface_arp_map_.find(intf_id);
851 0 : if (it == interface_arp_map_.end()) {
852 0 : InterfaceArpInfo intf_entry;
853 0 : intf_entry.arp_key_list.insert(entry->key());
854 0 : interface_arp_map_.insert(InterfaceArpPair(intf_id, intf_entry));
855 0 : } else {
856 0 : InterfaceArpInfo &intf_entry = it->second;
857 0 : ArpKeySet::iterator key_it = intf_entry.arp_key_list.find(entry->key());
858 0 : if (key_it == intf_entry.arp_key_list.end()) {
859 0 : intf_entry.arp_key_list.insert(entry->key());
860 : }
861 : }
862 0 : return ret;
863 : }
864 :
865 0 : bool ArpProto::DeleteArpEntry(ArpEntry *entry) {
866 0 : if (!entry)
867 0 : return false;
868 :
869 0 : ArpProto::ArpIterator iter = arp_cache_.find(entry->key());
870 0 : if (iter == arp_cache_.end()) {
871 0 : return false;
872 : }
873 :
874 0 : DeleteArpEntry(iter);
875 0 : return true;
876 : }
877 :
878 : ArpProto::ArpIterator
879 0 : ArpProto::DeleteArpEntry(ArpProto::ArpIterator iter) {
880 0 : ArpEntry *entry = iter->second;
881 0 : arp_cache_.erase(iter++);
882 0 : delete entry;
883 0 : return iter;
884 : }
885 :
886 0 : ArpEntry *ArpProto::FindArpEntry(const ArpKey &key) {
887 0 : ArpIterator it = arp_cache_.find(key);
888 0 : if (it == arp_cache_.end())
889 0 : return NULL;
890 0 : return it->second;
891 : }
892 :
893 0 : bool ArpProto::ValidateAndClearVrfState(VrfEntry *vrf,
894 : const ArpVrfState *vrf_state) {
895 0 : if (!vrf_state->deleted) {
896 0 : ARP_TRACE(Trace, "ARP state not cleared - VRF is not delete marked",
897 : "", vrf->GetName(), "");
898 0 : return false;
899 : }
900 :
901 0 : if (vrf_state->l3_walk_completed() == false) {
902 0 : return false;
903 : }
904 :
905 0 : if (vrf_state->evpn_walk_completed() == false) {
906 0 : return false;
907 : }
908 :
909 0 : if (vrf_state->managed_delete_walk_ref.get() != NULL ||
910 0 : vrf_state->evpn_walk_ref_.get() != NULL) {
911 0 : ARP_TRACE(Trace, "ARP state not cleared - Route table walk not complete",
912 : "", vrf->GetName(), "");
913 0 : return false;
914 : }
915 :
916 : DBState *state = static_cast<DBState *>
917 0 : (vrf->GetState(vrf->get_table_partition()->parent(),
918 : vrf_table_listener_id_));
919 0 : if (state) {
920 0 : vrf->ClearState(vrf->get_table_partition()->parent(),
921 : vrf_table_listener_id_);
922 : }
923 0 : return true;
924 : }
925 :
926 : ArpProto::ArpIterator
927 0 : ArpProto::FindUpperBoundArpEntry(const ArpKey &key) {
928 0 : return arp_cache_.upper_bound(key);
929 : }
930 :
931 : ArpProto::ArpIterator
932 0 : ArpProto::FindLowerBoundArpEntry(const ArpKey &key) {
933 0 : return arp_cache_.lower_bound(key);
934 : }
935 :
936 0 : void ArpPathPreferenceState::HandleArpReply(Ip4Address sip, uint32_t itf) {
937 0 : WaitForTrafficIntfMap::iterator it = l3_wait_for_traffic_map_.find(itf);
938 0 : if (it == l3_wait_for_traffic_map_.end()) {
939 0 : return;
940 : }
941 :
942 0 : InterfaceArpPathPreferenceInfo &data = it->second;
943 : // reset arp_try_count to 0.
944 0 : data.arp_try_count = 0;
945 :
946 0 : if (data.prev_responded_ip == sip) {
947 0 : ++data.arp_reply_count;
948 0 : data.arp_failure_count = 0;
949 : } else {
950 0 : data.prev_responded_ip = sip;
951 0 : data.arp_send_count = 0;
952 : }
953 : }
954 :
955 0 : void ArpProto::HandlePathPreferenceArpReply(const VrfEntry *vrf, uint32_t itf,
956 : Ip4Address sip) {
957 0 : if (!vrf) {
958 0 : return;
959 : }
960 0 : InetUnicastRouteEntry *rt = vrf->GetUcRoute(sip);
961 0 : if (!rt) {
962 0 : return;
963 : }
964 :
965 : ArpVrfState *state = static_cast<ArpVrfState *>
966 0 : (vrf->GetState(vrf->get_table_partition()->parent(),
967 : vrf_table_listener_id_));
968 0 : if (!state) {
969 0 : return;
970 : }
971 0 : ArpPathPreferenceState* pstate = state->Get(sip, rt->prefix_length());
972 0 : if (!pstate) {
973 0 : return;
974 : }
975 0 : pstate->HandleArpReply(sip, itf);
976 : }
977 :
978 0 : ArpInterfaceState::ArpInterfaceState(Interface *intf):
979 0 : intf_(intf), vrf_(NULL), fabric_vrf_(NULL), walk_ref_(NULL) {
980 0 : }
981 :
982 0 : ArpInterfaceState::~ArpInterfaceState() {
983 0 : if (walk_ref_.get() && vrf_) {
984 0 : vrf_->GetEvpnRouteTable()->ReleaseWalker(walk_ref_);
985 : }
986 0 : }
987 :
988 0 : void ArpInterfaceState::SetVrf(VrfEntry *vrf, VrfEntry *fabric_vrf) {
989 0 : bool walk = false;
990 :
991 0 : if (vrf_ != vrf) {
992 0 : if (walk_ref_.get() && vrf_) {
993 0 : vrf_->GetEvpnRouteTable()->ReleaseWalker(walk_ref_);
994 : }
995 0 : vrf_ = vrf;
996 : }
997 :
998 0 : if (fabric_vrf_ != fabric_vrf) {
999 0 : fabric_vrf_ = fabric_vrf;
1000 0 : walk = true;
1001 : }
1002 :
1003 0 : if (vrf_ == NULL) {
1004 0 : return;
1005 : }
1006 :
1007 0 : if (walk && walk_ref_.get() == NULL) {
1008 0 : walk_ref_ = vrf_->GetEvpnRouteTable()->AllocWalker(
1009 : boost::bind(&ArpInterfaceState::WalkNotify, this, _1, _2),
1010 0 : boost::bind(&ArpInterfaceState::WalkDone, this, _2));
1011 : }
1012 :
1013 0 : if (vrf_->vn() && vrf_->vn()->bridging()) {
1014 0 : if (walk) {
1015 0 : vrf_->GetEvpnRouteTable()->WalkAgain(walk_ref_);
1016 : }
1017 : }
1018 : }
1019 :
1020 0 : void ArpInterfaceState::WalkDone(DBTableBase *part) {
1021 0 : }
1022 :
1023 0 : bool ArpInterfaceState::WalkNotify(DBTablePartBase *partition,
1024 : DBEntryBase *e) {
1025 0 : const EvpnRouteEntry *evpn = static_cast<const EvpnRouteEntry *>(e);
1026 :
1027 0 : if (evpn->prefix_address().is_v4() == false) {
1028 0 : return true;
1029 : }
1030 :
1031 0 : if (evpn->prefix_address() == Ip4Address(0)) {
1032 0 : return true;
1033 : }
1034 :
1035 : const InterfaceNH *nh =
1036 0 : dynamic_cast<const InterfaceNH *>(evpn->GetActiveNextHop());
1037 0 : if (nh && nh->GetInterface() == intf_) {
1038 0 : return true;
1039 : }
1040 :
1041 0 : EvpnAgentRouteTable *table = static_cast<EvpnAgentRouteTable *>(evpn->get_table());
1042 0 : const VmInterface *vmi = static_cast<const VmInterface *>(intf_.get());
1043 0 : Agent *agent = table->agent();
1044 : boost::shared_ptr<PktInfo> pkt(new PktInfo(agent, ARP_TX_BUFF_LEN,
1045 0 : PktHandler::ARP, 0));
1046 0 : ArpHandler arp_handler(agent, pkt, *(agent->event_manager()->io_service()));
1047 :
1048 0 : MacAddress smac = vmi->GetVifMac(agent);
1049 0 : if (vrf_->forwarding_vrf() == NULL) {
1050 0 : smac = evpn->mac();
1051 : }
1052 :
1053 0 : agent->GetArpProto()->IncrementStatsVmGarpReq();
1054 0 : arp_handler.SendArp(ARPOP_REQUEST, smac, evpn->prefix_address().to_v4().to_ulong(),
1055 0 : smac, vmi->vm_mac(), evpn->prefix_address().to_v4().to_ulong(),
1056 : intf_->id(), intf_->vrf()->vrf_id());
1057 0 : return true;
1058 0 : }
|