xref: /linux/net/devlink/devl_internal.h (revision 0d0950a968009fd6e5c2a3763b2c6e81b3805b05)
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/mutex.h>
7 #include <linux/netdevice.h>
8 #include <linux/notifier.h>
9 #include <linux/types.h>
10 #include <linux/workqueue.h>
11 #include <linux/xarray.h>
12 #include <net/devlink.h>
13 #include <net/net_namespace.h>
14 
15 #define DEVLINK_REGISTERED XA_MARK_1
16 
17 #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \
18 	(__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX)
19 
20 struct devlink_dev_stats {
21 	u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
22 	u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
23 };
24 
25 struct devlink {
26 	u32 index;
27 	struct xarray ports;
28 	struct list_head rate_list;
29 	struct list_head sb_list;
30 	struct list_head dpipe_table_list;
31 	struct list_head resource_list;
32 	struct list_head param_list;
33 	struct list_head region_list;
34 	struct list_head reporter_list;
35 	struct mutex reporters_lock; /* protects reporter_list */
36 	struct devlink_dpipe_headers *dpipe_headers;
37 	struct list_head trap_list;
38 	struct list_head trap_group_list;
39 	struct list_head trap_policer_list;
40 	struct list_head linecard_list;
41 	struct mutex linecards_lock; /* protects linecard_list */
42 	const struct devlink_ops *ops;
43 	u64 features;
44 	struct xarray snapshot_ids;
45 	struct devlink_dev_stats stats;
46 	struct device *dev;
47 	possible_net_t _net;
48 	/* Serializes access to devlink instance specific objects such as
49 	 * port, sb, dpipe, resource, params, region, traps and more.
50 	 */
51 	struct mutex lock;
52 	struct lock_class_key lock_key;
53 	u8 reload_failed:1;
54 	refcount_t refcount;
55 	struct rcu_work rwork;
56 	struct notifier_block netdevice_nb;
57 	char priv[] __aligned(NETDEV_ALIGN);
58 };
59 
60 extern struct xarray devlinks;
61 extern struct genl_family devlink_nl_family;
62 
63 /* devlink instances are open to the access from the user space after
64  * devlink_register() call. Such logical barrier allows us to have certain
65  * expectations related to locking.
66  *
67  * Before *_register() - we are in initialization stage and no parallel
68  * access possible to the devlink instance. All drivers perform that phase
69  * by implicitly holding device_lock.
70  *
71  * After *_register() - users and driver can access devlink instance at
72  * the same time.
73  */
74 #define ASSERT_DEVLINK_REGISTERED(d)                                           \
75 	WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
76 #define ASSERT_DEVLINK_NOT_REGISTERED(d)                                       \
77 	WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
78 
79 /* Iterate over devlink pointers which were possible to get reference to.
80  * devlink_put() needs to be called for each iterated devlink pointer
81  * in loop body in order to release the reference.
82  */
83 #define devlinks_xa_for_each_registered_get(net, index, devlink)	\
84 	for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++)
85 
86 struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp);
87 
88 static inline bool devl_is_registered(struct devlink *devlink)
89 {
90 	/* To prevent races the caller must hold the instance lock
91 	 * or another lock taken during unregistration.
92 	 */
93 	return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
94 }
95 
96 /* Netlink */
97 #define DEVLINK_NL_FLAG_NEED_PORT		BIT(0)
98 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT	BIT(1)
99 #define DEVLINK_NL_FLAG_NEED_RATE		BIT(2)
100 #define DEVLINK_NL_FLAG_NEED_RATE_NODE		BIT(3)
101 #define DEVLINK_NL_FLAG_NEED_LINECARD		BIT(4)
102 
103 enum devlink_multicast_groups {
104 	DEVLINK_MCGRP_CONFIG,
105 };
106 
107 /* state held across netlink dumps */
108 struct devlink_nl_dump_state {
109 	unsigned long instance;
110 	int idx;
111 	union {
112 		/* DEVLINK_CMD_REGION_READ */
113 		struct {
114 			u64 start_offset;
115 		};
116 		/* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */
117 		struct {
118 			u64 dump_ts;
119 		};
120 	};
121 };
122 
123 struct devlink_gen_cmd {
124 	int (*dump_one)(struct sk_buff *msg, struct devlink *devlink,
125 			struct netlink_callback *cb);
126 };
127 
128 /* Iterate over registered devlink instances for devlink dump.
129  * devlink_put() needs to be called for each iterated devlink pointer
130  * in loop body in order to release the reference.
131  * Note: this is NOT a generic iterator, it makes assumptions about the use
132  *	 of @state and can only be used once per dumpit implementation.
133  */
134 #define devlink_dump_for_each_instance_get(msg, state, devlink)		\
135 	for (; (devlink = devlinks_xa_find_get(sock_net(msg->sk),	\
136 					       &state->instance));	\
137 	     state->instance++, state->idx = 0)
138 
139 extern const struct genl_small_ops devlink_nl_ops[56];
140 
141 struct devlink *
142 devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs);
143 
144 void devlink_notify_unregister(struct devlink *devlink);
145 void devlink_notify_register(struct devlink *devlink);
146 
147 int devlink_nl_instance_iter_dump(struct sk_buff *msg,
148 				  struct netlink_callback *cb);
149 
150 static inline struct devlink_nl_dump_state *
151 devlink_dump_state(struct netlink_callback *cb)
152 {
153 	NL_ASSET_DUMP_CTX_FITS(struct devlink_nl_dump_state);
154 
155 	return (struct devlink_nl_dump_state *)cb->ctx;
156 }
157 
158 /* gen cmds */
159 extern const struct devlink_gen_cmd devl_gen_inst;
160 extern const struct devlink_gen_cmd devl_gen_port;
161 extern const struct devlink_gen_cmd devl_gen_sb;
162 extern const struct devlink_gen_cmd devl_gen_sb_pool;
163 extern const struct devlink_gen_cmd devl_gen_sb_port_pool;
164 extern const struct devlink_gen_cmd devl_gen_sb_tc_pool_bind;
165 extern const struct devlink_gen_cmd devl_gen_selftests;
166 extern const struct devlink_gen_cmd devl_gen_param;
167 extern const struct devlink_gen_cmd devl_gen_region;
168 extern const struct devlink_gen_cmd devl_gen_info;
169 extern const struct devlink_gen_cmd devl_gen_trap;
170 extern const struct devlink_gen_cmd devl_gen_trap_group;
171 extern const struct devlink_gen_cmd devl_gen_trap_policer;
172 
173 /* Ports */
174 int devlink_port_netdevice_event(struct notifier_block *nb,
175 				 unsigned long event, void *ptr);
176 
177 struct devlink_port *
178 devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info);
179 
180 /* Reload */
181 bool devlink_reload_actions_valid(const struct devlink_ops *ops);
182 int devlink_reload(struct devlink *devlink, struct net *dest_net,
183 		   enum devlink_reload_action action,
184 		   enum devlink_reload_limit limit,
185 		   u32 *actions_performed, struct netlink_ext_ack *extack);
186 
187 static inline bool devlink_reload_supported(const struct devlink_ops *ops)
188 {
189 	return ops->reload_down && ops->reload_up;
190 }
191 
192 /* Line cards */
193 struct devlink_linecard;
194 
195 struct devlink_linecard *
196 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info);
197 void devlink_linecard_put(struct devlink_linecard *linecard);
198 
199 /* Rates */
200 extern const struct devlink_gen_cmd devl_gen_rate_get;
201 
202 struct devlink_rate *
203 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info);
204 struct devlink_rate *
205 devlink_rate_node_get_from_info(struct devlink *devlink,
206 				struct genl_info *info);
207