Contiki-NG
rpl-nbr-policy.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2020, Yanzi Networks AB.
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 copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 /**
33 * \addtogroup uip
34 * @{
35 */
36
37
38 /**
39 * \file
40 *
41 * Default RPL NBR policy
42 * decides when to add a new discovered node to the nbr table from RPL.
43 *
44 * \author Joakim Eriksson <joakime@sics.se>
45 * Contributors: Niclas Finne <nfi@sics.se>, Oriol PiƱol <oriol@yanzi.se>,
46 *
47 */
48
49#include "net/routing/rpl-classic/rpl-nbr-policy.h"
50#include "net/routing/rpl-classic/rpl-private.h"
51#include "net/nbr-table.h"
52#include "net/link-stats.h"
54#include "sys/log.h"
55
56#define LOG_MODULE "RPL-nbrpol"
57#define LOG_LEVEL LOG_LEVEL_NONE
58
59/*---------------------------------------------------------------------------*/
60static rpl_rank_t
61get_rank(const linkaddr_t *lladdr)
62{
63 rpl_parent_t *p = rpl_get_parent((uip_lladdr_t *)lladdr);
64 if(p == NULL) {
65 return RPL_INFINITE_RANK;
66 } else {
68 return instance != NULL ? instance->of->rank_via_parent(p) : RPL_INFINITE_RANK;
69 }
70}
71/*---------------------------------------------------------------------------*/
72const linkaddr_t *
73rpl_nbr_gc_get_worst(const linkaddr_t *lladdr1, const linkaddr_t *lladdr2)
74{
75 return get_rank(lladdr2) > get_rank(lladdr1) ? lladdr2 : lladdr1;
76}
77/*---------------------------------------------------------------------------*/
78static bool
79can_accept_new_parent(const linkaddr_t *candidate_for_removal, rpl_dio_t *dio)
80{
81 rpl_rank_t rank_candidate;
82 /* There's space left in the table or the worst entry has no rank: accept */
83 if(candidate_for_removal == NULL
84 || (rank_candidate = get_rank(candidate_for_removal)) == RPL_INFINITE_RANK) {
85 return true;
86 } else {
88 rpl_rank_t new_path_rank;
89
90 if(instance == NULL || dio == NULL) {
91 return false;
92 }
93
94 new_path_rank = dio->rank + instance->min_hoprankinc;
95 return new_path_rank < rank_candidate - instance->min_hoprankinc / 2;
96 }
97}
98/*---------------------------------------------------------------------------*/
99bool
100rpl_nbr_can_accept_new(const linkaddr_t *new, const linkaddr_t *candidate_for_removal,
101 nbr_table_reason_t reason, const void *data)
102{
103 bool accept;
104 switch(reason) {
105 case NBR_TABLE_REASON_RPL_DIO:
106 accept = can_accept_new_parent(candidate_for_removal, (rpl_dio_t *)data);
107 break;
108 case NBR_TABLE_REASON_ROUTE:
109 case NBR_TABLE_REASON_RPL_DAO:
110 /* Stop adding children if there is no space for nexthop neighbors,
111 * regardless of whether the table if full or not */
112 accept = rpl_nbr_policy_get_free_nexthop_neighbors() > 0;
113 break;
114 case NBR_TABLE_REASON_RPL_DIS:
115 case NBR_TABLE_REASON_UNDEFINED:
116 case NBR_TABLE_REASON_IPV6_ND:
117 case NBR_TABLE_REASON_MAC:
118 case NBR_TABLE_REASON_LLSEC:
119 case NBR_TABLE_REASON_LINK_STATS:
120 case NBR_TABLE_REASON_IPV6_ND_AUTOFILL:
121 default:
122 /* Behavior for all but new RPL parents/children: accept anything until table is full. */
123 accept = (candidate_for_removal == NULL);
124 break;
125 }
126 LOG_DBG("%s new neighbor ", accept ? "accept" : "reject");
127 LOG_DBG_LLADDR(new);
128 LOG_DBG_(", reason %u, worst is ", reason);
129 LOG_DBG_LLADDR(candidate_for_removal);
130 LOG_DBG_(" (total free %u, free nexthop neighbors %u)\n",
131 NBR_TABLE_MAX_NEIGHBORS - nbr_table_count_entries(),
132 rpl_nbr_policy_get_free_nexthop_neighbors());
133 return accept;
134}
135/*---------------------------------------------------------------------------*/
136int
137rpl_nbr_policy_get_free_nexthop_neighbors(void)
138{
139 return RPL_NBR_POLICY_MAX_NEXTHOP_NEIGHBORS - uip_ds6_route_count_nexthop_neighbors();
140}
141/*---------------------------------------------------------------------------*/
142/** @}*/
static uint8_t accept(uint8_t in)
Definition: mpl.c:1391
rpl_instance_t * rpl_get_default_instance(void)
Returns pointer to the default instance (for compatibility with legagy RPL code)
Definition: rpl-dag.c:627
Header file for the logging system.
RPL instance structure.
Definition: rpl.h:219
Header file for routing table manipulation.