ip_fragment_reassembly_lib.rst revision 97f17497
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
31IP Fragmentation and Reassembly Library
32=======================================
33
34The IP Fragmentation and Reassembly Library implements IPv4 and IPv6 packet fragmentation and reassembly.
35
36Packet fragmentation
37--------------------
38
39Packet fragmentation routines divide input packet into number of fragments.
40Both rte_ipv4_fragment_packet() and rte_ipv6_fragment_packet() functions assume that input mbuf data
41points to the start of the IP header of the packet (i.e. L2 header is already stripped out).
42To avoid copying of the actual packet's data zero-copy technique is used (rte_pktmbuf_attach).
43For 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
50Then L3 header is copied from the original mbuf into the 'direct' mbuf and updated to reflect new fragmented status.
51Note that for IPv4, header checksum is not recalculated and is set to zero.
52
53Finally 'direct' and 'indirect' mbufs for each fragment are linked together via mbuf's next filed to compose a packet for the new fragment.
54
55The caller has an ability to explicitly specify which mempools should be used to allocate 'direct' and 'indirect' mbufs from.
56
57For more information about direct and indirect mbufs, refer to :ref:`direct_indirect_buffer`.
58
59Packet reassembly
60-----------------
61
62IP Fragment Table
63~~~~~~~~~~~~~~~~~
64
65Fragment table maintains information about already received fragments of the packet.
66
67Each IP packet is uniquely identified by triple <Source IP address>, <Destination IP address>, <ID>.
68
69Note that all update/lookup operations on Fragment Table are not thread safe.
70So if different execution contexts (threads/processes) will access the same table simultaneously,
71then some external syncing mechanism have to be provided.
72
73Each table entry can hold information about packets consisting of up to RTE_LIBRTE_IP_FRAG_MAX (by default: 4) fragments.
74
75Code example, that demonstrates creation of a new Fragment table:
76
77.. code-block:: c
78
79    frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * max_flow_ttl;
80    bucket_num = max_flow_num + max_flow_num / 4;
81    frag_tbl = rte_ip_frag_table_create(max_flow_num, bucket_entries, max_flow_num, frag_cycles, socket_id);
82
83Internally Fragment table is a simple hash table.
84The basic idea is to use two hash functions and <bucket_entries> \* associativity.
85This provides 2 \* <bucket_entries> possible locations in the hash table for each key.
86When the collision occurs and all 2 \* <bucket_entries> are occupied,
87instead of reinserting existing keys into alternative locations, ip_frag_tbl_add() just returns a failure.
88
89Also, entries that resides in the table longer then <max_cycles> are considered as invalid,
90and could be removed/replaced by the new ones.
91
92Note that reassembly demands a lot of mbuf's to be allocated.
93At any given time up to (2 \* bucket_entries \* RTE_LIBRTE_IP_FRAG_MAX \* <maximum number of mbufs per packet>)
94can be stored inside Fragment Table waiting for remaining fragments.
95
96Packet Reassembly
97~~~~~~~~~~~~~~~~~
98
99Fragmented packets processing and reassembly is done by the rte_ipv4_frag_reassemble_packet()/rte_ipv6_frag_reassemble_packet.
100Functions. They either return a pointer to valid mbuf that contains reassembled packet,
101or NULL (if the packet can't be reassembled for some reason).
102
103These functions are responsible for:
104
105#.  Search the Fragment Table for entry with packet's <IPv4 Source Address, IPv4 Destination Address, Packet ID>.
106
107#.  If the entry is found, then check if that entry already timed-out.
108    If yes, then free all previously received fragments, and remove information about them from the entry.
109
110#.  If no entry with such key is found, then try to create a new one by one of two ways:
111
112    a) Use as empty entry.
113
114    b) Delete a timed-out entry, free mbufs associated with it mbufs and store a new entry with specified key in it.
115
116#.  Update the entry with new fragment information and check if a packet can be reassembled
117    (the packet's entry contains all fragments).
118
119    a) If yes, then, reassemble the packet, mark table's entry as empty and return the reassembled mbuf to the caller.
120
121    b) If no, then return a NULL to the caller.
122
123If at any stage of packet processing an error is encountered
124(e.g: can't insert new entry into the Fragment Table, or invalid/timed-out fragment),
125then the function will free all associated with the packet fragments,
126mark the table entry as invalid and return NULL to the caller.
127
128Debug logging and Statistics Collection
129~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130
131The RTE_LIBRTE_IP_FRAG_TBL_STAT config macro controls statistics collection for the Fragment Table.
132This macro is not enabled by default.
133
134The RTE_LIBRTE_IP_FRAG_DEBUG controls debug logging of IP fragments processing and reassembling.
135This macro is disabled by default.
136Note that while logging contains a lot of detailed information,
137it slows down packet processing and might cause the loss of a lot of packets.
138