blob: 35d4edbef0e3e18cf97cff5cd279f4b65e783aea [file] [log] [blame]
Alexander Afanasyeveee8c252013-11-21 23:22:41 +00001Type-Length-Value (TLV) Encoding
Davide Pesaventoec288fe2022-11-26 18:28:01 -05002================================
Alexander Afanasyeveee8c252013-11-21 23:22:41 +00003
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 Pesaventoec288fe2022-11-26 18:28:01 -050013
Davide Pesaventoacaccaf2022-03-06 19:00:06 -050014Variable-Size Encoding for Type and Length
Davide Pesaventoec288fe2022-11-26 18:28:01 -050015------------------------------------------
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000016
Davide Pesavento23e340c2021-12-03 04:52:22 -050017.. note::
18 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 +000019
20To 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.
21For implementation simplicity, both type and length take the same encoding format.
22
23We define a variable-length encoding for numbers in NDN as follows::
24
Davide Pesavento23e340c2021-12-03 04:52:22 -050025 VAR-NUMBER-1 = %x00-FC
26 VAR-NUMBER-3 = %xFD 2OCTET
27 VAR-NUMBER-5 = %xFE 4OCTET
28 VAR-NUMBER-9 = %xFF 8OCTET
29
30.. note::
Davide Pesavento4d6c3572022-05-27 12:05:05 -040031 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 +000032
Junxiao Shi1082b422017-07-18 23:37:52 +000033The 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 +000034
Davide Pesavento4d6c3572022-05-27 12:05:05 -040035- 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 +000036
Davide Pesavento4d6c3572022-05-27 12:05:05 -040037- If the first octet is 253 (``0xFD``), the number is encoded in the following 2 octets, in network byte-order.
38 This number must be greater than 252 (``0xFC``).
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000039
Davide Pesavento4d6c3572022-05-27 12:05:05 -040040- If the first octet is 254 (``0xFE``), the number is encoded in the following 4 octets, in network byte-order.
41 This number must be greater than 65535 (``0xFFFF``).
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000042
Davide Pesavento4d6c3572022-05-27 12:05:05 -040043- If the first octet is 255 (``0xFF``), the number is encoded in the following 8 octets, in network byte-order.
44 This number must be greater than 4294967295 (``0xFFFFFFFF``).
Junxiao Shi459e4662019-05-31 15:28:03 +000045
Davide Pesaventoec288fe2022-11-26 18:28:01 -050046A number MUST be encoded using the shortest possible format.
Davide Pesavento4d6c3572022-05-27 12:05:05 -040047For 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 +000048
Davide Pesaventoec288fe2022-11-26 18:28:01 -050049
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000050.. _TLV:
51
Davide Pesavento751939c2020-06-26 22:16:24 -040052NDN TLV Encoding
Davide Pesaventoec288fe2022-11-26 18:28:01 -050053----------------
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000054
Alexander Afanasyev4b896112014-06-23 21:47:15 -070055TLV encoding for NDN packets is defined as follows::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000056
Junxiao Shi78ce2952019-05-07 15:34:00 -040057 NDN-TLV = TLV-TYPE TLV-LENGTH TLV-VALUE
Junxiao Shi459e4662019-05-31 15:28:03 +000058 TLV-TYPE = VAR-NUMBER-1 / VAR-NUMBER-3 / VAR-NUMBER-5
59 TLV-LENGTH = VAR-NUMBER-1 / VAR-NUMBER-3 / VAR-NUMBER-5 / VAR-NUMBER-9
Junxiao Shi78ce2952019-05-07 15:34:00 -040060 TLV-VALUE = *OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000061
Davide Pesaventoec288fe2022-11-26 18:28:01 -050062``TLV-TYPE`` MUST be in the range [1, 4294967295].
63Type 0 is reserved to indicate an invalid TLV element and MUST NOT appear on the wire.
64``TLV-TYPE`` SHOULD be unique at all nested levels.
65Section :ref:`types` of this document lists the initial ``TLV-TYPE`` assignments.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000066
Junxiao Shi1082b422017-07-18 23:37:52 +000067The ``TLV-LENGTH`` field indicates number of bytes that ``TLV-VALUE`` uses.
Davide Pesaventoec288fe2022-11-26 18:28:01 -050068It does not include the number of bytes that ``TLV-TYPE`` and ``TLV-LENGTH`` fields themselves occupy.
69In particular, a TLV element with empty value will have ``TLV-LENGTH`` equal to 0.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000070
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000071This encoding offers a reasonable balance between compactness and flexibility.
Davide Pesaventoec288fe2022-11-26 18:28:01 -050072Most 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.
73
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000074
Davide Pesavento751939c2020-06-26 22:16:24 -040075Non-Negative Integer Encoding
Davide Pesaventoec288fe2022-11-26 18:28:01 -050076-----------------------------
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000077
Davide Pesaventof9353df2020-06-21 19:19:56 -040078A 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 +000079
Davide Pesaventof9353df2020-06-21 19:19:56 -040080 NonNegativeInteger = 1OCTET / 2OCTET / 4OCTET / 8OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000081
Davide Pesaventof9353df2020-06-21 19:19:56 -040082The TLV-LENGTH of the TLV element enclosing a ``NonNegativeInteger`` MUST be either 1, 2, 4, or 8.
83Depending on the TLV-LENGTH, a ``NonNegativeInteger`` is encoded as follows:
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000084
Davide Pesaventof9353df2020-06-21 19:19:56 -040085- if the length is 1, the ``NonNegativeInteger`` is encoded in 1 octet;
86- if the length is 2, the ``NonNegativeInteger`` is encoded in 2 octets, in network byte-order;
87- if the length is 4, the ``NonNegativeInteger`` is encoded in 4 octets, in network byte-order;
88- if the length is 8, the ``NonNegativeInteger`` is encoded in 8 octets, in network byte-order.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000089
Davide Pesaventoec746762020-06-24 22:57:31 -040090The 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):
91
92.. code-block:: none
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000093
94 0 => TT0100
95 1 => TT0101
96 255 => TT01FF
97 256 => TT020100
98 65535 => TT02FFFF
99 65536 => TT0400010000
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500100
Davide Pesaventoec288fe2022-11-26 18:28:01 -0500101
Junxiao Shi707ea002018-07-17 15:42:52 -0400102.. _evolvability:
103
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500104Considerations for Evolvability of TLV-Based Encoding
Davide Pesaventoec288fe2022-11-26 18:28:01 -0500105-----------------------------------------------------
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500106
Davide Pesaventoec288fe2022-11-26 18:28:01 -0500107To ensure that the TLV-based protocol can evolve over time without requiring flag days, the least significant bit of ``TLV-TYPE`` (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 -0500108A 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 -0400109At 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 -0500110
Davide Pesaventoec288fe2022-11-26 18:28:01 -0500111- if the least significant bit of the element's ``TLV-TYPE`` number is 1, abort decoding and report an error;
112- if the least significant bit of the element's ``TLV-TYPE`` number is 0, ignore the element and continue decoding;
113- ``TLV-TYPE`` numbers in the range [0, 31] are "grandfathered" and are all designated as *critical* for the purposes of packet processing.
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500114
115.. note::
Davide Pesavento23e340c2021-12-03 04:52:22 -0500116 A recognized element is considered out-of-order if it appears in the element order that violates a specification. For example:
117
118 - when a specification defines a sequence {``F1`` ``F2`` ``F3``}, an element ``F3`` would be out-of-order in the sequence {``F1`` ``F3`` ``F2``};
119 - 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 -0400120
Davide Pesaventoec288fe2022-11-26 18:28:01 -0500121
Davide Pesavento4d6c3572022-05-27 12:05:05 -0400122.. rubric:: Footnotes
123
124.. [#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/>`__