mac-csma.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 Daedalus Research
00017  *  Group at the University of California Berkeley.
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  * Contributed by Giao Nguyen, http://daedalus.cs.berkeley.edu/~gnguyen
00035  */
00036 
00037 #ifndef lint
00038 static const char rcsid[] =
00039     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/mac/mac-csma.cc,v 1.26 1998/11/17 23:36:35 yuriy Exp $ (UCB)";
00040 #endif
00041 
00042 #include "template.h"
00043 #include "random.h"
00044 #include "channel.h"
00045 #include "mac-csma.h"
00046 
00047 static class MacCsmaClass : public TclClass {
00048 public:
00049     MacCsmaClass() : TclClass("Mac/Csma") {}
00050     TclObject* create(int, const char*const*) {
00051         return (new MacCsma);
00052     }
00053 } class_mac_csma;
00054 
00055 static class MacCsmaCdClass : public TclClass {
00056 public:
00057     MacCsmaCdClass() : TclClass("Mac/Csma/Cd") {}
00058     TclObject* create(int, const char*const*) {
00059         return (new MacCsmaCd);
00060     }
00061 } class_mac_csma_cd;
00062 
00063 static class MacCsmaCaClass : public TclClass {
00064 public:
00065     MacCsmaCaClass() : TclClass("Mac/Csma/Ca") {}
00066     TclObject* create(int, const char*const*) {
00067         return (new MacCsmaCa);
00068     }
00069 } class_mac_csma_ca;
00070 
00071 
00072 void
00073 MacHandlerEoc::handle(Event* e)
00074 {
00075     mac_->endofContention((Packet*)e);
00076 }
00077 
00078 
00079 MacCsma::MacCsma() : txstart_(0), rtx_(0), csense_(1), hEoc_(this)
00080 {
00081     bind_time("ifs_", &ifs_);
00082     bind_time("slotTime_", &slotTime_);
00083     bind("cwmin_", &cwmin_);
00084     bind("cwmax_", &cwmax_);
00085     bind("rtxLimit_", &rtxLimit_);
00086     bind("csense_", &csense_);
00087     cw_ = cwmin_;
00088 }
00089 
00090 
00091 void MacCsma::resume(Packet* p)
00092 {
00093     Scheduler& s = Scheduler::instance();
00094     s.schedule(callback_, &intr_, ifs_ + slotTime_ * cwmin_);
00095     if (p != 0)
00096         drop(p);
00097     callback_ = 0;
00098     state(MAC_IDLE);
00099     rtx_ = 0;
00100     cw_ = cwmin_;
00101 }
00102 
00103 
00104 void MacCsma::send(Packet* p)
00105 {
00106     Scheduler& s = Scheduler::instance();
00107     double delay = channel_->txstop() + ifs_ - s.clock();
00108 
00109     // if channel is not ready, then wait
00110     // else content for the channel
00111 
00112     /* XXX floating point operations differences have been
00113        observed on the resulting delay value on Pentium II and
00114        SunSparc.  E.g.
00115                                PentiumII                   SunSparc
00116                                -------------------------------
00117        channel_->txstop_=      0.11665366666666668         0.11665366666666668
00118                        binary    0x3fbddd03c34ab4a2          0x3fbddd03c34ab4a2
00119        ifs_=                   5.1999999999999997e-05      5.1999999999999997e-05
00120                        binary    0x3f0b43526527a205          0x3f0b43526527a205
00121        s.clock_=               0.11670566666666668         0.11670566666666668
00122                        binary    0x3fbde06c2d975996          0x3fbde06c2d975996
00123 
00124        delay=                  3.5033282698437862e-18      0
00125                        binary    0x3c50280000000000          0x0000000000000000
00126 
00127        Because of that the value of (csense_ && delay > 0) was different.  Fixed by
00128        changing 0 to EPS
00129      */
00130     static const double EPS= 1.0e-12; //seems appropriate (less than nanosec)
00131     if (csense_ && delay > EPS)
00132         s.schedule(&hSend_, p, delay + 0.000001);
00133     else {
00134         txstart_ = s.clock();
00135         channel_->contention(p, &hEoc_);
00136     }
00137 }
00138 
00139 
00140 void MacCsma::backoff(Handler* h, Packet* p, double delay)
00141 {
00142     Scheduler& s = Scheduler::instance();
00143     double now = s.clock();
00144 
00145     // if retransmission time within limit, do exponential backoff
00146     // else drop the packet and resume
00147     if (++rtx_ < rtxLimit_) {
00148         delay += max(channel_->txstop() + ifs_ - now, 0.0);
00149         int slot = Random::integer(cw_);
00150         s.schedule(h, p, delay + slotTime_ * slot);
00151         cw_ = min(2 * cw_, cwmax_);
00152     }
00153     else
00154         resume(p);
00155 }
00156 
00157 
00158 void MacCsma::endofContention(Packet* p)
00159 {
00160     Scheduler& s = Scheduler::instance();
00161     double txt = txtime(p) - (s.clock() - txstart_);
00162     hdr_mac::access(p)->txtime() = txt;
00163     channel_->send(p, txt);
00164     s.schedule(&hRes_, &eEoc_, txt);
00165     rtx_ = 0;
00166     cw_ = cwmin_;
00167 }
00168 
00169 
00170 void MacCsmaCd::endofContention(Packet* p)
00171 {
00172     // If there is a collision, backoff
00173     if (channel_->collision()) {
00174         channel_->jam(0);
00175         backoff(&hSend_, p);
00176     }
00177     else
00178         MacCsma::endofContention(p);
00179 }
00180 
00181 
00182 void MacCsmaCa::send(Packet* p)
00183 {
00184     Scheduler& s = Scheduler::instance();
00185     double delay = channel_->txstop() + ifs_ - s.clock();
00186 
00187     if (csense_ && delay > 0)
00188         backoff(&hSend_, p);
00189     else {
00190         txstart_ = s.clock();
00191         channel_->contention(p, &hEoc_);
00192     }
00193 }

Generated on Tue Mar 6 16:47:47 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6