VOM: support for pipes
[vpp.git] / extras / vom / vom / tap_interface_cmds.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 "vom/tap_interface_cmds.hpp"
17
18 #include <vapi/tap.api.vapi.hpp>
19 #include <vapi/tapv2.api.vapi.hpp>
20
21 DEFINE_VAPI_MSG_IDS_TAP_API_JSON;
22 DEFINE_VAPI_MSG_IDS_TAPV2_API_JSON;
23
24 namespace VOM {
25 namespace tap_interface_cmds {
26 tap_create_cmd::tap_create_cmd(HW::item<handle_t>& item,
27                                const std::string& name,
28                                route::prefix_t& prefix,
29                                const l2_address_t& l2_address)
30   : interface::create_cmd<vapi::Tap_connect>(item, name)
31   , m_prefix(prefix)
32   , m_l2_address(l2_address)
33 {
34 }
35
36 rc_t
37 tap_create_cmd::issue(connection& con)
38 {
39   msg_t req(con.ctx(), std::ref(*this));
40
41   auto& payload = req.get_request().get_payload();
42   memset(payload.tap_name, 0, sizeof(payload.tap_name));
43   memcpy(payload.tap_name, m_name.c_str(),
44          std::min(m_name.length(), sizeof(payload.tap_name)));
45   if (m_prefix != route::prefix_t::ZERO) {
46     if (m_prefix.address().is_v6()) {
47       m_prefix.to_vpp(&payload.ip6_address_set, payload.ip6_address,
48                       &payload.ip6_mask_width);
49     } else {
50       m_prefix.to_vpp(&payload.ip4_address_set, payload.ip4_address,
51                       &payload.ip4_mask_width);
52       payload.ip4_address_set = 1;
53     }
54   }
55
56   if (m_l2_address != l2_address_t::ZERO) {
57     m_l2_address.to_bytes(payload.mac_address, 6);
58   } else {
59     payload.use_random_mac = 1;
60   }
61
62   VAPI_CALL(req.execute());
63
64   wait();
65   if (m_hw_item.rc() == rc_t::OK) {
66     insert_interface();
67   }
68
69   return (m_hw_item.rc());
70 }
71
72 std::string
73 tap_create_cmd::to_string() const
74 {
75   std::ostringstream s;
76   s << "tap-intf-create: " << m_hw_item.to_string()
77     << " ip-prefix:" << m_prefix.to_string();
78
79   return (s.str());
80 }
81
82 tap_delete_cmd::tap_delete_cmd(HW::item<handle_t>& item)
83   : interface::delete_cmd<vapi::Tap_delete>(item)
84 {
85 }
86
87 rc_t
88 tap_delete_cmd::issue(connection& con)
89 {
90   msg_t req(con.ctx(), std::ref(*this));
91
92   auto& payload = req.get_request().get_payload();
93   payload.sw_if_index = m_hw_item.data().value();
94
95   VAPI_CALL(req.execute());
96
97   wait();
98   m_hw_item.set(rc_t::NOOP);
99
100   remove_interface();
101   return rc_t::OK;
102 }
103
104 std::string
105 tap_delete_cmd::to_string() const
106 {
107   std::ostringstream s;
108   s << "tap-itf-delete: " << m_hw_item.to_string();
109
110   return (s.str());
111 }
112
113 tap_dump_cmd::tap_dump_cmd()
114 {
115 }
116
117 bool
118 tap_dump_cmd::operator==(const tap_dump_cmd& other) const
119 {
120   return (true);
121 }
122
123 rc_t
124 tap_dump_cmd::issue(connection& con)
125 {
126   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
127
128   VAPI_CALL(m_dump->execute());
129
130   wait();
131
132   return rc_t::OK;
133 }
134
135 std::string
136 tap_dump_cmd::to_string() const
137 {
138   return ("tap-itf-dump");
139 }
140
141 /*
142  * TAPV2
143  */
144 tapv2_create_cmd::tapv2_create_cmd(HW::item<handle_t>& item,
145                                    const std::string& name,
146                                    route::prefix_t& prefix,
147                                    const l2_address_t& l2_address)
148   : interface::create_cmd<vapi::Tap_create_v2>(item, name)
149   , m_prefix(prefix)
150   , m_l2_address(l2_address)
151 {
152 }
153
154 rc_t
155 tapv2_create_cmd::issue(connection& con)
156 {
157   msg_t req(con.ctx(), std::ref(*this));
158
159   auto& payload = req.get_request().get_payload();
160   memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
161   memcpy(payload.host_if_name, m_name.c_str(),
162          std::min(m_name.length(), sizeof(payload.host_if_name)));
163   payload.host_if_name_set = 1;
164
165   if (m_prefix != route::prefix_t::ZERO) {
166     if (m_prefix.address().is_v6()) {
167       m_prefix.to_vpp(&payload.host_ip6_addr_set, payload.host_ip6_addr,
168                       &payload.host_ip6_prefix_len);
169     } else {
170       m_prefix.to_vpp(&payload.host_ip4_addr_set, payload.host_ip4_addr,
171                       &payload.host_ip4_prefix_len);
172       payload.host_ip4_addr_set = 1;
173     }
174   }
175
176   if (m_l2_address != l2_address_t::ZERO) {
177     m_l2_address.to_bytes(payload.host_mac_addr, 6);
178     payload.host_mac_addr_set = 1;
179   }
180
181   payload.id = 0xffffffff;
182   payload.use_random_mac = 1;
183   payload.tx_ring_sz = 1024;
184   payload.rx_ring_sz = 1024;
185
186   VAPI_CALL(req.execute());
187
188   m_hw_item = wait();
189   if (m_hw_item.rc() == rc_t::OK) {
190     insert_interface();
191   }
192
193   return rc_t::OK;
194 }
195
196 std::string
197 tapv2_create_cmd::to_string() const
198 {
199   std::ostringstream s;
200   s << "tapv2-intf-create: " << m_hw_item.to_string()
201     << " ip-prefix:" << m_prefix.to_string();
202
203   return (s.str());
204 }
205
206 tapv2_delete_cmd::tapv2_delete_cmd(HW::item<handle_t>& item)
207   : interface::delete_cmd<vapi::Tap_delete_v2>(item)
208 {
209 }
210
211 rc_t
212 tapv2_delete_cmd::issue(connection& con)
213 {
214
215   msg_t req(con.ctx(), std::ref(*this));
216
217   auto& payload = req.get_request().get_payload();
218
219   payload.sw_if_index = m_hw_item.data().value();
220
221   VAPI_CALL(req.execute());
222
223   wait();
224   m_hw_item.set(rc_t::NOOP);
225
226   remove_interface();
227   return rc_t::OK;
228 }
229 std::string
230 tapv2_delete_cmd::to_string() const
231 {
232   std::ostringstream s;
233   s << "tapv2-itf-delete: " << m_hw_item.to_string();
234
235   return (s.str());
236 }
237
238 tapv2_dump_cmd::tapv2_dump_cmd()
239 {
240 }
241
242 bool
243 tapv2_dump_cmd::operator==(const tapv2_dump_cmd& other) const
244 {
245   return (true);
246 }
247
248 rc_t
249 tapv2_dump_cmd::issue(connection& con)
250 {
251   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
252
253   VAPI_CALL(m_dump->execute());
254
255   wait();
256
257   return rc_t::OK;
258 }
259
260 std::string
261 tapv2_dump_cmd::to_string() const
262 {
263   return ("tapv2-itf-dump");
264 }
265
266 } // namespace tap_interface_cmds
267 } // namespace VOM
268
269 /*
270  * fd.io coding-style-patch-verification: ON
271  *
272  * Local Variables:
273  * eval: (c-set-style "mozilla")
274  * End:
275  */