Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2 | /* |
| 3 | * net/core/devlink.c - Network physical/parent device Netlink interface |
| 4 | * |
| 5 | * Heavily inspired by net/wireless/ |
| 6 | * Copyright (c) 2016 Mellanox Technologies. All rights reserved. |
| 7 | * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/gfp.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/netdevice.h> |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 19 | #include <linux/refcount.h> |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 20 | #include <linux/workqueue.h> |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 21 | #include <linux/u64_stats_sync.h> |
| 22 | #include <linux/timekeeping.h> |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 23 | #include <rdma/ib_verbs.h> |
| 24 | #include <net/netlink.h> |
| 25 | #include <net/genetlink.h> |
| 26 | #include <net/rtnetlink.h> |
| 27 | #include <net/net_namespace.h> |
| 28 | #include <net/sock.h> |
| 29 | #include <net/devlink.h> |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 30 | #include <net/drop_monitor.h> |
Jiri Pirko | e5224f0 | 2016-07-12 18:05:03 +0200 | [diff] [blame] | 31 | #define CREATE_TRACE_POINTS |
| 32 | #include <trace/events/devlink.h> |
| 33 | |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 34 | static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = { |
| 35 | { |
David Ahern | 12bdc5e | 2017-08-30 17:07:30 -0700 | [diff] [blame] | 36 | .name = "destination mac", |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 37 | .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC, |
| 38 | .bitwidth = 48, |
| 39 | }, |
| 40 | }; |
| 41 | |
| 42 | struct devlink_dpipe_header devlink_dpipe_header_ethernet = { |
| 43 | .name = "ethernet", |
| 44 | .id = DEVLINK_DPIPE_HEADER_ETHERNET, |
| 45 | .fields = devlink_dpipe_fields_ethernet, |
| 46 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet), |
| 47 | .global = true, |
| 48 | }; |
| 49 | EXPORT_SYMBOL(devlink_dpipe_header_ethernet); |
| 50 | |
Arkadi Sharshevsky | 3fb886e | 2017-08-24 08:40:00 +0200 | [diff] [blame] | 51 | static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = { |
| 52 | { |
| 53 | .name = "destination ip", |
| 54 | .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP, |
| 55 | .bitwidth = 32, |
| 56 | }, |
| 57 | }; |
| 58 | |
| 59 | struct devlink_dpipe_header devlink_dpipe_header_ipv4 = { |
| 60 | .name = "ipv4", |
| 61 | .id = DEVLINK_DPIPE_HEADER_IPV4, |
| 62 | .fields = devlink_dpipe_fields_ipv4, |
| 63 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4), |
| 64 | .global = true, |
| 65 | }; |
| 66 | EXPORT_SYMBOL(devlink_dpipe_header_ipv4); |
| 67 | |
Arkadi Sharshevsky | 1797f5b | 2017-08-31 17:59:12 +0200 | [diff] [blame] | 68 | static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = { |
| 69 | { |
| 70 | .name = "destination ip", |
| 71 | .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP, |
| 72 | .bitwidth = 128, |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | struct devlink_dpipe_header devlink_dpipe_header_ipv6 = { |
| 77 | .name = "ipv6", |
| 78 | .id = DEVLINK_DPIPE_HEADER_IPV6, |
| 79 | .fields = devlink_dpipe_fields_ipv6, |
| 80 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6), |
| 81 | .global = true, |
| 82 | }; |
| 83 | EXPORT_SYMBOL(devlink_dpipe_header_ipv6); |
| 84 | |
Jiri Pirko | e5224f0 | 2016-07-12 18:05:03 +0200 | [diff] [blame] | 85 | EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg); |
Nir Dotan | 57186a5 | 2019-02-04 18:47:45 +0000 | [diff] [blame] | 86 | EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 87 | |
Parav Pandit | a1e8ae9 | 2020-06-19 03:32:49 +0000 | [diff] [blame] | 88 | static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = { |
| 89 | [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY }, |
| 90 | }; |
| 91 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 92 | static LIST_HEAD(devlink_list); |
| 93 | |
| 94 | /* devlink_mutex |
| 95 | * |
| 96 | * An overall lock guarding every operation coming from userspace. |
| 97 | * It also guards devlink devices list and it is taken when |
| 98 | * driver registers/unregisters it. |
| 99 | */ |
| 100 | static DEFINE_MUTEX(devlink_mutex); |
| 101 | |
Jiri Pirko | 471f894 | 2019-10-03 11:49:31 +0200 | [diff] [blame] | 102 | struct net *devlink_net(const struct devlink *devlink) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 103 | { |
| 104 | return read_pnet(&devlink->_net); |
| 105 | } |
Jiri Pirko | 471f894 | 2019-10-03 11:49:31 +0200 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(devlink_net); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 107 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 108 | static void __devlink_net_set(struct devlink *devlink, struct net *net) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 109 | { |
| 110 | write_pnet(&devlink->_net, net); |
| 111 | } |
| 112 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 113 | void devlink_net_set(struct devlink *devlink, struct net *net) |
| 114 | { |
| 115 | if (WARN_ON(devlink->registered)) |
| 116 | return; |
| 117 | __devlink_net_set(devlink, net); |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(devlink_net_set); |
| 120 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 121 | static struct devlink *devlink_get_from_attrs(struct net *net, |
| 122 | struct nlattr **attrs) |
| 123 | { |
| 124 | struct devlink *devlink; |
| 125 | char *busname; |
| 126 | char *devname; |
| 127 | |
| 128 | if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME]) |
| 129 | return ERR_PTR(-EINVAL); |
| 130 | |
| 131 | busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]); |
| 132 | devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]); |
| 133 | |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 134 | lockdep_assert_held(&devlink_mutex); |
| 135 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 136 | list_for_each_entry(devlink, &devlink_list, list) { |
| 137 | if (strcmp(devlink->dev->bus->name, busname) == 0 && |
| 138 | strcmp(dev_name(devlink->dev), devname) == 0 && |
| 139 | net_eq(devlink_net(devlink), net)) |
| 140 | return devlink; |
| 141 | } |
| 142 | |
| 143 | return ERR_PTR(-ENODEV); |
| 144 | } |
| 145 | |
| 146 | static struct devlink *devlink_get_from_info(struct genl_info *info) |
| 147 | { |
| 148 | return devlink_get_from_attrs(genl_info_net(info), info->attrs); |
| 149 | } |
| 150 | |
| 151 | static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, |
Parav Pandit | c7282b5 | 2019-08-30 05:39:44 -0500 | [diff] [blame] | 152 | unsigned int port_index) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 153 | { |
| 154 | struct devlink_port *devlink_port; |
| 155 | |
| 156 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 157 | if (devlink_port->index == port_index) |
| 158 | return devlink_port; |
| 159 | } |
| 160 | return NULL; |
| 161 | } |
| 162 | |
Parav Pandit | c7282b5 | 2019-08-30 05:39:44 -0500 | [diff] [blame] | 163 | static bool devlink_port_index_exists(struct devlink *devlink, |
| 164 | unsigned int port_index) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 165 | { |
| 166 | return devlink_port_get_by_index(devlink, port_index); |
| 167 | } |
| 168 | |
| 169 | static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, |
| 170 | struct nlattr **attrs) |
| 171 | { |
| 172 | if (attrs[DEVLINK_ATTR_PORT_INDEX]) { |
| 173 | u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 174 | struct devlink_port *devlink_port; |
| 175 | |
| 176 | devlink_port = devlink_port_get_by_index(devlink, port_index); |
| 177 | if (!devlink_port) |
| 178 | return ERR_PTR(-ENODEV); |
| 179 | return devlink_port; |
| 180 | } |
| 181 | return ERR_PTR(-EINVAL); |
| 182 | } |
| 183 | |
| 184 | static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink, |
| 185 | struct genl_info *info) |
| 186 | { |
| 187 | return devlink_port_get_from_attrs(devlink, info->attrs); |
| 188 | } |
| 189 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 190 | struct devlink_sb { |
| 191 | struct list_head list; |
| 192 | unsigned int index; |
| 193 | u32 size; |
| 194 | u16 ingress_pools_count; |
| 195 | u16 egress_pools_count; |
| 196 | u16 ingress_tc_count; |
| 197 | u16 egress_tc_count; |
| 198 | }; |
| 199 | |
| 200 | static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb) |
| 201 | { |
| 202 | return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count; |
| 203 | } |
| 204 | |
| 205 | static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink, |
| 206 | unsigned int sb_index) |
| 207 | { |
| 208 | struct devlink_sb *devlink_sb; |
| 209 | |
| 210 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 211 | if (devlink_sb->index == sb_index) |
| 212 | return devlink_sb; |
| 213 | } |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | static bool devlink_sb_index_exists(struct devlink *devlink, |
| 218 | unsigned int sb_index) |
| 219 | { |
| 220 | return devlink_sb_get_by_index(devlink, sb_index); |
| 221 | } |
| 222 | |
| 223 | static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink, |
| 224 | struct nlattr **attrs) |
| 225 | { |
| 226 | if (attrs[DEVLINK_ATTR_SB_INDEX]) { |
| 227 | u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]); |
| 228 | struct devlink_sb *devlink_sb; |
| 229 | |
| 230 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 231 | if (!devlink_sb) |
| 232 | return ERR_PTR(-ENODEV); |
| 233 | return devlink_sb; |
| 234 | } |
| 235 | return ERR_PTR(-EINVAL); |
| 236 | } |
| 237 | |
| 238 | static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink, |
| 239 | struct genl_info *info) |
| 240 | { |
| 241 | return devlink_sb_get_from_attrs(devlink, info->attrs); |
| 242 | } |
| 243 | |
| 244 | static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 245 | struct nlattr **attrs, |
| 246 | u16 *p_pool_index) |
| 247 | { |
| 248 | u16 val; |
| 249 | |
| 250 | if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX]) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]); |
| 254 | if (val >= devlink_sb_pool_count(devlink_sb)) |
| 255 | return -EINVAL; |
| 256 | *p_pool_index = val; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb, |
| 261 | struct genl_info *info, |
| 262 | u16 *p_pool_index) |
| 263 | { |
| 264 | return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs, |
| 265 | p_pool_index); |
| 266 | } |
| 267 | |
| 268 | static int |
| 269 | devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs, |
| 270 | enum devlink_sb_pool_type *p_pool_type) |
| 271 | { |
| 272 | u8 val; |
| 273 | |
| 274 | if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE]) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]); |
| 278 | if (val != DEVLINK_SB_POOL_TYPE_INGRESS && |
| 279 | val != DEVLINK_SB_POOL_TYPE_EGRESS) |
| 280 | return -EINVAL; |
| 281 | *p_pool_type = val; |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int |
| 286 | devlink_sb_pool_type_get_from_info(struct genl_info *info, |
| 287 | enum devlink_sb_pool_type *p_pool_type) |
| 288 | { |
| 289 | return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type); |
| 290 | } |
| 291 | |
| 292 | static int |
| 293 | devlink_sb_th_type_get_from_attrs(struct nlattr **attrs, |
| 294 | enum devlink_sb_threshold_type *p_th_type) |
| 295 | { |
| 296 | u8 val; |
| 297 | |
| 298 | if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]) |
| 299 | return -EINVAL; |
| 300 | |
| 301 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]); |
| 302 | if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC && |
| 303 | val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC) |
| 304 | return -EINVAL; |
| 305 | *p_th_type = val; |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static int |
| 310 | devlink_sb_th_type_get_from_info(struct genl_info *info, |
| 311 | enum devlink_sb_threshold_type *p_th_type) |
| 312 | { |
| 313 | return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type); |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 318 | struct nlattr **attrs, |
| 319 | enum devlink_sb_pool_type pool_type, |
| 320 | u16 *p_tc_index) |
| 321 | { |
| 322 | u16 val; |
| 323 | |
| 324 | if (!attrs[DEVLINK_ATTR_SB_TC_INDEX]) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]); |
| 328 | if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS && |
| 329 | val >= devlink_sb->ingress_tc_count) |
| 330 | return -EINVAL; |
| 331 | if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS && |
| 332 | val >= devlink_sb->egress_tc_count) |
| 333 | return -EINVAL; |
| 334 | *p_tc_index = val; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int |
| 339 | devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb, |
| 340 | struct genl_info *info, |
| 341 | enum devlink_sb_pool_type pool_type, |
| 342 | u16 *p_tc_index) |
| 343 | { |
| 344 | return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs, |
| 345 | pool_type, p_tc_index); |
| 346 | } |
| 347 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 348 | struct devlink_region { |
| 349 | struct devlink *devlink; |
| 350 | struct list_head list; |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 351 | const struct devlink_region_ops *ops; |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 352 | struct list_head snapshot_list; |
| 353 | u32 max_snapshots; |
| 354 | u32 cur_snapshots; |
| 355 | u64 size; |
| 356 | }; |
| 357 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 358 | struct devlink_snapshot { |
| 359 | struct list_head list; |
| 360 | struct devlink_region *region; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 361 | u8 *data; |
| 362 | u32 id; |
| 363 | }; |
| 364 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 365 | static struct devlink_region * |
| 366 | devlink_region_get_by_name(struct devlink *devlink, const char *region_name) |
| 367 | { |
| 368 | struct devlink_region *region; |
| 369 | |
| 370 | list_for_each_entry(region, &devlink->region_list, list) |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 371 | if (!strcmp(region->ops->name, region_name)) |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 372 | return region; |
| 373 | |
| 374 | return NULL; |
| 375 | } |
| 376 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 377 | static struct devlink_snapshot * |
| 378 | devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id) |
| 379 | { |
| 380 | struct devlink_snapshot *snapshot; |
| 381 | |
| 382 | list_for_each_entry(snapshot, ®ion->snapshot_list, list) |
| 383 | if (snapshot->id == id) |
| 384 | return snapshot; |
| 385 | |
| 386 | return NULL; |
| 387 | } |
| 388 | |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 389 | #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0) |
| 390 | #define DEVLINK_NL_FLAG_NEED_PORT BIT(1) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 391 | #define DEVLINK_NL_FLAG_NEED_SB BIT(2) |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 392 | |
| 393 | /* The per devlink instance lock is taken by default in the pre-doit |
| 394 | * operation, yet several commands do not require this. The global |
| 395 | * devlink lock is taken and protects from disruption by user-calls. |
| 396 | */ |
| 397 | #define DEVLINK_NL_FLAG_NO_LOCK BIT(3) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 398 | |
| 399 | static int devlink_nl_pre_doit(const struct genl_ops *ops, |
| 400 | struct sk_buff *skb, struct genl_info *info) |
| 401 | { |
| 402 | struct devlink *devlink; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 403 | int err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 404 | |
| 405 | mutex_lock(&devlink_mutex); |
| 406 | devlink = devlink_get_from_info(info); |
| 407 | if (IS_ERR(devlink)) { |
| 408 | mutex_unlock(&devlink_mutex); |
| 409 | return PTR_ERR(devlink); |
| 410 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 411 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 412 | mutex_lock(&devlink->lock); |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 413 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 414 | info->user_ptr[0] = devlink; |
| 415 | } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 416 | struct devlink_port *devlink_port; |
| 417 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 418 | devlink_port = devlink_port_get_from_info(devlink, info); |
| 419 | if (IS_ERR(devlink_port)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 420 | err = PTR_ERR(devlink_port); |
| 421 | goto unlock; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 422 | } |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 423 | info->user_ptr[0] = devlink_port; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 424 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 425 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) { |
| 426 | struct devlink_sb *devlink_sb; |
| 427 | |
| 428 | devlink_sb = devlink_sb_get_from_info(devlink, info); |
| 429 | if (IS_ERR(devlink_sb)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 430 | err = PTR_ERR(devlink_sb); |
| 431 | goto unlock; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 432 | } |
| 433 | info->user_ptr[1] = devlink_sb; |
| 434 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 435 | return 0; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 436 | |
| 437 | unlock: |
| 438 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 439 | mutex_unlock(&devlink->lock); |
| 440 | mutex_unlock(&devlink_mutex); |
| 441 | return err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static void devlink_nl_post_doit(const struct genl_ops *ops, |
| 445 | struct sk_buff *skb, struct genl_info *info) |
| 446 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 447 | struct devlink *devlink; |
| 448 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 449 | /* When devlink changes netns, it would not be found |
| 450 | * by devlink_get_from_info(). So try if it is stored first. |
| 451 | */ |
| 452 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 453 | devlink = info->user_ptr[0]; |
| 454 | } else { |
| 455 | devlink = devlink_get_from_info(info); |
| 456 | WARN_ON(IS_ERR(devlink)); |
| 457 | } |
| 458 | if (!IS_ERR(devlink) && ~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 459 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 460 | mutex_unlock(&devlink_mutex); |
| 461 | } |
| 462 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 463 | static struct genl_family devlink_nl_family; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 464 | |
| 465 | enum devlink_multicast_groups { |
| 466 | DEVLINK_MCGRP_CONFIG, |
| 467 | }; |
| 468 | |
| 469 | static const struct genl_multicast_group devlink_nl_mcgrps[] = { |
| 470 | [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, |
| 471 | }; |
| 472 | |
| 473 | static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) |
| 474 | { |
| 475 | if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name)) |
| 476 | return -EMSGSIZE; |
| 477 | if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev))) |
| 478 | return -EMSGSIZE; |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink, |
| 483 | enum devlink_command cmd, u32 portid, |
| 484 | u32 seq, int flags) |
| 485 | { |
| 486 | void *hdr; |
| 487 | |
| 488 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 489 | if (!hdr) |
| 490 | return -EMSGSIZE; |
| 491 | |
| 492 | if (devlink_nl_put_handle(msg, devlink)) |
| 493 | goto nla_put_failure; |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 494 | if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed)) |
| 495 | goto nla_put_failure; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 496 | |
| 497 | genlmsg_end(msg, hdr); |
| 498 | return 0; |
| 499 | |
| 500 | nla_put_failure: |
| 501 | genlmsg_cancel(msg, hdr); |
| 502 | return -EMSGSIZE; |
| 503 | } |
| 504 | |
| 505 | static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) |
| 506 | { |
| 507 | struct sk_buff *msg; |
| 508 | int err; |
| 509 | |
| 510 | WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); |
| 511 | |
| 512 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 513 | if (!msg) |
| 514 | return; |
| 515 | |
| 516 | err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0); |
| 517 | if (err) { |
| 518 | nlmsg_free(msg); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 523 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 524 | } |
| 525 | |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 526 | static int devlink_nl_port_attrs_put(struct sk_buff *msg, |
| 527 | struct devlink_port *devlink_port) |
| 528 | { |
| 529 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 530 | |
Danielle Ratson | 10a429b | 2020-07-09 16:18:14 +0300 | [diff] [blame] | 531 | if (!devlink_port->attrs_set) |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 532 | return 0; |
Danielle Ratson | a21cf0a | 2020-07-09 16:18:18 +0300 | [diff] [blame] | 533 | if (attrs->lanes) { |
| 534 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes)) |
| 535 | return -EMSGSIZE; |
| 536 | } |
Danielle Ratson | a0f49b5 | 2020-07-09 16:18:20 +0300 | [diff] [blame] | 537 | if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable)) |
| 538 | return -EMSGSIZE; |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 539 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour)) |
| 540 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 541 | switch (devlink_port->attrs.flavour) { |
| 542 | case DEVLINK_PORT_FLAVOUR_PCI_PF: |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 543 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, |
| 544 | attrs->pci_pf.pf)) |
| 545 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 546 | break; |
| 547 | case DEVLINK_PORT_FLAVOUR_PCI_VF: |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 548 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, |
| 549 | attrs->pci_vf.pf) || |
| 550 | nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, |
| 551 | attrs->pci_vf.vf)) |
| 552 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 553 | break; |
| 554 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: |
| 555 | case DEVLINK_PORT_FLAVOUR_CPU: |
| 556 | case DEVLINK_PORT_FLAVOUR_DSA: |
Parav Pandit | acf1ee4 | 2020-03-03 08:12:42 -0600 | [diff] [blame] | 557 | case DEVLINK_PORT_FLAVOUR_VIRTUAL: |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 558 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, |
| 559 | attrs->phys.port_number)) |
| 560 | return -EMSGSIZE; |
| 561 | if (!attrs->split) |
| 562 | return 0; |
| 563 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, |
| 564 | attrs->phys.port_number)) |
| 565 | return -EMSGSIZE; |
| 566 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER, |
| 567 | attrs->phys.split_subport_number)) |
| 568 | return -EMSGSIZE; |
| 569 | break; |
| 570 | default: |
| 571 | break; |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 572 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Parav Pandit | 2a916ec | 2020-06-19 03:32:48 +0000 | [diff] [blame] | 576 | static int |
| 577 | devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port, |
| 578 | struct netlink_ext_ack *extack) |
| 579 | { |
| 580 | struct devlink *devlink = port->devlink; |
| 581 | const struct devlink_ops *ops; |
| 582 | struct nlattr *function_attr; |
| 583 | bool empty_nest = true; |
| 584 | int err = 0; |
| 585 | |
| 586 | function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION); |
| 587 | if (!function_attr) |
| 588 | return -EMSGSIZE; |
| 589 | |
| 590 | ops = devlink->ops; |
| 591 | if (ops->port_function_hw_addr_get) { |
Stephen Rothwell | 29cb986 | 2020-06-23 13:43:06 +1000 | [diff] [blame] | 592 | int hw_addr_len; |
Parav Pandit | 2a916ec | 2020-06-19 03:32:48 +0000 | [diff] [blame] | 593 | u8 hw_addr[MAX_ADDR_LEN]; |
| 594 | |
| 595 | err = ops->port_function_hw_addr_get(devlink, port, hw_addr, &hw_addr_len, extack); |
| 596 | if (err == -EOPNOTSUPP) { |
| 597 | /* Port function attributes are optional for a port. If port doesn't |
| 598 | * support function attribute, returning -EOPNOTSUPP is not an error. |
| 599 | */ |
| 600 | err = 0; |
| 601 | goto out; |
| 602 | } else if (err) { |
| 603 | goto out; |
| 604 | } |
| 605 | err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr); |
| 606 | if (err) |
| 607 | goto out; |
| 608 | empty_nest = false; |
| 609 | } |
| 610 | |
| 611 | out: |
| 612 | if (err || empty_nest) |
| 613 | nla_nest_cancel(msg, function_attr); |
| 614 | else |
| 615 | nla_nest_end(msg, function_attr); |
| 616 | return err; |
| 617 | } |
| 618 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 619 | static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, |
| 620 | struct devlink_port *devlink_port, |
| 621 | enum devlink_command cmd, u32 portid, |
Parav Pandit | a829eb0 | 2020-06-19 03:32:47 +0000 | [diff] [blame] | 622 | u32 seq, int flags, |
| 623 | struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 624 | { |
| 625 | void *hdr; |
| 626 | |
| 627 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 628 | if (!hdr) |
| 629 | return -EMSGSIZE; |
| 630 | |
| 631 | if (devlink_nl_put_handle(msg, devlink)) |
| 632 | goto nla_put_failure; |
| 633 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 634 | goto nla_put_failure; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 635 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 636 | spin_lock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 637 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 638 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 639 | if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET && |
| 640 | nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE, |
| 641 | devlink_port->desired_type)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 642 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 643 | if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { |
| 644 | struct net_device *netdev = devlink_port->type_dev; |
| 645 | |
| 646 | if (netdev && |
| 647 | (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, |
| 648 | netdev->ifindex) || |
| 649 | nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, |
| 650 | netdev->name))) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 651 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 652 | } |
| 653 | if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { |
| 654 | struct ib_device *ibdev = devlink_port->type_dev; |
| 655 | |
| 656 | if (ibdev && |
| 657 | nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME, |
| 658 | ibdev->name)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 659 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 660 | } |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 661 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 662 | if (devlink_nl_port_attrs_put(msg, devlink_port)) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 663 | goto nla_put_failure; |
Parav Pandit | 2a916ec | 2020-06-19 03:32:48 +0000 | [diff] [blame] | 664 | if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack)) |
| 665 | goto nla_put_failure; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 666 | |
| 667 | genlmsg_end(msg, hdr); |
| 668 | return 0; |
| 669 | |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 670 | nla_put_failure_type_locked: |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 671 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 672 | nla_put_failure: |
| 673 | genlmsg_cancel(msg, hdr); |
| 674 | return -EMSGSIZE; |
| 675 | } |
| 676 | |
| 677 | static void devlink_port_notify(struct devlink_port *devlink_port, |
| 678 | enum devlink_command cmd) |
| 679 | { |
| 680 | struct devlink *devlink = devlink_port->devlink; |
| 681 | struct sk_buff *msg; |
| 682 | int err; |
| 683 | |
| 684 | if (!devlink_port->registered) |
| 685 | return; |
| 686 | |
| 687 | WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); |
| 688 | |
| 689 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 690 | if (!msg) |
| 691 | return; |
| 692 | |
Parav Pandit | a829eb0 | 2020-06-19 03:32:47 +0000 | [diff] [blame] | 693 | err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0, |
| 694 | NULL); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 695 | if (err) { |
| 696 | nlmsg_free(msg); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 701 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 702 | } |
| 703 | |
| 704 | static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info) |
| 705 | { |
| 706 | struct devlink *devlink = info->user_ptr[0]; |
| 707 | struct sk_buff *msg; |
| 708 | int err; |
| 709 | |
| 710 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 711 | if (!msg) |
| 712 | return -ENOMEM; |
| 713 | |
| 714 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 715 | info->snd_portid, info->snd_seq, 0); |
| 716 | if (err) { |
| 717 | nlmsg_free(msg); |
| 718 | return err; |
| 719 | } |
| 720 | |
| 721 | return genlmsg_reply(msg, info); |
| 722 | } |
| 723 | |
| 724 | static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg, |
| 725 | struct netlink_callback *cb) |
| 726 | { |
| 727 | struct devlink *devlink; |
| 728 | int start = cb->args[0]; |
| 729 | int idx = 0; |
| 730 | int err; |
| 731 | |
| 732 | mutex_lock(&devlink_mutex); |
| 733 | list_for_each_entry(devlink, &devlink_list, list) { |
| 734 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 735 | continue; |
| 736 | if (idx < start) { |
| 737 | idx++; |
| 738 | continue; |
| 739 | } |
| 740 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 741 | NETLINK_CB(cb->skb).portid, |
| 742 | cb->nlh->nlmsg_seq, NLM_F_MULTI); |
| 743 | if (err) |
| 744 | goto out; |
| 745 | idx++; |
| 746 | } |
| 747 | out: |
| 748 | mutex_unlock(&devlink_mutex); |
| 749 | |
| 750 | cb->args[0] = idx; |
| 751 | return msg->len; |
| 752 | } |
| 753 | |
| 754 | static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb, |
| 755 | struct genl_info *info) |
| 756 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 757 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 758 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 759 | struct sk_buff *msg; |
| 760 | int err; |
| 761 | |
| 762 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 763 | if (!msg) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 767 | DEVLINK_CMD_PORT_NEW, |
Parav Pandit | a829eb0 | 2020-06-19 03:32:47 +0000 | [diff] [blame] | 768 | info->snd_portid, info->snd_seq, 0, |
| 769 | info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 770 | if (err) { |
| 771 | nlmsg_free(msg); |
| 772 | return err; |
| 773 | } |
| 774 | |
| 775 | return genlmsg_reply(msg, info); |
| 776 | } |
| 777 | |
| 778 | static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg, |
| 779 | struct netlink_callback *cb) |
| 780 | { |
| 781 | struct devlink *devlink; |
| 782 | struct devlink_port *devlink_port; |
| 783 | int start = cb->args[0]; |
| 784 | int idx = 0; |
| 785 | int err; |
| 786 | |
| 787 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 788 | list_for_each_entry(devlink, &devlink_list, list) { |
| 789 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 790 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 791 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 792 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 793 | if (idx < start) { |
| 794 | idx++; |
| 795 | continue; |
| 796 | } |
| 797 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 798 | DEVLINK_CMD_NEW, |
| 799 | NETLINK_CB(cb->skb).portid, |
| 800 | cb->nlh->nlmsg_seq, |
Parav Pandit | a829eb0 | 2020-06-19 03:32:47 +0000 | [diff] [blame] | 801 | NLM_F_MULTI, |
| 802 | cb->extack); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 803 | if (err) { |
| 804 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 805 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 806 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 807 | idx++; |
| 808 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 809 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 810 | } |
| 811 | out: |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 812 | mutex_unlock(&devlink_mutex); |
| 813 | |
| 814 | cb->args[0] = idx; |
| 815 | return msg->len; |
| 816 | } |
| 817 | |
| 818 | static int devlink_port_type_set(struct devlink *devlink, |
| 819 | struct devlink_port *devlink_port, |
| 820 | enum devlink_port_type port_type) |
| 821 | |
| 822 | { |
| 823 | int err; |
| 824 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 825 | if (devlink->ops->port_type_set) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 826 | if (port_type == DEVLINK_PORT_TYPE_NOTSET) |
| 827 | return -EINVAL; |
Elad Raz | 6edf101 | 2016-10-23 17:43:05 +0200 | [diff] [blame] | 828 | if (port_type == devlink_port->type) |
| 829 | return 0; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 830 | err = devlink->ops->port_type_set(devlink_port, port_type); |
| 831 | if (err) |
| 832 | return err; |
| 833 | devlink_port->desired_type = port_type; |
| 834 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 835 | return 0; |
| 836 | } |
| 837 | return -EOPNOTSUPP; |
| 838 | } |
| 839 | |
Parav Pandit | a1e8ae9 | 2020-06-19 03:32:49 +0000 | [diff] [blame] | 840 | static int |
| 841 | devlink_port_function_hw_addr_set(struct devlink *devlink, struct devlink_port *port, |
| 842 | const struct nlattr *attr, struct netlink_ext_ack *extack) |
| 843 | { |
| 844 | const struct devlink_ops *ops; |
| 845 | const u8 *hw_addr; |
| 846 | int hw_addr_len; |
| 847 | int err; |
| 848 | |
| 849 | hw_addr = nla_data(attr); |
| 850 | hw_addr_len = nla_len(attr); |
| 851 | if (hw_addr_len > MAX_ADDR_LEN) { |
| 852 | NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long"); |
| 853 | return -EINVAL; |
| 854 | } |
| 855 | if (port->type == DEVLINK_PORT_TYPE_ETH) { |
| 856 | if (hw_addr_len != ETH_ALEN) { |
| 857 | NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device"); |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | if (!is_unicast_ether_addr(hw_addr)) { |
| 861 | NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported"); |
| 862 | return -EINVAL; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | ops = devlink->ops; |
| 867 | if (!ops->port_function_hw_addr_set) { |
| 868 | NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes"); |
| 869 | return -EOPNOTSUPP; |
| 870 | } |
| 871 | |
| 872 | err = ops->port_function_hw_addr_set(devlink, port, hw_addr, hw_addr_len, extack); |
| 873 | if (err) |
| 874 | return err; |
| 875 | |
| 876 | devlink_port_notify(port, DEVLINK_CMD_PORT_NEW); |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static int |
| 881 | devlink_port_function_set(struct devlink *devlink, struct devlink_port *port, |
| 882 | const struct nlattr *attr, struct netlink_ext_ack *extack) |
| 883 | { |
| 884 | struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1]; |
| 885 | int err; |
| 886 | |
| 887 | err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr, |
| 888 | devlink_function_nl_policy, extack); |
| 889 | if (err < 0) { |
| 890 | NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes"); |
| 891 | return err; |
| 892 | } |
| 893 | |
| 894 | attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]; |
| 895 | if (attr) |
| 896 | err = devlink_port_function_hw_addr_set(devlink, port, attr, extack); |
| 897 | |
| 898 | return err; |
| 899 | } |
| 900 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 901 | static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, |
| 902 | struct genl_info *info) |
| 903 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 904 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 905 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 906 | int err; |
| 907 | |
| 908 | if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) { |
| 909 | enum devlink_port_type port_type; |
| 910 | |
| 911 | port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]); |
| 912 | err = devlink_port_type_set(devlink, devlink_port, port_type); |
| 913 | if (err) |
| 914 | return err; |
| 915 | } |
Parav Pandit | a1e8ae9 | 2020-06-19 03:32:49 +0000 | [diff] [blame] | 916 | |
| 917 | if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) { |
| 918 | struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION]; |
| 919 | struct netlink_ext_ack *extack = info->extack; |
| 920 | |
| 921 | err = devlink_port_function_set(devlink, devlink_port, attr, extack); |
| 922 | if (err) |
| 923 | return err; |
| 924 | } |
| 925 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 929 | static int devlink_port_split(struct devlink *devlink, u32 port_index, |
| 930 | u32 count, struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 931 | |
| 932 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 933 | if (devlink->ops->port_split) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 934 | return devlink->ops->port_split(devlink, port_index, count, |
| 935 | extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 936 | return -EOPNOTSUPP; |
| 937 | } |
| 938 | |
| 939 | static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, |
| 940 | struct genl_info *info) |
| 941 | { |
| 942 | struct devlink *devlink = info->user_ptr[0]; |
Danielle Ratson | 82901ad | 2020-07-09 16:18:21 +0300 | [diff] [blame] | 943 | struct devlink_port *devlink_port; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 944 | u32 port_index; |
| 945 | u32 count; |
| 946 | |
| 947 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] || |
| 948 | !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]) |
| 949 | return -EINVAL; |
| 950 | |
Danielle Ratson | 82901ad | 2020-07-09 16:18:21 +0300 | [diff] [blame] | 951 | devlink_port = devlink_port_get_from_info(devlink, info); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 952 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 953 | count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]); |
Danielle Ratson | 82901ad | 2020-07-09 16:18:21 +0300 | [diff] [blame] | 954 | |
| 955 | if (IS_ERR(devlink_port)) |
| 956 | return -EINVAL; |
| 957 | |
| 958 | if (!devlink_port->attrs.splittable) { |
| 959 | /* Split ports cannot be split. */ |
| 960 | if (devlink_port->attrs.split) |
| 961 | NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further"); |
| 962 | else |
| 963 | NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split"); |
| 964 | return -EINVAL; |
| 965 | } |
| 966 | |
| 967 | if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) { |
| 968 | NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count"); |
| 969 | return -EINVAL; |
| 970 | } |
| 971 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 972 | return devlink_port_split(devlink, port_index, count, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 973 | } |
| 974 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 975 | static int devlink_port_unsplit(struct devlink *devlink, u32 port_index, |
| 976 | struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 977 | |
| 978 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 979 | if (devlink->ops->port_unsplit) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 980 | return devlink->ops->port_unsplit(devlink, port_index, extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 981 | return -EOPNOTSUPP; |
| 982 | } |
| 983 | |
| 984 | static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb, |
| 985 | struct genl_info *info) |
| 986 | { |
| 987 | struct devlink *devlink = info->user_ptr[0]; |
| 988 | u32 port_index; |
| 989 | |
| 990 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) |
| 991 | return -EINVAL; |
| 992 | |
| 993 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 994 | return devlink_port_unsplit(devlink, port_index, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 995 | } |
| 996 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 997 | static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink, |
| 998 | struct devlink_sb *devlink_sb, |
| 999 | enum devlink_command cmd, u32 portid, |
| 1000 | u32 seq, int flags) |
| 1001 | { |
| 1002 | void *hdr; |
| 1003 | |
| 1004 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1005 | if (!hdr) |
| 1006 | return -EMSGSIZE; |
| 1007 | |
| 1008 | if (devlink_nl_put_handle(msg, devlink)) |
| 1009 | goto nla_put_failure; |
| 1010 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1011 | goto nla_put_failure; |
| 1012 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size)) |
| 1013 | goto nla_put_failure; |
| 1014 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT, |
| 1015 | devlink_sb->ingress_pools_count)) |
| 1016 | goto nla_put_failure; |
| 1017 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT, |
| 1018 | devlink_sb->egress_pools_count)) |
| 1019 | goto nla_put_failure; |
| 1020 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT, |
| 1021 | devlink_sb->ingress_tc_count)) |
| 1022 | goto nla_put_failure; |
| 1023 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT, |
| 1024 | devlink_sb->egress_tc_count)) |
| 1025 | goto nla_put_failure; |
| 1026 | |
| 1027 | genlmsg_end(msg, hdr); |
| 1028 | return 0; |
| 1029 | |
| 1030 | nla_put_failure: |
| 1031 | genlmsg_cancel(msg, hdr); |
| 1032 | return -EMSGSIZE; |
| 1033 | } |
| 1034 | |
| 1035 | static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb, |
| 1036 | struct genl_info *info) |
| 1037 | { |
| 1038 | struct devlink *devlink = info->user_ptr[0]; |
| 1039 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1040 | struct sk_buff *msg; |
| 1041 | int err; |
| 1042 | |
| 1043 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1044 | if (!msg) |
| 1045 | return -ENOMEM; |
| 1046 | |
| 1047 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 1048 | DEVLINK_CMD_SB_NEW, |
| 1049 | info->snd_portid, info->snd_seq, 0); |
| 1050 | if (err) { |
| 1051 | nlmsg_free(msg); |
| 1052 | return err; |
| 1053 | } |
| 1054 | |
| 1055 | return genlmsg_reply(msg, info); |
| 1056 | } |
| 1057 | |
| 1058 | static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg, |
| 1059 | struct netlink_callback *cb) |
| 1060 | { |
| 1061 | struct devlink *devlink; |
| 1062 | struct devlink_sb *devlink_sb; |
| 1063 | int start = cb->args[0]; |
| 1064 | int idx = 0; |
| 1065 | int err; |
| 1066 | |
| 1067 | mutex_lock(&devlink_mutex); |
| 1068 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1069 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 1070 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1071 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1072 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1073 | if (idx < start) { |
| 1074 | idx++; |
| 1075 | continue; |
| 1076 | } |
| 1077 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 1078 | DEVLINK_CMD_SB_NEW, |
| 1079 | NETLINK_CB(cb->skb).portid, |
| 1080 | cb->nlh->nlmsg_seq, |
| 1081 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1082 | if (err) { |
| 1083 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1084 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1085 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1086 | idx++; |
| 1087 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1088 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1089 | } |
| 1090 | out: |
| 1091 | mutex_unlock(&devlink_mutex); |
| 1092 | |
| 1093 | cb->args[0] = idx; |
| 1094 | return msg->len; |
| 1095 | } |
| 1096 | |
| 1097 | static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1098 | struct devlink_sb *devlink_sb, |
| 1099 | u16 pool_index, enum devlink_command cmd, |
| 1100 | u32 portid, u32 seq, int flags) |
| 1101 | { |
| 1102 | struct devlink_sb_pool_info pool_info; |
| 1103 | void *hdr; |
| 1104 | int err; |
| 1105 | |
| 1106 | err = devlink->ops->sb_pool_get(devlink, devlink_sb->index, |
| 1107 | pool_index, &pool_info); |
| 1108 | if (err) |
| 1109 | return err; |
| 1110 | |
| 1111 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1112 | if (!hdr) |
| 1113 | return -EMSGSIZE; |
| 1114 | |
| 1115 | if (devlink_nl_put_handle(msg, devlink)) |
| 1116 | goto nla_put_failure; |
| 1117 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1118 | goto nla_put_failure; |
| 1119 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1120 | goto nla_put_failure; |
| 1121 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type)) |
| 1122 | goto nla_put_failure; |
| 1123 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size)) |
| 1124 | goto nla_put_failure; |
| 1125 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE, |
| 1126 | pool_info.threshold_type)) |
| 1127 | goto nla_put_failure; |
Jakub Kicinski | bff5731 | 2019-02-01 17:56:28 -0800 | [diff] [blame] | 1128 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE, |
| 1129 | pool_info.cell_size)) |
| 1130 | goto nla_put_failure; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1131 | |
| 1132 | genlmsg_end(msg, hdr); |
| 1133 | return 0; |
| 1134 | |
| 1135 | nla_put_failure: |
| 1136 | genlmsg_cancel(msg, hdr); |
| 1137 | return -EMSGSIZE; |
| 1138 | } |
| 1139 | |
| 1140 | static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb, |
| 1141 | struct genl_info *info) |
| 1142 | { |
| 1143 | struct devlink *devlink = info->user_ptr[0]; |
| 1144 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1145 | struct sk_buff *msg; |
| 1146 | u16 pool_index; |
| 1147 | int err; |
| 1148 | |
| 1149 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1150 | &pool_index); |
| 1151 | if (err) |
| 1152 | return err; |
| 1153 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1154 | if (!devlink->ops->sb_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1155 | return -EOPNOTSUPP; |
| 1156 | |
| 1157 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1158 | if (!msg) |
| 1159 | return -ENOMEM; |
| 1160 | |
| 1161 | err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index, |
| 1162 | DEVLINK_CMD_SB_POOL_NEW, |
| 1163 | info->snd_portid, info->snd_seq, 0); |
| 1164 | if (err) { |
| 1165 | nlmsg_free(msg); |
| 1166 | return err; |
| 1167 | } |
| 1168 | |
| 1169 | return genlmsg_reply(msg, info); |
| 1170 | } |
| 1171 | |
| 1172 | static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1173 | struct devlink *devlink, |
| 1174 | struct devlink_sb *devlink_sb, |
| 1175 | u32 portid, u32 seq) |
| 1176 | { |
| 1177 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1178 | u16 pool_index; |
| 1179 | int err; |
| 1180 | |
| 1181 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1182 | if (*p_idx < start) { |
| 1183 | (*p_idx)++; |
| 1184 | continue; |
| 1185 | } |
| 1186 | err = devlink_nl_sb_pool_fill(msg, devlink, |
| 1187 | devlink_sb, |
| 1188 | pool_index, |
| 1189 | DEVLINK_CMD_SB_POOL_NEW, |
| 1190 | portid, seq, NLM_F_MULTI); |
| 1191 | if (err) |
| 1192 | return err; |
| 1193 | (*p_idx)++; |
| 1194 | } |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg, |
| 1199 | struct netlink_callback *cb) |
| 1200 | { |
| 1201 | struct devlink *devlink; |
| 1202 | struct devlink_sb *devlink_sb; |
| 1203 | int start = cb->args[0]; |
| 1204 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1205 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1206 | |
| 1207 | mutex_lock(&devlink_mutex); |
| 1208 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1209 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1210 | !devlink->ops->sb_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1211 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1212 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1213 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1214 | err = __sb_pool_get_dumpit(msg, start, &idx, devlink, |
| 1215 | devlink_sb, |
| 1216 | NETLINK_CB(cb->skb).portid, |
| 1217 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1218 | if (err && err != -EOPNOTSUPP) { |
| 1219 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1220 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1221 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1222 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1223 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1224 | } |
| 1225 | out: |
| 1226 | mutex_unlock(&devlink_mutex); |
| 1227 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1228 | if (err != -EMSGSIZE) |
| 1229 | return err; |
| 1230 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1231 | cb->args[0] = idx; |
| 1232 | return msg->len; |
| 1233 | } |
| 1234 | |
| 1235 | static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index, |
| 1236 | u16 pool_index, u32 size, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1237 | enum devlink_sb_threshold_type threshold_type, |
| 1238 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1239 | |
| 1240 | { |
| 1241 | const struct devlink_ops *ops = devlink->ops; |
| 1242 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1243 | if (ops->sb_pool_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1244 | return ops->sb_pool_set(devlink, sb_index, pool_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1245 | size, threshold_type, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1246 | return -EOPNOTSUPP; |
| 1247 | } |
| 1248 | |
| 1249 | static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb, |
| 1250 | struct genl_info *info) |
| 1251 | { |
| 1252 | struct devlink *devlink = info->user_ptr[0]; |
| 1253 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1254 | enum devlink_sb_threshold_type threshold_type; |
| 1255 | u16 pool_index; |
| 1256 | u32 size; |
| 1257 | int err; |
| 1258 | |
| 1259 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1260 | &pool_index); |
| 1261 | if (err) |
| 1262 | return err; |
| 1263 | |
| 1264 | err = devlink_sb_th_type_get_from_info(info, &threshold_type); |
| 1265 | if (err) |
| 1266 | return err; |
| 1267 | |
| 1268 | if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]) |
| 1269 | return -EINVAL; |
| 1270 | |
| 1271 | size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]); |
| 1272 | return devlink_sb_pool_set(devlink, devlink_sb->index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1273 | pool_index, size, threshold_type, |
| 1274 | info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg, |
| 1278 | struct devlink *devlink, |
| 1279 | struct devlink_port *devlink_port, |
| 1280 | struct devlink_sb *devlink_sb, |
| 1281 | u16 pool_index, |
| 1282 | enum devlink_command cmd, |
| 1283 | u32 portid, u32 seq, int flags) |
| 1284 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1285 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1286 | u32 threshold; |
| 1287 | void *hdr; |
| 1288 | int err; |
| 1289 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1290 | err = ops->sb_port_pool_get(devlink_port, devlink_sb->index, |
| 1291 | pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1292 | if (err) |
| 1293 | return err; |
| 1294 | |
| 1295 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1296 | if (!hdr) |
| 1297 | return -EMSGSIZE; |
| 1298 | |
| 1299 | if (devlink_nl_put_handle(msg, devlink)) |
| 1300 | goto nla_put_failure; |
| 1301 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1302 | goto nla_put_failure; |
| 1303 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1304 | goto nla_put_failure; |
| 1305 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1306 | goto nla_put_failure; |
| 1307 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1308 | goto nla_put_failure; |
| 1309 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1310 | if (ops->sb_occ_port_pool_get) { |
| 1311 | u32 cur; |
| 1312 | u32 max; |
| 1313 | |
| 1314 | err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index, |
| 1315 | pool_index, &cur, &max); |
| 1316 | if (err && err != -EOPNOTSUPP) |
| 1317 | return err; |
| 1318 | if (!err) { |
| 1319 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1320 | goto nla_put_failure; |
| 1321 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1322 | goto nla_put_failure; |
| 1323 | } |
| 1324 | } |
| 1325 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1326 | genlmsg_end(msg, hdr); |
| 1327 | return 0; |
| 1328 | |
| 1329 | nla_put_failure: |
| 1330 | genlmsg_cancel(msg, hdr); |
| 1331 | return -EMSGSIZE; |
| 1332 | } |
| 1333 | |
| 1334 | static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb, |
| 1335 | struct genl_info *info) |
| 1336 | { |
| 1337 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1338 | struct devlink *devlink = devlink_port->devlink; |
| 1339 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1340 | struct sk_buff *msg; |
| 1341 | u16 pool_index; |
| 1342 | int err; |
| 1343 | |
| 1344 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1345 | &pool_index); |
| 1346 | if (err) |
| 1347 | return err; |
| 1348 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1349 | if (!devlink->ops->sb_port_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1350 | return -EOPNOTSUPP; |
| 1351 | |
| 1352 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1353 | if (!msg) |
| 1354 | return -ENOMEM; |
| 1355 | |
| 1356 | err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port, |
| 1357 | devlink_sb, pool_index, |
| 1358 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1359 | info->snd_portid, info->snd_seq, 0); |
| 1360 | if (err) { |
| 1361 | nlmsg_free(msg); |
| 1362 | return err; |
| 1363 | } |
| 1364 | |
| 1365 | return genlmsg_reply(msg, info); |
| 1366 | } |
| 1367 | |
| 1368 | static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1369 | struct devlink *devlink, |
| 1370 | struct devlink_sb *devlink_sb, |
| 1371 | u32 portid, u32 seq) |
| 1372 | { |
| 1373 | struct devlink_port *devlink_port; |
| 1374 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1375 | u16 pool_index; |
| 1376 | int err; |
| 1377 | |
| 1378 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1379 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1380 | if (*p_idx < start) { |
| 1381 | (*p_idx)++; |
| 1382 | continue; |
| 1383 | } |
| 1384 | err = devlink_nl_sb_port_pool_fill(msg, devlink, |
| 1385 | devlink_port, |
| 1386 | devlink_sb, |
| 1387 | pool_index, |
| 1388 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1389 | portid, seq, |
| 1390 | NLM_F_MULTI); |
| 1391 | if (err) |
| 1392 | return err; |
| 1393 | (*p_idx)++; |
| 1394 | } |
| 1395 | } |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg, |
| 1400 | struct netlink_callback *cb) |
| 1401 | { |
| 1402 | struct devlink *devlink; |
| 1403 | struct devlink_sb *devlink_sb; |
| 1404 | int start = cb->args[0]; |
| 1405 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1406 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1407 | |
| 1408 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1409 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1410 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1411 | !devlink->ops->sb_port_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1412 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1413 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1414 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1415 | err = __sb_port_pool_get_dumpit(msg, start, &idx, |
| 1416 | devlink, devlink_sb, |
| 1417 | NETLINK_CB(cb->skb).portid, |
| 1418 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1419 | if (err && err != -EOPNOTSUPP) { |
| 1420 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1421 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1422 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1423 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1424 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1425 | } |
| 1426 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1427 | mutex_unlock(&devlink_mutex); |
| 1428 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1429 | if (err != -EMSGSIZE) |
| 1430 | return err; |
| 1431 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1432 | cb->args[0] = idx; |
| 1433 | return msg->len; |
| 1434 | } |
| 1435 | |
| 1436 | static int devlink_sb_port_pool_set(struct devlink_port *devlink_port, |
| 1437 | unsigned int sb_index, u16 pool_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1438 | u32 threshold, |
| 1439 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1440 | |
| 1441 | { |
| 1442 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1443 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1444 | if (ops->sb_port_pool_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1445 | return ops->sb_port_pool_set(devlink_port, sb_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1446 | pool_index, threshold, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1447 | return -EOPNOTSUPP; |
| 1448 | } |
| 1449 | |
| 1450 | static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb, |
| 1451 | struct genl_info *info) |
| 1452 | { |
| 1453 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1454 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1455 | u16 pool_index; |
| 1456 | u32 threshold; |
| 1457 | int err; |
| 1458 | |
| 1459 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1460 | &pool_index); |
| 1461 | if (err) |
| 1462 | return err; |
| 1463 | |
| 1464 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1465 | return -EINVAL; |
| 1466 | |
| 1467 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1468 | return devlink_sb_port_pool_set(devlink_port, devlink_sb->index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1469 | pool_index, threshold, info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | static int |
| 1473 | devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1474 | struct devlink_port *devlink_port, |
| 1475 | struct devlink_sb *devlink_sb, u16 tc_index, |
| 1476 | enum devlink_sb_pool_type pool_type, |
| 1477 | enum devlink_command cmd, |
| 1478 | u32 portid, u32 seq, int flags) |
| 1479 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1480 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1481 | u16 pool_index; |
| 1482 | u32 threshold; |
| 1483 | void *hdr; |
| 1484 | int err; |
| 1485 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1486 | err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index, |
| 1487 | tc_index, pool_type, |
| 1488 | &pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1489 | if (err) |
| 1490 | return err; |
| 1491 | |
| 1492 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1493 | if (!hdr) |
| 1494 | return -EMSGSIZE; |
| 1495 | |
| 1496 | if (devlink_nl_put_handle(msg, devlink)) |
| 1497 | goto nla_put_failure; |
| 1498 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1499 | goto nla_put_failure; |
| 1500 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1501 | goto nla_put_failure; |
| 1502 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index)) |
| 1503 | goto nla_put_failure; |
| 1504 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type)) |
| 1505 | goto nla_put_failure; |
| 1506 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1507 | goto nla_put_failure; |
| 1508 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1509 | goto nla_put_failure; |
| 1510 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1511 | if (ops->sb_occ_tc_port_bind_get) { |
| 1512 | u32 cur; |
| 1513 | u32 max; |
| 1514 | |
| 1515 | err = ops->sb_occ_tc_port_bind_get(devlink_port, |
| 1516 | devlink_sb->index, |
| 1517 | tc_index, pool_type, |
| 1518 | &cur, &max); |
| 1519 | if (err && err != -EOPNOTSUPP) |
| 1520 | return err; |
| 1521 | if (!err) { |
| 1522 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1523 | goto nla_put_failure; |
| 1524 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1525 | goto nla_put_failure; |
| 1526 | } |
| 1527 | } |
| 1528 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1529 | genlmsg_end(msg, hdr); |
| 1530 | return 0; |
| 1531 | |
| 1532 | nla_put_failure: |
| 1533 | genlmsg_cancel(msg, hdr); |
| 1534 | return -EMSGSIZE; |
| 1535 | } |
| 1536 | |
| 1537 | static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb, |
| 1538 | struct genl_info *info) |
| 1539 | { |
| 1540 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1541 | struct devlink *devlink = devlink_port->devlink; |
| 1542 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1543 | struct sk_buff *msg; |
| 1544 | enum devlink_sb_pool_type pool_type; |
| 1545 | u16 tc_index; |
| 1546 | int err; |
| 1547 | |
| 1548 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1549 | if (err) |
| 1550 | return err; |
| 1551 | |
| 1552 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1553 | pool_type, &tc_index); |
| 1554 | if (err) |
| 1555 | return err; |
| 1556 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1557 | if (!devlink->ops->sb_tc_pool_bind_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1558 | return -EOPNOTSUPP; |
| 1559 | |
| 1560 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1561 | if (!msg) |
| 1562 | return -ENOMEM; |
| 1563 | |
| 1564 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port, |
| 1565 | devlink_sb, tc_index, pool_type, |
| 1566 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1567 | info->snd_portid, |
| 1568 | info->snd_seq, 0); |
| 1569 | if (err) { |
| 1570 | nlmsg_free(msg); |
| 1571 | return err; |
| 1572 | } |
| 1573 | |
| 1574 | return genlmsg_reply(msg, info); |
| 1575 | } |
| 1576 | |
| 1577 | static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1578 | int start, int *p_idx, |
| 1579 | struct devlink *devlink, |
| 1580 | struct devlink_sb *devlink_sb, |
| 1581 | u32 portid, u32 seq) |
| 1582 | { |
| 1583 | struct devlink_port *devlink_port; |
| 1584 | u16 tc_index; |
| 1585 | int err; |
| 1586 | |
| 1587 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1588 | for (tc_index = 0; |
| 1589 | tc_index < devlink_sb->ingress_tc_count; tc_index++) { |
| 1590 | if (*p_idx < start) { |
| 1591 | (*p_idx)++; |
| 1592 | continue; |
| 1593 | } |
| 1594 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1595 | devlink_port, |
| 1596 | devlink_sb, |
| 1597 | tc_index, |
| 1598 | DEVLINK_SB_POOL_TYPE_INGRESS, |
| 1599 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1600 | portid, seq, |
| 1601 | NLM_F_MULTI); |
| 1602 | if (err) |
| 1603 | return err; |
| 1604 | (*p_idx)++; |
| 1605 | } |
| 1606 | for (tc_index = 0; |
| 1607 | tc_index < devlink_sb->egress_tc_count; tc_index++) { |
| 1608 | if (*p_idx < start) { |
| 1609 | (*p_idx)++; |
| 1610 | continue; |
| 1611 | } |
| 1612 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1613 | devlink_port, |
| 1614 | devlink_sb, |
| 1615 | tc_index, |
| 1616 | DEVLINK_SB_POOL_TYPE_EGRESS, |
| 1617 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1618 | portid, seq, |
| 1619 | NLM_F_MULTI); |
| 1620 | if (err) |
| 1621 | return err; |
| 1622 | (*p_idx)++; |
| 1623 | } |
| 1624 | } |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | static int |
| 1629 | devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1630 | struct netlink_callback *cb) |
| 1631 | { |
| 1632 | struct devlink *devlink; |
| 1633 | struct devlink_sb *devlink_sb; |
| 1634 | int start = cb->args[0]; |
| 1635 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1636 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1637 | |
| 1638 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1639 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1640 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1641 | !devlink->ops->sb_tc_pool_bind_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1642 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1643 | |
| 1644 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1645 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1646 | err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx, |
| 1647 | devlink, |
| 1648 | devlink_sb, |
| 1649 | NETLINK_CB(cb->skb).portid, |
| 1650 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1651 | if (err && err != -EOPNOTSUPP) { |
| 1652 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1653 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1654 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1655 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1656 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1657 | } |
| 1658 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1659 | mutex_unlock(&devlink_mutex); |
| 1660 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1661 | if (err != -EMSGSIZE) |
| 1662 | return err; |
| 1663 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1664 | cb->args[0] = idx; |
| 1665 | return msg->len; |
| 1666 | } |
| 1667 | |
| 1668 | static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port, |
| 1669 | unsigned int sb_index, u16 tc_index, |
| 1670 | enum devlink_sb_pool_type pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1671 | u16 pool_index, u32 threshold, |
| 1672 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1673 | |
| 1674 | { |
| 1675 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1676 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1677 | if (ops->sb_tc_pool_bind_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1678 | return ops->sb_tc_pool_bind_set(devlink_port, sb_index, |
| 1679 | tc_index, pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1680 | pool_index, threshold, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1681 | return -EOPNOTSUPP; |
| 1682 | } |
| 1683 | |
| 1684 | static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb, |
| 1685 | struct genl_info *info) |
| 1686 | { |
| 1687 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1688 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1689 | enum devlink_sb_pool_type pool_type; |
| 1690 | u16 tc_index; |
| 1691 | u16 pool_index; |
| 1692 | u32 threshold; |
| 1693 | int err; |
| 1694 | |
| 1695 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1696 | if (err) |
| 1697 | return err; |
| 1698 | |
| 1699 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1700 | pool_type, &tc_index); |
| 1701 | if (err) |
| 1702 | return err; |
| 1703 | |
| 1704 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1705 | &pool_index); |
| 1706 | if (err) |
| 1707 | return err; |
| 1708 | |
| 1709 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1710 | return -EINVAL; |
| 1711 | |
| 1712 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1713 | return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index, |
| 1714 | tc_index, pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1715 | pool_index, threshold, info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1718 | static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb, |
| 1719 | struct genl_info *info) |
| 1720 | { |
| 1721 | struct devlink *devlink = info->user_ptr[0]; |
| 1722 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1723 | const struct devlink_ops *ops = devlink->ops; |
| 1724 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1725 | if (ops->sb_occ_snapshot) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1726 | return ops->sb_occ_snapshot(devlink, devlink_sb->index); |
| 1727 | return -EOPNOTSUPP; |
| 1728 | } |
| 1729 | |
| 1730 | static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb, |
| 1731 | struct genl_info *info) |
| 1732 | { |
| 1733 | struct devlink *devlink = info->user_ptr[0]; |
| 1734 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1735 | const struct devlink_ops *ops = devlink->ops; |
| 1736 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1737 | if (ops->sb_occ_max_clear) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1738 | return ops->sb_occ_max_clear(devlink, devlink_sb->index); |
| 1739 | return -EOPNOTSUPP; |
| 1740 | } |
| 1741 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1742 | static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1743 | enum devlink_command cmd, u32 portid, |
| 1744 | u32 seq, int flags) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1745 | { |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1746 | const struct devlink_ops *ops = devlink->ops; |
Leon Romanovsky | 98fdbea | 2019-06-12 15:20:11 +0300 | [diff] [blame] | 1747 | enum devlink_eswitch_encap_mode encap_mode; |
| 1748 | u8 inline_mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1749 | void *hdr; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1750 | int err = 0; |
| 1751 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1752 | |
| 1753 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1754 | if (!hdr) |
| 1755 | return -EMSGSIZE; |
| 1756 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1757 | err = devlink_nl_put_handle(msg, devlink); |
| 1758 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1759 | goto nla_put_failure; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1760 | |
Jiri Pirko | 4456f61 | 2017-02-09 15:54:36 +0100 | [diff] [blame] | 1761 | if (ops->eswitch_mode_get) { |
| 1762 | err = ops->eswitch_mode_get(devlink, &mode); |
| 1763 | if (err) |
| 1764 | goto nla_put_failure; |
| 1765 | err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode); |
| 1766 | if (err) |
| 1767 | goto nla_put_failure; |
| 1768 | } |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1769 | |
| 1770 | if (ops->eswitch_inline_mode_get) { |
| 1771 | err = ops->eswitch_inline_mode_get(devlink, &inline_mode); |
| 1772 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1773 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1774 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE, |
| 1775 | inline_mode); |
| 1776 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1777 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1778 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1779 | |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1780 | if (ops->eswitch_encap_mode_get) { |
| 1781 | err = ops->eswitch_encap_mode_get(devlink, &encap_mode); |
| 1782 | if (err) |
| 1783 | goto nla_put_failure; |
| 1784 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode); |
| 1785 | if (err) |
| 1786 | goto nla_put_failure; |
| 1787 | } |
| 1788 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1789 | genlmsg_end(msg, hdr); |
| 1790 | return 0; |
| 1791 | |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1792 | nla_put_failure: |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1793 | genlmsg_cancel(msg, hdr); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1794 | return err; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1795 | } |
| 1796 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1797 | static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, |
| 1798 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1799 | { |
| 1800 | struct devlink *devlink = info->user_ptr[0]; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1801 | struct sk_buff *msg; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1802 | int err; |
| 1803 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1804 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1805 | if (!msg) |
| 1806 | return -ENOMEM; |
| 1807 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1808 | err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET, |
| 1809 | info->snd_portid, info->snd_seq, 0); |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1810 | |
| 1811 | if (err) { |
| 1812 | nlmsg_free(msg); |
| 1813 | return err; |
| 1814 | } |
| 1815 | |
| 1816 | return genlmsg_reply(msg, info); |
| 1817 | } |
| 1818 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1819 | static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, |
| 1820 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1821 | { |
| 1822 | struct devlink *devlink = info->user_ptr[0]; |
| 1823 | const struct devlink_ops *ops = devlink->ops; |
Leon Romanovsky | 98fdbea | 2019-06-12 15:20:11 +0300 | [diff] [blame] | 1824 | enum devlink_eswitch_encap_mode encap_mode; |
| 1825 | u8 inline_mode; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1826 | int err = 0; |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1827 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1828 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1829 | if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) { |
| 1830 | if (!ops->eswitch_mode_set) |
| 1831 | return -EOPNOTSUPP; |
| 1832 | mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1833 | err = ops->eswitch_mode_set(devlink, mode, info->extack); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1834 | if (err) |
| 1835 | return err; |
| 1836 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1837 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1838 | if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) { |
| 1839 | if (!ops->eswitch_inline_mode_set) |
| 1840 | return -EOPNOTSUPP; |
| 1841 | inline_mode = nla_get_u8( |
| 1842 | info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1843 | err = ops->eswitch_inline_mode_set(devlink, inline_mode, |
| 1844 | info->extack); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1845 | if (err) |
| 1846 | return err; |
| 1847 | } |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1848 | |
| 1849 | if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) { |
| 1850 | if (!ops->eswitch_encap_mode_set) |
| 1851 | return -EOPNOTSUPP; |
| 1852 | encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1853 | err = ops->eswitch_encap_mode_set(devlink, encap_mode, |
| 1854 | info->extack); |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1855 | if (err) |
| 1856 | return err; |
| 1857 | } |
| 1858 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1859 | return 0; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1860 | } |
| 1861 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1862 | int devlink_dpipe_match_put(struct sk_buff *skb, |
| 1863 | struct devlink_dpipe_match *match) |
| 1864 | { |
| 1865 | struct devlink_dpipe_header *header = match->header; |
| 1866 | struct devlink_dpipe_field *field = &header->fields[match->field_id]; |
| 1867 | struct nlattr *match_attr; |
| 1868 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1869 | match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1870 | if (!match_attr) |
| 1871 | return -EMSGSIZE; |
| 1872 | |
| 1873 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) || |
| 1874 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) || |
| 1875 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1876 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1877 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1878 | goto nla_put_failure; |
| 1879 | |
| 1880 | nla_nest_end(skb, match_attr); |
| 1881 | return 0; |
| 1882 | |
| 1883 | nla_put_failure: |
| 1884 | nla_nest_cancel(skb, match_attr); |
| 1885 | return -EMSGSIZE; |
| 1886 | } |
| 1887 | EXPORT_SYMBOL_GPL(devlink_dpipe_match_put); |
| 1888 | |
| 1889 | static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table, |
| 1890 | struct sk_buff *skb) |
| 1891 | { |
| 1892 | struct nlattr *matches_attr; |
| 1893 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1894 | matches_attr = nla_nest_start_noflag(skb, |
| 1895 | DEVLINK_ATTR_DPIPE_TABLE_MATCHES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1896 | if (!matches_attr) |
| 1897 | return -EMSGSIZE; |
| 1898 | |
| 1899 | if (table->table_ops->matches_dump(table->priv, skb)) |
| 1900 | goto nla_put_failure; |
| 1901 | |
| 1902 | nla_nest_end(skb, matches_attr); |
| 1903 | return 0; |
| 1904 | |
| 1905 | nla_put_failure: |
| 1906 | nla_nest_cancel(skb, matches_attr); |
| 1907 | return -EMSGSIZE; |
| 1908 | } |
| 1909 | |
| 1910 | int devlink_dpipe_action_put(struct sk_buff *skb, |
| 1911 | struct devlink_dpipe_action *action) |
| 1912 | { |
| 1913 | struct devlink_dpipe_header *header = action->header; |
| 1914 | struct devlink_dpipe_field *field = &header->fields[action->field_id]; |
| 1915 | struct nlattr *action_attr; |
| 1916 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1917 | action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1918 | if (!action_attr) |
| 1919 | return -EMSGSIZE; |
| 1920 | |
| 1921 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) || |
| 1922 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) || |
| 1923 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1924 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1925 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1926 | goto nla_put_failure; |
| 1927 | |
| 1928 | nla_nest_end(skb, action_attr); |
| 1929 | return 0; |
| 1930 | |
| 1931 | nla_put_failure: |
| 1932 | nla_nest_cancel(skb, action_attr); |
| 1933 | return -EMSGSIZE; |
| 1934 | } |
| 1935 | EXPORT_SYMBOL_GPL(devlink_dpipe_action_put); |
| 1936 | |
| 1937 | static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table, |
| 1938 | struct sk_buff *skb) |
| 1939 | { |
| 1940 | struct nlattr *actions_attr; |
| 1941 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1942 | actions_attr = nla_nest_start_noflag(skb, |
| 1943 | DEVLINK_ATTR_DPIPE_TABLE_ACTIONS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1944 | if (!actions_attr) |
| 1945 | return -EMSGSIZE; |
| 1946 | |
| 1947 | if (table->table_ops->actions_dump(table->priv, skb)) |
| 1948 | goto nla_put_failure; |
| 1949 | |
| 1950 | nla_nest_end(skb, actions_attr); |
| 1951 | return 0; |
| 1952 | |
| 1953 | nla_put_failure: |
| 1954 | nla_nest_cancel(skb, actions_attr); |
| 1955 | return -EMSGSIZE; |
| 1956 | } |
| 1957 | |
| 1958 | static int devlink_dpipe_table_put(struct sk_buff *skb, |
| 1959 | struct devlink_dpipe_table *table) |
| 1960 | { |
| 1961 | struct nlattr *table_attr; |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1962 | u64 table_size; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1963 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1964 | table_size = table->table_ops->size_get(table->priv); |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1965 | table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1966 | if (!table_attr) |
| 1967 | return -EMSGSIZE; |
| 1968 | |
| 1969 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) || |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1970 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1971 | DEVLINK_ATTR_PAD)) |
| 1972 | goto nla_put_failure; |
| 1973 | if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED, |
| 1974 | table->counters_enabled)) |
| 1975 | goto nla_put_failure; |
| 1976 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1977 | if (table->resource_valid) { |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 1978 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID, |
| 1979 | table->resource_id, DEVLINK_ATTR_PAD) || |
| 1980 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS, |
| 1981 | table->resource_units, DEVLINK_ATTR_PAD)) |
| 1982 | goto nla_put_failure; |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1983 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1984 | if (devlink_dpipe_matches_put(table, skb)) |
| 1985 | goto nla_put_failure; |
| 1986 | |
| 1987 | if (devlink_dpipe_actions_put(table, skb)) |
| 1988 | goto nla_put_failure; |
| 1989 | |
| 1990 | nla_nest_end(skb, table_attr); |
| 1991 | return 0; |
| 1992 | |
| 1993 | nla_put_failure: |
| 1994 | nla_nest_cancel(skb, table_attr); |
| 1995 | return -EMSGSIZE; |
| 1996 | } |
| 1997 | |
| 1998 | static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb, |
| 1999 | struct genl_info *info) |
| 2000 | { |
| 2001 | int err; |
| 2002 | |
| 2003 | if (*pskb) { |
| 2004 | err = genlmsg_reply(*pskb, info); |
| 2005 | if (err) |
| 2006 | return err; |
| 2007 | } |
| 2008 | *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 2009 | if (!*pskb) |
| 2010 | return -ENOMEM; |
| 2011 | return 0; |
| 2012 | } |
| 2013 | |
| 2014 | static int devlink_dpipe_tables_fill(struct genl_info *info, |
| 2015 | enum devlink_command cmd, int flags, |
| 2016 | struct list_head *dpipe_tables, |
| 2017 | const char *table_name) |
| 2018 | { |
| 2019 | struct devlink *devlink = info->user_ptr[0]; |
| 2020 | struct devlink_dpipe_table *table; |
| 2021 | struct nlattr *tables_attr; |
| 2022 | struct sk_buff *skb = NULL; |
| 2023 | struct nlmsghdr *nlh; |
| 2024 | bool incomplete; |
| 2025 | void *hdr; |
| 2026 | int i; |
| 2027 | int err; |
| 2028 | |
| 2029 | table = list_first_entry(dpipe_tables, |
| 2030 | struct devlink_dpipe_table, list); |
| 2031 | start_again: |
| 2032 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2033 | if (err) |
| 2034 | return err; |
| 2035 | |
| 2036 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2037 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2038 | if (!hdr) { |
| 2039 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2040 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2041 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2042 | |
| 2043 | if (devlink_nl_put_handle(skb, devlink)) |
| 2044 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2045 | tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2046 | if (!tables_attr) |
| 2047 | goto nla_put_failure; |
| 2048 | |
| 2049 | i = 0; |
| 2050 | incomplete = false; |
| 2051 | list_for_each_entry_from(table, dpipe_tables, list) { |
| 2052 | if (!table_name) { |
| 2053 | err = devlink_dpipe_table_put(skb, table); |
| 2054 | if (err) { |
| 2055 | if (!i) |
| 2056 | goto err_table_put; |
| 2057 | incomplete = true; |
| 2058 | break; |
| 2059 | } |
| 2060 | } else { |
| 2061 | if (!strcmp(table->name, table_name)) { |
| 2062 | err = devlink_dpipe_table_put(skb, table); |
| 2063 | if (err) |
| 2064 | break; |
| 2065 | } |
| 2066 | } |
| 2067 | i++; |
| 2068 | } |
| 2069 | |
| 2070 | nla_nest_end(skb, tables_attr); |
| 2071 | genlmsg_end(skb, hdr); |
| 2072 | if (incomplete) |
| 2073 | goto start_again; |
| 2074 | |
| 2075 | send_done: |
| 2076 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2077 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2078 | if (!nlh) { |
| 2079 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2080 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2081 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2082 | goto send_done; |
| 2083 | } |
| 2084 | |
| 2085 | return genlmsg_reply(skb, info); |
| 2086 | |
| 2087 | nla_put_failure: |
| 2088 | err = -EMSGSIZE; |
| 2089 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2090 | nlmsg_free(skb); |
| 2091 | return err; |
| 2092 | } |
| 2093 | |
| 2094 | static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb, |
| 2095 | struct genl_info *info) |
| 2096 | { |
| 2097 | struct devlink *devlink = info->user_ptr[0]; |
| 2098 | const char *table_name = NULL; |
| 2099 | |
| 2100 | if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 2101 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2102 | |
| 2103 | return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0, |
| 2104 | &devlink->dpipe_table_list, |
| 2105 | table_name); |
| 2106 | } |
| 2107 | |
| 2108 | static int devlink_dpipe_value_put(struct sk_buff *skb, |
| 2109 | struct devlink_dpipe_value *value) |
| 2110 | { |
| 2111 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE, |
| 2112 | value->value_size, value->value)) |
| 2113 | return -EMSGSIZE; |
| 2114 | if (value->mask) |
| 2115 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK, |
| 2116 | value->value_size, value->mask)) |
| 2117 | return -EMSGSIZE; |
| 2118 | if (value->mapping_valid) |
| 2119 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING, |
| 2120 | value->mapping_value)) |
| 2121 | return -EMSGSIZE; |
| 2122 | return 0; |
| 2123 | } |
| 2124 | |
| 2125 | static int devlink_dpipe_action_value_put(struct sk_buff *skb, |
| 2126 | struct devlink_dpipe_value *value) |
| 2127 | { |
| 2128 | if (!value->action) |
| 2129 | return -EINVAL; |
| 2130 | if (devlink_dpipe_action_put(skb, value->action)) |
| 2131 | return -EMSGSIZE; |
| 2132 | if (devlink_dpipe_value_put(skb, value)) |
| 2133 | return -EMSGSIZE; |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
| 2137 | static int devlink_dpipe_action_values_put(struct sk_buff *skb, |
| 2138 | struct devlink_dpipe_value *values, |
| 2139 | unsigned int values_count) |
| 2140 | { |
| 2141 | struct nlattr *action_attr; |
| 2142 | int i; |
| 2143 | int err; |
| 2144 | |
| 2145 | for (i = 0; i < values_count; i++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2146 | action_attr = nla_nest_start_noflag(skb, |
| 2147 | DEVLINK_ATTR_DPIPE_ACTION_VALUE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2148 | if (!action_attr) |
| 2149 | return -EMSGSIZE; |
| 2150 | err = devlink_dpipe_action_value_put(skb, &values[i]); |
| 2151 | if (err) |
| 2152 | goto err_action_value_put; |
| 2153 | nla_nest_end(skb, action_attr); |
| 2154 | } |
| 2155 | return 0; |
| 2156 | |
| 2157 | err_action_value_put: |
| 2158 | nla_nest_cancel(skb, action_attr); |
| 2159 | return err; |
| 2160 | } |
| 2161 | |
| 2162 | static int devlink_dpipe_match_value_put(struct sk_buff *skb, |
| 2163 | struct devlink_dpipe_value *value) |
| 2164 | { |
| 2165 | if (!value->match) |
| 2166 | return -EINVAL; |
| 2167 | if (devlink_dpipe_match_put(skb, value->match)) |
| 2168 | return -EMSGSIZE; |
| 2169 | if (devlink_dpipe_value_put(skb, value)) |
| 2170 | return -EMSGSIZE; |
| 2171 | return 0; |
| 2172 | } |
| 2173 | |
| 2174 | static int devlink_dpipe_match_values_put(struct sk_buff *skb, |
| 2175 | struct devlink_dpipe_value *values, |
| 2176 | unsigned int values_count) |
| 2177 | { |
| 2178 | struct nlattr *match_attr; |
| 2179 | int i; |
| 2180 | int err; |
| 2181 | |
| 2182 | for (i = 0; i < values_count; i++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2183 | match_attr = nla_nest_start_noflag(skb, |
| 2184 | DEVLINK_ATTR_DPIPE_MATCH_VALUE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2185 | if (!match_attr) |
| 2186 | return -EMSGSIZE; |
| 2187 | err = devlink_dpipe_match_value_put(skb, &values[i]); |
| 2188 | if (err) |
| 2189 | goto err_match_value_put; |
| 2190 | nla_nest_end(skb, match_attr); |
| 2191 | } |
| 2192 | return 0; |
| 2193 | |
| 2194 | err_match_value_put: |
| 2195 | nla_nest_cancel(skb, match_attr); |
| 2196 | return err; |
| 2197 | } |
| 2198 | |
| 2199 | static int devlink_dpipe_entry_put(struct sk_buff *skb, |
| 2200 | struct devlink_dpipe_entry *entry) |
| 2201 | { |
| 2202 | struct nlattr *entry_attr, *matches_attr, *actions_attr; |
| 2203 | int err; |
| 2204 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2205 | entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2206 | if (!entry_attr) |
| 2207 | return -EMSGSIZE; |
| 2208 | |
| 2209 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index, |
| 2210 | DEVLINK_ATTR_PAD)) |
| 2211 | goto nla_put_failure; |
| 2212 | if (entry->counter_valid) |
| 2213 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER, |
| 2214 | entry->counter, DEVLINK_ATTR_PAD)) |
| 2215 | goto nla_put_failure; |
| 2216 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2217 | matches_attr = nla_nest_start_noflag(skb, |
| 2218 | DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2219 | if (!matches_attr) |
| 2220 | goto nla_put_failure; |
| 2221 | |
| 2222 | err = devlink_dpipe_match_values_put(skb, entry->match_values, |
| 2223 | entry->match_values_count); |
| 2224 | if (err) { |
| 2225 | nla_nest_cancel(skb, matches_attr); |
| 2226 | goto err_match_values_put; |
| 2227 | } |
| 2228 | nla_nest_end(skb, matches_attr); |
| 2229 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2230 | actions_attr = nla_nest_start_noflag(skb, |
| 2231 | DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2232 | if (!actions_attr) |
| 2233 | goto nla_put_failure; |
| 2234 | |
| 2235 | err = devlink_dpipe_action_values_put(skb, entry->action_values, |
| 2236 | entry->action_values_count); |
| 2237 | if (err) { |
| 2238 | nla_nest_cancel(skb, actions_attr); |
| 2239 | goto err_action_values_put; |
| 2240 | } |
| 2241 | nla_nest_end(skb, actions_attr); |
| 2242 | |
| 2243 | nla_nest_end(skb, entry_attr); |
| 2244 | return 0; |
| 2245 | |
| 2246 | nla_put_failure: |
| 2247 | err = -EMSGSIZE; |
| 2248 | err_match_values_put: |
| 2249 | err_action_values_put: |
| 2250 | nla_nest_cancel(skb, entry_attr); |
| 2251 | return err; |
| 2252 | } |
| 2253 | |
| 2254 | static struct devlink_dpipe_table * |
| 2255 | devlink_dpipe_table_find(struct list_head *dpipe_tables, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 2256 | const char *table_name, struct devlink *devlink) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2257 | { |
| 2258 | struct devlink_dpipe_table *table; |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 2259 | list_for_each_entry_rcu(table, dpipe_tables, list, |
| 2260 | lockdep_is_held(&devlink->lock)) { |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2261 | if (!strcmp(table->name, table_name)) |
| 2262 | return table; |
| 2263 | } |
| 2264 | return NULL; |
| 2265 | } |
| 2266 | |
| 2267 | int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2268 | { |
| 2269 | struct devlink *devlink; |
| 2270 | int err; |
| 2271 | |
| 2272 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb, |
| 2273 | dump_ctx->info); |
| 2274 | if (err) |
| 2275 | return err; |
| 2276 | |
| 2277 | dump_ctx->hdr = genlmsg_put(dump_ctx->skb, |
| 2278 | dump_ctx->info->snd_portid, |
| 2279 | dump_ctx->info->snd_seq, |
| 2280 | &devlink_nl_family, NLM_F_MULTI, |
| 2281 | dump_ctx->cmd); |
| 2282 | if (!dump_ctx->hdr) |
| 2283 | goto nla_put_failure; |
| 2284 | |
| 2285 | devlink = dump_ctx->info->user_ptr[0]; |
| 2286 | if (devlink_nl_put_handle(dump_ctx->skb, devlink)) |
| 2287 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2288 | dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb, |
| 2289 | DEVLINK_ATTR_DPIPE_ENTRIES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2290 | if (!dump_ctx->nest) |
| 2291 | goto nla_put_failure; |
| 2292 | return 0; |
| 2293 | |
| 2294 | nla_put_failure: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2295 | nlmsg_free(dump_ctx->skb); |
| 2296 | return -EMSGSIZE; |
| 2297 | } |
| 2298 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare); |
| 2299 | |
| 2300 | int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, |
| 2301 | struct devlink_dpipe_entry *entry) |
| 2302 | { |
| 2303 | return devlink_dpipe_entry_put(dump_ctx->skb, entry); |
| 2304 | } |
| 2305 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append); |
| 2306 | |
| 2307 | int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2308 | { |
| 2309 | nla_nest_end(dump_ctx->skb, dump_ctx->nest); |
| 2310 | genlmsg_end(dump_ctx->skb, dump_ctx->hdr); |
| 2311 | return 0; |
| 2312 | } |
| 2313 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close); |
| 2314 | |
Arkadi Sharshevsky | 3580732 | 2017-08-24 08:40:03 +0200 | [diff] [blame] | 2315 | void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry) |
| 2316 | |
| 2317 | { |
| 2318 | unsigned int value_count, value_index; |
| 2319 | struct devlink_dpipe_value *value; |
| 2320 | |
| 2321 | value = entry->action_values; |
| 2322 | value_count = entry->action_values_count; |
| 2323 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2324 | kfree(value[value_index].value); |
| 2325 | kfree(value[value_index].mask); |
| 2326 | } |
| 2327 | |
| 2328 | value = entry->match_values; |
| 2329 | value_count = entry->match_values_count; |
| 2330 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2331 | kfree(value[value_index].value); |
| 2332 | kfree(value[value_index].mask); |
| 2333 | } |
| 2334 | } |
| 2335 | EXPORT_SYMBOL(devlink_dpipe_entry_clear); |
| 2336 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2337 | static int devlink_dpipe_entries_fill(struct genl_info *info, |
| 2338 | enum devlink_command cmd, int flags, |
| 2339 | struct devlink_dpipe_table *table) |
| 2340 | { |
| 2341 | struct devlink_dpipe_dump_ctx dump_ctx; |
| 2342 | struct nlmsghdr *nlh; |
| 2343 | int err; |
| 2344 | |
| 2345 | dump_ctx.skb = NULL; |
| 2346 | dump_ctx.cmd = cmd; |
| 2347 | dump_ctx.info = info; |
| 2348 | |
| 2349 | err = table->table_ops->entries_dump(table->priv, |
| 2350 | table->counters_enabled, |
| 2351 | &dump_ctx); |
| 2352 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2353 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2354 | |
| 2355 | send_done: |
| 2356 | nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq, |
| 2357 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2358 | if (!nlh) { |
| 2359 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info); |
| 2360 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2361 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2362 | goto send_done; |
| 2363 | } |
| 2364 | return genlmsg_reply(dump_ctx.skb, info); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb, |
| 2368 | struct genl_info *info) |
| 2369 | { |
| 2370 | struct devlink *devlink = info->user_ptr[0]; |
| 2371 | struct devlink_dpipe_table *table; |
| 2372 | const char *table_name; |
| 2373 | |
| 2374 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 2375 | return -EINVAL; |
| 2376 | |
| 2377 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2378 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 2379 | table_name, devlink); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2380 | if (!table) |
| 2381 | return -EINVAL; |
| 2382 | |
| 2383 | if (!table->table_ops->entries_dump) |
| 2384 | return -EINVAL; |
| 2385 | |
| 2386 | return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET, |
| 2387 | 0, table); |
| 2388 | } |
| 2389 | |
| 2390 | static int devlink_dpipe_fields_put(struct sk_buff *skb, |
| 2391 | const struct devlink_dpipe_header *header) |
| 2392 | { |
| 2393 | struct devlink_dpipe_field *field; |
| 2394 | struct nlattr *field_attr; |
| 2395 | int i; |
| 2396 | |
| 2397 | for (i = 0; i < header->fields_count; i++) { |
| 2398 | field = &header->fields[i]; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2399 | field_attr = nla_nest_start_noflag(skb, |
| 2400 | DEVLINK_ATTR_DPIPE_FIELD); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2401 | if (!field_attr) |
| 2402 | return -EMSGSIZE; |
| 2403 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) || |
| 2404 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 2405 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) || |
| 2406 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type)) |
| 2407 | goto nla_put_failure; |
| 2408 | nla_nest_end(skb, field_attr); |
| 2409 | } |
| 2410 | return 0; |
| 2411 | |
| 2412 | nla_put_failure: |
| 2413 | nla_nest_cancel(skb, field_attr); |
| 2414 | return -EMSGSIZE; |
| 2415 | } |
| 2416 | |
| 2417 | static int devlink_dpipe_header_put(struct sk_buff *skb, |
| 2418 | struct devlink_dpipe_header *header) |
| 2419 | { |
| 2420 | struct nlattr *fields_attr, *header_attr; |
| 2421 | int err; |
| 2422 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2423 | header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER); |
Wei Yongjun | cb6bf9c | 2017-04-11 16:02:02 +0000 | [diff] [blame] | 2424 | if (!header_attr) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2425 | return -EMSGSIZE; |
| 2426 | |
| 2427 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) || |
| 2428 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 2429 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 2430 | goto nla_put_failure; |
| 2431 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2432 | fields_attr = nla_nest_start_noflag(skb, |
| 2433 | DEVLINK_ATTR_DPIPE_HEADER_FIELDS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2434 | if (!fields_attr) |
| 2435 | goto nla_put_failure; |
| 2436 | |
| 2437 | err = devlink_dpipe_fields_put(skb, header); |
| 2438 | if (err) { |
| 2439 | nla_nest_cancel(skb, fields_attr); |
| 2440 | goto nla_put_failure; |
| 2441 | } |
| 2442 | nla_nest_end(skb, fields_attr); |
| 2443 | nla_nest_end(skb, header_attr); |
| 2444 | return 0; |
| 2445 | |
| 2446 | nla_put_failure: |
| 2447 | err = -EMSGSIZE; |
| 2448 | nla_nest_cancel(skb, header_attr); |
| 2449 | return err; |
| 2450 | } |
| 2451 | |
| 2452 | static int devlink_dpipe_headers_fill(struct genl_info *info, |
| 2453 | enum devlink_command cmd, int flags, |
| 2454 | struct devlink_dpipe_headers * |
| 2455 | dpipe_headers) |
| 2456 | { |
| 2457 | struct devlink *devlink = info->user_ptr[0]; |
| 2458 | struct nlattr *headers_attr; |
| 2459 | struct sk_buff *skb = NULL; |
| 2460 | struct nlmsghdr *nlh; |
| 2461 | void *hdr; |
| 2462 | int i, j; |
| 2463 | int err; |
| 2464 | |
| 2465 | i = 0; |
| 2466 | start_again: |
| 2467 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2468 | if (err) |
| 2469 | return err; |
| 2470 | |
| 2471 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2472 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2473 | if (!hdr) { |
| 2474 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2475 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2476 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2477 | |
| 2478 | if (devlink_nl_put_handle(skb, devlink)) |
| 2479 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2480 | headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2481 | if (!headers_attr) |
| 2482 | goto nla_put_failure; |
| 2483 | |
| 2484 | j = 0; |
| 2485 | for (; i < dpipe_headers->headers_count; i++) { |
| 2486 | err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]); |
| 2487 | if (err) { |
| 2488 | if (!j) |
| 2489 | goto err_table_put; |
| 2490 | break; |
| 2491 | } |
| 2492 | j++; |
| 2493 | } |
| 2494 | nla_nest_end(skb, headers_attr); |
| 2495 | genlmsg_end(skb, hdr); |
| 2496 | if (i != dpipe_headers->headers_count) |
| 2497 | goto start_again; |
| 2498 | |
| 2499 | send_done: |
| 2500 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2501 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2502 | if (!nlh) { |
| 2503 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2504 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2505 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2506 | goto send_done; |
| 2507 | } |
| 2508 | return genlmsg_reply(skb, info); |
| 2509 | |
| 2510 | nla_put_failure: |
| 2511 | err = -EMSGSIZE; |
| 2512 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2513 | nlmsg_free(skb); |
| 2514 | return err; |
| 2515 | } |
| 2516 | |
| 2517 | static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb, |
| 2518 | struct genl_info *info) |
| 2519 | { |
| 2520 | struct devlink *devlink = info->user_ptr[0]; |
| 2521 | |
| 2522 | if (!devlink->dpipe_headers) |
| 2523 | return -EOPNOTSUPP; |
| 2524 | return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET, |
| 2525 | 0, devlink->dpipe_headers); |
| 2526 | } |
| 2527 | |
| 2528 | static int devlink_dpipe_table_counters_set(struct devlink *devlink, |
| 2529 | const char *table_name, |
| 2530 | bool enable) |
| 2531 | { |
| 2532 | struct devlink_dpipe_table *table; |
| 2533 | |
| 2534 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 2535 | table_name, devlink); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2536 | if (!table) |
| 2537 | return -EINVAL; |
| 2538 | |
| 2539 | if (table->counter_control_extern) |
| 2540 | return -EOPNOTSUPP; |
| 2541 | |
| 2542 | if (!(table->counters_enabled ^ enable)) |
| 2543 | return 0; |
| 2544 | |
| 2545 | table->counters_enabled = enable; |
| 2546 | if (table->table_ops->counters_set_update) |
| 2547 | table->table_ops->counters_set_update(table->priv, enable); |
| 2548 | return 0; |
| 2549 | } |
| 2550 | |
| 2551 | static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb, |
| 2552 | struct genl_info *info) |
| 2553 | { |
| 2554 | struct devlink *devlink = info->user_ptr[0]; |
| 2555 | const char *table_name; |
| 2556 | bool counters_enable; |
| 2557 | |
| 2558 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] || |
| 2559 | !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) |
| 2560 | return -EINVAL; |
| 2561 | |
| 2562 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2563 | counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]); |
| 2564 | |
| 2565 | return devlink_dpipe_table_counters_set(devlink, table_name, |
| 2566 | counters_enable); |
| 2567 | } |
| 2568 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2569 | static struct devlink_resource * |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2570 | devlink_resource_find(struct devlink *devlink, |
| 2571 | struct devlink_resource *resource, u64 resource_id) |
| 2572 | { |
| 2573 | struct list_head *resource_list; |
| 2574 | |
| 2575 | if (resource) |
| 2576 | resource_list = &resource->resource_list; |
| 2577 | else |
| 2578 | resource_list = &devlink->resource_list; |
| 2579 | |
| 2580 | list_for_each_entry(resource, resource_list, list) { |
| 2581 | struct devlink_resource *child_resource; |
| 2582 | |
| 2583 | if (resource->id == resource_id) |
| 2584 | return resource; |
| 2585 | |
| 2586 | child_resource = devlink_resource_find(devlink, resource, |
| 2587 | resource_id); |
| 2588 | if (child_resource) |
| 2589 | return child_resource; |
| 2590 | } |
| 2591 | return NULL; |
| 2592 | } |
| 2593 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2594 | static void |
| 2595 | devlink_resource_validate_children(struct devlink_resource *resource) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2596 | { |
| 2597 | struct devlink_resource *child_resource; |
| 2598 | bool size_valid = true; |
| 2599 | u64 parts_size = 0; |
| 2600 | |
| 2601 | if (list_empty(&resource->resource_list)) |
| 2602 | goto out; |
| 2603 | |
| 2604 | list_for_each_entry(child_resource, &resource->resource_list, list) |
| 2605 | parts_size += child_resource->size_new; |
| 2606 | |
Arkadi Sharshevsky | b9d1717 | 2018-02-26 10:59:53 +0100 | [diff] [blame] | 2607 | if (parts_size > resource->size_new) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2608 | size_valid = false; |
| 2609 | out: |
| 2610 | resource->size_valid = size_valid; |
| 2611 | } |
| 2612 | |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2613 | static int |
| 2614 | devlink_resource_validate_size(struct devlink_resource *resource, u64 size, |
| 2615 | struct netlink_ext_ack *extack) |
| 2616 | { |
| 2617 | u64 reminder; |
| 2618 | int err = 0; |
| 2619 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2620 | if (size > resource->size_params.size_max) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2621 | NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum"); |
| 2622 | err = -EINVAL; |
| 2623 | } |
| 2624 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2625 | if (size < resource->size_params.size_min) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2626 | NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum"); |
| 2627 | err = -EINVAL; |
| 2628 | } |
| 2629 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2630 | div64_u64_rem(size, resource->size_params.size_granularity, &reminder); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2631 | if (reminder) { |
| 2632 | NL_SET_ERR_MSG_MOD(extack, "Wrong granularity"); |
| 2633 | err = -EINVAL; |
| 2634 | } |
| 2635 | |
| 2636 | return err; |
| 2637 | } |
| 2638 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2639 | static int devlink_nl_cmd_resource_set(struct sk_buff *skb, |
| 2640 | struct genl_info *info) |
| 2641 | { |
| 2642 | struct devlink *devlink = info->user_ptr[0]; |
| 2643 | struct devlink_resource *resource; |
| 2644 | u64 resource_id; |
| 2645 | u64 size; |
| 2646 | int err; |
| 2647 | |
| 2648 | if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] || |
| 2649 | !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]) |
| 2650 | return -EINVAL; |
| 2651 | resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]); |
| 2652 | |
| 2653 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 2654 | if (!resource) |
| 2655 | return -EINVAL; |
| 2656 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2657 | size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2658 | err = devlink_resource_validate_size(resource, size, info->extack); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2659 | if (err) |
| 2660 | return err; |
| 2661 | |
| 2662 | resource->size_new = size; |
| 2663 | devlink_resource_validate_children(resource); |
| 2664 | if (resource->parent) |
| 2665 | devlink_resource_validate_children(resource->parent); |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2669 | static int |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2670 | devlink_resource_size_params_put(struct devlink_resource *resource, |
| 2671 | struct sk_buff *skb) |
| 2672 | { |
| 2673 | struct devlink_resource_size_params *size_params; |
| 2674 | |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 2675 | size_params = &resource->size_params; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2676 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN, |
| 2677 | size_params->size_granularity, DEVLINK_ATTR_PAD) || |
| 2678 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX, |
| 2679 | size_params->size_max, DEVLINK_ATTR_PAD) || |
| 2680 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN, |
| 2681 | size_params->size_min, DEVLINK_ATTR_PAD) || |
| 2682 | nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit)) |
| 2683 | return -EMSGSIZE; |
| 2684 | return 0; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2685 | } |
| 2686 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2687 | static int devlink_resource_occ_put(struct devlink_resource *resource, |
| 2688 | struct sk_buff *skb) |
| 2689 | { |
| 2690 | if (!resource->occ_get) |
| 2691 | return 0; |
| 2692 | return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC, |
| 2693 | resource->occ_get(resource->occ_get_priv), |
| 2694 | DEVLINK_ATTR_PAD); |
| 2695 | } |
| 2696 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2697 | static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb, |
| 2698 | struct devlink_resource *resource) |
| 2699 | { |
| 2700 | struct devlink_resource *child_resource; |
| 2701 | struct nlattr *child_resource_attr; |
| 2702 | struct nlattr *resource_attr; |
| 2703 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2704 | resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2705 | if (!resource_attr) |
| 2706 | return -EMSGSIZE; |
| 2707 | |
| 2708 | if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) || |
| 2709 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size, |
| 2710 | DEVLINK_ATTR_PAD) || |
| 2711 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id, |
| 2712 | DEVLINK_ATTR_PAD)) |
| 2713 | goto nla_put_failure; |
| 2714 | if (resource->size != resource->size_new) |
| 2715 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW, |
| 2716 | resource->size_new, DEVLINK_ATTR_PAD); |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2717 | if (devlink_resource_occ_put(resource, skb)) |
| 2718 | goto nla_put_failure; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2719 | if (devlink_resource_size_params_put(resource, skb)) |
| 2720 | goto nla_put_failure; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2721 | if (list_empty(&resource->resource_list)) |
| 2722 | goto out; |
| 2723 | |
| 2724 | if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID, |
| 2725 | resource->size_valid)) |
| 2726 | goto nla_put_failure; |
| 2727 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2728 | child_resource_attr = nla_nest_start_noflag(skb, |
| 2729 | DEVLINK_ATTR_RESOURCE_LIST); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2730 | if (!child_resource_attr) |
| 2731 | goto nla_put_failure; |
| 2732 | |
| 2733 | list_for_each_entry(child_resource, &resource->resource_list, list) { |
| 2734 | if (devlink_resource_put(devlink, skb, child_resource)) |
| 2735 | goto resource_put_failure; |
| 2736 | } |
| 2737 | |
| 2738 | nla_nest_end(skb, child_resource_attr); |
| 2739 | out: |
| 2740 | nla_nest_end(skb, resource_attr); |
| 2741 | return 0; |
| 2742 | |
| 2743 | resource_put_failure: |
| 2744 | nla_nest_cancel(skb, child_resource_attr); |
| 2745 | nla_put_failure: |
| 2746 | nla_nest_cancel(skb, resource_attr); |
| 2747 | return -EMSGSIZE; |
| 2748 | } |
| 2749 | |
| 2750 | static int devlink_resource_fill(struct genl_info *info, |
| 2751 | enum devlink_command cmd, int flags) |
| 2752 | { |
| 2753 | struct devlink *devlink = info->user_ptr[0]; |
| 2754 | struct devlink_resource *resource; |
| 2755 | struct nlattr *resources_attr; |
| 2756 | struct sk_buff *skb = NULL; |
| 2757 | struct nlmsghdr *nlh; |
| 2758 | bool incomplete; |
| 2759 | void *hdr; |
| 2760 | int i; |
| 2761 | int err; |
| 2762 | |
| 2763 | resource = list_first_entry(&devlink->resource_list, |
| 2764 | struct devlink_resource, list); |
| 2765 | start_again: |
| 2766 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2767 | if (err) |
| 2768 | return err; |
| 2769 | |
| 2770 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2771 | &devlink_nl_family, NLM_F_MULTI, cmd); |
| 2772 | if (!hdr) { |
| 2773 | nlmsg_free(skb); |
| 2774 | return -EMSGSIZE; |
| 2775 | } |
| 2776 | |
| 2777 | if (devlink_nl_put_handle(skb, devlink)) |
| 2778 | goto nla_put_failure; |
| 2779 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2780 | resources_attr = nla_nest_start_noflag(skb, |
| 2781 | DEVLINK_ATTR_RESOURCE_LIST); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2782 | if (!resources_attr) |
| 2783 | goto nla_put_failure; |
| 2784 | |
| 2785 | incomplete = false; |
| 2786 | i = 0; |
| 2787 | list_for_each_entry_from(resource, &devlink->resource_list, list) { |
| 2788 | err = devlink_resource_put(devlink, skb, resource); |
| 2789 | if (err) { |
| 2790 | if (!i) |
| 2791 | goto err_resource_put; |
| 2792 | incomplete = true; |
| 2793 | break; |
| 2794 | } |
| 2795 | i++; |
| 2796 | } |
| 2797 | nla_nest_end(skb, resources_attr); |
| 2798 | genlmsg_end(skb, hdr); |
| 2799 | if (incomplete) |
| 2800 | goto start_again; |
| 2801 | send_done: |
| 2802 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2803 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2804 | if (!nlh) { |
| 2805 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2806 | if (err) |
Dan Carpenter | 83fe9a9 | 2018-09-21 11:07:55 +0300 | [diff] [blame] | 2807 | return err; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2808 | goto send_done; |
| 2809 | } |
| 2810 | return genlmsg_reply(skb, info); |
| 2811 | |
| 2812 | nla_put_failure: |
| 2813 | err = -EMSGSIZE; |
| 2814 | err_resource_put: |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2815 | nlmsg_free(skb); |
| 2816 | return err; |
| 2817 | } |
| 2818 | |
| 2819 | static int devlink_nl_cmd_resource_dump(struct sk_buff *skb, |
| 2820 | struct genl_info *info) |
| 2821 | { |
| 2822 | struct devlink *devlink = info->user_ptr[0]; |
| 2823 | |
| 2824 | if (list_empty(&devlink->resource_list)) |
| 2825 | return -EOPNOTSUPP; |
| 2826 | |
| 2827 | return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0); |
| 2828 | } |
| 2829 | |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2830 | static int |
| 2831 | devlink_resources_validate(struct devlink *devlink, |
| 2832 | struct devlink_resource *resource, |
| 2833 | struct genl_info *info) |
| 2834 | { |
| 2835 | struct list_head *resource_list; |
| 2836 | int err = 0; |
| 2837 | |
| 2838 | if (resource) |
| 2839 | resource_list = &resource->resource_list; |
| 2840 | else |
| 2841 | resource_list = &devlink->resource_list; |
| 2842 | |
| 2843 | list_for_each_entry(resource, resource_list, list) { |
| 2844 | if (!resource->size_valid) |
| 2845 | return -EINVAL; |
| 2846 | err = devlink_resources_validate(devlink, resource, info); |
| 2847 | if (err) |
| 2848 | return err; |
| 2849 | } |
| 2850 | return err; |
| 2851 | } |
| 2852 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2853 | static struct net *devlink_netns_get(struct sk_buff *skb, |
| 2854 | struct genl_info *info) |
| 2855 | { |
| 2856 | struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID]; |
| 2857 | struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD]; |
| 2858 | struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID]; |
| 2859 | struct net *net; |
| 2860 | |
| 2861 | if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) { |
Jiri Pirko | 054eae8 | 2020-03-28 19:25:29 +0100 | [diff] [blame] | 2862 | NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified"); |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2863 | return ERR_PTR(-EINVAL); |
| 2864 | } |
| 2865 | |
| 2866 | if (netns_pid_attr) { |
| 2867 | net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr)); |
| 2868 | } else if (netns_fd_attr) { |
| 2869 | net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr)); |
| 2870 | } else if (netns_id_attr) { |
| 2871 | net = get_net_ns_by_id(sock_net(skb->sk), |
| 2872 | nla_get_u32(netns_id_attr)); |
| 2873 | if (!net) |
| 2874 | net = ERR_PTR(-EINVAL); |
| 2875 | } else { |
| 2876 | WARN_ON(1); |
| 2877 | net = ERR_PTR(-EINVAL); |
| 2878 | } |
| 2879 | if (IS_ERR(net)) { |
Jiri Pirko | 054eae8 | 2020-03-28 19:25:29 +0100 | [diff] [blame] | 2880 | NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace"); |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2881 | return ERR_PTR(-EINVAL); |
| 2882 | } |
| 2883 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
| 2884 | put_net(net); |
| 2885 | return ERR_PTR(-EPERM); |
| 2886 | } |
| 2887 | return net; |
| 2888 | } |
| 2889 | |
| 2890 | static void devlink_param_notify(struct devlink *devlink, |
| 2891 | unsigned int port_index, |
| 2892 | struct devlink_param_item *param_item, |
| 2893 | enum devlink_command cmd); |
| 2894 | |
| 2895 | static void devlink_reload_netns_change(struct devlink *devlink, |
| 2896 | struct net *dest_net) |
| 2897 | { |
| 2898 | struct devlink_param_item *param_item; |
| 2899 | |
| 2900 | /* Userspace needs to be notified about devlink objects |
| 2901 | * removed from original and entering new network namespace. |
| 2902 | * The rest of the devlink objects are re-created during |
| 2903 | * reload process so the notifications are generated separatelly. |
| 2904 | */ |
| 2905 | |
| 2906 | list_for_each_entry(param_item, &devlink->param_list, list) |
| 2907 | devlink_param_notify(devlink, 0, param_item, |
| 2908 | DEVLINK_CMD_PARAM_DEL); |
| 2909 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 2910 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 2911 | __devlink_net_set(devlink, dest_net); |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2912 | |
| 2913 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 2914 | list_for_each_entry(param_item, &devlink->param_list, list) |
| 2915 | devlink_param_notify(devlink, 0, param_item, |
| 2916 | DEVLINK_CMD_PARAM_NEW); |
| 2917 | } |
| 2918 | |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 2919 | static bool devlink_reload_supported(struct devlink *devlink) |
| 2920 | { |
| 2921 | return devlink->ops->reload_down && devlink->ops->reload_up; |
| 2922 | } |
| 2923 | |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 2924 | static void devlink_reload_failed_set(struct devlink *devlink, |
| 2925 | bool reload_failed) |
| 2926 | { |
| 2927 | if (devlink->reload_failed == reload_failed) |
| 2928 | return; |
| 2929 | devlink->reload_failed = reload_failed; |
| 2930 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 2931 | } |
| 2932 | |
| 2933 | bool devlink_is_reload_failed(const struct devlink *devlink) |
| 2934 | { |
| 2935 | return devlink->reload_failed; |
| 2936 | } |
| 2937 | EXPORT_SYMBOL_GPL(devlink_is_reload_failed); |
| 2938 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2939 | static int devlink_reload(struct devlink *devlink, struct net *dest_net, |
| 2940 | struct netlink_ext_ack *extack) |
| 2941 | { |
| 2942 | int err; |
| 2943 | |
Jiri Pirko | a0c7634 | 2019-11-08 21:42:43 +0100 | [diff] [blame] | 2944 | if (!devlink->reload_enabled) |
| 2945 | return -EOPNOTSUPP; |
| 2946 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2947 | err = devlink->ops->reload_down(devlink, !!dest_net, extack); |
| 2948 | if (err) |
| 2949 | return err; |
| 2950 | |
| 2951 | if (dest_net && !net_eq(dest_net, devlink_net(devlink))) |
| 2952 | devlink_reload_netns_change(devlink, dest_net); |
| 2953 | |
| 2954 | err = devlink->ops->reload_up(devlink, extack); |
| 2955 | devlink_reload_failed_set(devlink, !!err); |
| 2956 | return err; |
| 2957 | } |
| 2958 | |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2959 | static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info) |
| 2960 | { |
| 2961 | struct devlink *devlink = info->user_ptr[0]; |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2962 | struct net *dest_net = NULL; |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2963 | int err; |
| 2964 | |
Jiri Pirko | 5a508a2 | 2019-11-09 11:29:46 +0100 | [diff] [blame] | 2965 | if (!devlink_reload_supported(devlink) || !devlink->reload_enabled) |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2966 | return -EOPNOTSUPP; |
| 2967 | |
| 2968 | err = devlink_resources_validate(devlink, NULL, info); |
| 2969 | if (err) { |
| 2970 | NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed"); |
| 2971 | return err; |
| 2972 | } |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2973 | |
| 2974 | if (info->attrs[DEVLINK_ATTR_NETNS_PID] || |
| 2975 | info->attrs[DEVLINK_ATTR_NETNS_FD] || |
| 2976 | info->attrs[DEVLINK_ATTR_NETNS_ID]) { |
| 2977 | dest_net = devlink_netns_get(skb, info); |
| 2978 | if (IS_ERR(dest_net)) |
| 2979 | return PTR_ERR(dest_net); |
| 2980 | } |
| 2981 | |
| 2982 | err = devlink_reload(devlink, dest_net, info->extack); |
| 2983 | |
| 2984 | if (dest_net) |
| 2985 | put_net(dest_net); |
| 2986 | |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 2987 | return err; |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2988 | } |
| 2989 | |
Jiri Pirko | 191ed20 | 2019-06-04 15:40:40 +0200 | [diff] [blame] | 2990 | static int devlink_nl_flash_update_fill(struct sk_buff *msg, |
| 2991 | struct devlink *devlink, |
| 2992 | enum devlink_command cmd, |
| 2993 | const char *status_msg, |
| 2994 | const char *component, |
| 2995 | unsigned long done, unsigned long total) |
| 2996 | { |
| 2997 | void *hdr; |
| 2998 | |
| 2999 | hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd); |
| 3000 | if (!hdr) |
| 3001 | return -EMSGSIZE; |
| 3002 | |
| 3003 | if (devlink_nl_put_handle(msg, devlink)) |
| 3004 | goto nla_put_failure; |
| 3005 | |
| 3006 | if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS) |
| 3007 | goto out; |
| 3008 | |
| 3009 | if (status_msg && |
| 3010 | nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG, |
| 3011 | status_msg)) |
| 3012 | goto nla_put_failure; |
| 3013 | if (component && |
| 3014 | nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT, |
| 3015 | component)) |
| 3016 | goto nla_put_failure; |
| 3017 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE, |
| 3018 | done, DEVLINK_ATTR_PAD)) |
| 3019 | goto nla_put_failure; |
| 3020 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL, |
| 3021 | total, DEVLINK_ATTR_PAD)) |
| 3022 | goto nla_put_failure; |
| 3023 | |
| 3024 | out: |
| 3025 | genlmsg_end(msg, hdr); |
| 3026 | return 0; |
| 3027 | |
| 3028 | nla_put_failure: |
| 3029 | genlmsg_cancel(msg, hdr); |
| 3030 | return -EMSGSIZE; |
| 3031 | } |
| 3032 | |
| 3033 | static void __devlink_flash_update_notify(struct devlink *devlink, |
| 3034 | enum devlink_command cmd, |
| 3035 | const char *status_msg, |
| 3036 | const char *component, |
| 3037 | unsigned long done, |
| 3038 | unsigned long total) |
| 3039 | { |
| 3040 | struct sk_buff *msg; |
| 3041 | int err; |
| 3042 | |
| 3043 | WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE && |
| 3044 | cmd != DEVLINK_CMD_FLASH_UPDATE_END && |
| 3045 | cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); |
| 3046 | |
| 3047 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3048 | if (!msg) |
| 3049 | return; |
| 3050 | |
| 3051 | err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg, |
| 3052 | component, done, total); |
| 3053 | if (err) |
| 3054 | goto out_free_msg; |
| 3055 | |
| 3056 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3057 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 3058 | return; |
| 3059 | |
| 3060 | out_free_msg: |
| 3061 | nlmsg_free(msg); |
| 3062 | } |
| 3063 | |
| 3064 | void devlink_flash_update_begin_notify(struct devlink *devlink) |
| 3065 | { |
| 3066 | __devlink_flash_update_notify(devlink, |
| 3067 | DEVLINK_CMD_FLASH_UPDATE, |
| 3068 | NULL, NULL, 0, 0); |
| 3069 | } |
| 3070 | EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify); |
| 3071 | |
| 3072 | void devlink_flash_update_end_notify(struct devlink *devlink) |
| 3073 | { |
| 3074 | __devlink_flash_update_notify(devlink, |
| 3075 | DEVLINK_CMD_FLASH_UPDATE_END, |
| 3076 | NULL, NULL, 0, 0); |
| 3077 | } |
| 3078 | EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify); |
| 3079 | |
| 3080 | void devlink_flash_update_status_notify(struct devlink *devlink, |
| 3081 | const char *status_msg, |
| 3082 | const char *component, |
| 3083 | unsigned long done, |
| 3084 | unsigned long total) |
| 3085 | { |
| 3086 | __devlink_flash_update_notify(devlink, |
| 3087 | DEVLINK_CMD_FLASH_UPDATE_STATUS, |
| 3088 | status_msg, component, done, total); |
| 3089 | } |
| 3090 | EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify); |
| 3091 | |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 3092 | static int devlink_nl_cmd_flash_update(struct sk_buff *skb, |
| 3093 | struct genl_info *info) |
| 3094 | { |
| 3095 | struct devlink *devlink = info->user_ptr[0]; |
| 3096 | const char *file_name, *component; |
| 3097 | struct nlattr *nla_component; |
| 3098 | |
| 3099 | if (!devlink->ops->flash_update) |
| 3100 | return -EOPNOTSUPP; |
| 3101 | |
| 3102 | if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]) |
| 3103 | return -EINVAL; |
| 3104 | file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]); |
| 3105 | |
| 3106 | nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]; |
| 3107 | component = nla_component ? nla_data(nla_component) : NULL; |
| 3108 | |
| 3109 | return devlink->ops->flash_update(devlink, file_name, component, |
| 3110 | info->extack); |
| 3111 | } |
| 3112 | |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame] | 3113 | static const struct devlink_param devlink_param_generic[] = { |
| 3114 | { |
| 3115 | .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET, |
| 3116 | .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME, |
| 3117 | .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE, |
| 3118 | }, |
| 3119 | { |
| 3120 | .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS, |
| 3121 | .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME, |
| 3122 | .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE, |
| 3123 | }, |
Vasundhara Volam | f567bcd | 2018-07-04 14:30:36 +0300 | [diff] [blame] | 3124 | { |
| 3125 | .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, |
| 3126 | .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME, |
| 3127 | .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE, |
| 3128 | }, |
Alex Vesker | f6a69885 | 2018-07-12 15:13:17 +0300 | [diff] [blame] | 3129 | { |
| 3130 | .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT, |
| 3131 | .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME, |
| 3132 | .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE, |
| 3133 | }, |
Vasundhara Volam | e3b5106 | 2018-10-04 11:13:44 +0530 | [diff] [blame] | 3134 | { |
| 3135 | .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI, |
| 3136 | .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME, |
| 3137 | .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE, |
| 3138 | }, |
Vasundhara Volam | f61cba4 | 2018-10-04 11:13:45 +0530 | [diff] [blame] | 3139 | { |
| 3140 | .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX, |
| 3141 | .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME, |
| 3142 | .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE, |
| 3143 | }, |
Vasundhara Volam | 1651178 | 2018-10-04 11:13:46 +0530 | [diff] [blame] | 3144 | { |
| 3145 | .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN, |
| 3146 | .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME, |
| 3147 | .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE, |
| 3148 | }, |
Shalom Toledo | 846e980 | 2018-12-03 07:58:59 +0000 | [diff] [blame] | 3149 | { |
| 3150 | .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY, |
| 3151 | .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME, |
| 3152 | .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE, |
| 3153 | }, |
Dirk van der Merwe | 5bbd21d | 2019-09-09 00:54:18 +0100 | [diff] [blame] | 3154 | { |
| 3155 | .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE, |
| 3156 | .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME, |
| 3157 | .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE, |
| 3158 | }, |
Michael Guralnik | 6c7295e | 2019-11-08 23:45:20 +0000 | [diff] [blame] | 3159 | { |
| 3160 | .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE, |
| 3161 | .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME, |
| 3162 | .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE, |
| 3163 | }, |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame] | 3164 | }; |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3165 | |
| 3166 | static int devlink_param_generic_verify(const struct devlink_param *param) |
| 3167 | { |
| 3168 | /* verify it match generic parameter by id and name */ |
| 3169 | if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX) |
| 3170 | return -EINVAL; |
| 3171 | if (strcmp(param->name, devlink_param_generic[param->id].name)) |
| 3172 | return -ENOENT; |
| 3173 | |
| 3174 | WARN_ON(param->type != devlink_param_generic[param->id].type); |
| 3175 | |
| 3176 | return 0; |
| 3177 | } |
| 3178 | |
| 3179 | static int devlink_param_driver_verify(const struct devlink_param *param) |
| 3180 | { |
| 3181 | int i; |
| 3182 | |
| 3183 | if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX) |
| 3184 | return -EINVAL; |
| 3185 | /* verify no such name in generic params */ |
| 3186 | for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++) |
| 3187 | if (!strcmp(param->name, devlink_param_generic[i].name)) |
| 3188 | return -EEXIST; |
| 3189 | |
| 3190 | return 0; |
| 3191 | } |
| 3192 | |
| 3193 | static struct devlink_param_item * |
| 3194 | devlink_param_find_by_name(struct list_head *param_list, |
| 3195 | const char *param_name) |
| 3196 | { |
| 3197 | struct devlink_param_item *param_item; |
| 3198 | |
| 3199 | list_for_each_entry(param_item, param_list, list) |
| 3200 | if (!strcmp(param_item->param->name, param_name)) |
| 3201 | return param_item; |
| 3202 | return NULL; |
| 3203 | } |
| 3204 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 3205 | static struct devlink_param_item * |
| 3206 | devlink_param_find_by_id(struct list_head *param_list, u32 param_id) |
| 3207 | { |
| 3208 | struct devlink_param_item *param_item; |
| 3209 | |
| 3210 | list_for_each_entry(param_item, param_list, list) |
| 3211 | if (param_item->param->id == param_id) |
| 3212 | return param_item; |
| 3213 | return NULL; |
| 3214 | } |
| 3215 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3216 | static bool |
| 3217 | devlink_param_cmode_is_supported(const struct devlink_param *param, |
| 3218 | enum devlink_param_cmode cmode) |
| 3219 | { |
| 3220 | return test_bit(cmode, ¶m->supported_cmodes); |
| 3221 | } |
| 3222 | |
| 3223 | static int devlink_param_get(struct devlink *devlink, |
| 3224 | const struct devlink_param *param, |
| 3225 | struct devlink_param_gset_ctx *ctx) |
| 3226 | { |
| 3227 | if (!param->get) |
| 3228 | return -EOPNOTSUPP; |
| 3229 | return param->get(devlink, param->id, ctx); |
| 3230 | } |
| 3231 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3232 | static int devlink_param_set(struct devlink *devlink, |
| 3233 | const struct devlink_param *param, |
| 3234 | struct devlink_param_gset_ctx *ctx) |
| 3235 | { |
| 3236 | if (!param->set) |
| 3237 | return -EOPNOTSUPP; |
| 3238 | return param->set(devlink, param->id, ctx); |
| 3239 | } |
| 3240 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3241 | static int |
| 3242 | devlink_param_type_to_nla_type(enum devlink_param_type param_type) |
| 3243 | { |
| 3244 | switch (param_type) { |
| 3245 | case DEVLINK_PARAM_TYPE_U8: |
| 3246 | return NLA_U8; |
| 3247 | case DEVLINK_PARAM_TYPE_U16: |
| 3248 | return NLA_U16; |
| 3249 | case DEVLINK_PARAM_TYPE_U32: |
| 3250 | return NLA_U32; |
| 3251 | case DEVLINK_PARAM_TYPE_STRING: |
| 3252 | return NLA_STRING; |
| 3253 | case DEVLINK_PARAM_TYPE_BOOL: |
| 3254 | return NLA_FLAG; |
| 3255 | default: |
| 3256 | return -EINVAL; |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | static int |
| 3261 | devlink_nl_param_value_fill_one(struct sk_buff *msg, |
| 3262 | enum devlink_param_type type, |
| 3263 | enum devlink_param_cmode cmode, |
| 3264 | union devlink_param_value val) |
| 3265 | { |
| 3266 | struct nlattr *param_value_attr; |
| 3267 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3268 | param_value_attr = nla_nest_start_noflag(msg, |
| 3269 | DEVLINK_ATTR_PARAM_VALUE); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3270 | if (!param_value_attr) |
| 3271 | goto nla_put_failure; |
| 3272 | |
| 3273 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode)) |
| 3274 | goto value_nest_cancel; |
| 3275 | |
| 3276 | switch (type) { |
| 3277 | case DEVLINK_PARAM_TYPE_U8: |
| 3278 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8)) |
| 3279 | goto value_nest_cancel; |
| 3280 | break; |
| 3281 | case DEVLINK_PARAM_TYPE_U16: |
| 3282 | if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16)) |
| 3283 | goto value_nest_cancel; |
| 3284 | break; |
| 3285 | case DEVLINK_PARAM_TYPE_U32: |
| 3286 | if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32)) |
| 3287 | goto value_nest_cancel; |
| 3288 | break; |
| 3289 | case DEVLINK_PARAM_TYPE_STRING: |
| 3290 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, |
| 3291 | val.vstr)) |
| 3292 | goto value_nest_cancel; |
| 3293 | break; |
| 3294 | case DEVLINK_PARAM_TYPE_BOOL: |
| 3295 | if (val.vbool && |
| 3296 | nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA)) |
| 3297 | goto value_nest_cancel; |
| 3298 | break; |
| 3299 | } |
| 3300 | |
| 3301 | nla_nest_end(msg, param_value_attr); |
| 3302 | return 0; |
| 3303 | |
| 3304 | value_nest_cancel: |
| 3305 | nla_nest_cancel(msg, param_value_attr); |
| 3306 | nla_put_failure: |
| 3307 | return -EMSGSIZE; |
| 3308 | } |
| 3309 | |
| 3310 | static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3311 | unsigned int port_index, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3312 | struct devlink_param_item *param_item, |
| 3313 | enum devlink_command cmd, |
| 3314 | u32 portid, u32 seq, int flags) |
| 3315 | { |
| 3316 | union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1]; |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3317 | bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {}; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3318 | const struct devlink_param *param = param_item->param; |
| 3319 | struct devlink_param_gset_ctx ctx; |
| 3320 | struct nlattr *param_values_list; |
| 3321 | struct nlattr *param_attr; |
| 3322 | int nla_type; |
| 3323 | void *hdr; |
| 3324 | int err; |
| 3325 | int i; |
| 3326 | |
| 3327 | /* Get value from driver part to driverinit configuration mode */ |
| 3328 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
| 3329 | if (!devlink_param_cmode_is_supported(param, i)) |
| 3330 | continue; |
| 3331 | if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
| 3332 | if (!param_item->driverinit_value_valid) |
| 3333 | return -EOPNOTSUPP; |
| 3334 | param_value[i] = param_item->driverinit_value; |
| 3335 | } else { |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3336 | if (!param_item->published) |
| 3337 | continue; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3338 | ctx.cmode = i; |
| 3339 | err = devlink_param_get(devlink, param, &ctx); |
| 3340 | if (err) |
| 3341 | return err; |
| 3342 | param_value[i] = ctx.val; |
| 3343 | } |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3344 | param_value_set[i] = true; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3348 | if (!hdr) |
| 3349 | return -EMSGSIZE; |
| 3350 | |
| 3351 | if (devlink_nl_put_handle(msg, devlink)) |
| 3352 | goto genlmsg_cancel; |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3353 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3354 | if (cmd == DEVLINK_CMD_PORT_PARAM_GET || |
| 3355 | cmd == DEVLINK_CMD_PORT_PARAM_NEW || |
| 3356 | cmd == DEVLINK_CMD_PORT_PARAM_DEL) |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3357 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index)) |
| 3358 | goto genlmsg_cancel; |
| 3359 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3360 | param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3361 | if (!param_attr) |
| 3362 | goto genlmsg_cancel; |
| 3363 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name)) |
| 3364 | goto param_nest_cancel; |
| 3365 | if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC)) |
| 3366 | goto param_nest_cancel; |
| 3367 | |
| 3368 | nla_type = devlink_param_type_to_nla_type(param->type); |
| 3369 | if (nla_type < 0) |
| 3370 | goto param_nest_cancel; |
| 3371 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type)) |
| 3372 | goto param_nest_cancel; |
| 3373 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3374 | param_values_list = nla_nest_start_noflag(msg, |
| 3375 | DEVLINK_ATTR_PARAM_VALUES_LIST); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3376 | if (!param_values_list) |
| 3377 | goto param_nest_cancel; |
| 3378 | |
| 3379 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3380 | if (!param_value_set[i]) |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3381 | continue; |
| 3382 | err = devlink_nl_param_value_fill_one(msg, param->type, |
| 3383 | i, param_value[i]); |
| 3384 | if (err) |
| 3385 | goto values_list_nest_cancel; |
| 3386 | } |
| 3387 | |
| 3388 | nla_nest_end(msg, param_values_list); |
| 3389 | nla_nest_end(msg, param_attr); |
| 3390 | genlmsg_end(msg, hdr); |
| 3391 | return 0; |
| 3392 | |
| 3393 | values_list_nest_cancel: |
| 3394 | nla_nest_end(msg, param_values_list); |
| 3395 | param_nest_cancel: |
| 3396 | nla_nest_cancel(msg, param_attr); |
| 3397 | genlmsg_cancel: |
| 3398 | genlmsg_cancel(msg, hdr); |
| 3399 | return -EMSGSIZE; |
| 3400 | } |
| 3401 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3402 | static void devlink_param_notify(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3403 | unsigned int port_index, |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3404 | struct devlink_param_item *param_item, |
| 3405 | enum devlink_command cmd) |
| 3406 | { |
| 3407 | struct sk_buff *msg; |
| 3408 | int err; |
| 3409 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3410 | WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL && |
| 3411 | cmd != DEVLINK_CMD_PORT_PARAM_NEW && |
| 3412 | cmd != DEVLINK_CMD_PORT_PARAM_DEL); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3413 | |
| 3414 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3415 | if (!msg) |
| 3416 | return; |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3417 | err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd, |
| 3418 | 0, 0, 0); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3419 | if (err) { |
| 3420 | nlmsg_free(msg); |
| 3421 | return; |
| 3422 | } |
| 3423 | |
| 3424 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3425 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 3426 | } |
| 3427 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3428 | static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg, |
| 3429 | struct netlink_callback *cb) |
| 3430 | { |
| 3431 | struct devlink_param_item *param_item; |
| 3432 | struct devlink *devlink; |
| 3433 | int start = cb->args[0]; |
| 3434 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3435 | int err = 0; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3436 | |
| 3437 | mutex_lock(&devlink_mutex); |
| 3438 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3439 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3440 | continue; |
| 3441 | mutex_lock(&devlink->lock); |
| 3442 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 3443 | if (idx < start) { |
| 3444 | idx++; |
| 3445 | continue; |
| 3446 | } |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3447 | err = devlink_nl_param_fill(msg, devlink, 0, param_item, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3448 | DEVLINK_CMD_PARAM_GET, |
| 3449 | NETLINK_CB(cb->skb).portid, |
| 3450 | cb->nlh->nlmsg_seq, |
| 3451 | NLM_F_MULTI); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 3452 | if (err && err != -EOPNOTSUPP) { |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3453 | mutex_unlock(&devlink->lock); |
| 3454 | goto out; |
| 3455 | } |
| 3456 | idx++; |
| 3457 | } |
| 3458 | mutex_unlock(&devlink->lock); |
| 3459 | } |
| 3460 | out: |
| 3461 | mutex_unlock(&devlink_mutex); |
| 3462 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3463 | if (err != -EMSGSIZE) |
| 3464 | return err; |
| 3465 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3466 | cb->args[0] = idx; |
| 3467 | return msg->len; |
| 3468 | } |
| 3469 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3470 | static int |
| 3471 | devlink_param_type_get_from_info(struct genl_info *info, |
| 3472 | enum devlink_param_type *param_type) |
| 3473 | { |
| 3474 | if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE]) |
| 3475 | return -EINVAL; |
| 3476 | |
| 3477 | switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) { |
| 3478 | case NLA_U8: |
| 3479 | *param_type = DEVLINK_PARAM_TYPE_U8; |
| 3480 | break; |
| 3481 | case NLA_U16: |
| 3482 | *param_type = DEVLINK_PARAM_TYPE_U16; |
| 3483 | break; |
| 3484 | case NLA_U32: |
| 3485 | *param_type = DEVLINK_PARAM_TYPE_U32; |
| 3486 | break; |
| 3487 | case NLA_STRING: |
| 3488 | *param_type = DEVLINK_PARAM_TYPE_STRING; |
| 3489 | break; |
| 3490 | case NLA_FLAG: |
| 3491 | *param_type = DEVLINK_PARAM_TYPE_BOOL; |
| 3492 | break; |
| 3493 | default: |
| 3494 | return -EINVAL; |
| 3495 | } |
| 3496 | |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
| 3500 | static int |
| 3501 | devlink_param_value_get_from_info(const struct devlink_param *param, |
| 3502 | struct genl_info *info, |
| 3503 | union devlink_param_value *value) |
| 3504 | { |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3505 | struct nlattr *param_data; |
Moshe Shemesh | f355cfc | 2018-10-10 16:09:25 +0300 | [diff] [blame] | 3506 | int len; |
| 3507 | |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3508 | param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]; |
| 3509 | |
| 3510 | if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3511 | return -EINVAL; |
| 3512 | |
| 3513 | switch (param->type) { |
| 3514 | case DEVLINK_PARAM_TYPE_U8: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3515 | if (nla_len(param_data) != sizeof(u8)) |
| 3516 | return -EINVAL; |
| 3517 | value->vu8 = nla_get_u8(param_data); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3518 | break; |
| 3519 | case DEVLINK_PARAM_TYPE_U16: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3520 | if (nla_len(param_data) != sizeof(u16)) |
| 3521 | return -EINVAL; |
| 3522 | value->vu16 = nla_get_u16(param_data); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3523 | break; |
| 3524 | case DEVLINK_PARAM_TYPE_U32: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3525 | if (nla_len(param_data) != sizeof(u32)) |
| 3526 | return -EINVAL; |
| 3527 | value->vu32 = nla_get_u32(param_data); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3528 | break; |
| 3529 | case DEVLINK_PARAM_TYPE_STRING: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3530 | len = strnlen(nla_data(param_data), nla_len(param_data)); |
| 3531 | if (len == nla_len(param_data) || |
Moshe Shemesh | bde74ad1 | 2018-10-10 16:09:27 +0300 | [diff] [blame] | 3532 | len >= __DEVLINK_PARAM_MAX_STRING_VALUE) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3533 | return -EINVAL; |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3534 | strcpy(value->vstr, nla_data(param_data)); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3535 | break; |
| 3536 | case DEVLINK_PARAM_TYPE_BOOL: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3537 | if (param_data && nla_len(param_data)) |
| 3538 | return -EINVAL; |
| 3539 | value->vbool = nla_get_flag(param_data); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3540 | break; |
| 3541 | } |
| 3542 | return 0; |
| 3543 | } |
| 3544 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3545 | static struct devlink_param_item * |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3546 | devlink_param_get_from_info(struct list_head *param_list, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3547 | struct genl_info *info) |
| 3548 | { |
| 3549 | char *param_name; |
| 3550 | |
| 3551 | if (!info->attrs[DEVLINK_ATTR_PARAM_NAME]) |
| 3552 | return NULL; |
| 3553 | |
| 3554 | param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]); |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3555 | return devlink_param_find_by_name(param_list, param_name); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb, |
| 3559 | struct genl_info *info) |
| 3560 | { |
| 3561 | struct devlink *devlink = info->user_ptr[0]; |
| 3562 | struct devlink_param_item *param_item; |
| 3563 | struct sk_buff *msg; |
| 3564 | int err; |
| 3565 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3566 | param_item = devlink_param_get_from_info(&devlink->param_list, info); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3567 | if (!param_item) |
| 3568 | return -EINVAL; |
| 3569 | |
| 3570 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3571 | if (!msg) |
| 3572 | return -ENOMEM; |
| 3573 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3574 | err = devlink_nl_param_fill(msg, devlink, 0, param_item, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3575 | DEVLINK_CMD_PARAM_GET, |
| 3576 | info->snd_portid, info->snd_seq, 0); |
| 3577 | if (err) { |
| 3578 | nlmsg_free(msg); |
| 3579 | return err; |
| 3580 | } |
| 3581 | |
| 3582 | return genlmsg_reply(msg, info); |
| 3583 | } |
| 3584 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3585 | static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3586 | unsigned int port_index, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3587 | struct list_head *param_list, |
| 3588 | struct genl_info *info, |
| 3589 | enum devlink_command cmd) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3590 | { |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3591 | enum devlink_param_type param_type; |
| 3592 | struct devlink_param_gset_ctx ctx; |
| 3593 | enum devlink_param_cmode cmode; |
| 3594 | struct devlink_param_item *param_item; |
| 3595 | const struct devlink_param *param; |
| 3596 | union devlink_param_value value; |
| 3597 | int err = 0; |
| 3598 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3599 | param_item = devlink_param_get_from_info(param_list, info); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3600 | if (!param_item) |
| 3601 | return -EINVAL; |
| 3602 | param = param_item->param; |
| 3603 | err = devlink_param_type_get_from_info(info, ¶m_type); |
| 3604 | if (err) |
| 3605 | return err; |
| 3606 | if (param_type != param->type) |
| 3607 | return -EINVAL; |
| 3608 | err = devlink_param_value_get_from_info(param, info, &value); |
| 3609 | if (err) |
| 3610 | return err; |
| 3611 | if (param->validate) { |
| 3612 | err = param->validate(devlink, param->id, value, info->extack); |
| 3613 | if (err) |
| 3614 | return err; |
| 3615 | } |
| 3616 | |
| 3617 | if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]) |
| 3618 | return -EINVAL; |
| 3619 | cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]); |
| 3620 | if (!devlink_param_cmode_is_supported(param, cmode)) |
| 3621 | return -EOPNOTSUPP; |
| 3622 | |
| 3623 | if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
Moshe Shemesh | 1276534 | 2018-10-10 16:09:26 +0300 | [diff] [blame] | 3624 | if (param->type == DEVLINK_PARAM_TYPE_STRING) |
| 3625 | strcpy(param_item->driverinit_value.vstr, value.vstr); |
| 3626 | else |
| 3627 | param_item->driverinit_value = value; |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3628 | param_item->driverinit_value_valid = true; |
| 3629 | } else { |
| 3630 | if (!param->set) |
| 3631 | return -EOPNOTSUPP; |
| 3632 | ctx.val = value; |
| 3633 | ctx.cmode = cmode; |
| 3634 | err = devlink_param_set(devlink, param, &ctx); |
| 3635 | if (err) |
| 3636 | return err; |
| 3637 | } |
| 3638 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3639 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3640 | return 0; |
| 3641 | } |
| 3642 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3643 | static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb, |
| 3644 | struct genl_info *info) |
| 3645 | { |
| 3646 | struct devlink *devlink = info->user_ptr[0]; |
| 3647 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3648 | return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3649 | info, DEVLINK_CMD_PARAM_NEW); |
| 3650 | } |
| 3651 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3652 | static int devlink_param_register_one(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3653 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3654 | struct list_head *param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3655 | const struct devlink_param *param, |
| 3656 | enum devlink_command cmd) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3657 | { |
| 3658 | struct devlink_param_item *param_item; |
| 3659 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3660 | if (devlink_param_find_by_name(param_list, param->name)) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3661 | return -EEXIST; |
| 3662 | |
| 3663 | if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 3664 | WARN_ON(param->get || param->set); |
| 3665 | else |
| 3666 | WARN_ON(!param->get || !param->set); |
| 3667 | |
| 3668 | param_item = kzalloc(sizeof(*param_item), GFP_KERNEL); |
| 3669 | if (!param_item) |
| 3670 | return -ENOMEM; |
| 3671 | param_item->param = param; |
| 3672 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3673 | list_add_tail(¶m_item->list, param_list); |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3674 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3675 | return 0; |
| 3676 | } |
| 3677 | |
| 3678 | static void devlink_param_unregister_one(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3679 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3680 | struct list_head *param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3681 | const struct devlink_param *param, |
| 3682 | enum devlink_command cmd) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3683 | { |
| 3684 | struct devlink_param_item *param_item; |
| 3685 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3686 | param_item = devlink_param_find_by_name(param_list, param->name); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3687 | WARN_ON(!param_item); |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3688 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3689 | list_del(¶m_item->list); |
| 3690 | kfree(param_item); |
| 3691 | } |
| 3692 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3693 | static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg, |
| 3694 | struct netlink_callback *cb) |
| 3695 | { |
| 3696 | struct devlink_param_item *param_item; |
| 3697 | struct devlink_port *devlink_port; |
| 3698 | struct devlink *devlink; |
| 3699 | int start = cb->args[0]; |
| 3700 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3701 | int err = 0; |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3702 | |
| 3703 | mutex_lock(&devlink_mutex); |
| 3704 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3705 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3706 | continue; |
| 3707 | mutex_lock(&devlink->lock); |
| 3708 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 3709 | list_for_each_entry(param_item, |
| 3710 | &devlink_port->param_list, list) { |
| 3711 | if (idx < start) { |
| 3712 | idx++; |
| 3713 | continue; |
| 3714 | } |
| 3715 | err = devlink_nl_param_fill(msg, |
| 3716 | devlink_port->devlink, |
| 3717 | devlink_port->index, param_item, |
| 3718 | DEVLINK_CMD_PORT_PARAM_GET, |
| 3719 | NETLINK_CB(cb->skb).portid, |
| 3720 | cb->nlh->nlmsg_seq, |
| 3721 | NLM_F_MULTI); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 3722 | if (err && err != -EOPNOTSUPP) { |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3723 | mutex_unlock(&devlink->lock); |
| 3724 | goto out; |
| 3725 | } |
| 3726 | idx++; |
| 3727 | } |
| 3728 | } |
| 3729 | mutex_unlock(&devlink->lock); |
| 3730 | } |
| 3731 | out: |
| 3732 | mutex_unlock(&devlink_mutex); |
| 3733 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3734 | if (err != -EMSGSIZE) |
| 3735 | return err; |
| 3736 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3737 | cb->args[0] = idx; |
| 3738 | return msg->len; |
| 3739 | } |
| 3740 | |
| 3741 | static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb, |
| 3742 | struct genl_info *info) |
| 3743 | { |
| 3744 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 3745 | struct devlink_param_item *param_item; |
| 3746 | struct sk_buff *msg; |
| 3747 | int err; |
| 3748 | |
| 3749 | param_item = devlink_param_get_from_info(&devlink_port->param_list, |
| 3750 | info); |
| 3751 | if (!param_item) |
| 3752 | return -EINVAL; |
| 3753 | |
| 3754 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3755 | if (!msg) |
| 3756 | return -ENOMEM; |
| 3757 | |
| 3758 | err = devlink_nl_param_fill(msg, devlink_port->devlink, |
| 3759 | devlink_port->index, param_item, |
| 3760 | DEVLINK_CMD_PORT_PARAM_GET, |
| 3761 | info->snd_portid, info->snd_seq, 0); |
| 3762 | if (err) { |
| 3763 | nlmsg_free(msg); |
| 3764 | return err; |
| 3765 | } |
| 3766 | |
| 3767 | return genlmsg_reply(msg, info); |
| 3768 | } |
| 3769 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3770 | static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb, |
| 3771 | struct genl_info *info) |
| 3772 | { |
| 3773 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 3774 | |
| 3775 | return __devlink_nl_cmd_param_set_doit(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3776 | devlink_port->index, |
| 3777 | &devlink_port->param_list, info, |
| 3778 | DEVLINK_CMD_PORT_PARAM_NEW); |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3779 | } |
| 3780 | |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3781 | static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg, |
| 3782 | struct devlink *devlink, |
| 3783 | struct devlink_snapshot *snapshot) |
| 3784 | { |
| 3785 | struct nlattr *snap_attr; |
| 3786 | int err; |
| 3787 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3788 | snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT); |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3789 | if (!snap_attr) |
| 3790 | return -EINVAL; |
| 3791 | |
| 3792 | err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id); |
| 3793 | if (err) |
| 3794 | goto nla_put_failure; |
| 3795 | |
| 3796 | nla_nest_end(msg, snap_attr); |
| 3797 | return 0; |
| 3798 | |
| 3799 | nla_put_failure: |
| 3800 | nla_nest_cancel(msg, snap_attr); |
| 3801 | return err; |
| 3802 | } |
| 3803 | |
| 3804 | static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg, |
| 3805 | struct devlink *devlink, |
| 3806 | struct devlink_region *region) |
| 3807 | { |
| 3808 | struct devlink_snapshot *snapshot; |
| 3809 | struct nlattr *snapshots_attr; |
| 3810 | int err; |
| 3811 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3812 | snapshots_attr = nla_nest_start_noflag(msg, |
| 3813 | DEVLINK_ATTR_REGION_SNAPSHOTS); |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3814 | if (!snapshots_attr) |
| 3815 | return -EINVAL; |
| 3816 | |
| 3817 | list_for_each_entry(snapshot, ®ion->snapshot_list, list) { |
| 3818 | err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot); |
| 3819 | if (err) |
| 3820 | goto nla_put_failure; |
| 3821 | } |
| 3822 | |
| 3823 | nla_nest_end(msg, snapshots_attr); |
| 3824 | return 0; |
| 3825 | |
| 3826 | nla_put_failure: |
| 3827 | nla_nest_cancel(msg, snapshots_attr); |
| 3828 | return err; |
| 3829 | } |
| 3830 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3831 | static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink, |
| 3832 | enum devlink_command cmd, u32 portid, |
| 3833 | u32 seq, int flags, |
| 3834 | struct devlink_region *region) |
| 3835 | { |
| 3836 | void *hdr; |
| 3837 | int err; |
| 3838 | |
| 3839 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3840 | if (!hdr) |
| 3841 | return -EMSGSIZE; |
| 3842 | |
| 3843 | err = devlink_nl_put_handle(msg, devlink); |
| 3844 | if (err) |
| 3845 | goto nla_put_failure; |
| 3846 | |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 3847 | err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name); |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3848 | if (err) |
| 3849 | goto nla_put_failure; |
| 3850 | |
| 3851 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, |
| 3852 | region->size, |
| 3853 | DEVLINK_ATTR_PAD); |
| 3854 | if (err) |
| 3855 | goto nla_put_failure; |
| 3856 | |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3857 | err = devlink_nl_region_snapshots_id_put(msg, devlink, region); |
| 3858 | if (err) |
| 3859 | goto nla_put_failure; |
| 3860 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3861 | genlmsg_end(msg, hdr); |
| 3862 | return 0; |
| 3863 | |
| 3864 | nla_put_failure: |
| 3865 | genlmsg_cancel(msg, hdr); |
| 3866 | return err; |
| 3867 | } |
| 3868 | |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3869 | static struct sk_buff * |
| 3870 | devlink_nl_region_notify_build(struct devlink_region *region, |
| 3871 | struct devlink_snapshot *snapshot, |
| 3872 | enum devlink_command cmd, u32 portid, u32 seq) |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3873 | { |
| 3874 | struct devlink *devlink = region->devlink; |
| 3875 | struct sk_buff *msg; |
| 3876 | void *hdr; |
| 3877 | int err; |
| 3878 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3879 | |
| 3880 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3881 | if (!msg) |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3882 | return ERR_PTR(-ENOMEM); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3883 | |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3884 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd); |
| 3885 | if (!hdr) { |
| 3886 | err = -EMSGSIZE; |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3887 | goto out_free_msg; |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3888 | } |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3889 | |
| 3890 | err = devlink_nl_put_handle(msg, devlink); |
| 3891 | if (err) |
| 3892 | goto out_cancel_msg; |
| 3893 | |
| 3894 | err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 3895 | region->ops->name); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3896 | if (err) |
| 3897 | goto out_cancel_msg; |
| 3898 | |
| 3899 | if (snapshot) { |
| 3900 | err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, |
| 3901 | snapshot->id); |
| 3902 | if (err) |
| 3903 | goto out_cancel_msg; |
| 3904 | } else { |
| 3905 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, |
| 3906 | region->size, DEVLINK_ATTR_PAD); |
| 3907 | if (err) |
| 3908 | goto out_cancel_msg; |
| 3909 | } |
| 3910 | genlmsg_end(msg, hdr); |
| 3911 | |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3912 | return msg; |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3913 | |
| 3914 | out_cancel_msg: |
| 3915 | genlmsg_cancel(msg, hdr); |
| 3916 | out_free_msg: |
| 3917 | nlmsg_free(msg); |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3918 | return ERR_PTR(err); |
| 3919 | } |
| 3920 | |
| 3921 | static void devlink_nl_region_notify(struct devlink_region *region, |
| 3922 | struct devlink_snapshot *snapshot, |
| 3923 | enum devlink_command cmd) |
| 3924 | { |
| 3925 | struct devlink *devlink = region->devlink; |
| 3926 | struct sk_buff *msg; |
| 3927 | |
| 3928 | WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); |
| 3929 | |
| 3930 | msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0); |
| 3931 | if (IS_ERR(msg)) |
| 3932 | return; |
| 3933 | |
| 3934 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3935 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3936 | } |
| 3937 | |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 3938 | /** |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3939 | * __devlink_snapshot_id_increment - Increment number of snapshots using an id |
| 3940 | * @devlink: devlink instance |
| 3941 | * @id: the snapshot id |
| 3942 | * |
| 3943 | * Track when a new snapshot begins using an id. Load the count for the |
| 3944 | * given id from the snapshot xarray, increment it, and store it back. |
| 3945 | * |
| 3946 | * Called when a new snapshot is created with the given id. |
| 3947 | * |
| 3948 | * The id *must* have been previously allocated by |
| 3949 | * devlink_region_snapshot_id_get(). |
| 3950 | * |
| 3951 | * Returns 0 on success, or an error on failure. |
| 3952 | */ |
| 3953 | static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id) |
| 3954 | { |
| 3955 | unsigned long count; |
| 3956 | void *p; |
| 3957 | |
| 3958 | lockdep_assert_held(&devlink->lock); |
| 3959 | |
| 3960 | p = xa_load(&devlink->snapshot_ids, id); |
| 3961 | if (WARN_ON(!p)) |
| 3962 | return -EINVAL; |
| 3963 | |
| 3964 | if (WARN_ON(!xa_is_value(p))) |
| 3965 | return -EINVAL; |
| 3966 | |
| 3967 | count = xa_to_value(p); |
| 3968 | count++; |
| 3969 | |
| 3970 | return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count), |
| 3971 | GFP_KERNEL)); |
| 3972 | } |
| 3973 | |
| 3974 | /** |
| 3975 | * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id |
| 3976 | * @devlink: devlink instance |
| 3977 | * @id: the snapshot id |
| 3978 | * |
| 3979 | * Track when a snapshot is deleted and stops using an id. Load the count |
| 3980 | * for the given id from the snapshot xarray, decrement it, and store it |
| 3981 | * back. |
| 3982 | * |
| 3983 | * If the count reaches zero, erase this id from the xarray, freeing it |
| 3984 | * up for future re-use by devlink_region_snapshot_id_get(). |
| 3985 | * |
| 3986 | * Called when a snapshot using the given id is deleted, and when the |
| 3987 | * initial allocator of the id is finished using it. |
| 3988 | */ |
| 3989 | static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id) |
| 3990 | { |
| 3991 | unsigned long count; |
| 3992 | void *p; |
| 3993 | |
| 3994 | lockdep_assert_held(&devlink->lock); |
| 3995 | |
| 3996 | p = xa_load(&devlink->snapshot_ids, id); |
| 3997 | if (WARN_ON(!p)) |
| 3998 | return; |
| 3999 | |
| 4000 | if (WARN_ON(!xa_is_value(p))) |
| 4001 | return; |
| 4002 | |
| 4003 | count = xa_to_value(p); |
| 4004 | |
| 4005 | if (count > 1) { |
| 4006 | count--; |
| 4007 | xa_store(&devlink->snapshot_ids, id, xa_mk_value(count), |
| 4008 | GFP_KERNEL); |
| 4009 | } else { |
| 4010 | /* If this was the last user, we can erase this id */ |
| 4011 | xa_erase(&devlink->snapshot_ids, id); |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | /** |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4016 | * __devlink_snapshot_id_insert - Insert a specific snapshot ID |
| 4017 | * @devlink: devlink instance |
| 4018 | * @id: the snapshot id |
| 4019 | * |
| 4020 | * Mark the given snapshot id as used by inserting a zero value into the |
| 4021 | * snapshot xarray. |
| 4022 | * |
| 4023 | * This must be called while holding the devlink instance lock. Unlike |
| 4024 | * devlink_snapshot_id_get, the initial reference count is zero, not one. |
| 4025 | * It is expected that the id will immediately be used before |
| 4026 | * releasing the devlink instance lock. |
| 4027 | * |
| 4028 | * Returns zero on success, or an error code if the snapshot id could not |
| 4029 | * be inserted. |
| 4030 | */ |
| 4031 | static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id) |
| 4032 | { |
| 4033 | lockdep_assert_held(&devlink->lock); |
| 4034 | |
| 4035 | if (WARN_ON(xa_load(&devlink->snapshot_ids, id))) |
| 4036 | return -EEXIST; |
| 4037 | |
| 4038 | return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0), |
| 4039 | GFP_KERNEL)); |
| 4040 | } |
| 4041 | |
| 4042 | /** |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 4043 | * __devlink_region_snapshot_id_get - get snapshot ID |
| 4044 | * @devlink: devlink instance |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 4045 | * @id: storage to return snapshot id |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 4046 | * |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 4047 | * Allocates a new snapshot id. Returns zero on success, or a negative |
| 4048 | * error on failure. Must be called while holding the devlink instance |
| 4049 | * lock. |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 4050 | * |
| 4051 | * Snapshot IDs are tracked using an xarray which stores the number of |
| 4052 | * users of the snapshot id. |
| 4053 | * |
| 4054 | * Note that the caller of this function counts as a 'user', in order to |
| 4055 | * avoid race conditions. The caller must release its hold on the |
| 4056 | * snapshot by using devlink_region_snapshot_id_put. |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 4057 | */ |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 4058 | static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id) |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 4059 | { |
| 4060 | lockdep_assert_held(&devlink->lock); |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 4061 | |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 4062 | return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1), |
| 4063 | xa_limit_32b, GFP_KERNEL); |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | /** |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 4067 | * __devlink_region_snapshot_create - create a new snapshot |
| 4068 | * This will add a new snapshot of a region. The snapshot |
| 4069 | * will be stored on the region struct and can be accessed |
| 4070 | * from devlink. This is useful for future analyses of snapshots. |
| 4071 | * Multiple snapshots can be created on a region. |
| 4072 | * The @snapshot_id should be obtained using the getter function. |
| 4073 | * |
| 4074 | * Must be called only while holding the devlink instance lock. |
| 4075 | * |
| 4076 | * @region: devlink region of the snapshot |
| 4077 | * @data: snapshot data |
| 4078 | * @snapshot_id: snapshot id to be created |
| 4079 | */ |
| 4080 | static int |
| 4081 | __devlink_region_snapshot_create(struct devlink_region *region, |
| 4082 | u8 *data, u32 snapshot_id) |
| 4083 | { |
| 4084 | struct devlink *devlink = region->devlink; |
| 4085 | struct devlink_snapshot *snapshot; |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 4086 | int err; |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 4087 | |
| 4088 | lockdep_assert_held(&devlink->lock); |
| 4089 | |
| 4090 | /* check if region can hold one more snapshot */ |
| 4091 | if (region->cur_snapshots == region->max_snapshots) |
Jacob Keller | 47a39f6 | 2020-03-26 11:37:12 -0700 | [diff] [blame] | 4092 | return -ENOSPC; |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 4093 | |
| 4094 | if (devlink_region_snapshot_get_by_id(region, snapshot_id)) |
| 4095 | return -EEXIST; |
| 4096 | |
| 4097 | snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL); |
| 4098 | if (!snapshot) |
| 4099 | return -ENOMEM; |
| 4100 | |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 4101 | err = __devlink_snapshot_id_increment(devlink, snapshot_id); |
| 4102 | if (err) |
| 4103 | goto err_snapshot_id_increment; |
| 4104 | |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 4105 | snapshot->id = snapshot_id; |
| 4106 | snapshot->region = region; |
| 4107 | snapshot->data = data; |
| 4108 | |
| 4109 | list_add_tail(&snapshot->list, ®ion->snapshot_list); |
| 4110 | |
| 4111 | region->cur_snapshots++; |
| 4112 | |
| 4113 | devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW); |
| 4114 | return 0; |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 4115 | |
| 4116 | err_snapshot_id_increment: |
| 4117 | kfree(snapshot); |
| 4118 | return err; |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 4119 | } |
| 4120 | |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 4121 | static void devlink_region_snapshot_del(struct devlink_region *region, |
| 4122 | struct devlink_snapshot *snapshot) |
| 4123 | { |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 4124 | struct devlink *devlink = region->devlink; |
| 4125 | |
| 4126 | lockdep_assert_held(&devlink->lock); |
| 4127 | |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 4128 | devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL); |
| 4129 | region->cur_snapshots--; |
| 4130 | list_del(&snapshot->list); |
Jacob Keller | a0a09f6 | 2020-03-26 11:37:09 -0700 | [diff] [blame] | 4131 | region->ops->destructor(snapshot->data); |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 4132 | __devlink_snapshot_id_decrement(devlink, snapshot->id); |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 4133 | kfree(snapshot); |
| 4134 | } |
| 4135 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 4136 | static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb, |
| 4137 | struct genl_info *info) |
| 4138 | { |
| 4139 | struct devlink *devlink = info->user_ptr[0]; |
| 4140 | struct devlink_region *region; |
| 4141 | const char *region_name; |
| 4142 | struct sk_buff *msg; |
| 4143 | int err; |
| 4144 | |
| 4145 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) |
| 4146 | return -EINVAL; |
| 4147 | |
| 4148 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 4149 | region = devlink_region_get_by_name(devlink, region_name); |
| 4150 | if (!region) |
| 4151 | return -EINVAL; |
| 4152 | |
| 4153 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4154 | if (!msg) |
| 4155 | return -ENOMEM; |
| 4156 | |
| 4157 | err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET, |
| 4158 | info->snd_portid, info->snd_seq, 0, |
| 4159 | region); |
| 4160 | if (err) { |
| 4161 | nlmsg_free(msg); |
| 4162 | return err; |
| 4163 | } |
| 4164 | |
| 4165 | return genlmsg_reply(msg, info); |
| 4166 | } |
| 4167 | |
| 4168 | static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg, |
| 4169 | struct netlink_callback *cb) |
| 4170 | { |
| 4171 | struct devlink_region *region; |
| 4172 | struct devlink *devlink; |
| 4173 | int start = cb->args[0]; |
| 4174 | int idx = 0; |
| 4175 | int err; |
| 4176 | |
| 4177 | mutex_lock(&devlink_mutex); |
| 4178 | list_for_each_entry(devlink, &devlink_list, list) { |
| 4179 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 4180 | continue; |
| 4181 | |
| 4182 | mutex_lock(&devlink->lock); |
| 4183 | list_for_each_entry(region, &devlink->region_list, list) { |
| 4184 | if (idx < start) { |
| 4185 | idx++; |
| 4186 | continue; |
| 4187 | } |
| 4188 | err = devlink_nl_region_fill(msg, devlink, |
| 4189 | DEVLINK_CMD_REGION_GET, |
| 4190 | NETLINK_CB(cb->skb).portid, |
| 4191 | cb->nlh->nlmsg_seq, |
| 4192 | NLM_F_MULTI, region); |
| 4193 | if (err) { |
| 4194 | mutex_unlock(&devlink->lock); |
| 4195 | goto out; |
| 4196 | } |
| 4197 | idx++; |
| 4198 | } |
| 4199 | mutex_unlock(&devlink->lock); |
| 4200 | } |
| 4201 | out: |
| 4202 | mutex_unlock(&devlink_mutex); |
| 4203 | cb->args[0] = idx; |
| 4204 | return msg->len; |
| 4205 | } |
| 4206 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 4207 | static int devlink_nl_cmd_region_del(struct sk_buff *skb, |
| 4208 | struct genl_info *info) |
| 4209 | { |
| 4210 | struct devlink *devlink = info->user_ptr[0]; |
| 4211 | struct devlink_snapshot *snapshot; |
| 4212 | struct devlink_region *region; |
| 4213 | const char *region_name; |
| 4214 | u32 snapshot_id; |
| 4215 | |
| 4216 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME] || |
| 4217 | !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) |
| 4218 | return -EINVAL; |
| 4219 | |
| 4220 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 4221 | snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); |
| 4222 | |
| 4223 | region = devlink_region_get_by_name(devlink, region_name); |
| 4224 | if (!region) |
| 4225 | return -EINVAL; |
| 4226 | |
| 4227 | snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); |
| 4228 | if (!snapshot) |
| 4229 | return -EINVAL; |
| 4230 | |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 4231 | devlink_region_snapshot_del(region, snapshot); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 4232 | return 0; |
| 4233 | } |
| 4234 | |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4235 | static int |
| 4236 | devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info) |
| 4237 | { |
| 4238 | struct devlink *devlink = info->user_ptr[0]; |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4239 | struct devlink_snapshot *snapshot; |
| 4240 | struct nlattr *snapshot_id_attr; |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4241 | struct devlink_region *region; |
| 4242 | const char *region_name; |
| 4243 | u32 snapshot_id; |
| 4244 | u8 *data; |
| 4245 | int err; |
| 4246 | |
| 4247 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) { |
| 4248 | NL_SET_ERR_MSG_MOD(info->extack, "No region name provided"); |
| 4249 | return -EINVAL; |
| 4250 | } |
| 4251 | |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4252 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 4253 | region = devlink_region_get_by_name(devlink, region_name); |
| 4254 | if (!region) { |
| 4255 | NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist"); |
| 4256 | return -EINVAL; |
| 4257 | } |
| 4258 | |
| 4259 | if (!region->ops->snapshot) { |
| 4260 | NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot"); |
| 4261 | return -EOPNOTSUPP; |
| 4262 | } |
| 4263 | |
| 4264 | if (region->cur_snapshots == region->max_snapshots) { |
| 4265 | NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots"); |
| 4266 | return -ENOSPC; |
| 4267 | } |
| 4268 | |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4269 | snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]; |
| 4270 | if (snapshot_id_attr) { |
| 4271 | snapshot_id = nla_get_u32(snapshot_id_attr); |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4272 | |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4273 | if (devlink_region_snapshot_get_by_id(region, snapshot_id)) { |
| 4274 | NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use"); |
| 4275 | return -EEXIST; |
| 4276 | } |
| 4277 | |
| 4278 | err = __devlink_snapshot_id_insert(devlink, snapshot_id); |
| 4279 | if (err) |
| 4280 | return err; |
| 4281 | } else { |
| 4282 | err = __devlink_region_snapshot_id_get(devlink, &snapshot_id); |
| 4283 | if (err) { |
| 4284 | NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id"); |
| 4285 | return err; |
| 4286 | } |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4289 | err = region->ops->snapshot(devlink, info->extack, &data); |
| 4290 | if (err) |
| 4291 | goto err_snapshot_capture; |
| 4292 | |
| 4293 | err = __devlink_region_snapshot_create(region, data, snapshot_id); |
| 4294 | if (err) |
| 4295 | goto err_snapshot_create; |
| 4296 | |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4297 | if (!snapshot_id_attr) { |
| 4298 | struct sk_buff *msg; |
| 4299 | |
| 4300 | snapshot = devlink_region_snapshot_get_by_id(region, |
| 4301 | snapshot_id); |
| 4302 | if (WARN_ON(!snapshot)) |
| 4303 | return -EINVAL; |
| 4304 | |
| 4305 | msg = devlink_nl_region_notify_build(region, snapshot, |
| 4306 | DEVLINK_CMD_REGION_NEW, |
| 4307 | info->snd_portid, |
| 4308 | info->snd_seq); |
| 4309 | err = PTR_ERR_OR_ZERO(msg); |
| 4310 | if (err) |
| 4311 | goto err_notify; |
| 4312 | |
| 4313 | err = genlmsg_reply(msg, info); |
| 4314 | if (err) |
| 4315 | goto err_notify; |
| 4316 | } |
| 4317 | |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4318 | return 0; |
| 4319 | |
| 4320 | err_snapshot_create: |
| 4321 | region->ops->destructor(data); |
| 4322 | err_snapshot_capture: |
| 4323 | __devlink_snapshot_id_decrement(devlink, snapshot_id); |
| 4324 | return err; |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4325 | |
| 4326 | err_notify: |
| 4327 | devlink_region_snapshot_del(region, snapshot); |
| 4328 | return err; |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4329 | } |
| 4330 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4331 | static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg, |
| 4332 | struct devlink *devlink, |
| 4333 | u8 *chunk, u32 chunk_size, |
| 4334 | u64 addr) |
| 4335 | { |
| 4336 | struct nlattr *chunk_attr; |
| 4337 | int err; |
| 4338 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4339 | chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4340 | if (!chunk_attr) |
| 4341 | return -EINVAL; |
| 4342 | |
| 4343 | err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk); |
| 4344 | if (err) |
| 4345 | goto nla_put_failure; |
| 4346 | |
| 4347 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr, |
| 4348 | DEVLINK_ATTR_PAD); |
| 4349 | if (err) |
| 4350 | goto nla_put_failure; |
| 4351 | |
| 4352 | nla_nest_end(msg, chunk_attr); |
| 4353 | return 0; |
| 4354 | |
| 4355 | nla_put_failure: |
| 4356 | nla_nest_cancel(msg, chunk_attr); |
| 4357 | return err; |
| 4358 | } |
| 4359 | |
| 4360 | #define DEVLINK_REGION_READ_CHUNK_SIZE 256 |
| 4361 | |
| 4362 | static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb, |
| 4363 | struct devlink *devlink, |
| 4364 | struct devlink_region *region, |
| 4365 | struct nlattr **attrs, |
| 4366 | u64 start_offset, |
| 4367 | u64 end_offset, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4368 | u64 *new_offset) |
| 4369 | { |
| 4370 | struct devlink_snapshot *snapshot; |
| 4371 | u64 curr_offset = start_offset; |
| 4372 | u32 snapshot_id; |
| 4373 | int err = 0; |
| 4374 | |
| 4375 | *new_offset = start_offset; |
| 4376 | |
| 4377 | snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); |
| 4378 | snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); |
| 4379 | if (!snapshot) |
| 4380 | return -EINVAL; |
| 4381 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4382 | while (curr_offset < end_offset) { |
| 4383 | u32 data_size; |
| 4384 | u8 *data; |
| 4385 | |
| 4386 | if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE) |
| 4387 | data_size = end_offset - curr_offset; |
| 4388 | else |
| 4389 | data_size = DEVLINK_REGION_READ_CHUNK_SIZE; |
| 4390 | |
| 4391 | data = &snapshot->data[curr_offset]; |
| 4392 | err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink, |
| 4393 | data, data_size, |
| 4394 | curr_offset); |
| 4395 | if (err) |
| 4396 | break; |
| 4397 | |
| 4398 | curr_offset += data_size; |
| 4399 | } |
| 4400 | *new_offset = curr_offset; |
| 4401 | |
| 4402 | return err; |
| 4403 | } |
| 4404 | |
| 4405 | static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb, |
| 4406 | struct netlink_callback *cb) |
| 4407 | { |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 4408 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
Jakub Kicinski | 5a46b06 | 2020-05-13 10:28:22 -0700 | [diff] [blame] | 4409 | u64 ret_offset, start_offset, end_offset = U64_MAX; |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 4410 | struct nlattr **attrs = info->attrs; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4411 | struct devlink_region *region; |
| 4412 | struct nlattr *chunks_attr; |
| 4413 | const char *region_name; |
| 4414 | struct devlink *devlink; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4415 | void *hdr; |
| 4416 | int err; |
| 4417 | |
| 4418 | start_offset = *((u64 *)&cb->args[0]); |
| 4419 | |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 4420 | mutex_lock(&devlink_mutex); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4421 | devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4422 | if (IS_ERR(devlink)) { |
| 4423 | err = PTR_ERR(devlink); |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 4424 | goto out_dev; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4425 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4426 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4427 | mutex_lock(&devlink->lock); |
| 4428 | |
| 4429 | if (!attrs[DEVLINK_ATTR_REGION_NAME] || |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4430 | !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) { |
| 4431 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4432 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4433 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4434 | |
| 4435 | region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]); |
| 4436 | region = devlink_region_get_by_name(devlink, region_name); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4437 | if (!region) { |
| 4438 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4439 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4440 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4441 | |
Jakub Kicinski | 5a46b06 | 2020-05-13 10:28:22 -0700 | [diff] [blame] | 4442 | if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] && |
| 4443 | attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) { |
| 4444 | if (!start_offset) |
| 4445 | start_offset = |
| 4446 | nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); |
| 4447 | |
| 4448 | end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); |
| 4449 | end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]); |
| 4450 | } |
| 4451 | |
| 4452 | if (end_offset > region->size) |
| 4453 | end_offset = region->size; |
| 4454 | |
Jacob Keller | d5b90e9 | 2020-02-04 15:59:50 -0800 | [diff] [blame] | 4455 | /* return 0 if there is no further data to read */ |
Jakub Kicinski | 5a46b06 | 2020-05-13 10:28:22 -0700 | [diff] [blame] | 4456 | if (start_offset == end_offset) { |
Jacob Keller | d5b90e9 | 2020-02-04 15:59:50 -0800 | [diff] [blame] | 4457 | err = 0; |
| 4458 | goto out_unlock; |
| 4459 | } |
| 4460 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4461 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 4462 | &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, |
| 4463 | DEVLINK_CMD_REGION_READ); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4464 | if (!hdr) { |
| 4465 | err = -EMSGSIZE; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4466 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4467 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4468 | |
| 4469 | err = devlink_nl_put_handle(skb, devlink); |
| 4470 | if (err) |
| 4471 | goto nla_put_failure; |
| 4472 | |
| 4473 | err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name); |
| 4474 | if (err) |
| 4475 | goto nla_put_failure; |
| 4476 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4477 | chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4478 | if (!chunks_attr) { |
| 4479 | err = -EMSGSIZE; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4480 | goto nla_put_failure; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4481 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4482 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4483 | err = devlink_nl_region_read_snapshot_fill(skb, devlink, |
| 4484 | region, attrs, |
| 4485 | start_offset, |
Jakub Kicinski | 5a46b06 | 2020-05-13 10:28:22 -0700 | [diff] [blame] | 4486 | end_offset, &ret_offset); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4487 | |
| 4488 | if (err && err != -EMSGSIZE) |
| 4489 | goto nla_put_failure; |
| 4490 | |
| 4491 | /* Check if there was any progress done to prevent infinite loop */ |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4492 | if (ret_offset == start_offset) { |
| 4493 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4494 | goto nla_put_failure; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4495 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4496 | |
| 4497 | *((u64 *)&cb->args[0]) = ret_offset; |
| 4498 | |
| 4499 | nla_nest_end(skb, chunks_attr); |
| 4500 | genlmsg_end(skb, hdr); |
| 4501 | mutex_unlock(&devlink->lock); |
| 4502 | mutex_unlock(&devlink_mutex); |
| 4503 | |
| 4504 | return skb->len; |
| 4505 | |
| 4506 | nla_put_failure: |
| 4507 | genlmsg_cancel(skb, hdr); |
| 4508 | out_unlock: |
| 4509 | mutex_unlock(&devlink->lock); |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 4510 | out_dev: |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4511 | mutex_unlock(&devlink_mutex); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4512 | return err; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4513 | } |
| 4514 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4515 | struct devlink_info_req { |
| 4516 | struct sk_buff *msg; |
| 4517 | }; |
| 4518 | |
| 4519 | int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name) |
| 4520 | { |
| 4521 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name); |
| 4522 | } |
| 4523 | EXPORT_SYMBOL_GPL(devlink_info_driver_name_put); |
| 4524 | |
| 4525 | int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn) |
| 4526 | { |
| 4527 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn); |
| 4528 | } |
| 4529 | EXPORT_SYMBOL_GPL(devlink_info_serial_number_put); |
| 4530 | |
Vasundhara Volam | b5872cd | 2020-06-20 22:01:56 +0530 | [diff] [blame] | 4531 | int devlink_info_board_serial_number_put(struct devlink_info_req *req, |
| 4532 | const char *bsn) |
| 4533 | { |
| 4534 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER, |
| 4535 | bsn); |
| 4536 | } |
| 4537 | EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put); |
| 4538 | |
Jakub Kicinski | fc6fae7 | 2019-01-31 10:50:41 -0800 | [diff] [blame] | 4539 | static int devlink_info_version_put(struct devlink_info_req *req, int attr, |
| 4540 | const char *version_name, |
| 4541 | const char *version_value) |
| 4542 | { |
| 4543 | struct nlattr *nest; |
| 4544 | int err; |
| 4545 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4546 | nest = nla_nest_start_noflag(req->msg, attr); |
Jakub Kicinski | fc6fae7 | 2019-01-31 10:50:41 -0800 | [diff] [blame] | 4547 | if (!nest) |
| 4548 | return -EMSGSIZE; |
| 4549 | |
| 4550 | err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME, |
| 4551 | version_name); |
| 4552 | if (err) |
| 4553 | goto nla_put_failure; |
| 4554 | |
| 4555 | err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE, |
| 4556 | version_value); |
| 4557 | if (err) |
| 4558 | goto nla_put_failure; |
| 4559 | |
| 4560 | nla_nest_end(req->msg, nest); |
| 4561 | |
| 4562 | return 0; |
| 4563 | |
| 4564 | nla_put_failure: |
| 4565 | nla_nest_cancel(req->msg, nest); |
| 4566 | return err; |
| 4567 | } |
| 4568 | |
| 4569 | int devlink_info_version_fixed_put(struct devlink_info_req *req, |
| 4570 | const char *version_name, |
| 4571 | const char *version_value) |
| 4572 | { |
| 4573 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED, |
| 4574 | version_name, version_value); |
| 4575 | } |
| 4576 | EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put); |
| 4577 | |
| 4578 | int devlink_info_version_stored_put(struct devlink_info_req *req, |
| 4579 | const char *version_name, |
| 4580 | const char *version_value) |
| 4581 | { |
| 4582 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED, |
| 4583 | version_name, version_value); |
| 4584 | } |
| 4585 | EXPORT_SYMBOL_GPL(devlink_info_version_stored_put); |
| 4586 | |
| 4587 | int devlink_info_version_running_put(struct devlink_info_req *req, |
| 4588 | const char *version_name, |
| 4589 | const char *version_value) |
| 4590 | { |
| 4591 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING, |
| 4592 | version_name, version_value); |
| 4593 | } |
| 4594 | EXPORT_SYMBOL_GPL(devlink_info_version_running_put); |
| 4595 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4596 | static int |
| 4597 | devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink, |
| 4598 | enum devlink_command cmd, u32 portid, |
| 4599 | u32 seq, int flags, struct netlink_ext_ack *extack) |
| 4600 | { |
| 4601 | struct devlink_info_req req; |
| 4602 | void *hdr; |
| 4603 | int err; |
| 4604 | |
| 4605 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 4606 | if (!hdr) |
| 4607 | return -EMSGSIZE; |
| 4608 | |
| 4609 | err = -EMSGSIZE; |
| 4610 | if (devlink_nl_put_handle(msg, devlink)) |
| 4611 | goto err_cancel_msg; |
| 4612 | |
| 4613 | req.msg = msg; |
| 4614 | err = devlink->ops->info_get(devlink, &req, extack); |
| 4615 | if (err) |
| 4616 | goto err_cancel_msg; |
| 4617 | |
| 4618 | genlmsg_end(msg, hdr); |
| 4619 | return 0; |
| 4620 | |
| 4621 | err_cancel_msg: |
| 4622 | genlmsg_cancel(msg, hdr); |
| 4623 | return err; |
| 4624 | } |
| 4625 | |
| 4626 | static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb, |
| 4627 | struct genl_info *info) |
| 4628 | { |
| 4629 | struct devlink *devlink = info->user_ptr[0]; |
| 4630 | struct sk_buff *msg; |
| 4631 | int err; |
| 4632 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 4633 | if (!devlink->ops->info_get) |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4634 | return -EOPNOTSUPP; |
| 4635 | |
| 4636 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4637 | if (!msg) |
| 4638 | return -ENOMEM; |
| 4639 | |
| 4640 | err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET, |
| 4641 | info->snd_portid, info->snd_seq, 0, |
| 4642 | info->extack); |
| 4643 | if (err) { |
| 4644 | nlmsg_free(msg); |
| 4645 | return err; |
| 4646 | } |
| 4647 | |
| 4648 | return genlmsg_reply(msg, info); |
| 4649 | } |
| 4650 | |
| 4651 | static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg, |
| 4652 | struct netlink_callback *cb) |
| 4653 | { |
| 4654 | struct devlink *devlink; |
| 4655 | int start = cb->args[0]; |
| 4656 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 4657 | int err = 0; |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4658 | |
| 4659 | mutex_lock(&devlink_mutex); |
| 4660 | list_for_each_entry(devlink, &devlink_list, list) { |
| 4661 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 4662 | continue; |
| 4663 | if (idx < start) { |
| 4664 | idx++; |
| 4665 | continue; |
| 4666 | } |
| 4667 | |
Jiri Pirko | c493b09b | 2019-03-24 00:21:03 +0100 | [diff] [blame] | 4668 | if (!devlink->ops->info_get) { |
| 4669 | idx++; |
| 4670 | continue; |
| 4671 | } |
| 4672 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4673 | mutex_lock(&devlink->lock); |
| 4674 | err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET, |
| 4675 | NETLINK_CB(cb->skb).portid, |
| 4676 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 4677 | cb->extack); |
| 4678 | mutex_unlock(&devlink->lock); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 4679 | if (err && err != -EOPNOTSUPP) |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4680 | break; |
| 4681 | idx++; |
| 4682 | } |
| 4683 | mutex_unlock(&devlink_mutex); |
| 4684 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 4685 | if (err != -EMSGSIZE) |
| 4686 | return err; |
| 4687 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4688 | cb->args[0] = idx; |
| 4689 | return msg->len; |
| 4690 | } |
| 4691 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4692 | struct devlink_fmsg_item { |
| 4693 | struct list_head list; |
| 4694 | int attrtype; |
| 4695 | u8 nla_type; |
| 4696 | u16 len; |
Gustavo A. R. Silva | d2afb41 | 2020-02-28 07:43:24 -0600 | [diff] [blame] | 4697 | int value[]; |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4698 | }; |
| 4699 | |
| 4700 | struct devlink_fmsg { |
| 4701 | struct list_head item_list; |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4702 | bool putting_binary; /* This flag forces enclosing of binary data |
| 4703 | * in an array brackets. It forces using |
| 4704 | * of designated API: |
| 4705 | * devlink_fmsg_binary_pair_nest_start() |
| 4706 | * devlink_fmsg_binary_pair_nest_end() |
| 4707 | */ |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4708 | }; |
| 4709 | |
| 4710 | static struct devlink_fmsg *devlink_fmsg_alloc(void) |
| 4711 | { |
| 4712 | struct devlink_fmsg *fmsg; |
| 4713 | |
| 4714 | fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL); |
| 4715 | if (!fmsg) |
| 4716 | return NULL; |
| 4717 | |
| 4718 | INIT_LIST_HEAD(&fmsg->item_list); |
| 4719 | |
| 4720 | return fmsg; |
| 4721 | } |
| 4722 | |
| 4723 | static void devlink_fmsg_free(struct devlink_fmsg *fmsg) |
| 4724 | { |
| 4725 | struct devlink_fmsg_item *item, *tmp; |
| 4726 | |
| 4727 | list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) { |
| 4728 | list_del(&item->list); |
| 4729 | kfree(item); |
| 4730 | } |
| 4731 | kfree(fmsg); |
| 4732 | } |
| 4733 | |
| 4734 | static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg, |
| 4735 | int attrtype) |
| 4736 | { |
| 4737 | struct devlink_fmsg_item *item; |
| 4738 | |
| 4739 | item = kzalloc(sizeof(*item), GFP_KERNEL); |
| 4740 | if (!item) |
| 4741 | return -ENOMEM; |
| 4742 | |
| 4743 | item->attrtype = attrtype; |
| 4744 | list_add_tail(&item->list, &fmsg->item_list); |
| 4745 | |
| 4746 | return 0; |
| 4747 | } |
| 4748 | |
| 4749 | int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg) |
| 4750 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4751 | if (fmsg->putting_binary) |
| 4752 | return -EINVAL; |
| 4753 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4754 | return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START); |
| 4755 | } |
| 4756 | EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start); |
| 4757 | |
| 4758 | static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg) |
| 4759 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4760 | if (fmsg->putting_binary) |
| 4761 | return -EINVAL; |
| 4762 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4763 | return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END); |
| 4764 | } |
| 4765 | |
| 4766 | int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg) |
| 4767 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4768 | if (fmsg->putting_binary) |
| 4769 | return -EINVAL; |
| 4770 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4771 | return devlink_fmsg_nest_end(fmsg); |
| 4772 | } |
| 4773 | EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end); |
| 4774 | |
| 4775 | #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN) |
| 4776 | |
| 4777 | static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name) |
| 4778 | { |
| 4779 | struct devlink_fmsg_item *item; |
| 4780 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4781 | if (fmsg->putting_binary) |
| 4782 | return -EINVAL; |
| 4783 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4784 | if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE) |
| 4785 | return -EMSGSIZE; |
| 4786 | |
| 4787 | item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL); |
| 4788 | if (!item) |
| 4789 | return -ENOMEM; |
| 4790 | |
| 4791 | item->nla_type = NLA_NUL_STRING; |
| 4792 | item->len = strlen(name) + 1; |
| 4793 | item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME; |
| 4794 | memcpy(&item->value, name, item->len); |
| 4795 | list_add_tail(&item->list, &fmsg->item_list); |
| 4796 | |
| 4797 | return 0; |
| 4798 | } |
| 4799 | |
| 4800 | int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name) |
| 4801 | { |
| 4802 | int err; |
| 4803 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4804 | if (fmsg->putting_binary) |
| 4805 | return -EINVAL; |
| 4806 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4807 | err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START); |
| 4808 | if (err) |
| 4809 | return err; |
| 4810 | |
| 4811 | err = devlink_fmsg_put_name(fmsg, name); |
| 4812 | if (err) |
| 4813 | return err; |
| 4814 | |
| 4815 | return 0; |
| 4816 | } |
| 4817 | EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start); |
| 4818 | |
| 4819 | int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4820 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4821 | if (fmsg->putting_binary) |
| 4822 | return -EINVAL; |
| 4823 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4824 | return devlink_fmsg_nest_end(fmsg); |
| 4825 | } |
| 4826 | EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end); |
| 4827 | |
| 4828 | int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg, |
| 4829 | const char *name) |
| 4830 | { |
| 4831 | int err; |
| 4832 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4833 | if (fmsg->putting_binary) |
| 4834 | return -EINVAL; |
| 4835 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4836 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4837 | if (err) |
| 4838 | return err; |
| 4839 | |
| 4840 | err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START); |
| 4841 | if (err) |
| 4842 | return err; |
| 4843 | |
| 4844 | return 0; |
| 4845 | } |
| 4846 | EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start); |
| 4847 | |
| 4848 | int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4849 | { |
| 4850 | int err; |
| 4851 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4852 | if (fmsg->putting_binary) |
| 4853 | return -EINVAL; |
| 4854 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4855 | err = devlink_fmsg_nest_end(fmsg); |
| 4856 | if (err) |
| 4857 | return err; |
| 4858 | |
| 4859 | err = devlink_fmsg_nest_end(fmsg); |
| 4860 | if (err) |
| 4861 | return err; |
| 4862 | |
| 4863 | return 0; |
| 4864 | } |
| 4865 | EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end); |
| 4866 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4867 | int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg, |
| 4868 | const char *name) |
| 4869 | { |
| 4870 | int err; |
| 4871 | |
| 4872 | err = devlink_fmsg_arr_pair_nest_start(fmsg, name); |
| 4873 | if (err) |
| 4874 | return err; |
| 4875 | |
| 4876 | fmsg->putting_binary = true; |
| 4877 | return err; |
| 4878 | } |
| 4879 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start); |
| 4880 | |
| 4881 | int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4882 | { |
| 4883 | if (!fmsg->putting_binary) |
| 4884 | return -EINVAL; |
| 4885 | |
| 4886 | fmsg->putting_binary = false; |
| 4887 | return devlink_fmsg_arr_pair_nest_end(fmsg); |
| 4888 | } |
| 4889 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end); |
| 4890 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4891 | static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg, |
| 4892 | const void *value, u16 value_len, |
| 4893 | u8 value_nla_type) |
| 4894 | { |
| 4895 | struct devlink_fmsg_item *item; |
| 4896 | |
| 4897 | if (value_len > DEVLINK_FMSG_MAX_SIZE) |
| 4898 | return -EMSGSIZE; |
| 4899 | |
| 4900 | item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL); |
| 4901 | if (!item) |
| 4902 | return -ENOMEM; |
| 4903 | |
| 4904 | item->nla_type = value_nla_type; |
| 4905 | item->len = value_len; |
| 4906 | item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; |
| 4907 | memcpy(&item->value, value, item->len); |
| 4908 | list_add_tail(&item->list, &fmsg->item_list); |
| 4909 | |
| 4910 | return 0; |
| 4911 | } |
| 4912 | |
| 4913 | int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value) |
| 4914 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4915 | if (fmsg->putting_binary) |
| 4916 | return -EINVAL; |
| 4917 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4918 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG); |
| 4919 | } |
| 4920 | EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put); |
| 4921 | |
| 4922 | int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value) |
| 4923 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4924 | if (fmsg->putting_binary) |
| 4925 | return -EINVAL; |
| 4926 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4927 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8); |
| 4928 | } |
| 4929 | EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put); |
| 4930 | |
| 4931 | int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value) |
| 4932 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4933 | if (fmsg->putting_binary) |
| 4934 | return -EINVAL; |
| 4935 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4936 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32); |
| 4937 | } |
| 4938 | EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put); |
| 4939 | |
| 4940 | int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value) |
| 4941 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4942 | if (fmsg->putting_binary) |
| 4943 | return -EINVAL; |
| 4944 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4945 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64); |
| 4946 | } |
| 4947 | EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put); |
| 4948 | |
| 4949 | int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value) |
| 4950 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4951 | if (fmsg->putting_binary) |
| 4952 | return -EINVAL; |
| 4953 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4954 | return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, |
| 4955 | NLA_NUL_STRING); |
| 4956 | } |
| 4957 | EXPORT_SYMBOL_GPL(devlink_fmsg_string_put); |
| 4958 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4959 | int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value, |
| 4960 | u16 value_len) |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4961 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4962 | if (!fmsg->putting_binary) |
| 4963 | return -EINVAL; |
| 4964 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4965 | return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY); |
| 4966 | } |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4967 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4968 | |
| 4969 | int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4970 | bool value) |
| 4971 | { |
| 4972 | int err; |
| 4973 | |
| 4974 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4975 | if (err) |
| 4976 | return err; |
| 4977 | |
| 4978 | err = devlink_fmsg_bool_put(fmsg, value); |
| 4979 | if (err) |
| 4980 | return err; |
| 4981 | |
| 4982 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4983 | if (err) |
| 4984 | return err; |
| 4985 | |
| 4986 | return 0; |
| 4987 | } |
| 4988 | EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put); |
| 4989 | |
| 4990 | int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4991 | u8 value) |
| 4992 | { |
| 4993 | int err; |
| 4994 | |
| 4995 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4996 | if (err) |
| 4997 | return err; |
| 4998 | |
| 4999 | err = devlink_fmsg_u8_put(fmsg, value); |
| 5000 | if (err) |
| 5001 | return err; |
| 5002 | |
| 5003 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 5004 | if (err) |
| 5005 | return err; |
| 5006 | |
| 5007 | return 0; |
| 5008 | } |
| 5009 | EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put); |
| 5010 | |
| 5011 | int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 5012 | u32 value) |
| 5013 | { |
| 5014 | int err; |
| 5015 | |
| 5016 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 5017 | if (err) |
| 5018 | return err; |
| 5019 | |
| 5020 | err = devlink_fmsg_u32_put(fmsg, value); |
| 5021 | if (err) |
| 5022 | return err; |
| 5023 | |
| 5024 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 5025 | if (err) |
| 5026 | return err; |
| 5027 | |
| 5028 | return 0; |
| 5029 | } |
| 5030 | EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put); |
| 5031 | |
| 5032 | int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 5033 | u64 value) |
| 5034 | { |
| 5035 | int err; |
| 5036 | |
| 5037 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 5038 | if (err) |
| 5039 | return err; |
| 5040 | |
| 5041 | err = devlink_fmsg_u64_put(fmsg, value); |
| 5042 | if (err) |
| 5043 | return err; |
| 5044 | |
| 5045 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 5046 | if (err) |
| 5047 | return err; |
| 5048 | |
| 5049 | return 0; |
| 5050 | } |
| 5051 | EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put); |
| 5052 | |
| 5053 | int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 5054 | const char *value) |
| 5055 | { |
| 5056 | int err; |
| 5057 | |
| 5058 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 5059 | if (err) |
| 5060 | return err; |
| 5061 | |
| 5062 | err = devlink_fmsg_string_put(fmsg, value); |
| 5063 | if (err) |
| 5064 | return err; |
| 5065 | |
| 5066 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 5067 | if (err) |
| 5068 | return err; |
| 5069 | |
| 5070 | return 0; |
| 5071 | } |
| 5072 | EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put); |
| 5073 | |
| 5074 | int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name, |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 5075 | const void *value, u32 value_len) |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5076 | { |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 5077 | u32 data_size; |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 5078 | int end_err; |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 5079 | u32 offset; |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5080 | int err; |
| 5081 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 5082 | err = devlink_fmsg_binary_pair_nest_start(fmsg, name); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5083 | if (err) |
| 5084 | return err; |
| 5085 | |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 5086 | for (offset = 0; offset < value_len; offset += data_size) { |
| 5087 | data_size = value_len - offset; |
| 5088 | if (data_size > DEVLINK_FMSG_MAX_SIZE) |
| 5089 | data_size = DEVLINK_FMSG_MAX_SIZE; |
| 5090 | err = devlink_fmsg_binary_put(fmsg, value + offset, data_size); |
| 5091 | if (err) |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 5092 | break; |
| 5093 | /* Exit from loop with a break (instead of |
| 5094 | * return) to make sure putting_binary is turned off in |
| 5095 | * devlink_fmsg_binary_pair_nest_end |
| 5096 | */ |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 5097 | } |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5098 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 5099 | end_err = devlink_fmsg_binary_pair_nest_end(fmsg); |
| 5100 | if (end_err) |
| 5101 | err = end_err; |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5102 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 5103 | return err; |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5104 | } |
| 5105 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put); |
| 5106 | |
| 5107 | static int |
| 5108 | devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb) |
| 5109 | { |
| 5110 | switch (msg->nla_type) { |
| 5111 | case NLA_FLAG: |
| 5112 | case NLA_U8: |
| 5113 | case NLA_U32: |
| 5114 | case NLA_U64: |
| 5115 | case NLA_NUL_STRING: |
| 5116 | case NLA_BINARY: |
| 5117 | return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE, |
| 5118 | msg->nla_type); |
| 5119 | default: |
| 5120 | return -EINVAL; |
| 5121 | } |
| 5122 | } |
| 5123 | |
| 5124 | static int |
| 5125 | devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb) |
| 5126 | { |
| 5127 | int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; |
| 5128 | u8 tmp; |
| 5129 | |
| 5130 | switch (msg->nla_type) { |
| 5131 | case NLA_FLAG: |
| 5132 | /* Always provide flag data, regardless of its value */ |
| 5133 | tmp = *(bool *) msg->value; |
| 5134 | |
| 5135 | return nla_put_u8(skb, attrtype, tmp); |
| 5136 | case NLA_U8: |
| 5137 | return nla_put_u8(skb, attrtype, *(u8 *) msg->value); |
| 5138 | case NLA_U32: |
| 5139 | return nla_put_u32(skb, attrtype, *(u32 *) msg->value); |
| 5140 | case NLA_U64: |
| 5141 | return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value, |
| 5142 | DEVLINK_ATTR_PAD); |
| 5143 | case NLA_NUL_STRING: |
| 5144 | return nla_put_string(skb, attrtype, (char *) &msg->value); |
| 5145 | case NLA_BINARY: |
| 5146 | return nla_put(skb, attrtype, msg->len, (void *) &msg->value); |
| 5147 | default: |
| 5148 | return -EINVAL; |
| 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | static int |
| 5153 | devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb, |
| 5154 | int *start) |
| 5155 | { |
| 5156 | struct devlink_fmsg_item *item; |
| 5157 | struct nlattr *fmsg_nlattr; |
| 5158 | int i = 0; |
| 5159 | int err; |
| 5160 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5161 | fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5162 | if (!fmsg_nlattr) |
| 5163 | return -EMSGSIZE; |
| 5164 | |
| 5165 | list_for_each_entry(item, &fmsg->item_list, list) { |
| 5166 | if (i < *start) { |
| 5167 | i++; |
| 5168 | continue; |
| 5169 | } |
| 5170 | |
| 5171 | switch (item->attrtype) { |
| 5172 | case DEVLINK_ATTR_FMSG_OBJ_NEST_START: |
| 5173 | case DEVLINK_ATTR_FMSG_PAIR_NEST_START: |
| 5174 | case DEVLINK_ATTR_FMSG_ARR_NEST_START: |
| 5175 | case DEVLINK_ATTR_FMSG_NEST_END: |
| 5176 | err = nla_put_flag(skb, item->attrtype); |
| 5177 | break; |
| 5178 | case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA: |
| 5179 | err = devlink_fmsg_item_fill_type(item, skb); |
| 5180 | if (err) |
| 5181 | break; |
| 5182 | err = devlink_fmsg_item_fill_data(item, skb); |
| 5183 | break; |
| 5184 | case DEVLINK_ATTR_FMSG_OBJ_NAME: |
| 5185 | err = nla_put_string(skb, item->attrtype, |
| 5186 | (char *) &item->value); |
| 5187 | break; |
| 5188 | default: |
| 5189 | err = -EINVAL; |
| 5190 | break; |
| 5191 | } |
| 5192 | if (!err) |
| 5193 | *start = ++i; |
| 5194 | else |
| 5195 | break; |
| 5196 | } |
| 5197 | |
| 5198 | nla_nest_end(skb, fmsg_nlattr); |
| 5199 | return err; |
| 5200 | } |
| 5201 | |
| 5202 | static int devlink_fmsg_snd(struct devlink_fmsg *fmsg, |
| 5203 | struct genl_info *info, |
| 5204 | enum devlink_command cmd, int flags) |
| 5205 | { |
| 5206 | struct nlmsghdr *nlh; |
| 5207 | struct sk_buff *skb; |
| 5208 | bool last = false; |
| 5209 | int index = 0; |
| 5210 | void *hdr; |
| 5211 | int err; |
| 5212 | |
| 5213 | while (!last) { |
| 5214 | int tmp_index = index; |
| 5215 | |
| 5216 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 5217 | if (!skb) |
| 5218 | return -ENOMEM; |
| 5219 | |
| 5220 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 5221 | &devlink_nl_family, flags | NLM_F_MULTI, cmd); |
| 5222 | if (!hdr) { |
| 5223 | err = -EMSGSIZE; |
| 5224 | goto nla_put_failure; |
| 5225 | } |
| 5226 | |
| 5227 | err = devlink_fmsg_prepare_skb(fmsg, skb, &index); |
| 5228 | if (!err) |
| 5229 | last = true; |
| 5230 | else if (err != -EMSGSIZE || tmp_index == index) |
| 5231 | goto nla_put_failure; |
| 5232 | |
| 5233 | genlmsg_end(skb, hdr); |
| 5234 | err = genlmsg_reply(skb, info); |
| 5235 | if (err) |
| 5236 | return err; |
| 5237 | } |
| 5238 | |
| 5239 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 5240 | if (!skb) |
| 5241 | return -ENOMEM; |
| 5242 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 5243 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 5244 | if (!nlh) { |
| 5245 | err = -EMSGSIZE; |
| 5246 | goto nla_put_failure; |
| 5247 | } |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5248 | |
Li RongQing | fde55ea | 2019-02-11 19:09:07 +0800 | [diff] [blame] | 5249 | return genlmsg_reply(skb, info); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5250 | |
| 5251 | nla_put_failure: |
| 5252 | nlmsg_free(skb); |
| 5253 | return err; |
| 5254 | } |
| 5255 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5256 | static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb, |
| 5257 | struct netlink_callback *cb, |
| 5258 | enum devlink_command cmd) |
| 5259 | { |
| 5260 | int index = cb->args[0]; |
| 5261 | int tmp_index = index; |
| 5262 | void *hdr; |
| 5263 | int err; |
| 5264 | |
| 5265 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 5266 | &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd); |
| 5267 | if (!hdr) { |
| 5268 | err = -EMSGSIZE; |
| 5269 | goto nla_put_failure; |
| 5270 | } |
| 5271 | |
| 5272 | err = devlink_fmsg_prepare_skb(fmsg, skb, &index); |
| 5273 | if ((err && err != -EMSGSIZE) || tmp_index == index) |
| 5274 | goto nla_put_failure; |
| 5275 | |
| 5276 | cb->args[0] = index; |
| 5277 | genlmsg_end(skb, hdr); |
| 5278 | return skb->len; |
| 5279 | |
| 5280 | nla_put_failure: |
| 5281 | genlmsg_cancel(skb, hdr); |
| 5282 | return err; |
| 5283 | } |
| 5284 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5285 | struct devlink_health_reporter { |
| 5286 | struct list_head list; |
| 5287 | void *priv; |
| 5288 | const struct devlink_health_reporter_ops *ops; |
| 5289 | struct devlink *devlink; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5290 | struct devlink_fmsg *dump_fmsg; |
| 5291 | struct mutex dump_lock; /* lock parallel read/write from dump buffers */ |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5292 | u64 graceful_period; |
| 5293 | bool auto_recover; |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5294 | bool auto_dump; |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5295 | u8 health_state; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5296 | u64 dump_ts; |
Aya Levin | d279505 | 2019-11-10 14:11:56 +0200 | [diff] [blame] | 5297 | u64 dump_real_ts; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5298 | u64 error_count; |
| 5299 | u64 recovery_count; |
| 5300 | u64 last_recovery_ts; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5301 | refcount_t refcount; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5302 | }; |
| 5303 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5304 | void * |
| 5305 | devlink_health_reporter_priv(struct devlink_health_reporter *reporter) |
| 5306 | { |
| 5307 | return reporter->priv; |
| 5308 | } |
| 5309 | EXPORT_SYMBOL_GPL(devlink_health_reporter_priv); |
| 5310 | |
| 5311 | static struct devlink_health_reporter * |
| 5312 | devlink_health_reporter_find_by_name(struct devlink *devlink, |
| 5313 | const char *reporter_name) |
| 5314 | { |
| 5315 | struct devlink_health_reporter *reporter; |
| 5316 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5317 | lockdep_assert_held(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5318 | list_for_each_entry(reporter, &devlink->reporter_list, list) |
| 5319 | if (!strcmp(reporter->ops->name, reporter_name)) |
| 5320 | return reporter; |
| 5321 | return NULL; |
| 5322 | } |
| 5323 | |
Vladyslav Tarasiuk | c57544b | 2020-07-10 15:25:07 +0300 | [diff] [blame^] | 5324 | static struct devlink_health_reporter * |
| 5325 | __devlink_health_reporter_create(struct devlink *devlink, |
| 5326 | const struct devlink_health_reporter_ops *ops, |
| 5327 | u64 graceful_period, void *priv) |
| 5328 | { |
| 5329 | struct devlink_health_reporter *reporter; |
| 5330 | |
| 5331 | if (WARN_ON(graceful_period && !ops->recover)) |
| 5332 | return ERR_PTR(-EINVAL); |
| 5333 | |
| 5334 | reporter = kzalloc(sizeof(*reporter), GFP_KERNEL); |
| 5335 | if (!reporter) |
| 5336 | return ERR_PTR(-ENOMEM); |
| 5337 | |
| 5338 | reporter->priv = priv; |
| 5339 | reporter->ops = ops; |
| 5340 | reporter->devlink = devlink; |
| 5341 | reporter->graceful_period = graceful_period; |
| 5342 | reporter->auto_recover = !!ops->recover; |
| 5343 | reporter->auto_dump = !!ops->dump; |
| 5344 | mutex_init(&reporter->dump_lock); |
| 5345 | refcount_set(&reporter->refcount, 1); |
| 5346 | return reporter; |
| 5347 | } |
| 5348 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5349 | /** |
| 5350 | * devlink_health_reporter_create - create devlink health reporter |
| 5351 | * |
| 5352 | * @devlink: devlink |
| 5353 | * @ops: ops |
| 5354 | * @graceful_period: to avoid recovery loops, in msecs |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5355 | * @priv: priv |
| 5356 | */ |
| 5357 | struct devlink_health_reporter * |
| 5358 | devlink_health_reporter_create(struct devlink *devlink, |
| 5359 | const struct devlink_health_reporter_ops *ops, |
Eran Ben Elisha | ba7d16c | 2020-03-29 14:05:54 +0300 | [diff] [blame] | 5360 | u64 graceful_period, void *priv) |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5361 | { |
| 5362 | struct devlink_health_reporter *reporter; |
| 5363 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5364 | mutex_lock(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5365 | if (devlink_health_reporter_find_by_name(devlink, ops->name)) { |
| 5366 | reporter = ERR_PTR(-EEXIST); |
| 5367 | goto unlock; |
| 5368 | } |
| 5369 | |
Vladyslav Tarasiuk | c57544b | 2020-07-10 15:25:07 +0300 | [diff] [blame^] | 5370 | reporter = __devlink_health_reporter_create(devlink, ops, |
| 5371 | graceful_period, priv); |
| 5372 | if (IS_ERR(reporter)) |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5373 | goto unlock; |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5374 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5375 | list_add_tail(&reporter->list, &devlink->reporter_list); |
| 5376 | unlock: |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5377 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5378 | return reporter; |
| 5379 | } |
| 5380 | EXPORT_SYMBOL_GPL(devlink_health_reporter_create); |
| 5381 | |
| 5382 | /** |
| 5383 | * devlink_health_reporter_destroy - destroy devlink health reporter |
| 5384 | * |
| 5385 | * @reporter: devlink health reporter to destroy |
| 5386 | */ |
| 5387 | void |
| 5388 | devlink_health_reporter_destroy(struct devlink_health_reporter *reporter) |
| 5389 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5390 | mutex_lock(&reporter->devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5391 | list_del(&reporter->list); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5392 | mutex_unlock(&reporter->devlink->reporters_lock); |
| 5393 | while (refcount_read(&reporter->refcount) > 1) |
| 5394 | msleep(100); |
Jiri Pirko | 375cf8c | 2019-03-24 11:14:24 +0100 | [diff] [blame] | 5395 | mutex_destroy(&reporter->dump_lock); |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5396 | if (reporter->dump_fmsg) |
| 5397 | devlink_fmsg_free(reporter->dump_fmsg); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5398 | kfree(reporter); |
| 5399 | } |
| 5400 | EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy); |
| 5401 | |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5402 | static int |
| 5403 | devlink_nl_health_reporter_fill(struct sk_buff *msg, |
| 5404 | struct devlink *devlink, |
| 5405 | struct devlink_health_reporter *reporter, |
| 5406 | enum devlink_command cmd, u32 portid, |
| 5407 | u32 seq, int flags) |
| 5408 | { |
| 5409 | struct nlattr *reporter_attr; |
| 5410 | void *hdr; |
| 5411 | |
| 5412 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 5413 | if (!hdr) |
| 5414 | return -EMSGSIZE; |
| 5415 | |
| 5416 | if (devlink_nl_put_handle(msg, devlink)) |
| 5417 | goto genlmsg_cancel; |
| 5418 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5419 | reporter_attr = nla_nest_start_noflag(msg, |
| 5420 | DEVLINK_ATTR_HEALTH_REPORTER); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5421 | if (!reporter_attr) |
| 5422 | goto genlmsg_cancel; |
| 5423 | if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME, |
| 5424 | reporter->ops->name)) |
| 5425 | goto reporter_nest_cancel; |
| 5426 | if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE, |
| 5427 | reporter->health_state)) |
| 5428 | goto reporter_nest_cancel; |
Aya Levin | 5471952 | 2019-02-21 14:12:01 +0200 | [diff] [blame] | 5429 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5430 | reporter->error_count, DEVLINK_ATTR_PAD)) |
| 5431 | goto reporter_nest_cancel; |
Aya Levin | 5471952 | 2019-02-21 14:12:01 +0200 | [diff] [blame] | 5432 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5433 | reporter->recovery_count, DEVLINK_ATTR_PAD)) |
| 5434 | goto reporter_nest_cancel; |
Aya Levin | 574b1e1 | 2019-02-21 14:12:02 +0200 | [diff] [blame] | 5435 | if (reporter->ops->recover && |
| 5436 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5437 | reporter->graceful_period, |
| 5438 | DEVLINK_ATTR_PAD)) |
| 5439 | goto reporter_nest_cancel; |
Aya Levin | 574b1e1 | 2019-02-21 14:12:02 +0200 | [diff] [blame] | 5440 | if (reporter->ops->recover && |
| 5441 | nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5442 | reporter->auto_recover)) |
| 5443 | goto reporter_nest_cancel; |
| 5444 | if (reporter->dump_fmsg && |
| 5445 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS, |
| 5446 | jiffies_to_msecs(reporter->dump_ts), |
| 5447 | DEVLINK_ATTR_PAD)) |
| 5448 | goto reporter_nest_cancel; |
Aya Levin | d279505 | 2019-11-10 14:11:56 +0200 | [diff] [blame] | 5449 | if (reporter->dump_fmsg && |
| 5450 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS, |
| 5451 | reporter->dump_real_ts, DEVLINK_ATTR_PAD)) |
| 5452 | goto reporter_nest_cancel; |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5453 | if (reporter->ops->dump && |
| 5454 | nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP, |
| 5455 | reporter->auto_dump)) |
| 5456 | goto reporter_nest_cancel; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5457 | |
| 5458 | nla_nest_end(msg, reporter_attr); |
| 5459 | genlmsg_end(msg, hdr); |
| 5460 | return 0; |
| 5461 | |
| 5462 | reporter_nest_cancel: |
| 5463 | nla_nest_end(msg, reporter_attr); |
| 5464 | genlmsg_cancel: |
| 5465 | genlmsg_cancel(msg, hdr); |
| 5466 | return -EMSGSIZE; |
| 5467 | } |
| 5468 | |
Vikas Gupta | 97ff3bd | 2020-01-02 21:18:10 +0530 | [diff] [blame] | 5469 | static void devlink_recover_notify(struct devlink_health_reporter *reporter, |
| 5470 | enum devlink_command cmd) |
| 5471 | { |
| 5472 | struct sk_buff *msg; |
| 5473 | int err; |
| 5474 | |
| 5475 | WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER); |
| 5476 | |
| 5477 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 5478 | if (!msg) |
| 5479 | return; |
| 5480 | |
| 5481 | err = devlink_nl_health_reporter_fill(msg, reporter->devlink, |
| 5482 | reporter, cmd, 0, 0, 0); |
| 5483 | if (err) { |
| 5484 | nlmsg_free(msg); |
| 5485 | return; |
| 5486 | } |
| 5487 | |
| 5488 | genlmsg_multicast_netns(&devlink_nl_family, |
| 5489 | devlink_net(reporter->devlink), |
| 5490 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 5491 | } |
| 5492 | |
| 5493 | void |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5494 | devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter) |
| 5495 | { |
| 5496 | reporter->recovery_count++; |
| 5497 | reporter->last_recovery_ts = jiffies; |
| 5498 | } |
| 5499 | EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done); |
| 5500 | |
| 5501 | static int |
| 5502 | devlink_health_reporter_recover(struct devlink_health_reporter *reporter, |
| 5503 | void *priv_ctx, struct netlink_ext_ack *extack) |
| 5504 | { |
| 5505 | int err; |
| 5506 | |
| 5507 | if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY) |
| 5508 | return 0; |
| 5509 | |
| 5510 | if (!reporter->ops->recover) |
| 5511 | return -EOPNOTSUPP; |
| 5512 | |
| 5513 | err = reporter->ops->recover(reporter, priv_ctx, extack); |
| 5514 | if (err) |
| 5515 | return err; |
| 5516 | |
| 5517 | devlink_health_reporter_recovery_done(reporter); |
| 5518 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY; |
| 5519 | devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); |
| 5520 | |
| 5521 | return 0; |
| 5522 | } |
| 5523 | |
| 5524 | static void |
| 5525 | devlink_health_dump_clear(struct devlink_health_reporter *reporter) |
| 5526 | { |
| 5527 | if (!reporter->dump_fmsg) |
| 5528 | return; |
| 5529 | devlink_fmsg_free(reporter->dump_fmsg); |
| 5530 | reporter->dump_fmsg = NULL; |
| 5531 | } |
| 5532 | |
| 5533 | static int devlink_health_do_dump(struct devlink_health_reporter *reporter, |
| 5534 | void *priv_ctx, |
| 5535 | struct netlink_ext_ack *extack) |
| 5536 | { |
| 5537 | int err; |
| 5538 | |
| 5539 | if (!reporter->ops->dump) |
| 5540 | return 0; |
| 5541 | |
| 5542 | if (reporter->dump_fmsg) |
| 5543 | return 0; |
| 5544 | |
| 5545 | reporter->dump_fmsg = devlink_fmsg_alloc(); |
| 5546 | if (!reporter->dump_fmsg) { |
| 5547 | err = -ENOMEM; |
| 5548 | return err; |
| 5549 | } |
| 5550 | |
| 5551 | err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg); |
| 5552 | if (err) |
| 5553 | goto dump_err; |
| 5554 | |
| 5555 | err = reporter->ops->dump(reporter, reporter->dump_fmsg, |
| 5556 | priv_ctx, extack); |
| 5557 | if (err) |
| 5558 | goto dump_err; |
| 5559 | |
| 5560 | err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg); |
| 5561 | if (err) |
| 5562 | goto dump_err; |
| 5563 | |
| 5564 | reporter->dump_ts = jiffies; |
| 5565 | reporter->dump_real_ts = ktime_get_real_ns(); |
| 5566 | |
| 5567 | return 0; |
| 5568 | |
| 5569 | dump_err: |
| 5570 | devlink_health_dump_clear(reporter); |
| 5571 | return err; |
| 5572 | } |
| 5573 | |
| 5574 | int devlink_health_report(struct devlink_health_reporter *reporter, |
| 5575 | const char *msg, void *priv_ctx) |
| 5576 | { |
| 5577 | enum devlink_health_reporter_state prev_health_state; |
| 5578 | struct devlink *devlink = reporter->devlink; |
Aya Levin | bea0c5c | 2020-05-04 11:27:46 +0300 | [diff] [blame] | 5579 | unsigned long recover_ts_threshold; |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5580 | |
| 5581 | /* write a log message of the current error */ |
| 5582 | WARN_ON(!msg); |
| 5583 | trace_devlink_health_report(devlink, reporter->ops->name, msg); |
| 5584 | reporter->error_count++; |
| 5585 | prev_health_state = reporter->health_state; |
| 5586 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; |
| 5587 | devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); |
| 5588 | |
| 5589 | /* abort if the previous error wasn't recovered */ |
Aya Levin | bea0c5c | 2020-05-04 11:27:46 +0300 | [diff] [blame] | 5590 | recover_ts_threshold = reporter->last_recovery_ts + |
| 5591 | msecs_to_jiffies(reporter->graceful_period); |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5592 | if (reporter->auto_recover && |
| 5593 | (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY || |
Aya Levin | bea0c5c | 2020-05-04 11:27:46 +0300 | [diff] [blame] | 5594 | (reporter->last_recovery_ts && reporter->recovery_count && |
| 5595 | time_is_after_jiffies(recover_ts_threshold)))) { |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5596 | trace_devlink_health_recover_aborted(devlink, |
| 5597 | reporter->ops->name, |
| 5598 | reporter->health_state, |
| 5599 | jiffies - |
| 5600 | reporter->last_recovery_ts); |
| 5601 | return -ECANCELED; |
| 5602 | } |
| 5603 | |
| 5604 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; |
| 5605 | |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5606 | if (reporter->auto_dump) { |
| 5607 | mutex_lock(&reporter->dump_lock); |
| 5608 | /* store current dump of current error, for later analysis */ |
| 5609 | devlink_health_do_dump(reporter, priv_ctx, NULL); |
| 5610 | mutex_unlock(&reporter->dump_lock); |
| 5611 | } |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5612 | |
| 5613 | if (reporter->auto_recover) |
| 5614 | return devlink_health_reporter_recover(reporter, |
| 5615 | priv_ctx, NULL); |
| 5616 | |
| 5617 | return 0; |
| 5618 | } |
| 5619 | EXPORT_SYMBOL_GPL(devlink_health_report); |
| 5620 | |
| 5621 | static struct devlink_health_reporter * |
| 5622 | devlink_health_reporter_get_from_attrs(struct devlink *devlink, |
| 5623 | struct nlattr **attrs) |
| 5624 | { |
| 5625 | struct devlink_health_reporter *reporter; |
| 5626 | char *reporter_name; |
| 5627 | |
| 5628 | if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]) |
| 5629 | return NULL; |
| 5630 | |
| 5631 | reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]); |
| 5632 | mutex_lock(&devlink->reporters_lock); |
| 5633 | reporter = devlink_health_reporter_find_by_name(devlink, reporter_name); |
| 5634 | if (reporter) |
| 5635 | refcount_inc(&reporter->refcount); |
| 5636 | mutex_unlock(&devlink->reporters_lock); |
| 5637 | return reporter; |
| 5638 | } |
| 5639 | |
| 5640 | static struct devlink_health_reporter * |
| 5641 | devlink_health_reporter_get_from_info(struct devlink *devlink, |
| 5642 | struct genl_info *info) |
| 5643 | { |
| 5644 | return devlink_health_reporter_get_from_attrs(devlink, info->attrs); |
| 5645 | } |
| 5646 | |
| 5647 | static struct devlink_health_reporter * |
| 5648 | devlink_health_reporter_get_from_cb(struct netlink_callback *cb) |
| 5649 | { |
| 5650 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
| 5651 | struct devlink_health_reporter *reporter; |
| 5652 | struct nlattr **attrs = info->attrs; |
| 5653 | struct devlink *devlink; |
| 5654 | |
| 5655 | mutex_lock(&devlink_mutex); |
| 5656 | devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs); |
| 5657 | if (IS_ERR(devlink)) |
| 5658 | goto unlock; |
| 5659 | |
| 5660 | reporter = devlink_health_reporter_get_from_attrs(devlink, attrs); |
| 5661 | mutex_unlock(&devlink_mutex); |
| 5662 | return reporter; |
| 5663 | unlock: |
| 5664 | mutex_unlock(&devlink_mutex); |
| 5665 | return NULL; |
| 5666 | } |
| 5667 | |
| 5668 | static void |
| 5669 | devlink_health_reporter_put(struct devlink_health_reporter *reporter) |
| 5670 | { |
| 5671 | refcount_dec(&reporter->refcount); |
| 5672 | } |
| 5673 | |
| 5674 | void |
Vikas Gupta | 97ff3bd | 2020-01-02 21:18:10 +0530 | [diff] [blame] | 5675 | devlink_health_reporter_state_update(struct devlink_health_reporter *reporter, |
| 5676 | enum devlink_health_reporter_state state) |
| 5677 | { |
| 5678 | if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY && |
| 5679 | state != DEVLINK_HEALTH_REPORTER_STATE_ERROR)) |
| 5680 | return; |
| 5681 | |
| 5682 | if (reporter->health_state == state) |
| 5683 | return; |
| 5684 | |
| 5685 | reporter->health_state = state; |
| 5686 | trace_devlink_health_reporter_state_update(reporter->devlink, |
| 5687 | reporter->ops->name, state); |
| 5688 | devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); |
| 5689 | } |
| 5690 | EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update); |
| 5691 | |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5692 | static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb, |
| 5693 | struct genl_info *info) |
| 5694 | { |
| 5695 | struct devlink *devlink = info->user_ptr[0]; |
| 5696 | struct devlink_health_reporter *reporter; |
| 5697 | struct sk_buff *msg; |
| 5698 | int err; |
| 5699 | |
| 5700 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5701 | if (!reporter) |
| 5702 | return -EINVAL; |
| 5703 | |
| 5704 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5705 | if (!msg) { |
| 5706 | err = -ENOMEM; |
| 5707 | goto out; |
| 5708 | } |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5709 | |
| 5710 | err = devlink_nl_health_reporter_fill(msg, devlink, reporter, |
| 5711 | DEVLINK_CMD_HEALTH_REPORTER_GET, |
| 5712 | info->snd_portid, info->snd_seq, |
| 5713 | 0); |
| 5714 | if (err) { |
| 5715 | nlmsg_free(msg); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5716 | goto out; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5717 | } |
| 5718 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5719 | err = genlmsg_reply(msg, info); |
| 5720 | out: |
| 5721 | devlink_health_reporter_put(reporter); |
| 5722 | return err; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | static int |
| 5726 | devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg, |
| 5727 | struct netlink_callback *cb) |
| 5728 | { |
| 5729 | struct devlink_health_reporter *reporter; |
| 5730 | struct devlink *devlink; |
| 5731 | int start = cb->args[0]; |
| 5732 | int idx = 0; |
| 5733 | int err; |
| 5734 | |
| 5735 | mutex_lock(&devlink_mutex); |
| 5736 | list_for_each_entry(devlink, &devlink_list, list) { |
| 5737 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 5738 | continue; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5739 | mutex_lock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5740 | list_for_each_entry(reporter, &devlink->reporter_list, |
| 5741 | list) { |
| 5742 | if (idx < start) { |
| 5743 | idx++; |
| 5744 | continue; |
| 5745 | } |
| 5746 | err = devlink_nl_health_reporter_fill(msg, devlink, |
| 5747 | reporter, |
| 5748 | DEVLINK_CMD_HEALTH_REPORTER_GET, |
| 5749 | NETLINK_CB(cb->skb).portid, |
| 5750 | cb->nlh->nlmsg_seq, |
| 5751 | NLM_F_MULTI); |
| 5752 | if (err) { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5753 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5754 | goto out; |
| 5755 | } |
| 5756 | idx++; |
| 5757 | } |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5758 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5759 | } |
| 5760 | out: |
| 5761 | mutex_unlock(&devlink_mutex); |
| 5762 | |
| 5763 | cb->args[0] = idx; |
| 5764 | return msg->len; |
| 5765 | } |
| 5766 | |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5767 | static int |
| 5768 | devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb, |
| 5769 | struct genl_info *info) |
| 5770 | { |
| 5771 | struct devlink *devlink = info->user_ptr[0]; |
| 5772 | struct devlink_health_reporter *reporter; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5773 | int err; |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5774 | |
| 5775 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5776 | if (!reporter) |
| 5777 | return -EINVAL; |
| 5778 | |
| 5779 | if (!reporter->ops->recover && |
| 5780 | (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] || |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5781 | info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) { |
| 5782 | err = -EOPNOTSUPP; |
| 5783 | goto out; |
| 5784 | } |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5785 | if (!reporter->ops->dump && |
| 5786 | info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) { |
| 5787 | err = -EOPNOTSUPP; |
| 5788 | goto out; |
| 5789 | } |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5790 | |
| 5791 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]) |
| 5792 | reporter->graceful_period = |
| 5793 | nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]); |
| 5794 | |
| 5795 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]) |
| 5796 | reporter->auto_recover = |
| 5797 | nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]); |
| 5798 | |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5799 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) |
| 5800 | reporter->auto_dump = |
| 5801 | nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]); |
| 5802 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5803 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5804 | return 0; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5805 | out: |
| 5806 | devlink_health_reporter_put(reporter); |
| 5807 | return err; |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5808 | } |
| 5809 | |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5810 | static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb, |
| 5811 | struct genl_info *info) |
| 5812 | { |
| 5813 | struct devlink *devlink = info->user_ptr[0]; |
| 5814 | struct devlink_health_reporter *reporter; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5815 | int err; |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5816 | |
| 5817 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5818 | if (!reporter) |
| 5819 | return -EINVAL; |
| 5820 | |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5821 | err = devlink_health_reporter_recover(reporter, NULL, info->extack); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5822 | |
| 5823 | devlink_health_reporter_put(reporter); |
| 5824 | return err; |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5825 | } |
| 5826 | |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5827 | static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb, |
| 5828 | struct genl_info *info) |
| 5829 | { |
| 5830 | struct devlink *devlink = info->user_ptr[0]; |
| 5831 | struct devlink_health_reporter *reporter; |
| 5832 | struct devlink_fmsg *fmsg; |
| 5833 | int err; |
| 5834 | |
| 5835 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5836 | if (!reporter) |
| 5837 | return -EINVAL; |
| 5838 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5839 | if (!reporter->ops->diagnose) { |
| 5840 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5841 | return -EOPNOTSUPP; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5842 | } |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5843 | |
| 5844 | fmsg = devlink_fmsg_alloc(); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5845 | if (!fmsg) { |
| 5846 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5847 | return -ENOMEM; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5848 | } |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5849 | |
| 5850 | err = devlink_fmsg_obj_nest_start(fmsg); |
| 5851 | if (err) |
| 5852 | goto out; |
| 5853 | |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5854 | err = reporter->ops->diagnose(reporter, fmsg, info->extack); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5855 | if (err) |
| 5856 | goto out; |
| 5857 | |
| 5858 | err = devlink_fmsg_obj_nest_end(fmsg); |
| 5859 | if (err) |
| 5860 | goto out; |
| 5861 | |
| 5862 | err = devlink_fmsg_snd(fmsg, info, |
| 5863 | DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0); |
| 5864 | |
| 5865 | out: |
| 5866 | devlink_fmsg_free(fmsg); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5867 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5868 | return err; |
| 5869 | } |
| 5870 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5871 | static int |
| 5872 | devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb, |
| 5873 | struct netlink_callback *cb) |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5874 | { |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5875 | struct devlink_health_reporter *reporter; |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5876 | u64 start = cb->args[0]; |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5877 | int err; |
| 5878 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5879 | reporter = devlink_health_reporter_get_from_cb(cb); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5880 | if (!reporter) |
| 5881 | return -EINVAL; |
| 5882 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5883 | if (!reporter->ops->dump) { |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5884 | err = -EOPNOTSUPP; |
| 5885 | goto out; |
| 5886 | } |
| 5887 | mutex_lock(&reporter->dump_lock); |
| 5888 | if (!start) { |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5889 | err = devlink_health_do_dump(reporter, NULL, cb->extack); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5890 | if (err) |
| 5891 | goto unlock; |
| 5892 | cb->args[1] = reporter->dump_ts; |
| 5893 | } |
| 5894 | if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) { |
| 5895 | NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry"); |
| 5896 | err = -EAGAIN; |
| 5897 | goto unlock; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5898 | } |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5899 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5900 | err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb, |
| 5901 | DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET); |
| 5902 | unlock: |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5903 | mutex_unlock(&reporter->dump_lock); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5904 | out: |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5905 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5906 | return err; |
| 5907 | } |
| 5908 | |
| 5909 | static int |
| 5910 | devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb, |
| 5911 | struct genl_info *info) |
| 5912 | { |
| 5913 | struct devlink *devlink = info->user_ptr[0]; |
| 5914 | struct devlink_health_reporter *reporter; |
| 5915 | |
| 5916 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5917 | if (!reporter) |
| 5918 | return -EINVAL; |
| 5919 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5920 | if (!reporter->ops->dump) { |
| 5921 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5922 | return -EOPNOTSUPP; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5923 | } |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5924 | |
| 5925 | mutex_lock(&reporter->dump_lock); |
| 5926 | devlink_health_dump_clear(reporter); |
| 5927 | mutex_unlock(&reporter->dump_lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5928 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5929 | return 0; |
| 5930 | } |
| 5931 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5932 | struct devlink_stats { |
| 5933 | u64 rx_bytes; |
| 5934 | u64 rx_packets; |
| 5935 | struct u64_stats_sync syncp; |
| 5936 | }; |
| 5937 | |
| 5938 | /** |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 5939 | * struct devlink_trap_policer_item - Packet trap policer attributes. |
| 5940 | * @policer: Immutable packet trap policer attributes. |
| 5941 | * @rate: Rate in packets / sec. |
| 5942 | * @burst: Burst size in packets. |
| 5943 | * @list: trap_policer_list member. |
| 5944 | * |
| 5945 | * Describes packet trap policer attributes. Created by devlink during trap |
| 5946 | * policer registration. |
| 5947 | */ |
| 5948 | struct devlink_trap_policer_item { |
| 5949 | const struct devlink_trap_policer *policer; |
| 5950 | u64 rate; |
| 5951 | u64 burst; |
| 5952 | struct list_head list; |
| 5953 | }; |
| 5954 | |
| 5955 | /** |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5956 | * struct devlink_trap_group_item - Packet trap group attributes. |
| 5957 | * @group: Immutable packet trap group attributes. |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 5958 | * @policer_item: Associated policer item. Can be NULL. |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5959 | * @list: trap_group_list member. |
| 5960 | * @stats: Trap group statistics. |
| 5961 | * |
| 5962 | * Describes packet trap group attributes. Created by devlink during trap |
Ido Schimmel | a09b37f | 2020-03-22 20:48:29 +0200 | [diff] [blame] | 5963 | * group registration. |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5964 | */ |
| 5965 | struct devlink_trap_group_item { |
| 5966 | const struct devlink_trap_group *group; |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 5967 | struct devlink_trap_policer_item *policer_item; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5968 | struct list_head list; |
| 5969 | struct devlink_stats __percpu *stats; |
| 5970 | }; |
| 5971 | |
| 5972 | /** |
| 5973 | * struct devlink_trap_item - Packet trap attributes. |
| 5974 | * @trap: Immutable packet trap attributes. |
| 5975 | * @group_item: Associated group item. |
| 5976 | * @list: trap_list member. |
| 5977 | * @action: Trap action. |
| 5978 | * @stats: Trap statistics. |
| 5979 | * @priv: Driver private information. |
| 5980 | * |
| 5981 | * Describes both mutable and immutable packet trap attributes. Created by |
| 5982 | * devlink during trap registration and used for all trap related operations. |
| 5983 | */ |
| 5984 | struct devlink_trap_item { |
| 5985 | const struct devlink_trap *trap; |
| 5986 | struct devlink_trap_group_item *group_item; |
| 5987 | struct list_head list; |
| 5988 | enum devlink_trap_action action; |
| 5989 | struct devlink_stats __percpu *stats; |
| 5990 | void *priv; |
| 5991 | }; |
| 5992 | |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 5993 | static struct devlink_trap_policer_item * |
| 5994 | devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id) |
| 5995 | { |
| 5996 | struct devlink_trap_policer_item *policer_item; |
| 5997 | |
| 5998 | list_for_each_entry(policer_item, &devlink->trap_policer_list, list) { |
| 5999 | if (policer_item->policer->id == id) |
| 6000 | return policer_item; |
| 6001 | } |
| 6002 | |
| 6003 | return NULL; |
| 6004 | } |
| 6005 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6006 | static struct devlink_trap_item * |
| 6007 | devlink_trap_item_lookup(struct devlink *devlink, const char *name) |
| 6008 | { |
| 6009 | struct devlink_trap_item *trap_item; |
| 6010 | |
| 6011 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
| 6012 | if (!strcmp(trap_item->trap->name, name)) |
| 6013 | return trap_item; |
| 6014 | } |
| 6015 | |
| 6016 | return NULL; |
| 6017 | } |
| 6018 | |
| 6019 | static struct devlink_trap_item * |
| 6020 | devlink_trap_item_get_from_info(struct devlink *devlink, |
| 6021 | struct genl_info *info) |
| 6022 | { |
| 6023 | struct nlattr *attr; |
| 6024 | |
| 6025 | if (!info->attrs[DEVLINK_ATTR_TRAP_NAME]) |
| 6026 | return NULL; |
| 6027 | attr = info->attrs[DEVLINK_ATTR_TRAP_NAME]; |
| 6028 | |
| 6029 | return devlink_trap_item_lookup(devlink, nla_data(attr)); |
| 6030 | } |
| 6031 | |
| 6032 | static int |
| 6033 | devlink_trap_action_get_from_info(struct genl_info *info, |
| 6034 | enum devlink_trap_action *p_trap_action) |
| 6035 | { |
| 6036 | u8 val; |
| 6037 | |
| 6038 | val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]); |
| 6039 | switch (val) { |
| 6040 | case DEVLINK_TRAP_ACTION_DROP: /* fall-through */ |
Ido Schimmel | 9eefeab | 2020-05-29 21:36:39 +0300 | [diff] [blame] | 6041 | case DEVLINK_TRAP_ACTION_TRAP: /* fall-through */ |
| 6042 | case DEVLINK_TRAP_ACTION_MIRROR: |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6043 | *p_trap_action = val; |
| 6044 | break; |
| 6045 | default: |
| 6046 | return -EINVAL; |
| 6047 | } |
| 6048 | |
| 6049 | return 0; |
| 6050 | } |
| 6051 | |
| 6052 | static int devlink_trap_metadata_put(struct sk_buff *msg, |
| 6053 | const struct devlink_trap *trap) |
| 6054 | { |
| 6055 | struct nlattr *attr; |
| 6056 | |
| 6057 | attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA); |
| 6058 | if (!attr) |
| 6059 | return -EMSGSIZE; |
| 6060 | |
| 6061 | if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) && |
| 6062 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT)) |
| 6063 | goto nla_put_failure; |
Jiri Pirko | 85b0589 | 2020-02-25 11:45:19 +0100 | [diff] [blame] | 6064 | if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) && |
| 6065 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE)) |
| 6066 | goto nla_put_failure; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6067 | |
| 6068 | nla_nest_end(msg, attr); |
| 6069 | |
| 6070 | return 0; |
| 6071 | |
| 6072 | nla_put_failure: |
| 6073 | nla_nest_cancel(msg, attr); |
| 6074 | return -EMSGSIZE; |
| 6075 | } |
| 6076 | |
| 6077 | static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats, |
| 6078 | struct devlink_stats *stats) |
| 6079 | { |
| 6080 | int i; |
| 6081 | |
| 6082 | memset(stats, 0, sizeof(*stats)); |
| 6083 | for_each_possible_cpu(i) { |
| 6084 | struct devlink_stats *cpu_stats; |
| 6085 | u64 rx_packets, rx_bytes; |
| 6086 | unsigned int start; |
| 6087 | |
| 6088 | cpu_stats = per_cpu_ptr(trap_stats, i); |
| 6089 | do { |
| 6090 | start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
| 6091 | rx_packets = cpu_stats->rx_packets; |
| 6092 | rx_bytes = cpu_stats->rx_bytes; |
| 6093 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); |
| 6094 | |
| 6095 | stats->rx_packets += rx_packets; |
| 6096 | stats->rx_bytes += rx_bytes; |
| 6097 | } |
| 6098 | } |
| 6099 | |
| 6100 | static int devlink_trap_stats_put(struct sk_buff *msg, |
| 6101 | struct devlink_stats __percpu *trap_stats) |
| 6102 | { |
| 6103 | struct devlink_stats stats; |
| 6104 | struct nlattr *attr; |
| 6105 | |
| 6106 | devlink_trap_stats_read(trap_stats, &stats); |
| 6107 | |
| 6108 | attr = nla_nest_start(msg, DEVLINK_ATTR_STATS); |
| 6109 | if (!attr) |
| 6110 | return -EMSGSIZE; |
| 6111 | |
| 6112 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS, |
| 6113 | stats.rx_packets, DEVLINK_ATTR_PAD)) |
| 6114 | goto nla_put_failure; |
| 6115 | |
| 6116 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES, |
| 6117 | stats.rx_bytes, DEVLINK_ATTR_PAD)) |
| 6118 | goto nla_put_failure; |
| 6119 | |
| 6120 | nla_nest_end(msg, attr); |
| 6121 | |
| 6122 | return 0; |
| 6123 | |
| 6124 | nla_put_failure: |
| 6125 | nla_nest_cancel(msg, attr); |
| 6126 | return -EMSGSIZE; |
| 6127 | } |
| 6128 | |
| 6129 | static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink, |
| 6130 | const struct devlink_trap_item *trap_item, |
| 6131 | enum devlink_command cmd, u32 portid, u32 seq, |
| 6132 | int flags) |
| 6133 | { |
| 6134 | struct devlink_trap_group_item *group_item = trap_item->group_item; |
| 6135 | void *hdr; |
| 6136 | int err; |
| 6137 | |
| 6138 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 6139 | if (!hdr) |
| 6140 | return -EMSGSIZE; |
| 6141 | |
| 6142 | if (devlink_nl_put_handle(msg, devlink)) |
| 6143 | goto nla_put_failure; |
| 6144 | |
| 6145 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME, |
| 6146 | group_item->group->name)) |
| 6147 | goto nla_put_failure; |
| 6148 | |
| 6149 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name)) |
| 6150 | goto nla_put_failure; |
| 6151 | |
| 6152 | if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type)) |
| 6153 | goto nla_put_failure; |
| 6154 | |
| 6155 | if (trap_item->trap->generic && |
| 6156 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC)) |
| 6157 | goto nla_put_failure; |
| 6158 | |
| 6159 | if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action)) |
| 6160 | goto nla_put_failure; |
| 6161 | |
| 6162 | err = devlink_trap_metadata_put(msg, trap_item->trap); |
| 6163 | if (err) |
| 6164 | goto nla_put_failure; |
| 6165 | |
| 6166 | err = devlink_trap_stats_put(msg, trap_item->stats); |
| 6167 | if (err) |
| 6168 | goto nla_put_failure; |
| 6169 | |
| 6170 | genlmsg_end(msg, hdr); |
| 6171 | |
| 6172 | return 0; |
| 6173 | |
| 6174 | nla_put_failure: |
| 6175 | genlmsg_cancel(msg, hdr); |
| 6176 | return -EMSGSIZE; |
| 6177 | } |
| 6178 | |
| 6179 | static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb, |
| 6180 | struct genl_info *info) |
| 6181 | { |
| 6182 | struct netlink_ext_ack *extack = info->extack; |
| 6183 | struct devlink *devlink = info->user_ptr[0]; |
| 6184 | struct devlink_trap_item *trap_item; |
| 6185 | struct sk_buff *msg; |
| 6186 | int err; |
| 6187 | |
| 6188 | if (list_empty(&devlink->trap_list)) |
| 6189 | return -EOPNOTSUPP; |
| 6190 | |
| 6191 | trap_item = devlink_trap_item_get_from_info(devlink, info); |
| 6192 | if (!trap_item) { |
| 6193 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap"); |
| 6194 | return -ENOENT; |
| 6195 | } |
| 6196 | |
| 6197 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 6198 | if (!msg) |
| 6199 | return -ENOMEM; |
| 6200 | |
| 6201 | err = devlink_nl_trap_fill(msg, devlink, trap_item, |
| 6202 | DEVLINK_CMD_TRAP_NEW, info->snd_portid, |
| 6203 | info->snd_seq, 0); |
| 6204 | if (err) |
| 6205 | goto err_trap_fill; |
| 6206 | |
| 6207 | return genlmsg_reply(msg, info); |
| 6208 | |
| 6209 | err_trap_fill: |
| 6210 | nlmsg_free(msg); |
| 6211 | return err; |
| 6212 | } |
| 6213 | |
| 6214 | static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg, |
| 6215 | struct netlink_callback *cb) |
| 6216 | { |
| 6217 | struct devlink_trap_item *trap_item; |
| 6218 | struct devlink *devlink; |
| 6219 | int start = cb->args[0]; |
| 6220 | int idx = 0; |
| 6221 | int err; |
| 6222 | |
| 6223 | mutex_lock(&devlink_mutex); |
| 6224 | list_for_each_entry(devlink, &devlink_list, list) { |
| 6225 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 6226 | continue; |
| 6227 | mutex_lock(&devlink->lock); |
| 6228 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
| 6229 | if (idx < start) { |
| 6230 | idx++; |
| 6231 | continue; |
| 6232 | } |
| 6233 | err = devlink_nl_trap_fill(msg, devlink, trap_item, |
| 6234 | DEVLINK_CMD_TRAP_NEW, |
| 6235 | NETLINK_CB(cb->skb).portid, |
| 6236 | cb->nlh->nlmsg_seq, |
| 6237 | NLM_F_MULTI); |
| 6238 | if (err) { |
| 6239 | mutex_unlock(&devlink->lock); |
| 6240 | goto out; |
| 6241 | } |
| 6242 | idx++; |
| 6243 | } |
| 6244 | mutex_unlock(&devlink->lock); |
| 6245 | } |
| 6246 | out: |
| 6247 | mutex_unlock(&devlink_mutex); |
| 6248 | |
| 6249 | cb->args[0] = idx; |
| 6250 | return msg->len; |
| 6251 | } |
| 6252 | |
| 6253 | static int __devlink_trap_action_set(struct devlink *devlink, |
| 6254 | struct devlink_trap_item *trap_item, |
| 6255 | enum devlink_trap_action trap_action, |
| 6256 | struct netlink_ext_ack *extack) |
| 6257 | { |
| 6258 | int err; |
| 6259 | |
| 6260 | if (trap_item->action != trap_action && |
| 6261 | trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) { |
| 6262 | NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping"); |
| 6263 | return 0; |
| 6264 | } |
| 6265 | |
| 6266 | err = devlink->ops->trap_action_set(devlink, trap_item->trap, |
| 6267 | trap_action); |
| 6268 | if (err) |
| 6269 | return err; |
| 6270 | |
| 6271 | trap_item->action = trap_action; |
| 6272 | |
| 6273 | return 0; |
| 6274 | } |
| 6275 | |
| 6276 | static int devlink_trap_action_set(struct devlink *devlink, |
| 6277 | struct devlink_trap_item *trap_item, |
| 6278 | struct genl_info *info) |
| 6279 | { |
| 6280 | enum devlink_trap_action trap_action; |
| 6281 | int err; |
| 6282 | |
| 6283 | if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION]) |
| 6284 | return 0; |
| 6285 | |
| 6286 | err = devlink_trap_action_get_from_info(info, &trap_action); |
| 6287 | if (err) { |
| 6288 | NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action"); |
| 6289 | return -EINVAL; |
| 6290 | } |
| 6291 | |
| 6292 | return __devlink_trap_action_set(devlink, trap_item, trap_action, |
| 6293 | info->extack); |
| 6294 | } |
| 6295 | |
| 6296 | static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb, |
| 6297 | struct genl_info *info) |
| 6298 | { |
| 6299 | struct netlink_ext_ack *extack = info->extack; |
| 6300 | struct devlink *devlink = info->user_ptr[0]; |
| 6301 | struct devlink_trap_item *trap_item; |
| 6302 | int err; |
| 6303 | |
| 6304 | if (list_empty(&devlink->trap_list)) |
| 6305 | return -EOPNOTSUPP; |
| 6306 | |
| 6307 | trap_item = devlink_trap_item_get_from_info(devlink, info); |
| 6308 | if (!trap_item) { |
| 6309 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap"); |
| 6310 | return -ENOENT; |
| 6311 | } |
| 6312 | |
| 6313 | err = devlink_trap_action_set(devlink, trap_item, info); |
| 6314 | if (err) |
| 6315 | return err; |
| 6316 | |
| 6317 | return 0; |
| 6318 | } |
| 6319 | |
| 6320 | static struct devlink_trap_group_item * |
| 6321 | devlink_trap_group_item_lookup(struct devlink *devlink, const char *name) |
| 6322 | { |
| 6323 | struct devlink_trap_group_item *group_item; |
| 6324 | |
| 6325 | list_for_each_entry(group_item, &devlink->trap_group_list, list) { |
| 6326 | if (!strcmp(group_item->group->name, name)) |
| 6327 | return group_item; |
| 6328 | } |
| 6329 | |
| 6330 | return NULL; |
| 6331 | } |
| 6332 | |
| 6333 | static struct devlink_trap_group_item * |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 6334 | devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id) |
| 6335 | { |
| 6336 | struct devlink_trap_group_item *group_item; |
| 6337 | |
| 6338 | list_for_each_entry(group_item, &devlink->trap_group_list, list) { |
| 6339 | if (group_item->group->id == id) |
| 6340 | return group_item; |
| 6341 | } |
| 6342 | |
| 6343 | return NULL; |
| 6344 | } |
| 6345 | |
| 6346 | static struct devlink_trap_group_item * |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6347 | devlink_trap_group_item_get_from_info(struct devlink *devlink, |
| 6348 | struct genl_info *info) |
| 6349 | { |
| 6350 | char *name; |
| 6351 | |
| 6352 | if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]) |
| 6353 | return NULL; |
| 6354 | name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]); |
| 6355 | |
| 6356 | return devlink_trap_group_item_lookup(devlink, name); |
| 6357 | } |
| 6358 | |
| 6359 | static int |
| 6360 | devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink, |
| 6361 | const struct devlink_trap_group_item *group_item, |
| 6362 | enum devlink_command cmd, u32 portid, u32 seq, |
| 6363 | int flags) |
| 6364 | { |
| 6365 | void *hdr; |
| 6366 | int err; |
| 6367 | |
| 6368 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 6369 | if (!hdr) |
| 6370 | return -EMSGSIZE; |
| 6371 | |
| 6372 | if (devlink_nl_put_handle(msg, devlink)) |
| 6373 | goto nla_put_failure; |
| 6374 | |
| 6375 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME, |
| 6376 | group_item->group->name)) |
| 6377 | goto nla_put_failure; |
| 6378 | |
| 6379 | if (group_item->group->generic && |
| 6380 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC)) |
| 6381 | goto nla_put_failure; |
| 6382 | |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 6383 | if (group_item->policer_item && |
| 6384 | nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID, |
| 6385 | group_item->policer_item->policer->id)) |
| 6386 | goto nla_put_failure; |
| 6387 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6388 | err = devlink_trap_stats_put(msg, group_item->stats); |
| 6389 | if (err) |
| 6390 | goto nla_put_failure; |
| 6391 | |
| 6392 | genlmsg_end(msg, hdr); |
| 6393 | |
| 6394 | return 0; |
| 6395 | |
| 6396 | nla_put_failure: |
| 6397 | genlmsg_cancel(msg, hdr); |
| 6398 | return -EMSGSIZE; |
| 6399 | } |
| 6400 | |
| 6401 | static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb, |
| 6402 | struct genl_info *info) |
| 6403 | { |
| 6404 | struct netlink_ext_ack *extack = info->extack; |
| 6405 | struct devlink *devlink = info->user_ptr[0]; |
| 6406 | struct devlink_trap_group_item *group_item; |
| 6407 | struct sk_buff *msg; |
| 6408 | int err; |
| 6409 | |
| 6410 | if (list_empty(&devlink->trap_group_list)) |
| 6411 | return -EOPNOTSUPP; |
| 6412 | |
| 6413 | group_item = devlink_trap_group_item_get_from_info(devlink, info); |
| 6414 | if (!group_item) { |
| 6415 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group"); |
| 6416 | return -ENOENT; |
| 6417 | } |
| 6418 | |
| 6419 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 6420 | if (!msg) |
| 6421 | return -ENOMEM; |
| 6422 | |
| 6423 | err = devlink_nl_trap_group_fill(msg, devlink, group_item, |
| 6424 | DEVLINK_CMD_TRAP_GROUP_NEW, |
| 6425 | info->snd_portid, info->snd_seq, 0); |
| 6426 | if (err) |
| 6427 | goto err_trap_group_fill; |
| 6428 | |
| 6429 | return genlmsg_reply(msg, info); |
| 6430 | |
| 6431 | err_trap_group_fill: |
| 6432 | nlmsg_free(msg); |
| 6433 | return err; |
| 6434 | } |
| 6435 | |
| 6436 | static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg, |
| 6437 | struct netlink_callback *cb) |
| 6438 | { |
| 6439 | enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW; |
| 6440 | struct devlink_trap_group_item *group_item; |
| 6441 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 6442 | struct devlink *devlink; |
| 6443 | int start = cb->args[0]; |
| 6444 | int idx = 0; |
| 6445 | int err; |
| 6446 | |
| 6447 | mutex_lock(&devlink_mutex); |
| 6448 | list_for_each_entry(devlink, &devlink_list, list) { |
| 6449 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 6450 | continue; |
| 6451 | mutex_lock(&devlink->lock); |
| 6452 | list_for_each_entry(group_item, &devlink->trap_group_list, |
| 6453 | list) { |
| 6454 | if (idx < start) { |
| 6455 | idx++; |
| 6456 | continue; |
| 6457 | } |
| 6458 | err = devlink_nl_trap_group_fill(msg, devlink, |
| 6459 | group_item, cmd, |
| 6460 | portid, |
| 6461 | cb->nlh->nlmsg_seq, |
| 6462 | NLM_F_MULTI); |
| 6463 | if (err) { |
| 6464 | mutex_unlock(&devlink->lock); |
| 6465 | goto out; |
| 6466 | } |
| 6467 | idx++; |
| 6468 | } |
| 6469 | mutex_unlock(&devlink->lock); |
| 6470 | } |
| 6471 | out: |
| 6472 | mutex_unlock(&devlink_mutex); |
| 6473 | |
| 6474 | cb->args[0] = idx; |
| 6475 | return msg->len; |
| 6476 | } |
| 6477 | |
| 6478 | static int |
| 6479 | __devlink_trap_group_action_set(struct devlink *devlink, |
| 6480 | struct devlink_trap_group_item *group_item, |
| 6481 | enum devlink_trap_action trap_action, |
| 6482 | struct netlink_ext_ack *extack) |
| 6483 | { |
| 6484 | const char *group_name = group_item->group->name; |
| 6485 | struct devlink_trap_item *trap_item; |
| 6486 | int err; |
| 6487 | |
| 6488 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 6489 | if (strcmp(trap_item->group_item->group->name, group_name)) |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6490 | continue; |
| 6491 | err = __devlink_trap_action_set(devlink, trap_item, |
| 6492 | trap_action, extack); |
| 6493 | if (err) |
| 6494 | return err; |
| 6495 | } |
| 6496 | |
| 6497 | return 0; |
| 6498 | } |
| 6499 | |
| 6500 | static int |
| 6501 | devlink_trap_group_action_set(struct devlink *devlink, |
| 6502 | struct devlink_trap_group_item *group_item, |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6503 | struct genl_info *info, bool *p_modified) |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6504 | { |
| 6505 | enum devlink_trap_action trap_action; |
| 6506 | int err; |
| 6507 | |
| 6508 | if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION]) |
| 6509 | return 0; |
| 6510 | |
| 6511 | err = devlink_trap_action_get_from_info(info, &trap_action); |
| 6512 | if (err) { |
| 6513 | NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action"); |
| 6514 | return -EINVAL; |
| 6515 | } |
| 6516 | |
| 6517 | err = __devlink_trap_group_action_set(devlink, group_item, trap_action, |
| 6518 | info->extack); |
| 6519 | if (err) |
| 6520 | return err; |
| 6521 | |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6522 | *p_modified = true; |
| 6523 | |
| 6524 | return 0; |
| 6525 | } |
| 6526 | |
| 6527 | static int devlink_trap_group_set(struct devlink *devlink, |
| 6528 | struct devlink_trap_group_item *group_item, |
| 6529 | struct genl_info *info) |
| 6530 | { |
| 6531 | struct devlink_trap_policer_item *policer_item; |
| 6532 | struct netlink_ext_ack *extack = info->extack; |
| 6533 | const struct devlink_trap_policer *policer; |
| 6534 | struct nlattr **attrs = info->attrs; |
| 6535 | int err; |
| 6536 | |
| 6537 | if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) |
| 6538 | return 0; |
| 6539 | |
| 6540 | if (!devlink->ops->trap_group_set) |
| 6541 | return -EOPNOTSUPP; |
| 6542 | |
| 6543 | policer_item = group_item->policer_item; |
| 6544 | if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) { |
| 6545 | u32 policer_id; |
| 6546 | |
| 6547 | policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]); |
| 6548 | policer_item = devlink_trap_policer_item_lookup(devlink, |
| 6549 | policer_id); |
| 6550 | if (policer_id && !policer_item) { |
| 6551 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer"); |
| 6552 | return -ENOENT; |
| 6553 | } |
| 6554 | } |
| 6555 | policer = policer_item ? policer_item->policer : NULL; |
| 6556 | |
| 6557 | err = devlink->ops->trap_group_set(devlink, group_item->group, policer); |
| 6558 | if (err) |
| 6559 | return err; |
| 6560 | |
| 6561 | group_item->policer_item = policer_item; |
| 6562 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6563 | return 0; |
| 6564 | } |
| 6565 | |
| 6566 | static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb, |
| 6567 | struct genl_info *info) |
| 6568 | { |
| 6569 | struct netlink_ext_ack *extack = info->extack; |
| 6570 | struct devlink *devlink = info->user_ptr[0]; |
| 6571 | struct devlink_trap_group_item *group_item; |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6572 | bool modified = false; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6573 | int err; |
| 6574 | |
| 6575 | if (list_empty(&devlink->trap_group_list)) |
| 6576 | return -EOPNOTSUPP; |
| 6577 | |
| 6578 | group_item = devlink_trap_group_item_get_from_info(devlink, info); |
| 6579 | if (!group_item) { |
| 6580 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group"); |
| 6581 | return -ENOENT; |
| 6582 | } |
| 6583 | |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6584 | err = devlink_trap_group_action_set(devlink, group_item, info, |
| 6585 | &modified); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6586 | if (err) |
| 6587 | return err; |
| 6588 | |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6589 | err = devlink_trap_group_set(devlink, group_item, info); |
| 6590 | if (err) |
| 6591 | goto err_trap_group_set; |
| 6592 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6593 | return 0; |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6594 | |
| 6595 | err_trap_group_set: |
| 6596 | if (modified) |
| 6597 | NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already"); |
| 6598 | return err; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6599 | } |
| 6600 | |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 6601 | static struct devlink_trap_policer_item * |
| 6602 | devlink_trap_policer_item_get_from_info(struct devlink *devlink, |
| 6603 | struct genl_info *info) |
| 6604 | { |
| 6605 | u32 id; |
| 6606 | |
| 6607 | if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) |
| 6608 | return NULL; |
| 6609 | id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]); |
| 6610 | |
| 6611 | return devlink_trap_policer_item_lookup(devlink, id); |
| 6612 | } |
| 6613 | |
| 6614 | static int |
| 6615 | devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink, |
| 6616 | const struct devlink_trap_policer *policer) |
| 6617 | { |
| 6618 | struct nlattr *attr; |
| 6619 | u64 drops; |
| 6620 | int err; |
| 6621 | |
| 6622 | if (!devlink->ops->trap_policer_counter_get) |
| 6623 | return 0; |
| 6624 | |
| 6625 | err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops); |
| 6626 | if (err) |
| 6627 | return err; |
| 6628 | |
| 6629 | attr = nla_nest_start(msg, DEVLINK_ATTR_STATS); |
| 6630 | if (!attr) |
| 6631 | return -EMSGSIZE; |
| 6632 | |
| 6633 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops, |
| 6634 | DEVLINK_ATTR_PAD)) |
| 6635 | goto nla_put_failure; |
| 6636 | |
| 6637 | nla_nest_end(msg, attr); |
| 6638 | |
| 6639 | return 0; |
| 6640 | |
| 6641 | nla_put_failure: |
| 6642 | nla_nest_cancel(msg, attr); |
| 6643 | return -EMSGSIZE; |
| 6644 | } |
| 6645 | |
| 6646 | static int |
| 6647 | devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink, |
| 6648 | const struct devlink_trap_policer_item *policer_item, |
| 6649 | enum devlink_command cmd, u32 portid, u32 seq, |
| 6650 | int flags) |
| 6651 | { |
| 6652 | void *hdr; |
| 6653 | int err; |
| 6654 | |
| 6655 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 6656 | if (!hdr) |
| 6657 | return -EMSGSIZE; |
| 6658 | |
| 6659 | if (devlink_nl_put_handle(msg, devlink)) |
| 6660 | goto nla_put_failure; |
| 6661 | |
| 6662 | if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID, |
| 6663 | policer_item->policer->id)) |
| 6664 | goto nla_put_failure; |
| 6665 | |
| 6666 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE, |
| 6667 | policer_item->rate, DEVLINK_ATTR_PAD)) |
| 6668 | goto nla_put_failure; |
| 6669 | |
| 6670 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST, |
| 6671 | policer_item->burst, DEVLINK_ATTR_PAD)) |
| 6672 | goto nla_put_failure; |
| 6673 | |
| 6674 | err = devlink_trap_policer_stats_put(msg, devlink, |
| 6675 | policer_item->policer); |
| 6676 | if (err) |
| 6677 | goto nla_put_failure; |
| 6678 | |
| 6679 | genlmsg_end(msg, hdr); |
| 6680 | |
| 6681 | return 0; |
| 6682 | |
| 6683 | nla_put_failure: |
| 6684 | genlmsg_cancel(msg, hdr); |
| 6685 | return -EMSGSIZE; |
| 6686 | } |
| 6687 | |
| 6688 | static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb, |
| 6689 | struct genl_info *info) |
| 6690 | { |
| 6691 | struct devlink_trap_policer_item *policer_item; |
| 6692 | struct netlink_ext_ack *extack = info->extack; |
| 6693 | struct devlink *devlink = info->user_ptr[0]; |
| 6694 | struct sk_buff *msg; |
| 6695 | int err; |
| 6696 | |
| 6697 | if (list_empty(&devlink->trap_policer_list)) |
| 6698 | return -EOPNOTSUPP; |
| 6699 | |
| 6700 | policer_item = devlink_trap_policer_item_get_from_info(devlink, info); |
| 6701 | if (!policer_item) { |
| 6702 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer"); |
| 6703 | return -ENOENT; |
| 6704 | } |
| 6705 | |
| 6706 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 6707 | if (!msg) |
| 6708 | return -ENOMEM; |
| 6709 | |
| 6710 | err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, |
| 6711 | DEVLINK_CMD_TRAP_POLICER_NEW, |
| 6712 | info->snd_portid, info->snd_seq, 0); |
| 6713 | if (err) |
| 6714 | goto err_trap_policer_fill; |
| 6715 | |
| 6716 | return genlmsg_reply(msg, info); |
| 6717 | |
| 6718 | err_trap_policer_fill: |
| 6719 | nlmsg_free(msg); |
| 6720 | return err; |
| 6721 | } |
| 6722 | |
| 6723 | static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg, |
| 6724 | struct netlink_callback *cb) |
| 6725 | { |
| 6726 | enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW; |
| 6727 | struct devlink_trap_policer_item *policer_item; |
| 6728 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 6729 | struct devlink *devlink; |
| 6730 | int start = cb->args[0]; |
| 6731 | int idx = 0; |
| 6732 | int err; |
| 6733 | |
| 6734 | mutex_lock(&devlink_mutex); |
| 6735 | list_for_each_entry(devlink, &devlink_list, list) { |
| 6736 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 6737 | continue; |
| 6738 | mutex_lock(&devlink->lock); |
| 6739 | list_for_each_entry(policer_item, &devlink->trap_policer_list, |
| 6740 | list) { |
| 6741 | if (idx < start) { |
| 6742 | idx++; |
| 6743 | continue; |
| 6744 | } |
| 6745 | err = devlink_nl_trap_policer_fill(msg, devlink, |
| 6746 | policer_item, cmd, |
| 6747 | portid, |
| 6748 | cb->nlh->nlmsg_seq, |
| 6749 | NLM_F_MULTI); |
| 6750 | if (err) { |
| 6751 | mutex_unlock(&devlink->lock); |
| 6752 | goto out; |
| 6753 | } |
| 6754 | idx++; |
| 6755 | } |
| 6756 | mutex_unlock(&devlink->lock); |
| 6757 | } |
| 6758 | out: |
| 6759 | mutex_unlock(&devlink_mutex); |
| 6760 | |
| 6761 | cb->args[0] = idx; |
| 6762 | return msg->len; |
| 6763 | } |
| 6764 | |
| 6765 | static int |
| 6766 | devlink_trap_policer_set(struct devlink *devlink, |
| 6767 | struct devlink_trap_policer_item *policer_item, |
| 6768 | struct genl_info *info) |
| 6769 | { |
| 6770 | struct netlink_ext_ack *extack = info->extack; |
| 6771 | struct nlattr **attrs = info->attrs; |
| 6772 | u64 rate, burst; |
| 6773 | int err; |
| 6774 | |
| 6775 | rate = policer_item->rate; |
| 6776 | burst = policer_item->burst; |
| 6777 | |
| 6778 | if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]) |
| 6779 | rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]); |
| 6780 | |
| 6781 | if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]) |
| 6782 | burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]); |
| 6783 | |
| 6784 | if (rate < policer_item->policer->min_rate) { |
| 6785 | NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit"); |
| 6786 | return -EINVAL; |
| 6787 | } |
| 6788 | |
| 6789 | if (rate > policer_item->policer->max_rate) { |
| 6790 | NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit"); |
| 6791 | return -EINVAL; |
| 6792 | } |
| 6793 | |
| 6794 | if (burst < policer_item->policer->min_burst) { |
| 6795 | NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit"); |
| 6796 | return -EINVAL; |
| 6797 | } |
| 6798 | |
| 6799 | if (burst > policer_item->policer->max_burst) { |
| 6800 | NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit"); |
| 6801 | return -EINVAL; |
| 6802 | } |
| 6803 | |
| 6804 | err = devlink->ops->trap_policer_set(devlink, policer_item->policer, |
| 6805 | rate, burst, info->extack); |
| 6806 | if (err) |
| 6807 | return err; |
| 6808 | |
| 6809 | policer_item->rate = rate; |
| 6810 | policer_item->burst = burst; |
| 6811 | |
| 6812 | return 0; |
| 6813 | } |
| 6814 | |
| 6815 | static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb, |
| 6816 | struct genl_info *info) |
| 6817 | { |
| 6818 | struct devlink_trap_policer_item *policer_item; |
| 6819 | struct netlink_ext_ack *extack = info->extack; |
| 6820 | struct devlink *devlink = info->user_ptr[0]; |
| 6821 | |
| 6822 | if (list_empty(&devlink->trap_policer_list)) |
| 6823 | return -EOPNOTSUPP; |
| 6824 | |
| 6825 | if (!devlink->ops->trap_policer_set) |
| 6826 | return -EOPNOTSUPP; |
| 6827 | |
| 6828 | policer_item = devlink_trap_policer_item_get_from_info(devlink, info); |
| 6829 | if (!policer_item) { |
| 6830 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer"); |
| 6831 | return -ENOENT; |
| 6832 | } |
| 6833 | |
| 6834 | return devlink_trap_policer_set(devlink, policer_item, info); |
| 6835 | } |
| 6836 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6837 | static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = { |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 6838 | [DEVLINK_ATTR_UNSPEC] = { .strict_start_type = |
| 6839 | DEVLINK_ATTR_TRAP_POLICER_ID }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6840 | [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING }, |
| 6841 | [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING }, |
| 6842 | [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 }, |
| 6843 | [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 }, |
| 6844 | [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6845 | [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 }, |
| 6846 | [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 }, |
| 6847 | [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 }, |
| 6848 | [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 }, |
| 6849 | [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 }, |
| 6850 | [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 }, |
| 6851 | [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6852 | [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 }, |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 6853 | [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 }, |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 6854 | [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6855 | [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING }, |
| 6856 | [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6857 | [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64}, |
| 6858 | [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64}, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 6859 | [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING }, |
| 6860 | [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 }, |
| 6861 | [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 }, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6862 | [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING }, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6863 | [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 }, |
Jakub Kicinski | ff3b63b | 2020-03-02 21:05:12 -0800 | [diff] [blame] | 6864 | [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 }, |
| 6865 | [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 }, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 6866 | [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING }, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6867 | [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 }, |
| 6868 | [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 }, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 6869 | [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING }, |
| 6870 | [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING }, |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6871 | [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING }, |
| 6872 | [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 }, |
| 6873 | [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING }, |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 6874 | [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 }, |
| 6875 | [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 }, |
| 6876 | [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 }, |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 6877 | [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 }, |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 6878 | [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 }, |
| 6879 | [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 }, |
| 6880 | [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 }, |
Parav Pandit | a1e8ae9 | 2020-06-19 03:32:49 +0000 | [diff] [blame] | 6881 | [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6882 | }; |
| 6883 | |
| 6884 | static const struct genl_ops devlink_nl_ops[] = { |
| 6885 | { |
| 6886 | .cmd = DEVLINK_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6887 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6888 | .doit = devlink_nl_cmd_get_doit, |
| 6889 | .dumpit = devlink_nl_cmd_get_dumpit, |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 6890 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6891 | /* can be retrieved by unprivileged users */ |
| 6892 | }, |
| 6893 | { |
| 6894 | .cmd = DEVLINK_CMD_PORT_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6895 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6896 | .doit = devlink_nl_cmd_port_get_doit, |
| 6897 | .dumpit = devlink_nl_cmd_port_get_dumpit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6898 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 6899 | /* can be retrieved by unprivileged users */ |
| 6900 | }, |
| 6901 | { |
| 6902 | .cmd = DEVLINK_CMD_PORT_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6903 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6904 | .doit = devlink_nl_cmd_port_set_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6905 | .flags = GENL_ADMIN_PERM, |
| 6906 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 6907 | }, |
| 6908 | { |
| 6909 | .cmd = DEVLINK_CMD_PORT_SPLIT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6910 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6911 | .doit = devlink_nl_cmd_port_split_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6912 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6913 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6914 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6915 | }, |
| 6916 | { |
| 6917 | .cmd = DEVLINK_CMD_PORT_UNSPLIT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6918 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6919 | .doit = devlink_nl_cmd_port_unsplit_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6920 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6921 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6922 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6923 | }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6924 | { |
| 6925 | .cmd = DEVLINK_CMD_SB_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6926 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6927 | .doit = devlink_nl_cmd_sb_get_doit, |
| 6928 | .dumpit = devlink_nl_cmd_sb_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6929 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6930 | DEVLINK_NL_FLAG_NEED_SB, |
| 6931 | /* can be retrieved by unprivileged users */ |
| 6932 | }, |
| 6933 | { |
| 6934 | .cmd = DEVLINK_CMD_SB_POOL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6935 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6936 | .doit = devlink_nl_cmd_sb_pool_get_doit, |
| 6937 | .dumpit = devlink_nl_cmd_sb_pool_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6938 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6939 | DEVLINK_NL_FLAG_NEED_SB, |
| 6940 | /* can be retrieved by unprivileged users */ |
| 6941 | }, |
| 6942 | { |
| 6943 | .cmd = DEVLINK_CMD_SB_POOL_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6944 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6945 | .doit = devlink_nl_cmd_sb_pool_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6946 | .flags = GENL_ADMIN_PERM, |
| 6947 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6948 | DEVLINK_NL_FLAG_NEED_SB, |
| 6949 | }, |
| 6950 | { |
| 6951 | .cmd = DEVLINK_CMD_SB_PORT_POOL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6952 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6953 | .doit = devlink_nl_cmd_sb_port_pool_get_doit, |
| 6954 | .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6955 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6956 | DEVLINK_NL_FLAG_NEED_SB, |
| 6957 | /* can be retrieved by unprivileged users */ |
| 6958 | }, |
| 6959 | { |
| 6960 | .cmd = DEVLINK_CMD_SB_PORT_POOL_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6961 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6962 | .doit = devlink_nl_cmd_sb_port_pool_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6963 | .flags = GENL_ADMIN_PERM, |
| 6964 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6965 | DEVLINK_NL_FLAG_NEED_SB, |
| 6966 | }, |
| 6967 | { |
| 6968 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6969 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6970 | .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit, |
| 6971 | .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6972 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6973 | DEVLINK_NL_FLAG_NEED_SB, |
| 6974 | /* can be retrieved by unprivileged users */ |
| 6975 | }, |
| 6976 | { |
| 6977 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6978 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6979 | .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6980 | .flags = GENL_ADMIN_PERM, |
| 6981 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6982 | DEVLINK_NL_FLAG_NEED_SB, |
| 6983 | }, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6984 | { |
| 6985 | .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6986 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6987 | .doit = devlink_nl_cmd_sb_occ_snapshot_doit, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6988 | .flags = GENL_ADMIN_PERM, |
| 6989 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6990 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6991 | }, |
| 6992 | { |
| 6993 | .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6994 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6995 | .doit = devlink_nl_cmd_sb_occ_max_clear_doit, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6996 | .flags = GENL_ADMIN_PERM, |
| 6997 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6998 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6999 | }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 7000 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 7001 | .cmd = DEVLINK_CMD_ESWITCH_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7002 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 7003 | .doit = devlink_nl_cmd_eswitch_get_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 7004 | .flags = GENL_ADMIN_PERM, |
Parav Pandit | 98fed6e | 2020-02-23 19:06:56 -0600 | [diff] [blame] | 7005 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7006 | DEVLINK_NL_FLAG_NO_LOCK, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 7007 | }, |
| 7008 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 7009 | .cmd = DEVLINK_CMD_ESWITCH_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7010 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 7011 | .doit = devlink_nl_cmd_eswitch_set_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 7012 | .flags = GENL_ADMIN_PERM, |
Jakub Kicinski | 7ac1cc9 | 2018-05-21 22:12:50 -0700 | [diff] [blame] | 7013 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7014 | DEVLINK_NL_FLAG_NO_LOCK, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 7015 | }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7016 | { |
| 7017 | .cmd = DEVLINK_CMD_DPIPE_TABLE_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7018 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7019 | .doit = devlink_nl_cmd_dpipe_table_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7020 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 7021 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7022 | }, |
| 7023 | { |
| 7024 | .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7025 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7026 | .doit = devlink_nl_cmd_dpipe_entries_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7027 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 7028 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7029 | }, |
| 7030 | { |
| 7031 | .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7032 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7033 | .doit = devlink_nl_cmd_dpipe_headers_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7034 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 7035 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7036 | }, |
| 7037 | { |
| 7038 | .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7039 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7040 | .doit = devlink_nl_cmd_dpipe_table_counters_set, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7041 | .flags = GENL_ADMIN_PERM, |
| 7042 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7043 | }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7044 | { |
| 7045 | .cmd = DEVLINK_CMD_RESOURCE_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7046 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7047 | .doit = devlink_nl_cmd_resource_set, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7048 | .flags = GENL_ADMIN_PERM, |
| 7049 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7050 | }, |
| 7051 | { |
| 7052 | .cmd = DEVLINK_CMD_RESOURCE_DUMP, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7053 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7054 | .doit = devlink_nl_cmd_resource_dump, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7055 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 7056 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7057 | }, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 7058 | { |
| 7059 | .cmd = DEVLINK_CMD_RELOAD, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7060 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 7061 | .doit = devlink_nl_cmd_reload, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 7062 | .flags = GENL_ADMIN_PERM, |
| 7063 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7064 | DEVLINK_NL_FLAG_NO_LOCK, |
| 7065 | }, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 7066 | { |
| 7067 | .cmd = DEVLINK_CMD_PARAM_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7068 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 7069 | .doit = devlink_nl_cmd_param_get_doit, |
| 7070 | .dumpit = devlink_nl_cmd_param_get_dumpit, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 7071 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7072 | /* can be retrieved by unprivileged users */ |
| 7073 | }, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 7074 | { |
| 7075 | .cmd = DEVLINK_CMD_PARAM_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7076 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 7077 | .doit = devlink_nl_cmd_param_set_doit, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 7078 | .flags = GENL_ADMIN_PERM, |
| 7079 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7080 | }, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 7081 | { |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 7082 | .cmd = DEVLINK_CMD_PORT_PARAM_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7083 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 7084 | .doit = devlink_nl_cmd_port_param_get_doit, |
| 7085 | .dumpit = devlink_nl_cmd_port_param_get_dumpit, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 7086 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 7087 | /* can be retrieved by unprivileged users */ |
| 7088 | }, |
| 7089 | { |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 7090 | .cmd = DEVLINK_CMD_PORT_PARAM_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7091 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 7092 | .doit = devlink_nl_cmd_port_param_set_doit, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 7093 | .flags = GENL_ADMIN_PERM, |
| 7094 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 7095 | }, |
| 7096 | { |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 7097 | .cmd = DEVLINK_CMD_REGION_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7098 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 7099 | .doit = devlink_nl_cmd_region_get_doit, |
| 7100 | .dumpit = devlink_nl_cmd_region_get_dumpit, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 7101 | .flags = GENL_ADMIN_PERM, |
| 7102 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7103 | }, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 7104 | { |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 7105 | .cmd = DEVLINK_CMD_REGION_NEW, |
| 7106 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
| 7107 | .doit = devlink_nl_cmd_region_new, |
| 7108 | .flags = GENL_ADMIN_PERM, |
| 7109 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7110 | }, |
| 7111 | { |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 7112 | .cmd = DEVLINK_CMD_REGION_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7113 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 7114 | .doit = devlink_nl_cmd_region_del, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 7115 | .flags = GENL_ADMIN_PERM, |
| 7116 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7117 | }, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 7118 | { |
| 7119 | .cmd = DEVLINK_CMD_REGION_READ, |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 7120 | .validate = GENL_DONT_VALIDATE_STRICT | |
| 7121 | GENL_DONT_VALIDATE_DUMP_STRICT, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 7122 | .dumpit = devlink_nl_cmd_region_read_dumpit, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 7123 | .flags = GENL_ADMIN_PERM, |
| 7124 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7125 | }, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 7126 | { |
| 7127 | .cmd = DEVLINK_CMD_INFO_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7128 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 7129 | .doit = devlink_nl_cmd_info_get_doit, |
| 7130 | .dumpit = devlink_nl_cmd_info_get_dumpit, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 7131 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7132 | /* can be retrieved by unprivileged users */ |
| 7133 | }, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 7134 | { |
| 7135 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7136 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 7137 | .doit = devlink_nl_cmd_health_reporter_get_doit, |
| 7138 | .dumpit = devlink_nl_cmd_health_reporter_get_dumpit, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 7139 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7140 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 7141 | /* can be retrieved by unprivileged users */ |
| 7142 | }, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 7143 | { |
| 7144 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7145 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 7146 | .doit = devlink_nl_cmd_health_reporter_set_doit, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 7147 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 7148 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7149 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 7150 | }, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 7151 | { |
| 7152 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7153 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 7154 | .doit = devlink_nl_cmd_health_reporter_recover_doit, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 7155 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 7156 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7157 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 7158 | }, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 7159 | { |
| 7160 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7161 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 7162 | .doit = devlink_nl_cmd_health_reporter_diagnose_doit, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 7163 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 7164 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7165 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 7166 | }, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 7167 | { |
| 7168 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET, |
Jiri Pirko | 82a843d | 2019-10-07 09:28:31 +0200 | [diff] [blame] | 7169 | .validate = GENL_DONT_VALIDATE_STRICT | |
| 7170 | GENL_DONT_VALIDATE_DUMP_STRICT, |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 7171 | .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 7172 | .flags = GENL_ADMIN_PERM, |
| 7173 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7174 | DEVLINK_NL_FLAG_NO_LOCK, |
| 7175 | }, |
| 7176 | { |
| 7177 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7178 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 7179 | .doit = devlink_nl_cmd_health_reporter_dump_clear_doit, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 7180 | .flags = GENL_ADMIN_PERM, |
| 7181 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7182 | DEVLINK_NL_FLAG_NO_LOCK, |
| 7183 | }, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 7184 | { |
| 7185 | .cmd = DEVLINK_CMD_FLASH_UPDATE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7186 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 7187 | .doit = devlink_nl_cmd_flash_update, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 7188 | .flags = GENL_ADMIN_PERM, |
| 7189 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7190 | }, |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7191 | { |
| 7192 | .cmd = DEVLINK_CMD_TRAP_GET, |
| 7193 | .doit = devlink_nl_cmd_trap_get_doit, |
| 7194 | .dumpit = devlink_nl_cmd_trap_get_dumpit, |
| 7195 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7196 | /* can be retrieved by unprivileged users */ |
| 7197 | }, |
| 7198 | { |
| 7199 | .cmd = DEVLINK_CMD_TRAP_SET, |
| 7200 | .doit = devlink_nl_cmd_trap_set_doit, |
| 7201 | .flags = GENL_ADMIN_PERM, |
| 7202 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7203 | }, |
| 7204 | { |
| 7205 | .cmd = DEVLINK_CMD_TRAP_GROUP_GET, |
| 7206 | .doit = devlink_nl_cmd_trap_group_get_doit, |
| 7207 | .dumpit = devlink_nl_cmd_trap_group_get_dumpit, |
| 7208 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7209 | /* can be retrieved by unprivileged users */ |
| 7210 | }, |
| 7211 | { |
| 7212 | .cmd = DEVLINK_CMD_TRAP_GROUP_SET, |
| 7213 | .doit = devlink_nl_cmd_trap_group_set_doit, |
| 7214 | .flags = GENL_ADMIN_PERM, |
| 7215 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7216 | }, |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 7217 | { |
| 7218 | .cmd = DEVLINK_CMD_TRAP_POLICER_GET, |
| 7219 | .doit = devlink_nl_cmd_trap_policer_get_doit, |
| 7220 | .dumpit = devlink_nl_cmd_trap_policer_get_dumpit, |
| 7221 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7222 | /* can be retrieved by unprivileged users */ |
| 7223 | }, |
| 7224 | { |
| 7225 | .cmd = DEVLINK_CMD_TRAP_POLICER_SET, |
| 7226 | .doit = devlink_nl_cmd_trap_policer_set_doit, |
| 7227 | .flags = GENL_ADMIN_PERM, |
| 7228 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7229 | }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7230 | }; |
| 7231 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 7232 | static struct genl_family devlink_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 7233 | .name = DEVLINK_GENL_NAME, |
| 7234 | .version = DEVLINK_GENL_VERSION, |
| 7235 | .maxattr = DEVLINK_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 7236 | .policy = devlink_nl_policy, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 7237 | .netnsok = true, |
| 7238 | .pre_doit = devlink_nl_pre_doit, |
| 7239 | .post_doit = devlink_nl_post_doit, |
| 7240 | .module = THIS_MODULE, |
| 7241 | .ops = devlink_nl_ops, |
| 7242 | .n_ops = ARRAY_SIZE(devlink_nl_ops), |
| 7243 | .mcgrps = devlink_nl_mcgrps, |
| 7244 | .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps), |
| 7245 | }; |
| 7246 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7247 | /** |
| 7248 | * devlink_alloc - Allocate new devlink instance resources |
| 7249 | * |
| 7250 | * @ops: ops |
| 7251 | * @priv_size: size of user private data |
| 7252 | * |
| 7253 | * Allocate new devlink instance resources, including devlink index |
| 7254 | * and name. |
| 7255 | */ |
| 7256 | struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) |
| 7257 | { |
| 7258 | struct devlink *devlink; |
| 7259 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 7260 | if (WARN_ON(!ops)) |
| 7261 | return NULL; |
| 7262 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7263 | devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL); |
| 7264 | if (!devlink) |
| 7265 | return NULL; |
| 7266 | devlink->ops = ops; |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 7267 | xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC); |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 7268 | __devlink_net_set(devlink, &init_net); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7269 | INIT_LIST_HEAD(&devlink->port_list); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7270 | INIT_LIST_HEAD(&devlink->sb_list); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7271 | INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7272 | INIT_LIST_HEAD(&devlink->resource_list); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 7273 | INIT_LIST_HEAD(&devlink->param_list); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 7274 | INIT_LIST_HEAD(&devlink->region_list); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 7275 | INIT_LIST_HEAD(&devlink->reporter_list); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7276 | INIT_LIST_HEAD(&devlink->trap_list); |
| 7277 | INIT_LIST_HEAD(&devlink->trap_group_list); |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 7278 | INIT_LIST_HEAD(&devlink->trap_policer_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7279 | mutex_init(&devlink->lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 7280 | mutex_init(&devlink->reporters_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7281 | return devlink; |
| 7282 | } |
| 7283 | EXPORT_SYMBOL_GPL(devlink_alloc); |
| 7284 | |
| 7285 | /** |
| 7286 | * devlink_register - Register devlink instance |
| 7287 | * |
| 7288 | * @devlink: devlink |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 7289 | * @dev: parent device |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7290 | */ |
| 7291 | int devlink_register(struct devlink *devlink, struct device *dev) |
| 7292 | { |
| 7293 | mutex_lock(&devlink_mutex); |
| 7294 | devlink->dev = dev; |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 7295 | devlink->registered = true; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7296 | list_add_tail(&devlink->list, &devlink_list); |
| 7297 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 7298 | mutex_unlock(&devlink_mutex); |
| 7299 | return 0; |
| 7300 | } |
| 7301 | EXPORT_SYMBOL_GPL(devlink_register); |
| 7302 | |
| 7303 | /** |
| 7304 | * devlink_unregister - Unregister devlink instance |
| 7305 | * |
| 7306 | * @devlink: devlink |
| 7307 | */ |
| 7308 | void devlink_unregister(struct devlink *devlink) |
| 7309 | { |
| 7310 | mutex_lock(&devlink_mutex); |
Jiri Pirko | a0c7634 | 2019-11-08 21:42:43 +0100 | [diff] [blame] | 7311 | WARN_ON(devlink_reload_supported(devlink) && |
| 7312 | devlink->reload_enabled); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7313 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 7314 | list_del(&devlink->list); |
| 7315 | mutex_unlock(&devlink_mutex); |
| 7316 | } |
| 7317 | EXPORT_SYMBOL_GPL(devlink_unregister); |
| 7318 | |
| 7319 | /** |
Jiri Pirko | a0c7634 | 2019-11-08 21:42:43 +0100 | [diff] [blame] | 7320 | * devlink_reload_enable - Enable reload of devlink instance |
| 7321 | * |
| 7322 | * @devlink: devlink |
| 7323 | * |
| 7324 | * Should be called at end of device initialization |
| 7325 | * process when reload operation is supported. |
| 7326 | */ |
| 7327 | void devlink_reload_enable(struct devlink *devlink) |
| 7328 | { |
| 7329 | mutex_lock(&devlink_mutex); |
| 7330 | devlink->reload_enabled = true; |
| 7331 | mutex_unlock(&devlink_mutex); |
| 7332 | } |
| 7333 | EXPORT_SYMBOL_GPL(devlink_reload_enable); |
| 7334 | |
| 7335 | /** |
| 7336 | * devlink_reload_disable - Disable reload of devlink instance |
| 7337 | * |
| 7338 | * @devlink: devlink |
| 7339 | * |
| 7340 | * Should be called at the beginning of device cleanup |
| 7341 | * process when reload operation is supported. |
| 7342 | */ |
| 7343 | void devlink_reload_disable(struct devlink *devlink) |
| 7344 | { |
| 7345 | mutex_lock(&devlink_mutex); |
| 7346 | /* Mutex is taken which ensures that no reload operation is in |
| 7347 | * progress while setting up forbidded flag. |
| 7348 | */ |
| 7349 | devlink->reload_enabled = false; |
| 7350 | mutex_unlock(&devlink_mutex); |
| 7351 | } |
| 7352 | EXPORT_SYMBOL_GPL(devlink_reload_disable); |
| 7353 | |
| 7354 | /** |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7355 | * devlink_free - Free devlink instance resources |
| 7356 | * |
| 7357 | * @devlink: devlink |
| 7358 | */ |
| 7359 | void devlink_free(struct devlink *devlink) |
| 7360 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 7361 | mutex_destroy(&devlink->reporters_lock); |
Jiri Pirko | 375cf8c | 2019-03-24 11:14:24 +0100 | [diff] [blame] | 7362 | mutex_destroy(&devlink->lock); |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 7363 | WARN_ON(!list_empty(&devlink->trap_policer_list)); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7364 | WARN_ON(!list_empty(&devlink->trap_group_list)); |
| 7365 | WARN_ON(!list_empty(&devlink->trap_list)); |
Parav Pandit | b904aad | 2019-02-08 15:15:00 -0600 | [diff] [blame] | 7366 | WARN_ON(!list_empty(&devlink->reporter_list)); |
| 7367 | WARN_ON(!list_empty(&devlink->region_list)); |
| 7368 | WARN_ON(!list_empty(&devlink->param_list)); |
| 7369 | WARN_ON(!list_empty(&devlink->resource_list)); |
| 7370 | WARN_ON(!list_empty(&devlink->dpipe_table_list)); |
| 7371 | WARN_ON(!list_empty(&devlink->sb_list)); |
| 7372 | WARN_ON(!list_empty(&devlink->port_list)); |
| 7373 | |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 7374 | xa_destroy(&devlink->snapshot_ids); |
| 7375 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7376 | kfree(devlink); |
| 7377 | } |
| 7378 | EXPORT_SYMBOL_GPL(devlink_free); |
| 7379 | |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7380 | static void devlink_port_type_warn(struct work_struct *work) |
| 7381 | { |
| 7382 | WARN(true, "Type was not set for devlink port."); |
| 7383 | } |
| 7384 | |
| 7385 | static bool devlink_port_type_should_warn(struct devlink_port *devlink_port) |
| 7386 | { |
| 7387 | /* Ignore CPU and DSA flavours. */ |
| 7388 | return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU && |
| 7389 | devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA; |
| 7390 | } |
| 7391 | |
Ido Schimmel | 4c58223 | 2020-01-09 19:57:41 +0200 | [diff] [blame] | 7392 | #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600) |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7393 | |
| 7394 | static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port) |
| 7395 | { |
| 7396 | if (!devlink_port_type_should_warn(devlink_port)) |
| 7397 | return; |
| 7398 | /* Schedule a work to WARN in case driver does not set port |
| 7399 | * type within timeout. |
| 7400 | */ |
| 7401 | schedule_delayed_work(&devlink_port->type_warn_dw, |
| 7402 | DEVLINK_PORT_TYPE_WARN_TIMEOUT); |
| 7403 | } |
| 7404 | |
| 7405 | static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port) |
| 7406 | { |
| 7407 | if (!devlink_port_type_should_warn(devlink_port)) |
| 7408 | return; |
| 7409 | cancel_delayed_work_sync(&devlink_port->type_warn_dw); |
| 7410 | } |
| 7411 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7412 | /** |
| 7413 | * devlink_port_register - Register devlink port |
| 7414 | * |
| 7415 | * @devlink: devlink |
| 7416 | * @devlink_port: devlink port |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 7417 | * @port_index: driver-specific numerical identifier of the port |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7418 | * |
| 7419 | * Register devlink port with provided port index. User can use |
| 7420 | * any indexing, even hw-related one. devlink_port structure |
| 7421 | * is convenient to be embedded inside user driver private structure. |
| 7422 | * Note that the caller should take care of zeroing the devlink_port |
| 7423 | * structure. |
| 7424 | */ |
| 7425 | int devlink_port_register(struct devlink *devlink, |
| 7426 | struct devlink_port *devlink_port, |
| 7427 | unsigned int port_index) |
| 7428 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7429 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7430 | if (devlink_port_index_exists(devlink, port_index)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7431 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7432 | return -EEXIST; |
| 7433 | } |
| 7434 | devlink_port->devlink = devlink; |
| 7435 | devlink_port->index = port_index; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7436 | devlink_port->registered = true; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 7437 | spin_lock_init(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7438 | list_add_tail(&devlink_port->list, &devlink->port_list); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7439 | INIT_LIST_HEAD(&devlink_port->param_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7440 | mutex_unlock(&devlink->lock); |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7441 | INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn); |
| 7442 | devlink_port_type_warn_schedule(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7443 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 7444 | return 0; |
| 7445 | } |
| 7446 | EXPORT_SYMBOL_GPL(devlink_port_register); |
| 7447 | |
| 7448 | /** |
| 7449 | * devlink_port_unregister - Unregister devlink port |
| 7450 | * |
| 7451 | * @devlink_port: devlink port |
| 7452 | */ |
| 7453 | void devlink_port_unregister(struct devlink_port *devlink_port) |
| 7454 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7455 | struct devlink *devlink = devlink_port->devlink; |
| 7456 | |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7457 | devlink_port_type_warn_cancel(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7458 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7459 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7460 | list_del(&devlink_port->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7461 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7462 | } |
| 7463 | EXPORT_SYMBOL_GPL(devlink_port_unregister); |
| 7464 | |
| 7465 | static void __devlink_port_type_set(struct devlink_port *devlink_port, |
| 7466 | enum devlink_port_type type, |
| 7467 | void *type_dev) |
| 7468 | { |
Jiri Pirko | 2b239e7 | 2019-03-24 11:14:36 +0100 | [diff] [blame] | 7469 | if (WARN_ON(!devlink_port->registered)) |
| 7470 | return; |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7471 | devlink_port_type_warn_cancel(devlink_port); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7472 | spin_lock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7473 | devlink_port->type = type; |
| 7474 | devlink_port->type_dev = type_dev; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7475 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7476 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 7477 | } |
| 7478 | |
| 7479 | /** |
| 7480 | * devlink_port_type_eth_set - Set port type to Ethernet |
| 7481 | * |
| 7482 | * @devlink_port: devlink port |
| 7483 | * @netdev: related netdevice |
| 7484 | */ |
| 7485 | void devlink_port_type_eth_set(struct devlink_port *devlink_port, |
| 7486 | struct net_device *netdev) |
| 7487 | { |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 7488 | const struct net_device_ops *ops = netdev->netdev_ops; |
| 7489 | |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 7490 | /* If driver registers devlink port, it should set devlink port |
| 7491 | * attributes accordingly so the compat functions are called |
| 7492 | * and the original ops are not used. |
| 7493 | */ |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 7494 | if (ops->ndo_get_phys_port_name) { |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 7495 | /* Some drivers use the same set of ndos for netdevs |
| 7496 | * that have devlink_port registered and also for |
| 7497 | * those who don't. Make sure that ndo_get_phys_port_name |
| 7498 | * returns -EOPNOTSUPP here in case it is defined. |
| 7499 | * Warn if not. |
| 7500 | */ |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 7501 | char name[IFNAMSIZ]; |
| 7502 | int err; |
| 7503 | |
| 7504 | err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name)); |
| 7505 | WARN_ON(err != -EOPNOTSUPP); |
| 7506 | } |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 7507 | if (ops->ndo_get_port_parent_id) { |
| 7508 | /* Some drivers use the same set of ndos for netdevs |
| 7509 | * that have devlink_port registered and also for |
| 7510 | * those who don't. Make sure that ndo_get_port_parent_id |
| 7511 | * returns -EOPNOTSUPP here in case it is defined. |
| 7512 | * Warn if not. |
| 7513 | */ |
| 7514 | struct netdev_phys_item_id ppid; |
| 7515 | int err; |
| 7516 | |
| 7517 | err = ops->ndo_get_port_parent_id(netdev, &ppid); |
| 7518 | WARN_ON(err != -EOPNOTSUPP); |
| 7519 | } |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 7520 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7521 | } |
| 7522 | EXPORT_SYMBOL_GPL(devlink_port_type_eth_set); |
| 7523 | |
| 7524 | /** |
| 7525 | * devlink_port_type_ib_set - Set port type to InfiniBand |
| 7526 | * |
| 7527 | * @devlink_port: devlink port |
| 7528 | * @ibdev: related IB device |
| 7529 | */ |
| 7530 | void devlink_port_type_ib_set(struct devlink_port *devlink_port, |
| 7531 | struct ib_device *ibdev) |
| 7532 | { |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 7533 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7534 | } |
| 7535 | EXPORT_SYMBOL_GPL(devlink_port_type_ib_set); |
| 7536 | |
| 7537 | /** |
| 7538 | * devlink_port_type_clear - Clear port type |
| 7539 | * |
| 7540 | * @devlink_port: devlink port |
| 7541 | */ |
| 7542 | void devlink_port_type_clear(struct devlink_port *devlink_port) |
| 7543 | { |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 7544 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL); |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7545 | devlink_port_type_warn_schedule(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7546 | } |
| 7547 | EXPORT_SYMBOL_GPL(devlink_port_type_clear); |
| 7548 | |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7549 | static int __devlink_port_attrs_set(struct devlink_port *devlink_port, |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7550 | enum devlink_port_flavour flavour) |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7551 | { |
| 7552 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 7553 | |
| 7554 | if (WARN_ON(devlink_port->registered)) |
| 7555 | return -EEXIST; |
Danielle Ratson | 10a429b | 2020-07-09 16:18:14 +0300 | [diff] [blame] | 7556 | devlink_port->attrs_set = true; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7557 | attrs->flavour = flavour; |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7558 | if (attrs->switch_id.id_len) { |
Danielle Ratson | 46737a1 | 2020-07-09 16:18:15 +0300 | [diff] [blame] | 7559 | devlink_port->switch_port = true; |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7560 | if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN)) |
| 7561 | attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7562 | } else { |
Danielle Ratson | 46737a1 | 2020-07-09 16:18:15 +0300 | [diff] [blame] | 7563 | devlink_port->switch_port = false; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7564 | } |
| 7565 | return 0; |
| 7566 | } |
| 7567 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7568 | /** |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7569 | * devlink_port_attrs_set - Set port attributes |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7570 | * |
| 7571 | * @devlink_port: devlink port |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7572 | * @attrs: devlink port attrs |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7573 | */ |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7574 | void devlink_port_attrs_set(struct devlink_port *devlink_port, |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7575 | struct devlink_port_attrs *attrs) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7576 | { |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7577 | int ret; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7578 | |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7579 | devlink_port->attrs = *attrs; |
| 7580 | ret = __devlink_port_attrs_set(devlink_port, attrs->flavour); |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7581 | if (ret) |
Jiri Pirko | 45b8611 | 2019-03-24 11:14:33 +0100 | [diff] [blame] | 7582 | return; |
Danielle Ratson | a0f49b5 | 2020-07-09 16:18:20 +0300 | [diff] [blame] | 7583 | WARN_ON(attrs->splittable && attrs->split); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7584 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7585 | EXPORT_SYMBOL_GPL(devlink_port_attrs_set); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7586 | |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 7587 | /** |
| 7588 | * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes |
| 7589 | * |
| 7590 | * @devlink_port: devlink port |
| 7591 | * @pf: associated PF for the devlink port instance |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 7592 | */ |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7593 | void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u16 pf) |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 7594 | { |
| 7595 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 7596 | int ret; |
| 7597 | |
| 7598 | ret = __devlink_port_attrs_set(devlink_port, |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7599 | DEVLINK_PORT_FLAVOUR_PCI_PF); |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 7600 | if (ret) |
| 7601 | return; |
| 7602 | |
| 7603 | attrs->pci_pf.pf = pf; |
| 7604 | } |
| 7605 | EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set); |
| 7606 | |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 7607 | /** |
| 7608 | * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes |
| 7609 | * |
| 7610 | * @devlink_port: devlink port |
| 7611 | * @pf: associated PF for the devlink port instance |
| 7612 | * @vf: associated VF of a PF for the devlink port instance |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 7613 | */ |
| 7614 | void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 7615 | u16 pf, u16 vf) |
| 7616 | { |
| 7617 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 7618 | int ret; |
| 7619 | |
| 7620 | ret = __devlink_port_attrs_set(devlink_port, |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 7621 | DEVLINK_PORT_FLAVOUR_PCI_VF); |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 7622 | if (ret) |
| 7623 | return; |
| 7624 | attrs->pci_vf.pf = pf; |
| 7625 | attrs->pci_vf.vf = vf; |
| 7626 | } |
| 7627 | EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set); |
| 7628 | |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 7629 | static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port, |
| 7630 | char *name, size_t len) |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7631 | { |
| 7632 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 7633 | int n = 0; |
| 7634 | |
Danielle Ratson | 10a429b | 2020-07-09 16:18:14 +0300 | [diff] [blame] | 7635 | if (!devlink_port->attrs_set) |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7636 | return -EOPNOTSUPP; |
| 7637 | |
| 7638 | switch (attrs->flavour) { |
| 7639 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: |
Parav Pandit | acf1ee4 | 2020-03-03 08:12:42 -0600 | [diff] [blame] | 7640 | case DEVLINK_PORT_FLAVOUR_VIRTUAL: |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7641 | if (!attrs->split) |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7642 | n = snprintf(name, len, "p%u", attrs->phys.port_number); |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7643 | else |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7644 | n = snprintf(name, len, "p%us%u", |
| 7645 | attrs->phys.port_number, |
| 7646 | attrs->phys.split_subport_number); |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7647 | break; |
| 7648 | case DEVLINK_PORT_FLAVOUR_CPU: |
| 7649 | case DEVLINK_PORT_FLAVOUR_DSA: |
| 7650 | /* As CPU and DSA ports do not have a netdevice associated |
| 7651 | * case should not ever happen. |
| 7652 | */ |
| 7653 | WARN_ON(1); |
| 7654 | return -EINVAL; |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 7655 | case DEVLINK_PORT_FLAVOUR_PCI_PF: |
| 7656 | n = snprintf(name, len, "pf%u", attrs->pci_pf.pf); |
| 7657 | break; |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 7658 | case DEVLINK_PORT_FLAVOUR_PCI_VF: |
| 7659 | n = snprintf(name, len, "pf%uvf%u", |
| 7660 | attrs->pci_vf.pf, attrs->pci_vf.vf); |
| 7661 | break; |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | if (n >= len) |
| 7665 | return -EINVAL; |
| 7666 | |
| 7667 | return 0; |
| 7668 | } |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 7669 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7670 | int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, |
| 7671 | u32 size, u16 ingress_pools_count, |
| 7672 | u16 egress_pools_count, u16 ingress_tc_count, |
| 7673 | u16 egress_tc_count) |
| 7674 | { |
| 7675 | struct devlink_sb *devlink_sb; |
| 7676 | int err = 0; |
| 7677 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7678 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7679 | if (devlink_sb_index_exists(devlink, sb_index)) { |
| 7680 | err = -EEXIST; |
| 7681 | goto unlock; |
| 7682 | } |
| 7683 | |
| 7684 | devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL); |
| 7685 | if (!devlink_sb) { |
| 7686 | err = -ENOMEM; |
| 7687 | goto unlock; |
| 7688 | } |
| 7689 | devlink_sb->index = sb_index; |
| 7690 | devlink_sb->size = size; |
| 7691 | devlink_sb->ingress_pools_count = ingress_pools_count; |
| 7692 | devlink_sb->egress_pools_count = egress_pools_count; |
| 7693 | devlink_sb->ingress_tc_count = ingress_tc_count; |
| 7694 | devlink_sb->egress_tc_count = egress_tc_count; |
| 7695 | list_add_tail(&devlink_sb->list, &devlink->sb_list); |
| 7696 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7697 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7698 | return err; |
| 7699 | } |
| 7700 | EXPORT_SYMBOL_GPL(devlink_sb_register); |
| 7701 | |
| 7702 | void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index) |
| 7703 | { |
| 7704 | struct devlink_sb *devlink_sb; |
| 7705 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7706 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7707 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 7708 | WARN_ON(!devlink_sb); |
| 7709 | list_del(&devlink_sb->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7710 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7711 | kfree(devlink_sb); |
| 7712 | } |
| 7713 | EXPORT_SYMBOL_GPL(devlink_sb_unregister); |
| 7714 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7715 | /** |
| 7716 | * devlink_dpipe_headers_register - register dpipe headers |
| 7717 | * |
| 7718 | * @devlink: devlink |
| 7719 | * @dpipe_headers: dpipe header array |
| 7720 | * |
| 7721 | * Register the headers supported by hardware. |
| 7722 | */ |
| 7723 | int devlink_dpipe_headers_register(struct devlink *devlink, |
| 7724 | struct devlink_dpipe_headers *dpipe_headers) |
| 7725 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7726 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7727 | devlink->dpipe_headers = dpipe_headers; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7728 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7729 | return 0; |
| 7730 | } |
| 7731 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register); |
| 7732 | |
| 7733 | /** |
| 7734 | * devlink_dpipe_headers_unregister - unregister dpipe headers |
| 7735 | * |
| 7736 | * @devlink: devlink |
| 7737 | * |
| 7738 | * Unregister the headers supported by hardware. |
| 7739 | */ |
| 7740 | void devlink_dpipe_headers_unregister(struct devlink *devlink) |
| 7741 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7742 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7743 | devlink->dpipe_headers = NULL; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7744 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7745 | } |
| 7746 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister); |
| 7747 | |
| 7748 | /** |
| 7749 | * devlink_dpipe_table_counter_enabled - check if counter allocation |
| 7750 | * required |
| 7751 | * @devlink: devlink |
| 7752 | * @table_name: tables name |
| 7753 | * |
| 7754 | * Used by driver to check if counter allocation is required. |
| 7755 | * After counter allocation is turned on the table entries |
| 7756 | * are updated to include counter statistics. |
| 7757 | * |
| 7758 | * After that point on the driver must respect the counter |
| 7759 | * state so that each entry added to the table is added |
| 7760 | * with a counter. |
| 7761 | */ |
| 7762 | bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, |
| 7763 | const char *table_name) |
| 7764 | { |
| 7765 | struct devlink_dpipe_table *table; |
| 7766 | bool enabled; |
| 7767 | |
| 7768 | rcu_read_lock(); |
| 7769 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 7770 | table_name, devlink); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7771 | enabled = false; |
| 7772 | if (table) |
| 7773 | enabled = table->counters_enabled; |
| 7774 | rcu_read_unlock(); |
| 7775 | return enabled; |
| 7776 | } |
| 7777 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled); |
| 7778 | |
| 7779 | /** |
| 7780 | * devlink_dpipe_table_register - register dpipe table |
| 7781 | * |
| 7782 | * @devlink: devlink |
| 7783 | * @table_name: table name |
| 7784 | * @table_ops: table ops |
| 7785 | * @priv: priv |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7786 | * @counter_control_extern: external control for counters |
| 7787 | */ |
| 7788 | int devlink_dpipe_table_register(struct devlink *devlink, |
| 7789 | const char *table_name, |
| 7790 | struct devlink_dpipe_table_ops *table_ops, |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 7791 | void *priv, bool counter_control_extern) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7792 | { |
| 7793 | struct devlink_dpipe_table *table; |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7794 | int err = 0; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7795 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 7796 | if (WARN_ON(!table_ops->size_get)) |
| 7797 | return -EINVAL; |
| 7798 | |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7799 | mutex_lock(&devlink->lock); |
| 7800 | |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 7801 | if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name, |
| 7802 | devlink)) { |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7803 | err = -EEXIST; |
| 7804 | goto unlock; |
| 7805 | } |
| 7806 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7807 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7808 | if (!table) { |
| 7809 | err = -ENOMEM; |
| 7810 | goto unlock; |
| 7811 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7812 | |
| 7813 | table->name = table_name; |
| 7814 | table->table_ops = table_ops; |
| 7815 | table->priv = priv; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7816 | table->counter_control_extern = counter_control_extern; |
| 7817 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7818 | list_add_tail_rcu(&table->list, &devlink->dpipe_table_list); |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7819 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7820 | mutex_unlock(&devlink->lock); |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7821 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7822 | } |
| 7823 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_register); |
| 7824 | |
| 7825 | /** |
| 7826 | * devlink_dpipe_table_unregister - unregister dpipe table |
| 7827 | * |
| 7828 | * @devlink: devlink |
| 7829 | * @table_name: table name |
| 7830 | */ |
| 7831 | void devlink_dpipe_table_unregister(struct devlink *devlink, |
| 7832 | const char *table_name) |
| 7833 | { |
| 7834 | struct devlink_dpipe_table *table; |
| 7835 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7836 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7837 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 7838 | table_name, devlink); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7839 | if (!table) |
| 7840 | goto unlock; |
| 7841 | list_del_rcu(&table->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7842 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7843 | kfree_rcu(table, rcu); |
| 7844 | return; |
| 7845 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7846 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7847 | } |
| 7848 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister); |
| 7849 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7850 | /** |
| 7851 | * devlink_resource_register - devlink resource register |
| 7852 | * |
| 7853 | * @devlink: devlink |
| 7854 | * @resource_name: resource's name |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7855 | * @resource_size: resource's size |
| 7856 | * @resource_id: resource's id |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 7857 | * @parent_resource_id: resource's parent id |
| 7858 | * @size_params: size parameters |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7859 | */ |
| 7860 | int devlink_resource_register(struct devlink *devlink, |
| 7861 | const char *resource_name, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7862 | u64 resource_size, |
| 7863 | u64 resource_id, |
| 7864 | u64 parent_resource_id, |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 7865 | const struct devlink_resource_size_params *size_params) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7866 | { |
| 7867 | struct devlink_resource *resource; |
| 7868 | struct list_head *resource_list; |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 7869 | bool top_hierarchy; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7870 | int err = 0; |
| 7871 | |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 7872 | top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP; |
| 7873 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7874 | mutex_lock(&devlink->lock); |
| 7875 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 7876 | if (resource) { |
| 7877 | err = -EINVAL; |
| 7878 | goto out; |
| 7879 | } |
| 7880 | |
| 7881 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
| 7882 | if (!resource) { |
| 7883 | err = -ENOMEM; |
| 7884 | goto out; |
| 7885 | } |
| 7886 | |
| 7887 | if (top_hierarchy) { |
| 7888 | resource_list = &devlink->resource_list; |
| 7889 | } else { |
| 7890 | struct devlink_resource *parent_resource; |
| 7891 | |
| 7892 | parent_resource = devlink_resource_find(devlink, NULL, |
| 7893 | parent_resource_id); |
| 7894 | if (parent_resource) { |
| 7895 | resource_list = &parent_resource->resource_list; |
| 7896 | resource->parent = parent_resource; |
| 7897 | } else { |
Colin Ian King | b75703d | 2018-01-22 10:31:19 +0000 | [diff] [blame] | 7898 | kfree(resource); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7899 | err = -EINVAL; |
| 7900 | goto out; |
| 7901 | } |
| 7902 | } |
| 7903 | |
| 7904 | resource->name = resource_name; |
| 7905 | resource->size = resource_size; |
| 7906 | resource->size_new = resource_size; |
| 7907 | resource->id = resource_id; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7908 | resource->size_valid = true; |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 7909 | memcpy(&resource->size_params, size_params, |
| 7910 | sizeof(resource->size_params)); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7911 | INIT_LIST_HEAD(&resource->resource_list); |
| 7912 | list_add_tail(&resource->list, resource_list); |
| 7913 | out: |
| 7914 | mutex_unlock(&devlink->lock); |
| 7915 | return err; |
| 7916 | } |
| 7917 | EXPORT_SYMBOL_GPL(devlink_resource_register); |
| 7918 | |
| 7919 | /** |
| 7920 | * devlink_resources_unregister - free all resources |
| 7921 | * |
| 7922 | * @devlink: devlink |
| 7923 | * @resource: resource |
| 7924 | */ |
| 7925 | void devlink_resources_unregister(struct devlink *devlink, |
| 7926 | struct devlink_resource *resource) |
| 7927 | { |
| 7928 | struct devlink_resource *tmp, *child_resource; |
| 7929 | struct list_head *resource_list; |
| 7930 | |
| 7931 | if (resource) |
| 7932 | resource_list = &resource->resource_list; |
| 7933 | else |
| 7934 | resource_list = &devlink->resource_list; |
| 7935 | |
| 7936 | if (!resource) |
| 7937 | mutex_lock(&devlink->lock); |
| 7938 | |
| 7939 | list_for_each_entry_safe(child_resource, tmp, resource_list, list) { |
| 7940 | devlink_resources_unregister(devlink, child_resource); |
| 7941 | list_del(&child_resource->list); |
| 7942 | kfree(child_resource); |
| 7943 | } |
| 7944 | |
| 7945 | if (!resource) |
| 7946 | mutex_unlock(&devlink->lock); |
| 7947 | } |
| 7948 | EXPORT_SYMBOL_GPL(devlink_resources_unregister); |
| 7949 | |
| 7950 | /** |
| 7951 | * devlink_resource_size_get - get and update size |
| 7952 | * |
| 7953 | * @devlink: devlink |
| 7954 | * @resource_id: the requested resource id |
| 7955 | * @p_resource_size: ptr to update |
| 7956 | */ |
| 7957 | int devlink_resource_size_get(struct devlink *devlink, |
| 7958 | u64 resource_id, |
| 7959 | u64 *p_resource_size) |
| 7960 | { |
| 7961 | struct devlink_resource *resource; |
| 7962 | int err = 0; |
| 7963 | |
| 7964 | mutex_lock(&devlink->lock); |
| 7965 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 7966 | if (!resource) { |
| 7967 | err = -EINVAL; |
| 7968 | goto out; |
| 7969 | } |
| 7970 | *p_resource_size = resource->size_new; |
| 7971 | resource->size = resource->size_new; |
| 7972 | out: |
| 7973 | mutex_unlock(&devlink->lock); |
| 7974 | return err; |
| 7975 | } |
| 7976 | EXPORT_SYMBOL_GPL(devlink_resource_size_get); |
| 7977 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 7978 | /** |
| 7979 | * devlink_dpipe_table_resource_set - set the resource id |
| 7980 | * |
| 7981 | * @devlink: devlink |
| 7982 | * @table_name: table name |
| 7983 | * @resource_id: resource id |
| 7984 | * @resource_units: number of resource's units consumed per table's entry |
| 7985 | */ |
| 7986 | int devlink_dpipe_table_resource_set(struct devlink *devlink, |
| 7987 | const char *table_name, u64 resource_id, |
| 7988 | u64 resource_units) |
| 7989 | { |
| 7990 | struct devlink_dpipe_table *table; |
| 7991 | int err = 0; |
| 7992 | |
| 7993 | mutex_lock(&devlink->lock); |
| 7994 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 7995 | table_name, devlink); |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 7996 | if (!table) { |
| 7997 | err = -EINVAL; |
| 7998 | goto out; |
| 7999 | } |
| 8000 | table->resource_id = resource_id; |
| 8001 | table->resource_units = resource_units; |
| 8002 | table->resource_valid = true; |
| 8003 | out: |
| 8004 | mutex_unlock(&devlink->lock); |
| 8005 | return err; |
| 8006 | } |
| 8007 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set); |
| 8008 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 8009 | /** |
| 8010 | * devlink_resource_occ_get_register - register occupancy getter |
| 8011 | * |
| 8012 | * @devlink: devlink |
| 8013 | * @resource_id: resource id |
| 8014 | * @occ_get: occupancy getter callback |
| 8015 | * @occ_get_priv: occupancy getter callback priv |
| 8016 | */ |
| 8017 | void devlink_resource_occ_get_register(struct devlink *devlink, |
| 8018 | u64 resource_id, |
| 8019 | devlink_resource_occ_get_t *occ_get, |
| 8020 | void *occ_get_priv) |
| 8021 | { |
| 8022 | struct devlink_resource *resource; |
| 8023 | |
| 8024 | mutex_lock(&devlink->lock); |
| 8025 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 8026 | if (WARN_ON(!resource)) |
| 8027 | goto out; |
| 8028 | WARN_ON(resource->occ_get); |
| 8029 | |
| 8030 | resource->occ_get = occ_get; |
| 8031 | resource->occ_get_priv = occ_get_priv; |
| 8032 | out: |
| 8033 | mutex_unlock(&devlink->lock); |
| 8034 | } |
| 8035 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register); |
| 8036 | |
| 8037 | /** |
| 8038 | * devlink_resource_occ_get_unregister - unregister occupancy getter |
| 8039 | * |
| 8040 | * @devlink: devlink |
| 8041 | * @resource_id: resource id |
| 8042 | */ |
| 8043 | void devlink_resource_occ_get_unregister(struct devlink *devlink, |
| 8044 | u64 resource_id) |
| 8045 | { |
| 8046 | struct devlink_resource *resource; |
| 8047 | |
| 8048 | mutex_lock(&devlink->lock); |
| 8049 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 8050 | if (WARN_ON(!resource)) |
| 8051 | goto out; |
| 8052 | WARN_ON(!resource->occ_get); |
| 8053 | |
| 8054 | resource->occ_get = NULL; |
| 8055 | resource->occ_get_priv = NULL; |
| 8056 | out: |
| 8057 | mutex_unlock(&devlink->lock); |
| 8058 | } |
| 8059 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister); |
| 8060 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8061 | static int devlink_param_verify(const struct devlink_param *param) |
| 8062 | { |
| 8063 | if (!param || !param->name || !param->supported_cmodes) |
| 8064 | return -EINVAL; |
| 8065 | if (param->generic) |
| 8066 | return devlink_param_generic_verify(param); |
| 8067 | else |
| 8068 | return devlink_param_driver_verify(param); |
| 8069 | } |
| 8070 | |
| 8071 | static int __devlink_params_register(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8072 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8073 | struct list_head *param_list, |
| 8074 | const struct devlink_param *params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8075 | size_t params_count, |
| 8076 | enum devlink_command reg_cmd, |
| 8077 | enum devlink_command unreg_cmd) |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8078 | { |
| 8079 | const struct devlink_param *param = params; |
| 8080 | int i; |
| 8081 | int err; |
| 8082 | |
| 8083 | mutex_lock(&devlink->lock); |
| 8084 | for (i = 0; i < params_count; i++, param++) { |
| 8085 | err = devlink_param_verify(param); |
| 8086 | if (err) |
| 8087 | goto rollback; |
| 8088 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8089 | err = devlink_param_register_one(devlink, port_index, |
| 8090 | param_list, param, reg_cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8091 | if (err) |
| 8092 | goto rollback; |
| 8093 | } |
| 8094 | |
| 8095 | mutex_unlock(&devlink->lock); |
| 8096 | return 0; |
| 8097 | |
| 8098 | rollback: |
| 8099 | if (!i) |
| 8100 | goto unlock; |
| 8101 | for (param--; i > 0; i--, param--) |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8102 | devlink_param_unregister_one(devlink, port_index, param_list, |
| 8103 | param, unreg_cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8104 | unlock: |
| 8105 | mutex_unlock(&devlink->lock); |
| 8106 | return err; |
| 8107 | } |
| 8108 | |
| 8109 | static void __devlink_params_unregister(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8110 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8111 | struct list_head *param_list, |
| 8112 | const struct devlink_param *params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8113 | size_t params_count, |
| 8114 | enum devlink_command cmd) |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8115 | { |
| 8116 | const struct devlink_param *param = params; |
| 8117 | int i; |
| 8118 | |
| 8119 | mutex_lock(&devlink->lock); |
| 8120 | for (i = 0; i < params_count; i++, param++) |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8121 | devlink_param_unregister_one(devlink, 0, param_list, param, |
| 8122 | cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8123 | mutex_unlock(&devlink->lock); |
| 8124 | } |
| 8125 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 8126 | /** |
| 8127 | * devlink_params_register - register configuration parameters |
| 8128 | * |
| 8129 | * @devlink: devlink |
| 8130 | * @params: configuration parameters array |
| 8131 | * @params_count: number of parameters provided |
| 8132 | * |
| 8133 | * Register the configuration parameters supported by the driver. |
| 8134 | */ |
| 8135 | int devlink_params_register(struct devlink *devlink, |
| 8136 | const struct devlink_param *params, |
| 8137 | size_t params_count) |
| 8138 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8139 | return __devlink_params_register(devlink, 0, &devlink->param_list, |
| 8140 | params, params_count, |
| 8141 | DEVLINK_CMD_PARAM_NEW, |
| 8142 | DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 8143 | } |
| 8144 | EXPORT_SYMBOL_GPL(devlink_params_register); |
| 8145 | |
| 8146 | /** |
| 8147 | * devlink_params_unregister - unregister configuration parameters |
| 8148 | * @devlink: devlink |
| 8149 | * @params: configuration parameters to unregister |
| 8150 | * @params_count: number of parameters provided |
| 8151 | */ |
| 8152 | void devlink_params_unregister(struct devlink *devlink, |
| 8153 | const struct devlink_param *params, |
| 8154 | size_t params_count) |
| 8155 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8156 | return __devlink_params_unregister(devlink, 0, &devlink->param_list, |
| 8157 | params, params_count, |
| 8158 | DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 8159 | } |
| 8160 | EXPORT_SYMBOL_GPL(devlink_params_unregister); |
| 8161 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8162 | /** |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 8163 | * devlink_params_publish - publish configuration parameters |
| 8164 | * |
| 8165 | * @devlink: devlink |
| 8166 | * |
| 8167 | * Publish previously registered configuration parameters. |
| 8168 | */ |
| 8169 | void devlink_params_publish(struct devlink *devlink) |
| 8170 | { |
| 8171 | struct devlink_param_item *param_item; |
| 8172 | |
| 8173 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 8174 | if (param_item->published) |
| 8175 | continue; |
| 8176 | param_item->published = true; |
| 8177 | devlink_param_notify(devlink, 0, param_item, |
| 8178 | DEVLINK_CMD_PARAM_NEW); |
| 8179 | } |
| 8180 | } |
| 8181 | EXPORT_SYMBOL_GPL(devlink_params_publish); |
| 8182 | |
| 8183 | /** |
| 8184 | * devlink_params_unpublish - unpublish configuration parameters |
| 8185 | * |
| 8186 | * @devlink: devlink |
| 8187 | * |
| 8188 | * Unpublish previously registered configuration parameters. |
| 8189 | */ |
| 8190 | void devlink_params_unpublish(struct devlink *devlink) |
| 8191 | { |
| 8192 | struct devlink_param_item *param_item; |
| 8193 | |
| 8194 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 8195 | if (!param_item->published) |
| 8196 | continue; |
| 8197 | param_item->published = false; |
| 8198 | devlink_param_notify(devlink, 0, param_item, |
| 8199 | DEVLINK_CMD_PARAM_DEL); |
| 8200 | } |
| 8201 | } |
| 8202 | EXPORT_SYMBOL_GPL(devlink_params_unpublish); |
| 8203 | |
| 8204 | /** |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8205 | * devlink_port_params_register - register port configuration parameters |
| 8206 | * |
| 8207 | * @devlink_port: devlink port |
| 8208 | * @params: configuration parameters array |
| 8209 | * @params_count: number of parameters provided |
| 8210 | * |
| 8211 | * Register the configuration parameters supported by the port. |
| 8212 | */ |
| 8213 | int devlink_port_params_register(struct devlink_port *devlink_port, |
| 8214 | const struct devlink_param *params, |
| 8215 | size_t params_count) |
| 8216 | { |
| 8217 | return __devlink_params_register(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8218 | devlink_port->index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8219 | &devlink_port->param_list, params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8220 | params_count, |
| 8221 | DEVLINK_CMD_PORT_PARAM_NEW, |
| 8222 | DEVLINK_CMD_PORT_PARAM_DEL); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8223 | } |
| 8224 | EXPORT_SYMBOL_GPL(devlink_port_params_register); |
| 8225 | |
| 8226 | /** |
| 8227 | * devlink_port_params_unregister - unregister port configuration |
| 8228 | * parameters |
| 8229 | * |
| 8230 | * @devlink_port: devlink port |
| 8231 | * @params: configuration parameters array |
| 8232 | * @params_count: number of parameters provided |
| 8233 | */ |
| 8234 | void devlink_port_params_unregister(struct devlink_port *devlink_port, |
| 8235 | const struct devlink_param *params, |
| 8236 | size_t params_count) |
| 8237 | { |
| 8238 | return __devlink_params_unregister(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8239 | devlink_port->index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8240 | &devlink_port->param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8241 | params, params_count, |
| 8242 | DEVLINK_CMD_PORT_PARAM_DEL); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8243 | } |
| 8244 | EXPORT_SYMBOL_GPL(devlink_port_params_unregister); |
| 8245 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8246 | static int |
| 8247 | __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id, |
| 8248 | union devlink_param_value *init_val) |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8249 | { |
| 8250 | struct devlink_param_item *param_item; |
| 8251 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8252 | param_item = devlink_param_find_by_id(param_list, param_id); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8253 | if (!param_item) |
| 8254 | return -EINVAL; |
| 8255 | |
| 8256 | if (!param_item->driverinit_value_valid || |
| 8257 | !devlink_param_cmode_is_supported(param_item->param, |
| 8258 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 8259 | return -EOPNOTSUPP; |
| 8260 | |
Moshe Shemesh | 1276534 | 2018-10-10 16:09:26 +0300 | [diff] [blame] | 8261 | if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING) |
| 8262 | strcpy(init_val->vstr, param_item->driverinit_value.vstr); |
| 8263 | else |
| 8264 | *init_val = param_item->driverinit_value; |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8265 | |
| 8266 | return 0; |
| 8267 | } |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8268 | |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8269 | static int |
| 8270 | __devlink_param_driverinit_value_set(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8271 | unsigned int port_index, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8272 | struct list_head *param_list, u32 param_id, |
| 8273 | union devlink_param_value init_val, |
| 8274 | enum devlink_command cmd) |
| 8275 | { |
| 8276 | struct devlink_param_item *param_item; |
| 8277 | |
| 8278 | param_item = devlink_param_find_by_id(param_list, param_id); |
| 8279 | if (!param_item) |
| 8280 | return -EINVAL; |
| 8281 | |
| 8282 | if (!devlink_param_cmode_is_supported(param_item->param, |
| 8283 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 8284 | return -EOPNOTSUPP; |
| 8285 | |
| 8286 | if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING) |
| 8287 | strcpy(param_item->driverinit_value.vstr, init_val.vstr); |
| 8288 | else |
| 8289 | param_item->driverinit_value = init_val; |
| 8290 | param_item->driverinit_value_valid = true; |
| 8291 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8292 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8293 | return 0; |
| 8294 | } |
| 8295 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8296 | /** |
| 8297 | * devlink_param_driverinit_value_get - get configuration parameter |
| 8298 | * value for driver initializing |
| 8299 | * |
| 8300 | * @devlink: devlink |
| 8301 | * @param_id: parameter ID |
| 8302 | * @init_val: value of parameter in driverinit configuration mode |
| 8303 | * |
| 8304 | * This function should be used by the driver to get driverinit |
| 8305 | * configuration for initialization after reload command. |
| 8306 | */ |
| 8307 | int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id, |
| 8308 | union devlink_param_value *init_val) |
| 8309 | { |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 8310 | if (!devlink_reload_supported(devlink)) |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8311 | return -EOPNOTSUPP; |
| 8312 | |
| 8313 | return __devlink_param_driverinit_value_get(&devlink->param_list, |
| 8314 | param_id, init_val); |
| 8315 | } |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8316 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get); |
| 8317 | |
| 8318 | /** |
| 8319 | * devlink_param_driverinit_value_set - set value of configuration |
| 8320 | * parameter for driverinit |
| 8321 | * configuration mode |
| 8322 | * |
| 8323 | * @devlink: devlink |
| 8324 | * @param_id: parameter ID |
| 8325 | * @init_val: value of parameter to set for driverinit configuration mode |
| 8326 | * |
| 8327 | * This function should be used by the driver to set driverinit |
| 8328 | * configuration mode default value. |
| 8329 | */ |
| 8330 | int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id, |
| 8331 | union devlink_param_value init_val) |
| 8332 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8333 | return __devlink_param_driverinit_value_set(devlink, 0, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8334 | &devlink->param_list, |
| 8335 | param_id, init_val, |
| 8336 | DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8337 | } |
| 8338 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set); |
| 8339 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 8340 | /** |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8341 | * devlink_port_param_driverinit_value_get - get configuration parameter |
| 8342 | * value for driver initializing |
| 8343 | * |
| 8344 | * @devlink_port: devlink_port |
| 8345 | * @param_id: parameter ID |
| 8346 | * @init_val: value of parameter in driverinit configuration mode |
| 8347 | * |
| 8348 | * This function should be used by the driver to get driverinit |
| 8349 | * configuration for initialization after reload command. |
| 8350 | */ |
| 8351 | int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port, |
| 8352 | u32 param_id, |
| 8353 | union devlink_param_value *init_val) |
| 8354 | { |
| 8355 | struct devlink *devlink = devlink_port->devlink; |
| 8356 | |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 8357 | if (!devlink_reload_supported(devlink)) |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8358 | return -EOPNOTSUPP; |
| 8359 | |
| 8360 | return __devlink_param_driverinit_value_get(&devlink_port->param_list, |
| 8361 | param_id, init_val); |
| 8362 | } |
| 8363 | EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get); |
| 8364 | |
| 8365 | /** |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8366 | * devlink_port_param_driverinit_value_set - set value of configuration |
| 8367 | * parameter for driverinit |
| 8368 | * configuration mode |
| 8369 | * |
| 8370 | * @devlink_port: devlink_port |
| 8371 | * @param_id: parameter ID |
| 8372 | * @init_val: value of parameter to set for driverinit configuration mode |
| 8373 | * |
| 8374 | * This function should be used by the driver to set driverinit |
| 8375 | * configuration mode default value. |
| 8376 | */ |
| 8377 | int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port, |
| 8378 | u32 param_id, |
| 8379 | union devlink_param_value init_val) |
| 8380 | { |
| 8381 | return __devlink_param_driverinit_value_set(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8382 | devlink_port->index, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8383 | &devlink_port->param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8384 | param_id, init_val, |
| 8385 | DEVLINK_CMD_PORT_PARAM_NEW); |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8386 | } |
| 8387 | EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set); |
| 8388 | |
| 8389 | /** |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 8390 | * devlink_param_value_changed - notify devlink on a parameter's value |
| 8391 | * change. Should be called by the driver |
| 8392 | * right after the change. |
| 8393 | * |
| 8394 | * @devlink: devlink |
| 8395 | * @param_id: parameter ID |
| 8396 | * |
| 8397 | * This function should be used by the driver to notify devlink on value |
| 8398 | * change, excluding driverinit configuration mode. |
| 8399 | * For driverinit configuration mode driver should use the function |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 8400 | */ |
| 8401 | void devlink_param_value_changed(struct devlink *devlink, u32 param_id) |
| 8402 | { |
| 8403 | struct devlink_param_item *param_item; |
| 8404 | |
| 8405 | param_item = devlink_param_find_by_id(&devlink->param_list, param_id); |
| 8406 | WARN_ON(!param_item); |
| 8407 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8408 | devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 8409 | } |
| 8410 | EXPORT_SYMBOL_GPL(devlink_param_value_changed); |
| 8411 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8412 | /** |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8413 | * devlink_port_param_value_changed - notify devlink on a parameter's value |
| 8414 | * change. Should be called by the driver |
| 8415 | * right after the change. |
| 8416 | * |
| 8417 | * @devlink_port: devlink_port |
| 8418 | * @param_id: parameter ID |
| 8419 | * |
| 8420 | * This function should be used by the driver to notify devlink on value |
| 8421 | * change, excluding driverinit configuration mode. |
| 8422 | * For driverinit configuration mode driver should use the function |
| 8423 | * devlink_port_param_driverinit_value_set() instead. |
| 8424 | */ |
| 8425 | void devlink_port_param_value_changed(struct devlink_port *devlink_port, |
| 8426 | u32 param_id) |
| 8427 | { |
| 8428 | struct devlink_param_item *param_item; |
| 8429 | |
| 8430 | param_item = devlink_param_find_by_id(&devlink_port->param_list, |
| 8431 | param_id); |
| 8432 | WARN_ON(!param_item); |
| 8433 | |
| 8434 | devlink_param_notify(devlink_port->devlink, devlink_port->index, |
| 8435 | param_item, DEVLINK_CMD_PORT_PARAM_NEW); |
| 8436 | } |
| 8437 | EXPORT_SYMBOL_GPL(devlink_port_param_value_changed); |
| 8438 | |
| 8439 | /** |
Moshe Shemesh | bde74ad1 | 2018-10-10 16:09:27 +0300 | [diff] [blame] | 8440 | * devlink_param_value_str_fill - Safely fill-up the string preventing |
| 8441 | * from overflow of the preallocated buffer |
| 8442 | * |
| 8443 | * @dst_val: destination devlink_param_value |
| 8444 | * @src: source buffer |
| 8445 | */ |
| 8446 | void devlink_param_value_str_fill(union devlink_param_value *dst_val, |
| 8447 | const char *src) |
| 8448 | { |
| 8449 | size_t len; |
| 8450 | |
| 8451 | len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE); |
| 8452 | WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE); |
| 8453 | } |
| 8454 | EXPORT_SYMBOL_GPL(devlink_param_value_str_fill); |
| 8455 | |
| 8456 | /** |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8457 | * devlink_region_create - create a new address region |
| 8458 | * |
| 8459 | * @devlink: devlink |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 8460 | * @ops: region operations and name |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8461 | * @region_max_snapshots: Maximum supported number of snapshots for region |
| 8462 | * @region_size: size of region |
| 8463 | */ |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 8464 | struct devlink_region * |
| 8465 | devlink_region_create(struct devlink *devlink, |
| 8466 | const struct devlink_region_ops *ops, |
| 8467 | u32 region_max_snapshots, u64 region_size) |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8468 | { |
| 8469 | struct devlink_region *region; |
| 8470 | int err = 0; |
| 8471 | |
Jacob Keller | a0a09f6 | 2020-03-26 11:37:09 -0700 | [diff] [blame] | 8472 | if (WARN_ON(!ops) || WARN_ON(!ops->destructor)) |
| 8473 | return ERR_PTR(-EINVAL); |
| 8474 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8475 | mutex_lock(&devlink->lock); |
| 8476 | |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 8477 | if (devlink_region_get_by_name(devlink, ops->name)) { |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8478 | err = -EEXIST; |
| 8479 | goto unlock; |
| 8480 | } |
| 8481 | |
| 8482 | region = kzalloc(sizeof(*region), GFP_KERNEL); |
| 8483 | if (!region) { |
| 8484 | err = -ENOMEM; |
| 8485 | goto unlock; |
| 8486 | } |
| 8487 | |
| 8488 | region->devlink = devlink; |
| 8489 | region->max_snapshots = region_max_snapshots; |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 8490 | region->ops = ops; |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8491 | region->size = region_size; |
| 8492 | INIT_LIST_HEAD(®ion->snapshot_list); |
| 8493 | list_add_tail(®ion->list, &devlink->region_list); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 8494 | devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8495 | |
| 8496 | mutex_unlock(&devlink->lock); |
| 8497 | return region; |
| 8498 | |
| 8499 | unlock: |
| 8500 | mutex_unlock(&devlink->lock); |
| 8501 | return ERR_PTR(err); |
| 8502 | } |
| 8503 | EXPORT_SYMBOL_GPL(devlink_region_create); |
| 8504 | |
| 8505 | /** |
| 8506 | * devlink_region_destroy - destroy address region |
| 8507 | * |
| 8508 | * @region: devlink region to destroy |
| 8509 | */ |
| 8510 | void devlink_region_destroy(struct devlink_region *region) |
| 8511 | { |
| 8512 | struct devlink *devlink = region->devlink; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8513 | struct devlink_snapshot *snapshot, *ts; |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8514 | |
| 8515 | mutex_lock(&devlink->lock); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8516 | |
| 8517 | /* Free all snapshots of region */ |
| 8518 | list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list) |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 8519 | devlink_region_snapshot_del(region, snapshot); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8520 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8521 | list_del(®ion->list); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 8522 | |
| 8523 | devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8524 | mutex_unlock(&devlink->lock); |
| 8525 | kfree(region); |
| 8526 | } |
| 8527 | EXPORT_SYMBOL_GPL(devlink_region_destroy); |
| 8528 | |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8529 | /** |
Jacob Keller | b0efcae | 2020-01-09 11:08:20 -0800 | [diff] [blame] | 8530 | * devlink_region_snapshot_id_get - get snapshot ID |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8531 | * |
| 8532 | * This callback should be called when adding a new snapshot, |
| 8533 | * Driver should use the same id for multiple snapshots taken |
| 8534 | * on multiple regions at the same time/by the same trigger. |
| 8535 | * |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 8536 | * The caller of this function must use devlink_region_snapshot_id_put |
| 8537 | * when finished creating regions using this id. |
| 8538 | * |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8539 | * Returns zero on success, or a negative error code on failure. |
| 8540 | * |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8541 | * @devlink: devlink |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8542 | * @id: storage to return id |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8543 | */ |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8544 | int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id) |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8545 | { |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8546 | int err; |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8547 | |
| 8548 | mutex_lock(&devlink->lock); |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8549 | err = __devlink_region_snapshot_id_get(devlink, id); |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8550 | mutex_unlock(&devlink->lock); |
| 8551 | |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8552 | return err; |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8553 | } |
Jacob Keller | b0efcae | 2020-01-09 11:08:20 -0800 | [diff] [blame] | 8554 | EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get); |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8555 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8556 | /** |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 8557 | * devlink_region_snapshot_id_put - put snapshot ID reference |
| 8558 | * |
| 8559 | * This should be called by a driver after finishing creating snapshots |
| 8560 | * with an id. Doing so ensures that the ID can later be released in the |
| 8561 | * event that all snapshots using it have been destroyed. |
| 8562 | * |
| 8563 | * @devlink: devlink |
| 8564 | * @id: id to release reference on |
| 8565 | */ |
| 8566 | void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id) |
| 8567 | { |
| 8568 | mutex_lock(&devlink->lock); |
| 8569 | __devlink_snapshot_id_decrement(devlink, id); |
| 8570 | mutex_unlock(&devlink->lock); |
| 8571 | } |
| 8572 | EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put); |
| 8573 | |
| 8574 | /** |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8575 | * devlink_region_snapshot_create - create a new snapshot |
| 8576 | * This will add a new snapshot of a region. The snapshot |
| 8577 | * will be stored on the region struct and can be accessed |
Jacob Keller | 6d82f67 | 2020-03-26 11:37:10 -0700 | [diff] [blame] | 8578 | * from devlink. This is useful for future analyses of snapshots. |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8579 | * Multiple snapshots can be created on a region. |
| 8580 | * The @snapshot_id should be obtained using the getter function. |
| 8581 | * |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 8582 | * @region: devlink region of the snapshot |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8583 | * @data: snapshot data |
| 8584 | * @snapshot_id: snapshot id to be created |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8585 | */ |
Jiri Pirko | 3a5e523 | 2019-08-09 15:27:15 +0200 | [diff] [blame] | 8586 | int devlink_region_snapshot_create(struct devlink_region *region, |
Jacob Keller | a0a09f6 | 2020-03-26 11:37:09 -0700 | [diff] [blame] | 8587 | u8 *data, u32 snapshot_id) |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8588 | { |
| 8589 | struct devlink *devlink = region->devlink; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8590 | int err; |
| 8591 | |
| 8592 | mutex_lock(&devlink->lock); |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 8593 | err = __devlink_region_snapshot_create(region, data, snapshot_id); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8594 | mutex_unlock(&devlink->lock); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8595 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8596 | return err; |
| 8597 | } |
| 8598 | EXPORT_SYMBOL_GPL(devlink_region_snapshot_create); |
| 8599 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8600 | #define DEVLINK_TRAP(_id, _type) \ |
| 8601 | { \ |
| 8602 | .type = DEVLINK_TRAP_TYPE_##_type, \ |
| 8603 | .id = DEVLINK_TRAP_GENERIC_ID_##_id, \ |
| 8604 | .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \ |
| 8605 | } |
| 8606 | |
| 8607 | static const struct devlink_trap devlink_trap_generic[] = { |
Ido Schimmel | 391203a | 2019-08-17 16:28:18 +0300 | [diff] [blame] | 8608 | DEVLINK_TRAP(SMAC_MC, DROP), |
| 8609 | DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP), |
| 8610 | DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP), |
| 8611 | DEVLINK_TRAP(INGRESS_STP_FILTER, DROP), |
| 8612 | DEVLINK_TRAP(EMPTY_TX_LIST, DROP), |
| 8613 | DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP), |
| 8614 | DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP), |
| 8615 | DEVLINK_TRAP(TTL_ERROR, EXCEPTION), |
| 8616 | DEVLINK_TRAP(TAIL_DROP, DROP), |
Amit Cohen | 6896cc4 | 2019-11-07 18:42:09 +0200 | [diff] [blame] | 8617 | DEVLINK_TRAP(NON_IP_PACKET, DROP), |
| 8618 | DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP), |
| 8619 | DEVLINK_TRAP(DIP_LB, DROP), |
| 8620 | DEVLINK_TRAP(SIP_MC, DROP), |
| 8621 | DEVLINK_TRAP(SIP_LB, DROP), |
| 8622 | DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP), |
| 8623 | DEVLINK_TRAP(IPV4_SIP_BC, DROP), |
| 8624 | DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP), |
| 8625 | DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP), |
Amit Cohen | 3b063ae | 2019-11-07 18:42:14 +0200 | [diff] [blame] | 8626 | DEVLINK_TRAP(MTU_ERROR, EXCEPTION), |
| 8627 | DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION), |
| 8628 | DEVLINK_TRAP(RPF, EXCEPTION), |
| 8629 | DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION), |
| 8630 | DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION), |
| 8631 | DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION), |
Amit Cohen | 95f0ead | 2020-01-19 15:00:48 +0200 | [diff] [blame] | 8632 | DEVLINK_TRAP(NON_ROUTABLE, DROP), |
Amit Cohen | 13c056e | 2020-01-19 15:00:54 +0200 | [diff] [blame] | 8633 | DEVLINK_TRAP(DECAP_ERROR, EXCEPTION), |
Amit Cohen | c3cae49 | 2020-01-19 15:00:58 +0200 | [diff] [blame] | 8634 | DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP), |
Jiri Pirko | ecd942a | 2020-02-24 08:35:47 +0100 | [diff] [blame] | 8635 | DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP), |
| 8636 | DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP), |
Ido Schimmel | 515eac6 | 2020-05-29 21:36:41 +0300 | [diff] [blame] | 8637 | DEVLINK_TRAP(STP, CONTROL), |
| 8638 | DEVLINK_TRAP(LACP, CONTROL), |
| 8639 | DEVLINK_TRAP(LLDP, CONTROL), |
| 8640 | DEVLINK_TRAP(IGMP_QUERY, CONTROL), |
| 8641 | DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL), |
| 8642 | DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL), |
| 8643 | DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL), |
| 8644 | DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL), |
| 8645 | DEVLINK_TRAP(MLD_QUERY, CONTROL), |
| 8646 | DEVLINK_TRAP(MLD_V1_REPORT, CONTROL), |
| 8647 | DEVLINK_TRAP(MLD_V2_REPORT, CONTROL), |
| 8648 | DEVLINK_TRAP(MLD_V1_DONE, CONTROL), |
Ido Schimmel | d77cfd1 | 2020-05-29 21:36:42 +0300 | [diff] [blame] | 8649 | DEVLINK_TRAP(IPV4_DHCP, CONTROL), |
| 8650 | DEVLINK_TRAP(IPV6_DHCP, CONTROL), |
| 8651 | DEVLINK_TRAP(ARP_REQUEST, CONTROL), |
| 8652 | DEVLINK_TRAP(ARP_RESPONSE, CONTROL), |
| 8653 | DEVLINK_TRAP(ARP_OVERLAY, CONTROL), |
| 8654 | DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL), |
| 8655 | DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL), |
| 8656 | DEVLINK_TRAP(IPV4_BFD, CONTROL), |
| 8657 | DEVLINK_TRAP(IPV6_BFD, CONTROL), |
| 8658 | DEVLINK_TRAP(IPV4_OSPF, CONTROL), |
| 8659 | DEVLINK_TRAP(IPV6_OSPF, CONTROL), |
| 8660 | DEVLINK_TRAP(IPV4_BGP, CONTROL), |
| 8661 | DEVLINK_TRAP(IPV6_BGP, CONTROL), |
| 8662 | DEVLINK_TRAP(IPV4_VRRP, CONTROL), |
| 8663 | DEVLINK_TRAP(IPV6_VRRP, CONTROL), |
| 8664 | DEVLINK_TRAP(IPV4_PIM, CONTROL), |
| 8665 | DEVLINK_TRAP(IPV6_PIM, CONTROL), |
| 8666 | DEVLINK_TRAP(UC_LB, CONTROL), |
| 8667 | DEVLINK_TRAP(LOCAL_ROUTE, CONTROL), |
| 8668 | DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL), |
| 8669 | DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL), |
| 8670 | DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL), |
| 8671 | DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL), |
| 8672 | DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL), |
| 8673 | DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL), |
| 8674 | DEVLINK_TRAP(IPV6_REDIRECT, CONTROL), |
| 8675 | DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL), |
| 8676 | DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL), |
| 8677 | DEVLINK_TRAP(PTP_EVENT, CONTROL), |
| 8678 | DEVLINK_TRAP(PTP_GENERAL, CONTROL), |
Ido Schimmel | 5eb18a2 | 2020-05-29 21:36:43 +0300 | [diff] [blame] | 8679 | DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL), |
| 8680 | DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL), |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8681 | }; |
| 8682 | |
| 8683 | #define DEVLINK_TRAP_GROUP(_id) \ |
| 8684 | { \ |
| 8685 | .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \ |
| 8686 | .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \ |
| 8687 | } |
| 8688 | |
| 8689 | static const struct devlink_trap_group devlink_trap_group_generic[] = { |
Ido Schimmel | 391203a | 2019-08-17 16:28:18 +0300 | [diff] [blame] | 8690 | DEVLINK_TRAP_GROUP(L2_DROPS), |
| 8691 | DEVLINK_TRAP_GROUP(L3_DROPS), |
Ido Schimmel | 678eb19 | 2020-05-29 21:36:36 +0300 | [diff] [blame] | 8692 | DEVLINK_TRAP_GROUP(L3_EXCEPTIONS), |
Ido Schimmel | 391203a | 2019-08-17 16:28:18 +0300 | [diff] [blame] | 8693 | DEVLINK_TRAP_GROUP(BUFFER_DROPS), |
Amit Cohen | 13c056e | 2020-01-19 15:00:54 +0200 | [diff] [blame] | 8694 | DEVLINK_TRAP_GROUP(TUNNEL_DROPS), |
Jiri Pirko | ecd942a | 2020-02-24 08:35:47 +0100 | [diff] [blame] | 8695 | DEVLINK_TRAP_GROUP(ACL_DROPS), |
Ido Schimmel | 515eac6 | 2020-05-29 21:36:41 +0300 | [diff] [blame] | 8696 | DEVLINK_TRAP_GROUP(STP), |
| 8697 | DEVLINK_TRAP_GROUP(LACP), |
| 8698 | DEVLINK_TRAP_GROUP(LLDP), |
| 8699 | DEVLINK_TRAP_GROUP(MC_SNOOPING), |
Ido Schimmel | d77cfd1 | 2020-05-29 21:36:42 +0300 | [diff] [blame] | 8700 | DEVLINK_TRAP_GROUP(DHCP), |
| 8701 | DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY), |
| 8702 | DEVLINK_TRAP_GROUP(BFD), |
| 8703 | DEVLINK_TRAP_GROUP(OSPF), |
| 8704 | DEVLINK_TRAP_GROUP(BGP), |
| 8705 | DEVLINK_TRAP_GROUP(VRRP), |
| 8706 | DEVLINK_TRAP_GROUP(PIM), |
| 8707 | DEVLINK_TRAP_GROUP(UC_LB), |
| 8708 | DEVLINK_TRAP_GROUP(LOCAL_DELIVERY), |
| 8709 | DEVLINK_TRAP_GROUP(IPV6), |
| 8710 | DEVLINK_TRAP_GROUP(PTP_EVENT), |
| 8711 | DEVLINK_TRAP_GROUP(PTP_GENERAL), |
Ido Schimmel | 5eb18a2 | 2020-05-29 21:36:43 +0300 | [diff] [blame] | 8712 | DEVLINK_TRAP_GROUP(ACL_SAMPLE), |
| 8713 | DEVLINK_TRAP_GROUP(ACL_TRAP), |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8714 | }; |
| 8715 | |
| 8716 | static int devlink_trap_generic_verify(const struct devlink_trap *trap) |
| 8717 | { |
| 8718 | if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX) |
| 8719 | return -EINVAL; |
| 8720 | |
| 8721 | if (strcmp(trap->name, devlink_trap_generic[trap->id].name)) |
| 8722 | return -EINVAL; |
| 8723 | |
| 8724 | if (trap->type != devlink_trap_generic[trap->id].type) |
| 8725 | return -EINVAL; |
| 8726 | |
| 8727 | return 0; |
| 8728 | } |
| 8729 | |
| 8730 | static int devlink_trap_driver_verify(const struct devlink_trap *trap) |
| 8731 | { |
| 8732 | int i; |
| 8733 | |
| 8734 | if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX) |
| 8735 | return -EINVAL; |
| 8736 | |
| 8737 | for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) { |
| 8738 | if (!strcmp(trap->name, devlink_trap_generic[i].name)) |
| 8739 | return -EEXIST; |
| 8740 | } |
| 8741 | |
| 8742 | return 0; |
| 8743 | } |
| 8744 | |
| 8745 | static int devlink_trap_verify(const struct devlink_trap *trap) |
| 8746 | { |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 8747 | if (!trap || !trap->name) |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8748 | return -EINVAL; |
| 8749 | |
| 8750 | if (trap->generic) |
| 8751 | return devlink_trap_generic_verify(trap); |
| 8752 | else |
| 8753 | return devlink_trap_driver_verify(trap); |
| 8754 | } |
| 8755 | |
| 8756 | static int |
| 8757 | devlink_trap_group_generic_verify(const struct devlink_trap_group *group) |
| 8758 | { |
| 8759 | if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX) |
| 8760 | return -EINVAL; |
| 8761 | |
| 8762 | if (strcmp(group->name, devlink_trap_group_generic[group->id].name)) |
| 8763 | return -EINVAL; |
| 8764 | |
| 8765 | return 0; |
| 8766 | } |
| 8767 | |
| 8768 | static int |
| 8769 | devlink_trap_group_driver_verify(const struct devlink_trap_group *group) |
| 8770 | { |
| 8771 | int i; |
| 8772 | |
| 8773 | if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX) |
| 8774 | return -EINVAL; |
| 8775 | |
| 8776 | for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) { |
| 8777 | if (!strcmp(group->name, devlink_trap_group_generic[i].name)) |
| 8778 | return -EEXIST; |
| 8779 | } |
| 8780 | |
| 8781 | return 0; |
| 8782 | } |
| 8783 | |
| 8784 | static int devlink_trap_group_verify(const struct devlink_trap_group *group) |
| 8785 | { |
| 8786 | if (group->generic) |
| 8787 | return devlink_trap_group_generic_verify(group); |
| 8788 | else |
| 8789 | return devlink_trap_group_driver_verify(group); |
| 8790 | } |
| 8791 | |
| 8792 | static void |
| 8793 | devlink_trap_group_notify(struct devlink *devlink, |
| 8794 | const struct devlink_trap_group_item *group_item, |
| 8795 | enum devlink_command cmd) |
| 8796 | { |
| 8797 | struct sk_buff *msg; |
| 8798 | int err; |
| 8799 | |
| 8800 | WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW && |
| 8801 | cmd != DEVLINK_CMD_TRAP_GROUP_DEL); |
| 8802 | |
| 8803 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 8804 | if (!msg) |
| 8805 | return; |
| 8806 | |
| 8807 | err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0, |
| 8808 | 0); |
| 8809 | if (err) { |
| 8810 | nlmsg_free(msg); |
| 8811 | return; |
| 8812 | } |
| 8813 | |
| 8814 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 8815 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 8816 | } |
| 8817 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8818 | static int |
| 8819 | devlink_trap_item_group_link(struct devlink *devlink, |
| 8820 | struct devlink_trap_item *trap_item) |
| 8821 | { |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 8822 | u16 group_id = trap_item->trap->init_group_id; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8823 | struct devlink_trap_group_item *group_item; |
| 8824 | |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 8825 | group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id); |
Ido Schimmel | a09b37f | 2020-03-22 20:48:29 +0200 | [diff] [blame] | 8826 | if (WARN_ON_ONCE(!group_item)) |
| 8827 | return -EINVAL; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8828 | |
| 8829 | trap_item->group_item = group_item; |
| 8830 | |
| 8831 | return 0; |
| 8832 | } |
| 8833 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8834 | static void devlink_trap_notify(struct devlink *devlink, |
| 8835 | const struct devlink_trap_item *trap_item, |
| 8836 | enum devlink_command cmd) |
| 8837 | { |
| 8838 | struct sk_buff *msg; |
| 8839 | int err; |
| 8840 | |
| 8841 | WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW && |
| 8842 | cmd != DEVLINK_CMD_TRAP_DEL); |
| 8843 | |
| 8844 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 8845 | if (!msg) |
| 8846 | return; |
| 8847 | |
| 8848 | err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0); |
| 8849 | if (err) { |
| 8850 | nlmsg_free(msg); |
| 8851 | return; |
| 8852 | } |
| 8853 | |
| 8854 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 8855 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 8856 | } |
| 8857 | |
| 8858 | static int |
| 8859 | devlink_trap_register(struct devlink *devlink, |
| 8860 | const struct devlink_trap *trap, void *priv) |
| 8861 | { |
| 8862 | struct devlink_trap_item *trap_item; |
| 8863 | int err; |
| 8864 | |
| 8865 | if (devlink_trap_item_lookup(devlink, trap->name)) |
| 8866 | return -EEXIST; |
| 8867 | |
| 8868 | trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL); |
| 8869 | if (!trap_item) |
| 8870 | return -ENOMEM; |
| 8871 | |
| 8872 | trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats); |
| 8873 | if (!trap_item->stats) { |
| 8874 | err = -ENOMEM; |
| 8875 | goto err_stats_alloc; |
| 8876 | } |
| 8877 | |
| 8878 | trap_item->trap = trap; |
| 8879 | trap_item->action = trap->init_action; |
| 8880 | trap_item->priv = priv; |
| 8881 | |
| 8882 | err = devlink_trap_item_group_link(devlink, trap_item); |
| 8883 | if (err) |
| 8884 | goto err_group_link; |
| 8885 | |
| 8886 | err = devlink->ops->trap_init(devlink, trap, trap_item); |
| 8887 | if (err) |
| 8888 | goto err_trap_init; |
| 8889 | |
| 8890 | list_add_tail(&trap_item->list, &devlink->trap_list); |
| 8891 | devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW); |
| 8892 | |
| 8893 | return 0; |
| 8894 | |
| 8895 | err_trap_init: |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8896 | err_group_link: |
| 8897 | free_percpu(trap_item->stats); |
| 8898 | err_stats_alloc: |
| 8899 | kfree(trap_item); |
| 8900 | return err; |
| 8901 | } |
| 8902 | |
| 8903 | static void devlink_trap_unregister(struct devlink *devlink, |
| 8904 | const struct devlink_trap *trap) |
| 8905 | { |
| 8906 | struct devlink_trap_item *trap_item; |
| 8907 | |
| 8908 | trap_item = devlink_trap_item_lookup(devlink, trap->name); |
| 8909 | if (WARN_ON_ONCE(!trap_item)) |
| 8910 | return; |
| 8911 | |
| 8912 | devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL); |
| 8913 | list_del(&trap_item->list); |
| 8914 | if (devlink->ops->trap_fini) |
| 8915 | devlink->ops->trap_fini(devlink, trap, trap_item); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8916 | free_percpu(trap_item->stats); |
| 8917 | kfree(trap_item); |
| 8918 | } |
| 8919 | |
| 8920 | static void devlink_trap_disable(struct devlink *devlink, |
| 8921 | const struct devlink_trap *trap) |
| 8922 | { |
| 8923 | struct devlink_trap_item *trap_item; |
| 8924 | |
| 8925 | trap_item = devlink_trap_item_lookup(devlink, trap->name); |
| 8926 | if (WARN_ON_ONCE(!trap_item)) |
| 8927 | return; |
| 8928 | |
| 8929 | devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP); |
| 8930 | trap_item->action = DEVLINK_TRAP_ACTION_DROP; |
| 8931 | } |
| 8932 | |
| 8933 | /** |
| 8934 | * devlink_traps_register - Register packet traps with devlink. |
| 8935 | * @devlink: devlink. |
| 8936 | * @traps: Packet traps. |
| 8937 | * @traps_count: Count of provided packet traps. |
| 8938 | * @priv: Driver private information. |
| 8939 | * |
| 8940 | * Return: Non-zero value on failure. |
| 8941 | */ |
| 8942 | int devlink_traps_register(struct devlink *devlink, |
| 8943 | const struct devlink_trap *traps, |
| 8944 | size_t traps_count, void *priv) |
| 8945 | { |
| 8946 | int i, err; |
| 8947 | |
| 8948 | if (!devlink->ops->trap_init || !devlink->ops->trap_action_set) |
| 8949 | return -EINVAL; |
| 8950 | |
| 8951 | mutex_lock(&devlink->lock); |
| 8952 | for (i = 0; i < traps_count; i++) { |
| 8953 | const struct devlink_trap *trap = &traps[i]; |
| 8954 | |
| 8955 | err = devlink_trap_verify(trap); |
| 8956 | if (err) |
| 8957 | goto err_trap_verify; |
| 8958 | |
| 8959 | err = devlink_trap_register(devlink, trap, priv); |
| 8960 | if (err) |
| 8961 | goto err_trap_register; |
| 8962 | } |
| 8963 | mutex_unlock(&devlink->lock); |
| 8964 | |
| 8965 | return 0; |
| 8966 | |
| 8967 | err_trap_register: |
| 8968 | err_trap_verify: |
| 8969 | for (i--; i >= 0; i--) |
| 8970 | devlink_trap_unregister(devlink, &traps[i]); |
| 8971 | mutex_unlock(&devlink->lock); |
| 8972 | return err; |
| 8973 | } |
| 8974 | EXPORT_SYMBOL_GPL(devlink_traps_register); |
| 8975 | |
| 8976 | /** |
| 8977 | * devlink_traps_unregister - Unregister packet traps from devlink. |
| 8978 | * @devlink: devlink. |
| 8979 | * @traps: Packet traps. |
| 8980 | * @traps_count: Count of provided packet traps. |
| 8981 | */ |
| 8982 | void devlink_traps_unregister(struct devlink *devlink, |
| 8983 | const struct devlink_trap *traps, |
| 8984 | size_t traps_count) |
| 8985 | { |
| 8986 | int i; |
| 8987 | |
| 8988 | mutex_lock(&devlink->lock); |
| 8989 | /* Make sure we do not have any packets in-flight while unregistering |
| 8990 | * traps by disabling all of them and waiting for a grace period. |
| 8991 | */ |
| 8992 | for (i = traps_count - 1; i >= 0; i--) |
| 8993 | devlink_trap_disable(devlink, &traps[i]); |
| 8994 | synchronize_rcu(); |
| 8995 | for (i = traps_count - 1; i >= 0; i--) |
| 8996 | devlink_trap_unregister(devlink, &traps[i]); |
| 8997 | mutex_unlock(&devlink->lock); |
| 8998 | } |
| 8999 | EXPORT_SYMBOL_GPL(devlink_traps_unregister); |
| 9000 | |
| 9001 | static void |
| 9002 | devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats, |
| 9003 | size_t skb_len) |
| 9004 | { |
| 9005 | struct devlink_stats *stats; |
| 9006 | |
| 9007 | stats = this_cpu_ptr(trap_stats); |
| 9008 | u64_stats_update_begin(&stats->syncp); |
| 9009 | stats->rx_bytes += skb_len; |
| 9010 | stats->rx_packets++; |
| 9011 | u64_stats_update_end(&stats->syncp); |
| 9012 | } |
| 9013 | |
| 9014 | static void |
| 9015 | devlink_trap_report_metadata_fill(struct net_dm_hw_metadata *hw_metadata, |
| 9016 | const struct devlink_trap_item *trap_item, |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 9017 | struct devlink_port *in_devlink_port, |
| 9018 | const struct flow_action_cookie *fa_cookie) |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 9019 | { |
| 9020 | struct devlink_trap_group_item *group_item = trap_item->group_item; |
| 9021 | |
| 9022 | hw_metadata->trap_group_name = group_item->group->name; |
| 9023 | hw_metadata->trap_name = trap_item->trap->name; |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 9024 | hw_metadata->fa_cookie = fa_cookie; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 9025 | |
| 9026 | spin_lock(&in_devlink_port->type_lock); |
| 9027 | if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH) |
| 9028 | hw_metadata->input_dev = in_devlink_port->type_dev; |
| 9029 | spin_unlock(&in_devlink_port->type_lock); |
| 9030 | } |
| 9031 | |
| 9032 | /** |
| 9033 | * devlink_trap_report - Report trapped packet to drop monitor. |
| 9034 | * @devlink: devlink. |
| 9035 | * @skb: Trapped packet. |
| 9036 | * @trap_ctx: Trap context. |
| 9037 | * @in_devlink_port: Input devlink port. |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 9038 | * @fa_cookie: Flow action cookie. Could be NULL. |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 9039 | */ |
| 9040 | void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb, |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 9041 | void *trap_ctx, struct devlink_port *in_devlink_port, |
| 9042 | const struct flow_action_cookie *fa_cookie) |
| 9043 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 9044 | { |
| 9045 | struct devlink_trap_item *trap_item = trap_ctx; |
| 9046 | struct net_dm_hw_metadata hw_metadata = {}; |
| 9047 | |
| 9048 | devlink_trap_stats_update(trap_item->stats, skb->len); |
| 9049 | devlink_trap_stats_update(trap_item->group_item->stats, skb->len); |
| 9050 | |
Ido Schimmel | 30a4e9a | 2020-05-29 21:36:40 +0300 | [diff] [blame] | 9051 | /* Control packets were not dropped by the device or encountered an |
| 9052 | * exception during forwarding and therefore should not be reported to |
| 9053 | * the kernel's drop monitor. |
| 9054 | */ |
| 9055 | if (trap_item->trap->type == DEVLINK_TRAP_TYPE_CONTROL) |
| 9056 | return; |
| 9057 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 9058 | devlink_trap_report_metadata_fill(&hw_metadata, trap_item, |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 9059 | in_devlink_port, fa_cookie); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 9060 | net_dm_hw_report(skb, &hw_metadata); |
| 9061 | } |
| 9062 | EXPORT_SYMBOL_GPL(devlink_trap_report); |
| 9063 | |
| 9064 | /** |
| 9065 | * devlink_trap_ctx_priv - Trap context to driver private information. |
| 9066 | * @trap_ctx: Trap context. |
| 9067 | * |
| 9068 | * Return: Driver private information passed during registration. |
| 9069 | */ |
| 9070 | void *devlink_trap_ctx_priv(void *trap_ctx) |
| 9071 | { |
| 9072 | struct devlink_trap_item *trap_item = trap_ctx; |
| 9073 | |
| 9074 | return trap_item->priv; |
| 9075 | } |
| 9076 | EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv); |
| 9077 | |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 9078 | static int |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 9079 | devlink_trap_group_item_policer_link(struct devlink *devlink, |
| 9080 | struct devlink_trap_group_item *group_item) |
| 9081 | { |
| 9082 | u32 policer_id = group_item->group->init_policer_id; |
| 9083 | struct devlink_trap_policer_item *policer_item; |
| 9084 | |
| 9085 | if (policer_id == 0) |
| 9086 | return 0; |
| 9087 | |
| 9088 | policer_item = devlink_trap_policer_item_lookup(devlink, policer_id); |
| 9089 | if (WARN_ON_ONCE(!policer_item)) |
| 9090 | return -EINVAL; |
| 9091 | |
| 9092 | group_item->policer_item = policer_item; |
| 9093 | |
| 9094 | return 0; |
| 9095 | } |
| 9096 | |
| 9097 | static int |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 9098 | devlink_trap_group_register(struct devlink *devlink, |
| 9099 | const struct devlink_trap_group *group) |
| 9100 | { |
| 9101 | struct devlink_trap_group_item *group_item; |
| 9102 | int err; |
| 9103 | |
| 9104 | if (devlink_trap_group_item_lookup(devlink, group->name)) |
| 9105 | return -EEXIST; |
| 9106 | |
| 9107 | group_item = kzalloc(sizeof(*group_item), GFP_KERNEL); |
| 9108 | if (!group_item) |
| 9109 | return -ENOMEM; |
| 9110 | |
| 9111 | group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats); |
| 9112 | if (!group_item->stats) { |
| 9113 | err = -ENOMEM; |
| 9114 | goto err_stats_alloc; |
| 9115 | } |
| 9116 | |
| 9117 | group_item->group = group; |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 9118 | |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 9119 | err = devlink_trap_group_item_policer_link(devlink, group_item); |
| 9120 | if (err) |
| 9121 | goto err_policer_link; |
| 9122 | |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 9123 | if (devlink->ops->trap_group_init) { |
| 9124 | err = devlink->ops->trap_group_init(devlink, group); |
| 9125 | if (err) |
| 9126 | goto err_group_init; |
| 9127 | } |
| 9128 | |
| 9129 | list_add_tail(&group_item->list, &devlink->trap_group_list); |
| 9130 | devlink_trap_group_notify(devlink, group_item, |
| 9131 | DEVLINK_CMD_TRAP_GROUP_NEW); |
| 9132 | |
| 9133 | return 0; |
| 9134 | |
| 9135 | err_group_init: |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 9136 | err_policer_link: |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 9137 | free_percpu(group_item->stats); |
| 9138 | err_stats_alloc: |
| 9139 | kfree(group_item); |
| 9140 | return err; |
| 9141 | } |
| 9142 | |
| 9143 | static void |
| 9144 | devlink_trap_group_unregister(struct devlink *devlink, |
| 9145 | const struct devlink_trap_group *group) |
| 9146 | { |
| 9147 | struct devlink_trap_group_item *group_item; |
| 9148 | |
| 9149 | group_item = devlink_trap_group_item_lookup(devlink, group->name); |
| 9150 | if (WARN_ON_ONCE(!group_item)) |
| 9151 | return; |
| 9152 | |
| 9153 | devlink_trap_group_notify(devlink, group_item, |
| 9154 | DEVLINK_CMD_TRAP_GROUP_DEL); |
| 9155 | list_del(&group_item->list); |
| 9156 | free_percpu(group_item->stats); |
| 9157 | kfree(group_item); |
| 9158 | } |
| 9159 | |
| 9160 | /** |
| 9161 | * devlink_trap_groups_register - Register packet trap groups with devlink. |
| 9162 | * @devlink: devlink. |
| 9163 | * @groups: Packet trap groups. |
| 9164 | * @groups_count: Count of provided packet trap groups. |
| 9165 | * |
| 9166 | * Return: Non-zero value on failure. |
| 9167 | */ |
| 9168 | int devlink_trap_groups_register(struct devlink *devlink, |
| 9169 | const struct devlink_trap_group *groups, |
| 9170 | size_t groups_count) |
| 9171 | { |
| 9172 | int i, err; |
| 9173 | |
| 9174 | mutex_lock(&devlink->lock); |
| 9175 | for (i = 0; i < groups_count; i++) { |
| 9176 | const struct devlink_trap_group *group = &groups[i]; |
| 9177 | |
| 9178 | err = devlink_trap_group_verify(group); |
| 9179 | if (err) |
| 9180 | goto err_trap_group_verify; |
| 9181 | |
| 9182 | err = devlink_trap_group_register(devlink, group); |
| 9183 | if (err) |
| 9184 | goto err_trap_group_register; |
| 9185 | } |
| 9186 | mutex_unlock(&devlink->lock); |
| 9187 | |
| 9188 | return 0; |
| 9189 | |
| 9190 | err_trap_group_register: |
| 9191 | err_trap_group_verify: |
| 9192 | for (i--; i >= 0; i--) |
| 9193 | devlink_trap_group_unregister(devlink, &groups[i]); |
| 9194 | mutex_unlock(&devlink->lock); |
| 9195 | return err; |
| 9196 | } |
| 9197 | EXPORT_SYMBOL_GPL(devlink_trap_groups_register); |
| 9198 | |
| 9199 | /** |
| 9200 | * devlink_trap_groups_unregister - Unregister packet trap groups from devlink. |
| 9201 | * @devlink: devlink. |
| 9202 | * @groups: Packet trap groups. |
| 9203 | * @groups_count: Count of provided packet trap groups. |
| 9204 | */ |
| 9205 | void devlink_trap_groups_unregister(struct devlink *devlink, |
| 9206 | const struct devlink_trap_group *groups, |
| 9207 | size_t groups_count) |
| 9208 | { |
| 9209 | int i; |
| 9210 | |
| 9211 | mutex_lock(&devlink->lock); |
| 9212 | for (i = groups_count - 1; i >= 0; i--) |
| 9213 | devlink_trap_group_unregister(devlink, &groups[i]); |
| 9214 | mutex_unlock(&devlink->lock); |
| 9215 | } |
| 9216 | EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister); |
| 9217 | |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 9218 | static void |
| 9219 | devlink_trap_policer_notify(struct devlink *devlink, |
| 9220 | const struct devlink_trap_policer_item *policer_item, |
| 9221 | enum devlink_command cmd) |
| 9222 | { |
| 9223 | struct sk_buff *msg; |
| 9224 | int err; |
| 9225 | |
| 9226 | WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW && |
| 9227 | cmd != DEVLINK_CMD_TRAP_POLICER_DEL); |
| 9228 | |
| 9229 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 9230 | if (!msg) |
| 9231 | return; |
| 9232 | |
| 9233 | err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0, |
| 9234 | 0, 0); |
| 9235 | if (err) { |
| 9236 | nlmsg_free(msg); |
| 9237 | return; |
| 9238 | } |
| 9239 | |
| 9240 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 9241 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 9242 | } |
| 9243 | |
| 9244 | static int |
| 9245 | devlink_trap_policer_register(struct devlink *devlink, |
| 9246 | const struct devlink_trap_policer *policer) |
| 9247 | { |
| 9248 | struct devlink_trap_policer_item *policer_item; |
| 9249 | int err; |
| 9250 | |
| 9251 | if (devlink_trap_policer_item_lookup(devlink, policer->id)) |
| 9252 | return -EEXIST; |
| 9253 | |
| 9254 | policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL); |
| 9255 | if (!policer_item) |
| 9256 | return -ENOMEM; |
| 9257 | |
| 9258 | policer_item->policer = policer; |
| 9259 | policer_item->rate = policer->init_rate; |
| 9260 | policer_item->burst = policer->init_burst; |
| 9261 | |
| 9262 | if (devlink->ops->trap_policer_init) { |
| 9263 | err = devlink->ops->trap_policer_init(devlink, policer); |
| 9264 | if (err) |
| 9265 | goto err_policer_init; |
| 9266 | } |
| 9267 | |
| 9268 | list_add_tail(&policer_item->list, &devlink->trap_policer_list); |
| 9269 | devlink_trap_policer_notify(devlink, policer_item, |
| 9270 | DEVLINK_CMD_TRAP_POLICER_NEW); |
| 9271 | |
| 9272 | return 0; |
| 9273 | |
| 9274 | err_policer_init: |
| 9275 | kfree(policer_item); |
| 9276 | return err; |
| 9277 | } |
| 9278 | |
| 9279 | static void |
| 9280 | devlink_trap_policer_unregister(struct devlink *devlink, |
| 9281 | const struct devlink_trap_policer *policer) |
| 9282 | { |
| 9283 | struct devlink_trap_policer_item *policer_item; |
| 9284 | |
| 9285 | policer_item = devlink_trap_policer_item_lookup(devlink, policer->id); |
| 9286 | if (WARN_ON_ONCE(!policer_item)) |
| 9287 | return; |
| 9288 | |
| 9289 | devlink_trap_policer_notify(devlink, policer_item, |
| 9290 | DEVLINK_CMD_TRAP_POLICER_DEL); |
| 9291 | list_del(&policer_item->list); |
| 9292 | if (devlink->ops->trap_policer_fini) |
| 9293 | devlink->ops->trap_policer_fini(devlink, policer); |
| 9294 | kfree(policer_item); |
| 9295 | } |
| 9296 | |
| 9297 | /** |
| 9298 | * devlink_trap_policers_register - Register packet trap policers with devlink. |
| 9299 | * @devlink: devlink. |
| 9300 | * @policers: Packet trap policers. |
| 9301 | * @policers_count: Count of provided packet trap policers. |
| 9302 | * |
| 9303 | * Return: Non-zero value on failure. |
| 9304 | */ |
| 9305 | int |
| 9306 | devlink_trap_policers_register(struct devlink *devlink, |
| 9307 | const struct devlink_trap_policer *policers, |
| 9308 | size_t policers_count) |
| 9309 | { |
| 9310 | int i, err; |
| 9311 | |
| 9312 | mutex_lock(&devlink->lock); |
| 9313 | for (i = 0; i < policers_count; i++) { |
| 9314 | const struct devlink_trap_policer *policer = &policers[i]; |
| 9315 | |
| 9316 | if (WARN_ON(policer->id == 0 || |
| 9317 | policer->max_rate < policer->min_rate || |
| 9318 | policer->max_burst < policer->min_burst)) { |
| 9319 | err = -EINVAL; |
| 9320 | goto err_trap_policer_verify; |
| 9321 | } |
| 9322 | |
| 9323 | err = devlink_trap_policer_register(devlink, policer); |
| 9324 | if (err) |
| 9325 | goto err_trap_policer_register; |
| 9326 | } |
| 9327 | mutex_unlock(&devlink->lock); |
| 9328 | |
| 9329 | return 0; |
| 9330 | |
| 9331 | err_trap_policer_register: |
| 9332 | err_trap_policer_verify: |
| 9333 | for (i--; i >= 0; i--) |
| 9334 | devlink_trap_policer_unregister(devlink, &policers[i]); |
| 9335 | mutex_unlock(&devlink->lock); |
| 9336 | return err; |
| 9337 | } |
| 9338 | EXPORT_SYMBOL_GPL(devlink_trap_policers_register); |
| 9339 | |
| 9340 | /** |
| 9341 | * devlink_trap_policers_unregister - Unregister packet trap policers from devlink. |
| 9342 | * @devlink: devlink. |
| 9343 | * @policers: Packet trap policers. |
| 9344 | * @policers_count: Count of provided packet trap policers. |
| 9345 | */ |
| 9346 | void |
| 9347 | devlink_trap_policers_unregister(struct devlink *devlink, |
| 9348 | const struct devlink_trap_policer *policers, |
| 9349 | size_t policers_count) |
| 9350 | { |
| 9351 | int i; |
| 9352 | |
| 9353 | mutex_lock(&devlink->lock); |
| 9354 | for (i = policers_count - 1; i >= 0; i--) |
| 9355 | devlink_trap_policer_unregister(devlink, &policers[i]); |
| 9356 | mutex_unlock(&devlink->lock); |
| 9357 | } |
| 9358 | EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister); |
| 9359 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 9360 | static void __devlink_compat_running_version(struct devlink *devlink, |
| 9361 | char *buf, size_t len) |
| 9362 | { |
| 9363 | const struct nlattr *nlattr; |
| 9364 | struct devlink_info_req req; |
| 9365 | struct sk_buff *msg; |
| 9366 | int rem, err; |
| 9367 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 9368 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 9369 | if (!msg) |
| 9370 | return; |
| 9371 | |
| 9372 | req.msg = msg; |
| 9373 | err = devlink->ops->info_get(devlink, &req, NULL); |
| 9374 | if (err) |
| 9375 | goto free_msg; |
| 9376 | |
| 9377 | nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) { |
| 9378 | const struct nlattr *kv; |
| 9379 | int rem_kv; |
| 9380 | |
| 9381 | if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING) |
| 9382 | continue; |
| 9383 | |
| 9384 | nla_for_each_nested(kv, nlattr, rem_kv) { |
| 9385 | if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE) |
| 9386 | continue; |
| 9387 | |
| 9388 | strlcat(buf, nla_data(kv), len); |
| 9389 | strlcat(buf, " ", len); |
| 9390 | } |
| 9391 | } |
| 9392 | free_msg: |
| 9393 | nlmsg_free(msg); |
| 9394 | } |
| 9395 | |
| 9396 | void devlink_compat_running_version(struct net_device *dev, |
| 9397 | char *buf, size_t len) |
| 9398 | { |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 9399 | struct devlink *devlink; |
| 9400 | |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9401 | dev_hold(dev); |
| 9402 | rtnl_unlock(); |
| 9403 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9404 | devlink = netdev_to_devlink(dev); |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 9405 | if (!devlink || !devlink->ops->info_get) |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9406 | goto out; |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9407 | |
| 9408 | mutex_lock(&devlink->lock); |
| 9409 | __devlink_compat_running_version(devlink, buf, len); |
| 9410 | mutex_unlock(&devlink->lock); |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9411 | |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9412 | out: |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9413 | rtnl_lock(); |
| 9414 | dev_put(dev); |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 9415 | } |
| 9416 | |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9417 | int devlink_compat_flash_update(struct net_device *dev, const char *file_name) |
| 9418 | { |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9419 | struct devlink *devlink; |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9420 | int ret; |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9421 | |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9422 | dev_hold(dev); |
| 9423 | rtnl_unlock(); |
| 9424 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9425 | devlink = netdev_to_devlink(dev); |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9426 | if (!devlink || !devlink->ops->flash_update) { |
| 9427 | ret = -EOPNOTSUPP; |
| 9428 | goto out; |
| 9429 | } |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9430 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9431 | mutex_lock(&devlink->lock); |
| 9432 | ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL); |
| 9433 | mutex_unlock(&devlink->lock); |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9434 | |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9435 | out: |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9436 | rtnl_lock(); |
| 9437 | dev_put(dev); |
| 9438 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9439 | return ret; |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9440 | } |
| 9441 | |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 9442 | int devlink_compat_phys_port_name_get(struct net_device *dev, |
| 9443 | char *name, size_t len) |
| 9444 | { |
| 9445 | struct devlink_port *devlink_port; |
| 9446 | |
| 9447 | /* RTNL mutex is held here which ensures that devlink_port |
| 9448 | * instance cannot disappear in the middle. No need to take |
| 9449 | * any devlink lock as only permanent values are accessed. |
| 9450 | */ |
| 9451 | ASSERT_RTNL(); |
| 9452 | |
| 9453 | devlink_port = netdev_to_devlink_port(dev); |
| 9454 | if (!devlink_port) |
| 9455 | return -EOPNOTSUPP; |
| 9456 | |
| 9457 | return __devlink_port_phys_port_name_get(devlink_port, name, len); |
| 9458 | } |
| 9459 | |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 9460 | int devlink_compat_switch_id_get(struct net_device *dev, |
| 9461 | struct netdev_phys_item_id *ppid) |
| 9462 | { |
| 9463 | struct devlink_port *devlink_port; |
| 9464 | |
Vlad Buslov | 043b841 | 2019-08-12 20:02:02 +0300 | [diff] [blame] | 9465 | /* Caller must hold RTNL mutex or reference to dev, which ensures that |
| 9466 | * devlink_port instance cannot disappear in the middle. No need to take |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 9467 | * any devlink lock as only permanent values are accessed. |
| 9468 | */ |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 9469 | devlink_port = netdev_to_devlink_port(dev); |
Danielle Ratson | 46737a1 | 2020-07-09 16:18:15 +0300 | [diff] [blame] | 9470 | if (!devlink_port || !devlink_port->switch_port) |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 9471 | return -EOPNOTSUPP; |
| 9472 | |
| 9473 | memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid)); |
| 9474 | |
| 9475 | return 0; |
| 9476 | } |
| 9477 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 9478 | static void __net_exit devlink_pernet_pre_exit(struct net *net) |
| 9479 | { |
| 9480 | struct devlink *devlink; |
| 9481 | int err; |
| 9482 | |
| 9483 | /* In case network namespace is getting destroyed, reload |
| 9484 | * all devlink instances from this namespace into init_net. |
| 9485 | */ |
| 9486 | mutex_lock(&devlink_mutex); |
| 9487 | list_for_each_entry(devlink, &devlink_list, list) { |
| 9488 | if (net_eq(devlink_net(devlink), net)) { |
| 9489 | if (WARN_ON(!devlink_reload_supported(devlink))) |
| 9490 | continue; |
| 9491 | err = devlink_reload(devlink, &init_net, NULL); |
Jiri Pirko | a0c7634 | 2019-11-08 21:42:43 +0100 | [diff] [blame] | 9492 | if (err && err != -EOPNOTSUPP) |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 9493 | pr_warn("Failed to reload devlink instance into init_net\n"); |
| 9494 | } |
| 9495 | } |
| 9496 | mutex_unlock(&devlink_mutex); |
| 9497 | } |
| 9498 | |
| 9499 | static struct pernet_operations devlink_pernet_ops __net_initdata = { |
| 9500 | .pre_exit = devlink_pernet_pre_exit, |
| 9501 | }; |
| 9502 | |
Jakub Kicinski | f4b6bcc | 2019-02-25 19:34:02 -0800 | [diff] [blame] | 9503 | static int __init devlink_init(void) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 9504 | { |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 9505 | int err; |
| 9506 | |
| 9507 | err = genl_register_family(&devlink_nl_family); |
| 9508 | if (err) |
| 9509 | goto out; |
| 9510 | err = register_pernet_subsys(&devlink_pernet_ops); |
| 9511 | |
| 9512 | out: |
| 9513 | WARN_ON(err); |
| 9514 | return err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 9515 | } |
| 9516 | |
Jakub Kicinski | f4b6bcc | 2019-02-25 19:34:02 -0800 | [diff] [blame] | 9517 | subsys_initcall(devlink_init); |