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/gbp_contract.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_contract_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
23 singular_db<gbp_contract::key_t, gbp_contract> gbp_contract::m_db;
25 gbp_contract::event_handler gbp_contract::m_evh;
27 gbp_contract::gbp_contract(scope_t scope,
30 const ACL::l3_list& acl,
31 const gbp_rules_t& rules,
32 const ethertype_set_t& allowed_ethertypes)
37 , m_acl(acl.singular())
39 , m_allowed_ethertypes(allowed_ethertypes)
43 gbp_contract::gbp_contract(const gbp_contract& gbpc)
45 , m_scope(gbpc.m_scope)
46 , m_sclass(gbpc.m_sclass)
47 , m_dclass(gbpc.m_dclass)
49 , m_gbp_rules(gbpc.m_gbp_rules)
50 , m_allowed_ethertypes(gbpc.m_allowed_ethertypes)
54 gbp_contract::~gbp_contract()
58 // not in the DB anymore.
59 m_db.release(key(), this);
62 const gbp_contract::key_t
63 gbp_contract::key() const
65 return (std::make_tuple(m_scope, m_sclass, m_dclass));
69 gbp_contract::operator==(const gbp_contract& gbpc) const
71 return ((key() == gbpc.key()) && (m_acl->handle() == gbpc.m_acl->handle()));
79 new gbp_contract_cmds::delete_cmd(m_hw, m_scope, m_sclass, m_dclass));
85 gbp_contract::replay()
88 HW::enqueue(new gbp_contract_cmds::create_cmd(
89 m_hw, m_scope, m_sclass, m_dclass, m_acl->handle(), m_gbp_rules,
90 m_allowed_ethertypes));
95 gbp_contract::to_string() const
98 s << "gbp-contract:[{" << m_scope << ", " << m_sclass << ", " << m_dclass
99 << "}, " << m_acl->to_string();
100 if (m_gbp_rules.size()) {
101 auto it = m_gbp_rules.cbegin();
102 while (it != m_gbp_rules.cend()) {
103 s << it->to_string();
108 for (auto e : m_allowed_ethertypes)
116 gbp_contract::update(const gbp_contract& r)
119 * create the table if it is not yet created
121 if (rc_t::OK != m_hw.rc()) {
122 HW::enqueue(new gbp_contract_cmds::create_cmd(
123 m_hw, m_scope, m_sclass, m_dclass, m_acl->handle(), m_gbp_rules,
124 m_allowed_ethertypes));
128 std::shared_ptr<gbp_contract>
129 gbp_contract::find_or_add(const gbp_contract& temp)
131 return (m_db.find_or_add(temp.key(), temp));
134 std::shared_ptr<gbp_contract>
135 gbp_contract::find(const key_t& k)
137 return (m_db.find(k));
140 std::shared_ptr<gbp_contract>
141 gbp_contract::singular() const
143 return find_or_add(*this);
147 gbp_contract::dump(std::ostream& os)
152 gbp_contract::event_handler::event_handler()
154 OM::register_listener(this);
155 inspect::register_handler({ "gbp-contract" }, "GBP Contract", this);
159 gbp_contract::event_handler::handle_replay()
165 gbp_contract::event_handler::handle_populate(const client_db::key_t& key)
167 std::shared_ptr<gbp_contract_cmds::dump_cmd> cmd =
168 std::make_shared<gbp_contract_cmds::dump_cmd>();
173 for (auto& record : *cmd) {
174 auto& payload = record.get_payload();
176 std::shared_ptr<ACL::l3_list> acl =
177 ACL::l3_list::find(payload.contract.acl_index);
180 gbp_contract::gbp_rules_t rules;
182 for (uint8_t i = 0; i < payload.contract.n_rules; i++) {
183 const gbp_rule::action_t action =
184 gbp_rule::action_t::from_int(payload.contract.rules[i].action);
185 const gbp_rule::hash_mode_t hm = gbp_rule::hash_mode_t::from_int(
186 payload.contract.rules[i].nh_set.hash_mode);
187 gbp_rule::next_hops_t nhs;
188 for (u8 j = 0; j < payload.contract.rules[i].nh_set.n_nhs; j++) {
189 gbp_rule::next_hop_t nh(
190 from_api(payload.contract.rules[i].nh_set.nhs[j].ip),
191 from_api(payload.contract.rules[i].nh_set.nhs[j].mac),
192 payload.contract.rules[i].nh_set.nhs[j].bd_id,
193 payload.contract.rules[i].nh_set.nhs[j].rd_id);
196 gbp_rule::next_hop_set_t next_hop_set(hm, nhs);
197 gbp_rule gr(i, next_hop_set, action);
201 ethertype_set_t allowed_ethertypes;
205 data = (((u8*)&payload.contract.n_ether_types) +
206 (sizeof(payload.contract.rules[0]) * payload.contract.n_rules));
210 for (uint8_t i = 0; i < n_et; i++) {
211 allowed_ethertypes.insert(ethertype_t::from_numeric_val(et[i]));
214 gbp_contract gbpc(payload.contract.scope, payload.contract.sclass,
215 payload.contract.dclass, *acl, rules,
217 OM::commit(key, gbpc);
218 VOM_LOG(log_level_t::DEBUG) << "read: " << gbpc.to_string();
220 VOM_LOG(log_level_t::ERROR) << " no ACL:" << payload.contract.acl_index;
226 gbp_contract::event_handler::order() const
228 return (dependency_t::ENTRY);
232 gbp_contract::event_handler::show(std::ostream& os)
238 operator<<(std::ostream& os, const gbp_contract::key_t& key)
240 os << "{ " << std::get<0>(key) << "," << std::get<1>(key) << ", "
241 << std::get<2>(key) << "}";
249 * fd.io coding-style-patch-verification: ON
252 * eval: (c-set-style "mozilla")