blob: 5741ea1c69dc39c0bf0cfb017e5d53bb93d90ec2 [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
24 VAR-NUMBER := BYTE+
25
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
28- if the first octet is < 253, the number is encoded in that octet;
29
30- if the first octet == 253, the number is encoded in the
31 following 2 octets, in net byte-order;
32
33- if the first octet == 254, the number is encoded in the
34 following 4 octets, in net byte-order;
35
36- if the first octet == 255, the number is encoded in the
37 following 8 octets, in net byte-order.
38
39
Junxiao Shi1082b422017-07-18 23:37:52 +000040One-octet number::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000041
Alexander Afanasyeva6fc7272014-06-13 11:58:06 -070042 0 1 2 3 4 5 6 7
Junxiao Shi1082b422017-07-18 23:37:52 +000043 +----------------+
44 | < 253 = NUMBER |
45 +----------------+
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000046
47
Junxiao Shi1082b422017-07-18 23:37:52 +000048Two-octet number::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000049
50 1 2
Alexander Afanasyeva6fc7272014-06-13 11:58:06 -070051 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000052 +---------------+---------------+---------------+
Junxiao Shi1082b422017-07-18 23:37:52 +000053 | 253 | NUMBER (MSB) NUMBER (LSB) |
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000054 +---------------+---------------+---------------+
55
Junxiao Shi1082b422017-07-18 23:37:52 +000056Four-octet number::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000057
58 1 2 3
59 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
60 +---------------+---------------+----------------+--------------+
Junxiao Shi1082b422017-07-18 23:37:52 +000061 | 254 | NUMBER (MSB) /
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000062 +---------------+---------------+----------------+--------------+
Junxiao Shi1082b422017-07-18 23:37:52 +000063 | NUMBER (LSB) |
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000064 +---------------+
65
Junxiao Shi1082b422017-07-18 23:37:52 +000066Eight-octet number::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000067
68 1 2 3
69 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
70 +---------------+---------------+----------------+--------------+
Junxiao Shi1082b422017-07-18 23:37:52 +000071 | 255 | NUMBER (MSB) /
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000072 +---------------+ +
73 | /
74 + +---------------+----------------+--------------+
Junxiao Shi1082b422017-07-18 23:37:52 +000075 | NUMBER (LSB) |
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000076 +---------------+
77
78
79.. _TLV:
80
81TLV Encoding
82~~~~~~~~~~~~
83
Alexander Afanasyev4b896112014-06-23 21:47:15 -070084TLV encoding for NDN packets is defined as follows::
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000085
86 NDN-TLV := TLV-TYPE TLV-LENGTH TLV-VALUE?
87 TLV-TYPE := VAR-NUMBER
88 TLV-LENGTH := VAR-NUMBER
89 TLV-VALUE := BYTE+
90
91
92TLV-TYPE SHOULD be unique at all nested levels.
Alexander Afanasyev9998e282015-11-13 00:23:16 +020093The TLV Type number space and initial assignments listed in Section :ref:`types` of this document.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000094
Junxiao Shi1082b422017-07-18 23:37:52 +000095The ``TLV-LENGTH`` field indicates number of bytes that ``TLV-VALUE`` uses.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000096It **does not** include number of bytes that ``TLV-TYPE`` and ``TLV-LENGTH`` fields themselves occupy.
97In particular, empty payload TLV will carry ``TLV-LENGTH`` equal to 0.
98
Alexander Afanasyeveee8c252013-11-21 23:22:41 +000099This encoding offers a reasonable balance between compactness and flexibility.
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500100Most 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 +0000101
102Non Negative Integer Encoding
103~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104
105A number of TLV elements in NDN packet format take a non-negative integer as their value, with the following definition::
106
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500107 nonNegativeInteger ::= BYTE{1} | BYTE{2} | BYTE{4} | BYTE{8}
Alexander Afanasyeveee8c252013-11-21 23:22:41 +0000108
Junxiao Shi1082b422017-07-18 23:37:52 +0000109TLV-LENGTH of the TLV element MUST be either 1, 2, 4, or 8.
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500110Depending on TLV-LENGTH, a nonNegativeInteger is encoded as follows:
Alexander Afanasyeveee8c252013-11-21 23:22:41 +0000111
112- if the length is 1 (i.e. the value length is 1 octet), the nonNegativeInteger is encoded in one octet;
113
Alexander Afanasyeva6fc7272014-06-13 11:58:06 -0700114- if the length is 2 (= value length is 2 octets), the nonNegativeInteger is encoded in 2 octets, in net byte-order;
Alexander Afanasyeveee8c252013-11-21 23:22:41 +0000115
Alexander Afanasyeva6fc7272014-06-13 11:58:06 -0700116- if the length is 4 (= value length is 4 octets), the nonNegativeInteger is encoded in 4 octets, in net byte-order;
Alexander Afanasyeveee8c252013-11-21 23:22:41 +0000117
Alexander Afanasyeva6fc7272014-06-13 11:58:06 -0700118- if the length is 8 (= value length is 8 octets), the nonNegativeInteger is encoded in 8 octets, in net byte-order.
Alexander Afanasyeveee8c252013-11-21 23:22:41 +0000119
Cenk Gündoğanbc5f0ec2016-09-14 15:49:52 +0200120The 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 +0000121
122 0 => TT0100
123 1 => TT0101
124 255 => TT01FF
125 256 => TT020100
126 65535 => TT02FFFF
127 65536 => TT0400010000
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500128
Junxiao Shi707ea002018-07-17 15:42:52 -0400129.. _evolvability:
130
Alexander Afanasyeve9f48512018-01-15 23:44:50 -0500131Considerations for Evolvability of TLV-Based Encoding
132~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133
134To 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".
135A compliant TLV format decoder should follow the order, quantity, and presence requirements of the recognized elements defined in the corresponding specification.
136At the same time, if decoder encounters an unrecognized or out-of-order element, the behavior should be as follows:
137
138- if the least significant bit of element's TLV-TYPE number is ``1``, abort decoding and report an error;
139- if the least significant bit of element's TLV-TYPE number is ``0``, ignore the element and continue decoding.
140
141.. note::
142 A recognized element is considered out-of-order if it appears in the element order that violates a specification. For example,
143 - when a specification defines a sequence {``F1`` ``F2`` ``F3``}, an element ``F3`` would be out-of-order in the sequence {``F1`` ``F3`` ``F2``};
144 - for {``F1`` ``F2?`` ``F3``} specification (i.e., when ``F2`` is optional, ``F2`` would be out-of-order in the same sequence {``F1`` ``F3`` ``F2``}.
145
146.. note::
147 TLV-TYPE numbers 0-31 (inclusive) are "grandfathered" and all designated as "critical" for the purpose of packet processing.