Skip to content
Snippets Groups Projects
Commit 5c67d021 authored by Martin Teichmann's avatar Martin Teichmann
Browse files

add the prandom function to ebpf

and use it instead of our own random number generator for the
missed packages simulator
parent c0e57f6c
No related branches found
No related tags found
No related merge requests found
...@@ -902,6 +902,21 @@ class ktime(Expression): ...@@ -902,6 +902,21 @@ class ktime(Expression):
yield dst, True, False yield dst, True, False
class prandom(Expression):
"""a function that returns the current ktime in ns"""
def __init__(self, ebpf):
self.ebpf = ebpf
@contextmanager
def calculate(self, dst, long, signed, force=False):
with self.ebpf.get_free_register(dst) as dst:
with self.ebpf.save_registers([i for i in range(6) if i != dst]):
self.ebpf.call(FuncId.get_prandom_u32)
if dst != 0:
self.ebpf.r[dst] = self.ebpf.r0
yield dst, True, False
class RegisterDesc: class RegisterDesc:
def __init__(self, no, array): def __init__(self, no, array):
self.no = no self.no = no
......
...@@ -21,7 +21,7 @@ from struct import pack, unpack, calcsize, pack_into, unpack_from ...@@ -21,7 +21,7 @@ from struct import pack, unpack, calcsize, pack_into, unpack_from
from time import time from time import time
from .arraymap import ArrayMap, ArrayGlobalVarDesc from .arraymap import ArrayMap, ArrayGlobalVarDesc
from .ethercat import ECCmd, EtherCat, Packet, Terminal from .ethercat import ECCmd, EtherCat, Packet, Terminal
from .ebpf import FuncId, MemoryDesc, SubProgram, ktime from .ebpf import FuncId, MemoryDesc, SubProgram, prandom
from .xdp import XDP, XDPExitCode from .xdp import XDP, XDPExitCode
from .bpf import ( from .bpf import (
ProgType, MapType, create_map, update_elem, prog_test_run, lookup_elem) ProgType, MapType, create_map, update_elem, prog_test_run, lookup_elem)
...@@ -277,11 +277,9 @@ class EtherXDP(XDP): ...@@ -277,11 +277,9 @@ class EtherXDP(XDP):
rate = 0 rate = 0
def program(self): def program(self):
with self.tmp: with prandom(self.ebpf) & 0xffff < self.rate:
self.ebpf.tmp = ktime(self.ebpf) self.dropcounter += 1
self.ebpf.tmp = self.ebpf.tmp * 0xcf019d85 + 1 self.ebpf.exit(XDPExitCode.DROP)
with self.ebpf.tmp & 0xffff < self.rate:
self.ebpf.exit(XDPExitCode.DROP)
with self.packetSize > 24 as p, p.pH[12] == 0xA488, p.pB[16] == 0: with self.packetSize > 24 as p, p.pH[12] == 0xA488, p.pB[16] == 0:
self.r3 = p.pI[18] self.r3 = p.pI[18]
with self.counters.get_address(None, False, False) as (dst, _), \ with self.counters.get_address(None, False, False) as (dst, _), \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment