xref: /openbmc/linux/net/devlink/devl_internal.h (revision 1992f2af)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Copyright (c) 2016 Mellanox Technologies. All rights reserved.
3  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
4  */
5 
6 #include <linux/etherdevice.h>
7 #include <linux/mutex.h>
8 #include <linux/netdevice.h>
9 #include <linux/notifier.h>
10 #include <linux/types.h>
11 #include <linux/workqueue.h>
12 #include <linux/xarray.h>
13 #include <net/devlink.h>
14 #include <net/net_namespace.h>
15 #include <net/rtnetlink.h>
16 #include <rdma/ib_verbs.h>
17 
18 #include "netlink_gen.h"
19 
20 #define DEVLINK_REGISTERED XA_MARK_1
21 
22 #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \
23 	(__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX)
24 
25 struct devlink_dev_stats {
26 	u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
27 	u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
28 };
29 
30 struct devlink {
31 	u32 index;
32 	struct xarray ports;
33 	struct list_head rate_list;
34 	struct list_head sb_list;
35 	struct list_head dpipe_table_list;
36 	struct list_head resource_list;
37 	struct xarray params;
38 	struct list_head region_list;
39 	struct list_head reporter_list;
40 	struct devlink_dpipe_headers *dpipe_headers;
41 	struct list_head trap_list;
42 	struct list_head trap_group_list;
43 	struct list_head trap_policer_list;
44 	struct list_head linecard_list;
45 	const struct devlink_ops *ops;
46 	struct xarray snapshot_ids;
47 	struct devlink_dev_stats stats;
48 	struct device *dev;
49 	possible_net_t _net;
50 	/* Serializes access to devlink instance specific objects such as
51 	 * port, sb, dpipe, resource, params, region, traps and more.
52 	 */
53 	struct mutex lock;
54 	struct lock_class_key lock_key;
55 	u8 reload_failed:1;
56 	refcount_t refcount;
57 	struct rcu_work rwork;
58 	char priv[] __aligned(NETDEV_ALIGN);
59 };
60 
61 extern struct xarray devlinks;
62 extern struct genl_family devlink_nl_family;
63 
64 /* devlink instances are open to the access from the user space after
65  * devlink_register() call. Such logical barrier allows us to have certain
66  * expectations related to locking.
67  *
68  * Before *_register() - we are in initialization stage and no parallel
69  * access possible to the devlink instance. All drivers perform that phase
70  * by implicitly holding device_lock.
71  *
72  * After *_register() - users and driver can access devlink instance at
73  * the same time.
74  */
75 #define ASSERT_DEVLINK_REGISTERED(d)                                           \
76 	WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
77 #define ASSERT_DEVLINK_NOT_REGISTERED(d)                                       \
78 	WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
79 
80 /* Iterate over devlink pointers which were possible to get reference to.
81  * devlink_put() needs to be called for each iterated devlink pointer
82  * in loop body in order to release the reference.
83  */
84 #define devlinks_xa_for_each_registered_get(net, index, devlink)	\
85 	for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++)
86 
87 struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp);
88 
89 static inline bool devl_is_registered(struct devlink *devlink)
90 {
91 	devl_assert_locked(devlink);
92 	return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
93 }
94 
95 /* Netlink */
96 #define DEVLINK_NL_FLAG_NEED_PORT		BIT(0)
97 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT	BIT(1)
98 
99 enum devlink_multicast_groups {
100 	DEVLINK_MCGRP_CONFIG,
101 };
102 
103 /* state held across netlink dumps */
104 struct devlink_nl_dump_state {
105 	unsigned long instance;
106 	int idx;
107 	union {
108 		/* DEVLINK_CMD_REGION_READ */
109 		struct {
110 			u64 start_offset;
111 		};
112 		/* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */
113 		struct {
114 			u64 dump_ts;
115 		};
116 	};
117 };
118 
119 typedef int devlink_nl_dump_one_func_t(struct sk_buff *msg,
120 				       struct devlink *devlink,
121 				       struct netlink_callback *cb,
122 				       int flags);
123 
124 struct devlink *
125 devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs);
126 
127 int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb,
128 		      devlink_nl_dump_one_func_t *dump_one);
129 
130 static inline struct devlink_nl_dump_state *
131 devlink_dump_state(struct netlink_callback *cb)
132 {
133 	NL_ASSERT_DUMP_CTX_FITS(struct devlink_nl_dump_state);
134 
135 	return (struct devlink_nl_dump_state *)cb->ctx;
136 }
137 
138 static inline int
139 devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
140 {
141 	if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
142 		return -EMSGSIZE;
143 	if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
144 		return -EMSGSIZE;
145 	return 0;
146 }
147 
148 int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info);
149 
150 /* Notify */
151 void devlink_notify_register(struct devlink *devlink);
152 void devlink_notify_unregister(struct devlink *devlink);
153 void devlink_ports_notify_register(struct devlink *devlink);
154 void devlink_ports_notify_unregister(struct devlink *devlink);
155 void devlink_params_notify_register(struct devlink *devlink);
156 void devlink_params_notify_unregister(struct devlink *devlink);
157 void devlink_regions_notify_register(struct devlink *devlink);
158 void devlink_regions_notify_unregister(struct devlink *devlink);
159 void devlink_trap_policers_notify_register(struct devlink *devlink);
160 void devlink_trap_policers_notify_unregister(struct devlink *devlink);
161 void devlink_trap_groups_notify_register(struct devlink *devlink);
162 void devlink_trap_groups_notify_unregister(struct devlink *devlink);
163 void devlink_traps_notify_register(struct devlink *devlink);
164 void devlink_traps_notify_unregister(struct devlink *devlink);
165 void devlink_rates_notify_register(struct devlink *devlink);
166 void devlink_rates_notify_unregister(struct devlink *devlink);
167 void devlink_linecards_notify_register(struct devlink *devlink);
168 void devlink_linecards_notify_unregister(struct devlink *devlink);
169 
170 /* Ports */
171 #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port)				\
172 	WARN_ON_ONCE(!(devlink_port)->initialized)
173 
174 struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
175 					       unsigned int port_index);
176 int devlink_port_netdevice_event(struct notifier_block *nb,
177 				 unsigned long event, void *ptr);
178 struct devlink_port *
179 devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info);
180 struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
181 						 struct nlattr **attrs);
182 
183 /* Reload */
184 bool devlink_reload_actions_valid(const struct devlink_ops *ops);
185 int devlink_reload(struct devlink *devlink, struct net *dest_net,
186 		   enum devlink_reload_action action,
187 		   enum devlink_reload_limit limit,
188 		   u32 *actions_performed, struct netlink_ext_ack *extack);
189 
190 static inline bool devlink_reload_supported(const struct devlink_ops *ops)
191 {
192 	return ops->reload_down && ops->reload_up;
193 }
194 
195 /* Params */
196 void devlink_params_driverinit_load_new(struct devlink *devlink);
197 
198 /* Resources */
199 struct devlink_resource;
200 int devlink_resources_validate(struct devlink *devlink,
201 			       struct devlink_resource *resource,
202 			       struct genl_info *info);
203 
204 /* Rates */
205 int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
206 			     struct netlink_ext_ack *extack);
207 
208 /* Linecards */
209 struct devlink_linecard {
210 	struct list_head list;
211 	struct devlink *devlink;
212 	unsigned int index;
213 	const struct devlink_linecard_ops *ops;
214 	void *priv;
215 	enum devlink_linecard_state state;
216 	struct mutex state_lock; /* Protects state */
217 	const char *type;
218 	struct devlink_linecard_type *types;
219 	unsigned int types_count;
220 	struct devlink *nested_devlink;
221 };
222 
223 /* Devlink nl cmds */
224 int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info);
225 int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info);
226 int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info);
227 int devlink_nl_cmd_flash_update(struct sk_buff *skb, struct genl_info *info);
228 int devlink_nl_cmd_selftests_run(struct sk_buff *skb, struct genl_info *info);
229 int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, struct genl_info *info);
230 int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, struct genl_info *info);
231 int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
232 				     struct genl_info *info);
233 int devlink_nl_cmd_port_new_doit(struct sk_buff *skb, struct genl_info *info);
234 int devlink_nl_cmd_port_del_doit(struct sk_buff *skb, struct genl_info *info);
235 int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb, struct genl_info *info);
236 int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
237 					 struct genl_info *info);
238 int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
239 					    struct genl_info *info);
240 int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
241 					struct genl_info *info);
242 int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
243 					 struct genl_info *info);
244 int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb, struct genl_info *info);
245 int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
246 				     struct genl_info *info);
247 int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
248 				     struct genl_info *info);
249 int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
250 					    struct genl_info *info);
251 int devlink_nl_cmd_resource_set(struct sk_buff *skb, struct genl_info *info);
252 int devlink_nl_cmd_resource_dump(struct sk_buff *skb, struct genl_info *info);
253 int devlink_nl_cmd_param_set_doit(struct sk_buff *skb, struct genl_info *info);
254 int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
255 					 struct netlink_callback *cb);
256 int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
257 				       struct genl_info *info);
258 int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
259 				       struct genl_info *info);
260 int devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info);
261 int devlink_nl_cmd_region_del(struct sk_buff *skb, struct genl_info *info);
262 int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
263 				      struct netlink_callback *cb);
264 int devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
265 					    struct genl_info *info);
266 int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
267 						struct genl_info *info);
268 int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
269 						 struct genl_info *info);
270 int devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
271 						   struct netlink_callback *cb);
272 int devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
273 						   struct genl_info *info);
274 int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
275 					     struct genl_info *info);
276 int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb, struct genl_info *info);
277 int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
278 				       struct genl_info *info);
279 int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
280 					 struct genl_info *info);
281 int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb, struct genl_info *info);
282 int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb, struct genl_info *info);
283 int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb, struct genl_info *info);
284 int devlink_nl_cmd_linecard_set_doit(struct sk_buff *skb,
285 				     struct genl_info *info);
286