def test_resume_tunnel_flows(self): # Need to delete all default flows in table 0 before # install the specific flows test case. self.test_detach_default_tunnel_flows() ip_flow_dl = IPFlowDL(set_params=0) ue_ip_addr = "192.168.128.80" self.classifier_controller.resume_tunnel_flows( 3, IPAddress(version=IPAddress.IPV4, address=ue_ip_addr.encode('utf-8')), ip_flow_dl=ip_flow_dl, ) ue_ip_addr = "192.168.128.82" self.classifier_controller.resume_tunnel_flows( 4, IPAddress(version=IPAddress.IPV4, address=ue_ip_addr.encode('utf-8')), ip_flow_dl=ip_flow_dl, ) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, ) with snapshot_verifier: pass
def testFlowVlanSnapshotMatch2(self): fake_inout_setup(self.inout_controller) cls = self.__class__ vlan1 = "21" self.setUpNetworkAndController(vlan1) vlan2 = "22" cls._setup_vlan(vlan2) ip_addr = ipaddress.ip_address("10.200.21.211") mocked_set_mobilityd_gw_info( IPAddress(address=ip_addr.packed, version=IPBlock.IPV4), "", vlan1) ip_addr = ipaddress.ip_address("10.200.22.211") mocked_set_mobilityd_gw_info( IPAddress(address=ip_addr.packed, version=IPBlock.IPV4), "", vlan2) hub.sleep(2) while gw_info_map[vlan2].mac is None or gw_info_map[vlan2].mac == '': threading.Event().wait(.5) logging.info("done waiting for vlan: %s", vlan1) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, max_sleep_time=40, datapath=cls.inout_controller._datapath, try_snapshot=True) with snapshot_verifier: pass self.assertEqual(gw_info_map[vlan1].mac, 'b2:a0:cc:85:80:21') self.assertEqual(gw_info_map[vlan2].mac, 'b2:a0:cc:85:80:22')
def test_resume_tunnel_ip_flow_dl(self): dest_ip = IPAddress( version=IPAddress.IPV4, address=socket.inet_pton(socket.AF_INET, "192.168.128.12"), ) src_ip = IPAddress( version=IPAddress.IPV4, address=socket.inet_pton(socket.AF_INET, "192.168.129.64"), ) ip_flow_dl = IPFlowDL( set_params=71, tcp_dst_port=0, tcp_src_port=5002, udp_dst_port=0, udp_src_port=0, ip_proto=6, dest_ip=dest_ip, src_ip=src_ip, precedence=65530, ) ue_ip_addr = "192.168.128.30" seid1 = 5000 self.classifier_controller.gtp_handler( 5, 65525, 1, 100000, self.EnodeB_IP, ue_ip_addr, seid1, ip_flow_dl=ip_flow_dl, ) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, ) with snapshot_verifier: pass
def release_ip_handler(client, args): try: sid_msg = SIDUtils.to_pb(args.sid) except ValueError: print("Error: invalid SubscriberID format: %s" % args.sid) return try: ip = ipaddress.ip_address(args.ip) except ValueError: print("Error: invalid IP format: %s" % args.ip) return ip_msg = IPAddress() if ip.version == 4: ip_msg.version = IPAddress.IPV4 elif ip.version == 6: ip_msg.version = IPAddress.IPV6 else: print("Error: unknown IP version") return ip_msg.address = ip.packed request = ReleaseIPRequest() request.sid.CopyFrom(sid_msg) request.ip.CopyFrom(ip_msg) client.ReleaseIPAddress(request)
def test_set_gw_info_vlan2(self): mac1 = "22:22:c6:d0:02:3c" ipaddr1 = ipaddress.ip_address("10.1.1.11") gwinfo_msg1 = GWInfo(ip=IPAddress(version=IPAddress.IPV4, address=ipaddr1.packed), mac=mac1, vlan="1") self._stub.SetGatewayInfo(gwinfo_msg1) mac2 = "33:22:c6:d0:02:3c" ipaddr2 = ipaddress.ip_address("20.1.1.11") gwinfo_msg2 = GWInfo(ip=IPAddress(version=IPAddress.IPV4, address=ipaddr2.packed), mac=mac2, vlan="2") self._stub.SetGatewayInfo(gwinfo_msg2) gw_info_list = self._stub.ListGatewayInfo(Void()) count1 = 0 count2 = 0 for gw_info in gw_info_list.gw_list: if gw_info == gwinfo_msg1: count1 = count1 + 1 if gw_info == gwinfo_msg2: count2 = count2 + 1 self.assertEqual(count1, 1) self.assertEqual(count2, 1)
def AllocateIPAddress(self, request, context): """ Allocate an IP address from the free IP pool """ if request.version == AllocateIPRequest.IPV4: try: ip_addr = IPAddress() composite_sid = SIDUtils.to_str(request.sid) if request.apn: composite_sid = composite_sid + "." + request.apn ip, vlan = self._ipv4_allocator.alloc_ip_address(composite_sid) logging.info("Allocated IPv4 %s for sid %s for apn %s" % (ip, SIDUtils.to_str(request.sid), request.apn)) ip_addr.version = IPAddress.IPV4 ip_addr.address = ip.packed return AllocateIPAddressResponse(ip_addr=ip_addr, vlan=str(vlan)) except NoAvailableIPError: context.set_details('No free IPv4 IP available') context.set_code(grpc.StatusCode.RESOURCE_EXHAUSTED) except DuplicatedIPAllocationError: context.set_details('IP has been allocated for this subscriber') context.set_code(grpc.StatusCode.ALREADY_EXISTS) else: self._unimplemented_ip_version_error(context) return AllocateIPAddressResponse()
def GetIPForSubscriber(self, request, context): logging.debug("Received GetIPForSubscriber") self._print_grpc(request) composite_sid = SIDUtils.to_str(request.sid) if request.apn: composite_sid = composite_sid + "." + request.apn if request.version == IPAddress.IPV4: composite_sid += ",ipv4" elif request.version == IPAddress.IPV6: composite_sid += ",ipv6" ip = self._ip_address_man.get_ip_for_sid(composite_sid) if ip is None: context.set_details( 'SID %s not found' % SIDUtils.to_str(request.sid), ) context.set_code(grpc.StatusCode.NOT_FOUND) resp = IPAddress() else: version = IPAddress.IPV4 if ip.version == 4 else IPAddress.IPV6 resp = IPAddress(version=version, address=ip.packed) self._print_grpc(resp) return resp
def test_attach_tunnel_flows(self): # Need to delete all default flows in table 0 before # install the specific flows test case. self.test_detach_default_tunnel_flows() ip_no = hex(socket.htonl(int(ipaddress.ip_address(self.EnodeB_IP)))) buf = "g_{}".format(ip_no[2:]) BridgeTools.create_veth_pair(buf, buf + "ns") BridgeTools.add_ovs_port(self.BRIDGE, buf, "40") seid1 = 5000 ue_ip_addr = "192.168.128.30" self.classifier_controller.add_tunnel_flows(65525, 1, 100000, IPAddress(version=IPAddress.IPV4,address=ue_ip_addr.encode('utf-8')), self.EnodeB_IP, seid1) seid2 = 5001 ue_ip_addr = "192.168.128.31" self.classifier_controller.add_tunnel_flows(65525, 2,100001, IPAddress(version=IPAddress.IPV4,address=ue_ip_addr.encode('utf-8')), self.EnodeB_IP, seid2) snapshot_verifier = SnapshotVerifier(self, self.BRIDGE, self.service_manager) with snapshot_verifier: pass
def testFlowVlanSnapshotMatch_static1(self): cls = self.__class__ # setup network on unused vlan. vlan1 = "21" self.setUpNetworkAndController(vlan1) # statically configured config vlan2 = "22" ip_addr = ipaddress.ip_address("10.200.21.211") mocked_set_mobilityd_gw_info( IPAddress(address=ip_addr.packed, version=IPBlock.IPV4), "11:33:44:55:66:77", vlan1) ip_addr = ipaddress.ip_address("10.200.22.211") mocked_set_mobilityd_gw_info( IPAddress(address=ip_addr.packed, version=IPBlock.IPV4), "22:33:44:55:66:77", vlan2) hub.sleep(2) while gw_info_map[vlan2].mac is None or gw_info_map[vlan2].mac == '': threading.Event().wait(.5) logging.info("done waiting for vlan: %s", vlan1) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, max_sleep_time=20, datapath=cls.inout_controller._datapath, try_snapshot=True) with snapshot_verifier: pass self.assertEqual(gw_info_map[vlan1].mac, 'b2:a0:cc:85:80:21') self.assertEqual(gw_info_map[vlan2].mac, '22:33:44:55:66:77')
def test_detach_tunnel_flows_ipv6(self): ip_flow_dl = IPFlowDL(set_params=0) ue_ip_addr = "2001::1" self.classifier_controller.delete_tunnel_flows( 1, None, ip_flow_dl=ip_flow_dl, ue_ipv6_adr=IPAddress(version=IPAddress.IPV6, address=ue_ip_addr.encode('utf-8')), ) ue_ip_addr = "2001:db8::1" self.classifier_controller.delete_tunnel_flows( 2, None, ip_flow_dl=ip_flow_dl, ue_ipv6_adr=IPAddress(version=IPAddress.IPV6, address=ue_ip_addr.encode('utf-8')), ) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, ) with snapshot_verifier: pass
def test_discard_tunnel_flows(self): ip_flow_dl = IPFlowDL(set_params=0) self.classifier_controller._delete_all_flows() ue_ip_addr = "192.168.128.80" self.classifier_controller.discard_tunnel_flows( 3, IPAddress(version=IPAddress.IPV4, address=ue_ip_addr.encode('utf-8')), ip_flow_dl=ip_flow_dl, ) ue_ip_addr = "192.168.128.82" self.classifier_controller.discard_tunnel_flows( 4, IPAddress(version=IPAddress.IPV4, address=ue_ip_addr.encode('utf-8')), ip_flow_dl=ip_flow_dl, ) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, ) with snapshot_verifier: pass
def test_detach_tunnel_flows(self): ue_ip_addr = "192.168.128.30" ip_flow_dl = IPFlowDL(set_params=0) self.classifier_controller.delete_tunnel_flows( 1, IPAddress(version=IPAddress.IPV4, address=ue_ip_addr.encode('utf-8')), ip_flow_dl=ip_flow_dl, ) ue_ip_addr = "192.168.128.31" self.classifier_controller.delete_tunnel_flows( 2, IPAddress(version=IPAddress.IPV4, address=ue_ip_addr.encode('utf-8')), ip_flow_dl=ip_flow_dl, ) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, ) with snapshot_verifier: pass
def add_rule(args): rule_id = args.rule_id policy_dict = PolicyRuleDict() arg_list = { 'ip_proto': args.ip_proto, 'ip_dst': IPAddress(version=IPAddress.IPV4, address=args.ipv4_dst.encode('utf-8')), 'ip_src': IPAddress(version=IPAddress.IPV4, address=args.ipv4_src.encode('utf-8')), 'tcp_dst': args.tcp_dst, 'tcp_src': args.tcp_src, 'udp_dst': args.udp_dst, 'udp_src': args.udp_src, 'direction': args.direction } match = FlowMatch(**arg_list) flow = FlowDescription(match=match, action=args.action) rule = policy_dict.get(rule_id) if not rule or args.overwrite: action = 'add' rule = PolicyRule(id=rule_id, flow_list=[flow]) else: action = 'edit' rule.flow_list.extend([flow]) policy_dict[rule_id] = rule print("Rule '%s' successfully %sed!" % (rule_id, action))
def test_attach_tunnel_flows_ipv6(self): # Need to delete all default flows in table 0 before # install the specific flows test case. self.test_detach_default_tunnel_flows() seid1 = 5000 ue_ip_addr = "2001::1" self.classifier_controller.add_tunnel_flows( 65525, 1, 10000, IPAddress(version=IPAddress.IPV6, address=ue_ip_addr.encode('utf-8')), self.EnodeB_IP, seid1, True) seid2 = 5001 ue_ip_addr = "2001:db8::1" self.classifier_controller.add_tunnel_flows( 65525, 2, 100001, IPAddress(version=IPAddress.IPV6, address=ue_ip_addr.encode('utf-8')), self.EnodeB_IP, seid2, True) snapshot_verifier = SnapshotVerifier(self, self.BRIDGE, self.service_manager) with snapshot_verifier: pass
def testFlowVlanSnapshotMatch_static2(self): cls = self.__class__ # setup network on unused vlan. self.setUpNetworkAndController("34") # statically configured config fake_mandatory_controller_setup(cls.ingress_controller) fake_mandatory_controller_setup(cls.middle_controller) fake_mandatory_controller_setup(cls.egress_controller) vlan1 = "31" ip_addr = ipaddress.ip_address("10.200.21.100") mocked_set_mobilityd_gw_info( IPAddress( address=ip_addr.packed, version=IPBlock.IPV4, ), "11:33:44:55:66:77", vlan1, ) vlan2 = "32" ip_addr = ipaddress.ip_address("10.200.22.200") mocked_set_mobilityd_gw_info( IPAddress( address=ip_addr.packed, version=IPBlock.IPV4, ), "22:33:44:55:66:77", vlan2, ) ip_addr = ipaddress.ip_address("10.200.22.10") mocked_set_mobilityd_gw_info( IPAddress( address=ip_addr.packed, version=IPBlock.IPV4, ), "00:33:44:55:66:77", "", ) check_GW_rec(vlan1) check_GW_rec(vlan2) check_GW_rec("") assert_GW_mac(self, vlan1, '11:33:44:55:66:77') assert_GW_mac(self, vlan2, '22:33:44:55:66:77') assert_GW_mac(self, "", '00:33:44:55:66:77') hub.sleep(2) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, max_sleep_time=20, datapath=cls.ingress_controller._datapath, try_snapshot=True, ) with snapshot_verifier: pass
def testFlowVlanSnapshotMatch_static2(self): fake_inout_setup(self.inout_controller) cls = self.__class__ # setup network on unused vlan. self.setUpNetworkAndController("34") # statically configured config vlan1 = "31" ip_addr = ipaddress.ip_address("10.200.21.100") mocked_set_mobilityd_gw_info( IPAddress( address=ip_addr.packed, version=IPBlock.IPV4, ), "11:33:44:55:66:77", vlan1, ) vlan2 = "32" ip_addr = ipaddress.ip_address("10.200.22.200") mocked_set_mobilityd_gw_info( IPAddress( address=ip_addr.packed, version=IPBlock.IPV4, ), "22:33:44:55:66:77", vlan2, ) ip_addr = ipaddress.ip_address("10.200.22.10") mocked_set_mobilityd_gw_info( IPAddress( address=ip_addr.packed, version=IPBlock.IPV4, ), "00:33:44:55:66:77", "", ) hub.sleep(2) while gw_info_map[vlan2].mac is None or gw_info_map[vlan2].mac == '': threading.Event().wait(.5) logging.info("done waiting for vlan: %s", vlan1) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, max_sleep_time=20, datapath=cls.inout_controller._datapath, ) with snapshot_verifier: pass self.assertEqual(gw_info_map[vlan1].mac, '11:33:44:55:66:77') self.assertEqual(gw_info_map[vlan2].mac, '22:33:44:55:66:77') self.assertEqual(gw_info_map[""].mac, '00:33:44:55:66:77')
def create_ue_session_entry(tunnel_msg: UESessionSet) -> UESessionSet: """Do create named tuple from input message Args: tunnel_msg: Message from MME Returns: UESessionSet """ sid = 0 ue_ipv4_address = None ue_ipv6_address = None enb_ipv4_address = None apn = None if tunnel_msg.subscriber_id.id: sid = encode_imsi(tunnel_msg.subscriber_id.id) if tunnel_msg.ue_ipv4_address.address: addr_str = socket.inet_ntop( socket.AF_INET, tunnel_msg.ue_ipv4_address.address, ) ue_ipv4_address = IPAddress( version=tunnel_msg.ue_ipv4_address.version, address=addr_str.encode('utf8'), ) if tunnel_msg.ue_ipv6_address.address: addr_str = socket.inet_ntop( socket.AF_INET6, tunnel_msg.ue_ipv6_address.address, ) ue_ipv6_address = IPAddress( version=tunnel_msg.ue_ipv6_address.version, address=addr_str.encode('utf8'), ) if tunnel_msg.enb_ip_address.address: enb_ipv4_address = ipaddress.ip_address( tunnel_msg.enb_ip_address.address, ) if tunnel_msg.apn: apn = tunnel_msg.apn return ( UESessionEntry( sid=sid, precedence=tunnel_msg.precedence, ue_ipv4_address=ue_ipv4_address, ue_ipv6_address=ue_ipv6_address, enb_ip_address=enb_ipv4_address, apn=apn, vlan=tunnel_msg.vlan, in_teid=tunnel_msg.in_teid, out_teid=tunnel_msg.out_teid, ue_session_state=tunnel_msg.ue_session_state.ue_config_state, ip_flow_dl=tunnel_msg.ip_flow_dl, ) )
def GetIPForSubscriber(self, sid, context): sid_str = SIDUtils.to_str(sid) ip = self._ipv4_allocator.get_ip_for_sid(sid_str) if ip is None: context.set_details('SID %s not found' % sid) context.set_code(grpc.StatusCode.NOT_FOUND) return IPAddress() version = IPAddress.IPV4 if ip.version == 4 else IPAddress.IPV6 return IPAddress(version=version, address=ip.packed)
def test_add_tunnel_ip_flow_dl(self): # Need to delete all default flows in table 0 before # install the specific flows test case. self.test_detach_default_tunnel_flows() dest_ip = IPAddress( version=IPAddress.IPV4, address=socket.inet_pton(socket.AF_INET, "192.168.128.12"), ) src_ip = IPAddress( version=IPAddress.IPV4, address=socket.inet_pton(socket.AF_INET, "192.168.129.64"), ) ip_flow_dl = IPFlowDL( set_params=71, tcp_dst_port=0, tcp_src_port=5002, udp_dst_port=0, udp_src_port=0, ip_proto=6, dest_ip=dest_ip, src_ip=src_ip, precedence=65530, ) seid1 = 5000 ue_ip_addr = "192.168.128.30" self.classifier_controller.gtp_handler( 0, 65525, 1, 100000, self.EnodeB_IP, ue_ip_addr, seid1, ip_flow_dl=ip_flow_dl, ) dest_ip = IPAddress( version=IPAddress.IPV4, address=socket.inet_pton(socket.AF_INET, "192.168.128.111"), ) src_ip = IPAddress( version=IPAddress.IPV4, address=socket.inet_pton(socket.AF_INET, "192.168.129.4"), ) ip_flow_dl = IPFlowDL( set_params=70, tcp_dst_port=0, tcp_src_port=0, udp_dst_port=80, udp_src_port=5060, ip_proto=17, dest_ip=dest_ip, src_ip=src_ip, precedence=65525, ) seid2 = 5001 ue_ip_addr = "192.168.128.31" self.classifier_controller.gtp_handler( 0, 65525, 2, 100001, self.EnodeB_IP, ue_ip_addr, seid2, ip_flow_dl=ip_flow_dl, ) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, ) with snapshot_verifier: pass
def get_flow_match(self, flow_list, flow_match_list): """ Populates flow match list """ for flow in flow_list: flow_direction = (FlowMatch.UPLINK if flow["direction"] == "UL" else FlowMatch.DOWNLINK) ip_protocol = flow["ip_proto"] if ip_protocol == "TCP": ip_protocol = FlowMatch.IPPROTO_TCP udp_src_port = 0 udp_dst_port = 0 tcp_src_port = (int(flow["tcp_src_port"]) if "tcp_src_port" in flow else 0) tcp_dst_port = (int(flow["tcp_dst_port"]) if "tcp_dst_port" in flow else 0) elif ip_protocol == "UDP": ip_protocol = FlowMatch.IPPROTO_UDP tcp_src_port = 0 tcp_dst_port = 0 udp_src_port = (int(flow["udp_src_port"]) if "udp_src_port" in flow else 0) udp_dst_port = (int(flow["udp_dst_port"]) if "udp_dst_port" in flow else 0) else: udp_src_port = 0 udp_dst_port = 0 tcp_src_port = 0 tcp_dst_port = 0 ipv4_src_addr = None if flow.get("ipv4_src", None): ipv4_src_addr = IPAddress( version=IPAddress.IPV4, address=flow.get("ipv4_src").encode('utf-8')) ipv4_dst_addr = None if flow.get("ipv4_dst", None): ipv4_dst_addr = IPAddress( version=IPAddress.IPV4, address=flow.get("ipv4_dst").encode('utf-8')) flow_match_list.append( FlowDescription( match=FlowMatch( ip_dst=ipv4_dst_addr, ip_src=ipv4_src_addr, tcp_src=tcp_src_port, tcp_dst=tcp_dst_port, udp_src=udp_src_port, udp_dst=udp_dst_port, ip_proto=ip_protocol, direction=flow_direction, ), action=FlowDescription.PERMIT, ))
def test_detach_tunnel_flows_ipv6(self): ue_ip_addr = "2001::1" self.classifier_controller.delete_tunnel_flows(1, IPAddress(version=IPAddress.IPV6,address=ue_ip_addr.encode('utf-8'))) ue_ip_addr = "2001:db8::1" self.classifier_controller.delete_tunnel_flows(2, IPAddress(version=IPAddress.IPV6,address=ue_ip_addr.encode('utf-8'))) snapshot_verifier = SnapshotVerifier(self, self.BRIDGE, self.service_manager) with snapshot_verifier: pass
def _get_ip_flow_dl_match(self, ip_flow_dl: IPFlowDL, in_port: int): dest_ip = None src_ip = None if ip_flow_dl.dest_ip.address: addr_str = socket.inet_ntop( socket.AF_INET, ip_flow_dl.dest_ip.address, ) dest_ip = IPAddress( version=IPAddress.IPV4, address=addr_str.encode('utf8'), ) if ip_flow_dl.src_ip.address: addr_str = socket.inet_ntop( socket.AF_INET, ip_flow_dl.src_ip.address, ) src_ip = IPAddress( version=IPAddress.IPV4, address=addr_str.encode('utf8'), ) ip_match_dst = get_ue_ip_match_args(dest_ip, Direction.IN) ip_match_src = get_ue_ip_match_args(src_ip, Direction.OUT) if ip_flow_dl.ip_proto == IPPROTO_TCP: match = MagmaMatch( eth_type=get_eth_type(ip_flow_dl.dest_ip), in_port=in_port, **ip_match_dst, **ip_match_src, ip_proto=ip_flow_dl.ip_proto, tcp_src=ip_flow_dl.tcp_src_port, tcp_dst=ip_flow_dl.tcp_dst_port, ) elif ip_flow_dl.ip_proto == IPPROTO_UDP: match = MagmaMatch( eth_type=get_eth_type(ip_flow_dl.dest_ip), in_port=in_port, **ip_match_dst, **ip_match_src, ip_proto=ip_flow_dl.ip_proto, udp_src=ip_flow_dl.udp_src_port, udp_dst=ip_flow_dl.udp_dst_port, ) return match
def test_discard_tunnel_flows(self): self.classifier_controller._delete_all_flows() ue_ip_addr = "192.168.128.80" self.classifier_controller._discard_tunnel_flows(65525, 3, IPAddress(version=IPAddress.IPV4,address=ue_ip_addr.encode('utf-8'))) ue_ip_addr = "192.168.128.82" self.classifier_controller._discard_tunnel_flows(65525, 4, IPAddress(version=IPAddress.IPV4,address=ue_ip_addr.encode('utf-8'))) snapshot_verifier = SnapshotVerifier(self, self.BRIDGE, self.service_manager) with snapshot_verifier: pass
def GetIPForSubscriber(self, request, context): composite_sid = SIDUtils.to_str(request.sid) if request.apn: composite_sid = composite_sid + "." + request.apn ip = self._ipv4_allocator.get_ip_for_sid(composite_sid) if ip is None: context.set_details('SID %s not found' % SIDUtils.to_str(request.sid)) context.set_code(grpc.StatusCode.NOT_FOUND) return IPAddress() version = IPAddress.IPV4 if ip.version == 4 else IPAddress.IPV6 return IPAddress(version=version, address=ip.packed)
def test_detach_multi_tunnel_flows(self): ue_ip_addr = "192.168.128.30" self.classifier_controller.delete_tunnel_flows(1, IPAddress(version=IPAddress.IPV4,address=ue_ip_addr.encode('utf-8')), self.EnodeB_IP) ue_ip_addr = "192.168.128.31" self.classifier_controller.delete_tunnel_flows(2, IPAddress(version=IPAddress.IPV4,address=ue_ip_addr.encode('utf-8')), self.EnodeB2_IP) ue_ip_addr = "192.168.128.51" self.classifier_controller.delete_tunnel_flows(5, IPAddress(version=IPAddress.IPV4,address=ue_ip_addr.encode('utf-8')), self.EnodeB2_IP) snapshot_verifier = SnapshotVerifier(self, self.BRIDGE, self.service_manager) with snapshot_verifier: pass
def _old_flow_stats(self, stats_msgs): """ Generator function to filter the flow stats that should be deleted from the stats messages. """ for flow_stats in stats_msgs: for stat in flow_stats: if stat.table_id != self.tbl_num: # this update is not intended for policy return rule_id = self._get_rule_id(stat) sid = _get_sid(stat) ipv4_addr_str = _get_ipv4(stat) ipv4_addr = None if ipv4_addr_str: ipv4_addr = IPAddress(version=IPAddress.IPV4, address=ipv4_addr_str.encode('utf-8')) rule_version = _get_version(stat) if rule_id == "": continue current_ver = \ self._session_rule_version_mapper.get_version(sid, ipv4_addr, rule_id) if current_ver != rule_version: yield stat
def test_release_unknown_sid_apn_ip_tuple(self): """ releasing unknown sid-apn-ip tuple should raise NOT_FOUND """ self._stub.AddIPBlock(self._block_msg) alloc_request0 = AllocateIPRequest(sid=self._sid0, version=AllocateIPRequest.IPV4, apn=self._apn0) ip_msg0 = self._stub.AllocateIPAddress(alloc_request0) request = ReleaseIPRequest(sid=SIDUtils.to_pb("IMSI12345"), ip=ip_msg0.ip_list[0], apn=self._apn0) with self.assertRaises(grpc.RpcError) as err: self._stub.ReleaseIPAddress(request) self.assertEqual(err.exception.code(), grpc.StatusCode.NOT_FOUND) ip_bytes = bytes(map(int, '10.0.0.0'.split('.'))) request.ip.CopyFrom(IPAddress(version=IPAddress.IPV4, address=ip_bytes)) with self.assertRaises(grpc.RpcError) as err: self._stub.ReleaseIPAddress(request) self.assertEqual(err.exception.code(), grpc.StatusCode.NOT_FOUND) request = ReleaseIPRequest(sid=self._sid0, ip=ip_msg0.ip_list[0], apn=self._apn1) with self.assertRaises(grpc.RpcError) as err: self._stub.ReleaseIPAddress(request) self.assertEqual(err.exception.code(), grpc.StatusCode.NOT_FOUND)
def ListAllocatedIPs(self, ipblock_msg, context): """ Return a list of IPs allocated from a IP block Args: ipblock_msg (IPBlock): ip block to add. ipblock_msg has the type IPBlock, a protobuf message type for the gRPC interface. Internal representation of ip blocks uses the ipaddress.ip_network type and is named as ipblock. """ resp = ListAllocatedIPsResponse() ipblock = self._ipblock_msg_to_ipblock(ipblock_msg, context) if ipblock is None: return resp if ipblock_msg.version == IPBlock.IPV4: try: ips = self._ipv4_allocator.list_allocated_ips(ipblock) ip_msg_list = [ IPAddress(version=IPAddress.IPV4, address=ip.packed) for ip in ips ] resp.ip_list.extend(ip_msg_list) except IPBlockNotFoundError: context.set_details('IP block not found: %s' % ipblock) context.set_code(grpc.StatusCode.FAILED_PRECONDITION) else: self._unimplemented_ip_version_error(context) return resp
def test_attach_tunnel_flows(self): # Need to delete all default flows in table 0 before # install the specific flows test case. self.test_detach_default_tunnel_flows() seid1 = 5000 ue_ip_addr = "192.168.128.30" ip_flow_dl = IPFlowDL(set_params=0) self.classifier_controller.add_tunnel_flows( 65525, 1, 100000, self.EnodeB_IP, IPAddress(version=IPAddress.IPV4, address=ue_ip_addr.encode('utf-8')), seid1, True, ip_flow_dl=ip_flow_dl, ) snapshot_verifier = SnapshotVerifier( self, self.BRIDGE, self.service_manager, ) with snapshot_verifier: pass
def _ip_desc_to_proto(desc): """ Convert an IP descriptor to protobuf. Args: desc (magma.mobilityd.IPDesc): IP descriptor Returns: proto (protos.keyval_pb2.IPDesc): protobuf of :desc: """ ip = IPAddress( version=_ip_version_int_to_proto(desc.ip_block.version), address=desc.ip.packed, ) ip_block = IPBlock( version=_ip_version_int_to_proto(desc.ip_block.version), net_address=desc.ip_block.network_address.packed, prefix_len=desc.ip_block.prefixlen, ) state = _desc_state_str_to_proto(desc.state) sid = SubscriberID( id=desc.sid, type=SubscriberID.IMSI, ) proto = IPDesc(ip=ip, ip_block=ip_block, state=state, sid=sid) return proto