]>
git.proxmox.com Git - mirror_ovs.git/blob - tests/sendpkt.py
3 # Copyright (c) 2018, 2020 VMware, Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 # This program can be used send L2-L7 protocol messages using the hex bytes
19 # of the packet, to test simple protocol scenarios. (e.g. generate simple
20 # nsh packets to test nsh match fields/actions)
22 # Currently, the script supports sending the packets starting from the
23 # Ethernet header. As a part of future enchancement, raw ip packet support
24 # can also be added, and that's why there is "-t"/"--type" option
30 from optparse
import OptionParser
33 usage
= "usage: %prog [OPTIONS] OUT-INTERFACE HEX-BYTES \n \
34 bytes in HEX-BYTES must be separated by space(s)"
35 parser
= OptionParser(usage
=usage
)
36 parser
.add_option("-t", "--type", type="string", dest
="packet_type",
37 help="packet type ('eth' is the default PACKET_TYPE)",
40 (options
, args
) = parser
.parse_args()
42 # validate the arguments
47 # validate the "-t" or "--type" option
48 if options
.packet_type
!= "eth":
49 parser
.error('invalid argument to "-t"/"--type". Allowed value is "eth".')
51 # store the hex bytes with 0x appended at the beginning
52 # if not present in the user input and validate the hex bytes
60 temp
= int(hex_byte
, 0)
62 parser
.error("invalid hex byte " + a
)
65 parser
.error("hex byte " + a
+ " cannot be greater than 0xff!")
69 if sys
.version_info
< (3, 0):
70 pkt
= "".join(map(chr, hex_list
))
75 sockfd
= socket
.socket(socket
.AF_PACKET
, socket
.SOCK_RAW
)
76 except socket
.error
as msg
:
77 print('unable to create socket! error code: ' + str(msg
[0]) + ' : '
82 sockfd
.bind((args
[0], 0))
83 except socket
.error
as msg
:
84 print('unable to bind socket! error code: ' + str(msg
[0]) + ' : '
90 except socket
.error
as msg
:
91 print('unable to send packet! error code: ' + str(msg
[0]) + ' : '
95 print('send success!')