In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how the TCP/IP protocol to achieve ip slicing, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Analyze the ip fragments. First of all, we need to understand why we need slicing. For example, in Ethernet, using the CSMA/CD protocol (implemented by the network card), it specifies the maximum (MTU) and minimum values of a link layer packet (excluding Mac headers, but this version of the kernel implementation includes the size of mac headers). So if the upper packet is greater than this threshold, it needs to be fragmented. The implementation of sharding and grouping is in the ip layer. Let's look at the specific logic. The logic of ip fragmentation is implemented in the ip_fragment function.
Void ip_fragment (
Struct sock * sk
Struct sk_buff * skb
Struct device * dev
Int is_frag
)
Some of the variables defined.
Struct iphdr * iph
Unsigned char * raw
Unsigned char * ptr
Struct sk_buff * skb2
Int left, mtu, hlen, len
Int offset
Unsigned long flags
/ / mac first address
Raw = skb- > data
/ / ip header address. Hard_header_len is the mac header size.
Iph = (struct iphdr *) (raw + dev- > hard_header_len)
Skb- > ip_hdr = iph
/ / size of the ip header, excluding the data part
Hlen = (iph- > ihl * sizeof (unsigned long))
/ / the total size of the ip packet minus the data length of the ip layer equal to the ip message, that is, the size of the part that needs to be sliced
Left = ntohs (iph- > tot_len)-hlen
/ / ip header + Mac header
Hlen + = dev- > hard_header_len
/ / the length of the data part of each shard is equal to the mtu of the mac layer minus the Mac header and the ip header, that is, the mtu of the mac layer includes the sum of the mac header, the ip header and the ip data part.
Mtu = (dev- > mtu-hlen)
/ / first address of the data section
Ptr = (raw + hlen)
Determine whether it can be sliced or not.
/ / send icmp message if it cannot be sliced, which can be compared with ip message format
If (ntohs (iph- > frag_off) & IP_DF)
{
Icmp_send (skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev- > mtu, dev)
Return
}
Determine whether the ip packet to be sliced is itself a shard. That is, after many times of ip slicing.
/ *
The ip message itself is a fragment, and now it needs to be sliced again.
The offset first address is the first address of the message multiplied by 8, because when the message is sliced again, his offset is
Based on the offset of the original unsliced data. Not for the offset of the current slice.
, /
If (is_frag & 2)
Offset = (ntohs (iph- > frag_off) & 0x1fff) 0)
{
/ / ip packet defaults to the number of bytes carried, but if it is greater than mtu, it takes a small value, that is, mtu.
Len = left
/ / if it is greater than mtu, it has to be shredded, that is, it can only carry bytes of mtu size, otherwise it will be the last shard.
If (len > mtu)
Len = mtu
/ *
The following judgment holds when the remaining bytes are larger than mtu
That is, the remaining bytes can not be sent this time, but will continue to be sliced.
Except 8 times 8 is a multiple of 8, which is not necessarily equal to mtu
, /
If (len
< left) { len/=8; len*=8; } // len 为这一分片承载的数据大小 // 申请新的skb,大小为mac头+ip头+数据部分长度 if ((skb2 = alloc_skb(len + hlen,GFP_ATOMIC)) == NULL) { return; } skb2->Arp = skb- > arp
Skb2- > free = 1
/ / the total size is Mac head + IP head + data part length
Skb2- > len = len + hlen
/ / point to the memory header address you just assigned, and start copying data.
Skb2- > h.raw = (char *) skb2- > data
Save_flags (flags)
Restore_flags (flags)
/ / ip address
Skb2- > raddr = skb- > raddr
/ / raw points to the mac header address. Here, both the mac header and the ip header + option are copied to skb. The ip option should only be copied to the first shard. Here, it will be copied to each shard.
Memcpy (skb2- > h.raw, raw, hlen)
/ / copy the data part with the length of len,ptr pointing to the first address of the data part in the original ip message
Memcpy (skb2- > h.raw + hlen, ptr, len)
/ / the number of bytes remaining to be shredded
Left-= len
/ / point to the ip header address
Skb2- > h.rawboat devise-> hard_header_len
Iph = (struct iphdr *) (skb2- > h.raw)
/ / set the offset of the shard, divided by 8, see the ip protocol.
Iph- > frag_off = htons ((offset > > 3))
/ *
1. If there is still data, then set MF, and need more shards.
2. Is_frag = 1; it means there are more shards after the shard.
Indicates that the data to be sliced is itself an ip shard, that is, re-shard.
So all the sliced MF under the newspaper is 1.
, /
If (left > 0 | | (is_frag & 1))
Iph- > frag_off | = htons (IP_MF)
/ / Update data pointers and offsets
Ptr + = len
Offset + = len
/ / send shards
Ip_queue_xmit (sk, dev, skb2, 2)
}
The main logic of slicing is
1 apply for a new memory, copy the mac header and ip header in the message to be sliced into the new memory, and then cut the data part and continue to copy it behind the memory. So, until the slicing is finished.
2 modify the values of some fields in the ip message, such as MF.
3 adjust the underlying interface to send fragments one by one
The logic of slicing is not complicated and it is not explained in too much detail.
The above is all the contents of the article "how to achieve ip fragmentation in TCP/IP Protocol". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.