Csma: comply with IEEE 802.15.4

This commit is contained in:
Simon Duquennoy 2016-04-22 23:27:43 +02:00 committed by Billy Kozak
parent ca0f32f45b
commit 90b886aa71
10 changed files with 65 additions and 94 deletions

View file

@ -220,14 +220,6 @@
* on the target platform, and are therefore platform-specific. * on the target platform, and are therefore platform-specific.
*/ */
/* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS specifies how many times the
MAC layer should resend packets if no link-layer ACK was
received. This only makes sense with the csma_driver
NETSTACK_CONF_MAC. */
#ifndef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS 4
#endif /* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS */
/* SICSLOWPAN_CONF_FRAG specifies if 6lowpan fragmentation should be /* SICSLOWPAN_CONF_FRAG specifies if 6lowpan fragmentation should be
used or not. Fragmentation is on by default. */ used or not. Fragmentation is on by default. */
#ifndef SICSLOWPAN_CONF_FRAG #ifndef SICSLOWPAN_CONF_FRAG

View file

@ -92,12 +92,6 @@ void uip_log(char *msg);
#define UIP_LOG(m) #define UIP_LOG(m)
#endif /* UIP_LOGGING == 1 */ #endif /* UIP_LOGGING == 1 */
#ifdef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_MAX_MAC_TRANSMISSIONS SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#else
#define SICSLOWPAN_MAX_MAC_TRANSMISSIONS 4
#endif
#ifndef SICSLOWPAN_COMPRESSION #ifndef SICSLOWPAN_COMPRESSION
#ifdef SICSLOWPAN_CONF_COMPRESSION #ifdef SICSLOWPAN_CONF_COMPRESSION
#define SICSLOWPAN_COMPRESSION SICSLOWPAN_CONF_COMPRESSION #define SICSLOWPAN_COMPRESSION SICSLOWPAN_CONF_COMPRESSION
@ -1290,9 +1284,6 @@ output(const uip_lladdr_t *localdest)
packetbuf_clear(); packetbuf_clear();
packetbuf_ptr = packetbuf_dataptr(); packetbuf_ptr = packetbuf_dataptr();
packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
SICSLOWPAN_MAX_MAC_TRANSMISSIONS);
if(callback) { if(callback) {
/* call the attribution when the callback comes, but set attributes /* call the attribution when the callback comes, but set attributes
here ! */ here ! */

View file

@ -63,26 +63,35 @@
#define PRINTF(...) #define PRINTF(...)
#endif /* DEBUG */ #endif /* DEBUG */
#ifndef CSMA_MAX_BACKOFF_EXPONENT /* Constants of the IEEE 802.15.4 standard */
#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 /* macMinBE: Initial backoff exponent. Range 0--CSMA_MAX_BE */
#ifdef CSMA_CONF_MAX_MAC_TRANSMISSIONS #ifdef CSMA_CONF_MIN_BE
#define CSMA_MAX_MAC_TRANSMISSIONS CSMA_CONF_MAX_MAC_TRANSMISSIONS #define CSMA_MIN_BE CSMA_CONF_MIN_BE
#else #else
#define CSMA_MAX_MAC_TRANSMISSIONS 3 #define CSMA_MIN_BE 0
#endif /* CSMA_CONF_MAX_MAC_TRANSMISSIONS */ #endif
#endif /* CSMA_MAX_MAC_TRANSMISSIONS */
#if CSMA_MAX_MAC_TRANSMISSIONS < 1 /* macMaxBE: Maximum backoff exponent. Range 3--8 */
#error CSMA_CONF_MAX_MAC_TRANSMISSIONS must be at least 1. #ifdef CSMA_CONF_MAX_BE
#error Change CSMA_CONF_MAX_MAC_TRANSMISSIONS in contiki-conf.h or in your Makefile. #define CSMA_MAX_BE CSMA_CONF_MAX_BE
#endif /* CSMA_CONF_MAX_MAC_TRANSMISSIONS < 1 */ #else
#define CSMA_MAX_BE 4
#endif
/* macMaxCSMABackoffs: Maximum number of backoffs in case of channel busy/collision. Range 0--5 */
#ifdef CSMA_CONF_MAX_BACKOFF
#define CSMA_MAX_BACKOFF CSMA_CONF_MAX_BACKOFF
#else
#define CSMA_MAX_BACKOFF 5
#endif
/* macMaxFrameRetries: Maximum number of re-transmissions attampts. Range 0--7 */
#ifdef CSMA_CONF_MAX_FRAME_RETRIES
#define CSMA_MAX_MAX_FRAME_RETRIES CSMA_CONF_MAX_FRAME_RETRIES
#else
#define CSMA_MAX_MAX_FRAME_RETRIES 7
#endif
/* Packet metadata */ /* Packet metadata */
struct qbuf_metadata { struct qbuf_metadata {
@ -97,7 +106,7 @@ struct neighbor_queue {
linkaddr_t addr; linkaddr_t addr;
struct ctimer transmit_timer; struct ctimer transmit_timer;
uint8_t transmissions; uint8_t transmissions;
uint8_t collisions, deferrals; uint8_t collisions;
LIST_STRUCT(queued_packet_list); LIST_STRUCT(queued_packet_list);
}; };
@ -138,18 +147,18 @@ neighbor_queue_from_addr(const linkaddr_t *addr)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static clock_time_t static clock_time_t
default_timebase(void) backoff_period(void)
{ {
clock_time_t time; clock_time_t time;
/* The retransmission time must be proportional to the channel /* The retransmission time must be proportional to the channel
check interval of the underlying radio duty cycling layer. */ check interval of the underlying radio duty cycling layer. */
time = NETSTACK_RDC.channel_check_interval(); time = NETSTACK_RDC.channel_check_interval();
/* If the radio duty cycle has no channel check interval (i.e., it /* If the radio duty cycle has no channel check interval, we use
does not turn the radio off), we make the retransmission time * the default in IEEE 802.15.4: aUnitBackoffPeriod which is
proportional to the configured MAC channel check rate. */ * 20 symbols i.e. 320 usec. That is, 1/3125 second. */
if(time == 0) { if(time == 0) {
time = (CLOCK_SECOND / 3125) ? (CLOCK_SECOND / 3125) : 1; time = MAX(CLOCK_SECOND / 3125, 1);
} }
return time; return time;
} }
@ -170,10 +179,28 @@ transmit_packet_list(void *ptr)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
schedule_transmission(struct neighbor_queue *n)
{
clock_time_t delay;
int backoff_exponent; /* BE in IEEE 802.15.4 */
backoff_exponent = MIN(n->collisions, CSMA_MAX_BE);
/* Compute max delay as per IEEE 802.15.4: 2^BE-1 backoff periods */
delay = ((1 << backoff_exponent) - 1) * backoff_period();
if(delay > 0) {
/* Pick a time for next transmission */
delay = random_rand() % delay;
}
PRINTF("csma: scheduling transmission in %u ticks, NB=%u, BE=%u\n",
(unsigned)delay, n->collisions, backoff_exponent);
ctimer_set(&n->transmit_timer, delay, transmit_packet_list, n);
}
/*---------------------------------------------------------------------------*/
static void
free_packet(struct neighbor_queue *n, struct rdc_buf_list *p, int status) free_packet(struct neighbor_queue *n, struct rdc_buf_list *p, int status)
{ {
clock_time_t tx_delay;
if(p != NULL) { if(p != NULL) {
/* Remove packet from list and deallocate */ /* Remove packet from list and deallocate */
list_remove(n->queued_packet_list, p); list_remove(n->queued_packet_list, p);
@ -186,11 +213,9 @@ free_packet(struct neighbor_queue *n, struct rdc_buf_list *p, int status)
if(list_head(n->queued_packet_list) != NULL) { if(list_head(n->queued_packet_list) != NULL) {
/* There is a next packet. We reset current tx information */ /* There is a next packet. We reset current tx information */
n->transmissions = 0; n->transmissions = 0;
n->collisions = 0; n->collisions = CSMA_MIN_BE;
n->deferrals = 0; /* Schedule next transmissions */
/* Set a timer for next transmissions */ schedule_transmission(n);
tx_delay = (status == MAC_TX_OK) ? 0 : default_timebase();
ctimer_set(&n->transmit_timer, tx_delay, transmit_packet_list, n);
} else { } else {
/* This was the last packet in the queue, we free the neighbor */ /* This was the last packet in the queue, we free the neighbor */
ctimer_stop(&n->transmit_timer); ctimer_stop(&n->transmit_timer);
@ -232,22 +257,7 @@ tx_done(int status, struct rdc_buf_list *q, struct neighbor_queue *n)
static void static void
rexmit(struct rdc_buf_list *q, struct neighbor_queue *n) rexmit(struct rdc_buf_list *q, struct neighbor_queue *n)
{ {
clock_time_t time; schedule_transmission(n);
int backoff_exponent;
int backoff_transmissions;
time = default_timebase();
backoff_exponent = n->collisions;
/* 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));
PRINTF("csma: retransmitting with time %lu %p\n", time, q);
ctimer_set(&n->transmit_timer, time, transmit_packet_list, n);
/* This is needed to correctly attribute energy that we spent /* This is needed to correctly attribute energy that we spent
transmitting this packet. */ transmitting this packet. */
queuebuf_update_attr_from_packetbuf(q->buf); queuebuf_update_attr_from_packetbuf(q->buf);
@ -263,9 +273,10 @@ collision(struct rdc_buf_list *q, struct neighbor_queue *n,
n->collisions += num_transmissions; n->collisions += num_transmissions;
if(n->collisions > CSMA_MAX_BACKOFF_EXPONENT) { if(n->collisions > CSMA_MAX_BACKOFF) {
n->collisions = 0; n->collisions = CSMA_MIN_BE;
n->transmissions += num_transmissions; /* Increment to indicate a next retry */
n->transmissions++;
} }
if(n->transmissions >= metadata->max_transmissions) { if(n->transmissions >= metadata->max_transmissions) {
@ -283,8 +294,8 @@ noack(struct rdc_buf_list *q, struct neighbor_queue *n, int num_transmissions)
metadata = (struct qbuf_metadata *)q->ptr; metadata = (struct qbuf_metadata *)q->ptr;
n->collisions = CSMA_MIN_BE;
n->transmissions += num_transmissions; n->transmissions += num_transmissions;
n->collisions = 0;
if(n->transmissions >= metadata->max_transmissions) { if(n->transmissions >= metadata->max_transmissions) {
tx_done(MAC_TX_NOACK, q, n); tx_done(MAC_TX_NOACK, q, n);
@ -297,6 +308,7 @@ noack(struct rdc_buf_list *q, struct neighbor_queue *n, int num_transmissions)
static void static void
tx_ok(struct rdc_buf_list *q, struct neighbor_queue *n, int num_transmissions) tx_ok(struct rdc_buf_list *q, struct neighbor_queue *n, int num_transmissions)
{ {
n->collisions = CSMA_MIN_BE;
n->transmissions += num_transmissions; n->transmissions += num_transmissions;
tx_done(MAC_TX_OK, q, n); tx_done(MAC_TX_OK, q, n);
} }
@ -341,7 +353,6 @@ packet_sent(void *ptr, int status, int num_transmissions)
collision(q, n, num_transmissions); collision(q, n, num_transmissions);
break; break;
case MAC_TX_DEFERRED: case MAC_TX_DEFERRED:
n->deferrals += num_transmissions;
break; break;
default: default:
tx_done(status, q, n); tx_done(status, q, n);
@ -380,8 +391,7 @@ send_packet(mac_callback_t sent, void *ptr)
/* Init neighbor entry */ /* Init neighbor entry */
linkaddr_copy(&n->addr, addr); linkaddr_copy(&n->addr, addr);
n->transmissions = 0; n->transmissions = 0;
n->collisions = 0; n->collisions = CSMA_MIN_BE;
n->deferrals = 0;
/* Init packet list for this neighbor */ /* Init packet list for this neighbor */
LIST_STRUCT_INIT(n, queued_packet_list); LIST_STRUCT_INIT(n, queued_packet_list);
/* Add neighbor to the list */ /* Add neighbor to the list */
@ -402,7 +412,7 @@ send_packet(mac_callback_t sent, void *ptr)
/* Neighbor and packet successfully allocated */ /* Neighbor and packet successfully allocated */
if(packetbuf_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS) == 0) { if(packetbuf_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS) == 0) {
/* Use default configuration for max transmissions */ /* Use default configuration for max transmissions */
metadata->max_transmissions = CSMA_MAX_MAC_TRANSMISSIONS; metadata->max_transmissions = CSMA_MAX_MAX_FRAME_RETRIES + 1;
} else { } else {
metadata->max_transmissions = metadata->max_transmissions =
packetbuf_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS); packetbuf_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS);
@ -423,7 +433,7 @@ send_packet(mac_callback_t sent, void *ptr)
list_length(n->queued_packet_list), memb_numfree(&packet_memb)); list_length(n->queued_packet_list), memb_numfree(&packet_memb));
/* If q is the first packet in the neighbor's queue, send asap */ /* If q is the first packet in the neighbor's queue, send asap */
if(list_head(n->queued_packet_list) == q) { if(list_head(n->queued_packet_list) == q) {
ctimer_set(&n->transmit_timer, 0, transmit_packet_list, n); schedule_transmission(n);
} }
return; return;
} }

View file

@ -168,9 +168,6 @@
#define SICSLOWPAN_CONF_MAXAGE 8 #define SICSLOWPAN_CONF_MAXAGE 8
#endif /* SICSLOWPAN_CONF_FRAG */ #endif /* SICSLOWPAN_CONF_FRAG */
#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2 #define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2
#ifndef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS 8
#endif /* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS */
#endif /* NETSTACK_CONF_WITH_IPV6 */ #endif /* NETSTACK_CONF_WITH_IPV6 */

View file

@ -174,9 +174,6 @@
#define SICSLOWPAN_CONF_MAXAGE 8 #define SICSLOWPAN_CONF_MAXAGE 8
#endif /* SICSLOWPAN_CONF_FRAG */ #endif /* SICSLOWPAN_CONF_FRAG */
#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2 #define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2
#ifndef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS 5
#endif /* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS */
#else /* NETSTACK_CONF_WITH_IPV6 */ #else /* NETSTACK_CONF_WITH_IPV6 */
#define UIP_CONF_IP_FORWARD 1 #define UIP_CONF_IP_FORWARD 1
#define UIP_CONF_BUFFER_SIZE 108 #define UIP_CONF_BUFFER_SIZE 108

View file

@ -154,9 +154,6 @@
#define SICSLOWPAN_CONF_MAXAGE 8 #define SICSLOWPAN_CONF_MAXAGE 8
#endif /* SICSLOWPAN_CONF_FRAG */ #endif /* SICSLOWPAN_CONF_FRAG */
#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2 #define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2
#ifndef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS 5
#endif /* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS */
#define UIP_CONF_ICMP_DEST_UNREACH 1 #define UIP_CONF_ICMP_DEST_UNREACH 1
#define UIP_CONF_DHCP_LIGHT #define UIP_CONF_DHCP_LIGHT

View file

@ -110,9 +110,6 @@ typedef unsigned short uip_stats_t;
#define SICSLOWPAN_CONF_MAXAGE 8 #define SICSLOWPAN_CONF_MAXAGE 8
#endif /* SICSLOWPAN_CONF_FRAG */ #endif /* SICSLOWPAN_CONF_FRAG */
#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2 #define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2
#ifndef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS 5
#endif /* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS */
#define UIP_CONF_IPV6_CHECKS 1 #define UIP_CONF_IPV6_CHECKS 1
#define UIP_CONF_IPV6_QUEUE_PKT 1 #define UIP_CONF_IPV6_QUEUE_PKT 1

View file

@ -179,9 +179,6 @@
#define SICSLOWPAN_CONF_MAXAGE 8 #define SICSLOWPAN_CONF_MAXAGE 8
#endif /* SICSLOWPAN_CONF_FRAG */ #endif /* SICSLOWPAN_CONF_FRAG */
#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2 #define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2
#ifndef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS 5
#endif /* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS */
#else /* NETSTACK_CONF_WITH_IPV6 */ #else /* NETSTACK_CONF_WITH_IPV6 */
#define UIP_CONF_IP_FORWARD 1 #define UIP_CONF_IP_FORWARD 1
#define UIP_CONF_BUFFER_SIZE 108 #define UIP_CONF_BUFFER_SIZE 108

View file

@ -106,10 +106,6 @@
#define SICSLOWPAN_CONF_MAXAGE 4 #define SICSLOWPAN_CONF_MAXAGE 4
#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2 #define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2
#ifndef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS 5
#endif /* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS */
#define UIP_CONF_ICMP_DEST_UNREACH 1 #define UIP_CONF_ICMP_DEST_UNREACH 1
#define UIP_CONF_DHCP_LIGHT #define UIP_CONF_DHCP_LIGHT

View file

@ -157,9 +157,6 @@
#define SICSLOWPAN_CONF_MAXAGE 8 #define SICSLOWPAN_CONF_MAXAGE 8
#endif /* SICSLOWPAN_CONF_FRAG */ #endif /* SICSLOWPAN_CONF_FRAG */
#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2 #define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2
#ifndef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
#define SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS 5
#endif /* SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS */
#else /* NETSTACK_CONF_WITH_IPV6 */ #else /* NETSTACK_CONF_WITH_IPV6 */
#define UIP_CONF_IP_FORWARD 1 #define UIP_CONF_IP_FORWARD 1
#define UIP_CONF_BUFFER_SIZE 108 #define UIP_CONF_BUFFER_SIZE 108