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 dprint(FD_NET, "removed 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 dprint(FD_NET, "added client <%s>\n", hostname);
76 client = malloc(sizeof(*client));
77 memset(client, 0, sizeof(*client));
78 client->hostname = strdup(hostname);
80 flist_add(&client->list, &client_list);
84 static int fio_client_connect(struct fio_client *client)
88 dprint(FD_NET, "connect to host %s\n", client->hostname);
90 memset(&client->addr, 0, sizeof(client->addr));
91 client->addr.sin_family = AF_INET;
92 client->addr.sin_port = htons(fio_net_port);
94 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
97 hent = gethostbyname(client->hostname);
99 log_err("fio: gethostbyname: %s\n", strerror(errno));
103 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
106 fd = socket(AF_INET, SOCK_STREAM, 0);
108 log_err("fio: socket: %s\n", strerror(errno));
112 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
113 log_err("fio: connect: %s\n", strerror(errno));
121 int fio_clients_connect(void)
123 struct fio_client *client;
124 struct flist_head *entry, *tmp;
127 flist_for_each_safe(entry, tmp, &client_list) {
128 client = flist_entry(entry, struct fio_client, list);
130 ret = fio_client_connect(client);
132 remove_client(client);
138 static int send_file_buf(struct fio_client *client, char *buf, off_t size)
140 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
144 * Send file contents to server backend. We could use sendfile(), but to remain
145 * more portable lets just read/write the darn thing.
147 static int fio_client_send_ini(struct fio_client *client, const char *filename)
154 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
156 fd = open(filename, O_RDONLY);
158 log_err("fio: job file open: %s\n", strerror(errno));
162 if (fstat(fd, &sb) < 0) {
163 log_err("fio: job file stat: %s\n", strerror(errno));
167 buf = malloc(sb.st_size);
172 ret = read(fd, p, len);
181 else if (errno == EAGAIN || errno == EINTR)
185 ret = send_file_buf(client, buf, sb.st_size);
190 int fio_clients_send_ini(const char *filename)
192 struct fio_client *client;
193 struct flist_head *entry, *tmp;
195 flist_for_each_safe(entry, tmp, &client_list) {
196 client = flist_entry(entry, struct fio_client, list);
198 if (fio_client_send_ini(client, filename))
199 remove_client(client);
205 static int handle_client(struct fio_client *client)
207 struct fio_net_cmd *cmd;
209 while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
210 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
213 if (cmd->opcode == FIO_NET_CMD_ACK) {
217 if (cmd->opcode == FIO_NET_CMD_QUIT) {
218 remove_client(client);
222 if (cmd->opcode != FIO_NET_CMD_TEXT) {
223 printf("non text: %d\n", cmd->opcode);
227 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
235 int fio_handle_clients(void)
237 struct fio_client *client;
238 struct flist_head *entry;
242 pfds = malloc(nr_clients * sizeof(struct pollfd));
244 while (!exit_backend && nr_clients) {
246 flist_for_each(entry, &client_list) {
247 client = flist_entry(entry, struct fio_client, list);
249 pfds[i].fd = client->fd;
250 pfds[i].events = POLLIN;
254 assert(i == nr_clients);
256 ret = poll(pfds, nr_clients, 100);
260 log_err("fio: poll clients: %s\n", strerror(errno));
265 for (i = 0; i < nr_clients; i++) {
266 if (!(pfds[i].revents & POLLIN))
269 client = find_client_by_fd(pfds[i].fd);
271 log_err("fio: unknown client\n");
274 handle_client(client);