📄 cmu-trace.cc
字号:
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- *//* * Copyright (c) 1997 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the Computer Systems * Engineering Group at Lawrence Berkeley Laboratory. * 4. Neither the name of the University nor of the Laboratory may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Ported from CMU/Monarch's code, appropriate copyright applies. * nov'98 -Padma. * * $Header: /cvsroot/nsnam/ns-2/trace/cmu-trace.cc,v 1.89 2006/03/21 22:31:32 liyuan Exp $ */#include <packet.h>#include <ip.h>#include <tcp.h>#include <sctp.h>#include <rtp.h>#include <arp.h>#include <dsr/hdr_sr.h> // DSR#include <mac.h>#include <mac-802_11.h>#include <smac.h>#include <address.h>#include <tora/tora_packet.h> //TORA#include <imep/imep_spec.h> // IMEP#include <aodv/aodv_packet.h> //AODV#include <cmu-trace.h>#include <mobilenode.h>#include <simulator.h>//<zheng: add for 802.15.4>#include "wpan/p802_15_4pkt.h"#include "wpan/p802_15_4trace.h"#include "wpan/p802_15_4nam.h"//</zheng: add for 802.15.4>#include "diffusion/diff_header.h" // DIFFUSION -- Chalermek//#define LOG_POSITION//extern char* pt_names[];static class CMUTraceClass : public TclClass {public: CMUTraceClass() : TclClass("CMUTrace") { } TclObject* create(int, const char*const* argv) { return (new CMUTrace(argv[4], *argv[5])); }} cmutrace_class;//<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>double CMUTrace::bradius = 0.0;double CMUTrace::radius_scaling_factor_ = 0.0;double CMUTrace::duration_scaling_factor_ = 0.0;//</zheng>CMUTrace::CMUTrace(const char *s, char t) : Trace(t){ bzero(tracename, sizeof(tracename)); strncpy(tracename, s, MAX_ID_LEN); if(strcmp(tracename, "RTR") == 0) { tracetype = TR_ROUTER; } else if(strcmp(tracename, "TRP") == 0) { tracetype = TR_ROUTER; } else if(strcmp(tracename, "MAC") == 0) { tracetype = TR_MAC; } else if(strcmp(tracename, "IFQ") == 0) { tracetype = TR_IFQ; } else if(strcmp(tracename, "AGT") == 0) { tracetype = TR_AGENT; } else { fprintf(stderr, "CMU Trace Initialized with invalid type\n"); exit(1); }// change wrt Mike's code// assert(type_ == DROP || type_ == SEND || type_ == RECV); assert(type_ == DROP || type_ == SEND || type_ == RECV || ((type_ == EOT) && (tracetype == TR_MAC))); newtrace_ = 0; for (int i=0 ; i < MAX_NODE ; i++) nodeColor[i] = 3 ; node_ = 0;}voidCMUTrace::format_mac_common(Packet *p, const char *why, int offset){ struct hdr_cmn *ch = HDR_CMN(p); struct hdr_ip *ih = HDR_IP(p); struct hdr_mac802_11 *mh; struct hdr_smac *sh; char mactype[SMALL_LEN]; strcpy(mactype, Simulator::instance().macType()); if (strcmp (mactype, "Mac/SMAC") == 0) sh = HDR_SMAC(p); else mh = HDR_MAC802_11(p); double x = 0.0, y = 0.0, z = 0.0; char op = (char) type_; Node* thisnode = Node::get_node_by_address(src_); double energy = -1; if (thisnode) { if (thisnode->energy_model()) { energy = thisnode->energy_model()->energy(); } } // hack the IP address to convert pkt format to hostid format // for now until port ids are removed from IP address. -Padma. int src = Address::instance().get_nodeaddr(ih->saddr()); if(tracetype == TR_ROUTER && type_ == SEND) { if(src_ != src) op = FWRD; } // use tagged format if appropriate if (pt_->tagged()) { int next_hop = -1 ; Node* nextnode = Node::get_node_by_address(ch->next_hop_); if (nextnode) next_hop = nextnode->nodeid(); node_->getLoc(&x, &y, &z); if (op == DROP) op = 'd'; if (op == SEND) op = '+'; if (op == FWRD) op = 'h'; sprintf(pt_->buffer() + offset, "%c "TIME_FORMAT" -s %d -d %d -p %s -k %3s -i %d " "-N:loc {%.2f %.2f %.2f} -N:en %f ", op, // event type Scheduler::instance().clock(), // time src_, // this node next_hop, // next hop packet_info.name(ch->ptype()), // packet type tracename, // trace level ch->uid(), // event id x, y, z, // location energy); // energy offset = strlen(pt_->buffer()); if (strcmp (mactype, "Mac/SMAC") == 0) { format_smac(p, offset); } else { format_mac(p, offset); } return; } // Use new ns trace format to replace the old cmu trace format) if (newtrace_) { node_->getLoc(&x, &y, &z); // consistence if ( op == DROP ) { op = 'd';} // basic trace infomation + basic exenstion sprintf(pt_->buffer() + offset, "%c -t %.9f -Hs %d -Hd %d -Ni %d -Nx %.2f -Ny %.2f -Nz %.2f -Ne %f -Nl %3s -Nw %s ", op, // event type Scheduler::instance().clock(), // time src_, // this node ch->next_hop_, // next hop src_, // this node x, // x coordinate y, // y coordinate z, // z coordinate energy, // energy, -1 = not existing tracename, // trace level why); // reason // mac layer extension offset = strlen(pt_->buffer()); if (strcmp(mactype, "Mac/SMAC") == 0) { format_smac(p, offset); } else { format_mac(p, offset); } return; }#ifdef LOG_POSITION x = 0.0, y = 0.0, z = 0.0; node_->getLoc(&x, &y, &z);#endif sprintf(pt_->buffer() + offset,#ifdef LOG_POSITION "%c %.9f %d (%6.2f %6.2f) %3s %4s %d %s %d ",#else "%c %.9f _%d_ %3s %4s %d %s %d",#endif op, Scheduler::instance().clock(), src_, // this node#ifdef LOG_POSITION x, y,#endif tracename, why, ch->uid(), // identifier for this event ((ch->ptype() == PT_MAC) ? ( (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS" : (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS" : (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK" : //<zheng: add for 802.15.4> (mh->dh_fc.fc_subtype == MAC_Subtype_Beacon) ? "BCN" : //Beacon (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoReq) ? "CM1" : //CMD: Association request (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoRsp) ? "CM2" : //CMD: Association response (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DAssNtf) ? "CM3" : //CMD: Disassociation notification (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DataReq) ? "CM4" : //CMD: Data request (mh->dh_fc.fc_subtype == MAC_Subtype_Command_PIDCNtf) ? "CM5" : //CMD: PAN ID conflict notification (mh->dh_fc.fc_subtype == MAC_Subtype_Command_OrphNtf) ? "CM6" : //CMD: Orphan notification (mh->dh_fc.fc_subtype == MAC_Subtype_Command_BconReq) ? "CM7" : //CMD: Beacon request (mh->dh_fc.fc_subtype == MAC_Subtype_Command_CoorRea) ? "CM8" : //CMD: Coordinator realignment (mh->dh_fc.fc_subtype == MAC_Subtype_Command_GTSReq) ? "CM9" : //CMD: GTS request //</zheng: add for 802.15.4> "UNKN") : (ch->ptype() == PT_SMAC) ? ( (sh->type == RTS_PKT) ? "RTS" : (sh->type == CTS_PKT) ? "CTS" : (sh->type == ACK_PKT) ? "ACK" : (sh->type == SYNC_PKT) ? "SYNC" : "UNKN") : packet_info.name(ch->ptype())), ch->size()); offset = strlen(pt_->buffer()); if (strncmp (mactype, "Mac/SMAC", 8) == 0) { format_smac(p, offset); } else { format_mac(p, offset); } offset = strlen(pt_->buffer()); if (thisnode) { if (thisnode->energy_model()) { // log detailed energy consumption // total energy and breakdown in idle, sleep, transmit and receive modes sprintf(pt_->buffer() + offset, "[energy %f ei %.3f es %.3f et %.3f er %.3f] ", thisnode->energy_model()->energy(), thisnode->energy_model()->ei(), thisnode->energy_model()->es(), thisnode->energy_model()->et(), thisnode->energy_model()->er()); } }}voidCMUTrace::format_mac(Packet *p, int offset){ struct hdr_mac802_11 *mh = HDR_MAC802_11(p); if (pt_->tagged()) { sprintf(pt_->buffer() + offset, "-M:dur %x -M:s %x -M:d %x -M:t %x ", mh->dh_duration, // MAC: duration // change wrt Mike's code //ETHER_ADDR(mh->dh_da), // MAC: source //ETHER_ADDR(mh->dh_sa), // MAC: destination ETHER_ADDR(mh->dh_ra), // MAC: source ETHER_ADDR(mh->dh_ta), // MAC: destination GET_ETHER_TYPE(mh->dh_body)); // MAC: type } else if (newtrace_) { sprintf(pt_->buffer() + offset, "-Ma %x -Md %x -Ms %x -Mt %x ", mh->dh_duration, // change wrt Mike's code //ETHER_ADDR(mh->dh_da), //ETHER_ADDR(mh->dh_sa), ETHER_ADDR(mh->dh_ra), ETHER_ADDR(mh->dh_ta), GET_ETHER_TYPE(mh->dh_body)); } else { sprintf(pt_->buffer() + offset, " [%x %x %x %x] ", //*((u_int16_t*) &mh->dh_fc), mh->dh_duration, // change wrt Mike's code //ETHER_ADDR(mh->dh_da), //ETHER_ADDR(mh->dh_sa), ETHER_ADDR(mh->dh_ra), ETHER_ADDR(mh->dh_ta), GET_ETHER_TYPE(mh->dh_body)); }}voidCMUTrace::format_smac(Packet *p, int offset){ struct hdr_smac *sh = HDR_SMAC(p); sprintf(pt_->buffer() + offset, " [%.2f %d %d] ", sh->duration, sh->dstAddr, sh->srcAddr);} voidCMUTrace::format_ip(Packet *p, int offset){ struct hdr_cmn *ch = HDR_CMN(p); struct hdr_ip *ih = HDR_IP(p); // hack the IP address to convert pkt format to hostid format // for now until port ids are removed from IP address. -Padma. int src = Address::instance().get_nodeaddr(ih->saddr()); int dst = Address::instance().get_nodeaddr(ih->daddr()); if (pt_->tagged()) { sprintf(pt_->buffer() + offset, "-IP:s %d -IP:sp %d -IP:d %d -IP:dp %d -p %s -e %d " "-c %d -i %d -IP:ttl %d ", src, // packet src ih->sport(), // src port dst, // packet dest ih->dport(), // dst port packet_info.name(ch->ptype()), // packet type ch->size(), // packet size ih->flowid(), // flow id ch->uid(), // unique id ih->ttl_ // ttl ); } else if (newtrace_) { sprintf(pt_->buffer() + offset, "-Is %d.%d -Id %d.%d -It %s -Il %d -If %d -Ii %d -Iv %d ", src, // packet src ih->sport(), // src port dst, // packet dest ih->dport(), // dst port packet_info.name(ch->ptype()), // packet type ch->size(), // packet size ih->flowid(), // flow id ch->uid(), // unique id ih->ttl_); // ttl } else { sprintf(pt_->buffer() + offset, "------- [%d:%d %d:%d %d %d] ", src, ih->sport(), dst, ih->dport(), ih->ttl_, (ch->next_hop_ < 0) ? 0 : ch->next_hop_); }}// Note: HDLC format (format_hdlc()) has moved to satellite tracingvoidCMUTrace::format_arp(Packet *p, int offset){ struct hdr_arp *ah = HDR_ARP(p); if (pt_->tagged()) { sprintf(pt_->buffer() + offset, "-arp:op %s -arp:ms %d -arp:s %d -arp:md %d -arp:d %d ", ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY", ah->arp_sha, ah->arp_spa, ah->arp_tha, ah->arp_tpa); } else if (newtrace_) { sprintf(pt_->buffer() + offset, "-P arp -Po %s -Pms %d -Ps %d -Pmd %d -Pd %d ", ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY", ah->arp_sha, ah->arp_spa, ah->arp_tha, ah->arp_tpa); } else { sprintf(pt_->buffer() + offset, "------- [%s %d/%d %d/%d]", ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY", ah->arp_sha, ah->arp_spa, ah->arp_tha, ah->arp_tpa); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -