mac-802_11.h

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  * $Header: /nfs/jade/vint/CVSROOT/ns-2/mac/mac-802_11.h,v 1.24 2004/04/02 01:00:25 xuanc Exp $
00035  *
00036  * Ported from CMU/Monarch's code, nov'98 -Padma.
00037  * wireless-mac-802_11.h
00038  */
00039 
00040 #ifndef ns_mac_80211_h
00041 #define ns_mac_80211_h
00042 
00043 // Added by Sushmita to support event tracing (singal@nunki.usc.edu)
00044 #include "address.h"
00045 #include "ip.h"
00046 
00047 #include "mac-timers.h"
00048 #include "marshall.h"
00049 #include <math.h>
00050 
00051 class EventTrace;
00052 
00053 #define GET_ETHER_TYPE(x)       GET2BYTE((x))
00054 #define SET_ETHER_TYPE(x,y)            {u_int16_t t = (y); STORE2BYTE(x,&t);}
00055 
00056 /* ======================================================================
00057    Frame Formats
00058    ====================================================================== */
00059 
00060 #define MAC_ProtocolVersion 0x00
00061 
00062 #define MAC_Type_Management 0x00
00063 #define MAC_Type_Control    0x01
00064 #define MAC_Type_Data       0x02
00065 #define MAC_Type_Reserved   0x03
00066 
00067 #define MAC_Subtype_RTS     0x0B
00068 #define MAC_Subtype_CTS     0x0C
00069 #define MAC_Subtype_ACK     0x0D
00070 #define MAC_Subtype_Data    0x00
00071 
00072 
00073 struct frame_control {
00074     u_char      fc_subtype      : 4;
00075     u_char      fc_type         : 2;
00076     u_char      fc_protocol_version : 2;
00077 
00078     u_char      fc_order        : 1;
00079     u_char      fc_wep          : 1;
00080     u_char      fc_more_data        : 1;
00081     u_char      fc_pwr_mgt      : 1;
00082     u_char      fc_retry        : 1;
00083     u_char      fc_more_frag        : 1;
00084     u_char      fc_from_ds      : 1;
00085     u_char      fc_to_ds        : 1;
00086 };
00087 
00088 struct rts_frame {
00089     struct frame_control    rf_fc;
00090     u_int16_t       rf_duration;
00091     u_char          rf_ra[ETHER_ADDR_LEN];
00092     u_char          rf_ta[ETHER_ADDR_LEN];
00093     u_char          rf_fcs[ETHER_FCS_LEN];
00094 };
00095 
00096 struct cts_frame {
00097     struct frame_control    cf_fc;
00098     u_int16_t       cf_duration;
00099     u_char          cf_ra[ETHER_ADDR_LEN];
00100     u_char          cf_fcs[ETHER_FCS_LEN];
00101 };
00102 
00103 struct ack_frame {
00104     struct frame_control    af_fc;
00105     u_int16_t       af_duration;
00106     u_char          af_ra[ETHER_ADDR_LEN];
00107     u_char          af_fcs[ETHER_FCS_LEN];
00108 };
00109 
00110 // XXX This header does not have its header access function because it shares
00111 // the same header space with hdr_mac.
00112 struct hdr_mac802_11 {
00113     struct frame_control    dh_fc;
00114     u_int16_t       dh_duration;
00115     u_char                  dh_ra[ETHER_ADDR_LEN];
00116         u_char                  dh_ta[ETHER_ADDR_LEN];
00117         u_char                  dh_3a[ETHER_ADDR_LEN];
00118     u_int16_t       dh_scontrol;
00119     u_char          dh_body[0]; // XXX Non-ANSI
00120 };
00121 
00122 
00123 /* ======================================================================
00124    Definitions
00125    ====================================================================== */
00126 
00127 /* Must account for propagation delays added by the channel model when
00128  * calculating tx timeouts (as set in tcl/lan/ns-mac.tcl).
00129  *   -- Gavin Holland, March 2002
00130  */
00131 #define DSSS_MaxPropagationDelay        0.000002        // 2us   XXXX
00132 
00133 class PHY_MIB {
00134 public:
00135     PHY_MIB(Mac802_11 *parent);
00136 
00137     inline u_int32_t getCWMin() { return(CWMin); }
00138         inline u_int32_t getCWMax() { return(CWMax); }
00139     inline double getSlotTime() { return(SlotTime); }
00140     inline double getSIFS() { return(SIFSTime); }
00141     inline double getPIFS() { return(SIFSTime + SlotTime); }
00142     inline double getDIFS() { return(SIFSTime + 2 * SlotTime); }
00143     inline double getEIFS() {
00144         // see (802.11-1999, 9.2.10)
00145         return(SIFSTime + getDIFS()
00146                        + (8 *  getACKlen())/PLCPDataRate);
00147     }
00148     inline u_int32_t getPreambleLength() { return(PreambleLength); }
00149     inline double getPLCPDataRate() { return(PLCPDataRate); }
00150     
00151     inline u_int32_t getPLCPhdrLen() {
00152         return((PreambleLength + PLCPHeaderLength) >> 3);
00153     }
00154 
00155     inline u_int32_t getHdrLen11() {
00156         return(getPLCPhdrLen() + sizeof(struct hdr_mac802_11)
00157                        + ETHER_FCS_LEN);
00158     }
00159     
00160     inline u_int32_t getRTSlen() {
00161         return(getPLCPhdrLen() + sizeof(struct rts_frame));
00162     }
00163     
00164     inline u_int32_t getCTSlen() {
00165         return(getPLCPhdrLen() + sizeof(struct cts_frame));
00166     }
00167     
00168     inline u_int32_t getACKlen() {
00169         return(getPLCPhdrLen() + sizeof(struct ack_frame));
00170     }
00171 
00172  private:
00173 
00174 
00175 
00176 
00177     u_int32_t   CWMin;
00178     u_int32_t   CWMax;
00179     double      SlotTime;
00180     double      SIFSTime;
00181     u_int32_t   PreambleLength;
00182     u_int32_t   PLCPHeaderLength;
00183     double      PLCPDataRate;
00184 };
00185 
00186 
00187 /*
00188  * IEEE 802.11 Spec, section 11.4.4.2
00189  *      - default values for the MAC Attributes
00190  */
00191 #define MAC_FragmentationThreshold  2346        // bytes
00192 #define MAC_MaxTransmitMSDULifetime 512     // time units
00193 #define MAC_MaxReceiveLifetime      512     // time units
00194 
00195 class MAC_MIB {
00196 public:
00197 
00198     MAC_MIB(Mac802_11 *parent);
00199 
00200 private:
00201     u_int32_t   RTSThreshold;
00202     u_int32_t   ShortRetryLimit;
00203     u_int32_t   LongRetryLimit;
00204 public:
00205     u_int32_t   FailedCount;    
00206     u_int32_t   RTSFailureCount;
00207     u_int32_t   ACKFailureCount;
00208  public:
00209        inline u_int32_t getRTSThreshold() { return(RTSThreshold);}
00210        inline u_int32_t getShortRetryLimit() { return(ShortRetryLimit);}
00211        inline u_int32_t getLongRetryLimit() { return(LongRetryLimit);}
00212 };
00213 
00214 
00215 /* ======================================================================
00216    The following destination class is used for duplicate detection.
00217    ====================================================================== */
00218 class Host {
00219 public:
00220     LIST_ENTRY(Host) link;
00221     u_int32_t   index;
00222     u_int32_t   seqno;
00223 };
00224 
00225 
00226 /* ======================================================================
00227    The actual 802.11 MAC class.
00228    ====================================================================== */
00229 class Mac802_11 : public Mac {
00230     friend class DeferTimer;
00231 
00232 
00233     friend class BackoffTimer;
00234     friend class IFTimer;
00235     friend class NavTimer;
00236     friend class RxTimer;
00237     friend class TxTimer;
00238 public:
00239     Mac802_11();
00240     void        recv(Packet *p, Handler *h);
00241     inline int  hdr_dst(char* hdr, int dst = -2);
00242     inline int  hdr_src(char* hdr, int src = -2);
00243     inline int  hdr_type(char* hdr, u_int16_t type = 0);
00244     
00245     inline int bss_id() { return bss_id_; }
00246     
00247     // Added by Sushmita to support event tracing
00248         void trace_event(char *, Packet *);
00249         EventTrace *et_;
00250 
00251 protected:
00252     void    backoffHandler(void);
00253     void    deferHandler(void);
00254     void    navHandler(void);
00255     void    recvHandler(void);
00256     void    sendHandler(void);
00257     void    txHandler(void);
00258 
00259 private:
00260     int     command(int argc, const char*const* argv);
00261 
00262     /*
00263      * Called by the timers.
00264      */
00265     void        recv_timer(void);
00266     void        send_timer(void);
00267     int     check_pktCTRL();
00268     int     check_pktRTS();
00269     int     check_pktTx();
00270 
00271     /*
00272      * Packet Transmission Functions.
00273      */
00274     void    send(Packet *p, Handler *h);
00275     void    sendRTS(int dst);
00276     void    sendCTS(int dst, double duration);
00277     void    sendACK(int dst);
00278     void    sendDATA(Packet *p);
00279     void    RetransmitRTS();
00280     void    RetransmitDATA();
00281 
00282     /*
00283      * Packet Reception Functions.
00284      */
00285     void    recvRTS(Packet *p);
00286     void    recvCTS(Packet *p);
00287     void    recvACK(Packet *p);
00288     void    recvDATA(Packet *p);
00289 
00290     void        capture(Packet *p);
00291     void        collision(Packet *p);
00292     void        discard(Packet *p, const char* why);
00293     void        rx_resume(void);
00294     void        tx_resume(void);
00295 
00296     inline int  is_idle(void);
00297 
00298     /*
00299      * Debugging Functions.
00300      */
00301     void        trace_pkt(Packet *p);
00302     void        dump(char* fname);
00303 
00304     inline int initialized() {  
00305         return (cache_ && logtarget_
00306                         && Mac::initialized());
00307     }
00308 
00309     inline void mac_log(Packet *p) {
00310                 logtarget_->recv(p, (Handler*) 0);
00311         }
00312 
00313     double txtime(Packet *p);
00314     double txtime(double psz, double drt);
00315     double txtime(int bytes) { /* clobber inherited txtime() */ abort(); return 0;}
00316 
00317     inline void transmit(Packet *p, double timeout);
00318     inline void checkBackoffTimer(void);
00319     inline void postBackoff(int pri);
00320     inline void setRxState(MacState newState);
00321     inline void setTxState(MacState newState);
00322 
00323 
00324     inline void inc_cw() {
00325         cw_ = (cw_ << 1) + 1;
00326         if(cw_ > phymib_.getCWMax())
00327             cw_ = phymib_.getCWMax();
00328     }
00329     inline void rst_cw() { cw_ = phymib_.getCWMin(); }
00330 
00331     inline double sec(double t) { return(t *= 1.0e-6); }
00332     inline u_int16_t usec(double t) {
00333         u_int16_t us = (u_int16_t)floor((t *= 1e6) + 0.5);
00334         return us;
00335     }
00336     inline void set_nav(u_int16_t us) {
00337         double now = Scheduler::instance().clock();
00338         double t = us * 1e-6;
00339         if((now + t) > nav_) {
00340             nav_ = now + t;
00341             if(mhNav_.busy())
00342                 mhNav_.stop();
00343             mhNav_.start(t);
00344         }
00345     }
00346 
00347 protected:
00348     PHY_MIB         phymib_;
00349         MAC_MIB         macmib_;
00350 
00351        /* the macaddr of my AP in BSS mode; for IBSS mode
00352         * this is set to a reserved value IBSS_ID - the
00353         * MAC_BROADCAST reserved value can be used for this
00354         * purpose
00355         */
00356        int     bss_id_;
00357        enum    {IBSS_ID=MAC_BROADCAST};
00358 
00359 
00360 private:
00361     double      basicRate_;
00362     double      dataRate_;
00363     
00364     /*
00365      * Mac Timers
00366      */
00367     IFTimer     mhIF_;      // interface timer
00368     NavTimer    mhNav_;     // NAV timer
00369     RxTimer     mhRecv_;        // incoming packets
00370     TxTimer     mhSend_;        // outgoing packets
00371 
00372     DeferTimer  mhDefer_;   // defer timer
00373     BackoffTimer    mhBackoff_; // backoff timer
00374 
00375     /* ============================================================
00376        Internal MAC State
00377        ============================================================ */
00378     double      nav_;       // Network Allocation Vector
00379 
00380     MacState    rx_state_;  // incoming state (MAC_RECV or MAC_IDLE)
00381     MacState    tx_state_;  // outgoint state
00382     int     tx_active_; // transmitter is ACTIVE
00383 
00384     Packet          *eotPacket_;    // copy for eot callback
00385 
00386     Packet      *pktRTS_;   // outgoing RTS packet
00387     Packet      *pktCTRL_;  // outgoing non-RTS packet
00388 
00389     u_int32_t   cw_;        // Contention Window
00390     u_int32_t   ssrc_;      // STA Short Retry Count
00391     u_int32_t   slrc_;      // STA Long Retry Count
00392 
00393     int     min_frame_len_;
00394 
00395     NsObject*   logtarget_;
00396     NsObject*       EOTtarget_;     // given a copy of packet at TX end
00397 
00398 
00399 
00400 
00401     /* ============================================================
00402        Duplicate Detection state
00403        ============================================================ */
00404     u_int16_t   sta_seqno_; // next seqno that I'll use
00405     int     cache_node_count_;
00406     Host        *cache_;
00407 };
00408 
00409 #endif /* __mac_80211_h__ */
00410 

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