blob: ef26c3293d3639f3b40276200dcf89996ffbd6a0 [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);
Neale Ranns4ef42262018-02-20 08:10:44 -080037 std::string tag = "";
Neale Ranns812ed392017-10-16 04:20:13 -070038
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070039 if (interface::type_t::AFPACKET == type) {
Neale Ranns812ed392017-10-16 04:20:13 -070040 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080041 * need to strip VPP's "host-" prefix from the interface name
42 */
Neale Ranns812ed392017-10-16 04:20:13 -070043 name = name.substr(5);
44 }
45 /**
Neale Rannsa2ee0292017-11-28 22:29:13 -080046 * if the tag is set, then we wrote that to specify a name to make
47 * the interface type more specific
48 */
Neale Ranns812ed392017-10-16 04:20:13 -070049 if (vd.tag[0] != 0) {
Neale Ranns4ef42262018-02-20 08:10:44 -080050 tag = std::string(reinterpret_cast<const char*>(vd.tag));
51 }
52
53 if (!tag.empty() && interface::type_t::LOOPBACK == type) {
54 name = tag;
Neale Ranns812ed392017-10-16 04:20:13 -070055 type = interface::type_t::from_string(name);
56 }
57
58 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080059 * pull out the other special cases
60 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070061 if (interface::type_t::TAP == type) {
Neale Ranns812ed392017-10-16 04:20:13 -070062 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080063 * TAP interface
64 */
65 sp = tap_interface(name, state, route::prefix_t()).singular();
Neale Ranns4ef42262018-02-20 08:10:44 -080066 if (sp && !tag.empty())
67 sp->set(tag);
Neale Ranns812ed392017-10-16 04:20:13 -070068 } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
69 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080070 * Sub-interface
71 * split the name into the parent and VLAN
72 */
Neale Ranns812ed392017-10-16 04:20:13 -070073 std::vector<std::string> parts;
74 boost::split(parts, name, boost::is_any_of("."));
75
Neale Ranns4ef42262018-02-20 08:10:44 -080076 interface parent(parts[0], type, state, tag);
Neale Rannsa2ee0292017-11-28 22:29:13 -080077 sp = sub_interface(parent, state, vd.sub_id).singular();
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070078 } else if (interface::type_t::VXLAN == type) {
Neale Ranns812ed392017-10-16 04:20:13 -070079 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -080080 * there's not enough information in a SW interface record to
Neale Ranns4ef42262018-02-20 08:10:44 -080081 * construct a VXLAN tunnel. so skip it. They have
82 * their own dump routines
Neale Rannsa2ee0292017-11-28 22:29:13 -080083 */
Neale Ranns4ef42262018-02-20 08:10:44 -080084 } else if (interface::type_t::VHOST == type) {
85 /*
86 * vhost interfaces already exist in db, look for it using
87 * sw_if_index
88 */
89 sp = interface::find(hdl);
90 if (sp) {
91 sp->set(state);
92 sp->set(l2_address);
93 if (!tag.empty())
94 sp->set(tag);
95 }
Neale Ranns812ed392017-10-16 04:20:13 -070096 } else {
Neale Ranns4ef42262018-02-20 08:10:44 -080097 sp = interface(name, type, state, tag).singular();
Neale Rannsa2ee0292017-11-28 22:29:13 -080098 sp->set(l2_address);
Neale Ranns812ed392017-10-16 04:20:13 -070099 }
100
Neale Rannsa2ee0292017-11-28 22:29:13 -0800101 /*
102 * set the handle on the intterface - N.B. this is the sigluar instance
103 * not a stack local.
104 */
Neale Ranns4ef42262018-02-20 08:10:44 -0800105 if (sp)
106 sp->set(hdl);
Neale Rannsa2ee0292017-11-28 22:29:13 -0800107
108 return (sp);
Neale Ranns812ed392017-10-16 04:20:13 -0700109}
Neale Ranns4ef42262018-02-20 08:10:44 -0800110
111std::shared_ptr<interface>
112interface_factory::new_vhost_user_interface(
113 const vapi_payload_sw_interface_vhost_user_details& vd)
114{
115 std::shared_ptr<interface> sp;
116 std::string name = reinterpret_cast<const char*>(vd.sock_filename);
117 interface::type_t type = interface::type_t::from_string(name);
118 handle_t hdl(vd.sw_if_index);
119
120 sp = interface(name, type, interface::admin_state_t::DOWN).singular();
121 sp->set(hdl);
122 return (sp);
123}
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700124}; // namespace VOM
Neale Ranns812ed392017-10-16 04:20:13 -0700125
126/*
127 * fd.io coding-style-patch-verification: ON
128 *
129 * Local Variables:
130 * eval: (c-set-style "mozilla")
131 * End:
132 */