Hyunsun Moon | 6ff622e | 2020-03-11 17:14:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
Hyunsun Moon | 6ff622e | 2020-03-11 17:14:48 -0700 | [diff] [blame] | 3 | # Copyright(c) 2019 Intel Corporation |
| 4 | |
| 5 | import os |
| 6 | import signal |
| 7 | import socket |
| 8 | import sys |
| 9 | |
| 10 | import iptools |
| 11 | import json |
| 12 | import psutil |
| 13 | from pyroute2 import IPDB |
| 14 | |
| 15 | |
| 16 | def exit(code, msg): |
| 17 | print(msg) |
| 18 | sys.exit(code) |
| 19 | |
| 20 | |
| 21 | def getpid(process_name): |
| 22 | for proc in psutil.process_iter(attrs=['pid', 'name']): |
| 23 | if process_name == proc.info['name']: |
| 24 | return proc.info['pid'] |
| 25 | |
| 26 | |
| 27 | def getpythonpid(process_name): |
| 28 | for proc in psutil.process_iter(attrs=['pid', 'cmdline']): |
| 29 | if len(proc.info['cmdline']) < 2: |
| 30 | continue |
| 31 | if process_name in proc.info['cmdline'][1] and 'python' in proc.info['cmdline'][0]: |
| 32 | return proc.info['pid'] |
| 33 | return |
| 34 | |
Hyunsun Moon | 6ff622e | 2020-03-11 17:14:48 -0700 | [diff] [blame] | 35 | def get_json_conf(path, dump): |
| 36 | conf = json.loads(open(path).read()) |
| 37 | if dump: |
| 38 | print(json.dumps(conf, indent=4, sort_keys=True)) |
| 39 | return conf |
| 40 | |
| 41 | |
| 42 | def get_env(varname, default=None): |
| 43 | try: |
| 44 | var = os.environ[varname] |
| 45 | return var |
| 46 | except KeyError: |
| 47 | if default is not None: |
| 48 | return '{}'.format(default) |
| 49 | else: |
| 50 | exit(1, 'Empty env var {}'.format(varname)) |
| 51 | |
| 52 | |
| 53 | def ips_by_interface(name): |
| 54 | ipdb = IPDB() |
| 55 | return [ipobj[0] for ipobj in ipdb.interfaces[name]['ipaddr'].ipv4] |
| 56 | |
| 57 | |
| 58 | def mac_by_interface(name): |
| 59 | ipdb = IPDB() |
| 60 | return ipdb.interfaces[name]['address'] |
| 61 | |
| 62 | |
| 63 | def mac2hex(mac): |
| 64 | return int(mac.replace(':', ''), 16) |
| 65 | |
| 66 | |
| 67 | def peer_by_interface(name): |
| 68 | ipdb = IPDB() |
| 69 | try: |
| 70 | peer_idx = ipdb.interfaces[name]['link'] |
| 71 | peer_name = ipdb.interfaces[peer_idx]['ifname'] |
| 72 | except: |
| 73 | raise Exception('veth interface {} does not exist'.format(name)) |
| 74 | else: |
| 75 | return peer_name |
| 76 | |
| 77 | |
| 78 | def aton(ip): |
| 79 | return socket.inet_aton(ip) |
| 80 | |
| 81 | |
| 82 | def validate_cidr(cidr): |
| 83 | return iptools.ipv4.validate_cidr(cidr) |
| 84 | |
| 85 | |
| 86 | def cidr2mask(cidr): |
| 87 | _, prefix = cidr.split('/') |
| 88 | return format(0xffffffff << (32 - int(prefix)), '08x') |
| 89 | |
| 90 | |
| 91 | def cidr2block(cidr): |
| 92 | return iptools.ipv4.cidr2block(cidr) |
| 93 | |
| 94 | |
| 95 | def ip2hex(ip): |
| 96 | return iptools.ipv4.ip2hex(ip) |
| 97 | |
| 98 | |
| 99 | def ip2long(ip): |
| 100 | return iptools.ipv4.ip2long(ip) |
| 101 | |
| 102 | |
| 103 | def get_process_affinity(): |
| 104 | return psutil.Process().cpu_affinity() |