blob: c98e134d2d0b835acaa1009fbe2a02cdc68315fe [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
16read `NFD Developer's Guide
17<http://named-data.net/wp-content/uploads/2014/07/NFD-developer-guide.pdf>`_, section 5.
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080018
Alexander Afanasyeve6852122013-01-21 11:54:47 -080019Available forwarding strategies
20+++++++++++++++++++++++++++++++
21
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080022+--------------------------------------------+----------------------------------------------------------------------------------------------+
23| Strategy Name | Description |
24+============================================+==============================================================================================+
25| ``/localhost/nfd/strategy/best-route`` | :nfd:`Best Route Strategy <nfd::fw::BestRouteStrategy2>` **(default)** |
26| | |
27| | The best route strategy forwards an Interest to the |
28| | upstream with lowest routing cost. |
29+--------------------------------------------+----------------------------------------------------------------------------------------------+
30+--------------------------------------------+----------------------------------------------------------------------------------------------+
31| ``/localhost/nfd/strategy/ncc`` | :nfd:`NCC Strategy <nfd::fw::NccStrategy>` |
32| | |
33| | The NCC strategy is an reimplementation of CCNx 0.7.2 |
34| | default strategy. It has similar algorithm but is not |
35| | guaranteed to be equivalent. |
36+--------------------------------------------+----------------------------------------------------------------------------------------------+
37+--------------------------------------------+----------------------------------------------------------------------------------------------+
38| ``/localhost/nfd/strategy/broadcast`` | :nfd:`Broadcast Strategy <nfd::fw::BroadcastStrategy>` |
39| | |
40| | The broadcast strategy forwards every Interest to all |
41| | upstreams, indicated by the supplied FIB entry. |
42+--------------------------------------------+----------------------------------------------------------------------------------------------+
43+--------------------------------------------+----------------------------------------------------------------------------------------------+
44| ``/localhost/nfd/strategy/client-control`` | :nfd:`Client Control Strategy <nfd::fw::ClientControlStrategy>` |
45| | |
46| | The client control strategy allows a local consumer |
47| | application to choose the outgoing face of each Interest. |
48+--------------------------------------------+----------------------------------------------------------------------------------------------+
Alexander Afanasyeve6852122013-01-21 11:54:47 -080049
Alexander Afanasyeve6852122013-01-21 11:54:47 -080050
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080051.. note::
Alexander Afanasyeve6852122013-01-21 11:54:47 -080052
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080053 ndnSIM 2.0 exactly like NFD allows different namespaces to be associated with different
54 forwarding strategies. By default, the following forwarding strategy configuration is
55 defined:
Alexander Afanasyeve6852122013-01-21 11:54:47 -080056
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080057 +--------------------+-----------------------------------------------+
58 | Namespace | Strategy Name |
59 +====================+===============================================+
60 | ``/`` | ``/localhost/nfd/strategy/best-route`` |
61 +--------------------+-----------------------------------------------+
62 | ``/localhost`` | ``/localhost/nfd/strategy/broadcast`` |
63 +--------------------+-----------------------------------------------+
64 | ``/localhost/nfd`` | ``/localhost/nfd/strategy/best-route`` |
65 +--------------------+-----------------------------------------------+
66 | ``/ndn/broadcast`` | ``/localhost/nfd/strategy/broadcast`` |
67 +--------------------+-----------------------------------------------+
68
69
70
71Examples:
Alexander Afanasyeve6852122013-01-21 11:54:47 -080072
73 .. code-block:: c++
74
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080075 StrategyChoiceHelper::Install(nodes, prefix,
76 "/localhost/nfd/strategy/broadcast");
Alexander Afanasyeve6852122013-01-21 11:54:47 -080077
Alexander Afanasyeve6852122013-01-21 11:54:47 -080078
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080079.. _Writing your own custom strategy:
80
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080081Writing your own custom strategy
82++++++++++++++++++++++++++++++++
83
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080084One of the objectives and features of ndnSIM 2.0 is that it uses NFD codebase for packet
85forwarding. Therefore, writing strategy in ndnSIM is almost exactly the same process as
86outlined in `Section 5.3 of the NFD Developer's Guide
87<http://named-data.net/wp-content/uploads/2014/07/NFD-developer-guide.pdf>`_.
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080088
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080089The purpose of the strategy is to decides how to forward Interests and it is not intended to
90override any processing steps in the forwarding pipelines of NFD. If it is desired to support
91a new packet type (other than Interest and Data), a new field in Interest or Data packets, or
92override some actions in the pipelines (e.g., disable ContentStore lookup), it can be
93accomplished by modification of the forwarding pipelines in :nfd:`nfd::Forwarder` class.
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080094
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080095The initial step in creating a new strategy is to create a class, say ``MyStrategy`` that is
96derived from :nfd:`nfd::fw::Strategy`. This subclass must at least override the triggers that are
97marked pure virtual and may override other available triggers that are marked just virtual.
98
99If the strategy needs to store information, it is needed to decide whether the information
100is related to a namespace or an Interest. Information related to a namespace but not
101specific to an Interest should be stored in Measurements entries; information related to an
102Interest should be stored in PIT entries, PIT downstream records, or PIT upstream records.
103After this decision is made, a data structure derived from StrategyInfo class needs to be
104declared. In the existing implementation, such data structures are declared as nested
105classes as it provides natural grouping and scope protection of the strategy-specific
106entity, but it is not required to follow the same model. If timers are needed, EventId
107fields needs to be added to such data structure(s).
108
109.. image:: _static/nfd-forwarding-overview.png
110 :alt: Packet processing in NFD/ndnSIM is broken into a number of small pipelines and
111 strategy callbacks
112
113The final step is to implement one or more of the triggers with the desired strategy logic.
114These triggers are listed below:
115
116- :nfd:`After Receive Interest Trigger <nfd::fw::Strategy::afterReceiveInterest()>`
117
118 When an Interest is received, passes necessary checks, and needs to be forwarded, Incoming
119 Interest pipeline (Section 4.2.1) invokes this trigger with the PIT entry, incoming Interest
120 packet, and FIB entry. At that time, the following conditions hold for the Interest:
121
122 - The Interest does not violate ``/localhost`` scope.
123 - The Interest is not looped.
124 - The Interest cannot be satisfied by ContentStore.
125 - The Interest is under a namespace managed by this strategy.
126
127 After being triggered, the strategy should decide whether and where to forward this
128 Interest. If the strategy decides to forward this Interest, it should invoke send Interest
129 action at least once. If the strategy concludes that this Interest cannot be forwarded, it
130 should invoke reject pending Interest action, so that the PIT entry will be deleted
131 shortly.
132
133- :nfd:`Before Satisfy Interest Trigger <nfd::fw::Strategy::beforeSatisfyInterest()>`
134
135 This method will be triggered just before the pending Interest is being satisfied. It may
136 be useful override this method, e.g., to measure data plane performance.
137
138- :nfd:`Before Expire Interest Trigger <nfd::fw::Strategy::beforeExpirePendingInterest()>`
139
140 This method will be triggered just before the pending Interest is timed out. Same as with
141 the before satisfy interest trigger, this method may be useful o measure data plane
142 performance.
143
144Example
145^^^^^^^
146
147The following code implements a random load balancing forwarding strategy. This strategy
148was created by Steve DiBenedetto and was found in one of his `GitHub repository
149<https://github.com/dibenede/ndn-tutorial-gec21>`_.
150
151.. literalinclude:: ../../examples/ndn-load-balancer/random-load-balancer-strategy.hpp
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800152 :language: c++
153 :linenos:
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800154 :lines: 26-
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800155
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800156.. literalinclude:: ../../examples/ndn-load-balancer/random-load-balancer-strategy.cpp
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800157 :language: c++
158 :linenos:
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800159 :lines: 26-
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800160
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800161This forwarding strategy can be enabled for a specific name prefix when developing the
162simulation scenario as follows:
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800163
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800164 .. code-block:: c++
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800165
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800166 #include "random-load-balancer-strategy.hpp"
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800167
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800168 ...
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -0800169
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800170 StrategyChoiceHelper::Install<nfd::fw::RandomLoadBalancerStrategy>(nodes, prefix);
Alexander Afanasyeve97c6072012-11-21 23:51:12 -0800171
172Example of using custom strategy
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800173^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyeve97c6072012-11-21 23:51:12 -0800174
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800175Please refer to :ref:`6-node topology with custom NFD forwarding strategy` .