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