Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

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.35 2000/12/20 10:11:36 alefiyah 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 void tap(const Packet *p) = 0;
00145         // tap is given all packets received by the host.
00146         // it must not alter or free the pkt.  If you want to frob it, copy it.
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    MAC data structure
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         //mac methods to set dst, src and hdt_type in pkt hdrs.
00195         // note: -1 is the broadcast mac addr.
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_;             // MAC address
00227         double bandwidth_;      // channel bitrate
00228         double delay_;          // MAC overhead
00229         int abstract_;         //   MAC support for abstract LAN 
00230         
00231         Phy *netif_;            // network interface
00232         Tap *tap_;              // tap agent
00233         LL *ll_;                // LL this MAC is connected to
00234         Channel *channel_;      // channel this MAC is connected to
00235 
00236         Handler* callback_;     // callback for end-of-transmission
00237         MacHandlerResume hRes_; // resume handler
00238         MacHandlerSend hSend_;  // handle delay send due to busy channel
00239         Event intr_;
00240 
00241         /*
00242          * Internal MAC State
00243          */
00244         MacState state_;        // MAC's current state
00245         Packet *pktRx_;
00246         Packet *pktTx_;
00247 };
00248 
00249 #endif

Generated on Tue Apr 20 12:14:23 2004 for NS2.26SourcesOriginal by doxygen 1.3.3