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 printf("remove client %s\n", client->hostname);
65 flist_del(&client->list);
67 free(client->hostname);
71 void fio_client_add(const char *hostname)
73 struct fio_client *client;
75 client = malloc(sizeof(*client));
76 memset(client, 0, sizeof(*client));
77 client->hostname = strdup(hostname);
79 flist_add(&client->list, &client_list);
83 static int fio_client_connect(struct fio_client *client)
87 memset(&client->addr, 0, sizeof(client->addr));
88 client->addr.sin_family = AF_INET;
89 client->addr.sin_port = htons(fio_net_port);
91 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
94 hent = gethostbyname(client->hostname);
96 log_err("fio: gethostbyname: %s\n", strerror(errno));
100 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
103 fd = socket(AF_INET, SOCK_STREAM, 0);
105 log_err("fio: socket: %s\n", strerror(errno));
110 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
111 log_err("fio: connect: %s\n", strerror(errno));
120 int fio_clients_connect(void)
122 struct fio_client *client;
123 struct flist_head *entry, *tmp;
126 flist_for_each_safe(entry, tmp, &client_list) {
127 client = flist_entry(entry, struct fio_client, list);
129 ret = fio_client_connect(client);
131 remove_client(client);
137 static int send_file_buf(struct fio_client *client, char *buf, off_t size)
139 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
143 * Send file contents to server backend. We could use sendfile(), but to remain
144 * more portable lets just read/write the darn thing.
146 static int fio_client_send_ini(struct fio_client *client, const char *filename)
153 fd = open(filename, O_RDONLY);
155 log_err("fio: job file open: %s\n", strerror(errno));
159 if (fstat(fd, &sb) < 0) {
160 log_err("fio: job file stat: %s\n", strerror(errno));
164 buf = malloc(sb.st_size);
169 ret = read(fd, p, len);
178 else if (errno == EAGAIN || errno == EINTR)
182 ret = send_file_buf(client, buf, sb.st_size);
187 int fio_clients_send_ini(const char *filename)
189 struct fio_client *client;
190 struct flist_head *entry, *tmp;
192 flist_for_each_safe(entry, tmp, &client_list) {
193 client = flist_entry(entry, struct fio_client, list);
195 if (fio_client_send_ini(client, filename))
196 remove_client(client);
202 static int handle_client(struct fio_client *client)
204 struct fio_net_cmd *cmd;
206 while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
207 if (cmd->opcode == FIO_NET_CMD_ACK) {
211 if (cmd->opcode == FIO_NET_CMD_QUIT) {
212 remove_client(client);
216 if (cmd->opcode != FIO_NET_CMD_TEXT) {
217 printf("non text: %d\n", cmd->opcode);
221 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
229 int fio_handle_clients(void)
231 struct fio_client *client;
232 struct flist_head *entry;
236 pfds = malloc(nr_clients * sizeof(struct pollfd));
238 flist_for_each(entry, &client_list) {
239 client = flist_entry(entry, struct fio_client, list);
241 pfds[i].fd = client->fd;
242 pfds[i].events = POLLIN;
246 while (!exit_backend && nr_clients) {
247 ret = poll(pfds, nr_clients, 100);
251 log_err("fio: poll clients: %s\n", strerror(errno));
256 for (i = 0; i < nr_clients; i++) {
257 if (!(pfds[i].revents & POLLIN))
260 client = find_client_by_fd(pfds[i].fd);
262 log_err("fio: unknown client\n");
265 handle_client(client);