diff --git a/ebpfcat/ebpf.py b/ebpfcat/ebpf.py index 3ff007390f0572dd2d7c3bc570c075b21c774af7..7e5457d20c21583e40b709d74f51179087c33618 100644 --- a/ebpfcat/ebpf.py +++ b/ebpfcat/ebpf.py @@ -283,9 +283,6 @@ class Elser: def __exit__(self, exc_type, exc, tb): self.comp.__exit__(exc_type, exc, tb) - def Else(self): - return self.comp.Else() - class Comparison(ABC): """Base class for all logical operations""" @@ -865,9 +862,9 @@ class MemoryDesc: value = before & ~(1 << fmt[0]) except AssembleError: with ebpf.wtmp: - with value as cond: + with value as Else: ebpf.wtmp = before | (1 << fmt[0]) - with cond.Else(): + with Else: ebpf.wtmp = before & ~(1 << fmt[0]) else: mask = ((1 << fmt[1]) - 1) << fmt[0] diff --git a/ebpfcat/ebpf_test.py b/ebpfcat/ebpf_test.py index 366725ed1d9b786ddda0274f97eab7e33028f32e..e39c6afc697e04e98071e1802d179e1bc69d8867 100644 --- a/ebpfcat/ebpf_test.py +++ b/ebpfcat/ebpf_test.py @@ -367,31 +367,6 @@ class Tests(TestCase): Instruction(opcode=0xb7, dst=0, src=0, off=0, imm=1)]) def test_with(self): - e = EBPF() - e.owners = set(range(11)) - with e.r2 > 3 as cond: - e.r2 = 5 - with cond.Else(): - e.r6 = 7 - with e.r2: - e.r3 = 2 - with e.r4 > 3 as cond: - e.r5 = 7 - with cond.Else(): - e.r7 = 8 - self.assertEqual(e.opcodes, - [Instruction(opcode=0xb5, dst=2, src=0, off=2, imm=3), - Instruction(opcode=0xb7, dst=2, src=0, off=0, imm=5), - Instruction(opcode=0x5, dst=0, src=0, off=1, imm=0), - Instruction(opcode=O.MOV+O.LONG, dst=6, src=0, off=0, imm=7), - Instruction(opcode=O.JEQ, dst=2, src=0, off=1, imm=0), - Instruction(opcode=O.MOV+O.LONG, dst=3, src=0, off=0, imm=2), - Instruction(opcode=O.JLE, dst=4, src=0, off=2, imm=3), - Instruction(opcode=O.MOV+O.LONG, dst=5, src=0, off=0, imm=7), - Instruction(opcode=O.JMP, dst=0, src=0, off=1, imm=0), - Instruction(opcode=O.MOV+O.LONG, dst=7, src=0, off=0, imm=8)]) - - def test_with_new(self): e = EBPF() e.owners = set(range(11)) with e.r2 > 3 as Else: @@ -420,10 +395,10 @@ class Tests(TestCase): e = EBPF() with e.r1 & 1 as cond: e.r0 = 2 - with e.r1 & 7 as cond: + with e.r1 & 7 as Else: e.r0 = 2 e.r1 = 4 - with cond.Else(): + with Else: e.r0 = 3 self.assertEqual(e.opcodes, [ Instruction(opcode=69, dst=1, src=0, off=1, imm=1), @@ -438,11 +413,11 @@ class Tests(TestCase): def test_with_and(self): e = EBPF() e.owners = set(range(11)) - with (e.r2 > 3) & (e.r3 > 2) as cond: + with (e.r2 > 3) & (e.r3 > 2) as Else: e.r1 = 5 - with (e.r2 > 2) & (e.r1 < 2) as cond: + with (e.r2 > 2) & (e.r1 < 2) as Else: e.r2 = 5 - with cond.Else(): + with Else: e.r3 = 7 self.assertEqual(e.opcodes, [ Instruction(opcode=O.JLE, dst=2, src=0, off=2, imm=3), @@ -457,12 +432,12 @@ class Tests(TestCase): def test_with_or(self): e = EBPF() e.owners = set(range(11)) - with (e.r2 > 3) | (e.r3 > 2) as cond: + with (e.r2 > 3) | (e.r3 > 2) as Else: e.r1 = 5 - with (e.r2 > 2) | (e.r1 > 2) as cond: + with (e.r2 > 2) | (e.r1 > 2) as Else: e.r2 = 5 e.r5 = 4 - with cond.Else(): + with Else: e.r3 = 7 e.r4 = 3 self.assertEqual(e.opcodes, [ @@ -480,9 +455,9 @@ class Tests(TestCase): def test_comp_binary(self): e = EBPF() e.owners = {1, 2, 3, 5} - with e.r1 + e.r3 > 3 as cond: + with e.r1 + e.r3 > 3 as Else: e.r0 = 5 - with cond.Else(): + with Else: e.r0 = 7 tgt = e.jumpIf(e.r0 < e.r2 + e.r5) @@ -637,10 +612,10 @@ class Tests(TestCase): def test_with_data(self): e = EBPF() - with e.r1 > 0 as cond: + with e.r1 > 0 as Else: e.r2 = 3 e.r3 = 5 - with cond.Else(): + with Else: with self.assertRaises(AssembleError): e.r0 = e.r2 e.r3 = 5 @@ -749,7 +724,7 @@ class Tests(TestCase): e = XDP(license="GPL") with e.packetSize > 100 as p: e.r3 = p.pH[22] - with p.Else(): + with p.Else: e.r3 = 77 self.assertEqual(e.opcodes, [ Instruction(opcode=O.LD+O.W, dst=9, src=1, off=0, imm=0), diff --git a/ebpfcat/ebpfcat.py b/ebpfcat/ebpfcat.py index f0f0b6c4d375c78372d2235ce386b883a5511db1..1db24b151b50f9d39f4b4af25ae0b516ffa8393a 100644 --- a/ebpfcat/ebpfcat.py +++ b/ebpfcat/ebpfcat.py @@ -275,16 +275,16 @@ class EtherXDP(XDP): self.r[dst] += 4 * self.r3 self.r4 = self.mB[self.r[dst]] # we lost a packet - with p.pB[IDX0] == self.r4 as cond: + with p.pB[IDX0] == self.r4 as Else: self.mI[self.r[dst]] += 1 + (self.r4 & 1) # normal case: two packets on the wire - with cond.Else(), ((p.pB[IDX0] + 1 & 0xff) == self.r4) \ - | (p.pB[IDX0] == 0) as c2: + with Else, ((p.pB[IDX0] + 1 & 0xff) == self.r4) \ + | (p.pB[IDX0] == 0) as Else: self.mI[self.r[dst]] += 1 with self.r4 & 1: # last one was active p.pB[IDX0] = self.mB[self.r[dst]] self.exit(XDPExitCode.TX) - with c2.Else(): + with Else: self.exit(XDPExitCode.PASS) p.pB[IDX0] = self.mB[self.r[dst]] self.r2 = self.get_fd(self.programs) diff --git a/ebpfcat/xdp.py b/ebpfcat/xdp.py index 4a1526febb69a670a9e95a994283819889410cba..daa4c50d26ecc88668daafc3f4e24f41497517cb 100644 --- a/ebpfcat/xdp.py +++ b/ebpfcat/xdp.py @@ -104,9 +104,9 @@ class PacketArray: class Packet: - def __init__(self, ebpf, comp, no): + def __init__(self, ebpf, Else, no): self.ebpf = ebpf - self.comp = comp + self.Else = Else self.no = no self.pB = PacketArray(self.ebpf, self.no, self.ebpf.mB) @@ -114,9 +114,6 @@ class Packet: self.pI = PacketArray(self.ebpf, self.no, self.ebpf.mI) self.pQ = PacketArray(self.ebpf, self.no, self.ebpf.mQ) - def Else(self): - return self.comp.Else() - class PacketSize: def __init__(self, ebpf): @@ -126,15 +123,15 @@ class PacketSize: def __lt__(self, value): e = self.ebpf e.r9 = e.mA[e.r1] - with e.mA[e.r1 + 4] < e.mA[e.r1] + value as comp: - yield Packet(e, comp, 9) + with e.mA[e.r1 + 4] < e.mA[e.r1] + value as Else: + yield Packet(e, Else, 9) @contextmanager def __gt__(self, value): e = self.ebpf e.r9 = e.mA[e.r1] - with e.mA[e.r1 + 4] > e.mA[e.r1] + value as comp: - yield Packet(e, comp, 9) + with e.mA[e.r1 + 4] > e.mA[e.r1] + value as Else: + yield Packet(e, Else, 9) def __le__(self, value): return self < value + 1