Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Traffic control library for constraining the network configuration on a port. | 5 """Traffic control library for constraining the network configuration on a port. |
| 6 | 6 |
| 7 The traffic controller sets up a constrained network configuration on a port. | 7 The traffic controller sets up a constrained network configuration on a port. |
| 8 Traffic to the constrained port is forwarded to a specified server port. | 8 Traffic to the constrained port is forwarded to a specified server port. |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 import logging | 11 import logging |
| 12 import os | |
| 12 import re | 13 import re |
| 13 import subprocess | 14 import subprocess |
| 14 | 15 |
| 15 # The maximum bandwidth limit. | 16 # The maximum bandwidth limit. |
| 16 _DEFAULT_MAX_BANDWIDTH_KBIT = 1000000 | 17 _DEFAULT_MAX_BANDWIDTH_KBIT = 1000000 |
| 17 | 18 |
| 18 | 19 |
| 19 class TrafficControlError(BaseException): | 20 class TrafficControlError(BaseException): |
| 20 """Exception raised for errors in traffic control library. | 21 """Exception raised for errors in traffic control library. |
| 21 | 22 |
| 22 Attributes: | 23 Attributes: |
| 23 msg: User defined error message. | 24 msg: User defined error message. |
| 24 cmd: Command for which the exception was raised. | 25 cmd: Command for which the exception was raised. |
| 25 returncode: Return code of running the command. | 26 returncode: Return code of running the command. |
| 26 stdout: Output of running the command. | 27 stdout: Output of running the command. |
| 27 stderr: Error output of running the command. | 28 stderr: Error output of running the command. |
| 28 """ | 29 """ |
| 29 | 30 |
| 30 def __init__(self, msg, cmd=None, returncode=None, output=None, | 31 def __init__(self, msg, cmd=None, returncode=None, output=None, |
| 31 error=None): | 32 error=None): |
| 32 BaseException.__init__(self, msg) | 33 BaseException.__init__(self, msg) |
| 33 self.msg = msg | 34 self.msg = msg |
| 34 self.cmd = cmd | 35 self.cmd = cmd |
| 35 self.returncode = returncode | 36 self.returncode = returncode |
| 36 self.output = output | 37 self.output = output |
| 37 self.error = error | 38 self.error = error |
| 38 | 39 |
| 39 | 40 |
| 41 def CheckRequirements(): | |
| 42 """Checks if permissions are available to run traffic control commands. | |
| 43 | |
| 44 Raises: | |
| 45 TrafficControlError: If permissions to run traffic control commands are not | |
| 46 available. | |
| 47 """ | |
| 48 if not os.geteuid() == 0: | |
|
DaleCurtis
2012/01/10 00:17:59
Use != 0.
shadi
2012/01/10 00:36:39
I thought (from previous CL) 'not' is preferred ov
DaleCurtis
2012/01/10 00:40:01
For objects and boolean statements, but usually no
| |
| 49 _Exec(['sudo', '-n', 'tc'], msg=('Cannot run traffic control commands. ' | |
|
DaleCurtis
2012/01/10 00:17:59
Add -help just to be sure.
Where is the iptables
shadi
2012/01/10 00:36:39
My bad. I thought the purpose was to check if sudo
| |
| 50 'Please check that you have root access.')) | |
| 51 | |
| 52 | |
| 40 def CreateConstrainedPort(config): | 53 def CreateConstrainedPort(config): |
| 41 """Creates a new constrained port. | 54 """Creates a new constrained port. |
| 42 | 55 |
| 43 Imposes packet level constraints such as bandwidth, latency, and packet loss | 56 Imposes packet level constraints such as bandwidth, latency, and packet loss |
| 44 on a given port using the specified configuration dictionary. Traffic to that | 57 on a given port using the specified configuration dictionary. Traffic to that |
| 45 port is forwarded to a specified server port. | 58 port is forwarded to a specified server port. |
| 46 | 59 |
| 47 Args: | 60 Args: |
| 48 config: Constraint configuration dictionary, format: | 61 config: Constraint configuration dictionary, format: |
| 49 port: Port to constrain (integer 1-65535). | 62 port: Port to constrain (integer 1-65535). |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 """ | 341 """ |
| 329 cmd_list = [str(x) for x in command] | 342 cmd_list = [str(x) for x in command] |
| 330 cmd = ' '.join(cmd_list) | 343 cmd = ' '.join(cmd_list) |
| 331 logging.debug('Running command: %s', cmd) | 344 logging.debug('Running command: %s', cmd) |
| 332 | 345 |
| 333 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 346 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 334 output, error = p.communicate() | 347 output, error = p.communicate() |
| 335 if p.returncode != 0: | 348 if p.returncode != 0: |
| 336 raise TrafficControlError(msg, cmd, p.returncode, output, error) | 349 raise TrafficControlError(msg, cmd, p.returncode, output, error) |
| 337 return output.strip() | 350 return output.strip() |
| OLD | NEW |