From de00ac902e5cece9d28b47ad2c76519cacabf917 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Mon, 10 Jun 2024 11:58:34 +0800 Subject: [PATCH] mctp-netlink: Add a facility to store external data on links For upcoming mctpd changes, we will need a facility for storing dbus-specific information on each link. This change adds a void * userdata pointer to the links, and accessors for callers to get/set this pointer. For convenience, we also pass this pointer on change events, allowing for a quick way to release/update the userdata on link delete/change. WIP: untested. Signed-off-by: Jeremy Kerr --- src/mctp-netlink.c | 30 ++++++++++++++++++++++++++++-- src/mctp-netlink.h | 14 ++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/mctp-netlink.c b/src/mctp-netlink.c index 34fb668..0b70c7d 100644 --- a/src/mctp-netlink.c +++ b/src/mctp-netlink.c @@ -28,6 +28,8 @@ struct linkmap_entry { mctp_eid_t *local_eids; size_t num_local; + + void *userdata; }; struct mctp_nl { @@ -242,7 +244,7 @@ static void fill_eid_changes(const struct linkmap_entry *oe, } static void fill_link_changes(const struct linkmap_entry *old, size_t old_count, - const struct linkmap_entry *new, size_t new_count, + struct linkmap_entry *new, size_t new_count, mctp_nl_change **changes, size_t *num_changes) { size_t siz = 0; @@ -250,7 +252,7 @@ static void fill_link_changes(const struct linkmap_entry *old, size_t old_count, // iterate and match old/new interface lists for (size_t o = 0, n = 0; o < old_count || n < new_count; ) { const struct linkmap_entry *oe = &old[o]; - const struct linkmap_entry *ne = &new[n]; + struct linkmap_entry *ne = &new[n]; mctp_nl_change *ch = NULL; if (o >= old_count) @@ -261,6 +263,7 @@ static void fill_link_changes(const struct linkmap_entry *old, size_t old_count, if (oe && ne && oe->ifindex == ne->ifindex) { // Same link. + ne->userdata = oe->userdata; if (oe->net == ne->net) { // Same net. Check for eid changes. fill_eid_changes(oe, @@ -280,6 +283,7 @@ static void fill_link_changes(const struct linkmap_entry *old, size_t old_count, ch->op = MCTP_NL_CHANGE_NET; ch->ifindex = ne->ifindex; ch->old_net = oe->net; + ch->link_userdata = oe->userdata; } if (oe->up != ne->up) { @@ -287,6 +291,7 @@ static void fill_link_changes(const struct linkmap_entry *old, size_t old_count, ch->op = MCTP_NL_CHANGE_UP; ch->ifindex = ne->ifindex; ch->old_up = oe->up; + ch->link_userdata = oe->userdata; } o++; n++; @@ -309,6 +314,7 @@ static void fill_link_changes(const struct linkmap_entry *old, size_t old_count, ch->op = MCTP_NL_DEL_LINK; ch->ifindex = oe->ifindex; ch->old_net = oe->net; + ch->link_userdata = oe->userdata; o++; } } @@ -929,6 +935,26 @@ int mctp_nl_net_byindex(const mctp_nl *nl, int index) return 0; } +int mctp_nl_set_userdata(mctp_nl *nl, int ifindex, void *userdata) +{ + struct linkmap_entry *entry = entry_byindex(nl, ifindex); + if (!entry) + return -1; + + entry->userdata = userdata; + return 0; +} + +int mctp_nl_get_userdata(const mctp_nl *nl, int ifindex, void **userdata) +{ + struct linkmap_entry *entry = entry_byindex(nl, ifindex); + if (!entry) + return -1; + + *userdata = entry->userdata; + return 0; +} + bool mctp_nl_up_byindex(const mctp_nl *nl, int index) { struct linkmap_entry *entry = entry_byindex(nl, index); diff --git a/src/mctp-netlink.h b/src/mctp-netlink.h index 31ee6b6..ee67dde 100644 --- a/src/mctp-netlink.h +++ b/src/mctp-netlink.h @@ -31,6 +31,10 @@ struct mctp_nl_change { // Filled for CHANGE_UP bool old_up; + + // If userdata is present on the link, it is passed here. Populated for + // link change events (DEL_LINK, CHANGE_NET, CHANGE_UP). + void *link_userdata; }; typedef struct mctp_nl_change mctp_nl_change; @@ -69,6 +73,16 @@ int *mctp_nl_net_list(const mctp_nl *nl, size_t *ret_num_nets); /* Returns an allocated list of ifindex, caller to free */ int *mctp_nl_if_list(const mctp_nl *nl, size_t *ret_num_if); +/* Get/set userdata for a link. The userdata is attached to a link + * with index @ifindex. Userdata will be populated into + * struct mctp_nl_change->userdata, and would typically be freed on + * MCTP_NL_DEL_LINK events + * + * Returns non-zero if the link does not exist. + */ +int mctp_nl_set_userdata(mctp_nl *nl, int ifindex, void *userdata); +int mctp_nl_get_userdata(const mctp_nl *nl, int ifindex, void **userdata); + /* MCTP route helper */ int mctp_nl_route_add(struct mctp_nl *nl, uint8_t eid, const char* ifname, uint32_t mtu);