FD.io VPP  v18.01.2-1-g9b554f3
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 
19 #include "vom/sub_interface.hpp"
20 #include "vom/tap_interface.hpp"
21 
22 namespace VOM {
23 std::shared_ptr<interface>
24 interface_factory::new_interface(const vapi_payload_sw_interface_details& vd)
25 {
26  std::shared_ptr<interface> sp;
27 
28  /**
29  * Determine the interface type from the name and VLAN attributes
30  */
31  std::string name = reinterpret_cast<const char*>(vd.interface_name);
34  interface::admin_state_t::from_int(vd.link_up_down);
35  handle_t hdl(vd.sw_if_index);
36  l2_address_t l2_address(vd.l2_address, vd.l2_address_length);
37 
38  if (interface::type_t::AFPACKET == type) {
39  /*
40  * need to strip VPP's "host-" prefix from the interface name
41  */
42  name = name.substr(5);
43  }
44  /**
45  * if the tag is set, then we wrote that to specify a name to make
46  * the interface type more specific
47  */
48  if (vd.tag[0] != 0) {
49  name = std::string(reinterpret_cast<const char*>(vd.tag));
50  type = interface::type_t::from_string(name);
51  }
52 
53  /*
54  * pull out the other special cases
55  */
56  if (interface::type_t::TAP == type) {
57  /*
58  * TAP interface
59  */
60  sp = tap_interface(name, state, route::prefix_t()).singular();
61  } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
62  /*
63  * Sub-interface
64  * split the name into the parent and VLAN
65  */
66  std::vector<std::string> parts;
67  boost::split(parts, name, boost::is_any_of("."));
68 
69  interface parent(parts[0], type, state);
70  sp = sub_interface(parent, state, vd.sub_id).singular();
71  } else if (interface::type_t::VXLAN == type) {
72  /*
73  * there's not enough information in a SW interface record to
74  * construct a VXLAN tunnel. so skip it.
75  */
76  } else {
77  sp = interface(name, type, state).singular();
78  sp->set(l2_address);
79  }
80 
81  /*
82  * set the handle on the intterface - N.B. this is the sigluar instance
83  * not a stack local.
84  */
85  sp->set(hdl);
86 
87  return (sp);
88 }
89 }; // namespace VOM
90 
91 /*
92  * fd.io coding-style-patch-verification: ON
93  *
94  * Local Variables:
95  * eval: (c-set-style "mozilla")
96  * End:
97  */
A tap-interface.
static const type_t AFPACKET
AF-Packet interface type.
Definition: interface.hpp:79
A Sub-interface.
Type def of a L2 address as read from VPP.
Definition: types.hpp:265
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 admin_state_t from_int(uint8_t val)
Convert VPP&#39;s numerical value to enum type.
The admin state of the interface.
Definition: interface.hpp:108
A representation of an interface in VPP.
Definition: interface.hpp:41
A type declaration of an interface handle in VPP.
Definition: types.hpp:164
std::shared_ptr< interface > singular() const
Return the matching&#39;singular&#39; of the interface.
Definition: interface.cpp:390
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
An interface type.
Definition: interface.hpp:58
static const type_t VXLAN
VXLAN interface.
Definition: interface.hpp:71
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
static const type_t TAP
TAP interface type.
Definition: interface.hpp:91
std::shared_ptr< sub_interface > singular() const
Return the matching &#39;singular instance&#39; of the sub-interface.
A prefix defintion.
Definition: prefix.hpp:91