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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_INTERFACE_SPAN_H__
17 #define __VOM_INTERFACE_SPAN_H__
19 #include "vom/dump_cmd.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/interface.hpp"
23 #include "vom/object_base.hpp"
25 #include "vom/rpc_cmd.hpp"
26 #include "vom/singular_db.hpp"
28 #include <vapi/span.api.vapi.hpp>
32 * A representation of interface span configuration
34 class interface_span : public object_base
38 * The state of the interface - rx/tx or both to be mirrored
40 struct state_t : enum_base<state_t>
45 const static state_t DISABLED;
49 const static state_t RX_ENABLED;
53 const static state_t TX_ENABLED;
55 * TX and RX enable state
57 const static state_t TX_RX_ENABLED;
60 * Convert VPP's numerical value to enum type
62 static state_t from_int(uint8_t val);
66 * Private constructor taking the value and the string name
68 state_t(int v, const std::string& s);
72 * Construct a new object matching the desried state
74 * @param itf_from - The interface to be mirrored
75 * @param itf_to - The interface where the traffic is mirrored
77 interface_span(const interface& itf_from,
78 const interface& itf_to,
84 interface_span(const interface_span& o);
92 * Return the 'singular instance' of the interface_span that matches
95 std::shared_ptr<interface_span> singular() const;
98 * convert to string format for debug purposes
100 std::string to_string() const;
103 * Dump all interface_spans into the stream provided
105 static void dump(std::ostream& os);
108 * The key type for interface_spans
110 typedef std::pair<interface::key_type, interface::key_type> key_type_t;
113 * Find a singular instance in the DB for the interface passed
115 static std::shared_ptr<interface_span> find(const interface& i);
118 * A command class that configures the interface span
120 class config_cmd : public rpc_cmd<HW::item<bool>,
122 vapi::Sw_interface_span_enable_disable>
128 config_cmd(HW::item<bool>& item,
129 const handle_t& itf_from,
130 const handle_t& itf_to,
131 const state_t& state);
134 * Issue the command to VPP/HW
136 rc_t issue(connection& con);
138 * convert to string format for debug purposes
140 std::string to_string() const;
143 * Comparison operator - only used for UT
145 bool operator==(const config_cmd& i) const;
149 * Reference to the interface to be mirrored
151 const handle_t& m_itf_from;
153 * Reference to the interface where the traffic is mirrored
155 const handle_t& m_itf_to;
157 * the state (rx, tx or both) of the interface to be mirrored
159 const state_t& m_state;
163 * A cmd class that Unconfigs interface span
165 class unconfig_cmd : public rpc_cmd<HW::item<bool>,
167 vapi::Sw_interface_span_enable_disable>
173 unconfig_cmd(HW::item<bool>& item,
174 const handle_t& itf_from,
175 const handle_t& itf_to);
178 * Issue the command to VPP/HW
180 rc_t issue(connection& con);
182 * convert to string format for debug purposes
184 std::string to_string() const;
187 * Comparison operator - only used for UT
189 bool operator==(const unconfig_cmd& i) const;
193 * Reference to the interface to be mirrored
195 const handle_t& m_itf_from;
197 * Reference to the interface where the traffic is mirrored
199 const handle_t& m_itf_to;
203 * A cmd class that Dumps all the interface spans
205 class dump_cmd : public VOM::dump_cmd<vapi::Sw_interface_span_dump>
212 dump_cmd(const dump_cmd& d);
215 * Issue the command to VPP/HW
217 rc_t issue(connection& con);
219 * convert to string format for debug purposes
221 std::string to_string() const;
224 * Comparison operator - only used for UT
226 bool operator==(const dump_cmd& i) const;
237 * Class definition for listeners to OM events
239 class event_handler : public OM::listener, public inspect::command_handler
243 virtual ~event_handler() = default;
246 * Handle a populate event
248 void handle_populate(const client_db::key_t& key);
251 * Handle a replay event
253 void handle_replay();
256 * Show the object in the Singular DB
258 void show(std::ostream& os);
261 * Get the sortable Id of the listener
263 dependency_t order() const;
267 * event_handler to register with OM
269 static event_handler m_evh;
272 * Enquue commonds to the VPP command Q for the update
274 void update(const interface_span& obj);
277 * Find or add the singular instance in the DB
279 static std::shared_ptr<interface_span> find_or_add(
280 const interface_span& temp);
283 * It's the VPPHW class that updates the objects in HW
288 e* It's the singular_db class that calls replay()
290 friend class singular_db<key_type_t, interface_span>;
293 * Sweep/reap the object if still stale
298 * replay the object to create it in hardware
303 * A reference counting pointer the interface to be mirrored
305 const std::shared_ptr<interface> m_itf_from;
307 * A reference counting pointer the interface where the traffic is
310 const std::shared_ptr<interface> m_itf_to;
313 * the state (rx, tx or both) of the interface to be mirrored
315 const state_t m_state;
318 * HW configuration for the binding. The bool representing the
321 HW::item<bool> m_config;
324 * A map of all interface span keyed against the interface to be
327 static singular_db<key_type_t, interface_span> m_db;
331 * Ostream output for the key
333 std::ostream& operator<<(std::ostream& os,
334 const interface_span::key_type_t& key);
338 * fd.io coding-style-patch-verification: ON
341 * eval: (c-set-style "mozilla")