| 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/device.h> |
| 7 | #include <linux/etherdevice.h> |
| 8 | #include <linux/mutex.h> |
| 9 | #include <linux/netdevice.h> |
| 10 | #include <linux/notifier.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/workqueue.h> |
| 13 | #include <linux/xarray.h> |
| 14 | #include <net/devlink.h> |
| 15 | #include <net/net_namespace.h> |
| 16 | #include <net/rtnetlink.h> |
| 17 | #include <rdma/ib_verbs.h> |
| 18 | |
| 19 | #include "netlink_gen.h" |
| 20 | |
| 21 | struct devlink_rel; |
| 22 | |
| 23 | #define DEVLINK_REGISTERED XA_MARK_1 |
| 24 | |
| 25 | #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \ |
| 26 | (__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX) |
| 27 | |
| 28 | struct devlink_dev_stats { |
| 29 | u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE]; |
| 30 | u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE]; |
| 31 | }; |
| 32 | |
| 33 | struct devlink { |
| 34 | u32 index; |
| 35 | struct xarray ports; |
| 36 | struct list_head rate_list; |
| 37 | struct list_head sb_list; |
| 38 | struct list_head dpipe_table_list; |
| 39 | struct list_head resource_list; |
| 40 | struct xarray params; |
| 41 | struct list_head region_list; |
| 42 | struct list_head reporter_list; |
| 43 | struct devlink_dpipe_headers *; |
| 44 | struct list_head trap_list; |
| 45 | struct list_head trap_group_list; |
| 46 | struct list_head trap_policer_list; |
| 47 | struct list_head linecard_list; |
| 48 | const struct devlink_ops *ops; |
| 49 | struct xarray snapshot_ids; |
| 50 | struct devlink_dev_stats stats; |
| 51 | struct device *dev; |
| 52 | possible_net_t _net; |
| 53 | /* Serializes access to devlink instance specific objects such as |
| 54 | * port, sb, dpipe, resource, params, region, traps and more. |
| 55 | */ |
| 56 | struct mutex lock; |
| 57 | struct lock_class_key lock_key; |
| 58 | u8 reload_failed:1; |
| 59 | refcount_t refcount; |
| 60 | struct rcu_work rwork; |
| 61 | struct devlink_rel *rel; |
| 62 | struct xarray nested_rels; |
| 63 | char priv[] __aligned(NETDEV_ALIGN); |
| 64 | }; |
| 65 | |
| 66 | extern struct xarray devlinks; |
| 67 | extern struct genl_family devlink_nl_family; |
| 68 | |
| 69 | /* devlink instances are open to the access from the user space after |
| 70 | * devlink_register() call. Such logical barrier allows us to have certain |
| 71 | * expectations related to locking. |
| 72 | * |
| 73 | * Before *_register() - we are in initialization stage and no parallel |
| 74 | * access possible to the devlink instance. All drivers perform that phase |
| 75 | * by implicitly holding device_lock. |
| 76 | * |
| 77 | * After *_register() - users and driver can access devlink instance at |
| 78 | * the same time. |
| 79 | */ |
| 80 | #define ASSERT_DEVLINK_REGISTERED(d) \ |
| 81 | WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED)) |
| 82 | #define ASSERT_DEVLINK_NOT_REGISTERED(d) \ |
| 83 | WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED)) |
| 84 | |
| 85 | /* Iterate over devlink pointers which were possible to get reference to. |
| 86 | * devlink_put() needs to be called for each iterated devlink pointer |
| 87 | * in loop body in order to release the reference. |
| 88 | */ |
| 89 | #define devlinks_xa_for_each_registered_get(net, index, devlink) \ |
| 90 | for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++) |
| 91 | |
| 92 | struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp); |
| 93 | |
| 94 | static inline bool __devl_is_registered(struct devlink *devlink) |
| 95 | { |
| 96 | return xa_get_mark(&devlinks, index: devlink->index, DEVLINK_REGISTERED); |
| 97 | } |
| 98 | |
| 99 | static inline bool devl_is_registered(struct devlink *devlink) |
| 100 | { |
| 101 | devl_assert_locked(devlink); |
| 102 | return __devl_is_registered(devlink); |
| 103 | } |
| 104 | |
| 105 | static inline void devl_dev_lock(struct devlink *devlink, bool dev_lock) |
| 106 | { |
| 107 | if (dev_lock) |
| 108 | device_lock(dev: devlink->dev); |
| 109 | devl_lock(devlink); |
| 110 | } |
| 111 | |
| 112 | static inline void devl_dev_unlock(struct devlink *devlink, bool dev_lock) |
| 113 | { |
| 114 | devl_unlock(devlink); |
| 115 | if (dev_lock) |
| 116 | device_unlock(dev: devlink->dev); |
| 117 | } |
| 118 | |
| 119 | typedef void devlink_rel_notify_cb_t(struct devlink *devlink, u32 obj_index); |
| 120 | typedef void devlink_rel_cleanup_cb_t(struct devlink *devlink, u32 obj_index, |
| 121 | u32 rel_index); |
| 122 | |
| 123 | void devlink_rel_nested_in_clear(u32 rel_index); |
| 124 | int devlink_rel_nested_in_add(u32 *rel_index, u32 devlink_index, |
| 125 | u32 obj_index, devlink_rel_notify_cb_t *notify_cb, |
| 126 | devlink_rel_cleanup_cb_t *cleanup_cb, |
| 127 | struct devlink *devlink); |
| 128 | void devlink_rel_nested_in_notify(struct devlink *devlink); |
| 129 | int devlink_rel_devlink_handle_put(struct sk_buff *msg, struct devlink *devlink, |
| 130 | u32 rel_index, int attrtype, |
| 131 | bool *msg_updated); |
| 132 | |
| 133 | /* Netlink */ |
| 134 | enum devlink_multicast_groups { |
| 135 | DEVLINK_MCGRP_CONFIG, |
| 136 | }; |
| 137 | |
| 138 | /* state held across netlink dumps */ |
| 139 | struct devlink_nl_dump_state { |
| 140 | unsigned long instance; |
| 141 | int idx; |
| 142 | union { |
| 143 | /* DEVLINK_CMD_REGION_READ */ |
| 144 | struct { |
| 145 | u64 start_offset; |
| 146 | }; |
| 147 | /* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */ |
| 148 | struct { |
| 149 | u64 dump_ts; |
| 150 | }; |
| 151 | }; |
| 152 | }; |
| 153 | |
| 154 | typedef int devlink_nl_dump_one_func_t(struct sk_buff *msg, |
| 155 | struct devlink *devlink, |
| 156 | struct netlink_callback *cb, |
| 157 | int flags); |
| 158 | |
| 159 | struct devlink * |
| 160 | devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs, |
| 161 | bool dev_lock); |
| 162 | |
| 163 | int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb, |
| 164 | devlink_nl_dump_one_func_t *dump_one); |
| 165 | |
| 166 | static inline struct devlink_nl_dump_state * |
| 167 | devlink_dump_state(struct netlink_callback *cb) |
| 168 | { |
| 169 | NL_ASSERT_CTX_FITS(struct devlink_nl_dump_state); |
| 170 | |
| 171 | return (struct devlink_nl_dump_state *)cb->ctx; |
| 172 | } |
| 173 | |
| 174 | static inline int |
| 175 | devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) |
| 176 | { |
| 177 | if (nla_put_string(skb: msg, attrtype: DEVLINK_ATTR_BUS_NAME, str: devlink->dev->bus->name)) |
| 178 | return -EMSGSIZE; |
| 179 | if (nla_put_string(skb: msg, attrtype: DEVLINK_ATTR_DEV_NAME, str: dev_name(dev: devlink->dev))) |
| 180 | return -EMSGSIZE; |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static inline int devlink_nl_put_u64(struct sk_buff *msg, int attrtype, u64 val) |
| 185 | { |
| 186 | return nla_put_u64_64bit(skb: msg, attrtype, value: val, padattr: DEVLINK_ATTR_PAD); |
| 187 | } |
| 188 | |
| 189 | int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net, |
| 190 | struct devlink *devlink, int attrtype); |
| 191 | int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info); |
| 192 | |
| 193 | static inline bool devlink_nl_notify_need(struct devlink *devlink) |
| 194 | { |
| 195 | return genl_has_listeners(family: &devlink_nl_family, net: devlink_net(devlink), |
| 196 | group: DEVLINK_MCGRP_CONFIG); |
| 197 | } |
| 198 | |
| 199 | struct devlink_obj_desc { |
| 200 | struct rcu_head rcu; |
| 201 | const char *bus_name; |
| 202 | const char *dev_name; |
| 203 | unsigned int port_index; |
| 204 | bool port_index_valid; |
| 205 | long data[]; |
| 206 | }; |
| 207 | |
| 208 | static inline void devlink_nl_obj_desc_init(struct devlink_obj_desc *desc, |
| 209 | struct devlink *devlink) |
| 210 | { |
| 211 | memset(desc, 0, sizeof(*desc)); |
| 212 | desc->bus_name = devlink->dev->bus->name; |
| 213 | desc->dev_name = dev_name(dev: devlink->dev); |
| 214 | } |
| 215 | |
| 216 | static inline void devlink_nl_obj_desc_port_set(struct devlink_obj_desc *desc, |
| 217 | struct devlink_port *devlink_port) |
| 218 | { |
| 219 | desc->port_index = devlink_port->index; |
| 220 | desc->port_index_valid = true; |
| 221 | } |
| 222 | |
| 223 | int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data); |
| 224 | |
| 225 | static inline void devlink_nl_notify_send_desc(struct devlink *devlink, |
| 226 | struct sk_buff *msg, |
| 227 | struct devlink_obj_desc *desc) |
| 228 | { |
| 229 | genlmsg_multicast_netns_filtered(family: &devlink_nl_family, |
| 230 | net: devlink_net(devlink), |
| 231 | skb: msg, portid: 0, group: DEVLINK_MCGRP_CONFIG, |
| 232 | GFP_KERNEL, |
| 233 | filter: devlink_nl_notify_filter, filter_data: desc); |
| 234 | } |
| 235 | |
| 236 | static inline void devlink_nl_notify_send(struct devlink *devlink, |
| 237 | struct sk_buff *msg) |
| 238 | { |
| 239 | struct devlink_obj_desc desc; |
| 240 | |
| 241 | devlink_nl_obj_desc_init(desc: &desc, devlink); |
| 242 | devlink_nl_notify_send_desc(devlink, msg, desc: &desc); |
| 243 | } |
| 244 | |
| 245 | /* Notify */ |
| 246 | void devlink_notify_register(struct devlink *devlink); |
| 247 | void devlink_notify_unregister(struct devlink *devlink); |
| 248 | void devlink_ports_notify_register(struct devlink *devlink); |
| 249 | void devlink_ports_notify_unregister(struct devlink *devlink); |
| 250 | void devlink_params_notify_register(struct devlink *devlink); |
| 251 | void devlink_params_notify_unregister(struct devlink *devlink); |
| 252 | void devlink_regions_notify_register(struct devlink *devlink); |
| 253 | void devlink_regions_notify_unregister(struct devlink *devlink); |
| 254 | void devlink_trap_policers_notify_register(struct devlink *devlink); |
| 255 | void devlink_trap_policers_notify_unregister(struct devlink *devlink); |
| 256 | void devlink_trap_groups_notify_register(struct devlink *devlink); |
| 257 | void devlink_trap_groups_notify_unregister(struct devlink *devlink); |
| 258 | void devlink_traps_notify_register(struct devlink *devlink); |
| 259 | void devlink_traps_notify_unregister(struct devlink *devlink); |
| 260 | void devlink_rates_notify_register(struct devlink *devlink); |
| 261 | void devlink_rates_notify_unregister(struct devlink *devlink); |
| 262 | void devlink_linecards_notify_register(struct devlink *devlink); |
| 263 | void devlink_linecards_notify_unregister(struct devlink *devlink); |
| 264 | |
| 265 | /* Ports */ |
| 266 | #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port) \ |
| 267 | WARN_ON_ONCE(!(devlink_port)->initialized) |
| 268 | |
| 269 | struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, |
| 270 | unsigned int port_index); |
| 271 | int devlink_port_netdevice_event(struct notifier_block *nb, |
| 272 | unsigned long event, void *ptr); |
| 273 | struct devlink_port * |
| 274 | devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info); |
| 275 | struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, |
| 276 | struct nlattr **attrs); |
| 277 | |
| 278 | /* Reload */ |
| 279 | bool devlink_reload_actions_valid(const struct devlink_ops *ops); |
| 280 | int devlink_reload(struct devlink *devlink, struct net *dest_net, |
| 281 | enum devlink_reload_action action, |
| 282 | enum devlink_reload_limit limit, |
| 283 | u32 *actions_performed, struct netlink_ext_ack *extack); |
| 284 | |
| 285 | static inline bool devlink_reload_supported(const struct devlink_ops *ops) |
| 286 | { |
| 287 | return ops->reload_down && ops->reload_up; |
| 288 | } |
| 289 | |
| 290 | /* Params */ |
| 291 | void devlink_params_driverinit_load_new(struct devlink *devlink); |
| 292 | |
| 293 | /* Resources */ |
| 294 | struct devlink_resource; |
| 295 | int devlink_resources_validate(struct devlink *devlink, |
| 296 | struct devlink_resource *resource, |
| 297 | struct genl_info *info); |
| 298 | |
| 299 | /* Rates */ |
| 300 | int devlink_rate_nodes_check(struct devlink *devlink, u16 mode, |
| 301 | struct netlink_ext_ack *extack); |
| 302 | |
| 303 | /* Linecards */ |
| 304 | unsigned int devlink_linecard_index(struct devlink_linecard *linecard); |
| 305 | |