OpenSDN source code
vrouter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <ifmap/ifmap_node.h>
6 #include <vnc_cfg_types.h>
7 #include <base/address_util.h>
8 #include <oper/operdb_init.h>
9 #include <oper/route_common.h>
10 #include <oper/route_leak.h>
11 #include <oper/vrouter.h>
12 #include <oper/vrf.h>
13 #include <oper/config_manager.h>
14 #include <sandesh/sandesh_trace.h>
15 
16 VRouterSubnet::VRouterSubnet(const std::string& ip, uint8_t prefix_len) {
17  boost::system::error_code ec;
18  ip_prefix = IpAddress::from_string(ip, ec);
19  plen = prefix_len;
20 }
21 
22 bool VRouterSubnet::operator==(const VRouterSubnet& rhs) const {
23  if (plen != rhs.plen) {
24  return false;
25  }
26  if (ip_prefix != rhs.ip_prefix) {
27  return false;
28  }
29  return true;
30 }
31 
32 bool VRouterSubnet::IsLess(const VRouterSubnet *rhs) const {
33  if (ip_prefix != rhs->ip_prefix) {
34  return ip_prefix < rhs->ip_prefix;
35  }
36  return (plen < rhs->plen);
37 }
38 
40  const VRouterSubnet &rhs) const {
41  return lhs.IsLess(&rhs);
42 }
43 
45 }
46 
48 }
49 
50 void VRouter::Insert(const VRouterSubnet *rhs) {
51  AddRoute(*rhs);
52  subnet_list_.insert(*rhs);
53 }
54 
55 void VRouter::Remove(VRouterSubnetSet::iterator &it) {
56  DeleteRoute(*it);
57  subnet_list_.erase(it);
58 }
59 
61  return;
62 }
63 
64 IFMapNode *VRouter::FindTarget(IFMapNode *node, std::string node_type) const {
65  IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
66  for (DBGraphVertex::adjacency_iterator it = node->begin(table->GetGraph());
67  it != node->end(table->GetGraph()); ++it) {
68  IFMapNode *adj_node = static_cast<IFMapNode *>(it.operator->());
69  if (adj_node->table()->Typename() == node_type)
70  return adj_node;
71  }
72  return NULL;
73 }
74 
76  if (SubnetCount() == 0) {
77  return;
78  }
80  subnet_list_.clear();
82 }
83 
85  autogen::VirtualRouter *cfg = static_cast<autogen::VirtualRouter *>
86  (node->GetObject());
87  VRouterSubnetSet new_subnet_list;
88  if (node->IsDeleted() == false) {
89  const std::vector<autogen::KeyValuePair> &tblengths =
90  cfg->tracebuffer_length();
91  int new_buf_size;
92  for (const auto& kv_pair : tblengths) {
93  try {
94  new_buf_size = boost::lexical_cast<int>(kv_pair.value);
95  } catch (const boost::bad_lexical_cast& e) {
96  continue;
97  }
98  if (new_buf_size <= 0) {
99  continue;
100  }
101  SandeshTraceBufferResetSize(kv_pair.key, new_buf_size);
102  }
103  name_ = node->name();
104  display_name_ = cfg->display_name();
105  IFMapNode *vr_ipam_link = agent()->config_manager()->
106  FindAdjacentIFMapNode(node, "virtual-router-network-ipam");
107  /* If the link is deleted, clear the subnets configured earlier */
108  if (!vr_ipam_link) {
109  ClearSubnets();
110  return;
111  }
112  autogen::VirtualRouterNetworkIpam *vr_ipam =
113  static_cast<autogen::VirtualRouterNetworkIpam *>
114  (vr_ipam_link->GetObject());
115  const autogen::VirtualRouterNetworkIpamType &data = vr_ipam->data();
116  std::vector<autogen::SubnetType>::const_iterator it =
117  data.subnet.begin();
118  while (it != data.subnet.end()) {
119  VRouterSubnet subnet(it->ip_prefix, it->ip_prefix_len);
120  new_subnet_list.insert(subnet);
121  ++it;
122  }
123 
124  if (new_subnet_list != subnet_list_) {
125  bool changed = AuditList<VRouter, VRouterSubnetSet::iterator>
126  (*this, subnet_list_.begin(), subnet_list_.end(),
127  new_subnet_list.begin(), new_subnet_list.end());
128  if (changed) {
130  ReEvaluateRouteExports();
131  }
132  }
133  } else {
134  ClearSubnets();
135  }
136  return;
137 }
138 
140  VRouterSubnetSet::iterator it = subnet_list_.begin();
141  while (it != subnet_list_.end()) {
142  DeleteRoute(*it);
143  ++it;
144  }
145 }
146 
147 void VRouter::AddRoute(const VRouterSubnet &subnet) {
148  VrfEntry *vrf = agent()->fabric_vrf();
149  static_cast<InetUnicastAgentRouteTable *>(vrf->
150  GetInet4UnicastRouteTable())->AddVrouterSubnetRoute(subnet.ip_prefix,
151  subnet.plen);
152 }
153 
154 void VRouter::DeleteRoute(const VRouterSubnet &subnet) {
155  VrfEntry *vrf = agent()->fabric_vrf();
156  if (subnet.ip_prefix.is_v4()) {
157  static_cast<InetUnicastAgentRouteTable *>(vrf->
158  GetInet4UnicastRouteTable())->DeleteReq
159  (agent()->fabric_rt_export_peer(), vrf->GetName(),
160  subnet.ip_prefix, subnet.plen, NULL);
161  } else if (subnet.ip_prefix.is_v6()) {
162  static_cast<InetUnicastAgentRouteTable *>(vrf->
163  GetInet6UnicastRouteTable())->DeleteReq
164  (agent()->fabric_rt_export_peer(), vrf->GetName(),
165  subnet.ip_prefix, subnet.plen, NULL);
166  }
167 }
168 
171  return;
172 }
173 
174 bool VRouter::IsSubnetMember(const IpAddress &addr) const {
175  bool v4 = false;
176  if (addr.is_v4()) {
177  v4 = true;
178  }
179  VRouterSubnetSet::iterator it = subnet_list_.begin();
180  while (it != subnet_list_.end()) {
181  if (v4 && it->ip_prefix.is_v4()) {
182  if (IsIp4SubnetMember(addr.to_v4(), it->ip_prefix.to_v4(),
183  it->plen)) {
184  return true;
185  }
186  } else if (!v4 && it->ip_prefix.is_v6()) {
187  if (IsIp6SubnetMember(addr.to_v6(), it->ip_prefix.to_v6(),
188  it->plen)) {
189  return true;
190  }
191  }
192  ++it;
193  }
194  return false;
195 }
196 
198  ClearSubnets();
199 }
200 
202 // Introspect routines
204 void VRouter::FillSandeshInfo(VrouterInfoResp *resp) {
205  resp->set_name(name_);
206  resp->set_display_name(display_name_);
207  vector<VnIpamData> list;
208  VRouterSubnetSet::iterator it = subnet_list_.begin();
209  while (it != subnet_list_.end()) {
210  VnIpamData item;
211  item.set_ip_prefix(it->ip_prefix.to_string());
212  item.set_prefix_len(it->plen);
213  list.push_back(item);
214  ++it;
215  }
216  resp->set_subnet_list(list);
217 }
218 
219 void VrouterInfoReq::HandleRequest() const {
220  Agent *agent = Agent::GetInstance();
221  VRouter *vr = agent->oper_db()->vrouter();
222  VrouterInfoResp *resp = new VrouterInfoResp();
223  resp->set_context(context());
224  if (!vr) {
225  resp->set_more(false);
226  resp->Response();
227  return;
228  }
229  vr->FillSandeshInfo(resp);
230  resp->set_more(false);
231  resp->Response();
232  return;
233 }
boost::asio::ip::address IpAddress
Definition: address.h:13
bool IsIp6SubnetMember(const Ip6Address &ip, const Ip6Address &subnet, uint8_t plen)
Definition: address_util.cc:29
bool IsIp4SubnetMember(const Ip4Address &ip, const Ip4Address &prefix_ip, uint16_t plen)
Definition: address_util.cc:19
Definition: agent.h:360
ConfigManager * config_manager() const
Definition: agent.cc:892
OperDB * oper_db() const
Definition: agent.cc:1016
const Peer * fabric_rt_export_peer() const
Definition: agent.h:1039
VrfEntry * fabric_vrf() const
Definition: agent.h:917
static Agent * GetInstance()
Definition: agent.h:438
void AddVirtualRouterNode(IFMapNode *node)
bool IsDeleted() const
Definition: db_entry.h:49
adjacency_iterator end(DBGraph *graph)
adjacency_iterator begin(DBGraph *graph)
const DBGraph * GetGraph() const
IFMapTable * table()
Definition: ifmap_node.h:29
const std::string & name() const
Definition: ifmap_node.h:48
IFMapObject * GetObject()
Definition: ifmap_node.cc:63
virtual const char * Typename() const =0
RouteLeakManager * route_leak_manager() const
Definition: operdb_init.h:95
VRouter * vrouter() const
Definition: operdb_init.h:76
Agent * agent() const
Definition: oper_db.h:245
void ReEvaluateRouteExports()
Definition: route_leak.cc:451
bool IsSubnetMember(const IpAddress &addr) const
Definition: vrouter.cc:174
std::set< VRouterSubnet, VRouterSubnet > VRouterSubnetSet
Definition: vrouter.h:26
void AddRoute(const VRouterSubnet &subnet)
Definition: vrouter.cc:147
void ConfigManagerEnqueue(IFMapNode *node)
Definition: vrouter.cc:169
void Insert(const VRouterSubnet *rhs)
Definition: vrouter.cc:50
void DeleteRoute(const VRouterSubnet &subnet)
Definition: vrouter.cc:154
IFMapNode * FindTarget(IFMapNode *node, std::string node_type) const
Definition: vrouter.cc:64
virtual ~VRouter()
Definition: vrouter.cc:47
void ConfigAddChange(IFMapNode *node)
Definition: vrouter.cc:84
VRouter(Agent *agent)
Definition: vrouter.cc:44
std::string name_
Definition: vrouter.h:47
void ConfigDelete(IFMapNode *node)
Definition: vrouter.cc:60
VRouterSubnetSet subnet_list_
Definition: vrouter.h:48
std::string display_name_
Definition: vrouter.h:49
void Remove(VRouterSubnetSet::iterator &it)
Definition: vrouter.cc:55
void FillSandeshInfo(VrouterInfoResp *resp)
Definition: vrouter.cc:204
void DeleteSubnetRoutes()
Definition: vrouter.cc:139
void Shutdown()
Definition: vrouter.cc:197
void ClearSubnets()
Definition: vrouter.cc:75
uint32_t SubnetCount() const
Definition: vrouter.h:34
Definition: vrf.h:86
const string & GetName() const
Definition: vrf.h:100
SandeshTraceBufferPtr SandeshTraceBufferResetSize(const std::string &buf_name, size_t buf_size)
Definition: sandesh_trace.h:39
uint8_t plen
Definition: vrouter.h:15
bool operator==(const VRouterSubnet &rhs) const
Definition: vrouter.cc:22
IpAddress ip_prefix
Definition: vrouter.h:14
VRouterSubnet()
Definition: vrouter.h:16
bool operator()(const VRouterSubnet &lhs, const VRouterSubnet &rhs) const
Definition: vrouter.cc:39
bool IsLess(const VRouterSubnet *rhs) const
Definition: vrouter.cc:32