FD.io VPP  v19.04.4-rc0-5-ge88582fac
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 
19 
20 namespace VOM {
21 namespace dhcp_client_cmds {
22 
24  const handle_t& itf,
25  const std::string& hostname,
26  const l2_address_t& client_id,
27  bool set_broadcast_flag)
28  : rpc_cmd(item)
29  , m_itf(itf)
30  , m_hostname(hostname)
31  , m_client_id(client_id)
32  , m_set_broadcast_flag(set_broadcast_flag)
33 {
34 }
35 
36 bool
37 bind_cmd::operator==(const bind_cmd& other) const
38 {
39  return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
40 }
41 
42 rc_t
44 {
45  msg_t req(con.ctx(), std::ref(*this));
46 
47  auto& payload = req.get_request().get_payload();
48  payload.is_add = 1;
49  payload.client.sw_if_index = m_itf.value();
50  payload.client.pid = getpid();
51  payload.client.want_dhcp_event = 1;
52  payload.client.set_broadcast_flag = m_set_broadcast_flag;
53 
54  memset(payload.client.hostname, 0, sizeof(payload.client.hostname));
55  memcpy(payload.client.hostname, m_hostname.c_str(),
56  std::min(sizeof(payload.client.hostname), m_hostname.length()));
57 
58  memset(payload.client.id, 0, sizeof(payload.client.id));
59  payload.client.id[0] = 1;
60  std::copy_n(begin(m_client_id.bytes),
61  std::min(sizeof(payload.client.id), m_client_id.bytes.size()),
62  payload.client.id + 1);
63 
64  VAPI_CALL(req.execute());
65 
66  return (wait());
67 }
68 
69 std::string
71 {
72  std::ostringstream s;
73  s << "Dhcp-client-bind: " << m_hw_item.to_string()
74  << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
75 
76  return (s.str());
77 }
78 
80  const handle_t& itf,
81  const std::string& hostname)
82  : rpc_cmd(item)
83  , m_itf(itf)
84  , m_hostname(hostname)
85 {
86 }
87 
88 bool
90 {
91  return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
92 }
93 
94 rc_t
96 {
97  msg_t req(con.ctx(), std::ref(*this));
98 
99  auto& payload = req.get_request().get_payload();
100  payload.is_add = 0;
101  payload.client.sw_if_index = m_itf.value();
102  payload.client.pid = getpid();
103  payload.client.want_dhcp_event = 0;
104 
105  memcpy(payload.client.hostname, m_hostname.c_str(),
106  std::min(sizeof(payload.client.hostname), m_hostname.length()));
107 
108  VAPI_CALL(req.execute());
109 
110  wait();
112 
113  return rc_t::OK;
114 }
115 
116 std::string
118 {
119  std::ostringstream s;
120  s << "Dhcp-client-unbind: " << m_hw_item.to_string()
121  << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
122 
123  return (s.str());
124 }
125 
127  : event_cmd(el.status())
128  , m_listener(el)
129 {
130 }
131 
133 {
134  VOM_LOG(log_level_t::INFO) << "DHCP events destroyed";
135 }
136 
137 bool
139 {
140  return (true);
141 }
142 
143 rc_t
145 {
146  /*
147  * Set the call back to handle DHCP complete envets.
148  */
149  m_reg.reset(new reg_t(con.ctx(), std::ref(*this)));
150 
151  /*
152  * return in-progress so the command stays in the pending list.
153  */
154  return (rc_t::OK);
155 }
156 
157 void
159 {
160 }
161 
162 void
164 {
165  for (auto& msg : *this) {
166  auto& payload = msg.get_payload();
167 
168  const dhcp_client::state_t& s =
169  dhcp_client::state_t::from_vpp(payload.lease.state);
170  route::prefix_t pfx(payload.lease.is_ipv6, payload.lease.host_address,
171  payload.lease.mask_width);
172  std::shared_ptr<interface> itf = interface::find(payload.lease.sw_if_index);
173 
174  if (itf) {
175  std::shared_ptr<dhcp_client::lease_t> ev =
176  std::make_shared<dhcp_client::lease_t>(
177  s, itf, from_bytes(0, payload.lease.router_address), pfx,
178  reinterpret_cast<const char*>(payload.lease.hostname),
179  mac_address_t(payload.lease.host_mac));
180  m_listener.handle_dhcp_event(ev);
181 
182  VOM_LOG(log_level_t::INFO) << "DHCP: " << ev->to_string();
183  } else {
184  VOM_LOG(log_level_t::ERROR) << "DHCP: no interface: "
185  << payload.lease.sw_if_index;
186  }
187  }
188 
189  flush();
190 }
191 
192 std::string
194 {
195  return ("dhcp-events");
196 }
197 
199 {
200 }
201 
202 bool
203 dump_cmd::operator==(const dump_cmd& other) const
204 {
205  return (true);
206 }
207 
208 rc_t
210 {
211  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
212 
213  VAPI_CALL(m_dump->execute());
214 
215  wait();
216 
217  return rc_t::OK;
218 }
219 
220 std::string
222 {
223  return ("dhcp-client-dump");
224 }
225 
226 }; // namespace dhcp_client_cmds
227 }; // namespace VOM
228 
229 /*
230  * fd.io coding-style-patch-verification: ON
231  *
232  * Local Variables:
233  * eval: (c-set-style "mozilla")
234  * End:
235  */
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:531
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
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.
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
bool operator==(const events_cmd &i) const
Comparison operator - only used for UT.
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
bind_cmd(HW::item< bool > &item, const handle_t &itf, const std::string &hostname, const l2_address_t &client_id, bool set_braodcast_flag=false)
Constructor.
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:31
A type declaration of an interface handle in VPP.
Definition: types.hpp:233
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:193
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:92
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