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> |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 21 | #include <rdma/ib_verbs.h> |
| 22 | #include <net/netlink.h> |
| 23 | #include <net/genetlink.h> |
| 24 | #include <net/rtnetlink.h> |
| 25 | #include <net/net_namespace.h> |
| 26 | #include <net/sock.h> |
| 27 | #include <net/devlink.h> |
Jiri Pirko | e5224f0 | 2016-07-12 18:05:03 +0200 | [diff] [blame] | 28 | #define CREATE_TRACE_POINTS |
| 29 | #include <trace/events/devlink.h> |
| 30 | |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 31 | static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = { |
| 32 | { |
David Ahern | 12bdc5e | 2017-08-30 17:07:30 -0700 | [diff] [blame] | 33 | .name = "destination mac", |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 34 | .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC, |
| 35 | .bitwidth = 48, |
| 36 | }, |
| 37 | }; |
| 38 | |
| 39 | struct devlink_dpipe_header devlink_dpipe_header_ethernet = { |
| 40 | .name = "ethernet", |
| 41 | .id = DEVLINK_DPIPE_HEADER_ETHERNET, |
| 42 | .fields = devlink_dpipe_fields_ethernet, |
| 43 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet), |
| 44 | .global = true, |
| 45 | }; |
| 46 | EXPORT_SYMBOL(devlink_dpipe_header_ethernet); |
| 47 | |
Arkadi Sharshevsky | 3fb886e | 2017-08-24 08:40:00 +0200 | [diff] [blame] | 48 | static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = { |
| 49 | { |
| 50 | .name = "destination ip", |
| 51 | .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP, |
| 52 | .bitwidth = 32, |
| 53 | }, |
| 54 | }; |
| 55 | |
| 56 | struct devlink_dpipe_header devlink_dpipe_header_ipv4 = { |
| 57 | .name = "ipv4", |
| 58 | .id = DEVLINK_DPIPE_HEADER_IPV4, |
| 59 | .fields = devlink_dpipe_fields_ipv4, |
| 60 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4), |
| 61 | .global = true, |
| 62 | }; |
| 63 | EXPORT_SYMBOL(devlink_dpipe_header_ipv4); |
| 64 | |
Arkadi Sharshevsky | 1797f5b | 2017-08-31 17:59:12 +0200 | [diff] [blame] | 65 | static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = { |
| 66 | { |
| 67 | .name = "destination ip", |
| 68 | .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP, |
| 69 | .bitwidth = 128, |
| 70 | }, |
| 71 | }; |
| 72 | |
| 73 | struct devlink_dpipe_header devlink_dpipe_header_ipv6 = { |
| 74 | .name = "ipv6", |
| 75 | .id = DEVLINK_DPIPE_HEADER_IPV6, |
| 76 | .fields = devlink_dpipe_fields_ipv6, |
| 77 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6), |
| 78 | .global = true, |
| 79 | }; |
| 80 | EXPORT_SYMBOL(devlink_dpipe_header_ipv6); |
| 81 | |
Jiri Pirko | e5224f0 | 2016-07-12 18:05:03 +0200 | [diff] [blame] | 82 | EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg); |
Nir Dotan | 57186a5 | 2019-02-04 18:47:45 +0000 | [diff] [blame] | 83 | EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 84 | |
| 85 | static LIST_HEAD(devlink_list); |
| 86 | |
| 87 | /* devlink_mutex |
| 88 | * |
| 89 | * An overall lock guarding every operation coming from userspace. |
| 90 | * It also guards devlink devices list and it is taken when |
| 91 | * driver registers/unregisters it. |
| 92 | */ |
| 93 | static DEFINE_MUTEX(devlink_mutex); |
| 94 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 95 | static struct net *devlink_net(const struct devlink *devlink) |
| 96 | { |
| 97 | return read_pnet(&devlink->_net); |
| 98 | } |
| 99 | |
| 100 | static void devlink_net_set(struct devlink *devlink, struct net *net) |
| 101 | { |
| 102 | write_pnet(&devlink->_net, net); |
| 103 | } |
| 104 | |
| 105 | static struct devlink *devlink_get_from_attrs(struct net *net, |
| 106 | struct nlattr **attrs) |
| 107 | { |
| 108 | struct devlink *devlink; |
| 109 | char *busname; |
| 110 | char *devname; |
| 111 | |
| 112 | if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME]) |
| 113 | return ERR_PTR(-EINVAL); |
| 114 | |
| 115 | busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]); |
| 116 | devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]); |
| 117 | |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 118 | lockdep_assert_held(&devlink_mutex); |
| 119 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 120 | list_for_each_entry(devlink, &devlink_list, list) { |
| 121 | if (strcmp(devlink->dev->bus->name, busname) == 0 && |
| 122 | strcmp(dev_name(devlink->dev), devname) == 0 && |
| 123 | net_eq(devlink_net(devlink), net)) |
| 124 | return devlink; |
| 125 | } |
| 126 | |
| 127 | return ERR_PTR(-ENODEV); |
| 128 | } |
| 129 | |
| 130 | static struct devlink *devlink_get_from_info(struct genl_info *info) |
| 131 | { |
| 132 | return devlink_get_from_attrs(genl_info_net(info), info->attrs); |
| 133 | } |
| 134 | |
| 135 | static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, |
| 136 | int port_index) |
| 137 | { |
| 138 | struct devlink_port *devlink_port; |
| 139 | |
| 140 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 141 | if (devlink_port->index == port_index) |
| 142 | return devlink_port; |
| 143 | } |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | static bool devlink_port_index_exists(struct devlink *devlink, int port_index) |
| 148 | { |
| 149 | return devlink_port_get_by_index(devlink, port_index); |
| 150 | } |
| 151 | |
| 152 | static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, |
| 153 | struct nlattr **attrs) |
| 154 | { |
| 155 | if (attrs[DEVLINK_ATTR_PORT_INDEX]) { |
| 156 | u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 157 | struct devlink_port *devlink_port; |
| 158 | |
| 159 | devlink_port = devlink_port_get_by_index(devlink, port_index); |
| 160 | if (!devlink_port) |
| 161 | return ERR_PTR(-ENODEV); |
| 162 | return devlink_port; |
| 163 | } |
| 164 | return ERR_PTR(-EINVAL); |
| 165 | } |
| 166 | |
| 167 | static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink, |
| 168 | struct genl_info *info) |
| 169 | { |
| 170 | return devlink_port_get_from_attrs(devlink, info->attrs); |
| 171 | } |
| 172 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 173 | struct devlink_sb { |
| 174 | struct list_head list; |
| 175 | unsigned int index; |
| 176 | u32 size; |
| 177 | u16 ingress_pools_count; |
| 178 | u16 egress_pools_count; |
| 179 | u16 ingress_tc_count; |
| 180 | u16 egress_tc_count; |
| 181 | }; |
| 182 | |
| 183 | static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb) |
| 184 | { |
| 185 | return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count; |
| 186 | } |
| 187 | |
| 188 | static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink, |
| 189 | unsigned int sb_index) |
| 190 | { |
| 191 | struct devlink_sb *devlink_sb; |
| 192 | |
| 193 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 194 | if (devlink_sb->index == sb_index) |
| 195 | return devlink_sb; |
| 196 | } |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | static bool devlink_sb_index_exists(struct devlink *devlink, |
| 201 | unsigned int sb_index) |
| 202 | { |
| 203 | return devlink_sb_get_by_index(devlink, sb_index); |
| 204 | } |
| 205 | |
| 206 | static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink, |
| 207 | struct nlattr **attrs) |
| 208 | { |
| 209 | if (attrs[DEVLINK_ATTR_SB_INDEX]) { |
| 210 | u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]); |
| 211 | struct devlink_sb *devlink_sb; |
| 212 | |
| 213 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 214 | if (!devlink_sb) |
| 215 | return ERR_PTR(-ENODEV); |
| 216 | return devlink_sb; |
| 217 | } |
| 218 | return ERR_PTR(-EINVAL); |
| 219 | } |
| 220 | |
| 221 | static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink, |
| 222 | struct genl_info *info) |
| 223 | { |
| 224 | return devlink_sb_get_from_attrs(devlink, info->attrs); |
| 225 | } |
| 226 | |
| 227 | static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 228 | struct nlattr **attrs, |
| 229 | u16 *p_pool_index) |
| 230 | { |
| 231 | u16 val; |
| 232 | |
| 233 | if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX]) |
| 234 | return -EINVAL; |
| 235 | |
| 236 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]); |
| 237 | if (val >= devlink_sb_pool_count(devlink_sb)) |
| 238 | return -EINVAL; |
| 239 | *p_pool_index = val; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb, |
| 244 | struct genl_info *info, |
| 245 | u16 *p_pool_index) |
| 246 | { |
| 247 | return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs, |
| 248 | p_pool_index); |
| 249 | } |
| 250 | |
| 251 | static int |
| 252 | devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs, |
| 253 | enum devlink_sb_pool_type *p_pool_type) |
| 254 | { |
| 255 | u8 val; |
| 256 | |
| 257 | if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE]) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]); |
| 261 | if (val != DEVLINK_SB_POOL_TYPE_INGRESS && |
| 262 | val != DEVLINK_SB_POOL_TYPE_EGRESS) |
| 263 | return -EINVAL; |
| 264 | *p_pool_type = val; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int |
| 269 | devlink_sb_pool_type_get_from_info(struct genl_info *info, |
| 270 | enum devlink_sb_pool_type *p_pool_type) |
| 271 | { |
| 272 | return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type); |
| 273 | } |
| 274 | |
| 275 | static int |
| 276 | devlink_sb_th_type_get_from_attrs(struct nlattr **attrs, |
| 277 | enum devlink_sb_threshold_type *p_th_type) |
| 278 | { |
| 279 | u8 val; |
| 280 | |
| 281 | if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]); |
| 285 | if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC && |
| 286 | val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC) |
| 287 | return -EINVAL; |
| 288 | *p_th_type = val; |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int |
| 293 | devlink_sb_th_type_get_from_info(struct genl_info *info, |
| 294 | enum devlink_sb_threshold_type *p_th_type) |
| 295 | { |
| 296 | return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type); |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 301 | struct nlattr **attrs, |
| 302 | enum devlink_sb_pool_type pool_type, |
| 303 | u16 *p_tc_index) |
| 304 | { |
| 305 | u16 val; |
| 306 | |
| 307 | if (!attrs[DEVLINK_ATTR_SB_TC_INDEX]) |
| 308 | return -EINVAL; |
| 309 | |
| 310 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]); |
| 311 | if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS && |
| 312 | val >= devlink_sb->ingress_tc_count) |
| 313 | return -EINVAL; |
| 314 | if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS && |
| 315 | val >= devlink_sb->egress_tc_count) |
| 316 | return -EINVAL; |
| 317 | *p_tc_index = val; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int |
| 322 | devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb, |
| 323 | struct genl_info *info, |
| 324 | enum devlink_sb_pool_type pool_type, |
| 325 | u16 *p_tc_index) |
| 326 | { |
| 327 | return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs, |
| 328 | pool_type, p_tc_index); |
| 329 | } |
| 330 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 331 | struct devlink_region { |
| 332 | struct devlink *devlink; |
| 333 | struct list_head list; |
| 334 | const char *name; |
| 335 | struct list_head snapshot_list; |
| 336 | u32 max_snapshots; |
| 337 | u32 cur_snapshots; |
| 338 | u64 size; |
| 339 | }; |
| 340 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 341 | struct devlink_snapshot { |
| 342 | struct list_head list; |
| 343 | struct devlink_region *region; |
| 344 | devlink_snapshot_data_dest_t *data_destructor; |
| 345 | u64 data_len; |
| 346 | u8 *data; |
| 347 | u32 id; |
| 348 | }; |
| 349 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 350 | static struct devlink_region * |
| 351 | devlink_region_get_by_name(struct devlink *devlink, const char *region_name) |
| 352 | { |
| 353 | struct devlink_region *region; |
| 354 | |
| 355 | list_for_each_entry(region, &devlink->region_list, list) |
| 356 | if (!strcmp(region->name, region_name)) |
| 357 | return region; |
| 358 | |
| 359 | return NULL; |
| 360 | } |
| 361 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 362 | static struct devlink_snapshot * |
| 363 | devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id) |
| 364 | { |
| 365 | struct devlink_snapshot *snapshot; |
| 366 | |
| 367 | list_for_each_entry(snapshot, ®ion->snapshot_list, list) |
| 368 | if (snapshot->id == id) |
| 369 | return snapshot; |
| 370 | |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot) |
| 375 | { |
| 376 | snapshot->region->cur_snapshots--; |
| 377 | list_del(&snapshot->list); |
| 378 | (*snapshot->data_destructor)(snapshot->data); |
| 379 | kfree(snapshot); |
| 380 | } |
| 381 | |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 382 | #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0) |
| 383 | #define DEVLINK_NL_FLAG_NEED_PORT BIT(1) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 384 | #define DEVLINK_NL_FLAG_NEED_SB BIT(2) |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 385 | |
| 386 | /* The per devlink instance lock is taken by default in the pre-doit |
| 387 | * operation, yet several commands do not require this. The global |
| 388 | * devlink lock is taken and protects from disruption by user-calls. |
| 389 | */ |
| 390 | #define DEVLINK_NL_FLAG_NO_LOCK BIT(3) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 391 | |
| 392 | static int devlink_nl_pre_doit(const struct genl_ops *ops, |
| 393 | struct sk_buff *skb, struct genl_info *info) |
| 394 | { |
| 395 | struct devlink *devlink; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 396 | int err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 397 | |
| 398 | mutex_lock(&devlink_mutex); |
| 399 | devlink = devlink_get_from_info(info); |
| 400 | if (IS_ERR(devlink)) { |
| 401 | mutex_unlock(&devlink_mutex); |
| 402 | return PTR_ERR(devlink); |
| 403 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 404 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 405 | mutex_lock(&devlink->lock); |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 406 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 407 | info->user_ptr[0] = devlink; |
| 408 | } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 409 | struct devlink_port *devlink_port; |
| 410 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 411 | devlink_port = devlink_port_get_from_info(devlink, info); |
| 412 | if (IS_ERR(devlink_port)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 413 | err = PTR_ERR(devlink_port); |
| 414 | goto unlock; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 415 | } |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 416 | info->user_ptr[0] = devlink_port; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 417 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 418 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) { |
| 419 | struct devlink_sb *devlink_sb; |
| 420 | |
| 421 | devlink_sb = devlink_sb_get_from_info(devlink, info); |
| 422 | if (IS_ERR(devlink_sb)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 423 | err = PTR_ERR(devlink_sb); |
| 424 | goto unlock; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 425 | } |
| 426 | info->user_ptr[1] = devlink_sb; |
| 427 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 428 | return 0; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 429 | |
| 430 | unlock: |
| 431 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 432 | mutex_unlock(&devlink->lock); |
| 433 | mutex_unlock(&devlink_mutex); |
| 434 | return err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void devlink_nl_post_doit(const struct genl_ops *ops, |
| 438 | struct sk_buff *skb, struct genl_info *info) |
| 439 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 440 | struct devlink *devlink; |
| 441 | |
| 442 | devlink = devlink_get_from_info(info); |
| 443 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 444 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 445 | mutex_unlock(&devlink_mutex); |
| 446 | } |
| 447 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 448 | static struct genl_family devlink_nl_family; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 449 | |
| 450 | enum devlink_multicast_groups { |
| 451 | DEVLINK_MCGRP_CONFIG, |
| 452 | }; |
| 453 | |
| 454 | static const struct genl_multicast_group devlink_nl_mcgrps[] = { |
| 455 | [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, |
| 456 | }; |
| 457 | |
| 458 | static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) |
| 459 | { |
| 460 | if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name)) |
| 461 | return -EMSGSIZE; |
| 462 | if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev))) |
| 463 | return -EMSGSIZE; |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink, |
| 468 | enum devlink_command cmd, u32 portid, |
| 469 | u32 seq, int flags) |
| 470 | { |
| 471 | void *hdr; |
| 472 | |
| 473 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 474 | if (!hdr) |
| 475 | return -EMSGSIZE; |
| 476 | |
| 477 | if (devlink_nl_put_handle(msg, devlink)) |
| 478 | goto nla_put_failure; |
| 479 | |
| 480 | genlmsg_end(msg, hdr); |
| 481 | return 0; |
| 482 | |
| 483 | nla_put_failure: |
| 484 | genlmsg_cancel(msg, hdr); |
| 485 | return -EMSGSIZE; |
| 486 | } |
| 487 | |
| 488 | static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) |
| 489 | { |
| 490 | struct sk_buff *msg; |
| 491 | int err; |
| 492 | |
| 493 | WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); |
| 494 | |
| 495 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 496 | if (!msg) |
| 497 | return; |
| 498 | |
| 499 | err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0); |
| 500 | if (err) { |
| 501 | nlmsg_free(msg); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 506 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 507 | } |
| 508 | |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 509 | static int devlink_nl_port_attrs_put(struct sk_buff *msg, |
| 510 | struct devlink_port *devlink_port) |
| 511 | { |
| 512 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 513 | |
| 514 | if (!attrs->set) |
| 515 | return 0; |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 516 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour)) |
| 517 | return -EMSGSIZE; |
Parav Pandit | a2c6b87 | 2019-07-08 23:17:36 -0500 | [diff] [blame^] | 518 | if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PHYSICAL && |
| 519 | devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU && |
| 520 | devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA) |
| 521 | return 0; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 522 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, |
| 523 | attrs->phys.port_number)) |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 524 | return -EMSGSIZE; |
| 525 | if (!attrs->split) |
| 526 | return 0; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 527 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, |
| 528 | attrs->phys.port_number)) |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 529 | return -EMSGSIZE; |
| 530 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER, |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 531 | attrs->phys.split_subport_number)) |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 532 | return -EMSGSIZE; |
| 533 | return 0; |
| 534 | } |
| 535 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 536 | static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, |
| 537 | struct devlink_port *devlink_port, |
| 538 | enum devlink_command cmd, u32 portid, |
| 539 | u32 seq, int flags) |
| 540 | { |
| 541 | void *hdr; |
| 542 | |
| 543 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 544 | if (!hdr) |
| 545 | return -EMSGSIZE; |
| 546 | |
| 547 | if (devlink_nl_put_handle(msg, devlink)) |
| 548 | goto nla_put_failure; |
| 549 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 550 | goto nla_put_failure; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 551 | |
| 552 | spin_lock(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 553 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 554 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 555 | if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET && |
| 556 | nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE, |
| 557 | devlink_port->desired_type)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 558 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 559 | if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { |
| 560 | struct net_device *netdev = devlink_port->type_dev; |
| 561 | |
| 562 | if (netdev && |
| 563 | (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, |
| 564 | netdev->ifindex) || |
| 565 | nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, |
| 566 | netdev->name))) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 567 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 568 | } |
| 569 | if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { |
| 570 | struct ib_device *ibdev = devlink_port->type_dev; |
| 571 | |
| 572 | if (ibdev && |
| 573 | nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME, |
| 574 | ibdev->name)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 575 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 576 | } |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 577 | spin_unlock(&devlink_port->type_lock); |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 578 | if (devlink_nl_port_attrs_put(msg, devlink_port)) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 579 | goto nla_put_failure; |
| 580 | |
| 581 | genlmsg_end(msg, hdr); |
| 582 | return 0; |
| 583 | |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 584 | nla_put_failure_type_locked: |
| 585 | spin_unlock(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 586 | nla_put_failure: |
| 587 | genlmsg_cancel(msg, hdr); |
| 588 | return -EMSGSIZE; |
| 589 | } |
| 590 | |
| 591 | static void devlink_port_notify(struct devlink_port *devlink_port, |
| 592 | enum devlink_command cmd) |
| 593 | { |
| 594 | struct devlink *devlink = devlink_port->devlink; |
| 595 | struct sk_buff *msg; |
| 596 | int err; |
| 597 | |
| 598 | if (!devlink_port->registered) |
| 599 | return; |
| 600 | |
| 601 | WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); |
| 602 | |
| 603 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 604 | if (!msg) |
| 605 | return; |
| 606 | |
| 607 | err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0); |
| 608 | if (err) { |
| 609 | nlmsg_free(msg); |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 614 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 615 | } |
| 616 | |
| 617 | static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info) |
| 618 | { |
| 619 | struct devlink *devlink = info->user_ptr[0]; |
| 620 | struct sk_buff *msg; |
| 621 | int err; |
| 622 | |
| 623 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 624 | if (!msg) |
| 625 | return -ENOMEM; |
| 626 | |
| 627 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 628 | info->snd_portid, info->snd_seq, 0); |
| 629 | if (err) { |
| 630 | nlmsg_free(msg); |
| 631 | return err; |
| 632 | } |
| 633 | |
| 634 | return genlmsg_reply(msg, info); |
| 635 | } |
| 636 | |
| 637 | static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg, |
| 638 | struct netlink_callback *cb) |
| 639 | { |
| 640 | struct devlink *devlink; |
| 641 | int start = cb->args[0]; |
| 642 | int idx = 0; |
| 643 | int err; |
| 644 | |
| 645 | mutex_lock(&devlink_mutex); |
| 646 | list_for_each_entry(devlink, &devlink_list, list) { |
| 647 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 648 | continue; |
| 649 | if (idx < start) { |
| 650 | idx++; |
| 651 | continue; |
| 652 | } |
| 653 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 654 | NETLINK_CB(cb->skb).portid, |
| 655 | cb->nlh->nlmsg_seq, NLM_F_MULTI); |
| 656 | if (err) |
| 657 | goto out; |
| 658 | idx++; |
| 659 | } |
| 660 | out: |
| 661 | mutex_unlock(&devlink_mutex); |
| 662 | |
| 663 | cb->args[0] = idx; |
| 664 | return msg->len; |
| 665 | } |
| 666 | |
| 667 | static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb, |
| 668 | struct genl_info *info) |
| 669 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 670 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 671 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 672 | struct sk_buff *msg; |
| 673 | int err; |
| 674 | |
| 675 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 676 | if (!msg) |
| 677 | return -ENOMEM; |
| 678 | |
| 679 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 680 | DEVLINK_CMD_PORT_NEW, |
| 681 | info->snd_portid, info->snd_seq, 0); |
| 682 | if (err) { |
| 683 | nlmsg_free(msg); |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | return genlmsg_reply(msg, info); |
| 688 | } |
| 689 | |
| 690 | static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg, |
| 691 | struct netlink_callback *cb) |
| 692 | { |
| 693 | struct devlink *devlink; |
| 694 | struct devlink_port *devlink_port; |
| 695 | int start = cb->args[0]; |
| 696 | int idx = 0; |
| 697 | int err; |
| 698 | |
| 699 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 700 | list_for_each_entry(devlink, &devlink_list, list) { |
| 701 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 702 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 703 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 704 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 705 | if (idx < start) { |
| 706 | idx++; |
| 707 | continue; |
| 708 | } |
| 709 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 710 | DEVLINK_CMD_NEW, |
| 711 | NETLINK_CB(cb->skb).portid, |
| 712 | cb->nlh->nlmsg_seq, |
| 713 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 714 | if (err) { |
| 715 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 716 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 717 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 718 | idx++; |
| 719 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 720 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 721 | } |
| 722 | out: |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 723 | mutex_unlock(&devlink_mutex); |
| 724 | |
| 725 | cb->args[0] = idx; |
| 726 | return msg->len; |
| 727 | } |
| 728 | |
| 729 | static int devlink_port_type_set(struct devlink *devlink, |
| 730 | struct devlink_port *devlink_port, |
| 731 | enum devlink_port_type port_type) |
| 732 | |
| 733 | { |
| 734 | int err; |
| 735 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 736 | if (devlink->ops->port_type_set) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 737 | if (port_type == DEVLINK_PORT_TYPE_NOTSET) |
| 738 | return -EINVAL; |
Elad Raz | 6edf101 | 2016-10-23 17:43:05 +0200 | [diff] [blame] | 739 | if (port_type == devlink_port->type) |
| 740 | return 0; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 741 | err = devlink->ops->port_type_set(devlink_port, port_type); |
| 742 | if (err) |
| 743 | return err; |
| 744 | devlink_port->desired_type = port_type; |
| 745 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 746 | return 0; |
| 747 | } |
| 748 | return -EOPNOTSUPP; |
| 749 | } |
| 750 | |
| 751 | static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, |
| 752 | struct genl_info *info) |
| 753 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 754 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 755 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 756 | int err; |
| 757 | |
| 758 | if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) { |
| 759 | enum devlink_port_type port_type; |
| 760 | |
| 761 | port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]); |
| 762 | err = devlink_port_type_set(devlink, devlink_port, port_type); |
| 763 | if (err) |
| 764 | return err; |
| 765 | } |
| 766 | return 0; |
| 767 | } |
| 768 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 769 | static int devlink_port_split(struct devlink *devlink, u32 port_index, |
| 770 | u32 count, struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 771 | |
| 772 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 773 | if (devlink->ops->port_split) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 774 | return devlink->ops->port_split(devlink, port_index, count, |
| 775 | extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 776 | return -EOPNOTSUPP; |
| 777 | } |
| 778 | |
| 779 | static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, |
| 780 | struct genl_info *info) |
| 781 | { |
| 782 | struct devlink *devlink = info->user_ptr[0]; |
| 783 | u32 port_index; |
| 784 | u32 count; |
| 785 | |
| 786 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] || |
| 787 | !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]) |
| 788 | return -EINVAL; |
| 789 | |
| 790 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 791 | count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 792 | return devlink_port_split(devlink, port_index, count, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 793 | } |
| 794 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 795 | static int devlink_port_unsplit(struct devlink *devlink, u32 port_index, |
| 796 | struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 797 | |
| 798 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 799 | if (devlink->ops->port_unsplit) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 800 | return devlink->ops->port_unsplit(devlink, port_index, extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 801 | return -EOPNOTSUPP; |
| 802 | } |
| 803 | |
| 804 | static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb, |
| 805 | struct genl_info *info) |
| 806 | { |
| 807 | struct devlink *devlink = info->user_ptr[0]; |
| 808 | u32 port_index; |
| 809 | |
| 810 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) |
| 811 | return -EINVAL; |
| 812 | |
| 813 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 814 | return devlink_port_unsplit(devlink, port_index, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 815 | } |
| 816 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 817 | static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink, |
| 818 | struct devlink_sb *devlink_sb, |
| 819 | enum devlink_command cmd, u32 portid, |
| 820 | u32 seq, int flags) |
| 821 | { |
| 822 | void *hdr; |
| 823 | |
| 824 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 825 | if (!hdr) |
| 826 | return -EMSGSIZE; |
| 827 | |
| 828 | if (devlink_nl_put_handle(msg, devlink)) |
| 829 | goto nla_put_failure; |
| 830 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 831 | goto nla_put_failure; |
| 832 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size)) |
| 833 | goto nla_put_failure; |
| 834 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT, |
| 835 | devlink_sb->ingress_pools_count)) |
| 836 | goto nla_put_failure; |
| 837 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT, |
| 838 | devlink_sb->egress_pools_count)) |
| 839 | goto nla_put_failure; |
| 840 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT, |
| 841 | devlink_sb->ingress_tc_count)) |
| 842 | goto nla_put_failure; |
| 843 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT, |
| 844 | devlink_sb->egress_tc_count)) |
| 845 | goto nla_put_failure; |
| 846 | |
| 847 | genlmsg_end(msg, hdr); |
| 848 | return 0; |
| 849 | |
| 850 | nla_put_failure: |
| 851 | genlmsg_cancel(msg, hdr); |
| 852 | return -EMSGSIZE; |
| 853 | } |
| 854 | |
| 855 | static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb, |
| 856 | struct genl_info *info) |
| 857 | { |
| 858 | struct devlink *devlink = info->user_ptr[0]; |
| 859 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 860 | struct sk_buff *msg; |
| 861 | int err; |
| 862 | |
| 863 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 864 | if (!msg) |
| 865 | return -ENOMEM; |
| 866 | |
| 867 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 868 | DEVLINK_CMD_SB_NEW, |
| 869 | info->snd_portid, info->snd_seq, 0); |
| 870 | if (err) { |
| 871 | nlmsg_free(msg); |
| 872 | return err; |
| 873 | } |
| 874 | |
| 875 | return genlmsg_reply(msg, info); |
| 876 | } |
| 877 | |
| 878 | static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg, |
| 879 | struct netlink_callback *cb) |
| 880 | { |
| 881 | struct devlink *devlink; |
| 882 | struct devlink_sb *devlink_sb; |
| 883 | int start = cb->args[0]; |
| 884 | int idx = 0; |
| 885 | int err; |
| 886 | |
| 887 | mutex_lock(&devlink_mutex); |
| 888 | list_for_each_entry(devlink, &devlink_list, list) { |
| 889 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 890 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 891 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 892 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 893 | if (idx < start) { |
| 894 | idx++; |
| 895 | continue; |
| 896 | } |
| 897 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 898 | DEVLINK_CMD_SB_NEW, |
| 899 | NETLINK_CB(cb->skb).portid, |
| 900 | cb->nlh->nlmsg_seq, |
| 901 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 902 | if (err) { |
| 903 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 904 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 905 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 906 | idx++; |
| 907 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 908 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 909 | } |
| 910 | out: |
| 911 | mutex_unlock(&devlink_mutex); |
| 912 | |
| 913 | cb->args[0] = idx; |
| 914 | return msg->len; |
| 915 | } |
| 916 | |
| 917 | static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink, |
| 918 | struct devlink_sb *devlink_sb, |
| 919 | u16 pool_index, enum devlink_command cmd, |
| 920 | u32 portid, u32 seq, int flags) |
| 921 | { |
| 922 | struct devlink_sb_pool_info pool_info; |
| 923 | void *hdr; |
| 924 | int err; |
| 925 | |
| 926 | err = devlink->ops->sb_pool_get(devlink, devlink_sb->index, |
| 927 | pool_index, &pool_info); |
| 928 | if (err) |
| 929 | return err; |
| 930 | |
| 931 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 932 | if (!hdr) |
| 933 | return -EMSGSIZE; |
| 934 | |
| 935 | if (devlink_nl_put_handle(msg, devlink)) |
| 936 | goto nla_put_failure; |
| 937 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 938 | goto nla_put_failure; |
| 939 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 940 | goto nla_put_failure; |
| 941 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type)) |
| 942 | goto nla_put_failure; |
| 943 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size)) |
| 944 | goto nla_put_failure; |
| 945 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE, |
| 946 | pool_info.threshold_type)) |
| 947 | goto nla_put_failure; |
Jakub Kicinski | bff5731 | 2019-02-01 17:56:28 -0800 | [diff] [blame] | 948 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE, |
| 949 | pool_info.cell_size)) |
| 950 | goto nla_put_failure; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 951 | |
| 952 | genlmsg_end(msg, hdr); |
| 953 | return 0; |
| 954 | |
| 955 | nla_put_failure: |
| 956 | genlmsg_cancel(msg, hdr); |
| 957 | return -EMSGSIZE; |
| 958 | } |
| 959 | |
| 960 | static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb, |
| 961 | struct genl_info *info) |
| 962 | { |
| 963 | struct devlink *devlink = info->user_ptr[0]; |
| 964 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 965 | struct sk_buff *msg; |
| 966 | u16 pool_index; |
| 967 | int err; |
| 968 | |
| 969 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 970 | &pool_index); |
| 971 | if (err) |
| 972 | return err; |
| 973 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 974 | if (!devlink->ops->sb_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 975 | return -EOPNOTSUPP; |
| 976 | |
| 977 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 978 | if (!msg) |
| 979 | return -ENOMEM; |
| 980 | |
| 981 | err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index, |
| 982 | DEVLINK_CMD_SB_POOL_NEW, |
| 983 | info->snd_portid, info->snd_seq, 0); |
| 984 | if (err) { |
| 985 | nlmsg_free(msg); |
| 986 | return err; |
| 987 | } |
| 988 | |
| 989 | return genlmsg_reply(msg, info); |
| 990 | } |
| 991 | |
| 992 | static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 993 | struct devlink *devlink, |
| 994 | struct devlink_sb *devlink_sb, |
| 995 | u32 portid, u32 seq) |
| 996 | { |
| 997 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 998 | u16 pool_index; |
| 999 | int err; |
| 1000 | |
| 1001 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1002 | if (*p_idx < start) { |
| 1003 | (*p_idx)++; |
| 1004 | continue; |
| 1005 | } |
| 1006 | err = devlink_nl_sb_pool_fill(msg, devlink, |
| 1007 | devlink_sb, |
| 1008 | pool_index, |
| 1009 | DEVLINK_CMD_SB_POOL_NEW, |
| 1010 | portid, seq, NLM_F_MULTI); |
| 1011 | if (err) |
| 1012 | return err; |
| 1013 | (*p_idx)++; |
| 1014 | } |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg, |
| 1019 | struct netlink_callback *cb) |
| 1020 | { |
| 1021 | struct devlink *devlink; |
| 1022 | struct devlink_sb *devlink_sb; |
| 1023 | int start = cb->args[0]; |
| 1024 | int idx = 0; |
| 1025 | int err; |
| 1026 | |
| 1027 | mutex_lock(&devlink_mutex); |
| 1028 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1029 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1030 | !devlink->ops->sb_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1031 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1032 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1033 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1034 | err = __sb_pool_get_dumpit(msg, start, &idx, devlink, |
| 1035 | devlink_sb, |
| 1036 | NETLINK_CB(cb->skb).portid, |
| 1037 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1038 | if (err && err != -EOPNOTSUPP) { |
| 1039 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1040 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1041 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1042 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1043 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1044 | } |
| 1045 | out: |
| 1046 | mutex_unlock(&devlink_mutex); |
| 1047 | |
| 1048 | cb->args[0] = idx; |
| 1049 | return msg->len; |
| 1050 | } |
| 1051 | |
| 1052 | static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index, |
| 1053 | u16 pool_index, u32 size, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1054 | enum devlink_sb_threshold_type threshold_type, |
| 1055 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1056 | |
| 1057 | { |
| 1058 | const struct devlink_ops *ops = devlink->ops; |
| 1059 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1060 | if (ops->sb_pool_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1061 | return ops->sb_pool_set(devlink, sb_index, pool_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1062 | size, threshold_type, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1063 | return -EOPNOTSUPP; |
| 1064 | } |
| 1065 | |
| 1066 | static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb, |
| 1067 | struct genl_info *info) |
| 1068 | { |
| 1069 | struct devlink *devlink = info->user_ptr[0]; |
| 1070 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1071 | enum devlink_sb_threshold_type threshold_type; |
| 1072 | u16 pool_index; |
| 1073 | u32 size; |
| 1074 | int err; |
| 1075 | |
| 1076 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1077 | &pool_index); |
| 1078 | if (err) |
| 1079 | return err; |
| 1080 | |
| 1081 | err = devlink_sb_th_type_get_from_info(info, &threshold_type); |
| 1082 | if (err) |
| 1083 | return err; |
| 1084 | |
| 1085 | if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]) |
| 1086 | return -EINVAL; |
| 1087 | |
| 1088 | size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]); |
| 1089 | return devlink_sb_pool_set(devlink, devlink_sb->index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1090 | pool_index, size, threshold_type, |
| 1091 | info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg, |
| 1095 | struct devlink *devlink, |
| 1096 | struct devlink_port *devlink_port, |
| 1097 | struct devlink_sb *devlink_sb, |
| 1098 | u16 pool_index, |
| 1099 | enum devlink_command cmd, |
| 1100 | u32 portid, u32 seq, int flags) |
| 1101 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1102 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1103 | u32 threshold; |
| 1104 | void *hdr; |
| 1105 | int err; |
| 1106 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1107 | err = ops->sb_port_pool_get(devlink_port, devlink_sb->index, |
| 1108 | pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1109 | if (err) |
| 1110 | return err; |
| 1111 | |
| 1112 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1113 | if (!hdr) |
| 1114 | return -EMSGSIZE; |
| 1115 | |
| 1116 | if (devlink_nl_put_handle(msg, devlink)) |
| 1117 | goto nla_put_failure; |
| 1118 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1119 | goto nla_put_failure; |
| 1120 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1121 | goto nla_put_failure; |
| 1122 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1123 | goto nla_put_failure; |
| 1124 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1125 | goto nla_put_failure; |
| 1126 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1127 | if (ops->sb_occ_port_pool_get) { |
| 1128 | u32 cur; |
| 1129 | u32 max; |
| 1130 | |
| 1131 | err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index, |
| 1132 | pool_index, &cur, &max); |
| 1133 | if (err && err != -EOPNOTSUPP) |
| 1134 | return err; |
| 1135 | if (!err) { |
| 1136 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1137 | goto nla_put_failure; |
| 1138 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1139 | goto nla_put_failure; |
| 1140 | } |
| 1141 | } |
| 1142 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1143 | genlmsg_end(msg, hdr); |
| 1144 | return 0; |
| 1145 | |
| 1146 | nla_put_failure: |
| 1147 | genlmsg_cancel(msg, hdr); |
| 1148 | return -EMSGSIZE; |
| 1149 | } |
| 1150 | |
| 1151 | static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb, |
| 1152 | struct genl_info *info) |
| 1153 | { |
| 1154 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1155 | struct devlink *devlink = devlink_port->devlink; |
| 1156 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1157 | struct sk_buff *msg; |
| 1158 | u16 pool_index; |
| 1159 | int err; |
| 1160 | |
| 1161 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1162 | &pool_index); |
| 1163 | if (err) |
| 1164 | return err; |
| 1165 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1166 | if (!devlink->ops->sb_port_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1167 | return -EOPNOTSUPP; |
| 1168 | |
| 1169 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1170 | if (!msg) |
| 1171 | return -ENOMEM; |
| 1172 | |
| 1173 | err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port, |
| 1174 | devlink_sb, pool_index, |
| 1175 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1176 | info->snd_portid, info->snd_seq, 0); |
| 1177 | if (err) { |
| 1178 | nlmsg_free(msg); |
| 1179 | return err; |
| 1180 | } |
| 1181 | |
| 1182 | return genlmsg_reply(msg, info); |
| 1183 | } |
| 1184 | |
| 1185 | static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1186 | struct devlink *devlink, |
| 1187 | struct devlink_sb *devlink_sb, |
| 1188 | u32 portid, u32 seq) |
| 1189 | { |
| 1190 | struct devlink_port *devlink_port; |
| 1191 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1192 | u16 pool_index; |
| 1193 | int err; |
| 1194 | |
| 1195 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1196 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1197 | if (*p_idx < start) { |
| 1198 | (*p_idx)++; |
| 1199 | continue; |
| 1200 | } |
| 1201 | err = devlink_nl_sb_port_pool_fill(msg, devlink, |
| 1202 | devlink_port, |
| 1203 | devlink_sb, |
| 1204 | pool_index, |
| 1205 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1206 | portid, seq, |
| 1207 | NLM_F_MULTI); |
| 1208 | if (err) |
| 1209 | return err; |
| 1210 | (*p_idx)++; |
| 1211 | } |
| 1212 | } |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg, |
| 1217 | struct netlink_callback *cb) |
| 1218 | { |
| 1219 | struct devlink *devlink; |
| 1220 | struct devlink_sb *devlink_sb; |
| 1221 | int start = cb->args[0]; |
| 1222 | int idx = 0; |
| 1223 | int err; |
| 1224 | |
| 1225 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1226 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1227 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1228 | !devlink->ops->sb_port_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1229 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1230 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1231 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1232 | err = __sb_port_pool_get_dumpit(msg, start, &idx, |
| 1233 | devlink, devlink_sb, |
| 1234 | NETLINK_CB(cb->skb).portid, |
| 1235 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1236 | if (err && err != -EOPNOTSUPP) { |
| 1237 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1238 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1239 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1240 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1241 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1242 | } |
| 1243 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1244 | mutex_unlock(&devlink_mutex); |
| 1245 | |
| 1246 | cb->args[0] = idx; |
| 1247 | return msg->len; |
| 1248 | } |
| 1249 | |
| 1250 | static int devlink_sb_port_pool_set(struct devlink_port *devlink_port, |
| 1251 | unsigned int sb_index, u16 pool_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1252 | u32 threshold, |
| 1253 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1254 | |
| 1255 | { |
| 1256 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1257 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1258 | if (ops->sb_port_pool_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1259 | return ops->sb_port_pool_set(devlink_port, sb_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1260 | pool_index, threshold, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1261 | return -EOPNOTSUPP; |
| 1262 | } |
| 1263 | |
| 1264 | static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb, |
| 1265 | struct genl_info *info) |
| 1266 | { |
| 1267 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1268 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1269 | u16 pool_index; |
| 1270 | u32 threshold; |
| 1271 | int err; |
| 1272 | |
| 1273 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1274 | &pool_index); |
| 1275 | if (err) |
| 1276 | return err; |
| 1277 | |
| 1278 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1279 | return -EINVAL; |
| 1280 | |
| 1281 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1282 | return devlink_sb_port_pool_set(devlink_port, devlink_sb->index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1283 | pool_index, threshold, info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | static int |
| 1287 | devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1288 | struct devlink_port *devlink_port, |
| 1289 | struct devlink_sb *devlink_sb, u16 tc_index, |
| 1290 | enum devlink_sb_pool_type pool_type, |
| 1291 | enum devlink_command cmd, |
| 1292 | u32 portid, u32 seq, int flags) |
| 1293 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1294 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1295 | u16 pool_index; |
| 1296 | u32 threshold; |
| 1297 | void *hdr; |
| 1298 | int err; |
| 1299 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1300 | err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index, |
| 1301 | tc_index, pool_type, |
| 1302 | &pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1303 | if (err) |
| 1304 | return err; |
| 1305 | |
| 1306 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1307 | if (!hdr) |
| 1308 | return -EMSGSIZE; |
| 1309 | |
| 1310 | if (devlink_nl_put_handle(msg, devlink)) |
| 1311 | goto nla_put_failure; |
| 1312 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1313 | goto nla_put_failure; |
| 1314 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1315 | goto nla_put_failure; |
| 1316 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index)) |
| 1317 | goto nla_put_failure; |
| 1318 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type)) |
| 1319 | goto nla_put_failure; |
| 1320 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1321 | goto nla_put_failure; |
| 1322 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1323 | goto nla_put_failure; |
| 1324 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1325 | if (ops->sb_occ_tc_port_bind_get) { |
| 1326 | u32 cur; |
| 1327 | u32 max; |
| 1328 | |
| 1329 | err = ops->sb_occ_tc_port_bind_get(devlink_port, |
| 1330 | devlink_sb->index, |
| 1331 | tc_index, pool_type, |
| 1332 | &cur, &max); |
| 1333 | if (err && err != -EOPNOTSUPP) |
| 1334 | return err; |
| 1335 | if (!err) { |
| 1336 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1337 | goto nla_put_failure; |
| 1338 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1339 | goto nla_put_failure; |
| 1340 | } |
| 1341 | } |
| 1342 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1343 | genlmsg_end(msg, hdr); |
| 1344 | return 0; |
| 1345 | |
| 1346 | nla_put_failure: |
| 1347 | genlmsg_cancel(msg, hdr); |
| 1348 | return -EMSGSIZE; |
| 1349 | } |
| 1350 | |
| 1351 | static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb, |
| 1352 | struct genl_info *info) |
| 1353 | { |
| 1354 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1355 | struct devlink *devlink = devlink_port->devlink; |
| 1356 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1357 | struct sk_buff *msg; |
| 1358 | enum devlink_sb_pool_type pool_type; |
| 1359 | u16 tc_index; |
| 1360 | int err; |
| 1361 | |
| 1362 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1363 | if (err) |
| 1364 | return err; |
| 1365 | |
| 1366 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1367 | pool_type, &tc_index); |
| 1368 | if (err) |
| 1369 | return err; |
| 1370 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1371 | if (!devlink->ops->sb_tc_pool_bind_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1372 | return -EOPNOTSUPP; |
| 1373 | |
| 1374 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1375 | if (!msg) |
| 1376 | return -ENOMEM; |
| 1377 | |
| 1378 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port, |
| 1379 | devlink_sb, tc_index, pool_type, |
| 1380 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1381 | info->snd_portid, |
| 1382 | info->snd_seq, 0); |
| 1383 | if (err) { |
| 1384 | nlmsg_free(msg); |
| 1385 | return err; |
| 1386 | } |
| 1387 | |
| 1388 | return genlmsg_reply(msg, info); |
| 1389 | } |
| 1390 | |
| 1391 | static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1392 | int start, int *p_idx, |
| 1393 | struct devlink *devlink, |
| 1394 | struct devlink_sb *devlink_sb, |
| 1395 | u32 portid, u32 seq) |
| 1396 | { |
| 1397 | struct devlink_port *devlink_port; |
| 1398 | u16 tc_index; |
| 1399 | int err; |
| 1400 | |
| 1401 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1402 | for (tc_index = 0; |
| 1403 | tc_index < devlink_sb->ingress_tc_count; tc_index++) { |
| 1404 | if (*p_idx < start) { |
| 1405 | (*p_idx)++; |
| 1406 | continue; |
| 1407 | } |
| 1408 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1409 | devlink_port, |
| 1410 | devlink_sb, |
| 1411 | tc_index, |
| 1412 | DEVLINK_SB_POOL_TYPE_INGRESS, |
| 1413 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1414 | portid, seq, |
| 1415 | NLM_F_MULTI); |
| 1416 | if (err) |
| 1417 | return err; |
| 1418 | (*p_idx)++; |
| 1419 | } |
| 1420 | for (tc_index = 0; |
| 1421 | tc_index < devlink_sb->egress_tc_count; tc_index++) { |
| 1422 | if (*p_idx < start) { |
| 1423 | (*p_idx)++; |
| 1424 | continue; |
| 1425 | } |
| 1426 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1427 | devlink_port, |
| 1428 | devlink_sb, |
| 1429 | tc_index, |
| 1430 | DEVLINK_SB_POOL_TYPE_EGRESS, |
| 1431 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1432 | portid, seq, |
| 1433 | NLM_F_MULTI); |
| 1434 | if (err) |
| 1435 | return err; |
| 1436 | (*p_idx)++; |
| 1437 | } |
| 1438 | } |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | static int |
| 1443 | devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1444 | struct netlink_callback *cb) |
| 1445 | { |
| 1446 | struct devlink *devlink; |
| 1447 | struct devlink_sb *devlink_sb; |
| 1448 | int start = cb->args[0]; |
| 1449 | int idx = 0; |
| 1450 | int err; |
| 1451 | |
| 1452 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1453 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1454 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1455 | !devlink->ops->sb_tc_pool_bind_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1456 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1457 | |
| 1458 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1459 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1460 | err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx, |
| 1461 | devlink, |
| 1462 | devlink_sb, |
| 1463 | NETLINK_CB(cb->skb).portid, |
| 1464 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1465 | if (err && err != -EOPNOTSUPP) { |
| 1466 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1467 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1468 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1469 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1470 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1471 | } |
| 1472 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1473 | mutex_unlock(&devlink_mutex); |
| 1474 | |
| 1475 | cb->args[0] = idx; |
| 1476 | return msg->len; |
| 1477 | } |
| 1478 | |
| 1479 | static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port, |
| 1480 | unsigned int sb_index, u16 tc_index, |
| 1481 | enum devlink_sb_pool_type pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1482 | u16 pool_index, u32 threshold, |
| 1483 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1484 | |
| 1485 | { |
| 1486 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1487 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1488 | if (ops->sb_tc_pool_bind_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1489 | return ops->sb_tc_pool_bind_set(devlink_port, sb_index, |
| 1490 | tc_index, pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1491 | pool_index, threshold, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1492 | return -EOPNOTSUPP; |
| 1493 | } |
| 1494 | |
| 1495 | static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb, |
| 1496 | struct genl_info *info) |
| 1497 | { |
| 1498 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1499 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1500 | enum devlink_sb_pool_type pool_type; |
| 1501 | u16 tc_index; |
| 1502 | u16 pool_index; |
| 1503 | u32 threshold; |
| 1504 | int err; |
| 1505 | |
| 1506 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1507 | if (err) |
| 1508 | return err; |
| 1509 | |
| 1510 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1511 | pool_type, &tc_index); |
| 1512 | if (err) |
| 1513 | return err; |
| 1514 | |
| 1515 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1516 | &pool_index); |
| 1517 | if (err) |
| 1518 | return err; |
| 1519 | |
| 1520 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1521 | return -EINVAL; |
| 1522 | |
| 1523 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1524 | return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index, |
| 1525 | tc_index, pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1526 | pool_index, threshold, info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1527 | } |
| 1528 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1529 | static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb, |
| 1530 | struct genl_info *info) |
| 1531 | { |
| 1532 | struct devlink *devlink = info->user_ptr[0]; |
| 1533 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1534 | const struct devlink_ops *ops = devlink->ops; |
| 1535 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1536 | if (ops->sb_occ_snapshot) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1537 | return ops->sb_occ_snapshot(devlink, devlink_sb->index); |
| 1538 | return -EOPNOTSUPP; |
| 1539 | } |
| 1540 | |
| 1541 | static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb, |
| 1542 | struct genl_info *info) |
| 1543 | { |
| 1544 | struct devlink *devlink = info->user_ptr[0]; |
| 1545 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1546 | const struct devlink_ops *ops = devlink->ops; |
| 1547 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1548 | if (ops->sb_occ_max_clear) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1549 | return ops->sb_occ_max_clear(devlink, devlink_sb->index); |
| 1550 | return -EOPNOTSUPP; |
| 1551 | } |
| 1552 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1553 | static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1554 | enum devlink_command cmd, u32 portid, |
| 1555 | u32 seq, int flags) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1556 | { |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1557 | const struct devlink_ops *ops = devlink->ops; |
Leon Romanovsky | 98fdbea | 2019-06-12 15:20:11 +0300 | [diff] [blame] | 1558 | enum devlink_eswitch_encap_mode encap_mode; |
| 1559 | u8 inline_mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1560 | void *hdr; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1561 | int err = 0; |
| 1562 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1563 | |
| 1564 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1565 | if (!hdr) |
| 1566 | return -EMSGSIZE; |
| 1567 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1568 | err = devlink_nl_put_handle(msg, devlink); |
| 1569 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1570 | goto nla_put_failure; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1571 | |
Jiri Pirko | 4456f61 | 2017-02-09 15:54:36 +0100 | [diff] [blame] | 1572 | if (ops->eswitch_mode_get) { |
| 1573 | err = ops->eswitch_mode_get(devlink, &mode); |
| 1574 | if (err) |
| 1575 | goto nla_put_failure; |
| 1576 | err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode); |
| 1577 | if (err) |
| 1578 | goto nla_put_failure; |
| 1579 | } |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1580 | |
| 1581 | if (ops->eswitch_inline_mode_get) { |
| 1582 | err = ops->eswitch_inline_mode_get(devlink, &inline_mode); |
| 1583 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1584 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1585 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE, |
| 1586 | inline_mode); |
| 1587 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1588 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1589 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1590 | |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1591 | if (ops->eswitch_encap_mode_get) { |
| 1592 | err = ops->eswitch_encap_mode_get(devlink, &encap_mode); |
| 1593 | if (err) |
| 1594 | goto nla_put_failure; |
| 1595 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode); |
| 1596 | if (err) |
| 1597 | goto nla_put_failure; |
| 1598 | } |
| 1599 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1600 | genlmsg_end(msg, hdr); |
| 1601 | return 0; |
| 1602 | |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1603 | nla_put_failure: |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1604 | genlmsg_cancel(msg, hdr); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1605 | return err; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1606 | } |
| 1607 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1608 | static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, |
| 1609 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1610 | { |
| 1611 | struct devlink *devlink = info->user_ptr[0]; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1612 | struct sk_buff *msg; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1613 | int err; |
| 1614 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1615 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1616 | if (!msg) |
| 1617 | return -ENOMEM; |
| 1618 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1619 | err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET, |
| 1620 | info->snd_portid, info->snd_seq, 0); |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1621 | |
| 1622 | if (err) { |
| 1623 | nlmsg_free(msg); |
| 1624 | return err; |
| 1625 | } |
| 1626 | |
| 1627 | return genlmsg_reply(msg, info); |
| 1628 | } |
| 1629 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1630 | static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, |
| 1631 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1632 | { |
| 1633 | struct devlink *devlink = info->user_ptr[0]; |
| 1634 | const struct devlink_ops *ops = devlink->ops; |
Leon Romanovsky | 98fdbea | 2019-06-12 15:20:11 +0300 | [diff] [blame] | 1635 | enum devlink_eswitch_encap_mode encap_mode; |
| 1636 | u8 inline_mode; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1637 | int err = 0; |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1638 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1639 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1640 | if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) { |
| 1641 | if (!ops->eswitch_mode_set) |
| 1642 | return -EOPNOTSUPP; |
| 1643 | mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1644 | err = ops->eswitch_mode_set(devlink, mode, info->extack); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1645 | if (err) |
| 1646 | return err; |
| 1647 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1648 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1649 | if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) { |
| 1650 | if (!ops->eswitch_inline_mode_set) |
| 1651 | return -EOPNOTSUPP; |
| 1652 | inline_mode = nla_get_u8( |
| 1653 | info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1654 | err = ops->eswitch_inline_mode_set(devlink, inline_mode, |
| 1655 | info->extack); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1656 | if (err) |
| 1657 | return err; |
| 1658 | } |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1659 | |
| 1660 | if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) { |
| 1661 | if (!ops->eswitch_encap_mode_set) |
| 1662 | return -EOPNOTSUPP; |
| 1663 | encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1664 | err = ops->eswitch_encap_mode_set(devlink, encap_mode, |
| 1665 | info->extack); |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1666 | if (err) |
| 1667 | return err; |
| 1668 | } |
| 1669 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1670 | return 0; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1671 | } |
| 1672 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1673 | int devlink_dpipe_match_put(struct sk_buff *skb, |
| 1674 | struct devlink_dpipe_match *match) |
| 1675 | { |
| 1676 | struct devlink_dpipe_header *header = match->header; |
| 1677 | struct devlink_dpipe_field *field = &header->fields[match->field_id]; |
| 1678 | struct nlattr *match_attr; |
| 1679 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1680 | match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1681 | if (!match_attr) |
| 1682 | return -EMSGSIZE; |
| 1683 | |
| 1684 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) || |
| 1685 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) || |
| 1686 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1687 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1688 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1689 | goto nla_put_failure; |
| 1690 | |
| 1691 | nla_nest_end(skb, match_attr); |
| 1692 | return 0; |
| 1693 | |
| 1694 | nla_put_failure: |
| 1695 | nla_nest_cancel(skb, match_attr); |
| 1696 | return -EMSGSIZE; |
| 1697 | } |
| 1698 | EXPORT_SYMBOL_GPL(devlink_dpipe_match_put); |
| 1699 | |
| 1700 | static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table, |
| 1701 | struct sk_buff *skb) |
| 1702 | { |
| 1703 | struct nlattr *matches_attr; |
| 1704 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1705 | matches_attr = nla_nest_start_noflag(skb, |
| 1706 | DEVLINK_ATTR_DPIPE_TABLE_MATCHES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1707 | if (!matches_attr) |
| 1708 | return -EMSGSIZE; |
| 1709 | |
| 1710 | if (table->table_ops->matches_dump(table->priv, skb)) |
| 1711 | goto nla_put_failure; |
| 1712 | |
| 1713 | nla_nest_end(skb, matches_attr); |
| 1714 | return 0; |
| 1715 | |
| 1716 | nla_put_failure: |
| 1717 | nla_nest_cancel(skb, matches_attr); |
| 1718 | return -EMSGSIZE; |
| 1719 | } |
| 1720 | |
| 1721 | int devlink_dpipe_action_put(struct sk_buff *skb, |
| 1722 | struct devlink_dpipe_action *action) |
| 1723 | { |
| 1724 | struct devlink_dpipe_header *header = action->header; |
| 1725 | struct devlink_dpipe_field *field = &header->fields[action->field_id]; |
| 1726 | struct nlattr *action_attr; |
| 1727 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1728 | action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1729 | if (!action_attr) |
| 1730 | return -EMSGSIZE; |
| 1731 | |
| 1732 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) || |
| 1733 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) || |
| 1734 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1735 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1736 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1737 | goto nla_put_failure; |
| 1738 | |
| 1739 | nla_nest_end(skb, action_attr); |
| 1740 | return 0; |
| 1741 | |
| 1742 | nla_put_failure: |
| 1743 | nla_nest_cancel(skb, action_attr); |
| 1744 | return -EMSGSIZE; |
| 1745 | } |
| 1746 | EXPORT_SYMBOL_GPL(devlink_dpipe_action_put); |
| 1747 | |
| 1748 | static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table, |
| 1749 | struct sk_buff *skb) |
| 1750 | { |
| 1751 | struct nlattr *actions_attr; |
| 1752 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1753 | actions_attr = nla_nest_start_noflag(skb, |
| 1754 | DEVLINK_ATTR_DPIPE_TABLE_ACTIONS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1755 | if (!actions_attr) |
| 1756 | return -EMSGSIZE; |
| 1757 | |
| 1758 | if (table->table_ops->actions_dump(table->priv, skb)) |
| 1759 | goto nla_put_failure; |
| 1760 | |
| 1761 | nla_nest_end(skb, actions_attr); |
| 1762 | return 0; |
| 1763 | |
| 1764 | nla_put_failure: |
| 1765 | nla_nest_cancel(skb, actions_attr); |
| 1766 | return -EMSGSIZE; |
| 1767 | } |
| 1768 | |
| 1769 | static int devlink_dpipe_table_put(struct sk_buff *skb, |
| 1770 | struct devlink_dpipe_table *table) |
| 1771 | { |
| 1772 | struct nlattr *table_attr; |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1773 | u64 table_size; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1774 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1775 | table_size = table->table_ops->size_get(table->priv); |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1776 | table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1777 | if (!table_attr) |
| 1778 | return -EMSGSIZE; |
| 1779 | |
| 1780 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) || |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1781 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1782 | DEVLINK_ATTR_PAD)) |
| 1783 | goto nla_put_failure; |
| 1784 | if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED, |
| 1785 | table->counters_enabled)) |
| 1786 | goto nla_put_failure; |
| 1787 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1788 | if (table->resource_valid) { |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 1789 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID, |
| 1790 | table->resource_id, DEVLINK_ATTR_PAD) || |
| 1791 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS, |
| 1792 | table->resource_units, DEVLINK_ATTR_PAD)) |
| 1793 | goto nla_put_failure; |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1794 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1795 | if (devlink_dpipe_matches_put(table, skb)) |
| 1796 | goto nla_put_failure; |
| 1797 | |
| 1798 | if (devlink_dpipe_actions_put(table, skb)) |
| 1799 | goto nla_put_failure; |
| 1800 | |
| 1801 | nla_nest_end(skb, table_attr); |
| 1802 | return 0; |
| 1803 | |
| 1804 | nla_put_failure: |
| 1805 | nla_nest_cancel(skb, table_attr); |
| 1806 | return -EMSGSIZE; |
| 1807 | } |
| 1808 | |
| 1809 | static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb, |
| 1810 | struct genl_info *info) |
| 1811 | { |
| 1812 | int err; |
| 1813 | |
| 1814 | if (*pskb) { |
| 1815 | err = genlmsg_reply(*pskb, info); |
| 1816 | if (err) |
| 1817 | return err; |
| 1818 | } |
| 1819 | *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1820 | if (!*pskb) |
| 1821 | return -ENOMEM; |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
| 1825 | static int devlink_dpipe_tables_fill(struct genl_info *info, |
| 1826 | enum devlink_command cmd, int flags, |
| 1827 | struct list_head *dpipe_tables, |
| 1828 | const char *table_name) |
| 1829 | { |
| 1830 | struct devlink *devlink = info->user_ptr[0]; |
| 1831 | struct devlink_dpipe_table *table; |
| 1832 | struct nlattr *tables_attr; |
| 1833 | struct sk_buff *skb = NULL; |
| 1834 | struct nlmsghdr *nlh; |
| 1835 | bool incomplete; |
| 1836 | void *hdr; |
| 1837 | int i; |
| 1838 | int err; |
| 1839 | |
| 1840 | table = list_first_entry(dpipe_tables, |
| 1841 | struct devlink_dpipe_table, list); |
| 1842 | start_again: |
| 1843 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1844 | if (err) |
| 1845 | return err; |
| 1846 | |
| 1847 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1848 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 1849 | if (!hdr) { |
| 1850 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1851 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 1852 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1853 | |
| 1854 | if (devlink_nl_put_handle(skb, devlink)) |
| 1855 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1856 | tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1857 | if (!tables_attr) |
| 1858 | goto nla_put_failure; |
| 1859 | |
| 1860 | i = 0; |
| 1861 | incomplete = false; |
| 1862 | list_for_each_entry_from(table, dpipe_tables, list) { |
| 1863 | if (!table_name) { |
| 1864 | err = devlink_dpipe_table_put(skb, table); |
| 1865 | if (err) { |
| 1866 | if (!i) |
| 1867 | goto err_table_put; |
| 1868 | incomplete = true; |
| 1869 | break; |
| 1870 | } |
| 1871 | } else { |
| 1872 | if (!strcmp(table->name, table_name)) { |
| 1873 | err = devlink_dpipe_table_put(skb, table); |
| 1874 | if (err) |
| 1875 | break; |
| 1876 | } |
| 1877 | } |
| 1878 | i++; |
| 1879 | } |
| 1880 | |
| 1881 | nla_nest_end(skb, tables_attr); |
| 1882 | genlmsg_end(skb, hdr); |
| 1883 | if (incomplete) |
| 1884 | goto start_again; |
| 1885 | |
| 1886 | send_done: |
| 1887 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1888 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 1889 | if (!nlh) { |
| 1890 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1891 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 1892 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1893 | goto send_done; |
| 1894 | } |
| 1895 | |
| 1896 | return genlmsg_reply(skb, info); |
| 1897 | |
| 1898 | nla_put_failure: |
| 1899 | err = -EMSGSIZE; |
| 1900 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1901 | nlmsg_free(skb); |
| 1902 | return err; |
| 1903 | } |
| 1904 | |
| 1905 | static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb, |
| 1906 | struct genl_info *info) |
| 1907 | { |
| 1908 | struct devlink *devlink = info->user_ptr[0]; |
| 1909 | const char *table_name = NULL; |
| 1910 | |
| 1911 | if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 1912 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 1913 | |
| 1914 | return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0, |
| 1915 | &devlink->dpipe_table_list, |
| 1916 | table_name); |
| 1917 | } |
| 1918 | |
| 1919 | static int devlink_dpipe_value_put(struct sk_buff *skb, |
| 1920 | struct devlink_dpipe_value *value) |
| 1921 | { |
| 1922 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE, |
| 1923 | value->value_size, value->value)) |
| 1924 | return -EMSGSIZE; |
| 1925 | if (value->mask) |
| 1926 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK, |
| 1927 | value->value_size, value->mask)) |
| 1928 | return -EMSGSIZE; |
| 1929 | if (value->mapping_valid) |
| 1930 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING, |
| 1931 | value->mapping_value)) |
| 1932 | return -EMSGSIZE; |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
| 1936 | static int devlink_dpipe_action_value_put(struct sk_buff *skb, |
| 1937 | struct devlink_dpipe_value *value) |
| 1938 | { |
| 1939 | if (!value->action) |
| 1940 | return -EINVAL; |
| 1941 | if (devlink_dpipe_action_put(skb, value->action)) |
| 1942 | return -EMSGSIZE; |
| 1943 | if (devlink_dpipe_value_put(skb, value)) |
| 1944 | return -EMSGSIZE; |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
| 1948 | static int devlink_dpipe_action_values_put(struct sk_buff *skb, |
| 1949 | struct devlink_dpipe_value *values, |
| 1950 | unsigned int values_count) |
| 1951 | { |
| 1952 | struct nlattr *action_attr; |
| 1953 | int i; |
| 1954 | int err; |
| 1955 | |
| 1956 | for (i = 0; i < values_count; i++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1957 | action_attr = nla_nest_start_noflag(skb, |
| 1958 | DEVLINK_ATTR_DPIPE_ACTION_VALUE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1959 | if (!action_attr) |
| 1960 | return -EMSGSIZE; |
| 1961 | err = devlink_dpipe_action_value_put(skb, &values[i]); |
| 1962 | if (err) |
| 1963 | goto err_action_value_put; |
| 1964 | nla_nest_end(skb, action_attr); |
| 1965 | } |
| 1966 | return 0; |
| 1967 | |
| 1968 | err_action_value_put: |
| 1969 | nla_nest_cancel(skb, action_attr); |
| 1970 | return err; |
| 1971 | } |
| 1972 | |
| 1973 | static int devlink_dpipe_match_value_put(struct sk_buff *skb, |
| 1974 | struct devlink_dpipe_value *value) |
| 1975 | { |
| 1976 | if (!value->match) |
| 1977 | return -EINVAL; |
| 1978 | if (devlink_dpipe_match_put(skb, value->match)) |
| 1979 | return -EMSGSIZE; |
| 1980 | if (devlink_dpipe_value_put(skb, value)) |
| 1981 | return -EMSGSIZE; |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | static int devlink_dpipe_match_values_put(struct sk_buff *skb, |
| 1986 | struct devlink_dpipe_value *values, |
| 1987 | unsigned int values_count) |
| 1988 | { |
| 1989 | struct nlattr *match_attr; |
| 1990 | int i; |
| 1991 | int err; |
| 1992 | |
| 1993 | for (i = 0; i < values_count; i++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1994 | match_attr = nla_nest_start_noflag(skb, |
| 1995 | DEVLINK_ATTR_DPIPE_MATCH_VALUE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1996 | if (!match_attr) |
| 1997 | return -EMSGSIZE; |
| 1998 | err = devlink_dpipe_match_value_put(skb, &values[i]); |
| 1999 | if (err) |
| 2000 | goto err_match_value_put; |
| 2001 | nla_nest_end(skb, match_attr); |
| 2002 | } |
| 2003 | return 0; |
| 2004 | |
| 2005 | err_match_value_put: |
| 2006 | nla_nest_cancel(skb, match_attr); |
| 2007 | return err; |
| 2008 | } |
| 2009 | |
| 2010 | static int devlink_dpipe_entry_put(struct sk_buff *skb, |
| 2011 | struct devlink_dpipe_entry *entry) |
| 2012 | { |
| 2013 | struct nlattr *entry_attr, *matches_attr, *actions_attr; |
| 2014 | int err; |
| 2015 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2016 | entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2017 | if (!entry_attr) |
| 2018 | return -EMSGSIZE; |
| 2019 | |
| 2020 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index, |
| 2021 | DEVLINK_ATTR_PAD)) |
| 2022 | goto nla_put_failure; |
| 2023 | if (entry->counter_valid) |
| 2024 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER, |
| 2025 | entry->counter, DEVLINK_ATTR_PAD)) |
| 2026 | goto nla_put_failure; |
| 2027 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2028 | matches_attr = nla_nest_start_noflag(skb, |
| 2029 | DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2030 | if (!matches_attr) |
| 2031 | goto nla_put_failure; |
| 2032 | |
| 2033 | err = devlink_dpipe_match_values_put(skb, entry->match_values, |
| 2034 | entry->match_values_count); |
| 2035 | if (err) { |
| 2036 | nla_nest_cancel(skb, matches_attr); |
| 2037 | goto err_match_values_put; |
| 2038 | } |
| 2039 | nla_nest_end(skb, matches_attr); |
| 2040 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2041 | actions_attr = nla_nest_start_noflag(skb, |
| 2042 | DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2043 | if (!actions_attr) |
| 2044 | goto nla_put_failure; |
| 2045 | |
| 2046 | err = devlink_dpipe_action_values_put(skb, entry->action_values, |
| 2047 | entry->action_values_count); |
| 2048 | if (err) { |
| 2049 | nla_nest_cancel(skb, actions_attr); |
| 2050 | goto err_action_values_put; |
| 2051 | } |
| 2052 | nla_nest_end(skb, actions_attr); |
| 2053 | |
| 2054 | nla_nest_end(skb, entry_attr); |
| 2055 | return 0; |
| 2056 | |
| 2057 | nla_put_failure: |
| 2058 | err = -EMSGSIZE; |
| 2059 | err_match_values_put: |
| 2060 | err_action_values_put: |
| 2061 | nla_nest_cancel(skb, entry_attr); |
| 2062 | return err; |
| 2063 | } |
| 2064 | |
| 2065 | static struct devlink_dpipe_table * |
| 2066 | devlink_dpipe_table_find(struct list_head *dpipe_tables, |
| 2067 | const char *table_name) |
| 2068 | { |
| 2069 | struct devlink_dpipe_table *table; |
| 2070 | |
| 2071 | list_for_each_entry_rcu(table, dpipe_tables, list) { |
| 2072 | if (!strcmp(table->name, table_name)) |
| 2073 | return table; |
| 2074 | } |
| 2075 | return NULL; |
| 2076 | } |
| 2077 | |
| 2078 | int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2079 | { |
| 2080 | struct devlink *devlink; |
| 2081 | int err; |
| 2082 | |
| 2083 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb, |
| 2084 | dump_ctx->info); |
| 2085 | if (err) |
| 2086 | return err; |
| 2087 | |
| 2088 | dump_ctx->hdr = genlmsg_put(dump_ctx->skb, |
| 2089 | dump_ctx->info->snd_portid, |
| 2090 | dump_ctx->info->snd_seq, |
| 2091 | &devlink_nl_family, NLM_F_MULTI, |
| 2092 | dump_ctx->cmd); |
| 2093 | if (!dump_ctx->hdr) |
| 2094 | goto nla_put_failure; |
| 2095 | |
| 2096 | devlink = dump_ctx->info->user_ptr[0]; |
| 2097 | if (devlink_nl_put_handle(dump_ctx->skb, devlink)) |
| 2098 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2099 | dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb, |
| 2100 | DEVLINK_ATTR_DPIPE_ENTRIES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2101 | if (!dump_ctx->nest) |
| 2102 | goto nla_put_failure; |
| 2103 | return 0; |
| 2104 | |
| 2105 | nla_put_failure: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2106 | nlmsg_free(dump_ctx->skb); |
| 2107 | return -EMSGSIZE; |
| 2108 | } |
| 2109 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare); |
| 2110 | |
| 2111 | int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, |
| 2112 | struct devlink_dpipe_entry *entry) |
| 2113 | { |
| 2114 | return devlink_dpipe_entry_put(dump_ctx->skb, entry); |
| 2115 | } |
| 2116 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append); |
| 2117 | |
| 2118 | int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2119 | { |
| 2120 | nla_nest_end(dump_ctx->skb, dump_ctx->nest); |
| 2121 | genlmsg_end(dump_ctx->skb, dump_ctx->hdr); |
| 2122 | return 0; |
| 2123 | } |
| 2124 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close); |
| 2125 | |
Arkadi Sharshevsky | 3580732 | 2017-08-24 08:40:03 +0200 | [diff] [blame] | 2126 | void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry) |
| 2127 | |
| 2128 | { |
| 2129 | unsigned int value_count, value_index; |
| 2130 | struct devlink_dpipe_value *value; |
| 2131 | |
| 2132 | value = entry->action_values; |
| 2133 | value_count = entry->action_values_count; |
| 2134 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2135 | kfree(value[value_index].value); |
| 2136 | kfree(value[value_index].mask); |
| 2137 | } |
| 2138 | |
| 2139 | value = entry->match_values; |
| 2140 | value_count = entry->match_values_count; |
| 2141 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2142 | kfree(value[value_index].value); |
| 2143 | kfree(value[value_index].mask); |
| 2144 | } |
| 2145 | } |
| 2146 | EXPORT_SYMBOL(devlink_dpipe_entry_clear); |
| 2147 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2148 | static int devlink_dpipe_entries_fill(struct genl_info *info, |
| 2149 | enum devlink_command cmd, int flags, |
| 2150 | struct devlink_dpipe_table *table) |
| 2151 | { |
| 2152 | struct devlink_dpipe_dump_ctx dump_ctx; |
| 2153 | struct nlmsghdr *nlh; |
| 2154 | int err; |
| 2155 | |
| 2156 | dump_ctx.skb = NULL; |
| 2157 | dump_ctx.cmd = cmd; |
| 2158 | dump_ctx.info = info; |
| 2159 | |
| 2160 | err = table->table_ops->entries_dump(table->priv, |
| 2161 | table->counters_enabled, |
| 2162 | &dump_ctx); |
| 2163 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2164 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2165 | |
| 2166 | send_done: |
| 2167 | nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq, |
| 2168 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2169 | if (!nlh) { |
| 2170 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info); |
| 2171 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2172 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2173 | goto send_done; |
| 2174 | } |
| 2175 | return genlmsg_reply(dump_ctx.skb, info); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb, |
| 2179 | struct genl_info *info) |
| 2180 | { |
| 2181 | struct devlink *devlink = info->user_ptr[0]; |
| 2182 | struct devlink_dpipe_table *table; |
| 2183 | const char *table_name; |
| 2184 | |
| 2185 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 2186 | return -EINVAL; |
| 2187 | |
| 2188 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2189 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2190 | table_name); |
| 2191 | if (!table) |
| 2192 | return -EINVAL; |
| 2193 | |
| 2194 | if (!table->table_ops->entries_dump) |
| 2195 | return -EINVAL; |
| 2196 | |
| 2197 | return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET, |
| 2198 | 0, table); |
| 2199 | } |
| 2200 | |
| 2201 | static int devlink_dpipe_fields_put(struct sk_buff *skb, |
| 2202 | const struct devlink_dpipe_header *header) |
| 2203 | { |
| 2204 | struct devlink_dpipe_field *field; |
| 2205 | struct nlattr *field_attr; |
| 2206 | int i; |
| 2207 | |
| 2208 | for (i = 0; i < header->fields_count; i++) { |
| 2209 | field = &header->fields[i]; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2210 | field_attr = nla_nest_start_noflag(skb, |
| 2211 | DEVLINK_ATTR_DPIPE_FIELD); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2212 | if (!field_attr) |
| 2213 | return -EMSGSIZE; |
| 2214 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) || |
| 2215 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 2216 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) || |
| 2217 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type)) |
| 2218 | goto nla_put_failure; |
| 2219 | nla_nest_end(skb, field_attr); |
| 2220 | } |
| 2221 | return 0; |
| 2222 | |
| 2223 | nla_put_failure: |
| 2224 | nla_nest_cancel(skb, field_attr); |
| 2225 | return -EMSGSIZE; |
| 2226 | } |
| 2227 | |
| 2228 | static int devlink_dpipe_header_put(struct sk_buff *skb, |
| 2229 | struct devlink_dpipe_header *header) |
| 2230 | { |
| 2231 | struct nlattr *fields_attr, *header_attr; |
| 2232 | int err; |
| 2233 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2234 | header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER); |
Wei Yongjun | cb6bf9c | 2017-04-11 16:02:02 +0000 | [diff] [blame] | 2235 | if (!header_attr) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2236 | return -EMSGSIZE; |
| 2237 | |
| 2238 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) || |
| 2239 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 2240 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 2241 | goto nla_put_failure; |
| 2242 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2243 | fields_attr = nla_nest_start_noflag(skb, |
| 2244 | DEVLINK_ATTR_DPIPE_HEADER_FIELDS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2245 | if (!fields_attr) |
| 2246 | goto nla_put_failure; |
| 2247 | |
| 2248 | err = devlink_dpipe_fields_put(skb, header); |
| 2249 | if (err) { |
| 2250 | nla_nest_cancel(skb, fields_attr); |
| 2251 | goto nla_put_failure; |
| 2252 | } |
| 2253 | nla_nest_end(skb, fields_attr); |
| 2254 | nla_nest_end(skb, header_attr); |
| 2255 | return 0; |
| 2256 | |
| 2257 | nla_put_failure: |
| 2258 | err = -EMSGSIZE; |
| 2259 | nla_nest_cancel(skb, header_attr); |
| 2260 | return err; |
| 2261 | } |
| 2262 | |
| 2263 | static int devlink_dpipe_headers_fill(struct genl_info *info, |
| 2264 | enum devlink_command cmd, int flags, |
| 2265 | struct devlink_dpipe_headers * |
| 2266 | dpipe_headers) |
| 2267 | { |
| 2268 | struct devlink *devlink = info->user_ptr[0]; |
| 2269 | struct nlattr *headers_attr; |
| 2270 | struct sk_buff *skb = NULL; |
| 2271 | struct nlmsghdr *nlh; |
| 2272 | void *hdr; |
| 2273 | int i, j; |
| 2274 | int err; |
| 2275 | |
| 2276 | i = 0; |
| 2277 | start_again: |
| 2278 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2279 | if (err) |
| 2280 | return err; |
| 2281 | |
| 2282 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2283 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2284 | if (!hdr) { |
| 2285 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2286 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2287 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2288 | |
| 2289 | if (devlink_nl_put_handle(skb, devlink)) |
| 2290 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2291 | headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2292 | if (!headers_attr) |
| 2293 | goto nla_put_failure; |
| 2294 | |
| 2295 | j = 0; |
| 2296 | for (; i < dpipe_headers->headers_count; i++) { |
| 2297 | err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]); |
| 2298 | if (err) { |
| 2299 | if (!j) |
| 2300 | goto err_table_put; |
| 2301 | break; |
| 2302 | } |
| 2303 | j++; |
| 2304 | } |
| 2305 | nla_nest_end(skb, headers_attr); |
| 2306 | genlmsg_end(skb, hdr); |
| 2307 | if (i != dpipe_headers->headers_count) |
| 2308 | goto start_again; |
| 2309 | |
| 2310 | send_done: |
| 2311 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2312 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2313 | if (!nlh) { |
| 2314 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2315 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2316 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2317 | goto send_done; |
| 2318 | } |
| 2319 | return genlmsg_reply(skb, info); |
| 2320 | |
| 2321 | nla_put_failure: |
| 2322 | err = -EMSGSIZE; |
| 2323 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2324 | nlmsg_free(skb); |
| 2325 | return err; |
| 2326 | } |
| 2327 | |
| 2328 | static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb, |
| 2329 | struct genl_info *info) |
| 2330 | { |
| 2331 | struct devlink *devlink = info->user_ptr[0]; |
| 2332 | |
| 2333 | if (!devlink->dpipe_headers) |
| 2334 | return -EOPNOTSUPP; |
| 2335 | return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET, |
| 2336 | 0, devlink->dpipe_headers); |
| 2337 | } |
| 2338 | |
| 2339 | static int devlink_dpipe_table_counters_set(struct devlink *devlink, |
| 2340 | const char *table_name, |
| 2341 | bool enable) |
| 2342 | { |
| 2343 | struct devlink_dpipe_table *table; |
| 2344 | |
| 2345 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2346 | table_name); |
| 2347 | if (!table) |
| 2348 | return -EINVAL; |
| 2349 | |
| 2350 | if (table->counter_control_extern) |
| 2351 | return -EOPNOTSUPP; |
| 2352 | |
| 2353 | if (!(table->counters_enabled ^ enable)) |
| 2354 | return 0; |
| 2355 | |
| 2356 | table->counters_enabled = enable; |
| 2357 | if (table->table_ops->counters_set_update) |
| 2358 | table->table_ops->counters_set_update(table->priv, enable); |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
| 2362 | static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb, |
| 2363 | struct genl_info *info) |
| 2364 | { |
| 2365 | struct devlink *devlink = info->user_ptr[0]; |
| 2366 | const char *table_name; |
| 2367 | bool counters_enable; |
| 2368 | |
| 2369 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] || |
| 2370 | !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) |
| 2371 | return -EINVAL; |
| 2372 | |
| 2373 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2374 | counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]); |
| 2375 | |
| 2376 | return devlink_dpipe_table_counters_set(devlink, table_name, |
| 2377 | counters_enable); |
| 2378 | } |
| 2379 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2380 | static struct devlink_resource * |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2381 | devlink_resource_find(struct devlink *devlink, |
| 2382 | struct devlink_resource *resource, u64 resource_id) |
| 2383 | { |
| 2384 | struct list_head *resource_list; |
| 2385 | |
| 2386 | if (resource) |
| 2387 | resource_list = &resource->resource_list; |
| 2388 | else |
| 2389 | resource_list = &devlink->resource_list; |
| 2390 | |
| 2391 | list_for_each_entry(resource, resource_list, list) { |
| 2392 | struct devlink_resource *child_resource; |
| 2393 | |
| 2394 | if (resource->id == resource_id) |
| 2395 | return resource; |
| 2396 | |
| 2397 | child_resource = devlink_resource_find(devlink, resource, |
| 2398 | resource_id); |
| 2399 | if (child_resource) |
| 2400 | return child_resource; |
| 2401 | } |
| 2402 | return NULL; |
| 2403 | } |
| 2404 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2405 | static void |
| 2406 | devlink_resource_validate_children(struct devlink_resource *resource) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2407 | { |
| 2408 | struct devlink_resource *child_resource; |
| 2409 | bool size_valid = true; |
| 2410 | u64 parts_size = 0; |
| 2411 | |
| 2412 | if (list_empty(&resource->resource_list)) |
| 2413 | goto out; |
| 2414 | |
| 2415 | list_for_each_entry(child_resource, &resource->resource_list, list) |
| 2416 | parts_size += child_resource->size_new; |
| 2417 | |
Arkadi Sharshevsky | b9d1717 | 2018-02-26 10:59:53 +0100 | [diff] [blame] | 2418 | if (parts_size > resource->size_new) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2419 | size_valid = false; |
| 2420 | out: |
| 2421 | resource->size_valid = size_valid; |
| 2422 | } |
| 2423 | |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2424 | static int |
| 2425 | devlink_resource_validate_size(struct devlink_resource *resource, u64 size, |
| 2426 | struct netlink_ext_ack *extack) |
| 2427 | { |
| 2428 | u64 reminder; |
| 2429 | int err = 0; |
| 2430 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2431 | if (size > resource->size_params.size_max) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2432 | NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum"); |
| 2433 | err = -EINVAL; |
| 2434 | } |
| 2435 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2436 | if (size < resource->size_params.size_min) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2437 | NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum"); |
| 2438 | err = -EINVAL; |
| 2439 | } |
| 2440 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2441 | div64_u64_rem(size, resource->size_params.size_granularity, &reminder); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2442 | if (reminder) { |
| 2443 | NL_SET_ERR_MSG_MOD(extack, "Wrong granularity"); |
| 2444 | err = -EINVAL; |
| 2445 | } |
| 2446 | |
| 2447 | return err; |
| 2448 | } |
| 2449 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2450 | static int devlink_nl_cmd_resource_set(struct sk_buff *skb, |
| 2451 | struct genl_info *info) |
| 2452 | { |
| 2453 | struct devlink *devlink = info->user_ptr[0]; |
| 2454 | struct devlink_resource *resource; |
| 2455 | u64 resource_id; |
| 2456 | u64 size; |
| 2457 | int err; |
| 2458 | |
| 2459 | if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] || |
| 2460 | !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]) |
| 2461 | return -EINVAL; |
| 2462 | resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]); |
| 2463 | |
| 2464 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 2465 | if (!resource) |
| 2466 | return -EINVAL; |
| 2467 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2468 | size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2469 | err = devlink_resource_validate_size(resource, size, info->extack); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2470 | if (err) |
| 2471 | return err; |
| 2472 | |
| 2473 | resource->size_new = size; |
| 2474 | devlink_resource_validate_children(resource); |
| 2475 | if (resource->parent) |
| 2476 | devlink_resource_validate_children(resource->parent); |
| 2477 | return 0; |
| 2478 | } |
| 2479 | |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2480 | static int |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2481 | devlink_resource_size_params_put(struct devlink_resource *resource, |
| 2482 | struct sk_buff *skb) |
| 2483 | { |
| 2484 | struct devlink_resource_size_params *size_params; |
| 2485 | |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 2486 | size_params = &resource->size_params; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2487 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN, |
| 2488 | size_params->size_granularity, DEVLINK_ATTR_PAD) || |
| 2489 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX, |
| 2490 | size_params->size_max, DEVLINK_ATTR_PAD) || |
| 2491 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN, |
| 2492 | size_params->size_min, DEVLINK_ATTR_PAD) || |
| 2493 | nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit)) |
| 2494 | return -EMSGSIZE; |
| 2495 | return 0; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2498 | static int devlink_resource_occ_put(struct devlink_resource *resource, |
| 2499 | struct sk_buff *skb) |
| 2500 | { |
| 2501 | if (!resource->occ_get) |
| 2502 | return 0; |
| 2503 | return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC, |
| 2504 | resource->occ_get(resource->occ_get_priv), |
| 2505 | DEVLINK_ATTR_PAD); |
| 2506 | } |
| 2507 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2508 | static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb, |
| 2509 | struct devlink_resource *resource) |
| 2510 | { |
| 2511 | struct devlink_resource *child_resource; |
| 2512 | struct nlattr *child_resource_attr; |
| 2513 | struct nlattr *resource_attr; |
| 2514 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2515 | resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2516 | if (!resource_attr) |
| 2517 | return -EMSGSIZE; |
| 2518 | |
| 2519 | if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) || |
| 2520 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size, |
| 2521 | DEVLINK_ATTR_PAD) || |
| 2522 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id, |
| 2523 | DEVLINK_ATTR_PAD)) |
| 2524 | goto nla_put_failure; |
| 2525 | if (resource->size != resource->size_new) |
| 2526 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW, |
| 2527 | resource->size_new, DEVLINK_ATTR_PAD); |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2528 | if (devlink_resource_occ_put(resource, skb)) |
| 2529 | goto nla_put_failure; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2530 | if (devlink_resource_size_params_put(resource, skb)) |
| 2531 | goto nla_put_failure; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2532 | if (list_empty(&resource->resource_list)) |
| 2533 | goto out; |
| 2534 | |
| 2535 | if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID, |
| 2536 | resource->size_valid)) |
| 2537 | goto nla_put_failure; |
| 2538 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2539 | child_resource_attr = nla_nest_start_noflag(skb, |
| 2540 | DEVLINK_ATTR_RESOURCE_LIST); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2541 | if (!child_resource_attr) |
| 2542 | goto nla_put_failure; |
| 2543 | |
| 2544 | list_for_each_entry(child_resource, &resource->resource_list, list) { |
| 2545 | if (devlink_resource_put(devlink, skb, child_resource)) |
| 2546 | goto resource_put_failure; |
| 2547 | } |
| 2548 | |
| 2549 | nla_nest_end(skb, child_resource_attr); |
| 2550 | out: |
| 2551 | nla_nest_end(skb, resource_attr); |
| 2552 | return 0; |
| 2553 | |
| 2554 | resource_put_failure: |
| 2555 | nla_nest_cancel(skb, child_resource_attr); |
| 2556 | nla_put_failure: |
| 2557 | nla_nest_cancel(skb, resource_attr); |
| 2558 | return -EMSGSIZE; |
| 2559 | } |
| 2560 | |
| 2561 | static int devlink_resource_fill(struct genl_info *info, |
| 2562 | enum devlink_command cmd, int flags) |
| 2563 | { |
| 2564 | struct devlink *devlink = info->user_ptr[0]; |
| 2565 | struct devlink_resource *resource; |
| 2566 | struct nlattr *resources_attr; |
| 2567 | struct sk_buff *skb = NULL; |
| 2568 | struct nlmsghdr *nlh; |
| 2569 | bool incomplete; |
| 2570 | void *hdr; |
| 2571 | int i; |
| 2572 | int err; |
| 2573 | |
| 2574 | resource = list_first_entry(&devlink->resource_list, |
| 2575 | struct devlink_resource, list); |
| 2576 | start_again: |
| 2577 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2578 | if (err) |
| 2579 | return err; |
| 2580 | |
| 2581 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2582 | &devlink_nl_family, NLM_F_MULTI, cmd); |
| 2583 | if (!hdr) { |
| 2584 | nlmsg_free(skb); |
| 2585 | return -EMSGSIZE; |
| 2586 | } |
| 2587 | |
| 2588 | if (devlink_nl_put_handle(skb, devlink)) |
| 2589 | goto nla_put_failure; |
| 2590 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2591 | resources_attr = nla_nest_start_noflag(skb, |
| 2592 | DEVLINK_ATTR_RESOURCE_LIST); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2593 | if (!resources_attr) |
| 2594 | goto nla_put_failure; |
| 2595 | |
| 2596 | incomplete = false; |
| 2597 | i = 0; |
| 2598 | list_for_each_entry_from(resource, &devlink->resource_list, list) { |
| 2599 | err = devlink_resource_put(devlink, skb, resource); |
| 2600 | if (err) { |
| 2601 | if (!i) |
| 2602 | goto err_resource_put; |
| 2603 | incomplete = true; |
| 2604 | break; |
| 2605 | } |
| 2606 | i++; |
| 2607 | } |
| 2608 | nla_nest_end(skb, resources_attr); |
| 2609 | genlmsg_end(skb, hdr); |
| 2610 | if (incomplete) |
| 2611 | goto start_again; |
| 2612 | send_done: |
| 2613 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2614 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2615 | if (!nlh) { |
| 2616 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2617 | if (err) |
Dan Carpenter | 83fe9a9 | 2018-09-21 11:07:55 +0300 | [diff] [blame] | 2618 | return err; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2619 | goto send_done; |
| 2620 | } |
| 2621 | return genlmsg_reply(skb, info); |
| 2622 | |
| 2623 | nla_put_failure: |
| 2624 | err = -EMSGSIZE; |
| 2625 | err_resource_put: |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2626 | nlmsg_free(skb); |
| 2627 | return err; |
| 2628 | } |
| 2629 | |
| 2630 | static int devlink_nl_cmd_resource_dump(struct sk_buff *skb, |
| 2631 | struct genl_info *info) |
| 2632 | { |
| 2633 | struct devlink *devlink = info->user_ptr[0]; |
| 2634 | |
| 2635 | if (list_empty(&devlink->resource_list)) |
| 2636 | return -EOPNOTSUPP; |
| 2637 | |
| 2638 | return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0); |
| 2639 | } |
| 2640 | |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2641 | static int |
| 2642 | devlink_resources_validate(struct devlink *devlink, |
| 2643 | struct devlink_resource *resource, |
| 2644 | struct genl_info *info) |
| 2645 | { |
| 2646 | struct list_head *resource_list; |
| 2647 | int err = 0; |
| 2648 | |
| 2649 | if (resource) |
| 2650 | resource_list = &resource->resource_list; |
| 2651 | else |
| 2652 | resource_list = &devlink->resource_list; |
| 2653 | |
| 2654 | list_for_each_entry(resource, resource_list, list) { |
| 2655 | if (!resource->size_valid) |
| 2656 | return -EINVAL; |
| 2657 | err = devlink_resources_validate(devlink, resource, info); |
| 2658 | if (err) |
| 2659 | return err; |
| 2660 | } |
| 2661 | return err; |
| 2662 | } |
| 2663 | |
| 2664 | static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info) |
| 2665 | { |
| 2666 | struct devlink *devlink = info->user_ptr[0]; |
| 2667 | int err; |
| 2668 | |
| 2669 | if (!devlink->ops->reload) |
| 2670 | return -EOPNOTSUPP; |
| 2671 | |
| 2672 | err = devlink_resources_validate(devlink, NULL, info); |
| 2673 | if (err) { |
| 2674 | NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed"); |
| 2675 | return err; |
| 2676 | } |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 2677 | return devlink->ops->reload(devlink, info->extack); |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2678 | } |
| 2679 | |
Jiri Pirko | 191ed20 | 2019-06-04 15:40:40 +0200 | [diff] [blame] | 2680 | static int devlink_nl_flash_update_fill(struct sk_buff *msg, |
| 2681 | struct devlink *devlink, |
| 2682 | enum devlink_command cmd, |
| 2683 | const char *status_msg, |
| 2684 | const char *component, |
| 2685 | unsigned long done, unsigned long total) |
| 2686 | { |
| 2687 | void *hdr; |
| 2688 | |
| 2689 | hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd); |
| 2690 | if (!hdr) |
| 2691 | return -EMSGSIZE; |
| 2692 | |
| 2693 | if (devlink_nl_put_handle(msg, devlink)) |
| 2694 | goto nla_put_failure; |
| 2695 | |
| 2696 | if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS) |
| 2697 | goto out; |
| 2698 | |
| 2699 | if (status_msg && |
| 2700 | nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG, |
| 2701 | status_msg)) |
| 2702 | goto nla_put_failure; |
| 2703 | if (component && |
| 2704 | nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT, |
| 2705 | component)) |
| 2706 | goto nla_put_failure; |
| 2707 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE, |
| 2708 | done, DEVLINK_ATTR_PAD)) |
| 2709 | goto nla_put_failure; |
| 2710 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL, |
| 2711 | total, DEVLINK_ATTR_PAD)) |
| 2712 | goto nla_put_failure; |
| 2713 | |
| 2714 | out: |
| 2715 | genlmsg_end(msg, hdr); |
| 2716 | return 0; |
| 2717 | |
| 2718 | nla_put_failure: |
| 2719 | genlmsg_cancel(msg, hdr); |
| 2720 | return -EMSGSIZE; |
| 2721 | } |
| 2722 | |
| 2723 | static void __devlink_flash_update_notify(struct devlink *devlink, |
| 2724 | enum devlink_command cmd, |
| 2725 | const char *status_msg, |
| 2726 | const char *component, |
| 2727 | unsigned long done, |
| 2728 | unsigned long total) |
| 2729 | { |
| 2730 | struct sk_buff *msg; |
| 2731 | int err; |
| 2732 | |
| 2733 | WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE && |
| 2734 | cmd != DEVLINK_CMD_FLASH_UPDATE_END && |
| 2735 | cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); |
| 2736 | |
| 2737 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 2738 | if (!msg) |
| 2739 | return; |
| 2740 | |
| 2741 | err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg, |
| 2742 | component, done, total); |
| 2743 | if (err) |
| 2744 | goto out_free_msg; |
| 2745 | |
| 2746 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 2747 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 2748 | return; |
| 2749 | |
| 2750 | out_free_msg: |
| 2751 | nlmsg_free(msg); |
| 2752 | } |
| 2753 | |
| 2754 | void devlink_flash_update_begin_notify(struct devlink *devlink) |
| 2755 | { |
| 2756 | __devlink_flash_update_notify(devlink, |
| 2757 | DEVLINK_CMD_FLASH_UPDATE, |
| 2758 | NULL, NULL, 0, 0); |
| 2759 | } |
| 2760 | EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify); |
| 2761 | |
| 2762 | void devlink_flash_update_end_notify(struct devlink *devlink) |
| 2763 | { |
| 2764 | __devlink_flash_update_notify(devlink, |
| 2765 | DEVLINK_CMD_FLASH_UPDATE_END, |
| 2766 | NULL, NULL, 0, 0); |
| 2767 | } |
| 2768 | EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify); |
| 2769 | |
| 2770 | void devlink_flash_update_status_notify(struct devlink *devlink, |
| 2771 | const char *status_msg, |
| 2772 | const char *component, |
| 2773 | unsigned long done, |
| 2774 | unsigned long total) |
| 2775 | { |
| 2776 | __devlink_flash_update_notify(devlink, |
| 2777 | DEVLINK_CMD_FLASH_UPDATE_STATUS, |
| 2778 | status_msg, component, done, total); |
| 2779 | } |
| 2780 | EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify); |
| 2781 | |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 2782 | static int devlink_nl_cmd_flash_update(struct sk_buff *skb, |
| 2783 | struct genl_info *info) |
| 2784 | { |
| 2785 | struct devlink *devlink = info->user_ptr[0]; |
| 2786 | const char *file_name, *component; |
| 2787 | struct nlattr *nla_component; |
| 2788 | |
| 2789 | if (!devlink->ops->flash_update) |
| 2790 | return -EOPNOTSUPP; |
| 2791 | |
| 2792 | if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]) |
| 2793 | return -EINVAL; |
| 2794 | file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]); |
| 2795 | |
| 2796 | nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]; |
| 2797 | component = nla_component ? nla_data(nla_component) : NULL; |
| 2798 | |
| 2799 | return devlink->ops->flash_update(devlink, file_name, component, |
| 2800 | info->extack); |
| 2801 | } |
| 2802 | |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame] | 2803 | static const struct devlink_param devlink_param_generic[] = { |
| 2804 | { |
| 2805 | .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET, |
| 2806 | .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME, |
| 2807 | .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE, |
| 2808 | }, |
| 2809 | { |
| 2810 | .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS, |
| 2811 | .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME, |
| 2812 | .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE, |
| 2813 | }, |
Vasundhara Volam | f567bcd | 2018-07-04 14:30:36 +0300 | [diff] [blame] | 2814 | { |
| 2815 | .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, |
| 2816 | .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME, |
| 2817 | .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE, |
| 2818 | }, |
Alex Vesker | f6a69885 | 2018-07-12 15:13:17 +0300 | [diff] [blame] | 2819 | { |
| 2820 | .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT, |
| 2821 | .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME, |
| 2822 | .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE, |
| 2823 | }, |
Vasundhara Volam | e3b5106 | 2018-10-04 11:13:44 +0530 | [diff] [blame] | 2824 | { |
| 2825 | .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI, |
| 2826 | .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME, |
| 2827 | .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE, |
| 2828 | }, |
Vasundhara Volam | f61cba4 | 2018-10-04 11:13:45 +0530 | [diff] [blame] | 2829 | { |
| 2830 | .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX, |
| 2831 | .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME, |
| 2832 | .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE, |
| 2833 | }, |
Vasundhara Volam | 1651178 | 2018-10-04 11:13:46 +0530 | [diff] [blame] | 2834 | { |
| 2835 | .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN, |
| 2836 | .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME, |
| 2837 | .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE, |
| 2838 | }, |
Shalom Toledo | 846e980 | 2018-12-03 07:58:59 +0000 | [diff] [blame] | 2839 | { |
| 2840 | .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY, |
| 2841 | .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME, |
| 2842 | .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE, |
| 2843 | }, |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame] | 2844 | }; |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 2845 | |
| 2846 | static int devlink_param_generic_verify(const struct devlink_param *param) |
| 2847 | { |
| 2848 | /* verify it match generic parameter by id and name */ |
| 2849 | if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX) |
| 2850 | return -EINVAL; |
| 2851 | if (strcmp(param->name, devlink_param_generic[param->id].name)) |
| 2852 | return -ENOENT; |
| 2853 | |
| 2854 | WARN_ON(param->type != devlink_param_generic[param->id].type); |
| 2855 | |
| 2856 | return 0; |
| 2857 | } |
| 2858 | |
| 2859 | static int devlink_param_driver_verify(const struct devlink_param *param) |
| 2860 | { |
| 2861 | int i; |
| 2862 | |
| 2863 | if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX) |
| 2864 | return -EINVAL; |
| 2865 | /* verify no such name in generic params */ |
| 2866 | for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++) |
| 2867 | if (!strcmp(param->name, devlink_param_generic[i].name)) |
| 2868 | return -EEXIST; |
| 2869 | |
| 2870 | return 0; |
| 2871 | } |
| 2872 | |
| 2873 | static struct devlink_param_item * |
| 2874 | devlink_param_find_by_name(struct list_head *param_list, |
| 2875 | const char *param_name) |
| 2876 | { |
| 2877 | struct devlink_param_item *param_item; |
| 2878 | |
| 2879 | list_for_each_entry(param_item, param_list, list) |
| 2880 | if (!strcmp(param_item->param->name, param_name)) |
| 2881 | return param_item; |
| 2882 | return NULL; |
| 2883 | } |
| 2884 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 2885 | static struct devlink_param_item * |
| 2886 | devlink_param_find_by_id(struct list_head *param_list, u32 param_id) |
| 2887 | { |
| 2888 | struct devlink_param_item *param_item; |
| 2889 | |
| 2890 | list_for_each_entry(param_item, param_list, list) |
| 2891 | if (param_item->param->id == param_id) |
| 2892 | return param_item; |
| 2893 | return NULL; |
| 2894 | } |
| 2895 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2896 | static bool |
| 2897 | devlink_param_cmode_is_supported(const struct devlink_param *param, |
| 2898 | enum devlink_param_cmode cmode) |
| 2899 | { |
| 2900 | return test_bit(cmode, ¶m->supported_cmodes); |
| 2901 | } |
| 2902 | |
| 2903 | static int devlink_param_get(struct devlink *devlink, |
| 2904 | const struct devlink_param *param, |
| 2905 | struct devlink_param_gset_ctx *ctx) |
| 2906 | { |
| 2907 | if (!param->get) |
| 2908 | return -EOPNOTSUPP; |
| 2909 | return param->get(devlink, param->id, ctx); |
| 2910 | } |
| 2911 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 2912 | static int devlink_param_set(struct devlink *devlink, |
| 2913 | const struct devlink_param *param, |
| 2914 | struct devlink_param_gset_ctx *ctx) |
| 2915 | { |
| 2916 | if (!param->set) |
| 2917 | return -EOPNOTSUPP; |
| 2918 | return param->set(devlink, param->id, ctx); |
| 2919 | } |
| 2920 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2921 | static int |
| 2922 | devlink_param_type_to_nla_type(enum devlink_param_type param_type) |
| 2923 | { |
| 2924 | switch (param_type) { |
| 2925 | case DEVLINK_PARAM_TYPE_U8: |
| 2926 | return NLA_U8; |
| 2927 | case DEVLINK_PARAM_TYPE_U16: |
| 2928 | return NLA_U16; |
| 2929 | case DEVLINK_PARAM_TYPE_U32: |
| 2930 | return NLA_U32; |
| 2931 | case DEVLINK_PARAM_TYPE_STRING: |
| 2932 | return NLA_STRING; |
| 2933 | case DEVLINK_PARAM_TYPE_BOOL: |
| 2934 | return NLA_FLAG; |
| 2935 | default: |
| 2936 | return -EINVAL; |
| 2937 | } |
| 2938 | } |
| 2939 | |
| 2940 | static int |
| 2941 | devlink_nl_param_value_fill_one(struct sk_buff *msg, |
| 2942 | enum devlink_param_type type, |
| 2943 | enum devlink_param_cmode cmode, |
| 2944 | union devlink_param_value val) |
| 2945 | { |
| 2946 | struct nlattr *param_value_attr; |
| 2947 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2948 | param_value_attr = nla_nest_start_noflag(msg, |
| 2949 | DEVLINK_ATTR_PARAM_VALUE); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2950 | if (!param_value_attr) |
| 2951 | goto nla_put_failure; |
| 2952 | |
| 2953 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode)) |
| 2954 | goto value_nest_cancel; |
| 2955 | |
| 2956 | switch (type) { |
| 2957 | case DEVLINK_PARAM_TYPE_U8: |
| 2958 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8)) |
| 2959 | goto value_nest_cancel; |
| 2960 | break; |
| 2961 | case DEVLINK_PARAM_TYPE_U16: |
| 2962 | if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16)) |
| 2963 | goto value_nest_cancel; |
| 2964 | break; |
| 2965 | case DEVLINK_PARAM_TYPE_U32: |
| 2966 | if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32)) |
| 2967 | goto value_nest_cancel; |
| 2968 | break; |
| 2969 | case DEVLINK_PARAM_TYPE_STRING: |
| 2970 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, |
| 2971 | val.vstr)) |
| 2972 | goto value_nest_cancel; |
| 2973 | break; |
| 2974 | case DEVLINK_PARAM_TYPE_BOOL: |
| 2975 | if (val.vbool && |
| 2976 | nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA)) |
| 2977 | goto value_nest_cancel; |
| 2978 | break; |
| 2979 | } |
| 2980 | |
| 2981 | nla_nest_end(msg, param_value_attr); |
| 2982 | return 0; |
| 2983 | |
| 2984 | value_nest_cancel: |
| 2985 | nla_nest_cancel(msg, param_value_attr); |
| 2986 | nla_put_failure: |
| 2987 | return -EMSGSIZE; |
| 2988 | } |
| 2989 | |
| 2990 | 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] | 2991 | unsigned int port_index, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2992 | struct devlink_param_item *param_item, |
| 2993 | enum devlink_command cmd, |
| 2994 | u32 portid, u32 seq, int flags) |
| 2995 | { |
| 2996 | union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1]; |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 2997 | bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {}; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2998 | const struct devlink_param *param = param_item->param; |
| 2999 | struct devlink_param_gset_ctx ctx; |
| 3000 | struct nlattr *param_values_list; |
| 3001 | struct nlattr *param_attr; |
| 3002 | int nla_type; |
| 3003 | void *hdr; |
| 3004 | int err; |
| 3005 | int i; |
| 3006 | |
| 3007 | /* Get value from driver part to driverinit configuration mode */ |
| 3008 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
| 3009 | if (!devlink_param_cmode_is_supported(param, i)) |
| 3010 | continue; |
| 3011 | if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
| 3012 | if (!param_item->driverinit_value_valid) |
| 3013 | return -EOPNOTSUPP; |
| 3014 | param_value[i] = param_item->driverinit_value; |
| 3015 | } else { |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3016 | if (!param_item->published) |
| 3017 | continue; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3018 | ctx.cmode = i; |
| 3019 | err = devlink_param_get(devlink, param, &ctx); |
| 3020 | if (err) |
| 3021 | return err; |
| 3022 | param_value[i] = ctx.val; |
| 3023 | } |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3024 | param_value_set[i] = true; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3028 | if (!hdr) |
| 3029 | return -EMSGSIZE; |
| 3030 | |
| 3031 | if (devlink_nl_put_handle(msg, devlink)) |
| 3032 | goto genlmsg_cancel; |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3033 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3034 | if (cmd == DEVLINK_CMD_PORT_PARAM_GET || |
| 3035 | cmd == DEVLINK_CMD_PORT_PARAM_NEW || |
| 3036 | cmd == DEVLINK_CMD_PORT_PARAM_DEL) |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3037 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index)) |
| 3038 | goto genlmsg_cancel; |
| 3039 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3040 | param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3041 | if (!param_attr) |
| 3042 | goto genlmsg_cancel; |
| 3043 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name)) |
| 3044 | goto param_nest_cancel; |
| 3045 | if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC)) |
| 3046 | goto param_nest_cancel; |
| 3047 | |
| 3048 | nla_type = devlink_param_type_to_nla_type(param->type); |
| 3049 | if (nla_type < 0) |
| 3050 | goto param_nest_cancel; |
| 3051 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type)) |
| 3052 | goto param_nest_cancel; |
| 3053 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3054 | param_values_list = nla_nest_start_noflag(msg, |
| 3055 | DEVLINK_ATTR_PARAM_VALUES_LIST); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3056 | if (!param_values_list) |
| 3057 | goto param_nest_cancel; |
| 3058 | |
| 3059 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3060 | if (!param_value_set[i]) |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3061 | continue; |
| 3062 | err = devlink_nl_param_value_fill_one(msg, param->type, |
| 3063 | i, param_value[i]); |
| 3064 | if (err) |
| 3065 | goto values_list_nest_cancel; |
| 3066 | } |
| 3067 | |
| 3068 | nla_nest_end(msg, param_values_list); |
| 3069 | nla_nest_end(msg, param_attr); |
| 3070 | genlmsg_end(msg, hdr); |
| 3071 | return 0; |
| 3072 | |
| 3073 | values_list_nest_cancel: |
| 3074 | nla_nest_end(msg, param_values_list); |
| 3075 | param_nest_cancel: |
| 3076 | nla_nest_cancel(msg, param_attr); |
| 3077 | genlmsg_cancel: |
| 3078 | genlmsg_cancel(msg, hdr); |
| 3079 | return -EMSGSIZE; |
| 3080 | } |
| 3081 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3082 | static void devlink_param_notify(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3083 | unsigned int port_index, |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3084 | struct devlink_param_item *param_item, |
| 3085 | enum devlink_command cmd) |
| 3086 | { |
| 3087 | struct sk_buff *msg; |
| 3088 | int err; |
| 3089 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3090 | WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL && |
| 3091 | cmd != DEVLINK_CMD_PORT_PARAM_NEW && |
| 3092 | cmd != DEVLINK_CMD_PORT_PARAM_DEL); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3093 | |
| 3094 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3095 | if (!msg) |
| 3096 | return; |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3097 | err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd, |
| 3098 | 0, 0, 0); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3099 | if (err) { |
| 3100 | nlmsg_free(msg); |
| 3101 | return; |
| 3102 | } |
| 3103 | |
| 3104 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3105 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 3106 | } |
| 3107 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3108 | static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg, |
| 3109 | struct netlink_callback *cb) |
| 3110 | { |
| 3111 | struct devlink_param_item *param_item; |
| 3112 | struct devlink *devlink; |
| 3113 | int start = cb->args[0]; |
| 3114 | int idx = 0; |
| 3115 | int err; |
| 3116 | |
| 3117 | mutex_lock(&devlink_mutex); |
| 3118 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3119 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3120 | continue; |
| 3121 | mutex_lock(&devlink->lock); |
| 3122 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 3123 | if (idx < start) { |
| 3124 | idx++; |
| 3125 | continue; |
| 3126 | } |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3127 | err = devlink_nl_param_fill(msg, devlink, 0, param_item, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3128 | DEVLINK_CMD_PARAM_GET, |
| 3129 | NETLINK_CB(cb->skb).portid, |
| 3130 | cb->nlh->nlmsg_seq, |
| 3131 | NLM_F_MULTI); |
| 3132 | if (err) { |
| 3133 | mutex_unlock(&devlink->lock); |
| 3134 | goto out; |
| 3135 | } |
| 3136 | idx++; |
| 3137 | } |
| 3138 | mutex_unlock(&devlink->lock); |
| 3139 | } |
| 3140 | out: |
| 3141 | mutex_unlock(&devlink_mutex); |
| 3142 | |
| 3143 | cb->args[0] = idx; |
| 3144 | return msg->len; |
| 3145 | } |
| 3146 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3147 | static int |
| 3148 | devlink_param_type_get_from_info(struct genl_info *info, |
| 3149 | enum devlink_param_type *param_type) |
| 3150 | { |
| 3151 | if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE]) |
| 3152 | return -EINVAL; |
| 3153 | |
| 3154 | switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) { |
| 3155 | case NLA_U8: |
| 3156 | *param_type = DEVLINK_PARAM_TYPE_U8; |
| 3157 | break; |
| 3158 | case NLA_U16: |
| 3159 | *param_type = DEVLINK_PARAM_TYPE_U16; |
| 3160 | break; |
| 3161 | case NLA_U32: |
| 3162 | *param_type = DEVLINK_PARAM_TYPE_U32; |
| 3163 | break; |
| 3164 | case NLA_STRING: |
| 3165 | *param_type = DEVLINK_PARAM_TYPE_STRING; |
| 3166 | break; |
| 3167 | case NLA_FLAG: |
| 3168 | *param_type = DEVLINK_PARAM_TYPE_BOOL; |
| 3169 | break; |
| 3170 | default: |
| 3171 | return -EINVAL; |
| 3172 | } |
| 3173 | |
| 3174 | return 0; |
| 3175 | } |
| 3176 | |
| 3177 | static int |
| 3178 | devlink_param_value_get_from_info(const struct devlink_param *param, |
| 3179 | struct genl_info *info, |
| 3180 | union devlink_param_value *value) |
| 3181 | { |
Moshe Shemesh | f355cfc | 2018-10-10 16:09:25 +0300 | [diff] [blame] | 3182 | int len; |
| 3183 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3184 | if (param->type != DEVLINK_PARAM_TYPE_BOOL && |
| 3185 | !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) |
| 3186 | return -EINVAL; |
| 3187 | |
| 3188 | switch (param->type) { |
| 3189 | case DEVLINK_PARAM_TYPE_U8: |
| 3190 | value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 3191 | break; |
| 3192 | case DEVLINK_PARAM_TYPE_U16: |
| 3193 | value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 3194 | break; |
| 3195 | case DEVLINK_PARAM_TYPE_U32: |
| 3196 | value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 3197 | break; |
| 3198 | case DEVLINK_PARAM_TYPE_STRING: |
Moshe Shemesh | f355cfc | 2018-10-10 16:09:25 +0300 | [diff] [blame] | 3199 | len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]), |
| 3200 | nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])); |
| 3201 | if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) || |
Moshe Shemesh | bde74ad1 | 2018-10-10 16:09:27 +0300 | [diff] [blame] | 3202 | len >= __DEVLINK_PARAM_MAX_STRING_VALUE) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3203 | return -EINVAL; |
Moshe Shemesh | f355cfc | 2018-10-10 16:09:25 +0300 | [diff] [blame] | 3204 | strcpy(value->vstr, |
| 3205 | nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3206 | break; |
| 3207 | case DEVLINK_PARAM_TYPE_BOOL: |
| 3208 | value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ? |
| 3209 | true : false; |
| 3210 | break; |
| 3211 | } |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3215 | static struct devlink_param_item * |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3216 | devlink_param_get_from_info(struct list_head *param_list, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3217 | struct genl_info *info) |
| 3218 | { |
| 3219 | char *param_name; |
| 3220 | |
| 3221 | if (!info->attrs[DEVLINK_ATTR_PARAM_NAME]) |
| 3222 | return NULL; |
| 3223 | |
| 3224 | param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]); |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3225 | return devlink_param_find_by_name(param_list, param_name); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb, |
| 3229 | struct genl_info *info) |
| 3230 | { |
| 3231 | struct devlink *devlink = info->user_ptr[0]; |
| 3232 | struct devlink_param_item *param_item; |
| 3233 | struct sk_buff *msg; |
| 3234 | int err; |
| 3235 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3236 | param_item = devlink_param_get_from_info(&devlink->param_list, info); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3237 | if (!param_item) |
| 3238 | return -EINVAL; |
| 3239 | |
| 3240 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3241 | if (!msg) |
| 3242 | return -ENOMEM; |
| 3243 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3244 | err = devlink_nl_param_fill(msg, devlink, 0, param_item, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3245 | DEVLINK_CMD_PARAM_GET, |
| 3246 | info->snd_portid, info->snd_seq, 0); |
| 3247 | if (err) { |
| 3248 | nlmsg_free(msg); |
| 3249 | return err; |
| 3250 | } |
| 3251 | |
| 3252 | return genlmsg_reply(msg, info); |
| 3253 | } |
| 3254 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3255 | static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3256 | unsigned int port_index, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3257 | struct list_head *param_list, |
| 3258 | struct genl_info *info, |
| 3259 | enum devlink_command cmd) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3260 | { |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3261 | enum devlink_param_type param_type; |
| 3262 | struct devlink_param_gset_ctx ctx; |
| 3263 | enum devlink_param_cmode cmode; |
| 3264 | struct devlink_param_item *param_item; |
| 3265 | const struct devlink_param *param; |
| 3266 | union devlink_param_value value; |
| 3267 | int err = 0; |
| 3268 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3269 | param_item = devlink_param_get_from_info(param_list, info); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3270 | if (!param_item) |
| 3271 | return -EINVAL; |
| 3272 | param = param_item->param; |
| 3273 | err = devlink_param_type_get_from_info(info, ¶m_type); |
| 3274 | if (err) |
| 3275 | return err; |
| 3276 | if (param_type != param->type) |
| 3277 | return -EINVAL; |
| 3278 | err = devlink_param_value_get_from_info(param, info, &value); |
| 3279 | if (err) |
| 3280 | return err; |
| 3281 | if (param->validate) { |
| 3282 | err = param->validate(devlink, param->id, value, info->extack); |
| 3283 | if (err) |
| 3284 | return err; |
| 3285 | } |
| 3286 | |
| 3287 | if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]) |
| 3288 | return -EINVAL; |
| 3289 | cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]); |
| 3290 | if (!devlink_param_cmode_is_supported(param, cmode)) |
| 3291 | return -EOPNOTSUPP; |
| 3292 | |
| 3293 | if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
Moshe Shemesh | 1276534 | 2018-10-10 16:09:26 +0300 | [diff] [blame] | 3294 | if (param->type == DEVLINK_PARAM_TYPE_STRING) |
| 3295 | strcpy(param_item->driverinit_value.vstr, value.vstr); |
| 3296 | else |
| 3297 | param_item->driverinit_value = value; |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3298 | param_item->driverinit_value_valid = true; |
| 3299 | } else { |
| 3300 | if (!param->set) |
| 3301 | return -EOPNOTSUPP; |
| 3302 | ctx.val = value; |
| 3303 | ctx.cmode = cmode; |
| 3304 | err = devlink_param_set(devlink, param, &ctx); |
| 3305 | if (err) |
| 3306 | return err; |
| 3307 | } |
| 3308 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3309 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3310 | return 0; |
| 3311 | } |
| 3312 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3313 | static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb, |
| 3314 | struct genl_info *info) |
| 3315 | { |
| 3316 | struct devlink *devlink = info->user_ptr[0]; |
| 3317 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3318 | return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3319 | info, DEVLINK_CMD_PARAM_NEW); |
| 3320 | } |
| 3321 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3322 | static int devlink_param_register_one(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3323 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3324 | struct list_head *param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3325 | const struct devlink_param *param, |
| 3326 | enum devlink_command cmd) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3327 | { |
| 3328 | struct devlink_param_item *param_item; |
| 3329 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3330 | if (devlink_param_find_by_name(param_list, param->name)) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3331 | return -EEXIST; |
| 3332 | |
| 3333 | if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 3334 | WARN_ON(param->get || param->set); |
| 3335 | else |
| 3336 | WARN_ON(!param->get || !param->set); |
| 3337 | |
| 3338 | param_item = kzalloc(sizeof(*param_item), GFP_KERNEL); |
| 3339 | if (!param_item) |
| 3340 | return -ENOMEM; |
| 3341 | param_item->param = param; |
| 3342 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3343 | list_add_tail(¶m_item->list, param_list); |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3344 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3345 | return 0; |
| 3346 | } |
| 3347 | |
| 3348 | static void devlink_param_unregister_one(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3349 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3350 | struct list_head *param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3351 | const struct devlink_param *param, |
| 3352 | enum devlink_command cmd) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3353 | { |
| 3354 | struct devlink_param_item *param_item; |
| 3355 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3356 | param_item = devlink_param_find_by_name(param_list, param->name); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3357 | WARN_ON(!param_item); |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3358 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3359 | list_del(¶m_item->list); |
| 3360 | kfree(param_item); |
| 3361 | } |
| 3362 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3363 | static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg, |
| 3364 | struct netlink_callback *cb) |
| 3365 | { |
| 3366 | struct devlink_param_item *param_item; |
| 3367 | struct devlink_port *devlink_port; |
| 3368 | struct devlink *devlink; |
| 3369 | int start = cb->args[0]; |
| 3370 | int idx = 0; |
| 3371 | int err; |
| 3372 | |
| 3373 | mutex_lock(&devlink_mutex); |
| 3374 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3375 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3376 | continue; |
| 3377 | mutex_lock(&devlink->lock); |
| 3378 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 3379 | list_for_each_entry(param_item, |
| 3380 | &devlink_port->param_list, list) { |
| 3381 | if (idx < start) { |
| 3382 | idx++; |
| 3383 | continue; |
| 3384 | } |
| 3385 | err = devlink_nl_param_fill(msg, |
| 3386 | devlink_port->devlink, |
| 3387 | devlink_port->index, param_item, |
| 3388 | DEVLINK_CMD_PORT_PARAM_GET, |
| 3389 | NETLINK_CB(cb->skb).portid, |
| 3390 | cb->nlh->nlmsg_seq, |
| 3391 | NLM_F_MULTI); |
| 3392 | if (err) { |
| 3393 | mutex_unlock(&devlink->lock); |
| 3394 | goto out; |
| 3395 | } |
| 3396 | idx++; |
| 3397 | } |
| 3398 | } |
| 3399 | mutex_unlock(&devlink->lock); |
| 3400 | } |
| 3401 | out: |
| 3402 | mutex_unlock(&devlink_mutex); |
| 3403 | |
| 3404 | cb->args[0] = idx; |
| 3405 | return msg->len; |
| 3406 | } |
| 3407 | |
| 3408 | static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb, |
| 3409 | struct genl_info *info) |
| 3410 | { |
| 3411 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 3412 | struct devlink_param_item *param_item; |
| 3413 | struct sk_buff *msg; |
| 3414 | int err; |
| 3415 | |
| 3416 | param_item = devlink_param_get_from_info(&devlink_port->param_list, |
| 3417 | info); |
| 3418 | if (!param_item) |
| 3419 | return -EINVAL; |
| 3420 | |
| 3421 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3422 | if (!msg) |
| 3423 | return -ENOMEM; |
| 3424 | |
| 3425 | err = devlink_nl_param_fill(msg, devlink_port->devlink, |
| 3426 | devlink_port->index, param_item, |
| 3427 | DEVLINK_CMD_PORT_PARAM_GET, |
| 3428 | info->snd_portid, info->snd_seq, 0); |
| 3429 | if (err) { |
| 3430 | nlmsg_free(msg); |
| 3431 | return err; |
| 3432 | } |
| 3433 | |
| 3434 | return genlmsg_reply(msg, info); |
| 3435 | } |
| 3436 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3437 | static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb, |
| 3438 | struct genl_info *info) |
| 3439 | { |
| 3440 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 3441 | |
| 3442 | return __devlink_nl_cmd_param_set_doit(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3443 | devlink_port->index, |
| 3444 | &devlink_port->param_list, info, |
| 3445 | DEVLINK_CMD_PORT_PARAM_NEW); |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3446 | } |
| 3447 | |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3448 | static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg, |
| 3449 | struct devlink *devlink, |
| 3450 | struct devlink_snapshot *snapshot) |
| 3451 | { |
| 3452 | struct nlattr *snap_attr; |
| 3453 | int err; |
| 3454 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3455 | snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT); |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3456 | if (!snap_attr) |
| 3457 | return -EINVAL; |
| 3458 | |
| 3459 | err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id); |
| 3460 | if (err) |
| 3461 | goto nla_put_failure; |
| 3462 | |
| 3463 | nla_nest_end(msg, snap_attr); |
| 3464 | return 0; |
| 3465 | |
| 3466 | nla_put_failure: |
| 3467 | nla_nest_cancel(msg, snap_attr); |
| 3468 | return err; |
| 3469 | } |
| 3470 | |
| 3471 | static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg, |
| 3472 | struct devlink *devlink, |
| 3473 | struct devlink_region *region) |
| 3474 | { |
| 3475 | struct devlink_snapshot *snapshot; |
| 3476 | struct nlattr *snapshots_attr; |
| 3477 | int err; |
| 3478 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3479 | snapshots_attr = nla_nest_start_noflag(msg, |
| 3480 | DEVLINK_ATTR_REGION_SNAPSHOTS); |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3481 | if (!snapshots_attr) |
| 3482 | return -EINVAL; |
| 3483 | |
| 3484 | list_for_each_entry(snapshot, ®ion->snapshot_list, list) { |
| 3485 | err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot); |
| 3486 | if (err) |
| 3487 | goto nla_put_failure; |
| 3488 | } |
| 3489 | |
| 3490 | nla_nest_end(msg, snapshots_attr); |
| 3491 | return 0; |
| 3492 | |
| 3493 | nla_put_failure: |
| 3494 | nla_nest_cancel(msg, snapshots_attr); |
| 3495 | return err; |
| 3496 | } |
| 3497 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3498 | static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink, |
| 3499 | enum devlink_command cmd, u32 portid, |
| 3500 | u32 seq, int flags, |
| 3501 | struct devlink_region *region) |
| 3502 | { |
| 3503 | void *hdr; |
| 3504 | int err; |
| 3505 | |
| 3506 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3507 | if (!hdr) |
| 3508 | return -EMSGSIZE; |
| 3509 | |
| 3510 | err = devlink_nl_put_handle(msg, devlink); |
| 3511 | if (err) |
| 3512 | goto nla_put_failure; |
| 3513 | |
| 3514 | err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name); |
| 3515 | if (err) |
| 3516 | goto nla_put_failure; |
| 3517 | |
| 3518 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, |
| 3519 | region->size, |
| 3520 | DEVLINK_ATTR_PAD); |
| 3521 | if (err) |
| 3522 | goto nla_put_failure; |
| 3523 | |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3524 | err = devlink_nl_region_snapshots_id_put(msg, devlink, region); |
| 3525 | if (err) |
| 3526 | goto nla_put_failure; |
| 3527 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3528 | genlmsg_end(msg, hdr); |
| 3529 | return 0; |
| 3530 | |
| 3531 | nla_put_failure: |
| 3532 | genlmsg_cancel(msg, hdr); |
| 3533 | return err; |
| 3534 | } |
| 3535 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3536 | static void devlink_nl_region_notify(struct devlink_region *region, |
| 3537 | struct devlink_snapshot *snapshot, |
| 3538 | enum devlink_command cmd) |
| 3539 | { |
| 3540 | struct devlink *devlink = region->devlink; |
| 3541 | struct sk_buff *msg; |
| 3542 | void *hdr; |
| 3543 | int err; |
| 3544 | |
| 3545 | WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); |
| 3546 | |
| 3547 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3548 | if (!msg) |
| 3549 | return; |
| 3550 | |
| 3551 | hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd); |
| 3552 | if (!hdr) |
| 3553 | goto out_free_msg; |
| 3554 | |
| 3555 | err = devlink_nl_put_handle(msg, devlink); |
| 3556 | if (err) |
| 3557 | goto out_cancel_msg; |
| 3558 | |
| 3559 | err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, |
| 3560 | region->name); |
| 3561 | if (err) |
| 3562 | goto out_cancel_msg; |
| 3563 | |
| 3564 | if (snapshot) { |
| 3565 | err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, |
| 3566 | snapshot->id); |
| 3567 | if (err) |
| 3568 | goto out_cancel_msg; |
| 3569 | } else { |
| 3570 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, |
| 3571 | region->size, DEVLINK_ATTR_PAD); |
| 3572 | if (err) |
| 3573 | goto out_cancel_msg; |
| 3574 | } |
| 3575 | genlmsg_end(msg, hdr); |
| 3576 | |
| 3577 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3578 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 3579 | |
| 3580 | return; |
| 3581 | |
| 3582 | out_cancel_msg: |
| 3583 | genlmsg_cancel(msg, hdr); |
| 3584 | out_free_msg: |
| 3585 | nlmsg_free(msg); |
| 3586 | } |
| 3587 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3588 | static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb, |
| 3589 | struct genl_info *info) |
| 3590 | { |
| 3591 | struct devlink *devlink = info->user_ptr[0]; |
| 3592 | struct devlink_region *region; |
| 3593 | const char *region_name; |
| 3594 | struct sk_buff *msg; |
| 3595 | int err; |
| 3596 | |
| 3597 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) |
| 3598 | return -EINVAL; |
| 3599 | |
| 3600 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 3601 | region = devlink_region_get_by_name(devlink, region_name); |
| 3602 | if (!region) |
| 3603 | return -EINVAL; |
| 3604 | |
| 3605 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3606 | if (!msg) |
| 3607 | return -ENOMEM; |
| 3608 | |
| 3609 | err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET, |
| 3610 | info->snd_portid, info->snd_seq, 0, |
| 3611 | region); |
| 3612 | if (err) { |
| 3613 | nlmsg_free(msg); |
| 3614 | return err; |
| 3615 | } |
| 3616 | |
| 3617 | return genlmsg_reply(msg, info); |
| 3618 | } |
| 3619 | |
| 3620 | static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg, |
| 3621 | struct netlink_callback *cb) |
| 3622 | { |
| 3623 | struct devlink_region *region; |
| 3624 | struct devlink *devlink; |
| 3625 | int start = cb->args[0]; |
| 3626 | int idx = 0; |
| 3627 | int err; |
| 3628 | |
| 3629 | mutex_lock(&devlink_mutex); |
| 3630 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3631 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3632 | continue; |
| 3633 | |
| 3634 | mutex_lock(&devlink->lock); |
| 3635 | list_for_each_entry(region, &devlink->region_list, list) { |
| 3636 | if (idx < start) { |
| 3637 | idx++; |
| 3638 | continue; |
| 3639 | } |
| 3640 | err = devlink_nl_region_fill(msg, devlink, |
| 3641 | DEVLINK_CMD_REGION_GET, |
| 3642 | NETLINK_CB(cb->skb).portid, |
| 3643 | cb->nlh->nlmsg_seq, |
| 3644 | NLM_F_MULTI, region); |
| 3645 | if (err) { |
| 3646 | mutex_unlock(&devlink->lock); |
| 3647 | goto out; |
| 3648 | } |
| 3649 | idx++; |
| 3650 | } |
| 3651 | mutex_unlock(&devlink->lock); |
| 3652 | } |
| 3653 | out: |
| 3654 | mutex_unlock(&devlink_mutex); |
| 3655 | cb->args[0] = idx; |
| 3656 | return msg->len; |
| 3657 | } |
| 3658 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3659 | static int devlink_nl_cmd_region_del(struct sk_buff *skb, |
| 3660 | struct genl_info *info) |
| 3661 | { |
| 3662 | struct devlink *devlink = info->user_ptr[0]; |
| 3663 | struct devlink_snapshot *snapshot; |
| 3664 | struct devlink_region *region; |
| 3665 | const char *region_name; |
| 3666 | u32 snapshot_id; |
| 3667 | |
| 3668 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME] || |
| 3669 | !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) |
| 3670 | return -EINVAL; |
| 3671 | |
| 3672 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 3673 | snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); |
| 3674 | |
| 3675 | region = devlink_region_get_by_name(devlink, region_name); |
| 3676 | if (!region) |
| 3677 | return -EINVAL; |
| 3678 | |
| 3679 | snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); |
| 3680 | if (!snapshot) |
| 3681 | return -EINVAL; |
| 3682 | |
| 3683 | devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL); |
| 3684 | devlink_region_snapshot_del(snapshot); |
| 3685 | return 0; |
| 3686 | } |
| 3687 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3688 | static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg, |
| 3689 | struct devlink *devlink, |
| 3690 | u8 *chunk, u32 chunk_size, |
| 3691 | u64 addr) |
| 3692 | { |
| 3693 | struct nlattr *chunk_attr; |
| 3694 | int err; |
| 3695 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3696 | chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3697 | if (!chunk_attr) |
| 3698 | return -EINVAL; |
| 3699 | |
| 3700 | err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk); |
| 3701 | if (err) |
| 3702 | goto nla_put_failure; |
| 3703 | |
| 3704 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr, |
| 3705 | DEVLINK_ATTR_PAD); |
| 3706 | if (err) |
| 3707 | goto nla_put_failure; |
| 3708 | |
| 3709 | nla_nest_end(msg, chunk_attr); |
| 3710 | return 0; |
| 3711 | |
| 3712 | nla_put_failure: |
| 3713 | nla_nest_cancel(msg, chunk_attr); |
| 3714 | return err; |
| 3715 | } |
| 3716 | |
| 3717 | #define DEVLINK_REGION_READ_CHUNK_SIZE 256 |
| 3718 | |
| 3719 | static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb, |
| 3720 | struct devlink *devlink, |
| 3721 | struct devlink_region *region, |
| 3722 | struct nlattr **attrs, |
| 3723 | u64 start_offset, |
| 3724 | u64 end_offset, |
| 3725 | bool dump, |
| 3726 | u64 *new_offset) |
| 3727 | { |
| 3728 | struct devlink_snapshot *snapshot; |
| 3729 | u64 curr_offset = start_offset; |
| 3730 | u32 snapshot_id; |
| 3731 | int err = 0; |
| 3732 | |
| 3733 | *new_offset = start_offset; |
| 3734 | |
| 3735 | snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); |
| 3736 | snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); |
| 3737 | if (!snapshot) |
| 3738 | return -EINVAL; |
| 3739 | |
| 3740 | if (end_offset > snapshot->data_len || dump) |
| 3741 | end_offset = snapshot->data_len; |
| 3742 | |
| 3743 | while (curr_offset < end_offset) { |
| 3744 | u32 data_size; |
| 3745 | u8 *data; |
| 3746 | |
| 3747 | if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE) |
| 3748 | data_size = end_offset - curr_offset; |
| 3749 | else |
| 3750 | data_size = DEVLINK_REGION_READ_CHUNK_SIZE; |
| 3751 | |
| 3752 | data = &snapshot->data[curr_offset]; |
| 3753 | err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink, |
| 3754 | data, data_size, |
| 3755 | curr_offset); |
| 3756 | if (err) |
| 3757 | break; |
| 3758 | |
| 3759 | curr_offset += data_size; |
| 3760 | } |
| 3761 | *new_offset = curr_offset; |
| 3762 | |
| 3763 | return err; |
| 3764 | } |
| 3765 | |
| 3766 | static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb, |
| 3767 | struct netlink_callback *cb) |
| 3768 | { |
| 3769 | u64 ret_offset, start_offset, end_offset = 0; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3770 | struct devlink_region *region; |
| 3771 | struct nlattr *chunks_attr; |
| 3772 | const char *region_name; |
| 3773 | struct devlink *devlink; |
Jakub Kicinski | 6875056 | 2019-02-10 19:35:28 -0800 | [diff] [blame] | 3774 | struct nlattr **attrs; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3775 | bool dump = true; |
| 3776 | void *hdr; |
| 3777 | int err; |
| 3778 | |
| 3779 | start_offset = *((u64 *)&cb->args[0]); |
| 3780 | |
Jakub Kicinski | 6875056 | 2019-02-10 19:35:28 -0800 | [diff] [blame] | 3781 | attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL); |
| 3782 | if (!attrs) |
| 3783 | return -ENOMEM; |
| 3784 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3785 | err = nlmsg_parse_deprecated(cb->nlh, |
| 3786 | GENL_HDRLEN + devlink_nl_family.hdrsize, |
| 3787 | attrs, DEVLINK_ATTR_MAX, |
| 3788 | devlink_nl_family.policy, cb->extack); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3789 | if (err) |
Jakub Kicinski | 6875056 | 2019-02-10 19:35:28 -0800 | [diff] [blame] | 3790 | goto out_free; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3791 | |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 3792 | mutex_lock(&devlink_mutex); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3793 | devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3794 | if (IS_ERR(devlink)) { |
| 3795 | err = PTR_ERR(devlink); |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 3796 | goto out_dev; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3797 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3798 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3799 | mutex_lock(&devlink->lock); |
| 3800 | |
| 3801 | if (!attrs[DEVLINK_ATTR_REGION_NAME] || |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3802 | !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) { |
| 3803 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3804 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3805 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3806 | |
| 3807 | region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]); |
| 3808 | region = devlink_region_get_by_name(devlink, region_name); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3809 | if (!region) { |
| 3810 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3811 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3812 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3813 | |
| 3814 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 3815 | &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, |
| 3816 | DEVLINK_CMD_REGION_READ); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3817 | if (!hdr) { |
| 3818 | err = -EMSGSIZE; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3819 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3820 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3821 | |
| 3822 | err = devlink_nl_put_handle(skb, devlink); |
| 3823 | if (err) |
| 3824 | goto nla_put_failure; |
| 3825 | |
| 3826 | err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name); |
| 3827 | if (err) |
| 3828 | goto nla_put_failure; |
| 3829 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3830 | chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3831 | if (!chunks_attr) { |
| 3832 | err = -EMSGSIZE; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3833 | goto nla_put_failure; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3834 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3835 | |
| 3836 | if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] && |
| 3837 | attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) { |
| 3838 | if (!start_offset) |
| 3839 | start_offset = |
| 3840 | nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); |
| 3841 | |
| 3842 | end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); |
| 3843 | end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]); |
| 3844 | dump = false; |
| 3845 | } |
| 3846 | |
| 3847 | err = devlink_nl_region_read_snapshot_fill(skb, devlink, |
| 3848 | region, attrs, |
| 3849 | start_offset, |
| 3850 | end_offset, dump, |
| 3851 | &ret_offset); |
| 3852 | |
| 3853 | if (err && err != -EMSGSIZE) |
| 3854 | goto nla_put_failure; |
| 3855 | |
| 3856 | /* Check if there was any progress done to prevent infinite loop */ |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3857 | if (ret_offset == start_offset) { |
| 3858 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3859 | goto nla_put_failure; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3860 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3861 | |
| 3862 | *((u64 *)&cb->args[0]) = ret_offset; |
| 3863 | |
| 3864 | nla_nest_end(skb, chunks_attr); |
| 3865 | genlmsg_end(skb, hdr); |
| 3866 | mutex_unlock(&devlink->lock); |
| 3867 | mutex_unlock(&devlink_mutex); |
Jakub Kicinski | 6875056 | 2019-02-10 19:35:28 -0800 | [diff] [blame] | 3868 | kfree(attrs); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3869 | |
| 3870 | return skb->len; |
| 3871 | |
| 3872 | nla_put_failure: |
| 3873 | genlmsg_cancel(skb, hdr); |
| 3874 | out_unlock: |
| 3875 | mutex_unlock(&devlink->lock); |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 3876 | out_dev: |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3877 | mutex_unlock(&devlink_mutex); |
Jakub Kicinski | 6875056 | 2019-02-10 19:35:28 -0800 | [diff] [blame] | 3878 | out_free: |
| 3879 | kfree(attrs); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3880 | return err; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3881 | } |
| 3882 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 3883 | struct devlink_info_req { |
| 3884 | struct sk_buff *msg; |
| 3885 | }; |
| 3886 | |
| 3887 | int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name) |
| 3888 | { |
| 3889 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name); |
| 3890 | } |
| 3891 | EXPORT_SYMBOL_GPL(devlink_info_driver_name_put); |
| 3892 | |
| 3893 | int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn) |
| 3894 | { |
| 3895 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn); |
| 3896 | } |
| 3897 | EXPORT_SYMBOL_GPL(devlink_info_serial_number_put); |
| 3898 | |
Jakub Kicinski | fc6fae7 | 2019-01-31 10:50:41 -0800 | [diff] [blame] | 3899 | static int devlink_info_version_put(struct devlink_info_req *req, int attr, |
| 3900 | const char *version_name, |
| 3901 | const char *version_value) |
| 3902 | { |
| 3903 | struct nlattr *nest; |
| 3904 | int err; |
| 3905 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3906 | nest = nla_nest_start_noflag(req->msg, attr); |
Jakub Kicinski | fc6fae7 | 2019-01-31 10:50:41 -0800 | [diff] [blame] | 3907 | if (!nest) |
| 3908 | return -EMSGSIZE; |
| 3909 | |
| 3910 | err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME, |
| 3911 | version_name); |
| 3912 | if (err) |
| 3913 | goto nla_put_failure; |
| 3914 | |
| 3915 | err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE, |
| 3916 | version_value); |
| 3917 | if (err) |
| 3918 | goto nla_put_failure; |
| 3919 | |
| 3920 | nla_nest_end(req->msg, nest); |
| 3921 | |
| 3922 | return 0; |
| 3923 | |
| 3924 | nla_put_failure: |
| 3925 | nla_nest_cancel(req->msg, nest); |
| 3926 | return err; |
| 3927 | } |
| 3928 | |
| 3929 | int devlink_info_version_fixed_put(struct devlink_info_req *req, |
| 3930 | const char *version_name, |
| 3931 | const char *version_value) |
| 3932 | { |
| 3933 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED, |
| 3934 | version_name, version_value); |
| 3935 | } |
| 3936 | EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put); |
| 3937 | |
| 3938 | int devlink_info_version_stored_put(struct devlink_info_req *req, |
| 3939 | const char *version_name, |
| 3940 | const char *version_value) |
| 3941 | { |
| 3942 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED, |
| 3943 | version_name, version_value); |
| 3944 | } |
| 3945 | EXPORT_SYMBOL_GPL(devlink_info_version_stored_put); |
| 3946 | |
| 3947 | int devlink_info_version_running_put(struct devlink_info_req *req, |
| 3948 | const char *version_name, |
| 3949 | const char *version_value) |
| 3950 | { |
| 3951 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING, |
| 3952 | version_name, version_value); |
| 3953 | } |
| 3954 | EXPORT_SYMBOL_GPL(devlink_info_version_running_put); |
| 3955 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 3956 | static int |
| 3957 | devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink, |
| 3958 | enum devlink_command cmd, u32 portid, |
| 3959 | u32 seq, int flags, struct netlink_ext_ack *extack) |
| 3960 | { |
| 3961 | struct devlink_info_req req; |
| 3962 | void *hdr; |
| 3963 | int err; |
| 3964 | |
| 3965 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3966 | if (!hdr) |
| 3967 | return -EMSGSIZE; |
| 3968 | |
| 3969 | err = -EMSGSIZE; |
| 3970 | if (devlink_nl_put_handle(msg, devlink)) |
| 3971 | goto err_cancel_msg; |
| 3972 | |
| 3973 | req.msg = msg; |
| 3974 | err = devlink->ops->info_get(devlink, &req, extack); |
| 3975 | if (err) |
| 3976 | goto err_cancel_msg; |
| 3977 | |
| 3978 | genlmsg_end(msg, hdr); |
| 3979 | return 0; |
| 3980 | |
| 3981 | err_cancel_msg: |
| 3982 | genlmsg_cancel(msg, hdr); |
| 3983 | return err; |
| 3984 | } |
| 3985 | |
| 3986 | static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb, |
| 3987 | struct genl_info *info) |
| 3988 | { |
| 3989 | struct devlink *devlink = info->user_ptr[0]; |
| 3990 | struct sk_buff *msg; |
| 3991 | int err; |
| 3992 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 3993 | if (!devlink->ops->info_get) |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 3994 | return -EOPNOTSUPP; |
| 3995 | |
| 3996 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3997 | if (!msg) |
| 3998 | return -ENOMEM; |
| 3999 | |
| 4000 | err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET, |
| 4001 | info->snd_portid, info->snd_seq, 0, |
| 4002 | info->extack); |
| 4003 | if (err) { |
| 4004 | nlmsg_free(msg); |
| 4005 | return err; |
| 4006 | } |
| 4007 | |
| 4008 | return genlmsg_reply(msg, info); |
| 4009 | } |
| 4010 | |
| 4011 | static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg, |
| 4012 | struct netlink_callback *cb) |
| 4013 | { |
| 4014 | struct devlink *devlink; |
| 4015 | int start = cb->args[0]; |
| 4016 | int idx = 0; |
| 4017 | int err; |
| 4018 | |
| 4019 | mutex_lock(&devlink_mutex); |
| 4020 | list_for_each_entry(devlink, &devlink_list, list) { |
| 4021 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 4022 | continue; |
| 4023 | if (idx < start) { |
| 4024 | idx++; |
| 4025 | continue; |
| 4026 | } |
| 4027 | |
Jiri Pirko | c493b09b | 2019-03-24 00:21:03 +0100 | [diff] [blame] | 4028 | if (!devlink->ops->info_get) { |
| 4029 | idx++; |
| 4030 | continue; |
| 4031 | } |
| 4032 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4033 | mutex_lock(&devlink->lock); |
| 4034 | err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET, |
| 4035 | NETLINK_CB(cb->skb).portid, |
| 4036 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 4037 | cb->extack); |
| 4038 | mutex_unlock(&devlink->lock); |
| 4039 | if (err) |
| 4040 | break; |
| 4041 | idx++; |
| 4042 | } |
| 4043 | mutex_unlock(&devlink_mutex); |
| 4044 | |
| 4045 | cb->args[0] = idx; |
| 4046 | return msg->len; |
| 4047 | } |
| 4048 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4049 | struct devlink_fmsg_item { |
| 4050 | struct list_head list; |
| 4051 | int attrtype; |
| 4052 | u8 nla_type; |
| 4053 | u16 len; |
| 4054 | int value[0]; |
| 4055 | }; |
| 4056 | |
| 4057 | struct devlink_fmsg { |
| 4058 | struct list_head item_list; |
| 4059 | }; |
| 4060 | |
| 4061 | static struct devlink_fmsg *devlink_fmsg_alloc(void) |
| 4062 | { |
| 4063 | struct devlink_fmsg *fmsg; |
| 4064 | |
| 4065 | fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL); |
| 4066 | if (!fmsg) |
| 4067 | return NULL; |
| 4068 | |
| 4069 | INIT_LIST_HEAD(&fmsg->item_list); |
| 4070 | |
| 4071 | return fmsg; |
| 4072 | } |
| 4073 | |
| 4074 | static void devlink_fmsg_free(struct devlink_fmsg *fmsg) |
| 4075 | { |
| 4076 | struct devlink_fmsg_item *item, *tmp; |
| 4077 | |
| 4078 | list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) { |
| 4079 | list_del(&item->list); |
| 4080 | kfree(item); |
| 4081 | } |
| 4082 | kfree(fmsg); |
| 4083 | } |
| 4084 | |
| 4085 | static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg, |
| 4086 | int attrtype) |
| 4087 | { |
| 4088 | struct devlink_fmsg_item *item; |
| 4089 | |
| 4090 | item = kzalloc(sizeof(*item), GFP_KERNEL); |
| 4091 | if (!item) |
| 4092 | return -ENOMEM; |
| 4093 | |
| 4094 | item->attrtype = attrtype; |
| 4095 | list_add_tail(&item->list, &fmsg->item_list); |
| 4096 | |
| 4097 | return 0; |
| 4098 | } |
| 4099 | |
| 4100 | int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg) |
| 4101 | { |
| 4102 | return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START); |
| 4103 | } |
| 4104 | EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start); |
| 4105 | |
| 4106 | static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg) |
| 4107 | { |
| 4108 | return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END); |
| 4109 | } |
| 4110 | |
| 4111 | int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg) |
| 4112 | { |
| 4113 | return devlink_fmsg_nest_end(fmsg); |
| 4114 | } |
| 4115 | EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end); |
| 4116 | |
| 4117 | #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN) |
| 4118 | |
| 4119 | static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name) |
| 4120 | { |
| 4121 | struct devlink_fmsg_item *item; |
| 4122 | |
| 4123 | if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE) |
| 4124 | return -EMSGSIZE; |
| 4125 | |
| 4126 | item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL); |
| 4127 | if (!item) |
| 4128 | return -ENOMEM; |
| 4129 | |
| 4130 | item->nla_type = NLA_NUL_STRING; |
| 4131 | item->len = strlen(name) + 1; |
| 4132 | item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME; |
| 4133 | memcpy(&item->value, name, item->len); |
| 4134 | list_add_tail(&item->list, &fmsg->item_list); |
| 4135 | |
| 4136 | return 0; |
| 4137 | } |
| 4138 | |
| 4139 | int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name) |
| 4140 | { |
| 4141 | int err; |
| 4142 | |
| 4143 | err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START); |
| 4144 | if (err) |
| 4145 | return err; |
| 4146 | |
| 4147 | err = devlink_fmsg_put_name(fmsg, name); |
| 4148 | if (err) |
| 4149 | return err; |
| 4150 | |
| 4151 | return 0; |
| 4152 | } |
| 4153 | EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start); |
| 4154 | |
| 4155 | int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4156 | { |
| 4157 | return devlink_fmsg_nest_end(fmsg); |
| 4158 | } |
| 4159 | EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end); |
| 4160 | |
| 4161 | int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg, |
| 4162 | const char *name) |
| 4163 | { |
| 4164 | int err; |
| 4165 | |
| 4166 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4167 | if (err) |
| 4168 | return err; |
| 4169 | |
| 4170 | err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START); |
| 4171 | if (err) |
| 4172 | return err; |
| 4173 | |
| 4174 | return 0; |
| 4175 | } |
| 4176 | EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start); |
| 4177 | |
| 4178 | int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4179 | { |
| 4180 | int err; |
| 4181 | |
| 4182 | err = devlink_fmsg_nest_end(fmsg); |
| 4183 | if (err) |
| 4184 | return err; |
| 4185 | |
| 4186 | err = devlink_fmsg_nest_end(fmsg); |
| 4187 | if (err) |
| 4188 | return err; |
| 4189 | |
| 4190 | return 0; |
| 4191 | } |
| 4192 | EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end); |
| 4193 | |
| 4194 | static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg, |
| 4195 | const void *value, u16 value_len, |
| 4196 | u8 value_nla_type) |
| 4197 | { |
| 4198 | struct devlink_fmsg_item *item; |
| 4199 | |
| 4200 | if (value_len > DEVLINK_FMSG_MAX_SIZE) |
| 4201 | return -EMSGSIZE; |
| 4202 | |
| 4203 | item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL); |
| 4204 | if (!item) |
| 4205 | return -ENOMEM; |
| 4206 | |
| 4207 | item->nla_type = value_nla_type; |
| 4208 | item->len = value_len; |
| 4209 | item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; |
| 4210 | memcpy(&item->value, value, item->len); |
| 4211 | list_add_tail(&item->list, &fmsg->item_list); |
| 4212 | |
| 4213 | return 0; |
| 4214 | } |
| 4215 | |
| 4216 | int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value) |
| 4217 | { |
| 4218 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG); |
| 4219 | } |
| 4220 | EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put); |
| 4221 | |
| 4222 | int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value) |
| 4223 | { |
| 4224 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8); |
| 4225 | } |
| 4226 | EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put); |
| 4227 | |
| 4228 | int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value) |
| 4229 | { |
| 4230 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32); |
| 4231 | } |
| 4232 | EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put); |
| 4233 | |
| 4234 | int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value) |
| 4235 | { |
| 4236 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64); |
| 4237 | } |
| 4238 | EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put); |
| 4239 | |
| 4240 | int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value) |
| 4241 | { |
| 4242 | return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, |
| 4243 | NLA_NUL_STRING); |
| 4244 | } |
| 4245 | EXPORT_SYMBOL_GPL(devlink_fmsg_string_put); |
| 4246 | |
| 4247 | int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value, |
| 4248 | u16 value_len) |
| 4249 | { |
| 4250 | return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY); |
| 4251 | } |
| 4252 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put); |
| 4253 | |
| 4254 | int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4255 | bool value) |
| 4256 | { |
| 4257 | int err; |
| 4258 | |
| 4259 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4260 | if (err) |
| 4261 | return err; |
| 4262 | |
| 4263 | err = devlink_fmsg_bool_put(fmsg, value); |
| 4264 | if (err) |
| 4265 | return err; |
| 4266 | |
| 4267 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4268 | if (err) |
| 4269 | return err; |
| 4270 | |
| 4271 | return 0; |
| 4272 | } |
| 4273 | EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put); |
| 4274 | |
| 4275 | int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4276 | u8 value) |
| 4277 | { |
| 4278 | int err; |
| 4279 | |
| 4280 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4281 | if (err) |
| 4282 | return err; |
| 4283 | |
| 4284 | err = devlink_fmsg_u8_put(fmsg, value); |
| 4285 | if (err) |
| 4286 | return err; |
| 4287 | |
| 4288 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4289 | if (err) |
| 4290 | return err; |
| 4291 | |
| 4292 | return 0; |
| 4293 | } |
| 4294 | EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put); |
| 4295 | |
| 4296 | int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4297 | u32 value) |
| 4298 | { |
| 4299 | int err; |
| 4300 | |
| 4301 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4302 | if (err) |
| 4303 | return err; |
| 4304 | |
| 4305 | err = devlink_fmsg_u32_put(fmsg, value); |
| 4306 | if (err) |
| 4307 | return err; |
| 4308 | |
| 4309 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4310 | if (err) |
| 4311 | return err; |
| 4312 | |
| 4313 | return 0; |
| 4314 | } |
| 4315 | EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put); |
| 4316 | |
| 4317 | int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4318 | u64 value) |
| 4319 | { |
| 4320 | int err; |
| 4321 | |
| 4322 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4323 | if (err) |
| 4324 | return err; |
| 4325 | |
| 4326 | err = devlink_fmsg_u64_put(fmsg, value); |
| 4327 | if (err) |
| 4328 | return err; |
| 4329 | |
| 4330 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4331 | if (err) |
| 4332 | return err; |
| 4333 | |
| 4334 | return 0; |
| 4335 | } |
| 4336 | EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put); |
| 4337 | |
| 4338 | int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4339 | const char *value) |
| 4340 | { |
| 4341 | int err; |
| 4342 | |
| 4343 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4344 | if (err) |
| 4345 | return err; |
| 4346 | |
| 4347 | err = devlink_fmsg_string_put(fmsg, value); |
| 4348 | if (err) |
| 4349 | return err; |
| 4350 | |
| 4351 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4352 | if (err) |
| 4353 | return err; |
| 4354 | |
| 4355 | return 0; |
| 4356 | } |
| 4357 | EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put); |
| 4358 | |
| 4359 | int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4360 | const void *value, u16 value_len) |
| 4361 | { |
| 4362 | int err; |
| 4363 | |
| 4364 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4365 | if (err) |
| 4366 | return err; |
| 4367 | |
| 4368 | err = devlink_fmsg_binary_put(fmsg, value, value_len); |
| 4369 | if (err) |
| 4370 | return err; |
| 4371 | |
| 4372 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4373 | if (err) |
| 4374 | return err; |
| 4375 | |
| 4376 | return 0; |
| 4377 | } |
| 4378 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put); |
| 4379 | |
| 4380 | static int |
| 4381 | devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb) |
| 4382 | { |
| 4383 | switch (msg->nla_type) { |
| 4384 | case NLA_FLAG: |
| 4385 | case NLA_U8: |
| 4386 | case NLA_U32: |
| 4387 | case NLA_U64: |
| 4388 | case NLA_NUL_STRING: |
| 4389 | case NLA_BINARY: |
| 4390 | return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE, |
| 4391 | msg->nla_type); |
| 4392 | default: |
| 4393 | return -EINVAL; |
| 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | static int |
| 4398 | devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb) |
| 4399 | { |
| 4400 | int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; |
| 4401 | u8 tmp; |
| 4402 | |
| 4403 | switch (msg->nla_type) { |
| 4404 | case NLA_FLAG: |
| 4405 | /* Always provide flag data, regardless of its value */ |
| 4406 | tmp = *(bool *) msg->value; |
| 4407 | |
| 4408 | return nla_put_u8(skb, attrtype, tmp); |
| 4409 | case NLA_U8: |
| 4410 | return nla_put_u8(skb, attrtype, *(u8 *) msg->value); |
| 4411 | case NLA_U32: |
| 4412 | return nla_put_u32(skb, attrtype, *(u32 *) msg->value); |
| 4413 | case NLA_U64: |
| 4414 | return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value, |
| 4415 | DEVLINK_ATTR_PAD); |
| 4416 | case NLA_NUL_STRING: |
| 4417 | return nla_put_string(skb, attrtype, (char *) &msg->value); |
| 4418 | case NLA_BINARY: |
| 4419 | return nla_put(skb, attrtype, msg->len, (void *) &msg->value); |
| 4420 | default: |
| 4421 | return -EINVAL; |
| 4422 | } |
| 4423 | } |
| 4424 | |
| 4425 | static int |
| 4426 | devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb, |
| 4427 | int *start) |
| 4428 | { |
| 4429 | struct devlink_fmsg_item *item; |
| 4430 | struct nlattr *fmsg_nlattr; |
| 4431 | int i = 0; |
| 4432 | int err; |
| 4433 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4434 | fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4435 | if (!fmsg_nlattr) |
| 4436 | return -EMSGSIZE; |
| 4437 | |
| 4438 | list_for_each_entry(item, &fmsg->item_list, list) { |
| 4439 | if (i < *start) { |
| 4440 | i++; |
| 4441 | continue; |
| 4442 | } |
| 4443 | |
| 4444 | switch (item->attrtype) { |
| 4445 | case DEVLINK_ATTR_FMSG_OBJ_NEST_START: |
| 4446 | case DEVLINK_ATTR_FMSG_PAIR_NEST_START: |
| 4447 | case DEVLINK_ATTR_FMSG_ARR_NEST_START: |
| 4448 | case DEVLINK_ATTR_FMSG_NEST_END: |
| 4449 | err = nla_put_flag(skb, item->attrtype); |
| 4450 | break; |
| 4451 | case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA: |
| 4452 | err = devlink_fmsg_item_fill_type(item, skb); |
| 4453 | if (err) |
| 4454 | break; |
| 4455 | err = devlink_fmsg_item_fill_data(item, skb); |
| 4456 | break; |
| 4457 | case DEVLINK_ATTR_FMSG_OBJ_NAME: |
| 4458 | err = nla_put_string(skb, item->attrtype, |
| 4459 | (char *) &item->value); |
| 4460 | break; |
| 4461 | default: |
| 4462 | err = -EINVAL; |
| 4463 | break; |
| 4464 | } |
| 4465 | if (!err) |
| 4466 | *start = ++i; |
| 4467 | else |
| 4468 | break; |
| 4469 | } |
| 4470 | |
| 4471 | nla_nest_end(skb, fmsg_nlattr); |
| 4472 | return err; |
| 4473 | } |
| 4474 | |
| 4475 | static int devlink_fmsg_snd(struct devlink_fmsg *fmsg, |
| 4476 | struct genl_info *info, |
| 4477 | enum devlink_command cmd, int flags) |
| 4478 | { |
| 4479 | struct nlmsghdr *nlh; |
| 4480 | struct sk_buff *skb; |
| 4481 | bool last = false; |
| 4482 | int index = 0; |
| 4483 | void *hdr; |
| 4484 | int err; |
| 4485 | |
| 4486 | while (!last) { |
| 4487 | int tmp_index = index; |
| 4488 | |
| 4489 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4490 | if (!skb) |
| 4491 | return -ENOMEM; |
| 4492 | |
| 4493 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 4494 | &devlink_nl_family, flags | NLM_F_MULTI, cmd); |
| 4495 | if (!hdr) { |
| 4496 | err = -EMSGSIZE; |
| 4497 | goto nla_put_failure; |
| 4498 | } |
| 4499 | |
| 4500 | err = devlink_fmsg_prepare_skb(fmsg, skb, &index); |
| 4501 | if (!err) |
| 4502 | last = true; |
| 4503 | else if (err != -EMSGSIZE || tmp_index == index) |
| 4504 | goto nla_put_failure; |
| 4505 | |
| 4506 | genlmsg_end(skb, hdr); |
| 4507 | err = genlmsg_reply(skb, info); |
| 4508 | if (err) |
| 4509 | return err; |
| 4510 | } |
| 4511 | |
| 4512 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4513 | if (!skb) |
| 4514 | return -ENOMEM; |
| 4515 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 4516 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 4517 | if (!nlh) { |
| 4518 | err = -EMSGSIZE; |
| 4519 | goto nla_put_failure; |
| 4520 | } |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4521 | |
Li RongQing | fde55ea | 2019-02-11 19:09:07 +0800 | [diff] [blame] | 4522 | return genlmsg_reply(skb, info); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4523 | |
| 4524 | nla_put_failure: |
| 4525 | nlmsg_free(skb); |
| 4526 | return err; |
| 4527 | } |
| 4528 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4529 | static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb, |
| 4530 | struct netlink_callback *cb, |
| 4531 | enum devlink_command cmd) |
| 4532 | { |
| 4533 | int index = cb->args[0]; |
| 4534 | int tmp_index = index; |
| 4535 | void *hdr; |
| 4536 | int err; |
| 4537 | |
| 4538 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 4539 | &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd); |
| 4540 | if (!hdr) { |
| 4541 | err = -EMSGSIZE; |
| 4542 | goto nla_put_failure; |
| 4543 | } |
| 4544 | |
| 4545 | err = devlink_fmsg_prepare_skb(fmsg, skb, &index); |
| 4546 | if ((err && err != -EMSGSIZE) || tmp_index == index) |
| 4547 | goto nla_put_failure; |
| 4548 | |
| 4549 | cb->args[0] = index; |
| 4550 | genlmsg_end(skb, hdr); |
| 4551 | return skb->len; |
| 4552 | |
| 4553 | nla_put_failure: |
| 4554 | genlmsg_cancel(skb, hdr); |
| 4555 | return err; |
| 4556 | } |
| 4557 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4558 | struct devlink_health_reporter { |
| 4559 | struct list_head list; |
| 4560 | void *priv; |
| 4561 | const struct devlink_health_reporter_ops *ops; |
| 4562 | struct devlink *devlink; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4563 | struct devlink_fmsg *dump_fmsg; |
| 4564 | struct mutex dump_lock; /* lock parallel read/write from dump buffers */ |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4565 | u64 graceful_period; |
| 4566 | bool auto_recover; |
| 4567 | u8 health_state; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4568 | u64 dump_ts; |
| 4569 | u64 error_count; |
| 4570 | u64 recovery_count; |
| 4571 | u64 last_recovery_ts; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4572 | refcount_t refcount; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4573 | }; |
| 4574 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4575 | void * |
| 4576 | devlink_health_reporter_priv(struct devlink_health_reporter *reporter) |
| 4577 | { |
| 4578 | return reporter->priv; |
| 4579 | } |
| 4580 | EXPORT_SYMBOL_GPL(devlink_health_reporter_priv); |
| 4581 | |
| 4582 | static struct devlink_health_reporter * |
| 4583 | devlink_health_reporter_find_by_name(struct devlink *devlink, |
| 4584 | const char *reporter_name) |
| 4585 | { |
| 4586 | struct devlink_health_reporter *reporter; |
| 4587 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4588 | lockdep_assert_held(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4589 | list_for_each_entry(reporter, &devlink->reporter_list, list) |
| 4590 | if (!strcmp(reporter->ops->name, reporter_name)) |
| 4591 | return reporter; |
| 4592 | return NULL; |
| 4593 | } |
| 4594 | |
| 4595 | /** |
| 4596 | * devlink_health_reporter_create - create devlink health reporter |
| 4597 | * |
| 4598 | * @devlink: devlink |
| 4599 | * @ops: ops |
| 4600 | * @graceful_period: to avoid recovery loops, in msecs |
| 4601 | * @auto_recover: auto recover when error occurs |
| 4602 | * @priv: priv |
| 4603 | */ |
| 4604 | struct devlink_health_reporter * |
| 4605 | devlink_health_reporter_create(struct devlink *devlink, |
| 4606 | const struct devlink_health_reporter_ops *ops, |
| 4607 | u64 graceful_period, bool auto_recover, |
| 4608 | void *priv) |
| 4609 | { |
| 4610 | struct devlink_health_reporter *reporter; |
| 4611 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4612 | mutex_lock(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4613 | if (devlink_health_reporter_find_by_name(devlink, ops->name)) { |
| 4614 | reporter = ERR_PTR(-EEXIST); |
| 4615 | goto unlock; |
| 4616 | } |
| 4617 | |
| 4618 | if (WARN_ON(auto_recover && !ops->recover) || |
| 4619 | WARN_ON(graceful_period && !ops->recover)) { |
| 4620 | reporter = ERR_PTR(-EINVAL); |
| 4621 | goto unlock; |
| 4622 | } |
| 4623 | |
| 4624 | reporter = kzalloc(sizeof(*reporter), GFP_KERNEL); |
| 4625 | if (!reporter) { |
| 4626 | reporter = ERR_PTR(-ENOMEM); |
| 4627 | goto unlock; |
| 4628 | } |
| 4629 | |
| 4630 | reporter->priv = priv; |
| 4631 | reporter->ops = ops; |
| 4632 | reporter->devlink = devlink; |
| 4633 | reporter->graceful_period = graceful_period; |
| 4634 | reporter->auto_recover = auto_recover; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4635 | mutex_init(&reporter->dump_lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4636 | refcount_set(&reporter->refcount, 1); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4637 | list_add_tail(&reporter->list, &devlink->reporter_list); |
| 4638 | unlock: |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4639 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4640 | return reporter; |
| 4641 | } |
| 4642 | EXPORT_SYMBOL_GPL(devlink_health_reporter_create); |
| 4643 | |
| 4644 | /** |
| 4645 | * devlink_health_reporter_destroy - destroy devlink health reporter |
| 4646 | * |
| 4647 | * @reporter: devlink health reporter to destroy |
| 4648 | */ |
| 4649 | void |
| 4650 | devlink_health_reporter_destroy(struct devlink_health_reporter *reporter) |
| 4651 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4652 | mutex_lock(&reporter->devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4653 | list_del(&reporter->list); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4654 | mutex_unlock(&reporter->devlink->reporters_lock); |
| 4655 | while (refcount_read(&reporter->refcount) > 1) |
| 4656 | msleep(100); |
Jiri Pirko | 375cf8c | 2019-03-24 11:14:24 +0100 | [diff] [blame] | 4657 | mutex_destroy(&reporter->dump_lock); |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4658 | if (reporter->dump_fmsg) |
| 4659 | devlink_fmsg_free(reporter->dump_fmsg); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4660 | kfree(reporter); |
| 4661 | } |
| 4662 | EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy); |
| 4663 | |
Eran Ben Elisha | 3167b27 | 2019-03-03 10:57:30 +0200 | [diff] [blame] | 4664 | void |
| 4665 | devlink_health_reporter_state_update(struct devlink_health_reporter *reporter, |
| 4666 | enum devlink_health_reporter_state state) |
| 4667 | { |
| 4668 | if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY && |
| 4669 | state != DEVLINK_HEALTH_REPORTER_STATE_ERROR)) |
| 4670 | return; |
| 4671 | |
| 4672 | if (reporter->health_state == state) |
| 4673 | return; |
| 4674 | |
| 4675 | reporter->health_state = state; |
| 4676 | trace_devlink_health_reporter_state_update(reporter->devlink, |
| 4677 | reporter->ops->name, state); |
| 4678 | } |
| 4679 | EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update); |
| 4680 | |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4681 | static int |
| 4682 | devlink_health_reporter_recover(struct devlink_health_reporter *reporter, |
| 4683 | void *priv_ctx) |
| 4684 | { |
| 4685 | int err; |
| 4686 | |
| 4687 | if (!reporter->ops->recover) |
| 4688 | return -EOPNOTSUPP; |
| 4689 | |
| 4690 | err = reporter->ops->recover(reporter, priv_ctx); |
| 4691 | if (err) |
| 4692 | return err; |
| 4693 | |
| 4694 | reporter->recovery_count++; |
| 4695 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY; |
| 4696 | reporter->last_recovery_ts = jiffies; |
| 4697 | |
| 4698 | return 0; |
| 4699 | } |
| 4700 | |
| 4701 | static void |
| 4702 | devlink_health_dump_clear(struct devlink_health_reporter *reporter) |
| 4703 | { |
| 4704 | if (!reporter->dump_fmsg) |
| 4705 | return; |
| 4706 | devlink_fmsg_free(reporter->dump_fmsg); |
| 4707 | reporter->dump_fmsg = NULL; |
| 4708 | } |
| 4709 | |
| 4710 | static int devlink_health_do_dump(struct devlink_health_reporter *reporter, |
| 4711 | void *priv_ctx) |
| 4712 | { |
| 4713 | int err; |
| 4714 | |
| 4715 | if (!reporter->ops->dump) |
| 4716 | return 0; |
| 4717 | |
| 4718 | if (reporter->dump_fmsg) |
| 4719 | return 0; |
| 4720 | |
| 4721 | reporter->dump_fmsg = devlink_fmsg_alloc(); |
| 4722 | if (!reporter->dump_fmsg) { |
| 4723 | err = -ENOMEM; |
| 4724 | return err; |
| 4725 | } |
| 4726 | |
| 4727 | err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg); |
| 4728 | if (err) |
| 4729 | goto dump_err; |
| 4730 | |
| 4731 | err = reporter->ops->dump(reporter, reporter->dump_fmsg, |
| 4732 | priv_ctx); |
| 4733 | if (err) |
| 4734 | goto dump_err; |
| 4735 | |
| 4736 | err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg); |
| 4737 | if (err) |
| 4738 | goto dump_err; |
| 4739 | |
| 4740 | reporter->dump_ts = jiffies; |
| 4741 | |
| 4742 | return 0; |
| 4743 | |
| 4744 | dump_err: |
| 4745 | devlink_health_dump_clear(reporter); |
| 4746 | return err; |
| 4747 | } |
| 4748 | |
| 4749 | int devlink_health_report(struct devlink_health_reporter *reporter, |
| 4750 | const char *msg, void *priv_ctx) |
| 4751 | { |
Eran Ben Elisha | a0a21ad | 2019-03-03 10:57:29 +0200 | [diff] [blame] | 4752 | enum devlink_health_reporter_state prev_health_state; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4753 | struct devlink *devlink = reporter->devlink; |
| 4754 | |
| 4755 | /* write a log message of the current error */ |
| 4756 | WARN_ON(!msg); |
| 4757 | trace_devlink_health_report(devlink, reporter->ops->name, msg); |
| 4758 | reporter->error_count++; |
Eran Ben Elisha | a0a21ad | 2019-03-03 10:57:29 +0200 | [diff] [blame] | 4759 | prev_health_state = reporter->health_state; |
| 4760 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4761 | |
| 4762 | /* abort if the previous error wasn't recovered */ |
| 4763 | if (reporter->auto_recover && |
Eran Ben Elisha | a0a21ad | 2019-03-03 10:57:29 +0200 | [diff] [blame] | 4764 | (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY || |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4765 | jiffies - reporter->last_recovery_ts < |
| 4766 | msecs_to_jiffies(reporter->graceful_period))) { |
| 4767 | trace_devlink_health_recover_aborted(devlink, |
| 4768 | reporter->ops->name, |
| 4769 | reporter->health_state, |
| 4770 | jiffies - |
| 4771 | reporter->last_recovery_ts); |
| 4772 | return -ECANCELED; |
| 4773 | } |
| 4774 | |
| 4775 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; |
| 4776 | |
| 4777 | mutex_lock(&reporter->dump_lock); |
| 4778 | /* store current dump of current error, for later analysis */ |
| 4779 | devlink_health_do_dump(reporter, priv_ctx); |
| 4780 | mutex_unlock(&reporter->dump_lock); |
| 4781 | |
| 4782 | if (reporter->auto_recover) |
| 4783 | return devlink_health_reporter_recover(reporter, priv_ctx); |
| 4784 | |
| 4785 | return 0; |
| 4786 | } |
| 4787 | EXPORT_SYMBOL_GPL(devlink_health_report); |
| 4788 | |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4789 | static struct devlink_health_reporter * |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4790 | devlink_health_reporter_get_from_attrs(struct devlink *devlink, |
| 4791 | struct nlattr **attrs) |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4792 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4793 | struct devlink_health_reporter *reporter; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4794 | char *reporter_name; |
| 4795 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4796 | if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]) |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4797 | return NULL; |
| 4798 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4799 | reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4800 | mutex_lock(&devlink->reporters_lock); |
| 4801 | reporter = devlink_health_reporter_find_by_name(devlink, reporter_name); |
| 4802 | if (reporter) |
| 4803 | refcount_inc(&reporter->refcount); |
| 4804 | mutex_unlock(&devlink->reporters_lock); |
| 4805 | return reporter; |
| 4806 | } |
| 4807 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4808 | static struct devlink_health_reporter * |
| 4809 | devlink_health_reporter_get_from_info(struct devlink *devlink, |
| 4810 | struct genl_info *info) |
| 4811 | { |
| 4812 | return devlink_health_reporter_get_from_attrs(devlink, info->attrs); |
| 4813 | } |
| 4814 | |
| 4815 | static struct devlink_health_reporter * |
| 4816 | devlink_health_reporter_get_from_cb(struct netlink_callback *cb) |
| 4817 | { |
| 4818 | struct devlink_health_reporter *reporter; |
| 4819 | struct devlink *devlink; |
| 4820 | struct nlattr **attrs; |
| 4821 | int err; |
| 4822 | |
| 4823 | attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL); |
| 4824 | if (!attrs) |
| 4825 | return NULL; |
| 4826 | |
| 4827 | err = nlmsg_parse_deprecated(cb->nlh, |
| 4828 | GENL_HDRLEN + devlink_nl_family.hdrsize, |
| 4829 | attrs, DEVLINK_ATTR_MAX, |
| 4830 | devlink_nl_family.policy, cb->extack); |
| 4831 | if (err) |
| 4832 | goto free; |
| 4833 | |
| 4834 | mutex_lock(&devlink_mutex); |
| 4835 | devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs); |
| 4836 | if (IS_ERR(devlink)) |
| 4837 | goto unlock; |
| 4838 | |
| 4839 | reporter = devlink_health_reporter_get_from_attrs(devlink, attrs); |
| 4840 | mutex_unlock(&devlink_mutex); |
| 4841 | kfree(attrs); |
| 4842 | return reporter; |
| 4843 | unlock: |
| 4844 | mutex_unlock(&devlink_mutex); |
| 4845 | free: |
| 4846 | kfree(attrs); |
| 4847 | return NULL; |
| 4848 | } |
| 4849 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4850 | static void |
| 4851 | devlink_health_reporter_put(struct devlink_health_reporter *reporter) |
| 4852 | { |
| 4853 | refcount_dec(&reporter->refcount); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | static int |
| 4857 | devlink_nl_health_reporter_fill(struct sk_buff *msg, |
| 4858 | struct devlink *devlink, |
| 4859 | struct devlink_health_reporter *reporter, |
| 4860 | enum devlink_command cmd, u32 portid, |
| 4861 | u32 seq, int flags) |
| 4862 | { |
| 4863 | struct nlattr *reporter_attr; |
| 4864 | void *hdr; |
| 4865 | |
| 4866 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 4867 | if (!hdr) |
| 4868 | return -EMSGSIZE; |
| 4869 | |
| 4870 | if (devlink_nl_put_handle(msg, devlink)) |
| 4871 | goto genlmsg_cancel; |
| 4872 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4873 | reporter_attr = nla_nest_start_noflag(msg, |
| 4874 | DEVLINK_ATTR_HEALTH_REPORTER); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4875 | if (!reporter_attr) |
| 4876 | goto genlmsg_cancel; |
| 4877 | if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME, |
| 4878 | reporter->ops->name)) |
| 4879 | goto reporter_nest_cancel; |
| 4880 | if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE, |
| 4881 | reporter->health_state)) |
| 4882 | goto reporter_nest_cancel; |
Aya Levin | 5471952 | 2019-02-21 14:12:01 +0200 | [diff] [blame] | 4883 | 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] | 4884 | reporter->error_count, DEVLINK_ATTR_PAD)) |
| 4885 | goto reporter_nest_cancel; |
Aya Levin | 5471952 | 2019-02-21 14:12:01 +0200 | [diff] [blame] | 4886 | 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] | 4887 | reporter->recovery_count, DEVLINK_ATTR_PAD)) |
| 4888 | goto reporter_nest_cancel; |
Aya Levin | 574b1e1 | 2019-02-21 14:12:02 +0200 | [diff] [blame] | 4889 | if (reporter->ops->recover && |
| 4890 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4891 | reporter->graceful_period, |
| 4892 | DEVLINK_ATTR_PAD)) |
| 4893 | goto reporter_nest_cancel; |
Aya Levin | 574b1e1 | 2019-02-21 14:12:02 +0200 | [diff] [blame] | 4894 | if (reporter->ops->recover && |
| 4895 | nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4896 | reporter->auto_recover)) |
| 4897 | goto reporter_nest_cancel; |
| 4898 | if (reporter->dump_fmsg && |
| 4899 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS, |
| 4900 | jiffies_to_msecs(reporter->dump_ts), |
| 4901 | DEVLINK_ATTR_PAD)) |
| 4902 | goto reporter_nest_cancel; |
| 4903 | |
| 4904 | nla_nest_end(msg, reporter_attr); |
| 4905 | genlmsg_end(msg, hdr); |
| 4906 | return 0; |
| 4907 | |
| 4908 | reporter_nest_cancel: |
| 4909 | nla_nest_end(msg, reporter_attr); |
| 4910 | genlmsg_cancel: |
| 4911 | genlmsg_cancel(msg, hdr); |
| 4912 | return -EMSGSIZE; |
| 4913 | } |
| 4914 | |
| 4915 | static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb, |
| 4916 | struct genl_info *info) |
| 4917 | { |
| 4918 | struct devlink *devlink = info->user_ptr[0]; |
| 4919 | struct devlink_health_reporter *reporter; |
| 4920 | struct sk_buff *msg; |
| 4921 | int err; |
| 4922 | |
| 4923 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 4924 | if (!reporter) |
| 4925 | return -EINVAL; |
| 4926 | |
| 4927 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4928 | if (!msg) { |
| 4929 | err = -ENOMEM; |
| 4930 | goto out; |
| 4931 | } |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4932 | |
| 4933 | err = devlink_nl_health_reporter_fill(msg, devlink, reporter, |
| 4934 | DEVLINK_CMD_HEALTH_REPORTER_GET, |
| 4935 | info->snd_portid, info->snd_seq, |
| 4936 | 0); |
| 4937 | if (err) { |
| 4938 | nlmsg_free(msg); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4939 | goto out; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4940 | } |
| 4941 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4942 | err = genlmsg_reply(msg, info); |
| 4943 | out: |
| 4944 | devlink_health_reporter_put(reporter); |
| 4945 | return err; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | static int |
| 4949 | devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg, |
| 4950 | struct netlink_callback *cb) |
| 4951 | { |
| 4952 | struct devlink_health_reporter *reporter; |
| 4953 | struct devlink *devlink; |
| 4954 | int start = cb->args[0]; |
| 4955 | int idx = 0; |
| 4956 | int err; |
| 4957 | |
| 4958 | mutex_lock(&devlink_mutex); |
| 4959 | list_for_each_entry(devlink, &devlink_list, list) { |
| 4960 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 4961 | continue; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4962 | mutex_lock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4963 | list_for_each_entry(reporter, &devlink->reporter_list, |
| 4964 | list) { |
| 4965 | if (idx < start) { |
| 4966 | idx++; |
| 4967 | continue; |
| 4968 | } |
| 4969 | err = devlink_nl_health_reporter_fill(msg, devlink, |
| 4970 | reporter, |
| 4971 | DEVLINK_CMD_HEALTH_REPORTER_GET, |
| 4972 | NETLINK_CB(cb->skb).portid, |
| 4973 | cb->nlh->nlmsg_seq, |
| 4974 | NLM_F_MULTI); |
| 4975 | if (err) { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4976 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4977 | goto out; |
| 4978 | } |
| 4979 | idx++; |
| 4980 | } |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4981 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4982 | } |
| 4983 | out: |
| 4984 | mutex_unlock(&devlink_mutex); |
| 4985 | |
| 4986 | cb->args[0] = idx; |
| 4987 | return msg->len; |
| 4988 | } |
| 4989 | |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 4990 | static int |
| 4991 | devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb, |
| 4992 | struct genl_info *info) |
| 4993 | { |
| 4994 | struct devlink *devlink = info->user_ptr[0]; |
| 4995 | struct devlink_health_reporter *reporter; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4996 | int err; |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 4997 | |
| 4998 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 4999 | if (!reporter) |
| 5000 | return -EINVAL; |
| 5001 | |
| 5002 | if (!reporter->ops->recover && |
| 5003 | (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] || |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5004 | info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) { |
| 5005 | err = -EOPNOTSUPP; |
| 5006 | goto out; |
| 5007 | } |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5008 | |
| 5009 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]) |
| 5010 | reporter->graceful_period = |
| 5011 | nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]); |
| 5012 | |
| 5013 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]) |
| 5014 | reporter->auto_recover = |
| 5015 | nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]); |
| 5016 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5017 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5018 | return 0; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5019 | out: |
| 5020 | devlink_health_reporter_put(reporter); |
| 5021 | return err; |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5022 | } |
| 5023 | |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5024 | static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb, |
| 5025 | struct genl_info *info) |
| 5026 | { |
| 5027 | struct devlink *devlink = info->user_ptr[0]; |
| 5028 | struct devlink_health_reporter *reporter; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5029 | int err; |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5030 | |
| 5031 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5032 | if (!reporter) |
| 5033 | return -EINVAL; |
| 5034 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5035 | err = devlink_health_reporter_recover(reporter, NULL); |
| 5036 | |
| 5037 | devlink_health_reporter_put(reporter); |
| 5038 | return err; |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5039 | } |
| 5040 | |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5041 | static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb, |
| 5042 | struct genl_info *info) |
| 5043 | { |
| 5044 | struct devlink *devlink = info->user_ptr[0]; |
| 5045 | struct devlink_health_reporter *reporter; |
| 5046 | struct devlink_fmsg *fmsg; |
| 5047 | int err; |
| 5048 | |
| 5049 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5050 | if (!reporter) |
| 5051 | return -EINVAL; |
| 5052 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5053 | if (!reporter->ops->diagnose) { |
| 5054 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5055 | return -EOPNOTSUPP; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5056 | } |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5057 | |
| 5058 | fmsg = devlink_fmsg_alloc(); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5059 | if (!fmsg) { |
| 5060 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5061 | return -ENOMEM; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5062 | } |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5063 | |
| 5064 | err = devlink_fmsg_obj_nest_start(fmsg); |
| 5065 | if (err) |
| 5066 | goto out; |
| 5067 | |
| 5068 | err = reporter->ops->diagnose(reporter, fmsg); |
| 5069 | if (err) |
| 5070 | goto out; |
| 5071 | |
| 5072 | err = devlink_fmsg_obj_nest_end(fmsg); |
| 5073 | if (err) |
| 5074 | goto out; |
| 5075 | |
| 5076 | err = devlink_fmsg_snd(fmsg, info, |
| 5077 | DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0); |
| 5078 | |
| 5079 | out: |
| 5080 | devlink_fmsg_free(fmsg); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5081 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5082 | return err; |
| 5083 | } |
| 5084 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5085 | static int |
| 5086 | devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb, |
| 5087 | struct netlink_callback *cb) |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5088 | { |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5089 | struct devlink_health_reporter *reporter; |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5090 | u64 start = cb->args[0]; |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5091 | int err; |
| 5092 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5093 | reporter = devlink_health_reporter_get_from_cb(cb); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5094 | if (!reporter) |
| 5095 | return -EINVAL; |
| 5096 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5097 | if (!reporter->ops->dump) { |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5098 | err = -EOPNOTSUPP; |
| 5099 | goto out; |
| 5100 | } |
| 5101 | mutex_lock(&reporter->dump_lock); |
| 5102 | if (!start) { |
| 5103 | err = devlink_health_do_dump(reporter, NULL); |
| 5104 | if (err) |
| 5105 | goto unlock; |
| 5106 | cb->args[1] = reporter->dump_ts; |
| 5107 | } |
| 5108 | if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) { |
| 5109 | NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry"); |
| 5110 | err = -EAGAIN; |
| 5111 | goto unlock; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5112 | } |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5113 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5114 | err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb, |
| 5115 | DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET); |
| 5116 | unlock: |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5117 | mutex_unlock(&reporter->dump_lock); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5118 | out: |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5119 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5120 | return err; |
| 5121 | } |
| 5122 | |
| 5123 | static int |
| 5124 | devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb, |
| 5125 | struct genl_info *info) |
| 5126 | { |
| 5127 | struct devlink *devlink = info->user_ptr[0]; |
| 5128 | struct devlink_health_reporter *reporter; |
| 5129 | |
| 5130 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5131 | if (!reporter) |
| 5132 | return -EINVAL; |
| 5133 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5134 | if (!reporter->ops->dump) { |
| 5135 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5136 | return -EOPNOTSUPP; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5137 | } |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5138 | |
| 5139 | mutex_lock(&reporter->dump_lock); |
| 5140 | devlink_health_dump_clear(reporter); |
| 5141 | mutex_unlock(&reporter->dump_lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5142 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5143 | return 0; |
| 5144 | } |
| 5145 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5146 | static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = { |
| 5147 | [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING }, |
| 5148 | [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING }, |
| 5149 | [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 }, |
| 5150 | [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 }, |
| 5151 | [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5152 | [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 }, |
| 5153 | [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 }, |
| 5154 | [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 }, |
| 5155 | [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 }, |
| 5156 | [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 }, |
| 5157 | [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 }, |
| 5158 | [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 5159 | [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 }, |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 5160 | [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 }, |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 5161 | [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5162 | [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING }, |
| 5163 | [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5164 | [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64}, |
| 5165 | [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64}, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 5166 | [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING }, |
| 5167 | [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 }, |
| 5168 | [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 }, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 5169 | [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING }, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 5170 | [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 }, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5171 | [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING }, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5172 | [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 }, |
| 5173 | [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 }, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 5174 | [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING }, |
| 5175 | [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5176 | }; |
| 5177 | |
| 5178 | static const struct genl_ops devlink_nl_ops[] = { |
| 5179 | { |
| 5180 | .cmd = DEVLINK_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5181 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5182 | .doit = devlink_nl_cmd_get_doit, |
| 5183 | .dumpit = devlink_nl_cmd_get_dumpit, |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 5184 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5185 | /* can be retrieved by unprivileged users */ |
| 5186 | }, |
| 5187 | { |
| 5188 | .cmd = DEVLINK_CMD_PORT_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5189 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5190 | .doit = devlink_nl_cmd_port_get_doit, |
| 5191 | .dumpit = devlink_nl_cmd_port_get_dumpit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5192 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 5193 | /* can be retrieved by unprivileged users */ |
| 5194 | }, |
| 5195 | { |
| 5196 | .cmd = DEVLINK_CMD_PORT_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5197 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5198 | .doit = devlink_nl_cmd_port_set_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5199 | .flags = GENL_ADMIN_PERM, |
| 5200 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 5201 | }, |
| 5202 | { |
| 5203 | .cmd = DEVLINK_CMD_PORT_SPLIT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5204 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5205 | .doit = devlink_nl_cmd_port_split_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5206 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5207 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5208 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5209 | }, |
| 5210 | { |
| 5211 | .cmd = DEVLINK_CMD_PORT_UNSPLIT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5212 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5213 | .doit = devlink_nl_cmd_port_unsplit_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5214 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5215 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5216 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5217 | }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5218 | { |
| 5219 | .cmd = DEVLINK_CMD_SB_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5220 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5221 | .doit = devlink_nl_cmd_sb_get_doit, |
| 5222 | .dumpit = devlink_nl_cmd_sb_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5223 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5224 | DEVLINK_NL_FLAG_NEED_SB, |
| 5225 | /* can be retrieved by unprivileged users */ |
| 5226 | }, |
| 5227 | { |
| 5228 | .cmd = DEVLINK_CMD_SB_POOL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5229 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5230 | .doit = devlink_nl_cmd_sb_pool_get_doit, |
| 5231 | .dumpit = devlink_nl_cmd_sb_pool_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5232 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5233 | DEVLINK_NL_FLAG_NEED_SB, |
| 5234 | /* can be retrieved by unprivileged users */ |
| 5235 | }, |
| 5236 | { |
| 5237 | .cmd = DEVLINK_CMD_SB_POOL_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5238 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5239 | .doit = devlink_nl_cmd_sb_pool_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5240 | .flags = GENL_ADMIN_PERM, |
| 5241 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5242 | DEVLINK_NL_FLAG_NEED_SB, |
| 5243 | }, |
| 5244 | { |
| 5245 | .cmd = DEVLINK_CMD_SB_PORT_POOL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5246 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5247 | .doit = devlink_nl_cmd_sb_port_pool_get_doit, |
| 5248 | .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5249 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 5250 | DEVLINK_NL_FLAG_NEED_SB, |
| 5251 | /* can be retrieved by unprivileged users */ |
| 5252 | }, |
| 5253 | { |
| 5254 | .cmd = DEVLINK_CMD_SB_PORT_POOL_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5255 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5256 | .doit = devlink_nl_cmd_sb_port_pool_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5257 | .flags = GENL_ADMIN_PERM, |
| 5258 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 5259 | DEVLINK_NL_FLAG_NEED_SB, |
| 5260 | }, |
| 5261 | { |
| 5262 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5263 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5264 | .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit, |
| 5265 | .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5266 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 5267 | DEVLINK_NL_FLAG_NEED_SB, |
| 5268 | /* can be retrieved by unprivileged users */ |
| 5269 | }, |
| 5270 | { |
| 5271 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5272 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5273 | .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5274 | .flags = GENL_ADMIN_PERM, |
| 5275 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 5276 | DEVLINK_NL_FLAG_NEED_SB, |
| 5277 | }, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 5278 | { |
| 5279 | .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5280 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 5281 | .doit = devlink_nl_cmd_sb_occ_snapshot_doit, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 5282 | .flags = GENL_ADMIN_PERM, |
| 5283 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5284 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 5285 | }, |
| 5286 | { |
| 5287 | .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5288 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 5289 | .doit = devlink_nl_cmd_sb_occ_max_clear_doit, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 5290 | .flags = GENL_ADMIN_PERM, |
| 5291 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5292 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 5293 | }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 5294 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 5295 | .cmd = DEVLINK_CMD_ESWITCH_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5296 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 5297 | .doit = devlink_nl_cmd_eswitch_get_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 5298 | .flags = GENL_ADMIN_PERM, |
| 5299 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5300 | }, |
| 5301 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 5302 | .cmd = DEVLINK_CMD_ESWITCH_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5303 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 5304 | .doit = devlink_nl_cmd_eswitch_set_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 5305 | .flags = GENL_ADMIN_PERM, |
Jakub Kicinski | 7ac1cc9 | 2018-05-21 22:12:50 -0700 | [diff] [blame] | 5306 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5307 | DEVLINK_NL_FLAG_NO_LOCK, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 5308 | }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5309 | { |
| 5310 | .cmd = DEVLINK_CMD_DPIPE_TABLE_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5311 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5312 | .doit = devlink_nl_cmd_dpipe_table_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5313 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 5314 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5315 | }, |
| 5316 | { |
| 5317 | .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5318 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5319 | .doit = devlink_nl_cmd_dpipe_entries_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5320 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 5321 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5322 | }, |
| 5323 | { |
| 5324 | .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5325 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5326 | .doit = devlink_nl_cmd_dpipe_headers_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5327 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 5328 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5329 | }, |
| 5330 | { |
| 5331 | .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5332 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5333 | .doit = devlink_nl_cmd_dpipe_table_counters_set, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5334 | .flags = GENL_ADMIN_PERM, |
| 5335 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5336 | }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5337 | { |
| 5338 | .cmd = DEVLINK_CMD_RESOURCE_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5339 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5340 | .doit = devlink_nl_cmd_resource_set, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5341 | .flags = GENL_ADMIN_PERM, |
| 5342 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5343 | }, |
| 5344 | { |
| 5345 | .cmd = DEVLINK_CMD_RESOURCE_DUMP, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5346 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5347 | .doit = devlink_nl_cmd_resource_dump, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5348 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 5349 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5350 | }, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 5351 | { |
| 5352 | .cmd = DEVLINK_CMD_RELOAD, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5353 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 5354 | .doit = devlink_nl_cmd_reload, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 5355 | .flags = GENL_ADMIN_PERM, |
| 5356 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5357 | DEVLINK_NL_FLAG_NO_LOCK, |
| 5358 | }, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 5359 | { |
| 5360 | .cmd = DEVLINK_CMD_PARAM_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5361 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 5362 | .doit = devlink_nl_cmd_param_get_doit, |
| 5363 | .dumpit = devlink_nl_cmd_param_get_dumpit, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 5364 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5365 | /* can be retrieved by unprivileged users */ |
| 5366 | }, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 5367 | { |
| 5368 | .cmd = DEVLINK_CMD_PARAM_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5369 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 5370 | .doit = devlink_nl_cmd_param_set_doit, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 5371 | .flags = GENL_ADMIN_PERM, |
| 5372 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5373 | }, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 5374 | { |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 5375 | .cmd = DEVLINK_CMD_PORT_PARAM_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5376 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 5377 | .doit = devlink_nl_cmd_port_param_get_doit, |
| 5378 | .dumpit = devlink_nl_cmd_port_param_get_dumpit, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 5379 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 5380 | /* can be retrieved by unprivileged users */ |
| 5381 | }, |
| 5382 | { |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 5383 | .cmd = DEVLINK_CMD_PORT_PARAM_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5384 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 5385 | .doit = devlink_nl_cmd_port_param_set_doit, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 5386 | .flags = GENL_ADMIN_PERM, |
| 5387 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 5388 | }, |
| 5389 | { |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 5390 | .cmd = DEVLINK_CMD_REGION_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5391 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 5392 | .doit = devlink_nl_cmd_region_get_doit, |
| 5393 | .dumpit = devlink_nl_cmd_region_get_dumpit, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 5394 | .flags = GENL_ADMIN_PERM, |
| 5395 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5396 | }, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 5397 | { |
| 5398 | .cmd = DEVLINK_CMD_REGION_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5399 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 5400 | .doit = devlink_nl_cmd_region_del, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 5401 | .flags = GENL_ADMIN_PERM, |
| 5402 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5403 | }, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 5404 | { |
| 5405 | .cmd = DEVLINK_CMD_REGION_READ, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5406 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 5407 | .dumpit = devlink_nl_cmd_region_read_dumpit, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 5408 | .flags = GENL_ADMIN_PERM, |
| 5409 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5410 | }, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 5411 | { |
| 5412 | .cmd = DEVLINK_CMD_INFO_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5413 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 5414 | .doit = devlink_nl_cmd_info_get_doit, |
| 5415 | .dumpit = devlink_nl_cmd_info_get_dumpit, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 5416 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5417 | /* can be retrieved by unprivileged users */ |
| 5418 | }, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5419 | { |
| 5420 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5421 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5422 | .doit = devlink_nl_cmd_health_reporter_get_doit, |
| 5423 | .dumpit = devlink_nl_cmd_health_reporter_get_dumpit, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5424 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5425 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5426 | /* can be retrieved by unprivileged users */ |
| 5427 | }, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5428 | { |
| 5429 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5430 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5431 | .doit = devlink_nl_cmd_health_reporter_set_doit, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5432 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5433 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5434 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5435 | }, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5436 | { |
| 5437 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5438 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5439 | .doit = devlink_nl_cmd_health_reporter_recover_doit, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5440 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5441 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5442 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5443 | }, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5444 | { |
| 5445 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5446 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5447 | .doit = devlink_nl_cmd_health_reporter_diagnose_doit, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5448 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5449 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5450 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5451 | }, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5452 | { |
| 5453 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5454 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5455 | .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5456 | .flags = GENL_ADMIN_PERM, |
| 5457 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5458 | DEVLINK_NL_FLAG_NO_LOCK, |
| 5459 | }, |
| 5460 | { |
| 5461 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5462 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5463 | .doit = devlink_nl_cmd_health_reporter_dump_clear_doit, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5464 | .flags = GENL_ADMIN_PERM, |
| 5465 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5466 | DEVLINK_NL_FLAG_NO_LOCK, |
| 5467 | }, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 5468 | { |
| 5469 | .cmd = DEVLINK_CMD_FLASH_UPDATE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5470 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 5471 | .doit = devlink_nl_cmd_flash_update, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 5472 | .flags = GENL_ADMIN_PERM, |
| 5473 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 5474 | }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5475 | }; |
| 5476 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 5477 | static struct genl_family devlink_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 5478 | .name = DEVLINK_GENL_NAME, |
| 5479 | .version = DEVLINK_GENL_VERSION, |
| 5480 | .maxattr = DEVLINK_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 5481 | .policy = devlink_nl_policy, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 5482 | .netnsok = true, |
| 5483 | .pre_doit = devlink_nl_pre_doit, |
| 5484 | .post_doit = devlink_nl_post_doit, |
| 5485 | .module = THIS_MODULE, |
| 5486 | .ops = devlink_nl_ops, |
| 5487 | .n_ops = ARRAY_SIZE(devlink_nl_ops), |
| 5488 | .mcgrps = devlink_nl_mcgrps, |
| 5489 | .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps), |
| 5490 | }; |
| 5491 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5492 | /** |
| 5493 | * devlink_alloc - Allocate new devlink instance resources |
| 5494 | * |
| 5495 | * @ops: ops |
| 5496 | * @priv_size: size of user private data |
| 5497 | * |
| 5498 | * Allocate new devlink instance resources, including devlink index |
| 5499 | * and name. |
| 5500 | */ |
| 5501 | struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) |
| 5502 | { |
| 5503 | struct devlink *devlink; |
| 5504 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 5505 | if (WARN_ON(!ops)) |
| 5506 | return NULL; |
| 5507 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5508 | devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL); |
| 5509 | if (!devlink) |
| 5510 | return NULL; |
| 5511 | devlink->ops = ops; |
| 5512 | devlink_net_set(devlink, &init_net); |
| 5513 | INIT_LIST_HEAD(&devlink->port_list); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5514 | INIT_LIST_HEAD(&devlink->sb_list); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5515 | INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5516 | INIT_LIST_HEAD(&devlink->resource_list); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 5517 | INIT_LIST_HEAD(&devlink->param_list); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 5518 | INIT_LIST_HEAD(&devlink->region_list); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 5519 | INIT_LIST_HEAD(&devlink->reporter_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5520 | mutex_init(&devlink->lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5521 | mutex_init(&devlink->reporters_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5522 | return devlink; |
| 5523 | } |
| 5524 | EXPORT_SYMBOL_GPL(devlink_alloc); |
| 5525 | |
| 5526 | /** |
| 5527 | * devlink_register - Register devlink instance |
| 5528 | * |
| 5529 | * @devlink: devlink |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 5530 | * @dev: parent device |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5531 | */ |
| 5532 | int devlink_register(struct devlink *devlink, struct device *dev) |
| 5533 | { |
| 5534 | mutex_lock(&devlink_mutex); |
| 5535 | devlink->dev = dev; |
| 5536 | list_add_tail(&devlink->list, &devlink_list); |
| 5537 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 5538 | mutex_unlock(&devlink_mutex); |
| 5539 | return 0; |
| 5540 | } |
| 5541 | EXPORT_SYMBOL_GPL(devlink_register); |
| 5542 | |
| 5543 | /** |
| 5544 | * devlink_unregister - Unregister devlink instance |
| 5545 | * |
| 5546 | * @devlink: devlink |
| 5547 | */ |
| 5548 | void devlink_unregister(struct devlink *devlink) |
| 5549 | { |
| 5550 | mutex_lock(&devlink_mutex); |
| 5551 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 5552 | list_del(&devlink->list); |
| 5553 | mutex_unlock(&devlink_mutex); |
| 5554 | } |
| 5555 | EXPORT_SYMBOL_GPL(devlink_unregister); |
| 5556 | |
| 5557 | /** |
| 5558 | * devlink_free - Free devlink instance resources |
| 5559 | * |
| 5560 | * @devlink: devlink |
| 5561 | */ |
| 5562 | void devlink_free(struct devlink *devlink) |
| 5563 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5564 | mutex_destroy(&devlink->reporters_lock); |
Jiri Pirko | 375cf8c | 2019-03-24 11:14:24 +0100 | [diff] [blame] | 5565 | mutex_destroy(&devlink->lock); |
Parav Pandit | b904aad | 2019-02-08 15:15:00 -0600 | [diff] [blame] | 5566 | WARN_ON(!list_empty(&devlink->reporter_list)); |
| 5567 | WARN_ON(!list_empty(&devlink->region_list)); |
| 5568 | WARN_ON(!list_empty(&devlink->param_list)); |
| 5569 | WARN_ON(!list_empty(&devlink->resource_list)); |
| 5570 | WARN_ON(!list_empty(&devlink->dpipe_table_list)); |
| 5571 | WARN_ON(!list_empty(&devlink->sb_list)); |
| 5572 | WARN_ON(!list_empty(&devlink->port_list)); |
| 5573 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5574 | kfree(devlink); |
| 5575 | } |
| 5576 | EXPORT_SYMBOL_GPL(devlink_free); |
| 5577 | |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 5578 | static void devlink_port_type_warn(struct work_struct *work) |
| 5579 | { |
| 5580 | WARN(true, "Type was not set for devlink port."); |
| 5581 | } |
| 5582 | |
| 5583 | static bool devlink_port_type_should_warn(struct devlink_port *devlink_port) |
| 5584 | { |
| 5585 | /* Ignore CPU and DSA flavours. */ |
| 5586 | return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU && |
| 5587 | devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA; |
| 5588 | } |
| 5589 | |
| 5590 | #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 30) |
| 5591 | |
| 5592 | static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port) |
| 5593 | { |
| 5594 | if (!devlink_port_type_should_warn(devlink_port)) |
| 5595 | return; |
| 5596 | /* Schedule a work to WARN in case driver does not set port |
| 5597 | * type within timeout. |
| 5598 | */ |
| 5599 | schedule_delayed_work(&devlink_port->type_warn_dw, |
| 5600 | DEVLINK_PORT_TYPE_WARN_TIMEOUT); |
| 5601 | } |
| 5602 | |
| 5603 | static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port) |
| 5604 | { |
| 5605 | if (!devlink_port_type_should_warn(devlink_port)) |
| 5606 | return; |
| 5607 | cancel_delayed_work_sync(&devlink_port->type_warn_dw); |
| 5608 | } |
| 5609 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5610 | /** |
| 5611 | * devlink_port_register - Register devlink port |
| 5612 | * |
| 5613 | * @devlink: devlink |
| 5614 | * @devlink_port: devlink port |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 5615 | * @port_index: driver-specific numerical identifier of the port |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5616 | * |
| 5617 | * Register devlink port with provided port index. User can use |
| 5618 | * any indexing, even hw-related one. devlink_port structure |
| 5619 | * is convenient to be embedded inside user driver private structure. |
| 5620 | * Note that the caller should take care of zeroing the devlink_port |
| 5621 | * structure. |
| 5622 | */ |
| 5623 | int devlink_port_register(struct devlink *devlink, |
| 5624 | struct devlink_port *devlink_port, |
| 5625 | unsigned int port_index) |
| 5626 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5627 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5628 | if (devlink_port_index_exists(devlink, port_index)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5629 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5630 | return -EEXIST; |
| 5631 | } |
| 5632 | devlink_port->devlink = devlink; |
| 5633 | devlink_port->index = port_index; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5634 | devlink_port->registered = true; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 5635 | spin_lock_init(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5636 | list_add_tail(&devlink_port->list, &devlink->port_list); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 5637 | INIT_LIST_HEAD(&devlink_port->param_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5638 | mutex_unlock(&devlink->lock); |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 5639 | INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn); |
| 5640 | devlink_port_type_warn_schedule(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5641 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 5642 | return 0; |
| 5643 | } |
| 5644 | EXPORT_SYMBOL_GPL(devlink_port_register); |
| 5645 | |
| 5646 | /** |
| 5647 | * devlink_port_unregister - Unregister devlink port |
| 5648 | * |
| 5649 | * @devlink_port: devlink port |
| 5650 | */ |
| 5651 | void devlink_port_unregister(struct devlink_port *devlink_port) |
| 5652 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5653 | struct devlink *devlink = devlink_port->devlink; |
| 5654 | |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 5655 | devlink_port_type_warn_cancel(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5656 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5657 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5658 | list_del(&devlink_port->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5659 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5660 | } |
| 5661 | EXPORT_SYMBOL_GPL(devlink_port_unregister); |
| 5662 | |
| 5663 | static void __devlink_port_type_set(struct devlink_port *devlink_port, |
| 5664 | enum devlink_port_type type, |
| 5665 | void *type_dev) |
| 5666 | { |
Jiri Pirko | 2b239e7 | 2019-03-24 11:14:36 +0100 | [diff] [blame] | 5667 | if (WARN_ON(!devlink_port->registered)) |
| 5668 | return; |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 5669 | devlink_port_type_warn_cancel(devlink_port); |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 5670 | spin_lock(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5671 | devlink_port->type = type; |
| 5672 | devlink_port->type_dev = type_dev; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 5673 | spin_unlock(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5674 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 5675 | } |
| 5676 | |
| 5677 | /** |
| 5678 | * devlink_port_type_eth_set - Set port type to Ethernet |
| 5679 | * |
| 5680 | * @devlink_port: devlink port |
| 5681 | * @netdev: related netdevice |
| 5682 | */ |
| 5683 | void devlink_port_type_eth_set(struct devlink_port *devlink_port, |
| 5684 | struct net_device *netdev) |
| 5685 | { |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 5686 | const struct net_device_ops *ops = netdev->netdev_ops; |
| 5687 | |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 5688 | /* If driver registers devlink port, it should set devlink port |
| 5689 | * attributes accordingly so the compat functions are called |
| 5690 | * and the original ops are not used. |
| 5691 | */ |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 5692 | if (ops->ndo_get_phys_port_name) { |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 5693 | /* Some drivers use the same set of ndos for netdevs |
| 5694 | * that have devlink_port registered and also for |
| 5695 | * those who don't. Make sure that ndo_get_phys_port_name |
| 5696 | * returns -EOPNOTSUPP here in case it is defined. |
| 5697 | * Warn if not. |
| 5698 | */ |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 5699 | char name[IFNAMSIZ]; |
| 5700 | int err; |
| 5701 | |
| 5702 | err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name)); |
| 5703 | WARN_ON(err != -EOPNOTSUPP); |
| 5704 | } |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 5705 | if (ops->ndo_get_port_parent_id) { |
| 5706 | /* Some drivers use the same set of ndos for netdevs |
| 5707 | * that have devlink_port registered and also for |
| 5708 | * those who don't. Make sure that ndo_get_port_parent_id |
| 5709 | * returns -EOPNOTSUPP here in case it is defined. |
| 5710 | * Warn if not. |
| 5711 | */ |
| 5712 | struct netdev_phys_item_id ppid; |
| 5713 | int err; |
| 5714 | |
| 5715 | err = ops->ndo_get_port_parent_id(netdev, &ppid); |
| 5716 | WARN_ON(err != -EOPNOTSUPP); |
| 5717 | } |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 5718 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5719 | } |
| 5720 | EXPORT_SYMBOL_GPL(devlink_port_type_eth_set); |
| 5721 | |
| 5722 | /** |
| 5723 | * devlink_port_type_ib_set - Set port type to InfiniBand |
| 5724 | * |
| 5725 | * @devlink_port: devlink port |
| 5726 | * @ibdev: related IB device |
| 5727 | */ |
| 5728 | void devlink_port_type_ib_set(struct devlink_port *devlink_port, |
| 5729 | struct ib_device *ibdev) |
| 5730 | { |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 5731 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5732 | } |
| 5733 | EXPORT_SYMBOL_GPL(devlink_port_type_ib_set); |
| 5734 | |
| 5735 | /** |
| 5736 | * devlink_port_type_clear - Clear port type |
| 5737 | * |
| 5738 | * @devlink_port: devlink port |
| 5739 | */ |
| 5740 | void devlink_port_type_clear(struct devlink_port *devlink_port) |
| 5741 | { |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 5742 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL); |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 5743 | devlink_port_type_warn_schedule(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5744 | } |
| 5745 | EXPORT_SYMBOL_GPL(devlink_port_type_clear); |
| 5746 | |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 5747 | static int __devlink_port_attrs_set(struct devlink_port *devlink_port, |
| 5748 | enum devlink_port_flavour flavour, |
| 5749 | const unsigned char *switch_id, |
| 5750 | unsigned char switch_id_len) |
| 5751 | { |
| 5752 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 5753 | |
| 5754 | if (WARN_ON(devlink_port->registered)) |
| 5755 | return -EEXIST; |
| 5756 | attrs->set = true; |
| 5757 | attrs->flavour = flavour; |
| 5758 | if (switch_id) { |
| 5759 | attrs->switch_port = true; |
| 5760 | if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN)) |
| 5761 | switch_id_len = MAX_PHYS_ITEM_ID_LEN; |
| 5762 | memcpy(attrs->switch_id.id, switch_id, switch_id_len); |
| 5763 | attrs->switch_id.id_len = switch_id_len; |
| 5764 | } else { |
| 5765 | attrs->switch_port = false; |
| 5766 | } |
| 5767 | return 0; |
| 5768 | } |
| 5769 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5770 | /** |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 5771 | * devlink_port_attrs_set - Set port attributes |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5772 | * |
| 5773 | * @devlink_port: devlink port |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 5774 | * @flavour: flavour of the port |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 5775 | * @port_number: number of the port that is facing user, for example |
| 5776 | * the front panel port number |
| 5777 | * @split: indicates if this is split port |
| 5778 | * @split_subport_number: if the port is split, this is the number |
| 5779 | * of subport. |
Jiri Pirko | bec5267 | 2019-04-03 14:24:16 +0200 | [diff] [blame] | 5780 | * @switch_id: if the port is part of switch, this is buffer with ID, |
| 5781 | * otwerwise this is NULL |
| 5782 | * @switch_id_len: length of the switch_id buffer |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5783 | */ |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 5784 | void devlink_port_attrs_set(struct devlink_port *devlink_port, |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 5785 | enum devlink_port_flavour flavour, |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 5786 | u32 port_number, bool split, |
Jiri Pirko | bec5267 | 2019-04-03 14:24:16 +0200 | [diff] [blame] | 5787 | u32 split_subport_number, |
| 5788 | const unsigned char *switch_id, |
| 5789 | unsigned char switch_id_len) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5790 | { |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 5791 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 5792 | int ret; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 5793 | |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 5794 | ret = __devlink_port_attrs_set(devlink_port, flavour, |
| 5795 | switch_id, switch_id_len); |
| 5796 | if (ret) |
Jiri Pirko | 45b8611 | 2019-03-24 11:14:33 +0100 | [diff] [blame] | 5797 | return; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 5798 | attrs->split = split; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 5799 | attrs->phys.port_number = port_number; |
| 5800 | attrs->phys.split_subport_number = split_subport_number; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5801 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 5802 | EXPORT_SYMBOL_GPL(devlink_port_attrs_set); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5803 | |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 5804 | static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port, |
| 5805 | char *name, size_t len) |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 5806 | { |
| 5807 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 5808 | int n = 0; |
| 5809 | |
| 5810 | if (!attrs->set) |
| 5811 | return -EOPNOTSUPP; |
| 5812 | |
| 5813 | switch (attrs->flavour) { |
| 5814 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: |
| 5815 | if (!attrs->split) |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 5816 | n = snprintf(name, len, "p%u", attrs->phys.port_number); |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 5817 | else |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 5818 | n = snprintf(name, len, "p%us%u", |
| 5819 | attrs->phys.port_number, |
| 5820 | attrs->phys.split_subport_number); |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 5821 | break; |
| 5822 | case DEVLINK_PORT_FLAVOUR_CPU: |
| 5823 | case DEVLINK_PORT_FLAVOUR_DSA: |
| 5824 | /* As CPU and DSA ports do not have a netdevice associated |
| 5825 | * case should not ever happen. |
| 5826 | */ |
| 5827 | WARN_ON(1); |
| 5828 | return -EINVAL; |
| 5829 | } |
| 5830 | |
| 5831 | if (n >= len) |
| 5832 | return -EINVAL; |
| 5833 | |
| 5834 | return 0; |
| 5835 | } |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 5836 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5837 | int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, |
| 5838 | u32 size, u16 ingress_pools_count, |
| 5839 | u16 egress_pools_count, u16 ingress_tc_count, |
| 5840 | u16 egress_tc_count) |
| 5841 | { |
| 5842 | struct devlink_sb *devlink_sb; |
| 5843 | int err = 0; |
| 5844 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5845 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5846 | if (devlink_sb_index_exists(devlink, sb_index)) { |
| 5847 | err = -EEXIST; |
| 5848 | goto unlock; |
| 5849 | } |
| 5850 | |
| 5851 | devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL); |
| 5852 | if (!devlink_sb) { |
| 5853 | err = -ENOMEM; |
| 5854 | goto unlock; |
| 5855 | } |
| 5856 | devlink_sb->index = sb_index; |
| 5857 | devlink_sb->size = size; |
| 5858 | devlink_sb->ingress_pools_count = ingress_pools_count; |
| 5859 | devlink_sb->egress_pools_count = egress_pools_count; |
| 5860 | devlink_sb->ingress_tc_count = ingress_tc_count; |
| 5861 | devlink_sb->egress_tc_count = egress_tc_count; |
| 5862 | list_add_tail(&devlink_sb->list, &devlink->sb_list); |
| 5863 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5864 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5865 | return err; |
| 5866 | } |
| 5867 | EXPORT_SYMBOL_GPL(devlink_sb_register); |
| 5868 | |
| 5869 | void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index) |
| 5870 | { |
| 5871 | struct devlink_sb *devlink_sb; |
| 5872 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5873 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5874 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 5875 | WARN_ON(!devlink_sb); |
| 5876 | list_del(&devlink_sb->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5877 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5878 | kfree(devlink_sb); |
| 5879 | } |
| 5880 | EXPORT_SYMBOL_GPL(devlink_sb_unregister); |
| 5881 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5882 | /** |
| 5883 | * devlink_dpipe_headers_register - register dpipe headers |
| 5884 | * |
| 5885 | * @devlink: devlink |
| 5886 | * @dpipe_headers: dpipe header array |
| 5887 | * |
| 5888 | * Register the headers supported by hardware. |
| 5889 | */ |
| 5890 | int devlink_dpipe_headers_register(struct devlink *devlink, |
| 5891 | struct devlink_dpipe_headers *dpipe_headers) |
| 5892 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5893 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5894 | devlink->dpipe_headers = dpipe_headers; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5895 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5896 | return 0; |
| 5897 | } |
| 5898 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register); |
| 5899 | |
| 5900 | /** |
| 5901 | * devlink_dpipe_headers_unregister - unregister dpipe headers |
| 5902 | * |
| 5903 | * @devlink: devlink |
| 5904 | * |
| 5905 | * Unregister the headers supported by hardware. |
| 5906 | */ |
| 5907 | void devlink_dpipe_headers_unregister(struct devlink *devlink) |
| 5908 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5909 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5910 | devlink->dpipe_headers = NULL; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5911 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5912 | } |
| 5913 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister); |
| 5914 | |
| 5915 | /** |
| 5916 | * devlink_dpipe_table_counter_enabled - check if counter allocation |
| 5917 | * required |
| 5918 | * @devlink: devlink |
| 5919 | * @table_name: tables name |
| 5920 | * |
| 5921 | * Used by driver to check if counter allocation is required. |
| 5922 | * After counter allocation is turned on the table entries |
| 5923 | * are updated to include counter statistics. |
| 5924 | * |
| 5925 | * After that point on the driver must respect the counter |
| 5926 | * state so that each entry added to the table is added |
| 5927 | * with a counter. |
| 5928 | */ |
| 5929 | bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, |
| 5930 | const char *table_name) |
| 5931 | { |
| 5932 | struct devlink_dpipe_table *table; |
| 5933 | bool enabled; |
| 5934 | |
| 5935 | rcu_read_lock(); |
| 5936 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 5937 | table_name); |
| 5938 | enabled = false; |
| 5939 | if (table) |
| 5940 | enabled = table->counters_enabled; |
| 5941 | rcu_read_unlock(); |
| 5942 | return enabled; |
| 5943 | } |
| 5944 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled); |
| 5945 | |
| 5946 | /** |
| 5947 | * devlink_dpipe_table_register - register dpipe table |
| 5948 | * |
| 5949 | * @devlink: devlink |
| 5950 | * @table_name: table name |
| 5951 | * @table_ops: table ops |
| 5952 | * @priv: priv |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5953 | * @counter_control_extern: external control for counters |
| 5954 | */ |
| 5955 | int devlink_dpipe_table_register(struct devlink *devlink, |
| 5956 | const char *table_name, |
| 5957 | struct devlink_dpipe_table_ops *table_ops, |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 5958 | void *priv, bool counter_control_extern) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5959 | { |
| 5960 | struct devlink_dpipe_table *table; |
| 5961 | |
| 5962 | if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name)) |
| 5963 | return -EEXIST; |
| 5964 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 5965 | if (WARN_ON(!table_ops->size_get)) |
| 5966 | return -EINVAL; |
| 5967 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5968 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
| 5969 | if (!table) |
| 5970 | return -ENOMEM; |
| 5971 | |
| 5972 | table->name = table_name; |
| 5973 | table->table_ops = table_ops; |
| 5974 | table->priv = priv; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5975 | table->counter_control_extern = counter_control_extern; |
| 5976 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5977 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5978 | list_add_tail_rcu(&table->list, &devlink->dpipe_table_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5979 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5980 | return 0; |
| 5981 | } |
| 5982 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_register); |
| 5983 | |
| 5984 | /** |
| 5985 | * devlink_dpipe_table_unregister - unregister dpipe table |
| 5986 | * |
| 5987 | * @devlink: devlink |
| 5988 | * @table_name: table name |
| 5989 | */ |
| 5990 | void devlink_dpipe_table_unregister(struct devlink *devlink, |
| 5991 | const char *table_name) |
| 5992 | { |
| 5993 | struct devlink_dpipe_table *table; |
| 5994 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5995 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5996 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 5997 | table_name); |
| 5998 | if (!table) |
| 5999 | goto unlock; |
| 6000 | list_del_rcu(&table->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6001 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6002 | kfree_rcu(table, rcu); |
| 6003 | return; |
| 6004 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6005 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6006 | } |
| 6007 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister); |
| 6008 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6009 | /** |
| 6010 | * devlink_resource_register - devlink resource register |
| 6011 | * |
| 6012 | * @devlink: devlink |
| 6013 | * @resource_name: resource's name |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6014 | * @resource_size: resource's size |
| 6015 | * @resource_id: resource's id |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 6016 | * @parent_resource_id: resource's parent id |
| 6017 | * @size_params: size parameters |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6018 | */ |
| 6019 | int devlink_resource_register(struct devlink *devlink, |
| 6020 | const char *resource_name, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6021 | u64 resource_size, |
| 6022 | u64 resource_id, |
| 6023 | u64 parent_resource_id, |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 6024 | const struct devlink_resource_size_params *size_params) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6025 | { |
| 6026 | struct devlink_resource *resource; |
| 6027 | struct list_head *resource_list; |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 6028 | bool top_hierarchy; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6029 | int err = 0; |
| 6030 | |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 6031 | top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP; |
| 6032 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6033 | mutex_lock(&devlink->lock); |
| 6034 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 6035 | if (resource) { |
| 6036 | err = -EINVAL; |
| 6037 | goto out; |
| 6038 | } |
| 6039 | |
| 6040 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
| 6041 | if (!resource) { |
| 6042 | err = -ENOMEM; |
| 6043 | goto out; |
| 6044 | } |
| 6045 | |
| 6046 | if (top_hierarchy) { |
| 6047 | resource_list = &devlink->resource_list; |
| 6048 | } else { |
| 6049 | struct devlink_resource *parent_resource; |
| 6050 | |
| 6051 | parent_resource = devlink_resource_find(devlink, NULL, |
| 6052 | parent_resource_id); |
| 6053 | if (parent_resource) { |
| 6054 | resource_list = &parent_resource->resource_list; |
| 6055 | resource->parent = parent_resource; |
| 6056 | } else { |
Colin Ian King | b75703d | 2018-01-22 10:31:19 +0000 | [diff] [blame] | 6057 | kfree(resource); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6058 | err = -EINVAL; |
| 6059 | goto out; |
| 6060 | } |
| 6061 | } |
| 6062 | |
| 6063 | resource->name = resource_name; |
| 6064 | resource->size = resource_size; |
| 6065 | resource->size_new = resource_size; |
| 6066 | resource->id = resource_id; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6067 | resource->size_valid = true; |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 6068 | memcpy(&resource->size_params, size_params, |
| 6069 | sizeof(resource->size_params)); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6070 | INIT_LIST_HEAD(&resource->resource_list); |
| 6071 | list_add_tail(&resource->list, resource_list); |
| 6072 | out: |
| 6073 | mutex_unlock(&devlink->lock); |
| 6074 | return err; |
| 6075 | } |
| 6076 | EXPORT_SYMBOL_GPL(devlink_resource_register); |
| 6077 | |
| 6078 | /** |
| 6079 | * devlink_resources_unregister - free all resources |
| 6080 | * |
| 6081 | * @devlink: devlink |
| 6082 | * @resource: resource |
| 6083 | */ |
| 6084 | void devlink_resources_unregister(struct devlink *devlink, |
| 6085 | struct devlink_resource *resource) |
| 6086 | { |
| 6087 | struct devlink_resource *tmp, *child_resource; |
| 6088 | struct list_head *resource_list; |
| 6089 | |
| 6090 | if (resource) |
| 6091 | resource_list = &resource->resource_list; |
| 6092 | else |
| 6093 | resource_list = &devlink->resource_list; |
| 6094 | |
| 6095 | if (!resource) |
| 6096 | mutex_lock(&devlink->lock); |
| 6097 | |
| 6098 | list_for_each_entry_safe(child_resource, tmp, resource_list, list) { |
| 6099 | devlink_resources_unregister(devlink, child_resource); |
| 6100 | list_del(&child_resource->list); |
| 6101 | kfree(child_resource); |
| 6102 | } |
| 6103 | |
| 6104 | if (!resource) |
| 6105 | mutex_unlock(&devlink->lock); |
| 6106 | } |
| 6107 | EXPORT_SYMBOL_GPL(devlink_resources_unregister); |
| 6108 | |
| 6109 | /** |
| 6110 | * devlink_resource_size_get - get and update size |
| 6111 | * |
| 6112 | * @devlink: devlink |
| 6113 | * @resource_id: the requested resource id |
| 6114 | * @p_resource_size: ptr to update |
| 6115 | */ |
| 6116 | int devlink_resource_size_get(struct devlink *devlink, |
| 6117 | u64 resource_id, |
| 6118 | u64 *p_resource_size) |
| 6119 | { |
| 6120 | struct devlink_resource *resource; |
| 6121 | int err = 0; |
| 6122 | |
| 6123 | mutex_lock(&devlink->lock); |
| 6124 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 6125 | if (!resource) { |
| 6126 | err = -EINVAL; |
| 6127 | goto out; |
| 6128 | } |
| 6129 | *p_resource_size = resource->size_new; |
| 6130 | resource->size = resource->size_new; |
| 6131 | out: |
| 6132 | mutex_unlock(&devlink->lock); |
| 6133 | return err; |
| 6134 | } |
| 6135 | EXPORT_SYMBOL_GPL(devlink_resource_size_get); |
| 6136 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 6137 | /** |
| 6138 | * devlink_dpipe_table_resource_set - set the resource id |
| 6139 | * |
| 6140 | * @devlink: devlink |
| 6141 | * @table_name: table name |
| 6142 | * @resource_id: resource id |
| 6143 | * @resource_units: number of resource's units consumed per table's entry |
| 6144 | */ |
| 6145 | int devlink_dpipe_table_resource_set(struct devlink *devlink, |
| 6146 | const char *table_name, u64 resource_id, |
| 6147 | u64 resource_units) |
| 6148 | { |
| 6149 | struct devlink_dpipe_table *table; |
| 6150 | int err = 0; |
| 6151 | |
| 6152 | mutex_lock(&devlink->lock); |
| 6153 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 6154 | table_name); |
| 6155 | if (!table) { |
| 6156 | err = -EINVAL; |
| 6157 | goto out; |
| 6158 | } |
| 6159 | table->resource_id = resource_id; |
| 6160 | table->resource_units = resource_units; |
| 6161 | table->resource_valid = true; |
| 6162 | out: |
| 6163 | mutex_unlock(&devlink->lock); |
| 6164 | return err; |
| 6165 | } |
| 6166 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set); |
| 6167 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 6168 | /** |
| 6169 | * devlink_resource_occ_get_register - register occupancy getter |
| 6170 | * |
| 6171 | * @devlink: devlink |
| 6172 | * @resource_id: resource id |
| 6173 | * @occ_get: occupancy getter callback |
| 6174 | * @occ_get_priv: occupancy getter callback priv |
| 6175 | */ |
| 6176 | void devlink_resource_occ_get_register(struct devlink *devlink, |
| 6177 | u64 resource_id, |
| 6178 | devlink_resource_occ_get_t *occ_get, |
| 6179 | void *occ_get_priv) |
| 6180 | { |
| 6181 | struct devlink_resource *resource; |
| 6182 | |
| 6183 | mutex_lock(&devlink->lock); |
| 6184 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 6185 | if (WARN_ON(!resource)) |
| 6186 | goto out; |
| 6187 | WARN_ON(resource->occ_get); |
| 6188 | |
| 6189 | resource->occ_get = occ_get; |
| 6190 | resource->occ_get_priv = occ_get_priv; |
| 6191 | out: |
| 6192 | mutex_unlock(&devlink->lock); |
| 6193 | } |
| 6194 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register); |
| 6195 | |
| 6196 | /** |
| 6197 | * devlink_resource_occ_get_unregister - unregister occupancy getter |
| 6198 | * |
| 6199 | * @devlink: devlink |
| 6200 | * @resource_id: resource id |
| 6201 | */ |
| 6202 | void devlink_resource_occ_get_unregister(struct devlink *devlink, |
| 6203 | u64 resource_id) |
| 6204 | { |
| 6205 | struct devlink_resource *resource; |
| 6206 | |
| 6207 | mutex_lock(&devlink->lock); |
| 6208 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 6209 | if (WARN_ON(!resource)) |
| 6210 | goto out; |
| 6211 | WARN_ON(!resource->occ_get); |
| 6212 | |
| 6213 | resource->occ_get = NULL; |
| 6214 | resource->occ_get_priv = NULL; |
| 6215 | out: |
| 6216 | mutex_unlock(&devlink->lock); |
| 6217 | } |
| 6218 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister); |
| 6219 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6220 | static int devlink_param_verify(const struct devlink_param *param) |
| 6221 | { |
| 6222 | if (!param || !param->name || !param->supported_cmodes) |
| 6223 | return -EINVAL; |
| 6224 | if (param->generic) |
| 6225 | return devlink_param_generic_verify(param); |
| 6226 | else |
| 6227 | return devlink_param_driver_verify(param); |
| 6228 | } |
| 6229 | |
| 6230 | static int __devlink_params_register(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6231 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6232 | struct list_head *param_list, |
| 6233 | const struct devlink_param *params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6234 | size_t params_count, |
| 6235 | enum devlink_command reg_cmd, |
| 6236 | enum devlink_command unreg_cmd) |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6237 | { |
| 6238 | const struct devlink_param *param = params; |
| 6239 | int i; |
| 6240 | int err; |
| 6241 | |
| 6242 | mutex_lock(&devlink->lock); |
| 6243 | for (i = 0; i < params_count; i++, param++) { |
| 6244 | err = devlink_param_verify(param); |
| 6245 | if (err) |
| 6246 | goto rollback; |
| 6247 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6248 | err = devlink_param_register_one(devlink, port_index, |
| 6249 | param_list, param, reg_cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6250 | if (err) |
| 6251 | goto rollback; |
| 6252 | } |
| 6253 | |
| 6254 | mutex_unlock(&devlink->lock); |
| 6255 | return 0; |
| 6256 | |
| 6257 | rollback: |
| 6258 | if (!i) |
| 6259 | goto unlock; |
| 6260 | for (param--; i > 0; i--, param--) |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6261 | devlink_param_unregister_one(devlink, port_index, param_list, |
| 6262 | param, unreg_cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6263 | unlock: |
| 6264 | mutex_unlock(&devlink->lock); |
| 6265 | return err; |
| 6266 | } |
| 6267 | |
| 6268 | static void __devlink_params_unregister(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6269 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6270 | struct list_head *param_list, |
| 6271 | const struct devlink_param *params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6272 | size_t params_count, |
| 6273 | enum devlink_command cmd) |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6274 | { |
| 6275 | const struct devlink_param *param = params; |
| 6276 | int i; |
| 6277 | |
| 6278 | mutex_lock(&devlink->lock); |
| 6279 | for (i = 0; i < params_count; i++, param++) |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6280 | devlink_param_unregister_one(devlink, 0, param_list, param, |
| 6281 | cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6282 | mutex_unlock(&devlink->lock); |
| 6283 | } |
| 6284 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 6285 | /** |
| 6286 | * devlink_params_register - register configuration parameters |
| 6287 | * |
| 6288 | * @devlink: devlink |
| 6289 | * @params: configuration parameters array |
| 6290 | * @params_count: number of parameters provided |
| 6291 | * |
| 6292 | * Register the configuration parameters supported by the driver. |
| 6293 | */ |
| 6294 | int devlink_params_register(struct devlink *devlink, |
| 6295 | const struct devlink_param *params, |
| 6296 | size_t params_count) |
| 6297 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6298 | return __devlink_params_register(devlink, 0, &devlink->param_list, |
| 6299 | params, params_count, |
| 6300 | DEVLINK_CMD_PARAM_NEW, |
| 6301 | DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 6302 | } |
| 6303 | EXPORT_SYMBOL_GPL(devlink_params_register); |
| 6304 | |
| 6305 | /** |
| 6306 | * devlink_params_unregister - unregister configuration parameters |
| 6307 | * @devlink: devlink |
| 6308 | * @params: configuration parameters to unregister |
| 6309 | * @params_count: number of parameters provided |
| 6310 | */ |
| 6311 | void devlink_params_unregister(struct devlink *devlink, |
| 6312 | const struct devlink_param *params, |
| 6313 | size_t params_count) |
| 6314 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6315 | return __devlink_params_unregister(devlink, 0, &devlink->param_list, |
| 6316 | params, params_count, |
| 6317 | DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 6318 | } |
| 6319 | EXPORT_SYMBOL_GPL(devlink_params_unregister); |
| 6320 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 6321 | /** |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 6322 | * devlink_params_publish - publish configuration parameters |
| 6323 | * |
| 6324 | * @devlink: devlink |
| 6325 | * |
| 6326 | * Publish previously registered configuration parameters. |
| 6327 | */ |
| 6328 | void devlink_params_publish(struct devlink *devlink) |
| 6329 | { |
| 6330 | struct devlink_param_item *param_item; |
| 6331 | |
| 6332 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 6333 | if (param_item->published) |
| 6334 | continue; |
| 6335 | param_item->published = true; |
| 6336 | devlink_param_notify(devlink, 0, param_item, |
| 6337 | DEVLINK_CMD_PARAM_NEW); |
| 6338 | } |
| 6339 | } |
| 6340 | EXPORT_SYMBOL_GPL(devlink_params_publish); |
| 6341 | |
| 6342 | /** |
| 6343 | * devlink_params_unpublish - unpublish configuration parameters |
| 6344 | * |
| 6345 | * @devlink: devlink |
| 6346 | * |
| 6347 | * Unpublish previously registered configuration parameters. |
| 6348 | */ |
| 6349 | void devlink_params_unpublish(struct devlink *devlink) |
| 6350 | { |
| 6351 | struct devlink_param_item *param_item; |
| 6352 | |
| 6353 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 6354 | if (!param_item->published) |
| 6355 | continue; |
| 6356 | param_item->published = false; |
| 6357 | devlink_param_notify(devlink, 0, param_item, |
| 6358 | DEVLINK_CMD_PARAM_DEL); |
| 6359 | } |
| 6360 | } |
| 6361 | EXPORT_SYMBOL_GPL(devlink_params_unpublish); |
| 6362 | |
| 6363 | /** |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6364 | * devlink_port_params_register - register port configuration parameters |
| 6365 | * |
| 6366 | * @devlink_port: devlink port |
| 6367 | * @params: configuration parameters array |
| 6368 | * @params_count: number of parameters provided |
| 6369 | * |
| 6370 | * Register the configuration parameters supported by the port. |
| 6371 | */ |
| 6372 | int devlink_port_params_register(struct devlink_port *devlink_port, |
| 6373 | const struct devlink_param *params, |
| 6374 | size_t params_count) |
| 6375 | { |
| 6376 | return __devlink_params_register(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6377 | devlink_port->index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6378 | &devlink_port->param_list, params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6379 | params_count, |
| 6380 | DEVLINK_CMD_PORT_PARAM_NEW, |
| 6381 | DEVLINK_CMD_PORT_PARAM_DEL); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6382 | } |
| 6383 | EXPORT_SYMBOL_GPL(devlink_port_params_register); |
| 6384 | |
| 6385 | /** |
| 6386 | * devlink_port_params_unregister - unregister port configuration |
| 6387 | * parameters |
| 6388 | * |
| 6389 | * @devlink_port: devlink port |
| 6390 | * @params: configuration parameters array |
| 6391 | * @params_count: number of parameters provided |
| 6392 | */ |
| 6393 | void devlink_port_params_unregister(struct devlink_port *devlink_port, |
| 6394 | const struct devlink_param *params, |
| 6395 | size_t params_count) |
| 6396 | { |
| 6397 | return __devlink_params_unregister(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6398 | devlink_port->index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6399 | &devlink_port->param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6400 | params, params_count, |
| 6401 | DEVLINK_CMD_PORT_PARAM_DEL); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6402 | } |
| 6403 | EXPORT_SYMBOL_GPL(devlink_port_params_unregister); |
| 6404 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 6405 | static int |
| 6406 | __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id, |
| 6407 | union devlink_param_value *init_val) |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 6408 | { |
| 6409 | struct devlink_param_item *param_item; |
| 6410 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 6411 | param_item = devlink_param_find_by_id(param_list, param_id); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 6412 | if (!param_item) |
| 6413 | return -EINVAL; |
| 6414 | |
| 6415 | if (!param_item->driverinit_value_valid || |
| 6416 | !devlink_param_cmode_is_supported(param_item->param, |
| 6417 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 6418 | return -EOPNOTSUPP; |
| 6419 | |
Moshe Shemesh | 1276534 | 2018-10-10 16:09:26 +0300 | [diff] [blame] | 6420 | if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING) |
| 6421 | strcpy(init_val->vstr, param_item->driverinit_value.vstr); |
| 6422 | else |
| 6423 | *init_val = param_item->driverinit_value; |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 6424 | |
| 6425 | return 0; |
| 6426 | } |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 6427 | |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 6428 | static int |
| 6429 | __devlink_param_driverinit_value_set(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6430 | unsigned int port_index, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 6431 | struct list_head *param_list, u32 param_id, |
| 6432 | union devlink_param_value init_val, |
| 6433 | enum devlink_command cmd) |
| 6434 | { |
| 6435 | struct devlink_param_item *param_item; |
| 6436 | |
| 6437 | param_item = devlink_param_find_by_id(param_list, param_id); |
| 6438 | if (!param_item) |
| 6439 | return -EINVAL; |
| 6440 | |
| 6441 | if (!devlink_param_cmode_is_supported(param_item->param, |
| 6442 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 6443 | return -EOPNOTSUPP; |
| 6444 | |
| 6445 | if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING) |
| 6446 | strcpy(param_item->driverinit_value.vstr, init_val.vstr); |
| 6447 | else |
| 6448 | param_item->driverinit_value = init_val; |
| 6449 | param_item->driverinit_value_valid = true; |
| 6450 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6451 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 6452 | return 0; |
| 6453 | } |
| 6454 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 6455 | /** |
| 6456 | * devlink_param_driverinit_value_get - get configuration parameter |
| 6457 | * value for driver initializing |
| 6458 | * |
| 6459 | * @devlink: devlink |
| 6460 | * @param_id: parameter ID |
| 6461 | * @init_val: value of parameter in driverinit configuration mode |
| 6462 | * |
| 6463 | * This function should be used by the driver to get driverinit |
| 6464 | * configuration for initialization after reload command. |
| 6465 | */ |
| 6466 | int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id, |
| 6467 | union devlink_param_value *init_val) |
| 6468 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 6469 | if (!devlink->ops->reload) |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 6470 | return -EOPNOTSUPP; |
| 6471 | |
| 6472 | return __devlink_param_driverinit_value_get(&devlink->param_list, |
| 6473 | param_id, init_val); |
| 6474 | } |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 6475 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get); |
| 6476 | |
| 6477 | /** |
| 6478 | * devlink_param_driverinit_value_set - set value of configuration |
| 6479 | * parameter for driverinit |
| 6480 | * configuration mode |
| 6481 | * |
| 6482 | * @devlink: devlink |
| 6483 | * @param_id: parameter ID |
| 6484 | * @init_val: value of parameter to set for driverinit configuration mode |
| 6485 | * |
| 6486 | * This function should be used by the driver to set driverinit |
| 6487 | * configuration mode default value. |
| 6488 | */ |
| 6489 | int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id, |
| 6490 | union devlink_param_value init_val) |
| 6491 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6492 | return __devlink_param_driverinit_value_set(devlink, 0, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 6493 | &devlink->param_list, |
| 6494 | param_id, init_val, |
| 6495 | DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 6496 | } |
| 6497 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set); |
| 6498 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 6499 | /** |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 6500 | * devlink_port_param_driverinit_value_get - get configuration parameter |
| 6501 | * value for driver initializing |
| 6502 | * |
| 6503 | * @devlink_port: devlink_port |
| 6504 | * @param_id: parameter ID |
| 6505 | * @init_val: value of parameter in driverinit configuration mode |
| 6506 | * |
| 6507 | * This function should be used by the driver to get driverinit |
| 6508 | * configuration for initialization after reload command. |
| 6509 | */ |
| 6510 | int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port, |
| 6511 | u32 param_id, |
| 6512 | union devlink_param_value *init_val) |
| 6513 | { |
| 6514 | struct devlink *devlink = devlink_port->devlink; |
| 6515 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 6516 | if (!devlink->ops->reload) |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 6517 | return -EOPNOTSUPP; |
| 6518 | |
| 6519 | return __devlink_param_driverinit_value_get(&devlink_port->param_list, |
| 6520 | param_id, init_val); |
| 6521 | } |
| 6522 | EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get); |
| 6523 | |
| 6524 | /** |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 6525 | * devlink_port_param_driverinit_value_set - set value of configuration |
| 6526 | * parameter for driverinit |
| 6527 | * configuration mode |
| 6528 | * |
| 6529 | * @devlink_port: devlink_port |
| 6530 | * @param_id: parameter ID |
| 6531 | * @init_val: value of parameter to set for driverinit configuration mode |
| 6532 | * |
| 6533 | * This function should be used by the driver to set driverinit |
| 6534 | * configuration mode default value. |
| 6535 | */ |
| 6536 | int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port, |
| 6537 | u32 param_id, |
| 6538 | union devlink_param_value init_val) |
| 6539 | { |
| 6540 | return __devlink_param_driverinit_value_set(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6541 | devlink_port->index, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 6542 | &devlink_port->param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6543 | param_id, init_val, |
| 6544 | DEVLINK_CMD_PORT_PARAM_NEW); |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 6545 | } |
| 6546 | EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set); |
| 6547 | |
| 6548 | /** |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 6549 | * devlink_param_value_changed - notify devlink on a parameter's value |
| 6550 | * change. Should be called by the driver |
| 6551 | * right after the change. |
| 6552 | * |
| 6553 | * @devlink: devlink |
| 6554 | * @param_id: parameter ID |
| 6555 | * |
| 6556 | * This function should be used by the driver to notify devlink on value |
| 6557 | * change, excluding driverinit configuration mode. |
| 6558 | * For driverinit configuration mode driver should use the function |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 6559 | */ |
| 6560 | void devlink_param_value_changed(struct devlink *devlink, u32 param_id) |
| 6561 | { |
| 6562 | struct devlink_param_item *param_item; |
| 6563 | |
| 6564 | param_item = devlink_param_find_by_id(&devlink->param_list, param_id); |
| 6565 | WARN_ON(!param_item); |
| 6566 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6567 | devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 6568 | } |
| 6569 | EXPORT_SYMBOL_GPL(devlink_param_value_changed); |
| 6570 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 6571 | /** |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 6572 | * devlink_port_param_value_changed - notify devlink on a parameter's value |
| 6573 | * change. Should be called by the driver |
| 6574 | * right after the change. |
| 6575 | * |
| 6576 | * @devlink_port: devlink_port |
| 6577 | * @param_id: parameter ID |
| 6578 | * |
| 6579 | * This function should be used by the driver to notify devlink on value |
| 6580 | * change, excluding driverinit configuration mode. |
| 6581 | * For driverinit configuration mode driver should use the function |
| 6582 | * devlink_port_param_driverinit_value_set() instead. |
| 6583 | */ |
| 6584 | void devlink_port_param_value_changed(struct devlink_port *devlink_port, |
| 6585 | u32 param_id) |
| 6586 | { |
| 6587 | struct devlink_param_item *param_item; |
| 6588 | |
| 6589 | param_item = devlink_param_find_by_id(&devlink_port->param_list, |
| 6590 | param_id); |
| 6591 | WARN_ON(!param_item); |
| 6592 | |
| 6593 | devlink_param_notify(devlink_port->devlink, devlink_port->index, |
| 6594 | param_item, DEVLINK_CMD_PORT_PARAM_NEW); |
| 6595 | } |
| 6596 | EXPORT_SYMBOL_GPL(devlink_port_param_value_changed); |
| 6597 | |
| 6598 | /** |
Moshe Shemesh | bde74ad1 | 2018-10-10 16:09:27 +0300 | [diff] [blame] | 6599 | * devlink_param_value_str_fill - Safely fill-up the string preventing |
| 6600 | * from overflow of the preallocated buffer |
| 6601 | * |
| 6602 | * @dst_val: destination devlink_param_value |
| 6603 | * @src: source buffer |
| 6604 | */ |
| 6605 | void devlink_param_value_str_fill(union devlink_param_value *dst_val, |
| 6606 | const char *src) |
| 6607 | { |
| 6608 | size_t len; |
| 6609 | |
| 6610 | len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE); |
| 6611 | WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE); |
| 6612 | } |
| 6613 | EXPORT_SYMBOL_GPL(devlink_param_value_str_fill); |
| 6614 | |
| 6615 | /** |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 6616 | * devlink_region_create - create a new address region |
| 6617 | * |
| 6618 | * @devlink: devlink |
| 6619 | * @region_name: region name |
| 6620 | * @region_max_snapshots: Maximum supported number of snapshots for region |
| 6621 | * @region_size: size of region |
| 6622 | */ |
| 6623 | struct devlink_region *devlink_region_create(struct devlink *devlink, |
| 6624 | const char *region_name, |
| 6625 | u32 region_max_snapshots, |
| 6626 | u64 region_size) |
| 6627 | { |
| 6628 | struct devlink_region *region; |
| 6629 | int err = 0; |
| 6630 | |
| 6631 | mutex_lock(&devlink->lock); |
| 6632 | |
| 6633 | if (devlink_region_get_by_name(devlink, region_name)) { |
| 6634 | err = -EEXIST; |
| 6635 | goto unlock; |
| 6636 | } |
| 6637 | |
| 6638 | region = kzalloc(sizeof(*region), GFP_KERNEL); |
| 6639 | if (!region) { |
| 6640 | err = -ENOMEM; |
| 6641 | goto unlock; |
| 6642 | } |
| 6643 | |
| 6644 | region->devlink = devlink; |
| 6645 | region->max_snapshots = region_max_snapshots; |
| 6646 | region->name = region_name; |
| 6647 | region->size = region_size; |
| 6648 | INIT_LIST_HEAD(®ion->snapshot_list); |
| 6649 | list_add_tail(®ion->list, &devlink->region_list); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6650 | devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 6651 | |
| 6652 | mutex_unlock(&devlink->lock); |
| 6653 | return region; |
| 6654 | |
| 6655 | unlock: |
| 6656 | mutex_unlock(&devlink->lock); |
| 6657 | return ERR_PTR(err); |
| 6658 | } |
| 6659 | EXPORT_SYMBOL_GPL(devlink_region_create); |
| 6660 | |
| 6661 | /** |
| 6662 | * devlink_region_destroy - destroy address region |
| 6663 | * |
| 6664 | * @region: devlink region to destroy |
| 6665 | */ |
| 6666 | void devlink_region_destroy(struct devlink_region *region) |
| 6667 | { |
| 6668 | struct devlink *devlink = region->devlink; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 6669 | struct devlink_snapshot *snapshot, *ts; |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 6670 | |
| 6671 | mutex_lock(&devlink->lock); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 6672 | |
| 6673 | /* Free all snapshots of region */ |
| 6674 | list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list) |
| 6675 | devlink_region_snapshot_del(snapshot); |
| 6676 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 6677 | list_del(®ion->list); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6678 | |
| 6679 | devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 6680 | mutex_unlock(&devlink->lock); |
| 6681 | kfree(region); |
| 6682 | } |
| 6683 | EXPORT_SYMBOL_GPL(devlink_region_destroy); |
| 6684 | |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 6685 | /** |
| 6686 | * devlink_region_shapshot_id_get - get snapshot ID |
| 6687 | * |
| 6688 | * This callback should be called when adding a new snapshot, |
| 6689 | * Driver should use the same id for multiple snapshots taken |
| 6690 | * on multiple regions at the same time/by the same trigger. |
| 6691 | * |
| 6692 | * @devlink: devlink |
| 6693 | */ |
| 6694 | u32 devlink_region_shapshot_id_get(struct devlink *devlink) |
| 6695 | { |
| 6696 | u32 id; |
| 6697 | |
| 6698 | mutex_lock(&devlink->lock); |
| 6699 | id = ++devlink->snapshot_id; |
| 6700 | mutex_unlock(&devlink->lock); |
| 6701 | |
| 6702 | return id; |
| 6703 | } |
| 6704 | EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get); |
| 6705 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 6706 | /** |
| 6707 | * devlink_region_snapshot_create - create a new snapshot |
| 6708 | * This will add a new snapshot of a region. The snapshot |
| 6709 | * will be stored on the region struct and can be accessed |
| 6710 | * from devlink. This is useful for future analyses of snapshots. |
| 6711 | * Multiple snapshots can be created on a region. |
| 6712 | * The @snapshot_id should be obtained using the getter function. |
| 6713 | * |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 6714 | * @region: devlink region of the snapshot |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 6715 | * @data_len: size of snapshot data |
| 6716 | * @data: snapshot data |
| 6717 | * @snapshot_id: snapshot id to be created |
| 6718 | * @data_destructor: pointer to destructor function to free data |
| 6719 | */ |
| 6720 | int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len, |
| 6721 | u8 *data, u32 snapshot_id, |
| 6722 | devlink_snapshot_data_dest_t *data_destructor) |
| 6723 | { |
| 6724 | struct devlink *devlink = region->devlink; |
| 6725 | struct devlink_snapshot *snapshot; |
| 6726 | int err; |
| 6727 | |
| 6728 | mutex_lock(&devlink->lock); |
| 6729 | |
| 6730 | /* check if region can hold one more snapshot */ |
| 6731 | if (region->cur_snapshots == region->max_snapshots) { |
| 6732 | err = -ENOMEM; |
| 6733 | goto unlock; |
| 6734 | } |
| 6735 | |
| 6736 | if (devlink_region_snapshot_get_by_id(region, snapshot_id)) { |
| 6737 | err = -EEXIST; |
| 6738 | goto unlock; |
| 6739 | } |
| 6740 | |
| 6741 | snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL); |
| 6742 | if (!snapshot) { |
| 6743 | err = -ENOMEM; |
| 6744 | goto unlock; |
| 6745 | } |
| 6746 | |
| 6747 | snapshot->id = snapshot_id; |
| 6748 | snapshot->region = region; |
| 6749 | snapshot->data = data; |
| 6750 | snapshot->data_len = data_len; |
| 6751 | snapshot->data_destructor = data_destructor; |
| 6752 | |
| 6753 | list_add_tail(&snapshot->list, ®ion->snapshot_list); |
| 6754 | |
| 6755 | region->cur_snapshots++; |
| 6756 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6757 | devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 6758 | mutex_unlock(&devlink->lock); |
| 6759 | return 0; |
| 6760 | |
| 6761 | unlock: |
| 6762 | mutex_unlock(&devlink->lock); |
| 6763 | return err; |
| 6764 | } |
| 6765 | EXPORT_SYMBOL_GPL(devlink_region_snapshot_create); |
| 6766 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 6767 | static void __devlink_compat_running_version(struct devlink *devlink, |
| 6768 | char *buf, size_t len) |
| 6769 | { |
| 6770 | const struct nlattr *nlattr; |
| 6771 | struct devlink_info_req req; |
| 6772 | struct sk_buff *msg; |
| 6773 | int rem, err; |
| 6774 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 6775 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 6776 | if (!msg) |
| 6777 | return; |
| 6778 | |
| 6779 | req.msg = msg; |
| 6780 | err = devlink->ops->info_get(devlink, &req, NULL); |
| 6781 | if (err) |
| 6782 | goto free_msg; |
| 6783 | |
| 6784 | nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) { |
| 6785 | const struct nlattr *kv; |
| 6786 | int rem_kv; |
| 6787 | |
| 6788 | if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING) |
| 6789 | continue; |
| 6790 | |
| 6791 | nla_for_each_nested(kv, nlattr, rem_kv) { |
| 6792 | if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE) |
| 6793 | continue; |
| 6794 | |
| 6795 | strlcat(buf, nla_data(kv), len); |
| 6796 | strlcat(buf, " ", len); |
| 6797 | } |
| 6798 | } |
| 6799 | free_msg: |
| 6800 | nlmsg_free(msg); |
| 6801 | } |
| 6802 | |
| 6803 | void devlink_compat_running_version(struct net_device *dev, |
| 6804 | char *buf, size_t len) |
| 6805 | { |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 6806 | struct devlink *devlink; |
| 6807 | |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 6808 | dev_hold(dev); |
| 6809 | rtnl_unlock(); |
| 6810 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 6811 | devlink = netdev_to_devlink(dev); |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 6812 | if (!devlink || !devlink->ops->info_get) |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 6813 | goto out; |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 6814 | |
| 6815 | mutex_lock(&devlink->lock); |
| 6816 | __devlink_compat_running_version(devlink, buf, len); |
| 6817 | mutex_unlock(&devlink->lock); |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 6818 | |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 6819 | out: |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 6820 | rtnl_lock(); |
| 6821 | dev_put(dev); |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 6822 | } |
| 6823 | |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 6824 | int devlink_compat_flash_update(struct net_device *dev, const char *file_name) |
| 6825 | { |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 6826 | struct devlink *devlink; |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 6827 | int ret; |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 6828 | |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 6829 | dev_hold(dev); |
| 6830 | rtnl_unlock(); |
| 6831 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 6832 | devlink = netdev_to_devlink(dev); |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 6833 | if (!devlink || !devlink->ops->flash_update) { |
| 6834 | ret = -EOPNOTSUPP; |
| 6835 | goto out; |
| 6836 | } |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 6837 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 6838 | mutex_lock(&devlink->lock); |
| 6839 | ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL); |
| 6840 | mutex_unlock(&devlink->lock); |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 6841 | |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 6842 | out: |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 6843 | rtnl_lock(); |
| 6844 | dev_put(dev); |
| 6845 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 6846 | return ret; |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 6847 | } |
| 6848 | |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 6849 | int devlink_compat_phys_port_name_get(struct net_device *dev, |
| 6850 | char *name, size_t len) |
| 6851 | { |
| 6852 | struct devlink_port *devlink_port; |
| 6853 | |
| 6854 | /* RTNL mutex is held here which ensures that devlink_port |
| 6855 | * instance cannot disappear in the middle. No need to take |
| 6856 | * any devlink lock as only permanent values are accessed. |
| 6857 | */ |
| 6858 | ASSERT_RTNL(); |
| 6859 | |
| 6860 | devlink_port = netdev_to_devlink_port(dev); |
| 6861 | if (!devlink_port) |
| 6862 | return -EOPNOTSUPP; |
| 6863 | |
| 6864 | return __devlink_port_phys_port_name_get(devlink_port, name, len); |
| 6865 | } |
| 6866 | |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 6867 | int devlink_compat_switch_id_get(struct net_device *dev, |
| 6868 | struct netdev_phys_item_id *ppid) |
| 6869 | { |
| 6870 | struct devlink_port *devlink_port; |
| 6871 | |
| 6872 | /* RTNL mutex is held here which ensures that devlink_port |
| 6873 | * instance cannot disappear in the middle. No need to take |
| 6874 | * any devlink lock as only permanent values are accessed. |
| 6875 | */ |
| 6876 | ASSERT_RTNL(); |
| 6877 | devlink_port = netdev_to_devlink_port(dev); |
| 6878 | if (!devlink_port || !devlink_port->attrs.switch_port) |
| 6879 | return -EOPNOTSUPP; |
| 6880 | |
| 6881 | memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid)); |
| 6882 | |
| 6883 | return 0; |
| 6884 | } |
| 6885 | |
Jakub Kicinski | f4b6bcc | 2019-02-25 19:34:02 -0800 | [diff] [blame] | 6886 | static int __init devlink_init(void) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6887 | { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 6888 | return genl_register_family(&devlink_nl_family); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6889 | } |
| 6890 | |
Jakub Kicinski | f4b6bcc | 2019-02-25 19:34:02 -0800 | [diff] [blame] | 6891 | subsys_initcall(devlink_init); |