2017-06-10 12:35:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-09-29 20:18:48 +00:00
|
|
|
* \addtogroup rpl-lite
|
|
|
|
* @{
|
|
|
|
*
|
2017-06-10 12:35:39 +00:00
|
|
|
* \file
|
2017-06-19 09:35:02 +00:00
|
|
|
* Logic for DAG neighbors in RPL.
|
2017-06-10 12:35:39 +00:00
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>,
|
|
|
|
* Simon Duquennoy <simon.duquennoy@inria.fr>
|
|
|
|
* Contributors: George Oikonomou <oikonomou@users.sourceforge.net> (multicast)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
2017-12-09 12:55:57 +00:00
|
|
|
#include "net/routing/rpl-lite/rpl.h"
|
2017-06-10 12:35:39 +00:00
|
|
|
#include "net/link-stats.h"
|
|
|
|
#include "net/nbr-table.h"
|
|
|
|
|
2017-06-22 08:19:36 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "sys/log.h"
|
|
|
|
#define LOG_MODULE "RPL"
|
2017-07-15 13:19:09 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_RPL
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/* A configurable function called after every RPL parent switch */
|
|
|
|
#ifdef RPL_CALLBACK_PARENT_SWITCH
|
2017-06-19 09:35:02 +00:00
|
|
|
void RPL_CALLBACK_PARENT_SWITCH(rpl_nbr_t *old, rpl_nbr_t *new);
|
2017-06-10 12:35:39 +00:00
|
|
|
#endif /* RPL_CALLBACK_PARENT_SWITCH */
|
|
|
|
|
2017-06-27 09:06:38 +00:00
|
|
|
static rpl_nbr_t * best_parent(int fresh_only);
|
|
|
|
|
2017-06-10 12:35:39 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-19 09:35:02 +00:00
|
|
|
/* Per-neighbor RPL information */
|
|
|
|
NBR_TABLE_GLOBAL(rpl_nbr_t, rpl_neighbors);
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-19 09:35:02 +00:00
|
|
|
/* As per RFC 6550, section 8.2.2.4 */
|
2017-06-10 12:35:39 +00:00
|
|
|
static int
|
|
|
|
acceptable_rank(rpl_rank_t rank)
|
|
|
|
{
|
|
|
|
return rank != RPL_INFINITE_RANK
|
|
|
|
&& rank >= ROOT_RANK
|
|
|
|
&& ((curr_instance.max_rankinc == 0) ||
|
2017-06-27 09:06:38 +00:00
|
|
|
rank <= curr_instance.dag.lowest_rank + curr_instance.max_rankinc);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_print_list(const char *str)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
|
|
|
if(curr_instance.used) {
|
|
|
|
int curr_dio_interval = curr_instance.dag.dio_intcurrent;
|
|
|
|
int curr_rank = curr_instance.dag.rank;
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_nbr_t *nbr = nbr_table_head(rpl_neighbors);
|
2017-06-27 09:06:38 +00:00
|
|
|
rpl_nbr_t *best = best_parent(0);
|
2017-06-10 12:35:39 +00:00
|
|
|
clock_time_t clock_now = clock_time();
|
|
|
|
|
2017-06-23 19:12:36 +00:00
|
|
|
LOG_INFO("nbr: own state, addr ");
|
|
|
|
LOG_INFO_6ADDR(rpl_get_global_address());
|
2017-06-29 14:06:50 +00:00
|
|
|
LOG_INFO_(", DAG state: %s, MOP %u OCP %u rank %u max-rank %u, dioint %u, nbr count %u (%s)\n",
|
2017-06-27 09:06:38 +00:00
|
|
|
rpl_dag_state_to_str(curr_instance.dag.state),
|
2017-06-10 12:35:39 +00:00
|
|
|
curr_instance.mop, curr_instance.of->ocp, curr_rank,
|
2017-06-23 19:23:12 +00:00
|
|
|
curr_instance.max_rankinc != 0 ? curr_instance.dag.lowest_rank + curr_instance.max_rankinc : 0xffff,
|
2017-06-29 14:06:50 +00:00
|
|
|
curr_dio_interval, rpl_neighbor_count(), str);
|
2017-06-19 09:35:02 +00:00
|
|
|
while(nbr != NULL) {
|
|
|
|
const struct link_stats *stats = rpl_neighbor_get_link_stats(nbr);
|
2017-06-23 19:12:36 +00:00
|
|
|
LOG_INFO("nbr: ");
|
|
|
|
LOG_INFO_6ADDR(rpl_neighbor_get_ipaddr(nbr));
|
2017-06-27 09:06:38 +00:00
|
|
|
LOG_INFO_(" %5u, %5u => %5u -- %2u %c%c%c%c%c",
|
2017-06-19 09:35:02 +00:00
|
|
|
nbr->rank,
|
|
|
|
rpl_neighbor_get_link_metric(nbr),
|
|
|
|
rpl_neighbor_rank_via_nbr(nbr),
|
2017-06-10 12:35:39 +00:00
|
|
|
stats != NULL ? stats->freshness : 0,
|
2017-06-23 19:12:36 +00:00
|
|
|
(nbr->rank == ROOT_RANK) ? 'r' : ' ',
|
2017-06-27 09:06:38 +00:00
|
|
|
nbr == best ? 'b' : ' ',
|
2017-06-29 14:06:50 +00:00
|
|
|
(acceptable_rank(rpl_neighbor_rank_via_nbr(nbr)) && rpl_neighbor_is_acceptable_parent(nbr)) ? 'a' : ' ',
|
2017-06-10 12:35:39 +00:00
|
|
|
link_stats_is_fresh(stats) ? 'f' : ' ',
|
2017-06-23 19:23:12 +00:00
|
|
|
nbr == curr_instance.dag.preferred_parent ? 'p' : ' '
|
2017-06-10 12:35:39 +00:00
|
|
|
);
|
2017-06-23 19:23:12 +00:00
|
|
|
if(stats->last_tx_time > 0) {
|
2017-06-27 17:22:56 +00:00
|
|
|
LOG_INFO_(" (last tx %u min ago", (unsigned)((clock_now - stats->last_tx_time) / (60 * CLOCK_SECOND)));
|
2017-06-23 19:23:12 +00:00
|
|
|
} else {
|
2017-06-27 17:22:56 +00:00
|
|
|
LOG_INFO_(" (no tx");
|
|
|
|
}
|
|
|
|
if(nbr->better_parent_since > 0) {
|
|
|
|
LOG_INFO_(", better since %u min)\n", (unsigned)((clock_now - nbr->better_parent_since) / (60 * CLOCK_SECOND)));
|
|
|
|
} else {
|
|
|
|
LOG_INFO_(")\n");
|
2017-06-23 19:23:12 +00:00
|
|
|
}
|
2017-06-19 09:35:02 +00:00
|
|
|
nbr = nbr_table_next(rpl_neighbors, nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
2017-06-23 19:12:36 +00:00
|
|
|
LOG_INFO("nbr: end of list\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-29 14:06:50 +00:00
|
|
|
int
|
|
|
|
rpl_neighbor_count(void)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
rpl_nbr_t *nbr = nbr_table_head(rpl_neighbors);
|
|
|
|
for(nbr = nbr_table_head(rpl_neighbors);
|
|
|
|
nbr != NULL;
|
|
|
|
nbr = nbr_table_next(rpl_neighbors, nbr)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-10 12:35:39 +00:00
|
|
|
#if UIP_ND6_SEND_NS
|
|
|
|
static uip_ds6_nbr_t *
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_get_ds6_nbr(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
const linkaddr_t *lladdr = rpl_neighbor_get_lladdr(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
if(lladdr != NULL) {
|
|
|
|
return nbr_table_get_from_lladdr(ds6_neighbors, lladdr);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* UIP_ND6_SEND_NS */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2017-06-19 09:35:02 +00:00
|
|
|
remove_neighbor(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
/* Make sure we don't point to a removed neighbor. Note that we do not need
|
2017-06-10 12:35:39 +00:00
|
|
|
to worry about preferred_parent here, as it is locked in the the table
|
|
|
|
and will never be removed by external modules. */
|
2017-06-19 09:35:02 +00:00
|
|
|
if(nbr == curr_instance.dag.urgent_probing_target) {
|
2017-06-10 12:35:39 +00:00
|
|
|
curr_instance.dag.urgent_probing_target = NULL;
|
|
|
|
}
|
2017-06-19 09:35:02 +00:00
|
|
|
if(nbr == curr_instance.dag.unicast_dio_target) {
|
2017-06-10 12:35:39 +00:00
|
|
|
curr_instance.dag.unicast_dio_target = NULL;
|
|
|
|
}
|
2017-06-19 09:35:02 +00:00
|
|
|
nbr_table_remove(rpl_neighbors, nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_timers_schedule_state_update(); /* Updating from here is unsafe; postpone */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_nbr_t *
|
|
|
|
rpl_neighbor_get_from_lladdr(uip_lladdr_t *addr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
return nbr_table_get_from_lladdr(rpl_neighbors, (linkaddr_t *)addr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-29 14:06:50 +00:00
|
|
|
int
|
|
|
|
rpl_neighbor_is_acceptable_parent(rpl_nbr_t *nbr)
|
|
|
|
{
|
|
|
|
if(nbr != NULL && curr_instance.of->nbr_is_acceptable_parent != NULL) {
|
|
|
|
return curr_instance.of->nbr_is_acceptable_parent(nbr);
|
|
|
|
}
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-10 12:35:39 +00:00
|
|
|
uint16_t
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_get_link_metric(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
if(nbr != NULL && curr_instance.of->nbr_link_metric != NULL) {
|
|
|
|
return curr_instance.of->nbr_link_metric(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rpl_rank_t
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_rank_via_nbr(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
if(nbr != NULL && curr_instance.of->rank_via_nbr != NULL) {
|
|
|
|
return curr_instance.of->rank_via_nbr(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
return RPL_INFINITE_RANK;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const linkaddr_t *
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_get_lladdr(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
return nbr_table_get_lladdr(rpl_neighbors, nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ipaddr_t *
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_get_ipaddr(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
const linkaddr_t *lladdr = rpl_neighbor_get_lladdr(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
return uip_ds6_nbr_ipaddr_from_lladdr((uip_lladdr_t *)lladdr);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const struct link_stats *
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_get_link_stats(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
const linkaddr_t *lladdr = rpl_neighbor_get_lladdr(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
return link_stats_from_lladdr(lladdr);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_is_fresh(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
const struct link_stats *stats = rpl_neighbor_get_link_stats(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
return link_stats_is_fresh(stats);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_is_reachable(rpl_nbr_t *nbr) {
|
|
|
|
if(nbr == NULL) {
|
2017-06-10 12:35:39 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
#if UIP_ND6_SEND_NS
|
2017-06-19 09:35:02 +00:00
|
|
|
uip_ds6_nbr_t *ds6_nbr = rpl_get_ds6_nbr(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
/* Exclude links to a neighbor that is not reachable at a NUD level */
|
2017-06-19 09:35:02 +00:00
|
|
|
if(ds6_nbr == NULL || ds6_nbr->state != NBR_REACHABLE) {
|
2017-06-10 12:35:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* UIP_ND6_SEND_NS */
|
2017-06-19 09:35:02 +00:00
|
|
|
/* If we don't have fresh link information, assume the nbr is reachable. */
|
|
|
|
return !rpl_neighbor_is_fresh(nbr) || curr_instance.of->nbr_has_usable_link(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-19 09:35:02 +00:00
|
|
|
int
|
|
|
|
rpl_neighbor_is_parent(rpl_nbr_t *nbr)
|
|
|
|
{
|
|
|
|
return nbr != NULL && nbr->rank < curr_instance.dag.rank;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-10 12:35:39 +00:00
|
|
|
void
|
2017-06-27 09:06:38 +00:00
|
|
|
rpl_neighbor_set_preferred_parent(rpl_nbr_t *nbr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
if(curr_instance.dag.preferred_parent != nbr) {
|
2017-06-23 19:23:12 +00:00
|
|
|
LOG_INFO("parent switch: ");
|
2017-06-23 07:21:31 +00:00
|
|
|
LOG_INFO_6ADDR(rpl_neighbor_get_ipaddr(curr_instance.dag.preferred_parent));
|
2017-06-23 19:23:12 +00:00
|
|
|
LOG_INFO_(" -> ");
|
|
|
|
LOG_INFO_6ADDR(rpl_neighbor_get_ipaddr(nbr));
|
2017-06-23 07:21:31 +00:00
|
|
|
LOG_INFO_("\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
#ifdef RPL_CALLBACK_PARENT_SWITCH
|
2017-06-19 09:35:02 +00:00
|
|
|
RPL_CALLBACK_PARENT_SWITCH(curr_instance.dag.preferred_parent, nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
#endif /* RPL_CALLBACK_PARENT_SWITCH */
|
|
|
|
|
|
|
|
/* Always keep the preferred parent locked, so it remains in the
|
|
|
|
* neighbor table. */
|
2017-06-19 09:35:02 +00:00
|
|
|
nbr_table_unlock(rpl_neighbors, curr_instance.dag.preferred_parent);
|
|
|
|
nbr_table_lock(rpl_neighbors, nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/* Update DS6 default route. Use an infinite lifetime */
|
|
|
|
uip_ds6_defrt_rm(uip_ds6_defrt_lookup(
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_get_ipaddr(curr_instance.dag.preferred_parent)));
|
|
|
|
uip_ds6_defrt_add(rpl_neighbor_get_ipaddr(nbr), 0);
|
2017-06-10 12:35:39 +00:00
|
|
|
|
2017-06-19 09:35:02 +00:00
|
|
|
curr_instance.dag.preferred_parent = nbr;
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-19 09:35:02 +00:00
|
|
|
/* Remove DAG neighbors with a rank that is at least the same as minimum_rank. */
|
2017-06-10 12:35:39 +00:00
|
|
|
void
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_remove_all(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_nbr_t *nbr;
|
2017-06-10 12:35:39 +00:00
|
|
|
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_INFO("removing all neighbors\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
|
2017-06-19 09:35:02 +00:00
|
|
|
nbr = nbr_table_head(rpl_neighbors);
|
|
|
|
while(nbr != NULL) {
|
|
|
|
remove_neighbor(nbr);
|
|
|
|
nbr = nbr_table_next(rpl_neighbors, nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update needed immediately so as to ensure preferred_parent becomes NULL,
|
2017-06-19 09:35:02 +00:00
|
|
|
* and no longer points to a de-allocated neighbor. */
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_dag_update_state();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_nbr_t *
|
|
|
|
rpl_neighbor_get_from_ipaddr(uip_ipaddr_t *addr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
|
|
|
uip_ds6_nbr_t *ds6_nbr = uip_ds6_nbr_lookup(addr);
|
|
|
|
const uip_lladdr_t *lladdr = uip_ds6_nbr_get_ll(ds6_nbr);
|
2017-06-19 09:35:02 +00:00
|
|
|
return nbr_table_get_from_lladdr(rpl_neighbors, (linkaddr_t *)lladdr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-19 09:35:02 +00:00
|
|
|
static rpl_nbr_t *
|
2017-06-10 12:35:39 +00:00
|
|
|
best_parent(int fresh_only)
|
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_nbr_t *nbr;
|
|
|
|
rpl_nbr_t *best = NULL;
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
if(curr_instance.used == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for the best parent according to the OF */
|
2017-06-19 09:35:02 +00:00
|
|
|
for(nbr = nbr_table_head(rpl_neighbors); nbr != NULL; nbr = nbr_table_next(rpl_neighbors, nbr)) {
|
2017-06-10 12:35:39 +00:00
|
|
|
|
2017-06-19 09:35:02 +00:00
|
|
|
if(!acceptable_rank(nbr->rank) || !curr_instance.of->nbr_is_acceptable_parent(nbr)) {
|
|
|
|
/* Exclude neighbors with a rank that is not acceptable) */
|
2017-06-10 12:35:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-19 09:35:02 +00:00
|
|
|
if(fresh_only && !rpl_neighbor_is_fresh(nbr)) {
|
|
|
|
/* Filter out non-fresh nerighbors if fresh_only is set */
|
2017-06-10 12:35:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if UIP_ND6_SEND_NS
|
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
uip_ds6_nbr_t *ds6_nbr = rpl_get_ds6_nbr(nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
/* Exclude links to a neighbor that is not reachable at a NUD level */
|
2017-06-19 09:35:02 +00:00
|
|
|
if(ds6_nbr == NULL || ds6_nbr->state != NBR_REACHABLE) {
|
2017-06-10 12:35:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* UIP_ND6_SEND_NS */
|
|
|
|
|
|
|
|
/* Now we have an acceptable parent, check if it is the new best */
|
2017-06-19 09:35:02 +00:00
|
|
|
best = curr_instance.of->best_parent(best, nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_nbr_t *
|
|
|
|
rpl_neighbor_select_best(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_nbr_t *best;
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
if(rpl_dag_root_is_root()) {
|
|
|
|
return NULL; /* The root has no parent */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for best parent (regardless of freshness) */
|
|
|
|
best = best_parent(0);
|
|
|
|
|
|
|
|
#if RPL_WITH_PROBING
|
|
|
|
if(best != NULL) {
|
2017-06-19 09:35:02 +00:00
|
|
|
if(rpl_neighbor_is_fresh(best)) {
|
2017-06-29 07:25:21 +00:00
|
|
|
/* Return best if it is fresh */
|
2017-06-10 12:35:39 +00:00
|
|
|
return best;
|
|
|
|
} else {
|
2017-06-23 19:23:12 +00:00
|
|
|
rpl_nbr_t *best_fresh;
|
|
|
|
|
2017-06-27 09:06:38 +00:00
|
|
|
/* The best is not fresh. Probe it (unless there is already an urgent
|
|
|
|
probing target). We will be called back after the probing anyway. */
|
|
|
|
if(curr_instance.dag.urgent_probing_target == NULL) {
|
|
|
|
LOG_WARN("best parent is not fresh, schedule urgent probing to ");
|
|
|
|
LOG_WARN_6ADDR(rpl_neighbor_get_ipaddr(best));
|
|
|
|
LOG_WARN_("\n");
|
|
|
|
curr_instance.dag.urgent_probing_target = best;
|
|
|
|
rpl_schedule_probing();
|
|
|
|
}
|
2017-06-23 19:23:12 +00:00
|
|
|
|
2017-06-29 07:25:21 +00:00
|
|
|
/* The best is our preferred parent. It is not fresh but used to be,
|
|
|
|
else we would not have selected it in the first place. Stick to it
|
|
|
|
for a little while and rely on urgent probing to make a call. */
|
|
|
|
if(best == curr_instance.dag.preferred_parent) {
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2017-06-23 19:23:12 +00:00
|
|
|
/* Look for the best fresh parent. */
|
|
|
|
best_fresh = best_parent(1);
|
2017-06-10 12:35:39 +00:00
|
|
|
if(best_fresh == NULL) {
|
2017-06-23 19:23:12 +00:00
|
|
|
if(curr_instance.dag.preferred_parent == NULL) {
|
|
|
|
/* We will wait to find a fresh node before selecting our first parent */
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* We already have a parent, now stick to the best and count on
|
|
|
|
urgent probing to get a fresh parent soon */
|
|
|
|
return best;
|
|
|
|
}
|
2017-06-10 12:35:39 +00:00
|
|
|
} else {
|
|
|
|
/* Select best fresh */
|
|
|
|
return best_fresh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-29 07:25:21 +00:00
|
|
|
/* No acceptable parent */
|
2017-06-10 12:35:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else /* RPL_WITH_PROBING */
|
|
|
|
return best;
|
|
|
|
#endif /* RPL_WITH_PROBING */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_init(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
nbr_table_register(rpl_neighbors, (nbr_table_callback *)remove_neighbor);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/** @} */
|