blob: 9243f2dd7553c2d8d027842634fe04b13fb065cd [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"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/interface_span_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018
19namespace VOM {
20/**
21 * A DB of all interface_span config
22 */
Neale Rannsfd920602017-11-23 12:15:00 -080023singular_db<interface_span::key_t, interface_span> interface_span::m_db;
Neale Ranns812ed392017-10-16 04:20:13 -070024
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) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070057 HW::enqueue(new interface_span_cmds::unconfig_cmd(
58 m_config, m_itf_from->handle(), m_itf_to->handle()));
Neale Ranns812ed392017-10-16 04:20:13 -070059 }
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) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070073 HW::enqueue(new interface_span_cmds::config_cmd(
74 m_config, m_itf_from->handle(), m_itf_to->handle(), m_state));
Neale Ranns812ed392017-10-16 04:20:13 -070075 }
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) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070093 HW::enqueue(new interface_span_cmds::config_cmd(
94 m_config, m_itf_from->handle(), m_itf_to->handle(), m_state));
Neale Ranns812ed392017-10-16 04:20:13 -070095 }
96}
97
98std::ostream&
Neale Rannsfd920602017-11-23 12:15:00 -080099operator<<(std::ostream& os, const interface_span::key_t& key)
Neale Ranns812ed392017-10-16 04:20:13 -0700100{
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{
Neale Ranns1d781552017-11-27 04:52:35 -0800135 std::shared_ptr<interface_span_cmds::dump_cmd> cmd =
136 std::make_shared<interface_span_cmds::dump_cmd>();
Neale Ranns812ed392017-10-16 04:20:13 -0700137
138 HW::enqueue(cmd);
139 HW::write();
140
141 for (auto& record : *cmd) {
142 auto& payload = record.get_payload();
143
144 std::shared_ptr<interface> itf_from =
145 interface::find(payload.sw_if_index_from);
146 std::shared_ptr<interface> itf_to = interface::find(payload.sw_if_index_to);
147
148 interface_span itf_span(*itf_from, *itf_to,
149 state_t::from_int(payload.state));
150
151 VOM_LOG(log_level_t::DEBUG) << "span-dump: " << itf_from->to_string()
152 << itf_to->to_string()
153 << state_t::from_int(payload.state).to_string();
154
155 /*
156 * Write each of the discovered interfaces into the OM,
157 * but disable the HW Command q whilst we do, so that no
158 * commands are sent to VPP
159 */
160 OM::commit(key, itf_span);
161 }
162}
163
164dependency_t
165interface_span::event_handler::order() const
166{
167 return (dependency_t::BINDING);
168}
169
170void
171interface_span::event_handler::show(std::ostream& os)
172{
173 m_db.dump(os);
174}
175
176const interface_span::state_t interface_span::state_t::DISABLED(0, "disable");
177const interface_span::state_t interface_span::state_t::RX_ENABLED(1,
178 "rx-enable");
179const interface_span::state_t interface_span::state_t::TX_ENABLED(2,
180 "tx-enable");
181const interface_span::state_t interface_span::state_t::TX_RX_ENABLED(
182 3,
183 "tx-rx-enable");
184
185interface_span::state_t::state_t(int v, const std::string& s)
186 : enum_base<interface_span::state_t>(v, s)
187{
188}
189
190interface_span::state_t
191interface_span::state_t::from_int(uint8_t i)
192{
193 switch (i) {
194 case 0:
195 return interface_span::state_t::DISABLED;
196 break;
197 case 1:
198 return interface_span::state_t::RX_ENABLED;
199 break;
200 case 2:
201 return interface_span::state_t::TX_ENABLED;
202 break;
203 case 3:
204 default:
205 break;
206 }
207
208 return interface_span::state_t::TX_RX_ENABLED;
209}
210}
211
212/*
213 * fd.io coding-style-patch-verification: ON
214 *
215 * Local Variables:
216 * eval: (c-set-style "mozilla")
217 * End:
218 */