src/devices/emu/emu-net-device.h
author vincent@clarinet.u-strasbg.fr
Fri Nov 07 11:36:15 2008 -0800 (2008-11-07)
changeset 3852 9cf7ad0cac85
parent 3843 ca2159d54d75
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 #ifndef EMU_NET_DEVICE_H
    20 #define EMU_NET_DEVICE_H
    21 
    22 #include <string.h>
    23 #include "ns3/address.h"
    24 #include "ns3/net-device.h"
    25 #include "ns3/node.h"
    26 #include "ns3/callback.h"
    27 #include "ns3/packet.h"
    28 #include "ns3/traced-callback.h"
    29 #include "ns3/event-id.h"
    30 #include "ns3/nstime.h"
    31 #include "ns3/data-rate.h"
    32 #include "ns3/ptr.h"
    33 #include "ns3/mac48-address.h"
    34 #include "ns3/system-thread.h"
    35 
    36 namespace ns3 {
    37 
    38 class Queue;
    39 
    40 /**
    41  * \class EmuNetDevice
    42  * \brief A Device for an Emu Network Link.
    43  */
    44 class EmuNetDevice : public NetDevice 
    45 {
    46 public:
    47   static TypeId GetTypeId (void);
    48 
    49   /**
    50    * Construct a EmuNetDevice
    51    *
    52    * This is the constructor for the EmuNetDevice.  It takes as a
    53    */
    54   EmuNetDevice ();
    55 
    56   /**
    57    * Destroy a EmuNetDevice
    58    *
    59    * This is the destructor for the EmuNetDevice.
    60    */
    61   virtual ~EmuNetDevice ();
    62 
    63   /**
    64    * Set the Data Rate used for transmission of packets.  
    65    *
    66    * @see Attach ()
    67    * @param bps the data rate at which this object operates
    68    */
    69   void SetDataRate (DataRate bps);
    70 
    71   /**
    72    * Set a start time for the device.
    73    *
    74    * @param tStart the start time
    75    */
    76   void Start (Time tStart);
    77 
    78   /**
    79    * Set a stop time for the device.
    80    *
    81    * @param tStop the stop time
    82    */
    83   void Stop (Time tStop);
    84 
    85   /**
    86    * Attach a queue to the EmuNetDevice.
    87    *
    88    * The EmuNetDevice "owns" a queue that implements a queueing 
    89    * method such as DropTail or RED.  
    90    *
    91    * @see Queue
    92    * @see DropTailQueue
    93    * @param queue Ptr to the new queue.
    94    */
    95   void SetQueue (Ptr<Queue> queue);
    96 
    97   /**
    98    * Assign a MAC address to this device.
    99    *
   100    * @see Mac48Address
   101    * @param addr The new address.
   102    */
   103   void SetAddress (Mac48Address addr);
   104 
   105 //
   106 // Pure virtual methods inherited from NetDevice we must implement.
   107 //
   108   virtual void SetName(const std::string name);
   109   virtual std::string GetName(void) const;
   110 
   111   virtual void SetIfIndex(const uint32_t index);
   112   virtual uint32_t GetIfIndex(void) const;
   113 
   114   virtual Ptr<Channel> GetChannel (void) const;
   115   virtual Address GetAddress (void) const;
   116 
   117   virtual bool SetMtu (const uint16_t mtu);
   118   virtual uint16_t GetMtu (void) const;
   119 
   120   virtual bool IsLinkUp (void) const;
   121 
   122   virtual void SetLinkChangeCallback (Callback<void> callback);
   123 
   124   virtual bool IsBroadcast (void) const;
   125   virtual Address GetBroadcast (void) const;
   126 
   127   virtual bool IsMulticast (void) const;
   128 
   129   /**
   130    * \brief Make and return a MAC multicast address using the provided
   131    *        multicast group
   132    *
   133    * RFC 1112 says that an Ipv4 host group address is mapped to an Ethernet 
   134    * multicast address by placing the low-order 23-bits of the IP address into 
   135    * the low-order 23 bits of the Ethernet multicast address 
   136    * 01-00-5E-00-00-00 (hex).
   137    *
   138    * This method performs the multicast address creation function appropriate
   139    * to an EUI-48-based CSMA device.  This MAC address is encapsulated in an
   140    *  abstract Address to avoid dependencies on the exact address format.
   141    *
   142    * \param multicastGroup The IP address for the multicast group destination
   143    * of the packet.
   144    * \return The MAC multicast Address used to send packets to the provided
   145    * multicast group.
   146    *
   147    * \see Ipv4Address
   148    * \see Mac48Address
   149    * \see Address
   150    */
   151   virtual Address GetMulticast (Ipv4Address multicastGroup) const;
   152 
   153   /**
   154    * \brief Get the MAC multicast address corresponding
   155    * to the IPv6 address provided.
   156    * \param addr IPv6 address
   157    * \return the MAC multicast address
   158    * \warning Calling this method is invalid if IsMulticast returns not true.
   159    */
   160   virtual Address GetMulticast (Ipv6Address addr) const;
   161 
   162   /**
   163    * Is this a point to point link?
   164    * \returns false.
   165    */
   166   virtual bool IsPointToPoint (void) const;
   167 
   168   virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber);
   169 
   170   virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
   171 
   172   virtual Ptr<Node> GetNode (void) const;
   173   virtual void SetNode (Ptr<Node> node);
   174 
   175   virtual bool NeedsArp (void) const;
   176 
   177   virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
   178   virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb);
   179 
   180   virtual bool SupportsSendFrom (void) const;
   181 
   182 private:
   183 
   184   virtual void DoDispose (void);
   185 
   186   /**
   187    * Call out to a separate process running as suid root in order to get a raw 
   188    * socket.  We do this to avoid having the entire simulation running as root.
   189    * If this method returns, we'll have a raw socket waiting for us in m_sock.
   190    */
   191   void CreateSocket (void);
   192 
   193   /**
   194    * Figure out where the raw socket creation process lives on the system.
   195    */
   196   std::string FindCreator (void);
   197 
   198   /**
   199    * Get a copy of the attached Queue.
   200    *
   201    * This method is provided for any derived class that may need to get
   202    * direct access to the underlying queue.
   203    *
   204    * @returns Ptr to the queue.
   205    */
   206   Ptr<Queue> GetQueue(void) const; 
   207 
   208   /**
   209    * Spin up the device
   210    */
   211   void StartDevice (void);
   212 
   213   /**
   214    * Tear down the device
   215    */
   216   void StopDevice (void);
   217 
   218   /**
   219    * Loop to read and process packets
   220    */
   221   void ReadThread (void);
   222 
   223   /**
   224    * Method to handle received packets.  Synchronized with simulator via ScheduleNow from ReadThread.
   225    */
   226   void ForwardUp (uint8_t *buf, uint32_t len);
   227 
   228   /**
   229    * Adds the necessary headers and trailers to a packet of data in order to
   230    * respect the protocol implemented by the agent.
   231    */
   232   void AddHeader(Ptr<Packet> p, uint16_t protocolNumber);
   233 
   234   /**
   235    * Removes, from a packet of data, all headers and trailers that
   236    * relate to the protocol implemented by the agent
   237    * \return Returns true if the packet should be forwarded up the
   238    * protocol stack.
   239    */
   240   bool ProcessHeader(Ptr<Packet> p, uint16_t& param);
   241 
   242   /**
   243    * Start Sending a Packet Down the Wire.
   244    *
   245    * @returns true if success, false on failure
   246    */
   247   bool TransmitStart (Ptr<Packet> p);
   248 
   249   void NotifyLinkUp (void);
   250 
   251   /**
   252    * The Queue which this EmuNetDevice uses as a packet source.
   253    * Management of this Queue has been delegated to the EmuNetDevice
   254    * and it has the responsibility for deletion.
   255    * @see class Queue
   256    * @see class DropTailQueue
   257    */
   258   Ptr<Queue> m_queue;
   259 
   260   /**
   261    * The trace source for the packet reception events that the device can
   262    * fire.
   263    *
   264    * @see class CallBackTraceSource
   265    */
   266   TracedCallback<Ptr<const Packet> > m_rxTrace;
   267 
   268   /**
   269    * The trace source for the packet drop events that the device can
   270    * fire.
   271    *
   272    * @see class CallBackTraceSource
   273    */
   274   TracedCallback<Ptr<const Packet> > m_dropTrace;
   275 
   276   /**
   277    * Time to start spinning up the device
   278    */
   279   Time m_tStart;
   280 
   281   /**
   282    * Time to start tearing down the device
   283    */
   284   Time m_tStop;
   285 
   286   EventId m_startEvent;
   287   EventId m_stopEvent;
   288 
   289   int32_t m_sock;
   290 
   291   Ptr<SystemThread> m_readThread;
   292 
   293   /**
   294    * The Node to which this device is attached.
   295    */
   296   Ptr<Node> m_node;
   297 
   298   /**
   299    * The MAC address which has been assigned to this device.
   300    */
   301   Mac48Address m_address;
   302 
   303   /**
   304    * The callback used to notify higher layers that a packet has been received.
   305    */
   306   NetDevice::ReceiveCallback m_rxCallback;
   307 
   308   /**
   309    * The callback used to notify higher layers that a packet has been received in promiscuous mode.
   310    */
   311   NetDevice::PromiscReceiveCallback m_promiscRxCallback;
   312 
   313   /**
   314    * The ns-3 interface index (in the sense of net device index) that has been assigned to this network device.
   315    */
   316   uint32_t m_ifIndex;
   317 
   318   /**
   319    * The Unix interface index that we got from the system and which corresponds to the interface (e.g., "eth1")
   320    * we are using to talk to the network.  Valid when m_sock is valid.
   321    */
   322   int32_t m_sll_ifindex;
   323 
   324   /**
   325    * The human readable name of this device.
   326    */
   327   std::string m_name;
   328 
   329   /**
   330    * Flag indicating whether or not the link is up.  In this case,
   331    * whether or not the device is connected to a channel.
   332    */
   333   bool m_linkUp;
   334 
   335   /**
   336    * Callback to fire if the link changes state (up or down).
   337    */
   338   Callback<void> m_linkChangeCallback;
   339 
   340   /**
   341    * The unix/linux name of the underlying device (e.g., eth0)
   342    */
   343   std::string m_deviceName;
   344 };
   345 
   346 } // namespace ns3
   347 
   348 #endif // EMU_NET_DEVICE_H
   349