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 #include "mac-timers.h"
00044 #include "marshall.h"
00045
00046 #define GET_ETHER_TYPE(x) GET2BYTE((x))
00047 #define SET_ETHER_TYPE(x,y) {u_int16_t t = (y); STORE2BYTE(x,&t);}
00048
00049
00050
00051
00052
00053 #define MAC_ProtocolVersion 0x00
00054
00055 #define MAC_Type_Management 0x00
00056 #define MAC_Type_Control 0x01
00057 #define MAC_Type_Data 0x02
00058 #define MAC_Type_Reserved 0x03
00059
00060 #define MAC_Subtype_RTS 0x0B
00061 #define MAC_Subtype_CTS 0x0C
00062 #define MAC_Subtype_ACK 0x0D
00063 #define MAC_Subtype_Data 0x00
00064
00065 struct frame_control {
00066 u_char fc_subtype : 4;
00067 u_char fc_type : 2;
00068 u_char fc_protocol_version : 2;
00069
00070 u_char fc_order : 1;
00071 u_char fc_wep : 1;
00072 u_char fc_more_data : 1;
00073 u_char fc_pwr_mgt : 1;
00074 u_char fc_retry : 1;
00075 u_char fc_more_frag : 1;
00076 u_char fc_from_ds : 1;
00077 u_char fc_to_ds : 1;
00078 };
00079
00080 struct rts_frame {
00081 struct frame_control rf_fc;
00082 u_int16_t rf_duration;
00083 u_char rf_ra[ETHER_ADDR_LEN];
00084 u_char rf_ta[ETHER_ADDR_LEN];
00085 u_char rf_fcs[ETHER_FCS_LEN];
00086 };
00087
00088 struct cts_frame {
00089 struct frame_control cf_fc;
00090 u_int16_t cf_duration;
00091 u_char cf_ra[ETHER_ADDR_LEN];
00092 u_char cf_fcs[ETHER_FCS_LEN];
00093 };
00094
00095 struct ack_frame {
00096 struct frame_control af_fc;
00097 u_int16_t af_duration;
00098 u_char af_ra[ETHER_ADDR_LEN];
00099 u_char af_fcs[ETHER_FCS_LEN];
00100 };
00101
00102
00103
00104 struct hdr_mac802_11 {
00105 struct frame_control dh_fc;
00106 u_int16_t dh_duration;
00107 u_char dh_da[ETHER_ADDR_LEN];
00108 u_char dh_sa[ETHER_ADDR_LEN];
00109 u_char dh_bssid[ETHER_ADDR_LEN];
00110 u_int16_t dh_scontrol;
00111 u_char dh_body[0];
00112 };
00113
00114
00115
00116
00117
00118
00119 #define PLCP_HDR_LEN \
00120 ((phymib_->PreambleLength >> 3) + \
00121 (phymib_->PLCPHeaderLength >> 3))
00122
00123 #define ETHER_HDR_LEN11 \
00124 (PLCP_HDR_LEN + \
00125 sizeof(struct hdr_mac802_11) + \
00126 ETHER_FCS_LEN)
00127
00128 #define ETHER_RTS_LEN \
00129 (PLCP_HDR_LEN + \
00130 sizeof(struct rts_frame))
00131
00132 #define ETHER_CTS_LEN \
00133 (PLCP_HDR_LEN + \
00134 sizeof(struct cts_frame))
00135
00136 #define ETHER_ACK_LEN \
00137 (PLCP_HDR_LEN + \
00138 sizeof(struct ack_frame))
00139
00140
00141
00142
00143
00144 #define DSSS_CWMin 31
00145 #define DSSS_CWMax 1023
00146 #define DSSS_SlotTime 0.000020 // 20us
00147 #define DSSS_CCATime 0.000015 // 15us
00148 #define DSSS_RxTxTurnaroundTime 0.000005 // 5us
00149 #define DSSS_SIFSTime 0.000010 // 10us
00150 #define DSSS_PreambleLength 144 // 144 bits
00151 #define DSSS_PLCPHeaderLength 48 // 48 bits
00152 #define DSSS_PLCPDataRate 1.0e6 // 1Mbps
00153
00154
00155
00156
00157
00158 #define DSSS_MaxPropagationDelay 0.000002 // 2us XXXX
00159
00160 class PHY_MIB {
00161 public:
00162 u_int32_t CWMin;
00163 u_int32_t CWMax;
00164 double SlotTime;
00165 double CCATime;
00166 double RxTxTurnaroundTime;
00167 double SIFSTime;
00168 u_int32_t PreambleLength;
00169 u_int32_t PLCPHeaderLength;
00170 double PLCPDataRate;
00171 };
00172
00173
00174
00175
00176
00177
00178
00179 #define MAC_RTSThreshold 0 // bytes
00180 #define MAC_ShortRetryLimit 7 // retransmittions
00181 #define MAC_LongRetryLimit 4 // retransmissions
00182 #define MAC_FragmentationThreshold 2346 // bytes
00183 #define MAC_MaxTransmitMSDULifetime 512 // time units
00184 #define MAC_MaxReceiveLifetime 512 // time units
00185
00186 class MAC_MIB {
00187 public:
00188
00189
00190 u_int32_t RTSThreshold;
00191 u_int32_t ShortRetryLimit;
00192 u_int32_t LongRetryLimit;
00193 u_int32_t FragmentationThreshold;
00194 u_int32_t MaxTransmitMSDULifetime;
00195 u_int32_t MaxReceiveLifetime;
00196
00197
00198
00199 u_int32_t TransmittedFragmentCount;
00200 u_int32_t MulticastTransmittedFrameCount;
00201 u_int32_t FailedCount;
00202 u_int32_t RetryCount;
00203 u_int32_t MultipleRetryCount;
00204 u_int32_t FrameDuplicateCount;
00205 u_int32_t RTSSuccessCount;
00206 u_int32_t RTSFailureCount;
00207 u_int32_t ACKFailureCount;
00208 u_int32_t ReceivedFragmentCount;
00209 u_int32_t MulticastReceivedFrameCount;
00210 u_int32_t FCSErrorCount;
00211 };
00212
00213
00214
00215
00216
00217 class Host {
00218 public:
00219 LIST_ENTRY(Host) link;
00220 u_int32_t index;
00221 u_int32_t seqno;
00222 };
00223
00224
00225
00226
00227
00228 class Mac802_11 : public Mac {
00229 friend class DeferTimer;
00230 friend class BackoffTimer;
00231 friend class IFTimer;
00232 friend class NavTimer;
00233 friend class RxTimer;
00234 friend class TxTimer;
00235 public:
00236 Mac802_11(PHY_MIB* p, MAC_MIB *m);
00237 void recv(Packet *p, Handler *h);
00238 inline int hdr_dst(char* hdr, int dst = -2);
00239 inline int hdr_src(char* hdr, int src = -2);
00240 inline int hdr_type(char* hdr, u_int16_t type = 0);
00241
00242 protected:
00243 void backoffHandler(void);
00244 void deferHandler(void);
00245 void navHandler(void);
00246 void recvHandler(void);
00247 void sendHandler(void);
00248 void txHandler(void);
00249
00250 private:
00251 int command(int argc, const char*const* argv);
00252
00253
00254
00255
00256 void recv_timer(void);
00257 void send_timer(void);
00258 int check_pktCTRL();
00259 int check_pktRTS();
00260 int check_pktTx();
00261
00262
00263
00264
00265 void send(Packet *p, Handler *h);
00266 void sendRTS(int dst);
00267 void sendCTS(int dst, double duration);
00268 void sendACK(int dst);
00269 void sendDATA(Packet *p);
00270 void RetransmitRTS();
00271 void RetransmitDATA();
00272
00273
00274
00275
00276 void recvRTS(Packet *p);
00277 void recvCTS(Packet *p);
00278 void recvACK(Packet *p);
00279 void recvDATA(Packet *p);
00280
00281 void capture(Packet *p);
00282 void collision(Packet *p);
00283 void discard(Packet *p, const char* why);
00284 void rx_resume(void);
00285 void tx_resume(void);
00286
00287 inline int is_idle(void);
00288
00289
00290
00291
00292 void trace_pkt(Packet *p);
00293 void dump(char* fname);
00294
00295 inline int initialized() {
00296 return (phymib_ && macmib_ && cache_ && logtarget_ &&
00297 Mac::initialized());
00298 }
00299 void mac_log(Packet *p) {
00300 logtarget_->recv(p, (Handler*) 0);
00301 }
00302 double txtime(Packet *p);
00303 double txtime(double psz, double drt);
00304 double txtime(int bytes) { abort(); }
00305
00306 inline void inc_cw() {
00307 cw_ = (cw_ << 1) + 1;
00308 if(cw_ > phymib_->CWMax)
00309 cw_ = phymib_->CWMax;
00310 }
00311 inline void rst_cw() { cw_ = phymib_->CWMin; }
00312 inline double sec(double t) { return(t *= 1.0e-6); }
00313 inline u_int16_t usec(double t) {
00314 u_int16_t us = (u_int16_t)floor((t *= 1e6) + 0.5);
00315
00316 return us;
00317 }
00318 inline void set_nav(u_int16_t us) {
00319 double now = Scheduler::instance().clock();
00320 double t = us * 1e-6;
00321
00322 if((now + t) > nav_) {
00323 nav_ = now + t;
00324 if(mhNav_.busy())
00325 mhNav_.stop();
00326 mhNav_.start(t);
00327 }
00328 }
00329
00330 protected:
00331 PHY_MIB *phymib_;
00332 MAC_MIB *macmib_;
00333
00334 private:
00335 double basicRate_;
00336 double dataRate_;
00337
00338
00339
00340
00341 IFTimer mhIF_;
00342 NavTimer mhNav_;
00343 RxTimer mhRecv_;
00344 TxTimer mhSend_;
00345
00346 DeferTimer mhDefer_;
00347 BackoffTimer mhBackoff_;
00348
00349
00350
00351
00352 double nav_;
00353
00354 MacState rx_state_;
00355 MacState tx_state_;
00356 int tx_active_;
00357
00358 Packet *pktRTS_;
00359 Packet *pktCTRL_;
00360
00361 u_int32_t cw_;
00362 u_int32_t ssrc_;
00363 u_int32_t slrc_;
00364 double sifs_;
00365 double pifs_;
00366 double difs_;
00367 double eifs_;
00368 double tx_sifs_;
00369 double tx_pifs_;
00370 double tx_difs_;
00371
00372 int min_frame_len_;
00373
00374 NsObject* logtarget_;
00375
00376
00377
00378
00379 u_int16_t sta_seqno_;
00380 int cache_node_count_;
00381 Host *cache_;
00382 };
00383
00384 #endif
00385