FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
dhcp_config.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_config.hpp"
17 #include "vom/dhcp_config_cmds.hpp"
18 
19 namespace VOM {
20 /**
21  * A DB of all DHCP configs
22  */
23 singular_db<interface::key_t, dhcp_config> dhcp_config::m_db;
24 
25 dhcp_config::event_handler dhcp_config::m_evh;
26 
27 dhcp_config::dhcp_config(const interface& itf, const std::string& hostname)
28  : m_itf(itf.singular())
29  , m_hostname(hostname)
30  , m_client_id(l2_address_t::ZERO)
31  , m_binding(0)
32 {
33 }
34 
36  const std::string& hostname,
37  const l2_address_t& client_id)
38  : m_itf(itf.singular())
39  , m_hostname(hostname)
40  , m_client_id(client_id)
41  , m_binding(0)
42 {
43 }
44 
46  : m_itf(o.m_itf)
47  , m_hostname(o.m_hostname)
48  , m_client_id(o.m_client_id)
49  , m_binding(0)
50 {
51 }
52 
54 {
55  sweep();
56 
57  // not in the DB anymore.
58  m_db.release(m_itf->key(), this);
59 }
60 
61 bool
63 {
64  return ((key() == l.key()) && (m_hostname == l.m_hostname) &&
65  (m_client_id == l.m_client_id));
66 }
67 
68 const dhcp_config::key_t&
70 {
71  return (m_itf->key());
72 }
73 
74 void
75 dhcp_config::sweep()
76 {
77  if (m_binding) {
79  new dhcp_config_cmds::unbind_cmd(m_binding, m_itf->handle(), m_hostname));
80  }
81  HW::write();
82 }
83 
84 void
85 dhcp_config::dump(std::ostream& os)
86 {
87  m_db.dump(os);
88 }
89 
90 void
91 dhcp_config::replay()
92 {
93  if (m_binding) {
94  HW::enqueue(new dhcp_config_cmds::bind_cmd(m_binding, m_itf->handle(),
95  m_hostname, m_client_id));
96  }
97 }
98 
99 std::string
101 {
102  std::ostringstream s;
103  s << "Dhcp-config: " << m_itf->to_string() << " hostname:" << m_hostname
104  << " client_id:[" << m_client_id << "] " << m_binding.to_string();
105 
106  return (s.str());
107 }
108 
109 void
110 dhcp_config::update(const dhcp_config& desired)
111 {
112  /*
113  * the desired state is always that the interface should be created
114  */
115  if (!m_binding) {
116  HW::enqueue(new dhcp_config_cmds::bind_cmd(m_binding, m_itf->handle(),
117  m_hostname, m_client_id));
118  }
119 }
120 
121 std::shared_ptr<dhcp_config>
122 dhcp_config::find_or_add(const dhcp_config& temp)
123 {
124  return (m_db.find_or_add(temp.m_itf->key(), temp));
125 }
126 
127 std::shared_ptr<dhcp_config>
129 {
130  return (m_db.find(k));
131 }
132 
133 std::shared_ptr<dhcp_config>
135 {
136  return find_or_add(*this);
137 }
138 
140  : m_status(rc_t::NOOP)
141 {
142 }
143 
146 {
147  return (m_status);
148 }
149 
150 dhcp_config::event_handler::event_handler()
151 {
152  OM::register_listener(this);
153  inspect::register_handler({ "dhcp" }, "DHCP configurations", this);
154 }
155 
156 void
157 dhcp_config::event_handler::handle_replay()
158 {
159  m_db.replay();
160 }
161 
162 void
163 dhcp_config::event_handler::handle_populate(const client_db::key_t& key)
164 {
165  // FIXME
166 }
167 
169 dhcp_config::event_handler::order() const
170 {
171  return (dependency_t::BINDING);
172 }
173 
174 void
175 dhcp_config::event_handler::show(std::ostream& os)
176 {
177  m_db.dump(os);
178 }
179 }
180 
181 /*
182  * fd.io coding-style-patch-verification: ON
183  *
184  * Local Variables:
185  * eval: (c-set-style "mozilla")
186  * End:
187  */
A command class that binds the DHCP config to the interface.
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
dhcp_config(const interface &itf, const std::string &hostname)
Construct a new object matching the desried state.
Definition: dhcp_config.cpp:27
static void register_handler(const std::vector< std::string > &cmds, const std::string &help, command_handler *ch)
Register a command handler for inspection.
Definition: inspect.cpp:85
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:225
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:160
Type def of a L2 address as read from VPP.
Definition: types.hpp:265
bool operator==(const dhcp_config &d) const
Comparison operator - for UT.
Definition: dhcp_config.cpp:62
static std::shared_ptr< dhcp_config > find(const key_t &k)
Find a DHCP config from its key.
A cmd class that Unbinds Dhcp Config from an interface.
A representation of DHCP client configuration on an interface.
Definition: dhcp_config.hpp:33
HW::item< bool > & status()
Return the HW::item associated with this command.
const key_t & key() const
Return the object&#39;s key.
Definition: dhcp_config.cpp:69
std::shared_ptr< dhcp_config > singular() const
Return the &#39;singular&#39; of the DHCP config that matches this object.
A representation of an interface in VPP.
Definition: interface.hpp:41
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:189
Then L2/objects that bind to interfaces, BD, ACLS, etc.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
interface::key_t key_t
typedef for the DHCP config key type
Definition: dhcp_config.hpp:39
~dhcp_config()
Destructor.
Definition: dhcp_config.cpp:53
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
std::string to_string() const
convert to string format for debug purposes
static void dump(std::ostream &os)
Dump all DHCP configs into the stream provided.
Definition: dhcp_config.cpp:85
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:124
HW::item< bool > m_status
The HW::item associated with this command.