neels has uploaded this change for review. (
https://gerrit.osmocom.org/c/libosmocore/+/37881?usp=email )
Change subject: CTRL: get rate_ctr_group by the id from
rate_ctr_group_set_name()
......................................................................
CTRL: get rate_ctr_group by the id from rate_ctr_group_set_name()
Some objects always have fixed indexes, because they come from the
config file (think "bts.0" or "msc.0").
However, some object IDs are created dynamically, which makes it
"impossible" to verify them in tests. In osmo-hnbgw, the same hNodeB may
show up as hnb.0, hnb.1, etc.
We do already have the rate_ctr_group_set_name() API which allows
tagging an identifier on a rate counter group. For above example,
osmo-hnbgw sets rate_ctr_group_set_name(cell_id_str).
In CTRL, when a counter group index token is not a clean number, look up
that token as the group instance's name instead. This allows for
example:
GET 123 rate_ctr.abs.hnb.001-01-L2342-R0-S55-C1.iuh:established
in addition to currently:
GET 123 rate_ctr.abs.hnb.0.iuh:established
When merging this patch, magically all rate_ctr_groups in all osmo
programs will become accessible by their given rate_ctr_group_set_name()
IDs.
Related: OS#6545
Change-Id: I841a12f76e6fcb2bd7aecb5f4b1707d9ec927137
---
M include/osmocom/core/rate_ctr.h
M src/core/libosmocore.map
M src/core/rate_ctr.c
M src/ctrl/control_if.c
4 files changed, 52 insertions(+), 15 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/81/37881/1
diff --git a/include/osmocom/core/rate_ctr.h b/include/osmocom/core/rate_ctr.h
index 0a3eb2c..6e336db 100644
--- a/include/osmocom/core/rate_ctr.h
+++ b/include/osmocom/core/rate_ctr.h
@@ -117,6 +117,7 @@
int rate_ctr_init(void *tall_ctx);
struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const
unsigned int idx);
+struct rate_ctr_group *rate_ctr_get_group_by_name_name(const char *group_name,
const char *instance_name);
const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg,
const char *name);
typedef int (*rate_ctr_handler_t)(
diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map
index c5ab6e3..f0a7cb8 100644
--- a/src/core/libosmocore.map
+++ b/src/core/libosmocore.map
@@ -607,6 +607,7 @@
rate_ctr_for_each_group;
rate_ctr_get_by_name;
rate_ctr_get_group_by_name_idx;
+rate_ctr_get_group_by_name_name;
rate_ctr_group_alloc;
rate_ctr_group_free;
rate_ctr_group_get_ctr;
diff --git a/src/core/rate_ctr.c b/src/core/rate_ctr.c
index 44e2658..6e2c4a4 100644
--- a/src/core/rate_ctr.c
+++ b/src/core/rate_ctr.c
@@ -279,6 +279,9 @@
*/
void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name)
{
+ if (!osmo_identifier_valid(name))
+ LOGP(DLGLOBAL, LOGL_ERROR, "The application is setting an
invalid rate counter group name: %s\n",
+ osmo_quote_str(name, -1));
osmo_talloc_replace_string(grp, &grp->name, name);
}
@@ -390,10 +393,10 @@
return 0;
}
-/*! Search for counter group based on group name and index
- * \param[in] name Name of the counter group you're looking for
- * \param[in] idx Index inside the counter group
- * \returns \ref rate_ctr_group or NULL in case of error */
+/*! Search for counter group based on group name and index.
+ * \param[in] group_name Name of the counter group family (like a "bsc",
"hnb", ...).
+ * \param[in] idx Instance index inside the counter group (like 23 in
"bsc.23").
+ * \returns \ref rate_ctr_group or NULL in case of error. */
struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const
unsigned int idx)
{
struct rate_ctr_group *ctrg;
@@ -410,6 +413,29 @@
return NULL;
}
+/*! Search for counter group based on group name and instance name.
+ * \param[in] group_name Name of the counter group family (like a "bsc",
"hnb", ...).
+ * \param[in] instance_name Name given to the specific instance of the
counter group (something like a cell ID string,
+ * it is up to the calling application to set
rate_ctr_group_set_name() on each created instance).
+ * \returns \ref rate_ctr_group or NULL in case of error */
+struct rate_ctr_group *rate_ctr_get_group_by_name_name(const char *group_name,
const char *instance_name)
+{
+ struct rate_ctr_group *ctrg;
+
+ llist_for_each_entry(ctrg, &rate_ctr_groups, list) {
+ if (!ctrg->desc)
+ continue;
+
+ if (strcmp(ctrg->desc->group_name_prefix, group_name))
+ continue;
+
+ if (strcmp(ctrg->name, instance_name))
+ continue;
+ return ctrg;
+ }
+ return NULL;
+}
+
/*! Search for counter based on group + name
* \param[in] ctrg pointer to \ref rate_ctr_group
* \param[in] name name of counter inside group
diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c
index c265c3a..8445c54 100644
--- a/src/ctrl/control_if.c
+++ b/src/ctrl/control_if.c
@@ -633,8 +633,8 @@
static int get_rate_ctr(struct ctrl_cmd *cmd, void *data)
{
int intv;
- unsigned int idx;
- char *ctr_group, *ctr_idx, *tmp, *dup, *saveptr, *interval;
+ int idx;
+ char *ctr_group, *ctr_idx_or_name, *tmp, *dup, *saveptr, *interval;
struct rate_ctr_group *ctrg;
const struct rate_ctr *ctr;
@@ -680,21 +680,30 @@
}
ctr_group = strtok_r(NULL, ".", &saveptr);
- ctr_idx = strtok_r(NULL, ".", &saveptr);
- if (!ctr_group || !ctr_idx) {
+ ctr_idx_or_name = strtok_r(NULL, ".", &saveptr);
+ if (!ctr_group || !ctr_idx_or_name) {
talloc_free(dup);
cmd->reply = "Counter group must be of name.index form e. g. "
"e1inp.0";
goto err;
}
- idx = atoi(ctr_idx);
-
- ctrg = rate_ctr_get_group_by_name_idx(ctr_group, idx);
- if (!ctrg) {
- talloc_free(dup);
- cmd->reply = "Counter group with given name and index not
found";
- goto err;
+ /* If the token is a valid integer, osmo_str_to_int() returns success,
and we interpret it as an index. If not,
+ * try to look up a counter group instance name instead. */
+ if (osmo_str_to_int(&idx, ctr_idx_or_name, 10, 0, INT_MAX) == 0) {
+ ctrg = rate_ctr_get_group_by_name_idx(ctr_group, idx);
+ if (!ctrg) {
+ talloc_free(dup);
+ cmd->reply = "Counter group with given name and index
not found";
+ goto err;
+ }
+ } else {
+ ctrg = rate_ctr_get_group_by_name_name(ctr_group,
ctr_idx_or_name);
+ if (!ctrg) {
+ talloc_free(dup);
+ cmd->reply = "Counter group with given name and
instance ID not found";
+ goto err;
+ }
}
if (!strlen(saveptr)) {
--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/37881?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I841a12f76e6fcb2bd7aecb5f4b1707d9ec927137
Gerrit-Change-Number: 37881
Gerrit-PatchSet: 1
Gerrit-Owner: neels <[email protected]>