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 | |
| 24 | VAR-NUMBER := BYTE+ |
| 25 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 26 | 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] | 27 | |
| 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 Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 40 | One-octet number:: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 41 | |
Alexander Afanasyev | a6fc727 | 2014-06-13 11:58:06 -0700 | [diff] [blame] | 42 | 0 1 2 3 4 5 6 7 |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 43 | +----------------+ |
| 44 | | < 253 = NUMBER | |
| 45 | +----------------+ |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 46 | |
| 47 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 48 | Two-octet number:: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 49 | |
| 50 | 1 2 |
Alexander Afanasyev | a6fc727 | 2014-06-13 11:58:06 -0700 | [diff] [blame] | 51 | 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 Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 52 | +---------------+---------------+---------------+ |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 53 | | 253 | NUMBER (MSB) NUMBER (LSB) | |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 54 | +---------------+---------------+---------------+ |
| 55 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 56 | Four-octet number:: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 57 | |
| 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 Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 61 | | 254 | NUMBER (MSB) / |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 62 | +---------------+---------------+----------------+--------------+ |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 63 | | NUMBER (LSB) | |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 64 | +---------------+ |
| 65 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 66 | Eight-octet number:: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 67 | |
| 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 Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 71 | | 255 | NUMBER (MSB) / |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 72 | +---------------+ + |
| 73 | | / |
| 74 | + +---------------+----------------+--------------+ |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 75 | | NUMBER (LSB) | |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 76 | +---------------+ |
| 77 | |
| 78 | |
| 79 | .. _TLV: |
| 80 | |
| 81 | TLV Encoding |
| 82 | ~~~~~~~~~~~~ |
| 83 | |
Alexander Afanasyev | 4b89611 | 2014-06-23 21:47:15 -0700 | [diff] [blame] | 84 | TLV encoding for NDN packets is defined as follows:: |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 85 | |
| 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 | |
| 92 | TLV-TYPE SHOULD be unique at all nested levels. |
Alexander Afanasyev | 9998e28 | 2015-11-13 00:23:16 +0200 | [diff] [blame] | 93 | The TLV Type number space and initial assignments listed in Section :ref:`types` of this document. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 94 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 95 | The ``TLV-LENGTH`` field indicates number of bytes that ``TLV-VALUE`` uses. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 96 | It **does not** include number of bytes that ``TLV-TYPE`` and ``TLV-LENGTH`` fields themselves occupy. |
| 97 | In particular, empty payload TLV will carry ``TLV-LENGTH`` equal to 0. |
| 98 | |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 99 | This encoding offers a reasonable balance between compactness and flexibility. |
Alexander Afanasyev | 9998e28 | 2015-11-13 00:23:16 +0200 | [diff] [blame] | 100 | Most common, standardized Type codes 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] | 101 | |
| 102 | Non Negative Integer Encoding |
| 103 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 104 | |
| 105 | A number of TLV elements in NDN packet format take a non-negative integer as their value, with the following definition:: |
| 106 | |
| 107 | nonNegativeInteger ::= BYTE+ |
| 108 | |
Junxiao Shi | 1082b42 | 2017-07-18 23:37:52 +0000 | [diff] [blame^] | 109 | TLV-LENGTH of the TLV element MUST be either 1, 2, 4, or 8. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 110 | Depending on the length value, a nonNegativeInteger is encoded as follows: |
| 111 | |
| 112 | - if the length is 1 (i.e. the value length is 1 octet), the nonNegativeInteger is encoded in one octet; |
| 113 | |
Alexander Afanasyev | a6fc727 | 2014-06-13 11:58:06 -0700 | [diff] [blame] | 114 | - if the length is 2 (= value length is 2 octets), the nonNegativeInteger is encoded in 2 octets, in net byte-order; |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 115 | |
Alexander Afanasyev | a6fc727 | 2014-06-13 11:58:06 -0700 | [diff] [blame] | 116 | - if the length is 4 (= value length is 4 octets), the nonNegativeInteger is encoded in 4 octets, in net byte-order; |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 117 | |
Alexander Afanasyev | a6fc727 | 2014-06-13 11:58:06 -0700 | [diff] [blame] | 118 | - if the length is 8 (= value length is 8 octets), the nonNegativeInteger is encoded in 8 octets, in net byte-order. |
Alexander Afanasyev | eee8c25 | 2013-11-21 23:22:41 +0000 | [diff] [blame] | 119 | |
Cenk Gündoğan | bc5f0ec | 2016-09-14 15:49:52 +0200 | [diff] [blame] | 120 | 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] | 121 | |
| 122 | 0 => TT0100 |
| 123 | 1 => TT0101 |
| 124 | 255 => TT01FF |
| 125 | 256 => TT020100 |
| 126 | 65535 => TT02FFFF |
| 127 | 65536 => TT0400010000 |