OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
15 VRouterSubnet::VRouterSubnet(const std::string& ip, uint8_t prefix_len) {
16  boost::system::error_code ec;
17  ip_prefix = IpAddress::from_string(ip, ec);
18  plen = prefix_len;
19 }
20 
21 bool VRouterSubnet::operator==(const VRouterSubnet& rhs) const {
22  if (plen != rhs.plen) {
23  return false;
24  }
25  if (ip_prefix != rhs.ip_prefix) {
26  return false;
27  }
28  return true;
29 }
30 
31 bool VRouterSubnet::IsLess(const VRouterSubnet *rhs) const {
32  if (ip_prefix != rhs->ip_prefix) {
33  return ip_prefix < rhs->ip_prefix;
34  }
35  return (plen < rhs->plen);
36 }
37 
39  const VRouterSubnet &rhs) const {
40  return lhs.IsLess(&rhs);
41 }
42 
44 }
45 
47 }
48 
49 void VRouter::Insert(const VRouterSubnet *rhs) {
50  AddRoute(*rhs);
51  subnet_list_.insert(*rhs);
52 }
53 
54 void VRouter::Remove(VRouterSubnetSet::iterator &it) {
55  DeleteRoute(*it);
56  subnet_list_.erase(it);
57 }
58 
60  return;
61 }
62 
63 IFMapNode *VRouter::FindTarget(IFMapNode *node, std::string node_type) const {
64  IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
65  for (DBGraphVertex::adjacency_iterator it = node->begin(table->GetGraph());
66  it != node->end(table->GetGraph()); ++it) {
67  IFMapNode *adj_node = static_cast<IFMapNode *>(it.operator->());
68  if (adj_node->table()->Typename() == node_type)
69  return adj_node;
70  }
71  return NULL;
72 }
73 
75  if (SubnetCount() == 0) {
76  return;
77  }
79  subnet_list_.clear();
81 }
82 
84  autogen::VirtualRouter *cfg = static_cast<autogen::VirtualRouter *>
85  (node->GetObject());
86  VRouterSubnetSet new_subnet_list;
87  if (node->IsDeleted() == false) {
88  name_ = node->name();
89  display_name_ = cfg->display_name();
90  IFMapNode *vr_ipam_link = agent()->config_manager()->
91  FindAdjacentIFMapNode(node, "virtual-router-network-ipam");
92  /* If the link is deleted, clear the subnets configured earlier */
93  if (!vr_ipam_link) {
94  ClearSubnets();
95  return;
96  }
97  autogen::VirtualRouterNetworkIpam *vr_ipam =
98  static_cast<autogen::VirtualRouterNetworkIpam *>
99  (vr_ipam_link->GetObject());
100  const autogen::VirtualRouterNetworkIpamType &data = vr_ipam->data();
101  std::vector<autogen::SubnetType>::const_iterator it =
102  data.subnet.begin();
103  while (it != data.subnet.end()) {
104  VRouterSubnet subnet(it->ip_prefix, it->ip_prefix_len);
105  new_subnet_list.insert(subnet);
106  ++it;
107  }
108 
109  if (new_subnet_list != subnet_list_) {
110  bool changed = AuditList<VRouter, VRouterSubnetSet::iterator>
111  (*this, subnet_list_.begin(), subnet_list_.end(),
112  new_subnet_list.begin(), new_subnet_list.end());
113  if (changed) {
115  ReEvaluateRouteExports();
116  }
117  }
118  } else {
119  ClearSubnets();
120  }
121  return;
122 }
123 
125  VRouterSubnetSet::iterator it = subnet_list_.begin();
126  while (it != subnet_list_.end()) {
127  DeleteRoute(*it);
128  ++it;
129  }
130 }
131 
132 void VRouter::AddRoute(const VRouterSubnet &subnet) {
133  VrfEntry *vrf = agent()->fabric_vrf();
134  static_cast<InetUnicastAgentRouteTable *>(vrf->
135  GetInet4UnicastRouteTable())->AddVrouterSubnetRoute(subnet.ip_prefix,
136  subnet.plen);
137 }
138 
139 void VRouter::DeleteRoute(const VRouterSubnet &subnet) {
140  VrfEntry *vrf = agent()->fabric_vrf();
141  if (subnet.ip_prefix.is_v4()) {
142  static_cast<InetUnicastAgentRouteTable *>(vrf->
143  GetInet4UnicastRouteTable())->DeleteReq
144  (agent()->fabric_rt_export_peer(), vrf->GetName(),
145  subnet.ip_prefix, subnet.plen, NULL);
146  } else if (subnet.ip_prefix.is_v6()) {
147  static_cast<InetUnicastAgentRouteTable *>(vrf->
148  GetInet6UnicastRouteTable())->DeleteReq
149  (agent()->fabric_rt_export_peer(), vrf->GetName(),
150  subnet.ip_prefix, subnet.plen, NULL);
151  }
152 }
153 
156  return;
157 }
158 
159 bool VRouter::IsSubnetMember(const IpAddress &addr) const {
160  bool v4 = false;
161  if (addr.is_v4()) {
162  v4 = true;
163  }
164  VRouterSubnetSet::iterator it = subnet_list_.begin();
165  while (it != subnet_list_.end()) {
166  if (v4 && it->ip_prefix.is_v4()) {
167  if (IsIp4SubnetMember(addr.to_v4(), it->ip_prefix.to_v4(),
168  it->plen)) {
169  return true;
170  }
171  } else if (!v4 && it->ip_prefix.is_v6()) {
172  if (IsIp6SubnetMember(addr.to_v6(), it->ip_prefix.to_v6(),
173  it->plen)) {
174  return true;
175  }
176  }
177  ++it;
178  }
179  return false;
180 }
181 
183  ClearSubnets();
184 }
185 
187 // Introspect routines
189 void VRouter::FillSandeshInfo(VrouterInfoResp *resp) {
190  resp->set_name(name_);
191  resp->set_display_name(display_name_);
192  vector<VnIpamData> list;
193  VRouterSubnetSet::iterator it = subnet_list_.begin();
194  while (it != subnet_list_.end()) {
195  VnIpamData item;
196  item.set_ip_prefix(it->ip_prefix.to_string());
197  item.set_prefix_len(it->plen);
198  list.push_back(item);
199  ++it;
200  }
201  resp->set_subnet_list(list);
202 }
203 
204 void VrouterInfoReq::HandleRequest() const {
205  Agent *agent = Agent::GetInstance();
206  VRouter *vr = agent->oper_db()->vrouter();
207  VrouterInfoResp *resp = new VrouterInfoResp();
208  resp->set_context(context());
209  if (!vr) {
210  resp->set_more(false);
211  resp->Response();
212  return;
213  }
214  vr->FillSandeshInfo(resp);
215  resp->set_more(false);
216  resp->Response();
217  return;
218 }
std::string display_name_
Definition: vrouter.h:49
uint8_t plen
Definition: vrouter.h:15
void FillSandeshInfo(VrouterInfoResp *resp)
Definition: vrouter.cc:189
void DeleteRoute(const VRouterSubnet &subnet)
Definition: vrouter.cc:139
static Agent * GetInstance()
Definition: agent.h:436
RouteLeakManager * route_leak_manager() const
Definition: operdb_init.h:95
bool IsSubnetMember(const IpAddress &addr) const
Definition: vrouter.cc:159
Definition: vrf.h:86
void DeleteSubnetRoutes()
Definition: vrouter.cc:124
VrfEntry * fabric_vrf() const
Definition: agent.h:915
Agent * agent() const
Definition: oper_db.h:245
virtual ~VRouter()
Definition: vrouter.cc:46
bool IsIp4SubnetMember(const Ip4Address &ip, const Ip4Address &prefix_ip, uint16_t plen)
Definition: address_util.cc:19
bool IsDeleted() const
Definition: db_entry.h:49
ConfigManager * config_manager() const
Definition: agent.cc:889
boost::asio::ip::address IpAddress
Definition: address.h:13
VRouterSubnet()
Definition: vrouter.h:16
virtual const char * Typename() const =0
uint32_t SubnetCount() const
Definition: vrouter.h:34
adjacency_iterator end(DBGraph *graph)
const string & GetName() const
Definition: vrf.h:100
std::string name_
Definition: vrouter.h:47
IFMapTable * table()
Definition: ifmap_node.h:29
VRouter(Agent *agent)
Definition: vrouter.cc:43
void AddVirtualRouterNode(IFMapNode *node)
void ConfigAddChange(IFMapNode *node)
Definition: vrouter.cc:83
OperDB * oper_db() const
Definition: agent.cc:1013
void Remove(VRouterSubnetSet::iterator &it)
Definition: vrouter.cc:54
std::set< VRouterSubnet, VRouterSubnet > VRouterSubnetSet
Definition: vrouter.h:26
const DBGraph * GetGraph() const
void ClearSubnets()
Definition: vrouter.cc:74
bool IsLess(const VRouterSubnet *rhs) const
Definition: vrouter.cc:31
VRouter * vrouter() const
Definition: operdb_init.h:76
void Shutdown()
Definition: vrouter.cc:182
Definition: agent.h:358
void AddRoute(const VRouterSubnet &subnet)
Definition: vrouter.cc:132
void ReEvaluateRouteExports()
Definition: route_leak.cc:451
bool operator==(const VRouterSubnet &rhs) const
Definition: vrouter.cc:21
VRouterSubnetSet subnet_list_
Definition: vrouter.h:48
const std::string & name() const
Definition: ifmap_node.h:48
IFMapObject * GetObject()
Definition: ifmap_node.cc:63
void ConfigManagerEnqueue(IFMapNode *node)
Definition: vrouter.cc:154
void Insert(const VRouterSubnet *rhs)
Definition: vrouter.cc:49
IFMapNode * FindTarget(IFMapNode *node, std::string node_type) const
Definition: vrouter.cc:63
IpAddress ip_prefix
Definition: vrouter.h:14
const Peer * fabric_rt_export_peer() const
Definition: agent.h:1037
bool operator()(const VRouterSubnet &lhs, const VRouterSubnet &rhs) const
Definition: vrouter.cc:38
adjacency_iterator begin(DBGraph *graph)
bool IsIp6SubnetMember(const Ip6Address &ip, const Ip6Address &subnet, uint8_t plen)
Definition: address_util.cc:29
void ConfigDelete(IFMapNode *node)
Definition: vrouter.cc:59