FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
interface_cmds.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_cmds.hpp"
17 #include "vom/cmd.hpp"
18 
23 
24 namespace VOM {
25 namespace interface_cmds {
27  const std::string& name)
28  : create_cmd(item, name)
29 {
30 }
31 
32 rc_t
34 {
35  msg_t req(con.ctx(), std::ref(*this));
36 
37  VAPI_CALL(req.execute());
38 
39  wait();
40 
41  if (m_hw_item.rc() == rc_t::OK) {
43  }
44 
45  return rc_t::OK;
46 }
47 std::string
49 {
50  std::ostringstream s;
51  s << "loopback-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
52 
53  return (s.str());
54 }
55 
57  const std::string& name)
58  : create_cmd(item, name)
59 {
60 }
61 
62 rc_t
64 {
65  msg_t req(con.ctx(), std::ref(*this));
66 
67  auto& payload = req.get_request().get_payload();
68 
69  payload.use_random_hw_addr = 1;
70  memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
71  memcpy(payload.host_if_name, m_name.c_str(),
72  std::min(m_name.length(), sizeof(payload.host_if_name)));
73 
74  VAPI_CALL(req.execute());
75 
76  wait();
77 
78  if (m_hw_item.rc() == rc_t::OK) {
80  }
81 
82  return rc_t::OK;
83 }
84 std::string
86 {
87  std::ostringstream s;
88  s << "af-packet-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
89 
90  return (s.str());
91 }
92 
94  const std::string& name,
95  const std::string& tag)
96  : create_cmd(item, name)
97  , m_tag(tag)
98 {
99 }
100 
101 rc_t
103 {
104  msg_t req(con.ctx(), std::ref(*this));
105 
106  auto& payload = req.get_request().get_payload();
107  memset(payload.sock_filename, 0, sizeof(payload.sock_filename));
108  memcpy(payload.sock_filename, m_name.c_str(),
109  std::min(m_name.length(), sizeof(payload.sock_filename)));
110  memset(payload.tag, 0, sizeof(payload.tag));
111 
112  if (!m_tag.empty())
113  memcpy(payload.tag, m_tag.c_str(),
114  std::min(m_tag.length(), sizeof(payload.tag)));
115 
116  payload.is_server = 0;
117  payload.use_custom_mac = 0;
118  payload.renumber = 0;
119 
120  VAPI_CALL(req.execute());
121 
122  wait();
123 
124  if (m_hw_item.rc() == rc_t::OK) {
126  }
127 
128  return rc_t::OK;
129 }
130 
131 std::string
133 {
134  std::ostringstream s;
135  s << "vhost-intf-create: " << m_hw_item.to_string() << " name:" << m_name
136  << " tag:" << m_tag;
137 
138  return (s.str());
139 }
140 
142  : delete_cmd(item)
143 {
144 }
145 
146 rc_t
148 {
149  msg_t req(con.ctx(), std::ref(*this));
150 
151  auto& payload = req.get_request().get_payload();
152  payload.sw_if_index = m_hw_item.data().value();
153 
154  VAPI_CALL(req.execute());
155 
156  wait();
158 
160  return rc_t::OK;
161 }
162 
163 std::string
165 {
166  std::ostringstream s;
167  s << "loopback-itf-delete: " << m_hw_item.to_string();
168 
169  return (s.str());
170 }
171 
173  const std::string& name)
174  : delete_cmd(item, name)
175 {
176 }
177 
178 rc_t
180 {
181  msg_t req(con.ctx(), std::ref(*this));
182 
183  auto& payload = req.get_request().get_payload();
184  memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
185  memcpy(payload.host_if_name, m_name.c_str(),
186  std::min(m_name.length(), sizeof(payload.host_if_name)));
187 
188  VAPI_CALL(req.execute());
189 
190  wait();
192 
194  return rc_t::OK;
195 }
196 std::string
198 {
199  std::ostringstream s;
200  s << "af_packet-itf-delete: " << m_hw_item.to_string();
201 
202  return (s.str());
203 }
204 
206  const std::string& name)
207  : delete_cmd(item, name)
208 {
209 }
210 
211 rc_t
213 {
214  msg_t req(con.ctx(), std::ref(*this));
215 
216  auto& payload = req.get_request().get_payload();
217  payload.sw_if_index = m_hw_item.data().value();
218 
219  VAPI_CALL(req.execute());
220 
221  wait();
223 
224  return rc_t::OK;
225 }
226 std::string
228 {
229  std::ostringstream s;
230  s << "vhost-itf-delete: " << m_hw_item.to_string() << " name:" << m_name;
231 
232  return (s.str());
233 }
234 
236  const HW::item<handle_t>& hdl)
237  : rpc_cmd(state)
238  , m_hdl(hdl)
239 {
240 }
241 
242 bool
244 {
245  return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
246 }
247 
248 rc_t
250 {
251  msg_t req(con.ctx(), std::ref(*this));
252 
253  auto& payload = req.get_request().get_payload();
254  payload.sw_if_index = m_hdl.data().value();
255  payload.admin_up_down = m_hw_item.data().value();
256 
257  VAPI_CALL(req.execute());
258 
259  return (wait());
260 }
261 
262 std::string
264 {
265  std::ostringstream s;
266  s << "itf-state-change: " << m_hw_item.to_string()
267  << " hdl:" << m_hdl.to_string();
268  return (s.str());
269 }
270 
272  const l3_proto_t& proto,
273  const HW::item<handle_t>& hdl)
274  : rpc_cmd(table)
275  , m_hdl(hdl)
276  , m_proto(proto)
277 {
278 }
279 
280 bool
282 {
283  return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
284  (m_hw_item == other.m_hw_item));
285 }
286 
287 rc_t
289 {
290  msg_t req(con.ctx(), std::ref(*this));
291 
292  auto& payload = req.get_request().get_payload();
293  payload.sw_if_index = m_hdl.data().value();
294  payload.is_ipv6 = m_proto.is_ipv6();
295  payload.vrf_id = m_hw_item.data();
296 
297  VAPI_CALL(req.execute());
298 
299  return (wait());
300 }
301 
302 std::string
304 {
305  std::ostringstream s;
306  s << "itf-set-table: " << m_hw_item.to_string()
307  << " proto:" << m_proto.to_string() << " hdl:" << m_hdl.to_string();
308  return (s.str());
309 }
310 
312  const HW::item<handle_t>& hdl)
313  : rpc_cmd(mac)
314  , m_hdl(hdl)
315 {
316 }
317 
318 bool
320 {
321  return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
322 }
323 
324 rc_t
326 {
327  msg_t req(con.ctx(), std::ref(*this));
328 
329  auto& payload = req.get_request().get_payload();
330  payload.sw_if_index = m_hdl.data().value();
331  m_hw_item.data().to_mac().to_bytes(payload.mac_address,
332  sizeof(payload.mac_address));
333 
334  VAPI_CALL(req.execute());
335 
336  return (wait());
337 }
338 
339 std::string
341 {
342  std::ostringstream s;
343  s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
344  return (s.str());
345 }
346 
349  const handle_t& hdl,
350  bool enable)
351  : rpc_cmd(item)
352  , m_hdl(hdl)
353  , m_enable(enable)
354 {
355 }
356 
357 bool
359  const collect_detail_stats_change_cmd& other) const
360 {
361  return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item) &&
362  (m_enable == other.m_enable));
363 }
364 
365 rc_t
367 {
368  msg_t req(con.ctx(), std::ref(*this));
369 
370  auto& payload = req.get_request().get_payload();
371  payload.sw_if_index = m_hdl.value();
372  payload.enable_disable = m_enable;
373 
374  VAPI_CALL(req.execute());
375 
376  return (wait());
377 }
378 
379 std::string
381 {
382  std::ostringstream s;
383  s << "itf-stats: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
384  return (s.str());
385 }
386 
388  : event_cmd(el.status())
389  , m_listener(el)
390 {
391 }
392 
393 bool
395 {
396  return (true);
397 }
398 
399 rc_t
401 {
402  /*
403  * First set the call back to handle the interface events
404  */
405  m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
406 
407  /*
408  * then send the request to enable them
409  */
410  msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
411 
412  auto& payload = req.get_request().get_payload();
413  payload.enable_disable = 1;
414  payload.pid = getpid();
415 
416  VAPI_CALL(req.execute());
417 
418  wait();
419 
420  return (rc_t::OK);
421 }
422 
423 void
425 {
426  /*
427  * disable interface events.
428  */
429  msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
430 
431  auto& payload = req.get_request().get_payload();
432  payload.enable_disable = 0;
433  payload.pid = getpid();
434 
435  VAPI_CALL(req.execute());
436 
437  wait();
438 }
439 
440 void
442 {
443  std::lock_guard<interface_cmds::events_cmd> lg(*this);
444  std::vector<interface::event> events;
445 
446  for (auto& msg : *this) {
447  auto& payload = msg.get_payload();
448 
449  handle_t handle(payload.sw_if_index);
450  std::shared_ptr<interface> sp = interface::find(handle);
451 
452  if (sp) {
453  interface::oper_state_t oper_state =
454  interface::oper_state_t::from_int(payload.link_up_down);
455 
456  VOM_LOG(log_level_t::DEBUG) << "Interface Event: " << sp->to_string()
457  << " state: " << oper_state.to_string();
458 
459  sp->set(oper_state);
460  events.push_back({ *sp, oper_state });
461  }
462  }
463 
464  flush();
465 
466  m_listener.handle_interface_event(events);
467 }
468 
469 std::string
471 {
472  return ("itf-events");
473 }
474 
476 {
477 }
478 
479 bool
480 dump_cmd::operator==(const dump_cmd& other) const
481 {
482  return (true);
483 }
484 
485 rc_t
487 {
488  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
489 
490  auto& payload = m_dump->get_request().get_payload();
491  payload.name_filter_valid = 0;
492 
493  VAPI_CALL(m_dump->execute());
494 
495  wait();
496 
497  return rc_t::OK;
498 }
499 
500 std::string
502 {
503  return ("itf-dump");
504 }
505 
507 {
508 }
509 
510 bool
512 {
513  return (true);
514 }
515 
516 rc_t
518 {
519  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
520 
521  VAPI_CALL(m_dump->execute());
522 
523  wait();
524 
525  return rc_t::OK;
526 }
527 
528 std::string
530 {
531  return ("vhost-itf-dump");
532 }
533 
534 bool
536 {
537  return (true);
538 }
539 
540 rc_t
542 {
543  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
544 
545  VAPI_CALL(m_dump->execute());
546 
547  wait();
548 
549  return rc_t::OK;
550 }
551 
552 std::string
554 {
555  return ("af-packet-itf-dump");
556 }
557 
559  : rpc_cmd(item)
560  , m_name(name)
561 {
562 }
563 
564 rc_t
566 {
567  msg_t req(con.ctx(), std::ref(*this));
568 
569  auto& payload = req.get_request().get_payload();
570  payload.is_add = 1;
571  payload.sw_if_index = m_hw_item.data().value();
572  memset(payload.tag, 0, sizeof(payload.tag));
573  memcpy(payload.tag, m_name.c_str(), m_name.length());
574 
575  VAPI_CALL(req.execute());
576 
577  return (wait());
578 }
579 std::string
581 {
582  std::ostringstream s;
583  s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;
584 
585  return (s.str());
586 }
587 
588 bool
590 {
591  return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
592 }
593 }; // namespace interface_cmds
594 }; // namespace VOM
595 
596 /*
597  * fd.io coding-style-patch-verification: ON
598  *
599  * Local Variables:
600  * eval: (c-set-style "mozilla")
601  * End:
602  */
rc_t issue(connection &con)
Issue the command to VPP/HW.
std::string to_string() const
convert to string format for debug purposes
bool operator==(const set_mac_cmd &i) const
Comparison operator - only used for UT.
static const rc_t NOOP
The HW write/update action was/has not been attempted.
Definition: types.hpp:104
void retire(connection &con)
Retires the command - unsubscribe from the events.
delete_cmd(HW::item< handle_t > &item, const std::string &name)
Definition: interface.hpp:371
vhost_create_cmd(HW::item< handle_t > &item, const std::string &name, const std::string &tag)
DEFINE_VAPI_MSG_IDS_AF_PACKET_API_JSON
#define VOM_LOG(lvl)
Definition: logger.hpp:181
std::string to_string() const
convert to string format for debug purposes
uint32_t value() const
get the value of the handle
Definition: types.cpp:93
void remove_interface()
remove the deleted interface from the DB
Definition: interface.hpp:404
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:527
set_tag(HW::item< handle_t > &item, const std::string &name)
Constructor taking the HW::item to update.
rc_t issue(connection &con)
Issue the command to VPP/HW.
A cmd class that Dumps all the Vpp interfaces.
collect_detail_stats_change_cmd(HW::item< interface::stats_type_t > &item, const handle_t &h, bool enable)
Constructor taking the HW::item to update and the handle of the interface.
std::string to_string() const
convert to string format for debug purposes
rc_t issue(connection &con)
Issue the command to VPP/HW.
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:119
A command class to set tag on interfaces.
Error codes that VPP will return during a HW write.
Definition: types.hpp:89
static const log_level_t DEBUG
Definition: logger.hpp:32
An L3 protocol can be used to construct a prefix that is used to match packets are part of a route...
Definition: prefix.hpp:52
set_mac_cmd(HW::item< l2_address_t > &item, const HW::item< handle_t > &h)
Constructor taking the HW::item to update and the handle of the interface.
void flush()
flush/free all the events thus far reeived.
Definition: event_cmd.hpp:75
create_cmd(HW::item< handle_t > &item, const std::string &name)
Definition: interface.hpp:304
A command class that enables detailed stats collection on an interface.
void to_bytes(uint8_t *array, uint8_t len) const
Convert to byte array.
Definition: types.cpp:140
int value() const
Return the value of the enum - same as integer conversion.
Definition: enum_base.hpp:67
The oper state of the interface.
Definition: interface.hpp:164
rc_t wait()
Wait on the commands promise.
Definition: rpc_cmd.hpp:82
T & data()
Return the data read/written.
Definition: hw.hpp:109
bool operator==(const set_table_cmd &i) const
Comparison operator - only used for UT.
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
bool operator==(const events_cmd &i) const
Comparison operator - only used for UT.
DEFINE_VAPI_MSG_IDS_VPE_API_JSON
rc_t issue(connection &con)
Issue the command to VPP/HW.
vhost_vring_state_t state
Definition: vhost_user.h:120
rc_t issue(connection &con)
Issue the command to VPP/HW.
A command class that changes the MAC address on an interface.
const std::string & m_name
The name of the interface to be created.
Definition: interface.hpp:361
dump_cmd()
Default Constructor.
A representation of the connection to VPP.
Definition: connection.hpp:33
std::string to_string() const
convert to string format for debug purposes
set_table_cmd(HW::item< route::table_id_t > &item, const l3_proto_t &proto, const HW::item< handle_t > &h)
Constructor taking the HW::item to update and the name handle of the interface whose table is to chan...
DEFINE_VAPI_MSG_IDS_VHOST_USER_API_JSON
rc_t issue(connection &con)
Issue the command to VPP/HW.
state_change_cmd(HW::item< interface::admin_state_t > &s, const HW::item< handle_t > &h)
Constructor taking the HW::item to update and the name handle of the interface whose state is to chan...
rc_t issue(connection &con)
Issue the command to VPP/HW.
std::string to_string() const
convert to string format for debug purposes
std::string to_string() const
convert to string format for debug purposes
rc_t issue(connection &con)
Issue the command to VPP/HW.
A class that listens to interface Events.
Definition: interface.hpp:428
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:40
mac_address_t to_mac() const
MAC address conversion.
Definition: types.cpp:216
af_packet_delete_cmd(HW::item< handle_t > &item, const std::string &name)
Constructor taking the HW::item to update and the name of the interface to delete.
std::string to_string() const
convert to string format for debug purposes
bool operator==(const af_packet_dump_cmd &i) const
Comparison operator - only used for UT.
u8 name[64]
Definition: memclnt.api:152
#define VAPI_CALL(_stmt)
Convenince wrapper macro for error handling in VAPI sends.
Definition: types.hpp:29
bool operator==(const state_change_cmd &i) const
Comparison operator - only used for UT.
A cmd class that changes the admin state.
vhost_dump_cmd()
Default Constructor.
af_packet_create_cmd(HW::item< handle_t > &item, const std::string &name)
Constructor taking the HW::item to update and the name of the interface to create.
A command class that binds an interface to an L3 table.
A command class represents our desire to recieve interface events.
std::string to_string() const
convert to string format for debug purposes
Definition: types.cpp:69
std::unique_ptr< vapi::Event_registration< vapi::Sw_interface_event > > m_reg
The VAPI event registration.
Definition: event_cmd.hpp:100
A cmd class that Dumps all the Vpp Interfaces.
rc_t issue(connection &con)
Issue the command to VPP/HW.
bool operator==(const vhost_dump_cmd &i) const
Comparison operator - only used for UT.
std::string to_string() const
convert to string format for debug purposes
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
vapi::Event_registration< vapi::Sw_interface_event > reg_t
Definition: event_cmd.hpp:59
rc_t issue(connection &con)
Issue the command to VPP/HW.
rc_t issue(connection &con)
Issue the command to VPP/HW.
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:109
void notify()
Called when it&#39;s time to poke the listeners.
bool operator==(const dump_cmd &i) const
Comparison operator - only used for UT.
std::string to_string() const
convert to string format for debug purposes
const std::string m_name
The name of the interface to be created.
Definition: interface.hpp:410
loopback_delete_cmd(HW::item< handle_t > &item)
Constructor taking the HW::item to update.
static oper_state_t from_int(uint8_t val)
Convert VPP&#39;s numerical value to enum type.
vapi::Connection & ctx()
Retrun the VAPI context the commands will use.
Definition: connection.cpp:49
rc_t issue(connection &con)
Issue the command to VPP/HW.
std::string to_string() const
convert to string format for debug purposes
void insert_interface()
add the created interface to the DB
Definition: interface.hpp:335
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON
virtual void handle_interface_event(std::vector< event > es)=0
Virtual function called on the listener when the command has data ready to process.
rc_t issue(connection &con)
Issue the command to VPP/HW.
void set(const rc_t &rc)
Set the HW return code - should only be called from the family of Command objects.
Definition: hw.hpp:125
vhost_delete_cmd(HW::item< handle_t > &item, const std::string &name)
An Event command base class.
Definition: event_cmd.hpp:39
std::string to_string() const
convert to string format for debug purposes
rc_t issue(connection &con)
Issue the command to VPP/HW.
std::string to_string() const
convert to string format for debug purposes
events_cmd(interface::event_listener &el)
Constructor taking the listner to notify.
A cmd class that Dumps all the Vpp interfaces.
bool operator==(const set_tag &i) const
Comparison operator - only used for UT.
loopback_create_cmd(HW::item< handle_t > &item, const std::string &name)
Constructor taking the HW::item to update and the name of the interface to create.
std::string to_string() const
convert to string format for debug purposes
vl_api_mac_address_t mac
Definition: gbp.api:118
HW::item< handle_t > & m_hw_item
A reference to an object&#39;s HW::item that the command will update.
Definition: rpc_cmd.hpp:134
bool is_ipv6()
Definition: prefix.cpp:35
HW::item< handle_t > & item()
return the HW item the command updates
Definition: rpc_cmd.hpp:66
rc_t issue(connection &con)
Issue the command to VPP/HW.
vapi::Create_loopback msg_t
convenient typedef
Definition: rpc_cmd.hpp:46
std::string to_string() const
convert to string format for debug purposes
bool operator==(const collect_detail_stats_change_cmd &i) const
Comparison operator - only used for UT.
std::string to_string() const
convert to string format for debug purposes