@@ -24,28 +24,17 @@ var (
24
24
)
25
25
26
26
const (
27
- IOPortStart = 0x6200
28
- IOPortSize = 0x100
29
-
30
- // The number of free descriptors in virt queue must exceed
31
- // MAX_SKB_FRAGS (16). Otherwise, packet transmission from
32
- // the guest to the host will be stopped.
33
- //
34
- // refs https://github.com/torvalds/linux/blob/5859a2b/drivers/net/virtio_net.c#L1754
35
- QueueSize = 32
27
+ NetIOPortStart = 0x6200
28
+ NetIOPortSize = 0x100
36
29
)
37
30
38
- type IRQInjector interface {
39
- InjectVirtioNetIRQ () error
40
- }
41
-
42
- type Hdr struct {
31
+ type netHdr struct {
43
32
commonHeader commonHeader
44
33
_ netHeader
45
34
}
46
35
47
36
type Net struct {
48
- Hdr Hdr
37
+ Hdr netHdr
49
38
50
39
VirtQueue [2 ]* VirtQueue
51
40
Mem []byte
@@ -60,7 +49,7 @@ type Net struct {
60
49
IRQInjector IRQInjector
61
50
}
62
51
63
- func (h Hdr ) Bytes () ([]byte , error ) {
52
+ func (h netHdr ) Bytes () ([]byte , error ) {
64
53
buf := new (bytes.Buffer )
65
54
66
55
if err := binary .Write (buf , binary .LittleEndian , h ); err != nil {
@@ -70,17 +59,6 @@ func (h Hdr) Bytes() ([]byte, error) {
70
59
return buf .Bytes (), nil
71
60
}
72
61
73
- type commonHeader struct {
74
- _ uint32 // hostFeatures
75
- _ uint32 // guestFeatures
76
- _ uint32 // queuePFN
77
- queueNUM uint16
78
- queueSEL uint16
79
- _ uint16 // queueNotify
80
- _ uint8 // status
81
- isr uint8
82
- }
83
-
84
62
type netHeader struct {
85
63
_ [6 ]uint8 // mac
86
64
_ uint16 // netStatus
@@ -95,7 +73,7 @@ func (v Net) GetDeviceHeader() pci.DeviceHeader {
95
73
SubsystemID : 1 , // Network Card
96
74
Command : 1 , // Enable IO port
97
75
BAR : [6 ]uint32 {
98
- IOPortStart | 0x1 ,
76
+ NetIOPortStart | 0x1 ,
99
77
},
100
78
// https://github.com/torvalds/linux/blob/fb3b0673b7d5b477ed104949450cd511337ba3c6/drivers/pci/setup-irq.c#L30-L55
101
79
InterruptPin : 1 ,
@@ -105,7 +83,7 @@ func (v Net) GetDeviceHeader() pci.DeviceHeader {
105
83
}
106
84
107
85
func (v Net ) IOInHandler (port uint64 , bytes []byte ) error {
108
- offset := int (port - IOPortStart )
86
+ offset := int (port - NetIOPortStart )
109
87
110
88
b , err := v .Hdr .Bytes ()
111
89
if err != nil {
@@ -260,7 +238,7 @@ func (v *Net) Tx() error {
260
238
}
261
239
262
240
func (v * Net ) IOOutHandler (port uint64 , bytes []byte ) error {
263
- offset := int (port - IOPortStart )
241
+ offset := int (port - NetIOPortStart )
264
242
265
243
switch offset {
266
244
case 8 :
@@ -281,12 +259,12 @@ func (v *Net) IOOutHandler(port uint64, bytes []byte) error {
281
259
}
282
260
283
261
func (v Net ) GetIORange () (start , end uint64 ) {
284
- return IOPortStart , IOPortStart + IOPortSize
262
+ return NetIOPortStart , NetIOPortStart + NetIOPortSize
285
263
}
286
264
287
265
func NewNet (irq uint8 , irqInjector IRQInjector , tap io.ReadWriter , mem []byte ) * Net {
288
266
res := & Net {
289
- Hdr : Hdr {
267
+ Hdr : netHdr {
290
268
commonHeader : commonHeader {
291
269
queueNUM : QueueSize ,
292
270
isr : 0x0 ,
@@ -306,33 +284,3 @@ func NewNet(irq uint8, irqInjector IRQInjector, tap io.ReadWriter, mem []byte) *
306
284
307
285
return res
308
286
}
309
-
310
- // refs: https://wiki.osdev.org/Virtio#Virtual_Queue_Descriptor
311
- type VirtQueue struct {
312
- DescTable [QueueSize ]struct {
313
- Addr uint64
314
- Len uint32
315
- Flags uint16
316
- Next uint16
317
- }
318
-
319
- AvailRing struct {
320
- Flags uint16
321
- Idx uint16
322
- Ring [QueueSize ]uint16
323
- UsedEvent uint16
324
- }
325
-
326
- // padding for 4096 byte alignment
327
- _ [4096 - ((16 * QueueSize + 6 + 2 * QueueSize ) % 4096 )]uint8
328
-
329
- UsedRing struct {
330
- Flags uint16
331
- Idx uint16
332
- Ring [QueueSize ]struct {
333
- Idx uint32
334
- Len uint32
335
- }
336
- availEvent uint16
337
- }
338
- }
0 commit comments