Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.6.0.3
tx_pkt_example.py
1 /*! \file tx_pkt_example.py
2  */
3 
4 #!/mnt/fastpath/usr/bin/python
5 
6 """tx_pkt_example.py: OpEN API Packet Transmit example"""
7 
8 import OpEN_py as OpEN
9 from OpENUtil import *
10 import sys
11 
12 open_ = OpENUtil()
13 
14 #
15 # Copyright 2016 Broadcom.
16 #
17 # Licensed under the Apache License, Version 2.0 (the "License")
18 # you may not use this file except in compliance with the License.
19 # You may obtain a copy of the License at
20 #
21 # http://www.apache.org/licenses/LICENSE-2.0
22 #
23 # Unless required by applicable law or agreed to in writing, software
24 # distributed under the License is distributed on an "AS IS" BASIS,
25 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 # See the License for the specific language governing permissions and
27 # limitations under the License.
28 #
29 
30 #
31 # Python 2.6.6
32 #
33 
34 PKT_LEN = 128
35 
36 def print_sanity_results(result, test, msg, feat) :
37  """Print overall comparison results"""
38 
39  if result == OpEN.OPEN_E_UNAVAIL:
40  print "Sanity test skipped."
41  elif result == OpEN.OPEN_E_EXISTS:
42  print "Sanity Skipped (already exists) - %s - %s." % (msg, feat)
43  elif result == OpEN.OPEN_E_NONE and test == True:
44  print "Sanity Success - %s - %s." % (msg, feat)
45  else:
46  print "Sanity Failure - %s - %s." % (msg, feat)
47 
48 def print_bad_result(result, msg) :
49  """Print some general error messages if the result is bad"""
50 
51  if result == OpEN.OPEN_E_UNAVAIL:
52  print "Feature not supported - %s (err %d)." % (msg, result)
53  elif result == OpEN.OPEN_E_NOT_FOUND:
54  print "Test Skipped (not found) - ", msg
55  elif result != OpEN.OPEN_E_NONE:
56  print "Test Failure - %s (err %d)." % (msg, result)
57 
58 class TxPktExample :
59  """Packet transmission """
60 
61  def __init__(self, client) :
62  self.m_client = client
63 
64  def test_tx_pkt(self, intf) :
65  """Send a packet using Unicast transmission"""
66 
67  intf_max_p = OpEN.new_uint32_tp()
68 
69  result = OpEN.openapiMaxInterfaceCountGet(self.m_client, intf_max_p)
70  if result == OpEN.OPEN_E_NONE:
71  max_intf = OpEN.uint32_tp_value(intf_max_p)
72  if intf < 1 or intf > max_intf:
73  print "Invalid interface. The available range is 1 to %d." % max_intf
74  OpEN.delete_uint32_tp(intf_max_p)
75  return
76  else:
77  OpEN.delete_uint32_tp(intf_max_p)
78  return
79 
80  # Build a dummy ethernet packet
81  pkt_data = "\x00\x01\x02\x03\x04\x05"
82  pkt_data += "\x06\x07\x08\x09\x0a\x0b"
83  pkt_data += "\xcc" * (PKT_LEN-len(pkt_data))
84 
85  try:
86  pkt_string = open_.getByteBuffer(PKT_LEN, pkt_data)
87  except OpENBufferSizeError:
88  print("test_tx_pkt: getByteBuffer raised OpENBufferSizeError")
89  return
90  except TypeError:
91  print("test_tx_pkt: getByteBuffer raised TypeError")
92  return
93  pkt_buff = OpEN.open_buffdesc()
94  pkt_buff.pstart = pkt_string
95  pkt_buff.size = PKT_LEN
96 
97  print "Send Packet..."
98  result = OpEN.openapiExtPktTransmit(self.m_client, pkt_buff, OpEN.OPEN_EXT_PKT_TX_L2RAW_UNICAST, intf, 0, 0)
99  print_bad_result(result, 'openapiExtPktTransmit')
100  print_sanity_results(result, (1 == 1), 'openapiExtPktTransmit', 'ifnum %d' % intf)
101  OpEN.delete_uint32_tp(intf_max_p)
102 
103 def main(argv) :
104  """Demonstrate OpEN usage for Packet API"""
105 
106  if len(argv) == 1:
107  try:
108  intf = int(argv[0])
109  except:
110  print 'Invalid interface, <ifnum> must be numeric.'
111  sys.exit()
112  else:
113  print 'tx_pkt_example.py <ifnum>'
114  sys.exit()
115 
116  ret = open_.connect("tx_pkt_example")
117  if ret == OpEN.OPEN_E_NONE :
118  open_.getNetworkOSVersion()
119  client = open_.get_client()
120  example = TxPktExample(client)
121  example.test_tx_pkt(intf)
122  open_.terminate()
123  else :
124  print "Unable to connect"
125 
126 if __name__ == '__main__':
127  main(sys.argv[1:])
128 
129