Class: OVSImager::TcpDump
- Inherits:
-
Object
- Object
- OVSImager::TcpDump
- Defined in:
- lib/ovsimager/tcpdump.rb
Constant Summary collapse
- SIZE =
400
Instance Method Summary collapse
-
#initialize(ping = false, from = nil, to = nil) ⇒ TcpDump
constructor
A new instance of TcpDump.
- #test(ifaces) ⇒ Object
Constructor Details
#initialize(ping = false, from = nil, to = nil) ⇒ TcpDump
Returns a new instance of TcpDump.
6 7 8 9 10 11 |
# File 'lib/ovsimager/tcpdump.rb', line 6 def initialize(ping=false, from=nil, to=nil) throw 'must be root.' if Process::UID.eid != 0 @ping = ping @from = from @to = to end |
Instance Method Details
#test(ifaces) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ovsimager/tcpdump.rb', line 13 def test(ifaces) result = {} ping = nil if @ping puts "Sending ping from #{@from||'default'} to #{@to} ..." ping = IO.popen("ping -s #{SIZE} -c 15 #{@from?'-I '+@from:''} #{@to} >/dev/null", "r") end threads = ifaces.map do |(nn, iref)| Thread.new do iface = iref[:name] Thread.current[:iface] = iface ns = iref[:ns] nscmd = ns == :root ? '' : "ip netns exec #{ns} " dump = IO.popen("exec #{nscmd}tcpdump -v -l -n -nn -i #{iface} \\( icmp or udp port 4789 or proto gre \\) and greater #{SIZE} 2>&1", "r") puts dump.gets time_end = Time.now + 5 req_from = req_to = rep_from = rep_to = nil cap_from = cap_to = cap_type = cap_id = nil cap = false cap_line = 0 while (waitmax = time_end - Time.now) > 0 do rs, ws, = IO.select([dump], [], [], waitmax) break unless rs if r = rs[0] msg = r.gets break unless msg # puts msg if msg.match(/([\da-f\.:]+)\.\d+ > ([\da-f\.:]+)\.\d+: (VXLAN).*vni (\d+)/) || msg.match(/([\da-f\.:]+) > ([\da-f\.:]+): (GRE).*key=([\da-f]+)/) cap_from = $1 cap_to = $2 cap_type = $3 cap_id = $4 cap_line = 3 elsif msg.match(/length #{SIZE+8}/) && msg.match(/([\da-f\.:]+) > ([\da-f\.:]+): ICMP echo (request|reply)/) if $3 == 'request' req_from = $1 req_to = $2 else rep_from = $1 rep_to = $2 end if cap_line > 0 if $3 == 'request' cap = [cap_from, cap_to, cap_type, cap_id] else cap = [cap_to, cap_from, cap_type, cap_id] end end break if req_from && req_to && rep_from && rep_to end cap_line -= 1 if cap_line > 0 end end puts "Killing tcpdump(#{dump.pid}) on interface #{iface}." Process.kill('TERM', dump.pid) dump.close ifname_netns = iface + (ns == :root ? "" : ":" + ns.to_s) result[ifname_netns] = [req_from, req_to, rep_from, rep_to, {}] result[ifname_netns][4][:cap] = cap if cap end end threads.each {|th| th.join(10)} if @ping Process.kill('TERM', ping.pid) ping.close end return result end |