= Carrier-Sense Medium Access Reference Design (CSMAMAC) = The CSMAMAC is serves as the user-level MAC layer in [wiki:OFDMReferenceDesign reference design]. The basic algorithm is simple: * Do I have a packet to send? * If the medium is idle, send it, enter a timeout, and wait for an acknowledgment from the destination * If the medium is busy, enter a backoff period and wait for the medium to become idle * Did I receive a data packet? * If the packet pass checksum and is addressed to me, send an acknowledgment * Did no know acknowledgment happen during a timeout period? * If the maximum number of retransmits has not occurred, enter a backoff and try retransmitting * If the maximum number of retransmits has occurred, drop the packet * Did I wait through a backoff period? * If the medium is busy, retransmit, increment the total number of resends, enter a timeout, and wait for an acknowledgment from the destination * If the medium is busy, enter a backoff period and wait for the medium to become idle This simple algorithm encompasses much of the behavior in commercial 802.11 MAC/PHY chipsets. This algorithm lends itself nicely to a state diagram, which in turn, translates into C-code that can be executed in the PowerPC of the FPGA on WARP. In the following section, we track the "Life of a Packet" as it might journey through these states. == Life of a Packet == Below is the state-machine representation of the above algorithm. Each state can be "clicked" and is described in detail in the accompanying link. The up-to-date code can be found here: [source:/ResearchApps/MAC/CSMAMAC/csmaMac.c@L csmaMac.c]. The API documentation for the code can be found here: [http://warp.rice.edu/WARP_API/csma_mac_8c.html CSMAMAC API] {{{ #!html }}} ---- Note: Through all of the following descriptions, a "-" is used in place of arguments to functions when those arguments are not important to the overall purpose of the description. Howeverm, that is not to say that these arguments can be changed to ''anything'' and have the code still compile and run successfully. Care must be taken in modifying the reference design. === Idle === There are two purposes of this state: At startup must initialize the framework and WARP hardware, and in the steady state it must sit and wait for an event to take place. This state maps to the {{{ #!c int main() }}} function of [source:/ResearchApps/MAC/CSMAMAC/csmaMac.c@L csmaMac.c]. ==== Startup ==== This block of code calls the following [http://warp.rice.edu/WARP_API/warpmac_8c.html WARPMAC] functions: {{{ #!c warpmac_getMyId(); }}} This function reads the current setting on the [wiki:HardwareUsersGuides/FPGABoard_v1.2/UserIO#DIPSwitch Dip Switches]. The value is used simply to identify the node in the network. For the purposes of the Reference Design, one node must be identified as "0" and the other as "1." {{{ #!c warpmac_init() }}} This function sets reasonable default values for many of the parameters of the MAC, configures interrupts and exceptions, configures Ethernet, and finally initializes the custom peripherals such as the radio controller, the PHY, the packet detector, and the automatic gain control block. {{{ #!c warpmac_setMacAddr(-); }}} This function sets the wireless MAC address of the node. {{{ #!c warpmac_setMaxResend(-); warpmac_setMaxCW(-); warpmac_setTimeout(-); warpmac_setSlotTime(-); }}} These function set up internal parameters of the MAC. The maximum number of resends is the maximum number of times that a packet should be retransmitted, in the event of not receiving an acknowledgment. The maximum contention window defines "how random" backoffs should be in the worst case. The timeout time is the amount of time that the MAC should wait on an acknowledgment before retransmitting. Finally, the slot time is a length of the minimum contention window. {{{ #!c warpmac_setRxBuffer(-,0); warpmac_setTxBuffer(1); }}} The newest version of the [wiki:OFDM OFDM PHY] supports a large chunk of memory that can be used to queue multiple packets. By default, the system is configured with enough memory to support 4 full-size packets. This block of code instructs the framework to use the first 1/4th of memory to receive packets into, and the second 1/4th of memory to send from. In principle, these functions can be used to help juggle packets in the event of a busy wireless medium. For example, one can receive into a separate piece of memory in order to avoid overwriting a packet that has not been processed by higher layers yet. {{{ #!c warpmac_setGoodPacketHandler(receiveGoodPacket); warpmac_setBadPacketHandler(receiveBadPacket); warpmac_setTimerHandler(timerExpire); warpmac_setEmacHandler(ethernet_callback); }}} This block of code attaches user-level callbacks to interrupt service routines. These functions perform the behaviors that they sound like: receiving a packet that passes checksum, receiving a packet that fails checksum, processing a timer expiration, and handling Ethernet activity respectively. {{{ #!c warpmac_setChannel(-, -); }}} This function sets the center frequency of the radio in either the 2.4 or 5 GHz spectrum. {{{ #!c warpmac_setBaseRate(-) }}} Finally, the base rate modulation order is set. This rate must be agreed upon by all nodes in the network ''a priori.'' Within the base rate symbols is a field that specifies the modulation order of the rest of the packet (i.e. the full rate symbols). This enables dynamic modulation for autorate systems. ==== Steady State ==== The steady state behavior of the "Idle" state is very simple: it's nothing. At the bottom of the main function is a while loop that spins in place, waiting for an interrupt service routine to be called by a hardware event. ---- === Accepted Packet from Source === This state is entered if a higher layer has a packet it needs transmitted. For the reference design, this means a packet has arrived via Ethernet and needs processing. ---- === Medium State === The purpose of this state is to check to see if the medium has been idle for a DIFS period. {{{ #!c warpmac_carrierSense(-); }}} Is called and returns a 1 if the medium is idle (and hence the medium can be contented for) and a 0 if the medium is busy (and hence the node must wait). ---- === Transmit Packet via PHY === To transmit a packet over-the-air via the OFDM physical layer, the MAC calls {{{ #!c warpmac_sendOfdm(&txBuffer); }}} The argument of this function is the [http://warp.rice.edu/WARP_API/struct_macframe.html#o0 Macframe] containing the packet's header information. Note, the payload is already sitting inside the OFDM PHY's packet buffer thanks to the Ethernet MAC's built-in DMA engine. The particular packet buffer was designated back in the main function by {{{ #!c warpmac_setTxBuffer(-); }}} ---- === Set TIMEOUT Timer === This state starts the time located on the FPGA fabric in a deterministic countdown mode to wait for an acknowledgment. The framework function called is {{{ #!c warpmac_setTimer(TIMEOUT); }}} The length of the timeout period was specified back in the main function by {{{ #!c warpmac_setTimeout(-); }}} ---- === Set BACKOFF Timer === This state starts the time located on the FPGA fabric in a channel-dependent countdown mode. The hardware automatically supports automatic pausing of this counter in the event of a busy medium and automatic resumption of the timer during idle periods. The purpose of this function is to wait for a random idle time before attempting to use the medium again. The framework function called is {{{ #!c warpmac_setTimer(BACKOFF); }}} The length of the backoff period is a function of the current resend count, the slot size, and the maximum contention window size. These parameters were specified back in the main function. ---- === Received Packet via PHY === This state kick-starts the receive states. ---- === Checksum === Either {{{ #!c int receiveGoodPacket(-) }}} is called in the event of receiving a packet that passes checksum or {{{ #!c int receiveGoodPacket(-) }}} in the event of receiving a packet that fails checksum. ---- === Destination Address === In the received packet's [http://warp.rice.edu/WARP_API/struct_macframe.html#o0 Macframe] is a field that corresponds to the destination MAC address of packet. This field can be compared manually to the node's self address, or framework function {{{ #!c warpmac_addressedToMe(-) }}} can be used to perform this task. ---- === Packet Type === For this MAC algorithm, only two types of packets can be loaded into the [http://warp.rice.edu/WARP_API/struct_macframe.html#o0 Macframe] and understood by the receiver: DATA or ACK. The data packet flag tells the receiving node that a payload is present in this packet and must be delivered via Ethernet to some computer. If it is an ACK, the node must have been in a timeout waiting on the acknowledgment. It should clear the timeout and return to the idle state. ---- === Clear TIMEOUT === To halt and clear the timeout (and hence keep the timer expiration states from executing), the following function is called: {{{ #!c warpmac_clearTimer(TIMEOUT); }}} ---- === Deliver Packet to Sink === For the reference design, the sink is the Ethernet MAC. The function {{{ #!c warpmac_sendEthernet(-); }}} pushes the received packet over Ethernet. ---- === Timer Expired === This state begins the timer expiration states. ---- === Timer Type === In this system, there are two types of timers: timeouts and backoffs. A timeout timer expiring means that no acknowledgment was received in time, so the system should enter a backoff period before attempting to retransmit the packet. If a backoff timer expires, the channel is known to be idle for a DIFS period so transmission via the OFDM PHY can be initiated. ---- === Maximum Resends === To keep the system from hanging forever trying to send the same packet, a maximum number of retransmissions is specified in the main function with {{{ #!c warpmac_setMaxResend(-); }}} This switch state returns a boolean output: Either the maximum number of retransmissions have occurred, or they have not. ---- === Drop Packet === If the maximum number of retransmissions have occurred, the packet is dropped. ---- === Increment Resend Counter === The internal counter keeping track of the number of retransmissions is incremented. The function used is = Carrier-Sense Medium Access Reference Design (CSMAMAC) = The CSMAMAC is serves as the user-level MAC layer in [wiki:OFDMReferenceDesign reference design]. The basic algorithm is simple: * Do I have a packet to send? * If the medium is idle, send it, enter a timeout, and wait for an acknowledgment from the destination * If the medium is busy, enter a backoff period and wait for the medium to become idle * Did I receive a data packet? * If the packet pass checksum and is addressed to me, send an acknowledgment * Did no know acknowledgment happen during a timeout period? * If the maximum number of retransmits has not occurred, enter a backoff and try retransmitting * If the maximum number of retransmits has occurred, drop the packet * Did I wait through a backoff period? * If the medium is busy, retransmit, increment the total number of resends, enter a timeout, and wait for an acknowledgment from the destination * If the medium is busy, enter a backoff period and wait for the medium to become idle This simple algorithm encompasses much of the behavior in commercial 802.11 MAC/PHY chipsets. This algorithm lends itself nicely to a state diagram, which in turn, translates into C-code that can be executed in the PowerPC of the FPGA on WARP. In the following section, we track the "Life of a Packet" as it might journey through these states. == Life of a Packet == Below is the state-machine representation of the above algorithm. Each state can be "clicked" and is described in detail in the accompanying link. The up-to-date code can be found here: [source:/ResearchApps/MAC/CSMAMAC/csmaMac.c@L csmaMac.c]. The API documentation for the code can be found here: [http://warp.rice.edu/WARP_API/csma_mac_8c.html CSMAMAC API] {{{ #!html }}} ---- Note: Through all of the following descriptions, a "-" is used in place of arguments to functions when those arguments are not important to the overall purpose of the description. Howeverm, that is not to say that these arguments can be changed to ''anything'' and have the code still compile and run successfully. Care must be taken in modifying the reference design. === Idle === There are two purposes of this state: At startup must initialize the framework and WARP hardware, and in the steady state it must sit and wait for an event to take place. This state maps to the {{{ #!c int main() }}} function of [source:/ResearchApps/MAC/CSMAMAC/csmaMac.c@L csmaMac.c]. ==== Startup ==== This block of code calls the following [http://warp.rice.edu/WARP_API/warpmac_8c.html WARPMAC] functions: {{{ #!c warpmac_getMyId(); }}} This function reads the current setting on the [wiki:HardwareUsersGuides/FPGABoard_v1.2/UserIO#DIPSwitch Dip Switches]. The value is used simply to identify the node in the network. For the purposes of the Reference Design, one node must be identified as "0" and the other as "1." {{{ #!c warpmac_init() }}} This function sets reasonable default values for many of the parameters of the MAC, configures interrupts and exceptions, configures Ethernet, and finally initializes the custom peripherals such as the radio controller, the PHY, the packet detector, and the automatic gain control block. {{{ #!c warpmac_setMacAddr(-); }}} This function sets the wireless MAC address of the node. {{{ #!c warpmac_setMaxResend(-); warpmac_setMaxCW(-); warpmac_setTimeout(-); warpmac_setSlotTime(-); }}} These function set up internal parameters of the MAC. The maximum number of resends is the maximum number of times that a packet should be retransmitted, in the event of not receiving an acknowledgment. The maximum contention window defines "how random" backoffs should be in the worst case. The timeout time is the amount of time that the MAC should wait on an acknowledgment before retransmitting. Finally, the slot time is a length of the minimum contention window. {{{ #!c warpmac_setRxBuffer(-,0); warpmac_setTxBuffer(1); }}} The newest version of the [wiki:OFDM OFDM PHY] supports a large chunk of memory that can be used to queue multiple packets. By default, the system is configured with enough memory to support 4 full-size packets. This block of code instructs the framework to use the first 1/4th of memory to receive packets into, and the second 1/4th of memory to send from. In principle, these functions can be used to help juggle packets in the event of a busy wireless medium. For example, one can receive into a separate piece of memory in order to avoid overwriting a packet that has not been processed by higher layers yet. {{{ #!c warpmac_setGoodPacketHandler(receiveGoodPacket); warpmac_setBadPacketHandler(receiveBadPacket); warpmac_setTimerHandler(timerExpire); warpmac_setEmacHandler(ethernet_callback); }}} This block of code attaches user-level callbacks to interrupt service routines. These functions perform the behaviors that they sound like: receiving a packet that passes checksum, receiving a packet that fails checksum, processing a timer expiration, and handling Ethernet activity respectively. {{{ #!c warpmac_setChannel(-, -); }}} This function sets the center frequency of the radio in either the 2.4 or 5 GHz spectrum. {{{ #!c warpmac_setBaseRate(-) }}} Finally, the base rate modulation order is set. This rate must be agreed upon by all nodes in the network ''a priori.'' Within the base rate symbols is a field that specifies the modulation order of the rest of the packet (i.e. the full rate symbols). This enables dynamic modulation for autorate systems. ==== Steady State ==== The steady state behavior of the "Idle" state is very simple: it's nothing. At the bottom of the main function is a while loop that spins in place, waiting for an interrupt service routine to be called by a hardware event. ---- === Accepted Packet from Source === This state is entered if a higher layer has a packet it needs transmitted. For the reference design, this means a packet has arrived via Ethernet and needs processing. ---- === Medium State === The purpose of this state is to check to see if the medium has been idle for a DIFS period. {{{ #!c warpmac_carrierSense(-); }}} Is called and returns a 1 if the medium is idle (and hence the medium can be contented for) and a 0 if the medium is busy (and hence the node must wait). ---- === Transmit Packet via PHY === To transmit a packet over-the-air via the OFDM physical layer, the MAC calls {{{ #!c warpmac_sendOfdm(&txBuffer); }}} The argument of this function is the [http://warp.rice.edu/WARP_API/struct_macframe.html#o0 Macframe] containing the packet's header information. Note, the payload is already sitting inside the OFDM PHY's packet buffer thanks to the Ethernet MAC's built-in DMA engine. The particular packet buffer was designated back in the main function by {{{ #!c warpmac_setTxBuffer(-); }}} ---- === Set TIMEOUT Timer === This state starts the time located on the FPGA fabric in a deterministic countdown mode to wait for an acknowledgment. The framework function called is {{{ #!c warpmac_setTimer(TIMEOUT); }}} The length of the timeout period was specified back in the main function by {{{ #!c warpmac_setTimeout(-); }}} ---- === Set BACKOFF Timer === This state starts the time located on the FPGA fabric in a channel-dependent countdown mode. The hardware automatically supports automatic pausing of this counter in the event of a busy medium and automatic resumption of the timer during idle periods. The purpose of this function is to wait for a random idle time before attempting to use the medium again. The framework function called is {{{ #!c warpmac_setTimer(BACKOFF); }}} The length of the backoff period is a function of the current resend count, the slot size, and the maximum contention window size. These parameters were specified back in the main function. ---- === Received Packet via PHY === This state kick-starts the receive states. ---- === Checksum === Either {{{ #!c int receiveGoodPacket(-) }}} is called in the event of receiving a packet that passes checksum or {{{ #!c int receiveGoodPacket(-) }}} in the event of receiving a packet that fails checksum. ---- === Destination Address === In the received packet's [http://warp.rice.edu/WARP_API/struct_macframe.html#o0 Macframe] is a field that corresponds to the destination MAC address of packet. This field can be compared manually to the node's self address, or framework function {{{ #!c warpmac_addressedToMe(-) }}} can be used to perform this task. ---- === Packet Type === For this MAC algorithm, only two types of packets can be loaded into the [http://warp.rice.edu/WARP_API/struct_macframe.html#o0 Macframe] and understood by the receiver: DATA or ACK. The data packet flag tells the receiving node that a payload is present in this packet and must be delivered via Ethernet to some computer. If it is an ACK, the node must have been in a timeout waiting on the acknowledgment. It should clear the timeout and return to the idle state. ---- === Clear TIMEOUT === To halt and clear the timeout (and hence keep the timer expiration states from executing), the following function is called: {{{ #!c warpmac_clearTimer(TIMEOUT); }}} ---- === Deliver Packet to Sink === For the reference design, the sink is the Ethernet MAC. The function {{{ #!c warpmac_sendEthernet(-); }}} pushes the received packet over Ethernet. ---- === Timer Expired === This state begins the timer expiration states. ---- === Timer Type === In this system, there are two types of timers: timeouts and backoffs. A timeout timer expiring means that no acknowledgment was received in time, so the system should enter a backoff period before attempting to retransmit the packet. If a backoff timer expires, the channel is known to be idle for a DIFS period so transmission via the OFDM PHY can be initiated. ---- === Maximum Resends === To keep the system from hanging forever trying to send the same packet, a maximum number of retransmissions is specified in the main function with {{{ #!c warpmac_incrementResend(-); }}}