1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * March 6 2023, Christian Hopps <chopps@labn.net>
5 * Copyright (c) 2023, LabN Consulting, L.L.C.
14 DECLARE_MTYPE(MSG_CONN
);
17 * Messages on the stream start with a marker that encodes a version octet.
19 #define MGMT_MSG_MARKER_PFX (0x23232300u) /* ASCII - "###\ooo"*/
20 #define MGMT_MSG_IS_MARKER(x) (((x)&0xFFFFFF00u) == MGMT_MSG_MARKER_PFX)
21 #define MGMT_MSG_MARKER(version) (MGMT_MSG_MARKER_PFX | (version))
22 #define MGMT_MSG_MARKER_VERSION(x) (0xFF & (x))
24 #define MGMT_MSG_VERSION_PROTOBUF 0
25 #define MGMT_MSG_VERSION_NATIVE 1
28 struct mgmt_msg_state
{
31 struct stream_fifo inq
;
32 struct stream_fifo outq
;
33 uint64_t nrxm
; /* number of received messages */
34 uint64_t nrxb
; /* number of received bytes */
35 uint64_t ntxm
; /* number of sent messages */
36 uint64_t ntxb
; /* number of sent bytes */
37 size_t max_read_buf
; /* should replace with max time value */
38 size_t max_write_buf
; /* should replace with max time value */
40 char *idtag
; /* identifying tag for messages */
48 enum mgmt_msg_rsched
{
49 MSR_SCHED_BOTH
, /* schedule both queue and read */
50 MSR_SCHED_STREAM
, /* schedule read */
51 MSR_DISCONNECT
, /* disconnect and start reconnecting */
54 enum mgmt_msg_wsched
{
55 MSW_SCHED_NONE
, /* no scheduling required */
56 MSW_SCHED_STREAM
, /* schedule writing */
57 MSW_DISCONNECT
, /* disconnect and start reconnecting */
63 extern int mgmt_msg_connect(const char *path
, size_t sendbuf
, size_t recvbuf
,
65 extern bool mgmt_msg_procbufs(struct mgmt_msg_state
*ms
,
66 void (*handle_msg
)(uint8_t version
, uint8_t *msg
,
67 size_t msglen
, void *user
),
68 void *user
, bool debug
);
69 extern enum mgmt_msg_rsched
mgmt_msg_read(struct mgmt_msg_state
*ms
, int fd
,
71 extern size_t mgmt_msg_reset_writes(struct mgmt_msg_state
*ms
);
72 extern int mgmt_msg_send_msg(struct mgmt_msg_state
*ms
, uint8_t version
,
73 void *msg
, size_t len
,
74 size_t (*packf
)(void *msg
, void *buf
), bool debug
);
75 extern enum mgmt_msg_wsched
mgmt_msg_write(struct mgmt_msg_state
*ms
, int fd
,
78 extern void mgmt_msg_destroy(struct mgmt_msg_state
*state
);
80 extern void mgmt_msg_init(struct mgmt_msg_state
*ms
, size_t max_read_buf
,
81 size_t max_write_buf
, size_t max_msg_sz
,
90 struct mgmt_msg_state mstate
;
91 struct event_loop
*loop
;
92 struct event
*read_ev
;
93 struct event
*write_ev
;
94 struct event
*proc_msg_ev
;
95 struct msg_conn
*remote_conn
;
96 int (*notify_disconnect
)(struct msg_conn
*conn
);
97 void (*handle_msg
)(uint8_t version
, uint8_t *data
, size_t len
,
98 struct msg_conn
*conn
);
100 uint short_circuit_depth
;
102 bool is_short_circuit
;
108 * `notify_disconnect` is not called when `msg_conn_cleanup` is called for a
109 * msg_conn which is currently connected. The socket is closed but there is no
112 extern void msg_conn_cleanup(struct msg_conn
*conn
);
113 extern void msg_conn_disconnect(struct msg_conn
*conn
, bool reconnect
);
114 extern int msg_conn_send_msg(struct msg_conn
*client
, uint8_t version
,
115 void *msg
, size_t mlen
,
116 size_t (*packf
)(void *, void *),
117 bool short_circuit_ok
);
120 * Client-side Connections
124 struct msg_conn conn
;
125 struct event
*conn_retry_tmr
;
127 int (*notify_connect
)(struct msg_client
*client
);
128 bool short_circuit_ok
;
132 * `notify_disconnect` is not called when `msg_client_cleanup` is called for a
133 * msg_client which is currently connected. The socket is closed but there is no
136 extern void msg_client_cleanup(struct msg_client
*client
);
139 * `notify_disconnect` is not called when the user `msg_client_cleanup` is
140 * called for a client which is currently connected. The socket is closed
141 * but there is no notification.
144 msg_client_init(struct msg_client
*client
, struct event_loop
*tm
,
146 int (*notify_connect
)(struct msg_client
*client
),
147 int (*notify_disconnect
)(struct msg_conn
*client
),
148 void (*handle_msg
)(uint8_t version
, uint8_t *data
, size_t len
,
149 struct msg_conn
*client
),
150 size_t max_read_buf
, size_t max_write_buf
, size_t max_msg_sz
,
151 bool short_circuit_ok
, const char *idtag
, bool debug
);
154 * Server-side Connections
156 #define MGMTD_MAX_CONN 32
158 PREDECL_LIST(msg_server_list
);
162 struct msg_server_list_item link
;
163 struct event_loop
*loop
;
164 struct event
*listen_ev
;
167 struct msg_conn
*(*create
)(int fd
, union sockunion
*su
);
171 extern int msg_server_init(struct msg_server
*server
, const char *sopath
,
172 struct event_loop
*loop
,
173 struct msg_conn
*(*create
)(int fd
,
174 union sockunion
*su
),
175 const char *idtag
, struct debug
*debug
);
176 extern void msg_server_cleanup(struct msg_server
*server
);
179 * `notify_disconnect` is not called when the user `msg_conn_cleanup` is
180 * called for a client which is currently connected. The socket is closed
181 * but there is no notification.
184 msg_server_conn_create(struct event_loop
*tm
, int fd
,
185 int (*notify_disconnect
)(struct msg_conn
*conn
),
186 void (*handle_msg
)(uint8_t version
, uint8_t *data
,
187 size_t len
, struct msg_conn
*conn
),
188 size_t max_read
, size_t max_write
, size_t max_size
,
189 void *user
, const char *idtag
);
191 extern void msg_server_conn_delete(struct msg_conn
*conn
);
194 msg_conn_accept_init(struct msg_conn
*conn
, struct event_loop
*tm
, int fd
,
195 int (*notify_disconnect
)(struct msg_conn
*conn
),
196 void (*handle_msg
)(uint8_t version
, uint8_t *data
,
197 size_t len
, struct msg_conn
*conn
),
198 size_t max_read
, size_t max_write
, size_t max_size
,
201 #endif /* _MGMT_MSG_H */