Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rpl_mrhof.c Source File

rpl_mrhof.c

00001 /*
00002  * Copyright (c) 2015-2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "nsconfig.h"
00018 
00019 #ifdef HAVE_RPL
00020 
00021 #include "ns_types.h"
00022 #include "ns_trace.h"
00023 #include "common_functions.h"
00024 
00025 #include "net_rpl.h"
00026 
00027 #include "NWK_INTERFACE/Include/protocol_abstract.h"
00028 #include "Common_Protocols/ipv6_resolution.h"
00029 #include "Service_Libs/etx/etx.h"
00030 
00031 #include "RPL/rpl_protocol.h"
00032 #include "RPL/rpl_objective.h"
00033 #include "RPL/rpl_upward.h"
00034 #include "RPL/rpl_downward.h"
00035 #include "RPL/rpl_policy.h"
00036 #include "RPL/rpl_structures.h"
00037 #include "RPL/rpl_mrhof.h"
00038 
00039 #define TRACE_GROUP "rplm"
00040 
00041 /* Current implementation assumes ETX (and no metric container) - need to
00042  * ensure that we behave appropriately if a metric container is present.
00043  */
00044 
00045 static void rpl_mrhof_parent_selection(rpl_instance_t *instance);
00046 static uint16_t rpl_mrhof_path_cost_through_neighbour(const rpl_neighbour_t *neighbour);
00047 static bool rpl_mrhof_neighbour_acceptable(const rpl_instance_t *instance, const rpl_neighbour_t *neighbour);
00048 
00049 static rpl_objective_t rpl_mrhof = {
00050     .ocp = RPL_OCP_MRHOF,
00051     .parent_selection = rpl_mrhof_parent_selection,
00052     .path_cost = rpl_mrhof_path_cost_through_neighbour,
00053     .neighbour_acceptable = rpl_mrhof_neighbour_acceptable,
00054 };
00055 
00056 typedef struct rpl_of0_params {
00057     uint8_t stretch_of_rank;
00058     uint8_t rank_factor;
00059 } rpl_of0_params;
00060 
00061 void rpl_mrhof_init(void)
00062 {
00063     rpl_objective_register(&rpl_mrhof);
00064 }
00065 
00066 /* Return ETX in RFC 6551 metric form (*128), 0xFFFF = infinite */
00067 static uint16_t rpl_mrhof_etx(const rpl_neighbour_t *neighbour)
00068 {
00069     uint16_t etx88 = ipv6_map_ip_to_ll_and_call_ll_addr_handler(NULL, neighbour->interface_id, NULL, neighbour->ll_address, etx_read);
00070     switch (etx88) {
00071         case 0x0000: /* Unknown - assume poor */
00072             return 2 * 128;
00073         case 0x0001: /* Interface has no ETX - assume good */
00074             return 1 * 128;
00075         case 0xFFFF: /* Not associated */
00076             return RPL_RANK_INFINITE;
00077         default:
00078             return etx88 >> 1;
00079     }
00080 }
00081 
00082 static uint16_t rpl_mrhof_link_metric_to_neighbour(const rpl_neighbour_t *neighbour)
00083 {
00084     return rpl_mrhof_etx(neighbour);
00085 }
00086 
00087 static uint16_t rpl_mrhof_path_cost_through_neighbour(const rpl_neighbour_t *neighbour)
00088 {
00089     return rpl_rank_add(neighbour->rank, rpl_mrhof_link_metric_to_neighbour(neighbour));
00090 }
00091 
00092 static bool rpl_mrhof_neighbour_acceptable(const rpl_instance_t *instance, const rpl_neighbour_t *neighbour)
00093 {
00094     return rpl_mrhof_link_metric_to_neighbour(neighbour) <= rpl_policy_mrhof_max_link_metric(instance->domain);
00095 }
00096 
00097 
00098 /* Given a preferred parent, we are only permitted to stretch our above the
00099  * path cost through that parent by a certain (policy) amount to accommodate a
00100  * bigger parent set.
00101  */
00102 static uint16_t rpl_mrhof_max_stretched_rank(const rpl_dodag_version_t *version, uint16_t base_rank)
00103 {
00104     uint16_t max_stretch = rpl_policy_mrhof_max_rank_stretch_for_extra_parents(version->dodag->instance->domain);
00105     uint16_t max_stretched = rpl_rank_add(base_rank, max_stretch);
00106     if (max_stretched > version->greediness_rank_limit) {
00107         max_stretched = version->greediness_rank_limit;
00108     }
00109     return max_stretched;
00110 }
00111 
00112 
00113 
00114 /* RFC 6552 4.2.2. Selection of the Backup Feasible Successor
00115  * This implementation can be called multiple times to select multiple successors, and can be used
00116  * to check the backup for a potential parent.
00117  */
00118 static rpl_neighbour_t *rpl_mrhof_select_backup_parent(rpl_instance_t *instance, rpl_neighbour_t *pref, uint16_t *rank, uint16_t max_rank)
00119 {
00120     rpl_neighbour_t *best = NULL;
00121     uint16_t best_rank = RPL_RANK_INFINITE;
00122     uint16_t best_path_cost = RPL_RANK_INFINITE;
00123 
00124     ns_list_foreach(rpl_neighbour_t, c, &instance->candidate_neighbours) {
00125         /* New backup must not be (potential) preferred parent or already be a parent */
00126         if (c == pref || c->dodag_parent) {
00127             continue;
00128         }
00129 
00130         /* Backup must be in the same DODAG version (keep it simple) */
00131         if (c->dodag_version != pref->dodag_version) {
00132             continue;
00133         }
00134 
00135         rpl_dodag_t *dodag = c->dodag_version->dodag;
00136         if (dodag != pref->dodag_version->dodag) {
00137             continue;
00138         }
00139 
00140         /* Ignore high-link-metric neighbours */
00141         if (rpl_mrhof_link_metric_to_neighbour(c) > rpl_policy_mrhof_max_link_metric(instance->domain)) {
00142             continue;
00143         }
00144 
00145         /* It must not cause our rank to go up too much (rank affected by rules
00146          * 2 and 3 of RFC 6719 3.3.)
00147          */
00148         uint16_t path_cost = rpl_mrhof_path_cost_through_neighbour(c);
00149         uint16_t next_rank_rule2 = rpl_rank_next_level(dodag, c->rank);
00150         uint16_t path_cost_rule3 = rpl_rank_sub(path_cost, dodag->config.dag_max_rank_increase);
00151         uint16_t new_rank = next_rank_rule2 > path_cost_rule3 ? next_rank_rule2 : path_cost_rule3;
00152         if (new_rank > max_rank) {
00153             continue;
00154         }
00155 
00156         if (!best) {
00157             goto new_best;
00158         }
00159 
00160         /* Prefer lesser path cost */
00161         if (path_cost < best_path_cost) {
00162             goto new_best;
00163         } else if (path_cost > best_path_cost) {
00164             continue;
00165         }
00166 
00167         /* If it's a tie at this point, prefer the first in the list, which will
00168          * retain any previous parent ordering.
00169          */
00170         continue;
00171 
00172 new_best:
00173         best = c;
00174         best_path_cost = path_cost;
00175         best_rank = new_rank;
00176     }
00177 
00178     if (best) {
00179         if (best_rank > *rank) {
00180             *rank = best_rank;
00181         }
00182     }
00183 
00184     return best;
00185 }
00186 
00187 /* RFC 6719 3.2.2. Selection of the parent with lowest path cost */
00188 static rpl_neighbour_t *rpl_mrhof_select_best_parent(rpl_instance_t *instance, const rpl_neighbour_t *prev_preferred, uint16_t *rank_out)
00189 {
00190     rpl_neighbour_t *best = NULL;
00191     uint16_t best_rank = RPL_RANK_INFINITE;
00192     uint16_t best_path_cost = RPL_RANK_INFINITE;
00193     uint16_t best_link_metric = RPL_RANK_INFINITE;
00194     uint16_t prev_preferred_path_cost = RPL_RANK_INFINITE;
00195     const uint16_t metric_threshold = rpl_policy_mrhof_max_link_metric(instance->domain);
00196 
00197     /* Previous preferred parent, if any, will be at the start of the list */
00198     /* We can use this to simplify some logic */
00199     if (prev_preferred) {
00200         prev_preferred_path_cost = rpl_mrhof_path_cost_through_neighbour(prev_preferred);
00201     }
00202 
00203     ns_list_foreach(rpl_neighbour_t, c, &instance->candidate_neighbours) {
00204         rpl_dodag_t *dodag = c->dodag_version->dodag;
00205 
00206         /* Check link, and ignore totally unreachable neighbours */
00207         uint16_t link_metric = rpl_mrhof_link_metric_to_neighbour(c);
00208         if (link_metric == RPL_RANK_INFINITE) {
00209             continue;
00210         }
00211 
00212         /* For ETX, rank is the path cost, then make sure we increase by MinHopRankIncrease */
00213         /* (Normally, MinHopRankIncrease will be 0x80, so this would be superfluous) */
00214         uint16_t path_cost = rpl_mrhof_path_cost_through_neighbour(c);
00215         uint16_t new_rank = path_cost;
00216         uint16_t min_rank = rpl_rank_add(c->rank, dodag->config.min_hop_rank_increase);
00217         if (new_rank < min_rank) {
00218             new_rank = min_rank;
00219         }
00220 
00221         if (new_rank > c->dodag_version->hard_rank_limit) {
00222             new_rank = RPL_RANK_INFINITE;
00223         }
00224 
00225         if (!best) {
00226             goto new_best;
00227         }
00228 
00229         /* Avoid using high-link-metric neighbours (but allow if we have no alternative) */
00230         if (link_metric <= metric_threshold && best_link_metric > metric_threshold) {
00231             goto new_best;
00232         } else if (link_metric > metric_threshold && best_link_metric <= metric_threshold) {
00233             continue;
00234         }
00235 
00236         /* MRHOF RFC is fuzzy about versions and DODAG selection; it seems
00237          * in many ways to assume we're choosing parents only within a
00238          * DODAG Version, whereas we also need to choose the DODAG and Version.
00239          *
00240          * Therefore we retain some of the logic for selecting DODAG from OF0.
00241          */
00242 
00243         /* Prefer connection to a grounded DODAG */
00244         if ((dodag->g_mop_prf & RPL_GROUNDED) != (best->dodag_version->dodag->g_mop_prf & RPL_GROUNDED)) {
00245             if (dodag->g_mop_prf & RPL_GROUNDED) {
00246                 goto new_best;
00247             } else {
00248                 continue;
00249             }
00250         }
00251 
00252         /* Go by DODAG preference */
00253         rpl_cmp_t cmp = rpl_dodag_pref_compare(dodag, best->dodag_version->dodag);
00254         if (cmp == RPL_CMP_GREATER) {
00255             goto new_best;
00256         } else if (cmp == RPL_CMP_LESS) {
00257             continue;
00258         }
00259 
00260         /* No explicit version preference - versions should flow naturally
00261          * from the root without one?
00262          */
00263 
00264         /* Hysteresis path cost test, if we are thinking of switching parents */
00265         if (best == prev_preferred) {
00266             /* Do not switch away from current parent, until threshold met */
00267             /* Note that any current parent will be first in the list - we
00268              * don't need to worry about hitting it and giving it preference as
00269              * a candidate later.
00270              */
00271             if (rpl_rank_add(path_cost, rpl_policy_mrhof_parent_switch_threshold(instance->domain)) <= prev_preferred_path_cost) {
00272                 goto new_best;
00273             } else {
00274                 continue;
00275             }
00276         }
00277 
00278         /* Prefer lesser resulting path cost */
00279         if (path_cost < best_path_cost) {
00280             goto new_best;
00281         } else if (path_cost > best_path_cost) {
00282             continue;
00283         }
00284 
00285         /* Prefer parent that most recently sent a DIO */
00286         if (c->dio_timestamp != best->dio_timestamp) {
00287             if (common_serial_number_greater_32(c->dio_timestamp, best->dio_timestamp)) {
00288                 goto new_best;
00289             } else {
00290                 continue;
00291             }
00292         }
00293 
00294 
00295     new_best:
00296         best_rank = new_rank;
00297         best_path_cost = path_cost;
00298         best_link_metric = link_metric;
00299         best = c;
00300     }
00301 
00302     if (rank_out) {
00303         *rank_out = best_rank;
00304     }
00305 
00306     return best;
00307 }
00308 
00309 
00310 
00311 /*
00312  * Parent selection is a serious business. This runs periodically - it need not
00313  * be fast.
00314  *
00315  * Its job is to reorder the instance's candidate neighbour list, placing
00316  * members of the parent set at the front, in preference order. Those neighbours
00317  * must have dodag_parent set, and dodag_pref filled in.
00318  *
00319  * Before entry, "was_dodag_parent" is set to "dodag_parent", and "dodag_parent"
00320  * is cleared. "was_dodag_parent" must not be modified.
00321  *
00322  * Parent selection must not delete candidate neighbours.
00323  *
00324  * For each DODAGVersion, target_max_rank is the RPL core's desired maximum
00325  * This will be >= the current rank. This allows distinction between different
00326  * states in a DODAG:
00327  *
00328  * 1) Wanting to hold rank
00329  * 2) Wanting to hold DAGRank
00330  * 3) Willing to increase DAGRank, but not exceeding MaxDagRankIncrease
00331  * 4) Willing to take any rank (new version)
00332  *
00333  * instance->current_dodag_version and instance->current_rank should be updated
00334  * on exit.
00335  *
00336  * No other instance data structures should be modified - core will re-evaluate
00337  * by examining the reordered candidate list.
00338  */
00339 static void rpl_mrhof_parent_selection(rpl_instance_t *instance)
00340 {
00341     rpl_neighbour_t *old_pref_parent = rpl_instance_preferred_parent(instance);
00342     uint16_t rank;
00343     rpl_neighbour_t *pref_parent = rpl_mrhof_select_best_parent(instance, old_pref_parent, &rank);
00344     uint8_t last_pref;
00345     rpl_neighbour_list_t NS_LIST_NAME_INIT(parent_list);
00346     rpl_dodag_version_t *version;
00347     rpl_dodag_t *dodag;
00348 
00349     if (!pref_parent) {
00350         tr_debug("No pref_parent (mrhof) -> set RPL_RANK_INFINITE");
00351         rank = RPL_RANK_INFINITE;
00352         version = NULL;
00353         dodag = NULL;
00354         goto finish_up;
00355     }
00356 
00357     version = pref_parent->dodag_version;
00358     dodag = version->dodag;
00359 
00360     rpl_dodag_version_raise_greediness(version, rank);
00361 
00362     pref_parent->dodag_parent = true;
00363     pref_parent->dodag_pref = last_pref = 0;
00364 
00365     /* Move the preferred parent onto head of our empty parent list */
00366     ns_list_remove(&instance->candidate_neighbours, pref_parent);
00367     ns_list_add_to_end(&parent_list, pref_parent);
00368 
00369     uint_fast8_t more_successors = rpl_policy_mrhof_parent_set_size(instance->domain) - 1;
00370     uint16_t last_cost = rpl_mrhof_path_cost_through_neighbour(pref_parent);
00371     uint16_t max_rank = rpl_mrhof_max_stretched_rank(pref_parent->dodag_version, rank);
00372     while (more_successors--) {
00373         rpl_neighbour_t *backup = rpl_mrhof_select_backup_parent(instance, pref_parent, &rank, max_rank);
00374         if (!backup) {
00375             break;
00376         }
00377 
00378         if (rank != RPL_RANK_INFINITE && rank > version->greediness_rank_limit) {
00379             tr_error("Rank excess during stretch %"PRIu16" > %"PRIu16, rank, version->greediness_rank_limit);
00380             rank = version->greediness_rank_limit;
00381             break;
00382         }
00383 
00384         /* Indicate preference levels, comparing path cost. If the preferred
00385          * parent has a higher cost than the backups, due to hysteresis, all
00386          * the others will end up with the same "preference" level.
00387          */
00388         uint16_t backup_cost = rpl_mrhof_path_cost_through_neighbour(backup);
00389         if (backup_cost > last_cost && last_pref < 15) {
00390             last_pref++;
00391             last_cost = backup_cost;
00392         }
00393         backup->dodag_parent = true;
00394         backup->dodag_pref = last_pref;
00395         ns_list_remove(&instance->candidate_neighbours, backup);
00396         ns_list_add_to_end(&parent_list, backup);
00397     }
00398 
00399     /* Two-step shuffle: move remaining unselected neighbours onto end of our list */
00400     ns_list_concatenate(&parent_list, &instance->candidate_neighbours);
00401 
00402     /* Then transfer whole list back to instance */
00403     ns_list_concatenate(&instance->candidate_neighbours, &parent_list);
00404 
00405 finish_up:
00406     rpl_instance_set_dodag_version(instance, version, rank);
00407 
00408     /* Use default DAO path control */
00409     rpl_downward_convert_dodag_preferences_to_dao_path_control(dodag);
00410 }
00411 
00412 #endif /* HAVE_RPL */