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/device.h> 17 #include <linux/kernel.h> 18 #include <linux/list.h> 19 #include <linux/netdevice.h> 20 #include <linux/u64_stats_sync.h> 21 #include <net/xdp.h> 22 23 #define DRV_NAME "netdevsim" 24 25 #define NSIM_XDP_MAX_MTU 4000 26 27 #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg) 28 29 struct bpf_prog; 30 struct bpf_offload_dev; 31 struct dentry; 32 struct nsim_vf_config; 33 34 struct netdevsim_shared_dev { 35 unsigned int refcnt; 36 u32 switch_id; 37 38 struct dentry *ddir; 39 40 struct bpf_offload_dev *bpf_dev; 41 42 struct dentry *ddir_bpf_bound_progs; 43 u32 prog_id_gen; 44 45 struct list_head bpf_bound_progs; 46 struct list_head bpf_bound_maps; 47 }; 48 49 #define NSIM_IPSEC_MAX_SA_COUNT 33 50 #define NSIM_IPSEC_VALID BIT(31) 51 52 struct nsim_sa { 53 struct xfrm_state *xs; 54 __be32 ipaddr[4]; 55 u32 key[4]; 56 u32 salt; 57 bool used; 58 bool crypt; 59 bool rx; 60 }; 61 62 struct nsim_ipsec { 63 struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT]; 64 struct dentry *pfile; 65 u32 count; 66 u32 tx; 67 u32 ok; 68 }; 69 70 struct netdevsim { 71 struct net_device *netdev; 72 73 u64 tx_packets; 74 u64 tx_bytes; 75 struct u64_stats_sync syncp; 76 77 struct device dev; 78 struct netdevsim_shared_dev *sdev; 79 80 struct dentry *ddir; 81 82 unsigned int num_vfs; 83 struct nsim_vf_config *vfconfigs; 84 85 struct bpf_prog *bpf_offloaded; 86 u32 bpf_offloaded_id; 87 88 struct xdp_attachment_info xdp; 89 struct xdp_attachment_info xdp_hw; 90 91 bool bpf_bind_accept; 92 u32 bpf_bind_verifier_delay; 93 94 bool bpf_tc_accept; 95 bool bpf_tc_non_bound_accept; 96 bool bpf_xdpdrv_accept; 97 bool bpf_xdpoffload_accept; 98 99 bool bpf_map_accept; 100 #if IS_ENABLED(CONFIG_NET_DEVLINK) 101 struct devlink *devlink; 102 #endif 103 struct nsim_ipsec ipsec; 104 }; 105 106 extern struct dentry *nsim_ddir; 107 extern struct dentry *nsim_sdev_ddir; 108 109 #ifdef CONFIG_BPF_SYSCALL 110 int nsim_bpf_init(struct netdevsim *ns); 111 void nsim_bpf_uninit(struct netdevsim *ns); 112 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf); 113 int nsim_bpf_disable_tc(struct netdevsim *ns); 114 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, 115 void *type_data, void *cb_priv); 116 #else 117 static inline int nsim_bpf_init(struct netdevsim *ns) 118 { 119 return 0; 120 } 121 122 static inline void nsim_bpf_uninit(struct netdevsim *ns) 123 { 124 } 125 126 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) 127 { 128 return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP; 129 } 130 131 static inline int nsim_bpf_disable_tc(struct netdevsim *ns) 132 { 133 return 0; 134 } 135 136 static inline int 137 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 138 void *cb_priv) 139 { 140 return -EOPNOTSUPP; 141 } 142 #endif 143 144 #if IS_ENABLED(CONFIG_NET_DEVLINK) 145 enum nsim_resource_id { 146 NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */ 147 NSIM_RESOURCE_IPV4, 148 NSIM_RESOURCE_IPV4_FIB, 149 NSIM_RESOURCE_IPV4_FIB_RULES, 150 NSIM_RESOURCE_IPV6, 151 NSIM_RESOURCE_IPV6_FIB, 152 NSIM_RESOURCE_IPV6_FIB_RULES, 153 }; 154 155 int nsim_devlink_setup(struct netdevsim *ns); 156 void nsim_devlink_teardown(struct netdevsim *ns); 157 158 int nsim_devlink_init(void); 159 void nsim_devlink_exit(void); 160 161 int nsim_fib_init(void); 162 void nsim_fib_exit(void); 163 u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max); 164 int nsim_fib_set_max(struct net *net, enum nsim_resource_id res_id, u64 val, 165 struct netlink_ext_ack *extack); 166 #else 167 static inline int nsim_devlink_setup(struct netdevsim *ns) 168 { 169 return 0; 170 } 171 172 static inline void nsim_devlink_teardown(struct netdevsim *ns) 173 { 174 } 175 176 static inline int nsim_devlink_init(void) 177 { 178 return 0; 179 } 180 181 static inline void nsim_devlink_exit(void) 182 { 183 } 184 #endif 185 186 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD) 187 void nsim_ipsec_init(struct netdevsim *ns); 188 void nsim_ipsec_teardown(struct netdevsim *ns); 189 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb); 190 #else 191 static inline void nsim_ipsec_init(struct netdevsim *ns) 192 { 193 } 194 195 static inline void nsim_ipsec_teardown(struct netdevsim *ns) 196 { 197 } 198 199 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb) 200 { 201 return true; 202 } 203 #endif 204 205 static inline struct netdevsim *to_nsim(struct device *ptr) 206 { 207 return container_of(ptr, struct netdevsim, dev); 208 } 209