blob: b8815ed7ac938020999fdc006542fc61c0ae7758 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070018#include "vom/interface_factory.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070019#include "vom/sub_interface.hpp"
20#include "vom/tap_interface.hpp"
21
22namespace VOM {
Neale Rannsa2ee0292017-11-28 22:29:13 -080023std::shared_ptr<interface>
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070024interface_factory::new_interface(const vapi_payload_sw_interface_details& vd)
Neale Ranns812ed392017-10-16 04:20:13 -070025{
Neale Rannsa2ee0292017-11-28 22:29:13 -080026 std::shared_ptr<interface> sp;
Neale Ranns812ed392017-10-16 04:20:13 -070027
28 /**
Neale Rannsa2ee0292017-11-28 22:29:13 -080029 * Determine the interface type from the name and VLAN attributes
30 */
Neale Ranns812ed392017-10-16 04:20:13 -070031 std::string name = reinterpret_cast<const char*>(vd.interface_name);
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070032 interface::type_t type = interface::type_t::from_string(name);
33 interface::admin_state_t state =
34 interface::admin_state_t::from_int(vd.link_up_down);
Neale Ranns812ed392017-10-16 04:20:13 -070035 handle_t hdl(vd.sw_if_index);
36 l2_address_t l2_address(vd.l2_address, vd.l2_address_length);
37
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070038 if (interface::type_t::AFPACKET == type) {
Neale Ranns812ed392017-10-16 04:20:13 -070039 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080040 * need to strip VPP's "host-" prefix from the interface name
41 */
Neale Ranns812ed392017-10-16 04:20:13 -070042 name = name.substr(5);
43 }
44 /**
Neale Rannsa2ee0292017-11-28 22:29:13 -080045 * if the tag is set, then we wrote that to specify a name to make
46 * the interface type more specific
47 */
Neale Ranns812ed392017-10-16 04:20:13 -070048 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 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080054 * pull out the other special cases
55 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070056 if (interface::type_t::TAP == type) {
Neale Ranns812ed392017-10-16 04:20:13 -070057 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080058 * TAP interface
59 */
60 sp = tap_interface(name, state, route::prefix_t()).singular();
Neale Ranns812ed392017-10-16 04:20:13 -070061 } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
62 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080063 * Sub-interface
64 * split the name into the parent and VLAN
65 */
Neale Ranns812ed392017-10-16 04:20:13 -070066 std::vector<std::string> parts;
67 boost::split(parts, name, boost::is_any_of("."));
68
69 interface parent(parts[0], type, state);
Neale Rannsa2ee0292017-11-28 22:29:13 -080070 sp = sub_interface(parent, state, vd.sub_id).singular();
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070071 } else if (interface::type_t::VXLAN == type) {
Neale Ranns812ed392017-10-16 04:20:13 -070072 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080073 * there's not enough information in a SW interface record to
74 * construct a VXLAN tunnel. so skip it.
75 */
Neale Ranns812ed392017-10-16 04:20:13 -070076 } else {
Neale Rannsa2ee0292017-11-28 22:29:13 -080077 sp = interface(name, type, state).singular();
78 sp->set(l2_address);
Neale Ranns812ed392017-10-16 04:20:13 -070079 }
80
Neale Rannsa2ee0292017-11-28 22:29:13 -080081 /*
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);
Neale Ranns812ed392017-10-16 04:20:13 -070088}
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070089}; // namespace VOM
Neale Ranns812ed392017-10-16 04:20:13 -070090
91/*
92 * fd.io coding-style-patch-verification: ON
93 *
94 * Local Variables:
95 * eval: (c-set-style "mozilla")
96 * End:
97 */