Hook synthetic NFS client into test script.
[bluesky.git] / nfs3 / synclient.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
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.
12  *
13  * Much of this is copied from rpc.c and other BlueSky server code but is
14  * designed to run independently of BlueSky. */
15
16 #include "mount_prot.h"
17 #include "nfs3_prot.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <inttypes.h>
21 #include <rpc/pmap_clnt.h>
22 #include <string.h>
23 #include <signal.h>
24 #include <memory.h>
25 #include <netdb.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 #include <glib.h>
31
32 /* TCP port number to use for NFS protocol.  (Should be 2049.) */
33 #define NFS_SERVICE_PORT 2051
34
35 /* Maximum size of a single RPC message that we will accept (8 MB). */
36 #define MAX_RPC_MSGSIZE (8 << 20)
37
38 int threads;
39 int completed = 0;
40
41 struct rpc_reply {
42     uint32_t xid;
43     uint32_t type;
44     uint32_t stat;
45     uint32_t verf_flavor;
46     uint32_t verf_len;
47     uint32_t accept_stat;
48 };
49
50 struct rpc_fail_reply {
51     uint32_t xid;
52     uint32_t type;
53     uint32_t stat;
54     uint32_t verf_flavor;
55     uint32_t verf_len;
56     uint32_t accept_stat;
57 };
58
59 struct rpc_call_header {
60     uint32_t xid;
61     uint32_t mtype;
62     uint32_t rpcvers;
63     uint32_t prog;
64     uint32_t vers;
65     uint32_t proc;
66 };
67
68 struct rpc_auth {
69     uint32_t flavor;
70     uint32_t len;
71 };
72
73 typedef struct {
74     GIOChannel *channel;
75
76     /* The reassembled message, thus far. */
77     GString *msgbuf;
78
79     /* Remaining number of bytes in this message fragment; 0 if we next expect
80      * another fragment header. */
81     uint32_t frag_len;
82
83     /* If frag_len is zero: the number of bytes of the fragment header that
84      * have been read so far. */
85     int frag_hdr_bytes;
86
87     /* Mapping of XID values to outstanding RPC calls. */
88     GHashTable *xid_table;
89 } NFSConnection;
90
91 typedef struct {
92     GFunc callback;
93     gpointer user_data;
94     int64_t start, end;
95 } CallInfo;
96
97 static GMainLoop *main_loop;
98
99 int64_t now_hires()
100 {
101     struct timespec time;
102
103     if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
104         perror("clock_gettime");
105         return 0;
106     }
107
108     return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
109 }
110
111 static void do_write(NFSConnection *conn, const char *buf, size_t len)
112 {
113     while (len > 0) {
114         gsize written = 0;
115         switch (g_io_channel_write_chars(conn->channel, buf, len,
116                                          &written, NULL)) {
117         case G_IO_STATUS_ERROR:
118         case G_IO_STATUS_EOF:
119         case G_IO_STATUS_AGAIN:
120             fprintf(stderr, "Error writing to socket!\n");
121             return;
122         case G_IO_STATUS_NORMAL:
123             len -= written;
124             buf += written;
125             break;
126         }
127     }
128 }
129
130 static void send_rpc(NFSConnection *nfs, int proc, GString *msg,
131                      GFunc completion_handler, gpointer user_data)
132 {
133     static int xid_count = 0;
134     struct rpc_call_header header;
135     struct rpc_auth auth;
136
137     header.xid = GUINT32_TO_BE(xid_count++);
138     header.mtype = 0;
139     header.rpcvers = GUINT32_TO_BE(2);
140     header.prog = GUINT32_TO_BE(NFS_PROGRAM);
141     header.vers = GUINT32_TO_BE(NFS_V3);
142     header.proc = GUINT32_TO_BE(proc);
143
144     auth.flavor = GUINT32_TO_BE(AUTH_NULL);
145     auth.len = 0;
146
147     CallInfo *info = g_new0(CallInfo, 1);
148
149     uint32_t fragment = htonl(0x80000000
150                               | (sizeof(header) + 2*sizeof(auth) + msg->len));
151     do_write(nfs, (const char *)&fragment, sizeof(fragment));
152     do_write(nfs, (const char *)&header, sizeof(header));
153     do_write(nfs, (const char *)&auth, sizeof(auth));
154     do_write(nfs, (const char *)&auth, sizeof(auth));
155     do_write(nfs, msg->str, msg->len);
156     g_io_channel_flush(nfs->channel, NULL);
157
158     info->start = now_hires();
159     info->callback = completion_handler;
160     info->user_data = user_data;
161     g_hash_table_insert(nfs->xid_table,
162                         GINT_TO_POINTER(GUINT32_FROM_BE(header.xid)), info);
163 }
164
165 static void process_reply(NFSConnection *nfs, GString *msg)
166 {
167     struct rpc_reply *reply = (struct rpc_reply *)msg->str;
168
169     uint32_t xid = GUINT32_FROM_BE(reply->xid);
170
171     gpointer key = GINT_TO_POINTER(GUINT32_FROM_BE(reply->xid));
172     CallInfo *info = g_hash_table_lookup(nfs->xid_table, key);
173     if (info == NULL) {
174         g_print("Could not match reply XID %d with a call!\n", xid);
175         return;
176     }
177
178     info->end = now_hires();
179     printf("XID %d: Time = %"PRIi64"\n", xid, info->end - info->start);
180     if (info->callback != NULL)
181         info->callback(nfs, info->user_data);
182
183     g_hash_table_remove(nfs->xid_table, key);
184     g_free(info);
185
186     completed++;
187     if (completed == threads) {
188         g_main_loop_quit(main_loop);
189     }
190 }
191
192 static gboolean read_handler(GIOChannel *channel,
193                              GIOCondition condition,
194                              gpointer data)
195 {
196     NFSConnection *nfs = (NFSConnection *)data;
197
198     gsize bytes_to_read = 0;    /* Number of bytes to attempt to read. */
199
200     /* If we have not yet read in the fragment header, do that first.  This is
201      * 4 bytes that indicates the number of bytes in the message to follow
202      * (with the high bit set if this is the last fragment making up the
203      * message). */
204     if (nfs->frag_len == 0) {
205         bytes_to_read = 4 - nfs->frag_hdr_bytes;
206     } else {
207         bytes_to_read = nfs->frag_len & 0x7fffffff;
208     }
209
210     if (bytes_to_read > MAX_RPC_MSGSIZE
211         || nfs->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
212     {
213         fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
214                 bytes_to_read);
215         g_io_channel_shutdown(nfs->channel, TRUE, NULL);
216         return FALSE;
217     }
218
219     gsize bytes_read = 0;
220     g_string_set_size(nfs->msgbuf, nfs->msgbuf->len + bytes_to_read);
221     char *buf = &nfs->msgbuf->str[nfs->msgbuf->len - bytes_to_read];
222     switch (g_io_channel_read_chars(nfs->channel, buf,
223                                     bytes_to_read, &bytes_read, NULL)) {
224     case G_IO_STATUS_NORMAL:
225         break;
226     case G_IO_STATUS_AGAIN:
227         return TRUE;
228     case G_IO_STATUS_EOF:
229         if (bytes_read == bytes_to_read)
230             break;
231         /* else fall through */
232     case G_IO_STATUS_ERROR:
233         fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
234                 g_io_channel_unix_get_fd(nfs->channel));
235         g_io_channel_shutdown(nfs->channel, TRUE, NULL);
236         /* TODO: Clean up connection object. */
237         return FALSE;
238     }
239
240     g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
241
242     g_string_set_size(nfs->msgbuf,
243                       nfs->msgbuf->len - (bytes_to_read - bytes_read));
244
245     if (nfs->frag_len == 0) {
246         /* Handle reading in the fragment header.  If we've read the complete
247          * header, store the fragment size. */
248         nfs->frag_hdr_bytes += bytes_read;
249         if (nfs->frag_hdr_bytes == 4) {
250             memcpy((char *)&nfs->frag_len,
251                    &nfs->msgbuf->str[nfs->msgbuf->len - 4], 4);
252             nfs->frag_len = ntohl(nfs->frag_len);
253             g_string_set_size(nfs->msgbuf, nfs->msgbuf->len - 4);
254             nfs->frag_hdr_bytes = 0;
255         }
256     } else {
257         /* We were reading in the fragment body. */
258         nfs->frag_len -= bytes_read;
259
260         if (nfs->frag_len = 0x80000000) {
261             process_reply(nfs, nfs->msgbuf);
262             nfs->frag_len = 0;
263             g_string_set_size(nfs->msgbuf, 0);
264         }
265     }
266
267     return TRUE;
268 }
269
270 static gboolean idle_handler(gpointer data)
271 {
272     NFSConnection *nfs = (NFSConnection *)data;
273     int i;
274
275     g_print("Sending requests...\n");
276     for (i = 0; i < threads; i++) {
277         char buf[64];
278         struct diropargs3 lookup;
279         uint64_t rootfh = GUINT64_TO_BE(1);
280
281         sprintf(buf, "file-%d", i + 1);
282         lookup.dir.data.data_len = 8;
283         lookup.dir.data.data_val = (char *)&rootfh;
284         lookup.name = buf;
285
286         GString *str = g_string_new("");
287         XDR xdr;
288         xdr_string_create(&xdr, str, XDR_ENCODE);
289         xdr_diropargs3(&xdr, &lookup);
290         send_rpc(nfs, NFSPROC3_LOOKUP, str, NULL, NULL);
291         g_string_free(str, TRUE);
292     }
293
294     return FALSE;
295 }
296
297 NFSConnection *nfs_connect(const char *hostname)
298 {
299     int result;
300     struct addrinfo hints;
301     struct addrinfo *ai = NULL;
302
303     int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
304     if (fd < 0) {
305         fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
306         exit(1);
307     }
308
309     memset(&hints, 0, sizeof(hints));
310     hints.ai_family = AF_INET;
311     hints.ai_socktype = SOCK_STREAM;
312     hints.ai_protocol = IPPROTO_TCP;
313     result = getaddrinfo(hostname, "2051", NULL, &ai);
314     if (result < 0 || ai == NULL) {
315         fprintf(stderr, "Hostname lookup failure for %s: %s\n",
316                 hostname, gai_strerror(result));
317         exit(1);
318     }
319
320     if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
321         fprintf(stderr, "Unable to connect to : %m\n");
322     }
323
324     freeaddrinfo(ai);
325
326     NFSConnection *conn = g_new0(NFSConnection, 1);
327     conn->msgbuf = g_string_new("");
328     conn->xid_table = g_hash_table_new(g_direct_hash, g_direct_equal);
329
330     conn->channel = g_io_channel_unix_new(fd);
331     g_io_channel_set_encoding(conn->channel, NULL, NULL);
332     g_io_channel_set_flags(conn->channel, G_IO_FLAG_NONBLOCK, NULL);
333     g_io_add_watch(conn->channel, G_IO_IN, read_handler, conn);
334
335     g_idle_add(idle_handler, conn);
336
337     return conn;
338 }
339
340 int main(int argc, char *argv[])
341 {
342     g_thread_init(NULL);
343     g_set_prgname("synclient");
344     g_print("Launching synthetic NFS RPC client...\n");
345
346     threads = 8;
347     if (argc > 1)
348         threads = atoi(argv[1]);
349
350     main_loop = g_main_loop_new(NULL, FALSE);
351     nfs_connect("niniel.sysnet.ucsd.edu");
352
353     g_main_loop_run(main_loop);
354
355     return 0;
356 }