1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2010-2014 Intel Corporation. 3 4IP Fragmentation and Reassembly Library 5======================================= 6 7The IP Fragmentation and Reassembly Library implements IPv4 and IPv6 packet fragmentation and reassembly. 8 9Packet fragmentation 10-------------------- 11 12Packet fragmentation routines divide input packet into number of fragments. 13Both rte_ipv4_fragment_packet() and rte_ipv6_fragment_packet() functions assume that input mbuf data 14points to the start of the IP header of the packet (i.e. L2 header is already stripped out). 15To avoid copying of the actual packet's data zero-copy technique is used (rte_pktmbuf_attach). 16For each fragment two new mbufs are created: 17 18* Direct mbuf -- mbuf that will contain L3 header of the new fragment. 19 20* Indirect mbuf -- mbuf that is attached to the mbuf with the original packet. 21 It's data field points to the start of the original packets data plus fragment offset. 22 23Then L3 header is copied from the original mbuf into the 'direct' mbuf and updated to reflect new fragmented status. 24Note that for IPv4, header checksum is not recalculated and is set to zero. 25 26Finally 'direct' and 'indirect' mbufs for each fragment are linked together via mbuf's next filed to compose a packet for the new fragment. 27 28The caller has an ability to explicitly specify which mempools should be used to allocate 'direct' and 'indirect' mbufs from. 29 30For more information about direct and indirect mbufs, refer to :ref:`direct_indirect_buffer`. 31 32Packet reassembly 33----------------- 34 35IP Fragment Table 36~~~~~~~~~~~~~~~~~ 37 38Fragment table maintains information about already received fragments of the packet. 39 40Each IP packet is uniquely identified by triple <Source IP address>, <Destination IP address>, <ID>. 41 42Note that all update/lookup operations on Fragment Table are not thread safe. 43So if different execution contexts (threads/processes) will access the same table simultaneously, 44then some external syncing mechanism have to be provided. 45 46Each table entry can hold information about packets consisting of up to RTE_LIBRTE_IP_FRAG_MAX (by default: 8) fragments. 47 48Code example, that demonstrates creation of a new Fragment table: 49 50.. code-block:: c 51 52 frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * max_flow_ttl; 53 bucket_num = max_flow_num + max_flow_num / 4; 54 frag_tbl = rte_ip_frag_table_create(max_flow_num, bucket_entries, max_flow_num, frag_cycles, socket_id); 55 56Internally Fragment table is a simple hash table. 57The basic idea is to use two hash functions and <bucket_entries> \* associativity. 58This provides 2 \* <bucket_entries> possible locations in the hash table for each key. 59When the collision occurs and all 2 \* <bucket_entries> are occupied, 60instead of reinserting existing keys into alternative locations, ip_frag_tbl_add() just returns a failure. 61 62Also, entries that resides in the table longer then <max_cycles> are considered as invalid, 63and could be removed/replaced by the new ones. 64 65Note that reassembly demands a lot of mbuf's to be allocated. 66At any given time up to (2 \* bucket_entries \* RTE_LIBRTE_IP_FRAG_MAX \* <maximum number of mbufs per packet>) 67can be stored inside Fragment Table waiting for remaining fragments. 68 69Packet Reassembly 70~~~~~~~~~~~~~~~~~ 71 72Fragmented packets processing and reassembly is done by the rte_ipv4_frag_reassemble_packet()/rte_ipv6_frag_reassemble_packet. 73Functions. They either return a pointer to valid mbuf that contains reassembled packet, 74or NULL (if the packet can't be reassembled for some reason). 75 76These functions are responsible for: 77 78#. Search the Fragment Table for entry with packet's <IPv4 Source Address, IPv4 Destination Address, Packet ID>. 79 80#. If the entry is found, then check if that entry already timed-out. 81 If yes, then free all previously received fragments, and remove information about them from the entry. 82 83#. If no entry with such key is found, then try to create a new one by one of two ways: 84 85 a) Use as empty entry. 86 87 b) Delete a timed-out entry, free mbufs associated with it mbufs and store a new entry with specified key in it. 88 89#. Update the entry with new fragment information and check if a packet can be reassembled 90 (the packet's entry contains all fragments). 91 92 a) If yes, then, reassemble the packet, mark table's entry as empty and return the reassembled mbuf to the caller. 93 94 b) If no, then return a NULL to the caller. 95 96If at any stage of packet processing an error is encountered 97(e.g: can't insert new entry into the Fragment Table, or invalid/timed-out fragment), 98then the function will free all associated with the packet fragments, 99mark the table entry as invalid and return NULL to the caller. 100 101Debug logging and Statistics Collection 102~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 103 104The RTE_LIBRTE_IP_FRAG_TBL_STAT config macro controls statistics collection for the Fragment Table. 105This macro is not enabled by default. 106