Skip to content

Commit 53bac83

Browse files
committed
Merge branch 'rtnetlink-use-rtnl_register_many'
Kuniyuki Iwashima says: ==================== rtnetlink: Use rtnl_register_many(). This series converts all rtnl_register() and rtnl_register_module() to rtnl_register_many() and finally removes them. Once this series is applied, I'll start converting doit() to per-netns RTNL. v1: https://lore.kernel.org/20241011220550.46040-1-kuniyu@amazon.com/ ==================== Link: https://patch.msgid.link/20241014201828.91221-1-kuniyu@amazon.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents df24129 + e1c6c38 commit 53bac83

File tree

20 files changed

+267
-258
lines changed

20 files changed

+267
-258
lines changed

include/net/rtnetlink.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
2929
return msgtype & RTNL_KIND_MASK;
3030
}
3131

32+
/**
33+
* struct rtnl_msg_handler - rtnetlink message type and handlers
34+
*
35+
* @owner: NULL for built-in, THIS_MODULE for module
36+
* @protocol: Protocol family or PF_UNSPEC
37+
* @msgtype: rtnetlink message type
38+
* @doit: Function pointer called for each request message
39+
* @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
40+
* @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
41+
*/
3242
struct rtnl_msg_handler {
3343
struct module *owner;
3444
int protocol;
@@ -38,11 +48,6 @@ struct rtnl_msg_handler {
3848
int flags;
3949
};
4050

41-
void rtnl_register(int protocol, int msgtype,
42-
rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
43-
int rtnl_register_module(struct module *owner, int protocol, int msgtype,
44-
rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
45-
int rtnl_unregister(int protocol, int msgtype);
4651
void rtnl_unregister_all(int protocol);
4752

4853
int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n);

net/can/gw.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,15 @@ static struct pernet_operations cangw_pernet_ops = {
12651265
.exit_batch = cangw_pernet_exit_batch,
12661266
};
12671267

1268+
static const struct rtnl_msg_handler cgw_rtnl_msg_handlers[] __initconst_or_module = {
1269+
{.owner = THIS_MODULE, .protocol = PF_CAN, .msgtype = RTM_NEWROUTE,
1270+
.doit = cgw_create_job},
1271+
{.owner = THIS_MODULE, .protocol = PF_CAN, .msgtype = RTM_DELROUTE,
1272+
.doit = cgw_remove_job},
1273+
{.owner = THIS_MODULE, .protocol = PF_CAN, .msgtype = RTM_GETROUTE,
1274+
.dumpit = cgw_dump_jobs},
1275+
};
1276+
12681277
static __init int cgw_module_init(void)
12691278
{
12701279
int ret;
@@ -1290,27 +1299,13 @@ static __init int cgw_module_init(void)
12901299
if (ret)
12911300
goto out_register_notifier;
12921301

1293-
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
1294-
NULL, cgw_dump_jobs, 0);
1295-
if (ret)
1296-
goto out_rtnl_register1;
1297-
1298-
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
1299-
cgw_create_job, NULL, 0);
1300-
if (ret)
1301-
goto out_rtnl_register2;
1302-
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
1303-
cgw_remove_job, NULL, 0);
1302+
ret = rtnl_register_many(cgw_rtnl_msg_handlers);
13041303
if (ret)
1305-
goto out_rtnl_register3;
1304+
goto out_rtnl_register;
13061305

13071306
return 0;
13081307

1309-
out_rtnl_register3:
1310-
rtnl_unregister(PF_CAN, RTM_NEWROUTE);
1311-
out_rtnl_register2:
1312-
rtnl_unregister(PF_CAN, RTM_GETROUTE);
1313-
out_rtnl_register1:
1308+
out_rtnl_register:
13141309
unregister_netdevice_notifier(&notifier);
13151310
out_register_notifier:
13161311
kmem_cache_destroy(cgw_cache);

net/core/fib_rules.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,13 +1291,18 @@ static struct pernet_operations fib_rules_net_ops = {
12911291
.exit = fib_rules_net_exit,
12921292
};
12931293

1294+
static const struct rtnl_msg_handler fib_rules_rtnl_msg_handlers[] __initconst = {
1295+
{.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule},
1296+
{.msgtype = RTM_DELRULE, .doit = fib_nl_delrule},
1297+
{.msgtype = RTM_GETRULE, .dumpit = fib_nl_dumprule,
1298+
.flags = RTNL_FLAG_DUMP_UNLOCKED},
1299+
};
1300+
12941301
static int __init fib_rules_init(void)
12951302
{
12961303
int err;
1297-
rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, 0);
1298-
rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, 0);
1299-
rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule,
1300-
RTNL_FLAG_DUMP_UNLOCKED);
1304+
1305+
rtnl_register_many(fib_rules_rtnl_msg_handlers);
13011306

13021307
err = register_pernet_subsys(&fib_rules_net_ops);
13031308
if (err < 0)
@@ -1312,9 +1317,7 @@ static int __init fib_rules_init(void)
13121317
fail_unregister:
13131318
unregister_pernet_subsys(&fib_rules_net_ops);
13141319
fail:
1315-
rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
1316-
rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
1317-
rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
1320+
rtnl_unregister_many(fib_rules_rtnl_msg_handlers);
13181321
return err;
13191322
}
13201323

net/core/neighbour.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3886,17 +3886,18 @@ EXPORT_SYMBOL(neigh_sysctl_unregister);
38863886

38873887
#endif /* CONFIG_SYSCTL */
38883888

3889+
static const struct rtnl_msg_handler neigh_rtnl_msg_handlers[] __initconst = {
3890+
{.msgtype = RTM_NEWNEIGH, .doit = neigh_add},
3891+
{.msgtype = RTM_DELNEIGH, .doit = neigh_delete},
3892+
{.msgtype = RTM_GETNEIGH, .doit = neigh_get, .dumpit = neigh_dump_info,
3893+
.flags = RTNL_FLAG_DUMP_UNLOCKED},
3894+
{.msgtype = RTM_GETNEIGHTBL, .dumpit = neightbl_dump_info},
3895+
{.msgtype = RTM_SETNEIGHTBL, .doit = neightbl_set},
3896+
};
3897+
38893898
static int __init neigh_init(void)
38903899
{
3891-
rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, 0);
3892-
rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, 0);
3893-
rtnl_register(PF_UNSPEC, RTM_GETNEIGH, neigh_get, neigh_dump_info,
3894-
RTNL_FLAG_DUMP_UNLOCKED);
3895-
3896-
rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3897-
0);
3898-
rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, 0);
3899-
3900+
rtnl_register_many(neigh_rtnl_msg_handlers);
39003901
return 0;
39013902
}
39023903

net/core/net_namespace.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,14 @@ static void __init netns_ipv4_struct_check(void)
11691169
}
11701170
#endif
11711171

1172+
static const struct rtnl_msg_handler net_ns_rtnl_msg_handlers[] __initconst = {
1173+
{.msgtype = RTM_NEWNSID, .doit = rtnl_net_newid,
1174+
.flags = RTNL_FLAG_DOIT_UNLOCKED},
1175+
{.msgtype = RTM_GETNSID, .doit = rtnl_net_getid,
1176+
.dumpit = rtnl_net_dumpid,
1177+
.flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
1178+
};
1179+
11721180
void __init net_ns_init(void)
11731181
{
11741182
struct net_generic *ng;
@@ -1206,11 +1214,7 @@ void __init net_ns_init(void)
12061214
if (register_pernet_subsys(&net_ns_ops))
12071215
panic("Could not register network namespace subsystems");
12081216

1209-
rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
1210-
RTNL_FLAG_DOIT_UNLOCKED);
1211-
rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
1212-
RTNL_FLAG_DOIT_UNLOCKED |
1213-
RTNL_FLAG_DUMP_UNLOCKED);
1217+
rtnl_register_many(net_ns_rtnl_msg_handlers);
12141218
}
12151219

12161220
static void free_exit_list(struct pernet_operations *ops, struct list_head *net_exit_list)

net/core/rtnetlink.c

Lines changed: 58 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -338,65 +338,14 @@ static int rtnl_register_internal(struct module *owner,
338338
return ret;
339339
}
340340

341-
/**
342-
* rtnl_register_module - Register a rtnetlink message type
343-
*
344-
* @owner: module registering the hook (THIS_MODULE)
345-
* @protocol: Protocol family or PF_UNSPEC
346-
* @msgtype: rtnetlink message type
347-
* @doit: Function pointer called for each request message
348-
* @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
349-
* @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
350-
*
351-
* Like rtnl_register, but for use by removable modules.
352-
*/
353-
int rtnl_register_module(struct module *owner,
354-
int protocol, int msgtype,
355-
rtnl_doit_func doit, rtnl_dumpit_func dumpit,
356-
unsigned int flags)
357-
{
358-
return rtnl_register_internal(owner, protocol, msgtype,
359-
doit, dumpit, flags);
360-
}
361-
EXPORT_SYMBOL_GPL(rtnl_register_module);
362-
363-
/**
364-
* rtnl_register - Register a rtnetlink message type
365-
* @protocol: Protocol family or PF_UNSPEC
366-
* @msgtype: rtnetlink message type
367-
* @doit: Function pointer called for each request message
368-
* @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
369-
* @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
370-
*
371-
* Registers the specified function pointers (at least one of them has
372-
* to be non-NULL) to be called whenever a request message for the
373-
* specified protocol family and message type is received.
374-
*
375-
* The special protocol family PF_UNSPEC may be used to define fallback
376-
* function pointers for the case when no entry for the specific protocol
377-
* family exists.
378-
*/
379-
void rtnl_register(int protocol, int msgtype,
380-
rtnl_doit_func doit, rtnl_dumpit_func dumpit,
381-
unsigned int flags)
382-
{
383-
int err;
384-
385-
err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
386-
flags);
387-
if (err)
388-
pr_err("Unable to register rtnetlink message handler, "
389-
"protocol = %d, message type = %d\n", protocol, msgtype);
390-
}
391-
392341
/**
393342
* rtnl_unregister - Unregister a rtnetlink message type
394343
* @protocol: Protocol family or PF_UNSPEC
395344
* @msgtype: rtnetlink message type
396345
*
397346
* Returns 0 on success or a negative error code.
398347
*/
399-
int rtnl_unregister(int protocol, int msgtype)
348+
static int rtnl_unregister(int protocol, int msgtype)
400349
{
401350
struct rtnl_link __rcu **tab;
402351
struct rtnl_link *link;
@@ -419,7 +368,6 @@ int rtnl_unregister(int protocol, int msgtype)
419368

420369
return 0;
421370
}
422-
EXPORT_SYMBOL_GPL(rtnl_unregister);
423371

424372
/**
425373
* rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
@@ -454,6 +402,26 @@ void rtnl_unregister_all(int protocol)
454402
}
455403
EXPORT_SYMBOL_GPL(rtnl_unregister_all);
456404

405+
/**
406+
* __rtnl_register_many - Register rtnetlink message types
407+
* @handlers: Array of struct rtnl_msg_handlers
408+
* @n: The length of @handlers
409+
*
410+
* Registers the specified function pointers (at least one of them has
411+
* to be non-NULL) to be called whenever a request message for the
412+
* specified protocol family and message type is received.
413+
*
414+
* The special protocol family PF_UNSPEC may be used to define fallback
415+
* function pointers for the case when no entry for the specific protocol
416+
* family exists.
417+
*
418+
* When one element of @handlers fails to register,
419+
* 1) built-in: panics.
420+
* 2) modules : the previous successful registrations are unwinded
421+
* and an error is returned.
422+
*
423+
* Use rtnl_register_many().
424+
*/
457425
int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n)
458426
{
459427
const struct rtnl_msg_handler *handler;
@@ -464,6 +432,10 @@ int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n)
464432
handler->msgtype, handler->doit,
465433
handler->dumpit, handler->flags);
466434
if (err) {
435+
if (!handler->owner)
436+
panic("Unable to register rtnetlink message "
437+
"handlers, %pS\n", handlers);
438+
467439
__rtnl_unregister_many(handlers, i);
468440
break;
469441
}
@@ -6839,41 +6811,44 @@ static struct pernet_operations rtnetlink_net_ops = {
68396811
.exit = rtnetlink_net_exit,
68406812
};
68416813

6814+
static const struct rtnl_msg_handler rtnetlink_rtnl_msg_handlers[] __initconst = {
6815+
{.msgtype = RTM_NEWLINK, .doit = rtnl_newlink},
6816+
{.msgtype = RTM_DELLINK, .doit = rtnl_dellink},
6817+
{.msgtype = RTM_GETLINK, .doit = rtnl_getlink,
6818+
.dumpit = rtnl_dump_ifinfo, .flags = RTNL_FLAG_DUMP_SPLIT_NLM_DONE},
6819+
{.msgtype = RTM_SETLINK, .doit = rtnl_setlink},
6820+
{.msgtype = RTM_GETADDR, .dumpit = rtnl_dump_all},
6821+
{.msgtype = RTM_GETROUTE, .dumpit = rtnl_dump_all},
6822+
{.msgtype = RTM_GETNETCONF, .dumpit = rtnl_dump_all},
6823+
{.msgtype = RTM_GETSTATS, .doit = rtnl_stats_get,
6824+
.dumpit = rtnl_stats_dump},
6825+
{.msgtype = RTM_SETSTATS, .doit = rtnl_stats_set},
6826+
{.msgtype = RTM_NEWLINKPROP, .doit = rtnl_newlinkprop},
6827+
{.msgtype = RTM_DELLINKPROP, .doit = rtnl_dellinkprop},
6828+
{.protocol = PF_BRIDGE, .msgtype = RTM_GETLINK,
6829+
.dumpit = rtnl_bridge_getlink},
6830+
{.protocol = PF_BRIDGE, .msgtype = RTM_DELLINK,
6831+
.doit = rtnl_bridge_dellink},
6832+
{.protocol = PF_BRIDGE, .msgtype = RTM_SETLINK,
6833+
.doit = rtnl_bridge_setlink},
6834+
{.protocol = PF_BRIDGE, .msgtype = RTM_NEWNEIGH, .doit = rtnl_fdb_add},
6835+
{.protocol = PF_BRIDGE, .msgtype = RTM_DELNEIGH, .doit = rtnl_fdb_del,
6836+
.flags = RTNL_FLAG_BULK_DEL_SUPPORTED},
6837+
{.protocol = PF_BRIDGE, .msgtype = RTM_GETNEIGH, .doit = rtnl_fdb_get,
6838+
.dumpit = rtnl_fdb_dump},
6839+
{.protocol = PF_BRIDGE, .msgtype = RTM_NEWMDB, .doit = rtnl_mdb_add},
6840+
{.protocol = PF_BRIDGE, .msgtype = RTM_DELMDB, .doit = rtnl_mdb_del,
6841+
.flags = RTNL_FLAG_BULK_DEL_SUPPORTED},
6842+
{.protocol = PF_BRIDGE, .msgtype = RTM_GETMDB, .doit = rtnl_mdb_get,
6843+
.dumpit = rtnl_mdb_dump},
6844+
};
6845+
68426846
void __init rtnetlink_init(void)
68436847
{
68446848
if (register_pernet_subsys(&rtnetlink_net_ops))
68456849
panic("rtnetlink_init: cannot initialize rtnetlink\n");
68466850

68476851
register_netdevice_notifier(&rtnetlink_dev_notifier);
68486852

6849-
rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6850-
rtnl_dump_ifinfo, RTNL_FLAG_DUMP_SPLIT_NLM_DONE);
6851-
rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6852-
rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6853-
rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6854-
6855-
rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6856-
rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6857-
rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
6858-
6859-
rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
6860-
rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
6861-
6862-
rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
6863-
rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
6864-
RTNL_FLAG_BULK_DEL_SUPPORTED);
6865-
rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6866-
6867-
rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6868-
rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6869-
rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
6870-
6871-
rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6872-
0);
6873-
rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
6874-
6875-
rtnl_register(PF_BRIDGE, RTM_GETMDB, rtnl_mdb_get, rtnl_mdb_dump, 0);
6876-
rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0);
6877-
rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL,
6878-
RTNL_FLAG_BULK_DEL_SUPPORTED);
6853+
rtnl_register_many(rtnetlink_rtnl_msg_handlers);
68796854
}

net/dcb/dcbnl.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,6 +2408,11 @@ static struct notifier_block dcbnl_nb __read_mostly = {
24082408
.notifier_call = dcbnl_netdevice_event,
24092409
};
24102410

2411+
static const struct rtnl_msg_handler dcbnl_rtnl_msg_handlers[] __initconst = {
2412+
{.msgtype = RTM_GETDCB, .doit = dcb_doit},
2413+
{.msgtype = RTM_SETDCB, .doit = dcb_doit},
2414+
};
2415+
24112416
static int __init dcbnl_init(void)
24122417
{
24132418
int err;
@@ -2416,8 +2421,7 @@ static int __init dcbnl_init(void)
24162421
if (err)
24172422
return err;
24182423

2419-
rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, 0);
2420-
rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, 0);
2424+
rtnl_register_many(dcbnl_rtnl_msg_handlers);
24212425

24222426
return 0;
24232427
}

0 commit comments

Comments
 (0)