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_l2_list.hpp"
17 #include "vom/acl_list_cmds.hpp"
18 #include "vom/logger.hpp"
19 #include "vom/singular_db_funcs.hpp"
25 * Definition of the static singular_db for ACL Lists
27 singular_db<l2_list::key_t, l2_list> l2_list::m_db;
30 * Definition of the static per-handle DB for ACL Lists
32 std::map<handle_t, std::weak_ptr<l2_list>> l2_list::m_hdl_db;
34 l2_list::event_handler l2_list::m_evh;
36 l2_list::event_handler::event_handler()
38 OM::register_listener(this);
39 inspect::register_handler({ "l2-acl-list" }, "L2 ACL lists", this);
42 l2_list::l2_list(const key_t& key)
43 : m_hdl(handle_t::INVALID)
47 l2_list::l2_list(const handle_t& hdl, const key_t& key)
52 l2_list::l2_list(const key_t& key, const rules_t& rules)
53 : m_hdl(handle_t::INVALID)
58 l2_list::l2_list(const l2_list& o)
67 m_db.release(m_key, this);
70 std::shared_ptr<l2_list>
71 l2_list::singular() const
73 return find_or_add(*this);
77 * Dump all ACLs into the stream provided
80 l2_list::dump(std::ostream& os)
86 * convert to string format for debug purposes
89 l2_list::to_string() const
92 s << "acl-list:[" << m_key << " " << m_hdl.to_string() << " rules:[";
94 for (auto rule : m_rules) {
95 s << rule.to_string() << " ";
104 l2_list::insert(const l2_rule& rule)
106 m_rules.insert(rule);
110 l2_list::remove(const l2_rule& rule)
116 l2_list::handle() const
118 return (singular()->handle_i());
121 std::shared_ptr<l2_list>
122 l2_list::find(const handle_t& handle)
124 return (m_hdl_db[handle].lock());
127 std::shared_ptr<l2_list>
128 l2_list::find(const key_t& key)
130 return (m_db.find(key));
133 std::shared_ptr<l2_list>
134 l2_list::find_or_add(const l2_list& temp)
136 return (m_db.find_or_add(temp.key(), temp));
140 l2_list::handle_i() const
142 return (m_hdl.data());
146 l2_list::add(const key_t& key, const HW::item<handle_t>& item)
148 std::shared_ptr<l2_list> sp = find(key);
151 m_hdl_db[item.data()] = sp;
156 l2_list::remove(const HW::item<handle_t>& item)
158 m_hdl_db.erase(item.data());
161 const l2_list::key_t&
167 const l2_list::rules_t&
168 l2_list::rules() const
174 l2_list::operator==(const l2_list& l) const
176 return (key() == l.key() && rules() == l.rules());
180 l2_list::event_handler::handle_populate(const client_db::key_t& key)
185 std::shared_ptr<list_cmds::l2_dump_cmd> cmd =
186 std::make_shared<list_cmds::l2_dump_cmd>();
191 for (auto& record : *cmd) {
192 auto& payload = record.get_payload();
194 const handle_t hdl(payload.acl_index);
195 l2_list acl(hdl, std::string(reinterpret_cast<const char*>(payload.tag)));
197 for (unsigned int ii = 0; ii < payload.count; ii++) {
198 const route::prefix_t pfx(payload.r[ii].is_ipv6,
199 payload.r[ii].src_ip_addr,
200 payload.r[ii].src_ip_prefix_len);
202 action_t::from_int(payload.r[ii].is_permit),
204 { payload.r[ii].src_mac },
205 { payload.r[ii].src_mac_mask });
209 VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
212 * Write each of the discovered ACLs into the OM,
213 * but disable the HW Command q whilst we do, so that no
214 * commands are sent to VPP
216 OM::commit(key, acl);
221 l2_list::event_handler::show(std::ostream& os)
227 l2_list::event_handler::order() const
229 return (dependency_t::ACL);
233 l2_list::event_handler::handle_replay()
239 l2_list::update(const l2_list& obj)
242 * always update the instance with the latest rule set
244 if (rc_t::OK != m_hdl.rc() || obj.m_rules != m_rules) {
245 HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
248 * We don't, can't, read the priority from VPP,
249 * so the is equals check above does not include the priorty.
250 * but we save it now.
252 m_rules = obj.m_rules;
259 HW::enqueue(new list_cmds::l2_delete_cmd(m_hdl));
265 l2_list::replay(void)
268 m_hdl.data().reset();
269 HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
277 * fd.io coding-style-patch-verification: ON
280 * eval: (c-set-style "mozilla")