blob: a76b0b6040b18c13aa5fd0b0f8cbd56111e30795 [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 Pesavento5d642632023-10-03 00:36:08 -04003 * Copyright (c) 2014-2023, 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 Pesavento5d642632023-10-03 00:36:08 -040032#include <boost/asio/defer.hpp>
Davide Pesavento457f1352018-07-14 17:17:38 -040033#include <boost/endian/conversion.hpp>
Alexander Afanasyevd9479aa2014-06-26 13:52:02 -070034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010036
Davide Pesaventoa3148082018-04-12 18:21:54 -040037NFD_LOG_INIT(EthernetTransport);
Davide Pesavento44deacc2014-02-19 10:48:07 +010038
Junxiao Shi0d82d042017-07-07 06:15:27 +000039EthernetTransport::EthernetTransport(const ndn::net::NetworkInterface& localEndpoint,
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040040 const ethernet::Address& remoteEndpoint)
Davide Pesaventofe0580c2017-05-12 02:02:10 -040041 : m_socket(getGlobalIoService())
Junxiao Shi0d82d042017-07-07 06:15:27 +000042 , m_pcap(localEndpoint.getName())
43 , m_srcAddress(localEndpoint.getEthernetAddress())
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040044 , m_destAddress(remoteEndpoint)
Junxiao Shi0d82d042017-07-07 06:15:27 +000045 , m_interfaceName(localEndpoint.getName())
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040046 , m_hasRecentlyReceived(false)
Davide Pesavento9a090a02015-01-29 18:15:26 +010047#ifdef _DEBUG
48 , m_nDropped(0)
49#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +010050{
Davide Pesaventofe0580c2017-05-12 02:02:10 -040051 try {
52 m_pcap.activate(DLT_EN10MB);
53 m_socket.assign(m_pcap.getFd());
54 }
55 catch (const PcapHelper::Error& e) {
Davide Pesavento19779d82019-02-14 13:40:04 -050056 NDN_THROW_NESTED(Error(e.what()));
Davide Pesaventofe0580c2017-05-12 02:02:10 -040057 }
Davide Pesavento44deacc2014-02-19 10:48:07 +010058
Eric Newberryfeeb9182020-03-29 00:28:41 -070059 // Set initial transport state based upon the state of the underlying NetworkInterface
60 handleNetifStateChange(localEndpoint.getState());
61
Eric Newberryf3ee8082020-01-28 13:44:18 -080062 m_netifStateChangedConn = localEndpoint.onStateChanged.connect(
63 [this] (ndn::net::InterfaceState, ndn::net::InterfaceState newState) {
Junxiao Shifbdd5ce2018-11-26 15:10:51 +000064 handleNetifStateChange(newState);
65 });
66
Eric Newberryf3ee8082020-01-28 13:44:18 -080067 m_netifMtuChangedConn = localEndpoint.onMtuChanged.connect(
68 [this] (uint32_t, uint32_t mtu) {
69 setMtu(mtu);
70 });
71
Davide Pesavento43ff2a92017-05-18 19:50:57 -040072 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +010073}
74
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040075void
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040076EthernetTransport::doClose()
Davide Pesavento44deacc2014-02-19 10:48:07 +010077{
Davide Pesavento35120ea2015-11-17 21:13:18 +010078 NFD_LOG_FACE_TRACE(__func__);
Alexander Afanasyev251f3c12014-06-02 18:39:58 +030079
Davide Pesavento35120ea2015-11-17 21:13:18 +010080 if (m_socket.is_open()) {
81 // Cancel all outstanding operations and close the socket.
82 // Use the non-throwing variants and ignore errors, if any.
83 boost::system::error_code error;
84 m_socket.cancel(error);
85 m_socket.close(error);
86 }
Davide Pesaventofe0580c2017-05-12 02:02:10 -040087 m_pcap.close();
Davide Pesavento7726ae52014-11-23 21:01:05 +010088
Davide Pesavento35120ea2015-11-17 21:13:18 +010089 // Ensure that the Transport stays alive at least
90 // until all pending handlers are dispatched
Davide Pesavento5d642632023-10-03 00:36:08 -040091 boost::asio::defer(getGlobalIoService(), [this] {
Davide Pesavento35120ea2015-11-17 21:13:18 +010092 this->setState(TransportState::CLOSED);
93 });
Davide Pesavento44deacc2014-02-19 10:48:07 +010094}
95
96void
Junxiao Shifbdd5ce2018-11-26 15:10:51 +000097EthernetTransport::handleNetifStateChange(ndn::net::InterfaceState netifState)
98{
99 NFD_LOG_FACE_TRACE("netif is " << netifState);
100 if (netifState == ndn::net::InterfaceState::RUNNING) {
101 if (getState() == TransportState::DOWN) {
102 this->setState(TransportState::UP);
103 }
104 }
105 else if (getState() == TransportState::UP) {
106 this->setState(TransportState::DOWN);
107 }
108}
109
110void
Teng Liang13d582a2020-07-21 20:23:11 -0700111EthernetTransport::doSend(const Block& packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100112{
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400113 NFD_LOG_FACE_TRACE(__func__);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100114
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400115 sendPacket(packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100116}
117
Davide Pesavento10783f22014-03-15 04:40:01 +0100118void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100119EthernetTransport::sendPacket(const ndn::Block& block)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100120{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100121 ndn::EncodingBuffer buffer(block);
122
Davide Pesaventoe4631002014-03-21 20:55:36 +0100123 // pad with zeroes if the payload is too short
Davide Pesavento35120ea2015-11-17 21:13:18 +0100124 if (block.size() < ethernet::MIN_DATA_LEN) {
125 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500126 buffer.appendBytes(ndn::make_span(padding).subspan(block.size()));
Davide Pesavento35120ea2015-11-17 21:13:18 +0100127 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100128
129 // construct and prepend the ethernet header
Davide Pesavento457f1352018-07-14 17:17:38 -0400130 uint16_t ethertype = boost::endian::native_to_big(ethernet::ETHERTYPE_NDN);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500131 buffer.prependBytes({reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN});
132 buffer.prependBytes(m_srcAddress);
133 buffer.prependBytes(m_destAddress);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100134
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400135 // send the frame
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500136 int sent = pcap_inject(m_pcap, buffer.data(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100137 if (sent < 0)
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400138 handleError("Send operation failed: " + m_pcap.getLastError());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100139 else if (static_cast<size_t>(sent) < buffer.size())
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400140 handleError("Failed to send the full frame: size=" + to_string(buffer.size()) +
141 " sent=" + to_string(sent));
Davide Pesavento35120ea2015-11-17 21:13:18 +0100142 else
143 // print block size because we don't want to count the padding in buffer
144 NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100145}
146
147void
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400148EthernetTransport::asyncRead()
149{
150 m_socket.async_read_some(boost::asio::null_buffers(),
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400151 [this] (const auto& e, auto) { this->handleRead(e); });
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400152}
153
154void
155EthernetTransport::handleRead(const boost::system::error_code& error)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100156{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400157 if (error) {
158 // boost::asio::error::operation_aborted must be checked first: in that case, the Transport
159 // may already have been destructed, therefore it's unsafe to call getState() or do logging.
160 if (error != boost::asio::error::operation_aborted &&
161 getState() != TransportState::CLOSING &&
162 getState() != TransportState::FAILED &&
163 getState() != TransportState::CLOSED) {
164 handleError("Receive operation failed: " + error.message());
165 }
166 return;
167 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100168
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400169 auto [pkt, readErr] = m_pcap.readNextPacket();
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500170 if (pkt.empty()) {
Davide Pesavento0b015682022-12-12 21:00:04 -0500171 NFD_LOG_FACE_DEBUG("Read error: " << readErr);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400172 }
173 else {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400174 auto [eh, frameErr] = ethernet::checkFrameHeader(pkt, m_srcAddress,
175 m_destAddress.isMulticast() ? m_destAddress : m_srcAddress);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400176 if (eh == nullptr) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400177 NFD_LOG_FACE_WARN(frameErr);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400178 }
179 else {
180 ethernet::Address sender(eh->ether_shost);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500181 pkt = pkt.subspan(ethernet::HDR_LEN);
182 receivePayload(pkt, sender);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400183 }
184 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100185
Davide Pesavento9a090a02015-01-29 18:15:26 +0100186#ifdef _DEBUG
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400187 size_t nDropped = m_pcap.getNDropped();
188 if (nDropped - m_nDropped > 0)
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400189 NFD_LOG_FACE_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)");
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400190 m_nDropped = nDropped;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100191#endif
192
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400193 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100194}
195
196void
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500197EthernetTransport::receivePayload(span<const uint8_t> payload, const ethernet::Address& sender)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100198{
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500199 NFD_LOG_FACE_TRACE("Received: " << payload.size() << " bytes from " << sender);
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400200
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400201 auto [isOk, element] = Block::fromBuffer(payload);
Junxiao Shi083f7782015-02-21 12:06:26 -0700202 if (!isOk) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400203 NFD_LOG_FACE_WARN("Failed to parse incoming packet from " << sender);
204 // This packet won't extend the face lifetime
Junxiao Shi083f7782015-02-21 12:06:26 -0700205 return;
206 }
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400207 m_hasRecentlyReceived = true;
Matteo Sammarco66df9742014-11-21 18:31:26 +0100208
Teng Liangd94b7b32022-07-10 21:29:37 +0800209 this->receive(element, m_destAddress.isMulticast() ? sender : EndpointId{});
Davide Pesavento7726ae52014-11-23 21:01:05 +0100210}
211
212void
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400213EthernetTransport::handleError(const std::string& errorMessage)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100214{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400215 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
216 NFD_LOG_FACE_DEBUG("Permanent face ignores error: " << errorMessage);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100217 return;
Junxiao Shi7003c602017-01-10 13:35:28 +0000218 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100219
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400220 NFD_LOG_FACE_ERROR(errorMessage);
221 this->setState(TransportState::FAILED);
222 doClose();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100223}
224
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400225} // namespace nfd::face