Commit Graph

52 Commits

Author SHA1 Message Date
Harald Welte b4186824c2 ctrl: Add doxygen API documentation; generate html from it
Closes: OS#3293
Change-Id: I8dc2f24d4bf557ff7bb0f2f46881f9f8d9d7f86f
2018-05-26 21:58:15 +02:00
Harald Welte 3b8921fae2 cosmetic: Whitespace fixes in control_if.c
Change-Id: I24666d0b90a355e9fdefd280d48900b8cac1de64
2018-05-26 10:22:22 +00:00
Pau Espin 686eba9bfc control_if: Avoid heap-use-after-free in osmo_wqueue_bfd_cb
Imagine following scenario:
1- client connects to CTRL iface, a new conn is created with POLL_READ
enabled.
2- A non-related event happens which triggers a TRAP to be sent. As a
result, the wqueue for the conn has now enabled POLL_WRITE, and message
will be sent next time we go through osmo_main_select().
3- At the same time, we receive the GET cmd from the CTRL client, which
means POLL_READ event will be also triggered next time we call
osmo_main_select().
4- osmo_main_select triggers osmo_wqueue_bfd_cb with both READ/WRITE
flags set.
5- The read_cb of wqueue is executed first. The handler closes the CTRL
conn for some reason, freeing the osmo_fd struct and returns.
6- osmo_qeueue_bfd_cb keeps using the already freed osmo_fd and calls
write_cb.

So in step 6 we get a heap-use-after-free catched by AddressSanitizer:

20180424135406115 DLCTRL <0018> control_if.c:506 accept()ed new CTRL connection from (r=10.42.42.1:53910<->l=10.42.42.7:4249)
20180424135406116 DLCTRL <0018> control_cmd.c:378 Command: GET bts.0.oml-connection-state
20180424135406117 DLINP <0013> bts_ipaccess_nanobts.c:417 Identified BTS 1/0/0
20180424135406118 DNM <0005> abis_nm.c:1628 Get Attr (bts=0)
20180424135406118 DNM <0005> abis_nm.c:1628 Get Attr (bts=0)
20180424135406118 DCTRL <000e> osmo_bsc_ctrl.c:158 BTS connection (re)established, sending TRAP.
20180424135406119 DLCTRL <0018> control_if.c:173 close()d CTRL connection (r=10.42.42.1:53910<->l=10.42.42.7:4249)
=================================================================
==12301==ERROR: AddressSanitizer: heap-use-after-free on address 0x611000003e04 at pc 0x7f23091c3a2f bp 0x7ffc0cb73ff0 sp 0x7ffc0cb73fe8
READ of size 4 at 0x611000003e04 thread T0
    #0 0x7f23091c3a2e in osmo_wqueue_bfd_cb /home/osmocom-build/jenkins/workspace/osmo-gsm-tester_build-osmo-bsc/libosmocore/src/write_queue.c:65
    #1 0x7f23091ad5d8 in osmo_fd_disp_fds /home/osmocom-build/jenkins/workspace/osmo-gsm-tester_build-osmo-bsc/libosmocore/src/select.c:216
    #2 0x7f23091ad5d8 in osmo_select_main /home/osmocom-build/jenkins/workspace/osmo-gsm-tester_build-osmo-bsc/libosmocore/src/select.c:256
    #3 0x56538bdb7a26 in main /home/osmocom-build/jenkins/workspace/osmo-gsm-tester_build-osmo-bsc/osmo-bsc/src/osmo-bsc/osmo_bsc_main.c:532
    #4 0x7f23077532e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
    #5 0x56538bdb8999 in _start (/home/jenkins/workspace/osmo-gsm-tester_run-prod/trial-896/inst/osmo-bsc/bin/osmo-bsc+0x259999)

Fixes: OS#3206

Change-Id: I84d10caaadcfa6bd46ba8756ca89aa0badcfd2e3
2018-05-04 18:29:26 +02:00
Neels Hofmeyr 7c0031fc80 cosmetic: flatten ctrl_handle_msg()
Change-Id: I3a711f5c974b7f56e27b333d390d1a706fb57007
2018-04-05 03:11:52 +02:00
Neels Hofmeyr cdbc9afe5d ctrl: fix deferred commands (and hence fix osmo-bts-sysmo 'clock-info' cmd)
The CTRL interface has a ctrl_cmd_def_* API that allows deferring a CTRL
command reply until later. However, the command handling currently fails to
acknowledge this and deallocates the struct ctrl_cmd anyway.

Fix: in struct ctrl_cmd, add a defer pointer to be populated by
ctrl_cmd_def_make(). A cmd thus marked as deferred is not deallocated at the
end of command handling. This fix needs no change in calling code.

(Another idea was to return a different code than CTRL_CMD_HANDLED when the
command is to be deferred, but that would require adjusting each user of
ctrl_cmd_def_make(). The implicit marking is safer and easier.)

Show that handling deferred commands is fixed by adjusting the expectations of
ctrl_test.c's test_deferred_cmd() and removing the now obsolete exit_early
label.

One symptom of the breakage is that osmo-bts-sysmo crashes when asked to report
a trx's clock-info, which is aggravated by the fact that the sysmobts-mgr does
ask osmo-bts-sysmo for a clock-info.

The crash appears since Id583b413f8b8bd16e5cf92a8a9e8663903646381 -- it looked
like just fixing an obvious memory leak, which it did as shown by the unit
test, but deferred ctrl commands actually relied on that leak. Both fixed now.

Related: OS#3120
Change-Id: I24232be7dcf7be79f4def91ddc8b8f8005b56318
2018-04-05 03:11:49 +02:00
Harald Welte 5995281fd6 CTRL: Ensure peer/connection info is always printed the same way
Now that we use osmo_sock_get_name() to print connection information
at disconnect, let's use the same also at accept() time.

Furthermore, let's call it CTRL connection everywhere for consistency.

Change-Id: I33ee7d0ed853c5b2a4ae4e8ef945f8f27753cdea
2017-12-22 18:05:48 +01:00
Harald Welte 29e2798ec5 control_if: Log the disconnect of a CTRL client
We are logging new CTRL connections at LOGL_INFO, so we should
also log disconnects for symmetry.

Change-Id: Id30aa76a5a3dab32d6b4121ce6fdf56d71dfc2ba
2017-12-22 18:05:45 +01:00
Harald Welte f360b42ca1 control_if: Close control connection socket/fd on read/write == 0
When read() or write() system calls return '0' on a stream socket,
it means that the connection has been closed ("EOF").  We must
accordingly close this socket and remove all related state.

Before this patch, every new CTRL connection would introduce a leak
of both some memory/state, as well as a file descriptor :(

Change-Id: I4fb70e5f123b37dece29f156c5f430c875e7cbaf
2017-12-22 16:48:14 +01:00
Neels Hofmeyr c0b0b62305 ctrl: on parse errors, return a detailed message to sender
The recently added ctrl_cmd_parse2() returns non-NULL cmd with error messages
upon parsing errors. In handle_control_read(), use ctrl_cmd_parse2() and send
those back to the CTRL command sender as reply.

Retain the previous "Command parser error" reply only in case ctrl_cmd_parse2()
should return NULL, which shouldn't actually happen at all.

Change-Id: Ie35a02555b76913bb12734a76fc40fde7ffb244d
2017-12-18 23:05:50 +00:00
Neels Hofmeyr f2c10f1082 ctrl: fix mem leak when handling GET_REPLY and SET_REPLY
In ctrl_handle_msg() (code recently propagated from handle_control_read()),
talloc_free() the parsed ctrl_cmd in all code paths. In particular, a free was
missing in case ctrl_cmd_handle() returns CTRL_CMD_HANDLED.

CTRL_CMD_HANDLED is triggered by GET_REPLY / SET_REPLY parsing, as show by
ctrl_test.c. With the memleak fixed, adjust expected test output and make a
detected mem leak abort the test immediately.

Change-Id: Id583b413f8b8bd16e5cf92a8a9e8663903646381
2017-12-18 23:05:50 +00:00
Neels Hofmeyr d53d216944 ctrl: prep test: separate new ctrl_handle_msg() from handle_control_read()
In order to allow unit testing the ctrl iface msgb handling, have a separate
msgb entry point function from the actual fd read function.

An upcoming patch will prove a memory leak in CTRL msgb handling by a unit test
that needs this separation.

Change-Id: Ie09e39db668b866eeb80399b82e7b04b8f5ad7c3
2017-12-18 23:05:49 +00:00
Max 7dc8e88d64 ctrl: make response easier to parse
Previously ctrl request for all counters in
group (e. g. 'rate_ctr.abs.msc.0') will result in human-readable
description which is not regular enough and is hard to both parse and
generate. The ctrl interface is intended for m2m, not for human
interaction. Let's simplify things by making response similar to counter
group request ('rate_ctr.*').

Reply now looks as follows:
GET_REPLY 9084354783926137287 rate_ctr.abs.msc.0 loc_update_type:attach 0;loc_update_type:normal 0;

Previously it was:
GET_REPLY 9084354783926137287 rate_ctr.abs.msc.0 All counters in msc.0
loc_update_type:attach 0
loc_update_type:normal 0

Change-Id: I7a24cc307450efdcd28168fffe477320c59fcd36
Related: OS#2550
2017-11-27 16:44:25 +00:00
Max b4718fd233 Improve get_rate_ctr() error handling
Report back expected interval values.

Change-Id: I05ca7f716342af4f7424b28216ed6c1bf2bd589f
Related: OS#2550
2017-11-24 13:33:19 +00:00
Max b214af5360 ctrl: log incorrect interval values
This should never happen with the current code, but if it ever does, we
should log the error instead of silently returning 0.

Change-Id: I544001d3072e5f12a96a67e4178f9b945c5f6b6c
Related: OS#2550
2017-11-24 13:51:57 +01:00
Max 52a38ddfde Ctrl: add rate counter group introspection
Before user have to know group name and index in advance to request rate
counter value. Introduce introspection function which allows user to
obtain all the groups and their indexes by requesting 'rate_ctr.*'
variable.

This simplifies KPI dumping over ctrl interface.

Change-Id: Ifad8b4f0360c8bcd123a838676516476e84c246a
Related: OS#2550
2017-11-24 10:44:28 +00:00
Harald Welte e08da97570 Fix/Update copyright notices; Add SPDX annotation
Let's fix some erroneous/accidential references to wrong license,
update copyright information where applicable and introduce a
SPDX-License-Identifier to all files.

Change-Id: I39af26c6aaaf5c926966391f6565fc5936be21af
2017-11-13 01:35:12 +09:00
Neels Hofmeyr ea66852a62 ctrl: allow more nodes than those in enum ctrl_node_type
Add ctrl_interface_setup_dynip2() to add a node_count parameter, which can be
used to define more ctrl nodes without having to merge a patch to libosmocore.

In consequence, also add ctrl_handle_alloc2(), since
ctrl_interface_setup_dynip() uses ctrl_handle_alloc() to allocate the node
slots, and add node_count param to static ctrl_init().

Passing zero as node_count indicates to use the default of _LAST_CTRL_NODE as
before, i.e. to not define more ctrl nodes. Assert that we never allocate less
than _LAST_CTRL_NODE slots.

The current ctrl_interface_setup_dynip() and ctrl_handle_alloc() become simple
wrappers that pass zero as node_count. Their use is still valid and they do not
need to be deprecated.

The API comment to ctrl_interface_setup_dynip2() explains how to define more
node IDs.

This patch was verified to work by osmo-hlr.git change
I98ee6a06b3aa6a67adb868e0b63b0e04eb42eb50 which adds two node IDs for use by
osmo-hlr only.

Change-Id: I1bd62ae0d4eefde7e1517db15a2155640a1bab58
2017-10-23 22:31:01 +00:00
Harald Welte 216338c369 Rename 'statistics.c' to 'counter.c'
With stat_item, stats.c and stats_statsd.c, it is becoming a bit
difficult to understand file naming.  Also, the 'statistics.c' file
actually only contained osmo_counter handling, so let's rename it to
counter.c altogether.

Change-Id: I2cfb2310543902b7da46cb15a76e2da317eaed7d
2017-10-15 19:51:35 +02:00
Neels Hofmeyr 17518fe393 doxygen: unify use of \file across the board
Considering the various styles and implications found in the sources, edit
scores of files to follow the same API doc guidelines around the doxygen
grouping and the \file tag.

Many files now show a short description in the generated API doc that was so
far only available as C comment.

The guidelines and reasoning behind it is documented at
https://osmocom.org/projects/cellular-infrastructure/wiki/Guidelines_for_API_documentation

In some instances, remove file comments and add to the corresponding group
instead, to be shared among several files (e.g. bitvec).

Change-Id: Ifa70e77e90462b5eb2b0457c70fd25275910c72b
2017-06-23 00:18:23 +00:00
Neels Hofmeyr 87e4550585 doxygen: enable AUTOBRIEF, drop \brief
Especially for short descriptions, it is annoying to have to type \brief for
every single API doc.

Drop all \brief and enable the AUTOBRIEF feature of doxygen, which always takes
the first sentence of an API doc as the brief description.

Change-Id: I11a8a821b065a128108641a2a63fb5a2b1916e87
2017-06-23 00:18:22 +00:00
Max 33c7ba6934 Simplify ctrl cmd lookup
Replace if-else ladder & gotos with single switch statement & explicit
return to make reading code easier.

Change-Id: Ida1b389b571c60c26813cd29e61b3e4423c5df0f
2017-05-08 08:36:50 +00:00
Harald Welte 0b2f7153f2 control_if: Don't use magic number '5' when allocating vector
We have a proper constant for this (_LAST_CTRL_NODE), so let's use it.

Change-Id: I46275e644166156cb665da70d2964008f1c6cd88
2017-04-27 09:50:51 +02:00
Harald Welte 31c0fef2fd control_if: Add control interface commands for FSMs
This allows programmatic access to introspection of FSM instances, which
is quite handy from e.g. external test cases: Send a message to the
code, then use the CTRL interface to check if that message has triggered
the right kind of state transition.

Change-Id: I0f80340ee9c61c88962fdd6764a6098a844d0d1e
2017-04-27 09:50:47 +02:00
Harald Welte f85861d6eb control_if: Add helper function for 'local execution' of control command
Sometimes (particularly when testing), we may want to parse+execute an
arbitrary control command simply form a string buffer, rather than from
a msgb.  Let's add a helper for that.

Change-Id: Iaca748e0d942bb2a1ee7c2776b37485e1439eb0c
2017-04-27 09:50:33 +02:00
Harald Welte 79c137c654 control_if: Add API to initialize control interface without TCP port bind
When executing test cases, we don't want to bind to a local TCP port, as
we cannot make assumptions as to which ports are actually free.

Change-Id: I5717f9dd92d1f143f069cecd4b4c8ba3d03b25f8
2017-04-26 13:47:06 +02:00
Harald Welte d6b1f85fd8 ctrl: Allow installation of additional node lookup helpers
The existing code assumes that the main application knows about all
control command nodes and can thus present one lookup function.

As libraries are getting their own control interface handling, this
is too restrictive, and we need a way how library code can dynamically
register more node lookup helpers.   We can now do this by means of a
ctrl_lookup_register() function.

Change-Id: Ib69908d1c57f5bb721d5496e3b4a5258fca450e3
2017-04-26 09:22:19 +00:00
Thorsten Alteholz a81055db1d fix spelling in API docu, command reply, logging, descriptions
sections: ctrl, gb, gsm, vty

Change-Id: Iac211b5cd8504da36b699777b95a2448dd7c3e70
2017-04-23 14:34:18 +00:00
Max 2ed3659cac Handle replies in ctrl_cmd_handle()
Previously *_REPLY and ERROR messages were not explicitly handled which
would lead to sending error in response to them which in turn would
prompt other party to send error as well which would result in infinite
cycle.

Handle it explicitly by logging message id and other relevant data.

Change-Id: Id96f3a2fc81fa4549f49556d83f062c6b2f59e28
Related: OS#1615
2017-03-01 16:37:59 +00:00
Max 9756c4691d Fix client-side ctrl interface helpers
* remove unused ctrl_interface_connect() which is not part of public API
* add default read callback to osmo_ctrl_conn_alloc()

Change-Id: Iaa209e34a849ce0dfe2e29b482c3208ade1a32a4
Related: OS#1615
2017-03-01 16:37:59 +00:00
Harald Welte a1004640ce libosmoctrl: Fix typo in ctrl_interface_connect()
it's osmo_sock_init_ofd(), not osmo_sock_init_ifd()

Change-Id: Ia6a82031a691403f641815862613d99b31a3a159
2017-02-23 21:07:01 +01:00
Max fa9e05e7e8 Expand and expose ctrl connection allocation
Add function for allocating CTRL connection to public headers and
replace call to previous static function with it. Add doxygen docs for
this function.

It's useful if we need to allocate ctrl connection but don't need to
bind to any interfaces: when we act as ctrl client.

Related: OS#1615
Change-Id: I522ed809cbebfd3d7dd08b4ed9137b39ff192e32
2017-02-23 17:30:44 +00:00
Max 32ee5af893 Document ctrl_interface_setup_dynip() function
Change-Id: Ie1d5881dda7a9b797d15e9e1eead8281a994d91e
2017-02-19 08:54:27 +00:00
Harald Welte f12d40f4df fix various compiler warnings (on FreeBSD-11.0)
FreeBSD 11.0 uses clang version 3.8.0 which spits various warnings
during libosmocore compilation.  Let's clean this up a bit.

Change-Id: Ic14572e6970bd0b8916604fabf807f1608fa07e5
2017-02-08 16:49:14 +01:00
Max bc067eb0a2 Add function to send TRAP over Control Interface
Change-Id: Ic0b8d88c4f5c4d42c3f8fb754f8eabf049c9e388
Related: OS#1646
2016-10-12 11:37:49 +00:00
Neels Hofmeyr 38d232ee5d log CTRL bind address and port
Log 'CTRL at 1.2.3.4 5678' from ctrl_interface_setup*. All callers can now drop
any extra 'CTRL at 1.2.3.4 5678' logging.

Change-Id: If449d0514e3d0cc1b346d7452194d931aa090166
2016-09-23 02:39:12 +00:00
Neels Hofmeyr 5b34f773e4 remove unused local variable in get_rate_ctr()
Unused after 22886d9e32
"Fix retrieving rate_ctr over control interface"

Change-Id: Ib5411da80c4eb4f905a5ed87c60477eca2cdff42
2016-09-19 14:18:38 +02:00
Neels Hofmeyr 2e38d358b6 remove unused function get_rate_ctr_group()
Unused after 22886d9e32
"Fix retrieving rate_ctr over control interface"

Change-Id: I405367ef6ba5833957778a79dd398ce5ea29307e
2016-09-19 14:18:38 +02:00
Max 22886d9e32 Fix retrieving rate_ctr over control interface
Allow getting either particular
counter (e. g. rate_ctr.per_hour.e1inp.0.hdlc.abort) or entire rate
counter group for a given index (e. g. rate_ctr.per_hour.e1inp.0).

Change-Id: I2b0109536170f7b5388d3236df30b98f457aa98d
Fixes: OS#1730
Reviewed-on: https://gerrit.osmocom.org/274
Tested-by: Jenkins Builder
Reviewed-by: Harald Welte <laforge@gnumonks.org>
2016-06-14 22:21:24 +00:00
Neels Hofmeyr 4934309dab add ctrl_interface_setup_dynip() for bind address
Make the ctrl interface bind address configurable, so that it may be made
available on other addresses than 127.0.0.1. The specific aim is to allow
running multiple osmo-nitbs alongside each other (commits in openbsc follow).
2016-02-25 11:02:35 +01:00
Holger Hans Peter Freyther 267fd86e7d libctrl: Include config.h so we can include netinet/tcp.h
In e15ac060e7 we tried to fix
the nuttx build but we never included "netinet/tcp.h" after
it and the compiler warned about the unused "on" parameter
which we didn't notice because of the other warnings...

Include config.h so we can see if there is a tcp.h and then
include it.
2015-03-18 21:54:37 +01:00
Harald Welte e15ac060e7 fix libosmocore build for NuttX target
this fixes some compilation issues with libosmocore under NuttX,
particularly as some #defines are missing or some header files are
slightly different.
2014-12-04 14:15:36 +01:00
Harald Welte 39c9e7b471 libctrl: Add support for 'deferred control commands'
Sometimes a control interface command cannot be processed
and responded immediately, but we need to process it asynchronously.

In order to support this, we introduce the 'ctrl_cmd_def', which
represents such a deferred command.  It is created by the service
implementing the command using ctrl_cmd_def_make(), and a response is
later sent using ctrl_cmd_def_send().

ctrl_cmd_def_is_zombie() must be called to handle the case where
the control connection has disconnected/died between receiving the
command and sending the response.
2014-08-24 16:52:54 +02:00
Holger Hans Peter Freyther 5e21131c8d ctrl: Attempt fix the build on FreeBSD and add include file
IPPROTO_TCP is defined in netinet/in.h. Include it and hope the
build is fixed for FreeBSD.
2014-08-21 16:33:32 +02:00
Harald Welte ae2fcb22cf rename controlif_setup() to ctrl_interface_setup()
which means that all control interface related functions now have
the common ctrl_ prefix.
2014-08-21 15:34:19 +02:00
Harald Welte 528134b01c libctrl: Move bulk of control node lookup inti libosmoctrl
The control interface user now only has to register a very short
node lookup function callback.  This function is optional, and only
required if hierarchical command lookup should be supported.
2014-08-21 15:34:18 +02:00
Harald Welte c78e74e3d0 libctrl: remove 'struct gsm_network' references
libctrl doesn't need any knowledge about the type of the user-private
data that it gets passed upon setup time and includes on callbacks.
2014-08-21 15:34:18 +02:00
Harald Welte acbb4c91b6 libctrl: adopt to recent ipaccess/ipa naming change 2014-08-21 15:34:18 +02:00
Harald Welte 7fd0c830d9 libctrl: Add DLCTRL as logging context for the control interface
... and make libctrl code use it
2014-08-21 15:34:18 +02:00
Harald Welte c9df37d84a libctrl: Avoid using external tall_bsc_ctx
Instead of using one flat talloc context (and one that is specific to
openbsc), we should attach the objects to whatever parent context they
are being used in.
2014-08-21 15:34:17 +02:00
Harald Welte 1238cc64d7 libctrl: remove openbsc headers, convert from make_sock to libosmocore 2014-08-21 15:34:17 +02:00