FD.io VPP  v18.11-rc0-18-g2a3fb1a
Vector Packet Processing
interface_span.cpp
Go to the documentation of this file.
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"
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 
29  const interface& itf_to,
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 
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 
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) {
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) {
75  m_config, m_itf_from->handle(), m_itf_to->handle(), m_state));
76  }
77 }
78 
79 std::string
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) {
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>
116 {
117  return find_or_add(*this);
118 }
119 
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 
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 
179  "rx-enable");
181  "tx-enable");
183  3,
184  "tx-rx-enable");
185 
186 interface_span::state_t::state_t(int v, const std::string& s)
188 {
189 }
190 
193 {
194  switch (i) {
195  case 0:
197  break;
198  case 1:
200  break;
201  case 2:
203  break;
204  case 3:
205  default:
206  break;
207  }
208 
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  */
#define VOM_LOG(lvl)
Definition: logger.hpp:181
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
A template base class for all enum types.
Definition: enum_base.hpp:30
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
static std::shared_ptr< interface > find(const handle_t &h)
The the singular instance of the interface in the DB by handle.
Definition: interface.cpp:461
static void register_handler(const std::vector< std::string > &cmds, const std::string &help, command_handler *ch)
Register a command handler for inspection.
Definition: inspect.cpp:85
static const state_t TX_ENABLED
TX enable state.
int i
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:236
static const log_level_t DEBUG
Definition: logger.hpp:32
static const state_t TX_RX_ENABLED
TX and RX enable state.
std::shared_ptr< interface_span > singular() const
Return the &#39;singular instance&#39; of the interface_span that matches this object.
A command class that configures the interface span.
A cmd class that Unconfigs interface span.
std::string to_string() const
convert to string format for debug purposes
vhost_vring_state_t state
Definition: vhost_user.h:115
#define v
Definition: acl.c:491
static const state_t DISABLED
DISABLED state.
static void dump(std::ostream &os)
Dump all interface_spans into the stream provided.
A representation of an interface in VPP.
Definition: interface.hpp:41
std::pair< interface::key_t, interface::key_t > key_t
The key type for interface_spans.
static rc_t commit(const client_db::key_t &key, const OBJ &obj)
Make the State in VPP reflect the expressed desired state.
Definition: om.hpp:202
void event_handler(void *tls_async)
Definition: tls_async.c:311
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:194
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
Then L2/objects that bind to interfaces, BD, ACLS, etc.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A representation of a method call to VPP.
Definition: cmd.hpp:32
~interface_span()
Destructor.
static state_t from_int(uint8_t val)
Convert VPP&#39;s numerical value to enum type.
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
interface_span(const interface &itf_from, const interface &itf_to, state_t state)
Construct a new object matching the desried state.
static const state_t RX_ENABLED
RX enable state.
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
A representation of interface span configuration.
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
The state of the interface - rx/tx or both to be mirrored.