Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame^] | 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.hpp" |
| 17 | #include "vom/cmd.hpp" |
| 18 | |
| 19 | #include <vapi/tap.api.vapi.hpp> |
| 20 | |
| 21 | namespace VOM { |
| 22 | tap_interface::create_cmd::create_cmd(HW::item<handle_t>& item, |
| 23 | const std::string& name, |
| 24 | route::prefix_t& prefix, |
| 25 | const l2_address_t& l2_address) |
| 26 | : interface::create_cmd<vapi::Tap_connect>(item, name) |
| 27 | , m_prefix(prefix) |
| 28 | , m_l2_address(l2_address) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | rc_t |
| 33 | tap_interface::create_cmd::issue(connection& con) |
| 34 | { |
| 35 | msg_t req(con.ctx(), std::ref(*this)); |
| 36 | |
| 37 | auto& payload = req.get_request().get_payload(); |
| 38 | memset(payload.tap_name, 0, sizeof(payload.tap_name)); |
| 39 | memcpy(payload.tap_name, m_name.c_str(), |
| 40 | std::min(m_name.length(), sizeof(payload.tap_name))); |
| 41 | if (m_prefix != route::prefix_t::ZERO) { |
| 42 | if (m_prefix.address().is_v6()) { |
| 43 | m_prefix.to_vpp(&payload.ip6_address_set, payload.ip6_address, |
| 44 | &payload.ip6_mask_width); |
| 45 | } else { |
| 46 | m_prefix.to_vpp(&payload.ip4_address_set, payload.ip4_address, |
| 47 | &payload.ip4_mask_width); |
| 48 | payload.ip4_address_set = 1; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (m_l2_address != l2_address_t::ZERO) { |
| 53 | m_l2_address.to_bytes(payload.mac_address, 6); |
| 54 | } else { |
| 55 | payload.use_random_mac = 1; |
| 56 | } |
| 57 | |
| 58 | VAPI_CALL(req.execute()); |
| 59 | |
| 60 | m_hw_item = wait(); |
| 61 | |
| 62 | return rc_t::OK; |
| 63 | } |
| 64 | |
| 65 | std::string |
| 66 | tap_interface::create_cmd::to_string() const |
| 67 | { |
| 68 | std::ostringstream s; |
| 69 | s << "tap-intf-create: " << m_hw_item.to_string() |
| 70 | << " ip-prefix:" << m_prefix.to_string(); |
| 71 | |
| 72 | return (s.str()); |
| 73 | } |
| 74 | |
| 75 | tap_interface::delete_cmd::delete_cmd(HW::item<handle_t>& item) |
| 76 | : interface::delete_cmd<vapi::Tap_delete>(item) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | rc_t |
| 81 | tap_interface::delete_cmd::issue(connection& con) |
| 82 | { |
| 83 | // finally... call VPP |
| 84 | |
| 85 | return rc_t::OK; |
| 86 | } |
| 87 | std::string |
| 88 | tap_interface::delete_cmd::to_string() const |
| 89 | { |
| 90 | std::ostringstream s; |
| 91 | s << "tap-itf-delete: " << m_hw_item.to_string(); |
| 92 | |
| 93 | return (s.str()); |
| 94 | } |
| 95 | |
| 96 | tap_interface::dump_cmd::dump_cmd() |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | bool |
| 101 | tap_interface::dump_cmd::operator==(const dump_cmd& other) const |
| 102 | { |
| 103 | return (true); |
| 104 | } |
| 105 | |
| 106 | rc_t |
| 107 | tap_interface::dump_cmd::issue(connection& con) |
| 108 | { |
| 109 | m_dump.reset(new msg_t(con.ctx(), std::ref(*this))); |
| 110 | |
| 111 | VAPI_CALL(m_dump->execute()); |
| 112 | |
| 113 | wait(); |
| 114 | |
| 115 | return rc_t::INPROGRESS; |
| 116 | } |
| 117 | |
| 118 | std::string |
| 119 | tap_interface::dump_cmd::to_string() const |
| 120 | { |
| 121 | return ("tap-itf-dump"); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * fd.io coding-style-patch-verification: ON |
| 127 | * |
| 128 | * Local Variables: |
| 129 | * eval: (c-set-style "mozilla") |
| 130 | * End: |
| 131 | */ |