libosmoctrl 1.11.0.30-c17053
Osmocom CTRL library
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
control_if.c File Reference

SNMP-like status interface. More...

#include "config.h"
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <osmocom/ctrl/control_cmd.h>
#include <osmocom/ctrl/control_if.h>
#include <osmocom/ctrl/control_vty.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/rate_ctr.h>
#include <osmocom/core/stat_item.h>
#include <osmocom/core/select.h>
#include <osmocom/core/counter.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/socket.h>
#include <osmocom/gsm/protocol/ipaccess.h>
#include <osmocom/gsm/ipa.h>
#include <osmocom/vty/command.h>
#include <osmocom/vty/vector.h>

Data Structures

struct  lookup_helper
 

Functions

int osmo_fsm_ctrl_cmds_install (void)
 
static LLIST_HEAD (ctrl_lookup_helpers)
 
int ctrl_parse_get_num (vector vline, int i, long *num)
 Parse ascii-encoded decimal number at vline[i]. More...
 
int ctrl_cmd_send_to_all (struct ctrl_handle *ctrl, struct ctrl_cmd *cmd)
 Send a CTRL command to all connections. More...
 
int ctrl_cmd_send (struct osmo_wqueue *queue, struct ctrl_cmd *cmd)
 Encode a CTRL command and append it to the given ctrl_connection. More...
 
int ctrl_cmd_send2 (struct ctrl_connection *ccon, struct ctrl_cmd *cmd)
 Encode a CTRL command and append it to the given ctrl_connection. More...
 
int ctrl_cmd_send_trap (struct ctrl_handle *ctrl, const char *name, char *value)
 Send TRAP over given Control Interface. More...
 
struct ctrl_cmdctrl_cmd_trap (struct ctrl_cmd *cmd)
 Copy given cmd and convert copy to CTRL_TYPE_TRAP. More...
 
static void control_close_conn (struct ctrl_connection *ccon)
 
int ctrl_cmd_handle (struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data)
 
static int handle_control_read (struct osmo_fd *bfd)
 
int ctrl_handle_msg (struct ctrl_handle *ctrl, struct ctrl_connection *ccon, struct msgb *msg)
 Handle a received CTRL command contained in a Message buffers. More...
 
static int control_write_cb (struct osmo_fd *bfd, struct msgb *msg)
 
struct ctrl_connectionosmo_ctrl_conn_alloc (void *ctx, void *data)
 Allocate CTRL connection. More...
 
static int listen_fd_cb (struct osmo_fd *listen_bfd, unsigned int what)
 
static uint64_t get_rate_ctr_value (const struct rate_ctr *ctr, int intv, const char *grp)
 
static int get_rate_ctr_group_idx (struct rate_ctr_group *ctrg, int intv, struct ctrl_cmd *cmd)
 
static int ctrl_rate_ctr_group_handler (struct rate_ctr_group *ctrg, void *data)
 
 CTRL_CMD_DEFINE (rate_ctr, "rate_ctr *")
 
static int get_rate_ctr (struct ctrl_cmd *cmd, void *data)
 
static int set_rate_ctr (struct ctrl_cmd *cmd, void *data)
 
static int verify_rate_ctr (struct ctrl_cmd *cmd, const char *value, void *data)
 
 CTRL_CMD_DEFINE_RO (stat_item, "stat_item *")
 
static int get_stat_item (struct ctrl_cmd *cmd, void *data)
 
 CTRL_CMD_DEFINE (counter, "counter *")
 
static int get_counter (struct ctrl_cmd *cmd, void *data)
 
static int set_counter (struct ctrl_cmd *cmd, void *data)
 
static int verify_counter (struct ctrl_cmd *cmd, const char *value, void *data)
 
struct ctrl_handlectrl_interface_setup (void *data, uint16_t port, ctrl_cmd_lookup lookup)
 
static int ctrl_init (unsigned int node_count)
 
struct ctrl_handlectrl_handle_alloc2 (void *ctx, void *data, ctrl_cmd_lookup lookup, unsigned int node_count)
 Allocate a CTRL interface handle. More...
 
struct ctrl_handlectrl_handle_alloc (void *ctx, void *data, ctrl_cmd_lookup lookup)
 Allocate a CTRL interface handle. More...
 
struct ctrl_handlectrl_interface_setup_dynip2 (void *data, const char *bind_addr, uint16_t port, ctrl_cmd_lookup lookup, unsigned int node_count)
 Setup CTRL interface on a given address. More...
 
struct ctrl_handlectrl_interface_setup_dynip (void *data, const char *bind_addr, uint16_t port, ctrl_cmd_lookup lookup)
 Setup CTRL interface on a given address. More...
 
struct ctrl_handlectrl_interface_setup2 (void *data, uint16_t default_port, ctrl_cmd_lookup lookup, unsigned int node_count)
 Initializes CTRL interface using the configured bind addr/port. More...
 
int ctrl_lookup_register (ctrl_cmd_lookup lookup)
 Install a lookup helper function for control nodes This function is used by e.g. More...
 
struct ctrl_cmdctrl_cmd_exec_from_string (struct ctrl_handle *ch, const char *cmdstr)
 Helper for "local execution" of a CTRL command from a string The function will parse + execute the given control command string and return a corresponding ctrl_cmd. More...
 

Variables

vector ctrl_node_vec
 
static int ctrl_initialized = 0
 

Detailed Description

SNMP-like status interface.

Function Documentation

◆ control_close_conn()

◆ control_write_cb()

static int control_write_cb ( struct osmo_fd bfd,
struct msgb msg 
)
static

◆ CTRL_CMD_DEFINE() [1/2]

CTRL_CMD_DEFINE ( counter  ,
"counter *"   
)

◆ CTRL_CMD_DEFINE() [2/2]

CTRL_CMD_DEFINE ( rate_ctr  ,
"rate_ctr *"   
)

◆ CTRL_CMD_DEFINE_RO()

CTRL_CMD_DEFINE_RO ( stat_item  ,
"stat_item *"   
)

◆ ctrl_cmd_exec_from_string()

struct ctrl_cmd * ctrl_cmd_exec_from_string ( struct ctrl_handle ch,
const char *  cmdstr 
)

Helper for "local execution" of a CTRL command from a string The function will parse + execute the given control command string and return a corresponding ctrl_cmd.

Caller is responsible to talloc_free() the return value.

Parameters
[in]ControlInterface Command String
Returns
parsed command, including reply; NULL on error

References ctrl_cmd_handle(), CTRL_CMD_HANDLED, ctrl_cmd_parse2(), CTRL_TYPE_ERROR, msg, msgb_alloc(), msgb_free(), msgb_put(), msgb_tailroom(), osmo_strlcpy(), and ctrl_cmd::type.

◆ ctrl_cmd_handle()

◆ ctrl_cmd_send()

int ctrl_cmd_send ( struct osmo_wqueue queue,
struct ctrl_cmd cmd 
)

Encode a CTRL command and append it to the given ctrl_connection.

Parameters
[in,out]queuewrite queue to which encoded cmd shall be appended
[in]cmddecoded command representation
Returns
0 in case of success; negative on error

References container_of, ctrl_cmd_send2(), and ctrl_connection::write_queue.

◆ ctrl_cmd_send2()

int ctrl_cmd_send2 ( struct ctrl_connection ccon,
struct ctrl_cmd cmd 
)

Encode a CTRL command and append it to the given ctrl_connection.

Parameters
[in,out]queuewrite queue to which encoded cmd shall be appended
[in]cmddecoded command representation
Returns
0 in case of success; negative on error

References ctrl_cmd_make(), DLCTRL, ipa_prepend_header(), ipa_prepend_header_ext(), IPAC_PROTO_EXT_CTRL, IPAC_PROTO_OSMO, LOGL_ERROR, LOGP, msg, msgb_free(), osmo_wqueue_enqueue(), and ctrl_connection::write_queue.

Referenced by ctrl_cmd_def_send(), ctrl_cmd_send(), ctrl_cmd_send_to_all(), and ctrl_handle_msg().

◆ ctrl_cmd_send_to_all()

int ctrl_cmd_send_to_all ( struct ctrl_handle ctrl,
struct ctrl_cmd cmd 
)

Send a CTRL command to all connections.

Parameters
[in]ctrlglobal control handle
[in]cmdcommand to send to all connections in ctrl
Returns
number of times the command has been sent

References ctrl_cmd::ccon, ctrl_handle::ccon_list, ctrl_cmd_send2(), ctrl_connection::list_entry, and llist_for_each_entry.

Referenced by ctrl_cmd_send_trap().

◆ ctrl_cmd_send_trap()

int ctrl_cmd_send_trap ( struct ctrl_handle ctrl,
const char *  name,
char *  value 
)

Send TRAP over given Control Interface.

Parameters
[in]ctrlControl Interface over which TRAP will be sent
[in]nameName of the TRAP variable
[in]valueValue of the TRAP variable
Returns
Negative value on error, result of ctrl_cmd_send_to_all() otherwise

References ctrl_cmd_create(), ctrl_cmd_send_to_all(), CTRL_CMD_TRAP_ID, CTRL_TYPE_TRAP, ctrl_cmd::id, name, ctrl_cmd::reply, ctrl_cmd::value, and ctrl_cmd::variable.

◆ ctrl_cmd_trap()

struct ctrl_cmd * ctrl_cmd_trap ( struct ctrl_cmd cmd)

Copy given cmd and convert copy to CTRL_TYPE_TRAP.

Parameters
[in]cmdcommand to be copied
Returns
pointer to newly-allocated copy of type TRAP. Allocated as sibling of cmd

References ctrl_cmd::ccon, ctrl_cmd_cpy(), CTRL_TYPE_TRAP, and ctrl_cmd::type.

◆ ctrl_handle_alloc()

struct ctrl_handle * ctrl_handle_alloc ( void *  ctx,
void *  data,
ctrl_cmd_lookup  lookup 
)

Allocate a CTRL interface handle.

Parameters
[in]ctxTalloc allocation context to be used
[in]dataPointer which will be made available to each set_..() get_..() verify_..() control command function
[in]lookupLookup function pointer, can be NULL
Returns
ctrl_handle pointer or NULL in case of errors

References ctrl_handle_alloc2(), data, and ctrl_handle::lookup.

◆ ctrl_handle_alloc2()

struct ctrl_handle * ctrl_handle_alloc2 ( void *  ctx,
void *  data,
ctrl_cmd_lookup  lookup,
unsigned int  node_count 
)

Allocate a CTRL interface handle.

Parameters
[in]ctxTalloc allocation context to be used
[in]dataPointer which will be made available to each set_..() get_..() verify_..() control command function
[in]lookupLookup function pointer, can be NULL
[in]node_countNumber of CTRL nodes to allocate, 0 for default.
Returns
ctrl_handle pointer or NULL in case of errors

Please see ctrl_interface_setup_dynip2() for a detailed description of node_count semantics.

References ctrl_handle::ccon_list, ctrl_init(), data, ctrl_handle::data, INIT_LLIST_HEAD, and ctrl_handle::lookup.

Referenced by ctrl_handle_alloc(), and ctrl_interface_setup_dynip2().

◆ ctrl_handle_msg()

int ctrl_handle_msg ( struct ctrl_handle ctrl,
struct ctrl_connection ccon,
struct msgb msg 
)

◆ ctrl_init()

◆ ctrl_interface_setup()

struct ctrl_handle * ctrl_interface_setup ( void *  data,
uint16_t  port,
ctrl_cmd_lookup  lookup 
)

◆ ctrl_interface_setup2()

struct ctrl_handle * ctrl_interface_setup2 ( void *  data,
uint16_t  default_port,
ctrl_cmd_lookup  lookup,
unsigned int  node_count 
)

Initializes CTRL interface using the configured bind addr/port.

Parameters
[in]dataPointer which will be made available to each set_..() get_..() verify_..() control command function
[in]default_portTCP port number to bind to if not explicitly configured
[in]lookupLookup function pointer, can be NULL
[in]node_countNumber of CTRL nodes to allocate, 0 for default.

References ctrl_interface_setup_dynip2(), ctrl_vty_get_bind_addr(), ctrl_vty_get_bind_port(), data, and ctrl_handle::lookup.

Referenced by ctrl_interface_setup().

◆ ctrl_interface_setup_dynip()

struct ctrl_handle * ctrl_interface_setup_dynip ( void *  data,
const char *  bind_addr,
uint16_t  port,
ctrl_cmd_lookup  lookup 
)

Setup CTRL interface on a given address.

Parameters
[in]dataPointer which will be made available to each set_..() get_..() verify_..() control command function
[in]bind_addrAddress on which CTRL socket shall listen
[in]portPort on which CTRL socket shall listen
[in]lookupLookup function pointer, can be NULL
Returns
ctrl_handle pointer or NULL in case of errors

References ctrl_interface_setup_dynip2(), data, and ctrl_handle::lookup.

◆ ctrl_interface_setup_dynip2()

struct ctrl_handle * ctrl_interface_setup_dynip2 ( void *  data,
const char *  bind_addr,
uint16_t  port,
ctrl_cmd_lookup  lookup,
unsigned int  node_count 
)

Setup CTRL interface on a given address.

Parameters
[in]dataPointer which will be made available to each set_..() get_..() verify_..() control command function
[in]bind_addrAddress on which CTRL socket shall listen
[in]portPort on which CTRL socket shall listen
[in]lookupLookup function pointer, can be NULL
[in]node_countNumber of CTRL nodes to allocate, 0 for default.
Returns
ctrl_handle pointer or NULL in case of errors

Control interface nodes are identified by a node handle; some of these are defined in enum ctrl_node_type, here in libosmocore. However, applications defining own nodes may add own control nodes without having to extend the enum in libosmocore. For example, in the calling application, define an enum like "enum more_ctrl_nodes { CTRL_NODE_FOO = _LAST_CTRL_NODE, CTRL_NODE_BAR, _LAST_CTRL_NODE_EXTENDED }". In order to provide space for the additional control nodes, pass _LAST_CTRL_NODE_EXTENDED to the node_count parameter. Passing 0 is identical to passing _LAST_CTRL_NODE, i.e. to not define own control nodes apart from libosmocore ones.

References osmo_fd::cb, ctrl_handle_alloc2(), osmo_fd::data, data, DLCTRL, ctrl_handle::listen_fd, listen_fd_cb(), LOGL_NOTICE, LOGP, ctrl_handle::lookup, OSMO_SOCK_F_BIND, and osmo_sock_init_ofd().

Referenced by ctrl_interface_setup2(), and ctrl_interface_setup_dynip().

◆ ctrl_lookup_register()

int ctrl_lookup_register ( ctrl_cmd_lookup  lookup)

Install a lookup helper function for control nodes This function is used by e.g.

library code to install lookup helpers for additional nodes in the control interface.

Parameters
[in]lookupThe lookup helper function
Returns
- on success; negative on error.

References list, lookup_helper::list, llist_add_tail(), llist_for_each_entry, and lookup_helper::lookup.

Referenced by osmo_fsm_ctrl_cmds_install().

◆ ctrl_parse_get_num()

int ctrl_parse_get_num ( vector  vline,
int  i,
long *  num 
)

Parse ascii-encoded decimal number at vline[i].

Parameters
[in]vlinevector containing a tokenized line
[in]iindex into the vector vline
[out]numparsed decimal integer number at vline[i]
Returns
1 on success; 0 in case of error

References osmo_str_to_int64(), vector_active, and vector_slot.

◆ ctrl_rate_ctr_group_handler()

static int ctrl_rate_ctr_group_handler ( struct rate_ctr_group ctrg,
void *  data 
)
static

◆ get_counter()

static int get_counter ( struct ctrl_cmd cmd,
void *  data 
)
static

◆ get_rate_ctr()

◆ get_rate_ctr_group_idx()

◆ get_rate_ctr_value()

static uint64_t get_rate_ctr_value ( const struct rate_ctr ctr,
int  intv,
const char *  grp 
)
static

◆ get_stat_item()

◆ handle_control_read()

◆ listen_fd_cb()

◆ LLIST_HEAD()

static LLIST_HEAD ( ctrl_lookup_helpers  )
static

◆ osmo_ctrl_conn_alloc()

struct ctrl_connection * osmo_ctrl_conn_alloc ( void *  ctx,
void *  data 
)

Allocate CTRL connection.

Parameters
[in]ctxContext from which talloc should allocate it
[in]datacaller's private data parameter which should assigned to write queue's file descriptor data parameter.
Returns
Allocated CTRL connection structure or NULL in case of errors

References osmo_wqueue::bfd, ctrl_connection::cmds, control_write_cb(), osmo_fd::data, data, ctrl_connection::def_cmds, osmo_fd::fd, handle_control_read(), INIT_LLIST_HEAD, osmo_wqueue_init(), osmo_wqueue::read_cb, osmo_wqueue::write_cb, and ctrl_connection::write_queue.

Referenced by listen_fd_cb().

◆ osmo_fsm_ctrl_cmds_install()

int osmo_fsm_ctrl_cmds_install ( void  )

◆ set_counter()

static int set_counter ( struct ctrl_cmd cmd,
void *  data 
)
static

References CTRL_CMD_ERROR, and ctrl_cmd::reply.

◆ set_rate_ctr()

static int set_rate_ctr ( struct ctrl_cmd cmd,
void *  data 
)
static

References CTRL_CMD_ERROR, and ctrl_cmd::reply.

◆ verify_counter()

static int verify_counter ( struct ctrl_cmd cmd,
const char *  value,
void *  data 
)
static

◆ verify_rate_ctr()

static int verify_rate_ctr ( struct ctrl_cmd cmd,
const char *  value,
void *  data 
)
static

Variable Documentation

◆ ctrl_initialized

int ctrl_initialized = 0
static

Referenced by ctrl_init().

◆ ctrl_node_vec

vector ctrl_node_vec