OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vgw.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 #include <iostream>
5 
6 #include <base/logging.h>
7 #include <cmn/agent_cmn.h>
8 #include <init/agent_param.h>
9 #include <cfg/cfg_init.h>
10 
11 #include <oper/vm.h>
12 #include <oper/vn.h>
13 #include <oper/vrf.h>
14 #include <oper/nexthop.h>
15 #include <oper/mpls.h>
16 #include <oper/mirror_table.h>
17 #include <oper/route_common.h>
18 #include <oper/interface_common.h>
19 #include <oper/vrf_assign.h>
20 
21 #include <vgw/cfg_vgw.h>
22 #include <vgw/vgw.h>
23 
24 using namespace std;
25 
26 VirtualGateway::VirtualGateway(Agent *agent) : agent_(agent) {
28 }
29 
33  bool active_;
34 };
35 
37  if ((static_cast<Interface *>(entry))->type() != Interface::INET)
38  return;
39 
40  InetInterface *intf = static_cast<InetInterface *>(entry);
42  return;
43 
44  VirtualGatewayState *state = static_cast<VirtualGatewayState *>
45  (entry->GetState(partition->parent(), listener_id_));
46 
47  if (entry->IsDeleted()) {
48  if (state) {
49  entry->ClearState(partition->parent(), listener_id_);
50  delete state;
51  }
52  return;
53  }
54 
55  bool active = intf->ipv4_active();
56  VirtualGatewayConfig cfg(intf->name());
57  VirtualGatewayConfigTable::Table::iterator it =
58  vgw_config_table_->table().find(cfg);
59  if (it == vgw_config_table_->table().end())
60  return;
61 
62  if (state == NULL) {
63  state = new VirtualGatewayState();
64  entry->SetState(partition->parent(), listener_id_, state);
65  it->set_interface(intf);
66  }
67 
68  InetUnicastAgentRouteTable *rt_table =
69  (agent_->vrf_table()->GetInet4UnicastRouteTable(it->vrf_name()));
70 
71  state->active_ = active;
72 
73  // Add gateway routes in virtual-network.
74  // BGP will export the route to other compute nodes
76  if (active) {
77  RouteUpdate(*it, it->routes().size(), rt_table,
78  it->routes(), empty_list, true, false);
79  } else {
80  RouteUpdate(*it, it->routes().size(), rt_table,
81  empty_list, it->routes(), false, true);
82  }
83 
84  if (active) {
85  // Packets received on fabric vrf and destined to IP address in
86  // "public" network must reach kernel. Add a route in "fabric" VRF
87  // inside vrouter to trap packets destined to "public" network
88  SubnetUpdate(it->vrf_name(), rt_table, it->subnets(), empty_list);
89  } else {
90  // Delete the trap route added above
91  SubnetUpdate(it->vrf_name(), rt_table, empty_list, it->subnets());
92  }
93 }
94 
97  (boost::bind(&VirtualGateway::InterfaceNotify, this, _1, _2));
98 }
99 
100 // Create VRF for "public" virtual-network
102  if (vgw_config_table_ == NULL) {
103  return;
104  }
105  VirtualGatewayConfigTable::Table::iterator it;
107  for (it = table.begin(); it != table.end(); it++) {
108  CreateVrf(it->vrf_name());
109  }
110 }
111 
112 void VirtualGateway::CreateVrf(const std::string &vrf_name) {
114  vrf_name, boost::uuids::nil_uuid(), VrfData::GwVrf);
115 }
116 
117 void VirtualGateway::DeleteVrf(const std::string &vrf_name) {
118  agent_->vrf_table()->DeleteVrf(vrf_name, VrfData::GwVrf);
119 }
120 
121 // Create virtual-gateway interface
123  if (vgw_config_table_ == NULL) {
124  return;
125  }
126 
127  VirtualGatewayConfigTable::Table::iterator it;
129  for (it = table.begin(); it != table.end(); it++) {
130  CreateInterface(it->interface_name(), it->vrf_name(), transport);
131  }
132 }
133 
134 void VirtualGateway::CreateInterface(const std::string &interface_name,
135  const std::string &vrf_name,
136  Interface::Transport transport) {
137  InetInterface::Create(agent_->interface_table(), interface_name,
140  "", transport);
141 }
142 
143 void VirtualGateway::DeleteInterface(const std::string &interface_name) {
144  InetInterface::Delete(agent_->interface_table(), interface_name);
145 }
146 
147 void
149  const VirtualGatewayConfig::SubnetList &add_list,
150  const VirtualGatewayConfig::SubnetList &del_list) {
151  if (vgw.get_interface() && !vgw.get_interface()->ipv4_active())
152  return;
153 
154  InetUnicastAgentRouteTable *rt_table =
156  if (!rt_table)
157  return;
158 
159  SubnetUpdate(vgw.vrf_name(), rt_table, add_list, del_list);
160 }
161 
162 void
163 VirtualGateway::SubnetUpdate(const std::string &vrf,
164  InetUnicastAgentRouteTable *rt_table,
165  const VirtualGatewayConfig::SubnetList &add_list,
166  const VirtualGatewayConfig::SubnetList &del_list) {
167  for (uint32_t idx = 0; idx < add_list.size(); idx++) {
168  Ip4Address addr = Address::GetIp4SubnetAddress(add_list[idx].ip_,
169  add_list[idx].plen_);
171  boost::uuids::nil_uuid(),
173  rt_table->AddVHostRecvRouteReq(agent_->vgw_peer(),
175  vmi_key,
176  addr, add_list[idx].plen_,
177  vrf, false, true);
178  }
179  for (uint32_t idx = 0; idx < del_list.size(); idx++) {
180  Ip4Address addr = Address::GetIp4SubnetAddress(del_list[idx].ip_,
181  del_list[idx].plen_);
183  addr, del_list[idx].plen_, NULL);
184  }
185 }
186 
187 void
189  const VirtualGatewayConfig::SubnetList &new_list,
190  const VirtualGatewayConfig::SubnetList &add_list,
191  const VirtualGatewayConfig::SubnetList &del_list,
192  bool add_default_route) {
193  if (vgw.get_interface() && !vgw.get_interface()->ipv4_active())
194  return;
195 
196  InetUnicastAgentRouteTable *rt_table =
198 
199  RouteUpdate(vgw, new_list.size(), rt_table, add_list, del_list,
200  add_default_route, true);
201 }
202 
203 void
205  uint32_t new_list_size,
206  InetUnicastAgentRouteTable *rt_table,
207  const VirtualGatewayConfig::SubnetList &add_list,
208  const VirtualGatewayConfig::SubnetList &del_list,
209  bool add_default_route, bool del_default_route) {
210  VnListType name_list;
211  name_list.insert(vgw.vrf_name());
212  if (vgw.routes().size() == 0 && del_default_route) {
213  // no routes earlier, remove default route
214  rt_table->DeleteReq(agent_->vgw_peer(), vgw.vrf_name(),
215  Ip4Address(0), 0, NULL);
216  } else if (new_list_size == 0 && add_default_route) {
217  // no routes now, add a default route
218  rt_table->AddInetInterfaceRouteReq(agent_->vgw_peer(), vgw.vrf_name(),
219  Ip4Address(0), 0,
220  vgw.interface_name(),
221  vgw.get_interface()->label(),
222  name_list);
223  }
224  // remove old routes, add new routes
225  for (uint32_t idx = 0; idx < del_list.size(); idx++) {
226  Ip4Address addr = Address::GetIp4SubnetAddress(del_list[idx].ip_,
227  del_list[idx].plen_);
228  rt_table->DeleteReq(agent_->vgw_peer(), vgw.vrf_name(),
229  addr, del_list[idx].plen_, NULL);
230  }
231  for (uint32_t idx = 0; idx < add_list.size(); idx++) {
232  Ip4Address addr = Address::GetIp4SubnetAddress(add_list[idx].ip_,
233  add_list[idx].plen_);
235  vgw.vrf_name(), addr,
236  add_list[idx].plen_,
237  vgw.interface_name(),
238  vgw.get_interface()->label(),
239  name_list);
240  }
241 }
242 
244 }
245 
247  if (vgw_config_table_ == NULL)
248  return;
249 
250  VirtualGatewayConfigTable::Table::iterator it;
252  for (it = table.begin(); it != table.end(); it++) {
253  // Delete Interface
255  it->interface_name());
256 
257  // Delete VRF for "public" virtual-network
258  agent_->vrf_table()->DeleteStaticVrf(it->vrf_name());
259  }
260 }
void SubnetUpdate(const VirtualGatewayConfig &vgw, const VirtualGatewayConfig::SubnetList &add_list, const VirtualGatewayConfig::SubnetList &del_list)
Definition: vgw.cc:148
const SubnetList & routes() const
Definition: cfg_vgw.h:50
void CreateVrf(const string &name, const boost::uuids::uuid &vn_uuid, uint32_t flags)
Definition: vrf.cc:970
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:37
void RegisterDBClients()
Definition: vgw.cc:95
void DeleteVrf(const string &name, uint32_t flags=VrfData::ConfigVrf)
Definition: vrf.cc:987
bool IsDeleted() const
Definition: db_entry.h:49
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:22
void CreateInterfaces(Interface::Transport transport)
Definition: vgw.cc:122
Agent * agent_
Definition: vgw.h:47
void DeleteInterface(const std::string &interface_name)
Definition: vgw.cc:143
DBTableBase * parent()
const std::string & vhost_interface_name() const
Definition: agent.cc:104
InterfaceTable * interface_table() const
Definition: agent.h:465
void DeleteStaticVrf(const string &name)
Definition: vrf.cc:1011
static Ip4Address GetIp4SubnetAddress(const Ip4Address &prefix, uint16_t plen)
Definition: address.cc:179
void Shutdown()
Definition: vgw.cc:246
static void AddVHostRecvRouteReq(const Peer *peer, const string &vrf, const InterfaceKey &interface, const IpAddress &addr, uint8_t plen, const string &vn_name, bool policy, bool native_encap)
InetUnicastAgentRouteTable * GetInet4UnicastRouteTable(const std::string &vrf_name)
Definition: vrf.cc:898
void RouteUpdate(const VirtualGatewayConfig &vgw, const VirtualGatewayConfig::SubnetList &new_list, const VirtualGatewayConfig::SubnetList &add_list, const VirtualGatewayConfig::SubnetList &del_list, bool add_default_route)
Definition: vgw.cc:188
uint32_t label() const
Definition: interface.h:127
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:181
const std::string & fabric_vrf_name() const
Definition: agent.h:903
~VirtualGatewayState()
Definition: vgw.cc:32
uint8_t type
Definition: load_balance.h:109
Definition: agent.h:358
static void Create(InterfaceTable *table, const std::string &ifname, SubType sub_type, const std::string &vrf_name, const Ip4Address &addr, int plen, const Ip4Address &gw, const std::string &xconnect, const std::string &vn_name, Interface::Transport transport)
bool ipv4_active() const
Definition: interface.h:116
const std::string & interface_name() const
Definition: cfg_vgw.h:47
static const std::string & NullString()
Definition: agent.h:437
const Peer * vgw_peer() const
Definition: agent.h:1026
const Table & table() const
Definition: cfg_vgw.h:129
AgentParam * params() const
Definition: agent.h:1218
std::set< std::string > VnListType
Definition: agent.h:212
static void DeleteReq(const Peer *peer, const string &vrf_name, const IpAddress &addr, uint8_t plen, AgentRouteData *data)
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:73
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
void InterfaceNotify(DBTablePartBase *partition, DBEntryBase *entry)
Definition: vgw.cc:36
VrfTable * vrf_table() const
Definition: agent.h:485
void Delete()
Definition: db_entry.cc:131
VirtualGatewayConfigTable * vgw_config_table_
Definition: vgw.h:49
void CreateInterface(const std::string &interface_name, const std::string &vrf_name, Interface::Transport transport)
Definition: vgw.cc:134
VirtualGateway(Agent *agent)
Definition: vgw.cc:26
void Init()
Definition: vgw.cc:243
void AddInetInterfaceRouteReq(const Peer *peer, const string &vm_vrf, const Ip4Address &addr, uint8_t plen, InetInterfaceRoute *data)
VirtualGatewayState()
Definition: vgw.cc:31
std::set< VirtualGatewayConfig, VirtualGatewayConfigCompare > Table
Definition: cfg_vgw.h:121
const std::string & name() const
Definition: interface.h:114
std::vector< Subnet > SubnetList
Definition: cfg_vgw.h:30
const InetInterface * get_interface() const
Definition: cfg_vgw.h:54
DBTableBase::ListenerId listener_id_
Definition: vgw.h:48
SubType sub_type() const
VirtualGatewayConfigTable * vgw_config_table() const
Definition: agent_param.h:329
void DeleteVrf(const std::string &vrf_name)
Definition: vgw.cc:117
void CreateVrf()
Definition: vgw.cc:101
const std::string & vrf_name() const
Definition: cfg_vgw.h:48