OpenSDN source code
inet6vpn_table.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 #include "bgp/ipeer.h"
9 #include "bgp/bgp_server.h"
10 #include "bgp/bgp_peer.h"
11 #include "bgp/bgp_update.h"
12 #include "bgp/inet6/inet6_table.h"
14 #include "net/rd.h"
15 
16 Inet6VpnTable::Inet6VpnTable(DB *db, const std::string &name)
17  : BgpTable(db, name) {
18 }
19 
20 std::unique_ptr<DBEntry>
22  const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
23  return std::unique_ptr<DBEntry>(new Inet6VpnRoute(pfxkey->prefix));
24 }
25 
26 std::unique_ptr<DBEntry>
27 Inet6VpnTable::AllocEntryStr(const std::string &key_str) const {
28  Inet6VpnPrefix vpn_prefix = Inet6VpnPrefix::FromString(key_str);
29  return std::unique_ptr<DBEntry> (new Inet6VpnRoute(vpn_prefix));
30 }
31 
32 size_t Inet6VpnTable::Hash(const DBEntry *entry) const {
33  const Inet6VpnRoute *vpn_route = static_cast<const Inet6VpnRoute *>(entry);
34  const Inet6VpnPrefix &vpn_prefix = vpn_route->GetPrefix();
35  Inet6Prefix prefix(vpn_prefix.addr(), vpn_prefix.prefixlen());
36  size_t value = Inet6Table::HashFunction(prefix);
37  return value % DB::PartitionCount();
38 }
39 
40 size_t Inet6VpnTable::Hash(const DBRequestKey *key) const {
41  const RequestKey *rkey = static_cast<const RequestKey *>(key);
42  Inet6Prefix prefix(rkey->prefix.addr(), rkey->prefix.prefixlen());
43  size_t value = Inet6Table::HashFunction(prefix);
44  return value % DB::PartitionCount();
45 }
46 
48  const DBRequestKey *prefix) {
49  const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
50  Inet6VpnRoute vpn_route(pfxkey->prefix);
51  return static_cast<BgpRoute *>(rtp->Find(&vpn_route));
52 }
53 
54 DBTableBase *Inet6VpnTable::CreateTable(DB *db, const std::string &name) {
55  Inet6VpnTable *table = new Inet6VpnTable(db, name);
56  table->Init();
57  return table;
58 }
59 
61  const BgpTable *src_table, const BgpPath *src_path) {
62  const RouteDistinguisher &source_rd = src_path->GetAttr()->source_rd();
63  if (!src_path->GetPeer() || !(src_path->GetPeer()->IsRouterTypeBGPaaS())){
64  if (!source_rd.IsZero())
65  return source_rd;
66 
67  assert(!src_path->GetPeer() || !src_path->GetPeer()->IsXmppPeer());
68  const RoutingInstance *src_instance = src_table->routing_instance();
69  return *src_instance->GetRD();
70  }
71  uint16_t peer_port_id = 0;
72  as_t asn_num = src_path->GetAttr()->neighbor_as();
73 
74  std::string ri_name = src_table->routing_instance()->name();
75  for (const BgpPeer *peer = src_path->GetPeer()->server()->FindNextPeer(); peer != NULL;
76  peer = src_path->GetPeer()->server()->FindNextPeer(peer->peer_name())) {
77  if ((peer->peer_name().find(ri_name)!= std::string::npos)&&
78  (peer->peer_address_string().find(src_path->GetPeer()->ToString())!= std::string::npos)){
79  peer_port_id = peer->peer_port();
80  asn_num = peer->peer_as();
81  break;
82  }
83  }
84  RouteDistinguisher new_source_rd = RouteDistinguisher(true, asn_num, peer_port_id);
85  return new_source_rd;
86 }
87 
89  BgpRoute *source_rt, const BgpPath *src_path,
90  ExtCommunityPtr community) {
91  assert(src_table->family() == Address::INET6);
92 
93  Inet6Route *src_rt = dynamic_cast<Inet6Route *>(source_rt);
94  assert(src_rt);
95 
96  const RouteDistinguisher &rd = GenerateDistinguisher(src_table, src_path);
97  Inet6VpnPrefix vpn_prefix(rd, src_rt->GetPrefix().ip6_addr(),
98  src_rt->GetPrefix().prefixlen());
99 
100  Inet6VpnRoute vpn_route(vpn_prefix);
101  DBTablePartition *partition =
102  static_cast<DBTablePartition *>(GetTablePartition(&vpn_route));
103  BgpRoute *dest_route = static_cast<BgpRoute *>(partition->Find(&vpn_route));
104  if (dest_route == NULL) {
105  dest_route = new Inet6VpnRoute(vpn_prefix);
106  partition->Add(dest_route);
107  } else {
108  dest_route->ClearDelete();
109  }
110 
111  BgpAttrDB *attr_db = server->attr_db();
112 
113  BgpAttrPtr new_attr =
115  community);
116  new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(), rd);
117 
118  // Check whether there's already a path with the given peer and path id.
119  BgpPath *dest_path =
120  dest_route->FindSecondaryPath(source_rt, src_path->GetSource(),
121  src_path->GetPeer(),
122  src_path->GetPathId());
123  if (dest_path != NULL) {
124  if ((new_attr != dest_path->GetOriginalAttr()) ||
125  (src_path->GetFlags() != dest_path->GetFlags()) ||
126  (src_path->GetLabel() != dest_path->GetLabel())) {
127  // Update Attributes and notify (if needed)
128  assert(dest_route->RemoveSecondaryPath(source_rt,
129  src_path->GetSource(), src_path->GetPeer(),
130  src_path->GetPathId()));
131  } else {
132  return dest_route;
133  }
134  }
135 
136  // Create replicated path and insert it on the route
137  BgpSecondaryPath *replicated_path =
138  new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
139  src_path->GetSource(), new_attr,
140  src_path->GetFlags(), src_path->GetLabel());
141  replicated_path->SetReplicateInfo(src_table, source_rt);
142  dest_route->InsertPath(replicated_path);
143 
144  // Always trigger notification.
145  partition->Notify(dest_route);
146 
147  return dest_route;
148 }
149 
150 bool Inet6VpnTable::Export(RibOut *ribout, Route *route,
151  const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
152  BgpRoute *bgp_route = static_cast<BgpRoute *> (route);
153  UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
154  if (!uinfo) {
155  return false;
156  }
157  uinfo_slist->push_front(*uinfo);
158 
159  return true;
160 }
161 
162 static void RegisterFactory() {
163  DB::RegisterFactory("bgp.l3vpn-inet6.0", &Inet6VpnTable::CreateTable);
164 }
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
Definition: bgp_attr.h:999
uint32_t as_t
Definition: bgp_common.h:22
@ INET6
Definition: address.h:28
BgpAttrPtr ReplaceExtCommunityAndLocate(const BgpAttr *attr, ExtCommunityPtr extcomm)
Definition: bgp_attr.cc:1367
BgpAttrPtr ReplaceSourceRdAndLocate(const BgpAttr *attr, const RouteDistinguisher &source_rd)
Definition: bgp_attr.cc:1407
as_t neighbor_as() const
Definition: bgp_attr.cc:1167
const RouteDistinguisher & source_rd() const
Definition: bgp_attr.h:899
uint32_t GetLabel() const
Definition: bgp_path.h:90
uint32_t GetFlags() const
Definition: bgp_path.h:101
IPeer * GetPeer()
Definition: bgp_path.h:77
const uint32_t GetPathId() const
Definition: bgp_path.h:79
const BgpAttr * GetAttr() const
Definition: bgp_path.h:88
PathSource GetSource() const
Definition: bgp_path.h:104
const BgpAttr * GetOriginalAttr() const
Definition: bgp_path.h:89
bool RemoveSecondaryPath(const BgpRoute *src_rt, BgpPath::PathSource src, const IPeer *peer, uint32_t path_id)
Definition: bgp_route.cc:399
BgpPath * FindSecondaryPath(BgpRoute *src_rt, BgpPath::PathSource src, const IPeer *peer, uint32_t path_id)
Definition: bgp_route.cc:374
void InsertPath(BgpPath *path)
Definition: bgp_route.cc:62
void SetReplicateInfo(const BgpTable *table, const BgpRoute *rt)
Definition: bgp_path.h:173
BgpPeer * FindNextPeer(const std::string &name=std::string())
Definition: bgp_server.cc:643
BgpAttrDB * attr_db()
Definition: bgp_server.h:188
BgpServer * server()
Definition: bgp_table.cc:88
RoutingInstance * routing_instance()
Definition: bgp_table.h:148
UpdateInfo * GetUpdateInfo(RibOut *ribout, BgpRoute *route, const RibPeerSet &peerset)
Definition: bgp_table.cc:582
virtual Address::Family family() const =0
void ClearDelete()
Definition: db_entry.h:48
const std::string & name() const
Definition: db_table.h:111
void Notify(DBEntryBase *entry)
virtual void Add(DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
void Init()
Definition: db_table.cc:414
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:463
Definition: db.h:25
static void RegisterFactory(const std::string &prefix, CreateFunction create_fn)
Definition: db.cc:24
static int PartitionCount()
Definition: db.cc:32
virtual const std::string & ToString() const =0
virtual bool IsXmppPeer() const =0
virtual BgpServer * server()=0
virtual bool IsRouterTypeBGPaaS() const =0
Ip6Address ip6_addr() const
Definition: inet6_route.h:28
int prefixlen() const
Definition: inet6_route.h:34
const Inet6Prefix & GetPrefix() const
Definition: inet6_route.h:81
static size_t HashFunction(const Inet6Prefix &addr)
Definition: inet6_table.cc:18
Ip6Address addr() const
int prefixlen() const
static Inet6VpnPrefix FromString(const std::string &str, boost::system::error_code *errorp=NULL)
const Inet6VpnPrefix & GetPrefix() const
virtual BgpRoute * TableFind(DBTablePartition *rtp, const DBRequestKey *prefix)
virtual std::unique_ptr< DBEntry > AllocEntryStr(const std::string &key) const
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *key) const
virtual BgpRoute * RouteReplicate(BgpServer *server, BgpTable *src_table, BgpRoute *src_rt, const BgpPath *path, ExtCommunityPtr ptr)
virtual size_t Hash(const DBEntry *entry) const
Inet6VpnTable(DB *db, const std::string &name)
static DBTableBase * CreateTable(DB *db, const std::string &name)
virtual bool Export(RibOut *ribout, Route *route, const RibPeerSet &peerset, UpdateInfoSList &info_slist)
bool IsZero() const
Definition: rd.h:44
Definition: route.h:15
const std::string & name() const
const RouteDistinguisher * GetRD() const
boost::intrusive_ptr< const ExtCommunity > ExtCommunityPtr
Definition: community.h:451
static RouteDistinguisher GenerateDistinguisher(const BgpTable *src_table, const BgpPath *src_path)
MODULE_INITIALIZER(RegisterFactory)
static void RegisterFactory()