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