12 #include <netinet/in.h>
13 #include <arpa/inet.h>
18 #include "crc/crc32.h"
22 struct flist_head list;
23 struct sockaddr_in addr;
28 static FLIST_HEAD(client_list);
30 static struct fio_client *find_client_by_fd(int fd)
32 struct fio_client *client;
33 struct flist_head *entry;
35 flist_for_each(entry, &client_list) {
36 client = flist_entry(entry, struct fio_client, list);
46 static struct fio_client *find_client_by_name(const char *name)
48 struct fio_client *client;
49 struct flist_head *entry;
51 flist_for_each(entry, &client_list) {
52 client = flist_entry(entry, struct fio_client, list);
54 if (!strcmp(name, client->hostname))
62 static void remove_client(struct fio_client *client)
64 flist_del(&client->list);
66 free(client->hostname);
70 void fio_client_add(const char *hostname)
72 struct fio_client *client;
74 client = malloc(sizeof(*client));
75 memset(client, 0, sizeof(*client));
76 client->hostname = strdup(hostname);
78 flist_add(&client->list, &client_list);
82 static int fio_client_connect(struct fio_client *client)
86 memset(&client->addr, 0, sizeof(client->addr));
87 client->addr.sin_family = AF_INET;
88 client->addr.sin_port = htons(fio_net_port);
90 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
93 hent = gethostbyname(client->hostname);
95 log_err("fio: gethostbyname: %s\n", strerror(errno));
99 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
102 fd = socket(AF_INET, SOCK_STREAM, 0);
104 log_err("fio: socket: %s\n", strerror(errno));
109 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
110 log_err("fio: connect: %s\n", strerror(errno));
119 int fio_clients_connect(void)
121 struct fio_client *client;
122 struct flist_head *entry, *tmp;
125 flist_for_each_safe(entry, tmp, &client_list) {
126 client = flist_entry(entry, struct fio_client, list);
128 ret = fio_client_connect(client);
130 remove_client(client);
136 static int send_file_buf(struct fio_client *client, char *buf, off_t size)
138 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
142 * Send file contents to server backend. We could use sendfile(), but to remain
143 * more portable lets just read/write the darn thing.
145 static int fio_client_send_ini(struct fio_client *client, const char *filename)
152 fd = open(filename, O_RDONLY);
154 log_err("fio: job file open: %s\n", strerror(errno));
158 if (fstat(fd, &sb) < 0) {
159 log_err("fio: job file stat: %s\n", strerror(errno));
163 buf = malloc(sb.st_size);
168 ret = read(fd, p, len);
177 else if (errno == EAGAIN || errno == EINTR)
181 ret = send_file_buf(client, buf, sb.st_size);
186 int fio_clients_send_ini(const char *filename)
188 struct fio_client *client;
189 struct flist_head *entry, *tmp;
191 flist_for_each_safe(entry, tmp, &client_list) {
192 client = flist_entry(entry, struct fio_client, list);
194 if (fio_client_send_ini(client, filename))
195 remove_client(client);
201 static int handle_client(struct fio_client *client)
203 struct fio_net_cmd *cmd;
205 while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
206 if (cmd->opcode == FIO_NET_CMD_ACK) {
210 if (cmd->opcode == FIO_NET_CMD_QUIT) {
211 remove_client(client);
215 if (cmd->opcode != FIO_NET_CMD_TEXT) {
216 printf("non text: %d\n", cmd->opcode);
220 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
228 int fio_handle_clients(void)
230 struct fio_client *client;
231 struct flist_head *entry;
235 pfds = malloc(nr_clients * sizeof(struct pollfd));
237 while (!exit_backend && nr_clients) {
239 flist_for_each(entry, &client_list) {
240 client = flist_entry(entry, struct fio_client, list);
242 pfds[i].fd = client->fd;
243 pfds[i].events = POLLIN;
247 assert(i == nr_clients);
249 ret = poll(pfds, nr_clients, 100);
253 log_err("fio: poll clients: %s\n", strerror(errno));
258 for (i = 0; i < nr_clients; i++) {
259 if (!(pfds[i].revents & POLLIN))
262 client = find_client_by_fd(pfds[i].fd);
264 log_err("fio: unknown client\n");
267 handle_client(client);