├── .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 /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: golang 2 | 3 | go: 4 | - 1.8.x 5 | - 1.9.x 6 | - 1.10.x 7 | - 1.11.x 8 | 9 | script: 10 | - go test -v ./throttler/... 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BINARY="comcast" 2 | VERSION=1.0.0 3 | BUILD=`date +%FT%T%z` 4 | DIR=bin 5 | 6 | PACKAGES=`go list ./... | grep -v /vendor/` 7 | VETPACKAGES=`go list ./... | grep -v /vendor/ | grep -v /examples/` 8 | GOFILES=`find . -name "*.go" -type f -not -path "./vendor/*"` 9 | 10 | default: 11 | @mkdir ${DIR} 12 | @go build -o ${DIR}/${BINARY} -tags=jsoniter 13 | 14 | list: 15 | @echo ${PACKAGES} 16 | @echo ${VETPACKAGES} 17 | @echo ${GOFILES} 18 | 19 | fmt: 20 | @gofmt -s -w ${GOFILES} 21 | 22 | fmt-check: 23 | @diff=?(gofmt -s -d $(GOFILES)); \ 24 | if [ -n "$$diff" ]; then \ 25 | echo "Please run 'make fmt' and commit the result:"; \ 26 | echo "$${diff}"; \ 27 | exit 1; \ 28 | fi; 29 | 30 | install: 31 | @govendor sync -v 32 | 33 | test: 34 | @go test -cpu=1,2,4 -v -tags integration ./... 35 | 36 | vet: 37 | @go vet $(VETPACKAGES) 38 | 39 | 40 | clean: 41 | @if [ -f${DIR}/${BINARY} ] ; then rm -rf ${DIR} ; fi 42 | 43 | .PHONY: default fmt fmt-check install test vet docker clean 44 | 45 | 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Comcast 2 | 3 | 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. 4 | 5 | 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. 6 | 7 | ## Installation 8 | 9 | ``` 10 | $ go install github.com/tylertreat/comcast@latest 11 | ``` 12 | 13 | ### Path addition may be needed 14 | 15 | 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`. 16 | ``` 17 | $ export PATH=$PATH:$HOME/go/bin 18 | ``` 19 | 20 | ## Usage 21 | 22 | On Linux, Comcast supports several options: device, latency, target/default bandwidth, packet loss, protocol, and port number. 23 | 24 | ``` 25 | $ 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 26 | ``` 27 | 28 | 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. 29 | 30 | On BSD (with `ipfw`), Comcast currently supports only: device, latency, target bandwidth, and packet loss. 31 | 32 | ``` 33 | $ comcast --device=eth0 --latency=250 --target-bw=1000 --packet-loss=10% 34 | ``` 35 | 36 | 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: 37 | 38 | ``` 39 | $ comcast --stop 40 | ``` 41 | 42 | By default, comcast will determine the system commands to execute, log them to stdout, and execute them. The `--dry-run` flag will skip execution. 43 | 44 | ## I don't trust you, this code sucks, I hate Go, etc. 45 | 46 | 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. 47 | 48 | ### Linux 49 | 50 | On Linux, you can use `iptables` to drop incoming and outgoing packets. 51 | 52 | ``` 53 | $ iptables -A INPUT -m statistic --mode random --probability 0.1 -j DROP 54 | $ iptables -A OUTPUT -m statistic --mode random --probability 0.1 -j DROP 55 | ``` 56 | 57 | Alternatively, you can use `tc` which supports some additional options. 58 | 59 | ``` 60 | $ tc qdisc add dev eth0 root netem delay 50ms 20ms distribution normal 61 | $ tc qdisc change dev eth0 root netem reorder 0.02 duplicate 0.05 corrupt 0.01 62 | ``` 63 | 64 | To reset: 65 | 66 | ``` 67 | $ tc qdisc del dev eth0 root netem 68 | ``` 69 | 70 | ### BSD/OSX 71 | 72 | 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. 73 | 74 | ``` 75 | $ ipfw add 1 pipe 1 ip from me to any 76 | $ ipfw add 2 pipe 1 ip from any to me 77 | $ ipfw pipe 1 config delay 500ms bw 1Mbit/s plr 0.1 78 | ``` 79 | 80 | To reset: 81 | 82 | ``` 83 | $ ipfw delete 1 84 | ``` 85 | 86 | *Note: `ipfw` was removed in OSX Yosemite in favor of `pfctl`.* 87 | 88 | ## Network Condition Profiles 89 | 90 | Here's a list of network conditions with values that you can plug into Comcast. Please add any more that you may come across. 91 | 92 | Name | Latency | Bandwidth | Packet-loss 93 | :-- | --: | --: | --: 94 | GPRS (good) | 500 | 50 | 2 95 | EDGE (good) | 300 | 250 | 1.5 96 | 3G/HSDPA (good) | 250 | 750 | 1.5 97 | DIAL-UP (good) | 185 | 40 | 2 98 | DSL (poor) | 70 | 2000 | 2 99 | DSL (good) | 40 | 8000 | 0.5 100 | WIFI (good) | 40 | 30000 | 0.2 101 | Starlink | 20 | - | 2.5 102 | -------------------------------------------------------------------------------- /comcast.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "net" 7 | "os" 8 | "strconv" 9 | "strings" 10 | 11 | "github.com/tylertreat/comcast/throttler" 12 | ) 13 | 14 | const version = "1.0.0" 15 | 16 | func main() { 17 | // TODO: Add support for other options like packet reordering, duplication, etc. 18 | var ( 19 | device = flag.String("device", "", "Interface (device) to use (defaults to eth0 where applicable)") 20 | stop = flag.Bool("stop", false, "Stop packet controls") 21 | latency = flag.Int("latency", -1, "Latency to add in ms") 22 | targetbw = flag.Int("target-bw", -1, "Target bandwidth limit in kbit/s (slow-lane)") 23 | defaultbw = flag.Int("default-bw", -1, "Default bandwidth limit in kbit/s (fast-lane)") 24 | packetLoss = flag.String("packet-loss", "0", "Packet loss percentage (e.g. 0.1%)") 25 | 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)") 26 | targetport = flag.String("target-port", "", "Target port(s) (e.g. 80 or 1:65535 or 22,80,443,1000:1010)") 27 | targetproto = flag.String("target-proto", "tcp,udp,icmp", "Target protocol TCP/UDP (e.g. tcp or tcp,udp or icmp)") 28 | dryrun = flag.Bool("dry-run", false, "Specifies whether or not to actually commit the rule changes") 29 | //icmptype = flag.String("icmp-type", "", "icmp message type (e.g. reply or reply,request)") //TODO: Maybe later :3 30 | vers = flag.Bool("version", false, "Print Comcast's version") 31 | ) 32 | flag.Parse() 33 | 34 | if *vers { 35 | fmt.Printf("Comcast version %s\n", version) 36 | return 37 | } 38 | 39 | targetIPv4, targetIPv6 := parseAddrs(*targetaddr) 40 | 41 | throttler.Run(&throttler.Config{ 42 | Device: *device, 43 | Stop: *stop, 44 | Latency: *latency, 45 | TargetBandwidth: *targetbw, 46 | DefaultBandwidth: *defaultbw, 47 | PacketLoss: parseLoss(*packetLoss), 48 | TargetIps: targetIPv4, 49 | TargetIps6: targetIPv6, 50 | TargetPorts: parsePorts(*targetport), 51 | TargetProtos: parseProtos(*targetproto), 52 | DryRun: *dryrun, 53 | }) 54 | } 55 | 56 | func parseLoss(loss string) float64 { 57 | val := loss 58 | if strings.Contains(loss, "%") { 59 | val = loss[:len(loss)-1] 60 | } 61 | l, err := strconv.ParseFloat(val, 64) 62 | if err != nil { 63 | fmt.Println("Incorrectly specified packet loss:", loss) 64 | os.Exit(1) 65 | } 66 | return l 67 | } 68 | 69 | func parseAddrs(addrs string) ([]string, []string) { 70 | adrs := strings.Split(addrs, ",") 71 | parsedIPv4 := []string{} 72 | parsedIPv6 := []string{} 73 | 74 | if addrs != "" { 75 | for _, adr := range adrs { 76 | ip := net.ParseIP(adr) 77 | if ip != nil { 78 | if ip.To4() != nil { 79 | parsedIPv4 = append(parsedIPv4, adr) 80 | } else { 81 | parsedIPv6 = append(parsedIPv6, adr) 82 | } 83 | } else { //Not a valid single IP, could it be a CIDR? 84 | parsedIP, net, err := net.ParseCIDR(adr) 85 | if err == nil { 86 | if parsedIP.To4() != nil { 87 | parsedIPv4 = append(parsedIPv4, net.String()) 88 | } else { 89 | parsedIPv6 = append(parsedIPv6, net.String()) 90 | } 91 | } else { 92 | fmt.Println("Incorrectly specified target IP or CIDR:", adr) 93 | os.Exit(1) 94 | } 95 | } 96 | } 97 | } 98 | 99 | return parsedIPv4, parsedIPv6 100 | } 101 | 102 | func parsePorts(ports string) []string { 103 | prts := strings.Split(ports, ",") 104 | parsed := []string{} 105 | 106 | if ports != "" { 107 | for _, prt := range prts { 108 | if strings.Contains(prt, ":") { 109 | if validRange(prt) { 110 | parsed = append(parsed, prt) 111 | } else { 112 | fmt.Println("Incorrectly specified port range:", prt) 113 | os.Exit(1) 114 | } 115 | } else { //Isn't a range, check if just a single port 116 | if validPort(prt) { 117 | parsed = append(parsed, prt) 118 | } else { 119 | fmt.Println("Incorrectly specified port:", prt) 120 | os.Exit(1) 121 | } 122 | } 123 | } 124 | } 125 | 126 | return parsed 127 | } 128 | 129 | func parsePort(port string) int { 130 | prt, err := strconv.Atoi(port) 131 | if err != nil { 132 | return 0 133 | } 134 | 135 | return prt 136 | } 137 | 138 | func validPort(port string) bool { 139 | prt := parsePort(port) 140 | return prt > 0 && prt < 65536 141 | } 142 | 143 | func validRange(ports string) bool { 144 | pr := strings.Split(ports, ":") 145 | 146 | if len(pr) == 2 { 147 | if !validPort(pr[0]) || !validPort(pr[1]) { 148 | return false 149 | } 150 | 151 | if portHigher(pr[0], pr[1]) { 152 | return false 153 | } 154 | } else { 155 | return false 156 | } 157 | 158 | return true 159 | } 160 | 161 | func portHigher(prt1, prt2 string) bool { 162 | p1 := parsePort(prt1) 163 | p2 := parsePort(prt2) 164 | 165 | return p1 > p2 166 | } 167 | 168 | func parseProtos(protos string) []string { 169 | ptcs := strings.Split(protos, ",") 170 | parsed := []string{} 171 | 172 | if protos != "" { 173 | for _, ptc := range ptcs { 174 | p := strings.ToLower(ptc) 175 | if p == "udp" || 176 | p == "tcp" || 177 | p == "icmp" { 178 | parsed = append(parsed, p) 179 | } else { 180 | fmt.Println("Incorrectly specified protocol:", p) 181 | os.Exit(1) 182 | } 183 | } 184 | } 185 | 186 | return parsed 187 | } 188 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tylertreat/comcast 2 | 3 | go 1.15 4 | 5 | require github.com/tylertreat/comcast v1.0.1 6 | -------------------------------------------------------------------------------- /throttler/ipfw.go: -------------------------------------------------------------------------------- 1 | package throttler 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | const ( 8 | ipfwAddPipe = `sudo ipfw add 1 pipe 1 ip from any to any via ` 9 | ipfwTeardown = `sudo ipfw delete 1` 10 | ipfwConfig = `sudo ipfw pipe 1 config` 11 | ipfwExists = `sudo ipfw list | grep "pipe 1"` 12 | ipfwCheck = `sudo ipfw list` 13 | ) 14 | 15 | type ipfwThrottler struct { 16 | c commander 17 | } 18 | 19 | func (i *ipfwThrottler) setup(c *Config) error { 20 | cmd := ipfwAddPipe + c.Device 21 | err := i.c.execute(cmd) 22 | if err != nil { 23 | return err 24 | } 25 | 26 | configCmd := i.buildConfigCommand(c) 27 | err = i.c.execute(configCmd) 28 | return err 29 | } 30 | 31 | func (i *ipfwThrottler) teardown(_ *Config) error { 32 | err := i.c.execute(ipfwTeardown) 33 | return err 34 | } 35 | 36 | func (i *ipfwThrottler) exists() bool { 37 | if dry { 38 | return false 39 | } 40 | err := i.c.execute(ipfwExists) 41 | return err == nil 42 | } 43 | 44 | func (i *ipfwThrottler) check() string { 45 | return ipfwCheck 46 | } 47 | 48 | func (i *ipfwThrottler) buildConfigCommand(c *Config) string { 49 | cmd := ipfwConfig 50 | 51 | if c.Latency > 0 { 52 | cmd = cmd + " delay " + strconv.Itoa(c.Latency) + "ms" 53 | } 54 | 55 | if c.TargetBandwidth > 0 { 56 | cmd = cmd + " bw " + strconv.Itoa(c.TargetBandwidth) + "Kbit/s" 57 | } 58 | 59 | if c.PacketLoss > 0 { 60 | cmd = cmd + " plr " + strconv.FormatFloat(c.PacketLoss/100, 'f', 4, 64) 61 | } 62 | 63 | return cmd 64 | } 65 | -------------------------------------------------------------------------------- /throttler/pfctl.go: -------------------------------------------------------------------------------- 1 | package throttler 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "strings" 7 | ) 8 | 9 | const ( 10 | // TODO: use printf in favour of echo due to shell portability issues 11 | pfctlCreateAnchor = `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -` 12 | pfctlTeardown = `sudo pfctl -f /etc/pf.conf` 13 | dnctl = `sudo dnctl pipe 1 config` 14 | pfctlCreateDummynet = `echo $'dummynet in on %s all pipe 1'` 15 | pfctlExecuteInline = `%s | sudo pfctl -a mop -f - ` 16 | pfctlEnableFirewall = `sudo pfctl -E` 17 | pfctlEnableFwRegex = `pf enabled` 18 | pfctlDisableFirewall = `sudo pfctl -d` 19 | pfctlDisbleFwRegex = `pf disabled` 20 | pfctlIsEnabled = `sudo pfctl -sa | grep -i enabled` 21 | dnctlIsConfigured = `sudo dnctl show` 22 | pfctlIsEnabledRegex = `Enabled` 23 | dnctlTeardown = `sudo dnctl -q flush` 24 | ) 25 | 26 | type pfctlThrottler struct { 27 | c commander 28 | } 29 | 30 | // Execute a command and check that any matching line in the result contains 'match' 31 | func (i *pfctlThrottler) executeAndParse(cmd string, match string) bool { 32 | lines, err := i.c.executeGetLines(cmd) 33 | 34 | if err != nil { 35 | return false 36 | } 37 | 38 | for _, line := range lines { 39 | if strings.Contains(line, match) { 40 | return true 41 | } 42 | } 43 | return false 44 | } 45 | 46 | func (i *pfctlThrottler) setup(c *Config) error { 47 | // Enable firewall 48 | err := i.c.execute(pfctlEnableFirewall) 49 | if err != nil { 50 | return fmt.Errorf("Could not enable firewall using: `%s`. Error: %s", pfctlEnableFirewall, err.Error()) 51 | } 52 | 53 | // Add the dummynet and anchor 54 | err = i.c.execute(pfctlCreateAnchor) 55 | if err != nil { 56 | return fmt.Errorf("Could not create anchor rule for dummynet using: `%s`. Error: %s", pfctlCreateAnchor, err.Error()) 57 | } 58 | 59 | // Add 'execute' portion of the command 60 | input := fmt.Sprintf(pfctlCreateDummynet, c.Device) 61 | cmd := fmt.Sprintf(pfctlExecuteInline, input) 62 | 63 | err = i.c.execute(cmd) 64 | if err != nil { 65 | return fmt.Errorf("Could not create dummynet using: `%s`. Error: %s", input, err.Error()) 66 | } 67 | 68 | // Apply the shaping etc. 69 | for _, cmd := range i.buildConfigCommand(c) { 70 | err = i.c.execute(cmd) 71 | if err != nil { 72 | return err 73 | } 74 | } 75 | 76 | return nil 77 | } 78 | 79 | func (i *pfctlThrottler) teardown(_ *Config) error { 80 | 81 | // Reset firewall rules, leave it running 82 | err := i.c.execute(pfctlTeardown) 83 | if err != nil { 84 | return fmt.Errorf("Could not remove firewall rules using: `%s`. Error: %s", pfctlTeardown, err.Error()) 85 | } 86 | 87 | // Turn off the firewall, discarding any rules 88 | err = i.c.execute(pfctlDisableFirewall) 89 | if err != nil { 90 | return fmt.Errorf("Could not disable firewall using: `%s`. Error: %s", pfctlDisableFirewall, err.Error()) 91 | } 92 | 93 | // Disable dnctl rules 94 | err = i.c.execute(dnctlTeardown) 95 | if err != nil { 96 | return fmt.Errorf("Could not disable dnctl rules using: `%s`. Error: %s", dnctlTeardown, err.Error()) 97 | } 98 | 99 | return nil 100 | } 101 | 102 | func (i *pfctlThrottler) isFirewallRunning() bool { 103 | return i.executeAndParse(pfctlIsEnabled, pfctlIsEnabledRegex) 104 | } 105 | func (i *pfctlThrottler) exists() bool { 106 | if dry { 107 | return false 108 | } 109 | return i.executeAndParse(dnctlIsConfigured, "port") || i.isFirewallRunning() 110 | } 111 | 112 | func (i *pfctlThrottler) check() string { 113 | return pfctlIsEnabled 114 | } 115 | 116 | func addProtosToCommands(cmds []string, protos []string) []string { 117 | commands := make([]string, 0) 118 | 119 | for _, cmd := range cmds { 120 | for _, proto := range protos { 121 | commands = append(commands, fmt.Sprintf("%s proto %s", cmd, proto)) 122 | } 123 | } 124 | 125 | return commands 126 | } 127 | func addPortsToCommand(cmd string, ports []string) []string { 128 | commands := make([]string, 0) 129 | 130 | for _, port := range ports { 131 | commands = append(commands, fmt.Sprintf("%s dst-port %s", cmd, port)) 132 | commands = append(commands, fmt.Sprintf("%s src-port %s", cmd, port)) 133 | } 134 | 135 | return commands 136 | } 137 | 138 | // Takes care of the annoying differences between ipv4 and ipv6 139 | func addIpsAndProtoToCommands(ipVersion int, cmds []string, ips []string, protos []string) []string { 140 | 141 | commands := make([]string, 0) 142 | 143 | for _, cmd := range cmds { 144 | for _, ip := range ips { 145 | srcIpFlag := "src-ip" 146 | dstIpFlag := "dst-ip" 147 | if ipVersion == 6 { 148 | srcIpFlag = "src-ip6" 149 | dstIpFlag = "dst-ip6" 150 | } 151 | 152 | commands = append(commands, addProtoToCommands(ipVersion, fmt.Sprintf("%s %s %s", cmd, srcIpFlag, ip), protos)...) 153 | commands = append(commands, addProtoToCommands(ipVersion, fmt.Sprintf("%s %s %s", cmd, dstIpFlag, ip), protos)...) 154 | } 155 | } 156 | 157 | return commands 158 | } 159 | 160 | func addProtoToCommands(ipVersion int, cmd string, protos []string) []string { 161 | commands := make([]string, 0) 162 | for _, proto := range protos { 163 | if ipVersion == 6 { 164 | if proto == "icmp" { 165 | proto = "ipv6-icmp" 166 | } 167 | } 168 | commands = append(commands, fmt.Sprintf("%s proto %s", cmd, proto)) 169 | } 170 | return commands 171 | } 172 | 173 | func (i *pfctlThrottler) buildConfigCommand(c *Config) []string { 174 | 175 | cmd := dnctl 176 | 177 | // Add all non tcp version dependent stuff first... 178 | if c.Latency > 0 { 179 | cmd = cmd + " delay " + strconv.Itoa(c.Latency) + "ms" 180 | } 181 | 182 | if c.TargetBandwidth > 0 { 183 | cmd = cmd + " bw " + strconv.Itoa(c.TargetBandwidth) + "Kbit/s" 184 | } 185 | 186 | if c.PacketLoss > 0 { 187 | cmd = cmd + " plr " + strconv.FormatFloat(c.PacketLoss/100, 'f', 4, 64) 188 | } 189 | 190 | // Add Mask keyword if we have pipe qualifiers 191 | if len(c.TargetPorts) > 0 || len(c.TargetProtos) > 0 || len(c.TargetIps) > 0 || len(c.TargetIps6) > 0 { 192 | cmd = cmd + " mask " 193 | } 194 | 195 | // Expand commands with ports 196 | commands := []string{cmd} 197 | 198 | if len(c.TargetPorts) > 0 { 199 | commands = addPortsToCommand(cmd, c.TargetPorts) 200 | } 201 | 202 | if len(c.TargetIps) == 0 && len(c.TargetIps6) == 0 { 203 | if len(c.TargetProtos) > 0 { 204 | return addProtosToCommands(commands, c.TargetProtos) 205 | } 206 | return commands 207 | } 208 | 209 | // create and combine the ipv4 and ipv6 IPs with the protocol version specific keywords 210 | return append(addIpsAndProtoToCommands(4, commands, c.TargetIps, c.TargetProtos), addIpsAndProtoToCommands(6, commands, c.TargetIps6, c.TargetProtos)...) 211 | } 212 | -------------------------------------------------------------------------------- /throttler/pfctl_test.go: -------------------------------------------------------------------------------- 1 | package throttler 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestPfctlDefaultConfigCommand(t *testing.T) { 8 | 9 | r := newCmdRecorder() 10 | th := &pfctlThrottler{r} 11 | c := defaultTestConfig 12 | c.PacketLoss = 0 13 | c.TargetIps = []string{} 14 | c.TargetIps6 = []string{} 15 | c.TargetBandwidth = -1 16 | c.TargetPorts = []string{} 17 | c.TargetProtos = []string{"tcp,udp,icmp"} 18 | 19 | th.setup(&c) 20 | r.verifyCommands(t, []string{ 21 | "sudo pfctl -E", 22 | `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, 23 | `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, 24 | `sudo dnctl pipe 1 config mask proto tcp,udp,icmp`, 25 | }) 26 | } 27 | 28 | func TestPfctlThrottleOnlyConfigCommand(t *testing.T) { 29 | 30 | var c = Config{ 31 | Device: "eth0", 32 | Stop: false, 33 | Latency: -1, 34 | TargetBandwidth: -1, 35 | DefaultBandwidth: 20000, 36 | PacketLoss: 0.1, 37 | } 38 | r := newCmdRecorder() 39 | th := &pfctlThrottler{r} 40 | 41 | th.setup(&c) 42 | r.verifyCommands(t, []string{ 43 | "sudo pfctl -E", 44 | `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, 45 | `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, 46 | `sudo dnctl pipe 1 config plr 0.0010`, 47 | }) 48 | } 49 | func TestPfctlNoIPThrottleConfigCommand(t *testing.T) { 50 | 51 | var c = Config{ 52 | Device: "eth0", 53 | Stop: false, 54 | Latency: -1, 55 | TargetBandwidth: -1, 56 | DefaultBandwidth: 20000, 57 | PacketLoss: 0.1, 58 | TargetProtos: []string{"tcp"}, 59 | } 60 | r := newCmdRecorder() 61 | th := &pfctlThrottler{r} 62 | 63 | th.setup(&c) 64 | r.verifyCommands(t, []string{ 65 | "sudo pfctl -E", 66 | `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, 67 | `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, 68 | `sudo dnctl pipe 1 config plr 0.0010 mask proto tcp`, 69 | }) 70 | } 71 | 72 | func TestPfctlPacketSetup(t *testing.T) { 73 | 74 | r := newCmdRecorder() 75 | th := &pfctlThrottler{r} 76 | c := defaultTestConfig 77 | c.PacketLoss = 0.5 78 | 79 | th.setup(&c) 80 | r.verifyCommands(t, []string{ 81 | "sudo pfctl -E", 82 | `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, 83 | `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, 84 | `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 src-ip 10.10.10.10 proto tcp`, 85 | `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 dst-ip 10.10.10.10 proto tcp`, 86 | `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 src-ip 10.10.10.10 proto tcp`, 87 | `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 dst-ip 10.10.10.10 proto tcp`, 88 | }) 89 | } 90 | 91 | func TestPfctlProtoSetup(t *testing.T) { 92 | 93 | r := newCmdRecorder() 94 | th := &pfctlThrottler{r} 95 | c := defaultTestConfig 96 | c.PacketLoss = 0.5 97 | c.TargetProtos = []string{"tcp", "udp", "icmp"} 98 | 99 | th.setup(&c) 100 | r.verifyCommands(t, []string{ 101 | "sudo pfctl -E", 102 | `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, 103 | `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, 104 | `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 src-ip 10.10.10.10 proto tcp`, 105 | `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 src-ip 10.10.10.10 proto udp`, 106 | `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 src-ip 10.10.10.10 proto icmp`, 107 | `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 dst-ip 10.10.10.10 proto tcp`, 108 | `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 dst-ip 10.10.10.10 proto udp`, 109 | `sudo dnctl pipe 1 config plr 0.0050 mask dst-port 80 dst-ip 10.10.10.10 proto icmp`, 110 | `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 src-ip 10.10.10.10 proto tcp`, 111 | `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 src-ip 10.10.10.10 proto udp`, 112 | `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 src-ip 10.10.10.10 proto icmp`, 113 | `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 dst-ip 10.10.10.10 proto tcp`, 114 | `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 dst-ip 10.10.10.10 proto udp`, 115 | `sudo dnctl pipe 1 config plr 0.0050 mask src-port 80 dst-ip 10.10.10.10 proto icmp`, 116 | }) 117 | } 118 | 119 | func TestPfctlMultiplePortsAndIps(t *testing.T) { 120 | r := newCmdRecorder() 121 | th := &pfctlThrottler{r} 122 | cfg := defaultTestConfig 123 | cfg.TargetIps = []string{"1.1.1.1", "2.2.2.2"} 124 | cfg.TargetPorts = []string{"80", "8080"} 125 | cfg.TargetProtos = []string{"tcp"} 126 | th.setup(&cfg) 127 | r.verifyCommands(t, []string{ 128 | "sudo pfctl -E", 129 | `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, 130 | `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, 131 | `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 80 src-ip 1.1.1.1 proto tcp`, 132 | `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 80 dst-ip 1.1.1.1 proto tcp`, 133 | `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 80 src-ip 2.2.2.2 proto tcp`, 134 | `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 80 dst-ip 2.2.2.2 proto tcp`, 135 | `sudo dnctl pipe 1 config plr 0.0010 mask src-port 80 src-ip 1.1.1.1 proto tcp`, 136 | `sudo dnctl pipe 1 config plr 0.0010 mask src-port 80 dst-ip 1.1.1.1 proto tcp`, 137 | `sudo dnctl pipe 1 config plr 0.0010 mask src-port 80 src-ip 2.2.2.2 proto tcp`, 138 | `sudo dnctl pipe 1 config plr 0.0010 mask src-port 80 dst-ip 2.2.2.2 proto tcp`, 139 | `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 8080 src-ip 1.1.1.1 proto tcp`, 140 | `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 8080 dst-ip 1.1.1.1 proto tcp`, 141 | `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 8080 src-ip 2.2.2.2 proto tcp`, 142 | `sudo dnctl pipe 1 config plr 0.0010 mask dst-port 8080 dst-ip 2.2.2.2 proto tcp`, 143 | `sudo dnctl pipe 1 config plr 0.0010 mask src-port 8080 src-ip 1.1.1.1 proto tcp`, 144 | `sudo dnctl pipe 1 config plr 0.0010 mask src-port 8080 dst-ip 1.1.1.1 proto tcp`, 145 | `sudo dnctl pipe 1 config plr 0.0010 mask src-port 8080 src-ip 2.2.2.2 proto tcp`, 146 | `sudo dnctl pipe 1 config plr 0.0010 mask src-port 8080 dst-ip 2.2.2.2 proto tcp`, 147 | }) 148 | } 149 | 150 | func TestPfctlMixedIPv6Setup(t *testing.T) { 151 | r := newCmdRecorder() 152 | th := &pfctlThrottler{r} 153 | cfg := defaultTestConfig 154 | cfg.TargetProtos = []string{"icmp", "tcp"} 155 | cfg.PacketLoss = 0.2 156 | cfg.TargetIps6 = []string{"2001:db8::1"} 157 | th.setup(&cfg) 158 | r.verifyCommands(t, []string{ 159 | `sudo pfctl -E`, 160 | `(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | sudo pfctl -f -`, 161 | `echo $'dummynet in all pipe 1' | sudo pfctl -a mop -f - `, 162 | 163 | `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 src-ip 10.10.10.10 proto icmp`, 164 | `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 src-ip 10.10.10.10 proto tcp`, 165 | `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 dst-ip 10.10.10.10 proto icmp`, 166 | `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 dst-ip 10.10.10.10 proto tcp`, 167 | `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 src-ip 10.10.10.10 proto icmp`, 168 | `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 src-ip 10.10.10.10 proto tcp`, 169 | `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 dst-ip 10.10.10.10 proto icmp`, 170 | `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 dst-ip 10.10.10.10 proto tcp`, 171 | `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 src-ip6 2001:db8::1 proto ipv6-icmp`, 172 | `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 src-ip6 2001:db8::1 proto tcp`, 173 | `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 dst-ip6 2001:db8::1 proto ipv6-icmp`, 174 | `sudo dnctl pipe 1 config plr 0.0020 mask dst-port 80 dst-ip6 2001:db8::1 proto tcp`, 175 | `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 src-ip6 2001:db8::1 proto ipv6-icmp`, 176 | `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 src-ip6 2001:db8::1 proto tcp`, 177 | `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 dst-ip6 2001:db8::1 proto ipv6-icmp`, 178 | `sudo dnctl pipe 1 config plr 0.0020 mask src-port 80 dst-ip6 2001:db8::1 proto tcp`, 179 | }) 180 | } 181 | -------------------------------------------------------------------------------- /throttler/tc.go: -------------------------------------------------------------------------------- 1 | package throttler 2 | 3 | import ( 4 | "fmt" 5 | "os/exec" 6 | "strconv" 7 | "strings" 8 | "syscall" 9 | ) 10 | 11 | const ( 12 | tcRootQDisc = `dev %s handle 10: root` 13 | tcRootExtra = `default 1` 14 | tcDefaultClass = `dev %s parent 10: classid 10:1` 15 | tcTargetClass = `dev %s parent 10: classid 10:10` 16 | tcNetemRule = `dev %s parent 10:10 handle 100:` 17 | tcRate = `rate %vkbit` 18 | tcDelay = `delay %vms` 19 | tcLoss = `loss %v%%` 20 | tcAddClass = `sudo tc class add` 21 | tcDelClass = `sudo tc class del` 22 | tcAddQDisc = `sudo tc qdisc add` 23 | tcDelQDisc = `sudo tc qdisc del` 24 | iptAddTarget = `sudo %s -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10` 25 | iptDelTarget = `sudo %s -D POSTROUTING -t mangle -j CLASSIFY --set-class 10:10` 26 | iptDestIP = `-d %s` 27 | iptProto = `-p %s` 28 | iptDestPorts = `--match multiport --dports %s` 29 | iptDestPort = `--dport %s` 30 | iptDelSearch = `class 0010:0010` 31 | iptList = `sudo %s -S -t mangle` 32 | ip4Tables = `iptables` 33 | ip6Tables = `ip6tables` 34 | iptDel = `sudo %s -t mangle -D` 35 | tcExists = `sudo tc qdisc show | grep "netem"` 36 | tcCheck = `sudo tc -s qdisc` 37 | ) 38 | 39 | type tcThrottler struct { 40 | c commander 41 | } 42 | 43 | func (t *tcThrottler) setup(cfg *Config) error { 44 | err := addRootQDisc(cfg, t.c) //The root node to append the filters 45 | if err != nil { 46 | return err 47 | } 48 | 49 | err = addDefaultClass(cfg, t.c) //The default class for all traffic that isn't classified 50 | if err != nil { 51 | return err 52 | } 53 | 54 | err = addTargetClass(cfg, t.c) //The class that the network emulator rule is assigned 55 | if err != nil { 56 | return err 57 | } 58 | 59 | err = addNetemRule(cfg, t.c) //The network emulator rule that contains the desired behavior 60 | if err != nil { 61 | return err 62 | } 63 | 64 | return addIptablesRules(cfg, t.c) //The network emulator rule that contains the desired behavior 65 | } 66 | 67 | func addRootQDisc(cfg *Config, c commander) error { 68 | //Add the root QDisc 69 | root := fmt.Sprintf(tcRootQDisc, cfg.Device) 70 | strs := []string{tcAddQDisc, root, "htb", tcRootExtra} 71 | cmd := strings.Join(strs, " ") 72 | 73 | return c.execute(cmd) 74 | } 75 | 76 | func addDefaultClass(cfg *Config, c commander) error { 77 | //Add the default Class 78 | def := fmt.Sprintf(tcDefaultClass, cfg.Device) 79 | rate := "" 80 | 81 | if cfg.DefaultBandwidth > 0 { 82 | rate = fmt.Sprintf(tcRate, cfg.DefaultBandwidth) 83 | } else { 84 | rate = fmt.Sprintf(tcRate, 1000000) 85 | } 86 | 87 | strs := []string{tcAddClass, def, "htb", rate} 88 | cmd := strings.Join(strs, " ") 89 | 90 | return c.execute(cmd) 91 | } 92 | 93 | func addTargetClass(cfg *Config, c commander) error { 94 | //Add the target Class 95 | tar := fmt.Sprintf(tcTargetClass, cfg.Device) 96 | rate := "" 97 | 98 | if cfg.TargetBandwidth > -1 { 99 | rate = fmt.Sprintf(tcRate, cfg.TargetBandwidth) 100 | } else { 101 | rate = fmt.Sprintf(tcRate, 1000000) 102 | } 103 | 104 | strs := []string{tcAddClass, tar, "htb", rate} 105 | cmd := strings.Join(strs, " ") 106 | 107 | return c.execute(cmd) 108 | } 109 | 110 | func addNetemRule(cfg *Config, c commander) error { 111 | //Add the Network Emulator rule 112 | net := fmt.Sprintf(tcNetemRule, cfg.Device) 113 | strs := []string{tcAddQDisc, net, "netem"} 114 | 115 | if cfg.Latency > 0 { 116 | strs = append(strs, fmt.Sprintf(tcDelay, cfg.Latency)) 117 | } 118 | 119 | if cfg.TargetBandwidth > -1 { 120 | strs = append(strs, fmt.Sprintf(tcRate, cfg.TargetBandwidth)) 121 | } 122 | 123 | if cfg.PacketLoss > 0 { 124 | strs = append(strs, fmt.Sprintf(tcLoss, strconv.FormatFloat(cfg.PacketLoss, 'f', 2, 64))) 125 | } 126 | 127 | cmd := strings.Join(strs, " ") 128 | 129 | return c.execute(cmd) 130 | } 131 | 132 | func addIptablesRules(cfg *Config, c commander) error { 133 | var err error 134 | if len(cfg.TargetIps) == 0 && len(cfg.TargetIps6) == 0 { 135 | if err == nil { 136 | err = addIptablesRulesForAddrs(cfg, c, ip4Tables, cfg.TargetIps) 137 | } 138 | if err == nil { 139 | err = addIptablesRulesForAddrs(cfg, c, ip6Tables, cfg.TargetIps6) 140 | } 141 | return err 142 | } 143 | if err == nil && len(cfg.TargetIps) > 0 { 144 | err = addIptablesRulesForAddrs(cfg, c, ip4Tables, cfg.TargetIps) 145 | } 146 | if err == nil && len(cfg.TargetIps6) > 0 { 147 | err = addIptablesRulesForAddrs(cfg, c, ip6Tables, cfg.TargetIps6) 148 | } 149 | return err 150 | } 151 | 152 | func addIptablesRulesForAddrs(cfg *Config, c commander, command string, addrs []string) error { 153 | rules := []string{} 154 | ports := "" 155 | 156 | if len(cfg.TargetPorts) > 0 { 157 | if len(cfg.TargetPorts) > 1 { 158 | prts := strings.Join(cfg.TargetPorts, ",") 159 | ports = fmt.Sprintf(iptDestPorts, prts) 160 | } else { 161 | ports = fmt.Sprintf(iptDestPort, cfg.TargetPorts[0]) 162 | } 163 | } 164 | 165 | addTargetCmd := fmt.Sprintf(iptAddTarget, command) 166 | 167 | if len(cfg.TargetProtos) > 0 { 168 | for _, ptc := range cfg.TargetProtos { 169 | proto := fmt.Sprintf(iptProto, ptc) 170 | rule := addTargetCmd + " " + proto 171 | 172 | if ptc != "icmp" { 173 | if ports != "" { 174 | rule += " " + ports 175 | } 176 | } 177 | 178 | rules = append(rules, rule) 179 | } 180 | } else { 181 | rules = []string{addTargetCmd} 182 | } 183 | 184 | if len(addrs) > 0 { 185 | iprules := []string{} 186 | for _, ip := range addrs { 187 | dest := fmt.Sprintf(iptDestIP, ip) 188 | if len(rules) > 0 { 189 | for _, rule := range rules { 190 | r := rule + " " + dest 191 | iprules = append(iprules, r) 192 | } 193 | } else { 194 | iprules = append(iprules, dest) 195 | } 196 | } 197 | if len(iprules) > 0 { 198 | rules = iprules 199 | } 200 | } 201 | 202 | for _, rule := range rules { 203 | if err := c.execute(rule); err != nil { 204 | return err 205 | } 206 | } 207 | 208 | return nil 209 | } 210 | 211 | func (t *tcThrottler) teardown(cfg *Config) error { 212 | if err := delIptablesRules(cfg, t.c); err != nil { 213 | return err 214 | } 215 | 216 | // The root node to append the filters 217 | if err := delRootQDisc(cfg, t.c); err != nil { 218 | return err 219 | } 220 | return nil 221 | } 222 | 223 | func delIptablesRules(cfg *Config, c commander) error { 224 | iptablesCommands := []string{ip4Tables, ip6Tables} 225 | 226 | for _, iptablesCommand := range iptablesCommands { 227 | if !c.commandExists(iptablesCommand) { 228 | continue 229 | } 230 | lines, err := c.executeGetLines(fmt.Sprintf(iptList, iptablesCommand)) 231 | if err != nil { 232 | // ignore exit code 3 from iptables, which might happen if the system 233 | // has the ip6tables command, but no IPv6 capabilities 234 | werr, ok := err.(*exec.ExitError) 235 | if !ok { 236 | return err 237 | } 238 | status, ok := werr.Sys().(syscall.WaitStatus) 239 | if !ok { 240 | return err 241 | } 242 | if status.ExitStatus() == 3 { 243 | continue 244 | } 245 | return err 246 | } 247 | 248 | delCmdPrefix := fmt.Sprintf(iptDel, iptablesCommand) 249 | 250 | for _, line := range lines { 251 | if strings.Contains(line, iptDelSearch) { 252 | cmd := strings.Replace(line, "-A", delCmdPrefix, 1) 253 | err = c.execute(cmd) 254 | if err != nil { 255 | return err 256 | } 257 | } 258 | } 259 | } 260 | return nil 261 | } 262 | 263 | func delRootQDisc(cfg *Config, c commander) error { 264 | //Delete the root QDisc 265 | root := fmt.Sprintf(tcRootQDisc, cfg.Device) 266 | 267 | strs := []string{tcDelQDisc, root} 268 | cmd := strings.Join(strs, " ") 269 | 270 | return c.execute(cmd) 271 | } 272 | 273 | func (t *tcThrottler) exists() bool { 274 | if dry { 275 | return false 276 | } 277 | err := t.c.execute(tcExists) 278 | return err == nil 279 | } 280 | 281 | func (t *tcThrottler) check() string { 282 | return tcCheck 283 | } 284 | -------------------------------------------------------------------------------- /throttler/tc_test.go: -------------------------------------------------------------------------------- 1 | package throttler 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | type cmdRecorder struct { 8 | commands []string 9 | responses map[string][]string 10 | cmdBlackList []string 11 | } 12 | 13 | func newCmdRecorder() *cmdRecorder { 14 | return &cmdRecorder{[]string{}, map[string][]string{}, []string{}} 15 | } 16 | 17 | func (r *cmdRecorder) execute(cmd string) error { 18 | r.commands = append(r.commands, cmd) 19 | return nil 20 | } 21 | 22 | func (r *cmdRecorder) executeGetLines(cmd string) ([]string, error) { 23 | r.execute(cmd) 24 | if responses, found := r.responses[cmd]; found { 25 | return responses, nil 26 | } 27 | return []string{}, nil 28 | } 29 | 30 | func (r *cmdRecorder) commandExists(cmd string) bool { 31 | for _, blackListed := range r.cmdBlackList { 32 | if blackListed == cmd { 33 | return false 34 | } 35 | } 36 | return true 37 | } 38 | 39 | func (r *cmdRecorder) verifyCommands(t *testing.T, expected []string) { 40 | if len(expected) != len(r.commands) { 41 | for i, cmd := range expected { 42 | t.Logf("Expected (%d): %s", i, cmd) 43 | } 44 | for i, cmd := range r.commands { 45 | t.Logf("Actual (%d): %s", i, cmd) 46 | } 47 | 48 | t.Fatalf("Expected to see %d commands, got %d", len(expected), len(r.commands)) 49 | } 50 | 51 | for i, cmd := range expected { 52 | if actual := r.commands[i]; actual != cmd { 53 | t.Fatalf("Expected to see command `%s`, got `%s`", cmd, actual) 54 | } 55 | } 56 | } 57 | 58 | var defaultTestConfig = Config{ 59 | Device: "eth0", 60 | Stop: false, 61 | Latency: -1, 62 | TargetBandwidth: -1, 63 | DefaultBandwidth: 20000, 64 | PacketLoss: 0.1, 65 | TargetIps: []string{"10.10.10.10"}, 66 | TargetPorts: []string{"80"}, 67 | TargetProtos: []string{"tcp"}, 68 | DryRun: false, 69 | } 70 | 71 | func TestTcPacketLossSetup(t *testing.T) { 72 | r := newCmdRecorder() 73 | th := &tcThrottler{r} 74 | cfg := defaultTestConfig 75 | cfg.Device = "eth1" 76 | cfg.PacketLoss = 0.2 77 | th.setup(&cfg) 78 | r.verifyCommands(t, []string{ 79 | "sudo tc qdisc add dev eth1 handle 10: root htb default 1", 80 | "sudo tc class add dev eth1 parent 10: classid 10:1 htb rate 20000kbit", 81 | "sudo tc class add dev eth1 parent 10: classid 10:10 htb rate 1000000kbit", 82 | "sudo tc qdisc add dev eth1 parent 10:10 handle 100: netem loss 0.20%", 83 | "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 10.10.10.10", 84 | }) 85 | } 86 | 87 | func TestTcWildcardIps(t *testing.T) { 88 | r := newCmdRecorder() 89 | th := &tcThrottler{r} 90 | cfg := defaultTestConfig 91 | cfg.TargetIps = []string{} 92 | cfg.TargetPorts = []string{} 93 | cfg.TargetProtos = []string{} 94 | cfg.PacketLoss = -1 95 | th.setup(&cfg) 96 | r.verifyCommands(t, []string{ 97 | "sudo tc qdisc add dev eth0 handle 10: root htb default 1", 98 | "sudo tc class add dev eth0 parent 10: classid 10:1 htb rate 20000kbit", 99 | "sudo tc class add dev eth0 parent 10: classid 10:10 htb rate 1000000kbit", 100 | "sudo tc qdisc add dev eth0 parent 10:10 handle 100: netem", 101 | "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10", 102 | "sudo ip6tables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10", 103 | }) 104 | } 105 | 106 | func TestTcMultiplePortsAndIps(t *testing.T) { 107 | r := newCmdRecorder() 108 | th := &tcThrottler{r} 109 | cfg := defaultTestConfig 110 | cfg.TargetIps = []string{"1.1.1.1", "2.2.2.2"} 111 | cfg.TargetPorts = []string{"80", "8080"} 112 | cfg.TargetProtos = []string{"tcp", "udp"} 113 | th.setup(&cfg) 114 | r.verifyCommands(t, []string{ 115 | "sudo tc qdisc add dev eth0 handle 10: root htb default 1", 116 | "sudo tc class add dev eth0 parent 10: classid 10:1 htb rate 20000kbit", 117 | "sudo tc class add dev eth0 parent 10: classid 10:10 htb rate 1000000kbit", 118 | "sudo tc qdisc add dev eth0 parent 10:10 handle 100: netem loss 0.10%", 119 | "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --match multiport --dports 80,8080 -d 1.1.1.1", 120 | "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p udp --match multiport --dports 80,8080 -d 1.1.1.1", 121 | "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --match multiport --dports 80,8080 -d 2.2.2.2", 122 | "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p udp --match multiport --dports 80,8080 -d 2.2.2.2", 123 | }) 124 | } 125 | 126 | func TestTcMixedIPv6Setup(t *testing.T) { 127 | r := newCmdRecorder() 128 | th := &tcThrottler{r} 129 | cfg := defaultTestConfig 130 | cfg.Device = "eth1" 131 | cfg.PacketLoss = 0.2 132 | cfg.TargetIps6 = []string{"2001:db8::1"} 133 | th.setup(&cfg) 134 | r.verifyCommands(t, []string{ 135 | "sudo tc qdisc add dev eth1 handle 10: root htb default 1", 136 | "sudo tc class add dev eth1 parent 10: classid 10:1 htb rate 20000kbit", 137 | "sudo tc class add dev eth1 parent 10: classid 10:10 htb rate 1000000kbit", 138 | "sudo tc qdisc add dev eth1 parent 10:10 handle 100: netem loss 0.20%", 139 | "sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 10.10.10.10", 140 | "sudo ip6tables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 2001:db8::1", 141 | }) 142 | } 143 | 144 | func TestTcTeardown(t *testing.T) { 145 | r := newCmdRecorder() 146 | th := &tcThrottler{r} 147 | r.responses = map[string][]string{ 148 | "sudo iptables -S -t mangle": { 149 | "-P PREROUTING ACCEPT", 150 | "-P INPUT ACCEPT", 151 | "-P FORWARD ACCEPT", 152 | "-P OUTPUT ACCEPT", 153 | "-P POSTROUTING ACCEPT", 154 | "-A POSTROUTING -d 10.10.10.10 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", 155 | }, 156 | "sudo ip6tables -S -t mangle": { 157 | "-P PREROUTING ACCEPT", 158 | "-P INPUT ACCEPT", 159 | "-P FORWARD ACCEPT", 160 | "-P OUTPUT ACCEPT", 161 | "-P POSTROUTING ACCEPT", 162 | }, 163 | } 164 | th.teardown(&defaultTestConfig) 165 | r.verifyCommands(t, []string{ 166 | "sudo iptables -S -t mangle", 167 | "sudo iptables -t mangle -D POSTROUTING -d 10.10.10.10 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", 168 | "sudo ip6tables -S -t mangle", 169 | "sudo tc qdisc del dev eth0 handle 10: root", 170 | }) 171 | } 172 | 173 | func TestTcTeardownNoIpTables(t *testing.T) { 174 | r := newCmdRecorder() 175 | th := &tcThrottler{r} 176 | th.teardown(&defaultTestConfig) 177 | r.verifyCommands(t, []string{ 178 | "sudo iptables -S -t mangle", 179 | "sudo ip6tables -S -t mangle", 180 | "sudo tc qdisc del dev eth0 handle 10: root", 181 | }) 182 | } 183 | 184 | func TestTcIPv6Teardown(t *testing.T) { 185 | r := newCmdRecorder() 186 | th := &tcThrottler{r} 187 | r.responses = map[string][]string{ 188 | "sudo iptables -S -t mangle": {}, 189 | "sudo ip6tables -S -t mangle": { 190 | "-P PREROUTING ACCEPT", 191 | "-P INPUT ACCEPT", 192 | "-P FORWARD ACCEPT", 193 | "-P OUTPUT ACCEPT", 194 | "-P POSTROUTING ACCEPT", 195 | "-A POSTROUTING -d 2001:db8::1 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", 196 | }, 197 | } 198 | config := defaultTestConfig 199 | 200 | th.teardown(&config) 201 | r.verifyCommands(t, []string{ 202 | "sudo iptables -S -t mangle", 203 | "sudo ip6tables -S -t mangle", 204 | "sudo ip6tables -t mangle -D POSTROUTING -d 2001:db8::1 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", 205 | "sudo tc qdisc del dev eth0 handle 10: root", 206 | }) 207 | } 208 | 209 | func TestTcTeardownNoIPv6(t *testing.T) { 210 | r := newCmdRecorder() 211 | r.cmdBlackList = []string{"ip6tables"} 212 | th := &tcThrottler{r} 213 | r.responses = map[string][]string{ 214 | "sudo iptables -S -t mangle": { 215 | "-P PREROUTING ACCEPT", 216 | "-P INPUT ACCEPT", 217 | "-P FORWARD ACCEPT", 218 | "-P OUTPUT ACCEPT", 219 | "-P POSTROUTING ACCEPT", 220 | "-A POSTROUTING -d 10.10.10.10 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", 221 | }, 222 | } 223 | 224 | th.teardown(&defaultTestConfig) 225 | r.verifyCommands(t, []string{ 226 | "sudo iptables -S -t mangle", 227 | "sudo iptables -t mangle -D POSTROUTING -d 10.10.10.10 -p tcp -m tcp --dport 80 -j CLASSIFY --set-class 0010:0010", 228 | "sudo tc qdisc del dev eth0 handle 10: root", 229 | }) 230 | } 231 | -------------------------------------------------------------------------------- /throttler/throttler.go: -------------------------------------------------------------------------------- 1 | package throttler 2 | 3 | import ( 4 | "bufio" 5 | "errors" 6 | "fmt" 7 | "os" 8 | "os/exec" 9 | "runtime" 10 | ) 11 | 12 | const ( 13 | linux = "linux" 14 | darwin = "darwin" 15 | freebsd = "freebsd" 16 | windows = "windows" 17 | checkOSXVersion = "sw_vers -productVersion" 18 | ipfw = "ipfw" 19 | pfctl = "pfctl" 20 | ) 21 | 22 | // Config specifies options for configuring packet filter rules. 23 | type Config struct { 24 | Device string 25 | Stop bool 26 | Latency int 27 | TargetBandwidth int 28 | DefaultBandwidth int 29 | PacketLoss float64 30 | TargetIps []string 31 | TargetIps6 []string 32 | TargetPorts []string 33 | TargetProtos []string 34 | DryRun bool 35 | } 36 | 37 | type throttler interface { 38 | setup(*Config) error 39 | teardown(*Config) error 40 | exists() bool 41 | check() string 42 | } 43 | 44 | type commander interface { 45 | execute(string) error 46 | executeGetLines(string) ([]string, error) 47 | commandExists(string) bool 48 | } 49 | 50 | type dryRunCommander struct{} 51 | 52 | type shellCommander struct{} 53 | 54 | var dry bool 55 | 56 | func setup(t throttler, cfg *Config) { 57 | if t.exists() { 58 | fmt.Println("It looks like the packet rules are already setup") 59 | os.Exit(1) 60 | } 61 | 62 | if err := t.setup(cfg); err != nil { 63 | fmt.Println("I couldn't setup the packet rules:", err.Error()) 64 | os.Exit(1) 65 | } 66 | 67 | fmt.Println("Packet rules setup...") 68 | fmt.Printf("Run `%s` to double check\n", t.check()) 69 | fmt.Printf("Run `%s --device %s --stop` to reset\n", os.Args[0], cfg.Device) 70 | } 71 | 72 | func teardown(t throttler, cfg *Config) { 73 | if !t.exists() { 74 | fmt.Println("It looks like the packet rules aren't setup") 75 | os.Exit(1) 76 | } 77 | 78 | if err := t.teardown(cfg); err != nil { 79 | fmt.Println("Failed to stop packet controls") 80 | os.Exit(1) 81 | } 82 | 83 | fmt.Println("Packet rules stopped...") 84 | fmt.Printf("Run `%s` to double check\n", t.check()) 85 | fmt.Printf("Run `%s` to start\n", os.Args[0]) 86 | } 87 | 88 | // Run executes the packet filter operation, either setting it up or tearing 89 | // it down. 90 | func Run(cfg *Config) { 91 | dry = cfg.DryRun 92 | var t throttler 93 | var c commander 94 | 95 | if cfg.DryRun { 96 | c = &dryRunCommander{} 97 | } else { 98 | c = &shellCommander{} 99 | } 100 | 101 | switch runtime.GOOS { 102 | case freebsd: 103 | if cfg.Device == "" { 104 | fmt.Println("Device not specified, unable to default to eth0 on FreeBSD.") 105 | os.Exit(1) 106 | } 107 | 108 | t = &ipfwThrottler{c} 109 | case darwin: 110 | // Avoid OS version pinning and choose based on what's available 111 | if c.commandExists(pfctl) { 112 | t = &pfctlThrottler{c} 113 | } else if c.commandExists(ipfw) { 114 | t = &ipfwThrottler{c} 115 | } else { 116 | fmt.Println("Could not determine an appropriate firewall tool for OSX (tried pfctl, ipfw), exiting") 117 | os.Exit(1) 118 | } 119 | 120 | if cfg.Device == "" { 121 | cfg.Device = "eth0" 122 | } 123 | 124 | case linux: 125 | if cfg.Device == "" { 126 | cfg.Device = "eth0" 127 | } 128 | 129 | t = &tcThrottler{c} 130 | default: 131 | fmt.Printf("I don't support your OS: %s\n", runtime.GOOS) 132 | os.Exit(1) 133 | } 134 | 135 | if !cfg.Stop { 136 | setup(t, cfg) 137 | } else { 138 | teardown(t, cfg) 139 | } 140 | } 141 | 142 | func (c *dryRunCommander) execute(cmd string) error { 143 | fmt.Println(cmd) 144 | return nil 145 | } 146 | 147 | func (c *dryRunCommander) executeGetLines(cmd string) ([]string, error) { 148 | fmt.Println(cmd) 149 | return []string{}, nil 150 | } 151 | 152 | func (c *dryRunCommander) commandExists(cmd string) bool { 153 | return true 154 | } 155 | 156 | func (c *shellCommander) execute(cmd string) error { 157 | fmt.Println(cmd) 158 | return exec.Command("/bin/sh", "-c", cmd).Run() 159 | } 160 | 161 | func (c *shellCommander) executeGetLines(cmd string) ([]string, error) { 162 | lines := []string{} 163 | child := exec.Command("/bin/sh", "-c", cmd) 164 | 165 | out, err := child.StdoutPipe() 166 | if err != nil { 167 | return []string{}, err 168 | } 169 | 170 | err = child.Start() 171 | if err != nil { 172 | return []string{}, err 173 | } 174 | 175 | scanner := bufio.NewScanner(out) 176 | 177 | for scanner.Scan() { 178 | lines = append(lines, scanner.Text()) 179 | } 180 | if err := scanner.Err(); err != nil { 181 | return []string{}, errors.New(fmt.Sprint("Error reading standard input:", err)) 182 | } 183 | 184 | err = child.Wait() 185 | if err != nil { 186 | return []string{}, err 187 | } 188 | 189 | return lines, nil 190 | } 191 | 192 | func (c *shellCommander) commandExists(cmd string) bool { 193 | _, err := exec.LookPath(cmd) 194 | return err == nil 195 | } 196 | --------------------------------------------------------------------------------