]> git.proxmox.com Git - ovs.git/blame - tests/sendpkt.py
Fix ovs-dpctl-top by removing 3 wrong hunks in py3-compat.patch.
[ovs.git] / tests / sendpkt.py
CommitLineData
90c1cb3f 1#!/usr/bin/env python3
296251ca 2
90c1cb3f 3# Copyright (c) 2018, 2020 VMware, Inc.
296251ca
AV
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
28import socket
29import sys
30from optparse import OptionParser
31
32
33usage = "usage: %prog [OPTIONS] OUT-INTERFACE HEX-BYTES \n \
34 bytes in HEX-BYTES must be separated by space(s)"
35parser = OptionParser(usage=usage)
36parser.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
43if len(args) < 2:
44 parser.print_help()
45 sys.exit(1)
46
47# validate the "-t" or "--type" option
48if 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
53hex_list = []
54for 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
7c8d6fee
TR
69if sys.version_info < (3, 0):
70 pkt = "".join(map(chr, hex_list))
71else:
72 pkt = bytes(hex_list)
296251ca
AV
73
74try:
75 sockfd = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
76except socket.error as msg:
77 print('unable to create socket! error code: ' + str(msg[0]) + ' : '
78 + msg[1])
79 sys.exit(2)
80
81try:
82 sockfd.bind((args[0], 0))
83except socket.error as msg:
84 print('unable to bind socket! error code: ' + str(msg[0]) + ' : '
85 + msg[1])
86 sys.exit(2)
87
88try:
89 sockfd.send(pkt)
90except socket.error as msg:
91 print('unable to send packet! error code: ' + str(msg[0]) + ' : '
92 + msg[1])
93 sys.exit(2)
94
95print('send success!')
96sys.exit(0)