blob: 7373ed868757142e7468e6d60d2a104c628e4475 [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 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"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070034#include "ns3/ndn-name-components.h"
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -070035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036NS_LOG_COMPONENT_DEFINE ("ndn.NetDeviceFace");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070037
38namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039namespace ndn {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070040
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070041NS_OBJECT_ENSURE_REGISTERED (NetDeviceFace);
42
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080043TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070044NetDeviceFace::GetTypeId ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080045{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046 static TypeId tid = TypeId ("ns3::ndn::NetDeviceFace")
47 .SetParent<Face> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070048 .SetGroupName ("Ndn")
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080049 ;
50 return tid;
51}
52
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070053/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070054 * By default, Ndn face are created in the "down" state. Before
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070055 * becoming useable, the user must invoke SetUp on the face
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070056 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057NetDeviceFace::NetDeviceFace (Ptr<Node> node, const Ptr<NetDevice> &netDevice)
58 : Face (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080059 , m_netDevice (netDevice)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070060{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080061 NS_LOG_FUNCTION (this << netDevice);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070062
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070063 SetMetric (1); // default metric
64
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070065 NS_ASSERT_MSG (m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070066}
67
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070068NetDeviceFace::~NetDeviceFace ()
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070069{
70 NS_LOG_FUNCTION_NOARGS ();
71}
72
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073NetDeviceFace& NetDeviceFace::operator= (const NetDeviceFace &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070074{
75 return *this;
76}
77
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070078Ptr<NetDevice>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079NetDeviceFace::GetNetDevice () const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070080{
81 return m_netDevice;
82}
83
84void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085NetDeviceFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070086{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080087 NS_LOG_FUNCTION (this);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070088
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089 Face::RegisterProtocolHandler (handler);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080090
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091 m_node->RegisterProtocolHandler (MakeCallback (&NetDeviceFace::ReceiveFromNetDevice, this),
92 L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice, true/*promiscuous mode*/);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070093}
94
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070095bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096NetDeviceFace::SendImpl (Ptr<Packet> packet)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070097{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098 NS_LOG_FUNCTION (this << packet);
99
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700100 NS_ASSERT_MSG (packet->GetSize () <= m_netDevice->GetMtu (),
101 "Packet size " << packet->GetSize () << " exceeds device MTU "
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800102 << m_netDevice->GetMtu ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700103 << " for Ndn; fragmentation not supported");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700104
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700105 bool ok = m_netDevice->Send (packet, m_netDevice->GetBroadcast (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106 L3Protocol::ETHERNET_FRAME_TYPE);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700107 return ok;
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700108}
109
110// callback
111void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112NetDeviceFace::ReceiveFromNetDevice (Ptr<NetDevice> device,
113 Ptr<const Packet> p,
114 uint16_t protocol,
115 const Address &from,
116 const Address &to,
117 NetDevice::PacketType packetType)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700118{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800119 NS_LOG_FUNCTION (device << p << protocol << from << to << packetType);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800120 Receive (p);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700121}
122
123
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700124std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125NetDeviceFace::Print (std::ostream& os) const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700126{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127#ifdef NS3_LOG_ENABLE
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700128 os << "dev[" << GetNode ()->GetId () << "]=net(" << GetId () << ",";
129 os << DynamicCast<PointToPointNetDevice> (m_netDevice)->GetChannel ()->GetDevice (0)->GetNode ()->GetId ();
130 os << "-";
131 os << DynamicCast<PointToPointNetDevice> (m_netDevice)->GetChannel ()->GetDevice (1)->GetNode ()->GetId ();
132 os << ")";
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700133#else
134 os << "dev=net(" << GetId () << ")";
135#endif
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700136 return os;
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700137}
138
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700139} // namespace ndnsim
140} // namespace ns3
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700141