-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataplane_oper.go
119 lines (106 loc) · 2.94 KB
/
dataplane_oper.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
/*
* Copyright 2019-present Open Networking Foundation
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
Package dataplane implements packet send/receive functions
*/
package dataplane
import (
"github.com/stratum/testvectors-runner/pkg/logger"
pm "github.com/stratum/testvectors/proto/portmap"
)
var log = logger.NewLogger()
// Match is used by verify
type Match uint8
// Match values for verify
const (
Exact = Match(0x1)
In = Match(0x2)
)
// dataPlane interface implements packet send/receive/verify functions
type dataPlane interface {
// start packet capturing
capture() bool
// send packets to a specific port
send(pkts [][]byte, port uint32) bool
// verify packets captured on ports
verify(pkts [][]byte, ports []uint32) bool
// stop packet capturing
stop() bool
}
var dp dataPlane
// CreateDataPlane takes the dataplane mode, packet match type and portmap file name as arguments
// and creates one dataplane instance for packet sending/receiving/verification.
func CreateDataPlane(mode string, matchType string, portmap *pm.PortMap) {
var match Match
switch matchType {
case "exact":
match = Exact
case "in":
match = In
default:
log.Fatalf("Unknown data plane match type: %s", matchType)
}
switch mode {
case "direct":
log.Infof("Creating direct data plane with match type: %s and port map: %s\n", matchType, portmap)
dp = createDirectDataPlane(portmap, match)
case "loopback":
log.Infof("Creating loopback data plane with match type: %s and port map: %s\n", matchType, portmap)
dp = createLoopbackDataPlane(portmap, match)
default:
log.Fatalf("Unknown data plane mode: %s", mode)
}
}
//getPortMapEntryByPortNumber looks up given portmap and returns the first entry that has the same port number as specified.
//If none of the entries match it returns nil
func getPortMapEntryByPortNumber(portmap *pm.PortMap, portNumber uint32) *pm.Entry {
if dp == nil {
log.Error("data plane does not exist")
return nil
}
for _, entry := range portmap.GetEntries() {
if entry.GetPortNumber() == portNumber {
return entry
}
}
return nil
}
//ProcessTrafficStimulus sends packets to specific ports
func ProcessTrafficStimulus(pkts [][]byte, port uint32) bool {
log.Debug("In ProcessTrafficStimulus")
if dp == nil {
log.Error("data plane does not exist")
return false
}
return dp.send(pkts, port)
}
//ProcessTrafficExpectation verifies that packets arrived at specific ports
func ProcessTrafficExpectation(pkts [][]byte, ports []uint32) bool {
log.Debug("In ProcessTrafficExpectation")
if dp == nil {
log.Error("data plane does not exist")
return false
}
return dp.verify(pkts, ports)
}
//Capture starts packet capturing
func Capture() bool {
log.Debug("In Capture")
if dp == nil {
log.Error("data plane does not exist")
return false
}
return dp.capture()
}
//Stop stops packet capturing
func Stop() bool {
log.Debug("In Stop")
if dp == nil {
log.Error("data plane does not exist")
return false
}
return dp.stop()
}