FD.io VPP  v20.01-48-g3e0dafb74
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 
73 std::string
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 
122 std::string
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 
199 std::string
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 
227 std::string
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: ON
238  *
239  * Local Variables:
240  * eval: (c-set-style "mozilla")
241  * End:
242  */
string hostname[64]
Definition: dhcp.api:159
static const rc_t NOOP
The HW write/update action was/has not been attempted.
Definition: types.hpp:104
std::string to_string() const
convert to string format for debug purposes
#define VOM_LOG(lvl)
Definition: logger.hpp:181
void retire(connection &con)
Retire the command - unsubscribe.
uint32_t value() const
get the value of the handle
Definition: types.cpp:93
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
A functor class represents our desire to recieve interface events.
bool operator==(const unbind_cmd &i) const
Comparison operator - only used for UT.
Error codes that VPP will return during a HW write.
Definition: types.hpp:89
std::string to_string() const
convert to string format for debug purposes
void flush()
flush/free all the events thus far reeived.
Definition: event_cmd.hpp:75
A class that listens to DHCP Events.
Definition: dhcp_client.hpp:83
vapi_enum_ip_neighbor_flags to_api(const neighbour::flags_t &f)
Definition: api_types.cpp:21
Type def of a L2 address as read from VPP.
Definition: types.hpp:339
rc_t wait()
Wait on the commands promise.
Definition: rpc_cmd.hpp:82
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
void notify()
called in the VAPI RX thread when data is available.
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.
virtual void handle_dhcp_event(std::shared_ptr< lease_t > e)=0
listener&#39;s virtual function invoked when a DHCP event is available to read
A representation of the connection to VPP.
Definition: connection.hpp:33
vl_api_ip_dscp_t dscp
Definition: dhcp.api:163
bool operator==(const events_cmd &i) const
Comparison operator - only used for UT.
bool set_broadcast_flag
Definition: dhcp.api:162
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:40
std::vector< uint8_t > bytes
Underlying bytes array - filled from least to most significant.
Definition: types.hpp:383
#define VAPI_CALL(_stmt)
Convenince wrapper macro for error handling in VAPI sends.
Definition: types.hpp:29
IP DSCP values.
Definition: prefix.hpp:81
static const log_level_t INFO
Definition: logger.hpp:31
std::string to_string() const
convert to string format for debug purposes
Definition: types.cpp:69
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
std::unique_ptr< vapi::Event_registration< vapi::Dhcp_compl_event > > m_reg
The VAPI event registration.
Definition: event_cmd.hpp:100
unbind_cmd(HW::item< bool > &item, const handle_t &itf, const std::string &hostname)
Constructor.
static const state_t & from_vpp(int i)
Definition: dhcp_client.cpp:32
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
struct mac_address_t_ mac_address_t
vapi::Event_registration< vapi::Dhcp_compl_event > reg_t
Definition: event_cmd.hpp:59
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:109
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
static const log_level_t ERROR
Definition: logger.hpp:29
vapi::Connection & ctx()
Retrun the VAPI context the commands will use.
Definition: connection.cpp:49
events_cmd(dhcp_client::event_listener &el)
Constructor.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
rc_t issue(connection &con)
Issue the command to VPP/HW.
std::string to_string() const
convert to string format for debug purposes
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
rc_t issue(connection &con)
Issue the command to VPP/HW.
rc_t issue(connection &con)
Issue the command to VPP/HW - subscribe to DHCP events.
An Event command base class.
Definition: event_cmd.hpp:39
DEFINE_VAPI_MSG_IDS_DHCP_API_JSON
rc_t issue(connection &con)
Issue the command to VPP/HW.
A prefix defintion.
Definition: prefix.hpp:131
A cmd class that Unbinds Dhcp Config from an interface.
HW::item< bool > & m_hw_item
A reference to an object&#39;s HW::item that the command will update.
Definition: rpc_cmd.hpp:134
bool operator==(const bind_cmd &i) const
Comparison operator - only used for UT.
A command class that binds the DHCP config to the interface.
A cmd class that Dumps all the DHCP clients.
HW::item< bool > & item()
return the HW item the command updates
Definition: rpc_cmd.hpp:66
vapi::Dhcp_client_config msg_t
convenient typedef
Definition: rpc_cmd.hpp:46