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