Skip to content

Commit 5c761d9

Browse files
committed
vsock/tsi_dgram: Make send_to bind to correct socket adress structs
We need to bind to the correct socket types (IPv6, Unix) instead of only IPv4. This fixes UDP and unix dgram tests hanging when waiting for reply. Signed-off-by: Matej Hrica <[email protected]>
1 parent 9315147 commit 5c761d9

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/devices/src/virtio/vsock/tsi_dgram.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::HashMap;
2-
use std::net::{Ipv4Addr, SocketAddrV4};
2+
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
33
use std::num::Wrapping;
44
use std::os::fd::OwnedFd;
55
use std::os::unix::io::{AsRawFd, RawFd};
@@ -8,7 +8,7 @@ use std::sync::{Arc, Mutex};
88
use nix::fcntl::{fcntl, FcntlArg, OFlag};
99
use nix::sys::socket::{
1010
bind, connect, getpeername, recv, send, sendto, socket, AddressFamily, MsgFlags, SockFlag,
11-
SockType, SockaddrIn, SockaddrLike, SockaddrStorage,
11+
SockType, SockaddrIn, SockaddrLike, SockaddrStorage, UnixAddr,
1212
};
1313

1414
#[cfg(target_os = "macos")]
@@ -35,6 +35,7 @@ pub struct TsiDgramProxy {
3535
pub status: ProxyStatus,
3636
sendto_addr: Option<SockaddrStorage>,
3737
listening: bool,
38+
family: AddressFamily,
3839
mem: GuestMemoryMmap,
3940
queue: Arc<Mutex<VirtQueue>>,
4041
rxq: Arc<Mutex<MuxerRxQ>>,
@@ -102,6 +103,7 @@ impl TsiDgramProxy {
102103
status: ProxyStatus::Idle,
103104
sendto_addr: None,
104105
listening: false,
106+
family,
105107
mem,
106108
queue,
107109
rxq,
@@ -339,7 +341,25 @@ impl Proxy for TsiDgramProxy {
339341

340342
self.sendto_addr = Some(req.addr);
341343
if !self.listening {
342-
match bind(self.fd.as_raw_fd(), &SockaddrIn::new(0, 0, 0, 0, 0)) {
344+
let bind_result = match self.family {
345+
AddressFamily::Inet => bind(self.fd.as_raw_fd(), &SockaddrIn::new(0, 0, 0, 0, 0)),
346+
AddressFamily::Inet6 => {
347+
let addr6: SockaddrStorage =
348+
SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0).into();
349+
bind(self.fd.as_raw_fd(), &addr6)
350+
}
351+
#[cfg(target_os = "linux")]
352+
AddressFamily::Unix => {
353+
let addr = UnixAddr::new_unnamed();
354+
bind(self.fd.as_raw_fd(), &addr)
355+
}
356+
_ => {
357+
warn!("sendto_addr: unsupported address family: {:?}", self.family);
358+
return update;
359+
}
360+
};
361+
362+
match bind_result {
343363
Ok(_) => {
344364
self.listening = true;
345365
update.polling = Some((self.id, self.fd.as_raw_fd(), EventSet::IN));

0 commit comments

Comments
 (0)