Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include <algorithm> 7 : #include <boost/uuid/uuid_io.hpp> 8 : #include <base/parse_object.h> 9 : #include <ifmap/ifmap_link.h> 10 : #include <ifmap/ifmap_table.h> 11 : #include <vnc_cfg_types.h> 12 : 13 : #include <cmn/agent_cmn.h> 14 : #include <cfg/cfg_init.h> 15 : #include <oper/sg.h> 16 : #include <filter/acl.h> 17 : 18 : #include <oper/interface_common.h> 19 : #include <oper/mirror_table.h> 20 : #include <oper/agent_sandesh.h> 21 : #include <oper/config_manager.h> 22 : 23 : using namespace autogen; 24 : using namespace std; 25 : 26 : SgTable *SgTable::sg_table_; 27 : 28 0 : bool SgEntry::IsLess(const DBEntry &rhs) const { 29 0 : const SgEntry &a = static_cast<const SgEntry &>(rhs); 30 0 : return (sg_uuid_ < a.sg_uuid_); 31 : } 32 : 33 0 : string SgEntry::ToString() const { 34 0 : std::stringstream uuidstring; 35 0 : uuidstring << sg_uuid_; 36 0 : return uuidstring.str(); 37 0 : } 38 : 39 0 : DBEntryBase::KeyPtr SgEntry::GetDBRequestKey() const { 40 0 : SgKey *key = new SgKey(sg_uuid_); 41 0 : return DBEntryBase::KeyPtr(key); 42 : } 43 : 44 0 : void SgEntry::SetKey(const DBRequestKey *key) { 45 0 : const SgKey *k = static_cast<const SgKey *>(key); 46 0 : sg_uuid_ = k->sg_uuid_; 47 0 : } 48 : 49 0 : std::unique_ptr<DBEntry> SgTable::AllocEntry(const DBRequestKey *k) const { 50 0 : const SgKey *key = static_cast<const SgKey *>(k); 51 0 : SgEntry *sg = new SgEntry(key->sg_uuid_); 52 0 : return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(sg)); 53 : } 54 : 55 0 : DBEntry *SgTable::OperDBAdd(const DBRequest *req) { 56 0 : SgKey *key = static_cast<SgKey *>(req->key.get()); 57 0 : SgEntry *sg = new SgEntry(key->sg_uuid_); 58 0 : ChangeHandler(sg, req); 59 0 : sg->SendObjectLog(GetOperDBTraceBuf(), AgentLogEvent::ADD); 60 0 : return sg; 61 : } 62 : 63 0 : bool SgTable::OperDBOnChange(DBEntry *entry, const DBRequest *req) { 64 0 : bool ret = ChangeHandler(entry, req); 65 0 : SgEntry *sg = static_cast<SgEntry *>(entry); 66 0 : sg->SendObjectLog(GetOperDBTraceBuf(), AgentLogEvent::CHANGE); 67 0 : return ret; 68 : } 69 : 70 0 : bool SgTable::ChangeHandler(DBEntry *entry, const DBRequest *req) { 71 0 : bool ret = false; 72 0 : SgEntry *sg = static_cast<SgEntry *>(entry); 73 0 : SgData *data = static_cast<SgData *>(req->data.get()); 74 : 75 0 : if (sg->sg_id_ != data->sg_id_) { 76 0 : sg->sg_id_ = data->sg_id_; 77 0 : ret = true; 78 : } 79 : 80 0 : AclKey key(data->egress_acl_id_); 81 0 : AclDBEntry *acl = static_cast<AclDBEntry *>(agent()->acl_table()->FindActiveEntry(&key)); 82 0 : if (sg->egress_acl_ != acl) { 83 0 : sg->egress_acl_ = acl; 84 0 : ret = true; 85 : } 86 0 : key = AclKey(data->ingress_acl_id_); 87 0 : acl = static_cast<AclDBEntry *>(agent()->acl_table()->FindActiveEntry(&key)); 88 0 : if (sg->ingress_acl_ != acl) { 89 0 : sg->ingress_acl_ = acl; 90 0 : ret = true; 91 : } 92 0 : return ret; 93 0 : } 94 : 95 0 : bool SgTable::OperDBDelete(DBEntry *entry, const DBRequest *req) { 96 0 : SgEntry *sg = static_cast<SgEntry *>(entry); 97 0 : sg->SendObjectLog(GetOperDBTraceBuf(), AgentLogEvent::DEL); 98 0 : return true; 99 : } 100 : 101 0 : DBTableBase *SgTable::CreateTable(DB *db, const std::string &name) { 102 0 : sg_table_ = new SgTable(db, name); 103 0 : sg_table_->Init(); 104 0 : return sg_table_; 105 : }; 106 : 107 0 : bool SgTable::IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u) { 108 0 : SecurityGroup *cfg = static_cast<SecurityGroup *>(node->GetObject()); 109 0 : assert(cfg); 110 0 : autogen::IdPermsType id_perms = cfg->id_perms(); 111 0 : CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u); 112 0 : return true; 113 0 : } 114 : 115 0 : bool SgTable::IFNodeToReq(IFMapNode *node, DBRequest &req, 116 : const boost::uuids::uuid &u) { 117 0 : SecurityGroup *cfg = static_cast<SecurityGroup *>(node->GetObject()); 118 0 : assert(cfg); 119 : 120 0 : assert(!u.is_nil()); 121 : 122 0 : if ((req.oper == DBRequest::DB_ENTRY_DELETE) || node->IsDeleted()) { 123 0 : req.oper = DBRequest::DB_ENTRY_DELETE; 124 0 : req.key.reset(new SgKey(u)); 125 0 : agent()->sg_table()->Enqueue(&req); 126 0 : return false; 127 : } 128 : 129 0 : agent()->config_manager()->AddSgNode(node); 130 0 : return false; 131 : } 132 : 133 0 : bool SgTable::ProcessConfig(IFMapNode *node, DBRequest &req, 134 : const boost::uuids::uuid &u) { 135 : 136 0 : if (node->IsDeleted()) 137 0 : return false; 138 : 139 0 : SecurityGroup *cfg = static_cast<SecurityGroup *>(node->GetObject()); 140 0 : assert(cfg); 141 : 142 0 : req.oper = DBRequest::DB_ENTRY_ADD_CHANGE; 143 0 : uint32_t sg_id = cfg->id(); 144 0 : if (sg_id == SgTable::kInvalidSgId) { 145 0 : OPER_TRACE(Sg, "Ignore SG id 0", UuidToString(u)); 146 0 : return false; 147 : } 148 : 149 0 : SgKey *key = new SgKey(u); 150 0 : SgData *data = NULL; 151 : 152 0 : boost::uuids::uuid egress_acl_uuid = boost::uuids::nil_uuid(); 153 0 : boost::uuids::uuid ingress_acl_uuid = boost::uuids::nil_uuid(); 154 0 : IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table()); 155 0 : for (DBGraphVertex::adjacency_iterator iter = 156 0 : node->begin(table->GetGraph()); 157 0 : iter != node->end(table->GetGraph()); ++iter) { 158 0 : IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->()); 159 0 : if (agent()->config_manager()->SkipNode(adj_node)) { 160 0 : continue; 161 : } 162 : 163 0 : if (adj_node->table() == agent()->cfg()->cfg_acl_table()) { 164 : AccessControlList *acl_cfg = static_cast<AccessControlList *> 165 0 : (adj_node->GetObject()); 166 0 : assert(acl_cfg); 167 0 : autogen::IdPermsType id_perms = acl_cfg->id_perms(); 168 0 : if (adj_node->name().find("egress-access-control-list") != std::string::npos) { 169 0 : CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, 170 : egress_acl_uuid); 171 : } 172 0 : if (adj_node->name().find("ingress-access-control-list") != std::string::npos) { 173 0 : CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, 174 : ingress_acl_uuid); 175 : } 176 0 : } 177 : } 178 0 : data = new SgData(agent(), node, sg_id, egress_acl_uuid, ingress_acl_uuid); 179 0 : req.key.reset(key); 180 0 : req.data.reset(data); 181 0 : agent()->sg_table()->Enqueue(&req); 182 0 : return false; 183 : } 184 : 185 0 : bool SgEntry::DBEntrySandesh(Sandesh *sresp, std::string &name) const { 186 0 : SgListResp *resp = static_cast<SgListResp *>(sresp); 187 0 : std::string str_uuid = UuidToString(GetSgUuid()); 188 0 : if (name.empty() || 189 0 : (str_uuid == name) || 190 0 : (integerToString(GetSgId()) == name)) { 191 0 : SgSandeshData data; 192 0 : data.set_ref_count(GetRefCount()); 193 0 : data.set_sg_uuid(str_uuid); 194 0 : data.set_sg_id(GetSgId()); 195 0 : if (GetEgressAcl()) { 196 0 : data.set_egress_acl_uuid(UuidToString(GetEgressAcl()->GetUuid())); 197 : } 198 0 : if (GetIngressAcl()) { 199 0 : data.set_ingress_acl_uuid(UuidToString(GetIngressAcl()->GetUuid())); 200 : } 201 : std::vector<SgSandeshData> &list = 202 0 : const_cast<std::vector<SgSandeshData>&>(resp->get_sg_list()); 203 0 : list.push_back(data); 204 0 : return true; 205 0 : } 206 0 : return false; 207 0 : } 208 : 209 0 : void SgEntry::SendObjectLog(SandeshTraceBufferPtr buf, 210 : AgentLogEvent::type event) const { 211 0 : SgObjectLogInfo info; 212 : 213 0 : string str; 214 0 : switch(event) { 215 0 : case AgentLogEvent::ADD: 216 0 : str.assign("Addition"); 217 0 : break; 218 0 : case AgentLogEvent::DEL: 219 0 : str.assign("Deletion"); 220 0 : break; 221 0 : case AgentLogEvent::CHANGE: 222 0 : str.assign("Modification"); 223 0 : break; 224 0 : default: 225 0 : str.assign(""); 226 0 : break; 227 : } 228 0 : info.set_event(str); 229 : 230 0 : string sg_uuid = UuidToString(GetSgUuid()); 231 0 : info.set_uuid(sg_uuid); 232 0 : info.set_id(GetSgId()); 233 0 : if (GetEgressAcl()) { 234 0 : info.set_egress_acl_uuid(UuidToString(GetEgressAcl()->GetUuid())); 235 : } 236 0 : if (GetIngressAcl()) { 237 0 : info.set_ingress_acl_uuid(UuidToString(GetIngressAcl()->GetUuid())); 238 : } 239 0 : info.set_ref_count(GetRefCount()); 240 0 : SG_OBJECT_LOG_LOG("AgentSg", SandeshLevel::SYS_INFO, info); 241 0 : SG_OBJECT_TRACE_TRACE(buf, info); 242 0 : } 243 : 244 0 : void SgListReq::HandleRequest() const { 245 0 : AgentSandeshPtr sand(new AgentSgSandesh(context(), get_name())); 246 0 : sand->DoSandesh(sand); 247 0 : } 248 : 249 0 : AgentSandeshPtr SgTable::GetAgentSandesh(const AgentSandeshArguments *args, 250 : const std::string &context) { 251 : return AgentSandeshPtr(new AgentSgSandesh(context, 252 0 : args->GetString("name"))); 253 : }