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