diff --git a/tcpip/buffer/prependable.go b/tcpip/buffer/prependable.go index d7f765f..fa9e280 100644 --- a/tcpip/buffer/prependable.go +++ b/tcpip/buffer/prependable.go @@ -23,7 +23,7 @@ func (p Prependable) UsedLength() int { return len(p.buf) - p.usedIdx } -// 从内到外取出报文头的协议 +// 从内到外暴露报文头的协议 eth|ipv4|tcp func (p *Prependable) Prepend(size int) []byte { if size > p.usedIdx { return nil diff --git a/tcpip/network/fragmentation/fragmentation.go b/tcpip/network/fragmentation/fragmentation.go index 947f3e5..beaf7fe 100644 --- a/tcpip/network/fragmentation/fragmentation.go +++ b/tcpip/network/fragmentation/fragmentation.go @@ -67,11 +67,25 @@ func (f *Fragmentation) Process(id uint32, first, last uint16, more bool, vv buf } f.mu.Unlock() - //res, done, consumed := r.process(first, last, more, vv) - log.Printf("[%d]的分片 [%d,%d) 合并中\n", id, first, last) - r.process(first, last, more, vv) + res, done, consumed := r.process(first, last, more, vv) - return buffer.VectorisedView{}, false + f.mu.Lock() + f.size += consumed + log.Printf("[%d]的分片 [%d,%d] 合并中\n", id, first, last) + if done { + f.release(r) + } + // Evict reassemblers if we are consuming more memory than highLimit until + // we reach lowLimit. + if f.size > f.highLimit { + tail := f.rList.Back() + for f.size > f.lowLimit && tail != nil { + f.release(tail) + tail = tail.Prev() + } + } + f.mu.Unlock() + return res, done } func (f *Fragmentation) release(r *reassembler) { diff --git a/tcpip/network/fragmentation/fragmentation_test.go b/tcpip/network/fragmentation/fragmentation_test.go index 655b4ae..70634dc 100644 --- a/tcpip/network/fragmentation/fragmentation_test.go +++ b/tcpip/network/fragmentation/fragmentation_test.go @@ -162,8 +162,10 @@ func TestFragmentationBase(t *testing.T) { ip.SetChecksum(^ip.CalculateChecksum()) copy(v, ip) copy(v[header.IPv4MinimumSize:], payload.First()) - log.Println(ip.FragmentOffset()) inject(stackAddr1) + msg := <-c.linkEP.C + log.Println(msg.Header) + } diff --git a/tcpip/network/fragmentation/reassembler.go b/tcpip/network/fragmentation/reassembler.go index b59f4dc..2b66f78 100644 --- a/tcpip/network/fragmentation/reassembler.go +++ b/tcpip/network/fragmentation/reassembler.go @@ -36,7 +36,7 @@ type reassembler struct { id uint32 size int mu sync.Mutex - holes []hole + holes []hole // 每个临时ip报文的缓冲区 最大是65535 deleted int heap fragHeap // 小根堆用来自动排序 done bool @@ -68,7 +68,7 @@ func (r *reassembler) updateHoles(first, last uint16, more bool) bool { } used = true r.deleted++ - r.holes[i].deleted = true + r.holes[i].deleted = true // 当前位置被占用 if first > r.holes[i].first { r.holes = append(r.holes, hole{r.holes[i].first, first - 1, false}) } diff --git a/tcpip/network/ipv4/ipv4.go b/tcpip/network/ipv4/ipv4.go index 31ce754..9ab9df6 100644 --- a/tcpip/network/ipv4/ipv4.go +++ b/tcpip/network/ipv4/ipv4.go @@ -110,7 +110,8 @@ func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable, payload b if protocol == header.ICMPv4ProtocolNumber { log.Printf("IP 写回ICMP报文 长度: %d\n", hdr.UsedLength()+payload.Size()) } else { - log.Printf("send ipv4 packet %d bytes, proto: 0x%x", hdr.UsedLength()+payload.Size(), protocol) + //log.Printf("send ipv4 packet %d bytes, proto: 0x%x", hdr.UsedLength()+payload.Size(), protocol) + log.Println(header.IPv4(append(ip, payload.ToView()...))) } return e.linkEP.WritePacket(r, hdr, payload, ProtocolNumber) } diff --git a/tcpip/stack/nic.go b/tcpip/stack/nic.go index 6d0319c..58ab3be 100644 --- a/tcpip/stack/nic.go +++ b/tcpip/stack/nic.go @@ -388,6 +388,8 @@ func (n *NIC) DeliverNetworkPacket(linkEP LinkEndpoint, remoteLinkAddr, localLin // transport protocol endpoint. func (n *NIC) DeliverTransportPacket(r *Route, protocol tcpip.TransportProtocolNumber, vv buffer.VectorisedView) { log.Println("准备分发传输层数据报") + hdr := buffer.NewPrependable(header.EthernetMinimumSize + header.IPv4MinimumSize) + r.ref.ep.WritePacket(r, hdr, vv, protocol, 255) }