blob: 88bec50ac3da46bb761fb22dced3a83102cb4b2d [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/interface_span.hpp"
17#include "vom/cmd.hpp"
18
19namespace VOM {
20/**
21 * A DB of all interface_span config
22 */
23singular_db<interface_span::key_type_t, interface_span> interface_span::m_db;
24
25interface_span::event_handler interface_span::m_evh;
26
27interface_span::interface_span(const interface& itf_from,
28 const interface& itf_to,
29 interface_span::state_t state)
30 : m_itf_from(itf_from.singular())
31 , m_itf_to(itf_to.singular())
32 , m_state(state)
33 , m_config(true)
34{
35}
36
37interface_span::interface_span(const interface_span& o)
38 : m_itf_from(o.m_itf_from)
39 , m_itf_to(o.m_itf_to)
40 , m_state(o.m_state)
41 , m_config(o.m_config)
42{
43}
44
45interface_span::~interface_span()
46{
47 sweep();
48
49 // not in the DB anymore.
50 m_db.release(make_pair(m_itf_from->key(), m_itf_to->key()), this);
51}
52
53void
54interface_span::sweep()
55{
56 if (m_config) {
57 HW::enqueue(
58 new unconfig_cmd(m_config, m_itf_from->handle(), m_itf_to->handle()));
59 }
60 HW::write();
61}
62
63void
64interface_span::dump(std::ostream& os)
65{
66 m_db.dump(os);
67}
68
69void
70interface_span::replay()
71{
72 if (m_config) {
73 HW::enqueue(new config_cmd(m_config, m_itf_from->handle(),
74 m_itf_to->handle(), m_state));
75 }
76}
77
78std::string
79interface_span::to_string() const
80{
81 std::ostringstream s;
82 s << "Itf Span-config:"
83 << " itf-from:" << m_itf_from->to_string()
84 << " itf-to:" << m_itf_to->to_string() << " state:" << m_state.to_string();
85
86 return (s.str());
87}
88
89void
90interface_span::update(const interface_span& desired)
91{
92 if (!m_config) {
93 HW::enqueue(new config_cmd(m_config, m_itf_from->handle(),
94 m_itf_to->handle(), m_state));
95 }
96}
97
98std::ostream&
99operator<<(std::ostream& os, const interface_span::key_type_t& key)
100{
101 os << "[" << key.first << ", " << key.second << "]";
102
103 return (os);
104}
105
106std::shared_ptr<interface_span>
107interface_span::find_or_add(const interface_span& temp)
108{
109 return (m_db.find_or_add(
110 make_pair(temp.m_itf_from->key(), temp.m_itf_to->key()), temp));
111}
112
113std::shared_ptr<interface_span>
114interface_span::singular() const
115{
116 return find_or_add(*this);
117}
118
119interface_span::event_handler::event_handler()
120{
121 OM::register_listener(this);
122 inspect::register_handler({ "itf-span" }, "interface span configurations",
123 this);
124}
125
126void
127interface_span::event_handler::handle_replay()
128{
129 m_db.replay();
130}
131
132void
133interface_span::event_handler::handle_populate(const client_db::key_t& key)
134{
135 std::shared_ptr<interface_span::dump_cmd> cmd(new interface_span::dump_cmd());
136
137 HW::enqueue(cmd);
138 HW::write();
139
140 for (auto& record : *cmd) {
141 auto& payload = record.get_payload();
142
143 std::shared_ptr<interface> itf_from =
144 interface::find(payload.sw_if_index_from);
145 std::shared_ptr<interface> itf_to = interface::find(payload.sw_if_index_to);
146
147 interface_span itf_span(*itf_from, *itf_to,
148 state_t::from_int(payload.state));
149
150 VOM_LOG(log_level_t::DEBUG) << "span-dump: " << itf_from->to_string()
151 << itf_to->to_string()
152 << state_t::from_int(payload.state).to_string();
153
154 /*
155 * Write each of the discovered interfaces into the OM,
156 * but disable the HW Command q whilst we do, so that no
157 * commands are sent to VPP
158 */
159 OM::commit(key, itf_span);
160 }
161}
162
163dependency_t
164interface_span::event_handler::order() const
165{
166 return (dependency_t::BINDING);
167}
168
169void
170interface_span::event_handler::show(std::ostream& os)
171{
172 m_db.dump(os);
173}
174
175const interface_span::state_t interface_span::state_t::DISABLED(0, "disable");
176const interface_span::state_t interface_span::state_t::RX_ENABLED(1,
177 "rx-enable");
178const interface_span::state_t interface_span::state_t::TX_ENABLED(2,
179 "tx-enable");
180const interface_span::state_t interface_span::state_t::TX_RX_ENABLED(
181 3,
182 "tx-rx-enable");
183
184interface_span::state_t::state_t(int v, const std::string& s)
185 : enum_base<interface_span::state_t>(v, s)
186{
187}
188
189interface_span::state_t
190interface_span::state_t::from_int(uint8_t i)
191{
192 switch (i) {
193 case 0:
194 return interface_span::state_t::DISABLED;
195 break;
196 case 1:
197 return interface_span::state_t::RX_ENABLED;
198 break;
199 case 2:
200 return interface_span::state_t::TX_ENABLED;
201 break;
202 case 3:
203 default:
204 break;
205 }
206
207 return interface_span::state_t::TX_RX_ENABLED;
208}
209}
210
211/*
212 * fd.io coding-style-patch-verification: ON
213 *
214 * Local Variables:
215 * eval: (c-set-style "mozilla")
216 * End:
217 */