14.5.4 CSMA-based MAC

The CsmaMac../ns-2/mac-csma.cc extends the Mac class with new methods that implements carrier sense and backoff mechanisms. The CsmaMac::send() method detects when the channel becomes idle using Channel::txtime(). If the channel is busy, the MAC schedules the next carrier sense at the moment the channel turns idle. Once the channel is idle, the CsmaMac object initiates the contention period with Channel::contention(). At the end of the contention period, the endofContention() method is invoked. At this time, the basic CsmaMac just transmits the packet using Channel::send.

    class CsmaMac : public Mac {
    public:
        CsmaMac();
        void send(Packet* p);
        void resume(Packet* p = 0);
        virtual void endofContention(Packet* p);
        virtual void backoff(Handler* h, Packet* p, double delay=0);
                . . .
    };

    class CsmaCdMac : public CsmaMac {
    public:
        CsmaCdMac();
        void endofContention(Packet*);
    };

    class CsmaCaMac : public CsmaMac {
    public:
        CsmaCaMac();
        virtual void send(Packet*);
    };

The CsmaCdMac extends CsmaMac to carry out collision detection procedure of the CSMA/CD (Ethernet) protocol. When the channel signals the end of contention period, the endofContention method checks for collision using the Channel::collision() method. If there is a collision, the MAC invokes its backoff method to schedule the next carrier sense to retransmit the packet.

The CsmaCaMac extends the send method of CsmaMac to carry out the collision avoidance (CSMA/CA) procedure. Instead of transmitting immediately when the channel is idle, the CsmaCaMac object backs off a random number of slots, then transmits if the channel remains idle until the end of the backoff period.

Tom Henderson 2011-11-05