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(1, "transport");
36 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
38 gbp_subnet::event_handler gbp_subnet::m_evh;
40 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
41 const route::prefix_t& prefix,
52 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
53 const route::prefix_t& prefix,
54 const gbp_recirc& recirc,
55 const gbp_endpoint_group& epg)
59 , m_type(type_t::STITCHED_EXTERNAL)
60 , m_recirc(recirc.singular())
61 , m_epg(epg.singular())
65 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
66 const route::prefix_t& prefix,
67 const gbp_endpoint_group& epg)
71 , m_type(type_t::L3_OUT)
73 , m_epg(epg.singular())
77 gbp_subnet::gbp_subnet(const gbp_subnet& o)
80 , m_prefix(o.m_prefix)
82 , m_recirc(o.m_recirc)
87 gbp_subnet::~gbp_subnet()
90 m_db.release(key(), this);
93 const gbp_subnet::key_t
94 gbp_subnet::key() const
96 return (std::make_pair(m_rd->key(), m_prefix));
100 gbp_subnet::operator==(const gbp_subnet& gs) const
102 return ((key() == gs.key()) && (m_type == gs.m_type) &&
103 (m_recirc == gs.m_recirc) && (m_epg == gs.m_epg));
110 HW::enqueue(new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->id(), m_prefix));
119 HW::enqueue(new gbp_subnet_cmds::create_cmd(
120 m_hw, m_rd->id(), m_prefix, m_type,
121 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
122 (m_epg ? m_epg->id() : ~0)));
127 gbp_subnet::to_string() const
129 std::ostringstream s;
130 s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
131 << m_prefix.to_string();
133 s << ", " << m_recirc->to_string();
135 s << ", " << m_epg->to_string();
143 gbp_subnet::update(const gbp_subnet& r)
145 if (rc_t::OK != m_hw.rc()) {
146 HW::enqueue(new gbp_subnet_cmds::create_cmd(
147 m_hw, m_rd->id(), m_prefix, m_type,
148 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
149 (m_epg ? m_epg->id() : ~0)));
151 if (m_type != r.m_type) {
153 m_recirc = r.m_recirc;
156 HW::enqueue(new gbp_subnet_cmds::create_cmd(
157 m_hw, m_rd->id(), m_prefix, m_type,
158 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
159 (m_epg ? m_epg->id() : ~0)));
164 std::shared_ptr<gbp_subnet>
165 gbp_subnet::find_or_add(const gbp_subnet& temp)
167 return (m_db.find_or_add(temp.key(), temp));
170 std::shared_ptr<gbp_subnet>
171 gbp_subnet::find(const key_t& k)
173 return (m_db.find(k));
176 std::shared_ptr<gbp_subnet>
177 gbp_subnet::singular() const
179 return find_or_add(*this);
183 gbp_subnet::dump(std::ostream& os)
188 gbp_subnet::event_handler::event_handler()
190 OM::register_listener(this);
191 inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
195 gbp_subnet::event_handler::handle_replay()
201 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
203 std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
204 std::make_shared<gbp_subnet_cmds::dump_cmd>();
209 for (auto& record : *cmd) {
210 auto& payload = record.get_payload();
212 route::prefix_t pfx = from_api(payload.subnet.prefix);
213 std::shared_ptr<gbp_route_domain> rd =
214 gbp_route_domain::find(payload.subnet.rd_id);
217 switch (payload.subnet.type) {
218 case GBP_API_SUBNET_TRANSPORT: {
219 gbp_subnet gs(*rd, pfx, type_t::TRANSPORT);
221 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
224 case GBP_API_SUBNET_STITCHED_INTERNAL: {
225 gbp_subnet gs(*rd, pfx, type_t::STITCHED_INTERNAL);
227 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
230 case GBP_API_SUBNET_L3_OUT: {
231 std::shared_ptr<gbp_endpoint_group> epg =
232 gbp_endpoint_group::find(payload.subnet.epg_id);
234 gbp_subnet gs(*rd, pfx, *epg);
236 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
239 case GBP_API_SUBNET_STITCHED_EXTERNAL: {
240 std::shared_ptr<interface> itf =
241 interface::find(payload.subnet.sw_if_index);
242 std::shared_ptr<gbp_endpoint_group> epg =
243 gbp_endpoint_group::find(payload.subnet.epg_id);
246 std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
249 gbp_subnet gs(*rd, pfx, *recirc, *epg);
251 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
261 gbp_subnet::event_handler::order() const
263 return (dependency_t::ENTRY);
267 gbp_subnet::event_handler::show(std::ostream& os)
273 operator<<(std::ostream& os, const gbp_subnet::key_t& key)
275 os << "[" << key.first << ", " << key.second << "]";
283 * fd.io coding-style-patch-verification: ON
286 * eval: (c-set-style "mozilla")