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