Repository: tylertreat/comcast Branch: master Commit: 145d02155cff Files: 13 Total size: 52.6 KB Directory structure: gitextract_m1qezx1i/ ├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── comcast.go ├── go.mod └── throttler/ ├── ipfw.go ├── pfctl.go ├── pfctl_test.go ├── tc.go ├── tc_test.go └── throttler.go ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test *.prof ================================================ FILE: .travis.yml ================================================ language: golang go: - 1.8.x - 1.9.x - 1.10.x - 1.11.x script: - go test -v ./throttler/... ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: Makefile ================================================ BINARY="comcast" VERSION=1.0.0 BUILD=`date +%FT%T%z` DIR=bin PACKAGES=`go list ./... | grep -v /vendor/` VETPACKAGES=`go list ./... | grep -v /vendor/ | grep -v /examples/` GOFILES=`find . -name "*.go" -type f -not -path "./vendor/*"` default: @mkdir ${DIR} @go build -o ${DIR}/${BINARY} -tags=jsoniter list: @echo ${PACKAGES} @echo ${VETPACKAGES} @echo ${GOFILES} fmt: @gofmt -s -w ${GOFILES} fmt-check: @diff=?(gofmt -s -d $(GOFILES)); \ if [ -n "$$diff" ]; then \ echo "Please run 'make fmt' and commit the result:"; \ echo "$${diff}"; \ exit 1; \ fi; install: @govendor sync -v test: @go test -cpu=1,2,4 -v -tags integration ./... vet: @go vet $(VETPACKAGES) clean: @if [ -f${DIR}/${BINARY} ] ; then rm -rf ${DIR} ; fi .PHONY: default fmt fmt-check install test vet docker clean ================================================ FILE: README.md ================================================ # Comcast Testing distributed systems under hard failures like network partitions and instance termination is critical, but it's also important we test them under [less catastrophic conditions](http://www.bravenewgeek.com/sometimes-kill-9-isnt-enough/) because this is what they most often experience. Comcast is a tool designed to simulate common network problems like latency, bandwidth restrictions, and dropped/reordered/corrupted packets. It works by wrapping up some system tools in a portable(ish) way. On BSD-derived systems such as OSX, we use tools like `ipfw` and `pfctl` to inject failure. On Linux, we use `iptables` and `tc`. Comcast is merely a thin wrapper around these controls. Windows support may be possible with `wipfw` or even the native network stack, but this has not yet been implemented in Comcast and may be at a later date. ## Installation ``` $ go install github.com/tylertreat/comcast@latest ``` ### Path addition may be needed In unix-like systems, after installing comcast with go, it may be needed to add it to the `PATH`. Go installs by default in `$HOME/go/bin`. ``` $ export PATH=$PATH:$HOME/go/bin ``` ## Usage On Linux, Comcast supports several options: device, latency, target/default bandwidth, packet loss, protocol, and port number. ``` $ comcast --device=eth0 --latency=250 --target-bw=1000 --default-bw=1000000 --packet-loss=10% --target-addr=8.8.8.8,10.0.0.0/24 --target-proto=tcp,udp,icmp --target-port=80,22,1000:2000 ``` On OSX, Comcast will check for `pfctl` support (as of Yosemite), which supports the same options as above. If `pfctl` is not available, it will use `ipfw` instead, which supports device, latency, target bandwidth, and packet-loss options. On BSD (with `ipfw`), Comcast currently supports only: device, latency, target bandwidth, and packet loss. ``` $ comcast --device=eth0 --latency=250 --target-bw=1000 --packet-loss=10% ``` This will add 250ms of latency, limit bandwidth to 1Mbps, and drop 10% of packets to the targetted (on Linux) destination addresses using the specified protocols on the specified port numbers (slow lane). The default bandwidth specified will apply to all egress traffic (fast lane). To turn this off, run the following: ``` $ comcast --stop ``` By default, comcast will determine the system commands to execute, log them to stdout, and execute them. The `--dry-run` flag will skip execution. ## I don't trust you, this code sucks, I hate Go, etc. If you don't like running code that executes shell commands for you (despite it being open source, so you can read it and change the code) or want finer-grained control, you can run them directly instead. Read the man pages on these things for more details. ### Linux On Linux, you can use `iptables` to drop incoming and outgoing packets. ``` $ iptables -A INPUT -m statistic --mode random --probability 0.1 -j DROP $ iptables -A OUTPUT -m statistic --mode random --probability 0.1 -j DROP ``` Alternatively, you can use `tc` which supports some additional options. ``` $ tc qdisc add dev eth0 root netem delay 50ms 20ms distribution normal $ tc qdisc change dev eth0 root netem reorder 0.02 duplicate 0.05 corrupt 0.01 ``` To reset: ``` $ tc qdisc del dev eth0 root netem ``` ### BSD/OSX To shape traffic in BSD-derived systems, create an `ipfw` pipe and configure it. You can control incoming and outgoing traffic separately for any specific host or network. ``` $ ipfw add 1 pipe 1 ip from me to any $ ipfw add 2 pipe 1 ip from any to me $ ipfw pipe 1 config delay 500ms bw 1Mbit/s plr 0.1 ``` To reset: ``` $ ipfw delete 1 ``` *Note: `ipfw` was removed in OSX Yosemite in favor of `pfctl`.* ## Network Condition Profiles Here's a list of network conditions with values that you can plug into Comcast. Please add any more that you may come across. Name | Latency | Bandwidth | Packet-loss :-- | --: | --: | --: GPRS (good) | 500 | 50 | 2 EDGE (good) | 300 | 250 | 1.5 3G/HSDPA (good) | 250 | 750 | 1.5 DIAL-UP (good) | 185 | 40 | 2 DSL (poor) | 70 | 2000 | 2 DSL (good) | 40 | 8000 | 0.5 WIFI (good) | 40 | 30000 | 0.2 Starlink | 20 | - | 2.5 ================================================ FILE: comcast.go ================================================ package main import ( "flag" "fmt" "net" "os" "strconv" "strings" "github.com/tylertreat/comcast/throttler" ) const version = "1.0.0" func main() { // TODO: Add support for other options like packet reordering, duplication, etc. var ( device = flag.String("device", "", "Interface (device) to use (defaults to eth0 where applicable)") stop = flag.Bool("stop", false, "Stop packet controls") latency = flag.Int("latency", -1, "Latency to add in ms") targetbw = flag.Int("target-bw", -1, "Target bandwidth limit in kbit/s (slow-lane)") defaultbw = flag.Int("default-bw", -1, "Default bandwidth limit in kbit/s (fast-lane)") packetLoss = flag.String("packet-loss", "0", "Packet loss percentage (e.g. 0.1%)") targetaddr = flag.String("target-addr", "", "Target addresses, (e.g. 10.0.0.1 or 10.0.0.0/24 or 10.0.0.1,192.168.0.0/24 or 2001:db8:a::123)") targetport = flag.String("target-port", "", "Target port(s) (e.g. 80 or 1:65535 or 22,80,443,1000:1010)") targetproto = flag.String("target-proto", "tcp,udp,icmp", "Target protocol TCP/UDP (e.g. tcp or tcp,udp or icmp)") dryrun = flag.Bool("dry-run", false, "Specifies whether or not to actually commit the rule changes") //icmptype = flag.String("icmp-type", "", "icmp message type (e.g. reply or reply,request)") //TODO: Maybe later :3 vers = flag.Bool("version", false, "Print Comcast's version") ) flag.Parse() if *vers { fmt.Printf("Comcast version %s\n", version) return } targetIPv4, targetIPv6 := parseAddrs(*targetaddr) throttler.Run(&throttler.Config{ Device: *device, Stop: *stop, Latency: *latency, TargetBandwidth: *targetbw, DefaultBandwidth: *defaultbw, PacketLoss: parseLoss(*packetLoss), TargetIps: targetIPv4, TargetIps6: targetIPv6, TargetPorts: parsePorts(*targetport), TargetProtos: parseProtos(*targetproto), DryRun: *dryrun, }) } func parseLoss(loss string) float64 { val := loss if strings.Contains(loss, "%") { val = loss[:len(loss)-1] } l, err := strconv.ParseFloat(val, 64) if err != nil { fmt.Println("Incorrectly specified packet loss:", loss) os.Exit(1) } return l } func parseAddrs(addrs string) ([]string, []string) { adrs := strings.Split(addrs, ",") parsedIPv4 := []string{} parsedIPv6 := []string{} if addrs != "" { for _, adr := range adrs { ip := net.ParseIP(adr) if ip != nil { if ip.To4() != nil { parsedIPv4 = append(parsedIPv4, adr) } else { parsedIPv6 = append(parsedIPv6, adr) } } else { //Not a valid single IP, could it be a CIDR? parsedIP, net, err := net.ParseCIDR(adr) if err == nil { if parsedIP.To4() != nil { parsedIPv4 = append(parsedIPv4, net.String()) } else { parsedIPv6 = append(parsedIPv6, net.String()) } } else { fmt.Println("Incorrectly specified target IP or CIDR:", adr) os.Exit(1) } } } } return parsedIPv4, parsedIPv6 } func parsePorts(ports string) []string { prts := strings.Split(ports, ",") parsed := []string{} if ports != "" { for _, prt := range prts { if strings.Contains(prt, ":") { if validRange(prt) { parsed = append(parsed, prt) } else { fmt.Println("Incorrectly specified port range:", prt) os.Exit(1) } } else { //Isn't a range, check if just a single port if validPort(prt) { parsed = append(parsed, prt) } else { fmt.Println("Incorrectly specified port:", prt) os.Exit(1) } } } } return parsed } func parsePort(port string) int { prt, err := strconv.Atoi(port) if err != nil { return 0 } return prt } func validPort(port string) bool { prt := parsePort(port) return prt > 0 && prt < 65536 } func validRange(ports string) bool { pr := strings.Split(ports, ":") if len(pr) == 2 { if !validPort(pr[0]) || !validPort(pr[1]) { return false } if portHigher(pr[0], pr[1]) { return false } } else { return false } return true } func portHigher(prt1, prt2 string) bool { p1 := parsePort(prt1) p2 := parsePort(prt2) return p1 > p2 } func parseProtos(protos string) []string { ptcs := strings.Split(protos, ",") parsed := []string{} if protos != "" { for _, ptc := range ptcs { p := strings.ToLower(ptc) if p == "udp" || p == "tcp" || p == "icmp" { parsed = append(parsed, p) } else { fmt.Println("Incorrectly specified protocol:", p) os.Exit(1) } } } return parsed } ================================================ FILE: go.mod ================================================ module github.com/tylertreat/comcast go 1.15 require github.com/tylertreat/comcast v1.0.1 ================================================ FILE: throttler/ipfw.go ================================================ package throttler import ( "strconv" ) const ( ipfwAddPipe = `sudo ipfw add 1 pipe 1 ip from any to any via ` ipfwTeardown = `sudo ipfw delete 1` ipfwConfig = `sudo ipfw pipe 1 config` ipfwExists = `sudo ipfw list | grep "pipe 1"` ipfwCheck = `sudo ipfw list` ) type ipfwThrottler struct { c commander } func (i *ipfwThrottler) setup(c *Config) error { cmd := ipfwAddPipe + c.Device err := i.c.execute(cmd) if err != nil { return err } configCmd := i.buildConfigCommand(c) err = i.c.execute(configCmd) return err } func (i *ipfwThrottler) teardown(_ *Config) error { err := i.c.execute(ipfwTeardown) return err } func (i *ipfwThrottler) exists() bool { if dry { return false } err := i.c.execute(ipfwExists) return err == nil } func (i *ipfwThrottler) check() string { return ipfwCheck } func (i *ipfwThrottler) buildConfigCommand(c *Config) string { cmd := ipfwConfig if c.Latency > 0 { cmd = cmd + " delay " + strconv.Itoa(c.Latency) + "ms" } if c.TargetBandwidth > 0 { cmd = cmd + " bw " + strconv.Itoa(c.TargetBandwidth) + "Kbit/s" } if c.PacketLoss > 0 { cmd = cmd + " plr " + strconv.FormatFloat(c.PacketLoss/100, 'f', 4, 64) } return cmd } ================================================ FILE: throttler/pfctl.go ================================================ package throttler import ( "fmt" "strconv" "strings" ) const ( // TODO: use printf in favour of echo due to shell portability issues pfctlCreateAnchor = `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -` pfctlTeardown = `sudo pfctl -f /etc/pf.conf` dnctl = `sudo dnctl pipe 1 config` pfctlCreateDummynet = `echo $'dummynet in on %s all pipe 1'` pfctlExecuteInline = `%s | sudo pfctl -a mop -f - ` pfctlEnableFirewall = `sudo pfctl -E` pfctlEnableFwRegex = `pf enabled` pfctlDisableFirewall = `sudo pfctl -d` pfctlDisbleFwRegex = `pf disabled` pfctlIsEnabled = `sudo pfctl -sa | grep -i enabled` dnctlIsConfigured = `sudo dnctl show` pfctlIsEnabledRegex = `Enabled` dnctlTeardown = `sudo dnctl -q flush` ) type pfctlThrottler struct { c commander } // Execute a command and check that any matching line in the result contains 'match' func (i *pfctlThrottler) executeAndParse(cmd string, match string) bool { lines, err := i.c.executeGetLines(cmd) if err != nil { return false } for _, line := range lines { if strings.Contains(line, match) { return true } } return false } func (i *pfctlThrottler) setup(c *Config) error { // Enable firewall err := i.c.execute(pfctlEnableFirewall) if err != nil { return fmt.Errorf("Could not enable firewall using: `%s`. Error: %s", pfctlEnableFirewall, err.Error()) } // Add the dummynet and anchor err = i.c.execute(pfctlCreateAnchor) if err != nil { return fmt.Errorf("Could not create anchor rule for dummynet using: `%s`. Error: %s", pfctlCreateAnchor, err.Error()) } // Add 'execute' portion of the command input := fmt.Sprintf(pfctlCreateDummynet, c.Device) cmd := fmt.Sprintf(pfctlExecuteInline, input) err = i.c.execute(cmd) if err != nil { return fmt.Errorf("Could not create dummynet using: `%s`. Error: %s", input, err.Error()) } // Apply the shaping etc. for _, cmd := range i.buildConfigCommand(c) { err = i.c.execute(cmd) if err != nil { return err } } return nil } func (i *pfctlThrottler) teardown(_ *Config) error { // Reset firewall rules, leave it running err := i.c.execute(pfctlTeardown) if err != nil { return fmt.Errorf("Could not remove firewall rules using: `%s`. Error: %s", pfctlTeardown, err.Error()) } // Turn off the firewall, discarding any rules err = i.c.execute(pfctlDisableFirewall) if err != nil { return fmt.Errorf("Could not disable firewall using: `%s`. Error: %s", pfctlDisableFirewall, err.Error()) } // Disable dnctl rules err = i.c.execute(dnctlTeardown) if err != nil { return fmt.Errorf("Could not disable dnctl rules using: `%s`. Error: %s", dnctlTeardown, err.Error()) } return nil } func (i *pfctlThrottler) isFirewallRunning() bool { return i.executeAndParse(pfctlIsEnabled, pfctlIsEnabledRegex) } func (i *pfctlThrottler) exists() bool { if dry { return false } return i.executeAndParse(dnctlIsConfigured, "port") || i.isFirewallRunning() } func (i *pfctlThrottler) check() string { return pfctlIsEnabled } func addProtosToCommands(cmds []string, protos []string) []string { commands := make([]string, 0) for _, cmd := range cmds { for _, proto := range protos { commands = append(commands, fmt.Sprintf("%s proto %s", cmd, proto)) } } return commands } func addPortsToCommand(cmd string, ports []string) []string { commands := make([]string, 0) for _, port := range ports { commands = append(commands, fmt.Sprintf("%s dst-port %s", cmd, port)) commands = append(commands, fmt.Sprintf("%s src-port %s", cmd, port)) } return commands } // Takes care of the annoying differences between ipv4 and ipv6 func addIpsAndProtoToCommands(ipVersion int, cmds []string, ips []string, protos []string) []string { commands := make([]string, 0) for _, cmd := range cmds { for _, ip := range ips { srcIpFlag := "src-ip" dstIpFlag := "dst-ip" if ipVersion == 6 { srcIpFlag = "src-ip6" dstIpFlag = "dst-ip6" } commands = append(commands, addProtoToCommands(ipVersion, fmt.Sprintf("%s %s %s", cmd, srcIpFlag, ip), protos)...) commands = append(commands, addProtoToCommands(ipVersion, fmt.Sprintf("%s %s %s", cmd, dstIpFlag, ip), protos)...) } } return commands } func addProtoToCommands(ipVersion int, cmd string, protos []string) []string { commands := make([]string, 0) for _, proto := range protos { if ipVersion == 6 { if proto == "icmp" { proto = "ipv6-icmp" } } commands = append(commands, fmt.Sprintf("%s proto %s", cmd, proto)) } return commands } func (i *pfctlThrottler) buildConfigCommand(c *Config) []string { cmd := dnctl // Add all non tcp version dependent stuff first... if c.Latency > 0 { cmd = cmd + " delay " + strconv.Itoa(c.Latency) + "ms" } if c.TargetBandwidth > 0 { cmd = cmd + " bw " + strconv.Itoa(c.TargetBandwidth) + "Kbit/s" } if c.PacketLoss > 0 { cmd = cmd + " plr " + strconv.FormatFloat(c.PacketLoss/100, 'f', 4, 64) } // Add Mask keyword if we have pipe qualifiers if len(c.TargetPorts) > 0 || len(c.TargetProtos) > 0 || len(c.TargetIps) > 0 || len(c.TargetIps6) > 0 { cmd = cmd + " mask " } // Expand commands with ports commands := []string{cmd} if len(c.TargetPorts) > 0 { commands = addPortsToCommand(cmd, c.TargetPorts) } if len(c.TargetIps) == 0 && len(c.TargetIps6) == 0 { if len(c.TargetProtos) > 0 { return addProtosToCommands(commands, c.TargetProtos) } return commands } // create and combine the ipv4 and ipv6 IPs with the protocol version specific keywords return append(addIpsAndProtoToCommands(4, commands, c.TargetIps, c.TargetProtos), addIpsAndProtoToCommands(6, commands, c.TargetIps6, c.TargetProtos)...) } ================================================ FILE: throttler/pfctl_test.go ================================================ package throttler import ( "testing" ) func TestPfctlDefaultConfigCommand(t *testing.T) { r := newCmdRecorder() th := &pfctlThrottler{r} c := defaultTestConfig c.PacketLoss = 0 c.TargetIps = []string{} c.TargetIps6 = []string{} c.TargetBandwidth = -1 c.TargetPorts = []string{} c.TargetProtos = []string{"tcp,udp,icmp"} th.setup(&c) r.verifyCommands(t, []string{ "sudo pfctl -E", `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, `sudo dnctl pipe 1 config mask proto tcp,udp,icmp`, }) } func TestPfctlThrottleOnlyConfigCommand(t *testing.T) { var c = Config{ Device: "eth0", Stop: false, Latency: -1, TargetBandwidth: -1, DefaultBandwidth: 20000, PacketLoss: 0.1, } r := newCmdRecorder() th := &pfctlThrottler{r} th.setup(&c) r.verifyCommands(t, []string{ "sudo pfctl -E", `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, `sudo dnctl pipe 1 config plr 0.0010`, }) } func TestPfctlNoIPThrottleConfigCommand(t *testing.T) { var c = Config{ Device: "eth0", Stop: false, Latency: -1, TargetBandwidth: -1, DefaultBandwidth: 20000, PacketLoss: 0.1, TargetProtos: []string{"tcp"}, } r := newCmdRecorder() th := &pfctlThrottler{r} th.setup(&c) r.verifyCommands(t, []string{ "sudo pfctl -E", `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, `sudo dnctl pipe 1 config plr 0.0010 mask proto tcp`, }) } func TestPfctlPacketSetup(t *testing.T) { r := newCmdRecorder() th := &pfctlThrottler{r} c := defaultTestConfig c.PacketLoss = 0.5 th.setup(&c) r.verifyCommands(t, []string{ "sudo pfctl -E", `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 src-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 dst-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 src-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 dst-ip 10.10.10.10 proto tcp`, }) } func TestPfctlProtoSetup(t *testing.T) { r := newCmdRecorder() th := &pfctlThrottler{r} c := defaultTestConfig c.PacketLoss = 0.5 c.TargetProtos = []string{"tcp", "udp", "icmp"} th.setup(&c) r.verifyCommands(t, []string{ "sudo pfctl -E", `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 src-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 src-ip 10.10.10.10 proto udp`, `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 src-ip 10.10.10.10 proto icmp`, `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 dst-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 dst-ip 10.10.10.10 proto udp`, `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 dst-ip 10.10.10.10 proto icmp`, `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 src-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 src-ip 10.10.10.10 proto udp`, `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 src-ip 10.10.10.10 proto icmp`, `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 dst-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 dst-ip 10.10.10.10 proto udp`, `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 dst-ip 10.10.10.10 proto icmp`, }) } func TestPfctlMultiplePortsAndIps(t *testing.T) { r := newCmdRecorder() th := &pfctlThrottler{r} cfg := defaultTestConfig cfg.TargetIps = []string{"1.1.1.1", "2.2.2.2"} cfg.TargetPorts = []string{"80", "8080"} cfg.TargetProtos = []string{"tcp"} th.setup(&cfg) r.verifyCommands(t, []string{ "sudo pfctl -E", `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 80 src-ip 1.1.1.1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 80 dst-ip 1.1.1.1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 80 src-ip 2.2.2.2 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 80 dst-ip 2.2.2.2 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask src-port 80 src-ip 1.1.1.1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask src-port 80 dst-ip 1.1.1.1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask src-port 80 src-ip 2.2.2.2 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask src-port 80 dst-ip 2.2.2.2 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 8080 src-ip 1.1.1.1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 8080 dst-ip 1.1.1.1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 8080 src-ip 2.2.2.2 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 8080 dst-ip 2.2.2.2 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask src-port 8080 src-ip 1.1.1.1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask src-port 8080 dst-ip 1.1.1.1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask src-port 8080 src-ip 2.2.2.2 proto tcp`, `sudo dnctl pipe 1 config plr 0.0010 mask src-port 8080 dst-ip 2.2.2.2 proto tcp`, }) } func TestPfctlMixedIPv6Setup(t *testing.T) { r := newCmdRecorder() th := &pfctlThrottler{r} cfg := defaultTestConfig cfg.TargetProtos = []string{"icmp", "tcp"} cfg.PacketLoss = 0.2 cfg.TargetIps6 = []string{"2001:db8::1"} th.setup(&cfg) r.verifyCommands(t, []string{ `sudo pfctl -E`, `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 src-ip 10.10.10.10 proto icmp`, `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 src-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 dst-ip 10.10.10.10 proto icmp`, `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 dst-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 src-ip 10.10.10.10 proto icmp`, `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 src-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 dst-ip 10.10.10.10 proto icmp`, `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 dst-ip 10.10.10.10 proto tcp`, `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 src-ip6 2001:db8::1 proto ipv6-icmp`, `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 src-ip6 2001:db8::1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 dst-ip6 2001:db8::1 proto ipv6-icmp`, `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 dst-ip6 2001:db8::1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 src-ip6 2001:db8::1 proto ipv6-icmp`, `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 src-ip6 2001:db8::1 proto tcp`, `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 dst-ip6 2001:db8::1 proto ipv6-icmp`, `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 dst-ip6 2001:db8::1 proto tcp`, }) } ================================================ FILE: throttler/tc.go ================================================ package throttler import ( "fmt" "os/exec" "strconv" "strings" "syscall" ) const ( tcRootQDisc = `dev %s handle 10: root` tcRootExtra = `default 1` tcDefaultClass = `dev %s parent 10: classid 10:1` tcTargetClass = `dev %s parent 10: classid 10:10` tcNetemRule = `dev %s parent 10:10 handle 100:` tcRate = `rate %vkbit` tcDelay = `delay %vms` tcLoss = `loss %v%%` tcAddClass = `sudo tc class add` tcDelClass = `sudo tc class del` tcAddQDisc = `sudo tc qdisc add` tcDelQDisc = `sudo tc qdisc del` iptAddTarget = `sudo %s -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10` iptDelTarget = `sudo %s -D POSTROUTING -t mangle -j CLASSIFY --set-class 10:10` iptDestIP = `-d %s` iptProto = `-p %s` iptDestPorts = `--match multiport --dports %s` iptDestPort = `--dport %s` iptDelSearch = `class 0010:0010` iptList = `sudo %s -S -t mangle` ip4Tables = `iptables` ip6Tables = `ip6tables` iptDel = `sudo %s -t mangle -D` tcExists = `sudo tc qdisc show | grep "netem"` tcCheck = `sudo tc -s qdisc` ) type tcThrottler struct { c commander } func (t *tcThrottler) setup(cfg *Config) error { err := addRootQDisc(cfg, t.c) //The root node to append the filters if err != nil { return err } err = addDefaultClass(cfg, t.c) //The default class for all traffic that isn't classified if err != nil { return err } err = addTargetClass(cfg, t.c) //The class that the network emulator rule is assigned if err != nil { return err } err = addNetemRule(cfg, t.c) //The network emulator rule that contains the desired behavior if err != nil { return err } return addIptablesRules(cfg, t.c) //The network emulator rule that contains the desired behavior } func addRootQDisc(cfg *Config, c commander) error { //Add the root QDisc root := fmt.Sprintf(tcRootQDisc, cfg.Device) strs := []string{tcAddQDisc, root, "htb", tcRootExtra} cmd := strings.Join(strs, " ") return c.execute(cmd) } func addDefaultClass(cfg *Config, c commander) error { //Add the default Class def := fmt.Sprintf(tcDefaultClass, cfg.Device) rate := "" if cfg.DefaultBandwidth > 0 { rate = fmt.Sprintf(tcRate, cfg.DefaultBandwidth) } else { rate = fmt.Sprintf(tcRate, 1000000) } strs := []string{tcAddClass, def, "htb", rate} cmd := strings.Join(strs, " ") return c.execute(cmd) } func addTargetClass(cfg *Config, c commander) error { //Add the target Class tar := fmt.Sprintf(tcTargetClass, cfg.Device) rate := "" if cfg.TargetBandwidth > -1 { rate = fmt.Sprintf(tcRate, cfg.TargetBandwidth) } else { rate = fmt.Sprintf(tcRate, 1000000) } strs := []string{tcAddClass, tar, "htb", rate} cmd := strings.Join(strs, " ") return c.execute(cmd) } func addNetemRule(cfg *Config, c commander) error { //Add the Network Emulator rule net := fmt.Sprintf(tcNetemRule, cfg.Device) strs := []string{tcAddQDisc, net, "netem"} if cfg.Latency > 0 { strs = append(strs, fmt.Sprintf(tcDelay, cfg.Latency)) } if cfg.TargetBandwidth > -1 { strs = append(strs, fmt.Sprintf(tcRate, cfg.TargetBandwidth)) } if cfg.PacketLoss > 0 { strs = append(strs, fmt.Sprintf(tcLoss, strconv.FormatFloat(cfg.PacketLoss, 'f', 2, 64))) } cmd := strings.Join(strs, " ") return c.execute(cmd) } func addIptablesRules(cfg *Config, c commander) error { var err error if len(cfg.TargetIps) == 0 && len(cfg.TargetIps6) == 0 { if err == nil { err = addIptablesRulesForAddrs(cfg, c, ip4Tables, cfg.TargetIps) } if err == nil { err = addIptablesRulesForAddrs(cfg, c, ip6Tables, cfg.TargetIps6) } return err } if err == nil && len(cfg.TargetIps) > 0 { err = addIptablesRulesForAddrs(cfg, c, ip4Tables, cfg.TargetIps) } if err == nil && len(cfg.TargetIps6) > 0 { err = addIptablesRulesForAddrs(cfg, c, ip6Tables, cfg.TargetIps6) } return err } func addIptablesRulesForAddrs(cfg *Config, c commander, command string, addrs []string) error { rules := []string{} ports := "" if len(cfg.TargetPorts) > 0 { if len(cfg.TargetPorts) > 1 { prts := strings.Join(cfg.TargetPorts, ",") ports = fmt.Sprintf(iptDestPorts, prts) } else { ports = fmt.Sprintf(iptDestPort, cfg.TargetPorts[0]) } } addTargetCmd := fmt.Sprintf(iptAddTarget, command) if len(cfg.TargetProtos) > 0 { for _, ptc := range cfg.TargetProtos { proto := fmt.Sprintf(iptProto, ptc) rule := addTargetCmd + " " + proto if ptc != "icmp" { if ports != "" { rule += " " + ports } } rules = append(rules, rule) } } else { rules = []string{addTargetCmd} } if len(addrs) > 0 { iprules := []string{} for _, ip := range addrs { dest := fmt.Sprintf(iptDestIP, ip) if len(rules) > 0 { for _, rule := range rules { r := rule + " " + dest iprules = append(iprules, r) } } else { iprules = append(iprules, dest) } } if len(iprules) > 0 { rules = iprules } } for _, rule := range rules { if err := c.execute(rule); err != nil { return err } } return nil } func (t *tcThrottler) teardown(cfg *Config) error { if err := delIptablesRules(cfg, t.c); err != nil { return err } // The root node to append the filters if err := delRootQDisc(cfg, t.c); err != nil { return err } return nil } func delIptablesRules(cfg *Config, c commander) error { iptablesCommands := []string{ip4Tables, ip6Tables} for _, iptablesCommand := range iptablesCommands { if !c.commandExists(iptablesCommand) { continue } lines, err := c.executeGetLines(fmt.Sprintf(iptList, iptablesCommand)) if err != nil { // ignore exit code 3 from iptables, which might happen if the system // has the ip6tables command, but no IPv6 capabilities werr, ok := err.(*exec.ExitError) if !ok { return err } status, ok := werr.Sys().(syscall.WaitStatus) if !ok { return err } if status.ExitStatus() == 3 { continue } return err } delCmdPrefix := fmt.Sprintf(iptDel, iptablesCommand) for _, line := range lines { if strings.Contains(line, iptDelSearch) { cmd := strings.Replace(line, "-A", delCmdPrefix, 1) err = c.execute(cmd) if err != nil { return err } } } } return nil } func delRootQDisc(cfg *Config, c commander) error { //Delete the root QDisc root := fmt.Sprintf(tcRootQDisc, cfg.Device) strs := []string{tcDelQDisc, root} cmd := strings.Join(strs, " ") return c.execute(cmd) } func (t *tcThrottler) exists() bool { if dry { return false } err := t.c.execute(tcExists) return err == nil } func (t *tcThrottler) check() string { return tcCheck } ================================================ FILE: throttler/tc_test.go ================================================ package throttler import ( "testing" ) type cmdRecorder struct { commands []string responses map[string][]string cmdBlackList []string } func newCmdRecorder() *cmdRecorder { return &cmdRecorder{[]string{}, map[string][]string{}, []string{}} } func (r *cmdRecorder) execute(cmd string) error { r.commands = append(r.commands, cmd) return nil } func (r *cmdRecorder) executeGetLines(cmd string) ([]string, error) { r.execute(cmd) if responses, found := r.responses[cmd]; found { return responses, nil } return []string{}, nil } func (r *cmdRecorder) commandExists(cmd string) bool { for _, blackListed := range r.cmdBlackList { if blackListed == cmd { return false } } return true } func (r *cmdRecorder) verifyCommands(t *testing.T, expected []string) { if len(expected) != len(r.commands) { for i, cmd := range expected { t.Logf("Expected (%d): %s", i, cmd) } for i, cmd := range r.commands { t.Logf("Actual (%d): %s", i, cmd) } t.Fatalf("Expected to see %d commands, got %d", len(expected), len(r.commands)) } for i, cmd := range expected { if actual := r.commands[i]; actual != cmd { t.Fatalf("Expected to see command `%s`, got `%s`", cmd, actual) } } } var defaultTestConfig = Config{ Device: "eth0", Stop: false, Latency: -1, TargetBandwidth: -1, DefaultBandwidth: 20000, PacketLoss: 0.1, TargetIps: []string{"10.10.10.10"}, TargetPorts: []string{"80"}, TargetProtos: []string{"tcp"}, DryRun: false, } func TestTcPacketLossSetup(t *testing.T) { r := newCmdRecorder() th := &tcThrottler{r} cfg := defaultTestConfig cfg.Device = "eth1" cfg.PacketLoss = 0.2 th.setup(&cfg) r.verifyCommands(t, []string{ "sudo tc qdisc add dev eth1 handle 10: root htb default 1", "sudo tc class add dev eth1 parent 10: classid 10:1 htb rate 20000kbit", "sudo tc class add dev eth1 parent 10: classid 10:10 htb rate 1000000kbit", "sudo tc qdisc add dev eth1 parent 10:10 handle 100: netem loss 0.20%", "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 10.10.10.10", }) } func TestTcWildcardIps(t *testing.T) { r := newCmdRecorder() th := &tcThrottler{r} cfg := defaultTestConfig cfg.TargetIps = []string{} cfg.TargetPorts = []string{} cfg.TargetProtos = []string{} cfg.PacketLoss = -1 th.setup(&cfg) r.verifyCommands(t, []string{ "sudo tc qdisc add dev eth0 handle 10: root htb default 1", "sudo tc class add dev eth0 parent 10: classid 10:1 htb rate 20000kbit", "sudo tc class add dev eth0 parent 10: classid 10:10 htb rate 1000000kbit", "sudo tc qdisc add dev eth0 parent 10:10 handle 100: netem", "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10", "sudo ip6tables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10", }) } func TestTcMultiplePortsAndIps(t *testing.T) { r := newCmdRecorder() th := &tcThrottler{r} cfg := defaultTestConfig cfg.TargetIps = []string{"1.1.1.1", "2.2.2.2"} cfg.TargetPorts = []string{"80", "8080"} cfg.TargetProtos = []string{"tcp", "udp"} th.setup(&cfg) r.verifyCommands(t, []string{ "sudo tc qdisc add dev eth0 handle 10: root htb default 1", "sudo tc class add dev eth0 parent 10: classid 10:1 htb rate 20000kbit", "sudo tc class add dev eth0 parent 10: classid 10:10 htb rate 1000000kbit", "sudo tc qdisc add dev eth0 parent 10:10 handle 100: netem loss 0.10%", "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --match multiport --dports 80,8080 -d 1.1.1.1", "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p udp --match multiport --dports 80,8080 -d 1.1.1.1", "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --match multiport --dports 80,8080 -d 2.2.2.2", "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p udp --match multiport --dports 80,8080 -d 2.2.2.2", }) } func TestTcMixedIPv6Setup(t *testing.T) { r := newCmdRecorder() th := &tcThrottler{r} cfg := defaultTestConfig cfg.Device = "eth1" cfg.PacketLoss = 0.2 cfg.TargetIps6 = []string{"2001:db8::1"} th.setup(&cfg) r.verifyCommands(t, []string{ "sudo tc qdisc add dev eth1 handle 10: root htb default 1", "sudo tc class add dev eth1 parent 10: classid 10:1 htb rate 20000kbit", "sudo tc class add dev eth1 parent 10: classid 10:10 htb rate 1000000kbit", "sudo tc qdisc add dev eth1 parent 10:10 handle 100: netem loss 0.20%", "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 10.10.10.10", "sudo ip6tables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 2001:db8::1", }) } func TestTcTeardown(t *testing.T) { r := newCmdRecorder() th := &tcThrottler{r} r.responses = map[string][]string{ "sudo iptables -S -t mangle": { "-P PREROUTING ACCEPT", "-P INPUT ACCEPT", "-P FORWARD ACCEPT", "-P OUTPUT ACCEPT", "-P POSTROUTING ACCEPT", "-A POSTROUTING -d 10.10.10.10 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", }, "sudo ip6tables -S -t mangle": { "-P PREROUTING ACCEPT", "-P INPUT ACCEPT", "-P FORWARD ACCEPT", "-P OUTPUT ACCEPT", "-P POSTROUTING ACCEPT", }, } th.teardown(&defaultTestConfig) r.verifyCommands(t, []string{ "sudo iptables -S -t mangle", "sudo iptables -t mangle -D POSTROUTING -d 10.10.10.10 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", "sudo ip6tables -S -t mangle", "sudo tc qdisc del dev eth0 handle 10: root", }) } func TestTcTeardownNoIpTables(t *testing.T) { r := newCmdRecorder() th := &tcThrottler{r} th.teardown(&defaultTestConfig) r.verifyCommands(t, []string{ "sudo iptables -S -t mangle", "sudo ip6tables -S -t mangle", "sudo tc qdisc del dev eth0 handle 10: root", }) } func TestTcIPv6Teardown(t *testing.T) { r := newCmdRecorder() th := &tcThrottler{r} r.responses = map[string][]string{ "sudo iptables -S -t mangle": {}, "sudo ip6tables -S -t mangle": { "-P PREROUTING ACCEPT", "-P INPUT ACCEPT", "-P FORWARD ACCEPT", "-P OUTPUT ACCEPT", "-P POSTROUTING ACCEPT", "-A POSTROUTING -d 2001:db8::1 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", }, } config := defaultTestConfig th.teardown(&config) r.verifyCommands(t, []string{ "sudo iptables -S -t mangle", "sudo ip6tables -S -t mangle", "sudo ip6tables -t mangle -D POSTROUTING -d 2001:db8::1 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", "sudo tc qdisc del dev eth0 handle 10: root", }) } func TestTcTeardownNoIPv6(t *testing.T) { r := newCmdRecorder() r.cmdBlackList = []string{"ip6tables"} th := &tcThrottler{r} r.responses = map[string][]string{ "sudo iptables -S -t mangle": { "-P PREROUTING ACCEPT", "-P INPUT ACCEPT", "-P FORWARD ACCEPT", "-P OUTPUT ACCEPT", "-P POSTROUTING ACCEPT", "-A POSTROUTING -d 10.10.10.10 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", }, } th.teardown(&defaultTestConfig) r.verifyCommands(t, []string{ "sudo iptables -S -t mangle", "sudo iptables -t mangle -D POSTROUTING -d 10.10.10.10 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", "sudo tc qdisc del dev eth0 handle 10: root", }) } ================================================ FILE: throttler/throttler.go ================================================ package throttler import ( "bufio" "errors" "fmt" "os" "os/exec" "runtime" ) const ( linux = "linux" darwin = "darwin" freebsd = "freebsd" windows = "windows" checkOSXVersion = "sw_vers -productVersion" ipfw = "ipfw" pfctl = "pfctl" ) // Config specifies options for configuring packet filter rules. type Config struct { Device string Stop bool Latency int TargetBandwidth int DefaultBandwidth int PacketLoss float64 TargetIps []string TargetIps6 []string TargetPorts []string TargetProtos []string DryRun bool } type throttler interface { setup(*Config) error teardown(*Config) error exists() bool check() string } type commander interface { execute(string) error executeGetLines(string) ([]string, error) commandExists(string) bool } type dryRunCommander struct{} type shellCommander struct{} var dry bool func setup(t throttler, cfg *Config) { if t.exists() { fmt.Println("It looks like the packet rules are already setup") os.Exit(1) } if err := t.setup(cfg); err != nil { fmt.Println("I couldn't setup the packet rules:", err.Error()) os.Exit(1) } fmt.Println("Packet rules setup...") fmt.Printf("Run `%s` to double check\n", t.check()) fmt.Printf("Run `%s --device %s --stop` to reset\n", os.Args[0], cfg.Device) } func teardown(t throttler, cfg *Config) { if !t.exists() { fmt.Println("It looks like the packet rules aren't setup") os.Exit(1) } if err := t.teardown(cfg); err != nil { fmt.Println("Failed to stop packet controls") os.Exit(1) } fmt.Println("Packet rules stopped...") fmt.Printf("Run `%s` to double check\n", t.check()) fmt.Printf("Run `%s` to start\n", os.Args[0]) } // Run executes the packet filter operation, either setting it up or tearing // it down. func Run(cfg *Config) { dry = cfg.DryRun var t throttler var c commander if cfg.DryRun { c = &dryRunCommander{} } else { c = &shellCommander{} } switch runtime.GOOS { case freebsd: if cfg.Device == "" { fmt.Println("Device not specified, unable to default to eth0 on FreeBSD.") os.Exit(1) } t = &ipfwThrottler{c} case darwin: // Avoid OS version pinning and choose based on what's available if c.commandExists(pfctl) { t = &pfctlThrottler{c} } else if c.commandExists(ipfw) { t = &ipfwThrottler{c} } else { fmt.Println("Could not determine an appropriate firewall tool for OSX (tried pfctl, ipfw), exiting") os.Exit(1) } if cfg.Device == "" { cfg.Device = "eth0" } case linux: if cfg.Device == "" { cfg.Device = "eth0" } t = &tcThrottler{c} default: fmt.Printf("I don't support your OS: %s\n", runtime.GOOS) os.Exit(1) } if !cfg.Stop { setup(t, cfg) } else { teardown(t, cfg) } } func (c *dryRunCommander) execute(cmd string) error { fmt.Println(cmd) return nil } func (c *dryRunCommander) executeGetLines(cmd string) ([]string, error) { fmt.Println(cmd) return []string{}, nil } func (c *dryRunCommander) commandExists(cmd string) bool { return true } func (c *shellCommander) execute(cmd string) error { fmt.Println(cmd) return exec.Command("/bin/sh", "-c", cmd).Run() } func (c *shellCommander) executeGetLines(cmd string) ([]string, error) { lines := []string{} child := exec.Command("/bin/sh", "-c", cmd) out, err := child.StdoutPipe() if err != nil { return []string{}, err } err = child.Start() if err != nil { return []string{}, err } scanner := bufio.NewScanner(out) for scanner.Scan() { lines = append(lines, scanner.Text()) } if err := scanner.Err(); err != nil { return []string{}, errors.New(fmt.Sprint("Error reading standard input:", err)) } err = child.Wait() if err != nil { return []string{}, err } return lines, nil } func (c *shellCommander) commandExists(cmd string) bool { _, err := exec.LookPath(cmd) return err == nil }