Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 16 | #include <linux/debugfs.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 17 | #include <linux/device.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/u64_stats_sync.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 22 | #include <net/devlink.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 23 | #include <net/udp_tunnel.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 24 | #include <net/xdp.h> |
| 25 | |
| 26 | #define DRV_NAME "netdevsim" |
| 27 | |
| 28 | #define NSIM_XDP_MAX_MTU 4000 |
| 29 | |
| 30 | #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg) |
| 31 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 32 | #define NSIM_IPSEC_MAX_SA_COUNT 33 |
| 33 | #define NSIM_IPSEC_VALID BIT(31) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 34 | #define NSIM_UDP_TUNNEL_N_PORTS 4 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | |
| 36 | struct nsim_sa { |
| 37 | struct xfrm_state *xs; |
| 38 | __be32 ipaddr[4]; |
| 39 | u32 key[4]; |
| 40 | u32 salt; |
| 41 | bool used; |
| 42 | bool crypt; |
| 43 | bool rx; |
| 44 | }; |
| 45 | |
| 46 | struct nsim_ipsec { |
| 47 | struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT]; |
| 48 | struct dentry *pfile; |
| 49 | u32 count; |
| 50 | u32 tx; |
| 51 | u32 ok; |
| 52 | }; |
| 53 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 54 | struct nsim_ethtool { |
| 55 | bool rx; |
| 56 | bool tx; |
| 57 | bool report_stats_rx; |
| 58 | bool report_stats_tx; |
| 59 | }; |
| 60 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 61 | struct netdevsim { |
| 62 | struct net_device *netdev; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 63 | struct nsim_dev *nsim_dev; |
| 64 | struct nsim_dev_port *nsim_dev_port; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 65 | |
| 66 | u64 tx_packets; |
| 67 | u64 tx_bytes; |
| 68 | struct u64_stats_sync syncp; |
| 69 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 70 | struct nsim_bus_dev *nsim_bus_dev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 71 | |
| 72 | struct bpf_prog *bpf_offloaded; |
| 73 | u32 bpf_offloaded_id; |
| 74 | |
| 75 | struct xdp_attachment_info xdp; |
| 76 | struct xdp_attachment_info xdp_hw; |
| 77 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 78 | bool bpf_tc_accept; |
| 79 | bool bpf_tc_non_bound_accept; |
| 80 | bool bpf_xdpdrv_accept; |
| 81 | bool bpf_xdpoffload_accept; |
| 82 | |
| 83 | bool bpf_map_accept; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 84 | struct nsim_ipsec ipsec; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 85 | struct { |
| 86 | u32 inject_error; |
| 87 | u32 sleep; |
| 88 | u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS]; |
| 89 | u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS]; |
| 90 | struct debugfs_u32_array dfs_ports[2]; |
| 91 | } udp_ports; |
| 92 | |
| 93 | struct nsim_ethtool ethtool; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 96 | struct netdevsim * |
| 97 | nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port); |
| 98 | void nsim_destroy(struct netdevsim *ns); |
| 99 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 100 | void nsim_ethtool_init(struct netdevsim *ns); |
| 101 | |
| 102 | void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev); |
| 103 | int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev, |
| 104 | struct net_device *dev); |
| 105 | void nsim_udp_tunnels_info_destroy(struct net_device *dev); |
| 106 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 107 | #ifdef CONFIG_BPF_SYSCALL |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 108 | int nsim_bpf_dev_init(struct nsim_dev *nsim_dev); |
| 109 | void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | int nsim_bpf_init(struct netdevsim *ns); |
| 111 | void nsim_bpf_uninit(struct netdevsim *ns); |
| 112 | int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf); |
| 113 | int nsim_bpf_disable_tc(struct netdevsim *ns); |
| 114 | int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, |
| 115 | void *type_data, void *cb_priv); |
| 116 | #else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 117 | |
| 118 | static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev) |
| 119 | { |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev) |
| 124 | { |
| 125 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 126 | static inline int nsim_bpf_init(struct netdevsim *ns) |
| 127 | { |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static inline void nsim_bpf_uninit(struct netdevsim *ns) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) |
| 136 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 137 | return -EOPNOTSUPP; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static inline int nsim_bpf_disable_tc(struct netdevsim *ns) |
| 141 | { |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static inline int |
| 146 | nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data, |
| 147 | void *cb_priv) |
| 148 | { |
| 149 | return -EOPNOTSUPP; |
| 150 | } |
| 151 | #endif |
| 152 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 153 | enum nsim_resource_id { |
| 154 | NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */ |
| 155 | NSIM_RESOURCE_IPV4, |
| 156 | NSIM_RESOURCE_IPV4_FIB, |
| 157 | NSIM_RESOURCE_IPV4_FIB_RULES, |
| 158 | NSIM_RESOURCE_IPV6, |
| 159 | NSIM_RESOURCE_IPV6_FIB, |
| 160 | NSIM_RESOURCE_IPV6_FIB_RULES, |
| 161 | }; |
| 162 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 163 | struct nsim_dev_health { |
| 164 | struct devlink_health_reporter *empty_reporter; |
| 165 | struct devlink_health_reporter *dummy_reporter; |
| 166 | struct dentry *ddir; |
| 167 | char *recovered_break_msg; |
| 168 | u32 binary_len; |
| 169 | bool fail_recover; |
| 170 | }; |
| 171 | |
| 172 | int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink); |
| 173 | void nsim_dev_health_exit(struct nsim_dev *nsim_dev); |
| 174 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 175 | struct nsim_dev_port { |
| 176 | struct list_head list; |
| 177 | struct devlink_port devlink_port; |
| 178 | unsigned int port_index; |
| 179 | struct dentry *ddir; |
| 180 | struct netdevsim *ns; |
| 181 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 182 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 183 | struct nsim_dev { |
| 184 | struct nsim_bus_dev *nsim_bus_dev; |
| 185 | struct nsim_fib_data *fib_data; |
| 186 | struct nsim_trap_data *trap_data; |
| 187 | struct dentry *ddir; |
| 188 | struct dentry *ports_ddir; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 189 | struct dentry *take_snapshot; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 190 | struct bpf_offload_dev *bpf_dev; |
| 191 | bool bpf_bind_accept; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 192 | bool bpf_bind_verifier_accept; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 193 | u32 bpf_bind_verifier_delay; |
| 194 | struct dentry *ddir_bpf_bound_progs; |
| 195 | u32 prog_id_gen; |
| 196 | struct list_head bpf_bound_progs; |
| 197 | struct list_head bpf_bound_maps; |
| 198 | struct netdev_phys_item_id switch_id; |
| 199 | struct list_head port_list; |
| 200 | struct mutex port_list_lock; /* protects port list */ |
| 201 | bool fw_update_status; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 202 | u32 fw_update_overwrite_mask; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 203 | u32 max_macs; |
| 204 | bool test1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 205 | bool dont_allow_reload; |
| 206 | bool fail_reload; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 207 | struct devlink_region *dummy_region; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 208 | struct nsim_dev_health health; |
| 209 | struct flow_action_cookie *fa_cookie; |
| 210 | spinlock_t fa_cookie_lock; /* protects fa_cookie */ |
| 211 | bool fail_trap_group_set; |
| 212 | bool fail_trap_policer_set; |
| 213 | bool fail_trap_policer_counter_get; |
| 214 | struct { |
| 215 | struct udp_tunnel_nic_shared utn_shared; |
| 216 | u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS]; |
| 217 | bool sync_all; |
| 218 | bool open_only; |
| 219 | bool ipv4_only; |
| 220 | bool shared; |
| 221 | bool static_iana_vxlan; |
| 222 | u32 sleep; |
| 223 | } udp_ports; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 226 | static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev) |
| 227 | { |
| 228 | return devlink_net(priv_to_devlink(nsim_dev)); |
| 229 | } |
| 230 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 231 | int nsim_dev_init(void); |
| 232 | void nsim_dev_exit(void); |
| 233 | int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev); |
| 234 | void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev); |
| 235 | int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev, |
| 236 | unsigned int port_index); |
| 237 | int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev, |
| 238 | unsigned int port_index); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 239 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 240 | struct nsim_fib_data *nsim_fib_create(struct devlink *devlink, |
| 241 | struct netlink_ext_ack *extack); |
| 242 | void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data); |
| 243 | u64 nsim_fib_get_val(struct nsim_fib_data *fib_data, |
| 244 | enum nsim_resource_id res_id, bool max); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 245 | |
| 246 | #if IS_ENABLED(CONFIG_XFRM_OFFLOAD) |
| 247 | void nsim_ipsec_init(struct netdevsim *ns); |
| 248 | void nsim_ipsec_teardown(struct netdevsim *ns); |
| 249 | bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb); |
| 250 | #else |
| 251 | static inline void nsim_ipsec_init(struct netdevsim *ns) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | static inline void nsim_ipsec_teardown(struct netdevsim *ns) |
| 256 | { |
| 257 | } |
| 258 | |
| 259 | static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb) |
| 260 | { |
| 261 | return true; |
| 262 | } |
| 263 | #endif |
| 264 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 265 | struct nsim_vf_config { |
| 266 | int link_state; |
| 267 | u16 min_tx_rate; |
| 268 | u16 max_tx_rate; |
| 269 | u16 vlan; |
| 270 | __be16 vlan_proto; |
| 271 | u16 qos; |
| 272 | u8 vf_mac[ETH_ALEN]; |
| 273 | bool spoofchk_enabled; |
| 274 | bool trusted; |
| 275 | bool rss_query_enabled; |
| 276 | }; |
| 277 | |
| 278 | struct nsim_bus_dev { |
| 279 | struct device dev; |
| 280 | struct list_head list; |
| 281 | unsigned int port_count; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 282 | struct net *initial_net; /* Purpose of this is to carry net pointer |
| 283 | * during the probe time only. |
| 284 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 285 | unsigned int num_vfs; |
| 286 | struct nsim_vf_config *vfconfigs; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 287 | /* Lock for devlink->reload_enabled in netdevsim module */ |
| 288 | struct mutex nsim_bus_reload_lock; |
| 289 | bool init; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | int nsim_bus_init(void); |
| 293 | void nsim_bus_exit(void); |