27 RCSIDH(server_trunk_h,
"$Id: 1508f1c9b8bcf593c8e40a80751978b382337c8e $")
29 #include <freeradius-devel/server/connection.h>
30 #include <freeradius-devel/server/request.h>
31 #include <freeradius-devel/server/cf_parse.h>
41 # error _CONST can only be defined in the local header
43 #ifndef _TRUNK_PRIVATE
111 #define FR_TRUNK_CONN_ALL \
113 FR_TRUNK_CONN_INIT | \
114 FR_TRUNK_CONN_CONNECTING | \
115 FR_TRUNK_CONN_ACTIVE | \
116 FR_TRUNK_CONN_CLOSED | \
117 FR_TRUNK_CONN_FULL | \
118 FR_TRUNK_CONN_INACTIVE | \
119 FR_TRUNK_CONN_DRAINING | \
120 FR_TRUNK_CONN_DRAINING_TO_FREE \
126 #define FR_TRUNK_CONN_SERVICEABLE \
128 FR_TRUNK_CONN_ACTIVE | \
129 FR_TRUNK_CONN_INACTIVE | \
130 FR_TRUNK_CONN_DRAINING | \
131 FR_TRUNK_CONN_INACTIVE_DRAINING | \
132 FR_TRUNK_CONN_DRAINING_TO_FREE \
138 #define FR_TRUNK_CONN_PROCESSING \
140 FR_TRUNK_CONN_ACTIVE | \
141 FR_TRUNK_CONN_FULL | \
142 FR_TRUNK_CONN_INACTIVE | \
143 FR_TRUNK_CONN_DRAINING | \
144 FR_TRUNK_CONN_INACTIVE_DRAINING | \
145 FR_TRUNK_CONN_DRAINING_TO_FREE \
185 #define FR_TRUNK_REQUEST_STATE_ALL \
187 FR_TRUNK_REQUEST_STATE_BACKLOG | \
188 FR_TRUNK_REQUEST_STATE_PENDING | \
189 FR_TRUNK_REQUEST_STATE_PARTIAL | \
190 FR_TRUNK_REQUEST_STATE_SENT | \
191 FR_TRUNK_REQUEST_STATE_COMPLETE | \
192 FR_TRUNK_REQUEST_STATE_FAILED | \
193 FR_TRUNK_REQUEST_STATE_CANCEL | \
194 FR_TRUNK_REQUEST_STATE_CANCEL_PARTIAL | \
195 FR_TRUNK_REQUEST_STATE_CANCEL_SENT | \
196 FR_TRUNK_REQUEST_STATE_CANCEL_COMPLETE \
202 #define FR_TRUNK_REQUEST_STATE_CANCEL_ALL \
204 FR_TRUNK_REQUEST_STATE_CANCEL | \
205 FR_TRUNK_REQUEST_STATE_CANCEL_PARTIAL | \
206 FR_TRUNK_REQUEST_STATE_CANCEL_SENT | \
207 FR_TRUNK_REQUEST_STATE_CANCEL_COMPLETE \
413 char const *log_prefix,
void *uctx);
779 bool fail_bound) CC_HINT(
nonnull);
786 void *preq,
void *rctx) CC_HINT(
nonnull(2));
791 request_t *request,
void *preq,
void *rctx,
792 bool ignore_limits) CC_HINT(
nonnull(2));
889 char const *log_prefix,
void const *uctx,
bool delay_start) CC_HINT(
nonnull(2, 3, 4));
901 #ifndef TALLOC_GET_TYPE_ABORT_NOOP
906 # define FR_TRUNK_VERIFY(_trunk) fr_trunk_verify(__FILE__, __LINE__, _trunk)
907 # define FR_TRUNK_CONNECTION_VERIFY(_tconn) fr_trunk_connection_verify(__FILE__, __LINE__, _tconn)
908 # define FR_TRUNK_REQUEST_VERIFY(_treq) fr_trunk_request_verify(__FILE__, __LINE__, _treq)
909 #elif !defined(NDEBUG)
910 # define FR_TRUNK_VERIFY(_trunk) fr_assert(_trunk)
911 # define FR_TRUNK_CONNECTION_VERIFY(_tconn) fr_assert(_tconn)
912 # define FR_TRUNK_REQUEST_VERIFY(_treq) fr_assert(_treq)
914 # define FR_TRUNK_VERIFY(_trunk)
915 # define FR_TRUNK_CONNECTION_VERIFY(_tconn)
916 # define FR_TRUNK_REQUEST_VERIFY(_treq)
Defines a CONF_PAIR to C data type mapping.
int8_t(* fr_heap_cmp_t)(void const *a, void const *b)
Comparator to order heap elements.
Stores all information relating to an event list.
A time delta, a difference in time measured in nanoseconds.
Associates request queues with a connection.
Main trunk management handle.
An entry in a trunk watch function list.
fr_trunk_request_complete_t request_complete
Request is complete, interpret the response contained in preq.
fr_time_delta_t req_cleanup_delay
How long must a request in the unassigned (free) list not have been used for before it's cleaned up a...
int fr_trunk_start(fr_trunk_t *trunk)
Start the trunk running.
fr_time_t _CONST last_connected
Last time a connection connected.
uint64_t _CONST req_alloc_reused
How many requests were reused.
void fr_trunk_connection_manage_stop(fr_trunk_t *trunk)
Stop the trunk from opening and closing connections in response to load.
fr_trunk_cancel_reason_t
Reasons for a request being cancelled.
@ FR_TRUNK_CANCEL_REASON_SIGNAL
Request cancelled due to a signal.
@ FR_TRUNK_CANCEL_REASON_MOVE
Request cancelled because it's being moved.
@ FR_TRUNK_CANCEL_REASON_REQUEUE
A previously sent request is being requeued.
@ FR_TRUNK_CANCEL_REASON_NONE
Request has not been cancelled.
void fr_trunk_request_signal_cancel_sent(fr_trunk_request_t *treq)
Signal that a remote server has been notified of the cancellation.
fr_trunk_t * fr_trunk_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, fr_trunk_io_funcs_t const *funcs, fr_trunk_conf_t const *conf, char const *log_prefix, void const *uctx, bool delay_start))
Allocate a new collection of connections.
conf_parser_t const fr_trunk_config[]
Config parser definitions to populate a fr_trunk_conf_t.
fr_trunk_connection_t *_CONST tconn
Connection this request belongs to.
void fr_trunk_reconnect(fr_trunk_t *trunk, int state, fr_connection_reason_t reason)
Force the trunk to re-establish its connections.
uint32_t max_req_per_conn
Maximum connections per request.
fr_trunk_request_conn_release_t request_conn_release
Any connection specific resources should be removed from the treq as it's about to be moved or freed.
void fr_trunk_connection_signal_active(fr_trunk_connection_t *tconn)
Signal a trunk connection is no longer full.
fr_trunk_connection_state_t
Used for sanity checks and to track which list the connection is in.
@ FR_TRUNK_CONN_INACTIVE
Connection is inactive and can't accept any more requests.
@ FR_TRUNK_CONN_CLOSED
Connection was closed, either explicitly or due to failure.
@ FR_TRUNK_CONN_HALTED
Halted, ready to be freed.
@ FR_TRUNK_CONN_FULL
Connection is full and can't accept any more requests.
@ FR_TRUNK_CONN_CONNECTING
Connection is connecting.
@ FR_TRUNK_CONN_DRAINING_TO_FREE
Connection will be closed once it has no more outstanding requests.
@ FR_TRUNK_CONN_INACTIVE_DRAINING
Connection is inactive, can't accept any more requests, and will be closed once it has no more outsta...
@ FR_TRUNK_CONN_DRAINING
Connection will be closed once it has no more outstanding requests, if it's not reactivated.
@ FR_TRUNK_CONN_ACTIVE
Connection is connected and ready to service requests.
@ FR_TRUNK_CONN_INIT
In the initial state.
uint16_t start
How many connections to start.
void fr_trunk_request_signal_cancel(fr_trunk_request_t *treq)
Cancel a trunk request.
fr_trunk_request_demux_t request_demux
!< Write one or more requests to a connection.
void(* fr_trunk_request_mux_t)(fr_event_list_t *el, fr_trunk_connection_t *tconn, fr_connection_t *conn, void *uctx)
Multiplex one or more requests into a single connection.
void(* fr_trunk_watch_t)(fr_trunk_t *trunk, fr_trunk_state_t prev, fr_trunk_state_t state, void *uctx)
Receive a notification when a trunk enters a particular state.
void fr_trunk_connection_callback_writable(fr_event_list_t *el, int fd, int flags, void *uctx)
unsigned req_pool_headers
How many chunk headers the talloc pool allocated with the treq should contain.
fr_time_t _CONST last_below_target
Last time average utilisation went below the target value.
int fr_trunk_connection_pop_cancellation(fr_trunk_request_t **treq_out, fr_trunk_connection_t *tconn)
Pop a cancellation request off a connection's cancellation queue.
void fr_trunk_request_signal_sent(fr_trunk_request_t *treq)
Signal that the request was written to a connection successfully.
void fr_trunk_request_free(fr_trunk_request_t **treq)
If the trunk request is freed then update the target requests.
fr_trunk_request_state_t
Used for sanity checks and to simplify freeing.
@ FR_TRUNK_REQUEST_STATE_BACKLOG
In the backlog.
@ FR_TRUNK_REQUEST_STATE_CANCEL
A request on a particular socket was cancel.
@ FR_TRUNK_REQUEST_STATE_SENT
Was written to a socket. Waiting for a response.
@ FR_TRUNK_REQUEST_STATE_PENDING
In the queue of a connection and is pending writing.
@ FR_TRUNK_REQUEST_STATE_UNASSIGNED
Transition state - Request currently not assigned to any connection.
@ FR_TRUNK_REQUEST_STATE_CANCEL_SENT
We've informed the remote server that the request has been cancelled.
@ FR_TRUNK_REQUEST_STATE_FAILED
The request failed.
@ FR_TRUNK_REQUEST_STATE_CANCEL_COMPLETE
Remote server has acknowledged our cancellation.
@ FR_TRUNK_REQUEST_STATE_CANCEL_PARTIAL
We partially wrote a cancellation request.
@ FR_TRUNK_REQUEST_STATE_PARTIAL
Some of the request was written to the socket, more of it should be written later.
@ FR_TRUNK_REQUEST_STATE_INIT
Initial state.
@ FR_TRUNK_REQUEST_STATE_COMPLETE
The request is complete.
fr_time_t _CONST last_closed
Last time the connection management function closed a connection.
fr_connection_conf_t const * conn_conf
Connection configuration.
int fr_trunk_del_watch(fr_trunk_t *trunk, fr_trunk_state_t state, fr_trunk_watch_t watch)
Remove a watch function from a trunk state list.
fr_time_t _CONST last_above_target
Last time average utilisation went above the target value.
uint32_t fr_trunk_request_count_by_connection(fr_trunk_connection_t const *tconn, int req_state)
Return the count number of requests associated with a trunk connection.
bool _CONST triggers
do we run the triggers?
bool fr_trunk_connection_search(fr_trunk_connection_t *tconn, void *ptr)
fr_trunk_request_mux_t request_mux
fr_trunk_watch_entry_t * fr_trunk_add_watch(fr_trunk_t *trunk, fr_trunk_state_t state, fr_trunk_watch_t watch, bool oneshot, void const *uctx))
Add a watch entry to the trunk state list.
bool backlog_on_failed_conn
Assign requests to the backlog when there are no available connections and the last connection event ...
void fr_trunk_connection_callback_readable(fr_event_list_t *el, int fd, int flags, void *uctx)
uint64_t fr_trunk_connection_requests_requeue(fr_trunk_connection_t *tconn, int states, uint64_t max, bool fail_bound)
Move requests off of a connection and requeue elsewhere.
uint16_t fr_trunk_connection_count_by_state(fr_trunk_t *trunk, int conn_state)
Return the count number of connections in the specified states.
void fr_trunk_request_signal_complete(fr_trunk_request_t *treq)
Signal that a trunk request is complete.
bool fr_trunk_request_search(fr_trunk_request_t *treq, void *ptr)
uint64_t max_uses
The maximum time a connection can be used.
uint64_t fr_trunk_request_count_by_state(fr_trunk_t *trunk, int conn_state, int req_state)
Return a count of requests on a connection in a specific state.
fr_trunk_enqueue_t fr_trunk_request_requeue(fr_trunk_request_t *treq)
Re-enqueue a request on the same connection.
void(* fr_trunk_request_cancel_mux_t)(fr_event_list_t *el, fr_trunk_connection_t *tconn, fr_connection_t *conn, void *uctx)
Inform a remote service like a datastore that a request should be cancelled.
fr_trunk_request_state_t _CONST state
Which list the request is now located in.
void fr_trunk_request_state_log(fr_log_t const *log, fr_log_type_t log_type, char const *file, int line, fr_trunk_request_t const *treq)
fr_time_delta_t open_delay
How long we must be above target utilisation to spawn a new connection.
fr_heap_cmp_t request_prioritise
Ordering function for requests.
void fr_trunk_verify(char const *file, int line, fr_trunk_t *trunk)
Verify a trunk.
request_t *_CONST request
The request that we're writing the data on behalf of.
fr_trunk_request_t * fr_trunk_request_alloc(fr_trunk_t *trunk, request_t *request))
(Pre-)Allocate a new trunk request
void fr_trunk_request_signal_partial(fr_trunk_request_t *treq)
Signal a partial write.
uint64_t _CONST req_alloc_new
How many requests we've allocated.
@ FR_TRUNK_STATE_IDLE
Trunk has no connections.
@ FR_TRUNK_STATE_ACTIVE
Trunk has active connections.
@ FR_TRUNK_STATE_PENDING
Trunk has connections, but none are active.
int fr_trunk_connection_pop_request(fr_trunk_request_t **treq_out, fr_trunk_connection_t *tconn)
Pop a request off a connection's pending queue.
void(* fr_trunk_request_demux_t)(fr_event_list_t *el, fr_trunk_connection_t *tconn, fr_connection_t *conn, void *uctx)
Demultiplex on or more responses, reading them from a connection, decoding them, and matching them wi...
fr_trunk_connection_alloc_t connection_alloc
Allocate a new fr_connection_t.
void fr_trunk_connection_signal_inactive(fr_trunk_connection_t *tconn)
Signal a trunk connection cannot accept more requests.
fr_trunk_enqueue_t fr_trunk_request_enqueue_on_conn(fr_trunk_request_t **treq_out, fr_trunk_connection_t *tconn, request_t *request, void *preq, void *rctx, bool ignore_limits))
Enqueue additional requests on a specific connection.
fr_heap_cmp_t connection_prioritise
Ordering function for connections.
fr_time_delta_t close_delay
How long we must be below target utilisation to close an existing connection.
uint32_t target_req_per_conn
How many pending requests should ideally be running on each connection.
uint64_t _CONST req_alloc
The number of requests currently allocated that have not been freed or returned to the free list.
fr_trunk_connection_state_t _CONST state
What state the connection is in.
void fr_trunk_connection_signal_readable(fr_trunk_connection_t *tconn)
Signal that a trunk connection is readable.
void(* fr_trunk_request_fail_t)(request_t *request, void *preq, void *rctx, fr_trunk_request_state_t state, void *uctx)
Write a failure result to the rctx so that the trunk API client is aware that the request failed.
fr_trunk_connection_event_t
What type of I/O events the trunk connection is currently interested in receiving.
@ FR_TRUNK_CONN_EVENT_NONE
Don't notify the trunk on connection state changes.
@ FR_TRUNK_CONN_EVENT_WRITE
Trunk should be notified if a connection is writable.
@ FR_TRUNK_CONN_EVENT_READ
Trunk should be notified if a connection is readable.
@ FR_TRUNK_CONN_EVENT_BOTH
Trunk should be notified if a connection is readable or writable.
fr_trunk_t *_CONST trunk
Trunk this connection belongs to.
fr_connection_t *_CONST conn
The underlying connection.
fr_trunk_t *_CONST trunk
Trunk this request belongs to.
void *_CONST preq
Data for the muxer to write to the connection.
fr_trunk_request_free_t request_free
Free the preq and any resources it holds and provide a chance to mark the request as runnable.
void(* fr_trunk_request_cancel_t)(fr_connection_t *conn, void *preq_to_reset, fr_trunk_cancel_reason_t reason, void *uctx)
Remove an outstanding "sent" request from a tracking/matching structure.
uint16_t connecting
Maximum number of connections that can be in the connecting state.
void(* fr_trunk_request_free_t)(request_t *request, void *preq_to_free, void *uctx)
Free resources associated with a trunk request.
fr_trunk_enqueue_t fr_trunk_request_enqueue(fr_trunk_request_t **treq, fr_trunk_t *trunk, request_t *request, void *preq, void *rctx))
Enqueue a request that needs data written to the trunk.
void fr_trunk_request_signal_fail(fr_trunk_request_t *treq)
Signal that a trunk request failed.
bool always_writable
Set to true if our ability to write requests to a connection handle is not dependent on the state of ...
void fr_trunk_request_verify(char const *file, int line, fr_trunk_request_t *treq)
void(* fr_trunk_connection_notify_t)(fr_trunk_connection_t *tconn, fr_connection_t *conn, fr_event_list_t *el, fr_trunk_connection_event_t notify_on, void *uctx)
Inform the trunk API client which I/O events the trunk wants to receive.
uint16_t max
Maximum number of connections in the trunk.
fr_trunk_request_fail_t request_fail
Request failed, write out a canned response.
fr_time_delta_t manage_interval
How often we run the management algorithm to open/close connections.
void *_CONST rctx
Resume ctx of the module.
fr_trunk_state_t _CONST state
Current state of the trunk.
void(* fr_trunk_request_conn_release_t)(fr_connection_t *conn, void *preq_to_reset, void *uctx)
Free connection specific resources from a treq, as the treq is being removed from a connection.
fr_trunk_connection_notify_t connection_notify
Update the I/O event registrations for.
fr_time_t _CONST last_read_success
Last time we read a response.
void fr_trunk_connection_signal_reconnect(fr_trunk_connection_t *tconn, fr_connection_reason_t reason)
Signal a trunk connection is no longer viable.
fr_time_t _CONST last_open
Last time the connection management function opened a connection.
void fr_trunk_connection_signal_writable(fr_trunk_connection_t *tconn)
Signal that a trunk connection is writable.
size_t req_pool_size
The size of the talloc pool allocated with the treq.
fr_connection_t *(* fr_trunk_connection_alloc_t)(fr_trunk_connection_t *tconn, fr_event_list_t *el, fr_connection_conf_t const *conf, char const *log_prefix, void *uctx)
Allocate a new connection for the trunk.
void(* fr_trunk_request_complete_t)(request_t *request, void *preq, void *rctx, void *uctx)
Write a successful result to the rctx so that the trunk API client is aware of the result.
void fr_trunk_connection_verify(char const *file, int line, fr_trunk_connection_t *tconn)
fr_time_t _CONST last_failed
Last time a connection failed.
bool fr_trunk_connection_in_state(fr_trunk_connection_t *tconn, int state)
Returns true if the trunk connection is in one of the specified states.
fr_trunk_request_cancel_mux_t request_cancel_mux
!< Read one or more requests from a connection.
void fr_trunk_connection_manage_start(fr_trunk_t *trunk)
Allow the trunk to open and close connections in response to load.
fr_trunk_request_cancel_t request_cancel
Request should be removed from tracking and should be reset to its initial state.
void fr_trunk_request_signal_cancel_complete(fr_trunk_request_t *treq)
Signal that a remote server acked our cancellation.
void fr_trunk_request_signal_cancel_partial(fr_trunk_request_t *treq)
Signal a partial cancel write.
fr_time_delta_t lifetime
Time between reconnects.
bool fr_trunk_search(fr_trunk_t *trunk, void *ptr)
@ FR_TRUNK_ENQUEUE_IN_BACKLOG
Request should be enqueued in backlog.
@ FR_TRUNK_ENQUEUE_NO_CAPACITY
At maximum number of connections, and no connection has capacity.
@ FR_TRUNK_ENQUEUE_OK
Operation was successful.
@ FR_TRUNK_ENQUEUE_DST_UNAVAILABLE
Destination is down.
@ FR_TRUNK_ENQUEUE_FAIL
General failure.
int fr_trunk_connection_manage_schedule(fr_trunk_t *trunk)
Schedule a trunk management event for the next time the event loop is executed.
uint16_t min
Shouldn't let connections drop below this number.
Common configuration parameters for a trunk.
Public fields for the trunk connection.
I/O functions to pass to fr_trunk_alloc.
Public fields for the trunk.
Public fields for the trunk request.
static fr_event_list_t * el