from state import flow_emap EXP_TIME = 10 * 1000 EXT_DEVICE = 1 if a_packet_received: flow_emap.expire_all(now - EXP_TIME) h3 = pop_header(tcpudp, on_mismatch=([], [])) h2 = pop_header(ipv4, on_mismatch=([], [])) h1 = pop_header(ether, on_mismatch=([], [])) internal_flow = FlowIdc(h3.dst_port, h3.src_port, h2.daddr, h2.saddr, h2.npid) if received_on_port == EXT_DEVICE and not flow_emap.has(internal_flow): return ([], []) else: pass
from state import flow_emap, flow_id_to_backend_id, backends, backend_ip_emap, cht EXP_TIME = 10 * 1000 BACKEND_EXP_TIME = 3600000000 * 1000 EXT_PORT = 2 if a_packet_received: flow_emap.expire_all(now - EXP_TIME) backend_ip_emap.expire_all(now - BACKEND_EXP_TIME) h3 = pop_header(tcpudp, on_mismatch=([], [])) h2 = pop_header(ipv4, on_mismatch=([], [])) h1 = pop_header(ether, on_mismatch=([], [])) packet_flow = LoadBalancedFlowc(h2.saddr, h2.daddr, h3.src_port, h3.dst_port, h2.npid) if received_on_port == EXT_PORT and flow_emap.has(packet_flow): backend_id = flow_id_to_backend_id.get(flow_emap.get(packet_flow)) if not backend_ip_emap.has_idx(backend_id): flow_emap.erase(packet_flow) if backend_ip_emap.exists_with_cht( cht, _LoadBalancedFlow_hash(packet_flow)): bknd = backend_ip_emap.choose_with_cht( cht, _LoadBalancedFlow_hash(packet_flow)) idx = the_index_allocated flow_emap.add(packet_flow, idx, now) flow_id_to_backend_id.set(idx, bknd) else: pass else: pass else:
LAN_DEVICE = 1 WAN_DEVICE = 0 BURST = 3750000000 RATE = 375000000 EXP_TIME = 10 * 1000 * 1000 * 1000 h2 = pop_header(ipv4, on_mismatch=([], [])) # Malformed IPv4 if (h2.vihl & 15) < 5 or packet_size - 14 < (((h2.len & 0xFF) << 8) | ((h2.len & 0xFF00) >> 8)): return ([], []) h1 = pop_header(ether, on_mismatch=([], [])) flow_emap.expire_all(now - EXP_TIME) if received_on_port == WAN_DEVICE and flow_emap.has(ip_addrc(h2.daddr)): flow_idx = flow_emap.get(ip_addrc(h2.daddr)) flow_emap.refresh_idx(flow_idx, now) flow = dyn_vals.get(flow_idx) bucket_size = flow.bucket_size + (now - flow.bucket_time) * RATE if BURST < bucket_size: bucket_size = BURST if packet_size < bucket_size: bucket_size = bucket_size - packet_size dyn_vals.set(flow_idx, DynamicValuec(bucket_size, now)) return ([LAN_DEVICE], [ether(h1), ipv4(h2)]) else: pass else: pass
from state import flow_emap, backends, backend_ip_emap, cht EXP_TIME = 10 * 1000 BACKEND_EXP_TIME = 3600000000 * 1000 EXT_PORT = 2 if a_packet_received: flow_emap.expire_all(now - EXP_TIME) backend_ip_emap.expire_all(now - BACKEND_EXP_TIME) h3 = pop_header(tcpudp, on_mismatch=([],[])) h2 = pop_header(ipv4, on_mismatch=([],[])) h1 = pop_header(ether, on_mismatch=([],[])) packet_flow = LoadBalancedFlowc(h2.saddr, h2.daddr, h3.src_port, h3.dst_port, h2.npid) if (received_on_port == EXT_PORT and not flow_emap.has(packet_flow) and backend_ip_emap.exists_with_cht(cht, _LoadBalancedFlow_hash(packet_flow))): bknd = backend_ip_emap.choose_with_cht(cht, _LoadBalancedFlow_hash(packet_flow)) backend = backends.get(bknd) return ([backend.nic], [ether(h1, saddr=..., daddr=backend.mac), ipv4(h2, cksum=..., daddr=backend.ip), tcpudp(h3)]) pass # This allows us to ignore the state changes else: pass
from state import flow_emap EXP_TIME = 10 * 1000 EXT_IP_ADDR = ext_ip EXT_PORT = 1 if a_packet_received: flow_emap.expire_all(now - EXP_TIME) h3 = pop_header(tcpudp, on_mismatch=([], [])) h2 = pop_header(ipv4, on_mismatch=([], [])) h1 = pop_header(ether, on_mismatch=([], [])) internal_flow_id = FlowIdc(h3.src_port, h3.dst_port, h2.saddr, h2.daddr, received_on_port, h2.npid) if (received_on_port != EXT_PORT and not flow_emap.has(internal_flow_id) and not flow_emap.full()): idx = the_index_allocated flow_emap.add(internal_flow_id, idx, now) return ([EXT_PORT], [ ether(h1, saddr=..., daddr=...), ipv4(h2, cksum=..., saddr=EXT_IP_ADDR), tcpudp(h3, src_port=idx + start_port) ]) else: pass
from state import flow_emap, int_devices EXP_TIME = 10 * 1000 EXT_DEVICE = 1 if a_packet_received: flow_emap.expire_all(now - EXP_TIME) h3 = pop_header(tcpudp, on_mismatch=([],[])) h2 = pop_header(ipv4, on_mismatch=([],[])) h1 = pop_header(ether, on_mismatch=([],[])) if received_on_port == EXT_DEVICE: internal_flow = FlowIdc(h3.dst_port, h3.src_port, h2.daddr, h2.saddr, h2.npid) if flow_emap.has(internal_flow): flow_emap.refresh_idx(flow_emap.get(internal_flow), now) else: pass else: internal_flow = FlowIdc(h3.src_port, h3.dst_port, h2.saddr, h2.daddr, h2.npid) if flow_emap.has(internal_flow): flow_emap.refresh_idx(flow_emap.get(internal_flow), now) else: pass
if a_packet_received: flow_emap.expire_all(now - EXP_TIME) backend_ip_emap.expire_all(now - BACKEND_EXP_TIME) h3 = pop_header(tcpudp, on_mismatch=([],[])) h2 = pop_header(ipv4, on_mismatch=([],[])) h1 = pop_header(ether, on_mismatch=([],[])) assert a_packet_received assert h1.type == 8 # 0x0800 == IPv4 in big endian assert h2.npid == 6 or h2.npid == 17 # 6/17 -> TCP/UDP if received_on_port == EXT_PORT: # Packet from the external network - client packet_flow = LoadBalancedFlowc(h2.saddr, h2.daddr, h3.src_port, h3.dst_port, h2.npid) alloc_flow_and_process_packet = False; if flow_emap.has(packet_flow): flow_id = flow_emap.get(packet_flow) backend_id = flow_id_to_backend_id.get(flow_id) if backend_ip_emap.has_idx(backend_id): flow_emap.refresh_idx(flow_emap.get(packet_flow), now) backend = backends.get(backend_id) return ([backend.nic], [ether(h1, saddr=..., daddr=backend.mac), ipv4(h2, cksum=..., daddr=backend.ip), tcpudp(h3)]) else: flow_emap.erase(packet_flow) alloc_flow_and_process_packet = True else: alloc_flow_and_process_packet = True if alloc_flow_and_process_packet:
EXP_TIME = 10 * 1000 * 1000 * 1000 h2 = pop_header(ipv4, on_mismatch=([],[])) h1 = pop_header(ether, on_mismatch=([],[])) # Malformed IPv4 if (h2.vihl & 15) < 5 or packet_size - 14 < (((h2.len & 0xFF) << 8) | ((h2.len & 0xFF00) >> 8)): return ([],[]) flow_emap.expire_all(now - EXP_TIME) if received_on_port == LAN_DEVICE: return ([WAN_DEVICE],[ether(h1), ipv4(h2)]) else: if received_on_port == WAN_DEVICE: if flow_emap.has(ip_addrc(h2.daddr)): flow_idx = flow_emap.get(ip_addrc(h2.daddr)) flow_emap.refresh_idx(flow_idx, now) flow = dyn_vals.get(flow_idx) bucket_size = 0 if now - flow.bucket_time < EXP_TIME: bucket_size = flow.bucket_size + (now - flow.bucket_time)*RATE / 1000000000 if BURST < bucket_size: bucket_size = BURST else: bucket_size = BURST if packet_size < bucket_size: bucket_size = bucket_size - packet_size dyn_vals.set(flow_idx, DynamicValuec(bucket_size, now)) return ([LAN_DEVICE], [ether(h1),ipv4(h2)]) else:
from state import flow_emap EXP_TIME = 10 * 1000 EXT_PORT = 1 if a_packet_received: flow_emap.expire_all(now - EXP_TIME) h3 = pop_header(tcpudp, on_mismatch=([], [])) h2 = pop_header(ipv4, on_mismatch=([], [])) h1 = pop_header(ether, on_mismatch=([], [])) if received_on_port == EXT_PORT: flow_indx = h3.dst_port - start_port if flow_emap.has_idx(flow_indx): # Flow is present in the table flow_emap.refresh_idx(flow_indx, now) else: # packet from the internal network internal_flow_id = FlowIdc(h3.src_port, h3.dst_port, h2.saddr, h2.daddr, received_on_port, h2.npid) if flow_emap.has(internal_flow_id): # flow present in the table flow_emap.refresh_idx(flow_emap.get(internal_flow_id), now) elif not flow_emap.full(): # No flow in the table, but some space idx = the_index_allocated flow_emap.add(internal_flow_id, idx, now)
from state import flow_emap, flow_id_to_backend_id, backends, backend_ip_emap, cht EXP_TIME = 10 * 1000 BACKEND_EXP_TIME = 3600000000 * 1000 EXT_PORT = 2 if a_packet_received: flow_emap.expire_all(now - EXP_TIME) backend_ip_emap.expire_all(now - BACKEND_EXP_TIME) h3 = pop_header(tcpudp, on_mismatch=([], [])) h2 = pop_header(ipv4, on_mismatch=([], [])) h1 = pop_header(ether, on_mismatch=([], [])) packet_flow = LoadBalancedFlowc(h2.saddr, h2.daddr, h3.src_port, h3.dst_port, h2.npid) if received_on_port == EXT_PORT: if (not flow_emap.has(packet_flow) and backend_ip_emap.exists_with_cht( cht, _LoadBalancedFlow_hash(packet_flow)) and not flow_emap.full()): bknd = backend_ip_emap.choose_with_cht( cht, _LoadBalancedFlow_hash(packet_flow)) idx = the_index_allocated flow_emap.add(packet_flow, idx, now) flow_id_to_backend_id.set(idx, bknd) else: pass else: pass