Skip to content

Instantly share code, notes, and snippets.

@cwyang
Created May 26, 2022 13:58
Show Gist options
  • Save cwyang/a6972d859aea539a041bc7fb3fd2bb9b to your computer and use it in GitHub Desktop.
Save cwyang/a6972d859aea539a041bc7fb3fd2bb9b to your computer and use it in GitHub Desktop.
os-ken & logger perf
import sys
import logging
from os_ken.ofproto import ether, inet
from os_ken.lib.packet import ethernet
from os_ken.lib.packet import ipv4, ipv6
from os_ken.lib.packet import tcp, udp
from os_ken.lib.packet import packet, packet_utils
from os_ken.lib import addrconv
LOG = logging.getLogger('test_packet')
hdlr = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
LOG.addHandler(hdlr)
LOG.setLevel(logging.DEBUG)
class TestPacket:
dst_mac = 'aa:aa:aa:aa:aa:aa'
src_mac = 'bb:bb:bb:bb:bb:bb'
dst_mac_bin = addrconv.mac.text_to_bin(dst_mac)
src_mac_bin = addrconv.mac.text_to_bin(src_mac)
dst_ip = '192.168.128.10'
src_ip = '192.168.122.20'
dst_ip_bin = addrconv.ipv4.text_to_bin(dst_ip)
src_port = 50001
dst_port = 50002
src_ip_bin = addrconv.ipv4.text_to_bin(src_ip)
payload = b'\x06\x06\x47\x50\x00\x00\x00\x00' \
+ b'\xcd\xc5\x00\x00\x00\x00\x00\x00' \
+ b'\x10\x11\x12\x13\x14\x15\x16\x17' \
+ b'\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'
def __init__(self):
# buid packet
e = ethernet.ethernet(self.dst_mac, self.src_mac,
ether.ETH_TYPE_IP)
ip = ipv4.ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0,
self.src_ip, self.dst_ip)
t = tcp.tcp(0x190F, 0x1F90, 0x123, 1, 6, 0b101010, 2048, 0, 0x6f,
b'\x00\x00')
p = packet.Packet()
p.add_protocol(e)
p.add_protocol(ip)
p.add_protocol(t)
p.add_protocol(self.payload)
p.serialize()
self.p = p
def packet(self):
return packet.Packet(self.p.data)
n = int(sys.argv[1])
t = TestPacket()
for _ in range(n):
pkt = t.packet()
LOG.info("pkt=%s", pkt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment