18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (C) 2017 Netronome Systems, Inc. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This software is licensed under the GNU General License Version 2, 58c2ecf20Sopenharmony_ci * June 1991 as shown in the file COPYING in the top-level directory of this 68c2ecf20Sopenharmony_ci * source tree. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" 98c2ecf20Sopenharmony_ci * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, 108c2ecf20Sopenharmony_ci * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 118c2ecf20Sopenharmony_ci * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 128c2ecf20Sopenharmony_ci * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME 138c2ecf20Sopenharmony_ci * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 148c2ecf20Sopenharmony_ci */ 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <linux/debugfs.h> 178c2ecf20Sopenharmony_ci#include <linux/device.h> 188c2ecf20Sopenharmony_ci#include <linux/kernel.h> 198c2ecf20Sopenharmony_ci#include <linux/list.h> 208c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 218c2ecf20Sopenharmony_ci#include <linux/u64_stats_sync.h> 228c2ecf20Sopenharmony_ci#include <net/devlink.h> 238c2ecf20Sopenharmony_ci#include <net/udp_tunnel.h> 248c2ecf20Sopenharmony_ci#include <net/xdp.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define DRV_NAME "netdevsim" 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define NSIM_XDP_MAX_MTU 4000 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg) 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define NSIM_IPSEC_MAX_SA_COUNT 33 338c2ecf20Sopenharmony_ci#define NSIM_IPSEC_VALID BIT(31) 348c2ecf20Sopenharmony_ci#define NSIM_UDP_TUNNEL_N_PORTS 4 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistruct nsim_sa { 378c2ecf20Sopenharmony_ci struct xfrm_state *xs; 388c2ecf20Sopenharmony_ci __be32 ipaddr[4]; 398c2ecf20Sopenharmony_ci u32 key[4]; 408c2ecf20Sopenharmony_ci u32 salt; 418c2ecf20Sopenharmony_ci bool used; 428c2ecf20Sopenharmony_ci bool crypt; 438c2ecf20Sopenharmony_ci bool rx; 448c2ecf20Sopenharmony_ci}; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistruct nsim_ipsec { 478c2ecf20Sopenharmony_ci struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT]; 488c2ecf20Sopenharmony_ci struct dentry *pfile; 498c2ecf20Sopenharmony_ci u32 count; 508c2ecf20Sopenharmony_ci u32 tx; 518c2ecf20Sopenharmony_ci u32 ok; 528c2ecf20Sopenharmony_ci}; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistruct nsim_ethtool { 558c2ecf20Sopenharmony_ci bool rx; 568c2ecf20Sopenharmony_ci bool tx; 578c2ecf20Sopenharmony_ci bool report_stats_rx; 588c2ecf20Sopenharmony_ci bool report_stats_tx; 598c2ecf20Sopenharmony_ci}; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistruct netdevsim { 628c2ecf20Sopenharmony_ci struct net_device *netdev; 638c2ecf20Sopenharmony_ci struct nsim_dev *nsim_dev; 648c2ecf20Sopenharmony_ci struct nsim_dev_port *nsim_dev_port; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci u64 tx_packets; 678c2ecf20Sopenharmony_ci u64 tx_bytes; 688c2ecf20Sopenharmony_ci struct u64_stats_sync syncp; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci struct nsim_bus_dev *nsim_bus_dev; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci struct bpf_prog *bpf_offloaded; 738c2ecf20Sopenharmony_ci u32 bpf_offloaded_id; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci struct xdp_attachment_info xdp; 768c2ecf20Sopenharmony_ci struct xdp_attachment_info xdp_hw; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci bool bpf_tc_accept; 798c2ecf20Sopenharmony_ci bool bpf_tc_non_bound_accept; 808c2ecf20Sopenharmony_ci bool bpf_xdpdrv_accept; 818c2ecf20Sopenharmony_ci bool bpf_xdpoffload_accept; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci bool bpf_map_accept; 848c2ecf20Sopenharmony_ci struct nsim_ipsec ipsec; 858c2ecf20Sopenharmony_ci struct { 868c2ecf20Sopenharmony_ci u32 inject_error; 878c2ecf20Sopenharmony_ci u32 sleep; 888c2ecf20Sopenharmony_ci u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS]; 898c2ecf20Sopenharmony_ci u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS]; 908c2ecf20Sopenharmony_ci struct debugfs_u32_array dfs_ports[2]; 918c2ecf20Sopenharmony_ci } udp_ports; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci struct nsim_ethtool ethtool; 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistruct netdevsim * 978c2ecf20Sopenharmony_cinsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port); 988c2ecf20Sopenharmony_civoid nsim_destroy(struct netdevsim *ns); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_civoid nsim_ethtool_init(struct netdevsim *ns); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_civoid nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev); 1038c2ecf20Sopenharmony_ciint nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev, 1048c2ecf20Sopenharmony_ci struct net_device *dev); 1058c2ecf20Sopenharmony_civoid nsim_udp_tunnels_info_destroy(struct net_device *dev); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci#ifdef CONFIG_BPF_SYSCALL 1088c2ecf20Sopenharmony_ciint nsim_bpf_dev_init(struct nsim_dev *nsim_dev); 1098c2ecf20Sopenharmony_civoid nsim_bpf_dev_exit(struct nsim_dev *nsim_dev); 1108c2ecf20Sopenharmony_ciint nsim_bpf_init(struct netdevsim *ns); 1118c2ecf20Sopenharmony_civoid nsim_bpf_uninit(struct netdevsim *ns); 1128c2ecf20Sopenharmony_ciint nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf); 1138c2ecf20Sopenharmony_ciint nsim_bpf_disable_tc(struct netdevsim *ns); 1148c2ecf20Sopenharmony_ciint nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, 1158c2ecf20Sopenharmony_ci void *type_data, void *cb_priv); 1168c2ecf20Sopenharmony_ci#else 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_cistatic inline int nsim_bpf_init(struct netdevsim *ns) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci return 0; 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_cistatic inline void nsim_bpf_uninit(struct netdevsim *ns) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci} 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistatic inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic inline int nsim_bpf_disable_tc(struct netdevsim *ns) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci return 0; 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic inline int 1468c2ecf20Sopenharmony_cinsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 1478c2ecf20Sopenharmony_ci void *cb_priv) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci#endif 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cienum nsim_resource_id { 1548c2ecf20Sopenharmony_ci NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */ 1558c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV4, 1568c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV4_FIB, 1578c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV4_FIB_RULES, 1588c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV6, 1598c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV6_FIB, 1608c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV6_FIB_RULES, 1618c2ecf20Sopenharmony_ci}; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistruct nsim_dev_health { 1648c2ecf20Sopenharmony_ci struct devlink_health_reporter *empty_reporter; 1658c2ecf20Sopenharmony_ci struct devlink_health_reporter *dummy_reporter; 1668c2ecf20Sopenharmony_ci struct dentry *ddir; 1678c2ecf20Sopenharmony_ci char *recovered_break_msg; 1688c2ecf20Sopenharmony_ci u32 binary_len; 1698c2ecf20Sopenharmony_ci bool fail_recover; 1708c2ecf20Sopenharmony_ci}; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ciint nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink); 1738c2ecf20Sopenharmony_civoid nsim_dev_health_exit(struct nsim_dev *nsim_dev); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistruct nsim_dev_port { 1768c2ecf20Sopenharmony_ci struct list_head list; 1778c2ecf20Sopenharmony_ci struct devlink_port devlink_port; 1788c2ecf20Sopenharmony_ci unsigned int port_index; 1798c2ecf20Sopenharmony_ci struct dentry *ddir; 1808c2ecf20Sopenharmony_ci struct netdevsim *ns; 1818c2ecf20Sopenharmony_ci}; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistruct nsim_dev { 1848c2ecf20Sopenharmony_ci struct nsim_bus_dev *nsim_bus_dev; 1858c2ecf20Sopenharmony_ci struct nsim_fib_data *fib_data; 1868c2ecf20Sopenharmony_ci struct nsim_trap_data *trap_data; 1878c2ecf20Sopenharmony_ci struct dentry *ddir; 1888c2ecf20Sopenharmony_ci struct dentry *ports_ddir; 1898c2ecf20Sopenharmony_ci struct dentry *take_snapshot; 1908c2ecf20Sopenharmony_ci struct bpf_offload_dev *bpf_dev; 1918c2ecf20Sopenharmony_ci bool bpf_bind_accept; 1928c2ecf20Sopenharmony_ci bool bpf_bind_verifier_accept; 1938c2ecf20Sopenharmony_ci u32 bpf_bind_verifier_delay; 1948c2ecf20Sopenharmony_ci struct dentry *ddir_bpf_bound_progs; 1958c2ecf20Sopenharmony_ci u32 prog_id_gen; 1968c2ecf20Sopenharmony_ci struct list_head bpf_bound_progs; 1978c2ecf20Sopenharmony_ci struct list_head bpf_bound_maps; 1988c2ecf20Sopenharmony_ci struct netdev_phys_item_id switch_id; 1998c2ecf20Sopenharmony_ci struct list_head port_list; 2008c2ecf20Sopenharmony_ci struct mutex port_list_lock; /* protects port list */ 2018c2ecf20Sopenharmony_ci bool fw_update_status; 2028c2ecf20Sopenharmony_ci u32 fw_update_overwrite_mask; 2038c2ecf20Sopenharmony_ci u32 max_macs; 2048c2ecf20Sopenharmony_ci bool test1; 2058c2ecf20Sopenharmony_ci bool dont_allow_reload; 2068c2ecf20Sopenharmony_ci bool fail_reload; 2078c2ecf20Sopenharmony_ci struct devlink_region *dummy_region; 2088c2ecf20Sopenharmony_ci struct nsim_dev_health health; 2098c2ecf20Sopenharmony_ci struct flow_action_cookie *fa_cookie; 2108c2ecf20Sopenharmony_ci spinlock_t fa_cookie_lock; /* protects fa_cookie */ 2118c2ecf20Sopenharmony_ci bool fail_trap_group_set; 2128c2ecf20Sopenharmony_ci bool fail_trap_policer_set; 2138c2ecf20Sopenharmony_ci bool fail_trap_policer_counter_get; 2148c2ecf20Sopenharmony_ci struct { 2158c2ecf20Sopenharmony_ci struct udp_tunnel_nic_shared utn_shared; 2168c2ecf20Sopenharmony_ci u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS]; 2178c2ecf20Sopenharmony_ci bool sync_all; 2188c2ecf20Sopenharmony_ci bool open_only; 2198c2ecf20Sopenharmony_ci bool ipv4_only; 2208c2ecf20Sopenharmony_ci bool shared; 2218c2ecf20Sopenharmony_ci bool static_iana_vxlan; 2228c2ecf20Sopenharmony_ci u32 sleep; 2238c2ecf20Sopenharmony_ci } udp_ports; 2248c2ecf20Sopenharmony_ci}; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci return devlink_net(priv_to_devlink(nsim_dev)); 2298c2ecf20Sopenharmony_ci} 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ciint nsim_dev_init(void); 2328c2ecf20Sopenharmony_civoid nsim_dev_exit(void); 2338c2ecf20Sopenharmony_ciint nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev); 2348c2ecf20Sopenharmony_civoid nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev); 2358c2ecf20Sopenharmony_ciint nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev, 2368c2ecf20Sopenharmony_ci unsigned int port_index); 2378c2ecf20Sopenharmony_ciint nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev, 2388c2ecf20Sopenharmony_ci unsigned int port_index); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cistruct nsim_fib_data *nsim_fib_create(struct devlink *devlink, 2418c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack); 2428c2ecf20Sopenharmony_civoid nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data); 2438c2ecf20Sopenharmony_ciu64 nsim_fib_get_val(struct nsim_fib_data *fib_data, 2448c2ecf20Sopenharmony_ci enum nsim_resource_id res_id, bool max); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_XFRM_OFFLOAD) 2478c2ecf20Sopenharmony_civoid nsim_ipsec_init(struct netdevsim *ns); 2488c2ecf20Sopenharmony_civoid nsim_ipsec_teardown(struct netdevsim *ns); 2498c2ecf20Sopenharmony_cibool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb); 2508c2ecf20Sopenharmony_ci#else 2518c2ecf20Sopenharmony_cistatic inline void nsim_ipsec_init(struct netdevsim *ns) 2528c2ecf20Sopenharmony_ci{ 2538c2ecf20Sopenharmony_ci} 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_cistatic inline void nsim_ipsec_teardown(struct netdevsim *ns) 2568c2ecf20Sopenharmony_ci{ 2578c2ecf20Sopenharmony_ci} 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_cistatic inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb) 2608c2ecf20Sopenharmony_ci{ 2618c2ecf20Sopenharmony_ci return true; 2628c2ecf20Sopenharmony_ci} 2638c2ecf20Sopenharmony_ci#endif 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_cistruct nsim_vf_config { 2668c2ecf20Sopenharmony_ci int link_state; 2678c2ecf20Sopenharmony_ci u16 min_tx_rate; 2688c2ecf20Sopenharmony_ci u16 max_tx_rate; 2698c2ecf20Sopenharmony_ci u16 vlan; 2708c2ecf20Sopenharmony_ci __be16 vlan_proto; 2718c2ecf20Sopenharmony_ci u16 qos; 2728c2ecf20Sopenharmony_ci u8 vf_mac[ETH_ALEN]; 2738c2ecf20Sopenharmony_ci bool spoofchk_enabled; 2748c2ecf20Sopenharmony_ci bool trusted; 2758c2ecf20Sopenharmony_ci bool rss_query_enabled; 2768c2ecf20Sopenharmony_ci}; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cistruct nsim_bus_dev { 2798c2ecf20Sopenharmony_ci struct device dev; 2808c2ecf20Sopenharmony_ci struct list_head list; 2818c2ecf20Sopenharmony_ci unsigned int port_count; 2828c2ecf20Sopenharmony_ci struct net *initial_net; /* Purpose of this is to carry net pointer 2838c2ecf20Sopenharmony_ci * during the probe time only. 2848c2ecf20Sopenharmony_ci */ 2858c2ecf20Sopenharmony_ci unsigned int num_vfs; 2868c2ecf20Sopenharmony_ci struct nsim_vf_config *vfconfigs; 2878c2ecf20Sopenharmony_ci /* Lock for devlink->reload_enabled in netdevsim module */ 2888c2ecf20Sopenharmony_ci struct mutex nsim_bus_reload_lock; 2898c2ecf20Sopenharmony_ci bool init; 2908c2ecf20Sopenharmony_ci}; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ciint nsim_bus_init(void); 2938c2ecf20Sopenharmony_civoid nsim_bus_exit(void); 294