FD.io VPP  v20.05.1-6-gf53edbc3b
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  } else {
229  VOM_LOG(log_level_t::ERROR) << "no interface:" << payload.sw_if_index;
230  }
231  }
232 }
233 
235 acl_ethertype::event_handler::order() const
236 {
237  return (dependency_t::BINDING);
238 }
239 
240 void
241 acl_ethertype::event_handler::show(std::ostream& os)
242 {
243  db_dump(m_db, os);
244 }
245 };
246 };
247 /*
248  * fd.io coding-style-patch-verification: ON
249  *
250  * Local Variables:
251  * eval: (c-set-style "mozilla")
252  * End:
253  */
u8 count
Definition: dhcp.api:208
std::string to_string() const
convert to string format for debug purposes
#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.
std::string to_string() const
convert to string
ethertype_rule_t(const ethertype_t &eth, const direction_t &dir)
Constructor.
std::shared_ptr< acl_ethertype > singular() const
Return the &#39;singular&#39; of the acl ethertype that matches this object.
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:538
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
bool operator<(const ethertype_rule_t &other) const
comparision operator
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
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:167
static const direction_t INPUT
Permit Direction.
Definition: types.hpp:151
const direction_t & getDirection(void) const
get the direction
int value() const
Return the value of the enum - same as integer conversion.
Definition: enum_base.hpp:88
std::string key_t
The KEY can be used to uniquely identify the ACL ethertype.
Feature Directions.
Definition: types.hpp:136
A command class that binds the ethertype list to the interface.
const key_t & key() const
Return the binding&#39;s key.
An ACL ethertype list comprises a set of inbound ether types and out bound ether types to be applied ...
A Database to store the unique &#39;singular&#39; instances of a single object type.
Definition: singular_db.hpp:33
bool operator==(const acl_ethertype &o) const
comparision operator (for testing)
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.
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
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 (for testing)
A type declaration of an interface handle in VPP.
Definition: types.hpp:233
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
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:334
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:212
acl_ethertype(const interface &itf, const ethertype_rules_t &le)
Construct a new object matching the desried state.
static const log_level_t ERROR
Definition: logger.hpp:29
uint16_t getEthertype(void) const
get the ether value
Then L2/objects that bind to interfaces, BD, ACLS, etc.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
~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:156
save_rewrite_length must be aligned so that reass doesn t overwrite it
Definition: buffer.h:401
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127