Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 1 | Type-Length-Value (TLV) Encoding |
| 2 | -------------------------------- |
| 3 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame] | 4 | Each 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 Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 5 | |
| 6 | An 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 | |
| 8 | Note 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. |
| 9 | There is also no packet fragmentation support at network level. |
| 10 | Whenever needed, NDN packets may be fragmented and reassembled hop-by-hop. [#f1]_ |
| 11 | |
Alexander Afanasyev | 9998e28 | 2015-11-13 00:23:16 +0200 | [diff] [blame] | 12 | .. [#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 Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 13 | |
| 14 | Variable 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 | |
| 19 | To 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. |
| 20 | For implementation simplicity, both type and length take the same encoding format. |
| 21 | |
| 22 | We define a variable-length encoding for numbers in NDN as follows:: |
| 23 | |
Junxiao Shi | 459e466 | 2019-05-31 15:28:03 +0000 | [diff] [blame^] | 24 | VAR-NUMBER-1 = %x00-FC |
| 25 | VAR-NUMBER-3 = %xFD 2OCTET |
| 26 | VAR-NUMBER-5 = %xFE 4OCTET |
| 27 | VAR-NUMBER-9 = %xFF 8OCTET |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 28 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame] | 29 | The first octet of the number either carries the actual number, or signals that a multi-octet encoding is present, as defined below: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 30 | |
Junxiao Shi | 459e466 | 2019-05-31 15:28:03 +0000 | [diff] [blame^] | 31 | - If the first octet is less than or equal to 252 (0xFC), the number is encoded in that octet. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 32 | |
Junxiao Shi | 459e466 | 2019-05-31 15:28:03 +0000 | [diff] [blame^] | 33 | - If the first octet is 253 (0xFD), the number is encoded in the following 2 octets, in network byte-order. |
| 34 | This number must be greater than 252 (0xFC). |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 35 | |
Junxiao Shi | 459e466 | 2019-05-31 15:28:03 +0000 | [diff] [blame^] | 36 | - If the first octet is 254 (0xFE), the number is encoded in the following 4 octets, in network byte-order. |
| 37 | This number must be greater than 65535 (0xFFFF). |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 38 | |
Junxiao Shi | 459e466 | 2019-05-31 15:28:03 +0000 | [diff] [blame^] | 39 | - If the first octet is 255 (0xFF), the number is encoded in the following 8 octets, in network byte-order. |
| 40 | This number must be greater than 4294967295 (0xFFFFFFFF). |
| 41 | |
| 42 | A number MUST be encoded in the shortest format. |
| 43 | For example, the number 1024 is encoded as %xFD0400 in VAR-NUMBER-3 format, not %xFE00000400 in VAR-NUMBER-5 format. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 44 | |
Junxiao Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 45 | .. note:: |
| 46 | 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 Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 47 | |
| 48 | .. _TLV: |
| 49 | |
| 50 | TLV Encoding |
| 51 | ~~~~~~~~~~~~ |
| 52 | |
Alexander Afanasyev | 4b89611 | 2014-06-23 21:47:15 -0700 | [diff] [blame] | 53 | TLV encoding for NDN packets is defined as follows:: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 54 | |
Junxiao Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 55 | NDN-TLV = TLV-TYPE TLV-LENGTH TLV-VALUE |
Junxiao Shi | 459e466 | 2019-05-31 15:28:03 +0000 | [diff] [blame^] | 56 | 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 Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 58 | TLV-VALUE = *OCTET |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 59 | |
Junxiao Shi | dcb0f37 | 2019-04-06 18:45:55 +0000 | [diff] [blame] | 60 | TLV-TYPE MUST be in the range ``1-4294967295`` (inclusive). |
| 61 | Zero is reserved to indicate an invalid TLV element and MUST NOT appear on the wire. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 62 | TLV-TYPE SHOULD be unique at all nested levels. |
Junxiao Shi | dcb0f37 | 2019-04-06 18:45:55 +0000 | [diff] [blame] | 63 | Section :ref:`types` of this document lists initial TLV-TYPE assignments. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 64 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame] | 65 | The ``TLV-LENGTH`` field indicates number of bytes that ``TLV-VALUE`` uses. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 66 | It **does not** include number of bytes that ``TLV-TYPE`` and ``TLV-LENGTH`` fields themselves occupy. |
| 67 | In particular, empty payload TLV will carry ``TLV-LENGTH`` equal to 0. |
| 68 | |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 69 | This encoding offers a reasonable balance between compactness and flexibility. |
Alexander Afanasyev | e9f4851 | 2018-01-15 23:44:50 -0500 | [diff] [blame] | 70 | Most 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 Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 71 | |
| 72 | Non Negative Integer Encoding |
| 73 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 | |
Junxiao Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 75 | A number of TLV elements in NDN packet format take a non-negative integer as their TLV-VALUE, with the following definition:: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 76 | |
Junxiao Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 77 | nonNegativeInteger = 1OCTET / 2OCTET / 4OCTET / 8OCTET |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 78 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame] | 79 | TLV-LENGTH of the TLV element MUST be either 1, 2, 4, or 8. |
Alexander Afanasyev | e9f4851 | 2018-01-15 23:44:50 -0500 | [diff] [blame] | 80 | Depending on TLV-LENGTH, a nonNegativeInteger is encoded as follows: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 81 | |
Junxiao Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 82 | - if the length is 1, the nonNegativeInteger is encoded in one octet; |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 83 | |
Junxiao Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 84 | - if the length is 2, the nonNegativeInteger is encoded in 2 octets, in network byte-order; |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 85 | |
Junxiao Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 86 | - if the length is 4, the nonNegativeInteger is encoded in 4 octets, in network byte-order; |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 87 | |
Junxiao Shi | 78ce295 | 2019-05-07 15:34:00 -0400 | [diff] [blame] | 88 | - if the length is 8, the nonNegativeInteger is encoded in 8 octets, in network byte-order. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 89 | |
Cenk Gündoğan | bc5f0ec | 2016-09-14 15:49:52 +0200 | [diff] [blame] | 90 | The 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 Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 91 | |
| 92 | 0 => TT0100 |
| 93 | 1 => TT0101 |
| 94 | 255 => TT01FF |
| 95 | 256 => TT020100 |
| 96 | 65535 => TT02FFFF |
| 97 | 65536 => TT0400010000 |
Alexander Afanasyev | e9f4851 | 2018-01-15 23:44:50 -0500 | [diff] [blame] | 98 | |
Junxiao Shi | 707ea00 | 2018-07-17 15:42:52 -0400 | [diff] [blame] | 99 | .. _evolvability: |
| 100 | |
Alexander Afanasyev | e9f4851 | 2018-01-15 23:44:50 -0500 | [diff] [blame] | 101 | Considerations for Evolvability of TLV-Based Encoding |
| 102 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 103 | |
| 104 | To 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". |
| 105 | A compliant TLV format decoder should follow the order, quantity, and presence requirements of the recognized elements defined in the corresponding specification. |
| 106 | At the same time, if decoder encounters an unrecognized or out-of-order element, the behavior should be as follows: |
| 107 | |
| 108 | - if the least significant bit of element's TLV-TYPE number is ``1``, abort decoding and report an error; |
| 109 | - if the least significant bit of element's TLV-TYPE number is ``0``, ignore the element and continue decoding. |
| 110 | |
| 111 | .. note:: |
| 112 | A recognized element is considered out-of-order if it appears in the element order that violates a specification. For example, |
| 113 | - when a specification defines a sequence {``F1`` ``F2`` ``F3``}, an element ``F3`` would be out-of-order in the sequence {``F1`` ``F3`` ``F2``}; |
| 114 | - for {``F1`` ``F2?`` ``F3``} specification (i.e., when ``F2`` is optional, ``F2`` would be out-of-order in the same sequence {``F1`` ``F3`` ``F2``}. |
| 115 | |
| 116 | .. note:: |
| 117 | TLV-TYPE numbers 0-31 (inclusive) are "grandfathered" and all designated as "critical" for the purpose of packet processing. |