netdevsim.h 4.5 KB

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