00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef ns_mac_80211_h
00041 #define ns_mac_80211_h
00042
00043
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
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
00111
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];
00120 };
00121
00122
00123
00124
00125
00126
00127
00128
00129
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
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
00189
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
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
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
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
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
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
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
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) { 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
00352
00353
00354
00355
00356 int bss_id_;
00357 enum {IBSS_ID=MAC_BROADCAST};
00358
00359
00360 private:
00361 double basicRate_;
00362 double dataRate_;
00363
00364
00365
00366
00367 IFTimer mhIF_;
00368 NavTimer mhNav_;
00369 RxTimer mhRecv_;
00370 TxTimer mhSend_;
00371
00372 DeferTimer mhDefer_;
00373 BackoffTimer mhBackoff_;
00374
00375
00376
00377
00378 double nav_;
00379
00380 MacState rx_state_;
00381 MacState tx_state_;
00382 int tx_active_;
00383
00384 Packet *eotPacket_;
00385
00386 Packet *pktRTS_;
00387 Packet *pktCTRL_;
00388
00389 u_int32_t cw_;
00390 u_int32_t ssrc_;
00391 u_int32_t slrc_;
00392
00393 int min_frame_len_;
00394
00395 NsObject* logtarget_;
00396 NsObject* EOTtarget_;
00397
00398
00399
00400
00401
00402
00403
00404 u_int16_t sta_seqno_;
00405 int cache_node_count_;
00406 Host *cache_;
00407 };
00408
00409 #endif
00410