blob: cf6052722687d024707f6e4bc0a67daaaf65d740 [file] [log] [blame]
Davide Pesavento43ff2a92017-05-18 19:50:57 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
Davide Pesavento15b55052018-01-27 19:09:28 -05003 * Copyright (c) 2014-2018, Regents of the University of California,
Davide Pesavento43ff2a92017-05-18 19:50:57 -04004 * 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"
30#include "core/global-io.hpp"
31
32#include <boost/range/adaptor/map.hpp>
33#include <pcap/pcap.h>
34
35namespace nfd {
36namespace face {
37
38NFD_LOG_INIT("EthernetChannel");
39
Junxiao Shi0d82d042017-07-07 06:15:27 +000040EthernetChannel::EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040041 time::nanoseconds idleTimeout)
Junxiao Shi0d82d042017-07-07 06:15:27 +000042 : m_localEndpoint(std::move(localEndpoint))
Davide Pesavento43ff2a92017-05-18 19:50:57 -040043 , m_isListening(false)
44 , m_socket(getGlobalIoService())
Junxiao Shi0d82d042017-07-07 06:15:27 +000045 , m_pcap(m_localEndpoint->getName())
Davide Pesavento43ff2a92017-05-18 19:50:57 -040046 , m_idleFaceTimeout(idleTimeout)
47#ifdef _DEBUG
48 , m_nDropped(0)
49#endif
50{
Junxiao Shi0d82d042017-07-07 06:15:27 +000051 setUri(FaceUri::fromDev(m_localEndpoint->getName()));
Davide Pesavento43ff2a92017-05-18 19:50:57 -040052 NFD_LOG_CHAN_INFO("Creating channel");
53}
54
55void
56EthernetChannel::connect(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050057 const FaceParams& params,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040058 const FaceCreatedCallback& onFaceCreated,
59 const FaceCreationFailedCallback& onConnectFailed)
60{
61 shared_ptr<Face> face;
62 try {
Davide Pesavento15b55052018-01-27 19:09:28 -050063 face = createFace(remoteEndpoint, params).second;
Davide Pesavento43ff2a92017-05-18 19:50:57 -040064 }
65 catch (const boost::system::system_error& e) {
66 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
67 if (onConnectFailed)
68 onConnectFailed(504, std::string("Face creation failed: ") + e.what());
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
77void
78EthernetChannel::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) {
92 BOOST_THROW_EXCEPTION(Error(e.what()));
93 }
94 updateFilter();
95
96 asyncRead(onFaceCreated, onFaceCreationFailed);
97 NFD_LOG_CHAN_DEBUG("Started listening");
98}
99
100void
101EthernetChannel::asyncRead(const FaceCreatedCallback& onFaceCreated,
102 const FaceCreationFailedCallback& onReceiveFailed)
103{
104 m_socket.async_read_some(boost::asio::null_buffers(),
105 bind(&EthernetChannel::handleRead, this,
106 boost::asio::placeholders::error,
107 onFaceCreated, onReceiveFailed));
108}
109
110void
111EthernetChannel::handleRead(const boost::system::error_code& error,
112 const FaceCreatedCallback& onFaceCreated,
113 const FaceCreationFailedCallback& onReceiveFailed)
114{
115 if (error) {
116 if (error != boost::asio::error::operation_aborted) {
117 NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
118 if (onReceiveFailed)
119 onReceiveFailed(500, "Receive failed: " + error.message());
120 }
121 return;
122 }
123
124 const uint8_t* pkt;
125 size_t len;
126 std::string err;
127 std::tie(pkt, len, err) = m_pcap.readNextPacket();
128
129 if (pkt == nullptr) {
130 NFD_LOG_CHAN_WARN("Read error: " << err);
131 }
132 else {
133 const ether_header* eh;
Junxiao Shi0d82d042017-07-07 06:15:27 +0000134 std::tie(eh, err) = ethernet::checkFrameHeader(pkt, len, m_localEndpoint->getEthernetAddress(),
135 m_localEndpoint->getEthernetAddress());
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400136 if (eh == nullptr) {
137 NFD_LOG_CHAN_DEBUG(err);
138 }
139 else {
140 ethernet::Address sender(eh->ether_shost);
141 pkt += ethernet::HDR_LEN;
142 len -= ethernet::HDR_LEN;
143 processIncomingPacket(pkt, len, sender, onFaceCreated, onReceiveFailed);
144 }
145 }
146
147#ifdef _DEBUG
148 size_t nDropped = m_pcap.getNDropped();
149 if (nDropped - m_nDropped > 0)
150 NFD_LOG_CHAN_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)");
151 m_nDropped = nDropped;
152#endif
153
154 asyncRead(onFaceCreated, onReceiveFailed);
155}
156
157void
158EthernetChannel::processIncomingPacket(const uint8_t* packet, size_t length,
159 const ethernet::Address& sender,
160 const FaceCreatedCallback& onFaceCreated,
161 const FaceCreationFailedCallback& onReceiveFailed)
162{
163 NFD_LOG_CHAN_TRACE("New peer " << sender);
164
165 bool isCreated = false;
166 shared_ptr<Face> face;
167 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500168 FaceParams params;
169 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
170 std::tie(isCreated, face) = createFace(sender, params);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400171 }
172 catch (const EthernetTransport::Error& e) {
173 NFD_LOG_CHAN_DEBUG("Face creation for " << sender << " failed: " << e.what());
174 if (onReceiveFailed)
175 onReceiveFailed(504, std::string("Face creation failed: ") + e.what());
176 return;
177 }
178
179 if (isCreated)
180 onFaceCreated(face);
181 else
182 NFD_LOG_CHAN_DEBUG("Received frame for existing face");
183
184 // dispatch the packet to the face for processing
185 auto* transport = static_cast<UnicastEthernetTransport*>(face->getTransport());
186 transport->receivePayload(packet, length, sender);
187}
188
189std::pair<bool, shared_ptr<Face>>
190EthernetChannel::createFace(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500191 const FaceParams& params)
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400192{
193 auto it = m_channelFaces.find(remoteEndpoint);
194 if (it != m_channelFaces.end()) {
195 // we already have a face for this endpoint, so reuse it
196 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
197 return {false, it->second};
198 }
199
200 // else, create a new face
Eric Newberry2642cd22017-07-13 21:34:53 -0400201 GenericLinkService::Options options;
Davide Pesavento00a3c9d2017-08-10 22:40:16 -0400202 options.allowFragmentation = true;
203 options.allowReassembly = true;
Davide Pesavento15b55052018-01-27 19:09:28 -0500204 options.reliabilityOptions.isEnabled = params.wantLpReliability;
Davide Pesavento00a3c9d2017-08-10 22:40:16 -0400205
Eric Newberry2642cd22017-07-13 21:34:53 -0400206 auto linkService = make_unique<GenericLinkService>(options);
Junxiao Shi0d82d042017-07-07 06:15:27 +0000207 auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500208 params.persistency, m_idleFaceTimeout);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400209 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
210
211 m_channelFaces[remoteEndpoint] = face;
212 connectFaceClosedSignal(*face, [this, remoteEndpoint] {
213 m_channelFaces.erase(remoteEndpoint);
214 updateFilter();
215 });
216 updateFilter();
217
218 return {true, face};
219}
220
221void
222EthernetChannel::updateFilter()
223{
224 if (!isListening())
225 return;
226
227 std::string filter = "(ether proto " + to_string(ethernet::ETHERTYPE_NDN) +
Junxiao Shi0d82d042017-07-07 06:15:27 +0000228 ") && (ether dst " + m_localEndpoint->getEthernetAddress().toString() + ")";
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400229 for (const auto& addr : m_channelFaces | boost::adaptors::map_keys) {
230 filter += " && (not ether src " + addr.toString() + ")";
231 }
232 // "not vlan" must appear last in the filter expression, or the
233 // rest of the filter won't work as intended, see pcap-filter(7)
234 filter += " && (not vlan)";
235
236 NFD_LOG_CHAN_TRACE("Updating filter: " << filter);
237 m_pcap.setPacketFilter(filter.data());
238}
239
240} // namespace face
241} // namespace nfd