@@ -21,7 +21,7 @@ use crate::net::Interface;
2121use crate :: net:: { SocketBufferConfig , UdpMiscSockOpts } ;
2222
2323#[ derive( Clone , Debug , Eq , PartialEq ) ]
24- pub struct UdpListenConfig {
24+ struct NonReloadablePart {
2525 address : SocketAddr ,
2626 #[ cfg( any(
2727 target_os = "linux" ,
@@ -35,8 +35,6 @@ pub struct UdpListenConfig {
3535 ipv6only : Option < bool > ,
3636 #[ cfg( target_os = "linux" ) ]
3737 transparent : bool ,
38- buf_conf : SocketBufferConfig ,
39- misc_opts : UdpMiscSockOpts ,
4038 instance : usize ,
4139 scale : usize ,
4240 #[ cfg( target_os = "linux" ) ]
@@ -45,6 +43,18 @@ pub struct UdpListenConfig {
4543 fail_on_ebpf_error : bool ,
4644}
4745
46+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
47+ struct ReloadablePart {
48+ buf_conf : SocketBufferConfig ,
49+ misc_opts : UdpMiscSockOpts ,
50+ }
51+
52+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
53+ pub struct UdpListenConfig {
54+ non_reloadable : NonReloadablePart ,
55+ reloadable : ReloadablePart ,
56+ }
57+
4858impl Default for UdpListenConfig {
4959 fn default ( ) -> Self {
5060 UdpListenConfig :: new ( SocketAddr :: new ( IpAddr :: V6 ( Ipv6Addr :: UNSPECIFIED ) , 0 ) )
@@ -54,40 +64,44 @@ impl Default for UdpListenConfig {
5464impl UdpListenConfig {
5565 pub fn new ( address : SocketAddr ) -> Self {
5666 UdpListenConfig {
57- address,
58- #[ cfg( any(
59- target_os = "linux" ,
60- target_os = "android" ,
61- target_os = "macos" ,
62- target_os = "illumos" ,
63- target_os = "solaris"
64- ) ) ]
65- interface : None ,
66- #[ cfg( not( target_os = "openbsd" ) ) ]
67- ipv6only : None ,
68- #[ cfg( target_os = "linux" ) ]
69- transparent : false ,
70- buf_conf : SocketBufferConfig :: default ( ) ,
71- misc_opts : UdpMiscSockOpts :: default ( ) ,
72- instance : 1 ,
73- scale : 0 ,
74- #[ cfg( target_os = "linux" ) ]
75- use_ebpf : None ,
76- #[ cfg( target_os = "linux" ) ]
77- fail_on_ebpf_error : false ,
67+ non_reloadable : NonReloadablePart {
68+ address,
69+ #[ cfg( any(
70+ target_os = "linux" ,
71+ target_os = "android" ,
72+ target_os = "macos" ,
73+ target_os = "illumos" ,
74+ target_os = "solaris"
75+ ) ) ]
76+ interface : None ,
77+ #[ cfg( not( target_os = "openbsd" ) ) ]
78+ ipv6only : None ,
79+ #[ cfg( target_os = "linux" ) ]
80+ transparent : false ,
81+ instance : 1 ,
82+ scale : 0 ,
83+ #[ cfg( target_os = "linux" ) ]
84+ use_ebpf : None ,
85+ #[ cfg( target_os = "linux" ) ]
86+ fail_on_ebpf_error : false ,
87+ } ,
88+ reloadable : ReloadablePart {
89+ buf_conf : SocketBufferConfig :: default ( ) ,
90+ misc_opts : UdpMiscSockOpts :: default ( ) ,
91+ } ,
7892 }
7993 }
8094
8195 pub fn check ( & mut self ) -> anyhow:: Result < ( ) > {
82- if self . address . port ( ) == 0 {
96+ if self . non_reloadable . address . port ( ) == 0 {
8397 return Err ( anyhow ! ( "no listen port is set" ) ) ;
8498 }
8599 #[ cfg( not( target_os = "openbsd" ) ) ]
86- match self . address . ip ( ) {
87- IpAddr :: V4 ( _) => self . ipv6only = None ,
100+ match self . non_reloadable . address . ip ( ) {
101+ IpAddr :: V4 ( _) => self . non_reloadable . ipv6only = None ,
88102 IpAddr :: V6 ( v6) => {
89103 if !v6. is_unspecified ( ) {
90- self . ipv6only = None ;
104+ self . non_reloadable . ipv6only = None ;
91105 }
92106 }
93107 }
@@ -96,41 +110,16 @@ impl UdpListenConfig {
96110 }
97111
98112 pub fn need_respawn ( & self , other : & Self ) -> bool {
99- if self . address != other. address {
100- return true ;
101- }
102- if self . instance ( ) != other. instance ( ) {
103- return true ;
104- }
105- #[ cfg( any(
106- target_os = "linux" ,
107- target_os = "android" ,
108- target_os = "macos" ,
109- target_os = "illumos" ,
110- target_os = "solaris"
111- ) ) ]
112- if self . interface != other. interface {
113- return true ;
114- }
115- #[ cfg( not( target_os = "openbsd" ) ) ]
116- if self . ipv6only != other. ipv6only {
117- return true ;
118- }
119- #[ cfg( target_os = "linux" ) ]
120- if self . transparent != other. transparent {
121- return true ;
122- }
123- #[ cfg( target_os = "linux" ) ]
124- if self . use_ebpf != other. use_ebpf || self . fail_on_ebpf_error != other. fail_on_ebpf_error {
125- return true ;
126- }
113+ self . non_reloadable != other. non_reloadable
114+ }
127115
128- false
116+ pub fn need_reloadable_change ( & self , other : & Self ) -> bool {
117+ self . reloadable != other. reloadable
129118 }
130119
131120 #[ inline]
132121 pub fn address ( & self ) -> SocketAddr {
133- self . address
122+ self . non_reloadable . address
134123 }
135124
136125 #[ cfg( any(
@@ -142,39 +131,39 @@ impl UdpListenConfig {
142131 ) ) ]
143132 #[ inline]
144133 pub fn interface ( & self ) -> Option < & Interface > {
145- self . interface . as_ref ( )
134+ self . non_reloadable . interface . as_ref ( )
146135 }
147136
148137 #[ inline]
149138 pub fn socket_buffer ( & self ) -> SocketBufferConfig {
150- self . buf_conf
139+ self . reloadable . buf_conf
151140 }
152141
153142 #[ inline]
154143 pub fn socket_misc_opts ( & self ) -> UdpMiscSockOpts {
155- self . misc_opts
144+ self . reloadable . misc_opts
156145 }
157146
158147 #[ cfg( not( target_os = "openbsd" ) ) ]
159148 #[ inline]
160149 pub fn is_ipv6only ( & self ) -> Option < bool > {
161- self . ipv6only
150+ self . non_reloadable . ipv6only
162151 }
163152
164153 #[ cfg( target_os = "linux" ) ]
165154 #[ inline]
166155 pub fn transparent ( & self ) -> bool {
167- self . transparent
156+ self . non_reloadable . transparent
168157 }
169158
170159 #[ inline]
171160 pub fn instance ( & self ) -> usize {
172- self . instance . max ( self . scale )
161+ self . non_reloadable . instance . max ( self . non_reloadable . scale )
173162 }
174163
175164 #[ inline]
176165 pub fn set_socket_address ( & mut self , addr : SocketAddr ) {
177- self . address = addr;
166+ self . non_reloadable . address = addr;
178167 }
179168
180169 #[ cfg( any(
@@ -186,48 +175,48 @@ impl UdpListenConfig {
186175 ) ) ]
187176 #[ inline]
188177 pub fn set_interface ( & mut self , interface : Interface ) {
189- self . interface = Some ( interface) ;
178+ self . non_reloadable . interface = Some ( interface) ;
190179 }
191180
192181 #[ inline]
193182 pub fn set_socket_buffer ( & mut self , buf_conf : SocketBufferConfig ) {
194- self . buf_conf = buf_conf;
183+ self . reloadable . buf_conf = buf_conf;
195184 }
196185
197186 #[ inline]
198187 pub fn set_socket_misc_opts ( & mut self , misc_opts : UdpMiscSockOpts ) {
199- self . misc_opts = misc_opts;
188+ self . reloadable . misc_opts = misc_opts;
200189 }
201190
202191 #[ inline]
203192 pub fn set_port ( & mut self , port : u16 ) {
204- self . address . set_port ( port) ;
193+ self . non_reloadable . address . set_port ( port) ;
205194 }
206195
207196 #[ cfg( not( target_os = "openbsd" ) ) ]
208197 #[ inline]
209198 pub fn set_ipv6_only ( & mut self , ipv6only : bool ) {
210- self . ipv6only = Some ( ipv6only) ;
199+ self . non_reloadable . ipv6only = Some ( ipv6only) ;
211200 }
212201
213202 #[ cfg( target_os = "linux" ) ]
214203 #[ inline]
215204 pub fn set_transparent ( & mut self ) {
216- self . transparent = true ;
205+ self . non_reloadable . transparent = true ;
217206 }
218207
219208 pub fn set_instance ( & mut self , instance : usize ) {
220209 if instance == 0 {
221- self . instance = 1 ;
210+ self . non_reloadable . instance = 1 ;
222211 } else {
223- self . instance = instance;
212+ self . non_reloadable . instance = instance;
224213 }
225214 }
226215
227216 pub fn set_scale ( & mut self , scale : f64 ) -> anyhow:: Result < ( ) > {
228217 if let Ok ( p) = std:: thread:: available_parallelism ( ) {
229218 let v = ( p. get ( ) as f64 ) * scale;
230- self . scale = v
219+ self . non_reloadable . scale = v
231220 . round ( )
232221 . to_usize ( )
233222 . ok_or ( anyhow ! ( "out of range result: {v}" ) ) ?;
@@ -238,29 +227,29 @@ impl UdpListenConfig {
238227 pub fn set_fraction_scale ( & mut self , numerator : usize , denominator : usize ) {
239228 if let Ok ( p) = std:: thread:: available_parallelism ( ) {
240229 let v = p. get ( ) * numerator / denominator;
241- self . scale = v;
230+ self . non_reloadable . scale = v;
242231 }
243232 }
244233
245234 #[ cfg( target_os = "linux" ) ]
246235 pub fn use_ebpf ( & self , uid : u32 ) -> bool {
247- self . use_ebpf . unwrap_or ( uid == 0 )
236+ self . non_reloadable . use_ebpf . unwrap_or ( uid == 0 )
248237 }
249238
250239 #[ cfg( target_os = "linux" ) ]
251240 pub fn set_use_ebpf ( & mut self , use_ebpf : bool ) {
252- self . use_ebpf = Some ( use_ebpf) ;
241+ self . non_reloadable . use_ebpf = Some ( use_ebpf) ;
253242 }
254243
255244 #[ cfg( target_os = "linux" ) ]
256245 #[ inline]
257246 pub fn fail_on_ebpf_error ( & self ) -> bool {
258- self . fail_on_ebpf_error
247+ self . non_reloadable . fail_on_ebpf_error
259248 }
260249
261250 #[ cfg( target_os = "linux" ) ]
262251 pub fn set_fail_on_ebpf_error ( & mut self , fail : bool ) {
263- self . fail_on_ebpf_error = fail;
252+ self . non_reloadable . fail_on_ebpf_error = fail;
264253 }
265254}
266255
0 commit comments