FD.io VPP  v18.07.1-19-g511ce25
Vector Packet Processing
acl_ethertype.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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/acl_ethertype.hpp"
19 
20 namespace VOM {
21 namespace ACL {
22 
24  const direction_t& dir)
25  : m_eth(eth)
26  , m_dir(dir)
27 {
28 }
29 
30 std::string
32 {
33  std::ostringstream s;
34 
35  s << "["
36  << "ethertype:" << m_eth.to_string() << " dir:" << m_dir.to_string()
37  << "],";
38 
39  return (s.str());
40 }
41 
42 bool
44 {
45  return (m_dir > other.m_dir);
46 }
47 
48 bool
50 {
51  return (m_dir == other.m_dir && m_eth == other.m_eth);
52 }
53 
54 uint16_t
56 {
57  return m_eth.value();
58 }
59 
60 const direction_t&
62 {
63  return m_dir;
64 }
65 
66 /**
67  * A DB of all acl ethertype bindings configs
68  */
70 
71 acl_ethertype::event_handler acl_ethertype::m_evh;
72 
75  : m_itf(itf.singular())
76  , m_le(le)
77  , m_binding(true)
78 {
79 }
80 
82  : m_itf(o.m_itf)
83  , m_le(o.m_le)
84  , m_binding(o.m_binding)
85 {
86 }
87 
89 {
90  sweep();
91 
92  // not in the DB anymore.
93  m_db.release(m_itf->key(), this);
94 }
95 
96 void
97 acl_ethertype::sweep()
98 {
99  if (m_binding) {
100  HW::enqueue(new acl_ethertype_cmds::unbind_cmd(m_binding, m_itf->handle()));
101  }
102  HW::write();
103 }
104 
107 {
108  return (m_itf->key());
109 }
110 
111 bool
113 {
114  return (m_itf->key() == other.m_itf->key() && m_le == other.m_le);
115 }
116 
117 std::shared_ptr<acl_ethertype>
119 {
120  return (m_db.find(key));
121 }
122 
123 void
124 acl_ethertype::dump(std::ostream& os)
125 {
126  db_dump(m_db, os);
127 }
128 
129 void
130 acl_ethertype::replay()
131 {
132  if (m_binding) {
133  HW::enqueue(
134  new acl_ethertype_cmds::bind_cmd(m_binding, m_itf->handle(), m_le));
135  }
136 }
137 
138 std::string
140 {
141  std::ostringstream s;
142  s << "Acl-Ethertype:" << m_itf->to_string() << " ethertype-rules:";
143  auto it = m_le.cbegin();
144  while (it != m_le.cend()) {
145  s << it->to_string();
146  ++it;
147  }
148  s << " rules-size:" << m_le.size();
149 
150  return (s.str());
151 }
152 
153 void
154 acl_ethertype::update(const acl_ethertype& desired)
155 {
156  /*
157  * always update the instance with the latest rules
158  */
159  if (!m_binding || desired.m_le != m_le) {
160  HW::enqueue(
161  new acl_ethertype_cmds::bind_cmd(m_binding, m_itf->handle(), m_le));
162  }
163 
164  m_le = desired.m_le;
165 }
166 
167 std::shared_ptr<acl_ethertype>
168 acl_ethertype::find_or_add(const acl_ethertype& temp)
169 {
170  return (m_db.find_or_add(temp.m_itf->key(), temp));
171 }
172 
173 std::shared_ptr<acl_ethertype>
175 {
176  return find_or_add(*this);
177 }
178 
180 {
181  OM::register_listener(this);
182  inspect::register_handler({ "acl-ethertype" }, "ACL Ethertype bindings",
183  this);
184 }
185 
186 void
187 acl_ethertype::event_handler::handle_replay()
188 {
189  m_db.replay();
190 }
191 
192 void
193 acl_ethertype::event_handler::handle_populate(const client_db::key_t& key)
194 {
195  /*
196  * dump VPP acl ethertypes
197  */
198  std::shared_ptr<acl_ethertype_cmds::dump_cmd> cmd =
199  std::make_shared<acl_ethertype_cmds::dump_cmd>(~0);
200 
201  HW::enqueue(cmd);
202  HW::write();
203 
204  for (auto& record : *cmd) {
205  auto& payload = record.get_payload();
206  handle_t hdl(payload.sw_if_index);
207  std::shared_ptr<interface> itf = interface::find(hdl);
208  uint8_t n_input = payload.n_input;
209  uint8_t count = payload.count;
210  ethertype_rules_t ler;
211  if (itf) {
212  for (int i = 0; i < count; i++) {
213  ethertype_t e = ethertype_t::from_numeric_val(payload.whitelist[i]);
214  if (n_input) {
216  ler.insert(er);
217  n_input--;
218  } else {
220  ler.insert(er);
221  }
222  }
223  if (!ler.empty()) {
224  acl_ethertype a_e(*itf, ler);
225  VOM_LOG(log_level_t::DEBUG) << "ethertype dump: " << a_e.to_string();
226  OM::commit(key, a_e);
227  }
228  }
229  }
230 }
231 
233 acl_ethertype::event_handler::order() const
234 {
235  return (dependency_t::BINDING);
236 }
237 
238 void
239 acl_ethertype::event_handler::show(std::ostream& os)
240 {
241  db_dump(m_db, os);
242 }
243 };
244 };
245 /*
246  * fd.io coding-style-patch-verification: ON
247  *
248  * Local Variables:
249  * eval: (c-set-style "mozilla")
250  * End:
251  */
#define VOM_LOG(lvl)
Definition: logger.hpp:181
std::string to_string() const
convert to string format for debug purposes
int value() const
Return the value of the enum - same as integer conversion.
Definition: enum_base.hpp:67
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
ethertype_rule_t(const ethertype_t &eth, const direction_t &dir)
Constructor.
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
std::shared_ptr< acl_ethertype > singular() const
Return the &#39;singular&#39; of the acl ethertype that matches this object.
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
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 void dump(std::ostream &os)
Dump all acl ethertype into the stream provided.
static const ethertype_t & from_numeric_val(uint16_t numeric)
Get the ethertype from the numeric value.
Definition: types.cpp:300
Feature Ethertype.
Definition: types.hpp:162
static const direction_t INPUT
Permit Direction.
Definition: types.hpp:146
std::string key_t
The KEY can be used to uniquely identify the ACL ethertype.
Feature Directions.
Definition: types.hpp:131
A command class that binds the ethertype list to the interface.
An ACL ethertype list comprises a set of inbound ether types and out bound ether types to be applied ...
const direction_t & getDirection(void) const
get the direction
const key_t & key() const
Return the binding&#39;s key.
bool operator==(const acl_ethertype &o) const
comparision operator (for testing)
uint16_t getEthertype(void) const
get the ether value
A Database to store the unique &#39;singular&#39; instances of a single object type.
Definition: singular_db.hpp:33
static std::shared_ptr< acl_ethertype > find(const key_t &key)
Static function to find the acl_ethertype in the model.
A command class that unbinds the ethertype list to the interface.
std::multiset< ethertype_rule_t > ethertype_rules_t
The ethertype container.
A representation of an interface in VPP.
Definition: interface.hpp:41
bool operator<(const ethertype_rule_t &other) const
comparision operator
A type declaration of an interface handle in VPP.
Definition: types.hpp:228
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
acl_ethertype(const interface &itf, const ethertype_rules_t &le)
Construct a new object matching the desried state.
size_t count
Definition: vapi.c:46
Then L2/objects that bind to interfaces, BD, ACLS, etc.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
bool operator==(const ethertype_rule_t &other) const
comparision operator (for testing)
~acl_ethertype()
Destructor.
A representation of a method call to VPP.
Definition: cmd.hpp:32
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
static const direction_t OUTPUT
Deny Direction.
Definition: types.hpp:151
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
std::string to_string() const
convert to string