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 interface::type_t type = interface::type_t::from_string(name);
35 interface::admin_state_t state =
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);
41 if (interface::type_t::UNKNOWN == type) {
45 sp = interface::find(hdl);
55 * If here, Fall back to old routine
57 if (interface::type_t::AFPACKET == type) {
59 * need to strip VPP's "host-" prefix from the interface name
61 name = name.substr(5);
64 * if the tag is set, then we wrote that to specify a name to make
65 * the interface type more specific
68 tag = std::string(reinterpret_cast<const char*>(vd.tag));
71 if (!tag.empty() && interface::type_t::LOOPBACK == type) {
73 type = interface::type_t::from_string(name);
77 * pull out the other special cases
79 if (interface::type_t::TAPV2 == type) {
83 sp = interface::find(hdl);
84 if (sp && !tag.empty())
86 } else if (interface::type_t::PIPE == type) {
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
92 } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
95 * split the name into the parent and VLAN
97 std::vector<std::string> parts;
98 std::shared_ptr<interface> parent;
99 boost::split(parts, name, boost::is_any_of("."));
101 if ((parent = interface::find(parts[0])))
102 sp = sub_interface(*parent, state, vd.sub_id).singular();
104 interface parent_itf(parts[0], type, state, tag);
105 sp = sub_interface(parent_itf, state, vd.sub_id).singular();
107 } else if (interface::type_t::VXLAN == type) {
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
113 } else if (interface::type_t::VHOST == type) {
115 * vhost interface already exist in db, look for it using
118 } else if (interface::type_t::BOND == type) {
119 sp = bond_interface(name, state, l2_address,
120 bond_interface::mode_t::UNSPECIFIED)
123 sp = interface(name, type, state, tag).singular();
128 * set the handle on the intterface - N.B. this is the sigluar instance
137 std::shared_ptr<interface>
138 interface_factory::new_vhost_user_interface(
139 const vapi_payload_sw_interface_vhost_user_details& vd)
141 std::shared_ptr<interface> sp;
142 std::string name = reinterpret_cast<const char*>(vd.sock_filename);
143 interface::type_t type = interface::type_t::from_string(name);
144 handle_t hdl(vd.sw_if_index);
146 sp = interface(name, type, interface::admin_state_t::DOWN).singular();
151 std::shared_ptr<interface>
152 interface_factory::new_af_packet_interface(
153 const vapi_payload_af_packet_details& vd)
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);
160 interface(name, interface::type_t::AFPACKET, interface::admin_state_t::DOWN)
166 std::shared_ptr<tap_interface>
167 interface_factory::new_tap_interface(
168 const vapi_payload_sw_interface_tap_v2_details& vd)
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);
173 route::prefix_t pfx(route::prefix_t::ZERO);
174 boost::asio::ip::address addr;
176 if (vd.host_ip4_prefix_len)
178 route::prefix_t(0, (uint8_t*)vd.host_ip4_addr, vd.host_ip4_prefix_len);
179 else if (vd.host_ip6_prefix_len)
181 route::prefix_t(1, (uint8_t*)vd.host_ip6_addr, vd.host_ip6_prefix_len);
183 l2_address_t l2_address(vd.host_mac_addr, 6);
184 sp = tap_interface(name, interface::admin_state_t::UP, pfx, l2_address)
192 std::shared_ptr<bond_interface>
193 interface_factory::new_bond_interface(
194 const vapi_payload_sw_interface_bond_details& vd)
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);
199 bond_interface::mode_t mode =
200 bond_interface::mode_t::from_numeric_val(vd.mode);
201 bond_interface::lb_t lb = bond_interface::lb_t::from_numeric_val(vd.lb);
202 sp = bond_interface::find(hdl);
211 interface_factory::new_bond_member_interface(
212 const vapi_payload_sw_interface_slave_details& vd)
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);
217 bond_member::mode_t mode =
218 bond_member::mode_t::from_numeric_val(vd.is_passive);
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);
226 std::shared_ptr<pipe>
227 interface_factory::new_pipe_interface(const vapi_payload_pipe_details& payload)
229 std::shared_ptr<pipe> sp;
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]);
235 sp = pipe(payload.instance, interface::admin_state_t::UP).singular();
238 sp->set_ends(hdl_pair);
246 * fd.io coding-style-patch-verification: ON
249 * eval: (c-set-style "mozilla")