blob: dd49e570a8eaf32249515c1b27a5027e43dec1e1 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/l2_binding_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018
19namespace VOM {
20/**
21 * A DB of all the L2 Configs
22 */
Neale Rannsfd920602017-11-23 12:15:00 -080023singular_db<l2_binding::key_t, l2_binding> l2_binding::m_db;
Neale Ranns812ed392017-10-16 04:20:13 -070024
25l2_binding::event_handler l2_binding::m_evh;
26
27/*
28 * Make sure these are in sync with the smae enum in VPP
29 */
30const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_DISABLED(
31 0,
32 "disabled");
33const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_PUSH_1(1,
34 "push-1");
35const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_PUSH_2(2,
36 "push-2");
37const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_POP_1(3, "pop-1");
38const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_POP_2(4, "pop-2");
39const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_TRANSLATE_1_1(
40 5,
41 "translate-1-1");
42const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_TRANSLATE_1_2(
43 6,
44 "translate-1-2");
45const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_TRANSLATE_2_1(
46 7,
47 "translate-2-1");
48const l2_binding::l2_vtr_op_t l2_binding::l2_vtr_op_t::L2_VTR_TRANSLATE_2_2(
49 5,
50 "translate-2-2");
51
52l2_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 */
60l2_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
69l2_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
Neale Rannsfd920602017-11-23 12:15:00 -080078const l2_binding::key_t&
79l2_binding::key() const
80{
81 return (m_itf->key());
82}
83
84bool
85l2_binding::operator==(const l2_binding& l) const
86{
87 return ((*m_itf == *l.m_itf) && (*m_bd == *l.m_bd));
88}
89
90std::shared_ptr<l2_binding>
91l2_binding::find(const key_t& key)
92{
93 return (m_db.find(key));
94}
95
Neale Ranns812ed392017-10-16 04:20:13 -070096void
97l2_binding::sweep()
98{
99 if (m_binding && handle_t::INVALID != m_itf->handle()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700100 HW::enqueue(
101 new l2_binding_cmds::unbind_cmd(m_binding, m_itf->handle(), m_bd->id(),
102 interface::type_t::BVI == m_itf->type()));
Neale Ranns812ed392017-10-16 04:20:13 -0700103 }
104
105 // no need to undo the VTR operation.
106 HW::write();
107}
108
109void
110l2_binding::replay()
111{
112 if (m_binding && handle_t::INVALID != m_itf->handle()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700113 HW::enqueue(
114 new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_bd->id(),
115 interface::type_t::BVI == m_itf->type()));
Neale Ranns812ed392017-10-16 04:20:13 -0700116 }
117
118 if (m_vtr_op && handle_t::INVALID != m_itf->handle()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700119 HW::enqueue(new l2_binding_cmds::set_vtr_op_cmd(m_vtr_op, m_itf->handle(),
120 m_vtr_op_tag));
Neale Ranns812ed392017-10-16 04:20:13 -0700121 }
122}
123
124l2_binding::~l2_binding()
125{
126 sweep();
127
128 // not in the DB anymore.
Neale Rannsfd920602017-11-23 12:15:00 -0800129 m_db.release(m_itf->key(), this);
Neale Ranns812ed392017-10-16 04:20:13 -0700130}
131
132std::string
133l2_binding::to_string() const
134{
135 std::ostringstream s;
Neale Rannsfd920602017-11-23 12:15:00 -0800136 s << "L2-binding:[" << m_itf->to_string() << " " << m_bd->to_string() << " "
Neale Ranns812ed392017-10-16 04:20:13 -0700137 << m_binding.to_string() << "]";
138
139 return (s.str());
140}
141
142void
143l2_binding::set(const l2_vtr_op_t& op, uint16_t tag)
144{
145 assert(rc_t::UNSET == m_vtr_op.rc());
146 m_vtr_op.set(rc_t::NOOP);
147 m_vtr_op.update(op);
148 m_vtr_op_tag = tag;
149}
150
151void
152l2_binding::update(const l2_binding& desired)
153{
154 /*
155 * the desired state is always that the interface should be created
156 */
157 if (rc_t::OK != m_binding.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700158 HW::enqueue(
159 new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_bd->id(),
160 interface::type_t::BVI == m_itf->type()));
Neale Ranns812ed392017-10-16 04:20:13 -0700161 }
162
163 /*
164 * set the VTR operation is request
165 */
166 if (m_vtr_op.update(desired.m_vtr_op)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700167 HW::enqueue(new l2_binding_cmds::set_vtr_op_cmd(m_vtr_op, m_itf->handle(),
168 m_vtr_op_tag));
Neale Ranns812ed392017-10-16 04:20:13 -0700169 }
170}
171
172std::shared_ptr<l2_binding>
173l2_binding::find_or_add(const l2_binding& temp)
174{
Neale Rannsfd920602017-11-23 12:15:00 -0800175 return (m_db.find_or_add(temp.m_itf->key(), temp));
Neale Ranns812ed392017-10-16 04:20:13 -0700176}
177
178std::shared_ptr<l2_binding>
179l2_binding::singular() const
180{
181 return find_or_add(*this);
182}
183
184void
185l2_binding::dump(std::ostream& os)
186{
187 m_db.dump(os);
188}
189
190l2_binding::event_handler::event_handler()
191{
192 OM::register_listener(this);
193 inspect::register_handler({ "l2" }, "L2 bindings", this);
194}
195
196void
197l2_binding::event_handler::handle_replay()
198{
199 m_db.replay();
200}
201
202void
203l2_binding::event_handler::handle_populate(const client_db::key_t& key)
204{
205 /**
206 * This is done while populating the bridge-domain
207 */
208}
209
210dependency_t
211l2_binding::event_handler::order() const
212{
213 return (dependency_t::BINDING);
214}
215
216void
217l2_binding::event_handler::show(std::ostream& os)
218{
219 m_db.dump(os);
220}
221}
222
223/*
224 * fd.io coding-style-patch-verification: ON
225 *
226 * Local Variables:
227 * eval: (c-set-style "mozilla")
228 * End:
229 */