security communication with openssl and simplify netio

This commit is contained in:
Rongsong Shen 2025-11-18 11:21:24 +08:00
parent 2a7b7c39bb
commit 8f796b3109
20 changed files with 1176 additions and 922 deletions

View file

@ -16,6 +16,8 @@ struct logging {
typedef struct {
cluster_config_t* cluster_config;
char* conffile;
char* certfile;
char* keyfile;
} myconf_t;
extern cluster_config_t* get_default_conf(void*);

View file

@ -2,6 +2,8 @@
#define _NETIO_H_ 1
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/epoll.h>
@ -9,63 +11,127 @@
#include "dataq.h"
#include "mylock.h"
#include "xce/types.h"
typedef int (*msghandler_t)(struct epoll_event*);
typedef int (*conn_handler_t)(struct epoll_event*);
typedef int (*msg_handler_t)(void*);
typedef int (*err_handler_t)(void*);
#define IO_MON_INPUT (0x01)
#define IO_MON_OUTPUT (0x02)
typedef struct {
uint16_t sz;
char* data;
data_item_t link;
uint16_t sz;
char* data;
} io_packet_t;
#define ITPTR_TO_PACKET(it) \
((io_packet_t*)(((char*)it) - offsetof(io_packet_t, link)))
typedef struct {
mylock_t lock;
int epoll_fd;
struct epoll_event ev;
pthread_t worker;
SSL_CTX* ssl_ctx;
} iomgr_t;
typedef enum {
FSM_IO_READ = 0,
FSM_IO_WRITE
} io_fsm_op_t;
typedef enum {
FSM_STATE_INIT = 0,
FSM_STATE_READ_PKT_HEAD,
FSM_STATE_READ_PKT,
FSM_STATE_WRITE_PKT_HEAD,
FSM_STATE_WRITE_PKT,
FSM_STATE_DONE,
FSM_STATE_ERROR
} io_fsm_state_t;
typedef struct {
mylock_t lock;
iomgr_t* iomgr;
int fd;
int idx;
int flags;
unsigned int busy : 1;
unsigned int needfree : 1;
uint32_t refcount;
dataq_t outq;
uint64_t seq;
msghandler_t handler;
void* owner;
void* data;
void* ep;
io_fsm_op_t op;
io_fsm_state_t state;
io_packet_t* pkt;
size_t iocount;
} io_fsm_t;
typedef struct {
mylock_t lock;
iomgr_t* iomgr;
SSL* ssl;
int fd;
int idx;
int flags;
unsigned int busy : 1;
unsigned int ssl_handshake_done : 1;
dataq_t outq;
uint64_t seq;
conn_handler_t conn_handler;
void* owner;
void* data;
io_fsm_t fsm_in;
io_fsm_t fsm_out;
xce_peerinfo_t* peer;
msg_handler_t msg_handler;
err_handler_t err_handler;
} endpoint_t;
extern int netio_write(int, char*, int);
extern int netio_read(int, char*, int);
extern int write_packet(int, io_packet_t*);
#define INVOKE_MSG_HANDLER(ep) \
do { \
TRACE("%s msg handler %p", __func__, (ep)->msg_handler); \
if ((ep)->msg_handler != NULL) { \
(ep)->msg_handler(ep); \
/* passing ownership of packet to upper layer */ \
(ep)->fsm_in.pkt = NULL; \
} \
} while (0)
#define INVOKE_ERR_HANDLER(ep) \
do { \
if ((ep)->err_handler != NULL) { \
(ep)->err_handler(ep); \
} \
} while (0)
typedef struct {
endpoint_t* ep;
io_packet_t* pkt;
} io_task_data_t;
extern int alloc_packet(io_packet_t*);
extern int read_packet(int, io_packet_t*);
extern void free_packet(io_packet_t*);
extern endpoint_t* endpoint_get(void);
extern int endpoint_init(endpoint_t*, int, int, int, int);
extern int endpoint_connect(endpoint_t*, char*, int);
extern int endpoint_accept(endpoint_t*, endpoint_t*);
extern int endpoint_set_handler(endpoint_t*, msghandler_t, void*);
extern int endpoint_set_conn_handler(endpoint_t*, conn_handler_t);
extern int endpoint_enq(endpoint_t*, data_item_t*);
extern data_item_t* endpoint_deq(endpoint_t*);
extern endpoint_t* endpoint_ref(endpoint_t*);
extern void endpoint_deref(endpoint_t*);
extern uint64_t endpoint_getseq(endpoint_t*);
extern void endpoint_block_tasks(endpoint_t*);
extern void endpoint_unblock_tasks(endpoint_t*);
extern int endpoint_is_task_blocked(endpoint_t*);
extern int endpoint_set_owner(endpoint_t*, void*);
extern int endpoint_set_msg_handler(endpoint_t*, msg_handler_t);
extern int endpoint_set_err_handler(endpoint_t*, err_handler_t);
extern void endpoint_fini(endpoint_t*);
extern int endpoint_set_peer(endpoint_t*, xce_peerinfo_t*);
extern int io_fsm_start(io_fsm_t*, io_packet_t*);
extern void initialize_openssl(void);
extern void cleanup_openssl(void);
extern SSL_CTX* create_ssl_context(char*, char*);
extern void iomgr_set_ssl_ctx(iomgr_t*, SSL_CTX*);
extern int iomgr_init(iomgr_t*);
extern int iomgr_monitor(iomgr_t*, endpoint_t*);
extern int iomgr_monitor(endpoint_t*);
extern int iomgr_unmonitor(iomgr_t*, endpoint_t*);
extern int iomgr_start(iomgr_t*);
extern int iomgr_fini(iomgr_t*);

View file

@ -15,61 +15,45 @@ typedef struct {
int wait;
} reqbar_t;
typedef struct {
data_item_t link;
endpoint_t* ep;
xce_peerinfo_t* from;
io_packet_t pkt;
struct capn c;
struct Request rq;
uint32_t nrefs;
} ioctx_t;
#define ITPTR_TO_IOCTX(p) ((ioctx_t*)(((char*)p) - offsetof(ioctx_t, link)))
typedef int (*xce_check_fn_t)(void*);
extern ioctx_t* ioctx_alloc(endpoint_t*, xce_peerinfo_t*);
extern int ioctx_ref(ioctx_t*);
extern void ioctx_deref(ioctx_t*, int);
extern int ioctx_flush(ioctx_t*, int);
extern int ioctx_encode(ioctx_t*, xce_request_t*);
extern int ioctx_decode(ioctx_t*, xce_request_t*);
extern int xce_encode(xce_request_t*, io_packet_t*);
extern int xce_decode(io_packet_t*, xce_request_t*);
extern int xce_reply(endpoint_t*, xce_peerinfo_t*, xce_reply_t*);
extern int xce_hello_req(ioctx_t*, char*);
extern int xce_nop_req(ioctx_t*);
extern int xce_shutdown_req(ioctx_t*);
extern int xce_hello_req(endpoint_t*, char*);
extern int xce_nop_req(endpoint_t*);
extern int xce_shutdown_req(endpoint_t*);
extern void init_reply_by_req(xce_reply_t*, xce_request_t*);
extern int xce_join_req(ioctx_t*, int, hostattrs_t*);
extern int xce_join_req(endpoint_t*, int, hostattrs_t*);
extern int xce_pushconfig_req(endpoint_t*, xce_peerinfo_t*, xce_configdata_t*);
extern int xce_push_data_req(endpoint_t*, xce_peerinfo_t*, xce_data_t*,
xce_check_fn_t, void*, int);
extern int xce_workerlist_req(ioctx_t*, xce_peerinfo_t*);
extern int xce_workerlist_req(endpoint_t*, xce_peerinfo_t*);
extern int xce_reply_workerlist(endpoint_t*, uint64_t, int, xce_peerinfo_t*,
xce_workerlist_t*);
extern int xce_jobdispatch_req(ioctx_t*, xce_job_dispatch_t*);
extern int xce_jobsubmit_req(ioctx_t*, xce_job_submit_t*);
extern int xce_jobdispatch_req(endpoint_t*, xce_job_dispatch_t*);
extern int xce_jobsubmit_req(endpoint_t*, xce_job_submit_t*);
extern int xce_reply_jobdispatch(endpoint_t*, uint64_t, int32_t,
xce_peerinfo_t*);
extern int xce_reply_jobid(endpoint_t*, int, uint64_t, uint64_t,
xce_peerinfo_t*);
extern int xce_joblist_req(ioctx_t*, xce_peerinfo_t*);
extern int xce_joblist_req(endpoint_t*, xce_peerinfo_t*);
extern int xce_reply_joblist(endpoint_t*, uint64_t, int, xce_peerinfo_t*,
xce_job_list_t*);
extern int xce_startjob_req(ioctx_t*, xce_job_exec_t*);
extern int xce_clusterconf_req(ioctx_t*);
extern int xce_startjob_req(endpoint_t*, xce_job_exec_t*);
extern int xce_clusterconf_req(endpoint_t*);
extern int xce_reply_clusterconf(endpoint_t*, uint64_t, int, xce_peerinfo_t*,
xce_clusterconf_t*);
extern int xce_get_req(ioctx_t*, xce_get_t*);
extern int xce_get_req(endpoint_t*, xce_get_t*);
extern int xce_reply_get(endpoint_t*, xce_peerinfo_t*, xce_reply_t*,
xce_check_fn_t, void*, int);
extern int xce_notify_req(ioctx_t*, xce_notify_data_t*);
extern int xce_notify_req(endpoint_t*, xce_notify_data_t*);
extern int extract_data(dataset_t*, xce_data_t*);
extern int xce_update_req(ioctx_t*, xce_update_data_t*);
extern int xce_jobctrl_req(ioctx_t*, xce_jobctrl_t*);
extern int xce_queuectrl_req(ioctx_t*, xce_queuectrl_t*);
extern int xce_runtask_req(ioctx_t*, xce_taskrun_t*);
extern int xce_update_req(endpoint_t*, xce_update_data_t*);
extern int xce_jobctrl_req(endpoint_t*, xce_jobctrl_t*);
extern int xce_queuectrl_req(endpoint_t*, xce_queuectrl_t*);
extern int xce_runtask_req(endpoint_t*, xce_taskrun_t*);
#endif

View file

@ -12,15 +12,26 @@
typedef int (*vtask_func_t)(void*);
#define VTASK_IO 0x01
#define VTASK_GENERIC 0x02
typedef struct {
void* owner;
ioctx_t ctx;
int vcpu;
vtask_func_t entry;
int vtype;
union {
void* generic;
io_task_data_t iodata;
} u;
} vtask_data_t;
typedef struct {
data_item_t link;
void* owner;
vtask_data_t* data;
int vcpu;
vtask_func_t entry;
} vtask_t;
#define ITPTR_TO_VTASK(p) \
((vtask_t*)(((char*)p) - offsetof(ioctx_t, link) - offsetof(vtask_t, ctx)))
#define ITPTR_TO_VTASK(p) ((vtask_t*)(((char*)p) - offsetof(vtask_t, link)))
typedef enum {
VCPU_IDLE = 0,
@ -47,10 +58,11 @@ typedef struct {
} vtaskmgr_t;
extern vtask_t* vtask_alloc();
extern int vtask_init(vtask_t*, vtask_func_t, endpoint_t*);
extern int vtask_init(vtask_t*, vtask_func_t, void*);
extern void vtask_fini(vtask_t*);
extern int vtask_enq(vtaskmgr_t*, vtask_t*);
extern int vtask_handle_msg(vtaskmgr_t*, vtask_func_t, void* arg);
extern int vtaskmgr_init(vtaskmgr_t*, int);
extern int vtaskmgr_start(vtaskmgr_t*);