Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

cmu-trace.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *      This product includes software developed by the Computer Systems
00017  *      Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * Ported from CMU/Monarch's code, appropriate copyright applies.
00035  * nov'98 -Padma.
00036  *
00037  * $Header: /nfs/jade/vint/CVSROOT/ns-2/trace/cmu-trace.cc,v 1.72 2003/02/22 03:53:35 buchheim Exp $
00038  */
00039 
00040 #include <packet.h>
00041 #include <ip.h>
00042 #include <tcp.h>
00043 #include <rtp.h>
00044 #include <arp.h>
00045 #include <dsr/hdr_sr.h> // DSR
00046 #include <mac.h>
00047 #include <mac-802_11.h>
00048 #include <address.h>
00049 #include <tora/tora_packet.h> //TORA
00050 #include <imep/imep_spec.h>         // IMEP
00051 #include <aodv/aodv_packet.h> //AODV
00052 #include <cmu-trace.h>
00053 #include <mobilenode.h>
00054 
00055 #include "diffusion/diff_header.h" // DIFFUSION -- Chalermek
00056 
00057 
00058 //#define LOG_POSITION
00059 
00060 //extern char* pt_names[];
00061 
00062 static class CMUTraceClass : public TclClass {
00063 public:
00064         CMUTraceClass() : TclClass("CMUTrace") { }
00065         TclObject* create(int, const char*const* argv) {
00066                 return (new CMUTrace(argv[4], *argv[5]));
00067         }
00068 } cmutrace_class;
00069 
00070 
00071 
00072 double CMUTrace::bradius = 0.0;
00073 double CMUTrace::radius_scaling_factor_ = 0.0;
00074 double CMUTrace::duration_scaling_factor_ = 0.0;
00075 
00076 
00077 CMUTrace::CMUTrace(const char *s, char t) : Trace(t)
00078 {
00079         bzero(tracename, sizeof(tracename));
00080         strncpy(tracename, s, MAX_ID_LEN);
00081 
00082         if(strcmp(tracename, "RTR") == 0) {
00083                 tracetype = TR_ROUTER;
00084         }
00085         else if(strcmp(tracename, "TRP") == 0) {
00086                 tracetype = TR_ROUTER;
00087         }
00088         else if(strcmp(tracename, "MAC") == 0) {
00089                 tracetype = TR_MAC;
00090         }
00091         else if(strcmp(tracename, "IFQ") == 0) {
00092                 tracetype = TR_IFQ;
00093         }
00094         else if(strcmp(tracename, "AGT") == 0) {
00095                 tracetype = TR_AGENT;
00096         }
00097         else {
00098                 fprintf(stderr, "CMU Trace Initialized with invalid type\n");
00099                 exit(1);
00100         }
00101 
00102         assert(type_ == DROP || type_ == SEND || type_ == RECV);
00103 
00104         newtrace_ = 0;
00105         for (int i=0 ; i < MAX_NODE ; i++) 
00106                 nodeColor[i] = 3 ;
00107         node_ = 0;
00108 
00109 }
00110 
00111 void
00112 CMUTrace::format_mac(Packet *p, const char *why, int offset)
00113 {
00114         struct hdr_cmn *ch = HDR_CMN(p);
00115         struct hdr_ip *ih = HDR_IP(p);
00116         struct hdr_mac802_11 *mh = HDR_MAC802_11(p);
00117         double x = 0.0, y = 0.0, z = 0.0;
00118        
00119         char op = (char) type_;
00120         Node* thisnode = Node::get_node_by_address(src_);
00121         double energy = -1;
00122         if (thisnode) {
00123             if (thisnode->energy_model()) {
00124                     energy = thisnode->energy_model()->energy();
00125             }
00126         }
00127 
00128         // hack the IP address to convert pkt format to hostid format
00129         // for now until port ids are removed from IP address. -Padma.
00130 
00131         int src = Address::instance().get_nodeaddr(ih->saddr());
00132 
00133         if(tracetype == TR_ROUTER && type_ == SEND) {
00134                 if(src_ != src)
00135                         op = FWRD;
00136         }
00137 
00138         // use tagged format if appropriate
00139         if (pt_->tagged()) {
00140                 int next_hop = -1 ;
00141                 Node* nextnode = Node::get_node_by_address(ch->next_hop_);
00142                 if (nextnode) next_hop = nextnode->nodeid(); 
00143 
00144                 node_->getLoc(&x, &y, &z);
00145 
00146                 if (op == DROP) op = 'd';
00147                 if (op == SEND) op = '+';
00148                 if (op == FWRD) op = 'h';
00149 
00150                 sprintf(pt_->buffer() + offset,
00151                         "%c "TIME_FORMAT" -s %d -d %d -p %s -k %3s -i %d "
00152                         "-N:loc {%.2f %.2f %.2f} -N:en %f "
00153                         "-M:dur %x -M:s %x -M:d %x -M:t %x ",
00154                         op,                             // event type
00155                         Scheduler::instance().clock(),  // time
00156                         src_,                           // this node
00157                         next_hop,                       // next hop
00158                         packet_info.name(ch->ptype()),  // packet type
00159                         tracename,                      // trace level
00160                         ch->uid(),                      // event id
00161                         x, y, z,                        // location
00162                         energy,                         // energy
00163                         mh->dh_duration,                // MAC: duration
00164                         ETHER_ADDR(mh->dh_da),          // MAC: source
00165                         ETHER_ADDR(mh->dh_sa),          // MAC: destination
00166                         GET_ETHER_TYPE(mh->dh_body)     // MAC: type
00167                         );
00168                 return;
00169         }
00170 
00171 
00172         // Use new ns trace format to replace the old cmu trace format)
00173         if (newtrace_) {
00174             
00175             node_->getLoc(&x, &y, &z);
00176             // consistence
00177             if ( op == DROP ) { op = 'd';}
00178 
00179                 // basic trace infomation + basic exenstion
00180 
00181             sprintf(pt_->buffer() + offset,
00182                    "%c -t %.9f -Hs %d -Hd %d -Ni %d -Nx %.2f -Ny %.2f -Nz %.2f -Ne %f -Nl %3s -Nw %s ",
00183                     op,                       // event type
00184                     Scheduler::instance().clock(),  // time
00185                     src_,                           // this node
00186                     ch->next_hop_,                  // next hop
00187                     src_,                           // this node
00188                     x,                              // x coordinate
00189                     y,                              // y coordinate
00190                     z,                              // z coordinate
00191                     energy,                         // energy, -1 = not existing
00192                     tracename,                      // trace level
00193                     why);                            // reason
00194 
00195             // mac layer extension
00196 
00197             offset = strlen(pt_->buffer());
00198 
00199             sprintf(pt_->buffer() + offset, 
00200                     "-Ma %x -Md %x -Ms %x -Mt %x ",
00201                     mh->dh_duration,
00202                     ETHER_ADDR(mh->dh_da),
00203                     ETHER_ADDR(mh->dh_sa),
00204                     GET_ETHER_TYPE(mh->dh_body));
00205                     
00206              return;
00207         }
00208 
00209 
00210 #ifdef LOG_POSITION
00211         double x = 0.0, y = 0.0, z = 0.0;
00212         node_->getLoc(&x, &y, &z);
00213 #endif
00214 
00215         sprintf(pt_->buffer() + offset,
00216 #ifdef LOG_POSITION
00217                 "%c %.9f %d (%6.2f %6.2f) %3s %4s %d %s %d [%x %x %x %x] ",
00218 #else
00219                 "%c %.9f _%d_ %3s %4s %d %s %d [%x %x %x %x] ",
00220 #endif
00221                 op,
00222                 Scheduler::instance().clock(),
00223                 src_,                           // this node
00224 #ifdef LOG_POSITION
00225                 x,
00226                 y,
00227 #endif
00228                 tracename,
00229                 why,
00230                 
00231                 ch->uid(),                      // identifier for this event
00232                 
00233                 ((ch->ptype() == PT_MAC) ? (
00234                   (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS"  :
00235                   (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS"  :
00236                   (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK"  :
00237                   "UNKN"
00238                   ) : packet_info.name(ch->ptype())),
00239                 
00240                 ch->size(),
00241 
00242                 //*((u_int16_t*) &mh->dh_fc),
00243                 mh->dh_duration,
00244                 ETHER_ADDR(mh->dh_da),
00245                 ETHER_ADDR(mh->dh_sa),
00246                 GET_ETHER_TYPE(mh->dh_body));
00247 
00248         offset = strlen(pt_->buffer());
00249 
00250         if (thisnode) {
00251                 if (thisnode->energy_model()) {
00252                         sprintf(pt_->buffer() + offset,
00253                                 "[energy %f] ",
00254                                 thisnode->energy_model()->energy());
00255                 }
00256         }
00257 }
00258 
00259 void
00260 CMUTrace::format_ip(Packet *p, int offset)
00261 {
00262         struct hdr_cmn *ch = HDR_CMN(p);
00263         struct hdr_ip *ih = HDR_IP(p);
00264         
00265         // hack the IP address to convert pkt format to hostid format
00266         // for now until port ids are removed from IP address. -Padma.
00267         int src = Address::instance().get_nodeaddr(ih->saddr());
00268         int dst = Address::instance().get_nodeaddr(ih->daddr());
00269 
00270         if (pt_->tagged()) {
00271                 sprintf(pt_->buffer() + offset,
00272                         "-IP:s %d -IP:sp %d -IP:d %d -IP:dp %d -p %s -e %d "
00273                         "-c %d -i %d -IP:ttl %d ",
00274                         src,                           // packet src
00275                         ih->sport(),                   // src port
00276                         dst,                           // packet dest
00277                         ih->dport(),                   // dst port
00278                         packet_info.name(ch->ptype()), // packet type
00279                         ch->size(),                    // packet size
00280                         ih->flowid(),                  // flow id
00281                         ch->uid(),                     // unique id
00282                         ih->ttl_                       // ttl
00283                         );
00284         } else if (newtrace_) {
00285             sprintf(pt_->buffer() + offset,
00286                     "-Is %d.%d -Id %d.%d -It %s -Il %d -If %d -Ii %d -Iv %d ",
00287                     src,                           // packet src
00288                     ih->sport(),                   // src port
00289                     dst,                           // packet dest
00290                     ih->dport(),                   // dst port
00291                     packet_info.name(ch->ptype()),  // packet type
00292                     ch->size(),                     // packet size
00293                     ih->flowid(),                   // flow id
00294                     ch->uid(),                      // unique id
00295                     ih->ttl_);                      // ttl
00296         } else {
00297             sprintf(pt_->buffer() + offset, "------- [%d:%d %d:%d %d %d] ",
00298                 src, ih->sport(),
00299                 dst, ih->dport(),
00300                 ih->ttl_, (ch->next_hop_ < 0) ? 0 : ch->next_hop_);
00301         }
00302 }
00303 
00304 void
00305 CMUTrace::format_arp(Packet *p, int offset)
00306 {
00307         struct hdr_arp *ah = HDR_ARP(p);
00308 
00309         if (pt_->tagged()) {
00310             sprintf(pt_->buffer() + offset,
00311                     "-arp:op %s -arp:ms %d -arp:s %d -arp:md %d -arp:d %d ",
00312                     ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
00313                     ah->arp_sha,
00314                     ah->arp_spa,
00315                     ah->arp_tha,
00316                     ah->arp_tpa);
00317         } else if (newtrace_) {
00318             sprintf(pt_->buffer() + offset,
00319                     "-P arp -Po %s -Pms %d -Ps %d -Pmd %d -Pd %d ",
00320                     ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
00321                     ah->arp_sha,
00322                     ah->arp_spa,
00323                     ah->arp_tha,
00324                     ah->arp_tpa);
00325         } else {
00326 
00327             sprintf(pt_->buffer() + offset,
00328                 "------- [%s %d/%d %d/%d]",
00329                 ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
00330                 ah->arp_sha,
00331                 ah->arp_spa,
00332                 ah->arp_tha,
00333                 ah->arp_tpa);
00334         }
00335 }
00336 
00337 void
00338 CMUTrace::format_dsr(Packet *p, int offset)
00339 {
00340         hdr_sr *srh = hdr_sr::access(p);
00341 
00342         if (pt_->tagged()) {
00343             sprintf(pt_->buffer() + offset,
00344                     "-dsr:h %d -dsr:q %d -dsr:s %d -dsr:p %d -dsr:n %d "
00345                     "-dsr:l %d -dsr:e {%d %d} -dsr:w %d -dsr:m %d -dsr:c %d "
00346                     "-dsr:b {%d %d} ",
00347                     srh->num_addrs(),
00348                     srh->route_request(),
00349                     srh->rtreq_seq(),
00350                     srh->route_reply(),
00351                     srh->rtreq_seq(),
00352                     srh->route_reply_len(),
00353                     srh->reply_addrs()[0].addr,
00354                     srh->reply_addrs()[srh->route_reply_len()-1].addr,
00355                     srh->route_error(),
00356                     srh->num_route_errors(),
00357                     srh->down_links()[srh->num_route_errors() - 1].tell_addr,
00358                     srh->down_links()[srh->num_route_errors() - 1].from_addr,
00359                     srh->down_links()[srh->num_route_errors() - 1].to_addr);
00360             return;
00361         } else if (newtrace_) {
00362             sprintf(pt_->buffer() + offset, 
00363                 "-P dsr -Ph %d -Pq %d -Ps %d -Pp %d -Pn %d -Pl %d -Pe %d->%d -Pw %d -Pm %d -Pc %d -Pb %d->%d ",
00364                     srh->num_addrs(),                   // how many nodes travered
00365 
00366                 srh->route_request(),
00367                 srh->rtreq_seq(),
00368 
00369                 srh->route_reply(),
00370                 srh->rtreq_seq(),
00371                 srh->route_reply_len(),
00372                 // the dest of the src route
00373                 srh->reply_addrs()[0].addr,
00374                 srh->reply_addrs()[srh->route_reply_len()-1].addr,
00375 
00376                 srh->route_error(),
00377                 srh->num_route_errors(),
00378                 srh->down_links()[srh->num_route_errors() - 1].tell_addr,
00379                 srh->down_links()[srh->num_route_errors() - 1].from_addr,
00380                 srh->down_links()[srh->num_route_errors() - 1].to_addr);
00381 
00382            return;
00383         }
00384         sprintf(pt_->buffer() + offset, 
00385                 "%d [%d %d] [%d %d %d %d->%d] [%d %d %d %d->%d]",
00386                 srh->num_addrs(),
00387 
00388                 srh->route_request(),
00389                 srh->rtreq_seq(),
00390 
00391                 srh->route_reply(),
00392                 srh->rtreq_seq(),
00393                 srh->route_reply_len(),
00394                 // the dest of the src route
00395                 srh->reply_addrs()[0].addr,
00396                 srh->reply_addrs()[srh->route_reply_len()-1].addr,
00397 
00398                 srh->route_error(),
00399                 srh->num_route_errors(),
00400                 srh->down_links()[srh->num_route_errors() - 1].tell_addr,
00401                 srh->down_links()[srh->num_route_errors() - 1].from_addr,
00402                 srh->down_links()[srh->num_route_errors() - 1].to_addr);
00403 }
00404 
00405 void
00406 CMUTrace::format_msg(Packet *, int)
00407 {
00408 }
00409 
00410 void
00411 CMUTrace::format_tcp(Packet *p, int offset)
00412 {
00413         struct hdr_cmn *ch = HDR_CMN(p);
00414         struct hdr_tcp *th = HDR_TCP(p);
00415         
00416         if (pt_->tagged()) {
00417             sprintf(pt_->buffer() + offset,
00418                     "-tcp:s %d -tcp:a %d -tcp:f %d -tcp:o %d ",
00419                     th->seqno_,
00420                     th->ackno_,
00421                     ch->num_forwards(),
00422                     ch->opt_num_forwards());
00423         } else if (newtrace_) {
00424             sprintf(pt_->buffer() + offset,
00425                 "-Pn tcp -Ps %d -Pa %d -Pf %d -Po %d ",
00426                 th->seqno_,
00427                 th->ackno_,
00428                 ch->num_forwards(),
00429                 ch->opt_num_forwards());
00430 
00431         } else {
00432             sprintf(pt_->buffer() + offset,
00433                 "[%d %d] %d %d",
00434                 th->seqno_,
00435                 th->ackno_,
00436                 ch->num_forwards(),
00437                 ch->opt_num_forwards());
00438         }
00439 }
00440 
00441 void
00442 CMUTrace::format_rtp(Packet *p, int offset)
00443 {
00444         struct hdr_cmn *ch = HDR_CMN(p);
00445         struct hdr_rtp *rh = HDR_RTP(p);
00446         struct hdr_ip *ih = HDR_IP(p);
00447         Node* thisnode = Node::get_node_by_address(src_);
00448 
00449         //hacking, needs to change later, 
00450         int dst = Address::instance().get_nodeaddr(ih->daddr());
00451         
00452         if (dst == src_){
00453                 // I just received a cbr data packet
00454                 if (thisnode->energy_model() && 
00455                     thisnode->energy_model()->powersavingflag()) {
00456                         thisnode->energy_model()->set_node_state(EnergyModel::INROUTE);
00457                 }
00458         }
00459 
00460         if (pt_->tagged()) {
00461                 sprintf(pt_->buffer() + offset,
00462                         "-cbr:s %d -cbr:f %d -cbr:o %d ",
00463                         rh->seqno_,
00464                         ch->num_forwards(),
00465                         ch->opt_num_forwards());
00466         } else if (newtrace_) {
00467                 sprintf(pt_->buffer() + offset,
00468                         "-Pn cbr -Pi %d -Pf %d -Po %d ",
00469                         rh->seqno_,
00470                         ch->num_forwards(),
00471                         ch->opt_num_forwards());
00472         } else {
00473                 sprintf(pt_->buffer() + offset,
00474                         "[%d] %d %d",
00475                         rh->seqno_,
00476                         ch->num_forwards(),
00477                         ch->opt_num_forwards());
00478         }
00479 }
00480 
00481 void
00482 CMUTrace::format_imep(Packet *p, int offset)
00483 {
00484         struct hdr_imep *im = HDR_IMEP(p);
00485 
00486 #define U_INT16_T(x)    *((u_int16_t*) &(x))
00487 
00488         if (pt_->tagged()) {
00489             sprintf(pt_->buffer() + offset,
00490                     "-imep:a %c -imep:h %c -imep:o %c -imep:l %04x ",
00491                     (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
00492                     (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
00493                     (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
00494                     U_INT16_T(im->imep_length));
00495         } else if (newtrace_) {
00496             sprintf(pt_->buffer() + offset,
00497                 "-P imep -Pa %c -Ph %c -Po %c -Pl 0x%04x ] ",
00498                 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
00499                 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
00500                 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
00501                 U_INT16_T(im->imep_length));
00502         } else {
00503             sprintf(pt_->buffer() + offset,
00504                 "[%c %c %c 0x%04x] ",
00505                 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
00506                 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
00507                 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
00508                 U_INT16_T(im->imep_length));
00509         }
00510 #undef U_INT16_T
00511 }
00512 
00513 
00514 void
00515 CMUTrace::format_tora(Packet *p, int offset)
00516 {
00517         struct hdr_tora *th = HDR_TORA(p);
00518         struct hdr_tora_qry *qh = HDR_TORA_QRY(p);
00519         struct hdr_tora_upd *uh = HDR_TORA_UPD(p);
00520         struct hdr_tora_clr *ch = HDR_TORA_CLR(p);
00521 
00522         switch(th->th_type) {
00523 
00524         case TORATYPE_QRY:
00525 
00526                 if (pt_->tagged()) {
00527                     sprintf(pt_->buffer() + offset,
00528                             "-tora:t %x -tora:d %d -tora:c QUERY",
00529                             qh->tq_type, qh->tq_dst);
00530                 } else if (newtrace_) {
00531                     sprintf(pt_->buffer() + offset,
00532                         "-P tora -Pt 0x%x -Pd %d -Pc QUERY ",
00533                         qh->tq_type, qh->tq_dst);
00534                         
00535                 } else {
00536 
00537                     sprintf(pt_->buffer() + offset, "[0x%x %d] (QUERY)",
00538                         qh->tq_type, qh->tq_dst);
00539                 }
00540                 break;
00541 
00542         case TORATYPE_UPD:
00543 
00544                 if (pt_->tagged()) {
00545                     sprintf(pt_->buffer() + offset,
00546                             "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
00547                             "-tora:r %d -tora:e %d -tora:i %d -tora:c UPDATE",
00548                             uh->tu_type,
00549                             uh->tu_dst,
00550                             uh->tu_tau,
00551                             uh->tu_oid,
00552                             uh->tu_r,
00553                             uh->tu_delta,
00554                             uh->tu_id);
00555                 } else if (newtrace_) {
00556                     sprintf(pt_->buffer() + offset,
00557                         "-P tora -Pt 0x%x -Pd %d (%f %d %d %d %d) -Pc UPDATE ",
00558                         uh->tu_type,
00559                         uh->tu_dst,
00560                         uh->tu_tau,
00561                         uh->tu_oid,
00562                         uh->tu_r,
00563                         uh->tu_delta,
00564                         uh->tu_id);
00565                 } else {
00566                     sprintf(pt_->buffer() + offset,
00567                         "-Pt 0x%x -Pd %d -Pa %f -Po %d -Pr %d -Pe %d -Pi %d -Pc UPDATE ",
00568                         uh->tu_type,
00569                         uh->tu_dst,
00570                         uh->tu_tau,
00571                         uh->tu_oid,
00572                         uh->tu_r,
00573                         uh->tu_delta,
00574                         uh->tu_id);
00575                 }
00576                 break;
00577 
00578         case TORATYPE_CLR:
00579                 if (pt_->tagged()) {
00580                     sprintf(pt_->buffer() + offset,
00581                             "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
00582                             "-tora:c CLEAR ",
00583                             ch->tc_type,
00584                             ch->tc_dst,
00585                             ch->tc_tau,
00586                             ch->tc_oid);
00587                 } else if (newtrace_) {
00588                     sprintf(pt_->buffer() + offset, 
00589                         "-P tora -Pt 0x%x -Pd %d -Pa %f -Po %d -Pc CLEAR ",
00590                         ch->tc_type,
00591                         ch->tc_dst,
00592                         ch->tc_tau,
00593                         ch->tc_oid);
00594                 } else {
00595                     sprintf(pt_->buffer() + offset, "[0x%x %d %f %d] (CLEAR)",
00596                         ch->tc_type,
00597                         ch->tc_dst,
00598                         ch->tc_tau,
00599                         ch->tc_oid);
00600                 }
00601                 break;
00602         }
00603 }
00604 
00605 void
00606 CMUTrace::format_aodv(Packet *p, int offset)
00607 {
00608         struct hdr_aodv *ah = HDR_AODV(p);
00609         struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);
00610         struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);
00611 
00612 
00613         switch(ah->ah_type) {
00614         case AODVTYPE_RREQ:
00615 
00616                 if (pt_->tagged()) {
00617                     sprintf(pt_->buffer() + offset,
00618                             "-aodv:t %x -aodv:h %d -aodv:b %d -aodv:d %d "
00619                             "-aodv:ds %d -aodv:s %d -aodv:ss %d "
00620                             "-aodv:c REQUEST ",
00621                             rq->rq_type,
00622                             rq->rq_hop_count,
00623                             rq->rq_bcast_id,
00624                             rq->rq_dst,
00625                             rq->rq_dst_seqno,
00626                             rq->rq_src,
00627                             rq->rq_src_seqno);
00628                 } else if (newtrace_) {
00629 
00630                     sprintf(pt_->buffer() + offset,
00631                         "-P aodv -Pt 0x%x -Ph %d -Pb %d -Pd %d -Pds %d -Ps %d -Pss %d -Pc REQUEST ",
00632                         rq->rq_type,
00633                         rq->rq_hop_count,
00634                         rq->rq_bcast_id,
00635                         rq->rq_dst,
00636                         rq->rq_dst_seqno,
00637                         rq->rq_src,
00638                         rq->rq_src_seqno);
00639 
00640 
00641                 } else {
00642 
00643                     sprintf(pt_->buffer() + offset,
00644                         "[0x%x %d %d [%d %d] [%d %d]] (REQUEST)",
00645                         rq->rq_type,
00646                         rq->rq_hop_count,
00647                         rq->rq_bcast_id,
00648                         rq->rq_dst,
00649                         rq->rq_dst_seqno,
00650                         rq->rq_src,
00651                         rq->rq_src_seqno);
00652                 }
00653                 break;
00654 
00655         case AODVTYPE_RREP:
00656         case AODVTYPE_HELLO:
00657         case AODVTYPE_RERR:
00658                 
00659                 if (pt_->tagged()) {
00660                     sprintf(pt_->buffer() + offset,
00661                             "-aodv:t %x -aodv:h %d -aodv:d %d -adov:ds %d "
00662                             "-aodv:l %f -aodv:c %s ",
00663                             rp->rp_type,
00664                             rp->rp_hop_count,
00665                             rp->rp_dst,
00666                             rp->rp_dst_seqno,
00667                             rp->rp_lifetime,
00668                             rp->rp_type == AODVTYPE_RREP ? "REPLY" :
00669                             (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
00670                              "HELLO"));
00671                 } else if (newtrace_) {
00672                         
00673                         sprintf(pt_->buffer() + offset,
00674                             "-P aodv -Pt 0x%x -Ph %d -Pd %d -Pds %d -Pl %f -Pc %s ",
00675                                 rp->rp_type,
00676                                 rp->rp_hop_count,
00677                                 rp->rp_dst,
00678                                 rp->rp_dst_seqno,
00679                                 rp->rp_lifetime,
00680                                 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
00681                                 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
00682                                  "HELLO"));
00683                 } else {
00684                         
00685                         sprintf(pt_->buffer() + offset,
00686                                 "[0x%x %d [%d %d] %f] (%s)",
00687                                 rp->rp_type,
00688                                 rp->rp_hop_count,
00689                                 rp->rp_dst,
00690                                 rp->rp_dst_seqno,
00691                                 rp->rp_lifetime,
00692                                 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
00693                                 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
00694                                  "HELLO"));
00695                 }
00696                 break;
00697                 
00698         default:
00699 #ifdef WIN32
00700                 fprintf(stderr,
00701                         "CMUTrace::format_aodv: invalid AODV packet type\n");
00702 #else
00703                 fprintf(stderr,
00704                         "%s: invalid AODV packet type\n", __FUNCTION__);
00705 #endif
00706                 abort();
00707         }
00708 }
00709 
00710 void
00711 CMUTrace::nam_format(Packet *p, int offset)
00712 {
00713         Node* srcnode = 0 ;
00714         Node* dstnode = 0 ;
00715         Node* nextnode = 0 ;
00716         struct hdr_cmn *ch = HDR_CMN(p);
00717         struct hdr_ip *ih = HDR_IP(p);
00718         char op = (char) type_;
00719         char colors[32];
00720         int next_hop = -1 ;
00721 
00722         int dst = Address::instance().get_nodeaddr(ih->daddr());
00723 
00724         nextnode = Node::get_node_by_address(ch->next_hop_);
00725         if (nextnode) next_hop = nextnode->nodeid(); 
00726 
00727         srcnode = Node::get_node_by_address(src_);
00728         dstnode = Node::get_node_by_address(ch->next_hop_);
00729 
00730         double distance = 0;
00731 
00732         if ((srcnode) && (dstnode)) {
00733            MobileNode* tmnode = (MobileNode*)srcnode;
00734            MobileNode* rmnode = (MobileNode*)dstnode;
00735 
00736            distance = tmnode->propdelay(rmnode) * 300000000 ;
00737         }
00738 
00739         double energy = -1;
00740         double initenergy = -1;
00741 
00742         //default value for changing node color with respect to energy depletion
00743         double l1 = 0.5; 
00744         double l2 = 0.2;
00745         
00746         if (srcnode) {
00747             if (srcnode->energy_model()) {
00748                     energy = srcnode->energy_model()->energy();
00749                     initenergy = srcnode->energy_model()->initialenergy();
00750                     l1 = srcnode->energy_model()->level1();
00751                     l2 = srcnode->energy_model()->level2();
00752             }
00753         }
00754 
00755         int energyLevel = 0 ;
00756         double energyLeft = (double)(energy/initenergy) ;
00757 
00758         if ((energyLeft <= 1 ) && (energyLeft >= l1 )) energyLevel = 3; 
00759         if ((energyLeft >= l2 ) && (energyLeft < l1 )) energyLevel = 2; 
00760         if ((energyLeft > 0 ) && (energyLeft < l2 )) energyLevel = 1;   
00761 
00762         if (energyLevel == 0) 
00763                 strcpy(colors,"-c black -o red");
00764         else if (energyLevel == 1) 
00765                 strcpy(colors,"-c red -o yellow");
00766         else if (energyLevel == 2) 
00767                 strcpy(colors,"-c yellow -o green");
00768         else if (energyLevel == 3) 
00769                 strcpy(colors,"-c green -o black");
00770 
00771         // A simple hack for scadds demo (fernandez's visit) -- Chalermek
00772         int pkt_color = 0;
00773         if (ch->ptype()==PT_DIFF) {
00774                 hdr_cdiff *dfh= HDR_CDIFF(p);
00775                 if (dfh->mess_type != DATA) {
00776                         pkt_color = 1;
00777                 }
00778         }
00779         // convert to nam format 
00780         if (op == 's') op = 'h' ;
00781         if (op == 'D') op = 'd' ;
00782         if (op == 'h') {
00783            sprintf(pt_->nbuffer(),
00784                 "+ -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s ",
00785                 Scheduler::instance().clock(),
00786                 src_,                           // this node
00787                 next_hop,
00788                 packet_info.name(ch->ptype()),
00789                 ch->size(),
00790                 pkt_color,   
00791                 ch->uid(),
00792                 tracename);
00793 
00794            offset = strlen(pt_->nbuffer());
00795            pt_->namdump();
00796            sprintf(pt_->nbuffer() ,
00797                 "- -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
00798                 Scheduler::instance().clock(),
00799                 src_,                           // this node
00800                 next_hop,
00801                 packet_info.name(ch->ptype()),
00802                 ch->size(),
00803                 pkt_color,
00804                 ch->uid(),
00805                 tracename);
00806 
00807            offset = strlen(pt_->nbuffer());
00808            pt_->namdump();
00809         }
00810 
00811         // if nodes are too far from each other
00812         // nam won't dump SEND event 'cuz it's
00813         // gonna be dropped later anyway
00814         // this value 250 is pre-calculated by using 
00815         // two-ray ground refelction model with fixed
00816         // transmission power 3.652e-10
00817 //      if ((type_ == SEND)  && (distance > 250 )) return ;
00818 
00819         if(tracetype == TR_ROUTER && type_ == RECV && dst != -1 ) return ;
00820         if(type_ == RECV && dst == -1 )dst = src_ ; //broadcasting event
00821 
00822         if (energy != -1) { //energy model being turned on
00823            if (nodeColor[src_] != energyLevel ) { //only dump it when node  
00824                sprintf(pt_->nbuffer() ,                    //color change
00825                   "n -t %.9f -s %d -S COLOR %s",
00826                    Scheduler::instance().clock(),
00827                    src_,                           // this node
00828                    colors);
00829                offset = strlen(pt_->nbuffer());
00830                pt_->namdump();
00831                nodeColor[src_] = energyLevel ;
00832             }   
00833         }
00834 
00835 
00836         // don't generate an "r" event for a broadcast packet
00837         // as it was already generated when the "h" event was written
00838         if (next_hop == -1 && op == 'r')
00839                 return;
00840         
00841         sprintf(pt_->nbuffer(),
00842                 "%c -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
00843                 op,
00844                 Scheduler::instance().clock(),
00845                 src_,                           // this node
00846                 next_hop,
00847                 packet_info.name(ch->ptype()),
00848                 ch->size(),
00849                 pkt_color,
00850                 ch->uid(),
00851                 tracename);
00852 
00853         if (next_hop == -1 && op == 'h') {
00854                 // print extra fields for broadcast packets
00855 
00856                 // bradius is calculated assuming 2-ray ground reflectlon
00857                 // model using default settings of Phy/WirelessPhy and
00858                 // Antenna/OmniAntenna
00859                 if (bradius == 0.0) calculate_broadcast_parameters();
00860 
00861                 double radius = bradius*radius_scaling_factor_; 
00862 
00863                 // duration is calculated based on the radius and
00864                 // the speed of light (299792458 m/s)
00865                 double duration = (bradius/299792458.0)*duration_scaling_factor_;
00866 
00867 
00868                 sprintf(pt_->nbuffer() + strlen(pt_->nbuffer()),
00869                         " -R %.2f -D %.2f",
00870                         radius,
00871                         duration);
00872 
00873                 // schedule "r" event
00874                 Tcl& tcl = Tcl::instance();
00875                 tcl.evalf("[Simulator instance] at %f {[Simulator instance] puts-nam-traceall {r -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s -R %.2f -D %.2f}}",
00876                         Scheduler::instance().clock() + duration,
00877                         Scheduler::instance().clock() + duration,
00878                         src_,
00879                         next_hop,
00880                         packet_info.name(ch->ptype()),
00881                         ch->size(),
00882                         pkt_color,
00883                         ch->uid(),
00884                         tracename,
00885                         radius,
00886                         duration);
00887         }
00888 
00889         offset = strlen(pt_->nbuffer());
00890         pt_->namdump();
00891 }
00892 
00893 void CMUTrace::format(Packet* p, const char *why)
00894 {
00895         hdr_cmn *ch = HDR_CMN(p);
00896         int offset = 0;
00897 
00898         /*
00899          * Log the MAC Header
00900          */
00901         format_mac(p, why, offset);
00902 
00903         if (pt_->namchannel()) 
00904                 nam_format(p, offset);
00905         offset = strlen(pt_->buffer());
00906         switch(ch->ptype()) {
00907         case PT_MAC:
00908                 break;
00909         case PT_ARP:
00910                 format_arp(p, offset);
00911                 break;
00912         default:
00913                 format_ip(p, offset);
00914                 offset = strlen(pt_->buffer());
00915                 switch(ch->ptype()) {
00916                 case PT_AODV:
00917                         format_aodv(p, offset);
00918                         break;
00919                 case PT_TORA:
00920                         format_tora(p, offset);
00921                         break;
00922                 case PT_IMEP:
00923                         format_imep(p, offset);
00924                         break;
00925                 case PT_DSR:
00926                         format_dsr(p, offset);
00927                         break;
00928                 case PT_MESSAGE:
00929                 case PT_UDP:
00930                         format_msg(p, offset);
00931                         break;
00932                 case PT_TCP:
00933                 case PT_ACK:
00934                         format_tcp(p, offset);
00935                         break;
00936                 case PT_CBR:
00937                         format_rtp(p, offset);
00938                         break;
00939                 case PT_DIFF:
00940                         break;
00941                 case PT_GAF:
00942                 case PT_PING:
00943                         break;
00944                 default:
00945                         fprintf(stderr, "%s - invalid packet type (%s).\n",
00946                                 __PRETTY_FUNCTION__, packet_info.name(ch->ptype()));
00947                         exit(1);
00948                 }
00949         }
00950 }
00951 
00952 int
00953 CMUTrace::command(int argc, const char*const* argv)
00954 {
00955         
00956         if(argc == 3) {
00957                 if(strcmp(argv[1], "node") == 0) {
00958                         node_ = (MobileNode*) TclObject::lookup(argv[2]);
00959                         if(node_ == 0)
00960                                 return TCL_ERROR;
00961                         return TCL_OK;
00962                 }
00963                 if (strcmp(argv[1], "newtrace") == 0) {
00964                         newtrace_ = atoi(argv[2]);
00965                         return TCL_OK;
00966                 }
00967         }
00968         return Trace::command(argc, argv);
00969 }
00970 
00971 /*ARGSUSED*/
00972 void
00973 CMUTrace::recv(Packet *p, Handler *h)
00974 {
00975         if (!node_energy()) {
00976                 Packet::free(p);
00977                 return;
00978         }
00979         assert(initialized());
00980         /*
00981          * Agent Trace "stamp" the packet with the optimal route on
00982          * sending.
00983          */
00984         if (tracetype == TR_AGENT && type_ == SEND) {
00985                 God::instance()->stampPacket(p);
00986         }
00987 #if 0
00988         /*
00989          * When the originator of a packet drops the packet, it may or may
00990          * not have been stamped by GOD.  Stamp it before logging the
00991          * information.
00992          */
00993         if(src_ == src && type_ == DROP) {
00994                 God::instance()->stampPacket(p);
00995         }
00996 #endif
00997         format(p, "---");
00998         pt_->dump();
00999         //namdump();
01000         if(target_ == 0)
01001                 Packet::free(p);
01002         else
01003                 send(p, h);
01004 }
01005 
01006 void
01007 CMUTrace::recv(Packet *p, const char* why)
01008 {
01009         assert(initialized() && type_ == DROP);
01010         if (!node_energy()) {
01011                 Packet::free(p);
01012                 return;
01013         }
01014 #if 0
01015         /*
01016          * When the originator of a packet drops the packet, it may or may
01017          * not have been stamped by GOD.  Stamp it before logging the
01018          * information.
01019          */
01020         if(src_ == ih->saddr()) {
01021                 God::instance()->stampPacket(p);
01022         }
01023 #endif
01024         format(p, why);
01025         pt_->dump();
01026         //namdump();
01027         Packet::free(p);
01028 }
01029 
01030 int CMUTrace::node_energy()
01031 {
01032         Node* thisnode = Node::get_node_by_address(src_);
01033         double energy = 1;
01034         if (thisnode) {
01035                 if (thisnode->energy_model()) {
01036                         energy = thisnode->energy_model()->energy();
01037                 }
01038         } 
01039         if (energy > 0) return 1;
01040         return 0;
01041 }
01042 
01043 void CMUTrace::calculate_broadcast_parameters() {
01044         // Calculate the maximum distance at which a packet can be received
01045         // based on the two-ray reflection model using the current default
01046         // values for Phy/WirelessPhy and Antenna/OmniAntenna.
01047 
01048         double P_t, P_r, G_t, G_r, h, L;
01049         Tcl& tcl = Tcl::instance();
01050 
01051         tcl.evalc("Phy/WirelessPhy set Pt_");
01052         P_t = atof(tcl.result());
01053         tcl.evalc("Phy/WirelessPhy set RXThresh_");
01054         P_r = atof(tcl.result());
01055         tcl.evalc("Phy/WirelessPhy set L_");
01056         L = atof(tcl.result());
01057         tcl.evalc("Antenna/OmniAntenna set Gt_");
01058         G_t = atof(tcl.result());
01059         tcl.evalc("Antenna/OmniAntenna set Gr_");
01060         G_r = atof(tcl.result());
01061         tcl.evalc("Antenna/OmniAntenna set Z_");
01062         h = atof(tcl.result());
01063         bradius = pow(P_t*G_r*G_t*pow(h,4.0)/(P_r*L), 0.25);
01064 
01065         // Also get the scaling factors
01066         tcl.evalc("CMUTrace set radius_scaling_factor_");
01067         radius_scaling_factor_ = atof(tcl.result());
01068         tcl.evalc("CMUTrace set duration_scaling_factor_");
01069         duration_scaling_factor_ = atof(tcl.result());
01070 }
01071 
01072 

Generated on Tue Apr 20 12:14:12 2004 for NS2.26SourcesOriginal by doxygen 1.3.3