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