Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 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 | |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 16 | #include <linux/device.h> |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 18 | #include <linux/list.h> |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/u64_stats_sync.h> |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 21 | #include <net/devlink.h> |
Jakub Kicinski | 0529662 | 2018-07-11 20:36:40 -0700 | [diff] [blame] | 22 | #include <net/xdp.h> |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 23 | |
| 24 | #define DRV_NAME "netdevsim" |
| 25 | |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 26 | #define NSIM_XDP_MAX_MTU 4000 |
| 27 | |
| 28 | #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg) |
| 29 | |
Shannon Nelson | 7699353 | 2018-06-26 10:07:54 -0700 | [diff] [blame] | 30 | #define NSIM_IPSEC_MAX_SA_COUNT 33 |
| 31 | #define NSIM_IPSEC_VALID BIT(31) |
| 32 | |
| 33 | struct nsim_sa { |
| 34 | struct xfrm_state *xs; |
| 35 | __be32 ipaddr[4]; |
| 36 | u32 key[4]; |
| 37 | u32 salt; |
| 38 | bool used; |
| 39 | bool crypt; |
| 40 | bool rx; |
| 41 | }; |
| 42 | |
| 43 | struct nsim_ipsec { |
| 44 | struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT]; |
| 45 | struct dentry *pfile; |
| 46 | u32 count; |
| 47 | u32 tx; |
| 48 | u32 ok; |
| 49 | }; |
| 50 | |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 51 | struct netdevsim { |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 52 | struct net_device *netdev; |
Jiri Pirko | a60f9e4 | 2019-04-25 15:59:49 +0200 | [diff] [blame] | 53 | struct nsim_dev *nsim_dev; |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 54 | struct nsim_dev_port *nsim_dev_port; |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 55 | |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 56 | u64 tx_packets; |
| 57 | u64 tx_bytes; |
| 58 | struct u64_stats_sync syncp; |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 59 | |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 60 | struct nsim_bus_dev *nsim_bus_dev; |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 61 | |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 62 | struct bpf_prog *bpf_offloaded; |
| 63 | u32 bpf_offloaded_id; |
| 64 | |
Jakub Kicinski | 0529662 | 2018-07-11 20:36:40 -0700 | [diff] [blame] | 65 | struct xdp_attachment_info xdp; |
Jakub Kicinski | 799e173 | 2018-07-11 20:36:42 -0700 | [diff] [blame] | 66 | struct xdp_attachment_info xdp_hw; |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 67 | |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 68 | bool bpf_tc_accept; |
| 69 | bool bpf_tc_non_bound_accept; |
| 70 | bool bpf_xdpdrv_accept; |
| 71 | bool bpf_xdpoffload_accept; |
Jakub Kicinski | 395cacb | 2018-01-17 19:13:30 -0800 | [diff] [blame] | 72 | |
| 73 | bool bpf_map_accept; |
Shannon Nelson | 7699353 | 2018-06-26 10:07:54 -0700 | [diff] [blame] | 74 | struct nsim_ipsec ipsec; |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 75 | }; |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 76 | |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 77 | struct netdevsim * |
| 78 | nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port); |
| 79 | void nsim_destroy(struct netdevsim *ns); |
| 80 | |
Jakub Kicinski | 7c5db7e | 2018-01-23 11:22:54 -0800 | [diff] [blame] | 81 | #ifdef CONFIG_BPF_SYSCALL |
Jiri Pirko | d514f41 | 2019-04-25 15:59:50 +0200 | [diff] [blame] | 82 | int nsim_bpf_dev_init(struct nsim_dev *nsim_dev); |
| 83 | void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev); |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 84 | int nsim_bpf_init(struct netdevsim *ns); |
| 85 | void nsim_bpf_uninit(struct netdevsim *ns); |
| 86 | int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf); |
| 87 | int nsim_bpf_disable_tc(struct netdevsim *ns); |
| 88 | int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, |
| 89 | void *type_data, void *cb_priv); |
Jakub Kicinski | 7c5db7e | 2018-01-23 11:22:54 -0800 | [diff] [blame] | 90 | #else |
Jiri Pirko | d514f41 | 2019-04-25 15:59:50 +0200 | [diff] [blame] | 91 | |
| 92 | static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev) |
| 93 | { |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev) |
| 98 | { |
| 99 | } |
Jakub Kicinski | 7c5db7e | 2018-01-23 11:22:54 -0800 | [diff] [blame] | 100 | static inline int nsim_bpf_init(struct netdevsim *ns) |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static inline void nsim_bpf_uninit(struct netdevsim *ns) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) |
| 110 | { |
| 111 | return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP; |
| 112 | } |
| 113 | |
| 114 | static inline int nsim_bpf_disable_tc(struct netdevsim *ns) |
| 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static inline int |
| 120 | nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data, |
| 121 | void *cb_priv) |
| 122 | { |
| 123 | return -EOPNOTSUPP; |
| 124 | } |
| 125 | #endif |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 126 | |
David Ahern | 37923ed | 2018-03-27 18:22:00 -0700 | [diff] [blame] | 127 | enum nsim_resource_id { |
| 128 | NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */ |
| 129 | NSIM_RESOURCE_IPV4, |
| 130 | NSIM_RESOURCE_IPV4_FIB, |
| 131 | NSIM_RESOURCE_IPV4_FIB_RULES, |
| 132 | NSIM_RESOURCE_IPV6, |
| 133 | NSIM_RESOURCE_IPV6_FIB, |
| 134 | NSIM_RESOURCE_IPV6_FIB_RULES, |
| 135 | }; |
| 136 | |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 137 | struct nsim_dev_port { |
| 138 | struct list_head list; |
| 139 | struct devlink_port devlink_port; |
| 140 | unsigned int port_index; |
| 141 | struct dentry *ddir; |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 142 | struct netdevsim *ns; |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 143 | }; |
| 144 | |
Jiri Pirko | a60f9e4 | 2019-04-25 15:59:49 +0200 | [diff] [blame] | 145 | struct nsim_dev { |
Jiri Pirko | d514f41 | 2019-04-25 15:59:50 +0200 | [diff] [blame] | 146 | struct nsim_bus_dev *nsim_bus_dev; |
Jiri Pirko | a60f9e4 | 2019-04-25 15:59:49 +0200 | [diff] [blame] | 147 | struct nsim_fib_data *fib_data; |
Jiri Pirko | d514f41 | 2019-04-25 15:59:50 +0200 | [diff] [blame] | 148 | struct dentry *ddir; |
Jiri Pirko | ab1d0cc | 2019-04-25 15:59:52 +0200 | [diff] [blame] | 149 | struct dentry *ports_ddir; |
Jiri Pirko | d514f41 | 2019-04-25 15:59:50 +0200 | [diff] [blame] | 150 | struct bpf_offload_dev *bpf_dev; |
| 151 | bool bpf_bind_accept; |
| 152 | u32 bpf_bind_verifier_delay; |
| 153 | struct dentry *ddir_bpf_bound_progs; |
| 154 | u32 prog_id_gen; |
| 155 | struct list_head bpf_bound_progs; |
| 156 | struct list_head bpf_bound_maps; |
Jiri Pirko | 514cf64 | 2019-04-25 15:59:51 +0200 | [diff] [blame] | 157 | struct netdev_phys_item_id switch_id; |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 158 | struct list_head port_list; |
Jiri Pirko | 794b2c0 | 2019-04-25 15:59:54 +0200 | [diff] [blame] | 159 | struct mutex port_list_lock; /* protects port list */ |
Jiri Pirko | fa4dfc4 | 2019-06-04 15:40:43 +0200 | [diff] [blame] | 160 | bool fw_update_status; |
Jiri Pirko | 150e8f8 | 2019-08-09 13:05:12 +0200 | [diff] [blame] | 161 | u32 max_macs; |
| 162 | bool test1; |
Jiri Pirko | 4418f86 | 2019-08-15 15:46:33 +0200 | [diff] [blame^] | 163 | struct devlink_region *dummy_region; |
Jiri Pirko | a60f9e4 | 2019-04-25 15:59:49 +0200 | [diff] [blame] | 164 | }; |
| 165 | |
Jiri Pirko | d514f41 | 2019-04-25 15:59:50 +0200 | [diff] [blame] | 166 | int nsim_dev_init(void); |
| 167 | void nsim_dev_exit(void); |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 168 | int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev); |
| 169 | void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev); |
Jiri Pirko | 794b2c0 | 2019-04-25 15:59:54 +0200 | [diff] [blame] | 170 | int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev, |
| 171 | unsigned int port_index); |
| 172 | int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev, |
| 173 | unsigned int port_index); |
David Ahern | 37923ed | 2018-03-27 18:22:00 -0700 | [diff] [blame] | 174 | |
Jiri Pirko | 5fc4942 | 2019-04-25 15:59:42 +0200 | [diff] [blame] | 175 | struct nsim_fib_data *nsim_fib_create(void); |
| 176 | void nsim_fib_destroy(struct nsim_fib_data *fib_data); |
| 177 | u64 nsim_fib_get_val(struct nsim_fib_data *fib_data, |
| 178 | enum nsim_resource_id res_id, bool max); |
| 179 | int nsim_fib_set_max(struct nsim_fib_data *fib_data, |
| 180 | enum nsim_resource_id res_id, u64 val, |
David Ahern | 7fa76d7 | 2018-06-05 08:14:10 -0700 | [diff] [blame] | 181 | struct netlink_ext_ack *extack); |
David Ahern | 37923ed | 2018-03-27 18:22:00 -0700 | [diff] [blame] | 182 | |
Shannon Nelson | 7699353 | 2018-06-26 10:07:54 -0700 | [diff] [blame] | 183 | #if IS_ENABLED(CONFIG_XFRM_OFFLOAD) |
| 184 | void nsim_ipsec_init(struct netdevsim *ns); |
| 185 | void nsim_ipsec_teardown(struct netdevsim *ns); |
| 186 | bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb); |
| 187 | #else |
| 188 | static inline void nsim_ipsec_init(struct netdevsim *ns) |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | static inline void nsim_ipsec_teardown(struct netdevsim *ns) |
| 193 | { |
| 194 | } |
| 195 | |
| 196 | static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb) |
| 197 | { |
| 198 | return true; |
| 199 | } |
| 200 | #endif |
| 201 | |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 202 | struct nsim_vf_config { |
| 203 | int link_state; |
| 204 | u16 min_tx_rate; |
| 205 | u16 max_tx_rate; |
| 206 | u16 vlan; |
| 207 | __be16 vlan_proto; |
| 208 | u16 qos; |
| 209 | u8 vf_mac[ETH_ALEN]; |
| 210 | bool spoofchk_enabled; |
| 211 | bool trusted; |
| 212 | bool rss_query_enabled; |
| 213 | }; |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 214 | |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 215 | struct nsim_bus_dev { |
| 216 | struct device dev; |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 217 | struct list_head list; |
| 218 | unsigned int port_count; |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 219 | unsigned int num_vfs; |
| 220 | struct nsim_vf_config *vfconfigs; |
| 221 | }; |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 222 | |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 223 | int nsim_bus_init(void); |
| 224 | void nsim_bus_exit(void); |