2 * Copyright (c) 2018 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 #include "vom/ip_punt_redirect.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/ip_punt_redirect_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
23 * A DB of all IP Punt configs
25 singular_db<ip_punt_redirect::key_t, ip_punt_redirect> ip_punt_redirect::m_db;
27 ip_punt_redirect::event_handler ip_punt_redirect::m_evh;
29 ip_punt_redirect::ip_punt_redirect(const interface& rx_itf,
30 const interface& tx_itf,
31 const boost::asio::ip::address& addr)
32 : m_rx_itf(rx_itf.singular())
33 , m_tx_itf(tx_itf.singular())
38 ip_punt_redirect::ip_punt_redirect(const interface& tx_itf,
39 const boost::asio::ip::address& addr)
41 , m_tx_itf(tx_itf.singular())
46 ip_punt_redirect::ip_punt_redirect(const ip_punt_redirect& o)
47 : m_rx_itf(o.m_rx_itf)
48 , m_tx_itf(o.m_tx_itf)
50 , m_config(o.m_config)
54 ip_punt_redirect::~ip_punt_redirect()
58 // not in the DB anymore.
59 m_db.release(key(), this);
62 const ip_punt_redirect::key_t
63 ip_punt_redirect::key() const
66 return m_rx_itf->key();
72 ip_punt_redirect::sweep()
75 HW::enqueue(new ip_punt_redirect_cmds::unconfig_cmd(
76 m_config, (m_rx_itf ? m_rx_itf->handle() : handle_t::INVALID),
77 m_tx_itf->handle(), m_addr));
83 ip_punt_redirect::dump(std::ostream& os)
89 ip_punt_redirect::replay()
92 HW::enqueue(new ip_punt_redirect_cmds::config_cmd(
93 m_config, (m_rx_itf ? m_rx_itf->handle() : handle_t::INVALID),
94 m_tx_itf->handle(), m_addr));
99 ip_punt_redirect::to_string() const
101 std::ostringstream s;
102 s << "IP-punt-redirect:"
103 << " rx-itf:" << key() << " tx-itf:" << m_tx_itf->to_string()
104 << " next-hop:" << m_addr;
110 ip_punt_redirect::update(const ip_punt_redirect& desired)
113 HW::enqueue(new ip_punt_redirect_cmds::config_cmd(
114 m_config, (m_rx_itf ? m_rx_itf->handle() : handle_t::INVALID),
115 m_tx_itf->handle(), m_addr));
119 std::shared_ptr<ip_punt_redirect>
120 ip_punt_redirect::find_or_add(const ip_punt_redirect& temp)
122 return (m_db.find_or_add(temp.key(), temp));
125 std::shared_ptr<ip_punt_redirect>
126 ip_punt_redirect::singular() const
128 return find_or_add(*this);
131 ip_punt_redirect::event_handler::event_handler()
133 OM::register_listener(this);
134 inspect::register_handler({ "ip-punt-redirect" },
135 "IP punt redirect configurations", this);
139 ip_punt_redirect::event_handler::handle_replay()
145 ip_punt_redirect::event_handler::handle_populate(const client_db::key_t& key)
147 std::shared_ptr<ip_punt_redirect_cmds::dump_cmd> cmd =
148 std::make_shared<ip_punt_redirect_cmds::dump_cmd>();
153 for (auto& record : *cmd) {
154 auto& payload = record.get_payload();
156 std::shared_ptr<interface> tx_itf =
157 interface::find(payload.punt.tx_sw_if_index);
158 std::shared_ptr<interface> rx_itf =
159 interface::find(payload.punt.rx_sw_if_index);
160 boost::asio::ip::address nh = from_api(payload.punt.nh);
162 VOM_LOG(log_level_t::DEBUG) << "data: [" << payload.punt.tx_sw_if_index
163 << ", " << payload.punt.rx_sw_if_index << ", "
166 if (rx_itf && tx_itf) {
167 ip_punt_redirect ipr(*rx_itf, *tx_itf, nh);
168 OM::commit(key, ipr);
169 VOM_LOG(log_level_t::DEBUG) << "read: " << ipr.to_string();
171 ip_punt_redirect ipr(*tx_itf, nh);
172 OM::commit(key, ipr);
173 VOM_LOG(log_level_t::DEBUG) << "read: " << ipr.to_string();
179 ip_punt_redirect::event_handler::order() const
181 return (dependency_t::BINDING);
185 ip_punt_redirect::event_handler::show(std::ostream& os)
192 * fd.io coding-style-patch-verification: ON
195 * eval: (c-set-style "mozilla")