@@ -628,148 +628,148 @@ mod tests {
628628
629629 //TODO: fix tests
630630 /*
631- #[test]
632- fn test_vsock_thread_backend_sibling_vms() {
633- const CID: u64 = 3;
634- const SIBLING_CID: u64 = 4;
635- const SIBLING2_CID: u64 = 5;
636- const SIBLING_LISTENING_PORT: u32 = 1234;
637-
638- let test_dir = tempdir().expect("Could not create a temp test directory.");
639-
640- let vsock_socket_path = test_dir.path().join("test_vsock_thread_backend.vsock");
641- let sibling_vhost_socket_path = test_dir
642- .path()
643- .join("test_vsock_thread_backend_sibling.socket");
644- let sibling_vsock_socket_path = test_dir
645- .path()
646- .join("test_vsock_thread_backend_sibling.vsock");
647- let sibling2_vhost_socket_path = test_dir
648- .path()
649- .join("test_vsock_thread_backend_sibling2.socket");
650- let sibling2_vsock_socket_path = test_dir
651- .path()
652- .join("test_vsock_thread_backend_sibling2.vsock");
653-
654- let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));
655-
656- let sibling_config = VsockConfig::new(
657- SIBLING_CID,
658- sibling_vhost_socket_path,
659- BackendType::UnixDomainSocket(sibling_vsock_socket_path),
660- CONN_TX_BUF_SIZE,
661- QUEUE_SIZE,
662- vec!["group1", "group2", "group3"]
663- .into_iter()
664- .map(String::from)
665- .collect(),
666- );
667-
668- let sibling2_config = VsockConfig::new(
669- SIBLING2_CID,
670- sibling2_vhost_socket_path,
671- BackendType::UnixDomainSocket(sibling2_vsock_socket_path),
672- CONN_TX_BUF_SIZE,
673- QUEUE_SIZE,
674- vec!["group1"].into_iter().map(String::from).collect(),
675- );
676-
677- let sibling_backend =
678- Arc::new(VhostUserVsockBackend::new(sibling_config, cid_map.clone()).unwrap());
679- let sibling2_backend =
680- Arc::new(VhostUserVsockBackend::new(sibling2_config, cid_map.clone()).unwrap());
681-
682- let epoll_fd = epoll::create(false).unwrap();
683-
684- let groups_set: HashSet<String> = vec!["groupA", "groupB", "group3"]
685- .into_iter()
686- .map(String::from)
687- .collect();
688-
689- let mut vtp = VsockThreadBackend::new(
690- BackendType::UnixDomainSocket(vsock_socket_path),
691- epoll_fd,
692- CID,
693- CONN_TX_BUF_SIZE,
694- Arc::new(RwLock::new(groups_set)),
695- cid_map,
696- );
697-
698- assert!(!vtp.pending_raw_pkts());
699-
700- let mut pkt_raw = [0u8; PKT_HEADER_SIZE + DATA_LEN];
701- let (hdr_raw, data_raw) = pkt_raw.split_at_mut(PKT_HEADER_SIZE);
702-
703- let (mem, descr_chain) = prepare_desc_chain_vsock(false, PKT_HEADER_SIZE, 1, b"hello");
704- let mem = mem.memory();
705-
706- // SAFETY: Safe as hdr_raw and data_raw are guaranteed to be valid.
707- let mut packet =
708- VsockPacketRx::from_rx_virtq_chain(mem.deref(), descr_chain, CONN_TX_BUF_SIZE)
709- .unwrap();
710- assert_eq!(
711- vtp.recv_raw_pkt(&mut packet).unwrap_err().to_string(),
712- Error::EmptyRawPktsQueue.to_string()
713- );
714-
715-
716- packet.header_mut().set_type(VSOCK_TYPE_STREAM);
717- packet.header_mut().set_src_cid(CID);
718- packet.header_mut().set_dst_cid(SIBLING_CID);
719- packet.header_mut().set_dst_port(SIBLING_LISTENING_PORT);
720- packet.header_mut().set_op(VSOCK_OP_RW);
721- packet.header_mut().set_len(DATA_LEN as u32);
722-
723- //Payload is hello
724- /*packet
725- .data_slice()
726- .unwrap()
727- .copy_from(&[0xCAu8, 0xFEu8, 0xBAu8, 0xBEu8]);
728- */
729-
730- vtp.send_pkt(&mut &packet).unwrap();
731- assert!(sibling_backend.threads[0]
732- .lock()
733- .unwrap()
734- .thread_backend
735- .pending_raw_pkts());
736-
737- packet.header_mut().set_dst_cid(SIBLING2_CID);
738- vtp.send_pkt(&mut &packet).unwrap();
739- // packet should be discarded since sibling2 is not in the same group
740- assert!(!sibling2_backend.threads[0]
741- .lock()
742- .unwrap()
743- .thread_backend
744- .pending_raw_pkts());
745-
746- let mut recvd_pkt_raw = [0u8; PKT_HEADER_SIZE + DATA_LEN];
747- let (recvd_hdr_raw, recvd_data_raw) = recvd_pkt_raw.split_at_mut(PKT_HEADER_SIZE);
748-
749- let mut recvd_packet =
750- // SAFETY: Safe as recvd_hdr_raw and recvd_data_raw are guaranteed to be valid.
751- unsafe { VsockPacket::new(recvd_hdr_raw, Some(recvd_data_raw)).unwrap() };
752-
753- sibling_backend.threads[0]
754- .lock()
755- .unwrap()
756- .thread_backend
757- .recv_raw_pkt(&mut recvd_packet)
758- .unwrap();
759-
760- assert_eq!(recvd_packet.type_(), VSOCK_TYPE_STREAM);
761- assert_eq!(recvd_packet.src_cid(), CID);
762- assert_eq!(recvd_packet.dst_cid(), SIBLING_CID);
763- assert_eq!(recvd_packet.dst_port(), SIBLING_LISTENING_PORT);
764- assert_eq!(recvd_packet.op(), VSOCK_OP_RW);
765- assert_eq!(recvd_packet.len(), DATA_LEN as u32);
766-
767- assert_eq!(recvd_data_raw[0], 0xCAu8);
768- assert_eq!(recvd_data_raw[1], 0xFEu8);
769- assert_eq!(recvd_data_raw[2], 0xBAu8);
770- assert_eq!(recvd_data_raw[3], 0xBEu8);
771-
772- test_dir.close().unwrap();
773- }
774- */
631+ #[test]
632+ fn test_vsock_thread_backend_sibling_vms() {
633+ const CID: u64 = 3;
634+ const SIBLING_CID: u64 = 4;
635+ const SIBLING2_CID: u64 = 5;
636+ const SIBLING_LISTENING_PORT: u32 = 1234;
637+
638+ let test_dir = tempdir().expect("Could not create a temp test directory.");
639+
640+ let vsock_socket_path = test_dir.path().join("test_vsock_thread_backend.vsock");
641+ let sibling_vhost_socket_path = test_dir
642+ .path()
643+ .join("test_vsock_thread_backend_sibling.socket");
644+ let sibling_vsock_socket_path = test_dir
645+ .path()
646+ .join("test_vsock_thread_backend_sibling.vsock");
647+ let sibling2_vhost_socket_path = test_dir
648+ .path()
649+ .join("test_vsock_thread_backend_sibling2.socket");
650+ let sibling2_vsock_socket_path = test_dir
651+ .path()
652+ .join("test_vsock_thread_backend_sibling2.vsock");
653+
654+ let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));
655+
656+ let sibling_config = VsockConfig::new(
657+ SIBLING_CID,
658+ sibling_vhost_socket_path,
659+ BackendType::UnixDomainSocket(sibling_vsock_socket_path),
660+ CONN_TX_BUF_SIZE,
661+ QUEUE_SIZE,
662+ vec!["group1", "group2", "group3"]
663+ .into_iter()
664+ .map(String::from)
665+ .collect(),
666+ );
667+
668+ let sibling2_config = VsockConfig::new(
669+ SIBLING2_CID,
670+ sibling2_vhost_socket_path,
671+ BackendType::UnixDomainSocket(sibling2_vsock_socket_path),
672+ CONN_TX_BUF_SIZE,
673+ QUEUE_SIZE,
674+ vec!["group1"].into_iter().map(String::from).collect(),
675+ );
676+
677+ let sibling_backend =
678+ Arc::new(VhostUserVsockBackend::new(sibling_config, cid_map.clone()).unwrap());
679+ let sibling2_backend =
680+ Arc::new(VhostUserVsockBackend::new(sibling2_config, cid_map.clone()).unwrap());
681+
682+ let epoll_fd = epoll::create(false).unwrap();
683+
684+ let groups_set: HashSet<String> = vec!["groupA", "groupB", "group3"]
685+ .into_iter()
686+ .map(String::from)
687+ .collect();
688+
689+ let mut vtp = VsockThreadBackend::new(
690+ BackendType::UnixDomainSocket(vsock_socket_path),
691+ epoll_fd,
692+ CID,
693+ CONN_TX_BUF_SIZE,
694+ Arc::new(RwLock::new(groups_set)),
695+ cid_map,
696+ );
697+
698+ assert!(!vtp.pending_raw_pkts());
699+
700+ let mut pkt_raw = [0u8; PKT_HEADER_SIZE + DATA_LEN];
701+ let (hdr_raw, data_raw) = pkt_raw.split_at_mut(PKT_HEADER_SIZE);
702+
703+ let (mem, descr_chain) = prepare_desc_chain_vsock(false, PKT_HEADER_SIZE, 1, b"hello");
704+ let mem = mem.memory();
705+
706+ // SAFETY: Safe as hdr_raw and data_raw are guaranteed to be valid.
707+ let mut packet =
708+ VsockPacketRx::from_rx_virtq_chain(mem.deref(), descr_chain, CONN_TX_BUF_SIZE)
709+ .unwrap();
710+ assert_eq!(
711+ vtp.recv_raw_pkt(&mut packet).unwrap_err().to_string(),
712+ Error::EmptyRawPktsQueue.to_string()
713+ );
714+
715+
716+ packet.header_mut().set_type(VSOCK_TYPE_STREAM);
717+ packet.header_mut().set_src_cid(CID);
718+ packet.header_mut().set_dst_cid(SIBLING_CID);
719+ packet.header_mut().set_dst_port(SIBLING_LISTENING_PORT);
720+ packet.header_mut().set_op(VSOCK_OP_RW);
721+ packet.header_mut().set_len(DATA_LEN as u32);
722+
723+ //Payload is hello
724+ /*packet
725+ .data_slice()
726+ .unwrap()
727+ .copy_from(&[0xCAu8, 0xFEu8, 0xBAu8, 0xBEu8]);
728+ */
729+
730+ vtp.send_pkt(&mut &packet).unwrap();
731+ assert!(sibling_backend.threads[0]
732+ .lock()
733+ .unwrap()
734+ .thread_backend
735+ .pending_raw_pkts());
736+
737+ packet.header_mut().set_dst_cid(SIBLING2_CID);
738+ vtp.send_pkt(&mut &packet).unwrap();
739+ // packet should be discarded since sibling2 is not in the same group
740+ assert!(!sibling2_backend.threads[0]
741+ .lock()
742+ .unwrap()
743+ .thread_backend
744+ .pending_raw_pkts());
745+
746+ let mut recvd_pkt_raw = [0u8; PKT_HEADER_SIZE + DATA_LEN];
747+ let (recvd_hdr_raw, recvd_data_raw) = recvd_pkt_raw.split_at_mut(PKT_HEADER_SIZE);
748+
749+ let mut recvd_packet =
750+ // SAFETY: Safe as recvd_hdr_raw and recvd_data_raw are guaranteed to be valid.
751+ unsafe { VsockPacket::new(recvd_hdr_raw, Some(recvd_data_raw)).unwrap() };
752+
753+ sibling_backend.threads[0]
754+ .lock()
755+ .unwrap()
756+ .thread_backend
757+ .recv_raw_pkt(&mut recvd_packet)
758+ .unwrap();
759+
760+ assert_eq!(recvd_packet.type_(), VSOCK_TYPE_STREAM);
761+ assert_eq!(recvd_packet.src_cid(), CID);
762+ assert_eq!(recvd_packet.dst_cid(), SIBLING_CID);
763+ assert_eq!(recvd_packet.dst_port(), SIBLING_LISTENING_PORT);
764+ assert_eq!(recvd_packet.op(), VSOCK_OP_RW);
765+ assert_eq!(recvd_packet.len(), DATA_LEN as u32);
766+
767+ assert_eq!(recvd_data_raw[0], 0xCAu8);
768+ assert_eq!(recvd_data_raw[1], 0xFEu8);
769+ assert_eq!(recvd_data_raw[2], 0xBAu8);
770+ assert_eq!(recvd_data_raw[3], 0xBEu8);
771+
772+ test_dir.close().unwrap();
773+ }
774+ */
775775}
0 commit comments