src/devices/emu/emu-net-device.h
author vincent@clarinet.u-strasbg.fr
Wed, 05 Nov 2008 14:49:21 -0800
changeset 3842 545ddf9398ed
parent 3830 8862b9be62bb
child 3843 ca2159d54d75
permissions -rw-r--r--
apply patch for bug 294 (GetMulticastAddr) + emu device update + rescan
craigdo@3827
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
craigdo@3827
     2
/*
craigdo@3827
     3
 * Copyright (c) 2008 University of Washington
craigdo@3827
     4
 *
craigdo@3827
     5
 * This program is free software; you can redistribute it and/or modify
craigdo@3827
     6
 * it under the terms of the GNU General Public License version 2 as
craigdo@3827
     7
 * published by the Free Software Foundation;
craigdo@3827
     8
 *
craigdo@3827
     9
 * This program is distributed in the hope that it will be useful,
craigdo@3827
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
craigdo@3827
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
craigdo@3827
    12
 * GNU General Public License for more details.
craigdo@3827
    13
 *
craigdo@3827
    14
 * You should have received a copy of the GNU General Public License
craigdo@3827
    15
 * along with this program; if not, write to the Free Software
craigdo@3827
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
craigdo@3827
    17
 */
craigdo@3827
    18
craigdo@3830
    19
#ifndef EMU_NET_DEVICE_H
craigdo@3830
    20
#define EMU_NET_DEVICE_H
craigdo@3827
    21
craigdo@3827
    22
#include <string.h>
craigdo@3827
    23
#include "ns3/address.h"
craigdo@3827
    24
#include "ns3/net-device.h"
craigdo@3827
    25
#include "ns3/node.h"
craigdo@3827
    26
#include "ns3/callback.h"
craigdo@3827
    27
#include "ns3/packet.h"
craigdo@3827
    28
#include "ns3/traced-callback.h"
craigdo@3827
    29
#include "ns3/event-id.h"
craigdo@3827
    30
#include "ns3/nstime.h"
craigdo@3827
    31
#include "ns3/data-rate.h"
craigdo@3827
    32
#include "ns3/ptr.h"
craigdo@3827
    33
#include "ns3/mac48-address.h"
craigdo@3827
    34
#include "ns3/system-thread.h"
craigdo@3827
    35
craigdo@3827
    36
namespace ns3 {
craigdo@3827
    37
craigdo@3827
    38
class Queue;
craigdo@3827
    39
craigdo@3827
    40
/**
craigdo@3830
    41
 * \class EmuNetDevice
craigdo@3830
    42
 * \brief A Device for an Emu Network Link.
craigdo@3827
    43
 */
craigdo@3830
    44
class EmuNetDevice : public NetDevice 
craigdo@3827
    45
{
craigdo@3827
    46
public:
craigdo@3827
    47
  static TypeId GetTypeId (void);
craigdo@3827
    48
craigdo@3827
    49
  /**
craigdo@3830
    50
   * Construct a EmuNetDevice
craigdo@3827
    51
   *
craigdo@3830
    52
   * This is the constructor for the EmuNetDevice.  It takes as a
craigdo@3827
    53
   */
craigdo@3830
    54
  EmuNetDevice ();
craigdo@3827
    55
craigdo@3827
    56
  /**
craigdo@3830
    57
   * Destroy a EmuNetDevice
craigdo@3827
    58
   *
craigdo@3830
    59
   * This is the destructor for the EmuNetDevice.
craigdo@3827
    60
   */
craigdo@3830
    61
  virtual ~EmuNetDevice ();
craigdo@3827
    62
craigdo@3827
    63
  /**
craigdo@3827
    64
   * Set the Data Rate used for transmission of packets.  
craigdo@3827
    65
   *
craigdo@3827
    66
   * @see Attach ()
craigdo@3827
    67
   * @param bps the data rate at which this object operates
craigdo@3827
    68
   */
craigdo@3827
    69
  void SetDataRate (DataRate bps);
craigdo@3827
    70
craigdo@3827
    71
  /**
craigdo@3827
    72
   * Set the inteframe gap used to separate packets.  The interframe gap
craigdo@3827
    73
   * defines the minimum space required between packets sent by this device.
craigdo@3827
    74
   *
craigdo@3827
    75
   * @param t the interframe gap time
craigdo@3827
    76
   */
craigdo@3827
    77
  void SetInterframeGap (Time t);
craigdo@3827
    78
craigdo@3827
    79
  /**
craigdo@3827
    80
   * Set a start time for the device.
craigdo@3827
    81
   *
craigdo@3827
    82
   * @param tStart the start time
craigdo@3827
    83
   */
craigdo@3827
    84
  void Start (Time tStart);
craigdo@3827
    85
craigdo@3827
    86
  /**
craigdo@3827
    87
   * Set a stop time for the device.
craigdo@3827
    88
   *
craigdo@3827
    89
   * @param tStop the stop time
craigdo@3827
    90
   */
craigdo@3827
    91
  void Stop (Time tStop);
craigdo@3827
    92
craigdo@3827
    93
  /**
craigdo@3830
    94
   * Attach a queue to the EmuNetDevice.
craigdo@3827
    95
   *
craigdo@3830
    96
   * The EmuNetDevice "owns" a queue that implements a queueing 
craigdo@3827
    97
   * method such as DropTail or RED.  
craigdo@3827
    98
   *
craigdo@3827
    99
   * @see Queue
craigdo@3827
   100
   * @see DropTailQueue
craigdo@3827
   101
   * @param queue Ptr to the new queue.
craigdo@3827
   102
   */
craigdo@3827
   103
  void SetQueue (Ptr<Queue> queue);
craigdo@3827
   104
craigdo@3827
   105
  /**
craigdo@3827
   106
   * Receive a packet.
craigdo@3827
   107
   *
craigdo@3830
   108
   * The EmuNetDevice receives packets from its socket reader
craigdo@3827
   109
   * and forwards them up the protocol stack.  This is the public method
craigdo@3827
   110
   * used by the reader to indicate that a packet has arrived at the device.
craigdo@3827
   111
   *
craigdo@3827
   112
   * @param p Ptr to the received packet.
craigdo@3827
   113
   */
craigdo@3827
   114
  void Receive (Ptr<Packet> p);
craigdo@3827
   115
craigdo@3827
   116
  /**
craigdo@3827
   117
   * Assign a MAC address to this device.
craigdo@3827
   118
   *
craigdo@3827
   119
   * @see Mac48Address
craigdo@3827
   120
   * @param addr The new address.
craigdo@3827
   121
   */
craigdo@3827
   122
  void SetAddress (Mac48Address addr);
craigdo@3827
   123
craigdo@3827
   124
//
craigdo@3827
   125
// Pure virtual methods inherited from NetDevice we must implement.
craigdo@3827
   126
//
craigdo@3827
   127
  virtual void SetName(const std::string name);
craigdo@3827
   128
  virtual std::string GetName(void) const;
craigdo@3827
   129
craigdo@3827
   130
  virtual void SetIfIndex(const uint32_t index);
craigdo@3827
   131
  virtual uint32_t GetIfIndex(void) const;
craigdo@3827
   132
craigdo@3827
   133
  virtual Ptr<Channel> GetChannel (void) const;
craigdo@3827
   134
  virtual Address GetAddress (void) const;
craigdo@3827
   135
craigdo@3827
   136
  virtual bool SetMtu (const uint16_t mtu);
craigdo@3827
   137
  virtual uint16_t GetMtu (void) const;
craigdo@3827
   138
craigdo@3827
   139
  virtual bool IsLinkUp (void) const;
craigdo@3827
   140
craigdo@3827
   141
  virtual void SetLinkChangeCallback (Callback<void> callback);
craigdo@3827
   142
craigdo@3827
   143
  virtual bool IsBroadcast (void) const;
craigdo@3827
   144
  virtual Address GetBroadcast (void) const;
craigdo@3827
   145
craigdo@3827
   146
  virtual bool IsMulticast (void) const;
craigdo@3827
   147
vincent@3842
   148
  /**
vincent@3842
   149
   * \brief Make and return a MAC multicast address using the provided
vincent@3842
   150
   *        multicast group
vincent@3842
   151
   *
vincent@3842
   152
   * RFC 1112 says that an Ipv4 host group address is mapped to an Ethernet 
vincent@3842
   153
   * multicast address by placing the low-order 23-bits of the IP address into 
vincent@3842
   154
   * the low-order 23 bits of the Ethernet multicast address 
vincent@3842
   155
   * 01-00-5E-00-00-00 (hex).
vincent@3842
   156
   *
vincent@3842
   157
   * This method performs the multicast address creation function appropriate
vincent@3842
   158
   * to an EUI-48-based CSMA device.  This MAC address is encapsulated in an
vincent@3842
   159
   *  abstract Address to avoid dependencies on the exact address format.
vincent@3842
   160
   *
vincent@3842
   161
   * \param multicastGroup The IP address for the multicast group destination
vincent@3842
   162
   * of the packet.
vincent@3842
   163
   * \return The MAC multicast Address used to send packets to the provided
vincent@3842
   164
   * multicast group.
vincent@3842
   165
   *
vincent@3842
   166
   * \see Ipv4Address
vincent@3842
   167
   * \see Mac48Address
vincent@3842
   168
   * \see Address
vincent@3842
   169
   */
vincent@3842
   170
  virtual Address GetMulticast (Ipv4Address multicastGroup) const;
vincent@3842
   171
vincent@3842
   172
  /**
vincent@3842
   173
   * Is this a point to point link?
vincent@3842
   174
   * \returns false.
vincent@3842
   175
   */
craigdo@3827
   176
  virtual bool IsPointToPoint (void) const;
craigdo@3827
   177
craigdo@3827
   178
  virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber);
craigdo@3827
   179
craigdo@3827
   180
  virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
craigdo@3827
   181
craigdo@3827
   182
  virtual Ptr<Node> GetNode (void) const;
craigdo@3827
   183
  virtual void SetNode (Ptr<Node> node);
craigdo@3827
   184
craigdo@3827
   185
  virtual bool NeedsArp (void) const;
craigdo@3827
   186
craigdo@3827
   187
  virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
craigdo@3827
   188
  virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb);
craigdo@3827
   189
craigdo@3827
   190
  virtual bool SupportsSendFrom (void) const;
craigdo@3827
   191
craigdo@3827
   192
private:
craigdo@3827
   193
craigdo@3827
   194
  virtual void DoDispose (void);
craigdo@3827
   195
craigdo@3827
   196
  /**
craigdo@3830
   197
   * Call out to a separate process running as suid root in order to get a raw 
craigdo@3830
   198
   * socket.  We do this to avoid having the entire simulation running as root.
craigdo@3830
   199
   * If this method returns, we'll have a raw socket waiting for us in m_sock.
craigdo@3830
   200
   */
craigdo@3830
   201
  void CreateSocket (void);
craigdo@3830
   202
craigdo@3830
   203
  /**
craigdo@3830
   204
   * Figure out where the raw socket creation process lives on the system.
craigdo@3830
   205
   */
craigdo@3830
   206
  std::string FindCreator (void);
craigdo@3830
   207
craigdo@3830
   208
  /**
craigdo@3827
   209
   * Get a copy of the attached Queue.
craigdo@3827
   210
   *
craigdo@3827
   211
   * This method is provided for any derived class that may need to get
craigdo@3827
   212
   * direct access to the underlying queue.
craigdo@3827
   213
   *
craigdo@3827
   214
   * @returns Ptr to the queue.
craigdo@3827
   215
   */
craigdo@3827
   216
  Ptr<Queue> GetQueue(void) const; 
craigdo@3827
   217
craigdo@3827
   218
  /**
craigdo@3827
   219
   * Spin up the device
craigdo@3827
   220
   */
craigdo@3827
   221
  void StartDevice (void);
craigdo@3827
   222
craigdo@3827
   223
  /**
craigdo@3827
   224
   * Tear down the device
craigdo@3827
   225
   */
craigdo@3827
   226
  void StopDevice (void);
craigdo@3827
   227
craigdo@3827
   228
  /**
craigdo@3827
   229
   * Loop to read and process packets
craigdo@3827
   230
   */
craigdo@3827
   231
  void ReadThread (void);
craigdo@3827
   232
craigdo@3827
   233
  /**
craigdo@3827
   234
   * Method to handle received packets.  Synchronized with simulator via ScheduleNow from ReadThread.
craigdo@3827
   235
   */
craigdo@3827
   236
  void ForwardUp (uint8_t *buf, uint32_t len);
craigdo@3827
   237
craigdo@3827
   238
  /**
craigdo@3827
   239
   * Adds the necessary headers and trailers to a packet of data in order to
craigdo@3827
   240
   * respect the protocol implemented by the agent.
craigdo@3827
   241
   */
craigdo@3827
   242
  void AddHeader(Ptr<Packet> p, uint16_t protocolNumber);
craigdo@3827
   243
craigdo@3827
   244
  /**
craigdo@3827
   245
   * Removes, from a packet of data, all headers and trailers that
craigdo@3827
   246
   * relate to the protocol implemented by the agent
craigdo@3827
   247
   * \return Returns true if the packet should be forwarded up the
craigdo@3827
   248
   * protocol stack.
craigdo@3827
   249
   */
craigdo@3827
   250
  bool ProcessHeader(Ptr<Packet> p, uint16_t& param);
craigdo@3827
   251
craigdo@3827
   252
  /**
craigdo@3827
   253
   * Start Sending a Packet Down the Wire.
craigdo@3827
   254
   *
craigdo@3827
   255
   * @returns true if success, false on failure
craigdo@3827
   256
   */
craigdo@3827
   257
  bool TransmitStart (Ptr<Packet> p);
craigdo@3827
   258
craigdo@3827
   259
  void NotifyLinkUp (void);
craigdo@3827
   260
craigdo@3827
   261
  /**
craigdo@3830
   262
   * The Queue which this EmuNetDevice uses as a packet source.
craigdo@3830
   263
   * Management of this Queue has been delegated to the EmuNetDevice
craigdo@3827
   264
   * and it has the responsibility for deletion.
craigdo@3827
   265
   * @see class Queue
craigdo@3827
   266
   * @see class DropTailQueue
craigdo@3827
   267
   */
craigdo@3827
   268
  Ptr<Queue> m_queue;
craigdo@3827
   269
craigdo@3827
   270
  /**
craigdo@3827
   271
   * The trace source for the packet reception events that the device can
craigdo@3827
   272
   * fire.
craigdo@3827
   273
   *
craigdo@3827
   274
   * @see class CallBackTraceSource
craigdo@3827
   275
   */
craigdo@3827
   276
  TracedCallback<Ptr<const Packet> > m_rxTrace;
craigdo@3827
   277
craigdo@3827
   278
  /**
craigdo@3827
   279
   * The trace source for the packet drop events that the device can
craigdo@3827
   280
   * fire.
craigdo@3827
   281
   *
craigdo@3827
   282
   * @see class CallBackTraceSource
craigdo@3827
   283
   */
craigdo@3827
   284
  TracedCallback<Ptr<const Packet> > m_dropTrace;
craigdo@3827
   285
craigdo@3827
   286
  /**
craigdo@3827
   287
   * Time to start spinning up the device
craigdo@3827
   288
   */
craigdo@3827
   289
  Time m_tStart;
craigdo@3827
   290
craigdo@3827
   291
  /**
craigdo@3827
   292
   * Time to start tearing down the device
craigdo@3827
   293
   */
craigdo@3827
   294
  Time m_tStop;
craigdo@3827
   295
craigdo@3827
   296
  EventId m_startEvent;
craigdo@3827
   297
  EventId m_stopEvent;
craigdo@3827
   298
craigdo@3827
   299
  int32_t m_sock;
craigdo@3827
   300
craigdo@3827
   301
  Ptr<SystemThread> m_readThread;
craigdo@3827
   302
craigdo@3827
   303
  /**
craigdo@3827
   304
   * The Node to which this device is attached.
craigdo@3827
   305
   */
craigdo@3827
   306
  Ptr<Node> m_node;
craigdo@3827
   307
craigdo@3827
   308
  /**
craigdo@3827
   309
   * The MAC address which has been assigned to this device.
craigdo@3827
   310
   */
craigdo@3827
   311
  Mac48Address m_address;
craigdo@3827
   312
craigdo@3827
   313
  /**
craigdo@3827
   314
   * The callback used to notify higher layers that a packet has been received.
craigdo@3827
   315
   */
craigdo@3827
   316
  NetDevice::ReceiveCallback m_rxCallback;
craigdo@3827
   317
craigdo@3827
   318
  /**
craigdo@3827
   319
   * The callback used to notify higher layers that a packet has been received in promiscuous mode.
craigdo@3827
   320
   */
craigdo@3827
   321
  NetDevice::PromiscReceiveCallback m_promiscRxCallback;
craigdo@3827
   322
craigdo@3827
   323
  /**
craigdo@3827
   324
   * The ns-3 interface index (in the sense of net device index) that has been assigned to this network device.
craigdo@3827
   325
   */
craigdo@3827
   326
  uint32_t m_ifIndex;
craigdo@3827
   327
craigdo@3827
   328
  /**
craigdo@3827
   329
   * The Unix interface index that we got from the system and which corresponds to the interface (e.g., "eth1")
craigdo@3827
   330
   * we are using to talk to the network.  Valid when m_sock is valid.
craigdo@3827
   331
   */
craigdo@3827
   332
  int32_t m_sll_ifindex;
craigdo@3827
   333
craigdo@3827
   334
  /**
craigdo@3827
   335
   * The human readable name of this device.
craigdo@3827
   336
   */
craigdo@3827
   337
  std::string m_name;
craigdo@3827
   338
craigdo@3827
   339
  /**
craigdo@3827
   340
   * Flag indicating whether or not the link is up.  In this case,
craigdo@3827
   341
   * whether or not the device is connected to a channel.
craigdo@3827
   342
   */
craigdo@3827
   343
  bool m_linkUp;
craigdo@3827
   344
craigdo@3827
   345
  /**
craigdo@3827
   346
   * Callback to fire if the link changes state (up or down).
craigdo@3827
   347
   */
craigdo@3827
   348
  Callback<void> m_linkChangeCallback;
craigdo@3827
   349
craigdo@3827
   350
  /**
craigdo@3827
   351
   * The unix/linux name of the underlying device (e.g., eth0)
craigdo@3827
   352
   */
craigdo@3827
   353
  std::string m_deviceName;
craigdo@3827
   354
};
craigdo@3827
   355
craigdo@3827
   356
} // namespace ns3
craigdo@3827
   357
craigdo@3830
   358
#endif // EMU_NET_DEVICE_H
craigdo@3827
   359