Skip to content

Commit

Permalink
mctp-netlink: Add a facility to store external data on links
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jk-ozlabs committed Jun 10, 2024
1 parent 768b4d5 commit 2e09cfc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/mctp-netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct linkmap_entry {

mctp_eid_t *local_eids;
size_t num_local;

void *userdata;
};

struct mctp_nl {
Expand Down Expand Up @@ -242,15 +244,15 @@ 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;

// 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)
Expand All @@ -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,
Expand All @@ -280,13 +283,15 @@ 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) {
ch = push_change(changes, &siz);
ch->op = MCTP_NL_CHANGE_UP;
ch->ifindex = ne->ifindex;
ch->old_up = oe->up;
ch->link_userdata = oe->userdata;
}
o++;
n++;
Expand All @@ -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++;
}
}
Expand Down Expand Up @@ -929,6 +935,23 @@ 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;
}

void *mctp_nl_get_userdata(const mctp_nl *nl, int ifindex)
{
struct linkmap_entry *entry = entry_byindex(nl, ifindex);

return entry ? entry->userdata : NULL;
}

bool mctp_nl_up_byindex(const mctp_nl *nl, int index)
{
struct linkmap_entry *entry = entry_byindex(nl, index);
Expand Down
16 changes: 16 additions & 0 deletions src/mctp-netlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -69,6 +73,18 @@ 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 also 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);

/* Returns NULL if the link does not exist */
void *mctp_nl_get_userdata(const mctp_nl *nl, int ifindex);

/* MCTP route helper */
int mctp_nl_route_add(struct mctp_nl *nl, uint8_t eid, const char* ifname,
uint32_t mtu);
Expand Down

0 comments on commit 2e09cfc

Please sign in to comment.