blob: 7baa363bb44823d905c8ed466454dd8e205ea97d [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 Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-net-device-face.h"
23#include "ndn-l3-protocol.h"
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 Afanasyev7f3e49e2012-04-30 00:17:07 -070031#include "ns3/point-to-point-net-device.h"
32#include "ns3/channel.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070033#include "ns3/ndn-name-components.h"
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -070034
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070035NS_LOG_COMPONENT_DEFINE ("NdnNetDeviceFace");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070036
37namespace ns3 {
38
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080039TypeId
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040NdnNetDeviceFace::GetTypeId ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080041{
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042 static TypeId tid = TypeId ("ns3::NdnNetDeviceFace")
43 .SetParent<NdnFace> ()
44 .SetGroupName ("Ndn")
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080045 ;
46 return tid;
47}
48
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070049/**
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 Afanasyev4aac5572012-08-09 10:49:55 -070053NdnNetDeviceFace::NdnNetDeviceFace (Ptr<Node> node, const Ptr<NetDevice> &netDevice)
54 : NdnFace (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080055 , m_netDevice (netDevice)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070056{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080057 NS_LOG_FUNCTION (this << netDevice);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070058
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070059 SetMetric (1); // default metric
60
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070061 NS_ASSERT_MSG (m_netDevice != 0, "NdnNetDeviceFace needs to be assigned a valid NetDevice");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070062}
63
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064NdnNetDeviceFace::~NdnNetDeviceFace ()
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070065{
66 NS_LOG_FUNCTION_NOARGS ();
67}
68
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070069NdnNetDeviceFace& NdnNetDeviceFace::operator= (const NdnNetDeviceFace &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070070{
71 return *this;
72}
73
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070074Ptr<NetDevice>
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070075NdnNetDeviceFace::GetNetDevice () const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070076{
77 return m_netDevice;
78}
79
80void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070081NdnNetDeviceFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070082{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080083 NS_LOG_FUNCTION (this);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070084
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070085 NdnFace::RegisterProtocolHandler (handler);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080086
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070087 m_node->RegisterProtocolHandler (MakeCallback (&NdnNetDeviceFace::ReceiveFromNetDevice, this),
88 NdnL3Protocol::ETHERNET_FRAME_TYPE, m_netDevice, true/*promiscuous mode*/);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070089}
90
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070091bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070092NdnNetDeviceFace::SendImpl (Ptr<Packet> packet)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070093{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080094 NS_LOG_FUNCTION (this << packet);
95
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070096 NS_ASSERT_MSG (packet->GetSize () <= m_netDevice->GetMtu (),
97 "Packet size " << packet->GetSize () << " exceeds device MTU "
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080098 << m_netDevice->GetMtu ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070099 << " for Ndn; fragmentation not supported");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700100
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700101 bool ok = m_netDevice->Send (packet, m_netDevice->GetBroadcast (),
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700102 NdnL3Protocol::ETHERNET_FRAME_TYPE);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700103 return ok;
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700104}
105
106// callback
107void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700108NdnNetDeviceFace::ReceiveFromNetDevice (Ptr<NetDevice> device,
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700109 Ptr<const Packet> p,
110 uint16_t protocol,
111 const Address &from,
112 const Address &to,
113 NetDevice::PacketType packetType)
114{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800115 NS_LOG_FUNCTION (device << p << protocol << from << to << packetType);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 Receive (p);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700117}
118
119
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700120std::ostream&
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700121NdnNetDeviceFace::Print (std::ostream& os) const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700122{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700123 os << "dev[" << GetNode ()->GetId () << "]=net(" << GetId () << ",";
124 os << DynamicCast<PointToPointNetDevice> (m_netDevice)->GetChannel ()->GetDevice (0)->GetNode ()->GetId ();
125 os << "-";
126 os << DynamicCast<PointToPointNetDevice> (m_netDevice)->GetChannel ()->GetDevice (1)->GetNode ()->GetId ();
127 os << ")";
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700128 return os;
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700129}
130
131}; // namespace ns3
132