d17daa79dbde2c7520365f35861525421cc4d3e1
[dpdk.git] / doc / guides / prog_guide / ip_fragment_reassembly_lib.rst
1 ..  BSD LICENSE
2     Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
3     All rights reserved.
4
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8
9     * Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in
13     the documentation and/or other materials provided with the
14     distribution.
15     * Neither the name of Intel Corporation nor the names of its
16     contributors may be used to endorse or promote products derived
17     from this software without specific prior written permission.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23     OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 IP Fragmentation and Reassembly Library
32 =======================================
33
34 The IP Fragmentation and Reassembly Library implements IPv4 and IPv6 packet fragmentation and reassembly.
35
36 Packet fragmentation
37 --------------------
38
39 Packet fragmentation routines devide input packet into number of fragments.
40 Both rte_ipv4_fragment_packet() and rte_ipv6_fragment_packet() functions assume that input mbuf data
41 points to the start of the IP header of the packet (i.e. L2 header is already stripped out).
42 To avoid copying fo the actual packet's data zero-copy technique is used (rte_pktmbuf_attach).
43 For each fragment two new mbufs are created:
44
45 *   Direct mbuf -- mbuf that will contain L3 header of the new fragment.
46
47 *   Indirect mbuf -- mbuf that is attached to the mbuf with the original packet.
48     It's data field points to the start of the original packets data plus fragment offset.
49
50 Then L3 header is copied from the original mbuf into the 'direct' mbuf and updated to reflect new fragmented status.
51 Note that for IPv4, header checksum is not recalculated and is set to zero.
52
53 Finally 'direct' and 'indirect' mbufs for each fragnemt are linked together via mbuf's next filed to compose a packet for the new fragment.
54
55 The caller has an ability to explicitly specify which mempools should be used to allocate 'direct' and 'indirect' mbufs from.
56
57 Note that configuration macro RTE_MBUF_SCATTER_GATHER has to be enabled to make fragmentation library build and work correctly.
58 For more information about direct and indirect mbufs, refer to the *DPDK Programmers guide 7.7 Direct and Indirect Buffers.*
59
60 Packet reassembly
61 -----------------
62
63 IP Fragment Table
64 ~~~~~~~~~~~~~~~~~
65
66 Fragment table maintains information about already received fragments of the packet.
67
68 Each IP packet is uniquely identified by triple <Source IP address>, <Destination IP address>, <ID>.
69
70 Note that all update/lookup operations on Fragmen Table are not thread safe.
71 So if different execution contexts (threads/processes) will access the same table simultaneously,
72 then some exernal syncing mechanism have to be provided.
73
74 Each table entry can hold information about packets consisting of up to RTE_LIBRTE_IP_FRAG_MAX (by default: 4) fragments.
75
76 Code example, that demonstrates creation of a new Fragment table:
77
78 .. code-block:: c
79
80     frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * max_flow_ttl;
81     bucket_num = max_flow_num + max_flow_num / 4;
82     frag_tbl = rte_ip_frag_table_create(max_flow_num, bucket_entries, max_flow_num, frag_cycles, socket_id);
83
84 Internally Fragmen table is a simple hash table.
85 The basic idea is to use two hash functions and <bucket_entries> \* associativity.
86 This provides 2 \* <bucket_entries> possible locations in the hash table for each key.
87 When the collision occurs and all 2 \* <bucket_entries> are occupied,
88 instead of resinserting existing keys into alternative locations, ip_frag_tbl_add() just returns a faiure.
89
90 Also, entries that resides in the table longer then <max_cycles> are considered as invalid,
91 and could be removed/replaced by the new ones.
92
93 Note that reassembly demands a lot of mbuf's to be allocated.
94 At any given time up to (2 \* bucket_entries \* RTE_LIBRTE_IP_FRAG_MAX \* <maximum number of mbufs per packet>)
95 can be stored inside Fragment Table waiting for remaining fragments.
96
97 Packet Reassembly
98 ~~~~~~~~~~~~~~~~~
99
100 Fragmented packets processing and reassembly is done by the rte_ipv4_frag_reassemble_packet()/rte_ipv6_frag_reassemble_packet.
101 Functions. They either return a pointer to valid mbuf that contains reassembled packet,
102 or NULL (if the packet can't be reassembled for some reason).
103
104 These functions are responsible for:
105
106 #.  Search the Fragment Table for entry with packet's <IPv4 Source Address, IPv4 Destination Address, Packet ID>.
107
108 #.  If the entry is found, then check if that entry already timed-out.
109     If yes, then free all previously received fragments, and remove information about them from the entry.
110
111 #.  If no entry with such key is found, then try to create a new one by one of two ways:
112
113     a) Use as empty entry.
114
115     b) Delete a timed-out entry, free mbufs associated with it mbufs and store a new entry with specified key in it.
116
117 #.  Update the entry with new fragment information and check if a packet can be reassembled
118     (the packet's entry contains all fragments).
119
120     a) If yes, then, reassemble the packet, mark table's entry as empty and return the reassembled mbuf to the caller.
121
122     b) If no, then return a NULL to the caller.
123
124 If at any stage of packet processing an error is envountered
125 (e.g: can't insert new entry into the Fragment Table, or invalid/timed-out fragment),
126 then the function will free all associated with the packet fragments,
127 mark the table entry as invalid and return NULL to the caller.
128
129 Debug logging and Statistics Collection
130 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131
132 The RTE_LIBRTE_IP_FRAG_TBL_STAT config macro controls statistics collection for the Fragment Table.
133 This macro is not enabled by default.
134
135 The RTE_LIBRTE_IP_FRAG_DEBUG controls debug logging of IP fragments processing and reassembling.
136 This macro is disabled by default.
137 Note that while logging contains a lot of detailed information,
138 it slows down packet processing and might cause the loss of a lot of packets.