-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathutility.go
156 lines (140 loc) · 3.72 KB
/
utility.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2018 Intel Corporation.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package generator
import (
"fmt"
"math/rand"
"os"
"sync/atomic"
"github.com/intel-go/nff-go/flow"
"github.com/intel-go/nff-go/packet"
)
var gen generator
type generator struct {
Count uint64
}
// GetGenerator returns generator struct pointer
// generator is single and created only once
func GetGenerator() *generator {
return &gen
}
// GetGeneratedNumber returns a number of packets generated
func (g *generator) GetGeneratedNumber() uint64 {
return atomic.LoadUint64(&(g.Count))
}
// ReadConfig function reads and parses config file.
func ReadConfig(fileName string) (GeneratorConfig, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("opening file failed with: %v ", err)
}
cfg, err := ParseConfigFile(f)
if err != nil {
return nil, fmt.Errorf("parsing config failed with: %v", err)
}
return cfg, nil
}
func getGenerator(configuration PacketConfig) (func(*packet.Packet, *PacketConfig, *rand.Rand), error) {
switch configuration.DType {
case ETHERHDR:
l2 := configuration.Ether
switch l2.DType {
case IPv4HDR:
l3 := l2.IPv4
switch l3.DType {
case TCPHDR:
return generateTCPIPv4, nil
case UDPHDR:
return generateUDPIPv4, nil
case ICMPHDR:
return generateICMPIPv4, nil
case DATA:
return generateIPv4, nil
default:
return nil, fmt.Errorf("unknown packet l4 configuration")
}
case IPv6HDR:
l3 := l2.IPv6
switch l3.DType {
case TCPHDR:
return generateTCPIPv6, nil
case UDPHDR:
return generateUDPIPv6, nil
case ICMPHDR:
return generateICMPIPv6, nil
case DATA:
return generateIPv6, nil
default:
return nil, fmt.Errorf("unknown packet l4 configuration")
}
case ARPHDR:
return generateARP, nil
case DATA:
return generateEther, nil
default:
return nil, fmt.Errorf("unknown packet l3 configuration")
}
default:
return nil, fmt.Errorf("unknown packet l2 configuration")
}
}
// one unit for each mix
type generatorTableUnit struct {
have, need uint32
generatorFunc func(*packet.Packet, *PacketConfig, *rand.Rand)
config PacketConfig
}
func (gtu *generatorTableUnit) String() string {
return fmt.Sprintf("need: %d, config: %v\n", gtu.need, gtu.config)
}
type genParameters struct {
table []generatorTableUnit
next uint32
length uint32
rnd *rand.Rand
}
func (gp genParameters) Copy() interface{} {
ret := new(genParameters)
ret.table = make([]generatorTableUnit, len(gp.table))
copy(ret.table, gp.table)
ret.length = gp.length
ret.rnd = rand.New(rand.NewSource(13))
return ret
}
func (gp genParameters) Delete() {
}
// GetContext gets generator context according to config
func GetContext(mixConfig GeneratorConfig) (*genParameters, error) {
var t []generatorTableUnit
for _, packetConfig := range mixConfig {
genFunc, err := getGenerator(packetConfig.Config)
if err != nil {
return nil, err
}
tu := generatorTableUnit{have: 0, need: packetConfig.Quantity, generatorFunc: genFunc, config: packetConfig.Config}
t = append(t, tu)
}
ret := new(genParameters)
ret.table = t
ret.length = uint32(len(t))
ret.rnd = rand.New(rand.NewSource(13))
return ret, nil
}
// Generate is a main generatior func
func Generate(pkt *packet.Packet, context flow.UserContext) {
genP := context.(*genParameters)
if genP.length > 1 {
if genP.table[genP.next].have == genP.table[genP.next].need {
genP.table[genP.next].have = 0
if genP.next+1 < genP.length {
genP.next++
} else {
genP.next = 0
}
}
}
genP.table[genP.next].generatorFunc(pkt, &genP.table[genP.next].config, genP.rnd)
atomic.AddUint64(&(gen.Count), 1)
genP.table[genP.next].have++
}