A Discrete-Event Network Simulator
API
sixlowpan-net-device.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  * Michele Muccio <michelemuccio@virgilio.it>
19  */
20 
21 #ifndef SIXLOWPAN_NET_DEVICE_H
22 #define SIXLOWPAN_NET_DEVICE_H
23 
24 #include "ns3/net-device.h"
25 #include "ns3/nstime.h"
26 #include "ns3/random-variable-stream.h"
27 #include "ns3/simulator.h"
28 #include "ns3/traced-callback.h"
29 
30 #include <map>
31 #include <stdint.h>
32 #include <string>
33 #include <tuple>
34 
35 namespace ns3
36 {
37 
38 class Node;
39 class UniformRandomVariable;
40 class EventId;
41 
73 {
74  public:
79  {
86  };
87 
91  static constexpr uint16_t PROT_NUMBER{0xA0ED};
92 
97  static TypeId GetTypeId();
98 
103 
104  // Delete copy constructor and assignment operator to avoid misuse
107 
108  // inherited from NetDevice base class
109  void SetIfIndex(const uint32_t index) override;
110  uint32_t GetIfIndex() const override;
111  Ptr<Channel> GetChannel() const override;
112  void SetAddress(Address address) override;
113  Address GetAddress() const override;
114  bool SetMtu(const uint16_t mtu) override;
115 
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  bool IsPointToPoint() const override;
131  bool IsBridge() 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  Address GetMulticast(Ipv6Address addr) const override;
144 
151 
159  void SetNetDevice(Ptr<NetDevice> device);
160 
169  int64_t AssignStreams(int64_t stream);
170 
181  typedef void (*RxTxTracedCallback)(Ptr<const Packet> packet,
182  Ptr<SixLowPanNetDevice> sixNetDevice,
183  uint32_t ifindex);
184 
196  typedef void (*DropTracedCallback)(DropReason reason,
197  Ptr<const Packet> packet,
198  Ptr<SixLowPanNetDevice> sixNetDevice,
199  uint32_t ifindex);
200 
213  void AddContext(uint8_t contextId,
214  Ipv6Prefix contextPrefix,
215  bool compressionAllowed,
216  Time validLifetime);
217 
230  bool GetContext(uint8_t contextId,
231  Ipv6Prefix& contextPrefix,
232  bool& compressionAllowed,
233  Time& validLifetime);
234 
243  void RenewContext(uint8_t contextId, Time validLifetime);
244 
252  void InvalidateContext(uint8_t contextId);
253 
259  void RemoveContext(uint8_t contextId);
260 
261  protected:
262  void DoDispose() override;
263 
264  private:
274  void ReceiveFromDevice(Ptr<NetDevice> device,
275  Ptr<const Packet> packet,
276  uint16_t protocol,
277  const Address& source,
278  const Address& destination,
279  PacketType packetType);
280 
295  bool DoSend(Ptr<Packet> packet,
296  const Address& source,
297  const Address& dest,
298  uint16_t protocolNumber,
299  bool doSendFrom);
300 
305 
311 
324 
337 
351 
359  uint32_t CompressLowPanHc1(Ptr<Packet> packet, const Address& src, const Address& dst);
360 
367  void DecompressLowPanHc1(Ptr<Packet> packet, const Address& src, const Address& dst);
368 
376  uint32_t CompressLowPanIphc(Ptr<Packet> packet, const Address& src, const Address& dst);
377 
383  bool CanCompressLowPanNhc(uint8_t headerType);
384 
392  bool DecompressLowPanIphc(Ptr<Packet> packet, const Address& src, const Address& dst);
393 
402  uint32_t CompressLowPanNhc(Ptr<Packet> packet,
403  uint8_t headerType,
404  const Address& src,
405  const Address& dst);
406 
417  std::pair<uint8_t, bool> DecompressLowPanNhc(Ptr<Packet> packet,
418  const Address& src,
419  const Address& dst,
420  Ipv6Address srcAddress,
421  Ipv6Address dstAddress);
422 
429  uint32_t CompressLowPanUdpNhc(Ptr<Packet> packet, bool omitChecksum);
430 
437  void DecompressLowPanUdpNhc(Ptr<Packet> packet, Ipv6Address saddr, Ipv6Address daddr);
438 
442  typedef std::pair<std::pair<Address, Address>, std::pair<uint16_t, uint16_t>> FragmentKey_t;
443 
445  typedef std::list<std::tuple<Time, FragmentKey_t, uint32_t>> FragmentsTimeoutsList_t;
447  typedef std::list<std::tuple<Time, FragmentKey_t, uint32_t>>::iterator FragmentsTimeoutsListI_t;
448 
456 
460  void HandleTimeout();
461 
463 
465 
469  class Fragments : public SimpleRefCount<Fragments>
470  {
471  public:
475  Fragments();
476 
480  ~Fragments();
481 
487  void AddFragment(Ptr<Packet> fragment, uint16_t fragmentOffset);
488 
494  void AddFirstFragment(Ptr<Packet> fragment);
495 
500  bool IsEntire() const;
501 
506  Ptr<Packet> GetPacket() const;
507 
512  void SetPacketSize(uint32_t packetSize);
513 
518  std::list<Ptr<Packet>> GetFragments() const;
519 
525 
531 
532  private:
536  uint32_t m_packetSize;
537 
541  std::list<std::pair<Ptr<Packet>, uint16_t>> m_fragments;
542 
547 
552  };
553 
566  void DoFragmentation(Ptr<Packet> packet,
567  uint32_t origPacketSize,
568  uint32_t origHdrSize,
569  uint32_t extraHdrSize,
570  std::list<Ptr<Packet>>& listFragments);
571 
580  bool ProcessFragment(Ptr<Packet>& packet, const Address& src, const Address& dst, bool isFirst);
581 
587  void HandleFragmentsTimeout(FragmentKey_t key, uint32_t iif);
588 
593 
600 
604  typedef std::map<FragmentKey_t, Ptr<Fragments>> MapFragments_t;
608  typedef std::map<FragmentKey_t, Ptr<Fragments>>::iterator MapFragmentsI_t;
609 
612 
618 
619  bool m_useIphc;
620 
621  bool m_meshUnder;
622  uint8_t m_bc0Serial;
624  uint16_t m_meshCacheLength;
627  std::map<Address /* OriginatorAddress */, std::list<uint8_t /* SequenceNumber */>>
629 
632  uint32_t m_ifIndex;
633 
635 
637 
639 
644  {
649  };
650 
651  std::map<uint8_t, ContextEntry>
653 
661  bool FindUnicastCompressionContext(Ipv6Address address, uint8_t& contextId);
662 
670  bool FindMulticastCompressionContext(Ipv6Address address, uint8_t& contextId);
671 
683 };
684 
685 } // namespace ns3
686 
687 #endif /* SIXLOWPAN_NET_DEVICE_H */
a polymophic address class
Definition: address.h:101
An identifier for simulation events.
Definition: event-id.h:55
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
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
A template-based reference counting class.
void SetTimeoutIter(FragmentsTimeoutsListI_t iter)
Set the Timeout iterator.
bool IsEntire() const
If all fragments have been added.
FragmentsTimeoutsListI_t m_timeoutIter
Timeout iterator to "event" handler.
std::list< Ptr< Packet > > GetFragments() const
Get a list of the current stored fragments.
void SetPacketSize(uint32_t packetSize)
Set the packet-to-be-defragmented size.
void AddFragment(Ptr< Packet > fragment, uint16_t fragmentOffset)
Add a fragment to the pool.
uint32_t m_packetSize
The size of the reconstructed packet (bytes).
FragmentsTimeoutsListI_t GetTimeoutIter()
Get the Timeout iterator.
Ptr< Packet > GetPacket() const
Get the entire packet.
std::list< std::pair< Ptr< Packet >, uint16_t > > m_fragments
The current fragments.
Ptr< Packet > m_firstFragment
The very first fragment.
void AddFirstFragment(Ptr< Packet > fragment)
Add the first packet fragment.
Shim performing 6LoWPAN compression, decompression and fragmentation.
std::map< FragmentKey_t, Ptr< Fragments > > MapFragments_t
Container for fragment key -> fragments.
bool IsLinkUp() const override
bool SetMtu(const uint16_t mtu) override
void DecompressLowPanUdpNhc(Ptr< Packet > packet, Ipv6Address saddr, Ipv6Address daddr)
Decompress the headers according to NHC compression.
bool DoSend(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber, bool doSendFrom)
Ipv6Address CleanPrefix(Ipv6Address address, Ipv6Prefix prefix)
Clean an address from its prefix.
void(* DropTracedCallback)(DropReason reason, Ptr< const Packet > packet, Ptr< SixLowPanNetDevice > sixNetDevice, uint32_t ifindex)
TracedCallback signature for packet drop events.
uint8_t m_bc0Serial
Serial number used in BC0 header.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
void SetNode(Ptr< Node > node) override
bool NeedsArp() const override
static constexpr uint16_t PROT_NUMBER
The protocol number for 6LoWPAN (0xA0ED) - see RFC 7973.
EventId m_timeoutEvent
Event for the next scheduled timeout.
FragmentsTimeoutsListI_t SetTimeout(FragmentKey_t key, uint32_t iif)
Set a new timeout "event" for a fragmented packet.
Ptr< UniformRandomVariable > m_rng
Rng for the fragments tag.
uint16_t m_meshCacheLength
length of the cache for each source.
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
bool m_useIphc
Use IPHC or HC1.
void RenewContext(uint8_t contextId, Time validLifetime)
Renew a context used in IPHC stateful compression.
bool DecompressLowPanIphc(Ptr< Packet > packet, const Address &src, const Address &dst)
Decompress the headers according to IPHC compression.
uint32_t CompressLowPanHc1(Ptr< Packet > packet, const Address &src, const Address &dst)
Compress the headers according to HC1 compression.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
Ptr< Node > m_node
Smart pointer to the Node.
bool CanCompressLowPanNhc(uint8_t headerType)
Checks if the next header can be compressed using NHC.
Ptr< Channel > GetChannel() const override
uint16_t GetMtu() const override
Returns the link-layer MTU for this interface.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Address GetAddress() const override
void DropOldestFragmentSet()
Drops the oldest fragment set.
std::list< std::tuple< Time, FragmentKey_t, uint32_t > >::iterator FragmentsTimeoutsListI_t
Container Iterator for fragment timeouts.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
uint32_t m_compressionThreshold
Minimum L2 payload size.
void DoFragmentation(Ptr< Packet > packet, uint32_t origPacketSize, uint32_t origHdrSize, uint32_t extraHdrSize, std::list< Ptr< Packet >> &listFragments)
Performs a packet fragmentation.
Ptr< NetDevice > GetNetDevice() const
Returns a smart pointer to the underlying NetDevice.
void HandleTimeout()
Handles a fragmented packet timeout.
void ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
Receives all the packets from a NetDevice for further processing.
uint32_t CompressLowPanNhc(Ptr< Packet > packet, uint8_t headerType, const Address &src, const Address &dst)
Compress the headers according to NHC compression.
TracedCallback< Ptr< const Packet >, Ptr< SixLowPanNetDevice >, uint32_t > m_txTrace
Callback to trace TX (transmission) packets.
std::pair< std::pair< Address, Address >, std::pair< uint16_t, uint16_t > > FragmentKey_t
Fragment identifier type: src/dst address src/dst port.
DropReason
Enumeration of the dropping reasons in SixLoWPAN.
@ DROP_DISALLOWED_COMPRESSION
HC1 while in IPHC mode or vice-versa.
@ DROP_UNKNOWN_EXTENSION
Unsupported compression kind.
@ DROP_FRAGMENT_BUFFER_FULL
Fragment buffer size exceeded.
@ DROP_SATETFUL_DECOMPRESSION_PROBLEM
Decompression failed due to missing or expired context.
@ DROP_FRAGMENT_TIMEOUT
Fragment timeout exceeded.
void AddContext(uint8_t contextId, Ipv6Prefix contextPrefix, bool compressionAllowed, Time validLifetime)
Add, remove, or update a context used in IPHC stateful compression.
std::map< FragmentKey_t, Ptr< Fragments > >::iterator MapFragmentsI_t
Container Iterator for fragment key -> fragments.
Ptr< NetDevice > m_netDevice
Smart pointer to the underlying NetDevice.
void SetNetDevice(Ptr< NetDevice > device)
Setup SixLowPan to be a proxy for the specified NetDevice.
std::map< uint8_t, ContextEntry > m_contextTable
Table of the contexts used in compression/decompression.
TracedCallback< Ptr< const Packet >, Ptr< SixLowPanNetDevice >, uint32_t > m_rxTrace
Callback to trace RX (reception) packets.
bool GetContext(uint8_t contextId, Ipv6Prefix &contextPrefix, bool &compressionAllowed, Time &validLifetime)
Get a context used in IPHC stateful compression.
uint32_t GetIfIndex() const override
SixLowPanNetDevice(const SixLowPanNetDevice &)=delete
std::list< std::tuple< Time, FragmentKey_t, uint32_t > > FragmentsTimeoutsList_t
Container for fragment timeouts.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
bool IsBroadcast() const override
Address Get16MacFrom48Mac(Address addr)
Get a Mac16 from its Mac48 pseudo-MAC.
TracedCallback< DropReason, Ptr< const Packet >, Ptr< SixLowPanNetDevice >, uint32_t > m_dropTrace
Callback to trace drop packets.
void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb) override
bool FindUnicastCompressionContext(Ipv6Address address, uint8_t &contextId)
Finds if the given unicast address matches a context for compression.
Ptr< RandomVariableStream > m_meshUnderJitter
Random variable for the mesh-under packet retransmission.
uint32_t CompressLowPanUdpNhc(Ptr< Packet > packet, bool omitChecksum)
Compress the headers according to NHC compression.
void RemoveContext(uint8_t contextId)
Remove a context used in IPHC stateful compression.
bool m_omitUdpChecksum
Omit UDP checksum in NC1 encoding.
Ptr< Node > GetNode() const override
void AddLinkChangeCallback(Callback< void > callback) override
uint32_t m_ifIndex
Interface index.
Address GetBroadcast() const override
void SetIfIndex(const uint32_t index) override
uint32_t CompressLowPanIphc(Ptr< Packet > packet, const Address &src, const Address &dst)
Compress the headers according to IPHC compression.
void(* RxTxTracedCallback)(Ptr< const Packet > packet, Ptr< SixLowPanNetDevice > sixNetDevice, uint32_t ifindex)
TracedCallback signature for packet send/receive events.
void DoDispose() override
Destructor implementation.
bool IsMulticast() const override
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
Time m_fragmentExpirationTimeout
Time limit for fragment rebuilding.
uint16_t m_fragmentReassemblyListSize
How many packets can be rebuilt at the same time.
uint8_t m_meshUnderHopsLeft
Start value for mesh-under hops left.
void SetAddress(Address address) override
Set the address of this interface.
void DecompressLowPanHc1(Ptr< Packet > packet, const Address &src, const Address &dst)
Decompress the headers according to HC1 compression.
FragmentsTimeoutsList_t m_timeoutEventList
Timeout "events" container.
std::pair< uint8_t, bool > DecompressLowPanNhc(Ptr< Packet > packet, const Address &src, const Address &dst, Ipv6Address srcAddress, Ipv6Address dstAddress)
Decompress the headers according to NHC compression.
NetDevice::PromiscReceiveCallback m_promiscRxCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode.
void HandleFragmentsTimeout(FragmentKey_t key, uint32_t iif)
Process the timeout for packet fragments.
void InvalidateContext(uint8_t contextId)
Invalidate a context used in IPHC stateful compression.
static TypeId GetTypeId()
Get the type ID.
bool SupportsSendFrom() const override
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
bool ProcessFragment(Ptr< Packet > &packet, const Address &src, const Address &dst, bool isFirst)
Process a packet fragment.
MapFragments_t m_fragments
Fragments hold to be rebuilt.
bool FindMulticastCompressionContext(Ipv6Address address, uint8_t &contextId)
Finds if the given multicast address matches a context for compression.
bool m_meshUnder
Use a mesh-under routing.
SixLowPanNetDevice & operator=(const SixLowPanNetDevice &)=delete
std::map< Address, std::list< uint8_t > > m_seenPkts
Seen packets, memorized by OriginatorAddress, SequenceNumber.
SixLowPanNetDevice()
Constructor for the SixLowPanNetDevice.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define list
Structure holding the information for a context (used in compression and decompression)
Ipv6Prefix contextPrefix
context prefix to be used in compression/decompression
bool compressionAllowed
compression and decompression allowed (true), decompression only (false)
Time validLifetime
validity period
static const uint32_t packetSize
Packet size generated at the AP.