Postfix3.3.1
qmgr_transport.c
[詳解]
1 /*++
2 /* NAME
3 /* qmgr_transport 3
4 /* SUMMARY
5 /* per-transport data structures
6 /* SYNOPSIS
7 /* #include "qmgr.h"
8 /*
9 /* QMGR_TRANSPORT *qmgr_transport_create(name)
10 /* const char *name;
11 /*
12 /* QMGR_TRANSPORT *qmgr_transport_find(name)
13 /* const char *name;
14 /*
15 /* QMGR_TRANSPORT *qmgr_transport_select()
16 /*
17 /* void qmgr_transport_alloc(transport, notify)
18 /* QMGR_TRANSPORT *transport;
19 /* void (*notify)(QMGR_TRANSPORT *transport, VSTREAM *fp);
20 /*
21 /* void qmgr_transport_throttle(transport, dsn)
22 /* QMGR_TRANSPORT *transport;
23 /* DSN *dsn;
24 /*
25 /* void qmgr_transport_unthrottle(transport)
26 /* QMGR_TRANSPORT *transport;
27 /* DESCRIPTION
28 /* This module organizes the world by message transport type.
29 /* Each transport can have zero or more destination queues
30 /* associated with it.
31 /*
32 /* qmgr_transport_create() instantiates a data structure for the
33 /* named transport type.
34 /*
35 /* qmgr_transport_find() looks up an existing message transport
36 /* data structure.
37 /*
38 /* qmgr_transport_select() attempts to find a transport that
39 /* has messages pending delivery. This routine implements
40 /* round-robin search among transports.
41 /*
42 /* qmgr_transport_alloc() allocates a delivery process for the
43 /* specified transport type. Allocation is performed asynchronously.
44 /* When a process becomes available, the application callback routine
45 /* is invoked with as arguments the transport and a stream that
46 /* is connected to a delivery process. It is an error to call
47 /* qmgr_transport_alloc() while delivery process allocation for
48 /* the same transport is in progress.
49 /*
50 /* qmgr_transport_throttle blocks further allocation of delivery
51 /* processes for the named transport. Attempts to throttle a
52 /* throttled transport are ignored.
53 /*
54 /* qmgr_transport_unthrottle() undoes qmgr_transport_throttle().
55 /* Attempts to unthrottle a non-throttled transport are ignored.
56 /* DIAGNOSTICS
57 /* Panic: consistency check failure. Fatal: out of memory.
58 /* LICENSE
59 /* .ad
60 /* .fi
61 /* The Secure Mailer license must be distributed with this software.
62 /* AUTHOR(S)
63 /* Wietse Venema
64 /* IBM T.J. Watson Research
65 /* P.O. Box 704
66 /* Yorktown Heights, NY 10598, USA
67 /*
68 /* Wietse Venema
69 /* Google, Inc.
70 /* 111 8th Avenue
71 /* New York, NY 10011, USA
72 /*--*/
73 
74 /* System library. */
75 
76 #include <sys_defs.h>
77 #include <unistd.h>
78 
79 #include <sys/time.h> /* FD_SETSIZE */
80 #include <sys/types.h> /* FD_SETSIZE */
81 #include <unistd.h> /* FD_SETSIZE */
82 
83 #ifdef USE_SYS_SELECT_H
84 #include <sys/select.h> /* FD_SETSIZE */
85 #endif
86 
87 /* Utility library. */
88 
89 #include <msg.h>
90 #include <htable.h>
91 #include <events.h>
92 #include <mymalloc.h>
93 #include <vstream.h>
94 #include <iostuff.h>
95 
96 /* Global library. */
97 
98 #include <mail_proto.h>
99 #include <recipient_list.h>
100 #include <mail_conf.h>
101 #include <mail_params.h>
102 
103 /* Application-specific. */
104 
105 #include "qmgr.h"
106 
107 HTABLE *qmgr_transport_byname; /* transport by name */
108 QMGR_TRANSPORT_LIST qmgr_transport_list;/* transports, round robin */
109 
110  /*
111  * A local structure to remember a delivery process allocation request.
112  */
114 
116  QMGR_TRANSPORT *transport; /* transport context */
117  VSTREAM *stream; /* delivery service stream */
118  QMGR_TRANSPORT_ALLOC_NOTIFY notify; /* application call-back routine */
119 };
120 
121  /*
122  * Connections to delivery agents are managed asynchronously. Each delivery
123  * agent connection goes through multiple wait states:
124  *
125  * - With Linux/Solaris and old queue manager implementations only, wait for
126  * the server to invoke accept().
127  *
128  * - Wait for the delivery agent's announcement that it is ready to receive a
129  * delivery request.
130  *
131  * - Wait for the delivery request completion status.
132  *
133  * Older queue manager implementations had only one pending delivery agent
134  * connection per transport. With low-latency destinations, the output rates
135  * were reduced on Linux/Solaris systems that had the extra wait state.
136  *
137  * To maximize delivery agent output rates with low-latency destinations, the
138  * following changes were made to the queue manager by the end of the 2.4
139  * development cycle:
140  *
141  * - The Linux/Solaris accept() wait state was eliminated.
142  *
143  * - A pipeline was implemented for pending delivery agent connections. The
144  * number of pending delivery agent connections was increased from one to
145  * two: the number of before-delivery wait states, plus one extra pipeline
146  * slot to prevent the pipeline from stalling easily. Increasing the
147  * pipeline much further actually hurt performance.
148  *
149  * - To reduce queue manager disk competition with delivery agents, the queue
150  * scanning algorithm was modified to import only one message per interrupt.
151  * The incoming and deferred queue scans now happen on alternate interrupts.
152  *
153  * Simplistically reasoned, a non-zero (incoming + active) queue length is
154  * equivalent to a time shift for mail deliveries; this is undesirable when
155  * delivery agents are not fully utilized.
156  *
157  * On the other hand a non-empty active queue is what allows us to do clever
158  * things such as queue file prefetch, concurrency windows, and connection
159  * caching; the idea is that such "thinking time" is affordable only after
160  * the output channels are maxed out.
161  */
162 #ifndef QMGR_TRANSPORT_MAX_PEND
163 #define QMGR_TRANSPORT_MAX_PEND 2
164 #endif
165 
166  /*
167  * Important note on the _transport_rate_delay implementation: after
168  * qmgr_transport_alloc() sets the QMGR_TRANSPORT_STAT_RATE_LOCK flag, all
169  * code paths must directly or indirectly invoke qmgr_transport_unthrottle()
170  * or qmgr_transport_throttle(). Otherwise, transports with non-zero
171  * _transport_rate_delay will become stuck.
172  */
173 
174 /* qmgr_transport_unthrottle_wrapper - in case (char *) != (struct *) */
175 
176 static void qmgr_transport_unthrottle_wrapper(int unused_event, void *context)
177 {
179 }
180 
181 /* qmgr_transport_unthrottle - open the throttle */
182 
184 {
185  const char *myname = "qmgr_transport_unthrottle";
186 
187  /*
188  * This routine runs after expiration of the timer set by
189  * qmgr_transport_throttle(), or whenever a delivery transport has been
190  * used without malfunction. In either case, we enable delivery again if
191  * the transport was throttled. We always reset the transport rate lock.
192  */
193  if ((transport->flags & QMGR_TRANSPORT_STAT_DEAD) != 0) {
194  if (msg_verbose)
195  msg_info("%s: transport %s", myname, transport->name);
196  transport->flags &= ~QMGR_TRANSPORT_STAT_DEAD;
197  if (transport->dsn == 0)
198  msg_panic("%s: transport %s: null reason",
199  myname, transport->name);
200  dsn_free(transport->dsn);
201  transport->dsn = 0;
202  event_cancel_timer(qmgr_transport_unthrottle_wrapper,
203  (void *) transport);
204  }
205  if (transport->flags & QMGR_TRANSPORT_STAT_RATE_LOCK)
206  transport->flags &= ~QMGR_TRANSPORT_STAT_RATE_LOCK;
207 }
208 
209 /* qmgr_transport_throttle - disable delivery process allocation */
210 
212 {
213  const char *myname = "qmgr_transport_throttle";
214 
215  /*
216  * We are unable to connect to a deliver process for this type of message
217  * transport. Instead of hosing the system by retrying in a tight loop,
218  * back off and disable this transport type for a while.
219  */
220  if ((transport->flags & QMGR_TRANSPORT_STAT_DEAD) == 0) {
221  if (msg_verbose)
222  msg_info("%s: transport %s: status: %s reason: %s",
223  myname, transport->name, dsn->status, dsn->reason);
224  transport->flags |= QMGR_TRANSPORT_STAT_DEAD;
225  if (transport->dsn)
226  msg_panic("%s: transport %s: spurious reason: %s",
227  myname, transport->name, transport->dsn->reason);
228  transport->dsn = DSN_COPY(dsn);
229  event_request_timer(qmgr_transport_unthrottle_wrapper,
230  (void *) transport, var_transport_retry_time);
231  }
232 }
233 
234 /* qmgr_transport_abort - transport connect watchdog */
235 
236 static void qmgr_transport_abort(int unused_event, void *context)
237 {
238  QMGR_TRANSPORT_ALLOC *alloc = (QMGR_TRANSPORT_ALLOC *) context;
239 
240  msg_fatal("timeout connecting to transport: %s", alloc->transport->name);
241 }
242 
243 /* qmgr_transport_rate_event - delivery process availability notice */
244 
245 static void qmgr_transport_rate_event(int unused_event, void *context)
246 {
247  QMGR_TRANSPORT_ALLOC *alloc = (QMGR_TRANSPORT_ALLOC *) context;
248 
249  alloc->notify(alloc->transport, alloc->stream);
250  myfree((void *) alloc);
251 }
252 
253 /* qmgr_transport_event - delivery process availability notice */
254 
255 static void qmgr_transport_event(int unused_event, void *context)
256 {
257  QMGR_TRANSPORT_ALLOC *alloc = (QMGR_TRANSPORT_ALLOC *) context;
258 
259  /*
260  * This routine notifies the application when the request given to
261  * qmgr_transport_alloc() completes.
262  */
263  if (msg_verbose)
264  msg_info("transport_event: %s", alloc->transport->name);
265 
266  /*
267  * Connection request completed. Stop the watchdog timer.
268  */
269  event_cancel_timer(qmgr_transport_abort, context);
270 
271  /*
272  * Disable further read events that end up calling this function, and
273  * free up this pending connection pipeline slot.
274  */
275  if (alloc->stream) {
278  }
279  alloc->transport->pending -= 1;
280 
281  /*
282  * Notify the requestor.
283  */
284  if (alloc->transport->xport_rate_delay > 0) {
285  if ((alloc->transport->flags & QMGR_TRANSPORT_STAT_RATE_LOCK) == 0)
286  msg_panic("transport_event: missing rate lock for transport %s",
287  alloc->transport->name);
288  event_request_timer(qmgr_transport_rate_event, (void *) alloc,
289  alloc->transport->xport_rate_delay);
290  } else {
291  alloc->notify(alloc->transport, alloc->stream);
292  myfree((void *) alloc);
293  }
294 }
295 
296 /* qmgr_transport_select - select transport for allocation */
297 
299 {
300  QMGR_TRANSPORT *xport;
301  QMGR_QUEUE *queue;
302  int need;
303 
304  /*
305  * If we find a suitable transport, rotate the list of transports to
306  * effectuate round-robin selection. See similar selection code in
307  * qmgr_queue_select().
308  *
309  * This function is called repeatedly until all transports have maxed out
310  * the number of pending delivery agent connections, until all delivery
311  * agent concurrency windows are maxed out, or until we run out of "todo"
312  * queue entries.
313  */
314 #define MIN5af51743e4eef(x, y) ((x) < (y) ? (x) : (y))
315 
316  for (xport = qmgr_transport_list.next; xport; xport = xport->peers.next) {
317  if ((xport->flags & QMGR_TRANSPORT_STAT_DEAD) != 0
318  || (xport->flags & QMGR_TRANSPORT_STAT_RATE_LOCK) != 0
319  || xport->pending >= QMGR_TRANSPORT_MAX_PEND)
320  continue;
321  need = xport->pending + 1;
322  for (queue = xport->queue_list.next; queue; queue = queue->peers.next) {
323  if (QMGR_QUEUE_READY(queue) == 0)
324  continue;
325  if ((need -= MIN5af51743e4eef(queue->window - queue->busy_refcount,
326  queue->todo_refcount)) <= 0) {
327  QMGR_LIST_ROTATE(qmgr_transport_list, xport);
328  if (msg_verbose)
329  msg_info("qmgr_transport_select: %s", xport->name);
330  return (xport);
331  }
332  }
333  }
334  return (0);
335 }
336 
337 /* qmgr_transport_alloc - allocate delivery process */
338 
340 {
341  QMGR_TRANSPORT_ALLOC *alloc;
342 
343  /*
344  * Sanity checks.
345  */
346  if (transport->flags & QMGR_TRANSPORT_STAT_DEAD)
347  msg_panic("qmgr_transport: dead transport: %s", transport->name);
348  if (transport->flags & QMGR_TRANSPORT_STAT_RATE_LOCK)
349  msg_panic("qmgr_transport: rate-locked transport: %s", transport->name);
350  if (transport->pending >= QMGR_TRANSPORT_MAX_PEND)
351  msg_panic("qmgr_transport: excess allocation: %s", transport->name);
352 
353  /*
354  * When this message delivery transport is rate-limited, do not select it
355  * again before the end of a message delivery transaction.
356  */
357  if (transport->xport_rate_delay > 0)
358  transport->flags |= QMGR_TRANSPORT_STAT_RATE_LOCK;
359 
360  /*
361  * Connect to the well-known port for this delivery service, and wake up
362  * when a process announces its availability. Allow only a limited number
363  * of delivery process allocation attempts for this transport. In case of
364  * problems, back off. Do not hose the system when it is in trouble
365  * already.
366  *
367  * Use non-blocking connect(), so that Linux won't block the queue manager
368  * until the delivery agent calls accept().
369  *
370  * When the connection to delivery agent cannot be completed, notify the
371  * event handler so that it can throttle the transport and defer the todo
372  * queues, just like it does when communication fails *after* connection
373  * completion.
374  *
375  * Before Postfix 2.4, the event handler was not invoked after connect()
376  * error, and mail was not deferred. Because of this, mail would be stuck
377  * in the active queue after triggering a "connection refused" condition.
378  */
379  alloc = (QMGR_TRANSPORT_ALLOC *) mymalloc(sizeof(*alloc));
380  alloc->transport = transport;
381  alloc->notify = notify;
382  transport->pending += 1;
383  if ((alloc->stream = mail_connect(MAIL_CLASS_PRIVATE, transport->name,
384  NON_BLOCKING)) == 0) {
385  msg_warn("connect to transport %s/%s: %m",
386  MAIL_CLASS_PRIVATE, transport->name);
387  event_request_timer(qmgr_transport_event, (void *) alloc, 0);
388  return;
389  }
390 #if (EVENTS_STYLE != EVENTS_STYLE_SELECT) && defined(CA_VSTREAM_CTL_DUPFD)
391 #ifndef THRESHOLD_FD_WORKAROUND
392 #define THRESHOLD_FD_WORKAROUND 128
393 #endif
394  vstream_control(alloc->stream,
395  CA_VSTREAM_CTL_DUPFD(THRESHOLD_FD_WORKAROUND),
397 #endif
398  event_enable_read(vstream_fileno(alloc->stream), qmgr_transport_event,
399  (void *) alloc);
400 
401  /*
402  * Guard against broken systems.
403  */
404  event_request_timer(qmgr_transport_abort, (void *) alloc,
406 }
407 
408 /* qmgr_transport_create - create transport instance */
409 
411 {
412  QMGR_TRANSPORT *transport;
413 
414  if (htable_find(qmgr_transport_byname, name) != 0)
415  msg_panic("qmgr_transport_create: transport exists: %s", name);
416  transport = (QMGR_TRANSPORT *) mymalloc(sizeof(QMGR_TRANSPORT));
417  transport->flags = 0;
418  transport->pending = 0;
419  transport->name = mystrdup(name);
420 
421  /*
422  * Use global configuration settings or transport-specific settings.
423  */
424  transport->dest_concurrency_limit =
426  var_dest_con_limit, 0, 0);
427  transport->recipient_limit =
429  var_dest_rcpt_limit, 0, 0);
430  transport->init_dest_concurrency =
435  's', 0, 0);
438  's', 0, 0);
439 
440  if (transport->rate_delay > 0)
441  transport->dest_concurrency_limit = 1;
442  if (transport->dest_concurrency_limit != 0
443  && transport->dest_concurrency_limit < transport->init_dest_concurrency)
444  transport->init_dest_concurrency = transport->dest_concurrency_limit;
445 
446  transport->queue_byname = htable_create(0);
447  QMGR_LIST_INIT(transport->queue_list);
448  transport->dsn = 0;
453  transport->fail_cohort_limit =
455  var_conc_cohort_limit, 0, 0);
456  if (qmgr_transport_byname == 0)
457  qmgr_transport_byname = htable_create(10);
458  htable_enter(qmgr_transport_byname, name, (void *) transport);
459  QMGR_LIST_APPEND(qmgr_transport_list, transport);
460  if (msg_verbose)
461  msg_info("qmgr_transport_create: %s concurrency %d recipients %d",
462  transport->name, transport->dest_concurrency_limit,
463  transport->recipient_limit);
464  return (transport);
465 }
466 
467 /* qmgr_transport_find - find transport instance */
468 
470 {
471  return ((QMGR_TRANSPORT *) htable_find(qmgr_transport_byname, name));
472 }
int msg_verbose
Definition: msg.c:177
void event_enable_read(int fd, EVENT_NOTIFY_RDWR_FN callback, void *context)
Definition: events.c:729
QMGR_TRANSPORT * next
Definition: qmgr.h:98
int dest_concurrency_limit
Definition: qmgr.h:156
void myfree(void *ptr)
Definition: mymalloc.c:207
#define _CONC_NEG_FDBACK
Definition: mail_params.h:3503
int pending
Definition: qmgr.h:154
char * mystrdup(const char *str)
Definition: mymalloc.c:225
HTABLE * qmgr_transport_byname
int fail_cohort_limit
Definition: qmgr.h:165
#define MIN5af51743e4eef(x, y)
NORETURN msg_panic(const char *fmt,...)
Definition: msg.c:295
const char * reason
Definition: dsn.h:20
#define _CONC_POS_FDBACK
Definition: mail_params.h:3498
VSTREAM * mail_connect(const char *class, const char *name, int block_mode)
Definition: mail_connect.c:79
#define _CONC_COHORT_LIM
Definition: mail_params.h:3511
#define VAR_CONC_NEG_FDBACK
Definition: mail_params.h:3502
QMGR_TRANSPORT * qmgr_transport_find(const char *name)
#define DSN_COPY(dsn)
Definition: dsn.h:68
QMGR_FEEDBACK neg_feedback
Definition: qmgr.h:164
int var_init_dest_concurrency
Definition: qmgr.c:417
int xport_rate_delay
Definition: qmgr.h:166
Definition: htable.h:25
char * var_conc_pos_feedback
Definition: qmgr.c:427
int init_dest_concurrency
Definition: qmgr.h:157
#define QMGR_LIST_INIT(head)
Definition: qmgr.h:88
QMGR_QUEUE * next
Definition: qmgr.h:148
#define QMGR_LIST_APPEND(head, object)
Definition: qmgr.h:66
QMGR_TRANSPORT_LIST qmgr_transport_list
HTABLE * htable_create(ssize_t size)
Definition: htable.c:179
#define QMGR_LIST_ROTATE(head, object)
Definition: qmgr.h:46
int busy_refcount
Definition: qmgr.h:203
QMGR_QUEUE_LIST peers
Definition: qmgr.h:211
char * var_conc_neg_feedback
Definition: qmgr.c:428
struct HTABLE * queue_byname
Definition: qmgr.h:159
#define _DEST_RATE_DELAY
Definition: mail_params.h:3520
int var_dest_rcpt_limit
Definition: qmgr.c:420
int flags
Definition: qmgr.h:153
void dsn_free(DSN *dsn)
Definition: dsn.c:179
int var_xport_rate_delay
Definition: qmgr.c:431
#define _XPORT_RATE_DELAY
Definition: mail_params.h:3525
int var_dest_con_limit
Definition: qmgr.c:419
int var_dest_rate_delay
Definition: qmgr.c:432
int get_mail_conf_time2(const char *, const char *, int, int, int, int)
void msg_warn(const char *fmt,...)
Definition: msg.c:215
int recipient_limit
Definition: qmgr.h:158
#define VAR_CONC_POS_FDBACK
Definition: mail_params.h:3497
QMGR_TRANSPORT_LIST peers
Definition: qmgr.h:161
void * htable_find(HTABLE *table, const char *key)
Definition: htable.c:227
#define QMGR_TRANSPORT_MAX_PEND
char * name
Definition: qmgr.h:155
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
const char * status
Definition: dsn.h:18
int todo_refcount
Definition: qmgr.h:202
int get_mail_conf_int2(const char *, const char *, int, int, int)
DSN * dsn
Definition: qmgr.h:162
void qmgr_transport_unthrottle(QMGR_TRANSPORT *transport)
#define NON_BLOCKING
Definition: iostuff.h:49
void qmgr_transport_alloc(QMGR_TRANSPORT *transport, QMGR_TRANSPORT_ALLOC_NOTIFY notify)
QMGR_QUEUE_LIST queue_list
Definition: qmgr.h:160
void qmgr_feedback_init(QMGR_FEEDBACK *, const char *, const char *, const char *, const char *)
QMGR_TRANSPORT * qmgr_transport_select(void)
QMGR_TRANSPORT * qmgr_transport_create(const char *name)
#define _DEST_RCPT_LIMIT
Definition: mail_params.h:856
int non_blocking(int, int)
Definition: non_blocking.c:55
Definition: dsn.h:17
time_t event_request_timer(EVENT_NOTIFY_TIME_FN callback, void *context, int delay)
Definition: events.c:894
QMGR_TRANSPORT * transport
#define QMGR_TRANSPORT_STAT_DEAD
Definition: qmgr.h:170
#define QMGR_TRANSPORT_STAT_RATE_LOCK
Definition: qmgr.h:171
#define vstream_fileno(vp)
Definition: vstream.h:115
#define _INIT_DEST_CON
Definition: mail_params.h:839
int var_daemon_timeout
Definition: mail_params.c:284
#define CA_VSTREAM_CTL_END
Definition: vstream.h:155
int window
Definition: qmgr.h:204
int rate_delay
Definition: qmgr.h:167
QMGR_FEEDBACK pos_feedback
Definition: qmgr.h:163
void qmgr_transport_throttle(QMGR_TRANSPORT *transport, DSN *dsn)
void vstream_control(VSTREAM *stream, int name,...)
Definition: vstream.c:1372
void event_disable_readwrite(int fd)
Definition: events.c:839
#define QMGR_QUEUE_READY(q)
Definition: qmgr.h:244
#define BLOCKING
Definition: iostuff.h:48
int event_cancel_timer(EVENT_NOTIFY_TIME_FN callback, void *context)
Definition: events.c:965
void(* QMGR_TRANSPORT_ALLOC_NOTIFY)(QMGR_TRANSPORT *, VSTREAM *)
Definition: qmgr.h:173
#define MAIL_CLASS_PRIVATE
Definition: mail_proto.h:96
int var_transport_retry_time
Definition: qmgr.c:418
#define _DEST_CON_LIMIT
Definition: mail_params.h:844
void * mymalloc(ssize_t len)
Definition: mymalloc.c:150
int var_conc_cohort_limit
Definition: qmgr.c:429
QMGR_TRANSPORT_ALLOC_NOTIFY notify
HTABLE_INFO * htable_enter(HTABLE *table, const char *key, void *value)
Definition: htable.c:212
void msg_info(const char *fmt,...)
Definition: msg.c:199