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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "vom/acl_l3_list.hpp"
17 #include "vom/acl_list_cmds.hpp"
18 #include "vom/api_types.hpp"
19 #include "vom/logger.hpp"
20 #include "vom/singular_db_funcs.hpp"
26 * Definition of the static singular_db for ACL Lists
28 singular_db<l3_list::key_t, l3_list> l3_list::m_db;
31 * Definition of the static per-handle DB for ACL Lists
33 std::map<handle_t, std::weak_ptr<l3_list>> l3_list::m_hdl_db;
35 l3_list::event_handler l3_list::m_evh;
37 l3_list::event_handler::event_handler()
39 OM::register_listener(this);
40 inspect::register_handler({ "l3-acl-list" }, "L3 ACL lists", this);
43 l3_list::l3_list(const key_t& key)
44 : m_hdl(handle_t::INVALID)
48 l3_list::l3_list(const handle_t& hdl, const key_t& key)
53 l3_list::l3_list(const key_t& key, const rules_t& rules)
54 : m_hdl(handle_t::INVALID)
59 l3_list::l3_list(const l3_list& o)
68 m_db.release(m_key, this);
71 std::shared_ptr<l3_list>
72 l3_list::singular() const
74 return find_or_add(*this);
78 * Dump all ACLs into the stream provided
81 l3_list::dump(std::ostream& os)
87 * convert to string format for debug purposes
90 l3_list::to_string() const
93 s << "acl-list:[" << m_key << " " << m_hdl.to_string() << " rules:[";
95 for (auto rule : m_rules) {
96 s << rule.to_string() << " ";
105 l3_list::insert(const l3_rule& rule)
107 m_rules.insert(rule);
111 l3_list::remove(const l3_rule& rule)
117 l3_list::handle() const
119 return (singular()->handle_i());
122 std::shared_ptr<l3_list>
123 l3_list::find(const handle_t& handle)
125 return (m_hdl_db[handle].lock());
128 std::shared_ptr<l3_list>
129 l3_list::find(const key_t& key)
131 return (m_db.find(key));
134 std::shared_ptr<l3_list>
135 l3_list::find_or_add(const l3_list& temp)
137 return (m_db.find_or_add(temp.key(), temp));
141 l3_list::handle_i() const
143 return (m_hdl.data());
147 l3_list::add(const key_t& key, const HW::item<handle_t>& item)
149 std::shared_ptr<l3_list> sp = find(key);
152 m_hdl_db[item.data()] = sp;
157 l3_list::remove(const HW::item<handle_t>& item)
159 m_hdl_db.erase(item.data());
162 const l3_list::key_t&
168 const l3_list::rules_t&
169 l3_list::rules() const
175 l3_list::operator==(const l3_list& l) const
177 return (key() == l.key() && rules() == l.rules());
181 l3_list::event_handler::handle_populate(const client_db::key_t& key)
184 * dump L3 ACLs Bridge domains
186 std::shared_ptr<list_cmds::l3_dump_cmd> cmd =
187 std::make_shared<list_cmds::l3_dump_cmd>();
192 for (auto& record : *cmd) {
193 auto& payload = record.get_payload();
195 const handle_t hdl(payload.acl_index);
196 l3_list acl(hdl, std::string(reinterpret_cast<const char*>(payload.tag)));
198 for (unsigned int ii = 0; ii < payload.count; ii++) {
199 const route::prefix_t src = from_api(payload.r[ii].src_prefix);
200 const route::prefix_t dst = from_api(payload.r[ii].dst_prefix);
201 l3_rule rule(ii, action_t::from_int(payload.r[ii].is_permit), src, dst);
203 rule.set_proto(payload.r[ii].proto);
204 rule.set_src_from_port(payload.r[ii].srcport_or_icmptype_first);
205 rule.set_src_to_port(payload.r[ii].srcport_or_icmptype_last);
206 rule.set_dst_from_port(payload.r[ii].dstport_or_icmpcode_first);
207 rule.set_dst_to_port(payload.r[ii].dstport_or_icmpcode_last);
208 rule.set_tcp_flags_mask(payload.r[ii].tcp_flags_mask);
209 rule.set_tcp_flags_value(payload.r[ii].tcp_flags_value);
213 VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
216 * Write each of the discovered ACLs into the OM,
217 * but disable the HW Command q whilst we do, so that no
218 * commands are sent to VPP
220 OM::commit(key, acl);
225 l3_list::event_handler::show(std::ostream& os)
231 l3_list::event_handler::order() const
233 return (dependency_t::ACL);
237 l3_list::event_handler::handle_replay()
243 l3_list::update(const l3_list& obj)
246 * always update the instance with the latest rule set
248 if (rc_t::OK != m_hdl.rc() || obj.m_rules != m_rules) {
249 HW::enqueue(new list_cmds::l3_update_cmd(m_hdl, m_key, m_rules));
252 * We don't, can't, read the priority from VPP,
253 * so the is equals check above does not include the priorty.
254 * but we save it now.
256 m_rules = obj.m_rules;
260 * Sweep/reap the object if still stale
266 HW::enqueue(new list_cmds::l3_delete_cmd(m_hdl));
272 * Replay the objects state to HW
275 l3_list::replay(void)
278 m_hdl.data().reset();
279 HW::enqueue(new list_cmds::l3_update_cmd(m_hdl, m_key, m_rules));
287 * fd.io coding-style-patch-verification: ON
290 * eval: (c-set-style "mozilla")