| 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 formatstr |
| 19 |
import traceback |
| 20 |
import packet.pkt |
| 21 |
import packet.utils as utils |
| 22 |
from packet.pktt import Pktt |
| 23 |
import packet.record as record |
| 24 |
from optparse import OptionParser,OptionGroup,IndentedHelpFormatter,SUPPRESS_HELP |
| 25 |
|
| 26 |
# Module constants |
| 27 |
__author__ = "Jorge Mora ([email protected])" |
| 28 |
__copyright__ = "Copyright (C) 2014 NetApp, Inc." |
| 29 |
__license__ = "GPL v2" |
| 30 |
__version__ = "1.4" |
| 31 |
|
| 32 |
USAGE = """%prog [options] <trace1.cap> [<trace2.cap> ...] |
| 33 |
|
| 34 |
Packet trace decoder |
| 35 |
==================== |
| 36 |
Decode and display all packets in the packet trace file(s) given. |
| 37 |
The match option gives the ability to search for specific packets within |
| 38 |
the packet trace file. Other options allow displaying of their corresponding |
| 39 |
call or reply when only one or the other is matched. Only a range of packets |
| 40 |
can be displayed if the start and/or end options are used. |
| 41 |
|
| 42 |
There are three levels of verbosity in which they are specified using |
| 43 |
a bitmap, where the most significant bit gives a more verbose output. |
| 44 |
Verbose level 1 is used as a default where each packet is displayed |
| 45 |
condensed to one line using the last layer of the packet as the main output. |
| 46 |
By default only the NFS packets (NFS, MOUNT, NLM, etc.) are displayed. |
| 47 |
|
| 48 |
The packet trace files are processed either serially or in parallel. |
| 49 |
The packets are displayed using their timestamps so they are always |
| 50 |
displayed in the correct order even if the files given are out of order. |
| 51 |
If the packet traces were captured one after the other the packets |
| 52 |
are displayed serially, first the packets of the first file according |
| 53 |
to their timestamps, then the second and so forth. If the packet traces |
| 54 |
were captured at the same time on multiple clients the packets are |
| 55 |
displayed in parallel, packets are interleaved from all the files when |
| 56 |
displayed again according to their timestamps. |
| 57 |
|
| 58 |
Note: |
| 59 |
When using the --call option, a packet call can be displayed out of order |
| 60 |
if the call is not matched explicitly but its reply is matched so its |
| 61 |
corresponding call is displayed right before the reply. |
| 62 |
|
| 63 |
Examples: |
| 64 |
# Display all NFS packets (one line per packet) |
| 65 |
# Display only the NFS packets by default. |
| 66 |
# Default for --verbose option is 1 -- one line per packet |
| 67 |
$ %prog /tmp/trace.cap |
| 68 |
|
| 69 |
# Display all NFS packets (one line per layer) |
| 70 |
$ %prog -v 2 /tmp/trace.cap |
| 71 |
|
| 72 |
# Display all NFS packets (real verbose, all items in each layer are displayed) |
| 73 |
$ %prog -v 4 /tmp/trace.cap |
| 74 |
|
| 75 |
# Display all NFS packets (display both verbose level 1 and 2) |
| 76 |
$ %prog -v 3 /tmp/trace.cap |
| 77 |
|
| 78 |
# Display all TCP packets (this will display all RPC and NFS packets as well) |
| 79 |
$ %prog -l tcp /tmp/trace.cap |
| 80 |
|
| 81 |
# Display all packets |
| 82 |
$ %prog -l all /tmp/trace.cap |
| 83 |
|
| 84 |
# Display all NFS, NLM, MOUNT and PORTMAP packets |
| 85 |
$ %prog -l nfs,nlm,mount,portmap /tmp/trace.cap |
| 86 |
|
| 87 |
# Display packets 100 through 199 |
| 88 |
$ %prog -s 100 -e 200 -l all /tmp/trace.cap |
| 89 |
|
| 90 |
# Display all NFS packets with non-zero status |
| 91 |
$ %prog -m "nfs.status != 0" /tmp/trace.cap |
| 92 |
|
| 93 |
# Display all NFSv4 WRITE packets |
| 94 |
$ %prog -m "rpc.version == 4 and nfs.op == 38" /tmp/trace.cap |
| 95 |
|
| 96 |
# Display all NFSv4 WRITE calls |
| 97 |
$ %prog -m "rpc.version == 4 and nfs.argop == 38" /tmp/trace.cap |
| 98 |
|
| 99 |
# Display all NFS packets having a file name as f00000001 (OPEN, LOOKUP, etc.) |
| 100 |
# including their replies |
| 101 |
$ %prog -r -m "nfs.name == 'f00000001'" /tmp/trace.cap |
| 102 |
|
| 103 |
# Display all NFS packets with non-zero status including their respective calls |
| 104 |
$ %prog -c -m "nfs.status != 0" /tmp/trace.cap |
| 105 |
$ %prog -d "pkt_call,pkt" -m "nfs.status != 0" /tmp/trace.cap |
| 106 |
|
| 107 |
# Display all TCP packets (just the TCP layer) |
| 108 |
$ %prog -d "pkt.tcp" -l tcp /tmp/trace.cap |
| 109 |
|
| 110 |
# Display all NFS file handles |
| 111 |
$ %prog -d "pkt.NFSop.fh" -m "len(nfs.fh) > 0" /tmp/trace.cap |
| 112 |
|
| 113 |
# Display all RPC packets including the record information (packet number, timestamp, etc.) |
| 114 |
# For verbose level 1 (default) the "," separator will be converted to a |
| 115 |
# space if all items are only pkt(.*)? or pkt_call(.*)? |
| 116 |
$ %prog -d "pkt.record,pkt.rpc" -l rpc /tmp/trace.cap |
| 117 |
|
| 118 |
# Display all RPC packets including the record information (packet number, timestamp, etc.) |
| 119 |
# For verbose level 2 the "," separator will be converted to a new line if |
| 120 |
# all items are only pkt(.*)? or pkt_call(.*)? |
| 121 |
$ %prog -v 2 -d "pkt.record,pkt.rpc" -l rpc /tmp/trace.cap |
| 122 |
|
| 123 |
# Display all RPC packets including the record information (packet number, timestamp, etc.) |
| 124 |
# using the given display format |
| 125 |
$ %prog -d ">>> record: pkt.record >>> rpc: pkt.rpc" -l rpc /tmp/trace.cap |
| 126 |
|
| 127 |
# Display all packets truncating all strings to 100 bytes |
| 128 |
# This is useful when some packets are very large and there |
| 129 |
# is no need to display all the data |
| 130 |
$ %prog --strsize 100 -v 2 -l all /tmp/trace.cap |
| 131 |
|
| 132 |
# Display all NFSv4 packets displaying the main operation of the compound |
| 133 |
# e.g., display "WRITE" instead of "SEQUENCE;PUTFH;WRITE" |
| 134 |
$ %prog --nfs-mainop 1 -l nfs /tmp/trace.cap |
| 135 |
|
| 136 |
# Have all CRC16 strings displayed as plain strings |
| 137 |
$ %prog --crc16 0 /tmp/trace.cap |
| 138 |
|
| 139 |
# Have all CRC32 strings displayed as plain strings |
| 140 |
# e.g., display unformatted file handles or state ids |
| 141 |
$ %prog --crc32 0 /tmp/trace.cap |
| 142 |
|
| 143 |
# Display packets using India time zone |
| 144 |
$ %prog --tz "UTC-5:30" /tmp/trace.cap |
| 145 |
$ %prog --tz "Asia/Kolkata" /tmp/trace.cap |
| 146 |
|
| 147 |
# Display all packets for all trace files given |
| 148 |
# The packets are displayed in order using their timestamps |
| 149 |
$ %prog trace1.cap trace2.cap trace3.cap""" |
| 150 |
|
| 151 |
# Command line options |
| 152 |
opts = OptionParser(USAGE, formatter = IndentedHelpFormatter(2, 25), version = "%prog " + __version__) |
| 153 |
vhelp = "Verbose level bitmask [default: %default]. " |
| 154 |
vhelp += " bitmap 0x01: one line per packet. " |
| 155 |
vhelp += " bitmap 0x02: one line per layer. " |
| 156 |
vhelp += " bitmap 0x04: real verbose. " |
| 157 |
opts.add_option("-v", "--verbose", type="int", default=1, help=vhelp) |
| 158 |
lhelp = "Layers to display [default: '%default']. " |
| 159 |
lhelp += "Valid layers: ethernet, ip, tcp, udp, rpc, nfs, nlm, mount, portmap" |
| 160 |
opts.add_option("-l", "--layers", default="rpc", help=lhelp) |
| 161 |
shelp = "Start index [default: %default]" |
| 162 |
opts.add_option("-s", "--start", type="int", default=0, help=shelp) |
| 163 |
ehelp = "End index [default: %default]" |
| 164 |
opts.add_option("-e", "--end", type="int", default=0, help=ehelp) |
| 165 |
mhelp = "Match string [default: %default]" |
| 166 |
opts.add_option("-m", "--match", default="True", help=mhelp) |
| 167 |
chelp = "If matching a reply packet, include its corresponding call in the output" |
| 168 |
opts.add_option("-c", "--call", action="store_true", default=False, help=chelp) |
| 169 |
rhelp = "If matching a call packet, include its corresponding reply in the output" |
| 170 |
opts.add_option("-r", "--reply", action="store_true", default=False, help=rhelp) |
| 171 |
dhelp = "Print specific packet or part of a packet [default: %default]" |
| 172 |
opts.add_option("-d", "--display", default="pkt", help=dhelp) |
| 173 |
hhelp = "Time zone to use to display timestamps" |
| 174 |
opts.add_option("-z", "--tz", default=None, help=hhelp) |
| 175 |
hhelp = "Process packet traces one after the other in the order in which they" |
| 176 |
hhelp += " are given. The default is to open all files first and then display" |
| 177 |
hhelp += " the packets ordered according to their timestamps." |
| 178 |
opts.add_option("--serial", action="store_true", default=False, help=hhelp) |
| 179 |
hhelp = "Display progress bar [default: %default]" |
| 180 |
opts.add_option("--progress", type="int", default=1, help=hhelp) |
| 181 |
|
| 182 |
# Hidden options |
| 183 |
opts.add_option("--list--options", action="store_true", default=False, help=SUPPRESS_HELP) |
| 184 |
opts.add_option("--list--pktlayers", action="store_true", default=False, help=SUPPRESS_HELP) |
| 185 |
|
| 186 |
rpcdisp = OptionGroup(opts, "RPC display") |
| 187 |
hhelp = "Display RPC type [default: %default]" |
| 188 |
rpcdisp.add_option("--rpc-type", default=str(utils.RPC_type), help=hhelp) |
| 189 |
hhelp = "Display RPC load type (NFS, NLM, etc.) [default: %default]" |
| 190 |
rpcdisp.add_option("--rpc-load", default=str(utils.RPC_load), help=hhelp) |
| 191 |
hhelp = "Display RPC load version [default: %default]" |
| 192 |
rpcdisp.add_option("--rpc-ver", default=str(utils.RPC_ver), help=hhelp) |
| 193 |
hhelp = "Display RPC xid [default: %default]" |
| 194 |
rpcdisp.add_option("--rpc-xid", default=str(utils.RPC_xid), help=hhelp) |
| 195 |
opts.add_option_group(rpcdisp) |
| 196 |
|
| 197 |
pktdisp = OptionGroup(opts, "Packet display") |
| 198 |
hhelp = "Display NFSv4 main operation only [default: %default]" |
| 199 |
pktdisp.add_option("--nfs-mainop", default=str(utils.NFS_mainop), help=hhelp) |
| 200 |
hhelp = "Display RPC payload body [default: %default]" |
| 201 |
pktdisp.add_option("--load-body", default=str(utils.LOAD_body), help=hhelp) |
| 202 |
hhelp = "Display record frame number [default: %default]" |
| 203 |
pktdisp.add_option("--frame", default=str(record.FRAME), help=hhelp) |
| 204 |
hhelp = "Display packet number [default: %default]" |
| 205 |
pktdisp.add_option("--index", default=str(record.INDEX), help=hhelp) |
| 206 |
hhelp = "Display CRC16 encoded strings [default: %default]" |
| 207 |
pktdisp.add_option("--crc16", default=str(formatstr.CRC16), help=hhelp) |
| 208 |
hhelp = "Display CRC32 encoded strings [default: %default]" |
| 209 |
pktdisp.add_option("--crc32", default=str(formatstr.CRC32), help=hhelp) |
| 210 |
hhelp = "Truncate all strings to this size [default: %default]" |
| 211 |
pktdisp.add_option("--strsize", type="int", default=0, help=hhelp) |
| 212 |
opts.add_option_group(pktdisp) |
| 213 |
|
| 214 |
debug = OptionGroup(opts, "Debug") |
| 215 |
hhelp = "If set to True, enums are strictly enforced [default: %default]" |
| 216 |
debug.add_option("--enum-check", default=str(utils.ENUM_CHECK), help=hhelp) |
| 217 |
hhelp = "If set to True, enums are displayed as numbers [default: %default]" |
| 218 |
debug.add_option("--enum-repr", default=str(utils.ENUM_REPR), help=hhelp) |
| 219 |
hhelp = "Do not dissect RPC replies" |
| 220 |
debug.add_option("--no-rpc-replies", action="store_true", default=False, help=hhelp) |
| 221 |
hhelp = "Set debug level messages" |
| 222 |
debug.add_option("--debug-level", default="", help=hhelp) |
| 223 |
opts.add_option_group(debug) |
| 224 |
|
| 225 |
# Run parse_args to get options |
| 226 |
vopts, args = opts.parse_args() |
| 227 |
|
| 228 |
if vopts.list__options: |
| 229 |
hidden_opts = ("--list--options", "--list--pktlayers") |
| 230 |
long_opts = [x for x in opts._long_opt.keys() if x not in hidden_opts] |
| 231 |
print("\n".join(list(opts._short_opt.keys()) + long_opts)) |
| 232 |
sys.exit(0) |
| 233 |
if vopts.list__pktlayers: |
| 234 |
print("\n".join(["all"] + packet.pkt.PKT_layers)) |
| 235 |
sys.exit(0) |
| 236 |
|
| 237 |
if len(args) < 1: |
| 238 |
opts.error("No packet trace file!") |
| 239 |
|
| 240 |
if vopts.tz is not None: |
| 241 |
os.environ["TZ"] = vopts.tz |
| 242 |
|
| 243 |
allpkts = False |
| 244 |
if vopts.layers == "all": |
| 245 |
allpkts = True |
| 246 |
layers = vopts.layers.split(",") |
| 247 |
|
| 248 |
utils.RPC_type = eval(vopts.rpc_type) |
| 249 |
utils.RPC_load = eval(vopts.rpc_load) |
| 250 |
utils.RPC_ver = eval(vopts.rpc_ver) |
| 251 |
utils.RPC_xid = eval(vopts.rpc_xid) |
| 252 |
utils.NFS_mainop = eval(vopts.nfs_mainop) |
| 253 |
utils.LOAD_body = eval(vopts.load_body) |
| 254 |
record.FRAME = eval(vopts.frame) |
| 255 |
record.INDEX = eval(vopts.index) |
| 256 |
utils.ENUM_CHECK = eval(vopts.enum_check) |
| 257 |
utils.ENUM_REPR = eval(vopts.enum_repr) |
| 258 |
formatstr.CRC16 = eval(vopts.crc16) |
| 259 |
formatstr.CRC32 = eval(vopts.crc32) |
| 260 |
|
| 261 |
# Do not dissect RPC replies if command line option is given |
| 262 |
rpc_replies = not vopts.no_rpc_replies |
| 263 |
|
| 264 |
if vopts.reply and vopts.no_rpc_replies: |
| 265 |
opts.error("Options --reply and --no-rpc-replies are mutually exclusive") |
| 266 |
if vopts.call and vopts.no_rpc_replies: |
| 267 |
opts.error("Options --call and --no-rpc-replies are mutually exclusive") |
| 268 |
|
| 269 |
if vopts.call: |
| 270 |
vopts.display = "pkt_call,pkt" |
| 271 |
|
| 272 |
# Process the --display option |
| 273 |
dlist = [] |
| 274 |
dobjonly = True |
| 275 |
dcommaonly = True |
| 276 |
if vopts.display != "pkt": |
| 277 |
data = re.sub(r'"', '\\"', vopts.display) |
| 278 |
data = eval('"' + data + '"') |
| 279 |
mlist = re.split(r"\b(pkt(_call)?\b(\.[\.\w]+)?)", data) |
| 280 |
if mlist[0] == "": |
| 281 |
mlist.pop(0) |
| 282 |
if mlist[-1] == "": |
| 283 |
mlist.pop() |
| 284 |
while mlist: |
| 285 |
item = mlist.pop(0) |
| 286 |
if re.search(r"^pkt(_call)?\b", item): |
| 287 |
dlist.append([item, 1]) |
| 288 |
# Remove extra matches from nested regex |
| 289 |
mlist.pop(0) |
| 290 |
mlist.pop(0) |
| 291 |
if item not in ["pkt", "pkt_call"]: |
| 292 |
dobjonly = False |
| 293 |
else: |
| 294 |
dlist.append([item, 0]) |
| 295 |
if item != ",": |
| 296 |
dcommaonly = False |
| 297 |
|
| 298 |
def display_pkt(vlevel, pkttobj): |
| 299 |
"""Display packet for given verbose level""" |
| 300 |
if not vopts.verbose & vlevel: |
| 301 |
return |
| 302 |
level = 2 |
| 303 |
if vlevel == 0x01: |
| 304 |
level = 1 |
| 305 |
pkttobj.debug_repr(level) |
| 306 |
pkt = pkttobj.pkt |
| 307 |
|
| 308 |
disp = str |
| 309 |
if vlevel == 0x04: |
| 310 |
disp = repr |
| 311 |
|
| 312 |
rpctype = 0 |
| 313 |
if pkt == "rpc": |
| 314 |
rpctype = pkt.rpc.type |
| 315 |
|
| 316 |
if dlist: |
| 317 |
slist = [] |
| 318 |
sep = "" |
| 319 |
if dcommaonly: |
| 320 |
if not dobjonly and vlevel == 0x01: |
| 321 |
sep = " " |
| 322 |
else: |
| 323 |
sep = "\n" |
| 324 |
for item in dlist: |
| 325 |
if (rpctype == 0 or pkttobj.reply_matched) and item[0] == "pkt_call": |
| 326 |
continue |
| 327 |
if item[1]: |
| 328 |
try: |
| 329 |
slist.append(disp(eval("pkttobj.%s" % item[0]))) |
| 330 |
except: |
| 331 |
pass |
| 332 |
elif not dcommaonly: |
| 333 |
slist.append(item[0]) |
| 334 |
out = sep.join(slist) |
| 335 |
else: |
| 336 |
out = disp(pkt) |
| 337 |
print(out) |
| 338 |
|
| 339 |
def display_packet(pkttobj): |
| 340 |
"""Display packet given the verbose level""" |
| 341 |
if allpkts or pkttobj.pkt in layers: |
| 342 |
for level in (0x01, 0x02, 0x04): |
| 343 |
display_pkt(level, pkttobj) |
| 344 |
|
| 345 |
################################################################################ |
| 346 |
# Entry point |
| 347 |
if vopts.serial: |
| 348 |
# Process each file at a time |
| 349 |
trace_files = args |
| 350 |
else: |
| 351 |
# Open all files at once and display packets according |
| 352 |
# to their timestamps |
| 353 |
trace_files = [args] |
| 354 |
|
| 355 |
for tfile in trace_files: |
| 356 |
if vopts.serial: |
| 357 |
print("Processing", tfile) |
| 358 |
pkttobj = Pktt(tfile, rpc_replies=rpc_replies) |
| 359 |
pkttobj.showprog = vopts.progress |
| 360 |
if vopts.start > 1: |
| 361 |
pkttobj[vopts.start - 1] |
| 362 |
if vopts.strsize > 0: |
| 363 |
pkttobj.strsize(vopts.strsize) |
| 364 |
if len(vopts.debug_level): |
| 365 |
pkttobj.debug_level(vopts.debug_level) |
| 366 |
|
| 367 |
maxindex = None |
| 368 |
if vopts.end > 0: |
| 369 |
maxindex = vopts.end |
| 370 |
|
| 371 |
if vopts.match == "True": |
| 372 |
# Do not use the match method, instead use the iterator method |
| 373 |
# which is about 36% faster than match |
| 374 |
try: |
| 375 |
for pkt in pkttobj: |
| 376 |
if maxindex is not None and pkt.record.index >= maxindex: |
| 377 |
break |
| 378 |
display_packet(pkttobj) |
| 379 |
except: |
| 380 |
print(traceback.format_exc()) |
| 381 |
else: |
| 382 |
while pkttobj.match(vopts.match, rewind=False, reply=vopts.reply, maxindex=maxindex): |
| 383 |
display_packet(pkttobj) |
| 384 |
|
| 385 |
pkttobj.show_progress(True) |
| 386 |
pkttobj.close() |