Skip to content

Commit cf64f86

Browse files
committed
New nested timer system that avoids constantly reallocating timer memory
1 parent 006e02b commit cf64f86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1900
-1074
lines changed

src/bin/radiusd.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,32 +168,32 @@ do { \
168168
goto cleanup; \
169169
} while (0)
170170

171-
static fr_event_timer_t const *fr_time_sync_ev = NULL;
171+
static fr_timer_t *fr_time_sync_ev = NULL;
172172

173-
static void fr_time_sync_event(fr_event_list_t *el, UNUSED fr_time_t now, UNUSED void *uctx)
173+
static void fr_time_sync_event(fr_timer_list_t *tl, UNUSED fr_time_t now, UNUSED void *uctx)
174174
{
175175
fr_time_delta_t when = fr_time_delta_from_sec(1);
176176

177-
(void) fr_event_timer_in(el, el, &fr_time_sync_ev, when, fr_time_sync_event, NULL);
177+
(void) fr_timer_in(tl, tl, &fr_time_sync_ev, when, false, fr_time_sync_event, NULL);
178178
(void) fr_time_sync();
179179
}
180180

181181
#ifndef NDEBUG
182182
/** Encourage the server to exit after a period of time
183183
*
184-
* @param[in] el The main loop.
184+
* @param[in] tl The main loop.
185185
* @param[in] now Current time. Should be 0, when adding the event.
186186
* @param[in] uctx Pointer to a fr_time_delta_t indicating how long
187187
* the server should run before exit.
188188
*/
189-
static void fr_exit_after(fr_event_list_t *el, fr_time_t now, void *uctx)
189+
static void fr_exit_after(fr_timer_list_t *tl, fr_time_t now, void *uctx)
190190
{
191-
static fr_event_timer_t const *ev;
191+
static fr_timer_t *ev;
192192

193193
fr_time_delta_t exit_after = *(fr_time_delta_t *)uctx;
194194

195195
if (fr_time_eq(now, fr_time_wrap(0))) {
196-
if (fr_event_timer_in(el, el, &ev, exit_after, fr_exit_after, uctx) < 0) {
196+
if (fr_timer_in(tl, tl, &ev, exit_after, false, fr_exit_after, uctx) < 0) {
197197
PERROR("%s: Failed inserting exit event", program);
198198
}
199199
return;
@@ -978,9 +978,9 @@ int main(int argc, char *argv[])
978978
DEBUG("Global memory protected");
979979
}
980980

981-
fr_time_sync_event(main_loop_event_list(), fr_time(), NULL);
981+
fr_time_sync_event(main_loop_event_list()->tl, fr_time(), NULL);
982982
#ifndef NDEBUG
983-
if (fr_time_delta_ispos(exit_after)) fr_exit_after(main_loop_event_list(), fr_time_wrap(0), &exit_after);
983+
if (fr_time_delta_ispos(exit_after)) fr_exit_after(main_loop_event_list()->tl, fr_time_wrap(0), &exit_after);
984984
#endif
985985
/*
986986
* Process requests until HUP or exit.

src/bin/radsniff.c

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ static void rs_stats_print_csv(rs_update_t *this, rs_stats_t *stats, UNUSED stru
841841
/** Process stats for a single interval
842842
*
843843
*/
844-
static void rs_stats_process(fr_event_list_t *el, fr_time_t now_t, void *ctx)
844+
static void rs_stats_process(fr_timer_list_t *tl, fr_time_t now_t, void *ctx)
845845
{
846846
size_t i;
847847
size_t rs_codes_len = (NUM_ELEMENTS(rs_useful_codes));
@@ -903,13 +903,14 @@ static void rs_stats_process(fr_event_list_t *el, fr_time_t now_t, void *ctx)
903903
}
904904

905905
{
906-
static fr_event_timer_t const *event;
906+
static fr_timer_t *event;
907907

908908
now.tv_sec += conf->stats.interval;
909909
now.tv_usec = 0;
910910

911-
if (fr_event_timer_at(NULL, el, &event,
912-
fr_time_from_timeval(&now), rs_stats_process, ctx) < 0) {
911+
if (fr_timer_at(NULL, tl, &event,
912+
fr_time_from_timeval(&now),
913+
false, rs_stats_process, ctx) < 0) {
913914
ERROR("Failed inserting stats interval event");
914915
}
915916
}
@@ -939,7 +940,7 @@ static void rs_stats_update_latency(rs_latency_t *stats, struct timeval *latency
939940
static int rs_install_stats_processor(rs_stats_t *stats, fr_event_list_t *el,
940941
fr_pcap_t *in, struct timeval *now, bool live)
941942
{
942-
static fr_event_timer_t const *event;
943+
static fr_timer_t const *event;
943944
static rs_update_t update;
944945

945946
memset(&update, 0, sizeof(update));
@@ -978,8 +979,9 @@ static int rs_install_stats_processor(rs_stats_t *stats, fr_event_list_t *el,
978979
rs_tv_add_ms(now, conf->stats.timeout, &(stats->quiet));
979980
}
980981

981-
if (fr_event_timer_at(NULL, events, (void *) &event,
982-
fr_time_from_timeval(now), rs_stats_process, &update) < 0) {
982+
if (fr_timer_at(NULL, events->tl, (void *) &event,
983+
fr_time_from_timeval(now),
984+
false, rs_stats_process, &update) < 0) {
983985
ERROR("Failed inserting stats event");
984986
return -1;
985987
}
@@ -1044,7 +1046,7 @@ static int _request_free(rs_request_t *request)
10441046
}
10451047

10461048
if (request->event) {
1047-
ret = fr_event_timer_delete(&request->event);
1049+
ret = fr_timer_delete(&request->event);
10481050
if (ret < 0) {
10491051
fr_perror("Failed deleting timer");
10501052
RS_ASSERT(0 == 1);
@@ -1129,7 +1131,7 @@ static void rs_packet_cleanup(rs_request_t *request)
11291131
talloc_free(request);
11301132
}
11311133

1132-
static void _rs_event(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *ctx)
1134+
static void _rs_event(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *ctx)
11331135
{
11341136
rs_request_t *request = talloc_get_type_abort(ctx, rs_request_t);
11351137

@@ -1528,7 +1530,7 @@ static void rs_packet_process(uint64_t count, rs_event_t *event, struct pcap_pkt
15281530
*/
15291531
fr_pair_list_free(&original->link_vps);
15301532
fr_packet_free(&original->linked);
1531-
fr_event_timer_delete(&original->event);
1533+
fr_timer_delete(&original->event);
15321534
/*
15331535
* ...nope it's the first response to a request.
15341536
*/
@@ -1544,8 +1546,9 @@ static void rs_packet_process(uint64_t count, rs_event_t *event, struct pcap_pkt
15441546
original->linked = talloc_steal(original, packet);
15451547
fr_pair_list_append(&original->link_vps, &decoded); /* Move the vps over */
15461548
rs_tv_add_ms(&header->ts, conf->stats.timeout, &original->when);
1547-
if (fr_event_timer_at(NULL, event->list, &original->event,
1548-
fr_time_from_timeval(&original->when), _rs_event, original) < 0) {
1549+
if (fr_timer_at(NULL, event->list->tl, &original->event,
1550+
fr_time_from_timeval(&original->when),
1551+
false, _rs_event, original) < 0) {
15491552
REDEBUG("Failed inserting new event");
15501553
/*
15511554
* Delete the original request/event, it's no longer valid
@@ -1752,7 +1755,7 @@ static void rs_packet_process(uint64_t count, rs_event_t *event, struct pcap_pkt
17521755
fr_pair_list_append(&original->expect_vps, &search.expect_vps);
17531756

17541757
/* Disarm the timer for the cleanup event for the original request */
1755-
fr_event_timer_delete(&original->event);
1758+
fr_timer_delete(&original->event);
17561759
/*
17571760
* ...nope it's a new request.
17581761
*/
@@ -1813,8 +1816,9 @@ static void rs_packet_process(uint64_t count, rs_event_t *event, struct pcap_pkt
18131816
*/
18141817
original->packet->timestamp = fr_time_from_timeval(&header->ts);
18151818
rs_tv_add_ms(&header->ts, conf->stats.timeout, &original->when);
1816-
if (fr_event_timer_at(NULL, event->list, &original->event,
1817-
fr_time_from_timeval(&original->when), _rs_event, original) < 0) {
1819+
if (fr_timer_at(NULL, event->list->tl, &original->event,
1820+
fr_time_from_timeval(&original->when),
1821+
false, _rs_event, original) < 0) {
18181822
REDEBUG("Failed inserting new event");
18191823

18201824
talloc_free(original);
@@ -1986,9 +1990,7 @@ static void rs_got_packet(fr_event_list_t *el, int fd, UNUSED int flags, void *c
19861990
stats_started = true;
19871991
}
19881992

1989-
do {
1990-
now = fr_time_from_timeval(&header->ts);
1991-
} while (fr_event_timer_run(el, &now) == 1);
1993+
fr_timer_list_run(el->tl, &now);
19921994
count++;
19931995

19941996
rs_packet_process(count, event, header, data);
@@ -2172,19 +2174,19 @@ static void _unmark_link(void *request)
21722174
/** Exit the event loop after a given timeout.
21732175
*
21742176
*/
2175-
static void timeout_event(fr_event_list_t *el, UNUSED fr_time_t now_t, UNUSED void *ctx)
2177+
static void timeout_event(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now_t, void *ctx)
21762178
{
2177-
fr_event_loop_exit(el, 1);
2179+
fr_event_loop_exit(talloc_get_type_abort(ctx, fr_event_list_t), 1);
21782180
}
21792181

21802182

21812183
#ifdef HAVE_COLLECTDC_H
21822184
/** Re-open the collectd socket
21832185
*
21842186
*/
2185-
static void rs_collectd_reopen(fr_event_list_t *el, fr_time_t now, UNUSED void *ctx)
2187+
static void rs_collectd_reopen(fr_timer_list_t *tl, fr_time_t now, UNUSED void *ctx)
21862188
{
2187-
static fr_event_timer_t const *event;
2189+
static fr_timer_t *event;
21882190

21892191
if (rs_stats_collectd_open(conf) == 0) {
21902192
DEBUG2("Stats output socket (re)opened");
@@ -2193,9 +2195,9 @@ static void rs_collectd_reopen(fr_event_list_t *el, fr_time_t now, UNUSED void *
21932195

21942196
ERROR("Will attempt to re-establish connection in %i ms", RS_SOCKET_REOPEN_DELAY);
21952197

2196-
if (fr_event_timer_at(NULL, el, &event,
2197-
fr_time_add(now, fr_time_delta_from_msec(RS_SOCKET_REOPEN_DELAY)),
2198-
rs_collectd_reopen, el) < 0) {
2198+
if (fr_timer_at(NULL, tl, &event,
2199+
fr_time_add(now, fr_time_delta_from_msec(RS_SOCKET_REOPEN_DELAY)),
2200+
false, rs_collectd_reopen, NULL) < 0) {
21992201
ERROR("Failed inserting re-open event");
22002202
RS_ASSERT(0);
22012203
}
@@ -2328,7 +2330,7 @@ int main(int argc, char *argv[])
23282330

23292331
int c;
23302332
unsigned int timeout = 0;
2331-
fr_event_timer_t const *timeout_ev = NULL;
2333+
fr_timer_t *timeout_ev = NULL;
23322334
char const *raddb_dir = RADDBDIR;
23332335
char const *dict_dir = DICTDIR;
23342336
TALLOC_CTX *autofree;
@@ -3112,8 +3114,8 @@ int main(int argc, char *argv[])
31123114
}
31133115

31143116
if (timeout) {
3115-
if (fr_event_timer_in(NULL, events, &timeout_ev, fr_time_delta_from_sec(timeout),
3116-
timeout_event, NULL) < 0) {
3117+
if (fr_timer_in(NULL, events->tl, &timeout_ev, fr_time_delta_from_sec(timeout),
3118+
false, timeout_event, events) < 0) {
31173119
ERROR("Failed inserting timeout event");
31183120
}
31193121
}

src/bin/radsniff.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,18 @@ typedef struct {
183183
*/
184184
typedef struct {
185185
uint64_t id; //!< Monotonically increasing packet counter.
186-
fr_event_timer_t const *event; //!< Event created when we received the original request.
186+
fr_timer_t *event; //!< Event created when we received the original request.
187187

188188
bool logged; //!< Whether any messages regarding this request were logged.
189189

190190
struct timeval when; //!< Time when the packet was received, or next time an event
191191
//!< is scheduled.
192192
fr_pcap_t *in; //!< PCAP handle the original request was received on.
193-
fr_packet_t *packet; //!< The original packet.
193+
fr_packet_t *packet; //!< The original packet.
194194
fr_pair_list_t packet_vps;
195-
fr_packet_t *expect; //!< Request/response.
195+
fr_packet_t *expect; //!< Request/response.
196196
fr_pair_list_t expect_vps;
197-
fr_packet_t *linked; //!< The subsequent response or forwarded request the packet
197+
fr_packet_t *linked; //!< The subsequent response or forwarded request the packet
198198
//!< was linked against.
199199
fr_pair_list_t link_vps; //!< fr_pair_ts used to link retransmissions.
200200

src/bin/unit_test_module.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ static request_t *request_clone(request_t *old, int number, CONF_SECTION *server
626626
return request;
627627
}
628628

629-
static void cancel_request(UNUSED fr_event_list_t *el, UNUSED fr_time_t when, void *uctx)
629+
static void cancel_request(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t when, void *uctx)
630630
{
631631
request_t *request = talloc_get_type_abort(uctx, request_t);
632632
unlang_interpret_signal(request, FR_SIGNAL_CANCEL);
@@ -651,7 +651,7 @@ int main(int argc, char *argv[])
651651
fr_pair_list_t filter_vps;
652652
bool xlat_only = false;
653653
fr_event_list_t *el = NULL;
654-
fr_event_timer_t const *cancel_timer = NULL;
654+
fr_timer_t *cancel_timer = NULL;
655655
fr_client_t *client = NULL;
656656
fr_dict_t *dict = NULL;
657657
fr_dict_t const *dict_check;
@@ -1072,7 +1072,7 @@ int main(int argc, char *argv[])
10721072
}
10731073

10741074
if (count == 1) {
1075-
fr_event_timer_in(request, el, &cancel_timer, config->max_request_time, cancel_request, request);
1075+
fr_timer_in(request, el->tl, &cancel_timer, config->max_request_time, false, cancel_request, request);
10761076
unlang_interpret_synchronous(el, request);
10771077

10781078
} else {
@@ -1105,7 +1105,7 @@ int main(int argc, char *argv[])
11051105
}
11061106
#endif
11071107

1108-
fr_event_timer_in(request, el, &cancel_timer, config->max_request_time, cancel_request, request);
1108+
fr_timer_in(request, el->tl, &cancel_timer, config->max_request_time, false, cancel_request, request);
11091109
unlang_interpret_synchronous(el, request);
11101110
talloc_free(request);
11111111

src/lib/bio/dedup.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ struct fr_bio_dedup_s {
128128

129129
fr_bio_dedup_config_t config;
130130

131-
fr_event_timer_t const *ev;
131+
fr_timer_t *ev;
132132

133133
/*
134134
* The "first" entry is cached here so that we can detect when it changes. The insert / delete
@@ -159,7 +159,7 @@ struct fr_bio_dedup_s {
159159
FR_DLIST_HEAD(fr_bio_dedup_list) free; //!< free list
160160
};
161161

162-
static void fr_bio_dedup_timer(UNUSED fr_event_list_t *el, fr_time_t now, void *uctx);
162+
static void fr_bio_dedup_timer(UNUSED fr_timer_list_t *el, fr_time_t now, void *uctx);
163163
static ssize_t fr_bio_dedup_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size);
164164
static ssize_t fr_bio_dedup_blocked(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item, ssize_t rcode);
165165
static void fr_bio_dedup_release(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item, fr_bio_dedup_release_reason_t reason);
@@ -226,7 +226,7 @@ ssize_t fr_bio_dedup_respond(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
226226
case FR_BIO_DEDUP_STATE_ACTIVE:
227227
/*
228228
* If we're not writing to the socket, just insert the packet into the pending list.
229-
*/
229+
*/
230230
if (my->bio.write != fr_bio_dedup_write) {
231231
(void) fr_bio_dedup_list_remove(&my->active, item);
232232
fr_bio_dedup_list_insert_tail(&my->pending, item);
@@ -364,7 +364,7 @@ static int fr_bio_dedup_timer_reset(fr_bio_dedup_t *my)
364364
/*
365365
* Update the timer. This should never fail.
366366
*/
367-
if (fr_event_timer_at(my, my->el, &my->ev, first->expires, fr_bio_dedup_timer, my) < 0) return -1;
367+
if (fr_timer_at(my, my->el->tl, &my->ev, first->expires, false, fr_bio_dedup_timer, my) < 0) return -1;
368368

369369
my->first = first;
370370
return 0;
@@ -761,7 +761,7 @@ static ssize_t fr_bio_dedup_blocked_data(fr_bio_dedup_t *my, uint8_t const *buff
761761
*
762762
* @todo - expire items from the pending list, too
763763
*/
764-
static void fr_bio_dedup_timer(UNUSED fr_event_list_t *el, fr_time_t now, void *uctx)
764+
static void fr_bio_dedup_timer(UNUSED fr_timer_list_t *tl, fr_time_t now, void *uctx)
765765
{
766766
fr_bio_dedup_t *my = talloc_get_type_abort(uctx, fr_bio_dedup_t);
767767
fr_bio_dedup_entry_t *item;
@@ -811,7 +811,7 @@ static ssize_t fr_bio_dedup_write(fr_bio_t *bio, void *packet_ctx, void const *b
811811
*/
812812
next = fr_bio_next(&my->bio);
813813
fr_assert(next != NULL);
814-
814+
815815
/*
816816
* The caller is trying to flush partial data. But we don't have any partial data, so just call
817817
* the next bio to flush it.

src/lib/bio/fd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ static void fr_bio_fd_el_connect(NDEBUG_UNUSED fr_event_list_t *el, NDEBUG_UNUSE
12221222
/** We have a timeout on the conenction
12231223
*
12241224
*/
1225-
static void fr_bio_fd_el_timeout(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
1225+
static void fr_bio_fd_el_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
12261226
{
12271227
fr_bio_fd_t *my = talloc_get_type_abort(uctx, fr_bio_fd_t);
12281228

@@ -1327,7 +1327,7 @@ int fr_bio_fd_connect_full(fr_bio_t *bio, fr_event_list_t *el, fr_bio_callback_t
13271327
* Set the timeout callback if asked.
13281328
*/
13291329
if (timeout_cb) {
1330-
if (fr_event_timer_in(my, el, &my->connect.ev, *timeout, fr_bio_fd_el_timeout, my) < 0) {
1330+
if (fr_timer_in(my, el->tl, &my->connect.ev, *timeout, false, fr_bio_fd_el_timeout, my) < 0) {
13311331
goto error;
13321332
}
13331333
}

src/lib/bio/fd_open.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ static int fr_bio_fd_socket_bind(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
754754
* We've picked a random port in what is hopefully a large range. If that works, we're
755755
* done.
756756
*/
757-
if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) >= 0) goto done;
757+
if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) == 0) goto done;
758758

759759
/*
760760
* Hunt & peck. Which is horrible.
@@ -774,10 +774,14 @@ static int fr_bio_fd_socket_bind(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
774774

775775
sin->sin_port = htons(my->info.cfg->src_port_start + current);
776776

777-
if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) >= 0) goto done;
777+
if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) == 0) goto done;
778778
}
779779

780-
fr_strerror_const("There are no open ports between 'src_port_start' and 'src_port_end'");
780+
/*
781+
* The error is a good hint at _why_ we failed to bind.
782+
* We expect errno to be EADDRINUSE, anything else is a surprise.
783+
*/
784+
fr_strerror_printf("Failed binding port between 'src_port_start' and 'src_port_end': %s", fr_syserror(errno));
781785
return -1;
782786
}
783787

0 commit comments

Comments
 (0)