at v6.18 12 kB view raw
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/debugfs.h> 17#include <linux/device.h> 18#include <linux/ethtool.h> 19#include <linux/ethtool_netlink.h> 20#include <linux/kernel.h> 21#include <linux/list.h> 22#include <linux/netdevice.h> 23#include <linux/ptp_mock.h> 24#include <linux/u64_stats_sync.h> 25#include <net/devlink.h> 26#include <net/udp_tunnel.h> 27#include <net/xdp.h> 28#include <net/macsec.h> 29 30#define DRV_NAME "netdevsim" 31 32#define NSIM_XDP_MAX_MTU 4000 33 34#define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg) 35 36#define NSIM_IPSEC_MAX_SA_COUNT 33 37#define NSIM_IPSEC_VALID BIT(31) 38#define NSIM_UDP_TUNNEL_N_PORTS 4 39 40#define NSIM_HDS_THRESHOLD_MAX 1024 41 42struct nsim_sa { 43 struct xfrm_state *xs; 44 __be32 ipaddr[4]; 45 u32 key[4]; 46 u32 salt; 47 bool used; 48 bool crypt; 49 bool rx; 50}; 51 52struct nsim_ipsec { 53 struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT]; 54 struct dentry *pfile; 55 u32 count; 56 u32 tx; 57}; 58 59#define NSIM_MACSEC_MAX_SECY_COUNT 3 60#define NSIM_MACSEC_MAX_RXSC_COUNT 1 61struct nsim_rxsc { 62 sci_t sci; 63 bool used; 64}; 65 66struct nsim_secy { 67 sci_t sci; 68 struct nsim_rxsc nsim_rxsc[NSIM_MACSEC_MAX_RXSC_COUNT]; 69 u8 nsim_rxsc_count; 70 bool used; 71}; 72 73struct nsim_macsec { 74 struct nsim_secy nsim_secy[NSIM_MACSEC_MAX_SECY_COUNT]; 75 u8 nsim_secy_count; 76}; 77 78struct nsim_ethtool_pauseparam { 79 bool rx; 80 bool tx; 81 bool report_stats_rx; 82 bool report_stats_tx; 83}; 84 85struct nsim_ethtool { 86 u32 get_err; 87 u32 set_err; 88 u32 channels; 89 struct nsim_ethtool_pauseparam pauseparam; 90 struct ethtool_coalesce coalesce; 91 struct ethtool_ringparam ring; 92 struct ethtool_fecparam fec; 93}; 94 95struct nsim_rq { 96 struct napi_struct napi; 97 struct sk_buff_head skb_queue; 98 struct page_pool *page_pool; 99 struct hrtimer napi_timer; 100}; 101 102struct netdevsim { 103 struct net_device *netdev; 104 struct nsim_dev *nsim_dev; 105 struct nsim_dev_port *nsim_dev_port; 106 struct mock_phc *phc; 107 struct nsim_rq **rq; 108 109 int rq_reset_mode; 110 111 struct { 112 struct psp_dev *dev; 113 u32 spi; 114 u32 assoc_cnt; 115 } psp; 116 117 struct nsim_bus_dev *nsim_bus_dev; 118 119 struct bpf_prog *bpf_offloaded; 120 u32 bpf_offloaded_id; 121 122 struct xdp_attachment_info xdp; 123 struct xdp_attachment_info xdp_hw; 124 125 bool bpf_tc_accept; 126 bool bpf_tc_non_bound_accept; 127 bool bpf_xdpdrv_accept; 128 bool bpf_xdpoffload_accept; 129 130 bool bpf_map_accept; 131 struct nsim_ipsec ipsec; 132 struct nsim_macsec macsec; 133 struct { 134 u32 inject_error; 135 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS]; 136 u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS]; 137 struct dentry *ddir; 138 struct debugfs_u32_array dfs_ports[2]; 139 } udp_ports; 140 141 struct page *page; 142 struct dentry *pp_dfs; 143 struct dentry *qr_dfs; 144 145 struct nsim_ethtool ethtool; 146 struct netdevsim __rcu *peer; 147 148 struct notifier_block nb; 149 struct netdev_net_notifier nn; 150}; 151 152struct netdevsim *nsim_create(struct nsim_dev *nsim_dev, 153 struct nsim_dev_port *nsim_dev_port, 154 u8 perm_addr[ETH_ALEN]); 155void nsim_destroy(struct netdevsim *ns); 156bool netdev_is_nsim(struct net_device *dev); 157 158void nsim_ethtool_init(struct netdevsim *ns); 159 160void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev); 161int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev, 162 struct net_device *dev); 163void nsim_udp_tunnels_info_destroy(struct net_device *dev); 164 165#ifdef CONFIG_BPF_SYSCALL 166int nsim_bpf_dev_init(struct nsim_dev *nsim_dev); 167void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev); 168int nsim_bpf_init(struct netdevsim *ns); 169void nsim_bpf_uninit(struct netdevsim *ns); 170int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf); 171int nsim_bpf_disable_tc(struct netdevsim *ns); 172int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, 173 void *type_data, void *cb_priv); 174#else 175 176static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev) 177{ 178 return 0; 179} 180 181static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev) 182{ 183} 184static inline int nsim_bpf_init(struct netdevsim *ns) 185{ 186 return 0; 187} 188 189static inline void nsim_bpf_uninit(struct netdevsim *ns) 190{ 191} 192 193static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) 194{ 195 return -EOPNOTSUPP; 196} 197 198static inline int nsim_bpf_disable_tc(struct netdevsim *ns) 199{ 200 return 0; 201} 202 203static inline int 204nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 205 void *cb_priv) 206{ 207 return -EOPNOTSUPP; 208} 209#endif 210 211enum nsim_resource_id { 212 NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */ 213 NSIM_RESOURCE_IPV4, 214 NSIM_RESOURCE_IPV4_FIB, 215 NSIM_RESOURCE_IPV4_FIB_RULES, 216 NSIM_RESOURCE_IPV6, 217 NSIM_RESOURCE_IPV6_FIB, 218 NSIM_RESOURCE_IPV6_FIB_RULES, 219 NSIM_RESOURCE_NEXTHOPS, 220}; 221 222struct nsim_dev_health { 223 struct devlink_health_reporter *empty_reporter; 224 struct devlink_health_reporter *dummy_reporter; 225 struct dentry *ddir; 226 char *recovered_break_msg; 227 u32 binary_len; 228 bool fail_recover; 229}; 230 231int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink); 232void nsim_dev_health_exit(struct nsim_dev *nsim_dev); 233 234struct nsim_dev_hwstats_netdev { 235 struct list_head list; 236 struct net_device *netdev; 237 struct rtnl_hw_stats64 stats; 238 bool enabled; 239 bool fail_enable; 240}; 241 242struct nsim_dev_hwstats { 243 struct dentry *ddir; 244 struct dentry *l3_ddir; 245 246 struct mutex hwsdev_list_lock; /* protects hwsdev list(s) */ 247 struct list_head l3_list; 248 249 struct notifier_block netdevice_nb; 250 struct delayed_work traffic_dw; 251}; 252 253int nsim_dev_hwstats_init(struct nsim_dev *nsim_dev); 254void nsim_dev_hwstats_exit(struct nsim_dev *nsim_dev); 255 256#if IS_ENABLED(CONFIG_PSAMPLE) 257int nsim_dev_psample_init(struct nsim_dev *nsim_dev); 258void nsim_dev_psample_exit(struct nsim_dev *nsim_dev); 259#else 260static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev) 261{ 262 return 0; 263} 264 265static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev) 266{ 267} 268#endif 269 270enum nsim_dev_port_type { 271 NSIM_DEV_PORT_TYPE_PF, 272 NSIM_DEV_PORT_TYPE_VF, 273}; 274 275#define NSIM_DEV_VF_PORT_INDEX_BASE 128 276#define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX 277 278struct nsim_dev_port { 279 struct list_head list; 280 struct devlink_port devlink_port; 281 unsigned int port_index; 282 enum nsim_dev_port_type port_type; 283 struct dentry *ddir; 284 struct dentry *rate_parent; 285 char *parent_name; 286 u32 tc_bw[DEVLINK_RATE_TCS_MAX]; 287 struct netdevsim *ns; 288}; 289 290struct nsim_vf_config { 291 int link_state; 292 u16 min_tx_rate; 293 u16 max_tx_rate; 294 u16 vlan; 295 __be16 vlan_proto; 296 u16 qos; 297 u8 vf_mac[ETH_ALEN]; 298 bool spoofchk_enabled; 299 bool trusted; 300 bool rss_query_enabled; 301}; 302 303struct nsim_dev { 304 struct nsim_bus_dev *nsim_bus_dev; 305 struct nsim_fib_data *fib_data; 306 struct nsim_trap_data *trap_data; 307 struct dentry *ddir; 308 struct dentry *ports_ddir; 309 struct dentry *take_snapshot; 310 struct dentry *nodes_ddir; 311 312 struct nsim_vf_config *vfconfigs; 313 314 struct bpf_offload_dev *bpf_dev; 315 bool bpf_bind_accept; 316 bool bpf_bind_verifier_accept; 317 u32 bpf_bind_verifier_delay; 318 struct dentry *ddir_bpf_bound_progs; 319 u32 prog_id_gen; 320 struct list_head bpf_bound_progs; 321 struct list_head bpf_bound_maps; 322 struct netdev_phys_item_id switch_id; 323 struct list_head port_list; 324 bool fw_update_status; 325 u32 fw_update_overwrite_mask; 326 u32 fw_update_flash_chunk_time_ms; 327 u32 max_macs; 328 bool test1; 329 bool dont_allow_reload; 330 bool fail_reload; 331 struct devlink_region *dummy_region; 332 struct nsim_dev_health health; 333 struct nsim_dev_hwstats hwstats; 334 struct flow_action_cookie *fa_cookie; 335 spinlock_t fa_cookie_lock; /* protects fa_cookie */ 336 bool fail_trap_group_set; 337 bool fail_trap_policer_set; 338 bool fail_trap_policer_counter_get; 339 bool fail_trap_drop_counter_get; 340 struct { 341 struct udp_tunnel_nic_shared utn_shared; 342 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS]; 343 bool sync_all; 344 bool open_only; 345 bool ipv4_only; 346 bool shared; 347 bool static_iana_vxlan; 348 } udp_ports; 349 struct nsim_dev_psample *psample; 350 u16 esw_mode; 351}; 352 353static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev) 354{ 355 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY; 356} 357 358static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev) 359{ 360 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV; 361} 362 363static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev) 364{ 365 return devlink_net(priv_to_devlink(nsim_dev)); 366} 367 368int nsim_dev_init(void); 369void nsim_dev_exit(void); 370int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev); 371void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev); 372int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev, 373 enum nsim_dev_port_type type, unsigned int port_index, 374 u8 perm_addr[ETH_ALEN]); 375int nsim_drv_port_del(struct nsim_bus_dev *nsim_bus_dev, 376 enum nsim_dev_port_type type, 377 unsigned int port_index); 378int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev, 379 unsigned int num_vfs); 380 381unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev); 382 383struct nsim_fib_data *nsim_fib_create(struct devlink *devlink, 384 struct netlink_ext_ack *extack); 385void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data); 386u64 nsim_fib_get_val(struct nsim_fib_data *fib_data, 387 enum nsim_resource_id res_id, bool max); 388 389static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port) 390{ 391 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF; 392} 393 394static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port) 395{ 396 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF; 397} 398#if IS_ENABLED(CONFIG_XFRM_OFFLOAD) 399void nsim_ipsec_init(struct netdevsim *ns); 400void nsim_ipsec_teardown(struct netdevsim *ns); 401bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb); 402#else 403static inline void nsim_ipsec_init(struct netdevsim *ns) 404{ 405} 406 407static inline void nsim_ipsec_teardown(struct netdevsim *ns) 408{ 409} 410 411static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb) 412{ 413 return true; 414} 415#endif 416 417#if IS_ENABLED(CONFIG_MACSEC) 418void nsim_macsec_init(struct netdevsim *ns); 419void nsim_macsec_teardown(struct netdevsim *ns); 420#else 421static inline void nsim_macsec_init(struct netdevsim *ns) 422{ 423} 424 425static inline void nsim_macsec_teardown(struct netdevsim *ns) 426{ 427} 428#endif 429 430#if IS_ENABLED(CONFIG_INET_PSP) 431int nsim_psp_init(struct netdevsim *ns); 432void nsim_psp_uninit(struct netdevsim *ns); 433void nsim_psp_handle_ext(struct sk_buff *skb, struct skb_ext *psp_ext); 434enum skb_drop_reason 435nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns, 436 struct netdevsim *peer_ns, struct skb_ext **psp_ext); 437#else 438static inline int nsim_psp_init(struct netdevsim *ns) { return 0; } 439static inline void nsim_psp_uninit(struct netdevsim *ns) {} 440static inline enum skb_drop_reason 441nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns, 442 struct netdevsim *peer_ns, struct skb_ext **psp_ext) 443{ 444 return 0; 445} 446 447static inline void 448nsim_psp_handle_ext(struct sk_buff *skb, struct skb_ext *psp_ext) {} 449#endif 450 451struct nsim_bus_dev { 452 struct device dev; 453 struct list_head list; 454 unsigned int port_count; 455 unsigned int num_queues; /* Number of queues for each port on this bus */ 456 struct net *initial_net; /* Purpose of this is to carry net pointer 457 * during the probe time only. 458 */ 459 unsigned int max_vfs; 460 unsigned int num_vfs; 461 bool init; 462}; 463 464int nsim_bus_init(void); 465void nsim_bus_exit(void);