Add type checking to 16/32/64 endianness converters
[fio.git] / server.h
... / ...
CommitLineData
1#ifndef FIO_SERVER_H
2#define FIO_SERVER_H
3
4#include <inttypes.h>
5#include <string.h>
6#include <endian.h>
7
8#include "stat.h"
9
10/*
11 * On-wire encoding is little endian
12 */
13struct fio_net_cmd {
14 uint16_t version; /* protocol version */
15 uint16_t opcode; /* command opcode */
16 uint32_t flags; /* modifier flags */
17 uint64_t serial; /* serial number */
18 uint32_t pdu_len; /* length of post-cmd layload */
19 /*
20 * These must be immediately before the payload, anything before
21 * these fields are checksummed.
22 */
23 uint16_t cmd_crc16; /* cmd checksum */
24 uint16_t pdu_crc16; /* payload checksum */
25 uint8_t payload[0]; /* payload */
26};
27
28enum {
29 FIO_SERVER_VER = 1,
30 FIO_SERVER_VER1 = 1,
31
32 FIO_SERVER_MAX_PDU = 64,
33
34 FIO_NET_CMD_QUIT = 1,
35 FIO_NET_CMD_EXIT = 2,
36 FIO_NET_CMD_JOB = 3,
37 FIO_NET_CMD_ACK = 4,
38 FIO_NET_CMD_NAK = 5,
39 FIO_NET_CMD_TEXT = 6,
40 FIO_NET_CMD_TS = 7,
41 FIO_NET_CMD_GS = 8,
42
43 FIO_NET_CMD_F_MORE = 1UL << 0,
44
45 /* crc does not include the crc fields */
46 FIO_NET_CMD_CRC_SZ = sizeof(struct fio_net_cmd) -
47 2 * sizeof(uint16_t),
48};
49
50struct cmd_ts_pdu {
51 struct thread_stat ts;
52 struct group_run_stats rs;
53};
54
55extern int fio_start_server(int);
56extern int fio_server_text_output(const char *, unsigned int len);
57extern int fio_server_log(const char *format, ...);
58extern int fio_net_send_cmd(int, uint16_t, const void *, off_t);
59
60struct thread_stat;
61struct group_run_stats;
62extern void fio_server_send_ts(struct thread_stat *, struct group_run_stats *);
63extern void fio_server_send_gs(struct group_run_stats *);
64
65extern int fio_clients_connect(void);
66extern int fio_clients_send_ini(const char *);
67extern int fio_handle_clients(void);
68extern void fio_client_add(const char *);
69
70extern int fio_recv_data(int sk, void *p, unsigned int len);
71extern int fio_send_data(int sk, const void *p, unsigned int len);
72extern void fio_net_cmd_crc(struct fio_net_cmd *);
73extern struct fio_net_cmd *fio_net_recv_cmd(int sk);
74
75extern int exit_backend;
76extern int fio_net_port;
77
78#if __BYTE_ORDER == __LITTLE_ENDIAN
79#define __le16_to_cpu(x) (x)
80#define __le32_to_cpu(x) (x)
81#define __le64_to_cpu(x) (x)
82#define __cpu_to_le16(x) (x)
83#define __cpu_to_le32(x) (x)
84#define __cpu_to_le64(x) (x)
85#elif __BYTE_ORDER == __BIG_ENDIAN
86#define __le16_to_cpu(x) __bswap_16(x)
87#define __le32_to_cpu(x) __bswap_32(x)
88#define __le64_to_cpu(x) __bswap_64(x)
89#define __cpu_to_le16(x) __bswap_16(x)
90#define __cpu_to_le32(x) __bswap_32(x)
91#define __cpu_to_le64(x) __bswap_64(x)
92#else
93#error "Endianness not detected"
94#endif
95
96#define le16_to_cpu(val) ({ \
97 uint16_t *__val = &(val); \
98 __le16_to_cpu(*__val); \
99})
100#define le32_to_cpu(val) ({ \
101 uint32_t *__val = &(val); \
102 __le32_to_cpu(*__val); \
103})
104#define le64_to_cpu(val) ({ \
105 uint64_t *__val = &(val); \
106 __le64_to_cpu(*__val); \
107})
108#define cpu_to_le16(val) ({ \
109 uint16_t *__val = &(val); \
110 __cpu_to_le16(*__val); \
111})
112#define cpu_to_le32(val) ({ \
113 uint32_t *__val = &(val); \
114 __cpu_to_le32(*__val); \
115})
116#define cpu_to_le64(val) ({ \
117 uint64_t *__val = &(val); \
118 __cpu_to_le64(*__val); \
119})
120
121static inline void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
122 const void *pdu, uint32_t pdu_len)
123{
124 memset(cmd, 0, sizeof(*cmd));
125
126 cmd->version = __cpu_to_le16(FIO_SERVER_VER1);
127 cmd->opcode = cpu_to_le16(opcode);
128
129 if (pdu) {
130 cmd->pdu_len = cpu_to_le32(pdu_len);
131 memcpy(&cmd->payload, pdu, pdu_len);
132 }
133}
134
135#endif