NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-net-device-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011-2018 Regents of the University of California.
4  *
5  * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6  * contributors.
7  *
8  * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9  * of the GNU General Public License as published by the Free Software Foundation,
10  * either version 3 of the License, or (at your option) any later version.
11  *
12  * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  * PURPOSE. See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18  **/
19 
21 
22 #include "../helper/ndn-stack-helper.hpp"
23 #include "ndn-block-header.hpp"
24 #include "../utils/ndn-ns3-packet-tag.hpp"
25 
26 #include <ndn-cxx/encoding/block.hpp>
27 #include <ndn-cxx/interest.hpp>
28 #include <ndn-cxx/data.hpp>
29 
30 #include "ns3/queue.h"
31 
32 NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceTransport");
33 
34 namespace ns3 {
35 namespace ndn {
36 
38  const Ptr<NetDevice>& netDevice,
39  const std::string& localUri,
40  const std::string& remoteUri,
41  ::ndn::nfd::FaceScope scope,
42  ::ndn::nfd::FacePersistency persistency,
43  ::ndn::nfd::LinkType linkType)
44  : m_netDevice(netDevice)
45  , m_node(node)
46 {
47  this->setLocalUri(FaceUri(localUri));
48  this->setRemoteUri(FaceUri(remoteUri));
49  this->setScope(scope);
50  this->setPersistency(persistency);
51  this->setLinkType(linkType);
52  this->setMtu(m_netDevice->GetMtu()); // Use the MTU of the netDevice
53 
54  // Get send queue capacity for congestion marking
55  PointerValue txQueueAttribute;
56  if (m_netDevice->GetAttributeFailSafe("TxQueue", txQueueAttribute)) {
57  Ptr<ns3::QueueBase> txQueue = txQueueAttribute.Get<ns3::QueueBase>();
58  // must be put into bytes mode queue
59  this->setSendQueueCapacity(txQueue->GetMaxBytes());
60  }
61 
62  NS_LOG_FUNCTION(this << "Creating an ndnSIM transport instance for netDevice with URI"
63  << this->getLocalUri());
64 
65  NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
66 
67  m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceTransport::receiveFromNetDevice, this),
69  true /*promiscuous mode*/);
70 }
71 
73 {
74  NS_LOG_FUNCTION_NOARGS();
75 }
76 
77 ssize_t
79 {
80  PointerValue txQueueAttribute;
81  if (m_netDevice->GetAttributeFailSafe("TxQueue", txQueueAttribute)) {
82  Ptr<ns3::QueueBase> txQueue = txQueueAttribute.Get<ns3::QueueBase>();
83  return txQueue->GetNBytes();
84  }
85  else {
87  }
88 }
89 
90 void
91 NetDeviceTransport::doClose()
92 {
93  NS_LOG_FUNCTION(this << "Closing transport for netDevice with URI"
94  << this->getLocalUri());
95 
96  // set the state of the transport to "CLOSED"
98 }
99 
100 void
101 NetDeviceTransport::doSend(Packet&& packet)
102 {
103  NS_LOG_FUNCTION(this << "Sending packet from netDevice with URI"
104  << this->getLocalUri());
105 
106  // convert NFD packet to NS3 packet
107  BlockHeader header(packet);
108 
109  Ptr<ns3::Packet> ns3Packet = Create<ns3::Packet>();
110  ns3Packet->AddHeader(header);
111 
112  // send the NS3 packet
113  m_netDevice->Send(ns3Packet, m_netDevice->GetBroadcast(),
115 }
116 
117 // callback
118 void
119 NetDeviceTransport::receiveFromNetDevice(Ptr<NetDevice> device,
120  Ptr<const ns3::Packet> p,
121  uint16_t protocol,
122  const Address& from, const Address& to,
123  NetDevice::PacketType packetType)
124 {
125  NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
126 
127  // Convert NS3 packet to NFD packet
128  Ptr<ns3::Packet> packet = p->Copy();
129 
130  BlockHeader header;
131  packet->RemoveHeader(header);
132 
133  auto nfdPacket = Packet(std::move(header.getBlock()));
134 
135  this->receive(std::move(nfdPacket));
136 }
137 
138 Ptr<NetDevice>
140 {
141  return m_netDevice;
142 }
143 
144 } // namespace ndn
145 } // namespace ns3
const ssize_t QUEUE_UNSUPPORTED
indicates that the transport does not support reading the queue capacity/length
Definition: transport.hpp:104
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:158
Copyright (c) 2011-2015 Regents of the University of California.
static const uint16_t ETHERNET_FRAME_TYPE
Ethernet Frame Type of Ndn.
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:423
virtual ssize_t getSendQueueLength() final
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:453
void setMtu(ssize_t mtu)
Definition: transport.hpp:465
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:435
NetDeviceTransport(Ptr< Node > node, const Ptr< NetDevice > &netDevice, const std::string &localUri, const std::string &remoteUri, ::ndn::nfd::FaceScope scope=::ndn::nfd::FACE_SCOPE_NON_LOCAL, ::ndn::nfd::FacePersistency persistency=::ndn::nfd::FACE_PERSISTENCY_PERSISTENT, ::ndn::nfd::LinkType linkType=::ndn::nfd::LINK_TYPE_POINT_TO_POINT)
FaceUri getLocalUri() const
Definition: transport.hpp:405
the transport is closed, and can be safely deallocated
Copyright (c) 2011-2015 Regents of the University of California.
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:411
void setSendQueueCapacity(ssize_t sendQueueCapacity)
Definition: transport.hpp:478
void setState(TransportState newState)
set transport state
Definition: transport.cpp:181
void receive(Packet &&packet)
receive a link-layer packet
Definition: transport.cpp:118