diff --git a/SConstruct b/SConstruct index 7a6250d4a..6c0d99dfc 100644 --- a/SConstruct +++ b/SConstruct @@ -122,6 +122,7 @@ common.add_sources([ 'packet.cc', 'tags.cc', 'pcap-writer.cc', + 'trace-writer.cc', 'trace-container.cc', 'variable-tracer-test.cc', 'stream-tracer-test.cc', @@ -137,6 +138,7 @@ common.add_inst_headers([ 'f-variable-tracer.h', 'callback-tracer.h', 'stream-tracer.h', + 'trace-writer.h', 'trace-container.h', 'pcap-writer.h', ]) @@ -155,7 +157,7 @@ node.add_sources ([ 'ipv4-address.cc', 'internet-node.cc', 'net-device.cc', - 'net-device-serial.cc', + 'serial-net-device.cc', 'mac-address.cc', 'ipv4-header.cc', 'udp-header.cc', @@ -199,14 +201,13 @@ node.add_headers ([ 'l3-protocol.h', 'ipv4-l4-demux.h', 'net-device-list.h', - 'net-device-serial.h', + 'serial-net-device.h', 'llc-snap-header.h', 'header-utils.h', 'protocol.h', 'demux.h', 'serial-channel.h', 'queue.h', - 'drop-tail.h' ]) node.add_inst_headers ([ 'node.h', @@ -223,7 +224,8 @@ node.add_inst_headers ([ 'ipv4-route.h', 'serial-channel.h', 'queue.h', - 'net-device-serial.h' + 'drop-tail.h', + 'serial-net-device.h' ]) @@ -290,6 +292,13 @@ ns3.add(sample_test) sample_test.add_dep('core') sample_test.add_source('main-test.cc') +sample_serial_net_device_if = build.Ns3Module ('sample-serial-net-device-if', 'samples') +sample_serial_net_device_if.set_executable () +ns3.add (sample_serial_net_device_if) +sample_serial_net_device_if.add_dep ('common') +sample_serial_net_device_if.add_dep ('node') +sample_serial_net_device_if.add_source ('main-serial-net-device-if.cc') + sample_simple = build.Ns3Module('sample-simple', 'samples') sample_simple.set_executable() ns3.add(sample_simple) diff --git a/samples/main-serial-net-device-if.cc b/samples/main-serial-net-device-if.cc new file mode 100644 index 000000000..3ed5f1c2e --- /dev/null +++ b/samples/main-serial-net-device-if.cc @@ -0,0 +1,189 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 University of Washington + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#if 0 +#include +#include +#endif + +#include "ns3/debug.h" +#include "ns3/internet-node.h" +#include "ns3/mac-address.h" +#include "ns3/packet.h" +#include "ns3/arp-ipv4-interface.h" +#include "ns3/ipv4-address.h" +#include "ns3/serial-channel.h" +#include "ns3/serial-net-device.h" +#include "ns3/trace-writer.h" +#include "ns3/trace-container.h" +#include "ns3/drop-tail.h" +#include "ns3/arp-ipv4-interface.h" +#include "ns3/ipv4.h" + +#include "ns3/pcap-writer.h" + +using namespace ns3; + +class Logger : public TraceWriter{ +public: + Logger () + { + NS3_TRACEALL("**** Logger()") + }; + + Logger (std::string const &filename) + { + NS3_TRACEALL("**** Logger(string const &)") + Open(filename); + }; + + Logger (char const *filename) : m_tracer(filename) + { + NS3_TRACEALL("**** Logger(char const *)") + Open(filename); + }; + + ~Logger () {}; + + void Log (const char *s, const Packet &p) + { + NS3_TRACEALL("**** LogEnque ("<< s << &p << ")") + m_filestr << s << &p << std::endl; + } + +protected: + TraceWriter m_tracer; +}; + +int main (int argc, char *argv[]) +{ + NS3_TRACEALL("Serial Net Device Test") + + TraceContainer traceContainerA; + TraceContainer traceContainerB; + + // create two nodes and a simple SerialChannel + InternetNode a; + InternetNode b; + SerialChannel ch; + + // create two NetDevices and assign one to each node + // Note: this would normally be done also in conjunction with + // creating a Channel + // Here, we do not care about the Device Address (point-to-point) + // but more generally, we would use a subclass such as MacAddress + // as follows: MacAddress addra("00:00:00:00:00:01"); + // so we'll pretend and give them simple MacAddresses here + + MacAddress addra("00:00:00:00:00:01"); + SerialNetDevice neta(&a, addra); + + DropTailQueue dtqa (traceContainerA); + + neta.AddQueue(&dtqa); + neta.SetName("a.eth0"); + + MacAddress addrb("00:00:00:00:00:02"); + SerialNetDevice netb(&b, addrb); + + DropTailQueue dtqb (traceContainerB); + + netb.AddQueue(&dtqb); + netb.SetName("b.eth0"); + + // bind the two NetDevices together by using a simple Channel + // this method changed to do a bidirectional binding + ch.Attach(&neta); + ch.Attach(&netb); + + // Some simple prints to see whether it is working + NS3_TRACEALL("neta.GetMtu() <= " << neta.GetMtu()) + NS3_TRACEALL("netb.GetMtu() <= " << netb.GetMtu()) + NS3_DEBUG (MacAddress addr = neta.GetAddress();) + NS3_TRACEALL("neta.GetAddress() <= " << addr) + NS3_DEBUG (addr = netb.GetAddress();) + NS3_TRACEALL("netb.GetAddress() <= " << addr) + + // Note: InternetNode constructor instantiates multiple Layer-3 + // protocols and registers them with the L3Demux object. + // This takes care of Layer-2 -> Layer-3 bindings. + // XXX TODO: will need to create a dummy IPv4 object for insertion + // into the Demux + + // We now need to bind the InternetNode to the various interfaces. + // to get the Layer-3 -> Layer-2 bindings. + + // We do this by calling an "AddArpIpv4Interface(neta)" function on node a. + // This should: + // i) create an Ipv4ArpInterface object (subclass of pure virtual + // Ipv4Interface object) + // ii) add the Ipv4ArpInterface object to the InternetNode's internal + // vector of Ipv4Interfaces (keyed off of ifIndex) + + NS3_TRACEALL("Adding ARP Interface to InternetNode a") + ArpIpv4Interface* arpipv4interfacep = new ArpIpv4Interface(&a, &neta); + uint32_t indexA = (&a)->GetIpv4 ()->AddInterface (arpipv4interfacep); + NS3_TRACEALL("Adding Interface " << indexA); + + + // iii) give the interface an IP address + + NS3_TRACEALL("Giving IP address to ARP Interface") + arpipv4interfacep->SetAddress(Ipv4Address("10.1.1.1")); + arpipv4interfacep->SetNetworkMask(Ipv4Mask("255.255.255.0")); + + // iv) set the interface's state to "UP" + + NS3_TRACEALL("Setting ARP interface to UP") + arpipv4interfacep->SetUp(); + + NS3_TRACEALL("Adding ARP Interface to InternetNode b") + ArpIpv4Interface* arpipv4interfacepb = new ArpIpv4Interface(&b, &netb); + uint32_t indexB = (&b)->GetIpv4 ()->AddInterface (arpipv4interfacepb); + NS3_TRACEALL("Adding Interface " << indexB); + + + // iii) give the interface an IP address + + NS3_TRACEALL("Giving IP address to ARP Interface") + arpipv4interfacepb->SetAddress(Ipv4Address("10.1.1.2")); + arpipv4interfacepb->SetNetworkMask(Ipv4Mask("255.255.255.0")); + + // iv) set the interface's state to "UP" + + NS3_TRACEALL("Setting ARP interface to UP") + arpipv4interfacepb->SetUp(); + + Logger logger("serial-net-test.log"); + + traceContainerA.SetCallback ("Queue::Enque", + MakeCallback (&Logger::Log, &logger)); + + // create a packet on one node and send it through, reading it + // on the other node + Packet p; + + NS3_TRACEALL("Sending Packet " << &p) + arpipv4interfacep->Send(p, Ipv4Address("10.1.1.2")); + + //neta.Send(p, MacAddress()); // Test that all-zero's MacAddress used + //netb.Send(p, "00:01:02:03:04:05"); // Dummy function call + + return 0; +} diff --git a/src/common/trace-writer.cc b/src/common/trace-writer.cc index 84d94ba6e..0343eafbb 100644 --- a/src/common/trace-writer.cc +++ b/src/common/trace-writer.cc @@ -18,7 +18,7 @@ * * Author: Craig Dowell * - * Thu Feb 8 10:42:52 PST 2007 craigdo: Created + * Thu Feb 8 10:42:52 PST 2007 craigdo: Created from pcap-writer.c */ #include "ns3/debug.h" @@ -27,21 +27,7 @@ namespace ns3 { namespace { - int twDebug = 0; -} - - void -TraceWriter::Init (const char *filename) -{ - NS3_TRACE(twDebug, "TraceWriter()::Init(" << filename << ")") - - std::streambuf *sb = m_filestr.rdbuf(); - rdbuf(sb); - - if (filename) - { - m_filestr.open (filename, std::ios::out | std::ios::app); - } + int twDebug = 1; } TraceWriter::TraceWriter () : @@ -49,15 +35,27 @@ TraceWriter::TraceWriter () : { NS3_TRACE(twDebug, "TraceWriter()::TraceWriter()") - Init (0); + std::streambuf *sb = m_filestr.rdbuf(); + + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter(): rdbuf ()") + rdbuf(sb); + + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter(): done") } -TraceWriter::TraceWriter (std::string const &filename) : + TraceWriter::TraceWriter (std::string const &filename) : m_filestr() { NS3_TRACE(twDebug, "TraceWriter()::TraceWriter (\"" << filename << "\")") - Init (filename.c_str()); + m_filestr.open (filename.c_str(), std::ios::out | std::ios::app); + + std::streambuf *sb = m_filestr.rdbuf(); + + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter(): rdbuf ()") + rdbuf(sb); + + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter(): done") } TraceWriter::TraceWriter (char const *filename) : @@ -65,7 +63,14 @@ TraceWriter::TraceWriter (char const *filename) : { NS3_TRACE(twDebug, "TraceWriter()::TraceWriter (\"" << filename << "\")") - Init (filename); + m_filestr.open (filename, std::ios::out | std::ios::app); + + std::streambuf *sb = m_filestr.rdbuf(); + + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter(): rdbuf ()") + rdbuf(sb); + + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter(): done") } @@ -79,7 +84,7 @@ TraceWriter::Open (std::string const &filename) { NS3_TRACE(twDebug, "TraceWriter()::Open (\"" << filename << "\")") - Init(filename.c_str()); + m_filestr.open (filename.c_str(), std::ios::out | std::ios::app); } void @@ -87,7 +92,7 @@ TraceWriter::Open (char const *filename) { NS3_TRACE(twDebug, "TraceWriter()::Open (\"" << filename << "\")") - Init(filename); + m_filestr.open (filename, std::ios::out | std::ios::app); } void diff --git a/src/node/arp-ipv4-interface.cc b/src/node/arp-ipv4-interface.cc index 391903e7a..087809cea 100644 --- a/src/node/arp-ipv4-interface.cc +++ b/src/node/arp-ipv4-interface.cc @@ -40,6 +40,7 @@ void ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest) { Arp * arp = m_node->GetArp (); + printf("Arp %p\n", arp); MacAddress hardwareDestination; bool found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination); if (found) diff --git a/src/node/arp.cc b/src/node/arp.cc index 8ca5fc670..4bcf03383 100644 --- a/src/node/arp.cc +++ b/src/node/arp.cc @@ -180,6 +180,7 @@ void Arp::SendArpRequest (ArpCache const *cache, Ipv4Address to) { ArpHeader arp; + printf("%p %p %p %p\n", cache->GetDevice(), cache->GetInterface(), cache->GetDevice(), &to); arp.SetRequest (cache->GetDevice ()->GetAddress (), cache->GetInterface ()->GetAddress (), cache->GetDevice ()->GetBroadcast (), diff --git a/src/node/drop-tail.cc b/src/node/drop-tail.cc index 3b87ea5b6..4c8064376 100644 --- a/src/node/drop-tail.cc +++ b/src/node/drop-tail.cc @@ -23,7 +23,7 @@ namespace ns3 { namespace { - int dtqDebug = 0; + int dtqDebug = 1; } DropTailQueue::DropTailQueue () : diff --git a/src/node/queue.cc b/src/node/queue.cc index ec0631ddd..c92d39ec5 100644 --- a/src/node/queue.cc +++ b/src/node/queue.cc @@ -23,7 +23,7 @@ namespace ns3 { namespace { - int qDebug = 0; + int qDebug = 1; } Queue::Queue() : diff --git a/src/node/serial-channel.cc b/src/node/serial-channel.cc index 6e51c8b0b..6da4dcb92 100644 --- a/src/node/serial-channel.cc +++ b/src/node/serial-channel.cc @@ -18,7 +18,7 @@ */ #include "serial-channel.h" -#include "net-device-serial.h" +#include "serial-net-device.h" #include "ns3/packet.h" namespace ns3 { diff --git a/src/node/serial-channel.h b/src/node/serial-channel.h index f0bb04926..b5a7c4378 100644 --- a/src/node/serial-channel.h +++ b/src/node/serial-channel.h @@ -26,7 +26,7 @@ #define CHANNEL_SERIAL_H #include -#include "net-device-serial.h" +#include "serial-net-device.h" #include "ns3/packet.h" namespace ns3 { diff --git a/src/node/net-device-serial.cc b/src/node/serial-net-device.cc similarity index 99% rename from src/node/net-device-serial.cc rename to src/node/serial-net-device.cc index 1ceb3f8c7..0dc05ef89 100644 --- a/src/node/net-device-serial.cc +++ b/src/node/serial-net-device.cc @@ -25,7 +25,7 @@ #include "protocol.h" #include "demux.h" #include "queue.h" -#include "net-device-serial.h" +#include "serial-net-device.h" #include "serial-channel.h" namespace ns3 { diff --git a/src/node/net-device-serial.h b/src/node/serial-net-device.h similarity index 100% rename from src/node/net-device-serial.h rename to src/node/serial-net-device.h