blob: 568064bae045481fd8bf275bc6f1c3e736a96eb0 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi3ffe66d2014-11-06 15:37:59 -070024 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010025
Davide Pesavento35120ea2015-11-17 21:13:18 +010026#include "ethernet-transport.hpp"
Davide Pesavento43ff2a92017-05-18 19:50:57 -040027#include "ethernet-protocol.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010029
30#include <pcap/pcap.h>
31
Davide Pesavento457f1352018-07-14 17:17:38 -040032#include <cstring> // for memcpy()
33
34#include <boost/endian/conversion.hpp>
Alexander Afanasyevd9479aa2014-06-26 13:52:02 -070035
Davide Pesavento44deacc2014-02-19 10:48:07 +010036namespace nfd {
Davide Pesavento35120ea2015-11-17 21:13:18 +010037namespace face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010038
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(EthernetTransport);
Davide Pesavento44deacc2014-02-19 10:48:07 +010040
Junxiao Shi0d82d042017-07-07 06:15:27 +000041EthernetTransport::EthernetTransport(const ndn::net::NetworkInterface& localEndpoint,
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040042 const ethernet::Address& remoteEndpoint)
Davide Pesaventofe0580c2017-05-12 02:02:10 -040043 : m_socket(getGlobalIoService())
Junxiao Shi0d82d042017-07-07 06:15:27 +000044 , m_pcap(localEndpoint.getName())
45 , m_srcAddress(localEndpoint.getEthernetAddress())
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040046 , m_destAddress(remoteEndpoint)
Junxiao Shi0d82d042017-07-07 06:15:27 +000047 , m_interfaceName(localEndpoint.getName())
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040048 , m_hasRecentlyReceived(false)
Davide Pesavento9a090a02015-01-29 18:15:26 +010049#ifdef _DEBUG
50 , m_nDropped(0)
51#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +010052{
Davide Pesaventofe0580c2017-05-12 02:02:10 -040053 try {
54 m_pcap.activate(DLT_EN10MB);
55 m_socket.assign(m_pcap.getFd());
56 }
57 catch (const PcapHelper::Error& e) {
Davide Pesavento19779d82019-02-14 13:40:04 -050058 NDN_THROW_NESTED(Error(e.what()));
Davide Pesaventofe0580c2017-05-12 02:02:10 -040059 }
Davide Pesavento44deacc2014-02-19 10:48:07 +010060
Eric Newberryfeeb9182020-03-29 00:28:41 -070061 // Set initial transport state based upon the state of the underlying NetworkInterface
62 handleNetifStateChange(localEndpoint.getState());
63
Eric Newberryf3ee8082020-01-28 13:44:18 -080064 m_netifStateChangedConn = localEndpoint.onStateChanged.connect(
65 [this] (ndn::net::InterfaceState, ndn::net::InterfaceState newState) {
Junxiao Shifbdd5ce2018-11-26 15:10:51 +000066 handleNetifStateChange(newState);
67 });
68
Eric Newberryf3ee8082020-01-28 13:44:18 -080069 m_netifMtuChangedConn = localEndpoint.onMtuChanged.connect(
70 [this] (uint32_t, uint32_t mtu) {
71 setMtu(mtu);
72 });
73
Davide Pesavento43ff2a92017-05-18 19:50:57 -040074 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +010075}
76
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040077void
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040078EthernetTransport::doClose()
Davide Pesavento44deacc2014-02-19 10:48:07 +010079{
Davide Pesavento35120ea2015-11-17 21:13:18 +010080 NFD_LOG_FACE_TRACE(__func__);
Alexander Afanasyev251f3c12014-06-02 18:39:58 +030081
Davide Pesavento35120ea2015-11-17 21:13:18 +010082 if (m_socket.is_open()) {
83 // Cancel all outstanding operations and close the socket.
84 // Use the non-throwing variants and ignore errors, if any.
85 boost::system::error_code error;
86 m_socket.cancel(error);
87 m_socket.close(error);
88 }
Davide Pesaventofe0580c2017-05-12 02:02:10 -040089 m_pcap.close();
Davide Pesavento7726ae52014-11-23 21:01:05 +010090
Davide Pesavento35120ea2015-11-17 21:13:18 +010091 // Ensure that the Transport stays alive at least
92 // until all pending handlers are dispatched
93 getGlobalIoService().post([this] {
94 this->setState(TransportState::CLOSED);
95 });
Davide Pesavento44deacc2014-02-19 10:48:07 +010096}
97
98void
Junxiao Shifbdd5ce2018-11-26 15:10:51 +000099EthernetTransport::handleNetifStateChange(ndn::net::InterfaceState netifState)
100{
101 NFD_LOG_FACE_TRACE("netif is " << netifState);
102 if (netifState == ndn::net::InterfaceState::RUNNING) {
103 if (getState() == TransportState::DOWN) {
104 this->setState(TransportState::UP);
105 }
106 }
107 else if (getState() == TransportState::UP) {
108 this->setState(TransportState::DOWN);
109 }
110}
111
112void
Teng Liang13d582a2020-07-21 20:23:11 -0700113EthernetTransport::doSend(const Block& packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100114{
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400115 NFD_LOG_FACE_TRACE(__func__);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100116
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400117 sendPacket(packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100118}
119
Davide Pesavento10783f22014-03-15 04:40:01 +0100120void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100121EthernetTransport::sendPacket(const ndn::Block& block)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100122{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100123 ndn::EncodingBuffer buffer(block);
124
Davide Pesaventoe4631002014-03-21 20:55:36 +0100125 // pad with zeroes if the payload is too short
Davide Pesavento35120ea2015-11-17 21:13:18 +0100126 if (block.size() < ethernet::MIN_DATA_LEN) {
127 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500128 buffer.appendBytes(ndn::make_span(padding).subspan(block.size()));
Davide Pesavento35120ea2015-11-17 21:13:18 +0100129 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100130
131 // construct and prepend the ethernet header
Davide Pesavento457f1352018-07-14 17:17:38 -0400132 uint16_t ethertype = boost::endian::native_to_big(ethernet::ETHERTYPE_NDN);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500133 buffer.prependBytes({reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN});
134 buffer.prependBytes(m_srcAddress);
135 buffer.prependBytes(m_destAddress);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100136
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400137 // send the frame
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500138 int sent = pcap_inject(m_pcap, buffer.data(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100139 if (sent < 0)
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400140 handleError("Send operation failed: " + m_pcap.getLastError());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100141 else if (static_cast<size_t>(sent) < buffer.size())
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400142 handleError("Failed to send the full frame: size=" + to_string(buffer.size()) +
143 " sent=" + to_string(sent));
Davide Pesavento35120ea2015-11-17 21:13:18 +0100144 else
145 // print block size because we don't want to count the padding in buffer
146 NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100147}
148
149void
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400150EthernetTransport::asyncRead()
151{
152 m_socket.async_read_some(boost::asio::null_buffers(),
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400153 [this] (const auto& e, auto) { this->handleRead(e); });
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400154}
155
156void
157EthernetTransport::handleRead(const boost::system::error_code& error)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100158{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400159 if (error) {
160 // boost::asio::error::operation_aborted must be checked first: in that case, the Transport
161 // may already have been destructed, therefore it's unsafe to call getState() or do logging.
162 if (error != boost::asio::error::operation_aborted &&
163 getState() != TransportState::CLOSING &&
164 getState() != TransportState::FAILED &&
165 getState() != TransportState::CLOSED) {
166 handleError("Receive operation failed: " + error.message());
167 }
168 return;
169 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100170
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400171 auto [pkt, readErr] = m_pcap.readNextPacket();
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500172 if (pkt.empty()) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400173 NFD_LOG_FACE_WARN("Read error: " << readErr);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400174 }
175 else {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400176 auto [eh, frameErr] = ethernet::checkFrameHeader(pkt, m_srcAddress,
177 m_destAddress.isMulticast() ? m_destAddress : m_srcAddress);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400178 if (eh == nullptr) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400179 NFD_LOG_FACE_WARN(frameErr);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400180 }
181 else {
182 ethernet::Address sender(eh->ether_shost);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500183 pkt = pkt.subspan(ethernet::HDR_LEN);
184 receivePayload(pkt, sender);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400185 }
186 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100187
Davide Pesavento9a090a02015-01-29 18:15:26 +0100188#ifdef _DEBUG
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400189 size_t nDropped = m_pcap.getNDropped();
190 if (nDropped - m_nDropped > 0)
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400191 NFD_LOG_FACE_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)");
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400192 m_nDropped = nDropped;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100193#endif
194
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400195 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100196}
197
198void
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500199EthernetTransport::receivePayload(span<const uint8_t> payload, const ethernet::Address& sender)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100200{
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500201 NFD_LOG_FACE_TRACE("Received: " << payload.size() << " bytes from " << sender);
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400202
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400203 auto [isOk, element] = Block::fromBuffer(payload);
Junxiao Shi083f7782015-02-21 12:06:26 -0700204 if (!isOk) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400205 NFD_LOG_FACE_WARN("Failed to parse incoming packet from " << sender);
206 // This packet won't extend the face lifetime
Junxiao Shi083f7782015-02-21 12:06:26 -0700207 return;
208 }
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400209 m_hasRecentlyReceived = true;
Matteo Sammarco66df9742014-11-21 18:31:26 +0100210
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400211 static_assert(sizeof(EndpointId) >= ethernet::ADDR_LEN, "EndpointId is too small");
212 EndpointId endpoint = 0;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400213 if (m_destAddress.isMulticast()) {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400214 std::memcpy(&endpoint, sender.data(), sender.size());
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400215 }
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400216
217 this->receive(element, endpoint);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100218}
219
220void
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400221EthernetTransport::handleError(const std::string& errorMessage)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100222{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400223 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
224 NFD_LOG_FACE_DEBUG("Permanent face ignores error: " << errorMessage);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100225 return;
Junxiao Shi7003c602017-01-10 13:35:28 +0000226 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100227
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400228 NFD_LOG_FACE_ERROR(errorMessage);
229 this->setState(TransportState::FAILED);
230 doClose();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100231}
232
Davide Pesavento35120ea2015-11-17 21:13:18 +0100233} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100234} // namespace nfd