GBP V2
[vpp.git] / src / vpp-api / vom / gbp_subnet.cpp
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include "vom/gbp_subnet.hpp"
17 #include "vom/gbp_subnet_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
19
20 namespace VOM {
21
22 gbp_subnet::type_t::type_t(int v, const std::string s)
23   : enum_base<gbp_subnet::type_t>(v, s)
24 {
25 }
26
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");
29
30 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
31
32 gbp_subnet::event_handler gbp_subnet::m_evh;
33
34 gbp_subnet::gbp_subnet(const route_domain& rd, const route::prefix_t& prefix)
35   : m_hw(false)
36   , m_rd(rd.singular())
37   , m_prefix(prefix)
38   , m_type(type_t::INTERNAL)
39   , m_recirc(nullptr)
40   , m_epg(nullptr)
41 {
42 }
43
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)
48   : m_hw(false)
49   , m_rd(rd.singular())
50   , m_prefix(prefix)
51   , m_type(type_t::EXTERNAL)
52   , m_recirc(recirc.singular())
53   , m_epg(epg.singular())
54 {
55 }
56
57 gbp_subnet::gbp_subnet(const gbp_subnet& o)
58   : m_hw(o.m_hw)
59   , m_rd(o.m_rd)
60   , m_prefix(o.m_prefix)
61   , m_type(o.m_type)
62   , m_recirc(o.m_recirc)
63   , m_epg(o.m_epg)
64 {
65 }
66
67 gbp_subnet::~gbp_subnet()
68 {
69   sweep();
70   m_db.release(key(), this);
71 }
72
73 const gbp_subnet::key_t
74 gbp_subnet::key() const
75 {
76   return (std::make_pair(m_rd->key(), m_prefix));
77 }
78
79 bool
80 gbp_subnet::operator==(const gbp_subnet& gbpe) const
81 {
82   return ((key() == gbpe.key()) && (m_recirc == gbpe.m_recirc) &&
83           (m_epg == gbpe.m_epg));
84 }
85
86 void
87 gbp_subnet::sweep()
88 {
89   if (m_hw) {
90     HW::enqueue(
91       new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
92   }
93   HW::write();
94 }
95
96 void
97 gbp_subnet::replay()
98 {
99   if (m_hw) {
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)));
104   }
105 }
106
107 std::string
108 gbp_subnet::to_string() const
109 {
110   std::ostringstream s;
111   s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
112     << m_prefix.to_string();
113   if (m_recirc)
114     s << ", " << m_recirc->to_string();
115   if (m_epg)
116     s << ", " << m_epg->to_string();
117
118   s << "]";
119
120   return (s.str());
121 }
122
123 void
124 gbp_subnet::update(const gbp_subnet& r)
125 {
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)));
131   }
132 }
133
134 std::shared_ptr<gbp_subnet>
135 gbp_subnet::find_or_add(const gbp_subnet& temp)
136 {
137   return (m_db.find_or_add(temp.key(), temp));
138 }
139
140 std::shared_ptr<gbp_subnet>
141 gbp_subnet::find(const key_t& k)
142 {
143   return (m_db.find(k));
144 }
145
146 std::shared_ptr<gbp_subnet>
147 gbp_subnet::singular() const
148 {
149   return find_or_add(*this);
150 }
151
152 void
153 gbp_subnet::dump(std::ostream& os)
154 {
155   db_dump(m_db, os);
156 }
157
158 gbp_subnet::event_handler::event_handler()
159 {
160   OM::register_listener(this);
161   inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
162 }
163
164 void
165 gbp_subnet::event_handler::handle_replay()
166 {
167   m_db.replay();
168 }
169
170 void
171 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
172 {
173   std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
174     std::make_shared<gbp_subnet_cmds::dump_cmd>();
175
176   HW::enqueue(cmd);
177   HW::write();
178
179   for (auto& record : *cmd) {
180     auto& payload = record.get_payload();
181
182     route::prefix_t pfx(payload.subnet.is_ip6, payload.subnet.address,
183                         payload.subnet.address_length);
184     std::shared_ptr<route_domain> rd =
185       route_domain::find(payload.subnet.table_id);
186
187     if (rd) {
188       if (payload.subnet.is_internal) {
189         gbp_subnet gs(*rd, pfx);
190         OM::commit(key, gs);
191         VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
192       } else {
193         std::shared_ptr<interface> itf =
194           interface::find(payload.subnet.sw_if_index);
195         std::shared_ptr<gbp_endpoint_group> epg =
196           gbp_endpoint_group::find(payload.subnet.epg_id);
197
198         if (itf && epg) {
199           std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
200
201           if (recirc) {
202             gbp_subnet gs(*rd, pfx, *recirc, *epg);
203             OM::commit(key, gs);
204             VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
205           }
206         }
207       }
208     }
209   }
210 }
211
212 dependency_t
213 gbp_subnet::event_handler::order() const
214 {
215   return (dependency_t::ENTRY);
216 }
217
218 void
219 gbp_subnet::event_handler::show(std::ostream& os)
220 {
221   db_dump(m_db, os);
222 }
223 } // namespace VOM
224
225 /*
226  * fd.io coding-style-patch-verification: ON
227  *
228  * Local Variables:
229  * eval: (c-set-style "mozilla")
230  * End:
231  */