A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
bridge-net-device.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Gustavo Carneiro <gjc@inescporto.pt>
5 */
6#ifndef BRIDGE_NET_DEVICE_H
7#define BRIDGE_NET_DEVICE_H
8
9#include "bridge-channel.h"
10
11#include "ns3/mac48-address.h"
12#include "ns3/net-device.h"
13#include "ns3/nstime.h"
14
15#include <map>
16#include <stdint.h>
17#include <string>
18
19/**
20 * \file
21 * \ingroup bridge
22 * ns3::BridgeNetDevice declaration.
23 */
24
25namespace ns3
26{
27
28class Node;
29
30/**
31 * \defgroup bridge Bridge Network Device
32 *
33 * \brief a virtual net device that bridges multiple LAN segments
34 *
35 * The BridgeNetDevice object is a "virtual" netdevice that aggregates
36 * multiple "real" netdevices and implements the data plane forwarding
37 * part of IEEE 802.1D. By adding a BridgeNetDevice to a Node, it
38 * will act as a "bridge", or "switch", to multiple LAN segments.
39 *
40 * By default the bridge netdevice implements a "learning bridge"
41 * algorithm (see 802.1D), where incoming unicast frames from one port
42 * may occasionally be forwarded throughout all other ports, but
43 * usually they are forwarded only to a single correct output port.
44 *
45 * \attention The Spanning Tree Protocol part of 802.1D is not
46 * implemented. Therefore, you have to be careful not to create
47 * bridging loops, or else the network will collapse.
48 *
49 * \attention Bridging is designed to work only with NetDevices
50 * modelling IEEE 802-style technologies, such as CsmaNetDevice and
51 * WifiNetDevice.
52 *
53 * \attention If including a WifiNetDevice in a bridge, the wifi
54 * device must be in Access Point mode. Adhoc mode is not supported
55 * with bridging.
56 */
57
58/**
59 * \ingroup bridge
60 * \brief a virtual net device that bridges multiple LAN segments
61 */
63{
64 public:
65 /**
66 * \brief Get the type ID.
67 * \return the object TypeId
68 */
69 static TypeId GetTypeId();
71 ~BridgeNetDevice() override;
72
73 // Delete copy constructor and assignment operator to avoid misuse
76
77 /**
78 * \brief Add a 'port' to a bridge device
79 * \param bridgePort the NetDevice to add
80 *
81 * This method adds a new bridge port to a BridgeNetDevice, so that
82 * the new bridge port NetDevice becomes part of the bridge and L2
83 * frames start being forwarded to/from this NetDevice.
84 *
85 * \attention The netdevice that is being added as bridge port must
86 * _not_ have an IP address. In order to add IP connectivity to a
87 * bridging node you must enable IP on the BridgeNetDevice itself,
88 * never on its port netdevices.
89 */
90 void AddBridgePort(Ptr<NetDevice> bridgePort);
91
92 /**
93 * \brief Gets the number of bridged 'ports', i.e., the NetDevices currently bridged.
94 *
95 * \return the number of bridged ports.
96 */
98
99 /**
100 * \brief Gets the n-th bridged port.
101 * \param n the port index
102 * \return the n-th bridged NetDevice
103 */
105
106 // inherited from NetDevice base class.
107 void SetIfIndex(const uint32_t index) override;
108 uint32_t GetIfIndex() const override;
109 Ptr<Channel> GetChannel() const override;
110 void SetAddress(Address address) override;
111 Address GetAddress() const override;
112 bool SetMtu(const uint16_t mtu) override;
113 uint16_t GetMtu() const override;
114 bool IsLinkUp() const override;
115 void AddLinkChangeCallback(Callback<void> callback) override;
116 bool IsBroadcast() const override;
117 Address GetBroadcast() const override;
118 bool IsMulticast() const override;
119 Address GetMulticast(Ipv4Address multicastGroup) const override;
120 bool IsPointToPoint() const override;
121 bool IsBridge() const override;
122 bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) override;
123 bool SendFrom(Ptr<Packet> packet,
124 const Address& source,
125 const Address& dest,
126 uint16_t protocolNumber) override;
127 Ptr<Node> GetNode() const override;
128 void SetNode(Ptr<Node> node) override;
129 bool NeedsArp() const override;
132 bool SupportsSendFrom() const override;
133 Address GetMulticast(Ipv6Address addr) const override;
134
135 protected:
136 void DoDispose() override;
137
138 /**
139 * \brief Receives a packet from one bridged port.
140 * \param device the originating port
141 * \param packet the received packet
142 * \param protocol the packet protocol (e.g., Ethertype)
143 * \param source the packet source
144 * \param destination the packet destination
145 * \param packetType the packet type (e.g., host, broadcast, etc.)
146 */
148 Ptr<const Packet> packet,
149 uint16_t protocol,
150 const Address& source,
151 const Address& destination,
152 PacketType packetType);
153
154 /**
155 * \brief Forwards a unicast packet
156 * \param incomingPort the packet incoming port
157 * \param packet the packet
158 * \param protocol the packet protocol (e.g., Ethertype)
159 * \param src the packet source
160 * \param dst the packet destination
161 */
162 void ForwardUnicast(Ptr<NetDevice> incomingPort,
163 Ptr<const Packet> packet,
164 uint16_t protocol,
165 Mac48Address src,
166 Mac48Address dst);
167
168 /**
169 * \brief Forwards a broadcast or a multicast packet
170 * \param incomingPort the packet incoming port
171 * \param packet the packet
172 * \param protocol the packet protocol (e.g., Ethertype)
173 * \param src the packet source
174 * \param dst the packet destination
175 */
176 void ForwardBroadcast(Ptr<NetDevice> incomingPort,
177 Ptr<const Packet> packet,
178 uint16_t protocol,
179 Mac48Address src,
180 Mac48Address dst);
181
182 /**
183 * \brief Learns the port a MAC address is sending from
184 * \param source source address
185 * \param port the port the source is sending from
186 */
188
189 /**
190 * \brief Gets the port associated to a source address
191 * \param source the source address
192 * \returns the port the source is associated to, or NULL if no association is known.
193 */
195
196 private:
199
200 Mac48Address m_address; //!< MAC address of the NetDevice
201 Time m_expirationTime; //!< time it takes for learned MAC state to expire
202
203 /**
204 * \ingroup bridge
205 * Structure holding the status of an address
206 */
208 {
209 Ptr<NetDevice> associatedPort; //!< port associated with the address
210 Time expirationTime; //!< time it takes for learned MAC state to expire
211 };
212
213 std::map<Mac48Address, LearnedState> m_learnState; //!< Container for known address statuses
214 Ptr<Node> m_node; //!< node owning this NetDevice
215 Ptr<BridgeChannel> m_channel; //!< virtual bridged channel
216 std::vector<Ptr<NetDevice>> m_ports; //!< bridged ports
217 uint32_t m_ifIndex; //!< Interface index
218 uint16_t m_mtu; //!< MTU of the bridged NetDevice
219 bool m_enableLearning; //!< true if the bridge will learn the node status
220};
221
222} // namespace ns3
223
224#endif /* BRIDGE_NET_DEVICE_H */
ns3::BridgeChannel declaration.
a polymophic address class
Definition address.h:90
a virtual net device that bridges multiple LAN segments
bool IsBroadcast() const override
Ptr< BridgeChannel > m_channel
virtual bridged channel
void ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
Receives a packet from one bridged port.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
Ptr< Node > m_node
node owning this NetDevice
std::map< Mac48Address, LearnedState > m_learnState
Container for known address statuses.
Address GetBroadcast() const override
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
uint32_t m_ifIndex
Interface index.
bool SupportsSendFrom() const override
BridgeNetDevice(const BridgeNetDevice &)=delete
Mac48Address m_address
MAC address of the NetDevice.
uint16_t GetMtu() const override
bool NeedsArp() const override
NetDevice::ReceiveCallback m_rxCallback
receive callback
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
static TypeId GetTypeId()
Get the type ID.
std::vector< Ptr< NetDevice > > m_ports
bridged ports
Time m_expirationTime
time it takes for learned MAC state to expire
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
bool IsMulticast() const override
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
void DoDispose() override
Destructor implementation.
uint32_t GetIfIndex() const override
void ForwardUnicast(Ptr< NetDevice > incomingPort, Ptr< const Packet > packet, uint16_t protocol, Mac48Address src, Mac48Address dst)
Forwards a unicast packet.
Ptr< NetDevice > GetLearnedState(Mac48Address source)
Gets the port associated to a source address.
bool m_enableLearning
true if the bridge will learn the node status
void AddBridgePort(Ptr< NetDevice > bridgePort)
Add a 'port' to a bridge device.
void Learn(Mac48Address source, Ptr< NetDevice > port)
Learns the port a MAC address is sending from.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
void SetNode(Ptr< Node > node) override
bool SetMtu(const uint16_t mtu) override
uint16_t m_mtu
MTU of the bridged NetDevice.
Ptr< Channel > GetChannel() const override
BridgeNetDevice & operator=(const BridgeNetDevice &)=delete
Address GetAddress() const override
Ptr< NetDevice > GetBridgePort(uint32_t n) const
Gets the n-th bridged port.
void SetIfIndex(const uint32_t index) override
void AddLinkChangeCallback(Callback< void > callback) override
Ptr< Node > GetNode() const override
bool IsLinkUp() const override
NetDevice::PromiscReceiveCallback m_promiscRxCallback
promiscuous receive callback
void SetAddress(Address address) override
Set the address of this interface.
uint32_t GetNBridgePorts() const
Gets the number of bridged 'ports', i.e., the NetDevices currently bridged.
void ForwardBroadcast(Ptr< NetDevice > incomingPort, Ptr< const Packet > packet, uint16_t protocol, Mac48Address src, Mac48Address dst)
Forwards a broadcast or a multicast packet.
void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb) override
Callback template class.
Definition callback.h:422
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 address.
an EUI-48 address
Network layer to device interface.
Definition net-device.h:87
PacketType
Packet types are used as they are in Linux.
Definition net-device.h:289
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Structure holding the status of an address.
Time expirationTime
time it takes for learned MAC state to expire
Ptr< NetDevice > associatedPort
port associated with the address