Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 91c15c8 | 2024-01-15 17:14:23 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 24 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 25 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 26 | #include "ethernet-transport.hpp" |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 27 | #include "ethernet-protocol.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/global.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 29 | |
| 30 | #include <pcap/pcap.h> |
| 31 | |
Davide Pesavento | 2fae1d0 | 2024-06-08 20:02:27 -0400 | [diff] [blame] | 32 | #include <array> |
| 33 | |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 34 | #include <boost/asio/defer.hpp> |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame] | 35 | #include <boost/endian/conversion.hpp> |
Alexander Afanasyev | d9479aa | 2014-06-26 13:52:02 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | namespace nfd::face { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 38 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 39 | NFD_LOG_INIT(EthernetTransport); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 40 | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 41 | EthernetTransport::EthernetTransport(const ndn::net::NetworkInterface& localEndpoint, |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 42 | const ethernet::Address& remoteEndpoint) |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 43 | : m_socket(getGlobalIoService()) |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 44 | , m_pcap(localEndpoint.getName()) |
| 45 | , m_srcAddress(localEndpoint.getEthernetAddress()) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 46 | , m_destAddress(remoteEndpoint) |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 47 | , m_interfaceName(localEndpoint.getName()) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 48 | { |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 49 | try { |
| 50 | m_pcap.activate(DLT_EN10MB); |
| 51 | m_socket.assign(m_pcap.getFd()); |
| 52 | } |
| 53 | catch (const PcapHelper::Error& e) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 54 | NDN_THROW_NESTED(Error(e.what())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 55 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 56 | |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 57 | // Set initial transport state based upon the state of the underlying NetworkInterface |
| 58 | handleNetifStateChange(localEndpoint.getState()); |
| 59 | |
Eric Newberry | f3ee808 | 2020-01-28 13:44:18 -0800 | [diff] [blame] | 60 | m_netifStateChangedConn = localEndpoint.onStateChanged.connect( |
| 61 | [this] (ndn::net::InterfaceState, ndn::net::InterfaceState newState) { |
Junxiao Shi | fbdd5ce | 2018-11-26 15:10:51 +0000 | [diff] [blame] | 62 | handleNetifStateChange(newState); |
| 63 | }); |
| 64 | |
Eric Newberry | f3ee808 | 2020-01-28 13:44:18 -0800 | [diff] [blame] | 65 | m_netifMtuChangedConn = localEndpoint.onMtuChanged.connect( |
| 66 | [this] (uint32_t, uint32_t mtu) { |
| 67 | setMtu(mtu); |
| 68 | }); |
| 69 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 70 | asyncRead(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 73 | void |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 74 | EthernetTransport::doClose() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 75 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 76 | NFD_LOG_FACE_TRACE(__func__); |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 77 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 78 | if (m_socket.is_open()) { |
| 79 | // Cancel all outstanding operations and close the socket. |
| 80 | // Use the non-throwing variants and ignore errors, if any. |
| 81 | boost::system::error_code error; |
| 82 | m_socket.cancel(error); |
| 83 | m_socket.close(error); |
| 84 | } |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 85 | m_pcap.close(); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 86 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 87 | // Ensure that the Transport stays alive at least |
| 88 | // until all pending handlers are dispatched |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 89 | boost::asio::defer(getGlobalIoService(), [this] { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 90 | this->setState(TransportState::CLOSED); |
| 91 | }); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
Junxiao Shi | fbdd5ce | 2018-11-26 15:10:51 +0000 | [diff] [blame] | 95 | EthernetTransport::handleNetifStateChange(ndn::net::InterfaceState netifState) |
| 96 | { |
| 97 | NFD_LOG_FACE_TRACE("netif is " << netifState); |
| 98 | if (netifState == ndn::net::InterfaceState::RUNNING) { |
| 99 | if (getState() == TransportState::DOWN) { |
| 100 | this->setState(TransportState::UP); |
| 101 | } |
| 102 | } |
| 103 | else if (getState() == TransportState::UP) { |
| 104 | this->setState(TransportState::DOWN); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 109 | EthernetTransport::doSend(const Block& packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 110 | { |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 111 | NFD_LOG_FACE_TRACE(__func__); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 112 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 113 | sendPacket(packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 116 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 117 | EthernetTransport::sendPacket(const ndn::Block& block) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 118 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 119 | ndn::EncodingBuffer buffer(block); |
| 120 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 121 | // pad with zeroes if the payload is too short |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 122 | if (block.size() < ethernet::MIN_DATA_LEN) { |
Davide Pesavento | 2fae1d0 | 2024-06-08 20:02:27 -0400 | [diff] [blame] | 123 | static const std::array<uint8_t, ethernet::MIN_DATA_LEN> padding{}; |
| 124 | buffer.appendBytes(ndn::span(padding).subspan(block.size())); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 125 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 126 | |
| 127 | // construct and prepend the ethernet header |
Davide Pesavento | 2fae1d0 | 2024-06-08 20:02:27 -0400 | [diff] [blame] | 128 | #if BOOST_VERSION >= 107200 |
| 129 | constexpr |
| 130 | #endif |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame] | 131 | uint16_t ethertype = boost::endian::native_to_big(ethernet::ETHERTYPE_NDN); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 132 | buffer.prependBytes({reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN}); |
| 133 | buffer.prependBytes(m_srcAddress); |
| 134 | buffer.prependBytes(m_destAddress); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 135 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 136 | // send the frame |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 137 | int sent = pcap_inject(m_pcap, buffer.data(), buffer.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 138 | if (sent < 0) |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 139 | handleError("Send operation failed: " + m_pcap.getLastError()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 140 | else if (static_cast<size_t>(sent) < buffer.size()) |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 141 | handleError("Failed to send the full frame: size=" + std::to_string(buffer.size()) + |
| 142 | " sent=" + std::to_string(sent)); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 143 | else |
| 144 | // print block size because we don't want to count the padding in buffer |
| 145 | NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 149 | EthernetTransport::asyncRead() |
| 150 | { |
Davide Pesavento | 1e0c88e | 2023-10-05 03:52:52 -0400 | [diff] [blame] | 151 | m_socket.async_wait(boost::asio::posix::stream_descriptor::wait_read, |
| 152 | [this] (const auto& e) { this->handleRead(e); }); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
| 156 | EthernetTransport::handleRead(const boost::system::error_code& error) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 157 | { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 158 | if (error) { |
| 159 | // boost::asio::error::operation_aborted must be checked first: in that case, the Transport |
| 160 | // may already have been destructed, therefore it's unsafe to call getState() or do logging. |
| 161 | if (error != boost::asio::error::operation_aborted && |
| 162 | getState() != TransportState::CLOSING && |
| 163 | getState() != TransportState::FAILED && |
| 164 | getState() != TransportState::CLOSED) { |
| 165 | handleError("Receive operation failed: " + error.message()); |
| 166 | } |
| 167 | return; |
| 168 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 169 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 170 | auto [pkt, readErr] = m_pcap.readNextPacket(); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 171 | if (pkt.empty()) { |
Davide Pesavento | 0b01568 | 2022-12-12 21:00:04 -0500 | [diff] [blame] | 172 | NFD_LOG_FACE_DEBUG("Read error: " << readErr); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 173 | } |
| 174 | else { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 175 | auto [eh, frameErr] = ethernet::checkFrameHeader(pkt, m_srcAddress, |
| 176 | m_destAddress.isMulticast() ? m_destAddress : m_srcAddress); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 177 | if (eh == nullptr) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 178 | NFD_LOG_FACE_WARN(frameErr); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 179 | } |
| 180 | else { |
| 181 | ethernet::Address sender(eh->ether_shost); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 182 | pkt = pkt.subspan(ethernet::HDR_LEN); |
| 183 | receivePayload(pkt, sender); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 184 | } |
| 185 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 186 | |
Davide Pesavento | 91c15c8 | 2024-01-15 17:14:23 -0500 | [diff] [blame] | 187 | #ifndef NDEBUG |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 188 | size_t nDropped = m_pcap.getNDropped(); |
| 189 | if (nDropped - m_nDropped > 0) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 190 | NFD_LOG_FACE_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)"); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 191 | m_nDropped = nDropped; |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 192 | #endif |
| 193 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 194 | asyncRead(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 198 | EthernetTransport::receivePayload(span<const uint8_t> payload, const ethernet::Address& sender) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 199 | { |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 200 | NFD_LOG_FACE_TRACE("Received: " << payload.size() << " bytes from " << sender); |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 201 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 202 | auto [isOk, element] = Block::fromBuffer(payload); |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 203 | if (!isOk) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 204 | NFD_LOG_FACE_WARN("Failed to parse incoming packet from " << sender); |
| 205 | // This packet won't extend the face lifetime |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 206 | return; |
| 207 | } |
Davide Pesavento | 0b0a71d | 2017-07-02 13:57:36 -0400 | [diff] [blame] | 208 | m_hasRecentlyReceived = true; |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 209 | |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame] | 210 | this->receive(element, m_destAddress.isMulticast() ? sender : EndpointId{}); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 214 | EthernetTransport::handleError(const std::string& errorMessage) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 215 | { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 216 | if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
| 217 | NFD_LOG_FACE_DEBUG("Permanent face ignores error: " << errorMessage); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 218 | return; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 219 | } |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 220 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 221 | NFD_LOG_FACE_ERROR(errorMessage); |
| 222 | this->setState(TransportState::FAILED); |
| 223 | doClose(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 226 | } // namespace nfd::face |