1 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "https://www.w3.org/TR/html4/loose.dtd"> 3 4 <html> 5 6 <head> 7 8 <title>Postfix Stress-Dependent Configuration</title> 9 10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 11 <link rel='stylesheet' type='text/css' href='postfix-doc.css'> 12 13 </head> 14 15 <body> 16 17 <h1><img src="postfix-logo.jpg" width="203" height="98" ALT="">Postfix 18 Stress-Dependent Configuration</h1> 19 20 <hr> 21 22 <h2>Overview </h2> 23 24 <p> This document describes the symptoms of Postfix SMTP server 25 overload. It presents permanent main.cf changes to avoid overload 26 during normal operation, and temporary main.cf changes to cope with 27 an unexpected burst of mail. This document makes specific suggestions 28 for Postfix 2.5 and later which support stress-adaptive behavior, 29 and for earlier Postfix versions that don't. </p> 30 31 <p> Topics covered in this document: </p> 32 33 <ul> 34 35 <li><a href="#overload"> Symptoms of Postfix SMTP server overload </a> 36 37 <li><a href="#adapt"> Automatic stress-adaptive behavior </a> 38 39 <li><a href="#concurrency"> Service more SMTP clients at the same time </a> 40 41 <li><a href="#time"> Spend less time per SMTP client </a> 42 43 <li><a href="#hangup"> Disconnect suspicious SMTP clients </a> 44 45 <li><a href="#legacy"> Temporary measures for older Postfix releases </a> 46 47 <li><a href="#feature"> Detecting support for stress-adaptive behavior </a> 48 49 <li><a href="#forcing"> Forcing stress-adaptive behavior on or off </a> 50 51 <li><a href="#other"> Other measures to off-load zombies </a> 52 53 <li><a href="#credits"> Credits </a> 54 55 </ul> 56 57 <h2><a name="overload"> Symptoms of Postfix SMTP server overload </a></h2> 58 59 <p> Under normal conditions, the Postfix SMTP server responds 60 immediately when an SMTP client connects to it; the time to deliver 61 mail is noticeable only with large messages. Performance degrades 62 dramatically when the number of SMTP clients exceeds the number of 63 Postfix SMTP server processes. When an SMTP client connects while 64 all Postfix SMTP server processes are busy, the client must wait 65 until a server process becomes available. </p> 66 67 <p> SMTP server overload may be caused by a surge of legitimate 68 mail (example: a DNS registrar opens a new zone for registrations), 69 by mistake (mail explosion caused by a forwarding loop) or by malice 70 (worm outbreak, botnet, or other illegitimate activity). </p> 71 72 <p> Symptoms of Postfix SMTP server overload are: </p> 73 74 <ul> 75 76 <li> <p> Remote SMTP clients experience a long delay before Postfix 77 sends the "220 hostname.example.com ESMTP Postfix" greeting. </p> 78 79 <ul> 80 81 <li> <p> NOTE: Broken DNS configurations can also cause lengthy 82 delays before Postfix sends "220 hostname.example.com ...". These 83 delays also exist when Postfix is NOT overloaded. </p> 84 85 <li> <p> NOTE: To avoid "overload" delays for end-user mail 86 clients, enable the "submission" service entry in master.cf (present 87 since Postfix 2.1), and tell users to connect to this instead of 88 the public SMTP service. </p> 89 90 </ul> 91 92 <li> <p> The Postfix SMTP server logs an increased number of "lost 93 connection after CONNECT" events. This happens because remote SMTP 94 clients disconnect before Postfix answers the connection. </p> 95 96 <ul> 97 98 <li> <p> NOTE: A portscan for open SMTP ports can also result in 99 "lost connection ..." logfile messages. </p> 100 101 </ul> 102 103 <li> <p> Postfix 2.3 and later logs a warning that all server ports 104 are busy: </p> 105 106 <pre> 107 Oct 3 20:39:27 spike postfix/master[28905]: warning: service "smtp" 108 (25) has reached its process limit "30": new clients may experience 109 noticeable delays 110 Oct 3 20:39:27 spike postfix/master[28905]: warning: to avoid this 111 condition, increase the process count in master.cf or reduce the 112 service time per client 113 Oct 3 20:39:27 spike postfix/master[28905]: warning: see 114 <a href="https://www.postfix.org/STRESS_README.html">https://www.postfix.org/STRESS_README.html</a> for examples of 115 stress-adapting configuration settings 116 </pre> 117 118 </ul> 119 120 <p> Legitimate mail that doesn't get through during an episode of 121 Postfix SMTP server overload is not necessarily lost. It should 122 still arrive once the situation returns to normal, as long as the 123 overload condition is temporary. </p> 124 125 <h2><a name="adapt"> Automatic stress-adaptive behavior </a></h2> 126 127 <p> Postfix version 2.5 introduces automatic stress-adaptive behavior. 128 It works as follows. When a "public" network service such as the 129 SMTP server runs into an "all server ports are busy" condition, the 130 Postfix master(8) daemon logs a warning, restarts the service 131 (without interrupting existing network sessions), and runs the 132 service with "-o stress=yes" on the server process command line: 133 </p> 134 135 <blockquote> 136 <pre> 137 80821 ?? S 0:00.24 smtpd -n smtp -t inet -u -c -o stress=yes 138 </pre> 139 </blockquote> 140 141 <p> Normally, the Postfix master(8) daemon runs such a service with 142 "-o stress=" on the command line (i.e. with an empty parameter 143 value): </p> 144 145 <blockquote> 146 <pre> 147 83326 ?? S 0:00.28 smtpd -n smtp -t inet -u -c -o stress= 148 </pre> 149 </blockquote> 150 151 <p> You won't see "-o stress" command-line parameters with services 152 that have local clients only. These include services internal to 153 Postfix such as the queue manager, and services that listen on a 154 loopback interface only, such as after-filter SMTP services. </p> 155 156 <p> The "stress" parameter value is the key to making main.cf 157 parameter settings stress adaptive. The following settings are the 158 default with Postfix 2.6 and later. </p> 159 160 <blockquote> 161 <pre> 162 1 smtpd_timeout = ${stress?{10}:{300}}s 163 2 smtpd_hard_error_limit = ${stress?{1}:{20}} 164 3 smtpd_junk_command_limit = ${stress?{1}:{100}} 165 4 # Parameters added after Postfix 2.6: 166 5 smtpd_per_record_deadline = ${stress?{yes}:{no}} 167 6 smtpd_starttls_timeout = ${stress?{10}:{300}}s 168 7 address_verify_poll_count = ${stress?{1}:{3}} 169 </pre> 170 </blockquote> 171 172 <p> Postfix versions before 3.0 use the older form ${stress?x}${stress:y} 173 instead of the newer form ${stress?{x}:{y}}. </p> 174 175 <p> The syntax of ${name?{value}:{value}}, ${name?value} and 176 ${name:value} is explained at the beginning of the postconf(5) 177 manual page. </p> 178 179 <p> Translation: <p> 180 181 <ul> 182 183 <li> <p> Line 1: under conditions of stress, use an smtpd_timeout 184 value of 10 seconds instead of the default 300 seconds. Experience 185 on the postfix-users list from a variety of sysadmins shows that 186 reducing the "normal" smtpd_timeout to 60s is unlikely to affect 187 legitimate clients. However, it is unlikely to become the Postfix 188 default because it's not RFC compliant. Setting smtpd_timeout to 189 10s or even 5s under stress will still allow most 190 legitimate clients to connect and send mail, but may delay mail 191 from some clients. No mail should be lost, as long as this measure 192 is used only temporarily. </p> 193 194 <li> <p> Line 2: under conditions of stress, use an smtpd_hard_error_limit 195 of 1 instead of the default 20. This disconnects clients 196 after a single error, giving other clients a chance to connect. 197 However, this may cause significant delays with legitimate mail, 198 such as a mailing list that contains a few no-longer-active user 199 names that didn't bother to unsubscribe. No mail should be lost, 200 as long as this measure is used only temporarily. </p> 201 202 <li> <p> Line 3: under conditions of stress, use an 203 smtpd_junk_command_limit of 1 instead of the default 100. This 204 prevents clients from keeping connections open by repeatedly 205 sending HELO, EHLO, NOOP, RSET, VRFY or ETRN commands. </p> 206 207 <li> <p> Line 5: under conditions of stress, change the behavior 208 of smtpd_timeout and smtpd_starttls_timeout, from a time limit per 209 read or write system call, to a time limit to send or receive a 210 complete record (an SMTP command line, SMTP response line, SMTP 211 message content line, or TLS protocol message). </p> 212 213 <li> <p> Line 6: under conditions of stress, reduce the time limit 214 for TLS protocol handshake messages to 10 seconds, from the default 215 value of 300 seconds. See also the smtpd_timeout discussion above. 216 </p> 217 218 <li> <p> Line 7: under conditions of stress, do not wait up to 6 219 seconds for the completion of an address verification probe. If the 220 result is not already in the address verification cache, reply 221 immediately with $unverified_recipient_tempfail_action or 222 $unverified_sender_tempfail_action. No mail should be lost, as long 223 as this measure is used only temporarily. </p> 224 225 </ul> 226 227 <p> NOTE: Please keep in mind that the stress-adaptive feature is 228 a fairly desperate measure to keep <b>some</b> legitimate mail 229 flowing under overload conditions. If a site is reaching the SMTP 230 server process limit when there isn't an attack or bot flood 231 occurring, then either the process limit needs to be raised or more 232 hardware needs to be added. </p> 233 234 <h2><a name="concurrency"> Service more SMTP clients at the same time </a> </h2> 235 236 <p> This section and the ones that follow discuss permanent measures 237 against mail server overload. </p> 238 239 <p> One measure to avoid the "all server processes busy" condition 240 is to service more SMTP clients simultaneously. For this you need 241 to increase the number of Postfix SMTP server processes. This will 242 improve the 243 responsiveness for remote SMTP clients, as long as the server machine 244 has enough hardware and software resources to run the additional 245 processes, and as long as the file system can keep up with the 246 additional load. </p> 247 248 <ul> 249 250 <li> <p> You increase the number of SMTP server processes either 251 by increasing the default_process_limit in main.cf (line 3 below), 252 or by increasing the SMTP server's "maxproc" field in master.cf 253 (line 10 below). Either way, you need to issue a "postfix reload" 254 command to make the change effective. </p> 255 256 <li> <p> Process limits above 1000 require Postfix version 2.4 or 257 later, and an operating system that supports kernel-based event 258 filters (BSD kqueue(2), Linux epoll(4), or Solaris /dev/poll). 259 </p> 260 261 <li> <p> More processes use more memory. You can reduce the Postfix 262 memory footprint by using cdb: 263 lookup tables instead of Berkeley DB's hash: or btree: tables. </p> 264 265 <pre> 266 1 /etc/postfix/main.cf: 267 2 # Raise the global process limit, 100 since Postfix 2.0. 268 3 default_process_limit = 200 269 4 270 5 /etc/postfix/master.cf: 271 6 # ============================================================= 272 7 # service type private unpriv chroot wakeup maxproc command 273 8 # ============================================================= 274 9 # Raise the SMTP service process limit only. 275 10 smtp inet n - n - 200 smtpd 276 </pre> 277 278 <li> <p> NOTE: older versions of the SMTPD_POLICY_README document 279 contain a mistake: they configure a fixed number of policy daemon 280 processes. When you raise the SMTP server's "maxproc" field in 281 master.cf, SMTP server processes will report problems when connecting 282 to policy server processes, because there aren't enough of them. 283 Examples of errors are "connection refused" or "operation timed 284 out". </p> 285 286 <p> To fix, edit master.cf and specify a zero "maxproc" field 287 in all policy server entries; see line 6 in the example below. 288 Issue a "postfix reload" command to make the change effective. </p> 289 290 <pre> 291 1 /etc/postfix/master.cf: 292 2 # ============================================================= 293 3 # service type private unpriv chroot wakeup maxproc command 294 4 # ============================================================= 295 5 # Disable the policy service process limit. 296 6 policy unix - n n - 0 spawn 297 7 user=nobody argv=/some/where/policy-server 298 </pre> 299 300 </ul> 301 302 <h2><a name="time"> Spend less time per SMTP client </a></h2> 303 304 <p> When increasing the number of SMTP server processes is not 305 practical, you can improve Postfix server responsiveness by eliminating 306 delays. When Postfix spends less time per SMTP session, the same 307 number of SMTP server processes can service more clients in a given 308 amount of time. </p> 309 310 <ul> 311 312 <li> <p> Eliminate non-functional RBL lookups (blocklists that are 313 no longer in operation). These lookups can degrade performance. 314 Postfix logs a warning when an RBL server does not respond. </p> 315 316 <li> <p> Eliminate redundant RBL lookups (people often use multiple 317 Spamhaus RBLs that include each other). To find out whether RBLs 318 include other RBLs, look up the websites that document the RBL's 319 policies. </p> 320 321 <li> <p> Eliminate header_checks and body_checks, and keep just a few 322 emergency patterns to block the latest worm explosion or backscatter 323 mail. See BACKSCATTER_README for examples of the latter. 324 325 <li> <p> Group your header_checks and body_checks patterns to avoid 326 unnecessary pattern matching operations: 327 328 <pre> 329 1 /etc/postfix/header_checks: 330 2 if /^Subject:/ 331 3 /^Subject: virus found in mail from you/ reject 332 4 /^Subject: ..other../ reject 333 5 endif 334 6 335 7 if /^Received:/ 336 8 /^Received: from (postfix\.org) / reject forged client name in received header: $1 337 9 /^Received: from ..other../ reject .... 338 10 endif 339 </pre> 340 341 </ul> 342 343 <h2><a name="hangup"> Disconnect suspicious SMTP clients </a></h2> 344 345 <p> Under conditions of overload you can improve Postfix SMTP server 346 responsiveness by hanging up on suspicious clients, so that other 347 clients get a chance to talk to Postfix. </p> 348 349 <ul> 350 351 <li> <p> Use "521" SMTP reply codes (Postfix 2.6 and later) or "421" 352 (Postfix 2.3-2.5) to hang up on clients that that match botnet-related 353 RBLs (see next bullet) or that match selected non-RBL restrictions 354 such as SMTP access maps. The Postfix SMTP server will reject mail 355 and disconnect without waiting for the remote SMTP client to send 356 a QUIT command. </p> 357 358 <li> <p> To hang up connections from denylisted zombies, you can 359 set specific Postfix SMTP server reject codes for specific RBLs, 360 and for individual responses from specific RBLs. We'll use 361 zen.spamhaus.org as an example; by the time you read this document, 362 details may have changed. Right now, their documents say that a 363 response of 127.0.0.10 or 127.0.0.11 indicates a dynamic client IP 364 address, which means that the machine is probably running a bot of 365 some kind. To give a 521 response instead of the default 554 366 response, use something like: </p> 367 368 <pre> 369 1 /etc/postfix/main.cf: 370 2 smtpd_client_restrictions = 371 3 permit_mynetworks 372 4 reject_rbl_client zen.spamhaus.org=127.0.0.10 373 5 reject_rbl_client zen.spamhaus.org=127.0.0.11 374 6 reject_rbl_client zen.spamhaus.org 375 7 376 8 rbl_reply_maps = hash:/etc/postfix/rbl_reply_maps 377 9 378 10 /etc/postfix/rbl_reply_maps: 379 11 # With Postfix 2.3-2.5 use "421" to hang up connections. 380 12 zen.spamhaus.org=127.0.0.10 521 4.7.1 Service unavailable; 381 13 $rbl_class [$rbl_what] blocked using 382 14 $rbl_domain${rbl_reason?; $rbl_reason} 383 15 384 16 zen.spamhaus.org=127.0.0.11 521 4.7.1 Service unavailable; 385 17 $rbl_class [$rbl_what] blocked using 386 18 $rbl_domain${rbl_reason?; $rbl_reason} 387 </pre> 388 389 <p> Although the above example shows three RBL lookups (lines 4-6), 390 Postfix will only do a single DNS query, so it does not affect the 391 performance. </p> 392 393 <li> <p> With Postfix 2.3-2.5, use reply code 421 (521 will not 394 cause Postfix to disconnect). The down-side of replying with 421 395 is that it works only for zombies and other malware. If the client 396 is running a real MTA, then it may connect again several times until 397 the mail expires in its queue. When this is a problem, stick with 398 the default 554 reply, and use "smtpd_hard_error_limit = 1" as 399 described below. </p> 400 401 <li> <p> You can automatically turn on the above overload measure 402 with Postfix 2.5 and later, or with earlier releases that contain 403 the stress-adaptive behavior source code patch from the mirrors 404 listed at https://www.postfix.org/download.html. Simply replace line 405 above 8 with: </p> 406 407 <pre> 408 8 rbl_reply_maps = ${stress?hash:/etc/postfix/rbl_reply_maps} 409 </pre> 410 411 </ul> 412 413 <p> More information about automatic stress-adaptive behavior is 414 in section "<a href="#adapt">Automatic stress-adaptive behavior</a>". 415 </p> 416 417 <h2><a name="legacy"> Temporary measures for older Postfix releases </a></h2> 418 419 <p> See the section "<a href="#adapt">Automatic stress-adaptive 420 behavior</a>" if you are running Postfix version 2.5 or later, or 421 if you have applied the source code patch for stress-adaptive 422 behavior from the mirrors listed at https://www.postfix.org/download.html. 423 </p> 424 425 <p> The following measures can be applied temporarily during overload. 426 They still allow <b>most</b> legitimate clients to connect and send 427 mail, but may affect some legitimate clients. </p> 428 429 <ul> 430 431 <li> <p> Reduce smtpd_timeout (default: 300s). Experience on the 432 postfix-users list from a variety of sysadmins shows that reducing 433 the "normal" smtpd_timeout to 60s is unlikely to affect legitimate 434 clients. However, it is unlikely to become the Postfix default 435 because it's not RFC compliant. Setting smtpd_timeout to 10s (line 436 2 below) or even 5s under stress will still allow <b>most</b> 437 legitimate clients to connect and send mail, but may delay mail 438 from some clients. No mail should be lost, as long as this measure 439 is used only temporarily. </p> 440 441 <li> <p> Reduce smtpd_hard_error_limit (default: 20). Setting this 442 to 1 under stress (line 3 below) helps by disconnecting clients 443 after a single error, giving other clients a chance to connect. 444 However, this may cause significant delays with legitimate mail, 445 such as a mailing list that contains a few no-longer-active user 446 names that didn't bother to unsubscribe. No mail should be lost, 447 as long as this measure is used only temporarily. </p> 448 449 <li> <p> Use an smtpd_junk_command_limit of 1 instead of the default 450 100. This prevents clients from keeping idle connections open by 451 repeatedly sending NOOP or RSET commands. </p> 452 453 </ul> 454 455 <blockquote> 456 <pre> 457 1 /etc/postfix/main.cf: 458 2 smtpd_timeout = 10 459 3 smtpd_hard_error_limit = 1 460 4 smtpd_junk_command_limit = 1 461 </pre> 462 </blockquote> 463 464 <p> With these measures, no mail should be lost, as long 465 as these measures are used only temporarily. The next section of 466 this document introduces a way to automate this process. </p> 467 468 <h2><a name="feature"> Detecting support for stress-adaptive behavior </a></h2> 469 470 <p> To find out if your Postfix installation supports stress-adaptive 471 behavior, use the "ps" command, and look for the smtpd processes. 472 Postfix has stress-adaptive support when you see "-o stress=" or 473 "-o stress=yes" command-line options. Remember that Postfix never 474 enables stress-adaptive behavior on servers that listen on local 475 addresses only. </p> 476 477 <p> The following example is for FreeBSD or Linux. On Solaris, HP-UX 478 and other System-V flavors, use "ps -ef" instead of "ps ax". </p> 479 480 <blockquote> 481 <pre> 482 $ ps ax|grep smtpd 483 83326 ?? S 0:00.28 smtpd -n smtp -t inet -u -c -o stress= 484 84345 ?? Ss 0:00.11 /usr/bin/perl /usr/libexec/postfix/smtpd-policy.pl 485 </pre> 486 </blockquote> 487 488 <p> You can't use postconf(1) to detect stress-adaptive support. 489 The postconf(1) command ignores the existence of the stress parameter 490 in main.cf, because the parameter has no effect there. Command-line 491 "-o parameter" settings always take precedence over main.cf parameter 492 settings. <p> 493 494 <p> If you configure stress-adaptive behavior in main.cf when it 495 isn't supported, nothing bad will happen. The processes will run 496 as if the stress parameter always has an empty value. </p> 497 498 <h2><a name="forcing"> Forcing stress-adaptive behavior on or off </a></h2> 499 500 <p> You can manually force stress-adaptive behavior on, by adding 501 a "-o stress=yes" command-line option in master.cf. This can be 502 useful for testing overrides on the SMTP service. Issue "postfix 503 reload" to make the change effective. </p> 504 505 <p> Note: setting the stress parameter in main.cf has no effect for 506 services that accept remote connections. </p> 507 508 <blockquote> 509 <pre> 510 1 /etc/postfix/master.cf: 511 2 # ============================================================= 512 3 # service type private unpriv chroot wakeup maxproc command 513 4 # ============================================================= 514 5 # 515 6 smtp inet n - n - - smtpd 516 7 -o stress=yes 517 8 -o . . . 518 </pre> 519 </blockquote> 520 521 <p> To permanently force stress-adaptive behavior off with a specific 522 service, specify "-o stress=" on its master.cf command line. This 523 may be desirable for the "submission" service. Issue "postfix reload" 524 to make the change effective. </p> 525 526 <p> Note: setting the stress parameter in main.cf has no effect for 527 services that accept remote connections. </p> 528 529 <blockquote> 530 <pre> 531 1 /etc/postfix/master.cf: 532 2 # ============================================================= 533 3 # service type private unpriv chroot wakeup maxproc command 534 4 # ============================================================= 535 5 # 536 6 submission inet n - n - - smtpd 537 7 -o stress= 538 8 -o . . . 539 </pre> 540 </blockquote> 541 542 <h2><a name="other"> Other measures to off-load zombies </a> </h2> 543 544 <p> The postscreen(8) daemon, introduced with Postfix 2.8, provides 545 additional protection against mail server overload. One postscreen(8) 546 process handles multiple inbound SMTP connections, and decides which 547 clients may talk to a Postfix SMTP server process. By keeping 548 spambots away, postscreen(8) leaves more SMTP server processes 549 available for legitimate clients, and delays the onset of server 550 overload conditions. </p> 551 552 <h2><a name="credits"> Credits </a></h2> 553 554 <ul> 555 556 <li> Thanks to the postfix-users mailing list members for sharing 557 early experiences with the stress-adaptive feature. 558 559 <li> The RBL example and several other paragraphs of text were 560 adapted from postfix-users postings by Noel Jones. 561 562 <li> Wietse implemented stress-adaptive behavior as the smallest 563 possible patch while he should be working on other things. 564 565 </ul> 566 567 </body> </html> 568