devices: tap API cleanup
[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/tapv2.api.vapi.hpp>
19
20 DEFINE_VAPI_MSG_IDS_TAPV2_API_JSON;
21
22 namespace VOM {
23 namespace tap_interface_cmds {
24 /*
25  * TAPV2
26  */
27 tapv2_create_cmd::tapv2_create_cmd(HW::item<handle_t>& item,
28                                    const std::string& name,
29                                    const route::prefix_t& prefix,
30                                    const l2_address_t& l2_address)
31   : interface::create_cmd<vapi::Tap_create_v2>(item, name)
32   , m_prefix(prefix)
33   , m_l2_address(l2_address)
34 {}
35
36 rc_t
37 tapv2_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.host_if_name, 0, sizeof(payload.host_if_name));
43   memcpy(payload.host_if_name,
44          m_name.c_str(),
45          std::min(m_name.length(), sizeof(payload.host_if_name)));
46   payload.host_if_name_set = 1;
47
48   if (m_prefix != route::prefix_t::ZERO) {
49     if (m_prefix.address().is_v6()) {
50       m_prefix.to_vpp((uint8_t*)&payload.host_ip6_prefix_set,
51                       payload.host_ip6_prefix.address,
52                       &payload.host_ip6_prefix.len);
53     } else {
54       m_prefix.to_vpp((uint8_t*)&payload.host_ip4_prefix_set,
55                       payload.host_ip4_prefix.address,
56                       &payload.host_ip4_prefix.len);
57       payload.host_ip4_prefix_set = 1;
58     }
59   }
60
61   if (m_l2_address != l2_address_t::ZERO) {
62     m_l2_address.to_bytes(payload.host_mac_addr, 6);
63     payload.host_mac_addr_set = 1;
64   }
65
66   payload.id = 0xffffffff;
67   payload.use_random_mac = 1;
68   payload.tx_ring_sz = 1024;
69   payload.rx_ring_sz = 1024;
70
71   VAPI_CALL(req.execute());
72
73   wait();
74
75   if (m_hw_item.rc() == rc_t::OK) {
76     insert_interface();
77   }
78
79   return rc_t::OK;
80 }
81
82 std::string
83 tapv2_create_cmd::to_string() const
84 {
85   std::ostringstream s;
86   s << "tapv2-intf-create: " << m_hw_item.to_string()
87     << " ip-prefix:" << m_prefix.to_string();
88
89   return (s.str());
90 }
91
92 tapv2_delete_cmd::tapv2_delete_cmd(HW::item<handle_t>& item)
93   : interface::delete_cmd<vapi::Tap_delete_v2>(item)
94 {}
95
96 rc_t
97 tapv2_delete_cmd::issue(connection& con)
98 {
99
100   msg_t req(con.ctx(), std::ref(*this));
101
102   auto& payload = req.get_request().get_payload();
103
104   payload.sw_if_index = m_hw_item.data().value();
105
106   VAPI_CALL(req.execute());
107
108   wait();
109   m_hw_item.set(rc_t::NOOP);
110
111   remove_interface();
112   return rc_t::OK;
113 }
114 std::string
115 tapv2_delete_cmd::to_string() const
116 {
117   std::ostringstream s;
118   s << "tapv2-itf-delete: " << m_hw_item.to_string();
119
120   return (s.str());
121 }
122
123 tapv2_dump_cmd::tapv2_dump_cmd() {}
124
125 bool
126 tapv2_dump_cmd::operator==(const tapv2_dump_cmd& other) const
127 {
128   return (true);
129 }
130
131 rc_t
132 tapv2_dump_cmd::issue(connection& con)
133 {
134   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
135
136   VAPI_CALL(m_dump->execute());
137
138   wait();
139
140   return rc_t::OK;
141 }
142
143 std::string
144 tapv2_dump_cmd::to_string() const
145 {
146   return ("tapv2-itf-dump");
147 }
148
149 } // namespace tap_interface_cmds
150 } // namespace VOM
151
152 /*
153  * fd.io coding-style-patch-verification: ON
154  *
155  * Local Variables:
156  * eval: (c-set-style "mozilla")
157  * End:
158  */