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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
7 */
8
9#ifndef VIRTUAL_NET_DEVICE_H
10#define VIRTUAL_NET_DEVICE_H
11
12#include "ns3/address.h"
13#include "ns3/callback.h"
14#include "ns3/net-device.h"
15#include "ns3/node.h"
16#include "ns3/packet.h"
17#include "ns3/ptr.h"
18#include "ns3/traced-callback.h"
19
20namespace ns3
21{
22
23/**
24 * @defgroup virtual-net-device Virtual Device
25 *
26 */
27
28/**
29 * @ingroup virtual-net-device
30 *
31 * @class VirtualNetDevice
32 * @brief A virtual device, similar to Linux TUN/TAP interfaces.
33 *
34 * A VirtualNetDevice is a "virtual" NetDevice implementation which
35 * delegates to a user callback (see method SetSendCallback()) the
36 * task of actually transmitting a packet. It also allows the user
37 * code to inject the packet as if it had been received by the
38 * VirtualNetDevice. Together, these features allow one to build tunnels.
39 * For instance, by transmitting packets into a UDP socket we end up
40 * building an IP-over-UDP-over-IP tunnel, or IP-over-IP tunnels.
41 *
42 * The same thing could be accomplished by subclassing NetDevice
43 * directly. However, VirtualNetDevice is usually much simpler to program
44 * than a NetDevice subclass.
45 */
47{
48 public:
49 /**
50 * Callback the be invoked when the VirtualNetDevice is asked to queue/transmit a packet.
51 * For more information, consult the documentation of NetDevice::SendFrom().
52 */
53 typedef Callback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> SendCallback;
54
55 /**
56 * @brief Get the type ID.
57 * @return The object TypeId.
58 */
59 static TypeId GetTypeId();
61
62 ~VirtualNetDevice() override;
63
64 /**
65 * @brief Set the user callback to be called when a L2 packet is to be transmitted
66 * @param transmitCb the new transmit callback
67 */
68 void SetSendCallback(SendCallback transmitCb);
69
70 /**
71 * @brief Configure whether the virtual device needs ARP
72 *
73 * @param needsArp the the 'needs arp' value that will be returned
74 * by the NeedsArp() method. The method IsBroadcast() will also
75 * return this value.
76 */
77 void SetNeedsArp(bool needsArp);
78
79 /**
80 * @brief Configure whether the virtual device is point-to-point
81 *
82 * @param isPointToPoint the value that should be returned by the
83 * IsPointToPoint method for this instance.
84 */
85 void SetIsPointToPoint(bool isPointToPoint);
86
87 /**
88 * @brief Configure whether the virtual device supports SendFrom
89 * @param supportsSendFrom true if the device supports SendFrom
90 */
91 void SetSupportsSendFrom(bool supportsSendFrom);
92
93 /**
94 * @brief Configure the reported MTU for the virtual device.
95 * @param mtu MTU value to set
96 * @return whether the MTU value was within legal bounds
97 */
98 bool SetMtu(const uint16_t mtu) override;
99
100 /**
101 * @param packet packet sent from below up to Network Device
102 * @param protocol Protocol type
103 * @param source the address of the sender of this packet.
104 * @param destination the address of the receiver of this packet.
105 * @param packetType type of packet received (broadcast/multicast/unicast/otherhost)
106 * @returns true if the packet was forwarded successfully, false otherwise.
107 *
108 * Forward a "virtually received" packet up
109 * the node's protocol stack.
110 */
111 bool Receive(Ptr<Packet> packet,
112 uint16_t protocol,
113 const Address& source,
114 const Address& destination,
115 PacketType packetType);
116
117 // inherited from NetDevice base class.
118 void SetIfIndex(const uint32_t index) override;
119 uint32_t GetIfIndex() const override;
120 Ptr<Channel> GetChannel() const override;
121 void SetAddress(Address address) override;
122 Address GetAddress() const override;
123 uint16_t GetMtu() const override;
124 bool IsLinkUp() const override;
125 void AddLinkChangeCallback(Callback<void> callback) override;
126 bool IsBroadcast() const override;
127 Address GetBroadcast() const override;
128 bool IsMulticast() const override;
129 Address GetMulticast(Ipv4Address multicastGroup) const override;
130 Address GetMulticast(Ipv6Address addr) const override;
131 bool IsPointToPoint() const override;
132 bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) override;
133 bool SendFrom(Ptr<Packet> packet,
134 const Address& source,
135 const Address& dest,
136 uint16_t protocolNumber) override;
137 Ptr<Node> GetNode() const override;
138 void SetNode(Ptr<Node> node) override;
139 bool NeedsArp() const override;
142 bool SupportsSendFrom() const override;
143 bool IsBridge() const override;
144
145 protected:
146 void DoDispose() override;
147
148 private:
149 Address m_myAddress; //!< MAC address
150 SendCallback m_sendCb; //!< send callback
156 Ptr<Node> m_node; //!< Pointer to the node
159 std::string m_name; //!< Name of the device
160 uint32_t m_index; //!< Device index
161 uint16_t m_mtu; //!< MTU
162 bool m_needsArp; //!< True if the device needs ARP
163 bool m_supportsSendFrom; //!< True if the device supports SendFrm
164 bool m_isPointToPoint; //!< True if the device is a PointToPoint type device
165};
166
167} // namespace ns3
168
169#endif
a polymophic address class
Definition address.h:90
Callback template class.
Definition callback.h:422
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 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.
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:48
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.