add support for defining global host info

Change the '*' in the node info update cmd to refer to host info instead

Signed-off-by: Felix Fietkau <nbd@nbd.name>
This commit is contained in:
Felix Fietkau
2021-07-06 19:44:01 +02:00
parent 0029f3ce2e
commit e685db7fa7
9 changed files with 47 additions and 26 deletions

View File

@@ -506,23 +506,6 @@ node_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
usteer_register_node(ctx, obj->path, obj->id);
}
void usteer_local_node_update_node_info(struct usteer_local_node *ln)
{
struct blob_attr *val;
const char *name;
blob_buf_init(&b, 0);
kvlist_for_each(&ln->node_info, name, val)
blobmsg_add_field(&b, blobmsg_type(val), name,
blobmsg_data(val), blobmsg_len(val));
val = b.head;
if (!blobmsg_len(val))
val = NULL;
usteer_node_set_blob(&ln->node.node_info, val);
}
void config_set_node_up_script(struct blob_attr *data)
{
const char *val;

1
main.c
View File

@@ -26,6 +26,7 @@
struct ubus_context *ubus_ctx;
struct usteer_config config = {};
struct blob_attr *host_info_blob;
uint64_t current_time;
LIST_HEAD(node_handlers);

View File

@@ -117,6 +117,11 @@ decode_packet(struct blob_attr *data)
}
fprintf(stderr, "id=%08x, seq=%d\n", msg.id, msg.seq);
if (msg.host_info) {
char *data = blobmsg_format_json(msg.host_info, true);
fprintf(stderr, "\tHost info: %s\n", data);
free(data);
}
blob_for_each_attr(cur, msg.nodes, rem)
decode_node(cur);

1
node.h
View File

@@ -70,6 +70,7 @@ struct usteer_remote_host {
struct avl_node avl;
struct list_head nodes;
struct blob_attr *host_info;
char *addr;
};

View File

@@ -26,6 +26,7 @@ bool parse_apmsg(struct apmsg *msg, struct blob_attr *data)
[APMSG_ID] = { .type = BLOB_ATTR_INT32 },
[APMSG_SEQ] = { .type = BLOB_ATTR_INT32 },
[APMSG_NODES] = { .type = BLOB_ATTR_NESTED },
[APMSG_HOST_INFO] = { .type = BLOB_ATTR_NESTED },
};
struct blob_attr *tb[__APMSG_MAX];
@@ -36,6 +37,7 @@ bool parse_apmsg(struct apmsg *msg, struct blob_attr *data)
msg->id = blob_get_int32(tb[APMSG_ID]);
msg->seq = blob_get_int32(tb[APMSG_SEQ]);
msg->nodes = tb[APMSG_NODES];
msg->host_info = tb[APMSG_HOST_INFO];
return true;
}

View File

@@ -305,6 +305,7 @@ interface_recv_msg(struct interface *iface, struct in_addr *addr, void *buf, int
inet_ntop(AF_INET, addr, addr_str, sizeof(addr_str));
host = interface_get_host(addr_str, msg.id);
usteer_node_set_blob(&host->host_info, msg.host_info);
blob_for_each_attr(cur, msg.nodes, rem)
interface_add_node(host, cur);
@@ -492,6 +493,10 @@ usteer_update_init(void)
blob_buf_init(&buf, 0);
blob_put_int32(&buf, APMSG_ID, local_id);
blob_put_int32(&buf, APMSG_SEQ, ++msg_seq);
if (host_info_blob)
blob_put(&buf, APMSG_HOST_INFO,
blob_data(host_info_blob),
blob_len(host_info_blob));
return blob_nest_start(&buf, APMSG_NODES);
}

View File

@@ -26,6 +26,7 @@ enum {
APMSG_ID,
APMSG_SEQ,
APMSG_NODES,
APMSG_HOST_INFO,
__APMSG_MAX
};
@@ -33,6 +34,7 @@ struct apmsg {
uint32_t id;
uint32_t seq;
struct blob_attr *nodes;
struct blob_attr *host_info;
};
enum {

38
ubus.c
View File

@@ -29,6 +29,7 @@
#include "event.h"
static struct blob_buf b;
static KVLIST(host_info, kvlist_blob_len);
static int
usteer_ubus_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
@@ -319,6 +320,10 @@ usteer_ubus_remote_hosts(struct ubus_context *ctx, struct ubus_object *obj,
avl_for_each_element(&remote_hosts, host, avl) {
c = blobmsg_open_table(&b, host->addr);
blobmsg_add_u32(&b, "id", (uint32_t)(uintptr_t)host->avl.key);
if (host->host_info)
blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "host_info",
blobmsg_data(host->host_info),
blobmsg_len(host->host_info));
blobmsg_close_table(&b, c);
}
@@ -361,20 +366,36 @@ static const struct blobmsg_policy del_node_data_policy[] = {
};
static void
__usteer_ubus_update_node_data(struct usteer_local_node *ln, struct blob_attr *data,
bool delete)
usteer_update_kvlist_data(struct kvlist *kv, struct blob_attr *data,
bool delete)
{
struct blob_attr *cur;
int rem;
blobmsg_for_each_attr(cur, data, rem) {
if (delete)
kvlist_delete(&ln->node_info, blobmsg_get_string(cur));
kvlist_delete(kv, blobmsg_get_string(cur));
else
kvlist_set(&ln->node_info, blobmsg_name(cur), cur);
kvlist_set(kv, blobmsg_name(cur), cur);
}
}
usteer_local_node_update_node_info(ln);
static void
usteer_update_kvlist_blob(struct blob_attr **dest, struct kvlist *kv)
{
struct blob_attr *val;
const char *name;
blob_buf_init(&b, 0);
kvlist_for_each(kv, name, val)
blobmsg_add_field(&b, blobmsg_type(val), name,
blobmsg_data(val), blobmsg_len(val));
val = b.head;
if (!blobmsg_len(val))
val = NULL;
usteer_node_set_blob(dest, val);
}
static int
@@ -406,13 +427,14 @@ usteer_ubus_update_node_data(struct ubus_context *ctx, struct ubus_object *obj,
if (!ln)
return UBUS_STATUS_NOT_FOUND;
__usteer_ubus_update_node_data(ln, val, delete);
usteer_update_kvlist_data(&ln->node_info, val, delete);
usteer_update_kvlist_blob(&ln->node.node_info, &ln->node_info);
return 0;
}
avl_for_each_element(&local_nodes, ln, node.avl)
__usteer_ubus_update_node_data(ln, val, delete);
usteer_update_kvlist_data(&host_info, val, delete);
usteer_update_kvlist_blob(&host_info_blob, &host_info);
return 0;
}

View File

@@ -222,6 +222,7 @@ extern struct avl_tree stations;
extern struct ubus_object usteer_obj;
extern uint64_t current_time;
extern const char * const event_types[__EVENT_TYPE_MAX];
extern struct blob_attr *host_info_blob;
void usteer_update_time(void);
void usteer_init_defaults(void);
@@ -230,7 +231,6 @@ bool usteer_handle_sta_event(struct usteer_node *node, const uint8_t *addr,
void usteer_local_nodes_init(struct ubus_context *ctx);
void usteer_local_node_kick(struct usteer_local_node *ln);
void usteer_local_node_update_node_info(struct usteer_local_node *ln);
void usteer_ubus_init(struct ubus_context *ctx);
void usteer_ubus_kick_client(struct sta_info *si);