FD.io VPP  v19.04.4-rc0-5-ge88582fac
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 {
26 
28  const std::string& name)
29  : create_cmd(item, name)
30 {
31 }
32 
33 rc_t
35 {
36  msg_t req(con.ctx(), std::ref(*this));
37 
38  auto& payload = req.get_request().get_payload();
39 
40  payload.user_instance = ~0;
41 
42  VAPI_CALL(req.execute());
43 
44  wait();
45 
46  if (m_hw_item.rc() == rc_t::OK) {
48  }
49 
50  return rc_t::OK;
51 }
52 std::string
54 {
55  std::ostringstream s;
56  s << "bvi-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
57 
58  return (s.str());
59 }
60 
62  const std::string& name)
63  : create_cmd(item, name)
64 {
65 }
66 
67 rc_t
69 {
70  msg_t req(con.ctx(), std::ref(*this));
71 
72  VAPI_CALL(req.execute());
73 
74  wait();
75 
76  if (m_hw_item.rc() == rc_t::OK) {
78  }
79 
80  return rc_t::OK;
81 }
82 std::string
84 {
85  std::ostringstream s;
86  s << "loopback-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
87 
88  return (s.str());
89 }
90 
92  const std::string& name)
93  : create_cmd(item, name)
94 {
95 }
96 
97 rc_t
99 {
100  msg_t req(con.ctx(), std::ref(*this));
101 
102  auto& payload = req.get_request().get_payload();
103 
104  payload.use_random_hw_addr = 1;
105  memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
106  memcpy(payload.host_if_name, m_name.c_str(),
107  std::min(m_name.length(), sizeof(payload.host_if_name)));
108 
109  VAPI_CALL(req.execute());
110 
111  wait();
112 
113  if (m_hw_item.rc() == rc_t::OK) {
115  }
116 
117  return rc_t::OK;
118 }
119 std::string
121 {
122  std::ostringstream s;
123  s << "af-packet-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
124 
125  return (s.str());
126 }
127 
129  const std::string& name,
130  const std::string& tag)
131  : create_cmd(item, name)
132  , m_tag(tag)
133 {
134 }
135 
136 rc_t
138 {
139  msg_t req(con.ctx(), std::ref(*this));
140 
141  auto& payload = req.get_request().get_payload();
142  memset(payload.sock_filename, 0, sizeof(payload.sock_filename));
143  memcpy(payload.sock_filename, m_name.c_str(),
144  std::min(m_name.length(), sizeof(payload.sock_filename)));
145  memset(payload.tag, 0, sizeof(payload.tag));
146 
147  if (!m_tag.empty())
148  memcpy(payload.tag, m_tag.c_str(),
149  std::min(m_tag.length(), sizeof(payload.tag)));
150 
151  payload.is_server = 0;
152  payload.use_custom_mac = 0;
153  payload.renumber = 0;
154 
155  VAPI_CALL(req.execute());
156 
157  wait();
158 
159  if (m_hw_item.rc() == rc_t::OK) {
161  }
162 
163  return rc_t::OK;
164 }
165 
166 std::string
168 {
169  std::ostringstream s;
170  s << "vhost-intf-create: " << m_hw_item.to_string() << " name:" << m_name
171  << " tag:" << m_tag;
172 
173  return (s.str());
174 }
175 
177  : delete_cmd(item)
178 {
179 }
180 
181 rc_t
183 {
184  msg_t req(con.ctx(), std::ref(*this));
185 
186  auto& payload = req.get_request().get_payload();
187  payload.sw_if_index = m_hw_item.data().value();
188 
189  VAPI_CALL(req.execute());
190 
191  wait();
193 
195  return rc_t::OK;
196 }
197 
198 std::string
200 {
201  std::ostringstream s;
202  s << "bvi-itf-delete: " << m_hw_item.to_string();
203 
204  return (s.str());
205 }
206 
208  : delete_cmd(item)
209 {
210 }
211 
212 rc_t
214 {
215  msg_t req(con.ctx(), std::ref(*this));
216 
217  auto& payload = req.get_request().get_payload();
218  payload.sw_if_index = m_hw_item.data().value();
219 
220  VAPI_CALL(req.execute());
221 
222  wait();
224 
226  return rc_t::OK;
227 }
228 
229 std::string
231 {
232  std::ostringstream s;
233  s << "loopback-itf-delete: " << m_hw_item.to_string();
234 
235  return (s.str());
236 }
237 
239  const std::string& name)
240  : delete_cmd(item, name)
241 {
242 }
243 
244 rc_t
246 {
247  msg_t req(con.ctx(), std::ref(*this));
248 
249  auto& payload = req.get_request().get_payload();
250  memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
251  memcpy(payload.host_if_name, m_name.c_str(),
252  std::min(m_name.length(), sizeof(payload.host_if_name)));
253 
254  VAPI_CALL(req.execute());
255 
256  wait();
258 
260  return rc_t::OK;
261 }
262 std::string
264 {
265  std::ostringstream s;
266  s << "af_packet-itf-delete: " << m_hw_item.to_string();
267 
268  return (s.str());
269 }
270 
272  const std::string& name)
273  : delete_cmd(item, name)
274 {
275 }
276 
277 rc_t
279 {
280  msg_t req(con.ctx(), std::ref(*this));
281 
282  auto& payload = req.get_request().get_payload();
283  payload.sw_if_index = m_hw_item.data().value();
284 
285  VAPI_CALL(req.execute());
286 
287  wait();
289 
290  return rc_t::OK;
291 }
292 std::string
294 {
295  std::ostringstream s;
296  s << "vhost-itf-delete: " << m_hw_item.to_string() << " name:" << m_name;
297 
298  return (s.str());
299 }
300 
302  const HW::item<handle_t>& hdl)
303  : rpc_cmd(state)
304  , m_hdl(hdl)
305 {
306 }
307 
308 bool
310 {
311  return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
312 }
313 
314 rc_t
316 {
317  msg_t req(con.ctx(), std::ref(*this));
318 
319  auto& payload = req.get_request().get_payload();
320  payload.sw_if_index = m_hdl.data().value();
321  payload.admin_up_down = m_hw_item.data().value();
322 
323  VAPI_CALL(req.execute());
324 
325  return (wait());
326 }
327 
328 std::string
330 {
331  std::ostringstream s;
332  s << "itf-state-change: " << m_hw_item.to_string()
333  << " hdl:" << m_hdl.to_string();
334  return (s.str());
335 }
336 
338  const l3_proto_t& proto,
339  const HW::item<handle_t>& hdl)
340  : rpc_cmd(table)
341  , m_hdl(hdl)
342  , m_proto(proto)
343 {
344 }
345 
346 bool
348 {
349  return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
350  (m_hw_item == other.m_hw_item));
351 }
352 
353 rc_t
355 {
356  msg_t req(con.ctx(), std::ref(*this));
357 
358  auto& payload = req.get_request().get_payload();
359  payload.sw_if_index = m_hdl.data().value();
360  payload.is_ipv6 = m_proto.is_ipv6();
361  payload.vrf_id = m_hw_item.data();
362 
363  VAPI_CALL(req.execute());
364 
365  return (wait());
366 }
367 
368 std::string
370 {
371  std::ostringstream s;
372  s << "itf-set-table: " << m_hw_item.to_string()
373  << " proto:" << m_proto.to_string() << " hdl:" << m_hdl.to_string();
374  return (s.str());
375 }
376 
378  const HW::item<handle_t>& hdl)
379  : rpc_cmd(mac)
380  , m_hdl(hdl)
381 {
382 }
383 
384 bool
386 {
387  return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
388 }
389 
390 rc_t
392 {
393  msg_t req(con.ctx(), std::ref(*this));
394 
395  auto& payload = req.get_request().get_payload();
396  payload.sw_if_index = m_hdl.data().value();
397  m_hw_item.data().to_mac().to_bytes(payload.mac_address,
398  sizeof(payload.mac_address));
399 
400  VAPI_CALL(req.execute());
401 
402  return (wait());
403 }
404 
405 std::string
407 {
408  std::ostringstream s;
409  s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
410  return (s.str());
411 }
412 
415  const handle_t& hdl,
416  bool enable)
417  : rpc_cmd(item)
418  , m_hdl(hdl)
419  , m_enable(enable)
420 {
421 }
422 
423 bool
425  const collect_detail_stats_change_cmd& other) const
426 {
427  return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item) &&
428  (m_enable == other.m_enable));
429 }
430 
431 rc_t
433 {
434  msg_t req(con.ctx(), std::ref(*this));
435 
436  auto& payload = req.get_request().get_payload();
437  payload.sw_if_index = m_hdl.value();
438  payload.enable_disable = m_enable;
439 
440  VAPI_CALL(req.execute());
441 
442  return (wait());
443 }
444 
445 std::string
447 {
448  std::ostringstream s;
449  s << "itf-stats: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
450  return (s.str());
451 }
452 
454  : event_cmd(el.status())
455  , m_listener(el)
456 {
457 }
458 
459 bool
461 {
462  return (true);
463 }
464 
465 rc_t
467 {
468  /*
469  * First set the call back to handle the interface events
470  */
471  m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
472 
473  /*
474  * then send the request to enable them
475  */
476  msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
477 
478  auto& payload = req.get_request().get_payload();
479  payload.enable_disable = 1;
480  payload.pid = getpid();
481 
482  VAPI_CALL(req.execute());
483 
484  wait();
485 
486  return (rc_t::OK);
487 }
488 
489 void
491 {
492  /*
493  * disable interface events.
494  */
495  msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
496 
497  auto& payload = req.get_request().get_payload();
498  payload.enable_disable = 0;
499  payload.pid = getpid();
500 
501  VAPI_CALL(req.execute());
502 
503  wait();
504 }
505 
506 void
508 {
509  std::lock_guard<interface_cmds::events_cmd> lg(*this);
510  std::vector<interface::event> events;
511 
512  for (auto& msg : *this) {
513  auto& payload = msg.get_payload();
514 
515  handle_t handle(payload.sw_if_index);
516  std::shared_ptr<interface> sp = interface::find(handle);
517 
518  if (sp) {
519  interface::oper_state_t oper_state =
520  interface::oper_state_t::from_int(payload.link_up_down);
521 
522  VOM_LOG(log_level_t::DEBUG) << "Interface Event: " << sp->to_string()
523  << " state: " << oper_state.to_string();
524 
525  sp->set(oper_state);
526  events.push_back({ *sp, oper_state });
527  }
528  }
529 
530  flush();
531 
532  m_listener.handle_interface_event(events);
533 }
534 
535 std::string
537 {
538  return ("itf-events");
539 }
540 
542 {
543 }
544 
545 bool
546 dump_cmd::operator==(const dump_cmd& other) const
547 {
548  return (true);
549 }
550 
551 rc_t
553 {
554  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
555 
556  auto& payload = m_dump->get_request().get_payload();
557  payload.name_filter_valid = 0;
558 
559  VAPI_CALL(m_dump->execute());
560 
561  wait();
562 
563  return rc_t::OK;
564 }
565 
566 std::string
568 {
569  return ("itf-dump");
570 }
571 
573 {
574 }
575 
576 bool
578 {
579  return (true);
580 }
581 
582 rc_t
584 {
585  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
586 
587  VAPI_CALL(m_dump->execute());
588 
589  wait();
590 
591  return rc_t::OK;
592 }
593 
594 std::string
596 {
597  return ("vhost-itf-dump");
598 }
599 
600 bool
602 {
603  return (true);
604 }
605 
606 rc_t
608 {
609  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
610 
611  VAPI_CALL(m_dump->execute());
612 
613  wait();
614 
615  return rc_t::OK;
616 }
617 
618 std::string
620 {
621  return ("af-packet-itf-dump");
622 }
623 
625  : rpc_cmd(item)
626  , m_name(name)
627 {
628 }
629 
630 rc_t
632 {
633  msg_t req(con.ctx(), std::ref(*this));
634 
635  auto& payload = req.get_request().get_payload();
636  payload.is_add = 1;
637  payload.sw_if_index = m_hw_item.data().value();
638  memset(payload.tag, 0, sizeof(payload.tag));
639  memcpy(payload.tag, m_name.c_str(), m_name.length());
640 
641  VAPI_CALL(req.execute());
642 
643  return (wait());
644 }
645 std::string
647 {
648  std::ostringstream s;
649  s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;
650 
651  return (s.str());
652 }
653 
654 bool
656 {
657  return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
658 }
659 }; // namespace interface_cmds
660 }; // namespace VOM
661 
662 /*
663  * fd.io coding-style-patch-verification: ON
664  *
665  * Local Variables:
666  * eval: (c-set-style "mozilla")
667  * End:
668  */
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:374
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
bvi_delete_cmd(HW::item< handle_t > &item)
Constructor taking the HW::item to update.
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:407
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:531
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 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:307
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:88
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:364
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...
std::string to_string() const
convert to string format for debug purposes
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:431
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
std::string to_string() const
convert to string format for debug purposes
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.
bvi_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.
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:413
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:338
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:120
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::Bvi_create msg_t
convenient typedef
Definition: rpc_cmd.hpp:46
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 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