blob: 01da1ebe2f8178104629bf061c3c05336551ab10 [file] [log] [blame]
Alexander Afanasyeveee8c252013-11-21 23:22:41 +00001Type-Length-Value (TLV) Encoding
2--------------------------------
3
Junxiao Shi1082b422017-07-18 23:37:52 +00004Each NDN packet is encoded in a Type-Length-Value (TLV) format. NDN Interest and Data packets are distinguished by the type number in the first and outmost TLV\ :sub:`0`\ .
Alexander Afanasyeveee8c252013-11-21 23:22:41 +00005
6An 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.
7
8Note 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.
9There is also no packet fragmentation support at network level.
10Whenever needed, NDN packets may be fragmented and reassembled hop-by-hop. [#f1]_
11
Davide Pesavento4d177602020-06-21 22:30:55 -040012.. [#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/>`__
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000013
Davide Pesaventoacaccaf2022-03-06 19:00:06 -050014Variable-Size Encoding for Type and Length
15~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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::
31 The formal grammar of 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
Junxiao Shi459e4662019-05-31 15:28:03 +000035- 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
Junxiao Shi459e4662019-05-31 15:28:03 +000037- 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
Junxiao Shi459e4662019-05-31 15:28:03 +000040- 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
Junxiao Shi459e4662019-05-31 15:28:03 +000043- 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).
45
46A number MUST be encoded in the shortest format.
47For 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
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000049.. _TLV:
50
Davide Pesavento751939c2020-06-26 22:16:24 -040051NDN TLV Encoding
52~~~~~~~~~~~~~~~~
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000053
Alexander Afanasyev4b896112014-06-23 21:47:15 -070054TLV encoding for NDN packets is defined as follows::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000055
Junxiao Shi78ce2952019-05-07 15:34:00 -040056 NDN-TLV = TLV-TYPE TLV-LENGTH TLV-VALUE
Junxiao Shi459e4662019-05-31 15:28:03 +000057 TLV-TYPE = VAR-NUMBER-1 / VAR-NUMBER-3 / VAR-NUMBER-5
58 TLV-LENGTH = VAR-NUMBER-1 / VAR-NUMBER-3 / VAR-NUMBER-5 / VAR-NUMBER-9
Junxiao Shi78ce2952019-05-07 15:34:00 -040059 TLV-VALUE = *OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000060
Junxiao Shidcb0f372019-04-06 18:45:55 +000061TLV-TYPE MUST be in the range ``1-4294967295`` (inclusive).
62Zero is reserved to indicate an invalid TLV element and MUST NOT appear on the wire.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000063TLV-TYPE SHOULD be unique at all nested levels.
Junxiao Shidcb0f372019-04-06 18:45:55 +000064Section :ref:`types` of this document lists initial TLV-TYPE assignments.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000065
Junxiao Shi1082b422017-07-18 23:37:52 +000066The ``TLV-LENGTH`` field indicates number of bytes that ``TLV-VALUE`` uses.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000067It **does not** include number of bytes that ``TLV-TYPE`` and ``TLV-LENGTH`` fields themselves occupy.
68In particular, empty payload TLV will carry ``TLV-LENGTH`` equal to 0.
69
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000070This encoding offers a reasonable balance between compactness and flexibility.
Alexander Afanasyeve9f48512018-01-15 23:44:50 -050071Most 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 +000072
Davide Pesavento751939c2020-06-26 22:16:24 -040073Non-Negative Integer Encoding
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000074~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75
Davide Pesaventof9353df2020-06-21 19:19:56 -040076A 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 +000077
Davide Pesaventof9353df2020-06-21 19:19:56 -040078 NonNegativeInteger = 1OCTET / 2OCTET / 4OCTET / 8OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000079
Davide Pesaventof9353df2020-06-21 19:19:56 -040080The TLV-LENGTH of the TLV element enclosing a ``NonNegativeInteger`` MUST be either 1, 2, 4, or 8.
81Depending on the TLV-LENGTH, a ``NonNegativeInteger`` is encoded as follows:
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000082
Davide Pesaventof9353df2020-06-21 19:19:56 -040083- if the length is 1, the ``NonNegativeInteger`` is encoded in 1 octet;
84- if the length is 2, the ``NonNegativeInteger`` is encoded in 2 octets, in network byte-order;
85- if the length is 4, the ``NonNegativeInteger`` is encoded in 4 octets, in network byte-order;
86- if the length is 8, the ``NonNegativeInteger`` is encoded in 8 octets, in network byte-order.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000087
Davide Pesaventoec746762020-06-24 22:57:31 -040088The 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):
89
90.. code-block:: none
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000091
92 0 => TT0100
93 1 => TT0101
94 255 => TT01FF
95 256 => TT020100
96 65535 => TT02FFFF
97 65536 => TT0400010000
Alexander Afanasyeve9f48512018-01-15 23:44:50 -050098
Junxiao Shi707ea002018-07-17 15:42:52 -040099.. _evolvability:
100
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500101Considerations for Evolvability of TLV-Based Encoding
102~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103
Davide Pesavento3c0bc312020-05-18 22:03:09 -0400104To 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 -0500105A 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 -0400106At 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 -0500107
Davide Pesavento3c0bc312020-05-18 22:03:09 -0400108- if the least significant bit of the element's TLV-TYPE number is ``1``, abort decoding and report an error;
109- if the least significant bit of the element's TLV-TYPE number is ``0``, ignore the element and continue decoding;
110- 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 -0500111
112.. note::
Davide Pesavento23e340c2021-12-03 04:52:22 -0500113 A recognized element is considered out-of-order if it appears in the element order that violates a specification. For example:
114
115 - when a specification defines a sequence {``F1`` ``F2`` ``F3``}, an element ``F3`` would be out-of-order in the sequence {``F1`` ``F3`` ``F2``};
116 - for {``F1`` ``F2?`` ``F3``} specification (i.e., when ``F2`` is optional, ``F2`` would be out-of-order in the same sequence {``F1`` ``F3`` ``F2``}.