A Discrete-Event Network Simulator
API
tap-bridge.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "tap-bridge.h"
20 #include "tap-encode-decode.h"
21 
22 #include "ns3/node.h"
23 #include "ns3/channel.h"
24 #include "ns3/packet.h"
25 #include "ns3/ethernet-header.h"
26 #include "ns3/llc-snap-header.h"
27 #include "ns3/log.h"
28 #include "ns3/abort.h"
29 #include "ns3/boolean.h"
30 #include "ns3/string.h"
31 #include "ns3/enum.h"
32 #include "ns3/ipv4.h"
33 #include "ns3/simulator.h"
34 #include "ns3/realtime-simulator-impl.h"
35 #include "ns3/unix-fd-reader.h"
36 #include "ns3/uinteger.h"
37 
38 #include <sys/wait.h>
39 #include <sys/stat.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <sys/ioctl.h>
43 #include <net/if.h>
44 #include <cerrno>
45 #include <limits>
46 #include <cstdlib>
47 #include <unistd.h>
48 
49 namespace ns3 {
50 
51 NS_LOG_COMPONENT_DEFINE ("TapBridge");
52 
54 {
56 
57  uint32_t bufferSize = 65536;
58  uint8_t *buf = (uint8_t *)std::malloc (bufferSize);
59  NS_ABORT_MSG_IF (buf == 0, "malloc() failed");
60 
61  NS_LOG_LOGIC ("Calling read on tap device fd " << m_fd);
62  ssize_t len = read (m_fd, buf, bufferSize);
63  if (len <= 0)
64  {
65  NS_LOG_INFO ("TapBridgeFdReader::DoRead(): done");
66  std::free (buf);
67  buf = 0;
68  len = 0;
69  }
70 
71  return FdReader::Data (buf, len);
72 }
73 
74 #define TAP_MAGIC 95549
75 
76 NS_OBJECT_ENSURE_REGISTERED (TapBridge);
77 
78 TypeId
80 {
81  static TypeId tid = TypeId ("ns3::TapBridge")
82  .SetParent<NetDevice> ()
83  .SetGroupName ("TapBridge")
84  .AddConstructor<TapBridge> ()
85  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
86  UintegerValue (0),
89  MakeUintegerChecker<uint16_t> ())
90  .AddAttribute ("DeviceName",
91  "The name of the tap device to create.",
92  StringValue (""),
95  .AddAttribute ("Gateway",
96  "The IP address of the default gateway to assign to the host machine, when in ConfigureLocal mode.",
97  Ipv4AddressValue ("255.255.255.255"),
100  .AddAttribute ("IpAddress",
101  "The IP address to assign to the tap device, when in ConfigureLocal mode. "
102  "This address will override the discovered IP address of the simulated device.",
103  Ipv4AddressValue ("255.255.255.255"),
106  .AddAttribute ("MacAddress",
107  "The MAC address to assign to the tap device, when in ConfigureLocal mode. "
108  "This address will override the discovered MAC address of the simulated device.",
109  Mac48AddressValue (Mac48Address ("ff:ff:ff:ff:ff:ff")),
112  .AddAttribute ("Netmask",
113  "The network mask to assign to the tap device, when in ConfigureLocal mode. "
114  "This address will override the discovered MAC address of the simulated device.",
115  Ipv4MaskValue ("255.255.255.255"),
118  .AddAttribute ("Start",
119  "The simulation time at which to spin up the tap device read thread.",
120  TimeValue (Seconds (0.)),
122  MakeTimeChecker ())
123  .AddAttribute ("Stop",
124  "The simulation time at which to tear down the tap device read thread.",
125  TimeValue (Seconds (0.)),
127  MakeTimeChecker ())
128  .AddAttribute ("Mode",
129  "The operating and configuration mode to use.",
132  MakeEnumChecker (CONFIGURE_LOCAL, "ConfigureLocal",
133  USE_LOCAL, "UseLocal",
134  USE_BRIDGE, "UseBridge"))
135  ;
136  return tid;
137 }
138 
140  : m_node (0),
141  m_ifIndex (0),
142  m_sock (-1),
143  m_startEvent (),
144  m_stopEvent (),
145  m_fdReader (0),
146  m_ns3AddressRewritten (false)
147 {
149  m_packetBuffer = new uint8_t[65536];
150  Start (m_tStart);
151 }
152 
154 {
156 
157  StopTapDevice ();
158 
159  delete [] m_packetBuffer;
160  m_packetBuffer = 0;
161 
162  m_bridgedDevice = 0;
163 }
164 
165 void
167 {
170 }
171 
172 void
174 {
175  NS_LOG_FUNCTION (tStart);
176 
177  //
178  // Cancel any pending start event and schedule a new one at some relative time in the future.
179  //
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION (tStop);
188  //
189  // Cancel any pending stop event and schedule a new one at some relative time in the future.
190  //
193 }
194 
195 void
197 {
199 
200  NS_ABORT_MSG_IF (m_sock != -1, "TapBridge::StartTapDevice(): Tap is already started");
201 
202  //
203  // A similar story exists for the node ID. We can't just naively do a
204  // GetNode ()->GetId () since GetNode is going to give us a Ptr<Node> which
205  // is reference counted. We need to stash away the node ID for use in the
206  // read thread.
207  //
208  m_nodeId = GetNode ()->GetId ();
209 
210  //
211  // Spin up the tap bridge and start receiving packets.
212  //
213  NS_LOG_LOGIC ("Creating tap device");
214 
215  //
216  // Call out to a separate process running as suid root in order to get the
217  // tap device allocated and set up. We do this to avoid having the entire
218  // simulation running as root. If this method returns, we'll have a socket
219  // waiting for us in m_sock that we can use to talk to the newly created
220  // tap device.
221  //
222  CreateTap ();
223 
224  // Declare the link up
225  NotifyLinkUp ();
226 
227  //
228  // Now spin up a read thread to read packets from the tap device.
229  //
230  NS_ABORT_MSG_IF (m_fdReader != 0,"TapBridge::StartTapDevice(): Receive thread is already running");
231  NS_LOG_LOGIC ("Spinning up read thread");
232 
233  m_fdReader = Create<TapBridgeFdReader> ();
235 }
236 
237 void
239 {
241 
242  if (m_fdReader != 0)
243  {
244  m_fdReader->Stop ();
245  m_fdReader = 0;
246  }
247 
248  if (m_sock != -1)
249  {
250  close (m_sock);
251  m_sock = -1;
252  }
253 }
254 
255 void
257 {
259 
260  //
261  // The TapBridge has three distinct operating modes. At this point, the
262  // differences revolve around who is responsible for creating and configuring
263  // the underlying network tap that we use. In ConfigureLocal mode, the
264  // TapBridge has the responsibility for creating and configuring the TAP.
265  //
266  // In UseBridge or UseLocal modes, the user will provide us a configuration
267  // and we have to adapt to it. For example, in UseLocal mode, the user will
268  // be configuring a tap device outside the scope of the ns-3 simulation and
269  // will be expecting us to work with it. The user will do something like:
270  //
271  // sudo tunctl -t tap0
272  // sudo ifconfig tap0 hw ether 00:00:00:00:00:01
273  // sudo ifconfig tap0 10.1.1.1 netmask 255.255.255.0 up
274  //
275  // The user will then set the "Mode" Attribute of the TapBridge to "UseLocal"
276  // and the "DeviceName" Attribute to "tap0" in this case.
277  //
278  // In ConfigureLocal mode, the user is asking the TapBridge to do the
279  // configuration and create a TAP with the provided "DeviceName" with which
280  // the user can later do what she wants. We need to extract values for the
281  // MAC address, IP address, net mask, etc, from the simualtion itself and
282  // use them to initialize corresponding values on the created tap device.
283  //
284  // In UseBridge mode, the user is asking us to use an existing tap device
285  // has been included in an OS bridge. She is asking us to take the simulated
286  // net device and logically add it to the existing bridge. We expect that
287  // the user has done something like:
288  //
289  // sudo brctl addbr mybridge
290  // sudo tunctl -t mytap
291  // sudo ifconfig mytap hw ether 00:00:00:00:00:01
292  // sudo ifconfig mytap 0.0.0.0 up
293  // sudo brctl addif mybridge mytap
294  // sudo brctl addif mybridge ...
295  // sudo ifconfig mybridge 10.1.1.1 netmask 255.255.255.0 up
296  //
297  // The bottom line at this point is that we want to either create or use a
298  // tap device on the host based on the verb part "Use" or "Configure" of the
299  // operating mode. Unfortunately for us you have to have root privileges to
300  // do either. Instead of running the entire simulation as root, we decided
301  // to make a small program who's whole reason for being is to run as suid
302  // root and do what it takes to create the tap. We're just going to pass
303  // off the configuration information to that program and let it deal with
304  // the situation.
305  //
306  // We're going to fork and exec that program soon, but first we need to have
307  // a socket to talk to it with. So we create a local interprocess (Unix)
308  // socket for that purpose.
309  //
310  int sock = socket (PF_UNIX, SOCK_DGRAM, 0);
311  NS_ABORT_MSG_IF (sock == -1, "TapBridge::CreateTap(): Unix socket creation error, errno = " << std::strerror (errno));
312 
313  //
314  // Bind to that socket and let the kernel allocate an endpoint
315  //
316  struct sockaddr_un un;
317  memset (&un, 0, sizeof (un));
318  un.sun_family = AF_UNIX;
319  int status = bind (sock, (struct sockaddr*)&un, sizeof (sa_family_t));
320  NS_ABORT_MSG_IF (status == -1, "TapBridge::CreateTap(): Could not bind(): errno = " << std::strerror (errno));
321  NS_LOG_INFO ("Created Unix socket");
322  NS_LOG_INFO ("sun_family = " << un.sun_family);
323  NS_LOG_INFO ("sun_path = " << un.sun_path);
324 
325  //
326  // We have a socket here, but we want to get it there -- to the program we're
327  // going to exec. What we'll do is to do a getsockname and then encode the
328  // resulting address information as a string, and then send the string to the
329  // program as an argument. So we need to get the sock name.
330  //
331  socklen_t len = sizeof (un);
332  status = getsockname (sock, (struct sockaddr*)&un, &len);
333  NS_ABORT_MSG_IF (status == -1, "TapBridge::CreateTap(): Could not getsockname(): errno = " << std::strerror (errno));
334 
335  //
336  // Now encode that socket name (family and path) as a string of hex digits
337  //
338  std::string path = TapBufferToString ((uint8_t *)&un, len);
339  NS_LOG_INFO ("Encoded Unix socket as \"" << path << "\"");
340 
341  //
342  // Tom Goff reports the possibility of a deadlock when trying to acquire the
343  // python GIL here. He says that this might be due to trying to access Python
344  // objects after fork() without calling PyOS_AfterFork() to properly reset
345  // Python state (including the GIL). Originally these next three lines were
346  // done after the fork, but were moved here to work around the deadlock.
347  //
349  Ptr<Node> n = nd->GetNode ();
350  Ptr<Ipv4> ipv4 = n->GetObject<Ipv4> ();
351 
352  //
353  // Fork and exec the process to create our socket. If we're us (the parent)
354  // we wait for the child (the creator) to complete and read the socket it
355  // created and passed back using the ancillary data mechanism.
356  //
357  pid_t pid = ::fork ();
358  if (pid == 0)
359  {
360  NS_LOG_DEBUG ("Child process");
361 
362  //
363  // build a command line argument from the encoded endpoint string that
364  // the socket creation process will use to figure out how to respond to
365  // the (now) parent process. We're going to have to give this program
366  // quite a bit of information.
367  //
368  // -d<device-name> The name of the tap device we want to create;
369  // -g<gateway-address> The IP address to use as the default gateway;
370  // -i<IP-address> The IP address to assign to the new tap device;
371  // -m<MAC-address> The MAC-48 address to assign to the new tap device;
372  // -n<network-mask> The network mask to assign to the new tap device;
373  // -o<operating mode> The operating mode of the bridge (1=ConfigureLocal, 2=UseLocal, 3=UseBridge)
374  // -p<path> the path to the unix socket described above.
375  //
376  // Example tap-creator -dnewdev -g1.2.3.2 -i1.2.3.1 -m08:00:2e:00:01:23 -n255.255.255.0 -o1 -pblah
377  //
378  // We want to get as much of this stuff automagically as possible.
379  //
380  // For CONFIGURE_LOCAL mode only:
381  // <IP-address> is the IP address we are going to set in the newly
382  // created Tap device on the Linux host. At the point in the simulation
383  // where devices are coming up, we should have all of our IP addresses
384  // assigned. That means that we can find the IP address to assign to
385  // the new Tap device from the IP address associated with the bridged
386  // net device.
387  //
388 
389  bool wantIp = (m_mode == CONFIGURE_LOCAL);
390 
391  if (wantIp
392  && (ipv4 == 0)
393  && m_tapIp.IsBroadcast ()
395  {
396  NS_FATAL_ERROR ("TapBridge::CreateTap(): Tap device IP configuration requested but neither IP address nor IP netmask is provided");
397  }
398 
399  // Some stub values to make tap-creator happy
400  Ipv4Address ipv4Address ("255.255.255.255");
401  Ipv4Mask ipv4Mask ("255.255.255.255");
402 
403  if (ipv4 != 0)
404  {
405  uint32_t index = ipv4->GetInterfaceForDevice (nd);
406  if (ipv4->GetNAddresses (index) > 1)
407  {
408  NS_LOG_WARN ("Underlying bridged NetDevice has multiple IP addresses; using first one.");
409  }
410  ipv4Address = ipv4->GetAddress (index, 0).GetLocal ();
411 
412  //
413  // The net mask is sitting right there next to the ipv4 address.
414  //
415  ipv4Mask = ipv4->GetAddress (index, 0).GetMask ();
416  }
417 
418  //
419  // The MAC address should also already be assigned and waiting for us in
420  // the bridged net device.
421  //
422  Address address = nd->GetAddress ();
424 
425  //
426  // The device-name is something we may want the system to make up in
427  // every case. We also rely on it being configured via an Attribute
428  // through the helper. By default, it is set to the empty string
429  // which tells the system to make up a device name such as "tap123".
430  //
431  std::ostringstream ossDeviceName;
432  ossDeviceName << "-d" << m_tapDeviceName;
433 
434  //
435  // The gateway-address is something we can't derive, so we rely on it
436  // being configured via an Attribute through the helper.
437  //
438  std::ostringstream ossGateway;
439  ossGateway << "-g" << m_tapGateway;
440 
441  //
442  // For flexibility, we do allow a client to override any of the values
443  // above via attributes, so only use our found values if the Attribute
444  // is not equal to its default value (empty string or broadcast address).
445  //
446  std::ostringstream ossIp;
447  if (m_tapIp.IsBroadcast ())
448  {
449  ossIp << "-i" << ipv4Address;
450  }
451  else
452  {
453  ossIp << "-i" << m_tapIp;
454  }
455 
456  std::ostringstream ossMac;
457  if (m_tapMac.IsBroadcast ())
458  {
459  ossMac << "-m" << mac48Address;
460  }
461  else
462  {
463  ossMac << "-m" << m_tapMac;
464  }
465 
466  std::ostringstream ossNetmask;
468  {
469  ossNetmask << "-n" << ipv4Mask;
470  }
471  else
472  {
473  ossNetmask << "-n" << m_tapNetmask;
474  }
475 
476  std::ostringstream ossMode;
477  ossMode << "-o";
478  if (m_mode == CONFIGURE_LOCAL)
479  {
480  ossMode << "1";
481  }
482  else if (m_mode == USE_LOCAL)
483  {
484  ossMode << "2";
485  }
486  else
487  {
488  ossMode << "3";
489  }
490 
491  std::ostringstream ossPath;
492  ossPath << "-p" << path;
493  //
494  // Execute the socket creation process image.
495  //
496  status = ::execlp (TAP_CREATOR,
497  TAP_CREATOR, // argv[0] (filename)
498  ossDeviceName.str ().c_str (), // argv[1] (-d<device name>)
499  ossGateway.str ().c_str (), // argv[2] (-g<gateway>)
500  ossIp.str ().c_str (), // argv[3] (-i<IP address>)
501  ossMac.str ().c_str (), // argv[4] (-m<MAC address>)
502  ossNetmask.str ().c_str (), // argv[5] (-n<net mask>)
503  ossMode.str ().c_str (), // argv[6] (-o<operating mode>)
504  ossPath.str ().c_str (), // argv[7] (-p<path>)
505  (char *)NULL);
506 
507  //
508  // If the execlp successfully completes, it never returns. If it returns it failed or the OS is
509  // broken. In either case, we bail.
510  //
511  NS_FATAL_ERROR ("TapBridge::CreateTap(): Back from execlp(), status = " << status <<
512  " errno = " << ::strerror (errno));
513  }
514  else
515  {
516  NS_LOG_DEBUG ("Parent process");
517  //
518  // We're the process running the emu net device. We need to wait for the
519  // socket creator process to finish its job.
520  //
521  int st;
522  pid_t waited = waitpid (pid, &st, 0);
523  NS_ABORT_MSG_IF (waited == -1, "TapBridge::CreateTap(): waitpid() fails, errno = " << std::strerror (errno));
524  NS_ASSERT_MSG (pid == waited, "TapBridge::CreateTap(): pid mismatch");
525 
526  //
527  // Check to see if the socket creator exited normally and then take a
528  // look at the exit code. If it bailed, so should we. If it didn't
529  // even exit normally, we bail too.
530  //
531  if (WIFEXITED (st))
532  {
533  int exitStatus = WEXITSTATUS (st);
534  NS_ABORT_MSG_IF (exitStatus != 0,
535  "TapBridge::CreateTap(): socket creator exited normally with status " << exitStatus);
536  }
537  else
538  {
539  NS_FATAL_ERROR ("TapBridge::CreateTap(): socket creator exited abnormally");
540  }
541 
542  //
543  // At this point, the socket creator has run successfully and should
544  // have created our tap device, initialized it with the information we
545  // passed and sent it back to the socket address we provided. A socket
546  // (fd) we can use to talk to this tap device should be waiting on the
547  // Unix socket we set up to receive information back from the creator
548  // program. We've got to do a bunch of grunt work to get at it, though.
549  //
550  // The struct iovec below is part of a scatter-gather list. It describes a
551  // buffer. In this case, it describes a buffer (an integer) that will
552  // get the data that comes back from the socket creator process. It will
553  // be a magic number that we use as a consistency/sanity check.
554  //
555  struct iovec iov;
556  uint32_t magic;
557  iov.iov_base = &magic;
558  iov.iov_len = sizeof(magic);
559 
560  //
561  // The CMSG macros you'll see below are used to create and access control
562  // messages (which is another name for ancillary data). The ancillary
563  // data is made up of pairs of struct cmsghdr structures and associated
564  // data arrays.
565  //
566  // First, we're going to allocate a buffer on the stack to receive our
567  // data array (that contains the socket). Sometimes you'll see this called
568  // an "ancillary element" but the msghdr uses the control message termimology
569  // so we call it "control."
570  //
571  size_t msg_size = sizeof(int);
572  char control[CMSG_SPACE (msg_size)];
573 
574  //
575  // There is a msghdr that is used to minimize the number of parameters
576  // passed to recvmsg (which we will use to receive our ancillary data).
577  // This structure uses terminology corresponding to control messages, so
578  // you'll see msg_control, which is the pointer to the ancillary data and
579  // controllen which is the size of the ancillary data array.
580  //
581  // So, initialize the message header that describes the ancillary/control
582  // data we expect to receive and point it to buffer.
583  //
584  struct msghdr msg;
585  msg.msg_name = 0;
586  msg.msg_namelen = 0;
587  msg.msg_iov = &iov;
588  msg.msg_iovlen = 1;
589  msg.msg_control = control;
590  msg.msg_controllen = sizeof (control);
591  msg.msg_flags = 0;
592 
593  //
594  // Now we can actually receive the interesting bits from the tap
595  // creator process. Lots of pain to get four bytes.
596  //
597  ssize_t bytesRead = recvmsg (sock, &msg, 0);
598  NS_ABORT_MSG_IF (bytesRead != sizeof(int), "TapBridge::CreateTap(): Wrong byte count from socket creator");
599 
600  //
601  // There may be a number of message headers/ancillary data arrays coming in.
602  // Let's look for the one with a type SCM_RIGHTS which indicates it's the
603  // one we're interested in.
604  //
605  struct cmsghdr *cmsg;
606  for (cmsg = CMSG_FIRSTHDR (&msg); cmsg != NULL; cmsg = CMSG_NXTHDR (&msg, cmsg))
607  {
608  if (cmsg->cmsg_level == SOL_SOCKET &&
609  cmsg->cmsg_type == SCM_RIGHTS)
610  {
611  //
612  // This is the type of message we want. Check to see if the magic
613  // number is correct and then pull out the socket we care about if
614  // it matches
615  //
616  if (magic == TAP_MAGIC)
617  {
618  NS_LOG_INFO ("Got SCM_RIGHTS with correct magic " << magic);
619  int *rawSocket = (int*)CMSG_DATA (cmsg);
620  NS_LOG_INFO ("Got the socket from the socket creator = " << *rawSocket);
621  m_sock = *rawSocket;
622  break;
623  }
624  else
625  {
626  NS_LOG_INFO ("Got SCM_RIGHTS, but with bad magic " << magic);
627  }
628  }
629  }
630  if (cmsg == NULL)
631  {
632  NS_FATAL_ERROR ("Did not get the raw socket from the socket creator");
633  }
634 
635  if (m_mode == USE_BRIDGE)
636  {
637  //
638  // Set the ns-3 device's mac address to the overlying container's
639  // mac address
640  //
641  struct ifreq s;
642  memset (&s, 0, sizeof(struct ifreq));
643  strncpy (s.ifr_name, m_tapDeviceName.c_str (), IFNAMSIZ - 1);
644 
645  NS_LOG_INFO ("Trying to get MacAddr of " << m_tapDeviceName);
646  int ioctlResult = ioctl (sock, SIOCGIFHWADDR, &s);
647  if (ioctlResult == 0)
648  {
649  Mac48Address learnedMac;
650  learnedMac.CopyFrom ((uint8_t *)s.ifr_hwaddr.sa_data);
651  NS_LOG_INFO ("Learned Tap device MacAddr is " << learnedMac << ": setting ns-3 device to use this address");
652  m_bridgedDevice->SetAddress (learnedMac);
653  m_ns3AddressRewritten = true;
654  }
655 
657  {
658  NS_LOG_INFO ("Cannot get MacAddr of Tap device: " << m_tapDeviceName << " while in USE_LOCAL/USE_BRIDGE mode: " << std::strerror (errno));
659  NS_LOG_INFO ("Underlying ns-3 device will continue to use default address, what can lead to connectivity errors");
660  }
661  }
662  }
663 
664  close (sock);
665 }
666 
667 void
668 TapBridge::ReadCallback (uint8_t *buf, ssize_t len)
669 {
671 
672  NS_ASSERT_MSG (buf != 0, "invalid buf argument");
673  NS_ASSERT_MSG (len > 0, "invalid len argument");
674 
675  //
676  // It's important to remember that we're in a completely different thread
677  // than the simulator is running in. We need to synchronize with that
678  // other thread to get the packet up into ns-3. What we will need to do
679  // is to schedule a method to deal with the packet using the multithreaded
680  // simulator we are most certainly running. However, I just said it -- we
681  // are talking about two threads here, so it is very, very dangerous to do
682  // any kind of reference counting on a shared object. Just don't do it.
683  // So what we're going to do is pass the buffer allocated on the heap
684  // into the ns-3 context thread where it will create the packet.
685  //
686 
687  NS_LOG_INFO ("TapBridge::ReadCallback(): Received packet on node " << m_nodeId);
688  NS_LOG_INFO ("TapBridge::ReadCallback(): Scheduling handler");
690 }
691 
692 void
693 TapBridge::ForwardToBridgedDevice (uint8_t *buf, ssize_t len)
694 {
695  NS_LOG_FUNCTION (buf << len);
696 
697  //
698  // There are three operating modes for the TapBridge
699  //
700  // CONFIGURE_LOCAL means that ns-3 will create and configure a tap device
701  // and we are expected to use it. The tap device and the ns-3 net device
702  // will have the same MAC address by definition. Thus Send and SendFrom
703  // are equivalent in this case. We use Send to allow all ns-3 devices to
704  // participate in this mode.
705  //
706  // USE_LOCAL mode tells us that we have got to USE a pre-created tap device
707  // that will have a different MAC address from the ns-3 net device. We
708  // also enforce the requirement that there will only be one MAC address
709  // bridged on the Linux side so we can use Send (instead of SendFrom) in
710  // the linux to ns-3 direction. Again, all ns-3 devices can participate
711  // in this mode.
712  //
713  // USE_BRIDGE mode tells us that we are logically extending a Linux bridge
714  // on which lies our tap device. In this case there may be many linux
715  // net devices on the other side of the bridge and so we must use SendFrom
716  // to preserve the possibly many source addresses. Thus, ns-3 devices
717  // must support SendFrom in order to be considered for USE_BRIDGE mode.
718  //
719 
720  //
721  // First, create a packet out of the byte buffer we received and free that
722  // buffer.
723  //
724  Ptr<Packet> packet = Create<Packet> (reinterpret_cast<const uint8_t *> (buf), len);
725  std::free (buf);
726  buf = 0;
727 
728  //
729  // Make sure the packet we received is reasonable enough for the rest of the
730  // system to handle and get it ready to be injected directly into an ns-3
731  // device. What should come back is a packet with the Ethernet header
732  // (and possibly an LLC header as well) stripped off.
733  //
734  Address src, dst;
735  uint16_t type;
736 
737  NS_LOG_LOGIC ("Received packet from tap device");
738 
739  Ptr<Packet> p = Filter (packet, &src, &dst, &type);
740  if (p == 0)
741  {
742  NS_LOG_LOGIC ("TapBridge::ForwardToBridgedDevice: Discarding packet as unfit for ns-3 consumption");
743  return;
744  }
745 
746  NS_LOG_LOGIC ("Pkt source is " << src);
747  NS_LOG_LOGIC ("Pkt destination is " << dst);
748  NS_LOG_LOGIC ("Pkt LengthType is " << type);
749  if (m_mode == USE_LOCAL)
750  {
751  //
752  // Packets we are going to forward should not be from a broadcast src
753  //
754  NS_ASSERT_MSG (Mac48Address::ConvertFrom (src) != Mac48Address ("ff:ff:ff:ff:ff:ff"),
755  "TapBridge::ForwardToBridgedDevice: Source addr is broadcast");
756  if (m_ns3AddressRewritten == false)
757  {
758  //
759  // Set the ns-3 device's mac address to the overlying container's
760  // mac address
761  //
762  Mac48Address learnedMac = Mac48Address::ConvertFrom (src);
763  NS_LOG_LOGIC ("Learned MacAddr is " << learnedMac << ": setting ns-3 device to use this address");
764  m_bridgedDevice->SetAddress (Mac48Address::ConvertFrom (learnedMac));
765  m_ns3AddressRewritten = true;
766  }
767  //
768  // If we are operating in USE_LOCAL mode, we may be attached to an ns-3
769  // device that does not support bridging (SupportsSendFrom returns false).
770  // But, since the mac addresses are now aligned, we can call Send()
771  //
772  NS_LOG_LOGIC ("Forwarding packet to ns-3 device via Send()");
773  m_bridgedDevice->Send (packet, dst, type);
774  return;
775  }
776 
777  //
778  // If we are operating in USE_BRIDGE mode, we have the
779  // situation described below:
780  //
781  // Other Device <-bridge-> Tap Device <-bridge-> ns3 device
782  // Mac Addr A Mac Addr B Mac Addr C
783  //
784  // In Linux, "Other Device" and "Tap Device" are bridged together. By this
785  // we mean that a user has sone something in Linux like:
786  //
787  // brctl addbr mybridge
788  // brctl addif other-device
789  // brctl addif tap-device
790  //
791  // In USE_BRIDGE mode, we want to logically extend this Linux behavior to the
792  // simulated ns3 device and make it appear as if it is connected to the Linux
793  // subnet. As you may expect, this means that we need to act like a real
794  // Linux bridge and take all packets that come from "Tap Device" and ask
795  // "ns3 Device" to send them down its directly connected network. Just like
796  // in a normal everyday bridge we need to call SendFrom in order to preserve
797  //the original packet's from address.
798  //
799  // If we are operating in CONFIGURE_LOCAL mode, we simply simply take all packets
800  // that come from "Tap Device" and ask "ns3 Device" to send them down its
801  // directly connected network. A normal bridge would need to call SendFrom
802  // in order to preserve the original from address, but in CONFIGURE_LOCAL mode
803  // the tap device and the ns-3 device have the same MAC address by definition so
804  // we can call Send.
805  //
806  NS_LOG_LOGIC ("Forwarding packet");
807 
808  if (m_mode == USE_BRIDGE)
809  {
810  m_bridgedDevice->SendFrom (packet, src, dst, type);
811  }
812  else
813  {
814  NS_ASSERT_MSG (m_mode == CONFIGURE_LOCAL, "TapBridge::ForwardToBridgedDevice(): Internal error");
815  m_bridgedDevice->Send (packet, dst, type);
816  }
817 }
818 
820 TapBridge::Filter (Ptr<Packet> p, Address *src, Address *dst, uint16_t *type)
821 {
822  NS_LOG_FUNCTION (p);
823  uint32_t pktSize;
824 
825  //
826  // We have a candidate packet for injection into ns-3. We expect that since
827  // it came over a socket that provides Ethernet packets, it should be big
828  // enough to hold an EthernetHeader. If it can't, we signify the packet
829  // should be filtered out by returning 0.
830  //
831  pktSize = p->GetSize ();
832  EthernetHeader header (false);
833  if (pktSize < header.GetSerializedSize ())
834  {
835  return 0;
836  }
837 
838  uint32_t headerSize = p->PeekHeader (header);
839  p->RemoveAtStart (headerSize);
840 
841  NS_LOG_LOGIC ("Pkt source is " << header.GetSource ());
842  NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ());
843  NS_LOG_LOGIC ("Pkt LengthType is " << header.GetLengthType ());
844 
845  //
846  // If the length/type is less than 1500, it corresponds to a length
847  // interpretation packet. In this case, it is an 802.3 packet and
848  // will also have an 802.2 LLC header. If greater than 1500, we
849  // find the protocol number (Ethernet type) directly.
850  //
851  if (header.GetLengthType () <= 1500)
852  {
853  *src = header.GetSource ();
854  *dst = header.GetDestination ();
855 
856  pktSize = p->GetSize ();
857  LlcSnapHeader llc;
858  if (pktSize < llc.GetSerializedSize ())
859  {
860  return 0;
861  }
862 
863  p->RemoveHeader (llc);
864  *type = llc.GetType ();
865  }
866  else
867  {
868  *src = header.GetSource ();
869  *dst = header.GetDestination ();
870  *type = header.GetLengthType ();
871  }
872 
873  //
874  // What we give back is a packet without the Ethernet header (nor the
875  // possible llc/snap header) on it. We think it is ready to send on
876  // out the bridged net device.
877  //
878  return p;
879 }
880 
883 {
885  return m_bridgedDevice;
886 }
887 
888 void
890 {
891  NS_LOG_FUNCTION (bridgedDevice);
892 
893  NS_ASSERT_MSG (m_node != 0, "TapBridge::SetBridgedDevice: Bridge not installed in a node");
894  NS_ASSERT_MSG (bridgedDevice != this, "TapBridge::SetBridgedDevice: Cannot bridge to self");
895  NS_ASSERT_MSG (m_bridgedDevice == 0, "TapBridge::SetBridgedDevice: Already bridged");
896 
897  if (!Mac48Address::IsMatchingType (bridgedDevice->GetAddress ()))
898  {
899  NS_FATAL_ERROR ("TapBridge::SetBridgedDevice: Device does not support eui 48 addresses: cannot be added to bridge.");
900  }
901 
902  if (m_mode == USE_BRIDGE && !bridgedDevice->SupportsSendFrom ())
903  {
904  NS_FATAL_ERROR ("TapBridge::SetBridgedDevice: Device does not support SendFrom: cannot be added to bridge.");
905  }
906 
907  //
908  // We need to disconnect the bridged device from the internet stack on our
909  // node to ensure that only one stack responds to packets inbound over the
910  // bridged device. That one stack lives outside ns-3 so we just blatantly
911  // steal the device callbacks.
912  //
913  // N.B This can be undone if someone does a RegisterProtocolHandler later
914  // on this node.
915  //
916  bridgedDevice->SetReceiveCallback (MakeCallback (&TapBridge::DiscardFromBridgedDevice, this));
917  bridgedDevice->SetPromiscReceiveCallback (MakeCallback (&TapBridge::ReceiveFromBridgedDevice, this));
918  m_bridgedDevice = bridgedDevice;
919 }
920 
921 bool
922 TapBridge::DiscardFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, const Address &src)
923 {
924  NS_LOG_FUNCTION (device << packet << protocol << src);
925  NS_LOG_LOGIC ("Discarding packet stolen from bridged device " << device);
926  return true;
927 }
928 
929 bool
931  Ptr<NetDevice> device,
932  Ptr<const Packet> packet,
933  uint16_t protocol,
934  const Address &src,
935  const Address &dst,
936  PacketType packetType)
937 {
938  NS_LOG_FUNCTION (device << packet << protocol << src << dst << packetType);
939  NS_ASSERT_MSG (device == m_bridgedDevice, "TapBridge::SetBridgedDevice: Received packet from unexpected device");
940  NS_LOG_DEBUG ("Packet UID is " << packet->GetUid ());
941 
942  //
943  // There are three operating modes for the TapBridge
944  //
945  // CONFIGURE_LOCAL means that ns-3 will create and configure a tap device
946  // and we are expected to use it. The tap device and the ns-3 net device
947  // will have the same MAC address by definition.
948  //
949  // USE_LOCAL mode tells us that we have got to USE a pre-created tap device
950  // that will have a different MAC address from the ns-3 net device. In this
951  // case we will be spoofing the MAC address of a received packet to match
952  // the single allowed address on the Linux side.
953  //
954  // USE_BRIDGE mode tells us that we are logically extending a Linux bridge
955  // on which lies our tap device.
956  //
957 
958  if (m_mode == CONFIGURE_LOCAL && packetType == PACKET_OTHERHOST)
959  {
960  //
961  // We hooked the promiscuous mode protocol handler so we could get the
962  // destination address of the actual packet. This means we will be
963  // getting PACKET_OTHERHOST packets (not broadcast, not multicast, not
964  // unicast to the ns-3 net device, but to some other address). In
965  // CONFIGURE_LOCAL mode we are not interested in these packets since they
966  // don't refer to the single MAC address shared by the ns-3 device and
967  // the TAP device. If, however, we are in USE_LOCAL or USE_BRIDGE mode,
968  // we want to act like a bridge and forward these PACKET_OTHERHOST
969  // packets.
970  //
971  return true;
972  }
973 
976 
977  Ptr<Packet> p = packet->Copy ();
978  EthernetHeader header = EthernetHeader (false);
979  header.SetSource (from);
980  header.SetDestination (to);
981 
982  header.SetLengthType (protocol);
983  p->AddHeader (header);
984 
985  NS_LOG_LOGIC ("Writing packet to Linux host");
986  NS_LOG_LOGIC ("Pkt source is " << header.GetSource ());
987  NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ());
988  NS_LOG_LOGIC ("Pkt LengthType is " << header.GetLengthType ());
989  NS_LOG_LOGIC ("Pkt size is " << p->GetSize ());
990 
991  NS_ASSERT_MSG (p->GetSize () <= 65536, "TapBridge::ReceiveFromBridgedDevice: Packet too big " << p->GetSize ());
992  p->CopyData (m_packetBuffer, p->GetSize ());
993 
994  uint32_t bytesWritten = write (m_sock, m_packetBuffer, p->GetSize ());
995  NS_ABORT_MSG_IF (bytesWritten != p->GetSize (), "TapBridge::ReceiveFromBridgedDevice(): Write error.");
996 
997  NS_LOG_LOGIC ("End of receive packet handling on node " << m_node->GetId ());
998  return true;
999 }
1000 
1001 void
1002 TapBridge::SetIfIndex (const uint32_t index)
1003 {
1005  m_ifIndex = index;
1006 }
1007 
1008 uint32_t
1010 {
1012  return m_ifIndex;
1013 }
1014 
1015 Ptr<Channel>
1017 {
1019  return 0;
1020 }
1021 
1022 void
1024 {
1027 }
1028 
1029 Address
1031 {
1033  return m_address;
1034 }
1035 
1036 void
1038 {
1039  NS_LOG_FUNCTION (mode);
1040  m_mode = mode;
1041 }
1042 
1045 {
1047  return m_mode;
1048 }
1049 
1050 bool
1051 TapBridge::SetMtu (const uint16_t mtu)
1052 {
1054  m_mtu = mtu;
1055  return true;
1056 }
1057 
1058 uint16_t
1059 TapBridge::GetMtu (void) const
1060 {
1062  return m_mtu;
1063 }
1064 
1065 void
1067 {
1069  if (!m_linkUp)
1070  {
1071  m_linkUp = true;
1073  }
1074 }
1075 
1076 bool
1078 {
1080  return m_linkUp;
1081 }
1082 
1083 void
1085 {
1088 }
1089 
1090 bool
1092 {
1094  return true;
1095 }
1096 
1097 Address
1099 {
1101  return Mac48Address ("ff:ff:ff:ff:ff:ff");
1102 }
1103 
1104 bool
1106 {
1108  return true;
1109 }
1110 
1111 Address
1112 TapBridge::GetMulticast (Ipv4Address multicastGroup) const
1113 {
1114  NS_LOG_FUNCTION (this << multicastGroup);
1115  Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
1116  return multicast;
1117 }
1118 
1119 bool
1121 {
1123  return false;
1124 }
1125 
1126 bool
1128 {
1130  //
1131  // Returning false from IsBridge in a device called TapBridge may seem odd
1132  // at first glance, but this test is for a device that bridges ns-3 devices
1133  // together. The Tap bridge doesn't do that -- it bridges an ns-3 device to
1134  // a Linux device. This is a completely different story.
1135  //
1136  return false;
1137 }
1138 
1139 bool
1140 TapBridge::Send (Ptr<Packet> packet, const Address& dst, uint16_t protocol)
1141 {
1142  NS_LOG_FUNCTION (packet << dst << protocol);
1143  NS_FATAL_ERROR ("TapBridge::Send: You may not call Send on a TapBridge directly");
1144  return false;
1145 }
1146 
1147 bool
1148 TapBridge::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dst, uint16_t protocol)
1149 {
1150  NS_LOG_FUNCTION (packet << src << dst << protocol);
1151  NS_FATAL_ERROR ("TapBridge::Send: You may not call SendFrom on a TapBridge directly");
1152  return false;
1153 }
1154 
1155 Ptr<Node>
1157 {
1159  return m_node;
1160 }
1161 
1162 void
1164 {
1166  m_node = node;
1167 }
1168 
1169 bool
1171 {
1173  return true;
1174 }
1175 
1176 void
1178 {
1180  m_rxCallback = cb;
1181 }
1182 
1183 void
1185 {
1187  m_promiscRxCallback = cb;
1188 }
1189 
1190 bool
1192 {
1194  return true;
1195 }
1196 
1198 {
1199  NS_LOG_FUNCTION (this << addr);
1200  return Mac48Address::GetMulticast (addr);
1201 }
1202 
1203 } // namespace ns3
virtual bool NeedsArp(void) const
Definition: tap-bridge.cc:1170
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
Time m_tStop
Time to start tearing down the device.
Definition: tap-bridge.h:393
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
static bool IsMatchingType(const Address &address)
EventId m_startEvent
The ID of the ns-3 event used to schedule the start up of the underlying host Tap device and ns-3 rea...
Definition: tap-bridge.h:357
Ipv4Address m_tapIp
The IP address to use as the device IP on the host.
Definition: tap-bridge.h:409
virtual Ptr< Node > GetNode(void) const
Definition: tap-bridge.cc:1156
virtual void DoDispose(void)
Call out to a separate process running as suid root in order to get our tap device created...
Definition: tap-bridge.cc:166
static Ipv4Mask GetOnes(void)
bool IsBroadcast(void) const
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
virtual void SetNode(Ptr< Node > node)
Definition: tap-bridge.cc:1163
A structure representing data read.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Packet addressed to someone else.
Definition: net-device.h:304
Mode m_mode
The operating mode of the bridge.
Definition: tap-bridge.h:375
Ptr< const AttributeAccessor > MakeIpv4MaskAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: ipv4-address.h:330
uint32_t GetId(void) const
Definition: node.cc:107
bool IsBroadcast(void) const
void SetBridgedNetDevice(Ptr< NetDevice > bridgedDevice)
Set the ns-3 net device to bridge.
Definition: tap-bridge.cc:889
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
void SetMode(TapBridge::Mode mode)
Set the operating mode of this device.
Definition: tap-bridge.cc:1037
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Hold variables of type string.
Definition: string.h:41
virtual bool SetMtu(const uint16_t mtu)
Definition: tap-bridge.cc:1051
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
ns-3 uses a pre-created tap, without configuring it
Definition: tap-bridge.h:123
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: enum.h:209
#define TAP_MAGIC
Definition: tap-bridge.cc:74
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Definition: tap-bridge.cc:1127
uint32_t m_nodeId
a copy of the node id so the read thread doesn&#39;t have to GetNode() in in order to find the node ID...
Definition: tap-bridge.h:444
virtual uint32_t GetSerializedSize(void) const
void StartTapDevice(void)
Spin up the device.
Definition: tap-bridge.cc:196
void Start(Time tStart)
Set a start time for the device.
Definition: tap-bridge.cc:173
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
EventId m_stopEvent
The ID of the ns-3 event used to schedule the tear down of the underlying host Tap device and ns-3 re...
Definition: tap-bridge.h:363
Mac48Address m_tapMac
The MAC address to use as the hardware address on the host; only used in UseLocal mode...
Definition: tap-bridge.h:417
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
Ptr< const AttributeChecker > MakeMac48AddressChecker(void)
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:346
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
bool DiscardFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &src)
Receives a packet from a bridged Device.
Definition: tap-bridge.cc:922
virtual ~TapBridge()
Definition: tap-bridge.cc:153
void Stop(Time tStop)
Set a stop time for the device.
Definition: tap-bridge.cc:185
virtual bool SupportsSendFrom() const
Definition: tap-bridge.cc:1191
void SetSource(Mac48Address source)
Ptr< const AttributeAccessor > MakeMac48AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
static TypeId GetTypeId(void)
Get the type ID.
Definition: tap-bridge.cc:79
Mac48Address GetSource(void) const
a polymophic address class
Definition: address.h:90
bool m_linkUp
Flag indicating whether or not the link is up.
Definition: tap-bridge.h:451
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
void CreateTap(void)
Call out to a separate process running as suid root in order to get our tap device created...
Definition: tap-bridge.cc:256
std::string TapBufferToString(uint8_t *buffer, uint32_t len)
Convert a byte buffer to a string containing a hex representation of the buffer.
uint8_t * m_packetBuffer
A 64K buffer to hold packet data while it is being sent.
Definition: tap-bridge.h:437
virtual void AddLinkChangeCallback(Callback< void > callback)
Definition: tap-bridge.cc:1084
virtual uint32_t GetSerializedSize(void) const
virtual void SetIfIndex(const uint32_t index)
Definition: tap-bridge.cc:1002
virtual uint32_t GetIfIndex(void) const
Definition: tap-bridge.cc:1009
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:362
Hold variables of type enum.
Definition: enum.h:54
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
bool IsEqual(Ipv4Mask other) const
uint16_t GetLengthType(void) const
static Mac48Address GetMulticast(Ipv4Address address)
AttributeValue implementation for Time.
Definition: nstime.h:1076
virtual bool IsMulticast(void) const
Definition: tap-bridge.cc:1105
uint16_t GetType(void)
Return the Ethertype.
int m_fd
The file descriptor to read from.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1140
Ptr< const AttributeChecker > MakeIpv4AddressChecker(void)
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Definition: tap-bridge.cc:1120
Hold an unsigned integer type.
Definition: uinteger.h:44
A bridge to make it appear that a real host process is connected to an ns-3 net device.
Definition: tap-bridge.h:107
Ptr< NetDevice > GetBridgedNetDevice(void)
Get the bridged net device.
Definition: tap-bridge.cc:882
Mac48Address GetDestination(void) const
Mode
Enumeration of the operating modes supported in the class.
Definition: tap-bridge.h:120
FdReader::Data DoRead(void)
The read implementation.
Definition: tap-bridge.cc:53
bool ReceiveFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType)
Receives a packet from a bridged Device.
Definition: tap-bridge.cc:930
uint32_t m_ifIndex
The ns-3 interface index of this TapBridge net device.
Definition: tap-bridge.h:340
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1148
void StopTapDevice(void)
Tear down the device.
Definition: tap-bridge.cc:238
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
Definition: tap-bridge.h:456
Ptr< NetDevice > m_bridgedDevice
The ns-3 net device to which we are bridging.
Definition: tap-bridge.h:427
virtual void SetAddress(Address address)
Set the address of this interface.
Definition: tap-bridge.cc:1023
static Mac48Address ConvertFrom(const Address &address)
void ReadCallback(uint8_t *buf, ssize_t len)
Callback to process packets that are read.
Definition: tap-bridge.cc:668
ns-3 creates and configures tap device
Definition: tap-bridge.h:122
virtual bool IsLinkUp(void) const
Definition: tap-bridge.cc:1077
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
std::string m_tapDeviceName
The name of the device to create on the host.
Definition: tap-bridge.h:399
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< Node > m_node
Pointer to the (ghost) Node to which we are connected.
Definition: tap-bridge.h:334
address
Definition: first.py:37
Ptr< TapBridgeFdReader > m_fdReader
Includes the ns-3 read thread used to do blocking reads on the fd corresponding to the host device...
Definition: tap-bridge.h:369
ns-3 uses a pre-created tap, and bridges to a bridging net device
Definition: tap-bridge.h:124
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Definition: tap-bridge.cc:1112
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
Time m_tStart
Time to start spinning up the device.
Definition: tap-bridge.h:388
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Callback used to hook the promiscuous packet receive callback of the TapBridge ns-3 net device...
Definition: tap-bridge.h:329
virtual bool IsBroadcast(void) const
Definition: tap-bridge.cc:1091
Ipv4Address m_tapGateway
The IP address to use as the device default gateway on the host.
Definition: tap-bridge.h:404
an EUI-48 address
Definition: mac48-address.h:43
bool m_ns3AddressRewritten
Whether the MAC address of the underlying ns-3 device has already been rewritten is stored in this va...
Definition: tap-bridge.h:432
Packet header for Ethernet.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Definition: tap-bridge.cc:1177
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Definition: tap-bridge.cc:1184
AttributeValue implementation for Ipv4Address.
Definition: ipv4-address.h:329
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:1077
void CopyFrom(const uint8_t buffer[6])
virtual Address GetBroadcast(void) const
Definition: tap-bridge.cc:1098
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Mac48Address m_address
The (unused) MAC address of the TapBridge net device.
Definition: tap-bridge.h:383
virtual uint16_t GetMtu(void) const
Definition: tap-bridge.cc:1059
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.cc:184
Describes an IPv6 address.
Definition: ipv6-address.h:49
Ptr< Packet > Filter(Ptr< Packet > packet, Address *src, Address *dst, uint16_t *type)
The host we are bridged to is in the evil real world.
Definition: tap-bridge.cc:820
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1475
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
TapBridge::Mode GetMode(void)
Get the operating mode of this device.
Definition: tap-bridge.cc:1044
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
Network layer to device interface.
Definition: net-device.h:95
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:262
AttributeValue implementation for Mac48Address.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
virtual Ptr< Channel > GetChannel(void) const
Definition: tap-bridge.cc:1016
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
AttributeValue implementation for Ipv4Mask.
Definition: ipv4-address.h:330
void SetLengthType(uint16_t size)
Ptr< const AttributeChecker > MakeIpv4MaskChecker(void)
Ipv4Mask m_tapNetmask
The network mask to assign to the device created on the host.
Definition: tap-bridge.h:422
void NotifyLinkUp(void)
Notifies that the link is up and ready.
Definition: tap-bridge.cc:1066
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:296
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: string.h:42
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: ipv4-address.h:329
uint16_t m_mtu
The common mtu to use for the net devices.
Definition: tap-bridge.h:345
void SetDestination(Mac48Address destination)
int m_sock
The socket (actually interpreted as fd) to use to talk to the Tap device on the real internet host...
Definition: tap-bridge.h:351
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
void ForwardToBridgedDevice(uint8_t *buf, ssize_t len)
Forward a packet received from the tap device to the bridged ns-3 device.
Definition: tap-bridge.cc:693
a unique identifier for an interface.
Definition: type-id.h:58
NetDevice::ReceiveCallback m_rxCallback
Callback used to hook the standard packet receive callback of the TapBridge ns-3 net device...
Definition: tap-bridge.h:318
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
virtual Address GetAddress(void) const
Definition: tap-bridge.cc:1030
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
Header for the LLC/SNAP encapsulation.
EventImpl * MakeEvent(void(*f)(void))
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:34