GBP: add allowed ethertypes to contracts
[vpp.git] / extras / vom / vom / gbp_vxlan.cpp
1 /*
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:
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_vxlan.hpp"
17 #include "vom/gbp_vxlan_cmds.hpp"
18 #include "vom/interface.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 const std::string GBP_VXLAN_NAME = "gbp-vxlan";
24
25 /**
26  * A DB of al the interfaces, key on the name
27  */
28 singular_db<gbp_vxlan::key_t, gbp_vxlan> gbp_vxlan::m_db;
29
30 gbp_vxlan::event_handler gbp_vxlan::m_evh;
31
32 gbp_vxlan::gbp_vxlan(uint32_t vni, const gbp_route_domain& grd)
33   : interface(mk_name(vni),
34               interface::type_t::UNKNOWN,
35               interface::admin_state_t::UP)
36   , m_vni(vni)
37   , m_gbd()
38   , m_grd(grd.singular())
39 {
40 }
41 gbp_vxlan::gbp_vxlan(uint32_t vni, const gbp_bridge_domain& gbd)
42   : interface(mk_name(vni),
43               interface::type_t::UNKNOWN,
44               interface::admin_state_t::UP)
45   , m_vni(vni)
46   , m_gbd(gbd.singular())
47   , m_grd()
48 {
49 }
50
51 gbp_vxlan::gbp_vxlan(const gbp_vxlan& vt)
52   : interface(vt)
53   , m_vni(vt.m_vni)
54   , m_gbd(vt.m_gbd)
55   , m_grd(vt.m_grd)
56 {
57 }
58
59 std::string
60 gbp_vxlan::mk_name(uint32_t vni)
61 {
62   std::ostringstream s;
63
64   s << GBP_VXLAN_NAME << "-" << vni;
65
66   return (s.str());
67 }
68
69 const gbp_vxlan::key_t
70 gbp_vxlan::key() const
71 {
72   return (m_vni);
73 }
74
75 bool
76 gbp_vxlan::operator==(const gbp_vxlan& vt) const
77 {
78   return (m_vni == vt.m_vni);
79 }
80
81 void
82 gbp_vxlan::sweep()
83 {
84   if (rc_t::OK == m_hdl) {
85     HW::enqueue(new gbp_vxlan_cmds::delete_cmd(m_hdl, m_vni));
86   }
87   HW::write();
88 }
89
90 void
91 gbp_vxlan::replay()
92 {
93   if (rc_t::OK == m_hdl) {
94     if (m_grd)
95       HW::enqueue(new gbp_vxlan_cmds::create_cmd(m_hdl, name(), m_vni, false,
96                                                  m_grd->id()));
97     else if (m_gbd)
98       HW::enqueue(new gbp_vxlan_cmds::create_cmd(m_hdl, name(), m_vni, true,
99                                                  m_gbd->id()));
100   }
101 }
102
103 gbp_vxlan::~gbp_vxlan()
104 {
105   sweep();
106   m_db.release(key(), this);
107 }
108
109 std::string
110 gbp_vxlan::to_string() const
111 {
112   std::ostringstream s;
113   s << "gbp-vxlan:[" << m_vni << "]";
114
115   return (s.str());
116 }
117
118 std::shared_ptr<gbp_vxlan>
119 gbp_vxlan::find(const key_t key)
120 {
121   return (m_db.find(key));
122 }
123
124 void
125 gbp_vxlan::update(const gbp_vxlan& desired)
126 {
127   /*
128    * the desired state is always that the interface should be created
129    */
130   if (rc_t::OK != m_hdl) {
131     if (m_grd)
132       HW::enqueue(new gbp_vxlan_cmds::create_cmd(m_hdl, name(), m_vni, false,
133                                                  m_grd->id()));
134     else if (m_gbd)
135       HW::enqueue(new gbp_vxlan_cmds::create_cmd(m_hdl, name(), m_vni, true,
136                                                  m_gbd->id()));
137   }
138 }
139
140 std::shared_ptr<gbp_vxlan>
141 gbp_vxlan::find_or_add(const gbp_vxlan& temp)
142 {
143   return (m_db.find_or_add(temp.key(), temp));
144 }
145
146 std::shared_ptr<gbp_vxlan>
147 gbp_vxlan::singular() const
148 {
149   return find_or_add(*this);
150 }
151
152 std::shared_ptr<interface>
153 gbp_vxlan::singular_i() const
154 {
155   return find_or_add(*this);
156 }
157
158 void
159 gbp_vxlan::dump(std::ostream& os)
160 {
161   db_dump(m_db, os);
162 }
163
164 void
165 gbp_vxlan::event_handler::handle_populate(const client_db::key_t& key)
166 {
167   /*
168    * dump VPP Bridge domains
169    */
170   std::shared_ptr<gbp_vxlan_cmds::dump_cmd> cmd =
171     std::make_shared<gbp_vxlan_cmds::dump_cmd>();
172
173   HW::enqueue(cmd);
174   HW::write();
175
176   for (auto& record : *cmd) {
177     auto& payload = record.get_payload();
178
179     if (GBP_VXLAN_TUNNEL_MODE_L3 == payload.tunnel.mode) {
180       auto rd = gbp_route_domain::find(payload.tunnel.bd_rd_id);
181
182       if (rd) {
183         gbp_vxlan vt(payload.tunnel.vni, *rd);
184         OM::commit(key, vt);
185         VOM_LOG(log_level_t::DEBUG) << "dump: " << vt.to_string();
186       }
187     } else {
188       auto bd = gbp_bridge_domain::find(payload.tunnel.bd_rd_id);
189
190       if (bd) {
191         gbp_vxlan vt(payload.tunnel.vni, *bd);
192         OM::commit(key, vt);
193         VOM_LOG(log_level_t::DEBUG) << "dump: " << vt.to_string();
194       }
195     }
196   }
197 }
198
199 gbp_vxlan::event_handler::event_handler()
200 {
201   OM::register_listener(this);
202   inspect::register_handler({ "gvt", "gbp-vxlan-tunnel" }, "GBP VXLAN Tunnels",
203                             this);
204 }
205
206 void
207 gbp_vxlan::event_handler::handle_replay()
208 {
209   m_db.replay();
210 }
211
212 dependency_t
213 gbp_vxlan::event_handler::order() const
214 {
215   return (dependency_t::BINDING);
216 }
217
218 void
219 gbp_vxlan::event_handler::show(std::ostream& os)
220 {
221   db_dump(m_db, os);
222 }
223 }
224
225 /*
226  * fd.io coding-style-patch-verification: ON
227  *
228  * Local Variables:
229  * eval: (c-set-style "mozilla")
230  * End:
231  */