question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cocotb with scapy cosimulation

See original GitHub issue

Hi. I’ve been using Cocotb with scapy for simulating a tcp stack. Till now I’ve successfully run the ping/arp procedure. It’s TCP stack <=>MAC <=> cocotb <=> TAP device on wsl2 windows10 2004

This is the code I’m using for ping

@cocotb.coroutine
async def rgmii_sfuncion(a, b, c):
    while True:
        data = a.recv_raw()
        crc = crc32(data[1])
        rawdata = b'\x55\x55\x55\x55\x55\x55\x55\xD5' + data[1]
        for i in range(4):
            temp = (crc >> (8*i)) & 0xFF
            rawdata += bytes([temp])
        await b.send(rawdata)
        await b.wait()
        rx_frame = await c.recv()
        #a.send(rx_frame.data[8:])
        a.outs.write(rx_frame.data[8:])

It needs a packet send followed by a packet recv. So ping and arp works fine. I think.

So when it comes to tcp, the problem is how can I recv a packet while send a packet concurrently.

I’m thinking about

@cocotb.coroutine
async def rgmii_sfuncion(a, b, c):
    while True:
        data = a.recv_raw()
        crc = crc32(data[1])
        rawdata = b'\x55\x55\x55\x55\x55\x55\x55\xD5' + data[1]
        for i in range(4):
            temp = (crc >> (8*i)) & 0xFF
            rawdata += bytes([temp])
        await b.send(rawdata)
        await b.wait()
        #rx_frame = await c.recv()
        #a.send(rx_frame.data[8:])
        #a.outs.write(rx_frame.data[8:])

@cocotb.coroutine
async def rgmii_rfuncion(a, b, c):
    while True:
        rx_frame = await c.recv()
        #a.send(rx_frame.data[8:])
        a.outs.write(rx_frame.data[8:])

Is that work? I’ve tried the code before but no fortune.

But there may be a problem in the tcp stack.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
silenuszhicommented, Feb 5, 2021

The problem is my send and recv function is both while true loop.

This is the point of cocotb.fork. It creates two concurrent tasks that run “at the same time”. So two infinite loops will work fine.

In async def recv. I use the scapy.recv_raw() which is a blocking function

Ahh, I see. scapy.recv_raw and the other scapy calls are threaded blocking calls. Wrap the functions that block in cocotb.external to turn them into an coroutine that you can await on. For example:

data = await cocotb.external(a.recv_raw)()

I’m a such noob of python. My mistake. After surround both scapy read / write function with cocotb.external. Now it works like a charm.

Thankyou all for help. @ktbarrett @alexforencich @themperek

0reactions
silenuszhicommented, Feb 5, 2021

Ahh. I get the problem. Need sometime to figure it out. Update later. Close for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial: Driver Cosimulation — cocotb 1.1 documentation
In this tutorial we will call unmodified production software from our testbench and re-use the code written to configure the entity. For the...
Read more >
Cocotb: a Python-based digital logic verification framework
Cocotb is a library for digital logic verification in Python. Coroutine cosimulation testbench. Provides Python interface to control ...
Read more >
cocotb, a coroutine based cosimulation library for writing ...
cocotb is a coroutine based cosimulation library for writing VHDL and Verilog testbenches in Python. Documentation Status CI PyPI Gitpod Ready-to-Code codecov.
Read more >
Digital/Analog Cosimulation using CocoTB and Xyce
For interfacing with GHDL, we use CocoTB an open- source automated cosimulation and testbench library [1]. CocoTB is typically used for generating and...
Read more >
cocotb(COroutine-based COsimulation TestBench) and ...
This video is all about how cocotb (COroutine-based COsimulation TestBench) environment help's for verifying Verilog/VHDL/System Verilog RTL ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found