Introduce packet_data_generator3

packet_data_generator3 works as a UNIX domain socket server and
executes ovs-ofctl to collect flow_mod packets.

Signed-off-by: IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
IWAMOTO Toshihiro 2015-08-11 11:32:08 +09:00 committed by FUJITA Tomonori
parent 68ed177295
commit ea85625f63

View File

@ -0,0 +1,196 @@
#! /usr/bin/env python
import getopt
import os
import six
from six.moves import socketserver
import subprocess
import sys
import tempfile
import threading
from ryu.ofproto import ofproto_parser
from ryu.ofproto import ofproto_v1_0
from ryu.ofproto import ofproto_v1_0_parser
from ryu.ofproto import ofproto_v1_5
from ryu.ofproto import ofproto_v1_5_parser
from ryu.ofproto import ofproto_protocol
if six.PY3:
TimeoutExpired = subprocess.TimeoutExpired
else:
# As python2 doesn't have timeout for subprocess.call,
# this script may hang.
TimeoutExpired = None
STD_MATCH = [
'in_port=43981',
'dl_vlan=999',
'dl_dst=aa:bb:cc:99:88:77',
'dl_type=0x0800', # ETH_TYPE_IP
'nw_dst=192.168.2.1',
'tun_src=192.168.2.3',
'tun_dst=192.168.2.4',
'tun_id=50000']
MESSAGES = [
{'name': 'action_learn',
'versions': [4],
'cmd': 'add-flow',
'args': ['table=2',
'importance=39032'] + STD_MATCH + [
'actions=strip_vlan,mod_nw_dst:192.168.2.9,' +
'learn(table=99,priority=1,hard_timeout=300,' +
'OXM_OF_VLAN_VID[0..11],' +
'OXM_OF_ETH_DST[]=OXM_OF_ETH_SRC[],' +
'load:0->OXM_OF_VLAN_VID[],' +
'load:OXM_OF_TUNNEL_ID[]->OXM_OF_TUNNEL_ID[],' +
'output:OXM_OF_IN_PORT[]),goto_table:100']},
{'name': 'match_conj',
'versions': [4],
'cmd': 'mod-flows',
'args': ['table=3',
'cookie=0x123456789abcdef0/0xffffffffffffffff',
'dl_vlan=1234',
'conj_id=0xabcdef',
'actions=strip_vlan,goto_table:100']},
{'name': 'match_pkt_mark',
'versions': [4],
'cmd': 'mod-flows',
'args': ['table=3',
'cookie=0x123456789abcdef0/0xffffffffffffffff',
'dl_vlan=1234',
'pkt_mark=54321',
'actions=strip_vlan,goto_table:100']},
{'name': 'match_pkt_mark_masked',
'versions': [4],
'cmd': 'mod-flows',
'args': ['table=3',
'cookie=0x123456789abcdef0/0xffffffffffffffff',
'dl_vlan=1234',
'pkt_mark=0xd431/0xffff',
'actions=strip_vlan,goto_table:100']},
{'name': 'action_conjunction',
'versions': [4],
'cmd': 'mod-flows',
'args': (['table=2',
'cookie=0x123456789abcdef0/0xffffffffffffffff'] +
STD_MATCH +
['actions=conjunction(0xabcdef,1/2)'])},
{'name': 'match_move_nx_register',
'versions': [4],
'cmd': 'mod-flows',
'args': ['table=3',
'cookie=0x123456789abcdef0/0xffffffffffffffff',
'reg0=0x1234',
'reg5=0xabcd/0xffff',
'actions=move:NXM_NX_REG0[10..15]->NXM_NX_REG1[0..5]']},
{'name': 'action_resubmit',
'versions': [4],
'cmd': 'add-flow',
'args': (['table=3',
'importance=39032'] +
STD_MATCH +
['actions=resubmit(1234,99)'])},
]
buf = []
class MyHandler(socketserver.BaseRequestHandler):
verbose = False
def handle(self):
desc = ofproto_protocol.ProtocolDesc()
residue = b''
while True:
if residue:
data = residue
residue = b''
else:
data = self.request.recv(1024)
if data == b'':
break
if self.verbose:
print(data)
h = ofproto_parser.header(data)
if self.verbose:
print(h)
version, msg_type, msg_len, xid = h
residue = data[msg_len:]
desc.set_version(version=version)
if msg_type == desc.ofproto.OFPT_HELLO:
hello = desc.ofproto_parser.OFPHello(desc)
hello.serialize()
self.request.send(hello.buf)
elif msg_type == desc.ofproto.OFPT_FLOW_MOD:
buf.append(data[:msg_len])
elif msg_type == desc.ofproto.OFPT_BARRIER_REQUEST:
brep = desc.ofproto_parser.OFPBarrierReply(desc)
brep.xid = xid
brep.serialize()
self.request.send(brep.buf)
break
class MyVerboseHandler(MyHandler):
verbose = True
if __name__ == '__main__':
optlist, args = getopt.getopt(sys.argv[1:], 'dvo:')
debug = False
ofctl_cmd = '/usr/bin/ovs-ofctl'
verbose = False
for o, a in optlist:
if o == '-d':
debug = True
elif o == '-v':
verbose = True
elif o == '-o':
ofctl_cmd = a
if not os.access(ofctl_cmd, os.X_OK):
raise Exception("%s is not executable" % ofctl_cmd)
outpath = '../packet_data'
socketdir = tempfile.mkdtemp()
socketname = os.path.join(socketdir, 'ovs')
server = socketserver.UnixStreamServer(socketname,
MyVerboseHandler if verbose else
MyHandler)
if debug or verbose:
print("Serving at %s" % socketname)
for msg in MESSAGES:
for v in msg['versions']:
cmdargs = [ofctl_cmd, '-O', 'OpenFlow%2d' % (v + 9)]
if verbose:
cmdargs.append('-v')
cmdargs.append(msg['cmd'])
cmdargs.append('unix:%s' % socketname)
cmdargs.append('\n'.join(msg['args']))
if verbose:
print("Running cmd: " + ' '.join(cmdargs) + "\n")
t = threading.Thread(target=subprocess.call, args=[cmdargs],
kwargs={'timeout': 5})
t.start()
server.handle_request()
if debug:
print(buf.pop())
else:
outf = os.path.join(
outpath, "of%d" % (v + 9),
"ovs-ofctl-of%d-%s.packet" % (v + 9, msg['name']))
print("Writing %s..." % outf)
with open(outf, 'wb') as f:
f.write(buf.pop())
try:
t.join()
except TimeoutExpired as e:
print(e)
if debug:
while True:
server.handle_request()
print(buf.pop())
os.unlink(socketname)
os.rmdir(socketdir)