OpenSDN source code
inet4_multicast_route.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <boost/uuid/uuid_io.hpp>
7 
8 #include <cmn/agent_cmn.h>
9 #include <route/route.h>
10 
11 #include <oper/route_common.h>
12 #include <oper/vrf.h>
13 #include <oper/tunnel_nh.h>
14 #include <oper/mpls.h>
15 #include <oper/vxlan.h>
16 #include <oper/mirror_table.h>
17 #include <oper/multicast.h>
21 #include <oper/agent_sandesh.h>
22 
23 using namespace std;
24 using namespace boost::asio;
25 
26 static void MulticastTableEnqueue(Agent *agent, DBRequest *req) {
28  if (table) {
29  table->Enqueue(req);
30  }
31 }
32 
33 static void MulticastTableProcess(Agent *agent, const string &vrf_name,
34  DBRequest &req) {
35  AgentRouteTable *table =
36  agent->vrf_table()->GetInet4MulticastRouteTable(vrf_name);
37  if (table) {
38  table->Process(req);
39  }
40 }
41 
43  const std::string &name) {
44  AgentRouteTable *table = new Inet4MulticastAgentRouteTable(db, name);
45  table->Init();
46  //table->InitRouteTable(db, table, name, Agent::INET4_MULTICAST);
47  return table;
48 }
49 
50 void
52  const string &vn_name,
53  const Ip4Address &src_addr,
54  const Ip4Address &grp_addr,
56  &component_nh_key_list) {
57  DBRequest nh_req;
58  NextHopKey *nh_key;
59  CompositeNHData *nh_data;
60 
61  nh_key = new CompositeNHKey(Composite::L3COMP, true, component_nh_key_list,
62  vrf_name);
64  nh_req.key.reset(nh_key);
65  nh_data = new CompositeNHData();
66  nh_req.data.reset(nh_data);
67 
68  DBRequest req;
70  Inet4MulticastRouteKey *rt_key = new Inet4MulticastRouteKey(vrf_name,
71  grp_addr,
72  src_addr);
73  MulticastRoute *data = new MulticastRoute(vn_name,
77  nh_req, Composite::L3COMP, 0);
78  req.key.reset(rt_key);
79  req.data.reset(data);
81 }
82 
83 void
85  const string &vrf_name,
86  const Ip4Address &src_addr,
87  const Ip4Address &grp_addr,
88  uint32_t ethernet_tag,
89  AgentRouteData *data) {
90 
91  DBRequest req;
93  Inet4MulticastRouteKey *rt_key = new Inet4MulticastRouteKey(peer, vrf_name,
94  grp_addr, src_addr, ethernet_tag);
95  req.key.reset(rt_key);
96  req.data.reset(data);
98 }
99 
100 void
102  const string &interface_name,
103  const Ip4Address &addr,
104  bool policy)
105 {
106  DBRequest req;
107 
109  Inet4MulticastRouteKey *rt_key = new Inet4MulticastRouteKey(vm_vrf, addr);
110  req.key.reset(rt_key);
112  boost::uuids::nil_uuid(), interface_name);
113  ReceiveRoute *data =
115  TunnelType::AllType(), policy,
116  Agent::GetInstance()->fabric_vn_name());
117  //data->SetMulticast(true);
118  req.data.reset(data);
120 }
121 
122 void
124  const Ip4Address &src_addr,
125  const Ip4Address &grp_addr)
126 {
127  DBRequest req;
128 
130  Inet4MulticastRouteKey *rt_key = new Inet4MulticastRouteKey(vrf_name,
131  grp_addr,
132  src_addr);
133 
134  req.key.reset(rt_key);
135  req.data.reset(NULL);
137 }
138 
139 void
141  const string &vrf_name,
142  const Ip4Address &src_addr,
143  const Ip4Address &grp_addr,
144  uint32_t ethernet_tag,
146 
148  Inet4MulticastRouteKey *rt_key = new Inet4MulticastRouteKey(peer, vrf_name,
149  grp_addr, src_addr, ethernet_tag);
150  req.key.reset(rt_key);
151 
152  DBRequest nh_req;
153  const BgpPeer *bgp_peer = dynamic_cast<const BgpPeer *>(peer);
154  if (bgp_peer) {
155  req.data.reset(new MulticastRoute("", 0, ethernet_tag,
156  TunnelType::AllType(), nh_req, type,
157  bgp_peer->sequence_number()));
158  } else {
159  req.data.reset(new MulticastRoute("", 0, ethernet_tag,
160  TunnelType::AllType(), nh_req, type, 0));
161  }
162 
164 }
165 
166 void Inet4MulticastAgentRouteTable::Delete(const string &vrf_name,
167  const Ip4Address &src_addr,
168  const Ip4Address &grp_addr) {
170  req.key.reset(new Inet4MulticastRouteKey(vrf_name, grp_addr, src_addr));
171  req.data.reset(NULL);
172  MulticastTableProcess(Agent::GetInstance(), vrf_name, req);
173 }
174 
175 AgentRoute *
176 Inet4MulticastRouteKey::AllocRouteEntry(VrfEntry *vrf, bool is_multicast) const
177 {
178  Inet4MulticastRouteEntry * entry = new Inet4MulticastRouteEntry(vrf, dip_,
179  sip_);
180  return static_cast<AgentRoute *>(entry);
181 }
182 
184  return (new Inet4MulticastRouteKey(vrf_name_, dip_, sip_));
185 }
186 
189  const Ip4Address &src_addr) {
190 
191  Inet4MulticastRouteEntry entry(vrf_entry(), grp_addr, src_addr);
192  return static_cast<Inet4MulticastRouteEntry *>(FindActiveEntry(&entry));
193 }
194 
196  ostringstream str;
197  str << "Group:";
198  str << dip_.to_string();
199  str << " Src:";
200  str << sip_.to_string();
201  return str.str();
202 }
203 
205  ostringstream str;
206  str << "Group:";
207  str << dst_addr_.to_string();
208  str << " Src:";
209  str << src_addr_.to_string();
210  return str.str();
211 }
212 
214  const Inet4MulticastRouteEntry &a =
215  static_cast<const Inet4MulticastRouteEntry &>(rhs);
216 
217  if (src_addr_ < a.src_addr_)
218  return -1;
219 
220  if (src_addr_ > a.src_addr_)
221  return 1;
222 
223  if (dst_addr_ < a.dst_addr_)
224  return -1;
225 
226  if (dst_addr_ > a.dst_addr_)
227  return 1;
228 
229  return 0;
230 }
231 
234  new Inet4MulticastRouteKey(vrf()->GetName(), dst_addr_,
235  src_addr_);
236  return DBEntryBase::KeyPtr(key);
237 }
238 
240  const Inet4MulticastRouteKey *k =
241  static_cast<const Inet4MulticastRouteKey *>(key);
242  SetVrf(Agent::GetInstance()->vrf_table()->FindVrfFromName(k->vrf_name()));
243  Ip4Address grp(k->dest_ip_addr());
244  Ip4Address src(k->src_ip_addr());
245  set_dest_ip_addr(grp);
246  set_src_ip_addr(src);
247 }
248 
250  return ReComputeMulticastPaths(path, false);
251 }
252 
254  return ReComputeMulticastPaths(path, true);
255 }
256 
257 bool Inet4MulticastRouteEntry::DBEntrySandesh(Sandesh *sresp, bool stale) const {
258  Inet4McRouteResp *resp = static_cast<Inet4McRouteResp *>(sresp);
259 
260  RouteMcSandeshData data;
261  data.set_src(src_ip_addr().to_string());
262  data.set_grp(dest_ip_addr().to_string());
264  FindGroupObject(vrf()->GetName(), Ip4Address(), dest_ip_addr());
265  Agent *agent = (static_cast<AgentRouteTable *>(get_table()))->agent();
266  const NextHop *anh = GetActiveNextHop();
267  if (!stale || anh || (mc_obj && (mc_obj->peer_identifier() != agent->controller()->
268  multicast_sequence_number()))) {
269  anh->SetNHSandeshData(data.nh);
270  }
271 
272  std::vector<RouteMcSandeshData> &list =
273  const_cast<std::vector<RouteMcSandeshData>&>(resp->get_route_list());
274  list.push_back(data);
275  return true;
276 }
277 
279  const Ip4Address &src_addr,
280  const Ip4Address &dst_addr,
281  bool stale) const {
282 
283  if (src_addr_ == src_addr && dst_addr_ == dst_addr) {
284  return DBEntrySandesh(sresp, stale);
285  }
286 
287  return false;
288 }
289 
290 void Inet4McRouteReq::HandleRequest() const {
291  VrfEntry *vrf =
292  Agent::GetInstance()->vrf_table()->FindVrfFromId(get_vrf_index());
293  if (!vrf) {
294  ErrorResp *resp = new ErrorResp();
295  resp->set_context(context());
296  resp->Response();
297  return;
298  }
299 
300  AgentSandeshPtr sand(new AgentInet4McRtSandesh(vrf, context(), "",
301  get_stale()));
302  boost::system::error_code ec;
303  Ip4Address zero_addr = IpAddress::from_string("0.0.0.0", ec).to_v4();
304  Ip4Address src_ip, grp_ip;
305  if (get_src_ip().empty()) {
306  src_ip = zero_addr;
307  } else {
308  src_ip = Ip4Address::from_string(get_src_ip(), ec);
309  }
310  if (get_grp_ip().empty()) {
311  sand.reset(new AgentInet4McRtSandesh(vrf, context(), "", get_stale()));
312  } else {
313  grp_ip = Ip4Address::from_string(get_grp_ip(), ec);
314  sand.reset(new AgentInet4McRtSandesh(vrf, context(), "", src_ip,
315  grp_ip, get_stale()));
316  }
317  sand->DoSandesh(sand);
318 }
319 
321 (const AgentSandeshArguments *args, const std::string &context) {
322  return AgentSandeshPtr(new AgentInet4McRtSandesh(vrf_entry(), context, "",
323  false));
324 }
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:21
Agent supports multiple route tables - Inet-unicast (IPv4/IPv6), Inet-multicast, bridge,...
Definition: agent_route.h:110
void Process(DBRequest &req)
Definition: agent_route.cc:187
Base class for all Route entries in agent.
Definition: agent_route.h:225
Definition: agent.h:361
VNController * controller() const
Definition: agent.cc:985
VrfTable * vrf_table() const
Definition: agent.h:488
Inet4MulticastAgentRouteTable * fabric_inet4_multicast_table() const
Definition: agent.h:603
static Agent * GetInstance()
Definition: agent.h:439
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
bool Enqueue(DBRequest *req)
Definition: db_table.cc:221
void Init()
Definition: db_table.cc:414
Definition: db.h:25
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
static void DeleteMulticastRoute(const string &vrf_name, const Ip4Address &src_addr, const Ip4Address &grp_addr)
void AddVHostRecvRoute(const string &vm_vrf, const string &interface_name, const Ip4Address &addr, bool policy)
static void Delete(const string &vrf_name, const Ip4Address &src_addr, const Ip4Address &grp_addr)
static void AddMulticastRoute(const string &vn_name, const string &vrf_name, const Ip4Address &src_addr, const Ip4Address &grp_addr, ComponentNHKeyList &component_nh_key_list)
Inet4MulticastRouteEntry * FindRoute(const Ip4Address &grp_addr, const Ip4Address &src_addr)
static DBTableBase * CreateTable(DB *db, const std::string &name)
virtual int CompareTo(const Route &rhs) const
virtual bool ReComputePathDeletion(AgentPath *path)
virtual string ToString() const
virtual void SetKey(const DBRequestKey *key)
virtual bool ReComputePathAdd(AgentPath *path)
virtual bool DBEntrySandesh(Sandesh *sresp, bool stale) const
virtual KeyPtr GetDBRequestKey() const
virtual AgentRoute * AllocRouteEntry(VrfEntry *vrf, bool is_multicast) const
virtual AgentRouteKey * Clone() const
const Ip4Address & dest_ip_addr() const
const Ip4Address & src_ip_addr() const
virtual string ToString() const
static const uint32_t kInvalidLabel
Definition: mpls.h:102
uint64_t peer_identifier()
Definition: multicast.h:145
static MulticastHandler * GetInstance()
Definition: multicast.h:370
void SetNHSandeshData(NhSandeshData &data) const
Definition: nexthop.cc:3173
Definition: peer.h:45
uint64_t sequence_number() const
Definition: peer.h:95
Definition: route.h:15
static TypeBmap AllType()
Definition: nexthop.h:322
Definition: vrf.h:90
VrfEntry * FindVrfFromId(size_t index)
Definition: vrf.cc:885
AgentRouteTable * GetInet4MulticastRouteTable(const std::string &vrf_name)
Definition: vrf.cc:910
static const uint32_t kInvalidvxlan_id
Definition: vxlan.h:141
static void MulticastTableProcess(Agent *agent, const string &vrf_name, DBRequest &req)
static void MulticastTableEnqueue(Agent *agent, DBRequest *req)
uint8_t type
Definition: load_balance.h:2
#define COMPOSITETYPE
Definition: nexthop.h:1601
std::vector< ComponentNHKeyPtr > ComponentNHKeyList
Definition: nexthop.h:1642
@ ADD_DEL_CHANGE
Definition: agent_db.h:101
const std::string & vrf_name() const
Definition: agent_route.h:47
DBOperation oper
Definition: db_table.h:43
@ DB_ENTRY_DELETE
Definition: db_table.h:40
@ DB_ENTRY_ADD_CHANGE
Definition: db_table.h:39
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:49
std::unique_ptr< DBRequestData > data
Definition: db_table.h:50