Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.11.1.2
fdb_example.py
1 /*! \file fdb_example.py
2  */
3 
4 import OpEN_py as OpEN
5 from OpENUtil import *
6 import sys
7 
8 open_ = OpENUtil()
9 
10 #
11 # Copyright 2012-2017 Broadcom
12 #
13 # Licensed under the Apache License, Version 2.0 (the "License");
14 # you may not use this file except in compliance with the License.
15 # You may obtain a copy of the License at
16 #
17 # http://www.apache.org/licenses/LICENSE-2.0
18 #
19 # Unless required by applicable law or agreed to in writing, software
20 # distributed under the License is distributed on an "AS IS" BASIS,
21 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 # See the License for the specific language governing permissions and
23 # limitations under the License.
24 #
25 
26 
27 #
28 # Python 2.6.6
29 #
30 
31 
32 class FdbExample :
33  def __init__(self, client):
34  self.clientHandle = client
35  self.m_valn_table = []
36 
37  def main(self):
38  try:
39  buf = open_.getByteBuffer(100, '')
40  except TypeError:
41  print("received a TypeError exception from getByteBuffer")
42  sys.exit(1)
43  except OpENBufferSizeError:
44  print("received an OpENBufferSizeError exception")
45  sys.exit(1)
46 
47  mac = OpEN.open_buffdesc()
48  mac.pstart = buf
49  mac.size = 100
50 
51  vlanId = OpEN.new_uint32_tp()
52  OpEN.uint32_tp_assign(vlanId, 1)
53 
54  fdbIfNum = OpEN.new_uint32_tp()
55  OpEN.uint32_tp_assign(fdbIfNum, 1)
56 
57  fdbEntryType = OpEN.new_OPEN_FDB_ADDR_FLAG_tp()
58  fdbEntryType = OpEN.copy_OPEN_FDB_ADDR_FLAG_tp(OpEN.OPEN_FDB_ADDR_FLAG_STATIC)
59 
60  fdbId = OpEN.new_uint32_tp()
61  OpEN.uint32_tp_assign(fdbId, 0)
62  value = OpEN.new_uint32_tp()
63  vlType = OpEN.new_OPEN_VL_TYPE_tp()
64  vlType = OpEN.copy_OPEN_VL_TYPE_tp(OpEN.OPEN_IVL_TYPE)
65 
66  while True:
67 
68  ''' Testing of the retrieving the first entry from the forwarding database '''
69  result = OpEN.openapiFdbEntryNextGet(self.clientHandle, mac, vlanId, fdbIfNum, fdbEntryType)
70  if result != OpEN.OPEN_E_NONE:
71  print("Failed to get the first entry from the forwarding database (Result = %d)\n" % result)
72  break
73  else:
74  print("Success: Get the first entry from the forwarding database with MAC: %s\n" % buf.cast())
75 
76  ''' Testing of the retrieving the next entry from the forwarding database '''
77  result = OpEN.openapiFdbEntryNextGet(self.clientHandle, mac, vlanId, fdbIfNum, fdbEntryType)
78  if result != OpEN.OPEN_E_NONE:
79  if OpEN.OPEN_E_NOT_FOUND != result:
80  print("Failed to get the next entry from the forwarding database (Result = %d)\n" % result)
81  break
82  print("Success: Cannot get the next entry from the forwarding database - entry not found (Result = %d)\n" % result)
83  else:
84  print("Success: Get the next entry from the forwarding database with MAC: %s\n" % buf.cast())
85 
86  ''' Testing of the verification is a FDB with the specified ID exists '''
87  result = OpEN.openapiFdbIdGet(self.clientHandle, OpEN.uint32_tp_value(fdbId))
88  if OpEN.OPEN_E_NONE == result:
89  print("Success: FDB with ID %d exists.\n" % OpEN.uint32_tp_value(fdbId))
90  elif OpEN.OPEN_E_NOT_FOUND == result:
91  print("Success: FDB with ID %d is not exists.\n" % OpEN.uint32_tp_value(fdbId))
92  else:
93  print("Failed to verify is FDB with ID %d exists. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbId), result))
94  break
95 
96  ''' Testing Fdb Address Aging Timeout - only if FDB exists '''
97  if OpEN.OPEN_E_NONE == result:
98  result = OpEN.openapiFdbAddrAgingTimeoutGet(self.clientHandle, OpEN.uint32_tp_value(fdbId), value)
99  if OpEN.OPEN_E_NONE == result:
100  print("Success: Fdb Address Aging Timeout is %d.\n" % (OpEN.uint32_tp_value(value)))
101  else:
102  print("Failed to get Fdb Address Aging Timeout for FDB with ID %d. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbId), result))
103 
104  ''' Testing of the Set Fdb Address Aging Timeout '''
105  OpEN.uint32_tp_assign(value, OpEN.uint32_tp_value(value) + 1)
106  result = OpEN.openapiFdbAddrAgingTimeoutSet(self.clientHandle, OpEN.uint32_tp_value(fdbId), OpEN.uint32_tp_value(value))
107  if OpEN.OPEN_E_NONE == result:
108  print("Success: Set Fdb Address Aging Timeout value: %d.\n" % (OpEN.uint32_tp_value(value)))
109  else:
110  print("Failed to set Fdb Address Aging Timeout value: %d. (Result = %d)\n" % (OpEN.uint32_tp_value(value), result))
111 
112  ''' Testing maximum number of entries that the FDB table can hold '''
113  result = OpEN.openapiFdbMaxTableEntriesGet(self.clientHandle, value)
114  if OpEN.OPEN_E_NONE == result:
115  print("Success: The maximum number of FDB entries supported on the platform: %d.\n" % (OpEN.uint32_tp_value(value)))
116  else:
117  print("Failed to get maximum number of FDB entries supported on the platform. (Result = %d)\n" % (result))
118 
119  ''' Testing of current number of entries in the FDB table '''
120  result = OpEN.openapiActiveAddrEntriesGet(self.clientHandle, value)
121  if OpEN.OPEN_E_NONE == result:
122  print("Success: The current number of FDB entries: %d.\n" % (OpEN.uint32_tp_value(value)))
123  else:
124  print("Failed to get the current number of FDB entries. (Result = %d)\n" % (result))
125 
126  ''' Testing of count maximum number of entries in the FDB table '''
127  result = OpEN.openapiFdbMaxCountGet(self.clientHandle, value)
128  if OpEN.OPEN_E_NONE == result:
129  print("Success: The count maximum number of FDB entries: %d.\n" % (OpEN.uint32_tp_value(value)))
130  else:
131  print("Failed to get the count maximum number of FDB entries. (Result = %d)\n" % (result))
132 
133  ''' Testing the reset of the greatest number of entries in the FDB table '''
134  result = OpEN.openapiMaxCountReset(self.clientHandle)
135  if OpEN.OPEN_E_NONE == result:
136  print("Success: The count greatest number of FDB entries is resetted.\n")
137  else:
138  print("Failed to reset the greatest number of FDB entries. (Result = %d)\n" % (result))
139 
140  ''' Testing the count of dynamic entries in the L2FDB '''
141  result = OpEN.openapiFdbDynamicEntriesGet(self.clientHandle, value)
142  if OpEN.OPEN_E_NONE == result:
143  print("Success: The count of dynamic entries in the L2FDB: %d.\n" % (OpEN.uint32_tp_value(value)))
144  else:
145  print("Failed to get the count of dynamic entries in the L2FDB. (Result = %d)\n" % (result))
146 
147  ''' Testing the count of internally added entries in the L2FDB '''
148  result = OpEN.openapiFdbInternalEntriesGet(self.clientHandle, value)
149  if OpEN.OPEN_E_NONE == result:
150  print("Success: The count of internally added entries in the L2FDB: %d.\n" % (OpEN.uint32_tp_value(value)))
151  else:
152  print("Failed to get the count of internally added entries in the L2FDB. (Result = %d)\n" % (result))
153 
154  ''' Testing of verification is specified MAC is restricted '''
155  result = OpEN.openapiIsRestrFilterMacGet(self.clientHandle, mac)
156  if OpEN.OPEN_E_NONE == result:
157  print("Success: The specified MAC is not restricted.\n")
158  elif OpEN.OPEN_E_DISABLED == result:
159  print("Success: The specified MAC is restricted.\n")
160  else:
161  print("Failed to verify is specified MAC is restricted. (Result = %d)\n" % (result))
162 
163  ''' Testing the retrieving of the supported VL '''
164  result = OpEN.openapiFdbTypeOfVLGet(self.clientHandle, vlType)
165  if OpEN.OPEN_E_NONE == result:
166  print("Success: Get the supported VLAN learning type: %d.\n" % (OpEN.OPEN_VL_TYPE_tp_value(vlType)))
167  else:
168  print("Failed to get the supported VL. (Result = %d)\n" % (result))
169 
170  ''' Testing the retrieving of the next fdbID starting from a certain index '''
171  result = OpEN.openapiFdbIdNextGet(self.clientHandle, OpEN.uint32_tp_value(fdbId), value)
172  if OpEN.OPEN_E_NONE == result:
173  print("Success: Get the next fdbID starting from a '%d' index.\n" % (OpEN.uint32_tp_value(fdbId)))
174  else:
175  print("Cannot get the next fdbID starting from a '%d' index. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbId), result))
176 
177  ''' Testing of retrieving the number of failed FDB insert attempts due to failure to allocate space '''
178  result = OpEN.openapiFdbBadAddsGet(self.clientHandle, value)
179  if OpEN.OPEN_E_NONE == result:
180  print("Success: Get the number of failed FDB insert: %d.\n" % (OpEN.uint32_tp_value(value)))
181  else:
182  print("Failed to get the number of failed FDB insert. (Result = %d)\n" % (result))
183 
184  ''' Testing of retrieving the max limit of MAC entries set in the hardware '''
185  result = OpEN.openapiFdbMacMaxHwLimitGet(self.clientHandle, value)
186  if OpEN.OPEN_E_NONE == result:
187  print("Success: Get the max limit of MAC entries set in the hardware: %d.\n" % (OpEN.uint32_tp_value(value)))
188  else:
189  print("Failed to get the max limit of MAC entries set in the hardware. (Result = %d)\n" % (result))
190 
191  ''' Testing of retrieving the value of dynamic entries in the L2FDB for a specific interface '''
192  result = OpEN.openapiFdbIntfDynamicEntriesGet(self.clientHandle, OpEN.uint32_tp_value(fdbIfNum), value)
193  if OpEN.OPEN_E_NONE == result:
194  print("Success: Get the value of dynamic entries in the L2FDB for a %d interface: %d.\n" % (OpEN.uint32_tp_value(fdbIfNum), OpEN.uint32_tp_value(value)))
195  else:
196  print("Failed to get the value of dynamic entries in the L2FDB for a %d interface. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbIfNum), result))
197 
198  ''' Testing of retrieving the value of dynamic entries in the L2FDB for a specific VLAN '''
199  result = OpEN.openapiFdbVlanDynamicEntriesGet(self.clientHandle, OpEN.uint32_tp_value(vlanId), value)
200  if OpEN.OPEN_E_NONE == result:
201  print("Success: Get the value of dynamic entries in the L2FDB for a VLAN %d: %d.\n" % (OpEN.uint32_tp_value(vlanId), OpEN.uint32_tp_value(value)))
202  else:
203  print("Failed to get the value of dynamic entries in the L2FDB for a VLAN %d. (Result = %d)\n" % (OpEN.uint32_tp_value(vlanId), result))
204 
205  ''' Testing of retrieving the value of internal entries in the L2FDB for a specific interface '''
206  result = OpEN.openapiFdbIntfInternalEntriesGet(self.clientHandle, OpEN.uint32_tp_value(fdbIfNum), value)
207  if OpEN.OPEN_E_NONE == result:
208  print("Success: Get the value of internal entries in the L2FDB for a %d interface: %d.\n" % (OpEN.uint32_tp_value(fdbIfNum), OpEN.uint32_tp_value(value)))
209  else:
210  print("Failed to get the value of internal entries in the L2FDB for a %d interface. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbIfNum), result))
211 
212  ''' Testing of retrieving the value of internal entries in the L2FDB for a specific VLAN '''
213  result = OpEN.openapiFdbVlanInternalEntriesGet(self.clientHandle, OpEN.uint32_tp_value(vlanId), value)
214  if OpEN.OPEN_E_NONE == result:
215  print("Success: Get the value of internal entries in the L2FDB for a VLAN %d: %d.\n" % (OpEN.uint32_tp_value(vlanId), OpEN.uint32_tp_value(value)))
216  else:
217  print("Failed to get the value of internal entries in the L2FDB for a VLAN %d. (Result = %d)\n" % (OpEN.uint32_tp_value(vlanId), result))
218 
219  ''' Testing of flushing all entries in fdb learned on the specified interface '''
220  result = OpEN.openapiFdbFlushByPort(self.clientHandle, OpEN.uint32_tp_value(fdbIfNum))
221  if OpEN.OPEN_E_NONE == result:
222  print("Success: Flushed all entries in fdb learned on the interface %d.\n" % (OpEN.uint32_tp_value(fdbIfNum)))
223  else:
224  print("Failed to flush all entries in fdb learned on the interface %d. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbIfNum), result))
225 
226  ''' Testing of flushing all entries in fdb learned on the specified VLAN '''
227  result = OpEN.openapiFdbFlushByVlan(self.clientHandle, OpEN.uint32_tp_value(vlanId))
228  if OpEN.OPEN_E_NONE == result:
229  print("Success: Flushed all entries in fdb learned on the VLAN %d.\n" % (OpEN.uint32_tp_value(vlanId)))
230  else:
231  print("Failed to flush all entries in fdb learned on the VLAN %d. (Result = %d)\n" % (OpEN.uint32_tp_value(vlanId), result))
232 
233  ''' Testing of flushing all entries in fdb learned on the specified MAC '''
234  result = OpEN.openapiFdbFlushByMac(self.clientHandle, mac)
235  if OpEN.OPEN_E_NONE == result:
236  print("Success: Flushed all entries in fdb learned on the MAC.\n")
237  else:
238  print("Failed to flush all entries in fdb learned on the MAC. (Result = %d)\n" % (result))
239 
240  ''' Testing of flushing all learned entries from the L2FDB '''
241  result = OpEN.openapiFdbFlush(self.clientHandle)
242  if OpEN.OPEN_E_NONE == result:
243  print("Success: Flushed all learned entries from the L2FDB.\n")
244  else:
245  print("Failed to flush all learned entries from the L2FDB. (Result = %d)\n" % (result))
246 
247  break
248 
249 
250 def main():
251  ret = open_.connect("fdb_example")
252  if ret == OpEN.OPEN_E_NONE:
253  open_.getNetworkOSVersion()
254  open_.getAPIVersion()
255  client = open_.get_client()
256 
257  fdbExample = FdbExample(client)
258  fdbExample.main()
259 
260  open_.terminate()
261  else:
262  print("Unable to connect")
263 
264 
265 if __name__ == '__main__': main()