1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
9 /* Synthetic client for benchmarking: a tool for directly generating NFS
10 * requests and reading back the responses, so that we can exercise the server
11 * differently than the Linux kernel NFS client does.
13 * Much of this is copied from rpc.c and other BlueSky server code but is
14 * designed to run independently of BlueSky. */
16 #include "mount_prot.h"
17 #include "nfs3_prot.h"
21 #include <rpc/pmap_clnt.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
32 /* TCP port number to use for NFS protocol. (Should be 2049.) */
33 #define NFS_SERVICE_PORT 2051
35 /* Maximum size of a single RPC message that we will accept (8 MB). */
36 #define MAX_RPC_MSGSIZE (8 << 20)
50 struct rpc_fail_reply {
59 struct rpc_call_header {
76 /* The reassembled message, thus far. */
79 /* Remaining number of bytes in this message fragment; 0 if we next expect
80 * another fragment header. */
83 /* If frag_len is zero: the number of bytes of the fragment header that
84 * have been read so far. */
87 /* Mapping of XID values to outstanding RPC calls. */
88 GHashTable *xid_table;
92 typedef void (*NFSFunc)(NFSConnection *nfs,
93 gpointer user_data, const char *reply, size_t len);
101 static GMainLoop *main_loop;
105 struct timespec time;
107 if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
108 perror("clock_gettime");
112 return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
115 static void do_write(NFSConnection *conn, const char *buf, size_t len)
119 switch (g_io_channel_write_chars(conn->channel, buf, len,
121 case G_IO_STATUS_ERROR:
122 case G_IO_STATUS_EOF:
123 case G_IO_STATUS_AGAIN:
124 fprintf(stderr, "Error writing to socket!\n");
126 case G_IO_STATUS_NORMAL:
134 static void send_rpc(NFSConnection *nfs, int proc, GString *msg,
135 NFSFunc completion_handler, gpointer user_data)
137 static int xid_count = 0;
138 struct rpc_call_header header;
139 struct rpc_auth auth;
141 header.xid = GUINT32_TO_BE(xid_count++);
143 header.rpcvers = GUINT32_TO_BE(2);
144 header.prog = GUINT32_TO_BE(NFS_PROGRAM);
145 header.vers = GUINT32_TO_BE(NFS_V3);
146 header.proc = GUINT32_TO_BE(proc);
148 auth.flavor = GUINT32_TO_BE(AUTH_NULL);
151 CallInfo *info = g_new0(CallInfo, 1);
153 uint32_t fragment = htonl(0x80000000
154 | (sizeof(header) + 2*sizeof(auth) + msg->len));
155 do_write(nfs, (const char *)&fragment, sizeof(fragment));
156 do_write(nfs, (const char *)&header, sizeof(header));
157 do_write(nfs, (const char *)&auth, sizeof(auth));
158 do_write(nfs, (const char *)&auth, sizeof(auth));
159 do_write(nfs, msg->str, msg->len);
160 g_io_channel_flush(nfs->channel, NULL);
162 info->start = now_hires();
163 info->callback = completion_handler;
164 info->user_data = user_data;
165 g_hash_table_insert(nfs->xid_table,
166 GINT_TO_POINTER(GUINT32_FROM_BE(header.xid)), info);
169 static void process_reply(NFSConnection *nfs, GString *msg)
171 struct rpc_reply *reply = (struct rpc_reply *)msg->str;
173 uint32_t xid = GUINT32_FROM_BE(reply->xid);
175 gpointer key = GINT_TO_POINTER(GUINT32_FROM_BE(reply->xid));
176 CallInfo *info = g_hash_table_lookup(nfs->xid_table, key);
178 g_print("Could not match reply XID %d with a call!\n", xid);
182 info->end = now_hires();
183 printf("XID %d: Time = %"PRIi64"\n", xid, info->end - info->start);
184 if (info->callback != NULL)
185 info->callback(nfs, info->user_data,
186 msg->str + sizeof(*reply), msg->len - sizeof(*reply));
188 g_hash_table_remove(nfs->xid_table, key);
192 if (completed == 5 * threads) {
193 g_main_loop_quit(main_loop);
197 static gboolean read_handler(GIOChannel *channel,
198 GIOCondition condition,
201 NFSConnection *nfs = (NFSConnection *)data;
203 gsize bytes_to_read = 0; /* Number of bytes to attempt to read. */
205 /* If we have not yet read in the fragment header, do that first. This is
206 * 4 bytes that indicates the number of bytes in the message to follow
207 * (with the high bit set if this is the last fragment making up the
209 if (nfs->frag_len == 0) {
210 bytes_to_read = 4 - nfs->frag_hdr_bytes;
212 bytes_to_read = nfs->frag_len & 0x7fffffff;
215 if (bytes_to_read > MAX_RPC_MSGSIZE
216 || nfs->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
218 fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
220 g_io_channel_shutdown(nfs->channel, TRUE, NULL);
224 gsize bytes_read = 0;
225 g_string_set_size(nfs->msgbuf, nfs->msgbuf->len + bytes_to_read);
226 char *buf = &nfs->msgbuf->str[nfs->msgbuf->len - bytes_to_read];
227 switch (g_io_channel_read_chars(nfs->channel, buf,
228 bytes_to_read, &bytes_read, NULL)) {
229 case G_IO_STATUS_NORMAL:
231 case G_IO_STATUS_AGAIN:
233 case G_IO_STATUS_EOF:
234 if (bytes_read == bytes_to_read)
236 /* else fall through */
237 case G_IO_STATUS_ERROR:
238 fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
239 g_io_channel_unix_get_fd(nfs->channel));
240 g_io_channel_shutdown(nfs->channel, TRUE, NULL);
241 /* TODO: Clean up connection object. */
245 g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
247 g_string_set_size(nfs->msgbuf,
248 nfs->msgbuf->len - (bytes_to_read - bytes_read));
250 if (nfs->frag_len == 0) {
251 /* Handle reading in the fragment header. If we've read the complete
252 * header, store the fragment size. */
253 nfs->frag_hdr_bytes += bytes_read;
254 if (nfs->frag_hdr_bytes == 4) {
255 memcpy((char *)&nfs->frag_len,
256 &nfs->msgbuf->str[nfs->msgbuf->len - 4], 4);
257 nfs->frag_len = ntohl(nfs->frag_len);
258 g_string_set_size(nfs->msgbuf, nfs->msgbuf->len - 4);
259 nfs->frag_hdr_bytes = 0;
262 /* We were reading in the fragment body. */
263 nfs->frag_len -= bytes_read;
265 if (nfs->frag_len == 0x80000000) {
266 process_reply(nfs, nfs->msgbuf);
268 g_string_set_size(nfs->msgbuf, 0);
275 static void send_read_requests(NFSConnection *nfs, const struct nfs_fh3 *fh)
279 g_print("Sending read requests...\n");
280 for (i = 0; i < 4; i++) {
282 struct read3args read;
283 memcpy(&read.file, fh, sizeof(struct nfs_fh3));
284 read.offset = (1 << 20) * i;
285 read.count = (1 << 20);
287 GString *str = g_string_new("");
289 xdr_string_create(&xdr, str, XDR_ENCODE);
290 xdr_read3args(&xdr, &read);
291 send_rpc(nfs, NFSPROC3_READ, str, NULL, NULL);
292 g_string_free(str, TRUE);
296 static void store_fh(NFSConnection *nfs, gpointer user_data,
297 const char *reply, size_t len)
299 struct lookup3res res;
301 memset(&res, 0, sizeof(res));
302 xdrmem_create(&xdr, (char *)reply, len, XDR_DECODE);
303 if (!xdr_lookup3res(&xdr, &res)) {
304 g_print("Decode error for lookup3res!\n");
307 if (res.status != NFS3_OK) {
308 g_print("Response not NFS3_OK\n");
312 struct nfs_fh3 *fh = g_new0(struct nfs_fh3, 1);
313 fh->data.data_len = res.lookup3res_u.resok.object.data.data_len;
314 fh->data.data_val = g_memdup(res.lookup3res_u.resok.object.data.data_val,
318 xdr_lookup3res(&xdr, &res);
320 send_read_requests(nfs, fh);
323 static gboolean idle_handler(gpointer data)
325 NFSConnection *nfs = (NFSConnection *)data;
328 g_print("Sending requests...\n");
329 for (i = 0; i < threads; i++) {
331 struct diropargs3 lookup;
332 uint64_t rootfh = GUINT64_TO_BE(1);
334 sprintf(buf, "file-%d", i + 1);
335 lookup.dir.data.data_len = 8;
336 lookup.dir.data.data_val = (char *)&rootfh;
339 GString *str = g_string_new("");
341 xdr_string_create(&xdr, str, XDR_ENCODE);
342 xdr_diropargs3(&xdr, &lookup);
343 send_rpc(nfs, NFSPROC3_LOOKUP, str, store_fh, NULL);
344 g_string_free(str, TRUE);
350 NFSConnection *nfs_connect(const char *hostname)
353 struct addrinfo hints;
354 struct addrinfo *ai = NULL;
356 int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
358 fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
362 memset(&hints, 0, sizeof(hints));
363 hints.ai_family = AF_INET;
364 hints.ai_socktype = SOCK_STREAM;
365 hints.ai_protocol = IPPROTO_TCP;
366 result = getaddrinfo(hostname, "2051", NULL, &ai);
367 if (result < 0 || ai == NULL) {
368 fprintf(stderr, "Hostname lookup failure for %s: %s\n",
369 hostname, gai_strerror(result));
373 if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
374 fprintf(stderr, "Unable to connect to : %m\n");
379 NFSConnection *conn = g_new0(NFSConnection, 1);
380 conn->msgbuf = g_string_new("");
381 conn->xid_table = g_hash_table_new(g_direct_hash, g_direct_equal);
383 conn->channel = g_io_channel_unix_new(fd);
384 g_io_channel_set_encoding(conn->channel, NULL, NULL);
385 g_io_channel_set_flags(conn->channel, G_IO_FLAG_NONBLOCK, NULL);
386 g_io_add_watch(conn->channel, G_IO_IN, read_handler, conn);
388 g_idle_add(idle_handler, conn);
393 int main(int argc, char *argv[])
396 g_set_prgname("synclient");
397 g_print("Launching synthetic NFS RPC client...\n");
401 threads = atoi(argv[1]);
403 main_loop = g_main_loop_new(NULL, FALSE);
404 nfs_connect("niniel.sysnet.ucsd.edu");
406 g_main_loop_run(main_loop);