Skip to content

Commit d466ce4

Browse files
committed
add python scapy module example
1 parent 16403c9 commit d466ce4

File tree

12 files changed

+229
-8
lines changed

12 files changed

+229
-8
lines changed

0.systemverilog_only/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ all: clean comp run
44
clean:
55
\rm -rf simv* csrc* *.log __pycache__ ucli.key vc_hdrs.h stack.info.*
66
comp:
7-
vcs -full64 -LDFLAGS -Wl,--no-as-needed +incdir+./c -CC -lpython3.6m -CC -lpthread -CC -ldl -CC -lutil -lm -LDFLAGS -lpython3.6m -CC -I/usr/include/python3.6m -CC -I./ -sverilog gen_pkt_tb.sv c_call_py.c -l comp.log
7+
vcs -full64 -LDFLAGS -Wl,--no-as-needed -CC -lpython3.6m -lpthread -ldl -lutil -lm -LDFLAGS -lpython3.6m -CC -I/usr/include/python3.6m -CC -I. -sverilog gen_pkt_tb.sv c_call_py.c -l comp.log
88
run:
9-
./simv -l run.log
9+
export PYTHONPATH=. && ./simv -l run.log

0.systemverilog_only/py_gen.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python3
2-
from scapy.all import *
32

43
def gen_pkt(List):
5-
p=Ether(dst='33:32:33:44:55:66',src='33:22:22:34:56:33')/IP(ihl=5)/UDP(dport=0x4a00)/'This is Python Data !'
6-
p.show()
7-
return raw(p)
4+
print("C to Python :")
5+
print(List)
6+
p = "This is Python Data !"
7+
print("python print :"+p)
8+
byte = p.encode()
9+
return byte
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
all: clean comp run
3+
4+
clean:
5+
\rm -rf simv* csrc* *.log __pycache__ ucli.key vc_hdrs.h stack.info.*
6+
comp:
7+
vcs -full64 -LDFLAGS -Wl,--no-as-needed -CC -lpython3.6m -lpthread -ldl -lutil -lm -LDFLAGS -lpython3.6m -CC -I/usr/include/python3.6m -CC -I. -sverilog gen_pkt_tb.sv c_call_py.c -l comp.log
8+
run:
9+
export PYTHONPATH=.:./scapy-2.4.4 && ./simv -l run.log
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include<Python.h>
2+
#include<svdpi.h>
3+
#include<stdlib.h>
4+
5+
static PyObject *pModule = NULL;
6+
static PyObject *pFunc = NULL;
7+
static PyObject *pDict = NULL;
8+
static PyObject *pReturn = NULL;
9+
static PyObject *ArgList = NULL;
10+
static PyGILState_STATE py_state;
11+
12+
extern void c_py_init(){
13+
int i;
14+
double cArray[] = {1,2,3,4,5,6,};
15+
Py_Initialize();
16+
17+
py_state = PyGILState_Ensure();
18+
19+
pModule = PyImport_ImportModule("py_gen");
20+
pDict = PyModule_GetDict(pModule);
21+
pFunc = PyDict_GetItemString(pDict,"gen_pkt");
22+
23+
PyObject *PyList = PyList_New(6);
24+
ArgList = PyTuple_New(1);
25+
26+
for(i = 0; i < PyList_Size(PyList); i++){
27+
PyList_SetItem(PyList, i, PyFloat_FromDouble(cArray[i]));
28+
}
29+
30+
PyTuple_SetItem(ArgList, 0, PyList);
31+
printf("initialize Python runtime !\n");
32+
}
33+
34+
extern void c_py_final(){
35+
PyGILState_Release(py_state);
36+
Py_Finalize();
37+
printf("finalize Python runtime !\n");
38+
}
39+
40+
static unsigned char tmp[256] = {0};
41+
extern void c_py_gen_packet(/*output*/ svBitVecVal *pkt, /*output*/ svBitVecVal *len){
42+
int num;
43+
int i;
44+
unsigned char *result;
45+
46+
pReturn = PyObject_CallObject(pFunc, ArgList);
47+
48+
if(PyBytes_Check(pReturn)){
49+
num = PyBytes_Size(pReturn);
50+
result = PyBytes_AsString(pReturn);
51+
//memcpy(pkt,result,num);
52+
for( i = 0; i < num; i++){
53+
tmp[255-i] = result[i];
54+
printf("%02X ",result[i]);
55+
}
56+
memcpy(pkt, tmp, 256);
57+
58+
*len = num;
59+
printf("\n");
60+
} else {
61+
printf("Get No Bytes Form Python !!!\n");
62+
}
63+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
module hello_world;
3+
4+
import "DPI-C" function void c_py_init();
5+
import "DPI-C" function void c_py_final();
6+
import "DPI-C" function void c_py_gen_packet(output bit[2047:0] pkt, output bit [7:0] len);
7+
8+
bit [15:0][127:0] data;
9+
bit [7 :0] len ;
10+
11+
initial begin
12+
c_py_init();
13+
c_py_gen_packet(data,len);
14+
15+
$display("print data in systemverilog !");
16+
17+
$display("get len ='h%h",len );
18+
$display("get data ='h%h",data);
19+
$display("get data = %s",data);
20+
21+
c_py_final();
22+
$finish();
23+
end
24+
endmodule
25+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python3
2+
from scapy.all import *
3+
4+
def gen_pkt(List):
5+
p=Ether(dst='33:32:33:44:55:66',src='33:22:22:34:56:33')/IP(ihl=5)/UDP(dport=0x4a00)/'This is Python Data !'
6+
p.show()
7+
return raw(p)
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
all: clean comp run
3+
4+
clean:
5+
\rm -rf simv* csrc* *.log __pycache__ ucli.key vc_hdrs.h stack.info.*
6+
comp:
7+
vcs -full64 -LDFLAGS -Wl,--no-as-needed -CC -lpython3.6m -lpthread -ldl -lutil -lm -LDFLAGS -lpython3.6m -CC -I/usr/include/python3.6m -CC -I. -sverilog gen_pkt_tb.sv c_call_py.c -l comp.log
8+
run:
9+
export PYTHONPATH=. && ./simv -l run.log
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include<Python.h>
2+
#include<svdpi.h>
3+
#include<stdlib.h>
4+
5+
static PyObject *pModule = NULL;
6+
static PyObject *pFunc = NULL;
7+
static PyObject *pDict = NULL;
8+
static PyObject *pReturn = NULL;
9+
static PyObject *ArgList = NULL;
10+
static PyGILState_STATE py_state;
11+
12+
extern void c_py_init(){
13+
int i;
14+
double cArray[] = {1,2,3,4,5,6,};
15+
Py_Initialize();
16+
17+
py_state = PyGILState_Ensure();
18+
19+
pModule = PyImport_ImportModule("py_gen");
20+
pDict = PyModule_GetDict(pModule);
21+
pFunc = PyDict_GetItemString(pDict,"gen_pkt");
22+
23+
PyObject *PyList = PyList_New(6);
24+
ArgList = PyTuple_New(1);
25+
26+
for(i = 0; i < PyList_Size(PyList); i++){
27+
PyList_SetItem(PyList, i, PyFloat_FromDouble(cArray[i]));
28+
}
29+
30+
PyTuple_SetItem(ArgList, 0, PyList);
31+
printf("initialize Python runtime !\n");
32+
}
33+
34+
extern void c_py_final(){
35+
PyGILState_Release(py_state);
36+
Py_Finalize();
37+
printf("finalize Python runtime !\n");
38+
}
39+
40+
static unsigned char tmp[256] = {0};
41+
extern void c_py_gen_packet(/*output*/ svBitVecVal *pkt, /*output*/ svBitVecVal *len){
42+
int num;
43+
int i;
44+
unsigned char *result;
45+
46+
pReturn = PyObject_CallObject(pFunc, ArgList);
47+
48+
if(PyBytes_Check(pReturn)){
49+
num = PyBytes_Size(pReturn);
50+
result = PyBytes_AsString(pReturn);
51+
//memcpy(pkt,result,num);
52+
for( i = 0; i < num; i++){
53+
tmp[255-i] = result[i];
54+
printf("%02X ",result[i]);
55+
}
56+
memcpy(pkt, tmp, 256);
57+
58+
*len = num;
59+
printf("\n");
60+
} else {
61+
printf("Get No Bytes Form Python !!!\n");
62+
}
63+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
module hello_world;
3+
4+
import "DPI-C" function void c_py_init();
5+
import "DPI-C" function void c_py_final();
6+
import "DPI-C" function void c_py_gen_packet(output bit[2047:0] pkt, output bit [7:0] len);
7+
8+
bit [15:0][127:0] data;
9+
bit [7 :0] len ;
10+
11+
initial begin
12+
c_py_init();
13+
c_py_gen_packet(data,len);
14+
15+
$display("print data in systemverilog !");
16+
17+
$display("get len ='h%h",len );
18+
$display("get data ='h%h",data);
19+
$display("get data = %s",data);
20+
21+
c_py_final();
22+
$finish();
23+
end
24+
endmodule
25+

0 commit comments

Comments
 (0)