Add flow dispatcher

This commit is contained in:
世界 2026-07-06 11:49:18 +08:00
parent 47bdde06c3
commit ed63adda33
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
27 changed files with 2469 additions and 963 deletions

View file

@ -73,8 +73,6 @@ func (m *Mixed) tunLoop() {
return
}
if linuxTUN, isLinuxTUN := m.tun.(LinuxTUN); isLinuxTUN {
m.frontHeadroom = linuxTUN.FrontHeadroom()
m.txChecksumOffload = linuxTUN.TXChecksumOffload()
batchSize := linuxTUN.BatchSize()
if batchSize > 1 {
m.batchLoopLinux(linuxTUN, batchSize)
@ -105,6 +103,7 @@ func (m *Mixed) tunLoop() {
m.logger.Trace(E.Cause(err, "write packet"))
}
}
m.dispatcher.Flush()
}
}
@ -124,6 +123,7 @@ func (m *Mixed) wintunLoop(winTun WinTun) {
m.logger.Trace(E.Cause(err, "write packet"))
}
}
m.dispatcher.Flush()
release()
}
}
@ -164,11 +164,13 @@ func (m *Mixed) batchLoopLinux(linuxTUN LinuxTUN, batchSize int) {
}
writeBuffers = writeBuffers[:0]
}
m.dispatcher.Flush()
}
}
func (m *Mixed) batchLoopDarwin(darwinTUN DarwinTUN) {
var writeBuffers []*buf.Buffer
var releaseBuffers []*buf.Buffer
for {
buffers, err := darwinTUN.BatchRead()
if err != nil {
@ -181,6 +183,7 @@ func (m *Mixed) batchLoopDarwin(darwinTUN DarwinTUN) {
continue
}
writeBuffers = writeBuffers[:0]
releaseBuffers = releaseBuffers[:0]
for _, buffer := range buffers {
packetSize := buffer.Len()
if packetSize < header.IPv4MinimumSize {
@ -190,7 +193,7 @@ func (m *Mixed) batchLoopDarwin(darwinTUN DarwinTUN) {
if m.processPacket(buffer.Bytes()) {
writeBuffers = append(writeBuffers, buffer)
} else {
buffer.Release()
releaseBuffers = append(releaseBuffers, buffer)
}
}
if len(writeBuffers) > 0 {
@ -200,6 +203,8 @@ func (m *Mixed) batchLoopDarwin(darwinTUN DarwinTUN) {
}
buf.ReleaseMulti(writeBuffers)
}
m.dispatcher.Flush()
buf.ReleaseMulti(releaseBuffers)
}
}
@ -229,6 +234,9 @@ func (m *Mixed) processIPv4(ipHdr header.IPv4) (writeBack bool, err error) {
if destination == m.broadcastAddr || !destination.IsGlobalUnicast() {
return
}
if m.dispatchIPv4(ipHdr, destination) {
return false, nil
}
switch ipHdr.TransportProtocol() {
case header.TCPProtocolNumber:
writeBack, err = m.processIPv4TCP(ipHdr, ipHdr.Payload())
@ -249,9 +257,13 @@ func (m *Mixed) processIPv4(ipHdr header.IPv4) (writeBack bool, err error) {
func (m *Mixed) processIPv6(ipHdr header.IPv6) (writeBack bool, err error) {
writeBack = true
if !ipHdr.DestinationAddr().IsGlobalUnicast() {
destination := ipHdr.DestinationAddr()
if !destination.IsGlobalUnicast() {
return
}
if m.dispatchIPv6(ipHdr, destination) {
return false, nil
}
switch ipHdr.TransportProtocol() {
case header.TCPProtocolNumber:
writeBack, err = m.processIPv6TCP(ipHdr, ipHdr.Payload())