arcbox_virtio_vsock/
addr.rs1use std::os::unix::io::RawFd;
4
5pub const HOST_CID: u64 = 2;
7
8pub const RESERVED_CID: u64 = 1;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub struct VsockAddr {
14 pub cid: u64,
16 pub port: u32,
18}
19
20impl VsockAddr {
21 #[must_use]
23 pub const fn new(cid: u64, port: u32) -> Self {
24 Self { cid, port }
25 }
26
27 #[must_use]
29 pub const fn host(port: u32) -> Self {
30 Self::new(HOST_CID, port)
31 }
32}
33
34pub trait VsockHostConnections: Send + Sync {
40 fn fd_for(&self, guest_port: u32, host_port: u32) -> Option<RawFd>;
42
43 fn mark_connected(&mut self, guest_port: u32, host_port: u32);
45
46 fn remove_connection(&mut self, guest_port: u32, host_port: u32);
48
49 fn update_peer_credit(
52 &mut self,
53 _guest_port: u32,
54 _host_port: u32,
55 _buf_alloc: u32,
56 _fwd_cnt: u32,
57 ) {
58 }
59
60 fn advance_fwd_cnt(&mut self, _guest_port: u32, _host_port: u32, _bytes: u32) -> bool {
63 false
64 }
65
66 fn enqueue_credit_update(&mut self, _guest_port: u32, _host_port: u32) {}
68
69 fn handle_shutdown(&mut self, guest_port: u32, host_port: u32, _flags: u32) {
79 self.remove_connection(guest_port, host_port);
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_vsock_addr_new() {
89 let addr = VsockAddr::new(3, 1234);
90 assert_eq!(addr.cid, 3);
91 assert_eq!(addr.port, 1234);
92 }
93
94 #[test]
95 fn test_vsock_addr_host() {
96 let addr = VsockAddr::host(8080);
97 assert_eq!(addr.cid, HOST_CID);
98 assert_eq!(addr.cid, 2);
99 assert_eq!(addr.port, 8080);
100 }
101
102 #[test]
103 #[allow(clippy::clone_on_copy)]
104 fn test_vsock_addr_clone_copy() {
105 let addr = VsockAddr::new(10, 5000);
106 let cloned = addr.clone();
107 let copied = addr;
108
109 assert_eq!(cloned.cid, 10);
110 assert_eq!(copied.port, 5000);
111 }
112
113 #[test]
114 fn test_vsock_addr_eq() {
115 let addr1 = VsockAddr::new(3, 1234);
116 let addr2 = VsockAddr::new(3, 1234);
117 let addr3 = VsockAddr::new(3, 5678);
118
119 assert_eq!(addr1, addr2);
120 assert_ne!(addr1, addr3);
121 }
122
123 #[test]
124 fn test_vsock_addr_hash() {
125 use std::collections::HashSet;
126
127 let mut set = HashSet::new();
128 set.insert(VsockAddr::new(3, 1234));
129 set.insert(VsockAddr::new(3, 1234)); set.insert(VsockAddr::new(4, 1234));
131
132 assert_eq!(set.len(), 2);
133 }
134
135 #[test]
136 fn test_vsock_constants() {
137 assert_eq!(HOST_CID, 2);
138 assert_eq!(RESERVED_CID, 1);
139 }
140}