Skip to content

Commit ab519a8

Browse files
slpjakecorrenti
authored andcommitted
vsock: delay listening for events in TCP socket
As indicated in 3bd3b98, when a client in the guest attempts to send data to the server immediately after connecting to it, we might receive data in the TCP socket before we're able to actually process it. To avoid this situation, delay setting EventSet::IN until the vsock transport has been established in confirm_connect. Signed-off-by: Sergio Lopez <[email protected]>
1 parent 1ac1c75 commit ab519a8

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/devices/src/virtio/vsock/muxer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,9 @@ impl VsockMuxer {
498498
let mut proxy_map = self.proxy_map.write().unwrap();
499499

500500
if let Some(proxy) = proxy_map.get(&id) {
501-
proxy.lock().unwrap().confirm_connect(pkt)
501+
if let Some(update) = proxy.lock().unwrap().confirm_connect(pkt) {
502+
self.process_proxy_update(id, update);
503+
}
502504
} else if let Some(ref mut ipc_map) = &mut self.unix_ipc_port_map {
503505
if let Some((path, listen)) = ipc_map.get(&pkt.dst_port()) {
504506
let mem = self.mem.as_ref().unwrap();

src/devices/src/virtio/vsock/proxy.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ pub trait Proxy: Send + AsRawFd {
6969
#[allow(dead_code)]
7070
fn status(&self) -> ProxyStatus;
7171
fn connect(&mut self, pkt: &VsockPacket, req: TsiConnectReq) -> ProxyUpdate;
72-
fn confirm_connect(&mut self, _pkt: &VsockPacket) {}
72+
fn confirm_connect(&mut self, _pkt: &VsockPacket) -> Option<ProxyUpdate> {
73+
None
74+
}
7375
fn getpeername(&mut self, pkt: &VsockPacket);
7476
fn sendmsg(&mut self, pkt: &VsockPacket) -> ProxyUpdate;
7577
fn sendto_addr(&mut self, req: TsiSendtoAddr) -> ProxyUpdate;

src/devices/src/virtio/vsock/tcp.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Proxy for TcpProxy {
393393
};
394394

395395
if self.status == ProxyStatus::Connecting {
396-
update.polling = Some((self.id, self.fd, EventSet::IN | EventSet::OUT));
396+
update.polling = Some((self.id, self.fd, EventSet::OUT));
397397
} else {
398398
if self.status == ProxyStatus::Connected {
399399
update.polling = Some((self.id, self.fd, EventSet::IN));
@@ -404,7 +404,7 @@ impl Proxy for TcpProxy {
404404
update
405405
}
406406

407-
fn confirm_connect(&mut self, pkt: &VsockPacket) {
407+
fn confirm_connect(&mut self, pkt: &VsockPacket) -> Option<ProxyUpdate> {
408408
debug!(
409409
"tcp: confirm_connect: local_port={} peer_port={}, src_port={}, dst_port={}",
410410
pkt.dst_port(),
@@ -425,6 +425,13 @@ impl Proxy for TcpProxy {
425425
peer_port: pkt.src_port(),
426426
};
427427
push_packet(self.cid, rx, &self.rxq, &self.queue, &self.mem);
428+
429+
// Now that the vsock transport is fully established, start listening
430+
// for events in the TCP socket again.
431+
Some(ProxyUpdate {
432+
polling: Some((self.id, self.fd, EventSet::IN)),
433+
..Default::default()
434+
})
428435
}
429436

430437
fn getpeername(&mut self, pkt: &VsockPacket) {
@@ -751,7 +758,9 @@ impl Proxy for TcpProxy {
751758
self.switch_to_connected();
752759
self.push_connect_rsp(0);
753760
update.signal_queue = true;
754-
update.polling = Some((self.id(), self.fd, EventSet::IN));
761+
// Stop listening for events in the TCP socket until we receive
762+
// OP_REQUEST and the vsock transport is fully established.
763+
update.polling = Some((self.id(), self.fd, EventSet::empty()));
755764
} else {
756765
error!("vsock::tcp: EventSet::OUT while not connecting");
757766
}

src/devices/src/virtio/vsock/unix.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl Proxy for UnixProxy {
356356
update
357357
}
358358

359-
fn confirm_connect(&mut self, pkt: &VsockPacket) {
359+
fn confirm_connect(&mut self, pkt: &VsockPacket) -> Option<ProxyUpdate> {
360360
debug!(
361361
"tcp: confirm_connect: local_port={} peer_port={}, src_port={}, dst_port={}",
362362
pkt.dst_port(),
@@ -377,6 +377,8 @@ impl Proxy for UnixProxy {
377377
peer_port: pkt.src_port(),
378378
};
379379
push_packet(self.cid, rx, &self.rxq, &self.queue, &self.mem);
380+
381+
None
380382
}
381383

382384
fn getpeername(&mut self, _pkt: &VsockPacket) {

0 commit comments

Comments
 (0)