summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd
diff options
context:
space:
mode:
authorEric Faurot <eric@cvs.openbsd.org>2011-09-01 19:56:50 +0000
committerEric Faurot <eric@cvs.openbsd.org>2011-09-01 19:56:50 +0000
commitad47ec1eeb8b886915ea232368f798df48ab734d (patch)
treeb82ac55260162a3f1cdc3b009a94233126a1ecfc /usr.sbin/smtpd
parent9541d1e9935ed1f995936e6a2abd46c1ee30eaed (diff)
Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way. ok gilles@ chl@
Diffstat (limited to 'usr.sbin/smtpd')
-rw-r--r--usr.sbin/smtpd/bounce.c6
-rw-r--r--usr.sbin/smtpd/control.c15
-rw-r--r--usr.sbin/smtpd/dns.c22
-rw-r--r--usr.sbin/smtpd/queue.c6
-rw-r--r--usr.sbin/smtpd/ramqueue.c27
-rw-r--r--usr.sbin/smtpd/runner.c45
-rw-r--r--usr.sbin/smtpd/smtp.c22
-rw-r--r--usr.sbin/smtpd/smtp_session.c6
-rw-r--r--usr.sbin/smtpd/smtpctl.c170
-rw-r--r--usr.sbin/smtpd/smtpctl/Makefile4
-rw-r--r--usr.sbin/smtpd/smtpd.c3
-rw-r--r--usr.sbin/smtpd/smtpd.h116
-rw-r--r--usr.sbin/smtpd/smtpd/Makefile4
-rw-r--r--usr.sbin/smtpd/ssl.c30
-rw-r--r--usr.sbin/smtpd/stats.c79
15 files changed, 310 insertions, 245 deletions
diff --git a/usr.sbin/smtpd/bounce.c b/usr.sbin/smtpd/bounce.c
index 9ad1b7130b8..af6a60dda93 100644
--- a/usr.sbin/smtpd/bounce.c
+++ b/usr.sbin/smtpd/bounce.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bounce.c,v 1.32 2011/05/16 21:05:51 gilles Exp $ */
+/* $OpenBSD: bounce.c,v 1.33 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2009 Gilles Chehade <gilles@openbsd.org>
@@ -158,8 +158,8 @@ out:
queue_message_update(&cc->m);
}
- env->stats->runner.active--;
- env->stats->runner.bounces_active--;
+ stat_decrement(STATS_RUNNER);
+ stat_decrement(STATS_RUNNER_BOUNCES);
client_close(cc->pcb);
fclose(cc->msgfp);
free(cc);
diff --git a/usr.sbin/smtpd/control.c b/usr.sbin/smtpd/control.c
index 52749cfc2a7..3be91e81407 100644
--- a/usr.sbin/smtpd/control.c
+++ b/usr.sbin/smtpd/control.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: control.c,v 1.59 2011/07/21 23:29:24 gilles Exp $ */
+/* $OpenBSD: control.c,v 1.60 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
@@ -267,12 +267,7 @@ control_accept(int listenfd, short event, void *arg)
event_add(&c->iev.ev, NULL);
TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
- env->stats->control.sessions++;
- env->stats->control.sessions_active++;
- SET_IF_GREATER(env->stats->control.sessions_active,
- env->stats->control.sessions_maxactive);
-
- if (env->stats->control.sessions_active >= env->sc_maxconn) {
+ if (stat_increment(STATS_CONTROL_SESSION) >= env->sc_maxconn) {
log_warnx("ctl client limit hit, disabling new connections");
event_del(&control_state.ev);
}
@@ -305,10 +300,8 @@ control_close(int fd)
close(fd);
free(c);
- env->stats->control.sessions_active--;
-
- if (!event_pending(&control_state.ev, EV_READ, NULL) &&
- env->stats->control.sessions_active < env->sc_maxconn) {
+ if (stat_decrement(STATS_CONTROL_SESSION) < env->sc_maxconn &&
+ !event_pending(&control_state.ev, EV_READ, NULL)) {
log_warnx("re-enabling ctl connections");
event_add(&control_state.ev, NULL);
}
diff --git a/usr.sbin/smtpd/dns.c b/usr.sbin/smtpd/dns.c
index e1a66e92a06..a435ad9a8c5 100644
--- a/usr.sbin/smtpd/dns.c
+++ b/usr.sbin/smtpd/dns.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dns.c,v 1.44 2011/07/20 10:22:54 eric Exp $ */
+/* $OpenBSD: dns.c,v 1.45 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -139,13 +139,13 @@ dns_async(struct imsgev *asker, int type, struct dns *query)
switch (type) {
case IMSG_DNS_HOST:
dnssession_mx_insert(dnssession, query->host, 0);
- env->stats->lka.queries_host++;
+ stat_increment(STATS_LKA_SESSION_HOST);
dns_asr_dispatch_host(dnssession);
return;
case IMSG_DNS_PTR:
dnssession->aq = asr_query_cname(asr,
(struct sockaddr*)&query->ss, query->ss.ss_len);
- env->stats->lka.queries_cname++;
+ stat_increment(STATS_LKA_SESSION_CNAME);
if (dnssession->aq == NULL) {
log_debug("dns_async: asr_query_cname error");
break;
@@ -154,7 +154,7 @@ dns_async(struct imsgev *asker, int type, struct dns *query)
return;
case IMSG_DNS_MX:
dnssession->aq = asr_query_dns(asr, T_MX, C_IN, query->host, 0);
- env->stats->lka.queries_mx++;
+ stat_increment(STATS_LKA_SESSION_MX);
if (dnssession->aq == NULL) {
log_debug("dns_async: asr_query_dns error");
break;
@@ -166,7 +166,7 @@ dns_async(struct imsgev *asker, int type, struct dns *query)
break;
}
- env->stats->lka.queries_failure++;
+ stat_increment(STATS_LKA_FAILURE);
dnssession_destroy(dnssession);
noasr:
query->error = DNS_RETRY;
@@ -285,7 +285,7 @@ next:
/* XXX although not likely, this can still be temporary */
query->error = (dnssession->mxfound) ? DNS_OK : DNS_ENOTFOUND;
if (query->error)
- env->stats->lka.queries_failure++;
+ stat_increment(STATS_LKA_FAILURE);
dns_reply(query, IMSG_DNS_HOST_END);
dnssession_destroy(dnssession);
return;
@@ -330,7 +330,7 @@ dns_asr_dispatch_cname(struct dnssession *dnssession)
break;
case ASR_DONE:
/* This is necessarily an error */
- env->stats->lka.queries_failure++;
+ stat_increment(STATS_LKA_FAILURE);
query->error = ar.ar_err;
break;
}
@@ -347,11 +347,7 @@ dnssession_init(struct dns *query)
if (dnssession == NULL)
fatal("dnssession_init: calloc");
- env->stats->lka.queries++;
- env->stats->lka.queries_active++;
- if (env->stats->lka.queries_active > env->stats->lka.queries_maxactive)
- env->stats->lka.queries_maxactive = \
- env->stats->lka.queries_active;
+ stat_increment(STATS_LKA_SESSION);
dnssession->id = query->id;
dnssession->query = *query;
@@ -362,7 +358,7 @@ dnssession_init(struct dns *query)
static void
dnssession_destroy(struct dnssession *dnssession)
{
- env->stats->lka.queries_active--;
+ stat_decrement(STATS_LKA_SESSION);
SPLAY_REMOVE(dnstree, &dns_sessions, dnssession);
event_del(&dnssession->ev);
free(dnssession);
diff --git a/usr.sbin/smtpd/queue.c b/usr.sbin/smtpd/queue.c
index 1fc04156a31..593b4651c0f 100644
--- a/usr.sbin/smtpd/queue.c
+++ b/usr.sbin/smtpd/queue.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: queue.c,v 1.105 2011/08/29 18:49:29 chl Exp $ */
+/* $OpenBSD: queue.c,v 1.106 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -80,12 +80,12 @@ queue_imsg(struct imsgev *iev, struct imsg *imsg)
ss.id = e->session_id;
if (e->delivery.flags & DF_ENQUEUED) {
if (queue_message_commit(Q_ENQUEUE, evpid_to_msgid(e->delivery.id)))
- env->stats->queue.inserts_local++;
+ stat_increment(STATS_QUEUE_LOCAL);
else
ss.code = 421;
} else {
if (queue_message_commit(Q_INCOMING, evpid_to_msgid(e->delivery.id)))
- env->stats->queue.inserts_remote++;
+ stat_increment(STATS_QUEUE_REMOTE);
else
ss.code = 421;
}
diff --git a/usr.sbin/smtpd/ramqueue.c b/usr.sbin/smtpd/ramqueue.c
index 1d13a66cb89..6ebd85d35d1 100644
--- a/usr.sbin/smtpd/ramqueue.c
+++ b/usr.sbin/smtpd/ramqueue.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ramqueue.c,v 1.17 2011/08/30 17:06:01 chl Exp $ */
+/* $OpenBSD: ramqueue.c,v 1.18 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2011 Gilles Chehade <gilles@openbsd.org>
@@ -191,9 +191,7 @@ ramqueue_insert(struct ramqueue *rqueue, struct envelope *envelope, time_t curtm
rq_msg->msgid = msgkey.msgid;
RB_INSERT(msgtree, &rqueue->msgtree, rq_msg);
RB_INIT(&rq_msg->evptree);
- env->stats->ramqueue.messages++;
- SET_IF_GREATER(env->stats->ramqueue.messages,
- env->stats->ramqueue.messages_max);
+ stat_increment(STATS_RAMQUEUE_MESSAGE);
}
rq_msg->rq_host = ramqueue_get_host(rqueue, envelope->delivery.rcpt.domain);
@@ -219,9 +217,7 @@ ramqueue_insert(struct ramqueue *rqueue, struct envelope *envelope, time_t curtm
if (evp == NULL)
TAILQ_INSERT_TAIL(&rqueue->queue, rq_evp, queue_entry);
- env->stats->ramqueue.envelopes++;
- SET_IF_GREATER(env->stats->ramqueue.envelopes,
- env->stats->ramqueue.envelopes_max);
+ stat_increment(STATS_RAMQUEUE_ENVELOPE);
}
static int
@@ -291,9 +287,7 @@ ramqueue_get_host(struct ramqueue *rqueue, char *hostname)
strlcpy(rq_host->hostname, hostname, sizeof(rq_host->hostname));
TAILQ_INIT(&rq_host->batch_queue);
RB_INSERT(hosttree, &rqueue->hosttree, rq_host);
- env->stats->ramqueue.hosts++;
- SET_IF_GREATER(env->stats->ramqueue.hosts,
- env->stats->ramqueue.hosts_max);
+ stat_increment(STATS_RAMQUEUE_HOST);
}
return rq_host;
@@ -328,9 +322,8 @@ ramqueue_get_batch(struct ramqueue *rqueue, struct ramqueue_host *host,
TAILQ_INIT(&rq_batch->envelope_queue);
TAILQ_INSERT_TAIL(&host->batch_queue, rq_batch, batch_entry);
- env->stats->ramqueue.batches++;
- SET_IF_GREATER(env->stats->ramqueue.batches,
- env->stats->ramqueue.batches_max);
+ stat_increment(STATS_RAMQUEUE_BATCH);
+
return rq_batch;
}
@@ -354,7 +347,7 @@ ramqueue_remove_batch(struct ramqueue_host *rq_host, struct ramqueue_batch *rq_b
{
TAILQ_REMOVE(&rq_host->batch_queue, rq_batch, batch_entry);
free(rq_batch);
- env->stats->ramqueue.batches--;
+ stat_decrement(STATS_RAMQUEUE_BATCH);
}
void
@@ -362,7 +355,7 @@ ramqueue_remove_host(struct ramqueue *rqueue, struct ramqueue_host *rq_host)
{
RB_REMOVE(hosttree, &rqueue->hosttree, rq_host);
free(rq_host);
- env->stats->ramqueue.hosts--;
+ stat_decrement(STATS_RAMQUEUE_HOST);
}
void
@@ -370,7 +363,7 @@ ramqueue_remove_message(struct ramqueue *rqueue, struct ramqueue_message *rq_msg
{
RB_REMOVE(msgtree, &rqueue->msgtree, rq_msg);
free(rq_msg);
- env->stats->ramqueue.messages--;
+ stat_decrement(STATS_RAMQUEUE_MESSAGE);
}
void
@@ -488,7 +481,7 @@ ramqueue_remove_envelope(struct ramqueue *rq, struct ramqueue_envelope *rq_evp)
RB_REMOVE(evptree, &rq_msg->evptree, rq_evp);
TAILQ_REMOVE(&rq_batch->envelope_queue, rq_evp, batchqueue_entry);
TAILQ_REMOVE(&rq->queue, rq_evp, queue_entry);
- env->stats->ramqueue.envelopes--;
+ stat_decrement(STATS_RAMQUEUE_ENVELOPE);
free(rq_evp);
}
diff --git a/usr.sbin/smtpd/runner.c b/usr.sbin/smtpd/runner.c
index 5e15e34c6bf..58d6e5e374a 100644
--- a/usr.sbin/smtpd/runner.c
+++ b/usr.sbin/smtpd/runner.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: runner.c,v 1.116 2011/08/30 17:06:01 chl Exp $ */
+/* $OpenBSD: runner.c,v 1.117 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -80,7 +80,7 @@ runner_imsg(struct imsgev *iev, struct imsg *imsg)
case IMSG_QUEUE_MESSAGE_UPDATE:
e = imsg->data;
e->delivery.retry++;
- env->stats->runner.active--;
+ stat_decrement(STATS_RUNNER);
/* temporary failure, message remains in queue,
* gets reinserted in ramqueue
@@ -114,11 +114,11 @@ runner_imsg(struct imsgev *iev, struct imsg *imsg)
return;
case IMSG_MDA_SESS_NEW:
- env->stats->mda.sessions_active--;
+ stat_decrement(STATS_MDA_SESSION);
return;
case IMSG_BATCH_DONE:
- env->stats->mta.sessions_active--;
+ stat_decrement(STATS_MTA_SESSION);
return;
case IMSG_PARENT_ENQUEUE_OFFLINE:
@@ -362,9 +362,9 @@ runner_process_envelope(struct ramqueue_envelope *rq_evp, time_t curtm)
size_t mta_av, mda_av, bnc_av;
struct envelope envelope;
- mta_av = env->sc_maxconn - env->stats->mta.sessions_active;
- mda_av = env->sc_maxconn - env->stats->mda.sessions_active;
- bnc_av = env->sc_maxconn - env->stats->runner.bounces_active;
+ mta_av = env->sc_maxconn - stat_get(STATS_MTA_SESSION, STAT_ACTIVE);
+ mda_av = env->sc_maxconn - stat_get(STATS_MDA_SESSION, STAT_ACTIVE);
+ bnc_av = env->sc_maxconn - stat_get(STATS_RUNNER_BOUNCES, STAT_ACTIVE);
if (! queue_envelope_load(Q_QUEUE, rq_evp->evpid, &envelope))
return 0;
@@ -432,13 +432,10 @@ runner_process_batch(struct ramqueue_envelope *rq_evp, time_t curtm)
sizeof evp);
ramqueue_remove_envelope(&env->sc_rqueue, rq_evp);
}
- env->stats->runner.bounces_active++;
- env->stats->runner.bounces++;
- SET_IF_GREATER(env->stats->runner.bounces_active,
- env->stats->runner.bounces_maxactive);
- env->stats->runner.active++;
- SET_IF_GREATER(env->stats->runner.active,
- env->stats->runner.maxactive);
+
+ stat_increment(STATS_RUNNER);
+ stat_increment(STATS_RUNNER_BOUNCES);
+
break;
case D_MDA:
@@ -452,13 +449,9 @@ runner_process_batch(struct ramqueue_envelope *rq_evp, time_t curtm)
sizeof evp);
ramqueue_remove_envelope(&env->sc_rqueue, rq_evp);
- env->stats->mda.sessions_active++;
- env->stats->mda.sessions++;
- SET_IF_GREATER(env->stats->mda.sessions_active,
- env->stats->mda.sessions_maxactive);
- env->stats->runner.active++;
- SET_IF_GREATER(env->stats->runner.active,
- env->stats->runner.maxactive);
+ stat_increment(STATS_RUNNER);
+ stat_increment(STATS_MDA_SESSION);
+
break;
case D_MTA:
@@ -475,17 +468,13 @@ runner_process_batch(struct ramqueue_envelope *rq_evp, time_t curtm)
IMSG_BATCH_APPEND, PROC_MTA, 0, -1, &evp,
sizeof evp);
ramqueue_remove_envelope(&env->sc_rqueue, rq_evp);
- env->stats->runner.active++;
- SET_IF_GREATER(env->stats->runner.active,
- env->stats->runner.maxactive);
+ stat_increment(STATS_RUNNER);
}
imsg_compose_event(env->sc_ievs[PROC_QUEUE],
IMSG_BATCH_CLOSE, PROC_MTA, 0, -1, rq_batch,
sizeof *rq_batch);
- env->stats->mta.sessions_active++;
- env->stats->mta.sessions++;
- SET_IF_GREATER(env->stats->mta.sessions_active,
- env->stats->mta.sessions_maxactive);
+
+ stat_increment(STATS_MTA_SESSION);
break;
default:
diff --git a/usr.sbin/smtpd/smtp.c b/usr.sbin/smtpd/smtp.c
index 7614ca1344b..0718ce5b8f9 100644
--- a/usr.sbin/smtpd/smtp.c
+++ b/usr.sbin/smtpd/smtp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtp.c,v 1.89 2011/08/31 18:56:30 gilles Exp $ */
+/* $OpenBSD: smtp.c,v 1.90 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -533,12 +533,6 @@ smtp_new(struct listener *l)
if (env->sc_opts & SMTPD_SMTP_PAUSED)
fatalx("smtp_new: unexpected client");
- if (env->stats->smtp.sessions_active >= env->sc_maxconn) {
- log_warnx("client limit hit, disabling incoming connections");
- smtp_pause();
- return (NULL);
- }
-
if ((s = calloc(1, sizeof(*s))) == NULL)
fatal(NULL);
s->s_id = generate_uid();
@@ -546,17 +540,15 @@ smtp_new(struct listener *l)
strlcpy(s->s_msg.tag, l->tag, sizeof(s->s_msg.tag));
SPLAY_INSERT(sessiontree, &env->sc_sessions, s);
- env->stats->smtp.sessions++;
- env->stats->smtp.sessions_active++;
+ if (stat_increment(STATS_SMTP_SESSION) >= env->sc_maxconn) {
+ log_warnx("client limit hit, disabling incoming connections");
+ smtp_pause();
+ }
if (s->s_l->ss.ss_family == AF_INET)
- env->stats->smtp.sessions_inet4++;
+ stat_increment(STATS_SMTP_SESSION_INET4);
if (s->s_l->ss.ss_family == AF_INET6)
- env->stats->smtp.sessions_inet6++;
-
- SET_IF_GREATER(env->stats->smtp.sessions_active,
- env->stats->smtp.sessions_maxactive);
-
+ stat_increment(STATS_SMTP_SESSION_INET6);
return (s);
}
diff --git a/usr.sbin/smtpd/smtp_session.c b/usr.sbin/smtpd/smtp_session.c
index 9832e218eb5..32ba1db59ac 100644
--- a/usr.sbin/smtpd/smtp_session.c
+++ b/usr.sbin/smtpd/smtp_session.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtp_session.c,v 1.145 2011/09/01 09:42:15 chl Exp $ */
+/* $OpenBSD: smtp_session.c,v 1.146 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -1020,11 +1020,9 @@ session_destroy(struct session *s)
if (s->s_fd != -1 && close(s->s_fd) == -1)
fatal("session_destroy: close");
- env->stats->smtp.sessions_active--;
-
/* resume when session count decreases to 95% */
resume = env->sc_maxconn * 95 / 100;
- if (env->stats->smtp.sessions_active == resume) {
+ if (stat_decrement(STATS_SMTP_SESSION) == resume) {
log_warnx("re-enabling incoming connections");
smtp_resume();
}
diff --git a/usr.sbin/smtpd/smtpctl.c b/usr.sbin/smtpd/smtpctl.c
index 5f134d55e54..a417dfa6a47 100644
--- a/usr.sbin/smtpd/smtpctl.c
+++ b/usr.sbin/smtpd/smtpctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtpctl.c,v 1.66 2011/08/30 07:06:06 chl Exp $ */
+/* $OpenBSD: smtpctl.c,v 1.67 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org>
@@ -288,10 +288,56 @@ show_sizes(void)
printf("struct delivery: %zu\n", sizeof (struct delivery));
}
+static void
+stat_print(int stat, int what)
+{
+ static const char *names[STATS_MAX] = {
+ "smtp.sessions",
+ "smtp.sessions.inet4",
+ "smtp.sessions.inet6",
+ "smtp.sessions.smtps",
+ "smtp.sessions.starttls",
+
+ "mta.sessions",
+
+ "mda.sessions",
+
+ "control.sessions",
+
+ "lka.sessions",
+ "lka.sessions.mx",
+ "lka.sessions.host",
+ "lka.sessions.cname",
+ "lka.sessions.failure",
+
+ "runner",
+ "runner.bounces",
+
+ "queue.inserts.local",
+ "queue.inserts.remote",
+
+ "ramqueue.envelopes",
+ "ramqueue.messages",
+ "ramqueue.batches",
+ "ramqueue.hosts",
+ };
+ const char *sfx;
+
+ if (what == STAT_ACTIVE)
+ sfx = ".active";
+ else if (what == STAT_MAXACTIVE)
+ sfx = ".maxactive";
+ else
+ sfx = "";
+
+ printf("%s%s=%zd\n", names[stat], sfx, stat_get(stat, what));
+}
+
static int
show_stats_output(struct imsg *imsg)
{
struct stats *stats;
+ struct stat_counter *s;
if (imsg->hdr.type != IMSG_STATS)
errx(1, "show_stats_output: bad hdr type (%d)", imsg->hdr.type);
@@ -300,60 +346,63 @@ show_stats_output(struct imsg *imsg)
errx(1, "show_stats_output: bad data size");
stats = imsg->data;
+ stat_init(stats->counters, STATS_MAX);
+ s = stats->counters;
- printf("control.sessions=%zd\n", stats->control.sessions);
- printf("control.sessions_active=%zd\n",
- stats->control.sessions_active);
- printf("control.sessions_maxactive=%zd\n",
- stats->control.sessions_maxactive);
+ stat_print(STATS_CONTROL_SESSION, STAT_COUNT);
+ stat_print(STATS_CONTROL_SESSION, STAT_ACTIVE);
+ stat_print(STATS_CONTROL_SESSION, STAT_MAXACTIVE);
- printf("mda.sessions=%zd\n", stats->mda.sessions);
- printf("mda.sessions.active=%zd\n", stats->mda.sessions_active);
- printf("mda.sessions.maxactive=%zd\n", stats->mda.sessions_maxactive);
+ stat_print(STATS_MDA_SESSION, STAT_COUNT);
+ stat_print(STATS_MDA_SESSION, STAT_ACTIVE);
+ stat_print(STATS_MDA_SESSION, STAT_MAXACTIVE);
- printf("mta.sessions=%zd\n", stats->mta.sessions);
- printf("mta.sessions.active=%zd\n", stats->mta.sessions_active);
- printf("mta.sessions.maxactive=%zd\n", stats->mta.sessions_maxactive);
+ stat_print(STATS_MTA_SESSION, STAT_COUNT);
+ stat_print(STATS_MTA_SESSION, STAT_ACTIVE);
+ stat_print(STATS_MTA_SESSION, STAT_MAXACTIVE);
- printf("lka.queries=%zd\n", stats->lka.queries);
- printf("lka.queries.active=%zd\n", stats->lka.queries_active);
- printf("lka.queries.maxactive=%zd\n", stats->lka.queries_maxactive);
- printf("lka.queries.mx=%zd\n", stats->lka.queries_mx);
- printf("lka.queries.host=%zd\n", stats->lka.queries_host);
- printf("lka.queries.cname=%zd\n", stats->lka.queries_cname);
- printf("lka.queries.failure=%zd\n", stats->lka.queries_failure);
+ stat_print(STATS_LKA_SESSION, STAT_COUNT);
+ stat_print(STATS_LKA_SESSION, STAT_ACTIVE);
+ stat_print(STATS_LKA_SESSION, STAT_MAXACTIVE);
+ stat_print(STATS_LKA_SESSION_MX, STAT_COUNT);
+ stat_print(STATS_LKA_SESSION_HOST, STAT_COUNT);
+ stat_print(STATS_LKA_SESSION_CNAME, STAT_COUNT);
+ stat_print(STATS_LKA_FAILURE, STAT_COUNT);
printf("parent.uptime=%lld\n",
(long long int) (time(NULL) - stats->parent.start));
- printf("queue.inserts.local=%zd\n", stats->queue.inserts_local);
- printf("queue.inserts.remote=%zd\n", stats->queue.inserts_remote);
-
- printf("runner.active=%zd\n", stats->runner.active);
- printf("runner.maxactive=%zd\n", stats->runner.maxactive);
- printf("runner.bounces=%zd\n", stats->runner.bounces);
- printf("runner.bounces.active=%zd\n", stats->runner.bounces_active);
- printf("runner.bounces.maxactive=%zd\n",
- stats->runner.bounces_maxactive);
-
- printf("ramqueue.hosts=%zd\n", stats->ramqueue.hosts);
- printf("ramqueue.batches=%zd\n", stats->ramqueue.batches);
- printf("ramqueue.messages=%zd\n", stats->ramqueue.messages);
- printf("ramqueue.envelopes=%zd\n", stats->ramqueue.envelopes);
- printf("ramqueue.hosts.max=%zd\n", stats->ramqueue.hosts_max);
- printf("ramqueue.batches.max=%zd\n", stats->ramqueue.batches_max);
- printf("ramqueue.messages.max=%zd\n", stats->ramqueue.messages_max);
- printf("ramqueue.envelopes.max=%zd\n", stats->ramqueue.envelopes_max);
+ stat_print(STATS_QUEUE_LOCAL, STAT_COUNT);
+ stat_print(STATS_QUEUE_REMOTE, STAT_COUNT);
+
+ stat_print(STATS_RUNNER, STAT_COUNT);
+ stat_print(STATS_RUNNER, STAT_ACTIVE);
+ stat_print(STATS_RUNNER, STAT_MAXACTIVE);
+
+ stat_print(STATS_RUNNER_BOUNCES, STAT_COUNT);
+ stat_print(STATS_RUNNER_BOUNCES, STAT_ACTIVE);
+ stat_print(STATS_RUNNER_BOUNCES, STAT_MAXACTIVE);
+
+ stat_print(STATS_RAMQUEUE_HOST, STAT_ACTIVE);
+ stat_print(STATS_RAMQUEUE_BATCH, STAT_ACTIVE);
+ stat_print(STATS_RAMQUEUE_MESSAGE, STAT_ACTIVE);
+ stat_print(STATS_RAMQUEUE_ENVELOPE, STAT_ACTIVE);
+
+ stat_print(STATS_RAMQUEUE_HOST, STAT_MAXACTIVE);
+ stat_print(STATS_RAMQUEUE_BATCH, STAT_MAXACTIVE);
+ stat_print(STATS_RAMQUEUE_MESSAGE, STAT_MAXACTIVE);
+ stat_print(STATS_RAMQUEUE_ENVELOPE, STAT_MAXACTIVE);
+
printf("ramqueue.size=%zd\n",
- stats->ramqueue.hosts * sizeof(struct ramqueue_host) +
- stats->ramqueue.batches * sizeof(struct ramqueue_batch) +
- stats->ramqueue.messages * sizeof(struct ramqueue_message) +
- stats->ramqueue.envelopes * sizeof(struct ramqueue_envelope));
+ s[STATS_RAMQUEUE_HOST].active * sizeof(struct ramqueue_host) +
+ s[STATS_RAMQUEUE_BATCH].active * sizeof(struct ramqueue_batch) +
+ s[STATS_RAMQUEUE_MESSAGE].active * sizeof(struct ramqueue_message) +
+ s[STATS_RAMQUEUE_ENVELOPE].active * sizeof(struct ramqueue_envelope));
printf("ramqueue.size.max=%zd\n",
- stats->ramqueue.hosts_max * sizeof(struct ramqueue_host) +
- stats->ramqueue.batches_max * sizeof(struct ramqueue_batch) +
- stats->ramqueue.messages_max * sizeof(struct ramqueue_message) +
- stats->ramqueue.envelopes_max * sizeof(struct ramqueue_envelope));
+ s[STATS_RAMQUEUE_HOST].maxactive * sizeof(struct ramqueue_host) +
+ s[STATS_RAMQUEUE_BATCH].maxactive * sizeof(struct ramqueue_batch) +
+ s[STATS_RAMQUEUE_MESSAGE].maxactive * sizeof(struct ramqueue_message) +
+ s[STATS_RAMQUEUE_ENVELOPE].maxactive * sizeof(struct ramqueue_envelope));
printf("smtp.errors.delays=%zd\n", stats->smtp.delays);
printf("smtp.errors.linetoolong=%zd\n", stats->smtp.linetoolong);
@@ -366,27 +415,26 @@ show_stats_output(struct imsg *imsg)
printf("smtp.errors.write_system=%zd\n", stats->smtp.write_error);
printf("smtp.errors.write_timeout=%zd\n", stats->smtp.write_timeout);
- printf("smtp.sessions.inet4=%zd\n", stats->smtp.sessions_inet4);
- printf("smtp.sessions.inet6=%zd\n", stats->smtp.sessions_inet6);
-
- printf("smtp.sessions=%zd\n", stats->smtp.sessions);
+ stat_print(STATS_SMTP_SESSION, STAT_COUNT);
+ stat_print(STATS_SMTP_SESSION_INET4, STAT_COUNT);
+ stat_print(STATS_SMTP_SESSION_INET6, STAT_COUNT);
printf("smtp.sessions.aborted=%zd\n", stats->smtp.read_eof +
stats->smtp.read_error + stats->smtp.write_eof +
stats->smtp.write_error);
- printf("smtp.sessions.active=%zd\n", stats->smtp.sessions_active);
- printf("smtp.sessions.maxactive=%zd\n",
- stats->smtp.sessions_maxactive);
+
+ stat_print(STATS_SMTP_SESSION, STAT_ACTIVE);
+ stat_print(STATS_SMTP_SESSION, STAT_MAXACTIVE);
+
printf("smtp.sessions.timeout=%zd\n", stats->smtp.read_timeout +
stats->smtp.write_timeout);
- printf("smtp.sessions.smtps=%zd\n", stats->smtp.smtps);
- printf("smtp.sessions.smtps.active=%zd\n", stats->smtp.smtps_active);
- printf("smtp.sessions.smtps.maxactive=%zd\n",
- stats->smtp.smtps_maxactive);
- printf("smtp.sessions.starttls=%zd\n", stats->smtp.starttls);
- printf("smtp.sessions.starttls.active=%zd\n",
- stats->smtp.starttls_active);
- printf("smtp.sessions.starttls.maxactive=%zd\n",
- stats->smtp.starttls_maxactive);
+
+ stat_print(STATS_SMTP_SMTPS, STAT_COUNT);
+ stat_print(STATS_SMTP_SMTPS, STAT_ACTIVE);
+ stat_print(STATS_SMTP_SMTPS, STAT_MAXACTIVE);
+
+ stat_print(STATS_SMTP_STARTTLS, STAT_COUNT);
+ stat_print(STATS_SMTP_STARTTLS, STAT_ACTIVE);
+ stat_print(STATS_SMTP_STARTTLS, STAT_MAXACTIVE);
return (1);
}
diff --git a/usr.sbin/smtpd/smtpctl/Makefile b/usr.sbin/smtpd/smtpctl/Makefile
index 7cf59de82eb..9b3f4576c24 100644
--- a/usr.sbin/smtpd/smtpctl/Makefile
+++ b/usr.sbin/smtpd/smtpctl/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.17 2011/04/14 20:11:08 gilles Exp $
+# $OpenBSD: Makefile,v 1.18 2011/09/01 19:56:49 eric Exp $
.PATH: ${.CURDIR}/..
@@ -17,7 +17,7 @@ CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual
CFLAGS+= -Wsign-compare -Wbounded
CFLAGS+= -DCLIENT_NO_SSL
-SRCS= smtpctl.c parser.c log.c enqueue.c queue_shared.c util.c client.c queue_backend.c queue_fsqueue.c
+SRCS= smtpctl.c parser.c log.c enqueue.c queue_shared.c util.c client.c queue_backend.c queue_fsqueue.c stats.c
LDADD+= -lutil -levent
DPADD+= ${LIBUTIL} ${LIBEVENT}
.include <bsd.prog.mk>
diff --git a/usr.sbin/smtpd/smtpd.c b/usr.sbin/smtpd/smtpd.c
index ddd27ea2270..e52b1257004 100644
--- a/usr.sbin/smtpd/smtpd.c
+++ b/usr.sbin/smtpd/smtpd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtpd.c,v 1.129 2011/08/29 21:43:09 chl Exp $ */
+/* $OpenBSD: smtpd.c,v 1.130 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -513,6 +513,7 @@ main(int argc, char *argv[])
if (env->stats == MAP_FAILED)
fatal("mmap");
bzero(env->stats, sizeof(struct stats));
+ stat_init(env->stats->counters, STATS_MAX);
env->stats->parent.start = time(NULL);
diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h
index d05cc24b8f5..bf5ea9d062f 100644
--- a/usr.sbin/smtpd/smtpd.h
+++ b/usr.sbin/smtpd/smtpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtpd.h,v 1.236 2011/08/31 18:56:30 gilles Exp $ */
+/* $OpenBSD: smtpd.h,v 1.237 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -103,7 +103,6 @@
((s)->s_l->flags & F_AUTH && (s)->s_flags & F_SECURE && \
!((s)->s_flags & F_AUTHENTICATED))
-#define SET_IF_GREATER(x,y) do { y = MAX(x,y); } while(0)
typedef u_int32_t objid_t;
@@ -651,38 +650,54 @@ struct smtpd {
struct stats *stats;
};
-struct s_parent {
- time_t start;
-};
+enum {
+ STATS_SMTP_SESSION = 0,
+ STATS_SMTP_SESSION_INET4,
+ STATS_SMTP_SESSION_INET6,
+ STATS_SMTP_SMTPS,
+ STATS_SMTP_STARTTLS,
-struct s_queue {
- size_t inserts_local;
- size_t inserts_remote;
-};
+ STATS_MTA_SESSION,
+
+ STATS_MDA_SESSION,
+
+ STATS_CONTROL_SESSION,
+
+ STATS_LKA_SESSION,
+ STATS_LKA_SESSION_MX,
+ STATS_LKA_SESSION_HOST,
+ STATS_LKA_SESSION_CNAME,
+ STATS_LKA_FAILURE,
-struct s_runner {
- size_t active;
- size_t maxactive;
- size_t bounces_active;
- size_t bounces_maxactive;
- size_t bounces;
+ STATS_RUNNER,
+ STATS_RUNNER_BOUNCES,
+
+ STATS_QUEUE_LOCAL,
+ STATS_QUEUE_REMOTE,
+
+ STATS_RAMQUEUE_ENVELOPE,
+ STATS_RAMQUEUE_MESSAGE,
+ STATS_RAMQUEUE_BATCH,
+ STATS_RAMQUEUE_HOST,
+
+ STATS_MAX,
};
-struct s_session {
- size_t sessions;
- size_t sessions_inet4;
- size_t sessions_inet6;
- size_t sessions_active;
- size_t sessions_maxactive;
+#define STAT_COUNT 0
+#define STAT_ACTIVE 1
+#define STAT_MAXACTIVE 2
- size_t smtps;
- size_t smtps_active;
- size_t smtps_maxactive;
+struct stat_counter {
+ size_t count;
+ size_t active;
+ size_t maxactive;
+};
- size_t starttls;
- size_t starttls_active;
- size_t starttls_maxactive;
+struct s_parent {
+ time_t start;
+};
+struct s_session {
size_t read_error;
size_t read_timeout;
size_t read_eof;
@@ -695,49 +710,12 @@ struct s_session {
size_t delays;
};
-struct s_mda {
- size_t sessions;
- size_t sessions_active;
- size_t sessions_maxactive;
-};
-
-struct s_control {
- size_t sessions;
- size_t sessions_active;
- size_t sessions_maxactive;
-};
-
-struct s_lka {
- size_t queries;
- size_t queries_active;
- size_t queries_maxactive;
- size_t queries_mx;
- size_t queries_host;
- size_t queries_cname;
- size_t queries_failure;
-};
-
-struct s_ramqueue {
- size_t hosts;
- size_t batches;
- size_t messages;
- size_t envelopes;
- size_t hosts_max;
- size_t batches_max;
- size_t messages_max;
- size_t envelopes_max;
-};
-
struct stats {
struct s_parent parent;
- struct s_queue queue;
- struct s_runner runner;
struct s_session mta;
- struct s_mda mda;
struct s_session smtp;
- struct s_control control;
- struct s_lka lka;
- struct s_ramqueue ramqueue;
+
+ struct stat_counter counters[STATS_MAX];
};
struct reload {
@@ -1203,6 +1181,12 @@ SPLAY_PROTOTYPE(ssltree, ssl, ssl_nodes, ssl_cmp);
int ssl_ctx_use_private_key(void *, char *, off_t);
int ssl_ctx_use_certificate_chain(void *, char *, off_t);
+/* stats.c */
+void stat_init(struct stat_counter *, int);
+size_t stat_get(int, int);
+size_t stat_increment(int);
+size_t stat_decrement(int);
+
/* user_backend.c */
struct user_backend *user_backend_lookup(enum user_type);
diff --git a/usr.sbin/smtpd/smtpd/Makefile b/usr.sbin/smtpd/smtpd/Makefile
index 0c4fab2109f..45ef959c8f2 100644
--- a/usr.sbin/smtpd/smtpd/Makefile
+++ b/usr.sbin/smtpd/smtpd/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.29 2011/08/27 22:32:42 gilles Exp $
+# $OpenBSD: Makefile,v 1.30 2011/09/01 19:56:49 eric Exp $
PROG= smtpd
SRCS= aliases.c auth_backend.c bounce.c client.c \
@@ -10,7 +10,7 @@ SRCS= aliases.c auth_backend.c bounce.c client.c \
smtp_session.c smtpd.c ssl.c ssl_privsep.c util.c asr.c \
print.c pack.c dname.c res_random.c sockaddr.c \
ramqueue.c queue_backend.c queue_fsqueue.c \
- user_backend.c
+ user_backend.c stats.c
MAN= smtpd.8 smtpd.conf.5
BINDIR= /usr/sbin
diff --git a/usr.sbin/smtpd/ssl.c b/usr.sbin/smtpd/ssl.c
index 8ba2f68c6cd..1297b3c6a45 100644
--- a/usr.sbin/smtpd/ssl.c
+++ b/usr.sbin/smtpd/ssl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssl.c,v 1.36 2011/05/21 17:01:58 gilles Exp $ */
+/* $OpenBSD: ssl.c,v 1.37 2011/09/01 19:56:49 eric Exp $ */
/*
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
@@ -536,18 +536,11 @@ ssl_session_accept(int fd, short event, void *p)
log_info("ssl_session_accept: accepted ssl client");
s->s_flags |= F_SECURE;
- if (s->s_l->flags & F_SMTPS) {
- env->stats->smtp.smtps++;
- env->stats->smtp.smtps_active++;
- SET_IF_GREATER(env->stats->smtp.smtps_active,
- env->stats->smtp.smtps_maxactive);
- }
- if (s->s_l->flags & F_STARTTLS) {
- env->stats->smtp.starttls++;
- env->stats->smtp.starttls_active++;
- SET_IF_GREATER(env->stats->smtp.starttls_active,
- env->stats->smtp.starttls_maxactive);
- }
+ if (s->s_l->flags & F_SMTPS)
+ stat_increment(STATS_SMTP_SMTPS);
+
+ if (s->s_l->flags & F_STARTTLS)
+ stat_increment(STATS_SMTP_STARTTLS);
session_bufferevent_new(s);
event_set(&s->s_bev->ev_read, s->s_fd, EV_READ, ssl_read, s->s_bev);
@@ -644,14 +637,13 @@ ssl_session_destroy(struct session *s)
return;
}
- if (s->s_l->flags & F_SMTPS) {
+ if (s->s_l->flags & F_SMTPS)
if (s->s_flags & F_SECURE)
- env->stats->smtp.smtps_active--;
- }
- if (s->s_l->flags & F_STARTTLS) {
+ stat_decrement(STATS_SMTP_SMTPS);
+
+ if (s->s_l->flags & F_STARTTLS)
if (s->s_flags & F_SECURE)
- env->stats->smtp.starttls_active--;
- }
+ stat_decrement(STATS_SMTP_STARTTLS);
}
int
diff --git a/usr.sbin/smtpd/stats.c b/usr.sbin/smtpd/stats.c
new file mode 100644
index 00000000000..fb9715d2ddb
--- /dev/null
+++ b/usr.sbin/smtpd/stats.c
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2011 Eric Faurot <eric@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/queue.h>
+#include <sys/tree.h>
+#include <sys/param.h>
+
+#include <event.h>
+#include <imsg.h>
+#include <stdio.h>
+
+#include "smtpd.h"
+
+static struct stat_counter *counters = NULL;
+static int ncounter = 0;
+
+void
+stat_init(struct stat_counter *c, int n)
+{
+ counters = c;
+ ncounter = n;
+}
+
+size_t
+stat_increment(int stat)
+{
+ if (stat < 0 || stat >= ncounter)
+ return (-1);
+
+ counters[stat].count++;
+ if (++counters[stat].active > counters[stat].maxactive)
+ counters[stat].maxactive = counters[stat].active;
+
+ return (counters[stat].active);
+}
+
+size_t
+stat_decrement(int stat)
+{
+ if (stat < 0 || stat >= ncounter)
+ return (-1);
+
+ counters[stat].active--;
+
+ return (counters[stat].active);
+}
+
+size_t
+stat_get(int stat, int what)
+{
+ if (stat < 0 || stat >= ncounter)
+ return (-1);
+
+ switch (what) {
+ case STAT_COUNT:
+ return counters[stat].count;
+ case STAT_ACTIVE:
+ return counters[stat].active;
+ case STAT_MAXACTIVE:
+ return counters[stat].maxactive;
+ default:
+ return (-1);
+ }
+}