blob: 03d95db57afbe40e39a3d0f1cde54d01a3ca8adc [file] [log] [blame]
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Ilya Moiseenko956d0542012-01-02 15:26:40 -08003 * Copyright (c) 2011 University of California, Los Angeles
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 *
20 */
21
Alexander Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-net-device-face.hpp"
23#include "ndn-l3-protocol.hpp"
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070024
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080025#include "ndn-ns3.hpp"
26
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070027#include "ns3/net-device.h"
28#include "ns3/log.h"
29#include "ns3/packet.h"
30#include "ns3/node.h"
31#include "ns3/pointer.h"
32
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033// #include "ns3/address.h"
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -070034#include "ns3/point-to-point-net-device.h"
35#include "ns3/channel.h"
36
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080037#include "../utils/ndn-fw-hop-count-tag.hpp"
38
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceFace");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070040
41namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042namespace ndn {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070043
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044NetDeviceFace::NetDeviceFace(Ptr<Node> node, const Ptr<NetDevice>& netDevice)
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080045 : Face(FaceUri("netDeviceFace://"), FaceUri("netDeviceFace://"))
46 , m_node(node)
47 , m_netDevice(netDevice)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070048{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 NS_LOG_FUNCTION(this << netDevice);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070050
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080051 setMetric(1); // default metric
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070052
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080054
55 m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this),
56 L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice,
57 true /*promiscuous mode*/);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070058}
59
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060NetDeviceFace::~NetDeviceFace()
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070061{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080063
64 m_node->UnregisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this));
65}
66
67void
68NetDeviceFace::close()
69{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070070}
71
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070072Ptr<NetDevice>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073NetDeviceFace::GetNetDevice() const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070074{
75 return m_netDevice;
76}
77
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080078void
79NetDeviceFace::send(Ptr<Packet> packet)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070080{
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080081 NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(),
82 "Packet size " << packet->GetSize() << " exceeds device MTU "
83 << m_netDevice->GetMtu());
84
85 FwHopCountTag tag;
86 packet->RemovePacketTag(tag);
87 tag.Increment();
88 packet->AddPacketTag(tag);
89
90 m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE);
91}
92
93void
94NetDeviceFace::sendInterest(const Interest& interest)
95{
96 NS_LOG_FUNCTION(this << &interest);
97
98 this->onSendInterest(interest);
99
100 Ptr<Packet> packet = Convert::ToPacket(interest);
101 send(packet);
102}
103
104void
105NetDeviceFace::sendData(const Data& data)
106{
107 NS_LOG_FUNCTION(this << &data);
108
109 this->onSendData(data);
110
111 Ptr<Packet> packet = Convert::ToPacket(data);
112 send(packet);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700113}
114
115// callback
116void
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800117NetDeviceFace::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 const Address& from, const Address& to,
119 NetDevice::PacketType packetType)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700120{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800122
123 Ptr<Packet> packet = p->Copy();
124 try {
125 uint32_t type = Convert::getPacketType(p);
126 if (type == ::ndn::tlv::Interest) {
127 shared_ptr<const Interest> i = Convert::FromPacket<Interest>(packet);
128 this->onReceiveInterest(*i);
129 }
130 else if (type == ::ndn::tlv::Data) {
131 shared_ptr<const Data> d = Convert::FromPacket<Data>(packet);
132 this->onReceiveData(*d);
133 }
134 else {
135 NS_LOG_ERROR("Unsupported TLV packet");
136 }
137 }
138 catch (::ndn::tlv::Error&) {
139 NS_LOG_ERROR("Unrecognized TLV packet");
140 }
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700141}
142
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800143} // namespace ndn
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700144} // namespace ns3