blob: 240af0202a36b594e037ae013961c0df48eed6dc [file] [log] [blame]
Alexander Afanasyeveee8c252013-11-21 23:22:41 +00001Type-Length-Value (TLV) Encoding
2--------------------------------
3
Davide Pesavento4d6c3572022-05-27 12:05:05 -04004Each NDN packet is encoded in :abbr:`TLV (Type-Length-Value)` format.
5NDN Interest and Data packets are distinguished by the type number in the first and outermost TLV\ :sub:`0`\ .
Alexander Afanasyeveee8c252013-11-21 23:22:41 +00006
7An NDN packet is mainly a collection of TLVs inside TLV\ :sub:`0`\ . Some TLVs may contain sub-TLVs, and each sub-TLV may also be further nested. A guiding design principle is to keep the order of TLV\ :sub:`i`\ s deterministic, and keep the level of nesting as small as possible to minimize both processing overhead and chances for errors.
8
9Note that NDN packet format does not have a fixed packet header nor does it encode a protocol version number. Instead the design uses the TLV format to provide the flexibility of adding new types and phasing out old types as the protocol evolves over time. The absence of a fixed header makes it possible to support packets of very small sizes efficiently, without the header overhead.
10There is also no packet fragmentation support at network level.
Davide Pesavento4d6c3572022-05-27 12:05:05 -040011Whenever needed, NDN packets may be fragmented and reassembled hop-by-hop\ [#f1]_.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000012
Davide Pesaventoacaccaf2022-03-06 19:00:06 -050013Variable-Size Encoding for Type and Length
14~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000015
Davide Pesavento23e340c2021-12-03 04:52:22 -050016.. note::
17 The text below and that in the :ref:`TLV` section are adapted from an earlier packet specification draft by Mark Stapp.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000018
19To minimize the overhead during early deployment and to allow flexibility of future protocol extensions to meet unforeseeable needs, both type (T) and length (L) take a variable size format.
20For implementation simplicity, both type and length take the same encoding format.
21
22We define a variable-length encoding for numbers in NDN as follows::
23
Davide Pesavento23e340c2021-12-03 04:52:22 -050024 VAR-NUMBER-1 = %x00-FC
25 VAR-NUMBER-3 = %xFD 2OCTET
26 VAR-NUMBER-5 = %xFE 4OCTET
27 VAR-NUMBER-9 = %xFF 8OCTET
28
29.. note::
Davide Pesavento4d6c3572022-05-27 12:05:05 -040030 The formal grammar of the NDN packet format in this specification is given using :rfc:`Augmented BNF for Syntax Specifications <5234>`.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000031
Junxiao Shi1082b422017-07-18 23:37:52 +000032The first octet of the number either carries the actual number, or signals that a multi-octet encoding is present, as defined below:
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000033
Davide Pesavento4d6c3572022-05-27 12:05:05 -040034- If the first octet is less than or equal to 252 (``0xFC``), the number is encoded in that octet.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000035
Davide Pesavento4d6c3572022-05-27 12:05:05 -040036- If the first octet is 253 (``0xFD``), the number is encoded in the following 2 octets, in network byte-order.
37 This number must be greater than 252 (``0xFC``).
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000038
Davide Pesavento4d6c3572022-05-27 12:05:05 -040039- If the first octet is 254 (``0xFE``), the number is encoded in the following 4 octets, in network byte-order.
40 This number must be greater than 65535 (``0xFFFF``).
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000041
Davide Pesavento4d6c3572022-05-27 12:05:05 -040042- If the first octet is 255 (``0xFF``), the number is encoded in the following 8 octets, in network byte-order.
43 This number must be greater than 4294967295 (``0xFFFFFFFF``).
Junxiao Shi459e4662019-05-31 15:28:03 +000044
45A number MUST be encoded in the shortest format.
Davide Pesavento4d6c3572022-05-27 12:05:05 -040046For example, the number 1024 is encoded as ``%xFD0400`` in ``VAR-NUMBER-3`` format, not ``%xFE00000400`` in ``VAR-NUMBER-5`` format.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000047
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000048.. _TLV:
49
Davide Pesavento751939c2020-06-26 22:16:24 -040050NDN TLV Encoding
51~~~~~~~~~~~~~~~~
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000052
Alexander Afanasyev4b896112014-06-23 21:47:15 -070053TLV encoding for NDN packets is defined as follows::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000054
Junxiao Shi78ce2952019-05-07 15:34:00 -040055 NDN-TLV = TLV-TYPE TLV-LENGTH TLV-VALUE
Junxiao Shi459e4662019-05-31 15:28:03 +000056 TLV-TYPE = VAR-NUMBER-1 / VAR-NUMBER-3 / VAR-NUMBER-5
57 TLV-LENGTH = VAR-NUMBER-1 / VAR-NUMBER-3 / VAR-NUMBER-5 / VAR-NUMBER-9
Junxiao Shi78ce2952019-05-07 15:34:00 -040058 TLV-VALUE = *OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000059
Junxiao Shidcb0f372019-04-06 18:45:55 +000060TLV-TYPE MUST be in the range ``1-4294967295`` (inclusive).
61Zero is reserved to indicate an invalid TLV element and MUST NOT appear on the wire.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000062TLV-TYPE SHOULD be unique at all nested levels.
Junxiao Shidcb0f372019-04-06 18:45:55 +000063Section :ref:`types` of this document lists initial TLV-TYPE assignments.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000064
Junxiao Shi1082b422017-07-18 23:37:52 +000065The ``TLV-LENGTH`` field indicates number of bytes that ``TLV-VALUE`` uses.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000066It **does not** include number of bytes that ``TLV-TYPE`` and ``TLV-LENGTH`` fields themselves occupy.
67In particular, empty payload TLV will carry ``TLV-LENGTH`` equal to 0.
68
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000069This encoding offers a reasonable balance between compactness and flexibility.
Alexander Afanasyeve9f48512018-01-15 23:44:50 -050070Most common, standardized TLV-TYPE numbers will be allocated from a small-integer number-space, and these common types will be able to use the compact, single-byte encoding.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000071
Davide Pesavento751939c2020-06-26 22:16:24 -040072Non-Negative Integer Encoding
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000073~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74
Davide Pesaventof9353df2020-06-21 19:19:56 -040075A number of TLV elements in the NDN packet format take a non-negative integer as their TLV-VALUE, with the following definition::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000076
Davide Pesaventof9353df2020-06-21 19:19:56 -040077 NonNegativeInteger = 1OCTET / 2OCTET / 4OCTET / 8OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000078
Davide Pesaventof9353df2020-06-21 19:19:56 -040079The TLV-LENGTH of the TLV element enclosing a ``NonNegativeInteger`` MUST be either 1, 2, 4, or 8.
80Depending on the TLV-LENGTH, a ``NonNegativeInteger`` is encoded as follows:
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000081
Davide Pesaventof9353df2020-06-21 19:19:56 -040082- if the length is 1, the ``NonNegativeInteger`` is encoded in 1 octet;
83- if the length is 2, the ``NonNegativeInteger`` is encoded in 2 octets, in network byte-order;
84- if the length is 4, the ``NonNegativeInteger`` is encoded in 4 octets, in network byte-order;
85- if the length is 8, the ``NonNegativeInteger`` is encoded in 8 octets, in network byte-order.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000086
Davide Pesaventoec746762020-06-24 22:57:31 -040087The following shows a few examples of TLVs that have a ``NonNegativeInteger`` as their value component in hexadecimal format (where ``TT`` represents the TLV-TYPE, followed by the TLV-LENGTH, and then the TLV-VALUE):
88
89.. code-block:: none
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000090
91 0 => TT0100
92 1 => TT0101
93 255 => TT01FF
94 256 => TT020100
95 65535 => TT02FFFF
96 65536 => TT0400010000
Alexander Afanasyeve9f48512018-01-15 23:44:50 -050097
Junxiao Shi707ea002018-07-17 15:42:52 -040098.. _evolvability:
99
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500100Considerations for Evolvability of TLV-Based Encoding
101~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
Davide Pesavento3c0bc312020-05-18 22:03:09 -0400103To ensure that the TLV-based protocol can evolve over time without requiring flag days, the least significant bit of TLV-TYPE number (unless overridden by the specification of a particular network/library/application TLV element) is reserved to indicate whether that TLV element is "critical" or "non-critical".
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500104A compliant TLV format decoder should follow the order, quantity, and presence requirements of the recognized elements defined in the corresponding specification.
Davide Pesavento3c0bc312020-05-18 22:03:09 -0400105At the same time, if the decoder encounters an unrecognized or out-of-order element, the behavior should be as follows:
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500106
Davide Pesavento3c0bc312020-05-18 22:03:09 -0400107- if the least significant bit of the element's TLV-TYPE number is ``1``, abort decoding and report an error;
108- if the least significant bit of the element's TLV-TYPE number is ``0``, ignore the element and continue decoding;
109- TLV-TYPE numbers 0-31 (inclusive) are "grandfathered" and are all designated as "critical" for the purposes of packet processing.
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500110
111.. note::
Davide Pesavento23e340c2021-12-03 04:52:22 -0500112 A recognized element is considered out-of-order if it appears in the element order that violates a specification. For example:
113
114 - when a specification defines a sequence {``F1`` ``F2`` ``F3``}, an element ``F3`` would be out-of-order in the sequence {``F1`` ``F3`` ``F2``};
115 - for {``F1`` ``F2?`` ``F3``} specification (i.e., when ``F2`` is optional, ``F2`` would be out-of-order in the same sequence {``F1`` ``F3`` ``F2``}.
Davide Pesavento4d6c3572022-05-27 12:05:05 -0400116
117.. rubric:: Footnotes
118
119.. [#f1] `"Packet Fragmentation in NDN: Why NDN Uses Hop-By-Hop Fragmentation (NDN Memo)" by A. Afanasyev, J. Shi, L. Wang, B. Zhang, and L. Zhang., NDN Memo, Technical Report NDN-0032 <https://named-data.net/publications/techreports/ndn-0032-1-ndn-memo-fragmentation/>`__