vom: Add support for vtr in xconnect
[vpp.git] / extras / vom / vom / l2_binding.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/l2_binding.hpp"
17 #include "vom/l2_binding_cmds.hpp"
18 #include "vom/l2_vtr_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22 /**
23  * A DB of all the L2 Configs
24  */
25 singular_db<l2_binding::key_t, l2_binding> l2_binding::m_db;
26
27 l2_binding::event_handler l2_binding::m_evh;
28
29 const l2_binding::l2_port_type_t
30   l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL(0, "normal");
31 const l2_binding::l2_port_type_t l2_binding::l2_port_type_t::L2_PORT_TYPE_BVI(
32   1,
33   "bvi");
34 const l2_binding::l2_port_type_t
35   l2_binding::l2_port_type_t::L2_PORT_TYPE_UU_FWD(2, "uu-fwd");
36
37 l2_binding::l2_port_type_t::l2_port_type_t(int v, const std::string s)
38   : enum_base<l2_binding::l2_port_type_t>(v, s)
39 {
40 }
41
42 /**
43  * Construct a new object matching the desried state
44  */
45 l2_binding::l2_binding(const interface& itf, const bridge_domain& bd)
46   : m_itf(itf.singular())
47   , m_bd(bd.singular())
48   , m_port_type(l2_port_type_t::L2_PORT_TYPE_NORMAL)
49   , m_binding(0)
50   , m_vtr_op(l2_vtr_op_t::L2_VTR_DISABLED, rc_t::UNSET)
51   , m_vtr_op_tag(0)
52 {
53   if (interface::type_t::BVI == m_itf->type())
54     m_port_type = l2_port_type_t::L2_PORT_TYPE_BVI;
55 }
56
57 /**
58  * Construct a new object matching the desried state
59  */
60 l2_binding::l2_binding(const interface& itf,
61                        const bridge_domain& bd,
62                        const l2_port_type_t& port_type)
63   : m_itf(itf.singular())
64   , m_bd(bd.singular())
65   , m_port_type(port_type)
66   , m_binding(0)
67   , m_vtr_op(l2_vtr_op_t::L2_VTR_DISABLED, rc_t::UNSET)
68   , m_vtr_op_tag(0)
69 {
70 }
71
72 l2_binding::l2_binding(const l2_binding& o)
73   : m_itf(o.m_itf)
74   , m_bd(o.m_bd)
75   , m_port_type(o.m_port_type)
76   , m_binding(0)
77   , m_vtr_op(o.m_vtr_op)
78   , m_vtr_op_tag(o.m_vtr_op_tag)
79 {
80 }
81
82 const l2_binding::key_t&
83 l2_binding::key() const
84 {
85   return (m_itf->key());
86 }
87
88 bool
89 l2_binding::operator==(const l2_binding& l) const
90 {
91   return ((*m_itf == *l.m_itf) && (*m_bd == *l.m_bd) &&
92           (m_port_type == l.m_port_type));
93 }
94
95 std::shared_ptr<l2_binding>
96 l2_binding::find(const key_t& key)
97 {
98   return (m_db.find(key));
99 }
100
101 void
102 l2_binding::sweep()
103 {
104   if (m_binding && handle_t::INVALID != m_itf->handle()) {
105     HW::enqueue(new l2_binding_cmds::unbind_cmd(m_binding, m_itf->handle(),
106                                                 m_bd->id(), m_port_type));
107   }
108
109   // no need to undo the VTR operation.
110   HW::write();
111 }
112
113 void
114 l2_binding::replay()
115 {
116   if (m_binding && handle_t::INVALID != m_itf->handle()) {
117     HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
118                                               m_bd->id(), m_port_type));
119   }
120
121   if (m_vtr_op && handle_t::INVALID != m_itf->handle()) {
122     HW::enqueue(new set_vtr_op_cmd(m_vtr_op, m_itf->handle(), m_vtr_op_tag));
123   }
124 }
125
126 l2_binding::~l2_binding()
127 {
128   sweep();
129
130   // not in the DB anymore.
131   m_db.release(m_itf->key(), this);
132 }
133
134 std::string
135 l2_binding::to_string() const
136 {
137   std::ostringstream s;
138   s << "L2-binding:[" << m_itf->to_string() << " " << m_bd->to_string() << " "
139     << m_port_type.to_string() << " " << m_binding.to_string() << "]";
140
141   return (s.str());
142 }
143
144 void
145 l2_binding::set(const l2_vtr_op_t& op, uint16_t tag)
146 {
147   assert(rc_t::UNSET == m_vtr_op.rc());
148   m_vtr_op.set(rc_t::NOOP);
149   m_vtr_op.update(op);
150   m_vtr_op_tag = tag;
151 }
152
153 void
154 l2_binding::update(const l2_binding& desired)
155 {
156   /*
157    * the desired state is always that the interface should be created
158    */
159   if (rc_t::OK != m_binding.rc()) {
160     HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
161                                               m_bd->id(), m_port_type));
162   } else if (!(*m_bd == *desired.m_bd)) {
163     /*
164      * re-binding to a different BD. do unbind, bind.
165      */
166     HW::enqueue(new l2_binding_cmds::unbind_cmd(m_binding, m_itf->handle(),
167                                                 m_bd->id(), m_port_type));
168     m_bd = desired.m_bd;
169     HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
170                                               m_bd->id(), m_port_type));
171   }
172
173   /*
174    * set the VTR operation if request
175    */
176   if (m_vtr_op.update(desired.m_vtr_op)) {
177     HW::enqueue(new set_vtr_op_cmd(m_vtr_op, m_itf->handle(), m_vtr_op_tag));
178   }
179 }
180
181 std::shared_ptr<l2_binding>
182 l2_binding::find_or_add(const l2_binding& temp)
183 {
184   return (m_db.find_or_add(temp.m_itf->key(), temp));
185 }
186
187 std::shared_ptr<l2_binding>
188 l2_binding::singular() const
189 {
190   return find_or_add(*this);
191 }
192
193 void
194 l2_binding::dump(std::ostream& os)
195 {
196   db_dump(m_db, os);
197 }
198
199 l2_binding::event_handler::event_handler()
200 {
201   OM::register_listener(this);
202   inspect::register_handler({ "l2" }, "L2 bindings", this);
203 }
204
205 void
206 l2_binding::event_handler::handle_replay()
207 {
208   m_db.replay();
209 }
210
211 void
212 l2_binding::event_handler::handle_populate(const client_db::key_t& key)
213 {
214   /**
215    * This is done while populating the bridge-domain
216    */
217 }
218
219 dependency_t
220 l2_binding::event_handler::order() const
221 {
222   return (dependency_t::BINDING);
223 }
224
225 void
226 l2_binding::event_handler::show(std::ostream& os)
227 {
228   db_dump(m_db, os);
229 }
230 }
231
232 /*
233  * fd.io coding-style-patch-verification: ON
234  *
235  * Local Variables:
236  * eval: (c-set-style "mozilla")
237  * End:
238  */