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_mrclmac_80211_h
00041 #define ns_mrclmac_80211_h
00042
00043
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
00066
00067
00068 #define EF_COLLISION 2 // collision error flag
00069
00070
00071
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
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
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
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
00243
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
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
00292
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_;
00324 double bandwidth_;
00325 double delay_;
00326 int abstract_;
00327
00328 Phy *netif_;
00329 MrclTap *tap_;
00330 LL *ll_;
00331 Channel *channel_;
00332
00333 Handler* callback_;
00334 MrclMacHandlerResume hRes_;
00335 MrclMacHandlerSend hSend_;
00336 Event intr_;
00337
00338
00339
00340
00341 MacState 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
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
00404
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
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
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
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
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
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
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
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) { 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
00567
00568
00569
00570
00571 int bss_id_;
00572 enum {IBSS_ID=MAC_BROADCAST};
00573
00574
00575 private:
00576 double basicRate_;
00577 double dataRate_;
00578
00579
00580
00581
00582 MrclIFTimer mhIF_;
00583 MrclNavTimer mhNav_;
00584 MrclRxTimer mhRecv_;
00585 MrclTxTimer mhSend_;
00586
00587 MrclDeferTimer mhDefer_;
00588 MrclBackoffTimer mhBackoff_;
00589
00590
00591
00592
00593 double nav_;
00594
00595 MacState rx_state_;
00596 MacState tx_state_;
00597 int tx_active_;
00598
00599 Packet *eotPacket_;
00600
00601 Packet *pktRTS_;
00602 Packet *pktCTRL_;
00603
00604 u_int32_t cw_;
00605 u_int32_t ssrc_;
00606 u_int32_t slrc_;
00607
00608 int min_frame_len_;
00609
00610 NsObject* logtarget_;
00611 NsObject* EOTtarget_;
00612
00613
00614
00615
00616
00617
00618
00619 u_int16_t sta_seqno_;
00620 int cache_node_count_;
00621 Host *cache_;
00622 };
00623
00624 #endif
00625