mac-802_11.h
上传用户:sdhqmy
上传日期:2015-12-07
资源大小:63k
文件大小:16k
- /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*-
- *
- * Copyright (c) 1997 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the Computer Systems
- * Engineering Group at Lawrence Berkeley Laboratory.
- * 4. Neither the name of the University nor of the Laboratory may be used
- * to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * $Header: /nfs/jade/vint/CVSROOT/ns-2/mac/mac-802_11.h,v 1.20 2002/03/14 01:12:53 haldar Exp $
- *
- * Ported from CMU/Monarch's code, nov'98 -Padma.
- * wireless-mac-802_11.h
- */
- #ifndef ns_mac_80211_h
- #define ns_mac_80211_h
- #include "mac.h"
- #include "mac-timers.h"
- #include "marshall.h"
- #define GET_ETHER_TYPE(x) GET2BYTE((x))
- #define SET_ETHER_TYPE(x,y) {u_int16_t t = (y); STORE2BYTE(x,&t);}
- /* ======================================================================
- Frame Formats
- ====================================================================== */
- //JUNGMIN
- #define NUMBER_OF_CHANNELS 3
- //end of JUNGMIN
- #define MAC_ProtocolVersion 0x00
- #define MAC_Type_Management 0x00
- #define MAC_Type_Control 0x01
- #define MAC_Type_Data 0x02
- #define MAC_Type_Reserved 0x03
- #define MAC_Subtype_RTS 0x0B
- #define MAC_Subtype_CTS 0x0C
- #define MAC_Subtype_ACK 0x0D
- #define MAC_Subtype_Data 0x00
- #define MAC_Subtype_DRTS 0x05
- #define MAC_Subtype_DCTS 0x06
- #define MAC_Subtype_DRSH 0x07
- //JUNGMIN
- #define MAC_Subtype_Beacon 0x08
- #define MAC_Subtype_ATIM 0x09
- #define MAC_Subtype_ATIMACK 0x0A
- #define MAC_Subtype_ATIMRSH 0x0E
- //end of JUNGMIN
- struct frame_control {
- u_char fc_subtype : 4;
- u_char fc_type : 2;
- u_char fc_protocol_version : 2;
- u_char fc_order : 1;
- u_char fc_wep : 1;
- u_char fc_more_data : 1;
- u_char fc_pwr_mgt : 1;
- u_char fc_retry : 1;
- u_char fc_more_frag : 1;
- u_char fc_from_ds : 1;
- u_char fc_to_ds : 1;
- u_char fc_channel : 4;
- //double fc_delay_measure;
- };
- struct rts_frame {
- struct frame_control rf_fc;
- u_int16_t rf_duration; //duration field to set nav
- u_char rf_ra[ETHER_ADDR_LEN];
- u_char rf_ta[ETHER_ADDR_LEN];
- u_char rf_fcs[ETHER_FCS_LEN];
- };
- struct cts_frame {
- struct frame_control cf_fc;
- u_int16_t cf_duration;
- u_char cf_ra[ETHER_ADDR_LEN];
- u_char cf_fcs[ETHER_FCS_LEN];
- };
- struct ack_frame {
- struct frame_control af_fc;
- u_int16_t af_duration;
- u_char af_ra[ETHER_ADDR_LEN]; //return address
- u_char af_fcs[ETHER_FCS_LEN];
- };
- //JUNGMIN
- struct beacon_frame {
- struct frame_control bf_fc;
- u_int16_t bf_duration;
- u_char bf_da[ETHER_ADDR_LEN]; //destination address
- u_char bf_sa[ETHER_ADDR_LEN]; //sender address
- u_char bf_bssid[ETHER_ADDR_LEN];
- u_int16_t bf_scontrol;
- unsigned long long bf_timestamp;
- u_int16_t bf_binterval;
- u_int16_t bf_capability;
-
- u_char bf_fcs[ETHER_FCS_LEN];
- };
- struct dca_rts_frame {
-
- struct frame_control drts_fc;
- u_int16_t drts_duration;
- u_char drts_da[ETHER_ADDR_LEN];
- u_char drts_sa[ETHER_ADDR_LEN];
- int drts_channel_list[NUMBER_OF_CHANNELS];
- double drts_data_transfer_time;
- u_char drts_fcs[ETHER_FCS_LEN];
- };
- struct dca_cts_frame {
- struct frame_control dcts_fc;
- u_int16_t dcts_duration;
- u_char dcts_ra[ETHER_ADDR_LEN];
- u_char dcts_sa[ETHER_ADDR_LEN];
- int dcts_selected_channel;
- double dcts_usage_time;
- double dcts_wait_time;
- u_char dcts_fcs[ETHER_FCS_LEN];
- };
- struct dca_rsh_frame {
- struct frame_control drsh_fc;
- u_int16_t drsh_duration;
- u_char drsh_ra[ETHER_ADDR_LEN];
- int drsh_confirmed_channel;
- double drsh_usage_time;
- u_char ahf_fcs[ETHER_FCS_LEN];
- };
- struct atim_frame {
- struct frame_control atf_fc; //frame control
- u_int16_t atf_duration; //duration for nav
- u_char atf_da[ETHER_ADDR_LEN]; //destination
- u_char atf_sa[ETHER_ADDR_LEN]; //sender
-
- int atf_channel_list[NUMBER_OF_CHANNELS];
- u_char atf_fcs[ETHER_FCS_LEN];
- };
- struct atimack_frame {
- struct frame_control aaf_fc;
- u_int16_t aaf_duration;
- u_char aaf_ra[ETHER_ADDR_LEN];
- u_char aaf_sa[ETHER_ADDR_LEN];
- int aaf_selected_channel;
- u_char aaf_fcs[ETHER_FCS_LEN];
- };
- struct atimrsh_frame {
- struct frame_control ahf_fc;
- u_int16_t ahf_duration;
- u_char ahf_ra[ETHER_ADDR_LEN];
- int ahf_confirmed_channel;
- u_char ahf_fcs[ETHER_FCS_LEN];
- };
- //end of JUNGMIN
- // XXX This header does not have its header access function because it shares
- // the same header space with hdr_mac.
- struct hdr_mac802_11 {
- struct frame_control dh_fc;
- u_int16_t dh_duration;
- u_char dh_da[ETHER_ADDR_LEN];
- u_char dh_sa[ETHER_ADDR_LEN];
- u_char dh_bssid[ETHER_ADDR_LEN];
- u_int16_t dh_scontrol;
- u_char dh_body[0]; // XXX Non-ANSI
- };
- /* ======================================================================
- Definitions
- ====================================================================== */
- #define PLCP_HDR_LEN
- ((phymib_->PreambleLength >> 3) +
- (phymib_->PLCPHeaderLength >> 3))
- #define ETHER_HDR_LEN11
- (PLCP_HDR_LEN +
- sizeof(struct hdr_mac802_11) +
- ETHER_FCS_LEN)
- #define ETHER_RTS_LEN
- (PLCP_HDR_LEN +
- sizeof(struct rts_frame))
- #define ETHER_CTS_LEN
- (PLCP_HDR_LEN +
- sizeof(struct cts_frame))
- #define ETHER_DRTS_LEN
- (PLCP_HDR_LEN +
- sizeof(struct dca_rts_frame))
- #define ETHER_DCTS_LEN
- (PLCP_HDR_LEN +
- sizeof(struct dca_cts_frame))
- #define ETHER_DRSH_LEN
- (PLCP_HDR_LEN +
- sizeof(struct dca_rsh_frame))
- #define ETHER_ACK_LEN
- (PLCP_HDR_LEN +
- sizeof(struct ack_frame))
- #define ETHER_ATIM_LEN
- (PLCP_HDR_LEN +
- sizeof(struct atim_frame))
- #define ETHER_ATIMACK_LEN
- (PLCP_HDR_LEN +
- sizeof(struct atimack_frame))
- #define ETHER_ATIMRSH_LEN
- (PLCP_HDR_LEN +
- sizeof(struct atimrsh_frame))
-
- /*
- * IEEE 802.11 Spec, section 15.3.2
- * - default values for the DSSS PHY MIB
- */
- #define DSSS_CWMin 31
- #define DSSS_CWMax 1023
- #define DSSS_SlotTime 0.000020 // 20us
- #define DSSS_CCATime 0.000015 // 15us
- #define DSSS_RxTxTurnaroundTime 0.000005 // 5us
- #define DSSS_SIFSTime 0.000010 // 10us
- #define DSSS_PreambleLength 144 // 144 bits
- #define DSSS_PLCPHeaderLength 48 // 48 bits
- #define DSSS_PLCPDataRate 1.0e6 // 1Mbps
- /* Must account for propagation delays added by the channel model when
- * calculating tx timeouts (as set in tcl/lan/ns-mac.tcl).
- * -- Gavin Holland, March 2002
- */
- #define DSSS_MaxPropagationDelay 0.000002 // 2us XXXX
- class PHY_MIB {
- public:
- u_int32_t CWMin;
- u_int32_t CWMax;
- double SlotTime;
- double CCATime;
- double RxTxTurnaroundTime;
- double SIFSTime;
- u_int32_t PreambleLength;
- u_int32_t PLCPHeaderLength;
- double PLCPDataRate;
- };
- /*
- * IEEE 802.11 Spec, section 11.4.4.2
- * - default values for the MAC Attributes
- */
- //#define MAC_RTSThreshold 3000 // bytes
- #define MAC_RTSThreshold 0 // bytes
- #define MAC_ShortRetryLimit 7 // retransmittions
- #define MAC_LongRetryLimit 4 // retransmissions
- #define MAC_FragmentationThreshold 2346 // bytes
- #define MAC_MaxTransmitMSDULifetime 512 // time units
- #define MAC_MaxReceiveLifetime 512 // time units
- class MAC_MIB {
- public:
- // MACAddress;
- // GroupAddresses;
- u_int32_t RTSThreshold;
- u_int32_t ShortRetryLimit;
- u_int32_t LongRetryLimit;
- u_int32_t FragmentationThreshold;
- u_int32_t MaxTransmitMSDULifetime;
- u_int32_t MaxReceiveLifetime;
- // ManufacturerID;
- // ProductID;
- //JUNGMIN
- u_int32_t dot11BeaconPeriod; //(ms)
- u_int32_t dot11ATIMWindowSize; //(ms)
- //end of JUNGMIN
- u_int32_t TransmittedFragmentCount;
- u_int32_t MulticastTransmittedFrameCount;
- u_int32_t FailedCount;
- u_int32_t RetryCount;
- u_int32_t MultipleRetryCount;
- u_int32_t FrameDuplicateCount;
- u_int32_t RTSSuccessCount;
- u_int32_t RTSFailureCount;
- u_int32_t ACKFailureCount;
- u_int32_t ReceivedFragmentCount;
- u_int32_t MulticastReceivedFrameCount;
- u_int32_t FCSErrorCount;
- };
- /* ======================================================================
- The following destination class is used for duplicate detection.
- ====================================================================== */
- class Host {
- public:
- LIST_ENTRY(Host) link;
- u_int32_t index;
- u_int32_t seqno;
- };
- /* ======================================================================
- The actual 802.11 MAC class.
- ====================================================================== */
- class Mac802_11 : public Mac {
- friend class DeferTimer;
- friend class BackoffTimer;
- friend class IFTimer;
- friend class NavTimer;
- friend class RxTimer;
- friend class RxDATATimer;
- friend class TxTimer;
- //JUNGMIN
- friend class BeaconTimer;
- friend class BeaconBackoffTimer;
- friend class ATIMWindowTimer;
-
- friend class ChannelUsageTimer;
- friend class WaitTimer;
- //end of JUNGMIN
- public:
- Mac802_11(PHY_MIB* p, MAC_MIB *m);
- void recv(Packet *p, Handler *h);
- inline int hdr_dst(char* hdr, int dst = -2);
- inline int hdr_src(char* hdr, int src = -2);
- inline int hdr_type(char* hdr, u_int16_t type = 0);
- int CheckDest(Packet* p);
- void sendATIM(int dst, Handler* h);
- double NextBeaconTime();
- int NearBeacon(Packet* p, int);
- protected:
- void backoffHandler(void);
- void deferHandler(void);
- void navHandler(void);
- //JUNGMIN
- void channelusageHandler(int);
- void waitHandler(void);
- void recvHandler(void);
- void recvDATAHandler(void);
- void sendHandler(void);
- void txHandler(void);
- void beaconHandler(void);
- void beaconBackoffHandler(void);
- void atimHandler(void);
- //end of JUNGMIN
- private:
- int command(int argc, const char*const* argv);
- /*
- * Called by the timers.
- */
- //JUNGMIN
- void recv_timer(void);
- void recvDATA_timer(void);
- void send_timer(void);
- int check_pktCTRL();
- int check_pktRTS();
- int check_pktTx();
- int check_pktPCF();
- int check_pktATIMRSH();
- int check_pktATIMACK();
- int check_pktATIM();
- //end of JUNGMIN
- /*
- * Packet Transmission Functions.
- */
- void send(Packet *p, Handler *h);
- void sendRTS(int dst);
- void sendCTS(int dst, double duration);
- //JUNGMIN
- void sendDRTS(int dst);
- void sendDCTS(int dst, double duration);
- void sendDRSH(int dst, double duration);
- //end of JUNGMIN
- void sendACK(int dst);
- void sendDATA(Packet *p);
- void RetransmitRTS();
- void RetransmitDATA();
- //JUNGMIN
- void prepare_beacon();
- void send_beacon();
- void beacon_received();
- void pcfSend(int type, int subtype);
- void sendATIMACK(int dst);
- void sendATIMRSH(int dst);
- void RetransmitATIM();
- //end of JUNGMIN
- /*
- * Packet Reception Functions.
- */
- void recvRTS(Packet *p);
- void recvCTS(Packet *p);
- void recvDRTS(Packet *p);
- void recvDCTS(Packet *p);
- void recvDRSH(Packet *p);
- void recvACK(Packet *p);
- void recvDATA(Packet *p);
- //JUNGMIN
- void recvATIM(Packet *p);
- void recvATIMACK(Packet *p);
- void recvATIMRSH(Packet *p);
- void recvOtherATIMACK(Packet *p);
- void recvOtherATIMRSH(Packet *p);
- void DCAUpdateCUL(int, double);
- void DCABuildFreeChannelList(int *);
- void recvOtherDRSH(Packet *p);
- void recvOtherDCTS(Packet *p);
- //end of JUNGMIN
- void capture(Packet *p);
- void collision(Packet *p);
- void discard(Packet *p, const char* why);
- void rx_resume(void);
- void dx_resume(void);
- void tx_resume(void);
- inline int is_idle(void);
- inline int is_channel_idle(int ch);
- /*
- * Debugging Functions.
- */
- void trace_pkt(Packet *p);
- void dump(char* fname);
- inline int initialized() {
- return (phymib_ && macmib_ && cache_ && logtarget_ &&
- Mac::initialized());
- }
- void mac_log(Packet *p) {
- logtarget_->recv(p, (Handler*) 0);
- }
- double txtime(Packet *p);
- double txtime(double psz, double drt);
- double txtime(int bytes) { /* clobber inherited txtime() */ abort(); }
- inline void inc_cw() {
- cw_ = (cw_ << 1) + 1;
- if(cw_ > phymib_->CWMax)
- cw_ = phymib_->CWMax;
- }
- inline void rst_cw() { cw_ = phymib_->CWMin; }
- inline double sec(double t) { return(t *= 1.0e-6); }
- inline u_int16_t usec(double t) {
- u_int16_t us = (u_int16_t)floor((t *= 1e6) + 0.5);
- /* u_int16_t us = (u_int16_t)rint(t *= 1e6); */
- return us;
- }
- inline void set_nav(u_int16_t us) {
- double now = Scheduler::instance().clock();
- double t = us * 1e-6;
- if((now + t) > nav_) {
- nav_ = now + t;
- if(mhNav_.busy())
- mhNav_.stop();
- mhNav_.start(t);
- }
- }
- //JUNGMIN
- void SetATIMWindow();
- void SelectChannel(struct atim_frame *);
- void ConfirmChannel(struct atimack_frame *);
- int DCASelectChannel(int*);
- double FindNearestChannelFreeTime();
- void ResetChannelInfo();
- void ResetDestInfo();
- void ResetNeighborChannelStateInfo();
- void ResetAtBeacon(); //combination of above 3
- int LookupDestState(int dst);
- void InsertDestState(int dst);
- void InsertNeighborChannelState(int dst, int channel);
- //end of JUNGMIN
- protected:
- PHY_MIB *phymib_;
- MAC_MIB *macmib_;
- private:
- double basicRate_;
- double dataRate_;
- /*
- * Mac Timers
- */
- IFTimer mhIF_; // interface timer
- NavTimer mhNav_; // NAV timer
- RxTimer mhRecv_; // incoming packets
- RxDATATimer mhRecvDATA_;
- TxTimer mhSend_; // outgoing packets
- //JUNGMIN
- BeaconTimer mhBeacon_;
- BeaconBackoffTimer mhBeaconBackoff_;
- ATIMWindowTimer mhATIMWindow_;
- ChannelUsageTimer *mhChannelUsage_;
- WaitTimer mhWait_;
- //end of JUNGMIN
- DeferTimer mhDefer_; // defer timer
- BackoffTimer mhBackoff_; // backoff timer
- /* ============================================================
- Internal MAC State
- ============================================================ */
- //JUNGMIN
- int fcl_[NUMBER_OF_CHANNELS];
- int dca_recved_fcl_[NUMBER_OF_CHANNELS];
- double dca_cul_[NUMBER_OF_CHANNELS];
- int selected_channel_;
- int active_channel_;
- int dest_state_[MAX_NODES];
- //int q_state_[MAX_NODES]; //moved to queue
- int channel_state_[MAX_NODES];
- int ready_for_atim_;
- double next_beacon_time_;
- int recved_bytes_;
-
- //end of JUNGMIN
- double nav_; // Network Allocation Vector
- MacState rx_state_; // incoming state (MAC_RECV or MAC_IDLE)
- MacState tx_state_; // outgoint state
- MacState dx_state_; // for dca
- int tx_active_; // transmitter is ACTIVE
- Packet *pktRTS_; // outgoing RTS packet
- Packet *pktCTRL_; // outgoing non-RTS packet
- Packet *pktRSH_;
- //JUNGMIN
- Packet *pktPCF_; // outgoing PCF stuff
- Packet *pktATIM_;
- Packet *pktATIMACK_;
- Packet *pktATIMRSH_;
- Packet *pktDx_;
- Packet *pktPending_;
- //end of JUNGMIN
- u_int32_t cw_; // Contention Window
- u_int32_t ssrc_; // STA Short Retry Count
- //JUNGMIN
- u_int32_t sarc_; // STA ATIM Retry Count
- //end of JUNGMIN
- u_int32_t slrc_; // STA Long Retry Count
- double sifs_; // Short Interface Space
- double pifs_; // PCF Interframe Space
- double difs_; // DCF Interframe Space
- double eifs_; // Extended Interframe Space
- double tx_sifs_;
- double tx_pifs_;
- double tx_difs_;
- int min_frame_len_;
- NsObject* logtarget_;
- //JUNGMIN
- int pc_;
- int btbs_; //beacon
- int atim_window_; //whether atim window is on or off
- int dca_;
- double total_delay_;
- int packet_count_;
- //end of JUNGMIN
-
- /* ============================================================
- Duplicate Detection state
- ============================================================ */
- u_int16_t sta_seqno_; // next seqno that I'll use
- int cache_node_count_;
- Host *cache_;
- };
- #endif /* __mac_80211_h__ */