ticket-2618-patches-20031207
[openafs.git] / src / rx / rxdebug.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <sys/types.h>
17 #include <errno.h>
18 #ifdef AFS_NT40_ENV
19 #include <winsock2.h>
20 #else
21 #include <sys/socket.h>
22 #include <sys/file.h>
23 #include <netdb.h>
24 #endif
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #ifdef HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #else
37 #ifdef HAVE_STRINGS_H
38 #include <strings.h>
39 #endif
40 #endif
41 #include <sys/stat.h>
42 #include <afs/stds.h>
43 #include <afs/cmd.h>
44
45 #include <stdio.h>
46
47 #include "rx_user.h"
48 #include "rx_clock.h"
49 #include "rx_queue.h"
50 #include "rx.h"
51 #include "rx_globals.h"
52
53
54 #define TIMEOUT     20
55
56 static short
57 PortNumber(aport)
58      register char *aport;
59 {
60     register int tc;
61     register short total;
62
63     total = 0;
64     while (tc = *aport++) {
65         if (tc < '0' || tc > '9')
66             return -1;          /* bad port number */
67         total *= 10;
68         total += tc - (int)'0';
69     }
70     return htons(total);
71 }
72
73 static short
74 PortName(aname)
75      register char *aname;
76 {
77     register struct servent *ts;
78     ts = getservbyname(aname, NULL);
79     if (!ts)
80         return -1;
81     return ts->s_port;          /* returns it in network byte order */
82 }
83
84 int
85 MainCommand(as, arock)
86      char *arock;
87      struct cmd_syndesc *as;
88 {
89     register int i;
90     int s;
91     int j;
92     struct sockaddr_in taddr;
93     afs_int32 host;
94     struct in_addr hostAddr;
95     short port;
96     struct hostent *th;
97     register afs_int32 code;
98     int nodally;
99     int allconns;
100     int rxstats;
101     int onlyClient, onlyServer;
102     afs_int32 onlyHost;
103     short onlyPort;
104     int onlyAuth;
105     int flag;
106     int dallyCounter;
107     int withSecStats;
108     int withAllConn;
109     int withRxStats;
110     int withWaiters;
111     int withIdleThreads;
112     int withPeers;
113     struct rx_debugStats tstats;
114     char *portName, *hostName;
115     struct rx_debugConn tconn;
116     short noConns;
117     short showPeers;
118     int version_flag;
119     char version[64];
120     afs_int32 length = 64;
121
122     afs_uint32 supportedDebugValues = 0;
123     afs_uint32 supportedStatValues = 0;
124     afs_uint32 supportedConnValues = 0;
125     afs_uint32 supportedPeerValues = 0;
126     afs_int32 nextconn = 0;
127     afs_int32 nextpeer = 0;
128
129     nodally = (as->parms[2].items ? 1 : 0);
130     allconns = (as->parms[3].items ? 1 : 0);
131     rxstats = (as->parms[4].items ? 1 : 0);
132     onlyServer = (as->parms[5].items ? 1 : 0);
133     onlyClient = (as->parms[6].items ? 1 : 0);
134     version_flag = (as->parms[10].items ? 1 : 0);
135     noConns = (as->parms[11].items ? 1 : 0);
136     showPeers = (as->parms[12].items ? 1 : 0);
137
138     if (as->parms[0].items)
139         hostName = as->parms[0].items->data;
140     else
141         hostName = NULL;
142
143     if (as->parms[1].items)
144         portName = as->parms[1].items->data;
145     else
146         portName = NULL;
147
148     if (as->parms[7].items) {
149         char *name = as->parms[7].items->data;
150         if ((onlyPort = PortNumber(name)) == -1)
151             onlyPort = PortName(name);
152         if (onlyPort == -1) {
153             printf("rxdebug: can't resolve port name %s\n", name);
154             exit(1);
155         }
156     } else
157         onlyPort = -1;
158
159     if (as->parms[8].items) {
160         char *name = as->parms[8].items->data;
161         struct hostent *th;
162         th = hostutil_GetHostByName(name);
163         if (!th) {
164             printf("rxdebug: host %s not found in host table\n", name);
165             exit(1);
166         }
167         memcpy(&onlyHost, th->h_addr, sizeof(afs_int32));
168     } else
169         onlyHost = -1;
170
171     if (as->parms[9].items) {
172         char *name = as->parms[9].items->data;
173         if (strcmp(name, "clear") == 0)
174             onlyAuth = 0;
175         else if (strcmp(name, "auth") == 0)
176             onlyAuth = 1;
177         else if (strcmp(name, "crypt") == 0)
178             onlyAuth = 2;
179         else if ((strcmp(name, "null") == 0) || (strcmp(name, "none") == 0)
180                  || (strncmp(name, "noauth", 6) == 0)
181                  || (strncmp(name, "unauth", 6) == 0))
182             onlyAuth = -1;
183         else {
184             fprintf(stderr, "Unknown authentication level: %s\n", name);
185             exit(1);
186         }
187     } else
188         onlyAuth = 999;
189
190     /* lookup host */
191     if (hostName) {
192         th = hostutil_GetHostByName(hostName);
193         if (!th) {
194             printf("rxdebug: host %s not found in host table\n", hostName);
195             exit(1);
196         }
197         memcpy(&host, th->h_addr, sizeof(afs_int32));
198     } else
199         host = htonl(0x7f000001);       /* IP localhost */
200
201     if (!portName)
202         port = htons(7000);     /* default is fileserver */
203     else {
204         if ((port = PortNumber(portName)) == -1)
205             port = PortName(portName);
206         if (port == -1) {
207             printf("rxdebug: can't resolve port name %s\n", portName);
208             exit(1);
209         }
210     }
211
212     dallyCounter = 0;
213
214     hostAddr.s_addr = host;
215     printf("Trying %s (port %d):\n", inet_ntoa(hostAddr), ntohs(port));
216     s = socket(AF_INET, SOCK_DGRAM, 0);
217     taddr.sin_family = AF_INET;
218     taddr.sin_port = 0;
219     taddr.sin_addr.s_addr = 0;
220 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
221     taddr.sin_len = sizeof(struct sockaddr_in);
222 #endif
223     code = bind(s, (struct sockaddr *)&taddr, sizeof(struct sockaddr_in));
224     if (code) {
225         perror("bind");
226         exit(1);
227     }
228
229     if (version_flag) {
230
231         code = rx_GetServerVersion(s, host, port, length, version);
232         if (code < 0) {
233             printf("get version call failed with code %d, errno %d\n", code,
234                    errno);
235             exit(1);
236         }
237         printf("AFS version: %s\n", version);
238         fflush(stdout);
239
240         exit(0);
241
242     }
243
244
245     code = rx_GetServerDebug(s, host, port, &tstats, &supportedDebugValues);
246     if (code < 0) {
247         printf("getstats call failed with code %d\n", code);
248         exit(1);
249     }
250
251     withSecStats = (supportedDebugValues & RX_SERVER_DEBUG_SEC_STATS);
252     withAllConn = (supportedDebugValues & RX_SERVER_DEBUG_ALL_CONN);
253     withRxStats = (supportedDebugValues & RX_SERVER_DEBUG_RX_STATS);
254     withWaiters = (supportedDebugValues & RX_SERVER_DEBUG_WAITER_CNT);
255     withIdleThreads = (supportedDebugValues & RX_SERVER_DEBUG_IDLE_THREADS);
256     withPeers = (supportedDebugValues & RX_SERVER_DEBUG_ALL_PEER);
257
258     printf("Free packets: %d, packet reclaims: %d, calls: %d, used FDs: %d\n",
259            tstats.nFreePackets, tstats.packetReclaims, tstats.callsExecuted,
260            tstats.usedFDs);
261     if (!tstats.waitingForPackets)
262         printf("not ");
263     printf("waiting for packets.\n");
264     if (withWaiters)
265         printf("%d calls waiting for a thread\n", tstats.nWaiting);
266     if (withIdleThreads)
267         printf("%d threads are idle\n", tstats.idleThreads);
268
269     if (rxstats) {
270         if (!withRxStats) {
271           noRxStats:
272             withRxStats = 0;
273             fprintf(stderr,
274                     "WARNING: Server doesn't support retrieval of Rx statistics\n");
275         } else {
276             struct rx_stats rxstats;
277
278             /* should gracefully handle the case where rx_stats grows */
279             code =
280                 rx_GetServerStats(s, host, port, &rxstats,
281                                   &supportedStatValues);
282             if (code < 0) {
283                 printf("rxstats call failed with code %d\n", code);
284                 exit(1);
285             }
286             if (code != sizeof(rxstats)) {
287                 if ((((struct rx_debugIn *)(&rxstats))->type ==
288                      RX_DEBUGI_BADTYPE))
289                     goto noRxStats;
290                 printf
291                     ("WARNING: returned Rx statistics of unexpected size (got %d)\n",
292                      code);
293                 /* handle other versions?... */
294             }
295
296             rx_PrintTheseStats(stdout, &rxstats, sizeof(rxstats),
297                                tstats.nFreePackets, tstats.version);
298         }
299     }
300
301     if (!noConns) {
302         if (allconns) {
303             if (!withAllConn)
304                 fprintf(stderr,
305                         "WARNING: Server doesn't support retrieval of all connections,\n         getting only interesting instead.\n");
306         }
307
308         if (onlyServer)
309             printf("Showing only server connections\n");
310         if (onlyClient)
311             printf("Showing only client connections\n");
312         if (onlyAuth != 999) {
313             static char *name[] =
314                 { "unauthenticated", "rxkad_clear", "rxkad_auth",
315                 "rxkad_crypt"
316             };
317             printf("Showing only %s connections\n", name[onlyAuth + 1]);
318         }
319         if (onlyHost != -1) {
320             hostAddr.s_addr = onlyHost;
321             printf("Showing only connections from host %s\n",
322                    inet_ntoa(hostAddr));
323         }
324         if (onlyPort != -1)
325             printf("Showing only connections on port %u\n", ntohs(onlyPort));
326
327         for (i = 0;; i++) {
328             code =
329                 rx_GetServerConnections(s, host, port, &nextconn, allconns,
330                                         supportedDebugValues, &tconn,
331                                         &supportedConnValues);
332             if (code < 0) {
333                 printf("getconn call failed with code %d\n", code);
334                 break;
335             }
336             if (tconn.cid == (afs_int32)0xffffffff) {
337                 printf("Done.\n");
338                 break;
339             }
340
341             /* see if we're in nodally mode and all calls are dallying */
342             if (nodally) {
343                 flag = 0;
344                 for (j = 0; j < RX_MAXCALLS; j++) {
345                     if (tconn.callState[j] != RX_STATE_NOTINIT
346                         && tconn.callState[j] != RX_STATE_DALLY) {
347                         flag = 1;
348                         break;
349                     }
350                 }
351                 if (flag == 0) {
352                     /* this call looks too ordinary, bump skipped count and go
353                      * around again */
354                     dallyCounter++;
355                     continue;
356                 }
357             }
358             if ((onlyHost != -1) && (onlyHost != tconn.host))
359                 continue;
360             if ((onlyPort != -1) && (onlyPort != tconn.port))
361                 continue;
362             if (onlyServer && (tconn.type != RX_SERVER_CONNECTION))
363                 continue;
364             if (onlyClient && (tconn.type != RX_CLIENT_CONNECTION))
365                 continue;
366             if (onlyAuth != 999) {
367                 if (onlyAuth == -1) {
368                     if (tconn.securityIndex != 0)
369                         continue;
370                 } else {
371                     if (tconn.securityIndex != 2)
372                         continue;
373                     if (withSecStats && (tconn.secStats.type == 3)
374                         && (tconn.secStats.level != onlyAuth))
375                         continue;
376                 }
377             }
378
379             /* now display the connection */
380             hostAddr.s_addr = tconn.host;
381             printf("Connection from host %s, port %hu, ", inet_ntoa(hostAddr),
382                    ntohs(tconn.port));
383             if (tconn.epoch)
384                 printf("Cuid %x/%x", tconn.epoch, tconn.cid);
385             else
386                 printf("cid %x", tconn.cid);
387             if (tconn.error)
388                 printf(", error %d", tconn.error);
389             printf("\n  serial %d, ", tconn.serial);
390             printf(" natMTU %d, ", tconn.natMTU);
391
392             if (tconn.flags) {
393                 printf("flags");
394                 if (tconn.flags & RX_CONN_MAKECALL_WAITING)
395                     printf(" MAKECALL_WAITING");
396                 if (tconn.flags & RX_CONN_DESTROY_ME)
397                     printf(" DESTROYED");
398                 if (tconn.flags & RX_CONN_USING_PACKET_CKSUM)
399                     printf(" pktCksum");
400                 printf(", ");
401             }
402             printf("security index %d, ", tconn.securityIndex);
403             if (tconn.type == RX_CLIENT_CONNECTION)
404                 printf("client conn\n");
405             else
406                 printf("server conn\n");
407
408             if (withSecStats) {
409                 switch ((int)tconn.secStats.type) {
410                 case 0:
411                     if (tconn.securityIndex == 2)
412                         printf
413                             ("  no GetStats procedure for security object\n");
414                     break;
415                 case 1:
416                     printf("  rxnull level=%d, flags=%d\n",
417                            tconn.secStats.level, tconn.secStats.flags);
418                     break;
419                 case 2:
420                     printf("  rxvab level=%d, flags=%d\n",
421                            tconn.secStats.level, tconn.secStats.flags);
422                     break;
423                 case 3:{
424                         char *level;
425                         char flags = tconn.secStats.flags;
426                         if (tconn.secStats.level == 0)
427                             level = "clear";
428                         else if (tconn.secStats.level == 1)
429                             level = "auth";
430                         else if (tconn.secStats.level == 2)
431                             level = "crypt";
432                         else
433                             level = "unknown";
434                         printf("  rxkad: level %s", level);
435                         if (flags)
436                             printf(", flags");
437                         if (flags & 1)
438                             printf(" unalloc");
439                         if (flags & 2)
440                             printf(" authenticated");
441                         if (flags & 4)
442                             printf(" expired");
443                         if (flags & 8)
444                             printf(" pktCksum");
445                         if (tconn.secStats.expires)
446                             /* Apparently due to a bug in the RT compiler that
447                              * prevents (afs_uint32)0xffffffff => (double) from working,
448                              * this code produces negative lifetimes when run on the
449                              * RT. */
450                             printf(", expires in %.1f hours",
451                                    ((afs_uint32) tconn.secStats.expires -
452                                     time(0)) / 3600.0);
453                         if (!(flags & 1)) {
454                             printf("\n  Received %d bytes in %d packets\n",
455                                    tconn.secStats.bytesReceived,
456                                    tconn.secStats.packetsReceived);
457                             printf("  Sent %d bytes in %d packets\n",
458                                    tconn.secStats.bytesSent,
459                                    tconn.secStats.packetsSent);
460                         } else
461                             printf("\n");
462                         break;
463                     }
464
465                 default:
466                     printf("  unknown\n");
467                 }
468             }
469
470             for (j = 0; j < RX_MAXCALLS; j++) {
471                 printf("    call %d: # %d, state ", j, tconn.callNumber[j]);
472                 if (tconn.callState[j] == RX_STATE_NOTINIT) {
473                     printf("not initialized\n");
474                     continue;
475                 } else if (tconn.callState[j] == RX_STATE_PRECALL)
476                     printf("precall, ");
477                 else if (tconn.callState[j] == RX_STATE_ACTIVE)
478                     printf("active, ");
479                 else if (tconn.callState[j] == RX_STATE_DALLY)
480                     printf("dally, ");
481                 else if (tconn.callState[j] == RX_STATE_HOLD)
482                     printf("hold, ");
483                 printf("mode: ");
484                 if (tconn.callMode[j] == RX_MODE_SENDING)
485                     printf("sending");
486                 else if (tconn.callMode[j] == RX_MODE_RECEIVING)
487                     printf("receiving");
488                 else if (tconn.callMode[j] == RX_MODE_ERROR)
489                     printf("error");
490                 else if (tconn.callMode[j] == RX_MODE_EOF)
491                     printf("eof");
492                 else
493                     printf("unknown");
494                 if (tconn.callFlags[j]) {
495                     printf(", flags:");
496                     if (tconn.callFlags[j] & RX_CALL_READER_WAIT)
497                         printf(" reader_wait");
498                     if (tconn.callFlags[j] & RX_CALL_WAIT_WINDOW_ALLOC)
499                         printf(" window_alloc");
500                     if (tconn.callFlags[j] & RX_CALL_WAIT_WINDOW_SEND)
501                         printf(" window_send");
502                     if (tconn.callFlags[j] & RX_CALL_WAIT_PACKETS)
503                         printf(" wait_packets");
504                     if (tconn.callFlags[j] & RX_CALL_WAIT_PROC)
505                         printf(" waiting_for_process");
506                     if (tconn.callFlags[j] & RX_CALL_RECEIVE_DONE)
507                         printf(" receive_done");
508                     if (tconn.callFlags[j] & RX_CALL_CLEARED)
509                         printf(" call_cleared");
510                 }
511                 if (tconn.callOther[j] & RX_OTHER_IN)
512                     printf(", has_input_packets");
513                 if (tconn.callOther[j] & RX_OTHER_OUT)
514                     printf(", has_output_packets");
515                 printf("\n");
516             }
517         }
518         if (nodally)
519             printf("Skipped %d dallying connections.\n", dallyCounter);
520     }
521     if (showPeers && withPeers) {
522         for (i = 0;; i++) {
523             struct rx_debugPeer tpeer;
524             code =
525                 rx_GetServerPeers(s, host, port, &nextpeer, allconns, &tpeer,
526                                   &supportedPeerValues);
527             if (code < 0) {
528                 printf("getpeer call failed with code %d\n", code);
529                 break;
530             }
531             if (tpeer.host == 0xffffffff) {
532                 printf("Done.\n");
533                 break;
534             }
535
536             if ((onlyHost != -1) && (onlyHost != tpeer.host))
537                 continue;
538             if ((onlyPort != -1) && (onlyPort != tpeer.port))
539                 continue;
540
541             /* now display the peer */
542             hostAddr.s_addr = tpeer.host;
543             printf("Peer at host %s, port %hu\n", inet_ntoa(hostAddr),
544                    ntohs(tpeer.port));
545             printf("\tifMTU %hu\tnatMTU %hu\tmaxMTU %hu\n", tpeer.ifMTU,
546                    tpeer.natMTU, tpeer.maxMTU);
547             printf("\tpackets sent %d\tpacket resends %d\n", tpeer.nSent,
548                    tpeer.reSends);
549             printf("\tbytes sent high %d low %d\n", tpeer.bytesSent.high,
550                    tpeer.bytesSent.low);
551             printf("\tbytes received high %d low %d\n",
552                    tpeer.bytesReceived.high, tpeer.bytesReceived.low);
553             printf("\trtt %d msec, rtt_dev %d msec\n", tpeer.rtt >> 3,
554                    tpeer.rtt_dev >> 2);
555             printf("\ttimeout %d.%03d sec\n", tpeer.timeout.sec,
556                    tpeer.timeout.usec / 1000);
557         }
558     }
559     exit(0);
560 }
561
562 /* simple main program */
563 #ifndef AFS_NT40_ENV
564 #include "AFS_component_version_number.c"
565 #endif
566 int
567 main(argc, argv)
568      int argc;
569      char **argv;
570 {
571     struct cmd_syndesc *ts;
572
573 #ifdef AFS_NT40_ENV
574     if (afs_winsockInit() < 0) {
575         printf("%s: Couldn't initialize winsock. Exiting...\n", argv[0]);
576         return 1;
577     }
578 #endif
579
580     ts = cmd_CreateSyntax(NULL, MainCommand, 0, "probe RX server");
581     cmd_AddParm(ts, "-servers", CMD_SINGLE, CMD_REQUIRED, "server machine");
582     cmd_AddParm(ts, "-port", CMD_SINGLE, CMD_OPTIONAL, "IP port");
583     cmd_AddParm(ts, "-nodally", CMD_FLAG, CMD_OPTIONAL,
584                 "don't show dallying conns");
585     cmd_AddParm(ts, "-allconnections", CMD_FLAG, CMD_OPTIONAL,
586                 "don't filter out uninteresting connections on server");
587     cmd_AddParm(ts, "-rxstats", CMD_FLAG, CMD_OPTIONAL, "show Rx statistics");
588     cmd_AddParm(ts, "-onlyserver", CMD_FLAG, CMD_OPTIONAL,
589                 "only show server conns");
590     cmd_AddParm(ts, "-onlyclient", CMD_FLAG, CMD_OPTIONAL,
591                 "only show client conns");
592     cmd_AddParm(ts, "-onlyport", CMD_SINGLE, CMD_OPTIONAL,
593                 "show only <port>");
594     cmd_AddParm(ts, "-onlyhost", CMD_SINGLE, CMD_OPTIONAL,
595                 "show only <host>");
596     cmd_AddParm(ts, "-onlyauth", CMD_SINGLE, CMD_OPTIONAL,
597                 "show only <auth level>");
598
599     cmd_AddParm(ts, "-version", CMD_FLAG, CMD_OPTIONAL,
600                 "show AFS version id");
601     cmd_AddParm(ts, "-noconns", CMD_FLAG, CMD_OPTIONAL,
602                 "show no connections");
603     cmd_AddParm(ts, "-peers", CMD_FLAG, CMD_OPTIONAL, "show peers");
604
605     cmd_Dispatch(argc, argv);
606     exit(0);
607 }