forked from luigirizzo/netmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonepacket.py
More file actions
49 lines (38 loc) · 1.21 KB
/
onepacket.py
File metadata and controls
49 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# Written by Vincenzo Maffione <v.maffione AT gmail DOT com>
import netmap
import time
# open a netmap interface and register
desc = netmap.NetmapDesc('netmap:enp2s0f1')
while 1:
print('Waiting for a packet to come')
# sync RX rings with kernel
desc.rxsync()
# scan all the receive rings
rxr = None
for r in desc.receive_rings:
if r.head != r.tail:
# At least one packet has been received on
# this ring
rxr = r
break
if rxr == None:
# no packets received on the rings, let's sleep a bit
time.sleep(1)
continue
# slot pointed by rxr.head has been received
# and can be extracted
slot = rxr.slots[rxr.head]
print('Received a packet with len %d' % (slot.len))
# convert the buffer associated to the slot to
# a string of hexadecimal digits, up to the received length
pktstr = ''
for b in slot.buf[:slot.len].tolist():
pktstr += '%02x' % (b)
# print the received packet
print(pktstr)
# update head and cur, managing index wraparound
rxr.head = rxr.head + 1
if rxr.head >= rxr.num_slots:
rxr.head -= rxr.num_slots
rxr.cur = rxr.head