FD.io VPP  v21.01.1
Vector Packet Processing
gbp_endpoint.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/gbp_endpoint.hpp"
17 #include "vom/api_types.hpp"
20 
21 namespace VOM {
22 
23 singular_db<gbp_endpoint::key_t, gbp_endpoint> gbp_endpoint::m_db;
24 
25 gbp_endpoint::event_handler gbp_endpoint::m_evh;
26 
27 const gbp_endpoint::flags_t gbp_endpoint::flags_t::NONE(0, "none");
28 const gbp_endpoint::flags_t gbp_endpoint::flags_t::BOUNCE(1, "bounce");
29 const gbp_endpoint::flags_t gbp_endpoint::flags_t::LEARNT(2, "learnt");
30 const gbp_endpoint::flags_t gbp_endpoint::flags_t::REMOTE(4, "remote");
31 const gbp_endpoint::flags_t gbp_endpoint::flags_t::EXTERNAL(8, "external");
32 
33 gbp_endpoint::flags_t::flags_t(int v, const std::string& s)
34  : enum_base<gbp_endpoint::flags_t>(v, s)
35 {
36 }
37 
39  const interface& itf,
40  const std::vector<boost::asio::ip::address>& ip_addrs,
41  const mac_address_t& mac,
42  const gbp_endpoint_group& epg,
43  const flags_t& flags)
44  : m_hdl(handle_t::INVALID)
45  , m_itf(itf.singular())
46  , m_ips(ip_addrs)
47  , m_mac(mac)
48  , m_epg(epg.singular())
49  , m_flags(flags)
50 {
51 }
52 
54  : m_hdl(gbpe.m_hdl)
55  , m_itf(gbpe.m_itf)
56  , m_ips(gbpe.m_ips)
57  , m_mac(gbpe.m_mac)
58  , m_epg(gbpe.m_epg)
59  , m_flags(gbpe.m_flags)
60 {
61 }
62 
64 {
65  sweep();
66  m_db.release(key(), this);
67 }
68 
71 {
72  return (std::make_pair(m_itf->key(), m_mac));
73 }
74 
75 bool
77 {
78  return ((key() == gbpe.key()) && (m_epg == gbpe.m_epg) &&
79  (m_flags == gbpe.m_flags));
80 }
81 
82 void
83 gbp_endpoint::sweep()
84 {
85  if (m_hdl) {
87  }
88  HW::write();
89 }
90 
91 void
93 {
94  if (m_hdl) {
96  m_hdl, m_itf->handle(), m_ips, m_mac, m_epg->sclass(), m_flags));
97  }
98 }
99 
102 {
103  std::ostringstream s;
104  s << "gbp-endpoint:[" << m_itf->to_string() << ", ips:[";
105 
106  for (auto ip : m_ips)
107  s << ip.to_string();
108 
109  s << "], " << m_mac.to_string() << ", epg:" << m_epg->to_string() << "]";
110 
111  return (s.str());
112 }
113 
114 void
115 gbp_endpoint::update(const gbp_endpoint& r)
116 {
117  if (rc_t::OK != m_hdl.rc()) {
119  m_hdl, m_itf->handle(), m_ips, m_mac, m_epg->sclass(), m_flags));
120  }
121 }
122 
123 std::shared_ptr<gbp_endpoint>
124 gbp_endpoint::find_or_add(const gbp_endpoint& temp)
125 {
126  return (m_db.find_or_add(temp.key(), temp));
127 }
128 
129 std::shared_ptr<gbp_endpoint>
131 {
132  return (m_db.find(k));
133 }
134 
135 std::shared_ptr<gbp_endpoint>
137 {
138  return find_or_add(*this);
139 }
140 
141 void
142 gbp_endpoint::dump(std::ostream& os)
143 {
144  db_dump(m_db, os);
145 }
146 
148 {
149  OM::register_listener(this);
150  inspect::register_handler({ "gbp-endpoint" }, "GBP Endpoints", this);
151 }
152 
153 void
154 gbp_endpoint::event_handler::handle_replay()
155 {
156  m_db.replay();
157 }
158 
159 void
160 gbp_endpoint::event_handler::handle_populate(const client_db::key_t& key)
161 {
162  std::shared_ptr<gbp_endpoint_cmds::dump_cmd> cmd =
163  std::make_shared<gbp_endpoint_cmds::dump_cmd>();
164 
165  HW::enqueue(cmd);
166  HW::write();
167 
168  for (auto& record : *cmd) {
169  auto& payload = record.get_payload();
170 
171  std::vector<boost::asio::ip::address> addresses;
172 
173  for (uint8_t n = 0; n < payload.endpoint.n_ips; n++)
174  addresses.push_back(from_api(payload.endpoint.ips[n]));
175  std::shared_ptr<interface> itf =
176  interface::find(payload.endpoint.sw_if_index);
177  std::shared_ptr<gbp_endpoint_group> epg =
178  gbp_endpoint_group::find(payload.endpoint.sclass);
179  mac_address_t mac = from_api(payload.endpoint.mac);
180 
181  VOM_LOG(log_level_t::DEBUG) << "data: " << payload.endpoint.sw_if_index;
182 
183  if (itf && epg) {
184  gbp_endpoint gbpe(*itf, addresses, mac, *epg);
185  OM::commit(key, gbpe);
186 
187  VOM_LOG(log_level_t::DEBUG) << "read: " << gbpe.to_string();
188  } else {
190  << "no interface:" << payload.endpoint.sw_if_index
191  << "or sclass:" << payload.endpoint.sclass;
192  }
193  }
194 }
195 
197 gbp_endpoint::event_handler::order() const
198 {
199  return (dependency_t::ENTRY);
200 }
201 
202 void
203 gbp_endpoint::event_handler::show(std::ostream& os)
204 {
205  db_dump(m_db, os);
206 }
207 
208 std::ostream&
209 operator<<(std::ostream& os, const gbp_endpoint::key_t& key)
210 {
211  os << key.first << "," << key.second;
212 
213  return os;
214 }
215 
216 } // namespace VOM
217 
218 /*
219  * fd.io coding-style-patch-verification: OFF
220  *
221  * Local Variables:
222  * eval: (c-set-style "mozilla")
223  * End:
224  */
static const flags_t LEARNT
static const flags_t BOUNCE
std::shared_ptr< gbp_endpoint > singular() const
Return the matching &#39;singular instance&#39;.
#define VOM_LOG(lvl)
Definition: logger.hpp:181
static const flags_t NONE
vl_api_mac_address_t mac
Definition: l2.api:502
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
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 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
const char *const string
Definition: cJSON.h:172
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
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:119
static const log_level_t DEBUG
Definition: logger.hpp:32
std::pair< interface::key_t, mac_address_t > key_t
The key for a GBP endpoint; interface and IP.
~gbp_endpoint()
Destructor.
A cmd class that deletes a GBP endpoint.
gbp_endpoint(const interface &itf, const std::vector< boost::asio::ip::address > &ip_addr, const mac_address_t &mac, const gbp_endpoint_group &epg, const flags_t &flags=flags_t::NONE)
Construct a GBP endpoint.
const key_t key() const
Return the object&#39;s key.
A GBP Enpoint (i.e.
static const flags_t REMOTE
A command class that creates or updates the GBP endpoint.
static const flags_t EXTERNAL
static void dump(std::ostream &os)
Dump all bridge_domain-doamin into the stream provided.
std::string to_string() const
Convert to string for debugging.
bool operator==(const gbp_endpoint &bdae) const
comparison operator
A representation of an interface in VPP.
Definition: interface.hpp:41
A entry in the ARP termination table of a Bridge Domain.
A type declaration of an interface handle in VPP.
Definition: types.hpp:233
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:334
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 const rc_t OK
The HW write was successfull.
Definition: types.hpp:109
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:212
void replay(void)
replay the object to create it in hardware
static const log_level_t ERROR
Definition: logger.hpp:29
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
std::string to_string() const
String conversion.
Definition: types.cpp:148
typedef gbp_endpoint
Definition: gbp.api:129
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
vl_api_address_t ip
Definition: l2.api:501
static std::shared_ptr< gbp_endpoint > find(const key_t &k)
Find the instnace of the bridge_domain domain in the OM.
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
Entries in Tables.
static std::shared_ptr< gbp_endpoint_group > find(const key_t &k)
Find the instnace of the bridge_domain domain in the OM.
Type def of a Ethernet address.
Definition: types.hpp:295
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127