netdevsim.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifdef CONFIG_BPF_SYSCALL
  83. int nsim_bpf_init(struct netdevsim *ns);
  84. void nsim_bpf_uninit(struct netdevsim *ns);
  85. int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
  86. int nsim_bpf_disable_tc(struct netdevsim *ns);
  87. int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
  88. void *type_data, void *cb_priv);
  89. #else
  90. static inline int nsim_bpf_init(struct netdevsim *ns)
  91. {
  92. return 0;
  93. }
  94. static inline void nsim_bpf_uninit(struct netdevsim *ns)
  95. {
  96. }
  97. static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
  98. {
  99. return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP;
  100. }
  101. static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
  102. {
  103. return 0;
  104. }
  105. static inline int
  106. nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  107. void *cb_priv)
  108. {
  109. return -EOPNOTSUPP;
  110. }
  111. #endif
  112. #if IS_ENABLED(CONFIG_NET_DEVLINK)
  113. enum nsim_resource_id {
  114. NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
  115. NSIM_RESOURCE_IPV4,
  116. NSIM_RESOURCE_IPV4_FIB,
  117. NSIM_RESOURCE_IPV4_FIB_RULES,
  118. NSIM_RESOURCE_IPV6,
  119. NSIM_RESOURCE_IPV6_FIB,
  120. NSIM_RESOURCE_IPV6_FIB_RULES,
  121. };
  122. int nsim_devlink_setup(struct netdevsim *ns);
  123. void nsim_devlink_teardown(struct netdevsim *ns);
  124. int nsim_devlink_init(void);
  125. void nsim_devlink_exit(void);
  126. int nsim_fib_init(void);
  127. void nsim_fib_exit(void);
  128. u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max);
  129. int nsim_fib_set_max(struct net *net, enum nsim_resource_id res_id, u64 val,
  130. struct netlink_ext_ack *extack);
  131. #else
  132. static inline int nsim_devlink_setup(struct netdevsim *ns)
  133. {
  134. return 0;
  135. }
  136. static inline void nsim_devlink_teardown(struct netdevsim *ns)
  137. {
  138. }
  139. static inline int nsim_devlink_init(void)
  140. {
  141. return 0;
  142. }
  143. static inline void nsim_devlink_exit(void)
  144. {
  145. }
  146. #endif
  147. #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
  148. void nsim_ipsec_init(struct netdevsim *ns);
  149. void nsim_ipsec_teardown(struct netdevsim *ns);
  150. bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
  151. #else
  152. static inline void nsim_ipsec_init(struct netdevsim *ns)
  153. {
  154. }
  155. static inline void nsim_ipsec_teardown(struct netdevsim *ns)
  156. {
  157. }
  158. static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
  159. {
  160. return true;
  161. }
  162. #endif
  163. static inline struct netdevsim *to_nsim(struct device *ptr)
  164. {
  165. return container_of(ptr, struct netdevsim, dev);
  166. }