gen_fe_cfg.py revision aa97dd1c
1#!/usr/bin/env python
2#
3# Copyright (c) 2016 Intel Corporation.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at:
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16#
17# generate FE config script
18#
19
20import sys, optparse, math
21
22def print_usage ():
23	print "Usage: " + sys.argv[0] + " [-f fe_lcore_list] [-b be_lcore_list]"
24	print "          [-p start_port] [-n number_of_streams]"
25	print "          [-m mode] [-q local_address] [-r remote_address]"
26	print "          [-s fwd_local_address] [-t fwd_remote_address]"
27	print "          [-l txlen] [-4/-6] [-H]"
28	print
29	print "Options"
30	print "   -f, --fe_lcore_list: list of lcores used for FE. Multiple " \
31			"lcores are comma-separated, within double quote"
32	print "   -b, --be_lcore_list: list of lcores used for BE. Multiple " \
33			"lcores are comma-separated, within double quote"
34	print "   -p, --start_port: starting TCP/UDP port number"
35	print "   -n, --number_of_streams: number of streams to be generated"
36	print "   -m, --mode: mode of the application. [echo, rx, tx, fwd]"
37	print "   -q, --local_address: local address of the stream"
38	print "   -r, --remote_address: remote address of the stream"
39	print "   -s, --fwd_local_address: forwarding local address of the stream"
40	print "   -t, --fwd_remote_address: forwarding remote address of the " \
41			"stream"
42	print "   -l, --txlen: transmission length for rx/tx mode"
43	print "   -H: if port number to printed in hex format"
44	print "   -4/-6: if default ip addresses to be printed in ipv4/ipv6 format"
45
46def align_power_of_2(x):
47	return 2**(x-1).bit_length()
48
49def print_stream(mode, la, ra, fwd_la, fwd_ra, lcore, belcore, lport,
50		fwrport, txlen):
51	if support_hex != 0:
52		lport_str = str(format(lport, '#x'))
53		fwrport_str = str(format(fwrport, '#x'))
54	else:
55		lport_str = str(lport)
56		fwrport_str = str(fwrport)
57
58	stream = "lcore=" + str(lcore) + ",belcore=" + str(belcore)
59	stream += ",op=" + mode
60	stream += ",laddr=" + la + ",lport=" + lport_str
61	stream += ",raddr=" + ra + ",rport=0"
62
63	if mode == 'fwd':
64		stream += ",fwladdr=" + fwd_la + ",fwlport=0,fwraddr=" + fwd_ra
65		stream += ",fwrport=" + fwrport_str
66
67	if mode == 'rx' or mode == 'tx':
68		stream += ",txlen=" + str(txlen)
69
70	print stream
71
72parser = optparse.OptionParser()
73parser.add_option("-b", "--be_lcore_list", dest = "be_lcore_list",
74	help = "BE lcore lists.")
75parser.add_option("-f", "--fe_lcore_list", dest = "fe_lcore_list",
76	help = "FE lcore lists.")
77parser.add_option("-p", "--start_port", dest = "start_port",
78	help = "start port.")
79parser.add_option("-n", "--number_of_streams", dest = "number_of_streams",
80	help = "number of streams.")
81parser.add_option("-m", "--mode", dest = "mode",
82	help = "mode (op, rx, tx, fwd).")
83parser.add_option("-q", "--local_address", dest = "local_address",
84	help = "local_address.")
85parser.add_option("-r", "--remote_address", dest = "remote_address",
86	help = "remote_address.")
87parser.add_option("-s", "--fwd_local_address", dest = "fwd_local_address",
88	help = "fwd_local_address.")
89parser.add_option("-t", "--fwd_remote_address", dest = "fwd_remote_address",
90	help = "fwd_remote_address.")
91parser.add_option("-l", "--txlen", dest = "txlen", help = "txlen.")
92parser.add_option("-4", action = "store_false", dest = "ipv6",
93	help = "IPv4/IPv6")
94parser.add_option("-6", action = "store_true", dest = "ipv6",
95	help = "IPv4/IPv6")
96parser.add_option("-H", action = "store_true", dest = "support_hex",
97	help = "print ports in hexa format")
98
99(options, args) = parser.parse_args()
100
101if len(sys.argv) == 1:
102	print
103	print_usage()
104	print
105	sys.exit()
106
107supported_modes = ['echo', 'rx', 'tx', 'fwd']
108support_hex = 0
109txlen = 72
110
111if options.ipv6 == True:
112	la = '::'
113	ra = '::'
114	fwd_la = '::'
115	fwd_ra = '::'
116else:
117	la = '0.0.0.0'
118	ra = '0.0.0.0'
119	fwd_la = '0.0.0.0'
120	fwd_ra = '0.0.0.0'
121
122if options.support_hex == True:
123	support_hex = 1
124
125if options.txlen != None:
126	txlen = options.txlen
127
128if options.fe_lcore_list != None:
129	felcore_list = list(map(int, options.fe_lcore_list.split(",")))
130	felcore_list.sort()
131else:
132	felcore_list = []
133
134if options.be_lcore_list != None:
135	belcore_list = list(map(int, options.be_lcore_list.split(",")))
136	belcore_list.sort()
137else:
138	print "default BE lcore list = [ 1 ]"
139	belcore_list = [1]
140
141if options.mode != None:
142	mode = options.mode
143	if mode not in supported_modes:
144		print "Supported modes: " + str(supported_modes)
145		print "Provided mode \"" + mode + "\" is not supported. Terminating..."
146		sys.exit()
147else:
148	print "default mode = echo"
149	mode = "echo"
150
151if options.start_port != None:
152	start_port = int(options.start_port)
153else:
154	print "default start_port = 32768"
155	start_port = 32768
156
157if options.number_of_streams != None:
158	number_of_streams = int(options.number_of_streams)
159else:
160	print "default number_of_streams = 1"
161	number_of_streams = 1
162
163fwd_start_port = 53248
164
165if options.local_address != None:
166	la = options.local_address
167
168if options.remote_address != None:
169	ra = options.remote_address
170
171if options.fwd_local_address != None:
172	fwd_la = options.fwd_local_address
173
174if options.fwd_remote_address != None:
175	fwd_ra = options.fwd_remote_address
176
177belcore_count = len(belcore_list)
178align_belcore_count = align_power_of_2(belcore_count)
179nb_bits = int(math.log(align_belcore_count, 2))
180
181felcore_count = len(felcore_list)
182if felcore_count != 0:
183	if number_of_streams % felcore_count == 0:
184		nb_stream_per_flc = number_of_streams / felcore_count
185	else:
186		nb_stream_per_flc = (number_of_streams / felcore_count) + 1
187
188for i in range(start_port, start_port + number_of_streams):
189	k = i - start_port
190	align_belcore_count = align_power_of_2(belcore_count)
191	blc_indx = (i % align_belcore_count) % belcore_count
192	belcore = belcore_list[blc_indx]
193	if felcore_count != 0:
194		flc_indx = k / nb_stream_per_flc
195		felcore = felcore_list[flc_indx]
196	else:
197		felcore = belcore
198
199	print_stream(mode, la, ra, fwd_la, fwd_ra, felcore, belcore, i,
200		fwd_start_port + k, txlen)
201