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_BCN = 0x0300,
00087 MAC_CTS = 0x0400,
00088 MAC_ACK = 0x0800,
00089 MAC_COLL = 0x1000,
00090 MAC_MGMT = 0x1001
00091 };
00092
00093 enum MacFrameType {
00094 MF_BEACON = 0x0008,
00095 MF_CONTROL = 0x0010,
00096 MF_SLOTS = 0x001a,
00097 MF_RTS = 0x001b,
00098 MF_CTS = 0x001c,
00099 MF_ACK = 0x001d,
00100 MF_CF_END = 0x001e,
00101 MF_POLL = 0x001f,
00102 MF_DATA = 0x0020,
00103 MF_DATA_ACK = 0x0021
00104 };
00105
00106 struct hdr_mac {
00107 MacFrameType ftype_;
00108 int macSA_;
00109 int macDA_;
00110 u_int16_t hdr_type_;
00111
00112 double txtime_;
00113 double sstime_;
00114
00115 int padding_;
00116
00117 inline void set(MacFrameType ft, int sa, int da=-1) {
00118 ftype_ = ft;
00119 macSA_ = sa;
00120 if (da != -1) macDA_ = da;
00121 }
00122 inline MacFrameType& ftype() { return ftype_; }
00123 inline int& macSA() { return macSA_; }
00124 inline int& macDA() { return macDA_; }
00125 inline u_int16_t& hdr_type() {return hdr_type_; }
00126
00127 inline double& txtime() { return txtime_; }
00128 inline double& sstime() { return sstime_; }
00129
00130
00131 static int offset_;
00132 inline static int& offset() { return offset_; }
00133 inline static hdr_mac* access(const Packet* p) {
00134 return (hdr_mac*) p->access(offset_);
00135 }
00136 };
00137
00138
00139
00140
00141
00142
00143
00144 class Tap {
00145 public:
00146 virtual ~Tap () {}
00147 virtual void tap(const Packet *p) = 0;
00148
00149
00150 };
00151
00152
00153 class MacHandlerResume : public Handler {
00154 public:
00155 MacHandlerResume(Mac* m) : mac_(m) {}
00156 void handle(Event*);
00157 protected:
00158 Mac* mac_;
00159 };
00160
00161 class MacHandlerSend : public Handler {
00162 public:
00163 MacHandlerSend(Mac* m) : mac_(m) {}
00164 void handle(Event*);
00165 protected:
00166 Mac* mac_;
00167 };
00168
00169
00170
00171
00172
00173
00174 class Mac : public BiConnector {
00175 public:
00176 Mac();
00177 virtual void recv(Packet* p, Handler* h);
00178 virtual void sendDown(Packet* p);
00179 virtual void sendUp(Packet *p);
00180
00181 virtual void resume(Packet* p = 0);
00182 virtual void installTap(Tap *t) { tap_ = t; }
00183
00184 inline double txtime(int bytes) {
00185 return (8. * bytes / bandwidth_);
00186 }
00187 inline double txtime(Packet* p) {
00188 return 8. * (MAC_HDR_LEN + \
00189 (HDR_CMN(p))->size()) / bandwidth_;
00190 }
00191 inline double bandwidth() const { return bandwidth_; }
00192
00193 inline int addr() { return index_; }
00194 inline MacState state() { return state_; }
00195 inline MacState state(int m) { return state_ = (MacState) m; }
00196
00197
00198
00199 virtual inline int hdr_dst(char* hdr, int dst = -2) {
00200 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00201 if(dst > -2)
00202 dh->macDA_ = dst;
00203 return dh->macDA();
00204 }
00205 virtual inline int hdr_src(char* hdr, int src = -2) {
00206 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00207 if(src > -2)
00208 dh->macSA_ = src;
00209 return dh->macSA();
00210 }
00211 virtual inline int hdr_type(char *hdr, u_int16_t type = 0) {
00212 struct hdr_mac *dh = (struct hdr_mac*) hdr;
00213 if (type)
00214 dh->hdr_type_ = type;
00215 return dh->hdr_type();
00216 }
00217
00218 private:
00219 void mac_log(Packet *p) {
00220 logtarget_->recv(p, (Handler*) 0);
00221 }
00222 NsObject* logtarget_;
00223
00224 protected:
00225 int command(int argc, const char*const* argv);
00226 virtual int initialized() {
00227 return (netif_ && uptarget_ && downtarget_);
00228 }
00229 int index_;
00230 double bandwidth_;
00231 double delay_;
00232 int abstract_;
00233
00234 Phy *netif_;
00235 Tap *tap_;
00236 LL *ll_;
00237 Channel *channel_;
00238
00239 Handler* callback_;
00240 MacHandlerResume hRes_;
00241 MacHandlerSend hSend_;
00242 Event intr_;
00243
00244
00245
00246
00247 MacState state_;
00248 Packet *pktRx_;
00249 Packet *pktTx_;
00250 };
00251
00252 #endif