好难 好难 好难 ListenLoop的Sleeper为什么没有被唤醒

This commit is contained in:
impact-eintr
2022-12-07 21:28:51 +08:00
parent fa28cc3010
commit bea1fd210b
6 changed files with 22 additions and 29 deletions

View File

@@ -1,17 +0,0 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}

View File

@@ -233,7 +233,9 @@ func (s *Sleeper) Fetch(block bool) (id int, ok bool) {
// Reassociate the waker with the sleeper. If the waker was // Reassociate the waker with the sleeper. If the waker was
// still asserted we can return it, otherwise try the next one. // still asserted we can return it, otherwise try the next one.
old := (*Sleeper)(atomic.SwapPointer(&w.s, usleeper(s))) old := (*Sleeper)(atomic.SwapPointer(&w.s, usleeper(s)))
log.Println("Sleeper", unsafe.Pointer(s), "old", unsafe.Pointer(old), "&assertSleeper", unsafe.Pointer(&assertedSleeper), "w.id", w.id)
if old == &assertedSleeper { if old == &assertedSleeper {
log.Println("成功返回 没有阻塞")
return w.id, true return w.id, true
} }
} }

View File

@@ -23,7 +23,7 @@ func (p Prependable) UsedLength() int {
return len(p.buf) - p.usedIdx return len(p.buf) - p.usedIdx
} }
// 从内到外暴露报文头的协议 eth|ipv4|tcp // Prepend 向前扩展size个字节
func (p *Prependable) Prepend(size int) []byte { func (p *Prependable) Prepend(size int) []byte {
if size > p.usedIdx { if size > p.usedIdx {
return nil return nil

View File

@@ -110,8 +110,11 @@ func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable, payload b
if protocol == header.ICMPv4ProtocolNumber { if protocol == header.ICMPv4ProtocolNumber {
log.Println("IP 写回ICMP报文", header.IPv4(append(ip, payload.ToView()...))) log.Println("IP 写回ICMP报文", header.IPv4(append(ip, payload.ToView()...)))
} else { } else {
//log.Printf("send ipv4 packet %d bytes, proto: 0x%x", hdr.UsedLength()+payload.Size(), protocol) if payload.Size() == 0 {
log.Println(header.IPv4(append(ip, payload.ToView()...))) log.Printf("send ipv4 packet %d bytes, proto: 0x%x", hdr.UsedLength()+payload.Size(), protocol)
} else {
log.Println(header.IPv4(append(ip, payload.ToView()...)))
}
} }
return e.linkEP.WritePacket(r, hdr, payload, ProtocolNumber) return e.linkEP.WritePacket(r, hdr, payload, ProtocolNumber)
} }

View File

@@ -263,6 +263,7 @@ func (e *endpoint) handleSynSegment(ctx *listenContext, s *segment, opts *header
// handleListenSegment is called when a listening endpoint receives a segment // handleListenSegment is called when a listening endpoint receives a segment
// and needs to handle it. // and needs to handle it.
func (e *endpoint) handleListenSegment(ctx *listenContext, s *segment) { func (e *endpoint) handleListenSegment(ctx *listenContext, s *segment) {
log.Println(s.flags)
switch s.flags { switch s.flags {
case flagSyn: // syn报文处理 case flagSyn: // syn报文处理
// 分析tcp选项 // 分析tcp选项
@@ -276,6 +277,8 @@ func (e *endpoint) handleListenSegment(ctx *listenContext, s *segment) {
// 返回一个syn+ack报文 // 返回一个syn+ack报文
case flagFin: // fin报文处理 case flagFin: // fin报文处理
// 三次握手最后一次 ack 报文 // 三次握手最后一次 ack 报文
default:
panic(nil)
} }
} }
@@ -306,6 +309,7 @@ func (e *endpoint) protocolListenLoop(rcvWnd seqnum.Size) *tcpip.Error {
for { for {
switch index, _ := s.Fetch(true); index { // Fetch(true) 阻塞获取 switch index, _ := s.Fetch(true); index { // Fetch(true) 阻塞获取
case wakerForNewSegment: case wakerForNewSegment:
log.Println("新数据来咯 欸嘿嘿嘿")
mayRequeue := true mayRequeue := true
// 接收和处理tcp报文 // 接收和处理tcp报文
for i := 0; i < maxSegmentsPerWake; i++ { for i := 0; i < maxSegmentsPerWake; i++ {
@@ -325,6 +329,8 @@ func (e *endpoint) protocolListenLoop(rcvWnd seqnum.Size) *tcpip.Error {
case wakerForNotification: case wakerForNotification:
// TODO 触发其他事件 // TODO 触发其他事件
log.Println("其他事件?") log.Println("其他事件?")
default:
panic((nil))
} }
} }
} }

View File

@@ -205,14 +205,14 @@ func (h *handshake) execute() *tcpip.Error {
// 如果是客户端当发送 syn 报文,超过一定的时间未收到回包,触发超时重传 // 如果是客户端当发送 syn 报文,超过一定的时间未收到回包,触发超时重传
// 如果是服务端当发送 syn+ack 报文,超过一定的时间未收到 ack 回包,触发超时重传 // 如果是服务端当发送 syn+ack 报文,超过一定的时间未收到 ack 回包,触发超时重传
// 超时时间变为上次的2倍 // 超时时间变为上次的2倍
timeOut *= 2 //timeOut *= 2
if timeOut > 60*time.Second { //if timeOut > 60*time.Second {
return tcpip.ErrTimeout // return tcpip.ErrTimeout
} //}
rt.Reset(timeOut) //rt.Reset(timeOut)
// 重新发送syn报文 //// 重新发送syn报文
sendSynTCP(&h.ep.route, h.ep.id, h.flags, h.iss, h.ackNum, h.rcvWnd, synOpts) //sendSynTCP(&h.ep.route, h.ep.id, h.flags, h.iss, h.ackNum, h.rcvWnd, synOpts)
log.Println("超时重发了 xdm")
case wakerForNotification: case wakerForNotification:
case wakerForNewSegment: case wakerForNewSegment:
@@ -308,7 +308,6 @@ func sendSynTCP(r *stack.Route, id stack.TransportEndpointID, flags byte,
// 发送一个tcp段数据封装 tcp 首部,并写入网路层 // 发送一个tcp段数据封装 tcp 首部,并写入网路层
func sendTCP(r *stack.Route, id stack.TransportEndpointID, data buffer.VectorisedView, ttl uint8, flags byte, func sendTCP(r *stack.Route, id stack.TransportEndpointID, data buffer.VectorisedView, ttl uint8, flags byte,
seq, ack seqnum.Value, rcvWnd seqnum.Size, opts []byte) *tcpip.Error { seq, ack seqnum.Value, rcvWnd seqnum.Size, opts []byte) *tcpip.Error {
log.Println("进行一个报文的发送")
optLen := len(opts) optLen := len(opts)
// Allocate a buffer for the TCP header. // Allocate a buffer for the TCP header.
hdr := buffer.NewPrependable(header.TCPMinimumSize + int(r.MaxHeaderLength()) + optLen) hdr := buffer.NewPrependable(header.TCPMinimumSize + int(r.MaxHeaderLength()) + optLen)