| 1 |
#!/usr/bin/env python3 |
| 2 |
#=============================================================================== |
| 3 |
# Copyright 2015 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 sys |
| 16 |
import packet.utils as utils |
| 17 |
from packet.pktt import Pktt |
| 18 |
import packet.unpack as unpack |
| 19 |
from optparse import OptionParser,OptionGroup,IndentedHelpFormatter,SUPPRESS_HELP |
| 20 |
|
| 21 |
# Module constants |
| 22 |
__author__ = "Jorge Mora ([email protected])" |
| 23 |
__copyright__ = "Copyright (C) 2015 NetApp, Inc." |
| 24 |
__license__ = "GPL v2" |
| 25 |
__version__ = "1.0" |
| 26 |
|
| 27 |
USAGE = """%prog <trace1> [<trace2> ...] |
| 28 |
|
| 29 |
Verify packets are matched correctly by their XID |
| 30 |
================================================= |
| 31 |
Search all the packet traces given for XID inconsistencies. Verify all |
| 32 |
operations in the NFSv4.x COMPOUND reply are the same as the operations |
| 33 |
given in the call. |
| 34 |
|
| 35 |
Examples: |
| 36 |
$ %prog /tmp/trace1.pcap |
| 37 |
|
| 38 |
Notes: |
| 39 |
Valid for packet traces with NFSv4 and above""" |
| 40 |
|
| 41 |
# Defaults |
| 42 |
utils.LOAD_body = False |
| 43 |
|
| 44 |
# Command line options |
| 45 |
opts = OptionParser(USAGE, formatter = IndentedHelpFormatter(2, 16), version = "%prog " + __version__) |
| 46 |
|
| 47 |
# Hidden options |
| 48 |
opts.add_option("--list--options", action="store_true", default=False, help=SUPPRESS_HELP) |
| 49 |
|
| 50 |
pktdisp = OptionGroup(opts, "Packet display") |
| 51 |
hhelp = "Display RPC payload body [default: %default]" |
| 52 |
pktdisp.add_option("--load-body", default=str(utils.LOAD_body), help=hhelp) |
| 53 |
opts.add_option_group(pktdisp) |
| 54 |
|
| 55 |
debug = OptionGroup(opts, "Debug") |
| 56 |
hhelp = "If set to True, enums are strictly enforced [default: %default]" |
| 57 |
debug.add_option("--enum-check", default=str(utils.ENUM_CHECK), help=hhelp) |
| 58 |
hhelp = "Set debug level messages" |
| 59 |
debug.add_option("--debug-level", default="", help=hhelp) |
| 60 |
hhelp = "Raise unpack error when True" |
| 61 |
debug.add_option("--unpack-error", default=str(unpack.UNPACK_ERROR), help=hhelp) |
| 62 |
hhelp = "Exit on first error" |
| 63 |
debug.add_option("--error", action="store_true", default=False, help=hhelp) |
| 64 |
opts.add_option_group(debug) |
| 65 |
|
| 66 |
# Run parse_args to get options |
| 67 |
vopts, args = opts.parse_args() |
| 68 |
|
| 69 |
if vopts.list__options: |
| 70 |
hidden_opts = ("--list--options",) |
| 71 |
long_opts = [x for x in opts._long_opt.keys() if x not in hidden_opts] |
| 72 |
print("\n".join(list(opts._short_opt.keys()) + long_opts)) |
| 73 |
sys.exit(0) |
| 74 |
|
| 75 |
if len(args) < 1: |
| 76 |
opts.error("No packet trace file!") |
| 77 |
|
| 78 |
utils.LOAD_body = eval(vopts.load_body) |
| 79 |
utils.ENUM_CHECK = eval(vopts.enum_check) |
| 80 |
unpack.UNPACK_ERROR = eval(vopts.unpack_error) |
| 81 |
|
| 82 |
# Process all trace files |
| 83 |
for pfile in args: |
| 84 |
print(pfile) |
| 85 |
pkttobj = Pktt(pfile) |
| 86 |
if len(vopts.debug_level): |
| 87 |
pkttobj.debug_level(vopts.debug_level) |
| 88 |
while pkttobj.match("rpc.type == 1 and rpc.version > 3"): |
| 89 |
try: |
| 90 |
pkt_call = pkttobj.pkt_call |
| 91 |
pkt_reply = pkttobj.pkt |
| 92 |
if pkt_call is None or pkt_call.rpc.xid != pkt_reply.rpc.xid: |
| 93 |
# Do not process if there is no packet call or the xids don't match |
| 94 |
continue |
| 95 |
if pkt_call != "nfs" or pkt_reply != "nfs" or not hasattr(pkt_call.nfs, "array") or not hasattr(pkt_reply.nfs, "array"): |
| 96 |
# Do not process if no NFS layer or is not a COMPOUND |
| 97 |
continue |
| 98 |
|
| 99 |
idx = 0 |
| 100 |
nfs = pkt_reply.nfs |
| 101 |
array = pkt_call.nfs.array |
| 102 |
|
| 103 |
# Find out if packets have been truncated |
| 104 |
tlist = [] |
| 105 |
if pkt_call.record.length_orig > pkt_call.record.length_inc: |
| 106 |
tlist.append("truncated call") |
| 107 |
if pkt_reply.record.length_orig > pkt_reply.record.length_inc: |
| 108 |
tlist.append("truncated reply") |
| 109 |
strtrunc = "" |
| 110 |
if tlist: |
| 111 |
strtrunc = " (%s)" % ", ".join(tlist) |
| 112 |
|
| 113 |
# Check if array sizes don't match only when status is OK |
| 114 |
# -- an error will have the reply shorter than the call |
| 115 |
expr = nfs.status == 0 and len(array) != len(nfs.array) |
| 116 |
# Verify the list of operations are the same |
| 117 |
for item in pkt_reply.nfs.array: |
| 118 |
# Reply array is longer than call array |
| 119 |
# This also avoids an error with array[idx] |
| 120 |
expr = expr or idx >= len(array) |
| 121 |
# Operation mismatch between reply and call |
| 122 |
expr = expr or item.op != array[idx].op |
| 123 |
if expr: |
| 124 |
print(" >>> Operation lists do not match for xid:0x%08x%s" % (pkt_reply.rpc.xid, strtrunc)) |
| 125 |
print(" ", pkt_call) |
| 126 |
print(" ", pkt_reply) |
| 127 |
break |
| 128 |
idx += 1 |
| 129 |
except: |
| 130 |
if vopts.error: |
| 131 |
raise |