Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ratelimiter to its own crate #580

Merged
merged 8 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions api_server/src/request/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,33 @@ mod tests {
partuuid: None,
rate_limiter: None,
};

assert!(
&desc
.clone()
.into_parsed_request(Some(String::from("foo")), Method::Options)
&desc.into_parsed_request(Some(String::from("foo")), Method::Options)
== &Err(String::from("Invalid method."))
);

// BlockDeviceConfig doesn't implement Clone so we have to define multiple identical vars.
let desc = BlockDeviceConfig {
drive_id: String::from("foo"),
path_on_host: PathBuf::from(String::from("/foo/bar")),
is_root_device: true,
is_read_only: true,
partuuid: None,
rate_limiter: None,
};
let same_desc = BlockDeviceConfig {
drive_id: String::from("foo"),
path_on_host: PathBuf::from(String::from("/foo/bar")),
is_root_device: true,
is_read_only: true,
partuuid: None,
rate_limiter: None,
};
let (sender, receiver) = oneshot::channel();
assert!(
&desc
.clone()
.into_parsed_request(Some(String::from("foo")), Method::Put)
desc.into_parsed_request(Some(String::from("foo")), Method::Put)
.eq(&Ok(ParsedRequest::Sync(
VmmAction::InsertBlockDevice(desc, sender),
VmmAction::InsertBlockDevice(same_desc, sender),
receiver
)))
);
Expand Down
5 changes: 0 additions & 5 deletions api_server/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,6 @@ mod tests {
StartMicrovmError::OpenBlockDevice(std::io::Error::from_raw_os_error(22)),
);
check_error_response(vmm_resp, StatusCode::BadRequest);
let vmm_resp = VmmActionError::StartMicrovm(
ErrorKind::Internal,
StartMicrovmError::CreateRateLimiter(std::io::Error::from_raw_os_error(22)),
);
check_error_response(vmm_resp, StatusCode::InternalServerError);
let vmm_resp = VmmActionError::StartMicrovm(
ErrorKind::Internal,
StartMicrovmError::NetDeviceNotConfigured,
Expand Down
10 changes: 3 additions & 7 deletions api_server/src/request/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests {

use serde_json;

use self::rate_limiter::RateLimiterDescription;
use self::rate_limiter::RateLimiter;
use vmm::vmm_config::DeviceState;

fn get_dummy_netif(
Expand Down Expand Up @@ -100,8 +100,8 @@ mod tests {
state: DeviceState::Attached,
host_dev_name: String::from("bar"),
guest_mac: Some(MacAddr::parse_str("12:34:56:78:9A:BC").unwrap()),
rx_rate_limiter: Some(RateLimiterDescription::default()),
tx_rate_limiter: Some(RateLimiterDescription::default()),
rx_rate_limiter: Some(RateLimiter::default()),
tx_rate_limiter: Some(RateLimiter::default()),
allow_mmds_requests: true,
tap: None,
};
Expand All @@ -122,10 +122,6 @@ mod tests {
let x = serde_json::from_str(jstr).expect("deserialization failed.");
assert_eq!(netif, x);

let y = serde_json::to_string(&netif).expect("serialization failed.");
let z = serde_json::from_str(y.as_ref()).expect("deserialization (2) failed.");
assert_eq!(x, z);

// Check that guest_mac and rate limiters are truly optional.
let jstr_no_mac = r#"{
"iface_id": "foo",
Expand Down
2 changes: 1 addition & 1 deletion devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ epoll = "=2.1.0"
libc = ">=0.2.39"

dumbo = { path = "../dumbo" }
fc_util = { path = "../fc_util" }
logger = { path = "../logger" }
memory_model = { path = "../memory_model" }
net_util = { path = "../net_util" }
net_sys = { path = "../net_sys" }
rate_limiter = { path = "../rate_limiter" }
sys_util = { path = "../sys_util" }
virtio_sys = { path = "../virtio_sys" }

Expand Down
2 changes: 1 addition & 1 deletion devices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! Emulates virtual and hardware devices.
extern crate byteorder;
extern crate epoll;
extern crate fc_util;
extern crate libc;

extern crate dumbo;
Expand All @@ -14,6 +13,7 @@ extern crate logger;
extern crate memory_model;
extern crate net_sys;
extern crate net_util;
extern crate rate_limiter;
extern crate sys_util;
extern crate virtio_sys;

Expand Down
8 changes: 4 additions & 4 deletions devices/src/virtio/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use super::{
ActivateError, ActivateResult, DescriptorChain, EpollHandlerPayload, Queue, VirtioDevice,
TYPE_BLOCK, VIRTIO_MMIO_INT_VRING,
};
use fc_util::ratelimiter::{RateLimiter, TokenType};
use logger::{Metric, METRICS};
use memory_model::{GuestAddress, GuestMemory, GuestMemoryError};
use rate_limiter::{RateLimiter, TokenType};
use sys_util::EventFd;
use sys_util::Result as SysResult;
use virtio_sys::virtio_blk::*;
Expand Down Expand Up @@ -654,7 +654,7 @@ mod tests {
f.set_len(0x1000).unwrap();

// Rate limiting is enabled but with a high operation rate (10 million ops/s).
let rate_limiter = RateLimiter::new(0, 0, 0, 100000, 0, 10).unwrap();
let rate_limiter = RateLimiter::new(0, None, 0, 100000, None, 10).unwrap();
DummyBlock {
block: Block::new(f, is_disk_read_only, epoll_config, Some(rate_limiter)).unwrap(),
epoll_raw_fd,
Expand Down Expand Up @@ -1269,7 +1269,7 @@ mod tests {
// test the bandwidth rate limiter
{
// create bandwidth rate limiter that allows only 80 bytes/s with bucket size of 8 bytes
let mut rl = RateLimiter::new(8, 0, 100, 0, 0, 0).unwrap();
let mut rl = RateLimiter::new(8, None, 100, 0, None, 0).unwrap();
// use up the budget
assert!(rl.consume(8, TokenType::Bytes));

Expand Down Expand Up @@ -1328,7 +1328,7 @@ mod tests {
// test the ops/s rate limiter
{
// create ops rate limiter that allows only 10 ops/s with bucket size of 1 ops
let mut rl = RateLimiter::new(0, 0, 0, 1, 0, 100).unwrap();
let mut rl = RateLimiter::new(0, None, 0, 1, None, 100).unwrap();
// use up the budget
assert!(rl.consume(1, TokenType::Ops));

Expand Down
30 changes: 21 additions & 9 deletions devices/src/virtio/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use super::{
VIRTIO_MMIO_INT_VRING,
};
use dumbo::ns::MmdsNetworkStack;
use fc_util::ratelimiter::{RateLimiter, TokenType};
use logger::{Metric, METRICS};
use memory_model::{GuestAddress, GuestMemory};
use net_sys;
use net_util::{MacAddr, Tap, TapError, MAC_ADDR_LEN};
use rate_limiter::{RateLimiter, TokenType};
use sys_util::EventFd;
use virtio_sys::virtio_config::*;
use virtio_sys::virtio_net::*;
Expand Down Expand Up @@ -931,12 +931,24 @@ mod tests {
epoll_config,
// rate limiters present but with _very high_ allowed rate
Some(
RateLimiter::new(u64::max_value(), 0, 1000, u64::max_value(), 0, 1000)
.unwrap(),
RateLimiter::new(
u64::max_value(),
None,
1000,
u64::max_value(),
None,
1000,
).unwrap(),
),
Some(
RateLimiter::new(u64::max_value(), 0, 1000, u64::max_value(), 0, 1000)
.unwrap(),
RateLimiter::new(
u64::max_value(),
None,
1000,
u64::max_value(),
None,
1000,
).unwrap(),
),
true,
).unwrap(),
Expand Down Expand Up @@ -1498,7 +1510,7 @@ mod tests {
// Test TX bandwidth rate limiting
{
// create bandwidth rate limiter that allows 40960 bytes/s with bucket size 4096 bytes
let mut rl = RateLimiter::new(0x1000, 0, 100, 0, 0, 0).unwrap();
let mut rl = RateLimiter::new(0x1000, None, 100, 0, None, 0).unwrap();
// use up the budget
assert!(rl.consume(0x1000, TokenType::Bytes));

Expand Down Expand Up @@ -1539,7 +1551,7 @@ mod tests {
// Test RX bandwidth rate limiting
{
// create bandwidth rate limiter that allows 40960 bytes/s with bucket size 4096 bytes
let mut rl = RateLimiter::new(0x1000, 0, 100, 0, 0, 0).unwrap();
let mut rl = RateLimiter::new(0x1000, None, 100, 0, None, 0).unwrap();
// use up the budget
assert!(rl.consume(0x1000, TokenType::Bytes));

Expand Down Expand Up @@ -1601,7 +1613,7 @@ mod tests {
// Test TX ops rate limiting
{
// create ops rate limiter that allows 10 ops/s with bucket size 1 ops
let mut rl = RateLimiter::new(0, 0, 0, 1, 0, 100).unwrap();
let mut rl = RateLimiter::new(0, None, 0, 1, None, 100).unwrap();
// use up the budget
assert!(rl.consume(1, TokenType::Ops));

Expand Down Expand Up @@ -1642,7 +1654,7 @@ mod tests {
// Test RX ops rate limiting
{
// create ops rate limiter that allows 10 ops/s with bucket size 1 ops
let mut rl = RateLimiter::new(0, 0, 0, 1, 0, 100).unwrap();
let mut rl = RateLimiter::new(0, None, 0, 1, None, 100).unwrap();
// use up the budget
assert!(rl.consume(0x800, TokenType::Ops));

Expand Down
4 changes: 0 additions & 4 deletions fc_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ authors = ["Amazon firecracker team <firecracker-devel@amazon.com>"]

[dependencies]
libc = ">=0.2.39"
time = ">=0.1.39"
timerfd = "1.0"

logger = { path = "../logger" }
4 changes: 0 additions & 4 deletions fc_util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
extern crate libc;

#[macro_use]
extern crate logger;

pub mod ratelimiter;
pub mod validators;

#[cfg(target_arch = "x86_64")]
Expand Down
Loading