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 | |
| 32 | EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 33 | |
| 34 | static LIST_HEAD(devlink_list); |
| 35 | |
| 36 | /* devlink_mutex |
| 37 | * |
| 38 | * An overall lock guarding every operation coming from userspace. |
| 39 | * It also guards devlink devices list and it is taken when |
| 40 | * driver registers/unregisters it. |
| 41 | */ |
| 42 | static DEFINE_MUTEX(devlink_mutex); |
| 43 | |
| 44 | /* devlink_port_mutex |
| 45 | * |
| 46 | * Shared lock to guard lists of ports in all devlink devices. |
| 47 | */ |
| 48 | static DEFINE_MUTEX(devlink_port_mutex); |
| 49 | |
| 50 | static struct net *devlink_net(const struct devlink *devlink) |
| 51 | { |
| 52 | return read_pnet(&devlink->_net); |
| 53 | } |
| 54 | |
| 55 | static void devlink_net_set(struct devlink *devlink, struct net *net) |
| 56 | { |
| 57 | write_pnet(&devlink->_net, net); |
| 58 | } |
| 59 | |
| 60 | static struct devlink *devlink_get_from_attrs(struct net *net, |
| 61 | struct nlattr **attrs) |
| 62 | { |
| 63 | struct devlink *devlink; |
| 64 | char *busname; |
| 65 | char *devname; |
| 66 | |
| 67 | if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME]) |
| 68 | return ERR_PTR(-EINVAL); |
| 69 | |
| 70 | busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]); |
| 71 | devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]); |
| 72 | |
| 73 | list_for_each_entry(devlink, &devlink_list, list) { |
| 74 | if (strcmp(devlink->dev->bus->name, busname) == 0 && |
| 75 | strcmp(dev_name(devlink->dev), devname) == 0 && |
| 76 | net_eq(devlink_net(devlink), net)) |
| 77 | return devlink; |
| 78 | } |
| 79 | |
| 80 | return ERR_PTR(-ENODEV); |
| 81 | } |
| 82 | |
| 83 | static struct devlink *devlink_get_from_info(struct genl_info *info) |
| 84 | { |
| 85 | return devlink_get_from_attrs(genl_info_net(info), info->attrs); |
| 86 | } |
| 87 | |
| 88 | static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, |
| 89 | int port_index) |
| 90 | { |
| 91 | struct devlink_port *devlink_port; |
| 92 | |
| 93 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 94 | if (devlink_port->index == port_index) |
| 95 | return devlink_port; |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | static bool devlink_port_index_exists(struct devlink *devlink, int port_index) |
| 101 | { |
| 102 | return devlink_port_get_by_index(devlink, port_index); |
| 103 | } |
| 104 | |
| 105 | static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, |
| 106 | struct nlattr **attrs) |
| 107 | { |
| 108 | if (attrs[DEVLINK_ATTR_PORT_INDEX]) { |
| 109 | u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 110 | struct devlink_port *devlink_port; |
| 111 | |
| 112 | devlink_port = devlink_port_get_by_index(devlink, port_index); |
| 113 | if (!devlink_port) |
| 114 | return ERR_PTR(-ENODEV); |
| 115 | return devlink_port; |
| 116 | } |
| 117 | return ERR_PTR(-EINVAL); |
| 118 | } |
| 119 | |
| 120 | static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink, |
| 121 | struct genl_info *info) |
| 122 | { |
| 123 | return devlink_port_get_from_attrs(devlink, info->attrs); |
| 124 | } |
| 125 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 126 | struct devlink_sb { |
| 127 | struct list_head list; |
| 128 | unsigned int index; |
| 129 | u32 size; |
| 130 | u16 ingress_pools_count; |
| 131 | u16 egress_pools_count; |
| 132 | u16 ingress_tc_count; |
| 133 | u16 egress_tc_count; |
| 134 | }; |
| 135 | |
| 136 | static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb) |
| 137 | { |
| 138 | return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count; |
| 139 | } |
| 140 | |
| 141 | static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink, |
| 142 | unsigned int sb_index) |
| 143 | { |
| 144 | struct devlink_sb *devlink_sb; |
| 145 | |
| 146 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 147 | if (devlink_sb->index == sb_index) |
| 148 | return devlink_sb; |
| 149 | } |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | static bool devlink_sb_index_exists(struct devlink *devlink, |
| 154 | unsigned int sb_index) |
| 155 | { |
| 156 | return devlink_sb_get_by_index(devlink, sb_index); |
| 157 | } |
| 158 | |
| 159 | static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink, |
| 160 | struct nlattr **attrs) |
| 161 | { |
| 162 | if (attrs[DEVLINK_ATTR_SB_INDEX]) { |
| 163 | u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]); |
| 164 | struct devlink_sb *devlink_sb; |
| 165 | |
| 166 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 167 | if (!devlink_sb) |
| 168 | return ERR_PTR(-ENODEV); |
| 169 | return devlink_sb; |
| 170 | } |
| 171 | return ERR_PTR(-EINVAL); |
| 172 | } |
| 173 | |
| 174 | static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink, |
| 175 | struct genl_info *info) |
| 176 | { |
| 177 | return devlink_sb_get_from_attrs(devlink, info->attrs); |
| 178 | } |
| 179 | |
| 180 | static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 181 | struct nlattr **attrs, |
| 182 | u16 *p_pool_index) |
| 183 | { |
| 184 | u16 val; |
| 185 | |
| 186 | if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX]) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]); |
| 190 | if (val >= devlink_sb_pool_count(devlink_sb)) |
| 191 | return -EINVAL; |
| 192 | *p_pool_index = val; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb, |
| 197 | struct genl_info *info, |
| 198 | u16 *p_pool_index) |
| 199 | { |
| 200 | return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs, |
| 201 | p_pool_index); |
| 202 | } |
| 203 | |
| 204 | static int |
| 205 | devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs, |
| 206 | enum devlink_sb_pool_type *p_pool_type) |
| 207 | { |
| 208 | u8 val; |
| 209 | |
| 210 | if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE]) |
| 211 | return -EINVAL; |
| 212 | |
| 213 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]); |
| 214 | if (val != DEVLINK_SB_POOL_TYPE_INGRESS && |
| 215 | val != DEVLINK_SB_POOL_TYPE_EGRESS) |
| 216 | return -EINVAL; |
| 217 | *p_pool_type = val; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int |
| 222 | devlink_sb_pool_type_get_from_info(struct genl_info *info, |
| 223 | enum devlink_sb_pool_type *p_pool_type) |
| 224 | { |
| 225 | return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type); |
| 226 | } |
| 227 | |
| 228 | static int |
| 229 | devlink_sb_th_type_get_from_attrs(struct nlattr **attrs, |
| 230 | enum devlink_sb_threshold_type *p_th_type) |
| 231 | { |
| 232 | u8 val; |
| 233 | |
| 234 | if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]); |
| 238 | if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC && |
| 239 | val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC) |
| 240 | return -EINVAL; |
| 241 | *p_th_type = val; |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int |
| 246 | devlink_sb_th_type_get_from_info(struct genl_info *info, |
| 247 | enum devlink_sb_threshold_type *p_th_type) |
| 248 | { |
| 249 | return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type); |
| 250 | } |
| 251 | |
| 252 | static int |
| 253 | devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb, |
| 254 | struct nlattr **attrs, |
| 255 | enum devlink_sb_pool_type pool_type, |
| 256 | u16 *p_tc_index) |
| 257 | { |
| 258 | u16 val; |
| 259 | |
| 260 | if (!attrs[DEVLINK_ATTR_SB_TC_INDEX]) |
| 261 | return -EINVAL; |
| 262 | |
| 263 | val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]); |
| 264 | if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS && |
| 265 | val >= devlink_sb->ingress_tc_count) |
| 266 | return -EINVAL; |
| 267 | if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS && |
| 268 | val >= devlink_sb->egress_tc_count) |
| 269 | return -EINVAL; |
| 270 | *p_tc_index = val; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static int |
| 275 | devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb, |
| 276 | struct genl_info *info, |
| 277 | enum devlink_sb_pool_type pool_type, |
| 278 | u16 *p_tc_index) |
| 279 | { |
| 280 | return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs, |
| 281 | pool_type, p_tc_index); |
| 282 | } |
| 283 | |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 284 | #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0) |
| 285 | #define DEVLINK_NL_FLAG_NEED_PORT BIT(1) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 286 | #define DEVLINK_NL_FLAG_NEED_SB BIT(2) |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 287 | #define DEVLINK_NL_FLAG_LOCK_PORTS BIT(3) |
| 288 | /* port is not needed but we need to ensure they don't |
| 289 | * change in the middle of command |
| 290 | */ |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 291 | |
| 292 | static int devlink_nl_pre_doit(const struct genl_ops *ops, |
| 293 | struct sk_buff *skb, struct genl_info *info) |
| 294 | { |
| 295 | struct devlink *devlink; |
| 296 | |
| 297 | mutex_lock(&devlink_mutex); |
| 298 | devlink = devlink_get_from_info(info); |
| 299 | if (IS_ERR(devlink)) { |
| 300 | mutex_unlock(&devlink_mutex); |
| 301 | return PTR_ERR(devlink); |
| 302 | } |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 303 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) { |
| 304 | info->user_ptr[0] = devlink; |
| 305 | } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 306 | struct devlink_port *devlink_port; |
| 307 | |
| 308 | mutex_lock(&devlink_port_mutex); |
| 309 | devlink_port = devlink_port_get_from_info(devlink, info); |
| 310 | if (IS_ERR(devlink_port)) { |
| 311 | mutex_unlock(&devlink_port_mutex); |
| 312 | mutex_unlock(&devlink_mutex); |
| 313 | return PTR_ERR(devlink_port); |
| 314 | } |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 315 | info->user_ptr[0] = devlink_port; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 316 | } |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 317 | if (ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS) { |
| 318 | mutex_lock(&devlink_port_mutex); |
| 319 | } |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 320 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) { |
| 321 | struct devlink_sb *devlink_sb; |
| 322 | |
| 323 | devlink_sb = devlink_sb_get_from_info(devlink, info); |
| 324 | if (IS_ERR(devlink_sb)) { |
| 325 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) |
| 326 | mutex_unlock(&devlink_port_mutex); |
| 327 | mutex_unlock(&devlink_mutex); |
| 328 | return PTR_ERR(devlink_sb); |
| 329 | } |
| 330 | info->user_ptr[1] = devlink_sb; |
| 331 | } |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static void devlink_nl_post_doit(const struct genl_ops *ops, |
| 336 | struct sk_buff *skb, struct genl_info *info) |
| 337 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 338 | if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT || |
| 339 | ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS) |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 340 | mutex_unlock(&devlink_port_mutex); |
| 341 | mutex_unlock(&devlink_mutex); |
| 342 | } |
| 343 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 344 | static struct genl_family devlink_nl_family; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 345 | |
| 346 | enum devlink_multicast_groups { |
| 347 | DEVLINK_MCGRP_CONFIG, |
| 348 | }; |
| 349 | |
| 350 | static const struct genl_multicast_group devlink_nl_mcgrps[] = { |
| 351 | [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, |
| 352 | }; |
| 353 | |
| 354 | static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) |
| 355 | { |
| 356 | if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name)) |
| 357 | return -EMSGSIZE; |
| 358 | if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev))) |
| 359 | return -EMSGSIZE; |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink, |
| 364 | enum devlink_command cmd, u32 portid, |
| 365 | u32 seq, int flags) |
| 366 | { |
| 367 | void *hdr; |
| 368 | |
| 369 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 370 | if (!hdr) |
| 371 | return -EMSGSIZE; |
| 372 | |
| 373 | if (devlink_nl_put_handle(msg, devlink)) |
| 374 | goto nla_put_failure; |
| 375 | |
| 376 | genlmsg_end(msg, hdr); |
| 377 | return 0; |
| 378 | |
| 379 | nla_put_failure: |
| 380 | genlmsg_cancel(msg, hdr); |
| 381 | return -EMSGSIZE; |
| 382 | } |
| 383 | |
| 384 | static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) |
| 385 | { |
| 386 | struct sk_buff *msg; |
| 387 | int err; |
| 388 | |
| 389 | WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); |
| 390 | |
| 391 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 392 | if (!msg) |
| 393 | return; |
| 394 | |
| 395 | err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0); |
| 396 | if (err) { |
| 397 | nlmsg_free(msg); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 402 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 403 | } |
| 404 | |
| 405 | static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, |
| 406 | struct devlink_port *devlink_port, |
| 407 | enum devlink_command cmd, u32 portid, |
| 408 | u32 seq, int flags) |
| 409 | { |
| 410 | void *hdr; |
| 411 | |
| 412 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 413 | if (!hdr) |
| 414 | return -EMSGSIZE; |
| 415 | |
| 416 | if (devlink_nl_put_handle(msg, devlink)) |
| 417 | goto nla_put_failure; |
| 418 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 419 | goto nla_put_failure; |
| 420 | if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) |
| 421 | goto nla_put_failure; |
| 422 | if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET && |
| 423 | nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE, |
| 424 | devlink_port->desired_type)) |
| 425 | goto nla_put_failure; |
| 426 | if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { |
| 427 | struct net_device *netdev = devlink_port->type_dev; |
| 428 | |
| 429 | if (netdev && |
| 430 | (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, |
| 431 | netdev->ifindex) || |
| 432 | nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, |
| 433 | netdev->name))) |
| 434 | goto nla_put_failure; |
| 435 | } |
| 436 | if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { |
| 437 | struct ib_device *ibdev = devlink_port->type_dev; |
| 438 | |
| 439 | if (ibdev && |
| 440 | nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME, |
| 441 | ibdev->name)) |
| 442 | goto nla_put_failure; |
| 443 | } |
| 444 | if (devlink_port->split && |
| 445 | nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, |
| 446 | devlink_port->split_group)) |
| 447 | goto nla_put_failure; |
| 448 | |
| 449 | genlmsg_end(msg, hdr); |
| 450 | return 0; |
| 451 | |
| 452 | nla_put_failure: |
| 453 | genlmsg_cancel(msg, hdr); |
| 454 | return -EMSGSIZE; |
| 455 | } |
| 456 | |
| 457 | static void devlink_port_notify(struct devlink_port *devlink_port, |
| 458 | enum devlink_command cmd) |
| 459 | { |
| 460 | struct devlink *devlink = devlink_port->devlink; |
| 461 | struct sk_buff *msg; |
| 462 | int err; |
| 463 | |
| 464 | if (!devlink_port->registered) |
| 465 | return; |
| 466 | |
| 467 | WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); |
| 468 | |
| 469 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 470 | if (!msg) |
| 471 | return; |
| 472 | |
| 473 | err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0); |
| 474 | if (err) { |
| 475 | nlmsg_free(msg); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), |
| 480 | msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); |
| 481 | } |
| 482 | |
| 483 | static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info) |
| 484 | { |
| 485 | struct devlink *devlink = info->user_ptr[0]; |
| 486 | struct sk_buff *msg; |
| 487 | int err; |
| 488 | |
| 489 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 490 | if (!msg) |
| 491 | return -ENOMEM; |
| 492 | |
| 493 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 494 | info->snd_portid, info->snd_seq, 0); |
| 495 | if (err) { |
| 496 | nlmsg_free(msg); |
| 497 | return err; |
| 498 | } |
| 499 | |
| 500 | return genlmsg_reply(msg, info); |
| 501 | } |
| 502 | |
| 503 | static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg, |
| 504 | struct netlink_callback *cb) |
| 505 | { |
| 506 | struct devlink *devlink; |
| 507 | int start = cb->args[0]; |
| 508 | int idx = 0; |
| 509 | int err; |
| 510 | |
| 511 | mutex_lock(&devlink_mutex); |
| 512 | list_for_each_entry(devlink, &devlink_list, list) { |
| 513 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 514 | continue; |
| 515 | if (idx < start) { |
| 516 | idx++; |
| 517 | continue; |
| 518 | } |
| 519 | err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW, |
| 520 | NETLINK_CB(cb->skb).portid, |
| 521 | cb->nlh->nlmsg_seq, NLM_F_MULTI); |
| 522 | if (err) |
| 523 | goto out; |
| 524 | idx++; |
| 525 | } |
| 526 | out: |
| 527 | mutex_unlock(&devlink_mutex); |
| 528 | |
| 529 | cb->args[0] = idx; |
| 530 | return msg->len; |
| 531 | } |
| 532 | |
| 533 | static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb, |
| 534 | struct genl_info *info) |
| 535 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 536 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 537 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 538 | struct sk_buff *msg; |
| 539 | int err; |
| 540 | |
| 541 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 542 | if (!msg) |
| 543 | return -ENOMEM; |
| 544 | |
| 545 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 546 | DEVLINK_CMD_PORT_NEW, |
| 547 | info->snd_portid, info->snd_seq, 0); |
| 548 | if (err) { |
| 549 | nlmsg_free(msg); |
| 550 | return err; |
| 551 | } |
| 552 | |
| 553 | return genlmsg_reply(msg, info); |
| 554 | } |
| 555 | |
| 556 | static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg, |
| 557 | struct netlink_callback *cb) |
| 558 | { |
| 559 | struct devlink *devlink; |
| 560 | struct devlink_port *devlink_port; |
| 561 | int start = cb->args[0]; |
| 562 | int idx = 0; |
| 563 | int err; |
| 564 | |
| 565 | mutex_lock(&devlink_mutex); |
| 566 | mutex_lock(&devlink_port_mutex); |
| 567 | list_for_each_entry(devlink, &devlink_list, list) { |
| 568 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 569 | continue; |
| 570 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 571 | if (idx < start) { |
| 572 | idx++; |
| 573 | continue; |
| 574 | } |
| 575 | err = devlink_nl_port_fill(msg, devlink, devlink_port, |
| 576 | DEVLINK_CMD_NEW, |
| 577 | NETLINK_CB(cb->skb).portid, |
| 578 | cb->nlh->nlmsg_seq, |
| 579 | NLM_F_MULTI); |
| 580 | if (err) |
| 581 | goto out; |
| 582 | idx++; |
| 583 | } |
| 584 | } |
| 585 | out: |
| 586 | mutex_unlock(&devlink_port_mutex); |
| 587 | mutex_unlock(&devlink_mutex); |
| 588 | |
| 589 | cb->args[0] = idx; |
| 590 | return msg->len; |
| 591 | } |
| 592 | |
| 593 | static int devlink_port_type_set(struct devlink *devlink, |
| 594 | struct devlink_port *devlink_port, |
| 595 | enum devlink_port_type port_type) |
| 596 | |
| 597 | { |
| 598 | int err; |
| 599 | |
| 600 | if (devlink->ops && devlink->ops->port_type_set) { |
| 601 | if (port_type == DEVLINK_PORT_TYPE_NOTSET) |
| 602 | return -EINVAL; |
Elad Raz | 6edf101 | 2016-10-23 17:43:05 +0200 | [diff] [blame] | 603 | if (port_type == devlink_port->type) |
| 604 | return 0; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 605 | err = devlink->ops->port_type_set(devlink_port, port_type); |
| 606 | if (err) |
| 607 | return err; |
| 608 | devlink_port->desired_type = port_type; |
| 609 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 610 | return 0; |
| 611 | } |
| 612 | return -EOPNOTSUPP; |
| 613 | } |
| 614 | |
| 615 | static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, |
| 616 | struct genl_info *info) |
| 617 | { |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 618 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 619 | struct devlink *devlink = devlink_port->devlink; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 620 | int err; |
| 621 | |
| 622 | if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) { |
| 623 | enum devlink_port_type port_type; |
| 624 | |
| 625 | port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]); |
| 626 | err = devlink_port_type_set(devlink, devlink_port, port_type); |
| 627 | if (err) |
| 628 | return err; |
| 629 | } |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | static int devlink_port_split(struct devlink *devlink, |
| 634 | u32 port_index, u32 count) |
| 635 | |
| 636 | { |
| 637 | if (devlink->ops && devlink->ops->port_split) |
| 638 | return devlink->ops->port_split(devlink, port_index, count); |
| 639 | return -EOPNOTSUPP; |
| 640 | } |
| 641 | |
| 642 | static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, |
| 643 | struct genl_info *info) |
| 644 | { |
| 645 | struct devlink *devlink = info->user_ptr[0]; |
| 646 | u32 port_index; |
| 647 | u32 count; |
| 648 | |
| 649 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] || |
| 650 | !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]) |
| 651 | return -EINVAL; |
| 652 | |
| 653 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 654 | count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]); |
| 655 | return devlink_port_split(devlink, port_index, count); |
| 656 | } |
| 657 | |
| 658 | static int devlink_port_unsplit(struct devlink *devlink, u32 port_index) |
| 659 | |
| 660 | { |
| 661 | if (devlink->ops && devlink->ops->port_unsplit) |
| 662 | return devlink->ops->port_unsplit(devlink, port_index); |
| 663 | return -EOPNOTSUPP; |
| 664 | } |
| 665 | |
| 666 | static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb, |
| 667 | struct genl_info *info) |
| 668 | { |
| 669 | struct devlink *devlink = info->user_ptr[0]; |
| 670 | u32 port_index; |
| 671 | |
| 672 | if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) |
| 673 | return -EINVAL; |
| 674 | |
| 675 | port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); |
| 676 | return devlink_port_unsplit(devlink, port_index); |
| 677 | } |
| 678 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 679 | static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink, |
| 680 | struct devlink_sb *devlink_sb, |
| 681 | enum devlink_command cmd, u32 portid, |
| 682 | u32 seq, int flags) |
| 683 | { |
| 684 | void *hdr; |
| 685 | |
| 686 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 687 | if (!hdr) |
| 688 | return -EMSGSIZE; |
| 689 | |
| 690 | if (devlink_nl_put_handle(msg, devlink)) |
| 691 | goto nla_put_failure; |
| 692 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 693 | goto nla_put_failure; |
| 694 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size)) |
| 695 | goto nla_put_failure; |
| 696 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT, |
| 697 | devlink_sb->ingress_pools_count)) |
| 698 | goto nla_put_failure; |
| 699 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT, |
| 700 | devlink_sb->egress_pools_count)) |
| 701 | goto nla_put_failure; |
| 702 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT, |
| 703 | devlink_sb->ingress_tc_count)) |
| 704 | goto nla_put_failure; |
| 705 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT, |
| 706 | devlink_sb->egress_tc_count)) |
| 707 | goto nla_put_failure; |
| 708 | |
| 709 | genlmsg_end(msg, hdr); |
| 710 | return 0; |
| 711 | |
| 712 | nla_put_failure: |
| 713 | genlmsg_cancel(msg, hdr); |
| 714 | return -EMSGSIZE; |
| 715 | } |
| 716 | |
| 717 | static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb, |
| 718 | struct genl_info *info) |
| 719 | { |
| 720 | struct devlink *devlink = info->user_ptr[0]; |
| 721 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 722 | struct sk_buff *msg; |
| 723 | int err; |
| 724 | |
| 725 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 726 | if (!msg) |
| 727 | return -ENOMEM; |
| 728 | |
| 729 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 730 | DEVLINK_CMD_SB_NEW, |
| 731 | info->snd_portid, info->snd_seq, 0); |
| 732 | if (err) { |
| 733 | nlmsg_free(msg); |
| 734 | return err; |
| 735 | } |
| 736 | |
| 737 | return genlmsg_reply(msg, info); |
| 738 | } |
| 739 | |
| 740 | static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg, |
| 741 | struct netlink_callback *cb) |
| 742 | { |
| 743 | struct devlink *devlink; |
| 744 | struct devlink_sb *devlink_sb; |
| 745 | int start = cb->args[0]; |
| 746 | int idx = 0; |
| 747 | int err; |
| 748 | |
| 749 | mutex_lock(&devlink_mutex); |
| 750 | list_for_each_entry(devlink, &devlink_list, list) { |
| 751 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) |
| 752 | continue; |
| 753 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 754 | if (idx < start) { |
| 755 | idx++; |
| 756 | continue; |
| 757 | } |
| 758 | err = devlink_nl_sb_fill(msg, devlink, devlink_sb, |
| 759 | DEVLINK_CMD_SB_NEW, |
| 760 | NETLINK_CB(cb->skb).portid, |
| 761 | cb->nlh->nlmsg_seq, |
| 762 | NLM_F_MULTI); |
| 763 | if (err) |
| 764 | goto out; |
| 765 | idx++; |
| 766 | } |
| 767 | } |
| 768 | out: |
| 769 | mutex_unlock(&devlink_mutex); |
| 770 | |
| 771 | cb->args[0] = idx; |
| 772 | return msg->len; |
| 773 | } |
| 774 | |
| 775 | static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink, |
| 776 | struct devlink_sb *devlink_sb, |
| 777 | u16 pool_index, enum devlink_command cmd, |
| 778 | u32 portid, u32 seq, int flags) |
| 779 | { |
| 780 | struct devlink_sb_pool_info pool_info; |
| 781 | void *hdr; |
| 782 | int err; |
| 783 | |
| 784 | err = devlink->ops->sb_pool_get(devlink, devlink_sb->index, |
| 785 | pool_index, &pool_info); |
| 786 | if (err) |
| 787 | return err; |
| 788 | |
| 789 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 790 | if (!hdr) |
| 791 | return -EMSGSIZE; |
| 792 | |
| 793 | if (devlink_nl_put_handle(msg, devlink)) |
| 794 | goto nla_put_failure; |
| 795 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 796 | goto nla_put_failure; |
| 797 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 798 | goto nla_put_failure; |
| 799 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type)) |
| 800 | goto nla_put_failure; |
| 801 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size)) |
| 802 | goto nla_put_failure; |
| 803 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE, |
| 804 | pool_info.threshold_type)) |
| 805 | goto nla_put_failure; |
| 806 | |
| 807 | genlmsg_end(msg, hdr); |
| 808 | return 0; |
| 809 | |
| 810 | nla_put_failure: |
| 811 | genlmsg_cancel(msg, hdr); |
| 812 | return -EMSGSIZE; |
| 813 | } |
| 814 | |
| 815 | static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb, |
| 816 | struct genl_info *info) |
| 817 | { |
| 818 | struct devlink *devlink = info->user_ptr[0]; |
| 819 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 820 | struct sk_buff *msg; |
| 821 | u16 pool_index; |
| 822 | int err; |
| 823 | |
| 824 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 825 | &pool_index); |
| 826 | if (err) |
| 827 | return err; |
| 828 | |
| 829 | if (!devlink->ops || !devlink->ops->sb_pool_get) |
| 830 | return -EOPNOTSUPP; |
| 831 | |
| 832 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 833 | if (!msg) |
| 834 | return -ENOMEM; |
| 835 | |
| 836 | err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index, |
| 837 | DEVLINK_CMD_SB_POOL_NEW, |
| 838 | info->snd_portid, info->snd_seq, 0); |
| 839 | if (err) { |
| 840 | nlmsg_free(msg); |
| 841 | return err; |
| 842 | } |
| 843 | |
| 844 | return genlmsg_reply(msg, info); |
| 845 | } |
| 846 | |
| 847 | static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 848 | struct devlink *devlink, |
| 849 | struct devlink_sb *devlink_sb, |
| 850 | u32 portid, u32 seq) |
| 851 | { |
| 852 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 853 | u16 pool_index; |
| 854 | int err; |
| 855 | |
| 856 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 857 | if (*p_idx < start) { |
| 858 | (*p_idx)++; |
| 859 | continue; |
| 860 | } |
| 861 | err = devlink_nl_sb_pool_fill(msg, devlink, |
| 862 | devlink_sb, |
| 863 | pool_index, |
| 864 | DEVLINK_CMD_SB_POOL_NEW, |
| 865 | portid, seq, NLM_F_MULTI); |
| 866 | if (err) |
| 867 | return err; |
| 868 | (*p_idx)++; |
| 869 | } |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg, |
| 874 | struct netlink_callback *cb) |
| 875 | { |
| 876 | struct devlink *devlink; |
| 877 | struct devlink_sb *devlink_sb; |
| 878 | int start = cb->args[0]; |
| 879 | int idx = 0; |
| 880 | int err; |
| 881 | |
| 882 | mutex_lock(&devlink_mutex); |
| 883 | list_for_each_entry(devlink, &devlink_list, list) { |
| 884 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
| 885 | !devlink->ops || !devlink->ops->sb_pool_get) |
| 886 | continue; |
| 887 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 888 | err = __sb_pool_get_dumpit(msg, start, &idx, devlink, |
| 889 | devlink_sb, |
| 890 | NETLINK_CB(cb->skb).portid, |
| 891 | cb->nlh->nlmsg_seq); |
| 892 | if (err && err != -EOPNOTSUPP) |
| 893 | goto out; |
| 894 | } |
| 895 | } |
| 896 | out: |
| 897 | mutex_unlock(&devlink_mutex); |
| 898 | |
| 899 | cb->args[0] = idx; |
| 900 | return msg->len; |
| 901 | } |
| 902 | |
| 903 | static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index, |
| 904 | u16 pool_index, u32 size, |
| 905 | enum devlink_sb_threshold_type threshold_type) |
| 906 | |
| 907 | { |
| 908 | const struct devlink_ops *ops = devlink->ops; |
| 909 | |
| 910 | if (ops && ops->sb_pool_set) |
| 911 | return ops->sb_pool_set(devlink, sb_index, pool_index, |
| 912 | size, threshold_type); |
| 913 | return -EOPNOTSUPP; |
| 914 | } |
| 915 | |
| 916 | static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb, |
| 917 | struct genl_info *info) |
| 918 | { |
| 919 | struct devlink *devlink = info->user_ptr[0]; |
| 920 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 921 | enum devlink_sb_threshold_type threshold_type; |
| 922 | u16 pool_index; |
| 923 | u32 size; |
| 924 | int err; |
| 925 | |
| 926 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 927 | &pool_index); |
| 928 | if (err) |
| 929 | return err; |
| 930 | |
| 931 | err = devlink_sb_th_type_get_from_info(info, &threshold_type); |
| 932 | if (err) |
| 933 | return err; |
| 934 | |
| 935 | if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]) |
| 936 | return -EINVAL; |
| 937 | |
| 938 | size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]); |
| 939 | return devlink_sb_pool_set(devlink, devlink_sb->index, |
| 940 | pool_index, size, threshold_type); |
| 941 | } |
| 942 | |
| 943 | static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg, |
| 944 | struct devlink *devlink, |
| 945 | struct devlink_port *devlink_port, |
| 946 | struct devlink_sb *devlink_sb, |
| 947 | u16 pool_index, |
| 948 | enum devlink_command cmd, |
| 949 | u32 portid, u32 seq, int flags) |
| 950 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 951 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 952 | u32 threshold; |
| 953 | void *hdr; |
| 954 | int err; |
| 955 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 956 | err = ops->sb_port_pool_get(devlink_port, devlink_sb->index, |
| 957 | pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 958 | if (err) |
| 959 | return err; |
| 960 | |
| 961 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 962 | if (!hdr) |
| 963 | return -EMSGSIZE; |
| 964 | |
| 965 | if (devlink_nl_put_handle(msg, devlink)) |
| 966 | goto nla_put_failure; |
| 967 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 968 | goto nla_put_failure; |
| 969 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 970 | goto nla_put_failure; |
| 971 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 972 | goto nla_put_failure; |
| 973 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 974 | goto nla_put_failure; |
| 975 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 976 | if (ops->sb_occ_port_pool_get) { |
| 977 | u32 cur; |
| 978 | u32 max; |
| 979 | |
| 980 | err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index, |
| 981 | pool_index, &cur, &max); |
| 982 | if (err && err != -EOPNOTSUPP) |
| 983 | return err; |
| 984 | if (!err) { |
| 985 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 986 | goto nla_put_failure; |
| 987 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 988 | goto nla_put_failure; |
| 989 | } |
| 990 | } |
| 991 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 992 | genlmsg_end(msg, hdr); |
| 993 | return 0; |
| 994 | |
| 995 | nla_put_failure: |
| 996 | genlmsg_cancel(msg, hdr); |
| 997 | return -EMSGSIZE; |
| 998 | } |
| 999 | |
| 1000 | static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb, |
| 1001 | struct genl_info *info) |
| 1002 | { |
| 1003 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1004 | struct devlink *devlink = devlink_port->devlink; |
| 1005 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1006 | struct sk_buff *msg; |
| 1007 | u16 pool_index; |
| 1008 | int err; |
| 1009 | |
| 1010 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1011 | &pool_index); |
| 1012 | if (err) |
| 1013 | return err; |
| 1014 | |
| 1015 | if (!devlink->ops || !devlink->ops->sb_port_pool_get) |
| 1016 | return -EOPNOTSUPP; |
| 1017 | |
| 1018 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1019 | if (!msg) |
| 1020 | return -ENOMEM; |
| 1021 | |
| 1022 | err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port, |
| 1023 | devlink_sb, pool_index, |
| 1024 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1025 | info->snd_portid, info->snd_seq, 0); |
| 1026 | if (err) { |
| 1027 | nlmsg_free(msg); |
| 1028 | return err; |
| 1029 | } |
| 1030 | |
| 1031 | return genlmsg_reply(msg, info); |
| 1032 | } |
| 1033 | |
| 1034 | static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx, |
| 1035 | struct devlink *devlink, |
| 1036 | struct devlink_sb *devlink_sb, |
| 1037 | u32 portid, u32 seq) |
| 1038 | { |
| 1039 | struct devlink_port *devlink_port; |
| 1040 | u16 pool_count = devlink_sb_pool_count(devlink_sb); |
| 1041 | u16 pool_index; |
| 1042 | int err; |
| 1043 | |
| 1044 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1045 | for (pool_index = 0; pool_index < pool_count; pool_index++) { |
| 1046 | if (*p_idx < start) { |
| 1047 | (*p_idx)++; |
| 1048 | continue; |
| 1049 | } |
| 1050 | err = devlink_nl_sb_port_pool_fill(msg, devlink, |
| 1051 | devlink_port, |
| 1052 | devlink_sb, |
| 1053 | pool_index, |
| 1054 | DEVLINK_CMD_SB_PORT_POOL_NEW, |
| 1055 | portid, seq, |
| 1056 | NLM_F_MULTI); |
| 1057 | if (err) |
| 1058 | return err; |
| 1059 | (*p_idx)++; |
| 1060 | } |
| 1061 | } |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg, |
| 1066 | struct netlink_callback *cb) |
| 1067 | { |
| 1068 | struct devlink *devlink; |
| 1069 | struct devlink_sb *devlink_sb; |
| 1070 | int start = cb->args[0]; |
| 1071 | int idx = 0; |
| 1072 | int err; |
| 1073 | |
| 1074 | mutex_lock(&devlink_mutex); |
| 1075 | mutex_lock(&devlink_port_mutex); |
| 1076 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1077 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
| 1078 | !devlink->ops || !devlink->ops->sb_port_pool_get) |
| 1079 | continue; |
| 1080 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1081 | err = __sb_port_pool_get_dumpit(msg, start, &idx, |
| 1082 | devlink, devlink_sb, |
| 1083 | NETLINK_CB(cb->skb).portid, |
| 1084 | cb->nlh->nlmsg_seq); |
| 1085 | if (err && err != -EOPNOTSUPP) |
| 1086 | goto out; |
| 1087 | } |
| 1088 | } |
| 1089 | out: |
| 1090 | mutex_unlock(&devlink_port_mutex); |
| 1091 | mutex_unlock(&devlink_mutex); |
| 1092 | |
| 1093 | cb->args[0] = idx; |
| 1094 | return msg->len; |
| 1095 | } |
| 1096 | |
| 1097 | static int devlink_sb_port_pool_set(struct devlink_port *devlink_port, |
| 1098 | unsigned int sb_index, u16 pool_index, |
| 1099 | u32 threshold) |
| 1100 | |
| 1101 | { |
| 1102 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1103 | |
| 1104 | if (ops && ops->sb_port_pool_set) |
| 1105 | return ops->sb_port_pool_set(devlink_port, sb_index, |
| 1106 | pool_index, threshold); |
| 1107 | return -EOPNOTSUPP; |
| 1108 | } |
| 1109 | |
| 1110 | static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb, |
| 1111 | struct genl_info *info) |
| 1112 | { |
| 1113 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1114 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1115 | u16 pool_index; |
| 1116 | u32 threshold; |
| 1117 | int err; |
| 1118 | |
| 1119 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1120 | &pool_index); |
| 1121 | if (err) |
| 1122 | return err; |
| 1123 | |
| 1124 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1125 | return -EINVAL; |
| 1126 | |
| 1127 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1128 | return devlink_sb_port_pool_set(devlink_port, devlink_sb->index, |
| 1129 | pool_index, threshold); |
| 1130 | } |
| 1131 | |
| 1132 | static int |
| 1133 | devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1134 | struct devlink_port *devlink_port, |
| 1135 | struct devlink_sb *devlink_sb, u16 tc_index, |
| 1136 | enum devlink_sb_pool_type pool_type, |
| 1137 | enum devlink_command cmd, |
| 1138 | u32 portid, u32 seq, int flags) |
| 1139 | { |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1140 | const struct devlink_ops *ops = devlink->ops; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1141 | u16 pool_index; |
| 1142 | u32 threshold; |
| 1143 | void *hdr; |
| 1144 | int err; |
| 1145 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1146 | err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index, |
| 1147 | tc_index, pool_type, |
| 1148 | &pool_index, &threshold); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1149 | if (err) |
| 1150 | return err; |
| 1151 | |
| 1152 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1153 | if (!hdr) |
| 1154 | return -EMSGSIZE; |
| 1155 | |
| 1156 | if (devlink_nl_put_handle(msg, devlink)) |
| 1157 | goto nla_put_failure; |
| 1158 | if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) |
| 1159 | goto nla_put_failure; |
| 1160 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index)) |
| 1161 | goto nla_put_failure; |
| 1162 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index)) |
| 1163 | goto nla_put_failure; |
| 1164 | if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type)) |
| 1165 | goto nla_put_failure; |
| 1166 | if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index)) |
| 1167 | goto nla_put_failure; |
| 1168 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold)) |
| 1169 | goto nla_put_failure; |
| 1170 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1171 | if (ops->sb_occ_tc_port_bind_get) { |
| 1172 | u32 cur; |
| 1173 | u32 max; |
| 1174 | |
| 1175 | err = ops->sb_occ_tc_port_bind_get(devlink_port, |
| 1176 | devlink_sb->index, |
| 1177 | tc_index, pool_type, |
| 1178 | &cur, &max); |
| 1179 | if (err && err != -EOPNOTSUPP) |
| 1180 | return err; |
| 1181 | if (!err) { |
| 1182 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur)) |
| 1183 | goto nla_put_failure; |
| 1184 | if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max)) |
| 1185 | goto nla_put_failure; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 1189 | genlmsg_end(msg, hdr); |
| 1190 | return 0; |
| 1191 | |
| 1192 | nla_put_failure: |
| 1193 | genlmsg_cancel(msg, hdr); |
| 1194 | return -EMSGSIZE; |
| 1195 | } |
| 1196 | |
| 1197 | static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb, |
| 1198 | struct genl_info *info) |
| 1199 | { |
| 1200 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1201 | struct devlink *devlink = devlink_port->devlink; |
| 1202 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1203 | struct sk_buff *msg; |
| 1204 | enum devlink_sb_pool_type pool_type; |
| 1205 | u16 tc_index; |
| 1206 | int err; |
| 1207 | |
| 1208 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1209 | if (err) |
| 1210 | return err; |
| 1211 | |
| 1212 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1213 | pool_type, &tc_index); |
| 1214 | if (err) |
| 1215 | return err; |
| 1216 | |
| 1217 | if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get) |
| 1218 | return -EOPNOTSUPP; |
| 1219 | |
| 1220 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1221 | if (!msg) |
| 1222 | return -ENOMEM; |
| 1223 | |
| 1224 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port, |
| 1225 | devlink_sb, tc_index, pool_type, |
| 1226 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1227 | info->snd_portid, |
| 1228 | info->snd_seq, 0); |
| 1229 | if (err) { |
| 1230 | nlmsg_free(msg); |
| 1231 | return err; |
| 1232 | } |
| 1233 | |
| 1234 | return genlmsg_reply(msg, info); |
| 1235 | } |
| 1236 | |
| 1237 | static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1238 | int start, int *p_idx, |
| 1239 | struct devlink *devlink, |
| 1240 | struct devlink_sb *devlink_sb, |
| 1241 | u32 portid, u32 seq) |
| 1242 | { |
| 1243 | struct devlink_port *devlink_port; |
| 1244 | u16 tc_index; |
| 1245 | int err; |
| 1246 | |
| 1247 | list_for_each_entry(devlink_port, &devlink->port_list, list) { |
| 1248 | for (tc_index = 0; |
| 1249 | tc_index < devlink_sb->ingress_tc_count; tc_index++) { |
| 1250 | if (*p_idx < start) { |
| 1251 | (*p_idx)++; |
| 1252 | continue; |
| 1253 | } |
| 1254 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1255 | devlink_port, |
| 1256 | devlink_sb, |
| 1257 | tc_index, |
| 1258 | DEVLINK_SB_POOL_TYPE_INGRESS, |
| 1259 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1260 | portid, seq, |
| 1261 | NLM_F_MULTI); |
| 1262 | if (err) |
| 1263 | return err; |
| 1264 | (*p_idx)++; |
| 1265 | } |
| 1266 | for (tc_index = 0; |
| 1267 | tc_index < devlink_sb->egress_tc_count; tc_index++) { |
| 1268 | if (*p_idx < start) { |
| 1269 | (*p_idx)++; |
| 1270 | continue; |
| 1271 | } |
| 1272 | err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, |
| 1273 | devlink_port, |
| 1274 | devlink_sb, |
| 1275 | tc_index, |
| 1276 | DEVLINK_SB_POOL_TYPE_EGRESS, |
| 1277 | DEVLINK_CMD_SB_TC_POOL_BIND_NEW, |
| 1278 | portid, seq, |
| 1279 | NLM_F_MULTI); |
| 1280 | if (err) |
| 1281 | return err; |
| 1282 | (*p_idx)++; |
| 1283 | } |
| 1284 | } |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | static int |
| 1289 | devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg, |
| 1290 | struct netlink_callback *cb) |
| 1291 | { |
| 1292 | struct devlink *devlink; |
| 1293 | struct devlink_sb *devlink_sb; |
| 1294 | int start = cb->args[0]; |
| 1295 | int idx = 0; |
| 1296 | int err; |
| 1297 | |
| 1298 | mutex_lock(&devlink_mutex); |
| 1299 | mutex_lock(&devlink_port_mutex); |
| 1300 | list_for_each_entry(devlink, &devlink_list, list) { |
| 1301 | if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) || |
| 1302 | !devlink->ops || !devlink->ops->sb_tc_pool_bind_get) |
| 1303 | continue; |
| 1304 | list_for_each_entry(devlink_sb, &devlink->sb_list, list) { |
| 1305 | err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx, |
| 1306 | devlink, |
| 1307 | devlink_sb, |
| 1308 | NETLINK_CB(cb->skb).portid, |
| 1309 | cb->nlh->nlmsg_seq); |
| 1310 | if (err && err != -EOPNOTSUPP) |
| 1311 | goto out; |
| 1312 | } |
| 1313 | } |
| 1314 | out: |
| 1315 | mutex_unlock(&devlink_port_mutex); |
| 1316 | mutex_unlock(&devlink_mutex); |
| 1317 | |
| 1318 | cb->args[0] = idx; |
| 1319 | return msg->len; |
| 1320 | } |
| 1321 | |
| 1322 | static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port, |
| 1323 | unsigned int sb_index, u16 tc_index, |
| 1324 | enum devlink_sb_pool_type pool_type, |
| 1325 | u16 pool_index, u32 threshold) |
| 1326 | |
| 1327 | { |
| 1328 | const struct devlink_ops *ops = devlink_port->devlink->ops; |
| 1329 | |
| 1330 | if (ops && ops->sb_tc_pool_bind_set) |
| 1331 | return ops->sb_tc_pool_bind_set(devlink_port, sb_index, |
| 1332 | tc_index, pool_type, |
| 1333 | pool_index, threshold); |
| 1334 | return -EOPNOTSUPP; |
| 1335 | } |
| 1336 | |
| 1337 | static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb, |
| 1338 | struct genl_info *info) |
| 1339 | { |
| 1340 | struct devlink_port *devlink_port = info->user_ptr[0]; |
| 1341 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1342 | enum devlink_sb_pool_type pool_type; |
| 1343 | u16 tc_index; |
| 1344 | u16 pool_index; |
| 1345 | u32 threshold; |
| 1346 | int err; |
| 1347 | |
| 1348 | err = devlink_sb_pool_type_get_from_info(info, &pool_type); |
| 1349 | if (err) |
| 1350 | return err; |
| 1351 | |
| 1352 | err = devlink_sb_tc_index_get_from_info(devlink_sb, info, |
| 1353 | pool_type, &tc_index); |
| 1354 | if (err) |
| 1355 | return err; |
| 1356 | |
| 1357 | err = devlink_sb_pool_index_get_from_info(devlink_sb, info, |
| 1358 | &pool_index); |
| 1359 | if (err) |
| 1360 | return err; |
| 1361 | |
| 1362 | if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD]) |
| 1363 | return -EINVAL; |
| 1364 | |
| 1365 | threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]); |
| 1366 | return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index, |
| 1367 | tc_index, pool_type, |
| 1368 | pool_index, threshold); |
| 1369 | } |
| 1370 | |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 1371 | static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb, |
| 1372 | struct genl_info *info) |
| 1373 | { |
| 1374 | struct devlink *devlink = info->user_ptr[0]; |
| 1375 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1376 | const struct devlink_ops *ops = devlink->ops; |
| 1377 | |
| 1378 | if (ops && ops->sb_occ_snapshot) |
| 1379 | return ops->sb_occ_snapshot(devlink, devlink_sb->index); |
| 1380 | return -EOPNOTSUPP; |
| 1381 | } |
| 1382 | |
| 1383 | static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb, |
| 1384 | struct genl_info *info) |
| 1385 | { |
| 1386 | struct devlink *devlink = info->user_ptr[0]; |
| 1387 | struct devlink_sb *devlink_sb = info->user_ptr[1]; |
| 1388 | const struct devlink_ops *ops = devlink->ops; |
| 1389 | |
| 1390 | if (ops && ops->sb_occ_max_clear) |
| 1391 | return ops->sb_occ_max_clear(devlink, devlink_sb->index); |
| 1392 | return -EOPNOTSUPP; |
| 1393 | } |
| 1394 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1395 | static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink, |
| 1396 | enum devlink_command cmd, u32 portid, |
| 1397 | u32 seq, int flags) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1398 | { |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1399 | const struct devlink_ops *ops = devlink->ops; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1400 | void *hdr; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1401 | int err = 0; |
| 1402 | u16 mode; |
| 1403 | u8 inline_mode; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1404 | |
| 1405 | hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); |
| 1406 | if (!hdr) |
| 1407 | return -EMSGSIZE; |
| 1408 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1409 | err = devlink_nl_put_handle(msg, devlink); |
| 1410 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1411 | goto nla_put_failure; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1412 | |
Jiri Pirko | 4456f61 | 2017-02-09 15:54:36 +0100 | [diff] [blame] | 1413 | if (ops->eswitch_mode_get) { |
| 1414 | err = ops->eswitch_mode_get(devlink, &mode); |
| 1415 | if (err) |
| 1416 | goto nla_put_failure; |
| 1417 | err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode); |
| 1418 | if (err) |
| 1419 | goto nla_put_failure; |
| 1420 | } |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1421 | |
| 1422 | if (ops->eswitch_inline_mode_get) { |
| 1423 | err = ops->eswitch_inline_mode_get(devlink, &inline_mode); |
| 1424 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1425 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1426 | err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE, |
| 1427 | inline_mode); |
| 1428 | if (err) |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1429 | goto nla_put_failure; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1430 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1431 | |
| 1432 | genlmsg_end(msg, hdr); |
| 1433 | return 0; |
| 1434 | |
Jiri Pirko | 1a6aa36 | 2017-02-09 15:54:35 +0100 | [diff] [blame] | 1435 | nla_put_failure: |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1436 | genlmsg_cancel(msg, hdr); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1437 | return err; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1438 | } |
| 1439 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1440 | static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, |
| 1441 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1442 | { |
| 1443 | struct devlink *devlink = info->user_ptr[0]; |
| 1444 | const struct devlink_ops *ops = devlink->ops; |
| 1445 | struct sk_buff *msg; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1446 | int err; |
| 1447 | |
Jiri Pirko | 4456f61 | 2017-02-09 15:54:36 +0100 | [diff] [blame] | 1448 | if (!ops) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1449 | return -EOPNOTSUPP; |
| 1450 | |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1451 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1452 | if (!msg) |
| 1453 | return -ENOMEM; |
| 1454 | |
Jiri Pirko | 21e3d2d | 2017-02-09 15:54:34 +0100 | [diff] [blame] | 1455 | err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET, |
| 1456 | info->snd_portid, info->snd_seq, 0); |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1457 | |
| 1458 | if (err) { |
| 1459 | nlmsg_free(msg); |
| 1460 | return err; |
| 1461 | } |
| 1462 | |
| 1463 | return genlmsg_reply(msg, info); |
| 1464 | } |
| 1465 | |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 1466 | static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, |
| 1467 | struct genl_info *info) |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1468 | { |
| 1469 | struct devlink *devlink = info->user_ptr[0]; |
| 1470 | const struct devlink_ops *ops = devlink->ops; |
| 1471 | u16 mode; |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1472 | u8 inline_mode; |
| 1473 | int err = 0; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1474 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1475 | if (!ops) |
| 1476 | return -EOPNOTSUPP; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1477 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1478 | if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) { |
| 1479 | if (!ops->eswitch_mode_set) |
| 1480 | return -EOPNOTSUPP; |
| 1481 | mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]); |
| 1482 | err = ops->eswitch_mode_set(devlink, mode); |
| 1483 | if (err) |
| 1484 | return err; |
| 1485 | } |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1486 | |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1487 | if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) { |
| 1488 | if (!ops->eswitch_inline_mode_set) |
| 1489 | return -EOPNOTSUPP; |
| 1490 | inline_mode = nla_get_u8( |
| 1491 | info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]); |
| 1492 | err = ops->eswitch_inline_mode_set(devlink, inline_mode); |
| 1493 | if (err) |
| 1494 | return err; |
| 1495 | } |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 1496 | return 0; |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 1497 | } |
| 1498 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 1499 | int devlink_dpipe_match_put(struct sk_buff *skb, |
| 1500 | struct devlink_dpipe_match *match) |
| 1501 | { |
| 1502 | struct devlink_dpipe_header *header = match->header; |
| 1503 | struct devlink_dpipe_field *field = &header->fields[match->field_id]; |
| 1504 | struct nlattr *match_attr; |
| 1505 | |
| 1506 | match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH); |
| 1507 | if (!match_attr) |
| 1508 | return -EMSGSIZE; |
| 1509 | |
| 1510 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) || |
| 1511 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) || |
| 1512 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1513 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1514 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1515 | goto nla_put_failure; |
| 1516 | |
| 1517 | nla_nest_end(skb, match_attr); |
| 1518 | return 0; |
| 1519 | |
| 1520 | nla_put_failure: |
| 1521 | nla_nest_cancel(skb, match_attr); |
| 1522 | return -EMSGSIZE; |
| 1523 | } |
| 1524 | EXPORT_SYMBOL_GPL(devlink_dpipe_match_put); |
| 1525 | |
| 1526 | static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table, |
| 1527 | struct sk_buff *skb) |
| 1528 | { |
| 1529 | struct nlattr *matches_attr; |
| 1530 | |
| 1531 | matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES); |
| 1532 | if (!matches_attr) |
| 1533 | return -EMSGSIZE; |
| 1534 | |
| 1535 | if (table->table_ops->matches_dump(table->priv, skb)) |
| 1536 | goto nla_put_failure; |
| 1537 | |
| 1538 | nla_nest_end(skb, matches_attr); |
| 1539 | return 0; |
| 1540 | |
| 1541 | nla_put_failure: |
| 1542 | nla_nest_cancel(skb, matches_attr); |
| 1543 | return -EMSGSIZE; |
| 1544 | } |
| 1545 | |
| 1546 | int devlink_dpipe_action_put(struct sk_buff *skb, |
| 1547 | struct devlink_dpipe_action *action) |
| 1548 | { |
| 1549 | struct devlink_dpipe_header *header = action->header; |
| 1550 | struct devlink_dpipe_field *field = &header->fields[action->field_id]; |
| 1551 | struct nlattr *action_attr; |
| 1552 | |
| 1553 | action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION); |
| 1554 | if (!action_attr) |
| 1555 | return -EMSGSIZE; |
| 1556 | |
| 1557 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) || |
| 1558 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) || |
| 1559 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 1560 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 1561 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 1562 | goto nla_put_failure; |
| 1563 | |
| 1564 | nla_nest_end(skb, action_attr); |
| 1565 | return 0; |
| 1566 | |
| 1567 | nla_put_failure: |
| 1568 | nla_nest_cancel(skb, action_attr); |
| 1569 | return -EMSGSIZE; |
| 1570 | } |
| 1571 | EXPORT_SYMBOL_GPL(devlink_dpipe_action_put); |
| 1572 | |
| 1573 | static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table, |
| 1574 | struct sk_buff *skb) |
| 1575 | { |
| 1576 | struct nlattr *actions_attr; |
| 1577 | |
| 1578 | actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS); |
| 1579 | if (!actions_attr) |
| 1580 | return -EMSGSIZE; |
| 1581 | |
| 1582 | if (table->table_ops->actions_dump(table->priv, skb)) |
| 1583 | goto nla_put_failure; |
| 1584 | |
| 1585 | nla_nest_end(skb, actions_attr); |
| 1586 | return 0; |
| 1587 | |
| 1588 | nla_put_failure: |
| 1589 | nla_nest_cancel(skb, actions_attr); |
| 1590 | return -EMSGSIZE; |
| 1591 | } |
| 1592 | |
| 1593 | static int devlink_dpipe_table_put(struct sk_buff *skb, |
| 1594 | struct devlink_dpipe_table *table) |
| 1595 | { |
| 1596 | struct nlattr *table_attr; |
| 1597 | |
| 1598 | table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE); |
| 1599 | if (!table_attr) |
| 1600 | return -EMSGSIZE; |
| 1601 | |
| 1602 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) || |
| 1603 | nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table->size, |
| 1604 | DEVLINK_ATTR_PAD)) |
| 1605 | goto nla_put_failure; |
| 1606 | if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED, |
| 1607 | table->counters_enabled)) |
| 1608 | goto nla_put_failure; |
| 1609 | |
| 1610 | if (devlink_dpipe_matches_put(table, skb)) |
| 1611 | goto nla_put_failure; |
| 1612 | |
| 1613 | if (devlink_dpipe_actions_put(table, skb)) |
| 1614 | goto nla_put_failure; |
| 1615 | |
| 1616 | nla_nest_end(skb, table_attr); |
| 1617 | return 0; |
| 1618 | |
| 1619 | nla_put_failure: |
| 1620 | nla_nest_cancel(skb, table_attr); |
| 1621 | return -EMSGSIZE; |
| 1622 | } |
| 1623 | |
| 1624 | static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb, |
| 1625 | struct genl_info *info) |
| 1626 | { |
| 1627 | int err; |
| 1628 | |
| 1629 | if (*pskb) { |
| 1630 | err = genlmsg_reply(*pskb, info); |
| 1631 | if (err) |
| 1632 | return err; |
| 1633 | } |
| 1634 | *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1635 | if (!*pskb) |
| 1636 | return -ENOMEM; |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
| 1640 | static int devlink_dpipe_tables_fill(struct genl_info *info, |
| 1641 | enum devlink_command cmd, int flags, |
| 1642 | struct list_head *dpipe_tables, |
| 1643 | const char *table_name) |
| 1644 | { |
| 1645 | struct devlink *devlink = info->user_ptr[0]; |
| 1646 | struct devlink_dpipe_table *table; |
| 1647 | struct nlattr *tables_attr; |
| 1648 | struct sk_buff *skb = NULL; |
| 1649 | struct nlmsghdr *nlh; |
| 1650 | bool incomplete; |
| 1651 | void *hdr; |
| 1652 | int i; |
| 1653 | int err; |
| 1654 | |
| 1655 | table = list_first_entry(dpipe_tables, |
| 1656 | struct devlink_dpipe_table, list); |
| 1657 | start_again: |
| 1658 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1659 | if (err) |
| 1660 | return err; |
| 1661 | |
| 1662 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1663 | &devlink_nl_family, NLM_F_MULTI, cmd); |
| 1664 | if (!hdr) |
| 1665 | return -EMSGSIZE; |
| 1666 | |
| 1667 | if (devlink_nl_put_handle(skb, devlink)) |
| 1668 | goto nla_put_failure; |
| 1669 | tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES); |
| 1670 | if (!tables_attr) |
| 1671 | goto nla_put_failure; |
| 1672 | |
| 1673 | i = 0; |
| 1674 | incomplete = false; |
| 1675 | list_for_each_entry_from(table, dpipe_tables, list) { |
| 1676 | if (!table_name) { |
| 1677 | err = devlink_dpipe_table_put(skb, table); |
| 1678 | if (err) { |
| 1679 | if (!i) |
| 1680 | goto err_table_put; |
| 1681 | incomplete = true; |
| 1682 | break; |
| 1683 | } |
| 1684 | } else { |
| 1685 | if (!strcmp(table->name, table_name)) { |
| 1686 | err = devlink_dpipe_table_put(skb, table); |
| 1687 | if (err) |
| 1688 | break; |
| 1689 | } |
| 1690 | } |
| 1691 | i++; |
| 1692 | } |
| 1693 | |
| 1694 | nla_nest_end(skb, tables_attr); |
| 1695 | genlmsg_end(skb, hdr); |
| 1696 | if (incomplete) |
| 1697 | goto start_again; |
| 1698 | |
| 1699 | send_done: |
| 1700 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 1701 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 1702 | if (!nlh) { |
| 1703 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 1704 | if (err) |
| 1705 | goto err_skb_send_alloc; |
| 1706 | goto send_done; |
| 1707 | } |
| 1708 | |
| 1709 | return genlmsg_reply(skb, info); |
| 1710 | |
| 1711 | nla_put_failure: |
| 1712 | err = -EMSGSIZE; |
| 1713 | err_table_put: |
| 1714 | err_skb_send_alloc: |
| 1715 | genlmsg_cancel(skb, hdr); |
| 1716 | nlmsg_free(skb); |
| 1717 | return err; |
| 1718 | } |
| 1719 | |
| 1720 | static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb, |
| 1721 | struct genl_info *info) |
| 1722 | { |
| 1723 | struct devlink *devlink = info->user_ptr[0]; |
| 1724 | const char *table_name = NULL; |
| 1725 | |
| 1726 | if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 1727 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 1728 | |
| 1729 | return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0, |
| 1730 | &devlink->dpipe_table_list, |
| 1731 | table_name); |
| 1732 | } |
| 1733 | |
| 1734 | static int devlink_dpipe_value_put(struct sk_buff *skb, |
| 1735 | struct devlink_dpipe_value *value) |
| 1736 | { |
| 1737 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE, |
| 1738 | value->value_size, value->value)) |
| 1739 | return -EMSGSIZE; |
| 1740 | if (value->mask) |
| 1741 | if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK, |
| 1742 | value->value_size, value->mask)) |
| 1743 | return -EMSGSIZE; |
| 1744 | if (value->mapping_valid) |
| 1745 | if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING, |
| 1746 | value->mapping_value)) |
| 1747 | return -EMSGSIZE; |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | static int devlink_dpipe_action_value_put(struct sk_buff *skb, |
| 1752 | struct devlink_dpipe_value *value) |
| 1753 | { |
| 1754 | if (!value->action) |
| 1755 | return -EINVAL; |
| 1756 | if (devlink_dpipe_action_put(skb, value->action)) |
| 1757 | return -EMSGSIZE; |
| 1758 | if (devlink_dpipe_value_put(skb, value)) |
| 1759 | return -EMSGSIZE; |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
| 1763 | static int devlink_dpipe_action_values_put(struct sk_buff *skb, |
| 1764 | struct devlink_dpipe_value *values, |
| 1765 | unsigned int values_count) |
| 1766 | { |
| 1767 | struct nlattr *action_attr; |
| 1768 | int i; |
| 1769 | int err; |
| 1770 | |
| 1771 | for (i = 0; i < values_count; i++) { |
| 1772 | action_attr = nla_nest_start(skb, |
| 1773 | DEVLINK_ATTR_DPIPE_ACTION_VALUE); |
| 1774 | if (!action_attr) |
| 1775 | return -EMSGSIZE; |
| 1776 | err = devlink_dpipe_action_value_put(skb, &values[i]); |
| 1777 | if (err) |
| 1778 | goto err_action_value_put; |
| 1779 | nla_nest_end(skb, action_attr); |
| 1780 | } |
| 1781 | return 0; |
| 1782 | |
| 1783 | err_action_value_put: |
| 1784 | nla_nest_cancel(skb, action_attr); |
| 1785 | return err; |
| 1786 | } |
| 1787 | |
| 1788 | static int devlink_dpipe_match_value_put(struct sk_buff *skb, |
| 1789 | struct devlink_dpipe_value *value) |
| 1790 | { |
| 1791 | if (!value->match) |
| 1792 | return -EINVAL; |
| 1793 | if (devlink_dpipe_match_put(skb, value->match)) |
| 1794 | return -EMSGSIZE; |
| 1795 | if (devlink_dpipe_value_put(skb, value)) |
| 1796 | return -EMSGSIZE; |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | static int devlink_dpipe_match_values_put(struct sk_buff *skb, |
| 1801 | struct devlink_dpipe_value *values, |
| 1802 | unsigned int values_count) |
| 1803 | { |
| 1804 | struct nlattr *match_attr; |
| 1805 | int i; |
| 1806 | int err; |
| 1807 | |
| 1808 | for (i = 0; i < values_count; i++) { |
| 1809 | match_attr = nla_nest_start(skb, |
| 1810 | DEVLINK_ATTR_DPIPE_MATCH_VALUE); |
| 1811 | if (!match_attr) |
| 1812 | return -EMSGSIZE; |
| 1813 | err = devlink_dpipe_match_value_put(skb, &values[i]); |
| 1814 | if (err) |
| 1815 | goto err_match_value_put; |
| 1816 | nla_nest_end(skb, match_attr); |
| 1817 | } |
| 1818 | return 0; |
| 1819 | |
| 1820 | err_match_value_put: |
| 1821 | nla_nest_cancel(skb, match_attr); |
| 1822 | return err; |
| 1823 | } |
| 1824 | |
| 1825 | static int devlink_dpipe_entry_put(struct sk_buff *skb, |
| 1826 | struct devlink_dpipe_entry *entry) |
| 1827 | { |
| 1828 | struct nlattr *entry_attr, *matches_attr, *actions_attr; |
| 1829 | int err; |
| 1830 | |
| 1831 | entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY); |
| 1832 | if (!entry_attr) |
| 1833 | return -EMSGSIZE; |
| 1834 | |
| 1835 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index, |
| 1836 | DEVLINK_ATTR_PAD)) |
| 1837 | goto nla_put_failure; |
| 1838 | if (entry->counter_valid) |
| 1839 | if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER, |
| 1840 | entry->counter, DEVLINK_ATTR_PAD)) |
| 1841 | goto nla_put_failure; |
| 1842 | |
| 1843 | matches_attr = nla_nest_start(skb, |
| 1844 | DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES); |
| 1845 | if (!matches_attr) |
| 1846 | goto nla_put_failure; |
| 1847 | |
| 1848 | err = devlink_dpipe_match_values_put(skb, entry->match_values, |
| 1849 | entry->match_values_count); |
| 1850 | if (err) { |
| 1851 | nla_nest_cancel(skb, matches_attr); |
| 1852 | goto err_match_values_put; |
| 1853 | } |
| 1854 | nla_nest_end(skb, matches_attr); |
| 1855 | |
| 1856 | actions_attr = nla_nest_start(skb, |
| 1857 | DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES); |
| 1858 | if (!actions_attr) |
| 1859 | goto nla_put_failure; |
| 1860 | |
| 1861 | err = devlink_dpipe_action_values_put(skb, entry->action_values, |
| 1862 | entry->action_values_count); |
| 1863 | if (err) { |
| 1864 | nla_nest_cancel(skb, actions_attr); |
| 1865 | goto err_action_values_put; |
| 1866 | } |
| 1867 | nla_nest_end(skb, actions_attr); |
| 1868 | |
| 1869 | nla_nest_end(skb, entry_attr); |
| 1870 | return 0; |
| 1871 | |
| 1872 | nla_put_failure: |
| 1873 | err = -EMSGSIZE; |
| 1874 | err_match_values_put: |
| 1875 | err_action_values_put: |
| 1876 | nla_nest_cancel(skb, entry_attr); |
| 1877 | return err; |
| 1878 | } |
| 1879 | |
| 1880 | static struct devlink_dpipe_table * |
| 1881 | devlink_dpipe_table_find(struct list_head *dpipe_tables, |
| 1882 | const char *table_name) |
| 1883 | { |
| 1884 | struct devlink_dpipe_table *table; |
| 1885 | |
| 1886 | list_for_each_entry_rcu(table, dpipe_tables, list) { |
| 1887 | if (!strcmp(table->name, table_name)) |
| 1888 | return table; |
| 1889 | } |
| 1890 | return NULL; |
| 1891 | } |
| 1892 | |
| 1893 | int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 1894 | { |
| 1895 | struct devlink *devlink; |
| 1896 | int err; |
| 1897 | |
| 1898 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb, |
| 1899 | dump_ctx->info); |
| 1900 | if (err) |
| 1901 | return err; |
| 1902 | |
| 1903 | dump_ctx->hdr = genlmsg_put(dump_ctx->skb, |
| 1904 | dump_ctx->info->snd_portid, |
| 1905 | dump_ctx->info->snd_seq, |
| 1906 | &devlink_nl_family, NLM_F_MULTI, |
| 1907 | dump_ctx->cmd); |
| 1908 | if (!dump_ctx->hdr) |
| 1909 | goto nla_put_failure; |
| 1910 | |
| 1911 | devlink = dump_ctx->info->user_ptr[0]; |
| 1912 | if (devlink_nl_put_handle(dump_ctx->skb, devlink)) |
| 1913 | goto nla_put_failure; |
| 1914 | dump_ctx->nest = nla_nest_start(dump_ctx->skb, |
| 1915 | DEVLINK_ATTR_DPIPE_ENTRIES); |
| 1916 | if (!dump_ctx->nest) |
| 1917 | goto nla_put_failure; |
| 1918 | return 0; |
| 1919 | |
| 1920 | nla_put_failure: |
| 1921 | genlmsg_cancel(dump_ctx->skb, dump_ctx->hdr); |
| 1922 | nlmsg_free(dump_ctx->skb); |
| 1923 | return -EMSGSIZE; |
| 1924 | } |
| 1925 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare); |
| 1926 | |
| 1927 | int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, |
| 1928 | struct devlink_dpipe_entry *entry) |
| 1929 | { |
| 1930 | return devlink_dpipe_entry_put(dump_ctx->skb, entry); |
| 1931 | } |
| 1932 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append); |
| 1933 | |
| 1934 | int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 1935 | { |
| 1936 | nla_nest_end(dump_ctx->skb, dump_ctx->nest); |
| 1937 | genlmsg_end(dump_ctx->skb, dump_ctx->hdr); |
| 1938 | return 0; |
| 1939 | } |
| 1940 | EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close); |
| 1941 | |
| 1942 | static int devlink_dpipe_entries_fill(struct genl_info *info, |
| 1943 | enum devlink_command cmd, int flags, |
| 1944 | struct devlink_dpipe_table *table) |
| 1945 | { |
| 1946 | struct devlink_dpipe_dump_ctx dump_ctx; |
| 1947 | struct nlmsghdr *nlh; |
| 1948 | int err; |
| 1949 | |
| 1950 | dump_ctx.skb = NULL; |
| 1951 | dump_ctx.cmd = cmd; |
| 1952 | dump_ctx.info = info; |
| 1953 | |
| 1954 | err = table->table_ops->entries_dump(table->priv, |
| 1955 | table->counters_enabled, |
| 1956 | &dump_ctx); |
| 1957 | if (err) |
| 1958 | goto err_entries_dump; |
| 1959 | |
| 1960 | send_done: |
| 1961 | nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq, |
| 1962 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 1963 | if (!nlh) { |
| 1964 | err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info); |
| 1965 | if (err) |
| 1966 | goto err_skb_send_alloc; |
| 1967 | goto send_done; |
| 1968 | } |
| 1969 | return genlmsg_reply(dump_ctx.skb, info); |
| 1970 | |
| 1971 | err_entries_dump: |
| 1972 | err_skb_send_alloc: |
| 1973 | genlmsg_cancel(dump_ctx.skb, dump_ctx.hdr); |
| 1974 | nlmsg_free(dump_ctx.skb); |
| 1975 | return err; |
| 1976 | } |
| 1977 | |
| 1978 | static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb, |
| 1979 | struct genl_info *info) |
| 1980 | { |
| 1981 | struct devlink *devlink = info->user_ptr[0]; |
| 1982 | struct devlink_dpipe_table *table; |
| 1983 | const char *table_name; |
| 1984 | |
| 1985 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]) |
| 1986 | return -EINVAL; |
| 1987 | |
| 1988 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 1989 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 1990 | table_name); |
| 1991 | if (!table) |
| 1992 | return -EINVAL; |
| 1993 | |
| 1994 | if (!table->table_ops->entries_dump) |
| 1995 | return -EINVAL; |
| 1996 | |
| 1997 | return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET, |
| 1998 | 0, table); |
| 1999 | } |
| 2000 | |
| 2001 | static int devlink_dpipe_fields_put(struct sk_buff *skb, |
| 2002 | const struct devlink_dpipe_header *header) |
| 2003 | { |
| 2004 | struct devlink_dpipe_field *field; |
| 2005 | struct nlattr *field_attr; |
| 2006 | int i; |
| 2007 | |
| 2008 | for (i = 0; i < header->fields_count; i++) { |
| 2009 | field = &header->fields[i]; |
| 2010 | field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD); |
| 2011 | if (!field_attr) |
| 2012 | return -EMSGSIZE; |
| 2013 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) || |
| 2014 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) || |
| 2015 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) || |
| 2016 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type)) |
| 2017 | goto nla_put_failure; |
| 2018 | nla_nest_end(skb, field_attr); |
| 2019 | } |
| 2020 | return 0; |
| 2021 | |
| 2022 | nla_put_failure: |
| 2023 | nla_nest_cancel(skb, field_attr); |
| 2024 | return -EMSGSIZE; |
| 2025 | } |
| 2026 | |
| 2027 | static int devlink_dpipe_header_put(struct sk_buff *skb, |
| 2028 | struct devlink_dpipe_header *header) |
| 2029 | { |
| 2030 | struct nlattr *fields_attr, *header_attr; |
| 2031 | int err; |
| 2032 | |
| 2033 | header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER); |
| 2034 | if (!header) |
| 2035 | return -EMSGSIZE; |
| 2036 | |
| 2037 | if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) || |
| 2038 | nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) || |
| 2039 | nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global)) |
| 2040 | goto nla_put_failure; |
| 2041 | |
| 2042 | fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS); |
| 2043 | if (!fields_attr) |
| 2044 | goto nla_put_failure; |
| 2045 | |
| 2046 | err = devlink_dpipe_fields_put(skb, header); |
| 2047 | if (err) { |
| 2048 | nla_nest_cancel(skb, fields_attr); |
| 2049 | goto nla_put_failure; |
| 2050 | } |
| 2051 | nla_nest_end(skb, fields_attr); |
| 2052 | nla_nest_end(skb, header_attr); |
| 2053 | return 0; |
| 2054 | |
| 2055 | nla_put_failure: |
| 2056 | err = -EMSGSIZE; |
| 2057 | nla_nest_cancel(skb, header_attr); |
| 2058 | return err; |
| 2059 | } |
| 2060 | |
| 2061 | static int devlink_dpipe_headers_fill(struct genl_info *info, |
| 2062 | enum devlink_command cmd, int flags, |
| 2063 | struct devlink_dpipe_headers * |
| 2064 | dpipe_headers) |
| 2065 | { |
| 2066 | struct devlink *devlink = info->user_ptr[0]; |
| 2067 | struct nlattr *headers_attr; |
| 2068 | struct sk_buff *skb = NULL; |
| 2069 | struct nlmsghdr *nlh; |
| 2070 | void *hdr; |
| 2071 | int i, j; |
| 2072 | int err; |
| 2073 | |
| 2074 | i = 0; |
| 2075 | start_again: |
| 2076 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2077 | if (err) |
| 2078 | return err; |
| 2079 | |
| 2080 | hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2081 | &devlink_nl_family, NLM_F_MULTI, cmd); |
| 2082 | if (!hdr) |
| 2083 | return -EMSGSIZE; |
| 2084 | |
| 2085 | if (devlink_nl_put_handle(skb, devlink)) |
| 2086 | goto nla_put_failure; |
| 2087 | headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS); |
| 2088 | if (!headers_attr) |
| 2089 | goto nla_put_failure; |
| 2090 | |
| 2091 | j = 0; |
| 2092 | for (; i < dpipe_headers->headers_count; i++) { |
| 2093 | err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]); |
| 2094 | if (err) { |
| 2095 | if (!j) |
| 2096 | goto err_table_put; |
| 2097 | break; |
| 2098 | } |
| 2099 | j++; |
| 2100 | } |
| 2101 | nla_nest_end(skb, headers_attr); |
| 2102 | genlmsg_end(skb, hdr); |
| 2103 | if (i != dpipe_headers->headers_count) |
| 2104 | goto start_again; |
| 2105 | |
| 2106 | send_done: |
| 2107 | nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, |
| 2108 | NLMSG_DONE, 0, flags | NLM_F_MULTI); |
| 2109 | if (!nlh) { |
| 2110 | err = devlink_dpipe_send_and_alloc_skb(&skb, info); |
| 2111 | if (err) |
| 2112 | goto err_skb_send_alloc; |
| 2113 | goto send_done; |
| 2114 | } |
| 2115 | return genlmsg_reply(skb, info); |
| 2116 | |
| 2117 | nla_put_failure: |
| 2118 | err = -EMSGSIZE; |
| 2119 | err_table_put: |
| 2120 | err_skb_send_alloc: |
| 2121 | genlmsg_cancel(skb, hdr); |
| 2122 | nlmsg_free(skb); |
| 2123 | return err; |
| 2124 | } |
| 2125 | |
| 2126 | static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb, |
| 2127 | struct genl_info *info) |
| 2128 | { |
| 2129 | struct devlink *devlink = info->user_ptr[0]; |
| 2130 | |
| 2131 | if (!devlink->dpipe_headers) |
| 2132 | return -EOPNOTSUPP; |
| 2133 | return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET, |
| 2134 | 0, devlink->dpipe_headers); |
| 2135 | } |
| 2136 | |
| 2137 | static int devlink_dpipe_table_counters_set(struct devlink *devlink, |
| 2138 | const char *table_name, |
| 2139 | bool enable) |
| 2140 | { |
| 2141 | struct devlink_dpipe_table *table; |
| 2142 | |
| 2143 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2144 | table_name); |
| 2145 | if (!table) |
| 2146 | return -EINVAL; |
| 2147 | |
| 2148 | if (table->counter_control_extern) |
| 2149 | return -EOPNOTSUPP; |
| 2150 | |
| 2151 | if (!(table->counters_enabled ^ enable)) |
| 2152 | return 0; |
| 2153 | |
| 2154 | table->counters_enabled = enable; |
| 2155 | if (table->table_ops->counters_set_update) |
| 2156 | table->table_ops->counters_set_update(table->priv, enable); |
| 2157 | return 0; |
| 2158 | } |
| 2159 | |
| 2160 | static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb, |
| 2161 | struct genl_info *info) |
| 2162 | { |
| 2163 | struct devlink *devlink = info->user_ptr[0]; |
| 2164 | const char *table_name; |
| 2165 | bool counters_enable; |
| 2166 | |
| 2167 | if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] || |
| 2168 | !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) |
| 2169 | return -EINVAL; |
| 2170 | |
| 2171 | table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]); |
| 2172 | counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]); |
| 2173 | |
| 2174 | return devlink_dpipe_table_counters_set(devlink, table_name, |
| 2175 | counters_enable); |
| 2176 | } |
| 2177 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2178 | static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = { |
| 2179 | [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING }, |
| 2180 | [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING }, |
| 2181 | [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 }, |
| 2182 | [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 }, |
| 2183 | [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 2184 | [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 }, |
| 2185 | [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 }, |
| 2186 | [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 }, |
| 2187 | [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 }, |
| 2188 | [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 }, |
| 2189 | [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 }, |
| 2190 | [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 2191 | [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 }, |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 2192 | [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2193 | [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING }, |
| 2194 | [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2195 | }; |
| 2196 | |
| 2197 | static const struct genl_ops devlink_nl_ops[] = { |
| 2198 | { |
| 2199 | .cmd = DEVLINK_CMD_GET, |
| 2200 | .doit = devlink_nl_cmd_get_doit, |
| 2201 | .dumpit = devlink_nl_cmd_get_dumpit, |
| 2202 | .policy = devlink_nl_policy, |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 2203 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2204 | /* can be retrieved by unprivileged users */ |
| 2205 | }, |
| 2206 | { |
| 2207 | .cmd = DEVLINK_CMD_PORT_GET, |
| 2208 | .doit = devlink_nl_cmd_port_get_doit, |
| 2209 | .dumpit = devlink_nl_cmd_port_get_dumpit, |
| 2210 | .policy = devlink_nl_policy, |
| 2211 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 2212 | /* can be retrieved by unprivileged users */ |
| 2213 | }, |
| 2214 | { |
| 2215 | .cmd = DEVLINK_CMD_PORT_SET, |
| 2216 | .doit = devlink_nl_cmd_port_set_doit, |
| 2217 | .policy = devlink_nl_policy, |
| 2218 | .flags = GENL_ADMIN_PERM, |
| 2219 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT, |
| 2220 | }, |
| 2221 | { |
| 2222 | .cmd = DEVLINK_CMD_PORT_SPLIT, |
| 2223 | .doit = devlink_nl_cmd_port_split_doit, |
| 2224 | .policy = devlink_nl_policy, |
| 2225 | .flags = GENL_ADMIN_PERM, |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 2226 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2227 | }, |
| 2228 | { |
| 2229 | .cmd = DEVLINK_CMD_PORT_UNSPLIT, |
| 2230 | .doit = devlink_nl_cmd_port_unsplit_doit, |
| 2231 | .policy = devlink_nl_policy, |
| 2232 | .flags = GENL_ADMIN_PERM, |
Jiri Pirko | 1fc2257 | 2016-04-08 19:12:48 +0200 | [diff] [blame] | 2233 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2234 | }, |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 2235 | { |
| 2236 | .cmd = DEVLINK_CMD_SB_GET, |
| 2237 | .doit = devlink_nl_cmd_sb_get_doit, |
| 2238 | .dumpit = devlink_nl_cmd_sb_get_dumpit, |
| 2239 | .policy = devlink_nl_policy, |
| 2240 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 2241 | DEVLINK_NL_FLAG_NEED_SB, |
| 2242 | /* can be retrieved by unprivileged users */ |
| 2243 | }, |
| 2244 | { |
| 2245 | .cmd = DEVLINK_CMD_SB_POOL_GET, |
| 2246 | .doit = devlink_nl_cmd_sb_pool_get_doit, |
| 2247 | .dumpit = devlink_nl_cmd_sb_pool_get_dumpit, |
| 2248 | .policy = devlink_nl_policy, |
| 2249 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 2250 | DEVLINK_NL_FLAG_NEED_SB, |
| 2251 | /* can be retrieved by unprivileged users */ |
| 2252 | }, |
| 2253 | { |
| 2254 | .cmd = DEVLINK_CMD_SB_POOL_SET, |
| 2255 | .doit = devlink_nl_cmd_sb_pool_set_doit, |
| 2256 | .policy = devlink_nl_policy, |
| 2257 | .flags = GENL_ADMIN_PERM, |
| 2258 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 2259 | DEVLINK_NL_FLAG_NEED_SB, |
| 2260 | }, |
| 2261 | { |
| 2262 | .cmd = DEVLINK_CMD_SB_PORT_POOL_GET, |
| 2263 | .doit = devlink_nl_cmd_sb_port_pool_get_doit, |
| 2264 | .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit, |
| 2265 | .policy = devlink_nl_policy, |
| 2266 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 2267 | DEVLINK_NL_FLAG_NEED_SB, |
| 2268 | /* can be retrieved by unprivileged users */ |
| 2269 | }, |
| 2270 | { |
| 2271 | .cmd = DEVLINK_CMD_SB_PORT_POOL_SET, |
| 2272 | .doit = devlink_nl_cmd_sb_port_pool_set_doit, |
| 2273 | .policy = devlink_nl_policy, |
| 2274 | .flags = GENL_ADMIN_PERM, |
| 2275 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 2276 | DEVLINK_NL_FLAG_NEED_SB, |
| 2277 | }, |
| 2278 | { |
| 2279 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET, |
| 2280 | .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit, |
| 2281 | .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit, |
| 2282 | .policy = devlink_nl_policy, |
| 2283 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 2284 | DEVLINK_NL_FLAG_NEED_SB, |
| 2285 | /* can be retrieved by unprivileged users */ |
| 2286 | }, |
| 2287 | { |
| 2288 | .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET, |
| 2289 | .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit, |
| 2290 | .policy = devlink_nl_policy, |
| 2291 | .flags = GENL_ADMIN_PERM, |
| 2292 | .internal_flags = DEVLINK_NL_FLAG_NEED_PORT | |
| 2293 | DEVLINK_NL_FLAG_NEED_SB, |
| 2294 | }, |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 2295 | { |
| 2296 | .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT, |
| 2297 | .doit = devlink_nl_cmd_sb_occ_snapshot_doit, |
| 2298 | .policy = devlink_nl_policy, |
| 2299 | .flags = GENL_ADMIN_PERM, |
| 2300 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 2301 | DEVLINK_NL_FLAG_NEED_SB | |
| 2302 | DEVLINK_NL_FLAG_LOCK_PORTS, |
| 2303 | }, |
| 2304 | { |
| 2305 | .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR, |
| 2306 | .doit = devlink_nl_cmd_sb_occ_max_clear_doit, |
| 2307 | .policy = devlink_nl_policy, |
| 2308 | .flags = GENL_ADMIN_PERM, |
| 2309 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK | |
| 2310 | DEVLINK_NL_FLAG_NEED_SB | |
| 2311 | DEVLINK_NL_FLAG_LOCK_PORTS, |
| 2312 | }, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 2313 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 2314 | .cmd = DEVLINK_CMD_ESWITCH_GET, |
| 2315 | .doit = devlink_nl_cmd_eswitch_get_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 2316 | .policy = devlink_nl_policy, |
| 2317 | .flags = GENL_ADMIN_PERM, |
| 2318 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 2319 | }, |
| 2320 | { |
Jiri Pirko | adf200f | 2017-02-09 15:54:33 +0100 | [diff] [blame] | 2321 | .cmd = DEVLINK_CMD_ESWITCH_SET, |
| 2322 | .doit = devlink_nl_cmd_eswitch_set_doit, |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 2323 | .policy = devlink_nl_policy, |
| 2324 | .flags = GENL_ADMIN_PERM, |
| 2325 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 2326 | }, |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2327 | { |
| 2328 | .cmd = DEVLINK_CMD_DPIPE_TABLE_GET, |
| 2329 | .doit = devlink_nl_cmd_dpipe_table_get, |
| 2330 | .policy = devlink_nl_policy, |
| 2331 | .flags = GENL_ADMIN_PERM, |
| 2332 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 2333 | }, |
| 2334 | { |
| 2335 | .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET, |
| 2336 | .doit = devlink_nl_cmd_dpipe_entries_get, |
| 2337 | .policy = devlink_nl_policy, |
| 2338 | .flags = GENL_ADMIN_PERM, |
| 2339 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 2340 | }, |
| 2341 | { |
| 2342 | .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET, |
| 2343 | .doit = devlink_nl_cmd_dpipe_headers_get, |
| 2344 | .policy = devlink_nl_policy, |
| 2345 | .flags = GENL_ADMIN_PERM, |
| 2346 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 2347 | }, |
| 2348 | { |
| 2349 | .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET, |
| 2350 | .doit = devlink_nl_cmd_dpipe_table_counters_set, |
| 2351 | .policy = devlink_nl_policy, |
| 2352 | .flags = GENL_ADMIN_PERM, |
| 2353 | .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK, |
| 2354 | }, |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2355 | }; |
| 2356 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 2357 | static struct genl_family devlink_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 2358 | .name = DEVLINK_GENL_NAME, |
| 2359 | .version = DEVLINK_GENL_VERSION, |
| 2360 | .maxattr = DEVLINK_ATTR_MAX, |
| 2361 | .netnsok = true, |
| 2362 | .pre_doit = devlink_nl_pre_doit, |
| 2363 | .post_doit = devlink_nl_post_doit, |
| 2364 | .module = THIS_MODULE, |
| 2365 | .ops = devlink_nl_ops, |
| 2366 | .n_ops = ARRAY_SIZE(devlink_nl_ops), |
| 2367 | .mcgrps = devlink_nl_mcgrps, |
| 2368 | .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps), |
| 2369 | }; |
| 2370 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2371 | /** |
| 2372 | * devlink_alloc - Allocate new devlink instance resources |
| 2373 | * |
| 2374 | * @ops: ops |
| 2375 | * @priv_size: size of user private data |
| 2376 | * |
| 2377 | * Allocate new devlink instance resources, including devlink index |
| 2378 | * and name. |
| 2379 | */ |
| 2380 | struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) |
| 2381 | { |
| 2382 | struct devlink *devlink; |
| 2383 | |
| 2384 | devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL); |
| 2385 | if (!devlink) |
| 2386 | return NULL; |
| 2387 | devlink->ops = ops; |
| 2388 | devlink_net_set(devlink, &init_net); |
| 2389 | INIT_LIST_HEAD(&devlink->port_list); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 2390 | INIT_LIST_HEAD(&devlink->sb_list); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2391 | INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2392 | return devlink; |
| 2393 | } |
| 2394 | EXPORT_SYMBOL_GPL(devlink_alloc); |
| 2395 | |
| 2396 | /** |
| 2397 | * devlink_register - Register devlink instance |
| 2398 | * |
| 2399 | * @devlink: devlink |
| 2400 | */ |
| 2401 | int devlink_register(struct devlink *devlink, struct device *dev) |
| 2402 | { |
| 2403 | mutex_lock(&devlink_mutex); |
| 2404 | devlink->dev = dev; |
| 2405 | list_add_tail(&devlink->list, &devlink_list); |
| 2406 | devlink_notify(devlink, DEVLINK_CMD_NEW); |
| 2407 | mutex_unlock(&devlink_mutex); |
| 2408 | return 0; |
| 2409 | } |
| 2410 | EXPORT_SYMBOL_GPL(devlink_register); |
| 2411 | |
| 2412 | /** |
| 2413 | * devlink_unregister - Unregister devlink instance |
| 2414 | * |
| 2415 | * @devlink: devlink |
| 2416 | */ |
| 2417 | void devlink_unregister(struct devlink *devlink) |
| 2418 | { |
| 2419 | mutex_lock(&devlink_mutex); |
| 2420 | devlink_notify(devlink, DEVLINK_CMD_DEL); |
| 2421 | list_del(&devlink->list); |
| 2422 | mutex_unlock(&devlink_mutex); |
| 2423 | } |
| 2424 | EXPORT_SYMBOL_GPL(devlink_unregister); |
| 2425 | |
| 2426 | /** |
| 2427 | * devlink_free - Free devlink instance resources |
| 2428 | * |
| 2429 | * @devlink: devlink |
| 2430 | */ |
| 2431 | void devlink_free(struct devlink *devlink) |
| 2432 | { |
| 2433 | kfree(devlink); |
| 2434 | } |
| 2435 | EXPORT_SYMBOL_GPL(devlink_free); |
| 2436 | |
| 2437 | /** |
| 2438 | * devlink_port_register - Register devlink port |
| 2439 | * |
| 2440 | * @devlink: devlink |
| 2441 | * @devlink_port: devlink port |
| 2442 | * @port_index |
| 2443 | * |
| 2444 | * Register devlink port with provided port index. User can use |
| 2445 | * any indexing, even hw-related one. devlink_port structure |
| 2446 | * is convenient to be embedded inside user driver private structure. |
| 2447 | * Note that the caller should take care of zeroing the devlink_port |
| 2448 | * structure. |
| 2449 | */ |
| 2450 | int devlink_port_register(struct devlink *devlink, |
| 2451 | struct devlink_port *devlink_port, |
| 2452 | unsigned int port_index) |
| 2453 | { |
| 2454 | mutex_lock(&devlink_port_mutex); |
| 2455 | if (devlink_port_index_exists(devlink, port_index)) { |
| 2456 | mutex_unlock(&devlink_port_mutex); |
| 2457 | return -EEXIST; |
| 2458 | } |
| 2459 | devlink_port->devlink = devlink; |
| 2460 | devlink_port->index = port_index; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2461 | devlink_port->registered = true; |
| 2462 | list_add_tail(&devlink_port->list, &devlink->port_list); |
| 2463 | mutex_unlock(&devlink_port_mutex); |
| 2464 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 2465 | return 0; |
| 2466 | } |
| 2467 | EXPORT_SYMBOL_GPL(devlink_port_register); |
| 2468 | |
| 2469 | /** |
| 2470 | * devlink_port_unregister - Unregister devlink port |
| 2471 | * |
| 2472 | * @devlink_port: devlink port |
| 2473 | */ |
| 2474 | void devlink_port_unregister(struct devlink_port *devlink_port) |
| 2475 | { |
| 2476 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL); |
| 2477 | mutex_lock(&devlink_port_mutex); |
| 2478 | list_del(&devlink_port->list); |
| 2479 | mutex_unlock(&devlink_port_mutex); |
| 2480 | } |
| 2481 | EXPORT_SYMBOL_GPL(devlink_port_unregister); |
| 2482 | |
| 2483 | static void __devlink_port_type_set(struct devlink_port *devlink_port, |
| 2484 | enum devlink_port_type type, |
| 2485 | void *type_dev) |
| 2486 | { |
| 2487 | devlink_port->type = type; |
| 2488 | devlink_port->type_dev = type_dev; |
| 2489 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * devlink_port_type_eth_set - Set port type to Ethernet |
| 2494 | * |
| 2495 | * @devlink_port: devlink port |
| 2496 | * @netdev: related netdevice |
| 2497 | */ |
| 2498 | void devlink_port_type_eth_set(struct devlink_port *devlink_port, |
| 2499 | struct net_device *netdev) |
| 2500 | { |
| 2501 | return __devlink_port_type_set(devlink_port, |
| 2502 | DEVLINK_PORT_TYPE_ETH, netdev); |
| 2503 | } |
| 2504 | EXPORT_SYMBOL_GPL(devlink_port_type_eth_set); |
| 2505 | |
| 2506 | /** |
| 2507 | * devlink_port_type_ib_set - Set port type to InfiniBand |
| 2508 | * |
| 2509 | * @devlink_port: devlink port |
| 2510 | * @ibdev: related IB device |
| 2511 | */ |
| 2512 | void devlink_port_type_ib_set(struct devlink_port *devlink_port, |
| 2513 | struct ib_device *ibdev) |
| 2514 | { |
| 2515 | return __devlink_port_type_set(devlink_port, |
| 2516 | DEVLINK_PORT_TYPE_IB, ibdev); |
| 2517 | } |
| 2518 | EXPORT_SYMBOL_GPL(devlink_port_type_ib_set); |
| 2519 | |
| 2520 | /** |
| 2521 | * devlink_port_type_clear - Clear port type |
| 2522 | * |
| 2523 | * @devlink_port: devlink port |
| 2524 | */ |
| 2525 | void devlink_port_type_clear(struct devlink_port *devlink_port) |
| 2526 | { |
| 2527 | return __devlink_port_type_set(devlink_port, |
| 2528 | DEVLINK_PORT_TYPE_NOTSET, NULL); |
| 2529 | } |
| 2530 | EXPORT_SYMBOL_GPL(devlink_port_type_clear); |
| 2531 | |
| 2532 | /** |
| 2533 | * devlink_port_split_set - Set port is split |
| 2534 | * |
| 2535 | * @devlink_port: devlink port |
| 2536 | * @split_group: split group - identifies group split port is part of |
| 2537 | */ |
| 2538 | void devlink_port_split_set(struct devlink_port *devlink_port, |
| 2539 | u32 split_group) |
| 2540 | { |
| 2541 | devlink_port->split = true; |
| 2542 | devlink_port->split_group = split_group; |
| 2543 | devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); |
| 2544 | } |
| 2545 | EXPORT_SYMBOL_GPL(devlink_port_split_set); |
| 2546 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 2547 | int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, |
| 2548 | u32 size, u16 ingress_pools_count, |
| 2549 | u16 egress_pools_count, u16 ingress_tc_count, |
| 2550 | u16 egress_tc_count) |
| 2551 | { |
| 2552 | struct devlink_sb *devlink_sb; |
| 2553 | int err = 0; |
| 2554 | |
| 2555 | mutex_lock(&devlink_mutex); |
| 2556 | if (devlink_sb_index_exists(devlink, sb_index)) { |
| 2557 | err = -EEXIST; |
| 2558 | goto unlock; |
| 2559 | } |
| 2560 | |
| 2561 | devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL); |
| 2562 | if (!devlink_sb) { |
| 2563 | err = -ENOMEM; |
| 2564 | goto unlock; |
| 2565 | } |
| 2566 | devlink_sb->index = sb_index; |
| 2567 | devlink_sb->size = size; |
| 2568 | devlink_sb->ingress_pools_count = ingress_pools_count; |
| 2569 | devlink_sb->egress_pools_count = egress_pools_count; |
| 2570 | devlink_sb->ingress_tc_count = ingress_tc_count; |
| 2571 | devlink_sb->egress_tc_count = egress_tc_count; |
| 2572 | list_add_tail(&devlink_sb->list, &devlink->sb_list); |
| 2573 | unlock: |
| 2574 | mutex_unlock(&devlink_mutex); |
| 2575 | return err; |
| 2576 | } |
| 2577 | EXPORT_SYMBOL_GPL(devlink_sb_register); |
| 2578 | |
| 2579 | void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index) |
| 2580 | { |
| 2581 | struct devlink_sb *devlink_sb; |
| 2582 | |
| 2583 | mutex_lock(&devlink_mutex); |
| 2584 | devlink_sb = devlink_sb_get_by_index(devlink, sb_index); |
| 2585 | WARN_ON(!devlink_sb); |
| 2586 | list_del(&devlink_sb->list); |
| 2587 | mutex_unlock(&devlink_mutex); |
| 2588 | kfree(devlink_sb); |
| 2589 | } |
| 2590 | EXPORT_SYMBOL_GPL(devlink_sb_unregister); |
| 2591 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 2592 | /** |
| 2593 | * devlink_dpipe_headers_register - register dpipe headers |
| 2594 | * |
| 2595 | * @devlink: devlink |
| 2596 | * @dpipe_headers: dpipe header array |
| 2597 | * |
| 2598 | * Register the headers supported by hardware. |
| 2599 | */ |
| 2600 | int devlink_dpipe_headers_register(struct devlink *devlink, |
| 2601 | struct devlink_dpipe_headers *dpipe_headers) |
| 2602 | { |
| 2603 | mutex_lock(&devlink_mutex); |
| 2604 | devlink->dpipe_headers = dpipe_headers; |
| 2605 | mutex_unlock(&devlink_mutex); |
| 2606 | return 0; |
| 2607 | } |
| 2608 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register); |
| 2609 | |
| 2610 | /** |
| 2611 | * devlink_dpipe_headers_unregister - unregister dpipe headers |
| 2612 | * |
| 2613 | * @devlink: devlink |
| 2614 | * |
| 2615 | * Unregister the headers supported by hardware. |
| 2616 | */ |
| 2617 | void devlink_dpipe_headers_unregister(struct devlink *devlink) |
| 2618 | { |
| 2619 | mutex_lock(&devlink_mutex); |
| 2620 | devlink->dpipe_headers = NULL; |
| 2621 | mutex_unlock(&devlink_mutex); |
| 2622 | } |
| 2623 | EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister); |
| 2624 | |
| 2625 | /** |
| 2626 | * devlink_dpipe_table_counter_enabled - check if counter allocation |
| 2627 | * required |
| 2628 | * @devlink: devlink |
| 2629 | * @table_name: tables name |
| 2630 | * |
| 2631 | * Used by driver to check if counter allocation is required. |
| 2632 | * After counter allocation is turned on the table entries |
| 2633 | * are updated to include counter statistics. |
| 2634 | * |
| 2635 | * After that point on the driver must respect the counter |
| 2636 | * state so that each entry added to the table is added |
| 2637 | * with a counter. |
| 2638 | */ |
| 2639 | bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, |
| 2640 | const char *table_name) |
| 2641 | { |
| 2642 | struct devlink_dpipe_table *table; |
| 2643 | bool enabled; |
| 2644 | |
| 2645 | rcu_read_lock(); |
| 2646 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2647 | table_name); |
| 2648 | enabled = false; |
| 2649 | if (table) |
| 2650 | enabled = table->counters_enabled; |
| 2651 | rcu_read_unlock(); |
| 2652 | return enabled; |
| 2653 | } |
| 2654 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled); |
| 2655 | |
| 2656 | /** |
| 2657 | * devlink_dpipe_table_register - register dpipe table |
| 2658 | * |
| 2659 | * @devlink: devlink |
| 2660 | * @table_name: table name |
| 2661 | * @table_ops: table ops |
| 2662 | * @priv: priv |
| 2663 | * @size: size |
| 2664 | * @counter_control_extern: external control for counters |
| 2665 | */ |
| 2666 | int devlink_dpipe_table_register(struct devlink *devlink, |
| 2667 | const char *table_name, |
| 2668 | struct devlink_dpipe_table_ops *table_ops, |
| 2669 | void *priv, u64 size, |
| 2670 | bool counter_control_extern) |
| 2671 | { |
| 2672 | struct devlink_dpipe_table *table; |
| 2673 | |
| 2674 | if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name)) |
| 2675 | return -EEXIST; |
| 2676 | |
| 2677 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
| 2678 | if (!table) |
| 2679 | return -ENOMEM; |
| 2680 | |
| 2681 | table->name = table_name; |
| 2682 | table->table_ops = table_ops; |
| 2683 | table->priv = priv; |
| 2684 | table->size = size; |
| 2685 | table->counter_control_extern = counter_control_extern; |
| 2686 | |
| 2687 | mutex_lock(&devlink_mutex); |
| 2688 | list_add_tail_rcu(&table->list, &devlink->dpipe_table_list); |
| 2689 | mutex_unlock(&devlink_mutex); |
| 2690 | return 0; |
| 2691 | } |
| 2692 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_register); |
| 2693 | |
| 2694 | /** |
| 2695 | * devlink_dpipe_table_unregister - unregister dpipe table |
| 2696 | * |
| 2697 | * @devlink: devlink |
| 2698 | * @table_name: table name |
| 2699 | */ |
| 2700 | void devlink_dpipe_table_unregister(struct devlink *devlink, |
| 2701 | const char *table_name) |
| 2702 | { |
| 2703 | struct devlink_dpipe_table *table; |
| 2704 | |
| 2705 | mutex_lock(&devlink_mutex); |
| 2706 | table = devlink_dpipe_table_find(&devlink->dpipe_table_list, |
| 2707 | table_name); |
| 2708 | if (!table) |
| 2709 | goto unlock; |
| 2710 | list_del_rcu(&table->list); |
| 2711 | mutex_unlock(&devlink_mutex); |
| 2712 | kfree_rcu(table, rcu); |
| 2713 | return; |
| 2714 | unlock: |
| 2715 | mutex_unlock(&devlink_mutex); |
| 2716 | } |
| 2717 | EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister); |
| 2718 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2719 | static int __init devlink_module_init(void) |
| 2720 | { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 2721 | return genl_register_family(&devlink_nl_family); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | static void __exit devlink_module_exit(void) |
| 2725 | { |
| 2726 | genl_unregister_family(&devlink_nl_family); |
| 2727 | } |
| 2728 | |
| 2729 | module_init(devlink_module_init); |
| 2730 | module_exit(devlink_module_exit); |
| 2731 | |
| 2732 | MODULE_LICENSE("GPL v2"); |
| 2733 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); |
| 2734 | MODULE_DESCRIPTION("Network physical device Netlink interface"); |
| 2735 | MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME); |