Skip to content

Commit fef6ba5

Browse files
committed
Add support for configuring sFlow
1 parent c06b927 commit fef6ba5

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

ovs/vswitch.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,31 @@ func (v *VSwitchService) GetController(bridge string) (string, error) {
147147
return strings.TrimSpace(string(address)), nil
148148
}
149149

150+
// CreateSFlow configures an sFlow collector for the vSwitch.
151+
func (v *VSwitchService) CreateSFlow(bridgeName string, agentIP string, collectorIP string, collectorPort string, headerBytes string, samplingN string, pollingSecs string) (string, error) {
152+
var (
153+
agent = fmt.Sprintf("agent=%s", agentIP)
154+
target = fmt.Sprintf("target=\"%s:%s\"", collectorIP, collectorPort)
155+
header = fmt.Sprintf("header=%s", headerBytes)
156+
sampling = fmt.Sprintf("sampling=%s", samplingN)
157+
polling = fmt.Sprintf("polling=%s", pollingSecs)
158+
159+
sFlowID = "sflow"
160+
)
161+
162+
output, err := v.exec(
163+
"--",
164+
fmt.Sprintf("--id=@%s", sFlowID), "create", "sflow", agent, target, header, sampling, polling,
165+
"--",
166+
"set", "bridge", bridgeName, fmt.Sprintf("sflow=@%s", sFlowID),
167+
)
168+
if err != nil {
169+
return "", err
170+
}
171+
172+
return strings.TrimSpace(string(output)), nil
173+
}
174+
150175
// exec executes an ExecFunc using 'ovs-vsctl'.
151176
func (v *VSwitchService) exec(args ...string) ([]byte, error) {
152177
return v.c.exec("ovs-vsctl", args...)

ovs/vswitch_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,55 @@ func TestClientVSwitchGetControllerOK(t *testing.T) {
197197
}
198198
}
199199

200+
201+
func TestClienCreateSFlow(t *testing.T) {
202+
var (
203+
id = "830bab0b-4149-4f5e-b213-06c6d8a727b9"
204+
bridge = "br0"
205+
collectorIP = "10.0.0.10"
206+
collectorPort = "6343"
207+
agentIP = "ovsbr0"
208+
headerBytes = "128"
209+
samplingN = "64"
210+
pollingSecs = "5"
211+
)
212+
213+
// Apply Timeout option to verify arguments
214+
c := testClient([]OptionFunc{Timeout(1)}, func(cmd string, args ...string) ([]byte, error) {
215+
// Verify correct command and arguments passed, including option flags
216+
if want, got := "ovs-vsctl", cmd; want != got {
217+
t.Fatalf("incorrect command:\n- want: %v\n- got: %v",
218+
want, got)
219+
}
220+
221+
wantArgs := []string{
222+
"--timeout=1",
223+
"--",
224+
"--id=@sflow", "create", "sflow", fmt.Sprintf("agent=%s", agentIP), fmt.Sprintf("target=\"%s:%s\"", collectorIP, collectorPort),
225+
fmt.Sprintf("header=%s", headerBytes), fmt.Sprintf("sampling=%s", samplingN), fmt.Sprintf("polling=%s", pollingSecs),
226+
"--",
227+
"set", "bridge", bridge, "sflow=@sflow",
228+
}
229+
230+
if want, got := wantArgs, args; !reflect.DeepEqual(want, got) {
231+
t.Fatalf("incorrect arguments\n- want: %v\n- got: %v",
232+
want, got)
233+
}
234+
235+
return []byte(id), nil
236+
})
237+
238+
sflowID, err := c.VSwitch.CreateSFlow(bridge, agentIP, collectorIP, collectorPort, headerBytes, samplingN, pollingSecs)
239+
if err != nil {
240+
t.Fatalf("unexpected error for Client.VSwitch.CreateSflow: %v", err)
241+
}
242+
243+
if sflowID != id {
244+
t.Fatalf("sFlowID missmatch\n- got: %v\n- want: %v", sflowID, id)
245+
}
246+
247+
}
248+
200249
func TestClientVSwitchListPorts(t *testing.T) {
201250
tests := []struct {
202251
name string

0 commit comments

Comments
 (0)