netdevsim.h 11 KB

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