vom: Add support for af-packet dump
[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) {
76     /*
77      * TAP interface
78      */
79     sp = tap_interface(name, state, route::prefix_t()).singular();
80     if (sp && !tag.empty())
81       sp->set(tag);
82   } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) {
83     /*
84      * Sub-interface
85      *   split the name into the parent and VLAN
86      */
87     std::vector<std::string> parts;
88     std::shared_ptr<interface> parent;
89     boost::split(parts, name, boost::is_any_of("."));
90
91     if ((parent = interface::find(parts[0])))
92       sp = sub_interface(*parent, state, vd.sub_id).singular();
93     else {
94       interface parent_itf(parts[0], type, state, tag);
95       sp = sub_interface(parent_itf, state, vd.sub_id).singular();
96     }
97   } else if (interface::type_t::VXLAN == type) {
98     /*
99      * there's not enough information in a SW interface record to
100      * construct a VXLAN tunnel. so skip it. They have
101      * their own dump routines
102      */
103   } else if (interface::type_t::VHOST == type) {
104     /*
105      * vhost interface already exist in db, look for it using
106      * sw_if_index
107      */
108   } else if (interface::type_t::BOND == type) {
109     sp = bond_interface(name, state, l2_address,
110                         bond_interface::mode_t::UNSPECIFIED)
111            .singular();
112   } else {
113     sp = interface(name, type, state, tag).singular();
114     sp->set(l2_address);
115   }
116
117   /*
118    * set the handle on the intterface - N.B. this is the sigluar instance
119    * not a stack local.
120    */
121   if (sp)
122     sp->set(hdl);
123
124   return (sp);
125 }
126
127 std::shared_ptr<interface>
128 interface_factory::new_vhost_user_interface(
129   const vapi_payload_sw_interface_vhost_user_details& vd)
130 {
131   std::shared_ptr<interface> sp;
132   std::string name = reinterpret_cast<const char*>(vd.sock_filename);
133   interface::type_t type = interface::type_t::from_string(name);
134   handle_t hdl(vd.sw_if_index);
135
136   sp = interface(name, type, interface::admin_state_t::DOWN).singular();
137   sp->set(hdl);
138   return (sp);
139 }
140
141 std::shared_ptr<interface>
142 interface_factory::new_af_packet_interface(
143   const vapi_payload_af_packet_details& vd)
144 {
145   std::shared_ptr<interface> sp;
146   std::string name = reinterpret_cast<const char*>(vd.host_if_name);
147   handle_t hdl(vd.sw_if_index);
148
149   sp =
150     interface(name, interface::type_t::AFPACKET, interface::admin_state_t::DOWN)
151       .singular();
152   sp->set(hdl);
153   return (sp);
154 }
155
156 std::shared_ptr<bond_interface>
157 interface_factory::new_bond_interface(
158   const vapi_payload_sw_interface_bond_details& vd)
159 {
160   std::shared_ptr<bond_interface> sp;
161   std::string name = reinterpret_cast<const char*>(vd.interface_name);
162   handle_t hdl(vd.sw_if_index);
163   bond_interface::mode_t mode =
164     bond_interface::mode_t::from_numeric_val(vd.mode);
165   bond_interface::lb_t lb = bond_interface::lb_t::from_numeric_val(vd.lb);
166   sp = bond_interface::find(hdl);
167   if (sp) {
168     sp->set(mode);
169     sp->set(lb);
170   }
171   return (sp);
172 }
173
174 bond_member
175 interface_factory::new_bond_member_interface(
176   const vapi_payload_sw_interface_slave_details& vd)
177 {
178   std::shared_ptr<bond_member> sp;
179   std::string name = reinterpret_cast<const char*>(vd.interface_name);
180   handle_t hdl(vd.sw_if_index);
181   bond_member::mode_t mode =
182     bond_member::mode_t::from_numeric_val(vd.is_passive);
183   bond_member::rate_t rate =
184     bond_member::rate_t::from_numeric_val(vd.is_long_timeout);
185   std::shared_ptr<interface> itf = interface::find(hdl);
186   bond_member bm(*itf, mode, rate);
187   return (bm);
188 }
189 }; // namespace VOM
190
191 /*
192  * fd.io coding-style-patch-verification: ON
193  *
194  * Local Variables:
195  * eval: (c-set-style "mozilla")
196  * End:
197  */