Skip to content

Commit bd7fb8e

Browse files
author
Jonathan Woollett-Light
committed
fix: Update logging
Adds additional `std::fmt::Debug` implementations, reduces dynamic dispatch and removes the `BusDevice` trait. Signed-off-by: Jonathan Woollett-Light <[email protected]>
1 parent c0b3420 commit bd7fb8e

File tree

130 files changed

+1645
-1976
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+1645
-1976
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api_server/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
mod parsed_request;
1111
mod request;
1212

13+
use std::fmt::Debug;
1314
use std::path::PathBuf;
1415
use std::sync::mpsc;
1516

@@ -44,6 +45,7 @@ pub enum Error {
4445
type Result<T> = std::result::Result<T, Error>;
4546

4647
/// Structure associated with the API server implementation.
48+
#[derive(Debug)]
4749
pub struct ApiServer {
4850
/// Sender which allows passing messages to the VMM.
4951
api_request_sender: mpsc::Sender<ApiRequest>,
@@ -113,7 +115,7 @@ impl ApiServer {
113115
/// let (api_request_sender, _from_api) = channel();
114116
/// let (to_api, vmm_response_receiver) = channel();
115117
/// let time_reporter = ProcessTimeReporter::new(Some(1), Some(1), Some(1));
116-
/// let seccomp_filters = get_filters(SeccompConfig::None).unwrap();
118+
/// let seccomp_filters = get_filters(SeccompConfig::<std::io::Empty>::None).unwrap();
117119
/// let payload_limit = HTTP_MAX_PAYLOAD_SIZE;
118120
/// let (socket_ready_sender, socket_ready_receiver): (Sender<bool>, Receiver<bool>) = channel();
119121
///
@@ -291,13 +293,13 @@ impl ApiServer {
291293
}
292294

293295
/// An HTTP response which also includes a body.
294-
pub(crate) fn json_response<T: Into<String>>(status: StatusCode, body: T) -> Response {
296+
pub(crate) fn json_response<T: Into<String> + Debug>(status: StatusCode, body: T) -> Response {
295297
let mut response = Response::new(Version::Http11, status);
296298
response.set_body(Body::new(body.into()));
297299
response
298300
}
299301

300-
fn json_fault_message<T: AsRef<str> + serde::Serialize>(msg: T) -> String {
302+
fn json_fault_message<T: AsRef<str> + serde::Serialize + Debug>(msg: T) -> String {
301303
json!({ "fault_message": msg }).to_string()
302304
}
303305
}
@@ -443,7 +445,7 @@ mod tests {
443445
let to_vmm_fd = EventFd::new(libc::EFD_NONBLOCK).unwrap();
444446
let (api_request_sender, _from_api) = channel();
445447
let (to_api, vmm_response_receiver) = channel();
446-
let seccomp_filters = get_filters(SeccompConfig::Advanced).unwrap();
448+
let seccomp_filters = get_filters(SeccompConfig::<std::io::Empty>::Advanced).unwrap();
447449
let (socket_ready_sender, socket_ready_receiver) = channel();
448450

449451
thread::Builder::new()
@@ -491,7 +493,7 @@ mod tests {
491493
let to_vmm_fd = EventFd::new(libc::EFD_NONBLOCK).unwrap();
492494
let (api_request_sender, _from_api) = channel();
493495
let (_to_api, vmm_response_receiver) = channel();
494-
let seccomp_filters = get_filters(SeccompConfig::Advanced).unwrap();
496+
let seccomp_filters = get_filters(SeccompConfig::<std::io::Empty>::Advanced).unwrap();
495497
let (socket_ready_sender, socket_ready_receiver) = channel();
496498

497499
thread::Builder::new()

src/api_server/src/parsed_request.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use std::fmt::Debug;
5+
46
use logger::{error, info};
57
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
68
use serde::ser::Serialize;
@@ -27,14 +29,13 @@ use crate::request::version::parse_get_version;
2729
use crate::request::vsock::parse_put_vsock;
2830
use crate::ApiServer;
2931

30-
#[cfg_attr(test, derive(Debug))]
32+
#[derive(Debug)]
3133
pub(crate) enum RequestAction {
3234
Sync(Box<VmmAction>),
3335
ShutdownInternal, // !!! not an API, used by shutdown to thread::join the API thread
3436
}
3537

36-
#[derive(Default)]
37-
#[cfg_attr(test, derive(Debug, PartialEq))]
38+
#[derive(Debug, Default, PartialEq)]
3839
pub(crate) struct ParsingInfo {
3940
deprecation_message: Option<String>,
4041
}
@@ -52,7 +53,7 @@ impl ParsingInfo {
5253
}
5354
}
5455

55-
#[cfg_attr(test, derive(Debug))]
56+
#[derive(Debug)]
5657
pub(crate) struct ParsedRequest {
5758
action: RequestAction,
5859
parsing_info: ParsingInfo,
@@ -141,7 +142,7 @@ impl ParsedRequest {
141142

142143
pub(crate) fn success_response_with_data<T>(body_data: &T) -> Response
143144
where
144-
T: ?Sized + Serialize,
145+
T: ?Sized + Serialize + Debug,
145146
{
146147
info!("The request was executed successfully. Status code: 200 OK.");
147148
let mut response = Response::new(Version::Http11, StatusCode::OK);

src/cpu-template-helper/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn build_microvm_from_config(config: &str) -> Result<(Arc<Mutex<Vmm>>, VmRes
100100
let vm_resources = VmResources::from_json(config, &instance_info, HTTP_MAX_PAYLOAD_SIZE, None)
101101
.map_err(Error::CreateVmResources)?;
102102
let mut event_manager = EventManager::new().unwrap();
103-
let seccomp_filters = get_filters(SeccompConfig::None).unwrap();
103+
let seccomp_filters = get_filters(SeccompConfig::<std::io::Empty>::None).unwrap();
104104

105105
// Build a microVM.
106106
let vmm = build_microvm_for_boot(

src/dumbo/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ impl ByteBuffer for [u8] {
4848

4949
#[cfg(test)]
5050
mod tests {
51+
use std::fmt::Debug;
52+
5153
use super::*;
5254

53-
fn bb_len<T: ByteBuffer + ?Sized>(buf: &T) -> usize {
55+
fn bb_len<T: ByteBuffer + ?Sized + Debug>(buf: &T) -> usize {
5456
buf.len()
5557
}
5658

57-
fn bb_is_empty<T: ByteBuffer + ?Sized>(buf: &T) -> bool {
59+
fn bb_is_empty<T: ByteBuffer + ?Sized + Debug>(buf: &T) -> bool {
5860
buf.len() == 0
5961
}
6062

61-
fn bb_read_from_1<T: ByteBuffer + ?Sized>(src: &T, dst: &mut [u8]) {
63+
fn bb_read_from_1<T: ByteBuffer + ?Sized + Debug>(src: &T, dst: &mut [u8]) {
6264
src.read_to_slice(1, dst);
6365
}
6466

src/dumbo/src/pdu/arp.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub enum Error {
6565
/// ARP is a generic protocol as far as data
6666
/// link layer and network layer protocols go, but this particular implementation is concerned with
6767
/// ARP frames related to IPv4 over Ethernet.
68+
#[derive(Debug)]
6869
pub struct EthIPv4ArpFrame<'a, T: 'a> {
6970
bytes: InnerBytes<'a, T>,
7071
}
@@ -338,23 +339,17 @@ pub fn test_speculative_tpa(buf: &[u8], addr: Ipv4Addr) -> bool {
338339

339340
#[cfg(test)]
340341
mod tests {
341-
use std::fmt;
342+
use std::str::FromStr;
342343

343344
use super::*;
344345

345-
impl<'a, T: NetworkBytes> fmt::Debug for EthIPv4ArpFrame<'a, T> {
346-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
347-
write!(f, "(EthIPv4ArpFrame frame)")
348-
}
349-
}
350-
351346
#[test]
352347
fn test_eth_ipv4_arp_frame() {
353348
let mut a = [0u8; 1000];
354349
let mut bad_array = [0u8; 1];
355350

356-
let sha = MacAddr::parse_str("01:23:45:67:89:ab").unwrap();
357-
let tha = MacAddr::parse_str("cd:ef:01:23:45:67").unwrap();
351+
let sha = MacAddr::from_str("01:23:45:67:89:ab").unwrap();
352+
let tha = MacAddr::from_str("cd:ef:01:23:45:67").unwrap();
358353
let spa = Ipv4Addr::new(10, 1, 2, 3);
359354
let tpa = Ipv4Addr::new(10, 4, 5, 6);
360355

src/dumbo/src/pdu/bytes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl<'a> NetworkBytesMut for &'a mut [u8] {}
151151
// This struct is used as a convenience for any type which contains a generic member implementing
152152
// NetworkBytes with a lifetime, so we don't have to also add the PhantomData member each time. We
153153
// use pub(super) here because we only want this to be usable by the child modules of `pdu`.
154+
#[derive(Debug)]
154155
pub(super) struct InnerBytes<'a, T: 'a> {
155156
bytes: T,
156157
phantom: PhantomData<&'a T>,

src/dumbo/src/pdu/ethernet.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Contains support for parsing and writing Ethernet frames. Does not currently offer support for
55
//! 802.1Q tags.
66
7+
use std::fmt::Debug;
78
use std::result::Result;
89

910
use super::bytes::{InnerBytes, NetworkBytes, NetworkBytesMut};
@@ -33,6 +34,7 @@ pub enum Error {
3334
}
3435

3536
/// Interprets the inner bytes as an Ethernet frame.
37+
#[derive(Debug)]
3638
pub struct EthernetFrame<'a, T: 'a> {
3739
bytes: InnerBytes<'a, T>,
3840
}
@@ -99,7 +101,7 @@ impl<'a, T: NetworkBytes> EthernetFrame<'a, T> {
99101
}
100102
}
101103

102-
impl<'a, T: NetworkBytesMut> EthernetFrame<'a, T> {
104+
impl<'a, T: NetworkBytesMut + Debug> EthernetFrame<'a, T> {
103105
/// Attempts to write an Ethernet frame using the given header fields to `buf`.
104106
fn new_with_header(
105107
buf: T,
@@ -183,23 +185,17 @@ impl<'a, T: NetworkBytes> Incomplete<EthernetFrame<'a, T>> {
183185

184186
#[cfg(test)]
185187
mod tests {
186-
use std::fmt;
188+
use std::str::FromStr;
187189

188190
use super::*;
189191

190-
impl<'a, T: NetworkBytes> fmt::Debug for EthernetFrame<'a, T> {
191-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
192-
write!(f, "(Ethernet frame)")
193-
}
194-
}
195-
196192
#[test]
197193
fn test_ethernet_frame() {
198194
let mut a = [0u8; 10000];
199195
let mut bad_array = [0u8; 1];
200196

201-
let dst_mac = MacAddr::parse_str("01:23:45:67:89:ab").unwrap();
202-
let src_mac = MacAddr::parse_str("cd:ef:01:23:45:67").unwrap();
197+
let dst_mac = MacAddr::from_str("01:23:45:67:89:ab").unwrap();
198+
let src_mac = MacAddr::from_str("cd:ef:01:23:45:67").unwrap();
203199
let ethertype = 1289;
204200

205201
assert_eq!(

src/dumbo/src/pdu/ipv4.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! [here]: https://en.wikipedia.org/wiki/IPv4#Packet_structure
99
1010
use std::convert::From;
11+
use std::fmt::Debug;
1112
use std::net::Ipv4Addr;
1213
use std::result::Result;
1314

@@ -55,6 +56,7 @@ pub enum Error {
5556
}
5657

5758
/// Interprets the inner bytes as an IPv4 packet.
59+
#[derive(Debug)]
5860
pub struct IPv4Packet<'a, T: 'a> {
5961
bytes: InnerBytes<'a, T>,
6062
}
@@ -247,7 +249,7 @@ impl<'a, T: NetworkBytes> IPv4Packet<'a, T> {
247249
}
248250
}
249251

250-
impl<'a, T: NetworkBytesMut> IPv4Packet<'a, T> {
252+
impl<'a, T: NetworkBytesMut + Debug> IPv4Packet<'a, T> {
251253
/// Attempts to write an IPv4 packet header to `buf`, making sure there is enough space.
252254
///
253255
/// This method returns an incomplete packet, because the size of the payload might be unknown
@@ -381,7 +383,7 @@ impl<'a, T: NetworkBytesMut> IPv4Packet<'a, T> {
381383
/// It can be transformed into an `IPv4Packet` by specifying the size of the payload, and
382384
/// shrinking the inner byte sequence to be as large as the packet itself (this includes setting
383385
/// the `total length` header field).
384-
impl<'a, T: NetworkBytesMut> Incomplete<IPv4Packet<'a, T>> {
386+
impl<'a, T: NetworkBytesMut + Debug> Incomplete<IPv4Packet<'a, T>> {
385387
/// Transforms `self` into an `IPv4Packet` based on the supplied header and payload length. May
386388
/// panic for invalid values of the input parameters.
387389
///
@@ -466,25 +468,11 @@ pub fn test_speculative_dst_addr(buf: &[u8], addr: Ipv4Addr) -> bool {
466468

467469
#[cfg(test)]
468470
mod tests {
469-
use std::fmt;
470-
471471
use super::*;
472472
use crate::MacAddr;
473473

474474
const MAX_HEADER_LEN: usize = 60;
475475

476-
impl<'a, T: NetworkBytes> fmt::Debug for IPv4Packet<'a, T> {
477-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
478-
write!(f, "(IPv4 packet)")
479-
}
480-
}
481-
482-
impl<'a, T: NetworkBytes> fmt::Debug for Incomplete<IPv4Packet<'a, T>> {
483-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
484-
write!(f, "(Incomplete IPv4 packet)")
485-
}
486-
}
487-
488476
#[test]
489477
fn test_set_get() {
490478
let mut a = [0u8; 100];

src/dumbo/src/pdu/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! protocol. Ethernet frames, IP packets, and TCP segments are all examples of protocol data
88
//! units.
99
10+
use std::fmt::Debug;
1011
use std::net::Ipv4Addr;
1112

1213
use crate::pdu::bytes::NetworkBytes;
@@ -27,11 +28,12 @@ pub mod udp;
2728
/// should be equal to the actual size for a complete PDU. To that end, whenever a variable-length
2829
/// payload is involved, the slice is shrunk to an exact fit. The particular ways of completing an
2930
/// `Incomplete<T>` are implemented for each specific PDU.
31+
#[derive(Debug)]
3032
pub struct Incomplete<T> {
3133
inner: T,
3234
}
3335

34-
impl<T> Incomplete<T> {
36+
impl<T: Debug> Incomplete<T> {
3537
#[inline]
3638
fn new(inner: T) -> Self {
3739
Incomplete { inner }
@@ -51,7 +53,7 @@ impl<T> Incomplete<T> {
5153
}
5254

5355
#[repr(u8)]
54-
#[derive(Copy, Clone, PartialEq)]
56+
#[derive(Debug, Copy, Clone, PartialEq)]
5557
enum ChecksumProto {
5658
Tcp = PROTOCOL_TCP,
5759
Udp = PROTOCOL_UDP,
@@ -71,7 +73,7 @@ enum ChecksumProto {
7173
///
7274
/// [here]: https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation
7375
#[inline]
74-
fn compute_checksum<T: NetworkBytes>(
76+
fn compute_checksum<T: NetworkBytes + Debug>(
7577
bytes: &T,
7678
src_addr: Ipv4Addr,
7779
dst_addr: Ipv4Addr,

0 commit comments

Comments
 (0)