-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinterface.go
48 lines (40 loc) · 1.05 KB
/
interface.go
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
package gonetmap
import (
"os"
"unsafe"
)
type NetmapIf struct {
Name [16]byte
Version uint32
Flags uint32
TxRings uint32
RxRings uint32
BufsHead uint32
Spare1 [5]uint32
RingOffset unsafe.Pointer // NmRing is here
}
type Interface struct {
File *os.File
Nif *NetmapIf
}
func (i *Interface) ring(idx uint32) uintptr {
ptr := unsafe.Pointer(uintptr(unsafe.Pointer(i.Nif)) + unsafe.Offsetof(i.Nif.RingOffset))
h := *(*[]uintptr)(PtrSliceFrom(ptr, int(i.Nif.TxRings+i.Nif.RxRings+2)))
return uintptr(unsafe.Pointer(i.Nif)) + h[idx]
}
func (i *Interface) GetRing(ringIndex interface{}, direction Direction) *NetmapRing {
var ringPtr uintptr
idx := ifaceTOuint32(ringIndex)
if direction == TX {
ringPtr = i.ring(idx)
} else {
ringPtr = i.ring(idx + i.Nif.TxRings + 1)
}
return (*NetmapRing)(unsafe.Pointer(ringPtr))
}
func (i *Interface) RxSync() error {
return NmIoctl(i.File, NRxSync, unsafe.Pointer(uintptr(0)))
}
func (i *Interface) TxSync() error {
return NmIoctl(i.File, NTxSync, unsafe.Pointer(uintptr(0)))
}