Merge "bugfix: Fix creation of vsperfenv in Ubuntu"
[vswitchperf.git] / tools / pkt_gen / trafficgen / trafficgenhelper.py
1 # Copyright 2015-2016 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Helper methods collection.
15
16 Collection of helper methods used by traffic generators
17 implementation.
18 """
19
20 from collections import namedtuple
21
22 CMD_PREFIX = 'gencmd : '
23 TRAFFIC_DEFAULTS = {
24     'traffic_type' : 'rfc2544',
25     'frame_rate' : 100,
26     'bidir' : 'False',  # will be passed as string in title format to tgen
27     'multistream' : 0,
28     'stream_type' : 'L4',
29     'pre_installed_flows' : 'No',           # used by vswitch implementation
30     'flow_type' : 'port',                   # used by vswitch implementation
31
32     'l2': {
33         'framesize': 64,
34         'srcmac': '00:00:00:00:00:00',
35         'dstmac': '00:00:00:00:00:00',
36     },
37     'l3': {
38         'proto': 'udp',
39         'srcip': '1.1.1.1',
40         'dstip': '90.90.90.90',
41     },
42     'l4': {
43         'srcport': 3000,
44         'dstport': 3001,
45     },
46     'vlan': {
47         'enabled': False,
48         'id': 0,
49         'priority': 0,
50         'cfi': 0,
51     },
52 }
53
54 #TODO remove namedtuples and implement results through IResult interface found
55 #in core/results
56
57 BurstResult = namedtuple(
58     'BurstResult',
59     'frames_tx frames_rx bytes_tx bytes_rx payload_err seq_err')
60 Back2BackResult = namedtuple(
61     'Back2BackResult',
62     'rx_fps rx_mbps tx_percent rx_percent tx_count b2b_frames '
63     'frame_loss_frames frame_loss_percent')
64
65
66 def merge_spec(orig, new):
67     """Merges ``new`` dict with ``orig`` dict, and return orig.
68
69     This takes into account nested dictionaries. Example:
70
71         >>> old = {'foo': 1, 'bar': {'foo': 2, 'bar': 3}}
72         >>> new = {'foo': 6, 'bar': {'foo': 7}}
73         >>> merge_spec(old, new)
74         {'foo': 6, 'bar': {'foo': 7, 'bar': 3}}
75
76     You'll notice that ``bar.bar`` is not removed. This is the desired result.
77     """
78     for key in orig:
79         if key not in new:
80             continue
81
82         # Not allowing derived dictionary types for now
83         # pylint: disable=unidiomatic-typecheck
84         if type(orig[key]) == dict:
85             orig[key] = merge_spec(orig[key], new[key])
86         else:
87             orig[key] = new[key]
88
89     for key in new:
90         if key not in orig:
91             orig[key] = new[key]
92
93     return orig
94