Skip to content

Commit e30524b

Browse files
authored
Merge pull request #487 from intel-go/develop
Release v0.6.0
2 parents 1fbebd4 + 24d9c83 commit e30524b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+3803
-4829
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ script:
2121
- docker exec -i test-nff-go make
2222
# Build standalone examples
2323
- docker exec -i test-nff-go bash -c "cd examples && make gopacketParserExample && cd .."
24-
- docker exec -i test-nff-go bash -c "cd test/localTesting && make && cd -"
24+
- docker exec -i test-nff-go bash -c "cd examples && make nffPktgen && cd -"
2525
- docker exec -i test-nff-go make -C examples/dpi
2626
# Run unit tests
2727
- docker exec -i test-nff-go sysctl -w vm.nr_hugepages=1024

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ TESTING_TARGETS = $(CI_TESTING_TARGETS) test/stability
1010

1111
all: $(SUBDIRS)
1212

13+
debug:
14+
$(MAKE) all NFF_GO_DEBUG=y
15+
1316
dpdk: nff-go-base
1417

1518
test: dpdk

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ Use Go version 1.9 or higher. To check the version of Go, do:
139139
cd $GOPATH/src/github.com/intel-go/nff-go
140140
make -j8
141141

142+
## Building NFF-GO in debug mode
143+
144+
make debug -j8
145+
142146
# Running NFF-GO
143147

144148
## Documentation
@@ -202,7 +206,7 @@ downloaded automatically.
202206

203207
If you want to contribute to NFF-Go, check our [Contributing
204208
guide](https://github.com/intel-go/yanff/blob/master/CONTRIBUTING.md). We also
205-
recommend checking the 'janitorial' bugs in our list of open issues; these bugs
209+
recommend checking the bugs with 'help-wanted' or 'easyfix' in our list of open issues; these bugs
206210
can be solved without an extensive knowledge of NFF-Go. We would love to help
207211
you start contributing.
208212

common/common.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,18 @@ const (
4848
IPNumber = 0x04
4949
TCPNumber = 0x06
5050
UDPNumber = 0x11
51-
NoNextHeader = 0x3B
51+
ICMPv6Number = 0x3a
52+
NoNextHeader = 0x3b
5253
)
5354

5455
// Supported ICMP Types
5556
const (
56-
ICMPTypeEchoRequest uint8 = 8
57-
ICMPTypeEchoResponse uint8 = 0
57+
ICMPTypeEchoRequest uint8 = 8
58+
ICMPTypeEchoResponse uint8 = 0
59+
ICMPv6TypeEchoRequest uint8 = 128
60+
ICMPv6TypeEchoResponse uint8 = 129
61+
ICMPv6NeighborSolicitation uint8 = 135
62+
ICMPv6NeighborAdvertisement uint8 = 136
5863
)
5964

6065
// These constants keep length of supported headers in bytes.
@@ -92,11 +97,11 @@ const (
9297
// No - no output even after fatal errors
9398
No LogType = 1 << iota
9499
// Initialization - output during system initialization
95-
Initialization
100+
Initialization = 2
96101
// Debug - output during execution one time per time period (scheduler ticks)
97-
Debug
102+
Debug = 4
98103
// Verbose - output during execution as soon as something happens. Can influence performance
99-
Verbose
104+
Verbose = 8
100105
)
101106

102107
// TCPFlags contains set TCP flags.
@@ -147,6 +152,8 @@ const (
147152
MultipleReceivePort
148153
MultipleKNIPort
149154
WrongPort
155+
FailToInitDPDK
156+
FailToCreateKNI
150157
)
151158

152159
// NFError is error type returned by nff-go functions

devices/bind.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2018 Intel Corporation.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package devices helps to query DPDK compatibles devices and to bind/unbind drivers
6+
package devices
7+
8+
// Device is a DPDK compatible device and should be able to bind, unbind and
9+
// probe.
10+
type Device interface {
11+
// Binds a driver to the device
12+
Bind(driver string) error
13+
// Unbinds the current driver from the device
14+
Unbind() error
15+
// Returns the name of the driver that is currently bound
16+
CurrentDriver() (string, error)
17+
// Probes the currently bound driver and checks if there is an error
18+
Probe() error
19+
// Returns the ID of the device
20+
ID() string
21+
}
22+
23+
// New returns a corresponding device by given input
24+
func New(input string) (Device, error) {
25+
switch {
26+
case IsPciID.Match([]byte(input)):
27+
return NewDeviceByPciID(input)
28+
case IsUUID.Match([]byte(input)):
29+
return NewDeviceByVmbusID(input)
30+
default:
31+
return NewDeviceByNicName(input)
32+
}
33+
}
34+
35+
// NewDeviceByPciID returns a PCI device by given PCI ID
36+
func NewDeviceByPciID(pciID string) (Device, error) {
37+
device, err := GetPciDeviceByPciID(pciID)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
return device, nil
43+
}
44+
45+
// NewDeviceByVmbusID returns a VMBus device by given UUID
46+
func NewDeviceByVmbusID(uuid string) (Device, error) {
47+
device, err := GetVmbusDeviceByUUID(uuid)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
return device, nil
53+
}
54+
55+
// NewDeviceByNicName returns a device by given NIC name, e.g. eth0.
56+
func NewDeviceByNicName(nicName string) (Device, error) {
57+
devID, err := GetDeviceID(nicName)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
device, err := newDevice(devID)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
return device, nil
68+
}
69+
70+
func newDevice(id string) (Device, error) {
71+
if IsPciID.Match([]byte(id)) {
72+
return GetPciDeviceByPciID(id)
73+
}
74+
return GetVmbusDeviceByUUID(id)
75+
}

devices/consts.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2018 Intel Corporation.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package devices helps to query DPDK compatibles devices and to bind/unbind drivers
6+
package devices
7+
8+
import (
9+
"fmt"
10+
"regexp"
11+
)
12+
13+
// Driver names
14+
const (
15+
DriverHvNetvcs = "hv_netvcs"
16+
DriverUioPciGeneric = "uio_pci_generic"
17+
DriverIgUio = "ig_uio"
18+
DriverVfioPci = "vfio-pci"
19+
DriverUioHvGeneric = "uio_hv_generic"
20+
)
21+
22+
// Path to PCI
23+
const (
24+
PathSysPciDevices = "/sys/bus/pci/devices"
25+
PathSysPciDrivers = "/sys/bus/pci/drivers"
26+
PathSysPciDriverProbe = "/sys/bus/pci/drivers_probe"
27+
)
28+
29+
// Path to VMBus
30+
const (
31+
PathSysVmbusDevices = "/sys/bus/vmbus/devices"
32+
PathSysVmbusDrivers = "/sys/bus/vmbus/drivers"
33+
)
34+
35+
// Path to net
36+
const (
37+
PathSysClassNet = "/sys/class/net"
38+
)
39+
40+
// Regular expressions for PCI-ID and UUID
41+
var (
42+
IsPciID *regexp.Regexp
43+
IsUUID *regexp.Regexp
44+
)
45+
46+
// DPDK related drivers
47+
var (
48+
DefaultDpdkDriver = DriverUioPciGeneric
49+
DpdkDrivers = [...]string{DriverUioPciGeneric, DriverIgUio, DriverVfioPci, DriverUioHvGeneric}
50+
DpdkPciDrivers = [...]string{DriverUioPciGeneric, DriverIgUio, DriverVfioPci}
51+
DpdkVmbusDrivers = [...]string{DriverUioHvGeneric}
52+
)
53+
54+
type stringBuilder string
55+
56+
func (s stringBuilder) With(args ...interface{}) string {
57+
return fmt.Sprintf(string(s), args...)
58+
}
59+
60+
var (
61+
pathSysPciDevicesBind stringBuilder = PathSysPciDevices + "/%s/driver/bind"
62+
pathSysPciDevicesUnbind stringBuilder = PathSysPciDevices + "/%s/driver/unbind"
63+
pathSysPciDevicesOverrideDriver stringBuilder = PathSysPciDevices + "/%s/driver_override"
64+
65+
pathSysPciDriversBind stringBuilder = PathSysPciDrivers + "/%s/bind"
66+
pathSysPciDriversUnbind stringBuilder = PathSysPciDrivers + "/%s/unbind"
67+
pathSysPciDriversNewID stringBuilder = PathSysPciDrivers + "/%s/new_id"
68+
)
69+
70+
var (
71+
pathSysVmbusDriversBind stringBuilder = PathSysVmbusDrivers + "/%s/bind"
72+
pathSysVmbusDriversUnbind stringBuilder = PathSysVmbusDrivers + "/%s/unbind"
73+
pathSysVmbusDriversNewID stringBuilder = PathSysVmbusDrivers + "/%s/new_id"
74+
)
75+
76+
var (
77+
pathSysClassNetDeviceDriver stringBuilder = PathSysClassNet + "/%s/device/driver"
78+
pathSysClassNetDevice stringBuilder = PathSysClassNet + "/%s/device"
79+
)
80+
81+
var (
82+
vmbusDeviceStringer stringBuilder = "ID: %s\nClass:\t%s\nDriver:\t%s"
83+
pciDeviceStringer stringBuilder = "ID: %s\nClass:\t%s\nVendor:\t%s\nDevice:\t%s\nDriver:\t%s"
84+
)
85+
86+
var (
87+
// for ethtool output
88+
rPciIDForEthtool *regexp.Regexp
89+
90+
// for lspci output
91+
rPciClass *regexp.Regexp
92+
rPciVendor *regexp.Regexp
93+
rPciDevice *regexp.Regexp
94+
rPciDriver *regexp.Regexp
95+
96+
// for find output
97+
rVmbusDriver *regexp.Regexp
98+
)
99+
100+
func init() {
101+
rPciIDForEthtool = regexp.MustCompile("bus-info:\\s([0-9:.]+)")
102+
103+
rPciClass = regexp.MustCompile("[Cc]lass:\\s([0-9a-zA-Z]{4})")
104+
rPciVendor = regexp.MustCompile("[Vv]endor:\\s([0-9a-zA-Z]{4})")
105+
rPciDevice = regexp.MustCompile("[Dd]evice:\\s([0-9a-zA-Z]{4})")
106+
rPciDriver = regexp.MustCompile("[Dd]river:\\s(\\S+)")
107+
108+
rVmbusDriver = regexp.MustCompile("/sys/bus/vmbus/drivers/(\\S+)/")
109+
110+
IsPciID = regexp.MustCompile("^\\d{4}:\\d{2}:\\d{2}.\\d$")
111+
IsUUID = regexp.MustCompile("^[[:xdigit:]]{8}-[[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}$")
112+
}

devices/errno.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 Intel Corporation.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package devices helps to query DPDK compatibles devices and to bind/unbind drivers
6+
package devices
7+
8+
import (
9+
"errors"
10+
)
11+
12+
// Errors of devices package
13+
var (
14+
ErrNoBoundDriver = errors.New("no driver is bound to the device")
15+
ErrAlreadyBoundDriver = errors.New("device has already bound the selected driver")
16+
ErrBind = errors.New("fail to bind the driver")
17+
ErrUnbind = errors.New("fail to unbind the driver")
18+
ErrUnsupportedDriver = errors.New("unsupported DPDK driver")
19+
ErrNotProbe = errors.New("device doesn't support 'drive_probe'")
20+
ErrKernelModuleNotLoaded = errors.New("kernel module is not loaded")
21+
)

devices/misc.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2018 Intel Corporation.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package devices helps to query DPDK compatibles devices and to bind/unbind drivers
6+
package devices
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"os"
12+
"os/exec"
13+
"path/filepath"
14+
"strings"
15+
"time"
16+
)
17+
18+
var defaultTimeoutLimitation = 5 * time.Second
19+
20+
// FindDefaultDpdkDriver returns a default DPDK driver that the given NIC can
21+
// use.
22+
func FindDefaultDpdkDriver(nicName string) string {
23+
driver, err := readlinkBaseCmd(pathSysClassNetDeviceDriver.With(nicName))
24+
if err != nil {
25+
return DefaultDpdkDriver
26+
}
27+
switch driver {
28+
case DriverHvNetvcs:
29+
return DriverUioHvGeneric
30+
default:
31+
return DefaultDpdkDriver
32+
}
33+
}
34+
35+
// GetDeviceID returns the device ID of given NIC name.
36+
func GetDeviceID(nicName string) (string, error) {
37+
// DEV_ID=$(basename $(readlink /sys/class/net/<nicName>/device))
38+
return readlinkBaseCmd(pathSysClassNetDevice.With(nicName))
39+
}
40+
41+
// IsModuleLoaded checks if the kernel has already loaded the driver or not.
42+
func IsModuleLoaded(driver string) bool {
43+
output, err := cmdOutputWithTimeout(defaultTimeoutLimitation, "lsmod")
44+
if err != nil {
45+
// Can't run lsmod, return false
46+
return false
47+
}
48+
lines := strings.Split(string(output), "\n")
49+
for _, line := range lines {
50+
if checkModuleLine(line, driver) {
51+
return true
52+
}
53+
}
54+
return false
55+
}
56+
57+
func writeToTargetWithData(sysfs string, flag int, mode os.FileMode, data string) error {
58+
writer, err := os.OpenFile(sysfs, flag, mode)
59+
if err != nil {
60+
return fmt.Errorf("OpenFile failed: %s", err.Error())
61+
}
62+
defer writer.Close()
63+
64+
_, err = writer.Write([]byte(data))
65+
if err != nil {
66+
return fmt.Errorf("WriteFile failed: %s", err.Error())
67+
}
68+
69+
return nil
70+
}
71+
72+
func readlinkBaseCmd(path string) (string, error) {
73+
output, err := cmdOutputWithTimeout(defaultTimeoutLimitation, "readlink", path)
74+
if err != nil {
75+
return "", fmt.Errorf("Cmd Execute readlink failed: %s", err.Error())
76+
}
77+
outputStr := strings.Trim(string(output), "\n")
78+
result := filepath.Base(outputStr)
79+
return result, nil
80+
}
81+
82+
func checkModuleLine(line, driver string) bool {
83+
if !strings.Contains(line, driver) {
84+
return false
85+
}
86+
elements := strings.Split(line, " ")
87+
return elements[0] == driver
88+
}
89+
90+
func cmdOutputWithTimeout(duration time.Duration, cmd string, parameters ...string) ([]byte, error) {
91+
ctx, cancel := context.WithTimeout(context.Background(), duration)
92+
defer cancel()
93+
output, err := exec.CommandContext(ctx, cmd, parameters...).Output()
94+
if err != nil {
95+
return nil, err
96+
}
97+
return output, nil
98+
}

0 commit comments

Comments
 (0)