aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/core/fib_entry.c
blob: b510b68b4911e9419550273fac0032c56ee99527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
 * Copyright (c) 2021-2023 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>

#include <hicn/hicn-light/config.h>
#include <hicn/core/fib_entry.h>
#include <hicn/core/strategy.h>

#ifdef WITH_MAPME
#include <hicn/core/ticks.h>
#endif /* WITH_MAPME */

#ifdef WITH_POLICY
#include <hicn/core/forwarder.h>
#include <hicn/policy.h>

#ifdef WITH_MAPME
#include <hicn/core/mapme.h>
#endif /* WITH_MAPME */

#endif /* WITH_POLICY */

#ifdef WITH_POLICY_STATS
#include <hicn/core/policy_stats.h>
#endif /* WITH_POLICY_STATS */

fib_entry_t *fib_entry_create(const hicn_prefix_t *prefix,
                              strategy_type_t strategy_type,
                              strategy_options_t *strategy_options,
                              const forwarder_t *forwarder) {
  assert(prefix);
  /*
   * For tests, we allow forwarder to be NULL, some
   * functions cannot be called but otherwise we need a main loop, etc.
   */
  // assert(forwarder);

  fib_entry_t *entry = malloc(sizeof(fib_entry_t));
  if (!entry) goto ERR_MALLOC;

  memset(entry, 0, sizeof(*entry));
  hicn_prefix_copy(&entry->prefix, prefix);
  entry->nexthops = NEXTHOPS_EMPTY;

  fib_entry_add_strategy_options(entry, STRATEGY_TYPE_BESTPATH, NULL);
  fib_entry_add_strategy_options(entry, STRATEGY_TYPE_REPLICATION, NULL);
  fib_entry_set_strategy(entry, strategy_type, strategy_options);

#ifdef WITH_MAPME
  entry->user_data = NULL;
  entry->user_data_release = NULL;
#endif /* WITH_MAPME */

  entry->forwarder = forwarder;

#ifdef WITH_POLICY
  entry->policy = POLICY_EMPTY;
#endif /* WITH_POLICY */

#ifdef WITH_POLICY_STATS
  entry->policy_stats = POLICY_STATS_EMPTY;
  entry->policy_counters = POLICY_COUNTERS_EMPTY;
#endif /* WITH_POLICY_STATS */

  return entry;

ERR_MALLOC:
  return NULL;
}

void fib_entry_free(fib_entry_t *entry) {
  assert(entry);
#ifdef WITH_MAPME
  if (entry->user_data) entry->user_data_release(&entry->user_data);
#endif /* WITH_MAPME */
  free(entry);
}

void fib_entry_add_strategy_options(fib_entry_t *entry,
                                    strategy_type_t strategy_type,
                                    strategy_options_t *strategy_options) {
  // for the moment only the best path and replication strategies support
  // strategy options return for the other strategyes
  if (strategy_type != STRATEGY_TYPE_BESTPATH &&
      strategy_type != STRATEGY_TYPE_REPLICATION)
    return;

  if (!strategy_options) {
    if (strategy_type == STRATEGY_TYPE_BESTPATH) {
      entry->strategy.options.bestpath.local_prefixes = NULL;
    } else {
      entry->strategy.options.replication.local_prefixes = NULL;
    }
    return;
  }

  local_prefixes_t *new_prefixes;
  local_prefixes_t *curr_prefixes;

  if (strategy_type == STRATEGY_TYPE_BESTPATH) {
    new_prefixes = strategy_options->bestpath.local_prefixes;
    curr_prefixes = entry->strategy.options.bestpath.local_prefixes;

    if (!curr_prefixes) {
      entry->strategy.options.bestpath.local_prefixes = create_local_prefixes();
      curr_prefixes = entry->strategy.options.bestpath.local_prefixes;
    }
  } else {
    new_prefixes = strategy_options->replication.local_prefixes;
    curr_prefixes = entry->strategy.options.replication.local_prefixes;

    if (!curr_prefixes) {
      entry->strategy.options.replication.local_prefixes =
          create_local_prefixes();
      curr_prefixes = entry->strategy.options.replication.local_prefixes;
    }
  }

  local_prefixes_add_prefixes(curr_prefixes, new_prefixes);
}

void fib_entry_set_strategy(fib_entry_t *entry, strategy_type_t strategy_type,
                            strategy_options_t *strategy_options) {
  // Default strategy if undefined
  if (!STRATEGY_TYPE_VALID(strategy_type)) strategy_type = STRATEGY_TYPE_RANDOM;

  if (entry->strategy.type == strategy_type) {  // startegy alredy set
    if (strategy_type != STRATEGY_TYPE_BESTPATH) {
      return;  // nothing to do
    } else {
      strategy_initialize(&entry->strategy, entry->forwarder);
      return;
    }
  }

  entry->strategy.type = strategy_type;
  if (strategy_options) entry->strategy.options = *strategy_options;

  strategy_initialize(&entry->strategy, entry->forwarder);
}

/*
 * Filters the set of nexthops passed as parameters (and not the one stored in
 * the FIB entry
 */
nexthops_t *fib_entry_filter_nexthops(fib_entry_t *entry, nexthops_t *nexthops,
                                      unsigned ingress_id, bool prefer_local) {
  assert(entry);
  assert(nexthops);

  nexthops_reset(nexthops);

  // DEBUG("[fib_entry_filter_nexthops] num=%d/%d ingress_id=%d",
  //         nexthops_get_curlen(nexthops), nexthops_get_len(nexthops),
  //         ingress_id);

  /* Filter out ingress, down & administrative down faces */
  const connection_table_t *table =
      forwarder_get_connection_table(entry->forwarder);
  connection_t *conn;
  uint_fast32_t flags;

  hicn_policy_t policy = fib_entry_get_policy(entry);

  nexthops_enumerate(nexthops, i, nexthop, {
    conn = connection_table_at(table, nexthop);
    nexthops_disable_if(nexthops, i, nexthop == ingress_id);
    nexthops_disable_if(nexthops, i,
                        (connection_get_admin_state(conn) == FACE_STATE_DOWN));
    nexthops_disable_if(nexthops, i,
                        (connection_get_state(conn) == FACE_STATE_DOWN));
  });

  // DEBUG("After pruning, num=%d/%d", nexthops_get_curlen(nexthops),
  // nexthops_get_len(nexthops));

  if (prefer_local) {
    /* Backup flags and cur_len*/
    flags = nexthops->flags;
    size_t cur_len = nexthops_get_curlen(nexthops);

    /* Filter local */
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i, (!connection_is_local(conn)));
    });

    /* Local faces have priority */
    if (nexthops_get_curlen(nexthops) > 0) return nexthops;

    nexthops->flags = flags;
    nexthops->cur_elts = cur_len;
  }

  /* Filter out local */
  nexthops_enumerate(nexthops, i, nexthop, {
    conn = connection_table_at(table, nexthop);
    nexthops_disable_if(nexthops, i, (connection_is_local(conn)));

#ifdef WITH_POLICY
    /* Policy filtering : next hops */
    nexthops_disable_if(
        nexthops, i,
        (policy.tags[POLICY_TAG_WIRED].state == POLICY_STATE_REQUIRE) &&
            (!connection_has_tag(conn, POLICY_TAG_WIRED)));
    nexthops_disable_if(
        nexthops, i,
        (policy.tags[POLICY_TAG_WIRED].state == POLICY_STATE_PROHIBIT) &&
            (connection_has_tag(conn, POLICY_TAG_WIRED)));
    nexthops_disable_if(
        nexthops, i,
        (policy.tags[POLICY_TAG_WIFI].state == POLICY_STATE_REQUIRE) &&
            (!connection_has_tag(conn, POLICY_TAG_WIFI)));
    nexthops_disable_if(
        nexthops, i,
        (policy.tags[POLICY_TAG_WIFI].state == POLICY_STATE_PROHIBIT) &&
            (connection_has_tag(conn, POLICY_TAG_WIFI)));
    nexthops_disable_if(
        nexthops, i,
        (policy.tags[POLICY_TAG_CELLULAR].state == POLICY_STATE_REQUIRE) &&
            (!connection_has_tag(conn, POLICY_TAG_CELLULAR)));
    nexthops_disable_if(
        nexthops, i,
        (policy.tags[POLICY_TAG_CELLULAR].state == POLICY_STATE_PROHIBIT) &&
            (connection_has_tag(conn, POLICY_TAG_CELLULAR)));
    nexthops_disable_if(
        nexthops, i,
        (policy.tags[POLICY_TAG_TRUSTED].state == POLICY_STATE_REQUIRE) &&
            (!connection_has_tag(conn, POLICY_TAG_TRUSTED)));
    nexthops_disable_if(
        nexthops, i,
        (policy.tags[POLICY_TAG_TRUSTED].state == POLICY_STATE_PROHIBIT) &&
            (connection_has_tag(conn, POLICY_TAG_TRUSTED)));
#endif /* WITH_POLICY */
  });

  if (nexthops_get_curlen(nexthops) == 0) {
    DEBUG("After REQUIRE/PROHIBIT pruning, num=%d/%d",
          nexthops_get_curlen(nexthops), nexthops_get_len(nexthops));
    return nexthops;
  }

  /* We have at least one matching next hop, implement heuristic */

#ifdef WITH_POLICY
  /*
   * As VPN connections might trigger duplicate uses of one interface, we start
   * by filtering out interfaces based on trust status.
   */
  flags = nexthops->flags;

  if ((policy.tags[POLICY_TAG_TRUSTED].state == POLICY_STATE_REQUIRE) ||
      (policy.tags[POLICY_TAG_TRUSTED].state == POLICY_STATE_PREFER)) {
    /* Try to filter out NON TRUSTED faces */
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i,
                          (!connection_has_tag(conn, POLICY_TAG_TRUSTED)));
    });

    if ((nexthops_get_curlen(nexthops) == 0) &&
        (policy.tags[POLICY_TAG_TRUSTED].state == POLICY_STATE_REQUIRE)) {
      return nexthops;
    }

  } else {
    /* Try to filter out TRUSTED faces */
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i,
                          (connection_has_tag(conn, POLICY_TAG_TRUSTED)));
    });
  }

  if (nexthops_get_curlen(nexthops) == 0) nexthops->flags = flags;

  /* Other preferences */
  if (policy.tags[POLICY_TAG_WIRED].state == POLICY_STATE_AVOID) {
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i,
                          connection_has_tag(conn, POLICY_TAG_WIRED));
    });
    if (nexthops_get_curlen(nexthops) == 0) nexthops->flags = flags;
  }
  if (policy.tags[POLICY_TAG_WIFI].state == POLICY_STATE_AVOID) {
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i,
                          connection_has_tag(conn, POLICY_TAG_WIFI));
    });
    if (nexthops_get_curlen(nexthops) == 0) nexthops->flags = flags;
  }
  if (policy.tags[POLICY_TAG_CELLULAR].state == POLICY_STATE_AVOID) {
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i,
                          connection_has_tag(conn, POLICY_TAG_CELLULAR));
    });
    if (nexthops_get_curlen(nexthops) == 0) nexthops->flags = flags;
  }

  if (policy.tags[POLICY_TAG_WIRED].state == POLICY_STATE_PREFER) {
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i,
                          !connection_has_tag(conn, POLICY_TAG_WIRED));
    });
    if (nexthops_get_curlen(nexthops) == 0) nexthops->flags = flags;
  }
  if (policy.tags[POLICY_TAG_WIFI].state == POLICY_STATE_PREFER) {
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i,
                          !connection_has_tag(conn, POLICY_TAG_WIFI));
    });
    if (nexthops_get_curlen(nexthops) == 0) nexthops->flags = flags;
  }
  if (policy.tags[POLICY_TAG_CELLULAR].state == POLICY_STATE_PREFER) {
    nexthops_enumerate(nexthops, i, nexthop, {
      conn = connection_table_at(table, nexthop);
      nexthops_disable_if(nexthops, i,
                          !connection_has_tag(conn, POLICY_TAG_CELLULAR));
    });
    if (nexthops_get_curlen(nexthops) == 0) nexthops->flags = flags;
  }
// XXX backup curlen ???
#endif /* WITH_POLICY */

  DEBUG("[fib_entry_filter_nexthops] before face priority num=%d/%d",
        nexthops_get_curlen(nexthops), nexthops_get_len(nexthops));

  /* Priority */
  uint32_t max_priority = 0;
  nexthops_foreach(nexthops, nexthop, {
    conn = connection_table_at(table, nexthop);
    uint32_t priority = connection_get_priority(conn);
    if (priority > max_priority) max_priority = priority;
  });
  nexthops_enumerate(nexthops, i, nexthop, {
    conn = connection_table_at(table, nexthop);
    nexthops_disable_if(nexthops, i,
                        connection_get_priority(conn) < max_priority);
  });

  DEBUG("[fib_entry_filter_nexthops] result num=%d/%d",
        nexthops_get_curlen(nexthops), nexthops_get_len(nexthops));

  /* Nexthop priority */

  /*
   * Filter out nexthops with lowest strategy priority.
   * Initializing at 0 allows to disable nexthops with a negative priority
   */
  max_priority = 0;
  nexthops_enumerate(nexthops, i, nexthop, {
    (void)nexthop;
    int priority = nexthops->state[i].priority;
    if (priority > max_priority) max_priority = priority;
  });
  nexthops_enumerate(nexthops, i, nexthop, {
    int priority = nexthops->state[i].priority;
    nexthops_disable_if(nexthops, i, (priority < max_priority));
  });

  /*
   * If multipath is disabled, we don't offer much choice to the forwarding
   * strategy, but still go through it for accounting purposes.
   */
  if ((policy.tags[POLICY_TAG_MULTIPATH].state == POLICY_STATE_PROHIBIT) ||
      (policy.tags[POLICY_TAG_MULTIPATH].state == POLICY_STATE_AVOID)) {
    DEBUG(
        "[fib_entry_get_nexthops_from_strategy] select single nexthops due to "
        "multipath policy");
    nexthops_select_first(nexthops);
  }

  return nexthops;
}

/*
 * Retrieve all candidate nexthops for sending mapme updates == all non local
 * connections. We don't apply the policy at this stage.
 */
nexthops_t *fib_entry_get_mapme_nexthops(fib_entry_t *entry,
                                         nexthops_t *new_nexthops) {
  assert(new_nexthops);

  const connection_table_t *table =
      forwarder_get_connection_table(entry->forwarder);

  /* We create a nexthop structure based on connections */
  // XXX This should be done close to where it is needed
  connection_t *connection;
  connection_table_foreach(table, connection, {
    if (connection_is_local(connection)) continue;
    new_nexthops->elts[nexthops_get_len(new_nexthops)] =
        connection_table_get_connection_id(table, connection);
    nexthops_inc(new_nexthops);
  });

  return new_nexthops;
}

/*
 * Update available next hops following policy update.
 *
 * The last nexthop parameter is only used if needed, otherwise the pointer to
 * fib entry is returned to avoid an useless copy
 */
nexthops_t *fib_entry_get_available_nexthops(fib_entry_t *entry,
                                             unsigned ingress_id,
                                             nexthops_t *new_nexthops) {
  // DEBUG("[fib_entry_get_available_nexthops]");

  connection_table_t *table = forwarder_get_connection_table(entry->forwarder);

  /*
   * Give absolute preference to local faces, with no policy, unless
   * ingress_id == ~0, which means we are searching faces on which to
   * advertise our prefix
   */
  if (ingress_id == ~0) {
    assert(new_nexthops);
    /* We create a nexthop structure based on connections */
    // XXX This should be done close to where it is needed
    connection_t *connection;
    connection_table_foreach(table, connection, {
      if (connection_is_local(connection)) continue;
      new_nexthops->elts[nexthops_get_len(new_nexthops)] =
          (unsigned int)connection_table_get_connection_id(table, connection);
      nexthops_inc(new_nexthops);
    });

#ifdef WITH_POLICY
    return fib_entry_filter_nexthops(entry, new_nexthops, ingress_id, false);
#else
    return new_nexthops;
#endif
  }

#ifdef WITH_POLICY
  return fib_entry_filter_nexthops(entry, fib_entry_get_nexthops(entry),
                                   ingress_id, true);
#else
  return fib_entry_get_nexthops(entry);
#endif
}

#ifdef WITH_POLICY

hicn_policy_t fib_entry_get_policy(const fib_entry_t *entry) {
  return entry->policy;
}

void fib_entry_set_policy(fib_entry_t *entry, hicn_policy_t policy) {
  INFO("fib_entry_set_policy");
  entry->policy = policy;

  forwarder_on_route_event(entry->forwarder, entry);

  // XXX generic mechanism to perform a mapme update
#if 0
#ifdef WITH_MAPME
  /*
   * Skip entries that do not correspond to a producer ( / have a locally
   * served prefix / have no local connection as next hop)
   */
  if (!fib_entry_has_local_nexthop(entry)) return;
  mapme_t *mapme = forwarder_get_mapme(entry->forwarder);
#endif /* WITH_MAPME */
#endif
}

#endif /* WITH_POLICY */

void fib_entry_nexthops_add(fib_entry_t *entry, unsigned nexthop) {
  /*
   * Nexthop is added to both:
   *  - fib_entry: it is added to the nexthops_t struct, in the elts array.
   *  - strategy: only used to eventually initialize internal state, might be
   *  empty like in the random strategy.
   */
  off_t id = nexthops_add(&entry->nexthops, nexthop);
  strategy_add_nexthop(&entry->strategy, &entry->nexthops, id);
}

void fib_entry_nexthops_remove(fib_entry_t *entry, unsigned nexthop) {
  off_t id = nexthops_remove(&entry->nexthops, nexthop);
  strategy_remove_nexthop(&entry->strategy, &entry->nexthops, id);
}

nexthops_t *fib_entry_get_nexthops_from_strategy(fib_entry_t *entry,
                                                 const msgbuf_t *msgbuf,
                                                 bool is_retransmission) {
  assert(entry);
  assert(msgbuf);

  // DEBUG("[fib_entry_get_nexthops_from_strategy]");

  const policy_stats_mgr_t *mgr =
      forwarder_get_policy_stats_mgr(entry->forwarder);
  assert(mgr);

  /* Filtering */
  nexthops_t *nexthops = fib_entry_get_available_nexthops(
      entry, msgbuf_get_connection_id(msgbuf), NULL);
  if (nexthops_get_curlen(nexthops) == 0) return nexthops;

#ifdef WITH_POLICY_STATS
  /*
   * Update statistics about loss rates. We only detect losses upon
   * retransmissions, and assume for the computation that the candidate set of
   * output faces is the same as previously (i.e. does not take into account
   * event such as face up/down, policy update, etc. Otherwise we would need to
   * know what was the previous choice !
   */
  if (is_retransmission)
    policy_stats_on_retransmission(mgr, &entry->policy_counters, nexthops);
#endif /* WITH_POLICY_STATS */

  /*
   * NOTE: We might want to call a forwarding strategy even with no nexthop to
   * take a fallback decision.
   */
  if (nexthops_get_curlen(nexthops) == 0) return nexthops;

#ifdef WITH_POLICY

  /*
   * Filter out nexthops with lowest strategy priority.
   * Initializing at 0 allows to disable nexthops with a negative priority
   */
  unsigned max_priority = 0;
  nexthops_enumerate(nexthops, i, nexthop, {
    int priority = nexthops->state[i].priority;
    if (priority > max_priority) max_priority = priority;
  });
  nexthops_enumerate(nexthops, i, nexthop, {
    int priority = nexthops->state[i].priority;
    nexthops_disable_if(nexthops, i, (priority < max_priority));
  });

  /*
   * If multipath is disabled, we don't offer much choice to the forwarding
   * strategy, but still go through it for accounting purposes.
   */
  hicn_policy_t policy = fib_entry_get_policy(entry);
  if ((policy.tags[POLICY_TAG_MULTIPATH].state == POLICY_STATE_PROHIBIT) ||
      (policy.tags[POLICY_TAG_MULTIPATH].state == POLICY_STATE_AVOID)) {
    DEBUG(
        "[fib_entry_get_nexthops_from_strategy] select single nexthops due to "
        "multipath policy");
    nexthops_select_first(nexthops);
  }

#endif /* WITH_POLICY */

  // DEBUG("[fib_entry_get_nexthops_from_strategy] calling lookup_nethops "
  // "on strategy (num=%d)", nexthops_get_len(nexthops));
  return strategy_lookup_nexthops(&entry->strategy, nexthops, msgbuf);
}

void fib_entry_on_data(fib_entry_t *entry, nexthops_t *data_nexthops,
                       const msgbuf_t *msgbuf, Ticks pitEntryCreation,
                       Ticks objReception) {
  assert(entry);
  assert(data_nexthops);
  assert(msgbuf);

#ifdef WITH_POLICY_STATS
  if (pitEntryCreation != 0) {  // if pitEntryCreation we no match in the pit
                                // was found
    const policy_stats_mgr_t *mgr =
        forwarder_get_policy_stats_mgr(entry->forwarder);
    Ticks rtt = objReception - pitEntryCreation;
    policy_stats_on_data(mgr, &entry->policy_stats, &entry->policy_counters,
                         data_nexthops, msgbuf, rtt);
  }
#endif /* WITH_POLICY_STATS */

  if (pitEntryCreation != 0 ||
      (fib_entry_strategy_type(entry) == STRATEGY_TYPE_BESTPATH)) {
    strategy_on_data(&entry->strategy, &entry->nexthops, data_nexthops, msgbuf,
                     pitEntryCreation, objReception);
  }
}

void fib_entry_on_timeout(fib_entry_t *entry,
                          const nexthops_t *timeout_nexthops) {
  assert(entry);
  assert(timeout_nexthops);

#ifdef WITH_POLICY_STATS
  const policy_stats_mgr_t *mgr =
      forwarder_get_policy_stats_mgr(entry->forwarder);
  policy_stats_on_timeout(mgr, &entry->policy_counters, timeout_nexthops);
#endif /* WITH_POLICY_STATS */

  strategy_on_timeout(&entry->strategy, &entry->nexthops, timeout_nexthops);
}

const hicn_prefix_t *fib_entry_get_prefix(const fib_entry_t *entry) {
  assert(entry);
  return &(entry->prefix);
}

/*
 * Return true if we have at least one local connection as next hop
 */
bool fib_entry_has_local_nexthop(const fib_entry_t *entry) {
  connection_table_t *table = forwarder_get_connection_table(entry->forwarder);

  nexthops_foreach(fib_entry_get_nexthops(entry), nexthop, {
    const connection_t *conn = connection_table_at(table, nexthop);
    /* Ignore non-local connections */
    if (!connection_is_local(conn)) continue;
    return true;
  });
  return false;
}

bool fib_entry_has_all_local_nexthops(const fib_entry_t *entry) {
  connection_table_t *table = forwarder_get_connection_table(entry->forwarder);

  int count = 0;
  nexthops_foreach(fib_entry_get_nexthops(entry), nexthop, {
    const connection_t *conn = connection_table_at(table, nexthop);
    /* Ignore non-local connections */
    if (!connection_is_local(conn)) return false;
    count++;
  });
  return (count > 0);
}

#ifdef WITH_MAPME

void *fib_entry_get_user_data(const fib_entry_t *entry) {
  assert(entry);

  return entry->user_data;
}

void fib_entry_set_user_data(fib_entry_t *entry, const void *user_data,
                             void (*user_data_release)(void **)) {
  assert(entry);
  assert(user_data);
  assert(user_data_release);

  entry->user_data = (void *)user_data;
  entry->user_data_release = user_data_release;
}

#endif /* WITH_MAPME */