Skip to content

Commit 91cbf59

Browse files
committed
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. Signed-off-by: Jeremy Kerr <[email protected]>
1 parent d441a00 commit 91cbf59

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/mctp-netlink.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ struct linkmap_entry {
2828

2929
mctp_eid_t *local_eids;
3030
size_t num_local;
31+
32+
void *userdata;
3133
};
3234

3335
struct mctp_nl {
@@ -242,15 +244,15 @@ static void fill_eid_changes(const struct linkmap_entry *oe,
242244
}
243245

244246
static void fill_link_changes(const struct linkmap_entry *old, size_t old_count,
245-
const struct linkmap_entry *new, size_t new_count,
247+
struct linkmap_entry *new, size_t new_count,
246248
mctp_nl_change **changes, size_t *num_changes) {
247249

248250
size_t siz = 0;
249251

250252
// iterate and match old/new interface lists
251253
for (size_t o = 0, n = 0; o < old_count || n < new_count; ) {
252254
const struct linkmap_entry *oe = &old[o];
253-
const struct linkmap_entry *ne = &new[n];
255+
struct linkmap_entry *ne = &new[n];
254256
mctp_nl_change *ch = NULL;
255257

256258
if (o >= old_count)
@@ -261,6 +263,7 @@ static void fill_link_changes(const struct linkmap_entry *old, size_t old_count,
261263

262264
if (oe && ne && oe->ifindex == ne->ifindex) {
263265
// Same link.
266+
ne->userdata = oe->userdata;
264267
if (oe->net == ne->net) {
265268
// Same net. Check for eid changes.
266269
fill_eid_changes(oe,
@@ -280,13 +283,15 @@ static void fill_link_changes(const struct linkmap_entry *old, size_t old_count,
280283
ch->op = MCTP_NL_CHANGE_NET;
281284
ch->ifindex = ne->ifindex;
282285
ch->old_net = oe->net;
286+
ch->link_userdata = oe->userdata;
283287
}
284288

285289
if (oe->up != ne->up) {
286290
ch = push_change(changes, &siz);
287291
ch->op = MCTP_NL_CHANGE_UP;
288292
ch->ifindex = ne->ifindex;
289293
ch->old_up = oe->up;
294+
ch->link_userdata = oe->userdata;
290295
}
291296
o++;
292297
n++;
@@ -309,6 +314,7 @@ static void fill_link_changes(const struct linkmap_entry *old, size_t old_count,
309314
ch->op = MCTP_NL_DEL_LINK;
310315
ch->ifindex = oe->ifindex;
311316
ch->old_net = oe->net;
317+
ch->link_userdata = oe->userdata;
312318
o++;
313319
}
314320
}
@@ -929,6 +935,23 @@ int mctp_nl_net_byindex(const mctp_nl *nl, int index)
929935
return 0;
930936
}
931937

938+
int mctp_nl_set_link_userdata(mctp_nl *nl, int ifindex, void *userdata)
939+
{
940+
struct linkmap_entry *entry = entry_byindex(nl, ifindex);
941+
if (!entry)
942+
return -1;
943+
944+
entry->userdata = userdata;
945+
return 0;
946+
}
947+
948+
void *mctp_nl_get_link_userdata(const mctp_nl *nl, int ifindex)
949+
{
950+
struct linkmap_entry *entry = entry_byindex(nl, ifindex);
951+
952+
return entry ? entry->userdata : NULL;
953+
}
954+
932955
bool mctp_nl_up_byindex(const mctp_nl *nl, int index)
933956
{
934957
struct linkmap_entry *entry = entry_byindex(nl, index);

src/mctp-netlink.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ struct mctp_nl_change {
3131

3232
// Filled for CHANGE_UP
3333
bool old_up;
34+
35+
// If userdata is present on the link, it is passed here. Populated for
36+
// link change events (DEL_LINK, CHANGE_NET, CHANGE_UP).
37+
void *link_userdata;
3438
};
3539
typedef struct mctp_nl_change mctp_nl_change;
3640

@@ -69,6 +73,18 @@ int *mctp_nl_net_list(const mctp_nl *nl, size_t *ret_num_nets);
6973
/* Returns an allocated list of ifindex, caller to free */
7074
int *mctp_nl_if_list(const mctp_nl *nl, size_t *ret_num_if);
7175

76+
/* Get/set userdata for a link. The userdata is attached to a link
77+
* with index @ifindex. Userdata will also be populated into
78+
* struct mctp_nl_change->userdata, and would typically be freed on
79+
* MCTP_NL_DEL_LINK events
80+
*
81+
* Returns non-zero if the link does not exist.
82+
*/
83+
int mctp_nl_set_link_userdata(mctp_nl *nl, int ifindex, void *userdata);
84+
85+
/* Returns NULL if the link does not exist */
86+
void *mctp_nl_get_link_userdata(const mctp_nl *nl, int ifindex);
87+
7288
/* MCTP route helper */
7389
int mctp_nl_route_add(struct mctp_nl *nl, uint8_t eid, const char* ifname,
7490
uint32_t mtu);

0 commit comments

Comments
 (0)