devlink.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. struct list_head dpipe_table_list;
  26. struct devlink_dpipe_headers *dpipe_headers;
  27. const struct devlink_ops *ops;
  28. struct device *dev;
  29. possible_net_t _net;
  30. char priv[0] __aligned(NETDEV_ALIGN);
  31. };
  32. struct devlink_port {
  33. struct list_head list;
  34. struct devlink *devlink;
  35. unsigned index;
  36. bool registered;
  37. enum devlink_port_type type;
  38. enum devlink_port_type desired_type;
  39. void *type_dev;
  40. bool split;
  41. u32 split_group;
  42. };
  43. struct devlink_sb_pool_info {
  44. enum devlink_sb_pool_type pool_type;
  45. u32 size;
  46. enum devlink_sb_threshold_type threshold_type;
  47. };
  48. /**
  49. * struct devlink_dpipe_field - dpipe field object
  50. * @name: field name
  51. * @id: index inside the headers field array
  52. * @bitwidth: bitwidth
  53. * @mapping_type: mapping type
  54. */
  55. struct devlink_dpipe_field {
  56. const char *name;
  57. unsigned int id;
  58. unsigned int bitwidth;
  59. enum devlink_dpipe_field_mapping_type mapping_type;
  60. };
  61. /**
  62. * struct devlink_dpipe_header - dpipe header object
  63. * @name: header name
  64. * @id: index, global/local detrmined by global bit
  65. * @fields: fields
  66. * @fields_count: number of fields
  67. * @global: indicates if header is shared like most protocol header
  68. * or driver specific
  69. */
  70. struct devlink_dpipe_header {
  71. const char *name;
  72. unsigned int id;
  73. struct devlink_dpipe_field *fields;
  74. unsigned int fields_count;
  75. bool global;
  76. };
  77. /**
  78. * struct devlink_dpipe_match - represents match operation
  79. * @type: type of match
  80. * @header_index: header index (packets can have several headers of same
  81. * type like in case of tunnels)
  82. * @header: header
  83. * @fieled_id: field index
  84. */
  85. struct devlink_dpipe_match {
  86. enum devlink_dpipe_match_type type;
  87. unsigned int header_index;
  88. struct devlink_dpipe_header *header;
  89. unsigned int field_id;
  90. };
  91. /**
  92. * struct devlink_dpipe_action - represents action operation
  93. * @type: type of action
  94. * @header_index: header index (packets can have several headers of same
  95. * type like in case of tunnels)
  96. * @header: header
  97. * @fieled_id: field index
  98. */
  99. struct devlink_dpipe_action {
  100. enum devlink_dpipe_action_type type;
  101. unsigned int header_index;
  102. struct devlink_dpipe_header *header;
  103. unsigned int field_id;
  104. };
  105. /**
  106. * struct devlink_dpipe_value - represents value of match/action
  107. * @action: action
  108. * @match: match
  109. * @mapping_value: in case the field has some mapping this value
  110. * specified the mapping value
  111. * @mapping_valid: specify if mapping value is valid
  112. * @value_size: value size
  113. * @value: value
  114. * @mask: bit mask
  115. */
  116. struct devlink_dpipe_value {
  117. union {
  118. struct devlink_dpipe_action *action;
  119. struct devlink_dpipe_match *match;
  120. };
  121. unsigned int mapping_value;
  122. bool mapping_valid;
  123. unsigned int value_size;
  124. void *value;
  125. void *mask;
  126. };
  127. /**
  128. * struct devlink_dpipe_entry - table entry object
  129. * @index: index of the entry in the table
  130. * @match_values: match values
  131. * @matche_values_count: count of matches tuples
  132. * @action_values: actions values
  133. * @action_values_count: count of actions values
  134. * @counter: value of counter
  135. * @counter_valid: Specify if value is valid from hardware
  136. */
  137. struct devlink_dpipe_entry {
  138. u64 index;
  139. struct devlink_dpipe_value *match_values;
  140. unsigned int match_values_count;
  141. struct devlink_dpipe_value *action_values;
  142. unsigned int action_values_count;
  143. u64 counter;
  144. bool counter_valid;
  145. };
  146. /**
  147. * struct devlink_dpipe_dump_ctx - context provided to driver in order
  148. * to dump
  149. * @info: info
  150. * @cmd: devlink command
  151. * @skb: skb
  152. * @nest: top attribute
  153. * @hdr: hdr
  154. */
  155. struct devlink_dpipe_dump_ctx {
  156. struct genl_info *info;
  157. enum devlink_command cmd;
  158. struct sk_buff *skb;
  159. struct nlattr *nest;
  160. void *hdr;
  161. };
  162. struct devlink_dpipe_table_ops;
  163. /**
  164. * struct devlink_dpipe_table - table object
  165. * @priv: private
  166. * @name: table name
  167. * @counters_enabled: indicates if counters are active
  168. * @counter_control_extern: indicates if counter control is in dpipe or
  169. * external tool
  170. * @table_ops: table operations
  171. * @rcu: rcu
  172. */
  173. struct devlink_dpipe_table {
  174. void *priv;
  175. struct list_head list;
  176. const char *name;
  177. bool counters_enabled;
  178. bool counter_control_extern;
  179. struct devlink_dpipe_table_ops *table_ops;
  180. struct rcu_head rcu;
  181. };
  182. /**
  183. * struct devlink_dpipe_table_ops - dpipe_table ops
  184. * @actions_dump - dumps all tables actions
  185. * @matches_dump - dumps all tables matches
  186. * @entries_dump - dumps all active entries in the table
  187. * @counters_set_update - when changing the counter status hardware sync
  188. * maybe needed to allocate/free counter related
  189. * resources
  190. * @size_get - get size
  191. */
  192. struct devlink_dpipe_table_ops {
  193. int (*actions_dump)(void *priv, struct sk_buff *skb);
  194. int (*matches_dump)(void *priv, struct sk_buff *skb);
  195. int (*entries_dump)(void *priv, bool counters_enabled,
  196. struct devlink_dpipe_dump_ctx *dump_ctx);
  197. int (*counters_set_update)(void *priv, bool enable);
  198. u64 (*size_get)(void *priv);
  199. };
  200. /**
  201. * struct devlink_dpipe_headers - dpipe headers
  202. * @headers - header array can be shared (global bit) or driver specific
  203. * @headers_count - count of headers
  204. */
  205. struct devlink_dpipe_headers {
  206. struct devlink_dpipe_header **headers;
  207. unsigned int headers_count;
  208. };
  209. struct devlink_ops {
  210. int (*port_type_set)(struct devlink_port *devlink_port,
  211. enum devlink_port_type port_type);
  212. int (*port_split)(struct devlink *devlink, unsigned int port_index,
  213. unsigned int count);
  214. int (*port_unsplit)(struct devlink *devlink, unsigned int port_index);
  215. int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
  216. u16 pool_index,
  217. struct devlink_sb_pool_info *pool_info);
  218. int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
  219. u16 pool_index, u32 size,
  220. enum devlink_sb_threshold_type threshold_type);
  221. int (*sb_port_pool_get)(struct devlink_port *devlink_port,
  222. unsigned int sb_index, u16 pool_index,
  223. u32 *p_threshold);
  224. int (*sb_port_pool_set)(struct devlink_port *devlink_port,
  225. unsigned int sb_index, u16 pool_index,
  226. u32 threshold);
  227. int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
  228. unsigned int sb_index,
  229. u16 tc_index,
  230. enum devlink_sb_pool_type pool_type,
  231. u16 *p_pool_index, u32 *p_threshold);
  232. int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
  233. unsigned int sb_index,
  234. u16 tc_index,
  235. enum devlink_sb_pool_type pool_type,
  236. u16 pool_index, u32 threshold);
  237. int (*sb_occ_snapshot)(struct devlink *devlink,
  238. unsigned int sb_index);
  239. int (*sb_occ_max_clear)(struct devlink *devlink,
  240. unsigned int sb_index);
  241. int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
  242. unsigned int sb_index, u16 pool_index,
  243. u32 *p_cur, u32 *p_max);
  244. int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
  245. unsigned int sb_index,
  246. u16 tc_index,
  247. enum devlink_sb_pool_type pool_type,
  248. u32 *p_cur, u32 *p_max);
  249. int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
  250. int (*eswitch_mode_set)(struct devlink *devlink, u16 mode);
  251. int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
  252. int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode);
  253. int (*eswitch_encap_mode_get)(struct devlink *devlink, u8 *p_encap_mode);
  254. int (*eswitch_encap_mode_set)(struct devlink *devlink, u8 encap_mode);
  255. };
  256. static inline void *devlink_priv(struct devlink *devlink)
  257. {
  258. BUG_ON(!devlink);
  259. return &devlink->priv;
  260. }
  261. static inline struct devlink *priv_to_devlink(void *priv)
  262. {
  263. BUG_ON(!priv);
  264. return container_of(priv, struct devlink, priv);
  265. }
  266. struct ib_device;
  267. #if IS_ENABLED(CONFIG_NET_DEVLINK)
  268. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
  269. int devlink_register(struct devlink *devlink, struct device *dev);
  270. void devlink_unregister(struct devlink *devlink);
  271. void devlink_free(struct devlink *devlink);
  272. int devlink_port_register(struct devlink *devlink,
  273. struct devlink_port *devlink_port,
  274. unsigned int port_index);
  275. void devlink_port_unregister(struct devlink_port *devlink_port);
  276. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  277. struct net_device *netdev);
  278. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  279. struct ib_device *ibdev);
  280. void devlink_port_type_clear(struct devlink_port *devlink_port);
  281. void devlink_port_split_set(struct devlink_port *devlink_port,
  282. u32 split_group);
  283. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  284. u32 size, u16 ingress_pools_count,
  285. u16 egress_pools_count, u16 ingress_tc_count,
  286. u16 egress_tc_count);
  287. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
  288. int devlink_dpipe_table_register(struct devlink *devlink,
  289. const char *table_name,
  290. struct devlink_dpipe_table_ops *table_ops,
  291. void *priv, bool counter_control_extern);
  292. void devlink_dpipe_table_unregister(struct devlink *devlink,
  293. const char *table_name);
  294. int devlink_dpipe_headers_register(struct devlink *devlink,
  295. struct devlink_dpipe_headers *dpipe_headers);
  296. void devlink_dpipe_headers_unregister(struct devlink *devlink);
  297. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  298. const char *table_name);
  299. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx);
  300. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  301. struct devlink_dpipe_entry *entry);
  302. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx);
  303. void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry);
  304. int devlink_dpipe_action_put(struct sk_buff *skb,
  305. struct devlink_dpipe_action *action);
  306. int devlink_dpipe_match_put(struct sk_buff *skb,
  307. struct devlink_dpipe_match *match);
  308. extern struct devlink_dpipe_header devlink_dpipe_header_ethernet;
  309. extern struct devlink_dpipe_header devlink_dpipe_header_ipv4;
  310. extern struct devlink_dpipe_header devlink_dpipe_header_ipv6;
  311. #else
  312. static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
  313. size_t priv_size)
  314. {
  315. return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL);
  316. }
  317. static inline int devlink_register(struct devlink *devlink, struct device *dev)
  318. {
  319. return 0;
  320. }
  321. static inline void devlink_unregister(struct devlink *devlink)
  322. {
  323. }
  324. static inline void devlink_free(struct devlink *devlink)
  325. {
  326. kfree(devlink);
  327. }
  328. static inline int devlink_port_register(struct devlink *devlink,
  329. struct devlink_port *devlink_port,
  330. unsigned int port_index)
  331. {
  332. return 0;
  333. }
  334. static inline void devlink_port_unregister(struct devlink_port *devlink_port)
  335. {
  336. }
  337. static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  338. struct net_device *netdev)
  339. {
  340. }
  341. static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  342. struct ib_device *ibdev)
  343. {
  344. }
  345. static inline void devlink_port_type_clear(struct devlink_port *devlink_port)
  346. {
  347. }
  348. static inline void devlink_port_split_set(struct devlink_port *devlink_port,
  349. u32 split_group)
  350. {
  351. }
  352. static inline int devlink_sb_register(struct devlink *devlink,
  353. unsigned int sb_index, u32 size,
  354. u16 ingress_pools_count,
  355. u16 egress_pools_count,
  356. u16 ingress_tc_count,
  357. u16 egress_tc_count)
  358. {
  359. return 0;
  360. }
  361. static inline void devlink_sb_unregister(struct devlink *devlink,
  362. unsigned int sb_index)
  363. {
  364. }
  365. static inline int
  366. devlink_dpipe_table_register(struct devlink *devlink,
  367. const char *table_name,
  368. struct devlink_dpipe_table_ops *table_ops,
  369. void *priv, bool counter_control_extern)
  370. {
  371. return 0;
  372. }
  373. static inline void devlink_dpipe_table_unregister(struct devlink *devlink,
  374. const char *table_name)
  375. {
  376. }
  377. static inline int devlink_dpipe_headers_register(struct devlink *devlink,
  378. struct devlink_dpipe_headers *
  379. dpipe_headers)
  380. {
  381. return 0;
  382. }
  383. static inline void devlink_dpipe_headers_unregister(struct devlink *devlink)
  384. {
  385. }
  386. static inline bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  387. const char *table_name)
  388. {
  389. return false;
  390. }
  391. static inline int
  392. devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  393. {
  394. return 0;
  395. }
  396. static inline int
  397. devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  398. struct devlink_dpipe_entry *entry)
  399. {
  400. return 0;
  401. }
  402. static inline int
  403. devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  404. {
  405. return 0;
  406. }
  407. static inline void
  408. devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
  409. {
  410. }
  411. static inline int
  412. devlink_dpipe_action_put(struct sk_buff *skb,
  413. struct devlink_dpipe_action *action)
  414. {
  415. return 0;
  416. }
  417. static inline int
  418. devlink_dpipe_match_put(struct sk_buff *skb,
  419. struct devlink_dpipe_match *match)
  420. {
  421. return 0;
  422. }
  423. #endif
  424. #endif /* _NET_DEVLINK_H_ */