dhcp ip: DSCP settings for transmitted DHCP packets
[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 #include "vom/route_api_types.hpp"
18
19 DEFINE_VAPI_MSG_IDS_DHCP_API_JSON;
20
21 namespace VOM {
22 namespace dhcp_client_cmds {
23
24 bind_cmd::bind_cmd(HW::item<bool>& item,
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
46 bind_cmd::issue(connection& con)
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
74 bind_cmd::to_string() const
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
85 unbind_cmd::unbind_cmd(HW::item<bool>& item,
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
95 unbind_cmd::operator==(const unbind_cmd& other) const
96 {
97   return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
98 }
99
100 rc_t
101 unbind_cmd::issue(connection& con)
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();
117   m_hw_item.set(rc_t::NOOP);
118
119   return rc_t::OK;
120 }
121
122 std::string
123 unbind_cmd::to_string() const
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
132 events_cmd::events_cmd(dhcp_client::event_listener& el)
133   : event_cmd(el.status())
134   , m_listener(el)
135 {
136 }
137
138 events_cmd::~events_cmd()
139 {
140   VOM_LOG(log_level_t::INFO) << "DHCP events destroyed";
141 }
142
143 bool
144 events_cmd::operator==(const events_cmd& other) const
145 {
146   return (true);
147 }
148
149 rc_t
150 events_cmd::issue(connection& con)
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
164 events_cmd::retire(connection& con)
165 {
166 }
167
168 void
169 events_cmd::notify()
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, payload.lease.host_address,
177                         payload.lease.mask_width);
178     std::shared_ptr<interface> itf = interface::find(payload.lease.sw_if_index);
179
180     if (itf) {
181       std::shared_ptr<dhcp_client::lease_t> ev =
182         std::make_shared<dhcp_client::lease_t>(
183           s, itf, from_bytes(0, payload.lease.router_address), pfx,
184           reinterpret_cast<const char*>(payload.lease.hostname),
185           mac_address_t(payload.lease.host_mac));
186       m_listener.handle_dhcp_event(ev);
187
188       VOM_LOG(log_level_t::INFO) << "DHCP: " << ev->to_string();
189     } else {
190       VOM_LOG(log_level_t::ERROR) << "DHCP: no interface: "
191                                   << payload.lease.sw_if_index;
192     }
193   }
194
195   flush();
196 }
197
198 std::string
199 events_cmd::to_string() const
200 {
201   return ("dhcp-events");
202 }
203
204 dump_cmd::dump_cmd()
205 {
206 }
207
208 bool
209 dump_cmd::operator==(const dump_cmd& other) const
210 {
211   return (true);
212 }
213
214 rc_t
215 dump_cmd::issue(connection& con)
216 {
217   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
218
219   VAPI_CALL(m_dump->execute());
220
221   wait();
222
223   return rc_t::OK;
224 }
225
226 std::string
227 dump_cmd::to_string() const
228 {
229   return ("dhcp-client-dump");
230 }
231
232 }; // namespace dhcp_client_cmds
233 }; // namespace VOM
234
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "mozilla")
240  * End:
241  */