Debian Codegraph @

test/nfstest_file

raw
1 #!/usr/bin/env python3
2 #===============================================================================
3 # Copyright 2014 NetApp, Inc. All Rights Reserved,
4 # contribution by Jorge Mora <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 #===============================================================================
15 import os
16 import re
17 import sys
18 import time
19 import formatstr
20 import packet.utils as utils
21 import packet.record as record
22 from packet.pktt import Pktt,crc32
23 import packet.nfs.nfs3_const as nfs3
24 import packet.nfs.nfs4_const as nfs4
25 from optparse import OptionParser,OptionGroup,IndentedHelpFormatter,SUPPRESS_HELP
26
27 # Module constants
28 __author__ = "Jorge Mora ([email protected])"
29 __copyright__ = "Copyright (C) 2014 NetApp, Inc."
30 __license__ = "GPL v2"
31 __version__ = "1.2"
32
33 USAGE = """%prog [options] -p <filepath> <trace1.cap> [<trace2.cap> ...]
34
35 Find all packets for a specific file
36 ====================================
37 Display all NFS packets for the specified path. It takes a relative path,
38 where it searches for each of the directory entries given in the path until
39 it gets the file handle for the directory where the file is located. Once the
40 directory file handle is found, a LOOKUP or OPEN/CREATE is searched for the
41 given file name. If the file lookup or creation is found, all file handles
42 and state ids associated with that file are searched and all packets found,
43 including their respective replies are displayed.
44
45 There are three levels of verbosity in which they are specified using
46 a bitmap, where the most significant bit gives a more verbose output.
47 Verbose level 1 is used as a default where each packet is displayed
48 condensed to one line using the last layer of the packet as the main output.
49
50 The packet trace files are processed either serially or in parallel.
51 The packets are displayed using their timestamps so they are always
52 displayed in the correct order even if the files given are out of order.
53 If the packet traces were captured one after the other the packets
54 are displayed serially, first the packets of the first file according
55 to their timestamps, then the second and so forth. If the packet traces
56 were captured at the same time on multiple clients the packets are
57 displayed in parallel, packets are interleaved from all the files when
58 displayed again according to their timestamps.
59
60 Note:
61 A packet call can be displayed out of order if the call is not matched
62 by any of the file handles, state ids or names but its reply is matched
63 so its corresponding call is displayed right before the reply.
64
65 Examples:
66 # Find all packets for relative path:
67 %prog -p data/name_d_1/name_d_2/name_f_13 nested_dir_v3.cap
68
69 # Find all packets for relative path, starting with a directory file handle:
70 %prog -p DH:0x34ac5f28/name_d_1/name_d_2/name_f_13 nested_dir_v3.cap
71
72 # Find all packets for file, starting with a directory file handle:
73 %prog -p DH:0x0c35bb58/name_f_13 nested_dir_v3.cap
74
75 # Find all packets for file handle
76 %prog -p FH:0xc3f001b4 /tmp/trace.cap
77
78 # Find all packets for file, including all operations for the given state id
79 %prog -p f00000001 --stid 0x0fd4 /tmp/trace.cap
80
81 # Display all packets for file (one line per layer)
82 %prog -p f00000001 -v 2 /tmp/trace.cap
83
84 # Display all packets for file
85 # (real verbose, all items in each layer are displayed)
86 %prog -p f00000001 -v 4 /tmp/trace.cap
87
88 # Display all packets for file (display both verbose level 1 and 2)
89 %prog -p f00000001 -v 3 /tmp/trace.cap
90
91 # Display packets for file between packets 100 through 199
92 $ %prog -p f00000001 -s 100 -e 200 /tmp/trace.cap
93
94 # Display all packets truncating all strings to 100 bytes
95 # This is useful when some packets are very large and there
96 # is no need to display all the data
97 $ %prog -p f00000001 --strsize 100 -v 2 /tmp/trace.cap
98
99 # Display packets using India time zone
100 $ %prog -p f00000001 --tz "UTC-5:30" /tmp/trace.cap
101 $ %prog -p f00000001 --tz "Asia/Kolkata" /tmp/trace.cap
102
103 # Display all packets for file found in all trace files given
104 # The packets are displayed in order using their timestamps
105 $ %prog -p f00000001 trace1.cap trace2.cap trace3.cap"""
106
107 # Command line options
108 opts = OptionParser(USAGE, formatter = IndentedHelpFormatter(2, 25), version = "%prog " + __version__)
109 hhelp = "Path relative to the mount point, the path can be specified by " + \
110 "its file handle 'FH:0xc3f001b4'. Also the relative path could " + \
111 "start with a directory file handle 'DH:0x0c35bb58/file_name'"
112 opts.add_option("-p", "--path", default=None, help=hhelp)
113 hhelp = "State id to include in the search"
114 opts.add_option("--stid", default=None, help=hhelp)
115
116 vhelp = "Verbose level bitmask [default: %default]. "
117 vhelp += " bitmap 0x01: one line per packet. "
118 vhelp += " bitmap 0x02: one line per layer. "
119 vhelp += " bitmap 0x04: real verbose. "
120 opts.add_option("-v", "--verbose", type="int", default=1, help=vhelp)
121 shelp = "Start index [default: %default]"
122 opts.add_option("-s", "--start", type="int", default=0, help=shelp)
123 ehelp = "End index [default: %default]"
124 opts.add_option("-e", "--end", type="int", default=0, help=ehelp)
125 hhelp = "Time zone to use to display timestamps"
126 opts.add_option("-z", "--tz", default=None, help=hhelp)
127 hhelp = "Display progress bar [default: %default]"
128 opts.add_option("--progress", type="int", default=1, help=hhelp)
129
130 # Hidden options
131 opts.add_option("--list--options", action="store_true", default=False, help=SUPPRESS_HELP)
132
133 pktdisp = OptionGroup(opts, "Packet display")
134 hhelp = "Display record frame number [default: %default]"
135 pktdisp.add_option("--frame", default=str(record.FRAME), help=hhelp)
136 hhelp = "Display packet number [default: %default]"
137 pktdisp.add_option("--index", default=str(record.INDEX), help=hhelp)
138 hhelp = "Display CRC16 encoded strings [default: %default]"
139 pktdisp.add_option("--crc16", default=str(formatstr.CRC16), help=hhelp)
140 hhelp = "Display CRC32 encoded strings [default: %default]"
141 pktdisp.add_option("--crc32", default=str(formatstr.CRC32), help=hhelp)
142 hhelp = "Truncate all strings to this size [default: %default]"
143 pktdisp.add_option("--strsize", type="int", default=0, help=hhelp)
144 opts.add_option_group(pktdisp)
145
146 debug = OptionGroup(opts, "Debug")
147 hhelp = "If set to True, enums are strictly enforced [default: %default]"
148 debug.add_option("--enum-check", default=str(utils.ENUM_CHECK), help=hhelp)
149 hhelp = "If set to True, enums are displayed as numbers [default: %default]"
150 debug.add_option("--enum-repr", default=str(utils.ENUM_REPR), help=hhelp)
151 hhelp = "Set debug level messages"
152 debug.add_option("--debug-level", default="", help=hhelp)
153 opts.add_option_group(debug)
154
155 # Run parse_args to get options
156 vopts, args = opts.parse_args()
157
158 if vopts.list__options:
159 hidden_opts = ("--list--options",)
160 long_opts = [x for x in opts._long_opt.keys() if x not in hidden_opts]
161 print("\n".join(list(opts._short_opt.keys()) + long_opts))
162 sys.exit(0)
163
164 if vopts.tz is not None:
165 os.environ["TZ"] = vopts.tz
166
167 if vopts.path is None:
168 opts.error("No relative path is given")
169 if len(args) < 1:
170 opts.error("No packet trace file!")
171
172 def atoi(text):
173 """Convert string to integer or just return the string if it
174 does not represent an integer
175 """
176 return int(text) if text.isdigit() else text
177
178 def natural_keys(text):
179 """Natural sorting function"""
180 return [ atoi(c) for c in re.split('(\d+)', text) ]
181
182 def display_pkt(vlevel, pkttobj, pkt):
183 """Display packet for given verbose level"""
184 if not vopts.verbose & vlevel:
185 return
186 level = 2
187 if vlevel == 0x01:
188 level = 1
189 pkttobj.debug_repr(level)
190
191 disp = str
192 if vlevel == 0x04:
193 disp = repr
194
195 print(disp(pkt))
196
197 def print_pkt(pkttobj, pkt):
198 """Display packet for all verbose levels specified in the verbose option"""
199 if vopts.verbose & 0x01:
200 display_pkt(0x01, pkttobj, pkt)
201 if vopts.verbose & 0x02:
202 display_pkt(0x02, pkttobj, pkt)
203 if vopts.verbose & 0x04:
204 display_pkt(0x04, pkttobj, pkt)
205
206 record.FRAME = eval(vopts.frame)
207 record.INDEX = eval(vopts.index)
208 formatstr.CRC16 = eval(vopts.crc16)
209 formatstr.CRC32 = eval(vopts.crc32)
210 utils.ENUM_CHECK = eval(vopts.enum_check)
211
212 dirfh = None
213 dirfhcrc32 = None
214 idirfh = None
215 idirfhcrc32 = None
216
217 if os.path.isdir(args[0]):
218 files = [os.path.join(sys.argv[1], x) for x in os.listdir(sys.argv[1])]
219 else:
220 files = args
221
222 relpath = vopts.path
223 paths = relpath.split("/")
224 fname = paths.pop()
225 files.sort(key=natural_keys)
226
227 if len(paths) and paths[0][:3] == "DH:":
228 value = eval(paths.pop(0)[3:])
229 if len(paths) == 0:
230 dirfhcrc32 = value
231 else:
232 idirfhcrc32 = value
233
234 paths_c = list(paths)
235 dir_paths = []
236 fh_list = []
237 stid_list = []
238 pkttobj = None
239
240 if vopts.stid is not None:
241 stid_list.append(eval(vopts.stid))
242
243 if fname[:3] == "FH:":
244 fh_list = [eval(fname[3:])]
245 filestr = ""
246
247 ################################################################################
248 # Entry point
249 stime = time.time()
250 pkttobj = Pktt(files)
251 pkttobj.showprog = vopts.progress
252
253 maxindex = None
254 if vopts.end > 0:
255 maxindex = vopts.end
256 if vopts.start > 1:
257 pkttobj[vopts.start - 1]
258 if vopts.strsize > 0:
259 pkttobj.strsize(vopts.strsize)
260 if len(vopts.debug_level):
261 pkttobj.debug_level(vopts.debug_level)
262
263 if dirfhcrc32 is None:
264 # Search for file handle of directory where file is created
265 while len(paths_c):
266 path = paths_c[0]
267 if idirfhcrc32 is None:
268 dirmatch = ""
269 else:
270 dirmatch = " and crc32(nfs.fh) == %d" % idirfhcrc32
271 match_str = "nfs.name == '%s'%s" % (path, dirmatch)
272 while pkttobj.match(match_str, rewind=False, reply=True, maxindex=maxindex):
273 pkt = pkttobj.pkt
274 print_pkt(pkttobj, pkt)
275 if pkt.rpc.type == 1 and hasattr(pkt.nfs, "status") and pkt.nfs.status == 0:
276 # RPC reply
277 paths_c.pop(0)
278 dir_paths.append(path)
279 if pkt.rpc.version == 3:
280 idirfh = pkt.nfs.fh
281 idirfhcrc32 = crc32(idirfh)
282 else:
283 for item in pkt.nfs.array:
284 if item.resop == nfs4.OP_GETFH:
285 idirfh = item.fh
286 idirfhcrc32 = crc32(idirfh)
287 break
288 if len(paths_c) == 0:
289 # Last directory -- where file is created
290 dirfh = idirfh
291 dirfhcrc32 = idirfhcrc32
292 break
293 if pkttobj.pkt is None:
294 break
295
296 # Clear list of outstanding xids
297 pkttobj.clear_xid_list()
298 isnfsv4 = False
299
300 if not fh_list and (dirfhcrc32 is not None or len(paths) == 0):
301 # Search for file handle of file
302 if dirfhcrc32 is None:
303 filestr = "nfs.name == '%s'" % (fname)
304 else:
305 filestr = "(crc32(nfs.fh) == %d and nfs.name == '%s')" % (dirfhcrc32, fname)
306 while pkttobj.match(filestr, rewind=False, maxindex=maxindex):
307 pkt = pkttobj.pkt
308 if pkt:
309 print_pkt(pkttobj, pkt)
310 xid = pkt.rpc.xid
311 pkt = pkttobj.match("RPC.xid == %d" % xid, rewind=False, maxindex=maxindex)
312 if pkt:
313 print_pkt(pkttobj, pkt)
314 if pkt == "nfs" and hasattr(pkt.nfs, "status") and pkt.nfs.status == 0:
315 if pkt.rpc.version == 3:
316 fh_list.append(crc32(pkt.nfs.fh))
317 else:
318 isnfsv4 = True
319 for item in pkt.nfs.array:
320 if item.resop == nfs4.OP_OPEN:
321 stid_list.append(crc32(item.stateid.other))
322 if item.delegation.deleg_type in [nfs4.OPEN_DELEGATE_READ, nfs4.OPEN_DELEGATE_WRITE]:
323 stid_list.append(crc32(item.delegation.stateid.other))
324 elif item.resop == nfs4.OP_GETFH:
325 fh_list.append(crc32(item.fh))
326 break
327 break
328
329 teststid_xids = []
330 if fh_list:
331 # Look for all packets for given stateid and file handle
332 fhstr = " or ".join(["crc32(nfs.fh) == %d" % fh for fh in fh_list])
333 stidstr = " or ".join(["crc32(nfs.stateid.other) == %d" % stid for stid in stid_list])
334 nlmstr = " or ".join(["crc32(nlm.fh) == %d" % fh for fh in fh_list])
335 if isnfsv4:
336 opstr = "(rpc.version > 3 and nfs.op == %d)" % nfs4.OP_TEST_STATEID
337 else:
338 opstr = ""
339 mstr = " or ".join(filter(None, [stidstr, fhstr, nlmstr, filestr, opstr]))
340 while pkttobj.match(mstr, rewind=False, reply=True, maxindex=maxindex):
341 pkt = pkttobj.pkt
342 xid = pkt.rpc.xid
343 if pkt.rpc.type == 1:
344 if xid in teststid_xids:
345 # This reply should not be displayed
346 teststid_xids.remove(xid)
347 continue
348 if not pkttobj.reply_matched:
349 # Display pkt_call for matching replies which the call was never matched
350 print_pkt(pkttobj, pkttobj.pkt_call)
351 if pkt.rpc.version == 4:
352 for item in pkt.nfs.array:
353 if item.status != 0:
354 continue
355 if item.resop == nfs4.OP_OPEN:
356 stid_list.append(crc32(item.stateid.other))
357 if item.delegation.deleg_type in [nfs4.OPEN_DELEGATE_READ, nfs4.OPEN_DELEGATE_WRITE]:
358 stid_list.append(crc32(item.delegation.stateid.other))
359 elif item.resop == nfs4.OP_GETFH:
360 fh_list.append(crc32(item.fh))
361 break
362 elif item.resop == nfs4.OP_LOCK:
363 stid_list.append(crc32(item.stateid.other))
364 elif item.resop == nfs4.OP_LAYOUTGET:
365 stid_list.append(crc32(item.stateid.other))
366 for layout in item.layout:
367 fh_list.extend([crc32(fh) for fh in layout.content.body.fh_list])
368 else:
369 if pkt.rpc.version == 3:
370 if pkt.nfs.op == nfs3.NFSPROC3_RENAME:
371 filestr = "(crc32(nfs.fh) == %d and nfs.name == '%s')" % (crc32(pkt.nfs.fh), pkt.nfs.newname)
372 elif pkt.rpc.version == 4:
373 mflag = False
374 for item in pkt.nfs.array:
375 if item.op == nfs4.OP_RENAME:
376 filestr = "(crc32(nfs.fh) == %d and nfs.name == '%s')" % (crc32(item.fh), item.newname)
377 break
378 elif item.op == nfs4.OP_TEST_STATEID:
379 for stid in item.stateids:
380 if crc32(stid) not in stid_list:
381 # The matched TEST_STATEID does not have any stateids we are looking for
382 teststid_xids.append(xid)
383 mflag = True
384 break
385 if mflag:
386 continue
387 print_pkt(pkttobj, pkt)
388
389 # Make items in list unique
390 fh_list = list(set(fh_list))
391 stid_list = list(set(stid_list))
392
393 fhstr = " or ".join(["crc32(nfs.fh) == %d" % fh for fh in fh_list])
394 stidstr = " or ".join(["crc32(nfs.stateid.other) == %d" % stid for stid in stid_list])
395 nlmstr = " or ".join(["crc32(nlm.fh) == %d" % fh for fh in fh_list])
396 mstr = " or ".join(filter(None, [stidstr, fhstr, nlmstr, filestr, opstr]))
397
398 pkttobj.show_progress(True)
399 dtime = time.time() - stime
400 print("Duration: %d secs\n" % dtime)