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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <boost/algorithm/string.hpp>
18 #include "vom/bond_interface.hpp"
19 #include "vom/bond_member.hpp"
20 #include "vom/interface_factory.hpp"
21 #include "vom/sub_interface.hpp"
22 #include "vom/tap_interface.hpp"
25 std::shared_ptr<interface>
26 interface_factory::new_interface(const vapi_payload_sw_interface_details& vd)
28 std::shared_ptr<interface> sp;
31 * Determine the interface type from the name and VLAN attributes
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);
36 interface::type_t type = interface::type_t::from_string(device_type);
37 interface::admin_state_t state = interface::admin_state_t::from_int(
38 vd.flags & vapi_enum_if_status_flags::IF_STATUS_API_FLAG_ADMIN_UP);
39 handle_t hdl(vd.sw_if_index);
40 l2_address_t l2_address(vd.l2_address, 6);
43 if (interface::type_t::UNKNOWN == type) {
47 sp = interface::find(hdl);
57 * If here, Fall back to old routine
59 if (interface::type_t::AFPACKET == type) {
61 * need to strip VPP's "host-" prefix from the interface name
63 name = name.substr(5);
66 * if the tag is set, then we wrote that to specify a name to make
67 * the interface type more specific
70 tag = std::string(reinterpret_cast<const char*>(vd.tag));
73 if (!tag.empty() && interface::type_t::LOOPBACK == type) {
75 type = interface::type_t::from_string(name);
79 * pull out the other special cases
81 if (interface::type_t::TAPV2 == type) {
85 sp = interface::find(hdl);
86 if (sp && !tag.empty())
88 } else if (interface::type_t::PIPE == type) {
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
94 } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
97 * split the name into the parent and VLAN
99 std::vector<std::string> parts;
100 std::shared_ptr<interface> parent;
101 boost::split(parts, name, boost::is_any_of("."));
103 if ((parent = interface::find(parts[0])))
104 sp = sub_interface(*parent, state, vd.sub_id).singular();
106 interface parent_itf(parts[0], type, state, tag);
107 sp = sub_interface(parent_itf, state, vd.sub_id).singular();
109 } else if (interface::type_t::VXLAN == type) {
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
115 } else if (interface::type_t::VHOST == type) {
117 * vhost interface already exist in db, look for it using
120 } else if (interface::type_t::BOND == type) {
122 name, state, l2_address, bond_interface::mode_t::UNSPECIFIED)
125 sp = interface(name, type, state, tag).singular();
130 * set the handle on the intterface - N.B. this is the sigluar instance
139 std::shared_ptr<interface>
140 interface_factory::new_vhost_user_interface(
141 const vapi_payload_sw_interface_vhost_user_details& vd)
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);
147 sp = interface(name, interface::type_t::VHOST, interface::admin_state_t::DOWN)
153 std::shared_ptr<interface>
154 interface_factory::new_af_packet_interface(
155 const vapi_payload_af_packet_details& vd)
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);
162 interface(name, interface::type_t::AFPACKET, interface::admin_state_t::DOWN)
168 std::shared_ptr<tap_interface>
169 interface_factory::new_tap_interface(
170 const vapi_payload_sw_interface_tap_v2_details& vd)
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);
175 route::prefix_t pfx(route::prefix_t::ZERO);
176 boost::asio::ip::address addr;
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);
185 l2_address_t l2_address(vd.host_mac_addr, 6);
186 sp = tap_interface(name, interface::admin_state_t::UP, pfx, l2_address)
194 std::shared_ptr<bond_interface>
195 interface_factory::new_bond_interface(
196 const vapi_payload_sw_interface_bond_details& vd)
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);
201 bond_interface::mode_t mode =
202 bond_interface::mode_t::from_numeric_val(vd.mode);
203 bond_interface::lb_t lb = bond_interface::lb_t::from_numeric_val(vd.lb);
204 sp = bond_interface::find(hdl);
213 interface_factory::new_bond_member_interface(
214 const vapi_payload_sw_interface_slave_details& vd)
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);
219 bond_member::mode_t mode =
220 bond_member::mode_t::from_numeric_val(vd.is_passive);
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);
228 std::shared_ptr<pipe>
229 interface_factory::new_pipe_interface(const vapi_payload_pipe_details& payload)
231 std::shared_ptr<pipe> sp;
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]);
237 sp = pipe(payload.instance, interface::admin_state_t::UP).singular();
240 sp->set_ends(hdl_pair);
248 * fd.io coding-style-patch-verification: ON
251 * eval: (c-set-style "mozilla")