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