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