Skip to content

Commit

Permalink
Merge pull request #571 from intel-go/develop
Browse files Browse the repository at this point in the history
Release 0.8
  • Loading branch information
gshimansky authored Mar 7, 2019
2 parents d7badc5 + b2e018e commit 048b92a
Show file tree
Hide file tree
Showing 118 changed files with 3,750 additions and 1,684 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN apt-get -q update && apt-get -q -y install \
libmnl-dev \
libibverbs-dev

RUN cd /opt && curl -L -s https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz | tar zx
RUN cd /opt && curl -L -s https://dl.google.com/go/go1.11.5.linux-amd64.tar.gz | tar zx

RUN mkdir -p ${NFF_GO}
COPY . ${NFF_GO}
Expand Down
329 changes: 6 additions & 323 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,332 +7,9 @@
package common

import (
"fmt"
"io"
"log"
"math"
"os"
"strconv"

"github.com/pkg/errors"
)

// Max array length for type conversions
const MaxLength = math.MaxInt32

// Length of addresses.
const (
EtherAddrLen = 6
IPv4AddrLen = 4
IPv6AddrLen = 16
)

// Supported EtherType for L2
const (
IPV4Number = 0x0800
ARPNumber = 0x0806
VLANNumber = 0x8100
MPLSNumber = 0x8847
IPV6Number = 0x86dd

SwapIPV4Number = 0x0008
SwapARPNumber = 0x0608
SwapVLANNumber = 0x0081
SwapMPLSNumber = 0x4788
SwapIPV6Number = 0xdd86
)

// Supported L4 types
const (
ICMPNumber = 0x01
IPNumber = 0x04
TCPNumber = 0x06
UDPNumber = 0x11
ICMPv6Number = 0x3a
NoNextHeader = 0x3b
)

// Supported ICMP Types
const (
ICMPTypeEchoRequest uint8 = 8
ICMPTypeEchoResponse uint8 = 0
ICMPv6TypeEchoRequest uint8 = 128
ICMPv6TypeEchoResponse uint8 = 129
ICMPv6NeighborSolicitation uint8 = 135
ICMPv6NeighborAdvertisement uint8 = 136
)

// These constants keep length of supported headers in bytes.
//
// IPv6Len - minimum length of IPv6 header in bytes. It can be higher and it
// is not determined inside packet. Only default minimum size is used.
//
// IPv4MinLen and TCPMinLen are used only in packet generation functions.
//
// In parsing we take actual length of TCP header from DataOff field and length of
// IPv4 take from Ihl field.
const (
EtherLen = 14
VLANLen = 4
MPLSLen = 4
IPv4MinLen = 20
IPv6Len = 40
ICMPLen = 8
TCPMinLen = 20
UDPLen = 8
ARPLen = 28
GTPMinLen = 8
)

const (
TCPMinDataOffset = 0x50 // minimal tcp data offset
IPv4VersionIhl = 0x45 // IPv4, IHL = 5 (min header len)
IPv6VtcFlow = 0x60 // IPv6 version
)

// LogType - type of logging, used in flow package
type LogType uint8

const (
// No - no output even after fatal errors
No LogType = 1 << iota
// Initialization - output during system initialization
Initialization = 2
// Debug - output during execution one time per time period (scheduler ticks)
Debug = 4
// Verbose - output during execution as soon as something happens. Can influence performance
Verbose = 8
)

// TCPFlags contains set TCP flags.
type TCPFlags uint8

// Constants for valuues of TCP flags.
const (
TCPFlagFin = 0x01
TCPFlagSyn = 0x02
TCPFlagRst = 0x04
TCPFlagPsh = 0x08
TCPFlagAck = 0x10
TCPFlagUrg = 0x20
TCPFlagEce = 0x40
TCPFlagCwr = 0x80
)

// ErrorCode type for codes of errors
type ErrorCode int

// constants with error codes
const (
_ ErrorCode = iota
Fail
ParseCPUListErr
ReqTooManyPorts
BadArgument
UseNilFlowErr
UseClosedFlowErr
OpenedFlowAtTheEnd
PortHasNoQueues
NotAllQueuesUsed
FailToInitPort
ParseRuleJSONErr
FileErr
ParseRuleErr
IncorrectArgInRules
IncorrectRule
AllocMbufErr
PktMbufHeadRoomTooSmall
NotEnoughCores
CreatePortErr
MaxCPUExceedErr
PcapReadFail
PcapWriteFail
InvalidCPURangeErr
SetAffinityErr
MultipleReceivePort
MultipleKNIPort
WrongPort
FailToInitDPDK
FailToCreateKNI
FailToReleaseKNI
)

// NFError is error type returned by nff-go functions
type NFError struct {
Code ErrorCode
Message string
CauseErr error
}

type causer interface {
Cause() error
}

// Error method to implement error interface
func (err NFError) Error() string {
return fmt.Sprintf("%s (%d)", err.Message, err.Code)
}

// GetNFErrorCode returns value of cCode field if err is
// NFError or pointer to it and -1 otherwise.
func GetNFErrorCode(err error) ErrorCode {
if nferr := GetNFError(err); nferr != nil {
return nferr.Code
}
return -1
}

func checkAndGetNFErrPointer(err error) *NFError {
if err != nil {
if nferr, ok := err.(NFError); ok {
return &nferr
} else if nferr, ok := err.(*NFError); ok {
return nferr
}
}
return nil
}

// GetNFError if error is NFerror or pointer to int
// returns pointer to NFError, otherwise returns nil.
func GetNFError(err error) (nferr *NFError) {
nferr = checkAndGetNFErrPointer(err)
if nferr == nil {
if cause, ok := err.(causer); ok {
nferr = checkAndGetNFErrPointer(cause.Cause())
}
}
return nferr
}

// Cause returns the underlying cause of error, if
// possible. If not, returns err itself.
func (err *NFError) Cause() error {
if err == nil {
return nil
}
if err.CauseErr != nil {
if cause, ok := err.CauseErr.(causer); ok {
return cause.Cause()
}
return err.CauseErr
}
return err
}

// Format makes formatted printing of errors,
// the following verbs are supported:
// %s, %v print the error. If the error has a
// Cause it will be printed recursively
// %+v - extended format. Each Frame of the error's
// StackTrace will be printed in detail if possible.
func (err *NFError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
if cause := err.Cause(); cause != err && cause != nil {
fmt.Fprintf(s, "%+v\n", err.Cause())
io.WriteString(s, err.Message)
return
}
}
fallthrough
case 's', 'q':
io.WriteString(s, err.Error())
}
}

// WrapWithNFError returns an error annotating err with a stack trace
// at the point WrapWithNFError is called, and the next our NFError.
// If err is nil, Wrap returns nil.
func WrapWithNFError(err error, message string, code ErrorCode) error {
err = &NFError{
CauseErr: err,
Message: message,
Code: code,
}
return errors.WithStack(err)
}

var currentLogType = No | Initialization | Debug

// LogFatal internal, used in all packages
func LogFatal(logType LogType, v ...interface{}) {
if logType&currentLogType != 0 {
t := fmt.Sprintln(v...)
log.Fatal("ERROR: ", t)
}
os.Exit(1)
}

// LogFatalf is a wrapper at LogFatal which makes formatting before logger.
func LogFatalf(logType LogType, format string, v ...interface{}) {
LogFatal(logType, fmt.Sprintf(format, v...))
}

// LogError internal, used in all packages
func LogError(logType LogType, v ...interface{}) string {
if logType&currentLogType != 0 {
t := fmt.Sprintln(v...)
log.Print("ERROR: ", t)
return t
}
return ""
}

// LogWarning internal, used in all packages
func LogWarning(logType LogType, v ...interface{}) {
if logType&currentLogType != 0 {
t := fmt.Sprintln(v...)
log.Print("WARNING: ", t)
}
}

// LogDebug internal, used in all packages
func LogDebug(logType LogType, v ...interface{}) {
if logType&currentLogType != 0 {
t := fmt.Sprintln(v...)
log.Print("DEBUG: ", t)
}
}

// LogDrop internal, used in all packages
func LogDrop(logType LogType, v ...interface{}) {
if logType&currentLogType != 0 {
t := fmt.Sprintln(v...)
log.Print("DROP: ", t)
}
}

// LogTitle internal, used in all packages
func LogTitle(logType LogType, v ...interface{}) {
if logType&currentLogType != 0 {
log.Print(v...)
}
}

// SetLogType internal, used in flow package
func SetLogType(logType LogType) {
log.SetFlags(0)
currentLogType = logType
}

// GetDPDKLogLevel internal, used in flow package
func GetDPDKLogLevel() string {
switch currentLogType {
case No:
return "0"
case No | Initialization:
return "7"
case No | Initialization | Debug:
return "8"
case No | Initialization | Debug | Verbose:
return "8"
default:
return "8"
}
}

// GetDefaultCPUs returns default core list {0, 1, ..., NumCPU}
func GetDefaultCPUs(cpuNumber int) []int {
cpus := make([]int, cpuNumber, cpuNumber)
Expand Down Expand Up @@ -420,3 +97,9 @@ func dropInvalidCPUs(nums []int, maxcpu int) []int {
}
return nums[:i]
}

// RXTXStats describes statistics for sender or receiver flow function
// node.
type RXTXStats struct {
PacketsProcessed, PacketsDropped, BytesProcessed uint64
}
Loading

0 comments on commit 048b92a

Please sign in to comment.