mac.h

Go to the documentation of this file.
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: /nfs/jade/vint/CVSROOT/ns-2/mac/mac.h,v 1.36 2005/07/27 01:13:44 tomh 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_CTS     = 0x0400,
00087     MAC_ACK     = 0x0800,
00088     MAC_COLL    = 0x1000
00089 };
00090 
00091 enum MacFrameType {
00092     MF_BEACON   = 0x0008, // beaconing
00093     MF_CONTROL  = 0x0010, // used as mask for control frame
00094     MF_SLOTS    = 0x001a, // announce slots open for contention
00095     MF_RTS      = 0x001b, // request to send
00096     MF_CTS      = 0x001c, // clear to send, grant
00097     MF_ACK      = 0x001d, // acknowledgement
00098     MF_CF_END   = 0x001e, // contention free period end
00099     MF_POLL     = 0x001f, // polling
00100     MF_DATA     = 0x0020, // also used as mask for data frame
00101     MF_DATA_ACK = 0x0021  // ack for data frames
00102 };
00103 
00104 struct hdr_mac {
00105     MacFrameType ftype_;    // frame type
00106     int macSA_;     // source MAC address
00107     int macDA_;     // destination MAC address
00108     u_int16_t hdr_type_;     // mac_hdr type
00109 
00110     double txtime_;     // transmission time
00111     double sstime_;     // slot start time
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     // Header access methods
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    Objects that want to promiscously listen to the packets before
00138    address filtering must inherit from class Tap in order to plug into
00139    the tap
00140    =================================================================*/
00141 
00142 class Tap {
00143 public:
00144     virtual ~Tap () {}
00145     virtual void tap(const Packet *p) = 0;
00146     // tap is given all packets received by the host.
00147     // it must not alter or free the pkt.  If you want to frob it, copy it.
00148 };
00149 
00150 
00151 class MacHandlerResume : public Handler {
00152 public:
00153     MacHandlerResume(Mac* m) : mac_(m) {}
00154     void handle(Event*);
00155 protected:
00156     Mac* mac_;
00157 };
00158 
00159 class MacHandlerSend : public Handler {
00160 public:
00161     MacHandlerSend(Mac* m) : mac_(m) {}
00162     void handle(Event*);
00163 protected:
00164     Mac* mac_;
00165 };
00166 
00167 
00168 /* ==================================================================
00169    MAC data structure
00170    ================================================================*/
00171 
00172 class Mac : public BiConnector {
00173 public:
00174     Mac();
00175     virtual void recv(Packet* p, Handler* h);
00176     virtual void sendDown(Packet* p);
00177     virtual void sendUp(Packet *p);
00178 
00179     virtual void resume(Packet* p = 0);
00180     virtual void installTap(Tap *t) { tap_ = t; }
00181     
00182     inline double txtime(int bytes) {
00183         return (8. * bytes / bandwidth_);
00184     }
00185     inline double txtime(Packet* p) {
00186         return 8. * (MAC_HDR_LEN + \
00187                  (HDR_CMN(p))->size()) / bandwidth_;
00188     }
00189     inline double bandwidth() const { return bandwidth_; }
00190     
00191     inline int addr() { return index_; }
00192     inline MacState state() { return state_; }
00193     inline MacState state(int m) { return state_ = (MacState) m; }
00194     
00195         //mac methods to set dst, src and hdt_type in pkt hdrs.
00196     // note: -1 is the broadcast mac addr.
00197     virtual inline int hdr_dst(char* hdr, int dst = -2) {
00198         struct hdr_mac *dh = (struct hdr_mac*) hdr;
00199         if(dst > -2)
00200             dh->macDA_ = dst;
00201         return dh->macDA();
00202     }
00203     virtual inline int hdr_src(char* hdr, int src = -2) {
00204         struct hdr_mac *dh = (struct hdr_mac*) hdr;
00205         if(src > -2)
00206             dh->macSA_ = src;
00207         return dh->macSA();
00208     }
00209     virtual inline int hdr_type(char *hdr, u_int16_t type = 0) {
00210         struct hdr_mac *dh = (struct hdr_mac*) hdr;
00211         if (type)
00212             dh->hdr_type_ = type;
00213         return dh->hdr_type();
00214     }
00215 
00216 private:
00217         void mac_log(Packet *p) {
00218                 logtarget_->recv(p, (Handler*) 0);
00219         }
00220         NsObject*       logtarget_;
00221 
00222 protected:
00223     int command(int argc, const char*const* argv);
00224     virtual int initialized() { 
00225         return (netif_ && uptarget_ && downtarget_); 
00226     }
00227     int index_;     // MAC address
00228     double bandwidth_;      // channel bitrate
00229     double delay_;      // MAC overhead
00230     int abstract_;         //   MAC support for abstract LAN 
00231         
00232     Phy *netif_;            // network interface
00233         Tap *tap_;              // tap agent
00234     LL *ll_;                // LL this MAC is connected to
00235     Channel *channel_;  // channel this MAC is connected to
00236 
00237     Handler* callback_; // callback for end-of-transmission
00238     MacHandlerResume hRes_; // resume handler
00239     MacHandlerSend hSend_;  // handle delay send due to busy channel
00240     Event intr_;
00241 
00242     /*
00243      * Internal MAC State
00244      */
00245     MacState state_;    // MAC's current state
00246     Packet *pktRx_;
00247     Packet *pktTx_;
00248 };
00249 
00250 #endif

Generated on Tue Mar 6 16:47:47 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6