FD.io VPP  v18.04-17-g3a0d853
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"
19 
20 namespace VOM {
21 /**
22  * A DB of all DHCP configs
23  */
24 singular_db<interface::key_t, dhcp_config> dhcp_config::m_db;
25 
26 dhcp_config::event_handler dhcp_config::m_evh;
27 
29  const std::string& hostname,
30  bool set_broadcast_flag)
31  : m_itf(itf.singular())
32  , m_hostname(hostname)
33  , m_client_id(l2_address_t::ZERO)
34  , m_set_broadcast_flag(set_broadcast_flag)
35  , m_binding(0)
36 {
37 }
38 
40  const std::string& hostname,
41  const l2_address_t& client_id,
42  bool set_broadcast_flag)
43  : m_itf(itf.singular())
44  , m_hostname(hostname)
45  , m_client_id(client_id)
46  , m_set_broadcast_flag(set_broadcast_flag)
47  , m_binding(0)
48 {
49 }
50 
52  : m_itf(o.m_itf)
53  , m_hostname(o.m_hostname)
54  , m_client_id(o.m_client_id)
55  , m_set_broadcast_flag(o.m_set_broadcast_flag)
56  , m_binding(0)
57 {
58 }
59 
61 {
62  sweep();
63 
64  // not in the DB anymore.
65  m_db.release(m_itf->key(), this);
66 }
67 
68 bool
70 {
71  return ((key() == l.key()) && (m_hostname == l.m_hostname) &&
72  (m_client_id == l.m_client_id));
73 }
74 
75 const dhcp_config::key_t&
77 {
78  return (m_itf->key());
79 }
80 
81 void
82 dhcp_config::sweep()
83 {
84  if (m_binding) {
86  new dhcp_config_cmds::unbind_cmd(m_binding, m_itf->handle(), m_hostname));
87  }
88  HW::write();
89 }
90 
91 void
92 dhcp_config::dump(std::ostream& os)
93 {
94  db_dump(m_db, os);
95 }
96 
97 void
98 dhcp_config::replay()
99 {
100  if (m_binding) {
101  HW::enqueue(new dhcp_config_cmds::bind_cmd(m_binding, m_itf->handle(),
102  m_hostname, m_client_id));
103  }
104 }
105 
106 std::string
108 {
109  std::ostringstream s;
110  s << "Dhcp-config: " << m_itf->to_string() << " hostname:" << m_hostname
111  << " client_id:[" << m_client_id << "] " << m_binding.to_string();
112 
113  return (s.str());
114 }
115 
116 void
117 dhcp_config::update(const dhcp_config& desired)
118 {
119  /*
120  * the desired state is always that the interface should be created
121  */
122  if (!m_binding) {
123  HW::enqueue(new dhcp_config_cmds::bind_cmd(m_binding, m_itf->handle(),
124  m_hostname, m_client_id));
125  }
126 }
127 
128 std::shared_ptr<dhcp_config>
129 dhcp_config::find_or_add(const dhcp_config& temp)
130 {
131  return (m_db.find_or_add(temp.m_itf->key(), temp));
132 }
133 
134 std::shared_ptr<dhcp_config>
136 {
137  return (m_db.find(k));
138 }
139 
140 std::shared_ptr<dhcp_config>
142 {
143  return find_or_add(*this);
144 }
145 
147  : m_status(rc_t::NOOP)
148 {
149 }
150 
153 {
154  return (m_status);
155 }
156 
157 dhcp_config::event_handler::event_handler()
158 {
159  OM::register_listener(this);
160  inspect::register_handler({ "dhcp" }, "DHCP configurations", this);
161 }
162 
163 void
164 dhcp_config::event_handler::handle_replay()
165 {
166  m_db.replay();
167 }
168 
169 void
170 dhcp_config::event_handler::handle_populate(const client_db::key_t& key)
171 {
172  // FIXME
173 }
174 
176 dhcp_config::event_handler::order() const
177 {
178  return (dependency_t::BINDING);
179 }
180 
181 void
182 dhcp_config::event_handler::show(std::ostream& os)
183 {
184  db_dump(m_db, os);
185 }
186 }
187 
188 /*
189  * fd.io coding-style-patch-verification: ON
190  *
191  * Local Variables:
192  * eval: (c-set-style "mozilla")
193  * End:
194  */
A command class that binds the DHCP config to the interface.
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
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
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:236
Error codes that VPP will return during a HW write.
Definition: types.hpp:90
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:342
bool operator==(const dhcp_config &d) const
Comparison operator - for UT.
Definition: dhcp_config.cpp:69
dhcp_config(const interface &itf, const std::string &hostname, bool set_broadcast_flag=true)
Construct a new object matching the desried state.
Definition: dhcp_config.cpp:28
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:76
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:194
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:60
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:92
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
HW::item< bool > m_status
The HW::item associated with this command.