1 // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 // 4 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style license that can be 6 // found in the THIRD-PARTY file. 7 8 #[macro_use] 9 extern crate log; 10 11 mod ctrl_queue; 12 mod mac; 13 mod open_tap; 14 mod queue_pair; 15 mod tap; 16 17 use std::io::Error as IoError; 18 use std::os::raw::c_uint; 19 use std::os::unix::io::{FromRawFd, RawFd}; 20 use std::{io, mem, net}; 21 use thiserror::Error; 22 use versionize::{VersionMap, Versionize, VersionizeResult}; 23 use versionize_derive::Versionize; 24 use virtio_bindings::bindings::virtio_net::{ 25 virtio_net_hdr_v1, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN, 26 VIRTIO_NET_F_GUEST_CSUM, VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_TSO4, 27 VIRTIO_NET_F_GUEST_TSO6, VIRTIO_NET_F_GUEST_UFO, VIRTIO_NET_F_MAC, VIRTIO_NET_F_MQ, 28 }; 29 use vm_memory::{bitmap::AtomicBitmap, ByteValued}; 30 31 type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>; 32 33 pub use ctrl_queue::{CtrlQueue, Error as CtrlQueueError}; 34 pub use mac::{MacAddr, MAC_ADDR_LEN}; 35 pub use open_tap::{open_tap, Error as OpenTapError}; 36 pub use queue_pair::{NetCounters, NetQueuePair, NetQueuePairError, RxVirtio, TxVirtio}; 37 pub use tap::{Error as TapError, Tap}; 38 39 #[derive(Error, Debug)] 40 pub enum Error { 41 #[error("Failed to create a socket: {0}")] 42 CreateSocket(IoError), 43 } 44 45 pub type Result<T> = std::result::Result<T, Error>; 46 47 #[repr(C, packed)] 48 #[derive(Copy, Clone, Debug, Default, Versionize)] 49 pub struct VirtioNetConfig { 50 pub mac: [u8; 6], 51 pub status: u16, 52 pub max_virtqueue_pairs: u16, 53 pub mtu: u16, 54 pub speed: u32, 55 pub duplex: u8, 56 } 57 58 // SAFETY: it only has data and has no implicit padding. 59 unsafe impl ByteValued for VirtioNetConfig {} 60 61 /// Create a sockaddr_in from an IPv4 address, and expose it as 62 /// an opaque sockaddr suitable for usage by socket ioctls. 63 fn create_sockaddr(ip_addr: net::Ipv4Addr) -> net_gen::sockaddr { 64 // IPv4 addresses big-endian (network order), but Ipv4Addr will give us 65 // a view of those bytes directly so we can avoid any endian trickiness. 66 let addr_in = net_gen::sockaddr_in { 67 sin_family: net_gen::AF_INET as u16, 68 sin_port: 0, 69 sin_addr: unsafe { mem::transmute(ip_addr.octets()) }, 70 __pad: [0; 8usize], 71 }; 72 73 unsafe { mem::transmute(addr_in) } 74 } 75 76 fn create_inet_socket() -> Result<net::UdpSocket> { 77 // This is safe since we check the return value. 78 let sock = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, 0) }; 79 if sock < 0 { 80 return Err(Error::CreateSocket(IoError::last_os_error())); 81 } 82 83 // This is safe; nothing else will use or hold onto the raw sock fd. 84 Ok(unsafe { net::UdpSocket::from_raw_fd(sock) }) 85 } 86 87 fn create_unix_socket() -> Result<net::UdpSocket> { 88 // This is safe since we check the return value. 89 let sock = unsafe { libc::socket(libc::AF_UNIX, libc::SOCK_DGRAM, 0) }; 90 if sock < 0 { 91 return Err(Error::CreateSocket(IoError::last_os_error())); 92 } 93 94 // This is safe; nothing else will use or hold onto the raw sock fd. 95 Ok(unsafe { net::UdpSocket::from_raw_fd(sock) }) 96 } 97 98 fn vnet_hdr_len() -> usize { 99 std::mem::size_of::<virtio_net_hdr_v1>() 100 } 101 102 pub fn register_listener( 103 epoll_fd: RawFd, 104 fd: RawFd, 105 ev_type: epoll::Events, 106 data: u64, 107 ) -> std::result::Result<(), io::Error> { 108 epoll::ctl( 109 epoll_fd, 110 epoll::ControlOptions::EPOLL_CTL_ADD, 111 fd, 112 epoll::Event::new(ev_type, data), 113 ) 114 } 115 116 pub fn unregister_listener( 117 epoll_fd: RawFd, 118 fd: RawFd, 119 ev_type: epoll::Events, 120 data: u64, 121 ) -> std::result::Result<(), io::Error> { 122 epoll::ctl( 123 epoll_fd, 124 epoll::ControlOptions::EPOLL_CTL_DEL, 125 fd, 126 epoll::Event::new(ev_type, data), 127 ) 128 } 129 130 pub fn build_net_config_space( 131 config: &mut VirtioNetConfig, 132 mac: MacAddr, 133 num_queues: usize, 134 mtu: Option<u16>, 135 avail_features: &mut u64, 136 ) { 137 config.mac.copy_from_slice(mac.get_bytes()); 138 *avail_features |= 1 << VIRTIO_NET_F_MAC; 139 140 build_net_config_space_with_mq(config, num_queues, mtu, avail_features); 141 } 142 143 pub fn build_net_config_space_with_mq( 144 config: &mut VirtioNetConfig, 145 num_queues: usize, 146 mtu: Option<u16>, 147 avail_features: &mut u64, 148 ) { 149 let num_queue_pairs = (num_queues / 2) as u16; 150 if (num_queue_pairs >= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN as u16) 151 && (num_queue_pairs <= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as u16) 152 { 153 config.max_virtqueue_pairs = num_queue_pairs; 154 *avail_features |= 1 << VIRTIO_NET_F_MQ; 155 } 156 if let Some(mtu) = mtu { 157 config.mtu = mtu; 158 } 159 } 160 161 pub fn virtio_features_to_tap_offload(features: u64) -> c_uint { 162 let mut tap_offloads: c_uint = 0; 163 if features & (1 << VIRTIO_NET_F_GUEST_CSUM) != 0 { 164 tap_offloads |= net_gen::TUN_F_CSUM; 165 } 166 if features & (1 << VIRTIO_NET_F_GUEST_TSO4) != 0 { 167 tap_offloads |= net_gen::TUN_F_TSO4; 168 } 169 if features & (1 << VIRTIO_NET_F_GUEST_TSO6) != 0 { 170 tap_offloads |= net_gen::TUN_F_TSO6; 171 } 172 if features & (1 << VIRTIO_NET_F_GUEST_ECN) != 0 { 173 tap_offloads |= net_gen::TUN_F_TSO_ECN; 174 } 175 if features & (1 << VIRTIO_NET_F_GUEST_UFO) != 0 { 176 tap_offloads |= net_gen::TUN_F_UFO; 177 } 178 179 tap_offloads 180 } 181 182 #[cfg(test)] 183 mod tests { 184 use super::*; 185 186 #[test] 187 fn test_create_sockaddr() { 188 let addr: net::Ipv4Addr = "10.0.0.1".parse().unwrap(); 189 let sockaddr = create_sockaddr(addr); 190 191 assert_eq!(sockaddr.sa_family, net_gen::AF_INET as u16); 192 193 let data = &sockaddr.sa_data[..]; 194 195 // The first two bytes should represent the port, which is 0. 196 assert_eq!(data[0], 0); 197 assert_eq!(data[1], 0); 198 199 // The next four bytes should represent the actual IPv4 address, in network order. 200 assert_eq!(data[2], 10); 201 assert_eq!(data[3], 0); 202 assert_eq!(data[4], 0); 203 assert_eq!(data[5], 1); 204 } 205 } 206