linux/include/net/devlink.h
<<
>>
Prefs
   1/*
   2 * include/net/devlink.h - Network physical device Netlink interface
   3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
   4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or
   9 * (at your option) any later version.
  10 */
  11#ifndef _NET_DEVLINK_H_
  12#define _NET_DEVLINK_H_
  13
  14#include <linux/device.h>
  15#include <linux/slab.h>
  16#include <linux/gfp.h>
  17#include <linux/list.h>
  18#include <linux/netdevice.h>
  19#include <linux/spinlock.h>
  20#include <linux/workqueue.h>
  21#include <linux/refcount.h>
  22#include <net/net_namespace.h>
  23#include <net/flow_offload.h>
  24#include <uapi/linux/devlink.h>
  25#include <linux/xarray.h>
  26
  27struct devlink_ops;
  28
  29struct devlink {
  30        struct list_head list;
  31        struct list_head port_list;
  32        struct list_head sb_list;
  33        struct list_head dpipe_table_list;
  34        struct list_head resource_list;
  35        struct list_head param_list;
  36        struct list_head region_list;
  37        struct list_head reporter_list;
  38        struct mutex reporters_lock; /* protects reporter_list */
  39        struct devlink_dpipe_headers *dpipe_headers;
  40        struct list_head trap_list;
  41        struct list_head trap_group_list;
  42        struct list_head trap_policer_list;
  43        const struct devlink_ops *ops;
  44        struct xarray snapshot_ids;
  45        struct device *dev;
  46        possible_net_t _net;
  47        struct mutex lock; /* Serializes access to devlink instance specific objects such as
  48                            * port, sb, dpipe, resource, params, region, traps and more.
  49                            */
  50        u8 reload_failed:1,
  51           reload_enabled:1,
  52           registered:1;
  53        char priv[0] __aligned(NETDEV_ALIGN);
  54};
  55
  56struct devlink_port_phys_attrs {
  57        u32 port_number; /* Same value as "split group".
  58                          * A physical port which is visible to the user
  59                          * for a given port flavour.
  60                          */
  61        u32 split_subport_number; /* If the port is split, this is the number of subport. */
  62};
  63
  64struct devlink_port_pci_pf_attrs {
  65        u16 pf; /* Associated PCI PF for this port. */
  66};
  67
  68struct devlink_port_pci_vf_attrs {
  69        u16 pf; /* Associated PCI PF for this port. */
  70        u16 vf; /* Associated PCI VF for of the PCI PF for this port. */
  71};
  72
  73/**
  74 * struct devlink_port_attrs - devlink port object
  75 * @flavour: flavour of the port
  76 * @split: indicates if this is split port
  77 * @splittable: indicates if the port can be split.
  78 * @lanes: maximum number of lanes the port supports. 0 value is not passed to netlink.
  79 * @switch_id: if the port is part of switch, this is buffer with ID, otherwise this is NULL
  80 */
  81struct devlink_port_attrs {
  82        u8 split:1,
  83           splittable:1;
  84        u32 lanes;
  85        enum devlink_port_flavour flavour;
  86        struct netdev_phys_item_id switch_id;
  87        union {
  88                struct devlink_port_phys_attrs phys;
  89                struct devlink_port_pci_pf_attrs pci_pf;
  90                struct devlink_port_pci_vf_attrs pci_vf;
  91        };
  92};
  93
  94struct devlink_port {
  95        struct list_head list;
  96        struct list_head param_list;
  97        struct devlink *devlink;
  98        unsigned int index;
  99        bool registered;
 100        spinlock_t type_lock; /* Protects type and type_dev
 101                               * pointer consistency.
 102                               */
 103        enum devlink_port_type type;
 104        enum devlink_port_type desired_type;
 105        void *type_dev;
 106        struct devlink_port_attrs attrs;
 107        u8 attrs_set:1,
 108           switch_port:1;
 109        struct delayed_work type_warn_dw;
 110        struct list_head reporter_list;
 111        struct mutex reporters_lock; /* Protects reporter_list */
 112};
 113
 114struct devlink_sb_pool_info {
 115        enum devlink_sb_pool_type pool_type;
 116        u32 size;
 117        enum devlink_sb_threshold_type threshold_type;
 118        u32 cell_size;
 119};
 120
 121/**
 122 * struct devlink_dpipe_field - dpipe field object
 123 * @name: field name
 124 * @id: index inside the headers field array
 125 * @bitwidth: bitwidth
 126 * @mapping_type: mapping type
 127 */
 128struct devlink_dpipe_field {
 129        const char *name;
 130        unsigned int id;
 131        unsigned int bitwidth;
 132        enum devlink_dpipe_field_mapping_type mapping_type;
 133};
 134
 135/**
 136 * struct devlink_dpipe_header - dpipe header object
 137 * @name: header name
 138 * @id: index, global/local detrmined by global bit
 139 * @fields: fields
 140 * @fields_count: number of fields
 141 * @global: indicates if header is shared like most protocol header
 142 *          or driver specific
 143 */
 144struct devlink_dpipe_header {
 145        const char *name;
 146        unsigned int id;
 147        struct devlink_dpipe_field *fields;
 148        unsigned int fields_count;
 149        bool global;
 150};
 151
 152/**
 153 * struct devlink_dpipe_match - represents match operation
 154 * @type: type of match
 155 * @header_index: header index (packets can have several headers of same
 156 *                type like in case of tunnels)
 157 * @header: header
 158 * @fieled_id: field index
 159 */
 160struct devlink_dpipe_match {
 161        enum devlink_dpipe_match_type type;
 162        unsigned int header_index;
 163        struct devlink_dpipe_header *header;
 164        unsigned int field_id;
 165};
 166
 167/**
 168 * struct devlink_dpipe_action - represents action operation
 169 * @type: type of action
 170 * @header_index: header index (packets can have several headers of same
 171 *                type like in case of tunnels)
 172 * @header: header
 173 * @fieled_id: field index
 174 */
 175struct devlink_dpipe_action {
 176        enum devlink_dpipe_action_type type;
 177        unsigned int header_index;
 178        struct devlink_dpipe_header *header;
 179        unsigned int field_id;
 180};
 181
 182/**
 183 * struct devlink_dpipe_value - represents value of match/action
 184 * @action: action
 185 * @match: match
 186 * @mapping_value: in case the field has some mapping this value
 187 *                 specified the mapping value
 188 * @mapping_valid: specify if mapping value is valid
 189 * @value_size: value size
 190 * @value: value
 191 * @mask: bit mask
 192 */
 193struct devlink_dpipe_value {
 194        union {
 195                struct devlink_dpipe_action *action;
 196                struct devlink_dpipe_match *match;
 197        };
 198        unsigned int mapping_value;
 199        bool mapping_valid;
 200        unsigned int value_size;
 201        void *value;
 202        void *mask;
 203};
 204
 205/**
 206 * struct devlink_dpipe_entry - table entry object
 207 * @index: index of the entry in the table
 208 * @match_values: match values
 209 * @matche_values_count: count of matches tuples
 210 * @action_values: actions values
 211 * @action_values_count: count of actions values
 212 * @counter: value of counter
 213 * @counter_valid: Specify if value is valid from hardware
 214 */
 215struct devlink_dpipe_entry {
 216        u64 index;
 217        struct devlink_dpipe_value *match_values;
 218        unsigned int match_values_count;
 219        struct devlink_dpipe_value *action_values;
 220        unsigned int action_values_count;
 221        u64 counter;
 222        bool counter_valid;
 223};
 224
 225/**
 226 * struct devlink_dpipe_dump_ctx - context provided to driver in order
 227 *                                 to dump
 228 * @info: info
 229 * @cmd: devlink command
 230 * @skb: skb
 231 * @nest: top attribute
 232 * @hdr: hdr
 233 */
 234struct devlink_dpipe_dump_ctx {
 235        struct genl_info *info;
 236        enum devlink_command cmd;
 237        struct sk_buff *skb;
 238        struct nlattr *nest;
 239        void *hdr;
 240};
 241
 242struct devlink_dpipe_table_ops;
 243
 244/**
 245 * struct devlink_dpipe_table - table object
 246 * @priv: private
 247 * @name: table name
 248 * @counters_enabled: indicates if counters are active
 249 * @counter_control_extern: indicates if counter control is in dpipe or
 250 *                          external tool
 251 * @resource_valid: Indicate that the resource id is valid
 252 * @resource_id: relative resource this table is related to
 253 * @resource_units: number of resource's unit consumed per table's entry
 254 * @table_ops: table operations
 255 * @rcu: rcu
 256 */
 257struct devlink_dpipe_table {
 258        void *priv;
 259        struct list_head list;
 260        const char *name;
 261        bool counters_enabled;
 262        bool counter_control_extern;
 263        bool resource_valid;
 264        u64 resource_id;
 265        u64 resource_units;
 266        struct devlink_dpipe_table_ops *table_ops;
 267        struct rcu_head rcu;
 268};
 269
 270/**
 271 * struct devlink_dpipe_table_ops - dpipe_table ops
 272 * @actions_dump - dumps all tables actions
 273 * @matches_dump - dumps all tables matches
 274 * @entries_dump - dumps all active entries in the table
 275 * @counters_set_update - when changing the counter status hardware sync
 276 *                        maybe needed to allocate/free counter related
 277 *                        resources
 278 * @size_get - get size
 279 */
 280struct devlink_dpipe_table_ops {
 281        int (*actions_dump)(void *priv, struct sk_buff *skb);
 282        int (*matches_dump)(void *priv, struct sk_buff *skb);
 283        int (*entries_dump)(void *priv, bool counters_enabled,
 284                            struct devlink_dpipe_dump_ctx *dump_ctx);
 285        int (*counters_set_update)(void *priv, bool enable);
 286        u64 (*size_get)(void *priv);
 287};
 288
 289/**
 290 * struct devlink_dpipe_headers - dpipe headers
 291 * @headers - header array can be shared (global bit) or driver specific
 292 * @headers_count - count of headers
 293 */
 294struct devlink_dpipe_headers {
 295        struct devlink_dpipe_header **headers;
 296        unsigned int headers_count;
 297};
 298
 299/**
 300 * struct devlink_resource_size_params - resource's size parameters
 301 * @size_min: minimum size which can be set
 302 * @size_max: maximum size which can be set
 303 * @size_granularity: size granularity
 304 * @size_unit: resource's basic unit
 305 */
 306struct devlink_resource_size_params {
 307        u64 size_min;
 308        u64 size_max;
 309        u64 size_granularity;
 310        enum devlink_resource_unit unit;
 311};
 312
 313static inline void
 314devlink_resource_size_params_init(struct devlink_resource_size_params *size_params,
 315                                  u64 size_min, u64 size_max,
 316                                  u64 size_granularity,
 317                                  enum devlink_resource_unit unit)
 318{
 319        size_params->size_min = size_min;
 320        size_params->size_max = size_max;
 321        size_params->size_granularity = size_granularity;
 322        size_params->unit = unit;
 323}
 324
 325typedef u64 devlink_resource_occ_get_t(void *priv);
 326
 327/**
 328 * struct devlink_resource - devlink resource
 329 * @name: name of the resource
 330 * @id: id, per devlink instance
 331 * @size: size of the resource
 332 * @size_new: updated size of the resource, reload is needed
 333 * @size_valid: valid in case the total size of the resource is valid
 334 *              including its children
 335 * @parent: parent resource
 336 * @size_params: size parameters
 337 * @list: parent list
 338 * @resource_list: list of child resources
 339 */
 340struct devlink_resource {
 341        const char *name;
 342        u64 id;
 343        u64 size;
 344        u64 size_new;
 345        bool size_valid;
 346        struct devlink_resource *parent;
 347        struct devlink_resource_size_params size_params;
 348        struct list_head list;
 349        struct list_head resource_list;
 350        devlink_resource_occ_get_t *occ_get;
 351        void *occ_get_priv;
 352};
 353
 354#define DEVLINK_RESOURCE_ID_PARENT_TOP 0
 355
 356#define __DEVLINK_PARAM_MAX_STRING_VALUE 32
 357enum devlink_param_type {
 358        DEVLINK_PARAM_TYPE_U8,
 359        DEVLINK_PARAM_TYPE_U16,
 360        DEVLINK_PARAM_TYPE_U32,
 361        DEVLINK_PARAM_TYPE_STRING,
 362        DEVLINK_PARAM_TYPE_BOOL,
 363};
 364
 365union devlink_param_value {
 366        u8 vu8;
 367        u16 vu16;
 368        u32 vu32;
 369        char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];
 370        bool vbool;
 371};
 372
 373struct devlink_param_gset_ctx {
 374        union devlink_param_value val;
 375        enum devlink_param_cmode cmode;
 376};
 377
 378/**
 379 * struct devlink_flash_notify - devlink dev flash notify data
 380 * @status_msg: current status string
 381 * @component: firmware component being updated
 382 * @done: amount of work completed of total amount
 383 * @total: amount of work expected to be done
 384 * @timeout: expected max timeout in seconds
 385 *
 386 * These are values to be given to userland to be displayed in order
 387 * to show current activity in a firmware update process.
 388 */
 389struct devlink_flash_notify {
 390        const char *status_msg;
 391        const char *component;
 392        unsigned long done;
 393        unsigned long total;
 394        unsigned long timeout;
 395};
 396
 397/**
 398 * struct devlink_param - devlink configuration parameter data
 399 * @name: name of the parameter
 400 * @generic: indicates if the parameter is generic or driver specific
 401 * @type: parameter type
 402 * @supported_cmodes: bitmap of supported configuration modes
 403 * @get: get parameter value, used for runtime and permanent
 404 *       configuration modes
 405 * @set: set parameter value, used for runtime and permanent
 406 *       configuration modes
 407 * @validate: validate input value is applicable (within value range, etc.)
 408 *
 409 * This struct should be used by the driver to fill the data for
 410 * a parameter it registers.
 411 */
 412struct devlink_param {
 413        u32 id;
 414        const char *name;
 415        bool generic;
 416        enum devlink_param_type type;
 417        unsigned long supported_cmodes;
 418        int (*get)(struct devlink *devlink, u32 id,
 419                   struct devlink_param_gset_ctx *ctx);
 420        int (*set)(struct devlink *devlink, u32 id,
 421                   struct devlink_param_gset_ctx *ctx);
 422        int (*validate)(struct devlink *devlink, u32 id,
 423                        union devlink_param_value val,
 424                        struct netlink_ext_ack *extack);
 425};
 426
 427struct devlink_param_item {
 428        struct list_head list;
 429        const struct devlink_param *param;
 430        union devlink_param_value driverinit_value;
 431        bool driverinit_value_valid;
 432        bool published;
 433};
 434
 435enum devlink_param_generic_id {
 436        DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
 437        DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
 438        DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
 439        DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
 440        DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
 441        DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
 442        DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
 443        DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
 444        DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
 445        DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
 446
 447        /* add new param generic ids above here*/
 448        __DEVLINK_PARAM_GENERIC_ID_MAX,
 449        DEVLINK_PARAM_GENERIC_ID_MAX = __DEVLINK_PARAM_GENERIC_ID_MAX - 1,
 450};
 451
 452#define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME "internal_error_reset"
 453#define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE DEVLINK_PARAM_TYPE_BOOL
 454
 455#define DEVLINK_PARAM_GENERIC_MAX_MACS_NAME "max_macs"
 456#define DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE DEVLINK_PARAM_TYPE_U32
 457
 458#define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME "enable_sriov"
 459#define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE DEVLINK_PARAM_TYPE_BOOL
 460
 461#define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME "region_snapshot_enable"
 462#define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE DEVLINK_PARAM_TYPE_BOOL
 463
 464#define DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME "ignore_ari"
 465#define DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE DEVLINK_PARAM_TYPE_BOOL
 466
 467#define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME "msix_vec_per_pf_max"
 468#define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE DEVLINK_PARAM_TYPE_U32
 469
 470#define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME "msix_vec_per_pf_min"
 471#define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE DEVLINK_PARAM_TYPE_U32
 472
 473#define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME "fw_load_policy"
 474#define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE DEVLINK_PARAM_TYPE_U8
 475
 476#define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME \
 477        "reset_dev_on_drv_probe"
 478#define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE DEVLINK_PARAM_TYPE_U8
 479
 480#define DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME "enable_roce"
 481#define DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE DEVLINK_PARAM_TYPE_BOOL
 482
 483#define DEVLINK_PARAM_GENERIC(_id, _cmodes, _get, _set, _validate)      \
 484{                                                                       \
 485        .id = DEVLINK_PARAM_GENERIC_ID_##_id,                           \
 486        .name = DEVLINK_PARAM_GENERIC_##_id##_NAME,                     \
 487        .type = DEVLINK_PARAM_GENERIC_##_id##_TYPE,                     \
 488        .generic = true,                                                \
 489        .supported_cmodes = _cmodes,                                    \
 490        .get = _get,                                                    \
 491        .set = _set,                                                    \
 492        .validate = _validate,                                          \
 493}
 494
 495#define DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, _get, _set, _validate) \
 496{                                                                       \
 497        .id = _id,                                                      \
 498        .name = _name,                                                  \
 499        .type = _type,                                                  \
 500        .supported_cmodes = _cmodes,                                    \
 501        .get = _get,                                                    \
 502        .set = _set,                                                    \
 503        .validate = _validate,                                          \
 504}
 505
 506/* Part number, identifier of board design */
 507#define DEVLINK_INFO_VERSION_GENERIC_BOARD_ID   "board.id"
 508/* Revision of board design */
 509#define DEVLINK_INFO_VERSION_GENERIC_BOARD_REV  "board.rev"
 510/* Maker of the board */
 511#define DEVLINK_INFO_VERSION_GENERIC_BOARD_MANUFACTURE  "board.manufacture"
 512
 513/* Part number, identifier of asic design */
 514#define DEVLINK_INFO_VERSION_GENERIC_ASIC_ID    "asic.id"
 515/* Revision of asic design */
 516#define DEVLINK_INFO_VERSION_GENERIC_ASIC_REV   "asic.rev"
 517
 518/* Overall FW version */
 519#define DEVLINK_INFO_VERSION_GENERIC_FW         "fw"
 520/* Control processor FW version */
 521#define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT    "fw.mgmt"
 522/* FW interface specification version */
 523#define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT_API        "fw.mgmt.api"
 524/* Data path microcode controlling high-speed packet processing */
 525#define DEVLINK_INFO_VERSION_GENERIC_FW_APP     "fw.app"
 526/* UNDI software version */
 527#define DEVLINK_INFO_VERSION_GENERIC_FW_UNDI    "fw.undi"
 528/* NCSI support/handler version */
 529#define DEVLINK_INFO_VERSION_GENERIC_FW_NCSI    "fw.ncsi"
 530/* FW parameter set id */
 531#define DEVLINK_INFO_VERSION_GENERIC_FW_PSID    "fw.psid"
 532/* RoCE FW version */
 533#define DEVLINK_INFO_VERSION_GENERIC_FW_ROCE    "fw.roce"
 534/* Firmware bundle identifier */
 535#define DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID       "fw.bundle_id"
 536
 537struct devlink_region;
 538struct devlink_info_req;
 539
 540/**
 541 * struct devlink_region_ops - Region operations
 542 * @name: region name
 543 * @destructor: callback used to free snapshot memory when deleting
 544 * @snapshot: callback to request an immediate snapshot. On success,
 545 *            the data variable must be updated to point to the snapshot data.
 546 *            The function will be called while the devlink instance lock is
 547 *            held.
 548 */
 549struct devlink_region_ops {
 550        const char *name;
 551        void (*destructor)(const void *data);
 552        int (*snapshot)(struct devlink *devlink, struct netlink_ext_ack *extack,
 553                        u8 **data);
 554};
 555
 556struct devlink_fmsg;
 557struct devlink_health_reporter;
 558
 559enum devlink_health_reporter_state {
 560        DEVLINK_HEALTH_REPORTER_STATE_HEALTHY,
 561        DEVLINK_HEALTH_REPORTER_STATE_ERROR,
 562};
 563
 564/**
 565 * struct devlink_health_reporter_ops - Reporter operations
 566 * @name: reporter name
 567 * @recover: callback to recover from reported error
 568 *           if priv_ctx is NULL, run a full recover
 569 * @dump: callback to dump an object
 570 *        if priv_ctx is NULL, run a full dump
 571 * @diagnose: callback to diagnose the current status
 572 * @test: callback to trigger a test event
 573 */
 574
 575struct devlink_health_reporter_ops {
 576        char *name;
 577        int (*recover)(struct devlink_health_reporter *reporter,
 578                       void *priv_ctx, struct netlink_ext_ack *extack);
 579        int (*dump)(struct devlink_health_reporter *reporter,
 580                    struct devlink_fmsg *fmsg, void *priv_ctx,
 581                    struct netlink_ext_ack *extack);
 582        int (*diagnose)(struct devlink_health_reporter *reporter,
 583                        struct devlink_fmsg *fmsg,
 584                        struct netlink_ext_ack *extack);
 585        int (*test)(struct devlink_health_reporter *reporter,
 586                    struct netlink_ext_ack *extack);
 587};
 588
 589/**
 590 * struct devlink_trap_policer - Immutable packet trap policer attributes.
 591 * @id: Policer identifier.
 592 * @init_rate: Initial rate in packets / sec.
 593 * @init_burst: Initial burst size in packets.
 594 * @max_rate: Maximum rate.
 595 * @min_rate: Minimum rate.
 596 * @max_burst: Maximum burst size.
 597 * @min_burst: Minimum burst size.
 598 *
 599 * Describes immutable attributes of packet trap policers that drivers register
 600 * with devlink.
 601 */
 602struct devlink_trap_policer {
 603        u32 id;
 604        u64 init_rate;
 605        u64 init_burst;
 606        u64 max_rate;
 607        u64 min_rate;
 608        u64 max_burst;
 609        u64 min_burst;
 610};
 611
 612/**
 613 * struct devlink_trap_group - Immutable packet trap group attributes.
 614 * @name: Trap group name.
 615 * @id: Trap group identifier.
 616 * @generic: Whether the trap group is generic or not.
 617 * @init_policer_id: Initial policer identifier.
 618 *
 619 * Describes immutable attributes of packet trap groups that drivers register
 620 * with devlink.
 621 */
 622struct devlink_trap_group {
 623        const char *name;
 624        u16 id;
 625        bool generic;
 626        u32 init_policer_id;
 627};
 628
 629#define DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT    BIT(0)
 630#define DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE  BIT(1)
 631
 632/**
 633 * struct devlink_trap - Immutable packet trap attributes.
 634 * @type: Trap type.
 635 * @init_action: Initial trap action.
 636 * @generic: Whether the trap is generic or not.
 637 * @id: Trap identifier.
 638 * @name: Trap name.
 639 * @init_group_id: Initial group identifier.
 640 * @metadata_cap: Metadata types that can be provided by the trap.
 641 *
 642 * Describes immutable attributes of packet traps that drivers register with
 643 * devlink.
 644 */
 645struct devlink_trap {
 646        enum devlink_trap_type type;
 647        enum devlink_trap_action init_action;
 648        bool generic;
 649        u16 id;
 650        const char *name;
 651        u16 init_group_id;
 652        u32 metadata_cap;
 653};
 654
 655/* All traps must be documented in
 656 * Documentation/networking/devlink/devlink-trap.rst
 657 */
 658enum devlink_trap_generic_id {
 659        DEVLINK_TRAP_GENERIC_ID_SMAC_MC,
 660        DEVLINK_TRAP_GENERIC_ID_VLAN_TAG_MISMATCH,
 661        DEVLINK_TRAP_GENERIC_ID_INGRESS_VLAN_FILTER,
 662        DEVLINK_TRAP_GENERIC_ID_INGRESS_STP_FILTER,
 663        DEVLINK_TRAP_GENERIC_ID_EMPTY_TX_LIST,
 664        DEVLINK_TRAP_GENERIC_ID_PORT_LOOPBACK_FILTER,
 665        DEVLINK_TRAP_GENERIC_ID_BLACKHOLE_ROUTE,
 666        DEVLINK_TRAP_GENERIC_ID_TTL_ERROR,
 667        DEVLINK_TRAP_GENERIC_ID_TAIL_DROP,
 668        DEVLINK_TRAP_GENERIC_ID_NON_IP_PACKET,
 669        DEVLINK_TRAP_GENERIC_ID_UC_DIP_MC_DMAC,
 670        DEVLINK_TRAP_GENERIC_ID_DIP_LB,
 671        DEVLINK_TRAP_GENERIC_ID_SIP_MC,
 672        DEVLINK_TRAP_GENERIC_ID_SIP_LB,
 673        DEVLINK_TRAP_GENERIC_ID_CORRUPTED_IP_HDR,
 674        DEVLINK_TRAP_GENERIC_ID_IPV4_SIP_BC,
 675        DEVLINK_TRAP_GENERIC_ID_IPV6_MC_DIP_RESERVED_SCOPE,
 676        DEVLINK_TRAP_GENERIC_ID_IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE,
 677        DEVLINK_TRAP_GENERIC_ID_MTU_ERROR,
 678        DEVLINK_TRAP_GENERIC_ID_UNRESOLVED_NEIGH,
 679        DEVLINK_TRAP_GENERIC_ID_RPF,
 680        DEVLINK_TRAP_GENERIC_ID_REJECT_ROUTE,
 681        DEVLINK_TRAP_GENERIC_ID_IPV4_LPM_UNICAST_MISS,
 682        DEVLINK_TRAP_GENERIC_ID_IPV6_LPM_UNICAST_MISS,
 683        DEVLINK_TRAP_GENERIC_ID_NON_ROUTABLE,
 684        DEVLINK_TRAP_GENERIC_ID_DECAP_ERROR,
 685        DEVLINK_TRAP_GENERIC_ID_OVERLAY_SMAC_MC,
 686        DEVLINK_TRAP_GENERIC_ID_INGRESS_FLOW_ACTION_DROP,
 687        DEVLINK_TRAP_GENERIC_ID_EGRESS_FLOW_ACTION_DROP,
 688        DEVLINK_TRAP_GENERIC_ID_STP,
 689        DEVLINK_TRAP_GENERIC_ID_LACP,
 690        DEVLINK_TRAP_GENERIC_ID_LLDP,
 691        DEVLINK_TRAP_GENERIC_ID_IGMP_QUERY,
 692        DEVLINK_TRAP_GENERIC_ID_IGMP_V1_REPORT,
 693        DEVLINK_TRAP_GENERIC_ID_IGMP_V2_REPORT,
 694        DEVLINK_TRAP_GENERIC_ID_IGMP_V3_REPORT,
 695        DEVLINK_TRAP_GENERIC_ID_IGMP_V2_LEAVE,
 696        DEVLINK_TRAP_GENERIC_ID_MLD_QUERY,
 697        DEVLINK_TRAP_GENERIC_ID_MLD_V1_REPORT,
 698        DEVLINK_TRAP_GENERIC_ID_MLD_V2_REPORT,
 699        DEVLINK_TRAP_GENERIC_ID_MLD_V1_DONE,
 700        DEVLINK_TRAP_GENERIC_ID_IPV4_DHCP,
 701        DEVLINK_TRAP_GENERIC_ID_IPV6_DHCP,
 702        DEVLINK_TRAP_GENERIC_ID_ARP_REQUEST,
 703        DEVLINK_TRAP_GENERIC_ID_ARP_RESPONSE,
 704        DEVLINK_TRAP_GENERIC_ID_ARP_OVERLAY,
 705        DEVLINK_TRAP_GENERIC_ID_IPV6_NEIGH_SOLICIT,
 706        DEVLINK_TRAP_GENERIC_ID_IPV6_NEIGH_ADVERT,
 707        DEVLINK_TRAP_GENERIC_ID_IPV4_BFD,
 708        DEVLINK_TRAP_GENERIC_ID_IPV6_BFD,
 709        DEVLINK_TRAP_GENERIC_ID_IPV4_OSPF,
 710        DEVLINK_TRAP_GENERIC_ID_IPV6_OSPF,
 711        DEVLINK_TRAP_GENERIC_ID_IPV4_BGP,
 712        DEVLINK_TRAP_GENERIC_ID_IPV6_BGP,
 713        DEVLINK_TRAP_GENERIC_ID_IPV4_VRRP,
 714        DEVLINK_TRAP_GENERIC_ID_IPV6_VRRP,
 715        DEVLINK_TRAP_GENERIC_ID_IPV4_PIM,
 716        DEVLINK_TRAP_GENERIC_ID_IPV6_PIM,
 717        DEVLINK_TRAP_GENERIC_ID_UC_LB,
 718        DEVLINK_TRAP_GENERIC_ID_LOCAL_ROUTE,
 719        DEVLINK_TRAP_GENERIC_ID_EXTERNAL_ROUTE,
 720        DEVLINK_TRAP_GENERIC_ID_IPV6_UC_DIP_LINK_LOCAL_SCOPE,
 721        DEVLINK_TRAP_GENERIC_ID_IPV6_DIP_ALL_NODES,
 722        DEVLINK_TRAP_GENERIC_ID_IPV6_DIP_ALL_ROUTERS,
 723        DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_SOLICIT,
 724        DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_ADVERT,
 725        DEVLINK_TRAP_GENERIC_ID_IPV6_REDIRECT,
 726        DEVLINK_TRAP_GENERIC_ID_IPV4_ROUTER_ALERT,
 727        DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_ALERT,
 728        DEVLINK_TRAP_GENERIC_ID_PTP_EVENT,
 729        DEVLINK_TRAP_GENERIC_ID_PTP_GENERAL,
 730        DEVLINK_TRAP_GENERIC_ID_FLOW_ACTION_SAMPLE,
 731        DEVLINK_TRAP_GENERIC_ID_FLOW_ACTION_TRAP,
 732        DEVLINK_TRAP_GENERIC_ID_EARLY_DROP,
 733
 734        /* Add new generic trap IDs above */
 735        __DEVLINK_TRAP_GENERIC_ID_MAX,
 736        DEVLINK_TRAP_GENERIC_ID_MAX = __DEVLINK_TRAP_GENERIC_ID_MAX - 1,
 737};
 738
 739/* All trap groups must be documented in
 740 * Documentation/networking/devlink/devlink-trap.rst
 741 */
 742enum devlink_trap_group_generic_id {
 743        DEVLINK_TRAP_GROUP_GENERIC_ID_L2_DROPS,
 744        DEVLINK_TRAP_GROUP_GENERIC_ID_L3_DROPS,
 745        DEVLINK_TRAP_GROUP_GENERIC_ID_L3_EXCEPTIONS,
 746        DEVLINK_TRAP_GROUP_GENERIC_ID_BUFFER_DROPS,
 747        DEVLINK_TRAP_GROUP_GENERIC_ID_TUNNEL_DROPS,
 748        DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_DROPS,
 749        DEVLINK_TRAP_GROUP_GENERIC_ID_STP,
 750        DEVLINK_TRAP_GROUP_GENERIC_ID_LACP,
 751        DEVLINK_TRAP_GROUP_GENERIC_ID_LLDP,
 752        DEVLINK_TRAP_GROUP_GENERIC_ID_MC_SNOOPING,
 753        DEVLINK_TRAP_GROUP_GENERIC_ID_DHCP,
 754        DEVLINK_TRAP_GROUP_GENERIC_ID_NEIGH_DISCOVERY,
 755        DEVLINK_TRAP_GROUP_GENERIC_ID_BFD,
 756        DEVLINK_TRAP_GROUP_GENERIC_ID_OSPF,
 757        DEVLINK_TRAP_GROUP_GENERIC_ID_BGP,
 758        DEVLINK_TRAP_GROUP_GENERIC_ID_VRRP,
 759        DEVLINK_TRAP_GROUP_GENERIC_ID_PIM,
 760        DEVLINK_TRAP_GROUP_GENERIC_ID_UC_LB,
 761        DEVLINK_TRAP_GROUP_GENERIC_ID_LOCAL_DELIVERY,
 762        DEVLINK_TRAP_GROUP_GENERIC_ID_EXTERNAL_DELIVERY,
 763        DEVLINK_TRAP_GROUP_GENERIC_ID_IPV6,
 764        DEVLINK_TRAP_GROUP_GENERIC_ID_PTP_EVENT,
 765        DEVLINK_TRAP_GROUP_GENERIC_ID_PTP_GENERAL,
 766        DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_SAMPLE,
 767        DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_TRAP,
 768
 769        /* Add new generic trap group IDs above */
 770        __DEVLINK_TRAP_GROUP_GENERIC_ID_MAX,
 771        DEVLINK_TRAP_GROUP_GENERIC_ID_MAX =
 772                __DEVLINK_TRAP_GROUP_GENERIC_ID_MAX - 1,
 773};
 774
 775#define DEVLINK_TRAP_GENERIC_NAME_SMAC_MC \
 776        "source_mac_is_multicast"
 777#define DEVLINK_TRAP_GENERIC_NAME_VLAN_TAG_MISMATCH \
 778        "vlan_tag_mismatch"
 779#define DEVLINK_TRAP_GENERIC_NAME_INGRESS_VLAN_FILTER \
 780        "ingress_vlan_filter"
 781#define DEVLINK_TRAP_GENERIC_NAME_INGRESS_STP_FILTER \
 782        "ingress_spanning_tree_filter"
 783#define DEVLINK_TRAP_GENERIC_NAME_EMPTY_TX_LIST \
 784        "port_list_is_empty"
 785#define DEVLINK_TRAP_GENERIC_NAME_PORT_LOOPBACK_FILTER \
 786        "port_loopback_filter"
 787#define DEVLINK_TRAP_GENERIC_NAME_BLACKHOLE_ROUTE \
 788        "blackhole_route"
 789#define DEVLINK_TRAP_GENERIC_NAME_TTL_ERROR \
 790        "ttl_value_is_too_small"
 791#define DEVLINK_TRAP_GENERIC_NAME_TAIL_DROP \
 792        "tail_drop"
 793#define DEVLINK_TRAP_GENERIC_NAME_NON_IP_PACKET \
 794        "non_ip"
 795#define DEVLINK_TRAP_GENERIC_NAME_UC_DIP_MC_DMAC \
 796        "uc_dip_over_mc_dmac"
 797#define DEVLINK_TRAP_GENERIC_NAME_DIP_LB \
 798        "dip_is_loopback_address"
 799#define DEVLINK_TRAP_GENERIC_NAME_SIP_MC \
 800        "sip_is_mc"
 801#define DEVLINK_TRAP_GENERIC_NAME_SIP_LB \
 802        "sip_is_loopback_address"
 803#define DEVLINK_TRAP_GENERIC_NAME_CORRUPTED_IP_HDR \
 804        "ip_header_corrupted"
 805#define DEVLINK_TRAP_GENERIC_NAME_IPV4_SIP_BC \
 806        "ipv4_sip_is_limited_bc"
 807#define DEVLINK_TRAP_GENERIC_NAME_IPV6_MC_DIP_RESERVED_SCOPE \
 808        "ipv6_mc_dip_reserved_scope"
 809#define DEVLINK_TRAP_GENERIC_NAME_IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE \
 810        "ipv6_mc_dip_interface_local_scope"
 811#define DEVLINK_TRAP_GENERIC_NAME_MTU_ERROR \
 812        "mtu_value_is_too_small"
 813#define DEVLINK_TRAP_GENERIC_NAME_UNRESOLVED_NEIGH \
 814        "unresolved_neigh"
 815#define DEVLINK_TRAP_GENERIC_NAME_RPF \
 816        "mc_reverse_path_forwarding"
 817#define DEVLINK_TRAP_GENERIC_NAME_REJECT_ROUTE \
 818        "reject_route"
 819#define DEVLINK_TRAP_GENERIC_NAME_IPV4_LPM_UNICAST_MISS \
 820        "ipv4_lpm_miss"
 821#define DEVLINK_TRAP_GENERIC_NAME_IPV6_LPM_UNICAST_MISS \
 822        "ipv6_lpm_miss"
 823#define DEVLINK_TRAP_GENERIC_NAME_NON_ROUTABLE \
 824        "non_routable_packet"
 825#define DEVLINK_TRAP_GENERIC_NAME_DECAP_ERROR \
 826        "decap_error"
 827#define DEVLINK_TRAP_GENERIC_NAME_OVERLAY_SMAC_MC \
 828        "overlay_smac_is_mc"
 829#define DEVLINK_TRAP_GENERIC_NAME_INGRESS_FLOW_ACTION_DROP \
 830        "ingress_flow_action_drop"
 831#define DEVLINK_TRAP_GENERIC_NAME_EGRESS_FLOW_ACTION_DROP \
 832        "egress_flow_action_drop"
 833#define DEVLINK_TRAP_GENERIC_NAME_STP \
 834        "stp"
 835#define DEVLINK_TRAP_GENERIC_NAME_LACP \
 836        "lacp"
 837#define DEVLINK_TRAP_GENERIC_NAME_LLDP \
 838        "lldp"
 839#define DEVLINK_TRAP_GENERIC_NAME_IGMP_QUERY \
 840        "igmp_query"
 841#define DEVLINK_TRAP_GENERIC_NAME_IGMP_V1_REPORT \
 842        "igmp_v1_report"
 843#define DEVLINK_TRAP_GENERIC_NAME_IGMP_V2_REPORT \
 844        "igmp_v2_report"
 845#define DEVLINK_TRAP_GENERIC_NAME_IGMP_V3_REPORT \
 846        "igmp_v3_report"
 847#define DEVLINK_TRAP_GENERIC_NAME_IGMP_V2_LEAVE \
 848        "igmp_v2_leave"
 849#define DEVLINK_TRAP_GENERIC_NAME_MLD_QUERY \
 850        "mld_query"
 851#define DEVLINK_TRAP_GENERIC_NAME_MLD_V1_REPORT \
 852        "mld_v1_report"
 853#define DEVLINK_TRAP_GENERIC_NAME_MLD_V2_REPORT \
 854        "mld_v2_report"
 855#define DEVLINK_TRAP_GENERIC_NAME_MLD_V1_DONE \
 856        "mld_v1_done"
 857#define DEVLINK_TRAP_GENERIC_NAME_IPV4_DHCP \
 858        "ipv4_dhcp"
 859#define DEVLINK_TRAP_GENERIC_NAME_IPV6_DHCP \
 860        "ipv6_dhcp"
 861#define DEVLINK_TRAP_GENERIC_NAME_ARP_REQUEST \
 862        "arp_request"
 863#define DEVLINK_TRAP_GENERIC_NAME_ARP_RESPONSE \
 864        "arp_response"
 865#define DEVLINK_TRAP_GENERIC_NAME_ARP_OVERLAY \
 866        "arp_overlay"
 867#define DEVLINK_TRAP_GENERIC_NAME_IPV6_NEIGH_SOLICIT \
 868        "ipv6_neigh_solicit"
 869#define DEVLINK_TRAP_GENERIC_NAME_IPV6_NEIGH_ADVERT \
 870        "ipv6_neigh_advert"
 871#define DEVLINK_TRAP_GENERIC_NAME_IPV4_BFD \
 872        "ipv4_bfd"
 873#define DEVLINK_TRAP_GENERIC_NAME_IPV6_BFD \
 874        "ipv6_bfd"
 875#define DEVLINK_TRAP_GENERIC_NAME_IPV4_OSPF \
 876        "ipv4_ospf"
 877#define DEVLINK_TRAP_GENERIC_NAME_IPV6_OSPF \
 878        "ipv6_ospf"
 879#define DEVLINK_TRAP_GENERIC_NAME_IPV4_BGP \
 880        "ipv4_bgp"
 881#define DEVLINK_TRAP_GENERIC_NAME_IPV6_BGP \
 882        "ipv6_bgp"
 883#define DEVLINK_TRAP_GENERIC_NAME_IPV4_VRRP \
 884        "ipv4_vrrp"
 885#define DEVLINK_TRAP_GENERIC_NAME_IPV6_VRRP \
 886        "ipv6_vrrp"
 887#define DEVLINK_TRAP_GENERIC_NAME_IPV4_PIM \
 888        "ipv4_pim"
 889#define DEVLINK_TRAP_GENERIC_NAME_IPV6_PIM \
 890        "ipv6_pim"
 891#define DEVLINK_TRAP_GENERIC_NAME_UC_LB \
 892        "uc_loopback"
 893#define DEVLINK_TRAP_GENERIC_NAME_LOCAL_ROUTE \
 894        "local_route"
 895#define DEVLINK_TRAP_GENERIC_NAME_EXTERNAL_ROUTE \
 896        "external_route"
 897#define DEVLINK_TRAP_GENERIC_NAME_IPV6_UC_DIP_LINK_LOCAL_SCOPE \
 898        "ipv6_uc_dip_link_local_scope"
 899#define DEVLINK_TRAP_GENERIC_NAME_IPV6_DIP_ALL_NODES \
 900        "ipv6_dip_all_nodes"
 901#define DEVLINK_TRAP_GENERIC_NAME_IPV6_DIP_ALL_ROUTERS \
 902        "ipv6_dip_all_routers"
 903#define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_SOLICIT \
 904        "ipv6_router_solicit"
 905#define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_ADVERT \
 906        "ipv6_router_advert"
 907#define DEVLINK_TRAP_GENERIC_NAME_IPV6_REDIRECT \
 908        "ipv6_redirect"
 909#define DEVLINK_TRAP_GENERIC_NAME_IPV4_ROUTER_ALERT \
 910        "ipv4_router_alert"
 911#define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_ALERT \
 912        "ipv6_router_alert"
 913#define DEVLINK_TRAP_GENERIC_NAME_PTP_EVENT \
 914        "ptp_event"
 915#define DEVLINK_TRAP_GENERIC_NAME_PTP_GENERAL \
 916        "ptp_general"
 917#define DEVLINK_TRAP_GENERIC_NAME_FLOW_ACTION_SAMPLE \
 918        "flow_action_sample"
 919#define DEVLINK_TRAP_GENERIC_NAME_FLOW_ACTION_TRAP \
 920        "flow_action_trap"
 921#define DEVLINK_TRAP_GENERIC_NAME_EARLY_DROP \
 922        "early_drop"
 923
 924#define DEVLINK_TRAP_GROUP_GENERIC_NAME_L2_DROPS \
 925        "l2_drops"
 926#define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_DROPS \
 927        "l3_drops"
 928#define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_EXCEPTIONS \
 929        "l3_exceptions"
 930#define DEVLINK_TRAP_GROUP_GENERIC_NAME_BUFFER_DROPS \
 931        "buffer_drops"
 932#define DEVLINK_TRAP_GROUP_GENERIC_NAME_TUNNEL_DROPS \
 933        "tunnel_drops"
 934#define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_DROPS \
 935        "acl_drops"
 936#define DEVLINK_TRAP_GROUP_GENERIC_NAME_STP \
 937        "stp"
 938#define DEVLINK_TRAP_GROUP_GENERIC_NAME_LACP \
 939        "lacp"
 940#define DEVLINK_TRAP_GROUP_GENERIC_NAME_LLDP \
 941        "lldp"
 942#define DEVLINK_TRAP_GROUP_GENERIC_NAME_MC_SNOOPING  \
 943        "mc_snooping"
 944#define DEVLINK_TRAP_GROUP_GENERIC_NAME_DHCP \
 945        "dhcp"
 946#define DEVLINK_TRAP_GROUP_GENERIC_NAME_NEIGH_DISCOVERY \
 947        "neigh_discovery"
 948#define DEVLINK_TRAP_GROUP_GENERIC_NAME_BFD \
 949        "bfd"
 950#define DEVLINK_TRAP_GROUP_GENERIC_NAME_OSPF \
 951        "ospf"
 952#define DEVLINK_TRAP_GROUP_GENERIC_NAME_BGP \
 953        "bgp"
 954#define DEVLINK_TRAP_GROUP_GENERIC_NAME_VRRP \
 955        "vrrp"
 956#define DEVLINK_TRAP_GROUP_GENERIC_NAME_PIM \
 957        "pim"
 958#define DEVLINK_TRAP_GROUP_GENERIC_NAME_UC_LB \
 959        "uc_loopback"
 960#define DEVLINK_TRAP_GROUP_GENERIC_NAME_LOCAL_DELIVERY \
 961        "local_delivery"
 962#define DEVLINK_TRAP_GROUP_GENERIC_NAME_EXTERNAL_DELIVERY \
 963        "external_delivery"
 964#define DEVLINK_TRAP_GROUP_GENERIC_NAME_IPV6 \
 965        "ipv6"
 966#define DEVLINK_TRAP_GROUP_GENERIC_NAME_PTP_EVENT \
 967        "ptp_event"
 968#define DEVLINK_TRAP_GROUP_GENERIC_NAME_PTP_GENERAL \
 969        "ptp_general"
 970#define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_SAMPLE \
 971        "acl_sample"
 972#define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_TRAP \
 973        "acl_trap"
 974
 975#define DEVLINK_TRAP_GENERIC(_type, _init_action, _id, _group_id,             \
 976                             _metadata_cap)                                   \
 977        {                                                                     \
 978                .type = DEVLINK_TRAP_TYPE_##_type,                            \
 979                .init_action = DEVLINK_TRAP_ACTION_##_init_action,            \
 980                .generic = true,                                              \
 981                .id = DEVLINK_TRAP_GENERIC_ID_##_id,                          \
 982                .name = DEVLINK_TRAP_GENERIC_NAME_##_id,                      \
 983                .init_group_id = _group_id,                                   \
 984                .metadata_cap = _metadata_cap,                                \
 985        }
 986
 987#define DEVLINK_TRAP_DRIVER(_type, _init_action, _id, _name, _group_id,       \
 988                            _metadata_cap)                                    \
 989        {                                                                     \
 990                .type = DEVLINK_TRAP_TYPE_##_type,                            \
 991                .init_action = DEVLINK_TRAP_ACTION_##_init_action,            \
 992                .generic = false,                                             \
 993                .id = _id,                                                    \
 994                .name = _name,                                                \
 995                .init_group_id = _group_id,                                   \
 996                .metadata_cap = _metadata_cap,                                \
 997        }
 998
 999#define DEVLINK_TRAP_GROUP_GENERIC(_id, _policer_id)                          \
1000        {                                                                     \
1001                .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id,                \
1002                .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id,                    \
1003                .generic = true,                                              \
1004                .init_policer_id = _policer_id,                               \
1005        }
1006
1007#define DEVLINK_TRAP_POLICER(_id, _rate, _burst, _max_rate, _min_rate,        \
1008                             _max_burst, _min_burst)                          \
1009        {                                                                     \
1010                .id = _id,                                                    \
1011                .init_rate = _rate,                                           \
1012                .init_burst = _burst,                                         \
1013                .max_rate = _max_rate,                                        \
1014                .min_rate = _min_rate,                                        \
1015                .max_burst = _max_burst,                                      \
1016                .min_burst = _min_burst,                                      \
1017        }
1018
1019struct devlink_ops {
1020        int (*reload_down)(struct devlink *devlink, bool netns_change,
1021                           struct netlink_ext_ack *extack);
1022        int (*reload_up)(struct devlink *devlink,
1023                         struct netlink_ext_ack *extack);
1024        int (*port_type_set)(struct devlink_port *devlink_port,
1025                             enum devlink_port_type port_type);
1026        int (*port_split)(struct devlink *devlink, unsigned int port_index,
1027                          unsigned int count, struct netlink_ext_ack *extack);
1028        int (*port_unsplit)(struct devlink *devlink, unsigned int port_index,
1029                            struct netlink_ext_ack *extack);
1030        int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
1031                           u16 pool_index,
1032                           struct devlink_sb_pool_info *pool_info);
1033        int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
1034                           u16 pool_index, u32 size,
1035                           enum devlink_sb_threshold_type threshold_type,
1036                           struct netlink_ext_ack *extack);
1037        int (*sb_port_pool_get)(struct devlink_port *devlink_port,
1038                                unsigned int sb_index, u16 pool_index,
1039                                u32 *p_threshold);
1040        int (*sb_port_pool_set)(struct devlink_port *devlink_port,
1041                                unsigned int sb_index, u16 pool_index,
1042                                u32 threshold, struct netlink_ext_ack *extack);
1043        int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
1044                                   unsigned int sb_index,
1045                                   u16 tc_index,
1046                                   enum devlink_sb_pool_type pool_type,
1047                                   u16 *p_pool_index, u32 *p_threshold);
1048        int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
1049                                   unsigned int sb_index,
1050                                   u16 tc_index,
1051                                   enum devlink_sb_pool_type pool_type,
1052                                   u16 pool_index, u32 threshold,
1053                                   struct netlink_ext_ack *extack);
1054        int (*sb_occ_snapshot)(struct devlink *devlink,
1055                               unsigned int sb_index);
1056        int (*sb_occ_max_clear)(struct devlink *devlink,
1057                                unsigned int sb_index);
1058        int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
1059                                    unsigned int sb_index, u16 pool_index,
1060                                    u32 *p_cur, u32 *p_max);
1061        int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
1062                                       unsigned int sb_index,
1063                                       u16 tc_index,
1064                                       enum devlink_sb_pool_type pool_type,
1065                                       u32 *p_cur, u32 *p_max);
1066
1067        int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
1068        int (*eswitch_mode_set)(struct devlink *devlink, u16 mode,
1069                                struct netlink_ext_ack *extack);
1070        int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
1071        int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode,
1072                                       struct netlink_ext_ack *extack);
1073        int (*eswitch_encap_mode_get)(struct devlink *devlink,
1074                                      enum devlink_eswitch_encap_mode *p_encap_mode);
1075        int (*eswitch_encap_mode_set)(struct devlink *devlink,
1076                                      enum devlink_eswitch_encap_mode encap_mode,
1077                                      struct netlink_ext_ack *extack);
1078        int (*info_get)(struct devlink *devlink, struct devlink_info_req *req,
1079                        struct netlink_ext_ack *extack);
1080        int (*flash_update)(struct devlink *devlink, const char *file_name,
1081                            const char *component,
1082                            struct netlink_ext_ack *extack);
1083        /**
1084         * @trap_init: Trap initialization function.
1085         *
1086         * Should be used by device drivers to initialize the trap in the
1087         * underlying device. Drivers should also store the provided trap
1088         * context, so that they could efficiently pass it to
1089         * devlink_trap_report() when the trap is triggered.
1090         */
1091        int (*trap_init)(struct devlink *devlink,
1092                         const struct devlink_trap *trap, void *trap_ctx);
1093        /**
1094         * @trap_fini: Trap de-initialization function.
1095         *
1096         * Should be used by device drivers to de-initialize the trap in the
1097         * underlying device.
1098         */
1099        void (*trap_fini)(struct devlink *devlink,
1100                          const struct devlink_trap *trap, void *trap_ctx);
1101        /**
1102         * @trap_action_set: Trap action set function.
1103         */
1104        int (*trap_action_set)(struct devlink *devlink,
1105                               const struct devlink_trap *trap,
1106                               enum devlink_trap_action action,
1107                               struct netlink_ext_ack *extack);
1108        /**
1109         * @trap_group_init: Trap group initialization function.
1110         *
1111         * Should be used by device drivers to initialize the trap group in the
1112         * underlying device.
1113         */
1114        int (*trap_group_init)(struct devlink *devlink,
1115                               const struct devlink_trap_group *group);
1116        /**
1117         * @trap_group_set: Trap group parameters set function.
1118         *
1119         * Note: @policer can be NULL when a policer is being unbound from
1120         * @group.
1121         */
1122        int (*trap_group_set)(struct devlink *devlink,
1123                              const struct devlink_trap_group *group,
1124                              const struct devlink_trap_policer *policer,
1125                              struct netlink_ext_ack *extack);
1126        /**
1127         * @trap_policer_init: Trap policer initialization function.
1128         *
1129         * Should be used by device drivers to initialize the trap policer in
1130         * the underlying device.
1131         */
1132        int (*trap_policer_init)(struct devlink *devlink,
1133                                 const struct devlink_trap_policer *policer);
1134        /**
1135         * @trap_policer_fini: Trap policer de-initialization function.
1136         *
1137         * Should be used by device drivers to de-initialize the trap policer
1138         * in the underlying device.
1139         */
1140        void (*trap_policer_fini)(struct devlink *devlink,
1141                                  const struct devlink_trap_policer *policer);
1142        /**
1143         * @trap_policer_set: Trap policer parameters set function.
1144         */
1145        int (*trap_policer_set)(struct devlink *devlink,
1146                                const struct devlink_trap_policer *policer,
1147                                u64 rate, u64 burst,
1148                                struct netlink_ext_ack *extack);
1149        /**
1150         * @trap_policer_counter_get: Trap policer counter get function.
1151         *
1152         * Should be used by device drivers to report number of packets dropped
1153         * by the policer.
1154         */
1155        int (*trap_policer_counter_get)(struct devlink *devlink,
1156                                        const struct devlink_trap_policer *policer,
1157                                        u64 *p_drops);
1158        /**
1159         * @port_function_hw_addr_get: Port function's hardware address get function.
1160         *
1161         * Should be used by device drivers to report the hardware address of a function managed
1162         * by the devlink port. Driver should return -EOPNOTSUPP if it doesn't support port
1163         * function handling for a particular port.
1164         *
1165         * Note: @extack can be NULL when port notifier queries the port function.
1166         */
1167        int (*port_function_hw_addr_get)(struct devlink *devlink, struct devlink_port *port,
1168                                         u8 *hw_addr, int *hw_addr_len,
1169                                         struct netlink_ext_ack *extack);
1170        /**
1171         * @port_function_hw_addr_set: Port function's hardware address set function.
1172         *
1173         * Should be used by device drivers to set the hardware address of a function managed
1174         * by the devlink port. Driver should return -EOPNOTSUPP if it doesn't support port
1175         * function handling for a particular port.
1176         */
1177        int (*port_function_hw_addr_set)(struct devlink *devlink, struct devlink_port *port,
1178                                         const u8 *hw_addr, int hw_addr_len,
1179                                         struct netlink_ext_ack *extack);
1180};
1181
1182static inline void *devlink_priv(struct devlink *devlink)
1183{
1184        BUG_ON(!devlink);
1185        return &devlink->priv;
1186}
1187
1188static inline struct devlink *priv_to_devlink(void *priv)
1189{
1190        BUG_ON(!priv);
1191        return container_of(priv, struct devlink, priv);
1192}
1193
1194static inline struct devlink_port *
1195netdev_to_devlink_port(struct net_device *dev)
1196{
1197        if (dev->netdev_ops->ndo_get_devlink_port)
1198                return dev->netdev_ops->ndo_get_devlink_port(dev);
1199        return NULL;
1200}
1201
1202static inline struct devlink *netdev_to_devlink(struct net_device *dev)
1203{
1204        struct devlink_port *devlink_port = netdev_to_devlink_port(dev);
1205
1206        if (devlink_port)
1207                return devlink_port->devlink;
1208        return NULL;
1209}
1210
1211struct ib_device;
1212
1213struct net *devlink_net(const struct devlink *devlink);
1214void devlink_net_set(struct devlink *devlink, struct net *net);
1215struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
1216int devlink_register(struct devlink *devlink, struct device *dev);
1217void devlink_unregister(struct devlink *devlink);
1218void devlink_reload_enable(struct devlink *devlink);
1219void devlink_reload_disable(struct devlink *devlink);
1220void devlink_free(struct devlink *devlink);
1221int devlink_port_register(struct devlink *devlink,
1222                          struct devlink_port *devlink_port,
1223                          unsigned int port_index);
1224void devlink_port_unregister(struct devlink_port *devlink_port);
1225void devlink_port_type_eth_set(struct devlink_port *devlink_port,
1226                               struct net_device *netdev);
1227void devlink_port_type_ib_set(struct devlink_port *devlink_port,
1228                              struct ib_device *ibdev);
1229void devlink_port_type_clear(struct devlink_port *devlink_port);
1230void devlink_port_attrs_set(struct devlink_port *devlink_port,
1231                            struct devlink_port_attrs *devlink_port_attrs);
1232void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u16 pf);
1233void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port,
1234                                   u16 pf, u16 vf);
1235int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
1236                        u32 size, u16 ingress_pools_count,
1237                        u16 egress_pools_count, u16 ingress_tc_count,
1238                        u16 egress_tc_count);
1239void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
1240int devlink_dpipe_table_register(struct devlink *devlink,
1241                                 const char *table_name,
1242                                 struct devlink_dpipe_table_ops *table_ops,
1243                                 void *priv, bool counter_control_extern);
1244void devlink_dpipe_table_unregister(struct devlink *devlink,
1245                                    const char *table_name);
1246int devlink_dpipe_headers_register(struct devlink *devlink,
1247                                   struct devlink_dpipe_headers *dpipe_headers);
1248void devlink_dpipe_headers_unregister(struct devlink *devlink);
1249bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
1250                                         const char *table_name);
1251int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx);
1252int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
1253                                   struct devlink_dpipe_entry *entry);
1254int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx);
1255void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry);
1256int devlink_dpipe_action_put(struct sk_buff *skb,
1257                             struct devlink_dpipe_action *action);
1258int devlink_dpipe_match_put(struct sk_buff *skb,
1259                            struct devlink_dpipe_match *match);
1260extern struct devlink_dpipe_header devlink_dpipe_header_ethernet;
1261extern struct devlink_dpipe_header devlink_dpipe_header_ipv4;
1262extern struct devlink_dpipe_header devlink_dpipe_header_ipv6;
1263
1264int devlink_resource_register(struct devlink *devlink,
1265                              const char *resource_name,
1266                              u64 resource_size,
1267                              u64 resource_id,
1268                              u64 parent_resource_id,
1269                              const struct devlink_resource_size_params *size_params);
1270void devlink_resources_unregister(struct devlink *devlink,
1271                                  struct devlink_resource *resource);
1272int devlink_resource_size_get(struct devlink *devlink,
1273                              u64 resource_id,
1274                              u64 *p_resource_size);
1275int devlink_dpipe_table_resource_set(struct devlink *devlink,
1276                                     const char *table_name, u64 resource_id,
1277                                     u64 resource_units);
1278void devlink_resource_occ_get_register(struct devlink *devlink,
1279                                       u64 resource_id,
1280                                       devlink_resource_occ_get_t *occ_get,
1281                                       void *occ_get_priv);
1282void devlink_resource_occ_get_unregister(struct devlink *devlink,
1283                                         u64 resource_id);
1284int devlink_params_register(struct devlink *devlink,
1285                            const struct devlink_param *params,
1286                            size_t params_count);
1287void devlink_params_unregister(struct devlink *devlink,
1288                               const struct devlink_param *params,
1289                               size_t params_count);
1290void devlink_params_publish(struct devlink *devlink);
1291void devlink_params_unpublish(struct devlink *devlink);
1292int devlink_port_params_register(struct devlink_port *devlink_port,
1293                                 const struct devlink_param *params,
1294                                 size_t params_count);
1295void devlink_port_params_unregister(struct devlink_port *devlink_port,
1296                                    const struct devlink_param *params,
1297                                    size_t params_count);
1298int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
1299                                       union devlink_param_value *init_val);
1300int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
1301                                       union devlink_param_value init_val);
1302int
1303devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
1304                                        u32 param_id,
1305                                        union devlink_param_value *init_val);
1306int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
1307                                            u32 param_id,
1308                                            union devlink_param_value init_val);
1309void devlink_param_value_changed(struct devlink *devlink, u32 param_id);
1310void devlink_port_param_value_changed(struct devlink_port *devlink_port,
1311                                      u32 param_id);
1312void devlink_param_value_str_fill(union devlink_param_value *dst_val,
1313                                  const char *src);
1314struct devlink_region *
1315devlink_region_create(struct devlink *devlink,
1316                      const struct devlink_region_ops *ops,
1317                      u32 region_max_snapshots, u64 region_size);
1318void devlink_region_destroy(struct devlink_region *region);
1319int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id);
1320void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id);
1321int devlink_region_snapshot_create(struct devlink_region *region,
1322                                   u8 *data, u32 snapshot_id);
1323int devlink_info_serial_number_put(struct devlink_info_req *req,
1324                                   const char *sn);
1325int devlink_info_driver_name_put(struct devlink_info_req *req,
1326                                 const char *name);
1327int devlink_info_board_serial_number_put(struct devlink_info_req *req,
1328                                         const char *bsn);
1329int devlink_info_version_fixed_put(struct devlink_info_req *req,
1330                                   const char *version_name,
1331                                   const char *version_value);
1332int devlink_info_version_stored_put(struct devlink_info_req *req,
1333                                    const char *version_name,
1334                                    const char *version_value);
1335int devlink_info_version_running_put(struct devlink_info_req *req,
1336                                     const char *version_name,
1337                                     const char *version_value);
1338
1339int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg);
1340int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg);
1341
1342int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name);
1343int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg);
1344
1345int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
1346                                     const char *name);
1347int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg);
1348int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
1349                                        const char *name);
1350int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg);
1351
1352int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value);
1353int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value);
1354int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value);
1355int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value);
1356int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value);
1357int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
1358                            u16 value_len);
1359
1360int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
1361                               bool value);
1362int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
1363                             u8 value);
1364int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
1365                              u32 value);
1366int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
1367                              u64 value);
1368int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
1369                                 const char *value);
1370int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
1371                                 const void *value, u32 value_len);
1372
1373struct devlink_health_reporter *
1374devlink_health_reporter_create(struct devlink *devlink,
1375                               const struct devlink_health_reporter_ops *ops,
1376                               u64 graceful_period, void *priv);
1377
1378struct devlink_health_reporter *
1379devlink_port_health_reporter_create(struct devlink_port *port,
1380                                    const struct devlink_health_reporter_ops *ops,
1381                                    u64 graceful_period, void *priv);
1382
1383void
1384devlink_health_reporter_destroy(struct devlink_health_reporter *reporter);
1385
1386void
1387devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter);
1388
1389void *
1390devlink_health_reporter_priv(struct devlink_health_reporter *reporter);
1391int devlink_health_report(struct devlink_health_reporter *reporter,
1392                          const char *msg, void *priv_ctx);
1393void
1394devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
1395                                     enum devlink_health_reporter_state state);
1396void
1397devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter);
1398
1399bool devlink_is_reload_failed(const struct devlink *devlink);
1400
1401void devlink_flash_update_begin_notify(struct devlink *devlink);
1402void devlink_flash_update_end_notify(struct devlink *devlink);
1403void devlink_flash_update_status_notify(struct devlink *devlink,
1404                                        const char *status_msg,
1405                                        const char *component,
1406                                        unsigned long done,
1407                                        unsigned long total);
1408void devlink_flash_update_timeout_notify(struct devlink *devlink,
1409                                         const char *status_msg,
1410                                         const char *component,
1411                                         unsigned long timeout);
1412
1413int devlink_traps_register(struct devlink *devlink,
1414                           const struct devlink_trap *traps,
1415                           size_t traps_count, void *priv);
1416void devlink_traps_unregister(struct devlink *devlink,
1417                              const struct devlink_trap *traps,
1418                              size_t traps_count);
1419void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
1420                         void *trap_ctx, struct devlink_port *in_devlink_port,
1421                         const struct flow_action_cookie *fa_cookie);
1422void *devlink_trap_ctx_priv(void *trap_ctx);
1423int devlink_trap_groups_register(struct devlink *devlink,
1424                                 const struct devlink_trap_group *groups,
1425                                 size_t groups_count);
1426void devlink_trap_groups_unregister(struct devlink *devlink,
1427                                    const struct devlink_trap_group *groups,
1428                                    size_t groups_count);
1429int
1430devlink_trap_policers_register(struct devlink *devlink,
1431                               const struct devlink_trap_policer *policers,
1432                               size_t policers_count);
1433void
1434devlink_trap_policers_unregister(struct devlink *devlink,
1435                                 const struct devlink_trap_policer *policers,
1436                                 size_t policers_count);
1437
1438#if IS_ENABLED(CONFIG_NET_DEVLINK)
1439
1440void devlink_compat_running_version(struct net_device *dev,
1441                                    char *buf, size_t len);
1442int devlink_compat_flash_update(struct net_device *dev, const char *file_name);
1443int devlink_compat_phys_port_name_get(struct net_device *dev,
1444                                      char *name, size_t len);
1445int devlink_compat_switch_id_get(struct net_device *dev,
1446                                 struct netdev_phys_item_id *ppid);
1447
1448#else
1449
1450static inline void
1451devlink_compat_running_version(struct net_device *dev, char *buf, size_t len)
1452{
1453}
1454
1455static inline int
1456devlink_compat_flash_update(struct net_device *dev, const char *file_name)
1457{
1458        return -EOPNOTSUPP;
1459}
1460
1461static inline int
1462devlink_compat_phys_port_name_get(struct net_device *dev,
1463                                  char *name, size_t len)
1464{
1465        return -EOPNOTSUPP;
1466}
1467
1468static inline int
1469devlink_compat_switch_id_get(struct net_device *dev,
1470                             struct netdev_phys_item_id *ppid)
1471{
1472        return -EOPNOTSUPP;
1473}
1474
1475#endif
1476
1477#endif /* _NET_DEVLINK_H_ */
1478