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 "bgp/routing-instance/static_route.h"
7 :
8 : #include <boost/assign/list_of.hpp>
9 : #include <boost/foreach.hpp>
10 :
11 : #include <algorithm>
12 : #include <string>
13 : #include <vector>
14 :
15 : #include "base/map_util.h"
16 : #include "base/task_annotations.h"
17 : #include "base/task_trigger.h"
18 : #include "bgp/bgp_config.h"
19 : #include "bgp/bgp_log.h"
20 : #include "bgp/bgp_server.h"
21 : #include "bgp/inet6vpn/inet6vpn_route.h"
22 : #include "bgp/l3vpn/inetvpn_route.h"
23 : #include "bgp/origin-vn/origin_vn.h"
24 : #include "bgp/routing-instance/routepath_replicator.h"
25 : #include "bgp/routing-instance/routing_instance.h"
26 : #include "bgp/routing-instance/static_route_types.h"
27 : #include "bgp/routing-policy/routing_policy_match.h"
28 : #include "net/community_type.h"
29 :
30 : using boost::assign::list_of;
31 : using boost::system::error_code;
32 : using std::make_pair;
33 : using std::set;
34 : using std::string;
35 : using std::vector;
36 :
37 : class StaticRouteState : public ConditionMatchState {
38 : public:
39 0 : explicit StaticRouteState(StaticRoutePtr info) : info_(info) {
40 0 : }
41 : StaticRoutePtr info() {
42 : return info_;
43 : }
44 :
45 : private:
46 : StaticRoutePtr info_;
47 : DISALLOW_COPY_AND_ASSIGN(StaticRouteState);
48 : };
49 :
50 : template <typename T>
51 : class StaticRoute : public ConditionMatch {
52 : public:
53 : typedef typename T::RouteT RouteT;
54 : typedef typename T::VpnRouteT VpnRouteT;
55 : typedef typename T::PrefixT PrefixT;
56 : typedef typename T::AddressT AddressT;
57 : typedef StaticRouteMgr<T> StaticRouteMgrT;
58 :
59 : // List of Route targets
60 : typedef set<RouteTarget> RouteTargetList;
61 :
62 : // List of path ids for the Nexthop
63 : typedef set<uint32_t> NexthopPathIdList;
64 :
65 : enum CompareResult {
66 : NoChange = 0,
67 : PrefixChange = 1,
68 : NexthopChange = 2,
69 : AttributeChange = 3
70 : };
71 :
72 : StaticRoute(RoutingInstance *rtinstance, StaticRouteMgrT *manager,
73 : const PrefixT &static_route, const StaticRouteConfig &config);
74 0 : Address::Family GetFamily() const { return manager_->GetFamily(); }
75 0 : AddressT GetAddress(IpAddress addr) const {
76 0 : return manager_->GetAddress(addr);
77 : }
78 :
79 : CompareResult CompareConfig(const StaticRouteConfig &config);
80 : void FillShowInfo(StaticRouteInfo *info) const;
81 :
82 0 : const PrefixT &prefix() const {
83 0 : return prefix_;
84 : }
85 :
86 0 : const IpAddress &nexthop() const {
87 0 : return nexthop_;
88 : }
89 :
90 0 : RoutingInstance *routing_instance() const {
91 0 : return routing_instance_;
92 : }
93 :
94 0 : BgpTable *bgp_table() const {
95 0 : return routing_instance_->GetTable(this->GetFamily());
96 : }
97 :
98 0 : BgpRoute *nexthop_route() const {
99 0 : return nexthop_route_;
100 : }
101 :
102 0 : NexthopPathIdList *NexthopPathIds() { return &nexthop_path_ids_; }
103 :
104 0 : void set_nexthop_route(BgpRoute *nexthop_route) {
105 0 : nexthop_route_ = nexthop_route;
106 :
107 0 : if (!nexthop_route_) {
108 0 : nexthop_path_ids_.clear();
109 0 : return;
110 : }
111 :
112 0 : assert(nexthop_path_ids_.empty());
113 :
114 0 : for (Route::PathList::iterator it =
115 0 : nexthop_route->GetPathList().begin();
116 0 : it != nexthop_route->GetPathList().end(); it++) {
117 0 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
118 :
119 : // Infeasible paths are not considered
120 0 : if (!path->IsFeasible()) break;
121 :
122 : // take snapshot of all ECMP paths
123 0 : if (nexthop_route_->BestPath()->PathCompare(*path, true)) break;
124 :
125 : // Use the nexthop attribute of the nexthop path as the path id.
126 0 : uint32_t path_id = path->GetAttr()->nexthop().to_v4().to_ulong();
127 0 : nexthop_path_ids_.insert(path_id);
128 : }
129 : }
130 :
131 0 : const RouteTargetList &rtarget_list() const {
132 0 : return rtarget_list_;
133 : }
134 :
135 : void UpdateAttributes(const StaticRouteConfig &config);
136 : void AddStaticRoute(NexthopPathIdList *list);
137 : void UpdateStaticRoute();
138 : void RemoveStaticRoute();
139 : void NotifyRoute();
140 : bool IsPending() const;
141 :
142 : virtual bool Match(BgpServer *server, BgpTable *table,
143 : BgpRoute *route, bool deleted);
144 :
145 0 : virtual string ToString() const {
146 0 : return (string("StaticRoute ") + nexthop_.to_string());
147 : }
148 :
149 0 : void set_unregistered() {
150 0 : unregistered_ = true;
151 0 : }
152 :
153 0 : bool unregistered() const {
154 0 : return unregistered_;
155 : }
156 :
157 : private:
158 : // Helper function to match
159 0 : bool is_nexthop_route(BgpRoute *route) {
160 0 : RouteT *ip_route = dynamic_cast<RouteT *>(route);
161 0 : return (nexthop_ == ip_route->GetPrefix().addr());
162 : }
163 :
164 : CommunityPtr GetCommunity(const StaticRouteConfig &config);
165 : ExtCommunityPtr UpdateExtCommunity(const BgpAttr *attr) const;
166 :
167 : RoutingInstance *routing_instance_;
168 : StaticRouteMgrT *manager_;
169 : PrefixT prefix_;
170 : IpAddress nexthop_;
171 : BgpRoute *nexthop_route_;
172 : NexthopPathIdList nexthop_path_ids_;
173 : RouteTargetList rtarget_list_;
174 : CommunityPtr community_;
175 : bool unregistered_;
176 :
177 : DISALLOW_COPY_AND_ASSIGN(StaticRoute);
178 : };
179 :
180 : template <typename T>
181 0 : StaticRoute<T>::StaticRoute(RoutingInstance *rtinstance,
182 : StaticRouteMgrT *manager, const PrefixT &prefix,
183 : const StaticRouteConfig &config)
184 0 : : routing_instance_(rtinstance),
185 0 : manager_(manager),
186 0 : prefix_(prefix),
187 0 : nexthop_(config.nexthop),
188 0 : nexthop_route_(NULL),
189 0 : unregistered_(false) {
190 0 : UpdateAttributes(config);
191 0 : }
192 :
193 : //
194 : // Compare the given config against the current state of the StaticRoute.
195 : // Return the appropriate value from CompareResult.
196 : //
197 : template <typename T>
198 0 : typename StaticRoute<T>::CompareResult StaticRoute<T>::CompareConfig(
199 : const StaticRouteConfig &config) {
200 0 : AddressT address = this->GetAddress(config.address);
201 0 : PrefixT prefix(address, config.prefix_length);
202 0 : if (prefix_ != prefix) {
203 0 : return PrefixChange;
204 : }
205 0 : if (nexthop_ != config.nexthop) {
206 0 : return NexthopChange;
207 : }
208 0 : if (rtarget_list_.size() != config.route_targets.size()) {
209 0 : return AttributeChange;
210 : }
211 0 : for (vector<string>::const_iterator it = config.route_targets.begin();
212 0 : it != config.route_targets.end(); ++it) {
213 0 : error_code ec;
214 0 : RouteTarget rtarget = RouteTarget::FromString(*it, &ec);
215 0 : if (rtarget_list_.find(rtarget) == rtarget_list_.end()) {
216 0 : return AttributeChange;
217 : }
218 : }
219 0 : if (community_ != GetCommunity(config)) {
220 0 : return AttributeChange;
221 : }
222 :
223 0 : return NoChange;
224 : }
225 :
226 : template <typename T>
227 0 : void StaticRoute<T>::FillShowInfo(StaticRouteInfo *info) const {
228 0 : BgpTable *table = bgp_table();
229 0 : RouteT rt_key(prefix_);
230 0 : const BgpRoute *route = static_cast<const BgpRoute *>(table->Find(&rt_key));
231 0 : const BgpPath *path = route ? route->FindPath(BgpPath::StaticRoute) : NULL;
232 :
233 0 : info->set_prefix(prefix_.ToString());
234 0 : info->set_static_rt(path ? true : false);
235 0 : info->set_nexthop(nexthop_.to_string());
236 0 : if (nexthop_route_) {
237 0 : ShowRouteBrief show_route;
238 0 : nexthop_route_->FillRouteInfo(table, &show_route);
239 0 : info->set_nexthop_rt(show_route);
240 0 : }
241 :
242 0 : vector<string> community_list;
243 0 : BOOST_FOREACH(uint32_t value, community_->communities()) {
244 0 : community_list.push_back(CommunityType::CommunityToString(value));
245 : }
246 0 : info->set_community_list(community_list);
247 :
248 0 : vector<string> route_target_list;
249 0 : for (RouteTargetList::const_iterator it = rtarget_list_.begin();
250 0 : it != rtarget_list_.end(); ++it) {
251 0 : route_target_list.push_back(it->ToString());
252 : }
253 0 : info->set_route_target_list(route_target_list);
254 :
255 0 : if (path) {
256 0 : const RoutePathReplicator *replicator = table->server()->replicator(
257 : Address::VpnFamilyFromFamily(GetFamily()));
258 0 : info->set_secondary_tables(
259 : replicator->GetReplicatedTableNameList(table, route, path));
260 : }
261 0 : }
262 :
263 : // Match function called from BgpConditionListener
264 : // Concurrency : db::DBTable
265 : template <typename T>
266 0 : bool StaticRoute<T>::Match(BgpServer *server, BgpTable *table,
267 : BgpRoute *route, bool deleted) {
268 0 : CHECK_CONCURRENCY("db::DBTable");
269 : StaticRouteRequest::RequestType type;
270 :
271 0 : if (is_nexthop_route(route) && !unregistered()) {
272 0 : if (deleted)
273 0 : type = StaticRouteRequest::NEXTHOP_DELETE;
274 : else
275 0 : type = StaticRouteRequest::NEXTHOP_ADD_CHG;
276 : } else {
277 0 : return false;
278 : }
279 :
280 0 : BgpConditionListener *listener = server->condition_listener(GetFamily());
281 : StaticRouteState *state = static_cast<StaticRouteState *>
282 0 : (listener->GetMatchState(table, route, this));
283 0 : if (!deleted) {
284 : // MatchState is added to the Route to ensure that DBEntry is not
285 : // deleted before the module processes the WorkQueue request.
286 0 : if (!state) {
287 0 : state = new StaticRouteState(StaticRoutePtr(this));
288 0 : listener->SetMatchState(table, route, this, state);
289 : }
290 : } else {
291 : // MatchState is set on all the Routes that matches the conditions
292 : // Retrieve to check and ignore delete of unseen Add Match
293 0 : if (state == NULL) {
294 : // Not seen ADD ignore DELETE
295 0 : return false;
296 : }
297 : }
298 :
299 : // The MatchState reference is taken to ensure that the route is not
300 : // deleted when request is still present in the queue
301 : // This is to handle the case where MatchState already exists and
302 : // deleted entry gets reused or reused entry gets deleted.
303 0 : state->IncrementRefCnt();
304 :
305 : // Post the Match result to StaticRoute processing task to take Action
306 : // Nexthop route found in NAT instance ==> Add Static Route
307 : // and stitch the Path Attribute from nexthop route
308 0 : StaticRouteRequest *req =
309 0 : new StaticRouteRequest(type, table, route, StaticRoutePtr(this));
310 0 : manager_->EnqueueStaticRouteReq(req);
311 :
312 0 : return true;
313 : }
314 :
315 : //
316 : // Build a Community for the given StaticRouteConfig.
317 : // Always add the AcceptOwnNexthop community in addition to the configured
318 : // list.
319 : //
320 : template <typename T>
321 0 : CommunityPtr StaticRoute<T>::GetCommunity(const StaticRouteConfig &config) {
322 0 : CommunitySpec comm_spec;
323 0 : comm_spec.communities.push_back(CommunityType::AcceptOwnNexthop);
324 0 : for (vector<string>::const_iterator it = config.communities.begin();
325 0 : it != config.communities.end(); ++it) {
326 0 : uint32_t value = CommunityType::CommunityFromString(*it);
327 0 : if (!value)
328 0 : continue;
329 0 : comm_spec.communities.push_back(value);
330 : }
331 0 : CommunityDB *comm_db = routing_instance()->server()->comm_db();
332 0 : return comm_db->Locate(comm_spec);
333 0 : }
334 :
335 : //
336 : // Build an updated ExtCommunity for the static route.
337 : //
338 : // Replace any RouteTargets with the list of RouteTargets for the StaticRoute.
339 : // If the StaticRoute has an empty RouteTarget list, then we infer that this
340 : // is not a snat use case and add the OriginVn as well. We don't want to add
341 : // OriginVn in snat scenario because the route has to be imported into many
342 : // VRFs and we want to set the OriginVn differently for each imported route.
343 : //
344 : template <typename T>
345 0 : ExtCommunityPtr StaticRoute<T>::UpdateExtCommunity(const BgpAttr *attr) const {
346 0 : ExtCommunity::ExtCommunityList export_list;
347 0 : for (RouteTargetList::const_iterator it = rtarget_list().begin();
348 0 : it != rtarget_list().end(); it++) {
349 0 : export_list.push_back(it->GetExtCommunity());
350 : }
351 :
352 0 : BgpServer *server = routing_instance()->server();
353 0 : ExtCommunityDB *extcomm_db = server->extcomm_db();
354 0 : ExtCommunityPtr new_ext_community = extcomm_db->ReplaceRTargetAndLocate(
355 : attr->ext_community(), export_list);
356 :
357 0 : int vn_index = routing_instance()->virtual_network_index();
358 0 : if (export_list.empty() && vn_index) {
359 0 : as_t asn = server->autonomous_system();
360 0 : if (asn > AS2_MAX && vn_index > 0xffff) {
361 0 : OriginVn origin_vn(asn, AS_TRANS);
362 0 : new_ext_community = extcomm_db->ReplaceOriginVnAndLocate(
363 0 : new_ext_community.get(), origin_vn.GetExtCommunity());
364 0 : OriginVn origin_vn2(AS_TRANS, vn_index);
365 0 : new_ext_community = extcomm_db->AppendAndLocate(
366 0 : new_ext_community.get(), origin_vn2.GetExtCommunity());
367 0 : } else {
368 0 : OriginVn origin_vn(server->autonomous_system(), vn_index);
369 0 : new_ext_community = extcomm_db->ReplaceOriginVnAndLocate(
370 0 : new_ext_community.get(), origin_vn.GetExtCommunity());
371 : }
372 : }
373 0 : return new_ext_community;
374 0 : }
375 :
376 : template <typename T>
377 0 : void StaticRoute<T>::UpdateAttributes(const StaticRouteConfig &config) {
378 0 : rtarget_list_.clear();
379 0 : for (vector<string>::const_iterator it = config.route_targets.begin();
380 0 : it != config.route_targets.end(); ++it) {
381 0 : error_code ec;
382 0 : RouteTarget rtarget = RouteTarget::FromString(*it, &ec);
383 0 : if (ec.failed())
384 0 : continue;
385 0 : rtarget_list_.insert(rtarget);
386 : }
387 0 : community_ = GetCommunity(config);
388 0 : }
389 :
390 : // RemoveStaticRoute
391 : template <typename T>
392 0 : void StaticRoute<T>::RemoveStaticRoute() {
393 0 : CHECK_CONCURRENCY("bgp::StaticRoute");
394 0 : RouteT rt_key(prefix_);
395 : DBTablePartition *partition =
396 0 : static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
397 : BgpRoute *static_route =
398 0 : static_cast<BgpRoute *>(partition->Find(&rt_key));
399 0 : if (!static_route || static_route->IsDeleted()) return;
400 :
401 0 : for (NexthopPathIdList::iterator it = NexthopPathIds()->begin();
402 0 : it != NexthopPathIds()->end(); it++) {
403 0 : static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
404 0 : BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
405 : "Removed Static route path " << static_route->ToString() <<
406 : " path_id " << BgpPath::PathIdString(*it) <<
407 : " in table " << bgp_table()->name());
408 : }
409 :
410 0 : if (!static_route->HasPaths()) {
411 0 : partition->Delete(static_route);
412 : } else {
413 0 : partition->Notify(static_route);
414 : }
415 0 : }
416 :
417 : // UpdateStaticRoute
418 : template <typename T>
419 0 : void StaticRoute<T>::UpdateStaticRoute() {
420 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
421 0 : RouteT rt_key(prefix_);
422 : DBTablePartition *partition =
423 0 : static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
424 : BgpRoute *static_route =
425 0 : static_cast<BgpRoute *>(partition->Find(&rt_key));
426 0 : if (static_route == NULL) return;
427 :
428 0 : static_route->ClearDelete();
429 :
430 0 : BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
431 0 : for (NexthopPathIdList::iterator it = NexthopPathIds()->begin();
432 0 : it != NexthopPathIds()->end(); it++) {
433 : BgpPath *existing_path =
434 0 : static_route->FindPath(BgpPath::StaticRoute, NULL, *it);
435 0 : BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
436 : "Update attributes of StaticRoute path "
437 : << static_route->ToString() << " path_id "
438 : << BgpPath::PathIdString(*it) << " in table "
439 : << bgp_table()->name());
440 :
441 : // Add the RouteTarget and OrignVn in the ExtCommunity attribute.
442 0 : ExtCommunityPtr ptr = UpdateExtCommunity(existing_path->GetAttr());
443 0 : BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
444 : existing_path->GetAttr(), ptr);
445 :
446 : // Use pre-calculated community from the StaticRoute.
447 0 : new_attr =
448 0 : attr_db->ReplaceCommunityAndLocate(new_attr.get(), community_);
449 :
450 0 : BgpPath *new_path =
451 0 : new BgpPath(*it, BgpPath::StaticRoute, new_attr.get(),
452 0 : existing_path->GetFlags(), existing_path->GetLabel());
453 :
454 0 : static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
455 :
456 0 : static_route->InsertPath(new_path);
457 : }
458 0 : partition->Notify(static_route);
459 0 : }
460 :
461 : // AddStaticRoute
462 : template <typename T>
463 0 : void StaticRoute<T>::AddStaticRoute(NexthopPathIdList *old_path_ids) {
464 0 : CHECK_CONCURRENCY("bgp::StaticRoute");
465 :
466 0 : RouteT rt_key(prefix_);
467 : DBTablePartition *partition =
468 0 : static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
469 : BgpRoute *static_route =
470 0 : static_cast<BgpRoute *>(partition->Find(&rt_key));
471 :
472 0 : if (static_route == NULL) {
473 0 : static_route = new RouteT(prefix_);
474 0 : partition->Add(static_route);
475 : } else {
476 0 : static_route->ClearDelete();
477 : }
478 :
479 0 : BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
480 0 : for (Route::PathList::iterator it = nexthop_route()->GetPathList().begin();
481 0 : it != nexthop_route()->GetPathList().end(); it++) {
482 0 : BgpPath *nexthop_route_path = static_cast<BgpPath *>(it.operator->());
483 :
484 : // Infeasible paths are not considered
485 0 : if (!nexthop_route_path->IsFeasible()) break;
486 :
487 : // take snapshot of all ECMP paths
488 0 : if (nexthop_route()->BestPath()->PathCompare(*nexthop_route_path, true))
489 0 : break;
490 :
491 : // Skip paths with duplicate forwarding information. This ensures
492 : // that we generate only one path with any given next hop and label
493 : // when there are multiple nexthop paths from the original source
494 : // received via different peers e.g. directly via XMPP and via BGP.
495 0 : if (nexthop_route()->DuplicateForwardingPath(nexthop_route_path))
496 0 : continue;
497 :
498 : // Add the route target in the ExtCommunity attribute.
499 0 : ExtCommunityPtr ptr = UpdateExtCommunity(nexthop_route_path->GetAttr());
500 0 : BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
501 : nexthop_route_path->GetAttr(), ptr);
502 :
503 : // Use pre-calculated community from the static route.
504 0 : new_attr =
505 0 : attr_db->ReplaceCommunityAndLocate(new_attr.get(), community_);
506 :
507 : // Strip aspath. This is required when the nexthop route is learnt
508 : // via BGP.
509 0 : new_attr = attr_db->ReplaceAsPathAndLocate(new_attr.get(), AsPathPtr());
510 :
511 : // Replace the source rd if the nexthop path is a secondary path
512 : // of a primary path in the l3vpn table. Use the RD of the primary.
513 0 : if (nexthop_route_path->IsReplicated()) {
514 0 : const BgpSecondaryPath *spath =
515 : static_cast<const BgpSecondaryPath *>(nexthop_route_path);
516 0 : const RoutingInstance *ri = spath->src_table()->routing_instance();
517 0 : if (ri->IsMasterRoutingInstance()) {
518 : const VpnRouteT *vpn_route =
519 0 : static_cast<const VpnRouteT *>(spath->src_rt());
520 0 : new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(),
521 0 : vpn_route->GetPrefix().route_distinguisher());
522 : }
523 : }
524 :
525 : // Check whether we already have a path with the associated path id.
526 0 : uint32_t path_id =
527 0 : nexthop_route_path->GetAttr()->nexthop().to_v4().to_ulong();
528 0 : BgpPath *existing_path = static_route->FindPath(BgpPath::StaticRoute,
529 : NULL, path_id);
530 0 : if (existing_path != NULL) {
531 0 : if ((new_attr.get() != existing_path->GetAttr()) ||
532 0 : (nexthop_route_path->GetFlags() != existing_path->GetFlags()) ||
533 0 : (nexthop_route_path->GetLabel() != existing_path->GetLabel())) {
534 : // Update Attributes and notify (if needed)
535 0 : static_route->RemovePath(BgpPath::StaticRoute, NULL, path_id);
536 : } else {
537 0 : continue;
538 : }
539 : }
540 :
541 : // Populate SubProtocol for StaticRoute.
542 0 : new_attr = attr_db->ReplaceSubProtocolAndLocate(new_attr.get(),
543 : MatchProtocolToString(MatchProtocol::StaticRoute));
544 :
545 0 : BgpPath *new_path =
546 0 : new BgpPath(path_id, BgpPath::StaticRoute, new_attr.get(),
547 0 : nexthop_route_path->GetFlags(), nexthop_route_path->GetLabel());
548 0 : static_route->InsertPath(new_path);
549 0 : partition->Notify(static_route);
550 :
551 0 : BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
552 : "Added Static Route path " << static_route->ToString() <<
553 : " path_id " << BgpPath::PathIdString(path_id) <<
554 : " in table " << bgp_table()->name());
555 : }
556 :
557 0 : if (!old_path_ids) return;
558 :
559 0 : for (NexthopPathIdList::iterator it = old_path_ids->begin();
560 0 : it != old_path_ids->end(); it++) {
561 0 : if (NexthopPathIds()->find(*it) != NexthopPathIds()->end())
562 0 : continue;
563 0 : static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
564 0 : partition->Notify(static_route);
565 :
566 0 : BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
567 : "Removed StaticRoute path " << static_route->ToString() <<
568 : " path_id " << BgpPath::PathIdString(*it) <<
569 : " in table " << bgp_table()->name());
570 : }
571 0 : }
572 :
573 : template <typename T>
574 0 : void StaticRoute<T>::NotifyRoute() {
575 0 : RouteT rt_key(prefix_);
576 : DBTablePartition *partition =
577 0 : static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
578 0 : BgpRoute *static_route = static_cast<BgpRoute *>(partition->Find(&rt_key));
579 0 : if (!static_route)
580 0 : return;
581 0 : partition->Notify(static_route);
582 0 : }
583 :
584 : template <typename T>
585 0 : bool StaticRoute<T>::IsPending() const {
586 0 : RouteT rt_key(prefix_);
587 : DBTablePartition *partition =
588 0 : static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
589 0 : const BgpRoute *route = static_cast<BgpRoute *>(partition->Find(&rt_key));
590 0 : return (!route || !route->FindPath(BgpPath::StaticRoute));
591 0 : }
592 :
593 : template <>
594 : int StaticRouteMgr<StaticRouteInet>::static_route_task_id_ = -1;
595 : template <>
596 : int StaticRouteMgr<StaticRouteInet6>::static_route_task_id_ = -1;
597 :
598 : template <typename T>
599 0 : StaticRouteMgr<T>::StaticRouteMgr(RoutingInstance *rtinstance)
600 0 : : rtinstance_(rtinstance),
601 0 : listener_(rtinstance_->server()->condition_listener(GetFamily())),
602 0 : unregister_list_trigger_(new TaskTrigger(
603 : boost::bind(&StaticRouteMgr::ProcessUnregisterList, this),
604 0 : TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0)) {
605 0 : if (static_route_task_id_ == -1) {
606 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
607 0 : static_route_task_id_ = scheduler->GetTaskId("bgp::StaticRoute");
608 : }
609 :
610 0 : static_route_queue_ = new WorkQueue<StaticRouteRequest *>
611 : (static_route_task_id_, 0,
612 : boost::bind(&StaticRouteMgr::StaticRouteEventCallback, this, _1));
613 0 : }
614 :
615 : template <>
616 0 : Address::Family StaticRouteMgr<StaticRouteInet>::GetFamily() const {
617 0 : return Address::INET;
618 : }
619 :
620 : template <>
621 0 : Address::Family StaticRouteMgr<StaticRouteInet6>::GetFamily() const {
622 0 : return Address::INET6;
623 : }
624 :
625 : template <>
626 0 : Ip4Address StaticRouteMgr<StaticRouteInet>::GetAddress(IpAddress addr) const {
627 0 : assert(addr.is_v4());
628 0 : return addr.to_v4();
629 : }
630 :
631 : template <>
632 0 : Ip6Address StaticRouteMgr<StaticRouteInet6>::GetAddress(IpAddress addr) const {
633 0 : assert(addr.is_v6());
634 0 : return addr.to_v6();
635 : }
636 :
637 : template <typename T>
638 0 : void StaticRouteMgr<T>::EnqueueStaticRouteReq(StaticRouteRequest *req) {
639 0 : static_route_queue_->Enqueue(req);
640 0 : }
641 :
642 : template <typename T>
643 0 : bool StaticRouteMgr<T>::StaticRouteEventCallback(StaticRouteRequest *req) {
644 0 : CHECK_CONCURRENCY("bgp::StaticRoute");
645 0 : BgpTable *table = req->table_;
646 0 : BgpRoute *route = req->rt_;
647 0 : StaticRouteT *info = static_cast<StaticRouteT *>(req->info_.get());
648 :
649 0 : StaticRouteState *state = NULL;
650 0 : if (route) {
651 : state = static_cast<StaticRouteState *>
652 0 : (listener_->GetMatchState(table, route, info));
653 : }
654 :
655 0 : switch (req->type_) {
656 0 : case StaticRouteRequest::NEXTHOP_ADD_CHG: {
657 0 : assert(state);
658 0 : state->reset_deleted();
659 0 : if (route->IsDeleted() || !route->BestPath() ||
660 0 : !route->BestPath()->IsFeasible()) {
661 0 : break;
662 : }
663 :
664 : // Store the old path list
665 0 : typename StaticRouteT::NexthopPathIdList path_ids;
666 0 : path_ids.swap(*(info->NexthopPathIds()));
667 :
668 : // Populate the Nexthop PathID
669 0 : info->set_nexthop_route(route);
670 :
671 0 : info->AddStaticRoute(&path_ids);
672 0 : break;
673 0 : }
674 0 : case StaticRouteRequest::NEXTHOP_DELETE: {
675 0 : assert(state);
676 0 : info->RemoveStaticRoute();
677 0 : if (info->deleted() || route->IsDeleted()) {
678 0 : state->set_deleted();
679 : }
680 0 : info->set_nexthop_route(NULL);
681 0 : break;
682 : }
683 0 : default: {
684 0 : assert(0);
685 : break;
686 : }
687 : }
688 :
689 0 : if (state) {
690 0 : state->DecrementRefCnt();
691 0 : if (state->refcnt() == 0 && state->deleted()) {
692 0 : listener_->RemoveMatchState(table, route, info);
693 0 : delete state;
694 0 : if (!info->num_matchstate() && info->unregistered()) {
695 0 : UnregisterAndResolveStaticRoute(info);
696 : }
697 : }
698 : }
699 :
700 0 : delete req;
701 0 : return true;
702 : }
703 :
704 : template <typename T>
705 0 : bool StaticRouteMgr<T>::ProcessUnregisterList() {
706 0 : CHECK_CONCURRENCY("bgp::Config");
707 :
708 0 : for (StaticRouteProcessList::iterator
709 0 : it = unregister_static_route_list_.begin();
710 0 : it != unregister_static_route_list_.end(); ++it) {
711 0 : StaticRouteT *info = static_cast<StaticRouteT *>(it->get());
712 0 : listener_->UnregisterMatchCondition(info->bgp_table(), info);
713 0 : static_route_map_.erase(RouteKey(info->prefix(), info->nexthop()));
714 : }
715 :
716 0 : unregister_static_route_list_.clear();
717 :
718 0 : if (static_route_map_.empty()) {
719 0 : rtinstance_->server()->RemoveStaticRouteMgr(this);
720 : }
721 :
722 0 : if (!routing_instance()->deleted() &&
723 0 : routing_instance()->config()) {
724 0 : ProcessStaticRouteConfig();
725 : }
726 0 : return true;
727 : }
728 :
729 : template <typename T>
730 0 : void StaticRouteMgr<T>::UnregisterAndResolveStaticRoute(StaticRoutePtr entry) {
731 0 : std::scoped_lock lock(mutex_);
732 0 : unregister_static_route_list_.insert(entry);
733 0 : unregister_list_trigger_->Set();
734 0 : }
735 :
736 : template <typename T>
737 0 : void StaticRouteMgr<T>::LocateStaticRoutePrefix(
738 : const StaticRouteConfig &config) {
739 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
740 0 : AddressT address = this->GetAddress(config.address);
741 0 : PrefixT prefix(address, config.prefix_length);
742 0 : RouteKey route_key(prefix, config.nexthop);
743 :
744 : // Verify whether the entry already exists
745 0 : typename StaticRouteMap::iterator it = static_route_map_.find(route_key);
746 0 : if (it != static_route_map_.end()) {
747 : // Wait for the delete complete cb
748 0 : if (it->second->deleted()) return;
749 :
750 : StaticRouteT *match =
751 0 : static_cast<StaticRouteT *>(it->second.get());
752 : // Check whether the config has got updated
753 : typename StaticRouteT::CompareResult change =
754 0 : match->CompareConfig(config);
755 :
756 : // StaticRoutePrefix is the key, it can't change.
757 0 : assert(change != StaticRouteT::PrefixChange);
758 :
759 : // Skip if there's no change.
760 0 : if (change == StaticRouteT::NoChange)
761 0 : return;
762 :
763 : // Update the attributes and any existing BgpPaths.
764 0 : if (change == StaticRouteT::AttributeChange) {
765 0 : match->UpdateAttributes(config);
766 0 : match->UpdateStaticRoute();
767 0 : return;
768 : }
769 :
770 : // If the nexthop changes, remove the static route, if already added.
771 : // To do this, remove match condition and wait for remove completion.
772 0 : BgpConditionListener::RequestDoneCb callback =
773 : boost::bind(&StaticRouteMgr::StopStaticRouteDone, this, _1, _2);
774 0 : listener_->RemoveMatchCondition(
775 0 : match->bgp_table(), it->second.get(), callback);
776 0 : return;
777 0 : }
778 :
779 0 : StaticRouteT *match =
780 0 : new StaticRouteT(routing_instance(), this, prefix, config);
781 0 : StaticRoutePtr static_route_match = StaticRoutePtr(match);
782 :
783 0 : if (static_route_map_.empty())
784 0 : rtinstance_->server()->InsertStaticRouteMgr(this);
785 0 : static_route_map_.insert(make_pair(route_key, static_route_match));
786 :
787 0 : listener_->AddMatchCondition(match->bgp_table(), static_route_match.get(),
788 0 : BgpConditionListener::RequestDoneCb());
789 0 : }
790 :
791 : template <typename T>
792 0 : void StaticRouteMgr<T>::StopStaticRouteDone(BgpTable *table,
793 : ConditionMatch *info) {
794 0 : CHECK_CONCURRENCY("db::Walker");
795 0 : StaticRoute<T> *match = static_cast<StaticRoute<T> *>(info);
796 0 : match->set_unregistered();
797 0 : if (!match->num_matchstate() && match->unregistered()) {
798 0 : UnregisterAndResolveStaticRoute(match);
799 : }
800 0 : return;
801 : }
802 :
803 : template <typename T>
804 0 : void StaticRouteMgr<T>::RemoveStaticRoutePrefix(const RouteKey &static_route) {
805 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
806 0 : typename StaticRouteMap::iterator it = static_route_map_.find(static_route);
807 0 : if (it == static_route_map_.end()) return;
808 :
809 0 : if (it->second->deleted()) return;
810 :
811 0 : BgpConditionListener::RequestDoneCb callback =
812 : boost::bind(&StaticRouteMgr::StopStaticRouteDone, this, _1, _2);
813 :
814 0 : StaticRouteT *match = static_cast<StaticRouteT *>(it->second.get());
815 0 : listener_->RemoveMatchCondition(match->bgp_table(), match, callback);
816 0 : }
817 :
818 : template <typename T>
819 0 : void StaticRouteMgr<T>::ProcessStaticRouteConfig() {
820 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
821 0 : if (routing_instance()->deleted() || !routing_instance()->config()) return;
822 : const BgpInstanceConfig::StaticRouteList &list =
823 0 : routing_instance()->config()->static_routes(GetFamily());
824 : typedef BgpInstanceConfig::StaticRouteList::const_iterator iterator_t;
825 0 : for (iterator_t iter = list.begin(); iter != list.end(); ++iter) {
826 0 : LocateStaticRoutePrefix(*iter);
827 : }
828 : }
829 :
830 : template <typename T>
831 0 : StaticRouteMgr<T>::~StaticRouteMgr() {
832 0 : if (static_route_queue_)
833 0 : delete static_route_queue_;
834 0 : }
835 :
836 : template <typename T>
837 0 : void StaticRouteMgr<T>::UpdateStaticRouteConfig() {
838 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
839 0 : StaticRouteConfigList config_list =
840 0 : routing_instance()->config()->static_routes(GetFamily());
841 :
842 0 : map_difference(&static_route_map_,
843 : config_list.begin(), config_list.end(),
844 : boost::bind(&StaticRouteMgr<T>::CompareStaticRoute, this, _1, _2),
845 : boost::bind(&StaticRouteMgr<T>::AddStaticRoute, this, _1),
846 : boost::bind(&StaticRouteMgr<T>::DelStaticRoute, this, _1),
847 : boost::bind(&StaticRouteMgr<T>::UpdateStaticRoute, this, _1, _2));
848 0 : }
849 :
850 : template <typename T>
851 0 : void StaticRouteMgr<T>::FlushStaticRouteConfig() {
852 0 : CHECK_CONCURRENCY("bgp::Config");
853 0 : for (typename StaticRouteMap::iterator it = static_route_map_.begin();
854 0 : it != static_route_map_.end(); it++) {
855 0 : RemoveStaticRoutePrefix(it->first);
856 : }
857 0 : }
858 :
859 : template <typename T>
860 0 : int StaticRouteMgr<T>::CompareStaticRoute(
861 : typename StaticRouteMap::iterator loc,
862 : StaticRouteConfigList::iterator it) {
863 0 : AddressT address = this->GetAddress(it->address);
864 0 : PrefixT prefix(address, it->prefix_length);
865 0 : RouteKey route_key(prefix, it->nexthop);
866 0 : KEY_COMPARE(loc->first, route_key);
867 0 : return 0;
868 : }
869 :
870 : template <typename T>
871 0 : void StaticRouteMgr<T>::AddStaticRoute(StaticRouteConfigList::iterator it) {
872 0 : LocateStaticRoutePrefix(*it);
873 0 : }
874 :
875 : template <typename T>
876 0 : void StaticRouteMgr<T>::DelStaticRoute(typename StaticRouteMap::iterator loc) {
877 0 : RemoveStaticRoutePrefix(loc->first);
878 0 : }
879 :
880 : template <typename T>
881 0 : void StaticRouteMgr<T>::UpdateStaticRoute(typename StaticRouteMap::iterator loc,
882 : StaticRouteConfigList::iterator it) {
883 0 : LocateStaticRoutePrefix(*it);
884 0 : }
885 :
886 : template <typename T>
887 0 : void StaticRouteMgr<T>::NotifyAllRoutes() {
888 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
889 0 : for (typename StaticRouteMap::iterator it = static_route_map_.begin();
890 0 : it != static_route_map_.end(); ++it) {
891 : StaticRouteT *static_route =
892 0 : static_cast<StaticRouteT *>(it->second.get());
893 0 : static_route->NotifyRoute();
894 : }
895 0 : }
896 :
897 : template <typename T>
898 0 : void StaticRouteMgr<T>::UpdateAllRoutes() {
899 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
900 0 : for (typename StaticRouteMap::iterator it = static_route_map_.begin();
901 0 : it != static_route_map_.end(); ++it) {
902 : StaticRouteT *static_route =
903 0 : static_cast<StaticRouteT *>(it->second.get());
904 0 : static_route->UpdateStaticRoute();
905 : }
906 0 : }
907 :
908 : template <typename T>
909 0 : void StaticRouteMgr<T>::DisableUnregisterTrigger() {
910 0 : unregister_list_trigger_->set_disable();
911 0 : }
912 :
913 : template <typename T>
914 0 : void StaticRouteMgr<T>::EnableUnregisterTrigger() {
915 0 : unregister_list_trigger_->set_enable();
916 0 : }
917 :
918 : template <typename T>
919 0 : uint32_t StaticRouteMgr<T>::GetRouteCount() const {
920 0 : CHECK_CONCURRENCY("bgp::ShowCommand");
921 0 : return static_route_map_.size();
922 : }
923 :
924 : template <typename T>
925 0 : uint32_t StaticRouteMgr<T>::GetDownRouteCount() const {
926 0 : CHECK_CONCURRENCY("bgp::ShowCommand");
927 0 : uint32_t count = 0;
928 0 : for (typename StaticRouteMap::const_iterator it = static_route_map_.begin();
929 0 : it != static_route_map_.end(); ++it) {
930 : const StaticRouteT *static_route =
931 0 : static_cast<const StaticRouteT *>(it->second.get());
932 0 : if (static_route->IsPending())
933 0 : count++;
934 : }
935 0 : return count;
936 : }
937 :
938 0 : template <typename T> bool StaticRouteMgr<T>::FillStaticRouteInfo(
939 : RoutingInstance *ri, StaticRouteEntriesInfo *info) const {
940 0 : if (static_route_map_.empty())
941 0 : return false;
942 :
943 0 : info->set_ri_name(ri->name());
944 0 : for (typename StaticRouteMgr<T>::StaticRouteMap::const_iterator it =
945 0 : static_route_map_.begin(); it != static_route_map_.end(); ++it) {
946 : StaticRoute<T> *match =
947 0 : static_cast<StaticRoute<T> *>(it->second.get());
948 0 : StaticRouteInfo static_info;
949 0 : match->FillShowInfo(&static_info);
950 0 : info->static_route_list.push_back(static_info);
951 : }
952 0 : return true;
953 : }
954 :
955 : // Explicit instantiation of StaticRouteMgr for INET and INET6.
956 : template class StaticRouteMgr<StaticRouteInet>;
957 : template class StaticRouteMgr<StaticRouteInet6>;
|