Skip to content

Commit 16848c4

Browse files
committed
gvtap: enhance error handling and refactor tap device initialization
Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>
1 parent f5e4b9d commit 16848c4

1 file changed

Lines changed: 48 additions & 23 deletions

File tree

pkg/network/gvtap/gvtap.go

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"net"
99
"os"
1010
"path/filepath"
11+
"strings"
1112
"sync"
1213

14+
"github.com/containernetworking/plugins/pkg/ns"
1315
"github.com/containers/gvisor-tap-vsock/pkg/tap"
1416
"github.com/containers/gvisor-tap-vsock/pkg/types"
1517
"github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork"
@@ -251,6 +253,11 @@ func (d *parentDriver) acceptConnections() {
251253
default:
252254
conn, err := d.listener.Accept()
253255
if err != nil {
256+
// Check if the error is due to the listener being closed, which is expected during cleanup
257+
if errors.Is(err, io.EOF) || strings.Contains(err.Error(), "use of closed network connection") {
258+
logrus.Debugf("listener closed, stopping accept loop")
259+
return
260+
}
254261
logrus.Errorf("accepting connection: %v", err)
255262
continue
256263
}
@@ -279,7 +286,12 @@ func (d *parentDriver) handleConnection(conn net.Conn) {
279286
// This will handle the connection and forward packets in both directions
280287
logrus.Debugf("forwarding packets between Unix socket and virtual network using VfkitProtocol")
281288
if err := vn.AcceptVfkit(d.ctx, conn); err != nil {
282-
logrus.Errorf("accepting connection with VfkitProtocol: %v", err)
289+
if errors.Is(err, io.EOF) {
290+
// This is expected when the child process exits
291+
logrus.Debugf("child process exited, connection closed: %v", err)
292+
} else {
293+
logrus.Errorf("accepting connection with VfkitProtocol: %v", err)
294+
}
283295
}
284296
}
285297

@@ -388,35 +400,48 @@ func (d *childDriver) ConfigureNetworkChild(netmsg *messages.ParentInitNetworkDr
388400
return "", errors.New("could not determine the preconfigured tap")
389401
}
390402

391-
// Open the tap device
392-
var err error
393-
d.tap, err = water.New(water.Config{
394-
DeviceType: water.TAP,
395-
PlatformSpecificParams: water.PlatformSpecificParams{
396-
Name: tapName,
397-
},
398-
})
399-
if err != nil {
400-
return "", fmt.Errorf("opening tap device %s: %w", tapName, err)
401-
}
402-
403403
// Get the socket path from the network driver opaque
404404
d.socketPath = netmsg.NetworkDriverOpaque["socketPath"]
405405
if d.socketPath == "" {
406406
return "", errors.New("socket path not provided")
407407
}
408408

409-
// Connect to the Unix socket
410-
var conn net.Conn
411-
conn, err = net.Dial("unix", d.socketPath)
412-
if err != nil {
413-
return "", fmt.Errorf("connecting to socket %s: %w", d.socketPath, err)
414-
}
415-
d.conn = conn
409+
fn := func(_ ns.NetNS) error {
410+
// Open the tap device
411+
var err error
412+
d.tap, err = water.New(water.Config{
413+
DeviceType: water.TAP,
414+
PlatformSpecificParams: water.PlatformSpecificParams{
415+
Name: tapName,
416+
},
417+
})
418+
if err != nil {
419+
return fmt.Errorf("opening tap device %s: %w", tapName, err)
420+
}
421+
422+
// Connect to the Unix socket
423+
var conn net.Conn
424+
conn, err = net.Dial("unix", d.socketPath)
425+
if err != nil {
426+
return fmt.Errorf("connecting to socket %s: %w", d.socketPath, err)
427+
}
428+
d.conn = conn
429+
430+
// Start forwarding packets
431+
go d.forwardTapToSocket()
432+
go d.forwardSocketToTap()
416433

417-
// Start forwarding packets
418-
go d.forwardTapToSocket()
419-
go d.forwardSocketToTap()
434+
return nil
435+
}
436+
if detachedNetNSPath == "" {
437+
if err := fn(nil); err != nil {
438+
return "", err
439+
}
440+
} else {
441+
if err := ns.WithNetNSPath(detachedNetNSPath, fn); err != nil {
442+
return "", err
443+
}
444+
}
420445

421446
// tap is created, opened, and "up".
422447
// IP stuff and MTU are not configured by the parent here,

0 commit comments

Comments
 (0)