netdevsim.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #define DRV_NAME "netdevsim"
  21. #define NSIM_XDP_MAX_MTU 4000
  22. #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
  23. struct bpf_prog;
  24. struct dentry;
  25. struct nsim_vf_config;
  26. struct netdevsim {
  27. struct net_device *netdev;
  28. u64 tx_packets;
  29. u64 tx_bytes;
  30. struct u64_stats_sync syncp;
  31. struct device dev;
  32. struct dentry *ddir;
  33. unsigned int num_vfs;
  34. struct nsim_vf_config *vfconfigs;
  35. struct bpf_prog *bpf_offloaded;
  36. u32 bpf_offloaded_id;
  37. u32 xdp_flags;
  38. int xdp_prog_mode;
  39. struct bpf_prog *xdp_prog;
  40. u32 prog_id_gen;
  41. bool bpf_bind_accept;
  42. u32 bpf_bind_verifier_delay;
  43. struct dentry *ddir_bpf_bound_progs;
  44. struct list_head bpf_bound_progs;
  45. bool bpf_tc_accept;
  46. bool bpf_tc_non_bound_accept;
  47. bool bpf_xdpdrv_accept;
  48. bool bpf_xdpoffload_accept;
  49. bool bpf_map_accept;
  50. struct list_head bpf_bound_maps;
  51. #if IS_ENABLED(CONFIG_NET_DEVLINK)
  52. struct devlink *devlink;
  53. #endif
  54. };
  55. extern struct dentry *nsim_ddir;
  56. #ifdef CONFIG_BPF_SYSCALL
  57. int nsim_bpf_init(struct netdevsim *ns);
  58. void nsim_bpf_uninit(struct netdevsim *ns);
  59. int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
  60. int nsim_bpf_disable_tc(struct netdevsim *ns);
  61. int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
  62. void *type_data, void *cb_priv);
  63. #else
  64. static inline int nsim_bpf_init(struct netdevsim *ns)
  65. {
  66. return 0;
  67. }
  68. static inline void nsim_bpf_uninit(struct netdevsim *ns)
  69. {
  70. }
  71. static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
  72. {
  73. return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP;
  74. }
  75. static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
  76. {
  77. return 0;
  78. }
  79. static inline int
  80. nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  81. void *cb_priv)
  82. {
  83. return -EOPNOTSUPP;
  84. }
  85. #endif
  86. #if IS_ENABLED(CONFIG_NET_DEVLINK)
  87. enum nsim_resource_id {
  88. NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
  89. NSIM_RESOURCE_IPV4,
  90. NSIM_RESOURCE_IPV4_FIB,
  91. NSIM_RESOURCE_IPV4_FIB_RULES,
  92. NSIM_RESOURCE_IPV6,
  93. NSIM_RESOURCE_IPV6_FIB,
  94. NSIM_RESOURCE_IPV6_FIB_RULES,
  95. };
  96. int nsim_devlink_setup(struct netdevsim *ns);
  97. void nsim_devlink_teardown(struct netdevsim *ns);
  98. int nsim_devlink_init(void);
  99. void nsim_devlink_exit(void);
  100. int nsim_fib_init(void);
  101. void nsim_fib_exit(void);
  102. u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max);
  103. int nsim_fib_set_max(struct net *net, enum nsim_resource_id res_id, u64 val,
  104. struct netlink_ext_ack *extack);
  105. #else
  106. static inline int nsim_devlink_setup(struct netdevsim *ns)
  107. {
  108. return 0;
  109. }
  110. static inline void nsim_devlink_teardown(struct netdevsim *ns)
  111. {
  112. }
  113. static inline int nsim_devlink_init(void)
  114. {
  115. return 0;
  116. }
  117. static inline void nsim_devlink_exit(void)
  118. {
  119. }
  120. #endif
  121. static inline struct netdevsim *to_nsim(struct device *ptr)
  122. {
  123. return container_of(ptr, struct netdevsim, dev);
  124. }