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