Skip to content

Commit 279c8d5

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

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,54 @@ func TestClientVSwitchGetControllerOK(t *testing.T) {
197197
}
198198
}
199199

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

0 commit comments

Comments
 (0)