deprecate tapcli
[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
37 rc_t
38 tapv2_create_cmd::issue(connection& con)
39 {
40   msg_t req(con.ctx(), std::ref(*this));
41
42   auto& payload = req.get_request().get_payload();
43   memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
44   memcpy(payload.host_if_name, 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(&payload.host_ip6_addr_set, payload.host_ip6_addr,
51                       &payload.host_ip6_prefix_len);
52     } else {
53       m_prefix.to_vpp(&payload.host_ip4_addr_set, payload.host_ip4_addr,
54                       &payload.host_ip4_prefix_len);
55       payload.host_ip4_addr_set = 1;
56     }
57   }
58
59   if (m_l2_address != l2_address_t::ZERO) {
60     m_l2_address.to_bytes(payload.host_mac_addr, 6);
61     payload.host_mac_addr_set = 1;
62   }
63
64   payload.id = 0xffffffff;
65   payload.use_random_mac = 1;
66   payload.tx_ring_sz = 1024;
67   payload.rx_ring_sz = 1024;
68
69   VAPI_CALL(req.execute());
70
71   wait();
72
73   if (m_hw_item.rc() == rc_t::OK) {
74     insert_interface();
75   }
76
77   return rc_t::OK;
78 }
79
80 std::string
81 tapv2_create_cmd::to_string() const
82 {
83   std::ostringstream s;
84   s << "tapv2-intf-create: " << m_hw_item.to_string()
85     << " ip-prefix:" << m_prefix.to_string();
86
87   return (s.str());
88 }
89
90 tapv2_delete_cmd::tapv2_delete_cmd(HW::item<handle_t>& item)
91   : interface::delete_cmd<vapi::Tap_delete_v2>(item)
92 {
93 }
94
95 rc_t
96 tapv2_delete_cmd::issue(connection& con)
97 {
98
99   msg_t req(con.ctx(), std::ref(*this));
100
101   auto& payload = req.get_request().get_payload();
102
103   payload.sw_if_index = m_hw_item.data().value();
104
105   VAPI_CALL(req.execute());
106
107   wait();
108   m_hw_item.set(rc_t::NOOP);
109
110   remove_interface();
111   return rc_t::OK;
112 }
113 std::string
114 tapv2_delete_cmd::to_string() const
115 {
116   std::ostringstream s;
117   s << "tapv2-itf-delete: " << m_hw_item.to_string();
118
119   return (s.str());
120 }
121
122 tapv2_dump_cmd::tapv2_dump_cmd()
123 {
124 }
125
126 bool
127 tapv2_dump_cmd::operator==(const tapv2_dump_cmd& other) const
128 {
129   return (true);
130 }
131
132 rc_t
133 tapv2_dump_cmd::issue(connection& con)
134 {
135   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
136
137   VAPI_CALL(m_dump->execute());
138
139   wait();
140
141   return rc_t::OK;
142 }
143
144 std::string
145 tapv2_dump_cmd::to_string() const
146 {
147   return ("tapv2-itf-dump");
148 }
149
150 } // namespace tap_interface_cmds
151 } // namespace VOM
152
153 /*
154  * fd.io coding-style-patch-verification: ON
155  *
156  * Local Variables:
157  * eval: (c-set-style "mozilla")
158  * End:
159  */