Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/core/devlink.c - Network physical/parent device Netlink interface |
| 3 | * |
| 4 | * Heavily inspired by net/wireless/ |
| 5 | * Copyright (c) 2016 Mellanox Technologies. All rights reserved. |
| 6 | * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/gfp.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/netdevice.h> |
| 22 | #include <rdma/ib_verbs.h> |
| 23 | #include <net/netlink.h> |
| 24 | #include <net/genetlink.h> |
| 25 | #include <net/rtnetlink.h> |
| 26 | #include <net/net_namespace.h> |
| 27 | #include <net/sock.h> |
| 28 | #include <net/devlink.h> |
Jiri Pirko | e5224f0 | 2016-07-12 18:05:03 +0200 | [diff] [blame] | 29 | #define CREATE_TRACE_POINTS |
| 30 | #include <trace/events/devlink.h> |
| 31 | |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 32 | static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = { |
| 33 | { |
David Ahern | 12bdc5e | 2017-08-30 17:07:30 -0700 | [diff] [blame] | 34 | .name = "destination mac", |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 35 | .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC, |
| 36 | .bitwidth = 48, |
| 37 | }, |
| 38 | }; |
| 39 | |
| 40 | struct devlink_dpipe_header devlink_dpipe_header_ethernet = { |
| 41 | .name = "ethernet", |
| 42 | .id = DEVLINK_DPIPE_HEADER_ETHERNET, |
| 43 | .fields = devlink_dpipe_fields_ethernet, |
| 44 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet), |
| 45 | .global = true, |
| 46 | }; |
| 47 | EXPORT_SYMBOL(devlink_dpipe_header_ethernet); |
| 48 | |
Arkadi Sharshevsky | 3fb886e | 2017-08-24 08:40:00 +0200 | [diff] [blame] | 49 | static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = { |
| 50 | { |
| 51 | .name = "destination ip", |
| 52 | .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP, |
| 53 | .bitwidth = 32, |
| 54 | }, |
| 55 | }; |
| 56 | |
| 57 | struct devlink_dpipe_header devlink_dpipe_header_ipv4 = { |
| 58 | .name = "ipv4", |
| 59 | .id = DEVLINK_DPIPE_HEADER_IPV4, |
| 60 | .fields = devlink_dpipe_fields_ipv4, |
| 61 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4), |
| 62 | .global = true, |
| 63 | }; |
| 64 | EXPORT_SYMBOL(devlink_dpipe_header_ipv4); |
| 65 | |
Arkadi Sharshevsky | 1797f5b | 2017-08-31 17:59:12 +0200 | [diff] [blame] | 66 | static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = { |
| 67 | { |
| 68 | .name = "destination ip", |
| 69 | .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP, |
| 70 | .bitwidth = 128, |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | struct devlink_dpipe_header devlink_dpipe_header_ipv6 = { |
| 75 | .name = "ipv6", |
| 76 | .id = DEVLINK_DPIPE_HEADER_IPV6, |
| 77 | .fields = devlink_dpipe_fields_ipv6, |
| 78 | .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6), |
| 79 | .global = true, |
| 80 | }; |
| 81 | EXPORT_SYMBOL(devlink_dpipe_header_ipv6); |
| 82 | |
Jiri Pirko | e5224f0 | 2016-07-12 18:05:03 +0200 | [diff] [blame] | 83 | EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 84 | |
| 85 | static LIST_HEAD(devlink_list); |
| 86 | |
| 87 | /* devlink_mutex |
| 88 | * |
| 89 | * An overall lock guarding every operation coming from userspace. |
| 90 | * It also guards devlink devices list and it is taken when |
| 91 | * driver registers/unregisters it. |
| 92 | */ |
| 93 | static DEFINE_MUTEX(devlink_mutex); |
| 94 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 95 | static struct net *devlink_net(const struct devlink *devlink) |
| 96 | { |
| 97 | return read_pnet(&devlink->_net); |
| 98 | } |
| 99 | |
| 100 | static void devlink_net_set(struct devlink *devlink, struct net *net) |
| 101 | { |
| 102 | write_pnet(&devlink->_net, net); |
| 103 | } |
| 104 | |
| 105 | static struct devlink *devlink_get_from_attrs(struct net *net, |
| 106 | struct nlattr **attrs) |
| 107 | { |
| 108 | struct devlink *devlink; |
| 109 | char *busname; |
| 110 | char *devname; |
| 111 | |
| 112 | if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME]) |
| 113 | return ERR_PTR(-EINVAL); |
| 114 | |
| 115 | busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]); |
| 116 | devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]); |
| 117 | |
| 118 | list_for_each_entry(devlink, &devlink_list, list) { |
| 119 | if (strcmp(devlink->dev->bus->name, busname) == 0 && |
| 120 | strcmp(dev_name(devlink->dev), devname) == 0 && |
| 121 | net_eq(devlink_net(devlink), net)) |
| 122 | return devlink; |
| 123 | } |
| 124 | |
| 125 | return ERR_PTR(-ENODEV); |
| 126 | } |
| 127 | |
| 128 | static struct devlink *devlink_get_from_info(struct genl_info *info) |
| 129 | { |
| 130 | return devlink_get_from_attrs(genl_info_net(info), info->attrs); |
| 131 | } |
| 132 | |
| 133 | static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, |
| 134 | int port_index) |
| 135 | { |
| 136 | struct devlink_port *devlink_port; |
| 137 | |
| 138 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 139 | if (devlink_port->index == port_index) |
| 140 | return devlink_port; |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | static bool devlink_port_index_exists(struct devlink *devlink, int port_index) |
| 146 | { |
| 147 | return devlink_port_get_by_index(devlink, port_index); |
| 148 | } |
| 149 | |
| 150 | static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, |
| 151 | struct nlattr **attrs) |
| 152 | { |
| 153 | if (attrs[DEVLINK_ATTR_PORT_INDEX]) { |
| 154 | u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 155 | struct devlink_port *devlink_port; |
| 156 | |
| 157 | devlink_port = devlink_port_get_by_index(devlink, port_index); |
| 158 | if (!devlink_port) |
| 159 | return ERR_PTR(-ENODEV); |
| 160 | return devlink_port; |
| 161 | } |
| 162 | return ERR_PTR(-EINVAL); |
| 163 | } |
| 164 | |
| 165 | static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink, |
| 166 | struct genl_info *info) |
| 167 | { |
| 168 | return devlink_port_get_from_attrs(devlink, info->attrs); |
| 169 | } |
| 170 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 171 | struct devlink_sb { |
| 172 | struct list_head list; |
| 173 | unsigned int index; |
| 174 | u32 size; |
| 175 | u16 ingress_pools_count; |
| 176 | u16 egress_pools_count; |
| 177 | u16 ingress_tc_count; |
| 178 | u16 egress_tc_count; |
| 179 | }; |
| 180 | |
| 181 | static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb) |
| 182 | { |
| 183 | return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count; |
| 184 | } |
| 185 | |
| 186 | static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink, |
| 187 | unsigned int sb_index) |
| 188 | { |
| 189 | struct devlink_sb *devlink_sb; |
| 190 | |
| 191 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 192 | if (devlink_sb->index == sb_index) |
| 193 | return devlink_sb; |
| 194 | } |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | static bool devlink_sb_index_exists(struct devlink *devlink, |
| 199 | unsigned int sb_index) |
| 200 | { |
| 201 | return devlink_sb_get_by_index(devlink, sb_index); |
| 202 | } |
| 203 | |
| 204 | static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink, |
| 205 | struct nlattr **attrs) |
| 206 | { |
| 207 | if (attrs[DEVLINK_ATTR_SB_INDEX]) { |
| 208 | u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]); |
| 209 | struct devlink_sb *devlink_sb; |
| 210 | |
| 211 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 212 | if (!devlink_sb) |
| 213 | return ERR_PTR(-ENODEV); |
| 214 | return devlink_sb; |
| 215 | } |
| 216 | return ERR_PTR(-EINVAL); |
| 217 | } |
| 218 | |
| 219 | static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink, |
| 220 | struct genl_info *info) |
| 221 | { |
| 222 | return devlink_sb_get_from_attrs(devlink, info->attrs); |
| 223 | } |
| 224 | |
| 225 | static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 226 | struct nlattr **attrs, |
| 227 | u16 *p_pool_index) |
| 228 | { |
| 229 | u16 val; |
| 230 | |
| 231 | if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX]) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]); |
| 235 | if (val >= devlink_sb_pool_count(devlink_sb)) |
| 236 | return -EINVAL; |
| 237 | *p_pool_index = val; |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb, |
| 242 | struct genl_info *info, |
| 243 | u16 *p_pool_index) |
| 244 | { |
| 245 | return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs, |
| 246 | p_pool_index); |
| 247 | } |
| 248 | |
| 249 | static int |
| 250 | devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs, |
| 251 | enum devlink_sb_pool_type *p_pool_type) |
| 252 | { |
| 253 | u8 val; |
| 254 | |
| 255 | if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE]) |
| 256 | return -EINVAL; |
| 257 | |
| 258 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]); |
| 259 | if (val != DEVLINK_SB_POOL_TYPE_INGRESS && |
| 260 | val != DEVLINK_SB_POOL_TYPE_EGRESS) |
| 261 | return -EINVAL; |
| 262 | *p_pool_type = val; |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static int |
| 267 | devlink_sb_pool_type_get_from_info(struct genl_info *info, |
| 268 | enum devlink_sb_pool_type *p_pool_type) |
| 269 | { |
| 270 | return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type); |
| 271 | } |
| 272 | |
| 273 | static int |
| 274 | devlink_sb_th_type_get_from_attrs(struct nlattr **attrs, |
| 275 | enum devlink_sb_threshold_type *p_th_type) |
| 276 | { |
| 277 | u8 val; |
| 278 | |
| 279 | if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]) |
| 280 | return -EINVAL; |
| 281 | |
| 282 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]); |
| 283 | if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC && |
| 284 | val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC) |
| 285 | return -EINVAL; |
| 286 | *p_th_type = val; |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int |
| 291 | devlink_sb_th_type_get_from_info(struct genl_info *info, |
| 292 | enum devlink_sb_threshold_type *p_th_type) |
| 293 | { |
| 294 | return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type); |
| 295 | } |
| 296 | |
| 297 | static int |
| 298 | devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 299 | struct nlattr **attrs, |
| 300 | enum devlink_sb_pool_type pool_type, |
| 301 | u16 *p_tc_index) |
| 302 | { |
| 303 | u16 val; |
| 304 | |
| 305 | if (!attrs[DEVLINK_ATTR_SB_TC_INDEX]) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]); |
| 309 | if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS && |
| 310 | val >= devlink_sb->ingress_tc_count) |
| 311 | return -EINVAL; |
| 312 | if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS && |
| 313 | val >= devlink_sb->egress_tc_count) |
| 314 | return -EINVAL; |
| 315 | *p_tc_index = val; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int |
| 320 | devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb, |
| 321 | struct genl_info *info, |
| 322 | enum devlink_sb_pool_type pool_type, |
| 323 | u16 *p_tc_index) |
| 324 | { |
| 325 | return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs, |
| 326 | pool_type, p_tc_index); |
| 327 | } |
| 328 | |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 329 | #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0) |
| 330 | #define DEVLINK_NL_FLAG_NEED_PORT BIT(1) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 331 | #define DEVLINK_NL_FLAG_NEED_SB BIT(2) |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 332 | |
| 333 | /* The per devlink instance lock is taken by default in the pre-doit |
| 334 | * operation, yet several commands do not require this. The global |
| 335 | * devlink lock is taken and protects from disruption by user-calls. |
| 336 | */ |
| 337 | #define DEVLINK_NL_FLAG_NO_LOCK BIT(3) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 338 | |
| 339 | static int devlink_nl_pre_doit(const struct genl_ops *ops, |
| 340 | struct sk_buff *skb, struct genl_info *info) |
| 341 | { |
| 342 | struct devlink *devlink; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 343 | int err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 344 | |
| 345 | mutex_lock(&devlink_mutex); |
| 346 | devlink = devlink_get_from_info(info); |
| 347 | if (IS_ERR(devlink)) { |
| 348 | mutex_unlock(&devlink_mutex); |
| 349 | return PTR_ERR(devlink); |
| 350 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 351 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 352 | mutex_lock(&devlink->lock); |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 353 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 354 | info->user_ptr[0] = devlink; |
| 355 | } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 356 | struct devlink_port *devlink_port; |
| 357 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 358 | devlink_port = devlink_port_get_from_info(devlink, info); |
| 359 | if (IS_ERR(devlink_port)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 360 | err = PTR_ERR(devlink_port); |
| 361 | goto unlock; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 362 | } |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 363 | info->user_ptr[0] = devlink_port; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 364 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 365 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) { |
| 366 | struct devlink_sb *devlink_sb; |
| 367 | |
| 368 | devlink_sb = devlink_sb_get_from_info(devlink, info); |
| 369 | if (IS_ERR(devlink_sb)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 370 | err = PTR_ERR(devlink_sb); |
| 371 | goto unlock; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 372 | } |
| 373 | info->user_ptr[1] = devlink_sb; |
| 374 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 375 | return 0; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 376 | |
| 377 | unlock: |
| 378 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 379 | mutex_unlock(&devlink->lock); |
| 380 | mutex_unlock(&devlink_mutex); |
| 381 | return err; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static void devlink_nl_post_doit(const struct genl_ops *ops, |
| 385 | struct sk_buff *skb, struct genl_info *info) |
| 386 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 387 | struct devlink *devlink; |
| 388 | |
| 389 | devlink = devlink_get_from_info(info); |
| 390 | if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK) |
| 391 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 392 | mutex_unlock(&devlink_mutex); |
| 393 | } |
| 394 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 395 | static struct genl_family devlink_nl_family; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 396 | |
| 397 | enum devlink_multicast_groups { |
| 398 | DEVLINK_MCGRP_CONFIG, |
| 399 | }; |
| 400 | |
| 401 | static const struct genl_multicast_group devlink_nl_mcgrps[] = { |
| 402 | [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, |
| 403 | }; |
| 404 | |
| 405 | static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) |
| 406 | { |
| 407 | if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name)) |
| 408 | return -EMSGSIZE; |
| 409 | if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev))) |
| 410 | return -EMSGSIZE; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink, |
| 415 | enum devlink_command cmd, u32 portid, |
| 416 | u32 seq, int flags) |
| 417 | { |
| 418 | void *hdr; |
| 419 | |
| 420 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 421 | if (!hdr) |
| 422 | return -EMSGSIZE; |
| 423 | |
| 424 | if (devlink_nl_put_handle(msg, devlink)) |
| 425 | goto nla_put_failure; |
| 426 | |
| 427 | genlmsg_end(msg, hdr); |
| 428 | return 0; |
| 429 | |
| 430 | nla_put_failure: |
| 431 | genlmsg_cancel(msg, hdr); |
| 432 | return -EMSGSIZE; |
| 433 | } |
| 434 | |
| 435 | static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) |
| 436 | { |
| 437 | struct sk_buff *msg; |
| 438 | int err; |
| 439 | |
| 440 | WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); |
| 441 | |
| 442 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 443 | if (!msg) |
| 444 | return; |
| 445 | |
| 446 | err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0); |
| 447 | if (err) { |
| 448 | nlmsg_free(msg); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 453 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 454 | } |
| 455 | |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 456 | static int devlink_nl_port_attrs_put(struct sk_buff *msg, |
| 457 | struct devlink_port *devlink_port) |
| 458 | { |
| 459 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 460 | |
| 461 | if (!attrs->set) |
| 462 | return 0; |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 463 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour)) |
| 464 | return -EMSGSIZE; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 465 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number)) |
| 466 | return -EMSGSIZE; |
| 467 | if (!attrs->split) |
| 468 | return 0; |
| 469 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number)) |
| 470 | return -EMSGSIZE; |
| 471 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER, |
| 472 | attrs->split_subport_number)) |
| 473 | return -EMSGSIZE; |
| 474 | return 0; |
| 475 | } |
| 476 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 477 | static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, |
| 478 | struct devlink_port *devlink_port, |
| 479 | enum devlink_command cmd, u32 portid, |
| 480 | u32 seq, int flags) |
| 481 | { |
| 482 | void *hdr; |
| 483 | |
| 484 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 485 | if (!hdr) |
| 486 | return -EMSGSIZE; |
| 487 | |
| 488 | if (devlink_nl_put_handle(msg, devlink)) |
| 489 | goto nla_put_failure; |
| 490 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 491 | goto nla_put_failure; |
| 492 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) |
| 493 | goto nla_put_failure; |
| 494 | if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET && |
| 495 | nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE, |
| 496 | devlink_port->desired_type)) |
| 497 | goto nla_put_failure; |
| 498 | if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { |
| 499 | struct net_device *netdev = devlink_port->type_dev; |
| 500 | |
| 501 | if (netdev && |
| 502 | (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, |
| 503 | netdev->ifindex) || |
| 504 | nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, |
| 505 | netdev->name))) |
| 506 | goto nla_put_failure; |
| 507 | } |
| 508 | if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { |
| 509 | struct ib_device *ibdev = devlink_port->type_dev; |
| 510 | |
| 511 | if (ibdev && |
| 512 | nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME, |
| 513 | ibdev->name)) |
| 514 | goto nla_put_failure; |
| 515 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 516 | if (devlink_nl_port_attrs_put(msg, devlink_port)) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 517 | goto nla_put_failure; |
| 518 | |
| 519 | genlmsg_end(msg, hdr); |
| 520 | return 0; |
| 521 | |
| 522 | nla_put_failure: |
| 523 | genlmsg_cancel(msg, hdr); |
| 524 | return -EMSGSIZE; |
| 525 | } |
| 526 | |
| 527 | static void devlink_port_notify(struct devlink_port *devlink_port, |
| 528 | enum devlink_command cmd) |
| 529 | { |
| 530 | struct devlink *devlink = devlink_port->devlink; |
| 531 | struct sk_buff *msg; |
| 532 | int err; |
| 533 | |
| 534 | if (!devlink_port->registered) |
| 535 | return; |
| 536 | |
| 537 | WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); |
| 538 | |
| 539 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 540 | if (!msg) |
| 541 | return; |
| 542 | |
| 543 | err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0); |
| 544 | if (err) { |
| 545 | nlmsg_free(msg); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 550 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 551 | } |
| 552 | |
| 553 | static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info) |
| 554 | { |
| 555 | struct devlink *devlink = info->user_ptr[0]; |
| 556 | struct sk_buff *msg; |
| 557 | int err; |
| 558 | |
| 559 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 560 | if (!msg) |
| 561 | return -ENOMEM; |
| 562 | |
| 563 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 564 | info->snd_portid, info->snd_seq, 0); |
| 565 | if (err) { |
| 566 | nlmsg_free(msg); |
| 567 | return err; |
| 568 | } |
| 569 | |
| 570 | return genlmsg_reply(msg, info); |
| 571 | } |
| 572 | |
| 573 | static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg, |
| 574 | struct netlink_callback *cb) |
| 575 | { |
| 576 | struct devlink *devlink; |
| 577 | int start = cb->args[0]; |
| 578 | int idx = 0; |
| 579 | int err; |
| 580 | |
| 581 | mutex_lock(&devlink_mutex); |
| 582 | list_for_each_entry(devlink, &devlink_list, list) { |
| 583 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 584 | continue; |
| 585 | if (idx < start) { |
| 586 | idx++; |
| 587 | continue; |
| 588 | } |
| 589 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 590 | NETLINK_CB(cb->skb).portid, |
| 591 | cb->nlh->nlmsg_seq, NLM_F_MULTI); |
| 592 | if (err) |
| 593 | goto out; |
| 594 | idx++; |
| 595 | } |
| 596 | out: |
| 597 | mutex_unlock(&devlink_mutex); |
| 598 | |
| 599 | cb->args[0] = idx; |
| 600 | return msg->len; |
| 601 | } |
| 602 | |
| 603 | static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb, |
| 604 | struct genl_info *info) |
| 605 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 606 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 607 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 608 | struct sk_buff *msg; |
| 609 | int err; |
| 610 | |
| 611 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 612 | if (!msg) |
| 613 | return -ENOMEM; |
| 614 | |
| 615 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 616 | DEVLINK_CMD_PORT_NEW, |
| 617 | info->snd_portid, info->snd_seq, 0); |
| 618 | if (err) { |
| 619 | nlmsg_free(msg); |
| 620 | return err; |
| 621 | } |
| 622 | |
| 623 | return genlmsg_reply(msg, info); |
| 624 | } |
| 625 | |
| 626 | static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg, |
| 627 | struct netlink_callback *cb) |
| 628 | { |
| 629 | struct devlink *devlink; |
| 630 | struct devlink_port *devlink_port; |
| 631 | int start = cb->args[0]; |
| 632 | int idx = 0; |
| 633 | int err; |
| 634 | |
| 635 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 636 | list_for_each_entry(devlink, &devlink_list, list) { |
| 637 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 638 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 639 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 640 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 641 | if (idx < start) { |
| 642 | idx++; |
| 643 | continue; |
| 644 | } |
| 645 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 646 | DEVLINK_CMD_NEW, |
| 647 | NETLINK_CB(cb->skb).portid, |
| 648 | cb->nlh->nlmsg_seq, |
| 649 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 650 | if (err) { |
| 651 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 652 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 653 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 654 | idx++; |
| 655 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 656 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 657 | } |
| 658 | out: |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 659 | mutex_unlock(&devlink_mutex); |
| 660 | |
| 661 | cb->args[0] = idx; |
| 662 | return msg->len; |
| 663 | } |
| 664 | |
| 665 | static int devlink_port_type_set(struct devlink *devlink, |
| 666 | struct devlink_port *devlink_port, |
| 667 | enum devlink_port_type port_type) |
| 668 | |
| 669 | { |
| 670 | int err; |
| 671 | |
| 672 | if (devlink->ops && devlink->ops->port_type_set) { |
| 673 | if (port_type == DEVLINK_PORT_TYPE_NOTSET) |
| 674 | return -EINVAL; |
Elad Raz | 6edf101 | 2016-10-23 17:43:05 +0200 | [diff] [blame] | 675 | if (port_type == devlink_port->type) |
| 676 | return 0; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 677 | err = devlink->ops->port_type_set(devlink_port, port_type); |
| 678 | if (err) |
| 679 | return err; |
| 680 | devlink_port->desired_type = port_type; |
| 681 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 682 | return 0; |
| 683 | } |
| 684 | return -EOPNOTSUPP; |
| 685 | } |
| 686 | |
| 687 | static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, |
| 688 | struct genl_info *info) |
| 689 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 690 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 691 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 692 | int err; |
| 693 | |
| 694 | if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) { |
| 695 | enum devlink_port_type port_type; |
| 696 | |
| 697 | port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]); |
| 698 | err = devlink_port_type_set(devlink, devlink_port, port_type); |
| 699 | if (err) |
| 700 | return err; |
| 701 | } |
| 702 | return 0; |
| 703 | } |
| 704 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 705 | static int devlink_port_split(struct devlink *devlink, u32 port_index, |
| 706 | u32 count, struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 707 | |
| 708 | { |
| 709 | if (devlink->ops && devlink->ops->port_split) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 710 | return devlink->ops->port_split(devlink, port_index, count, |
| 711 | extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 712 | return -EOPNOTSUPP; |
| 713 | } |
| 714 | |
| 715 | static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, |
| 716 | struct genl_info *info) |
| 717 | { |
| 718 | struct devlink *devlink = info->user_ptr[0]; |
| 719 | u32 port_index; |
| 720 | u32 count; |
| 721 | |
| 722 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] || |
| 723 | !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]) |
| 724 | return -EINVAL; |
| 725 | |
| 726 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 727 | count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 728 | return devlink_port_split(devlink, port_index, count, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 729 | } |
| 730 | |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 731 | static int devlink_port_unsplit(struct devlink *devlink, u32 port_index, |
| 732 | struct netlink_ext_ack *extack) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 733 | |
| 734 | { |
| 735 | if (devlink->ops && devlink->ops->port_unsplit) |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 736 | return devlink->ops->port_unsplit(devlink, port_index, extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 737 | return -EOPNOTSUPP; |
| 738 | } |
| 739 | |
| 740 | static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb, |
| 741 | struct genl_info *info) |
| 742 | { |
| 743 | struct devlink *devlink = info->user_ptr[0]; |
| 744 | u32 port_index; |
| 745 | |
| 746 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) |
| 747 | return -EINVAL; |
| 748 | |
| 749 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 750 | return devlink_port_unsplit(devlink, port_index, info->extack); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 753 | static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink, |
| 754 | struct devlink_sb *devlink_sb, |
| 755 | enum devlink_command cmd, u32 portid, |
| 756 | u32 seq, int flags) |
| 757 | { |
| 758 | void *hdr; |
| 759 | |
| 760 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 761 | if (!hdr) |
| 762 | return -EMSGSIZE; |
| 763 | |
| 764 | if (devlink_nl_put_handle(msg, devlink)) |
| 765 | goto nla_put_failure; |
| 766 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 767 | goto nla_put_failure; |
| 768 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size)) |
| 769 | goto nla_put_failure; |
| 770 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT, |
| 771 | devlink_sb->ingress_pools_count)) |
| 772 | goto nla_put_failure; |
| 773 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT, |
| 774 | devlink_sb->egress_pools_count)) |
| 775 | goto nla_put_failure; |
| 776 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT, |
| 777 | devlink_sb->ingress_tc_count)) |
| 778 | goto nla_put_failure; |
| 779 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT, |
| 780 | devlink_sb->egress_tc_count)) |
| 781 | goto nla_put_failure; |
| 782 | |
| 783 | genlmsg_end(msg, hdr); |
| 784 | return 0; |
| 785 | |
| 786 | nla_put_failure: |
| 787 | genlmsg_cancel(msg, hdr); |
| 788 | return -EMSGSIZE; |
| 789 | } |
| 790 | |
| 791 | static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb, |
| 792 | struct genl_info *info) |
| 793 | { |
| 794 | struct devlink *devlink = info->user_ptr[0]; |
| 795 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 796 | struct sk_buff *msg; |
| 797 | int err; |
| 798 | |
| 799 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 800 | if (!msg) |
| 801 | return -ENOMEM; |
| 802 | |
| 803 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 804 | DEVLINK_CMD_SB_NEW, |
| 805 | info->snd_portid, info->snd_seq, 0); |
| 806 | if (err) { |
| 807 | nlmsg_free(msg); |
| 808 | return err; |
| 809 | } |
| 810 | |
| 811 | return genlmsg_reply(msg, info); |
| 812 | } |
| 813 | |
| 814 | static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg, |
| 815 | struct netlink_callback *cb) |
| 816 | { |
| 817 | struct devlink *devlink; |
| 818 | struct devlink_sb *devlink_sb; |
| 819 | int start = cb->args[0]; |
| 820 | int idx = 0; |
| 821 | int err; |
| 822 | |
| 823 | mutex_lock(&devlink_mutex); |
| 824 | list_for_each_entry(devlink, &devlink_list, list) { |
| 825 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 826 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 827 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 828 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 829 | if (idx < start) { |
| 830 | idx++; |
| 831 | continue; |
| 832 | } |
| 833 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 834 | DEVLINK_CMD_SB_NEW, |
| 835 | NETLINK_CB(cb->skb).portid, |
| 836 | cb->nlh->nlmsg_seq, |
| 837 | NLM_F_MULTI); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 838 | if (err) { |
| 839 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 840 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 841 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 842 | idx++; |
| 843 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 844 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 845 | } |
| 846 | out: |
| 847 | mutex_unlock(&devlink_mutex); |
| 848 | |
| 849 | cb->args[0] = idx; |
| 850 | return msg->len; |
| 851 | } |
| 852 | |
| 853 | static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink, |
| 854 | struct devlink_sb *devlink_sb, |
| 855 | u16 pool_index, enum devlink_command cmd, |
| 856 | u32 portid, u32 seq, int flags) |
| 857 | { |
| 858 | struct devlink_sb_pool_info pool_info; |
| 859 | void *hdr; |
| 860 | int err; |
| 861 | |
| 862 | err = devlink->ops->sb_pool_get(devlink, devlink_sb->index, |
| 863 | pool_index, &pool_info); |
| 864 | if (err) |
| 865 | return err; |
| 866 | |
| 867 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 868 | if (!hdr) |
| 869 | return -EMSGSIZE; |
| 870 | |
| 871 | if (devlink_nl_put_handle(msg, devlink)) |
| 872 | goto nla_put_failure; |
| 873 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 874 | goto nla_put_failure; |
| 875 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 876 | goto nla_put_failure; |
| 877 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type)) |
| 878 | goto nla_put_failure; |
| 879 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size)) |
| 880 | goto nla_put_failure; |
| 881 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE, |
| 882 | pool_info.threshold_type)) |
| 883 | goto nla_put_failure; |
| 884 | |
| 885 | genlmsg_end(msg, hdr); |
| 886 | return 0; |
| 887 | |
| 888 | nla_put_failure: |
| 889 | genlmsg_cancel(msg, hdr); |
| 890 | return -EMSGSIZE; |
| 891 | } |
| 892 | |
| 893 | static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb, |
| 894 | struct genl_info *info) |
| 895 | { |
| 896 | struct devlink *devlink = info->user_ptr[0]; |
| 897 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 898 | struct sk_buff *msg; |
| 899 | u16 pool_index; |
| 900 | int err; |
| 901 | |
| 902 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 903 | &pool_index); |
| 904 | if (err) |
| 905 | return err; |
| 906 | |
| 907 | if (!devlink->ops || !devlink->ops->sb_pool_get) |
| 908 | return -EOPNOTSUPP; |
| 909 | |
| 910 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 911 | if (!msg) |
| 912 | return -ENOMEM; |
| 913 | |
| 914 | err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index, |
| 915 | DEVLINK_CMD_SB_POOL_NEW, |
| 916 | info->snd_portid, info->snd_seq, 0); |
| 917 | if (err) { |
| 918 | nlmsg_free(msg); |
| 919 | return err; |
| 920 | } |
| 921 | |
| 922 | return genlmsg_reply(msg, info); |
| 923 | } |
| 924 | |
| 925 | static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 926 | struct devlink *devlink, |
| 927 | struct devlink_sb *devlink_sb, |
| 928 | u32 portid, u32 seq) |
| 929 | { |
| 930 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 931 | u16 pool_index; |
| 932 | int err; |
| 933 | |
| 934 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 935 | if (*p_idx < start) { |
| 936 | (*p_idx)++; |
| 937 | continue; |
| 938 | } |
| 939 | err = devlink_nl_sb_pool_fill(msg, devlink, |
| 940 | devlink_sb, |
| 941 | pool_index, |
| 942 | DEVLINK_CMD_SB_POOL_NEW, |
| 943 | portid, seq, NLM_F_MULTI); |
| 944 | if (err) |
| 945 | return err; |
| 946 | (*p_idx)++; |
| 947 | } |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg, |
| 952 | struct netlink_callback *cb) |
| 953 | { |
| 954 | struct devlink *devlink; |
| 955 | struct devlink_sb *devlink_sb; |
| 956 | int start = cb->args[0]; |
| 957 | int idx = 0; |
| 958 | int err; |
| 959 | |
| 960 | mutex_lock(&devlink_mutex); |
| 961 | list_for_each_entry(devlink, &devlink_list, list) { |
| 962 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
| 963 | !devlink->ops || !devlink->ops->sb_pool_get) |
| 964 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 965 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 966 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 967 | err = __sb_pool_get_dumpit(msg, start, &idx, devlink, |
| 968 | devlink_sb, |
| 969 | NETLINK_CB(cb->skb).portid, |
| 970 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 971 | if (err && err != -EOPNOTSUPP) { |
| 972 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 973 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 974 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 975 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 976 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 977 | } |
| 978 | out: |
| 979 | mutex_unlock(&devlink_mutex); |
| 980 | |
| 981 | cb->args[0] = idx; |
| 982 | return msg->len; |
| 983 | } |
| 984 | |
| 985 | static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index, |
| 986 | u16 pool_index, u32 size, |
| 987 | enum devlink_sb_threshold_type threshold_type) |
| 988 | |
| 989 | { |
| 990 | const struct devlink_ops *ops = devlink->ops; |
| 991 | |
| 992 | if (ops && ops->sb_pool_set) |
| 993 | return ops->sb_pool_set(devlink, sb_index, pool_index, |
| 994 | size, threshold_type); |
| 995 | return -EOPNOTSUPP; |
| 996 | } |
| 997 | |
| 998 | static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb, |
| 999 | struct genl_info *info) |
| 1000 | { |
| 1001 | struct devlink *devlink = info->user_ptr[0]; |
| 1002 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1003 | enum devlink_sb_threshold_type threshold_type; |
| 1004 | u16 pool_index; |
| 1005 | u32 size; |
| 1006 | int err; |
| 1007 | |
| 1008 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1009 | &pool_index); |
| 1010 | if (err) |
| 1011 | return err; |
| 1012 | |
| 1013 | err = devlink_sb_th_type_get_from_info(info, &threshold_type); |
| 1014 | if (err) |
| 1015 | return err; |
| 1016 | |
| 1017 | if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]) |
| 1018 | return -EINVAL; |
| 1019 | |
| 1020 | size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]); |
| 1021 | return devlink_sb_pool_set(devlink, devlink_sb->index, |
| 1022 | pool_index, size, threshold_type); |
| 1023 | } |
| 1024 | |
| 1025 | static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg, |
| 1026 | struct devlink *devlink, |
| 1027 | struct devlink_port *devlink_port, |
| 1028 | struct devlink_sb *devlink_sb, |
| 1029 | u16 pool_index, |
| 1030 | enum devlink_command cmd, |
| 1031 | u32 portid, u32 seq, int flags) |
| 1032 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1033 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1034 | u32 threshold; |
| 1035 | void *hdr; |
| 1036 | int err; |
| 1037 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1038 | err = ops->sb_port_pool_get(devlink_port, devlink_sb->index, |
| 1039 | pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1040 | if (err) |
| 1041 | return err; |
| 1042 | |
| 1043 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1044 | if (!hdr) |
| 1045 | return -EMSGSIZE; |
| 1046 | |
| 1047 | if (devlink_nl_put_handle(msg, devlink)) |
| 1048 | goto nla_put_failure; |
| 1049 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1050 | goto nla_put_failure; |
| 1051 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1052 | goto nla_put_failure; |
| 1053 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1054 | goto nla_put_failure; |
| 1055 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1056 | goto nla_put_failure; |
| 1057 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1058 | if (ops->sb_occ_port_pool_get) { |
| 1059 | u32 cur; |
| 1060 | u32 max; |
| 1061 | |
| 1062 | err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index, |
| 1063 | pool_index, &cur, &max); |
| 1064 | if (err && err != -EOPNOTSUPP) |
| 1065 | return err; |
| 1066 | if (!err) { |
| 1067 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1068 | goto nla_put_failure; |
| 1069 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1070 | goto nla_put_failure; |
| 1071 | } |
| 1072 | } |
| 1073 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1074 | genlmsg_end(msg, hdr); |
| 1075 | return 0; |
| 1076 | |
| 1077 | nla_put_failure: |
| 1078 | genlmsg_cancel(msg, hdr); |
| 1079 | return -EMSGSIZE; |
| 1080 | } |
| 1081 | |
| 1082 | static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb, |
| 1083 | struct genl_info *info) |
| 1084 | { |
| 1085 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1086 | struct devlink *devlink = devlink_port->devlink; |
| 1087 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1088 | struct sk_buff *msg; |
| 1089 | u16 pool_index; |
| 1090 | int err; |
| 1091 | |
| 1092 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1093 | &pool_index); |
| 1094 | if (err) |
| 1095 | return err; |
| 1096 | |
| 1097 | if (!devlink->ops || !devlink->ops->sb_port_pool_get) |
| 1098 | return -EOPNOTSUPP; |
| 1099 | |
| 1100 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1101 | if (!msg) |
| 1102 | return -ENOMEM; |
| 1103 | |
| 1104 | err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port, |
| 1105 | devlink_sb, pool_index, |
| 1106 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1107 | info->snd_portid, info->snd_seq, 0); |
| 1108 | if (err) { |
| 1109 | nlmsg_free(msg); |
| 1110 | return err; |
| 1111 | } |
| 1112 | |
| 1113 | return genlmsg_reply(msg, info); |
| 1114 | } |
| 1115 | |
| 1116 | static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1117 | struct devlink *devlink, |
| 1118 | struct devlink_sb *devlink_sb, |
| 1119 | u32 portid, u32 seq) |
| 1120 | { |
| 1121 | struct devlink_port *devlink_port; |
| 1122 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1123 | u16 pool_index; |
| 1124 | int err; |
| 1125 | |
| 1126 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1127 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1128 | if (*p_idx < start) { |
| 1129 | (*p_idx)++; |
| 1130 | continue; |
| 1131 | } |
| 1132 | err = devlink_nl_sb_port_pool_fill(msg, devlink, |
| 1133 | devlink_port, |
| 1134 | devlink_sb, |
| 1135 | pool_index, |
| 1136 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1137 | portid, seq, |
| 1138 | NLM_F_MULTI); |
| 1139 | if (err) |
| 1140 | return err; |
| 1141 | (*p_idx)++; |
| 1142 | } |
| 1143 | } |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg, |
| 1148 | struct netlink_callback *cb) |
| 1149 | { |
| 1150 | struct devlink *devlink; |
| 1151 | struct devlink_sb *devlink_sb; |
| 1152 | int start = cb->args[0]; |
| 1153 | int idx = 0; |
| 1154 | int err; |
| 1155 | |
| 1156 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1157 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1158 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
| 1159 | !devlink->ops || !devlink->ops->sb_port_pool_get) |
| 1160 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1161 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1162 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1163 | err = __sb_port_pool_get_dumpit(msg, start, &idx, |
| 1164 | devlink, devlink_sb, |
| 1165 | NETLINK_CB(cb->skb).portid, |
| 1166 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1167 | if (err && err != -EOPNOTSUPP) { |
| 1168 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1169 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1170 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1171 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1172 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1173 | } |
| 1174 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1175 | mutex_unlock(&devlink_mutex); |
| 1176 | |
| 1177 | cb->args[0] = idx; |
| 1178 | return msg->len; |
| 1179 | } |
| 1180 | |
| 1181 | static int devlink_sb_port_pool_set(struct devlink_port *devlink_port, |
| 1182 | unsigned int sb_index, u16 pool_index, |
| 1183 | u32 threshold) |
| 1184 | |
| 1185 | { |
| 1186 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1187 | |
| 1188 | if (ops && ops->sb_port_pool_set) |
| 1189 | return ops->sb_port_pool_set(devlink_port, sb_index, |
| 1190 | pool_index, threshold); |
| 1191 | return -EOPNOTSUPP; |
| 1192 | } |
| 1193 | |
| 1194 | static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb, |
| 1195 | struct genl_info *info) |
| 1196 | { |
| 1197 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1198 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1199 | u16 pool_index; |
| 1200 | u32 threshold; |
| 1201 | int err; |
| 1202 | |
| 1203 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1204 | &pool_index); |
| 1205 | if (err) |
| 1206 | return err; |
| 1207 | |
| 1208 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1209 | return -EINVAL; |
| 1210 | |
| 1211 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1212 | return devlink_sb_port_pool_set(devlink_port, devlink_sb->index, |
| 1213 | pool_index, threshold); |
| 1214 | } |
| 1215 | |
| 1216 | static int |
| 1217 | devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1218 | struct devlink_port *devlink_port, |
| 1219 | struct devlink_sb *devlink_sb, u16 tc_index, |
| 1220 | enum devlink_sb_pool_type pool_type, |
| 1221 | enum devlink_command cmd, |
| 1222 | u32 portid, u32 seq, int flags) |
| 1223 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1224 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1225 | u16 pool_index; |
| 1226 | u32 threshold; |
| 1227 | void *hdr; |
| 1228 | int err; |
| 1229 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1230 | err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index, |
| 1231 | tc_index, pool_type, |
| 1232 | &pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1233 | if (err) |
| 1234 | return err; |
| 1235 | |
| 1236 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1237 | if (!hdr) |
| 1238 | return -EMSGSIZE; |
| 1239 | |
| 1240 | if (devlink_nl_put_handle(msg, devlink)) |
| 1241 | goto nla_put_failure; |
| 1242 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1243 | goto nla_put_failure; |
| 1244 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1245 | goto nla_put_failure; |
| 1246 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index)) |
| 1247 | goto nla_put_failure; |
| 1248 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type)) |
| 1249 | goto nla_put_failure; |
| 1250 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1251 | goto nla_put_failure; |
| 1252 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1253 | goto nla_put_failure; |
| 1254 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1255 | if (ops->sb_occ_tc_port_bind_get) { |
| 1256 | u32 cur; |
| 1257 | u32 max; |
| 1258 | |
| 1259 | err = ops->sb_occ_tc_port_bind_get(devlink_port, |
| 1260 | devlink_sb->index, |
| 1261 | tc_index, pool_type, |
| 1262 | &cur, &max); |
| 1263 | if (err && err != -EOPNOTSUPP) |
| 1264 | return err; |
| 1265 | if (!err) { |
| 1266 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1267 | goto nla_put_failure; |
| 1268 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1269 | goto nla_put_failure; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1273 | genlmsg_end(msg, hdr); |
| 1274 | return 0; |
| 1275 | |
| 1276 | nla_put_failure: |
| 1277 | genlmsg_cancel(msg, hdr); |
| 1278 | return -EMSGSIZE; |
| 1279 | } |
| 1280 | |
| 1281 | static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb, |
| 1282 | struct genl_info *info) |
| 1283 | { |
| 1284 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1285 | struct devlink *devlink = devlink_port->devlink; |
| 1286 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1287 | struct sk_buff *msg; |
| 1288 | enum devlink_sb_pool_type pool_type; |
| 1289 | u16 tc_index; |
| 1290 | int err; |
| 1291 | |
| 1292 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1293 | if (err) |
| 1294 | return err; |
| 1295 | |
| 1296 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1297 | pool_type, &tc_index); |
| 1298 | if (err) |
| 1299 | return err; |
| 1300 | |
| 1301 | if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get) |
| 1302 | return -EOPNOTSUPP; |
| 1303 | |
| 1304 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1305 | if (!msg) |
| 1306 | return -ENOMEM; |
| 1307 | |
| 1308 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port, |
| 1309 | devlink_sb, tc_index, pool_type, |
| 1310 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1311 | info->snd_portid, |
| 1312 | info->snd_seq, 0); |
| 1313 | if (err) { |
| 1314 | nlmsg_free(msg); |
| 1315 | return err; |
| 1316 | } |
| 1317 | |
| 1318 | return genlmsg_reply(msg, info); |
| 1319 | } |
| 1320 | |
| 1321 | static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1322 | int start, int *p_idx, |
| 1323 | struct devlink *devlink, |
| 1324 | struct devlink_sb *devlink_sb, |
| 1325 | u32 portid, u32 seq) |
| 1326 | { |
| 1327 | struct devlink_port *devlink_port; |
| 1328 | u16 tc_index; |
| 1329 | int err; |
| 1330 | |
| 1331 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1332 | for (tc_index = 0; |
| 1333 | tc_index < devlink_sb->ingress_tc_count; tc_index++) { |
| 1334 | if (*p_idx < start) { |
| 1335 | (*p_idx)++; |
| 1336 | continue; |
| 1337 | } |
| 1338 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1339 | devlink_port, |
| 1340 | devlink_sb, |
| 1341 | tc_index, |
| 1342 | DEVLINK_SB_POOL_TYPE_INGRESS, |
| 1343 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1344 | portid, seq, |
| 1345 | NLM_F_MULTI); |
| 1346 | if (err) |
| 1347 | return err; |
| 1348 | (*p_idx)++; |
| 1349 | } |
| 1350 | for (tc_index = 0; |
| 1351 | tc_index < devlink_sb->egress_tc_count; tc_index++) { |
| 1352 | if (*p_idx < start) { |
| 1353 | (*p_idx)++; |
| 1354 | continue; |
| 1355 | } |
| 1356 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1357 | devlink_port, |
| 1358 | devlink_sb, |
| 1359 | tc_index, |
| 1360 | DEVLINK_SB_POOL_TYPE_EGRESS, |
| 1361 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1362 | portid, seq, |
| 1363 | NLM_F_MULTI); |
| 1364 | if (err) |
| 1365 | return err; |
| 1366 | (*p_idx)++; |
| 1367 | } |
| 1368 | } |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | static int |
| 1373 | devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1374 | struct netlink_callback *cb) |
| 1375 | { |
| 1376 | struct devlink *devlink; |
| 1377 | struct devlink_sb *devlink_sb; |
| 1378 | int start = cb->args[0]; |
| 1379 | int idx = 0; |
| 1380 | int err; |
| 1381 | |
| 1382 | mutex_lock(&devlink_mutex); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1383 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1384 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
| 1385 | !devlink->ops || !devlink->ops->sb_tc_pool_bind_get) |
| 1386 | continue; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1387 | |
| 1388 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1389 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1390 | err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx, |
| 1391 | devlink, |
| 1392 | devlink_sb, |
| 1393 | NETLINK_CB(cb->skb).portid, |
| 1394 | cb->nlh->nlmsg_seq); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1395 | if (err && err != -EOPNOTSUPP) { |
| 1396 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1397 | goto out; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1398 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1399 | } |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 1400 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1401 | } |
| 1402 | out: |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1403 | mutex_unlock(&devlink_mutex); |
| 1404 | |
| 1405 | cb->args[0] = idx; |
| 1406 | return msg->len; |
| 1407 | } |
| 1408 | |
| 1409 | static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port, |
| 1410 | unsigned int sb_index, u16 tc_index, |
| 1411 | enum devlink_sb_pool_type pool_type, |
| 1412 | u16 pool_index, u32 threshold) |
| 1413 | |
| 1414 | { |
| 1415 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1416 | |
| 1417 | if (ops && ops->sb_tc_pool_bind_set) |
| 1418 | return ops->sb_tc_pool_bind_set(devlink_port, sb_index, |
| 1419 | tc_index, pool_type, |
| 1420 | pool_index, threshold); |
| 1421 | return -EOPNOTSUPP; |
| 1422 | } |
| 1423 | |
| 1424 | static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb, |
| 1425 | struct genl_info *info) |
| 1426 | { |
| 1427 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1428 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1429 | enum devlink_sb_pool_type pool_type; |
| 1430 | u16 tc_index; |
| 1431 | u16 pool_index; |
| 1432 | u32 threshold; |
| 1433 | int err; |
| 1434 | |
| 1435 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1436 | if (err) |
| 1437 | return err; |
| 1438 | |
| 1439 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1440 | pool_type, &tc_index); |
| 1441 | if (err) |
| 1442 | return err; |
| 1443 | |
| 1444 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1445 | &pool_index); |
| 1446 | if (err) |
| 1447 | return err; |
| 1448 | |
| 1449 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1450 | return -EINVAL; |
| 1451 | |
| 1452 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1453 | return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index, |
| 1454 | tc_index, pool_type, |
| 1455 | pool_index, threshold); |
| 1456 | } |
| 1457 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1458 | static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb, |
| 1459 | struct genl_info *info) |
| 1460 | { |
| 1461 | struct devlink *devlink = info->user_ptr[0]; |
| 1462 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1463 | const struct devlink_ops *ops = devlink->ops; |
| 1464 | |
| 1465 | if (ops && ops->sb_occ_snapshot) |
| 1466 | return ops->sb_occ_snapshot(devlink, devlink_sb->index); |
| 1467 | return -EOPNOTSUPP; |
| 1468 | } |
| 1469 | |
| 1470 | static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb, |
| 1471 | struct genl_info *info) |
| 1472 | { |
| 1473 | struct devlink *devlink = info->user_ptr[0]; |
| 1474 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1475 | const struct devlink_ops *ops = devlink->ops; |
| 1476 | |
| 1477 | if (ops && ops->sb_occ_max_clear) |
| 1478 | return ops->sb_occ_max_clear(devlink, devlink_sb->index); |
| 1479 | return -EOPNOTSUPP; |
| 1480 | } |
| 1481 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1482 | static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1483 | enum devlink_command cmd, u32 portid, |
| 1484 | u32 seq, int flags) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1485 | { |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1486 | const struct devlink_ops *ops = devlink->ops; |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1487 | u8 inline_mode, encap_mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1488 | void *hdr; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1489 | int err = 0; |
| 1490 | u16 mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1491 | |
| 1492 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1493 | if (!hdr) |
| 1494 | return -EMSGSIZE; |
| 1495 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1496 | err = devlink_nl_put_handle(msg, devlink); |
| 1497 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1498 | goto nla_put_failure; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1499 | |
Jiri Pirko | 4456f61 | 2017-02-09 15:54:36 +0100 | [diff] [blame] | 1500 | if (ops->eswitch_mode_get) { |
| 1501 | err = ops->eswitch_mode_get(devlink, &mode); |
| 1502 | if (err) |
| 1503 | goto nla_put_failure; |
| 1504 | err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode); |
| 1505 | if (err) |
| 1506 | goto nla_put_failure; |
| 1507 | } |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1508 | |
| 1509 | if (ops->eswitch_inline_mode_get) { |
| 1510 | err = ops->eswitch_inline_mode_get(devlink, &inline_mode); |
| 1511 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1512 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1513 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE, |
| 1514 | inline_mode); |
| 1515 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1516 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1517 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1518 | |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1519 | if (ops->eswitch_encap_mode_get) { |
| 1520 | err = ops->eswitch_encap_mode_get(devlink, &encap_mode); |
| 1521 | if (err) |
| 1522 | goto nla_put_failure; |
| 1523 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode); |
| 1524 | if (err) |
| 1525 | goto nla_put_failure; |
| 1526 | } |
| 1527 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1528 | genlmsg_end(msg, hdr); |
| 1529 | return 0; |
| 1530 | |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1531 | nla_put_failure: |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1532 | genlmsg_cancel(msg, hdr); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1533 | return err; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1534 | } |
| 1535 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1536 | static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, |
| 1537 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1538 | { |
| 1539 | struct devlink *devlink = info->user_ptr[0]; |
| 1540 | const struct devlink_ops *ops = devlink->ops; |
| 1541 | struct sk_buff *msg; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1542 | int err; |
| 1543 | |
Jiri Pirko | 4456f61 | 2017-02-09 15:54:36 +0100 | [diff] [blame] | 1544 | if (!ops) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1545 | return -EOPNOTSUPP; |
| 1546 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1547 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1548 | if (!msg) |
| 1549 | return -ENOMEM; |
| 1550 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1551 | err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET, |
| 1552 | info->snd_portid, info->snd_seq, 0); |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1553 | |
| 1554 | if (err) { |
| 1555 | nlmsg_free(msg); |
| 1556 | return err; |
| 1557 | } |
| 1558 | |
| 1559 | return genlmsg_reply(msg, info); |
| 1560 | } |
| 1561 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1562 | static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, |
| 1563 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1564 | { |
| 1565 | struct devlink *devlink = info->user_ptr[0]; |
| 1566 | const struct devlink_ops *ops = devlink->ops; |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1567 | u8 inline_mode, encap_mode; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1568 | int err = 0; |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1569 | u16 mode; |
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 | if (!ops) |
| 1572 | return -EOPNOTSUPP; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1573 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1574 | if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) { |
| 1575 | if (!ops->eswitch_mode_set) |
| 1576 | return -EOPNOTSUPP; |
| 1577 | mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]); |
| 1578 | err = ops->eswitch_mode_set(devlink, mode); |
| 1579 | if (err) |
| 1580 | return err; |
| 1581 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1582 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1583 | if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) { |
| 1584 | if (!ops->eswitch_inline_mode_set) |
| 1585 | return -EOPNOTSUPP; |
| 1586 | inline_mode = nla_get_u8( |
| 1587 | info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]); |
| 1588 | err = ops->eswitch_inline_mode_set(devlink, inline_mode); |
| 1589 | if (err) |
| 1590 | return err; |
| 1591 | } |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 1592 | |
| 1593 | if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) { |
| 1594 | if (!ops->eswitch_encap_mode_set) |
| 1595 | return -EOPNOTSUPP; |
| 1596 | encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]); |
| 1597 | err = ops->eswitch_encap_mode_set(devlink, encap_mode); |
| 1598 | if (err) |
| 1599 | return err; |
| 1600 | } |
| 1601 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1602 | return 0; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1603 | } |
| 1604 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1605 | int devlink_dpipe_match_put(struct sk_buff *skb, |
| 1606 | struct devlink_dpipe_match *match) |
| 1607 | { |
| 1608 | struct devlink_dpipe_header *header = match->header; |
| 1609 | struct devlink_dpipe_field *field = &header->fields[match->field_id]; |
| 1610 | struct nlattr *match_attr; |
| 1611 | |
| 1612 | match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH); |
| 1613 | if (!match_attr) |
| 1614 | return -EMSGSIZE; |
| 1615 | |
| 1616 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) || |
| 1617 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) || |
| 1618 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1619 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1620 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1621 | goto nla_put_failure; |
| 1622 | |
| 1623 | nla_nest_end(skb, match_attr); |
| 1624 | return 0; |
| 1625 | |
| 1626 | nla_put_failure: |
| 1627 | nla_nest_cancel(skb, match_attr); |
| 1628 | return -EMSGSIZE; |
| 1629 | } |
| 1630 | EXPORT_SYMBOL_GPL(devlink_dpipe_match_put); |
| 1631 | |
| 1632 | static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table, |
| 1633 | struct sk_buff *skb) |
| 1634 | { |
| 1635 | struct nlattr *matches_attr; |
| 1636 | |
| 1637 | matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES); |
| 1638 | if (!matches_attr) |
| 1639 | return -EMSGSIZE; |
| 1640 | |
| 1641 | if (table->table_ops->matches_dump(table->priv, skb)) |
| 1642 | goto nla_put_failure; |
| 1643 | |
| 1644 | nla_nest_end(skb, matches_attr); |
| 1645 | return 0; |
| 1646 | |
| 1647 | nla_put_failure: |
| 1648 | nla_nest_cancel(skb, matches_attr); |
| 1649 | return -EMSGSIZE; |
| 1650 | } |
| 1651 | |
| 1652 | int devlink_dpipe_action_put(struct sk_buff *skb, |
| 1653 | struct devlink_dpipe_action *action) |
| 1654 | { |
| 1655 | struct devlink_dpipe_header *header = action->header; |
| 1656 | struct devlink_dpipe_field *field = &header->fields[action->field_id]; |
| 1657 | struct nlattr *action_attr; |
| 1658 | |
| 1659 | action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION); |
| 1660 | if (!action_attr) |
| 1661 | return -EMSGSIZE; |
| 1662 | |
| 1663 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) || |
| 1664 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) || |
| 1665 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1666 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1667 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1668 | goto nla_put_failure; |
| 1669 | |
| 1670 | nla_nest_end(skb, action_attr); |
| 1671 | return 0; |
| 1672 | |
| 1673 | nla_put_failure: |
| 1674 | nla_nest_cancel(skb, action_attr); |
| 1675 | return -EMSGSIZE; |
| 1676 | } |
| 1677 | EXPORT_SYMBOL_GPL(devlink_dpipe_action_put); |
| 1678 | |
| 1679 | static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table, |
| 1680 | struct sk_buff *skb) |
| 1681 | { |
| 1682 | struct nlattr *actions_attr; |
| 1683 | |
| 1684 | actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS); |
| 1685 | if (!actions_attr) |
| 1686 | return -EMSGSIZE; |
| 1687 | |
| 1688 | if (table->table_ops->actions_dump(table->priv, skb)) |
| 1689 | goto nla_put_failure; |
| 1690 | |
| 1691 | nla_nest_end(skb, actions_attr); |
| 1692 | return 0; |
| 1693 | |
| 1694 | nla_put_failure: |
| 1695 | nla_nest_cancel(skb, actions_attr); |
| 1696 | return -EMSGSIZE; |
| 1697 | } |
| 1698 | |
| 1699 | static int devlink_dpipe_table_put(struct sk_buff *skb, |
| 1700 | struct devlink_dpipe_table *table) |
| 1701 | { |
| 1702 | struct nlattr *table_attr; |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1703 | u64 table_size; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1704 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1705 | table_size = table->table_ops->size_get(table->priv); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1706 | table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE); |
| 1707 | if (!table_attr) |
| 1708 | return -EMSGSIZE; |
| 1709 | |
| 1710 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) || |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 1711 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1712 | DEVLINK_ATTR_PAD)) |
| 1713 | goto nla_put_failure; |
| 1714 | if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED, |
| 1715 | table->counters_enabled)) |
| 1716 | goto nla_put_failure; |
| 1717 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1718 | if (table->resource_valid) { |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 1719 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID, |
| 1720 | table->resource_id, DEVLINK_ATTR_PAD) || |
| 1721 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS, |
| 1722 | table->resource_units, DEVLINK_ATTR_PAD)) |
| 1723 | goto nla_put_failure; |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 1724 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1725 | if (devlink_dpipe_matches_put(table, skb)) |
| 1726 | goto nla_put_failure; |
| 1727 | |
| 1728 | if (devlink_dpipe_actions_put(table, skb)) |
| 1729 | goto nla_put_failure; |
| 1730 | |
| 1731 | nla_nest_end(skb, table_attr); |
| 1732 | return 0; |
| 1733 | |
| 1734 | nla_put_failure: |
| 1735 | nla_nest_cancel(skb, table_attr); |
| 1736 | return -EMSGSIZE; |
| 1737 | } |
| 1738 | |
| 1739 | static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb, |
| 1740 | struct genl_info *info) |
| 1741 | { |
| 1742 | int err; |
| 1743 | |
| 1744 | if (*pskb) { |
| 1745 | err = genlmsg_reply(*pskb, info); |
| 1746 | if (err) |
| 1747 | return err; |
| 1748 | } |
| 1749 | *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1750 | if (!*pskb) |
| 1751 | return -ENOMEM; |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
| 1755 | static int devlink_dpipe_tables_fill(struct genl_info *info, |
| 1756 | enum devlink_command cmd, int flags, |
| 1757 | struct list_head *dpipe_tables, |
| 1758 | const char *table_name) |
| 1759 | { |
| 1760 | struct devlink *devlink = info->user_ptr[0]; |
| 1761 | struct devlink_dpipe_table *table; |
| 1762 | struct nlattr *tables_attr; |
| 1763 | struct sk_buff *skb = NULL; |
| 1764 | struct nlmsghdr *nlh; |
| 1765 | bool incomplete; |
| 1766 | void *hdr; |
| 1767 | int i; |
| 1768 | int err; |
| 1769 | |
| 1770 | table = list_first_entry(dpipe_tables, |
| 1771 | struct devlink_dpipe_table, list); |
| 1772 | start_again: |
| 1773 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1774 | if (err) |
| 1775 | return err; |
| 1776 | |
| 1777 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1778 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 1779 | if (!hdr) { |
| 1780 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1781 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 1782 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1783 | |
| 1784 | if (devlink_nl_put_handle(skb, devlink)) |
| 1785 | goto nla_put_failure; |
| 1786 | tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES); |
| 1787 | if (!tables_attr) |
| 1788 | goto nla_put_failure; |
| 1789 | |
| 1790 | i = 0; |
| 1791 | incomplete = false; |
| 1792 | list_for_each_entry_from(table, dpipe_tables, list) { |
| 1793 | if (!table_name) { |
| 1794 | err = devlink_dpipe_table_put(skb, table); |
| 1795 | if (err) { |
| 1796 | if (!i) |
| 1797 | goto err_table_put; |
| 1798 | incomplete = true; |
| 1799 | break; |
| 1800 | } |
| 1801 | } else { |
| 1802 | if (!strcmp(table->name, table_name)) { |
| 1803 | err = devlink_dpipe_table_put(skb, table); |
| 1804 | if (err) |
| 1805 | break; |
| 1806 | } |
| 1807 | } |
| 1808 | i++; |
| 1809 | } |
| 1810 | |
| 1811 | nla_nest_end(skb, tables_attr); |
| 1812 | genlmsg_end(skb, hdr); |
| 1813 | if (incomplete) |
| 1814 | goto start_again; |
| 1815 | |
| 1816 | send_done: |
| 1817 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1818 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 1819 | if (!nlh) { |
| 1820 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1821 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 1822 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1823 | goto send_done; |
| 1824 | } |
| 1825 | |
| 1826 | return genlmsg_reply(skb, info); |
| 1827 | |
| 1828 | nla_put_failure: |
| 1829 | err = -EMSGSIZE; |
| 1830 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1831 | nlmsg_free(skb); |
| 1832 | return err; |
| 1833 | } |
| 1834 | |
| 1835 | static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb, |
| 1836 | struct genl_info *info) |
| 1837 | { |
| 1838 | struct devlink *devlink = info->user_ptr[0]; |
| 1839 | const char *table_name = NULL; |
| 1840 | |
| 1841 | if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 1842 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 1843 | |
| 1844 | return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0, |
| 1845 | &devlink->dpipe_table_list, |
| 1846 | table_name); |
| 1847 | } |
| 1848 | |
| 1849 | static int devlink_dpipe_value_put(struct sk_buff *skb, |
| 1850 | struct devlink_dpipe_value *value) |
| 1851 | { |
| 1852 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE, |
| 1853 | value->value_size, value->value)) |
| 1854 | return -EMSGSIZE; |
| 1855 | if (value->mask) |
| 1856 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK, |
| 1857 | value->value_size, value->mask)) |
| 1858 | return -EMSGSIZE; |
| 1859 | if (value->mapping_valid) |
| 1860 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING, |
| 1861 | value->mapping_value)) |
| 1862 | return -EMSGSIZE; |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | static int devlink_dpipe_action_value_put(struct sk_buff *skb, |
| 1867 | struct devlink_dpipe_value *value) |
| 1868 | { |
| 1869 | if (!value->action) |
| 1870 | return -EINVAL; |
| 1871 | if (devlink_dpipe_action_put(skb, value->action)) |
| 1872 | return -EMSGSIZE; |
| 1873 | if (devlink_dpipe_value_put(skb, value)) |
| 1874 | return -EMSGSIZE; |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
| 1878 | static int devlink_dpipe_action_values_put(struct sk_buff *skb, |
| 1879 | struct devlink_dpipe_value *values, |
| 1880 | unsigned int values_count) |
| 1881 | { |
| 1882 | struct nlattr *action_attr; |
| 1883 | int i; |
| 1884 | int err; |
| 1885 | |
| 1886 | for (i = 0; i < values_count; i++) { |
| 1887 | action_attr = nla_nest_start(skb, |
| 1888 | DEVLINK_ATTR_DPIPE_ACTION_VALUE); |
| 1889 | if (!action_attr) |
| 1890 | return -EMSGSIZE; |
| 1891 | err = devlink_dpipe_action_value_put(skb, &values[i]); |
| 1892 | if (err) |
| 1893 | goto err_action_value_put; |
| 1894 | nla_nest_end(skb, action_attr); |
| 1895 | } |
| 1896 | return 0; |
| 1897 | |
| 1898 | err_action_value_put: |
| 1899 | nla_nest_cancel(skb, action_attr); |
| 1900 | return err; |
| 1901 | } |
| 1902 | |
| 1903 | static int devlink_dpipe_match_value_put(struct sk_buff *skb, |
| 1904 | struct devlink_dpipe_value *value) |
| 1905 | { |
| 1906 | if (!value->match) |
| 1907 | return -EINVAL; |
| 1908 | if (devlink_dpipe_match_put(skb, value->match)) |
| 1909 | return -EMSGSIZE; |
| 1910 | if (devlink_dpipe_value_put(skb, value)) |
| 1911 | return -EMSGSIZE; |
| 1912 | return 0; |
| 1913 | } |
| 1914 | |
| 1915 | static int devlink_dpipe_match_values_put(struct sk_buff *skb, |
| 1916 | struct devlink_dpipe_value *values, |
| 1917 | unsigned int values_count) |
| 1918 | { |
| 1919 | struct nlattr *match_attr; |
| 1920 | int i; |
| 1921 | int err; |
| 1922 | |
| 1923 | for (i = 0; i < values_count; i++) { |
| 1924 | match_attr = nla_nest_start(skb, |
| 1925 | DEVLINK_ATTR_DPIPE_MATCH_VALUE); |
| 1926 | if (!match_attr) |
| 1927 | return -EMSGSIZE; |
| 1928 | err = devlink_dpipe_match_value_put(skb, &values[i]); |
| 1929 | if (err) |
| 1930 | goto err_match_value_put; |
| 1931 | nla_nest_end(skb, match_attr); |
| 1932 | } |
| 1933 | return 0; |
| 1934 | |
| 1935 | err_match_value_put: |
| 1936 | nla_nest_cancel(skb, match_attr); |
| 1937 | return err; |
| 1938 | } |
| 1939 | |
| 1940 | static int devlink_dpipe_entry_put(struct sk_buff *skb, |
| 1941 | struct devlink_dpipe_entry *entry) |
| 1942 | { |
| 1943 | struct nlattr *entry_attr, *matches_attr, *actions_attr; |
| 1944 | int err; |
| 1945 | |
| 1946 | entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY); |
| 1947 | if (!entry_attr) |
| 1948 | return -EMSGSIZE; |
| 1949 | |
| 1950 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index, |
| 1951 | DEVLINK_ATTR_PAD)) |
| 1952 | goto nla_put_failure; |
| 1953 | if (entry->counter_valid) |
| 1954 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER, |
| 1955 | entry->counter, DEVLINK_ATTR_PAD)) |
| 1956 | goto nla_put_failure; |
| 1957 | |
| 1958 | matches_attr = nla_nest_start(skb, |
| 1959 | DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES); |
| 1960 | if (!matches_attr) |
| 1961 | goto nla_put_failure; |
| 1962 | |
| 1963 | err = devlink_dpipe_match_values_put(skb, entry->match_values, |
| 1964 | entry->match_values_count); |
| 1965 | if (err) { |
| 1966 | nla_nest_cancel(skb, matches_attr); |
| 1967 | goto err_match_values_put; |
| 1968 | } |
| 1969 | nla_nest_end(skb, matches_attr); |
| 1970 | |
| 1971 | actions_attr = nla_nest_start(skb, |
| 1972 | DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES); |
| 1973 | if (!actions_attr) |
| 1974 | goto nla_put_failure; |
| 1975 | |
| 1976 | err = devlink_dpipe_action_values_put(skb, entry->action_values, |
| 1977 | entry->action_values_count); |
| 1978 | if (err) { |
| 1979 | nla_nest_cancel(skb, actions_attr); |
| 1980 | goto err_action_values_put; |
| 1981 | } |
| 1982 | nla_nest_end(skb, actions_attr); |
| 1983 | |
| 1984 | nla_nest_end(skb, entry_attr); |
| 1985 | return 0; |
| 1986 | |
| 1987 | nla_put_failure: |
| 1988 | err = -EMSGSIZE; |
| 1989 | err_match_values_put: |
| 1990 | err_action_values_put: |
| 1991 | nla_nest_cancel(skb, entry_attr); |
| 1992 | return err; |
| 1993 | } |
| 1994 | |
| 1995 | static struct devlink_dpipe_table * |
| 1996 | devlink_dpipe_table_find(struct list_head *dpipe_tables, |
| 1997 | const char *table_name) |
| 1998 | { |
| 1999 | struct devlink_dpipe_table *table; |
| 2000 | |
| 2001 | list_for_each_entry_rcu(table, dpipe_tables, list) { |
| 2002 | if (!strcmp(table->name, table_name)) |
| 2003 | return table; |
| 2004 | } |
| 2005 | return NULL; |
| 2006 | } |
| 2007 | |
| 2008 | int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2009 | { |
| 2010 | struct devlink *devlink; |
| 2011 | int err; |
| 2012 | |
| 2013 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb, |
| 2014 | dump_ctx->info); |
| 2015 | if (err) |
| 2016 | return err; |
| 2017 | |
| 2018 | dump_ctx->hdr = genlmsg_put(dump_ctx->skb, |
| 2019 | dump_ctx->info->snd_portid, |
| 2020 | dump_ctx->info->snd_seq, |
| 2021 | &devlink_nl_family, NLM_F_MULTI, |
| 2022 | dump_ctx->cmd); |
| 2023 | if (!dump_ctx->hdr) |
| 2024 | goto nla_put_failure; |
| 2025 | |
| 2026 | devlink = dump_ctx->info->user_ptr[0]; |
| 2027 | if (devlink_nl_put_handle(dump_ctx->skb, devlink)) |
| 2028 | goto nla_put_failure; |
| 2029 | dump_ctx->nest = nla_nest_start(dump_ctx->skb, |
| 2030 | DEVLINK_ATTR_DPIPE_ENTRIES); |
| 2031 | if (!dump_ctx->nest) |
| 2032 | goto nla_put_failure; |
| 2033 | return 0; |
| 2034 | |
| 2035 | nla_put_failure: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2036 | nlmsg_free(dump_ctx->skb); |
| 2037 | return -EMSGSIZE; |
| 2038 | } |
| 2039 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare); |
| 2040 | |
| 2041 | int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, |
| 2042 | struct devlink_dpipe_entry *entry) |
| 2043 | { |
| 2044 | return devlink_dpipe_entry_put(dump_ctx->skb, entry); |
| 2045 | } |
| 2046 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append); |
| 2047 | |
| 2048 | int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 2049 | { |
| 2050 | nla_nest_end(dump_ctx->skb, dump_ctx->nest); |
| 2051 | genlmsg_end(dump_ctx->skb, dump_ctx->hdr); |
| 2052 | return 0; |
| 2053 | } |
| 2054 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close); |
| 2055 | |
Arkadi Sharshevsky | 3580732 | 2017-08-24 08:40:03 +0200 | [diff] [blame] | 2056 | void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry) |
| 2057 | |
| 2058 | { |
| 2059 | unsigned int value_count, value_index; |
| 2060 | struct devlink_dpipe_value *value; |
| 2061 | |
| 2062 | value = entry->action_values; |
| 2063 | value_count = entry->action_values_count; |
| 2064 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2065 | kfree(value[value_index].value); |
| 2066 | kfree(value[value_index].mask); |
| 2067 | } |
| 2068 | |
| 2069 | value = entry->match_values; |
| 2070 | value_count = entry->match_values_count; |
| 2071 | for (value_index = 0; value_index < value_count; value_index++) { |
| 2072 | kfree(value[value_index].value); |
| 2073 | kfree(value[value_index].mask); |
| 2074 | } |
| 2075 | } |
| 2076 | EXPORT_SYMBOL(devlink_dpipe_entry_clear); |
| 2077 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2078 | static int devlink_dpipe_entries_fill(struct genl_info *info, |
| 2079 | enum devlink_command cmd, int flags, |
| 2080 | struct devlink_dpipe_table *table) |
| 2081 | { |
| 2082 | struct devlink_dpipe_dump_ctx dump_ctx; |
| 2083 | struct nlmsghdr *nlh; |
| 2084 | int err; |
| 2085 | |
| 2086 | dump_ctx.skb = NULL; |
| 2087 | dump_ctx.cmd = cmd; |
| 2088 | dump_ctx.info = info; |
| 2089 | |
| 2090 | err = table->table_ops->entries_dump(table->priv, |
| 2091 | table->counters_enabled, |
| 2092 | &dump_ctx); |
| 2093 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2094 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2095 | |
| 2096 | send_done: |
| 2097 | nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq, |
| 2098 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2099 | if (!nlh) { |
| 2100 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info); |
| 2101 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2102 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2103 | goto send_done; |
| 2104 | } |
| 2105 | return genlmsg_reply(dump_ctx.skb, info); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb, |
| 2109 | struct genl_info *info) |
| 2110 | { |
| 2111 | struct devlink *devlink = info->user_ptr[0]; |
| 2112 | struct devlink_dpipe_table *table; |
| 2113 | const char *table_name; |
| 2114 | |
| 2115 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 2116 | return -EINVAL; |
| 2117 | |
| 2118 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2119 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2120 | table_name); |
| 2121 | if (!table) |
| 2122 | return -EINVAL; |
| 2123 | |
| 2124 | if (!table->table_ops->entries_dump) |
| 2125 | return -EINVAL; |
| 2126 | |
| 2127 | return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET, |
| 2128 | 0, table); |
| 2129 | } |
| 2130 | |
| 2131 | static int devlink_dpipe_fields_put(struct sk_buff *skb, |
| 2132 | const struct devlink_dpipe_header *header) |
| 2133 | { |
| 2134 | struct devlink_dpipe_field *field; |
| 2135 | struct nlattr *field_attr; |
| 2136 | int i; |
| 2137 | |
| 2138 | for (i = 0; i < header->fields_count; i++) { |
| 2139 | field = &header->fields[i]; |
| 2140 | field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD); |
| 2141 | if (!field_attr) |
| 2142 | return -EMSGSIZE; |
| 2143 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) || |
| 2144 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 2145 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) || |
| 2146 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type)) |
| 2147 | goto nla_put_failure; |
| 2148 | nla_nest_end(skb, field_attr); |
| 2149 | } |
| 2150 | return 0; |
| 2151 | |
| 2152 | nla_put_failure: |
| 2153 | nla_nest_cancel(skb, field_attr); |
| 2154 | return -EMSGSIZE; |
| 2155 | } |
| 2156 | |
| 2157 | static int devlink_dpipe_header_put(struct sk_buff *skb, |
| 2158 | struct devlink_dpipe_header *header) |
| 2159 | { |
| 2160 | struct nlattr *fields_attr, *header_attr; |
| 2161 | int err; |
| 2162 | |
| 2163 | header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER); |
Wei Yongjun | cb6bf9c | 2017-04-11 16:02:02 +0000 | [diff] [blame] | 2164 | if (!header_attr) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2165 | return -EMSGSIZE; |
| 2166 | |
| 2167 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) || |
| 2168 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 2169 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 2170 | goto nla_put_failure; |
| 2171 | |
| 2172 | fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS); |
| 2173 | if (!fields_attr) |
| 2174 | goto nla_put_failure; |
| 2175 | |
| 2176 | err = devlink_dpipe_fields_put(skb, header); |
| 2177 | if (err) { |
| 2178 | nla_nest_cancel(skb, fields_attr); |
| 2179 | goto nla_put_failure; |
| 2180 | } |
| 2181 | nla_nest_end(skb, fields_attr); |
| 2182 | nla_nest_end(skb, header_attr); |
| 2183 | return 0; |
| 2184 | |
| 2185 | nla_put_failure: |
| 2186 | err = -EMSGSIZE; |
| 2187 | nla_nest_cancel(skb, header_attr); |
| 2188 | return err; |
| 2189 | } |
| 2190 | |
| 2191 | static int devlink_dpipe_headers_fill(struct genl_info *info, |
| 2192 | enum devlink_command cmd, int flags, |
| 2193 | struct devlink_dpipe_headers * |
| 2194 | dpipe_headers) |
| 2195 | { |
| 2196 | struct devlink *devlink = info->user_ptr[0]; |
| 2197 | struct nlattr *headers_attr; |
| 2198 | struct sk_buff *skb = NULL; |
| 2199 | struct nlmsghdr *nlh; |
| 2200 | void *hdr; |
| 2201 | int i, j; |
| 2202 | int err; |
| 2203 | |
| 2204 | i = 0; |
| 2205 | start_again: |
| 2206 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2207 | if (err) |
| 2208 | return err; |
| 2209 | |
| 2210 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2211 | &devlink_nl_family, NLM_F_MULTI, cmd); |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2212 | if (!hdr) { |
| 2213 | nlmsg_free(skb); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2214 | return -EMSGSIZE; |
Haishuang Yan | 6044bd4 | 2017-06-05 08:57:21 +0800 | [diff] [blame] | 2215 | } |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2216 | |
| 2217 | if (devlink_nl_put_handle(skb, devlink)) |
| 2218 | goto nla_put_failure; |
| 2219 | headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS); |
| 2220 | if (!headers_attr) |
| 2221 | goto nla_put_failure; |
| 2222 | |
| 2223 | j = 0; |
| 2224 | for (; i < dpipe_headers->headers_count; i++) { |
| 2225 | err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]); |
| 2226 | if (err) { |
| 2227 | if (!j) |
| 2228 | goto err_table_put; |
| 2229 | break; |
| 2230 | } |
| 2231 | j++; |
| 2232 | } |
| 2233 | nla_nest_end(skb, headers_attr); |
| 2234 | genlmsg_end(skb, hdr); |
| 2235 | if (i != dpipe_headers->headers_count) |
| 2236 | goto start_again; |
| 2237 | |
| 2238 | send_done: |
| 2239 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2240 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2241 | if (!nlh) { |
| 2242 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2243 | if (err) |
Arkadi Sharshevsky | 7fe4d6d | 2018-03-18 17:37:22 +0200 | [diff] [blame] | 2244 | return err; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2245 | goto send_done; |
| 2246 | } |
| 2247 | return genlmsg_reply(skb, info); |
| 2248 | |
| 2249 | nla_put_failure: |
| 2250 | err = -EMSGSIZE; |
| 2251 | err_table_put: |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2252 | nlmsg_free(skb); |
| 2253 | return err; |
| 2254 | } |
| 2255 | |
| 2256 | static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb, |
| 2257 | struct genl_info *info) |
| 2258 | { |
| 2259 | struct devlink *devlink = info->user_ptr[0]; |
| 2260 | |
| 2261 | if (!devlink->dpipe_headers) |
| 2262 | return -EOPNOTSUPP; |
| 2263 | return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET, |
| 2264 | 0, devlink->dpipe_headers); |
| 2265 | } |
| 2266 | |
| 2267 | static int devlink_dpipe_table_counters_set(struct devlink *devlink, |
| 2268 | const char *table_name, |
| 2269 | bool enable) |
| 2270 | { |
| 2271 | struct devlink_dpipe_table *table; |
| 2272 | |
| 2273 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2274 | table_name); |
| 2275 | if (!table) |
| 2276 | return -EINVAL; |
| 2277 | |
| 2278 | if (table->counter_control_extern) |
| 2279 | return -EOPNOTSUPP; |
| 2280 | |
| 2281 | if (!(table->counters_enabled ^ enable)) |
| 2282 | return 0; |
| 2283 | |
| 2284 | table->counters_enabled = enable; |
| 2285 | if (table->table_ops->counters_set_update) |
| 2286 | table->table_ops->counters_set_update(table->priv, enable); |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
| 2290 | static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb, |
| 2291 | struct genl_info *info) |
| 2292 | { |
| 2293 | struct devlink *devlink = info->user_ptr[0]; |
| 2294 | const char *table_name; |
| 2295 | bool counters_enable; |
| 2296 | |
| 2297 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] || |
| 2298 | !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) |
| 2299 | return -EINVAL; |
| 2300 | |
| 2301 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2302 | counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]); |
| 2303 | |
| 2304 | return devlink_dpipe_table_counters_set(devlink, table_name, |
| 2305 | counters_enable); |
| 2306 | } |
| 2307 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2308 | static struct devlink_resource * |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2309 | devlink_resource_find(struct devlink *devlink, |
| 2310 | struct devlink_resource *resource, u64 resource_id) |
| 2311 | { |
| 2312 | struct list_head *resource_list; |
| 2313 | |
| 2314 | if (resource) |
| 2315 | resource_list = &resource->resource_list; |
| 2316 | else |
| 2317 | resource_list = &devlink->resource_list; |
| 2318 | |
| 2319 | list_for_each_entry(resource, resource_list, list) { |
| 2320 | struct devlink_resource *child_resource; |
| 2321 | |
| 2322 | if (resource->id == resource_id) |
| 2323 | return resource; |
| 2324 | |
| 2325 | child_resource = devlink_resource_find(devlink, resource, |
| 2326 | resource_id); |
| 2327 | if (child_resource) |
| 2328 | return child_resource; |
| 2329 | } |
| 2330 | return NULL; |
| 2331 | } |
| 2332 | |
Wei Yongjun | 43dd751 | 2018-01-17 03:27:42 +0000 | [diff] [blame] | 2333 | static void |
| 2334 | devlink_resource_validate_children(struct devlink_resource *resource) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2335 | { |
| 2336 | struct devlink_resource *child_resource; |
| 2337 | bool size_valid = true; |
| 2338 | u64 parts_size = 0; |
| 2339 | |
| 2340 | if (list_empty(&resource->resource_list)) |
| 2341 | goto out; |
| 2342 | |
| 2343 | list_for_each_entry(child_resource, &resource->resource_list, list) |
| 2344 | parts_size += child_resource->size_new; |
| 2345 | |
Arkadi Sharshevsky | b9d1717 | 2018-02-26 10:59:53 +0100 | [diff] [blame] | 2346 | if (parts_size > resource->size_new) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2347 | size_valid = false; |
| 2348 | out: |
| 2349 | resource->size_valid = size_valid; |
| 2350 | } |
| 2351 | |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2352 | static int |
| 2353 | devlink_resource_validate_size(struct devlink_resource *resource, u64 size, |
| 2354 | struct netlink_ext_ack *extack) |
| 2355 | { |
| 2356 | u64 reminder; |
| 2357 | int err = 0; |
| 2358 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2359 | if (size > resource->size_params.size_max) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2360 | NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum"); |
| 2361 | err = -EINVAL; |
| 2362 | } |
| 2363 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2364 | if (size < resource->size_params.size_min) { |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2365 | NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum"); |
| 2366 | err = -EINVAL; |
| 2367 | } |
| 2368 | |
David S. Miller | 0f3e9c9 | 2018-03-06 00:53:44 -0500 | [diff] [blame] | 2369 | div64_u64_rem(size, resource->size_params.size_granularity, &reminder); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2370 | if (reminder) { |
| 2371 | NL_SET_ERR_MSG_MOD(extack, "Wrong granularity"); |
| 2372 | err = -EINVAL; |
| 2373 | } |
| 2374 | |
| 2375 | return err; |
| 2376 | } |
| 2377 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2378 | static int devlink_nl_cmd_resource_set(struct sk_buff *skb, |
| 2379 | struct genl_info *info) |
| 2380 | { |
| 2381 | struct devlink *devlink = info->user_ptr[0]; |
| 2382 | struct devlink_resource *resource; |
| 2383 | u64 resource_id; |
| 2384 | u64 size; |
| 2385 | int err; |
| 2386 | |
| 2387 | if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] || |
| 2388 | !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]) |
| 2389 | return -EINVAL; |
| 2390 | resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]); |
| 2391 | |
| 2392 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 2393 | if (!resource) |
| 2394 | return -EINVAL; |
| 2395 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2396 | size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]); |
Arkadi Sharshevsky | cc944ea | 2018-02-20 08:44:20 +0100 | [diff] [blame] | 2397 | err = devlink_resource_validate_size(resource, size, info->extack); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2398 | if (err) |
| 2399 | return err; |
| 2400 | |
| 2401 | resource->size_new = size; |
| 2402 | devlink_resource_validate_children(resource); |
| 2403 | if (resource->parent) |
| 2404 | devlink_resource_validate_children(resource->parent); |
| 2405 | return 0; |
| 2406 | } |
| 2407 | |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2408 | static int |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2409 | devlink_resource_size_params_put(struct devlink_resource *resource, |
| 2410 | struct sk_buff *skb) |
| 2411 | { |
| 2412 | struct devlink_resource_size_params *size_params; |
| 2413 | |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 2414 | size_params = &resource->size_params; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2415 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN, |
| 2416 | size_params->size_granularity, DEVLINK_ATTR_PAD) || |
| 2417 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX, |
| 2418 | size_params->size_max, DEVLINK_ATTR_PAD) || |
| 2419 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN, |
| 2420 | size_params->size_min, DEVLINK_ATTR_PAD) || |
| 2421 | nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit)) |
| 2422 | return -EMSGSIZE; |
| 2423 | return 0; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2424 | } |
| 2425 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2426 | static int devlink_resource_occ_put(struct devlink_resource *resource, |
| 2427 | struct sk_buff *skb) |
| 2428 | { |
| 2429 | if (!resource->occ_get) |
| 2430 | return 0; |
| 2431 | return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC, |
| 2432 | resource->occ_get(resource->occ_get_priv), |
| 2433 | DEVLINK_ATTR_PAD); |
| 2434 | } |
| 2435 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2436 | static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb, |
| 2437 | struct devlink_resource *resource) |
| 2438 | { |
| 2439 | struct devlink_resource *child_resource; |
| 2440 | struct nlattr *child_resource_attr; |
| 2441 | struct nlattr *resource_attr; |
| 2442 | |
| 2443 | resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE); |
| 2444 | if (!resource_attr) |
| 2445 | return -EMSGSIZE; |
| 2446 | |
| 2447 | if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) || |
| 2448 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size, |
| 2449 | DEVLINK_ATTR_PAD) || |
| 2450 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id, |
| 2451 | DEVLINK_ATTR_PAD)) |
| 2452 | goto nla_put_failure; |
| 2453 | if (resource->size != resource->size_new) |
| 2454 | nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW, |
| 2455 | resource->size_new, DEVLINK_ATTR_PAD); |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 2456 | if (devlink_resource_occ_put(resource, skb)) |
| 2457 | goto nla_put_failure; |
Arkadi Sharshevsky | 3d18e4f | 2018-02-26 18:25:42 +0200 | [diff] [blame] | 2458 | if (devlink_resource_size_params_put(resource, skb)) |
| 2459 | goto nla_put_failure; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2460 | if (list_empty(&resource->resource_list)) |
| 2461 | goto out; |
| 2462 | |
| 2463 | if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID, |
| 2464 | resource->size_valid)) |
| 2465 | goto nla_put_failure; |
| 2466 | |
| 2467 | child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST); |
| 2468 | if (!child_resource_attr) |
| 2469 | goto nla_put_failure; |
| 2470 | |
| 2471 | list_for_each_entry(child_resource, &resource->resource_list, list) { |
| 2472 | if (devlink_resource_put(devlink, skb, child_resource)) |
| 2473 | goto resource_put_failure; |
| 2474 | } |
| 2475 | |
| 2476 | nla_nest_end(skb, child_resource_attr); |
| 2477 | out: |
| 2478 | nla_nest_end(skb, resource_attr); |
| 2479 | return 0; |
| 2480 | |
| 2481 | resource_put_failure: |
| 2482 | nla_nest_cancel(skb, child_resource_attr); |
| 2483 | nla_put_failure: |
| 2484 | nla_nest_cancel(skb, resource_attr); |
| 2485 | return -EMSGSIZE; |
| 2486 | } |
| 2487 | |
| 2488 | static int devlink_resource_fill(struct genl_info *info, |
| 2489 | enum devlink_command cmd, int flags) |
| 2490 | { |
| 2491 | struct devlink *devlink = info->user_ptr[0]; |
| 2492 | struct devlink_resource *resource; |
| 2493 | struct nlattr *resources_attr; |
| 2494 | struct sk_buff *skb = NULL; |
| 2495 | struct nlmsghdr *nlh; |
| 2496 | bool incomplete; |
| 2497 | void *hdr; |
| 2498 | int i; |
| 2499 | int err; |
| 2500 | |
| 2501 | resource = list_first_entry(&devlink->resource_list, |
| 2502 | struct devlink_resource, list); |
| 2503 | start_again: |
| 2504 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2505 | if (err) |
| 2506 | return err; |
| 2507 | |
| 2508 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2509 | &devlink_nl_family, NLM_F_MULTI, cmd); |
| 2510 | if (!hdr) { |
| 2511 | nlmsg_free(skb); |
| 2512 | return -EMSGSIZE; |
| 2513 | } |
| 2514 | |
| 2515 | if (devlink_nl_put_handle(skb, devlink)) |
| 2516 | goto nla_put_failure; |
| 2517 | |
| 2518 | resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST); |
| 2519 | if (!resources_attr) |
| 2520 | goto nla_put_failure; |
| 2521 | |
| 2522 | incomplete = false; |
| 2523 | i = 0; |
| 2524 | list_for_each_entry_from(resource, &devlink->resource_list, list) { |
| 2525 | err = devlink_resource_put(devlink, skb, resource); |
| 2526 | if (err) { |
| 2527 | if (!i) |
| 2528 | goto err_resource_put; |
| 2529 | incomplete = true; |
| 2530 | break; |
| 2531 | } |
| 2532 | i++; |
| 2533 | } |
| 2534 | nla_nest_end(skb, resources_attr); |
| 2535 | genlmsg_end(skb, hdr); |
| 2536 | if (incomplete) |
| 2537 | goto start_again; |
| 2538 | send_done: |
| 2539 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2540 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2541 | if (!nlh) { |
| 2542 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2543 | if (err) |
| 2544 | goto err_skb_send_alloc; |
| 2545 | goto send_done; |
| 2546 | } |
| 2547 | return genlmsg_reply(skb, info); |
| 2548 | |
| 2549 | nla_put_failure: |
| 2550 | err = -EMSGSIZE; |
| 2551 | err_resource_put: |
| 2552 | err_skb_send_alloc: |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 2553 | nlmsg_free(skb); |
| 2554 | return err; |
| 2555 | } |
| 2556 | |
| 2557 | static int devlink_nl_cmd_resource_dump(struct sk_buff *skb, |
| 2558 | struct genl_info *info) |
| 2559 | { |
| 2560 | struct devlink *devlink = info->user_ptr[0]; |
| 2561 | |
| 2562 | if (list_empty(&devlink->resource_list)) |
| 2563 | return -EOPNOTSUPP; |
| 2564 | |
| 2565 | return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0); |
| 2566 | } |
| 2567 | |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2568 | static int |
| 2569 | devlink_resources_validate(struct devlink *devlink, |
| 2570 | struct devlink_resource *resource, |
| 2571 | struct genl_info *info) |
| 2572 | { |
| 2573 | struct list_head *resource_list; |
| 2574 | int err = 0; |
| 2575 | |
| 2576 | if (resource) |
| 2577 | resource_list = &resource->resource_list; |
| 2578 | else |
| 2579 | resource_list = &devlink->resource_list; |
| 2580 | |
| 2581 | list_for_each_entry(resource, resource_list, list) { |
| 2582 | if (!resource->size_valid) |
| 2583 | return -EINVAL; |
| 2584 | err = devlink_resources_validate(devlink, resource, info); |
| 2585 | if (err) |
| 2586 | return err; |
| 2587 | } |
| 2588 | return err; |
| 2589 | } |
| 2590 | |
| 2591 | static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info) |
| 2592 | { |
| 2593 | struct devlink *devlink = info->user_ptr[0]; |
| 2594 | int err; |
| 2595 | |
| 2596 | if (!devlink->ops->reload) |
| 2597 | return -EOPNOTSUPP; |
| 2598 | |
| 2599 | err = devlink_resources_validate(devlink, NULL, info); |
| 2600 | if (err) { |
| 2601 | NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed"); |
| 2602 | return err; |
| 2603 | } |
David Ahern | ac0fc8a | 2018-06-05 08:14:09 -0700 | [diff] [blame] | 2604 | return devlink->ops->reload(devlink, info->extack); |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 2605 | } |
| 2606 | |
Moshe Shemesh | 036467c | 2018-07-04 14:30:33 +0300 | [diff] [blame^] | 2607 | static const struct devlink_param devlink_param_generic[] = { |
| 2608 | { |
| 2609 | .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET, |
| 2610 | .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME, |
| 2611 | .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE, |
| 2612 | }, |
| 2613 | { |
| 2614 | .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS, |
| 2615 | .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME, |
| 2616 | .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE, |
| 2617 | }, |
| 2618 | |
| 2619 | }; |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 2620 | |
| 2621 | static int devlink_param_generic_verify(const struct devlink_param *param) |
| 2622 | { |
| 2623 | /* verify it match generic parameter by id and name */ |
| 2624 | if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX) |
| 2625 | return -EINVAL; |
| 2626 | if (strcmp(param->name, devlink_param_generic[param->id].name)) |
| 2627 | return -ENOENT; |
| 2628 | |
| 2629 | WARN_ON(param->type != devlink_param_generic[param->id].type); |
| 2630 | |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | static int devlink_param_driver_verify(const struct devlink_param *param) |
| 2635 | { |
| 2636 | int i; |
| 2637 | |
| 2638 | if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX) |
| 2639 | return -EINVAL; |
| 2640 | /* verify no such name in generic params */ |
| 2641 | for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++) |
| 2642 | if (!strcmp(param->name, devlink_param_generic[i].name)) |
| 2643 | return -EEXIST; |
| 2644 | |
| 2645 | return 0; |
| 2646 | } |
| 2647 | |
| 2648 | static struct devlink_param_item * |
| 2649 | devlink_param_find_by_name(struct list_head *param_list, |
| 2650 | const char *param_name) |
| 2651 | { |
| 2652 | struct devlink_param_item *param_item; |
| 2653 | |
| 2654 | list_for_each_entry(param_item, param_list, list) |
| 2655 | if (!strcmp(param_item->param->name, param_name)) |
| 2656 | return param_item; |
| 2657 | return NULL; |
| 2658 | } |
| 2659 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 2660 | static struct devlink_param_item * |
| 2661 | devlink_param_find_by_id(struct list_head *param_list, u32 param_id) |
| 2662 | { |
| 2663 | struct devlink_param_item *param_item; |
| 2664 | |
| 2665 | list_for_each_entry(param_item, param_list, list) |
| 2666 | if (param_item->param->id == param_id) |
| 2667 | return param_item; |
| 2668 | return NULL; |
| 2669 | } |
| 2670 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2671 | static bool |
| 2672 | devlink_param_cmode_is_supported(const struct devlink_param *param, |
| 2673 | enum devlink_param_cmode cmode) |
| 2674 | { |
| 2675 | return test_bit(cmode, ¶m->supported_cmodes); |
| 2676 | } |
| 2677 | |
| 2678 | static int devlink_param_get(struct devlink *devlink, |
| 2679 | const struct devlink_param *param, |
| 2680 | struct devlink_param_gset_ctx *ctx) |
| 2681 | { |
| 2682 | if (!param->get) |
| 2683 | return -EOPNOTSUPP; |
| 2684 | return param->get(devlink, param->id, ctx); |
| 2685 | } |
| 2686 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 2687 | static int devlink_param_set(struct devlink *devlink, |
| 2688 | const struct devlink_param *param, |
| 2689 | struct devlink_param_gset_ctx *ctx) |
| 2690 | { |
| 2691 | if (!param->set) |
| 2692 | return -EOPNOTSUPP; |
| 2693 | return param->set(devlink, param->id, ctx); |
| 2694 | } |
| 2695 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2696 | static int |
| 2697 | devlink_param_type_to_nla_type(enum devlink_param_type param_type) |
| 2698 | { |
| 2699 | switch (param_type) { |
| 2700 | case DEVLINK_PARAM_TYPE_U8: |
| 2701 | return NLA_U8; |
| 2702 | case DEVLINK_PARAM_TYPE_U16: |
| 2703 | return NLA_U16; |
| 2704 | case DEVLINK_PARAM_TYPE_U32: |
| 2705 | return NLA_U32; |
| 2706 | case DEVLINK_PARAM_TYPE_STRING: |
| 2707 | return NLA_STRING; |
| 2708 | case DEVLINK_PARAM_TYPE_BOOL: |
| 2709 | return NLA_FLAG; |
| 2710 | default: |
| 2711 | return -EINVAL; |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | static int |
| 2716 | devlink_nl_param_value_fill_one(struct sk_buff *msg, |
| 2717 | enum devlink_param_type type, |
| 2718 | enum devlink_param_cmode cmode, |
| 2719 | union devlink_param_value val) |
| 2720 | { |
| 2721 | struct nlattr *param_value_attr; |
| 2722 | |
| 2723 | param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE); |
| 2724 | if (!param_value_attr) |
| 2725 | goto nla_put_failure; |
| 2726 | |
| 2727 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode)) |
| 2728 | goto value_nest_cancel; |
| 2729 | |
| 2730 | switch (type) { |
| 2731 | case DEVLINK_PARAM_TYPE_U8: |
| 2732 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8)) |
| 2733 | goto value_nest_cancel; |
| 2734 | break; |
| 2735 | case DEVLINK_PARAM_TYPE_U16: |
| 2736 | if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16)) |
| 2737 | goto value_nest_cancel; |
| 2738 | break; |
| 2739 | case DEVLINK_PARAM_TYPE_U32: |
| 2740 | if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32)) |
| 2741 | goto value_nest_cancel; |
| 2742 | break; |
| 2743 | case DEVLINK_PARAM_TYPE_STRING: |
| 2744 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, |
| 2745 | val.vstr)) |
| 2746 | goto value_nest_cancel; |
| 2747 | break; |
| 2748 | case DEVLINK_PARAM_TYPE_BOOL: |
| 2749 | if (val.vbool && |
| 2750 | nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA)) |
| 2751 | goto value_nest_cancel; |
| 2752 | break; |
| 2753 | } |
| 2754 | |
| 2755 | nla_nest_end(msg, param_value_attr); |
| 2756 | return 0; |
| 2757 | |
| 2758 | value_nest_cancel: |
| 2759 | nla_nest_cancel(msg, param_value_attr); |
| 2760 | nla_put_failure: |
| 2761 | return -EMSGSIZE; |
| 2762 | } |
| 2763 | |
| 2764 | static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink, |
| 2765 | struct devlink_param_item *param_item, |
| 2766 | enum devlink_command cmd, |
| 2767 | u32 portid, u32 seq, int flags) |
| 2768 | { |
| 2769 | union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1]; |
| 2770 | const struct devlink_param *param = param_item->param; |
| 2771 | struct devlink_param_gset_ctx ctx; |
| 2772 | struct nlattr *param_values_list; |
| 2773 | struct nlattr *param_attr; |
| 2774 | int nla_type; |
| 2775 | void *hdr; |
| 2776 | int err; |
| 2777 | int i; |
| 2778 | |
| 2779 | /* Get value from driver part to driverinit configuration mode */ |
| 2780 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
| 2781 | if (!devlink_param_cmode_is_supported(param, i)) |
| 2782 | continue; |
| 2783 | if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
| 2784 | if (!param_item->driverinit_value_valid) |
| 2785 | return -EOPNOTSUPP; |
| 2786 | param_value[i] = param_item->driverinit_value; |
| 2787 | } else { |
| 2788 | ctx.cmode = i; |
| 2789 | err = devlink_param_get(devlink, param, &ctx); |
| 2790 | if (err) |
| 2791 | return err; |
| 2792 | param_value[i] = ctx.val; |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 2797 | if (!hdr) |
| 2798 | return -EMSGSIZE; |
| 2799 | |
| 2800 | if (devlink_nl_put_handle(msg, devlink)) |
| 2801 | goto genlmsg_cancel; |
| 2802 | param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM); |
| 2803 | if (!param_attr) |
| 2804 | goto genlmsg_cancel; |
| 2805 | if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name)) |
| 2806 | goto param_nest_cancel; |
| 2807 | if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC)) |
| 2808 | goto param_nest_cancel; |
| 2809 | |
| 2810 | nla_type = devlink_param_type_to_nla_type(param->type); |
| 2811 | if (nla_type < 0) |
| 2812 | goto param_nest_cancel; |
| 2813 | if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type)) |
| 2814 | goto param_nest_cancel; |
| 2815 | |
| 2816 | param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST); |
| 2817 | if (!param_values_list) |
| 2818 | goto param_nest_cancel; |
| 2819 | |
| 2820 | for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) { |
| 2821 | if (!devlink_param_cmode_is_supported(param, i)) |
| 2822 | continue; |
| 2823 | err = devlink_nl_param_value_fill_one(msg, param->type, |
| 2824 | i, param_value[i]); |
| 2825 | if (err) |
| 2826 | goto values_list_nest_cancel; |
| 2827 | } |
| 2828 | |
| 2829 | nla_nest_end(msg, param_values_list); |
| 2830 | nla_nest_end(msg, param_attr); |
| 2831 | genlmsg_end(msg, hdr); |
| 2832 | return 0; |
| 2833 | |
| 2834 | values_list_nest_cancel: |
| 2835 | nla_nest_end(msg, param_values_list); |
| 2836 | param_nest_cancel: |
| 2837 | nla_nest_cancel(msg, param_attr); |
| 2838 | genlmsg_cancel: |
| 2839 | genlmsg_cancel(msg, hdr); |
| 2840 | return -EMSGSIZE; |
| 2841 | } |
| 2842 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 2843 | static void devlink_param_notify(struct devlink *devlink, |
| 2844 | struct devlink_param_item *param_item, |
| 2845 | enum devlink_command cmd) |
| 2846 | { |
| 2847 | struct sk_buff *msg; |
| 2848 | int err; |
| 2849 | |
| 2850 | WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL); |
| 2851 | |
| 2852 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 2853 | if (!msg) |
| 2854 | return; |
| 2855 | err = devlink_nl_param_fill(msg, devlink, param_item, cmd, 0, 0, 0); |
| 2856 | if (err) { |
| 2857 | nlmsg_free(msg); |
| 2858 | return; |
| 2859 | } |
| 2860 | |
| 2861 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 2862 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 2863 | } |
| 2864 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2865 | static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg, |
| 2866 | struct netlink_callback *cb) |
| 2867 | { |
| 2868 | struct devlink_param_item *param_item; |
| 2869 | struct devlink *devlink; |
| 2870 | int start = cb->args[0]; |
| 2871 | int idx = 0; |
| 2872 | int err; |
| 2873 | |
| 2874 | mutex_lock(&devlink_mutex); |
| 2875 | list_for_each_entry(devlink, &devlink_list, list) { |
| 2876 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 2877 | continue; |
| 2878 | mutex_lock(&devlink->lock); |
| 2879 | list_for_each_entry(param_item, &devlink->param_list, list) { |
| 2880 | if (idx < start) { |
| 2881 | idx++; |
| 2882 | continue; |
| 2883 | } |
| 2884 | err = devlink_nl_param_fill(msg, devlink, param_item, |
| 2885 | DEVLINK_CMD_PARAM_GET, |
| 2886 | NETLINK_CB(cb->skb).portid, |
| 2887 | cb->nlh->nlmsg_seq, |
| 2888 | NLM_F_MULTI); |
| 2889 | if (err) { |
| 2890 | mutex_unlock(&devlink->lock); |
| 2891 | goto out; |
| 2892 | } |
| 2893 | idx++; |
| 2894 | } |
| 2895 | mutex_unlock(&devlink->lock); |
| 2896 | } |
| 2897 | out: |
| 2898 | mutex_unlock(&devlink_mutex); |
| 2899 | |
| 2900 | cb->args[0] = idx; |
| 2901 | return msg->len; |
| 2902 | } |
| 2903 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 2904 | static int |
| 2905 | devlink_param_type_get_from_info(struct genl_info *info, |
| 2906 | enum devlink_param_type *param_type) |
| 2907 | { |
| 2908 | if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE]) |
| 2909 | return -EINVAL; |
| 2910 | |
| 2911 | switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) { |
| 2912 | case NLA_U8: |
| 2913 | *param_type = DEVLINK_PARAM_TYPE_U8; |
| 2914 | break; |
| 2915 | case NLA_U16: |
| 2916 | *param_type = DEVLINK_PARAM_TYPE_U16; |
| 2917 | break; |
| 2918 | case NLA_U32: |
| 2919 | *param_type = DEVLINK_PARAM_TYPE_U32; |
| 2920 | break; |
| 2921 | case NLA_STRING: |
| 2922 | *param_type = DEVLINK_PARAM_TYPE_STRING; |
| 2923 | break; |
| 2924 | case NLA_FLAG: |
| 2925 | *param_type = DEVLINK_PARAM_TYPE_BOOL; |
| 2926 | break; |
| 2927 | default: |
| 2928 | return -EINVAL; |
| 2929 | } |
| 2930 | |
| 2931 | return 0; |
| 2932 | } |
| 2933 | |
| 2934 | static int |
| 2935 | devlink_param_value_get_from_info(const struct devlink_param *param, |
| 2936 | struct genl_info *info, |
| 2937 | union devlink_param_value *value) |
| 2938 | { |
| 2939 | if (param->type != DEVLINK_PARAM_TYPE_BOOL && |
| 2940 | !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) |
| 2941 | return -EINVAL; |
| 2942 | |
| 2943 | switch (param->type) { |
| 2944 | case DEVLINK_PARAM_TYPE_U8: |
| 2945 | value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 2946 | break; |
| 2947 | case DEVLINK_PARAM_TYPE_U16: |
| 2948 | value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 2949 | break; |
| 2950 | case DEVLINK_PARAM_TYPE_U32: |
| 2951 | value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 2952 | break; |
| 2953 | case DEVLINK_PARAM_TYPE_STRING: |
| 2954 | if (nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) > |
| 2955 | DEVLINK_PARAM_MAX_STRING_VALUE) |
| 2956 | return -EINVAL; |
| 2957 | value->vstr = nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]); |
| 2958 | break; |
| 2959 | case DEVLINK_PARAM_TYPE_BOOL: |
| 2960 | value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ? |
| 2961 | true : false; |
| 2962 | break; |
| 2963 | } |
| 2964 | return 0; |
| 2965 | } |
| 2966 | |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 2967 | static struct devlink_param_item * |
| 2968 | devlink_param_get_from_info(struct devlink *devlink, |
| 2969 | struct genl_info *info) |
| 2970 | { |
| 2971 | char *param_name; |
| 2972 | |
| 2973 | if (!info->attrs[DEVLINK_ATTR_PARAM_NAME]) |
| 2974 | return NULL; |
| 2975 | |
| 2976 | param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]); |
| 2977 | return devlink_param_find_by_name(&devlink->param_list, param_name); |
| 2978 | } |
| 2979 | |
| 2980 | static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb, |
| 2981 | struct genl_info *info) |
| 2982 | { |
| 2983 | struct devlink *devlink = info->user_ptr[0]; |
| 2984 | struct devlink_param_item *param_item; |
| 2985 | struct sk_buff *msg; |
| 2986 | int err; |
| 2987 | |
| 2988 | param_item = devlink_param_get_from_info(devlink, info); |
| 2989 | if (!param_item) |
| 2990 | return -EINVAL; |
| 2991 | |
| 2992 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 2993 | if (!msg) |
| 2994 | return -ENOMEM; |
| 2995 | |
| 2996 | err = devlink_nl_param_fill(msg, devlink, param_item, |
| 2997 | DEVLINK_CMD_PARAM_GET, |
| 2998 | info->snd_portid, info->snd_seq, 0); |
| 2999 | if (err) { |
| 3000 | nlmsg_free(msg); |
| 3001 | return err; |
| 3002 | } |
| 3003 | |
| 3004 | return genlmsg_reply(msg, info); |
| 3005 | } |
| 3006 | |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3007 | static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb, |
| 3008 | struct genl_info *info) |
| 3009 | { |
| 3010 | struct devlink *devlink = info->user_ptr[0]; |
| 3011 | enum devlink_param_type param_type; |
| 3012 | struct devlink_param_gset_ctx ctx; |
| 3013 | enum devlink_param_cmode cmode; |
| 3014 | struct devlink_param_item *param_item; |
| 3015 | const struct devlink_param *param; |
| 3016 | union devlink_param_value value; |
| 3017 | int err = 0; |
| 3018 | |
| 3019 | param_item = devlink_param_get_from_info(devlink, info); |
| 3020 | if (!param_item) |
| 3021 | return -EINVAL; |
| 3022 | param = param_item->param; |
| 3023 | err = devlink_param_type_get_from_info(info, ¶m_type); |
| 3024 | if (err) |
| 3025 | return err; |
| 3026 | if (param_type != param->type) |
| 3027 | return -EINVAL; |
| 3028 | err = devlink_param_value_get_from_info(param, info, &value); |
| 3029 | if (err) |
| 3030 | return err; |
| 3031 | if (param->validate) { |
| 3032 | err = param->validate(devlink, param->id, value, info->extack); |
| 3033 | if (err) |
| 3034 | return err; |
| 3035 | } |
| 3036 | |
| 3037 | if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]) |
| 3038 | return -EINVAL; |
| 3039 | cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]); |
| 3040 | if (!devlink_param_cmode_is_supported(param, cmode)) |
| 3041 | return -EOPNOTSUPP; |
| 3042 | |
| 3043 | if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) { |
| 3044 | param_item->driverinit_value = value; |
| 3045 | param_item->driverinit_value_valid = true; |
| 3046 | } else { |
| 3047 | if (!param->set) |
| 3048 | return -EOPNOTSUPP; |
| 3049 | ctx.val = value; |
| 3050 | ctx.cmode = cmode; |
| 3051 | err = devlink_param_set(devlink, param, &ctx); |
| 3052 | if (err) |
| 3053 | return err; |
| 3054 | } |
| 3055 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3056 | devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3057 | return 0; |
| 3058 | } |
| 3059 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3060 | static int devlink_param_register_one(struct devlink *devlink, |
| 3061 | const struct devlink_param *param) |
| 3062 | { |
| 3063 | struct devlink_param_item *param_item; |
| 3064 | |
| 3065 | if (devlink_param_find_by_name(&devlink->param_list, |
| 3066 | param->name)) |
| 3067 | return -EEXIST; |
| 3068 | |
| 3069 | if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 3070 | WARN_ON(param->get || param->set); |
| 3071 | else |
| 3072 | WARN_ON(!param->get || !param->set); |
| 3073 | |
| 3074 | param_item = kzalloc(sizeof(*param_item), GFP_KERNEL); |
| 3075 | if (!param_item) |
| 3076 | return -ENOMEM; |
| 3077 | param_item->param = param; |
| 3078 | |
| 3079 | list_add_tail(¶m_item->list, &devlink->param_list); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3080 | devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3081 | return 0; |
| 3082 | } |
| 3083 | |
| 3084 | static void devlink_param_unregister_one(struct devlink *devlink, |
| 3085 | const struct devlink_param *param) |
| 3086 | { |
| 3087 | struct devlink_param_item *param_item; |
| 3088 | |
| 3089 | param_item = devlink_param_find_by_name(&devlink->param_list, |
| 3090 | param->name); |
| 3091 | WARN_ON(!param_item); |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 3092 | devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_DEL); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3093 | list_del(¶m_item->list); |
| 3094 | kfree(param_item); |
| 3095 | } |
| 3096 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3097 | static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = { |
| 3098 | [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING }, |
| 3099 | [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING }, |
| 3100 | [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 }, |
| 3101 | [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 }, |
| 3102 | [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 3103 | [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 }, |
| 3104 | [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 }, |
| 3105 | [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 }, |
| 3106 | [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 }, |
| 3107 | [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 }, |
| 3108 | [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 }, |
| 3109 | [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 3110 | [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 }, |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 3111 | [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 }, |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 3112 | [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3113 | [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING }, |
| 3114 | [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3115 | [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64}, |
| 3116 | [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64}, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3117 | [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING }, |
| 3118 | [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 }, |
| 3119 | [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3120 | }; |
| 3121 | |
| 3122 | static const struct genl_ops devlink_nl_ops[] = { |
| 3123 | { |
| 3124 | .cmd = DEVLINK_CMD_GET, |
| 3125 | .doit = devlink_nl_cmd_get_doit, |
| 3126 | .dumpit = devlink_nl_cmd_get_dumpit, |
| 3127 | .policy = devlink_nl_policy, |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 3128 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3129 | /* can be retrieved by unprivileged users */ |
| 3130 | }, |
| 3131 | { |
| 3132 | .cmd = DEVLINK_CMD_PORT_GET, |
| 3133 | .doit = devlink_nl_cmd_port_get_doit, |
| 3134 | .dumpit = devlink_nl_cmd_port_get_dumpit, |
| 3135 | .policy = devlink_nl_policy, |
| 3136 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 3137 | /* can be retrieved by unprivileged users */ |
| 3138 | }, |
| 3139 | { |
| 3140 | .cmd = DEVLINK_CMD_PORT_SET, |
| 3141 | .doit = devlink_nl_cmd_port_set_doit, |
| 3142 | .policy = devlink_nl_policy, |
| 3143 | .flags = GENL_ADMIN_PERM, |
| 3144 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 3145 | }, |
| 3146 | { |
| 3147 | .cmd = DEVLINK_CMD_PORT_SPLIT, |
| 3148 | .doit = devlink_nl_cmd_port_split_doit, |
| 3149 | .policy = devlink_nl_policy, |
| 3150 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3151 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 3152 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3153 | }, |
| 3154 | { |
| 3155 | .cmd = DEVLINK_CMD_PORT_UNSPLIT, |
| 3156 | .doit = devlink_nl_cmd_port_unsplit_doit, |
| 3157 | .policy = devlink_nl_policy, |
| 3158 | .flags = GENL_ADMIN_PERM, |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3159 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 3160 | DEVLINK_NL_FLAG_NO_LOCK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3161 | }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 3162 | { |
| 3163 | .cmd = DEVLINK_CMD_SB_GET, |
| 3164 | .doit = devlink_nl_cmd_sb_get_doit, |
| 3165 | .dumpit = devlink_nl_cmd_sb_get_dumpit, |
| 3166 | .policy = devlink_nl_policy, |
| 3167 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 3168 | DEVLINK_NL_FLAG_NEED_SB, |
| 3169 | /* can be retrieved by unprivileged users */ |
| 3170 | }, |
| 3171 | { |
| 3172 | .cmd = DEVLINK_CMD_SB_POOL_GET, |
| 3173 | .doit = devlink_nl_cmd_sb_pool_get_doit, |
| 3174 | .dumpit = devlink_nl_cmd_sb_pool_get_dumpit, |
| 3175 | .policy = devlink_nl_policy, |
| 3176 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 3177 | DEVLINK_NL_FLAG_NEED_SB, |
| 3178 | /* can be retrieved by unprivileged users */ |
| 3179 | }, |
| 3180 | { |
| 3181 | .cmd = DEVLINK_CMD_SB_POOL_SET, |
| 3182 | .doit = devlink_nl_cmd_sb_pool_set_doit, |
| 3183 | .policy = devlink_nl_policy, |
| 3184 | .flags = GENL_ADMIN_PERM, |
| 3185 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 3186 | DEVLINK_NL_FLAG_NEED_SB, |
| 3187 | }, |
| 3188 | { |
| 3189 | .cmd = DEVLINK_CMD_SB_PORT_POOL_GET, |
| 3190 | .doit = devlink_nl_cmd_sb_port_pool_get_doit, |
| 3191 | .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit, |
| 3192 | .policy = devlink_nl_policy, |
| 3193 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 3194 | DEVLINK_NL_FLAG_NEED_SB, |
| 3195 | /* can be retrieved by unprivileged users */ |
| 3196 | }, |
| 3197 | { |
| 3198 | .cmd = DEVLINK_CMD_SB_PORT_POOL_SET, |
| 3199 | .doit = devlink_nl_cmd_sb_port_pool_set_doit, |
| 3200 | .policy = devlink_nl_policy, |
| 3201 | .flags = GENL_ADMIN_PERM, |
| 3202 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 3203 | DEVLINK_NL_FLAG_NEED_SB, |
| 3204 | }, |
| 3205 | { |
| 3206 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET, |
| 3207 | .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit, |
| 3208 | .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit, |
| 3209 | .policy = devlink_nl_policy, |
| 3210 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 3211 | DEVLINK_NL_FLAG_NEED_SB, |
| 3212 | /* can be retrieved by unprivileged users */ |
| 3213 | }, |
| 3214 | { |
| 3215 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET, |
| 3216 | .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit, |
| 3217 | .policy = devlink_nl_policy, |
| 3218 | .flags = GENL_ADMIN_PERM, |
| 3219 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 3220 | DEVLINK_NL_FLAG_NEED_SB, |
| 3221 | }, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 3222 | { |
| 3223 | .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT, |
| 3224 | .doit = devlink_nl_cmd_sb_occ_snapshot_doit, |
| 3225 | .policy = devlink_nl_policy, |
| 3226 | .flags = GENL_ADMIN_PERM, |
| 3227 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3228 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 3229 | }, |
| 3230 | { |
| 3231 | .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR, |
| 3232 | .doit = devlink_nl_cmd_sb_occ_max_clear_doit, |
| 3233 | .policy = devlink_nl_policy, |
| 3234 | .flags = GENL_ADMIN_PERM, |
| 3235 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3236 | DEVLINK_NL_FLAG_NEED_SB, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 3237 | }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 3238 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 3239 | .cmd = DEVLINK_CMD_ESWITCH_GET, |
| 3240 | .doit = devlink_nl_cmd_eswitch_get_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 3241 | .policy = devlink_nl_policy, |
| 3242 | .flags = GENL_ADMIN_PERM, |
| 3243 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 3244 | }, |
| 3245 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 3246 | .cmd = DEVLINK_CMD_ESWITCH_SET, |
| 3247 | .doit = devlink_nl_cmd_eswitch_set_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 3248 | .policy = devlink_nl_policy, |
| 3249 | .flags = GENL_ADMIN_PERM, |
Jakub Kicinski | 7ac1cc9 | 2018-05-21 22:12:50 -0700 | [diff] [blame] | 3250 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 3251 | DEVLINK_NL_FLAG_NO_LOCK, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 3252 | }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3253 | { |
| 3254 | .cmd = DEVLINK_CMD_DPIPE_TABLE_GET, |
| 3255 | .doit = devlink_nl_cmd_dpipe_table_get, |
| 3256 | .policy = devlink_nl_policy, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3257 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 3258 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3259 | }, |
| 3260 | { |
| 3261 | .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET, |
| 3262 | .doit = devlink_nl_cmd_dpipe_entries_get, |
| 3263 | .policy = devlink_nl_policy, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3264 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 3265 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3266 | }, |
| 3267 | { |
| 3268 | .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET, |
| 3269 | .doit = devlink_nl_cmd_dpipe_headers_get, |
| 3270 | .policy = devlink_nl_policy, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3271 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 3272 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3273 | }, |
| 3274 | { |
| 3275 | .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET, |
| 3276 | .doit = devlink_nl_cmd_dpipe_table_counters_set, |
| 3277 | .policy = devlink_nl_policy, |
| 3278 | .flags = GENL_ADMIN_PERM, |
| 3279 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 3280 | }, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3281 | { |
| 3282 | .cmd = DEVLINK_CMD_RESOURCE_SET, |
| 3283 | .doit = devlink_nl_cmd_resource_set, |
| 3284 | .policy = devlink_nl_policy, |
| 3285 | .flags = GENL_ADMIN_PERM, |
| 3286 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 3287 | }, |
| 3288 | { |
| 3289 | .cmd = DEVLINK_CMD_RESOURCE_DUMP, |
| 3290 | .doit = devlink_nl_cmd_resource_dump, |
| 3291 | .policy = devlink_nl_policy, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3292 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Arkadi Sharshevsky | 67ae686 | 2018-03-08 12:52:25 +0200 | [diff] [blame] | 3293 | /* can be retrieved by unprivileged users */ |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3294 | }, |
Arkadi Sharshevsky | 2d8dc5b | 2018-01-15 08:59:04 +0100 | [diff] [blame] | 3295 | { |
| 3296 | .cmd = DEVLINK_CMD_RELOAD, |
| 3297 | .doit = devlink_nl_cmd_reload, |
| 3298 | .policy = devlink_nl_policy, |
| 3299 | .flags = GENL_ADMIN_PERM, |
| 3300 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 3301 | DEVLINK_NL_FLAG_NO_LOCK, |
| 3302 | }, |
Moshe Shemesh | 45f05de | 2018-07-04 14:30:29 +0300 | [diff] [blame] | 3303 | { |
| 3304 | .cmd = DEVLINK_CMD_PARAM_GET, |
| 3305 | .doit = devlink_nl_cmd_param_get_doit, |
| 3306 | .dumpit = devlink_nl_cmd_param_get_dumpit, |
| 3307 | .policy = devlink_nl_policy, |
| 3308 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 3309 | /* can be retrieved by unprivileged users */ |
| 3310 | }, |
Moshe Shemesh | e3b7ca1 | 2018-07-04 14:30:30 +0300 | [diff] [blame] | 3311 | { |
| 3312 | .cmd = DEVLINK_CMD_PARAM_SET, |
| 3313 | .doit = devlink_nl_cmd_param_set_doit, |
| 3314 | .policy = devlink_nl_policy, |
| 3315 | .flags = GENL_ADMIN_PERM, |
| 3316 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 3317 | }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3318 | }; |
| 3319 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 3320 | static struct genl_family devlink_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 3321 | .name = DEVLINK_GENL_NAME, |
| 3322 | .version = DEVLINK_GENL_VERSION, |
| 3323 | .maxattr = DEVLINK_ATTR_MAX, |
| 3324 | .netnsok = true, |
| 3325 | .pre_doit = devlink_nl_pre_doit, |
| 3326 | .post_doit = devlink_nl_post_doit, |
| 3327 | .module = THIS_MODULE, |
| 3328 | .ops = devlink_nl_ops, |
| 3329 | .n_ops = ARRAY_SIZE(devlink_nl_ops), |
| 3330 | .mcgrps = devlink_nl_mcgrps, |
| 3331 | .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps), |
| 3332 | }; |
| 3333 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3334 | /** |
| 3335 | * devlink_alloc - Allocate new devlink instance resources |
| 3336 | * |
| 3337 | * @ops: ops |
| 3338 | * @priv_size: size of user private data |
| 3339 | * |
| 3340 | * Allocate new devlink instance resources, including devlink index |
| 3341 | * and name. |
| 3342 | */ |
| 3343 | struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) |
| 3344 | { |
| 3345 | struct devlink *devlink; |
| 3346 | |
| 3347 | devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL); |
| 3348 | if (!devlink) |
| 3349 | return NULL; |
| 3350 | devlink->ops = ops; |
| 3351 | devlink_net_set(devlink, &init_net); |
| 3352 | INIT_LIST_HEAD(&devlink->port_list); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 3353 | INIT_LIST_HEAD(&devlink->sb_list); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3354 | INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3355 | INIT_LIST_HEAD(&devlink->resource_list); |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3356 | INIT_LIST_HEAD(&devlink->param_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3357 | mutex_init(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3358 | return devlink; |
| 3359 | } |
| 3360 | EXPORT_SYMBOL_GPL(devlink_alloc); |
| 3361 | |
| 3362 | /** |
| 3363 | * devlink_register - Register devlink instance |
| 3364 | * |
| 3365 | * @devlink: devlink |
| 3366 | */ |
| 3367 | int devlink_register(struct devlink *devlink, struct device *dev) |
| 3368 | { |
| 3369 | mutex_lock(&devlink_mutex); |
| 3370 | devlink->dev = dev; |
| 3371 | list_add_tail(&devlink->list, &devlink_list); |
| 3372 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 3373 | mutex_unlock(&devlink_mutex); |
| 3374 | return 0; |
| 3375 | } |
| 3376 | EXPORT_SYMBOL_GPL(devlink_register); |
| 3377 | |
| 3378 | /** |
| 3379 | * devlink_unregister - Unregister devlink instance |
| 3380 | * |
| 3381 | * @devlink: devlink |
| 3382 | */ |
| 3383 | void devlink_unregister(struct devlink *devlink) |
| 3384 | { |
| 3385 | mutex_lock(&devlink_mutex); |
| 3386 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 3387 | list_del(&devlink->list); |
| 3388 | mutex_unlock(&devlink_mutex); |
| 3389 | } |
| 3390 | EXPORT_SYMBOL_GPL(devlink_unregister); |
| 3391 | |
| 3392 | /** |
| 3393 | * devlink_free - Free devlink instance resources |
| 3394 | * |
| 3395 | * @devlink: devlink |
| 3396 | */ |
| 3397 | void devlink_free(struct devlink *devlink) |
| 3398 | { |
| 3399 | kfree(devlink); |
| 3400 | } |
| 3401 | EXPORT_SYMBOL_GPL(devlink_free); |
| 3402 | |
| 3403 | /** |
| 3404 | * devlink_port_register - Register devlink port |
| 3405 | * |
| 3406 | * @devlink: devlink |
| 3407 | * @devlink_port: devlink port |
| 3408 | * @port_index |
| 3409 | * |
| 3410 | * Register devlink port with provided port index. User can use |
| 3411 | * any indexing, even hw-related one. devlink_port structure |
| 3412 | * is convenient to be embedded inside user driver private structure. |
| 3413 | * Note that the caller should take care of zeroing the devlink_port |
| 3414 | * structure. |
| 3415 | */ |
| 3416 | int devlink_port_register(struct devlink *devlink, |
| 3417 | struct devlink_port *devlink_port, |
| 3418 | unsigned int port_index) |
| 3419 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3420 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3421 | if (devlink_port_index_exists(devlink, port_index)) { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3422 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3423 | return -EEXIST; |
| 3424 | } |
| 3425 | devlink_port->devlink = devlink; |
| 3426 | devlink_port->index = port_index; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3427 | devlink_port->registered = true; |
| 3428 | list_add_tail(&devlink_port->list, &devlink->port_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3429 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3430 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 3431 | return 0; |
| 3432 | } |
| 3433 | EXPORT_SYMBOL_GPL(devlink_port_register); |
| 3434 | |
| 3435 | /** |
| 3436 | * devlink_port_unregister - Unregister devlink port |
| 3437 | * |
| 3438 | * @devlink_port: devlink port |
| 3439 | */ |
| 3440 | void devlink_port_unregister(struct devlink_port *devlink_port) |
| 3441 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3442 | struct devlink *devlink = devlink_port->devlink; |
| 3443 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3444 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3445 | mutex_lock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3446 | list_del(&devlink_port->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3447 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3448 | } |
| 3449 | EXPORT_SYMBOL_GPL(devlink_port_unregister); |
| 3450 | |
| 3451 | static void __devlink_port_type_set(struct devlink_port *devlink_port, |
| 3452 | enum devlink_port_type type, |
| 3453 | void *type_dev) |
| 3454 | { |
| 3455 | devlink_port->type = type; |
| 3456 | devlink_port->type_dev = type_dev; |
| 3457 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 3458 | } |
| 3459 | |
| 3460 | /** |
| 3461 | * devlink_port_type_eth_set - Set port type to Ethernet |
| 3462 | * |
| 3463 | * @devlink_port: devlink port |
| 3464 | * @netdev: related netdevice |
| 3465 | */ |
| 3466 | void devlink_port_type_eth_set(struct devlink_port *devlink_port, |
| 3467 | struct net_device *netdev) |
| 3468 | { |
| 3469 | return __devlink_port_type_set(devlink_port, |
| 3470 | DEVLINK_PORT_TYPE_ETH, netdev); |
| 3471 | } |
| 3472 | EXPORT_SYMBOL_GPL(devlink_port_type_eth_set); |
| 3473 | |
| 3474 | /** |
| 3475 | * devlink_port_type_ib_set - Set port type to InfiniBand |
| 3476 | * |
| 3477 | * @devlink_port: devlink port |
| 3478 | * @ibdev: related IB device |
| 3479 | */ |
| 3480 | void devlink_port_type_ib_set(struct devlink_port *devlink_port, |
| 3481 | struct ib_device *ibdev) |
| 3482 | { |
| 3483 | return __devlink_port_type_set(devlink_port, |
| 3484 | DEVLINK_PORT_TYPE_IB, ibdev); |
| 3485 | } |
| 3486 | EXPORT_SYMBOL_GPL(devlink_port_type_ib_set); |
| 3487 | |
| 3488 | /** |
| 3489 | * devlink_port_type_clear - Clear port type |
| 3490 | * |
| 3491 | * @devlink_port: devlink port |
| 3492 | */ |
| 3493 | void devlink_port_type_clear(struct devlink_port *devlink_port) |
| 3494 | { |
| 3495 | return __devlink_port_type_set(devlink_port, |
| 3496 | DEVLINK_PORT_TYPE_NOTSET, NULL); |
| 3497 | } |
| 3498 | EXPORT_SYMBOL_GPL(devlink_port_type_clear); |
| 3499 | |
| 3500 | /** |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 3501 | * devlink_port_attrs_set - Set port attributes |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3502 | * |
| 3503 | * @devlink_port: devlink port |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 3504 | * @flavour: flavour of the port |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 3505 | * @port_number: number of the port that is facing user, for example |
| 3506 | * the front panel port number |
| 3507 | * @split: indicates if this is split port |
| 3508 | * @split_subport_number: if the port is split, this is the number |
| 3509 | * of subport. |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3510 | */ |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 3511 | void devlink_port_attrs_set(struct devlink_port *devlink_port, |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 3512 | enum devlink_port_flavour flavour, |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 3513 | u32 port_number, bool split, |
| 3514 | u32 split_subport_number) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3515 | { |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 3516 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 3517 | |
| 3518 | attrs->set = true; |
Jiri Pirko | 5ec1380 | 2018-05-18 09:29:01 +0200 | [diff] [blame] | 3519 | attrs->flavour = flavour; |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 3520 | attrs->port_number = port_number; |
| 3521 | attrs->split = split; |
| 3522 | attrs->split_subport_number = split_subport_number; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3523 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 3524 | } |
Jiri Pirko | b9ffcba | 2018-05-18 09:29:00 +0200 | [diff] [blame] | 3525 | EXPORT_SYMBOL_GPL(devlink_port_attrs_set); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 3526 | |
Jiri Pirko | 08474c1 | 2018-05-18 09:29:02 +0200 | [diff] [blame] | 3527 | int devlink_port_get_phys_port_name(struct devlink_port *devlink_port, |
| 3528 | char *name, size_t len) |
| 3529 | { |
| 3530 | struct devlink_port_attrs *attrs = &devlink_port->attrs; |
| 3531 | int n = 0; |
| 3532 | |
| 3533 | if (!attrs->set) |
| 3534 | return -EOPNOTSUPP; |
| 3535 | |
| 3536 | switch (attrs->flavour) { |
| 3537 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: |
| 3538 | if (!attrs->split) |
| 3539 | n = snprintf(name, len, "p%u", attrs->port_number); |
| 3540 | else |
| 3541 | n = snprintf(name, len, "p%us%u", attrs->port_number, |
| 3542 | attrs->split_subport_number); |
| 3543 | break; |
| 3544 | case DEVLINK_PORT_FLAVOUR_CPU: |
| 3545 | case DEVLINK_PORT_FLAVOUR_DSA: |
| 3546 | /* As CPU and DSA ports do not have a netdevice associated |
| 3547 | * case should not ever happen. |
| 3548 | */ |
| 3549 | WARN_ON(1); |
| 3550 | return -EINVAL; |
| 3551 | } |
| 3552 | |
| 3553 | if (n >= len) |
| 3554 | return -EINVAL; |
| 3555 | |
| 3556 | return 0; |
| 3557 | } |
| 3558 | EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name); |
| 3559 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 3560 | int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, |
| 3561 | u32 size, u16 ingress_pools_count, |
| 3562 | u16 egress_pools_count, u16 ingress_tc_count, |
| 3563 | u16 egress_tc_count) |
| 3564 | { |
| 3565 | struct devlink_sb *devlink_sb; |
| 3566 | int err = 0; |
| 3567 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3568 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 3569 | if (devlink_sb_index_exists(devlink, sb_index)) { |
| 3570 | err = -EEXIST; |
| 3571 | goto unlock; |
| 3572 | } |
| 3573 | |
| 3574 | devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL); |
| 3575 | if (!devlink_sb) { |
| 3576 | err = -ENOMEM; |
| 3577 | goto unlock; |
| 3578 | } |
| 3579 | devlink_sb->index = sb_index; |
| 3580 | devlink_sb->size = size; |
| 3581 | devlink_sb->ingress_pools_count = ingress_pools_count; |
| 3582 | devlink_sb->egress_pools_count = egress_pools_count; |
| 3583 | devlink_sb->ingress_tc_count = ingress_tc_count; |
| 3584 | devlink_sb->egress_tc_count = egress_tc_count; |
| 3585 | list_add_tail(&devlink_sb->list, &devlink->sb_list); |
| 3586 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3587 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 3588 | return err; |
| 3589 | } |
| 3590 | EXPORT_SYMBOL_GPL(devlink_sb_register); |
| 3591 | |
| 3592 | void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index) |
| 3593 | { |
| 3594 | struct devlink_sb *devlink_sb; |
| 3595 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3596 | mutex_lock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 3597 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 3598 | WARN_ON(!devlink_sb); |
| 3599 | list_del(&devlink_sb->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3600 | mutex_unlock(&devlink->lock); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 3601 | kfree(devlink_sb); |
| 3602 | } |
| 3603 | EXPORT_SYMBOL_GPL(devlink_sb_unregister); |
| 3604 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3605 | /** |
| 3606 | * devlink_dpipe_headers_register - register dpipe headers |
| 3607 | * |
| 3608 | * @devlink: devlink |
| 3609 | * @dpipe_headers: dpipe header array |
| 3610 | * |
| 3611 | * Register the headers supported by hardware. |
| 3612 | */ |
| 3613 | int devlink_dpipe_headers_register(struct devlink *devlink, |
| 3614 | struct devlink_dpipe_headers *dpipe_headers) |
| 3615 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3616 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3617 | devlink->dpipe_headers = dpipe_headers; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3618 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3619 | return 0; |
| 3620 | } |
| 3621 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register); |
| 3622 | |
| 3623 | /** |
| 3624 | * devlink_dpipe_headers_unregister - unregister dpipe headers |
| 3625 | * |
| 3626 | * @devlink: devlink |
| 3627 | * |
| 3628 | * Unregister the headers supported by hardware. |
| 3629 | */ |
| 3630 | void devlink_dpipe_headers_unregister(struct devlink *devlink) |
| 3631 | { |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3632 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3633 | devlink->dpipe_headers = NULL; |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3634 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3635 | } |
| 3636 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister); |
| 3637 | |
| 3638 | /** |
| 3639 | * devlink_dpipe_table_counter_enabled - check if counter allocation |
| 3640 | * required |
| 3641 | * @devlink: devlink |
| 3642 | * @table_name: tables name |
| 3643 | * |
| 3644 | * Used by driver to check if counter allocation is required. |
| 3645 | * After counter allocation is turned on the table entries |
| 3646 | * are updated to include counter statistics. |
| 3647 | * |
| 3648 | * After that point on the driver must respect the counter |
| 3649 | * state so that each entry added to the table is added |
| 3650 | * with a counter. |
| 3651 | */ |
| 3652 | bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, |
| 3653 | const char *table_name) |
| 3654 | { |
| 3655 | struct devlink_dpipe_table *table; |
| 3656 | bool enabled; |
| 3657 | |
| 3658 | rcu_read_lock(); |
| 3659 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 3660 | table_name); |
| 3661 | enabled = false; |
| 3662 | if (table) |
| 3663 | enabled = table->counters_enabled; |
| 3664 | rcu_read_unlock(); |
| 3665 | return enabled; |
| 3666 | } |
| 3667 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled); |
| 3668 | |
| 3669 | /** |
| 3670 | * devlink_dpipe_table_register - register dpipe table |
| 3671 | * |
| 3672 | * @devlink: devlink |
| 3673 | * @table_name: table name |
| 3674 | * @table_ops: table ops |
| 3675 | * @priv: priv |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3676 | * @counter_control_extern: external control for counters |
| 3677 | */ |
| 3678 | int devlink_dpipe_table_register(struct devlink *devlink, |
| 3679 | const char *table_name, |
| 3680 | struct devlink_dpipe_table_ops *table_ops, |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 3681 | void *priv, bool counter_control_extern) |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3682 | { |
| 3683 | struct devlink_dpipe_table *table; |
| 3684 | |
| 3685 | if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name)) |
| 3686 | return -EEXIST; |
| 3687 | |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame] | 3688 | if (WARN_ON(!table_ops->size_get)) |
| 3689 | return -EINVAL; |
| 3690 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3691 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
| 3692 | if (!table) |
| 3693 | return -ENOMEM; |
| 3694 | |
| 3695 | table->name = table_name; |
| 3696 | table->table_ops = table_ops; |
| 3697 | table->priv = priv; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3698 | table->counter_control_extern = counter_control_extern; |
| 3699 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3700 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3701 | list_add_tail_rcu(&table->list, &devlink->dpipe_table_list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3702 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3703 | return 0; |
| 3704 | } |
| 3705 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_register); |
| 3706 | |
| 3707 | /** |
| 3708 | * devlink_dpipe_table_unregister - unregister dpipe table |
| 3709 | * |
| 3710 | * @devlink: devlink |
| 3711 | * @table_name: table name |
| 3712 | */ |
| 3713 | void devlink_dpipe_table_unregister(struct devlink *devlink, |
| 3714 | const char *table_name) |
| 3715 | { |
| 3716 | struct devlink_dpipe_table *table; |
| 3717 | |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3718 | mutex_lock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3719 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 3720 | table_name); |
| 3721 | if (!table) |
| 3722 | goto unlock; |
| 3723 | list_del_rcu(&table->list); |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3724 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3725 | kfree_rcu(table, rcu); |
| 3726 | return; |
| 3727 | unlock: |
Arkadi Sharshevsky | 2406e7e | 2018-01-15 08:59:02 +0100 | [diff] [blame] | 3728 | mutex_unlock(&devlink->lock); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 3729 | } |
| 3730 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister); |
| 3731 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3732 | /** |
| 3733 | * devlink_resource_register - devlink resource register |
| 3734 | * |
| 3735 | * @devlink: devlink |
| 3736 | * @resource_name: resource's name |
| 3737 | * @top_hierarchy: top hierarchy |
| 3738 | * @reload_required: reload is required for new configuration to |
| 3739 | * apply |
| 3740 | * @resource_size: resource's size |
| 3741 | * @resource_id: resource's id |
| 3742 | * @parent_reosurce_id: resource's parent id |
| 3743 | * @size params: size parameters |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3744 | */ |
| 3745 | int devlink_resource_register(struct devlink *devlink, |
| 3746 | const char *resource_name, |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3747 | u64 resource_size, |
| 3748 | u64 resource_id, |
| 3749 | u64 parent_resource_id, |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 3750 | const struct devlink_resource_size_params *size_params) |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3751 | { |
| 3752 | struct devlink_resource *resource; |
| 3753 | struct list_head *resource_list; |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 3754 | bool top_hierarchy; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3755 | int err = 0; |
| 3756 | |
David Ahern | 1453074 | 2018-03-20 19:31:14 -0700 | [diff] [blame] | 3757 | top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP; |
| 3758 | |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3759 | mutex_lock(&devlink->lock); |
| 3760 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 3761 | if (resource) { |
| 3762 | err = -EINVAL; |
| 3763 | goto out; |
| 3764 | } |
| 3765 | |
| 3766 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
| 3767 | if (!resource) { |
| 3768 | err = -ENOMEM; |
| 3769 | goto out; |
| 3770 | } |
| 3771 | |
| 3772 | if (top_hierarchy) { |
| 3773 | resource_list = &devlink->resource_list; |
| 3774 | } else { |
| 3775 | struct devlink_resource *parent_resource; |
| 3776 | |
| 3777 | parent_resource = devlink_resource_find(devlink, NULL, |
| 3778 | parent_resource_id); |
| 3779 | if (parent_resource) { |
| 3780 | resource_list = &parent_resource->resource_list; |
| 3781 | resource->parent = parent_resource; |
| 3782 | } else { |
Colin Ian King | b75703d | 2018-01-22 10:31:19 +0000 | [diff] [blame] | 3783 | kfree(resource); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3784 | err = -EINVAL; |
| 3785 | goto out; |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | resource->name = resource_name; |
| 3790 | resource->size = resource_size; |
| 3791 | resource->size_new = resource_size; |
| 3792 | resource->id = resource_id; |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3793 | resource->size_valid = true; |
Jiri Pirko | 77d2709 | 2018-02-28 13:12:09 +0100 | [diff] [blame] | 3794 | memcpy(&resource->size_params, size_params, |
| 3795 | sizeof(resource->size_params)); |
Arkadi Sharshevsky | d9f9b9a | 2018-01-15 08:59:03 +0100 | [diff] [blame] | 3796 | INIT_LIST_HEAD(&resource->resource_list); |
| 3797 | list_add_tail(&resource->list, resource_list); |
| 3798 | out: |
| 3799 | mutex_unlock(&devlink->lock); |
| 3800 | return err; |
| 3801 | } |
| 3802 | EXPORT_SYMBOL_GPL(devlink_resource_register); |
| 3803 | |
| 3804 | /** |
| 3805 | * devlink_resources_unregister - free all resources |
| 3806 | * |
| 3807 | * @devlink: devlink |
| 3808 | * @resource: resource |
| 3809 | */ |
| 3810 | void devlink_resources_unregister(struct devlink *devlink, |
| 3811 | struct devlink_resource *resource) |
| 3812 | { |
| 3813 | struct devlink_resource *tmp, *child_resource; |
| 3814 | struct list_head *resource_list; |
| 3815 | |
| 3816 | if (resource) |
| 3817 | resource_list = &resource->resource_list; |
| 3818 | else |
| 3819 | resource_list = &devlink->resource_list; |
| 3820 | |
| 3821 | if (!resource) |
| 3822 | mutex_lock(&devlink->lock); |
| 3823 | |
| 3824 | list_for_each_entry_safe(child_resource, tmp, resource_list, list) { |
| 3825 | devlink_resources_unregister(devlink, child_resource); |
| 3826 | list_del(&child_resource->list); |
| 3827 | kfree(child_resource); |
| 3828 | } |
| 3829 | |
| 3830 | if (!resource) |
| 3831 | mutex_unlock(&devlink->lock); |
| 3832 | } |
| 3833 | EXPORT_SYMBOL_GPL(devlink_resources_unregister); |
| 3834 | |
| 3835 | /** |
| 3836 | * devlink_resource_size_get - get and update size |
| 3837 | * |
| 3838 | * @devlink: devlink |
| 3839 | * @resource_id: the requested resource id |
| 3840 | * @p_resource_size: ptr to update |
| 3841 | */ |
| 3842 | int devlink_resource_size_get(struct devlink *devlink, |
| 3843 | u64 resource_id, |
| 3844 | u64 *p_resource_size) |
| 3845 | { |
| 3846 | struct devlink_resource *resource; |
| 3847 | int err = 0; |
| 3848 | |
| 3849 | mutex_lock(&devlink->lock); |
| 3850 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 3851 | if (!resource) { |
| 3852 | err = -EINVAL; |
| 3853 | goto out; |
| 3854 | } |
| 3855 | *p_resource_size = resource->size_new; |
| 3856 | resource->size = resource->size_new; |
| 3857 | out: |
| 3858 | mutex_unlock(&devlink->lock); |
| 3859 | return err; |
| 3860 | } |
| 3861 | EXPORT_SYMBOL_GPL(devlink_resource_size_get); |
| 3862 | |
Arkadi Sharshevsky | 56dc7cd | 2018-01-15 08:59:05 +0100 | [diff] [blame] | 3863 | /** |
| 3864 | * devlink_dpipe_table_resource_set - set the resource id |
| 3865 | * |
| 3866 | * @devlink: devlink |
| 3867 | * @table_name: table name |
| 3868 | * @resource_id: resource id |
| 3869 | * @resource_units: number of resource's units consumed per table's entry |
| 3870 | */ |
| 3871 | int devlink_dpipe_table_resource_set(struct devlink *devlink, |
| 3872 | const char *table_name, u64 resource_id, |
| 3873 | u64 resource_units) |
| 3874 | { |
| 3875 | struct devlink_dpipe_table *table; |
| 3876 | int err = 0; |
| 3877 | |
| 3878 | mutex_lock(&devlink->lock); |
| 3879 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 3880 | table_name); |
| 3881 | if (!table) { |
| 3882 | err = -EINVAL; |
| 3883 | goto out; |
| 3884 | } |
| 3885 | table->resource_id = resource_id; |
| 3886 | table->resource_units = resource_units; |
| 3887 | table->resource_valid = true; |
| 3888 | out: |
| 3889 | mutex_unlock(&devlink->lock); |
| 3890 | return err; |
| 3891 | } |
| 3892 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set); |
| 3893 | |
Jiri Pirko | fc56be4 | 2018-04-05 22:13:21 +0200 | [diff] [blame] | 3894 | /** |
| 3895 | * devlink_resource_occ_get_register - register occupancy getter |
| 3896 | * |
| 3897 | * @devlink: devlink |
| 3898 | * @resource_id: resource id |
| 3899 | * @occ_get: occupancy getter callback |
| 3900 | * @occ_get_priv: occupancy getter callback priv |
| 3901 | */ |
| 3902 | void devlink_resource_occ_get_register(struct devlink *devlink, |
| 3903 | u64 resource_id, |
| 3904 | devlink_resource_occ_get_t *occ_get, |
| 3905 | void *occ_get_priv) |
| 3906 | { |
| 3907 | struct devlink_resource *resource; |
| 3908 | |
| 3909 | mutex_lock(&devlink->lock); |
| 3910 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 3911 | if (WARN_ON(!resource)) |
| 3912 | goto out; |
| 3913 | WARN_ON(resource->occ_get); |
| 3914 | |
| 3915 | resource->occ_get = occ_get; |
| 3916 | resource->occ_get_priv = occ_get_priv; |
| 3917 | out: |
| 3918 | mutex_unlock(&devlink->lock); |
| 3919 | } |
| 3920 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register); |
| 3921 | |
| 3922 | /** |
| 3923 | * devlink_resource_occ_get_unregister - unregister occupancy getter |
| 3924 | * |
| 3925 | * @devlink: devlink |
| 3926 | * @resource_id: resource id |
| 3927 | */ |
| 3928 | void devlink_resource_occ_get_unregister(struct devlink *devlink, |
| 3929 | u64 resource_id) |
| 3930 | { |
| 3931 | struct devlink_resource *resource; |
| 3932 | |
| 3933 | mutex_lock(&devlink->lock); |
| 3934 | resource = devlink_resource_find(devlink, NULL, resource_id); |
| 3935 | if (WARN_ON(!resource)) |
| 3936 | goto out; |
| 3937 | WARN_ON(!resource->occ_get); |
| 3938 | |
| 3939 | resource->occ_get = NULL; |
| 3940 | resource->occ_get_priv = NULL; |
| 3941 | out: |
| 3942 | mutex_unlock(&devlink->lock); |
| 3943 | } |
| 3944 | EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister); |
| 3945 | |
Moshe Shemesh | eabaef1 | 2018-07-04 14:30:28 +0300 | [diff] [blame] | 3946 | /** |
| 3947 | * devlink_params_register - register configuration parameters |
| 3948 | * |
| 3949 | * @devlink: devlink |
| 3950 | * @params: configuration parameters array |
| 3951 | * @params_count: number of parameters provided |
| 3952 | * |
| 3953 | * Register the configuration parameters supported by the driver. |
| 3954 | */ |
| 3955 | int devlink_params_register(struct devlink *devlink, |
| 3956 | const struct devlink_param *params, |
| 3957 | size_t params_count) |
| 3958 | { |
| 3959 | const struct devlink_param *param = params; |
| 3960 | int i; |
| 3961 | int err; |
| 3962 | |
| 3963 | mutex_lock(&devlink->lock); |
| 3964 | for (i = 0; i < params_count; i++, param++) { |
| 3965 | if (!param || !param->name || !param->supported_cmodes) { |
| 3966 | err = -EINVAL; |
| 3967 | goto rollback; |
| 3968 | } |
| 3969 | if (param->generic) { |
| 3970 | err = devlink_param_generic_verify(param); |
| 3971 | if (err) |
| 3972 | goto rollback; |
| 3973 | } else { |
| 3974 | err = devlink_param_driver_verify(param); |
| 3975 | if (err) |
| 3976 | goto rollback; |
| 3977 | } |
| 3978 | err = devlink_param_register_one(devlink, param); |
| 3979 | if (err) |
| 3980 | goto rollback; |
| 3981 | } |
| 3982 | |
| 3983 | mutex_unlock(&devlink->lock); |
| 3984 | return 0; |
| 3985 | |
| 3986 | rollback: |
| 3987 | if (!i) |
| 3988 | goto unlock; |
| 3989 | for (param--; i > 0; i--, param--) |
| 3990 | devlink_param_unregister_one(devlink, param); |
| 3991 | unlock: |
| 3992 | mutex_unlock(&devlink->lock); |
| 3993 | return err; |
| 3994 | } |
| 3995 | EXPORT_SYMBOL_GPL(devlink_params_register); |
| 3996 | |
| 3997 | /** |
| 3998 | * devlink_params_unregister - unregister configuration parameters |
| 3999 | * @devlink: devlink |
| 4000 | * @params: configuration parameters to unregister |
| 4001 | * @params_count: number of parameters provided |
| 4002 | */ |
| 4003 | void devlink_params_unregister(struct devlink *devlink, |
| 4004 | const struct devlink_param *params, |
| 4005 | size_t params_count) |
| 4006 | { |
| 4007 | const struct devlink_param *param = params; |
| 4008 | int i; |
| 4009 | |
| 4010 | mutex_lock(&devlink->lock); |
| 4011 | for (i = 0; i < params_count; i++, param++) |
| 4012 | devlink_param_unregister_one(devlink, param); |
| 4013 | mutex_unlock(&devlink->lock); |
| 4014 | } |
| 4015 | EXPORT_SYMBOL_GPL(devlink_params_unregister); |
| 4016 | |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 4017 | /** |
| 4018 | * devlink_param_driverinit_value_get - get configuration parameter |
| 4019 | * value for driver initializing |
| 4020 | * |
| 4021 | * @devlink: devlink |
| 4022 | * @param_id: parameter ID |
| 4023 | * @init_val: value of parameter in driverinit configuration mode |
| 4024 | * |
| 4025 | * This function should be used by the driver to get driverinit |
| 4026 | * configuration for initialization after reload command. |
| 4027 | */ |
| 4028 | int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id, |
| 4029 | union devlink_param_value *init_val) |
| 4030 | { |
| 4031 | struct devlink_param_item *param_item; |
| 4032 | |
| 4033 | if (!devlink->ops || !devlink->ops->reload) |
| 4034 | return -EOPNOTSUPP; |
| 4035 | |
| 4036 | param_item = devlink_param_find_by_id(&devlink->param_list, param_id); |
| 4037 | if (!param_item) |
| 4038 | return -EINVAL; |
| 4039 | |
| 4040 | if (!param_item->driverinit_value_valid || |
| 4041 | !devlink_param_cmode_is_supported(param_item->param, |
| 4042 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 4043 | return -EOPNOTSUPP; |
| 4044 | |
| 4045 | *init_val = param_item->driverinit_value; |
| 4046 | |
| 4047 | return 0; |
| 4048 | } |
| 4049 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get); |
| 4050 | |
| 4051 | /** |
| 4052 | * devlink_param_driverinit_value_set - set value of configuration |
| 4053 | * parameter for driverinit |
| 4054 | * configuration mode |
| 4055 | * |
| 4056 | * @devlink: devlink |
| 4057 | * @param_id: parameter ID |
| 4058 | * @init_val: value of parameter to set for driverinit configuration mode |
| 4059 | * |
| 4060 | * This function should be used by the driver to set driverinit |
| 4061 | * configuration mode default value. |
| 4062 | */ |
| 4063 | int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id, |
| 4064 | union devlink_param_value init_val) |
| 4065 | { |
| 4066 | struct devlink_param_item *param_item; |
| 4067 | |
| 4068 | param_item = devlink_param_find_by_id(&devlink->param_list, param_id); |
| 4069 | if (!param_item) |
| 4070 | return -EINVAL; |
| 4071 | |
| 4072 | if (!devlink_param_cmode_is_supported(param_item->param, |
| 4073 | DEVLINK_PARAM_CMODE_DRIVERINIT)) |
| 4074 | return -EOPNOTSUPP; |
| 4075 | |
| 4076 | param_item->driverinit_value = init_val; |
| 4077 | param_item->driverinit_value_valid = true; |
| 4078 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 4079 | devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW); |
Moshe Shemesh | ec01aeb | 2018-07-04 14:30:31 +0300 | [diff] [blame] | 4080 | return 0; |
| 4081 | } |
| 4082 | EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set); |
| 4083 | |
Moshe Shemesh | ea601e1 | 2018-07-04 14:30:32 +0300 | [diff] [blame] | 4084 | /** |
| 4085 | * devlink_param_value_changed - notify devlink on a parameter's value |
| 4086 | * change. Should be called by the driver |
| 4087 | * right after the change. |
| 4088 | * |
| 4089 | * @devlink: devlink |
| 4090 | * @param_id: parameter ID |
| 4091 | * |
| 4092 | * This function should be used by the driver to notify devlink on value |
| 4093 | * change, excluding driverinit configuration mode. |
| 4094 | * For driverinit configuration mode driver should use the function |
| 4095 | * devlink_param_driverinit_value_set() instead. |
| 4096 | */ |
| 4097 | void devlink_param_value_changed(struct devlink *devlink, u32 param_id) |
| 4098 | { |
| 4099 | struct devlink_param_item *param_item; |
| 4100 | |
| 4101 | param_item = devlink_param_find_by_id(&devlink->param_list, param_id); |
| 4102 | WARN_ON(!param_item); |
| 4103 | |
| 4104 | devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW); |
| 4105 | } |
| 4106 | EXPORT_SYMBOL_GPL(devlink_param_value_changed); |
| 4107 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 4108 | static int __init devlink_module_init(void) |
| 4109 | { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 4110 | return genl_register_family(&devlink_nl_family); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | static void __exit devlink_module_exit(void) |
| 4114 | { |
| 4115 | genl_unregister_family(&devlink_nl_family); |
| 4116 | } |
| 4117 | |
| 4118 | module_init(devlink_module_init); |
| 4119 | module_exit(devlink_module_exit); |
| 4120 | |
| 4121 | MODULE_LICENSE("GPL v2"); |
| 4122 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); |
| 4123 | MODULE_DESCRIPTION("Network physical device Netlink interface"); |
| 4124 | MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME); |