Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring BESS scripts from UPF repo #6

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions bessctl/module_tests/ddp-rss-rx.bess
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Intel Corporation

num_q = 4
kwargs = {'flow_profiles' : [3],
'num_inc_q': num_q,
'num_out_q': num_q}
p0 = PMDPort(port_id=0, **kwargs)
qinc_0_0::QueueInc(port=p0.name, qid=0) -> Sink()
qinc_0_1::QueueInc(port=p0.name, qid=1) -> Sink()
qinc_0_2::QueueInc(port=p0.name, qid=2) -> Sink()
qinc_0_3::QueueInc(port=p0.name, qid=3) -> Sink()

kwargs = {'flow_profiles' : [6, 9],
'num_inc_q': num_q,
'num_out_q': num_q}
p1 = PMDPort(port_id=1, **kwargs)
qinc_1_0::QueueInc(port=p1.name, qid=0) -> Sink()
qinc_1_1::QueueInc(port=p1.name, qid=1) -> Sink()
qinc_1_2::QueueInc(port=p1.name, qid=2) -> Sink()
qinc_1_3::QueueInc(port=p1.name, qid=3) -> Sink()
111 changes: 111 additions & 0 deletions bessctl/module_tests/exactmatch-val.bess
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# vim: syntax=py
# -*- mode: python -*-
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 Intel Corporation
#
# Copyright 2014-2016, The Regents of the University of California.
# Copyright 2016-2017, Nefeli Networks, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the names of the copyright holders nor the names of their
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import scapy.all as scapy
import socket

src_mac='02:1e:67:9f:4d:ae'
dst_mac='06:16:3e:1b:72:32'
nh_mac1='00:00:00:00:00:01'
nh_mac2='00:00:00:00:00:02'
nh_mac3='00:00:00:00:00:03'
nh_mac4='60:61:62:63:64:65'

def aton(ip):
return socket.inet_aton(ip)

def mac2hex(mac):
return int(mac.replace(':', ''), 16)

# Craft a packet with the specified IP addresses
def gen_packet(proto, src_ip, dst_ip):
eth = scapy.Ether(src=src_mac, dst=dst_mac)
ip = scapy.IP(src=src_ip, dst=dst_ip)
udp = proto(sport=10001, dport=10002)
payload = 'helloworld'
pkt = eth/ip/udp/payload
return bytes(pkt)

packets = [gen_packet(scapy.UDP, '172.16.100.1', '10.0.0.1'),
gen_packet(scapy.UDP, '172.12.55.99', '12.34.56.78'),
gen_packet(scapy.UDP, '172.12.55.99', '10.0.0.1'),
gen_packet(scapy.UDP, '172.16.100.1', '12.34.56.78'),
gen_packet(scapy.UDP, '192.168.1.123', '12.34.56.78'),
]

# L4 protocol and source/destination IP addresses
em::ExactMatch(fields=[{'offset':0, 'num_bytes':6},
{'offset':23, 'num_bytes':1},
{'offset':26, 'num_bytes':4},
{'offset':30, 'num_bytes':4},
{'attr_name':'foo', 'num_bytes':2}],
values=[{'offset':0, 'num_bytes':6},
{'attr_name':'bar', 'num_bytes':1}])

Source() -> Rewrite(templates=packets) -> SetMetadata(attrs=[{'name': 'foo', 'size': 2, 'value_int':0x3344}]) -> em

em:0 -> m0::Merge() -> attrCmp::Split(size=1, attribute='bar')
em:1 -> m0
em:2 -> m0

em.add(fields=[{'value_int': mac2hex(dst_mac)}, {'value_int':17}, {'value_bin':aton('172.16.100.1')}, {'value_bin':aton('10.0.0.1')}, {'value_int': 0x4433}], gate=0, values=[{'value_int': mac2hex(nh_mac1)}, {'value_int': 30}])
em.add(fields=[{'value_int': mac2hex(dst_mac)}, {'value_int':17}, {'value_bin':aton('172.12.55.99')}, {'value_bin':aton('12.34.56.78')}, {'value_int': 0x4433}], gate=1, values=[{'value_int': mac2hex(nh_mac2)}, {'value_int': 29}])
em.add(fields=[{'value_int': mac2hex(dst_mac)}, {'value_int':17}, {'value_bin':aton('172.12.55.99')}, {'value_bin':aton('10.0.0.1')}, {'value_int': 0x4433}], gate=2, values=[{'value_int': mac2hex(nh_mac3)}, {'value_int': 28}])


# delete test
em.add(fields=[{'value_int': mac2hex(dst_mac)}, {'value_bin':chr(17)}, {'value_bin':aton('192.168.1.123')}, {'value_bin':aton('12.34.56.78')}, {'value_int': 0x4433}], gate=3, values=[{'value_int': mac2hex(nh_mac4)}, {'value_int': 27}])
em.delete(fields=[{'value_int': mac2hex(dst_mac)}, {'value_bin':chr(17)}, {'value_bin':aton('192.168.1.123')}, {'value_bin':aton('12.34.56.78')}, {'value_int': 0x4433}])

# Setting out gates
em:3 -> BackgroundTraffic::Sink() # used as default gate
em.set_default_gate(gate=3)

# setting failure gates
attrCmp -> em0Failure::Sink()

# setting success gates
attrCmp:30 -> m1::Merge() -> offsetCmp::Split(size=6, offset=0)
attrCmp:29 -> m1
attrCmp:28 -> m1


# setting failure gates
offsetCmp -> em1Failure::Sink()

# setting success gates
offsetCmp:1 -> m2::Merge() -> Success::Sink()
offsetCmp:2 -> m2
offsetCmp:3 -> m2
9 changes: 9 additions & 0 deletions bessctl/module_tests/ipdefrag.bess
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# vim: syntax=py
# -*- mode: python -*-
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 Intel Corporation

p = PMDPort(name='ipv4frags', vdev='eth_pcap0,rx_pcap=ipv4frags.pcap,tx_pcap=reassembled.pcap')
reassemb = IPDefrag(num_flows=10)
PortInc(port=p.name) -> reassemb:1 -> PortOut(port=p.name)
reassemb:0 -> Sink()
Binary file added bessctl/module_tests/ipv4frags.pcap
Binary file not shown.
136 changes: 136 additions & 0 deletions bessctl/module_tests/metering.bess
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Intel Corporation

# Update assignments to hit different QoS entries
iface, qer, fseid = 1, 2, 4
ip_pkt_size = 46
sleep_seconds_before_delete = 30

import struct
def convert(format, val, bigendian=True):
end = '>' if [bigendian] else '<'
return struct.pack(end + format, val, )


Meta::SetMetadata(attrs=[{'name': 'src_iface', 'size': 1, 'value_int': iface},
{'name': 'qer_id', 'size':4, 'value_int': qer},
{'name': 'fseid', 'size':8, 'value_int': fseid}])

Metering::Qos(fields=[{'attr_name':'src_iface', 'num_bytes':1},
{'attr_name':'qer_id', 'num_bytes':4},
{'attr_name':'fseid', 'num_bytes':8}],
values=[{'attr_name':'qfi', 'num_bytes':1}])

M::Merge()
S::Split(size=1, attribute='qfi')

# Reserved gates, reject rule adds with gate=1/2/3
m_meter = 0 # Placeholder gate not connected. Will meter if lookup result returns this gate
m_green = 1 # For green traffic
m_yellow = 2 # For yellow traffic
m_red = 3 # For red traffic

# Rules with gate number above 3 are directly sent out w/o metering
m_fail = 4 # Default gate for lookup failure
m_drop = 5 # Explicitly asked to drop
m_unmeter= 6 # Unmetered

Metering:m_green -> M
Metering:m_yellow -> M
Metering:m_red -> red::Sink()
Metering:m_drop -> drop::Sink()
Metering:m_unmeter -> S

Metering:m_fail -> fail::Sink()
Metering.set_default_gate(gate=m_fail)

# Pipeline
Source() -> Meta -> Metering
M -> S

S:0 -> bad::Sink() # If traffic is going to this gate, meta update in QoS is not working
S:1 -> gbr1::Sink()
S:2 -> gbr5::Sink()
S:3 -> gmbr6::Sink()
S:4 -> mbr6::Sink()
S:5 -> unmeter::Sink()

# Resume workers to test concurrent read-write
bess.resume_all()

MPPS = pow(10,6) * ip_pkt_size

# Add QoS entries
# GBR only
Metering.add(fields=[{'value_int': 1},
{'value_bin': convert('L', 2)},
{'value_bin': convert('Q', 4)}],
values=[{'value_int': 1}], # S:1
gate=m_meter, cir=1 * MPPS , pir=1 * MPPS, cbs=2048, pbs=2048)

Metering.add(fields=[{'value_int': 1},
{'value_bin': convert('L', 3)},
{'value_bin': convert('Q', 6)}],
values=[{'value_int': 2}], # S:2
gate=m_meter, cir=5 * MPPS, pir=5 * MPPS, cbs=2048, pbs=2048)

# GBR/MBR
Metering.add(fields=[{'value_int': 1},
{'value_bin': convert('L', 4)},
{'value_bin': convert('Q', 8)}],
values=[{'value_int': 3}], # S:3
gate=m_meter, cir=3 * MPPS, pir=6 * MPPS, cbs=2048, pbs=2048)

# MBR only
Metering.add(fields=[{'value_int': 1},
{'value_bin': convert('L', 5)},
{'value_bin': convert('Q', 10)}],
values=[{'value_int': 4}], # S:4
gate=m_meter, cir=1, pir=6 * MPPS, cbs=2048, pbs=2048)

# Unmeter
Metering.add(fields=[{'value_int': 1},
{'value_bin': convert('L', 6)},
{'value_bin': convert('Q', 12)}],
values=[{'value_int': 5}], # S:5
gate=m_unmeter)

# Gate closed
Metering.add(fields=[{'value_int': 1},
{'value_bin': convert('L', 2)},
{'value_bin': convert('Q', 3)}],
values=[{'value_int': 5}],
gate=m_drop)

# Delete QoS entries after sleep
import time
time.sleep(sleep_seconds_before_delete)

# GBR only
Metering.delete(fields=[{'value_int': 1},
{'value_bin': convert('L', 2)},
{'value_bin': convert('Q', 4)}])

Metering.delete(fields=[{'value_int': 1},
{'value_bin': convert('L', 3)},
{'value_bin': convert('Q', 6)}])

# GBR/MBR
Metering.delete(fields=[{'value_int': 1},
{'value_bin': convert('L', 4)},
{'value_bin': convert('Q', 8)}])

# MBR only
Metering.delete(fields=[{'value_int': 1},
{'value_bin': convert('L', 5)},
{'value_bin': convert('Q', 10)}])

# Unmeter
Metering.delete(fields=[{'value_int': 1},
{'value_bin': convert('L', 6)},
{'value_bin': convert('Q', 12)}])

# Gate closed
Metering.delete(fields=[{'value_int': 1},
{'value_bin': convert('L', 2)},
{'value_bin': convert('Q', 3)}])
101 changes: 101 additions & 0 deletions bessctl/module_tests/wildcardmatch-masks-bench.bess
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2022 Open Networking Foundation
# Copyright 2019 Intel Corporation
# Copyright 2014-2016, The Regents of the University of California.
# Copyright 2016-2017, Nefeli Networks, Inc.

# This pipeline showcases the effect of increasing the number of distinct
# entries in the WildcardMatch module on throughput.

import scapy.all as scapy
import socket
import struct
import ipaddress
import time

nh_mac='00:00:00:00:00:01'

def atoh(ip):
return socket.inet_aton(ip)

def mac2hex(mac):
return int(mac.replace(':', ''), 16)

# Craft a packet with the specified IP address and port number
def gen_packet(dst_ip, dst_port):
eth = scapy.Ether(src='02:1e:67:9f:4d:ae', dst='06:16:3e:1b:72:32')
ip = scapy.IP(src='10.0.0.1', dst=dst_ip)
udp = scapy.UDP(sport=1234, dport=dst_port)
payload = 'helloworld'
pkt = eth/ip/udp/payload
return bytes(pkt)

pkts = [
gen_packet('20.0.0.1', 80),
]

# destination IP address + destination port number + metadata 'in_port'
wm::WildcardMatch(fields=[{'offset':30, 'num_bytes':4},
{'offset':36, 'num_bytes':2},
{'attr_name':'in_port', 'num_bytes':2}],
values=[{'offset':6, 'num_bytes':6},
{'attr_name':'out_port1', 'num_bytes':2},
{'attr_name':'out_port2', 'num_bytes':2}
])

# locally emulate two input ports
Source() -> SetMetadata(attrs=[{'name': 'in_port', 'size': 2, 'value_int': 1}, {'name': 'in_port2', 'size':2, 'value_int':5}]) -> Rewrite(templates=pkts) -> wm
Source() -> SetMetadata(attrs=[{'name': 'in_port', 'size': 2, 'value_int': 2}]) -> Rewrite(templates=pkts) -> wm

wm:0 -> m0::Merge() -> offsetCmp::Split(size=6, offset=6)
wm:1 -> m0
wm:10 -> BackgroundTraffic::Sink() # used as default gate

wm.set_default_gate(gate=10)

# setting failure gates
offsetCmp -> wm0Failure::Sink()

# setting success gates
offsetCmp:1 -> m1::Merge() -> attr1Cmp::Split(size=2, attribute='out_port1')
offsetCmp:2 -> m1
offsetCmp:3 -> m1
offsetCmp:4 -> m1
offsetCmp:5 -> m1


# setting failure gates
attr1Cmp -> wm1Failure::Sink()

# setting success gates
attr1Cmp:17 -> attr2Cmp::Split(size=2, attribute='out_port2')

# setting failure gates
attr2Cmp -> wm2Failure::Sink()

attr2Cmp:4352 -> Success::Sink()


# Matching rule
# 20.0.0.1/32, in_port any
wm.add(values=[{'value_bin': atoh('20.0.0.1')}, {'value_int': 0}, {'value_int':0} ], gate=1,
masks=[{'value_bin': atoh('255.255.255.255')}, {'value_int':0x0000}, {'value_int': 0x0000}],
valuesv=[{'value_int': mac2hex(nh_mac)}, {'value_int':0x1100}, {'value_int':0x0011}],
priority=1)

bess.resume_all()
time.sleep(10)

# Dummy rules
for i in range(16, 16+16):
bess.pause_all()
prefix = 32 - i
subnet = ipaddress.ip_address(((2**32 - 1) >> prefix) << prefix)
print(i, prefix, subnet, subnet.packed)
# 10.0.0.0/i, in_port any
wm.add(values=[{'value_bin': atoh('10.0.0.0')}, {'value_int': 0}, {'value_int': 0} ], gate=0,
masks=[{'value_bin': subnet.packed}, {'value_int': 0x0000}, {'value_int': 0x0000}],
valuesv=[{'value_int': mac2hex(nh_mac)}, {'value_int': 0x1100}, {'value_int': 0x0011}],
priority=40+i)
bess.resume_all()
time.sleep(10)
Loading