/
ealinit.go
124 lines (110 loc) · 2.91 KB
/
ealinit.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Package ealinit initializes DPDK EAL and SPDK main thread.
package ealinit
/*
#include "../../csrc/dpdk/mbuf.h"
#include <rte_eal.h>
#include <rte_lcore.h>
#include <rte_version.h>
*/
import "C"
import (
"fmt"
"os"
"runtime"
"strings"
"sync"
"github.com/kballard/go-shellquote"
"github.com/usnistgov/ndn-dpdk/core/cptr"
"github.com/usnistgov/ndn-dpdk/core/logging"
"github.com/usnistgov/ndn-dpdk/dpdk/eal"
"github.com/usnistgov/ndn-dpdk/dpdk/ealconfig"
"github.com/usnistgov/ndn-dpdk/dpdk/spdkenv"
"go.uber.org/zap"
)
var logger = logging.New("ealinit")
func init() {
eal.Version = strings.TrimPrefix(C.GoString(C.rte_version()), "DPDK ")
ealconfig.PmdPath = C.RTE_EAL_PMD_PATH
}
var (
initOnce sync.Once
initError error
)
// Init initializes DPDK and SPDK.
// args should not include program name.
func Init(args []string) error {
initOnce.Do(func() {
updateLogLevels()
initLogStream()
ret := make(chan any)
go func() {
runtime.LockOSThread()
if e := initEal(args); e != nil {
ret <- e
return
}
initMbufDynfields()
if e := spdkenv.InitEnv(); e != nil {
ret <- e
return
}
spdkenv.InitMainThread(ret) // never returns
}()
rv := <-ret
switch rv := rv.(type) {
case error:
initError = rv
return
case *spdkenv.Thread:
eal.MainThread, eal.MainReadSide = rv, rv.RcuReadSide
default:
panic(rv)
}
updateLogLevels()
eal.CallMain(func() { logger.Debug("MainThread is running") })
initError = spdkenv.InitFinal()
})
if initError != nil {
logger.Error("EAL init error", zap.Error(initError), zap.String("args", shellquote.Join(args...)))
}
return initError
}
func initEal(args []string) error {
exe, e := os.Executable()
if e != nil {
exe = os.Args[0]
}
a := cptr.NewCArgs(append([]string{exe}, args...))
defer a.Close()
C.rte_mp_disable()
if res := C.rte_eal_init(C.int(a.Argc), (**C.char)(a.Argv)); res < 0 {
return fmt.Errorf("rte_eal_init %w", eal.GetErrno())
}
lcoreSockets := map[int]int{}
for lcID := C.rte_get_next_lcore(C.RTE_MAX_LCORE, 1, 1); lcID < C.RTE_MAX_LCORE; lcID = C.rte_get_next_lcore(lcID, 1, 0) {
lcoreSockets[int(lcID)] = int(C.rte_lcore_to_socket_id(lcID))
}
eal.UpdateLCoreSockets(lcoreSockets, int(C.rte_get_main_lcore()))
eal.InitTscUnit()
logger.Info("EAL ready",
zap.String("args", shellquote.Join(args...)),
eal.MainLCore.ZapField("main"),
zap.Array("workers", eal.Workers),
zap.Any("sockets", eal.Sockets),
zap.Bool("has-hugepages", C.rte_eal_has_hugepages() != 0),
zap.Bool("has-pci", C.rte_eal_has_pci() != 0),
zap.String("iova-mode", map[C.enum_rte_iova_mode]string{
C.RTE_IOVA_DC: "DC",
C.RTE_IOVA_PA: "PA",
C.RTE_IOVA_VA: "VA",
}[C.rte_eal_iova_mode()]),
zap.String("runtime-dir", C.GoString(C.rte_eal_get_runtime_dir())),
)
return nil
}
func initMbufDynfields() {
ok := bool(C.Mbuf_RegisterDynFields())
if !ok {
logger.Fatal("mbuf dynfields init error", zap.Error(eal.GetErrno()))
}
}