blob: 5a8f91ad1e6103b8ec47f09d37fde971831be154 [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/*
Junxiao Shi7003c602017-01-10 13:35:28 +00003 * Copyright (c) 2014-2017, 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 Pesaventof8b41eb2014-12-26 19:14:06 +010028#include "core/global-io.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010029
30#include <pcap/pcap.h>
31
Davide Pesavento43ff2a92017-05-18 19:50:57 -040032#include <arpa/inet.h> // for htons()
Davide Pesaventob8eebcb2017-08-08 15:03:17 -040033#include <cstring> // for memcpy()
Alexander Afanasyevd9479aa2014-06-26 13:52:02 -070034
Davide Pesavento44deacc2014-02-19 10:48:07 +010035namespace nfd {
Davide Pesavento35120ea2015-11-17 21:13:18 +010036namespace face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010037
Davide Pesavento35120ea2015-11-17 21:13:18 +010038NFD_LOG_INIT("EthernetTransport");
Davide Pesavento44deacc2014-02-19 10:48:07 +010039
Junxiao Shi0d82d042017-07-07 06:15:27 +000040EthernetTransport::EthernetTransport(const ndn::net::NetworkInterface& localEndpoint,
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040041 const ethernet::Address& remoteEndpoint)
Davide Pesaventofe0580c2017-05-12 02:02:10 -040042 : m_socket(getGlobalIoService())
Junxiao Shi0d82d042017-07-07 06:15:27 +000043 , m_pcap(localEndpoint.getName())
44 , m_srcAddress(localEndpoint.getEthernetAddress())
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040045 , m_destAddress(remoteEndpoint)
Junxiao Shi0d82d042017-07-07 06:15:27 +000046 , m_interfaceName(localEndpoint.getName())
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040047 , m_hasRecentlyReceived(false)
Davide Pesavento9a090a02015-01-29 18:15:26 +010048#ifdef _DEBUG
49 , m_nDropped(0)
50#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +010051{
Davide Pesaventofe0580c2017-05-12 02:02:10 -040052 try {
53 m_pcap.activate(DLT_EN10MB);
54 m_socket.assign(m_pcap.getFd());
55 }
56 catch (const PcapHelper::Error& e) {
57 BOOST_THROW_EXCEPTION(Error(e.what()));
58 }
Davide Pesavento44deacc2014-02-19 10:48:07 +010059
Davide Pesavento43ff2a92017-05-18 19:50:57 -040060 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +010061}
62
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040063void
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040064EthernetTransport::doClose()
Davide Pesavento44deacc2014-02-19 10:48:07 +010065{
Davide Pesavento35120ea2015-11-17 21:13:18 +010066 NFD_LOG_FACE_TRACE(__func__);
Alexander Afanasyev251f3c12014-06-02 18:39:58 +030067
Davide Pesavento35120ea2015-11-17 21:13:18 +010068 if (m_socket.is_open()) {
69 // Cancel all outstanding operations and close the socket.
70 // Use the non-throwing variants and ignore errors, if any.
71 boost::system::error_code error;
72 m_socket.cancel(error);
73 m_socket.close(error);
74 }
Davide Pesaventofe0580c2017-05-12 02:02:10 -040075 m_pcap.close();
Davide Pesavento7726ae52014-11-23 21:01:05 +010076
Davide Pesavento35120ea2015-11-17 21:13:18 +010077 // Ensure that the Transport stays alive at least
78 // until all pending handlers are dispatched
79 getGlobalIoService().post([this] {
80 this->setState(TransportState::CLOSED);
81 });
Davide Pesavento44deacc2014-02-19 10:48:07 +010082}
83
84void
Davide Pesaventofe0580c2017-05-12 02:02:10 -040085EthernetTransport::doSend(Transport::Packet&& packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +010086{
Davide Pesaventofe0580c2017-05-12 02:02:10 -040087 NFD_LOG_FACE_TRACE(__func__);
Davide Pesavento44deacc2014-02-19 10:48:07 +010088
Davide Pesaventofe0580c2017-05-12 02:02:10 -040089 sendPacket(packet.packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +010090}
91
Davide Pesavento10783f22014-03-15 04:40:01 +010092void
Davide Pesavento35120ea2015-11-17 21:13:18 +010093EthernetTransport::sendPacket(const ndn::Block& block)
Davide Pesavento44deacc2014-02-19 10:48:07 +010094{
Davide Pesavento44deacc2014-02-19 10:48:07 +010095 ndn::EncodingBuffer buffer(block);
96
Davide Pesaventoe4631002014-03-21 20:55:36 +010097 // pad with zeroes if the payload is too short
Davide Pesavento35120ea2015-11-17 21:13:18 +010098 if (block.size() < ethernet::MIN_DATA_LEN) {
99 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
100 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
101 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100102
103 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700104 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100105 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100106 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100107 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
108
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400109 // send the frame
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400110 int sent = pcap_inject(m_pcap, buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100111 if (sent < 0)
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400112 handleError("Send operation failed: " + m_pcap.getLastError());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100113 else if (static_cast<size_t>(sent) < buffer.size())
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400114 handleError("Failed to send the full frame: size=" + to_string(buffer.size()) +
115 " sent=" + to_string(sent));
Davide Pesavento35120ea2015-11-17 21:13:18 +0100116 else
117 // print block size because we don't want to count the padding in buffer
118 NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100119}
120
121void
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400122EthernetTransport::asyncRead()
123{
124 m_socket.async_read_some(boost::asio::null_buffers(),
125 bind(&EthernetTransport::handleRead, this,
126 boost::asio::placeholders::error));
127}
128
129void
130EthernetTransport::handleRead(const boost::system::error_code& error)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100131{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400132 if (error) {
133 // boost::asio::error::operation_aborted must be checked first: in that case, the Transport
134 // may already have been destructed, therefore it's unsafe to call getState() or do logging.
135 if (error != boost::asio::error::operation_aborted &&
136 getState() != TransportState::CLOSING &&
137 getState() != TransportState::FAILED &&
138 getState() != TransportState::CLOSED) {
139 handleError("Receive operation failed: " + error.message());
140 }
141 return;
142 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100143
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400144 const uint8_t* pkt;
145 size_t len;
146 std::string err;
147 std::tie(pkt, len, err) = m_pcap.readNextPacket();
Davide Pesavento35120ea2015-11-17 21:13:18 +0100148
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400149 if (pkt == nullptr) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400150 NFD_LOG_FACE_WARN("Read error: " << err);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400151 }
152 else {
153 const ether_header* eh;
154 std::tie(eh, err) = ethernet::checkFrameHeader(pkt, len, m_srcAddress,
155 m_destAddress.isMulticast() ? m_destAddress : m_srcAddress);
156 if (eh == nullptr) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400157 NFD_LOG_FACE_WARN(err);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400158 }
159 else {
160 ethernet::Address sender(eh->ether_shost);
161 pkt += ethernet::HDR_LEN;
162 len -= ethernet::HDR_LEN;
163 receivePayload(pkt, len, sender);
164 }
165 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100166
Davide Pesavento9a090a02015-01-29 18:15:26 +0100167#ifdef _DEBUG
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400168 size_t nDropped = m_pcap.getNDropped();
169 if (nDropped - m_nDropped > 0)
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400170 NFD_LOG_FACE_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)");
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400171 m_nDropped = nDropped;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100172#endif
173
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400174 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100175}
176
177void
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400178EthernetTransport::receivePayload(const uint8_t* payload, size_t length,
179 const ethernet::Address& sender)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100180{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400181 NFD_LOG_FACE_TRACE("Received: " << length << " bytes from " << sender);
182
Junxiao Shi78926c92015-02-28 22:56:06 -0700183 bool isOk = false;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100184 Block element;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400185 std::tie(isOk, element) = Block::fromBuffer(payload, length);
Junxiao Shi083f7782015-02-21 12:06:26 -0700186 if (!isOk) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400187 NFD_LOG_FACE_WARN("Failed to parse incoming packet from " << sender);
188 // This packet won't extend the face lifetime
Junxiao Shi083f7782015-02-21 12:06:26 -0700189 return;
190 }
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400191 m_hasRecentlyReceived = true;
Matteo Sammarco66df9742014-11-21 18:31:26 +0100192
Davide Pesavento35120ea2015-11-17 21:13:18 +0100193 Transport::Packet tp(std::move(element));
194 static_assert(sizeof(tp.remoteEndpoint) >= ethernet::ADDR_LEN,
195 "Transport::Packet::remoteEndpoint is too small");
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400196 if (m_destAddress.isMulticast()) {
197 std::memcpy(&tp.remoteEndpoint, sender.data(), sender.size());
198 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100199 this->receive(std::move(tp));
Davide Pesavento7726ae52014-11-23 21:01:05 +0100200}
201
202void
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400203EthernetTransport::handleError(const std::string& errorMessage)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100204{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400205 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
206 NFD_LOG_FACE_DEBUG("Permanent face ignores error: " << errorMessage);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100207 return;
Junxiao Shi7003c602017-01-10 13:35:28 +0000208 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100209
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400210 NFD_LOG_FACE_ERROR(errorMessage);
211 this->setState(TransportState::FAILED);
212 doClose();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100213}
214
Davide Pesavento35120ea2015-11-17 21:13:18 +0100215} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100216} // namespace nfd