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