1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2007 INRIA |
|
4 * All rights reserved. |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License version 2 as |
|
8 * published by the Free Software Foundation; |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 * |
|
19 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
20 */ |
|
21 #include "ascii-trace.h" |
|
22 |
|
23 #include "ns3/trace-context.h" |
|
24 #include "ns3/trace-root.h" |
|
25 #include "ns3/simulator.h" |
|
26 |
|
27 #include "node.h" |
|
28 #include "ipv4.h" |
|
29 #include "queue.h" |
|
30 #include "llc-snap-header.h" |
|
31 #include "arp-header.h" |
|
32 #include "udp-header.h" |
|
33 #include "ipv4-header.h" |
|
34 #include "node-list.h" |
|
35 |
|
36 namespace ns3 { |
|
37 |
|
38 AsciiTrace::AsciiTrace (std::string filename) |
|
39 { |
|
40 m_os.open (filename.c_str ()); |
|
41 } |
|
42 AsciiTrace::~AsciiTrace () |
|
43 { |
|
44 m_os.close (); |
|
45 } |
|
46 void |
|
47 AsciiTrace::TraceAllQueues (void) |
|
48 { |
|
49 TraceRoot::Connect ("/nodes/*/ipv4/interfaces/*/netdevice/queue/*", |
|
50 MakeCallback (&AsciiTrace::LogDevQueue, this)); |
|
51 } |
|
52 void |
|
53 AsciiTrace::TraceAllNetDeviceRx (void) |
|
54 { |
|
55 TraceRoot::Connect ("/nodes/*/ipv4/interfaces/*/netdevice/rx", |
|
56 MakeCallback (&AsciiTrace::LogDevRx, this)); |
|
57 } |
|
58 |
|
59 void |
|
60 AsciiTrace::PrintType (Packet const &packet) |
|
61 { |
|
62 Packet p = packet; |
|
63 LlcSnapHeader llc; |
|
64 p.RemoveHeader (llc); |
|
65 switch (llc.GetType ()) |
|
66 { |
|
67 case 0x0800: { |
|
68 Ipv4Header ipv4; |
|
69 p.RemoveHeader (ipv4); |
|
70 if (ipv4.GetProtocol () == 17) |
|
71 { |
|
72 UdpHeader udp; |
|
73 p.RemoveHeader (udp); |
|
74 m_os << "udp size=" << p.GetSize (); |
|
75 } |
|
76 } break; |
|
77 case 0x0806: { |
|
78 ArpHeader arp; |
|
79 p.RemoveHeader (arp); |
|
80 m_os << "arp "; |
|
81 if (arp.IsRequest ()) |
|
82 { |
|
83 m_os << "request"; |
|
84 } |
|
85 else |
|
86 { |
|
87 m_os << "reply "; |
|
88 } |
|
89 } break; |
|
90 } |
|
91 } |
|
92 |
|
93 void |
|
94 AsciiTrace::LogDevQueue (TraceContext const &context, Packet const &packet) |
|
95 { |
|
96 enum Queue::TraceType type; |
|
97 context.Get (type); |
|
98 switch (type) |
|
99 { |
|
100 case Queue::ENQUEUE: |
|
101 m_os << "+ "; |
|
102 break; |
|
103 case Queue::DEQUEUE: |
|
104 m_os << "- "; |
|
105 break; |
|
106 case Queue::DROP: |
|
107 m_os << "d "; |
|
108 break; |
|
109 } |
|
110 m_os << Simulator::Now ().GetSeconds () << " "; |
|
111 NodeList::NodeIndex nodeIndex; |
|
112 context.Get (nodeIndex); |
|
113 m_os << "node=" << NodeList::PeekNode (nodeIndex)->GetId () << " "; |
|
114 Ipv4::InterfaceIndex interfaceIndex; |
|
115 context.Get (interfaceIndex); |
|
116 m_os << "interface=" << interfaceIndex << " "; |
|
117 m_os << "pkt-uid=" << packet.GetUid () << " "; |
|
118 PrintType (packet); |
|
119 m_os << std::endl; |
|
120 } |
|
121 void |
|
122 AsciiTrace::LogDevRx (TraceContext const &context, Packet &p) |
|
123 { |
|
124 m_os << "r " << Simulator::Now ().GetSeconds () << " "; |
|
125 NodeList::NodeIndex nodeIndex; |
|
126 context.Get (nodeIndex); |
|
127 m_os << "node=" << NodeList::PeekNode (nodeIndex)->GetId () << " "; |
|
128 Ipv4::InterfaceIndex interfaceIndex; |
|
129 context.Get (interfaceIndex); |
|
130 m_os << "interface=" << interfaceIndex << " "; |
|
131 m_os << "pkt-uid=" << p.GetUid () << " "; |
|
132 PrintType (p); |
|
133 m_os << std::endl; |
|
134 } |
|
135 |
|
136 }//namespace ns3 |
|