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 <uve/agent_uve_base.h>
6 #include <ifmap/ifmap_node.h>
7 #include <vnc_cfg_types.h>
8 #include <base/address_util.h>
9 #include <oper/operdb_init.h>
10 #include <oper/route_common.h>
11 #include <oper/route_leak.h>
12 #include <oper/vrouter.h>
13 #include <oper/vrf.h>
14 #include <oper/config_manager.h>
15 #include <sandesh/sandesh_trace.h>
17 #include <init/agent_param.h>
18 
19 VRouterSubnet::VRouterSubnet(const std::string& ip, uint8_t prefix_len) {
20  boost::system::error_code ec;
21  ip_prefix = IpAddress::from_string(ip, ec);
22  plen = prefix_len;
23 }
24 
25 bool VRouterSubnet::operator==(const VRouterSubnet& rhs) const {
26  if (plen != rhs.plen) {
27  return false;
28  }
29  if (ip_prefix != rhs.ip_prefix) {
30  return false;
31  }
32  return true;
33 }
34 
35 bool VRouterSubnet::IsLess(const VRouterSubnet *rhs) const {
36  if (ip_prefix != rhs->ip_prefix) {
37  return ip_prefix < rhs->ip_prefix;
38  }
39  return (plen < rhs->plen);
40 }
41 
43  const VRouterSubnet &rhs) const {
44  return lhs.IsLess(&rhs);
45 }
46 
48 }
49 
51 }
52 
53 void VRouter::Insert(const VRouterSubnet *rhs) {
54  AddRoute(*rhs);
55  subnet_list_.insert(*rhs);
56 }
57 
58 void VRouter::Remove(VRouterSubnetSet::iterator &it) {
59  DeleteRoute(*it);
60  subnet_list_.erase(it);
61 }
62 
64  return;
65 }
66 
67 IFMapNode *VRouter::FindTarget(IFMapNode *node, std::string node_type) const {
68  IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
69  for (DBGraphVertex::adjacency_iterator it = node->begin(table->GetGraph());
70  it != node->end(table->GetGraph()); ++it) {
71  IFMapNode *adj_node = static_cast<IFMapNode *>(it.operator->());
72  if (adj_node->table()->Typename() == node_type)
73  return adj_node;
74  }
75  return NULL;
76 }
77 
79  if (SubnetCount() == 0) {
80  return;
81  }
83  subnet_list_.clear();
85 }
86 
88  autogen::VirtualRouter *cfg = static_cast<autogen::VirtualRouter *>
89  (node->GetObject());
90  VRouterSubnetSet new_subnet_list;
91  if (node->IsDeleted()) {
92  ClearSubnets();
93  return;
94  }
95 
96  const std::vector<autogen::KeyValuePair> &tblengths =
97  cfg->tracebuffer_length();
98  int new_buf_size;
99  for (const auto& kv_pair : tblengths) {
100  try {
101  new_buf_size = boost::lexical_cast<int>(kv_pair.value);
102  } catch (const boost::bad_lexical_cast& e) {
103  continue;
104  }
105  if (new_buf_size <= 0) {
106  continue;
107  }
108  SandeshTraceBufferResetSize(kv_pair.key, new_buf_size);
109  }
110 
111  agent()->uve()->set_default_interval(cfg->agent_uve_default_interval());
113  cfg->agent_uve_incremental_interval());
114  if (cfg->agent_stats_collector_interval() > 0) {
116  cfg->agent_stats_collector_interval());
117  } else {
119  agent()->params()->agent_stats_interval());
120  }
121 
122  name_ = node->name();
123  display_name_ = cfg->display_name();
124  IFMapNode *vr_ipam_link = agent()->config_manager()->
125  FindAdjacentIFMapNode(node, "virtual-router-network-ipam");
126  /* If the link is deleted, clear the subnets configured earlier */
127  if (!vr_ipam_link) {
128  ClearSubnets();
129  return;
130  }
131  autogen::VirtualRouterNetworkIpam *vr_ipam =
132  static_cast<autogen::VirtualRouterNetworkIpam *>
133  (vr_ipam_link->GetObject());
134  const autogen::VirtualRouterNetworkIpamType &data = vr_ipam->data();
135  std::vector<autogen::SubnetType>::const_iterator it =
136  data.subnet.begin();
137  while (it != data.subnet.end()) {
138  VRouterSubnet subnet(it->ip_prefix, it->ip_prefix_len);
139  new_subnet_list.insert(subnet);
140  ++it;
141  }
142 
143  if (new_subnet_list != subnet_list_) {
144  bool changed = AuditList<VRouter, VRouterSubnetSet::iterator>
145  (*this, subnet_list_.begin(), subnet_list_.end(),
146  new_subnet_list.begin(), new_subnet_list.end());
147  if (changed) {
149  ReEvaluateRouteExports();
150  }
151  }
152 }
153 
155  VRouterSubnetSet::iterator it = subnet_list_.begin();
156  while (it != subnet_list_.end()) {
157  DeleteRoute(*it);
158  ++it;
159  }
160 }
161 
162 void VRouter::AddRoute(const VRouterSubnet &subnet) {
163  VrfEntry *vrf = agent()->fabric_vrf();
164  static_cast<InetUnicastAgentRouteTable *>(vrf->
165  GetInet4UnicastRouteTable())->AddVrouterSubnetRoute(subnet.ip_prefix,
166  subnet.plen);
167 }
168 
169 void VRouter::DeleteRoute(const VRouterSubnet &subnet) {
170  VrfEntry *vrf = agent()->fabric_vrf();
171  if (subnet.ip_prefix.is_v4()) {
172  static_cast<InetUnicastAgentRouteTable *>(vrf->
173  GetInet4UnicastRouteTable())->DeleteReq
174  (agent()->fabric_rt_export_peer(), vrf->GetName(),
175  subnet.ip_prefix, subnet.plen, NULL);
176  } else if (subnet.ip_prefix.is_v6()) {
177  static_cast<InetUnicastAgentRouteTable *>(vrf->
178  GetInet6UnicastRouteTable())->DeleteReq
179  (agent()->fabric_rt_export_peer(), vrf->GetName(),
180  subnet.ip_prefix, subnet.plen, NULL);
181  }
182 }
183 
186  return;
187 }
188 
189 bool VRouter::IsSubnetMember(const IpAddress &addr) const {
190  bool v4 = false;
191  if (addr.is_v4()) {
192  v4 = true;
193  }
194  VRouterSubnetSet::iterator it = subnet_list_.begin();
195  while (it != subnet_list_.end()) {
196  if (v4 && it->ip_prefix.is_v4()) {
197  if (IsIp4SubnetMember(addr.to_v4(), it->ip_prefix.to_v4(),
198  it->plen)) {
199  return true;
200  }
201  } else if (!v4 && it->ip_prefix.is_v6()) {
202  if (IsIp6SubnetMember(addr.to_v6(), it->ip_prefix.to_v6(),
203  it->plen)) {
204  return true;
205  }
206  }
207  ++it;
208  }
209  return false;
210 }
211 
213  ClearSubnets();
214 }
215 
217 // Introspect routines
219 void VRouter::FillSandeshInfo(VrouterInfoResp *resp) {
220  resp->set_name(name_);
221  resp->set_display_name(display_name_);
222  vector<VnIpamData> list;
223  VRouterSubnetSet::iterator it = subnet_list_.begin();
224  while (it != subnet_list_.end()) {
225  VnIpamData item;
226  item.set_ip_prefix(it->ip_prefix.to_string());
227  item.set_prefix_len(it->plen);
228  list.push_back(item);
229  ++it;
230  }
231  resp->set_subnet_list(list);
232 }
233 
234 void VrouterInfoReq::HandleRequest() const {
235  Agent *agent = Agent::GetInstance();
236  VRouter *vr = agent->oper_db()->vrouter();
237  VrouterInfoResp *resp = new VrouterInfoResp();
238  resp->set_context(context());
239  if (!vr) {
240  resp->set_more(false);
241  resp->Response();
242  return;
243  }
244  vr->FillSandeshInfo(resp);
245  resp->set_more(false);
246  resp->Response();
247  return;
248 }
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
void set_incremental_interval(uint32_t new_interval=kIncrementalInterval)
Sets the incremental interval (time between dispatches of Agent's information and its remnant to UVE)...
void set_default_interval(uint32_t new_interval=kDefaultInterval)
Sets the default interval (time between two dispatches of Agent's information to UVE).
Definition: agent.h:360
ConfigManager * config_manager() const
Definition: agent.cc:892
OperDB * oper_db() const
Definition: agent.cc:1016
AgentUveBase * uve() const
Definition: agent.cc:912
const Peer * fabric_rt_export_peer() const
Definition: agent.h:1039
AgentStatsCollector * stats_collector() const
Definition: agent.cc:920
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:48
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
void set_expiry_time(int time)
bool IsSubnetMember(const IpAddress &addr) const
Definition: vrouter.cc:189
std::set< VRouterSubnet, VRouterSubnet > VRouterSubnetSet
Definition: vrouter.h:26
void AddRoute(const VRouterSubnet &subnet)
Definition: vrouter.cc:162
void ConfigManagerEnqueue(IFMapNode *node)
Definition: vrouter.cc:184
void Insert(const VRouterSubnet *rhs)
Definition: vrouter.cc:53
void DeleteRoute(const VRouterSubnet &subnet)
Definition: vrouter.cc:169
IFMapNode * FindTarget(IFMapNode *node, std::string node_type) const
Definition: vrouter.cc:67
virtual ~VRouter()
Definition: vrouter.cc:50
void ConfigAddChange(IFMapNode *node)
Definition: vrouter.cc:87
VRouter(Agent *agent)
Definition: vrouter.cc:47
std::string name_
Definition: vrouter.h:47
void ConfigDelete(IFMapNode *node)
Definition: vrouter.cc:63
VRouterSubnetSet subnet_list_
Definition: vrouter.h:48
std::string display_name_
Definition: vrouter.h:49
void Remove(VRouterSubnetSet::iterator &it)
Definition: vrouter.cc:58
void FillSandeshInfo(VrouterInfoResp *resp)
Definition: vrouter.cc:219
void DeleteSubnetRoutes()
Definition: vrouter.cc:154
void Shutdown()
Definition: vrouter.cc:212
void ClearSubnets()
Definition: vrouter.cc:78
uint32_t SubnetCount() const
Definition: vrouter.h:34
Definition: vrf.h:89
const string & GetName() const
Definition: vrf.h:103
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:25
IpAddress ip_prefix
Definition: vrouter.h:14
VRouterSubnet()
Definition: vrouter.h:16
bool operator()(const VRouterSubnet &lhs, const VRouterSubnet &rhs) const
Definition: vrouter.cc:42
bool IsLess(const VRouterSubnet *rhs) const
Definition: vrouter.cc:35