• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/mac/mac.h

00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *      This product includes software developed by the Daedalus Research
00017  *      Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * Contributed by Giao Nguyen, http://daedalus.cs.berkeley.edu/~gnguyen
00035  *
00036  * @(#) $Header: /cvsroot/nsnam/ns-2/mac/mac.h,v 1.37 2008/01/24 01:53:19 tom_henderson Exp $ (UCB)
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  * Medium Access Control (MAC)
00058  */
00059 
00060 #define EF_COLLISION 2          // collision error flag
00061 
00062 /* ======================================================================
00063    Defines / Macros used by all MACs.
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, // beaconing
00095         MF_CONTROL      = 0x0010, // used as mask for control frame
00096         MF_SLOTS        = 0x001a, // announce slots open for contention
00097         MF_RTS          = 0x001b, // request to send
00098         MF_CTS          = 0x001c, // clear to send, grant
00099         MF_ACK          = 0x001d, // acknowledgement
00100         MF_CF_END       = 0x001e, // contention free period end
00101         MF_POLL         = 0x001f, // polling
00102         MF_DATA         = 0x0020, // also used as mask for data frame
00103         MF_DATA_ACK     = 0x0021  // ack for data frames
00104 };
00105 
00106 struct hdr_mac {
00107         MacFrameType ftype_;    // frame type
00108         int macSA_;             // source MAC address
00109         int macDA_;             // destination MAC address
00110         u_int16_t hdr_type_;     // mac_hdr type
00111 
00112         double txtime_;         // transmission time
00113         double sstime_;         // slot start time
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         // Header access methods
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    Objects that want to promiscously listen to the packets before
00140    address filtering must inherit from class Tap in order to plug into
00141    the tap
00142    =================================================================*/
00143 
00144 class Tap {
00145 public:
00146         virtual ~Tap () {}
00147         virtual void tap(const Packet *p) = 0;
00148         // tap is given all packets received by the host.
00149         // it must not alter or free the pkt.  If you want to frob it, copy it.
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    MAC data structure
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         //mac methods to set dst, src and hdt_type in pkt hdrs.
00198         // note: -1 is the broadcast mac addr.
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_;             // MAC address
00230         double bandwidth_;      // channel bitrate
00231         double delay_;          // MAC overhead
00232         int abstract_;         //   MAC support for abstract LAN
00233 
00234         Phy *netif_;            // network interface
00235         Tap *tap_;              // tap agent
00236         LL *ll_;                // LL this MAC is connected to
00237         Channel *channel_;      // channel this MAC is connected to
00238 
00239         Handler* callback_;     // callback for end-of-transmission
00240         MacHandlerResume hRes_; // resume handler
00241         MacHandlerSend hSend_;  // handle delay send due to busy channel
00242         Event intr_;
00243 
00244         /*
00245          * Internal MAC State
00246          */
00247         MacState state_;        // MAC's current state
00248         Packet *pktRx_;
00249         Packet *pktTx_;
00250 };
00251 
00252 #endif

Generated on Tue Aug 10 2010 16:16:07 for ns-2.33 by  doxygen 1.7.1