Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.6.0.3
instru_example.py
1 /*! \file instru_example.py
2  */
3 
4 #!/mnt/fastpath/usr/bin/python
5 
6 """instru_example.py: OpEN API Instrumentation BST example"""
7 
8 import OpEN_py as OpEN
9 from OpENUtil import *
10 
11 open_ = OpENUtil()
12 
13 #
14 # Copyright 2016 Broadcom.
15 #
16 # Licensed under the Apache License, Version 2.0 (the "License")
17 # you may not use this file except in compliance with the License.
18 # You may obtain a copy of the License at
19 #
20 # http://www.apache.org/licenses/LICENSE-2.0
21 #
22 # Unless required by applicable law or agreed to in writing, software
23 # distributed under the License is distributed on an "AS IS" BASIS,
24 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 # See the License for the specific language governing permissions and
26 # limitations under the License.
27 #
28 
29 #
30 # Python 2.6.6
31 #
32 
33 # Define some constants for comparison convenience
34 
35 def print_sanity_results(result, test, msg, feat):
36  """Print overall comparision results"""
37 
38  if result == OpEN.OPEN_E_UNAVAIL:
39  print "Sanity test skipped."
40  elif result == OpEN.OPEN_E_NONE and test == True:
41  print "Sanity Success - %s - %s." % (msg, feat)
42  else:
43  print "Sanity Failure - %s - %s." % (msg, feat)
44 
45 def print_bad_result(result, msg):
46  """Print some general error messages if the result is bad"""
47 
48  if result == OpEN.OPEN_E_UNAVAIL:
49  print "Feature not supported - %s (err %d)." % (msg, result)
50  elif result != OpEN.OPEN_E_NONE:
51  print "Test Failure - %s (err %d)." % (msg, result)
52 
53 class InstruExample:
54  """Simple Instru Examples """
55 
56  def __init__(self, client, asic) :
57  self.m_client = client
58  self.m_asic = asic
59  self.m_ports = 0
60  self.m_ucq = 0
61  self.m_ucqg = 0
62  self.m_mcq = 0
63  self.m_spool = 0
64  self.m_cpuq = 0
65  self.m_rqe = 0
66  self.m_pg = 0
67 
68 
69  def show_asic_capability(self):
70  asic_cap_p = OpEN.new_OPEN_ASIC_CAPABILITIES_tp()
71  result=OpEN.openapiBstAsicCapabilityGet(self.m_client, self.m_asic, asic_cap_p)
72  if (result != OpEN.OPEN_E_NONE):
73  print "Error ! failed to get asic capability"
74  OpEN.delete_OPEN_ASIC_CAPABILITIES_tp(asic_cap_p)
75  return
76 
77  asic_cap = OpEN.OPEN_ASIC_CAPABILITIES_tp_value(asic_cap_p)
78  print "Asic Capability"
79  print "==============================================="
80  print "Asic numPorts - %d" % (asic_cap.numPorts)
81  print "Asic numUnicastQueues - %d" % (asic_cap.numUnicastQueues)
82  print "Asic numUnicastQueueGroups - %d" % (asic_cap.numUnicastQueueGroups)
83  print "Asic numMulticastQueues - %d" % (asic_cap.numMulticastQueues)
84  print "Asic numServicePools - %d" % (asic_cap.numServicePools)
85  print "Asic numCommonPools - %d" % (asic_cap.numCommonPools)
86  print "Asic numCpuQueues - %d" % (asic_cap.numCpuQueues)
87  print "Asic numRqeQueues - %d" % (asic_cap.numRqeQueues)
88  print "Asic numRqeQueuePools - %d" % (asic_cap.numRqeQueuePools)
89  print "Asic numPriorityGroups - %d" % (asic_cap.numPriorityGroups)
90 
91  self.m_ports = asic_cap.numPorts
92  self.m_ucq = asic_cap.numUnicastQueues
93  self.m_ucqg = asic_cap.numUnicastQueueGroups
94  self.m_mcq = asic_cap.numMulticastQueues
95  self.m_cpuq = asic_cap.numCpuQueues
96  self.m_rqe = asic_cap.numRqeQueues
97  self.m_spool = asic_cap.numServicePools
98  self.m_pg = asic_cap.numPriorityGroups
99 
100  OpEN.delete_OPEN_ASIC_CAPABILITIES_tp(asic_cap_p)
101 
102  def config_ipsp_threshold(self, spool, port, thre):
103  print ""
104  print "Setting IPSP Threshold"
105  print "========================================="
106  ipspThre_p = OpEN.new_OPEN_BST_INGRESS_PORT_SP_THRESHOLD_tp()
107  ipspThre = OpEN.OPEN_BST_INGRESS_PORT_SP_THRESHOLD_tp_value(ipspThre_p)
108 
109  ipspThre.umShareThreshold = thre;
110 
111  print "Setting threshold %d for service pool %d , port %d" % (thre, spool, port)
112 
113  OpEN.OPEN_BST_INGRESS_PORT_SP_THRESHOLD_tp_assign(ipspThre_p, ipspThre)
114  result=OpEN.openapiBstIpspThresholdSet(self.m_client, self.m_asic, port, spool, ipspThre_p)
115 
116  if (result != OpEN.OPEN_E_NONE) :
117  print "Error ! failed to set IPSP threshold"
118 
119  OpEN.delete_OPEN_BST_INGRESS_PORT_SP_THRESHOLD_tp(ipspThre_p)
120 
121  def show_ippg_stat(self):
122  """ Build all arguments to be passed """
123  ippg_p = OpEN.new_OPEN_BST_INGRESS_PORT_PG_DATA_tp()
124  time_p = OpEN.new_time_tp()
125  cnt_p = OpEN.new_uint32_tArray(2)
126 
127  result = OpEN.openapiBstIppgDataGet(self.m_client, self.m_asic, ippg_p, time_p)
128  if (result != OpEN.OPEN_E_NONE) :
129  print "Error ! failed to get IPPG stat"
130  else:
131  print "Showing IPPG Statistics"
132  print "==============================================="
133  for port in range(0, self.m_ports):
134 
135  for pg in range(0, self.m_pg):
136  print "Port %d, Pririty Group %d - " % (port + 1, pg),
137  OpEN.GetIPPGData(ippg_p, port, pg, cnt_p)
138  print "[%d , %d]" % (OpEN.uint32_tArray_getitem(cnt_p, 0),
139  OpEN.uint32_tArray_getitem(cnt_p, 1))
140 
141  OpEN.delete_time_tp(time_p)
142  OpEN.delete_uint32_tp(cnt_p)
143  OpEN.delete_OPEN_BST_INGRESS_PORT_PG_DATA_tp(ippg_p)
144 
145  def config_bst_feature(self, enable, mode):
146  print ""
147  print "Setting and Getting Bst feature"
148  print "========================================="
149  bstCfg_p = OpEN.new_OPEN_BST_CONFIG_tp()
150  bstCfg = OpEN.OPEN_BST_CONFIG_tp_value(bstCfg_p)
151  bstCfg.enableStatsMonitoring = enable
152  bstCfg.enableDeviceStatsMonitoring = 1
153  bstCfg.enableIngressStatsMonitoring = 1
154  bstCfg.enableEgressStatsMonitoring = 0
155  bstCfg.mode = mode
156 
157  print "Setting values and getting - enableStatsMonitoring - %d, mode - %d" % (bstCfg.enableStatsMonitoring, bstCfg.mode)
158  OpEN.OPEN_BST_CONFIG_tp_assign(bstCfg_p, bstCfg)
159  result=OpEN.openapiBstConfigSet(self.m_client, self.m_asic, bstCfg_p)
160 
161  if result == OpEN.OPEN_E_NONE :
162  #reset values to 0
163  bstCfg.enableStatsMonitoring = 0
164  bstCfg.mode = 0
165  OpEN.OPEN_BST_CONFIG_tp_assign(bstCfg_p, bstCfg)
166  result=OpEN.openapiBstConfigGet(self.m_client, self.m_asic, bstCfg_p)
167  bstCfg = OpEN.OPEN_BST_CONFIG_tp_value(bstCfg_p)
168  else:
169  print "Error ! failed to set bst feature"
170 
171 
172  print_sanity_results(result, ((bstCfg.enableStatsMonitoring == enable) and (bstCfg.mode == mode)) , "bst feature config ", "set and get")
173 
174  OpEN.delete_OPEN_BST_CONFIG_tp(bstCfg_p)
175 
176  def show_esp_stat(self):
177  """ Build all arguments to be passed """
178  esp_p = OpEN.new_OPEN_BST_EGRESS_SP_DATA_tp()
179  time_p = OpEN.new_time_tp()
180  cnt_p = OpEN.new_uint32_tArray(3)
181 
182  result = OpEN.openapiBstEspDataGet(self.m_client, self.m_asic, esp_p, time_p)
183  if (result != OpEN.OPEN_E_NONE) :
184  print "Error ! failed to get ESP stat"
185  else:
186  print ""
187  print "Showing ESP Statistics"
188  print "==============================================="
189  for sp in range(0, self.m_spool):
190  print "Service Pool %d - " % (sp),
191  OpEN.GetESPData(esp_p, sp, cnt_p)
192  print "[%d , %d, %d]" % (OpEN.uint32_tArray_getitem(cnt_p, 0),
193  OpEN.uint32_tArray_getitem(cnt_p, 1), OpEN.uint32_tArray_getitem(cnt_p, 2))
194 
195  OpEN.delete_time_tp(time_p)
196  OpEN.delete_uint32_tp(cnt_p)
197  OpEN.delete_OPEN_BST_EGRESS_SP_DATA_tp(esp_p)
198 
199  def show_ipsp_threshold(self):
200  """ Build all arguments to be passed """
201  snap_p = OpEN.new_OPEN_BST_ASIC_SNAPSHOT_DATA_tp()
202  ucq_p = OpEN.new_OPEN_BST_EGRESS_UC_QUEUE_DATA_tp()
203  mcq_p = OpEN.new_OPEN_BST_EGRESS_MC_QUEUE_DATA_tp()
204  time_p = OpEN.new_time_tp()
205  thre_p = OpEN.new_uint32_tp()
206 
207  """ Call OpEN API to get threshold values """
208  result = OpEN.openapiBstThresholdGet(self.m_client, self.m_asic, snap_p, ucq_p, mcq_p, time_p)
209  snap = OpEN.OPEN_BST_ASIC_SNAPSHOT_DATA_tp_value(snap_p)
210  ipsp_p = OpEN.copy_OPEN_BST_INGRESS_PORT_SP_DATA_tp(snap.iPortSp)
211 
212  if result != OpEN.OPEN_E_NONE :
213  print "Error ! failed to get IPSP threshold"
214  else:
215  print ""
216  print "Showing IPSP Threshold"
217  print "==============================================="
218  for port in range(0, self.m_ports):
219  print "Port %d - [" % (port + 1),
220  for sp in range(0, self.m_spool):
221  OpEN.GetIPSPData(ipsp_p, port, sp, thre_p)
222  thre = OpEN.uint32_tp_value(thre_p)
223  if sp > 0 :
224  print ", %d" % (thre),
225  else :
226  print "%d " % (thre),
227  print " ]"
228 
229  OpEN.delete_time_tp(time_p)
230  OpEN.delete_uint32_tp(thre_p)
231  OpEN.delete_OPEN_BST_ASIC_SNAPSHOT_DATA_tp(snap_p)
232  OpEN.delete_OPEN_BST_EGRESS_UC_QUEUE_DATA_tp(ucq_p)
233  OpEN.delete_OPEN_BST_EGRESS_MC_QUEUE_DATA_tp(mcq_p)
234  OpEN.delete_OPEN_BST_INGRESS_PORT_SP_DATA_tp(ipsp_p)
235 
236 def main():
237  """Demonstrate OpEN usage for BST APIs"""
238 
239  ret = open_.connect("instru_example")
240  if ret == OpEN.OPEN_E_NONE :
241  open_.getNetworkOSVersion()
242  client = open_.get_client()
243 
244  example = InstruExample(client, 0)
245  example.show_asic_capability()
246  example.config_bst_feature(1, OpEN.OPEN_BST_MODE_PEAK)
247  example.show_ippg_stat()
248  example.show_esp_stat()
249  example.show_ipsp_threshold()
250  example.config_ipsp_threshold(0, 5, 4160)
251  example.config_ipsp_threshold(0, 7, 2288)
252  example.config_ipsp_threshold(0, 8, 7696)
253  example.show_ipsp_threshold()
254 
255  open_.terminate()
256  else :
257  print "Unable to connect"
258 
259 if __name__ == '__main__': main()