The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
proto_detail_work.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 7f7b02f5f5460e2968fa9eb66b3a0bf96890cf9d $
19 * @file proto_detail_work.c
20 * @brief Detail handler for files
21 *
22 * @copyright 2017 The FreeRADIUS server project.
23 * @copyright 2017 Alan DeKok (aland@deployingradius.com)
24 */
25#include <netdb.h>
26#include <freeradius-devel/server/pair.h>
27#include <freeradius-devel/server/main_loop.h>
28#include <freeradius-devel/io/application.h>
29#include <freeradius-devel/io/listen.h>
30#include <freeradius-devel/util/syserror.h>
31#include "proto_detail.h"
32
33#include <fcntl.h>
34#include <sys/stat.h>
35
36#ifndef NDEBUG
37#if 0
38/*
39 * When we want detailed debugging here, without detailed server
40 * debugging.
41 */
42#define MPRINT DEBUG
43#else
44#define MPRINT DEBUG3
45#endif
46#else
47// No debugging, just remove the mprint entirely
48#define MPRINT(_x, ...)
49#endif
50
51typedef struct {
52 proto_detail_work_thread_t *parent; //!< talloc_parent is SLOW!
53 fr_time_t timestamp; //!< when we read the entry.
54 off_t done_offset; //!< where we're tracking the status
55
56 int id; //!< for retransmission counters
57
58 uint8_t *packet; //!< for retransmissions
59 size_t packet_len; //!< for retransmissions
60
61 fr_retry_t retry; //!< our retry timers
62 fr_timer_t *ev; //!< retransmission timer
63 fr_dlist_t entry; //!< for the retransmission list
65
67 { FR_CONF_OFFSET("initial_rtx_time", proto_detail_work_t, retry_config.irt), .dflt = STRINGIFY(2) },
68 { FR_CONF_OFFSET("max_rtx_time", proto_detail_work_t, retry_config.mrt), .dflt = STRINGIFY(16) },
69
70 /*
71 * Retransmit indefinitely, as v2 and v3 did.
72 */
73 { FR_CONF_OFFSET("max_rtx_count", proto_detail_work_t, retry_config.mrc), .dflt = STRINGIFY(0) },
74 /*
75 * ...again same as v2 and v3.
76 */
77 { FR_CONF_OFFSET("max_rtx_duration", proto_detail_work_t, retry_config.mrd), .dflt = STRINGIFY(0) },
78 { FR_CONF_OFFSET("max_outstanding", proto_detail_work_t, max_outstanding), .dflt = STRINGIFY(1) },
80};
81
82
84 { FR_CONF_OFFSET_FLAGS("filename", CONF_FLAG_REQUIRED, proto_detail_work_t, filename_work ) },
85
86 { FR_CONF_OFFSET("track", proto_detail_work_t, track_progress ) },
87
88 { FR_CONF_OFFSET("retransmit", proto_detail_work_t, retransmit ), .dflt = "yes" },
89
90 { FR_CONF_POINTER("limit", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) limit_config },
92};
93
95
98 { .out = &dict_freeradius, .proto = "freeradius" },
99
101};
102
104
110
111/*
112 * All of the decoding is done by proto_detail.c
113 */
114static int mod_decode(void const *instance, request_t *request, UNUSED uint8_t *const data, UNUSED size_t data_len)
115{
116
118 fr_detail_entry_t const *track = talloc_get_type_abort_const(request->async->packet_ctx, fr_detail_entry_t);
119 fr_pair_t *vp;
120
121 request->client = inst->client;
122
123 request->packet->id = track->id;
124 request->reply->id = track->id;
125 REQUEST_VERIFY(request);
126
128 vp->vp_uint32 = track->retry.count;
129
130 return 0;
131}
132
135 { 0 }
136};
137
140 { 0 }
141};
142
143static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time_p, uint8_t *buffer, size_t buffer_len, size_t *leftover)
144{
146 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
147
148 ssize_t data_size;
149 size_t packet_len;
150 fr_detail_entry_t *track;
151 uint8_t *partial, *end, *next, *p, *record_end;
152 uint8_t *stopped_search;
153 off_t done_offset;
154
155 fr_assert(*leftover < buffer_len);
156 fr_assert(thread->fd >= 0);
157 fr_assert(thread->el);
158
159 MPRINT("AT COUNT %d offset %ld", thread->count, (long) thread->read_offset);
160
161 /*
162 * Process retransmissions before anything else in the
163 * file.
164 */
165 track = fr_dlist_head(&thread->list);
166 if (track) {
167 fr_dlist_remove(&thread->list, track);
168
169 /*
170 * Don't over-write "leftover" bytes!
171 */
172 if (*leftover) {
173 fr_assert(thread->leftover == 0);
174 if (!thread->leftover_buffer) MEM(thread->leftover_buffer = talloc_array(thread, uint8_t, buffer_len));
175
176 memcpy(thread->leftover_buffer, buffer, *leftover);
177 thread->leftover = *leftover;
178 *leftover = 0;
179 }
180
181 fr_assert(buffer_len >= track->packet_len);
182 memcpy(buffer, track->packet, track->packet_len);
183
184 DEBUG("Retrying packet %d (retransmission %u)", track->id, track->retry.count);
185 *packet_ctx = track;
186 *recv_time_p = track->timestamp;
187 return track->packet_len;
188 }
189
190 /*
191 * If we decide that we're closing, ignore everything
192 * else in the file. Someone extended the file on us
193 * without locking it first. So too bad for them.
194 */
195 if (thread->closing) {
196 if (inst->track_progress) thread->read_offset = lseek(thread->fd, 0, SEEK_END);
197 return 0;
198 }
199
200 /*
201 * Once a socket is ready, the network side tries to read
202 * many packets. So if we want to stop it from reading,
203 * we have to check this ourselves.
204 */
205 if (thread->outstanding >= inst->max_outstanding) {
206 fr_assert(thread->paused);
207 return 0;
208 }
209
210 /*
211 * If we've cached leftover data from the ring buffer,
212 * copy it back.
213 */
214 if (thread->leftover) {
215 fr_assert(*leftover == 0);
216 fr_assert(thread->leftover < buffer_len);
217
218 memcpy(buffer, thread->leftover_buffer, thread->leftover);
219 *leftover = thread->leftover;
220 thread->leftover = 0;
221 }
222
223 /*
224 * Seek to the current read offset.
225 */
226 (void) lseek(thread->fd, thread->read_offset, SEEK_SET);
227
228 /*
229 * There will be "leftover" bytes left over in the buffer
230 * from any previous read. At the start of the file,
231 * "leftover" will be zero.
232 */
233 partial = buffer + *leftover;
234
235 MPRINT("READ leftover %zd", *leftover);
236
237 /*
238 * Try to read as much data as possible.
239 */
240 if (!thread->eof) {
241 size_t room;
242
243 room = buffer_len - *leftover;
244
245 data_size = read(thread->fd, partial, room);
246 if (data_size < 0) {
247 ERROR("proto_detail (%s): Failed reading file %s: %s",
248 thread->name, thread->filename_work, fr_syserror(errno));
249 return -1;
250 }
251
252 MPRINT("GOT %zd bytes", data_size);
253
254 /*
255 * Remember the read offset, and whether we got EOF.
256 */
257 thread->read_offset = lseek(thread->fd, 0, SEEK_CUR);
258
259 /*
260 * Only set EOF if there's no more data in the buffer to manage.
261 */
262 thread->eof = (data_size == 0) || (thread->read_offset == thread->file_size) || ((size_t) data_size < room);
263 if (thread->eof) {
264 MPRINT("Set EOF data_size %ld vs room %ld", data_size, room);
265 MPRINT("Set EOF read %ld vs file %ld", (long) thread->read_offset, (long) thread->file_size);
266 }
267 end = partial + data_size;
268
269 } else {
270 MPRINT("READ UNTIL EOF");
271 /*
272 * We didn't read any more data from the file,
273 * but there should be data left in the buffer.
274 */
275 fr_assert(*leftover > 0);
276 end = buffer + *leftover;
277 }
278
279redo:
280 next = NULL;
281 stopped_search = end;
282
283 /*
284 * Look for "end of record" marker, starting from the
285 * beginning of the buffer.
286 *
287 * Note that all of the data MUST be printable, and raw
288 * LFs are forbidden in attribute contents.
289 */
290 fr_assert((buffer + thread->last_search) <= end);
291
292 MPRINT("Starting search from offset %ld", thread->last_search);
293
294 p = buffer + thread->last_search;
295 while (p < end) {
296 if (p[0] != '\n') {
297 p++;
298 continue;
299 }
300 if ((p + 1) == end) {
301 /*
302 * Remember the last LF, so if the next
303 * read starts with a LF, we can find the
304 * end of record marker.
305 */
306 stopped_search = p;
307 break; /* no more data */
308 }
309
310 if (p[1] == '\n') {
311 p[0] = '\0';
312 p[1] = '\0';
313 next = p + 2;
314 stopped_search = next;
315 thread->last_line++;
316 break;
317 }
318
319 /*
320 * If we're not at EOF, and we're not at end of
321 * record, every line MUST have a leading tab.
322 */
323 if (p[1] != '\t') {
324 ERROR("proto_detail (%s): Missing tab indent at %s[%u], offset from start of file %zu",
325 thread->name,
326 thread->filename_work, thread->last_line,
327 (size_t)((p - buffer) + thread->header_offset));
328 return -1;
329 }
330
331 /*
332 * Smash the \n with zero, so that each line can
333 * be parsed individually.
334 */
335 p[0] = '\0';
336
337 /*
338 * Skip the \n\t
339 */
340 if ((p + 2) >= end) {
341 stopped_search = p;
342 break;
343 }
344
345 p += 2;
346
347 /*
348 * Skip attribute name
349 */
350 while ((p < end) && !isspace((uint8_t) *p)) p++;
351
352 /*
353 * Not enough room for " = ", skip this sanity
354 * check, and just search for a \n on the next
355 * round through the loop.
356 */
357 if ((end - p) < 3) {
358 stopped_search = p;
359 break;
360 }
361
362 /*
363 * Check for " = ". If the line doesn't contain
364 * this, it's malformed.
365 */
366 if (memcmp(p, " = ", 3) != 0) {
367 ERROR("proto_detail (%s): Missing pair assignment operator at %s[%u], offset from start of file %zu: %.*s",
368 thread->name,
369 thread->filename_work, thread->last_line,
370 (size_t)((p - buffer) + thread->header_offset), (int) (end - p), p);
371 return -1;
372 }
373
374 /*
375 * Skip the " = ", and go back to the top of the
376 * loop where we check for the next \n.
377 */
378 p += 3;
379 }
380
381 thread->last_search = (stopped_search - buffer);
382
383 /*
384 * If there is a next record, remember how large this
385 * record is, and update "leftover" bytes.
386 */
387 if (next) {
388 packet_len = next - buffer;
389 *leftover = end - next;
390
391 MPRINT("FOUND next at %zd, leftover is %zd", packet_len, *leftover);
392
393 } else if (!thread->eof) {
394 if ((size_t) (end - buffer) == buffer_len) {
395 ERROR("proto_detail (%s): Too large entry (>%d bytes) found at offset %zu: %.*s of file %s",
396 thread->name, (int) buffer_len,
397 (size_t)((p - buffer) + thread->header_offset), (int) (end - p), p,
398 thread->filename_work);
399 return -1;
400 }
401
402 /*
403 * We're not at EOF, and there is no "next"
404 * entry. Remember all of the leftover data in
405 * the buffer, and ask the caller to call us when
406 * there's more data.
407 */
408 *leftover = end - buffer;
409 MPRINT("Not at EOF, and no next. Leftover is %zd", *leftover);
410 return 0;
411
412 } else {
413 /*
414 * Else we're at EOF, it's OK to not have an "end
415 * of record" marker. We just eat all of the
416 * remaining data.
417 */
418 packet_len = end - buffer;
419 *leftover = 0;
420
421 MPRINT("NO end of record, but at EOF, found %zd leftover is 0", packet_len);
422 }
423
424 /*
425 * Too big? Ignore it.
426 *
427 * @todo - skip the record, using memmove() etc.
428 */
429 if (packet_len > inst->parent->max_packet_size) {
430 DEBUG("Ignoring 'too large' entry at offset %zu of %s",
431 (size_t) thread->header_offset, thread->filename_work);
432 DEBUG("Entry size %lu is greater than allowed maximum %u",
433 packet_len, inst->parent->max_packet_size);
434 skip_record:
435 MPRINT("Skipping record");
436 if (next) {
437 memmove(buffer, next, (end - next));
438 data_size = (end - next);
439 *leftover = 0;
440 end = buffer + data_size;
441 thread->last_search = 0;
442
443 /*
444 * No more data, we're done.
445 */
446 if (end == buffer) return 0;
447 goto redo;
448 }
449
450 fr_assert(*leftover == 0);
451 packet_len = 0;
452 goto done;
453 }
454
455 /*
456 * Search for the "Timestamp" attribute. We overload
457 * that to track which entries have been used.
458 */
459 record_end = buffer + packet_len;
460 p = buffer;
461 done_offset = 0;
462
463 while (p < record_end) {
464 if (*p != '\0') {
465 p++;
466 continue;
467 }
468
469 p++;
470 if (p == record_end) break;
471
472 if (((record_end - p) >= 5) &&
473 (memcmp(p, "\tDone", 5) == 0)) {
474 goto skip_record;
475 }
476
477 if (((record_end - p) > 10) &&
478 (memcmp(p, "\tTimestamp", 10) == 0)) {
479 p++;
480 done_offset = thread->header_offset + (p - buffer);
481 }
482 }
483
484 /*
485 * Allocate the tracking entry.
486 */
487 MEM(track = talloc_zero(thread, fr_detail_entry_t));
488 track->parent = thread;
489 track->timestamp = fr_time();
490 track->id = thread->count++;
491
492 track->done_offset = done_offset;
493 if (inst->retransmit) {
494 MEM(track->packet = talloc_memdup(track, buffer, packet_len));
495 track->packet_len = packet_len;
496 }
497
498 /*
499 * We've read one more packet.
500 */
501 thread->header_offset += packet_len;
502
503 *packet_ctx = track;
504 *recv_time_p = track->timestamp;
505
506 thread->outstanding++;
507
508done:
509 /*
510 * If we're at EOF, mark us as "closing".
511 */
512 if (thread->eof) {
513 fr_assert(!thread->closing);
514 thread->closing = (*leftover == 0);
515 MPRINT("AT EOF, BUT CLOSING %d", thread->closing);
516 }
517
518 /*
519 * Pause reading until such time as we need more packets.
520 */
521 if (!thread->paused && (thread->outstanding >= inst->max_outstanding)) {
522 (void) fr_event_filter_update(thread->el, thread->fd, FR_EVENT_FILTER_IO, pause_read);
523 thread->paused = true;
524
525 /*
526 * Back up so that read() knows there's more data.
527 */
528 if (*leftover) (void) lseek(thread->fd, thread->read_offset - 1, SEEK_SET);
529 }
530
531 /*
532 * Next time, start searching from the start of the
533 * buffer.
534 */
535 thread->last_search = 0;
536
537 MPRINT("Returning NUM %u - %.*s", thread->outstanding, (int) packet_len, buffer);
538 return packet_len;
539}
540
541
542static void work_retransmit(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
543{
544 fr_detail_entry_t *track = talloc_get_type_abort(uctx, fr_detail_entry_t);
545 proto_detail_work_thread_t *thread = track->parent;
546
547 DEBUG("%s - retransmitting packet %d", thread->name, track->id);
548
549 fr_dlist_insert_tail(&thread->list, track);
550
551 if (thread->paused && (thread->outstanding < thread->inst->max_outstanding)) {
552 (void) fr_event_filter_update(thread->el, thread->fd, FR_EVENT_FILTER_IO, resume_read);
553 thread->paused = false;
554 }
555
556 fr_assert(thread->fd >= 0);
557
558 /*
559 * Seek to the START of the file, so that the FD will
560 * always return ready.
561 *
562 * The mod_read() function will take care of seeking to
563 * the correct read offset.
564 */
565 (void) lseek(thread->fd, 0, SEEK_SET);
566
567#ifdef __linux__
568 fr_network_listen_read(thread->nr, thread->listen);
569#endif
570}
571
572static ssize_t mod_write(fr_listen_t *li, void *packet_ctx, UNUSED fr_time_t request_time,
573 uint8_t *buffer, size_t buffer_len, UNUSED size_t written)
574{
576 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
577 fr_detail_entry_t *track = packet_ctx;
578
579 if (buffer_len < 1) return -1;
580
581 fr_assert(thread->outstanding > 0);
582 fr_assert(thread->fd >= 0);
583
584 if (!buffer[0]) {
585 if (!inst->retransmit) goto mark_done;
586
587 if (fr_time_eq(track->retry.start, fr_time_wrap(0))) {
588 fr_retry_init(&track->retry, fr_time(), &inst->retry_config);
589 } else {
590 fr_retry_state_t state;
591
592 state = fr_retry_next(&track->retry, fr_time());
593 if (state == FR_RETRY_MRC) {
594 DEBUG("%s - packet %d failed after %u retransmissions",
595 thread->name, track->id, track->retry.count);
596 goto fail;
597
598 }
599
600 if (state == FR_RETRY_MRD) {
601 DEBUG("%s - packet %d failed after %u seconds",
602 thread->name, track->id,
603 (unsigned int) fr_time_delta_to_sec(inst->retry_config.mrd));
604 goto fail;
605 }
606 }
607
608 DEBUG("%s - packet %d failed during processing. Will retransmit in %.6fs",
609 thread->name, track->id, fr_time_delta_unwrap(track->retry.rt) / (double)NSEC);
610
611 if (fr_timer_at(thread, thread->el->tl, &track->ev,
612 track->retry.next, false, work_retransmit, track) < 0) {
613 ERROR("%s - Failed inserting retransmission timeout", thread->name);
614 fail:
615 if (inst->track_progress && (track->done_offset > 0)) goto mark_done;
616 goto free_track;
617 }
618
619 if (!thread->paused && (thread->outstanding >= inst->max_outstanding)) {
620 (void) fr_event_filter_update(thread->el, thread->fd, FR_EVENT_FILTER_IO, pause_read);
621 thread->paused = true;
622 }
623
624 return 1;
625
626 } else if (inst->track_progress && (track->done_offset > 0)) {
627 mark_done:
628 /*
629 * Seek to the entry, mark it as done, and then seek to
630 * the point in the file where we were reading from.
631 */
632 (void) lseek(thread->fd, track->done_offset, SEEK_SET);
633 if (write(thread->fd, "Done", 4) < 0) {
634 ERROR("%s - Failed marking entry as done: %s", thread->name, fr_syserror(errno));
635 }
636 (void) lseek(thread->fd, thread->read_offset, SEEK_SET);
637 }
638
639free_track:
640 thread->outstanding--;
641
642 /*
643 * If we need to read some more packet, let's do so.
644 */
645 if (thread->paused && (thread->outstanding < inst->max_outstanding)) {
646 (void) fr_event_filter_update(thread->el, thread->fd, FR_EVENT_FILTER_IO, resume_read);
647 thread->paused = false;
648
649 /*
650 * And seek to the start of the file, so that the
651 * reader gets activated again. The reader will
652 * lseek() to the read offset, so this seek is fine.
653 */
654 (void) lseek(thread->fd, 0, SEEK_SET);
655 }
656
657 /*
658 * @todo - add a used / free pool for these
659 */
660 talloc_free(track);
661
662 /*
663 * Close the socket if we're at EOF, and there are no
664 * outstanding replies to deal with.
665 */
666 if (thread->closing && !thread->outstanding) {
667 MPRINT("WRITE ASKED TO CLOSE");
668 errno = ECONNRESET;
669 return -1;
670 }
671
672 MPRINT("WRITE RETURN B %ld", buffer_len);
673 return buffer_len;
674}
675
676/** Open a detail listener
677 *
678 */
679static int mod_open(fr_listen_t *li)
680{
682 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
683
684 fr_dlist_init(&thread->list, fr_detail_entry_t, entry);
685
686 /*
687 * Open the file if we haven't already been given one.
688 */
689 if (thread->fd < 0) {
690 thread->filename_work = talloc_strdup(thread, inst->filename_work);
691
692 li->fd = thread->fd = open(thread->filename_work, inst->mode);
693 if (thread->fd < 0) {
694 cf_log_err(inst->cs, "Failed opening %s: %s", thread->filename_work, fr_syserror(errno));
695 return -1;
696 }
697 }
698
699 /*
700 * If we're tracking progress, learn where the EOF is.
701 */
702 if (inst->track_progress) {
703 struct stat buf;
704
705 if (fstat(thread->fd, &buf) < 0) {
706 cf_log_err(inst->cs, "Failed checking %s: %s", thread->filename_work, fr_syserror(errno));
707 return -1;
708 }
709
710 thread->file_size = buf.st_size;
711 } else {
712 /*
713 * Avoid triggering erroneous EOF.
714 */
715 thread->file_size = 1;
716 }
717
718 fr_assert(thread->name == NULL);
719 fr_assert(thread->filename_work != NULL);
720 thread->name = talloc_typed_asprintf(thread, "detail_work reading file %s", thread->filename_work);
721
722 /*
723 * Linux doesn't like us adding write callbacks for FDs
724 * which reference files. Since the callback is only
725 * used when the FD blocks, and files don't (mostly)
726 * block, we just mark this read-only.
727 *
728 * The code in src/lib/io/network.c will call the
729 * mod_write() callback on any write, even if the
730 * listener is marked "read_only"
731 */
732 li->no_write_callback = true;
733
734 return 0;
735}
736
737
739{
741
742 /*
743 * One less worker... we check for "0" because of the
744 * hacks in proto_detail which let us start up with
745 * "transport = work" for debugging purposes.
746 */
747 if (thread->file_parent) {
748 pthread_mutex_lock(&thread->file_parent->worker_mutex);
749 if (thread->file_parent->num_workers > 0) thread->file_parent->num_workers--;
750 pthread_mutex_unlock(&thread->file_parent->worker_mutex);
751 }
752
753 DEBUG("Closing %sdetail worker file %s", thread->outstanding == 0 ? "and deleting " : "", thread->name);
754
755#ifdef NOTE_REVOKE
757#endif
758 fr_event_fd_delete(thread->el, thread->fd, FR_EVENT_FILTER_IO);
759
760 if (thread->outstanding == 0) unlink(thread->filename_work);
761
762 close(thread->fd);
763 thread->fd = -1;
764
765 if (inst->parent->exit_when_done) {
766 INFO("Done reading detail files, process will now exit");
767
768 /*
769 * The least hacky way of signalling that the server
770 * should exit. This is an almost identical code
771 * path as receiving a SIGTERM.
772 */
774 }
775
776 return 0;
777}
778
779
780/** Close a detail listener
781 *
782 */
783static int mod_close(fr_listen_t *li)
784{
785 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
786
787 return mod_close_internal(thread);
788}
789
790#ifdef NOTE_REVOKE
791static void mod_revoke(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
792{
793 proto_detail_work_thread_t *thread = talloc_get_type_abort(uctx, proto_detail_work_thread_t);
794
795 /*
796 * The underlying file system is gone. Stop reading the
797 * file, destroy all of the IO handlers, and delete everything.
798 */
799 DEBUG("Detail worker %s had file system unmounted. Stopping.", thread->name);
800 mod_close_internal(thread);
801}
802#endif
803
804
805/** Set the event list for a new IO instance
806 *
807 * @param[in] li the listener
808 * @param[in] el the event list
809 * @param[in] nr context from the network side
810 */
812{
813 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
814
815#ifdef NOTE_REVOKE
817
818 memset(&funcs, 0, sizeof(funcs));
819 funcs.revoke = mod_revoke;
820
821 if (fr_event_filter_insert(thread, NULL, el, thread->fd, FR_EVENT_FILTER_VNODE, &funcs, NULL, thread) < 0) {
822 WARN("Failed to add event watching for unmounted file system");
823 }
824#endif
825
826 thread->el = el;
827 thread->nr = nr;
828}
829
830
831static char const *mod_name(fr_listen_t *li)
832
833{ proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
834
835 return thread->name;
836}
837
838static int mod_instantiate(module_inst_ctx_t const *mctx)
839{
840 proto_detail_work_t *inst = talloc_get_type_abort(mctx->mi->data, proto_detail_work_t);
841 fr_client_t *client;
842 CONF_SECTION *cs = mctx->mi->conf;
843 module_instance_t const *mi = mctx->mi;
844
845 inst->parent = talloc_get_type_abort(mi->parent->data, proto_detail_t);
846 inst->cs = cs;
847
848 if (inst->track_progress) {
849 inst->mode = O_RDWR;
850 } else {
851 inst->mode = O_RDONLY;
852 }
853
854 if (inst->retransmit) {
855 FR_TIME_DELTA_BOUND_CHECK("limit.initial_rtx_time", inst->retry_config.irt, >=, fr_time_delta_from_sec(1));
856 FR_TIME_DELTA_BOUND_CHECK("limit.initial_rtx_time", inst->retry_config.irt, <=, fr_time_delta_from_sec(60));
857
858 /*
859 * If you need more than this, just set it to
860 * "0", and check Packet-Transmit-Count manually.
861 */
862 FR_INTEGER_BOUND_CHECK("limit.max_rtx_count", inst->retry_config.mrc, <=, 20);
863 FR_TIME_DELTA_BOUND_CHECK("limit.max_rtx_duration", inst->retry_config.mrd, <=, fr_time_delta_from_sec(600));
864
865 /*
866 * This is a reasonable value.
867 */
868 FR_TIME_DELTA_BOUND_CHECK("limit.max_rtx_timer", inst->retry_config.mrt, <=, fr_time_delta_from_sec(30));
869 }
870
871 FR_INTEGER_BOUND_CHECK("limit.max_outstanding", inst->max_outstanding, >=, 1);
872
873 MEM(client = inst->client = talloc_zero(inst, fr_client_t));
874
875 client->ipaddr.af = AF_INET;
876 client->ipaddr.addr.v4.s_addr = htonl(INADDR_NONE);
877 client->src_ipaddr = client->ipaddr;
878
879 client->longname = client->shortname = client->secret = inst->filename_work;
880 client->nas_type = talloc_strdup(client, "other");
881
882 return 0;
883}
884
885/** Private interface for use by proto_detail_file
886 *
887 */
890 .common = {
891 .magic = MODULE_MAGIC_INIT,
892 .name = "detail_work",
894 .inst_size = sizeof(proto_detail_work_t),
895 .thread_inst_size = sizeof(proto_detail_work_thread_t),
896 .instantiate = mod_instantiate
897 },
898 .default_message_size = 65536,
899 .default_reply_size = 32,
900
901 .open = mod_open,
902 .close = mod_close,
903 .read = mod_read,
904 .decode = mod_decode,
905 .write = mod_write,
906 .event_list_set = mod_event_list_set,
907 .get_name = mod_name,
908};
static int const char char buffer[256]
Definition acutest.h:576
module_t common
Common fields to all loadable modules.
Definition app_io.h:34
Public structure describing an I/O path for a protocol.
Definition app_io.h:33
#define STRINGIFY(x)
Definition build.h:216
#define UNUSED
Definition build.h:384
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:669
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:529
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:334
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
#define FR_TIME_DELTA_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:540
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition cf_parse.h:429
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:423
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:606
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:345
static int max_outstanding
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:292
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:305
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:311
Specifies an attribute which must be present for the module to function.
Definition dict.h:291
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:304
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:242
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:620
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
Entry in a doubly linked list.
Definition dlist.h:41
@ FR_EVENT_FILTER_VNODE
Filter for vnode subfilters.
Definition event.h:84
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:83
#define fr_event_filter_update(...)
Definition event.h:239
#define fr_event_filter_insert(...)
Definition event.h:234
#define FR_EVENT_RESUME(_s, _f)
Re-add the filter for a func from kevent.
Definition event.h:131
#define FR_EVENT_SUSPEND(_s, _f)
Temporarily remove the filter for a func from kevent.
Definition event.h:115
Callbacks for the FR_EVENT_FILTER_IO filter.
Definition event.h:188
Structure describing a modification to a filter's state.
Definition event.h:96
Callbacks for the FR_EVENT_FILTER_VNODE filter.
Definition event.h:195
talloc_free(hp)
int af
Address family.
Definition inet.h:64
union fr_ipaddr_t::@143 addr
void fr_network_listen_read(fr_network_t *nr, fr_listen_t *li)
Signal the network to read from a listener.
Definition network.c:335
fr_ipaddr_t ipaddr
IPv4/IPv6 address of the host.
Definition client.h:83
char const * secret
Secret PSK.
Definition client.h:90
fr_ipaddr_t src_ipaddr
IPv4/IPv6 address to send responses from (family must match ipaddr).
Definition client.h:84
char const * nas_type
Type of client (arbitrary).
Definition client.h:131
char const * longname
Client identifier.
Definition client.h:87
char const * shortname
Client nickname.
Definition client.h:88
Describes a host allowed to send packets to the server.
Definition client.h:80
#define fr_time()
Definition event.c:60
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1203
Stores all information relating to an event list.
Definition event.c:377
void const * app_io_instance
I/O path configuration context.
Definition listen.h:33
void * thread_instance
thread / socket context
Definition listen.h:34
bool no_write_callback
sometimes we don't need to do writes
Definition listen.h:46
int fd
file descriptor for this socket - set by open
Definition listen.h:28
void main_loop_signal_raise(int flag)
Definition main_loop.c:78
@ RADIUS_SIGNAL_SELF_TERM
Definition main_loop.h:37
@ FR_TYPE_UINT32
32 Bit unsigned integer.
long int ssize_t
unsigned char uint8_t
long long int off_t
unsigned long int size_t
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
Detail master protocol handler.
struct proto_detail_work_s proto_detail_work_t
size_t last_search
where we last searched in the buffer MUST be offset, as the buffers can change.
proto_detail_work_thread_t * file_parent
thread instance of the directory reader that spawned us
bool paused
Is reading paused?
int count
number of packets we read from this file.
unsigned int last_line
line number of the last record read.
bool eof
are we at EOF on reading?
fr_listen_t * listen
talloc_parent() is slow
char const * name
debug name for printing
int num_workers
number of workers
uint16_t max_outstanding
number of packets to run in parallel
fr_event_list_t * el
for various timers
off_t file_size
size of the file
pthread_mutex_t worker_mutex
for the workers
char const * filename_work
work file name
off_t header_offset
offset of the current header we're reading
proto_detail_work_t const * inst
instance data
fr_dlist_head_t list
for retransmissions
int fd
file descriptor
bool closing
we should be closing the file
off_t read_offset
where we're reading from in filename_work
uint32_t outstanding
number of currently outstanding records;
fr_network_t * nr
for Linux-specific callbacks
fr_dict_attr_autoload_t proto_detail_work_dict_attr[]
static const conf_parser_t file_listen_config[]
static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time_p, uint8_t *buffer, size_t buffer_len, size_t *leftover)
#define MPRINT
static int mod_decode(void const *instance, request_t *request, UNUSED uint8_t *const data, UNUSED size_t data_len)
static fr_dict_t const * dict_freeradius
static void work_retransmit(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
proto_detail_work_thread_t * parent
talloc_parent is SLOW!
fr_dlist_t entry
for the retransmission list
static void mod_event_list_set(fr_listen_t *li, fr_event_list_t *el, void *nr)
Set the event list for a new IO instance.
static int mod_open(fr_listen_t *li)
Open a detail listener.
size_t packet_len
for retransmissions
fr_retry_t retry
our retry timers
uint8_t * packet
for retransmissions
fr_time_t timestamp
when we read the entry.
fr_dict_autoload_t proto_detail_work_dict[]
static fr_event_update_t pause_read[]
static conf_parser_t limit_config[]
fr_timer_t * ev
retransmission timer
fr_app_io_t proto_detail_work
Private interface for use by proto_detail_file.
static int mod_close_internal(proto_detail_work_thread_t *thread)
static char const * mod_name(fr_listen_t *li)
int id
for retransmission counters
static int mod_close(fr_listen_t *li)
Close a detail listener.
off_t done_offset
where we're tracking the status
static fr_dict_attr_t const * attr_packet_transmit_counter
static ssize_t mod_write(fr_listen_t *li, void *packet_ctx, UNUSED fr_time_t request_time, uint8_t *buffer, size_t buffer_len, UNUSED size_t written)
static int mod_instantiate(module_inst_ctx_t const *mctx)
static fr_event_update_t resume_read[]
#define fr_assert(_expr)
Definition rad_assert.h:37
#define pair_update_request(_attr, _da)
#define WARN(fmt,...)
static bool done
Definition radclient.c:80
#define INFO(fmt,...)
Definition radict.c:63
#define REQUEST_VERIFY(_x)
Definition request.h:310
static conf_parser_t retry_config[]
Definition rlm_tacacs.c:38
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:351
void * data
Module's instance data.
Definition module.h:293
module_instance_t const * parent
Parent module's instance (if any).
Definition module.h:359
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
Definition module.h:206
Module instance data.
Definition module.h:287
eap_aka_sim_process_conf_t * inst
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:546
#define talloc_get_type_abort_const
Definition talloc.h:117
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
static int64_t fr_time_delta_unwrap(fr_time_delta_t time)
Definition time.h:154
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_eq(_a, _b)
Definition time.h:241
static int64_t fr_time_delta_to_sec(fr_time_delta_t delta)
Definition time.h:647
#define NSEC
Definition time.h:379
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:49
A timer event.
Definition timer.c:83
#define fr_timer_at(...)
Definition timer.h:81
static fr_event_list_t * el
fr_retry_state_t fr_retry_next(fr_retry_t *r, fr_time_t now)
Initialize a retransmission counter.
Definition retry.c:110
void fr_retry_init(fr_retry_t *r, fr_time_t now, fr_retry_config_t const *config)
Initialize a retransmission counter.
Definition retry.c:36
fr_time_t start
when we started the retransmission
Definition retry.h:53
fr_time_delta_t rt
retransmit interval
Definition retry.h:57
fr_retry_state_t
Definition retry.h:45
@ FR_RETRY_MRC
reached maximum retransmission count
Definition retry.h:47
@ FR_RETRY_MRD
reached maximum retransmission duration
Definition retry.h:48
uint32_t count
number of sent packets
Definition retry.h:58
fr_time_t next
when the next timer should be set
Definition retry.h:55
static fr_slen_t data
Definition value.h:1340