src/devices/bridge/bridge-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  * This program is free software; you can redistribute it and/or modify
     4  * it under the terms of the GNU General Public License version 2 as
     5  * published by the Free Software Foundation;
     6  *
     7  * This program is distributed in the hope that it will be useful,
     8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  * GNU General Public License for more details.
    11  *
    12  * You should have received a copy of the GNU General Public License
    13  * along with this program; if not, write to the Free Software
    14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    15  *
    16  * Author: Gustavo Carneiro  <gjc@inescporto.pt>
    17  */
    18 #ifndef BRIDGE_NET_DEVICE_H
    19 #define BRIDGE_NET_DEVICE_H
    20 
    21 #include "ns3/net-device.h"
    22 #include "ns3/mac48-address.h"
    23 #include "ns3/nstime.h"
    24 #include "ns3/bridge-channel.h"
    25 #include <stdint.h>
    26 #include <string>
    27 #include <map>
    28 
    29 namespace ns3 {
    30 
    31 class Node;
    32 
    33 /**
    34  * \ingroup devices
    35  * \defgroup bridge Bridge
    36  * 
    37  * \brief a virtual net device that bridges multiple LAN segments
    38  *
    39  * The BridgeNetDevice object is a "virtual" netdevice that aggregates
    40  * multiple "real" netdevices and implements the data plane forwarding
    41  * part of IEEE 802.1D.  By adding a BridgeNetDevice to a Node, it
    42  * will act as a "bridge", or "switch", to multiple LAN segments.
    43  * 
    44  * By default the bridge netdevice implements a "learning bridge"
    45  * algorithm (see 802.1D), where incoming unicast frames from one port
    46  * may occasionally be forwarded throughout all other ports, but
    47  * usually they are forwarded only to a single correct output port.
    48  *
    49  * \attention The Spanning Tree Protocol part of 802.1D is not
    50  * implemented.  Therefore, you have to be careful not to create
    51  * bridging loops, or else the network will collapse.
    52  *
    53  * \attention Bridging is designed to work only with NetDevices
    54  * modelling IEEE 802-style technologies, such as CsmaNetDevice and
    55  * WifiNetDevice.
    56  *
    57  * \attention If including a WifiNetDevice in a bridge, the wifi
    58  * device must be in Access Point mode.  Adhoc mode is not supported
    59  * with bridging.
    60  */
    61 
    62 /**
    63  * \ingroup bridge
    64  * \brief a virtual net device that bridges multiple LAN segments
    65  */
    66 class BridgeNetDevice : public NetDevice
    67 {
    68 public:
    69   static TypeId GetTypeId (void);
    70   BridgeNetDevice ();
    71 
    72   /** \brief Add a 'port' to a bridge device
    73    *
    74    * This method adds a new bridge port to a BridgeNetDevice, so that
    75    * the new bridge port NetDevice becomes part of the bridge and L2
    76    * frames start being forwarded to/from this NetDevice.
    77    *
    78    * \attention The netdevice that is being added as bridge port must
    79    * _not_ have an IP address.  In order to add IP connectivity to a
    80    * bridging node you must enable IP on the BridgeNetDevice itself,
    81    * never on its port netdevices.
    82    */
    83   void AddBridgePort (Ptr<NetDevice> bridgePort);
    84 
    85   // inherited from NetDevice base class.
    86   virtual void SetName(const std::string name);
    87   virtual std::string GetName(void) const;
    88   virtual void SetIfIndex(const uint32_t index);
    89   virtual uint32_t GetIfIndex(void) const;
    90   virtual Ptr<Channel> GetChannel (void) const;
    91   virtual Address GetAddress (void) const;
    92   virtual bool SetMtu (const uint16_t mtu);
    93   virtual uint16_t GetMtu (void) const;
    94   virtual bool IsLinkUp (void) const;
    95   virtual void SetLinkChangeCallback (Callback<void> callback);
    96   virtual bool IsBroadcast (void) const;
    97   virtual Address GetBroadcast (void) const;
    98   virtual bool IsMulticast (void) const;
    99   virtual Address GetMulticast (Ipv4Address multicastGroup) const;
   100   virtual bool IsPointToPoint (void) const;
   101   virtual bool Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
   102   virtual bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
   103   virtual Ptr<Node> GetNode (void) const;
   104   virtual void SetNode (Ptr<Node> node);
   105   virtual bool NeedsArp (void) const;
   106   virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
   107   virtual void SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb);
   108   virtual bool SupportsSendFrom () const;
   109   virtual Address GetMulticast (Ipv6Address addr) const;
   110 
   111 protected:
   112   virtual void DoDispose (void);
   113 
   114   void ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
   115                           Address const &source, Address const &destination, PacketType packetType);
   116   void ForwardUnicast (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet,
   117                        uint16_t protocol, Mac48Address src, Mac48Address dst);
   118   void ForwardBroadcast (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet,
   119                          uint16_t protocol, Mac48Address src, Mac48Address dst);
   120   void Learn (Mac48Address source, Ptr<NetDevice> port);
   121   Ptr<NetDevice> GetLearnedState (Mac48Address source);
   122 
   123 private:
   124   NetDevice::ReceiveCallback m_rxCallback;
   125   NetDevice::PromiscReceiveCallback m_promiscRxCallback;
   126 
   127   Mac48Address m_address;
   128   Time m_expirationTime; // time it takes for learned MAC state to expire
   129   struct LearnedState
   130   {
   131     Ptr<NetDevice> associatedPort;
   132     Time expirationTime;
   133   };
   134   std::map<Mac48Address, LearnedState> m_learnState;
   135   Ptr<Node> m_node;
   136   Ptr<BridgeChannel> m_channel;
   137   std::string m_name;
   138   std::vector< Ptr<NetDevice> > m_ports;
   139   uint32_t m_ifIndex;
   140   uint16_t m_mtu;
   141   bool m_enableLearning;
   142 };
   143 
   144 } // namespace ns3
   145 
   146 #endif /* BRIDGE_NET_DEVICE_H */