src/node/net-device.h
changeset 234 6124bda39cb3
child 247 fb7375bb43d7
equal deleted inserted replaced
233:6b60d7b27ae4 234:6124bda39cb3
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2005,2006 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 #ifndef NET_DEVICE_H
       
    22 #define NET_DEVICE_H
       
    23 
       
    24 #include <string>
       
    25 #include <stdint.h>
       
    26 #include "ns3/callback.h"
       
    27 #include "ns3/packet.h"
       
    28 #include "mac-address.h"
       
    29 
       
    30 namespace ns3 {
       
    31 
       
    32 class Ipv4L4Demux;
       
    33 class Node;
       
    34 
       
    35 /**
       
    36  * \brief Network layer to device interface
       
    37  *
       
    38  * This interface defines the API which the IP and ARP
       
    39  * layers need to access to manage an instance of a network device 
       
    40  * layer. It currently does not support MAC-level 
       
    41  * multicast but this should not be too hard to add by adding
       
    42  * extra methods to register MAC multicast addresses to
       
    43  * filter out unwanted packets before handing them to the
       
    44  * higher layers.
       
    45  *
       
    46  * In Linux, this interface is analogous to the interface
       
    47  * just above dev_queue_xmit() (i.e., IP packet is fully
       
    48  * constructed with destination MAC address already selected).
       
    49  * 
       
    50  * If you want to write a new MAC layer, you need to subclass
       
    51  * this base class and implement your own version of the 
       
    52  * NetDevice::SendTo method.
       
    53  */
       
    54 class NetDevice {
       
    55  public:
       
    56   /**
       
    57    * \param node base class node pointer of device's node 
       
    58    */
       
    59   NetDevice(Node& node, const MacAddress& addr);
       
    60   virtual ~NetDevice() {}
       
    61   /**
       
    62    * \return the current MacAddress of this interface.
       
    63    */
       
    64   MacAddress GetAddress (void) const;
       
    65   /**
       
    66    * \param mtu MTU value, in bytes, to set for the device
       
    67    * \return whether the MTU value was within legal bounds
       
    68    * 
       
    69    * Override for default MTU defined on a per-type basis.
       
    70    */
       
    71   bool SetMtu (const uint16_t mtu);
       
    72   /**
       
    73    * \return the link-level MTU in bytes for this interface.
       
    74    * 
       
    75    * This value is typically used by the IP layer to perform
       
    76    * IP fragmentation when needed.
       
    77    */
       
    78   uint16_t GetMtu (void) const;
       
    79   /**
       
    80    * \param name name of the device (e.g. "eth0")
       
    81    */
       
    82   void SetName(const std::string name); 
       
    83   /**
       
    84    * \return name name of the device (e.g. "eth0")
       
    85    */
       
    86   std::string GetName(void) const;
       
    87   /**
       
    88    * \return true if link is up; false otherwise
       
    89    */
       
    90   bool IsLinkUp (void) const;
       
    91   /**
       
    92    * \param callback the callback to invoke
       
    93    *
       
    94    * Register a callback invoked whenever the link 
       
    95    * status changes to UP. This callback is typically used
       
    96    * by the IP/ARP layer to flush the ARP cache 
       
    97    * whenever the link goes up.
       
    98    */
       
    99   void SetLinkChangeCallback (Callback<void> callback);
       
   100   /**
       
   101    * \return true if this interface supports a broadcast address,
       
   102    *         false otherwise.
       
   103    */
       
   104   bool IsBroadcast (void) const;
       
   105   /**
       
   106    * \return the broadcast address supported by
       
   107    *         this netdevice.
       
   108    *
       
   109    * Calling this method is invalid if IsBroadcast returns
       
   110    * not true.
       
   111    */
       
   112   MacAddress const &GetBroadcast (void) const;
       
   113   /**
       
   114    * \return value of m_isMulticast flag
       
   115    */
       
   116   bool IsMulticast (void) const;
       
   117   /**
       
   118    * \return value of m_isPointToPoint flag
       
   119    */
       
   120   bool IsPointToPoint (void) const;
       
   121   /**
       
   122    * \param p packet sent from above down to Network Device
       
   123    * \param dest mac address of the destination (already resolved)
       
   124    * \param protocolNumber identifies the type of payload contained in
       
   125    *        this packet. Used to call the right L3Protocol when the packet
       
   126    *        is received.
       
   127    * 
       
   128    *  Called from higher layer to send packet into Network Device
       
   129    *  to the specified destination MacAddress
       
   130    * 
       
   131    * \return whether the Send operation succeeded 
       
   132    */
       
   133   bool Send(Packet& p, const MacAddress& dest, uint16_t protocolNumber);
       
   134 
       
   135  protected:
       
   136   /**
       
   137    * Enable broadcast support. This method should be
       
   138    * called by subclasses from their constructor
       
   139    */
       
   140   void EnableBroadcast (MacAddress broadcast);
       
   141   /**
       
   142    * Set m_isBroadcast flag to false
       
   143    */
       
   144   void DisableBroadcast (void);
       
   145   /**
       
   146    * Set m_isMulticast flag to true
       
   147    */
       
   148   void EnableMulticast (void);
       
   149   /**
       
   150    * Set m_isMulticast flag to false
       
   151    */
       
   152   void DisableMulticast (void);
       
   153   /**
       
   154    * Set m_isPointToPoint flag to true
       
   155    */
       
   156   void EnablePointToPoint (void);
       
   157   /**
       
   158    * Set m_isPointToPoint flag to false
       
   159    */
       
   160   void DisablePointToPoint (void);
       
   161   /**
       
   162    * When a subclass notices that the link status has changed to up,
       
   163    * it notifies its parent class by calling this method. This method
       
   164    * is responsible for notifying any LinkUp callbacks. 
       
   165    */
       
   166   void NotifyLinkUp (void);
       
   167   /**
       
   168    * When a subclass notices that the link status has changed to 
       
   169    * down, it notifies its parent class by calling this method.
       
   170    */
       
   171   void NotifyLinkDown (void);
       
   172   /**
       
   173    * \returns the node base class which contains this network
       
   174    *          interface.
       
   175    *
       
   176    * When a subclass needs to get access to the underlying node
       
   177    * base class to print the nodeid for example, it can invoke
       
   178    * this method.
       
   179    */
       
   180   Node& GetNode (void) const;
       
   181 
       
   182   /**
       
   183    * \param p packet sent from below up to Network Device
       
   184    * \param from source mac address of the sender 
       
   185    * \returns true if the packet was forwarded successfully,
       
   186    *          false otherwise.
       
   187    *
       
   188    * When a subclass gets a packet from the PHY layer, it 
       
   189    * forwards it to the higher layers by calling this method
       
   190    * which is responsible for passing it up to the Rx callback.
       
   191    */
       
   192   bool ForwardUp (Packet& p);
       
   193 
       
   194  private:
       
   195   /**
       
   196    * \param p packet to send
       
   197    * \param dest address of destination to which packet must be sent
       
   198    * \returns true if the packet could be sent successfully, false
       
   199    *          otherwise.
       
   200    *
       
   201    * This is the private virtual target function of the public Send()
       
   202    * method.  When the link is Up, this method is invoked to ask 
       
   203    * subclasses to forward packets down to the PHY layer. Subclasses 
       
   204    * MUST override this method.
       
   205    */
       
   206   virtual bool SendTo (Packet& p, const MacAddress& dest) = 0;
       
   207   Node&         m_node;
       
   208   std::string   m_name;
       
   209   uint16_t      m_ifIndex;
       
   210   MacAddress    m_address;
       
   211   MacAddress    m_broadcast;
       
   212   uint16_t      m_mtu;
       
   213   bool          m_isUp;
       
   214   bool          m_isBroadcast;
       
   215   bool          m_isMulticast;
       
   216   bool          m_isPointToPoint;
       
   217   Callback<void> m_linkChangeCallback;
       
   218 };
       
   219 
       
   220 }; // namespace ns3
       
   221 #endif