devlink.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * include/net/devlink.h - Network physical device Netlink interface
  3. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #ifndef _NET_DEVLINK_H_
  12. #define _NET_DEVLINK_H_
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/gfp.h>
  16. #include <linux/list.h>
  17. #include <linux/netdevice.h>
  18. #include <net/net_namespace.h>
  19. #include <uapi/linux/devlink.h>
  20. struct devlink_ops;
  21. struct devlink {
  22. struct list_head list;
  23. struct list_head port_list;
  24. struct list_head sb_list;
  25. const struct devlink_ops *ops;
  26. struct device *dev;
  27. possible_net_t _net;
  28. char priv[0] __aligned(NETDEV_ALIGN);
  29. };
  30. struct devlink_port {
  31. struct list_head list;
  32. struct devlink *devlink;
  33. unsigned index;
  34. bool registered;
  35. enum devlink_port_type type;
  36. enum devlink_port_type desired_type;
  37. void *type_dev;
  38. bool split;
  39. u32 split_group;
  40. };
  41. struct devlink_sb_pool_info {
  42. enum devlink_sb_pool_type pool_type;
  43. u32 size;
  44. enum devlink_sb_threshold_type threshold_type;
  45. };
  46. struct devlink_ops {
  47. int (*port_type_set)(struct devlink_port *devlink_port,
  48. enum devlink_port_type port_type);
  49. int (*port_split)(struct devlink *devlink, unsigned int port_index,
  50. unsigned int count);
  51. int (*port_unsplit)(struct devlink *devlink, unsigned int port_index);
  52. int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
  53. u16 pool_index,
  54. struct devlink_sb_pool_info *pool_info);
  55. int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
  56. u16 pool_index, u32 size,
  57. enum devlink_sb_threshold_type threshold_type);
  58. int (*sb_port_pool_get)(struct devlink_port *devlink_port,
  59. unsigned int sb_index, u16 pool_index,
  60. u32 *p_threshold);
  61. int (*sb_port_pool_set)(struct devlink_port *devlink_port,
  62. unsigned int sb_index, u16 pool_index,
  63. u32 threshold);
  64. int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
  65. unsigned int sb_index,
  66. u16 tc_index,
  67. enum devlink_sb_pool_type pool_type,
  68. u16 *p_pool_index, u32 *p_threshold);
  69. int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
  70. unsigned int sb_index,
  71. u16 tc_index,
  72. enum devlink_sb_pool_type pool_type,
  73. u16 pool_index, u32 threshold);
  74. int (*sb_occ_snapshot)(struct devlink *devlink,
  75. unsigned int sb_index);
  76. int (*sb_occ_max_clear)(struct devlink *devlink,
  77. unsigned int sb_index);
  78. int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
  79. unsigned int sb_index, u16 pool_index,
  80. u32 *p_cur, u32 *p_max);
  81. int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
  82. unsigned int sb_index,
  83. u16 tc_index,
  84. enum devlink_sb_pool_type pool_type,
  85. u32 *p_cur, u32 *p_max);
  86. int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
  87. int (*eswitch_mode_set)(struct devlink *devlink, u16 mode);
  88. };
  89. static inline void *devlink_priv(struct devlink *devlink)
  90. {
  91. BUG_ON(!devlink);
  92. return &devlink->priv;
  93. }
  94. static inline struct devlink *priv_to_devlink(void *priv)
  95. {
  96. BUG_ON(!priv);
  97. return container_of(priv, struct devlink, priv);
  98. }
  99. struct ib_device;
  100. #if IS_ENABLED(CONFIG_NET_DEVLINK)
  101. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
  102. int devlink_register(struct devlink *devlink, struct device *dev);
  103. void devlink_unregister(struct devlink *devlink);
  104. void devlink_free(struct devlink *devlink);
  105. int devlink_port_register(struct devlink *devlink,
  106. struct devlink_port *devlink_port,
  107. unsigned int port_index);
  108. void devlink_port_unregister(struct devlink_port *devlink_port);
  109. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  110. struct net_device *netdev);
  111. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  112. struct ib_device *ibdev);
  113. void devlink_port_type_clear(struct devlink_port *devlink_port);
  114. void devlink_port_split_set(struct devlink_port *devlink_port,
  115. u32 split_group);
  116. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  117. u32 size, u16 ingress_pools_count,
  118. u16 egress_pools_count, u16 ingress_tc_count,
  119. u16 egress_tc_count);
  120. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
  121. #else
  122. static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
  123. size_t priv_size)
  124. {
  125. return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL);
  126. }
  127. static inline int devlink_register(struct devlink *devlink, struct device *dev)
  128. {
  129. return 0;
  130. }
  131. static inline void devlink_unregister(struct devlink *devlink)
  132. {
  133. }
  134. static inline void devlink_free(struct devlink *devlink)
  135. {
  136. kfree(devlink);
  137. }
  138. static inline int devlink_port_register(struct devlink *devlink,
  139. struct devlink_port *devlink_port,
  140. unsigned int port_index)
  141. {
  142. return 0;
  143. }
  144. static inline void devlink_port_unregister(struct devlink_port *devlink_port)
  145. {
  146. }
  147. static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  148. struct net_device *netdev)
  149. {
  150. }
  151. static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  152. struct ib_device *ibdev)
  153. {
  154. }
  155. static inline void devlink_port_type_clear(struct devlink_port *devlink_port)
  156. {
  157. }
  158. static inline void devlink_port_split_set(struct devlink_port *devlink_port,
  159. u32 split_group)
  160. {
  161. }
  162. static inline int devlink_sb_register(struct devlink *devlink,
  163. unsigned int sb_index, u32 size,
  164. u16 ingress_pools_count,
  165. u16 egress_pools_count,
  166. u16 ingress_tc_count,
  167. u16 egress_tc_count)
  168. {
  169. return 0;
  170. }
  171. static inline void devlink_sb_unregister(struct devlink *devlink,
  172. unsigned int sb_index)
  173. {
  174. }
  175. #endif
  176. #endif /* _NET_DEVLINK_H_ */