Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 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 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 16 | #include "vom/dhcp_config_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 17 | |
| 18 | DEFINE_VAPI_MSG_IDS_DHCP_API_JSON; |
| 19 | |
| 20 | namespace VOM { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 21 | namespace dhcp_config_cmds { |
| 22 | |
| 23 | bind_cmd::bind_cmd(HW::item<bool>& item, |
| 24 | const handle_t& itf, |
| 25 | const std::string& hostname, |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 26 | const l2_address_t& client_id, |
| 27 | bool set_broadcast_flag) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 28 | : rpc_cmd(item) |
| 29 | , m_itf(itf) |
| 30 | , m_hostname(hostname) |
| 31 | , m_client_id(client_id) |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 32 | , m_set_broadcast_flag(set_broadcast_flag) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
| 36 | bool |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 37 | bind_cmd::operator==(const bind_cmd& other) const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 38 | { |
| 39 | return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname)); |
| 40 | } |
| 41 | |
| 42 | rc_t |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 43 | bind_cmd::issue(connection& con) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 44 | { |
| 45 | msg_t req(con.ctx(), std::ref(*this)); |
| 46 | |
| 47 | auto& payload = req.get_request().get_payload(); |
| 48 | payload.sw_if_index = m_itf.value(); |
| 49 | payload.is_add = 1; |
| 50 | payload.pid = getpid(); |
| 51 | payload.want_dhcp_event = 1; |
| 52 | |
Neale Ranns | 814b47b | 2017-11-09 05:18:04 -0800 | [diff] [blame] | 53 | memset(payload.hostname, 0, sizeof(payload.hostname)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 54 | memcpy(payload.hostname, m_hostname.c_str(), |
| 55 | std::min(sizeof(payload.hostname), m_hostname.length())); |
| 56 | |
| 57 | memset(payload.client_id, 0, sizeof(payload.client_id)); |
| 58 | payload.client_id[0] = 1; |
| 59 | std::copy_n(begin(m_client_id.bytes), |
| 60 | std::min(sizeof(payload.client_id), m_client_id.bytes.size()), |
| 61 | payload.client_id + 1); |
| 62 | |
| 63 | VAPI_CALL(req.execute()); |
| 64 | |
| 65 | m_hw_item.set(wait()); |
| 66 | |
| 67 | return rc_t::OK; |
| 68 | } |
| 69 | |
| 70 | std::string |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 71 | bind_cmd::to_string() const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 72 | { |
| 73 | std::ostringstream s; |
| 74 | s << "Dhcp-config-bind: " << m_hw_item.to_string() |
| 75 | << " itf:" << m_itf.to_string() << " hostname:" << m_hostname; |
| 76 | |
| 77 | return (s.str()); |
| 78 | } |
| 79 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 80 | unbind_cmd::unbind_cmd(HW::item<bool>& item, |
| 81 | const handle_t& itf, |
| 82 | const std::string& hostname) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 83 | : rpc_cmd(item) |
| 84 | , m_itf(itf) |
| 85 | , m_hostname(hostname) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | bool |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 90 | unbind_cmd::operator==(const unbind_cmd& other) const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 91 | { |
| 92 | return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname)); |
| 93 | } |
| 94 | |
| 95 | rc_t |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 96 | unbind_cmd::issue(connection& con) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 97 | { |
| 98 | msg_t req(con.ctx(), std::ref(*this)); |
| 99 | |
| 100 | auto& payload = req.get_request().get_payload(); |
| 101 | payload.sw_if_index = m_itf.value(); |
| 102 | payload.is_add = 0; |
| 103 | payload.pid = getpid(); |
| 104 | payload.want_dhcp_event = 0; |
| 105 | |
| 106 | memcpy(payload.hostname, m_hostname.c_str(), |
| 107 | std::min(sizeof(payload.hostname), m_hostname.length())); |
| 108 | |
| 109 | VAPI_CALL(req.execute()); |
| 110 | |
| 111 | wait(); |
| 112 | m_hw_item.set(rc_t::NOOP); |
| 113 | |
| 114 | return rc_t::OK; |
| 115 | } |
| 116 | |
| 117 | std::string |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 118 | unbind_cmd::to_string() const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 119 | { |
| 120 | std::ostringstream s; |
| 121 | s << "Dhcp-config-unbind: " << m_hw_item.to_string() |
| 122 | << " itf:" << m_itf.to_string() << " hostname:" << m_hostname; |
| 123 | |
| 124 | return (s.str()); |
| 125 | } |
| 126 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 127 | events_cmd::events_cmd(dhcp_config::event_listener& el) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 128 | : event_cmd(el.status()) |
| 129 | , m_listener(el) |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | bool |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 134 | events_cmd::operator==(const events_cmd& other) const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 135 | { |
| 136 | return (true); |
| 137 | } |
| 138 | |
| 139 | rc_t |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 140 | events_cmd::issue(connection& con) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 141 | { |
| 142 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 143 | * Set the call back to handle DHCP complete envets. |
| 144 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 145 | m_reg.reset(new reg_t(con.ctx(), std::ref(*this))); |
| 146 | |
| 147 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 148 | * return in-progress so the command stays in the pending list. |
| 149 | */ |
| 150 | return (rc_t::OK); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 154 | events_cmd::retire(connection& con) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 155 | { |
| 156 | } |
| 157 | |
| 158 | void |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 159 | events_cmd::notify() |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 160 | { |
| 161 | m_listener.handle_dhcp_event(this); |
| 162 | } |
| 163 | |
| 164 | std::string |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 165 | events_cmd::to_string() const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 166 | { |
| 167 | return ("dhcp-events"); |
| 168 | } |
| 169 | } |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 170 | }; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 171 | /* |
| 172 | * fd.io coding-style-patch-verification: ON |
| 173 | * |
| 174 | * Local Variables: |
| 175 | * eval: (c-set-style "mozilla") |
| 176 | * End: |
| 177 | */ |