The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Macros | Functions | Variables
rlm_sql_cassandra.c File Reference

This is free and unencumbered software released into the public domain. More...

#include <freeradius-devel/server/base.h>
#include <freeradius-devel/util/debug.h>
#include <cassandra.h>
#include "rlm_sql.h"
+ Include dependency graph for rlm_sql_cassandra.c:

Go to the source code of this file.

Data Structures

struct  rlm_sql_cassandra_t
 Cassandra driver instance. More...
 

Macros

#define DO_CASS_OPTION(_opt, _x)
 
#define LOG_PREFIX   "sql - cassandra"
 
#define RLM_CASS_ERR_DATA_RETRIVE(_t)
 

Functions

static void _rlm_sql_cassandra_log (CassLogMessage const *message, UNUSED void *data)
 Log callback for libcassandra. More...
 
static int _sql_socket_destructor (rlm_sql_cassandra_conn_t *conn)
 
 DIAG_OFF (strict-prototypes) typedef struct
 Cassandra cluster connection. More...
 
static int mod_detach (module_detach_ctx_t const *mctx)
 
static int mod_instantiate (module_inst_ctx_t const *mctx)
 
static int mod_load (void)
 
static void mod_unload (void)
 
static int sql_affected_rows (UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
 
static size_t sql_error (UNUSED TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
 
static sql_rcode_t sql_fetch_row (rlm_sql_row_t *out, rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
 
static sql_rcode_t sql_fields (char const **out[], rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
 
static sql_rcode_t sql_finish_query (rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
 
static sql_rcode_t sql_free_result (rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
 
static int sql_num_fields (rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
 
static int sql_num_rows (rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
 
static sql_rcode_t sql_query (rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config, char const *query)
 
static void sql_set_last_error (rlm_sql_cassandra_conn_t *conn, char const *message, size_t len)
 Replace the last error messages associated with the connection. More...
 
static void sql_set_last_error_printf (rlm_sql_cassandra_conn_t *conn, char const *fmt,...))
 Replace the last error messages associated with the connection. More...
 
static sql_rcode_t sql_socket_init (rlm_sql_handle_t *handle, rlm_sql_config_t const *config, fr_time_delta_t timeout)
 

Variables

static fr_table_num_sorted_t const consistency_levels []
 
static size_t consistency_levels_len = NUM_ELEMENTS(consistency_levels)
 
static const conf_parser_t driver_config []
 
static conf_parser_t latency_aware_routing_config []
 
static conf_parser_t load_balance_dc_aware_config []
 
rlm_sql_driver_t rlm_sql_cassandra
 
 rlm_sql_cassandra_conn_t
 
static conf_parser_t tls_config []
 
static fr_table_num_sorted_t const verify_cert_table []
 
static size_t verify_cert_table_len = NUM_ELEMENTS(verify_cert_table)
 

Detailed Description

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org

Id
1f33130b3c83c78f7fbe2996b4d21828a6c1291e

Cassandra SQL driver

Author
Linnaea Von Lavia (le.co.nosp@m.ncor.nosp@m.de.45.nosp@m.90@g.nosp@m.mail..nosp@m.com)
Arran Cudbard-Bell (a.cud.nosp@m.bard.nosp@m.b@fre.nosp@m.erad.nosp@m.ius.o.nosp@m.rg)

Definition in file rlm_sql_cassandra.c.


Data Structure Documentation

◆ rlm_sql_cassandra_t

struct rlm_sql_cassandra_t

Cassandra driver instance.

Definition at line 68 of file rlm_sql_cassandra.c.

+ Collaboration diagram for rlm_sql_cassandra_t:
Data Fields
CassCluster * cluster Configuration of the cassandra cluster connection.
pthread_mutex_t connect_mutex Mutex to prevent multiple connections attempting to connect a keyspace concurrently.
uint32_t connections_per_host Number of connections to each server in each IO thread.
uint32_t connections_per_host_max Maximum number of connections to each server in each IO threads.
CassConsistency consistency Level of consistency converted to a constant.
char const * consistency_str Level of consistency required.
bool done_connect_keyspace Whether we've connected to a keyspace.
uint32_t event_queue_size Sets the size of the the fixed size queue that stores events.
uint32_t io_flush_requests_max Maximum number of requests processed by an IO worker per flush.
uint32_t io_queue_size Size of the the fixed size queue that stores pending requests.
uint32_t io_threads Number of IO threads.
double lar_exclusion_threshold How much worse the latency me be, compared to the average latency of the best performing node before it's penalized.

This gets mangled to a double.

uint64_t lar_min_measured The minimum number of measurements per-host required to be considered by the policy.
fr_time_delta_t lar_retry_period The amount of time a node is penalized by the policy before being given a second chance when the current average latency exceeds the calculated threshold (exclusion_threshold * best_average_latency).
fr_time_delta_t lar_scale Weight given to older latencies when calculating the average latency of a node.

A bigger scale will give more weight to older latency measurements.

fr_time_delta_t lar_update_rate The rate at which the best average latency is recomputed.
bool lbdc_allow_remote_dcs_for_local_cl Allows remote hosts to be used if no local.

dc hosts are available and the consistency level is LOCAL_ONE or LOCAL_QUORUM.

uint32_t lbdc_hosts_per_remote_dc The number of host used in each remote DC if no hosts are available in the local dc.
char const * lbdc_local_dc The primary data center to try first.
bool load_balance_round_robin Enable round robin load balancing.
uint32_t pending_requests_high Sets the high water mark for the number of requests queued waiting for a connection in a connection pool.

Disables writes to a host on an IO worker if the number of requests queued exceed this value.

uint32_t pending_requests_low Sets the low water mark for the number of requests queued waiting for a connection in a connection pool.

After exceeding high water mark requests, writes to a host will only resume once the number of requests fall below this value.

uint32_t protocol_version The protocol version.
CassSession * session Cluster's connection pool.
uint32_t spawn_max The maximum number of connections that will be created concurrently.
fr_time_delta_t spawn_retry_delay Amount of time to wait before attempting to reconnect.
bool spawn_retry_delay_is_set
uint32_t spawn_threshold Threshold for the maximum number of concurrent requests in-flight on a connection before creating a new connection.
CassSsl * ssl Connection's SSL context.
uint32_t tcp_keepalive How often to send TCP keepalives.
bool tcp_nodelay Disable TCP naggle algorithm.
char const * tls_ca_file Path to the CA used to validate the server's certificate.
char const * tls_certificate_file Public certificate we present to the server.
char const * tls_private_key_file Private key for the certificate we present to the server.
char const * tls_private_key_password String to decrypt private key.
char const * tls_verify_cert_str Whether we validate the cert provided by the server.
bool token_aware_routing Whether to use token aware routing.
uint32_t write_bytes_high High water mark for the number of bytes outstanding on a connection.

Disables writes to a connection if the number of bytes queued exceed this value.

uint32_t write_bytes_low Low water mark for number of bytes outstanding on a connection.

After exceeding high water mark bytes, writes will only resume once the number of bytes fall below this value.

Macro Definition Documentation

◆ DO_CASS_OPTION

#define DO_CASS_OPTION (   _opt,
  _x 
)
Value:
do {\
CassError _ret;\
if ((_ret = (_x)) != CASS_OK) {\
ERROR("Error setting " _opt ": %s", cass_error_desc(_ret));\
return RLM_SQL_ERROR;\
}\
} while (0)
@ RLM_SQL_ERROR
General connection/server error.
Definition: rlm_sql.h:44

◆ LOG_PREFIX

#define LOG_PREFIX   "sql - cassandra"

Definition at line 36 of file rlm_sql_cassandra.c.

◆ RLM_CASS_ERR_DATA_RETRIVE

#define RLM_CASS_ERR_DATA_RETRIVE (   _t)
Value:
do {\
char const *_col_name;\
size_t _col_name_len;\
CassError _ret;\
if ((_ret = cass_result_column_name(conn->result, i, &_col_name, &_col_name_len)) != CASS_OK) {\
_col_name = "<INVALID>";\
}\
sql_set_last_error_printf(conn, "Failed to retrieve " _t " data at column %s (%d): %s", \
_col_name, i, cass_error_desc(_ret));\
TALLOC_FREE(handle->row);\
return RLM_SQL_ERROR;\
} while(0)

Function Documentation

◆ _rlm_sql_cassandra_log()

static void _rlm_sql_cassandra_log ( CassLogMessage const *  message,
UNUSED void *  data 
)
static

Log callback for libcassandra.

libcassandra seems to use this to log global events in the library, other messages like query errors are not logged here, and should be retrieved with functions like cass_future_error_message();

Messages here do not need to be made available via sql_error.

Parameters
messageContains the log message and information about its source.
datauser data (not used).

Definition at line 289 of file rlm_sql_cassandra.c.

+ Here is the caller graph for this function:

◆ _sql_socket_destructor()

static int _sql_socket_destructor ( rlm_sql_cassandra_conn_t conn)
static

Definition at line 376 of file rlm_sql_cassandra.c.

+ Here is the caller graph for this function:

◆ DIAG_OFF()

DIAG_OFF ( strict-  prototypes)

Cassandra cluster connection.

< Result from executing a query.

< Row set iterator.

< Prevent unneeded memory allocation by keeping a permanent pool, to store log entries.

Definition at line 44 of file rlm_sql_cassandra.c.

◆ mod_detach()

static int mod_detach ( module_detach_ctx_t const *  mctx)
static

Definition at line 710 of file rlm_sql_cassandra.c.

◆ mod_instantiate()

static int mod_instantiate ( module_inst_ctx_t const *  mctx)
static

Definition at line 723 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:

◆ mod_load()

static int mod_load ( void  )
static

Definition at line 948 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:

◆ mod_unload()

static void mod_unload ( void  )
static

Definition at line 938 of file rlm_sql_cassandra.c.

◆ sql_affected_rows()

static int sql_affected_rows ( UNUSED rlm_sql_handle_t handle,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 705 of file rlm_sql_cassandra.c.

◆ sql_error()

static size_t sql_error ( UNUSED TALLOC_CTX *  ctx,
sql_log_entry_t  out[],
size_t  outlen,
rlm_sql_handle_t handle,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 668 of file rlm_sql_cassandra.c.

◆ sql_fetch_row()

static sql_rcode_t sql_fetch_row ( rlm_sql_row_t out,
rlm_sql_handle_t handle,
rlm_sql_config_t const *  config 
)
static

Definition at line 517 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:

◆ sql_fields()

static sql_rcode_t sql_fields ( char const **  out[],
rlm_sql_handle_t handle,
rlm_sql_config_t const *  config 
)
static

Definition at line 489 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:

◆ sql_finish_query()

static sql_rcode_t sql_finish_query ( rlm_sql_handle_t handle,
rlm_sql_config_t const *  config 
)
static

Definition at line 684 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:

◆ sql_free_result()

static sql_rcode_t sql_free_result ( rlm_sql_handle_t handle,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 649 of file rlm_sql_cassandra.c.

+ Here is the caller graph for this function:

◆ sql_num_fields()

static int sql_num_fields ( rlm_sql_handle_t handle,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 475 of file rlm_sql_cassandra.c.

+ Here is the caller graph for this function:

◆ sql_num_rows()

static int sql_num_rows ( rlm_sql_handle_t handle,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 482 of file rlm_sql_cassandra.c.

◆ sql_query()

static sql_rcode_t sql_query ( rlm_sql_handle_t handle,
UNUSED rlm_sql_config_t const *  config,
char const *  query 
)
static

Definition at line 435 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:

◆ sql_set_last_error()

static void sql_set_last_error ( rlm_sql_cassandra_conn_t conn,
char const *  message,
size_t  len 
)
static

Replace the last error messages associated with the connection.

This could be modified in future to maintain a circular buffer of log entries, but it's not required for now.

Parameters
connto replace log message in.
messagefrom libcassandra.
lenof message.

Definition at line 344 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sql_set_last_error_printf()

static void sql_set_last_error_printf ( rlm_sql_cassandra_conn_t conn,
char const *  fmt,
  ... 
)
static

Replace the last error messages associated with the connection.

This could be modified in future to maintain a circular buffer of log entries, but it's not required for now.

Parameters
connto replace log message in.
fmtof message.
...args.

Definition at line 364 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sql_socket_init()

static sql_rcode_t sql_socket_init ( rlm_sql_handle_t handle,
rlm_sql_config_t const *  config,
fr_time_delta_t  timeout 
)
static

Definition at line 386 of file rlm_sql_cassandra.c.

+ Here is the call graph for this function:

Variable Documentation

◆ consistency_levels

fr_table_num_sorted_t const consistency_levels[]
static
Initial value:
= {
{ L("all"), CASS_CONSISTENCY_ALL },
{ L("any"), CASS_CONSISTENCY_ANY },
{ L("each_quorum"), CASS_CONSISTENCY_EACH_QUORUM },
{ L("local_one"), CASS_CONSISTENCY_LOCAL_ONE },
{ L("local_quorum"), CASS_CONSISTENCY_LOCAL_QUORUM },
{ L("one"), CASS_CONSISTENCY_ONE },
{ L("quorum"), CASS_CONSISTENCY_QUORUM },
{ L("three"), CASS_CONSISTENCY_THREE },
{ L("two"), CASS_CONSISTENCY_TWO }
}
#define L(_str)
Helper for initialising arrays of string literals.
Definition: build.h:207

Definition at line 176 of file rlm_sql_cassandra.c.

◆ consistency_levels_len

size_t consistency_levels_len = NUM_ELEMENTS(consistency_levels)
static

Definition at line 187 of file rlm_sql_cassandra.c.

◆ driver_config

const conf_parser_t driver_config[]
static

Definition at line 222 of file rlm_sql_cassandra.c.

◆ latency_aware_routing_config

conf_parser_t latency_aware_routing_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET("exclusion_threshold", rlm_sql_cassandra_t, lar_exclusion_threshold), .dflt = "2.0" },
{ FR_CONF_OFFSET("scale", rlm_sql_cassandra_t, lar_scale), .dflt = "0.1" },
{ FR_CONF_OFFSET("retry_period", rlm_sql_cassandra_t, lar_retry_period), .dflt = "10" },
{ FR_CONF_OFFSET("update_rate", rlm_sql_cassandra_t, lar_update_rate), .dflt = "0.1" },
{ FR_CONF_OFFSET("min_measured", rlm_sql_cassandra_t, lar_min_measured), .dflt = "50" },
}
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:626
#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:268
Cassandra driver instance.

Definition at line 203 of file rlm_sql_cassandra.c.

◆ load_balance_dc_aware_config

conf_parser_t load_balance_dc_aware_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET("local_dc", rlm_sql_cassandra_t, lbdc_local_dc) },
{ FR_CONF_OFFSET("hosts_per_remote_dc", rlm_sql_cassandra_t, lbdc_hosts_per_remote_dc), .dflt = "0" },
{ FR_CONF_OFFSET("allow_remote_dcs_for_local_cl", rlm_sql_cassandra_t, lbdc_allow_remote_dcs_for_local_cl), .dflt = "no" },
}

Definition at line 196 of file rlm_sql_cassandra.c.

◆ rlm_sql_cassandra

rlm_sql_driver_t rlm_sql_cassandra
Initial value:
= {
.common = {
.name = "sql_cassandra",
.inst_size = sizeof(rlm_sql_cassandra_t),
.onload = mod_load,
.unload = mod_unload,
.detach = mod_detach
},
.number = 7,
.sql_socket_init = sql_socket_init,
.sql_query = sql_query,
.sql_select_query = sql_query,
.sql_num_fields = sql_num_fields,
.sql_num_rows = sql_num_rows,
.sql_affected_rows = sql_affected_rows,
.sql_fields = sql_fields,
.sql_fetch_row = sql_fetch_row,
.sql_free_result = sql_free_result,
.sql_error = sql_error,
.sql_finish_query = sql_finish_query,
.sql_finish_select_query = sql_finish_query
}
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:65
static const conf_parser_t config[]
Definition: base.c:188
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1312
static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t const *config, fr_time_delta_t timeout)
static int mod_detach(module_detach_ctx_t const *mctx)
static int mod_load(void)
static sql_rcode_t sql_fetch_row(rlm_sql_row_t *out, rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
static sql_rcode_t sql_finish_query(rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
static int sql_affected_rows(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static void mod_unload(void)
static int sql_num_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static sql_rcode_t sql_fields(char const **out[], rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
static const conf_parser_t driver_config[]
static sql_rcode_t sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config, char const *query)
static size_t sql_error(UNUSED TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static int sql_num_fields(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static int mod_instantiate(module_inst_ctx_t const *mctx)

Definition at line 964 of file rlm_sql_cassandra.c.

◆ rlm_sql_cassandra_conn_t

rlm_sql_cassandra_conn_t

Definition at line 63 of file rlm_sql_cassandra.c.

◆ tls_config

conf_parser_t tls_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET_FLAGS("certificate_file", CONF_FLAG_FILE_INPUT, rlm_sql_cassandra_t, tls_certificate_file) },
{ FR_CONF_OFFSET_FLAGS("private_key_file", CONF_FLAG_FILE_INPUT, rlm_sql_cassandra_t, tls_private_key_file) },
{ FR_CONF_OFFSET_FLAGS("private_key_password", CONF_FLAG_SECRET, rlm_sql_cassandra_t, tls_private_key_password) },
{ FR_CONF_OFFSET("verify_cert", rlm_sql_cassandra_t, tls_verify_cert_str) },
}
#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:256
@ CONF_FLAG_SECRET
Only print value if debug level >= 3.
Definition: cf_parse.h:410
@ CONF_FLAG_FILE_INPUT
File matching value must exist, and must be readable.
Definition: cf_parse.h:412

Definition at line 212 of file rlm_sql_cassandra.c.

◆ verify_cert_table

fr_table_num_sorted_t const verify_cert_table[]
static
Initial value:
= {
{ L("identity"), CASS_SSL_VERIFY_PEER_IDENTITY },
{ L("no"), CASS_SSL_VERIFY_NONE },
{ L("yes"), CASS_SSL_VERIFY_PEER_CERT }
}

Definition at line 189 of file rlm_sql_cassandra.c.

◆ verify_cert_table_len

size_t verify_cert_table_len = NUM_ELEMENTS(verify_cert_table)
static

Definition at line 194 of file rlm_sql_cassandra.c.