messenger.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef __FS_CEPH_MESSENGER_H
  2. #define __FS_CEPH_MESSENGER_H
  3. #include <linux/kref.h>
  4. #include <linux/mutex.h>
  5. #include <linux/net.h>
  6. #include <linux/radix-tree.h>
  7. #include <linux/uio.h>
  8. #include <linux/version.h>
  9. #include <linux/workqueue.h>
  10. #include "types.h"
  11. #include "buffer.h"
  12. struct ceph_msg;
  13. struct ceph_connection;
  14. extern struct workqueue_struct *ceph_msgr_wq; /* receive work queue */
  15. /*
  16. * Ceph defines these callbacks for handling connection events.
  17. */
  18. struct ceph_connection_operations {
  19. struct ceph_connection *(*get)(struct ceph_connection *);
  20. void (*put)(struct ceph_connection *);
  21. /* handle an incoming message. */
  22. void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
  23. /* authorize an outgoing connection */
  24. int (*get_authorizer) (struct ceph_connection *con,
  25. void **buf, int *len, int *proto,
  26. void **reply_buf, int *reply_len, int force_new);
  27. int (*verify_authorizer_reply) (struct ceph_connection *con, int len);
  28. int (*invalidate_authorizer)(struct ceph_connection *con);
  29. /* protocol version mismatch */
  30. void (*bad_proto) (struct ceph_connection *con);
  31. /* there was some error on the socket (disconnect, whatever) */
  32. void (*fault) (struct ceph_connection *con);
  33. /* a remote host as terminated a message exchange session, and messages
  34. * we sent (or they tried to send us) may be lost. */
  35. void (*peer_reset) (struct ceph_connection *con);
  36. struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
  37. struct ceph_msg_header *hdr,
  38. int *skip);
  39. };
  40. /* use format string %s%d */
  41. #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
  42. struct ceph_messenger {
  43. struct ceph_entity_inst inst; /* my name+address */
  44. struct ceph_entity_addr my_enc_addr;
  45. struct page *zero_page; /* used in certain error cases */
  46. bool nocrc;
  47. /*
  48. * the global_seq counts connections i (attempt to) initiate
  49. * in order to disambiguate certain connect race conditions.
  50. */
  51. u32 global_seq;
  52. spinlock_t global_seq_lock;
  53. u32 supported_features;
  54. u32 required_features;
  55. };
  56. /*
  57. * a single message. it contains a header (src, dest, message type, etc.),
  58. * footer (crc values, mainly), a "front" message body, and possibly a
  59. * data payload (stored in some number of pages).
  60. */
  61. struct ceph_msg {
  62. struct ceph_msg_header hdr; /* header */
  63. struct ceph_msg_footer footer; /* footer */
  64. struct kvec front; /* unaligned blobs of message */
  65. struct ceph_buffer *middle;
  66. struct page **pages; /* data payload. NOT OWNER. */
  67. unsigned nr_pages; /* size of page array */
  68. struct ceph_pagelist *pagelist; /* instead of pages */
  69. struct list_head list_head;
  70. struct kref kref;
  71. struct bio *bio; /* instead of pages/pagelist */
  72. struct bio *bio_iter; /* bio iterator */
  73. int bio_seg; /* current bio segment */
  74. struct ceph_pagelist *trail; /* the trailing part of the data */
  75. bool front_is_vmalloc;
  76. bool more_to_follow;
  77. bool needs_out_seq;
  78. int front_max;
  79. struct ceph_msgpool *pool;
  80. };
  81. struct ceph_msg_pos {
  82. int page, page_pos; /* which page; offset in page */
  83. int data_pos; /* offset in data payload */
  84. int did_page_crc; /* true if we've calculated crc for current page */
  85. };
  86. /* ceph connection fault delay defaults, for exponential backoff */
  87. #define BASE_DELAY_INTERVAL (HZ/2)
  88. #define MAX_DELAY_INTERVAL (5 * 60 * HZ)
  89. /*
  90. * ceph_connection state bit flags
  91. *
  92. * QUEUED and BUSY are used together to ensure that only a single
  93. * thread is currently opening, reading or writing data to the socket.
  94. */
  95. #define LOSSYTX 0 /* we can close channel or drop messages on errors */
  96. #define CONNECTING 1
  97. #define NEGOTIATING 2
  98. #define KEEPALIVE_PENDING 3
  99. #define WRITE_PENDING 4 /* we have data ready to send */
  100. #define QUEUED 5 /* there is work queued on this connection */
  101. #define BUSY 6 /* work is being done */
  102. #define STANDBY 8 /* no outgoing messages, socket closed. we keep
  103. * the ceph_connection around to maintain shared
  104. * state with the peer. */
  105. #define CLOSED 10 /* we've closed the connection */
  106. #define SOCK_CLOSED 11 /* socket state changed to closed */
  107. #define OPENING 13 /* open connection w/ (possibly new) peer */
  108. #define DEAD 14 /* dead, about to kfree */
  109. /*
  110. * A single connection with another host.
  111. *
  112. * We maintain a queue of outgoing messages, and some session state to
  113. * ensure that we can preserve the lossless, ordered delivery of
  114. * messages in the case of a TCP disconnect.
  115. */
  116. struct ceph_connection {
  117. void *private;
  118. atomic_t nref;
  119. const struct ceph_connection_operations *ops;
  120. struct ceph_messenger *msgr;
  121. struct socket *sock;
  122. unsigned long state; /* connection state (see flags above) */
  123. const char *error_msg; /* error message, if any */
  124. struct ceph_entity_addr peer_addr; /* peer address */
  125. struct ceph_entity_name peer_name; /* peer name */
  126. struct ceph_entity_addr peer_addr_for_me;
  127. unsigned peer_features;
  128. u32 connect_seq; /* identify the most recent connection
  129. attempt for this connection, client */
  130. u32 peer_global_seq; /* peer's global seq for this connection */
  131. int auth_retry; /* true if we need a newer authorizer */
  132. void *auth_reply_buf; /* where to put the authorizer reply */
  133. int auth_reply_buf_len;
  134. struct mutex mutex;
  135. /* out queue */
  136. struct list_head out_queue;
  137. struct list_head out_sent; /* sending or sent but unacked */
  138. u64 out_seq; /* last message queued for send */
  139. bool out_keepalive_pending;
  140. u64 in_seq, in_seq_acked; /* last message received, acked */
  141. /* connection negotiation temps */
  142. char in_banner[CEPH_BANNER_MAX_LEN];
  143. union {
  144. struct { /* outgoing connection */
  145. struct ceph_msg_connect out_connect;
  146. struct ceph_msg_connect_reply in_reply;
  147. };
  148. struct { /* incoming */
  149. struct ceph_msg_connect in_connect;
  150. struct ceph_msg_connect_reply out_reply;
  151. };
  152. };
  153. struct ceph_entity_addr actual_peer_addr;
  154. /* message out temps */
  155. struct ceph_msg *out_msg; /* sending message (== tail of
  156. out_sent) */
  157. bool out_msg_done;
  158. struct ceph_msg_pos out_msg_pos;
  159. struct kvec out_kvec[8], /* sending header/footer data */
  160. *out_kvec_cur;
  161. int out_kvec_left; /* kvec's left in out_kvec */
  162. int out_skip; /* skip this many bytes */
  163. int out_kvec_bytes; /* total bytes left */
  164. bool out_kvec_is_msg; /* kvec refers to out_msg */
  165. int out_more; /* there is more data after the kvecs */
  166. __le64 out_temp_ack; /* for writing an ack */
  167. /* message in temps */
  168. struct ceph_msg_header in_hdr;
  169. struct ceph_msg *in_msg;
  170. struct ceph_msg_pos in_msg_pos;
  171. u32 in_front_crc, in_middle_crc, in_data_crc; /* calculated crc */
  172. char in_tag; /* protocol control byte */
  173. int in_base_pos; /* bytes read */
  174. __le64 in_temp_ack; /* for reading an ack */
  175. struct delayed_work work; /* send|recv work */
  176. unsigned long delay; /* current delay interval */
  177. };
  178. extern const char *ceph_pr_addr(const struct sockaddr_storage *ss);
  179. extern int ceph_parse_ips(const char *c, const char *end,
  180. struct ceph_entity_addr *addr,
  181. int max_count, int *count);
  182. extern int ceph_msgr_init(void);
  183. extern void ceph_msgr_exit(void);
  184. extern void ceph_msgr_flush(void);
  185. extern struct ceph_messenger *ceph_messenger_create(
  186. struct ceph_entity_addr *myaddr,
  187. u32 features, u32 required);
  188. extern void ceph_messenger_destroy(struct ceph_messenger *);
  189. extern void ceph_con_init(struct ceph_messenger *msgr,
  190. struct ceph_connection *con);
  191. extern void ceph_con_open(struct ceph_connection *con,
  192. struct ceph_entity_addr *addr);
  193. extern bool ceph_con_opened(struct ceph_connection *con);
  194. extern void ceph_con_close(struct ceph_connection *con);
  195. extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
  196. extern void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg);
  197. extern void ceph_con_revoke_message(struct ceph_connection *con,
  198. struct ceph_msg *msg);
  199. extern void ceph_con_keepalive(struct ceph_connection *con);
  200. extern struct ceph_connection *ceph_con_get(struct ceph_connection *con);
  201. extern void ceph_con_put(struct ceph_connection *con);
  202. extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags);
  203. extern void ceph_msg_kfree(struct ceph_msg *m);
  204. static inline struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
  205. {
  206. kref_get(&msg->kref);
  207. return msg;
  208. }
  209. extern void ceph_msg_last_put(struct kref *kref);
  210. static inline void ceph_msg_put(struct ceph_msg *msg)
  211. {
  212. kref_put(&msg->kref, ceph_msg_last_put);
  213. }
  214. extern void ceph_msg_dump(struct ceph_msg *msg);
  215. #endif