ynl.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. #ifndef __YNL_C_H
  3. #define __YNL_C_H 1
  4. #include <stddef.h>
  5. #include <linux/genetlink.h>
  6. #include <linux/types.h>
  7. #include "ynl-priv.h"
  8. enum ynl_error_code {
  9. YNL_ERROR_NONE = 0,
  10. __YNL_ERRNO_END = 4096,
  11. YNL_ERROR_INTERNAL,
  12. YNL_ERROR_DUMP_INTER,
  13. YNL_ERROR_EXPECT_ACK,
  14. YNL_ERROR_EXPECT_MSG,
  15. YNL_ERROR_UNEXPECT_MSG,
  16. YNL_ERROR_ATTR_MISSING,
  17. YNL_ERROR_ATTR_INVALID,
  18. YNL_ERROR_UNKNOWN_NTF,
  19. YNL_ERROR_INV_RESP,
  20. YNL_ERROR_INPUT_INVALID,
  21. YNL_ERROR_INPUT_TOO_BIG,
  22. };
  23. /**
  24. * struct ynl_error - error encountered by YNL
  25. * @code: errno (low values) or YNL error code (enum ynl_error_code)
  26. * @attr_offs: offset of bad attribute (for very advanced users)
  27. * @msg: error message
  28. *
  29. * Error information for when YNL operations fail.
  30. * Users should interact with the err member of struct ynl_sock directly.
  31. * The main exception to that rule is ynl_sock_create().
  32. */
  33. struct ynl_error {
  34. enum ynl_error_code code;
  35. unsigned int attr_offs;
  36. char msg[512];
  37. };
  38. /**
  39. * struct ynl_family - YNL family info
  40. * Family description generated by codegen. Pass to ynl_sock_create().
  41. */
  42. struct ynl_family {
  43. /* private: */
  44. const char *name;
  45. size_t hdr_len;
  46. const struct ynl_ntf_info *ntf_info;
  47. unsigned int ntf_info_size;
  48. };
  49. /**
  50. * struct ynl_sock - YNL wrapped netlink socket
  51. * @err: YNL error descriptor, cleared on every request.
  52. */
  53. struct ynl_sock {
  54. struct ynl_error err;
  55. /* private: */
  56. const struct ynl_family *family;
  57. int socket;
  58. __u32 seq;
  59. __u32 portid;
  60. __u16 family_id;
  61. unsigned int n_mcast_groups;
  62. struct {
  63. unsigned int id;
  64. char name[GENL_NAMSIZ];
  65. } *mcast_groups;
  66. struct ynl_ntf_base_type *ntf_first;
  67. struct ynl_ntf_base_type **ntf_last_next;
  68. struct nlmsghdr *nlh;
  69. const struct ynl_policy_nest *req_policy;
  70. unsigned char *tx_buf;
  71. unsigned char *rx_buf;
  72. unsigned char raw_buf[];
  73. };
  74. struct ynl_sock *
  75. ynl_sock_create(const struct ynl_family *yf, struct ynl_error *e);
  76. void ynl_sock_destroy(struct ynl_sock *ys);
  77. #define ynl_dump_foreach(dump, iter) \
  78. for (typeof(dump->obj) *iter = &dump->obj; \
  79. !ynl_dump_obj_is_last(iter); \
  80. iter = ynl_dump_obj_next(iter))
  81. /**
  82. * ynl_dump_empty() - does the dump have no entries
  83. * @dump: pointer to the dump list, as returned by a dump call
  84. *
  85. * Check if the dump is empty, i.e. contains no objects.
  86. * Dump calls return NULL on error, and terminator element if empty.
  87. */
  88. static inline bool ynl_dump_empty(void *dump)
  89. {
  90. return dump == (void *)YNL_LIST_END;
  91. }
  92. int ynl_subscribe(struct ynl_sock *ys, const char *grp_name);
  93. int ynl_socket_get_fd(struct ynl_sock *ys);
  94. int ynl_ntf_check(struct ynl_sock *ys);
  95. /**
  96. * ynl_has_ntf() - check if socket has *parsed* notifications
  97. * @ys: active YNL socket
  98. *
  99. * Note that this does not take into account notifications sitting
  100. * in netlink socket, just the notifications which have already been
  101. * read and parsed (e.g. during a ynl_ntf_check() call).
  102. */
  103. static inline bool ynl_has_ntf(struct ynl_sock *ys)
  104. {
  105. return ys->ntf_last_next != &ys->ntf_first;
  106. }
  107. struct ynl_ntf_base_type *ynl_ntf_dequeue(struct ynl_sock *ys);
  108. void ynl_ntf_free(struct ynl_ntf_base_type *ntf);
  109. #endif