44#include "contiki-lib.h"
45#include "contiki-net.h"
49#include "dev/watchdog.h"
52#define DEBUG DEBUG_NONE
55#define TRICKLE_VERBOSE 0
57#if DEBUG && TRICKLE_VERBOSE
58#define VERBOSE_PRINTF(...) PRINTF(__VA_ARGS__)
59#define VERBOSE_PRINT_SEED(s) PRINT_SEED(s)
61#define VERBOSE_PRINTF(...)
62#define VERBOSE_PRINT_SEED(...)
68#if ROLL_TM_SHORT_SEEDS
69typedef union seed_id_u {
74#define seed_is_null(s) ((s)->id == 0)
75#define PRINT_SEED(s) PRINTF("0x%02x%02x", (s)->u8[0], (s)->u8[1])
77typedef uip_ip6addr_t seed_id_t;
79#define seed_is_null(s) uip_is_addr_unspecified(s)
80#define PRINT_SEED(s) PRINT6ADDR(s)
82#define seed_id_cmp(a, b) (memcmp((a), (b), sizeof(seed_id_t)) == 0)
83#define seed_id_cpy(a, b) (memcpy((a), (b), sizeof(seed_id_t)))
91 clock_time_t t_last_trigger;
99 uint8_t inconsistency;
107#define TRICKLE_TIME(m, d) ((clock_time_t)((m) << (d)))
112#define TRICKLE_IMAX(t) ((uint32_t)((t)->i_min << (t)->i_max))
119#define TRICKLE_ACTIVE(t) ((uint32_t)(TRICKLE_IMAX(t) * t->t_active))
126#define TRICKLE_DWELL(t) ((uint32_t)(TRICKLE_IMAX(t) * t->t_dwell))
132#define SUPPRESSION_ENABLED(t) ((t)->k != ROLL_TM_INFINITE_REDUNDANCY)
138#define SUPPRESSION_DISABLED(t) ((t)->k == ROLL_TM_INFINITE_REDUNDANCY)
143#define TIMER_CONFIGURE(m) do { \
144 t[m].i_min = ROLL_TM_IMIN_##m; \
145 t[m].i_max = ROLL_TM_IMAX_##m; \
146 t[m].k = ROLL_TM_K_##m; \
147 t[m].t_active = ROLL_TM_T_ACTIVE_##m; \
148 t[m].t_dwell = ROLL_TM_T_DWELL_##m; \
149 t[m].t_last_trigger = clock_time(); \
169#define SEQ_VAL_IS_EQ(i1, i2) ((i1) == (i2))
174#define SEQ_VAL_IS_LT(i1, i2) \
177 ((((i1) < (i2)) && ((int16_t)((i2) - (i1)) < 0x4000)) || \
178 (((i1) > (i2)) && ((int16_t)((i1) - (i2)) > 0x4000))) \
184#define SEQ_VAL_IS_GT(i1, i2) \
187 ((((i1) < (i2)) && ((int16_t)((i2) - (i1)) > 0x4000)) || \
188 (((i1) > (i2)) && ((int16_t)((i1) - (i2)) < 0x4000))) \
194#define SEQ_VAL_ADD(s, n) (((s) + (n)) % 0x8000)
197struct sliding_window {
206#define SLIDING_WINDOW_U_BIT 0x80
207#define SLIDING_WINDOW_M_BIT 0x40
208#define SLIDING_WINDOW_L_BIT 0x20
209#define SLIDING_WINDOW_B_BIT 0x10
215#define SLIDING_WINDOW_IS_USED(w) ((w)->flags & SLIDING_WINDOW_U_BIT)
221#define SLIDING_WINDOW_IS_USED_SET(w) ((w)->flags |= SLIDING_WINDOW_U_BIT)
227#define SLIDING_WINDOW_IS_USED_CLR(w) ((w)->flags &= ~SLIDING_WINDOW_U_BIT)
228#define window_free(w) SLIDING_WINDOW_IS_USED_CLR(w)
234#define SLIDING_WINDOW_LISTED_SET(w) ((w)->flags |= SLIDING_WINDOW_L_BIT)
240#define SLIDING_WINDOW_LISTED_CLR(w) ((w)->flags &= ~SLIDING_WINDOW_L_BIT)
246#define SLIDING_WINDOW_IS_LISTED(w) ((w)->flags & SLIDING_WINDOW_L_BIT)
252#define SLIDING_WINDOW_M_SET(w) ((w)->flags |= SLIDING_WINDOW_M_BIT)
258#define SLIDING_WINDOW_M_CLR(w) ((w)->flags &= ~SLIDING_WINDOW_M_BIT)
264#define SLIDING_WINDOW_GET_M(w) \
265 ((uint8_t)(((w)->flags & SLIDING_WINDOW_M_BIT) == SLIDING_WINDOW_M_BIT))
269#if ROLL_TM_SHORT_SEEDS
277 struct sliding_window *sw;
283#define MCAST_PACKET_U_BIT 0x80
284#define MCAST_PACKET_S_BIT 0x20
285#define MCAST_PACKET_L_BIT 0x10
288#if ROLL_TM_SHORT_SEEDS
289#define MCAST_PACKET_GET_SEED(p) ((seed_id_t *)&((p)->seed_id))
291#define MCAST_PACKET_GET_SEED(p) \
292 ((seed_id_t *)&((struct uip_ip_hdr *)&(p)->buff[0])->srcipaddr)
299#define MCAST_PACKET_TTL(p) \
300 (((struct uip_ip_hdr *)(p)->buff)->ttl)
306#define MCAST_PACKET_USED_SET(p) ((p)->flags |= MCAST_PACKET_U_BIT)
312#define MCAST_PACKET_USED_CLR(p) ((p)->flags &= ~MCAST_PACKET_U_BIT)
317#define MCAST_PACKET_IS_USED(p) ((p)->flags & MCAST_PACKET_U_BIT)
322#define MCAST_PACKET_MUST_SEND(p) ((p)->flags & MCAST_PACKET_S_BIT)
328#define MCAST_PACKET_SEND_SET(p) ((p)->flags |= MCAST_PACKET_S_BIT)
334#define MCAST_PACKET_SEND_CLR(p) ((p)->flags &= ~MCAST_PACKET_S_BIT)
340#define MCAST_PACKET_IS_LISTED(p) ((p)->flags & MCAST_PACKET_L_BIT)
346#define MCAST_PACKET_LISTED_SET(p) ((p)->flags |= MCAST_PACKET_L_BIT)
352#define MCAST_PACKET_LISTED_CLR(p) ((p)->flags &= ~MCAST_PACKET_L_BIT)
358#define MCAST_PACKET_FREE(p) ((p)->flags = 0)
361struct sequence_list_header {
367#define SEQUENCE_LIST_S_BIT 0x80
368#define SEQUENCE_LIST_M_BIT 0x40
369#define SEQUENCE_LIST_RES 0x3F
375#define SEQUENCE_LIST_GET_M(l) \
376 ((uint8_t)(((l)->flags & SEQUENCE_LIST_M_BIT) == SEQUENCE_LIST_M_BIT))
382#define SEQUENCE_LIST_GET_S(l) \
383 ((uint8_t)(((l)->flags & SEQUENCE_LIST_S_BIT) == SEQUENCE_LIST_S_BIT))
389#if ROLL_TM_SHORT_SEEDS
394#if !ROLL_TM_SHORT_SEEDS
401#define HBHO_OPT_TYPE_TRICKLE 0x0C
402#define HBHO_LEN_LONG_SEED 2
403#define HBHO_LEN_SHORT_SEED 4
404#define HBHO_TOTAL_LEN 8
409#define HBH_GET_M(h) (((h)->flags & 0x80) == 0x80)
415#define HBH_SET_M(h) ((h)->flags |= 0x80)
421#define HBH_GET_SV_MSB(h) ((h)->flags & 0x7F)
424#if ROLL_TM_CONF_DEST_ALL_NODES
425#define roll_tm_create_dest(a) uip_create_linklocal_allnodes_mcast(a)
427#define roll_tm_create_dest(a) uip_create_linklocal_allrouters_mcast(a)
434#define ROLL_TM_STATS_ADD(x) stats.x++
435#define ROLL_TM_STATS_INIT() do { memset(&stats, 0, sizeof(stats)); } while(0)
437#define ROLL_TM_STATS_ADD(x)
438#define ROLL_TM_STATS_INIT()
443static struct trickle_param t[2];
449static struct trickle_param *loctpptr;
450static struct sequence_list_header *locslhptr;
451static struct sliding_window *locswptr;
452static struct sliding_window *iterswptr;
453static struct mcast_packet *locmpptr;
454static struct hbho_mcast *lochbhmptr;
455static uint16_t last_seq;
459#define UIP_EXT_BUF ((struct uip_ext_hdr *)UIP_IP_PAYLOAD(0))
460#define UIP_EXT_BUF_NEXT ((uint8_t *)(UIP_IP_PAYLOAD(HBHO_TOTAL_LEN)))
461#define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)(UIP_IP_PAYLOAD(0) + 2))
462extern uint16_t uip_slen;
466static void icmp_input(
void);
467static void icmp_output(
void);
468static void window_update_bounds(
void);
469static void reset_trickle_timer(uint8_t);
470static void handle_timer(
void *);
474 UIP_ICMP6_HANDLER_CODE_ANY, icmp_input);
479random_interval(clock_time_t i_min, uint8_t d)
483 VERBOSE_PRINTF(
"ROLL TM: Random [%lu, %lu)\n", (
unsigned long)min,
491double_interval(
void *ptr)
493 struct trickle_param *param = (
struct trickle_param *)ptr;
501 offset = (int16_t)(
clock_time() - param->t_end);
504 if(param->i_current < param->i_max) {
508 param->t_start = param->t_end;
509 param->t_end = param->t_start + (param->i_min << param->i_current);
511 next = random_interval(param->i_min, param->i_current);
517 param->t_next = next;
518 ctimer_set(¶m->ct, param->t_next, handle_timer, (
void *)param);
520 VERBOSE_PRINTF(
"ROLL TM: Doubling at %lu (offset %d), Start %lu, End %lu,"
522 (
unsigned long)param->t_start,
523 (
unsigned long)param->t_end, (
unsigned long)param->t_next);
531handle_timer(
void *ptr)
533 struct trickle_param *param;
534 clock_time_t diff_last;
535 clock_time_t diff_start;
538 param = (
struct trickle_param *)ptr;
541 }
else if(param == &t[1]) {
549 if(uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) {
551 (
"ROLL TM: Suppressing timer processing. Stack not ready\n");
552 reset_trickle_timer(m);
556 VERBOSE_PRINTF(
"ROLL TM: M=%u Periodic at %lu, last=%lu\n",
558 (
unsigned long)param->t_last_trigger);
562 diff_last = param->t_next - param->t_last_trigger;
563 diff_start = param->t_next - param->t_start;
564 param->t_last_trigger = param->t_next;
567 (
"ROLL TM: M=%u Periodic diff from last %lu, from start %lu\n", m,
568 (
unsigned long)diff_last, (
unsigned long)diff_start);
572 locmpptr >= buffered_msgs; locmpptr--) {
589 if(locmpptr->active == 0) {
590 locmpptr->active += diff_start;
591 locmpptr->dwell += diff_start;
593 locmpptr->active += diff_last;
594 locmpptr->dwell += diff_last;
597 VERBOSE_PRINTF(
"ROLL TM: M=%u Packet %u active %lu of %lu\n",
598 m, locmpptr->seq_val, locmpptr->active,
602 locmpptr->sw->count--;
603 PRINTF(
"ROLL TM: M=%u Free Packet %u (%lu > %lu), Window now at %u\n",
604 m, locmpptr->seq_val, locmpptr->dwell,
606 if(locmpptr->sw->count == 0) {
607 PRINTF(
"ROLL TM: M=%u Free Window ", m);
608 PRINT_SEED(&locmpptr->sw->seed_id);
610 window_free(locmpptr->sw);
618 PRINTF(
"ROLL TM: M=%u Periodic - Sending packet from Seed ", m);
619 PRINT_SEED(&locmpptr->sw->seed_id);
620 PRINTF(
" seq %u\n", locmpptr->seq_val);
624 UIP_MCAST6_STATS_ADD(mcast_fwd);
635 if(param->c < param->k) {
641 param->inconsistency = 0;
644 window_update_bounds();
648 if(param->t_next >= param->t_end) {
652 param->t_next = param->t_end - param->t_next;
655 (
"ROLL TM: M=%u Periodic at %lu, Interval End at %lu in %lu\n", m,
656 (
unsigned long)
clock_time(), (
unsigned long)param->t_end,
657 (
unsigned long)param->t_next);
658 ctimer_set(¶m->ct, param->t_next, double_interval, (
void *)param);
664reset_trickle_timer(uint8_t index)
667 t[index].t_end = t[index].t_start + (t[index].i_min);
668 t[index].i_current = 0;
670 t[index].t_next = random_interval(t[index].i_min, t[index].i_current);
673 (
"ROLL TM: M=%u Reset at %lu, Start %lu, End %lu, New Interval %lu\n",
674 index, (
unsigned long)t[index].t_start, (
unsigned long)t[index].t_start,
675 (
unsigned long)t[index].t_end, (
unsigned long)t[index].t_next);
677 ctimer_set(&t[index].ct, t[index].t_next, handle_timer, (
void *)&t[index]);
680static struct sliding_window *
683 for(iterswptr = &windows[
ROLL_TM_WINS - 1]; iterswptr >= windows;
686 iterswptr->count = 0;
687 iterswptr->lower_bound = -1;
688 iterswptr->upper_bound = -1;
689 iterswptr->min_listed = -1;
696static struct sliding_window *
697window_lookup(seed_id_t *s, uint8_t m)
699 for(iterswptr = &windows[
ROLL_TM_WINS - 1]; iterswptr >= windows;
702 VERBOSE_PRINT_SEED(&iterswptr->seed_id);
703 VERBOSE_PRINTF(
"\n");
713window_update_bounds()
715 for(iterswptr = &windows[
ROLL_TM_WINS - 1]; iterswptr >= windows;
717 iterswptr->lower_bound = -1;
721 locmpptr >= buffered_msgs; locmpptr--) {
723 iterswptr = locmpptr->sw;
724 VERBOSE_PRINTF(
"ROLL TM: Update Bounds: [%d - %d] vs %u\n",
725 iterswptr->lower_bound, iterswptr->upper_bound,
727 if(iterswptr->lower_bound < 0
728 ||
SEQ_VAL_IS_LT(locmpptr->seq_val, iterswptr->lower_bound)) {
729 iterswptr->lower_bound = locmpptr->seq_val;
731 if(iterswptr->upper_bound < 0 ||
733 iterswptr->upper_bound = locmpptr->seq_val;
739static struct mcast_packet *
742 struct sliding_window *largest = windows;
743 struct mcast_packet *rv;
745 for(iterswptr = &windows[
ROLL_TM_WINS - 1]; iterswptr >= windows;
747 if(iterswptr->count > largest->count) {
752 if(largest->count == 1) {
757 PRINTF(
"ROLL TM: Reclaim from Seed ");
758 PRINT_SEED(&largest->seed_id);
759 PRINTF(
" M=%u, count was %u\n",
763 locmpptr >= buffered_msgs; locmpptr--) {
767 PRINTF(
"ROLL TM: Reclaim seq. val %u\n", locmpptr->seq_val);
770 window_update_bounds();
771 VERBOSE_PRINTF(
"ROLL TM: Reclaim - new bounds [%u , %u]\n",
772 largest->lower_bound, largest->upper_bound);
781static struct mcast_packet *
785 locmpptr >= buffered_msgs; locmpptr--) {
796 struct sequence_list_header *sl;
798 uint16_t payload_len;
800 PRINTF(
"ROLL TM: ICMPv6 Out\n");
808 sl = (
struct sequence_list_header *)UIP_ICMP_PAYLOAD;
811 VERBOSE_PRINTF(
"ROLL TM: ICMPv6 Out - Hdr @ %p, payload @ %p\n",
UIP_ICMP_BUF, sl);
813 for(iterswptr = &windows[
ROLL_TM_WINS - 1]; iterswptr >= windows;
816 memset(sl, 0,
sizeof(
struct sequence_list_header));
817#if ROLL_TM_SHORT_SEEDS
818 sl->flags = SEQUENCE_LIST_S_BIT;
821 sl->flags |= SEQUENCE_LIST_M_BIT;
825 PRINTF(
"ROLL TM: ICMPv6 Out - Seq. F=0x%02x, Seed ID=", sl->flags);
826 PRINT_SEED(&sl->seed_id);
828 buffer = (uint8_t *)sl +
sizeof(
struct sequence_list_header);
831 locmpptr >= buffered_msgs; locmpptr--) {
834 if(locmpptr->sw == iterswptr) {
836 PRINTF(
", %u", locmpptr->seq_val);
837 *buffer = (uint8_t)(locmpptr->seq_val >> 8);
839 *buffer = (uint8_t)(locmpptr->seq_val & 0xFF);
844 PRINTF(
", Len=%u\n", sl->seq_len);
847 if(sl->seq_len > 0) {
848 payload_len +=
sizeof(
struct sequence_list_header) + sl->seq_len * 2;
849 sl = (
struct sequence_list_header *)buffer;
854 if(payload_len == 0) {
855 VERBOSE_PRINTF(
"ROLL TM: ICMPv6 Out - nothing to send\n");
862 uipbuf_set_len_field(
UIP_IP_BUF, UIP_ICMPH_LEN + payload_len);
870 uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + payload_len;
872 VERBOSE_PRINTF(
"ROLL TM: ICMPv6 Out - %u bytes\n", payload_len);
875 ROLL_TM_STATS_ADD(icmp_out);
894 PRINTF(
"ROLL TM: Multicast I/O\n");
896#if UIP_CONF_IPV6_CHECKS
898 PRINTF(
"ROLL TM: Mcast I/O, bad destination\n");
899 UIP_MCAST6_STATS_ADD(mcast_bad);
900 return UIP_MCAST6_DROP;
907 PRINTF(
"ROLL TM: Mcast I/O, bad source\n");
908 UIP_MCAST6_STATS_ADD(mcast_bad);
909 return UIP_MCAST6_DROP;
915 PRINTF(
"ROLL TM: Mcast I/O, bad proto\n");
916 UIP_MCAST6_STATS_ADD(mcast_bad);
917 return UIP_MCAST6_DROP;
920 if(UIP_EXT_OPT_FIRST->type != HBHO_OPT_TYPE_TRICKLE) {
921 PRINTF(
"ROLL TM: Mcast I/O, bad HBHO type\n");
922 UIP_MCAST6_STATS_ADD(mcast_bad);
923 return UIP_MCAST6_DROP;
926 lochbhmptr = UIP_EXT_OPT_FIRST;
928 PRINTF(
"ROLL TM: HBHO T=%u, L=%u, M=%u, S=0x%02x%02x\n",
929 lochbhmptr->type, lochbhmptr->len,
HBH_GET_M(lochbhmptr),
933#if ROLL_TM_SHORT_SEEDS
935 if(lochbhmptr->len != HBHO_LEN_SHORT_SEED) {
936 PRINTF(
"ROLL TM: Mcast I/O, bad length\n");
937 UIP_MCAST6_STATS_ADD(mcast_bad);
938 return UIP_MCAST6_DROP;
942 if(lochbhmptr->len != HBHO_LEN_LONG_SEED) {
943 PRINTF(
"ROLL TM: Mcast I/O, bad length\n");
944 UIP_MCAST6_STATS_ADD(mcast_bad);
945 return UIP_MCAST6_DROP;
950 if(in == ROLL_TM_DGRAM_IN) {
951 UIP_MCAST6_STATS_ADD(mcast_in_all);
956#if ROLL_TM_SHORT_SEEDS
957 seed_ptr = &lochbhmptr->seed_id;
963 locswptr = window_lookup(seed_ptr, m);
965 seq_val = lochbhmptr->seq_id_lsb;
971 PRINTF(
"ROLL TM: Too old\n");
972 UIP_MCAST6_STATS_ADD(mcast_dropped);
973 return UIP_MCAST6_DROP;
976 locmpptr >= buffered_msgs; locmpptr--) {
978 locmpptr->sw == locswptr &&
982 PRINTF(
"ROLL TM: Seen before\n");
983 UIP_MCAST6_STATS_ADD(mcast_dropped);
984 return UIP_MCAST6_DROP;
989 PRINTF(
"ROLL TM: New message\n");
994 locswptr = window_allocate();
995 PRINTF(
"ROLL TM: New seed\n");
999 PRINTF(
"ROLL TM: Failed to allocate window\n");
1000 UIP_MCAST6_STATS_ADD(mcast_dropped);
1001 return UIP_MCAST6_DROP;
1005 locmpptr = buffer_allocate();
1007 PRINTF(
"ROLL TM: Buffer allocation failed, reclaiming\n");
1008 locmpptr = buffer_reclaim();
1014 PRINTF(
"ROLL TM: Buffer reclaim failed\n");
1015 if(locswptr->count == 0) {
1016 window_free(locswptr);
1017 UIP_MCAST6_STATS_ADD(mcast_dropped);
1018 return UIP_MCAST6_DROP;
1022 if(in == ROLL_TM_DGRAM_IN) {
1023 UIP_MCAST6_STATS_ADD(mcast_in_unique);
1035 PRINTF(
"ROLL TM: Window for seed ");
1036 PRINT_SEED(&locswptr->seed_id);
1037 PRINTF(
" M=%u, count=%u\n",
1041 if(locswptr->count == 0) {
1042 locswptr->lower_bound = seq_val;
1043 VERBOSE_PRINTF(
"ROLL TM: New Lower Bound %u\n", locswptr->lower_bound);
1047 if(locswptr->count == 0 ||
SEQ_VAL_IS_GT(seq_val, locswptr->upper_bound)) {
1048 locswptr->upper_bound = seq_val;
1049 VERBOSE_PRINTF(
"ROLL TM: New Upper Bound %u\n", locswptr->upper_bound);
1054 memset(locmpptr, 0,
sizeof(
struct mcast_packet));
1056 locmpptr->sw = locswptr;
1058 locmpptr->seq_val = seq_val;
1061 PRINTF(
"ROLL TM: Window for seed ");
1062 PRINT_SEED(&locswptr->seed_id);
1063 PRINTF(
" M=%u, %u values within [%u , %u]\n",
1065 locswptr->lower_bound, locswptr->upper_bound);
1073 if(in == ROLL_TM_DGRAM_IN) {
1077 t[m].inconsistency = 1;
1079 PRINTF(
"ROLL TM: Inconsistency. Reset T%u\n", m);
1080 reset_trickle_timer(m);
1084 return UIP_MCAST6_ACCEPT;
1091 uint8_t inconsistency;
1096#if UIP_CONF_IPV6_CHECKS
1098 PRINTF(
"ROLL TM: ICMPv6 In, bad source ");
1103 ROLL_TM_STATS_ADD(icmp_bad);
1109 PRINTF(
"ROLL TM: ICMPv6 In, bad destination\n");
1110 ROLL_TM_STATS_ADD(icmp_bad);
1115 PRINTF(
"ROLL TM: ICMPv6 In, bad ICMP code\n");
1116 ROLL_TM_STATS_ADD(icmp_bad);
1121 PRINTF(
"ROLL TM: ICMPv6 In, bad TTL\n");
1122 ROLL_TM_STATS_ADD(icmp_bad);
1127 PRINTF(
"ROLL TM: ICMPv6 In from ");
1134 for(iterswptr = &windows[
ROLL_TM_WINS - 1]; iterswptr >= windows;
1141 locmpptr >= buffered_msgs; locmpptr--) {
1145 locslhptr = (
struct sequence_list_header *)UIP_ICMP_PAYLOAD;
1147 VERBOSE_PRINTF(
"ROLL TM: ICMPv6 In, parse from %p to %p\n",
1149 (uint8_t *)UIP_ICMP_PAYLOAD +
uip_len -
1150 uip_l3_icmp_hdr_len);
1152 (
struct sequence_list_header *)((uint8_t *)UIP_ICMP_PAYLOAD +
1153 uip_len - uip_l3_icmp_hdr_len)) {
1154 VERBOSE_PRINTF(
"ROLL TM: ICMPv6 In, seq hdr @ %p\n", locslhptr);
1156 if((locslhptr->flags & SEQUENCE_LIST_RES) != 0) {
1157 PRINTF(
"ROLL TM: ICMPv6 In, non-zero reserved bits\n");
1162#if ROLL_TM_SHORT_SEEDS
1164 ROLL_TM_STATS_ADD(icmp_bad);
1169 ROLL_TM_STATS_ADD(icmp_bad);
1174 PRINTF(
"ROLL TM: ICMPv6 In, Sequence List for Seed ID ");
1175 PRINT_SEED(&locslhptr->seed_id);
1179 seq_ptr = (uint16_t *)((uint8_t *)locslhptr
1180 +
sizeof(
struct sequence_list_header));
1181 end_ptr = (uint16_t *)((uint8_t *)locslhptr
1182 +
sizeof(
struct sequence_list_header) +
1183 locslhptr->seq_len * 2);
1191 locswptr = window_lookup(&locslhptr->seed_id,
1197 locswptr->min_listed = -1;
1198 PRINTF(
"ROLL TM: ICMPv6 In, Window bounds [%u , %u]\n",
1199 locswptr->lower_bound, locswptr->upper_bound);
1200 for(; seq_ptr < end_ptr; seq_ptr++) {
1204 PRINTF(
"ROLL TM: ICMPv6 In, Check seq %u @ %p\n", val, seq_ptr);
1206 PRINTF(
"ROLL TM: Inconsistency - Advertised Seq. ID %u GT upper"
1207 " bound %u\n", val, locswptr->upper_bound);
1208 loctpptr->inconsistency = 1;
1220 locmpptr >= buffered_msgs; locmpptr--) {
1226 PRINTF(
"ROLL TM: ICMPv6 In, %u listed\n", locmpptr->seq_val);
1230 if(locswptr->min_listed == -1 ||
1232 locswptr->min_listed = val;
1239 PRINTF(
"ROLL TM: Inconsistency - ");
1240 PRINTF(
"Advertised Seq. ID %u within bounds", val);
1241 PRINTF(
" [%u, %u] but no matching entry\n",
1242 locswptr->lower_bound, locswptr->upper_bound);
1243 loctpptr->inconsistency = 1;
1252 PRINTF(
"ROLL TM: Inconsistency - Advertised window unknown to us\n");
1253 loctpptr->inconsistency = 1;
1255 locslhptr = (
struct sequence_list_header *)(((uint8_t *)locslhptr) +
1256 sizeof(
struct sequence_list_header) + (2 * locslhptr->seq_len));
1261 PRINTF(
"ROLL TM: ICMPv6 In, Check our buffer\n");
1263 locmpptr >= buffered_msgs; locmpptr--) {
1265 locswptr = locmpptr->sw;
1266 PRINTF(
"ROLL TM: ICMPv6 In, ");
1267 PRINTF(
"Check %u, Seed L: %u, This L: %u Min L: %d\n",
1275 PRINTF(
"ROLL TM: Inconsistency - Seed ID ");
1276 PRINT_SEED(&locswptr->seed_id);
1277 PRINTF(
" was not listed\n");
1278 loctpptr->inconsistency = 1;
1283 (locswptr->min_listed >= 0) &&
1285 PRINTF(
"ROLL TM: Inconsistency - ");
1286 PRINTF(
"Seq. %u was not listed but %u was\n",
1287 locmpptr->seq_val, locswptr->min_listed);
1288 loctpptr->inconsistency = 1;
1297 if(t[0].inconsistency) {
1298 reset_trickle_timer(0);
1302 if(t[1].inconsistency) {
1303 reset_trickle_timer(1);
1319 PRINTF(
"ROLL TM: Multicast Out can not add HBHO. Packet too long\n");
1324 memmove(UIP_EXT_BUF_NEXT, UIP_EXT_BUF,
uip_len - UIP_IPH_LEN);
1325 memset(UIP_EXT_BUF, 0, HBHO_TOTAL_LEN);
1328 UIP_EXT_BUF->len = 0;
1330 lochbhmptr = UIP_EXT_OPT_FIRST;
1331 lochbhmptr->type = HBHO_OPT_TYPE_TRICKLE;
1335 lochbhmptr->flags = last_seq >> 8;
1336 lochbhmptr->seq_id_lsb = last_seq & 0xFF;
1337#if ROLL_TM_SHORT_SEEDS
1339 lochbhmptr->len = HBHO_LEN_SHORT_SEED;
1341 lochbhmptr->len = HBHO_LEN_LONG_SEED;
1343 lochbhmptr->padn_type = UIP_EXT_HDR_OPT_PADN;
1344 lochbhmptr->padn_len = 0;
1348#if ROLL_TM_SET_M_BIT
1352 uipbuf_add_ext_hdr(HBHO_TOTAL_LEN);
1358 PRINTF(
"ROLL TM: Multicast Out, HBHO: T=%u, L=%u, M=%u, S=0x%02x%02x\n",
1359 lochbhmptr->type, lochbhmptr->len,
HBH_GET_M(lochbhmptr),
1371 if(
accept(ROLL_TM_DGRAM_OUT)) {
1373 UIP_MCAST6_STATS_ADD(mcast_out);
1389 if(
accept(ROLL_TM_DGRAM_IN) == UIP_MCAST6_DROP) {
1390 return UIP_MCAST6_DROP;
1393 if(!uip_ds6_is_my_maddr(&
UIP_IP_BUF->destipaddr)) {
1394 PRINTF(
"ROLL TM: Not a group member. No further processing\n");
1395 return UIP_MCAST6_DROP;
1397 PRINTF(
"ROLL TM: Ours. Deliver to upper layers\n");
1398 UIP_MCAST6_STATS_ADD(mcast_in_ours);
1399 return UIP_MCAST6_ACCEPT;
1406 PRINTF(
"ROLL TM: ROLL Multicast - Draft #%u\n",
ROLL_TM_VER);
1408 memset(windows, 0,
sizeof(windows));
1409 memset(buffered_msgs, 0,
sizeof(buffered_msgs));
1410 memset(t, 0,
sizeof(t));
1412 ROLL_TM_STATS_INIT();
1413 UIP_MCAST6_STATS_INIT(&stats);
1418 for(iterswptr = &windows[
ROLL_TM_WINS - 1]; iterswptr >= windows;
1420 iterswptr->lower_bound = -1;
1421 iterswptr->upper_bound = -1;
1422 iterswptr->min_listed = -1;
1426 reset_trickle_timer(0);
1428 reset_trickle_timer(1);
clock_time_t clock_time(void)
Get the current clock time.
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
void watchdog_periodic(void)
Writes the WDT clear sequence.
static void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
#define seed_id_cpy(a, b)
Copy one seed_id_t into another.
static void icmp_in(void)
#define seed_id_cmp(a, b)
Compare two contiki seed ids represented as seed_id_t types a: First value to compare b: Second value...
#define SLIDING_WINDOW_IS_USED(w)
Is Occupied sliding window location w w: pointer to a sliding window.
#define TIMER_CONFIGURE(m)
Init trickle_timer[m].
#define SUPPRESSION_ENABLED(t)
Check if suppression is enabled for trickle_param t t is a pointer to the timer.
#define SLIDING_WINDOW_M_SET(w)
Set M bit for window w w: pointer to a sliding window.
#define SLIDING_WINDOW_IS_LISTED(w)
Is the sliding window at location w listed in current ICMP message? w: pointer to a sliding window.
#define MCAST_PACKET_IS_USED(p)
Is Occupied buffer location p.
#define ROLL_TM_WINS
Number of Sliding Windows In essence: How many unique sources of simultaneous multicast traffic do we...
#define ROLL_TM_ICMP_CODE
ROLL TM ICMPv6 code field.
#define HBH_GET_M(h)
Get the Trickle Parametrization for a multicast HBHO header m: pointer to the HBHO header.
#define SUPPRESSION_DISABLED(t)
Check if suppression is disabled for trickle_param t t is a pointer to the timer.
#define ROLL_TM_VER
Supported Draft Version.
#define MCAST_PACKET_LISTED_CLR(p)
Clear 'Is Listed' bit for message p p: pointer to a struct mcast_packet.
#define MCAST_PACKET_USED_SET(p)
Set 'Is Used' bit for packet p p: pointer to a packet buffer.
#define SLIDING_WINDOW_LISTED_CLR(w)
Clear 'Is Seen' bit for window w w: pointer to a sliding window.
#define SEQ_VAL_ADD(s, n)
Add n to s: (s + n) modulo (2 ^ SERIAL_BITS) => ((s + n) % 0x8000)
#define HBH_GET_SV_MSB(h)
Retrieve the Sequence Value MSB from a multicast HBHO header m: pointer to the HBHO header.
static uint8_t accept(uint8_t in)
Processes an incoming or outgoing multicast message and determines whether it should be dropped or ac...
#define MCAST_PACKET_TTL(p)
Get the TTL of a buffered packet p: pointer to a packet buffer.
#define SEQ_VAL_IS_LT(i1, i2)
s1 is said to be less than s2 iif SEQ_VAL_IS_LT(s1, s2) == 1
const struct uip_mcast6_driver roll_tm_driver
The ROLL TM engine driver.
#define SLIDING_WINDOW_IS_USED_SET(w)
Set 'Is Used' bit for window w w: pointer to a sliding window.
#define MCAST_PACKET_SEND_CLR(p)
Clear 'Must Send' bit for message p p: pointer to a struct mcast_packet.
#define TRICKLE_TIME(m, d)
Convert a timer to a sane clock_time_t value after d doublings m is a value of Imin,...
#define TRICKLE_DWELL(t)
Convert Tdwell for a trickle timer to a sane clock_time_t value t is a pointer to the timer Careful o...
#define SEQ_VAL_IS_EQ(i1, i2)
s1 is said to be equal s2 iif SEQ_VAL_IS_EQ(s1, s2) == 1
#define MCAST_PACKET_LISTED_SET(p)
Set 'Is Listed' bit for message p p: pointer to a struct mcast_packet.
#define MCAST_PACKET_FREE(p)
Free a multicast packet buffer p: pointer to a struct mcast_packet.
#define ROLL_TM_IP_HOP_LIMIT
Hop limit for ICMP messages.
#define SLIDING_WINDOW_LISTED_SET(w)
Set 'Is Seen' bit for window w w: pointer to a sliding window.
#define SLIDING_WINDOW_M_CLR(w)
Clear M bit for window w w: pointer to a sliding window.
#define SEQUENCE_LIST_GET_M(l)
Get the Trickle Parametrization for an ICMPv6 sequence list l: pointer to a sequence list structure.
#define SEQUENCE_LIST_GET_S(l)
Get the Seed ID Length for an ICMPv6 sequence list l: pointer to a sequence list structure.
#define MCAST_PACKET_MUST_SEND(p)
Must we send this message this pass?
#define SEQ_VAL_IS_GT(i1, i2)
s1 is said to be greater than s2 iif SEQ_VAL_IS_LT(s1, s2) == 1
#define MCAST_PACKET_IS_LISTED(p)
Is the message p listed in current ICMP message? p: pointer to a struct mcast_packet.
#define MCAST_PACKET_SEND_SET(p)
Set 'Must Send' bit for message p p: pointer to a struct mcast_packet.
#define SLIDING_WINDOW_GET_M(w)
Retrieve trickle parametrization for sliding window at location w w: pointer to a sliding window.
#define TRICKLE_ACTIVE(t)
Convert Tactive for a trickle timer to a sane clock_time_t value t is a pointer to the timer Careful ...
#define HBH_SET_M(h)
Set the Trickle Parametrization bit for a multicast HBHO header m: pointer to the HBHO header.
#define ROLL_TM_BUFF_NUM
Maximum Number of Buffered Multicast Messages This buffer is shared across all Seed IDs,...
uint8_t tcpip_output(const uip_lladdr_t *a)
Output packet to layer 2 The eventual parameter is the MAC address of the destination.
void tcpip_ipv6_output(void)
This function does address resolution and then calls tcpip_output.
#define ICMP6_ROLL_TM
ROLL Trickle Multicast.
uip_lladdr_t uip_lladdr
Host L2 address.
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
#define UIP_ICMP_BUF
Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset (global uip_...
#define UIP_LLADDR_LEN
802.15.4 address
#define uip_is_addr_mcast_non_routable(a)
is address a non-routable multicast address.
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
#define uip_is_addr_linklocal_allrouters_mcast(a)
Is IPv6 address a the link local all-routers multicast address.
#define uip_is_addr_linklocal_allnodes_mcast(a)
Is IPv6 address a the link local all-nodes multicast address.
void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler)
Register a handler which can handle a specific ICMPv6 message type.
#define UIP_PROTO_HBHO
extension headers types
#define UIP_IP_BUF
Direct access to IPv6 header.
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
uint16_t uip_ext_len
The length of the extension headers.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Header file for the implementation of the ROLL-TM multicast engine.
Multicast stats extension for the ROLL TM engine.
The data structure used to represent a multicast engine.
void(* out)(void)
Process an outgoing datagram with a multicast IPv6 destination address.
uint8_t(* in)(void)
Process an incoming multicast datagram and determine whether it should be delivered up the stack or n...
void(* init)(void)
Initialize the multicast engine.
A set of debugging macros for the IP stack.
Header file for ICMPv6 message and error handing (RFC 4443)
This header file contains configuration directives for uIPv6 multicast support.