xref: /linux-6.15/include/linux/ceph/messenger.h (revision e27947c7)
13d14c5d2SYehuda Sadeh #ifndef __FS_CEPH_MESSENGER_H
23d14c5d2SYehuda Sadeh #define __FS_CEPH_MESSENGER_H
33d14c5d2SYehuda Sadeh 
43d14c5d2SYehuda Sadeh #include <linux/kref.h>
53d14c5d2SYehuda Sadeh #include <linux/mutex.h>
63d14c5d2SYehuda Sadeh #include <linux/net.h>
73d14c5d2SYehuda Sadeh #include <linux/radix-tree.h>
83d14c5d2SYehuda Sadeh #include <linux/uio.h>
93d14c5d2SYehuda Sadeh #include <linux/workqueue.h>
103d14c5d2SYehuda Sadeh 
113d14c5d2SYehuda Sadeh #include "types.h"
123d14c5d2SYehuda Sadeh #include "buffer.h"
133d14c5d2SYehuda Sadeh 
143d14c5d2SYehuda Sadeh struct ceph_msg;
153d14c5d2SYehuda Sadeh struct ceph_connection;
163d14c5d2SYehuda Sadeh 
173d14c5d2SYehuda Sadeh /*
183d14c5d2SYehuda Sadeh  * Ceph defines these callbacks for handling connection events.
193d14c5d2SYehuda Sadeh  */
203d14c5d2SYehuda Sadeh struct ceph_connection_operations {
213d14c5d2SYehuda Sadeh 	struct ceph_connection *(*get)(struct ceph_connection *);
223d14c5d2SYehuda Sadeh 	void (*put)(struct ceph_connection *);
233d14c5d2SYehuda Sadeh 
243d14c5d2SYehuda Sadeh 	/* handle an incoming message. */
253d14c5d2SYehuda Sadeh 	void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
263d14c5d2SYehuda Sadeh 
273d14c5d2SYehuda Sadeh 	/* authorize an outgoing connection */
28a3530df3SAlex Elder 	struct ceph_auth_handshake *(*get_authorizer) (
29a3530df3SAlex Elder 				struct ceph_connection *con,
308f43fb53SAlex Elder 			       int *proto, int force_new);
313d14c5d2SYehuda Sadeh 	int (*verify_authorizer_reply) (struct ceph_connection *con, int len);
323d14c5d2SYehuda Sadeh 	int (*invalidate_authorizer)(struct ceph_connection *con);
333d14c5d2SYehuda Sadeh 
343d14c5d2SYehuda Sadeh 	/* there was some error on the socket (disconnect, whatever) */
353d14c5d2SYehuda Sadeh 	void (*fault) (struct ceph_connection *con);
363d14c5d2SYehuda Sadeh 
373d14c5d2SYehuda Sadeh 	/* a remote host as terminated a message exchange session, and messages
383d14c5d2SYehuda Sadeh 	 * we sent (or they tried to send us) may be lost. */
393d14c5d2SYehuda Sadeh 	void (*peer_reset) (struct ceph_connection *con);
403d14c5d2SYehuda Sadeh 
413d14c5d2SYehuda Sadeh 	struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
423d14c5d2SYehuda Sadeh 					struct ceph_msg_header *hdr,
433d14c5d2SYehuda Sadeh 					int *skip);
443d14c5d2SYehuda Sadeh };
453d14c5d2SYehuda Sadeh 
463d14c5d2SYehuda Sadeh /* use format string %s%d */
473d14c5d2SYehuda Sadeh #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
483d14c5d2SYehuda Sadeh 
493d14c5d2SYehuda Sadeh struct ceph_messenger {
503d14c5d2SYehuda Sadeh 	struct ceph_entity_inst inst;    /* my name+address */
513d14c5d2SYehuda Sadeh 	struct ceph_entity_addr my_enc_addr;
523d14c5d2SYehuda Sadeh 
533d14c5d2SYehuda Sadeh 	bool nocrc;
543d14c5d2SYehuda Sadeh 
553d14c5d2SYehuda Sadeh 	/*
563d14c5d2SYehuda Sadeh 	 * the global_seq counts connections i (attempt to) initiate
573d14c5d2SYehuda Sadeh 	 * in order to disambiguate certain connect race conditions.
583d14c5d2SYehuda Sadeh 	 */
593d14c5d2SYehuda Sadeh 	u32 global_seq;
603d14c5d2SYehuda Sadeh 	spinlock_t global_seq_lock;
613d14c5d2SYehuda Sadeh 
623d14c5d2SYehuda Sadeh 	u32 supported_features;
633d14c5d2SYehuda Sadeh 	u32 required_features;
643d14c5d2SYehuda Sadeh };
653d14c5d2SYehuda Sadeh 
663d14c5d2SYehuda Sadeh /*
673d14c5d2SYehuda Sadeh  * a single message.  it contains a header (src, dest, message type, etc.),
683d14c5d2SYehuda Sadeh  * footer (crc values, mainly), a "front" message body, and possibly a
693d14c5d2SYehuda Sadeh  * data payload (stored in some number of pages).
703d14c5d2SYehuda Sadeh  */
713d14c5d2SYehuda Sadeh struct ceph_msg {
723d14c5d2SYehuda Sadeh 	struct ceph_msg_header hdr;	/* header */
733d14c5d2SYehuda Sadeh 	struct ceph_msg_footer footer;	/* footer */
743d14c5d2SYehuda Sadeh 	struct kvec front;              /* unaligned blobs of message */
753d14c5d2SYehuda Sadeh 	struct ceph_buffer *middle;
763d14c5d2SYehuda Sadeh 	struct page **pages;            /* data payload.  NOT OWNER. */
773d14c5d2SYehuda Sadeh 	unsigned nr_pages;              /* size of page array */
78c5c6b19dSSage Weil 	unsigned page_alignment;        /* io offset in first page */
793d14c5d2SYehuda Sadeh 	struct ceph_pagelist *pagelist; /* instead of pages */
8038941f80SAlex Elder 
8138941f80SAlex Elder 	struct ceph_connection *con;
823d14c5d2SYehuda Sadeh 	struct list_head list_head;
8338941f80SAlex Elder 
843d14c5d2SYehuda Sadeh 	struct kref kref;
853d14c5d2SYehuda Sadeh 	struct bio  *bio;		/* instead of pages/pagelist */
863d14c5d2SYehuda Sadeh 	struct bio  *bio_iter;		/* bio iterator */
873d14c5d2SYehuda Sadeh 	int bio_seg;			/* current bio segment */
883d14c5d2SYehuda Sadeh 	struct ceph_pagelist *trail;	/* the trailing part of the data */
893d14c5d2SYehuda Sadeh 	bool front_is_vmalloc;
903d14c5d2SYehuda Sadeh 	bool more_to_follow;
913d14c5d2SYehuda Sadeh 	bool needs_out_seq;
923d14c5d2SYehuda Sadeh 	int front_max;
934cf9d544SSage Weil 	unsigned long ack_stamp;        /* tx: when we were acked */
943d14c5d2SYehuda Sadeh 
953d14c5d2SYehuda Sadeh 	struct ceph_msgpool *pool;
963d14c5d2SYehuda Sadeh };
973d14c5d2SYehuda Sadeh 
983d14c5d2SYehuda Sadeh struct ceph_msg_pos {
993d14c5d2SYehuda Sadeh 	int page, page_pos;  /* which page; offset in page */
1003d14c5d2SYehuda Sadeh 	int data_pos;        /* offset in data payload */
101bca064d2SAlex Elder 	bool did_page_crc;   /* true if we've calculated crc for current page */
1023d14c5d2SYehuda Sadeh };
1033d14c5d2SYehuda Sadeh 
1043d14c5d2SYehuda Sadeh /* ceph connection fault delay defaults, for exponential backoff */
1053d14c5d2SYehuda Sadeh #define BASE_DELAY_INTERVAL	(HZ/2)
1063d14c5d2SYehuda Sadeh #define MAX_DELAY_INTERVAL	(5 * 60 * HZ)
1073d14c5d2SYehuda Sadeh 
1083d14c5d2SYehuda Sadeh /*
109928443cdSAlex Elder  * ceph_connection flag bits
1103d14c5d2SYehuda Sadeh  */
111928443cdSAlex Elder 
1123d14c5d2SYehuda Sadeh #define LOSSYTX         0  /* we can close channel or drop messages on errors */
1133d14c5d2SYehuda Sadeh #define KEEPALIVE_PENDING      3
1143d14c5d2SYehuda Sadeh #define WRITE_PENDING	4  /* we have data ready to send */
115928443cdSAlex Elder #define SOCK_CLOSED	11 /* socket state changed to closed */
116928443cdSAlex Elder #define BACKOFF         15
117928443cdSAlex Elder 
118928443cdSAlex Elder /*
119928443cdSAlex Elder  * ceph_connection states
120928443cdSAlex Elder  */
121928443cdSAlex Elder #define CONNECTING	1
122928443cdSAlex Elder #define NEGOTIATING	2
123*e27947c7SAlex Elder #define CONNECTED	5
1243d14c5d2SYehuda Sadeh #define STANDBY		8  /* no outgoing messages, socket closed.  we keep
1253d14c5d2SYehuda Sadeh 			    * the ceph_connection around to maintain shared
1263d14c5d2SYehuda Sadeh 			    * state with the peer. */
1273d14c5d2SYehuda Sadeh #define CLOSED		10 /* we've closed the connection */
1283d14c5d2SYehuda Sadeh #define OPENING         13 /* open connection w/ (possibly new) peer */
1293d14c5d2SYehuda Sadeh 
1303d14c5d2SYehuda Sadeh /*
1313d14c5d2SYehuda Sadeh  * A single connection with another host.
1323d14c5d2SYehuda Sadeh  *
1333d14c5d2SYehuda Sadeh  * We maintain a queue of outgoing messages, and some session state to
1343d14c5d2SYehuda Sadeh  * ensure that we can preserve the lossless, ordered delivery of
1353d14c5d2SYehuda Sadeh  * messages in the case of a TCP disconnect.
1363d14c5d2SYehuda Sadeh  */
1373d14c5d2SYehuda Sadeh struct ceph_connection {
1383d14c5d2SYehuda Sadeh 	void *private;
1393d14c5d2SYehuda Sadeh 
1403d14c5d2SYehuda Sadeh 	const struct ceph_connection_operations *ops;
1413d14c5d2SYehuda Sadeh 
1423d14c5d2SYehuda Sadeh 	struct ceph_messenger *msgr;
143ce2c8903SAlex Elder 
144ce2c8903SAlex Elder 	atomic_t sock_state;
1453d14c5d2SYehuda Sadeh 	struct socket *sock;
146ce2c8903SAlex Elder 	struct ceph_entity_addr peer_addr; /* peer address */
147ce2c8903SAlex Elder 	struct ceph_entity_addr peer_addr_for_me;
148ce2c8903SAlex Elder 
149928443cdSAlex Elder 	unsigned long flags;
150928443cdSAlex Elder 	unsigned long state;
1513d14c5d2SYehuda Sadeh 	const char *error_msg;  /* error message, if any */
1523d14c5d2SYehuda Sadeh 
1533d14c5d2SYehuda Sadeh 	struct ceph_entity_name peer_name; /* peer name */
154ce2c8903SAlex Elder 
1553d14c5d2SYehuda Sadeh 	unsigned peer_features;
1563d14c5d2SYehuda Sadeh 	u32 connect_seq;      /* identify the most recent connection
1573d14c5d2SYehuda Sadeh 				 attempt for this connection, client */
1583d14c5d2SYehuda Sadeh 	u32 peer_global_seq;  /* peer's global seq for this connection */
1593d14c5d2SYehuda Sadeh 
1603d14c5d2SYehuda Sadeh 	int auth_retry;       /* true if we need a newer authorizer */
1613d14c5d2SYehuda Sadeh 	void *auth_reply_buf;   /* where to put the authorizer reply */
1623d14c5d2SYehuda Sadeh 	int auth_reply_buf_len;
1633d14c5d2SYehuda Sadeh 
1643d14c5d2SYehuda Sadeh 	struct mutex mutex;
1653d14c5d2SYehuda Sadeh 
1663d14c5d2SYehuda Sadeh 	/* out queue */
1673d14c5d2SYehuda Sadeh 	struct list_head out_queue;
1683d14c5d2SYehuda Sadeh 	struct list_head out_sent;   /* sending or sent but unacked */
1693d14c5d2SYehuda Sadeh 	u64 out_seq;		     /* last message queued for send */
1703d14c5d2SYehuda Sadeh 
1713d14c5d2SYehuda Sadeh 	u64 in_seq, in_seq_acked;  /* last message received, acked */
1723d14c5d2SYehuda Sadeh 
1733d14c5d2SYehuda Sadeh 	/* connection negotiation temps */
1743d14c5d2SYehuda Sadeh 	char in_banner[CEPH_BANNER_MAX_LEN];
1753d14c5d2SYehuda Sadeh 	union {
1763d14c5d2SYehuda Sadeh 		struct {  /* outgoing connection */
1773d14c5d2SYehuda Sadeh 			struct ceph_msg_connect out_connect;
1783d14c5d2SYehuda Sadeh 			struct ceph_msg_connect_reply in_reply;
1793d14c5d2SYehuda Sadeh 		};
1803d14c5d2SYehuda Sadeh 		struct {  /* incoming */
1813d14c5d2SYehuda Sadeh 			struct ceph_msg_connect in_connect;
1823d14c5d2SYehuda Sadeh 			struct ceph_msg_connect_reply out_reply;
1833d14c5d2SYehuda Sadeh 		};
1843d14c5d2SYehuda Sadeh 	};
1853d14c5d2SYehuda Sadeh 	struct ceph_entity_addr actual_peer_addr;
1863d14c5d2SYehuda Sadeh 
1873d14c5d2SYehuda Sadeh 	/* message out temps */
1883d14c5d2SYehuda Sadeh 	struct ceph_msg *out_msg;        /* sending message (== tail of
1893d14c5d2SYehuda Sadeh 					    out_sent) */
1903d14c5d2SYehuda Sadeh 	bool out_msg_done;
1913d14c5d2SYehuda Sadeh 	struct ceph_msg_pos out_msg_pos;
1923d14c5d2SYehuda Sadeh 
1933d14c5d2SYehuda Sadeh 	struct kvec out_kvec[8],         /* sending header/footer data */
1943d14c5d2SYehuda Sadeh 		*out_kvec_cur;
1953d14c5d2SYehuda Sadeh 	int out_kvec_left;   /* kvec's left in out_kvec */
1963d14c5d2SYehuda Sadeh 	int out_skip;        /* skip this many bytes */
1973d14c5d2SYehuda Sadeh 	int out_kvec_bytes;  /* total bytes left */
1983d14c5d2SYehuda Sadeh 	bool out_kvec_is_msg; /* kvec refers to out_msg */
1993d14c5d2SYehuda Sadeh 	int out_more;        /* there is more data after the kvecs */
2003d14c5d2SYehuda Sadeh 	__le64 out_temp_ack; /* for writing an ack */
2013d14c5d2SYehuda Sadeh 
2023d14c5d2SYehuda Sadeh 	/* message in temps */
2033d14c5d2SYehuda Sadeh 	struct ceph_msg_header in_hdr;
2043d14c5d2SYehuda Sadeh 	struct ceph_msg *in_msg;
2053d14c5d2SYehuda Sadeh 	struct ceph_msg_pos in_msg_pos;
2063d14c5d2SYehuda Sadeh 	u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
2073d14c5d2SYehuda Sadeh 
2083d14c5d2SYehuda Sadeh 	char in_tag;         /* protocol control byte */
2093d14c5d2SYehuda Sadeh 	int in_base_pos;     /* bytes read */
2103d14c5d2SYehuda Sadeh 	__le64 in_temp_ack;  /* for reading an ack */
2113d14c5d2SYehuda Sadeh 
2123d14c5d2SYehuda Sadeh 	struct delayed_work work;	    /* send|recv work */
2133d14c5d2SYehuda Sadeh 	unsigned long       delay;          /* current delay interval */
2143d14c5d2SYehuda Sadeh };
2153d14c5d2SYehuda Sadeh 
2163d14c5d2SYehuda Sadeh 
2173d14c5d2SYehuda Sadeh extern const char *ceph_pr_addr(const struct sockaddr_storage *ss);
2183d14c5d2SYehuda Sadeh extern int ceph_parse_ips(const char *c, const char *end,
2193d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr,
2203d14c5d2SYehuda Sadeh 			  int max_count, int *count);
2213d14c5d2SYehuda Sadeh 
2223d14c5d2SYehuda Sadeh 
2233d14c5d2SYehuda Sadeh extern int ceph_msgr_init(void);
2243d14c5d2SYehuda Sadeh extern void ceph_msgr_exit(void);
2253d14c5d2SYehuda Sadeh extern void ceph_msgr_flush(void);
2263d14c5d2SYehuda Sadeh 
22715d9882cSAlex Elder extern void ceph_messenger_init(struct ceph_messenger *msgr,
2283d14c5d2SYehuda Sadeh 			struct ceph_entity_addr *myaddr,
22915d9882cSAlex Elder 			u32 supported_features,
23015d9882cSAlex Elder 			u32 required_features,
23115d9882cSAlex Elder 			bool nocrc);
2323d14c5d2SYehuda Sadeh 
2331bfd89f4SAlex Elder extern void ceph_con_init(struct ceph_connection *con, void *private,
2341bfd89f4SAlex Elder 			const struct ceph_connection_operations *ops,
2351bfd89f4SAlex Elder 			struct ceph_messenger *msgr, __u8 entity_type,
2361bfd89f4SAlex Elder 			__u64 entity_num);
2373d14c5d2SYehuda Sadeh extern void ceph_con_open(struct ceph_connection *con,
2383d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr);
2393d14c5d2SYehuda Sadeh extern bool ceph_con_opened(struct ceph_connection *con);
2403d14c5d2SYehuda Sadeh extern void ceph_con_close(struct ceph_connection *con);
2413d14c5d2SYehuda Sadeh extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
2426740a845SAlex Elder 
2436740a845SAlex Elder extern void ceph_msg_revoke(struct ceph_msg *msg);
2448921d114SAlex Elder extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
2458921d114SAlex Elder 
2463d14c5d2SYehuda Sadeh extern void ceph_con_keepalive(struct ceph_connection *con);
2473d14c5d2SYehuda Sadeh extern struct ceph_connection *ceph_con_get(struct ceph_connection *con);
2483d14c5d2SYehuda Sadeh extern void ceph_con_put(struct ceph_connection *con);
2493d14c5d2SYehuda Sadeh 
250b61c2763SSage Weil extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
251b61c2763SSage Weil 				     bool can_fail);
2523d14c5d2SYehuda Sadeh extern void ceph_msg_kfree(struct ceph_msg *m);
2533d14c5d2SYehuda Sadeh 
2543d14c5d2SYehuda Sadeh 
2553d14c5d2SYehuda Sadeh static inline struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
2563d14c5d2SYehuda Sadeh {
2573d14c5d2SYehuda Sadeh 	kref_get(&msg->kref);
2583d14c5d2SYehuda Sadeh 	return msg;
2593d14c5d2SYehuda Sadeh }
2603d14c5d2SYehuda Sadeh extern void ceph_msg_last_put(struct kref *kref);
2613d14c5d2SYehuda Sadeh static inline void ceph_msg_put(struct ceph_msg *msg)
2623d14c5d2SYehuda Sadeh {
2633d14c5d2SYehuda Sadeh 	kref_put(&msg->kref, ceph_msg_last_put);
2643d14c5d2SYehuda Sadeh }
2653d14c5d2SYehuda Sadeh 
2663d14c5d2SYehuda Sadeh extern void ceph_msg_dump(struct ceph_msg *msg);
2673d14c5d2SYehuda Sadeh 
2683d14c5d2SYehuda Sadeh #endif
269