netdevsim.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. #include <linux/debugfs.h>
  16. #include <linux/device.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/u64_stats_sync.h>
  22. #include <net/devlink.h>
  23. #include <net/udp_tunnel.h>
  24. #include <net/xdp.h>
  25. #define DRV_NAME "netdevsim"
  26. #define NSIM_XDP_MAX_MTU 4000
  27. #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
  28. #define NSIM_IPSEC_MAX_SA_COUNT 33
  29. #define NSIM_IPSEC_VALID BIT(31)
  30. #define NSIM_UDP_TUNNEL_N_PORTS 4
  31. struct nsim_sa {
  32. struct xfrm_state *xs;
  33. __be32 ipaddr[4];
  34. u32 key[4];
  35. u32 salt;
  36. bool used;
  37. bool crypt;
  38. bool rx;
  39. };
  40. struct nsim_ipsec {
  41. struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
  42. struct dentry *pfile;
  43. u32 count;
  44. u32 tx;
  45. u32 ok;
  46. };
  47. struct nsim_ethtool_pauseparam {
  48. bool rx;
  49. bool tx;
  50. bool report_stats_rx;
  51. bool report_stats_tx;
  52. };
  53. struct nsim_ethtool {
  54. u32 get_err;
  55. u32 set_err;
  56. u32 channels;
  57. struct nsim_ethtool_pauseparam pauseparam;
  58. struct ethtool_coalesce coalesce;
  59. struct ethtool_ringparam ring;
  60. struct ethtool_fecparam fec;
  61. };
  62. struct netdevsim {
  63. struct net_device *netdev;
  64. struct nsim_dev *nsim_dev;
  65. struct nsim_dev_port *nsim_dev_port;
  66. u64 tx_packets;
  67. u64 tx_bytes;
  68. struct u64_stats_sync syncp;
  69. struct nsim_bus_dev *nsim_bus_dev;
  70. struct bpf_prog *bpf_offloaded;
  71. u32 bpf_offloaded_id;
  72. struct xdp_attachment_info xdp;
  73. struct xdp_attachment_info xdp_hw;
  74. bool bpf_tc_accept;
  75. bool bpf_tc_non_bound_accept;
  76. bool bpf_xdpdrv_accept;
  77. bool bpf_xdpoffload_accept;
  78. bool bpf_map_accept;
  79. struct nsim_ipsec ipsec;
  80. struct {
  81. u32 inject_error;
  82. u32 sleep;
  83. u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
  84. u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS];
  85. struct debugfs_u32_array dfs_ports[2];
  86. } udp_ports;
  87. struct nsim_ethtool ethtool;
  88. };
  89. struct netdevsim *
  90. nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
  91. void nsim_destroy(struct netdevsim *ns);
  92. void nsim_ethtool_init(struct netdevsim *ns);
  93. void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev);
  94. int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
  95. struct net_device *dev);
  96. void nsim_udp_tunnels_info_destroy(struct net_device *dev);
  97. #ifdef CONFIG_BPF_SYSCALL
  98. int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
  99. void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
  100. int nsim_bpf_init(struct netdevsim *ns);
  101. void nsim_bpf_uninit(struct netdevsim *ns);
  102. int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
  103. int nsim_bpf_disable_tc(struct netdevsim *ns);
  104. int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
  105. void *type_data, void *cb_priv);
  106. #else
  107. static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
  108. {
  109. return 0;
  110. }
  111. static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
  112. {
  113. }
  114. static inline int nsim_bpf_init(struct netdevsim *ns)
  115. {
  116. return 0;
  117. }
  118. static inline void nsim_bpf_uninit(struct netdevsim *ns)
  119. {
  120. }
  121. static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
  122. {
  123. return -EOPNOTSUPP;
  124. }
  125. static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
  126. {
  127. return 0;
  128. }
  129. static inline int
  130. nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  131. void *cb_priv)
  132. {
  133. return -EOPNOTSUPP;
  134. }
  135. #endif
  136. enum nsim_resource_id {
  137. NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
  138. NSIM_RESOURCE_IPV4,
  139. NSIM_RESOURCE_IPV4_FIB,
  140. NSIM_RESOURCE_IPV4_FIB_RULES,
  141. NSIM_RESOURCE_IPV6,
  142. NSIM_RESOURCE_IPV6_FIB,
  143. NSIM_RESOURCE_IPV6_FIB_RULES,
  144. NSIM_RESOURCE_NEXTHOPS,
  145. };
  146. struct nsim_dev_health {
  147. struct devlink_health_reporter *empty_reporter;
  148. struct devlink_health_reporter *dummy_reporter;
  149. struct dentry *ddir;
  150. char *recovered_break_msg;
  151. u32 binary_len;
  152. bool fail_recover;
  153. };
  154. int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
  155. void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
  156. struct nsim_dev_hwstats_netdev {
  157. struct list_head list;
  158. struct net_device *netdev;
  159. struct rtnl_hw_stats64 stats;
  160. bool enabled;
  161. bool fail_enable;
  162. };
  163. struct nsim_dev_hwstats {
  164. struct dentry *ddir;
  165. struct dentry *l3_ddir;
  166. struct mutex hwsdev_list_lock; /* protects hwsdev list(s) */
  167. struct list_head l3_list;
  168. struct notifier_block netdevice_nb;
  169. struct delayed_work traffic_dw;
  170. };
  171. int nsim_dev_hwstats_init(struct nsim_dev *nsim_dev);
  172. void nsim_dev_hwstats_exit(struct nsim_dev *nsim_dev);
  173. #if IS_ENABLED(CONFIG_PSAMPLE)
  174. int nsim_dev_psample_init(struct nsim_dev *nsim_dev);
  175. void nsim_dev_psample_exit(struct nsim_dev *nsim_dev);
  176. #else
  177. static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
  178. {
  179. return 0;
  180. }
  181. static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
  182. {
  183. }
  184. #endif
  185. enum nsim_dev_port_type {
  186. NSIM_DEV_PORT_TYPE_PF,
  187. NSIM_DEV_PORT_TYPE_VF,
  188. };
  189. #define NSIM_DEV_VF_PORT_INDEX_BASE 128
  190. #define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX
  191. struct nsim_dev_port {
  192. struct list_head list;
  193. struct devlink_port devlink_port;
  194. unsigned int port_index;
  195. enum nsim_dev_port_type port_type;
  196. struct dentry *ddir;
  197. struct dentry *rate_parent;
  198. char *parent_name;
  199. struct netdevsim *ns;
  200. };
  201. struct nsim_vf_config {
  202. int link_state;
  203. u16 min_tx_rate;
  204. u16 max_tx_rate;
  205. u16 vlan;
  206. __be16 vlan_proto;
  207. u16 qos;
  208. u8 vf_mac[ETH_ALEN];
  209. bool spoofchk_enabled;
  210. bool trusted;
  211. bool rss_query_enabled;
  212. };
  213. struct nsim_dev {
  214. struct nsim_bus_dev *nsim_bus_dev;
  215. struct nsim_fib_data *fib_data;
  216. struct nsim_trap_data *trap_data;
  217. struct dentry *ddir;
  218. struct dentry *ports_ddir;
  219. struct dentry *take_snapshot;
  220. struct dentry *nodes_ddir;
  221. struct nsim_vf_config *vfconfigs;
  222. struct bpf_offload_dev *bpf_dev;
  223. bool bpf_bind_accept;
  224. bool bpf_bind_verifier_accept;
  225. u32 bpf_bind_verifier_delay;
  226. struct dentry *ddir_bpf_bound_progs;
  227. u32 prog_id_gen;
  228. struct list_head bpf_bound_progs;
  229. struct list_head bpf_bound_maps;
  230. struct netdev_phys_item_id switch_id;
  231. struct list_head port_list;
  232. bool fw_update_status;
  233. u32 fw_update_overwrite_mask;
  234. u32 max_macs;
  235. bool test1;
  236. bool dont_allow_reload;
  237. bool fail_reload;
  238. struct devlink_region *dummy_region;
  239. struct nsim_dev_health health;
  240. struct nsim_dev_hwstats hwstats;
  241. struct flow_action_cookie *fa_cookie;
  242. spinlock_t fa_cookie_lock; /* protects fa_cookie */
  243. bool fail_trap_group_set;
  244. bool fail_trap_policer_set;
  245. bool fail_trap_policer_counter_get;
  246. bool fail_trap_drop_counter_get;
  247. struct {
  248. struct udp_tunnel_nic_shared utn_shared;
  249. u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
  250. bool sync_all;
  251. bool open_only;
  252. bool ipv4_only;
  253. bool shared;
  254. bool static_iana_vxlan;
  255. u32 sleep;
  256. } udp_ports;
  257. struct nsim_dev_psample *psample;
  258. u16 esw_mode;
  259. };
  260. static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
  261. {
  262. return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
  263. }
  264. static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
  265. {
  266. return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
  267. }
  268. static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
  269. {
  270. return devlink_net(priv_to_devlink(nsim_dev));
  271. }
  272. int nsim_dev_init(void);
  273. void nsim_dev_exit(void);
  274. int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev);
  275. void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev);
  276. int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev,
  277. enum nsim_dev_port_type type,
  278. unsigned int port_index);
  279. int nsim_drv_port_del(struct nsim_bus_dev *nsim_bus_dev,
  280. enum nsim_dev_port_type type,
  281. unsigned int port_index);
  282. int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
  283. unsigned int num_vfs);
  284. unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);
  285. struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
  286. struct netlink_ext_ack *extack);
  287. void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
  288. u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
  289. enum nsim_resource_id res_id, bool max);
  290. static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
  291. {
  292. return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;
  293. }
  294. static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port)
  295. {
  296. return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF;
  297. }
  298. #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
  299. void nsim_ipsec_init(struct netdevsim *ns);
  300. void nsim_ipsec_teardown(struct netdevsim *ns);
  301. bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
  302. #else
  303. static inline void nsim_ipsec_init(struct netdevsim *ns)
  304. {
  305. }
  306. static inline void nsim_ipsec_teardown(struct netdevsim *ns)
  307. {
  308. }
  309. static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
  310. {
  311. return true;
  312. }
  313. #endif
  314. struct nsim_bus_dev {
  315. struct device dev;
  316. struct list_head list;
  317. unsigned int port_count;
  318. unsigned int num_queues; /* Number of queues for each port on this bus */
  319. struct net *initial_net; /* Purpose of this is to carry net pointer
  320. * during the probe time only.
  321. */
  322. unsigned int max_vfs;
  323. unsigned int num_vfs;
  324. bool init;
  325. };
  326. int nsim_bus_init(void);
  327. void nsim_bus_exit(void);