2 * Copyright (c) 2018 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_subnet.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_subnet_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
23 gbp_subnet::type_t::type_t(int v, const std::string s)
24 : enum_base<gbp_subnet::type_t>(v, s)
28 const gbp_subnet::type_t gbp_subnet::type_t::STITCHED_INTERNAL(
31 const gbp_subnet::type_t gbp_subnet::type_t::STITCHED_EXTERNAL(
34 const gbp_subnet::type_t gbp_subnet::type_t::TRANSPORT(2, "transport");
35 const gbp_subnet::type_t gbp_subnet::type_t::L3_OUT(3, "l3-out");
36 const gbp_subnet::type_t gbp_subnet::type_t::ANON_L3_OUT(4, "anon-l3-out");
38 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
40 gbp_subnet::event_handler gbp_subnet::m_evh;
42 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
43 const route::prefix_t& prefix,
55 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
56 const route::prefix_t& prefix,
57 const gbp_recirc& recirc,
58 const gbp_endpoint_group& epg)
62 , m_type(type_t::STITCHED_EXTERNAL)
63 , m_recirc(recirc.singular())
64 , m_epg(epg.singular())
69 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
70 const route::prefix_t& prefix,
83 gbp_subnet::gbp_subnet(const gbp_subnet& o)
86 , m_prefix(o.m_prefix)
88 , m_recirc(o.m_recirc)
90 , m_sclass(o.m_sclass)
94 gbp_subnet::~gbp_subnet()
97 m_db.release(key(), this);
100 const gbp_subnet::key_t
101 gbp_subnet::key() const
103 return (std::make_pair(m_rd->key(), m_prefix));
107 gbp_subnet::operator==(const gbp_subnet& gs) const
109 return ((key() == gs.key()) && (m_type == gs.m_type) &&
110 (m_recirc == gs.m_recirc) && (m_epg == gs.m_epg) &&
111 (m_sclass == gs.m_sclass));
118 HW::enqueue(new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->id(), m_prefix));
127 HW::enqueue(new gbp_subnet_cmds::create_cmd(
128 m_hw, m_rd->id(), m_prefix, m_type,
129 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
130 (m_epg ? m_epg->sclass() : m_sclass)));
135 gbp_subnet::to_string() const
137 std::ostringstream s;
138 s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
139 << m_prefix.to_string();
141 s << ", " << m_recirc->to_string();
143 s << ", " << m_epg->to_string();
151 gbp_subnet::update(const gbp_subnet& r)
153 if (rc_t::OK != m_hw.rc()) {
154 HW::enqueue(new gbp_subnet_cmds::create_cmd(
155 m_hw, m_rd->id(), m_prefix, m_type,
156 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
157 (m_epg ? m_epg->sclass() : m_sclass)));
159 if (m_type != r.m_type) {
161 m_recirc = r.m_recirc;
164 HW::enqueue(new gbp_subnet_cmds::create_cmd(
165 m_hw, m_rd->id(), m_prefix, m_type,
166 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
167 (m_epg ? m_epg->sclass() : m_sclass)));
172 std::shared_ptr<gbp_subnet>
173 gbp_subnet::find_or_add(const gbp_subnet& temp)
175 return (m_db.find_or_add(temp.key(), temp));
178 std::shared_ptr<gbp_subnet>
179 gbp_subnet::find(const key_t& k)
181 return (m_db.find(k));
184 std::shared_ptr<gbp_subnet>
185 gbp_subnet::singular() const
187 return find_or_add(*this);
191 gbp_subnet::dump(std::ostream& os)
196 gbp_subnet::event_handler::event_handler()
198 OM::register_listener(this);
199 inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
203 gbp_subnet::event_handler::handle_replay()
209 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
211 std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
212 std::make_shared<gbp_subnet_cmds::dump_cmd>();
217 for (auto& record : *cmd) {
218 auto& payload = record.get_payload();
220 route::prefix_t pfx = from_api(payload.subnet.prefix);
221 std::shared_ptr<gbp_route_domain> rd =
222 gbp_route_domain::find(payload.subnet.rd_id);
225 switch (payload.subnet.type) {
226 case GBP_API_SUBNET_TRANSPORT: {
227 gbp_subnet gs(*rd, pfx, type_t::TRANSPORT);
229 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
232 case GBP_API_SUBNET_STITCHED_INTERNAL: {
233 gbp_subnet gs(*rd, pfx, type_t::STITCHED_INTERNAL);
235 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
238 case GBP_API_SUBNET_L3_OUT: {
239 gbp_subnet gs(*rd, pfx, payload.subnet.sclass);
241 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
244 case GBP_API_SUBNET_ANON_L3_OUT: {
245 gbp_subnet gs(*rd, pfx, payload.subnet.sclass, type_t::ANON_L3_OUT);
247 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
250 case GBP_API_SUBNET_STITCHED_EXTERNAL: {
251 std::shared_ptr<interface> itf =
252 interface::find(payload.subnet.sw_if_index);
253 std::shared_ptr<gbp_endpoint_group> epg =
254 gbp_endpoint_group::find(payload.subnet.sclass);
257 std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
260 gbp_subnet gs(*rd, pfx, *recirc, *epg);
262 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
272 gbp_subnet::event_handler::order() const
274 return (dependency_t::ENTRY);
278 gbp_subnet::event_handler::show(std::ostream& os)
284 operator<<(std::ostream& os, const gbp_subnet::key_t& key)
286 os << "[" << key.first << ", " << key.second << "]";
294 * fd.io coding-style-patch-verification: ON
297 * eval: (c-set-style "mozilla")