OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
acl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef __AGENT_ACL_N_H__
6 #define __AGENT_ACL_N_H__
7 
8 #include <boost/intrusive/list.hpp>
9 #include <boost/uuid/uuid.hpp>
10 #include <boost/intrusive_ptr.hpp>
11 #include <tbb/atomic.h>
12 
13 #include <oper/oper_db.h>
14 #include <filter/traffic_action.h>
15 #include <filter/acl_entry_match.h>
16 #include <filter/acl_entry_spec.h>
17 #include <filter/acl_entry.h>
18 #include <filter/packet_header.h>
19 
20 struct FlowKey;
21 class VnEntry;
22 class Interface;
23 
25  std::string uuid;
26  bool drop;
27  bool terminal;
28  bool other;
29  std::string src_match_vn; // source VN that matched
30  std::string dst_match_vn; // destination VN that matched
31  std::string acl_name;
32  FlowPolicyInfo(const std::string &u);
33 };
34 
35 struct FlowAction {
37  action(0), mirror_l() {};
38  ~FlowAction() { };
39 
40  void Clear() {
41  action = 0;
42  mirror_l.clear();
44  };
45 
46  uint32_t action;
47  std::vector<MirrorActionSpec> mirror_l;
50 };
51 
55 
60 };
61 
62 struct AclKey : public AgentOperDBKey {
63  AclKey(const boost::uuids::uuid &id) : AgentOperDBKey(), uuid_(id) {} ;
64  virtual ~AclKey() {};
65 
67 };
68 
69 struct AclData: public AgentOperDBData {
70  AclData(Agent *agent, IFMapNode *node, AclSpec &aclspec) :
71  AgentOperDBData(agent, node), ace_id_to_del_(0), ace_add(false),
72  acl_spec_(aclspec) {
73  }
74  AclData(Agent *agent, IFMapNode *node, int ace_id_to_del) :
75  AgentOperDBData(agent, node), ace_id_to_del_(ace_id_to_del) {
76  }
77  virtual ~AclData() { }
78 
79  // Delete a particular ace
81  // true: add to existing aces, false:replace existing aces with specified in the spec
82  bool ace_add;
83  std::string cfg_name_;
85 };
86 
89  AgentOperDBData(agent, node) {}
90 };
91 
92 class AclDBEntry : AgentRefCount<AclDBEntry>, public AgentOperDBEntry {
93 public:
94  typedef boost::intrusive::member_hook<AclEntry,
95  boost::intrusive::list_member_hook<>,
97  typedef boost::intrusive::list<AclEntry, AclEntryNode> AclEntries;
98 
100  AgentOperDBEntry(), uuid_(id), dynamic_acl_(false) {
101  }
103  }
104 
105  bool IsLess(const DBEntry &rhs) const;
106  KeyPtr GetDBRequestKey() const;
107  void SetKey(const DBRequestKey *key);
108  std::string ToString() const;
109  uint32_t GetRefCount() const {
111  }
112  const boost::uuids::uuid &GetUuid() const {return uuid_;};
113  const std::string &GetName() const {return name_;};
114  void SetName(const std::string name) {name_ = name;};
115  bool DBEntrySandesh(Sandesh *resp, std::string &name) const;
116  void SetAclSandeshData(AclSandeshData &data) const;
117 
118  // ACL methods
119  //AclEntry *AddAclEntry(const AclEntrySpec &acl_entry_spec);
120  AclEntry *AddAclEntry(const AclEntrySpec &acl_entry_spec, AclEntries &entries);
121  bool DeleteAclEntry(const uint32_t acl_entry_id);
122  void DeleteAllAclEntries();
123  uint32_t Size() const {return acl_entries_.size();};
124  void SetAclEntries(AclEntries &entries);
125  void SetDynamicAcl(bool dyn) {dynamic_acl_ = dyn;};
126  bool GetDynamicAcl () const {return dynamic_acl_;};
127 
128  // Packet Match
129  bool PacketMatch(const PacketHeader &packet_header, MatchAclParams &m_acl,
130  FlowPolicyInfo *info) const;
131  bool Changed(const AclEntries &new_acl_entries) const;
132  uint32_t ace_count() const { return acl_entries_.size();}
133  bool IsRulePresent(const std::string &uuid) const;
134  bool ResyncQosConfigEntries();
135  bool IsQosConfigResolved();
136  bool Isresolved();
137  const AclEntry* GetAclEntryAtIndex(uint32_t) const;
138 private:
139  friend class AclTable;
142  std::string name_;
145 };
146 
147 class AclTable : public AgentOperDBTable {
148 public:
149  typedef std::map<std::string, TrafficAction::Action> TrafficActionMap;
150  typedef std::set<AclDBEntry*> UnResolvedAclEntries;
151 
152  // Packet module is optional. Callback function to update the flow stats
153  // for ACL. The callback is defined to avoid linking error
154  // when flow is not enabled
155  typedef boost::function<void(const AclDBEntry *acl, AclFlowCountResp &data,
156  const std::string &ace_id)> FlowAceSandeshDataFn;
157  typedef boost::function<void(const AclDBEntry *acl, AclFlowResp &data,
158  const int last_count)> FlowAclSandeshDataFn;
159 
160  AclTable(DB *db, const std::string &name) : AgentOperDBTable(db, name) { }
161  virtual ~AclTable() { }
162  void GetTables(DB *db) { };
163 
164  virtual std::unique_ptr<DBEntry> AllocEntry(const DBRequestKey *k) const;
165  virtual size_t Hash(const DBEntry *entry) const {return 0;};
166  virtual size_t Hash(const DBRequestKey *key) const {return 0;};
167 
168  virtual DBEntry *OperDBAdd(const DBRequest *req);
169  virtual bool OperDBOnChange(DBEntry *entry, const DBRequest *req);
170  virtual bool OperDBDelete(DBEntry *entry, const DBRequest *req);
171  virtual bool OperDBResync(DBEntry *entry, const DBRequest *req);
172 
173  virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req,
174  const boost::uuids::uuid &u);
176  const boost::uuids::uuid &u,
177  AclSpec &acl_spec);
178  void AclIFNodeToReq(IFMapNode *node, DBRequest &req,
179  const boost::uuids::uuid &u,
180  AclSpec &acl_spec);
181  virtual bool IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u);
183  const std::string &context);
184 
185  static DBTableBase *CreateTable(DB *db, const std::string &name);
186  TrafficAction::Action ConvertActionString(std::string action) const;
187  static void AclFlowResponse(const std::string acl_uuid_str,
188  const std::string ctx, const int last_count);
189  static void AclFlowCountResponse(const std::string acl_uuid_str,
190  const std::string ctx,
191  const std::string &ace_id);
194  void ListenerInit();
195  void Notify(DBTablePartBase *partition, DBEntryBase *e);
196  void AddUnresolvedEntry(AclDBEntry *entry);
197  void DeleteUnresolvedEntry(AclDBEntry *entry);
198 private:
199  bool SubnetTypeEqual(const autogen::SubnetType &lhs,
200  const autogen::SubnetType &rhs) const;
201  bool AddressTypeEqual(const autogen::AddressType &lhs,
202  const autogen::AddressType &rhs) const;
203  bool PortTypeEqual(const autogen::PortType &src,
204  const autogen::PortType &dst) const;
205  static const AclDBEntry* GetAclDBEntry(const std::string uuid_str,
206  const std::string ctx,
207  SandeshResponse *resp);
208  void AddImplicitRule(AclSpec &acl_spec, AclEntrySpec &ace_spec,
209  const autogen::FirewallRule *rule);
210  void PopulateServicePort(AclEntrySpec &ace_spec, IFMapNode *node);
212  void ActionInit();
219 };
220 
222 
223 #define ACL_TRACE(obj, ...)\
224 do {\
225  Acl##obj::TraceMsg(AclTraceBuf, __FILE__, __LINE__, ##__VA_ARGS__);\
226 } while (false)
227 
228 #endif
virtual bool IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u)
Definition: acl.cc:403
AclSpec acl_spec_
Definition: acl.h:84
~FlowAction()
Definition: acl.h:38
boost::intrusive_ptr< const AclDBEntry > AclDBEntryConstRef
Definition: agent.h:143
MatchAclParams()
Definition: acl.h:53
bool Changed(const AclEntries &new_acl_entries) const
Definition: acl.cc:902
void SetName(const std::string name)
Definition: acl.h:114
boost::function< void(const AclDBEntry *acl, AclFlowResp &data, const int last_count)> FlowAclSandeshDataFn
Definition: acl.h:158
virtual ~AclTable()
Definition: acl.h:161
bool drop
Definition: acl.h:26
uint32_t GetRefCount() const
Definition: agent_db.h:54
AclEntryIDList ace_id_list
Definition: acl.h:57
virtual bool OperDBOnChange(DBEntry *entry, const DBRequest *req)
Definition: acl.cc:127
virtual DBEntry * OperDBAdd(const DBRequest *req)
Definition: acl.cc:107
virtual bool OperDBDelete(DBEntry *entry, const DBRequest *req)
Definition: acl.cc:203
Definition: acl.h:147
void SetDynamicAcl(bool dyn)
Definition: acl.h:125
bool DBEntrySandesh(Sandesh *resp, std::string &name) const
Definition: acl.cc:68
void ActionInit()
Definition: acl.cc:211
std::set< AclDBEntry * > UnResolvedAclEntries
Definition: acl.h:150
AclData(Agent *agent, IFMapNode *node, AclSpec &aclspec)
Definition: acl.h:70
Definition: acl.h:62
std::map< std::string, TrafficAction::Action > TrafficActionMap
Definition: acl.h:149
Definition: acl.h:69
~AclDBEntry()
Definition: acl.h:102
void GetTables(DB *db)
Definition: acl.h:162
int ListenerId
Definition: db_table.h:62
static DBTableBase * CreateTable(DB *db, const std::string &name)
Definition: acl.cc:274
bool ResyncQosConfigEntries()
Definition: acl.cc:712
void FirewallPolicyIFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u, AclSpec &acl_spec)
Definition: acl.cc:506
std::vector< AclEntryID > AclEntryIDList
Definition: acl_entry.h:85
bool ace_add
Definition: acl.h:82
uint32_t Size() const
Definition: acl.h:123
boost::uuids::uuid uuid
uint32_t action
Definition: acl.h:44
void ListenerInit()
Definition: acl.cc:268
const boost::uuids::uuid & GetUuid() const
Definition: acl.h:112
AclResyncQosConfigData(Agent *agent, IFMapNode *node)
Definition: acl.h:88
virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
Definition: acl.cc:650
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:18
bool terminal
Definition: acl.h:27
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
SandeshTraceBufferPtr AclTraceBuf
QosConfigActionSpec qos_config_action_
Definition: acl.h:49
std::string src_match_vn
Definition: acl.h:29
bool dynamic_acl_
Definition: acl.h:141
AclTable(DB *db, const std::string &name)
Definition: acl.h:160
bool SubnetTypeEqual(const autogen::SubnetType &lhs, const autogen::SubnetType &rhs) const
Definition: acl.cc:565
virtual ~AclKey()
Definition: acl.h:64
std::vector< MirrorActionSpec > mirror_l
Definition: acl.h:47
uint32_t ace_count() const
Definition: acl.h:132
Definition: db.h:24
std::string dst_match_vn
Definition: acl.h:30
void set_acl_flow_sandesh_data_cb(FlowAclSandeshDataFn fn)
Definition: acl.cc:1580
KeyPtr GetDBRequestKey() const
Definition: acl.cc:58
bool IsQosConfigResolved()
Definition: acl.cc:699
int ace_id_to_del_
Definition: acl.h:80
const std::string & GetName() const
Definition: acl.h:113
virtual bool OperDBResync(DBEntry *entry, const DBRequest *req)
Definition: acl.cc:199
bool IsRulePresent(const std::string &uuid) const
Definition: acl.cc:937
void DeleteAllAclEntries()
Definition: acl.cc:786
Definition: agent.h:358
void Notify(DBTablePartBase *partition, DBEntryBase *e)
Definition: acl.cc:236
VrfTranslateActionSpec vrf_translate_action_
Definition: acl.h:48
AclEntry * AddAclEntry(const AclEntrySpec &acl_entry_spec, AclEntries &entries)
Definition: acl.cc:726
static void AclFlowCountResponse(const std::string acl_uuid_str, const std::string ctx, const std::string &ace_id)
Definition: acl.cc:965
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *k) const
Definition: acl.cc:101
void PopulateServicePort(AclEntrySpec &ace_spec, IFMapNode *node)
Definition: acl.cc:454
bool AddressTypeEqual(const autogen::AddressType &lhs, const autogen::AddressType &rhs) const
Definition: acl.cc:574
std::string name_
Definition: acl.h:142
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
Definition: acl.cc:986
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:18
bool PortTypeEqual(const autogen::PortType &src, const autogen::PortType &dst) const
Definition: acl.cc:598
const std::string & name() const
Definition: db_table.h:110
AclData(Agent *agent, IFMapNode *node, int ace_id_to_del)
Definition: acl.h:74
FlowAction()
Definition: acl.h:36
virtual size_t Hash(const DBRequestKey *key) const
Definition: acl.h:166
TrafficActionMap ta_map_
Definition: acl.h:215
boost::uuids::uuid uuid_
Definition: acl.h:64
Definition: vn.h:151
AclDBEntryConstRef acl
Definition: acl.h:54
bool DeleteAclEntry(const uint32_t acl_entry_id)
Definition: acl.cc:768
std::string uuid
Definition: acl.h:25
DISALLOW_COPY_AND_ASSIGN(AclDBEntry)
const AclEntry * GetAclEntryAtIndex(uint32_t) const
Definition: acl.cc:888
virtual ~AclData()
Definition: acl.h:77
FlowAclSandeshDataFn flow_acl_sandesh_data_cb_
Definition: acl.h:217
bool GetDynamicAcl() const
Definition: acl.h:126
IFMapNode * GetFirewallRule(IFMapNode *node)
Definition: acl.cc:474
bool IsLess(const DBEntry &rhs) const
Definition: acl.cc:47
bool PacketMatch(const PacketHeader &packet_header, MatchAclParams &m_acl, FlowPolicyInfo *info) const
Definition: acl.cc:798
uint32_t GetRefCount() const
Definition: acl.h:109
const Agent * agent() const
Definition: oper_db.h:65
void AddImplicitRule(AclSpec &acl_spec, AclEntrySpec &ace_spec, const autogen::FirewallRule *rule)
Definition: acl.cc:495
virtual size_t Hash(const DBEntry *entry) const
Definition: acl.h:165
TrafficAction::Action ConvertActionString(std::string action) const
Definition: acl.cc:218
void DeleteUnresolvedEntry(AclDBEntry *entry)
Definition: acl.cc:232
AclKey(const boost::uuids::uuid &id)
Definition: acl.h:63
bool other
Definition: acl.h:28
AclEntries acl_entries_
Definition: acl.h:143
~MatchAclParams()
Definition: acl.h:54
bool Isresolved()
std::string acl_name
Definition: acl.h:31
static void AclFlowResponse(const std::string acl_uuid_str, const std::string ctx, const int last_count)
Definition: acl.cc:949
DBTableBase::ListenerId qos_config_listener_id_
Definition: acl.h:213
FlowAction action_info
Definition: acl.h:58
boost::intrusive::list_member_hook acl_list_node
Definition: acl_entry.h:123
UnResolvedAclEntries unresolved_acl_entries_
Definition: acl.h:214
Definition: acl.h:35
void Clear()
Definition: acl.h:40
void SetAclEntries(AclEntries &entries)
Definition: acl.cc:687
AclDBEntry(const boost::uuids::uuid &id)
Definition: acl.h:99
std::string ToString() const
Definition: acl.cc:52
boost::function< void(const AclDBEntry *acl, AclFlowCountResp &data, const std::string &ace_id)> FlowAceSandeshDataFn
Definition: acl.h:156
void AclIFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u, AclSpec &acl_spec)
Definition: acl.cc:607
bool terminal_rule
Definition: acl.h:59
boost::intrusive::list< AclEntry, AclEntryNode > AclEntries
Definition: acl.h:97
void SetAclSandeshData(AclSandeshData &data) const
Definition: acl.cc:89
DISALLOW_COPY_AND_ASSIGN(AclTable)
boost::intrusive::member_hook< AclEntry, boost::intrusive::list_member_hook<>,&AclEntry::acl_list_node > AclEntryNode
Definition: acl.h:96
void SetKey(const DBRequestKey *key)
Definition: acl.cc:63
FlowPolicyInfo(const std::string &u)
Definition: acl.cc:42
std::string cfg_name_
Definition: acl.h:83
Definition: acl.h:92
void AddUnresolvedEntry(AclDBEntry *entry)
Definition: acl.cc:228
boost::uuids::uuid uuid_
Definition: acl.h:140
void set_ace_flow_sandesh_data_cb(FlowAceSandeshDataFn fn)
Definition: acl.cc:1576
FlowAceSandeshDataFn flow_ace_sandesh_data_cb_
Definition: acl.h:216
static const AclDBEntry * GetAclDBEntry(const std::string uuid_str, const std::string ctx, SandeshResponse *resp)
Definition: acl.cc:921