src/node/net-device.h
author vincent@clarinet.u-strasbg.fr
Fri Nov 07 11:36:15 2008 -0800 (2008-11-07)
changeset 3852 9cf7ad0cac85
parent 3841 1e7abf5fca79
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) 2005,2006 INRIA
     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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  * Modified by Emmanuelle Laprise to remove dependance on LLC headers
    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/object.h"
    28 #include "ns3/ptr.h"
    29 #include "address.h"
    30 #include "ipv4-address.h"
    31 #include "ipv6-address.h"
    32 
    33 namespace ns3 {
    34 
    35 class Node;
    36 class Channel;
    37 class Packet;
    38 
    39 /**
    40  * \ingroup node
    41  * \defgroup netdevice NetDevice
    42  */
    43 /**
    44  * \ingroup netdevice
    45  *
    46  * \brief Network layer to device interface
    47  *
    48  * This interface defines the API which the IP and ARP
    49  * layers need to access to manage an instance of a network device 
    50  * layer. It currently does not support MAC-level 
    51  * multicast but this should not be too hard to add by adding
    52  * extra methods to register MAC multicast addresses to
    53  * filter out unwanted packets before handing them to the
    54  * higher layers.
    55  *
    56  * In Linux, this interface is analogous to the interface
    57  * just above dev_queue_xmit() (i.e., IP packet is fully
    58  * constructed with destination MAC address already selected).
    59  * 
    60  * If you want to write a new MAC layer, you need to subclass
    61  * this base class and implement your own version of the
    62  * NetDevice::SendTo method.
    63  */
    64 class NetDevice : public Object
    65 {
    66 public:
    67   static TypeId GetTypeId (void);
    68   virtual ~NetDevice();
    69 
    70   /**
    71    * \param name name of the device (e.g. "eth0")
    72    */
    73   virtual void SetName(const std::string name) = 0;
    74   /**
    75    * \return name name of the device (e.g. "eth0")
    76    */
    77   virtual std::string GetName(void) const = 0;
    78   /**
    79    * \param index ifIndex of the device 
    80    */
    81   virtual void SetIfIndex(const uint32_t index) = 0;
    82   /**
    83    * \return index ifIndex of the device 
    84    */
    85   virtual uint32_t GetIfIndex(void) const = 0;
    86 
    87 
    88   /**
    89    * \return the channel this NetDevice is connected to. The value
    90    *         returned can be zero if the NetDevice is not yet connected
    91    *         to any channel.
    92    */
    93   virtual Ptr<Channel> GetChannel (void) const = 0;
    94 
    95   /**
    96    * \return the current Address of this interface.
    97    */
    98   virtual Address GetAddress (void) const = 0;
    99   /**
   100    * \param mtu MTU value, in bytes, to set for the device
   101    * \return whether the MTU value was within legal bounds
   102    * 
   103    * Override for default MTU defined on a per-type basis.
   104    */
   105   virtual bool SetMtu (const uint16_t mtu) = 0;
   106   /**
   107    * \return the link-level MTU in bytes for this interface.
   108    * 
   109    * This value is typically used by the IP layer to perform
   110    * IP fragmentation when needed.
   111    */
   112   virtual uint16_t GetMtu (void) const = 0;
   113   /**
   114    * \return true if link is up; false otherwise
   115    */
   116   virtual bool IsLinkUp (void) const = 0;
   117   /**
   118    * \param callback the callback to invoke
   119    *
   120    * Register a callback invoked whenever the link 
   121    * status changes to UP. This callback is typically used
   122    * by the IP/ARP layer to flush the ARP cache 
   123    * whenever the link goes up.
   124    */
   125   virtual void SetLinkChangeCallback (Callback<void> callback) = 0;
   126   /**
   127    * \return true if this interface supports a broadcast address,
   128    *         false otherwise.
   129    */
   130   virtual bool IsBroadcast (void) const = 0;
   131   /**
   132    * \return the broadcast address supported by
   133    *         this netdevice.
   134    *
   135    * Calling this method is invalid if IsBroadcast returns
   136    * not true.
   137    */
   138   virtual Address GetBroadcast (void) const = 0;
   139 
   140   /**
   141    * \return value of m_isMulticast flag
   142    */
   143   virtual bool IsMulticast (void) const = 0;
   144 
   145   /**
   146    * \brief Make and return a MAC multicast address using the provided
   147    *        multicast group
   148    *
   149    * RFC 1112 says that an Ipv4 host group address is mapped to an Ethernet 
   150    * multicast address by placing the low-order 23-bits of the IP address into 
   151    * the low-order 23 bits of the Ethernet multicast address 
   152    * 01-00-5E-00-00-00 (hex).  Similar RFCs exist for Ipv6 and Eui64 mappings.
   153    * This method performs the multicast address creation function appropriate
   154    * to the underlying MAC address of the device.  This MAC address is
   155    * encapsulated in an abstract Address to avoid dependencies on the exact
   156    * MAC address format.
   157    *
   158    * A default imlementation of GetMulticast is provided, but this
   159    * method simply NS_ASSERTS.  In the case of net devices that do not support
   160    * multicast, clients are expected to test NetDevice::IsMulticast and avoid
   161    * attempting to map multicast packets.  Subclasses of NetDevice that do
   162    * support multicasting are expected to override this method and provide an
   163    * implementation appropriate to the particular device.
   164    *
   165    * \param multicastGroup The IP address for the multicast group destination
   166    * of the packet.
   167    * \return The MAC multicast Address used to send packets to the provided
   168    * multicast group.
   169    *
   170    * \warning Calling this method is invalid if IsMulticast returns not true.
   171    * \see Ipv4Address
   172    * \see Address
   173    * \see NetDevice::IsMulticast
   174    */
   175   virtual Address GetMulticast (Ipv4Address multicastGroup) const = 0;
   176   
   177 	/**
   178    * \brief Get the MAC multicast address corresponding
   179    * to the IPv6 address provided.
   180    * \param addr IPv6 address
   181    * \return the MAC multicast address
   182    * \warning Calling this method is invalid if IsMulticast returns not true.
   183    */
   184   virtual Address GetMulticast (Ipv6Address addr) const = 0;
   185 
   186   /**
   187    * \return value of m_isPointToPoint flag
   188    */
   189   virtual bool IsPointToPoint (void) const = 0;
   190   /**
   191    * \param packet packet sent from above down to Network Device
   192    * \param dest mac address of the destination (already resolved)
   193    * \param protocolNumber identifies the type of payload contained in
   194    *        this packet. Used to call the right L3Protocol when the packet
   195    *        is received.
   196    * 
   197    *  Called from higher layer to send packet into Network Device
   198    *  to the specified destination Address
   199    * 
   200    * \return whether the Send operation succeeded 
   201    */
   202   virtual bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) = 0;
   203   /**
   204    * \param packet packet sent from above down to Network Device
   205    * \param source source mac address (so called "MAC spoofing")
   206    * \param dest mac address of the destination (already resolved)
   207    * \param protocolNumber identifies the type of payload contained in
   208    *        this packet. Used to call the right L3Protocol when the packet
   209    *        is received.
   210    * 
   211    *  Called from higher layer to send packet into Network Device
   212    *  with the specified source and destination Addresses.
   213    * 
   214    * \return whether the Send operation succeeded 
   215    */
   216   virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber) = 0;
   217   /**
   218    * \returns the node base class which contains this network
   219    *          interface.
   220    *
   221    * When a subclass needs to get access to the underlying node
   222    * base class to print the nodeid for example, it can invoke
   223    * this method.
   224    */
   225   virtual Ptr<Node> GetNode (void) const = 0;
   226 
   227   /**
   228    * \param node the node associated to this netdevice.
   229    *
   230    * This method is called from ns3::Node::AddDevice.
   231    */
   232   virtual void SetNode (Ptr<Node> node) = 0;
   233 
   234   /**
   235    * \returns true if ARP is needed, false otherwise.
   236    *
   237    * Called by higher-layers to check if this NetDevice requires
   238    * ARP to be used.
   239    */
   240   virtual bool NeedsArp (void) const = 0;
   241 
   242 
   243   /** 
   244    * Packet types are used as they are in Linux.  GCC name resolution on 
   245    * typedef enum {} PacketType is broken for the foreseeable future, so
   246    * if you need to use ns-3 PacketType in a driver that also uses the 
   247    * Linux packet types you're hosed unless we define a shadow type, 
   248    * which we do here.
   249    */
   250   enum PacketType
   251     {
   252       PACKET_HOST = 1, /**< Packet addressed oo us */
   253       NS3_PACKET_HOST = PACKET_HOST,
   254       PACKET_BROADCAST, /**< Packet addressed to all */
   255       NS3_PACKET_BROADCAST = PACKET_BROADCAST,
   256       PACKET_MULTICAST, /**< Packet addressed to multicast group */
   257       NS3_PACKET_MULTICAST = PACKET_MULTICAST,
   258       PACKET_OTHERHOST, /**< Packet addressed to someone else */
   259       NS3_PACKET_OTHERHOST = PACKET_OTHERHOST,
   260     };
   261 
   262   /**
   263    * \param device a pointer to the net device which is calling this callback
   264    * \param packet the packet received
   265    * \param protocol the 16 bit protocol number associated with this packet.
   266    *        This protocol number is expected to be the same protocol number
   267    *        given to the Send method by the user on the sender side.
   268    * \param sender the address of the sender
   269    * \returns true if the callback could handle the packet successfully, false
   270    *          otherwise.
   271    */
   272   typedef Callback<bool,Ptr<NetDevice>,Ptr<const Packet>,uint16_t,const Address &> ReceiveCallback;
   273 
   274   /**
   275    * \param cb callback to invoke whenever a packet has been received and must
   276    *        be forwarded to the higher layers.
   277    *
   278    */
   279   virtual void SetReceiveCallback (ReceiveCallback cb) = 0;
   280 
   281 
   282   /**
   283    * \param device a pointer to the net device which is calling this callback
   284    * \param packet the packet received
   285    * \param protocol the 16 bit protocol number associated with this packet.
   286    *        This protocol number is expected to be the same protocol number
   287    *        given to the Send method by the user on the sender side.
   288    * \param sender the address of the sender
   289    * \param receiver the address of the receiver
   290    * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
   291    * \returns true if the callback could handle the packet successfully, false
   292    *          otherwise.
   293    */
   294   typedef Callback< bool, Ptr<NetDevice>, Ptr<const Packet>, uint16_t,
   295                     const Address &, const Address &, enum PacketType > PromiscReceiveCallback;
   296 
   297   /**
   298    * \param cb callback to invoke whenever a packet has been received in promiscuous mode and must
   299    *        be forwarded to the higher layers.
   300    * 
   301    * Enables netdevice promiscuous mode and sets the callback that
   302    * will handle promiscuous mode packets.  Note, promiscuous mode
   303    * packets means _all_ packets, including those packets that can be
   304    * sensed by the netdevice but which are intended to be received by
   305    * other hosts.
   306    */
   307   virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb) = 0;
   308 
   309   /**
   310    * \return true if this interface supports a bridging mode, false otherwise.
   311    */
   312   virtual bool SupportsSendFrom (void) const = 0;
   313 
   314 };
   315 
   316 } // namespace ns3
   317 
   318 #endif /* NET_DEVICE_H */