linux/drivers/net/netdevsim/netdevsim.h
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2017 Netronome Systems, Inc.
   3 *
   4 * This software is licensed under the GNU General License Version 2,
   5 * June 1991 as shown in the file COPYING in the top-level directory of this
   6 * source tree.
   7 *
   8 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
   9 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  10 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  11 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
  12 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
  13 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  14 */
  15
  16#include <linux/device.h>
  17#include <linux/kernel.h>
  18#include <linux/list.h>
  19#include <linux/netdevice.h>
  20#include <linux/u64_stats_sync.h>
  21#include <net/devlink.h>
  22#include <net/xdp.h>
  23
  24#define DRV_NAME        "netdevsim"
  25
  26#define NSIM_XDP_MAX_MTU        4000
  27
  28#define NSIM_EA(extack, msg)    NL_SET_ERR_MSG_MOD((extack), msg)
  29
  30#define NSIM_IPSEC_MAX_SA_COUNT         33
  31#define NSIM_IPSEC_VALID                BIT(31)
  32
  33struct nsim_sa {
  34        struct xfrm_state *xs;
  35        __be32 ipaddr[4];
  36        u32 key[4];
  37        u32 salt;
  38        bool used;
  39        bool crypt;
  40        bool rx;
  41};
  42
  43struct nsim_ipsec {
  44        struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
  45        struct dentry *pfile;
  46        u32 count;
  47        u32 tx;
  48        u32 ok;
  49};
  50
  51struct netdevsim {
  52        struct net_device *netdev;
  53        struct nsim_dev *nsim_dev;
  54        struct nsim_dev_port *nsim_dev_port;
  55
  56        u64 tx_packets;
  57        u64 tx_bytes;
  58        struct u64_stats_sync syncp;
  59
  60        struct nsim_bus_dev *nsim_bus_dev;
  61
  62        struct bpf_prog *bpf_offloaded;
  63        u32 bpf_offloaded_id;
  64
  65        struct xdp_attachment_info xdp;
  66        struct xdp_attachment_info xdp_hw;
  67
  68        bool bpf_tc_accept;
  69        bool bpf_tc_non_bound_accept;
  70        bool bpf_xdpdrv_accept;
  71        bool bpf_xdpoffload_accept;
  72
  73        bool bpf_map_accept;
  74        struct nsim_ipsec ipsec;
  75};
  76
  77struct netdevsim *
  78nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
  79void nsim_destroy(struct netdevsim *ns);
  80
  81#ifdef CONFIG_BPF_SYSCALL
  82int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
  83void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
  84int nsim_bpf_init(struct netdevsim *ns);
  85void nsim_bpf_uninit(struct netdevsim *ns);
  86int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
  87int nsim_bpf_disable_tc(struct netdevsim *ns);
  88int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
  89                               void *type_data, void *cb_priv);
  90#else
  91
  92static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
  93{
  94        return 0;
  95}
  96
  97static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
  98{
  99}
 100static inline int nsim_bpf_init(struct netdevsim *ns)
 101{
 102        return 0;
 103}
 104
 105static inline void nsim_bpf_uninit(struct netdevsim *ns)
 106{
 107}
 108
 109static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
 110{
 111        return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP;
 112}
 113
 114static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
 115{
 116        return 0;
 117}
 118
 119static inline int
 120nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
 121                           void *cb_priv)
 122{
 123        return -EOPNOTSUPP;
 124}
 125#endif
 126
 127enum nsim_resource_id {
 128        NSIM_RESOURCE_NONE,   /* DEVLINK_RESOURCE_ID_PARENT_TOP */
 129        NSIM_RESOURCE_IPV4,
 130        NSIM_RESOURCE_IPV4_FIB,
 131        NSIM_RESOURCE_IPV4_FIB_RULES,
 132        NSIM_RESOURCE_IPV6,
 133        NSIM_RESOURCE_IPV6_FIB,
 134        NSIM_RESOURCE_IPV6_FIB_RULES,
 135};
 136
 137struct nsim_dev_health {
 138        struct devlink_health_reporter *empty_reporter;
 139        struct devlink_health_reporter *dummy_reporter;
 140        struct dentry *ddir;
 141        char *recovered_break_msg;
 142        u32 binary_len;
 143        bool fail_recover;
 144};
 145
 146int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
 147void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
 148
 149struct nsim_dev_port {
 150        struct list_head list;
 151        struct devlink_port devlink_port;
 152        unsigned int port_index;
 153        struct dentry *ddir;
 154        struct netdevsim *ns;
 155};
 156
 157struct nsim_dev {
 158        struct nsim_bus_dev *nsim_bus_dev;
 159        struct nsim_fib_data *fib_data;
 160        struct nsim_trap_data *trap_data;
 161        struct dentry *ddir;
 162        struct dentry *ports_ddir;
 163        struct dentry *take_snapshot;
 164        struct bpf_offload_dev *bpf_dev;
 165        bool bpf_bind_accept;
 166        u32 bpf_bind_verifier_delay;
 167        struct dentry *ddir_bpf_bound_progs;
 168        u32 prog_id_gen;
 169        struct list_head bpf_bound_progs;
 170        struct list_head bpf_bound_maps;
 171        struct netdev_phys_item_id switch_id;
 172        struct list_head port_list;
 173        struct mutex port_list_lock; /* protects port list */
 174        bool fw_update_status;
 175        u32 max_macs;
 176        bool test1;
 177        bool dont_allow_reload;
 178        bool fail_reload;
 179        struct devlink_region *dummy_region;
 180        struct nsim_dev_health health;
 181        struct flow_action_cookie *fa_cookie;
 182        spinlock_t fa_cookie_lock; /* protects fa_cookie */
 183        bool fail_trap_group_set;
 184        bool fail_trap_policer_set;
 185        bool fail_trap_policer_counter_get;
 186};
 187
 188static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
 189{
 190        return devlink_net(priv_to_devlink(nsim_dev));
 191}
 192
 193int nsim_dev_init(void);
 194void nsim_dev_exit(void);
 195int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev);
 196void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev);
 197int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
 198                      unsigned int port_index);
 199int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
 200                      unsigned int port_index);
 201
 202struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
 203                                      struct netlink_ext_ack *extack);
 204void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
 205u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
 206                     enum nsim_resource_id res_id, bool max);
 207
 208#if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
 209void nsim_ipsec_init(struct netdevsim *ns);
 210void nsim_ipsec_teardown(struct netdevsim *ns);
 211bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
 212#else
 213static inline void nsim_ipsec_init(struct netdevsim *ns)
 214{
 215}
 216
 217static inline void nsim_ipsec_teardown(struct netdevsim *ns)
 218{
 219}
 220
 221static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
 222{
 223        return true;
 224}
 225#endif
 226
 227struct nsim_vf_config {
 228        int link_state;
 229        u16 min_tx_rate;
 230        u16 max_tx_rate;
 231        u16 vlan;
 232        __be16 vlan_proto;
 233        u16 qos;
 234        u8 vf_mac[ETH_ALEN];
 235        bool spoofchk_enabled;
 236        bool trusted;
 237        bool rss_query_enabled;
 238};
 239
 240struct nsim_bus_dev {
 241        struct device dev;
 242        struct list_head list;
 243        unsigned int port_count;
 244        struct net *initial_net; /* Purpose of this is to carry net pointer
 245                                  * during the probe time only.
 246                                  */
 247        unsigned int num_vfs;
 248        struct nsim_vf_config *vfconfigs;
 249        /* Lock for devlink->reload_enabled in netdevsim module */
 250        struct mutex nsim_bus_reload_lock;
 251        bool init;
 252};
 253
 254int nsim_bus_init(void);
 255void nsim_bus_exit(void);
 256