forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
134 lines (112 loc) · 3.31 KB
/
config.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
package tgproducer
//go:generate go run ../../mk/enumgen/ -guard=NDNDPDK_TGPRODUCER_ENUM_H -out=../../csrc/tgproducer/enum.h .
import (
"errors"
"fmt"
"github.com/usnistgov/ndn-dpdk/iface"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndn/an"
"github.com/usnistgov/ndn-dpdk/ndni"
)
const (
// MaxPatterns is maximum number of traffic patterns.
MaxPatterns = 128
// MaxReplies is maximum number of replies per pattern.
MaxReplies = 8
// MaxSumWeight is maximum sum of weights among replies.
MaxSumWeight = 256
_ = "enumgen::Tgp"
)
// Error conditions.
var (
ErrNoPattern = errors.New("no pattern specified")
ErrTooManyPatterns = fmt.Errorf("cannot add more than %d patterns", MaxPatterns)
ErrPrefixTooLong = fmt.Errorf("prefix cannot exceed %d octets", ndni.NameMaxLength)
ErrTooManyReplies = fmt.Errorf("cannot add more than %d replies", MaxReplies)
ErrTooManyWeights = fmt.Errorf("sum of weight cannot exceed %d", MaxSumWeight)
)
// Config describes producer configuration.
type Config struct {
NThreads int `json:"nThreads,omitempty"` // number of threads, minimum/default is 1
RxQueue iface.PktQueueConfig `json:"rxQueue,omitempty"`
Patterns []Pattern `json:"patterns"`
nDataGen int
}
// Validate applies defaults and validates the configuration.
func (cfg *Config) Validate() error {
cfg.NThreads = max(1, cfg.NThreads)
cfg.RxQueue.DisableCoDel = true
if len(cfg.Patterns) == 0 {
return ErrNoPattern
}
if len(cfg.Patterns) > MaxPatterns {
return ErrTooManyPatterns
}
patterns := []Pattern{}
nDataGen := 0
for _, pattern := range cfg.Patterns {
sumWeight, nData := pattern.applyDefaults()
if sumWeight > MaxSumWeight {
return ErrTooManyWeights
}
nDataGen += nData
if pattern.Prefix.Length() > ndni.NameMaxLength {
return ErrPrefixTooLong
}
patterns = append(patterns, pattern)
}
cfg.Patterns, cfg.nDataGen = patterns, nDataGen
return nil
}
// Pattern configures how the producer replies to Interests under a name prefix.
type Pattern struct {
Prefix ndn.Name `json:"prefix"`
Replies []Reply `json:"replies"` // if empty, reply with Data FreshnessPeriod=1
}
func (pattern *Pattern) applyDefaults() (sumWeight, nDataGen int) {
if len(pattern.Replies) == 0 {
pattern.Replies = []Reply{
{
DataGenConfig: ndni.DataGenConfig{
FreshnessPeriod: 1,
},
},
}
}
for i := range pattern.Replies {
reply := &pattern.Replies[i]
reply.Weight = max(1, reply.Weight)
sumWeight += reply.Weight
if reply.Kind() == ReplyData {
nDataGen++
}
}
return
}
// ReplyKind indicates reply packet type.
type ReplyKind int
// ReplyKind values.
const (
ReplyData ReplyKind = iota
ReplyNack
ReplyTimeout
_ = "enumgen:TgpReplyKind:TgpReply:Reply"
)
// Reply configures how the producer replies to the Interest.
type Reply struct {
Weight int `json:"weight,omitempty"` // weight of random choice, minimum/default is 1
ndni.DataGenConfig
Nack uint8 `json:"nack,omitempty"` // if not NackNone, reply with Nack instead of Data
Timeout bool `json:"timeout,omitempty"` // if true, drop the Interest instead of sending Data
}
// Kind returns ReplyKind.
func (reply Reply) Kind() ReplyKind {
switch {
case reply.Timeout:
return ReplyTimeout
case reply.Nack != an.NackNone:
return ReplyNack
default:
return ReplyData
}
}