]> git.proxmox.com Git - mirror_ovs.git/blob - tests/sendpkt.py
odp-util: Fix netlink message overflow with userdata.
[mirror_ovs.git] / tests / sendpkt.py
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2018, 2020 VMware, Inc.
4 #
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:
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17
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)
21 #
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
25 #
26
27
28 import socket
29 import sys
30 from optparse import OptionParser
31
32
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)",
38 default="eth")
39
40 (options, args) = parser.parse_args()
41
42 # validate the arguments
43 if len(args) < 2:
44 parser.print_help()
45 sys.exit(1)
46
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".')
50
51 # store the hex bytes with 0x appended at the beginning
52 # if not present in the user input and validate the hex bytes
53 hex_list = []
54 for a in args[1:]:
55 if a[:2] != "0x":
56 hex_byte = "0x" + a
57 else:
58 hex_byte = a
59 try:
60 temp = int(hex_byte, 0)
61 except:
62 parser.error("invalid hex byte " + a)
63
64 if temp > 0xff:
65 parser.error("hex byte " + a + " cannot be greater than 0xff!")
66
67 hex_list.append(temp)
68
69 if sys.version_info < (3, 0):
70 pkt = "".join(map(chr, hex_list))
71 else:
72 pkt = bytes(hex_list)
73
74 try:
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]) + ' : '
78 + msg[1])
79 sys.exit(2)
80
81 try:
82 sockfd.bind((args[0], 0))
83 except socket.error as msg:
84 print('unable to bind socket! error code: ' + str(msg[0]) + ' : '
85 + msg[1])
86 sys.exit(2)
87
88 try:
89 sockfd.send(pkt)
90 except socket.error as msg:
91 print('unable to send packet! error code: ' + str(msg[0]) + ' : '
92 + msg[1])
93 sys.exit(2)
94
95 print('send success!')
96 sys.exit(0)