blob: 2a6a5aad8c2a5eac9096490f18a9121c368704a3 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * 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.
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * 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.
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * 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 **/
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-net-device-face.hpp"
21#include "ndn-l3-protocol.hpp"
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070022
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080023#include "ndn-ns3.hpp"
24
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070025#include "ns3/net-device.h"
26#include "ns3/log.h"
27#include "ns3/packet.h"
28#include "ns3/node.h"
29#include "ns3/pointer.h"
30
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031// #include "ns3/address.h"
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -070032#include "ns3/point-to-point-net-device.h"
33#include "ns3/channel.h"
34
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080035#include "../utils/ndn-fw-hop-count-tag.hpp"
36
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceFace");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070038
39namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace ndn {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070041
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042NetDeviceFace::NetDeviceFace(Ptr<Node> node, const Ptr<NetDevice>& netDevice)
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080043 : Face(FaceUri("netDeviceFace://"), FaceUri("netDeviceFace://"))
44 , m_node(node)
45 , m_netDevice(netDevice)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070046{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 NS_LOG_FUNCTION(this << netDevice);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070048
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080049 setMetric(1); // default metric
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070050
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080052
53 m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this),
54 L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice,
55 true /*promiscuous mode*/);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070056}
57
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058NetDeviceFace::~NetDeviceFace()
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070059{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060 NS_LOG_FUNCTION_NOARGS();
Mickey Sweatt89046c12014-11-16 20:32:27 -080061 close();
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080062}
63
64void
65NetDeviceFace::close()
66{
Mickey Sweatt89046c12014-11-16 20:32:27 -080067 m_node->UnregisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this));
68 this->fail("Close connection");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070069}
70
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070071Ptr<NetDevice>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072NetDeviceFace::GetNetDevice() const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070073{
74 return m_netDevice;
75}
76
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080077void
78NetDeviceFace::send(Ptr<Packet> packet)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070079{
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080080 NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(),
81 "Packet size " << packet->GetSize() << " exceeds device MTU "
82 << m_netDevice->GetMtu());
83
84 FwHopCountTag tag;
85 packet->RemovePacketTag(tag);
86 tag.Increment();
87 packet->AddPacketTag(tag);
88
89 m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE);
90}
91
92void
93NetDeviceFace::sendInterest(const Interest& interest)
94{
95 NS_LOG_FUNCTION(this << &interest);
96
97 this->onSendInterest(interest);
98
99 Ptr<Packet> packet = Convert::ToPacket(interest);
100 send(packet);
101}
102
103void
104NetDeviceFace::sendData(const Data& data)
105{
106 NS_LOG_FUNCTION(this << &data);
107
108 this->onSendData(data);
109
110 Ptr<Packet> packet = Convert::ToPacket(data);
111 send(packet);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700112}
113
114// callback
115void
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800116NetDeviceFace::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 const Address& from, const Address& to,
118 NetDevice::PacketType packetType)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700119{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800121
122 Ptr<Packet> packet = p->Copy();
123 try {
124 uint32_t type = Convert::getPacketType(p);
125 if (type == ::ndn::tlv::Interest) {
126 shared_ptr<const Interest> i = Convert::FromPacket<Interest>(packet);
127 this->onReceiveInterest(*i);
128 }
129 else if (type == ::ndn::tlv::Data) {
130 shared_ptr<const Data> d = Convert::FromPacket<Data>(packet);
131 this->onReceiveData(*d);
132 }
133 else {
134 NS_LOG_ERROR("Unsupported TLV packet");
135 }
136 }
137 catch (::ndn::tlv::Error&) {
138 NS_LOG_ERROR("Unrecognized TLV packet");
139 }
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700140}
141
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800142} // namespace ndn
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700143} // namespace ns3