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