OpenSDN source code
vrf_assign.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 <cmn/agent_cmn.h>
7 #include <boost/uuid/uuid_io.hpp>
8 
10 #include <oper/mirror_table.h>
11 #include <oper/vrf.h>
12 #include <oper/vrf_assign.h>
13 #include <oper/nexthop.h>
14 #include <oper/agent_sandesh.h>
15 
16 using namespace std;
17 using namespace boost::uuids;
18 
20 
22 // VRF Assignment table routines
24 std::unique_ptr<DBEntry> VrfAssignTable::AllocEntry(const DBRequestKey *k) const {
25  return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(AllocWithKey(k)));
26 }
27 
29  const VrfAssign::VrfAssignKey *key =
30  static_cast<const VrfAssign::VrfAssignKey *>(k);
31  VrfAssign *assign = NULL;
32 
34  Interface *intrface = static_cast<Interface *>
35  (agent()->interface_table()->Find(&intf_key, true));
36 
37  switch (key->type_) {
38  case VrfAssign::VLAN: {
39  VlanVrfAssign *vlan_assign =
40  new VlanVrfAssign(intrface, key->vlan_tag_);
41  assign = static_cast<VrfAssign *>(vlan_assign);
42  }
43  break;
44 
45  default:
46  assert(0);
47  break;
48  }
49 
50  return assign;
51 }
52 
54  const VrfAssign::VrfAssignKey *key =
55  static_cast<const VrfAssign::VrfAssignKey *>(req->key.get());
56  VrfAssign *rule = AllocWithKey(key);
57  if (rule->interface_.get() == NULL) {
58  delete rule;
59  return NULL;
60  }
61 
62  OnChange(rule, req);
63  return rule;
64 }
65 
66 // Assigned VRF can potentially change
67 bool VrfAssignTable::OnChange(DBEntry *entry, const DBRequest *req) {
68  return (static_cast<VrfAssign *>(entry))->Change(req);
69 }
70 
71 DBTableBase *VrfAssignTable::CreateTable(DB *db, const string &name) {
72  vrf_assign_table_ = new VrfAssignTable(db, name);
73  vrf_assign_table_->Init();
74  return vrf_assign_table_;
75 }
76 
78  VmInterfaceKey key(AgentKey::ADD_DEL_CHANGE, intf_uuid, "");
79  return static_cast<Interface *>
81 }
82 
83 VrfEntry *VrfAssignTable::FindVrf(const string &name) {
84  return Agent::GetInstance()->vrf_table()->FindVrfFromName(name);
85 }
86 
88  const std::string &vrf_name, uint16_t vlan_tag) {
89  DBRequest req;
91 
93  key->VlanInit(intf_uuid, vlan_tag);
94  req.key.reset(key);
95 
97  req.data.reset(data);
98 
99  vrf_assign_table_->Enqueue(&req);
100  return;
101 }
102 
104  uint16_t vlan_tag) {
105  DBRequest req;
107 
109  key->VlanInit(intf_uuid, vlan_tag);
110  req.key.reset(key);
111 
112  req.data.reset(NULL);
113  vrf_assign_table_->Enqueue(&req);
114  return;
115 }
116 
118  const std::string &vrf_name, uint16_t vlan_tag) {
119  DBRequest req;
121 
123  key->VlanInit(intf_uuid, vlan_tag);
124  req.key.reset(key);
125 
127  req.data.reset(data);
128 
129  vrf_assign_table_->Process(req);
130  return;
131 }
132 
134  uint16_t vlan_tag) {
135  DBRequest req;
137 
139  key->VlanInit(intf_uuid, vlan_tag);
140  req.key.reset(key);
141 
142  req.data.reset(NULL);
143  vrf_assign_table_->Process(req);
144  return;
145 }
146 
148  uint16_t vlan_tag) {
150  key.VlanInit(intf_uuid, vlan_tag);
151  return static_cast<VrfAssign *>(vrf_assign_table_->FindActiveEntry(&key));
152 }
153 
155 // VRF Assignment routines
157 bool VrfAssign::IsLess(const DBEntry &rhs) const {
158  const VrfAssign &vassign = static_cast<const VrfAssign &>(rhs);
159  if (type_ != vassign.type_) {
160  return type_ < vassign.type_;
161  }
162 
163  if (interface_.get() != vassign.interface_.get()) {
164  return interface_.get() < vassign.interface_.get();
165  }
166 
167  return VrfAssignIsLess(vassign);
168 }
169 
170 bool VrfAssign::Change(const DBRequest *req) {
171  bool ret = false;
173  static_cast<VrfAssign::VrfAssignData *>(req->data.get());
174 
176  if (vrf_.get() != vrf) {
177  vrf_ = vrf;
178  ret = true;
179  }
180 
181  if (VrfAssignChange(req)) {
182  ret = true;
183  }
184 
185  return ret;
186 }
187 
189  bool ret = false;
190  const VrfAssign::VrfAssignKey *key =
191  static_cast<const VrfAssign::VrfAssignKey *>(req->key.get());
192  VlanNHKey nh_key(key->intf_uuid_, key->vlan_tag_);
193  Agent *agent = Agent::GetInstance();
194  const NextHop *nh = static_cast<NextHop *>
195  (agent->nexthop_table()->FindActiveEntry(&nh_key));
196  assert(nh);
197  if (nh_ != nh) {
198  nh_ = nh;
199  ret = true;
200  }
201  return ret;
202 }
203 
205 // VLAN based VRF Assignment routines
208  const VlanVrfAssign &rhs_vassign = static_cast<const VlanVrfAssign &>(rhs);
209  return (vlan_tag_ < rhs_vassign.vlan_tag_);
210 }
211 
213  VrfAssignKey *key = new VrfAssignKey();
214  key->VlanInit(interface_->GetUuid(), vlan_tag_);
215  return DBEntryBase::KeyPtr(key);
216 }
217 
219  const VrfAssignKey *key = static_cast<const VrfAssignKey *>(k);
220  type_ = key->type_;
222  vlan_tag_ = key->vlan_tag_;
223 }
224 
225 bool VlanVrfAssign::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
226  VrfAssignResp *resp = static_cast<VrfAssignResp *> (sresp);
227 
228  if (!name.empty() && interface_->name().find(name) != string::npos) {
229  return false;
230  }
231 
232  VrfAssignSandeshData data;
233  assert(type_ == VrfAssign::VLAN);
234 
235  data.set_vlan_tag(vlan_tag_);
236  data.set_itf(interface_->name());
237  if (vrf_.get()) {
238  data.set_vrf(vrf_->GetName());
239  } else {
240  data.set_vrf("--ERROR--");
241  }
242  std::vector<VrfAssignSandeshData> &list =
243  const_cast<std::vector<VrfAssignSandeshData>&>
244  (resp->get_vrf_assign_list());
245  list.push_back(data);
246  return true;
247 }
248 
249 void VrfAssignReq::HandleRequest() const {
250  AgentSandeshPtr sand(new AgentVrfAssignSandesh(context(), get_uuid()));
251  sand->DoSandesh(sand);
252 }
253 
255  const std::string &context) {
256  return AgentSandeshPtr(new AgentVrfAssignSandesh(context,
257  args->GetString("name")));
258 }
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:21
AgentDBEntry * FindActiveEntry(const DBEntry *key)
Definition: agent_db.cc:111
std::string GetString(const std::string &key) const
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
VrfTable * vrf_table() const
Definition: agent.h:488
NextHopTable * nexthop_table() const
Definition: agent.h:478
static Agent * GetInstance()
Definition: agent.h:439
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
Definition: db.h:25
uint16_t vlan_tag_
Definition: vrf_assign.h:94
KeyPtr GetDBRequestKey() const
Definition: vrf_assign.cc:212
bool DBEntrySandesh(Sandesh *sresp, std::string &name) const
Definition: vrf_assign.cc:225
virtual bool VrfAssignChange(const DBRequest *req)
Definition: vrf_assign.cc:188
void SetKey(const DBRequestKey *key)
Definition: vrf_assign.cc:218
bool VrfAssignIsLess(const VrfAssign &rhs) const
Definition: vrf_assign.cc:207
static VrfEntry * FindVrf(const std::string &name)
Definition: vrf_assign.cc:83
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
Definition: vrf_assign.cc:254
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *k) const
Definition: vrf_assign.cc:24
virtual bool OnChange(DBEntry *entry, const DBRequest *req)
Definition: vrf_assign.cc:67
static VrfAssignTable * vrf_assign_table_
Definition: vrf_assign.h:134
static void DeleteVlan(const boost::uuids::uuid &intf_uuid, uint16_t vlan_tag)
Definition: vrf_assign.cc:133
static DBTableBase * CreateTable(DB *db, const std::string &name)
Definition: vrf_assign.cc:71
static void CreateVlan(const boost::uuids::uuid &intf_uuid, const std::string &vrf_name, uint16_t vlan_tag)
Definition: vrf_assign.cc:117
static VrfAssignTable * GetInstance()
Definition: vrf_assign.h:117
static void CreateVlanReq(const boost::uuids::uuid &intf_uuid, const std::string &vrf_name, uint16_t vlan_tag)
Definition: vrf_assign.cc:87
static VrfAssign * FindVlanReq(const boost::uuids::uuid &intf_uuid, uint16_t vlan_tag)
Definition: vrf_assign.cc:147
static void DeleteVlanReq(const boost::uuids::uuid &intf_uuid, uint16_t vlan_tag)
Definition: vrf_assign.cc:103
virtual DBEntry * Add(const DBRequest *req)
Definition: vrf_assign.cc:53
static Interface * FindInterface(const boost::uuids::uuid &intf_uuid)
Definition: vrf_assign.cc:77
VrfAssign * AllocWithKey(const DBRequestKey *k) const
Definition: vrf_assign.cc:28
Type type_
Definition: vrf_assign.h:66
InterfaceRef interface_
Definition: vrf_assign.h:68
bool Change(const DBRequest *req)
Definition: vrf_assign.cc:170
bool IsLess(const DBEntry &rhs) const
Definition: vrf_assign.cc:157
Definition: vrf.h:90
VrfEntry * FindVrfFromName(const string &name)
Definition: vrf.cc:874
@ ADD_DEL_CHANGE
Definition: agent_db.h:101
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
const std::string vrf_name_
Definition: vrf_assign.h:42
void VlanInit(const boost::uuids::uuid intf_uuid, uint16_t vlan_tag)
Definition: vrf_assign.h:32
boost::uuids::uuid intf_uuid_
Definition: vrf_assign.h:29
VrfAssign::Type type_
Definition: vrf_assign.h:26
boost::uuids::uuid uuid