Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2 | /* |
| 3 | * net/core/devlink.c - Network physical/parent device Netlink interface |
| 4 | * |
| 5 | * Heavily inspired by net/wireless/ |
| 6 | * Copyright (c) 2016 Mellanox Technologies. All rights reserved. |
| 7 | * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/gfp.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/netdevice.h> |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 19 | #include <linux/refcount.h> |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 20 | #include <linux/workqueue.h> |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 21 | #include <linux/u64_stats_sync.h> |
| 22 | #include <linux/timekeeping.h> |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 23 | #include <rdma/ib_verbs.h> |
| 24 | #include <net/netlink.h> |
| 25 | #include <net/genetlink.h> |
| 26 | #include <net/rtnetlink.h> |
| 27 | #include <net/net_namespace.h> |
| 28 | #include <net/sock.h> |
| 29 | #include <net/devlink.h> |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 30 | #include <net/drop_monitor.h> |
Jiri Pirko | e5224f0 | 2016-07-12 18:05:03 +0200 | [diff] [blame] | 31 | #define CREATE_TRACE_POINTS |
| 32 | #include <trace/events/devlink.h> |
| 33 | |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 34 | static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = { |
| 35 | { |
David Ahern | 12bdc5e | 2017-08-30 17:07:30 -0700 | [diff] [blame] | 36 | .name = "destination mac", |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 37 | .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC, |
| 38 | .bitwidth = 48, |
| 39 | }, |
| 40 | }; |
| 41 | |
| 42 | struct devlink_dpipe_header devlink_dpipe_header_ethernet = { |
| 43 | .name = "ethernet", |
| 44 | .id = DEVLINK_DPIPE_HEADER_ETHERNET, |
| 45 | .fields = devlink_dpipe_fields_ethernet, |
| 46 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet), |
| 47 | .global = true, |
| 48 | }; |
| 49 | EXPORT_SYMBOL(devlink_dpipe_header_ethernet); |
| 50 | |
Arkadi Sharshevsky | 3fb886e | 2017-08-24 08:40:00 +0200 | [diff] [blame] | 51 | static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = { |
| 52 | { |
| 53 | .name = "destination ip", |
| 54 | .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP, |
| 55 | .bitwidth = 32, |
| 56 | }, |
| 57 | }; |
| 58 | |
| 59 | struct devlink_dpipe_header devlink_dpipe_header_ipv4 = { |
| 60 | .name = "ipv4", |
| 61 | .id = DEVLINK_DPIPE_HEADER_IPV4, |
| 62 | .fields = devlink_dpipe_fields_ipv4, |
| 63 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4), |
| 64 | .global = true, |
| 65 | }; |
| 66 | EXPORT_SYMBOL(devlink_dpipe_header_ipv4); |
| 67 | |
Arkadi Sharshevsky | 1797f5b | 2017-08-31 17:59:12 +0200 | [diff] [blame] | 68 | static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = { |
| 69 | { |
| 70 | .name = "destination ip", |
| 71 | .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP, |
| 72 | .bitwidth = 128, |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | struct devlink_dpipe_header devlink_dpipe_header_ipv6 = { |
| 77 | .name = "ipv6", |
| 78 | .id = DEVLINK_DPIPE_HEADER_IPV6, |
| 79 | .fields = devlink_dpipe_fields_ipv6, |
| 80 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6), |
| 81 | .global = true, |
| 82 | }; |
| 83 | EXPORT_SYMBOL(devlink_dpipe_header_ipv6); |
| 84 | |
Jiri Pirko | e5224f0 | 2016-07-12 18:05:03 +0200 | [diff] [blame] | 85 | EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg); |
Nir Dotan | 57186a5 | 2019-02-04 18:47:45 +0000 | [diff] [blame] | 86 | EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 87 | |
| 88 | static LIST_HEAD(devlink_list); |
| 89 | |
| 90 | /* devlink_mutex |
| 91 | * |
| 92 | * An overall lock guarding every operation coming from userspace. |
| 93 | * It also guards devlink devices list and it is taken when |
| 94 | * driver registers/unregisters it. |
| 95 | */ |
| 96 | static DEFINE_MUTEX(devlink_mutex); |
| 97 | |
Jiri Pirko | 471f894 | 2019-10-03 11:49:31 +0200 | [diff] [blame] | 98 | struct net *devlink_net(const struct devlink *devlink) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 99 | { |
| 100 | return read_pnet(&devlink->_net); |
| 101 | } |
Jiri Pirko | 471f894 | 2019-10-03 11:49:31 +0200 | [diff] [blame] | 102 | EXPORT_SYMBOL_GPL(devlink_net); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 103 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 104 | static void __devlink_net_set(struct devlink *devlink, struct net *net) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 105 | { |
| 106 | write_pnet(&devlink->_net, net); |
| 107 | } |
| 108 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 109 | void devlink_net_set(struct devlink *devlink, struct net *net) |
| 110 | { |
| 111 | if (WARN_ON(devlink->registered)) |
| 112 | return; |
| 113 | __devlink_net_set(devlink, net); |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(devlink_net_set); |
| 116 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 117 | static struct devlink *devlink_get_from_attrs(struct net *net, |
| 118 | struct nlattr **attrs) |
| 119 | { |
| 120 | struct devlink *devlink; |
| 121 | char *busname; |
| 122 | char *devname; |
| 123 | |
| 124 | if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME]) |
| 125 | return ERR_PTR(-EINVAL); |
| 126 | |
| 127 | busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]); |
| 128 | devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]); |
| 129 | |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 130 | lockdep_assert_held(&devlink_mutex); |
| 131 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 132 | list_for_each_entry(devlink, &devlink_list, list) { |
| 133 | if (strcmp(devlink->dev->bus->name, busname) == 0 && |
| 134 | strcmp(dev_name(devlink->dev), devname) == 0 && |
| 135 | net_eq(devlink_net(devlink), net)) |
| 136 | return devlink; |
| 137 | } |
| 138 | |
| 139 | return ERR_PTR(-ENODEV); |
| 140 | } |
| 141 | |
| 142 | static struct devlink *devlink_get_from_info(struct genl_info *info) |
| 143 | { |
| 144 | return devlink_get_from_attrs(genl_info_net(info), info->attrs); |
| 145 | } |
| 146 | |
| 147 | static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, |
Parav Pandit | c7282b5 | 2019-08-30 05:39:44 -0500 | [diff] [blame] | 148 | unsigned int port_index) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 149 | { |
| 150 | struct devlink_port *devlink_port; |
| 151 | |
| 152 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 153 | if (devlink_port->index == port_index) |
| 154 | return devlink_port; |
| 155 | } |
| 156 | return NULL; |
| 157 | } |
| 158 | |
Parav Pandit | c7282b5 | 2019-08-30 05:39:44 -0500 | [diff] [blame] | 159 | static bool devlink_port_index_exists(struct devlink *devlink, |
| 160 | unsigned int port_index) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 161 | { |
| 162 | return devlink_port_get_by_index(devlink, port_index); |
| 163 | } |
| 164 | |
| 165 | static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, |
| 166 | struct nlattr **attrs) |
| 167 | { |
| 168 | if (attrs[DEVLINK_ATTR_PORT_INDEX]) { |
| 169 | u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 170 | struct devlink_port *devlink_port; |
| 171 | |
| 172 | devlink_port = devlink_port_get_by_index(devlink, port_index); |
| 173 | if (!devlink_port) |
| 174 | return ERR_PTR(-ENODEV); |
| 175 | return devlink_port; |
| 176 | } |
| 177 | return ERR_PTR(-EINVAL); |
| 178 | } |
| 179 | |
| 180 | static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink, |
| 181 | struct genl_info *info) |
| 182 | { |
| 183 | return devlink_port_get_from_attrs(devlink, info->attrs); |
| 184 | } |
| 185 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 186 | struct devlink_sb { |
| 187 | struct list_head list; |
| 188 | unsigned int index; |
| 189 | u32 size; |
| 190 | u16 ingress_pools_count; |
| 191 | u16 egress_pools_count; |
| 192 | u16 ingress_tc_count; |
| 193 | u16 egress_tc_count; |
| 194 | }; |
| 195 | |
| 196 | static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb) |
| 197 | { |
| 198 | return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count; |
| 199 | } |
| 200 | |
| 201 | static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink, |
| 202 | unsigned int sb_index) |
| 203 | { |
| 204 | struct devlink_sb *devlink_sb; |
| 205 | |
| 206 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 207 | if (devlink_sb->index == sb_index) |
| 208 | return devlink_sb; |
| 209 | } |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | static bool devlink_sb_index_exists(struct devlink *devlink, |
| 214 | unsigned int sb_index) |
| 215 | { |
| 216 | return devlink_sb_get_by_index(devlink, sb_index); |
| 217 | } |
| 218 | |
| 219 | static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink, |
| 220 | struct nlattr **attrs) |
| 221 | { |
| 222 | if (attrs[DEVLINK_ATTR_SB_INDEX]) { |
| 223 | u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]); |
| 224 | struct devlink_sb *devlink_sb; |
| 225 | |
| 226 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 227 | if (!devlink_sb) |
| 228 | return ERR_PTR(-ENODEV); |
| 229 | return devlink_sb; |
| 230 | } |
| 231 | return ERR_PTR(-EINVAL); |
| 232 | } |
| 233 | |
| 234 | static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink, |
| 235 | struct genl_info *info) |
| 236 | { |
| 237 | return devlink_sb_get_from_attrs(devlink, info->attrs); |
| 238 | } |
| 239 | |
| 240 | static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 241 | struct nlattr **attrs, |
| 242 | u16 *p_pool_index) |
| 243 | { |
| 244 | u16 val; |
| 245 | |
| 246 | if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX]) |
| 247 | return -EINVAL; |
| 248 | |
| 249 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]); |
| 250 | if (val >= devlink_sb_pool_count(devlink_sb)) |
| 251 | return -EINVAL; |
| 252 | *p_pool_index = val; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb, |
| 257 | struct genl_info *info, |
| 258 | u16 *p_pool_index) |
| 259 | { |
| 260 | return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs, |
| 261 | p_pool_index); |
| 262 | } |
| 263 | |
| 264 | static int |
| 265 | devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs, |
| 266 | enum devlink_sb_pool_type *p_pool_type) |
| 267 | { |
| 268 | u8 val; |
| 269 | |
| 270 | if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE]) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]); |
| 274 | if (val != DEVLINK_SB_POOL_TYPE_INGRESS && |
| 275 | val != DEVLINK_SB_POOL_TYPE_EGRESS) |
| 276 | return -EINVAL; |
| 277 | *p_pool_type = val; |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int |
| 282 | devlink_sb_pool_type_get_from_info(struct genl_info *info, |
| 283 | enum devlink_sb_pool_type *p_pool_type) |
| 284 | { |
| 285 | return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type); |
| 286 | } |
| 287 | |
| 288 | static int |
| 289 | devlink_sb_th_type_get_from_attrs(struct nlattr **attrs, |
| 290 | enum devlink_sb_threshold_type *p_th_type) |
| 291 | { |
| 292 | u8 val; |
| 293 | |
| 294 | if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]); |
| 298 | if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC && |
| 299 | val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC) |
| 300 | return -EINVAL; |
| 301 | *p_th_type = val; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int |
| 306 | devlink_sb_th_type_get_from_info(struct genl_info *info, |
| 307 | enum devlink_sb_threshold_type *p_th_type) |
| 308 | { |
| 309 | return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type); |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 314 | struct nlattr **attrs, |
| 315 | enum devlink_sb_pool_type pool_type, |
| 316 | u16 *p_tc_index) |
| 317 | { |
| 318 | u16 val; |
| 319 | |
| 320 | if (!attrs[DEVLINK_ATTR_SB_TC_INDEX]) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]); |
| 324 | if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS && |
| 325 | val >= devlink_sb->ingress_tc_count) |
| 326 | return -EINVAL; |
| 327 | if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS && |
| 328 | val >= devlink_sb->egress_tc_count) |
| 329 | return -EINVAL; |
| 330 | *p_tc_index = val; |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int |
| 335 | devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb, |
| 336 | struct genl_info *info, |
| 337 | enum devlink_sb_pool_type pool_type, |
| 338 | u16 *p_tc_index) |
| 339 | { |
| 340 | return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs, |
| 341 | pool_type, p_tc_index); |
| 342 | } |
| 343 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 344 | struct devlink_region { |
| 345 | struct devlink *devlink; |
| 346 | struct list_head list; |
| 347 | const char *name; |
| 348 | struct list_head snapshot_list; |
| 349 | u32 max_snapshots; |
| 350 | u32 cur_snapshots; |
| 351 | u64 size; |
| 352 | }; |
| 353 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 354 | struct devlink_snapshot { |
| 355 | struct list_head list; |
| 356 | struct devlink_region *region; |
| 357 | devlink_snapshot_data_dest_t *data_destructor; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 358 | u8 *data; |
| 359 | u32 id; |
| 360 | }; |
| 361 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 362 | static struct devlink_region * |
| 363 | devlink_region_get_by_name(struct devlink *devlink, const char *region_name) |
| 364 | { |
| 365 | struct devlink_region *region; |
| 366 | |
| 367 | list_for_each_entry(region, &devlink->region_list, list) |
| 368 | if (!strcmp(region->name, region_name)) |
| 369 | return region; |
| 370 | |
| 371 | return NULL; |
| 372 | } |
| 373 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 374 | static struct devlink_snapshot * |
| 375 | devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id) |
| 376 | { |
| 377 | struct devlink_snapshot *snapshot; |
| 378 | |
| 379 | list_for_each_entry(snapshot, ®ion->snapshot_list, list) |
| 380 | if (snapshot->id == id) |
| 381 | return snapshot; |
| 382 | |
| 383 | return NULL; |
| 384 | } |
| 385 | |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 386 | #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0) |
| 387 | #define DEVLINK_NL_FLAG_NEED_PORT BIT(1) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 388 | #define DEVLINK_NL_FLAG_NEED_SB BIT(2) |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 389 | |
| 390 | /* The per devlink instance lock is taken by default in the pre-doit |
| 391 | * operation, yet several commands do not require this. The global |
| 392 | * devlink lock is taken and protects from disruption by user-calls. |
| 393 | */ |
| 394 | #define DEVLINK_NL_FLAG_NO_LOCK BIT(3) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 395 | |
| 396 | static int devlink_nl_pre_doit(const struct genl_ops *ops, |
| 397 | struct sk_buff *skb, struct genl_info *info) |
| 398 | { |
| 399 | struct devlink *devlink; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 400 | int err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 401 | |
| 402 | mutex_lock(&devlink_mutex); |
| 403 | devlink = devlink_get_from_info(info); |
| 404 | if (IS_ERR(devlink)) { |
| 405 | mutex_unlock(&devlink_mutex); |
| 406 | return PTR_ERR(devlink); |
| 407 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 408 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 409 | mutex_lock(&devlink->lock); |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 410 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 411 | info->user_ptr[0] = devlink; |
| 412 | } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 413 | struct devlink_port *devlink_port; |
| 414 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 415 | devlink_port = devlink_port_get_from_info(devlink, info); |
| 416 | if (IS_ERR(devlink_port)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 417 | err = PTR_ERR(devlink_port); |
| 418 | goto unlock; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 419 | } |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 420 | info->user_ptr[0] = devlink_port; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 421 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 422 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) { |
| 423 | struct devlink_sb *devlink_sb; |
| 424 | |
| 425 | devlink_sb = devlink_sb_get_from_info(devlink, info); |
| 426 | if (IS_ERR(devlink_sb)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 427 | err = PTR_ERR(devlink_sb); |
| 428 | goto unlock; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 429 | } |
| 430 | info->user_ptr[1] = devlink_sb; |
| 431 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 432 | return 0; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 433 | |
| 434 | unlock: |
| 435 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 436 | mutex_unlock(&devlink->lock); |
| 437 | mutex_unlock(&devlink_mutex); |
| 438 | return err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static void devlink_nl_post_doit(const struct genl_ops *ops, |
| 442 | struct sk_buff *skb, struct genl_info *info) |
| 443 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 444 | struct devlink *devlink; |
| 445 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 446 | /* When devlink changes netns, it would not be found |
| 447 | * by devlink_get_from_info(). So try if it is stored first. |
| 448 | */ |
| 449 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 450 | devlink = info->user_ptr[0]; |
| 451 | } else { |
| 452 | devlink = devlink_get_from_info(info); |
| 453 | WARN_ON(IS_ERR(devlink)); |
| 454 | } |
| 455 | if (!IS_ERR(devlink) && ~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 456 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 457 | mutex_unlock(&devlink_mutex); |
| 458 | } |
| 459 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 460 | static struct genl_family devlink_nl_family; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 461 | |
| 462 | enum devlink_multicast_groups { |
| 463 | DEVLINK_MCGRP_CONFIG, |
| 464 | }; |
| 465 | |
| 466 | static const struct genl_multicast_group devlink_nl_mcgrps[] = { |
| 467 | [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, |
| 468 | }; |
| 469 | |
| 470 | static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) |
| 471 | { |
| 472 | if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name)) |
| 473 | return -EMSGSIZE; |
| 474 | if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev))) |
| 475 | return -EMSGSIZE; |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink, |
| 480 | enum devlink_command cmd, u32 portid, |
| 481 | u32 seq, int flags) |
| 482 | { |
| 483 | void *hdr; |
| 484 | |
| 485 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 486 | if (!hdr) |
| 487 | return -EMSGSIZE; |
| 488 | |
| 489 | if (devlink_nl_put_handle(msg, devlink)) |
| 490 | goto nla_put_failure; |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 491 | if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed)) |
| 492 | goto nla_put_failure; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 493 | |
| 494 | genlmsg_end(msg, hdr); |
| 495 | return 0; |
| 496 | |
| 497 | nla_put_failure: |
| 498 | genlmsg_cancel(msg, hdr); |
| 499 | return -EMSGSIZE; |
| 500 | } |
| 501 | |
| 502 | static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) |
| 503 | { |
| 504 | struct sk_buff *msg; |
| 505 | int err; |
| 506 | |
| 507 | WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); |
| 508 | |
| 509 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 510 | if (!msg) |
| 511 | return; |
| 512 | |
| 513 | err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0); |
| 514 | if (err) { |
| 515 | nlmsg_free(msg); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 520 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 521 | } |
| 522 | |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 523 | static int devlink_nl_port_attrs_put(struct sk_buff *msg, |
| 524 | struct devlink_port *devlink_port) |
| 525 | { |
| 526 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 527 | |
| 528 | if (!attrs->set) |
| 529 | return 0; |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 530 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour)) |
| 531 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 532 | switch (devlink_port->attrs.flavour) { |
| 533 | case DEVLINK_PORT_FLAVOUR_PCI_PF: |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 534 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, |
| 535 | attrs->pci_pf.pf)) |
| 536 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 537 | break; |
| 538 | case DEVLINK_PORT_FLAVOUR_PCI_VF: |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 539 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, |
| 540 | attrs->pci_vf.pf) || |
| 541 | nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, |
| 542 | attrs->pci_vf.vf)) |
| 543 | return -EMSGSIZE; |
Parav Pandit | 58b6be4 | 2019-08-30 05:39:45 -0500 | [diff] [blame] | 544 | break; |
| 545 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: |
| 546 | case DEVLINK_PORT_FLAVOUR_CPU: |
| 547 | case DEVLINK_PORT_FLAVOUR_DSA: |
| 548 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, |
| 549 | attrs->phys.port_number)) |
| 550 | return -EMSGSIZE; |
| 551 | if (!attrs->split) |
| 552 | return 0; |
| 553 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, |
| 554 | attrs->phys.port_number)) |
| 555 | return -EMSGSIZE; |
| 556 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER, |
| 557 | attrs->phys.split_subport_number)) |
| 558 | return -EMSGSIZE; |
| 559 | break; |
| 560 | default: |
| 561 | break; |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 562 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 566 | static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, |
| 567 | struct devlink_port *devlink_port, |
| 568 | enum devlink_command cmd, u32 portid, |
| 569 | u32 seq, int flags) |
| 570 | { |
| 571 | void *hdr; |
| 572 | |
| 573 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 574 | if (!hdr) |
| 575 | return -EMSGSIZE; |
| 576 | |
| 577 | if (devlink_nl_put_handle(msg, devlink)) |
| 578 | goto nla_put_failure; |
| 579 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 580 | goto nla_put_failure; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 581 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 582 | spin_lock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 583 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 584 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 585 | if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET && |
| 586 | nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE, |
| 587 | devlink_port->desired_type)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 588 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 589 | if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { |
| 590 | struct net_device *netdev = devlink_port->type_dev; |
| 591 | |
| 592 | if (netdev && |
| 593 | (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, |
| 594 | netdev->ifindex) || |
| 595 | nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, |
| 596 | netdev->name))) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 597 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 598 | } |
| 599 | if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { |
| 600 | struct ib_device *ibdev = devlink_port->type_dev; |
| 601 | |
| 602 | if (ibdev && |
| 603 | nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME, |
| 604 | ibdev->name)) |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 605 | goto nla_put_failure_type_locked; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 606 | } |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 607 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 608 | if (devlink_nl_port_attrs_put(msg, devlink_port)) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 609 | goto nla_put_failure; |
| 610 | |
| 611 | genlmsg_end(msg, hdr); |
| 612 | return 0; |
| 613 | |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 614 | nla_put_failure_type_locked: |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 615 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 616 | nla_put_failure: |
| 617 | genlmsg_cancel(msg, hdr); |
| 618 | return -EMSGSIZE; |
| 619 | } |
| 620 | |
| 621 | static void devlink_port_notify(struct devlink_port *devlink_port, |
| 622 | enum devlink_command cmd) |
| 623 | { |
| 624 | struct devlink *devlink = devlink_port->devlink; |
| 625 | struct sk_buff *msg; |
| 626 | int err; |
| 627 | |
| 628 | if (!devlink_port->registered) |
| 629 | return; |
| 630 | |
| 631 | WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); |
| 632 | |
| 633 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 634 | if (!msg) |
| 635 | return; |
| 636 | |
| 637 | err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0); |
| 638 | if (err) { |
| 639 | nlmsg_free(msg); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 644 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 645 | } |
| 646 | |
| 647 | static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info) |
| 648 | { |
| 649 | struct devlink *devlink = info->user_ptr[0]; |
| 650 | struct sk_buff *msg; |
| 651 | int err; |
| 652 | |
| 653 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 654 | if (!msg) |
| 655 | return -ENOMEM; |
| 656 | |
| 657 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 658 | info->snd_portid, info->snd_seq, 0); |
| 659 | if (err) { |
| 660 | nlmsg_free(msg); |
| 661 | return err; |
| 662 | } |
| 663 | |
| 664 | return genlmsg_reply(msg, info); |
| 665 | } |
| 666 | |
| 667 | static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg, |
| 668 | struct netlink_callback *cb) |
| 669 | { |
| 670 | struct devlink *devlink; |
| 671 | int start = cb->args[0]; |
| 672 | int idx = 0; |
| 673 | int err; |
| 674 | |
| 675 | mutex_lock(&devlink_mutex); |
| 676 | list_for_each_entry(devlink, &devlink_list, list) { |
| 677 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 678 | continue; |
| 679 | if (idx < start) { |
| 680 | idx++; |
| 681 | continue; |
| 682 | } |
| 683 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 684 | NETLINK_CB(cb->skb).portid, |
| 685 | cb->nlh->nlmsg_seq, NLM_F_MULTI); |
| 686 | if (err) |
| 687 | goto out; |
| 688 | idx++; |
| 689 | } |
| 690 | out: |
| 691 | mutex_unlock(&devlink_mutex); |
| 692 | |
| 693 | cb->args[0] = idx; |
| 694 | return msg->len; |
| 695 | } |
| 696 | |
| 697 | static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb, |
| 698 | struct genl_info *info) |
| 699 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 700 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 701 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 702 | struct sk_buff *msg; |
| 703 | int err; |
| 704 | |
| 705 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 706 | if (!msg) |
| 707 | return -ENOMEM; |
| 708 | |
| 709 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 710 | DEVLINK_CMD_PORT_NEW, |
| 711 | info->snd_portid, info->snd_seq, 0); |
| 712 | if (err) { |
| 713 | nlmsg_free(msg); |
| 714 | return err; |
| 715 | } |
| 716 | |
| 717 | return genlmsg_reply(msg, info); |
| 718 | } |
| 719 | |
| 720 | static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg, |
| 721 | struct netlink_callback *cb) |
| 722 | { |
| 723 | struct devlink *devlink; |
| 724 | struct devlink_port *devlink_port; |
| 725 | int start = cb->args[0]; |
| 726 | int idx = 0; |
| 727 | int err; |
| 728 | |
| 729 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 730 | list_for_each_entry(devlink, &devlink_list, list) { |
| 731 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 732 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 733 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 734 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 735 | if (idx < start) { |
| 736 | idx++; |
| 737 | continue; |
| 738 | } |
| 739 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 740 | DEVLINK_CMD_NEW, |
| 741 | NETLINK_CB(cb->skb).portid, |
| 742 | cb->nlh->nlmsg_seq, |
| 743 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 744 | if (err) { |
| 745 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 746 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 747 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 748 | idx++; |
| 749 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 750 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 751 | } |
| 752 | out: |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 753 | mutex_unlock(&devlink_mutex); |
| 754 | |
| 755 | cb->args[0] = idx; |
| 756 | return msg->len; |
| 757 | } |
| 758 | |
| 759 | static int devlink_port_type_set(struct devlink *devlink, |
| 760 | struct devlink_port *devlink_port, |
| 761 | enum devlink_port_type port_type) |
| 762 | |
| 763 | { |
| 764 | int err; |
| 765 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 766 | if (devlink->ops->port_type_set) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 767 | if (port_type == DEVLINK_PORT_TYPE_NOTSET) |
| 768 | return -EINVAL; |
Elad Raz | 6edf101 | 2016-10-23 17:43:05 +0200 | [diff] [blame] | 769 | if (port_type == devlink_port->type) |
| 770 | return 0; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 771 | err = devlink->ops->port_type_set(devlink_port, port_type); |
| 772 | if (err) |
| 773 | return err; |
| 774 | devlink_port->desired_type = port_type; |
| 775 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 776 | return 0; |
| 777 | } |
| 778 | return -EOPNOTSUPP; |
| 779 | } |
| 780 | |
| 781 | static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, |
| 782 | struct genl_info *info) |
| 783 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 784 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 785 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 786 | int err; |
| 787 | |
| 788 | if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) { |
| 789 | enum devlink_port_type port_type; |
| 790 | |
| 791 | port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]); |
| 792 | err = devlink_port_type_set(devlink, devlink_port, port_type); |
| 793 | if (err) |
| 794 | return err; |
| 795 | } |
| 796 | return 0; |
| 797 | } |
| 798 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 799 | static int devlink_port_split(struct devlink *devlink, u32 port_index, |
| 800 | u32 count, struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 801 | |
| 802 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 803 | if (devlink->ops->port_split) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 804 | return devlink->ops->port_split(devlink, port_index, count, |
| 805 | extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 806 | return -EOPNOTSUPP; |
| 807 | } |
| 808 | |
| 809 | static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, |
| 810 | struct genl_info *info) |
| 811 | { |
| 812 | struct devlink *devlink = info->user_ptr[0]; |
| 813 | u32 port_index; |
| 814 | u32 count; |
| 815 | |
| 816 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] || |
| 817 | !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]) |
| 818 | return -EINVAL; |
| 819 | |
| 820 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 821 | count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 822 | return devlink_port_split(devlink, port_index, count, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 823 | } |
| 824 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 825 | static int devlink_port_unsplit(struct devlink *devlink, u32 port_index, |
| 826 | struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 827 | |
| 828 | { |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 829 | if (devlink->ops->port_unsplit) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 830 | return devlink->ops->port_unsplit(devlink, port_index, extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 831 | return -EOPNOTSUPP; |
| 832 | } |
| 833 | |
| 834 | static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb, |
| 835 | struct genl_info *info) |
| 836 | { |
| 837 | struct devlink *devlink = info->user_ptr[0]; |
| 838 | u32 port_index; |
| 839 | |
| 840 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) |
| 841 | return -EINVAL; |
| 842 | |
| 843 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 844 | return devlink_port_unsplit(devlink, port_index, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 847 | static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink, |
| 848 | struct devlink_sb *devlink_sb, |
| 849 | enum devlink_command cmd, u32 portid, |
| 850 | u32 seq, int flags) |
| 851 | { |
| 852 | void *hdr; |
| 853 | |
| 854 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 855 | if (!hdr) |
| 856 | return -EMSGSIZE; |
| 857 | |
| 858 | if (devlink_nl_put_handle(msg, devlink)) |
| 859 | goto nla_put_failure; |
| 860 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 861 | goto nla_put_failure; |
| 862 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size)) |
| 863 | goto nla_put_failure; |
| 864 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT, |
| 865 | devlink_sb->ingress_pools_count)) |
| 866 | goto nla_put_failure; |
| 867 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT, |
| 868 | devlink_sb->egress_pools_count)) |
| 869 | goto nla_put_failure; |
| 870 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT, |
| 871 | devlink_sb->ingress_tc_count)) |
| 872 | goto nla_put_failure; |
| 873 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT, |
| 874 | devlink_sb->egress_tc_count)) |
| 875 | goto nla_put_failure; |
| 876 | |
| 877 | genlmsg_end(msg, hdr); |
| 878 | return 0; |
| 879 | |
| 880 | nla_put_failure: |
| 881 | genlmsg_cancel(msg, hdr); |
| 882 | return -EMSGSIZE; |
| 883 | } |
| 884 | |
| 885 | static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb, |
| 886 | struct genl_info *info) |
| 887 | { |
| 888 | struct devlink *devlink = info->user_ptr[0]; |
| 889 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 890 | struct sk_buff *msg; |
| 891 | int err; |
| 892 | |
| 893 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 894 | if (!msg) |
| 895 | return -ENOMEM; |
| 896 | |
| 897 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 898 | DEVLINK_CMD_SB_NEW, |
| 899 | info->snd_portid, info->snd_seq, 0); |
| 900 | if (err) { |
| 901 | nlmsg_free(msg); |
| 902 | return err; |
| 903 | } |
| 904 | |
| 905 | return genlmsg_reply(msg, info); |
| 906 | } |
| 907 | |
| 908 | static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg, |
| 909 | struct netlink_callback *cb) |
| 910 | { |
| 911 | struct devlink *devlink; |
| 912 | struct devlink_sb *devlink_sb; |
| 913 | int start = cb->args[0]; |
| 914 | int idx = 0; |
| 915 | int err; |
| 916 | |
| 917 | mutex_lock(&devlink_mutex); |
| 918 | list_for_each_entry(devlink, &devlink_list, list) { |
| 919 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 920 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 921 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 922 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 923 | if (idx < start) { |
| 924 | idx++; |
| 925 | continue; |
| 926 | } |
| 927 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 928 | DEVLINK_CMD_SB_NEW, |
| 929 | NETLINK_CB(cb->skb).portid, |
| 930 | cb->nlh->nlmsg_seq, |
| 931 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 932 | if (err) { |
| 933 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 934 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 935 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 936 | idx++; |
| 937 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 938 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 939 | } |
| 940 | out: |
| 941 | mutex_unlock(&devlink_mutex); |
| 942 | |
| 943 | cb->args[0] = idx; |
| 944 | return msg->len; |
| 945 | } |
| 946 | |
| 947 | static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink, |
| 948 | struct devlink_sb *devlink_sb, |
| 949 | u16 pool_index, enum devlink_command cmd, |
| 950 | u32 portid, u32 seq, int flags) |
| 951 | { |
| 952 | struct devlink_sb_pool_info pool_info; |
| 953 | void *hdr; |
| 954 | int err; |
| 955 | |
| 956 | err = devlink->ops->sb_pool_get(devlink, devlink_sb->index, |
| 957 | pool_index, &pool_info); |
| 958 | if (err) |
| 959 | return err; |
| 960 | |
| 961 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 962 | if (!hdr) |
| 963 | return -EMSGSIZE; |
| 964 | |
| 965 | if (devlink_nl_put_handle(msg, devlink)) |
| 966 | goto nla_put_failure; |
| 967 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 968 | goto nla_put_failure; |
| 969 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 970 | goto nla_put_failure; |
| 971 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type)) |
| 972 | goto nla_put_failure; |
| 973 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size)) |
| 974 | goto nla_put_failure; |
| 975 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE, |
| 976 | pool_info.threshold_type)) |
| 977 | goto nla_put_failure; |
Jakub Kicinski | bff5731 | 2019-02-01 17:56:28 -0800 | [diff] [blame] | 978 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE, |
| 979 | pool_info.cell_size)) |
| 980 | goto nla_put_failure; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 981 | |
| 982 | genlmsg_end(msg, hdr); |
| 983 | return 0; |
| 984 | |
| 985 | nla_put_failure: |
| 986 | genlmsg_cancel(msg, hdr); |
| 987 | return -EMSGSIZE; |
| 988 | } |
| 989 | |
| 990 | static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb, |
| 991 | struct genl_info *info) |
| 992 | { |
| 993 | struct devlink *devlink = info->user_ptr[0]; |
| 994 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 995 | struct sk_buff *msg; |
| 996 | u16 pool_index; |
| 997 | int err; |
| 998 | |
| 999 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1000 | &pool_index); |
| 1001 | if (err) |
| 1002 | return err; |
| 1003 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1004 | if (!devlink->ops->sb_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1005 | return -EOPNOTSUPP; |
| 1006 | |
| 1007 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1008 | if (!msg) |
| 1009 | return -ENOMEM; |
| 1010 | |
| 1011 | err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index, |
| 1012 | DEVLINK_CMD_SB_POOL_NEW, |
| 1013 | info->snd_portid, info->snd_seq, 0); |
| 1014 | if (err) { |
| 1015 | nlmsg_free(msg); |
| 1016 | return err; |
| 1017 | } |
| 1018 | |
| 1019 | return genlmsg_reply(msg, info); |
| 1020 | } |
| 1021 | |
| 1022 | static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1023 | struct devlink *devlink, |
| 1024 | struct devlink_sb *devlink_sb, |
| 1025 | u32 portid, u32 seq) |
| 1026 | { |
| 1027 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1028 | u16 pool_index; |
| 1029 | int err; |
| 1030 | |
| 1031 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1032 | if (*p_idx < start) { |
| 1033 | (*p_idx)++; |
| 1034 | continue; |
| 1035 | } |
| 1036 | err = devlink_nl_sb_pool_fill(msg, devlink, |
| 1037 | devlink_sb, |
| 1038 | pool_index, |
| 1039 | DEVLINK_CMD_SB_POOL_NEW, |
| 1040 | portid, seq, NLM_F_MULTI); |
| 1041 | if (err) |
| 1042 | return err; |
| 1043 | (*p_idx)++; |
| 1044 | } |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg, |
| 1049 | struct netlink_callback *cb) |
| 1050 | { |
| 1051 | struct devlink *devlink; |
| 1052 | struct devlink_sb *devlink_sb; |
| 1053 | int start = cb->args[0]; |
| 1054 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1055 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1056 | |
| 1057 | mutex_lock(&devlink_mutex); |
| 1058 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1059 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1060 | !devlink->ops->sb_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1061 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1062 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1063 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1064 | err = __sb_pool_get_dumpit(msg, start, &idx, devlink, |
| 1065 | devlink_sb, |
| 1066 | NETLINK_CB(cb->skb).portid, |
| 1067 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1068 | if (err && err != -EOPNOTSUPP) { |
| 1069 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1070 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1071 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1072 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1073 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1074 | } |
| 1075 | out: |
| 1076 | mutex_unlock(&devlink_mutex); |
| 1077 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1078 | if (err != -EMSGSIZE) |
| 1079 | return err; |
| 1080 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1081 | cb->args[0] = idx; |
| 1082 | return msg->len; |
| 1083 | } |
| 1084 | |
| 1085 | static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index, |
| 1086 | u16 pool_index, u32 size, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1087 | enum devlink_sb_threshold_type threshold_type, |
| 1088 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1089 | |
| 1090 | { |
| 1091 | const struct devlink_ops *ops = devlink->ops; |
| 1092 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1093 | if (ops->sb_pool_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1094 | return ops->sb_pool_set(devlink, sb_index, pool_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1095 | size, threshold_type, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1096 | return -EOPNOTSUPP; |
| 1097 | } |
| 1098 | |
| 1099 | static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb, |
| 1100 | struct genl_info *info) |
| 1101 | { |
| 1102 | struct devlink *devlink = info->user_ptr[0]; |
| 1103 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1104 | enum devlink_sb_threshold_type threshold_type; |
| 1105 | u16 pool_index; |
| 1106 | u32 size; |
| 1107 | int err; |
| 1108 | |
| 1109 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1110 | &pool_index); |
| 1111 | if (err) |
| 1112 | return err; |
| 1113 | |
| 1114 | err = devlink_sb_th_type_get_from_info(info, &threshold_type); |
| 1115 | if (err) |
| 1116 | return err; |
| 1117 | |
| 1118 | if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]) |
| 1119 | return -EINVAL; |
| 1120 | |
| 1121 | size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]); |
| 1122 | return devlink_sb_pool_set(devlink, devlink_sb->index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1123 | pool_index, size, threshold_type, |
| 1124 | info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg, |
| 1128 | struct devlink *devlink, |
| 1129 | struct devlink_port *devlink_port, |
| 1130 | struct devlink_sb *devlink_sb, |
| 1131 | u16 pool_index, |
| 1132 | enum devlink_command cmd, |
| 1133 | u32 portid, u32 seq, int flags) |
| 1134 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1135 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1136 | u32 threshold; |
| 1137 | void *hdr; |
| 1138 | int err; |
| 1139 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1140 | err = ops->sb_port_pool_get(devlink_port, devlink_sb->index, |
| 1141 | pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1142 | if (err) |
| 1143 | return err; |
| 1144 | |
| 1145 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1146 | if (!hdr) |
| 1147 | return -EMSGSIZE; |
| 1148 | |
| 1149 | if (devlink_nl_put_handle(msg, devlink)) |
| 1150 | goto nla_put_failure; |
| 1151 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1152 | goto nla_put_failure; |
| 1153 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1154 | goto nla_put_failure; |
| 1155 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1156 | goto nla_put_failure; |
| 1157 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1158 | goto nla_put_failure; |
| 1159 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1160 | if (ops->sb_occ_port_pool_get) { |
| 1161 | u32 cur; |
| 1162 | u32 max; |
| 1163 | |
| 1164 | err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index, |
| 1165 | pool_index, &cur, &max); |
| 1166 | if (err && err != -EOPNOTSUPP) |
| 1167 | return err; |
| 1168 | if (!err) { |
| 1169 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1170 | goto nla_put_failure; |
| 1171 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1172 | goto nla_put_failure; |
| 1173 | } |
| 1174 | } |
| 1175 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1176 | genlmsg_end(msg, hdr); |
| 1177 | return 0; |
| 1178 | |
| 1179 | nla_put_failure: |
| 1180 | genlmsg_cancel(msg, hdr); |
| 1181 | return -EMSGSIZE; |
| 1182 | } |
| 1183 | |
| 1184 | static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb, |
| 1185 | struct genl_info *info) |
| 1186 | { |
| 1187 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1188 | struct devlink *devlink = devlink_port->devlink; |
| 1189 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1190 | struct sk_buff *msg; |
| 1191 | u16 pool_index; |
| 1192 | int err; |
| 1193 | |
| 1194 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1195 | &pool_index); |
| 1196 | if (err) |
| 1197 | return err; |
| 1198 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1199 | if (!devlink->ops->sb_port_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1200 | return -EOPNOTSUPP; |
| 1201 | |
| 1202 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1203 | if (!msg) |
| 1204 | return -ENOMEM; |
| 1205 | |
| 1206 | err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port, |
| 1207 | devlink_sb, pool_index, |
| 1208 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1209 | info->snd_portid, info->snd_seq, 0); |
| 1210 | if (err) { |
| 1211 | nlmsg_free(msg); |
| 1212 | return err; |
| 1213 | } |
| 1214 | |
| 1215 | return genlmsg_reply(msg, info); |
| 1216 | } |
| 1217 | |
| 1218 | static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1219 | struct devlink *devlink, |
| 1220 | struct devlink_sb *devlink_sb, |
| 1221 | u32 portid, u32 seq) |
| 1222 | { |
| 1223 | struct devlink_port *devlink_port; |
| 1224 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1225 | u16 pool_index; |
| 1226 | int err; |
| 1227 | |
| 1228 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1229 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1230 | if (*p_idx < start) { |
| 1231 | (*p_idx)++; |
| 1232 | continue; |
| 1233 | } |
| 1234 | err = devlink_nl_sb_port_pool_fill(msg, devlink, |
| 1235 | devlink_port, |
| 1236 | devlink_sb, |
| 1237 | pool_index, |
| 1238 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1239 | portid, seq, |
| 1240 | NLM_F_MULTI); |
| 1241 | if (err) |
| 1242 | return err; |
| 1243 | (*p_idx)++; |
| 1244 | } |
| 1245 | } |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg, |
| 1250 | struct netlink_callback *cb) |
| 1251 | { |
| 1252 | struct devlink *devlink; |
| 1253 | struct devlink_sb *devlink_sb; |
| 1254 | int start = cb->args[0]; |
| 1255 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1256 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1257 | |
| 1258 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1259 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1260 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1261 | !devlink->ops->sb_port_pool_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1262 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1263 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1264 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1265 | err = __sb_port_pool_get_dumpit(msg, start, &idx, |
| 1266 | devlink, devlink_sb, |
| 1267 | NETLINK_CB(cb->skb).portid, |
| 1268 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1269 | if (err && err != -EOPNOTSUPP) { |
| 1270 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1271 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1272 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1273 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1274 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1275 | } |
| 1276 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1277 | mutex_unlock(&devlink_mutex); |
| 1278 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1279 | if (err != -EMSGSIZE) |
| 1280 | return err; |
| 1281 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1282 | cb->args[0] = idx; |
| 1283 | return msg->len; |
| 1284 | } |
| 1285 | |
| 1286 | static int devlink_sb_port_pool_set(struct devlink_port *devlink_port, |
| 1287 | unsigned int sb_index, u16 pool_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1288 | u32 threshold, |
| 1289 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1290 | |
| 1291 | { |
| 1292 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1293 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1294 | if (ops->sb_port_pool_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1295 | return ops->sb_port_pool_set(devlink_port, sb_index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1296 | pool_index, threshold, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1297 | return -EOPNOTSUPP; |
| 1298 | } |
| 1299 | |
| 1300 | static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb, |
| 1301 | struct genl_info *info) |
| 1302 | { |
| 1303 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1304 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1305 | u16 pool_index; |
| 1306 | u32 threshold; |
| 1307 | int err; |
| 1308 | |
| 1309 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1310 | &pool_index); |
| 1311 | if (err) |
| 1312 | return err; |
| 1313 | |
| 1314 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1315 | return -EINVAL; |
| 1316 | |
| 1317 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1318 | return devlink_sb_port_pool_set(devlink_port, devlink_sb->index, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1319 | pool_index, threshold, info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | static int |
| 1323 | devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1324 | struct devlink_port *devlink_port, |
| 1325 | struct devlink_sb *devlink_sb, u16 tc_index, |
| 1326 | enum devlink_sb_pool_type pool_type, |
| 1327 | enum devlink_command cmd, |
| 1328 | u32 portid, u32 seq, int flags) |
| 1329 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1330 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1331 | u16 pool_index; |
| 1332 | u32 threshold; |
| 1333 | void *hdr; |
| 1334 | int err; |
| 1335 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1336 | err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index, |
| 1337 | tc_index, pool_type, |
| 1338 | &pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1339 | if (err) |
| 1340 | return err; |
| 1341 | |
| 1342 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1343 | if (!hdr) |
| 1344 | return -EMSGSIZE; |
| 1345 | |
| 1346 | if (devlink_nl_put_handle(msg, devlink)) |
| 1347 | goto nla_put_failure; |
| 1348 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1349 | goto nla_put_failure; |
| 1350 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1351 | goto nla_put_failure; |
| 1352 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index)) |
| 1353 | goto nla_put_failure; |
| 1354 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type)) |
| 1355 | goto nla_put_failure; |
| 1356 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1357 | goto nla_put_failure; |
| 1358 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1359 | goto nla_put_failure; |
| 1360 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1361 | if (ops->sb_occ_tc_port_bind_get) { |
| 1362 | u32 cur; |
| 1363 | u32 max; |
| 1364 | |
| 1365 | err = ops->sb_occ_tc_port_bind_get(devlink_port, |
| 1366 | devlink_sb->index, |
| 1367 | tc_index, pool_type, |
| 1368 | &cur, &max); |
| 1369 | if (err && err != -EOPNOTSUPP) |
| 1370 | return err; |
| 1371 | if (!err) { |
| 1372 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1373 | goto nla_put_failure; |
| 1374 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1375 | goto nla_put_failure; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1379 | genlmsg_end(msg, hdr); |
| 1380 | return 0; |
| 1381 | |
| 1382 | nla_put_failure: |
| 1383 | genlmsg_cancel(msg, hdr); |
| 1384 | return -EMSGSIZE; |
| 1385 | } |
| 1386 | |
| 1387 | static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb, |
| 1388 | struct genl_info *info) |
| 1389 | { |
| 1390 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1391 | struct devlink *devlink = devlink_port->devlink; |
| 1392 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1393 | struct sk_buff *msg; |
| 1394 | enum devlink_sb_pool_type pool_type; |
| 1395 | u16 tc_index; |
| 1396 | int err; |
| 1397 | |
| 1398 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1399 | if (err) |
| 1400 | return err; |
| 1401 | |
| 1402 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1403 | pool_type, &tc_index); |
| 1404 | if (err) |
| 1405 | return err; |
| 1406 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1407 | if (!devlink->ops->sb_tc_pool_bind_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1408 | return -EOPNOTSUPP; |
| 1409 | |
| 1410 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1411 | if (!msg) |
| 1412 | return -ENOMEM; |
| 1413 | |
| 1414 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port, |
| 1415 | devlink_sb, tc_index, pool_type, |
| 1416 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1417 | info->snd_portid, |
| 1418 | info->snd_seq, 0); |
| 1419 | if (err) { |
| 1420 | nlmsg_free(msg); |
| 1421 | return err; |
| 1422 | } |
| 1423 | |
| 1424 | return genlmsg_reply(msg, info); |
| 1425 | } |
| 1426 | |
| 1427 | static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1428 | int start, int *p_idx, |
| 1429 | struct devlink *devlink, |
| 1430 | struct devlink_sb *devlink_sb, |
| 1431 | u32 portid, u32 seq) |
| 1432 | { |
| 1433 | struct devlink_port *devlink_port; |
| 1434 | u16 tc_index; |
| 1435 | int err; |
| 1436 | |
| 1437 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1438 | for (tc_index = 0; |
| 1439 | tc_index < devlink_sb->ingress_tc_count; tc_index++) { |
| 1440 | if (*p_idx < start) { |
| 1441 | (*p_idx)++; |
| 1442 | continue; |
| 1443 | } |
| 1444 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1445 | devlink_port, |
| 1446 | devlink_sb, |
| 1447 | tc_index, |
| 1448 | DEVLINK_SB_POOL_TYPE_INGRESS, |
| 1449 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1450 | portid, seq, |
| 1451 | NLM_F_MULTI); |
| 1452 | if (err) |
| 1453 | return err; |
| 1454 | (*p_idx)++; |
| 1455 | } |
| 1456 | for (tc_index = 0; |
| 1457 | tc_index < devlink_sb->egress_tc_count; tc_index++) { |
| 1458 | if (*p_idx < start) { |
| 1459 | (*p_idx)++; |
| 1460 | continue; |
| 1461 | } |
| 1462 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1463 | devlink_port, |
| 1464 | devlink_sb, |
| 1465 | tc_index, |
| 1466 | DEVLINK_SB_POOL_TYPE_EGRESS, |
| 1467 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1468 | portid, seq, |
| 1469 | NLM_F_MULTI); |
| 1470 | if (err) |
| 1471 | return err; |
| 1472 | (*p_idx)++; |
| 1473 | } |
| 1474 | } |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | static int |
| 1479 | devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1480 | struct netlink_callback *cb) |
| 1481 | { |
| 1482 | struct devlink *devlink; |
| 1483 | struct devlink_sb *devlink_sb; |
| 1484 | int start = cb->args[0]; |
| 1485 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1486 | int err = 0; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1487 | |
| 1488 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1489 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1490 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1491 | !devlink->ops->sb_tc_pool_bind_get) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1492 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1493 | |
| 1494 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1495 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1496 | err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx, |
| 1497 | devlink, |
| 1498 | devlink_sb, |
| 1499 | NETLINK_CB(cb->skb).portid, |
| 1500 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1501 | if (err && err != -EOPNOTSUPP) { |
| 1502 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1503 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1504 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1505 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1506 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1507 | } |
| 1508 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1509 | mutex_unlock(&devlink_mutex); |
| 1510 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 1511 | if (err != -EMSGSIZE) |
| 1512 | return err; |
| 1513 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1514 | cb->args[0] = idx; |
| 1515 | return msg->len; |
| 1516 | } |
| 1517 | |
| 1518 | static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port, |
| 1519 | unsigned int sb_index, u16 tc_index, |
| 1520 | enum devlink_sb_pool_type pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1521 | u16 pool_index, u32 threshold, |
| 1522 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1523 | |
| 1524 | { |
| 1525 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1526 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1527 | if (ops->sb_tc_pool_bind_set) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1528 | return ops->sb_tc_pool_bind_set(devlink_port, sb_index, |
| 1529 | tc_index, pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1530 | pool_index, threshold, extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1531 | return -EOPNOTSUPP; |
| 1532 | } |
| 1533 | |
| 1534 | static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb, |
| 1535 | struct genl_info *info) |
| 1536 | { |
| 1537 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1538 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1539 | enum devlink_sb_pool_type pool_type; |
| 1540 | u16 tc_index; |
| 1541 | u16 pool_index; |
| 1542 | u32 threshold; |
| 1543 | int err; |
| 1544 | |
| 1545 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1546 | if (err) |
| 1547 | return err; |
| 1548 | |
| 1549 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1550 | pool_type, &tc_index); |
| 1551 | if (err) |
| 1552 | return err; |
| 1553 | |
| 1554 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1555 | &pool_index); |
| 1556 | if (err) |
| 1557 | return err; |
| 1558 | |
| 1559 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1560 | return -EINVAL; |
| 1561 | |
| 1562 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1563 | return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index, |
| 1564 | tc_index, pool_type, |
Ido Schimmel | f2ad1a5 | 2019-04-22 12:08:39 +0000 | [diff] [blame] | 1565 | pool_index, threshold, info->extack); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1568 | static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb, |
| 1569 | struct genl_info *info) |
| 1570 | { |
| 1571 | struct devlink *devlink = info->user_ptr[0]; |
| 1572 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1573 | const struct devlink_ops *ops = devlink->ops; |
| 1574 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1575 | if (ops->sb_occ_snapshot) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1576 | return ops->sb_occ_snapshot(devlink, devlink_sb->index); |
| 1577 | return -EOPNOTSUPP; |
| 1578 | } |
| 1579 | |
| 1580 | static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb, |
| 1581 | struct genl_info *info) |
| 1582 | { |
| 1583 | struct devlink *devlink = info->user_ptr[0]; |
| 1584 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1585 | const struct devlink_ops *ops = devlink->ops; |
| 1586 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 1587 | if (ops->sb_occ_max_clear) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1588 | return ops->sb_occ_max_clear(devlink, devlink_sb->index); |
| 1589 | return -EOPNOTSUPP; |
| 1590 | } |
| 1591 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1592 | static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1593 | enum devlink_command cmd, u32 portid, |
| 1594 | u32 seq, int flags) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1595 | { |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1596 | const struct devlink_ops *ops = devlink->ops; |
Leon Romanovsky | 98fdbea | 2019-06-12 15:20:11 +0300 | [diff] [blame] | 1597 | enum devlink_eswitch_encap_mode encap_mode; |
| 1598 | u8 inline_mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1599 | void *hdr; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1600 | int err = 0; |
| 1601 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1602 | |
| 1603 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1604 | if (!hdr) |
| 1605 | return -EMSGSIZE; |
| 1606 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1607 | err = devlink_nl_put_handle(msg, devlink); |
| 1608 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1609 | goto nla_put_failure; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1610 | |
Jiri Pirko | 4456f61 | 2017-02-09 15:54:36 +0100 | [diff] [blame] | 1611 | if (ops->eswitch_mode_get) { |
| 1612 | err = ops->eswitch_mode_get(devlink, &mode); |
| 1613 | if (err) |
| 1614 | goto nla_put_failure; |
| 1615 | err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode); |
| 1616 | if (err) |
| 1617 | goto nla_put_failure; |
| 1618 | } |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1619 | |
| 1620 | if (ops->eswitch_inline_mode_get) { |
| 1621 | err = ops->eswitch_inline_mode_get(devlink, &inline_mode); |
| 1622 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1623 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1624 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE, |
| 1625 | inline_mode); |
| 1626 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1627 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1628 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1629 | |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1630 | if (ops->eswitch_encap_mode_get) { |
| 1631 | err = ops->eswitch_encap_mode_get(devlink, &encap_mode); |
| 1632 | if (err) |
| 1633 | goto nla_put_failure; |
| 1634 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode); |
| 1635 | if (err) |
| 1636 | goto nla_put_failure; |
| 1637 | } |
| 1638 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1639 | genlmsg_end(msg, hdr); |
| 1640 | return 0; |
| 1641 | |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1642 | nla_put_failure: |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1643 | genlmsg_cancel(msg, hdr); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1644 | return err; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1645 | } |
| 1646 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1647 | static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, |
| 1648 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1649 | { |
| 1650 | struct devlink *devlink = info->user_ptr[0]; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1651 | struct sk_buff *msg; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1652 | int err; |
| 1653 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1654 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1655 | if (!msg) |
| 1656 | return -ENOMEM; |
| 1657 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1658 | err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET, |
| 1659 | info->snd_portid, info->snd_seq, 0); |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1660 | |
| 1661 | if (err) { |
| 1662 | nlmsg_free(msg); |
| 1663 | return err; |
| 1664 | } |
| 1665 | |
| 1666 | return genlmsg_reply(msg, info); |
| 1667 | } |
| 1668 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1669 | static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, |
| 1670 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1671 | { |
| 1672 | struct devlink *devlink = info->user_ptr[0]; |
| 1673 | const struct devlink_ops *ops = devlink->ops; |
Leon Romanovsky | 98fdbea | 2019-06-12 15:20:11 +0300 | [diff] [blame] | 1674 | enum devlink_eswitch_encap_mode encap_mode; |
| 1675 | u8 inline_mode; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1676 | int err = 0; |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1677 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1678 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1679 | if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) { |
| 1680 | if (!ops->eswitch_mode_set) |
| 1681 | return -EOPNOTSUPP; |
| 1682 | mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1683 | err = ops->eswitch_mode_set(devlink, mode, info->extack); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1684 | if (err) |
| 1685 | return err; |
| 1686 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1687 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1688 | if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) { |
| 1689 | if (!ops->eswitch_inline_mode_set) |
| 1690 | return -EOPNOTSUPP; |
| 1691 | inline_mode = nla_get_u8( |
| 1692 | info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1693 | err = ops->eswitch_inline_mode_set(devlink, inline_mode, |
| 1694 | info->extack); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1695 | if (err) |
| 1696 | return err; |
| 1697 | } |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1698 | |
| 1699 | if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) { |
| 1700 | if (!ops->eswitch_encap_mode_set) |
| 1701 | return -EOPNOTSUPP; |
| 1702 | encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]); |
Eli Britstein | db7ff19 | 2018-08-15 16:02:18 +0300 | [diff] [blame] | 1703 | err = ops->eswitch_encap_mode_set(devlink, encap_mode, |
| 1704 | info->extack); |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1705 | if (err) |
| 1706 | return err; |
| 1707 | } |
| 1708 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1709 | return 0; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1710 | } |
| 1711 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1712 | int devlink_dpipe_match_put(struct sk_buff *skb, |
| 1713 | struct devlink_dpipe_match *match) |
| 1714 | { |
| 1715 | struct devlink_dpipe_header *header = match->header; |
| 1716 | struct devlink_dpipe_field *field = &header->fields[match->field_id]; |
| 1717 | struct nlattr *match_attr; |
| 1718 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1719 | match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1720 | if (!match_attr) |
| 1721 | return -EMSGSIZE; |
| 1722 | |
| 1723 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) || |
| 1724 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) || |
| 1725 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1726 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1727 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1728 | goto nla_put_failure; |
| 1729 | |
| 1730 | nla_nest_end(skb, match_attr); |
| 1731 | return 0; |
| 1732 | |
| 1733 | nla_put_failure: |
| 1734 | nla_nest_cancel(skb, match_attr); |
| 1735 | return -EMSGSIZE; |
| 1736 | } |
| 1737 | EXPORT_SYMBOL_GPL(devlink_dpipe_match_put); |
| 1738 | |
| 1739 | static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table, |
| 1740 | struct sk_buff *skb) |
| 1741 | { |
| 1742 | struct nlattr *matches_attr; |
| 1743 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1744 | matches_attr = nla_nest_start_noflag(skb, |
| 1745 | DEVLINK_ATTR_DPIPE_TABLE_MATCHES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1746 | if (!matches_attr) |
| 1747 | return -EMSGSIZE; |
| 1748 | |
| 1749 | if (table->table_ops->matches_dump(table->priv, skb)) |
| 1750 | goto nla_put_failure; |
| 1751 | |
| 1752 | nla_nest_end(skb, matches_attr); |
| 1753 | return 0; |
| 1754 | |
| 1755 | nla_put_failure: |
| 1756 | nla_nest_cancel(skb, matches_attr); |
| 1757 | return -EMSGSIZE; |
| 1758 | } |
| 1759 | |
| 1760 | int devlink_dpipe_action_put(struct sk_buff *skb, |
| 1761 | struct devlink_dpipe_action *action) |
| 1762 | { |
| 1763 | struct devlink_dpipe_header *header = action->header; |
| 1764 | struct devlink_dpipe_field *field = &header->fields[action->field_id]; |
| 1765 | struct nlattr *action_attr; |
| 1766 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1767 | action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1768 | if (!action_attr) |
| 1769 | return -EMSGSIZE; |
| 1770 | |
| 1771 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) || |
| 1772 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) || |
| 1773 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1774 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1775 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1776 | goto nla_put_failure; |
| 1777 | |
| 1778 | nla_nest_end(skb, action_attr); |
| 1779 | return 0; |
| 1780 | |
| 1781 | nla_put_failure: |
| 1782 | nla_nest_cancel(skb, action_attr); |
| 1783 | return -EMSGSIZE; |
| 1784 | } |
| 1785 | EXPORT_SYMBOL_GPL(devlink_dpipe_action_put); |
| 1786 | |
| 1787 | static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table, |
| 1788 | struct sk_buff *skb) |
| 1789 | { |
| 1790 | struct nlattr *actions_attr; |
| 1791 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1792 | actions_attr = nla_nest_start_noflag(skb, |
| 1793 | DEVLINK_ATTR_DPIPE_TABLE_ACTIONS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1794 | if (!actions_attr) |
| 1795 | return -EMSGSIZE; |
| 1796 | |
| 1797 | if (table->table_ops->actions_dump(table->priv, skb)) |
| 1798 | goto nla_put_failure; |
| 1799 | |
| 1800 | nla_nest_end(skb, actions_attr); |
| 1801 | return 0; |
| 1802 | |
| 1803 | nla_put_failure: |
| 1804 | nla_nest_cancel(skb, actions_attr); |
| 1805 | return -EMSGSIZE; |
| 1806 | } |
| 1807 | |
| 1808 | static int devlink_dpipe_table_put(struct sk_buff *skb, |
| 1809 | struct devlink_dpipe_table *table) |
| 1810 | { |
| 1811 | struct nlattr *table_attr; |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1812 | u64 table_size; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1813 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1814 | table_size = table->table_ops->size_get(table->priv); |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1815 | table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1816 | if (!table_attr) |
| 1817 | return -EMSGSIZE; |
| 1818 | |
| 1819 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) || |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1820 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1821 | DEVLINK_ATTR_PAD)) |
| 1822 | goto nla_put_failure; |
| 1823 | if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED, |
| 1824 | table->counters_enabled)) |
| 1825 | goto nla_put_failure; |
| 1826 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1827 | if (table->resource_valid) { |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 1828 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID, |
| 1829 | table->resource_id, DEVLINK_ATTR_PAD) || |
| 1830 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS, |
| 1831 | table->resource_units, DEVLINK_ATTR_PAD)) |
| 1832 | goto nla_put_failure; |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1833 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1834 | if (devlink_dpipe_matches_put(table, skb)) |
| 1835 | goto nla_put_failure; |
| 1836 | |
| 1837 | if (devlink_dpipe_actions_put(table, skb)) |
| 1838 | goto nla_put_failure; |
| 1839 | |
| 1840 | nla_nest_end(skb, table_attr); |
| 1841 | return 0; |
| 1842 | |
| 1843 | nla_put_failure: |
| 1844 | nla_nest_cancel(skb, table_attr); |
| 1845 | return -EMSGSIZE; |
| 1846 | } |
| 1847 | |
| 1848 | static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb, |
| 1849 | struct genl_info *info) |
| 1850 | { |
| 1851 | int err; |
| 1852 | |
| 1853 | if (*pskb) { |
| 1854 | err = genlmsg_reply(*pskb, info); |
| 1855 | if (err) |
| 1856 | return err; |
| 1857 | } |
| 1858 | *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1859 | if (!*pskb) |
| 1860 | return -ENOMEM; |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
| 1864 | static int devlink_dpipe_tables_fill(struct genl_info *info, |
| 1865 | enum devlink_command cmd, int flags, |
| 1866 | struct list_head *dpipe_tables, |
| 1867 | const char *table_name) |
| 1868 | { |
| 1869 | struct devlink *devlink = info->user_ptr[0]; |
| 1870 | struct devlink_dpipe_table *table; |
| 1871 | struct nlattr *tables_attr; |
| 1872 | struct sk_buff *skb = NULL; |
| 1873 | struct nlmsghdr *nlh; |
| 1874 | bool incomplete; |
| 1875 | void *hdr; |
| 1876 | int i; |
| 1877 | int err; |
| 1878 | |
| 1879 | table = list_first_entry(dpipe_tables, |
| 1880 | struct devlink_dpipe_table, list); |
| 1881 | start_again: |
| 1882 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1883 | if (err) |
| 1884 | return err; |
| 1885 | |
| 1886 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1887 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 1888 | if (!hdr) { |
| 1889 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1890 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 1891 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1892 | |
| 1893 | if (devlink_nl_put_handle(skb, devlink)) |
| 1894 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1895 | tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1896 | if (!tables_attr) |
| 1897 | goto nla_put_failure; |
| 1898 | |
| 1899 | i = 0; |
| 1900 | incomplete = false; |
| 1901 | list_for_each_entry_from(table, dpipe_tables, list) { |
| 1902 | if (!table_name) { |
| 1903 | err = devlink_dpipe_table_put(skb, table); |
| 1904 | if (err) { |
| 1905 | if (!i) |
| 1906 | goto err_table_put; |
| 1907 | incomplete = true; |
| 1908 | break; |
| 1909 | } |
| 1910 | } else { |
| 1911 | if (!strcmp(table->name, table_name)) { |
| 1912 | err = devlink_dpipe_table_put(skb, table); |
| 1913 | if (err) |
| 1914 | break; |
| 1915 | } |
| 1916 | } |
| 1917 | i++; |
| 1918 | } |
| 1919 | |
| 1920 | nla_nest_end(skb, tables_attr); |
| 1921 | genlmsg_end(skb, hdr); |
| 1922 | if (incomplete) |
| 1923 | goto start_again; |
| 1924 | |
| 1925 | send_done: |
| 1926 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1927 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 1928 | if (!nlh) { |
| 1929 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1930 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 1931 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1932 | goto send_done; |
| 1933 | } |
| 1934 | |
| 1935 | return genlmsg_reply(skb, info); |
| 1936 | |
| 1937 | nla_put_failure: |
| 1938 | err = -EMSGSIZE; |
| 1939 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1940 | nlmsg_free(skb); |
| 1941 | return err; |
| 1942 | } |
| 1943 | |
| 1944 | static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb, |
| 1945 | struct genl_info *info) |
| 1946 | { |
| 1947 | struct devlink *devlink = info->user_ptr[0]; |
| 1948 | const char *table_name = NULL; |
| 1949 | |
| 1950 | if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 1951 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 1952 | |
| 1953 | return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0, |
| 1954 | &devlink->dpipe_table_list, |
| 1955 | table_name); |
| 1956 | } |
| 1957 | |
| 1958 | static int devlink_dpipe_value_put(struct sk_buff *skb, |
| 1959 | struct devlink_dpipe_value *value) |
| 1960 | { |
| 1961 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE, |
| 1962 | value->value_size, value->value)) |
| 1963 | return -EMSGSIZE; |
| 1964 | if (value->mask) |
| 1965 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK, |
| 1966 | value->value_size, value->mask)) |
| 1967 | return -EMSGSIZE; |
| 1968 | if (value->mapping_valid) |
| 1969 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING, |
| 1970 | value->mapping_value)) |
| 1971 | return -EMSGSIZE; |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
| 1975 | static int devlink_dpipe_action_value_put(struct sk_buff *skb, |
| 1976 | struct devlink_dpipe_value *value) |
| 1977 | { |
| 1978 | if (!value->action) |
| 1979 | return -EINVAL; |
| 1980 | if (devlink_dpipe_action_put(skb, value->action)) |
| 1981 | return -EMSGSIZE; |
| 1982 | if (devlink_dpipe_value_put(skb, value)) |
| 1983 | return -EMSGSIZE; |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | static int devlink_dpipe_action_values_put(struct sk_buff *skb, |
| 1988 | struct devlink_dpipe_value *values, |
| 1989 | unsigned int values_count) |
| 1990 | { |
| 1991 | struct nlattr *action_attr; |
| 1992 | int i; |
| 1993 | int err; |
| 1994 | |
| 1995 | for (i = 0; i < values_count; i++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1996 | action_attr = nla_nest_start_noflag(skb, |
| 1997 | DEVLINK_ATTR_DPIPE_ACTION_VALUE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1998 | if (!action_attr) |
| 1999 | return -EMSGSIZE; |
| 2000 | err = devlink_dpipe_action_value_put(skb, &values[i]); |
| 2001 | if (err) |
| 2002 | goto err_action_value_put; |
| 2003 | nla_nest_end(skb, action_attr); |
| 2004 | } |
| 2005 | return 0; |
| 2006 | |
| 2007 | err_action_value_put: |
| 2008 | nla_nest_cancel(skb, action_attr); |
| 2009 | return err; |
| 2010 | } |
| 2011 | |
| 2012 | static int devlink_dpipe_match_value_put(struct sk_buff *skb, |
| 2013 | struct devlink_dpipe_value *value) |
| 2014 | { |
| 2015 | if (!value->match) |
| 2016 | return -EINVAL; |
| 2017 | if (devlink_dpipe_match_put(skb, value->match)) |
| 2018 | return -EMSGSIZE; |
| 2019 | if (devlink_dpipe_value_put(skb, value)) |
| 2020 | return -EMSGSIZE; |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
| 2024 | static int devlink_dpipe_match_values_put(struct sk_buff *skb, |
| 2025 | struct devlink_dpipe_value *values, |
| 2026 | unsigned int values_count) |
| 2027 | { |
| 2028 | struct nlattr *match_attr; |
| 2029 | int i; |
| 2030 | int err; |
| 2031 | |
| 2032 | for (i = 0; i < values_count; i++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2033 | match_attr = nla_nest_start_noflag(skb, |
| 2034 | DEVLINK_ATTR_DPIPE_MATCH_VALUE); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2035 | if (!match_attr) |
| 2036 | return -EMSGSIZE; |
| 2037 | err = devlink_dpipe_match_value_put(skb, &values[i]); |
| 2038 | if (err) |
| 2039 | goto err_match_value_put; |
| 2040 | nla_nest_end(skb, match_attr); |
| 2041 | } |
| 2042 | return 0; |
| 2043 | |
| 2044 | err_match_value_put: |
| 2045 | nla_nest_cancel(skb, match_attr); |
| 2046 | return err; |
| 2047 | } |
| 2048 | |
| 2049 | static int devlink_dpipe_entry_put(struct sk_buff *skb, |
| 2050 | struct devlink_dpipe_entry *entry) |
| 2051 | { |
| 2052 | struct nlattr *entry_attr, *matches_attr, *actions_attr; |
| 2053 | int err; |
| 2054 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2055 | entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2056 | if (!entry_attr) |
| 2057 | return -EMSGSIZE; |
| 2058 | |
| 2059 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index, |
| 2060 | DEVLINK_ATTR_PAD)) |
| 2061 | goto nla_put_failure; |
| 2062 | if (entry->counter_valid) |
| 2063 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER, |
| 2064 | entry->counter, DEVLINK_ATTR_PAD)) |
| 2065 | goto nla_put_failure; |
| 2066 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2067 | matches_attr = nla_nest_start_noflag(skb, |
| 2068 | DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2069 | if (!matches_attr) |
| 2070 | goto nla_put_failure; |
| 2071 | |
| 2072 | err = devlink_dpipe_match_values_put(skb, entry->match_values, |
| 2073 | entry->match_values_count); |
| 2074 | if (err) { |
| 2075 | nla_nest_cancel(skb, matches_attr); |
| 2076 | goto err_match_values_put; |
| 2077 | } |
| 2078 | nla_nest_end(skb, matches_attr); |
| 2079 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2080 | actions_attr = nla_nest_start_noflag(skb, |
| 2081 | DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2082 | if (!actions_attr) |
| 2083 | goto nla_put_failure; |
| 2084 | |
| 2085 | err = devlink_dpipe_action_values_put(skb, entry->action_values, |
| 2086 | entry->action_values_count); |
| 2087 | if (err) { |
| 2088 | nla_nest_cancel(skb, actions_attr); |
| 2089 | goto err_action_values_put; |
| 2090 | } |
| 2091 | nla_nest_end(skb, actions_attr); |
| 2092 | |
| 2093 | nla_nest_end(skb, entry_attr); |
| 2094 | return 0; |
| 2095 | |
| 2096 | nla_put_failure: |
| 2097 | err = -EMSGSIZE; |
| 2098 | err_match_values_put: |
| 2099 | err_action_values_put: |
| 2100 | nla_nest_cancel(skb, entry_attr); |
| 2101 | return err; |
| 2102 | } |
| 2103 | |
| 2104 | static struct devlink_dpipe_table * |
| 2105 | devlink_dpipe_table_find(struct list_head *dpipe_tables, |
| 2106 | const char *table_name) |
| 2107 | { |
| 2108 | struct devlink_dpipe_table *table; |
| 2109 | |
| 2110 | list_for_each_entry_rcu(table, dpipe_tables, list) { |
| 2111 | if (!strcmp(table->name, table_name)) |
| 2112 | return table; |
| 2113 | } |
| 2114 | return NULL; |
| 2115 | } |
| 2116 | |
| 2117 | int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2118 | { |
| 2119 | struct devlink *devlink; |
| 2120 | int err; |
| 2121 | |
| 2122 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb, |
| 2123 | dump_ctx->info); |
| 2124 | if (err) |
| 2125 | return err; |
| 2126 | |
| 2127 | dump_ctx->hdr = genlmsg_put(dump_ctx->skb, |
| 2128 | dump_ctx->info->snd_portid, |
| 2129 | dump_ctx->info->snd_seq, |
| 2130 | &devlink_nl_family, NLM_F_MULTI, |
| 2131 | dump_ctx->cmd); |
| 2132 | if (!dump_ctx->hdr) |
| 2133 | goto nla_put_failure; |
| 2134 | |
| 2135 | devlink = dump_ctx->info->user_ptr[0]; |
| 2136 | if (devlink_nl_put_handle(dump_ctx->skb, devlink)) |
| 2137 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2138 | dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb, |
| 2139 | DEVLINK_ATTR_DPIPE_ENTRIES); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2140 | if (!dump_ctx->nest) |
| 2141 | goto nla_put_failure; |
| 2142 | return 0; |
| 2143 | |
| 2144 | nla_put_failure: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2145 | nlmsg_free(dump_ctx->skb); |
| 2146 | return -EMSGSIZE; |
| 2147 | } |
| 2148 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare); |
| 2149 | |
| 2150 | int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, |
| 2151 | struct devlink_dpipe_entry *entry) |
| 2152 | { |
| 2153 | return devlink_dpipe_entry_put(dump_ctx->skb, entry); |
| 2154 | } |
| 2155 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append); |
| 2156 | |
| 2157 | int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2158 | { |
| 2159 | nla_nest_end(dump_ctx->skb, dump_ctx->nest); |
| 2160 | genlmsg_end(dump_ctx->skb, dump_ctx->hdr); |
| 2161 | return 0; |
| 2162 | } |
| 2163 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close); |
| 2164 | |
Arkadi Sharshevsky | 3580732 | 2017-08-24 08:40:03 +0200 | [diff] [blame] | 2165 | void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry) |
| 2166 | |
| 2167 | { |
| 2168 | unsigned int value_count, value_index; |
| 2169 | struct devlink_dpipe_value *value; |
| 2170 | |
| 2171 | value = entry->action_values; |
| 2172 | value_count = entry->action_values_count; |
| 2173 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2174 | kfree(value[value_index].value); |
| 2175 | kfree(value[value_index].mask); |
| 2176 | } |
| 2177 | |
| 2178 | value = entry->match_values; |
| 2179 | value_count = entry->match_values_count; |
| 2180 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2181 | kfree(value[value_index].value); |
| 2182 | kfree(value[value_index].mask); |
| 2183 | } |
| 2184 | } |
| 2185 | EXPORT_SYMBOL(devlink_dpipe_entry_clear); |
| 2186 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2187 | static int devlink_dpipe_entries_fill(struct genl_info *info, |
| 2188 | enum devlink_command cmd, int flags, |
| 2189 | struct devlink_dpipe_table *table) |
| 2190 | { |
| 2191 | struct devlink_dpipe_dump_ctx dump_ctx; |
| 2192 | struct nlmsghdr *nlh; |
| 2193 | int err; |
| 2194 | |
| 2195 | dump_ctx.skb = NULL; |
| 2196 | dump_ctx.cmd = cmd; |
| 2197 | dump_ctx.info = info; |
| 2198 | |
| 2199 | err = table->table_ops->entries_dump(table->priv, |
| 2200 | table->counters_enabled, |
| 2201 | &dump_ctx); |
| 2202 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2203 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2204 | |
| 2205 | send_done: |
| 2206 | nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq, |
| 2207 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2208 | if (!nlh) { |
| 2209 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info); |
| 2210 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2211 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2212 | goto send_done; |
| 2213 | } |
| 2214 | return genlmsg_reply(dump_ctx.skb, info); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb, |
| 2218 | struct genl_info *info) |
| 2219 | { |
| 2220 | struct devlink *devlink = info->user_ptr[0]; |
| 2221 | struct devlink_dpipe_table *table; |
| 2222 | const char *table_name; |
| 2223 | |
| 2224 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 2225 | return -EINVAL; |
| 2226 | |
| 2227 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2228 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2229 | table_name); |
| 2230 | if (!table) |
| 2231 | return -EINVAL; |
| 2232 | |
| 2233 | if (!table->table_ops->entries_dump) |
| 2234 | return -EINVAL; |
| 2235 | |
| 2236 | return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET, |
| 2237 | 0, table); |
| 2238 | } |
| 2239 | |
| 2240 | static int devlink_dpipe_fields_put(struct sk_buff *skb, |
| 2241 | const struct devlink_dpipe_header *header) |
| 2242 | { |
| 2243 | struct devlink_dpipe_field *field; |
| 2244 | struct nlattr *field_attr; |
| 2245 | int i; |
| 2246 | |
| 2247 | for (i = 0; i < header->fields_count; i++) { |
| 2248 | field = &header->fields[i]; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2249 | field_attr = nla_nest_start_noflag(skb, |
| 2250 | DEVLINK_ATTR_DPIPE_FIELD); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2251 | if (!field_attr) |
| 2252 | return -EMSGSIZE; |
| 2253 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) || |
| 2254 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 2255 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) || |
| 2256 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type)) |
| 2257 | goto nla_put_failure; |
| 2258 | nla_nest_end(skb, field_attr); |
| 2259 | } |
| 2260 | return 0; |
| 2261 | |
| 2262 | nla_put_failure: |
| 2263 | nla_nest_cancel(skb, field_attr); |
| 2264 | return -EMSGSIZE; |
| 2265 | } |
| 2266 | |
| 2267 | static int devlink_dpipe_header_put(struct sk_buff *skb, |
| 2268 | struct devlink_dpipe_header *header) |
| 2269 | { |
| 2270 | struct nlattr *fields_attr, *header_attr; |
| 2271 | int err; |
| 2272 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2273 | header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER); |
Wei Yongjun | cb6bf9c | 2017-04-11 16:02:02 +0000 | [diff] [blame] | 2274 | if (!header_attr) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2275 | return -EMSGSIZE; |
| 2276 | |
| 2277 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) || |
| 2278 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 2279 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 2280 | goto nla_put_failure; |
| 2281 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2282 | fields_attr = nla_nest_start_noflag(skb, |
| 2283 | DEVLINK_ATTR_DPIPE_HEADER_FIELDS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2284 | if (!fields_attr) |
| 2285 | goto nla_put_failure; |
| 2286 | |
| 2287 | err = devlink_dpipe_fields_put(skb, header); |
| 2288 | if (err) { |
| 2289 | nla_nest_cancel(skb, fields_attr); |
| 2290 | goto nla_put_failure; |
| 2291 | } |
| 2292 | nla_nest_end(skb, fields_attr); |
| 2293 | nla_nest_end(skb, header_attr); |
| 2294 | return 0; |
| 2295 | |
| 2296 | nla_put_failure: |
| 2297 | err = -EMSGSIZE; |
| 2298 | nla_nest_cancel(skb, header_attr); |
| 2299 | return err; |
| 2300 | } |
| 2301 | |
| 2302 | static int devlink_dpipe_headers_fill(struct genl_info *info, |
| 2303 | enum devlink_command cmd, int flags, |
| 2304 | struct devlink_dpipe_headers * |
| 2305 | dpipe_headers) |
| 2306 | { |
| 2307 | struct devlink *devlink = info->user_ptr[0]; |
| 2308 | struct nlattr *headers_attr; |
| 2309 | struct sk_buff *skb = NULL; |
| 2310 | struct nlmsghdr *nlh; |
| 2311 | void *hdr; |
| 2312 | int i, j; |
| 2313 | int err; |
| 2314 | |
| 2315 | i = 0; |
| 2316 | start_again: |
| 2317 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2318 | if (err) |
| 2319 | return err; |
| 2320 | |
| 2321 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2322 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2323 | if (!hdr) { |
| 2324 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2325 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2326 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2327 | |
| 2328 | if (devlink_nl_put_handle(skb, devlink)) |
| 2329 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2330 | headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2331 | if (!headers_attr) |
| 2332 | goto nla_put_failure; |
| 2333 | |
| 2334 | j = 0; |
| 2335 | for (; i < dpipe_headers->headers_count; i++) { |
| 2336 | err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]); |
| 2337 | if (err) { |
| 2338 | if (!j) |
| 2339 | goto err_table_put; |
| 2340 | break; |
| 2341 | } |
| 2342 | j++; |
| 2343 | } |
| 2344 | nla_nest_end(skb, headers_attr); |
| 2345 | genlmsg_end(skb, hdr); |
| 2346 | if (i != dpipe_headers->headers_count) |
| 2347 | goto start_again; |
| 2348 | |
| 2349 | send_done: |
| 2350 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2351 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2352 | if (!nlh) { |
| 2353 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2354 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2355 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2356 | goto send_done; |
| 2357 | } |
| 2358 | return genlmsg_reply(skb, info); |
| 2359 | |
| 2360 | nla_put_failure: |
| 2361 | err = -EMSGSIZE; |
| 2362 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2363 | nlmsg_free(skb); |
| 2364 | return err; |
| 2365 | } |
| 2366 | |
| 2367 | static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb, |
| 2368 | struct genl_info *info) |
| 2369 | { |
| 2370 | struct devlink *devlink = info->user_ptr[0]; |
| 2371 | |
| 2372 | if (!devlink->dpipe_headers) |
| 2373 | return -EOPNOTSUPP; |
| 2374 | return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET, |
| 2375 | 0, devlink->dpipe_headers); |
| 2376 | } |
| 2377 | |
| 2378 | static int devlink_dpipe_table_counters_set(struct devlink *devlink, |
| 2379 | const char *table_name, |
| 2380 | bool enable) |
| 2381 | { |
| 2382 | struct devlink_dpipe_table *table; |
| 2383 | |
| 2384 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2385 | table_name); |
| 2386 | if (!table) |
| 2387 | return -EINVAL; |
| 2388 | |
| 2389 | if (table->counter_control_extern) |
| 2390 | return -EOPNOTSUPP; |
| 2391 | |
| 2392 | if (!(table->counters_enabled ^ enable)) |
| 2393 | return 0; |
| 2394 | |
| 2395 | table->counters_enabled = enable; |
| 2396 | if (table->table_ops->counters_set_update) |
| 2397 | table->table_ops->counters_set_update(table->priv, enable); |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb, |
| 2402 | struct genl_info *info) |
| 2403 | { |
| 2404 | struct devlink *devlink = info->user_ptr[0]; |
| 2405 | const char *table_name; |
| 2406 | bool counters_enable; |
| 2407 | |
| 2408 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] || |
| 2409 | !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) |
| 2410 | return -EINVAL; |
| 2411 | |
| 2412 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2413 | counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]); |
| 2414 | |
| 2415 | return devlink_dpipe_table_counters_set(devlink, table_name, |
| 2416 | counters_enable); |
| 2417 | } |
| 2418 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2419 | static struct devlink_resource * |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2420 | devlink_resource_find(struct devlink *devlink, |
| 2421 | struct devlink_resource *resource, u64 resource_id) |
| 2422 | { |
| 2423 | struct list_head *resource_list; |
| 2424 | |
| 2425 | if (resource) |
| 2426 | resource_list = &resource->resource_list; |
| 2427 | else |
| 2428 | resource_list = &devlink->resource_list; |
| 2429 | |
| 2430 | list_for_each_entry(resource, resource_list, list) { |
| 2431 | struct devlink_resource *child_resource; |
| 2432 | |
| 2433 | if (resource->id == resource_id) |
| 2434 | return resource; |
| 2435 | |
| 2436 | child_resource = devlink_resource_find(devlink, resource, |
| 2437 | resource_id); |
| 2438 | if (child_resource) |
| 2439 | return child_resource; |
| 2440 | } |
| 2441 | return NULL; |
| 2442 | } |
| 2443 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2444 | static void |
| 2445 | devlink_resource_validate_children(struct devlink_resource *resource) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2446 | { |
| 2447 | struct devlink_resource *child_resource; |
| 2448 | bool size_valid = true; |
| 2449 | u64 parts_size = 0; |
| 2450 | |
| 2451 | if (list_empty(&resource->resource_list)) |
| 2452 | goto out; |
| 2453 | |
| 2454 | list_for_each_entry(child_resource, &resource->resource_list, list) |
| 2455 | parts_size += child_resource->size_new; |
| 2456 | |
Arkadi Sharshevsky | b9d1717 | 2018-02-26 10:59:53 +0100 | [diff] [blame] | 2457 | if (parts_size > resource->size_new) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2458 | size_valid = false; |
| 2459 | out: |
| 2460 | resource->size_valid = size_valid; |
| 2461 | } |
| 2462 | |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2463 | static int |
| 2464 | devlink_resource_validate_size(struct devlink_resource *resource, u64 size, |
| 2465 | struct netlink_ext_ack *extack) |
| 2466 | { |
| 2467 | u64 reminder; |
| 2468 | int err = 0; |
| 2469 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2470 | if (size > resource->size_params.size_max) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2471 | NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum"); |
| 2472 | err = -EINVAL; |
| 2473 | } |
| 2474 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2475 | if (size < resource->size_params.size_min) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2476 | NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum"); |
| 2477 | err = -EINVAL; |
| 2478 | } |
| 2479 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2480 | div64_u64_rem(size, resource->size_params.size_granularity, &reminder); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2481 | if (reminder) { |
| 2482 | NL_SET_ERR_MSG_MOD(extack, "Wrong granularity"); |
| 2483 | err = -EINVAL; |
| 2484 | } |
| 2485 | |
| 2486 | return err; |
| 2487 | } |
| 2488 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2489 | static int devlink_nl_cmd_resource_set(struct sk_buff *skb, |
| 2490 | struct genl_info *info) |
| 2491 | { |
| 2492 | struct devlink *devlink = info->user_ptr[0]; |
| 2493 | struct devlink_resource *resource; |
| 2494 | u64 resource_id; |
| 2495 | u64 size; |
| 2496 | int err; |
| 2497 | |
| 2498 | if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] || |
| 2499 | !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]) |
| 2500 | return -EINVAL; |
| 2501 | resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]); |
| 2502 | |
| 2503 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 2504 | if (!resource) |
| 2505 | return -EINVAL; |
| 2506 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2507 | size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2508 | err = devlink_resource_validate_size(resource, size, info->extack); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2509 | if (err) |
| 2510 | return err; |
| 2511 | |
| 2512 | resource->size_new = size; |
| 2513 | devlink_resource_validate_children(resource); |
| 2514 | if (resource->parent) |
| 2515 | devlink_resource_validate_children(resource->parent); |
| 2516 | return 0; |
| 2517 | } |
| 2518 | |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2519 | static int |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2520 | devlink_resource_size_params_put(struct devlink_resource *resource, |
| 2521 | struct sk_buff *skb) |
| 2522 | { |
| 2523 | struct devlink_resource_size_params *size_params; |
| 2524 | |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 2525 | size_params = &resource->size_params; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2526 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN, |
| 2527 | size_params->size_granularity, DEVLINK_ATTR_PAD) || |
| 2528 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX, |
| 2529 | size_params->size_max, DEVLINK_ATTR_PAD) || |
| 2530 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN, |
| 2531 | size_params->size_min, DEVLINK_ATTR_PAD) || |
| 2532 | nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit)) |
| 2533 | return -EMSGSIZE; |
| 2534 | return 0; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2535 | } |
| 2536 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2537 | static int devlink_resource_occ_put(struct devlink_resource *resource, |
| 2538 | struct sk_buff *skb) |
| 2539 | { |
| 2540 | if (!resource->occ_get) |
| 2541 | return 0; |
| 2542 | return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC, |
| 2543 | resource->occ_get(resource->occ_get_priv), |
| 2544 | DEVLINK_ATTR_PAD); |
| 2545 | } |
| 2546 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2547 | static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb, |
| 2548 | struct devlink_resource *resource) |
| 2549 | { |
| 2550 | struct devlink_resource *child_resource; |
| 2551 | struct nlattr *child_resource_attr; |
| 2552 | struct nlattr *resource_attr; |
| 2553 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2554 | resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2555 | if (!resource_attr) |
| 2556 | return -EMSGSIZE; |
| 2557 | |
| 2558 | if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) || |
| 2559 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size, |
| 2560 | DEVLINK_ATTR_PAD) || |
| 2561 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id, |
| 2562 | DEVLINK_ATTR_PAD)) |
| 2563 | goto nla_put_failure; |
| 2564 | if (resource->size != resource->size_new) |
| 2565 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW, |
| 2566 | resource->size_new, DEVLINK_ATTR_PAD); |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2567 | if (devlink_resource_occ_put(resource, skb)) |
| 2568 | goto nla_put_failure; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2569 | if (devlink_resource_size_params_put(resource, skb)) |
| 2570 | goto nla_put_failure; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2571 | if (list_empty(&resource->resource_list)) |
| 2572 | goto out; |
| 2573 | |
| 2574 | if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID, |
| 2575 | resource->size_valid)) |
| 2576 | goto nla_put_failure; |
| 2577 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2578 | child_resource_attr = nla_nest_start_noflag(skb, |
| 2579 | DEVLINK_ATTR_RESOURCE_LIST); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2580 | if (!child_resource_attr) |
| 2581 | goto nla_put_failure; |
| 2582 | |
| 2583 | list_for_each_entry(child_resource, &resource->resource_list, list) { |
| 2584 | if (devlink_resource_put(devlink, skb, child_resource)) |
| 2585 | goto resource_put_failure; |
| 2586 | } |
| 2587 | |
| 2588 | nla_nest_end(skb, child_resource_attr); |
| 2589 | out: |
| 2590 | nla_nest_end(skb, resource_attr); |
| 2591 | return 0; |
| 2592 | |
| 2593 | resource_put_failure: |
| 2594 | nla_nest_cancel(skb, child_resource_attr); |
| 2595 | nla_put_failure: |
| 2596 | nla_nest_cancel(skb, resource_attr); |
| 2597 | return -EMSGSIZE; |
| 2598 | } |
| 2599 | |
| 2600 | static int devlink_resource_fill(struct genl_info *info, |
| 2601 | enum devlink_command cmd, int flags) |
| 2602 | { |
| 2603 | struct devlink *devlink = info->user_ptr[0]; |
| 2604 | struct devlink_resource *resource; |
| 2605 | struct nlattr *resources_attr; |
| 2606 | struct sk_buff *skb = NULL; |
| 2607 | struct nlmsghdr *nlh; |
| 2608 | bool incomplete; |
| 2609 | void *hdr; |
| 2610 | int i; |
| 2611 | int err; |
| 2612 | |
| 2613 | resource = list_first_entry(&devlink->resource_list, |
| 2614 | struct devlink_resource, list); |
| 2615 | start_again: |
| 2616 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2617 | if (err) |
| 2618 | return err; |
| 2619 | |
| 2620 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2621 | &devlink_nl_family, NLM_F_MULTI, cmd); |
| 2622 | if (!hdr) { |
| 2623 | nlmsg_free(skb); |
| 2624 | return -EMSGSIZE; |
| 2625 | } |
| 2626 | |
| 2627 | if (devlink_nl_put_handle(skb, devlink)) |
| 2628 | goto nla_put_failure; |
| 2629 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2630 | resources_attr = nla_nest_start_noflag(skb, |
| 2631 | DEVLINK_ATTR_RESOURCE_LIST); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2632 | if (!resources_attr) |
| 2633 | goto nla_put_failure; |
| 2634 | |
| 2635 | incomplete = false; |
| 2636 | i = 0; |
| 2637 | list_for_each_entry_from(resource, &devlink->resource_list, list) { |
| 2638 | err = devlink_resource_put(devlink, skb, resource); |
| 2639 | if (err) { |
| 2640 | if (!i) |
| 2641 | goto err_resource_put; |
| 2642 | incomplete = true; |
| 2643 | break; |
| 2644 | } |
| 2645 | i++; |
| 2646 | } |
| 2647 | nla_nest_end(skb, resources_attr); |
| 2648 | genlmsg_end(skb, hdr); |
| 2649 | if (incomplete) |
| 2650 | goto start_again; |
| 2651 | send_done: |
| 2652 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2653 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2654 | if (!nlh) { |
| 2655 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2656 | if (err) |
Dan Carpenter | 83fe9a9 | 2018-09-21 11:07:55 +0300 | [diff] [blame] | 2657 | return err; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2658 | goto send_done; |
| 2659 | } |
| 2660 | return genlmsg_reply(skb, info); |
| 2661 | |
| 2662 | nla_put_failure: |
| 2663 | err = -EMSGSIZE; |
| 2664 | err_resource_put: |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2665 | nlmsg_free(skb); |
| 2666 | return err; |
| 2667 | } |
| 2668 | |
| 2669 | static int devlink_nl_cmd_resource_dump(struct sk_buff *skb, |
| 2670 | struct genl_info *info) |
| 2671 | { |
| 2672 | struct devlink *devlink = info->user_ptr[0]; |
| 2673 | |
| 2674 | if (list_empty(&devlink->resource_list)) |
| 2675 | return -EOPNOTSUPP; |
| 2676 | |
| 2677 | return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0); |
| 2678 | } |
| 2679 | |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2680 | static int |
| 2681 | devlink_resources_validate(struct devlink *devlink, |
| 2682 | struct devlink_resource *resource, |
| 2683 | struct genl_info *info) |
| 2684 | { |
| 2685 | struct list_head *resource_list; |
| 2686 | int err = 0; |
| 2687 | |
| 2688 | if (resource) |
| 2689 | resource_list = &resource->resource_list; |
| 2690 | else |
| 2691 | resource_list = &devlink->resource_list; |
| 2692 | |
| 2693 | list_for_each_entry(resource, resource_list, list) { |
| 2694 | if (!resource->size_valid) |
| 2695 | return -EINVAL; |
| 2696 | err = devlink_resources_validate(devlink, resource, info); |
| 2697 | if (err) |
| 2698 | return err; |
| 2699 | } |
| 2700 | return err; |
| 2701 | } |
| 2702 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2703 | static struct net *devlink_netns_get(struct sk_buff *skb, |
| 2704 | struct genl_info *info) |
| 2705 | { |
| 2706 | struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID]; |
| 2707 | struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD]; |
| 2708 | struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID]; |
| 2709 | struct net *net; |
| 2710 | |
| 2711 | if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) { |
| 2712 | NL_SET_ERR_MSG(info->extack, "multiple netns identifying attributes specified"); |
| 2713 | return ERR_PTR(-EINVAL); |
| 2714 | } |
| 2715 | |
| 2716 | if (netns_pid_attr) { |
| 2717 | net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr)); |
| 2718 | } else if (netns_fd_attr) { |
| 2719 | net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr)); |
| 2720 | } else if (netns_id_attr) { |
| 2721 | net = get_net_ns_by_id(sock_net(skb->sk), |
| 2722 | nla_get_u32(netns_id_attr)); |
| 2723 | if (!net) |
| 2724 | net = ERR_PTR(-EINVAL); |
| 2725 | } else { |
| 2726 | WARN_ON(1); |
| 2727 | net = ERR_PTR(-EINVAL); |
| 2728 | } |
| 2729 | if (IS_ERR(net)) { |
| 2730 | NL_SET_ERR_MSG(info->extack, "Unknown network namespace"); |
| 2731 | return ERR_PTR(-EINVAL); |
| 2732 | } |
| 2733 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
| 2734 | put_net(net); |
| 2735 | return ERR_PTR(-EPERM); |
| 2736 | } |
| 2737 | return net; |
| 2738 | } |
| 2739 | |
| 2740 | static void devlink_param_notify(struct devlink *devlink, |
| 2741 | unsigned int port_index, |
| 2742 | struct devlink_param_item *param_item, |
| 2743 | enum devlink_command cmd); |
| 2744 | |
| 2745 | static void devlink_reload_netns_change(struct devlink *devlink, |
| 2746 | struct net *dest_net) |
| 2747 | { |
| 2748 | struct devlink_param_item *param_item; |
| 2749 | |
| 2750 | /* Userspace needs to be notified about devlink objects |
| 2751 | * removed from original and entering new network namespace. |
| 2752 | * The rest of the devlink objects are re-created during |
| 2753 | * reload process so the notifications are generated separatelly. |
| 2754 | */ |
| 2755 | |
| 2756 | list_for_each_entry(param_item, &devlink->param_list, list) |
| 2757 | devlink_param_notify(devlink, 0, param_item, |
| 2758 | DEVLINK_CMD_PARAM_DEL); |
| 2759 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 2760 | |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 2761 | __devlink_net_set(devlink, dest_net); |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2762 | |
| 2763 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 2764 | list_for_each_entry(param_item, &devlink->param_list, list) |
| 2765 | devlink_param_notify(devlink, 0, param_item, |
| 2766 | DEVLINK_CMD_PARAM_NEW); |
| 2767 | } |
| 2768 | |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 2769 | static bool devlink_reload_supported(struct devlink *devlink) |
| 2770 | { |
| 2771 | return devlink->ops->reload_down && devlink->ops->reload_up; |
| 2772 | } |
| 2773 | |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 2774 | static void devlink_reload_failed_set(struct devlink *devlink, |
| 2775 | bool reload_failed) |
| 2776 | { |
| 2777 | if (devlink->reload_failed == reload_failed) |
| 2778 | return; |
| 2779 | devlink->reload_failed = reload_failed; |
| 2780 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 2781 | } |
| 2782 | |
| 2783 | bool devlink_is_reload_failed(const struct devlink *devlink) |
| 2784 | { |
| 2785 | return devlink->reload_failed; |
| 2786 | } |
| 2787 | EXPORT_SYMBOL_GPL(devlink_is_reload_failed); |
| 2788 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2789 | static int devlink_reload(struct devlink *devlink, struct net *dest_net, |
| 2790 | struct netlink_ext_ack *extack) |
| 2791 | { |
| 2792 | int err; |
| 2793 | |
| 2794 | err = devlink->ops->reload_down(devlink, !!dest_net, extack); |
| 2795 | if (err) |
| 2796 | return err; |
| 2797 | |
| 2798 | if (dest_net && !net_eq(dest_net, devlink_net(devlink))) |
| 2799 | devlink_reload_netns_change(devlink, dest_net); |
| 2800 | |
| 2801 | err = devlink->ops->reload_up(devlink, extack); |
| 2802 | devlink_reload_failed_set(devlink, !!err); |
| 2803 | return err; |
| 2804 | } |
| 2805 | |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2806 | static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info) |
| 2807 | { |
| 2808 | struct devlink *devlink = info->user_ptr[0]; |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2809 | struct net *dest_net = NULL; |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2810 | int err; |
| 2811 | |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 2812 | if (!devlink_reload_supported(devlink)) |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2813 | return -EOPNOTSUPP; |
| 2814 | |
| 2815 | err = devlink_resources_validate(devlink, NULL, info); |
| 2816 | if (err) { |
| 2817 | NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed"); |
| 2818 | return err; |
| 2819 | } |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 2820 | |
| 2821 | if (info->attrs[DEVLINK_ATTR_NETNS_PID] || |
| 2822 | info->attrs[DEVLINK_ATTR_NETNS_FD] || |
| 2823 | info->attrs[DEVLINK_ATTR_NETNS_ID]) { |
| 2824 | dest_net = devlink_netns_get(skb, info); |
| 2825 | if (IS_ERR(dest_net)) |
| 2826 | return PTR_ERR(dest_net); |
| 2827 | } |
| 2828 | |
| 2829 | err = devlink_reload(devlink, dest_net, info->extack); |
| 2830 | |
| 2831 | if (dest_net) |
| 2832 | put_net(dest_net); |
| 2833 | |
Jiri Pirko | 2670ac2 | 2019-09-12 10:49:46 +0200 | [diff] [blame] | 2834 | return err; |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2835 | } |
| 2836 | |
Jiri Pirko | 191ed20 | 2019-06-04 15:40:40 +0200 | [diff] [blame] | 2837 | static int devlink_nl_flash_update_fill(struct sk_buff *msg, |
| 2838 | struct devlink *devlink, |
| 2839 | enum devlink_command cmd, |
| 2840 | const char *status_msg, |
| 2841 | const char *component, |
| 2842 | unsigned long done, unsigned long total) |
| 2843 | { |
| 2844 | void *hdr; |
| 2845 | |
| 2846 | hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd); |
| 2847 | if (!hdr) |
| 2848 | return -EMSGSIZE; |
| 2849 | |
| 2850 | if (devlink_nl_put_handle(msg, devlink)) |
| 2851 | goto nla_put_failure; |
| 2852 | |
| 2853 | if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS) |
| 2854 | goto out; |
| 2855 | |
| 2856 | if (status_msg && |
| 2857 | nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG, |
| 2858 | status_msg)) |
| 2859 | goto nla_put_failure; |
| 2860 | if (component && |
| 2861 | nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT, |
| 2862 | component)) |
| 2863 | goto nla_put_failure; |
| 2864 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE, |
| 2865 | done, DEVLINK_ATTR_PAD)) |
| 2866 | goto nla_put_failure; |
| 2867 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL, |
| 2868 | total, DEVLINK_ATTR_PAD)) |
| 2869 | goto nla_put_failure; |
| 2870 | |
| 2871 | out: |
| 2872 | genlmsg_end(msg, hdr); |
| 2873 | return 0; |
| 2874 | |
| 2875 | nla_put_failure: |
| 2876 | genlmsg_cancel(msg, hdr); |
| 2877 | return -EMSGSIZE; |
| 2878 | } |
| 2879 | |
| 2880 | static void __devlink_flash_update_notify(struct devlink *devlink, |
| 2881 | enum devlink_command cmd, |
| 2882 | const char *status_msg, |
| 2883 | const char *component, |
| 2884 | unsigned long done, |
| 2885 | unsigned long total) |
| 2886 | { |
| 2887 | struct sk_buff *msg; |
| 2888 | int err; |
| 2889 | |
| 2890 | WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE && |
| 2891 | cmd != DEVLINK_CMD_FLASH_UPDATE_END && |
| 2892 | cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); |
| 2893 | |
| 2894 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 2895 | if (!msg) |
| 2896 | return; |
| 2897 | |
| 2898 | err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg, |
| 2899 | component, done, total); |
| 2900 | if (err) |
| 2901 | goto out_free_msg; |
| 2902 | |
| 2903 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 2904 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 2905 | return; |
| 2906 | |
| 2907 | out_free_msg: |
| 2908 | nlmsg_free(msg); |
| 2909 | } |
| 2910 | |
| 2911 | void devlink_flash_update_begin_notify(struct devlink *devlink) |
| 2912 | { |
| 2913 | __devlink_flash_update_notify(devlink, |
| 2914 | DEVLINK_CMD_FLASH_UPDATE, |
| 2915 | NULL, NULL, 0, 0); |
| 2916 | } |
| 2917 | EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify); |
| 2918 | |
| 2919 | void devlink_flash_update_end_notify(struct devlink *devlink) |
| 2920 | { |
| 2921 | __devlink_flash_update_notify(devlink, |
| 2922 | DEVLINK_CMD_FLASH_UPDATE_END, |
| 2923 | NULL, NULL, 0, 0); |
| 2924 | } |
| 2925 | EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify); |
| 2926 | |
| 2927 | void devlink_flash_update_status_notify(struct devlink *devlink, |
| 2928 | const char *status_msg, |
| 2929 | const char *component, |
| 2930 | unsigned long done, |
| 2931 | unsigned long total) |
| 2932 | { |
| 2933 | __devlink_flash_update_notify(devlink, |
| 2934 | DEVLINK_CMD_FLASH_UPDATE_STATUS, |
| 2935 | status_msg, component, done, total); |
| 2936 | } |
| 2937 | EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify); |
| 2938 | |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 2939 | static int devlink_nl_cmd_flash_update(struct sk_buff *skb, |
| 2940 | struct genl_info *info) |
| 2941 | { |
| 2942 | struct devlink *devlink = info->user_ptr[0]; |
| 2943 | const char *file_name, *component; |
| 2944 | struct nlattr *nla_component; |
| 2945 | |
| 2946 | if (!devlink->ops->flash_update) |
| 2947 | return -EOPNOTSUPP; |
| 2948 | |
| 2949 | if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]) |
| 2950 | return -EINVAL; |
| 2951 | file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]); |
| 2952 | |
| 2953 | nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]; |
| 2954 | component = nla_component ? nla_data(nla_component) : NULL; |
| 2955 | |
| 2956 | return devlink->ops->flash_update(devlink, file_name, component, |
| 2957 | info->extack); |
| 2958 | } |
| 2959 | |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame] | 2960 | static const struct devlink_param devlink_param_generic[] = { |
| 2961 | { |
| 2962 | .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET, |
| 2963 | .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME, |
| 2964 | .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE, |
| 2965 | }, |
| 2966 | { |
| 2967 | .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS, |
| 2968 | .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME, |
| 2969 | .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE, |
| 2970 | }, |
Vasundhara Volam | f567bcd | 2018-07-04 14:30:36 +0300 | [diff] [blame] | 2971 | { |
| 2972 | .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, |
| 2973 | .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME, |
| 2974 | .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE, |
| 2975 | }, |
Alex Vesker | f6a69885 | 2018-07-12 15:13:17 +0300 | [diff] [blame] | 2976 | { |
| 2977 | .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT, |
| 2978 | .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME, |
| 2979 | .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE, |
| 2980 | }, |
Vasundhara Volam | e3b5106 | 2018-10-04 11:13:44 +0530 | [diff] [blame] | 2981 | { |
| 2982 | .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI, |
| 2983 | .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME, |
| 2984 | .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE, |
| 2985 | }, |
Vasundhara Volam | f61cba4 | 2018-10-04 11:13:45 +0530 | [diff] [blame] | 2986 | { |
| 2987 | .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX, |
| 2988 | .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME, |
| 2989 | .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE, |
| 2990 | }, |
Vasundhara Volam | 1651178 | 2018-10-04 11:13:46 +0530 | [diff] [blame] | 2991 | { |
| 2992 | .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN, |
| 2993 | .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME, |
| 2994 | .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE, |
| 2995 | }, |
Shalom Toledo | 846e980 | 2018-12-03 07:58:59 +0000 | [diff] [blame] | 2996 | { |
| 2997 | .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY, |
| 2998 | .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME, |
| 2999 | .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE, |
| 3000 | }, |
Dirk van der Merwe | 5bbd21d | 2019-09-09 00:54:18 +0100 | [diff] [blame] | 3001 | { |
| 3002 | .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE, |
| 3003 | .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME, |
| 3004 | .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE, |
| 3005 | }, |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame] | 3006 | }; |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3007 | |
| 3008 | static int devlink_param_generic_verify(const struct devlink_param *param) |
| 3009 | { |
| 3010 | /* verify it match generic parameter by id and name */ |
| 3011 | if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX) |
| 3012 | return -EINVAL; |
| 3013 | if (strcmp(param->name, devlink_param_generic[param->id].name)) |
| 3014 | return -ENOENT; |
| 3015 | |
| 3016 | WARN_ON(param->type != devlink_param_generic[param->id].type); |
| 3017 | |
| 3018 | return 0; |
| 3019 | } |
| 3020 | |
| 3021 | static int devlink_param_driver_verify(const struct devlink_param *param) |
| 3022 | { |
| 3023 | int i; |
| 3024 | |
| 3025 | if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX) |
| 3026 | return -EINVAL; |
| 3027 | /* verify no such name in generic params */ |
| 3028 | for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++) |
| 3029 | if (!strcmp(param->name, devlink_param_generic[i].name)) |
| 3030 | return -EEXIST; |
| 3031 | |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
| 3035 | static struct devlink_param_item * |
| 3036 | devlink_param_find_by_name(struct list_head *param_list, |
| 3037 | const char *param_name) |
| 3038 | { |
| 3039 | struct devlink_param_item *param_item; |
| 3040 | |
| 3041 | list_for_each_entry(param_item, param_list, list) |
| 3042 | if (!strcmp(param_item->param->name, param_name)) |
| 3043 | return param_item; |
| 3044 | return NULL; |
| 3045 | } |
| 3046 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 3047 | static struct devlink_param_item * |
| 3048 | devlink_param_find_by_id(struct list_head *param_list, u32 param_id) |
| 3049 | { |
| 3050 | struct devlink_param_item *param_item; |
| 3051 | |
| 3052 | list_for_each_entry(param_item, param_list, list) |
| 3053 | if (param_item->param->id == param_id) |
| 3054 | return param_item; |
| 3055 | return NULL; |
| 3056 | } |
| 3057 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3058 | static bool |
| 3059 | devlink_param_cmode_is_supported(const struct devlink_param *param, |
| 3060 | enum devlink_param_cmode cmode) |
| 3061 | { |
| 3062 | return test_bit(cmode, ¶m->supported_cmodes); |
| 3063 | } |
| 3064 | |
| 3065 | static int devlink_param_get(struct devlink *devlink, |
| 3066 | const struct devlink_param *param, |
| 3067 | struct devlink_param_gset_ctx *ctx) |
| 3068 | { |
| 3069 | if (!param->get) |
| 3070 | return -EOPNOTSUPP; |
| 3071 | return param->get(devlink, param->id, ctx); |
| 3072 | } |
| 3073 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3074 | static int devlink_param_set(struct devlink *devlink, |
| 3075 | const struct devlink_param *param, |
| 3076 | struct devlink_param_gset_ctx *ctx) |
| 3077 | { |
| 3078 | if (!param->set) |
| 3079 | return -EOPNOTSUPP; |
| 3080 | return param->set(devlink, param->id, ctx); |
| 3081 | } |
| 3082 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3083 | static int |
| 3084 | devlink_param_type_to_nla_type(enum devlink_param_type param_type) |
| 3085 | { |
| 3086 | switch (param_type) { |
| 3087 | case DEVLINK_PARAM_TYPE_U8: |
| 3088 | return NLA_U8; |
| 3089 | case DEVLINK_PARAM_TYPE_U16: |
| 3090 | return NLA_U16; |
| 3091 | case DEVLINK_PARAM_TYPE_U32: |
| 3092 | return NLA_U32; |
| 3093 | case DEVLINK_PARAM_TYPE_STRING: |
| 3094 | return NLA_STRING; |
| 3095 | case DEVLINK_PARAM_TYPE_BOOL: |
| 3096 | return NLA_FLAG; |
| 3097 | default: |
| 3098 | return -EINVAL; |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | static int |
| 3103 | devlink_nl_param_value_fill_one(struct sk_buff *msg, |
| 3104 | enum devlink_param_type type, |
| 3105 | enum devlink_param_cmode cmode, |
| 3106 | union devlink_param_value val) |
| 3107 | { |
| 3108 | struct nlattr *param_value_attr; |
| 3109 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3110 | param_value_attr = nla_nest_start_noflag(msg, |
| 3111 | DEVLINK_ATTR_PARAM_VALUE); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3112 | if (!param_value_attr) |
| 3113 | goto nla_put_failure; |
| 3114 | |
| 3115 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode)) |
| 3116 | goto value_nest_cancel; |
| 3117 | |
| 3118 | switch (type) { |
| 3119 | case DEVLINK_PARAM_TYPE_U8: |
| 3120 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8)) |
| 3121 | goto value_nest_cancel; |
| 3122 | break; |
| 3123 | case DEVLINK_PARAM_TYPE_U16: |
| 3124 | if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16)) |
| 3125 | goto value_nest_cancel; |
| 3126 | break; |
| 3127 | case DEVLINK_PARAM_TYPE_U32: |
| 3128 | if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32)) |
| 3129 | goto value_nest_cancel; |
| 3130 | break; |
| 3131 | case DEVLINK_PARAM_TYPE_STRING: |
| 3132 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, |
| 3133 | val.vstr)) |
| 3134 | goto value_nest_cancel; |
| 3135 | break; |
| 3136 | case DEVLINK_PARAM_TYPE_BOOL: |
| 3137 | if (val.vbool && |
| 3138 | nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA)) |
| 3139 | goto value_nest_cancel; |
| 3140 | break; |
| 3141 | } |
| 3142 | |
| 3143 | nla_nest_end(msg, param_value_attr); |
| 3144 | return 0; |
| 3145 | |
| 3146 | value_nest_cancel: |
| 3147 | nla_nest_cancel(msg, param_value_attr); |
| 3148 | nla_put_failure: |
| 3149 | return -EMSGSIZE; |
| 3150 | } |
| 3151 | |
| 3152 | 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] | 3153 | unsigned int port_index, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3154 | struct devlink_param_item *param_item, |
| 3155 | enum devlink_command cmd, |
| 3156 | u32 portid, u32 seq, int flags) |
| 3157 | { |
| 3158 | union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1]; |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3159 | bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {}; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3160 | const struct devlink_param *param = param_item->param; |
| 3161 | struct devlink_param_gset_ctx ctx; |
| 3162 | struct nlattr *param_values_list; |
| 3163 | struct nlattr *param_attr; |
| 3164 | int nla_type; |
| 3165 | void *hdr; |
| 3166 | int err; |
| 3167 | int i; |
| 3168 | |
| 3169 | /* Get value from driver part to driverinit configuration mode */ |
| 3170 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
| 3171 | if (!devlink_param_cmode_is_supported(param, i)) |
| 3172 | continue; |
| 3173 | if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
| 3174 | if (!param_item->driverinit_value_valid) |
| 3175 | return -EOPNOTSUPP; |
| 3176 | param_value[i] = param_item->driverinit_value; |
| 3177 | } else { |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3178 | if (!param_item->published) |
| 3179 | continue; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3180 | ctx.cmode = i; |
| 3181 | err = devlink_param_get(devlink, param, &ctx); |
| 3182 | if (err) |
| 3183 | return err; |
| 3184 | param_value[i] = ctx.val; |
| 3185 | } |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3186 | param_value_set[i] = true; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3190 | if (!hdr) |
| 3191 | return -EMSGSIZE; |
| 3192 | |
| 3193 | if (devlink_nl_put_handle(msg, devlink)) |
| 3194 | goto genlmsg_cancel; |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3195 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3196 | if (cmd == DEVLINK_CMD_PORT_PARAM_GET || |
| 3197 | cmd == DEVLINK_CMD_PORT_PARAM_NEW || |
| 3198 | cmd == DEVLINK_CMD_PORT_PARAM_DEL) |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3199 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index)) |
| 3200 | goto genlmsg_cancel; |
| 3201 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3202 | param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3203 | if (!param_attr) |
| 3204 | goto genlmsg_cancel; |
| 3205 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name)) |
| 3206 | goto param_nest_cancel; |
| 3207 | if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC)) |
| 3208 | goto param_nest_cancel; |
| 3209 | |
| 3210 | nla_type = devlink_param_type_to_nla_type(param->type); |
| 3211 | if (nla_type < 0) |
| 3212 | goto param_nest_cancel; |
| 3213 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type)) |
| 3214 | goto param_nest_cancel; |
| 3215 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3216 | param_values_list = nla_nest_start_noflag(msg, |
| 3217 | DEVLINK_ATTR_PARAM_VALUES_LIST); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3218 | if (!param_values_list) |
| 3219 | goto param_nest_cancel; |
| 3220 | |
| 3221 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 3222 | if (!param_value_set[i]) |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3223 | continue; |
| 3224 | err = devlink_nl_param_value_fill_one(msg, param->type, |
| 3225 | i, param_value[i]); |
| 3226 | if (err) |
| 3227 | goto values_list_nest_cancel; |
| 3228 | } |
| 3229 | |
| 3230 | nla_nest_end(msg, param_values_list); |
| 3231 | nla_nest_end(msg, param_attr); |
| 3232 | genlmsg_end(msg, hdr); |
| 3233 | return 0; |
| 3234 | |
| 3235 | values_list_nest_cancel: |
| 3236 | nla_nest_end(msg, param_values_list); |
| 3237 | param_nest_cancel: |
| 3238 | nla_nest_cancel(msg, param_attr); |
| 3239 | genlmsg_cancel: |
| 3240 | genlmsg_cancel(msg, hdr); |
| 3241 | return -EMSGSIZE; |
| 3242 | } |
| 3243 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3244 | static void devlink_param_notify(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3245 | unsigned int port_index, |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3246 | struct devlink_param_item *param_item, |
| 3247 | enum devlink_command cmd) |
| 3248 | { |
| 3249 | struct sk_buff *msg; |
| 3250 | int err; |
| 3251 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3252 | WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL && |
| 3253 | cmd != DEVLINK_CMD_PORT_PARAM_NEW && |
| 3254 | cmd != DEVLINK_CMD_PORT_PARAM_DEL); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3255 | |
| 3256 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3257 | if (!msg) |
| 3258 | return; |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3259 | err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd, |
| 3260 | 0, 0, 0); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3261 | if (err) { |
| 3262 | nlmsg_free(msg); |
| 3263 | return; |
| 3264 | } |
| 3265 | |
| 3266 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3267 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 3268 | } |
| 3269 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3270 | static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg, |
| 3271 | struct netlink_callback *cb) |
| 3272 | { |
| 3273 | struct devlink_param_item *param_item; |
| 3274 | struct devlink *devlink; |
| 3275 | int start = cb->args[0]; |
| 3276 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3277 | int err = 0; |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3278 | |
| 3279 | mutex_lock(&devlink_mutex); |
| 3280 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3281 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3282 | continue; |
| 3283 | mutex_lock(&devlink->lock); |
| 3284 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 3285 | if (idx < start) { |
| 3286 | idx++; |
| 3287 | continue; |
| 3288 | } |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3289 | err = devlink_nl_param_fill(msg, devlink, 0, param_item, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3290 | DEVLINK_CMD_PARAM_GET, |
| 3291 | NETLINK_CB(cb->skb).portid, |
| 3292 | cb->nlh->nlmsg_seq, |
| 3293 | NLM_F_MULTI); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 3294 | if (err && err != -EOPNOTSUPP) { |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3295 | mutex_unlock(&devlink->lock); |
| 3296 | goto out; |
| 3297 | } |
| 3298 | idx++; |
| 3299 | } |
| 3300 | mutex_unlock(&devlink->lock); |
| 3301 | } |
| 3302 | out: |
| 3303 | mutex_unlock(&devlink_mutex); |
| 3304 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3305 | if (err != -EMSGSIZE) |
| 3306 | return err; |
| 3307 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3308 | cb->args[0] = idx; |
| 3309 | return msg->len; |
| 3310 | } |
| 3311 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3312 | static int |
| 3313 | devlink_param_type_get_from_info(struct genl_info *info, |
| 3314 | enum devlink_param_type *param_type) |
| 3315 | { |
| 3316 | if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE]) |
| 3317 | return -EINVAL; |
| 3318 | |
| 3319 | switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) { |
| 3320 | case NLA_U8: |
| 3321 | *param_type = DEVLINK_PARAM_TYPE_U8; |
| 3322 | break; |
| 3323 | case NLA_U16: |
| 3324 | *param_type = DEVLINK_PARAM_TYPE_U16; |
| 3325 | break; |
| 3326 | case NLA_U32: |
| 3327 | *param_type = DEVLINK_PARAM_TYPE_U32; |
| 3328 | break; |
| 3329 | case NLA_STRING: |
| 3330 | *param_type = DEVLINK_PARAM_TYPE_STRING; |
| 3331 | break; |
| 3332 | case NLA_FLAG: |
| 3333 | *param_type = DEVLINK_PARAM_TYPE_BOOL; |
| 3334 | break; |
| 3335 | default: |
| 3336 | return -EINVAL; |
| 3337 | } |
| 3338 | |
| 3339 | return 0; |
| 3340 | } |
| 3341 | |
| 3342 | static int |
| 3343 | devlink_param_value_get_from_info(const struct devlink_param *param, |
| 3344 | struct genl_info *info, |
| 3345 | union devlink_param_value *value) |
| 3346 | { |
Moshe Shemesh | f355cfc | 2018-10-10 16:09:25 +0300 | [diff] [blame] | 3347 | int len; |
| 3348 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3349 | if (param->type != DEVLINK_PARAM_TYPE_BOOL && |
| 3350 | !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) |
| 3351 | return -EINVAL; |
| 3352 | |
| 3353 | switch (param->type) { |
| 3354 | case DEVLINK_PARAM_TYPE_U8: |
| 3355 | value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 3356 | break; |
| 3357 | case DEVLINK_PARAM_TYPE_U16: |
| 3358 | value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 3359 | break; |
| 3360 | case DEVLINK_PARAM_TYPE_U32: |
| 3361 | value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 3362 | break; |
| 3363 | case DEVLINK_PARAM_TYPE_STRING: |
Moshe Shemesh | f355cfc | 2018-10-10 16:09:25 +0300 | [diff] [blame] | 3364 | len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]), |
| 3365 | nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])); |
| 3366 | if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) || |
Moshe Shemesh | bde74ad1 | 2018-10-10 16:09:27 +0300 | [diff] [blame] | 3367 | len >= __DEVLINK_PARAM_MAX_STRING_VALUE) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3368 | return -EINVAL; |
Moshe Shemesh | f355cfc | 2018-10-10 16:09:25 +0300 | [diff] [blame] | 3369 | strcpy(value->vstr, |
| 3370 | nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3371 | break; |
| 3372 | case DEVLINK_PARAM_TYPE_BOOL: |
| 3373 | value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ? |
| 3374 | true : false; |
| 3375 | break; |
| 3376 | } |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3380 | static struct devlink_param_item * |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3381 | devlink_param_get_from_info(struct list_head *param_list, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3382 | struct genl_info *info) |
| 3383 | { |
| 3384 | char *param_name; |
| 3385 | |
| 3386 | if (!info->attrs[DEVLINK_ATTR_PARAM_NAME]) |
| 3387 | return NULL; |
| 3388 | |
| 3389 | param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]); |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3390 | return devlink_param_find_by_name(param_list, param_name); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb, |
| 3394 | struct genl_info *info) |
| 3395 | { |
| 3396 | struct devlink *devlink = info->user_ptr[0]; |
| 3397 | struct devlink_param_item *param_item; |
| 3398 | struct sk_buff *msg; |
| 3399 | int err; |
| 3400 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3401 | param_item = devlink_param_get_from_info(&devlink->param_list, info); |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3402 | if (!param_item) |
| 3403 | return -EINVAL; |
| 3404 | |
| 3405 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3406 | if (!msg) |
| 3407 | return -ENOMEM; |
| 3408 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3409 | err = devlink_nl_param_fill(msg, devlink, 0, param_item, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3410 | DEVLINK_CMD_PARAM_GET, |
| 3411 | info->snd_portid, info->snd_seq, 0); |
| 3412 | if (err) { |
| 3413 | nlmsg_free(msg); |
| 3414 | return err; |
| 3415 | } |
| 3416 | |
| 3417 | return genlmsg_reply(msg, info); |
| 3418 | } |
| 3419 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3420 | static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3421 | unsigned int port_index, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3422 | struct list_head *param_list, |
| 3423 | struct genl_info *info, |
| 3424 | enum devlink_command cmd) |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3425 | { |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3426 | enum devlink_param_type param_type; |
| 3427 | struct devlink_param_gset_ctx ctx; |
| 3428 | enum devlink_param_cmode cmode; |
| 3429 | struct devlink_param_item *param_item; |
| 3430 | const struct devlink_param *param; |
| 3431 | union devlink_param_value value; |
| 3432 | int err = 0; |
| 3433 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3434 | param_item = devlink_param_get_from_info(param_list, info); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3435 | if (!param_item) |
| 3436 | return -EINVAL; |
| 3437 | param = param_item->param; |
| 3438 | err = devlink_param_type_get_from_info(info, ¶m_type); |
| 3439 | if (err) |
| 3440 | return err; |
| 3441 | if (param_type != param->type) |
| 3442 | return -EINVAL; |
| 3443 | err = devlink_param_value_get_from_info(param, info, &value); |
| 3444 | if (err) |
| 3445 | return err; |
| 3446 | if (param->validate) { |
| 3447 | err = param->validate(devlink, param->id, value, info->extack); |
| 3448 | if (err) |
| 3449 | return err; |
| 3450 | } |
| 3451 | |
| 3452 | if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]) |
| 3453 | return -EINVAL; |
| 3454 | cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]); |
| 3455 | if (!devlink_param_cmode_is_supported(param, cmode)) |
| 3456 | return -EOPNOTSUPP; |
| 3457 | |
| 3458 | if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
Moshe Shemesh | 1276534 | 2018-10-10 16:09:26 +0300 | [diff] [blame] | 3459 | if (param->type == DEVLINK_PARAM_TYPE_STRING) |
| 3460 | strcpy(param_item->driverinit_value.vstr, value.vstr); |
| 3461 | else |
| 3462 | param_item->driverinit_value = value; |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3463 | param_item->driverinit_value_valid = true; |
| 3464 | } else { |
| 3465 | if (!param->set) |
| 3466 | return -EOPNOTSUPP; |
| 3467 | ctx.val = value; |
| 3468 | ctx.cmode = cmode; |
| 3469 | err = devlink_param_set(devlink, param, &ctx); |
| 3470 | if (err) |
| 3471 | return err; |
| 3472 | } |
| 3473 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3474 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3475 | return 0; |
| 3476 | } |
| 3477 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3478 | static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb, |
| 3479 | struct genl_info *info) |
| 3480 | { |
| 3481 | struct devlink *devlink = info->user_ptr[0]; |
| 3482 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3483 | return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3484 | info, DEVLINK_CMD_PARAM_NEW); |
| 3485 | } |
| 3486 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3487 | static int devlink_param_register_one(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3488 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3489 | struct list_head *param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3490 | const struct devlink_param *param, |
| 3491 | enum devlink_command cmd) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3492 | { |
| 3493 | struct devlink_param_item *param_item; |
| 3494 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3495 | if (devlink_param_find_by_name(param_list, param->name)) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3496 | return -EEXIST; |
| 3497 | |
| 3498 | if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 3499 | WARN_ON(param->get || param->set); |
| 3500 | else |
| 3501 | WARN_ON(!param->get || !param->set); |
| 3502 | |
| 3503 | param_item = kzalloc(sizeof(*param_item), GFP_KERNEL); |
| 3504 | if (!param_item) |
| 3505 | return -ENOMEM; |
| 3506 | param_item->param = param; |
| 3507 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3508 | list_add_tail(¶m_item->list, param_list); |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3509 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3510 | return 0; |
| 3511 | } |
| 3512 | |
| 3513 | static void devlink_param_unregister_one(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3514 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3515 | struct list_head *param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3516 | const struct devlink_param *param, |
| 3517 | enum devlink_command cmd) |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3518 | { |
| 3519 | struct devlink_param_item *param_item; |
| 3520 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 3521 | param_item = devlink_param_find_by_name(param_list, param->name); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3522 | WARN_ON(!param_item); |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3523 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3524 | list_del(¶m_item->list); |
| 3525 | kfree(param_item); |
| 3526 | } |
| 3527 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3528 | static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg, |
| 3529 | struct netlink_callback *cb) |
| 3530 | { |
| 3531 | struct devlink_param_item *param_item; |
| 3532 | struct devlink_port *devlink_port; |
| 3533 | struct devlink *devlink; |
| 3534 | int start = cb->args[0]; |
| 3535 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3536 | int err = 0; |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3537 | |
| 3538 | mutex_lock(&devlink_mutex); |
| 3539 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3540 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3541 | continue; |
| 3542 | mutex_lock(&devlink->lock); |
| 3543 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 3544 | list_for_each_entry(param_item, |
| 3545 | &devlink_port->param_list, list) { |
| 3546 | if (idx < start) { |
| 3547 | idx++; |
| 3548 | continue; |
| 3549 | } |
| 3550 | err = devlink_nl_param_fill(msg, |
| 3551 | devlink_port->devlink, |
| 3552 | devlink_port->index, param_item, |
| 3553 | DEVLINK_CMD_PORT_PARAM_GET, |
| 3554 | NETLINK_CB(cb->skb).portid, |
| 3555 | cb->nlh->nlmsg_seq, |
| 3556 | NLM_F_MULTI); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 3557 | if (err && err != -EOPNOTSUPP) { |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3558 | mutex_unlock(&devlink->lock); |
| 3559 | goto out; |
| 3560 | } |
| 3561 | idx++; |
| 3562 | } |
| 3563 | } |
| 3564 | mutex_unlock(&devlink->lock); |
| 3565 | } |
| 3566 | out: |
| 3567 | mutex_unlock(&devlink_mutex); |
| 3568 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 3569 | if (err != -EMSGSIZE) |
| 3570 | return err; |
| 3571 | |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 3572 | cb->args[0] = idx; |
| 3573 | return msg->len; |
| 3574 | } |
| 3575 | |
| 3576 | static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb, |
| 3577 | struct genl_info *info) |
| 3578 | { |
| 3579 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 3580 | struct devlink_param_item *param_item; |
| 3581 | struct sk_buff *msg; |
| 3582 | int err; |
| 3583 | |
| 3584 | param_item = devlink_param_get_from_info(&devlink_port->param_list, |
| 3585 | info); |
| 3586 | if (!param_item) |
| 3587 | return -EINVAL; |
| 3588 | |
| 3589 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3590 | if (!msg) |
| 3591 | return -ENOMEM; |
| 3592 | |
| 3593 | err = devlink_nl_param_fill(msg, devlink_port->devlink, |
| 3594 | devlink_port->index, param_item, |
| 3595 | DEVLINK_CMD_PORT_PARAM_GET, |
| 3596 | info->snd_portid, info->snd_seq, 0); |
| 3597 | if (err) { |
| 3598 | nlmsg_free(msg); |
| 3599 | return err; |
| 3600 | } |
| 3601 | |
| 3602 | return genlmsg_reply(msg, info); |
| 3603 | } |
| 3604 | |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3605 | static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb, |
| 3606 | struct genl_info *info) |
| 3607 | { |
| 3608 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 3609 | |
| 3610 | return __devlink_nl_cmd_param_set_doit(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 3611 | devlink_port->index, |
| 3612 | &devlink_port->param_list, info, |
| 3613 | DEVLINK_CMD_PORT_PARAM_NEW); |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 3614 | } |
| 3615 | |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3616 | static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg, |
| 3617 | struct devlink *devlink, |
| 3618 | struct devlink_snapshot *snapshot) |
| 3619 | { |
| 3620 | struct nlattr *snap_attr; |
| 3621 | int err; |
| 3622 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3623 | snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT); |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3624 | if (!snap_attr) |
| 3625 | return -EINVAL; |
| 3626 | |
| 3627 | err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id); |
| 3628 | if (err) |
| 3629 | goto nla_put_failure; |
| 3630 | |
| 3631 | nla_nest_end(msg, snap_attr); |
| 3632 | return 0; |
| 3633 | |
| 3634 | nla_put_failure: |
| 3635 | nla_nest_cancel(msg, snap_attr); |
| 3636 | return err; |
| 3637 | } |
| 3638 | |
| 3639 | static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg, |
| 3640 | struct devlink *devlink, |
| 3641 | struct devlink_region *region) |
| 3642 | { |
| 3643 | struct devlink_snapshot *snapshot; |
| 3644 | struct nlattr *snapshots_attr; |
| 3645 | int err; |
| 3646 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3647 | snapshots_attr = nla_nest_start_noflag(msg, |
| 3648 | DEVLINK_ATTR_REGION_SNAPSHOTS); |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3649 | if (!snapshots_attr) |
| 3650 | return -EINVAL; |
| 3651 | |
| 3652 | list_for_each_entry(snapshot, ®ion->snapshot_list, list) { |
| 3653 | err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot); |
| 3654 | if (err) |
| 3655 | goto nla_put_failure; |
| 3656 | } |
| 3657 | |
| 3658 | nla_nest_end(msg, snapshots_attr); |
| 3659 | return 0; |
| 3660 | |
| 3661 | nla_put_failure: |
| 3662 | nla_nest_cancel(msg, snapshots_attr); |
| 3663 | return err; |
| 3664 | } |
| 3665 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3666 | static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink, |
| 3667 | enum devlink_command cmd, u32 portid, |
| 3668 | u32 seq, int flags, |
| 3669 | struct devlink_region *region) |
| 3670 | { |
| 3671 | void *hdr; |
| 3672 | int err; |
| 3673 | |
| 3674 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 3675 | if (!hdr) |
| 3676 | return -EMSGSIZE; |
| 3677 | |
| 3678 | err = devlink_nl_put_handle(msg, devlink); |
| 3679 | if (err) |
| 3680 | goto nla_put_failure; |
| 3681 | |
| 3682 | err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name); |
| 3683 | if (err) |
| 3684 | goto nla_put_failure; |
| 3685 | |
| 3686 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, |
| 3687 | region->size, |
| 3688 | DEVLINK_ATTR_PAD); |
| 3689 | if (err) |
| 3690 | goto nla_put_failure; |
| 3691 | |
Alex Vesker | a006d46 | 2018-07-12 15:13:12 +0300 | [diff] [blame] | 3692 | err = devlink_nl_region_snapshots_id_put(msg, devlink, region); |
| 3693 | if (err) |
| 3694 | goto nla_put_failure; |
| 3695 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3696 | genlmsg_end(msg, hdr); |
| 3697 | return 0; |
| 3698 | |
| 3699 | nla_put_failure: |
| 3700 | genlmsg_cancel(msg, hdr); |
| 3701 | return err; |
| 3702 | } |
| 3703 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3704 | static void devlink_nl_region_notify(struct devlink_region *region, |
| 3705 | struct devlink_snapshot *snapshot, |
| 3706 | enum devlink_command cmd) |
| 3707 | { |
| 3708 | struct devlink *devlink = region->devlink; |
| 3709 | struct sk_buff *msg; |
| 3710 | void *hdr; |
| 3711 | int err; |
| 3712 | |
| 3713 | WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); |
| 3714 | |
| 3715 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3716 | if (!msg) |
| 3717 | return; |
| 3718 | |
| 3719 | hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd); |
| 3720 | if (!hdr) |
| 3721 | goto out_free_msg; |
| 3722 | |
| 3723 | err = devlink_nl_put_handle(msg, devlink); |
| 3724 | if (err) |
| 3725 | goto out_cancel_msg; |
| 3726 | |
| 3727 | err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, |
| 3728 | region->name); |
| 3729 | if (err) |
| 3730 | goto out_cancel_msg; |
| 3731 | |
| 3732 | if (snapshot) { |
| 3733 | err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, |
| 3734 | snapshot->id); |
| 3735 | if (err) |
| 3736 | goto out_cancel_msg; |
| 3737 | } else { |
| 3738 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, |
| 3739 | region->size, DEVLINK_ATTR_PAD); |
| 3740 | if (err) |
| 3741 | goto out_cancel_msg; |
| 3742 | } |
| 3743 | genlmsg_end(msg, hdr); |
| 3744 | |
| 3745 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 3746 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 3747 | |
| 3748 | return; |
| 3749 | |
| 3750 | out_cancel_msg: |
| 3751 | genlmsg_cancel(msg, hdr); |
| 3752 | out_free_msg: |
| 3753 | nlmsg_free(msg); |
| 3754 | } |
| 3755 | |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 3756 | static void devlink_region_snapshot_del(struct devlink_region *region, |
| 3757 | struct devlink_snapshot *snapshot) |
| 3758 | { |
| 3759 | devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL); |
| 3760 | region->cur_snapshots--; |
| 3761 | list_del(&snapshot->list); |
| 3762 | (*snapshot->data_destructor)(snapshot->data); |
| 3763 | kfree(snapshot); |
| 3764 | } |
| 3765 | |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 3766 | static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb, |
| 3767 | struct genl_info *info) |
| 3768 | { |
| 3769 | struct devlink *devlink = info->user_ptr[0]; |
| 3770 | struct devlink_region *region; |
| 3771 | const char *region_name; |
| 3772 | struct sk_buff *msg; |
| 3773 | int err; |
| 3774 | |
| 3775 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) |
| 3776 | return -EINVAL; |
| 3777 | |
| 3778 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 3779 | region = devlink_region_get_by_name(devlink, region_name); |
| 3780 | if (!region) |
| 3781 | return -EINVAL; |
| 3782 | |
| 3783 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3784 | if (!msg) |
| 3785 | return -ENOMEM; |
| 3786 | |
| 3787 | err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET, |
| 3788 | info->snd_portid, info->snd_seq, 0, |
| 3789 | region); |
| 3790 | if (err) { |
| 3791 | nlmsg_free(msg); |
| 3792 | return err; |
| 3793 | } |
| 3794 | |
| 3795 | return genlmsg_reply(msg, info); |
| 3796 | } |
| 3797 | |
| 3798 | static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg, |
| 3799 | struct netlink_callback *cb) |
| 3800 | { |
| 3801 | struct devlink_region *region; |
| 3802 | struct devlink *devlink; |
| 3803 | int start = cb->args[0]; |
| 3804 | int idx = 0; |
| 3805 | int err; |
| 3806 | |
| 3807 | mutex_lock(&devlink_mutex); |
| 3808 | list_for_each_entry(devlink, &devlink_list, list) { |
| 3809 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 3810 | continue; |
| 3811 | |
| 3812 | mutex_lock(&devlink->lock); |
| 3813 | list_for_each_entry(region, &devlink->region_list, list) { |
| 3814 | if (idx < start) { |
| 3815 | idx++; |
| 3816 | continue; |
| 3817 | } |
| 3818 | err = devlink_nl_region_fill(msg, devlink, |
| 3819 | DEVLINK_CMD_REGION_GET, |
| 3820 | NETLINK_CB(cb->skb).portid, |
| 3821 | cb->nlh->nlmsg_seq, |
| 3822 | NLM_F_MULTI, region); |
| 3823 | if (err) { |
| 3824 | mutex_unlock(&devlink->lock); |
| 3825 | goto out; |
| 3826 | } |
| 3827 | idx++; |
| 3828 | } |
| 3829 | mutex_unlock(&devlink->lock); |
| 3830 | } |
| 3831 | out: |
| 3832 | mutex_unlock(&devlink_mutex); |
| 3833 | cb->args[0] = idx; |
| 3834 | return msg->len; |
| 3835 | } |
| 3836 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3837 | static int devlink_nl_cmd_region_del(struct sk_buff *skb, |
| 3838 | struct genl_info *info) |
| 3839 | { |
| 3840 | struct devlink *devlink = info->user_ptr[0]; |
| 3841 | struct devlink_snapshot *snapshot; |
| 3842 | struct devlink_region *region; |
| 3843 | const char *region_name; |
| 3844 | u32 snapshot_id; |
| 3845 | |
| 3846 | if (!info->attrs[DEVLINK_ATTR_REGION_NAME] || |
| 3847 | !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) |
| 3848 | return -EINVAL; |
| 3849 | |
| 3850 | region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); |
| 3851 | snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); |
| 3852 | |
| 3853 | region = devlink_region_get_by_name(devlink, region_name); |
| 3854 | if (!region) |
| 3855 | return -EINVAL; |
| 3856 | |
| 3857 | snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); |
| 3858 | if (!snapshot) |
| 3859 | return -EINVAL; |
| 3860 | |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 3861 | devlink_region_snapshot_del(region, snapshot); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 3862 | return 0; |
| 3863 | } |
| 3864 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3865 | static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg, |
| 3866 | struct devlink *devlink, |
| 3867 | u8 *chunk, u32 chunk_size, |
| 3868 | u64 addr) |
| 3869 | { |
| 3870 | struct nlattr *chunk_attr; |
| 3871 | int err; |
| 3872 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3873 | chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3874 | if (!chunk_attr) |
| 3875 | return -EINVAL; |
| 3876 | |
| 3877 | err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk); |
| 3878 | if (err) |
| 3879 | goto nla_put_failure; |
| 3880 | |
| 3881 | err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr, |
| 3882 | DEVLINK_ATTR_PAD); |
| 3883 | if (err) |
| 3884 | goto nla_put_failure; |
| 3885 | |
| 3886 | nla_nest_end(msg, chunk_attr); |
| 3887 | return 0; |
| 3888 | |
| 3889 | nla_put_failure: |
| 3890 | nla_nest_cancel(msg, chunk_attr); |
| 3891 | return err; |
| 3892 | } |
| 3893 | |
| 3894 | #define DEVLINK_REGION_READ_CHUNK_SIZE 256 |
| 3895 | |
| 3896 | static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb, |
| 3897 | struct devlink *devlink, |
| 3898 | struct devlink_region *region, |
| 3899 | struct nlattr **attrs, |
| 3900 | u64 start_offset, |
| 3901 | u64 end_offset, |
| 3902 | bool dump, |
| 3903 | u64 *new_offset) |
| 3904 | { |
| 3905 | struct devlink_snapshot *snapshot; |
| 3906 | u64 curr_offset = start_offset; |
| 3907 | u32 snapshot_id; |
| 3908 | int err = 0; |
| 3909 | |
| 3910 | *new_offset = start_offset; |
| 3911 | |
| 3912 | snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); |
| 3913 | snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); |
| 3914 | if (!snapshot) |
| 3915 | return -EINVAL; |
| 3916 | |
Jiri Pirko | 3a5e523 | 2019-08-09 15:27:15 +0200 | [diff] [blame] | 3917 | if (end_offset > region->size || dump) |
| 3918 | end_offset = region->size; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3919 | |
| 3920 | while (curr_offset < end_offset) { |
| 3921 | u32 data_size; |
| 3922 | u8 *data; |
| 3923 | |
| 3924 | if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE) |
| 3925 | data_size = end_offset - curr_offset; |
| 3926 | else |
| 3927 | data_size = DEVLINK_REGION_READ_CHUNK_SIZE; |
| 3928 | |
| 3929 | data = &snapshot->data[curr_offset]; |
| 3930 | err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink, |
| 3931 | data, data_size, |
| 3932 | curr_offset); |
| 3933 | if (err) |
| 3934 | break; |
| 3935 | |
| 3936 | curr_offset += data_size; |
| 3937 | } |
| 3938 | *new_offset = curr_offset; |
| 3939 | |
| 3940 | return err; |
| 3941 | } |
| 3942 | |
| 3943 | static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb, |
| 3944 | struct netlink_callback *cb) |
| 3945 | { |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 3946 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3947 | u64 ret_offset, start_offset, end_offset = 0; |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 3948 | struct nlattr **attrs = info->attrs; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3949 | struct devlink_region *region; |
| 3950 | struct nlattr *chunks_attr; |
| 3951 | const char *region_name; |
| 3952 | struct devlink *devlink; |
| 3953 | bool dump = true; |
| 3954 | void *hdr; |
| 3955 | int err; |
| 3956 | |
| 3957 | start_offset = *((u64 *)&cb->args[0]); |
| 3958 | |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 3959 | mutex_lock(&devlink_mutex); |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3960 | devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3961 | if (IS_ERR(devlink)) { |
| 3962 | err = PTR_ERR(devlink); |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 3963 | goto out_dev; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3964 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3965 | |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3966 | mutex_lock(&devlink->lock); |
| 3967 | |
| 3968 | if (!attrs[DEVLINK_ATTR_REGION_NAME] || |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3969 | !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) { |
| 3970 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3971 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3972 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3973 | |
| 3974 | region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]); |
| 3975 | region = devlink_region_get_by_name(devlink, region_name); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3976 | if (!region) { |
| 3977 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3978 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3979 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3980 | |
| 3981 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 3982 | &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, |
| 3983 | DEVLINK_CMD_REGION_READ); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3984 | if (!hdr) { |
| 3985 | err = -EMSGSIZE; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3986 | goto out_unlock; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3987 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 3988 | |
| 3989 | err = devlink_nl_put_handle(skb, devlink); |
| 3990 | if (err) |
| 3991 | goto nla_put_failure; |
| 3992 | |
| 3993 | err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name); |
| 3994 | if (err) |
| 3995 | goto nla_put_failure; |
| 3996 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3997 | chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 3998 | if (!chunks_attr) { |
| 3999 | err = -EMSGSIZE; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4000 | goto nla_put_failure; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4001 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4002 | |
| 4003 | if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] && |
| 4004 | attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) { |
| 4005 | if (!start_offset) |
| 4006 | start_offset = |
| 4007 | nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); |
| 4008 | |
| 4009 | end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); |
| 4010 | end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]); |
| 4011 | dump = false; |
| 4012 | } |
| 4013 | |
| 4014 | err = devlink_nl_region_read_snapshot_fill(skb, devlink, |
| 4015 | region, attrs, |
| 4016 | start_offset, |
| 4017 | end_offset, dump, |
| 4018 | &ret_offset); |
| 4019 | |
| 4020 | if (err && err != -EMSGSIZE) |
| 4021 | goto nla_put_failure; |
| 4022 | |
| 4023 | /* Check if there was any progress done to prevent infinite loop */ |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4024 | if (ret_offset == start_offset) { |
| 4025 | err = -EINVAL; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4026 | goto nla_put_failure; |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4027 | } |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4028 | |
| 4029 | *((u64 *)&cb->args[0]) = ret_offset; |
| 4030 | |
| 4031 | nla_nest_end(skb, chunks_attr); |
| 4032 | genlmsg_end(skb, hdr); |
| 4033 | mutex_unlock(&devlink->lock); |
| 4034 | mutex_unlock(&devlink_mutex); |
| 4035 | |
| 4036 | return skb->len; |
| 4037 | |
| 4038 | nla_put_failure: |
| 4039 | genlmsg_cancel(skb, hdr); |
| 4040 | out_unlock: |
| 4041 | mutex_unlock(&devlink->lock); |
Parav Pandit | dac7c08 | 2019-02-12 14:24:08 -0600 | [diff] [blame] | 4042 | out_dev: |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4043 | mutex_unlock(&devlink_mutex); |
Parav Pandit | fdd41ec | 2019-02-12 14:23:58 -0600 | [diff] [blame] | 4044 | return err; |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 4045 | } |
| 4046 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4047 | struct devlink_info_req { |
| 4048 | struct sk_buff *msg; |
| 4049 | }; |
| 4050 | |
| 4051 | int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name) |
| 4052 | { |
| 4053 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name); |
| 4054 | } |
| 4055 | EXPORT_SYMBOL_GPL(devlink_info_driver_name_put); |
| 4056 | |
| 4057 | int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn) |
| 4058 | { |
| 4059 | return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn); |
| 4060 | } |
| 4061 | EXPORT_SYMBOL_GPL(devlink_info_serial_number_put); |
| 4062 | |
Jakub Kicinski | fc6fae7 | 2019-01-31 10:50:41 -0800 | [diff] [blame] | 4063 | static int devlink_info_version_put(struct devlink_info_req *req, int attr, |
| 4064 | const char *version_name, |
| 4065 | const char *version_value) |
| 4066 | { |
| 4067 | struct nlattr *nest; |
| 4068 | int err; |
| 4069 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4070 | nest = nla_nest_start_noflag(req->msg, attr); |
Jakub Kicinski | fc6fae7 | 2019-01-31 10:50:41 -0800 | [diff] [blame] | 4071 | if (!nest) |
| 4072 | return -EMSGSIZE; |
| 4073 | |
| 4074 | err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME, |
| 4075 | version_name); |
| 4076 | if (err) |
| 4077 | goto nla_put_failure; |
| 4078 | |
| 4079 | err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE, |
| 4080 | version_value); |
| 4081 | if (err) |
| 4082 | goto nla_put_failure; |
| 4083 | |
| 4084 | nla_nest_end(req->msg, nest); |
| 4085 | |
| 4086 | return 0; |
| 4087 | |
| 4088 | nla_put_failure: |
| 4089 | nla_nest_cancel(req->msg, nest); |
| 4090 | return err; |
| 4091 | } |
| 4092 | |
| 4093 | int devlink_info_version_fixed_put(struct devlink_info_req *req, |
| 4094 | const char *version_name, |
| 4095 | const char *version_value) |
| 4096 | { |
| 4097 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED, |
| 4098 | version_name, version_value); |
| 4099 | } |
| 4100 | EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put); |
| 4101 | |
| 4102 | int devlink_info_version_stored_put(struct devlink_info_req *req, |
| 4103 | const char *version_name, |
| 4104 | const char *version_value) |
| 4105 | { |
| 4106 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED, |
| 4107 | version_name, version_value); |
| 4108 | } |
| 4109 | EXPORT_SYMBOL_GPL(devlink_info_version_stored_put); |
| 4110 | |
| 4111 | int devlink_info_version_running_put(struct devlink_info_req *req, |
| 4112 | const char *version_name, |
| 4113 | const char *version_value) |
| 4114 | { |
| 4115 | return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING, |
| 4116 | version_name, version_value); |
| 4117 | } |
| 4118 | EXPORT_SYMBOL_GPL(devlink_info_version_running_put); |
| 4119 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4120 | static int |
| 4121 | devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink, |
| 4122 | enum devlink_command cmd, u32 portid, |
| 4123 | u32 seq, int flags, struct netlink_ext_ack *extack) |
| 4124 | { |
| 4125 | struct devlink_info_req req; |
| 4126 | void *hdr; |
| 4127 | int err; |
| 4128 | |
| 4129 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 4130 | if (!hdr) |
| 4131 | return -EMSGSIZE; |
| 4132 | |
| 4133 | err = -EMSGSIZE; |
| 4134 | if (devlink_nl_put_handle(msg, devlink)) |
| 4135 | goto err_cancel_msg; |
| 4136 | |
| 4137 | req.msg = msg; |
| 4138 | err = devlink->ops->info_get(devlink, &req, extack); |
| 4139 | if (err) |
| 4140 | goto err_cancel_msg; |
| 4141 | |
| 4142 | genlmsg_end(msg, hdr); |
| 4143 | return 0; |
| 4144 | |
| 4145 | err_cancel_msg: |
| 4146 | genlmsg_cancel(msg, hdr); |
| 4147 | return err; |
| 4148 | } |
| 4149 | |
| 4150 | static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb, |
| 4151 | struct genl_info *info) |
| 4152 | { |
| 4153 | struct devlink *devlink = info->user_ptr[0]; |
| 4154 | struct sk_buff *msg; |
| 4155 | int err; |
| 4156 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 4157 | if (!devlink->ops->info_get) |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4158 | return -EOPNOTSUPP; |
| 4159 | |
| 4160 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4161 | if (!msg) |
| 4162 | return -ENOMEM; |
| 4163 | |
| 4164 | err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET, |
| 4165 | info->snd_portid, info->snd_seq, 0, |
| 4166 | info->extack); |
| 4167 | if (err) { |
| 4168 | nlmsg_free(msg); |
| 4169 | return err; |
| 4170 | } |
| 4171 | |
| 4172 | return genlmsg_reply(msg, info); |
| 4173 | } |
| 4174 | |
| 4175 | static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg, |
| 4176 | struct netlink_callback *cb) |
| 4177 | { |
| 4178 | struct devlink *devlink; |
| 4179 | int start = cb->args[0]; |
| 4180 | int idx = 0; |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 4181 | int err = 0; |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4182 | |
| 4183 | mutex_lock(&devlink_mutex); |
| 4184 | list_for_each_entry(devlink, &devlink_list, list) { |
| 4185 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 4186 | continue; |
| 4187 | if (idx < start) { |
| 4188 | idx++; |
| 4189 | continue; |
| 4190 | } |
| 4191 | |
Jiri Pirko | c493b09b | 2019-03-24 00:21:03 +0100 | [diff] [blame] | 4192 | if (!devlink->ops->info_get) { |
| 4193 | idx++; |
| 4194 | continue; |
| 4195 | } |
| 4196 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4197 | mutex_lock(&devlink->lock); |
| 4198 | err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET, |
| 4199 | NETLINK_CB(cb->skb).portid, |
| 4200 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 4201 | cb->extack); |
| 4202 | mutex_unlock(&devlink->lock); |
Vasundhara Volam | 93c2fcb | 2019-09-30 11:52:21 +0530 | [diff] [blame] | 4203 | if (err && err != -EOPNOTSUPP) |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4204 | break; |
| 4205 | idx++; |
| 4206 | } |
| 4207 | mutex_unlock(&devlink_mutex); |
| 4208 | |
Jiri Pirko | c62c2cf | 2019-10-04 11:50:12 +0200 | [diff] [blame] | 4209 | if (err != -EMSGSIZE) |
| 4210 | return err; |
| 4211 | |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 4212 | cb->args[0] = idx; |
| 4213 | return msg->len; |
| 4214 | } |
| 4215 | |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4216 | struct devlink_fmsg_item { |
| 4217 | struct list_head list; |
| 4218 | int attrtype; |
| 4219 | u8 nla_type; |
| 4220 | u16 len; |
| 4221 | int value[0]; |
| 4222 | }; |
| 4223 | |
| 4224 | struct devlink_fmsg { |
| 4225 | struct list_head item_list; |
| 4226 | }; |
| 4227 | |
| 4228 | static struct devlink_fmsg *devlink_fmsg_alloc(void) |
| 4229 | { |
| 4230 | struct devlink_fmsg *fmsg; |
| 4231 | |
| 4232 | fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL); |
| 4233 | if (!fmsg) |
| 4234 | return NULL; |
| 4235 | |
| 4236 | INIT_LIST_HEAD(&fmsg->item_list); |
| 4237 | |
| 4238 | return fmsg; |
| 4239 | } |
| 4240 | |
| 4241 | static void devlink_fmsg_free(struct devlink_fmsg *fmsg) |
| 4242 | { |
| 4243 | struct devlink_fmsg_item *item, *tmp; |
| 4244 | |
| 4245 | list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) { |
| 4246 | list_del(&item->list); |
| 4247 | kfree(item); |
| 4248 | } |
| 4249 | kfree(fmsg); |
| 4250 | } |
| 4251 | |
| 4252 | static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg, |
| 4253 | int attrtype) |
| 4254 | { |
| 4255 | struct devlink_fmsg_item *item; |
| 4256 | |
| 4257 | item = kzalloc(sizeof(*item), GFP_KERNEL); |
| 4258 | if (!item) |
| 4259 | return -ENOMEM; |
| 4260 | |
| 4261 | item->attrtype = attrtype; |
| 4262 | list_add_tail(&item->list, &fmsg->item_list); |
| 4263 | |
| 4264 | return 0; |
| 4265 | } |
| 4266 | |
| 4267 | int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg) |
| 4268 | { |
| 4269 | return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START); |
| 4270 | } |
| 4271 | EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start); |
| 4272 | |
| 4273 | static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg) |
| 4274 | { |
| 4275 | return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END); |
| 4276 | } |
| 4277 | |
| 4278 | int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg) |
| 4279 | { |
| 4280 | return devlink_fmsg_nest_end(fmsg); |
| 4281 | } |
| 4282 | EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end); |
| 4283 | |
| 4284 | #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN) |
| 4285 | |
| 4286 | static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name) |
| 4287 | { |
| 4288 | struct devlink_fmsg_item *item; |
| 4289 | |
| 4290 | if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE) |
| 4291 | return -EMSGSIZE; |
| 4292 | |
| 4293 | item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL); |
| 4294 | if (!item) |
| 4295 | return -ENOMEM; |
| 4296 | |
| 4297 | item->nla_type = NLA_NUL_STRING; |
| 4298 | item->len = strlen(name) + 1; |
| 4299 | item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME; |
| 4300 | memcpy(&item->value, name, item->len); |
| 4301 | list_add_tail(&item->list, &fmsg->item_list); |
| 4302 | |
| 4303 | return 0; |
| 4304 | } |
| 4305 | |
| 4306 | int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name) |
| 4307 | { |
| 4308 | int err; |
| 4309 | |
| 4310 | err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START); |
| 4311 | if (err) |
| 4312 | return err; |
| 4313 | |
| 4314 | err = devlink_fmsg_put_name(fmsg, name); |
| 4315 | if (err) |
| 4316 | return err; |
| 4317 | |
| 4318 | return 0; |
| 4319 | } |
| 4320 | EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start); |
| 4321 | |
| 4322 | int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4323 | { |
| 4324 | return devlink_fmsg_nest_end(fmsg); |
| 4325 | } |
| 4326 | EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end); |
| 4327 | |
| 4328 | int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg, |
| 4329 | const char *name) |
| 4330 | { |
| 4331 | int err; |
| 4332 | |
| 4333 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4334 | if (err) |
| 4335 | return err; |
| 4336 | |
| 4337 | err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START); |
| 4338 | if (err) |
| 4339 | return err; |
| 4340 | |
| 4341 | return 0; |
| 4342 | } |
| 4343 | EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start); |
| 4344 | |
| 4345 | int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg) |
| 4346 | { |
| 4347 | int err; |
| 4348 | |
| 4349 | err = devlink_fmsg_nest_end(fmsg); |
| 4350 | if (err) |
| 4351 | return err; |
| 4352 | |
| 4353 | err = devlink_fmsg_nest_end(fmsg); |
| 4354 | if (err) |
| 4355 | return err; |
| 4356 | |
| 4357 | return 0; |
| 4358 | } |
| 4359 | EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end); |
| 4360 | |
| 4361 | static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg, |
| 4362 | const void *value, u16 value_len, |
| 4363 | u8 value_nla_type) |
| 4364 | { |
| 4365 | struct devlink_fmsg_item *item; |
| 4366 | |
| 4367 | if (value_len > DEVLINK_FMSG_MAX_SIZE) |
| 4368 | return -EMSGSIZE; |
| 4369 | |
| 4370 | item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL); |
| 4371 | if (!item) |
| 4372 | return -ENOMEM; |
| 4373 | |
| 4374 | item->nla_type = value_nla_type; |
| 4375 | item->len = value_len; |
| 4376 | item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; |
| 4377 | memcpy(&item->value, value, item->len); |
| 4378 | list_add_tail(&item->list, &fmsg->item_list); |
| 4379 | |
| 4380 | return 0; |
| 4381 | } |
| 4382 | |
| 4383 | int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value) |
| 4384 | { |
| 4385 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG); |
| 4386 | } |
| 4387 | EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put); |
| 4388 | |
| 4389 | int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value) |
| 4390 | { |
| 4391 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8); |
| 4392 | } |
| 4393 | EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put); |
| 4394 | |
| 4395 | int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value) |
| 4396 | { |
| 4397 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32); |
| 4398 | } |
| 4399 | EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put); |
| 4400 | |
| 4401 | int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value) |
| 4402 | { |
| 4403 | return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64); |
| 4404 | } |
| 4405 | EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put); |
| 4406 | |
| 4407 | int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value) |
| 4408 | { |
| 4409 | return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, |
| 4410 | NLA_NUL_STRING); |
| 4411 | } |
| 4412 | EXPORT_SYMBOL_GPL(devlink_fmsg_string_put); |
| 4413 | |
| 4414 | int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value, |
| 4415 | u16 value_len) |
| 4416 | { |
| 4417 | return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY); |
| 4418 | } |
| 4419 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put); |
| 4420 | |
| 4421 | int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4422 | bool value) |
| 4423 | { |
| 4424 | int err; |
| 4425 | |
| 4426 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4427 | if (err) |
| 4428 | return err; |
| 4429 | |
| 4430 | err = devlink_fmsg_bool_put(fmsg, value); |
| 4431 | if (err) |
| 4432 | return err; |
| 4433 | |
| 4434 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4435 | if (err) |
| 4436 | return err; |
| 4437 | |
| 4438 | return 0; |
| 4439 | } |
| 4440 | EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put); |
| 4441 | |
| 4442 | int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4443 | u8 value) |
| 4444 | { |
| 4445 | int err; |
| 4446 | |
| 4447 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4448 | if (err) |
| 4449 | return err; |
| 4450 | |
| 4451 | err = devlink_fmsg_u8_put(fmsg, value); |
| 4452 | if (err) |
| 4453 | return err; |
| 4454 | |
| 4455 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4456 | if (err) |
| 4457 | return err; |
| 4458 | |
| 4459 | return 0; |
| 4460 | } |
| 4461 | EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put); |
| 4462 | |
| 4463 | int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4464 | u32 value) |
| 4465 | { |
| 4466 | int err; |
| 4467 | |
| 4468 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4469 | if (err) |
| 4470 | return err; |
| 4471 | |
| 4472 | err = devlink_fmsg_u32_put(fmsg, value); |
| 4473 | if (err) |
| 4474 | return err; |
| 4475 | |
| 4476 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4477 | if (err) |
| 4478 | return err; |
| 4479 | |
| 4480 | return 0; |
| 4481 | } |
| 4482 | EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put); |
| 4483 | |
| 4484 | int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4485 | u64 value) |
| 4486 | { |
| 4487 | int err; |
| 4488 | |
| 4489 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4490 | if (err) |
| 4491 | return err; |
| 4492 | |
| 4493 | err = devlink_fmsg_u64_put(fmsg, value); |
| 4494 | if (err) |
| 4495 | return err; |
| 4496 | |
| 4497 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4498 | if (err) |
| 4499 | return err; |
| 4500 | |
| 4501 | return 0; |
| 4502 | } |
| 4503 | EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put); |
| 4504 | |
| 4505 | int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4506 | const char *value) |
| 4507 | { |
| 4508 | int err; |
| 4509 | |
| 4510 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4511 | if (err) |
| 4512 | return err; |
| 4513 | |
| 4514 | err = devlink_fmsg_string_put(fmsg, value); |
| 4515 | if (err) |
| 4516 | return err; |
| 4517 | |
| 4518 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4519 | if (err) |
| 4520 | return err; |
| 4521 | |
| 4522 | return 0; |
| 4523 | } |
| 4524 | EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put); |
| 4525 | |
| 4526 | int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name, |
| 4527 | const void *value, u16 value_len) |
| 4528 | { |
| 4529 | int err; |
| 4530 | |
| 4531 | err = devlink_fmsg_pair_nest_start(fmsg, name); |
| 4532 | if (err) |
| 4533 | return err; |
| 4534 | |
| 4535 | err = devlink_fmsg_binary_put(fmsg, value, value_len); |
| 4536 | if (err) |
| 4537 | return err; |
| 4538 | |
| 4539 | err = devlink_fmsg_pair_nest_end(fmsg); |
| 4540 | if (err) |
| 4541 | return err; |
| 4542 | |
| 4543 | return 0; |
| 4544 | } |
| 4545 | EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put); |
| 4546 | |
| 4547 | static int |
| 4548 | devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb) |
| 4549 | { |
| 4550 | switch (msg->nla_type) { |
| 4551 | case NLA_FLAG: |
| 4552 | case NLA_U8: |
| 4553 | case NLA_U32: |
| 4554 | case NLA_U64: |
| 4555 | case NLA_NUL_STRING: |
| 4556 | case NLA_BINARY: |
| 4557 | return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE, |
| 4558 | msg->nla_type); |
| 4559 | default: |
| 4560 | return -EINVAL; |
| 4561 | } |
| 4562 | } |
| 4563 | |
| 4564 | static int |
| 4565 | devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb) |
| 4566 | { |
| 4567 | int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; |
| 4568 | u8 tmp; |
| 4569 | |
| 4570 | switch (msg->nla_type) { |
| 4571 | case NLA_FLAG: |
| 4572 | /* Always provide flag data, regardless of its value */ |
| 4573 | tmp = *(bool *) msg->value; |
| 4574 | |
| 4575 | return nla_put_u8(skb, attrtype, tmp); |
| 4576 | case NLA_U8: |
| 4577 | return nla_put_u8(skb, attrtype, *(u8 *) msg->value); |
| 4578 | case NLA_U32: |
| 4579 | return nla_put_u32(skb, attrtype, *(u32 *) msg->value); |
| 4580 | case NLA_U64: |
| 4581 | return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value, |
| 4582 | DEVLINK_ATTR_PAD); |
| 4583 | case NLA_NUL_STRING: |
| 4584 | return nla_put_string(skb, attrtype, (char *) &msg->value); |
| 4585 | case NLA_BINARY: |
| 4586 | return nla_put(skb, attrtype, msg->len, (void *) &msg->value); |
| 4587 | default: |
| 4588 | return -EINVAL; |
| 4589 | } |
| 4590 | } |
| 4591 | |
| 4592 | static int |
| 4593 | devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb, |
| 4594 | int *start) |
| 4595 | { |
| 4596 | struct devlink_fmsg_item *item; |
| 4597 | struct nlattr *fmsg_nlattr; |
| 4598 | int i = 0; |
| 4599 | int err; |
| 4600 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4601 | fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4602 | if (!fmsg_nlattr) |
| 4603 | return -EMSGSIZE; |
| 4604 | |
| 4605 | list_for_each_entry(item, &fmsg->item_list, list) { |
| 4606 | if (i < *start) { |
| 4607 | i++; |
| 4608 | continue; |
| 4609 | } |
| 4610 | |
| 4611 | switch (item->attrtype) { |
| 4612 | case DEVLINK_ATTR_FMSG_OBJ_NEST_START: |
| 4613 | case DEVLINK_ATTR_FMSG_PAIR_NEST_START: |
| 4614 | case DEVLINK_ATTR_FMSG_ARR_NEST_START: |
| 4615 | case DEVLINK_ATTR_FMSG_NEST_END: |
| 4616 | err = nla_put_flag(skb, item->attrtype); |
| 4617 | break; |
| 4618 | case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA: |
| 4619 | err = devlink_fmsg_item_fill_type(item, skb); |
| 4620 | if (err) |
| 4621 | break; |
| 4622 | err = devlink_fmsg_item_fill_data(item, skb); |
| 4623 | break; |
| 4624 | case DEVLINK_ATTR_FMSG_OBJ_NAME: |
| 4625 | err = nla_put_string(skb, item->attrtype, |
| 4626 | (char *) &item->value); |
| 4627 | break; |
| 4628 | default: |
| 4629 | err = -EINVAL; |
| 4630 | break; |
| 4631 | } |
| 4632 | if (!err) |
| 4633 | *start = ++i; |
| 4634 | else |
| 4635 | break; |
| 4636 | } |
| 4637 | |
| 4638 | nla_nest_end(skb, fmsg_nlattr); |
| 4639 | return err; |
| 4640 | } |
| 4641 | |
| 4642 | static int devlink_fmsg_snd(struct devlink_fmsg *fmsg, |
| 4643 | struct genl_info *info, |
| 4644 | enum devlink_command cmd, int flags) |
| 4645 | { |
| 4646 | struct nlmsghdr *nlh; |
| 4647 | struct sk_buff *skb; |
| 4648 | bool last = false; |
| 4649 | int index = 0; |
| 4650 | void *hdr; |
| 4651 | int err; |
| 4652 | |
| 4653 | while (!last) { |
| 4654 | int tmp_index = index; |
| 4655 | |
| 4656 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4657 | if (!skb) |
| 4658 | return -ENOMEM; |
| 4659 | |
| 4660 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 4661 | &devlink_nl_family, flags | NLM_F_MULTI, cmd); |
| 4662 | if (!hdr) { |
| 4663 | err = -EMSGSIZE; |
| 4664 | goto nla_put_failure; |
| 4665 | } |
| 4666 | |
| 4667 | err = devlink_fmsg_prepare_skb(fmsg, skb, &index); |
| 4668 | if (!err) |
| 4669 | last = true; |
| 4670 | else if (err != -EMSGSIZE || tmp_index == index) |
| 4671 | goto nla_put_failure; |
| 4672 | |
| 4673 | genlmsg_end(skb, hdr); |
| 4674 | err = genlmsg_reply(skb, info); |
| 4675 | if (err) |
| 4676 | return err; |
| 4677 | } |
| 4678 | |
| 4679 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 4680 | if (!skb) |
| 4681 | return -ENOMEM; |
| 4682 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 4683 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 4684 | if (!nlh) { |
| 4685 | err = -EMSGSIZE; |
| 4686 | goto nla_put_failure; |
| 4687 | } |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4688 | |
Li RongQing | fde55ea | 2019-02-11 19:09:07 +0800 | [diff] [blame] | 4689 | return genlmsg_reply(skb, info); |
Eran Ben Elisha | 1db64e8 | 2019-02-07 11:36:32 +0200 | [diff] [blame] | 4690 | |
| 4691 | nla_put_failure: |
| 4692 | nlmsg_free(skb); |
| 4693 | return err; |
| 4694 | } |
| 4695 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4696 | static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb, |
| 4697 | struct netlink_callback *cb, |
| 4698 | enum devlink_command cmd) |
| 4699 | { |
| 4700 | int index = cb->args[0]; |
| 4701 | int tmp_index = index; |
| 4702 | void *hdr; |
| 4703 | int err; |
| 4704 | |
| 4705 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 4706 | &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd); |
| 4707 | if (!hdr) { |
| 4708 | err = -EMSGSIZE; |
| 4709 | goto nla_put_failure; |
| 4710 | } |
| 4711 | |
| 4712 | err = devlink_fmsg_prepare_skb(fmsg, skb, &index); |
| 4713 | if ((err && err != -EMSGSIZE) || tmp_index == index) |
| 4714 | goto nla_put_failure; |
| 4715 | |
| 4716 | cb->args[0] = index; |
| 4717 | genlmsg_end(skb, hdr); |
| 4718 | return skb->len; |
| 4719 | |
| 4720 | nla_put_failure: |
| 4721 | genlmsg_cancel(skb, hdr); |
| 4722 | return err; |
| 4723 | } |
| 4724 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4725 | struct devlink_health_reporter { |
| 4726 | struct list_head list; |
| 4727 | void *priv; |
| 4728 | const struct devlink_health_reporter_ops *ops; |
| 4729 | struct devlink *devlink; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4730 | struct devlink_fmsg *dump_fmsg; |
| 4731 | struct mutex dump_lock; /* lock parallel read/write from dump buffers */ |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4732 | u64 graceful_period; |
| 4733 | bool auto_recover; |
| 4734 | u8 health_state; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4735 | u64 dump_ts; |
| 4736 | u64 error_count; |
| 4737 | u64 recovery_count; |
| 4738 | u64 last_recovery_ts; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4739 | refcount_t refcount; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4740 | }; |
| 4741 | |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4742 | void * |
| 4743 | devlink_health_reporter_priv(struct devlink_health_reporter *reporter) |
| 4744 | { |
| 4745 | return reporter->priv; |
| 4746 | } |
| 4747 | EXPORT_SYMBOL_GPL(devlink_health_reporter_priv); |
| 4748 | |
| 4749 | static struct devlink_health_reporter * |
| 4750 | devlink_health_reporter_find_by_name(struct devlink *devlink, |
| 4751 | const char *reporter_name) |
| 4752 | { |
| 4753 | struct devlink_health_reporter *reporter; |
| 4754 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4755 | lockdep_assert_held(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4756 | list_for_each_entry(reporter, &devlink->reporter_list, list) |
| 4757 | if (!strcmp(reporter->ops->name, reporter_name)) |
| 4758 | return reporter; |
| 4759 | return NULL; |
| 4760 | } |
| 4761 | |
| 4762 | /** |
| 4763 | * devlink_health_reporter_create - create devlink health reporter |
| 4764 | * |
| 4765 | * @devlink: devlink |
| 4766 | * @ops: ops |
| 4767 | * @graceful_period: to avoid recovery loops, in msecs |
| 4768 | * @auto_recover: auto recover when error occurs |
| 4769 | * @priv: priv |
| 4770 | */ |
| 4771 | struct devlink_health_reporter * |
| 4772 | devlink_health_reporter_create(struct devlink *devlink, |
| 4773 | const struct devlink_health_reporter_ops *ops, |
| 4774 | u64 graceful_period, bool auto_recover, |
| 4775 | void *priv) |
| 4776 | { |
| 4777 | struct devlink_health_reporter *reporter; |
| 4778 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4779 | mutex_lock(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4780 | if (devlink_health_reporter_find_by_name(devlink, ops->name)) { |
| 4781 | reporter = ERR_PTR(-EEXIST); |
| 4782 | goto unlock; |
| 4783 | } |
| 4784 | |
| 4785 | if (WARN_ON(auto_recover && !ops->recover) || |
| 4786 | WARN_ON(graceful_period && !ops->recover)) { |
| 4787 | reporter = ERR_PTR(-EINVAL); |
| 4788 | goto unlock; |
| 4789 | } |
| 4790 | |
| 4791 | reporter = kzalloc(sizeof(*reporter), GFP_KERNEL); |
| 4792 | if (!reporter) { |
| 4793 | reporter = ERR_PTR(-ENOMEM); |
| 4794 | goto unlock; |
| 4795 | } |
| 4796 | |
| 4797 | reporter->priv = priv; |
| 4798 | reporter->ops = ops; |
| 4799 | reporter->devlink = devlink; |
| 4800 | reporter->graceful_period = graceful_period; |
| 4801 | reporter->auto_recover = auto_recover; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4802 | mutex_init(&reporter->dump_lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4803 | refcount_set(&reporter->refcount, 1); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4804 | list_add_tail(&reporter->list, &devlink->reporter_list); |
| 4805 | unlock: |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4806 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4807 | return reporter; |
| 4808 | } |
| 4809 | EXPORT_SYMBOL_GPL(devlink_health_reporter_create); |
| 4810 | |
| 4811 | /** |
| 4812 | * devlink_health_reporter_destroy - destroy devlink health reporter |
| 4813 | * |
| 4814 | * @reporter: devlink health reporter to destroy |
| 4815 | */ |
| 4816 | void |
| 4817 | devlink_health_reporter_destroy(struct devlink_health_reporter *reporter) |
| 4818 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4819 | mutex_lock(&reporter->devlink->reporters_lock); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4820 | list_del(&reporter->list); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4821 | mutex_unlock(&reporter->devlink->reporters_lock); |
| 4822 | while (refcount_read(&reporter->refcount) > 1) |
| 4823 | msleep(100); |
Jiri Pirko | 375cf8c | 2019-03-24 11:14:24 +0100 | [diff] [blame] | 4824 | mutex_destroy(&reporter->dump_lock); |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4825 | if (reporter->dump_fmsg) |
| 4826 | devlink_fmsg_free(reporter->dump_fmsg); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 4827 | kfree(reporter); |
| 4828 | } |
| 4829 | EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy); |
| 4830 | |
Eran Ben Elisha | 3167b27 | 2019-03-03 10:57:30 +0200 | [diff] [blame] | 4831 | void |
| 4832 | devlink_health_reporter_state_update(struct devlink_health_reporter *reporter, |
| 4833 | enum devlink_health_reporter_state state) |
| 4834 | { |
| 4835 | if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY && |
| 4836 | state != DEVLINK_HEALTH_REPORTER_STATE_ERROR)) |
| 4837 | return; |
| 4838 | |
| 4839 | if (reporter->health_state == state) |
| 4840 | return; |
| 4841 | |
| 4842 | reporter->health_state = state; |
| 4843 | trace_devlink_health_reporter_state_update(reporter->devlink, |
| 4844 | reporter->ops->name, state); |
| 4845 | } |
| 4846 | EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update); |
| 4847 | |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4848 | static int |
| 4849 | devlink_health_reporter_recover(struct devlink_health_reporter *reporter, |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 4850 | void *priv_ctx, struct netlink_ext_ack *extack) |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4851 | { |
| 4852 | int err; |
| 4853 | |
Jiri Pirko | 4028182 | 2019-10-10 15:18:48 +0200 | [diff] [blame] | 4854 | if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY) |
| 4855 | return 0; |
| 4856 | |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4857 | if (!reporter->ops->recover) |
| 4858 | return -EOPNOTSUPP; |
| 4859 | |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 4860 | err = reporter->ops->recover(reporter, priv_ctx, extack); |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4861 | if (err) |
| 4862 | return err; |
| 4863 | |
| 4864 | reporter->recovery_count++; |
| 4865 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY; |
| 4866 | reporter->last_recovery_ts = jiffies; |
| 4867 | |
| 4868 | return 0; |
| 4869 | } |
| 4870 | |
| 4871 | static void |
| 4872 | devlink_health_dump_clear(struct devlink_health_reporter *reporter) |
| 4873 | { |
| 4874 | if (!reporter->dump_fmsg) |
| 4875 | return; |
| 4876 | devlink_fmsg_free(reporter->dump_fmsg); |
| 4877 | reporter->dump_fmsg = NULL; |
| 4878 | } |
| 4879 | |
| 4880 | static int devlink_health_do_dump(struct devlink_health_reporter *reporter, |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 4881 | void *priv_ctx, |
| 4882 | struct netlink_ext_ack *extack) |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4883 | { |
| 4884 | int err; |
| 4885 | |
| 4886 | if (!reporter->ops->dump) |
| 4887 | return 0; |
| 4888 | |
| 4889 | if (reporter->dump_fmsg) |
| 4890 | return 0; |
| 4891 | |
| 4892 | reporter->dump_fmsg = devlink_fmsg_alloc(); |
| 4893 | if (!reporter->dump_fmsg) { |
| 4894 | err = -ENOMEM; |
| 4895 | return err; |
| 4896 | } |
| 4897 | |
| 4898 | err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg); |
| 4899 | if (err) |
| 4900 | goto dump_err; |
| 4901 | |
| 4902 | err = reporter->ops->dump(reporter, reporter->dump_fmsg, |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 4903 | priv_ctx, extack); |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4904 | if (err) |
| 4905 | goto dump_err; |
| 4906 | |
| 4907 | err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg); |
| 4908 | if (err) |
| 4909 | goto dump_err; |
| 4910 | |
| 4911 | reporter->dump_ts = jiffies; |
| 4912 | |
| 4913 | return 0; |
| 4914 | |
| 4915 | dump_err: |
| 4916 | devlink_health_dump_clear(reporter); |
| 4917 | return err; |
| 4918 | } |
| 4919 | |
| 4920 | int devlink_health_report(struct devlink_health_reporter *reporter, |
| 4921 | const char *msg, void *priv_ctx) |
| 4922 | { |
Eran Ben Elisha | a0a21ad | 2019-03-03 10:57:29 +0200 | [diff] [blame] | 4923 | enum devlink_health_reporter_state prev_health_state; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4924 | struct devlink *devlink = reporter->devlink; |
| 4925 | |
| 4926 | /* write a log message of the current error */ |
| 4927 | WARN_ON(!msg); |
| 4928 | trace_devlink_health_report(devlink, reporter->ops->name, msg); |
| 4929 | reporter->error_count++; |
Eran Ben Elisha | a0a21ad | 2019-03-03 10:57:29 +0200 | [diff] [blame] | 4930 | prev_health_state = reporter->health_state; |
| 4931 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4932 | |
| 4933 | /* abort if the previous error wasn't recovered */ |
| 4934 | if (reporter->auto_recover && |
Eran Ben Elisha | a0a21ad | 2019-03-03 10:57:29 +0200 | [diff] [blame] | 4935 | (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY || |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4936 | jiffies - reporter->last_recovery_ts < |
| 4937 | msecs_to_jiffies(reporter->graceful_period))) { |
| 4938 | trace_devlink_health_recover_aborted(devlink, |
| 4939 | reporter->ops->name, |
| 4940 | reporter->health_state, |
| 4941 | jiffies - |
| 4942 | reporter->last_recovery_ts); |
| 4943 | return -ECANCELED; |
| 4944 | } |
| 4945 | |
| 4946 | reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; |
| 4947 | |
| 4948 | mutex_lock(&reporter->dump_lock); |
| 4949 | /* store current dump of current error, for later analysis */ |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 4950 | devlink_health_do_dump(reporter, priv_ctx, NULL); |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4951 | mutex_unlock(&reporter->dump_lock); |
| 4952 | |
| 4953 | if (reporter->auto_recover) |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 4954 | return devlink_health_reporter_recover(reporter, |
| 4955 | priv_ctx, NULL); |
Eran Ben Elisha | c8e1da0 | 2019-02-07 11:36:34 +0200 | [diff] [blame] | 4956 | |
| 4957 | return 0; |
| 4958 | } |
| 4959 | EXPORT_SYMBOL_GPL(devlink_health_report); |
| 4960 | |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4961 | static struct devlink_health_reporter * |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4962 | devlink_health_reporter_get_from_attrs(struct devlink *devlink, |
| 4963 | struct nlattr **attrs) |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4964 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4965 | struct devlink_health_reporter *reporter; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4966 | char *reporter_name; |
| 4967 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4968 | if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]) |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 4969 | return NULL; |
| 4970 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4971 | reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 4972 | mutex_lock(&devlink->reporters_lock); |
| 4973 | reporter = devlink_health_reporter_find_by_name(devlink, reporter_name); |
| 4974 | if (reporter) |
| 4975 | refcount_inc(&reporter->refcount); |
| 4976 | mutex_unlock(&devlink->reporters_lock); |
| 4977 | return reporter; |
| 4978 | } |
| 4979 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4980 | static struct devlink_health_reporter * |
| 4981 | devlink_health_reporter_get_from_info(struct devlink *devlink, |
| 4982 | struct genl_info *info) |
| 4983 | { |
| 4984 | return devlink_health_reporter_get_from_attrs(devlink, info->attrs); |
| 4985 | } |
| 4986 | |
| 4987 | static struct devlink_health_reporter * |
| 4988 | devlink_health_reporter_get_from_cb(struct netlink_callback *cb) |
| 4989 | { |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 4990 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4991 | struct devlink_health_reporter *reporter; |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 4992 | struct nlattr **attrs = info->attrs; |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4993 | struct devlink *devlink; |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 4994 | |
| 4995 | mutex_lock(&devlink_mutex); |
| 4996 | devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs); |
| 4997 | if (IS_ERR(devlink)) |
| 4998 | goto unlock; |
| 4999 | |
| 5000 | reporter = devlink_health_reporter_get_from_attrs(devlink, attrs); |
| 5001 | mutex_unlock(&devlink_mutex); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5002 | return reporter; |
| 5003 | unlock: |
| 5004 | mutex_unlock(&devlink_mutex); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5005 | return NULL; |
| 5006 | } |
| 5007 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5008 | static void |
| 5009 | devlink_health_reporter_put(struct devlink_health_reporter *reporter) |
| 5010 | { |
| 5011 | refcount_dec(&reporter->refcount); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | static int |
| 5015 | devlink_nl_health_reporter_fill(struct sk_buff *msg, |
| 5016 | struct devlink *devlink, |
| 5017 | struct devlink_health_reporter *reporter, |
| 5018 | enum devlink_command cmd, u32 portid, |
| 5019 | u32 seq, int flags) |
| 5020 | { |
| 5021 | struct nlattr *reporter_attr; |
| 5022 | void *hdr; |
| 5023 | |
| 5024 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 5025 | if (!hdr) |
| 5026 | return -EMSGSIZE; |
| 5027 | |
| 5028 | if (devlink_nl_put_handle(msg, devlink)) |
| 5029 | goto genlmsg_cancel; |
| 5030 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5031 | reporter_attr = nla_nest_start_noflag(msg, |
| 5032 | DEVLINK_ATTR_HEALTH_REPORTER); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5033 | if (!reporter_attr) |
| 5034 | goto genlmsg_cancel; |
| 5035 | if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME, |
| 5036 | reporter->ops->name)) |
| 5037 | goto reporter_nest_cancel; |
| 5038 | if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE, |
| 5039 | reporter->health_state)) |
| 5040 | goto reporter_nest_cancel; |
Aya Levin | 5471952 | 2019-02-21 14:12:01 +0200 | [diff] [blame] | 5041 | 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] | 5042 | reporter->error_count, DEVLINK_ATTR_PAD)) |
| 5043 | goto reporter_nest_cancel; |
Aya Levin | 5471952 | 2019-02-21 14:12:01 +0200 | [diff] [blame] | 5044 | 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] | 5045 | reporter->recovery_count, DEVLINK_ATTR_PAD)) |
| 5046 | goto reporter_nest_cancel; |
Aya Levin | 574b1e1 | 2019-02-21 14:12:02 +0200 | [diff] [blame] | 5047 | if (reporter->ops->recover && |
| 5048 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5049 | reporter->graceful_period, |
| 5050 | DEVLINK_ATTR_PAD)) |
| 5051 | goto reporter_nest_cancel; |
Aya Levin | 574b1e1 | 2019-02-21 14:12:02 +0200 | [diff] [blame] | 5052 | if (reporter->ops->recover && |
| 5053 | nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5054 | reporter->auto_recover)) |
| 5055 | goto reporter_nest_cancel; |
| 5056 | if (reporter->dump_fmsg && |
| 5057 | nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS, |
| 5058 | jiffies_to_msecs(reporter->dump_ts), |
| 5059 | DEVLINK_ATTR_PAD)) |
| 5060 | goto reporter_nest_cancel; |
| 5061 | |
| 5062 | nla_nest_end(msg, reporter_attr); |
| 5063 | genlmsg_end(msg, hdr); |
| 5064 | return 0; |
| 5065 | |
| 5066 | reporter_nest_cancel: |
| 5067 | nla_nest_end(msg, reporter_attr); |
| 5068 | genlmsg_cancel: |
| 5069 | genlmsg_cancel(msg, hdr); |
| 5070 | return -EMSGSIZE; |
| 5071 | } |
| 5072 | |
| 5073 | static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb, |
| 5074 | struct genl_info *info) |
| 5075 | { |
| 5076 | struct devlink *devlink = info->user_ptr[0]; |
| 5077 | struct devlink_health_reporter *reporter; |
| 5078 | struct sk_buff *msg; |
| 5079 | int err; |
| 5080 | |
| 5081 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5082 | if (!reporter) |
| 5083 | return -EINVAL; |
| 5084 | |
| 5085 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5086 | if (!msg) { |
| 5087 | err = -ENOMEM; |
| 5088 | goto out; |
| 5089 | } |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5090 | |
| 5091 | err = devlink_nl_health_reporter_fill(msg, devlink, reporter, |
| 5092 | DEVLINK_CMD_HEALTH_REPORTER_GET, |
| 5093 | info->snd_portid, info->snd_seq, |
| 5094 | 0); |
| 5095 | if (err) { |
| 5096 | nlmsg_free(msg); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5097 | goto out; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5098 | } |
| 5099 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5100 | err = genlmsg_reply(msg, info); |
| 5101 | out: |
| 5102 | devlink_health_reporter_put(reporter); |
| 5103 | return err; |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | static int |
| 5107 | devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg, |
| 5108 | struct netlink_callback *cb) |
| 5109 | { |
| 5110 | struct devlink_health_reporter *reporter; |
| 5111 | struct devlink *devlink; |
| 5112 | int start = cb->args[0]; |
| 5113 | int idx = 0; |
| 5114 | int err; |
| 5115 | |
| 5116 | mutex_lock(&devlink_mutex); |
| 5117 | list_for_each_entry(devlink, &devlink_list, list) { |
| 5118 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 5119 | continue; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5120 | mutex_lock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5121 | list_for_each_entry(reporter, &devlink->reporter_list, |
| 5122 | list) { |
| 5123 | if (idx < start) { |
| 5124 | idx++; |
| 5125 | continue; |
| 5126 | } |
| 5127 | err = devlink_nl_health_reporter_fill(msg, devlink, |
| 5128 | reporter, |
| 5129 | DEVLINK_CMD_HEALTH_REPORTER_GET, |
| 5130 | NETLINK_CB(cb->skb).portid, |
| 5131 | cb->nlh->nlmsg_seq, |
| 5132 | NLM_F_MULTI); |
| 5133 | if (err) { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5134 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5135 | goto out; |
| 5136 | } |
| 5137 | idx++; |
| 5138 | } |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5139 | mutex_unlock(&devlink->reporters_lock); |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5140 | } |
| 5141 | out: |
| 5142 | mutex_unlock(&devlink_mutex); |
| 5143 | |
| 5144 | cb->args[0] = idx; |
| 5145 | return msg->len; |
| 5146 | } |
| 5147 | |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5148 | static int |
| 5149 | devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb, |
| 5150 | struct genl_info *info) |
| 5151 | { |
| 5152 | struct devlink *devlink = info->user_ptr[0]; |
| 5153 | struct devlink_health_reporter *reporter; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5154 | int err; |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5155 | |
| 5156 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5157 | if (!reporter) |
| 5158 | return -EINVAL; |
| 5159 | |
| 5160 | if (!reporter->ops->recover && |
| 5161 | (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] || |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5162 | info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) { |
| 5163 | err = -EOPNOTSUPP; |
| 5164 | goto out; |
| 5165 | } |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5166 | |
| 5167 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]) |
| 5168 | reporter->graceful_period = |
| 5169 | nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]); |
| 5170 | |
| 5171 | if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]) |
| 5172 | reporter->auto_recover = |
| 5173 | nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]); |
| 5174 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5175 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5176 | return 0; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5177 | out: |
| 5178 | devlink_health_reporter_put(reporter); |
| 5179 | return err; |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5180 | } |
| 5181 | |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5182 | static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb, |
| 5183 | struct genl_info *info) |
| 5184 | { |
| 5185 | struct devlink *devlink = info->user_ptr[0]; |
| 5186 | struct devlink_health_reporter *reporter; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5187 | int err; |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5188 | |
| 5189 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5190 | if (!reporter) |
| 5191 | return -EINVAL; |
| 5192 | |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5193 | err = devlink_health_reporter_recover(reporter, NULL, info->extack); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5194 | |
| 5195 | devlink_health_reporter_put(reporter); |
| 5196 | return err; |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 5197 | } |
| 5198 | |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5199 | static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb, |
| 5200 | struct genl_info *info) |
| 5201 | { |
| 5202 | struct devlink *devlink = info->user_ptr[0]; |
| 5203 | struct devlink_health_reporter *reporter; |
| 5204 | struct devlink_fmsg *fmsg; |
| 5205 | int err; |
| 5206 | |
| 5207 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5208 | if (!reporter) |
| 5209 | return -EINVAL; |
| 5210 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5211 | if (!reporter->ops->diagnose) { |
| 5212 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5213 | return -EOPNOTSUPP; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5214 | } |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5215 | |
| 5216 | fmsg = devlink_fmsg_alloc(); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5217 | if (!fmsg) { |
| 5218 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5219 | return -ENOMEM; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5220 | } |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5221 | |
| 5222 | err = devlink_fmsg_obj_nest_start(fmsg); |
| 5223 | if (err) |
| 5224 | goto out; |
| 5225 | |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5226 | err = reporter->ops->diagnose(reporter, fmsg, info->extack); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5227 | if (err) |
| 5228 | goto out; |
| 5229 | |
| 5230 | err = devlink_fmsg_obj_nest_end(fmsg); |
| 5231 | if (err) |
| 5232 | goto out; |
| 5233 | |
| 5234 | err = devlink_fmsg_snd(fmsg, info, |
| 5235 | DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0); |
| 5236 | |
| 5237 | out: |
| 5238 | devlink_fmsg_free(fmsg); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5239 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 5240 | return err; |
| 5241 | } |
| 5242 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5243 | static int |
| 5244 | devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb, |
| 5245 | struct netlink_callback *cb) |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5246 | { |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5247 | struct devlink_health_reporter *reporter; |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5248 | u64 start = cb->args[0]; |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5249 | int err; |
| 5250 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5251 | reporter = devlink_health_reporter_get_from_cb(cb); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5252 | if (!reporter) |
| 5253 | return -EINVAL; |
| 5254 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5255 | if (!reporter->ops->dump) { |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5256 | err = -EOPNOTSUPP; |
| 5257 | goto out; |
| 5258 | } |
| 5259 | mutex_lock(&reporter->dump_lock); |
| 5260 | if (!start) { |
Jiri Pirko | e7a9810 | 2019-10-10 15:18:49 +0200 | [diff] [blame] | 5261 | err = devlink_health_do_dump(reporter, NULL, cb->extack); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5262 | if (err) |
| 5263 | goto unlock; |
| 5264 | cb->args[1] = reporter->dump_ts; |
| 5265 | } |
| 5266 | if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) { |
| 5267 | NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry"); |
| 5268 | err = -EAGAIN; |
| 5269 | goto unlock; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5270 | } |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5271 | |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5272 | err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb, |
| 5273 | DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET); |
| 5274 | unlock: |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5275 | mutex_unlock(&reporter->dump_lock); |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 5276 | out: |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5277 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5278 | return err; |
| 5279 | } |
| 5280 | |
| 5281 | static int |
| 5282 | devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb, |
| 5283 | struct genl_info *info) |
| 5284 | { |
| 5285 | struct devlink *devlink = info->user_ptr[0]; |
| 5286 | struct devlink_health_reporter *reporter; |
| 5287 | |
| 5288 | reporter = devlink_health_reporter_get_from_info(devlink, info); |
| 5289 | if (!reporter) |
| 5290 | return -EINVAL; |
| 5291 | |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5292 | if (!reporter->ops->dump) { |
| 5293 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5294 | return -EOPNOTSUPP; |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5295 | } |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5296 | |
| 5297 | mutex_lock(&reporter->dump_lock); |
| 5298 | devlink_health_dump_clear(reporter); |
| 5299 | mutex_unlock(&reporter->dump_lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 5300 | devlink_health_reporter_put(reporter); |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 5301 | return 0; |
| 5302 | } |
| 5303 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5304 | struct devlink_stats { |
| 5305 | u64 rx_bytes; |
| 5306 | u64 rx_packets; |
| 5307 | struct u64_stats_sync syncp; |
| 5308 | }; |
| 5309 | |
| 5310 | /** |
| 5311 | * struct devlink_trap_group_item - Packet trap group attributes. |
| 5312 | * @group: Immutable packet trap group attributes. |
| 5313 | * @refcount: Number of trap items using the group. |
| 5314 | * @list: trap_group_list member. |
| 5315 | * @stats: Trap group statistics. |
| 5316 | * |
| 5317 | * Describes packet trap group attributes. Created by devlink during trap |
| 5318 | * registration. |
| 5319 | */ |
| 5320 | struct devlink_trap_group_item { |
| 5321 | const struct devlink_trap_group *group; |
| 5322 | refcount_t refcount; |
| 5323 | struct list_head list; |
| 5324 | struct devlink_stats __percpu *stats; |
| 5325 | }; |
| 5326 | |
| 5327 | /** |
| 5328 | * struct devlink_trap_item - Packet trap attributes. |
| 5329 | * @trap: Immutable packet trap attributes. |
| 5330 | * @group_item: Associated group item. |
| 5331 | * @list: trap_list member. |
| 5332 | * @action: Trap action. |
| 5333 | * @stats: Trap statistics. |
| 5334 | * @priv: Driver private information. |
| 5335 | * |
| 5336 | * Describes both mutable and immutable packet trap attributes. Created by |
| 5337 | * devlink during trap registration and used for all trap related operations. |
| 5338 | */ |
| 5339 | struct devlink_trap_item { |
| 5340 | const struct devlink_trap *trap; |
| 5341 | struct devlink_trap_group_item *group_item; |
| 5342 | struct list_head list; |
| 5343 | enum devlink_trap_action action; |
| 5344 | struct devlink_stats __percpu *stats; |
| 5345 | void *priv; |
| 5346 | }; |
| 5347 | |
| 5348 | static struct devlink_trap_item * |
| 5349 | devlink_trap_item_lookup(struct devlink *devlink, const char *name) |
| 5350 | { |
| 5351 | struct devlink_trap_item *trap_item; |
| 5352 | |
| 5353 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
| 5354 | if (!strcmp(trap_item->trap->name, name)) |
| 5355 | return trap_item; |
| 5356 | } |
| 5357 | |
| 5358 | return NULL; |
| 5359 | } |
| 5360 | |
| 5361 | static struct devlink_trap_item * |
| 5362 | devlink_trap_item_get_from_info(struct devlink *devlink, |
| 5363 | struct genl_info *info) |
| 5364 | { |
| 5365 | struct nlattr *attr; |
| 5366 | |
| 5367 | if (!info->attrs[DEVLINK_ATTR_TRAP_NAME]) |
| 5368 | return NULL; |
| 5369 | attr = info->attrs[DEVLINK_ATTR_TRAP_NAME]; |
| 5370 | |
| 5371 | return devlink_trap_item_lookup(devlink, nla_data(attr)); |
| 5372 | } |
| 5373 | |
| 5374 | static int |
| 5375 | devlink_trap_action_get_from_info(struct genl_info *info, |
| 5376 | enum devlink_trap_action *p_trap_action) |
| 5377 | { |
| 5378 | u8 val; |
| 5379 | |
| 5380 | val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]); |
| 5381 | switch (val) { |
| 5382 | case DEVLINK_TRAP_ACTION_DROP: /* fall-through */ |
| 5383 | case DEVLINK_TRAP_ACTION_TRAP: |
| 5384 | *p_trap_action = val; |
| 5385 | break; |
| 5386 | default: |
| 5387 | return -EINVAL; |
| 5388 | } |
| 5389 | |
| 5390 | return 0; |
| 5391 | } |
| 5392 | |
| 5393 | static int devlink_trap_metadata_put(struct sk_buff *msg, |
| 5394 | const struct devlink_trap *trap) |
| 5395 | { |
| 5396 | struct nlattr *attr; |
| 5397 | |
| 5398 | attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA); |
| 5399 | if (!attr) |
| 5400 | return -EMSGSIZE; |
| 5401 | |
| 5402 | if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) && |
| 5403 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT)) |
| 5404 | goto nla_put_failure; |
| 5405 | |
| 5406 | nla_nest_end(msg, attr); |
| 5407 | |
| 5408 | return 0; |
| 5409 | |
| 5410 | nla_put_failure: |
| 5411 | nla_nest_cancel(msg, attr); |
| 5412 | return -EMSGSIZE; |
| 5413 | } |
| 5414 | |
| 5415 | static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats, |
| 5416 | struct devlink_stats *stats) |
| 5417 | { |
| 5418 | int i; |
| 5419 | |
| 5420 | memset(stats, 0, sizeof(*stats)); |
| 5421 | for_each_possible_cpu(i) { |
| 5422 | struct devlink_stats *cpu_stats; |
| 5423 | u64 rx_packets, rx_bytes; |
| 5424 | unsigned int start; |
| 5425 | |
| 5426 | cpu_stats = per_cpu_ptr(trap_stats, i); |
| 5427 | do { |
| 5428 | start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
| 5429 | rx_packets = cpu_stats->rx_packets; |
| 5430 | rx_bytes = cpu_stats->rx_bytes; |
| 5431 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); |
| 5432 | |
| 5433 | stats->rx_packets += rx_packets; |
| 5434 | stats->rx_bytes += rx_bytes; |
| 5435 | } |
| 5436 | } |
| 5437 | |
| 5438 | static int devlink_trap_stats_put(struct sk_buff *msg, |
| 5439 | struct devlink_stats __percpu *trap_stats) |
| 5440 | { |
| 5441 | struct devlink_stats stats; |
| 5442 | struct nlattr *attr; |
| 5443 | |
| 5444 | devlink_trap_stats_read(trap_stats, &stats); |
| 5445 | |
| 5446 | attr = nla_nest_start(msg, DEVLINK_ATTR_STATS); |
| 5447 | if (!attr) |
| 5448 | return -EMSGSIZE; |
| 5449 | |
| 5450 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS, |
| 5451 | stats.rx_packets, DEVLINK_ATTR_PAD)) |
| 5452 | goto nla_put_failure; |
| 5453 | |
| 5454 | if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES, |
| 5455 | stats.rx_bytes, DEVLINK_ATTR_PAD)) |
| 5456 | goto nla_put_failure; |
| 5457 | |
| 5458 | nla_nest_end(msg, attr); |
| 5459 | |
| 5460 | return 0; |
| 5461 | |
| 5462 | nla_put_failure: |
| 5463 | nla_nest_cancel(msg, attr); |
| 5464 | return -EMSGSIZE; |
| 5465 | } |
| 5466 | |
| 5467 | static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink, |
| 5468 | const struct devlink_trap_item *trap_item, |
| 5469 | enum devlink_command cmd, u32 portid, u32 seq, |
| 5470 | int flags) |
| 5471 | { |
| 5472 | struct devlink_trap_group_item *group_item = trap_item->group_item; |
| 5473 | void *hdr; |
| 5474 | int err; |
| 5475 | |
| 5476 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 5477 | if (!hdr) |
| 5478 | return -EMSGSIZE; |
| 5479 | |
| 5480 | if (devlink_nl_put_handle(msg, devlink)) |
| 5481 | goto nla_put_failure; |
| 5482 | |
| 5483 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME, |
| 5484 | group_item->group->name)) |
| 5485 | goto nla_put_failure; |
| 5486 | |
| 5487 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name)) |
| 5488 | goto nla_put_failure; |
| 5489 | |
| 5490 | if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type)) |
| 5491 | goto nla_put_failure; |
| 5492 | |
| 5493 | if (trap_item->trap->generic && |
| 5494 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC)) |
| 5495 | goto nla_put_failure; |
| 5496 | |
| 5497 | if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action)) |
| 5498 | goto nla_put_failure; |
| 5499 | |
| 5500 | err = devlink_trap_metadata_put(msg, trap_item->trap); |
| 5501 | if (err) |
| 5502 | goto nla_put_failure; |
| 5503 | |
| 5504 | err = devlink_trap_stats_put(msg, trap_item->stats); |
| 5505 | if (err) |
| 5506 | goto nla_put_failure; |
| 5507 | |
| 5508 | genlmsg_end(msg, hdr); |
| 5509 | |
| 5510 | return 0; |
| 5511 | |
| 5512 | nla_put_failure: |
| 5513 | genlmsg_cancel(msg, hdr); |
| 5514 | return -EMSGSIZE; |
| 5515 | } |
| 5516 | |
| 5517 | static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb, |
| 5518 | struct genl_info *info) |
| 5519 | { |
| 5520 | struct netlink_ext_ack *extack = info->extack; |
| 5521 | struct devlink *devlink = info->user_ptr[0]; |
| 5522 | struct devlink_trap_item *trap_item; |
| 5523 | struct sk_buff *msg; |
| 5524 | int err; |
| 5525 | |
| 5526 | if (list_empty(&devlink->trap_list)) |
| 5527 | return -EOPNOTSUPP; |
| 5528 | |
| 5529 | trap_item = devlink_trap_item_get_from_info(devlink, info); |
| 5530 | if (!trap_item) { |
| 5531 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap"); |
| 5532 | return -ENOENT; |
| 5533 | } |
| 5534 | |
| 5535 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 5536 | if (!msg) |
| 5537 | return -ENOMEM; |
| 5538 | |
| 5539 | err = devlink_nl_trap_fill(msg, devlink, trap_item, |
| 5540 | DEVLINK_CMD_TRAP_NEW, info->snd_portid, |
| 5541 | info->snd_seq, 0); |
| 5542 | if (err) |
| 5543 | goto err_trap_fill; |
| 5544 | |
| 5545 | return genlmsg_reply(msg, info); |
| 5546 | |
| 5547 | err_trap_fill: |
| 5548 | nlmsg_free(msg); |
| 5549 | return err; |
| 5550 | } |
| 5551 | |
| 5552 | static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg, |
| 5553 | struct netlink_callback *cb) |
| 5554 | { |
| 5555 | struct devlink_trap_item *trap_item; |
| 5556 | struct devlink *devlink; |
| 5557 | int start = cb->args[0]; |
| 5558 | int idx = 0; |
| 5559 | int err; |
| 5560 | |
| 5561 | mutex_lock(&devlink_mutex); |
| 5562 | list_for_each_entry(devlink, &devlink_list, list) { |
| 5563 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 5564 | continue; |
| 5565 | mutex_lock(&devlink->lock); |
| 5566 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
| 5567 | if (idx < start) { |
| 5568 | idx++; |
| 5569 | continue; |
| 5570 | } |
| 5571 | err = devlink_nl_trap_fill(msg, devlink, trap_item, |
| 5572 | DEVLINK_CMD_TRAP_NEW, |
| 5573 | NETLINK_CB(cb->skb).portid, |
| 5574 | cb->nlh->nlmsg_seq, |
| 5575 | NLM_F_MULTI); |
| 5576 | if (err) { |
| 5577 | mutex_unlock(&devlink->lock); |
| 5578 | goto out; |
| 5579 | } |
| 5580 | idx++; |
| 5581 | } |
| 5582 | mutex_unlock(&devlink->lock); |
| 5583 | } |
| 5584 | out: |
| 5585 | mutex_unlock(&devlink_mutex); |
| 5586 | |
| 5587 | cb->args[0] = idx; |
| 5588 | return msg->len; |
| 5589 | } |
| 5590 | |
| 5591 | static int __devlink_trap_action_set(struct devlink *devlink, |
| 5592 | struct devlink_trap_item *trap_item, |
| 5593 | enum devlink_trap_action trap_action, |
| 5594 | struct netlink_ext_ack *extack) |
| 5595 | { |
| 5596 | int err; |
| 5597 | |
| 5598 | if (trap_item->action != trap_action && |
| 5599 | trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) { |
| 5600 | NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping"); |
| 5601 | return 0; |
| 5602 | } |
| 5603 | |
| 5604 | err = devlink->ops->trap_action_set(devlink, trap_item->trap, |
| 5605 | trap_action); |
| 5606 | if (err) |
| 5607 | return err; |
| 5608 | |
| 5609 | trap_item->action = trap_action; |
| 5610 | |
| 5611 | return 0; |
| 5612 | } |
| 5613 | |
| 5614 | static int devlink_trap_action_set(struct devlink *devlink, |
| 5615 | struct devlink_trap_item *trap_item, |
| 5616 | struct genl_info *info) |
| 5617 | { |
| 5618 | enum devlink_trap_action trap_action; |
| 5619 | int err; |
| 5620 | |
| 5621 | if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION]) |
| 5622 | return 0; |
| 5623 | |
| 5624 | err = devlink_trap_action_get_from_info(info, &trap_action); |
| 5625 | if (err) { |
| 5626 | NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action"); |
| 5627 | return -EINVAL; |
| 5628 | } |
| 5629 | |
| 5630 | return __devlink_trap_action_set(devlink, trap_item, trap_action, |
| 5631 | info->extack); |
| 5632 | } |
| 5633 | |
| 5634 | static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb, |
| 5635 | struct genl_info *info) |
| 5636 | { |
| 5637 | struct netlink_ext_ack *extack = info->extack; |
| 5638 | struct devlink *devlink = info->user_ptr[0]; |
| 5639 | struct devlink_trap_item *trap_item; |
| 5640 | int err; |
| 5641 | |
| 5642 | if (list_empty(&devlink->trap_list)) |
| 5643 | return -EOPNOTSUPP; |
| 5644 | |
| 5645 | trap_item = devlink_trap_item_get_from_info(devlink, info); |
| 5646 | if (!trap_item) { |
| 5647 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap"); |
| 5648 | return -ENOENT; |
| 5649 | } |
| 5650 | |
| 5651 | err = devlink_trap_action_set(devlink, trap_item, info); |
| 5652 | if (err) |
| 5653 | return err; |
| 5654 | |
| 5655 | return 0; |
| 5656 | } |
| 5657 | |
| 5658 | static struct devlink_trap_group_item * |
| 5659 | devlink_trap_group_item_lookup(struct devlink *devlink, const char *name) |
| 5660 | { |
| 5661 | struct devlink_trap_group_item *group_item; |
| 5662 | |
| 5663 | list_for_each_entry(group_item, &devlink->trap_group_list, list) { |
| 5664 | if (!strcmp(group_item->group->name, name)) |
| 5665 | return group_item; |
| 5666 | } |
| 5667 | |
| 5668 | return NULL; |
| 5669 | } |
| 5670 | |
| 5671 | static struct devlink_trap_group_item * |
| 5672 | devlink_trap_group_item_get_from_info(struct devlink *devlink, |
| 5673 | struct genl_info *info) |
| 5674 | { |
| 5675 | char *name; |
| 5676 | |
| 5677 | if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]) |
| 5678 | return NULL; |
| 5679 | name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]); |
| 5680 | |
| 5681 | return devlink_trap_group_item_lookup(devlink, name); |
| 5682 | } |
| 5683 | |
| 5684 | static int |
| 5685 | devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink, |
| 5686 | const struct devlink_trap_group_item *group_item, |
| 5687 | enum devlink_command cmd, u32 portid, u32 seq, |
| 5688 | int flags) |
| 5689 | { |
| 5690 | void *hdr; |
| 5691 | int err; |
| 5692 | |
| 5693 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 5694 | if (!hdr) |
| 5695 | return -EMSGSIZE; |
| 5696 | |
| 5697 | if (devlink_nl_put_handle(msg, devlink)) |
| 5698 | goto nla_put_failure; |
| 5699 | |
| 5700 | if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME, |
| 5701 | group_item->group->name)) |
| 5702 | goto nla_put_failure; |
| 5703 | |
| 5704 | if (group_item->group->generic && |
| 5705 | nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC)) |
| 5706 | goto nla_put_failure; |
| 5707 | |
| 5708 | err = devlink_trap_stats_put(msg, group_item->stats); |
| 5709 | if (err) |
| 5710 | goto nla_put_failure; |
| 5711 | |
| 5712 | genlmsg_end(msg, hdr); |
| 5713 | |
| 5714 | return 0; |
| 5715 | |
| 5716 | nla_put_failure: |
| 5717 | genlmsg_cancel(msg, hdr); |
| 5718 | return -EMSGSIZE; |
| 5719 | } |
| 5720 | |
| 5721 | static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb, |
| 5722 | struct genl_info *info) |
| 5723 | { |
| 5724 | struct netlink_ext_ack *extack = info->extack; |
| 5725 | struct devlink *devlink = info->user_ptr[0]; |
| 5726 | struct devlink_trap_group_item *group_item; |
| 5727 | struct sk_buff *msg; |
| 5728 | int err; |
| 5729 | |
| 5730 | if (list_empty(&devlink->trap_group_list)) |
| 5731 | return -EOPNOTSUPP; |
| 5732 | |
| 5733 | group_item = devlink_trap_group_item_get_from_info(devlink, info); |
| 5734 | if (!group_item) { |
| 5735 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group"); |
| 5736 | return -ENOENT; |
| 5737 | } |
| 5738 | |
| 5739 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 5740 | if (!msg) |
| 5741 | return -ENOMEM; |
| 5742 | |
| 5743 | err = devlink_nl_trap_group_fill(msg, devlink, group_item, |
| 5744 | DEVLINK_CMD_TRAP_GROUP_NEW, |
| 5745 | info->snd_portid, info->snd_seq, 0); |
| 5746 | if (err) |
| 5747 | goto err_trap_group_fill; |
| 5748 | |
| 5749 | return genlmsg_reply(msg, info); |
| 5750 | |
| 5751 | err_trap_group_fill: |
| 5752 | nlmsg_free(msg); |
| 5753 | return err; |
| 5754 | } |
| 5755 | |
| 5756 | static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg, |
| 5757 | struct netlink_callback *cb) |
| 5758 | { |
| 5759 | enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW; |
| 5760 | struct devlink_trap_group_item *group_item; |
| 5761 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 5762 | struct devlink *devlink; |
| 5763 | int start = cb->args[0]; |
| 5764 | int idx = 0; |
| 5765 | int err; |
| 5766 | |
| 5767 | mutex_lock(&devlink_mutex); |
| 5768 | list_for_each_entry(devlink, &devlink_list, list) { |
| 5769 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 5770 | continue; |
| 5771 | mutex_lock(&devlink->lock); |
| 5772 | list_for_each_entry(group_item, &devlink->trap_group_list, |
| 5773 | list) { |
| 5774 | if (idx < start) { |
| 5775 | idx++; |
| 5776 | continue; |
| 5777 | } |
| 5778 | err = devlink_nl_trap_group_fill(msg, devlink, |
| 5779 | group_item, cmd, |
| 5780 | portid, |
| 5781 | cb->nlh->nlmsg_seq, |
| 5782 | NLM_F_MULTI); |
| 5783 | if (err) { |
| 5784 | mutex_unlock(&devlink->lock); |
| 5785 | goto out; |
| 5786 | } |
| 5787 | idx++; |
| 5788 | } |
| 5789 | mutex_unlock(&devlink->lock); |
| 5790 | } |
| 5791 | out: |
| 5792 | mutex_unlock(&devlink_mutex); |
| 5793 | |
| 5794 | cb->args[0] = idx; |
| 5795 | return msg->len; |
| 5796 | } |
| 5797 | |
| 5798 | static int |
| 5799 | __devlink_trap_group_action_set(struct devlink *devlink, |
| 5800 | struct devlink_trap_group_item *group_item, |
| 5801 | enum devlink_trap_action trap_action, |
| 5802 | struct netlink_ext_ack *extack) |
| 5803 | { |
| 5804 | const char *group_name = group_item->group->name; |
| 5805 | struct devlink_trap_item *trap_item; |
| 5806 | int err; |
| 5807 | |
| 5808 | list_for_each_entry(trap_item, &devlink->trap_list, list) { |
| 5809 | if (strcmp(trap_item->trap->group.name, group_name)) |
| 5810 | continue; |
| 5811 | err = __devlink_trap_action_set(devlink, trap_item, |
| 5812 | trap_action, extack); |
| 5813 | if (err) |
| 5814 | return err; |
| 5815 | } |
| 5816 | |
| 5817 | return 0; |
| 5818 | } |
| 5819 | |
| 5820 | static int |
| 5821 | devlink_trap_group_action_set(struct devlink *devlink, |
| 5822 | struct devlink_trap_group_item *group_item, |
| 5823 | struct genl_info *info) |
| 5824 | { |
| 5825 | enum devlink_trap_action trap_action; |
| 5826 | int err; |
| 5827 | |
| 5828 | if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION]) |
| 5829 | return 0; |
| 5830 | |
| 5831 | err = devlink_trap_action_get_from_info(info, &trap_action); |
| 5832 | if (err) { |
| 5833 | NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action"); |
| 5834 | return -EINVAL; |
| 5835 | } |
| 5836 | |
| 5837 | err = __devlink_trap_group_action_set(devlink, group_item, trap_action, |
| 5838 | info->extack); |
| 5839 | if (err) |
| 5840 | return err; |
| 5841 | |
| 5842 | return 0; |
| 5843 | } |
| 5844 | |
| 5845 | static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb, |
| 5846 | struct genl_info *info) |
| 5847 | { |
| 5848 | struct netlink_ext_ack *extack = info->extack; |
| 5849 | struct devlink *devlink = info->user_ptr[0]; |
| 5850 | struct devlink_trap_group_item *group_item; |
| 5851 | int err; |
| 5852 | |
| 5853 | if (list_empty(&devlink->trap_group_list)) |
| 5854 | return -EOPNOTSUPP; |
| 5855 | |
| 5856 | group_item = devlink_trap_group_item_get_from_info(devlink, info); |
| 5857 | if (!group_item) { |
| 5858 | NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group"); |
| 5859 | return -ENOENT; |
| 5860 | } |
| 5861 | |
| 5862 | err = devlink_trap_group_action_set(devlink, group_item, info); |
| 5863 | if (err) |
| 5864 | return err; |
| 5865 | |
| 5866 | return 0; |
| 5867 | } |
| 5868 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5869 | static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = { |
| 5870 | [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING }, |
| 5871 | [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING }, |
| 5872 | [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 }, |
| 5873 | [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 }, |
| 5874 | [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5875 | [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 }, |
| 5876 | [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 }, |
| 5877 | [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 }, |
| 5878 | [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 }, |
| 5879 | [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 }, |
| 5880 | [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 }, |
| 5881 | [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 5882 | [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 }, |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 5883 | [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 }, |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 5884 | [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 5885 | [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING }, |
| 5886 | [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 5887 | [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64}, |
| 5888 | [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64}, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 5889 | [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING }, |
| 5890 | [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 }, |
| 5891 | [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 }, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 5892 | [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING }, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 5893 | [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 }, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 5894 | [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING }, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 5895 | [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 }, |
| 5896 | [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 }, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 5897 | [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING }, |
| 5898 | [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING }, |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 5899 | [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING }, |
| 5900 | [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 }, |
| 5901 | [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING }, |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 5902 | [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 }, |
| 5903 | [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 }, |
| 5904 | [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5905 | }; |
| 5906 | |
| 5907 | static const struct genl_ops devlink_nl_ops[] = { |
| 5908 | { |
| 5909 | .cmd = DEVLINK_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5910 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5911 | .doit = devlink_nl_cmd_get_doit, |
| 5912 | .dumpit = devlink_nl_cmd_get_dumpit, |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 5913 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5914 | /* can be retrieved by unprivileged users */ |
| 5915 | }, |
| 5916 | { |
| 5917 | .cmd = DEVLINK_CMD_PORT_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5918 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5919 | .doit = devlink_nl_cmd_port_get_doit, |
| 5920 | .dumpit = devlink_nl_cmd_port_get_dumpit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5921 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 5922 | /* can be retrieved by unprivileged users */ |
| 5923 | }, |
| 5924 | { |
| 5925 | .cmd = DEVLINK_CMD_PORT_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5926 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5927 | .doit = devlink_nl_cmd_port_set_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5928 | .flags = GENL_ADMIN_PERM, |
| 5929 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 5930 | }, |
| 5931 | { |
| 5932 | .cmd = DEVLINK_CMD_PORT_SPLIT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5933 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5934 | .doit = devlink_nl_cmd_port_split_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5935 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5936 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5937 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5938 | }, |
| 5939 | { |
| 5940 | .cmd = DEVLINK_CMD_PORT_UNSPLIT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5941 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5942 | .doit = devlink_nl_cmd_port_unsplit_doit, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5943 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 5944 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5945 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 5946 | }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5947 | { |
| 5948 | .cmd = DEVLINK_CMD_SB_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5949 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5950 | .doit = devlink_nl_cmd_sb_get_doit, |
| 5951 | .dumpit = devlink_nl_cmd_sb_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5952 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5953 | DEVLINK_NL_FLAG_NEED_SB, |
| 5954 | /* can be retrieved by unprivileged users */ |
| 5955 | }, |
| 5956 | { |
| 5957 | .cmd = DEVLINK_CMD_SB_POOL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5958 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5959 | .doit = devlink_nl_cmd_sb_pool_get_doit, |
| 5960 | .dumpit = devlink_nl_cmd_sb_pool_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5961 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5962 | DEVLINK_NL_FLAG_NEED_SB, |
| 5963 | /* can be retrieved by unprivileged users */ |
| 5964 | }, |
| 5965 | { |
| 5966 | .cmd = DEVLINK_CMD_SB_POOL_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5967 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5968 | .doit = devlink_nl_cmd_sb_pool_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5969 | .flags = GENL_ADMIN_PERM, |
| 5970 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 5971 | DEVLINK_NL_FLAG_NEED_SB, |
| 5972 | }, |
| 5973 | { |
| 5974 | .cmd = DEVLINK_CMD_SB_PORT_POOL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5975 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5976 | .doit = devlink_nl_cmd_sb_port_pool_get_doit, |
| 5977 | .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5978 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 5979 | DEVLINK_NL_FLAG_NEED_SB, |
| 5980 | /* can be retrieved by unprivileged users */ |
| 5981 | }, |
| 5982 | { |
| 5983 | .cmd = DEVLINK_CMD_SB_PORT_POOL_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5984 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5985 | .doit = devlink_nl_cmd_sb_port_pool_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5986 | .flags = GENL_ADMIN_PERM, |
| 5987 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 5988 | DEVLINK_NL_FLAG_NEED_SB, |
| 5989 | }, |
| 5990 | { |
| 5991 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 5992 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5993 | .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit, |
| 5994 | .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 5995 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 5996 | DEVLINK_NL_FLAG_NEED_SB, |
| 5997 | /* can be retrieved by unprivileged users */ |
| 5998 | }, |
| 5999 | { |
| 6000 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6001 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6002 | .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6003 | .flags = GENL_ADMIN_PERM, |
| 6004 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 6005 | DEVLINK_NL_FLAG_NEED_SB, |
| 6006 | }, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6007 | { |
| 6008 | .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6009 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6010 | .doit = devlink_nl_cmd_sb_occ_snapshot_doit, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6011 | .flags = GENL_ADMIN_PERM, |
| 6012 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6013 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6014 | }, |
| 6015 | { |
| 6016 | .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6017 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6018 | .doit = devlink_nl_cmd_sb_occ_max_clear_doit, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6019 | .flags = GENL_ADMIN_PERM, |
| 6020 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6021 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 6022 | }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6023 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 6024 | .cmd = DEVLINK_CMD_ESWITCH_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6025 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 6026 | .doit = devlink_nl_cmd_eswitch_get_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6027 | .flags = GENL_ADMIN_PERM, |
| 6028 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6029 | }, |
| 6030 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 6031 | .cmd = DEVLINK_CMD_ESWITCH_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6032 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 6033 | .doit = devlink_nl_cmd_eswitch_set_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6034 | .flags = GENL_ADMIN_PERM, |
Jakub Kicinski | 7ac1cc9 | 2018-05-21 22:12:50 -0700 | [diff] [blame] | 6035 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6036 | DEVLINK_NL_FLAG_NO_LOCK, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 6037 | }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6038 | { |
| 6039 | .cmd = DEVLINK_CMD_DPIPE_TABLE_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6040 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6041 | .doit = devlink_nl_cmd_dpipe_table_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6042 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 6043 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6044 | }, |
| 6045 | { |
| 6046 | .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6047 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6048 | .doit = devlink_nl_cmd_dpipe_entries_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6049 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 6050 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6051 | }, |
| 6052 | { |
| 6053 | .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6054 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6055 | .doit = devlink_nl_cmd_dpipe_headers_get, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6056 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 6057 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6058 | }, |
| 6059 | { |
| 6060 | .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6061 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6062 | .doit = devlink_nl_cmd_dpipe_table_counters_set, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6063 | .flags = GENL_ADMIN_PERM, |
| 6064 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6065 | }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6066 | { |
| 6067 | .cmd = DEVLINK_CMD_RESOURCE_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6068 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6069 | .doit = devlink_nl_cmd_resource_set, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6070 | .flags = GENL_ADMIN_PERM, |
| 6071 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6072 | }, |
| 6073 | { |
| 6074 | .cmd = DEVLINK_CMD_RESOURCE_DUMP, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6075 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6076 | .doit = devlink_nl_cmd_resource_dump, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6077 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 6078 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6079 | }, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 6080 | { |
| 6081 | .cmd = DEVLINK_CMD_RELOAD, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6082 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 6083 | .doit = devlink_nl_cmd_reload, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 6084 | .flags = GENL_ADMIN_PERM, |
| 6085 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6086 | DEVLINK_NL_FLAG_NO_LOCK, |
| 6087 | }, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 6088 | { |
| 6089 | .cmd = DEVLINK_CMD_PARAM_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6090 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 6091 | .doit = devlink_nl_cmd_param_get_doit, |
| 6092 | .dumpit = devlink_nl_cmd_param_get_dumpit, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 6093 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6094 | /* can be retrieved by unprivileged users */ |
| 6095 | }, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 6096 | { |
| 6097 | .cmd = DEVLINK_CMD_PARAM_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6098 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 6099 | .doit = devlink_nl_cmd_param_set_doit, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 6100 | .flags = GENL_ADMIN_PERM, |
| 6101 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6102 | }, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6103 | { |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 6104 | .cmd = DEVLINK_CMD_PORT_PARAM_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6105 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 6106 | .doit = devlink_nl_cmd_port_param_get_doit, |
| 6107 | .dumpit = devlink_nl_cmd_port_param_get_dumpit, |
Vasundhara Volam | f4601de | 2019-01-28 18:00:21 +0530 | [diff] [blame] | 6108 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 6109 | /* can be retrieved by unprivileged users */ |
| 6110 | }, |
| 6111 | { |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 6112 | .cmd = DEVLINK_CMD_PORT_PARAM_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6113 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 6114 | .doit = devlink_nl_cmd_port_param_set_doit, |
Vasundhara Volam | 9c54873 | 2019-01-28 18:00:22 +0530 | [diff] [blame] | 6115 | .flags = GENL_ADMIN_PERM, |
| 6116 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 6117 | }, |
| 6118 | { |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6119 | .cmd = DEVLINK_CMD_REGION_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6120 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6121 | .doit = devlink_nl_cmd_region_get_doit, |
| 6122 | .dumpit = devlink_nl_cmd_region_get_dumpit, |
Alex Vesker | d8db7ea5 | 2018-07-12 15:13:11 +0300 | [diff] [blame] | 6123 | .flags = GENL_ADMIN_PERM, |
| 6124 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6125 | }, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6126 | { |
| 6127 | .cmd = DEVLINK_CMD_REGION_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6128 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6129 | .doit = devlink_nl_cmd_region_del, |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 6130 | .flags = GENL_ADMIN_PERM, |
| 6131 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6132 | }, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 6133 | { |
| 6134 | .cmd = DEVLINK_CMD_REGION_READ, |
Jiri Pirko | ee85da5 | 2019-10-05 20:04:42 +0200 | [diff] [blame] | 6135 | .validate = GENL_DONT_VALIDATE_STRICT | |
| 6136 | GENL_DONT_VALIDATE_DUMP_STRICT, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 6137 | .dumpit = devlink_nl_cmd_region_read_dumpit, |
Alex Vesker | 4e54795 | 2018-07-12 15:13:14 +0300 | [diff] [blame] | 6138 | .flags = GENL_ADMIN_PERM, |
| 6139 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6140 | }, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 6141 | { |
| 6142 | .cmd = DEVLINK_CMD_INFO_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6143 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 6144 | .doit = devlink_nl_cmd_info_get_doit, |
| 6145 | .dumpit = devlink_nl_cmd_info_get_dumpit, |
Jakub Kicinski | f9cf228 | 2019-01-31 10:50:40 -0800 | [diff] [blame] | 6146 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6147 | /* can be retrieved by unprivileged users */ |
| 6148 | }, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 6149 | { |
| 6150 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6151 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 6152 | .doit = devlink_nl_cmd_health_reporter_get_doit, |
| 6153 | .dumpit = devlink_nl_cmd_health_reporter_get_dumpit, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6154 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6155 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | 7afe335a | 2019-02-07 11:36:35 +0200 | [diff] [blame] | 6156 | /* can be retrieved by unprivileged users */ |
| 6157 | }, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6158 | { |
| 6159 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6160 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6161 | .doit = devlink_nl_cmd_health_reporter_set_doit, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6162 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6163 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6164 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | a1e55ec | 2019-02-07 11:36:36 +0200 | [diff] [blame] | 6165 | }, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 6166 | { |
| 6167 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6168 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 6169 | .doit = devlink_nl_cmd_health_reporter_recover_doit, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 6170 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6171 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6172 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | 20a0943 | 2019-02-07 11:36:37 +0200 | [diff] [blame] | 6173 | }, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 6174 | { |
| 6175 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6176 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 6177 | .doit = devlink_nl_cmd_health_reporter_diagnose_doit, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 6178 | .flags = GENL_ADMIN_PERM, |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6179 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6180 | DEVLINK_NL_FLAG_NO_LOCK, |
Eran Ben Elisha | fca42a2 | 2019-02-07 11:36:38 +0200 | [diff] [blame] | 6181 | }, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 6182 | { |
| 6183 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET, |
Jiri Pirko | 82a843d | 2019-10-07 09:28:31 +0200 | [diff] [blame] | 6184 | .validate = GENL_DONT_VALIDATE_STRICT | |
| 6185 | GENL_DONT_VALIDATE_DUMP_STRICT, |
Aya Levin | e44ef4e | 2019-05-16 09:49:20 +0300 | [diff] [blame] | 6186 | .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 6187 | .flags = GENL_ADMIN_PERM, |
| 6188 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6189 | DEVLINK_NL_FLAG_NO_LOCK, |
| 6190 | }, |
| 6191 | { |
| 6192 | .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6193 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 6194 | .doit = devlink_nl_cmd_health_reporter_dump_clear_doit, |
Eran Ben Elisha | 35455e2 | 2019-02-07 11:36:39 +0200 | [diff] [blame] | 6195 | .flags = GENL_ADMIN_PERM, |
| 6196 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 6197 | DEVLINK_NL_FLAG_NO_LOCK, |
| 6198 | }, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 6199 | { |
| 6200 | .cmd = DEVLINK_CMD_FLASH_UPDATE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 6201 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 6202 | .doit = devlink_nl_cmd_flash_update, |
Jakub Kicinski | 76726cc | 2019-02-14 13:40:44 -0800 | [diff] [blame] | 6203 | .flags = GENL_ADMIN_PERM, |
| 6204 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6205 | }, |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6206 | { |
| 6207 | .cmd = DEVLINK_CMD_TRAP_GET, |
| 6208 | .doit = devlink_nl_cmd_trap_get_doit, |
| 6209 | .dumpit = devlink_nl_cmd_trap_get_dumpit, |
| 6210 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6211 | /* can be retrieved by unprivileged users */ |
| 6212 | }, |
| 6213 | { |
| 6214 | .cmd = DEVLINK_CMD_TRAP_SET, |
| 6215 | .doit = devlink_nl_cmd_trap_set_doit, |
| 6216 | .flags = GENL_ADMIN_PERM, |
| 6217 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6218 | }, |
| 6219 | { |
| 6220 | .cmd = DEVLINK_CMD_TRAP_GROUP_GET, |
| 6221 | .doit = devlink_nl_cmd_trap_group_get_doit, |
| 6222 | .dumpit = devlink_nl_cmd_trap_group_get_dumpit, |
| 6223 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6224 | /* can be retrieved by unprivileged users */ |
| 6225 | }, |
| 6226 | { |
| 6227 | .cmd = DEVLINK_CMD_TRAP_GROUP_SET, |
| 6228 | .doit = devlink_nl_cmd_trap_group_set_doit, |
| 6229 | .flags = GENL_ADMIN_PERM, |
| 6230 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 6231 | }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6232 | }; |
| 6233 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 6234 | static struct genl_family devlink_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 6235 | .name = DEVLINK_GENL_NAME, |
| 6236 | .version = DEVLINK_GENL_VERSION, |
| 6237 | .maxattr = DEVLINK_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 6238 | .policy = devlink_nl_policy, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 6239 | .netnsok = true, |
| 6240 | .pre_doit = devlink_nl_pre_doit, |
| 6241 | .post_doit = devlink_nl_post_doit, |
| 6242 | .module = THIS_MODULE, |
| 6243 | .ops = devlink_nl_ops, |
| 6244 | .n_ops = ARRAY_SIZE(devlink_nl_ops), |
| 6245 | .mcgrps = devlink_nl_mcgrps, |
| 6246 | .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps), |
| 6247 | }; |
| 6248 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6249 | /** |
| 6250 | * devlink_alloc - Allocate new devlink instance resources |
| 6251 | * |
| 6252 | * @ops: ops |
| 6253 | * @priv_size: size of user private data |
| 6254 | * |
| 6255 | * Allocate new devlink instance resources, including devlink index |
| 6256 | * and name. |
| 6257 | */ |
| 6258 | struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) |
| 6259 | { |
| 6260 | struct devlink *devlink; |
| 6261 | |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 6262 | if (WARN_ON(!ops)) |
| 6263 | return NULL; |
| 6264 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6265 | devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL); |
| 6266 | if (!devlink) |
| 6267 | return NULL; |
| 6268 | devlink->ops = ops; |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 6269 | __devlink_net_set(devlink, &init_net); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6270 | INIT_LIST_HEAD(&devlink->port_list); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6271 | INIT_LIST_HEAD(&devlink->sb_list); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6272 | INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6273 | INIT_LIST_HEAD(&devlink->resource_list); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 6274 | INIT_LIST_HEAD(&devlink->param_list); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 6275 | INIT_LIST_HEAD(&devlink->region_list); |
Eran Ben Elisha | a0bdcc5 | 2019-02-07 11:36:33 +0200 | [diff] [blame] | 6276 | INIT_LIST_HEAD(&devlink->reporter_list); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6277 | INIT_LIST_HEAD(&devlink->trap_list); |
| 6278 | INIT_LIST_HEAD(&devlink->trap_group_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6279 | mutex_init(&devlink->lock); |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6280 | mutex_init(&devlink->reporters_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6281 | return devlink; |
| 6282 | } |
| 6283 | EXPORT_SYMBOL_GPL(devlink_alloc); |
| 6284 | |
| 6285 | /** |
| 6286 | * devlink_register - Register devlink instance |
| 6287 | * |
| 6288 | * @devlink: devlink |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 6289 | * @dev: parent device |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6290 | */ |
| 6291 | int devlink_register(struct devlink *devlink, struct device *dev) |
| 6292 | { |
| 6293 | mutex_lock(&devlink_mutex); |
| 6294 | devlink->dev = dev; |
Jiri Pirko | 8273fd8 | 2019-10-05 08:10:31 +0200 | [diff] [blame] | 6295 | devlink->registered = true; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6296 | list_add_tail(&devlink->list, &devlink_list); |
| 6297 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 6298 | mutex_unlock(&devlink_mutex); |
| 6299 | return 0; |
| 6300 | } |
| 6301 | EXPORT_SYMBOL_GPL(devlink_register); |
| 6302 | |
| 6303 | /** |
| 6304 | * devlink_unregister - Unregister devlink instance |
| 6305 | * |
| 6306 | * @devlink: devlink |
| 6307 | */ |
| 6308 | void devlink_unregister(struct devlink *devlink) |
| 6309 | { |
| 6310 | mutex_lock(&devlink_mutex); |
| 6311 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 6312 | list_del(&devlink->list); |
| 6313 | mutex_unlock(&devlink_mutex); |
| 6314 | } |
| 6315 | EXPORT_SYMBOL_GPL(devlink_unregister); |
| 6316 | |
| 6317 | /** |
| 6318 | * devlink_free - Free devlink instance resources |
| 6319 | * |
| 6320 | * @devlink: devlink |
| 6321 | */ |
| 6322 | void devlink_free(struct devlink *devlink) |
| 6323 | { |
Moshe Shemesh | b587bda | 2019-04-29 12:41:45 +0300 | [diff] [blame] | 6324 | mutex_destroy(&devlink->reporters_lock); |
Jiri Pirko | 375cf8c | 2019-03-24 11:14:24 +0100 | [diff] [blame] | 6325 | mutex_destroy(&devlink->lock); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6326 | WARN_ON(!list_empty(&devlink->trap_group_list)); |
| 6327 | WARN_ON(!list_empty(&devlink->trap_list)); |
Parav Pandit | b904aad | 2019-02-08 15:15:00 -0600 | [diff] [blame] | 6328 | WARN_ON(!list_empty(&devlink->reporter_list)); |
| 6329 | WARN_ON(!list_empty(&devlink->region_list)); |
| 6330 | WARN_ON(!list_empty(&devlink->param_list)); |
| 6331 | WARN_ON(!list_empty(&devlink->resource_list)); |
| 6332 | WARN_ON(!list_empty(&devlink->dpipe_table_list)); |
| 6333 | WARN_ON(!list_empty(&devlink->sb_list)); |
| 6334 | WARN_ON(!list_empty(&devlink->port_list)); |
| 6335 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6336 | kfree(devlink); |
| 6337 | } |
| 6338 | EXPORT_SYMBOL_GPL(devlink_free); |
| 6339 | |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 6340 | static void devlink_port_type_warn(struct work_struct *work) |
| 6341 | { |
| 6342 | WARN(true, "Type was not set for devlink port."); |
| 6343 | } |
| 6344 | |
| 6345 | static bool devlink_port_type_should_warn(struct devlink_port *devlink_port) |
| 6346 | { |
| 6347 | /* Ignore CPU and DSA flavours. */ |
| 6348 | return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU && |
| 6349 | devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA; |
| 6350 | } |
| 6351 | |
| 6352 | #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 30) |
| 6353 | |
| 6354 | static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port) |
| 6355 | { |
| 6356 | if (!devlink_port_type_should_warn(devlink_port)) |
| 6357 | return; |
| 6358 | /* Schedule a work to WARN in case driver does not set port |
| 6359 | * type within timeout. |
| 6360 | */ |
| 6361 | schedule_delayed_work(&devlink_port->type_warn_dw, |
| 6362 | DEVLINK_PORT_TYPE_WARN_TIMEOUT); |
| 6363 | } |
| 6364 | |
| 6365 | static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port) |
| 6366 | { |
| 6367 | if (!devlink_port_type_should_warn(devlink_port)) |
| 6368 | return; |
| 6369 | cancel_delayed_work_sync(&devlink_port->type_warn_dw); |
| 6370 | } |
| 6371 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6372 | /** |
| 6373 | * devlink_port_register - Register devlink port |
| 6374 | * |
| 6375 | * @devlink: devlink |
| 6376 | * @devlink_port: devlink port |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 6377 | * @port_index: driver-specific numerical identifier of the port |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6378 | * |
| 6379 | * Register devlink port with provided port index. User can use |
| 6380 | * any indexing, even hw-related one. devlink_port structure |
| 6381 | * is convenient to be embedded inside user driver private structure. |
| 6382 | * Note that the caller should take care of zeroing the devlink_port |
| 6383 | * structure. |
| 6384 | */ |
| 6385 | int devlink_port_register(struct devlink *devlink, |
| 6386 | struct devlink_port *devlink_port, |
| 6387 | unsigned int port_index) |
| 6388 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6389 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6390 | if (devlink_port_index_exists(devlink, port_index)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6391 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6392 | return -EEXIST; |
| 6393 | } |
| 6394 | devlink_port->devlink = devlink; |
| 6395 | devlink_port->index = port_index; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6396 | devlink_port->registered = true; |
Jiri Pirko | b8f9755 | 2019-03-24 11:14:37 +0100 | [diff] [blame] | 6397 | spin_lock_init(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6398 | list_add_tail(&devlink_port->list, &devlink->port_list); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 6399 | INIT_LIST_HEAD(&devlink_port->param_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6400 | mutex_unlock(&devlink->lock); |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 6401 | INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn); |
| 6402 | devlink_port_type_warn_schedule(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6403 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 6404 | return 0; |
| 6405 | } |
| 6406 | EXPORT_SYMBOL_GPL(devlink_port_register); |
| 6407 | |
| 6408 | /** |
| 6409 | * devlink_port_unregister - Unregister devlink port |
| 6410 | * |
| 6411 | * @devlink_port: devlink port |
| 6412 | */ |
| 6413 | void devlink_port_unregister(struct devlink_port *devlink_port) |
| 6414 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6415 | struct devlink *devlink = devlink_port->devlink; |
| 6416 | |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 6417 | devlink_port_type_warn_cancel(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6418 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6419 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6420 | list_del(&devlink_port->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6421 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6422 | } |
| 6423 | EXPORT_SYMBOL_GPL(devlink_port_unregister); |
| 6424 | |
| 6425 | static void __devlink_port_type_set(struct devlink_port *devlink_port, |
| 6426 | enum devlink_port_type type, |
| 6427 | void *type_dev) |
| 6428 | { |
Jiri Pirko | 2b239e7 | 2019-03-24 11:14:36 +0100 | [diff] [blame] | 6429 | if (WARN_ON(!devlink_port->registered)) |
| 6430 | return; |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 6431 | devlink_port_type_warn_cancel(devlink_port); |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6432 | spin_lock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6433 | devlink_port->type = type; |
| 6434 | devlink_port->type_dev = type_dev; |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 6435 | spin_unlock_bh(&devlink_port->type_lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6436 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 6437 | } |
| 6438 | |
| 6439 | /** |
| 6440 | * devlink_port_type_eth_set - Set port type to Ethernet |
| 6441 | * |
| 6442 | * @devlink_port: devlink port |
| 6443 | * @netdev: related netdevice |
| 6444 | */ |
| 6445 | void devlink_port_type_eth_set(struct devlink_port *devlink_port, |
| 6446 | struct net_device *netdev) |
| 6447 | { |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 6448 | const struct net_device_ops *ops = netdev->netdev_ops; |
| 6449 | |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 6450 | /* If driver registers devlink port, it should set devlink port |
| 6451 | * attributes accordingly so the compat functions are called |
| 6452 | * and the original ops are not used. |
| 6453 | */ |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 6454 | if (ops->ndo_get_phys_port_name) { |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 6455 | /* Some drivers use the same set of ndos for netdevs |
| 6456 | * that have devlink_port registered and also for |
| 6457 | * those who don't. Make sure that ndo_get_phys_port_name |
| 6458 | * returns -EOPNOTSUPP here in case it is defined. |
| 6459 | * Warn if not. |
| 6460 | */ |
Jiri Pirko | 746364f | 2019-03-28 13:56:46 +0100 | [diff] [blame] | 6461 | char name[IFNAMSIZ]; |
| 6462 | int err; |
| 6463 | |
| 6464 | err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name)); |
| 6465 | WARN_ON(err != -EOPNOTSUPP); |
| 6466 | } |
Jiri Pirko | 119c0b5 | 2019-04-03 14:24:27 +0200 | [diff] [blame] | 6467 | if (ops->ndo_get_port_parent_id) { |
| 6468 | /* Some drivers use the same set of ndos for netdevs |
| 6469 | * that have devlink_port registered and also for |
| 6470 | * those who don't. Make sure that ndo_get_port_parent_id |
| 6471 | * returns -EOPNOTSUPP here in case it is defined. |
| 6472 | * Warn if not. |
| 6473 | */ |
| 6474 | struct netdev_phys_item_id ppid; |
| 6475 | int err; |
| 6476 | |
| 6477 | err = ops->ndo_get_port_parent_id(netdev, &ppid); |
| 6478 | WARN_ON(err != -EOPNOTSUPP); |
| 6479 | } |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 6480 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6481 | } |
| 6482 | EXPORT_SYMBOL_GPL(devlink_port_type_eth_set); |
| 6483 | |
| 6484 | /** |
| 6485 | * devlink_port_type_ib_set - Set port type to InfiniBand |
| 6486 | * |
| 6487 | * @devlink_port: devlink port |
| 6488 | * @ibdev: related IB device |
| 6489 | */ |
| 6490 | void devlink_port_type_ib_set(struct devlink_port *devlink_port, |
| 6491 | struct ib_device *ibdev) |
| 6492 | { |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 6493 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6494 | } |
| 6495 | EXPORT_SYMBOL_GPL(devlink_port_type_ib_set); |
| 6496 | |
| 6497 | /** |
| 6498 | * devlink_port_type_clear - Clear port type |
| 6499 | * |
| 6500 | * @devlink_port: devlink port |
| 6501 | */ |
| 6502 | void devlink_port_type_clear(struct devlink_port *devlink_port) |
| 6503 | { |
Jiri Pirko | 773b1f3 | 2019-03-24 11:14:30 +0100 | [diff] [blame] | 6504 | __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL); |
Jiri Pirko | 136bf27 | 2019-05-23 10:43:35 +0200 | [diff] [blame] | 6505 | devlink_port_type_warn_schedule(devlink_port); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6506 | } |
| 6507 | EXPORT_SYMBOL_GPL(devlink_port_type_clear); |
| 6508 | |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 6509 | static int __devlink_port_attrs_set(struct devlink_port *devlink_port, |
| 6510 | enum devlink_port_flavour flavour, |
| 6511 | const unsigned char *switch_id, |
| 6512 | unsigned char switch_id_len) |
| 6513 | { |
| 6514 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 6515 | |
| 6516 | if (WARN_ON(devlink_port->registered)) |
| 6517 | return -EEXIST; |
| 6518 | attrs->set = true; |
| 6519 | attrs->flavour = flavour; |
| 6520 | if (switch_id) { |
| 6521 | attrs->switch_port = true; |
| 6522 | if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN)) |
| 6523 | switch_id_len = MAX_PHYS_ITEM_ID_LEN; |
| 6524 | memcpy(attrs->switch_id.id, switch_id, switch_id_len); |
| 6525 | attrs->switch_id.id_len = switch_id_len; |
| 6526 | } else { |
| 6527 | attrs->switch_port = false; |
| 6528 | } |
| 6529 | return 0; |
| 6530 | } |
| 6531 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6532 | /** |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 6533 | * devlink_port_attrs_set - Set port attributes |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6534 | * |
| 6535 | * @devlink_port: devlink port |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 6536 | * @flavour: flavour of the port |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 6537 | * @port_number: number of the port that is facing user, for example |
| 6538 | * the front panel port number |
| 6539 | * @split: indicates if this is split port |
| 6540 | * @split_subport_number: if the port is split, this is the number |
| 6541 | * of subport. |
Jiri Pirko | bec5267 | 2019-04-03 14:24:16 +0200 | [diff] [blame] | 6542 | * @switch_id: if the port is part of switch, this is buffer with ID, |
| 6543 | * otwerwise this is NULL |
| 6544 | * @switch_id_len: length of the switch_id buffer |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6545 | */ |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 6546 | void devlink_port_attrs_set(struct devlink_port *devlink_port, |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 6547 | enum devlink_port_flavour flavour, |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 6548 | u32 port_number, bool split, |
Jiri Pirko | bec5267 | 2019-04-03 14:24:16 +0200 | [diff] [blame] | 6549 | u32 split_subport_number, |
| 6550 | const unsigned char *switch_id, |
| 6551 | unsigned char switch_id_len) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6552 | { |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 6553 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 6554 | int ret; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 6555 | |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 6556 | ret = __devlink_port_attrs_set(devlink_port, flavour, |
| 6557 | switch_id, switch_id_len); |
| 6558 | if (ret) |
Jiri Pirko | 45b8611 | 2019-03-24 11:14:33 +0100 | [diff] [blame] | 6559 | return; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 6560 | attrs->split = split; |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 6561 | attrs->phys.port_number = port_number; |
| 6562 | attrs->phys.split_subport_number = split_subport_number; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6563 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 6564 | EXPORT_SYMBOL_GPL(devlink_port_attrs_set); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 6565 | |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 6566 | /** |
| 6567 | * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes |
| 6568 | * |
| 6569 | * @devlink_port: devlink port |
| 6570 | * @pf: associated PF for the devlink port instance |
| 6571 | * @switch_id: if the port is part of switch, this is buffer with ID, |
| 6572 | * otherwise this is NULL |
| 6573 | * @switch_id_len: length of the switch_id buffer |
| 6574 | */ |
| 6575 | void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, |
| 6576 | const unsigned char *switch_id, |
| 6577 | unsigned char switch_id_len, u16 pf) |
| 6578 | { |
| 6579 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 6580 | int ret; |
| 6581 | |
| 6582 | ret = __devlink_port_attrs_set(devlink_port, |
| 6583 | DEVLINK_PORT_FLAVOUR_PCI_PF, |
| 6584 | switch_id, switch_id_len); |
| 6585 | if (ret) |
| 6586 | return; |
| 6587 | |
| 6588 | attrs->pci_pf.pf = pf; |
| 6589 | } |
| 6590 | EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set); |
| 6591 | |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 6592 | /** |
| 6593 | * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes |
| 6594 | * |
| 6595 | * @devlink_port: devlink port |
| 6596 | * @pf: associated PF for the devlink port instance |
| 6597 | * @vf: associated VF of a PF for the devlink port instance |
| 6598 | * @switch_id: if the port is part of switch, this is buffer with ID, |
| 6599 | * otherwise this is NULL |
| 6600 | * @switch_id_len: length of the switch_id buffer |
| 6601 | */ |
| 6602 | void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, |
| 6603 | const unsigned char *switch_id, |
| 6604 | unsigned char switch_id_len, |
| 6605 | u16 pf, u16 vf) |
| 6606 | { |
| 6607 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 6608 | int ret; |
| 6609 | |
| 6610 | ret = __devlink_port_attrs_set(devlink_port, |
| 6611 | DEVLINK_PORT_FLAVOUR_PCI_VF, |
| 6612 | switch_id, switch_id_len); |
| 6613 | if (ret) |
| 6614 | return; |
| 6615 | attrs->pci_vf.pf = pf; |
| 6616 | attrs->pci_vf.vf = vf; |
| 6617 | } |
| 6618 | EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set); |
| 6619 | |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 6620 | static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port, |
| 6621 | char *name, size_t len) |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 6622 | { |
| 6623 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 6624 | int n = 0; |
| 6625 | |
| 6626 | if (!attrs->set) |
| 6627 | return -EOPNOTSUPP; |
| 6628 | |
| 6629 | switch (attrs->flavour) { |
| 6630 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: |
| 6631 | if (!attrs->split) |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 6632 | n = snprintf(name, len, "p%u", attrs->phys.port_number); |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 6633 | else |
Parav Pandit | 378ef01 | 2019-07-08 23:17:35 -0500 | [diff] [blame] | 6634 | n = snprintf(name, len, "p%us%u", |
| 6635 | attrs->phys.port_number, |
| 6636 | attrs->phys.split_subport_number); |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 6637 | break; |
| 6638 | case DEVLINK_PORT_FLAVOUR_CPU: |
| 6639 | case DEVLINK_PORT_FLAVOUR_DSA: |
| 6640 | /* As CPU and DSA ports do not have a netdevice associated |
| 6641 | * case should not ever happen. |
| 6642 | */ |
| 6643 | WARN_ON(1); |
| 6644 | return -EINVAL; |
Parav Pandit | 98fd2d6 | 2019-07-08 23:17:37 -0500 | [diff] [blame] | 6645 | case DEVLINK_PORT_FLAVOUR_PCI_PF: |
| 6646 | n = snprintf(name, len, "pf%u", attrs->pci_pf.pf); |
| 6647 | break; |
Parav Pandit | e41b6bf | 2019-07-08 23:17:38 -0500 | [diff] [blame] | 6648 | case DEVLINK_PORT_FLAVOUR_PCI_VF: |
| 6649 | n = snprintf(name, len, "pf%uvf%u", |
| 6650 | attrs->pci_vf.pf, attrs->pci_vf.vf); |
| 6651 | break; |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 6652 | } |
| 6653 | |
| 6654 | if (n >= len) |
| 6655 | return -EINVAL; |
| 6656 | |
| 6657 | return 0; |
| 6658 | } |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 6659 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6660 | int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, |
| 6661 | u32 size, u16 ingress_pools_count, |
| 6662 | u16 egress_pools_count, u16 ingress_tc_count, |
| 6663 | u16 egress_tc_count) |
| 6664 | { |
| 6665 | struct devlink_sb *devlink_sb; |
| 6666 | int err = 0; |
| 6667 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6668 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6669 | if (devlink_sb_index_exists(devlink, sb_index)) { |
| 6670 | err = -EEXIST; |
| 6671 | goto unlock; |
| 6672 | } |
| 6673 | |
| 6674 | devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL); |
| 6675 | if (!devlink_sb) { |
| 6676 | err = -ENOMEM; |
| 6677 | goto unlock; |
| 6678 | } |
| 6679 | devlink_sb->index = sb_index; |
| 6680 | devlink_sb->size = size; |
| 6681 | devlink_sb->ingress_pools_count = ingress_pools_count; |
| 6682 | devlink_sb->egress_pools_count = egress_pools_count; |
| 6683 | devlink_sb->ingress_tc_count = ingress_tc_count; |
| 6684 | devlink_sb->egress_tc_count = egress_tc_count; |
| 6685 | list_add_tail(&devlink_sb->list, &devlink->sb_list); |
| 6686 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6687 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6688 | return err; |
| 6689 | } |
| 6690 | EXPORT_SYMBOL_GPL(devlink_sb_register); |
| 6691 | |
| 6692 | void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index) |
| 6693 | { |
| 6694 | struct devlink_sb *devlink_sb; |
| 6695 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6696 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6697 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 6698 | WARN_ON(!devlink_sb); |
| 6699 | list_del(&devlink_sb->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6700 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 6701 | kfree(devlink_sb); |
| 6702 | } |
| 6703 | EXPORT_SYMBOL_GPL(devlink_sb_unregister); |
| 6704 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6705 | /** |
| 6706 | * devlink_dpipe_headers_register - register dpipe headers |
| 6707 | * |
| 6708 | * @devlink: devlink |
| 6709 | * @dpipe_headers: dpipe header array |
| 6710 | * |
| 6711 | * Register the headers supported by hardware. |
| 6712 | */ |
| 6713 | int devlink_dpipe_headers_register(struct devlink *devlink, |
| 6714 | struct devlink_dpipe_headers *dpipe_headers) |
| 6715 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6716 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6717 | devlink->dpipe_headers = dpipe_headers; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6718 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6719 | return 0; |
| 6720 | } |
| 6721 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register); |
| 6722 | |
| 6723 | /** |
| 6724 | * devlink_dpipe_headers_unregister - unregister dpipe headers |
| 6725 | * |
| 6726 | * @devlink: devlink |
| 6727 | * |
| 6728 | * Unregister the headers supported by hardware. |
| 6729 | */ |
| 6730 | void devlink_dpipe_headers_unregister(struct devlink *devlink) |
| 6731 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6732 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6733 | devlink->dpipe_headers = NULL; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6734 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6735 | } |
| 6736 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister); |
| 6737 | |
| 6738 | /** |
| 6739 | * devlink_dpipe_table_counter_enabled - check if counter allocation |
| 6740 | * required |
| 6741 | * @devlink: devlink |
| 6742 | * @table_name: tables name |
| 6743 | * |
| 6744 | * Used by driver to check if counter allocation is required. |
| 6745 | * After counter allocation is turned on the table entries |
| 6746 | * are updated to include counter statistics. |
| 6747 | * |
| 6748 | * After that point on the driver must respect the counter |
| 6749 | * state so that each entry added to the table is added |
| 6750 | * with a counter. |
| 6751 | */ |
| 6752 | bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, |
| 6753 | const char *table_name) |
| 6754 | { |
| 6755 | struct devlink_dpipe_table *table; |
| 6756 | bool enabled; |
| 6757 | |
| 6758 | rcu_read_lock(); |
| 6759 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 6760 | table_name); |
| 6761 | enabled = false; |
| 6762 | if (table) |
| 6763 | enabled = table->counters_enabled; |
| 6764 | rcu_read_unlock(); |
| 6765 | return enabled; |
| 6766 | } |
| 6767 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled); |
| 6768 | |
| 6769 | /** |
| 6770 | * devlink_dpipe_table_register - register dpipe table |
| 6771 | * |
| 6772 | * @devlink: devlink |
| 6773 | * @table_name: table name |
| 6774 | * @table_ops: table ops |
| 6775 | * @priv: priv |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6776 | * @counter_control_extern: external control for counters |
| 6777 | */ |
| 6778 | int devlink_dpipe_table_register(struct devlink *devlink, |
| 6779 | const char *table_name, |
| 6780 | struct devlink_dpipe_table_ops *table_ops, |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 6781 | void *priv, bool counter_control_extern) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6782 | { |
| 6783 | struct devlink_dpipe_table *table; |
| 6784 | |
| 6785 | if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name)) |
| 6786 | return -EEXIST; |
| 6787 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 6788 | if (WARN_ON(!table_ops->size_get)) |
| 6789 | return -EINVAL; |
| 6790 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6791 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
| 6792 | if (!table) |
| 6793 | return -ENOMEM; |
| 6794 | |
| 6795 | table->name = table_name; |
| 6796 | table->table_ops = table_ops; |
| 6797 | table->priv = priv; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6798 | table->counter_control_extern = counter_control_extern; |
| 6799 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6800 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6801 | list_add_tail_rcu(&table->list, &devlink->dpipe_table_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6802 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6803 | return 0; |
| 6804 | } |
| 6805 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_register); |
| 6806 | |
| 6807 | /** |
| 6808 | * devlink_dpipe_table_unregister - unregister dpipe table |
| 6809 | * |
| 6810 | * @devlink: devlink |
| 6811 | * @table_name: table name |
| 6812 | */ |
| 6813 | void devlink_dpipe_table_unregister(struct devlink *devlink, |
| 6814 | const char *table_name) |
| 6815 | { |
| 6816 | struct devlink_dpipe_table *table; |
| 6817 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6818 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6819 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 6820 | table_name); |
| 6821 | if (!table) |
| 6822 | goto unlock; |
| 6823 | list_del_rcu(&table->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6824 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6825 | kfree_rcu(table, rcu); |
| 6826 | return; |
| 6827 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 6828 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 6829 | } |
| 6830 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister); |
| 6831 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6832 | /** |
| 6833 | * devlink_resource_register - devlink resource register |
| 6834 | * |
| 6835 | * @devlink: devlink |
| 6836 | * @resource_name: resource's name |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6837 | * @resource_size: resource's size |
| 6838 | * @resource_id: resource's id |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 6839 | * @parent_resource_id: resource's parent id |
| 6840 | * @size_params: size parameters |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6841 | */ |
| 6842 | int devlink_resource_register(struct devlink *devlink, |
| 6843 | const char *resource_name, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6844 | u64 resource_size, |
| 6845 | u64 resource_id, |
| 6846 | u64 parent_resource_id, |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 6847 | const struct devlink_resource_size_params *size_params) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6848 | { |
| 6849 | struct devlink_resource *resource; |
| 6850 | struct list_head *resource_list; |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 6851 | bool top_hierarchy; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6852 | int err = 0; |
| 6853 | |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 6854 | top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP; |
| 6855 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6856 | mutex_lock(&devlink->lock); |
| 6857 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 6858 | if (resource) { |
| 6859 | err = -EINVAL; |
| 6860 | goto out; |
| 6861 | } |
| 6862 | |
| 6863 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
| 6864 | if (!resource) { |
| 6865 | err = -ENOMEM; |
| 6866 | goto out; |
| 6867 | } |
| 6868 | |
| 6869 | if (top_hierarchy) { |
| 6870 | resource_list = &devlink->resource_list; |
| 6871 | } else { |
| 6872 | struct devlink_resource *parent_resource; |
| 6873 | |
| 6874 | parent_resource = devlink_resource_find(devlink, NULL, |
| 6875 | parent_resource_id); |
| 6876 | if (parent_resource) { |
| 6877 | resource_list = &parent_resource->resource_list; |
| 6878 | resource->parent = parent_resource; |
| 6879 | } else { |
Colin Ian King | b75703d | 2018-01-22 10:31:19 +0000 | [diff] [blame] | 6880 | kfree(resource); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6881 | err = -EINVAL; |
| 6882 | goto out; |
| 6883 | } |
| 6884 | } |
| 6885 | |
| 6886 | resource->name = resource_name; |
| 6887 | resource->size = resource_size; |
| 6888 | resource->size_new = resource_size; |
| 6889 | resource->id = resource_id; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6890 | resource->size_valid = true; |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 6891 | memcpy(&resource->size_params, size_params, |
| 6892 | sizeof(resource->size_params)); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 6893 | INIT_LIST_HEAD(&resource->resource_list); |
| 6894 | list_add_tail(&resource->list, resource_list); |
| 6895 | out: |
| 6896 | mutex_unlock(&devlink->lock); |
| 6897 | return err; |
| 6898 | } |
| 6899 | EXPORT_SYMBOL_GPL(devlink_resource_register); |
| 6900 | |
| 6901 | /** |
| 6902 | * devlink_resources_unregister - free all resources |
| 6903 | * |
| 6904 | * @devlink: devlink |
| 6905 | * @resource: resource |
| 6906 | */ |
| 6907 | void devlink_resources_unregister(struct devlink *devlink, |
| 6908 | struct devlink_resource *resource) |
| 6909 | { |
| 6910 | struct devlink_resource *tmp, *child_resource; |
| 6911 | struct list_head *resource_list; |
| 6912 | |
| 6913 | if (resource) |
| 6914 | resource_list = &resource->resource_list; |
| 6915 | else |
| 6916 | resource_list = &devlink->resource_list; |
| 6917 | |
| 6918 | if (!resource) |
| 6919 | mutex_lock(&devlink->lock); |
| 6920 | |
| 6921 | list_for_each_entry_safe(child_resource, tmp, resource_list, list) { |
| 6922 | devlink_resources_unregister(devlink, child_resource); |
| 6923 | list_del(&child_resource->list); |
| 6924 | kfree(child_resource); |
| 6925 | } |
| 6926 | |
| 6927 | if (!resource) |
| 6928 | mutex_unlock(&devlink->lock); |
| 6929 | } |
| 6930 | EXPORT_SYMBOL_GPL(devlink_resources_unregister); |
| 6931 | |
| 6932 | /** |
| 6933 | * devlink_resource_size_get - get and update size |
| 6934 | * |
| 6935 | * @devlink: devlink |
| 6936 | * @resource_id: the requested resource id |
| 6937 | * @p_resource_size: ptr to update |
| 6938 | */ |
| 6939 | int devlink_resource_size_get(struct devlink *devlink, |
| 6940 | u64 resource_id, |
| 6941 | u64 *p_resource_size) |
| 6942 | { |
| 6943 | struct devlink_resource *resource; |
| 6944 | int err = 0; |
| 6945 | |
| 6946 | mutex_lock(&devlink->lock); |
| 6947 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 6948 | if (!resource) { |
| 6949 | err = -EINVAL; |
| 6950 | goto out; |
| 6951 | } |
| 6952 | *p_resource_size = resource->size_new; |
| 6953 | resource->size = resource->size_new; |
| 6954 | out: |
| 6955 | mutex_unlock(&devlink->lock); |
| 6956 | return err; |
| 6957 | } |
| 6958 | EXPORT_SYMBOL_GPL(devlink_resource_size_get); |
| 6959 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 6960 | /** |
| 6961 | * devlink_dpipe_table_resource_set - set the resource id |
| 6962 | * |
| 6963 | * @devlink: devlink |
| 6964 | * @table_name: table name |
| 6965 | * @resource_id: resource id |
| 6966 | * @resource_units: number of resource's units consumed per table's entry |
| 6967 | */ |
| 6968 | int devlink_dpipe_table_resource_set(struct devlink *devlink, |
| 6969 | const char *table_name, u64 resource_id, |
| 6970 | u64 resource_units) |
| 6971 | { |
| 6972 | struct devlink_dpipe_table *table; |
| 6973 | int err = 0; |
| 6974 | |
| 6975 | mutex_lock(&devlink->lock); |
| 6976 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 6977 | table_name); |
| 6978 | if (!table) { |
| 6979 | err = -EINVAL; |
| 6980 | goto out; |
| 6981 | } |
| 6982 | table->resource_id = resource_id; |
| 6983 | table->resource_units = resource_units; |
| 6984 | table->resource_valid = true; |
| 6985 | out: |
| 6986 | mutex_unlock(&devlink->lock); |
| 6987 | return err; |
| 6988 | } |
| 6989 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set); |
| 6990 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 6991 | /** |
| 6992 | * devlink_resource_occ_get_register - register occupancy getter |
| 6993 | * |
| 6994 | * @devlink: devlink |
| 6995 | * @resource_id: resource id |
| 6996 | * @occ_get: occupancy getter callback |
| 6997 | * @occ_get_priv: occupancy getter callback priv |
| 6998 | */ |
| 6999 | void devlink_resource_occ_get_register(struct devlink *devlink, |
| 7000 | u64 resource_id, |
| 7001 | devlink_resource_occ_get_t *occ_get, |
| 7002 | void *occ_get_priv) |
| 7003 | { |
| 7004 | struct devlink_resource *resource; |
| 7005 | |
| 7006 | mutex_lock(&devlink->lock); |
| 7007 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 7008 | if (WARN_ON(!resource)) |
| 7009 | goto out; |
| 7010 | WARN_ON(resource->occ_get); |
| 7011 | |
| 7012 | resource->occ_get = occ_get; |
| 7013 | resource->occ_get_priv = occ_get_priv; |
| 7014 | out: |
| 7015 | mutex_unlock(&devlink->lock); |
| 7016 | } |
| 7017 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register); |
| 7018 | |
| 7019 | /** |
| 7020 | * devlink_resource_occ_get_unregister - unregister occupancy getter |
| 7021 | * |
| 7022 | * @devlink: devlink |
| 7023 | * @resource_id: resource id |
| 7024 | */ |
| 7025 | void devlink_resource_occ_get_unregister(struct devlink *devlink, |
| 7026 | u64 resource_id) |
| 7027 | { |
| 7028 | struct devlink_resource *resource; |
| 7029 | |
| 7030 | mutex_lock(&devlink->lock); |
| 7031 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 7032 | if (WARN_ON(!resource)) |
| 7033 | goto out; |
| 7034 | WARN_ON(!resource->occ_get); |
| 7035 | |
| 7036 | resource->occ_get = NULL; |
| 7037 | resource->occ_get_priv = NULL; |
| 7038 | out: |
| 7039 | mutex_unlock(&devlink->lock); |
| 7040 | } |
| 7041 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister); |
| 7042 | |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7043 | static int devlink_param_verify(const struct devlink_param *param) |
| 7044 | { |
| 7045 | if (!param || !param->name || !param->supported_cmodes) |
| 7046 | return -EINVAL; |
| 7047 | if (param->generic) |
| 7048 | return devlink_param_generic_verify(param); |
| 7049 | else |
| 7050 | return devlink_param_driver_verify(param); |
| 7051 | } |
| 7052 | |
| 7053 | static int __devlink_params_register(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7054 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7055 | struct list_head *param_list, |
| 7056 | const struct devlink_param *params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7057 | size_t params_count, |
| 7058 | enum devlink_command reg_cmd, |
| 7059 | enum devlink_command unreg_cmd) |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7060 | { |
| 7061 | const struct devlink_param *param = params; |
| 7062 | int i; |
| 7063 | int err; |
| 7064 | |
| 7065 | mutex_lock(&devlink->lock); |
| 7066 | for (i = 0; i < params_count; i++, param++) { |
| 7067 | err = devlink_param_verify(param); |
| 7068 | if (err) |
| 7069 | goto rollback; |
| 7070 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7071 | err = devlink_param_register_one(devlink, port_index, |
| 7072 | param_list, param, reg_cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7073 | if (err) |
| 7074 | goto rollback; |
| 7075 | } |
| 7076 | |
| 7077 | mutex_unlock(&devlink->lock); |
| 7078 | return 0; |
| 7079 | |
| 7080 | rollback: |
| 7081 | if (!i) |
| 7082 | goto unlock; |
| 7083 | for (param--; i > 0; i--, param--) |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7084 | devlink_param_unregister_one(devlink, port_index, param_list, |
| 7085 | param, unreg_cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7086 | unlock: |
| 7087 | mutex_unlock(&devlink->lock); |
| 7088 | return err; |
| 7089 | } |
| 7090 | |
| 7091 | static void __devlink_params_unregister(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7092 | unsigned int port_index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7093 | struct list_head *param_list, |
| 7094 | const struct devlink_param *params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7095 | size_t params_count, |
| 7096 | enum devlink_command cmd) |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7097 | { |
| 7098 | const struct devlink_param *param = params; |
| 7099 | int i; |
| 7100 | |
| 7101 | mutex_lock(&devlink->lock); |
| 7102 | for (i = 0; i < params_count; i++, param++) |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7103 | devlink_param_unregister_one(devlink, 0, param_list, param, |
| 7104 | cmd); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7105 | mutex_unlock(&devlink->lock); |
| 7106 | } |
| 7107 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 7108 | /** |
| 7109 | * devlink_params_register - register configuration parameters |
| 7110 | * |
| 7111 | * @devlink: devlink |
| 7112 | * @params: configuration parameters array |
| 7113 | * @params_count: number of parameters provided |
| 7114 | * |
| 7115 | * Register the configuration parameters supported by the driver. |
| 7116 | */ |
| 7117 | int devlink_params_register(struct devlink *devlink, |
| 7118 | const struct devlink_param *params, |
| 7119 | size_t params_count) |
| 7120 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7121 | return __devlink_params_register(devlink, 0, &devlink->param_list, |
| 7122 | params, params_count, |
| 7123 | DEVLINK_CMD_PARAM_NEW, |
| 7124 | DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 7125 | } |
| 7126 | EXPORT_SYMBOL_GPL(devlink_params_register); |
| 7127 | |
| 7128 | /** |
| 7129 | * devlink_params_unregister - unregister configuration parameters |
| 7130 | * @devlink: devlink |
| 7131 | * @params: configuration parameters to unregister |
| 7132 | * @params_count: number of parameters provided |
| 7133 | */ |
| 7134 | void devlink_params_unregister(struct devlink *devlink, |
| 7135 | const struct devlink_param *params, |
| 7136 | size_t params_count) |
| 7137 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7138 | return __devlink_params_unregister(devlink, 0, &devlink->param_list, |
| 7139 | params, params_count, |
| 7140 | DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 7141 | } |
| 7142 | EXPORT_SYMBOL_GPL(devlink_params_unregister); |
| 7143 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 7144 | /** |
Jiri Pirko | 7c62cfb | 2019-02-07 11:22:45 +0000 | [diff] [blame] | 7145 | * devlink_params_publish - publish configuration parameters |
| 7146 | * |
| 7147 | * @devlink: devlink |
| 7148 | * |
| 7149 | * Publish previously registered configuration parameters. |
| 7150 | */ |
| 7151 | void devlink_params_publish(struct devlink *devlink) |
| 7152 | { |
| 7153 | struct devlink_param_item *param_item; |
| 7154 | |
| 7155 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 7156 | if (param_item->published) |
| 7157 | continue; |
| 7158 | param_item->published = true; |
| 7159 | devlink_param_notify(devlink, 0, param_item, |
| 7160 | DEVLINK_CMD_PARAM_NEW); |
| 7161 | } |
| 7162 | } |
| 7163 | EXPORT_SYMBOL_GPL(devlink_params_publish); |
| 7164 | |
| 7165 | /** |
| 7166 | * devlink_params_unpublish - unpublish configuration parameters |
| 7167 | * |
| 7168 | * @devlink: devlink |
| 7169 | * |
| 7170 | * Unpublish previously registered configuration parameters. |
| 7171 | */ |
| 7172 | void devlink_params_unpublish(struct devlink *devlink) |
| 7173 | { |
| 7174 | struct devlink_param_item *param_item; |
| 7175 | |
| 7176 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 7177 | if (!param_item->published) |
| 7178 | continue; |
| 7179 | param_item->published = false; |
| 7180 | devlink_param_notify(devlink, 0, param_item, |
| 7181 | DEVLINK_CMD_PARAM_DEL); |
| 7182 | } |
| 7183 | } |
| 7184 | EXPORT_SYMBOL_GPL(devlink_params_unpublish); |
| 7185 | |
| 7186 | /** |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7187 | * devlink_port_params_register - register port configuration parameters |
| 7188 | * |
| 7189 | * @devlink_port: devlink port |
| 7190 | * @params: configuration parameters array |
| 7191 | * @params_count: number of parameters provided |
| 7192 | * |
| 7193 | * Register the configuration parameters supported by the port. |
| 7194 | */ |
| 7195 | int devlink_port_params_register(struct devlink_port *devlink_port, |
| 7196 | const struct devlink_param *params, |
| 7197 | size_t params_count) |
| 7198 | { |
| 7199 | return __devlink_params_register(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7200 | devlink_port->index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7201 | &devlink_port->param_list, params, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7202 | params_count, |
| 7203 | DEVLINK_CMD_PORT_PARAM_NEW, |
| 7204 | DEVLINK_CMD_PORT_PARAM_DEL); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7205 | } |
| 7206 | EXPORT_SYMBOL_GPL(devlink_port_params_register); |
| 7207 | |
| 7208 | /** |
| 7209 | * devlink_port_params_unregister - unregister port configuration |
| 7210 | * parameters |
| 7211 | * |
| 7212 | * @devlink_port: devlink port |
| 7213 | * @params: configuration parameters array |
| 7214 | * @params_count: number of parameters provided |
| 7215 | */ |
| 7216 | void devlink_port_params_unregister(struct devlink_port *devlink_port, |
| 7217 | const struct devlink_param *params, |
| 7218 | size_t params_count) |
| 7219 | { |
| 7220 | return __devlink_params_unregister(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7221 | devlink_port->index, |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7222 | &devlink_port->param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7223 | params, params_count, |
| 7224 | DEVLINK_CMD_PORT_PARAM_DEL); |
Vasundhara Volam | 39e6160 | 2019-01-28 18:00:20 +0530 | [diff] [blame] | 7225 | } |
| 7226 | EXPORT_SYMBOL_GPL(devlink_port_params_unregister); |
| 7227 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 7228 | static int |
| 7229 | __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id, |
| 7230 | union devlink_param_value *init_val) |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 7231 | { |
| 7232 | struct devlink_param_item *param_item; |
| 7233 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 7234 | param_item = devlink_param_find_by_id(param_list, param_id); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 7235 | if (!param_item) |
| 7236 | return -EINVAL; |
| 7237 | |
| 7238 | if (!param_item->driverinit_value_valid || |
| 7239 | !devlink_param_cmode_is_supported(param_item->param, |
| 7240 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 7241 | return -EOPNOTSUPP; |
| 7242 | |
Moshe Shemesh | 1276534 | 2018-10-10 16:09:26 +0300 | [diff] [blame] | 7243 | if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING) |
| 7244 | strcpy(init_val->vstr, param_item->driverinit_value.vstr); |
| 7245 | else |
| 7246 | *init_val = param_item->driverinit_value; |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 7247 | |
| 7248 | return 0; |
| 7249 | } |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 7250 | |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 7251 | static int |
| 7252 | __devlink_param_driverinit_value_set(struct devlink *devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7253 | unsigned int port_index, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 7254 | struct list_head *param_list, u32 param_id, |
| 7255 | union devlink_param_value init_val, |
| 7256 | enum devlink_command cmd) |
| 7257 | { |
| 7258 | struct devlink_param_item *param_item; |
| 7259 | |
| 7260 | param_item = devlink_param_find_by_id(param_list, param_id); |
| 7261 | if (!param_item) |
| 7262 | return -EINVAL; |
| 7263 | |
| 7264 | if (!devlink_param_cmode_is_supported(param_item->param, |
| 7265 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 7266 | return -EOPNOTSUPP; |
| 7267 | |
| 7268 | if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING) |
| 7269 | strcpy(param_item->driverinit_value.vstr, init_val.vstr); |
| 7270 | else |
| 7271 | param_item->driverinit_value = init_val; |
| 7272 | param_item->driverinit_value_valid = true; |
| 7273 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7274 | devlink_param_notify(devlink, port_index, param_item, cmd); |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 7275 | return 0; |
| 7276 | } |
| 7277 | |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 7278 | /** |
| 7279 | * devlink_param_driverinit_value_get - get configuration parameter |
| 7280 | * value for driver initializing |
| 7281 | * |
| 7282 | * @devlink: devlink |
| 7283 | * @param_id: parameter ID |
| 7284 | * @init_val: value of parameter in driverinit configuration mode |
| 7285 | * |
| 7286 | * This function should be used by the driver to get driverinit |
| 7287 | * configuration for initialization after reload command. |
| 7288 | */ |
| 7289 | int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id, |
| 7290 | union devlink_param_value *init_val) |
| 7291 | { |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 7292 | if (!devlink_reload_supported(devlink)) |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 7293 | return -EOPNOTSUPP; |
| 7294 | |
| 7295 | return __devlink_param_driverinit_value_get(&devlink->param_list, |
| 7296 | param_id, init_val); |
| 7297 | } |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 7298 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get); |
| 7299 | |
| 7300 | /** |
| 7301 | * devlink_param_driverinit_value_set - set value of configuration |
| 7302 | * parameter for driverinit |
| 7303 | * configuration mode |
| 7304 | * |
| 7305 | * @devlink: devlink |
| 7306 | * @param_id: parameter ID |
| 7307 | * @init_val: value of parameter to set for driverinit configuration mode |
| 7308 | * |
| 7309 | * This function should be used by the driver to set driverinit |
| 7310 | * configuration mode default value. |
| 7311 | */ |
| 7312 | int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id, |
| 7313 | union devlink_param_value init_val) |
| 7314 | { |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7315 | return __devlink_param_driverinit_value_set(devlink, 0, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 7316 | &devlink->param_list, |
| 7317 | param_id, init_val, |
| 7318 | DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 7319 | } |
| 7320 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set); |
| 7321 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 7322 | /** |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 7323 | * devlink_port_param_driverinit_value_get - get configuration parameter |
| 7324 | * value for driver initializing |
| 7325 | * |
| 7326 | * @devlink_port: devlink_port |
| 7327 | * @param_id: parameter ID |
| 7328 | * @init_val: value of parameter in driverinit configuration mode |
| 7329 | * |
| 7330 | * This function should be used by the driver to get driverinit |
| 7331 | * configuration for initialization after reload command. |
| 7332 | */ |
| 7333 | int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port, |
| 7334 | u32 param_id, |
| 7335 | union devlink_param_value *init_val) |
| 7336 | { |
| 7337 | struct devlink *devlink = devlink_port->devlink; |
| 7338 | |
Jiri Pirko | 9769106 | 2019-09-12 10:49:45 +0200 | [diff] [blame] | 7339 | if (!devlink_reload_supported(devlink)) |
Vasundhara Volam | ffd19b9 | 2019-01-28 18:00:23 +0530 | [diff] [blame] | 7340 | return -EOPNOTSUPP; |
| 7341 | |
| 7342 | return __devlink_param_driverinit_value_get(&devlink_port->param_list, |
| 7343 | param_id, init_val); |
| 7344 | } |
| 7345 | EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get); |
| 7346 | |
| 7347 | /** |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 7348 | * devlink_port_param_driverinit_value_set - set value of configuration |
| 7349 | * parameter for driverinit |
| 7350 | * configuration mode |
| 7351 | * |
| 7352 | * @devlink_port: devlink_port |
| 7353 | * @param_id: parameter ID |
| 7354 | * @init_val: value of parameter to set for driverinit configuration mode |
| 7355 | * |
| 7356 | * This function should be used by the driver to set driverinit |
| 7357 | * configuration mode default value. |
| 7358 | */ |
| 7359 | int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port, |
| 7360 | u32 param_id, |
| 7361 | union devlink_param_value init_val) |
| 7362 | { |
| 7363 | return __devlink_param_driverinit_value_set(devlink_port->devlink, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7364 | devlink_port->index, |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 7365 | &devlink_port->param_list, |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7366 | param_id, init_val, |
| 7367 | DEVLINK_CMD_PORT_PARAM_NEW); |
Vasundhara Volam | 5473a7b | 2019-01-28 18:00:24 +0530 | [diff] [blame] | 7368 | } |
| 7369 | EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set); |
| 7370 | |
| 7371 | /** |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 7372 | * devlink_param_value_changed - notify devlink on a parameter's value |
| 7373 | * change. Should be called by the driver |
| 7374 | * right after the change. |
| 7375 | * |
| 7376 | * @devlink: devlink |
| 7377 | * @param_id: parameter ID |
| 7378 | * |
| 7379 | * This function should be used by the driver to notify devlink on value |
| 7380 | * change, excluding driverinit configuration mode. |
| 7381 | * For driverinit configuration mode driver should use the function |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 7382 | */ |
| 7383 | void devlink_param_value_changed(struct devlink *devlink, u32 param_id) |
| 7384 | { |
| 7385 | struct devlink_param_item *param_item; |
| 7386 | |
| 7387 | param_item = devlink_param_find_by_id(&devlink->param_list, param_id); |
| 7388 | WARN_ON(!param_item); |
| 7389 | |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7390 | devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 7391 | } |
| 7392 | EXPORT_SYMBOL_GPL(devlink_param_value_changed); |
| 7393 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 7394 | /** |
Vasundhara Volam | c1e5786d | 2019-01-28 18:00:25 +0530 | [diff] [blame] | 7395 | * devlink_port_param_value_changed - notify devlink on a parameter's value |
| 7396 | * change. Should be called by the driver |
| 7397 | * right after the change. |
| 7398 | * |
| 7399 | * @devlink_port: devlink_port |
| 7400 | * @param_id: parameter ID |
| 7401 | * |
| 7402 | * This function should be used by the driver to notify devlink on value |
| 7403 | * change, excluding driverinit configuration mode. |
| 7404 | * For driverinit configuration mode driver should use the function |
| 7405 | * devlink_port_param_driverinit_value_set() instead. |
| 7406 | */ |
| 7407 | void devlink_port_param_value_changed(struct devlink_port *devlink_port, |
| 7408 | u32 param_id) |
| 7409 | { |
| 7410 | struct devlink_param_item *param_item; |
| 7411 | |
| 7412 | param_item = devlink_param_find_by_id(&devlink_port->param_list, |
| 7413 | param_id); |
| 7414 | WARN_ON(!param_item); |
| 7415 | |
| 7416 | devlink_param_notify(devlink_port->devlink, devlink_port->index, |
| 7417 | param_item, DEVLINK_CMD_PORT_PARAM_NEW); |
| 7418 | } |
| 7419 | EXPORT_SYMBOL_GPL(devlink_port_param_value_changed); |
| 7420 | |
| 7421 | /** |
Moshe Shemesh | bde74ad1 | 2018-10-10 16:09:27 +0300 | [diff] [blame] | 7422 | * devlink_param_value_str_fill - Safely fill-up the string preventing |
| 7423 | * from overflow of the preallocated buffer |
| 7424 | * |
| 7425 | * @dst_val: destination devlink_param_value |
| 7426 | * @src: source buffer |
| 7427 | */ |
| 7428 | void devlink_param_value_str_fill(union devlink_param_value *dst_val, |
| 7429 | const char *src) |
| 7430 | { |
| 7431 | size_t len; |
| 7432 | |
| 7433 | len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE); |
| 7434 | WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE); |
| 7435 | } |
| 7436 | EXPORT_SYMBOL_GPL(devlink_param_value_str_fill); |
| 7437 | |
| 7438 | /** |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 7439 | * devlink_region_create - create a new address region |
| 7440 | * |
| 7441 | * @devlink: devlink |
| 7442 | * @region_name: region name |
| 7443 | * @region_max_snapshots: Maximum supported number of snapshots for region |
| 7444 | * @region_size: size of region |
| 7445 | */ |
| 7446 | struct devlink_region *devlink_region_create(struct devlink *devlink, |
| 7447 | const char *region_name, |
| 7448 | u32 region_max_snapshots, |
| 7449 | u64 region_size) |
| 7450 | { |
| 7451 | struct devlink_region *region; |
| 7452 | int err = 0; |
| 7453 | |
| 7454 | mutex_lock(&devlink->lock); |
| 7455 | |
| 7456 | if (devlink_region_get_by_name(devlink, region_name)) { |
| 7457 | err = -EEXIST; |
| 7458 | goto unlock; |
| 7459 | } |
| 7460 | |
| 7461 | region = kzalloc(sizeof(*region), GFP_KERNEL); |
| 7462 | if (!region) { |
| 7463 | err = -ENOMEM; |
| 7464 | goto unlock; |
| 7465 | } |
| 7466 | |
| 7467 | region->devlink = devlink; |
| 7468 | region->max_snapshots = region_max_snapshots; |
| 7469 | region->name = region_name; |
| 7470 | region->size = region_size; |
| 7471 | INIT_LIST_HEAD(®ion->snapshot_list); |
| 7472 | list_add_tail(®ion->list, &devlink->region_list); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 7473 | devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 7474 | |
| 7475 | mutex_unlock(&devlink->lock); |
| 7476 | return region; |
| 7477 | |
| 7478 | unlock: |
| 7479 | mutex_unlock(&devlink->lock); |
| 7480 | return ERR_PTR(err); |
| 7481 | } |
| 7482 | EXPORT_SYMBOL_GPL(devlink_region_create); |
| 7483 | |
| 7484 | /** |
| 7485 | * devlink_region_destroy - destroy address region |
| 7486 | * |
| 7487 | * @region: devlink region to destroy |
| 7488 | */ |
| 7489 | void devlink_region_destroy(struct devlink_region *region) |
| 7490 | { |
| 7491 | struct devlink *devlink = region->devlink; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 7492 | struct devlink_snapshot *snapshot, *ts; |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 7493 | |
| 7494 | mutex_lock(&devlink->lock); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 7495 | |
| 7496 | /* Free all snapshots of region */ |
| 7497 | list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list) |
Jiri Pirko | 92b4982 | 2019-08-12 14:28:31 +0200 | [diff] [blame] | 7498 | devlink_region_snapshot_del(region, snapshot); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 7499 | |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 7500 | list_del(®ion->list); |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 7501 | |
| 7502 | devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL); |
Alex Vesker | b16ebe9 | 2018-07-12 15:13:08 +0300 | [diff] [blame] | 7503 | mutex_unlock(&devlink->lock); |
| 7504 | kfree(region); |
| 7505 | } |
| 7506 | EXPORT_SYMBOL_GPL(devlink_region_destroy); |
| 7507 | |
Alex Vesker | ccadfa4 | 2018-07-12 15:13:09 +0300 | [diff] [blame] | 7508 | /** |
| 7509 | * devlink_region_shapshot_id_get - get snapshot ID |
| 7510 | * |
| 7511 | * This callback should be called when adding a new snapshot, |
| 7512 | * Driver should use the same id for multiple snapshots taken |
| 7513 | * on multiple regions at the same time/by the same trigger. |
| 7514 | * |
| 7515 | * @devlink: devlink |
| 7516 | */ |
| 7517 | u32 devlink_region_shapshot_id_get(struct devlink *devlink) |
| 7518 | { |
| 7519 | u32 id; |
| 7520 | |
| 7521 | mutex_lock(&devlink->lock); |
| 7522 | id = ++devlink->snapshot_id; |
| 7523 | mutex_unlock(&devlink->lock); |
| 7524 | |
| 7525 | return id; |
| 7526 | } |
| 7527 | EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get); |
| 7528 | |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 7529 | /** |
| 7530 | * devlink_region_snapshot_create - create a new snapshot |
| 7531 | * This will add a new snapshot of a region. The snapshot |
| 7532 | * will be stored on the region struct and can be accessed |
| 7533 | * from devlink. This is useful for future analyses of snapshots. |
| 7534 | * Multiple snapshots can be created on a region. |
| 7535 | * The @snapshot_id should be obtained using the getter function. |
| 7536 | * |
Jakub Kicinski | eeaadd8 | 2019-02-27 11:36:36 -0800 | [diff] [blame] | 7537 | * @region: devlink region of the snapshot |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 7538 | * @data: snapshot data |
| 7539 | * @snapshot_id: snapshot id to be created |
| 7540 | * @data_destructor: pointer to destructor function to free data |
| 7541 | */ |
Jiri Pirko | 3a5e523 | 2019-08-09 15:27:15 +0200 | [diff] [blame] | 7542 | int devlink_region_snapshot_create(struct devlink_region *region, |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 7543 | u8 *data, u32 snapshot_id, |
| 7544 | devlink_snapshot_data_dest_t *data_destructor) |
| 7545 | { |
| 7546 | struct devlink *devlink = region->devlink; |
| 7547 | struct devlink_snapshot *snapshot; |
| 7548 | int err; |
| 7549 | |
| 7550 | mutex_lock(&devlink->lock); |
| 7551 | |
| 7552 | /* check if region can hold one more snapshot */ |
| 7553 | if (region->cur_snapshots == region->max_snapshots) { |
| 7554 | err = -ENOMEM; |
| 7555 | goto unlock; |
| 7556 | } |
| 7557 | |
| 7558 | if (devlink_region_snapshot_get_by_id(region, snapshot_id)) { |
| 7559 | err = -EEXIST; |
| 7560 | goto unlock; |
| 7561 | } |
| 7562 | |
| 7563 | snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL); |
| 7564 | if (!snapshot) { |
| 7565 | err = -ENOMEM; |
| 7566 | goto unlock; |
| 7567 | } |
| 7568 | |
| 7569 | snapshot->id = snapshot_id; |
| 7570 | snapshot->region = region; |
| 7571 | snapshot->data = data; |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 7572 | snapshot->data_destructor = data_destructor; |
| 7573 | |
| 7574 | list_add_tail(&snapshot->list, ®ion->snapshot_list); |
| 7575 | |
| 7576 | region->cur_snapshots++; |
| 7577 | |
Alex Vesker | 866319b | 2018-07-12 15:13:13 +0300 | [diff] [blame] | 7578 | devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW); |
Alex Vesker | d7e5272 | 2018-07-12 15:13:10 +0300 | [diff] [blame] | 7579 | mutex_unlock(&devlink->lock); |
| 7580 | return 0; |
| 7581 | |
| 7582 | unlock: |
| 7583 | mutex_unlock(&devlink->lock); |
| 7584 | return err; |
| 7585 | } |
| 7586 | EXPORT_SYMBOL_GPL(devlink_region_snapshot_create); |
| 7587 | |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7588 | #define DEVLINK_TRAP(_id, _type) \ |
| 7589 | { \ |
| 7590 | .type = DEVLINK_TRAP_TYPE_##_type, \ |
| 7591 | .id = DEVLINK_TRAP_GENERIC_ID_##_id, \ |
| 7592 | .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \ |
| 7593 | } |
| 7594 | |
| 7595 | static const struct devlink_trap devlink_trap_generic[] = { |
Ido Schimmel | 391203a | 2019-08-17 16:28:18 +0300 | [diff] [blame] | 7596 | DEVLINK_TRAP(SMAC_MC, DROP), |
| 7597 | DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP), |
| 7598 | DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP), |
| 7599 | DEVLINK_TRAP(INGRESS_STP_FILTER, DROP), |
| 7600 | DEVLINK_TRAP(EMPTY_TX_LIST, DROP), |
| 7601 | DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP), |
| 7602 | DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP), |
| 7603 | DEVLINK_TRAP(TTL_ERROR, EXCEPTION), |
| 7604 | DEVLINK_TRAP(TAIL_DROP, DROP), |
Amit Cohen | 6896cc4 | 2019-11-07 18:42:09 +0200 | [diff] [blame^] | 7605 | DEVLINK_TRAP(NON_IP_PACKET, DROP), |
| 7606 | DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP), |
| 7607 | DEVLINK_TRAP(DIP_LB, DROP), |
| 7608 | DEVLINK_TRAP(SIP_MC, DROP), |
| 7609 | DEVLINK_TRAP(SIP_LB, DROP), |
| 7610 | DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP), |
| 7611 | DEVLINK_TRAP(IPV4_SIP_BC, DROP), |
| 7612 | DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP), |
| 7613 | DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP), |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7614 | }; |
| 7615 | |
| 7616 | #define DEVLINK_TRAP_GROUP(_id) \ |
| 7617 | { \ |
| 7618 | .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \ |
| 7619 | .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \ |
| 7620 | } |
| 7621 | |
| 7622 | static const struct devlink_trap_group devlink_trap_group_generic[] = { |
Ido Schimmel | 391203a | 2019-08-17 16:28:18 +0300 | [diff] [blame] | 7623 | DEVLINK_TRAP_GROUP(L2_DROPS), |
| 7624 | DEVLINK_TRAP_GROUP(L3_DROPS), |
| 7625 | DEVLINK_TRAP_GROUP(BUFFER_DROPS), |
Ido Schimmel | 0f420b6 | 2019-08-17 16:28:17 +0300 | [diff] [blame] | 7626 | }; |
| 7627 | |
| 7628 | static int devlink_trap_generic_verify(const struct devlink_trap *trap) |
| 7629 | { |
| 7630 | if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX) |
| 7631 | return -EINVAL; |
| 7632 | |
| 7633 | if (strcmp(trap->name, devlink_trap_generic[trap->id].name)) |
| 7634 | return -EINVAL; |
| 7635 | |
| 7636 | if (trap->type != devlink_trap_generic[trap->id].type) |
| 7637 | return -EINVAL; |
| 7638 | |
| 7639 | return 0; |
| 7640 | } |
| 7641 | |
| 7642 | static int devlink_trap_driver_verify(const struct devlink_trap *trap) |
| 7643 | { |
| 7644 | int i; |
| 7645 | |
| 7646 | if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX) |
| 7647 | return -EINVAL; |
| 7648 | |
| 7649 | for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) { |
| 7650 | if (!strcmp(trap->name, devlink_trap_generic[i].name)) |
| 7651 | return -EEXIST; |
| 7652 | } |
| 7653 | |
| 7654 | return 0; |
| 7655 | } |
| 7656 | |
| 7657 | static int devlink_trap_verify(const struct devlink_trap *trap) |
| 7658 | { |
| 7659 | if (!trap || !trap->name || !trap->group.name) |
| 7660 | return -EINVAL; |
| 7661 | |
| 7662 | if (trap->generic) |
| 7663 | return devlink_trap_generic_verify(trap); |
| 7664 | else |
| 7665 | return devlink_trap_driver_verify(trap); |
| 7666 | } |
| 7667 | |
| 7668 | static int |
| 7669 | devlink_trap_group_generic_verify(const struct devlink_trap_group *group) |
| 7670 | { |
| 7671 | if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX) |
| 7672 | return -EINVAL; |
| 7673 | |
| 7674 | if (strcmp(group->name, devlink_trap_group_generic[group->id].name)) |
| 7675 | return -EINVAL; |
| 7676 | |
| 7677 | return 0; |
| 7678 | } |
| 7679 | |
| 7680 | static int |
| 7681 | devlink_trap_group_driver_verify(const struct devlink_trap_group *group) |
| 7682 | { |
| 7683 | int i; |
| 7684 | |
| 7685 | if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX) |
| 7686 | return -EINVAL; |
| 7687 | |
| 7688 | for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) { |
| 7689 | if (!strcmp(group->name, devlink_trap_group_generic[i].name)) |
| 7690 | return -EEXIST; |
| 7691 | } |
| 7692 | |
| 7693 | return 0; |
| 7694 | } |
| 7695 | |
| 7696 | static int devlink_trap_group_verify(const struct devlink_trap_group *group) |
| 7697 | { |
| 7698 | if (group->generic) |
| 7699 | return devlink_trap_group_generic_verify(group); |
| 7700 | else |
| 7701 | return devlink_trap_group_driver_verify(group); |
| 7702 | } |
| 7703 | |
| 7704 | static void |
| 7705 | devlink_trap_group_notify(struct devlink *devlink, |
| 7706 | const struct devlink_trap_group_item *group_item, |
| 7707 | enum devlink_command cmd) |
| 7708 | { |
| 7709 | struct sk_buff *msg; |
| 7710 | int err; |
| 7711 | |
| 7712 | WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW && |
| 7713 | cmd != DEVLINK_CMD_TRAP_GROUP_DEL); |
| 7714 | |
| 7715 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 7716 | if (!msg) |
| 7717 | return; |
| 7718 | |
| 7719 | err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0, |
| 7720 | 0); |
| 7721 | if (err) { |
| 7722 | nlmsg_free(msg); |
| 7723 | return; |
| 7724 | } |
| 7725 | |
| 7726 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 7727 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 7728 | } |
| 7729 | |
| 7730 | static struct devlink_trap_group_item * |
| 7731 | devlink_trap_group_item_create(struct devlink *devlink, |
| 7732 | const struct devlink_trap_group *group) |
| 7733 | { |
| 7734 | struct devlink_trap_group_item *group_item; |
| 7735 | int err; |
| 7736 | |
| 7737 | err = devlink_trap_group_verify(group); |
| 7738 | if (err) |
| 7739 | return ERR_PTR(err); |
| 7740 | |
| 7741 | group_item = kzalloc(sizeof(*group_item), GFP_KERNEL); |
| 7742 | if (!group_item) |
| 7743 | return ERR_PTR(-ENOMEM); |
| 7744 | |
| 7745 | group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats); |
| 7746 | if (!group_item->stats) { |
| 7747 | err = -ENOMEM; |
| 7748 | goto err_stats_alloc; |
| 7749 | } |
| 7750 | |
| 7751 | group_item->group = group; |
| 7752 | refcount_set(&group_item->refcount, 1); |
| 7753 | |
| 7754 | if (devlink->ops->trap_group_init) { |
| 7755 | err = devlink->ops->trap_group_init(devlink, group); |
| 7756 | if (err) |
| 7757 | goto err_group_init; |
| 7758 | } |
| 7759 | |
| 7760 | list_add_tail(&group_item->list, &devlink->trap_group_list); |
| 7761 | devlink_trap_group_notify(devlink, group_item, |
| 7762 | DEVLINK_CMD_TRAP_GROUP_NEW); |
| 7763 | |
| 7764 | return group_item; |
| 7765 | |
| 7766 | err_group_init: |
| 7767 | free_percpu(group_item->stats); |
| 7768 | err_stats_alloc: |
| 7769 | kfree(group_item); |
| 7770 | return ERR_PTR(err); |
| 7771 | } |
| 7772 | |
| 7773 | static void |
| 7774 | devlink_trap_group_item_destroy(struct devlink *devlink, |
| 7775 | struct devlink_trap_group_item *group_item) |
| 7776 | { |
| 7777 | devlink_trap_group_notify(devlink, group_item, |
| 7778 | DEVLINK_CMD_TRAP_GROUP_DEL); |
| 7779 | list_del(&group_item->list); |
| 7780 | free_percpu(group_item->stats); |
| 7781 | kfree(group_item); |
| 7782 | } |
| 7783 | |
| 7784 | static struct devlink_trap_group_item * |
| 7785 | devlink_trap_group_item_get(struct devlink *devlink, |
| 7786 | const struct devlink_trap_group *group) |
| 7787 | { |
| 7788 | struct devlink_trap_group_item *group_item; |
| 7789 | |
| 7790 | group_item = devlink_trap_group_item_lookup(devlink, group->name); |
| 7791 | if (group_item) { |
| 7792 | refcount_inc(&group_item->refcount); |
| 7793 | return group_item; |
| 7794 | } |
| 7795 | |
| 7796 | return devlink_trap_group_item_create(devlink, group); |
| 7797 | } |
| 7798 | |
| 7799 | static void |
| 7800 | devlink_trap_group_item_put(struct devlink *devlink, |
| 7801 | struct devlink_trap_group_item *group_item) |
| 7802 | { |
| 7803 | if (!refcount_dec_and_test(&group_item->refcount)) |
| 7804 | return; |
| 7805 | |
| 7806 | devlink_trap_group_item_destroy(devlink, group_item); |
| 7807 | } |
| 7808 | |
| 7809 | static int |
| 7810 | devlink_trap_item_group_link(struct devlink *devlink, |
| 7811 | struct devlink_trap_item *trap_item) |
| 7812 | { |
| 7813 | struct devlink_trap_group_item *group_item; |
| 7814 | |
| 7815 | group_item = devlink_trap_group_item_get(devlink, |
| 7816 | &trap_item->trap->group); |
| 7817 | if (IS_ERR(group_item)) |
| 7818 | return PTR_ERR(group_item); |
| 7819 | |
| 7820 | trap_item->group_item = group_item; |
| 7821 | |
| 7822 | return 0; |
| 7823 | } |
| 7824 | |
| 7825 | static void |
| 7826 | devlink_trap_item_group_unlink(struct devlink *devlink, |
| 7827 | struct devlink_trap_item *trap_item) |
| 7828 | { |
| 7829 | devlink_trap_group_item_put(devlink, trap_item->group_item); |
| 7830 | } |
| 7831 | |
| 7832 | static void devlink_trap_notify(struct devlink *devlink, |
| 7833 | const struct devlink_trap_item *trap_item, |
| 7834 | enum devlink_command cmd) |
| 7835 | { |
| 7836 | struct sk_buff *msg; |
| 7837 | int err; |
| 7838 | |
| 7839 | WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW && |
| 7840 | cmd != DEVLINK_CMD_TRAP_DEL); |
| 7841 | |
| 7842 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 7843 | if (!msg) |
| 7844 | return; |
| 7845 | |
| 7846 | err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0); |
| 7847 | if (err) { |
| 7848 | nlmsg_free(msg); |
| 7849 | return; |
| 7850 | } |
| 7851 | |
| 7852 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 7853 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 7854 | } |
| 7855 | |
| 7856 | static int |
| 7857 | devlink_trap_register(struct devlink *devlink, |
| 7858 | const struct devlink_trap *trap, void *priv) |
| 7859 | { |
| 7860 | struct devlink_trap_item *trap_item; |
| 7861 | int err; |
| 7862 | |
| 7863 | if (devlink_trap_item_lookup(devlink, trap->name)) |
| 7864 | return -EEXIST; |
| 7865 | |
| 7866 | trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL); |
| 7867 | if (!trap_item) |
| 7868 | return -ENOMEM; |
| 7869 | |
| 7870 | trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats); |
| 7871 | if (!trap_item->stats) { |
| 7872 | err = -ENOMEM; |
| 7873 | goto err_stats_alloc; |
| 7874 | } |
| 7875 | |
| 7876 | trap_item->trap = trap; |
| 7877 | trap_item->action = trap->init_action; |
| 7878 | trap_item->priv = priv; |
| 7879 | |
| 7880 | err = devlink_trap_item_group_link(devlink, trap_item); |
| 7881 | if (err) |
| 7882 | goto err_group_link; |
| 7883 | |
| 7884 | err = devlink->ops->trap_init(devlink, trap, trap_item); |
| 7885 | if (err) |
| 7886 | goto err_trap_init; |
| 7887 | |
| 7888 | list_add_tail(&trap_item->list, &devlink->trap_list); |
| 7889 | devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW); |
| 7890 | |
| 7891 | return 0; |
| 7892 | |
| 7893 | err_trap_init: |
| 7894 | devlink_trap_item_group_unlink(devlink, trap_item); |
| 7895 | err_group_link: |
| 7896 | free_percpu(trap_item->stats); |
| 7897 | err_stats_alloc: |
| 7898 | kfree(trap_item); |
| 7899 | return err; |
| 7900 | } |
| 7901 | |
| 7902 | static void devlink_trap_unregister(struct devlink *devlink, |
| 7903 | const struct devlink_trap *trap) |
| 7904 | { |
| 7905 | struct devlink_trap_item *trap_item; |
| 7906 | |
| 7907 | trap_item = devlink_trap_item_lookup(devlink, trap->name); |
| 7908 | if (WARN_ON_ONCE(!trap_item)) |
| 7909 | return; |
| 7910 | |
| 7911 | devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL); |
| 7912 | list_del(&trap_item->list); |
| 7913 | if (devlink->ops->trap_fini) |
| 7914 | devlink->ops->trap_fini(devlink, trap, trap_item); |
| 7915 | devlink_trap_item_group_unlink(devlink, trap_item); |
| 7916 | free_percpu(trap_item->stats); |
| 7917 | kfree(trap_item); |
| 7918 | } |
| 7919 | |
| 7920 | static void devlink_trap_disable(struct devlink *devlink, |
| 7921 | const struct devlink_trap *trap) |
| 7922 | { |
| 7923 | struct devlink_trap_item *trap_item; |
| 7924 | |
| 7925 | trap_item = devlink_trap_item_lookup(devlink, trap->name); |
| 7926 | if (WARN_ON_ONCE(!trap_item)) |
| 7927 | return; |
| 7928 | |
| 7929 | devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP); |
| 7930 | trap_item->action = DEVLINK_TRAP_ACTION_DROP; |
| 7931 | } |
| 7932 | |
| 7933 | /** |
| 7934 | * devlink_traps_register - Register packet traps with devlink. |
| 7935 | * @devlink: devlink. |
| 7936 | * @traps: Packet traps. |
| 7937 | * @traps_count: Count of provided packet traps. |
| 7938 | * @priv: Driver private information. |
| 7939 | * |
| 7940 | * Return: Non-zero value on failure. |
| 7941 | */ |
| 7942 | int devlink_traps_register(struct devlink *devlink, |
| 7943 | const struct devlink_trap *traps, |
| 7944 | size_t traps_count, void *priv) |
| 7945 | { |
| 7946 | int i, err; |
| 7947 | |
| 7948 | if (!devlink->ops->trap_init || !devlink->ops->trap_action_set) |
| 7949 | return -EINVAL; |
| 7950 | |
| 7951 | mutex_lock(&devlink->lock); |
| 7952 | for (i = 0; i < traps_count; i++) { |
| 7953 | const struct devlink_trap *trap = &traps[i]; |
| 7954 | |
| 7955 | err = devlink_trap_verify(trap); |
| 7956 | if (err) |
| 7957 | goto err_trap_verify; |
| 7958 | |
| 7959 | err = devlink_trap_register(devlink, trap, priv); |
| 7960 | if (err) |
| 7961 | goto err_trap_register; |
| 7962 | } |
| 7963 | mutex_unlock(&devlink->lock); |
| 7964 | |
| 7965 | return 0; |
| 7966 | |
| 7967 | err_trap_register: |
| 7968 | err_trap_verify: |
| 7969 | for (i--; i >= 0; i--) |
| 7970 | devlink_trap_unregister(devlink, &traps[i]); |
| 7971 | mutex_unlock(&devlink->lock); |
| 7972 | return err; |
| 7973 | } |
| 7974 | EXPORT_SYMBOL_GPL(devlink_traps_register); |
| 7975 | |
| 7976 | /** |
| 7977 | * devlink_traps_unregister - Unregister packet traps from devlink. |
| 7978 | * @devlink: devlink. |
| 7979 | * @traps: Packet traps. |
| 7980 | * @traps_count: Count of provided packet traps. |
| 7981 | */ |
| 7982 | void devlink_traps_unregister(struct devlink *devlink, |
| 7983 | const struct devlink_trap *traps, |
| 7984 | size_t traps_count) |
| 7985 | { |
| 7986 | int i; |
| 7987 | |
| 7988 | mutex_lock(&devlink->lock); |
| 7989 | /* Make sure we do not have any packets in-flight while unregistering |
| 7990 | * traps by disabling all of them and waiting for a grace period. |
| 7991 | */ |
| 7992 | for (i = traps_count - 1; i >= 0; i--) |
| 7993 | devlink_trap_disable(devlink, &traps[i]); |
| 7994 | synchronize_rcu(); |
| 7995 | for (i = traps_count - 1; i >= 0; i--) |
| 7996 | devlink_trap_unregister(devlink, &traps[i]); |
| 7997 | mutex_unlock(&devlink->lock); |
| 7998 | } |
| 7999 | EXPORT_SYMBOL_GPL(devlink_traps_unregister); |
| 8000 | |
| 8001 | static void |
| 8002 | devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats, |
| 8003 | size_t skb_len) |
| 8004 | { |
| 8005 | struct devlink_stats *stats; |
| 8006 | |
| 8007 | stats = this_cpu_ptr(trap_stats); |
| 8008 | u64_stats_update_begin(&stats->syncp); |
| 8009 | stats->rx_bytes += skb_len; |
| 8010 | stats->rx_packets++; |
| 8011 | u64_stats_update_end(&stats->syncp); |
| 8012 | } |
| 8013 | |
| 8014 | static void |
| 8015 | devlink_trap_report_metadata_fill(struct net_dm_hw_metadata *hw_metadata, |
| 8016 | const struct devlink_trap_item *trap_item, |
| 8017 | struct devlink_port *in_devlink_port) |
| 8018 | { |
| 8019 | struct devlink_trap_group_item *group_item = trap_item->group_item; |
| 8020 | |
| 8021 | hw_metadata->trap_group_name = group_item->group->name; |
| 8022 | hw_metadata->trap_name = trap_item->trap->name; |
| 8023 | |
| 8024 | spin_lock(&in_devlink_port->type_lock); |
| 8025 | if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH) |
| 8026 | hw_metadata->input_dev = in_devlink_port->type_dev; |
| 8027 | spin_unlock(&in_devlink_port->type_lock); |
| 8028 | } |
| 8029 | |
| 8030 | /** |
| 8031 | * devlink_trap_report - Report trapped packet to drop monitor. |
| 8032 | * @devlink: devlink. |
| 8033 | * @skb: Trapped packet. |
| 8034 | * @trap_ctx: Trap context. |
| 8035 | * @in_devlink_port: Input devlink port. |
| 8036 | */ |
| 8037 | void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb, |
| 8038 | void *trap_ctx, struct devlink_port *in_devlink_port) |
| 8039 | { |
| 8040 | struct devlink_trap_item *trap_item = trap_ctx; |
| 8041 | struct net_dm_hw_metadata hw_metadata = {}; |
| 8042 | |
| 8043 | devlink_trap_stats_update(trap_item->stats, skb->len); |
| 8044 | devlink_trap_stats_update(trap_item->group_item->stats, skb->len); |
| 8045 | |
| 8046 | devlink_trap_report_metadata_fill(&hw_metadata, trap_item, |
| 8047 | in_devlink_port); |
| 8048 | net_dm_hw_report(skb, &hw_metadata); |
| 8049 | } |
| 8050 | EXPORT_SYMBOL_GPL(devlink_trap_report); |
| 8051 | |
| 8052 | /** |
| 8053 | * devlink_trap_ctx_priv - Trap context to driver private information. |
| 8054 | * @trap_ctx: Trap context. |
| 8055 | * |
| 8056 | * Return: Driver private information passed during registration. |
| 8057 | */ |
| 8058 | void *devlink_trap_ctx_priv(void *trap_ctx) |
| 8059 | { |
| 8060 | struct devlink_trap_item *trap_item = trap_ctx; |
| 8061 | |
| 8062 | return trap_item->priv; |
| 8063 | } |
| 8064 | EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv); |
| 8065 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 8066 | static void __devlink_compat_running_version(struct devlink *devlink, |
| 8067 | char *buf, size_t len) |
| 8068 | { |
| 8069 | const struct nlattr *nlattr; |
| 8070 | struct devlink_info_req req; |
| 8071 | struct sk_buff *msg; |
| 8072 | int rem, err; |
| 8073 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 8074 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 8075 | if (!msg) |
| 8076 | return; |
| 8077 | |
| 8078 | req.msg = msg; |
| 8079 | err = devlink->ops->info_get(devlink, &req, NULL); |
| 8080 | if (err) |
| 8081 | goto free_msg; |
| 8082 | |
| 8083 | nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) { |
| 8084 | const struct nlattr *kv; |
| 8085 | int rem_kv; |
| 8086 | |
| 8087 | if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING) |
| 8088 | continue; |
| 8089 | |
| 8090 | nla_for_each_nested(kv, nlattr, rem_kv) { |
| 8091 | if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE) |
| 8092 | continue; |
| 8093 | |
| 8094 | strlcat(buf, nla_data(kv), len); |
| 8095 | strlcat(buf, " ", len); |
| 8096 | } |
| 8097 | } |
| 8098 | free_msg: |
| 8099 | nlmsg_free(msg); |
| 8100 | } |
| 8101 | |
| 8102 | void devlink_compat_running_version(struct net_device *dev, |
| 8103 | char *buf, size_t len) |
| 8104 | { |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 8105 | struct devlink *devlink; |
| 8106 | |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 8107 | dev_hold(dev); |
| 8108 | rtnl_unlock(); |
| 8109 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 8110 | devlink = netdev_to_devlink(dev); |
Jakub Kicinski | be6fe1d | 2019-02-25 19:34:07 -0800 | [diff] [blame] | 8111 | if (!devlink || !devlink->ops->info_get) |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 8112 | goto out; |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 8113 | |
| 8114 | mutex_lock(&devlink->lock); |
| 8115 | __devlink_compat_running_version(devlink, buf, len); |
| 8116 | mutex_unlock(&devlink->lock); |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 8117 | |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 8118 | out: |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 8119 | rtnl_lock(); |
| 8120 | dev_put(dev); |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 8121 | } |
| 8122 | |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 8123 | int devlink_compat_flash_update(struct net_device *dev, const char *file_name) |
| 8124 | { |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 8125 | struct devlink *devlink; |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 8126 | int ret; |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 8127 | |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 8128 | dev_hold(dev); |
| 8129 | rtnl_unlock(); |
| 8130 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 8131 | devlink = netdev_to_devlink(dev); |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 8132 | if (!devlink || !devlink->ops->flash_update) { |
| 8133 | ret = -EOPNOTSUPP; |
| 8134 | goto out; |
| 8135 | } |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 8136 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 8137 | mutex_lock(&devlink->lock); |
| 8138 | ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL); |
| 8139 | mutex_unlock(&devlink->lock); |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 8140 | |
Jiri Pirko | e0dcd38 | 2019-03-24 11:14:29 +0100 | [diff] [blame] | 8141 | out: |
Jakub Kicinski | 1b45ff6 | 2019-02-25 19:34:06 -0800 | [diff] [blame] | 8142 | rtnl_lock(); |
| 8143 | dev_put(dev); |
| 8144 | |
Jakub Kicinski | b473b0d | 2019-02-25 19:34:03 -0800 | [diff] [blame] | 8145 | return ret; |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 8146 | } |
| 8147 | |
Jiri Pirko | af3836d | 2019-03-28 13:56:37 +0100 | [diff] [blame] | 8148 | int devlink_compat_phys_port_name_get(struct net_device *dev, |
| 8149 | char *name, size_t len) |
| 8150 | { |
| 8151 | struct devlink_port *devlink_port; |
| 8152 | |
| 8153 | /* RTNL mutex is held here which ensures that devlink_port |
| 8154 | * instance cannot disappear in the middle. No need to take |
| 8155 | * any devlink lock as only permanent values are accessed. |
| 8156 | */ |
| 8157 | ASSERT_RTNL(); |
| 8158 | |
| 8159 | devlink_port = netdev_to_devlink_port(dev); |
| 8160 | if (!devlink_port) |
| 8161 | return -EOPNOTSUPP; |
| 8162 | |
| 8163 | return __devlink_port_phys_port_name_get(devlink_port, name, len); |
| 8164 | } |
| 8165 | |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 8166 | int devlink_compat_switch_id_get(struct net_device *dev, |
| 8167 | struct netdev_phys_item_id *ppid) |
| 8168 | { |
| 8169 | struct devlink_port *devlink_port; |
| 8170 | |
Vlad Buslov | 043b841 | 2019-08-12 20:02:02 +0300 | [diff] [blame] | 8171 | /* Caller must hold RTNL mutex or reference to dev, which ensures that |
| 8172 | * devlink_port instance cannot disappear in the middle. No need to take |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 8173 | * any devlink lock as only permanent values are accessed. |
| 8174 | */ |
Jiri Pirko | 7e1146e | 2019-04-03 14:24:17 +0200 | [diff] [blame] | 8175 | devlink_port = netdev_to_devlink_port(dev); |
| 8176 | if (!devlink_port || !devlink_port->attrs.switch_port) |
| 8177 | return -EOPNOTSUPP; |
| 8178 | |
| 8179 | memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid)); |
| 8180 | |
| 8181 | return 0; |
| 8182 | } |
| 8183 | |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 8184 | static void __net_exit devlink_pernet_pre_exit(struct net *net) |
| 8185 | { |
| 8186 | struct devlink *devlink; |
| 8187 | int err; |
| 8188 | |
| 8189 | /* In case network namespace is getting destroyed, reload |
| 8190 | * all devlink instances from this namespace into init_net. |
| 8191 | */ |
| 8192 | mutex_lock(&devlink_mutex); |
| 8193 | list_for_each_entry(devlink, &devlink_list, list) { |
| 8194 | if (net_eq(devlink_net(devlink), net)) { |
| 8195 | if (WARN_ON(!devlink_reload_supported(devlink))) |
| 8196 | continue; |
| 8197 | err = devlink_reload(devlink, &init_net, NULL); |
| 8198 | if (err) |
| 8199 | pr_warn("Failed to reload devlink instance into init_net\n"); |
| 8200 | } |
| 8201 | } |
| 8202 | mutex_unlock(&devlink_mutex); |
| 8203 | } |
| 8204 | |
| 8205 | static struct pernet_operations devlink_pernet_ops __net_initdata = { |
| 8206 | .pre_exit = devlink_pernet_pre_exit, |
| 8207 | }; |
| 8208 | |
Jakub Kicinski | f4b6bcc | 2019-02-25 19:34:02 -0800 | [diff] [blame] | 8209 | static int __init devlink_init(void) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 8210 | { |
Jiri Pirko | 070c63f | 2019-10-03 11:49:39 +0200 | [diff] [blame] | 8211 | int err; |
| 8212 | |
| 8213 | err = genl_register_family(&devlink_nl_family); |
| 8214 | if (err) |
| 8215 | goto out; |
| 8216 | err = register_pernet_subsys(&devlink_pernet_ops); |
| 8217 | |
| 8218 | out: |
| 8219 | WARN_ON(err); |
| 8220 | return err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 8221 | } |
| 8222 | |
Jakub Kicinski | f4b6bcc | 2019-02-25 19:34:02 -0800 | [diff] [blame] | 8223 | subsys_initcall(devlink_init); |