Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.6.0.3
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 2016 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  value = OpEN.new_uint32_tp()
62  vlType = OpEN.new_OPEN_VL_TYPE_tp()
63  vlType = OpEN.copy_OPEN_VL_TYPE_tp(OpEN.OPEN_IVL_TYPE)
64 
65  open_event = OpEN.openapiEventList_t()
66  OpEN.openapiEventListClear(open_event)
67 
68  result = OpEN.openapiEventRegister(self.clientHandle, open_event)
69  if result != OpEN.OPEN_E_NONE:
70  print "Failed to register for OPEN_EVENT_FDB events.\n"
71  return
72  else:
73  print "Successfully registered for OPEN_EVENT_FDB events.\n"
74 
75  while True:
76 
77  ''' Testing of the retrieving the first entry from the forwarding database '''
78  result = OpEN.openapiFdbEntryNextGet(self.clientHandle, mac, vlanId, fdbIfNum, fdbEntryType)
79  if result != OpEN.OPEN_E_NONE:
80  print "Failed to get the first entry from the forwarding database (Result = %d)\n" % result
81  break
82  else:
83  print "Success: Get the first entry from the forwarding database with MAC: %s\n" % buf.cast()
84 
85  ''' Testing of the retrieving the next entry from the forwarding database '''
86  result = OpEN.openapiFdbEntryNextGet(self.clientHandle, mac, vlanId, fdbIfNum, fdbEntryType)
87  if result != OpEN.OPEN_E_NONE:
88  if OpEN.OPEN_E_NOT_FOUND != result:
89  print "Failed to get the next entry from the forwarding database (Result = %d)\n" % result
90  break
91  print "Success: Cannot get the next entry from the forwarding database - entry not found (Result = %d)\n" % result
92  else:
93  print "Success: Get the next entry from the forwarding database with MAC: %s\n" % buf.cast()
94 
95  ''' Testing of the verification is a FDB with the specified ID exists '''
96  result = OpEN.openapiFdbIdGet(self.clientHandle, OpEN.uint32_tp_value(fdbId))
97  if OpEN.OPEN_E_NONE == result:
98  print "Success: FDB with ID %d exists.\n" % OpEN.uint32_tp_value(fdbId)
99  elif OpEN.OPEN_E_NOT_FOUND == result:
100  print "Success: FDB with ID %d is not exists.\n" % OpEN.uint32_tp_value(fdbId)
101  else:
102  print "Failed to verify is FDB with ID %d exists. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbId), result)
103  break
104 
105  ''' Testing Fdb Address Aging Timeout - only if FDB exists '''
106  if OpEN.OPEN_E_NONE == result:
107  result = OpEN.openapiFdbAddrAgingTimeoutGet(self.clientHandle, OpEN.uint32_tp_value(fdbId), value)
108  if OpEN.OPEN_E_NONE == result:
109  print "Success: Fdb Address Aging Timeout is %d.\n" % (OpEN.uint32_tp_value(value))
110  else:
111  print "Failed to get Fdb Address Aging Timeout for FDB with ID %d. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbId), result)
112 
113  ''' Testing of the Set Fdb Address Aging Timeout '''
114  OpEN.uint32_tp_assign(value, OpEN.uint32_tp_value(value) + 1)
115  OpEN.uint32_tp_assign(value, OpEN.uint32_tp_value(value) + 1)
116  result = OpEN.openapiFdbAddrAgingTimeoutSet(self.clientHandle, OpEN.uint32_tp_value(fdbId), value)
117  if OpEN.OPEN_E_NONE == result:
118  print "Success: Set Fdb Address Aging Timeout value: %d.\n" % (OpEN.uint32_tp_value(value))
119  else:
120  print "Failed to set Fdb Address Aging Timeout value: %d. (Result = %d)\n" % (OpEN.uint32_tp_value(value), result)
121 
122  ''' Testing maximum number of entries that the FDB table can hold '''
123  result = OpEN.openapiFdbMaxTableEntriesGet(self.clientHandle, value)
124  if OpEN.OPEN_E_NONE == result:
125  print "Success: The maximum number of FDB entries supported on the platform: %d.\n" % (OpEN.uint32_tp_value(value))
126  else:
127  print "Failed to get maximum number of FDB entries supported on the platform. (Result = %d)\n" % (result)
128 
129  ''' Testing of current number of entries in the FDB table '''
130  result = OpEN.openapiActiveAddrEntriesGet(self.clientHandle, value)
131  if OpEN.OPEN_E_NONE == result:
132  print "Success: The current number of FDB entries: %d.\n" % (value)
133  else:
134  print "Failed to get the current number of FDB entries. (Result = %d)\n" % (result)
135 
136  ''' Testing of count maximum number of entries in the FDB table '''
137  result = OpEN.openapiFdbMaxCountGet(self.clientHandle, value)
138  if OpEN.OPEN_E_NONE == result:
139  print "Success: The count maximum number of FDB entries: %d.\n" % (value)
140  else:
141  print "Failed to get the count maximum number of FDB entries. (Result = %d)\n" % (result)
142 
143  ''' Testing the reset of the greatest number of entries in the FDB table '''
144  result = OpEN.openapiMaxCountReset(self.clientHandle)
145  if OpEN.OPEN_E_NONE == result:
146  print "Success: The count greatest number of FDB entries is resetted.\n"
147  else:
148  print "Failed to reset the greatest number of FDB entries. (Result = %d)\n" % (result)
149 
150  ''' Testing the count of dynamic entries in the L2FDB '''
151  result = OpEN.openapiFdbDynamicEntriesGet(self.clientHandle, value)
152  if OpEN.OPEN_E_NONE == result:
153  print "Success: The count of dynamic entries in the L2FDB: %d.\n" % (value)
154  else:
155  print "Failed to get the count of dynamic entries in the L2FDB. (Result = %d)\n" % (result)
156 
157  ''' Testing the count of internally added entries in the L2FDB '''
158  result = OpEN.openapiFdbInternalEntriesGet(self.clientHandle, value)
159  if OpEN.OPEN_E_NONE == result:
160  print "Success: The count of internally added entries in the L2FDB: %d.\n" % (value)
161  else:
162  print "Failed to get the count of internally added entries in the L2FDB. (Result = %d)\n" % (result)
163 
164  ''' Testing of verification is specified MAC is restricted '''
165  result = OpEN.openapiIsRestrFilterMacGet(self.clientHandle, mac)
166  if OpEN.OPEN_E_NONE == result:
167  print "Success: The specified MAC is not restricted.\n"
168  elif OpEN.OPEN_E_DISABLED == result:
169  print "Success: The specified MAC is restricted.\n"
170  else:
171  print "Failed to verify is specified MAC is restricted. (Result = %d)\n" % (result)
172 
173  ''' Testing the retrieving next entry from the forwarding database based for the specified interface '''
174  result = OpEN.openapiFdbIntfEntryNextGet(self.clientHandle, mac, fdbIfNum, fdbEntryType)
175  if OpEN.OPEN_E_NONE == result:
176  print "Success: Get next entry from the forwarding database for the specified internal interface number: %d.\n" % (fdbIfNum)
177  else:
178  print "Failed to get next entry from the forwarding database for the specified interface. (Result = %d)\n" % (result)
179 
180  ''' Testing the retrieving next entry from the forwarding database based for the specified VLAN '''
181  result = OpEN.openapiFdbVlanEntryNextGet(self.clientHandle, mac, fdbIfNum, fdbEntryType, vlanId)
182  if OpEN.OPEN_E_NONE == result:
183  print "Success: Get next entry from the forwarding database for the specified VLAN.\n"
184  else:
185  print "Failed to get next entry from the forwarding database for the specified VLAN. (Result = %d)\n" % (result)
186 
187  ''' Testing the retrieving of the supported VL '''
188  result = OpEN.openapiFdbTypeOfVLGet(self.clientHandle, vlType)
189  if OpEN.OPEN_E_NONE == result:
190  print "Success: Get the supported VLAN learning type: %d.\n" % (vlType)
191  else:
192  print "Failed to get the supported VL. (Result = %d)\n" % (result)
193 
194  ''' Testing the retrieving of the next fdbID starting from a certain index '''
195  result = OpEN.openapiFdbIdNextGet(self.clientHandle, OpEN.uint32_tp_value(fdbId), value)
196  if OpEN.OPEN_E_NONE == result:
197  print "Success: Get the next fdbID starting from a '%d' index.\n" % (OpEN.uint32_tp_value(fdbId))
198  else:
199  print "Cannot get the next fdbID starting from a '%d' index. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbId), result)
200 
201  ''' Testing of retrieving the number of failed FDB insert attempts due to failure to allocate space '''
202  result = OpEN.openapiFdbBadAddsGet(self.clientHandle, value)
203  if OpEN.OPEN_E_NONE == result:
204  print "Success: Get the number of failed FDB insert: %d.\n" % (value)
205  else:
206  print "Failed to get the number of failed FDB insert. (Result = %d)\n" % (result)
207 
208  ''' Testing of retrieving the max limit of MAC entries set in the hardware '''
209  result = OpEN.openapiFdbBadAddsGet(self.clientHandle, value)
210  if OpEN.OPEN_E_NONE == result:
211  print "Success: Get the max limit of MAC entries set in the hardware: %d.\n" % (value)
212  else:
213  print "Failed to get the max limit of MAC entries set in the hardware. (Result = %d)\n" % (result)
214 
215  ''' Testing of retrieving the value of dynamic entries in the L2FDB for a specific interface '''
216  result = OpEN.openapiFdbIntfDynamicEntriesGet(self.clientHandle, OpEN.uint32_tp_value(fdbIfNum), value)
217  if OpEN.OPEN_E_NONE == result:
218  print "Success: Get the value of dynamic entries in the L2FDB for a %d interface: %d.\n" % (OpEN.uint32_tp_value(fdbIfNum), value)
219  else:
220  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)
221 
222  ''' Testing of retrieving the value of dynamic entries in the L2FDB for a specific VLAN '''
223  result = OpEN.openapiFdbVlanDynamicEntriesGet(self.clientHandle, OpEN.uint32_tp_value(vlanId), value)
224  if OpEN.OPEN_E_NONE == result:
225  print "Success: Get the value of dynamic entries in the L2FDB for a VLAN %d: %d.\n" % (OpEN.uint32_tp_value(vlanId), value)
226  else:
227  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)
228 
229  ''' Testing of retrieving the value of internal entries in the L2FDB for a specific interface '''
230  result = OpEN.openapiFdbIntfInternalEntriesGet(self.clientHandle, OpEN.uint32_tp_value(fdbIfNum), value)
231  if OpEN.OPEN_E_NONE == result:
232  print "Success: Get the value of internal entries in the L2FDB for a %d interface: %d.\n" % (OpEN.uint32_tp_value(fdbIfNum), value)
233  else:
234  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)
235 
236  ''' Testing of retrieving the value of internal entries in the L2FDB for a specific VLAN '''
237  result = OpEN.openapiFdbVlanInternalEntriesGet(self.clientHandle, OpEN.uint32_tp_value(vlanId), value)
238  if OpEN.OPEN_E_NONE == result:
239  print "Success: Get the value of internal entries in the L2FDB for a VLAN %d: %d.\n" % (OpEN.uint32_tp_value(vlanId), value)
240  else:
241  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)
242 
243  ''' Testing of flushing all entries in fdb learned on the specified interface '''
244  result = OpEN.openapiFdbFlushByPort(self.clientHandle, OpEN.uint32_tp_value(fdbIfNum))
245  if OpEN.OPEN_E_NONE == result:
246  print "Success: Flushed all entries in fdb learned on the interface %d.\n" % (OpEN.uint32_tp_value(fdbIfNum))
247  else:
248  print "Failed to flush all entries in fdb learned on the interface %d. (Result = %d)\n" % (OpEN.uint32_tp_value(fdbIfNum), result)
249 
250  ''' Testing of flushing all entries in fdb learned on the specified VLAN '''
251  result = OpEN.openapiFdbFlushByVlan(self.clientHandle, OpEN.uint32_tp_value(vlanId))
252  if OpEN.OPEN_E_NONE == result:
253  print "Success: Flushed all entries in fdb learned on the VLAN %d.\n" % (OpEN.uint32_tp_value(vlanId))
254  else:
255  print "Failed to flush all entries in fdb learned on the VLAN %d. (Result = %d)\n" % (OpEN.uint32_tp_value(vlanId), result)
256 
257  ''' Testing of flushing all entries in fdb learned on the specified MAC '''
258  result = OpEN.openapiFdbFlushByMac(self.clientHandle, mac)
259  if OpEN.OPEN_E_NONE == result:
260  print "Success: Flushed all entries in fdb learned on the MAC.\n"
261  else:
262  print "Failed to flush all entries in fdb learned on the MAC. (Result = %d)\n" % (result)
263 
264  ''' Testing of flushing all learned entries from the L2FDB '''
265  result = OpEN.openapiFdbFlush(self.clientHandle)
266  if OpEN.OPEN_E_NONE == result:
267  print "Success: Flushed all learned entries from the L2FDB.\n"
268  else:
269  print "Failed to flush all learned entries from the L2FDB. (Result = %d)\n" % (result)
270 
271 
272 def main():
273  ret = open_.connect("fdb_example")
274  if ret == OpEN.OPEN_E_NONE:
275  open_.getNetworkOSVersion()
276  open_.getAPIVersion()
277  client = open_.get_client()
278 
279  fdbExample = FdbExample(client)
280  fdbExample.main()
281 
282  open_.terminate()
283  else:
284  print "Unable to connect"
285 
286 
287 if __name__ == '__main__': main()