blob: 4e294ecb2eab534f506a7e68b3c0039657824fea [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
Alexander Afanasyev9998e282015-11-13 00:23:16 +020012.. [#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 <http://named-data.net/publications/techreports/ndn-0032-1-ndn-memo-fragmentation/>`__
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000013
14Variable Size Encoding for type (T) and length (L)
15~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16
17(Both the text below and that in :ref:`TLV encoding section <TLV>` are adopted from an earlier packet specification draft by Mark Stapp)
18
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
Junxiao Shi78ce2952019-05-07 15:34:00 -040024 VAR-NUMBER = %x00-FC / %xFD 2OCTET / %xFE 4OCTET / %xFF 8OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000025
Junxiao Shi1082b422017-07-18 23:37:52 +000026The 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 +000027
Junxiao Shi78ce2952019-05-07 15:34:00 -040028- 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 +000029
Junxiao Shi78ce2952019-05-07 15:34:00 -040030- if the first octet is 253 (0xFD), the number is encoded in the following 2 octets, in network byte-order;
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000031
Junxiao Shi78ce2952019-05-07 15:34:00 -040032- if the first octet is 254 (0xFE), the number is encoded in the following 4 octets, in network byte-order;
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000033
Junxiao Shi78ce2952019-05-07 15:34:00 -040034- if the first octet is 255 (0xFF), the number is encoded in the following 8 octets, in network byte-order.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000035
Junxiao Shi78ce2952019-05-07 15:34:00 -040036.. note::
37 The formal grammar of NDN packet format in this specification is given using `Augmented BNF for Syntax Specifications <https://tools.ietf.org/html/rfc5234>`__
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000038
39.. _TLV:
40
41TLV Encoding
42~~~~~~~~~~~~
43
Alexander Afanasyev4b896112014-06-23 21:47:15 -070044TLV encoding for NDN packets is defined as follows::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000045
Junxiao Shi78ce2952019-05-07 15:34:00 -040046 NDN-TLV = TLV-TYPE TLV-LENGTH TLV-VALUE
47 TLV-TYPE = VAR-NUMBER
48 TLV-LENGTH = VAR-NUMBER
49 TLV-VALUE = *OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000050
Junxiao Shidcb0f372019-04-06 18:45:55 +000051TLV-TYPE MUST be in the range ``1-4294967295`` (inclusive).
52Zero is reserved to indicate an invalid TLV element and MUST NOT appear on the wire.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000053TLV-TYPE SHOULD be unique at all nested levels.
Junxiao Shidcb0f372019-04-06 18:45:55 +000054Section :ref:`types` of this document lists initial TLV-TYPE assignments.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000055
Junxiao Shi1082b422017-07-18 23:37:52 +000056The ``TLV-LENGTH`` field indicates number of bytes that ``TLV-VALUE`` uses.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000057It **does not** include number of bytes that ``TLV-TYPE`` and ``TLV-LENGTH`` fields themselves occupy.
58In particular, empty payload TLV will carry ``TLV-LENGTH`` equal to 0.
59
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000060This encoding offers a reasonable balance between compactness and flexibility.
Alexander Afanasyeve9f48512018-01-15 23:44:50 -050061Most 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 +000062
63Non Negative Integer Encoding
64~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65
Junxiao Shi78ce2952019-05-07 15:34:00 -040066A number of TLV elements in NDN packet format take a non-negative integer as their TLV-VALUE, with the following definition::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000067
Junxiao Shi78ce2952019-05-07 15:34:00 -040068 nonNegativeInteger = 1OCTET / 2OCTET / 4OCTET / 8OCTET
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000069
Junxiao Shi1082b422017-07-18 23:37:52 +000070TLV-LENGTH of the TLV element MUST be either 1, 2, 4, or 8.
Alexander Afanasyeve9f48512018-01-15 23:44:50 -050071Depending on TLV-LENGTH, a nonNegativeInteger is encoded as follows:
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000072
Junxiao Shi78ce2952019-05-07 15:34:00 -040073- if the length is 1, the nonNegativeInteger is encoded in one octet;
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000074
Junxiao Shi78ce2952019-05-07 15:34:00 -040075- if the length is 2, the nonNegativeInteger is encoded in 2 octets, in network byte-order;
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000076
Junxiao Shi78ce2952019-05-07 15:34:00 -040077- if the length is 4, the nonNegativeInteger is encoded in 4 octets, in network byte-order;
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000078
Junxiao Shi78ce2952019-05-07 15:34:00 -040079- if the length is 8, the nonNegativeInteger is encoded in 8 octets, in network byte-order.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000080
Cenk Gündoğanbc5f0ec2016-09-14 15:49:52 +020081The following shows a few examples of TLVs that have nonNegativeInteger as their value component in hexadecimal format (where ``TT`` represents ``TLV-TYPE``, followed by the ``TLV-LENGTH``, then ``TLV-VALUE``)::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000082
83 0 => TT0100
84 1 => TT0101
85 255 => TT01FF
86 256 => TT020100
87 65535 => TT02FFFF
88 65536 => TT0400010000
Alexander Afanasyeve9f48512018-01-15 23:44:50 -050089
Junxiao Shi707ea002018-07-17 15:42:52 -040090.. _evolvability:
91
Alexander Afanasyeve9f48512018-01-15 23:44:50 -050092Considerations for Evolvability of TLV-Based Encoding
93~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
95To ensure that the TLV-based protocol can evolve over time without requiring flag days, the least significant bit of TLV-TYPE number (unless overriden by the specification of a particular network/library/application TLV element) is reserved to indicate whether that TLV element is "critical" or "non-critical".
96A compliant TLV format decoder should follow the order, quantity, and presence requirements of the recognized elements defined in the corresponding specification.
97At the same time, if decoder encounters an unrecognized or out-of-order element, the behavior should be as follows:
98
99- if the least significant bit of element's TLV-TYPE number is ``1``, abort decoding and report an error;
100- if the least significant bit of element's TLV-TYPE number is ``0``, ignore the element and continue decoding.
101
102.. note::
103 A recognized element is considered out-of-order if it appears in the element order that violates a specification. For example,
104 - when a specification defines a sequence {``F1`` ``F2`` ``F3``}, an element ``F3`` would be out-of-order in the sequence {``F1`` ``F3`` ``F2``};
105 - for {``F1`` ``F2?`` ``F3``} specification (i.e., when ``F2`` is optional, ``F2`` would be out-of-order in the same sequence {``F1`` ``F3`` ``F2``}.
106
107.. note::
108 TLV-TYPE numbers 0-31 (inclusive) are "grandfathered" and all designated as "critical" for the purpose of packet processing.