@@ -19,6 +19,8 @@ use concurrency::sync::Arc;
1919use acl_filter:: { AclFilter , AclFilterContextReaderFactory , AclFilterContextWriter } ;
2020use flow_entry:: flow_table:: { FlowLookup , FlowTable } ;
2121use flow_filter:: { FlowFilter , FlowFilterContextReaderFactory , FlowFilterContextWriter } ;
22+ use rate_limit:: { RateLimitReader , RateLimitWriter , RateLimiter , RateLimits , VpcDestination } ;
23+ use std:: num:: NonZeroUsize ;
2224
2325use nat:: masquerade:: { NatAllocatorReaderFactory , NatAllocatorWriter } ;
2426use nat:: portfw:: { PortForwarder , PortFwTableReaderFactory , PortFwTableWriter } ;
@@ -50,6 +52,7 @@ pub(crate) struct InternalSetup {
5052 pub natallocatorw : NatAllocatorWriter ,
5153 pub flow_filter_writer : FlowFilterContextWriter ,
5254 pub aclfiltertablesw : AclFilterContextWriter ,
55+ pub rate_limits : RateLimitWriter < VpcDestination > ,
5356 pub stats : StatsCollector ,
5457 pub vpc_stats_store : Arc < VpcStatsStore > ,
5558 pub portfw_w : PortFwTableWriter ,
@@ -71,6 +74,7 @@ pub(crate) struct PipelineIngredients {
7174 natallocator_factory : NatAllocatorReaderFactory ,
7275 flow_filter_reader_factory : FlowFilterContextReaderFactory ,
7376 aclfiltertablesr_factory : AclFilterContextReaderFactory ,
77+ rate_limits : RateLimitReader < VpcDestination > ,
7478 portfw_factory : PortFwTableReaderFactory ,
7579 stats_w : PacketStatsWriter ,
7680 pkt_stats : Arc < PacketStats > ,
@@ -115,6 +119,7 @@ impl PipelineIngredients {
115119 let flow_filter =
116120 FlowFilter :: new ( "flow-filter" , self . flow_filter_reader_factory . handle ( ) ) ;
117121 let acl_filter = AclFilter :: new ( "acl-filter" , self . aclfiltertablesr_factory . handle ( ) ) ;
122+ let rate_limiter = RateLimiter :: new ( self . rate_limits . clone ( ) ) ;
118123 let icmp_error_handler = IcmpErrorHandler :: new ( self . flow_table . clone ( ) ) ;
119124 let flow_lookup = FlowLookup :: new ( "flow-lookup" , self . flow_table . clone ( ) ) ;
120125 let portfw = PortForwarder :: new (
@@ -141,6 +146,9 @@ impl PipelineIngredients {
141146 . add_stage ( flow_lookup)
142147 . add_stage ( flow_filter)
143148 . add_stage ( acl_filter)
149+ // After the ACLs, so that denied packets are not charged; before NAT, so that rate
150+ // limits see destinations as the source VPC addresses them.
151+ . add_stage ( rate_limiter)
144152 . add_stage ( static_nat)
145153 . add_stage ( portfw)
146154 . add_stage ( masquerade)
@@ -161,6 +169,7 @@ pub(crate) fn start_router(
161169 params : RouterParams ,
162170 driver_status : DriverStatusReader ,
163171 capture_dir : Option < String > ,
172+ workers : NonZeroUsize ,
164173) -> Result < InternalSetup , RouterError > {
165174 let vpcmapw = VpcMapWriter :: < VpcMapName > :: new ( ) ;
166175 let vpc_stats_store: Arc < VpcStatsStore > = VpcStatsStore :: new ( ) ;
@@ -176,6 +185,7 @@ pub(crate) fn start_router(
176185 let flow_filter_reader_factory = flow_filter_writer. get_reader_factory ( ) ;
177186 let aclfiltertablesw = AclFilterContextWriter :: new ( ) ;
178187 let aclfiltertablesr_factory = aclfiltertablesw. get_reader_factory ( ) ;
188+ let rate_limits = RateLimitWriter :: new ( RateLimits :: empty ( ) , workers) ;
179189 let nattablesw = NatTablesWriter :: new ( ) ;
180190 let natallocatorw = NatAllocatorWriter :: new ( ) ;
181191 let nattabler_factory = nattablesw. get_reader_factory ( ) ;
@@ -209,6 +219,7 @@ pub(crate) fn start_router(
209219 transceivers_read,
210220 ) ) ) ,
211221 link : Some ( Box :: new ( crate :: phy_agent:: PhyCli ( phy_read) ) ) ,
222+ rate_limits : Some ( Box :: new ( rate_limits. reader ( ) ) ) ,
212223 } ;
213224
214225 // create router
@@ -226,6 +237,7 @@ pub(crate) fn start_router(
226237 natallocator_factory,
227238 flow_filter_reader_factory,
228239 aclfiltertablesr_factory,
240+ rate_limits : rate_limits. reader ( ) ,
229241 portfw_factory,
230242 stats_w,
231243 pkt_stats,
@@ -242,6 +254,7 @@ pub(crate) fn start_router(
242254 natallocatorw,
243255 flow_filter_writer,
244256 aclfiltertablesw,
257+ rate_limits,
245258 stats,
246259 vpc_stats_store,
247260 portfw_w,
0 commit comments