FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
dhcp_client_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/dhcp_client_cmds.hpp"
17 #include "vom/route_api_types.hpp"
18 
20 
21 namespace VOM {
22 namespace dhcp_client_cmds {
23 
25  const handle_t& itf,
26  const std::string& hostname,
27  const l2_address_t& client_id,
28  bool set_broadcast_flag,
29  const ip_dscp_t& dscp)
30  : rpc_cmd(item)
31  , m_itf(itf)
32  , m_hostname(hostname)
33  , m_client_id(client_id)
34  , m_set_broadcast_flag(set_broadcast_flag)
35  , m_dscp(dscp)
36 {
37 }
38 
39 bool
40 bind_cmd::operator==(const bind_cmd& other) const
41 {
42  return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
43 }
44 
45 rc_t
47 {
48  msg_t req(con.ctx(), std::ref(*this));
49 
50  auto& payload = req.get_request().get_payload();
51  payload.is_add = 1;
52  payload.client.sw_if_index = m_itf.value();
53  payload.client.pid = getpid();
54  payload.client.want_dhcp_event = 1;
55  payload.client.set_broadcast_flag = m_set_broadcast_flag;
56  payload.client.dscp = to_api(m_dscp);
57 
58  memset(payload.client.hostname, 0, sizeof(payload.client.hostname));
59  memcpy(payload.client.hostname, m_hostname.c_str(),
60  std::min(sizeof(payload.client.hostname), m_hostname.length()));
61 
62  memset(payload.client.id, 0, sizeof(payload.client.id));
63  payload.client.id[0] = 1;
64  std::copy_n(begin(m_client_id.bytes),
65  std::min(sizeof(payload.client.id), m_client_id.bytes.size()),
66  payload.client.id + 1);
67 
68  VAPI_CALL(req.execute());
69 
70  return (wait());
71 }
72 
75 {
76  std::ostringstream s;
77  s << "Dhcp-client-bind: " << m_hw_item.to_string()
78  << " itf:" << m_itf.to_string() << " hostname:" << m_hostname
79  << " client_id:[" << m_client_id << "] "
80  << "dscp:" << m_dscp.to_string();
81 
82  return (s.str());
83 }
84 
86  const handle_t& itf,
87  const std::string& hostname)
88  : rpc_cmd(item)
89  , m_itf(itf)
90  , m_hostname(hostname)
91 {
92 }
93 
94 bool
96 {
97  return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
98 }
99 
100 rc_t
102 {
103  msg_t req(con.ctx(), std::ref(*this));
104 
105  auto& payload = req.get_request().get_payload();
106  payload.is_add = 0;
107  payload.client.sw_if_index = m_itf.value();
108  payload.client.pid = getpid();
109  payload.client.want_dhcp_event = 0;
110 
111  memcpy(payload.client.hostname, m_hostname.c_str(),
112  std::min(sizeof(payload.client.hostname), m_hostname.length()));
113 
114  VAPI_CALL(req.execute());
115 
116  wait();
118 
119  return rc_t::OK;
120 }
121 
124 {
125  std::ostringstream s;
126  s << "Dhcp-client-unbind: " << m_hw_item.to_string()
127  << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
128 
129  return (s.str());
130 }
131 
133  : event_cmd(el.status())
134  , m_listener(el)
135 {
136 }
137 
139 {
140  VOM_LOG(log_level_t::INFO) << "DHCP events destroyed";
141 }
142 
143 bool
145 {
146  return (true);
147 }
148 
149 rc_t
151 {
152  /*
153  * Set the call back to handle DHCP complete envets.
154  */
155  m_reg.reset(new reg_t(con.ctx(), std::ref(*this)));
156 
157  /*
158  * return in-progress so the command stays in the pending list.
159  */
160  return (rc_t::OK);
161 }
162 
163 void
165 {
166 }
167 
168 void
170 {
171  for (auto& msg : *this) {
172  auto& payload = msg.get_payload();
173 
174  const dhcp_client::state_t& s =
175  dhcp_client::state_t::from_vpp(payload.lease.state);
176  route::prefix_t pfx(payload.lease.is_ipv6,
177  (uint8_t*)&payload.lease.host_address.un,
178  payload.lease.mask_width);
179  std::shared_ptr<interface> itf = interface::find(payload.lease.sw_if_index);
180 
181  if (itf) {
182  std::shared_ptr<dhcp_client::lease_t> ev =
183  std::make_shared<dhcp_client::lease_t>(
184  s, itf, from_bytes(0, (uint8_t*)&payload.lease.router_address.un),
185  pfx, reinterpret_cast<const char*>(payload.lease.hostname),
186  mac_address_t(payload.lease.host_mac));
187  m_listener.handle_dhcp_event(ev);
188 
189  VOM_LOG(log_level_t::INFO) << "DHCP: " << ev->to_string();
190  } else {
191  VOM_LOG(log_level_t::ERROR) << "DHCP: no interface: "
192  << payload.lease.sw_if_index;
193  }
194  }
195 
196  flush();
197 }
198 
201 {
202  return ("dhcp-events");
203 }
204 
206 {
207 }
208 
209 bool
210 dump_cmd::operator==(const dump_cmd& other) const
211 {
212  return (true);
213 }
214 
215 rc_t
217 {
218  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
219 
220  VAPI_CALL(m_dump->execute());
221 
222  wait();
223 
224  return rc_t::OK;
225 }
226 
229 {
230  return ("dhcp-client-dump");
231 }
232 
233 }; // namespace dhcp_client_cmds
234 }; // namespace VOM
235 
236 /*
237  * fd.io coding-style-patch-verification: OFF
238  *
239  * Local Variables:
240  * eval: (c-set-style "mozilla")
241  * End:
242  */
VOM::route::prefix_t
A prefix defintion.
Definition: prefix.hpp:131
VOM::HW::item< bool >
VOM::dump_cmd< vapi::Dhcp_client_dump >::msg_t
vapi::Dhcp_client_dump msg_t
Definition: dump_cmd.hpp:46
VOM::dhcp_client_cmds::dump_cmd
A cmd class that Dumps all the DHCP clients.
Definition: dhcp_client_cmds.hpp:172
VOM::rpc_cmd< HW::item< bool >, vapi::Dhcp_client_config >::wait
rc_t wait()
Wait on the commands promise.
Definition: rpc_cmd.hpp:82
VOM::HW::item::set
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
VOM_LOG
#define VOM_LOG(lvl)
Definition: logger.hpp:181
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::l2_address_t::bytes
std::vector< uint8_t > bytes
Underlying bytes array - filled from least to most significant.
Definition: types.hpp:383
VOM::rc_t::OK
const static rc_t OK
The HW write was successfull.
Definition: types.hpp:109
VOM::event_cmd< vapi::Control_ping, vapi::Dhcp_compl_event >::reg_t
vapi::Event_registration< vapi::Dhcp_compl_event > reg_t
Definition: event_cmd.hpp:59
VOM::log_level_t::INFO
const static log_level_t INFO
Definition: logger.hpp:31
VOM::handle_t::value
uint32_t value() const
get the value of the handle
Definition: types.cpp:93
VOM::dhcp_client_cmds::events_cmd::retire
void retire(connection &con)
Retire the command - unsubscribe.
Definition: dhcp_client_cmds.cpp:164
VOM::rpc_cmd
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:40
VOM::dhcp_client_cmds::events_cmd::notify
void notify()
called in the VAPI RX thread when data is available.
Definition: dhcp_client_cmds.cpp:169
VOM::from_bytes
boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
Definition: prefix.cpp:224
con
Connection con
Definition: vapi_cpp_test.cpp:56
VOM::dhcp_client_cmds::events_cmd
A functor class represents our desire to recieve interface events.
Definition: dhcp_client_cmds.hpp:128
VOM::dhcp_client_cmds::bind_cmd
A command class that binds the DHCP config to the interface.
Definition: dhcp_client_cmds.hpp:32
VOM::dhcp_client_cmds::dump_cmd::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: dhcp_client_cmds.cpp:228
VOM::rpc_cmd< HW::item< bool >, vapi::Dhcp_client_config >::m_hw_item
HW::item< bool > & m_hw_item
A reference to an object's HW::item that the command will update.
Definition: rpc_cmd.hpp:134
VOM::dump_cmd< vapi::Dhcp_client_dump >::m_dump
std::unique_ptr< vapi::Dhcp_client_dump > m_dump
The VAPI event registration.
Definition: dump_cmd.hpp:143
VOM::dhcp_client_cmds::events_cmd::~events_cmd
~events_cmd()
Definition: dhcp_client_cmds.cpp:138
VOM::dhcp_client_cmds::dump_cmd::dump_cmd
dump_cmd()
Constructor.
Definition: dhcp_client_cmds.cpp:205
route_api_types.hpp
hostname
string hostname[64]
Definition: dhcp.api:159
VOM::dhcp_client::state_t
Definition: dhcp_client.hpp:42
VAPI_CALL
#define VAPI_CALL(_stmt)
Convenince wrapper macro for error handling in VAPI sends.
Definition: types.hpp:29
DEFINE_VAPI_MSG_IDS_DHCP_API_JSON
DEFINE_VAPI_MSG_IDS_DHCP_API_JSON
Definition: dhcp_client_cmds.cpp:19
VOM::event_cmd
An Event command base class.
Definition: event_cmd.hpp:39
VOM::connection
A representation of the connection to VPP.
Definition: connection.hpp:33
VOM::dhcp_client::event_listener::handle_dhcp_event
virtual void handle_dhcp_event(std::shared_ptr< lease_t > e)=0
listener's virtual function invoked when a DHCP event is available to read
VOM::dump_cmd< vapi::Dhcp_client_dump >::wait
rc_t wait()
Wait for the issue of the command to complete.
Definition: dump_cmd.hpp:93
VOM::rc_t::NOOP
const static rc_t NOOP
The HW write/update action was/has not been attempted.
Definition: types.hpp:104
VOM::dhcp_client_cmds::events_cmd::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: dhcp_client_cmds.cpp:200
VOM::dhcp_client_cmds::events_cmd::operator==
bool operator==(const events_cmd &i) const
Comparison operator - only used for UT.
Definition: dhcp_client_cmds.cpp:144
VOM::handle_t
A type declaration of an interface handle in VPP.
Definition: types.hpp:233
VOM::dhcp_client::state_t::from_vpp
static const state_t & from_vpp(int i)
Definition: dhcp_client.cpp:32
VOM::HW::item::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
VOM::dhcp_client_cmds::bind_cmd::bind_cmd
bind_cmd(HW::item< bool > &item, const handle_t &itf, const std::string &hostname, const l2_address_t &client_id, bool set_braodcast_flag, const ip_dscp_t &dscp)
Constructor.
Definition: dhcp_client_cmds.cpp:24
VOM::dhcp_client::event_listener
A class that listens to DHCP Events.
Definition: dhcp_client.hpp:83
VOM::dhcp_client_cmds::unbind_cmd
A cmd class that Unbinds Dhcp Config from an interface.
Definition: dhcp_client_cmds.hpp:89
VOM::event_cmd< vapi::Control_ping, vapi::Dhcp_compl_event >::m_reg
std::unique_ptr< vapi::Event_registration< vapi::Dhcp_compl_event > > m_reg
The VAPI event registration.
Definition: event_cmd.hpp:100
VOM::to_api
vapi_enum_ip_neighbor_flags to_api(const neighbour::flags_t &f)
Definition: api_types.cpp:21
VOM::dhcp_client_cmds::unbind_cmd::unbind_cmd
unbind_cmd(HW::item< bool > &item, const handle_t &itf, const std::string &hostname)
Constructor.
Definition: dhcp_client_cmds.cpp:85
VOM::rpc_cmd< HW::item< bool >, vapi::Dhcp_client_config >::msg_t
vapi::Dhcp_client_config msg_t
convenient typedef
Definition: rpc_cmd.hpp:46
VOM::enum_base::to_string
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
VOM::l2_address_t
Type def of a L2 address as read from VPP.
Definition: types.hpp:339
VOM::handle_t::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: types.cpp:69
VOM::event_cmd< vapi::Control_ping, vapi::Dhcp_compl_event >::flush
void flush()
flush/free all the events thus far reeived.
Definition: event_cmd.hpp:75
VOM::mac_address_t
Type def of a Ethernet address.
Definition: types.hpp:295
VOM::ip_dscp_t
IP DSCP values.
Definition: prefix.hpp:81
VOM::dhcp_client_cmds::bind_cmd::issue
rc_t issue(connection &con)
Issue the command to VPP/HW.
Definition: dhcp_client_cmds.cpp:46
VOM::dhcp_client_cmds::bind_cmd::operator==
bool operator==(const bind_cmd &i) const
Comparison operator - only used for UT.
Definition: dhcp_client_cmds.cpp:40
item
cJSON * item
Definition: cJSON.h:222
VOM::dhcp_client_cmds::unbind_cmd::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: dhcp_client_cmds.cpp:123
VOM::dhcp_client_cmds::bind_cmd::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: dhcp_client_cmds.cpp:74
dhcp_client_cmds.hpp
VOM::dhcp_client_cmds::dump_cmd::operator==
bool operator==(const dump_cmd &i) const
Comparison operator - only used for UT.
Definition: dhcp_client_cmds.cpp:210
set_broadcast_flag
bool set_broadcast_flag
Definition: dhcp.api:162
VOM::dhcp_client_cmds::events_cmd::events_cmd
events_cmd(dhcp_client::event_listener &el)
Constructor.
Definition: dhcp_client_cmds.cpp:132
VOM::rc_t
Error codes that VPP will return during a HW write.
Definition: types.hpp:89
VOM::log_level_t::ERROR
const static log_level_t ERROR
Definition: logger.hpp:29
VOM::dhcp_client_cmds::events_cmd::issue
rc_t issue(connection &con)
Issue the command to VPP/HW - subscribe to DHCP events.
Definition: dhcp_client_cmds.cpp:150
dscp
vl_api_ip_dscp_t dscp
Definition: dhcp.api:163
VOM::dhcp_client_cmds::unbind_cmd::operator==
bool operator==(const unbind_cmd &i) const
Comparison operator - only used for UT.
Definition: dhcp_client_cmds.cpp:95
VOM::dhcp_client_cmds::unbind_cmd::issue
rc_t issue(connection &con)
Issue the command to VPP/HW.
Definition: dhcp_client_cmds.cpp:101
VOM::interface::find
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
VOM::dhcp_client_cmds::dump_cmd::issue
rc_t issue(connection &con)
Issue the command to VPP/HW.
Definition: dhcp_client_cmds.cpp:216
string
const char *const string
Definition: cJSON.h:172