blob: 0fbed1c880ed449c52bb054a3348a76fd61cbdfd [file] [log] [blame]
Alexander Afanasyeve6852122013-01-21 11:54:47 -08001.. _forwarding strategies:
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -08002
3Forwarding Strategies
4=====================
5
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -08006ndnSIM through its integration with NFD provides simple ways to experiment with the custom
7Interest/Data forwarding strategies of NFD.
8
9A new forwarding strategy can be implement completely different processing or override just
10specific actions/events of the :nfd:`forwarding strategy interface <nfd::fw::Strategy>`. NFD
11offers the maximum flexibility by allowing per-namespace selection of each specific forwarding
12strategy.
13
14Please refer to :nfd:`API documentation <nfd::fw::Strategy>` of the forwarding strategy
15interface, which lists all default actions/events. For a more detailed specification, you can
Xinyu Mafc8956c2019-04-08 23:17:52 -070016read the corresponding section of `"NFD Developer's Guide" <https://named-data.net/publications/techreports/>`__.
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080017
Alexander Afanasyeve6852122013-01-21 11:54:47 -080018Available forwarding strategies
19+++++++++++++++++++++++++++++++
20
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080021+--------------------------------------------+----------------------------------------------------------------------------------------------+
22| Strategy Name | Description |
23+============================================+==============================================================================================+
Alexander Afanasyev90d50392022-05-05 13:07:24 -040024| ``/localhost/nfd/strategy/best-route`` | :nfd:`Best Route Strategy <nfd::fw::BestRouteStrategy>` **(default)** |
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080025| | |
26| | The best route strategy forwards an Interest to the |
27| | upstream with lowest routing cost. |
28+--------------------------------------------+----------------------------------------------------------------------------------------------+
29+--------------------------------------------+----------------------------------------------------------------------------------------------+
30| ``/localhost/nfd/strategy/ncc`` | :nfd:`NCC Strategy <nfd::fw::NccStrategy>` |
31| | |
32| | The NCC strategy is an reimplementation of CCNx 0.7.2 |
33| | default strategy. It has similar algorithm but is not |
34| | guaranteed to be equivalent. |
35+--------------------------------------------+----------------------------------------------------------------------------------------------+
36+--------------------------------------------+----------------------------------------------------------------------------------------------+
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -070037| ``/localhost/nfd/strategy/multicast`` | :nfd:`Multicast Strategy <nfd::fw::MulticastStrategy>` |
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080038| | |
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -070039| | The multicast strategy forwards every Interest to all |
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080040| | upstreams, indicated by the supplied FIB entry. |
41+--------------------------------------------+----------------------------------------------------------------------------------------------+
42+--------------------------------------------+----------------------------------------------------------------------------------------------+
43| ``/localhost/nfd/strategy/client-control`` | :nfd:`Client Control Strategy <nfd::fw::ClientControlStrategy>` |
44| | |
45| | The client control strategy allows a local consumer |
46| | application to choose the outgoing face of each Interest. |
47+--------------------------------------------+----------------------------------------------------------------------------------------------+
Alexander Afanasyeve6852122013-01-21 11:54:47 -080048
Alexander Afanasyeve6852122013-01-21 11:54:47 -080049
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080050.. note::
Alexander Afanasyeve6852122013-01-21 11:54:47 -080051
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080052 ndnSIM 2.0 exactly like NFD allows different namespaces to be associated with different
53 forwarding strategies. By default, the following forwarding strategy configuration is
54 defined:
Alexander Afanasyeve6852122013-01-21 11:54:47 -080055
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080056 +--------------------+-----------------------------------------------+
57 | Namespace | Strategy Name |
58 +====================+===============================================+
59 | ``/`` | ``/localhost/nfd/strategy/best-route`` |
60 +--------------------+-----------------------------------------------+
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -070061 | ``/localhost`` | ``/localhost/nfd/strategy/multicast`` |
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080062 +--------------------+-----------------------------------------------+
63 | ``/localhost/nfd`` | ``/localhost/nfd/strategy/best-route`` |
64 +--------------------+-----------------------------------------------+
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -070065 | ``/ndn/multicast`` | ``/localhost/nfd/strategy/multicast`` |
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080066 +--------------------+-----------------------------------------------+
67
68
69
70Examples:
Alexander Afanasyeve6852122013-01-21 11:54:47 -080071
72 .. code-block:: c++
73
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080074 StrategyChoiceHelper::Install(nodes, prefix,
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -070075 "/localhost/nfd/strategy/multicast");
Alexander Afanasyeve6852122013-01-21 11:54:47 -080076
Alexander Afanasyeve6852122013-01-21 11:54:47 -080077
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080078.. _Writing your own custom strategy:
79
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080080Writing your own custom strategy
81++++++++++++++++++++++++++++++++
82
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080083One of the objectives and features of ndnSIM 2.0 is that it uses NFD codebase for packet
84forwarding. Therefore, writing strategy in ndnSIM is almost exactly the same process as
Xinyu Mafc8956c2019-04-08 23:17:52 -070085outlined in the `"NFD Developer's Guide" <https://named-data.net/publications/techreports/>`__.
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080086
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080087The purpose of the strategy is to decides how to forward Interests and it is not intended to
88override any processing steps in the forwarding pipelines of NFD. If it is desired to support
89a new packet type (other than Interest and Data), a new field in Interest or Data packets, or
90override some actions in the pipelines (e.g., disable ContentStore lookup), it can be
91accomplished by modification of the forwarding pipelines in :nfd:`nfd::Forwarder` class.
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080092
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080093The initial step in creating a new strategy is to create a class, say ``MyStrategy`` that is
94derived from :nfd:`nfd::fw::Strategy`. This subclass must at least override the triggers that are
95marked pure virtual and may override other available triggers that are marked just virtual.
96
97If the strategy needs to store information, it is needed to decide whether the information
98is related to a namespace or an Interest. Information related to a namespace but not
99specific to an Interest should be stored in Measurements entries; information related to an
100Interest should be stored in PIT entries, PIT downstream records, or PIT upstream records.
101After this decision is made, a data structure derived from StrategyInfo class needs to be
102declared. In the existing implementation, such data structures are declared as nested
103classes as it provides natural grouping and scope protection of the strategy-specific
104entity, but it is not required to follow the same model. If timers are needed, EventId
105fields needs to be added to such data structure(s).
106
107.. image:: _static/nfd-forwarding-overview.png
108 :alt: Packet processing in NFD/ndnSIM is broken into a number of small pipelines and
109 strategy callbacks
110
111The final step is to implement one or more of the triggers with the desired strategy logic.
112These triggers are listed below:
113
114- :nfd:`After Receive Interest Trigger <nfd::fw::Strategy::afterReceiveInterest()>`
115
116 When an Interest is received, passes necessary checks, and needs to be forwarded, Incoming
117 Interest pipeline (Section 4.2.1) invokes this trigger with the PIT entry, incoming Interest
118 packet, and FIB entry. At that time, the following conditions hold for the Interest:
119
120 - The Interest does not violate ``/localhost`` scope.
121 - The Interest is not looped.
122 - The Interest cannot be satisfied by ContentStore.
123 - The Interest is under a namespace managed by this strategy.
124
125 After being triggered, the strategy should decide whether and where to forward this
126 Interest. If the strategy decides to forward this Interest, it should invoke send Interest
127 action at least once. If the strategy concludes that this Interest cannot be forwarded, it
128 should invoke reject pending Interest action, so that the PIT entry will be deleted
129 shortly.
130
131- :nfd:`Before Satisfy Interest Trigger <nfd::fw::Strategy::beforeSatisfyInterest()>`
132
133 This method will be triggered just before the pending Interest is being satisfied. It may
134 be useful override this method, e.g., to measure data plane performance.
135
136- :nfd:`Before Expire Interest Trigger <nfd::fw::Strategy::beforeExpirePendingInterest()>`
137
138 This method will be triggered just before the pending Interest is timed out. Same as with
139 the before satisfy interest trigger, this method may be useful o measure data plane
140 performance.
141
142Example
143^^^^^^^
144
145The following code implements a random load balancing forwarding strategy. This strategy
146was created by Steve DiBenedetto and was found in one of his `GitHub repository
147<https://github.com/dibenede/ndn-tutorial-gec21>`_.
148
149.. literalinclude:: ../../examples/ndn-load-balancer/random-load-balancer-strategy.hpp
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800150 :language: c++
151 :linenos:
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800152 :lines: 26-
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800153
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800154.. literalinclude:: ../../examples/ndn-load-balancer/random-load-balancer-strategy.cpp
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800155 :language: c++
156 :linenos:
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800157 :lines: 26-
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800158
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800159This forwarding strategy can be enabled for a specific name prefix when developing the
160simulation scenario as follows:
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800161
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800162 .. code-block:: c++
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800163
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800164 #include "random-load-balancer-strategy.hpp"
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800165
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800166 ...
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800167
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800168 StrategyChoiceHelper::Install<nfd::fw::RandomLoadBalancerStrategy>(nodes, prefix);
Alexander Afanasyeve97c6072012-11-21 23:51:12 -0800169
170Example of using custom strategy
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800171^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyeve97c6072012-11-21 23:51:12 -0800172
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800173Please refer to :ref:`6-node topology with custom NFD forwarding strategy` .