src/devices/emu/emu-net-device.cc
author vincent@clarinet.u-strasbg.fr
Fri Nov 07 11:36:15 2008 -0800 (2008-11-07)
changeset 3852 9cf7ad0cac85
parent 3851 cc6a7f93dc3f
child 3936 e525995ce5dc
permissions -rw-r--r--
Initial IPv6 capability
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2008 University of Washington
     4  *
     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 
    19 #include "emu-net-device.h"
    20 #include "emu-encode-decode.h"
    21 
    22 #include "ns3/log.h"
    23 #include "ns3/queue.h"
    24 #include "ns3/simulator.h"
    25 #include "ns3/realtime-simulator-impl.h"
    26 #include "ns3/mac48-address.h"
    27 #include "ns3/ethernet-header.h"
    28 #include "ns3/ethernet-trailer.h"
    29 #include "ns3/llc-snap-header.h"
    30 #include "ns3/trace-source-accessor.h"
    31 #include "ns3/pointer.h"
    32 #include "ns3/channel.h"
    33 #include "ns3/system-thread.h"
    34 #include "ns3/string.h"
    35 #include "ns3/boolean.h"
    36 
    37 #include <sys/wait.h>
    38 #include <sys/stat.h>
    39 #include <sys/socket.h>
    40 #include <sys/un.h>
    41 #include <sys/ioctl.h>
    42 #include <net/ethernet.h>
    43 #include <net/if.h>
    44 #include <netinet/in.h>
    45 #include <netpacket/packet.h>
    46 #include <arpa/inet.h>
    47 #include <errno.h>
    48 #include <limits>
    49 #include <stdlib.h>
    50 
    51 NS_LOG_COMPONENT_DEFINE ("EmuNetDevice");
    52 
    53 namespace ns3 {
    54 
    55 NS_OBJECT_ENSURE_REGISTERED (EmuNetDevice);
    56 
    57 #define EMU_MAGIC 65867
    58 
    59 TypeId 
    60 EmuNetDevice::GetTypeId (void)
    61 {
    62   static TypeId tid = TypeId ("ns3::EmuNetDevice")
    63     .SetParent<NetDevice> ()
    64     .AddConstructor<EmuNetDevice> ()
    65     .AddAttribute ("Address", 
    66                    "The ns-3 MAC address of this (virtual) device.",
    67                    Mac48AddressValue (Mac48Address ("ff:ff:ff:ff:ff:ff")),
    68                    MakeMac48AddressAccessor (&EmuNetDevice::m_address),
    69                    MakeMac48AddressChecker ())
    70     .AddAttribute ("DeviceName", 
    71                    "The name of the underlying real device (e.g. eth1).",
    72                    StringValue ("eth1"),
    73                    MakeStringAccessor (&EmuNetDevice::m_deviceName),
    74                    MakeStringChecker ())
    75     .AddAttribute ("Start", 
    76                    "The simulation time at which to spin up the device thread.",
    77                    TimeValue (Seconds (0.)),
    78                    MakeTimeAccessor (&EmuNetDevice::m_tStart),
    79                    MakeTimeChecker ())
    80     .AddAttribute ("Stop", 
    81                    "The simulation time at which to tear down the device thread.",
    82                    TimeValue (Seconds (0.)),
    83                    MakeTimeAccessor (&EmuNetDevice::m_tStop),
    84                    MakeTimeChecker ())
    85     .AddAttribute ("TxQueue", 
    86                    "A queue to use as the transmit queue in the device.",
    87                    PointerValue (),
    88                    MakePointerAccessor (&EmuNetDevice::m_queue),
    89                    MakePointerChecker<Queue> ())
    90     .AddTraceSource ("Rx", 
    91                      "Trace source to fire on reception of a MAC packet.",
    92                      MakeTraceSourceAccessor (&EmuNetDevice::m_rxTrace))
    93     .AddTraceSource ("Drop", 
    94                      "Trace source to fire on when a MAC packet is dropped.",
    95                      MakeTraceSourceAccessor (&EmuNetDevice::m_dropTrace))
    96     ;
    97   return tid;
    98 }
    99 
   100 
   101 EmuNetDevice::EmuNetDevice () 
   102 : 
   103   m_startEvent (),
   104   m_stopEvent (),
   105   m_sock (-1),
   106   m_readThread (0),
   107   m_ifIndex (std::numeric_limits<uint32_t>::max ()),  // absurdly large value
   108   m_sll_ifindex (-1),
   109   m_name ("Emu NetDevice")
   110 {
   111   NS_LOG_FUNCTION (this);
   112   Start (m_tStart);
   113 }
   114 
   115 EmuNetDevice::~EmuNetDevice ()
   116 {
   117 }
   118 
   119 void 
   120 EmuNetDevice::DoDispose()
   121 {
   122   NS_LOG_FUNCTION_NOARGS ();
   123   m_node = 0;
   124   NetDevice::DoDispose ();
   125 }
   126 
   127 void
   128 EmuNetDevice::Start (Time tStart)
   129 {
   130   NS_LOG_FUNCTION (tStart);
   131 
   132   //
   133   // Cancel any pending start event and schedule a new one at some relative time in the future.
   134   //
   135   Simulator::Cancel (m_startEvent);
   136   m_startEvent = Simulator::Schedule (tStart, &EmuNetDevice::StartDevice, this);
   137 }
   138 
   139   void
   140 EmuNetDevice::Stop (Time tStop)
   141 {
   142   NS_LOG_FUNCTION (tStop);
   143   //
   144   // Cancel any pending stop event and schedule a new one at some relative time in the future.
   145   //
   146   Simulator::Cancel (m_stopEvent);
   147   m_startEvent = Simulator::Schedule (tStop, &EmuNetDevice::StopDevice, this);
   148 }
   149 
   150   void
   151 EmuNetDevice::StartDevice (void)
   152 {
   153   NS_LOG_FUNCTION_NOARGS ();
   154 
   155   //
   156   // Spin up the emu net device and start receiving packets.
   157   //
   158   if (m_sock != -1)
   159     {
   160       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Device is already started");
   161     }
   162 
   163   NS_LOG_LOGIC ("Creating socket");
   164   //
   165   // Call out to a separate process running as suid root in order to get a raw 
   166   // socket.  We do this to avoid having the entire simulation running as root.
   167   // If this method returns, we'll have a raw socket waiting for us in m_sock.
   168   //
   169   CreateSocket ();
   170 
   171   //
   172   // Figure out which interface index corresponds to the device name in the corresponding attribute.
   173   //
   174   struct ifreq ifr;
   175   bzero (&ifr, sizeof(ifr));
   176   strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ);
   177 
   178   NS_LOG_LOGIC ("Getting interface index");
   179   int32_t rc = ioctl (m_sock, SIOCGIFINDEX, &ifr);
   180   if (rc == -1)
   181     {
   182       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Can't get interface index");
   183     }
   184 
   185   //
   186   // Save the real interface index for later calls to sendto
   187   //
   188   m_sll_ifindex = ifr.ifr_ifindex;
   189 
   190   //
   191   // Bind the socket to the interface we just found.
   192   //
   193   struct sockaddr_ll ll;
   194   bzero (&ll, sizeof(ll));
   195 
   196   ll.sll_family = AF_PACKET;
   197   ll.sll_ifindex = m_sll_ifindex;
   198   ll.sll_protocol = htons(ETH_P_ALL); 
   199 
   200   NS_LOG_LOGIC ("Binding socket to interface");
   201 
   202   rc = bind (m_sock, (struct sockaddr *)&ll, sizeof (ll));
   203   if (rc == -1)
   204     {
   205       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Can't bind to specified interface");
   206     }
   207 
   208   rc = ioctl(m_sock, SIOCGIFFLAGS, &ifr);
   209   if (rc == -1)
   210     {
   211       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Can't get interface flags");
   212     }
   213   
   214   //
   215   // This device only works if the underlying interface is up in promiscuous 
   216   // mode.  We could have turned it on in the socket creator, but the situation
   217   // is that we expect these devices to be used in conjunction with virtual 
   218   // machines with connected host-only (simulated) networks, or in a testbed.
   219   // There is a lot of setup and configuration happening outside of this one 
   220   // issue, and we expect that configuration to include choosing a valid
   221   // interface (e.g, "ath1"), ensuring that the device supports promiscuous 
   222   // mode, and placing it in promiscuous mode.  We just make sure of the
   223   // end result.
   224   //
   225   if ((ifr.ifr_flags & IFF_PROMISC) == 0)
   226     {
   227       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): " << m_deviceName << " is not in promiscuous mode");
   228     }
   229 
   230   //
   231   // Now spin up a read thread to read packets.
   232   //
   233   if (m_readThread != 0)
   234     {
   235       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Receive thread is already running");
   236     }
   237 
   238   NS_LOG_LOGIC ("Spinning up read thread");
   239 
   240   m_readThread = Create<SystemThread> (MakeCallback (&EmuNetDevice::ReadThread, this));
   241   m_readThread->Start ();
   242 
   243   NotifyLinkUp ();
   244 }
   245 
   246 void
   247 EmuNetDevice::CreateSocket (void)
   248 {
   249   NS_LOG_FUNCTION_NOARGS ();
   250   //
   251   // We want to create a raw socket for our net device.  Unfortunately for us
   252   // you have to have root privileges to do that.  Instead of running the 
   253   // entire simulation as root, we decided to make a small program who's whole
   254   // reason for being is to run as suid root and create a raw socket.  We're
   255   // going to fork and exec that program soon, but we need to have a socket
   256   // to talk to it with.  So we create a local interprocess (Unix) socket 
   257   // for that purpose.
   258   //
   259   int sock = socket (PF_UNIX, SOCK_DGRAM, 0);
   260   if (sock == -1)
   261     {
   262       NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Unix socket creation error, errno = " << strerror (errno));
   263     }
   264 
   265   //
   266   // Bind to that socket and let the kernel allocate an endpoint
   267   //
   268   struct sockaddr_un un;
   269   memset (&un, 0, sizeof (un));
   270   un.sun_family = AF_UNIX;
   271   int status = bind (sock, (struct sockaddr*)&un, sizeof (sa_family_t));
   272   if (status == -1)
   273     {
   274       NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Could not bind(): errno = " << strerror (errno));
   275     }
   276 
   277   NS_LOG_INFO ("Created Unix socket");
   278   NS_LOG_INFO ("sun_family = " << un.sun_family);
   279   NS_LOG_INFO ("sun_path = " << un.sun_path);
   280 
   281   //
   282   // We have a socket here, but we want to get it there -- to the program we're
   283   // going to exec.  What we'll do is to do a getsockname and then encode the
   284   // resulting address information as a string, and then send the string to the
   285   // program as an argument.  So we need to get the sock name.
   286   //
   287   socklen_t len = sizeof (un);
   288   status = getsockname (sock, (struct sockaddr*)&un, &len);
   289   if (status == -1)
   290     {
   291       NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Could not getsockname(): errno = " << strerror (errno));
   292     }
   293 
   294   //
   295   // Now encode that socket name (family and path) as a string of hex digits
   296   //
   297   std::string path = EmuBufferToString((uint8_t *)&un, len);
   298   NS_LOG_INFO ("Encoded Unix socket as \"" << path << "\"");
   299   //
   300   // Fork and exec the process to create our socket.  If we're us (the parent)
   301   // we wait for the child (the socket creator) to complete and read the 
   302   // socket it created using the ancillary data mechanism.
   303   //
   304   pid_t pid = ::fork ();
   305   if (pid == 0)
   306     {
   307       NS_LOG_DEBUG ("Child process");
   308 
   309       //
   310       // build a command line argument from the encoded endpoint string that 
   311       // the socket creation process will use to figure out how to respond to
   312       // the (now) parent process.
   313       //
   314       std::ostringstream oss;
   315       oss << "-p" << path;
   316       NS_LOG_INFO ("Parameters set to \"" << oss.str () << "\"");
   317 
   318       //
   319       // Execute the socket creation process image.
   320       //
   321       status = ::execl (FindCreator ().c_str (), "emu-sock-creator", oss.str ().c_str (), (char *)NULL);
   322 
   323       //
   324       // If the execl successfully completes, it never returns.  If it returns it failed or the OS is
   325       // broken.  In either case, we bail.
   326       //
   327       NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Back from execl(), errno = " << ::strerror (errno));
   328     }
   329   else
   330     {
   331       NS_LOG_DEBUG ("Parent process");
   332       //
   333       // We're the process running the emu net device.  We need to wait for the
   334       // socket creator process to finish its job.
   335       //
   336       int st;
   337       pid_t waited = waitpid (pid, &st, 0);
   338       if (waited == -1)
   339 	{
   340 	  NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): waitpid() fails, errno = " << strerror (errno));
   341 	}
   342       NS_ASSERT_MSG (pid == waited, "EmuNetDevice::CreateSocket(): pid mismatch");
   343 
   344       //
   345       // Check to see if the socket creator exited normally and then take a 
   346       // look at the exit code.  If it bailed, so should we.  If it didn't
   347       // even exit normally, we bail too.
   348       //
   349       if (WIFEXITED (st))
   350 	{
   351           int exitStatus = WEXITSTATUS (st);
   352           if (exitStatus != 0)
   353             {
   354               NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): socket creator exited normally with status " << exitStatus);
   355             }
   356 	}
   357       else 
   358 	{
   359           NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): socket creator exited abnormally");
   360 	}
   361 
   362       //
   363       // At this point, the socket creator has run successfully and should 
   364       // have created our raw socket and sent it back to the socket address
   365       // we provided.  Our socket should be waiting on the Unix socket.  We've
   366       // got to do a bunch of grunto work to get at it, though.
   367       //
   368       // The struct iovec below is part of a scatter-gather list.  It describes a
   369       // buffer.  In this case, it describes a buffer (an integer) that will
   370       // get the data that comes back from the socket creator process.  It will
   371       // be a magic number that we use as a consistency/sanity check.
   372       // 
   373       struct iovec iov;
   374       uint32_t magic;
   375       iov.iov_base = &magic;
   376       iov.iov_len = sizeof(magic);
   377 
   378       //
   379       // The CMSG macros you'll see below are used to create and access control 
   380       // messages (which is another name for ancillary data).  The ancillary 
   381       // data is made up of pairs of struct cmsghdr structures and associated
   382       // data arrays.
   383       //
   384       // First, we're going to allocate a buffer on the stack to receive our 
   385       // data array (that contains the socket).  Sometimes you'll see this called
   386       // an "ancillary element" but the msghdr uses the control message termimology
   387       // so we call it "control."
   388       //
   389       size_t msg_size = sizeof(int);
   390       char control[CMSG_SPACE(msg_size)];
   391 
   392       //
   393       // There is a msghdr that is used to minimize the number of parameters
   394       // passed to recvmsg (which we will use to receive our ancillary data).  
   395       // This structure uses terminology corresponding to control messages, so
   396       // you'll see msg_control, which is the pointer to the ancillary data and 
   397       // controllen which is the size of the ancillary data array.
   398       //
   399       // So, initialize the message header that describes the ancillary/control
   400       // data we expect to receive and point it to buffer.
   401       //
   402       struct msghdr msg;
   403       msg.msg_name = 0;
   404       msg.msg_namelen = 0;
   405       msg.msg_iov = &iov;
   406       msg.msg_iovlen = 1;
   407       msg.msg_control = control;
   408       msg.msg_controllen = sizeof (control);
   409       msg.msg_flags = 0;
   410 
   411       //
   412       // Now we can actually receive the interesting bits from the socket
   413       // creator process.
   414       //
   415       ssize_t bytesRead = recvmsg (sock, &msg, 0);
   416       if (bytesRead != sizeof(int))
   417 	{
   418           NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Wrong byte count from socket creator");
   419 	}
   420 
   421       //
   422       // There may be a number of message headers/ancillary data arrays coming in.
   423       // Let's look for the one with a type SCM_RIGHTS which indicates it' the
   424       // one we're interested in.
   425       //
   426       struct cmsghdr *cmsg;
   427       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) 
   428 	{
   429 	  if (cmsg->cmsg_level == SOL_SOCKET &&
   430 	      cmsg->cmsg_type == SCM_RIGHTS)
   431 	    {
   432               //
   433               // This is the type of message we want.  Check to see if the magic 
   434               // number is correct and then pull out the socket we care about if
   435               // it matches
   436               //
   437               if (magic == EMU_MAGIC)
   438                 {
   439                   NS_LOG_INFO ("Got SCM_RIGHTS with correct magic " << magic);
   440                   int *rawSocket = (int*)CMSG_DATA (cmsg);
   441                   NS_LOG_INFO ("Got the socket from the socket creator = " << *rawSocket);
   442                   m_sock = *rawSocket;
   443                   return;
   444                 }
   445               else
   446                 {
   447                   NS_LOG_INFO ("Got SCM_RIGHTS, but with bad magic " << magic);                  
   448                 }
   449 	    }
   450 	}
   451       NS_FATAL_ERROR ("Did not get the raw socket from the socket creator");
   452     }
   453 }
   454 
   455 std::string
   456 EmuNetDevice::FindCreator (void)
   457 {
   458   struct stat st;
   459   std::string debug = "./build/debug/src/devices/emu/emu-sock-creator";
   460   std::string optimized = "./build/optimized/src/devices/emu/emu-sock-creator";
   461 
   462   if (::stat (debug.c_str (), &st) == 0)
   463     {
   464       return debug;
   465     }
   466 
   467   if (::stat (optimized.c_str (), &st) == 0)
   468     {
   469       return optimized;
   470     }
   471 
   472   NS_FATAL_ERROR ("EmuNetDevice::FindCreator(): Couldn't find creator");
   473   return ""; // quiet compiler
   474 }
   475 
   476 void
   477 EmuNetDevice::StopDevice (void)
   478 {
   479   NS_LOG_FUNCTION_NOARGS ();
   480 
   481   close (m_sock);
   482   m_sock = -1;
   483 
   484   NS_ASSERT_MSG (m_readThread != 0, "EmuNetDevice::StopDevice(): Receive thread is not running");
   485 
   486   NS_LOG_LOGIC ("Joining read thread");
   487   m_readThread->Join ();
   488   m_readThread = 0;
   489 }
   490 
   491 void
   492 EmuNetDevice::ForwardUp (uint8_t *buf, uint32_t len)
   493 {
   494   NS_LOG_FUNCTION (buf << len);
   495 
   496   //
   497   // Create a packet out of the buffer we received and free that buffer.
   498   //
   499   Ptr<Packet> packet = Create<Packet> (reinterpret_cast<const uint8_t *> (buf), len);
   500   free (buf);
   501   buf = 0;
   502 
   503   //
   504   // Trace sinks will expect complete packets, not packets without some of the
   505   // headers.
   506   //
   507   Ptr<Packet> originalPacket = packet->Copy ();
   508 
   509   //
   510   // Checksum the packet
   511   //
   512   EthernetTrailer trailer;
   513   packet->RemoveTrailer (trailer);
   514   trailer.CheckFcs (packet);
   515 
   516   EthernetHeader header (false);
   517   packet->RemoveHeader (header);
   518 
   519   NS_LOG_LOGIC ("Pkt source is " << header.GetSource ());
   520   NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ());
   521 
   522   LlcSnapHeader llc;
   523   packet->RemoveHeader (llc);
   524   uint16_t protocol = llc.GetType ();
   525 
   526   PacketType packetType;
   527       
   528   if (header.GetDestination ().IsBroadcast ())
   529     {
   530       NS_LOG_LOGIC ("Pkt destination is PACKET_BROADCAST");
   531       packetType = NS3_PACKET_BROADCAST;
   532     }
   533   else if (header.GetDestination ().IsMulticast ())
   534     {
   535       NS_LOG_LOGIC ("Pkt destination is PACKET_MULTICAST");
   536       packetType = NS3_PACKET_MULTICAST;
   537     }
   538   else if (header.GetDestination () == m_address)
   539     {
   540       NS_LOG_LOGIC ("Pkt destination is PACKET_HOST");
   541       packetType = NS3_PACKET_HOST;
   542     }
   543   else
   544     {
   545       NS_LOG_LOGIC ("Pkt destination is PACKET_OTHERHOST");
   546       packetType = NS3_PACKET_OTHERHOST;
   547     }
   548 
   549   if (!m_promiscRxCallback.IsNull ())
   550     {
   551       NS_LOG_LOGIC ("calling m_promiscRxCallback");
   552       m_promiscRxCallback (this, packet->Copy (), protocol, header.GetSource (), header.GetDestination (), packetType);
   553     }
   554 
   555   if (packetType != NS3_PACKET_OTHERHOST)
   556     {
   557       m_rxTrace (originalPacket);
   558       NS_LOG_LOGIC ("calling m_rxCallback");
   559       m_rxCallback (this, packet, protocol, header.GetSource ());
   560     }
   561 }
   562 
   563 void
   564 EmuNetDevice::ReadThread (void)
   565 {
   566   NS_LOG_FUNCTION_NOARGS ();
   567 
   568   //
   569   // It's important to remember that we're in a completely different thread than the simulator is running in.  We
   570   // need to synchronize with that other thread to get the packet up into ns-3.  What we will need to do is to schedule 
   571   // a method to forward up the packet using the multithreaded simulator we are most certainly running.  However, I just 
   572   // said it -- we are talking about two threads here, so it is very, very dangerous to do any kind of reference couning
   573   // on a shared object.  Just don't do it.  So what we're going to do is to allocate a buffer on the heap and pass that
   574   // buffer into the ns-3 context thread where it will create the packet.
   575   //
   576 
   577   int32_t len = -1;
   578   struct sockaddr_ll addr;
   579   socklen_t addrSize = sizeof (addr);
   580 
   581   for (;;) 
   582     {
   583       uint32_t bufferSize = 65536;
   584       uint8_t *buf = (uint8_t *)malloc (bufferSize);
   585       if (buf == 0)
   586         {
   587           NS_FATAL_ERROR ("EmuNetDevice::ReadThread(): malloc packet buffer failed");
   588         }
   589 
   590       NS_LOG_LOGIC ("Calling recvfrom");
   591       len = recvfrom (m_sock, buf, bufferSize, 0, (struct sockaddr *)&addr, &addrSize);
   592 
   593       if (len == -1)
   594         {
   595           free (buf);
   596           buf = 0;
   597           return;
   598         }
   599 
   600       NS_LOG_INFO ("EmuNetDevice::ReadThread(): Received packet");
   601       NS_LOG_INFO ("EmuNetDevice::ReadThread(): Scheduling handler");
   602       DynamicCast<RealtimeSimulatorImpl> (Simulator::GetImplementation ())->ScheduleRealtimeNow (
   603         MakeEvent (&EmuNetDevice::ForwardUp, this, buf, len));
   604       buf = 0;
   605     }
   606 }
   607 
   608 bool 
   609 EmuNetDevice::Send (Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber)
   610 {
   611   NS_LOG_FUNCTION (packet << dest << protocolNumber);
   612   //
   613   // The immediate questions here are how are we going to encapsulate packets and what do we use as the MAC source and 
   614   // destination (hardware) addresses?
   615   //
   616   // If we return false from EmuNetDevice::NeedsArp, the ArpIpv4Interface will pass the broadcast address as the 
   617   // hardware (Ethernet) destination by default.  If we return true from EmuNetDevice::NeedsArp, then the hardware
   618   // destination is actually meaningful, but we'll have an ns-3 ARP running on this device.  There can also be an ARP
   619   // running on the underlying OS so we have to be very careful, both about multiple ARPs and also about TCP, UDP, etc.
   620   //
   621   // We are operating in promiscuous mode on the receive side (all ns-3 net devices are required to implement the 
   622   // promiscuous callback in a meaningful way), so we have an option regarding the hardware addresses.  We don't actually have
   623   // to use the real hardware addresses and IP addresses of the underlying system.  We can completely use MAC-spoofing to
   624   // fake out the OS by using the ns-3 assigned MAC address (and also the ns-3 assigned IP addresses).  Ns-3 starts its 
   625   // MAC address allocation using the OUI (vendor-code) 00:00:00 which is unassigned to any organization and is a globally
   626   // administered address, so there shouldn't be any collisions with real hardware.
   627   //
   628   // So what we do is we return true from EmuNetDevice::NeedsArp which tells ns-3 to use its own ARP.  We spoof the 
   629   // MAC address of the device and use promiscuous mode to receive traffic destined to that address.
   630   //
   631   return SendFrom (packet, m_address, dest, protocolNumber);
   632 }
   633 
   634 bool 
   635 EmuNetDevice::SendFrom (Ptr<Packet> packet, const Address &src, const Address &dest, uint16_t protocolNumber)
   636 {
   637   NS_LOG_FUNCTION (packet << src << dest << protocolNumber);
   638 
   639   if (IsLinkUp () == false)
   640     {
   641       NS_LOG_LOGIC ("Link is down, returning");
   642       return false;
   643     }
   644 
   645   Mac48Address destination = Mac48Address::ConvertFrom (dest);
   646   Mac48Address source = Mac48Address::ConvertFrom (src);
   647 
   648   NS_LOG_LOGIC ("Transmit packet with UID " << packet->GetUid ());
   649   NS_LOG_LOGIC ("Transmit packet from " << source);
   650   NS_LOG_LOGIC ("Transmit packet to " << destination);
   651 
   652 #if 0
   653   {
   654     struct ifreq ifr;
   655     bzero (&ifr, sizeof(ifr));
   656     strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ);
   657 
   658     NS_LOG_LOGIC ("Getting MAC address");
   659     int32_t rc = ioctl (m_sock, SIOCGIFHWADDR, &ifr);
   660     if (rc == -1)
   661       {
   662         NS_FATAL_ERROR ("EmuNetDevice::SendFrom(): Can't get MAC address");
   663       }
   664 
   665     std::ostringstream oss;
   666     oss << std::hex <<
   667       (ifr.ifr_hwaddr.sa_data[0] & 0xff) << ":" <<
   668       (ifr.ifr_hwaddr.sa_data[1] & 0xff) << ":" <<
   669       (ifr.ifr_hwaddr.sa_data[2] & 0xff) << ":" <<
   670       (ifr.ifr_hwaddr.sa_data[3] & 0xff) << ":" <<
   671       (ifr.ifr_hwaddr.sa_data[4] & 0xff) << ":" <<
   672       (ifr.ifr_hwaddr.sa_data[5] & 0xff) << std::dec;
   673 
   674     NS_LOG_LOGIC ("Fixup source to HW MAC " << oss.str ());
   675     source = Mac48Address (oss.str ().c_str ());
   676     NS_LOG_LOGIC ("source now " << source);
   677   }
   678 #endif
   679 
   680   LlcSnapHeader llc;
   681   llc.SetType (protocolNumber);
   682   packet->AddHeader (llc);
   683 
   684   EthernetHeader header (false);
   685   header.SetSource (source);
   686   header.SetDestination (destination);
   687   header.SetLengthType (packet->GetSize ());
   688   packet->AddHeader (header);
   689 
   690   EthernetTrailer trailer;
   691   trailer.CalcFcs (packet);
   692   packet->AddTrailer (trailer);
   693 
   694   // 
   695   // Enqueue and dequeue the packet to hit the tracing hooks.
   696   //
   697   m_queue->Enqueue (packet);
   698   packet = m_queue->Dequeue ();
   699 
   700   struct sockaddr_ll ll;
   701   bzero (&ll, sizeof (ll));
   702 
   703   ll.sll_family = AF_PACKET;
   704   ll.sll_ifindex = m_sll_ifindex;
   705   ll.sll_protocol = htons(ETH_P_ALL); 
   706 
   707   NS_LOG_LOGIC ("calling sendto");
   708 
   709   int32_t rc;
   710   rc = sendto (m_sock, packet->PeekData (), packet->GetSize (), 0, reinterpret_cast<struct sockaddr *> (&ll), sizeof (ll));
   711 
   712   NS_LOG_LOGIC ("sendto returns " << rc);
   713 
   714   return rc == -1 ? false : true;
   715 }
   716 
   717 void 
   718 EmuNetDevice::SetDataRate(DataRate bps)
   719 {
   720   NS_LOG_FUNCTION (this << bps);
   721   NS_FATAL_ERROR ("EmuNetDevice::SetDataRate():  Unable."); 
   722 }
   723 
   724 void
   725 EmuNetDevice::SetQueue (Ptr<Queue> q)
   726 {
   727   NS_LOG_FUNCTION (this << q);
   728   m_queue = q;
   729 }
   730 
   731 Ptr<Queue> 
   732 EmuNetDevice::GetQueue(void) const 
   733 { 
   734   NS_LOG_FUNCTION_NOARGS ();
   735   return m_queue;
   736 }
   737 
   738 void
   739 EmuNetDevice::NotifyLinkUp (void)
   740 {
   741   m_linkUp = true;
   742   if (!m_linkChangeCallback.IsNull ())
   743     {
   744       m_linkChangeCallback ();
   745     }
   746 }
   747 
   748 void 
   749 EmuNetDevice::SetName(const std::string name)
   750 {
   751   m_name = name;
   752 }
   753 
   754 std::string 
   755 EmuNetDevice::GetName(void) const
   756 {
   757   return m_name;
   758 }
   759 
   760 void 
   761 EmuNetDevice::SetIfIndex(const uint32_t index)
   762 {
   763   m_ifIndex = index;
   764 }
   765 
   766 uint32_t 
   767 EmuNetDevice::GetIfIndex(void) const
   768 {
   769   return m_ifIndex;
   770 }
   771 
   772 Ptr<Channel> 
   773 EmuNetDevice::GetChannel (void) const
   774 {
   775   NS_FATAL_ERROR ("EmuNetDevice::GetChannel():  Unable."); 
   776   return 0;
   777 }
   778 
   779 void 
   780 EmuNetDevice::SetAddress (Mac48Address addr)
   781 {
   782   NS_LOG_FUNCTION (addr);
   783   m_address = addr;
   784 }
   785 
   786 Address 
   787 EmuNetDevice::GetAddress (void) const
   788 {
   789   NS_LOG_FUNCTION_NOARGS ();
   790   return m_address;
   791 }
   792 
   793 bool 
   794 EmuNetDevice::SetMtu (const uint16_t mtu)
   795 {
   796   NS_FATAL_ERROR ("EmuNetDevice::SetMtu():  Unable."); 
   797   return false;
   798 }
   799 
   800 uint16_t 
   801 EmuNetDevice::GetMtu (void) const
   802 {
   803   struct ifreq ifr;
   804   bzero (&ifr, sizeof (ifr));
   805   strcpy(ifr.ifr_name, m_deviceName.c_str ());
   806 
   807   int32_t fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
   808 
   809 
   810   int32_t rc = ioctl(fd, SIOCGIFMTU, &ifr);
   811   if (rc == -1)
   812     {
   813       NS_FATAL_ERROR ("EmuNetDevice::GetMtu(): Can't ioctl SIOCGIFMTU");
   814     }
   815 
   816   close (fd);
   817 
   818   return ifr.ifr_mtu;
   819 }
   820 
   821 bool 
   822 EmuNetDevice::IsLinkUp (void) const
   823 {
   824   return m_linkUp;
   825 }
   826 
   827 void 
   828 EmuNetDevice::SetLinkChangeCallback (Callback<void> callback)
   829 {
   830   m_linkChangeCallback = callback;
   831 }
   832 
   833 bool 
   834 EmuNetDevice::IsBroadcast (void) const
   835 {
   836   return true;
   837 }
   838 
   839 Address
   840 EmuNetDevice::GetBroadcast (void) const
   841 {
   842   return Mac48Address ("ff:ff:ff:ff:ff:ff");
   843 }
   844 
   845 bool 
   846 EmuNetDevice::IsMulticast (void) const
   847 {
   848   return false;
   849 }
   850 
   851   Address 
   852 EmuNetDevice::GetMulticast (Ipv4Address multicastGroup) const
   853 {
   854   NS_LOG_FUNCTION (multicastGroup);
   855 
   856   Mac48Address ad = Mac48Address::GetMulticast (multicastGroup);
   857 
   858   //
   859   // Implicit conversion (operator Address ()) is defined for Mac48Address, so
   860   // use it by just returning the EUI-48 address which is automagically converted
   861   // to an Address.
   862   //
   863   NS_LOG_LOGIC ("multicast address is " << ad);
   864 
   865   return ad;
   866 }
   867 
   868 Address
   869 EmuNetDevice::GetMulticast (Ipv6Address addr) const
   870 {
   871   NS_LOG_FUNCTION(this << addr);
   872 
   873   Mac48Address ad = Mac48Address::GetMulticast (addr);
   874   NS_LOG_LOGIC("MAC IPv6 multicast address is " << ad);
   875 
   876   return ad;
   877 }
   878 
   879 bool 
   880 EmuNetDevice::IsPointToPoint (void) const
   881 {
   882   return false;
   883 }
   884 
   885 void
   886 EmuNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
   887 {
   888   NS_FATAL_ERROR ("EmuNetDevice::SetPromiscReceiveCallback(): Not implemented");
   889 }
   890 
   891   bool 
   892 EmuNetDevice::SupportsSendFrom () const
   893 {
   894   NS_LOG_FUNCTION_NOARGS ();
   895   return true;
   896 }
   897 
   898 
   899 Ptr<Node> 
   900 EmuNetDevice::GetNode (void) const
   901 {
   902   return m_node;
   903 }
   904 
   905 void 
   906 EmuNetDevice::SetNode (Ptr<Node> node)
   907 {
   908   m_node = node;
   909 }
   910 
   911 bool 
   912 EmuNetDevice::NeedsArp (void) const
   913 {
   914   return true;
   915 }
   916 
   917 void 
   918 EmuNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
   919 {
   920   m_rxCallback = cb;
   921 }
   922 
   923 } // namespace ns3