From 018684b8c612b80516ef2615f792cd13dcb54288 Mon Sep 17 00:00:00 2001 From: xgfone Date: Sat, 20 Feb 2021 23:01:46 +0800 Subject: [PATCH] add some constants and variables --- constant.go | 36 ++++++++++++++++++++++++++++++++++++ ovs_flow.go | 16 ++++++++-------- 2 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 constant.go diff --git a/constant.go b/constant.go new file mode 100644 index 0000000..ca696f9 --- /dev/null +++ b/constant.go @@ -0,0 +1,36 @@ +package ovs + +// Some linux commands. +var ( + IPCmd = "ip" + OfctlCmd = "ovs-ofctl" + VsctlCmd = "ovs-vsctl" +) + +// L2 Data-Link Protocol Number +const ( + ARP = 0x0806 + IPv4 = 0x0800 + IPv6 = 0x86dd +) + +// L3 IP Protocol Number +const ( + ICMP = 1 + TCP = 6 + UDP = 17 + GRE = 47 +) + +// OVS Actions +const ( + DROP = "drop" + LOCAL = "local" + FLOOD = "flood" + NORMAL = "normal" +) + +// MAC address +const ( + BroadcastMac = "ff:ff:ff:ff:ff:ff" +) diff --git a/ovs_flow.go b/ovs_flow.go index 588a1e3..6f43e04 100644 --- a/ovs_flow.go +++ b/ovs_flow.go @@ -48,15 +48,15 @@ func GetAllFlows(bridge string, isName, isStats bool) (flows []string, err error var out string if isName { if isStats { - out, err = exec.Outputs("ovs-ofctl", "--names", "--stats", "dump-flows", bridge) + out, err = exec.Outputs(OfctlCmd, "--names", "--stats", "dump-flows", bridge) } else { - out, err = exec.Outputs("ovs-ofctl", "--names", "--no-stats", "dump-flows", bridge) + out, err = exec.Outputs(OfctlCmd, "--names", "--no-stats", "dump-flows", bridge) } } else { if isStats { - out, err = exec.Outputs("ovs-ofctl", "--no-names", "--stats", "dump-flows", bridge) + out, err = exec.Outputs(OfctlCmd, "--no-names", "--stats", "dump-flows", bridge) } else { - out, err = exec.Outputs("ovs-ofctl", "--no-names", "--no-stats", "dump-flows", bridge) + out, err = exec.Outputs(OfctlCmd, "--no-names", "--no-stats", "dump-flows", bridge) } } @@ -70,7 +70,7 @@ func GetAllFlows(bridge string, isName, isStats bool) (flows []string, err error // AddFlows adds the flows. func AddFlows(bridge string, flows ...string) (err error) { for _, flow := range flows { - if err = exec.Executes("ovs-ofctl", "add-flow", bridge, flow); err != nil { + if err = exec.Executes(OfctlCmd, "add-flow", bridge, flow); err != nil { return } } @@ -80,7 +80,7 @@ func AddFlows(bridge string, flows ...string) (err error) { // DelFlows deletes the flows. func DelFlows(bridge string, matches ...string) (err error) { for _, match := range matches { - if err = exec.Executes("ovs-ofctl", "del-flows", bridge, match); err != nil { + if err = exec.Executes(OfctlCmd, "del-flows", bridge, match); err != nil { return } } @@ -91,7 +91,7 @@ func DelFlows(bridge string, matches ...string) (err error) { func DelFlowsStrict(bridge string, priority int, matches ...string) (err error) { for _, match := range matches { match = fmt.Sprintf("priority=%d,%s", priority, match) - err = exec.Executes("ovs-ofctl", "--strict", "del-flows", bridge, match) + err = exec.Executes(OfctlCmd, "--strict", "del-flows", bridge, match) if err != nil { return } @@ -153,6 +153,6 @@ func SendARPRequest(bridge, output, inPort, srcMac, srcIP, dstIP string, } pkt := fmt.Sprintf(arpPacket, srcmac, vlan, srcmac, srcIP, dstIP) - exec.Execute("ovs-ofctl", "packet-out", bridge, inPort, output, pkt) + exec.Execute(OfctlCmd, "packet-out", bridge, inPort, output, pkt) return }