1812ed39fSNeale Ranns/*
2812ed39fSNeale Ranns * Copyright (c) 2017 Cisco and/or its affiliates.
3812ed39fSNeale Ranns * Licensed under the Apache License, Version 2.0 (the "License");
4812ed39fSNeale Ranns * you may not use this file except in compliance with the License.
5812ed39fSNeale Ranns * You may obtain a copy of the License at:
6812ed39fSNeale Ranns *
7812ed39fSNeale Ranns *     http://www.apache.org/licenses/LICENSE-2.0
8812ed39fSNeale Ranns *
9812ed39fSNeale Ranns * Unless required by applicable law or agreed to in writing, software
10812ed39fSNeale Ranns * distributed under the License is distributed on an "AS IS" BASIS,
11812ed39fSNeale Ranns * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12812ed39fSNeale Ranns * See the License for the specific language governing permissions and
13812ed39fSNeale Ranns * limitations under the License.
14812ed39fSNeale Ranns */
15812ed39fSNeale Ranns
169ef1c0adSNeale Ranns#include "vom/tap_interface_cmds.hpp"
17812ed39fSNeale Ranns
189aca7b5eSMohsin Kazmi#include <vapi/tapv2.api.vapi.hpp>
199aca7b5eSMohsin Kazmi
209aca7b5eSMohsin KazmiDEFINE_VAPI_MSG_IDS_TAPV2_API_JSON;
21812ed39fSNeale Ranns
22812ed39fSNeale Rannsnamespace VOM {
239ef1c0adSNeale Rannsnamespace tap_interface_cmds {
249aca7b5eSMohsin Kazmi/*
259aca7b5eSMohsin Kazmi * TAPV2
269aca7b5eSMohsin Kazmi */
279aca7b5eSMohsin Kazmitapv2_create_cmd::tapv2_create_cmd(HW::item<handle_t>& item,
289aca7b5eSMohsin Kazmi                                   const std::string& name,
298e1cc46bSNeale Ranns                                   const route::prefix_t& prefix,
309aca7b5eSMohsin Kazmi                                   const l2_address_t& l2_address)
319aca7b5eSMohsin Kazmi  : interface::create_cmd<vapi::Tap_create_v2>(item, name)
329aca7b5eSMohsin Kazmi  , m_prefix(prefix)
339aca7b5eSMohsin Kazmi  , m_l2_address(l2_address)
345de4fb70SJakub Grajciar{}
359aca7b5eSMohsin Kazmi
369aca7b5eSMohsin Kazmirc_t
379aca7b5eSMohsin Kazmitapv2_create_cmd::issue(connection& con)
389aca7b5eSMohsin Kazmi{
399aca7b5eSMohsin Kazmi  msg_t req(con.ctx(), std::ref(*this));
409aca7b5eSMohsin Kazmi
419aca7b5eSMohsin Kazmi  auto& payload = req.get_request().get_payload();
429aca7b5eSMohsin Kazmi  memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
435de4fb70SJakub Grajciar  memcpy(payload.host_if_name,
445de4fb70SJakub Grajciar         m_name.c_str(),
459aca7b5eSMohsin Kazmi         std::min(m_name.length(), sizeof(payload.host_if_name)));
469aca7b5eSMohsin Kazmi  payload.host_if_name_set = 1;
479aca7b5eSMohsin Kazmi
489aca7b5eSMohsin Kazmi  if (m_prefix != route::prefix_t::ZERO) {
499aca7b5eSMohsin Kazmi    if (m_prefix.address().is_v6()) {
505de4fb70SJakub Grajciar      m_prefix.to_vpp((uint8_t*)&payload.host_ip6_prefix_set,
515de4fb70SJakub Grajciar                      payload.host_ip6_prefix.address,
525de4fb70SJakub Grajciar                      &payload.host_ip6_prefix.len);
539aca7b5eSMohsin Kazmi    } else {
545de4fb70SJakub Grajciar      m_prefix.to_vpp((uint8_t*)&payload.host_ip4_prefix_set,
555de4fb70SJakub Grajciar                      payload.host_ip4_prefix.address,
565de4fb70SJakub Grajciar                      &payload.host_ip4_prefix.len);
575de4fb70SJakub Grajciar      payload.host_ip4_prefix_set = 1;
589aca7b5eSMohsin Kazmi    }
599aca7b5eSMohsin Kazmi  }
609aca7b5eSMohsin Kazmi
619aca7b5eSMohsin Kazmi  if (m_l2_address != l2_address_t::ZERO) {
629aca7b5eSMohsin Kazmi    m_l2_address.to_bytes(payload.host_mac_addr, 6);
639aca7b5eSMohsin Kazmi    payload.host_mac_addr_set = 1;
649aca7b5eSMohsin Kazmi  }
659aca7b5eSMohsin Kazmi
669aca7b5eSMohsin Kazmi  payload.id = 0xffffffff;
679aca7b5eSMohsin Kazmi  payload.use_random_mac = 1;
689aca7b5eSMohsin Kazmi  payload.tx_ring_sz = 1024;
699aca7b5eSMohsin Kazmi  payload.rx_ring_sz = 1024;
709aca7b5eSMohsin Kazmi
719aca7b5eSMohsin Kazmi  VAPI_CALL(req.execute());
729aca7b5eSMohsin Kazmi
738e1cc46bSNeale Ranns  wait();
748e1cc46bSNeale Ranns
759aca7b5eSMohsin Kazmi  if (m_hw_item.rc() == rc_t::OK) {
769aca7b5eSMohsin Kazmi    insert_interface();
779aca7b5eSMohsin Kazmi  }
789aca7b5eSMohsin Kazmi
799aca7b5eSMohsin Kazmi  return rc_t::OK;
809aca7b5eSMohsin Kazmi}
819aca7b5eSMohsin Kazmi
829aca7b5eSMohsin Kazmistd::string
839aca7b5eSMohsin Kazmitapv2_create_cmd::to_string() const
849aca7b5eSMohsin Kazmi{
859aca7b5eSMohsin Kazmi  std::ostringstream s;
869aca7b5eSMohsin Kazmi  s << "tapv2-intf-create: " << m_hw_item.to_string()
879aca7b5eSMohsin Kazmi    << " ip-prefix:" << m_prefix.to_string();
889aca7b5eSMohsin Kazmi
899aca7b5eSMohsin Kazmi  return (s.str());
909aca7b5eSMohsin Kazmi}
919aca7b5eSMohsin Kazmi
929aca7b5eSMohsin Kazmitapv2_delete_cmd::tapv2_delete_cmd(HW::item<handle_t>& item)
939aca7b5eSMohsin Kazmi  : interface::delete_cmd<vapi::Tap_delete_v2>(item)
945de4fb70SJakub Grajciar{}
959aca7b5eSMohsin Kazmi
969aca7b5eSMohsin Kazmirc_t
979aca7b5eSMohsin Kazmitapv2_delete_cmd::issue(connection& con)
989aca7b5eSMohsin Kazmi{
999aca7b5eSMohsin Kazmi
1009aca7b5eSMohsin Kazmi  msg_t req(con.ctx(), std::ref(*this));
1019aca7b5eSMohsin Kazmi
1029aca7b5eSMohsin Kazmi  auto& payload = req.get_request().get_payload();
1039aca7b5eSMohsin Kazmi
1049aca7b5eSMohsin Kazmi  payload.sw_if_index = m_hw_item.data().value();
1059aca7b5eSMohsin Kazmi
1069aca7b5eSMohsin Kazmi  VAPI_CALL(req.execute());
1079aca7b5eSMohsin Kazmi
1089aca7b5eSMohsin Kazmi  wait();
1099aca7b5eSMohsin Kazmi  m_hw_item.set(rc_t::NOOP);
1109aca7b5eSMohsin Kazmi
1119aca7b5eSMohsin Kazmi  remove_interface();
1129aca7b5eSMohsin Kazmi  return rc_t::OK;
1139aca7b5eSMohsin Kazmi}
1149aca7b5eSMohsin Kazmistd::string
1159aca7b5eSMohsin Kazmitapv2_delete_cmd::to_string() const
1169aca7b5eSMohsin Kazmi{
1179aca7b5eSMohsin Kazmi  std::ostringstream s;
1189aca7b5eSMohsin Kazmi  s << "tapv2-itf-delete: " << m_hw_item.to_string();
1199aca7b5eSMohsin Kazmi
1209aca7b5eSMohsin Kazmi  return (s.str());
1219aca7b5eSMohsin Kazmi}
1229aca7b5eSMohsin Kazmi
1235de4fb70SJakub Grajciartapv2_dump_cmd::tapv2_dump_cmd() {}
1249aca7b5eSMohsin Kazmi
1259aca7b5eSMohsin Kazmibool
1269aca7b5eSMohsin Kazmitapv2_dump_cmd::operator==(const tapv2_dump_cmd& other) const
1279aca7b5eSMohsin Kazmi{
1289aca7b5eSMohsin Kazmi  return (true);
1299aca7b5eSMohsin Kazmi}
1309aca7b5eSMohsin Kazmi
1319aca7b5eSMohsin Kazmirc_t
1329aca7b5eSMohsin Kazmitapv2_dump_cmd::issue(connection& con)
1339aca7b5eSMohsin Kazmi{
1349aca7b5eSMohsin Kazmi  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
1359aca7b5eSMohsin Kazmi
1369aca7b5eSMohsin Kazmi  VAPI_CALL(m_dump->execute());
1379aca7b5eSMohsin Kazmi
1389aca7b5eSMohsin Kazmi  wait();
1399aca7b5eSMohsin Kazmi
1409aca7b5eSMohsin Kazmi  return rc_t::OK;
1419aca7b5eSMohsin Kazmi}
1429aca7b5eSMohsin Kazmi
1439aca7b5eSMohsin Kazmistd::string
1449aca7b5eSMohsin Kazmitapv2_dump_cmd::to_string() const
1459aca7b5eSMohsin Kazmi{
1469aca7b5eSMohsin Kazmi  return ("tapv2-itf-dump");
1479aca7b5eSMohsin Kazmi}
1489aca7b5eSMohsin Kazmi
1499ef1c0adSNeale Ranns} // namespace tap_interface_cmds
1509ef1c0adSNeale Ranns} // namespace VOM
151812ed39fSNeale Ranns
152812ed39fSNeale Ranns/*
153812ed39fSNeale Ranns * fd.io coding-style-patch-verification: ON
154812ed39fSNeale Ranns *
155812ed39fSNeale Ranns * Local Variables:
156812ed39fSNeale Ranns * eval: (c-set-style "mozilla")
157812ed39fSNeale Ranns * End:
158812ed39fSNeale Ranns */
159