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 | |
| 88 | static LIST_HEAD(devlink_list); |
| 89 | |
| 90 | /* devlink_mutex |
| 91 | * |
| 92 | * An overall lock guarding every operation coming from userspace. |
| 93 | * It also guards devlink devices list and it is taken when |
| 94 | * driver registers/unregisters it. |
| 95 | */ |
| 96 | static DEFINE_MUTEX(devlink_mutex); |
| 97 | |
Jiri Pirko | 471f894 | 2019-10-03 11:49:31 +0200 | [diff] [blame] | 98 | struct net *devlink_net(const struct devlink *devlink) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 99 | { |
| 100 | return read_pnet(&devlink->_net); |
| 101 | } |
Jiri Pirko | 471f894 | 2019-10-03 11:49:31 +0200 | [diff] [blame] | 102 | EXPORT_SYMBOL_GPL(devlink_net); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 103 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 104 | static void __devlink_net_set(struct devlink *devlink, struct net *net) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 105 | { |
| 106 | write_pnet(&devlink->_net, net); |
| 107 | } |
| 108 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 109 | void devlink_net_set(struct devlink *devlink, struct net *net) |
| 110 | { |
| 111 | if (WARN_ON(devlink->registered)) |
| 112 | return; |
| 113 | __devlink_net_set(devlink, net); |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(devlink_net_set); |
| 116 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 117 | static struct devlink *devlink_get_from_attrs(struct net *net, |
| 118 | struct nlattr **attrs) |
| 119 | { |
| 120 | struct devlink *devlink; |
| 121 | char *busname; |
| 122 | char *devname; |
| 123 | |
| 124 | if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME]) |
| 125 | return ERR_PTR(-EINVAL); |
| 126 | |
| 127 | busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]); |
| 128 | devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]); |
| 129 | |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 130 | lockdep_assert_held(&devlink_mutex); |
| 131 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 132 | list_for_each_entry(devlink, &devlink_list, list) { |
| 133 | if (strcmp(devlink->dev->bus->name, busname) == 0 && |
| 134 | strcmp(dev_name(devlink->dev), devname) == 0 && |
| 135 | net_eq(devlink_net(devlink), net)) |
| 136 | return devlink; |
| 137 | } |
| 138 | |
| 139 | return ERR_PTR(-ENODEV); |
| 140 | } |
| 141 | |
| 142 | static struct devlink *devlink_get_from_info(struct genl_info *info) |
| 143 | { |
| 144 | return devlink_get_from_attrs(genl_info_net(info), info->attrs); |
| 145 | } |
| 146 | |
| 147 | static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, |
Parav Pandit | c7282b5 | 2019-08-30 05:39:44 -0500 | [diff] [blame] | 148 | unsigned int port_index) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 149 | { |
| 150 | struct devlink_port *devlink_port; |
| 151 | |
| 152 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 153 | if (devlink_port->index == port_index) |
| 154 | return devlink_port; |
| 155 | } |
| 156 | return NULL; |
| 157 | } |
| 158 | |
Parav Pandit | c7282b5 | 2019-08-30 05:39:44 -0500 | [diff] [blame] | 159 | static bool devlink_port_index_exists(struct devlink *devlink, |
| 160 | unsigned int port_index) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 161 | { |
| 162 | return devlink_port_get_by_index(devlink, port_index); |
| 163 | } |
| 164 | |
| 165 | static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, |
| 166 | struct nlattr **attrs) |
| 167 | { |
| 168 | if (attrs[DEVLINK_ATTR_PORT_INDEX]) { |
| 169 | u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 170 | struct devlink_port *devlink_port; |
| 171 | |
| 172 | devlink_port = devlink_port_get_by_index(devlink, port_index); |
| 173 | if (!devlink_port) |
| 174 | return ERR_PTR(-ENODEV); |
| 175 | return devlink_port; |
| 176 | } |
| 177 | return ERR_PTR(-EINVAL); |
| 178 | } |
| 179 | |
| 180 | static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink, |
| 181 | struct genl_info *info) |
| 182 | { |
| 183 | return devlink_port_get_from_attrs(devlink, info->attrs); |
| 184 | } |
| 185 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 186 | struct devlink_sb { |
| 187 | struct list_head list; |
| 188 | unsigned int index; |
| 189 | u32 size; |
| 190 | u16 ingress_pools_count; |
| 191 | u16 egress_pools_count; |
| 192 | u16 ingress_tc_count; |
| 193 | u16 egress_tc_count; |
| 194 | }; |
| 195 | |
| 196 | static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb) |
| 197 | { |
| 198 | return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count; |
| 199 | } |
| 200 | |
| 201 | static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink, |
| 202 | unsigned int sb_index) |
| 203 | { |
| 204 | struct devlink_sb *devlink_sb; |
| 205 | |
| 206 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 207 | if (devlink_sb->index == sb_index) |
| 208 | return devlink_sb; |
| 209 | } |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | static bool devlink_sb_index_exists(struct devlink *devlink, |
| 214 | unsigned int sb_index) |
| 215 | { |
| 216 | return devlink_sb_get_by_index(devlink, sb_index); |
| 217 | } |
| 218 | |
| 219 | static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink, |
| 220 | struct nlattr **attrs) |
| 221 | { |
| 222 | if (attrs[DEVLINK_ATTR_SB_INDEX]) { |
| 223 | u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]); |
| 224 | struct devlink_sb *devlink_sb; |
| 225 | |
| 226 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 227 | if (!devlink_sb) |
| 228 | return ERR_PTR(-ENODEV); |
| 229 | return devlink_sb; |
| 230 | } |
| 231 | return ERR_PTR(-EINVAL); |
| 232 | } |
| 233 | |
| 234 | static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink, |
| 235 | struct genl_info *info) |
| 236 | { |
| 237 | return devlink_sb_get_from_attrs(devlink, info->attrs); |
| 238 | } |
| 239 | |
| 240 | static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 241 | struct nlattr **attrs, |
| 242 | u16 *p_pool_index) |
| 243 | { |
| 244 | u16 val; |
| 245 | |
| 246 | if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX]) |
| 247 | return -EINVAL; |
| 248 | |
| 249 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]); |
| 250 | if (val >= devlink_sb_pool_count(devlink_sb)) |
| 251 | return -EINVAL; |
| 252 | *p_pool_index = val; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb, |
| 257 | struct genl_info *info, |
| 258 | u16 *p_pool_index) |
| 259 | { |
| 260 | return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs, |
| 261 | p_pool_index); |
| 262 | } |
| 263 | |
| 264 | static int |
| 265 | devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs, |
| 266 | enum devlink_sb_pool_type *p_pool_type) |
| 267 | { |
| 268 | u8 val; |
| 269 | |
| 270 | if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE]) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]); |
| 274 | if (val != DEVLINK_SB_POOL_TYPE_INGRESS && |
| 275 | val != DEVLINK_SB_POOL_TYPE_EGRESS) |
| 276 | return -EINVAL; |
| 277 | *p_pool_type = val; |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int |
| 282 | devlink_sb_pool_type_get_from_info(struct genl_info *info, |
| 283 | enum devlink_sb_pool_type *p_pool_type) |
| 284 | { |
| 285 | return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type); |
| 286 | } |
| 287 | |
| 288 | static int |
| 289 | devlink_sb_th_type_get_from_attrs(struct nlattr **attrs, |
| 290 | enum devlink_sb_threshold_type *p_th_type) |
| 291 | { |
| 292 | u8 val; |
| 293 | |
| 294 | if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]); |
| 298 | if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC && |
| 299 | val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC) |
| 300 | return -EINVAL; |
| 301 | *p_th_type = val; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int |
| 306 | devlink_sb_th_type_get_from_info(struct genl_info *info, |
| 307 | enum devlink_sb_threshold_type *p_th_type) |
| 308 | { |
| 309 | return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type); |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 314 | struct nlattr **attrs, |
| 315 | enum devlink_sb_pool_type pool_type, |
| 316 | u16 *p_tc_index) |
| 317 | { |
| 318 | u16 val; |
| 319 | |
| 320 | if (!attrs[DEVLINK_ATTR_SB_TC_INDEX]) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]); |
| 324 | if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS && |
| 325 | val >= devlink_sb->ingress_tc_count) |
| 326 | return -EINVAL; |
| 327 | if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS && |
| 328 | val >= devlink_sb->egress_tc_count) |
| 329 | return -EINVAL; |
| 330 | *p_tc_index = val; |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int |
| 335 | devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb, |
| 336 | struct genl_info *info, |
| 337 | enum devlink_sb_pool_type pool_type, |
| 338 | u16 *p_tc_index) |
| 339 | { |
| 340 | return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs, |
| 341 | pool_type, p_tc_index); |
| 342 | } |
| 343 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 344 | struct devlink_region { |
| 345 | struct devlink *devlink; |
| 346 | struct list_head list; |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 347 | const struct devlink_region_ops *ops; |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 348 | struct list_head snapshot_list; |
| 349 | u32 max_snapshots; |
| 350 | u32 cur_snapshots; |
| 351 | u64 size; |
| 352 | }; |
| 353 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 354 | struct devlink_snapshot { |
| 355 | struct list_head list; |
| 356 | struct devlink_region *region; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 357 | u8 *data; |
| 358 | u32 id; |
| 359 | }; |
| 360 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 361 | static struct devlink_region * |
| 362 | devlink_region_get_by_name(struct devlink *devlink, const char *region_name) |
| 363 | { |
| 364 | struct devlink_region *region; |
| 365 | |
| 366 | list_for_each_entry(region, &devlink->region_list, list) |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 367 | if (!strcmp(region->ops->name, region_name)) |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 368 | return region; |
| 369 | |
| 370 | return NULL; |
| 371 | } |
| 372 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 373 | static struct devlink_snapshot * |
| 374 | devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id) |
| 375 | { |
| 376 | struct devlink_snapshot *snapshot; |
| 377 | |
| 378 | list_for_each_entry(snapshot, ®ion->snapshot_list, list) |
| 379 | if (snapshot->id == id) |
| 380 | return snapshot; |
| 381 | |
| 382 | return NULL; |
| 383 | } |
| 384 | |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 385 | #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0) |
| 386 | #define DEVLINK_NL_FLAG_NEED_PORT BIT(1) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 387 | #define DEVLINK_NL_FLAG_NEED_SB BIT(2) |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 388 | |
| 389 | /* The per devlink instance lock is taken by default in the pre-doit |
| 390 | * operation, yet several commands do not require this. The global |
| 391 | * devlink lock is taken and protects from disruption by user-calls. |
| 392 | */ |
| 393 | #define DEVLINK_NL_FLAG_NO_LOCK BIT(3) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 394 | |
| 395 | static int devlink_nl_pre_doit(const struct genl_ops *ops, |
| 396 | struct sk_buff *skb, struct genl_info *info) |
| 397 | { |
| 398 | struct devlink *devlink; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 399 | int err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 400 | |
| 401 | mutex_lock(&devlink_mutex); |
| 402 | devlink = devlink_get_from_info(info); |
| 403 | if (IS_ERR(devlink)) { |
| 404 | mutex_unlock(&devlink_mutex); |
| 405 | return PTR_ERR(devlink); |
| 406 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 407 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 408 | mutex_lock(&devlink->lock); |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 409 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 410 | info->user_ptr[0] = devlink; |
| 411 | } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 412 | struct devlink_port *devlink_port; |
| 413 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 414 | devlink_port = devlink_port_get_from_info(devlink, info); |
| 415 | if (IS_ERR(devlink_port)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 416 | err = PTR_ERR(devlink_port); |
| 417 | goto unlock; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 418 | } |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 419 | info->user_ptr[0] = devlink_port; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 420 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 421 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) { |
| 422 | struct devlink_sb *devlink_sb; |
| 423 | |
| 424 | devlink_sb = devlink_sb_get_from_info(devlink, info); |
| 425 | if (IS_ERR(devlink_sb)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 426 | err = PTR_ERR(devlink_sb); |
| 427 | goto unlock; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 428 | } |
| 429 | info->user_ptr[1] = devlink_sb; |
| 430 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 431 | return 0; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 432 | |
| 433 | unlock: |
| 434 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 435 | mutex_unlock(&devlink->lock); |
| 436 | mutex_unlock(&devlink_mutex); |
| 437 | return err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static void devlink_nl_post_doit(const struct genl_ops *ops, |
| 441 | struct sk_buff *skb, struct genl_info *info) |
| 442 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 443 | struct devlink *devlink; |
| 444 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 445 | /* When devlink changes netns, it would not be found |
| 446 | * by devlink_get_from_info(). So try if it is stored first. |
| 447 | */ |
| 448 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 449 | devlink = info->user_ptr[0]; |
| 450 | } else { |
| 451 | devlink = devlink_get_from_info(info); |
| 452 | WARN_ON(IS_ERR(devlink)); |
| 453 | } |
| 454 | if (!IS_ERR(devlink) && ~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 455 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 456 | mutex_unlock(&devlink_mutex); |
| 457 | } |
| 458 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 459 | static struct genl_family devlink_nl_family; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 460 | |
| 461 | enum devlink_multicast_groups { |
| 462 | DEVLINK_MCGRP_CONFIG, |
| 463 | }; |
| 464 | |
| 465 | static const struct genl_multicast_group devlink_nl_mcgrps[] = { |
| 466 | [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, |
| 467 | }; |
| 468 | |
| 469 | static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) |
| 470 | { |
| 471 | if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name)) |
| 472 | return -EMSGSIZE; |
| 473 | if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev))) |
| 474 | return -EMSGSIZE; |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink, |
| 479 | enum devlink_command cmd, u32 portid, |
| 480 | u32 seq, int flags) |
| 481 | { |
| 482 | void *hdr; |
| 483 | |
| 484 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 485 | if (!hdr) |
| 486 | return -EMSGSIZE; |
| 487 | |
| 488 | if (devlink_nl_put_handle(msg, devlink)) |
| 489 | goto nla_put_failure; |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 490 | if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed)) |
| 491 | goto nla_put_failure; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 492 | |
| 493 | genlmsg_end(msg, hdr); |
| 494 | return 0; |
| 495 | |
| 496 | nla_put_failure: |
| 497 | genlmsg_cancel(msg, hdr); |
| 498 | return -EMSGSIZE; |
| 499 | } |
| 500 | |
| 501 | static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) |
| 502 | { |
| 503 | struct sk_buff *msg; |
| 504 | int err; |
| 505 | |
| 506 | WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); |
| 507 | |
| 508 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 509 | if (!msg) |
| 510 | return; |
| 511 | |
| 512 | err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0); |
| 513 | if (err) { |
| 514 | nlmsg_free(msg); |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 519 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 520 | } |
| 521 | |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 522 | static int devlink_nl_port_attrs_put(struct sk_buff *msg, |
| 523 | struct devlink_port *devlink_port) |
| 524 | { |
| 525 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 526 | |
| 527 | if (!attrs->set) |
| 528 | return 0; |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 529 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour)) |
| 530 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 531 | switch (devlink_port->attrs.flavour) { |
| 532 | case DEVLINK_PORT_FLAVOUR_PCI_PF: |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 533 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, |
| 534 | attrs->pci_pf.pf)) |
| 535 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 536 | break; |
| 537 | case DEVLINK_PORT_FLAVOUR_PCI_VF: |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 538 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, |
| 539 | attrs->pci_vf.pf) || |
| 540 | nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, |
| 541 | attrs->pci_vf.vf)) |
| 542 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 543 | break; |
| 544 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: |
| 545 | case DEVLINK_PORT_FLAVOUR_CPU: |
| 546 | case DEVLINK_PORT_FLAVOUR_DSA: |
Parav Pandit | acf1ee4 | 2020-03-03 08:12:42 -0600 | [diff] [blame] | 547 | case DEVLINK_PORT_FLAVOUR_VIRTUAL: |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 548 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, |
| 549 | attrs->phys.port_number)) |
| 550 | return -EMSGSIZE; |
| 551 | if (!attrs->split) |
| 552 | return 0; |
| 553 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, |
| 554 | attrs->phys.port_number)) |
| 555 | return -EMSGSIZE; |
| 556 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER, |
| 557 | attrs->phys.split_subport_number)) |
| 558 | return -EMSGSIZE; |
| 559 | break; |
| 560 | default: |
| 561 | break; |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 562 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 566 | static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, |
| 567 | struct devlink_port *devlink_port, |
| 568 | enum devlink_command cmd, u32 portid, |
| 569 | u32 seq, int flags) |
| 570 | { |
| 571 | void *hdr; |
| 572 | |
| 573 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 574 | if (!hdr) |
| 575 | return -EMSGSIZE; |
| 576 | |
| 577 | if (devlink_nl_put_handle(msg, devlink)) |
| 578 | goto nla_put_failure; |
| 579 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 580 | goto nla_put_failure; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 581 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 582 | spin_lock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 583 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 584 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 585 | if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET && |
| 586 | nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE, |
| 587 | devlink_port->desired_type)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 588 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 589 | if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { |
| 590 | struct net_device *netdev = devlink_port->type_dev; |
| 591 | |
| 592 | if (netdev && |
| 593 | (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, |
| 594 | netdev->ifindex) || |
| 595 | nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, |
| 596 | netdev->name))) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 597 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 598 | } |
| 599 | if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { |
| 600 | struct ib_device *ibdev = devlink_port->type_dev; |
| 601 | |
| 602 | if (ibdev && |
| 603 | nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME, |
| 604 | ibdev->name)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 605 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 606 | } |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 607 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 608 | if (devlink_nl_port_attrs_put(msg, devlink_port)) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 609 | goto nla_put_failure; |
| 610 | |
| 611 | genlmsg_end(msg, hdr); |
| 612 | return 0; |
| 613 | |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 614 | nla_put_failure_type_locked: |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 615 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 616 | nla_put_failure: |
| 617 | genlmsg_cancel(msg, hdr); |
| 618 | return -EMSGSIZE; |
| 619 | } |
| 620 | |
| 621 | static void devlink_port_notify(struct devlink_port *devlink_port, |
| 622 | enum devlink_command cmd) |
| 623 | { |
| 624 | struct devlink *devlink = devlink_port->devlink; |
| 625 | struct sk_buff *msg; |
| 626 | int err; |
| 627 | |
| 628 | if (!devlink_port->registered) |
| 629 | return; |
| 630 | |
| 631 | WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); |
| 632 | |
| 633 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 634 | if (!msg) |
| 635 | return; |
| 636 | |
| 637 | err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0); |
| 638 | if (err) { |
| 639 | nlmsg_free(msg); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 644 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 645 | } |
| 646 | |
| 647 | static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info) |
| 648 | { |
| 649 | struct devlink *devlink = info->user_ptr[0]; |
| 650 | struct sk_buff *msg; |
| 651 | int err; |
| 652 | |
| 653 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 654 | if (!msg) |
| 655 | return -ENOMEM; |
| 656 | |
| 657 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 658 | info->snd_portid, info->snd_seq, 0); |
| 659 | if (err) { |
| 660 | nlmsg_free(msg); |
| 661 | return err; |
| 662 | } |
| 663 | |
| 664 | return genlmsg_reply(msg, info); |
| 665 | } |
| 666 | |
| 667 | static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg, |
| 668 | struct netlink_callback *cb) |
| 669 | { |
| 670 | struct devlink *devlink; |
| 671 | int start = cb->args[0]; |
| 672 | int idx = 0; |
| 673 | int err; |
| 674 | |
| 675 | mutex_lock(&devlink_mutex); |
| 676 | list_for_each_entry(devlink, &devlink_list, list) { |
| 677 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 678 | continue; |
| 679 | if (idx < start) { |
| 680 | idx++; |
| 681 | continue; |
| 682 | } |
| 683 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 684 | NETLINK_CB(cb->skb).portid, |
| 685 | cb->nlh->nlmsg_seq, NLM_F_MULTI); |
| 686 | if (err) |
| 687 | goto out; |
| 688 | idx++; |
| 689 | } |
| 690 | out: |
| 691 | mutex_unlock(&devlink_mutex); |
| 692 | |
| 693 | cb->args[0] = idx; |
| 694 | return msg->len; |
| 695 | } |
| 696 | |
| 697 | static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb, |
| 698 | struct genl_info *info) |
| 699 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 700 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 701 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 702 | struct sk_buff *msg; |
| 703 | int err; |
| 704 | |
| 705 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 706 | if (!msg) |
| 707 | return -ENOMEM; |
| 708 | |
| 709 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 710 | DEVLINK_CMD_PORT_NEW, |
| 711 | info->snd_portid, info->snd_seq, 0); |
| 712 | if (err) { |
| 713 | nlmsg_free(msg); |
| 714 | return err; |
| 715 | } |
| 716 | |
| 717 | return genlmsg_reply(msg, info); |
| 718 | } |
| 719 | |
| 720 | static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg, |
| 721 | struct netlink_callback *cb) |
| 722 | { |
| 723 | struct devlink *devlink; |
| 724 | struct devlink_port *devlink_port; |
| 725 | int start = cb->args[0]; |
| 726 | int idx = 0; |
| 727 | int err; |
| 728 | |
| 729 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 730 | list_for_each_entry(devlink, &devlink_list, list) { |
| 731 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 732 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 733 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 734 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 735 | if (idx < start) { |
| 736 | idx++; |
| 737 | continue; |
| 738 | } |
| 739 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 740 | DEVLINK_CMD_NEW, |
| 741 | NETLINK_CB(cb->skb).portid, |
| 742 | cb->nlh->nlmsg_seq, |
| 743 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 744 | if (err) { |
| 745 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 746 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 747 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 748 | idx++; |
| 749 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 750 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 751 | } |
| 752 | out: |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 753 | mutex_unlock(&devlink_mutex); |
| 754 | |
| 755 | cb->args[0] = idx; |
| 756 | return msg->len; |
| 757 | } |
| 758 | |
| 759 | static int devlink_port_type_set(struct devlink *devlink, |
| 760 | struct devlink_port *devlink_port, |
| 761 | enum devlink_port_type port_type) |
| 762 | |
| 763 | { |
| 764 | int err; |
| 765 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 766 | if (devlink->ops->port_type_set) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 767 | if (port_type == DEVLINK_PORT_TYPE_NOTSET) |
| 768 | return -EINVAL; |
Elad Raz | 6edf101 | 2016-10-23 17:43:05 +0200 | [diff] [blame] | 769 | if (port_type == devlink_port->type) |
| 770 | return 0; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 771 | err = devlink->ops->port_type_set(devlink_port, port_type); |
| 772 | if (err) |
| 773 | return err; |
| 774 | devlink_port->desired_type = port_type; |
| 775 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 776 | return 0; |
| 777 | } |
| 778 | return -EOPNOTSUPP; |
| 779 | } |
| 780 | |
| 781 | static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, |
| 782 | struct genl_info *info) |
| 783 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 784 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 785 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 786 | int err; |
| 787 | |
| 788 | if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) { |
| 789 | enum devlink_port_type port_type; |
| 790 | |
| 791 | port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]); |
| 792 | err = devlink_port_type_set(devlink, devlink_port, port_type); |
| 793 | if (err) |
| 794 | return err; |
| 795 | } |
| 796 | return 0; |
| 797 | } |
| 798 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 799 | static int devlink_port_split(struct devlink *devlink, u32 port_index, |
| 800 | u32 count, struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 801 | |
| 802 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 803 | if (devlink->ops->port_split) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 804 | return devlink->ops->port_split(devlink, port_index, count, |
| 805 | extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 806 | return -EOPNOTSUPP; |
| 807 | } |
| 808 | |
| 809 | static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, |
| 810 | struct genl_info *info) |
| 811 | { |
| 812 | struct devlink *devlink = info->user_ptr[0]; |
| 813 | u32 port_index; |
| 814 | u32 count; |
| 815 | |
| 816 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] || |
| 817 | !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]) |
| 818 | return -EINVAL; |
| 819 | |
| 820 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 821 | count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 822 | return devlink_port_split(devlink, port_index, count, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 823 | } |
| 824 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 825 | static int devlink_port_unsplit(struct devlink *devlink, u32 port_index, |
| 826 | struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 827 | |
| 828 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 829 | if (devlink->ops->port_unsplit) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 830 | return devlink->ops->port_unsplit(devlink, port_index, extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 831 | return -EOPNOTSUPP; |
| 832 | } |
| 833 | |
| 834 | static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb, |
| 835 | struct genl_info *info) |
| 836 | { |
| 837 | struct devlink *devlink = info->user_ptr[0]; |
| 838 | u32 port_index; |
| 839 | |
| 840 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) |
| 841 | return -EINVAL; |
| 842 | |
| 843 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 844 | return devlink_port_unsplit(devlink, port_index, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 847 | static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink, |
| 848 | struct devlink_sb *devlink_sb, |
| 849 | enum devlink_command cmd, u32 portid, |
| 850 | u32 seq, int flags) |
| 851 | { |
| 852 | void *hdr; |
| 853 | |
| 854 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 855 | if (!hdr) |
| 856 | return -EMSGSIZE; |
| 857 | |
| 858 | if (devlink_nl_put_handle(msg, devlink)) |
| 859 | goto nla_put_failure; |
| 860 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 861 | goto nla_put_failure; |
| 862 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size)) |
| 863 | goto nla_put_failure; |
| 864 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT, |
| 865 | devlink_sb->ingress_pools_count)) |
| 866 | goto nla_put_failure; |
| 867 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT, |
| 868 | devlink_sb->egress_pools_count)) |
| 869 | goto nla_put_failure; |
| 870 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT, |
| 871 | devlink_sb->ingress_tc_count)) |
| 872 | goto nla_put_failure; |
| 873 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT, |
| 874 | devlink_sb->egress_tc_count)) |
| 875 | goto nla_put_failure; |
| 876 | |
| 877 | genlmsg_end(msg, hdr); |
| 878 | return 0; |
| 879 | |
| 880 | nla_put_failure: |
| 881 | genlmsg_cancel(msg, hdr); |
| 882 | return -EMSGSIZE; |
| 883 | } |
| 884 | |
| 885 | static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb, |
| 886 | struct genl_info *info) |
| 887 | { |
| 888 | struct devlink *devlink = info->user_ptr[0]; |
| 889 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 890 | struct sk_buff *msg; |
| 891 | int err; |
| 892 | |
| 893 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 894 | if (!msg) |
| 895 | return -ENOMEM; |
| 896 | |
| 897 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 898 | DEVLINK_CMD_SB_NEW, |
| 899 | info->snd_portid, info->snd_seq, 0); |
| 900 | if (err) { |
| 901 | nlmsg_free(msg); |
| 902 | return err; |
| 903 | } |
| 904 | |
| 905 | return genlmsg_reply(msg, info); |
| 906 | } |
| 907 | |
| 908 | static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg, |
| 909 | struct netlink_callback *cb) |
| 910 | { |
| 911 | struct devlink *devlink; |
| 912 | struct devlink_sb *devlink_sb; |
| 913 | int start = cb->args[0]; |
| 914 | int idx = 0; |
| 915 | int err; |
| 916 | |
| 917 | mutex_lock(&devlink_mutex); |
| 918 | list_for_each_entry(devlink, &devlink_list, list) { |
| 919 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 920 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 921 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 922 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 923 | if (idx < start) { |
| 924 | idx++; |
| 925 | continue; |
| 926 | } |
| 927 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 928 | DEVLINK_CMD_SB_NEW, |
| 929 | NETLINK_CB(cb->skb).portid, |
| 930 | cb->nlh->nlmsg_seq, |
| 931 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 932 | if (err) { |
| 933 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 934 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 935 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 936 | idx++; |
| 937 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 938 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 939 | } |
| 940 | out: |
| 941 | mutex_unlock(&devlink_mutex); |
| 942 | |
| 943 | cb->args[0] = idx; |
| 944 | return msg->len; |
| 945 | } |
| 946 | |
| 947 | static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink, |
| 948 | struct devlink_sb *devlink_sb, |
| 949 | u16 pool_index, enum devlink_command cmd, |
| 950 | u32 portid, u32 seq, int flags) |
| 951 | { |
| 952 | struct devlink_sb_pool_info pool_info; |
| 953 | void *hdr; |
| 954 | int err; |
| 955 | |
| 956 | err = devlink->ops->sb_pool_get(devlink, devlink_sb->index, |
| 957 | pool_index, &pool_info); |
| 958 | if (err) |
| 959 | return err; |
| 960 | |
| 961 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 962 | if (!hdr) |
| 963 | return -EMSGSIZE; |
| 964 | |
| 965 | if (devlink_nl_put_handle(msg, devlink)) |
| 966 | goto nla_put_failure; |
| 967 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 968 | goto nla_put_failure; |
| 969 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 970 | goto nla_put_failure; |
| 971 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type)) |
| 972 | goto nla_put_failure; |
| 973 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size)) |
| 974 | goto nla_put_failure; |
| 975 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE, |
| 976 | pool_info.threshold_type)) |
| 977 | goto nla_put_failure; |
Jakub Kicinski | bff5731 | 2019-02-01 17:56:28 -0800 | [diff] [blame] | 978 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE, |
| 979 | pool_info.cell_size)) |
| 980 | goto nla_put_failure; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 981 | |
| 982 | genlmsg_end(msg, hdr); |
| 983 | return 0; |
| 984 | |
| 985 | nla_put_failure: |
| 986 | genlmsg_cancel(msg, hdr); |
| 987 | return -EMSGSIZE; |
| 988 | } |
| 989 | |
| 990 | static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb, |
| 991 | struct genl_info *info) |
| 992 | { |
| 993 | struct devlink *devlink = info->user_ptr[0]; |
| 994 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 995 | struct sk_buff *msg; |
| 996 | u16 pool_index; |
| 997 | int err; |
| 998 | |
| 999 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1000 | &pool_index); |
| 1001 | if (err) |
| 1002 | return err; |
| 1003 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1004 | if (!devlink->ops->sb_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1005 | return -EOPNOTSUPP; |
| 1006 | |
| 1007 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1008 | if (!msg) |
| 1009 | return -ENOMEM; |
| 1010 | |
| 1011 | err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index, |
| 1012 | DEVLINK_CMD_SB_POOL_NEW, |
| 1013 | info->snd_portid, info->snd_seq, 0); |
| 1014 | if (err) { |
| 1015 | nlmsg_free(msg); |
| 1016 | return err; |
| 1017 | } |
| 1018 | |
| 1019 | return genlmsg_reply(msg, info); |
| 1020 | } |
| 1021 | |
| 1022 | static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1023 | struct devlink *devlink, |
| 1024 | struct devlink_sb *devlink_sb, |
| 1025 | u32 portid, u32 seq) |
| 1026 | { |
| 1027 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1028 | u16 pool_index; |
| 1029 | int err; |
| 1030 | |
| 1031 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1032 | if (*p_idx < start) { |
| 1033 | (*p_idx)++; |
| 1034 | continue; |
| 1035 | } |
| 1036 | err = devlink_nl_sb_pool_fill(msg, devlink, |
| 1037 | devlink_sb, |
| 1038 | pool_index, |
| 1039 | DEVLINK_CMD_SB_POOL_NEW, |
| 1040 | portid, seq, NLM_F_MULTI); |
| 1041 | if (err) |
| 1042 | return err; |
| 1043 | (*p_idx)++; |
| 1044 | } |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg, |
| 1049 | struct netlink_callback *cb) |
| 1050 | { |
| 1051 | struct devlink *devlink; |
| 1052 | struct devlink_sb *devlink_sb; |
| 1053 | int start = cb->args[0]; |
| 1054 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1055 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1056 | |
| 1057 | mutex_lock(&devlink_mutex); |
| 1058 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1059 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1060 | !devlink->ops->sb_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1061 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1062 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1063 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1064 | err = __sb_pool_get_dumpit(msg, start, &idx, devlink, |
| 1065 | devlink_sb, |
| 1066 | NETLINK_CB(cb->skb).portid, |
| 1067 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1068 | if (err && err != -EOPNOTSUPP) { |
| 1069 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1070 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1071 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1072 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1073 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1074 | } |
| 1075 | out: |
| 1076 | mutex_unlock(&devlink_mutex); |
| 1077 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1078 | if (err != -EMSGSIZE) |
| 1079 | return err; |
| 1080 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1081 | cb->args[0] = idx; |
| 1082 | return msg->len; |
| 1083 | } |
| 1084 | |
| 1085 | static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index, |
| 1086 | u16 pool_index, u32 size, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1087 | enum devlink_sb_threshold_type threshold_type, |
| 1088 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1089 | |
| 1090 | { |
| 1091 | const struct devlink_ops *ops = devlink->ops; |
| 1092 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1093 | if (ops->sb_pool_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1094 | return ops->sb_pool_set(devlink, sb_index, pool_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1095 | size, threshold_type, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1096 | return -EOPNOTSUPP; |
| 1097 | } |
| 1098 | |
| 1099 | static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb, |
| 1100 | struct genl_info *info) |
| 1101 | { |
| 1102 | struct devlink *devlink = info->user_ptr[0]; |
| 1103 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1104 | enum devlink_sb_threshold_type threshold_type; |
| 1105 | u16 pool_index; |
| 1106 | u32 size; |
| 1107 | int err; |
| 1108 | |
| 1109 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1110 | &pool_index); |
| 1111 | if (err) |
| 1112 | return err; |
| 1113 | |
| 1114 | err = devlink_sb_th_type_get_from_info(info, &threshold_type); |
| 1115 | if (err) |
| 1116 | return err; |
| 1117 | |
| 1118 | if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]) |
| 1119 | return -EINVAL; |
| 1120 | |
| 1121 | size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]); |
| 1122 | return devlink_sb_pool_set(devlink, devlink_sb->index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1123 | pool_index, size, threshold_type, |
| 1124 | info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg, |
| 1128 | struct devlink *devlink, |
| 1129 | struct devlink_port *devlink_port, |
| 1130 | struct devlink_sb *devlink_sb, |
| 1131 | u16 pool_index, |
| 1132 | enum devlink_command cmd, |
| 1133 | u32 portid, u32 seq, int flags) |
| 1134 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1135 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1136 | u32 threshold; |
| 1137 | void *hdr; |
| 1138 | int err; |
| 1139 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1140 | err = ops->sb_port_pool_get(devlink_port, devlink_sb->index, |
| 1141 | pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1142 | if (err) |
| 1143 | return err; |
| 1144 | |
| 1145 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1146 | if (!hdr) |
| 1147 | return -EMSGSIZE; |
| 1148 | |
| 1149 | if (devlink_nl_put_handle(msg, devlink)) |
| 1150 | goto nla_put_failure; |
| 1151 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1152 | goto nla_put_failure; |
| 1153 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1154 | goto nla_put_failure; |
| 1155 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1156 | goto nla_put_failure; |
| 1157 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1158 | goto nla_put_failure; |
| 1159 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1160 | if (ops->sb_occ_port_pool_get) { |
| 1161 | u32 cur; |
| 1162 | u32 max; |
| 1163 | |
| 1164 | err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index, |
| 1165 | pool_index, &cur, &max); |
| 1166 | if (err && err != -EOPNOTSUPP) |
| 1167 | return err; |
| 1168 | if (!err) { |
| 1169 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1170 | goto nla_put_failure; |
| 1171 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1172 | goto nla_put_failure; |
| 1173 | } |
| 1174 | } |
| 1175 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1176 | genlmsg_end(msg, hdr); |
| 1177 | return 0; |
| 1178 | |
| 1179 | nla_put_failure: |
| 1180 | genlmsg_cancel(msg, hdr); |
| 1181 | return -EMSGSIZE; |
| 1182 | } |
| 1183 | |
| 1184 | static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb, |
| 1185 | struct genl_info *info) |
| 1186 | { |
| 1187 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1188 | struct devlink *devlink = devlink_port->devlink; |
| 1189 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1190 | struct sk_buff *msg; |
| 1191 | u16 pool_index; |
| 1192 | int err; |
| 1193 | |
| 1194 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1195 | &pool_index); |
| 1196 | if (err) |
| 1197 | return err; |
| 1198 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1199 | if (!devlink->ops->sb_port_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1200 | return -EOPNOTSUPP; |
| 1201 | |
| 1202 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1203 | if (!msg) |
| 1204 | return -ENOMEM; |
| 1205 | |
| 1206 | err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port, |
| 1207 | devlink_sb, pool_index, |
| 1208 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1209 | info->snd_portid, info->snd_seq, 0); |
| 1210 | if (err) { |
| 1211 | nlmsg_free(msg); |
| 1212 | return err; |
| 1213 | } |
| 1214 | |
| 1215 | return genlmsg_reply(msg, info); |
| 1216 | } |
| 1217 | |
| 1218 | static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1219 | struct devlink *devlink, |
| 1220 | struct devlink_sb *devlink_sb, |
| 1221 | u32 portid, u32 seq) |
| 1222 | { |
| 1223 | struct devlink_port *devlink_port; |
| 1224 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1225 | u16 pool_index; |
| 1226 | int err; |
| 1227 | |
| 1228 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1229 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1230 | if (*p_idx < start) { |
| 1231 | (*p_idx)++; |
| 1232 | continue; |
| 1233 | } |
| 1234 | err = devlink_nl_sb_port_pool_fill(msg, devlink, |
| 1235 | devlink_port, |
| 1236 | devlink_sb, |
| 1237 | pool_index, |
| 1238 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1239 | portid, seq, |
| 1240 | NLM_F_MULTI); |
| 1241 | if (err) |
| 1242 | return err; |
| 1243 | (*p_idx)++; |
| 1244 | } |
| 1245 | } |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg, |
| 1250 | struct netlink_callback *cb) |
| 1251 | { |
| 1252 | struct devlink *devlink; |
| 1253 | struct devlink_sb *devlink_sb; |
| 1254 | int start = cb->args[0]; |
| 1255 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1256 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1257 | |
| 1258 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1259 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1260 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1261 | !devlink->ops->sb_port_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1262 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1263 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1264 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1265 | err = __sb_port_pool_get_dumpit(msg, start, &idx, |
| 1266 | devlink, devlink_sb, |
| 1267 | NETLINK_CB(cb->skb).portid, |
| 1268 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1269 | if (err && err != -EOPNOTSUPP) { |
| 1270 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1271 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1272 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1273 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1274 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1275 | } |
| 1276 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1277 | mutex_unlock(&devlink_mutex); |
| 1278 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1279 | if (err != -EMSGSIZE) |
| 1280 | return err; |
| 1281 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1282 | cb->args[0] = idx; |
| 1283 | return msg->len; |
| 1284 | } |
| 1285 | |
| 1286 | static int devlink_sb_port_pool_set(struct devlink_port *devlink_port, |
| 1287 | unsigned int sb_index, u16 pool_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1288 | u32 threshold, |
| 1289 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1290 | |
| 1291 | { |
| 1292 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1293 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1294 | if (ops->sb_port_pool_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1295 | return ops->sb_port_pool_set(devlink_port, sb_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1296 | pool_index, threshold, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1297 | return -EOPNOTSUPP; |
| 1298 | } |
| 1299 | |
| 1300 | static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb, |
| 1301 | struct genl_info *info) |
| 1302 | { |
| 1303 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1304 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1305 | u16 pool_index; |
| 1306 | u32 threshold; |
| 1307 | int err; |
| 1308 | |
| 1309 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1310 | &pool_index); |
| 1311 | if (err) |
| 1312 | return err; |
| 1313 | |
| 1314 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1315 | return -EINVAL; |
| 1316 | |
| 1317 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1318 | return devlink_sb_port_pool_set(devlink_port, devlink_sb->index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1319 | pool_index, threshold, info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | static int |
| 1323 | devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1324 | struct devlink_port *devlink_port, |
| 1325 | struct devlink_sb *devlink_sb, u16 tc_index, |
| 1326 | enum devlink_sb_pool_type pool_type, |
| 1327 | enum devlink_command cmd, |
| 1328 | u32 portid, u32 seq, int flags) |
| 1329 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1330 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1331 | u16 pool_index; |
| 1332 | u32 threshold; |
| 1333 | void *hdr; |
| 1334 | int err; |
| 1335 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1336 | err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index, |
| 1337 | tc_index, pool_type, |
| 1338 | &pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1339 | if (err) |
| 1340 | return err; |
| 1341 | |
| 1342 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1343 | if (!hdr) |
| 1344 | return -EMSGSIZE; |
| 1345 | |
| 1346 | if (devlink_nl_put_handle(msg, devlink)) |
| 1347 | goto nla_put_failure; |
| 1348 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1349 | goto nla_put_failure; |
| 1350 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1351 | goto nla_put_failure; |
| 1352 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index)) |
| 1353 | goto nla_put_failure; |
| 1354 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type)) |
| 1355 | goto nla_put_failure; |
| 1356 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1357 | goto nla_put_failure; |
| 1358 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1359 | goto nla_put_failure; |
| 1360 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1361 | if (ops->sb_occ_tc_port_bind_get) { |
| 1362 | u32 cur; |
| 1363 | u32 max; |
| 1364 | |
| 1365 | err = ops->sb_occ_tc_port_bind_get(devlink_port, |
| 1366 | devlink_sb->index, |
| 1367 | tc_index, pool_type, |
| 1368 | &cur, &max); |
| 1369 | if (err && err != -EOPNOTSUPP) |
| 1370 | return err; |
| 1371 | if (!err) { |
| 1372 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1373 | goto nla_put_failure; |
| 1374 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1375 | goto nla_put_failure; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1379 | genlmsg_end(msg, hdr); |
| 1380 | return 0; |
| 1381 | |
| 1382 | nla_put_failure: |
| 1383 | genlmsg_cancel(msg, hdr); |
| 1384 | return -EMSGSIZE; |
| 1385 | } |
| 1386 | |
| 1387 | static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb, |
| 1388 | struct genl_info *info) |
| 1389 | { |
| 1390 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1391 | struct devlink *devlink = devlink_port->devlink; |
| 1392 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1393 | struct sk_buff *msg; |
| 1394 | enum devlink_sb_pool_type pool_type; |
| 1395 | u16 tc_index; |
| 1396 | int err; |
| 1397 | |
| 1398 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1399 | if (err) |
| 1400 | return err; |
| 1401 | |
| 1402 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1403 | pool_type, &tc_index); |
| 1404 | if (err) |
| 1405 | return err; |
| 1406 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1407 | if (!devlink->ops->sb_tc_pool_bind_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1408 | return -EOPNOTSUPP; |
| 1409 | |
| 1410 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1411 | if (!msg) |
| 1412 | return -ENOMEM; |
| 1413 | |
| 1414 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port, |
| 1415 | devlink_sb, tc_index, pool_type, |
| 1416 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1417 | info->snd_portid, |
| 1418 | info->snd_seq, 0); |
| 1419 | if (err) { |
| 1420 | nlmsg_free(msg); |
| 1421 | return err; |
| 1422 | } |
| 1423 | |
| 1424 | return genlmsg_reply(msg, info); |
| 1425 | } |
| 1426 | |
| 1427 | static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1428 | int start, int *p_idx, |
| 1429 | struct devlink *devlink, |
| 1430 | struct devlink_sb *devlink_sb, |
| 1431 | u32 portid, u32 seq) |
| 1432 | { |
| 1433 | struct devlink_port *devlink_port; |
| 1434 | u16 tc_index; |
| 1435 | int err; |
| 1436 | |
| 1437 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1438 | for (tc_index = 0; |
| 1439 | tc_index < devlink_sb->ingress_tc_count; tc_index++) { |
| 1440 | if (*p_idx < start) { |
| 1441 | (*p_idx)++; |
| 1442 | continue; |
| 1443 | } |
| 1444 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1445 | devlink_port, |
| 1446 | devlink_sb, |
| 1447 | tc_index, |
| 1448 | DEVLINK_SB_POOL_TYPE_INGRESS, |
| 1449 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1450 | portid, seq, |
| 1451 | NLM_F_MULTI); |
| 1452 | if (err) |
| 1453 | return err; |
| 1454 | (*p_idx)++; |
| 1455 | } |
| 1456 | for (tc_index = 0; |
| 1457 | tc_index < devlink_sb->egress_tc_count; tc_index++) { |
| 1458 | if (*p_idx < start) { |
| 1459 | (*p_idx)++; |
| 1460 | continue; |
| 1461 | } |
| 1462 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1463 | devlink_port, |
| 1464 | devlink_sb, |
| 1465 | tc_index, |
| 1466 | DEVLINK_SB_POOL_TYPE_EGRESS, |
| 1467 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1468 | portid, seq, |
| 1469 | NLM_F_MULTI); |
| 1470 | if (err) |
| 1471 | return err; |
| 1472 | (*p_idx)++; |
| 1473 | } |
| 1474 | } |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | static int |
| 1479 | devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1480 | struct netlink_callback *cb) |
| 1481 | { |
| 1482 | struct devlink *devlink; |
| 1483 | struct devlink_sb *devlink_sb; |
| 1484 | int start = cb->args[0]; |
| 1485 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1486 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1487 | |
| 1488 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1489 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1490 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1491 | !devlink->ops->sb_tc_pool_bind_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1492 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1493 | |
| 1494 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1495 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1496 | err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx, |
| 1497 | devlink, |
| 1498 | devlink_sb, |
| 1499 | NETLINK_CB(cb->skb).portid, |
| 1500 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1501 | if (err && err != -EOPNOTSUPP) { |
| 1502 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1503 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1504 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1505 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1506 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1507 | } |
| 1508 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1509 | mutex_unlock(&devlink_mutex); |
| 1510 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1511 | if (err != -EMSGSIZE) |
| 1512 | return err; |
| 1513 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1514 | cb->args[0] = idx; |
| 1515 | return msg->len; |
| 1516 | } |
| 1517 | |
| 1518 | static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port, |
| 1519 | unsigned int sb_index, u16 tc_index, |
| 1520 | enum devlink_sb_pool_type pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1521 | u16 pool_index, u32 threshold, |
| 1522 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1523 | |
| 1524 | { |
| 1525 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1526 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1527 | if (ops->sb_tc_pool_bind_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1528 | return ops->sb_tc_pool_bind_set(devlink_port, sb_index, |
| 1529 | tc_index, pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1530 | pool_index, threshold, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1531 | return -EOPNOTSUPP; |
| 1532 | } |
| 1533 | |
| 1534 | static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb, |
| 1535 | struct genl_info *info) |
| 1536 | { |
| 1537 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1538 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1539 | enum devlink_sb_pool_type pool_type; |
| 1540 | u16 tc_index; |
| 1541 | u16 pool_index; |
| 1542 | u32 threshold; |
| 1543 | int err; |
| 1544 | |
| 1545 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1546 | if (err) |
| 1547 | return err; |
| 1548 | |
| 1549 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1550 | pool_type, &tc_index); |
| 1551 | if (err) |
| 1552 | return err; |
| 1553 | |
| 1554 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1555 | &pool_index); |
| 1556 | if (err) |
| 1557 | return err; |
| 1558 | |
| 1559 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1560 | return -EINVAL; |
| 1561 | |
| 1562 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1563 | return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index, |
| 1564 | tc_index, pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1565 | pool_index, threshold, info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1568 | static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb, |
| 1569 | struct genl_info *info) |
| 1570 | { |
| 1571 | struct devlink *devlink = info->user_ptr[0]; |
| 1572 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1573 | const struct devlink_ops *ops = devlink->ops; |
| 1574 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1575 | if (ops->sb_occ_snapshot) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1576 | return ops->sb_occ_snapshot(devlink, devlink_sb->index); |
| 1577 | return -EOPNOTSUPP; |
| 1578 | } |
| 1579 | |
| 1580 | static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb, |
| 1581 | struct genl_info *info) |
| 1582 | { |
| 1583 | struct devlink *devlink = info->user_ptr[0]; |
| 1584 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1585 | const struct devlink_ops *ops = devlink->ops; |
| 1586 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1587 | if (ops->sb_occ_max_clear) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1588 | return ops->sb_occ_max_clear(devlink, devlink_sb->index); |
| 1589 | return -EOPNOTSUPP; |
| 1590 | } |
| 1591 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1592 | static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1593 | enum devlink_command cmd, u32 portid, |
| 1594 | u32 seq, int flags) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1595 | { |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1596 | const struct devlink_ops *ops = devlink->ops; |
Leon Romanovsky | 98fdbea | 2019-06-12 15:20:11 +0300 | [diff] [blame] | 1597 | enum devlink_eswitch_encap_mode encap_mode; |
| 1598 | u8 inline_mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1599 | void *hdr; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1600 | int err = 0; |
| 1601 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1602 | |
| 1603 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1604 | if (!hdr) |
| 1605 | return -EMSGSIZE; |
| 1606 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1607 | err = devlink_nl_put_handle(msg, devlink); |
| 1608 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1609 | goto nla_put_failure; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1610 | |
Jiri Pirko | 4456f61 | 2017-02-09 15:54:36 +0100 | [diff] [blame] | 1611 | if (ops->eswitch_mode_get) { |
| 1612 | err = ops->eswitch_mode_get(devlink, &mode); |
| 1613 | if (err) |
| 1614 | goto nla_put_failure; |
| 1615 | err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode); |
| 1616 | if (err) |
| 1617 | goto nla_put_failure; |
| 1618 | } |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1619 | |
| 1620 | if (ops->eswitch_inline_mode_get) { |
| 1621 | err = ops->eswitch_inline_mode_get(devlink, &inline_mode); |
| 1622 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1623 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1624 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE, |
| 1625 | inline_mode); |
| 1626 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1627 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1628 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1629 | |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1630 | if (ops->eswitch_encap_mode_get) { |
| 1631 | err = ops->eswitch_encap_mode_get(devlink, &encap_mode); |
| 1632 | if (err) |
| 1633 | goto nla_put_failure; |
| 1634 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode); |
| 1635 | if (err) |
| 1636 | goto nla_put_failure; |
| 1637 | } |
| 1638 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1639 | genlmsg_end(msg, hdr); |
| 1640 | return 0; |
| 1641 | |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1642 | nla_put_failure: |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1643 | genlmsg_cancel(msg, hdr); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1644 | return err; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1645 | } |
| 1646 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1647 | static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, |
| 1648 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1649 | { |
| 1650 | struct devlink *devlink = info->user_ptr[0]; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1651 | struct sk_buff *msg; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1652 | int err; |
| 1653 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1654 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1655 | if (!msg) |
| 1656 | return -ENOMEM; |
| 1657 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1658 | err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET, |
| 1659 | info->snd_portid, info->snd_seq, 0); |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1660 | |
| 1661 | if (err) { |
| 1662 | nlmsg_free(msg); |
| 1663 | return err; |
| 1664 | } |
| 1665 | |
| 1666 | return genlmsg_reply(msg, info); |
| 1667 | } |
| 1668 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1669 | static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, |
| 1670 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1671 | { |
| 1672 | struct devlink *devlink = info->user_ptr[0]; |
| 1673 | const struct devlink_ops *ops = devlink->ops; |
Leon Romanovsky | 98fdbea | 2019-06-12 15:20:11 +0300 | [diff] [blame] | 1674 | enum devlink_eswitch_encap_mode encap_mode; |
| 1675 | u8 inline_mode; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1676 | int err = 0; |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1677 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1678 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1679 | if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) { |
| 1680 | if (!ops->eswitch_mode_set) |
| 1681 | return -EOPNOTSUPP; |
| 1682 | mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1683 | err = ops->eswitch_mode_set(devlink, mode, info->extack); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1684 | if (err) |
| 1685 | return err; |
| 1686 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1687 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1688 | if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) { |
| 1689 | if (!ops->eswitch_inline_mode_set) |
| 1690 | return -EOPNOTSUPP; |
| 1691 | inline_mode = nla_get_u8( |
| 1692 | info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1693 | err = ops->eswitch_inline_mode_set(devlink, inline_mode, |
| 1694 | info->extack); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1695 | if (err) |
| 1696 | return err; |
| 1697 | } |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1698 | |
| 1699 | if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) { |
| 1700 | if (!ops->eswitch_encap_mode_set) |
| 1701 | return -EOPNOTSUPP; |
| 1702 | encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1703 | err = ops->eswitch_encap_mode_set(devlink, encap_mode, |
| 1704 | info->extack); |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1705 | if (err) |
| 1706 | return err; |
| 1707 | } |
| 1708 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1709 | return 0; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1710 | } |
| 1711 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1712 | int devlink_dpipe_match_put(struct sk_buff *skb, |
| 1713 | struct devlink_dpipe_match *match) |
| 1714 | { |
| 1715 | struct devlink_dpipe_header *header = match->header; |
| 1716 | struct devlink_dpipe_field *field = &header->fields[match->field_id]; |
| 1717 | struct nlattr *match_attr; |
| 1718 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1719 | match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1720 | if (!match_attr) |
| 1721 | return -EMSGSIZE; |
| 1722 | |
| 1723 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) || |
| 1724 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) || |
| 1725 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1726 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1727 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1728 | goto nla_put_failure; |
| 1729 | |
| 1730 | nla_nest_end(skb, match_attr); |
| 1731 | return 0; |
| 1732 | |
| 1733 | nla_put_failure: |
| 1734 | nla_nest_cancel(skb, match_attr); |
| 1735 | return -EMSGSIZE; |
| 1736 | } |
| 1737 | EXPORT_SYMBOL_GPL(devlink_dpipe_match_put); |
| 1738 | |
| 1739 | static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table, |
| 1740 | struct sk_buff *skb) |
| 1741 | { |
| 1742 | struct nlattr *matches_attr; |
| 1743 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1744 | matches_attr = nla_nest_start_noflag(skb, |
| 1745 | DEVLINK_ATTR_DPIPE_TABLE_MATCHES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1746 | if (!matches_attr) |
| 1747 | return -EMSGSIZE; |
| 1748 | |
| 1749 | if (table->table_ops->matches_dump(table->priv, skb)) |
| 1750 | goto nla_put_failure; |
| 1751 | |
| 1752 | nla_nest_end(skb, matches_attr); |
| 1753 | return 0; |
| 1754 | |
| 1755 | nla_put_failure: |
| 1756 | nla_nest_cancel(skb, matches_attr); |
| 1757 | return -EMSGSIZE; |
| 1758 | } |
| 1759 | |
| 1760 | int devlink_dpipe_action_put(struct sk_buff *skb, |
| 1761 | struct devlink_dpipe_action *action) |
| 1762 | { |
| 1763 | struct devlink_dpipe_header *header = action->header; |
| 1764 | struct devlink_dpipe_field *field = &header->fields[action->field_id]; |
| 1765 | struct nlattr *action_attr; |
| 1766 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1767 | action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1768 | if (!action_attr) |
| 1769 | return -EMSGSIZE; |
| 1770 | |
| 1771 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) || |
| 1772 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) || |
| 1773 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1774 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1775 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1776 | goto nla_put_failure; |
| 1777 | |
| 1778 | nla_nest_end(skb, action_attr); |
| 1779 | return 0; |
| 1780 | |
| 1781 | nla_put_failure: |
| 1782 | nla_nest_cancel(skb, action_attr); |
| 1783 | return -EMSGSIZE; |
| 1784 | } |
| 1785 | EXPORT_SYMBOL_GPL(devlink_dpipe_action_put); |
| 1786 | |
| 1787 | static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table, |
| 1788 | struct sk_buff *skb) |
| 1789 | { |
| 1790 | struct nlattr *actions_attr; |
| 1791 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1792 | actions_attr = nla_nest_start_noflag(skb, |
| 1793 | DEVLINK_ATTR_DPIPE_TABLE_ACTIONS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1794 | if (!actions_attr) |
| 1795 | return -EMSGSIZE; |
| 1796 | |
| 1797 | if (table->table_ops->actions_dump(table->priv, skb)) |
| 1798 | goto nla_put_failure; |
| 1799 | |
| 1800 | nla_nest_end(skb, actions_attr); |
| 1801 | return 0; |
| 1802 | |
| 1803 | nla_put_failure: |
| 1804 | nla_nest_cancel(skb, actions_attr); |
| 1805 | return -EMSGSIZE; |
| 1806 | } |
| 1807 | |
| 1808 | static int devlink_dpipe_table_put(struct sk_buff *skb, |
| 1809 | struct devlink_dpipe_table *table) |
| 1810 | { |
| 1811 | struct nlattr *table_attr; |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1812 | u64 table_size; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1813 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1814 | table_size = table->table_ops->size_get(table->priv); |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1815 | table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1816 | if (!table_attr) |
| 1817 | return -EMSGSIZE; |
| 1818 | |
| 1819 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) || |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1820 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1821 | DEVLINK_ATTR_PAD)) |
| 1822 | goto nla_put_failure; |
| 1823 | if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED, |
| 1824 | table->counters_enabled)) |
| 1825 | goto nla_put_failure; |
| 1826 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1827 | if (table->resource_valid) { |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 1828 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID, |
| 1829 | table->resource_id, DEVLINK_ATTR_PAD) || |
| 1830 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS, |
| 1831 | table->resource_units, DEVLINK_ATTR_PAD)) |
| 1832 | goto nla_put_failure; |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1833 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1834 | if (devlink_dpipe_matches_put(table, skb)) |
| 1835 | goto nla_put_failure; |
| 1836 | |
| 1837 | if (devlink_dpipe_actions_put(table, skb)) |
| 1838 | goto nla_put_failure; |
| 1839 | |
| 1840 | nla_nest_end(skb, table_attr); |
| 1841 | return 0; |
| 1842 | |
| 1843 | nla_put_failure: |
| 1844 | nla_nest_cancel(skb, table_attr); |
| 1845 | return -EMSGSIZE; |
| 1846 | } |
| 1847 | |
| 1848 | static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb, |
| 1849 | struct genl_info *info) |
| 1850 | { |
| 1851 | int err; |
| 1852 | |
| 1853 | if (*pskb) { |
| 1854 | err = genlmsg_reply(*pskb, info); |
| 1855 | if (err) |
| 1856 | return err; |
| 1857 | } |
| 1858 | *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1859 | if (!*pskb) |
| 1860 | return -ENOMEM; |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
| 1864 | static int devlink_dpipe_tables_fill(struct genl_info *info, |
| 1865 | enum devlink_command cmd, int flags, |
| 1866 | struct list_head *dpipe_tables, |
| 1867 | const char *table_name) |
| 1868 | { |
| 1869 | struct devlink *devlink = info->user_ptr[0]; |
| 1870 | struct devlink_dpipe_table *table; |
| 1871 | struct nlattr *tables_attr; |
| 1872 | struct sk_buff *skb = NULL; |
| 1873 | struct nlmsghdr *nlh; |
| 1874 | bool incomplete; |
| 1875 | void *hdr; |
| 1876 | int i; |
| 1877 | int err; |
| 1878 | |
| 1879 | table = list_first_entry(dpipe_tables, |
| 1880 | struct devlink_dpipe_table, list); |
| 1881 | start_again: |
| 1882 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1883 | if (err) |
| 1884 | return err; |
| 1885 | |
| 1886 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1887 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 1888 | if (!hdr) { |
| 1889 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1890 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 1891 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1892 | |
| 1893 | if (devlink_nl_put_handle(skb, devlink)) |
| 1894 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1895 | tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1896 | if (!tables_attr) |
| 1897 | goto nla_put_failure; |
| 1898 | |
| 1899 | i = 0; |
| 1900 | incomplete = false; |
| 1901 | list_for_each_entry_from(table, dpipe_tables, list) { |
| 1902 | if (!table_name) { |
| 1903 | err = devlink_dpipe_table_put(skb, table); |
| 1904 | if (err) { |
| 1905 | if (!i) |
| 1906 | goto err_table_put; |
| 1907 | incomplete = true; |
| 1908 | break; |
| 1909 | } |
| 1910 | } else { |
| 1911 | if (!strcmp(table->name, table_name)) { |
| 1912 | err = devlink_dpipe_table_put(skb, table); |
| 1913 | if (err) |
| 1914 | break; |
| 1915 | } |
| 1916 | } |
| 1917 | i++; |
| 1918 | } |
| 1919 | |
| 1920 | nla_nest_end(skb, tables_attr); |
| 1921 | genlmsg_end(skb, hdr); |
| 1922 | if (incomplete) |
| 1923 | goto start_again; |
| 1924 | |
| 1925 | send_done: |
| 1926 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1927 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 1928 | if (!nlh) { |
| 1929 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1930 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 1931 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1932 | goto send_done; |
| 1933 | } |
| 1934 | |
| 1935 | return genlmsg_reply(skb, info); |
| 1936 | |
| 1937 | nla_put_failure: |
| 1938 | err = -EMSGSIZE; |
| 1939 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1940 | nlmsg_free(skb); |
| 1941 | return err; |
| 1942 | } |
| 1943 | |
| 1944 | static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb, |
| 1945 | struct genl_info *info) |
| 1946 | { |
| 1947 | struct devlink *devlink = info->user_ptr[0]; |
| 1948 | const char *table_name = NULL; |
| 1949 | |
| 1950 | if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 1951 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 1952 | |
| 1953 | return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0, |
| 1954 | &devlink->dpipe_table_list, |
| 1955 | table_name); |
| 1956 | } |
| 1957 | |
| 1958 | static int devlink_dpipe_value_put(struct sk_buff *skb, |
| 1959 | struct devlink_dpipe_value *value) |
| 1960 | { |
| 1961 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE, |
| 1962 | value->value_size, value->value)) |
| 1963 | return -EMSGSIZE; |
| 1964 | if (value->mask) |
| 1965 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK, |
| 1966 | value->value_size, value->mask)) |
| 1967 | return -EMSGSIZE; |
| 1968 | if (value->mapping_valid) |
| 1969 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING, |
| 1970 | value->mapping_value)) |
| 1971 | return -EMSGSIZE; |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
| 1975 | static int devlink_dpipe_action_value_put(struct sk_buff *skb, |
| 1976 | struct devlink_dpipe_value *value) |
| 1977 | { |
| 1978 | if (!value->action) |
| 1979 | return -EINVAL; |
| 1980 | if (devlink_dpipe_action_put(skb, value->action)) |
| 1981 | return -EMSGSIZE; |
| 1982 | if (devlink_dpipe_value_put(skb, value)) |
| 1983 | return -EMSGSIZE; |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | static int devlink_dpipe_action_values_put(struct sk_buff *skb, |
| 1988 | struct devlink_dpipe_value *values, |
| 1989 | unsigned int values_count) |
| 1990 | { |
| 1991 | struct nlattr *action_attr; |
| 1992 | int i; |
| 1993 | int err; |
| 1994 | |
| 1995 | for (i = 0; i < values_count; i++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1996 | action_attr = nla_nest_start_noflag(skb, |
| 1997 | DEVLINK_ATTR_DPIPE_ACTION_VALUE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1998 | if (!action_attr) |
| 1999 | return -EMSGSIZE; |
| 2000 | err = devlink_dpipe_action_value_put(skb, &values[i]); |
| 2001 | if (err) |
| 2002 | goto err_action_value_put; |
| 2003 | nla_nest_end(skb, action_attr); |
| 2004 | } |
| 2005 | return 0; |
| 2006 | |
| 2007 | err_action_value_put: |
| 2008 | nla_nest_cancel(skb, action_attr); |
| 2009 | return err; |
| 2010 | } |
| 2011 | |
| 2012 | static int devlink_dpipe_match_value_put(struct sk_buff *skb, |
| 2013 | struct devlink_dpipe_value *value) |
| 2014 | { |
| 2015 | if (!value->match) |
| 2016 | return -EINVAL; |
| 2017 | if (devlink_dpipe_match_put(skb, value->match)) |
| 2018 | return -EMSGSIZE; |
| 2019 | if (devlink_dpipe_value_put(skb, value)) |
| 2020 | return -EMSGSIZE; |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
| 2024 | static int devlink_dpipe_match_values_put(struct sk_buff *skb, |
| 2025 | struct devlink_dpipe_value *values, |
| 2026 | unsigned int values_count) |
| 2027 | { |
| 2028 | struct nlattr *match_attr; |
| 2029 | int i; |
| 2030 | int err; |
| 2031 | |
| 2032 | for (i = 0; i < values_count; i++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2033 | match_attr = nla_nest_start_noflag(skb, |
| 2034 | DEVLINK_ATTR_DPIPE_MATCH_VALUE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2035 | if (!match_attr) |
| 2036 | return -EMSGSIZE; |
| 2037 | err = devlink_dpipe_match_value_put(skb, &values[i]); |
| 2038 | if (err) |
| 2039 | goto err_match_value_put; |
| 2040 | nla_nest_end(skb, match_attr); |
| 2041 | } |
| 2042 | return 0; |
| 2043 | |
| 2044 | err_match_value_put: |
| 2045 | nla_nest_cancel(skb, match_attr); |
| 2046 | return err; |
| 2047 | } |
| 2048 | |
| 2049 | static int devlink_dpipe_entry_put(struct sk_buff *skb, |
| 2050 | struct devlink_dpipe_entry *entry) |
| 2051 | { |
| 2052 | struct nlattr *entry_attr, *matches_attr, *actions_attr; |
| 2053 | int err; |
| 2054 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2055 | entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2056 | if (!entry_attr) |
| 2057 | return -EMSGSIZE; |
| 2058 | |
| 2059 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index, |
| 2060 | DEVLINK_ATTR_PAD)) |
| 2061 | goto nla_put_failure; |
| 2062 | if (entry->counter_valid) |
| 2063 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER, |
| 2064 | entry->counter, DEVLINK_ATTR_PAD)) |
| 2065 | goto nla_put_failure; |
| 2066 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2067 | matches_attr = nla_nest_start_noflag(skb, |
| 2068 | DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2069 | if (!matches_attr) |
| 2070 | goto nla_put_failure; |
| 2071 | |
| 2072 | err = devlink_dpipe_match_values_put(skb, entry->match_values, |
| 2073 | entry->match_values_count); |
| 2074 | if (err) { |
| 2075 | nla_nest_cancel(skb, matches_attr); |
| 2076 | goto err_match_values_put; |
| 2077 | } |
| 2078 | nla_nest_end(skb, matches_attr); |
| 2079 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2080 | actions_attr = nla_nest_start_noflag(skb, |
| 2081 | DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2082 | if (!actions_attr) |
| 2083 | goto nla_put_failure; |
| 2084 | |
| 2085 | err = devlink_dpipe_action_values_put(skb, entry->action_values, |
| 2086 | entry->action_values_count); |
| 2087 | if (err) { |
| 2088 | nla_nest_cancel(skb, actions_attr); |
| 2089 | goto err_action_values_put; |
| 2090 | } |
| 2091 | nla_nest_end(skb, actions_attr); |
| 2092 | |
| 2093 | nla_nest_end(skb, entry_attr); |
| 2094 | return 0; |
| 2095 | |
| 2096 | nla_put_failure: |
| 2097 | err = -EMSGSIZE; |
| 2098 | err_match_values_put: |
| 2099 | err_action_values_put: |
| 2100 | nla_nest_cancel(skb, entry_attr); |
| 2101 | return err; |
| 2102 | } |
| 2103 | |
| 2104 | static struct devlink_dpipe_table * |
| 2105 | devlink_dpipe_table_find(struct list_head *dpipe_tables, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 2106 | const char *table_name, struct devlink *devlink) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2107 | { |
| 2108 | struct devlink_dpipe_table *table; |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 2109 | list_for_each_entry_rcu(table, dpipe_tables, list, |
| 2110 | lockdep_is_held(&devlink->lock)) { |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2111 | if (!strcmp(table->name, table_name)) |
| 2112 | return table; |
| 2113 | } |
| 2114 | return NULL; |
| 2115 | } |
| 2116 | |
| 2117 | int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2118 | { |
| 2119 | struct devlink *devlink; |
| 2120 | int err; |
| 2121 | |
| 2122 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb, |
| 2123 | dump_ctx->info); |
| 2124 | if (err) |
| 2125 | return err; |
| 2126 | |
| 2127 | dump_ctx->hdr = genlmsg_put(dump_ctx->skb, |
| 2128 | dump_ctx->info->snd_portid, |
| 2129 | dump_ctx->info->snd_seq, |
| 2130 | &devlink_nl_family, NLM_F_MULTI, |
| 2131 | dump_ctx->cmd); |
| 2132 | if (!dump_ctx->hdr) |
| 2133 | goto nla_put_failure; |
| 2134 | |
| 2135 | devlink = dump_ctx->info->user_ptr[0]; |
| 2136 | if (devlink_nl_put_handle(dump_ctx->skb, devlink)) |
| 2137 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2138 | dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb, |
| 2139 | DEVLINK_ATTR_DPIPE_ENTRIES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2140 | if (!dump_ctx->nest) |
| 2141 | goto nla_put_failure; |
| 2142 | return 0; |
| 2143 | |
| 2144 | nla_put_failure: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2145 | nlmsg_free(dump_ctx->skb); |
| 2146 | return -EMSGSIZE; |
| 2147 | } |
| 2148 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare); |
| 2149 | |
| 2150 | int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, |
| 2151 | struct devlink_dpipe_entry *entry) |
| 2152 | { |
| 2153 | return devlink_dpipe_entry_put(dump_ctx->skb, entry); |
| 2154 | } |
| 2155 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append); |
| 2156 | |
| 2157 | int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2158 | { |
| 2159 | nla_nest_end(dump_ctx->skb, dump_ctx->nest); |
| 2160 | genlmsg_end(dump_ctx->skb, dump_ctx->hdr); |
| 2161 | return 0; |
| 2162 | } |
| 2163 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close); |
| 2164 | |
Arkadi Sharshevsky | 3580732 | 2017-08-24 08:40:03 +0200 | [diff] [blame] | 2165 | void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry) |
| 2166 | |
| 2167 | { |
| 2168 | unsigned int value_count, value_index; |
| 2169 | struct devlink_dpipe_value *value; |
| 2170 | |
| 2171 | value = entry->action_values; |
| 2172 | value_count = entry->action_values_count; |
| 2173 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2174 | kfree(value[value_index].value); |
| 2175 | kfree(value[value_index].mask); |
| 2176 | } |
| 2177 | |
| 2178 | value = entry->match_values; |
| 2179 | value_count = entry->match_values_count; |
| 2180 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2181 | kfree(value[value_index].value); |
| 2182 | kfree(value[value_index].mask); |
| 2183 | } |
| 2184 | } |
| 2185 | EXPORT_SYMBOL(devlink_dpipe_entry_clear); |
| 2186 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2187 | static int devlink_dpipe_entries_fill(struct genl_info *info, |
| 2188 | enum devlink_command cmd, int flags, |
| 2189 | struct devlink_dpipe_table *table) |
| 2190 | { |
| 2191 | struct devlink_dpipe_dump_ctx dump_ctx; |
| 2192 | struct nlmsghdr *nlh; |
| 2193 | int err; |
| 2194 | |
| 2195 | dump_ctx.skb = NULL; |
| 2196 | dump_ctx.cmd = cmd; |
| 2197 | dump_ctx.info = info; |
| 2198 | |
| 2199 | err = table->table_ops->entries_dump(table->priv, |
| 2200 | table->counters_enabled, |
| 2201 | &dump_ctx); |
| 2202 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2203 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2204 | |
| 2205 | send_done: |
| 2206 | nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq, |
| 2207 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2208 | if (!nlh) { |
| 2209 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info); |
| 2210 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2211 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2212 | goto send_done; |
| 2213 | } |
| 2214 | return genlmsg_reply(dump_ctx.skb, info); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb, |
| 2218 | struct genl_info *info) |
| 2219 | { |
| 2220 | struct devlink *devlink = info->user_ptr[0]; |
| 2221 | struct devlink_dpipe_table *table; |
| 2222 | const char *table_name; |
| 2223 | |
| 2224 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 2225 | return -EINVAL; |
| 2226 | |
| 2227 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2228 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 2229 | table_name, devlink); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2230 | if (!table) |
| 2231 | return -EINVAL; |
| 2232 | |
| 2233 | if (!table->table_ops->entries_dump) |
| 2234 | return -EINVAL; |
| 2235 | |
| 2236 | return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET, |
| 2237 | 0, table); |
| 2238 | } |
| 2239 | |
| 2240 | static int devlink_dpipe_fields_put(struct sk_buff *skb, |
| 2241 | const struct devlink_dpipe_header *header) |
| 2242 | { |
| 2243 | struct devlink_dpipe_field *field; |
| 2244 | struct nlattr *field_attr; |
| 2245 | int i; |
| 2246 | |
| 2247 | for (i = 0; i < header->fields_count; i++) { |
| 2248 | field = &header->fields[i]; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2249 | field_attr = nla_nest_start_noflag(skb, |
| 2250 | DEVLINK_ATTR_DPIPE_FIELD); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2251 | if (!field_attr) |
| 2252 | return -EMSGSIZE; |
| 2253 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) || |
| 2254 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 2255 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) || |
| 2256 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type)) |
| 2257 | goto nla_put_failure; |
| 2258 | nla_nest_end(skb, field_attr); |
| 2259 | } |
| 2260 | return 0; |
| 2261 | |
| 2262 | nla_put_failure: |
| 2263 | nla_nest_cancel(skb, field_attr); |
| 2264 | return -EMSGSIZE; |
| 2265 | } |
| 2266 | |
| 2267 | static int devlink_dpipe_header_put(struct sk_buff *skb, |
| 2268 | struct devlink_dpipe_header *header) |
| 2269 | { |
| 2270 | struct nlattr *fields_attr, *header_attr; |
| 2271 | int err; |
| 2272 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2273 | header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER); |
Wei Yongjun | cb6bf9c | 2017-04-11 16:02:02 +0000 | [diff] [blame] | 2274 | if (!header_attr) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2275 | return -EMSGSIZE; |
| 2276 | |
| 2277 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) || |
| 2278 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 2279 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 2280 | goto nla_put_failure; |
| 2281 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2282 | fields_attr = nla_nest_start_noflag(skb, |
| 2283 | DEVLINK_ATTR_DPIPE_HEADER_FIELDS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2284 | if (!fields_attr) |
| 2285 | goto nla_put_failure; |
| 2286 | |
| 2287 | err = devlink_dpipe_fields_put(skb, header); |
| 2288 | if (err) { |
| 2289 | nla_nest_cancel(skb, fields_attr); |
| 2290 | goto nla_put_failure; |
| 2291 | } |
| 2292 | nla_nest_end(skb, fields_attr); |
| 2293 | nla_nest_end(skb, header_attr); |
| 2294 | return 0; |
| 2295 | |
| 2296 | nla_put_failure: |
| 2297 | err = -EMSGSIZE; |
| 2298 | nla_nest_cancel(skb, header_attr); |
| 2299 | return err; |
| 2300 | } |
| 2301 | |
| 2302 | static int devlink_dpipe_headers_fill(struct genl_info *info, |
| 2303 | enum devlink_command cmd, int flags, |
| 2304 | struct devlink_dpipe_headers * |
| 2305 | dpipe_headers) |
| 2306 | { |
| 2307 | struct devlink *devlink = info->user_ptr[0]; |
| 2308 | struct nlattr *headers_attr; |
| 2309 | struct sk_buff *skb = NULL; |
| 2310 | struct nlmsghdr *nlh; |
| 2311 | void *hdr; |
| 2312 | int i, j; |
| 2313 | int err; |
| 2314 | |
| 2315 | i = 0; |
| 2316 | start_again: |
| 2317 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2318 | if (err) |
| 2319 | return err; |
| 2320 | |
| 2321 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2322 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2323 | if (!hdr) { |
| 2324 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2325 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2326 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2327 | |
| 2328 | if (devlink_nl_put_handle(skb, devlink)) |
| 2329 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2330 | headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2331 | if (!headers_attr) |
| 2332 | goto nla_put_failure; |
| 2333 | |
| 2334 | j = 0; |
| 2335 | for (; i < dpipe_headers->headers_count; i++) { |
| 2336 | err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]); |
| 2337 | if (err) { |
| 2338 | if (!j) |
| 2339 | goto err_table_put; |
| 2340 | break; |
| 2341 | } |
| 2342 | j++; |
| 2343 | } |
| 2344 | nla_nest_end(skb, headers_attr); |
| 2345 | genlmsg_end(skb, hdr); |
| 2346 | if (i != dpipe_headers->headers_count) |
| 2347 | goto start_again; |
| 2348 | |
| 2349 | send_done: |
| 2350 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2351 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2352 | if (!nlh) { |
| 2353 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2354 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2355 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2356 | goto send_done; |
| 2357 | } |
| 2358 | return genlmsg_reply(skb, info); |
| 2359 | |
| 2360 | nla_put_failure: |
| 2361 | err = -EMSGSIZE; |
| 2362 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2363 | nlmsg_free(skb); |
| 2364 | return err; |
| 2365 | } |
| 2366 | |
| 2367 | static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb, |
| 2368 | struct genl_info *info) |
| 2369 | { |
| 2370 | struct devlink *devlink = info->user_ptr[0]; |
| 2371 | |
| 2372 | if (!devlink->dpipe_headers) |
| 2373 | return -EOPNOTSUPP; |
| 2374 | return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET, |
| 2375 | 0, devlink->dpipe_headers); |
| 2376 | } |
| 2377 | |
| 2378 | static int devlink_dpipe_table_counters_set(struct devlink *devlink, |
| 2379 | const char *table_name, |
| 2380 | bool enable) |
| 2381 | { |
| 2382 | struct devlink_dpipe_table *table; |
| 2383 | |
| 2384 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 2385 | table_name, devlink); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2386 | if (!table) |
| 2387 | return -EINVAL; |
| 2388 | |
| 2389 | if (table->counter_control_extern) |
| 2390 | return -EOPNOTSUPP; |
| 2391 | |
| 2392 | if (!(table->counters_enabled ^ enable)) |
| 2393 | return 0; |
| 2394 | |
| 2395 | table->counters_enabled = enable; |
| 2396 | if (table->table_ops->counters_set_update) |
| 2397 | table->table_ops->counters_set_update(table->priv, enable); |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb, |
| 2402 | struct genl_info *info) |
| 2403 | { |
| 2404 | struct devlink *devlink = info->user_ptr[0]; |
| 2405 | const char *table_name; |
| 2406 | bool counters_enable; |
| 2407 | |
| 2408 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] || |
| 2409 | !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) |
| 2410 | return -EINVAL; |
| 2411 | |
| 2412 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2413 | counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]); |
| 2414 | |
| 2415 | return devlink_dpipe_table_counters_set(devlink, table_name, |
| 2416 | counters_enable); |
| 2417 | } |
| 2418 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2419 | static struct devlink_resource * |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2420 | devlink_resource_find(struct devlink *devlink, |
| 2421 | struct devlink_resource *resource, u64 resource_id) |
| 2422 | { |
| 2423 | struct list_head *resource_list; |
| 2424 | |
| 2425 | if (resource) |
| 2426 | resource_list = &resource->resource_list; |
| 2427 | else |
| 2428 | resource_list = &devlink->resource_list; |
| 2429 | |
| 2430 | list_for_each_entry(resource, resource_list, list) { |
| 2431 | struct devlink_resource *child_resource; |
| 2432 | |
| 2433 | if (resource->id == resource_id) |
| 2434 | return resource; |
| 2435 | |
| 2436 | child_resource = devlink_resource_find(devlink, resource, |
| 2437 | resource_id); |
| 2438 | if (child_resource) |
| 2439 | return child_resource; |
| 2440 | } |
| 2441 | return NULL; |
| 2442 | } |
| 2443 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2444 | static void |
| 2445 | devlink_resource_validate_children(struct devlink_resource *resource) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2446 | { |
| 2447 | struct devlink_resource *child_resource; |
| 2448 | bool size_valid = true; |
| 2449 | u64 parts_size = 0; |
| 2450 | |
| 2451 | if (list_empty(&resource->resource_list)) |
| 2452 | goto out; |
| 2453 | |
| 2454 | list_for_each_entry(child_resource, &resource->resource_list, list) |
| 2455 | parts_size += child_resource->size_new; |
| 2456 | |
Arkadi Sharshevsky | b9d1717 | 2018-02-26 10:59:53 +0100 | [diff] [blame] | 2457 | if (parts_size > resource->size_new) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2458 | size_valid = false; |
| 2459 | out: |
| 2460 | resource->size_valid = size_valid; |
| 2461 | } |
| 2462 | |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2463 | static int |
| 2464 | devlink_resource_validate_size(struct devlink_resource *resource, u64 size, |
| 2465 | struct netlink_ext_ack *extack) |
| 2466 | { |
| 2467 | u64 reminder; |
| 2468 | int err = 0; |
| 2469 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2470 | if (size > resource->size_params.size_max) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2471 | NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum"); |
| 2472 | err = -EINVAL; |
| 2473 | } |
| 2474 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2475 | if (size < resource->size_params.size_min) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2476 | NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum"); |
| 2477 | err = -EINVAL; |
| 2478 | } |
| 2479 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2480 | div64_u64_rem(size, resource->size_params.size_granularity, &reminder); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2481 | if (reminder) { |
| 2482 | NL_SET_ERR_MSG_MOD(extack, "Wrong granularity"); |
| 2483 | err = -EINVAL; |
| 2484 | } |
| 2485 | |
| 2486 | return err; |
| 2487 | } |
| 2488 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2489 | static int devlink_nl_cmd_resource_set(struct sk_buff *skb, |
| 2490 | struct genl_info *info) |
| 2491 | { |
| 2492 | struct devlink *devlink = info->user_ptr[0]; |
| 2493 | struct devlink_resource *resource; |
| 2494 | u64 resource_id; |
| 2495 | u64 size; |
| 2496 | int err; |
| 2497 | |
| 2498 | if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] || |
| 2499 | !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]) |
| 2500 | return -EINVAL; |
| 2501 | resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]); |
| 2502 | |
| 2503 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 2504 | if (!resource) |
| 2505 | return -EINVAL; |
| 2506 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2507 | size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2508 | err = devlink_resource_validate_size(resource, size, info->extack); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2509 | if (err) |
| 2510 | return err; |
| 2511 | |
| 2512 | resource->size_new = size; |
| 2513 | devlink_resource_validate_children(resource); |
| 2514 | if (resource->parent) |
| 2515 | devlink_resource_validate_children(resource->parent); |
| 2516 | return 0; |
| 2517 | } |
| 2518 | |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2519 | static int |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2520 | devlink_resource_size_params_put(struct devlink_resource *resource, |
| 2521 | struct sk_buff *skb) |
| 2522 | { |
| 2523 | struct devlink_resource_size_params *size_params; |
| 2524 | |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 2525 | size_params = &resource->size_params; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2526 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN, |
| 2527 | size_params->size_granularity, DEVLINK_ATTR_PAD) || |
| 2528 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX, |
| 2529 | size_params->size_max, DEVLINK_ATTR_PAD) || |
| 2530 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN, |
| 2531 | size_params->size_min, DEVLINK_ATTR_PAD) || |
| 2532 | nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit)) |
| 2533 | return -EMSGSIZE; |
| 2534 | return 0; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2535 | } |
| 2536 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2537 | static int devlink_resource_occ_put(struct devlink_resource *resource, |
| 2538 | struct sk_buff *skb) |
| 2539 | { |
| 2540 | if (!resource->occ_get) |
| 2541 | return 0; |
| 2542 | return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC, |
| 2543 | resource->occ_get(resource->occ_get_priv), |
| 2544 | DEVLINK_ATTR_PAD); |
| 2545 | } |
| 2546 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2547 | static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb, |
| 2548 | struct devlink_resource *resource) |
| 2549 | { |
| 2550 | struct devlink_resource *child_resource; |
| 2551 | struct nlattr *child_resource_attr; |
| 2552 | struct nlattr *resource_attr; |
| 2553 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2554 | resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2555 | if (!resource_attr) |
| 2556 | return -EMSGSIZE; |
| 2557 | |
| 2558 | if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) || |
| 2559 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size, |
| 2560 | DEVLINK_ATTR_PAD) || |
| 2561 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id, |
| 2562 | DEVLINK_ATTR_PAD)) |
| 2563 | goto nla_put_failure; |
| 2564 | if (resource->size != resource->size_new) |
| 2565 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW, |
| 2566 | resource->size_new, DEVLINK_ATTR_PAD); |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2567 | if (devlink_resource_occ_put(resource, skb)) |
| 2568 | goto nla_put_failure; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2569 | if (devlink_resource_size_params_put(resource, skb)) |
| 2570 | goto nla_put_failure; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2571 | if (list_empty(&resource->resource_list)) |
| 2572 | goto out; |
| 2573 | |
| 2574 | if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID, |
| 2575 | resource->size_valid)) |
| 2576 | goto nla_put_failure; |
| 2577 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2578 | child_resource_attr = nla_nest_start_noflag(skb, |
| 2579 | DEVLINK_ATTR_RESOURCE_LIST); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2580 | if (!child_resource_attr) |
| 2581 | goto nla_put_failure; |
| 2582 | |
| 2583 | list_for_each_entry(child_resource, &resource->resource_list, list) { |
| 2584 | if (devlink_resource_put(devlink, skb, child_resource)) |
| 2585 | goto resource_put_failure; |
| 2586 | } |
| 2587 | |
| 2588 | nla_nest_end(skb, child_resource_attr); |
| 2589 | out: |
| 2590 | nla_nest_end(skb, resource_attr); |
| 2591 | return 0; |
| 2592 | |
| 2593 | resource_put_failure: |
| 2594 | nla_nest_cancel(skb, child_resource_attr); |
| 2595 | nla_put_failure: |
| 2596 | nla_nest_cancel(skb, resource_attr); |
| 2597 | return -EMSGSIZE; |
| 2598 | } |
| 2599 | |
| 2600 | static int devlink_resource_fill(struct genl_info *info, |
| 2601 | enum devlink_command cmd, int flags) |
| 2602 | { |
| 2603 | struct devlink *devlink = info->user_ptr[0]; |
| 2604 | struct devlink_resource *resource; |
| 2605 | struct nlattr *resources_attr; |
| 2606 | struct sk_buff *skb = NULL; |
| 2607 | struct nlmsghdr *nlh; |
| 2608 | bool incomplete; |
| 2609 | void *hdr; |
| 2610 | int i; |
| 2611 | int err; |
| 2612 | |
| 2613 | resource = list_first_entry(&devlink->resource_list, |
| 2614 | struct devlink_resource, list); |
| 2615 | start_again: |
| 2616 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2617 | if (err) |
| 2618 | return err; |
| 2619 | |
| 2620 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2621 | &devlink_nl_family, NLM_F_MULTI, cmd); |
| 2622 | if (!hdr) { |
| 2623 | nlmsg_free(skb); |
| 2624 | return -EMSGSIZE; |
| 2625 | } |
| 2626 | |
| 2627 | if (devlink_nl_put_handle(skb, devlink)) |
| 2628 | goto nla_put_failure; |
| 2629 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2630 | resources_attr = nla_nest_start_noflag(skb, |
| 2631 | DEVLINK_ATTR_RESOURCE_LIST); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2632 | if (!resources_attr) |
| 2633 | goto nla_put_failure; |
| 2634 | |
| 2635 | incomplete = false; |
| 2636 | i = 0; |
| 2637 | list_for_each_entry_from(resource, &devlink->resource_list, list) { |
| 2638 | err = devlink_resource_put(devlink, skb, resource); |
| 2639 | if (err) { |
| 2640 | if (!i) |
| 2641 | goto err_resource_put; |
| 2642 | incomplete = true; |
| 2643 | break; |
| 2644 | } |
| 2645 | i++; |
| 2646 | } |
| 2647 | nla_nest_end(skb, resources_attr); |
| 2648 | genlmsg_end(skb, hdr); |
| 2649 | if (incomplete) |
| 2650 | goto start_again; |
| 2651 | send_done: |
| 2652 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2653 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2654 | if (!nlh) { |
| 2655 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2656 | if (err) |
Dan Carpenter | 83fe9a9 | 2018-09-21 11:07:55 +0300 | [diff] [blame] | 2657 | return err; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2658 | goto send_done; |
| 2659 | } |
| 2660 | return genlmsg_reply(skb, info); |
| 2661 | |
| 2662 | nla_put_failure: |
| 2663 | err = -EMSGSIZE; |
| 2664 | err_resource_put: |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2665 | nlmsg_free(skb); |
| 2666 | return err; |
| 2667 | } |
| 2668 | |
| 2669 | static int devlink_nl_cmd_resource_dump(struct sk_buff *skb, |
| 2670 | struct genl_info *info) |
| 2671 | { |
| 2672 | struct devlink *devlink = info->user_ptr[0]; |
| 2673 | |
| 2674 | if (list_empty(&devlink->resource_list)) |
| 2675 | return -EOPNOTSUPP; |
| 2676 | |
| 2677 | return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0); |
| 2678 | } |
| 2679 | |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2680 | static int |
| 2681 | devlink_resources_validate(struct devlink *devlink, |
| 2682 | struct devlink_resource *resource, |
| 2683 | struct genl_info *info) |
| 2684 | { |
| 2685 | struct list_head *resource_list; |
| 2686 | int err = 0; |
| 2687 | |
| 2688 | if (resource) |
| 2689 | resource_list = &resource->resource_list; |
| 2690 | else |
| 2691 | resource_list = &devlink->resource_list; |
| 2692 | |
| 2693 | list_for_each_entry(resource, resource_list, list) { |
| 2694 | if (!resource->size_valid) |
| 2695 | return -EINVAL; |
| 2696 | err = devlink_resources_validate(devlink, resource, info); |
| 2697 | if (err) |
| 2698 | return err; |
| 2699 | } |
| 2700 | return err; |
| 2701 | } |
| 2702 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2703 | static struct net *devlink_netns_get(struct sk_buff *skb, |
| 2704 | struct genl_info *info) |
| 2705 | { |
| 2706 | struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID]; |
| 2707 | struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD]; |
| 2708 | struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID]; |
| 2709 | struct net *net; |
| 2710 | |
| 2711 | if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) { |
Jiri Pirko | 054eae8 | 2020-03-28 19:25:29 +0100 | [diff] [blame] | 2712 | NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified"); |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2713 | return ERR_PTR(-EINVAL); |
| 2714 | } |
| 2715 | |
| 2716 | if (netns_pid_attr) { |
| 2717 | net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr)); |
| 2718 | } else if (netns_fd_attr) { |
| 2719 | net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr)); |
| 2720 | } else if (netns_id_attr) { |
| 2721 | net = get_net_ns_by_id(sock_net(skb->sk), |
| 2722 | nla_get_u32(netns_id_attr)); |
| 2723 | if (!net) |
| 2724 | net = ERR_PTR(-EINVAL); |
| 2725 | } else { |
| 2726 | WARN_ON(1); |
| 2727 | net = ERR_PTR(-EINVAL); |
| 2728 | } |
| 2729 | if (IS_ERR(net)) { |
Jiri Pirko | 054eae8 | 2020-03-28 19:25:29 +0100 | [diff] [blame] | 2730 | NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace"); |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2731 | return ERR_PTR(-EINVAL); |
| 2732 | } |
| 2733 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
| 2734 | put_net(net); |
| 2735 | return ERR_PTR(-EPERM); |
| 2736 | } |
| 2737 | return net; |
| 2738 | } |
| 2739 | |
| 2740 | static void devlink_param_notify(struct devlink *devlink, |
| 2741 | unsigned int port_index, |
| 2742 | struct devlink_param_item *param_item, |
| 2743 | enum devlink_command cmd); |
| 2744 | |
| 2745 | static void devlink_reload_netns_change(struct devlink *devlink, |
| 2746 | struct net *dest_net) |
| 2747 | { |
| 2748 | struct devlink_param_item *param_item; |
| 2749 | |
| 2750 | /* Userspace needs to be notified about devlink objects |
| 2751 | * removed from original and entering new network namespace. |
| 2752 | * The rest of the devlink objects are re-created during |
| 2753 | * reload process so the notifications are generated separatelly. |
| 2754 | */ |
| 2755 | |
| 2756 | list_for_each_entry(param_item, &devlink->param_list, list) |
| 2757 | devlink_param_notify(devlink, 0, param_item, |
| 2758 | DEVLINK_CMD_PARAM_DEL); |
| 2759 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 2760 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 2761 | __devlink_net_set(devlink, dest_net); |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2762 | |
| 2763 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 2764 | list_for_each_entry(param_item, &devlink->param_list, list) |
| 2765 | devlink_param_notify(devlink, 0, param_item, |
| 2766 | DEVLINK_CMD_PARAM_NEW); |
| 2767 | } |
| 2768 | |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 2769 | static bool devlink_reload_supported(struct devlink *devlink) |
| 2770 | { |
| 2771 | return devlink->ops->reload_down && devlink->ops->reload_up; |
| 2772 | } |
| 2773 | |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 2774 | static void devlink_reload_failed_set(struct devlink *devlink, |
| 2775 | bool reload_failed) |
| 2776 | { |
| 2777 | if (devlink->reload_failed == reload_failed) |
| 2778 | return; |
| 2779 | devlink->reload_failed = reload_failed; |
| 2780 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 2781 | } |
| 2782 | |
| 2783 | bool devlink_is_reload_failed(const struct devlink *devlink) |
| 2784 | { |
| 2785 | return devlink->reload_failed; |
| 2786 | } |
| 2787 | EXPORT_SYMBOL_GPL(devlink_is_reload_failed); |
| 2788 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2789 | static int devlink_reload(struct devlink *devlink, struct net *dest_net, |
| 2790 | struct netlink_ext_ack *extack) |
| 2791 | { |
| 2792 | int err; |
| 2793 | |
Jiri Pirko | a0c7634 | 2019-11-08 21:42:43 +0100 | [diff] [blame] | 2794 | if (!devlink->reload_enabled) |
| 2795 | return -EOPNOTSUPP; |
| 2796 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2797 | err = devlink->ops->reload_down(devlink, !!dest_net, extack); |
| 2798 | if (err) |
| 2799 | return err; |
| 2800 | |
| 2801 | if (dest_net && !net_eq(dest_net, devlink_net(devlink))) |
| 2802 | devlink_reload_netns_change(devlink, dest_net); |
| 2803 | |
| 2804 | err = devlink->ops->reload_up(devlink, extack); |
| 2805 | devlink_reload_failed_set(devlink, !!err); |
| 2806 | return err; |
| 2807 | } |
| 2808 | |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2809 | static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info) |
| 2810 | { |
| 2811 | struct devlink *devlink = info->user_ptr[0]; |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2812 | struct net *dest_net = NULL; |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2813 | int err; |
| 2814 | |
Jiri Pirko | 5a508a2 | 2019-11-09 11:29:46 +0100 | [diff] [blame] | 2815 | if (!devlink_reload_supported(devlink) || !devlink->reload_enabled) |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2816 | return -EOPNOTSUPP; |
| 2817 | |
| 2818 | err = devlink_resources_validate(devlink, NULL, info); |
| 2819 | if (err) { |
| 2820 | NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed"); |
| 2821 | return err; |
| 2822 | } |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2823 | |
| 2824 | if (info->attrs[DEVLINK_ATTR_NETNS_PID] || |
| 2825 | info->attrs[DEVLINK_ATTR_NETNS_FD] || |
| 2826 | info->attrs[DEVLINK_ATTR_NETNS_ID]) { |
| 2827 | dest_net = devlink_netns_get(skb, info); |
| 2828 | if (IS_ERR(dest_net)) |
| 2829 | return PTR_ERR(dest_net); |
| 2830 | } |
| 2831 | |
| 2832 | err = devlink_reload(devlink, dest_net, info->extack); |
| 2833 | |
| 2834 | if (dest_net) |
| 2835 | put_net(dest_net); |
| 2836 | |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 2837 | return err; |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2838 | } |
| 2839 | |
Jiri Pirko | 191ed20 | 2019-06-04 15:40:40 +0200 | [diff] [blame] | 2840 | static int devlink_nl_flash_update_fill(struct sk_buff *msg, |
| 2841 | struct devlink *devlink, |
| 2842 | enum devlink_command cmd, |
| 2843 | const char *status_msg, |
| 2844 | const char *component, |
| 2845 | unsigned long done, unsigned long total) |
| 2846 | { |
| 2847 | void *hdr; |
| 2848 | |
| 2849 | hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd); |
| 2850 | if (!hdr) |
| 2851 | return -EMSGSIZE; |
| 2852 | |
| 2853 | if (devlink_nl_put_handle(msg, devlink)) |
| 2854 | goto nla_put_failure; |
| 2855 | |
| 2856 | if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS) |
| 2857 | goto out; |
| 2858 | |
| 2859 | if (status_msg && |
| 2860 | nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG, |
| 2861 | status_msg)) |
| 2862 | goto nla_put_failure; |
| 2863 | if (component && |
| 2864 | nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT, |
| 2865 | component)) |
| 2866 | goto nla_put_failure; |
| 2867 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE, |
| 2868 | done, DEVLINK_ATTR_PAD)) |
| 2869 | goto nla_put_failure; |
| 2870 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL, |
| 2871 | total, DEVLINK_ATTR_PAD)) |
| 2872 | goto nla_put_failure; |
| 2873 | |
| 2874 | out: |
| 2875 | genlmsg_end(msg, hdr); |
| 2876 | return 0; |
| 2877 | |
| 2878 | nla_put_failure: |
| 2879 | genlmsg_cancel(msg, hdr); |
| 2880 | return -EMSGSIZE; |
| 2881 | } |
| 2882 | |
| 2883 | static void __devlink_flash_update_notify(struct devlink *devlink, |
| 2884 | enum devlink_command cmd, |
| 2885 | const char *status_msg, |
| 2886 | const char *component, |
| 2887 | unsigned long done, |
| 2888 | unsigned long total) |
| 2889 | { |
| 2890 | struct sk_buff *msg; |
| 2891 | int err; |
| 2892 | |
| 2893 | WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE && |
| 2894 | cmd != DEVLINK_CMD_FLASH_UPDATE_END && |
| 2895 | cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); |
| 2896 | |
| 2897 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 2898 | if (!msg) |
| 2899 | return; |
| 2900 | |
| 2901 | err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg, |
| 2902 | component, done, total); |
| 2903 | if (err) |
| 2904 | goto out_free_msg; |
| 2905 | |
| 2906 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 2907 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 2908 | return; |
| 2909 | |
| 2910 | out_free_msg: |
| 2911 | nlmsg_free(msg); |
| 2912 | } |
| 2913 | |
| 2914 | void devlink_flash_update_begin_notify(struct devlink *devlink) |
| 2915 | { |
| 2916 | __devlink_flash_update_notify(devlink, |
| 2917 | DEVLINK_CMD_FLASH_UPDATE, |
| 2918 | NULL, NULL, 0, 0); |
| 2919 | } |
| 2920 | EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify); |
| 2921 | |
| 2922 | void devlink_flash_update_end_notify(struct devlink *devlink) |
| 2923 | { |
| 2924 | __devlink_flash_update_notify(devlink, |
| 2925 | DEVLINK_CMD_FLASH_UPDATE_END, |
| 2926 | NULL, NULL, 0, 0); |
| 2927 | } |
| 2928 | EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify); |
| 2929 | |
| 2930 | void devlink_flash_update_status_notify(struct devlink *devlink, |
| 2931 | const char *status_msg, |
| 2932 | const char *component, |
| 2933 | unsigned long done, |
| 2934 | unsigned long total) |
| 2935 | { |
| 2936 | __devlink_flash_update_notify(devlink, |
| 2937 | DEVLINK_CMD_FLASH_UPDATE_STATUS, |
| 2938 | status_msg, component, done, total); |
| 2939 | } |
| 2940 | EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify); |
| 2941 | |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 2942 | static int devlink_nl_cmd_flash_update(struct sk_buff *skb, |
| 2943 | struct genl_info *info) |
| 2944 | { |
| 2945 | struct devlink *devlink = info->user_ptr[0]; |
| 2946 | const char *file_name, *component; |
| 2947 | struct nlattr *nla_component; |
| 2948 | |
| 2949 | if (!devlink->ops->flash_update) |
| 2950 | return -EOPNOTSUPP; |
| 2951 | |
| 2952 | if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]) |
| 2953 | return -EINVAL; |
| 2954 | file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]); |
| 2955 | |
| 2956 | nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]; |
| 2957 | component = nla_component ? nla_data(nla_component) : NULL; |
| 2958 | |
| 2959 | return devlink->ops->flash_update(devlink, file_name, component, |
| 2960 | info->extack); |
| 2961 | } |
| 2962 | |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame] | 2963 | static const struct devlink_param devlink_param_generic[] = { |
| 2964 | { |
| 2965 | .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET, |
| 2966 | .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME, |
| 2967 | .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE, |
| 2968 | }, |
| 2969 | { |
| 2970 | .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS, |
| 2971 | .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME, |
| 2972 | .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE, |
| 2973 | }, |
Vasundhara Volam | f567bcd | 2018-07-04 14:30:36 +0300 | [diff] [blame] | 2974 | { |
| 2975 | .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, |
| 2976 | .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME, |
| 2977 | .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE, |
| 2978 | }, |
Alex Vesker | f6a69885 | 2018-07-12 15:13:17 +0300 | [diff] [blame] | 2979 | { |
| 2980 | .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT, |
| 2981 | .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME, |
| 2982 | .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE, |
| 2983 | }, |
Vasundhara Volam | e3b5106 | 2018-10-04 11:13:44 +0530 | [diff] [blame] | 2984 | { |
| 2985 | .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI, |
| 2986 | .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME, |
| 2987 | .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE, |
| 2988 | }, |
Vasundhara Volam | f61cba4 | 2018-10-04 11:13:45 +0530 | [diff] [blame] | 2989 | { |
| 2990 | .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX, |
| 2991 | .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME, |
| 2992 | .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE, |
| 2993 | }, |
Vasundhara Volam | 1651178 | 2018-10-04 11:13:46 +0530 | [diff] [blame] | 2994 | { |
| 2995 | .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN, |
| 2996 | .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME, |
| 2997 | .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE, |
| 2998 | }, |
Shalom Toledo | 846e980 | 2018-12-03 07:58:59 +0000 | [diff] [blame] | 2999 | { |
| 3000 | .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY, |
| 3001 | .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME, |
| 3002 | .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE, |
| 3003 | }, |
Dirk van der Merwe | 5bbd21d | 2019-09-09 00:54:18 +0100 | [diff] [blame] | 3004 | { |
| 3005 | .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE, |
| 3006 | .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME, |
| 3007 | .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE, |
| 3008 | }, |
Michael Guralnik | 6c7295e | 2019-11-08 23:45:20 +0000 | [diff] [blame] | 3009 | { |
| 3010 | .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE, |
| 3011 | .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME, |
| 3012 | .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE, |
| 3013 | }, |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame] | 3014 | }; |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3015 | |
| 3016 | static int devlink_param_generic_verify(const struct devlink_param *param) |
| 3017 | { |
| 3018 | /* verify it match generic parameter by id and name */ |
| 3019 | if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX) |
| 3020 | return -EINVAL; |
| 3021 | if (strcmp(param->name, devlink_param_generic[param->id].name)) |
| 3022 | return -ENOENT; |
| 3023 | |
| 3024 | WARN_ON(param->type != devlink_param_generic[param->id].type); |
| 3025 | |
| 3026 | return 0; |
| 3027 | } |
| 3028 | |
| 3029 | static int devlink_param_driver_verify(const struct devlink_param *param) |
| 3030 | { |
| 3031 | int i; |
| 3032 | |
| 3033 | if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX) |
| 3034 | return -EINVAL; |
| 3035 | /* verify no such name in generic params */ |
| 3036 | for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++) |
| 3037 | if (!strcmp(param->name, devlink_param_generic[i].name)) |
| 3038 | return -EEXIST; |
| 3039 | |
| 3040 | return 0; |
| 3041 | } |
| 3042 | |
| 3043 | static struct devlink_param_item * |
| 3044 | devlink_param_find_by_name(struct list_head *param_list, |
| 3045 | const char *param_name) |
| 3046 | { |
| 3047 | struct devlink_param_item *param_item; |
| 3048 | |
| 3049 | list_for_each_entry(param_item, param_list, list) |
| 3050 | if (!strcmp(param_item->param->name, param_name)) |
| 3051 | return param_item; |
| 3052 | return NULL; |
| 3053 | } |
| 3054 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 3055 | static struct devlink_param_item * |
| 3056 | devlink_param_find_by_id(struct list_head *param_list, u32 param_id) |
| 3057 | { |
| 3058 | struct devlink_param_item *param_item; |
| 3059 | |
| 3060 | list_for_each_entry(param_item, param_list, list) |
| 3061 | if (param_item->param->id == param_id) |
| 3062 | return param_item; |
| 3063 | return NULL; |
| 3064 | } |
| 3065 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3066 | static bool |
| 3067 | devlink_param_cmode_is_supported(const struct devlink_param *param, |
| 3068 | enum devlink_param_cmode cmode) |
| 3069 | { |
| 3070 | return test_bit(cmode, ¶m->supported_cmodes); |
| 3071 | } |
| 3072 | |
| 3073 | static int devlink_param_get(struct devlink *devlink, |
| 3074 | const struct devlink_param *param, |
| 3075 | struct devlink_param_gset_ctx *ctx) |
| 3076 | { |
| 3077 | if (!param->get) |
| 3078 | return -EOPNOTSUPP; |
| 3079 | return param->get(devlink, param->id, ctx); |
| 3080 | } |
| 3081 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3082 | static int devlink_param_set(struct devlink *devlink, |
| 3083 | const struct devlink_param *param, |
| 3084 | struct devlink_param_gset_ctx *ctx) |
| 3085 | { |
| 3086 | if (!param->set) |
| 3087 | return -EOPNOTSUPP; |
| 3088 | return param->set(devlink, param->id, ctx); |
| 3089 | } |
| 3090 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3091 | static int |
| 3092 | devlink_param_type_to_nla_type(enum devlink_param_type param_type) |
| 3093 | { |
| 3094 | switch (param_type) { |
| 3095 | case DEVLINK_PARAM_TYPE_U8: |
| 3096 | return NLA_U8; |
| 3097 | case DEVLINK_PARAM_TYPE_U16: |
| 3098 | return NLA_U16; |
| 3099 | case DEVLINK_PARAM_TYPE_U32: |
| 3100 | return NLA_U32; |
| 3101 | case DEVLINK_PARAM_TYPE_STRING: |
| 3102 | return NLA_STRING; |
| 3103 | case DEVLINK_PARAM_TYPE_BOOL: |
| 3104 | return NLA_FLAG; |
| 3105 | default: |
| 3106 | return -EINVAL; |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | static int |
| 3111 | devlink_nl_param_value_fill_one(struct sk_buff *msg, |
| 3112 | enum devlink_param_type type, |
| 3113 | enum devlink_param_cmode cmode, |
| 3114 | union devlink_param_value val) |
| 3115 | { |
| 3116 | struct nlattr *param_value_attr; |
| 3117 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3118 | param_value_attr = nla_nest_start_noflag(msg, |
| 3119 | DEVLINK_ATTR_PARAM_VALUE); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3120 | if (!param_value_attr) |
| 3121 | goto nla_put_failure; |
| 3122 | |
| 3123 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode)) |
| 3124 | goto value_nest_cancel; |
| 3125 | |
| 3126 | switch (type) { |
| 3127 | case DEVLINK_PARAM_TYPE_U8: |
| 3128 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8)) |
| 3129 | goto value_nest_cancel; |
| 3130 | break; |
| 3131 | case DEVLINK_PARAM_TYPE_U16: |
| 3132 | if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16)) |
| 3133 | goto value_nest_cancel; |
| 3134 | break; |
| 3135 | case DEVLINK_PARAM_TYPE_U32: |
| 3136 | if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32)) |
| 3137 | goto value_nest_cancel; |
| 3138 | break; |
| 3139 | case DEVLINK_PARAM_TYPE_STRING: |
| 3140 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, |
| 3141 | val.vstr)) |
| 3142 | goto value_nest_cancel; |
| 3143 | break; |
| 3144 | case DEVLINK_PARAM_TYPE_BOOL: |
| 3145 | if (val.vbool && |
| 3146 | nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA)) |
| 3147 | goto value_nest_cancel; |
| 3148 | break; |
| 3149 | } |
| 3150 | |
| 3151 | nla_nest_end(msg, param_value_attr); |
| 3152 | return 0; |
| 3153 | |
| 3154 | value_nest_cancel: |
| 3155 | nla_nest_cancel(msg, param_value_attr); |
| 3156 | nla_put_failure: |
| 3157 | return -EMSGSIZE; |
| 3158 | } |
| 3159 | |
| 3160 | 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] | 3161 | unsigned int port_index, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3162 | struct devlink_param_item *param_item, |
| 3163 | enum devlink_command cmd, |
| 3164 | u32 portid, u32 seq, int flags) |
| 3165 | { |
| 3166 | union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1]; |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3167 | bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {}; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3168 | const struct devlink_param *param = param_item->param; |
| 3169 | struct devlink_param_gset_ctx ctx; |
| 3170 | struct nlattr *param_values_list; |
| 3171 | struct nlattr *param_attr; |
| 3172 | int nla_type; |
| 3173 | void *hdr; |
| 3174 | int err; |
| 3175 | int i; |
| 3176 | |
| 3177 | /* Get value from driver part to driverinit configuration mode */ |
| 3178 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
| 3179 | if (!devlink_param_cmode_is_supported(param, i)) |
| 3180 | continue; |
| 3181 | if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
| 3182 | if (!param_item->driverinit_value_valid) |
| 3183 | return -EOPNOTSUPP; |
| 3184 | param_value[i] = param_item->driverinit_value; |
| 3185 | } else { |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3186 | if (!param_item->published) |
| 3187 | continue; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3188 | ctx.cmode = i; |
| 3189 | err = devlink_param_get(devlink, param, &ctx); |
| 3190 | if (err) |
| 3191 | return err; |
| 3192 | param_value[i] = ctx.val; |
| 3193 | } |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3194 | param_value_set[i] = true; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3198 | if (!hdr) |
| 3199 | return -EMSGSIZE; |
| 3200 | |
| 3201 | if (devlink_nl_put_handle(msg, devlink)) |
| 3202 | goto genlmsg_cancel; |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3203 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3204 | if (cmd == DEVLINK_CMD_PORT_PARAM_GET || |
| 3205 | cmd == DEVLINK_CMD_PORT_PARAM_NEW || |
| 3206 | cmd == DEVLINK_CMD_PORT_PARAM_DEL) |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3207 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index)) |
| 3208 | goto genlmsg_cancel; |
| 3209 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3210 | param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3211 | if (!param_attr) |
| 3212 | goto genlmsg_cancel; |
| 3213 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name)) |
| 3214 | goto param_nest_cancel; |
| 3215 | if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC)) |
| 3216 | goto param_nest_cancel; |
| 3217 | |
| 3218 | nla_type = devlink_param_type_to_nla_type(param->type); |
| 3219 | if (nla_type < 0) |
| 3220 | goto param_nest_cancel; |
| 3221 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type)) |
| 3222 | goto param_nest_cancel; |
| 3223 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3224 | param_values_list = nla_nest_start_noflag(msg, |
| 3225 | DEVLINK_ATTR_PARAM_VALUES_LIST); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3226 | if (!param_values_list) |
| 3227 | goto param_nest_cancel; |
| 3228 | |
| 3229 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3230 | if (!param_value_set[i]) |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3231 | continue; |
| 3232 | err = devlink_nl_param_value_fill_one(msg, param->type, |
| 3233 | i, param_value[i]); |
| 3234 | if (err) |
| 3235 | goto values_list_nest_cancel; |
| 3236 | } |
| 3237 | |
| 3238 | nla_nest_end(msg, param_values_list); |
| 3239 | nla_nest_end(msg, param_attr); |
| 3240 | genlmsg_end(msg, hdr); |
| 3241 | return 0; |
| 3242 | |
| 3243 | values_list_nest_cancel: |
| 3244 | nla_nest_end(msg, param_values_list); |
| 3245 | param_nest_cancel: |
| 3246 | nla_nest_cancel(msg, param_attr); |
| 3247 | genlmsg_cancel: |
| 3248 | genlmsg_cancel(msg, hdr); |
| 3249 | return -EMSGSIZE; |
| 3250 | } |
| 3251 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3252 | static void devlink_param_notify(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3253 | unsigned int port_index, |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3254 | struct devlink_param_item *param_item, |
| 3255 | enum devlink_command cmd) |
| 3256 | { |
| 3257 | struct sk_buff *msg; |
| 3258 | int err; |
| 3259 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3260 | WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL && |
| 3261 | cmd != DEVLINK_CMD_PORT_PARAM_NEW && |
| 3262 | cmd != DEVLINK_CMD_PORT_PARAM_DEL); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3263 | |
| 3264 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3265 | if (!msg) |
| 3266 | return; |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3267 | err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd, |
| 3268 | 0, 0, 0); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3269 | if (err) { |
| 3270 | nlmsg_free(msg); |
| 3271 | return; |
| 3272 | } |
| 3273 | |
| 3274 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3275 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 3276 | } |
| 3277 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3278 | static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg, |
| 3279 | struct netlink_callback *cb) |
| 3280 | { |
| 3281 | struct devlink_param_item *param_item; |
| 3282 | struct devlink *devlink; |
| 3283 | int start = cb->args[0]; |
| 3284 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3285 | int err = 0; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3286 | |
| 3287 | mutex_lock(&devlink_mutex); |
| 3288 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3289 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3290 | continue; |
| 3291 | mutex_lock(&devlink->lock); |
| 3292 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 3293 | if (idx < start) { |
| 3294 | idx++; |
| 3295 | continue; |
| 3296 | } |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3297 | err = devlink_nl_param_fill(msg, devlink, 0, param_item, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3298 | DEVLINK_CMD_PARAM_GET, |
| 3299 | NETLINK_CB(cb->skb).portid, |
| 3300 | cb->nlh->nlmsg_seq, |
| 3301 | NLM_F_MULTI); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 3302 | if (err && err != -EOPNOTSUPP) { |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3303 | mutex_unlock(&devlink->lock); |
| 3304 | goto out; |
| 3305 | } |
| 3306 | idx++; |
| 3307 | } |
| 3308 | mutex_unlock(&devlink->lock); |
| 3309 | } |
| 3310 | out: |
| 3311 | mutex_unlock(&devlink_mutex); |
| 3312 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3313 | if (err != -EMSGSIZE) |
| 3314 | return err; |
| 3315 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3316 | cb->args[0] = idx; |
| 3317 | return msg->len; |
| 3318 | } |
| 3319 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3320 | static int |
| 3321 | devlink_param_type_get_from_info(struct genl_info *info, |
| 3322 | enum devlink_param_type *param_type) |
| 3323 | { |
| 3324 | if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE]) |
| 3325 | return -EINVAL; |
| 3326 | |
| 3327 | switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) { |
| 3328 | case NLA_U8: |
| 3329 | *param_type = DEVLINK_PARAM_TYPE_U8; |
| 3330 | break; |
| 3331 | case NLA_U16: |
| 3332 | *param_type = DEVLINK_PARAM_TYPE_U16; |
| 3333 | break; |
| 3334 | case NLA_U32: |
| 3335 | *param_type = DEVLINK_PARAM_TYPE_U32; |
| 3336 | break; |
| 3337 | case NLA_STRING: |
| 3338 | *param_type = DEVLINK_PARAM_TYPE_STRING; |
| 3339 | break; |
| 3340 | case NLA_FLAG: |
| 3341 | *param_type = DEVLINK_PARAM_TYPE_BOOL; |
| 3342 | break; |
| 3343 | default: |
| 3344 | return -EINVAL; |
| 3345 | } |
| 3346 | |
| 3347 | return 0; |
| 3348 | } |
| 3349 | |
| 3350 | static int |
| 3351 | devlink_param_value_get_from_info(const struct devlink_param *param, |
| 3352 | struct genl_info *info, |
| 3353 | union devlink_param_value *value) |
| 3354 | { |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3355 | struct nlattr *param_data; |
Moshe Shemesh | f355cfc | 2018-10-10 16:09:25 +0300 | [diff] [blame] | 3356 | int len; |
| 3357 | |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3358 | param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]; |
| 3359 | |
| 3360 | if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3361 | return -EINVAL; |
| 3362 | |
| 3363 | switch (param->type) { |
| 3364 | case DEVLINK_PARAM_TYPE_U8: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3365 | if (nla_len(param_data) != sizeof(u8)) |
| 3366 | return -EINVAL; |
| 3367 | value->vu8 = nla_get_u8(param_data); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3368 | break; |
| 3369 | case DEVLINK_PARAM_TYPE_U16: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3370 | if (nla_len(param_data) != sizeof(u16)) |
| 3371 | return -EINVAL; |
| 3372 | value->vu16 = nla_get_u16(param_data); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3373 | break; |
| 3374 | case DEVLINK_PARAM_TYPE_U32: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3375 | if (nla_len(param_data) != sizeof(u32)) |
| 3376 | return -EINVAL; |
| 3377 | value->vu32 = nla_get_u32(param_data); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3378 | break; |
| 3379 | case DEVLINK_PARAM_TYPE_STRING: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3380 | len = strnlen(nla_data(param_data), nla_len(param_data)); |
| 3381 | if (len == nla_len(param_data) || |
Moshe Shemesh | bde74ad1 | 2018-10-10 16:09:27 +0300 | [diff] [blame] | 3382 | len >= __DEVLINK_PARAM_MAX_STRING_VALUE) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3383 | return -EINVAL; |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3384 | strcpy(value->vstr, nla_data(param_data)); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3385 | break; |
| 3386 | case DEVLINK_PARAM_TYPE_BOOL: |
Jakub Kicinski | 8750939 | 2020-03-02 21:05:11 -0800 | [diff] [blame] | 3387 | if (param_data && nla_len(param_data)) |
| 3388 | return -EINVAL; |
| 3389 | value->vbool = nla_get_flag(param_data); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3390 | break; |
| 3391 | } |
| 3392 | return 0; |
| 3393 | } |
| 3394 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3395 | static struct devlink_param_item * |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3396 | devlink_param_get_from_info(struct list_head *param_list, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3397 | struct genl_info *info) |
| 3398 | { |
| 3399 | char *param_name; |
| 3400 | |
| 3401 | if (!info->attrs[DEVLINK_ATTR_PARAM_NAME]) |
| 3402 | return NULL; |
| 3403 | |
| 3404 | param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]); |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3405 | return devlink_param_find_by_name(param_list, param_name); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb, |
| 3409 | struct genl_info *info) |
| 3410 | { |
| 3411 | struct devlink *devlink = info->user_ptr[0]; |
| 3412 | struct devlink_param_item *param_item; |
| 3413 | struct sk_buff *msg; |
| 3414 | int err; |
| 3415 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3416 | param_item = devlink_param_get_from_info(&devlink->param_list, info); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3417 | if (!param_item) |
| 3418 | return -EINVAL; |
| 3419 | |
| 3420 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3421 | if (!msg) |
| 3422 | return -ENOMEM; |
| 3423 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3424 | err = devlink_nl_param_fill(msg, devlink, 0, param_item, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3425 | DEVLINK_CMD_PARAM_GET, |
| 3426 | info->snd_portid, info->snd_seq, 0); |
| 3427 | if (err) { |
| 3428 | nlmsg_free(msg); |
| 3429 | return err; |
| 3430 | } |
| 3431 | |
| 3432 | return genlmsg_reply(msg, info); |
| 3433 | } |
| 3434 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3435 | static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3436 | unsigned int port_index, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3437 | struct list_head *param_list, |
| 3438 | struct genl_info *info, |
| 3439 | enum devlink_command cmd) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3440 | { |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3441 | enum devlink_param_type param_type; |
| 3442 | struct devlink_param_gset_ctx ctx; |
| 3443 | enum devlink_param_cmode cmode; |
| 3444 | struct devlink_param_item *param_item; |
| 3445 | const struct devlink_param *param; |
| 3446 | union devlink_param_value value; |
| 3447 | int err = 0; |
| 3448 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3449 | param_item = devlink_param_get_from_info(param_list, info); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3450 | if (!param_item) |
| 3451 | return -EINVAL; |
| 3452 | param = param_item->param; |
| 3453 | err = devlink_param_type_get_from_info(info, ¶m_type); |
| 3454 | if (err) |
| 3455 | return err; |
| 3456 | if (param_type != param->type) |
| 3457 | return -EINVAL; |
| 3458 | err = devlink_param_value_get_from_info(param, info, &value); |
| 3459 | if (err) |
| 3460 | return err; |
| 3461 | if (param->validate) { |
| 3462 | err = param->validate(devlink, param->id, value, info->extack); |
| 3463 | if (err) |
| 3464 | return err; |
| 3465 | } |
| 3466 | |
| 3467 | if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]) |
| 3468 | return -EINVAL; |
| 3469 | cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]); |
| 3470 | if (!devlink_param_cmode_is_supported(param, cmode)) |
| 3471 | return -EOPNOTSUPP; |
| 3472 | |
| 3473 | if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
Moshe Shemesh | 1276534 | 2018-10-10 16:09:26 +0300 | [diff] [blame] | 3474 | if (param->type == DEVLINK_PARAM_TYPE_STRING) |
| 3475 | strcpy(param_item->driverinit_value.vstr, value.vstr); |
| 3476 | else |
| 3477 | param_item->driverinit_value = value; |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3478 | param_item->driverinit_value_valid = true; |
| 3479 | } else { |
| 3480 | if (!param->set) |
| 3481 | return -EOPNOTSUPP; |
| 3482 | ctx.val = value; |
| 3483 | ctx.cmode = cmode; |
| 3484 | err = devlink_param_set(devlink, param, &ctx); |
| 3485 | if (err) |
| 3486 | return err; |
| 3487 | } |
| 3488 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3489 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3490 | return 0; |
| 3491 | } |
| 3492 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3493 | static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb, |
| 3494 | struct genl_info *info) |
| 3495 | { |
| 3496 | struct devlink *devlink = info->user_ptr[0]; |
| 3497 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3498 | return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3499 | info, DEVLINK_CMD_PARAM_NEW); |
| 3500 | } |
| 3501 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3502 | static int devlink_param_register_one(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3503 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3504 | struct list_head *param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3505 | const struct devlink_param *param, |
| 3506 | enum devlink_command cmd) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3507 | { |
| 3508 | struct devlink_param_item *param_item; |
| 3509 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3510 | if (devlink_param_find_by_name(param_list, param->name)) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3511 | return -EEXIST; |
| 3512 | |
| 3513 | if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 3514 | WARN_ON(param->get || param->set); |
| 3515 | else |
| 3516 | WARN_ON(!param->get || !param->set); |
| 3517 | |
| 3518 | param_item = kzalloc(sizeof(*param_item), GFP_KERNEL); |
| 3519 | if (!param_item) |
| 3520 | return -ENOMEM; |
| 3521 | param_item->param = param; |
| 3522 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3523 | list_add_tail(¶m_item->list, param_list); |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3524 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3525 | return 0; |
| 3526 | } |
| 3527 | |
| 3528 | static void devlink_param_unregister_one(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3529 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3530 | struct list_head *param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3531 | const struct devlink_param *param, |
| 3532 | enum devlink_command cmd) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3533 | { |
| 3534 | struct devlink_param_item *param_item; |
| 3535 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3536 | param_item = devlink_param_find_by_name(param_list, param->name); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3537 | WARN_ON(!param_item); |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3538 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3539 | list_del(¶m_item->list); |
| 3540 | kfree(param_item); |
| 3541 | } |
| 3542 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3543 | static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg, |
| 3544 | struct netlink_callback *cb) |
| 3545 | { |
| 3546 | struct devlink_param_item *param_item; |
| 3547 | struct devlink_port *devlink_port; |
| 3548 | struct devlink *devlink; |
| 3549 | int start = cb->args[0]; |
| 3550 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3551 | int err = 0; |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3552 | |
| 3553 | mutex_lock(&devlink_mutex); |
| 3554 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3555 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3556 | continue; |
| 3557 | mutex_lock(&devlink->lock); |
| 3558 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 3559 | list_for_each_entry(param_item, |
| 3560 | &devlink_port->param_list, list) { |
| 3561 | if (idx < start) { |
| 3562 | idx++; |
| 3563 | continue; |
| 3564 | } |
| 3565 | err = devlink_nl_param_fill(msg, |
| 3566 | devlink_port->devlink, |
| 3567 | devlink_port->index, param_item, |
| 3568 | DEVLINK_CMD_PORT_PARAM_GET, |
| 3569 | NETLINK_CB(cb->skb).portid, |
| 3570 | cb->nlh->nlmsg_seq, |
| 3571 | NLM_F_MULTI); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 3572 | if (err && err != -EOPNOTSUPP) { |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3573 | mutex_unlock(&devlink->lock); |
| 3574 | goto out; |
| 3575 | } |
| 3576 | idx++; |
| 3577 | } |
| 3578 | } |
| 3579 | mutex_unlock(&devlink->lock); |
| 3580 | } |
| 3581 | out: |
| 3582 | mutex_unlock(&devlink_mutex); |
| 3583 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3584 | if (err != -EMSGSIZE) |
| 3585 | return err; |
| 3586 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3587 | cb->args[0] = idx; |
| 3588 | return msg->len; |
| 3589 | } |
| 3590 | |
| 3591 | static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb, |
| 3592 | struct genl_info *info) |
| 3593 | { |
| 3594 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 3595 | struct devlink_param_item *param_item; |
| 3596 | struct sk_buff *msg; |
| 3597 | int err; |
| 3598 | |
| 3599 | param_item = devlink_param_get_from_info(&devlink_port->param_list, |
| 3600 | info); |
| 3601 | if (!param_item) |
| 3602 | return -EINVAL; |
| 3603 | |
| 3604 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3605 | if (!msg) |
| 3606 | return -ENOMEM; |
| 3607 | |
| 3608 | err = devlink_nl_param_fill(msg, devlink_port->devlink, |
| 3609 | devlink_port->index, param_item, |
| 3610 | DEVLINK_CMD_PORT_PARAM_GET, |
| 3611 | info->snd_portid, info->snd_seq, 0); |
| 3612 | if (err) { |
| 3613 | nlmsg_free(msg); |
| 3614 | return err; |
| 3615 | } |
| 3616 | |
| 3617 | return genlmsg_reply(msg, info); |
| 3618 | } |
| 3619 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3620 | static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb, |
| 3621 | struct genl_info *info) |
| 3622 | { |
| 3623 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 3624 | |
| 3625 | return __devlink_nl_cmd_param_set_doit(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3626 | devlink_port->index, |
| 3627 | &devlink_port->param_list, info, |
| 3628 | DEVLINK_CMD_PORT_PARAM_NEW); |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3629 | } |
| 3630 | |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3631 | static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg, |
| 3632 | struct devlink *devlink, |
| 3633 | struct devlink_snapshot *snapshot) |
| 3634 | { |
| 3635 | struct nlattr *snap_attr; |
| 3636 | int err; |
| 3637 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3638 | snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT); |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3639 | if (!snap_attr) |
| 3640 | return -EINVAL; |
| 3641 | |
| 3642 | err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id); |
| 3643 | if (err) |
| 3644 | goto nla_put_failure; |
| 3645 | |
| 3646 | nla_nest_end(msg, snap_attr); |
| 3647 | return 0; |
| 3648 | |
| 3649 | nla_put_failure: |
| 3650 | nla_nest_cancel(msg, snap_attr); |
| 3651 | return err; |
| 3652 | } |
| 3653 | |
| 3654 | static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg, |
| 3655 | struct devlink *devlink, |
| 3656 | struct devlink_region *region) |
| 3657 | { |
| 3658 | struct devlink_snapshot *snapshot; |
| 3659 | struct nlattr *snapshots_attr; |
| 3660 | int err; |
| 3661 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3662 | snapshots_attr = nla_nest_start_noflag(msg, |
| 3663 | DEVLINK_ATTR_REGION_SNAPSHOTS); |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3664 | if (!snapshots_attr) |
| 3665 | return -EINVAL; |
| 3666 | |
| 3667 | list_for_each_entry(snapshot, ®ion->snapshot_list, list) { |
| 3668 | err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot); |
| 3669 | if (err) |
| 3670 | goto nla_put_failure; |
| 3671 | } |
| 3672 | |
| 3673 | nla_nest_end(msg, snapshots_attr); |
| 3674 | return 0; |
| 3675 | |
| 3676 | nla_put_failure: |
| 3677 | nla_nest_cancel(msg, snapshots_attr); |
| 3678 | return err; |
| 3679 | } |
| 3680 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3681 | static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink, |
| 3682 | enum devlink_command cmd, u32 portid, |
| 3683 | u32 seq, int flags, |
| 3684 | struct devlink_region *region) |
| 3685 | { |
| 3686 | void *hdr; |
| 3687 | int err; |
| 3688 | |
| 3689 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3690 | if (!hdr) |
| 3691 | return -EMSGSIZE; |
| 3692 | |
| 3693 | err = devlink_nl_put_handle(msg, devlink); |
| 3694 | if (err) |
| 3695 | goto nla_put_failure; |
| 3696 | |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 3697 | err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name); |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3698 | if (err) |
| 3699 | goto nla_put_failure; |
| 3700 | |
| 3701 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, |
| 3702 | region->size, |
| 3703 | DEVLINK_ATTR_PAD); |
| 3704 | if (err) |
| 3705 | goto nla_put_failure; |
| 3706 | |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3707 | err = devlink_nl_region_snapshots_id_put(msg, devlink, region); |
| 3708 | if (err) |
| 3709 | goto nla_put_failure; |
| 3710 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3711 | genlmsg_end(msg, hdr); |
| 3712 | return 0; |
| 3713 | |
| 3714 | nla_put_failure: |
| 3715 | genlmsg_cancel(msg, hdr); |
| 3716 | return err; |
| 3717 | } |
| 3718 | |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3719 | static struct sk_buff * |
| 3720 | devlink_nl_region_notify_build(struct devlink_region *region, |
| 3721 | struct devlink_snapshot *snapshot, |
| 3722 | enum devlink_command cmd, u32 portid, u32 seq) |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3723 | { |
| 3724 | struct devlink *devlink = region->devlink; |
| 3725 | struct sk_buff *msg; |
| 3726 | void *hdr; |
| 3727 | int err; |
| 3728 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3729 | |
| 3730 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3731 | if (!msg) |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3732 | return ERR_PTR(-ENOMEM); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3733 | |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3734 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd); |
| 3735 | if (!hdr) { |
| 3736 | err = -EMSGSIZE; |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3737 | goto out_free_msg; |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3738 | } |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3739 | |
| 3740 | err = devlink_nl_put_handle(msg, devlink); |
| 3741 | if (err) |
| 3742 | goto out_cancel_msg; |
| 3743 | |
| 3744 | err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 3745 | region->ops->name); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3746 | if (err) |
| 3747 | goto out_cancel_msg; |
| 3748 | |
| 3749 | if (snapshot) { |
| 3750 | err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, |
| 3751 | snapshot->id); |
| 3752 | if (err) |
| 3753 | goto out_cancel_msg; |
| 3754 | } else { |
| 3755 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, |
| 3756 | region->size, DEVLINK_ATTR_PAD); |
| 3757 | if (err) |
| 3758 | goto out_cancel_msg; |
| 3759 | } |
| 3760 | genlmsg_end(msg, hdr); |
| 3761 | |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3762 | return msg; |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3763 | |
| 3764 | out_cancel_msg: |
| 3765 | genlmsg_cancel(msg, hdr); |
| 3766 | out_free_msg: |
| 3767 | nlmsg_free(msg); |
Jakub Kicinski | dd86fec | 2020-05-01 09:40:40 -0700 | [diff] [blame] | 3768 | return ERR_PTR(err); |
| 3769 | } |
| 3770 | |
| 3771 | static void devlink_nl_region_notify(struct devlink_region *region, |
| 3772 | struct devlink_snapshot *snapshot, |
| 3773 | enum devlink_command cmd) |
| 3774 | { |
| 3775 | struct devlink *devlink = region->devlink; |
| 3776 | struct sk_buff *msg; |
| 3777 | |
| 3778 | WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); |
| 3779 | |
| 3780 | msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0); |
| 3781 | if (IS_ERR(msg)) |
| 3782 | return; |
| 3783 | |
| 3784 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3785 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3786 | } |
| 3787 | |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 3788 | /** |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3789 | * __devlink_snapshot_id_increment - Increment number of snapshots using an id |
| 3790 | * @devlink: devlink instance |
| 3791 | * @id: the snapshot id |
| 3792 | * |
| 3793 | * Track when a new snapshot begins using an id. Load the count for the |
| 3794 | * given id from the snapshot xarray, increment it, and store it back. |
| 3795 | * |
| 3796 | * Called when a new snapshot is created with the given id. |
| 3797 | * |
| 3798 | * The id *must* have been previously allocated by |
| 3799 | * devlink_region_snapshot_id_get(). |
| 3800 | * |
| 3801 | * Returns 0 on success, or an error on failure. |
| 3802 | */ |
| 3803 | static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id) |
| 3804 | { |
| 3805 | unsigned long count; |
| 3806 | void *p; |
| 3807 | |
| 3808 | lockdep_assert_held(&devlink->lock); |
| 3809 | |
| 3810 | p = xa_load(&devlink->snapshot_ids, id); |
| 3811 | if (WARN_ON(!p)) |
| 3812 | return -EINVAL; |
| 3813 | |
| 3814 | if (WARN_ON(!xa_is_value(p))) |
| 3815 | return -EINVAL; |
| 3816 | |
| 3817 | count = xa_to_value(p); |
| 3818 | count++; |
| 3819 | |
| 3820 | return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count), |
| 3821 | GFP_KERNEL)); |
| 3822 | } |
| 3823 | |
| 3824 | /** |
| 3825 | * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id |
| 3826 | * @devlink: devlink instance |
| 3827 | * @id: the snapshot id |
| 3828 | * |
| 3829 | * Track when a snapshot is deleted and stops using an id. Load the count |
| 3830 | * for the given id from the snapshot xarray, decrement it, and store it |
| 3831 | * back. |
| 3832 | * |
| 3833 | * If the count reaches zero, erase this id from the xarray, freeing it |
| 3834 | * up for future re-use by devlink_region_snapshot_id_get(). |
| 3835 | * |
| 3836 | * Called when a snapshot using the given id is deleted, and when the |
| 3837 | * initial allocator of the id is finished using it. |
| 3838 | */ |
| 3839 | static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id) |
| 3840 | { |
| 3841 | unsigned long count; |
| 3842 | void *p; |
| 3843 | |
| 3844 | lockdep_assert_held(&devlink->lock); |
| 3845 | |
| 3846 | p = xa_load(&devlink->snapshot_ids, id); |
| 3847 | if (WARN_ON(!p)) |
| 3848 | return; |
| 3849 | |
| 3850 | if (WARN_ON(!xa_is_value(p))) |
| 3851 | return; |
| 3852 | |
| 3853 | count = xa_to_value(p); |
| 3854 | |
| 3855 | if (count > 1) { |
| 3856 | count--; |
| 3857 | xa_store(&devlink->snapshot_ids, id, xa_mk_value(count), |
| 3858 | GFP_KERNEL); |
| 3859 | } else { |
| 3860 | /* If this was the last user, we can erase this id */ |
| 3861 | xa_erase(&devlink->snapshot_ids, id); |
| 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | /** |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 3866 | * __devlink_snapshot_id_insert - Insert a specific snapshot ID |
| 3867 | * @devlink: devlink instance |
| 3868 | * @id: the snapshot id |
| 3869 | * |
| 3870 | * Mark the given snapshot id as used by inserting a zero value into the |
| 3871 | * snapshot xarray. |
| 3872 | * |
| 3873 | * This must be called while holding the devlink instance lock. Unlike |
| 3874 | * devlink_snapshot_id_get, the initial reference count is zero, not one. |
| 3875 | * It is expected that the id will immediately be used before |
| 3876 | * releasing the devlink instance lock. |
| 3877 | * |
| 3878 | * Returns zero on success, or an error code if the snapshot id could not |
| 3879 | * be inserted. |
| 3880 | */ |
| 3881 | static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id) |
| 3882 | { |
| 3883 | lockdep_assert_held(&devlink->lock); |
| 3884 | |
| 3885 | if (WARN_ON(xa_load(&devlink->snapshot_ids, id))) |
| 3886 | return -EEXIST; |
| 3887 | |
| 3888 | return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0), |
| 3889 | GFP_KERNEL)); |
| 3890 | } |
| 3891 | |
| 3892 | /** |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 3893 | * __devlink_region_snapshot_id_get - get snapshot ID |
| 3894 | * @devlink: devlink instance |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 3895 | * @id: storage to return snapshot id |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 3896 | * |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 3897 | * Allocates a new snapshot id. Returns zero on success, or a negative |
| 3898 | * error on failure. Must be called while holding the devlink instance |
| 3899 | * lock. |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3900 | * |
| 3901 | * Snapshot IDs are tracked using an xarray which stores the number of |
| 3902 | * users of the snapshot id. |
| 3903 | * |
| 3904 | * Note that the caller of this function counts as a 'user', in order to |
| 3905 | * avoid race conditions. The caller must release its hold on the |
| 3906 | * snapshot by using devlink_region_snapshot_id_put. |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 3907 | */ |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 3908 | static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id) |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 3909 | { |
| 3910 | lockdep_assert_held(&devlink->lock); |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 3911 | |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3912 | return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1), |
| 3913 | xa_limit_32b, GFP_KERNEL); |
Jacob Keller | 7000108 | 2020-03-26 11:37:13 -0700 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | /** |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 3917 | * __devlink_region_snapshot_create - create a new snapshot |
| 3918 | * This will add a new snapshot of a region. The snapshot |
| 3919 | * will be stored on the region struct and can be accessed |
| 3920 | * from devlink. This is useful for future analyses of snapshots. |
| 3921 | * Multiple snapshots can be created on a region. |
| 3922 | * The @snapshot_id should be obtained using the getter function. |
| 3923 | * |
| 3924 | * Must be called only while holding the devlink instance lock. |
| 3925 | * |
| 3926 | * @region: devlink region of the snapshot |
| 3927 | * @data: snapshot data |
| 3928 | * @snapshot_id: snapshot id to be created |
| 3929 | */ |
| 3930 | static int |
| 3931 | __devlink_region_snapshot_create(struct devlink_region *region, |
| 3932 | u8 *data, u32 snapshot_id) |
| 3933 | { |
| 3934 | struct devlink *devlink = region->devlink; |
| 3935 | struct devlink_snapshot *snapshot; |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3936 | int err; |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 3937 | |
| 3938 | lockdep_assert_held(&devlink->lock); |
| 3939 | |
| 3940 | /* check if region can hold one more snapshot */ |
| 3941 | if (region->cur_snapshots == region->max_snapshots) |
Jacob Keller | 47a39f6 | 2020-03-26 11:37:12 -0700 | [diff] [blame] | 3942 | return -ENOSPC; |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 3943 | |
| 3944 | if (devlink_region_snapshot_get_by_id(region, snapshot_id)) |
| 3945 | return -EEXIST; |
| 3946 | |
| 3947 | snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL); |
| 3948 | if (!snapshot) |
| 3949 | return -ENOMEM; |
| 3950 | |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3951 | err = __devlink_snapshot_id_increment(devlink, snapshot_id); |
| 3952 | if (err) |
| 3953 | goto err_snapshot_id_increment; |
| 3954 | |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 3955 | snapshot->id = snapshot_id; |
| 3956 | snapshot->region = region; |
| 3957 | snapshot->data = data; |
| 3958 | |
| 3959 | list_add_tail(&snapshot->list, ®ion->snapshot_list); |
| 3960 | |
| 3961 | region->cur_snapshots++; |
| 3962 | |
| 3963 | devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW); |
| 3964 | return 0; |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3965 | |
| 3966 | err_snapshot_id_increment: |
| 3967 | kfree(snapshot); |
| 3968 | return err; |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 3969 | } |
| 3970 | |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 3971 | static void devlink_region_snapshot_del(struct devlink_region *region, |
| 3972 | struct devlink_snapshot *snapshot) |
| 3973 | { |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3974 | struct devlink *devlink = region->devlink; |
| 3975 | |
| 3976 | lockdep_assert_held(&devlink->lock); |
| 3977 | |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 3978 | devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL); |
| 3979 | region->cur_snapshots--; |
| 3980 | list_del(&snapshot->list); |
Jacob Keller | a0a09f6 | 2020-03-26 11:37:09 -0700 | [diff] [blame] | 3981 | region->ops->destructor(snapshot->data); |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 3982 | __devlink_snapshot_id_decrement(devlink, snapshot->id); |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 3983 | kfree(snapshot); |
| 3984 | } |
| 3985 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3986 | static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb, |
| 3987 | struct genl_info *info) |
| 3988 | { |
| 3989 | struct devlink *devlink = info->user_ptr[0]; |
| 3990 | struct devlink_region *region; |
| 3991 | const char *region_name; |
| 3992 | struct sk_buff *msg; |
| 3993 | int err; |
| 3994 | |
| 3995 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) |
| 3996 | return -EINVAL; |
| 3997 | |
| 3998 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 3999 | region = devlink_region_get_by_name(devlink, region_name); |
| 4000 | if (!region) |
| 4001 | return -EINVAL; |
| 4002 | |
| 4003 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4004 | if (!msg) |
| 4005 | return -ENOMEM; |
| 4006 | |
| 4007 | err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET, |
| 4008 | info->snd_portid, info->snd_seq, 0, |
| 4009 | region); |
| 4010 | if (err) { |
| 4011 | nlmsg_free(msg); |
| 4012 | return err; |
| 4013 | } |
| 4014 | |
| 4015 | return genlmsg_reply(msg, info); |
| 4016 | } |
| 4017 | |
| 4018 | static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg, |
| 4019 | struct netlink_callback *cb) |
| 4020 | { |
| 4021 | struct devlink_region *region; |
| 4022 | struct devlink *devlink; |
| 4023 | int start = cb->args[0]; |
| 4024 | int idx = 0; |
| 4025 | int err; |
| 4026 | |
| 4027 | mutex_lock(&devlink_mutex); |
| 4028 | list_for_each_entry(devlink, &devlink_list, list) { |
| 4029 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 4030 | continue; |
| 4031 | |
| 4032 | mutex_lock(&devlink->lock); |
| 4033 | list_for_each_entry(region, &devlink->region_list, list) { |
| 4034 | if (idx < start) { |
| 4035 | idx++; |
| 4036 | continue; |
| 4037 | } |
| 4038 | err = devlink_nl_region_fill(msg, devlink, |
| 4039 | DEVLINK_CMD_REGION_GET, |
| 4040 | NETLINK_CB(cb->skb).portid, |
| 4041 | cb->nlh->nlmsg_seq, |
| 4042 | NLM_F_MULTI, region); |
| 4043 | if (err) { |
| 4044 | mutex_unlock(&devlink->lock); |
| 4045 | goto out; |
| 4046 | } |
| 4047 | idx++; |
| 4048 | } |
| 4049 | mutex_unlock(&devlink->lock); |
| 4050 | } |
| 4051 | out: |
| 4052 | mutex_unlock(&devlink_mutex); |
| 4053 | cb->args[0] = idx; |
| 4054 | return msg->len; |
| 4055 | } |
| 4056 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 4057 | static int devlink_nl_cmd_region_del(struct sk_buff *skb, |
| 4058 | struct genl_info *info) |
| 4059 | { |
| 4060 | struct devlink *devlink = info->user_ptr[0]; |
| 4061 | struct devlink_snapshot *snapshot; |
| 4062 | struct devlink_region *region; |
| 4063 | const char *region_name; |
| 4064 | u32 snapshot_id; |
| 4065 | |
| 4066 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME] || |
| 4067 | !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) |
| 4068 | return -EINVAL; |
| 4069 | |
| 4070 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 4071 | snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); |
| 4072 | |
| 4073 | region = devlink_region_get_by_name(devlink, region_name); |
| 4074 | if (!region) |
| 4075 | return -EINVAL; |
| 4076 | |
| 4077 | snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); |
| 4078 | if (!snapshot) |
| 4079 | return -EINVAL; |
| 4080 | |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 4081 | devlink_region_snapshot_del(region, snapshot); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 4082 | return 0; |
| 4083 | } |
| 4084 | |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4085 | static int |
| 4086 | devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info) |
| 4087 | { |
| 4088 | struct devlink *devlink = info->user_ptr[0]; |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4089 | struct devlink_snapshot *snapshot; |
| 4090 | struct nlattr *snapshot_id_attr; |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4091 | struct devlink_region *region; |
| 4092 | const char *region_name; |
| 4093 | u32 snapshot_id; |
| 4094 | u8 *data; |
| 4095 | int err; |
| 4096 | |
| 4097 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) { |
| 4098 | NL_SET_ERR_MSG_MOD(info->extack, "No region name provided"); |
| 4099 | return -EINVAL; |
| 4100 | } |
| 4101 | |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4102 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 4103 | region = devlink_region_get_by_name(devlink, region_name); |
| 4104 | if (!region) { |
| 4105 | NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist"); |
| 4106 | return -EINVAL; |
| 4107 | } |
| 4108 | |
| 4109 | if (!region->ops->snapshot) { |
| 4110 | NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot"); |
| 4111 | return -EOPNOTSUPP; |
| 4112 | } |
| 4113 | |
| 4114 | if (region->cur_snapshots == region->max_snapshots) { |
| 4115 | NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots"); |
| 4116 | return -ENOSPC; |
| 4117 | } |
| 4118 | |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4119 | snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]; |
| 4120 | if (snapshot_id_attr) { |
| 4121 | snapshot_id = nla_get_u32(snapshot_id_attr); |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4122 | |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4123 | if (devlink_region_snapshot_get_by_id(region, snapshot_id)) { |
| 4124 | NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use"); |
| 4125 | return -EEXIST; |
| 4126 | } |
| 4127 | |
| 4128 | err = __devlink_snapshot_id_insert(devlink, snapshot_id); |
| 4129 | if (err) |
| 4130 | return err; |
| 4131 | } else { |
| 4132 | err = __devlink_region_snapshot_id_get(devlink, &snapshot_id); |
| 4133 | if (err) { |
| 4134 | NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id"); |
| 4135 | return err; |
| 4136 | } |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4137 | } |
| 4138 | |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4139 | err = region->ops->snapshot(devlink, info->extack, &data); |
| 4140 | if (err) |
| 4141 | goto err_snapshot_capture; |
| 4142 | |
| 4143 | err = __devlink_region_snapshot_create(region, data, snapshot_id); |
| 4144 | if (err) |
| 4145 | goto err_snapshot_create; |
| 4146 | |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4147 | if (!snapshot_id_attr) { |
| 4148 | struct sk_buff *msg; |
| 4149 | |
| 4150 | snapshot = devlink_region_snapshot_get_by_id(region, |
| 4151 | snapshot_id); |
| 4152 | if (WARN_ON(!snapshot)) |
| 4153 | return -EINVAL; |
| 4154 | |
| 4155 | msg = devlink_nl_region_notify_build(region, snapshot, |
| 4156 | DEVLINK_CMD_REGION_NEW, |
| 4157 | info->snd_portid, |
| 4158 | info->snd_seq); |
| 4159 | err = PTR_ERR_OR_ZERO(msg); |
| 4160 | if (err) |
| 4161 | goto err_notify; |
| 4162 | |
| 4163 | err = genlmsg_reply(msg, info); |
| 4164 | if (err) |
| 4165 | goto err_notify; |
| 4166 | } |
| 4167 | |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4168 | return 0; |
| 4169 | |
| 4170 | err_snapshot_create: |
| 4171 | region->ops->destructor(data); |
| 4172 | err_snapshot_capture: |
| 4173 | __devlink_snapshot_id_decrement(devlink, snapshot_id); |
| 4174 | return err; |
Jakub Kicinski | 043b3e2 | 2020-05-01 09:40:41 -0700 | [diff] [blame] | 4175 | |
| 4176 | err_notify: |
| 4177 | devlink_region_snapshot_del(region, snapshot); |
| 4178 | return err; |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4181 | static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg, |
| 4182 | struct devlink *devlink, |
| 4183 | u8 *chunk, u32 chunk_size, |
| 4184 | u64 addr) |
| 4185 | { |
| 4186 | struct nlattr *chunk_attr; |
| 4187 | int err; |
| 4188 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4189 | chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4190 | if (!chunk_attr) |
| 4191 | return -EINVAL; |
| 4192 | |
| 4193 | err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk); |
| 4194 | if (err) |
| 4195 | goto nla_put_failure; |
| 4196 | |
| 4197 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr, |
| 4198 | DEVLINK_ATTR_PAD); |
| 4199 | if (err) |
| 4200 | goto nla_put_failure; |
| 4201 | |
| 4202 | nla_nest_end(msg, chunk_attr); |
| 4203 | return 0; |
| 4204 | |
| 4205 | nla_put_failure: |
| 4206 | nla_nest_cancel(msg, chunk_attr); |
| 4207 | return err; |
| 4208 | } |
| 4209 | |
| 4210 | #define DEVLINK_REGION_READ_CHUNK_SIZE 256 |
| 4211 | |
| 4212 | static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb, |
| 4213 | struct devlink *devlink, |
| 4214 | struct devlink_region *region, |
| 4215 | struct nlattr **attrs, |
| 4216 | u64 start_offset, |
| 4217 | u64 end_offset, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4218 | u64 *new_offset) |
| 4219 | { |
| 4220 | struct devlink_snapshot *snapshot; |
| 4221 | u64 curr_offset = start_offset; |
| 4222 | u32 snapshot_id; |
| 4223 | int err = 0; |
| 4224 | |
| 4225 | *new_offset = start_offset; |
| 4226 | |
| 4227 | snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); |
| 4228 | snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); |
| 4229 | if (!snapshot) |
| 4230 | return -EINVAL; |
| 4231 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4232 | while (curr_offset < end_offset) { |
| 4233 | u32 data_size; |
| 4234 | u8 *data; |
| 4235 | |
| 4236 | if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE) |
| 4237 | data_size = end_offset - curr_offset; |
| 4238 | else |
| 4239 | data_size = DEVLINK_REGION_READ_CHUNK_SIZE; |
| 4240 | |
| 4241 | data = &snapshot->data[curr_offset]; |
| 4242 | err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink, |
| 4243 | data, data_size, |
| 4244 | curr_offset); |
| 4245 | if (err) |
| 4246 | break; |
| 4247 | |
| 4248 | curr_offset += data_size; |
| 4249 | } |
| 4250 | *new_offset = curr_offset; |
| 4251 | |
| 4252 | return err; |
| 4253 | } |
| 4254 | |
| 4255 | static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb, |
| 4256 | struct netlink_callback *cb) |
| 4257 | { |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 4258 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
Jakub Kicinski | 5a46b06 | 2020-05-13 10:28:22 -0700 | [diff] [blame] | 4259 | u64 ret_offset, start_offset, end_offset = U64_MAX; |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 4260 | struct nlattr **attrs = info->attrs; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4261 | struct devlink_region *region; |
| 4262 | struct nlattr *chunks_attr; |
| 4263 | const char *region_name; |
| 4264 | struct devlink *devlink; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4265 | void *hdr; |
| 4266 | int err; |
| 4267 | |
| 4268 | start_offset = *((u64 *)&cb->args[0]); |
| 4269 | |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 4270 | mutex_lock(&devlink_mutex); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4271 | devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4272 | if (IS_ERR(devlink)) { |
| 4273 | err = PTR_ERR(devlink); |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 4274 | goto out_dev; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4275 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4276 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4277 | mutex_lock(&devlink->lock); |
| 4278 | |
| 4279 | if (!attrs[DEVLINK_ATTR_REGION_NAME] || |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4280 | !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) { |
| 4281 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4282 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4283 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4284 | |
| 4285 | region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]); |
| 4286 | region = devlink_region_get_by_name(devlink, region_name); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4287 | if (!region) { |
| 4288 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4289 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4290 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4291 | |
Jakub Kicinski | 5a46b06 | 2020-05-13 10:28:22 -0700 | [diff] [blame] | 4292 | if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] && |
| 4293 | attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) { |
| 4294 | if (!start_offset) |
| 4295 | start_offset = |
| 4296 | nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); |
| 4297 | |
| 4298 | end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); |
| 4299 | end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]); |
| 4300 | } |
| 4301 | |
| 4302 | if (end_offset > region->size) |
| 4303 | end_offset = region->size; |
| 4304 | |
Jacob Keller | d5b90e9 | 2020-02-04 15:59:50 -0800 | [diff] [blame] | 4305 | /* return 0 if there is no further data to read */ |
Jakub Kicinski | 5a46b06 | 2020-05-13 10:28:22 -0700 | [diff] [blame] | 4306 | if (start_offset == end_offset) { |
Jacob Keller | d5b90e9 | 2020-02-04 15:59:50 -0800 | [diff] [blame] | 4307 | err = 0; |
| 4308 | goto out_unlock; |
| 4309 | } |
| 4310 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4311 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 4312 | &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, |
| 4313 | DEVLINK_CMD_REGION_READ); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4314 | if (!hdr) { |
| 4315 | err = -EMSGSIZE; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4316 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4317 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4318 | |
| 4319 | err = devlink_nl_put_handle(skb, devlink); |
| 4320 | if (err) |
| 4321 | goto nla_put_failure; |
| 4322 | |
| 4323 | err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name); |
| 4324 | if (err) |
| 4325 | goto nla_put_failure; |
| 4326 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4327 | chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4328 | if (!chunks_attr) { |
| 4329 | err = -EMSGSIZE; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4330 | goto nla_put_failure; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4331 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4332 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4333 | err = devlink_nl_region_read_snapshot_fill(skb, devlink, |
| 4334 | region, attrs, |
| 4335 | start_offset, |
Jakub Kicinski | 5a46b06 | 2020-05-13 10:28:22 -0700 | [diff] [blame] | 4336 | end_offset, &ret_offset); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4337 | |
| 4338 | if (err && err != -EMSGSIZE) |
| 4339 | goto nla_put_failure; |
| 4340 | |
| 4341 | /* Check if there was any progress done to prevent infinite loop */ |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4342 | if (ret_offset == start_offset) { |
| 4343 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4344 | goto nla_put_failure; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4345 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4346 | |
| 4347 | *((u64 *)&cb->args[0]) = ret_offset; |
| 4348 | |
| 4349 | nla_nest_end(skb, chunks_attr); |
| 4350 | genlmsg_end(skb, hdr); |
| 4351 | mutex_unlock(&devlink->lock); |
| 4352 | mutex_unlock(&devlink_mutex); |
| 4353 | |
| 4354 | return skb->len; |
| 4355 | |
| 4356 | nla_put_failure: |
| 4357 | genlmsg_cancel(skb, hdr); |
| 4358 | out_unlock: |
| 4359 | mutex_unlock(&devlink->lock); |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 4360 | out_dev: |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4361 | mutex_unlock(&devlink_mutex); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4362 | return err; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4363 | } |
| 4364 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4365 | struct devlink_info_req { |
| 4366 | struct sk_buff *msg; |
| 4367 | }; |
| 4368 | |
| 4369 | int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name) |
| 4370 | { |
| 4371 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name); |
| 4372 | } |
| 4373 | EXPORT_SYMBOL_GPL(devlink_info_driver_name_put); |
| 4374 | |
| 4375 | int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn) |
| 4376 | { |
| 4377 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn); |
| 4378 | } |
| 4379 | EXPORT_SYMBOL_GPL(devlink_info_serial_number_put); |
| 4380 | |
Jakub Kicinski | fc6fae7 | 2019-01-31 10:50:41 -0800 | [diff] [blame] | 4381 | static int devlink_info_version_put(struct devlink_info_req *req, int attr, |
| 4382 | const char *version_name, |
| 4383 | const char *version_value) |
| 4384 | { |
| 4385 | struct nlattr *nest; |
| 4386 | int err; |
| 4387 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4388 | nest = nla_nest_start_noflag(req->msg, attr); |
Jakub Kicinski | fc6fae7 | 2019-01-31 10:50:41 -0800 | [diff] [blame] | 4389 | if (!nest) |
| 4390 | return -EMSGSIZE; |
| 4391 | |
| 4392 | err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME, |
| 4393 | version_name); |
| 4394 | if (err) |
| 4395 | goto nla_put_failure; |
| 4396 | |
| 4397 | err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE, |
| 4398 | version_value); |
| 4399 | if (err) |
| 4400 | goto nla_put_failure; |
| 4401 | |
| 4402 | nla_nest_end(req->msg, nest); |
| 4403 | |
| 4404 | return 0; |
| 4405 | |
| 4406 | nla_put_failure: |
| 4407 | nla_nest_cancel(req->msg, nest); |
| 4408 | return err; |
| 4409 | } |
| 4410 | |
| 4411 | int devlink_info_version_fixed_put(struct devlink_info_req *req, |
| 4412 | const char *version_name, |
| 4413 | const char *version_value) |
| 4414 | { |
| 4415 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED, |
| 4416 | version_name, version_value); |
| 4417 | } |
| 4418 | EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put); |
| 4419 | |
| 4420 | int devlink_info_version_stored_put(struct devlink_info_req *req, |
| 4421 | const char *version_name, |
| 4422 | const char *version_value) |
| 4423 | { |
| 4424 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED, |
| 4425 | version_name, version_value); |
| 4426 | } |
| 4427 | EXPORT_SYMBOL_GPL(devlink_info_version_stored_put); |
| 4428 | |
| 4429 | int devlink_info_version_running_put(struct devlink_info_req *req, |
| 4430 | const char *version_name, |
| 4431 | const char *version_value) |
| 4432 | { |
| 4433 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING, |
| 4434 | version_name, version_value); |
| 4435 | } |
| 4436 | EXPORT_SYMBOL_GPL(devlink_info_version_running_put); |
| 4437 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4438 | static int |
| 4439 | devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink, |
| 4440 | enum devlink_command cmd, u32 portid, |
| 4441 | u32 seq, int flags, struct netlink_ext_ack *extack) |
| 4442 | { |
| 4443 | struct devlink_info_req req; |
| 4444 | void *hdr; |
| 4445 | int err; |
| 4446 | |
| 4447 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 4448 | if (!hdr) |
| 4449 | return -EMSGSIZE; |
| 4450 | |
| 4451 | err = -EMSGSIZE; |
| 4452 | if (devlink_nl_put_handle(msg, devlink)) |
| 4453 | goto err_cancel_msg; |
| 4454 | |
| 4455 | req.msg = msg; |
| 4456 | err = devlink->ops->info_get(devlink, &req, extack); |
| 4457 | if (err) |
| 4458 | goto err_cancel_msg; |
| 4459 | |
| 4460 | genlmsg_end(msg, hdr); |
| 4461 | return 0; |
| 4462 | |
| 4463 | err_cancel_msg: |
| 4464 | genlmsg_cancel(msg, hdr); |
| 4465 | return err; |
| 4466 | } |
| 4467 | |
| 4468 | static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb, |
| 4469 | struct genl_info *info) |
| 4470 | { |
| 4471 | struct devlink *devlink = info->user_ptr[0]; |
| 4472 | struct sk_buff *msg; |
| 4473 | int err; |
| 4474 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 4475 | if (!devlink->ops->info_get) |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4476 | return -EOPNOTSUPP; |
| 4477 | |
| 4478 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4479 | if (!msg) |
| 4480 | return -ENOMEM; |
| 4481 | |
| 4482 | err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET, |
| 4483 | info->snd_portid, info->snd_seq, 0, |
| 4484 | info->extack); |
| 4485 | if (err) { |
| 4486 | nlmsg_free(msg); |
| 4487 | return err; |
| 4488 | } |
| 4489 | |
| 4490 | return genlmsg_reply(msg, info); |
| 4491 | } |
| 4492 | |
| 4493 | static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg, |
| 4494 | struct netlink_callback *cb) |
| 4495 | { |
| 4496 | struct devlink *devlink; |
| 4497 | int start = cb->args[0]; |
| 4498 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 4499 | int err = 0; |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4500 | |
| 4501 | mutex_lock(&devlink_mutex); |
| 4502 | list_for_each_entry(devlink, &devlink_list, list) { |
| 4503 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 4504 | continue; |
| 4505 | if (idx < start) { |
| 4506 | idx++; |
| 4507 | continue; |
| 4508 | } |
| 4509 | |
Jiri Pirko | c493b09b | 2019-03-24 00:21:03 +0100 | [diff] [blame] | 4510 | if (!devlink->ops->info_get) { |
| 4511 | idx++; |
| 4512 | continue; |
| 4513 | } |
| 4514 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4515 | mutex_lock(&devlink->lock); |
| 4516 | err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET, |
| 4517 | NETLINK_CB(cb->skb).portid, |
| 4518 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 4519 | cb->extack); |
| 4520 | mutex_unlock(&devlink->lock); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 4521 | if (err && err != -EOPNOTSUPP) |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4522 | break; |
| 4523 | idx++; |
| 4524 | } |
| 4525 | mutex_unlock(&devlink_mutex); |
| 4526 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 4527 | if (err != -EMSGSIZE) |
| 4528 | return err; |
| 4529 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4530 | cb->args[0] = idx; |
| 4531 | return msg->len; |
| 4532 | } |
| 4533 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4534 | struct devlink_fmsg_item { |
| 4535 | struct list_head list; |
| 4536 | int attrtype; |
| 4537 | u8 nla_type; |
| 4538 | u16 len; |
Gustavo A. R. Silva | d2afb41 | 2020-02-28 07:43:24 -0600 | [diff] [blame] | 4539 | int value[]; |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4540 | }; |
| 4541 | |
| 4542 | struct devlink_fmsg { |
| 4543 | struct list_head item_list; |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4544 | bool putting_binary; /* This flag forces enclosing of binary data |
| 4545 | * in an array brackets. It forces using |
| 4546 | * of designated API: |
| 4547 | * devlink_fmsg_binary_pair_nest_start() |
| 4548 | * devlink_fmsg_binary_pair_nest_end() |
| 4549 | */ |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4550 | }; |
| 4551 | |
| 4552 | static struct devlink_fmsg *devlink_fmsg_alloc(void) |
| 4553 | { |
| 4554 | struct devlink_fmsg *fmsg; |
| 4555 | |
| 4556 | fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL); |
| 4557 | if (!fmsg) |
| 4558 | return NULL; |
| 4559 | |
| 4560 | INIT_LIST_HEAD(&fmsg->item_list); |
| 4561 | |
| 4562 | return fmsg; |
| 4563 | } |
| 4564 | |
| 4565 | static void devlink_fmsg_free(struct devlink_fmsg *fmsg) |
| 4566 | { |
| 4567 | struct devlink_fmsg_item *item, *tmp; |
| 4568 | |
| 4569 | list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) { |
| 4570 | list_del(&item->list); |
| 4571 | kfree(item); |
| 4572 | } |
| 4573 | kfree(fmsg); |
| 4574 | } |
| 4575 | |
| 4576 | static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg, |
| 4577 | int attrtype) |
| 4578 | { |
| 4579 | struct devlink_fmsg_item *item; |
| 4580 | |
| 4581 | item = kzalloc(sizeof(*item), GFP_KERNEL); |
| 4582 | if (!item) |
| 4583 | return -ENOMEM; |
| 4584 | |
| 4585 | item->attrtype = attrtype; |
| 4586 | list_add_tail(&item->list, &fmsg->item_list); |
| 4587 | |
| 4588 | return 0; |
| 4589 | } |
| 4590 | |
| 4591 | int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg) |
| 4592 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4593 | if (fmsg->putting_binary) |
| 4594 | return -EINVAL; |
| 4595 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4596 | return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START); |
| 4597 | } |
| 4598 | EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start); |
| 4599 | |
| 4600 | static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg) |
| 4601 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4602 | if (fmsg->putting_binary) |
| 4603 | return -EINVAL; |
| 4604 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4605 | return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END); |
| 4606 | } |
| 4607 | |
| 4608 | int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg) |
| 4609 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4610 | if (fmsg->putting_binary) |
| 4611 | return -EINVAL; |
| 4612 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4613 | return devlink_fmsg_nest_end(fmsg); |
| 4614 | } |
| 4615 | EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end); |
| 4616 | |
| 4617 | #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN) |
| 4618 | |
| 4619 | static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name) |
| 4620 | { |
| 4621 | struct devlink_fmsg_item *item; |
| 4622 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4623 | if (fmsg->putting_binary) |
| 4624 | return -EINVAL; |
| 4625 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4626 | if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE) |
| 4627 | return -EMSGSIZE; |
| 4628 | |
| 4629 | item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL); |
| 4630 | if (!item) |
| 4631 | return -ENOMEM; |
| 4632 | |
| 4633 | item->nla_type = NLA_NUL_STRING; |
| 4634 | item->len = strlen(name) + 1; |
| 4635 | item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME; |
| 4636 | memcpy(&item->value, name, item->len); |
| 4637 | list_add_tail(&item->list, &fmsg->item_list); |
| 4638 | |
| 4639 | return 0; |
| 4640 | } |
| 4641 | |
| 4642 | int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name) |
| 4643 | { |
| 4644 | int err; |
| 4645 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4646 | if (fmsg->putting_binary) |
| 4647 | return -EINVAL; |
| 4648 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4649 | err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START); |
| 4650 | if (err) |
| 4651 | return err; |
| 4652 | |
| 4653 | err = devlink_fmsg_put_name(fmsg, name); |
| 4654 | if (err) |
| 4655 | return err; |
| 4656 | |
| 4657 | return 0; |
| 4658 | } |
| 4659 | EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start); |
| 4660 | |
| 4661 | int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4662 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4663 | if (fmsg->putting_binary) |
| 4664 | return -EINVAL; |
| 4665 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4666 | return devlink_fmsg_nest_end(fmsg); |
| 4667 | } |
| 4668 | EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end); |
| 4669 | |
| 4670 | int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg, |
| 4671 | const char *name) |
| 4672 | { |
| 4673 | int err; |
| 4674 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4675 | if (fmsg->putting_binary) |
| 4676 | return -EINVAL; |
| 4677 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4678 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4679 | if (err) |
| 4680 | return err; |
| 4681 | |
| 4682 | err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START); |
| 4683 | if (err) |
| 4684 | return err; |
| 4685 | |
| 4686 | return 0; |
| 4687 | } |
| 4688 | EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start); |
| 4689 | |
| 4690 | int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4691 | { |
| 4692 | int err; |
| 4693 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4694 | if (fmsg->putting_binary) |
| 4695 | return -EINVAL; |
| 4696 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4697 | err = devlink_fmsg_nest_end(fmsg); |
| 4698 | if (err) |
| 4699 | return err; |
| 4700 | |
| 4701 | err = devlink_fmsg_nest_end(fmsg); |
| 4702 | if (err) |
| 4703 | return err; |
| 4704 | |
| 4705 | return 0; |
| 4706 | } |
| 4707 | EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end); |
| 4708 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4709 | int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg, |
| 4710 | const char *name) |
| 4711 | { |
| 4712 | int err; |
| 4713 | |
| 4714 | err = devlink_fmsg_arr_pair_nest_start(fmsg, name); |
| 4715 | if (err) |
| 4716 | return err; |
| 4717 | |
| 4718 | fmsg->putting_binary = true; |
| 4719 | return err; |
| 4720 | } |
| 4721 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start); |
| 4722 | |
| 4723 | int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4724 | { |
| 4725 | if (!fmsg->putting_binary) |
| 4726 | return -EINVAL; |
| 4727 | |
| 4728 | fmsg->putting_binary = false; |
| 4729 | return devlink_fmsg_arr_pair_nest_end(fmsg); |
| 4730 | } |
| 4731 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end); |
| 4732 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4733 | static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg, |
| 4734 | const void *value, u16 value_len, |
| 4735 | u8 value_nla_type) |
| 4736 | { |
| 4737 | struct devlink_fmsg_item *item; |
| 4738 | |
| 4739 | if (value_len > DEVLINK_FMSG_MAX_SIZE) |
| 4740 | return -EMSGSIZE; |
| 4741 | |
| 4742 | item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL); |
| 4743 | if (!item) |
| 4744 | return -ENOMEM; |
| 4745 | |
| 4746 | item->nla_type = value_nla_type; |
| 4747 | item->len = value_len; |
| 4748 | item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; |
| 4749 | memcpy(&item->value, value, item->len); |
| 4750 | list_add_tail(&item->list, &fmsg->item_list); |
| 4751 | |
| 4752 | return 0; |
| 4753 | } |
| 4754 | |
| 4755 | int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value) |
| 4756 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4757 | if (fmsg->putting_binary) |
| 4758 | return -EINVAL; |
| 4759 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4760 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG); |
| 4761 | } |
| 4762 | EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put); |
| 4763 | |
| 4764 | int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value) |
| 4765 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4766 | if (fmsg->putting_binary) |
| 4767 | return -EINVAL; |
| 4768 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4769 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8); |
| 4770 | } |
| 4771 | EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put); |
| 4772 | |
| 4773 | int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value) |
| 4774 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4775 | if (fmsg->putting_binary) |
| 4776 | return -EINVAL; |
| 4777 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4778 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32); |
| 4779 | } |
| 4780 | EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put); |
| 4781 | |
| 4782 | int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value) |
| 4783 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4784 | if (fmsg->putting_binary) |
| 4785 | return -EINVAL; |
| 4786 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4787 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64); |
| 4788 | } |
| 4789 | EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put); |
| 4790 | |
| 4791 | int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value) |
| 4792 | { |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4793 | if (fmsg->putting_binary) |
| 4794 | return -EINVAL; |
| 4795 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4796 | return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, |
| 4797 | NLA_NUL_STRING); |
| 4798 | } |
| 4799 | EXPORT_SYMBOL_GPL(devlink_fmsg_string_put); |
| 4800 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4801 | int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value, |
| 4802 | u16 value_len) |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 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 | return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY); |
| 4808 | } |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4809 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4810 | |
| 4811 | int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4812 | bool value) |
| 4813 | { |
| 4814 | int err; |
| 4815 | |
| 4816 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4817 | if (err) |
| 4818 | return err; |
| 4819 | |
| 4820 | err = devlink_fmsg_bool_put(fmsg, value); |
| 4821 | if (err) |
| 4822 | return err; |
| 4823 | |
| 4824 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4825 | if (err) |
| 4826 | return err; |
| 4827 | |
| 4828 | return 0; |
| 4829 | } |
| 4830 | EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put); |
| 4831 | |
| 4832 | int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4833 | u8 value) |
| 4834 | { |
| 4835 | int err; |
| 4836 | |
| 4837 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4838 | if (err) |
| 4839 | return err; |
| 4840 | |
| 4841 | err = devlink_fmsg_u8_put(fmsg, value); |
| 4842 | if (err) |
| 4843 | return err; |
| 4844 | |
| 4845 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4846 | if (err) |
| 4847 | return err; |
| 4848 | |
| 4849 | return 0; |
| 4850 | } |
| 4851 | EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put); |
| 4852 | |
| 4853 | int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4854 | u32 value) |
| 4855 | { |
| 4856 | int err; |
| 4857 | |
| 4858 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4859 | if (err) |
| 4860 | return err; |
| 4861 | |
| 4862 | err = devlink_fmsg_u32_put(fmsg, value); |
| 4863 | if (err) |
| 4864 | return err; |
| 4865 | |
| 4866 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4867 | if (err) |
| 4868 | return err; |
| 4869 | |
| 4870 | return 0; |
| 4871 | } |
| 4872 | EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put); |
| 4873 | |
| 4874 | int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4875 | u64 value) |
| 4876 | { |
| 4877 | int err; |
| 4878 | |
| 4879 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4880 | if (err) |
| 4881 | return err; |
| 4882 | |
| 4883 | err = devlink_fmsg_u64_put(fmsg, value); |
| 4884 | if (err) |
| 4885 | return err; |
| 4886 | |
| 4887 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4888 | if (err) |
| 4889 | return err; |
| 4890 | |
| 4891 | return 0; |
| 4892 | } |
| 4893 | EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put); |
| 4894 | |
| 4895 | int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4896 | const char *value) |
| 4897 | { |
| 4898 | int err; |
| 4899 | |
| 4900 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4901 | if (err) |
| 4902 | return err; |
| 4903 | |
| 4904 | err = devlink_fmsg_string_put(fmsg, value); |
| 4905 | if (err) |
| 4906 | return err; |
| 4907 | |
| 4908 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4909 | if (err) |
| 4910 | return err; |
| 4911 | |
| 4912 | return 0; |
| 4913 | } |
| 4914 | EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put); |
| 4915 | |
| 4916 | 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] | 4917 | const void *value, u32 value_len) |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4918 | { |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 4919 | u32 data_size; |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4920 | int end_err; |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 4921 | u32 offset; |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4922 | int err; |
| 4923 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4924 | err = devlink_fmsg_binary_pair_nest_start(fmsg, name); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4925 | if (err) |
| 4926 | return err; |
| 4927 | |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 4928 | for (offset = 0; offset < value_len; offset += data_size) { |
| 4929 | data_size = value_len - offset; |
| 4930 | if (data_size > DEVLINK_FMSG_MAX_SIZE) |
| 4931 | data_size = DEVLINK_FMSG_MAX_SIZE; |
| 4932 | err = devlink_fmsg_binary_put(fmsg, value + offset, data_size); |
| 4933 | if (err) |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4934 | break; |
| 4935 | /* Exit from loop with a break (instead of |
| 4936 | * return) to make sure putting_binary is turned off in |
| 4937 | * devlink_fmsg_binary_pair_nest_end |
| 4938 | */ |
Aya Levin | e2cde86 | 2019-11-12 14:07:49 +0200 | [diff] [blame] | 4939 | } |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4940 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4941 | end_err = devlink_fmsg_binary_pair_nest_end(fmsg); |
| 4942 | if (end_err) |
| 4943 | err = end_err; |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4944 | |
Aya Levin | 573ed90 | 2020-02-11 14:32:42 -0800 | [diff] [blame] | 4945 | return err; |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4946 | } |
| 4947 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put); |
| 4948 | |
| 4949 | static int |
| 4950 | devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb) |
| 4951 | { |
| 4952 | switch (msg->nla_type) { |
| 4953 | case NLA_FLAG: |
| 4954 | case NLA_U8: |
| 4955 | case NLA_U32: |
| 4956 | case NLA_U64: |
| 4957 | case NLA_NUL_STRING: |
| 4958 | case NLA_BINARY: |
| 4959 | return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE, |
| 4960 | msg->nla_type); |
| 4961 | default: |
| 4962 | return -EINVAL; |
| 4963 | } |
| 4964 | } |
| 4965 | |
| 4966 | static int |
| 4967 | devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb) |
| 4968 | { |
| 4969 | int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; |
| 4970 | u8 tmp; |
| 4971 | |
| 4972 | switch (msg->nla_type) { |
| 4973 | case NLA_FLAG: |
| 4974 | /* Always provide flag data, regardless of its value */ |
| 4975 | tmp = *(bool *) msg->value; |
| 4976 | |
| 4977 | return nla_put_u8(skb, attrtype, tmp); |
| 4978 | case NLA_U8: |
| 4979 | return nla_put_u8(skb, attrtype, *(u8 *) msg->value); |
| 4980 | case NLA_U32: |
| 4981 | return nla_put_u32(skb, attrtype, *(u32 *) msg->value); |
| 4982 | case NLA_U64: |
| 4983 | return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value, |
| 4984 | DEVLINK_ATTR_PAD); |
| 4985 | case NLA_NUL_STRING: |
| 4986 | return nla_put_string(skb, attrtype, (char *) &msg->value); |
| 4987 | case NLA_BINARY: |
| 4988 | return nla_put(skb, attrtype, msg->len, (void *) &msg->value); |
| 4989 | default: |
| 4990 | return -EINVAL; |
| 4991 | } |
| 4992 | } |
| 4993 | |
| 4994 | static int |
| 4995 | devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb, |
| 4996 | int *start) |
| 4997 | { |
| 4998 | struct devlink_fmsg_item *item; |
| 4999 | struct nlattr *fmsg_nlattr; |
| 5000 | int i = 0; |
| 5001 | int err; |
| 5002 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5003 | fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5004 | if (!fmsg_nlattr) |
| 5005 | return -EMSGSIZE; |
| 5006 | |
| 5007 | list_for_each_entry(item, &fmsg->item_list, list) { |
| 5008 | if (i < *start) { |
| 5009 | i++; |
| 5010 | continue; |
| 5011 | } |
| 5012 | |
| 5013 | switch (item->attrtype) { |
| 5014 | case DEVLINK_ATTR_FMSG_OBJ_NEST_START: |
| 5015 | case DEVLINK_ATTR_FMSG_PAIR_NEST_START: |
| 5016 | case DEVLINK_ATTR_FMSG_ARR_NEST_START: |
| 5017 | case DEVLINK_ATTR_FMSG_NEST_END: |
| 5018 | err = nla_put_flag(skb, item->attrtype); |
| 5019 | break; |
| 5020 | case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA: |
| 5021 | err = devlink_fmsg_item_fill_type(item, skb); |
| 5022 | if (err) |
| 5023 | break; |
| 5024 | err = devlink_fmsg_item_fill_data(item, skb); |
| 5025 | break; |
| 5026 | case DEVLINK_ATTR_FMSG_OBJ_NAME: |
| 5027 | err = nla_put_string(skb, item->attrtype, |
| 5028 | (char *) &item->value); |
| 5029 | break; |
| 5030 | default: |
| 5031 | err = -EINVAL; |
| 5032 | break; |
| 5033 | } |
| 5034 | if (!err) |
| 5035 | *start = ++i; |
| 5036 | else |
| 5037 | break; |
| 5038 | } |
| 5039 | |
| 5040 | nla_nest_end(skb, fmsg_nlattr); |
| 5041 | return err; |
| 5042 | } |
| 5043 | |
| 5044 | static int devlink_fmsg_snd(struct devlink_fmsg *fmsg, |
| 5045 | struct genl_info *info, |
| 5046 | enum devlink_command cmd, int flags) |
| 5047 | { |
| 5048 | struct nlmsghdr *nlh; |
| 5049 | struct sk_buff *skb; |
| 5050 | bool last = false; |
| 5051 | int index = 0; |
| 5052 | void *hdr; |
| 5053 | int err; |
| 5054 | |
| 5055 | while (!last) { |
| 5056 | int tmp_index = index; |
| 5057 | |
| 5058 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 5059 | if (!skb) |
| 5060 | return -ENOMEM; |
| 5061 | |
| 5062 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 5063 | &devlink_nl_family, flags | NLM_F_MULTI, cmd); |
| 5064 | if (!hdr) { |
| 5065 | err = -EMSGSIZE; |
| 5066 | goto nla_put_failure; |
| 5067 | } |
| 5068 | |
| 5069 | err = devlink_fmsg_prepare_skb(fmsg, skb, &index); |
| 5070 | if (!err) |
| 5071 | last = true; |
| 5072 | else if (err != -EMSGSIZE || tmp_index == index) |
| 5073 | goto nla_put_failure; |
| 5074 | |
| 5075 | genlmsg_end(skb, hdr); |
| 5076 | err = genlmsg_reply(skb, info); |
| 5077 | if (err) |
| 5078 | return err; |
| 5079 | } |
| 5080 | |
| 5081 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 5082 | if (!skb) |
| 5083 | return -ENOMEM; |
| 5084 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 5085 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 5086 | if (!nlh) { |
| 5087 | err = -EMSGSIZE; |
| 5088 | goto nla_put_failure; |
| 5089 | } |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5090 | |
Li RongQing | fde55ea | 2019-02-11 19:09:07 +0800 | [diff] [blame] | 5091 | return genlmsg_reply(skb, info); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 5092 | |
| 5093 | nla_put_failure: |
| 5094 | nlmsg_free(skb); |
| 5095 | return err; |
| 5096 | } |
| 5097 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5098 | static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb, |
| 5099 | struct netlink_callback *cb, |
| 5100 | enum devlink_command cmd) |
| 5101 | { |
| 5102 | int index = cb->args[0]; |
| 5103 | int tmp_index = index; |
| 5104 | void *hdr; |
| 5105 | int err; |
| 5106 | |
| 5107 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 5108 | &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd); |
| 5109 | if (!hdr) { |
| 5110 | err = -EMSGSIZE; |
| 5111 | goto nla_put_failure; |
| 5112 | } |
| 5113 | |
| 5114 | err = devlink_fmsg_prepare_skb(fmsg, skb, &index); |
| 5115 | if ((err && err != -EMSGSIZE) || tmp_index == index) |
| 5116 | goto nla_put_failure; |
| 5117 | |
| 5118 | cb->args[0] = index; |
| 5119 | genlmsg_end(skb, hdr); |
| 5120 | return skb->len; |
| 5121 | |
| 5122 | nla_put_failure: |
| 5123 | genlmsg_cancel(skb, hdr); |
| 5124 | return err; |
| 5125 | } |
| 5126 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5127 | struct devlink_health_reporter { |
| 5128 | struct list_head list; |
| 5129 | void *priv; |
| 5130 | const struct devlink_health_reporter_ops *ops; |
| 5131 | struct devlink *devlink; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5132 | struct devlink_fmsg *dump_fmsg; |
| 5133 | struct mutex dump_lock; /* lock parallel read/write from dump buffers */ |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5134 | u64 graceful_period; |
| 5135 | bool auto_recover; |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5136 | bool auto_dump; |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5137 | u8 health_state; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5138 | u64 dump_ts; |
Aya Levin | d279505 | 2019-11-10 14:11:56 +0200 | [diff] [blame] | 5139 | u64 dump_real_ts; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5140 | u64 error_count; |
| 5141 | u64 recovery_count; |
| 5142 | u64 last_recovery_ts; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5143 | refcount_t refcount; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5144 | }; |
| 5145 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5146 | void * |
| 5147 | devlink_health_reporter_priv(struct devlink_health_reporter *reporter) |
| 5148 | { |
| 5149 | return reporter->priv; |
| 5150 | } |
| 5151 | EXPORT_SYMBOL_GPL(devlink_health_reporter_priv); |
| 5152 | |
| 5153 | static struct devlink_health_reporter * |
| 5154 | devlink_health_reporter_find_by_name(struct devlink *devlink, |
| 5155 | const char *reporter_name) |
| 5156 | { |
| 5157 | struct devlink_health_reporter *reporter; |
| 5158 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5159 | lockdep_assert_held(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5160 | list_for_each_entry(reporter, &devlink->reporter_list, list) |
| 5161 | if (!strcmp(reporter->ops->name, reporter_name)) |
| 5162 | return reporter; |
| 5163 | return NULL; |
| 5164 | } |
| 5165 | |
| 5166 | /** |
| 5167 | * devlink_health_reporter_create - create devlink health reporter |
| 5168 | * |
| 5169 | * @devlink: devlink |
| 5170 | * @ops: ops |
| 5171 | * @graceful_period: to avoid recovery loops, in msecs |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5172 | * @priv: priv |
| 5173 | */ |
| 5174 | struct devlink_health_reporter * |
| 5175 | devlink_health_reporter_create(struct devlink *devlink, |
| 5176 | const struct devlink_health_reporter_ops *ops, |
Eran Ben Elisha | ba7d16c | 2020-03-29 14:05:54 +0300 | [diff] [blame] | 5177 | u64 graceful_period, void *priv) |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5178 | { |
| 5179 | struct devlink_health_reporter *reporter; |
| 5180 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5181 | mutex_lock(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5182 | if (devlink_health_reporter_find_by_name(devlink, ops->name)) { |
| 5183 | reporter = ERR_PTR(-EEXIST); |
| 5184 | goto unlock; |
| 5185 | } |
| 5186 | |
Eran Ben Elisha | ba7d16c | 2020-03-29 14:05:54 +0300 | [diff] [blame] | 5187 | if (WARN_ON(graceful_period && !ops->recover)) { |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5188 | reporter = ERR_PTR(-EINVAL); |
| 5189 | goto unlock; |
| 5190 | } |
| 5191 | |
| 5192 | reporter = kzalloc(sizeof(*reporter), GFP_KERNEL); |
| 5193 | if (!reporter) { |
| 5194 | reporter = ERR_PTR(-ENOMEM); |
| 5195 | goto unlock; |
| 5196 | } |
| 5197 | |
| 5198 | reporter->priv = priv; |
| 5199 | reporter->ops = ops; |
| 5200 | reporter->devlink = devlink; |
| 5201 | reporter->graceful_period = graceful_period; |
Eran Ben Elisha | ba7d16c | 2020-03-29 14:05:54 +0300 | [diff] [blame] | 5202 | reporter->auto_recover = !!ops->recover; |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5203 | reporter->auto_dump = !!ops->dump; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5204 | mutex_init(&reporter->dump_lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5205 | refcount_set(&reporter->refcount, 1); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5206 | list_add_tail(&reporter->list, &devlink->reporter_list); |
| 5207 | unlock: |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5208 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5209 | return reporter; |
| 5210 | } |
| 5211 | EXPORT_SYMBOL_GPL(devlink_health_reporter_create); |
| 5212 | |
| 5213 | /** |
| 5214 | * devlink_health_reporter_destroy - destroy devlink health reporter |
| 5215 | * |
| 5216 | * @reporter: devlink health reporter to destroy |
| 5217 | */ |
| 5218 | void |
| 5219 | devlink_health_reporter_destroy(struct devlink_health_reporter *reporter) |
| 5220 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5221 | mutex_lock(&reporter->devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5222 | list_del(&reporter->list); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5223 | mutex_unlock(&reporter->devlink->reporters_lock); |
| 5224 | while (refcount_read(&reporter->refcount) > 1) |
| 5225 | msleep(100); |
Jiri Pirko | 375cf8c | 2019-03-24 11:14:24 +0100 | [diff] [blame] | 5226 | mutex_destroy(&reporter->dump_lock); |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 5227 | if (reporter->dump_fmsg) |
| 5228 | devlink_fmsg_free(reporter->dump_fmsg); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5229 | kfree(reporter); |
| 5230 | } |
| 5231 | EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy); |
| 5232 | |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5233 | static int |
| 5234 | devlink_nl_health_reporter_fill(struct sk_buff *msg, |
| 5235 | struct devlink *devlink, |
| 5236 | struct devlink_health_reporter *reporter, |
| 5237 | enum devlink_command cmd, u32 portid, |
| 5238 | u32 seq, int flags) |
| 5239 | { |
| 5240 | struct nlattr *reporter_attr; |
| 5241 | void *hdr; |
| 5242 | |
| 5243 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 5244 | if (!hdr) |
| 5245 | return -EMSGSIZE; |
| 5246 | |
| 5247 | if (devlink_nl_put_handle(msg, devlink)) |
| 5248 | goto genlmsg_cancel; |
| 5249 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5250 | reporter_attr = nla_nest_start_noflag(msg, |
| 5251 | DEVLINK_ATTR_HEALTH_REPORTER); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5252 | if (!reporter_attr) |
| 5253 | goto genlmsg_cancel; |
| 5254 | if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME, |
| 5255 | reporter->ops->name)) |
| 5256 | goto reporter_nest_cancel; |
| 5257 | if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE, |
| 5258 | reporter->health_state)) |
| 5259 | goto reporter_nest_cancel; |
Aya Levin | 5471952 | 2019-02-21 14:12:01 +0200 | [diff] [blame] | 5260 | 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] | 5261 | reporter->error_count, DEVLINK_ATTR_PAD)) |
| 5262 | goto reporter_nest_cancel; |
Aya Levin | 5471952 | 2019-02-21 14:12:01 +0200 | [diff] [blame] | 5263 | 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] | 5264 | reporter->recovery_count, DEVLINK_ATTR_PAD)) |
| 5265 | goto reporter_nest_cancel; |
Aya Levin | 574b1e1 | 2019-02-21 14:12:02 +0200 | [diff] [blame] | 5266 | if (reporter->ops->recover && |
| 5267 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5268 | reporter->graceful_period, |
| 5269 | DEVLINK_ATTR_PAD)) |
| 5270 | goto reporter_nest_cancel; |
Aya Levin | 574b1e1 | 2019-02-21 14:12:02 +0200 | [diff] [blame] | 5271 | if (reporter->ops->recover && |
| 5272 | nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5273 | reporter->auto_recover)) |
| 5274 | goto reporter_nest_cancel; |
| 5275 | if (reporter->dump_fmsg && |
| 5276 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS, |
| 5277 | jiffies_to_msecs(reporter->dump_ts), |
| 5278 | DEVLINK_ATTR_PAD)) |
| 5279 | goto reporter_nest_cancel; |
Aya Levin | d279505 | 2019-11-10 14:11:56 +0200 | [diff] [blame] | 5280 | if (reporter->dump_fmsg && |
| 5281 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS, |
| 5282 | reporter->dump_real_ts, DEVLINK_ATTR_PAD)) |
| 5283 | goto reporter_nest_cancel; |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5284 | if (reporter->ops->dump && |
| 5285 | nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP, |
| 5286 | reporter->auto_dump)) |
| 5287 | goto reporter_nest_cancel; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5288 | |
| 5289 | nla_nest_end(msg, reporter_attr); |
| 5290 | genlmsg_end(msg, hdr); |
| 5291 | return 0; |
| 5292 | |
| 5293 | reporter_nest_cancel: |
| 5294 | nla_nest_end(msg, reporter_attr); |
| 5295 | genlmsg_cancel: |
| 5296 | genlmsg_cancel(msg, hdr); |
| 5297 | return -EMSGSIZE; |
| 5298 | } |
| 5299 | |
Vikas Gupta | 97ff3bd | 2020-01-02 21:18:10 +0530 | [diff] [blame] | 5300 | static void devlink_recover_notify(struct devlink_health_reporter *reporter, |
| 5301 | enum devlink_command cmd) |
| 5302 | { |
| 5303 | struct sk_buff *msg; |
| 5304 | int err; |
| 5305 | |
| 5306 | WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER); |
| 5307 | |
| 5308 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 5309 | if (!msg) |
| 5310 | return; |
| 5311 | |
| 5312 | err = devlink_nl_health_reporter_fill(msg, reporter->devlink, |
| 5313 | reporter, cmd, 0, 0, 0); |
| 5314 | if (err) { |
| 5315 | nlmsg_free(msg); |
| 5316 | return; |
| 5317 | } |
| 5318 | |
| 5319 | genlmsg_multicast_netns(&devlink_nl_family, |
| 5320 | devlink_net(reporter->devlink), |
| 5321 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 5322 | } |
| 5323 | |
| 5324 | void |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5325 | devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter) |
| 5326 | { |
| 5327 | reporter->recovery_count++; |
| 5328 | reporter->last_recovery_ts = jiffies; |
| 5329 | } |
| 5330 | EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done); |
| 5331 | |
| 5332 | static int |
| 5333 | devlink_health_reporter_recover(struct devlink_health_reporter *reporter, |
| 5334 | void *priv_ctx, struct netlink_ext_ack *extack) |
| 5335 | { |
| 5336 | int err; |
| 5337 | |
| 5338 | if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY) |
| 5339 | return 0; |
| 5340 | |
| 5341 | if (!reporter->ops->recover) |
| 5342 | return -EOPNOTSUPP; |
| 5343 | |
| 5344 | err = reporter->ops->recover(reporter, priv_ctx, extack); |
| 5345 | if (err) |
| 5346 | return err; |
| 5347 | |
| 5348 | devlink_health_reporter_recovery_done(reporter); |
| 5349 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY; |
| 5350 | devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); |
| 5351 | |
| 5352 | return 0; |
| 5353 | } |
| 5354 | |
| 5355 | static void |
| 5356 | devlink_health_dump_clear(struct devlink_health_reporter *reporter) |
| 5357 | { |
| 5358 | if (!reporter->dump_fmsg) |
| 5359 | return; |
| 5360 | devlink_fmsg_free(reporter->dump_fmsg); |
| 5361 | reporter->dump_fmsg = NULL; |
| 5362 | } |
| 5363 | |
| 5364 | static int devlink_health_do_dump(struct devlink_health_reporter *reporter, |
| 5365 | void *priv_ctx, |
| 5366 | struct netlink_ext_ack *extack) |
| 5367 | { |
| 5368 | int err; |
| 5369 | |
| 5370 | if (!reporter->ops->dump) |
| 5371 | return 0; |
| 5372 | |
| 5373 | if (reporter->dump_fmsg) |
| 5374 | return 0; |
| 5375 | |
| 5376 | reporter->dump_fmsg = devlink_fmsg_alloc(); |
| 5377 | if (!reporter->dump_fmsg) { |
| 5378 | err = -ENOMEM; |
| 5379 | return err; |
| 5380 | } |
| 5381 | |
| 5382 | err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg); |
| 5383 | if (err) |
| 5384 | goto dump_err; |
| 5385 | |
| 5386 | err = reporter->ops->dump(reporter, reporter->dump_fmsg, |
| 5387 | priv_ctx, extack); |
| 5388 | if (err) |
| 5389 | goto dump_err; |
| 5390 | |
| 5391 | err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg); |
| 5392 | if (err) |
| 5393 | goto dump_err; |
| 5394 | |
| 5395 | reporter->dump_ts = jiffies; |
| 5396 | reporter->dump_real_ts = ktime_get_real_ns(); |
| 5397 | |
| 5398 | return 0; |
| 5399 | |
| 5400 | dump_err: |
| 5401 | devlink_health_dump_clear(reporter); |
| 5402 | return err; |
| 5403 | } |
| 5404 | |
| 5405 | int devlink_health_report(struct devlink_health_reporter *reporter, |
| 5406 | const char *msg, void *priv_ctx) |
| 5407 | { |
| 5408 | enum devlink_health_reporter_state prev_health_state; |
| 5409 | struct devlink *devlink = reporter->devlink; |
Aya Levin | bea0c5c | 2020-05-04 11:27:46 +0300 | [diff] [blame] | 5410 | unsigned long recover_ts_threshold; |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5411 | |
| 5412 | /* write a log message of the current error */ |
| 5413 | WARN_ON(!msg); |
| 5414 | trace_devlink_health_report(devlink, reporter->ops->name, msg); |
| 5415 | reporter->error_count++; |
| 5416 | prev_health_state = reporter->health_state; |
| 5417 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; |
| 5418 | devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); |
| 5419 | |
| 5420 | /* abort if the previous error wasn't recovered */ |
Aya Levin | bea0c5c | 2020-05-04 11:27:46 +0300 | [diff] [blame] | 5421 | recover_ts_threshold = reporter->last_recovery_ts + |
| 5422 | msecs_to_jiffies(reporter->graceful_period); |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5423 | if (reporter->auto_recover && |
| 5424 | (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY || |
Aya Levin | bea0c5c | 2020-05-04 11:27:46 +0300 | [diff] [blame] | 5425 | (reporter->last_recovery_ts && reporter->recovery_count && |
| 5426 | time_is_after_jiffies(recover_ts_threshold)))) { |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5427 | trace_devlink_health_recover_aborted(devlink, |
| 5428 | reporter->ops->name, |
| 5429 | reporter->health_state, |
| 5430 | jiffies - |
| 5431 | reporter->last_recovery_ts); |
| 5432 | return -ECANCELED; |
| 5433 | } |
| 5434 | |
| 5435 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; |
| 5436 | |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5437 | if (reporter->auto_dump) { |
| 5438 | mutex_lock(&reporter->dump_lock); |
| 5439 | /* store current dump of current error, for later analysis */ |
| 5440 | devlink_health_do_dump(reporter, priv_ctx, NULL); |
| 5441 | mutex_unlock(&reporter->dump_lock); |
| 5442 | } |
Moshe Shemesh | 6ec8b6c | 2020-01-23 19:57:13 +0200 | [diff] [blame] | 5443 | |
| 5444 | if (reporter->auto_recover) |
| 5445 | return devlink_health_reporter_recover(reporter, |
| 5446 | priv_ctx, NULL); |
| 5447 | |
| 5448 | return 0; |
| 5449 | } |
| 5450 | EXPORT_SYMBOL_GPL(devlink_health_report); |
| 5451 | |
| 5452 | static struct devlink_health_reporter * |
| 5453 | devlink_health_reporter_get_from_attrs(struct devlink *devlink, |
| 5454 | struct nlattr **attrs) |
| 5455 | { |
| 5456 | struct devlink_health_reporter *reporter; |
| 5457 | char *reporter_name; |
| 5458 | |
| 5459 | if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]) |
| 5460 | return NULL; |
| 5461 | |
| 5462 | reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]); |
| 5463 | mutex_lock(&devlink->reporters_lock); |
| 5464 | reporter = devlink_health_reporter_find_by_name(devlink, reporter_name); |
| 5465 | if (reporter) |
| 5466 | refcount_inc(&reporter->refcount); |
| 5467 | mutex_unlock(&devlink->reporters_lock); |
| 5468 | return reporter; |
| 5469 | } |
| 5470 | |
| 5471 | static struct devlink_health_reporter * |
| 5472 | devlink_health_reporter_get_from_info(struct devlink *devlink, |
| 5473 | struct genl_info *info) |
| 5474 | { |
| 5475 | return devlink_health_reporter_get_from_attrs(devlink, info->attrs); |
| 5476 | } |
| 5477 | |
| 5478 | static struct devlink_health_reporter * |
| 5479 | devlink_health_reporter_get_from_cb(struct netlink_callback *cb) |
| 5480 | { |
| 5481 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
| 5482 | struct devlink_health_reporter *reporter; |
| 5483 | struct nlattr **attrs = info->attrs; |
| 5484 | struct devlink *devlink; |
| 5485 | |
| 5486 | mutex_lock(&devlink_mutex); |
| 5487 | devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs); |
| 5488 | if (IS_ERR(devlink)) |
| 5489 | goto unlock; |
| 5490 | |
| 5491 | reporter = devlink_health_reporter_get_from_attrs(devlink, attrs); |
| 5492 | mutex_unlock(&devlink_mutex); |
| 5493 | return reporter; |
| 5494 | unlock: |
| 5495 | mutex_unlock(&devlink_mutex); |
| 5496 | return NULL; |
| 5497 | } |
| 5498 | |
| 5499 | static void |
| 5500 | devlink_health_reporter_put(struct devlink_health_reporter *reporter) |
| 5501 | { |
| 5502 | refcount_dec(&reporter->refcount); |
| 5503 | } |
| 5504 | |
| 5505 | void |
Vikas Gupta | 97ff3bd | 2020-01-02 21:18:10 +0530 | [diff] [blame] | 5506 | devlink_health_reporter_state_update(struct devlink_health_reporter *reporter, |
| 5507 | enum devlink_health_reporter_state state) |
| 5508 | { |
| 5509 | if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY && |
| 5510 | state != DEVLINK_HEALTH_REPORTER_STATE_ERROR)) |
| 5511 | return; |
| 5512 | |
| 5513 | if (reporter->health_state == state) |
| 5514 | return; |
| 5515 | |
| 5516 | reporter->health_state = state; |
| 5517 | trace_devlink_health_reporter_state_update(reporter->devlink, |
| 5518 | reporter->ops->name, state); |
| 5519 | devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); |
| 5520 | } |
| 5521 | EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update); |
| 5522 | |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5523 | static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb, |
| 5524 | struct genl_info *info) |
| 5525 | { |
| 5526 | struct devlink *devlink = info->user_ptr[0]; |
| 5527 | struct devlink_health_reporter *reporter; |
| 5528 | struct sk_buff *msg; |
| 5529 | int err; |
| 5530 | |
| 5531 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5532 | if (!reporter) |
| 5533 | return -EINVAL; |
| 5534 | |
| 5535 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5536 | if (!msg) { |
| 5537 | err = -ENOMEM; |
| 5538 | goto out; |
| 5539 | } |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5540 | |
| 5541 | err = devlink_nl_health_reporter_fill(msg, devlink, reporter, |
| 5542 | DEVLINK_CMD_HEALTH_REPORTER_GET, |
| 5543 | info->snd_portid, info->snd_seq, |
| 5544 | 0); |
| 5545 | if (err) { |
| 5546 | nlmsg_free(msg); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5547 | goto out; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5548 | } |
| 5549 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5550 | err = genlmsg_reply(msg, info); |
| 5551 | out: |
| 5552 | devlink_health_reporter_put(reporter); |
| 5553 | return err; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | static int |
| 5557 | devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg, |
| 5558 | struct netlink_callback *cb) |
| 5559 | { |
| 5560 | struct devlink_health_reporter *reporter; |
| 5561 | struct devlink *devlink; |
| 5562 | int start = cb->args[0]; |
| 5563 | int idx = 0; |
| 5564 | int err; |
| 5565 | |
| 5566 | mutex_lock(&devlink_mutex); |
| 5567 | list_for_each_entry(devlink, &devlink_list, list) { |
| 5568 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 5569 | continue; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5570 | mutex_lock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5571 | list_for_each_entry(reporter, &devlink->reporter_list, |
| 5572 | list) { |
| 5573 | if (idx < start) { |
| 5574 | idx++; |
| 5575 | continue; |
| 5576 | } |
| 5577 | err = devlink_nl_health_reporter_fill(msg, devlink, |
| 5578 | reporter, |
| 5579 | DEVLINK_CMD_HEALTH_REPORTER_GET, |
| 5580 | NETLINK_CB(cb->skb).portid, |
| 5581 | cb->nlh->nlmsg_seq, |
| 5582 | NLM_F_MULTI); |
| 5583 | if (err) { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5584 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5585 | goto out; |
| 5586 | } |
| 5587 | idx++; |
| 5588 | } |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5589 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5590 | } |
| 5591 | out: |
| 5592 | mutex_unlock(&devlink_mutex); |
| 5593 | |
| 5594 | cb->args[0] = idx; |
| 5595 | return msg->len; |
| 5596 | } |
| 5597 | |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5598 | static int |
| 5599 | devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb, |
| 5600 | struct genl_info *info) |
| 5601 | { |
| 5602 | struct devlink *devlink = info->user_ptr[0]; |
| 5603 | struct devlink_health_reporter *reporter; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5604 | int err; |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5605 | |
| 5606 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5607 | if (!reporter) |
| 5608 | return -EINVAL; |
| 5609 | |
| 5610 | if (!reporter->ops->recover && |
| 5611 | (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] || |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5612 | info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) { |
| 5613 | err = -EOPNOTSUPP; |
| 5614 | goto out; |
| 5615 | } |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5616 | if (!reporter->ops->dump && |
| 5617 | info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) { |
| 5618 | err = -EOPNOTSUPP; |
| 5619 | goto out; |
| 5620 | } |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5621 | |
| 5622 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]) |
| 5623 | reporter->graceful_period = |
| 5624 | nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]); |
| 5625 | |
| 5626 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]) |
| 5627 | reporter->auto_recover = |
| 5628 | nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]); |
| 5629 | |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 5630 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) |
| 5631 | reporter->auto_dump = |
| 5632 | nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]); |
| 5633 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5634 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5635 | return 0; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5636 | out: |
| 5637 | devlink_health_reporter_put(reporter); |
| 5638 | return err; |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5639 | } |
| 5640 | |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5641 | static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb, |
| 5642 | struct genl_info *info) |
| 5643 | { |
| 5644 | struct devlink *devlink = info->user_ptr[0]; |
| 5645 | struct devlink_health_reporter *reporter; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5646 | int err; |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5647 | |
| 5648 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5649 | if (!reporter) |
| 5650 | return -EINVAL; |
| 5651 | |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5652 | err = devlink_health_reporter_recover(reporter, NULL, info->extack); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5653 | |
| 5654 | devlink_health_reporter_put(reporter); |
| 5655 | return err; |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5656 | } |
| 5657 | |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5658 | static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb, |
| 5659 | struct genl_info *info) |
| 5660 | { |
| 5661 | struct devlink *devlink = info->user_ptr[0]; |
| 5662 | struct devlink_health_reporter *reporter; |
| 5663 | struct devlink_fmsg *fmsg; |
| 5664 | int err; |
| 5665 | |
| 5666 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5667 | if (!reporter) |
| 5668 | return -EINVAL; |
| 5669 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5670 | if (!reporter->ops->diagnose) { |
| 5671 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5672 | return -EOPNOTSUPP; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5673 | } |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5674 | |
| 5675 | fmsg = devlink_fmsg_alloc(); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5676 | if (!fmsg) { |
| 5677 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5678 | return -ENOMEM; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5679 | } |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5680 | |
| 5681 | err = devlink_fmsg_obj_nest_start(fmsg); |
| 5682 | if (err) |
| 5683 | goto out; |
| 5684 | |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5685 | err = reporter->ops->diagnose(reporter, fmsg, info->extack); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5686 | if (err) |
| 5687 | goto out; |
| 5688 | |
| 5689 | err = devlink_fmsg_obj_nest_end(fmsg); |
| 5690 | if (err) |
| 5691 | goto out; |
| 5692 | |
| 5693 | err = devlink_fmsg_snd(fmsg, info, |
| 5694 | DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0); |
| 5695 | |
| 5696 | out: |
| 5697 | devlink_fmsg_free(fmsg); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5698 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5699 | return err; |
| 5700 | } |
| 5701 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5702 | static int |
| 5703 | devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb, |
| 5704 | struct netlink_callback *cb) |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5705 | { |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5706 | struct devlink_health_reporter *reporter; |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5707 | u64 start = cb->args[0]; |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5708 | int err; |
| 5709 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5710 | reporter = devlink_health_reporter_get_from_cb(cb); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5711 | if (!reporter) |
| 5712 | return -EINVAL; |
| 5713 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5714 | if (!reporter->ops->dump) { |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5715 | err = -EOPNOTSUPP; |
| 5716 | goto out; |
| 5717 | } |
| 5718 | mutex_lock(&reporter->dump_lock); |
| 5719 | if (!start) { |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5720 | err = devlink_health_do_dump(reporter, NULL, cb->extack); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5721 | if (err) |
| 5722 | goto unlock; |
| 5723 | cb->args[1] = reporter->dump_ts; |
| 5724 | } |
| 5725 | if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) { |
| 5726 | NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry"); |
| 5727 | err = -EAGAIN; |
| 5728 | goto unlock; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5729 | } |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5730 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5731 | err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb, |
| 5732 | DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET); |
| 5733 | unlock: |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5734 | mutex_unlock(&reporter->dump_lock); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5735 | out: |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5736 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5737 | return err; |
| 5738 | } |
| 5739 | |
| 5740 | static int |
| 5741 | devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb, |
| 5742 | struct genl_info *info) |
| 5743 | { |
| 5744 | struct devlink *devlink = info->user_ptr[0]; |
| 5745 | struct devlink_health_reporter *reporter; |
| 5746 | |
| 5747 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5748 | if (!reporter) |
| 5749 | return -EINVAL; |
| 5750 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5751 | if (!reporter->ops->dump) { |
| 5752 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5753 | return -EOPNOTSUPP; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5754 | } |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5755 | |
| 5756 | mutex_lock(&reporter->dump_lock); |
| 5757 | devlink_health_dump_clear(reporter); |
| 5758 | mutex_unlock(&reporter->dump_lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5759 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5760 | return 0; |
| 5761 | } |
| 5762 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5763 | struct devlink_stats { |
| 5764 | u64 rx_bytes; |
| 5765 | u64 rx_packets; |
| 5766 | struct u64_stats_sync syncp; |
| 5767 | }; |
| 5768 | |
| 5769 | /** |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 5770 | * struct devlink_trap_policer_item - Packet trap policer attributes. |
| 5771 | * @policer: Immutable packet trap policer attributes. |
| 5772 | * @rate: Rate in packets / sec. |
| 5773 | * @burst: Burst size in packets. |
| 5774 | * @list: trap_policer_list member. |
| 5775 | * |
| 5776 | * Describes packet trap policer attributes. Created by devlink during trap |
| 5777 | * policer registration. |
| 5778 | */ |
| 5779 | struct devlink_trap_policer_item { |
| 5780 | const struct devlink_trap_policer *policer; |
| 5781 | u64 rate; |
| 5782 | u64 burst; |
| 5783 | struct list_head list; |
| 5784 | }; |
| 5785 | |
| 5786 | /** |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5787 | * struct devlink_trap_group_item - Packet trap group attributes. |
| 5788 | * @group: Immutable packet trap group attributes. |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 5789 | * @policer_item: Associated policer item. Can be NULL. |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5790 | * @list: trap_group_list member. |
| 5791 | * @stats: Trap group statistics. |
| 5792 | * |
| 5793 | * Describes packet trap group attributes. Created by devlink during trap |
Ido Schimmel | a09b37f | 2020-03-22 20:48:29 +0200 | [diff] [blame] | 5794 | * group registration. |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5795 | */ |
| 5796 | struct devlink_trap_group_item { |
| 5797 | const struct devlink_trap_group *group; |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 5798 | struct devlink_trap_policer_item *policer_item; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5799 | struct list_head list; |
| 5800 | struct devlink_stats __percpu *stats; |
| 5801 | }; |
| 5802 | |
| 5803 | /** |
| 5804 | * struct devlink_trap_item - Packet trap attributes. |
| 5805 | * @trap: Immutable packet trap attributes. |
| 5806 | * @group_item: Associated group item. |
| 5807 | * @list: trap_list member. |
| 5808 | * @action: Trap action. |
| 5809 | * @stats: Trap statistics. |
| 5810 | * @priv: Driver private information. |
| 5811 | * |
| 5812 | * Describes both mutable and immutable packet trap attributes. Created by |
| 5813 | * devlink during trap registration and used for all trap related operations. |
| 5814 | */ |
| 5815 | struct devlink_trap_item { |
| 5816 | const struct devlink_trap *trap; |
| 5817 | struct devlink_trap_group_item *group_item; |
| 5818 | struct list_head list; |
| 5819 | enum devlink_trap_action action; |
| 5820 | struct devlink_stats __percpu *stats; |
| 5821 | void *priv; |
| 5822 | }; |
| 5823 | |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 5824 | static struct devlink_trap_policer_item * |
| 5825 | devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id) |
| 5826 | { |
| 5827 | struct devlink_trap_policer_item *policer_item; |
| 5828 | |
| 5829 | list_for_each_entry(policer_item, &devlink->trap_policer_list, list) { |
| 5830 | if (policer_item->policer->id == id) |
| 5831 | return policer_item; |
| 5832 | } |
| 5833 | |
| 5834 | return NULL; |
| 5835 | } |
| 5836 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5837 | static struct devlink_trap_item * |
| 5838 | devlink_trap_item_lookup(struct devlink *devlink, const char *name) |
| 5839 | { |
| 5840 | struct devlink_trap_item *trap_item; |
| 5841 | |
| 5842 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
| 5843 | if (!strcmp(trap_item->trap->name, name)) |
| 5844 | return trap_item; |
| 5845 | } |
| 5846 | |
| 5847 | return NULL; |
| 5848 | } |
| 5849 | |
| 5850 | static struct devlink_trap_item * |
| 5851 | devlink_trap_item_get_from_info(struct devlink *devlink, |
| 5852 | struct genl_info *info) |
| 5853 | { |
| 5854 | struct nlattr *attr; |
| 5855 | |
| 5856 | if (!info->attrs[DEVLINK_ATTR_TRAP_NAME]) |
| 5857 | return NULL; |
| 5858 | attr = info->attrs[DEVLINK_ATTR_TRAP_NAME]; |
| 5859 | |
| 5860 | return devlink_trap_item_lookup(devlink, nla_data(attr)); |
| 5861 | } |
| 5862 | |
| 5863 | static int |
| 5864 | devlink_trap_action_get_from_info(struct genl_info *info, |
| 5865 | enum devlink_trap_action *p_trap_action) |
| 5866 | { |
| 5867 | u8 val; |
| 5868 | |
| 5869 | val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]); |
| 5870 | switch (val) { |
| 5871 | case DEVLINK_TRAP_ACTION_DROP: /* fall-through */ |
Ido Schimmel | 9eefeab | 2020-05-29 21:36:39 +0300 | [diff] [blame] | 5872 | case DEVLINK_TRAP_ACTION_TRAP: /* fall-through */ |
| 5873 | case DEVLINK_TRAP_ACTION_MIRROR: |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5874 | *p_trap_action = val; |
| 5875 | break; |
| 5876 | default: |
| 5877 | return -EINVAL; |
| 5878 | } |
| 5879 | |
| 5880 | return 0; |
| 5881 | } |
| 5882 | |
| 5883 | static int devlink_trap_metadata_put(struct sk_buff *msg, |
| 5884 | const struct devlink_trap *trap) |
| 5885 | { |
| 5886 | struct nlattr *attr; |
| 5887 | |
| 5888 | attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA); |
| 5889 | if (!attr) |
| 5890 | return -EMSGSIZE; |
| 5891 | |
| 5892 | if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) && |
| 5893 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT)) |
| 5894 | goto nla_put_failure; |
Jiri Pirko | 85b0589 | 2020-02-25 11:45:19 +0100 | [diff] [blame] | 5895 | if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) && |
| 5896 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE)) |
| 5897 | goto nla_put_failure; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5898 | |
| 5899 | nla_nest_end(msg, attr); |
| 5900 | |
| 5901 | return 0; |
| 5902 | |
| 5903 | nla_put_failure: |
| 5904 | nla_nest_cancel(msg, attr); |
| 5905 | return -EMSGSIZE; |
| 5906 | } |
| 5907 | |
| 5908 | static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats, |
| 5909 | struct devlink_stats *stats) |
| 5910 | { |
| 5911 | int i; |
| 5912 | |
| 5913 | memset(stats, 0, sizeof(*stats)); |
| 5914 | for_each_possible_cpu(i) { |
| 5915 | struct devlink_stats *cpu_stats; |
| 5916 | u64 rx_packets, rx_bytes; |
| 5917 | unsigned int start; |
| 5918 | |
| 5919 | cpu_stats = per_cpu_ptr(trap_stats, i); |
| 5920 | do { |
| 5921 | start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
| 5922 | rx_packets = cpu_stats->rx_packets; |
| 5923 | rx_bytes = cpu_stats->rx_bytes; |
| 5924 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); |
| 5925 | |
| 5926 | stats->rx_packets += rx_packets; |
| 5927 | stats->rx_bytes += rx_bytes; |
| 5928 | } |
| 5929 | } |
| 5930 | |
| 5931 | static int devlink_trap_stats_put(struct sk_buff *msg, |
| 5932 | struct devlink_stats __percpu *trap_stats) |
| 5933 | { |
| 5934 | struct devlink_stats stats; |
| 5935 | struct nlattr *attr; |
| 5936 | |
| 5937 | devlink_trap_stats_read(trap_stats, &stats); |
| 5938 | |
| 5939 | attr = nla_nest_start(msg, DEVLINK_ATTR_STATS); |
| 5940 | if (!attr) |
| 5941 | return -EMSGSIZE; |
| 5942 | |
| 5943 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS, |
| 5944 | stats.rx_packets, DEVLINK_ATTR_PAD)) |
| 5945 | goto nla_put_failure; |
| 5946 | |
| 5947 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES, |
| 5948 | stats.rx_bytes, DEVLINK_ATTR_PAD)) |
| 5949 | goto nla_put_failure; |
| 5950 | |
| 5951 | nla_nest_end(msg, attr); |
| 5952 | |
| 5953 | return 0; |
| 5954 | |
| 5955 | nla_put_failure: |
| 5956 | nla_nest_cancel(msg, attr); |
| 5957 | return -EMSGSIZE; |
| 5958 | } |
| 5959 | |
| 5960 | static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink, |
| 5961 | const struct devlink_trap_item *trap_item, |
| 5962 | enum devlink_command cmd, u32 portid, u32 seq, |
| 5963 | int flags) |
| 5964 | { |
| 5965 | struct devlink_trap_group_item *group_item = trap_item->group_item; |
| 5966 | void *hdr; |
| 5967 | int err; |
| 5968 | |
| 5969 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 5970 | if (!hdr) |
| 5971 | return -EMSGSIZE; |
| 5972 | |
| 5973 | if (devlink_nl_put_handle(msg, devlink)) |
| 5974 | goto nla_put_failure; |
| 5975 | |
| 5976 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME, |
| 5977 | group_item->group->name)) |
| 5978 | goto nla_put_failure; |
| 5979 | |
| 5980 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name)) |
| 5981 | goto nla_put_failure; |
| 5982 | |
| 5983 | if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type)) |
| 5984 | goto nla_put_failure; |
| 5985 | |
| 5986 | if (trap_item->trap->generic && |
| 5987 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC)) |
| 5988 | goto nla_put_failure; |
| 5989 | |
| 5990 | if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action)) |
| 5991 | goto nla_put_failure; |
| 5992 | |
| 5993 | err = devlink_trap_metadata_put(msg, trap_item->trap); |
| 5994 | if (err) |
| 5995 | goto nla_put_failure; |
| 5996 | |
| 5997 | err = devlink_trap_stats_put(msg, trap_item->stats); |
| 5998 | if (err) |
| 5999 | goto nla_put_failure; |
| 6000 | |
| 6001 | genlmsg_end(msg, hdr); |
| 6002 | |
| 6003 | return 0; |
| 6004 | |
| 6005 | nla_put_failure: |
| 6006 | genlmsg_cancel(msg, hdr); |
| 6007 | return -EMSGSIZE; |
| 6008 | } |
| 6009 | |
| 6010 | static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb, |
| 6011 | struct genl_info *info) |
| 6012 | { |
| 6013 | struct netlink_ext_ack *extack = info->extack; |
| 6014 | struct devlink *devlink = info->user_ptr[0]; |
| 6015 | struct devlink_trap_item *trap_item; |
| 6016 | struct sk_buff *msg; |
| 6017 | int err; |
| 6018 | |
| 6019 | if (list_empty(&devlink->trap_list)) |
| 6020 | return -EOPNOTSUPP; |
| 6021 | |
| 6022 | trap_item = devlink_trap_item_get_from_info(devlink, info); |
| 6023 | if (!trap_item) { |
| 6024 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap"); |
| 6025 | return -ENOENT; |
| 6026 | } |
| 6027 | |
| 6028 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 6029 | if (!msg) |
| 6030 | return -ENOMEM; |
| 6031 | |
| 6032 | err = devlink_nl_trap_fill(msg, devlink, trap_item, |
| 6033 | DEVLINK_CMD_TRAP_NEW, info->snd_portid, |
| 6034 | info->snd_seq, 0); |
| 6035 | if (err) |
| 6036 | goto err_trap_fill; |
| 6037 | |
| 6038 | return genlmsg_reply(msg, info); |
| 6039 | |
| 6040 | err_trap_fill: |
| 6041 | nlmsg_free(msg); |
| 6042 | return err; |
| 6043 | } |
| 6044 | |
| 6045 | static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg, |
| 6046 | struct netlink_callback *cb) |
| 6047 | { |
| 6048 | struct devlink_trap_item *trap_item; |
| 6049 | struct devlink *devlink; |
| 6050 | int start = cb->args[0]; |
| 6051 | int idx = 0; |
| 6052 | int err; |
| 6053 | |
| 6054 | mutex_lock(&devlink_mutex); |
| 6055 | list_for_each_entry(devlink, &devlink_list, list) { |
| 6056 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 6057 | continue; |
| 6058 | mutex_lock(&devlink->lock); |
| 6059 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
| 6060 | if (idx < start) { |
| 6061 | idx++; |
| 6062 | continue; |
| 6063 | } |
| 6064 | err = devlink_nl_trap_fill(msg, devlink, trap_item, |
| 6065 | DEVLINK_CMD_TRAP_NEW, |
| 6066 | NETLINK_CB(cb->skb).portid, |
| 6067 | cb->nlh->nlmsg_seq, |
| 6068 | NLM_F_MULTI); |
| 6069 | if (err) { |
| 6070 | mutex_unlock(&devlink->lock); |
| 6071 | goto out; |
| 6072 | } |
| 6073 | idx++; |
| 6074 | } |
| 6075 | mutex_unlock(&devlink->lock); |
| 6076 | } |
| 6077 | out: |
| 6078 | mutex_unlock(&devlink_mutex); |
| 6079 | |
| 6080 | cb->args[0] = idx; |
| 6081 | return msg->len; |
| 6082 | } |
| 6083 | |
| 6084 | static int __devlink_trap_action_set(struct devlink *devlink, |
| 6085 | struct devlink_trap_item *trap_item, |
| 6086 | enum devlink_trap_action trap_action, |
| 6087 | struct netlink_ext_ack *extack) |
| 6088 | { |
| 6089 | int err; |
| 6090 | |
| 6091 | if (trap_item->action != trap_action && |
| 6092 | trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) { |
| 6093 | NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping"); |
| 6094 | return 0; |
| 6095 | } |
| 6096 | |
| 6097 | err = devlink->ops->trap_action_set(devlink, trap_item->trap, |
| 6098 | trap_action); |
| 6099 | if (err) |
| 6100 | return err; |
| 6101 | |
| 6102 | trap_item->action = trap_action; |
| 6103 | |
| 6104 | return 0; |
| 6105 | } |
| 6106 | |
| 6107 | static int devlink_trap_action_set(struct devlink *devlink, |
| 6108 | struct devlink_trap_item *trap_item, |
| 6109 | struct genl_info *info) |
| 6110 | { |
| 6111 | enum devlink_trap_action trap_action; |
| 6112 | int err; |
| 6113 | |
| 6114 | if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION]) |
| 6115 | return 0; |
| 6116 | |
| 6117 | err = devlink_trap_action_get_from_info(info, &trap_action); |
| 6118 | if (err) { |
| 6119 | NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action"); |
| 6120 | return -EINVAL; |
| 6121 | } |
| 6122 | |
| 6123 | return __devlink_trap_action_set(devlink, trap_item, trap_action, |
| 6124 | info->extack); |
| 6125 | } |
| 6126 | |
| 6127 | static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb, |
| 6128 | struct genl_info *info) |
| 6129 | { |
| 6130 | struct netlink_ext_ack *extack = info->extack; |
| 6131 | struct devlink *devlink = info->user_ptr[0]; |
| 6132 | struct devlink_trap_item *trap_item; |
| 6133 | int err; |
| 6134 | |
| 6135 | if (list_empty(&devlink->trap_list)) |
| 6136 | return -EOPNOTSUPP; |
| 6137 | |
| 6138 | trap_item = devlink_trap_item_get_from_info(devlink, info); |
| 6139 | if (!trap_item) { |
| 6140 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap"); |
| 6141 | return -ENOENT; |
| 6142 | } |
| 6143 | |
| 6144 | err = devlink_trap_action_set(devlink, trap_item, info); |
| 6145 | if (err) |
| 6146 | return err; |
| 6147 | |
| 6148 | return 0; |
| 6149 | } |
| 6150 | |
| 6151 | static struct devlink_trap_group_item * |
| 6152 | devlink_trap_group_item_lookup(struct devlink *devlink, const char *name) |
| 6153 | { |
| 6154 | struct devlink_trap_group_item *group_item; |
| 6155 | |
| 6156 | list_for_each_entry(group_item, &devlink->trap_group_list, list) { |
| 6157 | if (!strcmp(group_item->group->name, name)) |
| 6158 | return group_item; |
| 6159 | } |
| 6160 | |
| 6161 | return NULL; |
| 6162 | } |
| 6163 | |
| 6164 | static struct devlink_trap_group_item * |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 6165 | devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id) |
| 6166 | { |
| 6167 | struct devlink_trap_group_item *group_item; |
| 6168 | |
| 6169 | list_for_each_entry(group_item, &devlink->trap_group_list, list) { |
| 6170 | if (group_item->group->id == id) |
| 6171 | return group_item; |
| 6172 | } |
| 6173 | |
| 6174 | return NULL; |
| 6175 | } |
| 6176 | |
| 6177 | static struct devlink_trap_group_item * |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6178 | devlink_trap_group_item_get_from_info(struct devlink *devlink, |
| 6179 | struct genl_info *info) |
| 6180 | { |
| 6181 | char *name; |
| 6182 | |
| 6183 | if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]) |
| 6184 | return NULL; |
| 6185 | name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]); |
| 6186 | |
| 6187 | return devlink_trap_group_item_lookup(devlink, name); |
| 6188 | } |
| 6189 | |
| 6190 | static int |
| 6191 | devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink, |
| 6192 | const struct devlink_trap_group_item *group_item, |
| 6193 | enum devlink_command cmd, u32 portid, u32 seq, |
| 6194 | int flags) |
| 6195 | { |
| 6196 | void *hdr; |
| 6197 | int err; |
| 6198 | |
| 6199 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 6200 | if (!hdr) |
| 6201 | return -EMSGSIZE; |
| 6202 | |
| 6203 | if (devlink_nl_put_handle(msg, devlink)) |
| 6204 | goto nla_put_failure; |
| 6205 | |
| 6206 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME, |
| 6207 | group_item->group->name)) |
| 6208 | goto nla_put_failure; |
| 6209 | |
| 6210 | if (group_item->group->generic && |
| 6211 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC)) |
| 6212 | goto nla_put_failure; |
| 6213 | |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 6214 | if (group_item->policer_item && |
| 6215 | nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID, |
| 6216 | group_item->policer_item->policer->id)) |
| 6217 | goto nla_put_failure; |
| 6218 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6219 | err = devlink_trap_stats_put(msg, group_item->stats); |
| 6220 | if (err) |
| 6221 | goto nla_put_failure; |
| 6222 | |
| 6223 | genlmsg_end(msg, hdr); |
| 6224 | |
| 6225 | return 0; |
| 6226 | |
| 6227 | nla_put_failure: |
| 6228 | genlmsg_cancel(msg, hdr); |
| 6229 | return -EMSGSIZE; |
| 6230 | } |
| 6231 | |
| 6232 | static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb, |
| 6233 | struct genl_info *info) |
| 6234 | { |
| 6235 | struct netlink_ext_ack *extack = info->extack; |
| 6236 | struct devlink *devlink = info->user_ptr[0]; |
| 6237 | struct devlink_trap_group_item *group_item; |
| 6238 | struct sk_buff *msg; |
| 6239 | int err; |
| 6240 | |
| 6241 | if (list_empty(&devlink->trap_group_list)) |
| 6242 | return -EOPNOTSUPP; |
| 6243 | |
| 6244 | group_item = devlink_trap_group_item_get_from_info(devlink, info); |
| 6245 | if (!group_item) { |
| 6246 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group"); |
| 6247 | return -ENOENT; |
| 6248 | } |
| 6249 | |
| 6250 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 6251 | if (!msg) |
| 6252 | return -ENOMEM; |
| 6253 | |
| 6254 | err = devlink_nl_trap_group_fill(msg, devlink, group_item, |
| 6255 | DEVLINK_CMD_TRAP_GROUP_NEW, |
| 6256 | info->snd_portid, info->snd_seq, 0); |
| 6257 | if (err) |
| 6258 | goto err_trap_group_fill; |
| 6259 | |
| 6260 | return genlmsg_reply(msg, info); |
| 6261 | |
| 6262 | err_trap_group_fill: |
| 6263 | nlmsg_free(msg); |
| 6264 | return err; |
| 6265 | } |
| 6266 | |
| 6267 | static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg, |
| 6268 | struct netlink_callback *cb) |
| 6269 | { |
| 6270 | enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW; |
| 6271 | struct devlink_trap_group_item *group_item; |
| 6272 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 6273 | struct devlink *devlink; |
| 6274 | int start = cb->args[0]; |
| 6275 | int idx = 0; |
| 6276 | int err; |
| 6277 | |
| 6278 | mutex_lock(&devlink_mutex); |
| 6279 | list_for_each_entry(devlink, &devlink_list, list) { |
| 6280 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 6281 | continue; |
| 6282 | mutex_lock(&devlink->lock); |
| 6283 | list_for_each_entry(group_item, &devlink->trap_group_list, |
| 6284 | list) { |
| 6285 | if (idx < start) { |
| 6286 | idx++; |
| 6287 | continue; |
| 6288 | } |
| 6289 | err = devlink_nl_trap_group_fill(msg, devlink, |
| 6290 | group_item, cmd, |
| 6291 | portid, |
| 6292 | cb->nlh->nlmsg_seq, |
| 6293 | NLM_F_MULTI); |
| 6294 | if (err) { |
| 6295 | mutex_unlock(&devlink->lock); |
| 6296 | goto out; |
| 6297 | } |
| 6298 | idx++; |
| 6299 | } |
| 6300 | mutex_unlock(&devlink->lock); |
| 6301 | } |
| 6302 | out: |
| 6303 | mutex_unlock(&devlink_mutex); |
| 6304 | |
| 6305 | cb->args[0] = idx; |
| 6306 | return msg->len; |
| 6307 | } |
| 6308 | |
| 6309 | static int |
| 6310 | __devlink_trap_group_action_set(struct devlink *devlink, |
| 6311 | struct devlink_trap_group_item *group_item, |
| 6312 | enum devlink_trap_action trap_action, |
| 6313 | struct netlink_ext_ack *extack) |
| 6314 | { |
| 6315 | const char *group_name = group_item->group->name; |
| 6316 | struct devlink_trap_item *trap_item; |
| 6317 | int err; |
| 6318 | |
| 6319 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 6320 | if (strcmp(trap_item->group_item->group->name, group_name)) |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6321 | continue; |
| 6322 | err = __devlink_trap_action_set(devlink, trap_item, |
| 6323 | trap_action, extack); |
| 6324 | if (err) |
| 6325 | return err; |
| 6326 | } |
| 6327 | |
| 6328 | return 0; |
| 6329 | } |
| 6330 | |
| 6331 | static int |
| 6332 | devlink_trap_group_action_set(struct devlink *devlink, |
| 6333 | struct devlink_trap_group_item *group_item, |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6334 | struct genl_info *info, bool *p_modified) |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6335 | { |
| 6336 | enum devlink_trap_action trap_action; |
| 6337 | int err; |
| 6338 | |
| 6339 | if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION]) |
| 6340 | return 0; |
| 6341 | |
| 6342 | err = devlink_trap_action_get_from_info(info, &trap_action); |
| 6343 | if (err) { |
| 6344 | NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action"); |
| 6345 | return -EINVAL; |
| 6346 | } |
| 6347 | |
| 6348 | err = __devlink_trap_group_action_set(devlink, group_item, trap_action, |
| 6349 | info->extack); |
| 6350 | if (err) |
| 6351 | return err; |
| 6352 | |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6353 | *p_modified = true; |
| 6354 | |
| 6355 | return 0; |
| 6356 | } |
| 6357 | |
| 6358 | static int devlink_trap_group_set(struct devlink *devlink, |
| 6359 | struct devlink_trap_group_item *group_item, |
| 6360 | struct genl_info *info) |
| 6361 | { |
| 6362 | struct devlink_trap_policer_item *policer_item; |
| 6363 | struct netlink_ext_ack *extack = info->extack; |
| 6364 | const struct devlink_trap_policer *policer; |
| 6365 | struct nlattr **attrs = info->attrs; |
| 6366 | int err; |
| 6367 | |
| 6368 | if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) |
| 6369 | return 0; |
| 6370 | |
| 6371 | if (!devlink->ops->trap_group_set) |
| 6372 | return -EOPNOTSUPP; |
| 6373 | |
| 6374 | policer_item = group_item->policer_item; |
| 6375 | if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) { |
| 6376 | u32 policer_id; |
| 6377 | |
| 6378 | policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]); |
| 6379 | policer_item = devlink_trap_policer_item_lookup(devlink, |
| 6380 | policer_id); |
| 6381 | if (policer_id && !policer_item) { |
| 6382 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer"); |
| 6383 | return -ENOENT; |
| 6384 | } |
| 6385 | } |
| 6386 | policer = policer_item ? policer_item->policer : NULL; |
| 6387 | |
| 6388 | err = devlink->ops->trap_group_set(devlink, group_item->group, policer); |
| 6389 | if (err) |
| 6390 | return err; |
| 6391 | |
| 6392 | group_item->policer_item = policer_item; |
| 6393 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6394 | return 0; |
| 6395 | } |
| 6396 | |
| 6397 | static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb, |
| 6398 | struct genl_info *info) |
| 6399 | { |
| 6400 | struct netlink_ext_ack *extack = info->extack; |
| 6401 | struct devlink *devlink = info->user_ptr[0]; |
| 6402 | struct devlink_trap_group_item *group_item; |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6403 | bool modified = false; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6404 | int err; |
| 6405 | |
| 6406 | if (list_empty(&devlink->trap_group_list)) |
| 6407 | return -EOPNOTSUPP; |
| 6408 | |
| 6409 | group_item = devlink_trap_group_item_get_from_info(devlink, info); |
| 6410 | if (!group_item) { |
| 6411 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group"); |
| 6412 | return -ENOENT; |
| 6413 | } |
| 6414 | |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6415 | err = devlink_trap_group_action_set(devlink, group_item, info, |
| 6416 | &modified); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6417 | if (err) |
| 6418 | return err; |
| 6419 | |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6420 | err = devlink_trap_group_set(devlink, group_item, info); |
| 6421 | if (err) |
| 6422 | goto err_trap_group_set; |
| 6423 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6424 | return 0; |
Ido Schimmel | c064875 | 2020-03-30 22:38:22 +0300 | [diff] [blame] | 6425 | |
| 6426 | err_trap_group_set: |
| 6427 | if (modified) |
| 6428 | NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already"); |
| 6429 | return err; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6430 | } |
| 6431 | |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 6432 | static struct devlink_trap_policer_item * |
| 6433 | devlink_trap_policer_item_get_from_info(struct devlink *devlink, |
| 6434 | struct genl_info *info) |
| 6435 | { |
| 6436 | u32 id; |
| 6437 | |
| 6438 | if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) |
| 6439 | return NULL; |
| 6440 | id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]); |
| 6441 | |
| 6442 | return devlink_trap_policer_item_lookup(devlink, id); |
| 6443 | } |
| 6444 | |
| 6445 | static int |
| 6446 | devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink, |
| 6447 | const struct devlink_trap_policer *policer) |
| 6448 | { |
| 6449 | struct nlattr *attr; |
| 6450 | u64 drops; |
| 6451 | int err; |
| 6452 | |
| 6453 | if (!devlink->ops->trap_policer_counter_get) |
| 6454 | return 0; |
| 6455 | |
| 6456 | err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops); |
| 6457 | if (err) |
| 6458 | return err; |
| 6459 | |
| 6460 | attr = nla_nest_start(msg, DEVLINK_ATTR_STATS); |
| 6461 | if (!attr) |
| 6462 | return -EMSGSIZE; |
| 6463 | |
| 6464 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops, |
| 6465 | DEVLINK_ATTR_PAD)) |
| 6466 | goto nla_put_failure; |
| 6467 | |
| 6468 | nla_nest_end(msg, attr); |
| 6469 | |
| 6470 | return 0; |
| 6471 | |
| 6472 | nla_put_failure: |
| 6473 | nla_nest_cancel(msg, attr); |
| 6474 | return -EMSGSIZE; |
| 6475 | } |
| 6476 | |
| 6477 | static int |
| 6478 | devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink, |
| 6479 | const struct devlink_trap_policer_item *policer_item, |
| 6480 | enum devlink_command cmd, u32 portid, u32 seq, |
| 6481 | int flags) |
| 6482 | { |
| 6483 | void *hdr; |
| 6484 | int err; |
| 6485 | |
| 6486 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 6487 | if (!hdr) |
| 6488 | return -EMSGSIZE; |
| 6489 | |
| 6490 | if (devlink_nl_put_handle(msg, devlink)) |
| 6491 | goto nla_put_failure; |
| 6492 | |
| 6493 | if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID, |
| 6494 | policer_item->policer->id)) |
| 6495 | goto nla_put_failure; |
| 6496 | |
| 6497 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE, |
| 6498 | policer_item->rate, DEVLINK_ATTR_PAD)) |
| 6499 | goto nla_put_failure; |
| 6500 | |
| 6501 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST, |
| 6502 | policer_item->burst, DEVLINK_ATTR_PAD)) |
| 6503 | goto nla_put_failure; |
| 6504 | |
| 6505 | err = devlink_trap_policer_stats_put(msg, devlink, |
| 6506 | policer_item->policer); |
| 6507 | if (err) |
| 6508 | goto nla_put_failure; |
| 6509 | |
| 6510 | genlmsg_end(msg, hdr); |
| 6511 | |
| 6512 | return 0; |
| 6513 | |
| 6514 | nla_put_failure: |
| 6515 | genlmsg_cancel(msg, hdr); |
| 6516 | return -EMSGSIZE; |
| 6517 | } |
| 6518 | |
| 6519 | static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb, |
| 6520 | struct genl_info *info) |
| 6521 | { |
| 6522 | struct devlink_trap_policer_item *policer_item; |
| 6523 | struct netlink_ext_ack *extack = info->extack; |
| 6524 | struct devlink *devlink = info->user_ptr[0]; |
| 6525 | struct sk_buff *msg; |
| 6526 | int err; |
| 6527 | |
| 6528 | if (list_empty(&devlink->trap_policer_list)) |
| 6529 | return -EOPNOTSUPP; |
| 6530 | |
| 6531 | policer_item = devlink_trap_policer_item_get_from_info(devlink, info); |
| 6532 | if (!policer_item) { |
| 6533 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer"); |
| 6534 | return -ENOENT; |
| 6535 | } |
| 6536 | |
| 6537 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 6538 | if (!msg) |
| 6539 | return -ENOMEM; |
| 6540 | |
| 6541 | err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, |
| 6542 | DEVLINK_CMD_TRAP_POLICER_NEW, |
| 6543 | info->snd_portid, info->snd_seq, 0); |
| 6544 | if (err) |
| 6545 | goto err_trap_policer_fill; |
| 6546 | |
| 6547 | return genlmsg_reply(msg, info); |
| 6548 | |
| 6549 | err_trap_policer_fill: |
| 6550 | nlmsg_free(msg); |
| 6551 | return err; |
| 6552 | } |
| 6553 | |
| 6554 | static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg, |
| 6555 | struct netlink_callback *cb) |
| 6556 | { |
| 6557 | enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW; |
| 6558 | struct devlink_trap_policer_item *policer_item; |
| 6559 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 6560 | struct devlink *devlink; |
| 6561 | int start = cb->args[0]; |
| 6562 | int idx = 0; |
| 6563 | int err; |
| 6564 | |
| 6565 | mutex_lock(&devlink_mutex); |
| 6566 | list_for_each_entry(devlink, &devlink_list, list) { |
| 6567 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 6568 | continue; |
| 6569 | mutex_lock(&devlink->lock); |
| 6570 | list_for_each_entry(policer_item, &devlink->trap_policer_list, |
| 6571 | list) { |
| 6572 | if (idx < start) { |
| 6573 | idx++; |
| 6574 | continue; |
| 6575 | } |
| 6576 | err = devlink_nl_trap_policer_fill(msg, devlink, |
| 6577 | policer_item, cmd, |
| 6578 | portid, |
| 6579 | cb->nlh->nlmsg_seq, |
| 6580 | NLM_F_MULTI); |
| 6581 | if (err) { |
| 6582 | mutex_unlock(&devlink->lock); |
| 6583 | goto out; |
| 6584 | } |
| 6585 | idx++; |
| 6586 | } |
| 6587 | mutex_unlock(&devlink->lock); |
| 6588 | } |
| 6589 | out: |
| 6590 | mutex_unlock(&devlink_mutex); |
| 6591 | |
| 6592 | cb->args[0] = idx; |
| 6593 | return msg->len; |
| 6594 | } |
| 6595 | |
| 6596 | static int |
| 6597 | devlink_trap_policer_set(struct devlink *devlink, |
| 6598 | struct devlink_trap_policer_item *policer_item, |
| 6599 | struct genl_info *info) |
| 6600 | { |
| 6601 | struct netlink_ext_ack *extack = info->extack; |
| 6602 | struct nlattr **attrs = info->attrs; |
| 6603 | u64 rate, burst; |
| 6604 | int err; |
| 6605 | |
| 6606 | rate = policer_item->rate; |
| 6607 | burst = policer_item->burst; |
| 6608 | |
| 6609 | if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]) |
| 6610 | rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]); |
| 6611 | |
| 6612 | if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]) |
| 6613 | burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]); |
| 6614 | |
| 6615 | if (rate < policer_item->policer->min_rate) { |
| 6616 | NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit"); |
| 6617 | return -EINVAL; |
| 6618 | } |
| 6619 | |
| 6620 | if (rate > policer_item->policer->max_rate) { |
| 6621 | NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit"); |
| 6622 | return -EINVAL; |
| 6623 | } |
| 6624 | |
| 6625 | if (burst < policer_item->policer->min_burst) { |
| 6626 | NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit"); |
| 6627 | return -EINVAL; |
| 6628 | } |
| 6629 | |
| 6630 | if (burst > policer_item->policer->max_burst) { |
| 6631 | NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit"); |
| 6632 | return -EINVAL; |
| 6633 | } |
| 6634 | |
| 6635 | err = devlink->ops->trap_policer_set(devlink, policer_item->policer, |
| 6636 | rate, burst, info->extack); |
| 6637 | if (err) |
| 6638 | return err; |
| 6639 | |
| 6640 | policer_item->rate = rate; |
| 6641 | policer_item->burst = burst; |
| 6642 | |
| 6643 | return 0; |
| 6644 | } |
| 6645 | |
| 6646 | static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb, |
| 6647 | struct genl_info *info) |
| 6648 | { |
| 6649 | struct devlink_trap_policer_item *policer_item; |
| 6650 | struct netlink_ext_ack *extack = info->extack; |
| 6651 | struct devlink *devlink = info->user_ptr[0]; |
| 6652 | |
| 6653 | if (list_empty(&devlink->trap_policer_list)) |
| 6654 | return -EOPNOTSUPP; |
| 6655 | |
| 6656 | if (!devlink->ops->trap_policer_set) |
| 6657 | return -EOPNOTSUPP; |
| 6658 | |
| 6659 | policer_item = devlink_trap_policer_item_get_from_info(devlink, info); |
| 6660 | if (!policer_item) { |
| 6661 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer"); |
| 6662 | return -ENOENT; |
| 6663 | } |
| 6664 | |
| 6665 | return devlink_trap_policer_set(devlink, policer_item, info); |
| 6666 | } |
| 6667 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6668 | static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = { |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 6669 | [DEVLINK_ATTR_UNSPEC] = { .strict_start_type = |
| 6670 | DEVLINK_ATTR_TRAP_POLICER_ID }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6671 | [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING }, |
| 6672 | [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING }, |
| 6673 | [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 }, |
| 6674 | [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 }, |
| 6675 | [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6676 | [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 }, |
| 6677 | [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 }, |
| 6678 | [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 }, |
| 6679 | [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 }, |
| 6680 | [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 }, |
| 6681 | [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 }, |
| 6682 | [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6683 | [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 }, |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 6684 | [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 }, |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 6685 | [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6686 | [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING }, |
| 6687 | [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6688 | [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64}, |
| 6689 | [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64}, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 6690 | [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING }, |
| 6691 | [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 }, |
| 6692 | [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 }, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6693 | [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING }, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6694 | [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 }, |
Jakub Kicinski | ff3b63b | 2020-03-02 21:05:12 -0800 | [diff] [blame] | 6695 | [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 }, |
| 6696 | [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 }, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 6697 | [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING }, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6698 | [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 }, |
| 6699 | [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 }, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 6700 | [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING }, |
| 6701 | [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING }, |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6702 | [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING }, |
| 6703 | [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 }, |
| 6704 | [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING }, |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 6705 | [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 }, |
| 6706 | [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 }, |
| 6707 | [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 }, |
Eran Ben Elisha | 48bb52c | 2020-03-29 14:05:55 +0300 | [diff] [blame] | 6708 | [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 }, |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 6709 | [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 }, |
| 6710 | [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 }, |
| 6711 | [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6712 | }; |
| 6713 | |
| 6714 | static const struct genl_ops devlink_nl_ops[] = { |
| 6715 | { |
| 6716 | .cmd = DEVLINK_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6717 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6718 | .doit = devlink_nl_cmd_get_doit, |
| 6719 | .dumpit = devlink_nl_cmd_get_dumpit, |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 6720 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6721 | /* can be retrieved by unprivileged users */ |
| 6722 | }, |
| 6723 | { |
| 6724 | .cmd = DEVLINK_CMD_PORT_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6725 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6726 | .doit = devlink_nl_cmd_port_get_doit, |
| 6727 | .dumpit = devlink_nl_cmd_port_get_dumpit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6728 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 6729 | /* can be retrieved by unprivileged users */ |
| 6730 | }, |
| 6731 | { |
| 6732 | .cmd = DEVLINK_CMD_PORT_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6733 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6734 | .doit = devlink_nl_cmd_port_set_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6735 | .flags = GENL_ADMIN_PERM, |
| 6736 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 6737 | }, |
| 6738 | { |
| 6739 | .cmd = DEVLINK_CMD_PORT_SPLIT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6740 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6741 | .doit = devlink_nl_cmd_port_split_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6742 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6743 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6744 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6745 | }, |
| 6746 | { |
| 6747 | .cmd = DEVLINK_CMD_PORT_UNSPLIT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6748 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6749 | .doit = devlink_nl_cmd_port_unsplit_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6750 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6751 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6752 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6753 | }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6754 | { |
| 6755 | .cmd = DEVLINK_CMD_SB_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6756 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6757 | .doit = devlink_nl_cmd_sb_get_doit, |
| 6758 | .dumpit = devlink_nl_cmd_sb_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6759 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6760 | DEVLINK_NL_FLAG_NEED_SB, |
| 6761 | /* can be retrieved by unprivileged users */ |
| 6762 | }, |
| 6763 | { |
| 6764 | .cmd = DEVLINK_CMD_SB_POOL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6765 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6766 | .doit = devlink_nl_cmd_sb_pool_get_doit, |
| 6767 | .dumpit = devlink_nl_cmd_sb_pool_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6768 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6769 | DEVLINK_NL_FLAG_NEED_SB, |
| 6770 | /* can be retrieved by unprivileged users */ |
| 6771 | }, |
| 6772 | { |
| 6773 | .cmd = DEVLINK_CMD_SB_POOL_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6774 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6775 | .doit = devlink_nl_cmd_sb_pool_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6776 | .flags = GENL_ADMIN_PERM, |
| 6777 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6778 | DEVLINK_NL_FLAG_NEED_SB, |
| 6779 | }, |
| 6780 | { |
| 6781 | .cmd = DEVLINK_CMD_SB_PORT_POOL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6782 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6783 | .doit = devlink_nl_cmd_sb_port_pool_get_doit, |
| 6784 | .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6785 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6786 | DEVLINK_NL_FLAG_NEED_SB, |
| 6787 | /* can be retrieved by unprivileged users */ |
| 6788 | }, |
| 6789 | { |
| 6790 | .cmd = DEVLINK_CMD_SB_PORT_POOL_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6791 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6792 | .doit = devlink_nl_cmd_sb_port_pool_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6793 | .flags = GENL_ADMIN_PERM, |
| 6794 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6795 | DEVLINK_NL_FLAG_NEED_SB, |
| 6796 | }, |
| 6797 | { |
| 6798 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6799 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6800 | .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit, |
| 6801 | .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6802 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6803 | DEVLINK_NL_FLAG_NEED_SB, |
| 6804 | /* can be retrieved by unprivileged users */ |
| 6805 | }, |
| 6806 | { |
| 6807 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6808 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6809 | .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6810 | .flags = GENL_ADMIN_PERM, |
| 6811 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6812 | DEVLINK_NL_FLAG_NEED_SB, |
| 6813 | }, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6814 | { |
| 6815 | .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6816 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6817 | .doit = devlink_nl_cmd_sb_occ_snapshot_doit, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6818 | .flags = GENL_ADMIN_PERM, |
| 6819 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6820 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6821 | }, |
| 6822 | { |
| 6823 | .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6824 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6825 | .doit = devlink_nl_cmd_sb_occ_max_clear_doit, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6826 | .flags = GENL_ADMIN_PERM, |
| 6827 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6828 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6829 | }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6830 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 6831 | .cmd = DEVLINK_CMD_ESWITCH_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6832 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 6833 | .doit = devlink_nl_cmd_eswitch_get_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6834 | .flags = GENL_ADMIN_PERM, |
Parav Pandit | 98fed6e | 2020-02-23 19:06:56 -0600 | [diff] [blame] | 6835 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6836 | DEVLINK_NL_FLAG_NO_LOCK, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6837 | }, |
| 6838 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 6839 | .cmd = DEVLINK_CMD_ESWITCH_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6840 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 6841 | .doit = devlink_nl_cmd_eswitch_set_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6842 | .flags = GENL_ADMIN_PERM, |
Jakub Kicinski | 7ac1cc9 | 2018-05-21 22:12:50 -0700 | [diff] [blame] | 6843 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6844 | DEVLINK_NL_FLAG_NO_LOCK, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6845 | }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6846 | { |
| 6847 | .cmd = DEVLINK_CMD_DPIPE_TABLE_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6848 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6849 | .doit = devlink_nl_cmd_dpipe_table_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6850 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 6851 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6852 | }, |
| 6853 | { |
| 6854 | .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6855 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6856 | .doit = devlink_nl_cmd_dpipe_entries_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6857 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 6858 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6859 | }, |
| 6860 | { |
| 6861 | .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6862 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6863 | .doit = devlink_nl_cmd_dpipe_headers_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6864 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 6865 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6866 | }, |
| 6867 | { |
| 6868 | .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6869 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6870 | .doit = devlink_nl_cmd_dpipe_table_counters_set, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6871 | .flags = GENL_ADMIN_PERM, |
| 6872 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6873 | }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6874 | { |
| 6875 | .cmd = DEVLINK_CMD_RESOURCE_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6876 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6877 | .doit = devlink_nl_cmd_resource_set, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6878 | .flags = GENL_ADMIN_PERM, |
| 6879 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6880 | }, |
| 6881 | { |
| 6882 | .cmd = DEVLINK_CMD_RESOURCE_DUMP, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6883 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6884 | .doit = devlink_nl_cmd_resource_dump, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6885 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 6886 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6887 | }, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 6888 | { |
| 6889 | .cmd = DEVLINK_CMD_RELOAD, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6890 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 6891 | .doit = devlink_nl_cmd_reload, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 6892 | .flags = GENL_ADMIN_PERM, |
| 6893 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6894 | DEVLINK_NL_FLAG_NO_LOCK, |
| 6895 | }, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 6896 | { |
| 6897 | .cmd = DEVLINK_CMD_PARAM_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6898 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 6899 | .doit = devlink_nl_cmd_param_get_doit, |
| 6900 | .dumpit = devlink_nl_cmd_param_get_dumpit, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 6901 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6902 | /* can be retrieved by unprivileged users */ |
| 6903 | }, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 6904 | { |
| 6905 | .cmd = DEVLINK_CMD_PARAM_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6906 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 6907 | .doit = devlink_nl_cmd_param_set_doit, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 6908 | .flags = GENL_ADMIN_PERM, |
| 6909 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6910 | }, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6911 | { |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 6912 | .cmd = DEVLINK_CMD_PORT_PARAM_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6913 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 6914 | .doit = devlink_nl_cmd_port_param_get_doit, |
| 6915 | .dumpit = devlink_nl_cmd_port_param_get_dumpit, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 6916 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 6917 | /* can be retrieved by unprivileged users */ |
| 6918 | }, |
| 6919 | { |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 6920 | .cmd = DEVLINK_CMD_PORT_PARAM_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6921 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 6922 | .doit = devlink_nl_cmd_port_param_set_doit, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 6923 | .flags = GENL_ADMIN_PERM, |
| 6924 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 6925 | }, |
| 6926 | { |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6927 | .cmd = DEVLINK_CMD_REGION_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6928 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6929 | .doit = devlink_nl_cmd_region_get_doit, |
| 6930 | .dumpit = devlink_nl_cmd_region_get_dumpit, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6931 | .flags = GENL_ADMIN_PERM, |
| 6932 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6933 | }, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6934 | { |
Jacob Keller | b9a17ab | 2020-03-26 11:37:16 -0700 | [diff] [blame] | 6935 | .cmd = DEVLINK_CMD_REGION_NEW, |
| 6936 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
| 6937 | .doit = devlink_nl_cmd_region_new, |
| 6938 | .flags = GENL_ADMIN_PERM, |
| 6939 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6940 | }, |
| 6941 | { |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6942 | .cmd = DEVLINK_CMD_REGION_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6943 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6944 | .doit = devlink_nl_cmd_region_del, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6945 | .flags = GENL_ADMIN_PERM, |
| 6946 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6947 | }, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 6948 | { |
| 6949 | .cmd = DEVLINK_CMD_REGION_READ, |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 6950 | .validate = GENL_DONT_VALIDATE_STRICT | |
| 6951 | GENL_DONT_VALIDATE_DUMP_STRICT, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 6952 | .dumpit = devlink_nl_cmd_region_read_dumpit, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 6953 | .flags = GENL_ADMIN_PERM, |
| 6954 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6955 | }, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 6956 | { |
| 6957 | .cmd = DEVLINK_CMD_INFO_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6958 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 6959 | .doit = devlink_nl_cmd_info_get_doit, |
| 6960 | .dumpit = devlink_nl_cmd_info_get_dumpit, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 6961 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6962 | /* can be retrieved by unprivileged users */ |
| 6963 | }, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 6964 | { |
| 6965 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6966 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 6967 | .doit = devlink_nl_cmd_health_reporter_get_doit, |
| 6968 | .dumpit = devlink_nl_cmd_health_reporter_get_dumpit, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6969 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6970 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 6971 | /* can be retrieved by unprivileged users */ |
| 6972 | }, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6973 | { |
| 6974 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6975 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6976 | .doit = devlink_nl_cmd_health_reporter_set_doit, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6977 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6978 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6979 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6980 | }, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 6981 | { |
| 6982 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6983 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 6984 | .doit = devlink_nl_cmd_health_reporter_recover_doit, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 6985 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6986 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6987 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 6988 | }, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 6989 | { |
| 6990 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6991 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 6992 | .doit = devlink_nl_cmd_health_reporter_diagnose_doit, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 6993 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6994 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6995 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 6996 | }, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 6997 | { |
| 6998 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET, |
Jiri Pirko | 82a843d | 2019-10-07 09:28:31 +0200 | [diff] [blame] | 6999 | .validate = GENL_DONT_VALIDATE_STRICT | |
| 7000 | GENL_DONT_VALIDATE_DUMP_STRICT, |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 7001 | .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 7002 | .flags = GENL_ADMIN_PERM, |
| 7003 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7004 | DEVLINK_NL_FLAG_NO_LOCK, |
| 7005 | }, |
| 7006 | { |
| 7007 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7008 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 7009 | .doit = devlink_nl_cmd_health_reporter_dump_clear_doit, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 7010 | .flags = GENL_ADMIN_PERM, |
| 7011 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 7012 | DEVLINK_NL_FLAG_NO_LOCK, |
| 7013 | }, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 7014 | { |
| 7015 | .cmd = DEVLINK_CMD_FLASH_UPDATE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 7016 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 7017 | .doit = devlink_nl_cmd_flash_update, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 7018 | .flags = GENL_ADMIN_PERM, |
| 7019 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7020 | }, |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7021 | { |
| 7022 | .cmd = DEVLINK_CMD_TRAP_GET, |
| 7023 | .doit = devlink_nl_cmd_trap_get_doit, |
| 7024 | .dumpit = devlink_nl_cmd_trap_get_dumpit, |
| 7025 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7026 | /* can be retrieved by unprivileged users */ |
| 7027 | }, |
| 7028 | { |
| 7029 | .cmd = DEVLINK_CMD_TRAP_SET, |
| 7030 | .doit = devlink_nl_cmd_trap_set_doit, |
| 7031 | .flags = GENL_ADMIN_PERM, |
| 7032 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7033 | }, |
| 7034 | { |
| 7035 | .cmd = DEVLINK_CMD_TRAP_GROUP_GET, |
| 7036 | .doit = devlink_nl_cmd_trap_group_get_doit, |
| 7037 | .dumpit = devlink_nl_cmd_trap_group_get_dumpit, |
| 7038 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7039 | /* can be retrieved by unprivileged users */ |
| 7040 | }, |
| 7041 | { |
| 7042 | .cmd = DEVLINK_CMD_TRAP_GROUP_SET, |
| 7043 | .doit = devlink_nl_cmd_trap_group_set_doit, |
| 7044 | .flags = GENL_ADMIN_PERM, |
| 7045 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7046 | }, |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 7047 | { |
| 7048 | .cmd = DEVLINK_CMD_TRAP_POLICER_GET, |
| 7049 | .doit = devlink_nl_cmd_trap_policer_get_doit, |
| 7050 | .dumpit = devlink_nl_cmd_trap_policer_get_dumpit, |
| 7051 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7052 | /* can be retrieved by unprivileged users */ |
| 7053 | }, |
| 7054 | { |
| 7055 | .cmd = DEVLINK_CMD_TRAP_POLICER_SET, |
| 7056 | .doit = devlink_nl_cmd_trap_policer_set_doit, |
| 7057 | .flags = GENL_ADMIN_PERM, |
| 7058 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 7059 | }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7060 | }; |
| 7061 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 7062 | static struct genl_family devlink_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 7063 | .name = DEVLINK_GENL_NAME, |
| 7064 | .version = DEVLINK_GENL_VERSION, |
| 7065 | .maxattr = DEVLINK_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 7066 | .policy = devlink_nl_policy, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 7067 | .netnsok = true, |
| 7068 | .pre_doit = devlink_nl_pre_doit, |
| 7069 | .post_doit = devlink_nl_post_doit, |
| 7070 | .module = THIS_MODULE, |
| 7071 | .ops = devlink_nl_ops, |
| 7072 | .n_ops = ARRAY_SIZE(devlink_nl_ops), |
| 7073 | .mcgrps = devlink_nl_mcgrps, |
| 7074 | .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps), |
| 7075 | }; |
| 7076 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7077 | /** |
| 7078 | * devlink_alloc - Allocate new devlink instance resources |
| 7079 | * |
| 7080 | * @ops: ops |
| 7081 | * @priv_size: size of user private data |
| 7082 | * |
| 7083 | * Allocate new devlink instance resources, including devlink index |
| 7084 | * and name. |
| 7085 | */ |
| 7086 | struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) |
| 7087 | { |
| 7088 | struct devlink *devlink; |
| 7089 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 7090 | if (WARN_ON(!ops)) |
| 7091 | return NULL; |
| 7092 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7093 | devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL); |
| 7094 | if (!devlink) |
| 7095 | return NULL; |
| 7096 | devlink->ops = ops; |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 7097 | xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC); |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 7098 | __devlink_net_set(devlink, &init_net); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7099 | INIT_LIST_HEAD(&devlink->port_list); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7100 | INIT_LIST_HEAD(&devlink->sb_list); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7101 | INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7102 | INIT_LIST_HEAD(&devlink->resource_list); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 7103 | INIT_LIST_HEAD(&devlink->param_list); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 7104 | INIT_LIST_HEAD(&devlink->region_list); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 7105 | INIT_LIST_HEAD(&devlink->reporter_list); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7106 | INIT_LIST_HEAD(&devlink->trap_list); |
| 7107 | INIT_LIST_HEAD(&devlink->trap_group_list); |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 7108 | INIT_LIST_HEAD(&devlink->trap_policer_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7109 | mutex_init(&devlink->lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 7110 | mutex_init(&devlink->reporters_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7111 | return devlink; |
| 7112 | } |
| 7113 | EXPORT_SYMBOL_GPL(devlink_alloc); |
| 7114 | |
| 7115 | /** |
| 7116 | * devlink_register - Register devlink instance |
| 7117 | * |
| 7118 | * @devlink: devlink |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 7119 | * @dev: parent device |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7120 | */ |
| 7121 | int devlink_register(struct devlink *devlink, struct device *dev) |
| 7122 | { |
| 7123 | mutex_lock(&devlink_mutex); |
| 7124 | devlink->dev = dev; |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 7125 | devlink->registered = true; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7126 | list_add_tail(&devlink->list, &devlink_list); |
| 7127 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 7128 | mutex_unlock(&devlink_mutex); |
| 7129 | return 0; |
| 7130 | } |
| 7131 | EXPORT_SYMBOL_GPL(devlink_register); |
| 7132 | |
| 7133 | /** |
| 7134 | * devlink_unregister - Unregister devlink instance |
| 7135 | * |
| 7136 | * @devlink: devlink |
| 7137 | */ |
| 7138 | void devlink_unregister(struct devlink *devlink) |
| 7139 | { |
| 7140 | mutex_lock(&devlink_mutex); |
Jiri Pirko | a0c7634 | 2019-11-08 21:42:43 +0100 | [diff] [blame] | 7141 | WARN_ON(devlink_reload_supported(devlink) && |
| 7142 | devlink->reload_enabled); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7143 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 7144 | list_del(&devlink->list); |
| 7145 | mutex_unlock(&devlink_mutex); |
| 7146 | } |
| 7147 | EXPORT_SYMBOL_GPL(devlink_unregister); |
| 7148 | |
| 7149 | /** |
Jiri Pirko | a0c7634 | 2019-11-08 21:42:43 +0100 | [diff] [blame] | 7150 | * devlink_reload_enable - Enable reload of devlink instance |
| 7151 | * |
| 7152 | * @devlink: devlink |
| 7153 | * |
| 7154 | * Should be called at end of device initialization |
| 7155 | * process when reload operation is supported. |
| 7156 | */ |
| 7157 | void devlink_reload_enable(struct devlink *devlink) |
| 7158 | { |
| 7159 | mutex_lock(&devlink_mutex); |
| 7160 | devlink->reload_enabled = true; |
| 7161 | mutex_unlock(&devlink_mutex); |
| 7162 | } |
| 7163 | EXPORT_SYMBOL_GPL(devlink_reload_enable); |
| 7164 | |
| 7165 | /** |
| 7166 | * devlink_reload_disable - Disable reload of devlink instance |
| 7167 | * |
| 7168 | * @devlink: devlink |
| 7169 | * |
| 7170 | * Should be called at the beginning of device cleanup |
| 7171 | * process when reload operation is supported. |
| 7172 | */ |
| 7173 | void devlink_reload_disable(struct devlink *devlink) |
| 7174 | { |
| 7175 | mutex_lock(&devlink_mutex); |
| 7176 | /* Mutex is taken which ensures that no reload operation is in |
| 7177 | * progress while setting up forbidded flag. |
| 7178 | */ |
| 7179 | devlink->reload_enabled = false; |
| 7180 | mutex_unlock(&devlink_mutex); |
| 7181 | } |
| 7182 | EXPORT_SYMBOL_GPL(devlink_reload_disable); |
| 7183 | |
| 7184 | /** |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7185 | * devlink_free - Free devlink instance resources |
| 7186 | * |
| 7187 | * @devlink: devlink |
| 7188 | */ |
| 7189 | void devlink_free(struct devlink *devlink) |
| 7190 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 7191 | mutex_destroy(&devlink->reporters_lock); |
Jiri Pirko | 375cf8c | 2019-03-24 11:14:24 +0100 | [diff] [blame] | 7192 | mutex_destroy(&devlink->lock); |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 7193 | WARN_ON(!list_empty(&devlink->trap_policer_list)); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7194 | WARN_ON(!list_empty(&devlink->trap_group_list)); |
| 7195 | WARN_ON(!list_empty(&devlink->trap_list)); |
Parav Pandit | b904aad | 2019-02-08 15:15:00 -0600 | [diff] [blame] | 7196 | WARN_ON(!list_empty(&devlink->reporter_list)); |
| 7197 | WARN_ON(!list_empty(&devlink->region_list)); |
| 7198 | WARN_ON(!list_empty(&devlink->param_list)); |
| 7199 | WARN_ON(!list_empty(&devlink->resource_list)); |
| 7200 | WARN_ON(!list_empty(&devlink->dpipe_table_list)); |
| 7201 | WARN_ON(!list_empty(&devlink->sb_list)); |
| 7202 | WARN_ON(!list_empty(&devlink->port_list)); |
| 7203 | |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 7204 | xa_destroy(&devlink->snapshot_ids); |
| 7205 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7206 | kfree(devlink); |
| 7207 | } |
| 7208 | EXPORT_SYMBOL_GPL(devlink_free); |
| 7209 | |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7210 | static void devlink_port_type_warn(struct work_struct *work) |
| 7211 | { |
| 7212 | WARN(true, "Type was not set for devlink port."); |
| 7213 | } |
| 7214 | |
| 7215 | static bool devlink_port_type_should_warn(struct devlink_port *devlink_port) |
| 7216 | { |
| 7217 | /* Ignore CPU and DSA flavours. */ |
| 7218 | return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU && |
| 7219 | devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA; |
| 7220 | } |
| 7221 | |
Ido Schimmel | 4c58223 | 2020-01-09 19:57:41 +0200 | [diff] [blame] | 7222 | #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600) |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7223 | |
| 7224 | static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port) |
| 7225 | { |
| 7226 | if (!devlink_port_type_should_warn(devlink_port)) |
| 7227 | return; |
| 7228 | /* Schedule a work to WARN in case driver does not set port |
| 7229 | * type within timeout. |
| 7230 | */ |
| 7231 | schedule_delayed_work(&devlink_port->type_warn_dw, |
| 7232 | DEVLINK_PORT_TYPE_WARN_TIMEOUT); |
| 7233 | } |
| 7234 | |
| 7235 | static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port) |
| 7236 | { |
| 7237 | if (!devlink_port_type_should_warn(devlink_port)) |
| 7238 | return; |
| 7239 | cancel_delayed_work_sync(&devlink_port->type_warn_dw); |
| 7240 | } |
| 7241 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7242 | /** |
| 7243 | * devlink_port_register - Register devlink port |
| 7244 | * |
| 7245 | * @devlink: devlink |
| 7246 | * @devlink_port: devlink port |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 7247 | * @port_index: driver-specific numerical identifier of the port |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7248 | * |
| 7249 | * Register devlink port with provided port index. User can use |
| 7250 | * any indexing, even hw-related one. devlink_port structure |
| 7251 | * is convenient to be embedded inside user driver private structure. |
| 7252 | * Note that the caller should take care of zeroing the devlink_port |
| 7253 | * structure. |
| 7254 | */ |
| 7255 | int devlink_port_register(struct devlink *devlink, |
| 7256 | struct devlink_port *devlink_port, |
| 7257 | unsigned int port_index) |
| 7258 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7259 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7260 | if (devlink_port_index_exists(devlink, port_index)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7261 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7262 | return -EEXIST; |
| 7263 | } |
| 7264 | devlink_port->devlink = devlink; |
| 7265 | devlink_port->index = port_index; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7266 | devlink_port->registered = true; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 7267 | spin_lock_init(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7268 | list_add_tail(&devlink_port->list, &devlink->port_list); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7269 | INIT_LIST_HEAD(&devlink_port->param_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7270 | mutex_unlock(&devlink->lock); |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7271 | INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn); |
| 7272 | devlink_port_type_warn_schedule(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7273 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 7274 | return 0; |
| 7275 | } |
| 7276 | EXPORT_SYMBOL_GPL(devlink_port_register); |
| 7277 | |
| 7278 | /** |
| 7279 | * devlink_port_unregister - Unregister devlink port |
| 7280 | * |
| 7281 | * @devlink_port: devlink port |
| 7282 | */ |
| 7283 | void devlink_port_unregister(struct devlink_port *devlink_port) |
| 7284 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7285 | struct devlink *devlink = devlink_port->devlink; |
| 7286 | |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7287 | devlink_port_type_warn_cancel(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7288 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7289 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7290 | list_del(&devlink_port->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7291 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7292 | } |
| 7293 | EXPORT_SYMBOL_GPL(devlink_port_unregister); |
| 7294 | |
| 7295 | static void __devlink_port_type_set(struct devlink_port *devlink_port, |
| 7296 | enum devlink_port_type type, |
| 7297 | void *type_dev) |
| 7298 | { |
Jiri Pirko | 2b239e7 | 2019-03-24 11:14:36 +0100 | [diff] [blame] | 7299 | if (WARN_ON(!devlink_port->registered)) |
| 7300 | return; |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7301 | devlink_port_type_warn_cancel(devlink_port); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7302 | spin_lock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7303 | devlink_port->type = type; |
| 7304 | devlink_port->type_dev = type_dev; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7305 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7306 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 7307 | } |
| 7308 | |
| 7309 | /** |
| 7310 | * devlink_port_type_eth_set - Set port type to Ethernet |
| 7311 | * |
| 7312 | * @devlink_port: devlink port |
| 7313 | * @netdev: related netdevice |
| 7314 | */ |
| 7315 | void devlink_port_type_eth_set(struct devlink_port *devlink_port, |
| 7316 | struct net_device *netdev) |
| 7317 | { |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 7318 | const struct net_device_ops *ops = netdev->netdev_ops; |
| 7319 | |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 7320 | /* If driver registers devlink port, it should set devlink port |
| 7321 | * attributes accordingly so the compat functions are called |
| 7322 | * and the original ops are not used. |
| 7323 | */ |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 7324 | if (ops->ndo_get_phys_port_name) { |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 7325 | /* Some drivers use the same set of ndos for netdevs |
| 7326 | * that have devlink_port registered and also for |
| 7327 | * those who don't. Make sure that ndo_get_phys_port_name |
| 7328 | * returns -EOPNOTSUPP here in case it is defined. |
| 7329 | * Warn if not. |
| 7330 | */ |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 7331 | char name[IFNAMSIZ]; |
| 7332 | int err; |
| 7333 | |
| 7334 | err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name)); |
| 7335 | WARN_ON(err != -EOPNOTSUPP); |
| 7336 | } |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 7337 | if (ops->ndo_get_port_parent_id) { |
| 7338 | /* Some drivers use the same set of ndos for netdevs |
| 7339 | * that have devlink_port registered and also for |
| 7340 | * those who don't. Make sure that ndo_get_port_parent_id |
| 7341 | * returns -EOPNOTSUPP here in case it is defined. |
| 7342 | * Warn if not. |
| 7343 | */ |
| 7344 | struct netdev_phys_item_id ppid; |
| 7345 | int err; |
| 7346 | |
| 7347 | err = ops->ndo_get_port_parent_id(netdev, &ppid); |
| 7348 | WARN_ON(err != -EOPNOTSUPP); |
| 7349 | } |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 7350 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7351 | } |
| 7352 | EXPORT_SYMBOL_GPL(devlink_port_type_eth_set); |
| 7353 | |
| 7354 | /** |
| 7355 | * devlink_port_type_ib_set - Set port type to InfiniBand |
| 7356 | * |
| 7357 | * @devlink_port: devlink port |
| 7358 | * @ibdev: related IB device |
| 7359 | */ |
| 7360 | void devlink_port_type_ib_set(struct devlink_port *devlink_port, |
| 7361 | struct ib_device *ibdev) |
| 7362 | { |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 7363 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7364 | } |
| 7365 | EXPORT_SYMBOL_GPL(devlink_port_type_ib_set); |
| 7366 | |
| 7367 | /** |
| 7368 | * devlink_port_type_clear - Clear port type |
| 7369 | * |
| 7370 | * @devlink_port: devlink port |
| 7371 | */ |
| 7372 | void devlink_port_type_clear(struct devlink_port *devlink_port) |
| 7373 | { |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 7374 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL); |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 7375 | devlink_port_type_warn_schedule(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7376 | } |
| 7377 | EXPORT_SYMBOL_GPL(devlink_port_type_clear); |
| 7378 | |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7379 | static int __devlink_port_attrs_set(struct devlink_port *devlink_port, |
| 7380 | enum devlink_port_flavour flavour, |
| 7381 | const unsigned char *switch_id, |
| 7382 | unsigned char switch_id_len) |
| 7383 | { |
| 7384 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 7385 | |
| 7386 | if (WARN_ON(devlink_port->registered)) |
| 7387 | return -EEXIST; |
| 7388 | attrs->set = true; |
| 7389 | attrs->flavour = flavour; |
| 7390 | if (switch_id) { |
| 7391 | attrs->switch_port = true; |
| 7392 | if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN)) |
| 7393 | switch_id_len = MAX_PHYS_ITEM_ID_LEN; |
| 7394 | memcpy(attrs->switch_id.id, switch_id, switch_id_len); |
| 7395 | attrs->switch_id.id_len = switch_id_len; |
| 7396 | } else { |
| 7397 | attrs->switch_port = false; |
| 7398 | } |
| 7399 | return 0; |
| 7400 | } |
| 7401 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7402 | /** |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7403 | * devlink_port_attrs_set - Set port attributes |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7404 | * |
| 7405 | * @devlink_port: devlink port |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 7406 | * @flavour: flavour of the port |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7407 | * @port_number: number of the port that is facing user, for example |
| 7408 | * the front panel port number |
| 7409 | * @split: indicates if this is split port |
| 7410 | * @split_subport_number: if the port is split, this is the number |
| 7411 | * of subport. |
Jiri Pirko | bec5267 | 2019-04-03 14:24:16 +0200 | [diff] [blame] | 7412 | * @switch_id: if the port is part of switch, this is buffer with ID, |
| 7413 | * otwerwise this is NULL |
| 7414 | * @switch_id_len: length of the switch_id buffer |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7415 | */ |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7416 | void devlink_port_attrs_set(struct devlink_port *devlink_port, |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 7417 | enum devlink_port_flavour flavour, |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7418 | u32 port_number, bool split, |
Jiri Pirko | bec5267 | 2019-04-03 14:24:16 +0200 | [diff] [blame] | 7419 | u32 split_subport_number, |
| 7420 | const unsigned char *switch_id, |
| 7421 | unsigned char switch_id_len) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7422 | { |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7423 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7424 | int ret; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7425 | |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7426 | ret = __devlink_port_attrs_set(devlink_port, flavour, |
| 7427 | switch_id, switch_id_len); |
| 7428 | if (ret) |
Jiri Pirko | 45b8611 | 2019-03-24 11:14:33 +0100 | [diff] [blame] | 7429 | return; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7430 | attrs->split = split; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7431 | attrs->phys.port_number = port_number; |
| 7432 | attrs->phys.split_subport_number = split_subport_number; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7433 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 7434 | EXPORT_SYMBOL_GPL(devlink_port_attrs_set); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 7435 | |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 7436 | /** |
| 7437 | * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes |
| 7438 | * |
| 7439 | * @devlink_port: devlink port |
| 7440 | * @pf: associated PF for the devlink port instance |
| 7441 | * @switch_id: if the port is part of switch, this is buffer with ID, |
| 7442 | * otherwise this is NULL |
| 7443 | * @switch_id_len: length of the switch_id buffer |
| 7444 | */ |
| 7445 | void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, |
| 7446 | const unsigned char *switch_id, |
| 7447 | unsigned char switch_id_len, u16 pf) |
| 7448 | { |
| 7449 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 7450 | int ret; |
| 7451 | |
| 7452 | ret = __devlink_port_attrs_set(devlink_port, |
| 7453 | DEVLINK_PORT_FLAVOUR_PCI_PF, |
| 7454 | switch_id, switch_id_len); |
| 7455 | if (ret) |
| 7456 | return; |
| 7457 | |
| 7458 | attrs->pci_pf.pf = pf; |
| 7459 | } |
| 7460 | EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set); |
| 7461 | |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 7462 | /** |
| 7463 | * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes |
| 7464 | * |
| 7465 | * @devlink_port: devlink port |
| 7466 | * @pf: associated PF for the devlink port instance |
| 7467 | * @vf: associated VF of a PF for the devlink port instance |
| 7468 | * @switch_id: if the port is part of switch, this is buffer with ID, |
| 7469 | * otherwise this is NULL |
| 7470 | * @switch_id_len: length of the switch_id buffer |
| 7471 | */ |
| 7472 | void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, |
| 7473 | const unsigned char *switch_id, |
| 7474 | unsigned char switch_id_len, |
| 7475 | u16 pf, u16 vf) |
| 7476 | { |
| 7477 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 7478 | int ret; |
| 7479 | |
| 7480 | ret = __devlink_port_attrs_set(devlink_port, |
| 7481 | DEVLINK_PORT_FLAVOUR_PCI_VF, |
| 7482 | switch_id, switch_id_len); |
| 7483 | if (ret) |
| 7484 | return; |
| 7485 | attrs->pci_vf.pf = pf; |
| 7486 | attrs->pci_vf.vf = vf; |
| 7487 | } |
| 7488 | EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set); |
| 7489 | |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 7490 | static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port, |
| 7491 | char *name, size_t len) |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7492 | { |
| 7493 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 7494 | int n = 0; |
| 7495 | |
| 7496 | if (!attrs->set) |
| 7497 | return -EOPNOTSUPP; |
| 7498 | |
| 7499 | switch (attrs->flavour) { |
| 7500 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: |
Parav Pandit | acf1ee4 | 2020-03-03 08:12:42 -0600 | [diff] [blame] | 7501 | case DEVLINK_PORT_FLAVOUR_VIRTUAL: |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7502 | if (!attrs->split) |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7503 | n = snprintf(name, len, "p%u", attrs->phys.port_number); |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7504 | else |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 7505 | n = snprintf(name, len, "p%us%u", |
| 7506 | attrs->phys.port_number, |
| 7507 | attrs->phys.split_subport_number); |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7508 | break; |
| 7509 | case DEVLINK_PORT_FLAVOUR_CPU: |
| 7510 | case DEVLINK_PORT_FLAVOUR_DSA: |
| 7511 | /* As CPU and DSA ports do not have a netdevice associated |
| 7512 | * case should not ever happen. |
| 7513 | */ |
| 7514 | WARN_ON(1); |
| 7515 | return -EINVAL; |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 7516 | case DEVLINK_PORT_FLAVOUR_PCI_PF: |
| 7517 | n = snprintf(name, len, "pf%u", attrs->pci_pf.pf); |
| 7518 | break; |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 7519 | case DEVLINK_PORT_FLAVOUR_PCI_VF: |
| 7520 | n = snprintf(name, len, "pf%uvf%u", |
| 7521 | attrs->pci_vf.pf, attrs->pci_vf.vf); |
| 7522 | break; |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 7523 | } |
| 7524 | |
| 7525 | if (n >= len) |
| 7526 | return -EINVAL; |
| 7527 | |
| 7528 | return 0; |
| 7529 | } |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 7530 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7531 | int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, |
| 7532 | u32 size, u16 ingress_pools_count, |
| 7533 | u16 egress_pools_count, u16 ingress_tc_count, |
| 7534 | u16 egress_tc_count) |
| 7535 | { |
| 7536 | struct devlink_sb *devlink_sb; |
| 7537 | int err = 0; |
| 7538 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7539 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7540 | if (devlink_sb_index_exists(devlink, sb_index)) { |
| 7541 | err = -EEXIST; |
| 7542 | goto unlock; |
| 7543 | } |
| 7544 | |
| 7545 | devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL); |
| 7546 | if (!devlink_sb) { |
| 7547 | err = -ENOMEM; |
| 7548 | goto unlock; |
| 7549 | } |
| 7550 | devlink_sb->index = sb_index; |
| 7551 | devlink_sb->size = size; |
| 7552 | devlink_sb->ingress_pools_count = ingress_pools_count; |
| 7553 | devlink_sb->egress_pools_count = egress_pools_count; |
| 7554 | devlink_sb->ingress_tc_count = ingress_tc_count; |
| 7555 | devlink_sb->egress_tc_count = egress_tc_count; |
| 7556 | list_add_tail(&devlink_sb->list, &devlink->sb_list); |
| 7557 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7558 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7559 | return err; |
| 7560 | } |
| 7561 | EXPORT_SYMBOL_GPL(devlink_sb_register); |
| 7562 | |
| 7563 | void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index) |
| 7564 | { |
| 7565 | struct devlink_sb *devlink_sb; |
| 7566 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7567 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7568 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 7569 | WARN_ON(!devlink_sb); |
| 7570 | list_del(&devlink_sb->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7571 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 7572 | kfree(devlink_sb); |
| 7573 | } |
| 7574 | EXPORT_SYMBOL_GPL(devlink_sb_unregister); |
| 7575 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7576 | /** |
| 7577 | * devlink_dpipe_headers_register - register dpipe headers |
| 7578 | * |
| 7579 | * @devlink: devlink |
| 7580 | * @dpipe_headers: dpipe header array |
| 7581 | * |
| 7582 | * Register the headers supported by hardware. |
| 7583 | */ |
| 7584 | int devlink_dpipe_headers_register(struct devlink *devlink, |
| 7585 | struct devlink_dpipe_headers *dpipe_headers) |
| 7586 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7587 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7588 | devlink->dpipe_headers = dpipe_headers; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7589 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7590 | return 0; |
| 7591 | } |
| 7592 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register); |
| 7593 | |
| 7594 | /** |
| 7595 | * devlink_dpipe_headers_unregister - unregister dpipe headers |
| 7596 | * |
| 7597 | * @devlink: devlink |
| 7598 | * |
| 7599 | * Unregister the headers supported by hardware. |
| 7600 | */ |
| 7601 | void devlink_dpipe_headers_unregister(struct devlink *devlink) |
| 7602 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7603 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7604 | devlink->dpipe_headers = NULL; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7605 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7606 | } |
| 7607 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister); |
| 7608 | |
| 7609 | /** |
| 7610 | * devlink_dpipe_table_counter_enabled - check if counter allocation |
| 7611 | * required |
| 7612 | * @devlink: devlink |
| 7613 | * @table_name: tables name |
| 7614 | * |
| 7615 | * Used by driver to check if counter allocation is required. |
| 7616 | * After counter allocation is turned on the table entries |
| 7617 | * are updated to include counter statistics. |
| 7618 | * |
| 7619 | * After that point on the driver must respect the counter |
| 7620 | * state so that each entry added to the table is added |
| 7621 | * with a counter. |
| 7622 | */ |
| 7623 | bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, |
| 7624 | const char *table_name) |
| 7625 | { |
| 7626 | struct devlink_dpipe_table *table; |
| 7627 | bool enabled; |
| 7628 | |
| 7629 | rcu_read_lock(); |
| 7630 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 7631 | table_name, devlink); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7632 | enabled = false; |
| 7633 | if (table) |
| 7634 | enabled = table->counters_enabled; |
| 7635 | rcu_read_unlock(); |
| 7636 | return enabled; |
| 7637 | } |
| 7638 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled); |
| 7639 | |
| 7640 | /** |
| 7641 | * devlink_dpipe_table_register - register dpipe table |
| 7642 | * |
| 7643 | * @devlink: devlink |
| 7644 | * @table_name: table name |
| 7645 | * @table_ops: table ops |
| 7646 | * @priv: priv |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7647 | * @counter_control_extern: external control for counters |
| 7648 | */ |
| 7649 | int devlink_dpipe_table_register(struct devlink *devlink, |
| 7650 | const char *table_name, |
| 7651 | struct devlink_dpipe_table_ops *table_ops, |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 7652 | void *priv, bool counter_control_extern) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7653 | { |
| 7654 | struct devlink_dpipe_table *table; |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7655 | int err = 0; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7656 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 7657 | if (WARN_ON(!table_ops->size_get)) |
| 7658 | return -EINVAL; |
| 7659 | |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7660 | mutex_lock(&devlink->lock); |
| 7661 | |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 7662 | if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name, |
| 7663 | devlink)) { |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7664 | err = -EEXIST; |
| 7665 | goto unlock; |
| 7666 | } |
| 7667 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7668 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7669 | if (!table) { |
| 7670 | err = -ENOMEM; |
| 7671 | goto unlock; |
| 7672 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7673 | |
| 7674 | table->name = table_name; |
| 7675 | table->table_ops = table_ops; |
| 7676 | table->priv = priv; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7677 | table->counter_control_extern = counter_control_extern; |
| 7678 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7679 | list_add_tail_rcu(&table->list, &devlink->dpipe_table_list); |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7680 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7681 | mutex_unlock(&devlink->lock); |
Madhuparna Bhowmik | 6132c1d | 2020-02-23 16:52:33 +0530 | [diff] [blame] | 7682 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7683 | } |
| 7684 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_register); |
| 7685 | |
| 7686 | /** |
| 7687 | * devlink_dpipe_table_unregister - unregister dpipe table |
| 7688 | * |
| 7689 | * @devlink: devlink |
| 7690 | * @table_name: table name |
| 7691 | */ |
| 7692 | void devlink_dpipe_table_unregister(struct devlink *devlink, |
| 7693 | const char *table_name) |
| 7694 | { |
| 7695 | struct devlink_dpipe_table *table; |
| 7696 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7697 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7698 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 7699 | table_name, devlink); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7700 | if (!table) |
| 7701 | goto unlock; |
| 7702 | list_del_rcu(&table->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7703 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7704 | kfree_rcu(table, rcu); |
| 7705 | return; |
| 7706 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 7707 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 7708 | } |
| 7709 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister); |
| 7710 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7711 | /** |
| 7712 | * devlink_resource_register - devlink resource register |
| 7713 | * |
| 7714 | * @devlink: devlink |
| 7715 | * @resource_name: resource's name |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7716 | * @resource_size: resource's size |
| 7717 | * @resource_id: resource's id |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 7718 | * @parent_resource_id: resource's parent id |
| 7719 | * @size_params: size parameters |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7720 | */ |
| 7721 | int devlink_resource_register(struct devlink *devlink, |
| 7722 | const char *resource_name, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7723 | u64 resource_size, |
| 7724 | u64 resource_id, |
| 7725 | u64 parent_resource_id, |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 7726 | const struct devlink_resource_size_params *size_params) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7727 | { |
| 7728 | struct devlink_resource *resource; |
| 7729 | struct list_head *resource_list; |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 7730 | bool top_hierarchy; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7731 | int err = 0; |
| 7732 | |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 7733 | top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP; |
| 7734 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7735 | mutex_lock(&devlink->lock); |
| 7736 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 7737 | if (resource) { |
| 7738 | err = -EINVAL; |
| 7739 | goto out; |
| 7740 | } |
| 7741 | |
| 7742 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
| 7743 | if (!resource) { |
| 7744 | err = -ENOMEM; |
| 7745 | goto out; |
| 7746 | } |
| 7747 | |
| 7748 | if (top_hierarchy) { |
| 7749 | resource_list = &devlink->resource_list; |
| 7750 | } else { |
| 7751 | struct devlink_resource *parent_resource; |
| 7752 | |
| 7753 | parent_resource = devlink_resource_find(devlink, NULL, |
| 7754 | parent_resource_id); |
| 7755 | if (parent_resource) { |
| 7756 | resource_list = &parent_resource->resource_list; |
| 7757 | resource->parent = parent_resource; |
| 7758 | } else { |
Colin Ian King | b75703d | 2018-01-22 10:31:19 +0000 | [diff] [blame] | 7759 | kfree(resource); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7760 | err = -EINVAL; |
| 7761 | goto out; |
| 7762 | } |
| 7763 | } |
| 7764 | |
| 7765 | resource->name = resource_name; |
| 7766 | resource->size = resource_size; |
| 7767 | resource->size_new = resource_size; |
| 7768 | resource->id = resource_id; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7769 | resource->size_valid = true; |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 7770 | memcpy(&resource->size_params, size_params, |
| 7771 | sizeof(resource->size_params)); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 7772 | INIT_LIST_HEAD(&resource->resource_list); |
| 7773 | list_add_tail(&resource->list, resource_list); |
| 7774 | out: |
| 7775 | mutex_unlock(&devlink->lock); |
| 7776 | return err; |
| 7777 | } |
| 7778 | EXPORT_SYMBOL_GPL(devlink_resource_register); |
| 7779 | |
| 7780 | /** |
| 7781 | * devlink_resources_unregister - free all resources |
| 7782 | * |
| 7783 | * @devlink: devlink |
| 7784 | * @resource: resource |
| 7785 | */ |
| 7786 | void devlink_resources_unregister(struct devlink *devlink, |
| 7787 | struct devlink_resource *resource) |
| 7788 | { |
| 7789 | struct devlink_resource *tmp, *child_resource; |
| 7790 | struct list_head *resource_list; |
| 7791 | |
| 7792 | if (resource) |
| 7793 | resource_list = &resource->resource_list; |
| 7794 | else |
| 7795 | resource_list = &devlink->resource_list; |
| 7796 | |
| 7797 | if (!resource) |
| 7798 | mutex_lock(&devlink->lock); |
| 7799 | |
| 7800 | list_for_each_entry_safe(child_resource, tmp, resource_list, list) { |
| 7801 | devlink_resources_unregister(devlink, child_resource); |
| 7802 | list_del(&child_resource->list); |
| 7803 | kfree(child_resource); |
| 7804 | } |
| 7805 | |
| 7806 | if (!resource) |
| 7807 | mutex_unlock(&devlink->lock); |
| 7808 | } |
| 7809 | EXPORT_SYMBOL_GPL(devlink_resources_unregister); |
| 7810 | |
| 7811 | /** |
| 7812 | * devlink_resource_size_get - get and update size |
| 7813 | * |
| 7814 | * @devlink: devlink |
| 7815 | * @resource_id: the requested resource id |
| 7816 | * @p_resource_size: ptr to update |
| 7817 | */ |
| 7818 | int devlink_resource_size_get(struct devlink *devlink, |
| 7819 | u64 resource_id, |
| 7820 | u64 *p_resource_size) |
| 7821 | { |
| 7822 | struct devlink_resource *resource; |
| 7823 | int err = 0; |
| 7824 | |
| 7825 | mutex_lock(&devlink->lock); |
| 7826 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 7827 | if (!resource) { |
| 7828 | err = -EINVAL; |
| 7829 | goto out; |
| 7830 | } |
| 7831 | *p_resource_size = resource->size_new; |
| 7832 | resource->size = resource->size_new; |
| 7833 | out: |
| 7834 | mutex_unlock(&devlink->lock); |
| 7835 | return err; |
| 7836 | } |
| 7837 | EXPORT_SYMBOL_GPL(devlink_resource_size_get); |
| 7838 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 7839 | /** |
| 7840 | * devlink_dpipe_table_resource_set - set the resource id |
| 7841 | * |
| 7842 | * @devlink: devlink |
| 7843 | * @table_name: table name |
| 7844 | * @resource_id: resource id |
| 7845 | * @resource_units: number of resource's units consumed per table's entry |
| 7846 | */ |
| 7847 | int devlink_dpipe_table_resource_set(struct devlink *devlink, |
| 7848 | const char *table_name, u64 resource_id, |
| 7849 | u64 resource_units) |
| 7850 | { |
| 7851 | struct devlink_dpipe_table *table; |
| 7852 | int err = 0; |
| 7853 | |
| 7854 | mutex_lock(&devlink->lock); |
| 7855 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
Madhuparna Bhowmik | 2eb51c7 | 2020-02-25 17:57:45 +0530 | [diff] [blame] | 7856 | table_name, devlink); |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 7857 | if (!table) { |
| 7858 | err = -EINVAL; |
| 7859 | goto out; |
| 7860 | } |
| 7861 | table->resource_id = resource_id; |
| 7862 | table->resource_units = resource_units; |
| 7863 | table->resource_valid = true; |
| 7864 | out: |
| 7865 | mutex_unlock(&devlink->lock); |
| 7866 | return err; |
| 7867 | } |
| 7868 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set); |
| 7869 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 7870 | /** |
| 7871 | * devlink_resource_occ_get_register - register occupancy getter |
| 7872 | * |
| 7873 | * @devlink: devlink |
| 7874 | * @resource_id: resource id |
| 7875 | * @occ_get: occupancy getter callback |
| 7876 | * @occ_get_priv: occupancy getter callback priv |
| 7877 | */ |
| 7878 | void devlink_resource_occ_get_register(struct devlink *devlink, |
| 7879 | u64 resource_id, |
| 7880 | devlink_resource_occ_get_t *occ_get, |
| 7881 | void *occ_get_priv) |
| 7882 | { |
| 7883 | struct devlink_resource *resource; |
| 7884 | |
| 7885 | mutex_lock(&devlink->lock); |
| 7886 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 7887 | if (WARN_ON(!resource)) |
| 7888 | goto out; |
| 7889 | WARN_ON(resource->occ_get); |
| 7890 | |
| 7891 | resource->occ_get = occ_get; |
| 7892 | resource->occ_get_priv = occ_get_priv; |
| 7893 | out: |
| 7894 | mutex_unlock(&devlink->lock); |
| 7895 | } |
| 7896 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register); |
| 7897 | |
| 7898 | /** |
| 7899 | * devlink_resource_occ_get_unregister - unregister occupancy getter |
| 7900 | * |
| 7901 | * @devlink: devlink |
| 7902 | * @resource_id: resource id |
| 7903 | */ |
| 7904 | void devlink_resource_occ_get_unregister(struct devlink *devlink, |
| 7905 | u64 resource_id) |
| 7906 | { |
| 7907 | struct devlink_resource *resource; |
| 7908 | |
| 7909 | mutex_lock(&devlink->lock); |
| 7910 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 7911 | if (WARN_ON(!resource)) |
| 7912 | goto out; |
| 7913 | WARN_ON(!resource->occ_get); |
| 7914 | |
| 7915 | resource->occ_get = NULL; |
| 7916 | resource->occ_get_priv = NULL; |
| 7917 | out: |
| 7918 | mutex_unlock(&devlink->lock); |
| 7919 | } |
| 7920 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister); |
| 7921 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7922 | static int devlink_param_verify(const struct devlink_param *param) |
| 7923 | { |
| 7924 | if (!param || !param->name || !param->supported_cmodes) |
| 7925 | return -EINVAL; |
| 7926 | if (param->generic) |
| 7927 | return devlink_param_generic_verify(param); |
| 7928 | else |
| 7929 | return devlink_param_driver_verify(param); |
| 7930 | } |
| 7931 | |
| 7932 | static int __devlink_params_register(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7933 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7934 | struct list_head *param_list, |
| 7935 | const struct devlink_param *params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7936 | size_t params_count, |
| 7937 | enum devlink_command reg_cmd, |
| 7938 | enum devlink_command unreg_cmd) |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7939 | { |
| 7940 | const struct devlink_param *param = params; |
| 7941 | int i; |
| 7942 | int err; |
| 7943 | |
| 7944 | mutex_lock(&devlink->lock); |
| 7945 | for (i = 0; i < params_count; i++, param++) { |
| 7946 | err = devlink_param_verify(param); |
| 7947 | if (err) |
| 7948 | goto rollback; |
| 7949 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7950 | err = devlink_param_register_one(devlink, port_index, |
| 7951 | param_list, param, reg_cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7952 | if (err) |
| 7953 | goto rollback; |
| 7954 | } |
| 7955 | |
| 7956 | mutex_unlock(&devlink->lock); |
| 7957 | return 0; |
| 7958 | |
| 7959 | rollback: |
| 7960 | if (!i) |
| 7961 | goto unlock; |
| 7962 | for (param--; i > 0; i--, param--) |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7963 | devlink_param_unregister_one(devlink, port_index, param_list, |
| 7964 | param, unreg_cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7965 | unlock: |
| 7966 | mutex_unlock(&devlink->lock); |
| 7967 | return err; |
| 7968 | } |
| 7969 | |
| 7970 | static void __devlink_params_unregister(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7971 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7972 | struct list_head *param_list, |
| 7973 | const struct devlink_param *params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7974 | size_t params_count, |
| 7975 | enum devlink_command cmd) |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7976 | { |
| 7977 | const struct devlink_param *param = params; |
| 7978 | int i; |
| 7979 | |
| 7980 | mutex_lock(&devlink->lock); |
| 7981 | for (i = 0; i < params_count; i++, param++) |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7982 | devlink_param_unregister_one(devlink, 0, param_list, param, |
| 7983 | cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7984 | mutex_unlock(&devlink->lock); |
| 7985 | } |
| 7986 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 7987 | /** |
| 7988 | * devlink_params_register - register configuration parameters |
| 7989 | * |
| 7990 | * @devlink: devlink |
| 7991 | * @params: configuration parameters array |
| 7992 | * @params_count: number of parameters provided |
| 7993 | * |
| 7994 | * Register the configuration parameters supported by the driver. |
| 7995 | */ |
| 7996 | int devlink_params_register(struct devlink *devlink, |
| 7997 | const struct devlink_param *params, |
| 7998 | size_t params_count) |
| 7999 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8000 | return __devlink_params_register(devlink, 0, &devlink->param_list, |
| 8001 | params, params_count, |
| 8002 | DEVLINK_CMD_PARAM_NEW, |
| 8003 | DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 8004 | } |
| 8005 | EXPORT_SYMBOL_GPL(devlink_params_register); |
| 8006 | |
| 8007 | /** |
| 8008 | * devlink_params_unregister - unregister configuration parameters |
| 8009 | * @devlink: devlink |
| 8010 | * @params: configuration parameters to unregister |
| 8011 | * @params_count: number of parameters provided |
| 8012 | */ |
| 8013 | void devlink_params_unregister(struct devlink *devlink, |
| 8014 | const struct devlink_param *params, |
| 8015 | size_t params_count) |
| 8016 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8017 | return __devlink_params_unregister(devlink, 0, &devlink->param_list, |
| 8018 | params, params_count, |
| 8019 | DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 8020 | } |
| 8021 | EXPORT_SYMBOL_GPL(devlink_params_unregister); |
| 8022 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8023 | /** |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 8024 | * devlink_params_publish - publish configuration parameters |
| 8025 | * |
| 8026 | * @devlink: devlink |
| 8027 | * |
| 8028 | * Publish previously registered configuration parameters. |
| 8029 | */ |
| 8030 | void devlink_params_publish(struct devlink *devlink) |
| 8031 | { |
| 8032 | struct devlink_param_item *param_item; |
| 8033 | |
| 8034 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 8035 | if (param_item->published) |
| 8036 | continue; |
| 8037 | param_item->published = true; |
| 8038 | devlink_param_notify(devlink, 0, param_item, |
| 8039 | DEVLINK_CMD_PARAM_NEW); |
| 8040 | } |
| 8041 | } |
| 8042 | EXPORT_SYMBOL_GPL(devlink_params_publish); |
| 8043 | |
| 8044 | /** |
| 8045 | * devlink_params_unpublish - unpublish configuration parameters |
| 8046 | * |
| 8047 | * @devlink: devlink |
| 8048 | * |
| 8049 | * Unpublish previously registered configuration parameters. |
| 8050 | */ |
| 8051 | void devlink_params_unpublish(struct devlink *devlink) |
| 8052 | { |
| 8053 | struct devlink_param_item *param_item; |
| 8054 | |
| 8055 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 8056 | if (!param_item->published) |
| 8057 | continue; |
| 8058 | param_item->published = false; |
| 8059 | devlink_param_notify(devlink, 0, param_item, |
| 8060 | DEVLINK_CMD_PARAM_DEL); |
| 8061 | } |
| 8062 | } |
| 8063 | EXPORT_SYMBOL_GPL(devlink_params_unpublish); |
| 8064 | |
| 8065 | /** |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8066 | * devlink_port_params_register - register port configuration parameters |
| 8067 | * |
| 8068 | * @devlink_port: devlink port |
| 8069 | * @params: configuration parameters array |
| 8070 | * @params_count: number of parameters provided |
| 8071 | * |
| 8072 | * Register the configuration parameters supported by the port. |
| 8073 | */ |
| 8074 | int devlink_port_params_register(struct devlink_port *devlink_port, |
| 8075 | const struct devlink_param *params, |
| 8076 | size_t params_count) |
| 8077 | { |
| 8078 | return __devlink_params_register(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8079 | devlink_port->index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8080 | &devlink_port->param_list, params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8081 | params_count, |
| 8082 | DEVLINK_CMD_PORT_PARAM_NEW, |
| 8083 | DEVLINK_CMD_PORT_PARAM_DEL); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8084 | } |
| 8085 | EXPORT_SYMBOL_GPL(devlink_port_params_register); |
| 8086 | |
| 8087 | /** |
| 8088 | * devlink_port_params_unregister - unregister port configuration |
| 8089 | * parameters |
| 8090 | * |
| 8091 | * @devlink_port: devlink port |
| 8092 | * @params: configuration parameters array |
| 8093 | * @params_count: number of parameters provided |
| 8094 | */ |
| 8095 | void devlink_port_params_unregister(struct devlink_port *devlink_port, |
| 8096 | const struct devlink_param *params, |
| 8097 | size_t params_count) |
| 8098 | { |
| 8099 | return __devlink_params_unregister(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8100 | devlink_port->index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8101 | &devlink_port->param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8102 | params, params_count, |
| 8103 | DEVLINK_CMD_PORT_PARAM_DEL); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 8104 | } |
| 8105 | EXPORT_SYMBOL_GPL(devlink_port_params_unregister); |
| 8106 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8107 | static int |
| 8108 | __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id, |
| 8109 | union devlink_param_value *init_val) |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8110 | { |
| 8111 | struct devlink_param_item *param_item; |
| 8112 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8113 | param_item = devlink_param_find_by_id(param_list, param_id); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8114 | if (!param_item) |
| 8115 | return -EINVAL; |
| 8116 | |
| 8117 | if (!param_item->driverinit_value_valid || |
| 8118 | !devlink_param_cmode_is_supported(param_item->param, |
| 8119 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 8120 | return -EOPNOTSUPP; |
| 8121 | |
Moshe Shemesh | 1276534 | 2018-10-10 16:09:26 +0300 | [diff] [blame] | 8122 | if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING) |
| 8123 | strcpy(init_val->vstr, param_item->driverinit_value.vstr); |
| 8124 | else |
| 8125 | *init_val = param_item->driverinit_value; |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8126 | |
| 8127 | return 0; |
| 8128 | } |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8129 | |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8130 | static int |
| 8131 | __devlink_param_driverinit_value_set(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8132 | unsigned int port_index, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8133 | struct list_head *param_list, u32 param_id, |
| 8134 | union devlink_param_value init_val, |
| 8135 | enum devlink_command cmd) |
| 8136 | { |
| 8137 | struct devlink_param_item *param_item; |
| 8138 | |
| 8139 | param_item = devlink_param_find_by_id(param_list, param_id); |
| 8140 | if (!param_item) |
| 8141 | return -EINVAL; |
| 8142 | |
| 8143 | if (!devlink_param_cmode_is_supported(param_item->param, |
| 8144 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 8145 | return -EOPNOTSUPP; |
| 8146 | |
| 8147 | if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING) |
| 8148 | strcpy(param_item->driverinit_value.vstr, init_val.vstr); |
| 8149 | else |
| 8150 | param_item->driverinit_value = init_val; |
| 8151 | param_item->driverinit_value_valid = true; |
| 8152 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8153 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8154 | return 0; |
| 8155 | } |
| 8156 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8157 | /** |
| 8158 | * devlink_param_driverinit_value_get - get configuration parameter |
| 8159 | * value for driver initializing |
| 8160 | * |
| 8161 | * @devlink: devlink |
| 8162 | * @param_id: parameter ID |
| 8163 | * @init_val: value of parameter in driverinit configuration mode |
| 8164 | * |
| 8165 | * This function should be used by the driver to get driverinit |
| 8166 | * configuration for initialization after reload command. |
| 8167 | */ |
| 8168 | int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id, |
| 8169 | union devlink_param_value *init_val) |
| 8170 | { |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 8171 | if (!devlink_reload_supported(devlink)) |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8172 | return -EOPNOTSUPP; |
| 8173 | |
| 8174 | return __devlink_param_driverinit_value_get(&devlink->param_list, |
| 8175 | param_id, init_val); |
| 8176 | } |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8177 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get); |
| 8178 | |
| 8179 | /** |
| 8180 | * devlink_param_driverinit_value_set - set value of configuration |
| 8181 | * parameter for driverinit |
| 8182 | * configuration mode |
| 8183 | * |
| 8184 | * @devlink: devlink |
| 8185 | * @param_id: parameter ID |
| 8186 | * @init_val: value of parameter to set for driverinit configuration mode |
| 8187 | * |
| 8188 | * This function should be used by the driver to set driverinit |
| 8189 | * configuration mode default value. |
| 8190 | */ |
| 8191 | int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id, |
| 8192 | union devlink_param_value init_val) |
| 8193 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8194 | return __devlink_param_driverinit_value_set(devlink, 0, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8195 | &devlink->param_list, |
| 8196 | param_id, init_val, |
| 8197 | DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 8198 | } |
| 8199 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set); |
| 8200 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 8201 | /** |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8202 | * devlink_port_param_driverinit_value_get - get configuration parameter |
| 8203 | * value for driver initializing |
| 8204 | * |
| 8205 | * @devlink_port: devlink_port |
| 8206 | * @param_id: parameter ID |
| 8207 | * @init_val: value of parameter in driverinit configuration mode |
| 8208 | * |
| 8209 | * This function should be used by the driver to get driverinit |
| 8210 | * configuration for initialization after reload command. |
| 8211 | */ |
| 8212 | int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port, |
| 8213 | u32 param_id, |
| 8214 | union devlink_param_value *init_val) |
| 8215 | { |
| 8216 | struct devlink *devlink = devlink_port->devlink; |
| 8217 | |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 8218 | if (!devlink_reload_supported(devlink)) |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 8219 | return -EOPNOTSUPP; |
| 8220 | |
| 8221 | return __devlink_param_driverinit_value_get(&devlink_port->param_list, |
| 8222 | param_id, init_val); |
| 8223 | } |
| 8224 | EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get); |
| 8225 | |
| 8226 | /** |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8227 | * devlink_port_param_driverinit_value_set - set value of configuration |
| 8228 | * parameter for driverinit |
| 8229 | * configuration mode |
| 8230 | * |
| 8231 | * @devlink_port: devlink_port |
| 8232 | * @param_id: parameter ID |
| 8233 | * @init_val: value of parameter to set for driverinit configuration mode |
| 8234 | * |
| 8235 | * This function should be used by the driver to set driverinit |
| 8236 | * configuration mode default value. |
| 8237 | */ |
| 8238 | int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port, |
| 8239 | u32 param_id, |
| 8240 | union devlink_param_value init_val) |
| 8241 | { |
| 8242 | return __devlink_param_driverinit_value_set(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8243 | devlink_port->index, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8244 | &devlink_port->param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8245 | param_id, init_val, |
| 8246 | DEVLINK_CMD_PORT_PARAM_NEW); |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 8247 | } |
| 8248 | EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set); |
| 8249 | |
| 8250 | /** |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 8251 | * devlink_param_value_changed - notify devlink on a parameter's value |
| 8252 | * change. Should be called by the driver |
| 8253 | * right after the change. |
| 8254 | * |
| 8255 | * @devlink: devlink |
| 8256 | * @param_id: parameter ID |
| 8257 | * |
| 8258 | * This function should be used by the driver to notify devlink on value |
| 8259 | * change, excluding driverinit configuration mode. |
| 8260 | * For driverinit configuration mode driver should use the function |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 8261 | */ |
| 8262 | void devlink_param_value_changed(struct devlink *devlink, u32 param_id) |
| 8263 | { |
| 8264 | struct devlink_param_item *param_item; |
| 8265 | |
| 8266 | param_item = devlink_param_find_by_id(&devlink->param_list, param_id); |
| 8267 | WARN_ON(!param_item); |
| 8268 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8269 | devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 8270 | } |
| 8271 | EXPORT_SYMBOL_GPL(devlink_param_value_changed); |
| 8272 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8273 | /** |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 8274 | * devlink_port_param_value_changed - notify devlink on a parameter's value |
| 8275 | * change. Should be called by the driver |
| 8276 | * right after the change. |
| 8277 | * |
| 8278 | * @devlink_port: devlink_port |
| 8279 | * @param_id: parameter ID |
| 8280 | * |
| 8281 | * This function should be used by the driver to notify devlink on value |
| 8282 | * change, excluding driverinit configuration mode. |
| 8283 | * For driverinit configuration mode driver should use the function |
| 8284 | * devlink_port_param_driverinit_value_set() instead. |
| 8285 | */ |
| 8286 | void devlink_port_param_value_changed(struct devlink_port *devlink_port, |
| 8287 | u32 param_id) |
| 8288 | { |
| 8289 | struct devlink_param_item *param_item; |
| 8290 | |
| 8291 | param_item = devlink_param_find_by_id(&devlink_port->param_list, |
| 8292 | param_id); |
| 8293 | WARN_ON(!param_item); |
| 8294 | |
| 8295 | devlink_param_notify(devlink_port->devlink, devlink_port->index, |
| 8296 | param_item, DEVLINK_CMD_PORT_PARAM_NEW); |
| 8297 | } |
| 8298 | EXPORT_SYMBOL_GPL(devlink_port_param_value_changed); |
| 8299 | |
| 8300 | /** |
Moshe Shemesh | bde74ad1 | 2018-10-10 16:09:27 +0300 | [diff] [blame] | 8301 | * devlink_param_value_str_fill - Safely fill-up the string preventing |
| 8302 | * from overflow of the preallocated buffer |
| 8303 | * |
| 8304 | * @dst_val: destination devlink_param_value |
| 8305 | * @src: source buffer |
| 8306 | */ |
| 8307 | void devlink_param_value_str_fill(union devlink_param_value *dst_val, |
| 8308 | const char *src) |
| 8309 | { |
| 8310 | size_t len; |
| 8311 | |
| 8312 | len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE); |
| 8313 | WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE); |
| 8314 | } |
| 8315 | EXPORT_SYMBOL_GPL(devlink_param_value_str_fill); |
| 8316 | |
| 8317 | /** |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8318 | * devlink_region_create - create a new address region |
| 8319 | * |
| 8320 | * @devlink: devlink |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 8321 | * @ops: region operations and name |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8322 | * @region_max_snapshots: Maximum supported number of snapshots for region |
| 8323 | * @region_size: size of region |
| 8324 | */ |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 8325 | struct devlink_region * |
| 8326 | devlink_region_create(struct devlink *devlink, |
| 8327 | const struct devlink_region_ops *ops, |
| 8328 | u32 region_max_snapshots, u64 region_size) |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8329 | { |
| 8330 | struct devlink_region *region; |
| 8331 | int err = 0; |
| 8332 | |
Jacob Keller | a0a09f6 | 2020-03-26 11:37:09 -0700 | [diff] [blame] | 8333 | if (WARN_ON(!ops) || WARN_ON(!ops->destructor)) |
| 8334 | return ERR_PTR(-EINVAL); |
| 8335 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8336 | mutex_lock(&devlink->lock); |
| 8337 | |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 8338 | if (devlink_region_get_by_name(devlink, ops->name)) { |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8339 | err = -EEXIST; |
| 8340 | goto unlock; |
| 8341 | } |
| 8342 | |
| 8343 | region = kzalloc(sizeof(*region), GFP_KERNEL); |
| 8344 | if (!region) { |
| 8345 | err = -ENOMEM; |
| 8346 | goto unlock; |
| 8347 | } |
| 8348 | |
| 8349 | region->devlink = devlink; |
| 8350 | region->max_snapshots = region_max_snapshots; |
Jacob Keller | e893768 | 2020-03-26 11:37:08 -0700 | [diff] [blame] | 8351 | region->ops = ops; |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8352 | region->size = region_size; |
| 8353 | INIT_LIST_HEAD(®ion->snapshot_list); |
| 8354 | list_add_tail(®ion->list, &devlink->region_list); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 8355 | devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8356 | |
| 8357 | mutex_unlock(&devlink->lock); |
| 8358 | return region; |
| 8359 | |
| 8360 | unlock: |
| 8361 | mutex_unlock(&devlink->lock); |
| 8362 | return ERR_PTR(err); |
| 8363 | } |
| 8364 | EXPORT_SYMBOL_GPL(devlink_region_create); |
| 8365 | |
| 8366 | /** |
| 8367 | * devlink_region_destroy - destroy address region |
| 8368 | * |
| 8369 | * @region: devlink region to destroy |
| 8370 | */ |
| 8371 | void devlink_region_destroy(struct devlink_region *region) |
| 8372 | { |
| 8373 | struct devlink *devlink = region->devlink; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8374 | struct devlink_snapshot *snapshot, *ts; |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8375 | |
| 8376 | mutex_lock(&devlink->lock); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8377 | |
| 8378 | /* Free all snapshots of region */ |
| 8379 | list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list) |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 8380 | devlink_region_snapshot_del(region, snapshot); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8381 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8382 | list_del(®ion->list); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 8383 | |
| 8384 | devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 8385 | mutex_unlock(&devlink->lock); |
| 8386 | kfree(region); |
| 8387 | } |
| 8388 | EXPORT_SYMBOL_GPL(devlink_region_destroy); |
| 8389 | |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8390 | /** |
Jacob Keller | b0efcae | 2020-01-09 11:08:20 -0800 | [diff] [blame] | 8391 | * devlink_region_snapshot_id_get - get snapshot ID |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8392 | * |
| 8393 | * This callback should be called when adding a new snapshot, |
| 8394 | * Driver should use the same id for multiple snapshots taken |
| 8395 | * on multiple regions at the same time/by the same trigger. |
| 8396 | * |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 8397 | * The caller of this function must use devlink_region_snapshot_id_put |
| 8398 | * when finished creating regions using this id. |
| 8399 | * |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8400 | * Returns zero on success, or a negative error code on failure. |
| 8401 | * |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8402 | * @devlink: devlink |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8403 | * @id: storage to return id |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8404 | */ |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8405 | int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id) |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8406 | { |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8407 | int err; |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8408 | |
| 8409 | mutex_lock(&devlink->lock); |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8410 | err = __devlink_region_snapshot_id_get(devlink, id); |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8411 | mutex_unlock(&devlink->lock); |
| 8412 | |
Jacob Keller | 7ef19d3 | 2020-03-26 11:37:14 -0700 | [diff] [blame] | 8413 | return err; |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8414 | } |
Jacob Keller | b0efcae | 2020-01-09 11:08:20 -0800 | [diff] [blame] | 8415 | EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get); |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 8416 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8417 | /** |
Jacob Keller | 1210243 | 2020-03-26 11:37:15 -0700 | [diff] [blame] | 8418 | * devlink_region_snapshot_id_put - put snapshot ID reference |
| 8419 | * |
| 8420 | * This should be called by a driver after finishing creating snapshots |
| 8421 | * with an id. Doing so ensures that the ID can later be released in the |
| 8422 | * event that all snapshots using it have been destroyed. |
| 8423 | * |
| 8424 | * @devlink: devlink |
| 8425 | * @id: id to release reference on |
| 8426 | */ |
| 8427 | void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id) |
| 8428 | { |
| 8429 | mutex_lock(&devlink->lock); |
| 8430 | __devlink_snapshot_id_decrement(devlink, id); |
| 8431 | mutex_unlock(&devlink->lock); |
| 8432 | } |
| 8433 | EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put); |
| 8434 | |
| 8435 | /** |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8436 | * devlink_region_snapshot_create - create a new snapshot |
| 8437 | * This will add a new snapshot of a region. The snapshot |
| 8438 | * will be stored on the region struct and can be accessed |
Jacob Keller | 6d82f67 | 2020-03-26 11:37:10 -0700 | [diff] [blame] | 8439 | * from devlink. This is useful for future analyses of snapshots. |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8440 | * Multiple snapshots can be created on a region. |
| 8441 | * The @snapshot_id should be obtained using the getter function. |
| 8442 | * |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 8443 | * @region: devlink region of the snapshot |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8444 | * @data: snapshot data |
| 8445 | * @snapshot_id: snapshot id to be created |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8446 | */ |
Jiri Pirko | 3a5e523 | 2019-08-09 15:27:15 +0200 | [diff] [blame] | 8447 | int devlink_region_snapshot_create(struct devlink_region *region, |
Jacob Keller | a0a09f6 | 2020-03-26 11:37:09 -0700 | [diff] [blame] | 8448 | u8 *data, u32 snapshot_id) |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8449 | { |
| 8450 | struct devlink *devlink = region->devlink; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8451 | int err; |
| 8452 | |
| 8453 | mutex_lock(&devlink->lock); |
Jacob Keller | cf80fae | 2020-03-26 11:37:11 -0700 | [diff] [blame] | 8454 | err = __devlink_region_snapshot_create(region, data, snapshot_id); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8455 | mutex_unlock(&devlink->lock); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8456 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 8457 | return err; |
| 8458 | } |
| 8459 | EXPORT_SYMBOL_GPL(devlink_region_snapshot_create); |
| 8460 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8461 | #define DEVLINK_TRAP(_id, _type) \ |
| 8462 | { \ |
| 8463 | .type = DEVLINK_TRAP_TYPE_##_type, \ |
| 8464 | .id = DEVLINK_TRAP_GENERIC_ID_##_id, \ |
| 8465 | .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \ |
| 8466 | } |
| 8467 | |
| 8468 | static const struct devlink_trap devlink_trap_generic[] = { |
Ido Schimmel | 391203a | 2019-08-17 16:28:18 +0300 | [diff] [blame] | 8469 | DEVLINK_TRAP(SMAC_MC, DROP), |
| 8470 | DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP), |
| 8471 | DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP), |
| 8472 | DEVLINK_TRAP(INGRESS_STP_FILTER, DROP), |
| 8473 | DEVLINK_TRAP(EMPTY_TX_LIST, DROP), |
| 8474 | DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP), |
| 8475 | DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP), |
| 8476 | DEVLINK_TRAP(TTL_ERROR, EXCEPTION), |
| 8477 | DEVLINK_TRAP(TAIL_DROP, DROP), |
Amit Cohen | 6896cc4 | 2019-11-07 18:42:09 +0200 | [diff] [blame] | 8478 | DEVLINK_TRAP(NON_IP_PACKET, DROP), |
| 8479 | DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP), |
| 8480 | DEVLINK_TRAP(DIP_LB, DROP), |
| 8481 | DEVLINK_TRAP(SIP_MC, DROP), |
| 8482 | DEVLINK_TRAP(SIP_LB, DROP), |
| 8483 | DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP), |
| 8484 | DEVLINK_TRAP(IPV4_SIP_BC, DROP), |
| 8485 | DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP), |
| 8486 | DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP), |
Amit Cohen | 3b063ae | 2019-11-07 18:42:14 +0200 | [diff] [blame] | 8487 | DEVLINK_TRAP(MTU_ERROR, EXCEPTION), |
| 8488 | DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION), |
| 8489 | DEVLINK_TRAP(RPF, EXCEPTION), |
| 8490 | DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION), |
| 8491 | DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION), |
| 8492 | DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION), |
Amit Cohen | 95f0ead | 2020-01-19 15:00:48 +0200 | [diff] [blame] | 8493 | DEVLINK_TRAP(NON_ROUTABLE, DROP), |
Amit Cohen | 13c056e | 2020-01-19 15:00:54 +0200 | [diff] [blame] | 8494 | DEVLINK_TRAP(DECAP_ERROR, EXCEPTION), |
Amit Cohen | c3cae49 | 2020-01-19 15:00:58 +0200 | [diff] [blame] | 8495 | DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP), |
Jiri Pirko | ecd942a | 2020-02-24 08:35:47 +0100 | [diff] [blame] | 8496 | DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP), |
| 8497 | DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP), |
Ido Schimmel | 515eac6 | 2020-05-29 21:36:41 +0300 | [diff] [blame^] | 8498 | DEVLINK_TRAP(STP, CONTROL), |
| 8499 | DEVLINK_TRAP(LACP, CONTROL), |
| 8500 | DEVLINK_TRAP(LLDP, CONTROL), |
| 8501 | DEVLINK_TRAP(IGMP_QUERY, CONTROL), |
| 8502 | DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL), |
| 8503 | DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL), |
| 8504 | DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL), |
| 8505 | DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL), |
| 8506 | DEVLINK_TRAP(MLD_QUERY, CONTROL), |
| 8507 | DEVLINK_TRAP(MLD_V1_REPORT, CONTROL), |
| 8508 | DEVLINK_TRAP(MLD_V2_REPORT, CONTROL), |
| 8509 | DEVLINK_TRAP(MLD_V1_DONE, CONTROL), |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8510 | }; |
| 8511 | |
| 8512 | #define DEVLINK_TRAP_GROUP(_id) \ |
| 8513 | { \ |
| 8514 | .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \ |
| 8515 | .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \ |
| 8516 | } |
| 8517 | |
| 8518 | static const struct devlink_trap_group devlink_trap_group_generic[] = { |
Ido Schimmel | 391203a | 2019-08-17 16:28:18 +0300 | [diff] [blame] | 8519 | DEVLINK_TRAP_GROUP(L2_DROPS), |
| 8520 | DEVLINK_TRAP_GROUP(L3_DROPS), |
Ido Schimmel | 678eb19 | 2020-05-29 21:36:36 +0300 | [diff] [blame] | 8521 | DEVLINK_TRAP_GROUP(L3_EXCEPTIONS), |
Ido Schimmel | 391203a | 2019-08-17 16:28:18 +0300 | [diff] [blame] | 8522 | DEVLINK_TRAP_GROUP(BUFFER_DROPS), |
Amit Cohen | 13c056e | 2020-01-19 15:00:54 +0200 | [diff] [blame] | 8523 | DEVLINK_TRAP_GROUP(TUNNEL_DROPS), |
Jiri Pirko | ecd942a | 2020-02-24 08:35:47 +0100 | [diff] [blame] | 8524 | DEVLINK_TRAP_GROUP(ACL_DROPS), |
Ido Schimmel | 515eac6 | 2020-05-29 21:36:41 +0300 | [diff] [blame^] | 8525 | DEVLINK_TRAP_GROUP(STP), |
| 8526 | DEVLINK_TRAP_GROUP(LACP), |
| 8527 | DEVLINK_TRAP_GROUP(LLDP), |
| 8528 | DEVLINK_TRAP_GROUP(MC_SNOOPING), |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8529 | }; |
| 8530 | |
| 8531 | static int devlink_trap_generic_verify(const struct devlink_trap *trap) |
| 8532 | { |
| 8533 | if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX) |
| 8534 | return -EINVAL; |
| 8535 | |
| 8536 | if (strcmp(trap->name, devlink_trap_generic[trap->id].name)) |
| 8537 | return -EINVAL; |
| 8538 | |
| 8539 | if (trap->type != devlink_trap_generic[trap->id].type) |
| 8540 | return -EINVAL; |
| 8541 | |
| 8542 | return 0; |
| 8543 | } |
| 8544 | |
| 8545 | static int devlink_trap_driver_verify(const struct devlink_trap *trap) |
| 8546 | { |
| 8547 | int i; |
| 8548 | |
| 8549 | if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX) |
| 8550 | return -EINVAL; |
| 8551 | |
| 8552 | for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) { |
| 8553 | if (!strcmp(trap->name, devlink_trap_generic[i].name)) |
| 8554 | return -EEXIST; |
| 8555 | } |
| 8556 | |
| 8557 | return 0; |
| 8558 | } |
| 8559 | |
| 8560 | static int devlink_trap_verify(const struct devlink_trap *trap) |
| 8561 | { |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 8562 | if (!trap || !trap->name) |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8563 | return -EINVAL; |
| 8564 | |
| 8565 | if (trap->generic) |
| 8566 | return devlink_trap_generic_verify(trap); |
| 8567 | else |
| 8568 | return devlink_trap_driver_verify(trap); |
| 8569 | } |
| 8570 | |
| 8571 | static int |
| 8572 | devlink_trap_group_generic_verify(const struct devlink_trap_group *group) |
| 8573 | { |
| 8574 | if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX) |
| 8575 | return -EINVAL; |
| 8576 | |
| 8577 | if (strcmp(group->name, devlink_trap_group_generic[group->id].name)) |
| 8578 | return -EINVAL; |
| 8579 | |
| 8580 | return 0; |
| 8581 | } |
| 8582 | |
| 8583 | static int |
| 8584 | devlink_trap_group_driver_verify(const struct devlink_trap_group *group) |
| 8585 | { |
| 8586 | int i; |
| 8587 | |
| 8588 | if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX) |
| 8589 | return -EINVAL; |
| 8590 | |
| 8591 | for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) { |
| 8592 | if (!strcmp(group->name, devlink_trap_group_generic[i].name)) |
| 8593 | return -EEXIST; |
| 8594 | } |
| 8595 | |
| 8596 | return 0; |
| 8597 | } |
| 8598 | |
| 8599 | static int devlink_trap_group_verify(const struct devlink_trap_group *group) |
| 8600 | { |
| 8601 | if (group->generic) |
| 8602 | return devlink_trap_group_generic_verify(group); |
| 8603 | else |
| 8604 | return devlink_trap_group_driver_verify(group); |
| 8605 | } |
| 8606 | |
| 8607 | static void |
| 8608 | devlink_trap_group_notify(struct devlink *devlink, |
| 8609 | const struct devlink_trap_group_item *group_item, |
| 8610 | enum devlink_command cmd) |
| 8611 | { |
| 8612 | struct sk_buff *msg; |
| 8613 | int err; |
| 8614 | |
| 8615 | WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW && |
| 8616 | cmd != DEVLINK_CMD_TRAP_GROUP_DEL); |
| 8617 | |
| 8618 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 8619 | if (!msg) |
| 8620 | return; |
| 8621 | |
| 8622 | err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0, |
| 8623 | 0); |
| 8624 | if (err) { |
| 8625 | nlmsg_free(msg); |
| 8626 | return; |
| 8627 | } |
| 8628 | |
| 8629 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 8630 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 8631 | } |
| 8632 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8633 | static int |
| 8634 | devlink_trap_item_group_link(struct devlink *devlink, |
| 8635 | struct devlink_trap_item *trap_item) |
| 8636 | { |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 8637 | u16 group_id = trap_item->trap->init_group_id; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8638 | struct devlink_trap_group_item *group_item; |
| 8639 | |
Ido Schimmel | 107f167 | 2020-03-22 20:48:30 +0200 | [diff] [blame] | 8640 | group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id); |
Ido Schimmel | a09b37f | 2020-03-22 20:48:29 +0200 | [diff] [blame] | 8641 | if (WARN_ON_ONCE(!group_item)) |
| 8642 | return -EINVAL; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8643 | |
| 8644 | trap_item->group_item = group_item; |
| 8645 | |
| 8646 | return 0; |
| 8647 | } |
| 8648 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8649 | static void devlink_trap_notify(struct devlink *devlink, |
| 8650 | const struct devlink_trap_item *trap_item, |
| 8651 | enum devlink_command cmd) |
| 8652 | { |
| 8653 | struct sk_buff *msg; |
| 8654 | int err; |
| 8655 | |
| 8656 | WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW && |
| 8657 | cmd != DEVLINK_CMD_TRAP_DEL); |
| 8658 | |
| 8659 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 8660 | if (!msg) |
| 8661 | return; |
| 8662 | |
| 8663 | err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0); |
| 8664 | if (err) { |
| 8665 | nlmsg_free(msg); |
| 8666 | return; |
| 8667 | } |
| 8668 | |
| 8669 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 8670 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 8671 | } |
| 8672 | |
| 8673 | static int |
| 8674 | devlink_trap_register(struct devlink *devlink, |
| 8675 | const struct devlink_trap *trap, void *priv) |
| 8676 | { |
| 8677 | struct devlink_trap_item *trap_item; |
| 8678 | int err; |
| 8679 | |
| 8680 | if (devlink_trap_item_lookup(devlink, trap->name)) |
| 8681 | return -EEXIST; |
| 8682 | |
| 8683 | trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL); |
| 8684 | if (!trap_item) |
| 8685 | return -ENOMEM; |
| 8686 | |
| 8687 | trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats); |
| 8688 | if (!trap_item->stats) { |
| 8689 | err = -ENOMEM; |
| 8690 | goto err_stats_alloc; |
| 8691 | } |
| 8692 | |
| 8693 | trap_item->trap = trap; |
| 8694 | trap_item->action = trap->init_action; |
| 8695 | trap_item->priv = priv; |
| 8696 | |
| 8697 | err = devlink_trap_item_group_link(devlink, trap_item); |
| 8698 | if (err) |
| 8699 | goto err_group_link; |
| 8700 | |
| 8701 | err = devlink->ops->trap_init(devlink, trap, trap_item); |
| 8702 | if (err) |
| 8703 | goto err_trap_init; |
| 8704 | |
| 8705 | list_add_tail(&trap_item->list, &devlink->trap_list); |
| 8706 | devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW); |
| 8707 | |
| 8708 | return 0; |
| 8709 | |
| 8710 | err_trap_init: |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8711 | err_group_link: |
| 8712 | free_percpu(trap_item->stats); |
| 8713 | err_stats_alloc: |
| 8714 | kfree(trap_item); |
| 8715 | return err; |
| 8716 | } |
| 8717 | |
| 8718 | static void devlink_trap_unregister(struct devlink *devlink, |
| 8719 | const struct devlink_trap *trap) |
| 8720 | { |
| 8721 | struct devlink_trap_item *trap_item; |
| 8722 | |
| 8723 | trap_item = devlink_trap_item_lookup(devlink, trap->name); |
| 8724 | if (WARN_ON_ONCE(!trap_item)) |
| 8725 | return; |
| 8726 | |
| 8727 | devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL); |
| 8728 | list_del(&trap_item->list); |
| 8729 | if (devlink->ops->trap_fini) |
| 8730 | devlink->ops->trap_fini(devlink, trap, trap_item); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8731 | free_percpu(trap_item->stats); |
| 8732 | kfree(trap_item); |
| 8733 | } |
| 8734 | |
| 8735 | static void devlink_trap_disable(struct devlink *devlink, |
| 8736 | const struct devlink_trap *trap) |
| 8737 | { |
| 8738 | struct devlink_trap_item *trap_item; |
| 8739 | |
| 8740 | trap_item = devlink_trap_item_lookup(devlink, trap->name); |
| 8741 | if (WARN_ON_ONCE(!trap_item)) |
| 8742 | return; |
| 8743 | |
| 8744 | devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP); |
| 8745 | trap_item->action = DEVLINK_TRAP_ACTION_DROP; |
| 8746 | } |
| 8747 | |
| 8748 | /** |
| 8749 | * devlink_traps_register - Register packet traps with devlink. |
| 8750 | * @devlink: devlink. |
| 8751 | * @traps: Packet traps. |
| 8752 | * @traps_count: Count of provided packet traps. |
| 8753 | * @priv: Driver private information. |
| 8754 | * |
| 8755 | * Return: Non-zero value on failure. |
| 8756 | */ |
| 8757 | int devlink_traps_register(struct devlink *devlink, |
| 8758 | const struct devlink_trap *traps, |
| 8759 | size_t traps_count, void *priv) |
| 8760 | { |
| 8761 | int i, err; |
| 8762 | |
| 8763 | if (!devlink->ops->trap_init || !devlink->ops->trap_action_set) |
| 8764 | return -EINVAL; |
| 8765 | |
| 8766 | mutex_lock(&devlink->lock); |
| 8767 | for (i = 0; i < traps_count; i++) { |
| 8768 | const struct devlink_trap *trap = &traps[i]; |
| 8769 | |
| 8770 | err = devlink_trap_verify(trap); |
| 8771 | if (err) |
| 8772 | goto err_trap_verify; |
| 8773 | |
| 8774 | err = devlink_trap_register(devlink, trap, priv); |
| 8775 | if (err) |
| 8776 | goto err_trap_register; |
| 8777 | } |
| 8778 | mutex_unlock(&devlink->lock); |
| 8779 | |
| 8780 | return 0; |
| 8781 | |
| 8782 | err_trap_register: |
| 8783 | err_trap_verify: |
| 8784 | for (i--; i >= 0; i--) |
| 8785 | devlink_trap_unregister(devlink, &traps[i]); |
| 8786 | mutex_unlock(&devlink->lock); |
| 8787 | return err; |
| 8788 | } |
| 8789 | EXPORT_SYMBOL_GPL(devlink_traps_register); |
| 8790 | |
| 8791 | /** |
| 8792 | * devlink_traps_unregister - Unregister packet traps from devlink. |
| 8793 | * @devlink: devlink. |
| 8794 | * @traps: Packet traps. |
| 8795 | * @traps_count: Count of provided packet traps. |
| 8796 | */ |
| 8797 | void devlink_traps_unregister(struct devlink *devlink, |
| 8798 | const struct devlink_trap *traps, |
| 8799 | size_t traps_count) |
| 8800 | { |
| 8801 | int i; |
| 8802 | |
| 8803 | mutex_lock(&devlink->lock); |
| 8804 | /* Make sure we do not have any packets in-flight while unregistering |
| 8805 | * traps by disabling all of them and waiting for a grace period. |
| 8806 | */ |
| 8807 | for (i = traps_count - 1; i >= 0; i--) |
| 8808 | devlink_trap_disable(devlink, &traps[i]); |
| 8809 | synchronize_rcu(); |
| 8810 | for (i = traps_count - 1; i >= 0; i--) |
| 8811 | devlink_trap_unregister(devlink, &traps[i]); |
| 8812 | mutex_unlock(&devlink->lock); |
| 8813 | } |
| 8814 | EXPORT_SYMBOL_GPL(devlink_traps_unregister); |
| 8815 | |
| 8816 | static void |
| 8817 | devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats, |
| 8818 | size_t skb_len) |
| 8819 | { |
| 8820 | struct devlink_stats *stats; |
| 8821 | |
| 8822 | stats = this_cpu_ptr(trap_stats); |
| 8823 | u64_stats_update_begin(&stats->syncp); |
| 8824 | stats->rx_bytes += skb_len; |
| 8825 | stats->rx_packets++; |
| 8826 | u64_stats_update_end(&stats->syncp); |
| 8827 | } |
| 8828 | |
| 8829 | static void |
| 8830 | devlink_trap_report_metadata_fill(struct net_dm_hw_metadata *hw_metadata, |
| 8831 | const struct devlink_trap_item *trap_item, |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 8832 | struct devlink_port *in_devlink_port, |
| 8833 | const struct flow_action_cookie *fa_cookie) |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8834 | { |
| 8835 | struct devlink_trap_group_item *group_item = trap_item->group_item; |
| 8836 | |
| 8837 | hw_metadata->trap_group_name = group_item->group->name; |
| 8838 | hw_metadata->trap_name = trap_item->trap->name; |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 8839 | hw_metadata->fa_cookie = fa_cookie; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8840 | |
| 8841 | spin_lock(&in_devlink_port->type_lock); |
| 8842 | if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH) |
| 8843 | hw_metadata->input_dev = in_devlink_port->type_dev; |
| 8844 | spin_unlock(&in_devlink_port->type_lock); |
| 8845 | } |
| 8846 | |
| 8847 | /** |
| 8848 | * devlink_trap_report - Report trapped packet to drop monitor. |
| 8849 | * @devlink: devlink. |
| 8850 | * @skb: Trapped packet. |
| 8851 | * @trap_ctx: Trap context. |
| 8852 | * @in_devlink_port: Input devlink port. |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 8853 | * @fa_cookie: Flow action cookie. Could be NULL. |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8854 | */ |
| 8855 | void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb, |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 8856 | void *trap_ctx, struct devlink_port *in_devlink_port, |
| 8857 | const struct flow_action_cookie *fa_cookie) |
| 8858 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8859 | { |
| 8860 | struct devlink_trap_item *trap_item = trap_ctx; |
| 8861 | struct net_dm_hw_metadata hw_metadata = {}; |
| 8862 | |
| 8863 | devlink_trap_stats_update(trap_item->stats, skb->len); |
| 8864 | devlink_trap_stats_update(trap_item->group_item->stats, skb->len); |
| 8865 | |
Ido Schimmel | 30a4e9a | 2020-05-29 21:36:40 +0300 | [diff] [blame] | 8866 | /* Control packets were not dropped by the device or encountered an |
| 8867 | * exception during forwarding and therefore should not be reported to |
| 8868 | * the kernel's drop monitor. |
| 8869 | */ |
| 8870 | if (trap_item->trap->type == DEVLINK_TRAP_TYPE_CONTROL) |
| 8871 | return; |
| 8872 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8873 | devlink_trap_report_metadata_fill(&hw_metadata, trap_item, |
Jiri Pirko | 5a2e106 | 2020-02-25 11:45:21 +0100 | [diff] [blame] | 8874 | in_devlink_port, fa_cookie); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 8875 | net_dm_hw_report(skb, &hw_metadata); |
| 8876 | } |
| 8877 | EXPORT_SYMBOL_GPL(devlink_trap_report); |
| 8878 | |
| 8879 | /** |
| 8880 | * devlink_trap_ctx_priv - Trap context to driver private information. |
| 8881 | * @trap_ctx: Trap context. |
| 8882 | * |
| 8883 | * Return: Driver private information passed during registration. |
| 8884 | */ |
| 8885 | void *devlink_trap_ctx_priv(void *trap_ctx) |
| 8886 | { |
| 8887 | struct devlink_trap_item *trap_item = trap_ctx; |
| 8888 | |
| 8889 | return trap_item->priv; |
| 8890 | } |
| 8891 | EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv); |
| 8892 | |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 8893 | static int |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 8894 | devlink_trap_group_item_policer_link(struct devlink *devlink, |
| 8895 | struct devlink_trap_group_item *group_item) |
| 8896 | { |
| 8897 | u32 policer_id = group_item->group->init_policer_id; |
| 8898 | struct devlink_trap_policer_item *policer_item; |
| 8899 | |
| 8900 | if (policer_id == 0) |
| 8901 | return 0; |
| 8902 | |
| 8903 | policer_item = devlink_trap_policer_item_lookup(devlink, policer_id); |
| 8904 | if (WARN_ON_ONCE(!policer_item)) |
| 8905 | return -EINVAL; |
| 8906 | |
| 8907 | group_item->policer_item = policer_item; |
| 8908 | |
| 8909 | return 0; |
| 8910 | } |
| 8911 | |
| 8912 | static int |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 8913 | devlink_trap_group_register(struct devlink *devlink, |
| 8914 | const struct devlink_trap_group *group) |
| 8915 | { |
| 8916 | struct devlink_trap_group_item *group_item; |
| 8917 | int err; |
| 8918 | |
| 8919 | if (devlink_trap_group_item_lookup(devlink, group->name)) |
| 8920 | return -EEXIST; |
| 8921 | |
| 8922 | group_item = kzalloc(sizeof(*group_item), GFP_KERNEL); |
| 8923 | if (!group_item) |
| 8924 | return -ENOMEM; |
| 8925 | |
| 8926 | group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats); |
| 8927 | if (!group_item->stats) { |
| 8928 | err = -ENOMEM; |
| 8929 | goto err_stats_alloc; |
| 8930 | } |
| 8931 | |
| 8932 | group_item->group = group; |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 8933 | |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 8934 | err = devlink_trap_group_item_policer_link(devlink, group_item); |
| 8935 | if (err) |
| 8936 | goto err_policer_link; |
| 8937 | |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 8938 | if (devlink->ops->trap_group_init) { |
| 8939 | err = devlink->ops->trap_group_init(devlink, group); |
| 8940 | if (err) |
| 8941 | goto err_group_init; |
| 8942 | } |
| 8943 | |
| 8944 | list_add_tail(&group_item->list, &devlink->trap_group_list); |
| 8945 | devlink_trap_group_notify(devlink, group_item, |
| 8946 | DEVLINK_CMD_TRAP_GROUP_NEW); |
| 8947 | |
| 8948 | return 0; |
| 8949 | |
| 8950 | err_group_init: |
Ido Schimmel | f9f5439 | 2020-03-30 22:38:21 +0300 | [diff] [blame] | 8951 | err_policer_link: |
Ido Schimmel | 95ad955 | 2020-03-22 20:48:26 +0200 | [diff] [blame] | 8952 | free_percpu(group_item->stats); |
| 8953 | err_stats_alloc: |
| 8954 | kfree(group_item); |
| 8955 | return err; |
| 8956 | } |
| 8957 | |
| 8958 | static void |
| 8959 | devlink_trap_group_unregister(struct devlink *devlink, |
| 8960 | const struct devlink_trap_group *group) |
| 8961 | { |
| 8962 | struct devlink_trap_group_item *group_item; |
| 8963 | |
| 8964 | group_item = devlink_trap_group_item_lookup(devlink, group->name); |
| 8965 | if (WARN_ON_ONCE(!group_item)) |
| 8966 | return; |
| 8967 | |
| 8968 | devlink_trap_group_notify(devlink, group_item, |
| 8969 | DEVLINK_CMD_TRAP_GROUP_DEL); |
| 8970 | list_del(&group_item->list); |
| 8971 | free_percpu(group_item->stats); |
| 8972 | kfree(group_item); |
| 8973 | } |
| 8974 | |
| 8975 | /** |
| 8976 | * devlink_trap_groups_register - Register packet trap groups with devlink. |
| 8977 | * @devlink: devlink. |
| 8978 | * @groups: Packet trap groups. |
| 8979 | * @groups_count: Count of provided packet trap groups. |
| 8980 | * |
| 8981 | * Return: Non-zero value on failure. |
| 8982 | */ |
| 8983 | int devlink_trap_groups_register(struct devlink *devlink, |
| 8984 | const struct devlink_trap_group *groups, |
| 8985 | size_t groups_count) |
| 8986 | { |
| 8987 | int i, err; |
| 8988 | |
| 8989 | mutex_lock(&devlink->lock); |
| 8990 | for (i = 0; i < groups_count; i++) { |
| 8991 | const struct devlink_trap_group *group = &groups[i]; |
| 8992 | |
| 8993 | err = devlink_trap_group_verify(group); |
| 8994 | if (err) |
| 8995 | goto err_trap_group_verify; |
| 8996 | |
| 8997 | err = devlink_trap_group_register(devlink, group); |
| 8998 | if (err) |
| 8999 | goto err_trap_group_register; |
| 9000 | } |
| 9001 | mutex_unlock(&devlink->lock); |
| 9002 | |
| 9003 | return 0; |
| 9004 | |
| 9005 | err_trap_group_register: |
| 9006 | err_trap_group_verify: |
| 9007 | for (i--; i >= 0; i--) |
| 9008 | devlink_trap_group_unregister(devlink, &groups[i]); |
| 9009 | mutex_unlock(&devlink->lock); |
| 9010 | return err; |
| 9011 | } |
| 9012 | EXPORT_SYMBOL_GPL(devlink_trap_groups_register); |
| 9013 | |
| 9014 | /** |
| 9015 | * devlink_trap_groups_unregister - Unregister packet trap groups from devlink. |
| 9016 | * @devlink: devlink. |
| 9017 | * @groups: Packet trap groups. |
| 9018 | * @groups_count: Count of provided packet trap groups. |
| 9019 | */ |
| 9020 | void devlink_trap_groups_unregister(struct devlink *devlink, |
| 9021 | const struct devlink_trap_group *groups, |
| 9022 | size_t groups_count) |
| 9023 | { |
| 9024 | int i; |
| 9025 | |
| 9026 | mutex_lock(&devlink->lock); |
| 9027 | for (i = groups_count - 1; i >= 0; i--) |
| 9028 | devlink_trap_group_unregister(devlink, &groups[i]); |
| 9029 | mutex_unlock(&devlink->lock); |
| 9030 | } |
| 9031 | EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister); |
| 9032 | |
Ido Schimmel | 1e8c661 | 2020-03-30 22:38:18 +0300 | [diff] [blame] | 9033 | static void |
| 9034 | devlink_trap_policer_notify(struct devlink *devlink, |
| 9035 | const struct devlink_trap_policer_item *policer_item, |
| 9036 | enum devlink_command cmd) |
| 9037 | { |
| 9038 | struct sk_buff *msg; |
| 9039 | int err; |
| 9040 | |
| 9041 | WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW && |
| 9042 | cmd != DEVLINK_CMD_TRAP_POLICER_DEL); |
| 9043 | |
| 9044 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 9045 | if (!msg) |
| 9046 | return; |
| 9047 | |
| 9048 | err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0, |
| 9049 | 0, 0); |
| 9050 | if (err) { |
| 9051 | nlmsg_free(msg); |
| 9052 | return; |
| 9053 | } |
| 9054 | |
| 9055 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 9056 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 9057 | } |
| 9058 | |
| 9059 | static int |
| 9060 | devlink_trap_policer_register(struct devlink *devlink, |
| 9061 | const struct devlink_trap_policer *policer) |
| 9062 | { |
| 9063 | struct devlink_trap_policer_item *policer_item; |
| 9064 | int err; |
| 9065 | |
| 9066 | if (devlink_trap_policer_item_lookup(devlink, policer->id)) |
| 9067 | return -EEXIST; |
| 9068 | |
| 9069 | policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL); |
| 9070 | if (!policer_item) |
| 9071 | return -ENOMEM; |
| 9072 | |
| 9073 | policer_item->policer = policer; |
| 9074 | policer_item->rate = policer->init_rate; |
| 9075 | policer_item->burst = policer->init_burst; |
| 9076 | |
| 9077 | if (devlink->ops->trap_policer_init) { |
| 9078 | err = devlink->ops->trap_policer_init(devlink, policer); |
| 9079 | if (err) |
| 9080 | goto err_policer_init; |
| 9081 | } |
| 9082 | |
| 9083 | list_add_tail(&policer_item->list, &devlink->trap_policer_list); |
| 9084 | devlink_trap_policer_notify(devlink, policer_item, |
| 9085 | DEVLINK_CMD_TRAP_POLICER_NEW); |
| 9086 | |
| 9087 | return 0; |
| 9088 | |
| 9089 | err_policer_init: |
| 9090 | kfree(policer_item); |
| 9091 | return err; |
| 9092 | } |
| 9093 | |
| 9094 | static void |
| 9095 | devlink_trap_policer_unregister(struct devlink *devlink, |
| 9096 | const struct devlink_trap_policer *policer) |
| 9097 | { |
| 9098 | struct devlink_trap_policer_item *policer_item; |
| 9099 | |
| 9100 | policer_item = devlink_trap_policer_item_lookup(devlink, policer->id); |
| 9101 | if (WARN_ON_ONCE(!policer_item)) |
| 9102 | return; |
| 9103 | |
| 9104 | devlink_trap_policer_notify(devlink, policer_item, |
| 9105 | DEVLINK_CMD_TRAP_POLICER_DEL); |
| 9106 | list_del(&policer_item->list); |
| 9107 | if (devlink->ops->trap_policer_fini) |
| 9108 | devlink->ops->trap_policer_fini(devlink, policer); |
| 9109 | kfree(policer_item); |
| 9110 | } |
| 9111 | |
| 9112 | /** |
| 9113 | * devlink_trap_policers_register - Register packet trap policers with devlink. |
| 9114 | * @devlink: devlink. |
| 9115 | * @policers: Packet trap policers. |
| 9116 | * @policers_count: Count of provided packet trap policers. |
| 9117 | * |
| 9118 | * Return: Non-zero value on failure. |
| 9119 | */ |
| 9120 | int |
| 9121 | devlink_trap_policers_register(struct devlink *devlink, |
| 9122 | const struct devlink_trap_policer *policers, |
| 9123 | size_t policers_count) |
| 9124 | { |
| 9125 | int i, err; |
| 9126 | |
| 9127 | mutex_lock(&devlink->lock); |
| 9128 | for (i = 0; i < policers_count; i++) { |
| 9129 | const struct devlink_trap_policer *policer = &policers[i]; |
| 9130 | |
| 9131 | if (WARN_ON(policer->id == 0 || |
| 9132 | policer->max_rate < policer->min_rate || |
| 9133 | policer->max_burst < policer->min_burst)) { |
| 9134 | err = -EINVAL; |
| 9135 | goto err_trap_policer_verify; |
| 9136 | } |
| 9137 | |
| 9138 | err = devlink_trap_policer_register(devlink, policer); |
| 9139 | if (err) |
| 9140 | goto err_trap_policer_register; |
| 9141 | } |
| 9142 | mutex_unlock(&devlink->lock); |
| 9143 | |
| 9144 | return 0; |
| 9145 | |
| 9146 | err_trap_policer_register: |
| 9147 | err_trap_policer_verify: |
| 9148 | for (i--; i >= 0; i--) |
| 9149 | devlink_trap_policer_unregister(devlink, &policers[i]); |
| 9150 | mutex_unlock(&devlink->lock); |
| 9151 | return err; |
| 9152 | } |
| 9153 | EXPORT_SYMBOL_GPL(devlink_trap_policers_register); |
| 9154 | |
| 9155 | /** |
| 9156 | * devlink_trap_policers_unregister - Unregister packet trap policers from devlink. |
| 9157 | * @devlink: devlink. |
| 9158 | * @policers: Packet trap policers. |
| 9159 | * @policers_count: Count of provided packet trap policers. |
| 9160 | */ |
| 9161 | void |
| 9162 | devlink_trap_policers_unregister(struct devlink *devlink, |
| 9163 | const struct devlink_trap_policer *policers, |
| 9164 | size_t policers_count) |
| 9165 | { |
| 9166 | int i; |
| 9167 | |
| 9168 | mutex_lock(&devlink->lock); |
| 9169 | for (i = policers_count - 1; i >= 0; i--) |
| 9170 | devlink_trap_policer_unregister(devlink, &policers[i]); |
| 9171 | mutex_unlock(&devlink->lock); |
| 9172 | } |
| 9173 | EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister); |
| 9174 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 9175 | static void __devlink_compat_running_version(struct devlink *devlink, |
| 9176 | char *buf, size_t len) |
| 9177 | { |
| 9178 | const struct nlattr *nlattr; |
| 9179 | struct devlink_info_req req; |
| 9180 | struct sk_buff *msg; |
| 9181 | int rem, err; |
| 9182 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 9183 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 9184 | if (!msg) |
| 9185 | return; |
| 9186 | |
| 9187 | req.msg = msg; |
| 9188 | err = devlink->ops->info_get(devlink, &req, NULL); |
| 9189 | if (err) |
| 9190 | goto free_msg; |
| 9191 | |
| 9192 | nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) { |
| 9193 | const struct nlattr *kv; |
| 9194 | int rem_kv; |
| 9195 | |
| 9196 | if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING) |
| 9197 | continue; |
| 9198 | |
| 9199 | nla_for_each_nested(kv, nlattr, rem_kv) { |
| 9200 | if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE) |
| 9201 | continue; |
| 9202 | |
| 9203 | strlcat(buf, nla_data(kv), len); |
| 9204 | strlcat(buf, " ", len); |
| 9205 | } |
| 9206 | } |
| 9207 | free_msg: |
| 9208 | nlmsg_free(msg); |
| 9209 | } |
| 9210 | |
| 9211 | void devlink_compat_running_version(struct net_device *dev, |
| 9212 | char *buf, size_t len) |
| 9213 | { |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 9214 | struct devlink *devlink; |
| 9215 | |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9216 | dev_hold(dev); |
| 9217 | rtnl_unlock(); |
| 9218 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9219 | devlink = netdev_to_devlink(dev); |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 9220 | if (!devlink || !devlink->ops->info_get) |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9221 | goto out; |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9222 | |
| 9223 | mutex_lock(&devlink->lock); |
| 9224 | __devlink_compat_running_version(devlink, buf, len); |
| 9225 | mutex_unlock(&devlink->lock); |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9226 | |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9227 | out: |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9228 | rtnl_lock(); |
| 9229 | dev_put(dev); |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 9230 | } |
| 9231 | |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9232 | int devlink_compat_flash_update(struct net_device *dev, const char *file_name) |
| 9233 | { |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9234 | struct devlink *devlink; |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9235 | int ret; |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9236 | |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9237 | dev_hold(dev); |
| 9238 | rtnl_unlock(); |
| 9239 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9240 | devlink = netdev_to_devlink(dev); |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9241 | if (!devlink || !devlink->ops->flash_update) { |
| 9242 | ret = -EOPNOTSUPP; |
| 9243 | goto out; |
| 9244 | } |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9245 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9246 | mutex_lock(&devlink->lock); |
| 9247 | ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL); |
| 9248 | mutex_unlock(&devlink->lock); |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9249 | |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 9250 | out: |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 9251 | rtnl_lock(); |
| 9252 | dev_put(dev); |
| 9253 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 9254 | return ret; |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 9255 | } |
| 9256 | |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 9257 | int devlink_compat_phys_port_name_get(struct net_device *dev, |
| 9258 | char *name, size_t len) |
| 9259 | { |
| 9260 | struct devlink_port *devlink_port; |
| 9261 | |
| 9262 | /* RTNL mutex is held here which ensures that devlink_port |
| 9263 | * instance cannot disappear in the middle. No need to take |
| 9264 | * any devlink lock as only permanent values are accessed. |
| 9265 | */ |
| 9266 | ASSERT_RTNL(); |
| 9267 | |
| 9268 | devlink_port = netdev_to_devlink_port(dev); |
| 9269 | if (!devlink_port) |
| 9270 | return -EOPNOTSUPP; |
| 9271 | |
| 9272 | return __devlink_port_phys_port_name_get(devlink_port, name, len); |
| 9273 | } |
| 9274 | |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 9275 | int devlink_compat_switch_id_get(struct net_device *dev, |
| 9276 | struct netdev_phys_item_id *ppid) |
| 9277 | { |
| 9278 | struct devlink_port *devlink_port; |
| 9279 | |
Vlad Buslov | 043b841 | 2019-08-12 20:02:02 +0300 | [diff] [blame] | 9280 | /* Caller must hold RTNL mutex or reference to dev, which ensures that |
| 9281 | * devlink_port instance cannot disappear in the middle. No need to take |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 9282 | * any devlink lock as only permanent values are accessed. |
| 9283 | */ |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 9284 | devlink_port = netdev_to_devlink_port(dev); |
| 9285 | if (!devlink_port || !devlink_port->attrs.switch_port) |
| 9286 | return -EOPNOTSUPP; |
| 9287 | |
| 9288 | memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid)); |
| 9289 | |
| 9290 | return 0; |
| 9291 | } |
| 9292 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 9293 | static void __net_exit devlink_pernet_pre_exit(struct net *net) |
| 9294 | { |
| 9295 | struct devlink *devlink; |
| 9296 | int err; |
| 9297 | |
| 9298 | /* In case network namespace is getting destroyed, reload |
| 9299 | * all devlink instances from this namespace into init_net. |
| 9300 | */ |
| 9301 | mutex_lock(&devlink_mutex); |
| 9302 | list_for_each_entry(devlink, &devlink_list, list) { |
| 9303 | if (net_eq(devlink_net(devlink), net)) { |
| 9304 | if (WARN_ON(!devlink_reload_supported(devlink))) |
| 9305 | continue; |
| 9306 | err = devlink_reload(devlink, &init_net, NULL); |
Jiri Pirko | a0c7634 | 2019-11-08 21:42:43 +0100 | [diff] [blame] | 9307 | if (err && err != -EOPNOTSUPP) |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 9308 | pr_warn("Failed to reload devlink instance into init_net\n"); |
| 9309 | } |
| 9310 | } |
| 9311 | mutex_unlock(&devlink_mutex); |
| 9312 | } |
| 9313 | |
| 9314 | static struct pernet_operations devlink_pernet_ops __net_initdata = { |
| 9315 | .pre_exit = devlink_pernet_pre_exit, |
| 9316 | }; |
| 9317 | |
Jakub Kicinski | f4b6bcc | 2019-02-25 19:34:02 -0800 | [diff] [blame] | 9318 | static int __init devlink_init(void) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 9319 | { |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 9320 | int err; |
| 9321 | |
| 9322 | err = genl_register_family(&devlink_nl_family); |
| 9323 | if (err) |
| 9324 | goto out; |
| 9325 | err = register_pernet_subsys(&devlink_pernet_ops); |
| 9326 | |
| 9327 | out: |
| 9328 | WARN_ON(err); |
| 9329 | return err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 9330 | } |
| 9331 | |
Jakub Kicinski | f4b6bcc | 2019-02-25 19:34:02 -0800 | [diff] [blame] | 9332 | subsys_initcall(devlink_init); |