messenger.h 8.9 KB

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