VOM: deprecate TAP add ip-punt redirect dump
[vpp.git] / extras / vom / vom / ip_punt_redirect.cpp
1 /*
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:
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/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"
20
21 namespace VOM {
22 /**
23  * A DB of all IP Punt configs
24  */
25 singular_db<ip_punt_redirect::key_t, ip_punt_redirect> ip_punt_redirect::m_db;
26
27 ip_punt_redirect::event_handler ip_punt_redirect::m_evh;
28
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())
34   , m_addr(addr)
35 {
36 }
37
38 ip_punt_redirect::ip_punt_redirect(const interface& tx_itf,
39                                    const boost::asio::ip::address& addr)
40   : m_rx_itf(nullptr)
41   , m_tx_itf(tx_itf.singular())
42   , m_addr(addr)
43 {
44 }
45
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)
49   , m_addr(o.m_addr)
50   , m_config(o.m_config)
51 {
52 }
53
54 ip_punt_redirect::~ip_punt_redirect()
55 {
56   sweep();
57
58   // not in the DB anymore.
59   m_db.release(key(), this);
60 }
61
62 const ip_punt_redirect::key_t
63 ip_punt_redirect::key() const
64 {
65   if (m_rx_itf)
66     return m_rx_itf->key();
67   else
68     return ("ALL");
69 }
70
71 void
72 ip_punt_redirect::sweep()
73 {
74   if (m_config) {
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));
78   }
79   HW::write();
80 }
81
82 void
83 ip_punt_redirect::dump(std::ostream& os)
84 {
85   db_dump(m_db, os);
86 }
87
88 void
89 ip_punt_redirect::replay()
90 {
91   if (m_config) {
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));
95   }
96 }
97
98 std::string
99 ip_punt_redirect::to_string() const
100 {
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;
105
106   return (s.str());
107 }
108
109 void
110 ip_punt_redirect::update(const ip_punt_redirect& desired)
111 {
112   if (!m_config) {
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));
116   }
117 }
118
119 std::shared_ptr<ip_punt_redirect>
120 ip_punt_redirect::find_or_add(const ip_punt_redirect& temp)
121 {
122   return (m_db.find_or_add(temp.key(), temp));
123 }
124
125 std::shared_ptr<ip_punt_redirect>
126 ip_punt_redirect::singular() const
127 {
128   return find_or_add(*this);
129 }
130
131 ip_punt_redirect::event_handler::event_handler()
132 {
133   OM::register_listener(this);
134   inspect::register_handler({ "ip-punt-redirect" },
135                             "IP punt redirect configurations", this);
136 }
137
138 void
139 ip_punt_redirect::event_handler::handle_replay()
140 {
141   m_db.replay();
142 }
143
144 void
145 ip_punt_redirect::event_handler::handle_populate(const client_db::key_t& key)
146 {
147   std::shared_ptr<ip_punt_redirect_cmds::dump_cmd> cmd =
148     std::make_shared<ip_punt_redirect_cmds::dump_cmd>();
149
150   HW::enqueue(cmd);
151   HW::write();
152
153   for (auto& record : *cmd) {
154     auto& payload = record.get_payload();
155
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);
161
162     VOM_LOG(log_level_t::DEBUG) << "data: [" << payload.punt.tx_sw_if_index
163                                 << ", " << payload.punt.rx_sw_if_index << ", "
164                                 << nh << "]";
165
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();
170     } else if (tx_itf) {
171       ip_punt_redirect ipr(*tx_itf, nh);
172       OM::commit(key, ipr);
173       VOM_LOG(log_level_t::DEBUG) << "read: " << ipr.to_string();
174     }
175   }
176 }
177
178 dependency_t
179 ip_punt_redirect::event_handler::order() const
180 {
181   return (dependency_t::BINDING);
182 }
183
184 void
185 ip_punt_redirect::event_handler::show(std::ostream& os)
186 {
187   db_dump(m_db, os);
188 }
189 }
190
191 /*
192  * fd.io coding-style-patch-verification: ON
193  *
194  * Local Variables:
195  * eval: (c-set-style "mozilla")
196  * End:
197  */