Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 1 | from distutils.util import strtobool |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 2 | import copy |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 3 | |
| 4 | FALSE = '0' |
| 5 | TRUE = '1' |
| 6 | DIR_UPLINK = '1' |
| 7 | DIR_DOWNLINK = '2' |
| 8 | IFACE_ACCESS = '1' |
| 9 | IFACE_CORE = '2' |
| 10 | TUNNEL_SPORT = '2152' |
| 11 | TUNNEL_TYPE_GPDU = '3' |
| 12 | |
| 13 | UE_PORT = 400 |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 14 | DEFAULT_PDN_PORT = 800 |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 15 | GPDU_PORT = 2152 |
| 16 | |
Daniele Moro | a197519 | 2022-01-21 18:01:19 +0100 | [diff] [blame] | 17 | DEFAULT_APP_ID = 0 |
Daniele Moro | 4650ffb | 2022-02-15 22:44:18 +0100 | [diff] [blame] | 18 | DEFAULT_SESSION_METER_IDX = 0 |
| 19 | DEFAULT_APP_METER_IDX = 0 |
| 20 | |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 21 | |
| 22 | class UP4: |
| 23 | """ |
| 24 | Utility that manages interaction with UP4 via a P4RuntimeCliDriver available |
| 25 | in the cluster. Additionally, can verify connectivity by crafting GTP packets |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 26 | via Scapy with an HostDriver component, specified via <enodebs>, <pdn_host>, |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 27 | and <router_mac> parameters. |
| 28 | |
| 29 | Example params file: |
| 30 | <UP4> |
| 31 | <pdn_host>Compute1</pdn_host> # Needed to verify connectivity with scapy |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 32 | <enodebs> # List of emulated eNodeBs |
| 33 | <enode_1> |
| 34 | <host>Compute1</host> # Host that emulates this eNodeB |
| 35 | <interface>eno3</interface> # Name of the linux interface to use on the host, if not specified take the default |
| 36 | <enb_address>10.32.11.122</enb_address> # IP address of the eNodeB |
| 37 | <ues>ue3</ues> # Emulated ues connected to this eNB |
| 38 | </enode_1> |
| 39 | <enodeb_2> |
| 40 | <host>Compute3</host> |
| 41 | <enb_address>10.32.11.194</enb_address> |
| 42 | <ues>ue1,ue2</ues> |
| 43 | </enodeb_2> |
| 44 | </enodebs> |
| 45 | <enodeb_host>Compute3</enodeb_host> |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 46 | <router_mac>00:00:0A:4C:1C:46</router_mac> # Needed to verify connectivity with scapy |
| 47 | <s1u_address>10.32.11.126</s1u_address> |
Daniele Moro | cc4ecda | 2022-02-25 23:27:25 +0100 | [diff] [blame] | 48 | <slice_id>1</slice_id> # Mobile SLICE ID, used when pushing application filtering entries |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 49 | <ues> |
| 50 | <ue2> |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 51 | <ue_address>10.240.0.2</ue_address> |
| 52 | <teid>200</teid> |
| 53 | <up_id>20</up_id> |
| 54 | <down_id>21</down_id> |
Carmelo Cascone | 848d1f5 | 2022-01-27 18:15:58 -0800 | [diff] [blame] | 55 | <!-- TC 0 means BEST EFFORT --> |
| 56 | <tc>2</tc> |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 57 | <five_g>False</five_g> |
| 58 | </ue2> |
| 59 | </ues> |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 60 | <switch_to_kill>Leaf2</switch_to_kill> # Component name of the switch to kill in CASE 5 |
| 61 | <enodebs_fail>enodeb_1</enodebs_fail> # List of eNodeBs that should fail traffic forwarding in CASE 5 |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 62 | </UP4> |
| 63 | """ |
| 64 | |
| 65 | def __init__(self): |
| 66 | self.s1u_address = None |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 67 | self.enodebs = None |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 68 | self.pdn_host = None |
| 69 | self.pdn_interface = None |
| 70 | self.router_mac = None |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 71 | self.emulated_ues = {} |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 72 | self.app_filters = {} |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 73 | self.up4_client = None |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 74 | self.no_host = False |
Daniele Moro | cc4ecda | 2022-02-25 23:27:25 +0100 | [diff] [blame] | 75 | self.slice_id = None |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 76 | |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 77 | def setup(self, p4rt_client, no_host=False): |
| 78 | """ |
| 79 | Set up P4RT and scapy on eNB and PDN hosts |
| 80 | :param p4rt_client: a P4RuntimeCliDriver component |
| 81 | :param no_host: True if you don't want to start scapy on the hosts |
| 82 | :return: |
| 83 | """ |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 84 | self.s1u_address = main.params["UP4"]["s1u_address"] |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 85 | self.emulated_ues = main.params["UP4"]['ues'] |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 86 | self.app_filters = main.params["UP4"]['app_filters'] |
Daniele Moro | cc4ecda | 2022-02-25 23:27:25 +0100 | [diff] [blame] | 87 | self.slice_id = main.params["UP4"]['slice_id'] |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 88 | self.up4_client = p4rt_client |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 89 | self.no_host = no_host |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 90 | |
| 91 | # Optional Parameters |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 92 | |
| 93 | self.enodebs = copy.deepcopy((main.params["UP4"]["enodebs"])) |
| 94 | for enb in self.enodebs.values(): |
| 95 | enb["ues"] = enb["ues"].split(",") |
| 96 | enb["host"] = getattr(main, enb["host"]) |
| 97 | # If interface not provided by the params, use the default in the host |
| 98 | if "interface" not in enb.keys(): |
| 99 | enb["interface"] = enb["host"].interfaces[0]["name"] |
| 100 | if "pdn_host" in main.params["UP4"]: |
| 101 | self.pdn_host = getattr(main, main.params["UP4"]["pdn_host"]) |
| 102 | self.pdn_interface = self.pdn_host.interfaces[0] |
| 103 | self.router_mac = main.params["UP4"].get("router_mac", None) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 104 | |
Jon Hall | a601dde | 2022-02-28 14:22:07 -0800 | [diff] [blame] | 105 | for app_filter in self.app_filters.values(): |
| 106 | if app_filter.get('slice_id', None) is None: |
| 107 | app_filter['slice_id'] = self.slice_id |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 108 | # Start components |
| 109 | self.up4_client.startP4RtClient() |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 110 | if not self.no_host: |
| 111 | if self.enodebs is not None: |
| 112 | for enb in self.enodebs.values(): |
| 113 | enb["host"].startScapy(ifaceName=enb["interface"], |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 114 | enableGtp=True) |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 115 | if self.pdn_host is not None: |
| 116 | self.pdn_host.startScapy(ifaceName=self.pdn_interface["name"]) |
Daniele Moro | cc4ecda | 2022-02-25 23:27:25 +0100 | [diff] [blame] | 117 | # TODO: configure interfaces table. Currently, we rely on netcfg or |
| 118 | # PFCP agent to push interface entries, but we should explicitly push |
| 119 | # them here |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 120 | |
| 121 | def teardown(self): |
| 122 | self.up4_client.stopP4RtClient() |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 123 | if not self.no_host: |
| 124 | if self.enodebs is not None: |
| 125 | for enb in self.enodebs.values(): |
| 126 | enb["host"].stopScapy() |
| 127 | if self.pdn_host is not None: |
| 128 | self.pdn_host.stopScapy() |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 129 | |
| 130 | def attachUes(self): |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 131 | for app_filter in self.app_filters.values(): |
| 132 | self.insertAppFilter(**app_filter) |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 133 | for (name, ue) in self.emulated_ues.items(): |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 134 | ue = UP4.__sanitizeUeData(ue) |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 135 | self.attachUe(name, **ue) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 136 | |
| 137 | def detachUes(self): |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 138 | for app_filter in self.app_filters.values(): |
| 139 | self.removeAppFilter(**app_filter) |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 140 | for (name, ue) in self.emulated_ues.items(): |
Daniele Moro | 249d6e7 | 2021-09-20 10:32:54 +0200 | [diff] [blame] | 141 | ue = UP4.__sanitizeUeData(ue) |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 142 | self.detachUe(name, **ue) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 143 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 144 | def __pickPortFromRange(self, range): |
| 145 | if range is None or len(range) == 0: |
| 146 | return DEFAULT_PDN_PORT |
| 147 | # First port in range |
| 148 | return int(range.split('..')[0]) |
| 149 | |
| 150 | def testUpstreamTraffic(self, enb_names=None, app_filter_name=None, shouldFail=False): |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 151 | if self.enodebs is None or self.pdn_host is None: |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 152 | main.log.error( |
| 153 | "Need eNodeB and PDN host params to generate scapy traffic") |
| 154 | return |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 155 | if enb_names is None or enb_names == []: |
| 156 | enodebs = self.enodebs.values() |
| 157 | else: |
| 158 | enodebs = [self.enodebs[enb] for enb in enb_names] |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 159 | |
| 160 | app_filter = self.app_filters[app_filter_name] |
| 161 | pdn_port = self.__pickPortFromRange(app_filter.get("port_range", None)) |
| 162 | app_filter_should_drop = app_filter["action"] != "allow" |
| 163 | |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 164 | pkt_filter_upstream = "" |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 165 | ues = [] |
| 166 | for enb in enodebs: |
| 167 | for ue_name in enb["ues"]: |
| 168 | ue = self.emulated_ues[ue_name] |
| 169 | if "ue_address" in ue: |
| 170 | ues.append(ue) |
| 171 | if len(pkt_filter_upstream) != 0: |
| 172 | pkt_filter_upstream += " or " |
| 173 | pkt_filter_upstream += "src host " + ue["ue_address"] |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 174 | pkt_filter_upstream = "ip and udp dst port %s and (%s) and dst host %s" % \ |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 175 | (pdn_port, pkt_filter_upstream, |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 176 | self.pdn_interface["ips"][0]) |
| 177 | main.log.info("Start listening on %s intf %s" % |
| 178 | (self.pdn_host.name, self.pdn_interface["name"])) |
| 179 | main.log.debug("BPF Filter Upstream: \n %s" % pkt_filter_upstream) |
| 180 | self.pdn_host.startFilter(ifaceName=self.pdn_interface["name"], |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 181 | sniffCount=len(ues), |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 182 | pktFilter=pkt_filter_upstream) |
| 183 | |
| 184 | main.log.info( |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 185 | "Sending %d packets from eNodeB host" % len(ues)) |
| 186 | for enb in enodebs: |
| 187 | for ue_name in enb["ues"]: |
| 188 | main.log.info(ue_name) |
| 189 | ue = self.emulated_ues[ue_name] |
| 190 | main.log.info(str(ue)) |
| 191 | UP4.buildGtpPacket(enb["host"], |
| 192 | src_ip_outer=enb["enb_address"], |
| 193 | dst_ip_outer=self.s1u_address, |
| 194 | src_ip_inner=ue["ue_address"], |
| 195 | dst_ip_inner=self.pdn_interface["ips"][0], |
| 196 | src_udp_inner=UE_PORT, |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 197 | dst_udp_inner=pdn_port, |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 198 | teid=int(ue["teid"])) |
| 199 | enb["host"].sendPacket(iface=enb["interface"]) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 200 | |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 201 | packets = UP4.checkFilterAndGetPackets(self.pdn_host) |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 202 | if app_filter_should_drop: |
| 203 | expected_pkt_count = 0 |
| 204 | else: |
| 205 | # We expect exactly 1 packet per UE. |
| 206 | expected_pkt_count = len(ues) |
| 207 | actual_pkt_count = packets.count('Ether') |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 208 | fail = False |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 209 | if expected_pkt_count != actual_pkt_count: |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 210 | fail = True |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 211 | msg = "Received %d packets (expected %d)\n%s\n" % ( |
| 212 | actual_pkt_count, expected_pkt_count, str(packets) |
| 213 | ) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 214 | else: |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 215 | msg = "Received %d packets (expected %d)\n" % ( |
| 216 | actual_pkt_count, expected_pkt_count |
| 217 | ) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 218 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 219 | if expected_pkt_count > 0: |
| 220 | # Make sure the captured packets are from the expected UE addresses. |
| 221 | for ue in ues: |
| 222 | ue_pkt_count = packets.count("src=" + ue["ue_address"]) |
| 223 | if ue_pkt_count != 1: |
| 224 | fail = True |
| 225 | msg += "Received %d packet(s) from UE %s (expected 1)\n" % ( |
| 226 | ue_pkt_count, ue["ue_address"] |
| 227 | ) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 228 | utilities.assert_equal( |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 229 | expect=shouldFail, actual=fail, onpass=msg, onfail=msg |
| 230 | ) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 231 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 232 | def testDownstreamTraffic(self, enb_names=None, app_filter_name=None, shouldFail=False): |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 233 | if self.enodebs is None or self.pdn_host is None: |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 234 | main.log.error( |
| 235 | "Need eNodeB and PDN host params to generate scapy traffic") |
| 236 | return |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 237 | if enb_names is None or enb_names == []: |
| 238 | enodebs = self.enodebs.values() |
| 239 | else: |
| 240 | enodebs = [self.enodebs[enb] for enb in enb_names] |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 241 | |
| 242 | app_filter = self.app_filters[app_filter_name] |
| 243 | pdn_port = self.__pickPortFromRange(app_filter.get("port_range", None)) |
| 244 | app_filter_should_drop = app_filter["action"] != "allow" |
| 245 | |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 246 | pkt_filter_downstream = "ip and udp src port %d and udp dst port %d and src host %s" % ( |
| 247 | GPDU_PORT, GPDU_PORT, self.s1u_address) |
| 248 | ues = [] |
| 249 | for enb in enodebs: |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 250 | filter_down = pkt_filter_downstream + " and dst host %s" % enb[ |
| 251 | "enb_address"] |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 252 | main.log.info("Start listening on %s intf %s" % ( |
| 253 | enb["host"], enb["interface"])) |
| 254 | main.log.debug("BPF Filter Downstream: \n %s" % filter_down) |
| 255 | enb["host"].startFilter(ifaceName=enb["interface"], |
| 256 | sniffCount=len(enb["ues"]), |
| 257 | pktFilter=filter_down) |
| 258 | ues.extend([self.emulated_ues[ue_name] for ue_name in enb["ues"]]) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 259 | |
| 260 | main.log.info( |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 261 | "Sending %d packets from PDN host" % len(ues)) |
| 262 | for ue in ues: |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 263 | # From PDN we have to set dest MAC, otherwise scapy will do ARP |
| 264 | # request for the UE IP address. |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 265 | UP4.buildUdpPacket(self.pdn_host, |
| 266 | dst_eth=self.router_mac, |
| 267 | src_ip=self.pdn_interface["ips"][0], |
| 268 | dst_ip=ue["ue_address"], |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 269 | src_udp=pdn_port, |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 270 | dst_udp=UE_PORT) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 271 | self.pdn_host.sendPacket(iface=self.pdn_interface["name"]) |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 272 | packets = "" |
| 273 | for enb in enodebs: |
| 274 | pkt = UP4.checkFilterAndGetPackets(enb["host"]) |
| 275 | packets += pkt |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 276 | # The BPF filter might capture non-GTP packets because we can't filter |
| 277 | # GTP header in BPF. For this reason, check that the captured packets |
| 278 | # are from the expected tunnels. |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 279 | # TODO: check inner IP fields as well |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 280 | # FIXME: with newer scapy TEID becomes teid (required for Scapy 2.4.5) |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 281 | downlink_teids = [int(ue["teid"]) + 1 for ue in ues] |
| 282 | # Number of GTP packets from expected TEID per UEs |
| 283 | gtp_pkts = [ |
| 284 | packets.count("TEID=" + hex(teid) + "L ") for teid in downlink_teids |
| 285 | ] |
| 286 | # Number of packets from the expected PDN port |
| 287 | app_pkts = packets.count("UDP sport=" + str(pdn_port)) |
| 288 | if app_filter_should_drop: |
| 289 | expected_pkt_count = 0 |
| 290 | else: |
| 291 | # We expect exactly 1 packet per UE. |
| 292 | expected_pkt_count = len(ues) |
| 293 | |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 294 | fail = False |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 295 | if expected_pkt_count != sum(gtp_pkts): |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 296 | fail = True |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 297 | msg = "Received %d packets (expected %d) from TEIDs %s\n%s\n" % ( |
| 298 | sum(gtp_pkts), expected_pkt_count, downlink_teids, str(packets) |
| 299 | ) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 300 | else: |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 301 | msg = "Received %d packets (expected %d) from TEIDs %s\n" % ( |
| 302 | sum(gtp_pkts), expected_pkt_count, downlink_teids |
| 303 | ) |
| 304 | if expected_pkt_count != app_pkts: |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 305 | fail = True |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 306 | msg += "Received %d packets (expected %d) from PDN port %s\n%s\n" % ( |
| 307 | sum(gtp_pkts), expected_pkt_count, pdn_port, str(packets) |
| 308 | ) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 309 | else: |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 310 | msg += "Received %d packets (expected %d) from PDN port %s\n" % ( |
| 311 | sum(gtp_pkts), expected_pkt_count, pdn_port |
| 312 | ) |
| 313 | if expected_pkt_count > 0: |
| 314 | if gtp_pkts.count(1) != len(gtp_pkts): |
| 315 | fail = True |
| 316 | msg += "Received %s packet(s) per UE (expected %s)\n%s\n" % ( |
| 317 | gtp_pkts, [1] * len(gtp_pkts), packets |
| 318 | ) |
| 319 | else: |
| 320 | msg += "Received %s packet(s) per UE (expected %s)\n" % ( |
| 321 | gtp_pkts, [1] * len(gtp_pkts) |
| 322 | ) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 323 | |
| 324 | utilities.assert_equal( |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 325 | expect=shouldFail, actual=fail, onpass=msg, onfail=msg |
| 326 | ) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 327 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 328 | def readUeSessionsNumber(self): |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 329 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 330 | Read the UE session tables and return the number of entries |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 331 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 332 | :return: Number of entries in the UE session tables |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 333 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 334 | tableName = 'PreQosPipe.sessions_uplink' |
| 335 | nUeSess = self.up4_client.readNumberTableEntries(tableName) |
| 336 | tableName = 'PreQosPipe.sessions_downlink' |
| 337 | nUeSess += self.up4_client.readNumberTableEntries(tableName) |
| 338 | return nUeSess |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 339 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 340 | def readTerminationsNumber(self): |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 341 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 342 | Read the terminations and return the number of entities |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 343 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 344 | :return: Number of terminations entities |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 345 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 346 | tableName = 'PreQosPipe.terminations_uplink' |
| 347 | nTerm = self.up4_client.readNumberTableEntries(tableName) |
| 348 | tableName = 'PreQosPipe.terminations_downlink' |
| 349 | nTerm += self.up4_client.readNumberTableEntries(tableName) |
| 350 | return nTerm |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 351 | |
| 352 | def verifyUesFlowNumberP4rt(self): |
| 353 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 354 | Verify via P4RT CLI that the number of UE sessions and terminations |
| 355 | is the expected one |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 356 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 357 | :return: True if the number of UE sessions and terminations is expected, |
| 358 | False otherwise |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 359 | """ |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 360 | return self.readUeSessionsNumber() == len(self.emulated_ues) * 2 and \ |
| 361 | self.readTerminationsNumber() == len(self.emulated_ues) * 2 * len(self.app_filters) |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 362 | |
| 363 | def verifyNoUesFlowNumberP4rt(self, preInstalledUes=0): |
| 364 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 365 | Verify via P4RT CLI that there is no UE sessions and terminations installed. |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 366 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 367 | :param preInstalledUes: Number of UEs whose UE sessions and terminations |
| 368 | are still programmed |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 369 | :return: |
| 370 | """ |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 371 | return self.readUeSessionsNumber() == preInstalledUes * 2 and \ |
| 372 | self.readTerminationsNumber() == preInstalledUes * 2 * len(self.app_filters) |
Daniele Moro | 954e228 | 2021-09-22 17:32:03 +0200 | [diff] [blame] | 373 | |
Daniele Moro | 6dfbfef | 2021-09-28 22:44:19 +0200 | [diff] [blame] | 374 | def verifyNoUesFlow(self, onosCli, retries=10): |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 375 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 376 | Verify that no UE session, terminations are installed in ONOS. |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 377 | |
| 378 | :param onosCli: An instance of a OnosCliDriver |
| 379 | :param retries: number of retries |
| 380 | :return: |
| 381 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 382 | retValue = utilities.retry(f=UP4.__verifyNoUeSessionAndTerminationOnos, |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 383 | retValue=False, |
| 384 | args=[onosCli], |
Daniele Moro | 6dfbfef | 2021-09-28 22:44:19 +0200 | [diff] [blame] | 385 | sleep=5, |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 386 | attempts=retries) |
| 387 | utilities.assert_equal(expect=True, |
| 388 | actual=retValue, |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 389 | onpass="No UE session and terminations in ONOS", |
| 390 | onfail="Stale UE session or terminations") |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 391 | |
| 392 | @staticmethod |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 393 | def __verifyNoUeSessionAndTerminationOnos(onosCli): |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 394 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 395 | Verify that no UE session, terminations are installed in ONOS |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 396 | |
| 397 | :param onosCli: An instance of a OnosCliDriver |
| 398 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 399 | sessions = onosCli.sendline(cmdStr="up4:read-entities -s", |
| 400 | showResponse=True, |
| 401 | noExit=True, expectJson=False) |
| 402 | terminations = onosCli.sendline(cmdStr="up4:read-entities -t", |
| 403 | showResponse=True, |
| 404 | noExit=True, expectJson=False) |
| 405 | return sessions == "" and terminations == "" |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 406 | |
Daniele Moro | c6811a8 | 2021-10-12 11:29:41 +0200 | [diff] [blame] | 407 | def verifyUp4Flow(self, onosCli, retries=10): |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 408 | """ |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 409 | Verify UE session, terminations and GTP tunnel peers installed via UP4 |
| 410 | using the ONOS CLI. |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 411 | |
| 412 | :param onosCli: An instance of a OnosCliDriver |
Daniele Moro | c6811a8 | 2021-10-12 11:29:41 +0200 | [diff] [blame] | 413 | :param retries: Number of retries |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 414 | """ |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 415 | failString = [] |
Daniele Moro | c6811a8 | 2021-10-12 11:29:41 +0200 | [diff] [blame] | 416 | retValue = utilities.retry(f=self.__internalVerifyUp4Flow, |
| 417 | retValue=False, |
| 418 | args=[onosCli, failString], |
| 419 | sleep=5, |
| 420 | attempts=retries) |
| 421 | utilities.assert_equal( |
| 422 | expect=True, |
| 423 | actual=retValue, |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 424 | onpass="Correct UE session, terminations and GTP tunnel peers in ONOS", |
Jon Hall | a601dde | 2022-02-28 14:22:07 -0800 | [diff] [blame] | 425 | onfail="Wrong Application Filters, UE sessions, terminations and/or GTP tunnel peers in ONOS. " + |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 426 | "Missing:\n" + '\n'.join(failString) |
Daniele Moro | c6811a8 | 2021-10-12 11:29:41 +0200 | [diff] [blame] | 427 | ) |
| 428 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 429 | def __internalVerifyUp4Flow(self, onosCli, failMsg=[]): |
| 430 | # Need to pass a list, so it's an object and we can use failMsg to |
| 431 | # return a string values from this method. |
| 432 | |
| 433 | # Cleanup failMsg if any remaining from previous runs |
| 434 | del failMsg[:] |
| 435 | applications = onosCli.sendline(cmdStr="up4:read-entities -f", |
| 436 | showResponse=True, |
| 437 | noExit=True, expectJson=False) |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 438 | sessions = onosCli.sendline(cmdStr="up4:read-entities -s", |
| 439 | showResponse=True, |
| 440 | noExit=True, expectJson=False) |
| 441 | terminations = onosCli.sendline(cmdStr="up4:read-entities -t", |
| 442 | showResponse=True, |
| 443 | noExit=True, expectJson=False) |
| 444 | tunn_peer = onosCli.sendline(cmdStr="up4:read-entities -g", |
| 445 | showResponse=True, |
| 446 | noExit=True, expectJson=False) |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 447 | fail = False |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 448 | for app_filter in self.app_filters.values(): |
| 449 | if not UP4.__defaultApp(**app_filter): |
| 450 | if applications.count(self.appFilterOnosString(**app_filter)) != 1: |
| 451 | failMsg.append(self.appFilterOnosString(**app_filter)) |
| 452 | fail = True |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 453 | for (ue_name, ue) in self.emulated_ues.items(): |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 454 | if sessions.count(self.upUeSessionOnosString(**ue)) != 1: |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 455 | failMsg.append(self.upUeSessionOnosString(**ue)) |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 456 | fail = True |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 457 | if sessions.count(self.downUeSessionOnosString(**ue)) != 1: |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 458 | failMsg.append(self.downUeSessionOnosString(**ue)) |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 459 | fail = True |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 460 | for app_filter in self.app_filters.values(): |
| 461 | if terminations.count(self.upTerminationOnosString(app_filter=app_filter, **ue)) != 1: |
| 462 | failMsg.append(self.upTerminationOnosString(app_filter=app_filter, **ue)) |
| 463 | fail = True |
| 464 | if terminations.count(self.downTerminationOnosString(app_filter=app_filter, **ue)) != 1: |
| 465 | failMsg.append(self.downTerminationOnosString(app_filter=app_filter, **ue)) |
| 466 | fail = True |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 467 | if tunn_peer.count(self.gtpTunnelPeerOnosString(ue_name, **ue)) != 1: |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 468 | failMsg.append(self.gtpTunnelPeerOnosString(ue_name, **ue)) |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 469 | fail = True |
Daniele Moro | c6811a8 | 2021-10-12 11:29:41 +0200 | [diff] [blame] | 470 | return not fail |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 471 | |
Jon Hall | a601dde | 2022-02-28 14:22:07 -0800 | [diff] [blame] | 472 | def appFilterOnosString(self, app_id, priority, slice_id=None, ip_proto=None, ip_prefix=None, port_range=None, **kwargs): |
| 473 | if slice_id is None: |
| 474 | slice_id = self.slice_id |
| 475 | matches = [] |
| 476 | if ip_prefix: |
| 477 | matches.append("ip_prefix=%s" % ip_prefix) |
| 478 | if port_range: |
| 479 | matches.append("l4_port_range=[%s]" % port_range) |
| 480 | if ip_proto: |
| 481 | matches.append("ip_proto=%s" % ip_proto) |
| 482 | matches.append("slice_id=%s" % slice_id) |
| 483 | |
| 484 | return "UpfApplication(priority=%s, Match(%s) -> Action(app_id=%s))" % ( |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 485 | priority, |
Jon Hall | a601dde | 2022-02-28 14:22:07 -0800 | [diff] [blame] | 486 | ", ".join(matches), |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 487 | app_id |
| 488 | ) |
| 489 | |
Daniele Moro | 4650ffb | 2022-02-15 22:44:18 +0100 | [diff] [blame] | 490 | def upUeSessionOnosString(self, teid=None, teid_up=None, sess_meter_idx=DEFAULT_SESSION_METER_IDX, **kwargs): |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 491 | if teid is not None: |
| 492 | teid_up = teid |
Daniele Moro | 4650ffb | 2022-02-15 22:44:18 +0100 | [diff] [blame] | 493 | return "UpfSessionUL(Match(tun_dst_addr={}, teid={}) -> Action(FWD, session_meter_idx={}))".format( |
| 494 | self.s1u_address, teid_up, sess_meter_idx) |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 495 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 496 | def downUeSessionOnosString(self, ue_address, down_id=None, |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 497 | tunn_peer_id=None, sess_meter_idx=DEFAULT_SESSION_METER_IDX, |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 498 | **kwargs): |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 499 | if down_id is not None: |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 500 | tunn_peer_id = down_id |
Daniele Moro | 4650ffb | 2022-02-15 22:44:18 +0100 | [diff] [blame] | 501 | return "UpfSessionDL(Match(ue_addr={}) -> Action(FWD, tun_peer={}, session_meter_idx={}))".format( |
| 502 | ue_address, tunn_peer_id, sess_meter_idx) |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 503 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 504 | def upTerminationOnosString(self, ue_address, app_filter, up_id=None, |
Daniele Moro | 4650ffb | 2022-02-15 22:44:18 +0100 | [diff] [blame] | 505 | ctr_id_up=None, tc=None, app_meter_idx=DEFAULT_APP_METER_IDX, **kwargs): |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 506 | if up_id is not None: |
| 507 | ctr_id_up = up_id |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 508 | if app_filter["action"] == "allow": |
| 509 | return "UpfTerminationUL(Match(ue_addr={}, app_id={}) -> Action(FWD, ctr_id={}, tc={}, app_meter_idx={}))".format( |
| 510 | ue_address, app_filter["app_id"], ctr_id_up, tc, app_meter_idx) |
| 511 | else: |
| 512 | return "UpfTerminationUL(Match(ue_addr={}, app_id={}) -> Action(DROP, ctr_id={}, tc=null, app_meter_idx=0))".format( |
| 513 | ue_address, app_filter["app_id"], ctr_id_up) |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 514 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 515 | def downTerminationOnosString(self, ue_address, app_filter, teid=None, |
Daniele Moro | 1f7ca6f | 2022-01-27 11:58:50 +0100 | [diff] [blame] | 516 | down_id=None, ctr_id_down=None, teid_down=None, |
Daniele Moro | 4650ffb | 2022-02-15 22:44:18 +0100 | [diff] [blame] | 517 | tc=None, app_meter_idx=DEFAULT_APP_METER_IDX, |
| 518 | **kwargs): |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 519 | if down_id is not None: |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 520 | ctr_id_down = down_id |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 521 | if teid is not None: |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 522 | teid_down = int(teid) + 1 |
| 523 | if tc is None: |
| 524 | tc="null" |
| 525 | if app_filter["action"] == "allow": |
| 526 | return "UpfTerminationDL(Match(ue_addr={}, app_id={}) -> Action(FWD, teid={}, ctr_id={}, qfi={}, tc={}, app_meter_idx={}))".format( |
| 527 | ue_address, app_filter["app_id"], teid_down, ctr_id_down, tc, tc, app_meter_idx) |
| 528 | else: |
| 529 | return "UpfTerminationDL(Match(ue_addr={}, app_id={}) -> Action(DROP, teid=null, ctr_id={}, qfi=null, tc=null, app_meter_idx=0))".format( |
| 530 | ue_address, app_filter["app_id"], ctr_id_down) |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 531 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 532 | def gtpTunnelPeerOnosString(self, ue_name, down_id=None, tunn_peer_id=None, |
| 533 | **kwargs): |
| 534 | if down_id is not None: |
| 535 | tunn_peer_id = down_id |
| 536 | enb_address = self.__getEnbAddress(ue_name) |
Daniele Moro | 1f7ca6f | 2022-01-27 11:58:50 +0100 | [diff] [blame] | 537 | return "UpfGtpTunnelPeer(tunn_peer_id={} -> src={}, dst={} src_port={})".format( |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 538 | tunn_peer_id, self.s1u_address, enb_address, GPDU_PORT) |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 539 | |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 540 | @staticmethod |
| 541 | def __sanitizeUeData(ue): |
Daniele Moro | 249d6e7 | 2021-09-20 10:32:54 +0200 | [diff] [blame] | 542 | if "five_g" in ue and type(ue["five_g"]) != bool: |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 543 | ue["five_g"] = bool(strtobool(ue["five_g"])) |
Carmelo Cascone | 848d1f5 | 2022-01-27 18:15:58 -0800 | [diff] [blame] | 544 | if "tc" in ue and ue["tc"] == "": |
| 545 | ue["tc"] = 0 |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 546 | return ue |
| 547 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 548 | def insertAppFilter(self, **kwargs): |
| 549 | if not UP4.__defaultApp(**kwargs): |
| 550 | self.__programAppFilter(op="program", **kwargs) |
| 551 | |
| 552 | def removeAppFilter(self, **kwargs): |
| 553 | if not UP4.__defaultApp(**kwargs): |
| 554 | self.__programAppFilter(op="clear", **kwargs) |
| 555 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 556 | def attachUe(self, ue_name, ue_address, |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 557 | teid=None, up_id=None, down_id=None, |
| 558 | teid_up=None, teid_down=None, |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 559 | ctr_id_up=None, ctr_id_down=None, |
| 560 | tunn_peer_id=None, |
Carmelo Cascone | 848d1f5 | 2022-01-27 18:15:58 -0800 | [diff] [blame] | 561 | tc=None, five_g=False): |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 562 | self.__programUeRules(ue_name, |
| 563 | ue_address, |
| 564 | teid, up_id, down_id, |
| 565 | teid_up, teid_down, |
| 566 | ctr_id_up, ctr_id_down, |
| 567 | tunn_peer_id, |
| 568 | tc, five_g, op="program") |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 569 | |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 570 | def detachUe(self, ue_name, ue_address, |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 571 | teid=None, up_id=None, down_id=None, |
| 572 | teid_up=None, teid_down=None, |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 573 | ctr_id_up=None, ctr_id_down=None, |
| 574 | tunn_peer_id=None, |
Carmelo Cascone | 848d1f5 | 2022-01-27 18:15:58 -0800 | [diff] [blame] | 575 | tc=None, five_g=False): |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 576 | self.__programUeRules(ue_name, |
| 577 | ue_address, |
| 578 | teid, up_id, down_id, |
| 579 | teid_up, teid_down, |
| 580 | ctr_id_up, ctr_id_down, |
| 581 | tunn_peer_id, |
| 582 | tc, five_g, op="clear") |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 583 | |
Jon Hall | a601dde | 2022-02-28 14:22:07 -0800 | [diff] [blame] | 584 | def __programAppFilter(self, app_id, slice_id, ip_prefix=None, ip_proto=None, |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 585 | port_range=None, priority=0, op="program", **kwargs): |
| 586 | |
| 587 | entries = [] |
| 588 | |
| 589 | tableName = 'PreQosPipe.applications' |
| 590 | actionName = 'PreQosPipe.set_app_id' |
| 591 | actionParams = {'app_id': str(app_id)} |
Daniele Moro | cc4ecda | 2022-02-25 23:27:25 +0100 | [diff] [blame] | 592 | matchFields = { |
Jon Hall | a601dde | 2022-02-28 14:22:07 -0800 | [diff] [blame] | 593 | 'slice_id': str(slice_id) |
Daniele Moro | cc4ecda | 2022-02-25 23:27:25 +0100 | [diff] [blame] | 594 | } |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 595 | if ip_prefix: |
| 596 | matchFields['app_ip_addr'] = str(ip_prefix) |
| 597 | if ip_proto: |
| 598 | matchFields['app_ip_proto'] = str(ip_proto) |
| 599 | if port_range: |
| 600 | matchFields['app_l4_port'] = str(port_range) |
| 601 | |
| 602 | if not self.__add_entry(tableName, actionName, matchFields, |
| 603 | actionParams, entries, op, priority): |
| 604 | return False |
| 605 | |
| 606 | if op == "program": |
| 607 | main.log.info("Application entry added successfully.") |
| 608 | elif op == "clear": |
| 609 | self.__clear_entries(entries) |
| 610 | |
| 611 | def __programUeRules(self, ue_name, ue_address, |
| 612 | teid=None, up_id=None, down_id=None, |
| 613 | teid_up=None, teid_down=None, ctr_id_up=None, |
| 614 | ctr_id_down=None, tunn_peer_id=None, |
| 615 | tc=0, five_g=False, |
| 616 | op="program"): |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 617 | if up_id is not None: |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 618 | ctr_id_up = up_id |
| 619 | if down_id is not None: |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 620 | tunn_peer_id = down_id |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 621 | ctr_id_down = down_id |
| 622 | if teid is not None: |
| 623 | teid_up = teid |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 624 | teid_down = int(teid) + 1 |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 625 | |
| 626 | entries = [] |
| 627 | |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 628 | # Retrieve eNobeB address from eNodeB list |
| 629 | enb_address = self.__getEnbAddress(ue_name) |
| 630 | |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 631 | # ========================# |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 632 | # UE Session Entries |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 633 | # ========================# |
| 634 | |
| 635 | # Uplink |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 636 | tableName = 'PreQosPipe.sessions_uplink' |
| 637 | actionName = 'PreQosPipe.set_session_uplink' |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 638 | matchFields = {} |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 639 | actionParams = {} |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 640 | # Match fields |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 641 | matchFields['n3_address'] = str(self.s1u_address) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 642 | matchFields['teid'] = str(teid_up) |
Daniele Moro | 4650ffb | 2022-02-15 22:44:18 +0100 | [diff] [blame] | 643 | # Action params |
| 644 | actionParams["session_meter_idx"] = str(DEFAULT_SESSION_METER_IDX) |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 645 | if five_g: |
| 646 | # TODO: currently QFI match is unsupported in TNA |
| 647 | main.log.warn("Matching on QFI is currently unsupported in TNA") |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 648 | if not self.__add_entry(tableName, actionName, matchFields, |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 649 | actionParams, entries, op): |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 650 | return False |
| 651 | |
| 652 | # Downlink |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 653 | tableName = 'PreQosPipe.sessions_downlink' |
| 654 | actionName = 'PreQosPipe.set_session_downlink' |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 655 | matchFields = {} |
| 656 | actionParams = {} |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 657 | # Match fields |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 658 | matchFields['ue_address'] = str(ue_address) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 659 | # Action params |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 660 | actionParams['tunnel_peer_id'] = str(tunn_peer_id) |
Daniele Moro | 4650ffb | 2022-02-15 22:44:18 +0100 | [diff] [blame] | 661 | actionParams["session_meter_idx"] = str(DEFAULT_SESSION_METER_IDX) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 662 | if not self.__add_entry(tableName, actionName, matchFields, |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 663 | actionParams, entries, op): |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 664 | return False |
| 665 | |
| 666 | # ========================# |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 667 | # Terminations Entries |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 668 | # ========================# |
| 669 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 670 | # Insert one termination entry per app filtering rule, |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 671 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 672 | # Uplink |
| 673 | for f in self.app_filters.values(): |
| 674 | tableName = 'PreQosPipe.terminations_uplink' |
| 675 | matchFields = {} |
| 676 | actionParams = {} |
| 677 | |
| 678 | # Match fields |
| 679 | matchFields['ue_address'] = str(ue_address) |
| 680 | matchFields['app_id'] = str(f["app_id"]) |
| 681 | |
| 682 | # Action params |
| 683 | if f['action'] == 'allow': |
| 684 | actionName = 'PreQosPipe.uplink_term_fwd' |
| 685 | actionParams['app_meter_idx'] = str(DEFAULT_APP_METER_IDX) |
| 686 | actionParams['tc'] = str(tc) |
| 687 | else: |
| 688 | actionName = 'PreQosPipe.uplink_term_drop' |
| 689 | actionParams['ctr_idx'] = str(ctr_id_up) |
| 690 | if not self.__add_entry( |
| 691 | tableName, actionName, matchFields, actionParams, entries, op |
| 692 | ): |
| 693 | return False |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 694 | |
| 695 | # Downlink |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 696 | for f in self.app_filters.values(): |
| 697 | tableName = 'PreQosPipe.terminations_downlink' |
| 698 | matchFields = {} |
| 699 | actionParams = {} |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 700 | |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 701 | # Match fields |
| 702 | matchFields['ue_address'] = str(ue_address) |
| 703 | matchFields['app_id'] = str(f["app_id"]) |
| 704 | |
| 705 | # Action params |
| 706 | if f['action'] == 'allow': |
| 707 | actionName = 'PreQosPipe.downlink_term_fwd' |
| 708 | actionParams['teid'] = str(teid_down) |
| 709 | # 1-1 mapping between QFI and TC |
| 710 | actionParams['tc'] = str(tc) |
| 711 | actionParams['qfi'] = str(tc) |
| 712 | actionParams['app_meter_idx'] = str(DEFAULT_APP_METER_IDX) |
| 713 | else: |
| 714 | actionName = 'PreQosPipe.downlink_term_drop' |
| 715 | actionParams['ctr_idx'] = str(ctr_id_down) |
| 716 | |
| 717 | if not self.__add_entry(tableName, actionName, matchFields, |
| 718 | actionParams, entries, op): |
| 719 | return False |
Daniele Moro | 49a843c | 2022-01-05 14:36:32 +0100 | [diff] [blame] | 720 | |
| 721 | # ========================# |
| 722 | # Tunnel Peer Entry |
| 723 | # ========================# |
| 724 | tableName = 'PreQosPipe.tunnel_peers' |
| 725 | actionName = 'PreQosPipe.load_tunnel_param' |
| 726 | matchFields = {} |
| 727 | actionParams = {} |
| 728 | # Match fields |
| 729 | matchFields['tunnel_peer_id'] = str(tunn_peer_id) |
| 730 | # Action params |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 731 | actionParams['src_addr'] = str(self.s1u_address) |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 732 | actionParams['dst_addr'] = str(enb_address) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 733 | actionParams['sport'] = TUNNEL_SPORT |
| 734 | if not self.__add_entry(tableName, actionName, matchFields, |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 735 | actionParams, entries, op): |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 736 | return False |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 737 | if op == "program": |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 738 | main.log.info("All entries added successfully.") |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 739 | elif op == "clear": |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 740 | self.__clear_entries(entries) |
| 741 | |
| 742 | def __add_entry(self, tableName, actionName, matchFields, actionParams, |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 743 | entries, op, priority=0): |
| 744 | if op == "program": |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 745 | self.up4_client.buildP4RtTableEntry( |
| 746 | tableName=tableName, actionName=actionName, |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 747 | actionParams=actionParams, matchFields=matchFields, priority=priority) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 748 | if self.up4_client.pushTableEntry(debug=True) == main.TRUE: |
| 749 | main.log.info("*** Entry added.") |
| 750 | else: |
| 751 | main.log.error("Error during table insertion") |
| 752 | self.__clear_entries(entries) |
| 753 | return False |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 754 | entries.append({ |
| 755 | "tableName": tableName, "actionName": actionName, |
| 756 | "matchFields": matchFields, |
| 757 | "actionParams": actionParams, |
| 758 | "priority": priority |
| 759 | }) |
Daniele Moro | 8088956 | 2021-09-08 10:09:26 +0200 | [diff] [blame] | 760 | return True |
| 761 | |
| 762 | def __clear_entries(self, entries): |
| 763 | for i, entry in enumerate(entries): |
| 764 | self.up4_client.buildP4RtTableEntry(**entry) |
| 765 | if self.up4_client.deleteTableEntry(debug=True) == main.TRUE: |
| 766 | main.log.info( |
| 767 | "*** Entry %d of %d deleted." % (i + 1, len(entries))) |
| 768 | else: |
| 769 | main.log.error("Error during table delete") |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 770 | |
Daniele Moro | 522023c | 2021-10-15 17:30:33 +0200 | [diff] [blame] | 771 | def __getEnbAddress(self, ue_name): |
| 772 | for enb in self.enodebs.values(): |
| 773 | if ue_name in enb["ues"]: |
| 774 | return enb["enb_address"] |
| 775 | main.log.error("Missing eNodeB address!") |
| 776 | return "" |
| 777 | |
Daniele Moro | bf53dec | 2021-09-13 18:11:56 +0200 | [diff] [blame] | 778 | @staticmethod |
| 779 | def buildGtpPacket(host, src_ip_outer, dst_ip_outer, src_ip_inner, |
| 780 | dst_ip_inner, src_udp_inner, dst_udp_inner, teid): |
| 781 | host.buildEther() |
| 782 | host.buildIP(src=src_ip_outer, dst=dst_ip_outer) |
| 783 | host.buildUDP(ipVersion=4, dport=GPDU_PORT) |
| 784 | # FIXME: With newer scapy TEID becomes teid (required for Scapy 2.4.5) |
| 785 | host.buildGTP(gtp_type=0xFF, TEID=teid) |
| 786 | host.buildIP(overGtp=True, src=src_ip_inner, dst=dst_ip_inner) |
| 787 | host.buildUDP(ipVersion=4, overGtp=True, sport=src_udp_inner, |
| 788 | dport=dst_udp_inner) |
| 789 | |
| 790 | @staticmethod |
| 791 | def buildUdpPacket(host, src_ip, dst_ip, src_udp, dst_udp, src_eth=None, |
| 792 | dst_eth=None): |
| 793 | host.buildEther(src=src_eth, dst=dst_eth) |
| 794 | host.buildIP(src=src_ip, dst=dst_ip) |
| 795 | host.buildUDP(ipVersion=4, sport=src_udp, dport=dst_udp) |
| 796 | |
| 797 | @staticmethod |
| 798 | def checkFilterAndGetPackets(host): |
| 799 | finished = host.checkFilter() |
| 800 | if finished: |
| 801 | packets = host.readPackets(detailed=True) |
| 802 | for p in packets.splitlines(): |
| 803 | main.log.debug(p) |
| 804 | # We care only of the last line from readPackets |
| 805 | return packets.splitlines()[-1] |
| 806 | else: |
| 807 | kill = host.killFilter() |
| 808 | main.log.debug(kill) |
| 809 | return "" |
Daniele Moro | bef0c7e | 2022-02-16 17:47:13 -0800 | [diff] [blame] | 810 | |
| 811 | @staticmethod |
| 812 | def __defaultApp(ip_prefix=None, ip_proto=None, port_range=None, **kwargs): |
| 813 | if ip_prefix is None and ip_proto is None and port_range is None: |
| 814 | return True |
| 815 | return False |
| 816 | |