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