Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef __SPGW__ |
| 18 | #define __SPGW__ |
| 19 | |
| 20 | |
| 21 | control spgw_ingress( |
| 22 | inout ipv4_t gtpu_ipv4, |
| 23 | inout udp_t gtpu_udp, |
| 24 | inout gtpu_t gtpu, |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 25 | inout ipv4_t ipv4, |
| 26 | inout udp_t udp, |
| 27 | inout spgw_meta_t spgw_meta |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 28 | ) { |
| 29 | |
| 30 | direct_counter(CounterType.packets_and_bytes) ue_counter; |
| 31 | |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 32 | action drop_now() { |
| 33 | mark_to_drop(); |
| 34 | exit; |
| 35 | } |
| 36 | |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 37 | action gtpu_decap() { |
| 38 | gtpu_ipv4.setInvalid(); |
| 39 | gtpu_udp.setInvalid(); |
| 40 | gtpu.setInvalid(); |
| 41 | } |
| 42 | |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 43 | action set_dl_sess_info(bit<32> teid, |
| 44 | bit<32> s1u_enb_addr, |
| 45 | bit<32> s1u_sgw_addr) { |
| 46 | spgw_meta.teid = teid; |
| 47 | spgw_meta.s1u_enb_addr = s1u_enb_addr; |
| 48 | spgw_meta.s1u_sgw_addr = s1u_sgw_addr; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | action update_ue_cdr() { |
| 52 | ue_counter.count(); |
| 53 | } |
| 54 | |
| 55 | table ue_filter_table { |
| 56 | key = { |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 57 | // IP prefixes of the UEs managed by this switch (when downlink) |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 58 | ipv4.dst_addr : lpm; |
| 59 | } |
| 60 | actions = { |
| 61 | NoAction(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | table s1u_filter_table { |
| 66 | key = { |
| 67 | // IP addresses of the S1U interfaces embodied by this switch. |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 68 | spgw_meta.s1u_sgw_addr : exact; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 69 | } |
| 70 | actions = { |
| 71 | NoAction(); |
| 72 | } |
| 73 | } |
| 74 | |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 75 | #ifdef WITH_SPGW_PCC_GATING |
| 76 | action set_sdf_rule_id(sdf_rule_id_t id) { |
| 77 | spgw_meta.sdf_rule_id = id; |
| 78 | } |
| 79 | |
| 80 | action set_pcc_rule_id(pcc_rule_id_t id) { |
| 81 | spgw_meta.pcc_rule_id = id; |
| 82 | } |
| 83 | |
| 84 | action set_pcc_info(pcc_gate_status_t gate_status) { |
| 85 | spgw_meta.pcc_gate_status = gate_status; |
| 86 | } |
| 87 | |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 88 | table sdf_rule_lookup { |
| 89 | key = { |
| 90 | spgw_meta.direction : exact; |
| 91 | ipv4.src_addr : ternary; |
| 92 | ipv4.dst_addr : ternary; |
| 93 | ipv4.protocol : ternary; |
| 94 | spgw_meta.l4_src_port : ternary; |
| 95 | spgw_meta.l4_dst_port : ternary; |
| 96 | } |
| 97 | actions = { |
| 98 | set_sdf_rule_id(); |
| 99 | } |
| 100 | const default_action = set_sdf_rule_id(DEFAULT_SDF_RULE_ID); |
| 101 | } |
| 102 | |
| 103 | table pcc_rule_lookup { |
| 104 | key = { |
| 105 | spgw_meta.sdf_rule_id : exact; |
| 106 | } |
| 107 | actions = { |
| 108 | set_pcc_rule_id(); |
| 109 | } |
| 110 | const default_action = set_pcc_rule_id(DEFAULT_PCC_RULE_ID); |
| 111 | } |
| 112 | |
| 113 | table pcc_info_lookup { |
| 114 | key = { |
| 115 | spgw_meta.pcc_rule_id : exact; |
| 116 | } |
| 117 | actions = { |
| 118 | set_pcc_info(); |
| 119 | } |
| 120 | const default_action = set_pcc_info(PCC_GATE_OPEN); |
| 121 | } |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 122 | #endif // WITH_SPGW_PCC_GATING |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 123 | |
| 124 | table dl_sess_lookup { |
| 125 | key = { |
| 126 | // UE addr for downlink |
| 127 | ipv4.dst_addr : exact; |
| 128 | } |
| 129 | actions = { |
| 130 | set_dl_sess_info(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | table ue_cdr_table { |
| 135 | key = { |
| 136 | // UE addr for downlink |
| 137 | ipv4.dst_addr : exact; |
| 138 | } |
| 139 | actions = { |
| 140 | update_ue_cdr(); |
| 141 | } |
| 142 | counters = ue_counter; |
| 143 | } |
| 144 | |
| 145 | apply { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 146 | spgw_meta.do_spgw = false; |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 147 | if (gtpu.isValid()) { |
| 148 | // If here, the parsed ipv4 header is the outer GTP one, but |
| 149 | // fabric needs to forward on the inner one, i.e. this. |
| 150 | // We store the outer values we need in the metadata, then replace |
| 151 | // the ipv4 header extracted before with this one. |
| 152 | spgw_meta.s1u_enb_addr = ipv4.src_addr; |
| 153 | spgw_meta.s1u_sgw_addr = ipv4.dst_addr; |
| 154 | ipv4 = gtpu_ipv4; |
| 155 | udp = gtpu_udp; |
| 156 | |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 157 | if (s1u_filter_table.apply().hit) { |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 158 | // TODO: check also that gtpu.msgtype == GTP_GPDU |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 159 | spgw_meta.do_spgw = true; |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 160 | spgw_meta.direction = DIR_UPLINK; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 161 | } |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 162 | } else if (ue_filter_table.apply().hit) { |
| 163 | spgw_meta.do_spgw = true; |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 164 | spgw_meta.direction = DIR_DOWNLINK; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (!spgw_meta.do_spgw) { |
| 168 | // Exit this control block. |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (spgw_meta.direction == DIR_UPLINK) { |
| 173 | gtpu_decap(); |
| 174 | } |
| 175 | |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 176 | #ifdef WITH_SPGW_PCC_GATING |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 177 | // Allow all traffic by default. |
| 178 | spgw_meta.pcc_gate_status = PCC_GATE_OPEN; |
| 179 | |
| 180 | sdf_rule_lookup.apply(); |
| 181 | pcc_rule_lookup.apply(); |
| 182 | pcc_info_lookup.apply(); |
| 183 | |
| 184 | if (spgw_meta.pcc_gate_status == PCC_GATE_CLOSED) { |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 185 | drop_now(); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 186 | } |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 187 | #endif // WITH_SPGW_PCC_GATING |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 188 | |
| 189 | if (spgw_meta.direction == DIR_DOWNLINK) { |
| 190 | if (!dl_sess_lookup.apply().hit) { |
| 191 | // We have no other choice than drop, as we miss the session |
| 192 | // info necessary to properly GTPU encap the packet. |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 193 | drop_now(); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 194 | } |
| 195 | ue_cdr_table.apply(); |
| 196 | } |
Carmelo Cascone | 274daef | 2018-02-14 20:32:49 -0800 | [diff] [blame] | 197 | |
| 198 | // Don't ask why... we'll need this later. |
| 199 | spgw_meta.ipv4_len = ipv4.total_len; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | control spgw_egress( |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 205 | in ipv4_t ipv4, |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 206 | out ipv4_t gtpu_ipv4, |
| 207 | out udp_t gtpu_udp, |
| 208 | out gtpu_t gtpu, |
| 209 | in spgw_meta_t spgw_meta, |
| 210 | in standard_metadata_t std_meta |
| 211 | ) { |
| 212 | |
| 213 | action gtpu_encap() { |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 214 | gtpu_ipv4.setValid(); |
| 215 | gtpu_ipv4.version = IP_VERSION_4; |
| 216 | gtpu_ipv4.ihl = IPV4_MIN_IHL; |
| 217 | gtpu_ipv4.diffserv = 0; |
Carmelo Cascone | 274daef | 2018-02-14 20:32:49 -0800 | [diff] [blame] | 218 | gtpu_ipv4.total_len = spgw_meta.ipv4_len |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 219 | + (IPV4_HDR_SIZE + UDP_HDR_SIZE + GTP_HDR_SIZE); |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 220 | gtpu_ipv4.identification = 0x1513; /* From NGIC */ |
| 221 | gtpu_ipv4.flags = 0; |
| 222 | gtpu_ipv4.frag_offset = 0; |
| 223 | gtpu_ipv4.ttl = DEFAULT_IPV4_TTL; |
| 224 | gtpu_ipv4.protocol = PROTO_UDP; |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 225 | gtpu_ipv4.dst_addr = spgw_meta.s1u_enb_addr; |
| 226 | gtpu_ipv4.src_addr = spgw_meta.s1u_sgw_addr; |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 227 | gtpu_ipv4.hdr_checksum = 0; // Updated later |
| 228 | |
| 229 | gtpu_udp.setValid(); |
| 230 | gtpu_udp.src_port = UDP_PORT_GTPU; |
| 231 | gtpu_udp.dst_port = UDP_PORT_GTPU; |
Carmelo Cascone | 274daef | 2018-02-14 20:32:49 -0800 | [diff] [blame] | 232 | gtpu_udp.len = spgw_meta.ipv4_len |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 233 | + (UDP_HDR_SIZE + GTP_HDR_SIZE); |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 234 | gtpu_udp.checksum = 0; // Updated later |
| 235 | |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 236 | gtpu.setValid(); |
| 237 | gtpu.version = GTPU_VERSION; |
| 238 | gtpu.pt = GTP_PROTOCOL_TYPE_GTP; |
| 239 | gtpu.spare = 0; |
| 240 | gtpu.ex_flag = 0; |
| 241 | gtpu.seq_flag = 0; |
| 242 | gtpu.npdu_flag = 0; |
| 243 | gtpu.msgtype = GTP_GPDU; |
Carmelo Cascone | 274daef | 2018-02-14 20:32:49 -0800 | [diff] [blame] | 244 | gtpu.msglen = spgw_meta.ipv4_len; |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 245 | gtpu.teid = spgw_meta.teid; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | apply { |
| 249 | if (spgw_meta.do_spgw && spgw_meta.direction == DIR_DOWNLINK) { |
| 250 | gtpu_encap(); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 256 | control verify_gtpu_checksum( |
| 257 | inout ipv4_t gtpu_ipv4 |
| 258 | ) { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 259 | apply { |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 260 | // TODO: re-enable gtpu_ipv4 verification |
| 261 | // with the current parser logic, gtpu_ip4 contains values of |
| 262 | // the inner header, which is already verified by include/checksum.p4. |
| 263 | // We need to modify the parser to copy the outer header somewhere |
| 264 | // else, and verify that here. |
| 265 | |
| 266 | // verify_checksum(gtpu_ipv4.isValid(), |
| 267 | // { |
| 268 | // gtpu_ipv4.version, |
| 269 | // gtpu_ipv4.ihl, |
| 270 | // gtpu_ipv4.diffserv, |
| 271 | // gtpu_ipv4.total_len, |
| 272 | // gtpu_ipv4.identification, |
| 273 | // gtpu_ipv4.flags, |
| 274 | // gtpu_ipv4.frag_offset, |
| 275 | // gtpu_ipv4.ttl, |
| 276 | // gtpu_ipv4.protocol, |
| 277 | // gtpu_ipv4.src_addr, |
| 278 | // gtpu_ipv4.dst_addr |
| 279 | // }, |
| 280 | // gtpu_ipv4.hdr_checksum, |
| 281 | // HashAlgorithm.csum16 |
| 282 | // ); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 287 | control update_gtpu_checksum( |
| 288 | inout ipv4_t gtpu_ipv4, |
| 289 | inout udp_t gtpu_udp, |
| 290 | in gtpu_t gtpu, |
| 291 | in ipv4_t ipv4, |
| 292 | in udp_t udp |
| 293 | ) { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 294 | apply { |
| 295 | // Compute outer IPv4 checksum. |
| 296 | update_checksum(gtpu_ipv4.isValid(), |
| 297 | { |
| 298 | gtpu_ipv4.version, |
| 299 | gtpu_ipv4.ihl, |
| 300 | gtpu_ipv4.diffserv, |
| 301 | gtpu_ipv4.total_len, |
| 302 | gtpu_ipv4.identification, |
| 303 | gtpu_ipv4.flags, |
| 304 | gtpu_ipv4.frag_offset, |
| 305 | gtpu_ipv4.ttl, |
| 306 | gtpu_ipv4.protocol, |
| 307 | gtpu_ipv4.src_addr, |
| 308 | gtpu_ipv4.dst_addr |
| 309 | }, |
| 310 | gtpu_ipv4.hdr_checksum, |
| 311 | HashAlgorithm.csum16 |
| 312 | ); |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 313 | |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 314 | #ifdef WITH_SPGW_UDP_CSUM_UPDATE |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 315 | // Compute outer UDP checksum. |
| 316 | update_checksum_with_payload(gtpu_udp.isValid(), |
| 317 | { |
| 318 | gtpu_ipv4.src_addr, |
| 319 | gtpu_ipv4.dst_addr, |
| 320 | 8w0, |
| 321 | gtpu_ipv4.protocol, |
| 322 | gtpu_udp.len, |
| 323 | gtpu_udp.src_port, |
| 324 | gtpu_udp.dst_port, |
| 325 | gtpu_udp.len, |
| 326 | gtpu, |
| 327 | ipv4, |
| 328 | // FIXME: we are assuming only UDP for downlink packets |
| 329 | // How to conditionally switch between UDP/TCP/ICMP? |
| 330 | udp |
| 331 | }, |
| 332 | gtpu_udp.checksum, |
| 333 | HashAlgorithm.csum16 |
| 334 | ); |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 335 | #endif // WITH_SPGW_UDP_CSUM_UPDATE |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | #endif |