Contiki-NG
Loading...
Searching...
No Matches
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 * \file
39 *
40 * Default RPL NBR policy
41 * decides when to add a new discovered node to the nbr table from RPL.
42 *
43 * \author Joakim Eriksson <joakime@sics.se>
44 * Contributors: Niclas Finne <nfi@sics.se>, Oriol PiƱol <oriol@yanzi.se>,
45 *
46 */
47
48#include "net/routing/rpl-classic/rpl-nbr-policy.h"
49#include "net/routing/rpl-classic/rpl-private.h"
50#include "net/nbr-table.h"
51#include "net/link-stats.h"
53#include "sys/log.h"
54
55#define LOG_MODULE "RPL-nbrpol"
56#define LOG_LEVEL LOG_LEVEL_NONE
57
58/*---------------------------------------------------------------------------*/
59static rpl_rank_t
60get_rank(const linkaddr_t *lladdr)
61{
62 rpl_parent_t *p = rpl_get_parent((uip_lladdr_t *)lladdr);
63 if(p == NULL) {
64 return RPL_INFINITE_RANK;
65 } else {
66 rpl_instance_t *instance = rpl_get_default_instance();
67 return instance != NULL ?
68 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
83 /* There's space left in the table or the worst entry has no rank: accept. */
84 if(candidate_for_removal == NULL
85 || (rank_candidate = get_rank(candidate_for_removal)) == RPL_INFINITE_RANK) {
86 return true;
87 } else {
88 rpl_instance_t *instance = rpl_get_default_instance();
89 rpl_rank_t new_path_rank;
90
91 if(instance == NULL || dio == NULL) {
92 return false;
93 }
94
95 new_path_rank = dio->rank + instance->min_hoprankinc;
96 return new_path_rank < rank_candidate - instance->min_hoprankinc / 2;
97 }
98}
99/*---------------------------------------------------------------------------*/
100bool
101rpl_nbr_can_accept_new(const linkaddr_t *new,
102 const linkaddr_t *candidate_for_removal,
103 nbr_table_reason_t reason, const void *data)
104{
105 bool accept;
106 switch(reason) {
107 case NBR_TABLE_REASON_RPL_DIO:
108 accept = can_accept_new_parent(candidate_for_removal, (rpl_dio_t *)data);
109 break;
110 case NBR_TABLE_REASON_ROUTE:
111 case NBR_TABLE_REASON_RPL_DAO:
112 /* Stop adding children if there is no space for nexthop
113 neighbors, regardless of whether the table if full or not. */
114 accept = rpl_nbr_policy_get_free_nexthop_neighbors() > 0;
115 break;
116 case NBR_TABLE_REASON_RPL_DIS:
117 case NBR_TABLE_REASON_UNDEFINED:
118 case NBR_TABLE_REASON_IPV6_ND:
119 case NBR_TABLE_REASON_MAC:
120 case NBR_TABLE_REASON_LLSEC:
121 case NBR_TABLE_REASON_LINK_STATS:
122 case NBR_TABLE_REASON_IPV6_ND_AUTOFILL:
123 default:
124 /* Behavior for all but new RPL parents/children: accept anything
125 until table is full. */
126 accept = (candidate_for_removal == NULL);
127 break;
128 }
129 LOG_DBG("%s new neighbor ", accept ? "accept" : "reject");
130 LOG_DBG_LLADDR(new);
131 LOG_DBG_(", reason %u, worst is ", reason);
132 LOG_DBG_LLADDR(candidate_for_removal);
133 LOG_DBG_(" (total free %u, free nexthop neighbors %u)\n",
134 NBR_TABLE_MAX_NEIGHBORS - nbr_table_count_entries(),
135 rpl_nbr_policy_get_free_nexthop_neighbors());
136 return accept;
137}
138/*---------------------------------------------------------------------------*/
139int
140rpl_nbr_policy_get_free_nexthop_neighbors(void)
141{
142 return RPL_NBR_POLICY_MAX_NEXTHOP_NEIGHBORS - uip_ds6_route_count_nexthop_neighbors();
143}
144/*---------------------------------------------------------------------------*/
145/** @}*/
static uint8_t accept(uint8_t in)
Definition mpl.c:1390
Header file for the logging system.
RPL instance structure.
Definition rpl.h:222
Header file for routing table manipulation.