0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/debugfs.h>
0017 #include <linux/device.h>
0018 #include <linux/ethtool.h>
0019 #include <linux/kernel.h>
0020 #include <linux/list.h>
0021 #include <linux/netdevice.h>
0022 #include <linux/u64_stats_sync.h>
0023 #include <net/devlink.h>
0024 #include <net/udp_tunnel.h>
0025 #include <net/xdp.h>
0026
0027 #define DRV_NAME "netdevsim"
0028
0029 #define NSIM_XDP_MAX_MTU 4000
0030
0031 #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
0032
0033 #define NSIM_IPSEC_MAX_SA_COUNT 33
0034 #define NSIM_IPSEC_VALID BIT(31)
0035 #define NSIM_UDP_TUNNEL_N_PORTS 4
0036
0037 struct nsim_sa {
0038 struct xfrm_state *xs;
0039 __be32 ipaddr[4];
0040 u32 key[4];
0041 u32 salt;
0042 bool used;
0043 bool crypt;
0044 bool rx;
0045 };
0046
0047 struct nsim_ipsec {
0048 struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
0049 struct dentry *pfile;
0050 u32 count;
0051 u32 tx;
0052 u32 ok;
0053 };
0054
0055 struct nsim_ethtool_pauseparam {
0056 bool rx;
0057 bool tx;
0058 bool report_stats_rx;
0059 bool report_stats_tx;
0060 };
0061
0062 struct nsim_ethtool {
0063 u32 get_err;
0064 u32 set_err;
0065 u32 channels;
0066 struct nsim_ethtool_pauseparam pauseparam;
0067 struct ethtool_coalesce coalesce;
0068 struct ethtool_ringparam ring;
0069 struct ethtool_fecparam fec;
0070 };
0071
0072 struct netdevsim {
0073 struct net_device *netdev;
0074 struct nsim_dev *nsim_dev;
0075 struct nsim_dev_port *nsim_dev_port;
0076
0077 u64 tx_packets;
0078 u64 tx_bytes;
0079 struct u64_stats_sync syncp;
0080
0081 struct nsim_bus_dev *nsim_bus_dev;
0082
0083 struct bpf_prog *bpf_offloaded;
0084 u32 bpf_offloaded_id;
0085
0086 struct xdp_attachment_info xdp;
0087 struct xdp_attachment_info xdp_hw;
0088
0089 bool bpf_tc_accept;
0090 bool bpf_tc_non_bound_accept;
0091 bool bpf_xdpdrv_accept;
0092 bool bpf_xdpoffload_accept;
0093
0094 bool bpf_map_accept;
0095 struct nsim_ipsec ipsec;
0096 struct {
0097 u32 inject_error;
0098 u32 sleep;
0099 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
0100 u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS];
0101 struct debugfs_u32_array dfs_ports[2];
0102 } udp_ports;
0103
0104 struct nsim_ethtool ethtool;
0105 };
0106
0107 struct netdevsim *
0108 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
0109 void nsim_destroy(struct netdevsim *ns);
0110
0111 void nsim_ethtool_init(struct netdevsim *ns);
0112
0113 void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev);
0114 int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
0115 struct net_device *dev);
0116 void nsim_udp_tunnels_info_destroy(struct net_device *dev);
0117
0118 #ifdef CONFIG_BPF_SYSCALL
0119 int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
0120 void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
0121 int nsim_bpf_init(struct netdevsim *ns);
0122 void nsim_bpf_uninit(struct netdevsim *ns);
0123 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
0124 int nsim_bpf_disable_tc(struct netdevsim *ns);
0125 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
0126 void *type_data, void *cb_priv);
0127 #else
0128
0129 static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
0130 {
0131 return 0;
0132 }
0133
0134 static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
0135 {
0136 }
0137 static inline int nsim_bpf_init(struct netdevsim *ns)
0138 {
0139 return 0;
0140 }
0141
0142 static inline void nsim_bpf_uninit(struct netdevsim *ns)
0143 {
0144 }
0145
0146 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
0147 {
0148 return -EOPNOTSUPP;
0149 }
0150
0151 static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
0152 {
0153 return 0;
0154 }
0155
0156 static inline int
0157 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
0158 void *cb_priv)
0159 {
0160 return -EOPNOTSUPP;
0161 }
0162 #endif
0163
0164 enum nsim_resource_id {
0165 NSIM_RESOURCE_NONE,
0166 NSIM_RESOURCE_IPV4,
0167 NSIM_RESOURCE_IPV4_FIB,
0168 NSIM_RESOURCE_IPV4_FIB_RULES,
0169 NSIM_RESOURCE_IPV6,
0170 NSIM_RESOURCE_IPV6_FIB,
0171 NSIM_RESOURCE_IPV6_FIB_RULES,
0172 NSIM_RESOURCE_NEXTHOPS,
0173 };
0174
0175 struct nsim_dev_health {
0176 struct devlink_health_reporter *empty_reporter;
0177 struct devlink_health_reporter *dummy_reporter;
0178 struct dentry *ddir;
0179 char *recovered_break_msg;
0180 u32 binary_len;
0181 bool fail_recover;
0182 };
0183
0184 int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
0185 void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
0186
0187 struct nsim_dev_hwstats_netdev {
0188 struct list_head list;
0189 struct net_device *netdev;
0190 struct rtnl_hw_stats64 stats;
0191 bool enabled;
0192 bool fail_enable;
0193 };
0194
0195 struct nsim_dev_hwstats {
0196 struct dentry *ddir;
0197 struct dentry *l3_ddir;
0198
0199 struct mutex hwsdev_list_lock;
0200 struct list_head l3_list;
0201
0202 struct notifier_block netdevice_nb;
0203 struct delayed_work traffic_dw;
0204 };
0205
0206 int nsim_dev_hwstats_init(struct nsim_dev *nsim_dev);
0207 void nsim_dev_hwstats_exit(struct nsim_dev *nsim_dev);
0208
0209 #if IS_ENABLED(CONFIG_PSAMPLE)
0210 int nsim_dev_psample_init(struct nsim_dev *nsim_dev);
0211 void nsim_dev_psample_exit(struct nsim_dev *nsim_dev);
0212 #else
0213 static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
0214 {
0215 return 0;
0216 }
0217
0218 static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
0219 {
0220 }
0221 #endif
0222
0223 enum nsim_dev_port_type {
0224 NSIM_DEV_PORT_TYPE_PF,
0225 NSIM_DEV_PORT_TYPE_VF,
0226 };
0227
0228 #define NSIM_DEV_VF_PORT_INDEX_BASE 128
0229 #define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX
0230
0231 struct nsim_dev_port {
0232 struct list_head list;
0233 struct devlink_port devlink_port;
0234 unsigned int port_index;
0235 enum nsim_dev_port_type port_type;
0236 struct dentry *ddir;
0237 struct dentry *rate_parent;
0238 char *parent_name;
0239 struct netdevsim *ns;
0240 };
0241
0242 struct nsim_vf_config {
0243 int link_state;
0244 u16 min_tx_rate;
0245 u16 max_tx_rate;
0246 u16 vlan;
0247 __be16 vlan_proto;
0248 u16 qos;
0249 u8 vf_mac[ETH_ALEN];
0250 bool spoofchk_enabled;
0251 bool trusted;
0252 bool rss_query_enabled;
0253 };
0254
0255 struct nsim_dev {
0256 struct nsim_bus_dev *nsim_bus_dev;
0257 struct nsim_fib_data *fib_data;
0258 struct nsim_trap_data *trap_data;
0259 struct dentry *ddir;
0260 struct dentry *ports_ddir;
0261 struct dentry *take_snapshot;
0262 struct dentry *nodes_ddir;
0263
0264 struct nsim_vf_config *vfconfigs;
0265
0266 struct bpf_offload_dev *bpf_dev;
0267 bool bpf_bind_accept;
0268 bool bpf_bind_verifier_accept;
0269 u32 bpf_bind_verifier_delay;
0270 struct dentry *ddir_bpf_bound_progs;
0271 u32 prog_id_gen;
0272 struct list_head bpf_bound_progs;
0273 struct list_head bpf_bound_maps;
0274 struct netdev_phys_item_id switch_id;
0275 struct list_head port_list;
0276 bool fw_update_status;
0277 u32 fw_update_overwrite_mask;
0278 u32 max_macs;
0279 bool test1;
0280 bool dont_allow_reload;
0281 bool fail_reload;
0282 struct devlink_region *dummy_region;
0283 struct nsim_dev_health health;
0284 struct nsim_dev_hwstats hwstats;
0285 struct flow_action_cookie *fa_cookie;
0286 spinlock_t fa_cookie_lock;
0287 bool fail_trap_group_set;
0288 bool fail_trap_policer_set;
0289 bool fail_trap_policer_counter_get;
0290 bool fail_trap_drop_counter_get;
0291 struct {
0292 struct udp_tunnel_nic_shared utn_shared;
0293 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
0294 bool sync_all;
0295 bool open_only;
0296 bool ipv4_only;
0297 bool shared;
0298 bool static_iana_vxlan;
0299 u32 sleep;
0300 } udp_ports;
0301 struct nsim_dev_psample *psample;
0302 u16 esw_mode;
0303 };
0304
0305 static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
0306 {
0307 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
0308 }
0309
0310 static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
0311 {
0312 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
0313 }
0314
0315 static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
0316 {
0317 return devlink_net(priv_to_devlink(nsim_dev));
0318 }
0319
0320 int nsim_dev_init(void);
0321 void nsim_dev_exit(void);
0322 int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev);
0323 void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev);
0324 int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev,
0325 enum nsim_dev_port_type type,
0326 unsigned int port_index);
0327 int nsim_drv_port_del(struct nsim_bus_dev *nsim_bus_dev,
0328 enum nsim_dev_port_type type,
0329 unsigned int port_index);
0330 int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
0331 unsigned int num_vfs);
0332
0333 unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);
0334
0335 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
0336 struct netlink_ext_ack *extack);
0337 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
0338 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
0339 enum nsim_resource_id res_id, bool max);
0340
0341 static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
0342 {
0343 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;
0344 }
0345
0346 static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port)
0347 {
0348 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF;
0349 }
0350 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
0351 void nsim_ipsec_init(struct netdevsim *ns);
0352 void nsim_ipsec_teardown(struct netdevsim *ns);
0353 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
0354 #else
0355 static inline void nsim_ipsec_init(struct netdevsim *ns)
0356 {
0357 }
0358
0359 static inline void nsim_ipsec_teardown(struct netdevsim *ns)
0360 {
0361 }
0362
0363 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
0364 {
0365 return true;
0366 }
0367 #endif
0368
0369 struct nsim_bus_dev {
0370 struct device dev;
0371 struct list_head list;
0372 unsigned int port_count;
0373 unsigned int num_queues;
0374 struct net *initial_net;
0375
0376
0377 unsigned int max_vfs;
0378 unsigned int num_vfs;
0379 bool init;
0380 };
0381
0382 int nsim_bus_init(void);
0383 void nsim_bus_exit(void);