Store specific client ops in the fio_client structure

We could be handling a mix of gui and cli clients from the server,
so passing. Plus this is a lot cleaner.

Also unify ETA opcode handling between clients.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/client.c b/client.c
index 1c609b6..23c5b6c 100644
--- a/client.c
+++ b/client.c
@@ -24,7 +24,6 @@
 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
 static void handle_ts(struct fio_net_cmd *cmd);
 static void handle_gs(struct fio_net_cmd *cmd);
-static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd);
 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
 
@@ -33,7 +32,7 @@
 	.disk_util	= handle_du,
 	.thread_status	= handle_ts,
 	.group_stats	= handle_gs,
-	.eta		= handle_eta,
+	.eta		= display_thread_status,
 	.probe		= handle_probe,
 };
 
@@ -110,7 +109,7 @@
 
 	if (!flist_empty(&client->eta_list)) {
 		flist_del_init(&client->eta_list);
-		fio_client_dec_jobs_eta(client->eta_in_flight, display_thread_status);
+		fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
 	}
 
 	free(client->hostname);
@@ -155,7 +154,8 @@
 	}
 }
 
-struct fio_client *fio_client_add_explicit(const char *hostname, int type,
+struct fio_client *fio_client_add_explicit(struct client_ops *ops,
+					   const char *hostname, int type,
 					   int port)
 {
 	struct fio_client *client;
@@ -186,6 +186,7 @@
 	}
 
 	client->fd = -1;
+	client->ops = ops;
 
 	__fio_client_add_cmd_option(client, "fio");
 
@@ -198,7 +199,7 @@
 	return NULL;
 }
 
-int fio_client_add(const char *hostname, void **cookie)
+int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
 {
 	struct fio_client *existing = *cookie;
 	struct fio_client *client;
@@ -233,6 +234,7 @@
 		return -1;
 
 	client->fd = -1;
+	client->ops = ops;
 
 	__fio_client_add_cmd_option(client, "fio");
 
@@ -754,10 +756,10 @@
 		dst->eta_sec = je->eta_sec;
 }
 
-void fio_client_dec_jobs_eta(struct client_eta *eta, void (*fn)(struct jobs_eta *))
+void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
 {
 	if (!--eta->pending) {
-		fn(&eta->eta);
+		eta_fn(&eta->eta);
 		free(eta);
 	}
 }
@@ -799,7 +801,7 @@
 	flist_del_init(&client->eta_list);
 
 	fio_client_sum_jobs_eta(&eta->eta, je);
-	fio_client_dec_jobs_eta(eta, display_thread_status);
+	fio_client_dec_jobs_eta(eta, client->ops->eta);
 }
 
 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
@@ -855,8 +857,9 @@
 	pdu->log_usec	= le64_to_cpu(pdu->log_usec);
 }
 
-int fio_handle_client(struct fio_client *client, struct client_ops *ops)
+int fio_handle_client(struct fio_client *client)
 {
+	struct client_ops *ops = client->ops;
 	struct fio_net_cmd *cmd;
 
 	dprint(FD_NET, "client: handle %s\n", client->hostname);
@@ -914,7 +917,7 @@
 
 		remove_reply_cmd(client, cmd);
 		convert_jobs_eta(je);
-		ops->eta(client, cmd);
+		handle_eta(client, cmd);
 		free(cmd);
 		break;
 		}
@@ -949,7 +952,7 @@
 	return 1;
 }
 
-static void request_client_etas(void)
+static void request_client_etas(struct client_ops *ops)
 {
 	struct fio_client *client;
 	struct flist_head *entry;
@@ -980,7 +983,7 @@
 	}
 
 	while (skipped--)
-		fio_client_dec_jobs_eta(eta, display_thread_status);
+		fio_client_dec_jobs_eta(eta, ops->eta);
 
 	dprint(FD_NET, "client: requested eta tag %p\n", eta);
 }
@@ -1008,7 +1011,7 @@
 	return flist_empty(&client->cmd_list) && ret;
 }
 
-static int fio_client_timed_out(struct client_ops *ops)
+static int fio_check_clients_timed_out(void)
 {
 	struct fio_client *client;
 	struct flist_head *entry, *tmp;
@@ -1026,8 +1029,8 @@
 		if (!client_check_cmd_timeout(client, &tv))
 			continue;
 
-		if (ops->timed_out)
-			ops->timed_out(client);
+		if (client->ops->timed_out)
+			client->ops->timed_out(client);
 		else
 			log_err("fio: client %s timed out\n", client->hostname);
 
@@ -1059,7 +1062,7 @@
 		flist_for_each_safe(entry, tmp, &client_list) {
 			client = flist_entry(entry, struct fio_client, list);
 
-			if (!client->sent_job && !ops->stay_connected &&
+			if (!client->sent_job && !client->ops->stay_connected &&
 			    flist_empty(&client->cmd_list)) {
 				remove_client(client);
 				continue;
@@ -1080,10 +1083,10 @@
 
 			gettimeofday(&tv, NULL);
 			if (mtime_since(&eta_tv, &tv) >= 900) {
-				request_client_etas();
+				request_client_etas(ops);
 				memcpy(&eta_tv, &tv, sizeof(tv));
 
-				if (fio_client_timed_out(ops))
+				if (fio_check_clients_timed_out())
 					break;
 			}
 
@@ -1106,7 +1109,7 @@
 				log_err("fio: unknown client fd %d\n", pfds[i].fd);
 				continue;
 			}
-			if (!fio_handle_client(client, ops)) {
+			if (!fio_handle_client(client)) {
 				log_info("client: host=%s disconnected\n",
 						client->hostname);
 				remove_client(client);
diff --git a/client.h b/client.h
index 532a33a..e7a1d83 100644
--- a/client.h
+++ b/client.h
@@ -9,6 +9,7 @@
 #include "stat.h"
 
 struct fio_net_cmd;
+struct client_ops;
 
 struct fio_client {
 	struct flist_head list;
@@ -43,6 +44,7 @@
 	uint16_t argc;
 	char **argv;
 
+	struct client_ops *ops;
 	void *client_data;
 };
 
@@ -50,7 +52,7 @@
 typedef void (*client_disk_util_op_func)(struct fio_client *client, struct fio_net_cmd *cmd);
 typedef void (*client_thread_status_op)(struct fio_net_cmd *cmd);
 typedef void (*client_group_stats_op)(struct fio_net_cmd *cmd);
-typedef void (*client_eta_op)(struct fio_client *client, struct fio_net_cmd *cmd);
+typedef void (*client_eta_op)(struct jobs_eta *je);
 typedef void (*client_probe_op)(struct fio_client *client, struct fio_net_cmd *cmd);
 typedef void (*client_thread_status_display_op)(char *status_message, double perc);
 typedef void (*client_quit_op)(struct fio_client *);
@@ -77,8 +79,8 @@
 	unsigned int pending;
 };
 
-extern int fio_handle_client(struct fio_client *, struct client_ops *ops);
-extern void fio_client_dec_jobs_eta(struct client_eta *eta, void (*fn)(struct jobs_eta *));
+extern int fio_handle_client(struct fio_client *);
+extern void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op fn);
 extern void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je);
 
 enum {
@@ -89,9 +91,9 @@
 
 extern int fio_clients_connect(void);
 extern int fio_clients_send_ini(const char *);
-extern int fio_handle_clients(struct client_ops *ops);
-extern int fio_client_add(const char *, void **);
-extern struct fio_client *fio_client_add_explicit(const char *, int, int);
+extern int fio_handle_clients(struct client_ops *);
+extern int fio_client_add(struct client_ops *, const char *, void **);
+extern struct fio_client *fio_client_add_explicit(struct client_ops *, const char *, int, int);
 extern void fio_client_add_cmd_option(void *, const char *);
 extern void fio_clients_terminate(void);
 
diff --git a/gfio.c b/gfio.c
index ac7200a..157dd66 100644
--- a/gfio.c
+++ b/gfio.c
@@ -246,18 +246,6 @@
 	gfio_update_thread_status(output, perc);
 }
 
-static void gfio_eta_op(struct fio_client *client, struct fio_net_cmd *cmd)
-{
-	struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
-	struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
-
-	client->eta_in_flight = NULL;
-	flist_del_init(&client->eta_list);
-
-	fio_client_sum_jobs_eta(&eta->eta, je);
-	fio_client_dec_jobs_eta(eta, gfio_update_eta);
-}
-
 static void gfio_probe_op(struct fio_client *client, struct fio_net_cmd *cmd)
 {
 	struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
@@ -365,7 +353,7 @@
 	.disk_util		= gfio_disk_util_op,
 	.thread_status		= gfio_thread_status_op,
 	.group_stats		= gfio_group_stats_op,
-	.eta			= gfio_eta_op,
+	.eta			= gfio_update_eta,
 	.probe			= gfio_probe_op,
 	.quit			= gfio_quit_op,
 	.add_job		= gfio_add_job_op,
@@ -655,7 +643,7 @@
 		ui.job_files[ui.nr_job_files] = strdup(filenames->data);
 		ui.nr_job_files++;
 
-		ui.client = fio_client_add_explicit(host, type, port);
+		ui.client = fio_client_add_explicit(&gfio_client_ops, host, type, port);
 		if (!ui.client) {
 			GError *error;
 
diff --git a/init.c b/init.c
index fd61c68..0b57a9c 100644
--- a/init.c
+++ b/init.c
@@ -1569,7 +1569,7 @@
 				exit_val = 1;
 				break;
 			}
-			if (fio_client_add(optarg, &cur_client)) {
+			if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
 				log_err("fio: failed adding client %s\n", optarg);
 				do_exit++;
 				exit_val = 1;