VPP Object Model (VOM)
[vpp.git] / src / vpp-api / 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/cmd.hpp"
18
19 namespace VOM {
20 /**
21  * A DB of all the L2 Configs
22  */
23 singular_db<const handle_t, l2_binding> l2_binding::m_db;
24
25 l2_binding::event_handler l2_binding::m_evh;
26
27 /*
28  * Make sure these are in sync with the smae enum in VPP
29  */
30 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_DISABLED(
31   0,
32   "disabled");
33 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_PUSH_1(1,
34                                                                      "push-1");
35 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_PUSH_2(2,
36                                                                      "push-2");
37 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_POP_1(3, "pop-1");
38 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_POP_2(4, "pop-2");
39 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_TRANSLATE_1_1(
40   5,
41   "translate-1-1");
42 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_TRANSLATE_1_2(
43   6,
44   "translate-1-2");
45 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_TRANSLATE_2_1(
46   7,
47   "translate-2-1");
48 const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_TRANSLATE_2_2(
49   5,
50   "translate-2-2");
51
52 l2_binding::l2_vtr_op_t::l2_vtr_op_t(int v, const std::string s)
53   : enum_base<l2_binding::l2_vtr_op_t>(v, s)
54 {
55 }
56
57 /**
58  * Construct a new object matching the desried state
59  */
60 l2_binding::l2_binding(const interface& itf, const bridge_domain& bd)
61   : m_itf(itf.singular())
62   , m_bd(bd.singular())
63   , m_binding(0)
64   , m_vtr_op(l2_vtr_op_t::L2_VTR_DISABLED, rc_t::UNSET)
65   , m_vtr_op_tag(0)
66 {
67 }
68
69 l2_binding::l2_binding(const l2_binding& o)
70   : m_itf(o.m_itf)
71   , m_bd(o.m_bd)
72   , m_binding(0)
73   , m_vtr_op(o.m_vtr_op)
74   , m_vtr_op_tag(o.m_vtr_op_tag)
75 {
76 }
77
78 void
79 l2_binding::sweep()
80 {
81   if (m_binding && handle_t::INVALID != m_itf->handle()) {
82     HW::enqueue(new unbind_cmd(m_binding, m_itf->handle(), m_bd->id(),
83                                interface::type_t::BVI == m_itf->type()));
84   }
85
86   // no need to undo the VTR operation.
87   HW::write();
88 }
89
90 void
91 l2_binding::replay()
92 {
93   if (m_binding && handle_t::INVALID != m_itf->handle()) {
94     HW::enqueue(new bind_cmd(m_binding, m_itf->handle(), m_bd->id(),
95                              interface::type_t::BVI == m_itf->type()));
96   }
97
98   if (m_vtr_op && handle_t::INVALID != m_itf->handle()) {
99     HW::enqueue(new set_vtr_op_cmd(m_vtr_op, m_itf->handle(), m_vtr_op_tag));
100   }
101 }
102
103 l2_binding::~l2_binding()
104 {
105   sweep();
106
107   // not in the DB anymore.
108   m_db.release(m_itf->handle(), this);
109 }
110
111 std::string
112 l2_binding::to_string() const
113 {
114   std::ostringstream s;
115   s << "L2-config:[" << m_itf->to_string() << " " << m_bd->to_string() << " "
116     << m_binding.to_string() << "]";
117
118   return (s.str());
119 }
120
121 void
122 l2_binding::set(const l2_vtr_op_t& op, uint16_t tag)
123 {
124   assert(rc_t::UNSET == m_vtr_op.rc());
125   m_vtr_op.set(rc_t::NOOP);
126   m_vtr_op.update(op);
127   m_vtr_op_tag = tag;
128 }
129
130 void
131 l2_binding::update(const l2_binding& desired)
132 {
133   /*
134  * the desired state is always that the interface should be created
135  */
136   if (rc_t::OK != m_binding.rc()) {
137     HW::enqueue(new bind_cmd(m_binding, m_itf->handle(), m_bd->id(),
138                              interface::type_t::BVI == m_itf->type()));
139   }
140
141   /*
142  * set the VTR operation is request
143  */
144   if (m_vtr_op.update(desired.m_vtr_op)) {
145     HW::enqueue(new set_vtr_op_cmd(m_vtr_op, m_itf->handle(), m_vtr_op_tag));
146   }
147 }
148
149 std::shared_ptr<l2_binding>
150 l2_binding::find_or_add(const l2_binding& temp)
151 {
152   return (m_db.find_or_add(temp.m_itf->handle(), temp));
153 }
154
155 std::shared_ptr<l2_binding>
156 l2_binding::singular() const
157 {
158   return find_or_add(*this);
159 }
160
161 void
162 l2_binding::dump(std::ostream& os)
163 {
164   m_db.dump(os);
165 }
166
167 l2_binding::event_handler::event_handler()
168 {
169   OM::register_listener(this);
170   inspect::register_handler({ "l2" }, "L2 bindings", this);
171 }
172
173 void
174 l2_binding::event_handler::handle_replay()
175 {
176   m_db.replay();
177 }
178
179 void
180 l2_binding::event_handler::handle_populate(const client_db::key_t& key)
181 {
182   /**
183  * This is done while populating the bridge-domain
184  */
185 }
186
187 dependency_t
188 l2_binding::event_handler::order() const
189 {
190   return (dependency_t::BINDING);
191 }
192
193 void
194 l2_binding::event_handler::show(std::ostream& os)
195 {
196   m_db.dump(os);
197 }
198 }
199
200 /*
201  * fd.io coding-style-patch-verification: ON
202  *
203  * Local Variables:
204  * eval: (c-set-style "mozilla")
205  * End:
206  */