-
Notifications
You must be signed in to change notification settings - Fork 612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix packet forwarding between vz and socket_vmnet #2680
Merged
+245
−53
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
//go:build darwin && !no_vz | ||
|
||
package vz | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"net" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
const vmnetMaxPacketSize = 1514 | ||
const packetsCount = 1000 | ||
|
||
func TestDialQemu(t *testing.T) { | ||
listener, err := listenUnix(t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer listener.Close() | ||
t.Logf("Listening at %q", listener.Addr()) | ||
|
||
errc := make(chan error, 2) | ||
|
||
// Start the fake vmnet server. | ||
go func() { | ||
t.Log("Fake vmnet started") | ||
errc <- serveOneClient(listener) | ||
t.Log("Fake vmnet finished") | ||
}() | ||
|
||
// Connect to the fake vmnet server. | ||
client, err := DialQemu(listener.Addr().String()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log("Connected to fake vment server") | ||
|
||
dgramConn, err := net.FileConn(client) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
vzConn := packetConn{Conn: dgramConn} | ||
defer vzConn.Close() | ||
|
||
go func() { | ||
t.Log("Sender started") | ||
buf := make([]byte, vmnetMaxPacketSize) | ||
for i := 0; i < vmnetMaxPacketSize; i++ { | ||
buf[i] = 0x55 | ||
} | ||
|
||
// data packet format: | ||
// 0-4 packet number | ||
// 4-1514 0x55 ... | ||
for i := 0; i < packetsCount; i++ { | ||
binary.BigEndian.PutUint32(buf, uint32(i)) | ||
if _, err := vzConn.Write(buf); err != nil { | ||
errc <- err | ||
return | ||
} | ||
} | ||
t.Logf("Sent %d data packets", packetsCount) | ||
|
||
// quit packet format: | ||
// 0-4: "quit" | ||
copy(buf[:4], []byte("quit")) | ||
if _, err := vzConn.Write(buf[:4]); err != nil { | ||
errc <- err | ||
return | ||
} | ||
|
||
errc <- nil | ||
t.Log("Sender finished") | ||
}() | ||
|
||
// Read and verify packets to the server. | ||
|
||
buf := make([]byte, vmnetMaxPacketSize) | ||
|
||
t.Logf("Receiving and verifying data packets...") | ||
for i := 0; i < packetsCount; i++ { | ||
n, err := vzConn.Read(buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n < vmnetMaxPacketSize { | ||
t.Fatalf("Expected %d bytes, got %d", vmnetMaxPacketSize, n) | ||
} | ||
|
||
number := binary.BigEndian.Uint32(buf[:4]) | ||
if number != uint32(i) { | ||
t.Fatalf("Expected packet %d, got packet %d", i, number) | ||
} | ||
|
||
for j := 4; j < vmnetMaxPacketSize; j++ { | ||
if buf[j] != 0x55 { | ||
t.Fatalf("Expected byte 0x55 at offset %d, got 0x%02x", j, buf[j]) | ||
} | ||
} | ||
} | ||
t.Logf("Recived and verified %d data packets", packetsCount) | ||
|
||
for i := 0; i < 2; i++ { | ||
err := <-errc | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
// serveOneClient accepts one client and echo back received packets until a | ||
// "quit" packet is sent. | ||
func serveOneClient(listener *net.UnixListener) error { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
return err | ||
} | ||
qemuConn := qemuPacketConn{Conn: conn} | ||
defer qemuConn.Close() | ||
|
||
buf := make([]byte, vmnetMaxPacketSize) | ||
for { | ||
nr, err := qemuConn.Read(buf) | ||
if err != nil { | ||
return err | ||
} | ||
if string(buf[:4]) == "quit" { | ||
return nil | ||
} | ||
nw, err := qemuConn.Write(buf[:nr]) | ||
if err != nil { | ||
return err | ||
} | ||
if nw != nr { | ||
return fmt.Errorf("incomplete write: expected: %d, wrote: %d", nr, nw) | ||
} | ||
} | ||
} | ||
|
||
// listenUnix creates and listen to unix socket under dir | ||
func listenUnix(dir string) (*net.UnixListener, error) { | ||
sock := filepath.Join(dir, "sock") | ||
addr, err := net.ResolveUnixAddr("unix", sock) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return net.ListenUnix("unix", addr) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need our custom forwardPackets ??
I would still prefer to use inetproxy itself. Unless it doesn't work even after wrapping
We could simply wrap the packetConn with the fileconn. This way retry on ENOBUFS are present.
https://github.com/lima-vm/lima/blob/master/pkg/vz/network_darwin.go#L49
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for 2 reasons:
Can you explain why?
Looking at https://github.com/inetaf/tcpproxy:
We don't do any of that. We used a tiny bit of tcproxy copying bytes around, and this is better done in lima itself, where we can implement it in the best way for lima, and change it easily when needed.
Adding another layer of wrapping to keep unneeded dependency does not sounds like the right way to me.