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