netdevsim.h 4.3 KB

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