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/gbp_subnet_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
22 gbp_subnet::type_t::type_t(int v, const std::string s)
23 : enum_base<gbp_subnet::type_t>(v, s)
27 const gbp_subnet::type_t gbp_subnet::type_t::INTERNAL(0, "internal");
28 const gbp_subnet::type_t gbp_subnet::type_t::EXTERNAL(1, "external");
30 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
32 gbp_subnet::event_handler gbp_subnet::m_evh;
34 gbp_subnet::gbp_subnet(const route_domain& rd, const route::prefix_t& prefix)
38 , m_type(type_t::INTERNAL)
44 gbp_subnet::gbp_subnet(const route_domain& rd,
45 const route::prefix_t& prefix,
46 const gbp_recirc& recirc,
47 const gbp_endpoint_group& epg)
51 , m_type(type_t::EXTERNAL)
52 , m_recirc(recirc.singular())
53 , m_epg(epg.singular())
57 gbp_subnet::gbp_subnet(const gbp_subnet& o)
60 , m_prefix(o.m_prefix)
62 , m_recirc(o.m_recirc)
67 gbp_subnet::~gbp_subnet()
70 m_db.release(key(), this);
73 const gbp_subnet::key_t
74 gbp_subnet::key() const
76 return (std::make_pair(m_rd->key(), m_prefix));
80 gbp_subnet::operator==(const gbp_subnet& gs) const
82 return ((key() == gs.key()) && (m_type == gs.m_type) &&
83 (m_recirc == gs.m_recirc) && (m_epg == gs.m_epg));
91 new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
100 HW::enqueue(new gbp_subnet_cmds::create_cmd(
101 m_hw, m_rd->table_id(), m_prefix, (m_type == type_t::INTERNAL),
102 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
103 (m_epg ? m_epg->id() : ~0)));
108 gbp_subnet::to_string() const
110 std::ostringstream s;
111 s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
112 << m_prefix.to_string();
114 s << ", " << m_recirc->to_string();
116 s << ", " << m_epg->to_string();
124 gbp_subnet::update(const gbp_subnet& r)
126 if (rc_t::OK != m_hw.rc()) {
127 HW::enqueue(new gbp_subnet_cmds::create_cmd(
128 m_hw, m_rd->table_id(), m_prefix, (m_type == type_t::INTERNAL),
129 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
130 (m_epg ? m_epg->id() : ~0)));
132 if (m_type != r.m_type) {
134 m_recirc = r.m_recirc;
137 HW::enqueue(new gbp_subnet_cmds::create_cmd(
138 m_hw, m_rd->table_id(), m_prefix, (m_type == type_t::INTERNAL),
139 (m_recirc ? m_recirc->handle() : handle_t::INVALID),
140 (m_epg ? m_epg->id() : ~0)));
145 std::shared_ptr<gbp_subnet>
146 gbp_subnet::find_or_add(const gbp_subnet& temp)
148 return (m_db.find_or_add(temp.key(), temp));
151 std::shared_ptr<gbp_subnet>
152 gbp_subnet::find(const key_t& k)
154 return (m_db.find(k));
157 std::shared_ptr<gbp_subnet>
158 gbp_subnet::singular() const
160 return find_or_add(*this);
164 gbp_subnet::dump(std::ostream& os)
169 gbp_subnet::event_handler::event_handler()
171 OM::register_listener(this);
172 inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
176 gbp_subnet::event_handler::handle_replay()
182 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
184 std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
185 std::make_shared<gbp_subnet_cmds::dump_cmd>();
190 for (auto& record : *cmd) {
191 auto& payload = record.get_payload();
193 route::prefix_t pfx(payload.subnet.is_ip6, payload.subnet.address,
194 payload.subnet.address_length);
195 std::shared_ptr<route_domain> rd =
196 route_domain::find(payload.subnet.table_id);
199 if (payload.subnet.is_internal) {
200 gbp_subnet gs(*rd, pfx);
202 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
204 std::shared_ptr<interface> itf =
205 interface::find(payload.subnet.sw_if_index);
206 std::shared_ptr<gbp_endpoint_group> epg =
207 gbp_endpoint_group::find(payload.subnet.epg_id);
210 std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
213 gbp_subnet gs(*rd, pfx, *recirc, *epg);
215 VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
224 gbp_subnet::event_handler::order() const
226 return (dependency_t::ENTRY);
230 gbp_subnet::event_handler::show(std::ostream& os)
237 * fd.io coding-style-patch-verification: ON
240 * eval: (c-set-style "mozilla")