DHCP Client Dump
[vpp.git] / extras / vom / vom / dhcp_client_cmds.cpp
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
18 DEFINE_VAPI_MSG_IDS_DHCP_API_JSON;
19
20 namespace VOM {
21 namespace dhcp_client_cmds {
22
23 bind_cmd::bind_cmd(HW::item<bool>& item,
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
43 bind_cmd::issue(connection& con)
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   m_hw_item.set(wait());
67
68   return rc_t::OK;
69 }
70
71 std::string
72 bind_cmd::to_string() const
73 {
74   std::ostringstream s;
75   s << "Dhcp-client-bind: " << m_hw_item.to_string()
76     << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
77
78   return (s.str());
79 }
80
81 unbind_cmd::unbind_cmd(HW::item<bool>& item,
82                        const handle_t& itf,
83                        const std::string& hostname)
84   : rpc_cmd(item)
85   , m_itf(itf)
86   , m_hostname(hostname)
87 {
88 }
89
90 bool
91 unbind_cmd::operator==(const unbind_cmd& other) const
92 {
93   return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
94 }
95
96 rc_t
97 unbind_cmd::issue(connection& con)
98 {
99   msg_t req(con.ctx(), std::ref(*this));
100
101   auto& payload = req.get_request().get_payload();
102   payload.is_add = 0;
103   payload.client.sw_if_index = m_itf.value();
104   payload.client.pid = getpid();
105   payload.client.want_dhcp_event = 0;
106
107   memcpy(payload.client.hostname, m_hostname.c_str(),
108          std::min(sizeof(payload.client.hostname), m_hostname.length()));
109
110   VAPI_CALL(req.execute());
111
112   wait();
113   m_hw_item.set(rc_t::NOOP);
114
115   return rc_t::OK;
116 }
117
118 std::string
119 unbind_cmd::to_string() const
120 {
121   std::ostringstream s;
122   s << "Dhcp-client-unbind: " << m_hw_item.to_string()
123     << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
124
125   return (s.str());
126 }
127
128 events_cmd::events_cmd(dhcp_client::event_listener& el)
129   : event_cmd(el.status())
130   , m_listener(el)
131 {
132 }
133
134 events_cmd::~events_cmd()
135 {
136   VOM_LOG(log_level_t::INFO) << "DHCP events destroyed";
137 }
138
139 bool
140 events_cmd::operator==(const events_cmd& other) const
141 {
142   return (true);
143 }
144
145 rc_t
146 events_cmd::issue(connection& con)
147 {
148   /*
149    * Set the call back to handle DHCP complete envets.
150    */
151   m_reg.reset(new reg_t(con.ctx(), std::ref(*this)));
152
153   /*
154    * return in-progress so the command stays in the pending list.
155    */
156   return (rc_t::OK);
157 }
158
159 void
160 events_cmd::retire(connection& con)
161 {
162 }
163
164 void
165 events_cmd::notify()
166 {
167   for (auto& msg : *this) {
168     auto& payload = msg.get_payload();
169
170     const dhcp_client::state_t& s =
171       dhcp_client::state_t::from_vpp(payload.lease.state);
172     route::prefix_t pfx(payload.lease.is_ipv6, payload.lease.host_address,
173                         payload.lease.mask_width);
174     std::shared_ptr<interface> itf = interface::find(payload.lease.sw_if_index);
175
176     if (itf) {
177       std::shared_ptr<dhcp_client::lease_t> ev =
178         std::make_shared<dhcp_client::lease_t>(
179           s, itf, from_bytes(0, payload.lease.router_address), pfx,
180           reinterpret_cast<const char*>(payload.lease.hostname),
181           mac_address_t(payload.lease.host_mac));
182       m_listener.handle_dhcp_event(ev);
183
184       VOM_LOG(log_level_t::INFO) << "DHCP: " << ev->to_string();
185     } else {
186       VOM_LOG(log_level_t::ERROR) << "DHCP: no interface: "
187                                   << payload.lease.sw_if_index;
188     }
189   }
190
191   flush();
192 }
193
194 std::string
195 events_cmd::to_string() const
196 {
197   return ("dhcp-events");
198 }
199
200 dump_cmd::dump_cmd()
201 {
202 }
203
204 bool
205 dump_cmd::operator==(const dump_cmd& other) const
206 {
207   return (true);
208 }
209
210 rc_t
211 dump_cmd::issue(connection& con)
212 {
213   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
214
215   VAPI_CALL(m_dump->execute());
216
217   wait();
218
219   return rc_t::OK;
220 }
221
222 std::string
223 dump_cmd::to_string() const
224 {
225   return ("dhcp-client-dump");
226 }
227
228 }; // namespace dhcp_client_cmds
229 }; // namespace VOM
230
231 /*
232  * fd.io coding-style-patch-verification: ON
233  *
234  * Local Variables:
235  * eval: (c-set-style "mozilla")
236  * End:
237  */