<commit_before>// Copyright 2017 Intel Corporation.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"errors"
	"flag"
	"fmt"
	"log"
	"sync"
	"sync/atomic"
	"time"

	"github.com/intel-go/nff-go/flow"
	"github.com/intel-go/nff-go/packet"
	"github.com/intel-go/nff-go/test/stability/stabilityCommon"
)

// testScenario=1:
// Firstly packets are generated with different UDP destination addresses,
// filled with IPv4 and UDP checksums and sent to 0 port.
// Secondly packets are received on 0 and 1 ports, verified and calculated.
// Expects to get exact proportion of input packets.
//
// testScenario=2:
// Packets are received on 0 port (from first part), divided according to testType
// and sent back for checking.
//
// testScenario=0:
// Combination of above parts at one machine. Is used by go test.
// Packets are generated, divided and calculated in the same pipeline.

const (
	gotest uint = iota
	generatePart
	receivePart
)

const (
	separate uint = iota
	vseparate
	split
	vsplit
	partition
	vpartition
	pcopy
	handles
	handle
	vhandle
	dhandle
	vdhandle
)

const vecSize = 32

var (
	totalPackets     uint64
	userTotalPackets uint64 = 10000000
	payloadSize      uint   = 16
	speed            uint64 = 1000000

	recvPacketsGroup1 uint64
	recvPacketsGroup2 uint64
	count             uint64
	countRes          uint64
	recvPackets       uint64
	brokenPackets     uint64

	dstPort1 uint16 = 111
	dstPort2 uint16 = 222
	dstPort3 uint16 = 333

	testDoneEvent *sync.Cond
	progStart     time.Time

	// T second timeout is used to let generator reach required speed
	// During timeout packets are skipped and not counted
	T = 10 * time.Second

	outport1     uint
	outport2     uint
	inport1      uint
	inport2      uint
	dpdkLogLevel = "--log-level=0"

	fixMACAddrs  func(*packet.Packet, flow.UserContext)
	fixMACAddrs1 func(*packet.Packet, flow.UserContext)
	fixMACAddrs2 func(*packet.Packet, flow.UserContext)

	l3Rules     *packet.L3Rules
	gTestType   uint
	lessPercent int
	passedLimit uint64
	delta       int = 2
	checkSlice  []uint8
	shift       uint64
)

type packetData struct {
	F1, F2 uint64
}

func main() {
	var testScenario uint
	var testType uint
	flag.UintVar(&testScenario, "testScenario", 0, "1 to use 1 as GENERATE part, 2 to use as RECEIVE part, 0 to use as ONE-MACHINE variant")
	flag.UintVar(&testType, "testType", 0, "0 - Separate test, 1 - Split test, 2 - Partition test, 3 - pCopy test, 4 - Handle test, 5 - DHandle test")
	flag.Uint64Var(&speed, "speed", speed, "speed of generator, Pkts/s")
	flag.UintVar(&outport1, "outport1", 0, "port for 1st sender")
	flag.UintVar(&outport2, "outport2", 1, "port for 2nd sender")
	flag.UintVar(&inport1, "inport1", 0, "port for 1st receiver")
	flag.UintVar(&inport2, "inport2", 1, "port for 2nd receiver")
	flag.Uint64Var(&userTotalPackets, "number", userTotalPackets, "total number of packets to receive by test")
	flag.DurationVar(&T, "timeout", T, "test start delay, needed to stabilize speed. Packets sent during timeout do not affect test result")
	configFile := flag.String("config", "", "Specify json config file name (mandatory for VM)")
	target := flag.String("target", "", "Target host name from config file (mandatory for VM)")
	flag.StringVar(&dpdkLogLevel, "dpdk", "--log-level=0", "Passes an arbitrary argument to dpdk EAL")
	flag.Parse()

	if err := initDPDK(); err != nil {
		fmt.Printf("fail: %+v\n", err)
	}
	if err := executeTest(*configFile, *target, testScenario, testType); err != nil {
		fmt.Printf("fail: %+v\n", err)
	}
}

func initDPDK() error {
	// Init NFF-GO system
	config := flow.Config{
		DPDKArgs: []string{dpdkLogLevel},
	}
	if err := flow.SystemInit(&config); err != nil {
		return err
	}
	return nil
}

func executeTest(configFile, target string, testScenario uint, testType uint) error {
	if testScenario > 3 || testScenario < 0 {
		return errors.New("testScenario should be in interval [0, 3]")
	}
	gTestType = testType
	countRes = 0
	recvPacketsGroup1 = 0
	recvPacketsGroup2 = 0
	recvPackets = 0
	count = 0
	brokenPackets = 0

	checkSlice = make([]uint8, userTotalPackets*2, userTotalPackets*2)

	stabilityCommon.InitCommonState(configFile, target)
	fixMACAddrs = stabilityCommon.ModifyPacket[outport1].(func(*packet.Packet, flow.UserContext))
	fixMACAddrs1 = stabilityCommon.ModifyPacket[outport1].(func(*packet.Packet, flow.UserContext))
	fixMACAddrs2 = stabilityCommon.ModifyPacket[outport2].(func(*packet.Packet, flow.UserContext))

	if err := setParameters(testScenario); err != nil {<commit_msg>Fixed a bug 'index out of range' when port is more than 1 in testSingleFF<commit_after>// Copyright 2017 Intel Corporation.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"errors"
	"flag"
	"fmt"
	"log"
	"sync"
	"sync/atomic"
	"time"

	"github.com/intel-go/nff-go/flow"
	"github.com/intel-go/nff-go/packet"
	"github.com/intel-go/nff-go/test/stability/stabilityCommon"
)

// testScenario=1:
// Firstly packets are generated with different UDP destination addresses,
// filled with IPv4 and UDP checksums and sent to 0 port.
// Secondly packets are received on 0 and 1 ports, verified and calculated.
// Expects to get exact proportion of input packets.
//
// testScenario=2:
// Packets are received on 0 port (from first part), divided according to testType
// and sent back for checking.
//
// testScenario=0:
// Combination of above parts at one machine. Is used by go test.
// Packets are generated, divided and calculated in the same pipeline.

const (
	gotest uint = iota
	generatePart
	receivePart
)

const (
	separate uint = iota
	vseparate
	split
	vsplit
	partition
	vpartition
	pcopy
	handles
	handle
	vhandle
	dhandle
	vdhandle
)

const vecSize = 32

var (
	totalPackets     uint64
	userTotalPackets uint64 = 10000000
	payloadSize      uint   = 16
	speed            uint64 = 1000000

	recvPacketsGroup1 uint64
	recvPacketsGroup2 uint64
	count             uint64
	countRes          uint64
	recvPackets       uint64
	brokenPackets     uint64

	dstPort1 uint16 = 111
	dstPort2 uint16 = 222
	dstPort3 uint16 = 333

	testDoneEvent *sync.Cond
	progStart     time.Time

	// T second timeout is used to let generator reach required speed
	// During timeout packets are skipped and not counted
	T = 10 * time.Second

	outport1     uint
	outport2     uint
	inport1      uint
	inport2      uint
	dpdkLogLevel = "--log-level=0"

	fixMACAddrs  func(*packet.Packet, flow.UserContext)
	fixMACAddrs1 func(*packet.Packet, flow.UserContext)
	fixMACAddrs2 func(*packet.Packet, flow.UserContext)

	l3Rules     *packet.L3Rules
	gTestType   uint
	lessPercent int
	passedLimit uint64
	delta       int = 2
	checkSlice  []uint8
	shift       uint64
)

type packetData struct {
	F1, F2 uint64
}

func main() {
	var testScenario uint
	var testType uint
	flag.UintVar(&testScenario, "testScenario", 0, "1 to use 1 as GENERATE part, 2 to use as RECEIVE part, 0 to use as ONE-MACHINE variant")
	flag.UintVar(&testType, "testType", 0, "0 - Separate test, 1 - Split test, 2 - Partition test, 3 - pCopy test, 4 - Handle test, 5 - DHandle test")
	flag.Uint64Var(&speed, "speed", speed, "speed of generator, Pkts/s")
	flag.UintVar(&outport1, "outport1", 0, "port for 1st sender")
	flag.UintVar(&outport2, "outport2", 1, "port for 2nd sender")
	flag.UintVar(&inport1, "inport1", 0, "port for 1st receiver")
	flag.UintVar(&inport2, "inport2", 1, "port for 2nd receiver")
	flag.Uint64Var(&userTotalPackets, "number", userTotalPackets, "total number of packets to receive by test")
	flag.DurationVar(&T, "timeout", T, "test start delay, needed to stabilize speed. Packets sent during timeout do not affect test result")
	configFile := flag.String("config", "", "Specify json config file name (mandatory for VM)")
	target := flag.String("target", "", "Target host name from config file (mandatory for VM)")
	flag.StringVar(&dpdkLogLevel, "dpdk", "--log-level=0", "Passes an arbitrary argument to dpdk EAL")
	flag.Parse()

	if err := initDPDK(); err != nil {
		fmt.Printf("fail: %+v\n", err)
	}
	if err := executeTest(*configFile, *target, testScenario, testType); err != nil {
		fmt.Printf("fail: %+v\n", err)
	}
}

func initDPDK() error {
	// Init NFF-GO system
	config := flow.Config{
		DPDKArgs: []string{dpdkLogLevel},
	}
	if err := flow.SystemInit(&config); err != nil {
		return err
	}
	return nil
}

func executeTest(configFile, target string, testScenario uint, testType uint) error {
	if testScenario > 3 || testScenario < 0 {
		return errors.New("testScenario should be in interval [0, 3]")
	}
	gTestType = testType
	countRes = 0
	recvPacketsGroup1 = 0
	recvPacketsGroup2 = 0
	recvPackets = 0
	count = 0
	brokenPackets = 0

	checkSlice = make([]uint8, userTotalPackets*2, userTotalPackets*2)

	stabilityCommon.InitCommonState(configFile, target)
	fixMACAddrs = stabilityCommon.ModifyPacket[0].(func(*packet.Packet, flow.UserContext))
	fixMACAddrs1 = stabilityCommon.ModifyPacket[0].(func(*packet.Packet, flow.UserContext))
	fixMACAddrs2 = stabilityCommon.ModifyPacket[1].(func(*packet.Packet, flow.UserContext))

	if err := setParameters(testScenario); err != nil {
{'fraction_non_alphanumeric': 0.10620045607557824, 'fraction_numerical': 0.031165164512976438, 'mean_word_length': 3.922501336183859, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3136831', 'n_tokens_mistral': 3292, 'n_tokens_neox': 3062, 'n_words': 984}