Contiki 3.x
tsch-queue.h
1 /*
2  * Copyright (c) 2014, SICS Swedish ICT.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 #ifndef __TSCH_QUEUE_H__
34 #define __TSCH_QUEUE_H__
35 
36 /********** Includes **********/
37 
38 #include "contiki.h"
39 #include "lib/ringbufindex.h"
40 #include "net/linkaddr.h"
41 #include "net/mac/tsch/tsch-schedule.h"
42 
43 /******** Configuration *******/
44 
45 /* The maximum number of outgoing packets towards each neighbor
46  * Must be power of two to enable atomic ringbuf operations.
47  * Note: the total number of outgoing packets in the system (for
48  * all neighbors) is defined via QUEUEBUF_CONF_NUM */
49 #ifdef TSCH_QUEUE_CONF_NUM_PER_NEIGHBOR
50 #define TSCH_QUEUE_NUM_PER_NEIGHBOR TSCH_QUEUE_CONF_NUM_PER_NEIGHBOR
51 #else
52 /* By default, round QUEUEBUF_CONF_NUM to next power of two
53  * (in the range [4;256]) */
54 #if QUEUEBUF_CONF_NUM <= 4
55 #define TSCH_QUEUE_NUM_PER_NEIGHBOR 4
56 #elif QUEUEBUF_CONF_NUM <= 8
57 #define TSCH_QUEUE_NUM_PER_NEIGHBOR 8
58 #elif QUEUEBUF_CONF_NUM <= 16
59 #define TSCH_QUEUE_NUM_PER_NEIGHBOR 16
60 #elif QUEUEBUF_CONF_NUM <= 32
61 #define TSCH_QUEUE_NUM_PER_NEIGHBOR 32
62 #elif QUEUEBUF_CONF_NUM <= 64
63 #define TSCH_QUEUE_NUM_PER_NEIGHBOR 64
64 #elif QUEUEBUF_CONF_NUM <= 128
65 #define TSCH_QUEUE_NUM_PER_NEIGHBOR 128
66 #else
67 #define TSCH_QUEUE_NUM_PER_NEIGHBOR 256
68 #endif
69 #endif
70 
71 /* The number of neighbor queues. There are two queues allocated at all times:
72  * one for EBs, one for broadcasts. Other queues are for unicast to neighbors */
73 #ifdef TSCH_QUEUE_CONF_MAX_NEIGHBOR_QUEUES
74 #define TSCH_QUEUE_MAX_NEIGHBOR_QUEUES TSCH_QUEUE_CONF_MAX_NEIGHBOR_QUEUES
75 #else
76 #define TSCH_QUEUE_MAX_NEIGHBOR_QUEUES ((NBR_TABLE_CONF_MAX_NEIGHBORS) + 2)
77 #endif
78 
79 /* TSCH CSMA-CA parameters, see IEEE 802.15.4e-2012 */
80 /* Min backoff exponent */
81 #ifdef TSCH_CONF_MAC_MIN_BE
82 #define TSCH_MAC_MIN_BE TSCH_CONF_MAC_MIN_BE
83 #else
84 #define TSCH_MAC_MIN_BE 1
85 #endif
86 /* Max backoff exponent */
87 #ifdef TSCH_CONF_MAC_MAX_BE
88 #define TSCH_MAC_MAX_BE TSCH_CONF_MAC_MAX_BE
89 #else
90 #define TSCH_MAC_MAX_BE 7
91 #endif
92 /* Max number of re-transmissions */
93 #ifdef TSCH_CONF_MAC_MAX_FRAME_RETRIES
94 #define TSCH_MAC_MAX_FRAME_RETRIES TSCH_CONF_MAC_MAX_FRAME_RETRIES
95 #else
96 #define TSCH_MAC_MAX_FRAME_RETRIES 8
97 #endif
98 
99 /*********** Callbacks *********/
100 
101 /* Called by TSCH when switching time source */
102 #ifdef TSCH_CALLBACK_NEW_TIME_SOURCE
103 struct tsch_neighbor;
104 void TSCH_CALLBACK_NEW_TIME_SOURCE(const struct tsch_neighbor *old, const struct tsch_neighbor *new);
105 #endif
106 
107 /* Called by TSCH every time a packet is ready to be added to the send queue */
108 #ifdef TSCH_CALLBACK_PACKET_READY
109 void TSCH_CALLBACK_PACKET_READY(void);
110 #endif
111 
112 /************ Types ***********/
113 
114 /* TSCH packet information */
115 struct tsch_packet {
116  struct queuebuf *qb; /* pointer to the queuebuf to be sent */
117  mac_callback_t sent; /* callback for this packet */
118  void *ptr; /* MAC callback parameter */
119  uint8_t transmissions; /* #transmissions performed for this packet */
120  uint8_t ret; /* status -- MAC return code */
121  uint8_t header_len; /* length of header and header IEs (needed for link-layer security) */
122  uint8_t tsch_sync_ie_offset; /* Offset within the frame used for quick update of EB ASN and join priority */
123 };
124 
125 /* TSCH neighbor information */
126 struct tsch_neighbor {
127  /* Neighbors are stored as a list: "next" must be the first field */
128  struct tsch_neighbor *next;
129  linkaddr_t addr; /* MAC address of the neighbor */
130  uint8_t is_broadcast; /* is this neighbor a virtual neighbor used for broadcast (of data packets or EBs) */
131  uint8_t is_time_source; /* is this neighbor a time source? */
132  uint8_t backoff_exponent; /* CSMA backoff exponent */
133  uint8_t backoff_window; /* CSMA backoff window (number of slots to skip) */
134  uint8_t last_backoff_window; /* Last CSMA backoff window */
135  uint8_t tx_links_count; /* How many links do we have to this neighbor? */
136  uint8_t dedicated_tx_links_count; /* How many dedicated links do we have to this neighbor? */
137  /* Array for the ringbuf. Contains pointers to packets.
138  * Its size must be a power of two to allow for atomic put */
139  struct tsch_packet *tx_array[TSCH_QUEUE_NUM_PER_NEIGHBOR];
140  /* Circular buffer of pointers to packet. */
141  struct ringbufindex tx_ringbuf;
142 };
143 
144 /***** External Variables *****/
145 
146 /* Broadcast and EB virtual neighbors */
147 extern struct tsch_neighbor *n_broadcast;
148 extern struct tsch_neighbor *n_eb;
149 
150 /********** Functions *********/
151 
152 /* Add a TSCH neighbor */
153 struct tsch_neighbor *tsch_queue_add_nbr(const linkaddr_t *addr);
154 /* Get a TSCH neighbor */
155 struct tsch_neighbor *tsch_queue_get_nbr(const linkaddr_t *addr);
156 /* Get a TSCH time source (we currently assume there is only one) */
157 struct tsch_neighbor *tsch_queue_get_time_source(void);
158 /* Update TSCH time source */
159 int tsch_queue_update_time_source(const linkaddr_t *new_addr);
160 /* Add packet to neighbor queue. Use same lockfree implementation as ringbuf.c (put is atomic) */
161 struct tsch_packet *tsch_queue_add_packet(const linkaddr_t *addr, mac_callback_t sent, void *ptr);
162 /* Returns the number of packets currently a given neighbor queue */
163 int tsch_queue_packet_count(const linkaddr_t *addr);
164 /* Remove first packet from a neighbor queue. The packet is stored in a separate
165  * dequeued packet list, for later processing. Return the packet. */
166 struct tsch_packet *tsch_queue_remove_packet_from_queue(struct tsch_neighbor *n);
167 /* Free a packet */
168 void tsch_queue_free_packet(struct tsch_packet *p);
169 /* Reset neighbor queues */
170 void tsch_queue_reset(void);
171 /* Deallocate neighbors with empty queue */
172 void tsch_queue_free_unused_neighbors(void);
173 /* Is the neighbor queue empty? */
174 int tsch_queue_is_empty(const struct tsch_neighbor *n);
175 /* Returns the first packet from a neighbor queue */
176 struct tsch_packet *tsch_queue_get_packet_for_nbr(const struct tsch_neighbor *n, struct tsch_link *link);
177 /* Returns the head packet from a neighbor queue (from neighbor address) */
178 struct tsch_packet *tsch_queue_get_packet_for_dest_addr(const linkaddr_t *addr, struct tsch_link *link);
179 /* Returns the head packet of any neighbor queue with zero backoff counter.
180  * Writes pointer to the neighbor in *n */
181 struct tsch_packet *tsch_queue_get_unicast_packet_for_any(struct tsch_neighbor **n, struct tsch_link *link);
182 /* May the neighbor transmit over a share link? */
183 int tsch_queue_backoff_expired(const struct tsch_neighbor *n);
184 /* Reset neighbor backoff */
185 void tsch_queue_backoff_reset(struct tsch_neighbor *n);
186 /* Increment backoff exponent, pick a new window */
187 void tsch_queue_backoff_inc(struct tsch_neighbor *n);
188 /* Decrement backoff window for all queues directed at dest_addr */
189 void tsch_queue_update_all_backoff_windows(const linkaddr_t *dest_addr);
190 /* Initialize TSCH queue module */
191 void tsch_queue_init(void);
192 
193 #endif /* __TSCH_QUEUE_H__ */
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
Header file for the ringbufindex library
Header file for the Rime address representation