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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_INTERFACE_IP6_ND_H__
17 #define __VOM_INTERFACE_IP6_ND_H__
19 #include "vom/dump_cmd.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/interface.hpp"
23 #include "vom/object_base.hpp"
25 #include "vom/ra_config.hpp"
26 #include "vom/ra_prefix.hpp"
27 #include "vom/rpc_cmd.hpp"
28 #include "vom/singular_db.hpp"
29 #include "vom/sub_interface.hpp"
33 * A representation of L3 configuration on an interface
35 template <typename CLASS, typename CMD>
36 class interface_ip6_nd : public object_base
39 typedef CLASS class_t;
41 * Construct a new object matching the desried state
43 interface_ip6_nd(const interface& itf, const class_t cls)
44 : m_itf(itf.singular())
53 interface_ip6_nd(const interface_ip6_nd& o)
56 , m_config(o.m_config)
66 m_db.release(m_itf->key(), this);
70 * Return the 'singular instance' of the interface ip6nd that matches
73 std::shared_ptr<interface_ip6_nd> singular() const
75 return find_or_add(*this);
79 * convert to string format for debug purposes
81 std::string to_string() const
84 s << "interface-ip6-nd:["
85 << " itf:" << m_itf->to_string() << " " << m_cls.to_string() << " "
86 << m_config.to_string() << "]";
92 * Dump all config into the stream provided
94 static void dump(std::ostream& os) { m_db.dump(os); }
97 * The key type for interface ip6 nd
99 typedef interface::key_type key_t;
102 * Find an singular instance in the DB for the interface passed
104 static std::shared_ptr<interface_ip6_nd> find(const interface& i)
107 * Loop throught the entire map looking for matching interface.
108 * not the most efficient algorithm, but it will do for now. The
109 * number of ra configs is low.
111 std::deque<std::shared_ptr<interface_ip6_nd>> rac;
113 auto it = m_db.cbegin();
115 while (it != m_db.cend()) {
117 * The key in the DB is a pair of the interface's name.
118 * If the keys match, save the ra-config
120 auto key = it->first;
122 if (i.key() == key.first) {
123 rac.push_back(it->second.lock());
133 * A functor class that binds the ra config to the interface
135 class config_cmd : public rpc_cmd<HW::item<bool>, rc_t, CMD>
141 config_cmd(HW::item<bool>& item, const handle_t& itf, const class_t& cls)
142 : rpc_cmd<HW::item<bool>, rc_t, CMD>(item)
149 * Issue the command to VPP/HW
151 rc_t issue(connection& con);
154 * convert to string format for debug purposes
156 std::string to_string() const
158 std::ostringstream s;
159 s << "interface-ip6-nd: " << this->item().to_string()
160 << " itf:" << m_itf.to_string() << " " << m_cls.to_string();
166 * Comparison operator - only used for UT
168 bool operator==(const config_cmd& other) const
170 return ((m_itf == other.m_itf) && (m_cls == other.m_cls));
175 * Reference to the interface to bind to
177 const handle_t& m_itf;
180 * Reference to the config class
182 const class_t& m_cls;
186 * A cmd class that Unbinds L3 Config from an interface
188 class unconfig_cmd : public rpc_cmd<HW::item<bool>, rc_t, CMD>
194 unconfig_cmd(HW::item<bool>& item, const handle_t& itf, const class_t& cls)
195 : rpc_cmd<HW::item<bool>, rc_t, CMD>(item)
202 * Issue the command to VPP/HW
204 rc_t issue(connection& con);
207 * convert to string format for debug purposes
209 std::string to_string() const
211 std::ostringstream s;
212 s << "interface-ip6-nd: " << this->item().to_string()
213 << " itf:" << m_itf.to_string() << " " << m_cls.to_string();
219 * Comparison operator - only used for UT
221 bool operator==(const unconfig_cmd& other) const
223 return ((m_itf == other.m_itf) && (m_cls == other.m_cls));
228 * Reference to the interface to unbind fomr
230 const handle_t& m_itf;
233 * Reference to the config class to undo configurations
235 const class_t& m_cls;
240 * Class definition for listeners to OM events
242 class event_handler : public OM::listener, public inspect::command_handler
247 OM::register_listener(this);
248 inspect::register_handler({ "ip6_nd " }, "interface ip6 nd", this);
251 virtual ~event_handler() = default;
254 * Handle a populate event
256 void handle_populate(const client_db::key_t& key)
259 * VPP provides no dump for ra config
264 * Handle a replay event
266 void handle_replay() { m_db.replay(); }
269 * Show the object in the Singular DB
271 void show(std::ostream& os) { m_db.dump(os); }
274 * Get the sortable Id of the listener
276 dependency_t order() const { return (dependency_t::BINDING); }
280 * event_handler to register with OM
282 static event_handler m_evh;
285 * Enqueue commands to the VPP for the update
287 void update(const interface_ip6_nd& obj)
290 HW::enqueue(new config_cmd(m_config, m_itf->handle(), m_cls));
297 HW::enqueue(new unconfig_cmd(m_config, m_itf->handle(), m_cls));
303 * Replay the objects state to HW
308 HW::enqueue(new config_cmd(m_config, m_itf->handle(), m_cls));
313 * Find or add the singular instance in the DB
315 static std::shared_ptr<interface_ip6_nd> find_or_add(
316 const interface_ip6_nd& temp)
318 return (m_db.find_or_add(temp.m_itf->key(), temp));
322 * It's the VPPHW class that updates the objects in HW
327 * It's the singular_db class that calls replay()
329 friend class singular_db<key_t, interface_ip6_nd>;
331 const std::shared_ptr<interface> m_itf;
338 * HW configuration for the binding. The bool representing the
341 HW::item<bool> m_config;
344 * A map of all interface ip6 nd keyed against a combination of the
345 * interface and subnet's keys.
347 static singular_db<key_t, interface_ip6_nd> m_db;
351 * Typedef the ip6nd_ra_config
353 typedef interface_ip6_nd<ra_config, vapi::Sw_interface_ip6nd_ra_config>
357 * Typedef the ip6nd_ra_prefix
359 typedef interface_ip6_nd<ra_prefix, vapi::Sw_interface_ip6nd_ra_prefix>
363 * Definition of the static singular_db for ACL Lists
365 template <typename CLASS, typename CMD>
366 singular_db<typename interface_ip6_nd<CLASS, CMD>::key_t,
367 interface_ip6_nd<CLASS, CMD>>
368 interface_ip6_nd<CLASS, CMD>::m_db;
370 template <typename CLASS, typename CMD>
371 typename interface_ip6_nd<CLASS, CMD>::event_handler
372 interface_ip6_nd<CLASS, CMD>::m_evh;
376 * fd.io coding-style-patch-verification: ON
379 * eval: (c-set-style "mozilla")