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