blob: 71c23ce5812eaeaec2493e649ed4effc16cbfea1 [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 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 Afanasyevbe55cf62014-12-20 17:51:09 -080035NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceFace");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070036
37namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038namespace ndn {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070039
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040NS_OBJECT_ENSURE_REGISTERED(NetDeviceFace);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070041
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080042TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043NetDeviceFace::GetTypeId()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080044{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 static TypeId tid = TypeId("ns3::ndn::NetDeviceFace").SetParent<Face>().SetGroupName("Ndn");
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080046 return tid;
47}
48
Alexander Afanasyev565b50c2013-02-01 13:22:56 -080049/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070050 * By default, Ndn face are created in the "down" state. Before
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070051 * becoming useable, the user must invoke SetUp on the face
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070052 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053NetDeviceFace::NetDeviceFace(Ptr<Node> node, const Ptr<NetDevice>& netDevice)
54 : Face(node)
55 , m_netDevice(netDevice)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070056{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 NS_LOG_FUNCTION(this << netDevice);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070058
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 SetMetric(1); // default metric
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070060
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070062}
63
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064NetDeviceFace::~NetDeviceFace()
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070065{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070067}
68
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070069NetDeviceFace& NetDeviceFace::operator= (const NetDeviceFace &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070070{
71 return *this;
72}
73
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070074Ptr<NetDevice>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075NetDeviceFace::GetNetDevice() const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070076{
77 return m_netDevice;
78}
79
80void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081NetDeviceFace::RegisterProtocolHandlers(const InterestHandler& interestHandler,
82 const DataHandler& dataHandler)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070083{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 NS_LOG_FUNCTION(this);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070085
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 Face::RegisterProtocolHandlers(interestHandler, dataHandler);
Alexander Afanasyev565b50c2013-02-01 13:22:56 -080087
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceFace::ReceiveFromNetDevice, this),
89 L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice,
90 true /*promiscuous mode*/);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070091}
92
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070093void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094NetDeviceFace::UnRegisterProtocolHandlers()
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070095{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 m_node->UnregisterProtocolHandler(MakeCallback(&NetDeviceFace::ReceiveFromNetDevice, this));
97 Face::UnRegisterProtocolHandlers();
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070098}
99
100bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101NetDeviceFace::Send(Ptr<Packet> packet)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700102{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 if (!Face::Send(packet)) {
104 return false;
105 }
Alexander Afanasyev565b50c2013-02-01 13:22:56 -0800106
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 NS_LOG_FUNCTION(this << packet);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700108
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(),
110 "Packet size " << packet->GetSize() << " exceeds device MTU "
111 << m_netDevice->GetMtu() << " for Ndn; fragmentation not supported");
112
113 bool ok = m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700114 return ok;
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700115}
116
117// callback
118void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119NetDeviceFace::ReceiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
120 const Address& from, const Address& to,
121 NetDevice::PacketType packetType)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700122{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123 NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
124 Receive(p);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700125}
126
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700127std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128NetDeviceFace::Print(std::ostream& os) const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700129{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700130#ifdef NS3_LOG_ENABLE
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 os << "dev[" << GetNode()->GetId() << "]=net(" << GetId();
Alexander Afanasyev565b50c2013-02-01 13:22:56 -0800132
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 if (DynamicCast<PointToPointNetDevice>(m_netDevice)) {
134 // extra debugging information which available ONLY for PointToPointNetDevice's
135 os << ",";
136 os << DynamicCast<PointToPointNetDevice>(m_netDevice)
137 ->GetChannel()
138 ->GetDevice(0)
139 ->GetNode()
140 ->GetId();
141 os << "-";
142 os << DynamicCast<PointToPointNetDevice>(m_netDevice)
143 ->GetChannel()
144 ->GetDevice(1)
145 ->GetNode()
146 ->GetId();
147 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700148 os << ")";
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149#else
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800150 os << "dev=net(" << GetId() << ")";
Alexander Afanasyev565b50c2013-02-01 13:22:56 -0800151#endif
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700152 return os;
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700153}
154
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700155} // namespace ndnsim
156} // namespace ns3