def forward_a(testname): hs = TESTS[testname].host_s hr = TESTS[testname].host_r r_mac = info.get("router_mac", hs) target_ip = info.get("host_ip", hr) return [Ether(dst=r_mac) / IP(dst=target_ip)]
def run_test(self, testname): log = os.path.join(info.LOGDIR, testname) Path(log).mkdir(parents=True, exist_ok=True) test = tests.TESTS[testname] for hp in range(len(self.hosts)): lout = os.path.join(log, info.get("output_file", hp)) lerr = os.path.join(log, info.get("error_file", hp)) cmd = "checker/checker.py \ --passive \ --testname={} \ --host={} \ > {} \ 2> {} &".format(testname, hp, lout, lerr) self.hosts[hp].cmd(cmd) time.sleep(info.TIMEOUT / 2) cmd = "checker/checker.py \ --active \ --testname={} \ --host={} &".format(testname, test.host_s) self.hosts[test.host_s].cmd(cmd) results = {} time.sleep(info.TIMEOUT) for hp in range(len(self.hosts)): lout = os.path.join(log, info.get("output_file", hp)) with open(lout, "r") as fin: results[hp] = fin.read().strip("\r\n") return results
def setup_ifaces(self): for i in range(len(self.hosts)): host_ip = info.get("host_ip", i) router_ip = info.get("router_ip", i) self.router.setIP(router_ip, prefixLen=24, intf="r-{}".format(i)) self.hosts[i].setIP(host_ip, prefixLen=24, intf="h-{}".format(i))
def disable_unneeded(self): def disable_ipv6(host): host.cmd('sysctl -w net.ipv6.conf.all.disable_ipv6=1') host.cmd('sysctl -w net.ipv6.conf.default.disable_ipv6=1') def disable_nic_checksum(host, iface): host.cmd('ethtool iface {} --offload rx off tx off'.format(iface)) host.cmd('ethtool -K {} tx-checksum-ip-generic off'.format(iface)) def disable_arp(host, iface): host.cmd("ip link set dev {} arp off".format(iface)) for i, (router, hosts) in enumerate(self.routers): disable_ipv6(router) for j, host in enumerate(hosts): disable_ipv6(host) hidx = i * len(hosts) + j h_if = info.get("host_if_name", hidx) disable_nic_checksum(host, h_if) # we want complete control over these actions router.cmd('echo "0" > /proc/sys/net/ipv4/ip_forward') router.cmd('echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all') if not static_arp(): for (i, (router, hosts)) in enumerate(self.routers): for j in range(len(hosts)): hidx = i * len(hosts) + j ifn = info.get("router_if_name", hidx) disable_arp(router, ifn)
def icmp_timeout_a(testname): hs = TESTS[testname].host_s hr = TESTS[testname].host_r r_mac = info.get("router_mac", hs) target_ip = info.get("host_ip", hr) return [Ether(dst=r_mac) / IP(dst=target_ip, ttl=1)]
def run_test(self, testname): test = tests.TESTS[testname] for hp in test.hosts_p: h = self.net.get(info.get("host_name", hp)) cmd = "python ./checker.py \ --passive \ --testname={} \ --host={}".format(testname, hp) self.hosts[hp].sendCmd(cmd) for ha in test.hosts_a: cmd = "python ./checker.py \ --active \ --testname={} \ --host={} &".format(testname, ha) self.hosts[ha].cmd(cmd) results = {} passed = True # XXX will this loop necessarily end? for hp in test.hosts_p: h = self.net.get(info.get("host_name", hp)) # Somehow, somewhere, a newline is converted to a CRLF... results[h.name] = h.waitOutput().strip("\r\n") passed = passed and (results[h.name] == "PASS") return passed
def router_arp_reply_a(testname): """Test the router responds to an ARP request""" hs = TESTS[testname].host_s r_ip = info.get("router_ip", hs) s_mac = info.get("host_mac", hs) s_ip = info.get("host_ip", hs) return [Ether(src=s_mac, dst=ETHER_BROADCAST) / ARP(psrc=s_ip, pdst=r_ip)]
def build(self, n=2): switch = self.addHost('router') # Python's range(N) generates 0..N-1 for h in range(n): host = self.addHost(info.get("host_name", h)) i1 = info.get("host_if_name", h) i2 = info.get("router_if_name", h) self.addLink(host, switch, intfName1=i1, intfName2=i2)
def router_icmp_a(testname): hs = TESTS[testname].host_s router = TESTS[testname].router r_mac = info.get("router_mac", router, hs) r_ip = info.get("router_ip", hs) s_mac = info.get("host_mac", hs) s_ip = info.get("host_ip", hs) return [Ether(src=s_mac, dst=r_mac) / IP(src=s_ip, dst=r_ip) / ICMP()]
def setup_macs(self): for i, host in enumerate(self.hosts): h_mac = info.get("host_mac", i) h_if = info.get("host_if_name", i) host.cmd("ifconfig {} hw ether {}".format(h_if, h_mac)) r_mac = info.get("router_mac", i) r_if = info.get("router_if_name", i) self.router.cmd("ifconfig {} hw ether {}".format(r_if, r_mac))
def forward_no_arp_a(testname): """Send two packets; there should be at most just one! ARP request""" hs = TESTS[testname].host_s hr = TESTS[testname].host_r r_mac = info.get("router_mac", hs) target_ip = info.get("host_ip", hr) packet = Ether(dst=r_mac) / IP(dst=target_ip) return [packet, packet]
def host_unreachable_a(testname): target_ip = "10.0.0.1" hs = TESTS[testname].host_s router = TESTS[testname].router s_mac = info.get("host_mac", hs) s_ip = info.get("host_ip", hs) r_mac = info.get("router_mac", hs, router) return [Ether(src=s_mac, dst=r_mac) / IP(src=s_ip, dst=target_ip)]
def icmp_timeout_a(testname): hr = TESTS[testname].host_r hs = TESTS[testname].host_s router = TESTS[testname].router target_ip = info.get("host_ip", hr) s_mac = info.get("host_mac", hs) s_ip = info.get("host_ip", hs) r_mac = info.get("router_mac", hs, router) return [Ether(src=s_mac, dst=r_mac) / IP(src=s_ip, dst=target_ip, ttl=1)]
def forward_a(testname): hs = TESTS[testname].host_s hr = TESTS[testname].host_r router = TESTS[testname].router r_mac = info.get("router_mac", hs, router) s_mac = info.get("host_mac", hs) s_ip = info.get("host_ip", hs) target_ip = info.get("host_ip", hr) return [Ether(src=s_mac, dst=r_mac) / IP(src=s_ip, dst=target_ip)]
def wrong_checksum_a(testname): hs = TESTS[testname].host_s hr = TESTS[testname].host_r r_mac = info.get("router_mac", hs) target_ip = info.get("host_ip", hr) i = IP(dst=target_ip) chk = checksum(bytes(i)) chk = (chk + 1) % (2**16) i.chksum = chk return Ether(dst=r_mac) / i
def validate_all_from_host_or_replies(host, packets): """True if all packets are sent from host (an eventual replies)""" for ps, pr in zip(packets[::2], packets[1::2]): if Ether not in ps or Ether not in pr: return False if ps[Ether].src != info.get("host_mac", host): return False if pr[Ether].dst != info.get("host_mac", host): return False return True
def __init__(self, net, n_routers, n_hosts): self.net = net self.hosts = [] self.routers = [] self.n_hosts = n_hosts for i in range(n_routers): r = self.net.get(info.get("router_name", i)) hosts = [] for j in range(n_hosts): hidx = i * n_hosts + j h = self.net.get(info.get("host_name", hidx)) hosts.append(h) self.hosts.append(h) self.routers.append((r, hosts))
def __init__(self, net, n_hosts): self.net = net self.router = self.net.get("router") self.hosts = [] for i in range(n_hosts): h = self.net.get(info.get("host_name", i)) self.hosts.append(h)
def add_hosts_entries(self): for i, (router, hosts) in enumerate(self.routers): for j, host in enumerate(hosts): for h in range(len(self.hosts)): ip = info.get("host_ip", h) # FIXME entries should be added only if they aren't there. host.cmd("echo '{} h{}' >> /etc/hosts".format(ip, h))
def validate_all_from_host(host, packets): """True if all packets are sent from host (an eventual replies)""" for packet in packets: if Ether not in packet: return False if packet[Ether].src != info.get("host_mac", host): return False return True
def router_arp_request_p(testname, packets): # at this point we're not interested in whether the router actually # delivered the packet, just the arp request. hr = TESTS[testname].host_r assert len(packets) >= 1, "No packet received!" assert ARP in packets[0], "No ARP request!" a = packets[0][ARP] if a.get_field("op").i2repr(a, a.op) != "who-has": error("Wrong ARP type") if a.pdst != info.get("host_ip", hr): error("Wrong ARP address request") error("Expected {}".format(info.get("host_ip", hr))) error("Got {}".format(a.pdst)) return False return True
def start_routers(self): ifaces = "" for i in range(len(self.routers)): for j in range(i + 1, len(self.routers)): ifaces += "{} ".format(info.get("r2r_if_name", i, j)) for i in range(self.n_hosts): ifaces += "{} ".format(info.get("router_if_name", i)) for i, (router, _) in enumerate(self.routers): out = info.get("out_file", i) err = info.get("err_file", i) rtable = info.get("rtable", i) rname = "router{}".format(i) router.cmd("ln -s router {}".format(rname)) if not len(router.cmd("pgrep {}".format(rname))): cmd = "./{} {} {} > {} 2> {} &".format(rname, rtable, ifaces, out, err) router.cmd(cmd) time.sleep(info.TIMEOUT / 2)
def build(self, nr=2, nh=2): routers = [] for i in range(nr): routers.append(self.addHost(info.get("router_name", i))) for i in range(nr): for j in range(i + 1, nr): ifn = info.get("r2r_if_name", i, j) self.addLink(routers[i], routers[j], intfName1=ifn, intfName2=ifn) for i in range(nr): for j in range(nh): hidx = i * nh + j host = self.addHost(info.get("host_name", hidx)) i1 = info.get("host_if_name", hidx) i2 = info.get("router_if_name", j) self.addLink(host, routers[i], intfName1=i1, intfName2=i2)
def router_arp_reply_p(testname, packets): hr = TESTS[testname].host_r if len(packets) < 2: error("No reply received") return False assert Ether in packets[1], "Packet not of Ethernet type" e = packets[1][Ether] if e.src != info.get("router_mac", hr): error("Wrong source address") error("Expected: {}".format(info.get("router_mac", hr))) error("Got: {}".format(e.src)) return False if e.dst != info.get("host_mac", hr): error("Wrong destination address") error("Expected: {}".format(info.get("host_mac", hr))) error("Got: {}".format(e.dst)) return False assert ARP in packets[0], "Packet not of ARP type" a = packets[1][ARP] if a.get_field("op").i2repr(a, a.op) != "is-at": error("Wrong ARP type") return False if a[ARP].hwdst != info.get("host_mac", hr): error("Wrong destination in ARP reply") error("Expected: {}".format(info.get("host_mac", hr))) error("Got: {}".format(a[ARP].hwdst)) return False return True
def setup_ifaces(self): for i, (router, hosts) in enumerate(self.routers): for j, host in enumerate(hosts): hidx = i * len(hosts) + j host_ip = info.get("host_ip", hidx) router_ip = info.get("router_ip", hidx) host_if = info.get("host_if_name", hidx) router_if = info.get("router_if_name", j) router.setIP(router_ip, prefixLen=24, intf=router_if) host.setIP(host_ip, prefixLen=24, intf=host_if) nr = len(self.routers) for i in range(nr): for j in range(i + 1, nr): ri_if = info.get("r2r_if_name", i, j) rj_if = info.get("r2r_if_name", i, j) ri_ip = info.get("r2r_ip1", i, j) rj_ip = info.get("r2r_ip2", i, j) self.routers[i][0].setIP(ri_ip, prefixLen=24, intf=ri_if) self.routers[j][0].setIP(rj_ip, prefixLen=24, intf=rj_if)
def disable_unneeded(self): def disable_ipv6(host): host.cmd('sysctl -w net.ipv6.conf.all.disable_ipv6=1') host.cmd('sysctl -w net.ipv6.conf.default.disable_ipv6=1') def disable_nic_checksum(host, iface): host.cmd('ethtool iface {} --offload rx off tx off'.format(iface)) host.cmd('ethtool -K {} tx-checksum-ip-generic off'.format(iface)) def disable_arp(host, iface): host.cmd("ip link set dev {} arp off".format(iface)) disable_ipv6(self.router) for i, host in enumerate(self.hosts): disable_ipv6(host) h_if = info.get("host_if_name", i) disable_nic_checksum(host, h_if) # we want complete control over these actions self.router.cmd('echo "0" > /proc/sys/net/ipv4/ip_forward') self.router.cmd('echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all')
def setup_macs(self): for i, (router, hosts) in enumerate(self.routers): for j, host in enumerate(hosts): hidx = i * len(hosts) + j h_mac = info.get("host_mac", hidx) h_if = info.get("host_if_name", hidx) host.cmd("ifconfig {} hw ether {}".format(h_if, h_mac)) r_mac = info.get("router_mac", hidx, i) r_if = info.get("router_if_name", j) router.cmd("ifconfig {} hw ether {}".format(r_if, r_mac)) nr = len(self.routers) for i in range(nr): for j in range(i + 1, nr): ri_mac = info.get("r2r_mac", i, j) rj_mac = info.get("r2r_mac", j, i) ri_if = info.get("r2r_if_name", i, j) rj_if = info.get("r2r_if_name", i, j) self.routers[i][0].cmd("ifconfig {} hw ether {}".format( ri_if, ri_mac)) self.routers[j][0].cmd("ifconfig {} hw ether {}".format( rj_if, rj_mac))
def passive(host, testname): iface = info.get("host_if_name", host) packets = capture(iface) test = tests.TESTS[testname] if host == test.host_r: fn = test.passive_fn elif host == test.host_s: fn = tests.sender_default else: fn = tests.check_nothing try: status = fn(testname, packets) except AssertionError as e: traceback.print_tb(e.__traceback__) status = False if (status): print("PASS") else: print("FAIL")
def add_default_routes(self): for i, host in enumerate(self.hosts): ip = info.get("router_ip", i) host.cmd("ip route add default via {}".format(ip))
def get_info(pe, filename): show_info = json.loads(info.get(pe, filename)) return show_info
def fromfile(cls, filename, mode='rb', endian='>', skip_values=False): """\ Creates a binary punch file from a given filename. Arguments: - filename - mode (default 'rb') - endian (default '>') - skip values: If True only datablock headers will be read into memory and the values will read on request. Default: False. """ cls = cls() cls.name = filename cls.mode = mode cls.datablocks = list() # read file header cls.size = os.path.getsize(filename) cls.endian = endian cls._file = uff.FortranFile(filename, mode, endian) cls.filetype = cls._file.readline().strip() cls.title = cls._file.readline().strip() while cls._file.tell() < cls.size: # read first and second header line line = cls._file.readline('20sffii') modelname, res0, res1, halfpolar, center180 = line line = cls._file.readline('40si40sdd40s7i') category, number, unit, tau0, tau1, reserved = line[:6] dim0, dim1, dim2, dim3, dim4, dim5, skip = line[6:] # skip datablock (read datablock if accessing value) position = cls._file.tell() if skip_values: cls._file.skipline() values = None else: values = numpy.array(cls._file.readline('*f')) values = values.reshape((dim0,dim1,dim2), order='F') # get addtional information offset = CATEGORY2OFFSET.get(category.strip(), None) if offset is None: info = {} else: info = TRACER2INFO.get(number+offset, {}) # create DataBlock datablock = DataBlock( category = category.strip(), center180 = bool(center180), halfpolar = bool(halfpolar), modelname = modelname.strip(), number = number, origin = (dim3, dim4, dim5), resolution = (res0, res1), shape = (dim0, dim1, dim2), times = (misc.tau2time(tau0), misc.tau2time(tau1)), unit = unit.strip(), name = info.get('name', None), fullname = info.get('full_name', None), carbon_weight = info.get('c_weight', None), molecular_weight = info.get('molecular_weight', None), position = position, values = values, file = cls._file ) cls.append(datablock) return cls
def add_hosts_entries(self): for host in self.hosts: for j in range(len(self.hosts)): ip = info.get("host_ip", j) host.cmd("echo '{} h{}' >> /etc/hosts".format(ip, j))
def get_info(pe, filename): show_info = json.loads(info.get(pe, filename)) return json.dumps({"Short Info": show_info}, indent=4, separators=(',', ': '))
#!/usr/bin/python2 import yaml import info import cgi import time print "Status: 302 Found\r\nLocation: /\r\n\r\n" accounts = yaml.load(open('accounts.yaml').read()) form = cgi.FieldStorage() for account in accounts: ainfo = info.get(account['num']) ainfo.update({ 'used': int(time.time()) if (form.getvalue("u%s" % account['num'], 'off') == 'on') else False }) info.set(account['num'], ainfo)