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 | |
| 16 | #ifndef __VOM_DHCP_INTERFACE_H__ |
| 17 | #define __VOM_DHCP_INTERFACE_H__ |
| 18 | |
| 19 | #include "vom/dump_cmd.hpp" |
| 20 | #include "vom/hw.hpp" |
| 21 | #include "vom/inspect.hpp" |
| 22 | #include "vom/interface.hpp" |
| 23 | #include "vom/object_base.hpp" |
| 24 | #include "vom/om.hpp" |
| 25 | #include "vom/rpc_cmd.hpp" |
| 26 | #include "vom/singular_db.hpp" |
| 27 | #include "vom/sub_interface.hpp" |
| 28 | |
| 29 | #include <vapi/dhcp.api.vapi.hpp> |
| 30 | |
| 31 | namespace VOM { |
| 32 | /** |
| 33 | * A representation of DHCP client configuration on an interface |
| 34 | */ |
| 35 | class dhcp_config : public object_base |
| 36 | { |
| 37 | public: |
| 38 | /** |
| 39 | * Construct a new object matching the desried state |
| 40 | */ |
| 41 | dhcp_config(const interface& itf, const std::string& hostname); |
| 42 | |
| 43 | /** |
| 44 | * Construct a new object matching the desried state |
| 45 | */ |
| 46 | dhcp_config(const interface& itf, |
| 47 | const std::string& hostname, |
| 48 | const l2_address_t& client_id); |
| 49 | |
| 50 | /** |
| 51 | * Copy Constructor |
| 52 | */ |
| 53 | dhcp_config(const dhcp_config& o); |
| 54 | |
| 55 | /** |
| 56 | * Destructor |
| 57 | */ |
| 58 | ~dhcp_config(); |
| 59 | |
| 60 | /** |
| 61 | * Return the 'singular' of the DHCP config that matches this object |
| 62 | */ |
| 63 | std::shared_ptr<dhcp_config> singular() const; |
| 64 | |
| 65 | /** |
| 66 | * convert to string format for debug purposes |
| 67 | */ |
| 68 | std::string to_string() const; |
| 69 | |
| 70 | /** |
| 71 | * Dump all DHCP configs into the stream provided |
| 72 | */ |
| 73 | static void dump(std::ostream& os); |
| 74 | |
| 75 | /** |
| 76 | * A command class that binds the DHCP config to the interface |
| 77 | */ |
| 78 | class bind_cmd |
| 79 | : public rpc_cmd<HW::item<bool>, rc_t, vapi::Dhcp_client_config> |
| 80 | { |
| 81 | public: |
| 82 | /** |
| 83 | * Constructor |
| 84 | */ |
| 85 | bind_cmd(HW::item<bool>& item, |
| 86 | const handle_t& itf, |
| 87 | const std::string& hostname, |
| 88 | const l2_address_t& client_id); |
| 89 | |
| 90 | /** |
| 91 | * Issue the command to VPP/HW |
| 92 | */ |
| 93 | rc_t issue(connection& con); |
| 94 | /** |
| 95 | * convert to string format for debug purposes |
| 96 | */ |
| 97 | std::string to_string() const; |
| 98 | |
| 99 | /** |
| 100 | * Comparison operator - only used for UT |
| 101 | */ |
| 102 | bool operator==(const bind_cmd& i) const; |
| 103 | |
| 104 | private: |
| 105 | /** |
| 106 | * Reference to the HW::item of the interface to bind |
| 107 | */ |
| 108 | const handle_t& m_itf; |
| 109 | |
| 110 | /** |
| 111 | * The DHCP client's hostname |
| 112 | */ |
| 113 | const std::string m_hostname; |
| 114 | |
| 115 | /** |
| 116 | * The DHCP client's ID |
| 117 | */ |
| 118 | const l2_address_t m_client_id; |
| 119 | }; |
| 120 | |
| 121 | /** |
| 122 | * A cmd class that Unbinds Dhcp Config from an interface |
| 123 | */ |
| 124 | class unbind_cmd |
| 125 | : public rpc_cmd<HW::item<bool>, rc_t, vapi::Dhcp_client_config> |
| 126 | { |
| 127 | public: |
| 128 | /** |
| 129 | * Constructor |
| 130 | */ |
| 131 | unbind_cmd(HW::item<bool>& item, |
| 132 | const handle_t& itf, |
| 133 | const std::string& hostname); |
| 134 | |
| 135 | /** |
| 136 | * Issue the command to VPP/HW |
| 137 | */ |
| 138 | rc_t issue(connection& con); |
| 139 | /** |
| 140 | * convert to string format for debug purposes |
| 141 | */ |
| 142 | std::string to_string() const; |
| 143 | |
| 144 | /** |
| 145 | * Comparison operator - only used for UT |
| 146 | */ |
| 147 | bool operator==(const unbind_cmd& i) const; |
| 148 | |
| 149 | private: |
| 150 | /** |
| 151 | * Reference to the HW::item of the interface to unbind |
| 152 | */ |
| 153 | const handle_t& m_itf; |
| 154 | |
| 155 | /** |
| 156 | * The DHCP client's hostname |
| 157 | */ |
| 158 | const std::string m_hostname; |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Forward declartion of the Event Command |
| 163 | */ |
| 164 | class events_cmd; |
| 165 | |
| 166 | /** |
| 167 | * A class that listens to DHCP Events |
| 168 | */ |
| 169 | class event_listener |
| 170 | { |
| 171 | public: |
| 172 | /** |
| 173 | * Constructor |
| 174 | */ |
| 175 | event_listener(); |
| 176 | |
| 177 | /** |
| 178 | * listener's virtual function invoked when a DHCP event is |
| 179 | * available to read |
| 180 | */ |
| 181 | virtual void handle_dhcp_event(events_cmd* cmd) = 0; |
| 182 | |
| 183 | /** |
| 184 | * Return the HW::item associated with this command |
| 185 | */ |
| 186 | HW::item<bool>& status(); |
| 187 | |
| 188 | protected: |
| 189 | /** |
| 190 | * The HW::item associated with this command |
| 191 | */ |
| 192 | HW::item<bool> m_status; |
| 193 | }; |
| 194 | |
| 195 | /** |
| 196 | * A functor class represents our desire to recieve interface events |
| 197 | */ |
| 198 | class events_cmd |
| 199 | : public event_cmd<vapi::Control_ping, vapi::Dhcp_compl_event> |
| 200 | { |
| 201 | public: |
| 202 | /** |
| 203 | * Constructor |
| 204 | */ |
| 205 | events_cmd(event_listener& el); |
| 206 | |
| 207 | /** |
| 208 | * Issue the command to VPP/HW - subscribe to DHCP events |
| 209 | */ |
| 210 | rc_t issue(connection& con); |
| 211 | |
| 212 | /** |
| 213 | * Retire the command - unsubscribe |
| 214 | */ |
| 215 | void retire(connection& con); |
| 216 | /** |
| 217 | * convert to string format for debug purposes |
| 218 | */ |
| 219 | std::string to_string() const; |
| 220 | |
| 221 | /** |
| 222 | * Comparison operator - only used for UT |
| 223 | */ |
| 224 | bool operator==(const events_cmd& i) const; |
| 225 | |
| 226 | /** |
| 227 | * called in the VAPI RX thread when data is available. |
| 228 | */ |
| 229 | void notify(); |
| 230 | |
| 231 | private: |
| 232 | void succeeded() {} |
| 233 | /** |
| 234 | * The listner of this command |
| 235 | */ |
| 236 | event_listener& m_listener; |
| 237 | }; |
| 238 | |
| 239 | private: |
| 240 | /** |
| 241 | * Class definition for listeners to OM events |
| 242 | */ |
| 243 | class event_handler : public OM::listener, public inspect::command_handler |
| 244 | { |
| 245 | public: |
| 246 | event_handler(); |
| 247 | virtual ~event_handler() = default; |
| 248 | |
| 249 | /** |
| 250 | * Handle a populate event |
| 251 | */ |
| 252 | void handle_populate(const client_db::key_t& key); |
| 253 | |
| 254 | /** |
| 255 | * Handle a replay event |
| 256 | */ |
| 257 | void handle_replay(); |
| 258 | |
| 259 | /** |
| 260 | * Show the object in the Singular DB |
| 261 | */ |
| 262 | void show(std::ostream& os); |
| 263 | |
| 264 | /** |
| 265 | * Get the sortable Id of the listener |
| 266 | */ |
| 267 | dependency_t order() const; |
| 268 | }; |
| 269 | |
| 270 | /** |
| 271 | * event_handler to register with OM |
| 272 | */ |
| 273 | static event_handler m_evh; |
| 274 | |
| 275 | /** |
| 276 | * Enquue commonds to the VPP command Q for the update |
| 277 | */ |
| 278 | void update(const dhcp_config& obj); |
| 279 | |
| 280 | /** |
| 281 | * Find or add DHCP config to the OM |
| 282 | */ |
| 283 | static std::shared_ptr<dhcp_config> find_or_add(const dhcp_config& temp); |
| 284 | |
| 285 | /* |
| 286 | * It's the OM class that calls singular() |
| 287 | */ |
| 288 | friend class OM; |
| 289 | |
| 290 | /** |
| 291 | * It's the singular_db class that calls replay() |
| 292 | */ |
| 293 | friend class singular_db<interface::key_type, dhcp_config>; |
| 294 | |
| 295 | /** |
| 296 | * Sweep/reap the object if still stale |
| 297 | */ |
| 298 | void sweep(void); |
| 299 | |
| 300 | /** |
| 301 | * replay the object to create it in hardware |
| 302 | */ |
| 303 | void replay(void); |
| 304 | |
| 305 | /** |
| 306 | * A reference counting pointer to the interface on which DHCP config |
| 307 | * resides. By holding the reference here, we can guarantee that |
| 308 | * this object will outlive the interface |
| 309 | */ |
| 310 | const std::shared_ptr<interface> m_itf; |
| 311 | |
| 312 | /** |
| 313 | * The hostname in the DHCP configuration |
| 314 | */ |
| 315 | const std::string m_hostname; |
| 316 | |
| 317 | /** |
| 318 | * The option-61 client_id in the DHCP configuration |
| 319 | */ |
| 320 | const l2_address_t m_client_id; |
| 321 | |
| 322 | /** |
| 323 | * HW configuration for the binding. The bool representing the |
| 324 | * do/don't bind. |
| 325 | */ |
| 326 | HW::item<bool> m_binding; |
| 327 | |
| 328 | /** |
| 329 | * A map of all Dhcp configs keyed against the interface. |
| 330 | */ |
| 331 | static singular_db<interface::key_type, dhcp_config> m_db; |
| 332 | }; |
| 333 | }; |
| 334 | |
| 335 | /* |
| 336 | * fd.io coding-style-patch-verification: ON |
| 337 | * |
| 338 | * Local Variables: |
| 339 | * eval: (c-set-style "mozilla") |
| 340 | * End: |
| 341 | */ |
| 342 | |
| 343 | #endif |