Implement a truncated exponential backoff for csma

This commit is contained in:
Simon Duquennoy 2013-10-07 16:00:18 +02:00
parent 6da394139c
commit 03dcca15cb
1 changed files with 21 additions and 9 deletions

View File

@ -63,6 +63,14 @@
#define PRINTF(...) #define PRINTF(...)
#endif /* DEBUG */ #endif /* DEBUG */
#ifndef CSMA_MAX_BACKOFF_EXPONENT
#ifdef CSMA_CONF_MAX_BACKOFF_EXPONENT
#define CSMA_MAX_BACKOFF_EXPONENT CSMA_CONF_MAX_BACKOFF_EXPONENT
#else
#define CSMA_MAX_BACKOFF_EXPONENT 3
#endif /* CSMA_CONF_MAX_BACKOFF_EXPONENT */
#endif /* CSMA_MAX_BACKOFF_EXPONENT */
#ifndef CSMA_MAX_MAC_TRANSMISSIONS #ifndef CSMA_MAX_MAC_TRANSMISSIONS
#ifdef CSMA_CONF_MAX_MAC_TRANSMISSIONS #ifdef CSMA_CONF_MAX_MAC_TRANSMISSIONS
#define CSMA_MAX_MAC_TRANSMISSIONS CSMA_CONF_MAX_MAC_TRANSMISSIONS #define CSMA_MAX_MAC_TRANSMISSIONS CSMA_CONF_MAX_MAC_TRANSMISSIONS
@ -194,6 +202,7 @@ packet_sent(void *ptr, int status, int num_transmissions)
mac_callback_t sent; mac_callback_t sent;
void *cptr; void *cptr;
int num_tx; int num_tx;
int backoff_exponent;
int backoff_transmissions; int backoff_transmissions;
n = ptr; n = ptr;
@ -249,18 +258,21 @@ packet_sent(void *ptr, int status, int num_transmissions)
check interval of the underlying radio duty cycling layer. */ check interval of the underlying radio duty cycling layer. */
time = default_timebase(); time = default_timebase();
/* The retransmission time uses a linear backoff so that the /* The retransmission time uses a truncated exponential backoff
interval between the transmissions increase with each * so that the interval between the transmissions increase with
retransmit. */ * each retransmit. */
backoff_transmissions = n->transmissions + 1; backoff_exponent = num_tx;
/* Clamp the number of backoffs so that we don't get a too long /* Truncate the exponent if needed. */
timeout here, since that will delay all packets in the if(backoff_exponent > CSMA_MAX_BACKOFF_EXPONENT) {
queue. */ backoff_exponent = CSMA_MAX_BACKOFF_EXPONENT;
if(backoff_transmissions > 3) {
backoff_transmissions = 3;
} }
/* Proceed to exponentiation. */
backoff_transmissions = 1 << backoff_exponent;
/* Pick a time for next transmission, within the interval:
* [time, time + 2^backoff_exponent * time[ */
time = time + (random_rand() % (backoff_transmissions * time)); time = time + (random_rand() % (backoff_transmissions * time));
if(n->transmissions < metadata->max_transmissions) { if(n->transmissions < metadata->max_transmissions) {