Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [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, |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "ethernet-channel.hpp" |
| 27 | #include "ethernet-protocol.hpp" |
Davide Pesavento | cb425e8 | 2019-07-14 21:48:22 -0400 | [diff] [blame] | 28 | #include "face.hpp" |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 29 | #include "generic-link-service.hpp" |
| 30 | #include "unicast-ethernet-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 31 | #include "common/global.hpp" |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 32 | |
| 33 | #include <boost/range/adaptor/map.hpp> |
| 34 | #include <pcap/pcap.h> |
| 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::face { |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 37 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 38 | NFD_LOG_INIT(EthernetChannel); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 39 | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 40 | EthernetChannel::EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint, |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 41 | time::nanoseconds idleTimeout) |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 42 | : m_localEndpoint(std::move(localEndpoint)) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 43 | , m_socket(getGlobalIoService()) |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 44 | , m_pcap(m_localEndpoint->getName()) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 45 | , m_idleFaceTimeout(idleTimeout) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 46 | { |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 47 | setUri(FaceUri::fromDev(m_localEndpoint->getName())); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 48 | NFD_LOG_CHAN_INFO("Creating channel"); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | EthernetChannel::connect(const ethernet::Address& remoteEndpoint, |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 53 | const FaceParams& params, |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 54 | const FaceCreatedCallback& onFaceCreated, |
| 55 | const FaceCreationFailedCallback& onConnectFailed) |
| 56 | { |
| 57 | shared_ptr<Face> face; |
| 58 | try { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 59 | face = createFace(remoteEndpoint, params).second; |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 60 | } |
| 61 | catch (const boost::system::system_error& e) { |
| 62 | NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what()); |
| 63 | if (onConnectFailed) |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 64 | onConnectFailed(504, "Face creation failed: "s + e.what()); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | |
| 68 | // Need to invoke the callback regardless of whether or not we had already |
| 69 | // created the face so that control responses and such can be sent |
| 70 | onFaceCreated(face); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | EthernetChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 75 | const FaceCreationFailedCallback& onFaceCreationFailed) |
| 76 | { |
| 77 | if (isListening()) { |
| 78 | NFD_LOG_CHAN_WARN("Already listening"); |
| 79 | return; |
| 80 | } |
| 81 | m_isListening = true; |
| 82 | |
| 83 | try { |
| 84 | m_pcap.activate(DLT_EN10MB); |
| 85 | m_socket.assign(m_pcap.getFd()); |
| 86 | } |
| 87 | catch (const PcapHelper::Error& e) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 88 | NDN_THROW_NESTED(Error(e.what())); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 89 | } |
| 90 | updateFilter(); |
| 91 | |
| 92 | asyncRead(onFaceCreated, onFaceCreationFailed); |
| 93 | NFD_LOG_CHAN_DEBUG("Started listening"); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | EthernetChannel::asyncRead(const FaceCreatedCallback& onFaceCreated, |
| 98 | const FaceCreationFailedCallback& onReceiveFailed) |
| 99 | { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 100 | m_socket.async_wait(boost::asio::posix::stream_descriptor::wait_read, [=] (const auto& e) { |
| 101 | handleRead(e, onFaceCreated, onReceiveFailed); |
| 102 | }); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | EthernetChannel::handleRead(const boost::system::error_code& error, |
| 107 | const FaceCreatedCallback& onFaceCreated, |
| 108 | const FaceCreationFailedCallback& onReceiveFailed) |
| 109 | { |
| 110 | if (error) { |
| 111 | if (error != boost::asio::error::operation_aborted) { |
| 112 | NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message()); |
| 113 | if (onReceiveFailed) |
| 114 | onReceiveFailed(500, "Receive failed: " + error.message()); |
| 115 | } |
| 116 | return; |
| 117 | } |
| 118 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 119 | auto [pkt, readErr] = m_pcap.readNextPacket(); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 120 | if (pkt.empty()) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 121 | NFD_LOG_CHAN_WARN("Read error: " << readErr); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 122 | } |
| 123 | else { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 124 | auto [eh, frameErr] = ethernet::checkFrameHeader(pkt, m_localEndpoint->getEthernetAddress(), |
| 125 | m_localEndpoint->getEthernetAddress()); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 126 | if (eh == nullptr) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 127 | NFD_LOG_CHAN_DEBUG(frameErr); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 128 | } |
| 129 | else { |
| 130 | ethernet::Address sender(eh->ether_shost); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 131 | pkt = pkt.subspan(ethernet::HDR_LEN); |
| 132 | processIncomingPacket(pkt, sender, onFaceCreated, onReceiveFailed); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Davide Pesavento | 91c15c8 | 2024-01-15 17:14:23 -0500 | [diff] [blame] | 136 | #ifndef NDEBUG |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 137 | size_t nDropped = m_pcap.getNDropped(); |
| 138 | if (nDropped - m_nDropped > 0) |
| 139 | NFD_LOG_CHAN_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)"); |
| 140 | m_nDropped = nDropped; |
| 141 | #endif |
| 142 | |
| 143 | asyncRead(onFaceCreated, onReceiveFailed); |
| 144 | } |
| 145 | |
| 146 | void |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 147 | EthernetChannel::processIncomingPacket(span<const uint8_t> packet, |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 148 | const ethernet::Address& sender, |
| 149 | const FaceCreatedCallback& onFaceCreated, |
| 150 | const FaceCreationFailedCallback& onReceiveFailed) |
| 151 | { |
| 152 | NFD_LOG_CHAN_TRACE("New peer " << sender); |
| 153 | |
| 154 | bool isCreated = false; |
| 155 | shared_ptr<Face> face; |
| 156 | try { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 157 | FaceParams params; |
| 158 | params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND; |
| 159 | std::tie(isCreated, face) = createFace(sender, params); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 160 | } |
| 161 | catch (const EthernetTransport::Error& e) { |
| 162 | NFD_LOG_CHAN_DEBUG("Face creation for " << sender << " failed: " << e.what()); |
| 163 | if (onReceiveFailed) |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 164 | onReceiveFailed(504, "Face creation failed: "s + e.what()); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
| 168 | if (isCreated) |
| 169 | onFaceCreated(face); |
| 170 | else |
| 171 | NFD_LOG_CHAN_DEBUG("Received frame for existing face"); |
| 172 | |
| 173 | // dispatch the packet to the face for processing |
| 174 | auto* transport = static_cast<UnicastEthernetTransport*>(face->getTransport()); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 175 | transport->receivePayload(packet, sender); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | std::pair<bool, shared_ptr<Face>> |
| 179 | EthernetChannel::createFace(const ethernet::Address& remoteEndpoint, |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 180 | const FaceParams& params) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 181 | { |
| 182 | auto it = m_channelFaces.find(remoteEndpoint); |
| 183 | if (it != m_channelFaces.end()) { |
| 184 | // we already have a face for this endpoint, so reuse it |
| 185 | NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint); |
| 186 | return {false, it->second}; |
| 187 | } |
| 188 | |
| 189 | // else, create a new face |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 190 | GenericLinkService::Options options; |
Davide Pesavento | 00a3c9d | 2017-08-10 22:40:16 -0400 | [diff] [blame] | 191 | options.allowFragmentation = true; |
| 192 | options.allowReassembly = true; |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 193 | options.reliabilityOptions.isEnabled = params.wantLpReliability; |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 194 | if (params.mtu) { |
| 195 | options.overrideMtu = *params.mtu; |
| 196 | } |
Davide Pesavento | 00a3c9d | 2017-08-10 22:40:16 -0400 | [diff] [blame] | 197 | |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 198 | auto linkService = make_unique<GenericLinkService>(options); |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 199 | auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint, |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 200 | params.persistency, m_idleFaceTimeout); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 201 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 202 | face->setChannel(weak_from_this()); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 203 | |
| 204 | m_channelFaces[remoteEndpoint] = face; |
| 205 | connectFaceClosedSignal(*face, [this, remoteEndpoint] { |
| 206 | m_channelFaces.erase(remoteEndpoint); |
| 207 | updateFilter(); |
| 208 | }); |
| 209 | updateFilter(); |
| 210 | |
| 211 | return {true, face}; |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | EthernetChannel::updateFilter() |
| 216 | { |
| 217 | if (!isListening()) |
| 218 | return; |
| 219 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame^] | 220 | std::string filter = "(ether proto " + std::to_string(ethernet::ETHERTYPE_NDN) + |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 221 | ") && (ether dst " + m_localEndpoint->getEthernetAddress().toString() + ")"; |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 222 | for (const auto& addr : m_channelFaces | boost::adaptors::map_keys) { |
| 223 | filter += " && (not ether src " + addr.toString() + ")"; |
| 224 | } |
| 225 | // "not vlan" must appear last in the filter expression, or the |
| 226 | // rest of the filter won't work as intended, see pcap-filter(7) |
| 227 | filter += " && (not vlan)"; |
| 228 | |
| 229 | NFD_LOG_CHAN_TRACE("Updating filter: " << filter); |
| 230 | m_pcap.setPacketFilter(filter.data()); |
| 231 | } |
| 232 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 233 | } // namespace nfd::face |