VOM: support for pipes
[vpp.git] / extras / vom / vom / interface_factory.cpp
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
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"
23
24 namespace VOM {
25 std::shared_ptr<interface>
26 interface_factory::new_interface(const vapi_payload_sw_interface_details& vd)
27 {
28   std::shared_ptr<interface> sp;
29
30   /**
31    * Determine the interface type from the name and VLAN attributes
32    */
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);
39   std::string tag = "";
40
41   sp = interface::find(hdl);
42   if (sp) {
43     sp->set(state);
44     sp->set(l2_address);
45     if (!tag.empty())
46       sp->set(tag);
47     return sp;
48   }
49
50   /*
51    * If here, Fall back to old routine
52    */
53   if (interface::type_t::AFPACKET == type) {
54     /*
55      * need to strip VPP's "host-" prefix from the interface name
56      */
57     name = name.substr(5);
58   }
59   /**
60    * if the tag is set, then we wrote that to specify a name to make
61    * the interface type more specific
62    */
63   if (vd.tag[0] != 0) {
64     tag = std::string(reinterpret_cast<const char*>(vd.tag));
65   }
66
67   if (!tag.empty() && interface::type_t::LOOPBACK == type) {
68     name = tag;
69     type = interface::type_t::from_string(name);
70   }
71
72   /*
73    * pull out the other special cases
74    */
75   if (interface::type_t::TAP == type || interface::type_t::TAPV2 == type) {
76     /*
77      * TAP interfaces
78      */
79     sp = interface::find(hdl);
80     if (sp && !tag.empty())
81       sp->set(tag);
82   } else if (interface::type_t::PIPE == type) {
83     /*
84      * there's not enough information in a SW interface record to
85      * construct a pipe. so skip it. They have
86      * their own dump routines
87      */
88   } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
89     /*
90      * Sub-interface
91      *   split the name into the parent and VLAN
92      */
93     std::vector<std::string> parts;
94     std::shared_ptr<interface> parent;
95     boost::split(parts, name, boost::is_any_of("."));
96
97     if ((parent = interface::find(parts[0])))
98       sp = sub_interface(*parent, state, vd.sub_id).singular();
99     else {
100       interface parent_itf(parts[0], type, state, tag);
101       sp = sub_interface(parent_itf, state, vd.sub_id).singular();
102     }
103   } else if (interface::type_t::VXLAN == type) {
104     /*
105      * there's not enough information in a SW interface record to
106      * construct a VXLAN tunnel. so skip it. They have
107      * their own dump routines
108      */
109   } else if (interface::type_t::VHOST == type) {
110     /*
111      * vhost interface already exist in db, look for it using
112      * sw_if_index
113      */
114   } else if (interface::type_t::BOND == type) {
115     sp = bond_interface(name, state, l2_address,
116                         bond_interface::mode_t::UNSPECIFIED)
117            .singular();
118   } else {
119     sp = interface(name, type, state, tag).singular();
120     sp->set(l2_address);
121   }
122
123   /*
124    * set the handle on the intterface - N.B. this is the sigluar instance
125    * not a stack local.
126    */
127   if (sp)
128     sp->set(hdl);
129
130   return (sp);
131 }
132
133 std::shared_ptr<interface>
134 interface_factory::new_vhost_user_interface(
135   const vapi_payload_sw_interface_vhost_user_details& vd)
136 {
137   std::shared_ptr<interface> sp;
138   std::string name = reinterpret_cast<const char*>(vd.sock_filename);
139   interface::type_t type = interface::type_t::from_string(name);
140   handle_t hdl(vd.sw_if_index);
141
142   sp = interface(name, type, interface::admin_state_t::DOWN).singular();
143   sp->set(hdl);
144   return (sp);
145 }
146
147 std::shared_ptr<interface>
148 interface_factory::new_af_packet_interface(
149   const vapi_payload_af_packet_details& vd)
150 {
151   std::shared_ptr<interface> sp;
152   std::string name = reinterpret_cast<const char*>(vd.host_if_name);
153   handle_t hdl(vd.sw_if_index);
154
155   sp =
156     interface(name, interface::type_t::AFPACKET, interface::admin_state_t::DOWN)
157       .singular();
158   sp->set(hdl);
159   return (sp);
160 }
161
162 std::shared_ptr<tap_interface>
163 interface_factory::new_tap_interface(
164   const vapi_payload_sw_interface_tap_details& vd)
165 {
166   std::shared_ptr<tap_interface> sp;
167   std::string name = reinterpret_cast<const char*>(vd.dev_name);
168   handle_t hdl(vd.sw_if_index);
169
170   sp = tap_interface(name, interface::type_t::TAP, interface::admin_state_t::UP,
171                      route::prefix_t::ZERO)
172          .singular();
173   sp->set(hdl);
174   return (sp);
175 }
176
177 std::shared_ptr<tap_interface>
178 interface_factory::new_tap_v2_interface(
179   const vapi_payload_sw_interface_tap_v2_details& vd)
180 {
181   std::shared_ptr<tap_interface> sp;
182   handle_t hdl(vd.sw_if_index);
183   std::string name = reinterpret_cast<const char*>(vd.host_if_name);
184   route::prefix_t pfx(route::prefix_t::ZERO);
185   boost::asio::ip::address addr;
186
187   if (vd.host_ip4_prefix_len)
188     pfx =
189       route::prefix_t(0, (uint8_t*)vd.host_ip4_addr, vd.host_ip4_prefix_len);
190   else if (vd.host_ip6_prefix_len)
191     pfx =
192       route::prefix_t(1, (uint8_t*)vd.host_ip6_addr, vd.host_ip6_prefix_len);
193
194   l2_address_t l2_address(vd.host_mac_addr, 6);
195   sp = tap_interface(name, interface::type_t::TAPV2,
196                      interface::admin_state_t::UP, pfx, l2_address)
197          .singular();
198
199   sp->set(hdl);
200
201   return (sp);
202 }
203
204 std::shared_ptr<bond_interface>
205 interface_factory::new_bond_interface(
206   const vapi_payload_sw_interface_bond_details& vd)
207 {
208   std::shared_ptr<bond_interface> sp;
209   std::string name = reinterpret_cast<const char*>(vd.interface_name);
210   handle_t hdl(vd.sw_if_index);
211   bond_interface::mode_t mode =
212     bond_interface::mode_t::from_numeric_val(vd.mode);
213   bond_interface::lb_t lb = bond_interface::lb_t::from_numeric_val(vd.lb);
214   sp = bond_interface::find(hdl);
215   if (sp) {
216     sp->set(mode);
217     sp->set(lb);
218   }
219   return (sp);
220 }
221
222 bond_member
223 interface_factory::new_bond_member_interface(
224   const vapi_payload_sw_interface_slave_details& vd)
225 {
226   std::shared_ptr<bond_member> sp;
227   std::string name = reinterpret_cast<const char*>(vd.interface_name);
228   handle_t hdl(vd.sw_if_index);
229   bond_member::mode_t mode =
230     bond_member::mode_t::from_numeric_val(vd.is_passive);
231   bond_member::rate_t rate =
232     bond_member::rate_t::from_numeric_val(vd.is_long_timeout);
233   std::shared_ptr<interface> itf = interface::find(hdl);
234   bond_member bm(*itf, mode, rate);
235   return (bm);
236 }
237
238 std::shared_ptr<pipe>
239 interface_factory::new_pipe_interface(const vapi_payload_pipe_details& payload)
240 {
241   std::shared_ptr<pipe> sp;
242
243   handle_t hdl(payload.sw_if_index);
244   pipe::handle_pair_t hdl_pair(payload.pipe_sw_if_index[0],
245                                payload.pipe_sw_if_index[1]);
246
247   sp = pipe(payload.instance, interface::admin_state_t::UP).singular();
248
249   sp->set(hdl);
250   sp->set_ends(hdl_pair);
251
252   return (sp);
253 }
254
255 }; // namespace VOM
256
257 /*
258  * fd.io coding-style-patch-verification: ON
259  *
260  * Local Variables:
261  * eval: (c-set-style "mozilla")
262  * End:
263  */