OpenSDN source code
qos_config.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.h>
7 #include <vnc_cfg_types.h>
8 #include <agent_types.h>
9 #include <oper/agent_sandesh.h>
10 #include <cfg/cfg_init.h>
11 #include <oper_db.h>
12 #include <oper/config_manager.h>
13 #include <oper/interface_common.h>
14 #include <forwarding_class.h>
15 #include <qos_config.h>
19 
20 using namespace autogen;
22  uuid_(uuid), id_(AgentQosConfigTable::kInvalidIndex) {
23 }
24 
27  static_cast<AgentQosConfigTable *>(get_table())->ReleaseIndex(this);
28  }
29 }
30 
33  return DBEntryBase::KeyPtr(key);
34 }
35 
36 std::string AgentQosConfig::ToString() const {
37  std::ostringstream buffer;
38  buffer << uuid_;
39  return buffer.str();
40 }
41 
42 bool AgentQosConfig::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
43  AgentQosConfigSandeshResp *resp =
44  static_cast<AgentQosConfigSandeshResp *>(sresp);
45  AgentQosConfigSandeshData data;
46 
47  data.set_uuid(UuidToString(uuid_));
48  data.set_name(name_);
49  data.set_id(id_);
50  if (type_ == VHOST) {
51  data.set_type("vhost");
52  } else if (type_ == FABRIC) {
53  data.set_type("fabric");
54  } else {
55  data.set_type("default");
56  }
57 
58  std::vector<QosForwardingClassSandeshPair> dscp_list;
59  AgentQosConfig::QosIdForwardingClassMap::const_iterator it =
60  dscp_map_.begin();
61  for(; it != dscp_map_.end(); it++) {
62  QosForwardingClassSandeshPair pair;
63  pair.set_qos_value(it->first);
64  pair.set_forwarding_class_id(it->second);
65  dscp_list.push_back(pair);
66  }
67 
68  std::vector<QosForwardingClassSandeshPair> vlan_priority_list;
69  it = vlan_priority_map_.begin();
70  for(; it != vlan_priority_map_.end(); it++) {
71  QosForwardingClassSandeshPair pair;
72  pair.set_qos_value(it->first);
73  pair.set_forwarding_class_id(it->second);
74  vlan_priority_list.push_back(pair);
75  }
76 
77  std::vector<QosForwardingClassSandeshPair> mpls_exp_list;
78  it = mpls_exp_map_.begin();
79  for(; it != mpls_exp_map_.end(); it++) {
80  QosForwardingClassSandeshPair pair;
81  pair.set_qos_value(it->first);
82  pair.set_forwarding_class_id(it->second);
83  mpls_exp_list.push_back(pair);
84  }
85 
86  data.set_dscp_list(dscp_list);
87  data.set_vlan_priority_list(vlan_priority_list);
88  data.set_mpls_exp_list(mpls_exp_list);
89  data.set_default_forwarding_class(default_forwarding_class_);
90 
91  std::vector<AgentQosConfigSandeshData> &list =
92  const_cast<std::vector<AgentQosConfigSandeshData>&>(resp->get_qc_list());
93  list.push_back(data);
94  return true;
95 }
96 
97 bool AgentQosConfig::IsLess(const DBEntry &rhs) const {
98  const AgentQosConfig &fc = static_cast<const AgentQosConfig &>(rhs);
99  return (uuid_ < fc.uuid_);
100 }
101 
104  const
106  &data_map) {
107 
108  QosIdForwardingClassMap new_map;
109  AgentQosIdForwardingClassMap::const_iterator it = data_map.begin();
110  for (; it != data_map.end(); it++) {
111  new_map.insert(QosIdForwardingClassPair(it->first, it->second));
112  }
113 
114  if (map != new_map) {
115  map = new_map;
116  return true;
117  }
118 
119  return false;
120 }
121 
123  const AgentQosConfigData *data) {
124  IFMapNode *node = data->ifmap_node();
125  IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
126 
128  node->begin(table->GetGraph()); iter != node->end(table->GetGraph());
129  ++iter) {
130  const IFMapNode *adj_node = static_cast<const IFMapNode *>(iter.operator->());
131  if (adj_node->table() == agent->cfg()->cfg_global_qos_table()) {
132  return true;
133  }
134  }
135  return false;
136 }
137 
139  const AgentQosConfigData *data, bool deleted) {
140  if (deleted == false) {
141  //Verify that link to Global QoS config exists
142  if (VerifyLinkToGlobalQosConfig(agent, data) == false) {
143  deleted = true;
144  }
145  }
146 
147  AgentQosConfigTable *table =
148  static_cast<AgentQosConfigTable *>(agent->qos_config_table());
149 
150  if (deleted) {
151  table->EraseVhostQosConfig(uuid());
152  } else {
153  table->InsertVhostQosConfig(uuid());
154  }
155 
157  VmInterfaceKey *key =
158  new VmInterfaceKey(AgentKey::ADD_DEL_CHANGE, boost::uuids::nil_uuid(),
159  agent->vhost_interface_name());
160  key->sub_op_ = AgentKey::RESYNC;
161  boost::uuids::uuid qos_config_uuid = table->GetActiveVhostQosConfig();
162  InterfaceQosConfigData *qos_data =
163  new InterfaceQosConfigData(agent, NULL, qos_config_uuid);
164  req.key.reset(key);
165  req.data.reset(qos_data);
166  agent->interface_table()->Enqueue(&req);
167 }
168 
170  const AgentQosConfigData *data, bool deleted) {
171 
172  if (deleted == false) {
173  //Verify that link to Global QoS config exists
174  if (VerifyLinkToGlobalQosConfig(agent, data) == false) {
175  deleted = true;
176  }
177  }
178 
179  AgentQosConfigTable *table =
180  static_cast<AgentQosConfigTable *>(agent->qos_config_table());
181 
182  if (deleted) {
183  table->EraseFabricQosConfig(uuid());
184  } else {
185  table->InsertFabricQosConfig(uuid());
186  }
187 
189  PhysicalInterfaceKey *key =
191  key->sub_op_ = AgentKey::RESYNC;
192 
193  boost::uuids::uuid qos_config_uuid = table->GetActiveFabricQosConfig();
194  InterfaceQosConfigData *qos_data =
195  new InterfaceQosConfigData(agent, NULL, qos_config_uuid);
196  req.key.reset(key);
197  req.data.reset(qos_data);
198  agent->interface_table()->Enqueue(&req);
199 }
200 
202  if (data->ifmap_node() == NULL) {
203  return;
204  }
205  if (type_ == VHOST) {
206  HandleVhostQosConfig(data->agent(), data, false);
207  } else {
208  HandleFabricQosConfig(data->agent(), data, false);
209  }
210 }
211 
213  bool ret = false;
214  const AgentQosConfigData *data =
215  static_cast<const AgentQosConfigData *>(req->data.get());
216 
217  //Ignore change notification if entry node is marked for deletion
218  if ((data->ifmap_node() == NULL) || (data->ifmap_node()->IsDeleted())) {
219  return ret;
220  }
221 
222  if (name_ != data->name_) {
223  name_ = data->name_;
224  ret = true;
225  }
226 
227  if (HandleQosForwardingMapChange(data->agent(), dscp_map_, data->dscp_map_)) {
228  ret = true;
229  }
230 
232  data->vlan_priority_map_)) {
233  ret = true;
234  }
235 
237  data->mpls_exp_map_)) {
238  ret = true;
239  }
240 
241  if (type_ != data->type_) {
242  type_ = data->type_;
243  ret = true;
244  }
245 
246  if (type_ != DEFAULT) {
247  HandleGlobalQosConfig(data);
248  }
249 
252  ret = true;
253  }
254 
255  return ret;
256 }
257 
259  Agent *agent = (static_cast<AgentOperDBTable *>(get_table())->agent());
260  if (type_ == VHOST) {
261  HandleVhostQosConfig(agent, NULL, true);
262  } else if (type_ == FABRIC) {
263  HandleFabricQosConfig(agent, NULL, true);
264  }
265 }
266 
268  const AgentQosConfigKey *fc_key =
269  static_cast<const AgentQosConfigKey *>(key);
270  uuid_ = fc_key->uuid_;
271 }
272 
274  DB *db, const std::string &name):
275  AgentOperDBTable(db, name) {
276  set_agent(agent);
277 }
278 
280 }
281 
283 AgentQosConfigTable::CreateTable(Agent *agent, DB *db, const std::string &name) {
284  AgentQosConfigTable *qos_table = new AgentQosConfigTable(agent, db, name);
285  (static_cast<DBTable *>(qos_table))->Init();
286  return qos_table;
287 }
288 
289 std::unique_ptr<DBEntry>
291  const AgentQosConfigKey *key =
292  static_cast<const AgentQosConfigKey *>(k);
293  AgentQosConfig *fc = new AgentQosConfig(key->uuid_);
294  return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(fc));
295 }
296 
298  const AgentQosConfigKey *key =
299  static_cast<const AgentQosConfigKey *>(req->key.get());
300  AgentQosConfig *qc = new AgentQosConfig(key->uuid_);
302  agent()->resource_manager(), key->uuid_));
303  int id = static_cast<IndexResourceData *>(agent()->resource_manager()->
304  Allocate(rkey).get())->index();
305  index_table_.InsertAtIndex(id, qc);
306  qc->set_id(id);
307  qc->Change(req);
308  name_map_.insert(AgentQosConfigNamePair(qc->name(), qc));
309  return static_cast<DBEntry *>(qc);
310 }
311 
313  AgentQosConfig *qc = static_cast<AgentQosConfig *>(entry);
314  return qc->Change(req);
315 }
316 
318  return OperDBOnChange(entry, req);
319 }
320 
322  AgentQosConfig *qc = static_cast<AgentQosConfig *>(entry);
323  qc->Delete(req);
324  name_map_.erase(qc->name());
325  return true;
326 }
327 
329  if (qc->id() != kInvalidIndex) {
331  index_table_.Remove(qc->id());
332  }
333 }
334 
336  const boost::uuids::uuid &u) {
337  assert(!u.is_nil());
338 
339  if ((req.oper == DBRequest::DB_ENTRY_DELETE) || node->IsDeleted()) {
340  req.key.reset(new AgentQosConfigKey(u));
342  Enqueue(&req);
343  return false;
344  }
345 
347  return false;
348 }
349 
351  autogen::QosConfig *cfg = static_cast <autogen::QosConfig *> (node->GetObject());
352  assert(cfg);
353  autogen::IdPermsType id_perms = cfg->id_perms();
354  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u);
355  return true;
356 }
357 
359  const boost::uuids::uuid &u) {
360  if (node->IsDeleted()) {
361  return false;
362  }
363 
365  req.key.reset(new AgentQosConfigKey(u));
366  req.data.reset(BuildData(node));
367  Enqueue(&req);
368  return false;
369 }
370 
373  AgentQosConfigData *qcd = new AgentQosConfigData(agent(), node);
374  autogen::QosConfig *cfg = static_cast <autogen::QosConfig *> (node->GetObject());
375 
376  std::vector<QosIdForwardingClassPair>::const_iterator it =
377  cfg->dscp_entries().begin();
378  for(; it != cfg->dscp_entries().end(); it++) {
379  qcd->dscp_map_[it->key] = it->forwarding_class_id;
380  }
381 
382  for(it = cfg->vlan_priority_entries().begin();
383  it != cfg->vlan_priority_entries().end(); it++) {
384  qcd->vlan_priority_map_[it->key] = it->forwarding_class_id;
385  }
386 
387  for(it = cfg->mpls_exp_entries().begin();
388  it != cfg->mpls_exp_entries().end(); it++) {
389  qcd->mpls_exp_map_[it->key] = it->forwarding_class_id;
390  }
391 
392  qcd->name_ = node->name();
393  if (cfg->type() == "vhost") {
395  } else if (cfg->type() == "fabric") {
397  } else {
399  }
400 
401  qcd->default_forwarding_class_ = cfg->default_forwarding_class_id();
402 
403  return qcd;
404 }
405 
408  const std::string &context) {
409  return AgentSandeshPtr(new AgentQosConfigSandesh(context,
410  args->GetString("uuid"), args->GetString("name"),
411  args->GetString("id")));
412 }
413 
414 void AgentQosConfigSandeshReq::HandleRequest() const {
415  AgentSandeshPtr sand(new AgentQosConfigSandesh(context(), get_uuid(),
416  get_name(), get_id()));
417  sand->DoSandesh(sand);
418 }
419 
420 void AddQosConfig::HandleRequest() const {
421  QosResponse *resp = new QosResponse();
422  resp->set_context(context());
423 
424  char str[50];
425  sprintf(str, "00000000-0000-0000-0000-00%010x", get_uuid());
426  boost::uuids::uuid u1 = StringToUuid(std::string(str));
427 
429 
430  data->dscp_map_.insert(AgentQosIdForwardingClassPair(get_dscp(),
431  get_dscp_forwarding_class_id()));
432  data->vlan_priority_map_.insert(AgentQosIdForwardingClassPair(get_vlan_priority(),
433  get_vlan_priority_forwarding_class_id()));
434  data->mpls_exp_map_.insert(AgentQosIdForwardingClassPair(get_mpls_exp(),
435  get_mpls_exp_forwarding_class_id()));
436  data->name_ = get_name();
437 
438  if (get_type() == "vhost") {
440  } else if (get_type() == "fabric") {
442  } else {
444  }
445 
446 
448  DBRequest req;
450  req.key.reset(new AgentQosConfigKey(u1));
451  req.data.reset(data);
452  table->Enqueue(&req);
453  resp->set_resp("Success");
454  resp->Response();
455 }
456 
457 void DeleteQosConfig::HandleRequest() const {
458  QosResponse *resp = new QosResponse();
459  resp->set_context(context());
460 
461  char str[50];
462  sprintf(str, "00000000-0000-0000-0000-00%010x", get_uuid());
463  boost::uuids::uuid u1 = StringToUuid(std::string(str));
464 
466  DBRequest req;
468  req.key.reset(new AgentQosConfigKey(u1));
469  req.data.reset(NULL);
470  table->Enqueue(&req);
471  resp->set_resp("Success");
472  resp->Response();
473 }
static void CfgUuidSet(uint64_t ms_long, uint64_t ls_long, boost::uuids::uuid &u)
Definition: agent_cmn.h:67
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:21
IFMapAgentTable * cfg_global_qos_table() const
Definition: cfg_init.h:87
Agent * agent() const
Definition: agent_db.h:216
void set_agent(Agent *agent)
Definition: agent_db.h:215
virtual bool IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u)
Definition: qos_config.cc:350
virtual bool OperDBDelete(DBEntry *entry, const DBRequest *req)
Definition: qos_config.cc:321
void EraseFabricQosConfig(const boost::uuids::uuid &uuid)
Definition: qos_config.h:186
static DBTableBase * CreateTable(Agent *agent, DB *db, const std::string &name)
Definition: qos_config.cc:283
virtual bool OperDBResync(DBEntry *entry, const DBRequest *req)
Definition: qos_config.cc:317
static const uint32_t kInvalidIndex
Definition: qos_config.h:145
AgentQosConfigData * BuildData(IFMapNode *node)
Definition: qos_config.cc:372
virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
Definition: qos_config.cc:335
const boost::uuids::uuid GetActiveFabricQosConfig()
Definition: qos_config.h:194
virtual bool OperDBOnChange(DBEntry *entry, const DBRequest *req)
Definition: qos_config.cc:312
void ReleaseIndex(AgentQosConfig *qc)
Definition: qos_config.cc:328
void InsertVhostQosConfig(const boost::uuids::uuid &uuid)
Definition: qos_config.h:207
void InsertFabricQosConfig(const boost::uuids::uuid &uuid)
Definition: qos_config.h:190
AgentQosConfigNameMap name_map_
Definition: qos_config.h:220
AgentQosConfigTable(Agent *agent, DB *db, const std::string &name)
Definition: qos_config.cc:273
virtual bool ProcessConfig(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
Definition: qos_config.cc:358
void EraseVhostQosConfig(const boost::uuids::uuid &uuid)
Definition: qos_config.h:203
const boost::uuids::uuid GetActiveVhostQosConfig()
Definition: qos_config.h:211
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
Definition: qos_config.cc:407
std::pair< std::string, AgentQosConfig * > AgentQosConfigNamePair
Definition: qos_config.h:141
IndexVector< AgentQosConfig * > index_table_
Definition: qos_config.h:223
virtual DBEntry * OperDBAdd(const DBRequest *req)
Definition: qos_config.cc:297
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *k) const
Definition: qos_config.cc:290
virtual ~AgentQosConfigTable()
Definition: qos_config.cc:279
boost::uuids::uuid uuid() const
Definition: qos_config.h:67
bool HandleQosForwardingMapChange(const Agent *agent, QosIdForwardingClassMap &map, const AgentQosIdForwardingClassMap &data_map)
Definition: qos_config.cc:102
virtual void Delete(const DBRequest *req)
Definition: qos_config.cc:258
virtual void SetKey(const DBRequestKey *key)
Definition: qos_config.cc:267
void HandleVhostQosConfig(const Agent *agent, const AgentQosConfigData *data, bool deleted)
Definition: qos_config.cc:138
uint32_t id() const
Definition: qos_config.h:74
std::string name() const
Definition: qos_config.h:78
KeyPtr GetDBRequestKey() const
Definition: qos_config.cc:31
virtual bool IsLess(const DBEntry &rhs) const
Definition: qos_config.cc:97
uint32_t id_
Definition: qos_config.h:117
QosIdForwardingClassMap dscp_map_
Definition: qos_config.h:118
std::map< uint32_t, uint32_t > QosIdForwardingClassMap
Definition: qos_config.h:45
virtual bool Change(const DBRequest *req)
Definition: qos_config.cc:212
AgentQosConfig(const boost::uuids::uuid uuid)
Definition: qos_config.cc:21
std::string ToString() const
Definition: qos_config.cc:36
std::pair< uint32_t, uint32_t > QosIdForwardingClassPair
Definition: qos_config.h:44
virtual ~AgentQosConfig()
Definition: qos_config.cc:25
void HandleGlobalQosConfig(const AgentQosConfigData *data)
Definition: qos_config.cc:201
bool VerifyLinkToGlobalQosConfig(const Agent *agent, const AgentQosConfigData *data)
Definition: qos_config.cc:122
void set_id(uint32_t id)
Definition: qos_config.h:71
QosIdForwardingClassMap mpls_exp_map_
Definition: qos_config.h:120
virtual bool DBEntrySandesh(Sandesh *resp, std::string &name) const
Definition: qos_config.cc:42
std::string name_
Definition: qos_config.h:116
uint32_t default_forwarding_class_
Definition: qos_config.h:122
void HandleFabricQosConfig(const Agent *agent, const AgentQosConfigData *data, bool deleted)
Definition: qos_config.cc:169
boost::uuids::uuid uuid_
Definition: qos_config.h:115
QosIdForwardingClassMap vlan_priority_map_
Definition: qos_config.h:119
std::string GetString(const std::string &key) const
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
ConfigManager * config_manager() const
Definition: agent.cc:893
ResourceManager * resource_manager() const
Definition: agent.cc:1025
AgentQosConfigTable * qos_config_table() const
Definition: agent.h:557
AgentConfig * cfg() const
Definition: agent.cc:869
const std::string & vhost_interface_name() const
Definition: agent.cc:105
static Agent * GetInstance()
Definition: agent.h:439
const std::string & fabric_interface_name() const
Definition: agent.h:1132
void AddQosConfigNode(IFMapNode *node)
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
DBTableBase * get_table() const
Definition: db_entry.cc:120
bool IsDeleted() const
Definition: db_entry.h:49
void Delete()
Definition: db_entry.cc:132
adjacency_iterator end(DBGraph *graph)
adjacency_iterator begin(DBGraph *graph)
bool Enqueue(DBRequest *req)
Definition: db_table.cc:221
const std::string & name() const
Definition: db_table.h:111
Definition: db.h:25
const DBGraph * GetGraph() const
IFMapTable * table()
Definition: ifmap_node.h:30
const std::string & name() const
Definition: ifmap_node.h:49
IFMapObject * GetObject()
Definition: ifmap_node.cc:64
size_t InsertAtIndex(uint32_t index, EntryType entry)
Definition: index_vector.h:55
void Remove(size_t index)
Definition: index_vector.h:79
void Release(KeyPtr key)
boost::shared_ptr< ResourceKey > KeyPtr
std::map< uint32_t, uint32_t > AgentQosIdForwardingClassMap
Definition: qos_config.h:33
std::pair< uint32_t, uint32_t > AgentQosIdForwardingClassPair
Definition: qos_config.h:32
static boost::uuids::uuid StringToUuid(const std::string &str)
Definition: string_util.h:146
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
@ ADD_DEL_CHANGE
Definition: agent_db.h:101
@ RESYNC
Definition: agent_db.h:104
uint8_t sub_op_
Definition: agent_db.h:109
const Agent * agent() const
Definition: oper_db.h:66
IFMapNode * ifmap_node() const
Definition: oper_db.h:60
std::string name_
Definition: qos_config.h:130
AgentQosConfig::Type type_
Definition: qos_config.h:134
AgentQosIdForwardingClassMap mpls_exp_map_
Definition: qos_config.h:133
AgentQosIdForwardingClassMap vlan_priority_map_
Definition: qos_config.h:132
uint32_t default_forwarding_class_
Definition: qos_config.h:135
AgentQosIdForwardingClassMap dscp_map_
Definition: qos_config.h:131
boost::uuids::uuid uuid_
Definition: qos_config.h:29
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
boost::uuids::uuid uuid