A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
virtual-net-device.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008,2009 INESC Porto
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
18 */
19
20#ifndef VIRTUAL_NET_DEVICE_H
21#define VIRTUAL_NET_DEVICE_H
22
23#include "ns3/address.h"
24#include "ns3/callback.h"
25#include "ns3/net-device.h"
26#include "ns3/node.h"
27#include "ns3/packet.h"
28#include "ns3/ptr.h"
29#include "ns3/traced-callback.h"
30
31namespace ns3
32{
33
34/**
35 * \defgroup virtual-net-device Virtual Device
36 *
37 */
38
39/**
40 * \ingroup virtual-net-device
41 *
42 * \class VirtualNetDevice
43 * \brief A virtual device, similar to Linux TUN/TAP interfaces.
44 *
45 * A VirtualNetDevice is a "virtual" NetDevice implementation which
46 * delegates to a user callback (see method SetSendCallback()) the
47 * task of actually transmitting a packet. It also allows the user
48 * code to inject the packet as if it had been received by the
49 * VirtualNetDevice. Together, these features allow one to build tunnels.
50 * For instance, by transmitting packets into a UDP socket we end up
51 * building an IP-over-UDP-over-IP tunnel, or IP-over-IP tunnels.
52 *
53 * The same thing could be accomplished by subclassing NetDevice
54 * directly. However, VirtualNetDevice is usually much simpler to program
55 * than a NetDevice subclass.
56 */
58{
59 public:
60 /**
61 * Callback the be invoked when the VirtualNetDevice is asked to queue/transmit a packet.
62 * For more information, consult the documentation of NetDevice::SendFrom().
63 */
64 typedef Callback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> SendCallback;
65
66 /**
67 * \brief Get the type ID.
68 * \return The object TypeId.
69 */
70 static TypeId GetTypeId();
72
73 ~VirtualNetDevice() override;
74
75 /**
76 * \brief Set the user callback to be called when a L2 packet is to be transmitted
77 * \param transmitCb the new transmit callback
78 */
79 void SetSendCallback(SendCallback transmitCb);
80
81 /**
82 * \brief Configure whether the virtual device needs ARP
83 *
84 * \param needsArp the the 'needs arp' value that will be returned
85 * by the NeedsArp() method. The method IsBroadcast() will also
86 * return this value.
87 */
88 void SetNeedsArp(bool needsArp);
89
90 /**
91 * \brief Configure whether the virtual device is point-to-point
92 *
93 * \param isPointToPoint the value that should be returned by the
94 * IsPointToPoint method for this instance.
95 */
96 void SetIsPointToPoint(bool isPointToPoint);
97
98 /**
99 * \brief Configure whether the virtual device supports SendFrom
100 * \param supportsSendFrom true if the device supports SendFrom
101 */
102 void SetSupportsSendFrom(bool supportsSendFrom);
103
104 /**
105 * \brief Configure the reported MTU for the virtual device.
106 * \param mtu MTU value to set
107 * \return whether the MTU value was within legal bounds
108 */
109 bool SetMtu(const uint16_t mtu) override;
110
111 /**
112 * \param packet packet sent from below up to Network Device
113 * \param protocol Protocol type
114 * \param source the address of the sender of this packet.
115 * \param destination the address of the receiver of this packet.
116 * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
117 * \returns true if the packet was forwarded successfully, false otherwise.
118 *
119 * Forward a "virtually received" packet up
120 * the node's protocol stack.
121 */
122 bool Receive(Ptr<Packet> packet,
123 uint16_t protocol,
124 const Address& source,
125 const Address& destination,
126 PacketType packetType);
127
128 // inherited from NetDevice base class.
129 void SetIfIndex(const uint32_t index) override;
130 uint32_t GetIfIndex() const override;
131 Ptr<Channel> GetChannel() const override;
132 void SetAddress(Address address) override;
133 Address GetAddress() const override;
134 uint16_t GetMtu() const override;
135 bool IsLinkUp() const override;
136 void AddLinkChangeCallback(Callback<void> callback) override;
137 bool IsBroadcast() const override;
138 Address GetBroadcast() const override;
139 bool IsMulticast() const override;
140 Address GetMulticast(Ipv4Address multicastGroup) const override;
141 Address GetMulticast(Ipv6Address addr) const override;
142 bool IsPointToPoint() const override;
143 bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) override;
144 bool SendFrom(Ptr<Packet> packet,
145 const Address& source,
146 const Address& dest,
147 uint16_t protocolNumber) override;
148 Ptr<Node> GetNode() const override;
149 void SetNode(Ptr<Node> node) override;
150 bool NeedsArp() const override;
153 bool SupportsSendFrom() const override;
154 bool IsBridge() const override;
155
156 protected:
157 void DoDispose() override;
158
159 private:
160 Address m_myAddress; //!< MAC address
161 SendCallback m_sendCb; //!< send callback
167 Ptr<Node> m_node; //!< Pointer to the node
170 std::string m_name; //!< Name of the device
171 uint32_t m_index; //!< Device index
172 uint16_t m_mtu; //!< MTU
173 bool m_needsArp; //!< True if the device needs ARP
174 bool m_supportsSendFrom; //!< True if the device supports SendFrm
175 bool m_isPointToPoint; //!< True if the device is a PointToPoint type device
176};
177
178} // namespace ns3
179
180#endif
a polymophic address class
Definition: address.h:101
Callback template class.
Definition: callback.h:438
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
A virtual device, similar to Linux TUN/TAP interfaces.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
static TypeId GetTypeId()
Get the type ID.
Ptr< Node > m_node
Pointer to the node.
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
Promisc Sniffer trace.
uint32_t GetIfIndex() const override
bool SetMtu(const uint16_t mtu) override
Configure the reported MTU for the virtual device.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
Address GetAddress() const override
uint32_t m_index
Device index.
bool SupportsSendFrom() const override
bool IsMulticast() const override
bool IsLinkUp() const override
void SetAddress(Address address) override
Set the address of this interface.
void AddLinkChangeCallback(Callback< void > callback) override
TracedCallback< Ptr< const Packet > > m_macRxTrace
Rx trace.
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
void SetSupportsSendFrom(bool supportsSendFrom)
Configure whether the virtual device supports SendFrom.
void SetNode(Ptr< Node > node) override
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
TracedCallback< Ptr< const Packet > > m_snifferTrace
Sniffer trace.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
void SetNeedsArp(bool needsArp)
Configure whether the virtual device needs ARP.
bool NeedsArp() const override
void DoDispose() override
Destructor implementation.
Ptr< Channel > GetChannel() const override
bool m_needsArp
True if the device needs ARP.
bool IsBroadcast() const override
void SetIfIndex(const uint32_t index) override
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
Promisc Rx trace.
bool m_supportsSendFrom
True if the device supports SendFrm.
void SetIsPointToPoint(bool isPointToPoint)
Configure whether the virtual device is point-to-point.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Ptr< Node > GetNode() const override
Address GetBroadcast() const override
Address m_myAddress
MAC address.
uint16_t GetMtu() const override
PromiscReceiveCallback m_promiscRxCallback
Promisc Rx callback.
std::string m_name
Name of the device.
ReceiveCallback m_rxCallback
Rx callback.
void SetSendCallback(SendCallback transmitCb)
Set the user callback to be called when a L2 packet is to be transmitted.
Callback< bool, Ptr< Packet >, const Address &, const Address &, uint16_t > SendCallback
Callback the be invoked when the VirtualNetDevice is asked to queue/transmit a packet.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
bool m_isPointToPoint
True if the device is a PointToPoint type device.
SendCallback m_sendCb
send callback
TracedCallback< Ptr< const Packet > > m_macTxTrace
Tx trace.
void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb) override
Every class exported by the ns3 library is enclosed in the ns3 namespace.