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