xref: /openbmc/linux/net/devlink/devl_internal.h (revision 25573968)
1687125b5SJakub Kicinski /* SPDX-License-Identifier: GPL-2.0-or-later */
2687125b5SJakub Kicinski /* Copyright (c) 2016 Mellanox Technologies. All rights reserved.
3687125b5SJakub Kicinski  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
4687125b5SJakub Kicinski  */
5687125b5SJakub Kicinski 
6687125b5SJakub Kicinski #include <linux/mutex.h>
7687125b5SJakub Kicinski #include <linux/netdevice.h>
8687125b5SJakub Kicinski #include <linux/notifier.h>
9687125b5SJakub Kicinski #include <linux/types.h>
1093e71edfSJakub Kicinski #include <linux/workqueue.h>
11687125b5SJakub Kicinski #include <linux/xarray.h>
12687125b5SJakub Kicinski #include <net/devlink.h>
13687125b5SJakub Kicinski #include <net/net_namespace.h>
14687125b5SJakub Kicinski 
15687125b5SJakub Kicinski #define DEVLINK_REGISTERED XA_MARK_1
16687125b5SJakub Kicinski 
17687125b5SJakub Kicinski #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \
18687125b5SJakub Kicinski 	(__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX)
19687125b5SJakub Kicinski 
20687125b5SJakub Kicinski struct devlink_dev_stats {
21687125b5SJakub Kicinski 	u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
22687125b5SJakub Kicinski 	u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
23687125b5SJakub Kicinski };
24687125b5SJakub Kicinski 
25687125b5SJakub Kicinski struct devlink {
26687125b5SJakub Kicinski 	u32 index;
27687125b5SJakub Kicinski 	struct xarray ports;
28687125b5SJakub Kicinski 	struct list_head rate_list;
29687125b5SJakub Kicinski 	struct list_head sb_list;
30687125b5SJakub Kicinski 	struct list_head dpipe_table_list;
31687125b5SJakub Kicinski 	struct list_head resource_list;
32687125b5SJakub Kicinski 	struct list_head param_list;
33687125b5SJakub Kicinski 	struct list_head region_list;
34687125b5SJakub Kicinski 	struct list_head reporter_list;
35687125b5SJakub Kicinski 	struct devlink_dpipe_headers *dpipe_headers;
36687125b5SJakub Kicinski 	struct list_head trap_list;
37687125b5SJakub Kicinski 	struct list_head trap_group_list;
38687125b5SJakub Kicinski 	struct list_head trap_policer_list;
39687125b5SJakub Kicinski 	struct list_head linecard_list;
40687125b5SJakub Kicinski 	const struct devlink_ops *ops;
41687125b5SJakub Kicinski 	u64 features;
42687125b5SJakub Kicinski 	struct xarray snapshot_ids;
43687125b5SJakub Kicinski 	struct devlink_dev_stats stats;
44687125b5SJakub Kicinski 	struct device *dev;
45687125b5SJakub Kicinski 	possible_net_t _net;
46687125b5SJakub Kicinski 	/* Serializes access to devlink instance specific objects such as
47687125b5SJakub Kicinski 	 * port, sb, dpipe, resource, params, region, traps and more.
48687125b5SJakub Kicinski 	 */
49687125b5SJakub Kicinski 	struct mutex lock;
50687125b5SJakub Kicinski 	struct lock_class_key lock_key;
51687125b5SJakub Kicinski 	u8 reload_failed:1;
52687125b5SJakub Kicinski 	refcount_t refcount;
5393e71edfSJakub Kicinski 	struct rcu_work rwork;
54687125b5SJakub Kicinski 	struct notifier_block netdevice_nb;
55687125b5SJakub Kicinski 	char priv[] __aligned(NETDEV_ALIGN);
56687125b5SJakub Kicinski };
57687125b5SJakub Kicinski 
58687125b5SJakub Kicinski extern struct xarray devlinks;
59687125b5SJakub Kicinski extern struct genl_family devlink_nl_family;
60687125b5SJakub Kicinski 
61687125b5SJakub Kicinski /* devlink instances are open to the access from the user space after
62687125b5SJakub Kicinski  * devlink_register() call. Such logical barrier allows us to have certain
63687125b5SJakub Kicinski  * expectations related to locking.
64687125b5SJakub Kicinski  *
65687125b5SJakub Kicinski  * Before *_register() - we are in initialization stage and no parallel
66687125b5SJakub Kicinski  * access possible to the devlink instance. All drivers perform that phase
67687125b5SJakub Kicinski  * by implicitly holding device_lock.
68687125b5SJakub Kicinski  *
69687125b5SJakub Kicinski  * After *_register() - users and driver can access devlink instance at
70687125b5SJakub Kicinski  * the same time.
71687125b5SJakub Kicinski  */
72687125b5SJakub Kicinski #define ASSERT_DEVLINK_REGISTERED(d)                                           \
73687125b5SJakub Kicinski 	WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
74687125b5SJakub Kicinski #define ASSERT_DEVLINK_NOT_REGISTERED(d)                                       \
75687125b5SJakub Kicinski 	WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
76687125b5SJakub Kicinski 
77687125b5SJakub Kicinski /* Iterate over devlink pointers which were possible to get reference to.
78687125b5SJakub Kicinski  * devlink_put() needs to be called for each iterated devlink pointer
79687125b5SJakub Kicinski  * in loop body in order to release the reference.
80687125b5SJakub Kicinski  */
81687125b5SJakub Kicinski #define devlinks_xa_for_each_registered_get(net, index, devlink)	\
82d7727819SJakub Kicinski 	for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++)
83687125b5SJakub Kicinski 
84d7727819SJakub Kicinski struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp);
85687125b5SJakub Kicinski 
86ed539ba6SJakub Kicinski static inline bool devl_is_registered(struct devlink *devlink)
87ed539ba6SJakub Kicinski {
88ed539ba6SJakub Kicinski 	/* To prevent races the caller must hold the instance lock
89ed539ba6SJakub Kicinski 	 * or another lock taken during unregistration.
90ed539ba6SJakub Kicinski 	 */
91ed539ba6SJakub Kicinski 	return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
92ed539ba6SJakub Kicinski }
93ed539ba6SJakub Kicinski 
94687125b5SJakub Kicinski /* Netlink */
95623cd13bSJakub Kicinski #define DEVLINK_NL_FLAG_NEED_PORT		BIT(0)
96623cd13bSJakub Kicinski #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT	BIT(1)
97623cd13bSJakub Kicinski #define DEVLINK_NL_FLAG_NEED_RATE		BIT(2)
98623cd13bSJakub Kicinski #define DEVLINK_NL_FLAG_NEED_RATE_NODE		BIT(3)
99623cd13bSJakub Kicinski #define DEVLINK_NL_FLAG_NEED_LINECARD		BIT(4)
100623cd13bSJakub Kicinski 
101623cd13bSJakub Kicinski enum devlink_multicast_groups {
102623cd13bSJakub Kicinski 	DEVLINK_MCGRP_CONFIG,
103623cd13bSJakub Kicinski };
104623cd13bSJakub Kicinski 
1053015f822SJakub Kicinski /* state held across netlink dumps */
1063015f822SJakub Kicinski struct devlink_nl_dump_state {
107731d69a6SJakub Kicinski 	unsigned long instance;
1083015f822SJakub Kicinski 	int idx;
1093015f822SJakub Kicinski 	union {
1103015f822SJakub Kicinski 		/* DEVLINK_CMD_REGION_READ */
1113015f822SJakub Kicinski 		struct {
1123015f822SJakub Kicinski 			u64 start_offset;
1133015f822SJakub Kicinski 		};
1143015f822SJakub Kicinski 		/* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */
1153015f822SJakub Kicinski 		struct {
1163015f822SJakub Kicinski 			u64 dump_ts;
1173015f822SJakub Kicinski 		};
1183015f822SJakub Kicinski 	};
1193015f822SJakub Kicinski };
1203015f822SJakub Kicinski 
12107f3af66SJakub Kicinski struct devlink_gen_cmd {
12207f3af66SJakub Kicinski 	int (*dump_one)(struct sk_buff *msg, struct devlink *devlink,
12307f3af66SJakub Kicinski 			struct netlink_callback *cb);
12407f3af66SJakub Kicinski };
12507f3af66SJakub Kicinski 
126731d69a6SJakub Kicinski /* Iterate over registered devlink instances for devlink dump.
127731d69a6SJakub Kicinski  * devlink_put() needs to be called for each iterated devlink pointer
128731d69a6SJakub Kicinski  * in loop body in order to release the reference.
129731d69a6SJakub Kicinski  * Note: this is NOT a generic iterator, it makes assumptions about the use
130731d69a6SJakub Kicinski  *	 of @state and can only be used once per dumpit implementation.
131731d69a6SJakub Kicinski  */
132731d69a6SJakub Kicinski #define devlink_dump_for_each_instance_get(msg, state, devlink)		\
133731d69a6SJakub Kicinski 	for (; (devlink = devlinks_xa_find_get(sock_net(msg->sk),	\
134d7727819SJakub Kicinski 					       &state->instance));	\
135a8f94707SJakub Kicinski 	     state->instance++, state->idx = 0)
136731d69a6SJakub Kicinski 
137623cd13bSJakub Kicinski extern const struct genl_small_ops devlink_nl_ops[56];
138623cd13bSJakub Kicinski 
139870c7ad4SJakub Kicinski struct devlink *
140870c7ad4SJakub Kicinski devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs);
141623cd13bSJakub Kicinski 
142687125b5SJakub Kicinski void devlink_notify_unregister(struct devlink *devlink);
143687125b5SJakub Kicinski void devlink_notify_register(struct devlink *devlink);
144687125b5SJakub Kicinski 
14507f3af66SJakub Kicinski int devlink_nl_instance_iter_dump(struct sk_buff *msg,
14607f3af66SJakub Kicinski 				  struct netlink_callback *cb);
14707f3af66SJakub Kicinski 
1483015f822SJakub Kicinski static inline struct devlink_nl_dump_state *
1493015f822SJakub Kicinski devlink_dump_state(struct netlink_callback *cb)
1503015f822SJakub Kicinski {
1513015f822SJakub Kicinski 	NL_ASSET_DUMP_CTX_FITS(struct devlink_nl_dump_state);
1523015f822SJakub Kicinski 
1533015f822SJakub Kicinski 	return (struct devlink_nl_dump_state *)cb->ctx;
1543015f822SJakub Kicinski }
1553015f822SJakub Kicinski 
1565ce76d78SJakub Kicinski /* gen cmds */
1575ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_inst;
1585ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_port;
1595ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_sb;
1605ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_sb_pool;
1615ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_sb_port_pool;
1625ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_sb_tc_pool_bind;
1635ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_selftests;
1645ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_param;
1655ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_region;
1665ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_info;
1675ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_trap;
1685ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_trap_group;
1695ce76d78SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_trap_policer;
170*25573968SJiri Pirko extern const struct devlink_gen_cmd devl_gen_linecard;
1715ce76d78SJakub Kicinski 
172687125b5SJakub Kicinski /* Ports */
173687125b5SJakub Kicinski int devlink_port_netdevice_event(struct notifier_block *nb,
174687125b5SJakub Kicinski 				 unsigned long event, void *ptr);
175687125b5SJakub Kicinski 
176623cd13bSJakub Kicinski struct devlink_port *
177623cd13bSJakub Kicinski devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info);
178623cd13bSJakub Kicinski 
179687125b5SJakub Kicinski /* Reload */
180687125b5SJakub Kicinski bool devlink_reload_actions_valid(const struct devlink_ops *ops);
181687125b5SJakub Kicinski int devlink_reload(struct devlink *devlink, struct net *dest_net,
182687125b5SJakub Kicinski 		   enum devlink_reload_action action,
183687125b5SJakub Kicinski 		   enum devlink_reload_limit limit,
184687125b5SJakub Kicinski 		   u32 *actions_performed, struct netlink_ext_ack *extack);
185687125b5SJakub Kicinski 
186687125b5SJakub Kicinski static inline bool devlink_reload_supported(const struct devlink_ops *ops)
187687125b5SJakub Kicinski {
188687125b5SJakub Kicinski 	return ops->reload_down && ops->reload_up;
189687125b5SJakub Kicinski }
190623cd13bSJakub Kicinski 
191623cd13bSJakub Kicinski /* Line cards */
192623cd13bSJakub Kicinski struct devlink_linecard;
193623cd13bSJakub Kicinski 
194623cd13bSJakub Kicinski struct devlink_linecard *
195623cd13bSJakub Kicinski devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info);
196623cd13bSJakub Kicinski 
197623cd13bSJakub Kicinski /* Rates */
19807f3af66SJakub Kicinski extern const struct devlink_gen_cmd devl_gen_rate_get;
19907f3af66SJakub Kicinski 
200623cd13bSJakub Kicinski struct devlink_rate *
201623cd13bSJakub Kicinski devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info);
202623cd13bSJakub Kicinski struct devlink_rate *
203623cd13bSJakub Kicinski devlink_rate_node_get_from_info(struct devlink *devlink,
204623cd13bSJakub Kicinski 				struct genl_info *info);
205