From 721c146726087bec5f7e2c04130f989c6fbf1bd3 Mon Sep 17 00:00:00 2001 From: kskb Date: Tue, 3 Aug 2021 12:26:05 +0800 Subject: [PATCH] fix bug: read all buf --- README.md | 2 ++ tun/tun_linux_vpp.go | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bcd2d68..15b1faa 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,8 @@ To run with more logging you may set the environment variable `LOG_LEVEL=debug`. Remember to replace the path to real path ``` +export LDP_DEBUG=0 +export VCL_DEBUG=0 export VCL_VPP_API_SOCKET="/run/vpp/api.sock" export LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libvcl_ldpreload.so" ``` diff --git a/tun/tun_linux_vpp.go b/tun/tun_linux_vpp.go index d597710..4a77d02 100644 --- a/tun/tun_linux_vpp.go +++ b/tun/tun_linux_vpp.go @@ -138,6 +138,7 @@ type NativeTun struct { secret string RxQueues int RxintCh <-chan uint8 + RxintChNext chan uint8 RxintErrCh <-chan error TxQueues int TxCount uint @@ -332,6 +333,16 @@ func (tun *NativeTun) Read(buf []byte, offset int) (n int, err error) { tun.logger.Errorf("tun error: %v\n", err) return 0, err case queueID := <-tun.RxintCh: + select { + case tun.RxintChNext <- queueID: + { + // Use non-blocking write to prevent program stuck + } + default: + tun.logger.Debugln("Buffer full") + } + + case queueID := <-tun.RxintChNext: packets, err := tun.memif.RxBurst(queueID, 1) if err != nil { tun.logger.Errorf("libmemif.Memif.RxBurst() error: %v\n", err) @@ -339,9 +350,18 @@ func (tun *NativeTun) Read(buf []byte, offset int) (n int, err error) { } if len(packets) == 0 { // No more packets to read until the next interrupt. - break + return 0, nil } for _, packetData := range packets { + select { + case tun.RxintChNext <- queueID: + { + // Use non-blocking write to prevent program stuck + // repeatedly call RxBurst() until returns an empty slice of packets + } + default: + tun.logger.Debugln("Buffer full") + } //check if dst mac addr is a boardcast mac destMac := packetData[0:6] @@ -1284,6 +1304,7 @@ func CreateTUN(name string, mtu int) (Device, error) { VppBridgeID: ifConfig.VppBridgeID, tempMTU: 9000, logger: thelogger, + RxintChNext: make(chan uint8, 1<<6), events: make(chan Event, 5), errors: make(chan error, 5), statusListenersShutdown: make(chan struct{}),