FD.io VPP  v18.04-17-g3a0d853
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.link_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::AFPACKET == type) {
42  /*
43  * need to strip VPP's "host-" prefix from the interface name
44  */
45  name = name.substr(5);
46  }
47  /**
48  * if the tag is set, then we wrote that to specify a name to make
49  * the interface type more specific
50  */
51  if (vd.tag[0] != 0) {
52  tag = std::string(reinterpret_cast<const char*>(vd.tag));
53  }
54 
55  if (!tag.empty() && interface::type_t::LOOPBACK == type) {
56  name = tag;
57  type = interface::type_t::from_string(name);
58  }
59 
60  /*
61  * pull out the other special cases
62  */
63  if (interface::type_t::TAP == type) {
64  /*
65  * TAP interface
66  */
67  sp = tap_interface(name, state, route::prefix_t()).singular();
68  if (sp && !tag.empty())
69  sp->set(tag);
70  } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
71  /*
72  * Sub-interface
73  * split the name into the parent and VLAN
74  */
75  std::vector<std::string> parts;
76  boost::split(parts, name, boost::is_any_of("."));
77 
78  interface parent(parts[0], type, state, tag);
79  sp = sub_interface(parent, state, vd.sub_id).singular();
80  } else if (interface::type_t::VXLAN == type) {
81  /*
82  * there's not enough information in a SW interface record to
83  * construct a VXLAN tunnel. so skip it. They have
84  * their own dump routines
85  */
86  } else if (interface::type_t::VHOST == type) {
87  /*
88  * vhost interface already exist in db, look for it using
89  * sw_if_index
90  */
91  sp = interface::find(hdl);
92  if (sp) {
93  sp->set(state);
94  sp->set(l2_address);
95  if (!tag.empty())
96  sp->set(tag);
97  }
98  } else if (interface::type_t::BOND == type) {
99  sp = bond_interface(name, state, l2_address,
101  .singular();
102  } else {
103  sp = interface(name, type, state, tag).singular();
104  sp->set(l2_address);
105  }
106 
107  /*
108  * set the handle on the intterface - N.B. this is the sigluar instance
109  * not a stack local.
110  */
111  if (sp)
112  sp->set(hdl);
113 
114  return (sp);
115 }
116 
117 std::shared_ptr<interface>
119  const vapi_payload_sw_interface_vhost_user_details& vd)
120 {
121  std::shared_ptr<interface> sp;
122  std::string name = reinterpret_cast<const char*>(vd.sock_filename);
124  handle_t hdl(vd.sw_if_index);
125 
127  sp->set(hdl);
128  return (sp);
129 }
130 
131 std::shared_ptr<bond_interface>
133  const vapi_payload_sw_interface_bond_details& vd)
134 {
135  std::shared_ptr<bond_interface> sp;
136  std::string name = reinterpret_cast<const char*>(vd.interface_name);
137  handle_t hdl(vd.sw_if_index);
141  sp = bond_interface::find(hdl);
142  if (sp) {
143  sp->set(mode);
144  sp->set(lb);
145  }
146  return (sp);
147 }
148 
151  const vapi_payload_sw_interface_slave_details& vd)
152 {
153  std::shared_ptr<bond_member> sp;
154  std::string name = reinterpret_cast<const char*>(vd.interface_name);
155  handle_t hdl(vd.sw_if_index);
156  bond_member::mode_t mode =
158  bond_member::rate_t rate =
159  bond_member::rate_t::from_numeric_val(vd.is_long_timeout);
160  std::shared_ptr<interface> itf = interface::find(hdl);
161  bond_member bm(*itf, mode, rate);
162  return (bm);
163 }
164 }; // namespace VOM
165 
166 /*
167  * fd.io coding-style-patch-verification: ON
168  *
169  * Local Variables:
170  * eval: (c-set-style "mozilla")
171  * End:
172  */
A member interface mode.
Definition: bond_member.hpp:32
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:110
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:465
A Sub-interface.
std::shared_ptr< bond_interface > singular() const
Return the matching &#39;singular instance&#39; of the BOND interface.
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:342
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
Unspecificed bond interface mode.
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:127
A representation of an interface in VPP.
Definition: interface.hpp:41
A type declaration of an interface handle in VPP.
Definition: types.hpp:236
A bond interface mode.
std::shared_ptr< interface > singular() const
Return the matching&#39;singular&#39; of the interface.
Definition: interface.cpp:453
std::shared_ptr< tap_interface > singular() const
Return the matching &#39;singular instance&#39; of the TAP interface.
static type_t from_string(const std::string &str)
Convert VPP&#39;s name of the interface to a type.
vhost_vring_state_t state
Definition: vhost-user.h:82
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 DOWN
Admin DOWN state.
Definition: interface.hpp:132
static const type_t TAP
TAP interface type.
Definition: interface.hpp:100
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 mode_t from_numeric_val(uint8_t v)
Convert VPP&#39;s value of the bond to a mode.
static const type_t LOOPBACK
loopback interface type
Definition: interface.hpp:92
static const type_t VHOST
vhost-user interface type
Definition: interface.hpp:105
std::shared_ptr< sub_interface > singular() const
Return the matching &#39;singular instance&#39; of the sub-interface.
A prefix defintion.
Definition: prefix.hpp:93
static std::shared_ptr< bond_interface > new_bond_interface(const vapi_payload_sw_interface_bond_details &vd)