FD.io VPP  v18.01-8-g0eacf49
Vector Packet Processing
acl_list.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "vom/acl_list.hpp"
17 #include "vom/acl_list_cmds.hpp"
18 #include "vom/logger.hpp"
19 
20 namespace VOM {
21 namespace ACL {
22 template <>
23 void
24 l2_list::event_handler::handle_populate(const client_db::key_t& key)
25 {
26  /* hack to get this function instantiated */
27  m_evh.order();
28 
29  /*
30  * dump VPP Bridge domains
31  */
32  std::shared_ptr<list_cmds::l2_dump_cmd> cmd =
33  std::make_shared<list_cmds::l2_dump_cmd>();
34 
35  HW::enqueue(cmd);
36  HW::write();
37 
38  for (auto& record : *cmd) {
39  auto& payload = record.get_payload();
40 
41  const handle_t hdl(payload.acl_index);
42  l2_list acl(hdl, std::string(reinterpret_cast<const char*>(payload.tag)));
43 
44  for (unsigned int ii = 0; ii < payload.count; ii++) {
45  const route::prefix_t pfx(payload.r[ii].is_ipv6,
46  payload.r[ii].src_ip_addr,
47  payload.r[ii].src_ip_prefix_len);
48  l2_rule rule(ii, action_t::from_int(payload.r[ii].is_permit), pfx,
49  { payload.r[ii].src_mac }, { payload.r[ii].src_mac_mask });
50 
51  acl.insert(rule);
52  }
53  VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
54 
55  /*
56  * Write each of the discovered ACLs into the OM,
57  * but disable the HW Command q whilst we do, so that no
58  * commands are sent to VPP
59  */
60  OM::commit(key, acl);
61  }
62 }
63 
64 template <>
65 void
66 l3_list::event_handler::handle_populate(const client_db::key_t& key)
67 {
68  /* hack to get this function instantiated */
69  m_evh.order();
70 
71  /*
72  * dump L3 ACLs Bridge domains
73  */
74  std::shared_ptr<list_cmds::l3_dump_cmd> cmd =
75  std::make_shared<list_cmds::l3_dump_cmd>();
76 
77  HW::enqueue(cmd);
78  HW::write();
79 
80  for (auto& record : *cmd) {
81  auto& payload = record.get_payload();
82 
83  const handle_t hdl(payload.acl_index);
84  l3_list acl(hdl, std::string(reinterpret_cast<const char*>(payload.tag)));
85 
86  for (unsigned int ii = 0; ii < payload.count; ii++) {
87  const route::prefix_t src(payload.r[ii].is_ipv6,
88  payload.r[ii].src_ip_addr,
89  payload.r[ii].src_ip_prefix_len);
90  const route::prefix_t dst(payload.r[ii].is_ipv6,
91  payload.r[ii].dst_ip_addr,
92  payload.r[ii].dst_ip_prefix_len);
93  l3_rule rule(ii, action_t::from_int(payload.r[ii].is_permit), src, dst);
94 
95  acl.insert(rule);
96  }
97  VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
98 
99  /*
100  * Write each of the discovered ACLs into the OM,
101  * but disable the HW Command q whilst we do, so that no
102  * commands are sent to VPP
103  */
104  OM::commit(key, acl);
105  }
106 }
107 
108 template <>
109 void
110 l3_list::update(const l3_list& obj)
111 {
112  /*
113  * always update the instance with the latest rule set
114  */
115  if (!m_hdl || obj.m_rules != m_rules) {
116  HW::enqueue(new list_cmds::l3_update_cmd(m_hdl, m_key, m_rules));
117  }
118  /*
119  * We don't, can't, read the priority from VPP,
120  * so the is equals check above does not include the priorty.
121  * but we save it now.
122  */
123  m_rules = obj.m_rules;
124 }
125 template <>
126 void
127 l2_list::update(const l2_list& obj)
128 {
129  /*
130  * always update the instance with the latest rule set
131  */
132  if (!m_hdl || obj.m_rules != m_rules) {
133  HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
134  }
135  /*
136  * We don't, can't, read the priority from VPP,
137  * so the is equals check above does not include the priorty.
138  * but we save it now.
139  */
140  m_rules = obj.m_rules;
141 }
142 /**
143  * Sweep/reap the object if still stale
144  */
145 template <>
146 void
147 l3_list::sweep(void)
148 {
149  if (m_hdl) {
151  }
152  HW::write();
153 }
154 template <>
155 void
156 l2_list::sweep(void)
157 {
158  if (m_hdl) {
160  }
161  HW::write();
162 }
163 
164 /**
165  * Replay the objects state to HW
166  */
167 template <>
168 void
169 l3_list::replay(void)
170 {
171  if (m_hdl) {
172  HW::enqueue(new list_cmds::l3_update_cmd(m_hdl, m_key, m_rules));
173  }
174 }
175 template <>
176 void
177 l2_list::replay(void)
178 {
179  if (m_hdl) {
180  HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
181  }
182 }
183 
184 }; // namespace ACL
185 }; // namespace VOM
186 
187 /*
188  * fd.io coding-style-patch-verification: ON
189  *
190  * Local Variables:
191  * eval: (c-set-style "mozilla")
192  * End:
193  */
static const action_t & from_int(uint8_t i)
Get the enum type from a VPP integer value.
Definition: acl_types.cpp:30
update_cmd< l3_rule, vapi::Acl_add_replace > l3_update_cmd
Typedef the L3 ACL commands.
#define VOM_LOG(lvl)
Definition: logger.hpp:181
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
delete_cmd< vapi::Macip_acl_del > l2_delete_cmd
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:225
static const log_level_t DEBUG
Definition: logger.hpp:32
delete_cmd< vapi::Acl_del > l3_delete_cmd
static rc_t commit(const client_db::key_t &key, const OBJ &obj)
Make the State in VPP reflect the expressed desired state.
Definition: om.hpp:202
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:189
list< l3_rule > l3_list
Typedef the L3 ACL type.
Definition: acl_list.hpp:265
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
update_cmd< l2_rule, vapi::Macip_acl_add > l2_update_cmd
Typedef the L2 ACL commands.
list< l2_rule > l2_list
Typedef the L2 ACL type.
Definition: acl_list.hpp:270