From e685db7fa718ca05cfe358ceb23612c2ad587c02 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Tue, 6 Jul 2021 19:44:01 +0200 Subject: [PATCH] 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 --- local_node.c | 17 ----------------- main.c | 1 + monitor.c | 5 +++++ node.h | 1 + parse.c | 2 ++ remote.c | 5 +++++ remote.h | 2 ++ ubus.c | 38 ++++++++++++++++++++++++++++++-------- usteer.h | 2 +- 9 files changed, 47 insertions(+), 26 deletions(-) diff --git a/local_node.c b/local_node.c index 97cb716..985b37b 100644 --- a/local_node.c +++ b/local_node.c @@ -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; diff --git a/main.c b/main.c index a26818e..9e35bf9 100644 --- a/main.c +++ b/main.c @@ -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); diff --git a/monitor.c b/monitor.c index bf02ed7..6257837 100644 --- a/monitor.c +++ b/monitor.c @@ -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); diff --git a/node.h b/node.h index 9d5b7aa..d177ced 100644 --- a/node.h +++ b/node.h @@ -70,6 +70,7 @@ struct usteer_remote_host { struct avl_node avl; struct list_head nodes; + struct blob_attr *host_info; char *addr; }; diff --git a/parse.c b/parse.c index d8bd359..0ff2dfb 100644 --- a/parse.c +++ b/parse.c @@ -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; } diff --git a/remote.c b/remote.c index e5b2797..de183a4 100644 --- a/remote.c +++ b/remote.c @@ -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); } diff --git a/remote.h b/remote.h index b749f64..1c01233 100644 --- a/remote.h +++ b/remote.h @@ -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 { diff --git a/ubus.c b/ubus.c index 4ae5ef9..bc328f3 100644 --- a/ubus.c +++ b/ubus.c @@ -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); } +} + +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_local_node_update_node_info(ln); + 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; } diff --git a/usteer.h b/usteer.h index 81ed1f4..f8ffc8d 100644 --- a/usteer.h +++ b/usteer.h @@ -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); -- 2.30.2