FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
interface_factory.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 <boost/algorithm/string.hpp>
17 
18 #include "vom/bond_interface.hpp"
19 #include "vom/bond_member.hpp"
21 #include "vom/sub_interface.hpp"
22 #include "vom/tap_interface.hpp"
23 
24 namespace VOM {
25 std::shared_ptr<interface>
26 interface_factory::new_interface(const vapi_payload_sw_interface_details& vd)
27 {
28  std::shared_ptr<interface> sp;
29 
30  /**
31  * Determine the interface type from the name and VLAN attributes
32  */
33  std::string name = reinterpret_cast<const char*>(vd.interface_name);
36  interface::admin_state_t::from_int(vd.admin_up_down);
37  handle_t hdl(vd.sw_if_index);
38  l2_address_t l2_address(vd.l2_address, vd.l2_address_length);
39  std::string tag = "";
40 
41  if (interface::type_t::UNKNOWN == type) {
42  return sp;
43  }
44 
45  sp = interface::find(hdl);
46  if (sp) {
47  sp->set(state);
48  sp->set(l2_address);
49  if (!tag.empty())
50  sp->set(tag);
51  return sp;
52  }
53 
54  /*
55  * If here, Fall back to old routine
56  */
57  if (interface::type_t::AFPACKET == type) {
58  /*
59  * need to strip VPP's "host-" prefix from the interface name
60  */
61  name = name.substr(5);
62  }
63  /**
64  * if the tag is set, then we wrote that to specify a name to make
65  * the interface type more specific
66  */
67  if (vd.tag[0] != 0) {
68  tag = std::string(reinterpret_cast<const char*>(vd.tag));
69  }
70 
71  if (!tag.empty() && interface::type_t::LOOPBACK == type) {
72  name = tag;
73  type = interface::type_t::from_string(name);
74  }
75 
76  /*
77  * pull out the other special cases
78  */
79  if (interface::type_t::TAPV2 == type) {
80  /*
81  * TAP interfaces
82  */
83  sp = interface::find(hdl);
84  if (sp && !tag.empty())
85  sp->set(tag);
86  } else if (interface::type_t::PIPE == type) {
87  /*
88  * there's not enough information in a SW interface record to
89  * construct a pipe. so skip it. They have
90  * their own dump routines
91  */
92  } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
93  /*
94  * Sub-interface
95  * split the name into the parent and VLAN
96  */
97  std::vector<std::string> parts;
98  std::shared_ptr<interface> parent;
99  boost::split(parts, name, boost::is_any_of("."));
100 
101  if ((parent = interface::find(parts[0])))
102  sp = sub_interface(*parent, state, vd.sub_id).singular();
103  else {
104  interface parent_itf(parts[0], type, state, tag);
105  sp = sub_interface(parent_itf, state, vd.sub_id).singular();
106  }
107  } else if (interface::type_t::VXLAN == type) {
108  /*
109  * there's not enough information in a SW interface record to
110  * construct a VXLAN tunnel. so skip it. They have
111  * their own dump routines
112  */
113  } else if (interface::type_t::VHOST == type) {
114  /*
115  * vhost interface already exist in db, look for it using
116  * sw_if_index
117  */
118  } else if (interface::type_t::BOND == type) {
119  sp = bond_interface(name, state, l2_address,
121  .singular();
122  } else {
123  sp = interface(name, type, state, tag).singular();
124  sp->set(l2_address);
125  }
126 
127  /*
128  * set the handle on the intterface - N.B. this is the sigluar instance
129  * not a stack local.
130  */
131  if (sp)
132  sp->set(hdl);
133 
134  return (sp);
135 }
136 
137 std::shared_ptr<interface>
139  const vapi_payload_sw_interface_vhost_user_details& vd)
140 {
141  std::shared_ptr<interface> sp;
142  std::string name = reinterpret_cast<const char*>(vd.sock_filename);
144  handle_t hdl(vd.sw_if_index);
145 
147  sp->set(hdl);
148  return (sp);
149 }
150 
151 std::shared_ptr<interface>
153  const vapi_payload_af_packet_details& vd)
154 {
155  std::shared_ptr<interface> sp;
156  std::string name = reinterpret_cast<const char*>(vd.host_if_name);
157  handle_t hdl(vd.sw_if_index);
158 
159  sp =
161  .singular();
162  sp->set(hdl);
163  return (sp);
164 }
165 
166 std::shared_ptr<tap_interface>
168  const vapi_payload_sw_interface_tap_v2_details& vd)
169 {
170  std::shared_ptr<tap_interface> sp;
171  handle_t hdl(vd.sw_if_index);
172  std::string name = reinterpret_cast<const char*>(vd.host_if_name);
175 
176  if (vd.host_ip4_prefix_len)
177  pfx =
178  route::prefix_t(0, (uint8_t*)vd.host_ip4_addr, vd.host_ip4_prefix_len);
179  else if (vd.host_ip6_prefix_len)
180  pfx =
181  route::prefix_t(1, (uint8_t*)vd.host_ip6_addr, vd.host_ip6_prefix_len);
182 
183  l2_address_t l2_address(vd.host_mac_addr, 6);
184  sp = tap_interface(name, interface::admin_state_t::UP, pfx, l2_address)
185  .singular();
186 
187  sp->set(hdl);
188 
189  return (sp);
190 }
191 
192 std::shared_ptr<bond_interface>
194  const vapi_payload_sw_interface_bond_details& vd)
195 {
196  std::shared_ptr<bond_interface> sp;
197  std::string name = reinterpret_cast<const char*>(vd.interface_name);
198  handle_t hdl(vd.sw_if_index);
202  sp = bond_interface::find(hdl);
203  if (sp) {
204  sp->set(mode);
205  sp->set(lb);
206  }
207  return (sp);
208 }
209 
212  const vapi_payload_sw_interface_slave_details& vd)
213 {
214  std::shared_ptr<bond_member> sp;
215  std::string name = reinterpret_cast<const char*>(vd.interface_name);
216  handle_t hdl(vd.sw_if_index);
219  bond_member::rate_t rate =
220  bond_member::rate_t::from_numeric_val(vd.is_long_timeout);
221  std::shared_ptr<interface> itf = interface::find(hdl);
222  bond_member bm(*itf, mode, rate);
223  return (bm);
224 }
225 
226 std::shared_ptr<pipe>
227 interface_factory::new_pipe_interface(const vapi_payload_pipe_details& payload)
228 {
229  std::shared_ptr<pipe> sp;
230 
231  handle_t hdl(payload.sw_if_index);
232  pipe::handle_pair_t hdl_pair(payload.pipe_sw_if_index[0],
233  payload.pipe_sw_if_index[1]);
234 
235  sp = pipe(payload.instance, interface::admin_state_t::UP).singular();
236 
237  sp->set(hdl);
238  sp->set_ends(hdl_pair);
239 
240  return (sp);
241 }
242 
243 }; // namespace VOM
244 
245 /*
246  * fd.io coding-style-patch-verification: ON
247  *
248  * Local Variables:
249  * eval: (c-set-style "mozilla")
250  * End:
251  */
A member interface mode.
Definition: bond_member.hpp:32
typedef address
Definition: ip_types.api:83
A Pipe interface.
Definition: pipe.hpp:31
A tap-interface.
static const type_t AFPACKET
AF-Packet interface type.
Definition: interface.hpp:88
static const type_t BOND
bond interface type
Definition: interface.hpp:111
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
A Sub-interface.
static std::shared_ptr< pipe > new_pipe_interface(const vapi_payload_pipe_details &payload)
static const type_t PIPE
pipe-parent type
Definition: interface.hpp:116
vhost_vring_addr_t addr
Definition: vhost_user.h:147
A bond-member.
Definition: bond_member.hpp:26
static const lb_t from_numeric_val(uint8_t v)
Convert VPP&#39;s value of the bond to a lb.
A bond interface load balance.
Type def of a L2 address as read from VPP.
Definition: types.hpp:339
static std::shared_ptr< interface > new_af_packet_interface(const vapi_payload_af_packet_details &vd)
vhost_vring_state_t state
Definition: vhost_user.h:146
vl_api_fib_path_type_t type
Definition: fib_types.api:123
static std::shared_ptr< interface > new_interface(const vapi_payload_sw_interface_details &vd)
Factory method to construct a new interface from the VPP record.
static const mode_t UNSPECIFIED
Unspecified bond interface mode.
std::shared_ptr< sub_interface > singular() const
Return the matching &#39;singular instance&#39; of the sub-interface.
u8 name[64]
Definition: memclnt.api:152
static std::shared_ptr< interface > new_vhost_user_interface(const vapi_payload_sw_interface_vhost_user_details &vd)
A bond-interface.
static const mode_t from_numeric_val(uint8_t v)
Convert VPP&#39;s value of the bond to a mode.
Definition: bond_member.cpp:93
static const rate_t from_numeric_val(uint8_t v)
Convert VPP&#39;s value of the bond to a mode.
static admin_state_t from_int(uint8_t val)
Convert VPP&#39;s numerical value to enum type.
static bond_member new_bond_member_interface(const vapi_payload_sw_interface_slave_details &vd)
The admin state of the interface.
Definition: interface.hpp:138
static std::shared_ptr< tap_interface > new_tap_interface(const vapi_payload_sw_interface_tap_v2_details &vd)
A representation of an interface in VPP.
Definition: interface.hpp:41
A type declaration of an interface handle in VPP.
Definition: types.hpp:233
vl_api_vxlan_gbp_api_tunnel_mode_t mode
Definition: vxlan_gbp.api:44
A bond interface mode.
std::shared_ptr< interface > singular() const
Return the matching&#39;singular&#39; of the interface.
Definition: interface.cpp:526
static type_t from_string(const std::string &str)
Convert VPP&#39;s name of the interface to a type.
std::shared_ptr< bond_interface > singular() const
Return the matching &#39;singular instance&#39; of the BOND interface.
static const type_t UNKNOWN
Unknown type.
Definition: interface.hpp:72
std::pair< handle_t, handle_t > handle_pair_t
Definition: pipe.hpp:34
A member interface rate.
Definition: bond_member.hpp:58
An interface type.
Definition: interface.hpp:67
static const type_t VXLAN
VXLAN interface.
Definition: interface.hpp:80
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
static const admin_state_t UP
Admin UP state.
Definition: interface.hpp:147
std::shared_ptr< pipe > singular() const
Return the matching &#39;singular instance&#39; of the sub-interface.
Definition: pipe.cpp:87
static const prefix_t ZERO
The all Zeros prefix.
Definition: prefix.hpp:205
static const admin_state_t DOWN
Admin DOWN state.
Definition: interface.hpp:143
static std::shared_ptr< bond_interface > find(const handle_t &hdl)
The the singular instance of the bond interface in the DB by handle.
static const type_t TAPV2
TAPv2 interface type.
Definition: interface.hpp:101
static const mode_t from_numeric_val(uint8_t v)
Convert VPP&#39;s value of the bond to a mode.
std::shared_ptr< tap_interface > singular() const
Return the matching &#39;singular instance&#39; of the TAP interface.
static const type_t LOOPBACK
loopback interface type
Definition: interface.hpp:92
static const type_t VHOST
vhost-user interface type
Definition: interface.hpp:106
A prefix defintion.
Definition: prefix.hpp:131
static std::shared_ptr< bond_interface > new_bond_interface(const vapi_payload_sw_interface_bond_details &vd)