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 #ifndef ns_mac_h
00040 #define ns_mac_h
00041
00042 #include <assert.h>
00043 #include "bi-connector.h"
00044 #include "packet.h"
00045 #include "ip.h"
00046 #include "route.h"
00047 #include "ll.h"
00048 #include "phy.h"
00049 #include "marshall.h"
00050 #include "channel.h"
00051
00052 class Channel;
00053
00054 #define ZERO 0.00000
00055
00056
00057
00058
00059
00060 #define EF_COLLISION 2 // collision error flag
00061
00062
00063
00064
00065
00066 #define ETHER_ADDR(x) (GET4BYTE(x))
00067
00068 #define MAC_HDR_LEN 64
00069
00070 #define MAC_BROADCAST ((u_int32_t) 0xffffffff)
00071 #define BCAST_ADDR -1
00072
00073 #define ETHER_ADDR_LEN 6
00074 #define ETHER_TYPE_LEN 2
00075 #define ETHER_FCS_LEN 4
00076
00077 #define ETHERTYPE_IP 0x0800
00078 #define ETHERTYPE_ARP 0x0806
00079
00080 enum MacState {
00081 MAC_IDLE = 0x0000,
00082 MAC_POLLING = 0x0001,
00083 MAC_RECV = 0x0010,
00084 MAC_SEND = 0x0100,
00085 MAC_RTS = 0x0200,
00086 MAC_CTS = 0x0400,
00087 MAC_ACK = 0x0800,
00088 MAC_COLL = 0x1000
00089 };
00090
00091 enum MacFrameType {
00092 MF_BEACON = 0x0008,
00093 MF_CONTROL = 0x0010,
00094 MF_SLOTS = 0x001a,
00095 MF_RTS = 0x001b,
00096 MF_CTS = 0x001c,
00097 MF_ACK = 0x001d,
00098 MF_CF_END = 0x001e,
00099 MF_POLL = 0x001f,
00100 MF_DATA = 0x0020,
00101 MF_DATA_ACK = 0x0021
00102 };
00103
00104 struct hdr_mac {
00105 MacFrameType ftype_;
00106 int macSA_;
00107 int macDA_;
00108 u_int16_t hdr_type_;
00109
00110 double txtime_;
00111 double sstime_;
00112
00113 int padding_;
00114
00115 inline void set(MacFrameType ft, int sa, int da=-1) {
00116 ftype_ = ft;
00117 macSA_ = sa;
00118 if (da != -1) macDA_ = da;
00119 }
00120 inline MacFrameType& ftype() { return ftype_; }
00121 inline int& macSA() { return macSA_; }
00122 inline int& macDA() { return macDA_; }
00123 inline u_int16_t& hdr_type() {return hdr_type_; }
00124
00125 inline double& txtime() { return txtime_; }
00126 inline double& sstime() { return sstime_; }
00127
00128
00129 static int offset_;
00130 inline static int& offset() { return offset_; }
00131 inline static hdr_mac* access(const Packet* p) {
00132 return (hdr_mac*) p->access(offset_);
00133 }
00134 };
00135
00136
00137
00138
00139
00140
00141
00142 class Tap {
00143 public:
00144 virtual void tap(const Packet *p) = 0;
00145
00146
00147 };
00148
00149
00150 class MacHandlerResume : public Handler {
00151 public:
00152 MacHandlerResume(Mac* m) : mac_(m) {}
00153 void handle(Event*);
00154 protected:
00155 Mac* mac_;
00156 };
00157
00158 class MacHandlerSend : public Handler {
00159 public:
00160 MacHandlerSend(Mac* m) : mac_(m) {}
00161 void handle(Event*);
00162 protected:
00163 Mac* mac_;
00164 };
00165
00166
00167
00168
00169
00170
00171 class Mac : public BiConnector {
00172 public:
00173 Mac();
00174 virtual void recv(Packet* p, Handler* h);
00175 virtual void sendDown(Packet* p);
00176 virtual void sendUp(Packet *p);
00177
00178 virtual void resume(Packet* p = 0);
00179 virtual void installTap(Tap *t) { tap_ = t; }
00180
00181 inline double txtime(int bytes) {
00182 return (8. * bytes / bandwidth_);
00183 }
00184 inline double txtime(Packet* p) {
00185 return 8. * (MAC_HDR_LEN + \
00186 (HDR_CMN(p))->size()) / bandwidth_;
00187 }
00188 inline double bandwidth() const { return bandwidth_; }
00189
00190 inline int addr() { return index_; }
00191 inline MacState state() { return state_; }
00192 inline MacState state(int m) { return state_ = (MacState) m; }
00193
00194
00195
00196 virtual inline int hdr_dst(char* hdr, int dst = -2) {
00197 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00198 if(dst > -2)
00199 dh->macDA_ = dst;
00200 return dh->macDA();
00201 }
00202 virtual inline int hdr_src(char* hdr, int src = -2) {
00203 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00204 if(src > -2)
00205 dh->macSA_ = src;
00206 return dh->macSA();
00207 }
00208 virtual inline int hdr_type(char *hdr, u_int16_t type = 0) {
00209 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00210 if (type)
00211 dh->hdr_type_ = type;
00212 return dh->hdr_type();
00213 }
00214
00215 private:
00216 void mac_log(Packet *p) {
00217 logtarget_->recv(p, (Handler*) 0);
00218 }
00219 NsObject* logtarget_;
00220
00221 protected:
00222 int command(int argc, const char*const* argv);
00223 virtual int initialized() {
00224 return (netif_ && uptarget_ && downtarget_);
00225 }
00226 int index_;
00227 double bandwidth_;
00228 double delay_;
00229 int abstract_;
00230
00231 Phy *netif_;
00232 Tap *tap_;
00233 LL *ll_;
00234 Channel *channel_;
00235
00236 Handler* callback_;
00237 MacHandlerResume hRes_;
00238 MacHandlerSend hSend_;
00239 Event intr_;
00240
00241
00242
00243
00244 MacState state_;
00245 Packet *pktRx_;
00246 Packet *pktTx_;
00247 };
00248
00249 #endif