miracle_mac-802_11.h

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_mrclmac_80211_h
00041 #define ns_mrclmac_80211_h
00042 
00043 // Added by Sushmita to support event tracing (singal@nunki.usc.edu)
00044 #include "address.h"
00045 #include "ip.h"
00046 #include "bi-connector.h"
00047 #include "packet.h"
00048 #include "route.h"
00049 #include "ll.h"
00050 #include "phy.h"
00051 #include "channel.h"
00052 
00053 #include "mrcl_mac-timers.h"
00054 #include "marshall.h"
00055 #include "mac-802_11.h"
00056 #include <math.h>
00057 
00058 class Channel;
00059 
00060 class EventTrace;
00061 
00062 #define ZERO    0.00000
00063 
00064 /*
00065  * Medium Access Control (MAC)
00066  */
00067 
00068 #define EF_COLLISION 2          // collision error flag
00069 
00070 /* ======================================================================
00071    Defines / Macros used by all MACs.
00072    ====================================================================== */
00073 
00074 #define ETHER_ADDR(x)   (GET4BYTE(x))
00075 
00076 #define MAC_HDR_LEN     64
00077 
00078 #define MAC_BROADCAST   ((u_int32_t) 0xffffffff)
00079 #define BCAST_ADDR -1
00080 
00081 #define ETHER_ADDR_LEN  6
00082 #define ETHER_TYPE_LEN  2
00083 #define ETHER_FCS_LEN   4
00084 
00085 #define ETHERTYPE_IP    0x0800
00086 #define ETHERTYPE_ARP   0x0806
00087 
00088 
00089 #define GET_ETHER_TYPE(x)               GET2BYTE((x))
00090 #define SET_ETHER_TYPE(x,y)            {u_int16_t t = (y); STORE2BYTE(x,&t);}
00091 
00092 /* ======================================================================
00093    Frame Formats
00094    ====================================================================== */
00095 
00096 #define MAC_ProtocolVersion     0x00
00097 
00098 #define MAC_Type_Management     0x00
00099 #define MAC_Type_Control        0x01
00100 #define MAC_Type_Data           0x02
00101 #define MAC_Type_Reserved       0x03
00102 
00103 #define MAC_Subtype_RTS         0x0B
00104 #define MAC_Subtype_CTS         0x0C
00105 #define MAC_Subtype_ACK         0x0D
00106 #define MAC_Subtype_Data        0x00
00107 
00108 // NB: The next structs are just defined in mac-802_11.h
00109 
00110 // enum MacState {
00111 //      MAC_IDLE        = 0x0000,
00112 //      MAC_POLLING     = 0x0001,
00113 //      MAC_RECV        = 0x0010,
00114 //      MAC_SEND        = 0x0100,
00115 //      MAC_RTS         = 0x0200,
00116 //      MAC_CTS         = 0x0400,
00117 //      MAC_ACK         = 0x0800,
00118 //      MAC_COLL        = 0x1000
00119 // };
00120 // 
00121 // enum MacFrameType {
00122 //      MF_BEACON       = 0x0008, // beaconing
00123 //      MF_CONTROL      = 0x0010, // used as mask for control frame
00124 //      MF_SLOTS        = 0x001a, // announce slots open for contention
00125 //      MF_RTS          = 0x001b, // request to send
00126 //      MF_CTS          = 0x001c, // clear to send, grant
00127 //      MF_ACK          = 0x001d, // acknowledgement
00128 //      MF_CF_END       = 0x001e, // contention free period end
00129 //      MF_POLL         = 0x001f, // polling
00130 //      MF_DATA         = 0x0020, // also used as mask for data frame
00131 //      MF_DATA_ACK     = 0x0021  // ack for data frames
00132 // };
00133 
00134 // struct hdr_mrclmac {
00135 //      MrclMacFrameType ftype_;        // frame type
00136 //      int macSA_;             // source MAC address
00137 //      int macDA_;             // destination MAC address
00138 //      u_int16_t hdr_type_;     // mac_hdr type
00139 // 
00140 //      double txtime_;         // transmission time
00141 //      double sstime_;         // slot start time
00142 // 
00143 //      int padding_;
00144 // 
00145 //      inline void set(MrclMacFrameType ft, int sa, int da=-1) {
00146 //              ftype_ = ft;
00147 //              macSA_ = sa;
00148 //              if (da != -1)  macDA_ = da;
00149 //      }
00150 //      inline MrclMacFrameType& ftype() { return ftype_; }
00151 //      inline int& macSA() { return macSA_; }
00152 //      inline int& macDA() { return macDA_; }
00153 //      inline u_int16_t& hdr_type() {return hdr_type_; }
00154 // 
00155 //      inline double& txtime() { return txtime_; }
00156 //      inline double& sstime() { return sstime_; }
00157 // 
00158 //      // Header access methods
00159 //      static int offset_;
00160 //      inline static int& offset() { return offset_; }
00161 //      inline static hdr_mrclmac* access(const Packet* p) {
00162 //              return (hdr_mrclmac*) p->access(offset_);
00163 //      }
00164 // };
00165 
00166 //#define HDR_MRCLMAC(p)      (hdr_mrclmac::access(p))
00167 
00168 // struct frame_control {
00169 //      u_char          fc_subtype              : 4;
00170 //      u_char          fc_type                 : 2;
00171 //      u_char          fc_protocol_version     : 2;
00172 // 
00173 //      u_char          fc_order                : 1;
00174 //      u_char          fc_wep                  : 1;
00175 //      u_char          fc_more_data            : 1;
00176 //      u_char          fc_pwr_mgt              : 1;
00177 //      u_char          fc_retry                : 1;
00178 //      u_char          fc_more_frag            : 1;
00179 //      u_char          fc_from_ds              : 1;
00180 //      u_char          fc_to_ds                : 1;
00181 // };
00182 
00183 // struct rts_frame {
00184 //      struct frame_control    rf_fc;
00185 //      u_int16_t               rf_duration;
00186 //      u_char                  rf_ra[ETHER_ADDR_LEN];
00187 //      u_char                  rf_ta[ETHER_ADDR_LEN];
00188 //      u_char                  rf_fcs[ETHER_FCS_LEN];
00189 // };
00190 
00191 // struct cts_frame {
00192 //      struct frame_control    cf_fc;
00193 //      u_int16_t               cf_duration;
00194 //      u_char                  cf_ra[ETHER_ADDR_LEN];
00195 //      u_char                  cf_fcs[ETHER_FCS_LEN];
00196 // };
00197 
00198 // struct ack_frame {
00199 //      struct frame_control    af_fc;
00200 //      u_int16_t               af_duration;
00201 //      u_char                  af_ra[ETHER_ADDR_LEN];
00202 //      u_char                  af_fcs[ETHER_FCS_LEN];
00203 // };
00204 
00205 /*XXX This header does not have its header access function because it shares
00206 the same header space with hdr_mrclmac.*/
00207 // struct hdr_mrclmac802_11 {
00208 //      struct frame_control    dh_fc;
00209 //      u_int16_t               dh_duration;
00210 //      u_char                  dh_ra[ETHER_ADDR_LEN];
00211 //         u_char                  dh_ta[ETHER_ADDR_LEN];
00212 //         u_char                  dh_3a[ETHER_ADDR_LEN];
00213 //      u_int16_t               dh_scontrol;
00214 //      u_char                  dh_body[0]; // XXX Non-ANSI
00215 //      // Header access methods
00216 //      static int offset_;
00217 //      inline static int& offset() { return offset_; }
00218 //      inline static hdr_mrclmac802_11* access(const Packet* p) {
00219 //              return (hdr_mrclmac802_11*) p->access(offset_);
00220 //      }
00221 // };
00222 
00223 //#define HDR_MRCLMAC802_11(p)      (hdr_mrclmac802_11::access(p))
00224 
00225 /* ======================================================================
00226    Definitions
00227    ====================================================================== */
00228 
00229 /* Must account for propagation delays added by the channel model when
00230  * calculating tx timeouts (as set in tcl/lan/ns-mac.tcl).
00231  *   -- Gavin Holland, March 2002
00232  */
00233 #define DSSS_MaxPropagationDelay        0.000002        // 2us   XXXX
00234 
00235 
00236 class MrclMac;
00237 
00238 class MrclTap {
00239 public:
00240         virtual ~MrclTap () {}
00241         virtual void tap(const Packet *p) = 0;
00242         // tap is given all packets received by the host.
00243         // it must not alter or free the pkt.  If you want to frob it, copy it.
00244 };
00245 
00246 
00247 class MrclMacHandlerResume : public Handler {
00248 public:
00249         MrclMacHandlerResume(MrclMac* m) : mac_(m) {}
00250         void handle(Event*);
00251 protected:
00252         MrclMac* mac_;
00253 };
00254 
00255 class MrclMacHandlerSend : public Handler {
00256 public:
00257         MrclMacHandlerSend(MrclMac* m) : mac_(m) {}
00258         void handle(Event*);
00259 protected:
00260         MrclMac* mac_;
00261 };
00262 
00263 
00264 /* ==================================================================
00265    MAC data structure
00266    ================================================================*/
00267 
00268 class MrclMac : public BiConnector {
00269 public:
00270         MrclMac();
00271         virtual void recv(Packet* p, Handler* h);
00272         virtual void sendDown(Packet* p);
00273         virtual void sendUp(Packet *p);
00274 
00275         virtual void resume(Packet* p = 0);
00276         virtual void installTap(MrclTap *t) { tap_ = t; }
00277         
00278         inline double txtime(int bytes) {
00279                 return (8. * bytes / bandwidth_);
00280         }
00281         inline double txtime(Packet* p) {
00282                 return 8. * (MAC_HDR_LEN + \
00283                              (HDR_CMN(p))->size()) / bandwidth_;
00284         }
00285         inline double bandwidth() const { return bandwidth_; }
00286         
00287         inline int addr() { return index_; }
00288         inline MacState state() { return state_; }
00289         inline MacState state(int m) { return state_ = (MacState) m; }
00290         
00291         //mac methods to set dst, src and hdt_type in pkt hdrs.
00292         // note: -1 is the broadcast mac addr.
00293         virtual inline int hdr_dst(char* hdr, int dst = -2) {
00294                 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00295                 if(dst > -2)
00296                         dh->macDA_ = dst;
00297                 return dh->macDA();
00298         }
00299         virtual inline int hdr_src(char* hdr, int src = -2) {
00300                 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00301                 if(src > -2)
00302                         dh->macSA_ = src;
00303                 return dh->macSA();
00304         }
00305         virtual inline int hdr_type(char *hdr, u_int16_t type = 0) {
00306                 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00307                 if (type)
00308                         dh->hdr_type_ = type;
00309                 return dh->hdr_type();
00310         }
00311 
00312 private:
00313         void mac_log(Packet *p) {
00314                 logtarget_->recv(p, (Handler*) 0);
00315         }
00316         NsObject*       logtarget_;
00317 
00318 protected:
00319         int command(int argc, const char*const* argv);
00320         virtual int initialized() { 
00321                 return (netif_ && uptarget_ && downtarget_); 
00322         }
00323         int index_;             // MAC address
00324         double bandwidth_;      // channel bitrate
00325         double delay_;          // MAC overhead
00326         int abstract_;         //   MAC support for abstract LAN 
00327         
00328         Phy *netif_;            // network interface
00329         MrclTap *tap_;              // tap agent
00330         LL *ll_;                // LL this MAC is connected to
00331         Channel *channel_;      // channel this MAC is connected to
00332 
00333         Handler* callback_;     // callback for end-of-transmission
00334         MrclMacHandlerResume hRes_;     // resume handler
00335         MrclMacHandlerSend hSend_;      // handle delay send due to busy channel
00336         Event intr_;
00337 
00338         /*
00339          * Internal MAC State
00340          */
00341         MacState state_;        // MAC's current state
00342         Packet *pktRx_;
00343         Packet *pktTx_;
00344 };
00345 
00346 
00347 
00348 class MPHY_MIB {
00349 public:
00350         MPHY_MIB(MrclMac802_11 *parent);
00351 
00352         inline u_int32_t getCWMin() { return(CWMin); }
00353         inline u_int32_t getCWMax() { return(CWMax); }
00354         inline double getSlotTime() { return(SlotTime); }
00355         inline double getSIFS() { return(SIFSTime); }
00356         inline double getPIFS() { return(SIFSTime + SlotTime); }
00357         inline double getDIFS() { return(SIFSTime + 2 * SlotTime); }
00358         inline double getEIFS() {
00359                 // see (802.11-1999, 9.2.10)
00360                 return(SIFSTime + getDIFS()
00361                        + (8 *  getACKlen())/PLCPDataRate);
00362         }
00363         inline u_int32_t getPreambleLength() { return(PreambleLength); }
00364         inline double getPLCPDataRate() { return(PLCPDataRate); }
00365         
00366         inline u_int32_t getPLCPhdrLen() {
00367                 return((PreambleLength + PLCPHeaderLength) >> 3);
00368         }
00369 
00370         inline u_int32_t getHdrLen11() {
00371                 return(getPLCPhdrLen() + sizeof(struct hdr_mac802_11)
00372                        + ETHER_FCS_LEN);
00373         }
00374         
00375         inline u_int32_t getRTSlen() {
00376                 return(getPLCPhdrLen() + sizeof(struct rts_frame));
00377         }
00378         
00379         inline u_int32_t getCTSlen() {
00380                 return(getPLCPhdrLen() + sizeof(struct cts_frame));
00381         }
00382         
00383         inline u_int32_t getACKlen() {
00384                 return(getPLCPhdrLen() + sizeof(struct ack_frame));
00385         }
00386 
00387  private:
00388 
00389 
00390 
00391 
00392         u_int32_t       CWMin;
00393         u_int32_t       CWMax;
00394         double          SlotTime;
00395         double          SIFSTime;
00396         u_int32_t       PreambleLength;
00397         u_int32_t       PLCPHeaderLength;
00398         double          PLCPDataRate;
00399 };
00400 
00401 
00402 /*
00403  * IEEE 802.11 Spec, section 11.4.4.2
00404  *      - default values for the MAC Attributes
00405  */
00406 #define MAC_FragmentationThreshold      2346            // bytes
00407 #define MAC_MaxTransmitMSDULifetime     512             // time units
00408 #define MAC_MaxReceiveLifetime          512             // time units
00409 
00410 class MMAC_MIB {
00411 public:
00412 
00413         MMAC_MIB(MrclMac802_11 *parent);
00414 
00415 private:
00416         u_int32_t       RTSThreshold;
00417         u_int32_t       ShortRetryLimit;
00418         u_int32_t       LongRetryLimit;
00419 public:
00420         u_int32_t       FailedCount;    
00421         u_int32_t       RTSFailureCount;
00422         u_int32_t       ACKFailureCount;
00423  public:
00424        inline u_int32_t getRTSThreshold() { return(RTSThreshold);}
00425        inline u_int32_t getShortRetryLimit() { return(ShortRetryLimit);}
00426        inline u_int32_t getLongRetryLimit() { return(LongRetryLimit);}
00427 };
00428 
00429 
00430 /* ======================================================================
00431    The following destination class is used for duplicate detection.
00432    ====================================================================== */
00433 // class Host {
00434 // public:
00435 //      LIST_ENTRY(Host) link;
00436 //      u_int32_t       index;
00437 //      u_int32_t       seqno;
00438 // };
00439 
00440 
00441 /* ======================================================================
00442    The Miracle 802.11 MAC class.
00443    ====================================================================== */
00444 class MrclMac802_11 : public Mac {
00445         friend class MrclDeferTimer;
00446 
00447 
00448         friend class MrclBackoffTimer;
00449         friend class MrclIFTimer;
00450         friend class MrclNavTimer;
00451         friend class MrclRxTimer;
00452         friend class MrclTxTimer;
00453 public:
00454         MrclMac802_11();
00455         void            recv(Packet *p, Handler *h);
00456         inline int      hdr_dst(char* hdr, int dst = -2);
00457         inline int      hdr_src(char* hdr, int src = -2);
00458         inline int      hdr_type(char* hdr, u_int16_t type = 0);
00459         
00460         inline int bss_id() { return bss_id_; }
00461         
00462         // Added by Sushmita to support event tracing
00463         void trace_event(char *, Packet *);
00464         EventTrace *et_;
00465 
00466 protected:
00467         void    backoffHandler(void);
00468         void    deferHandler(void);
00469         void    navHandler(void);
00470         void    recvHandler(void);
00471         void    sendHandler(void);
00472         void    txHandler(void);
00473 
00474 private:
00475         int             command(int argc, const char*const* argv);
00476 
00477         /*
00478          * Called by the timers.
00479          */
00480         void            recv_timer(void);
00481         void            send_timer(void);
00482         int             check_pktCTRL();
00483         int             check_pktRTS();
00484         int             check_pktTx();
00485 
00486         /*
00487          * Packet Transmission Functions.
00488          */
00489         void    send(Packet *p, Handler *h);
00490         void    sendRTS(int dst);
00491         void    sendCTS(int dst, double duration);
00492         void    sendACK(int dst);
00493         void    sendDATA(Packet *p);
00494         void    RetransmitRTS();
00495         void    RetransmitDATA();
00496 
00497         /*
00498          * Packet Reception Functions.
00499          */
00500         void    recvRTS(Packet *p);
00501         void    recvCTS(Packet *p);
00502         void    recvACK(Packet *p);
00503         void    recvDATA(Packet *p);
00504 
00505         void            capture(Packet *p);
00506         void            collision(Packet *p);
00507         void            discard(Packet *p, const char* why);
00508         void            rx_resume(void);
00509         void            tx_resume(void);
00510 
00511         inline int      is_idle(void);
00512 
00513         /*
00514          * Debugging Functions.
00515          */
00516         void            trace_pkt(Packet *p);
00517         void            dump(char* fname);
00518 
00519         inline int initialized() {      
00520                 return (cache_ && logtarget_
00521                         && Mac::initialized());
00522         }
00523 
00524         inline void mac_log(Packet *p) {
00525                 logtarget_->recv(p, (Handler*) 0);
00526         }
00527 
00528         double txtime(Packet *p);
00529         double txtime(double psz, double drt);
00530         double txtime(int bytes) { /* clobber inherited txtime() */ abort(); return 0;}
00531 
00532         inline void transmit(Packet *p, double timeout);
00533         inline void checkBackoffTimer(void);
00534         inline void postBackoff(int pri);
00535         inline void setRxState(MacState newState);
00536         inline void setTxState(MacState newState);
00537 
00538 
00539         inline void inc_cw() {
00540                 cw_ = (cw_ << 1) + 1;
00541                 if(cw_ > phymib_.getCWMax())
00542                         cw_ = phymib_.getCWMax();
00543         }
00544         inline void rst_cw() { cw_ = phymib_.getCWMin(); }
00545 
00546         inline double sec(double t) { return(t *= 1.0e-6); }
00547         inline u_int16_t usec(double t) {
00548                 u_int16_t us = (u_int16_t)floor((t *= 1e6) + 0.5);
00549                 return us;
00550         }
00551         inline void set_nav(u_int16_t us) {
00552                 double now = Scheduler::instance().clock();
00553                 double t = us * 1e-6;
00554                 if((now + t) > nav_) {
00555                         nav_ = now + t;
00556                         if(mhNav_.busy())
00557                                 mhNav_.stop();
00558                         mhNav_.start(t);
00559                 }
00560         }
00561 
00562 protected:
00563         MPHY_MIB         phymib_;
00564         MMAC_MIB         macmib_;
00565 
00566        /* the macaddr of my AP in BSS mode; for IBSS mode
00567         * this is set to a reserved value IBSS_ID - the
00568         * MAC_BROADCAST reserved value can be used for this
00569         * purpose
00570         */
00571        int     bss_id_;
00572        enum    {IBSS_ID=MAC_BROADCAST};
00573 
00574 
00575 private:
00576         double          basicRate_;
00577         double          dataRate_;
00578         
00579         /*
00580          * Mac Timers
00581          */
00582         MrclIFTimer             mhIF_;          // interface timer
00583         MrclNavTimer    mhNav_;         // NAV timer
00584         MrclRxTimer             mhRecv_;                // incoming packets
00585         MrclTxTimer             mhSend_;                // outgoing packets
00586 
00587         MrclDeferTimer  mhDefer_;       // defer timer
00588         MrclBackoffTimer        mhBackoff_;     // backoff timer
00589 
00590         /* ============================================================
00591            Internal MAC State
00592            ============================================================ */
00593         double          nav_;           // Network Allocation Vector
00594 
00595         MacState        rx_state_;      // incoming state (MAC_RECV or MMAC_IDLE)
00596         MacState        tx_state_;      // outgoint state
00597         int             tx_active_;     // transmitter is ACTIVE
00598 
00599         Packet          *eotPacket_;    // copy for eot callback
00600 
00601         Packet          *pktRTS_;       // outgoing RTS packet
00602         Packet          *pktCTRL_;      // outgoing non-RTS packet
00603 
00604         u_int32_t       cw_;            // Contention Window
00605         u_int32_t       ssrc_;          // STA Short Retry Count
00606         u_int32_t       slrc_;          // STA Long Retry Count
00607 
00608         int             min_frame_len_;
00609 
00610         NsObject*       logtarget_;
00611         NsObject*       EOTtarget_;     // given a copy of packet at TX end
00612 
00613 
00614 
00615 
00616         /* ============================================================
00617            Duplicate Detection state
00618            ============================================================ */
00619         u_int16_t       sta_seqno_;     // next seqno that I'll use
00620         int             cache_node_count_;
00621         Host            *cache_;
00622 };
00623 
00624 #endif /* __mac_80211_h__ */
00625 

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2