FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
ip_punt_redirect.cpp
Go to the documentation of this file.
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"
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 
30  const interface& tx_itf,
32  : m_rx_itf(rx_itf.singular())
33  , m_tx_itf(tx_itf.singular())
34  , m_addr(addr)
35 {
36 }
37 
40  : m_rx_itf(nullptr)
41  , m_tx_itf(tx_itf.singular())
42  , m_addr(addr)
43 {
44 }
45 
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 
55 {
56  sweep();
57 
58  // not in the DB anymore.
59  m_db.release(key(), this);
60 }
61 
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) {
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) {
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
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) {
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>
127 {
128  return find_or_add(*this);
129 }
130 
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 
179 ip_punt_redirect::event_handler::order() const
180 {
181  return (dependency_t::BINDING);
182 }
183 
184 void
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  */
#define VOM_LOG(lvl)
Definition: logger.hpp:181
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
vl_api_fib_path_nh_t nh
Definition: fib_types.api:126
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
A command class that configures the IP punt_redirect.
static std::shared_ptr< interface > find(const handle_t &h)
The the singular instance of the interface in the DB by handle.
Definition: interface.cpp:538
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:255
std::string to_string() const
convert to string format for debug purposes
static const log_level_t DEBUG
Definition: logger.hpp:32
vhost_vring_addr_t addr
Definition: vhost_user.h:147
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:268
A cmd class that Unconfigs Ip punt redirect.
static void dump(std::ostream &os)
Dump all ip_punt_redirects into the stream provided.
~ip_punt_redirect()
Destructor.
interface::key_t key_t
The key type for ip_punt_redirects.
A representation of an interface in VPP.
Definition: interface.hpp:41
static rc_t commit(const client_db::key_t &key, const OBJ &obj)
Make the State in VPP reflect the expressed desired state.
Definition: om.hpp:202
void event_handler(void *tls_async)
Definition: tls_async.c:338
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:212
manual_print typedef address
Definition: ip_types.api:84
const key_t key() const
return the object&#39;s key
std::shared_ptr< ip_punt_redirect > singular() const
Return the &#39;singular instance&#39; of the ip_punt_redirect that matches this object.
ip_punt_redirect(const interface &rx_itf, const interface &tx_itf, const boost::asio::ip::address &addr)
Construct a new object matching the desried state.
Then L2/objects that bind to interfaces, BD, ACLS, etc.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
const neighbour::flags_t from_api(vapi_enum_ip_neighbor_flags f)
Definition: api_types.cpp:36
A representation of a method call to VPP.
Definition: cmd.hpp:32
A representation of IP punt_redirect configuration on an interface.
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127