@@ -964,10 +964,6 @@ pub mod tests {
964
964
impl Net {
965
965
pub ( crate ) fn read_tap ( & mut self ) -> io:: Result < usize > {
966
966
match & self . tap . mocks . read_tap {
967
- ReadTapMock :: MockFrame ( frame) => {
968
- self . rx_frame_buf [ ..frame. len ( ) ] . copy_from_slice ( frame) ;
969
- Ok ( frame. len ( ) )
970
- }
971
967
ReadTapMock :: Failure => Err ( io:: Error :: new (
972
968
io:: ErrorKind :: Other ,
973
969
"Read tap synthetically failed." ,
@@ -1911,18 +1907,22 @@ pub mod tests {
1911
1907
1912
1908
// Test RX bandwidth rate limiting
1913
1909
{
1914
- // create bandwidth rate limiter that allows 40960 bytes/s with bucket size 4096 bytes
1915
- let mut rl = RateLimiter :: new ( 0x1000 , 0 , 100 , 0 , 0 , 0 ) . unwrap ( ) ;
1916
- // use up the budget
1917
- assert ! ( rl. consume( 0x1000 , TokenType :: Bytes ) ) ;
1918
-
1919
- // set this rx rate limiter to be used
1920
- th. net ( ) . rx_rate_limiter = rl;
1910
+ // create bandwidth rate limiter that allows 2000 bytes/s with bucket size 1000 bytes
1911
+ let mut rl = RateLimiter :: new ( 1000 , 0 , 500 , 0 , 0 , 0 ) . unwrap ( ) ;
1921
1912
1922
1913
// set up RX
1923
1914
assert ! ( !th. net( ) . rx_deferred_frame) ;
1924
1915
th. add_desc_chain ( NetQueue :: Rx , 0 , & [ ( 0 , 4096 , VIRTQ_DESC_F_WRITE ) ] ) ;
1925
1916
1917
+ let frame = inject_tap_tx_frame ( & th. net ( ) , 1000 ) ;
1918
+
1919
+ // use up the budget (do it after injecting the tx frame, as socket communication is
1920
+ // slow enough that the ratelimiter could replenish in the meantime).
1921
+ assert ! ( rl. consume( 1000 , TokenType :: Bytes ) ) ;
1922
+
1923
+ // set this rx rate limiter to be used
1924
+ th. net ( ) . rx_rate_limiter = rl;
1925
+
1926
1926
// following RX procedure should fail because of bandwidth rate limiting
1927
1927
{
1928
1928
// trigger the RX handler
@@ -1946,13 +1946,12 @@ pub mod tests {
1946
1946
assert_eq ! ( th. net( ) . metrics. rx_rate_limiter_throttled. count( ) , 2 ) ;
1947
1947
}
1948
1948
1949
- // wait for 100ms to give the rate-limiter timer a chance to replenish
1950
- // wait for an extra 100ms to make sure the timerfd event makes its way from the kernel
1951
- thread:: sleep ( Duration :: from_millis ( 200 ) ) ;
1949
+ // wait for 500ms to give the rate-limiter timer a chance to replenish
1950
+ // wait for an extra 500ms to make sure the timerfd event makes its way from the kernel
1951
+ thread:: sleep ( Duration :: from_millis ( 1000 ) ) ;
1952
1952
1953
1953
// following RX procedure should succeed because bandwidth should now be available
1954
1954
{
1955
- let frame = & th. net ( ) . tap . mocks . read_tap . mock_frame ( ) ;
1956
1955
// no longer throttled
1957
1956
check_metric_after_block ! (
1958
1957
th. net( ) . metrics. rx_rate_limiter_throttled,
@@ -1967,7 +1966,7 @@ pub mod tests {
1967
1966
assert_eq ! ( th. rxq. used. idx. get( ) , 1 ) ;
1968
1967
th. rxq
1969
1968
. check_used_elem ( 0 , 0 , frame. len ( ) . try_into ( ) . unwrap ( ) ) ;
1970
- th. rxq . dtable [ 0 ] . check_data ( frame) ;
1969
+ th. rxq . dtable [ 0 ] . check_data ( & frame) ;
1971
1970
}
1972
1971
}
1973
1972
}
@@ -2025,18 +2024,20 @@ pub mod tests {
2025
2024
2026
2025
// Test RX ops rate limiting
2027
2026
{
2028
- // create ops rate limiter that allows 10 ops/s with bucket size 1 ops
2029
- let mut rl = RateLimiter :: new ( 0 , 0 , 0 , 1 , 0 , 100 ) . unwrap ( ) ;
2027
+ // create ops rate limiter that allows 2 ops/s with bucket size 1 ops
2028
+ let mut rl = RateLimiter :: new ( 0 , 0 , 0 , 1 , 0 , 500 ) . unwrap ( ) ;
2029
+
2030
+ // set up RX
2031
+ assert ! ( !th. net( ) . rx_deferred_frame) ;
2032
+ th. add_desc_chain ( NetQueue :: Rx , 0 , & [ ( 0 , 4096 , VIRTQ_DESC_F_WRITE ) ] ) ;
2033
+ let frame = inject_tap_tx_frame ( & th. net ( ) , 1234 ) ;
2034
+
2030
2035
// use up the initial budget
2031
2036
assert ! ( rl. consume( 1 , TokenType :: Ops ) ) ;
2032
2037
2033
2038
// set this rx rate limiter to be used
2034
2039
th. net ( ) . rx_rate_limiter = rl;
2035
2040
2036
- // set up RX
2037
- assert ! ( !th. net( ) . rx_deferred_frame) ;
2038
- th. add_desc_chain ( NetQueue :: Rx , 0 , & [ ( 0 , 4096 , VIRTQ_DESC_F_WRITE ) ] ) ;
2039
-
2040
2041
// following RX procedure should fail because of ops rate limiting
2041
2042
{
2042
2043
// trigger the RX handler
@@ -2063,21 +2064,20 @@ pub mod tests {
2063
2064
assert_eq ! ( th. rxq. used. idx. get( ) , 0 ) ;
2064
2065
}
2065
2066
2066
- // wait for 100ms to give the rate-limiter timer a chance to replenish
2067
- // wait for an extra 100ms to make sure the timerfd event makes its way from the kernel
2068
- thread:: sleep ( Duration :: from_millis ( 200 ) ) ;
2067
+ // wait for 500ms to give the rate-limiter timer a chance to replenish
2068
+ // wait for an extra 500ms to make sure the timerfd event makes its way from the kernel
2069
+ thread:: sleep ( Duration :: from_millis ( 1000 ) ) ;
2069
2070
2070
2071
// following RX procedure should succeed because ops should now be available
2071
2072
{
2072
- let frame = & th. net ( ) . tap . mocks . read_tap . mock_frame ( ) ;
2073
2073
th. simulate_event ( NetEvent :: RxRateLimiter ) ;
2074
2074
// make sure the virtio queue operation completed this time
2075
2075
assert ! ( & th. net( ) . irq_trigger. has_pending_irq( IrqType :: Vring ) ) ;
2076
2076
// make sure the data queue advanced
2077
2077
assert_eq ! ( th. rxq. used. idx. get( ) , 1 ) ;
2078
2078
th. rxq
2079
2079
. check_used_elem ( 0 , 0 , frame. len ( ) . try_into ( ) . unwrap ( ) ) ;
2080
- th. rxq . dtable [ 0 ] . check_data ( frame) ;
2080
+ th. rxq . dtable [ 0 ] . check_data ( & frame) ;
2081
2081
}
2082
2082
}
2083
2083
}
0 commit comments