messenger.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #ifndef __FS_CEPH_MESSENGER_H
  2. #define __FS_CEPH_MESSENGER_H
  3. #include <linux/blk_types.h>
  4. #include <linux/kref.h>
  5. #include <linux/mutex.h>
  6. #include <linux/net.h>
  7. #include <linux/radix-tree.h>
  8. #include <linux/uio.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/ceph/types.h>
  11. #include <linux/ceph/buffer.h>
  12. struct ceph_msg;
  13. struct ceph_connection;
  14. /*
  15. * Ceph defines these callbacks for handling connection events.
  16. */
  17. struct ceph_connection_operations {
  18. struct ceph_connection *(*get)(struct ceph_connection *);
  19. void (*put)(struct ceph_connection *);
  20. /* handle an incoming message. */
  21. void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
  22. /* authorize an outgoing connection */
  23. struct ceph_auth_handshake *(*get_authorizer) (
  24. struct ceph_connection *con,
  25. int *proto, int force_new);
  26. int (*verify_authorizer_reply) (struct ceph_connection *con, int len);
  27. int (*invalidate_authorizer)(struct ceph_connection *con);
  28. /* there was some error on the socket (disconnect, whatever) */
  29. void (*fault) (struct ceph_connection *con);
  30. /* a remote host as terminated a message exchange session, and messages
  31. * we sent (or they tried to send us) may be lost. */
  32. void (*peer_reset) (struct ceph_connection *con);
  33. struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
  34. struct ceph_msg_header *hdr,
  35. int *skip);
  36. };
  37. /* use format string %s%d */
  38. #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
  39. struct ceph_messenger {
  40. struct ceph_entity_inst inst; /* my name+address */
  41. struct ceph_entity_addr my_enc_addr;
  42. atomic_t stopping;
  43. bool nocrc;
  44. /*
  45. * the global_seq counts connections i (attempt to) initiate
  46. * in order to disambiguate certain connect race conditions.
  47. */
  48. u32 global_seq;
  49. spinlock_t global_seq_lock;
  50. u64 supported_features;
  51. u64 required_features;
  52. };
  53. enum ceph_msg_data_type {
  54. CEPH_MSG_DATA_NONE, /* message contains no data payload */
  55. CEPH_MSG_DATA_PAGES, /* data source/destination is a page array */
  56. CEPH_MSG_DATA_PAGELIST, /* data source/destination is a pagelist */
  57. #ifdef CONFIG_BLOCK
  58. CEPH_MSG_DATA_BIO, /* data source/destination is a bio list */
  59. #endif /* CONFIG_BLOCK */
  60. };
  61. static __inline__ bool ceph_msg_data_type_valid(enum ceph_msg_data_type type)
  62. {
  63. switch (type) {
  64. case CEPH_MSG_DATA_NONE:
  65. case CEPH_MSG_DATA_PAGES:
  66. case CEPH_MSG_DATA_PAGELIST:
  67. #ifdef CONFIG_BLOCK
  68. case CEPH_MSG_DATA_BIO:
  69. #endif /* CONFIG_BLOCK */
  70. return true;
  71. default:
  72. return false;
  73. }
  74. }
  75. struct ceph_msg_data {
  76. struct list_head links; /* ceph_msg->data */
  77. enum ceph_msg_data_type type;
  78. union {
  79. #ifdef CONFIG_BLOCK
  80. struct {
  81. struct bio *bio;
  82. size_t bio_length;
  83. };
  84. #endif /* CONFIG_BLOCK */
  85. struct {
  86. struct page **pages; /* NOT OWNER. */
  87. size_t length; /* total # bytes */
  88. unsigned int alignment; /* first page */
  89. };
  90. struct ceph_pagelist *pagelist;
  91. };
  92. };
  93. struct ceph_msg_data_cursor {
  94. size_t total_resid; /* across all data items */
  95. struct list_head *data_head; /* = &ceph_msg->data */
  96. struct ceph_msg_data *data; /* current data item */
  97. size_t resid; /* bytes not yet consumed */
  98. bool last_piece; /* current is last piece */
  99. bool need_crc; /* crc update needed */
  100. union {
  101. #ifdef CONFIG_BLOCK
  102. struct { /* bio */
  103. struct bio *bio; /* bio from list */
  104. struct bvec_iter bvec_iter;
  105. };
  106. #endif /* CONFIG_BLOCK */
  107. struct { /* pages */
  108. unsigned int page_offset; /* offset in page */
  109. unsigned short page_index; /* index in array */
  110. unsigned short page_count; /* pages in array */
  111. };
  112. struct { /* pagelist */
  113. struct page *page; /* page from list */
  114. size_t offset; /* bytes from list */
  115. };
  116. };
  117. };
  118. /*
  119. * a single message. it contains a header (src, dest, message type, etc.),
  120. * footer (crc values, mainly), a "front" message body, and possibly a
  121. * data payload (stored in some number of pages).
  122. */
  123. struct ceph_msg {
  124. struct ceph_msg_header hdr; /* header */
  125. struct ceph_msg_footer footer; /* footer */
  126. struct kvec front; /* unaligned blobs of message */
  127. struct ceph_buffer *middle;
  128. size_t data_length;
  129. struct list_head data;
  130. struct ceph_msg_data_cursor cursor;
  131. struct ceph_connection *con;
  132. struct list_head list_head; /* links for connection lists */
  133. struct kref kref;
  134. bool more_to_follow;
  135. bool needs_out_seq;
  136. int front_alloc_len;
  137. unsigned long ack_stamp; /* tx: when we were acked */
  138. struct ceph_msgpool *pool;
  139. };
  140. /* ceph connection fault delay defaults, for exponential backoff */
  141. #define BASE_DELAY_INTERVAL (HZ/2)
  142. #define MAX_DELAY_INTERVAL (5 * 60 * HZ)
  143. /*
  144. * A single connection with another host.
  145. *
  146. * We maintain a queue of outgoing messages, and some session state to
  147. * ensure that we can preserve the lossless, ordered delivery of
  148. * messages in the case of a TCP disconnect.
  149. */
  150. struct ceph_connection {
  151. void *private;
  152. const struct ceph_connection_operations *ops;
  153. struct ceph_messenger *msgr;
  154. atomic_t sock_state;
  155. struct socket *sock;
  156. struct ceph_entity_addr peer_addr; /* peer address */
  157. struct ceph_entity_addr peer_addr_for_me;
  158. unsigned long flags;
  159. unsigned long state;
  160. const char *error_msg; /* error message, if any */
  161. struct ceph_entity_name peer_name; /* peer name */
  162. u64 peer_features;
  163. u32 connect_seq; /* identify the most recent connection
  164. attempt for this connection, client */
  165. u32 peer_global_seq; /* peer's global seq for this connection */
  166. int auth_retry; /* true if we need a newer authorizer */
  167. void *auth_reply_buf; /* where to put the authorizer reply */
  168. int auth_reply_buf_len;
  169. struct mutex mutex;
  170. /* out queue */
  171. struct list_head out_queue;
  172. struct list_head out_sent; /* sending or sent but unacked */
  173. u64 out_seq; /* last message queued for send */
  174. u64 in_seq, in_seq_acked; /* last message received, acked */
  175. /* connection negotiation temps */
  176. char in_banner[CEPH_BANNER_MAX_LEN];
  177. struct ceph_msg_connect out_connect;
  178. struct ceph_msg_connect_reply in_reply;
  179. struct ceph_entity_addr actual_peer_addr;
  180. /* message out temps */
  181. struct ceph_msg *out_msg; /* sending message (== tail of
  182. out_sent) */
  183. bool out_msg_done;
  184. struct kvec out_kvec[8], /* sending header/footer data */
  185. *out_kvec_cur;
  186. int out_kvec_left; /* kvec's left in out_kvec */
  187. int out_skip; /* skip this many bytes */
  188. int out_kvec_bytes; /* total bytes left */
  189. bool out_kvec_is_msg; /* kvec refers to out_msg */
  190. int out_more; /* there is more data after the kvecs */
  191. __le64 out_temp_ack; /* for writing an ack */
  192. /* message in temps */
  193. struct ceph_msg_header in_hdr;
  194. struct ceph_msg *in_msg;
  195. u32 in_front_crc, in_middle_crc, in_data_crc; /* calculated crc */
  196. char in_tag; /* protocol control byte */
  197. int in_base_pos; /* bytes read */
  198. __le64 in_temp_ack; /* for reading an ack */
  199. struct delayed_work work; /* send|recv work */
  200. unsigned long delay; /* current delay interval */
  201. };
  202. extern const char *ceph_pr_addr(const struct sockaddr_storage *ss);
  203. extern int ceph_parse_ips(const char *c, const char *end,
  204. struct ceph_entity_addr *addr,
  205. int max_count, int *count);
  206. extern int ceph_msgr_init(void);
  207. extern void ceph_msgr_exit(void);
  208. extern void ceph_msgr_flush(void);
  209. extern void ceph_messenger_init(struct ceph_messenger *msgr,
  210. struct ceph_entity_addr *myaddr,
  211. u64 supported_features,
  212. u64 required_features,
  213. bool nocrc);
  214. extern void ceph_con_init(struct ceph_connection *con, void *private,
  215. const struct ceph_connection_operations *ops,
  216. struct ceph_messenger *msgr);
  217. extern void ceph_con_open(struct ceph_connection *con,
  218. __u8 entity_type, __u64 entity_num,
  219. struct ceph_entity_addr *addr);
  220. extern bool ceph_con_opened(struct ceph_connection *con);
  221. extern void ceph_con_close(struct ceph_connection *con);
  222. extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
  223. extern void ceph_msg_revoke(struct ceph_msg *msg);
  224. extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
  225. extern void ceph_con_keepalive(struct ceph_connection *con);
  226. extern void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
  227. size_t length, size_t alignment);
  228. extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
  229. struct ceph_pagelist *pagelist);
  230. #ifdef CONFIG_BLOCK
  231. extern void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
  232. size_t length);
  233. #endif /* CONFIG_BLOCK */
  234. extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
  235. bool can_fail);
  236. extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
  237. extern void ceph_msg_put(struct ceph_msg *msg);
  238. extern void ceph_msg_dump(struct ceph_msg *msg);
  239. #endif