netdevsim.h 10 KB

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