Browse Source

Add VsockEventType::ConnectionRequest.

Andrew Walbran 1 year ago
parent
commit
0573c13b5c

+ 3 - 0
src/device/socket/multiconnectionmanager.rs

@@ -96,6 +96,9 @@ impl<H: Hal, T: Transport> VsockConnectionManager<H, T> {
             connection.info.update_for_event(&event);
             connection.info.update_for_event(&event);
 
 
             match event.event_type {
             match event.event_type {
+                VsockEventType::ConnectionRequest => {
+                    // TODO: Send Rst or handle incoming connections.
+                }
                 VsockEventType::Connected => {}
                 VsockEventType::Connected => {}
                 VsockEventType::Disconnected { .. } => {
                 VsockEventType::Disconnected { .. } => {
                     // TODO: Wait until client reads all data before removing connection.
                     // TODO: Wait until client reads all data before removing connection.

+ 6 - 1
src/device/socket/singleconnectionmanager.rs

@@ -108,6 +108,9 @@ impl<H: Hal, T: Transport> SingleConnectionManager<H, T> {
             connection_info.update_for_event(&event);
             connection_info.update_for_event(&event);
 
 
             match event.event_type {
             match event.event_type {
+                VsockEventType::ConnectionRequest => {
+                    // TODO: Send Rst or handle incoming connections.
+                }
                 VsockEventType::Connected => {}
                 VsockEventType::Connected => {}
                 VsockEventType::Disconnected { .. } => {
                 VsockEventType::Disconnected { .. } => {
                     *self_connection_info = None;
                     *self_connection_info = None;
@@ -167,7 +170,9 @@ impl<H: Hal, T: Transport> SingleConnectionManager<H, T> {
                     return Err(SocketError::ConnectionFailed.into())
                     return Err(SocketError::ConnectionFailed.into())
                 }
                 }
                 VsockEventType::Received { .. } => return Err(SocketError::InvalidOperation.into()),
                 VsockEventType::Received { .. } => return Err(SocketError::InvalidOperation.into()),
-                VsockEventType::CreditRequest | VsockEventType::CreditUpdate => {}
+                VsockEventType::ConnectionRequest
+                | VsockEventType::CreditRequest
+                | VsockEventType::CreditUpdate => {}
             }
             }
         }
         }
     }
     }

+ 22 - 44
src/device/socket/vsock.rs

@@ -113,7 +113,7 @@ impl VsockEvent {
             && self.destination.port == connection_info.src_port
             && self.destination.port == connection_info.src_port
     }
     }
 
 
-    fn from_header(header: &VirtioVsockHdr) -> Result<Option<Self>> {
+    fn from_header(header: &VirtioVsockHdr) -> Result<Self> {
         let op = header.op()?;
         let op = header.op()?;
         let buffer_status = VsockBufferStatus {
         let buffer_status = VsockBufferStatus {
             buffer_allocation: header.buf_alloc.into(),
             buffer_allocation: header.buf_alloc.into(),
@@ -122,66 +122,45 @@ impl VsockEvent {
         let source = header.source();
         let source = header.source();
         let destination = header.destination();
         let destination = header.destination();
 
 
-        match op {
+        let event_type = match op {
             VirtioVsockOp::Request => {
             VirtioVsockOp::Request => {
                 header.check_data_is_empty()?;
                 header.check_data_is_empty()?;
-                // TODO: Send a Rst, or support listening.
-                Ok(None)
+                VsockEventType::ConnectionRequest
             }
             }
             VirtioVsockOp::Response => {
             VirtioVsockOp::Response => {
                 header.check_data_is_empty()?;
                 header.check_data_is_empty()?;
-                Ok(Some(VsockEvent {
-                    source,
-                    destination,
-                    buffer_status,
-                    event_type: VsockEventType::Connected,
-                }))
+                VsockEventType::Connected
             }
             }
             VirtioVsockOp::CreditUpdate => {
             VirtioVsockOp::CreditUpdate => {
                 header.check_data_is_empty()?;
                 header.check_data_is_empty()?;
-                Ok(Some(VsockEvent {
-                    source,
-                    destination,
-                    buffer_status,
-                    event_type: VsockEventType::CreditUpdate,
-                }))
+                VsockEventType::CreditUpdate
             }
             }
             VirtioVsockOp::Rst | VirtioVsockOp::Shutdown => {
             VirtioVsockOp::Rst | VirtioVsockOp::Shutdown => {
                 header.check_data_is_empty()?;
                 header.check_data_is_empty()?;
-
                 info!("Disconnected from the peer");
                 info!("Disconnected from the peer");
-
                 let reason = if op == VirtioVsockOp::Rst {
                 let reason = if op == VirtioVsockOp::Rst {
                     DisconnectReason::Reset
                     DisconnectReason::Reset
                 } else {
                 } else {
                     DisconnectReason::Shutdown
                     DisconnectReason::Shutdown
                 };
                 };
-                Ok(Some(VsockEvent {
-                    source,
-                    destination,
-                    buffer_status,
-                    event_type: VsockEventType::Disconnected { reason },
-                }))
+                VsockEventType::Disconnected { reason }
             }
             }
-            VirtioVsockOp::Rw => Ok(Some(VsockEvent {
-                source,
-                destination,
-                buffer_status,
-                event_type: VsockEventType::Received {
-                    length: header.len() as usize,
-                },
-            })),
+            VirtioVsockOp::Rw => VsockEventType::Received {
+                length: header.len() as usize,
+            },
             VirtioVsockOp::CreditRequest => {
             VirtioVsockOp::CreditRequest => {
                 header.check_data_is_empty()?;
                 header.check_data_is_empty()?;
-                Ok(Some(VsockEvent {
-                    source,
-                    destination,
-                    buffer_status,
-                    event_type: VsockEventType::CreditRequest,
-                }))
+                VsockEventType::CreditRequest
             }
             }
-            VirtioVsockOp::Invalid => Err(SocketError::InvalidOperation.into()),
-        }
+            VirtioVsockOp::Invalid => return Err(SocketError::InvalidOperation.into()),
+        };
+
+        Ok(VsockEvent {
+            source,
+            destination,
+            buffer_status,
+            event_type,
+        })
     }
     }
 }
 }
 
 
@@ -204,6 +183,8 @@ pub enum DisconnectReason {
 /// Details of the type of an event received from a VirtIO socket.
 /// Details of the type of an event received from a VirtIO socket.
 #[derive(Clone, Debug, Eq, PartialEq)]
 #[derive(Clone, Debug, Eq, PartialEq)]
 pub enum VsockEventType {
 pub enum VsockEventType {
+    /// The peer requests to establish a connection with us.
+    ConnectionRequest,
     /// The connection was successfully established.
     /// The connection was successfully established.
     Connected,
     Connected,
     /// The connection was closed.
     /// The connection was closed.
@@ -384,10 +365,7 @@ impl<H: Hal, T: Transport> VirtIOSocket<H, T> {
             return Ok(None);
             return Ok(None);
         };
         };
 
 
-        let result = match VsockEvent::from_header(&header) {
-            Ok(Some(event)) => handler(event, body),
-            other => other,
-        };
+        let result = VsockEvent::from_header(&header).and_then(|event| handler(event, body));
 
 
         unsafe {
         unsafe {
             // TODO: What about if both handler and this give errors?
             // TODO: What about if both handler and this give errors?