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 | |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 20 | control spgw_normalizer( |
| 21 | in bool is_gtpu_encapped, |
| 22 | out ipv4_t gtpu_ipv4, |
| 23 | out udp_t gtpu_udp, |
| 24 | inout ipv4_t ipv4, |
| 25 | inout udp_t udp, |
| 26 | in ipv4_t inner_ipv4, |
| 27 | in udp_t inner_udp |
| 28 | ) { |
| 29 | apply { |
| 30 | if (! is_gtpu_encapped) return; |
| 31 | gtpu_ipv4 = ipv4; |
| 32 | ipv4 = inner_ipv4; |
| 33 | gtpu_udp = udp; |
| 34 | if (inner_udp.isValid()) { |
| 35 | udp = inner_udp; |
| 36 | } else { |
| 37 | udp.setInvalid(); |
| 38 | } |
| 39 | } |
| 40 | } |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 41 | |
| 42 | control spgw_ingress( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 43 | inout ipv4_t gtpu_ipv4, |
| 44 | inout udp_t gtpu_udp, |
| 45 | inout gtpu_t gtpu, |
| 46 | inout ipv4_t ipv4, |
| 47 | inout udp_t udp, |
| 48 | inout fabric_metadata_t fabric_meta |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 49 | ) { |
| 50 | |
| 51 | direct_counter(CounterType.packets_and_bytes) ue_counter; |
| 52 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 53 | @hidden |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 54 | action gtpu_decap() { |
| 55 | gtpu_ipv4.setInvalid(); |
| 56 | gtpu_udp.setInvalid(); |
| 57 | gtpu.setInvalid(); |
| 58 | } |
| 59 | |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 60 | action set_dl_sess_info(bit<32> teid, |
| 61 | bit<32> s1u_enb_addr, |
| 62 | bit<32> s1u_sgw_addr) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 63 | fabric_meta.spgw.teid = teid; |
| 64 | fabric_meta.spgw.s1u_enb_addr = s1u_enb_addr; |
| 65 | fabric_meta.spgw.s1u_sgw_addr = s1u_sgw_addr; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 66 | ue_counter.count(); |
| 67 | } |
| 68 | |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 69 | table dl_sess_lookup { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 70 | key = { |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 71 | // UE addr for downlink |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 72 | ipv4.dst_addr : exact @name("ipv4_dst"); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 73 | } |
| 74 | actions = { |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 75 | set_dl_sess_info(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 76 | @defaultonly nop(); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 77 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 78 | const default_action = nop(); |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 79 | counters = ue_counter; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | table s1u_filter_table { |
| 83 | key = { |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 84 | // IP addresses of the S1U interfaces of this SPGW-U instance (when uplink) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 85 | gtpu_ipv4.dst_addr : exact @name("gtp_ipv4_dst"); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 86 | } |
| 87 | actions = { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 88 | nop(); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 89 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 90 | const default_action = nop(); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 93 | #ifdef WITH_SPGW_PCC_GATING |
| 94 | action set_sdf_rule_id(sdf_rule_id_t id) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 95 | fabric_meta.spgw.sdf_rule_id = id; |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | action set_pcc_rule_id(pcc_rule_id_t id) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 99 | fabric_meta.spgw.pcc_rule_id = id; |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | action set_pcc_info(pcc_gate_status_t gate_status) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 103 | fabric_meta.spgw.pcc_gate_status = gate_status; |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 106 | table sdf_rule_lookup { |
| 107 | key = { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 108 | fabric_meta.spgw.direction : exact @name("spgw_direction"); |
| 109 | ipv4.src_addr : ternary @name("ipv4_src"); |
| 110 | ipv4.dst_addr : ternary @name("ipv4_dst"); |
| 111 | ipv4.protocol : ternary @name("ip_proto"); |
| 112 | fabric_meta.l4_sport : ternary @name("l4_sport"); |
| 113 | fabric_meta.l4_dport : ternary @name("l4_dport"); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 114 | } |
| 115 | actions = { |
| 116 | set_sdf_rule_id(); |
| 117 | } |
| 118 | const default_action = set_sdf_rule_id(DEFAULT_SDF_RULE_ID); |
| 119 | } |
| 120 | |
| 121 | table pcc_rule_lookup { |
| 122 | key = { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 123 | fabric_meta.spgw.sdf_rule_id : exact @name("sdf_rule_id"); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 124 | } |
| 125 | actions = { |
| 126 | set_pcc_rule_id(); |
| 127 | } |
| 128 | const default_action = set_pcc_rule_id(DEFAULT_PCC_RULE_ID); |
| 129 | } |
| 130 | |
| 131 | table pcc_info_lookup { |
| 132 | key = { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 133 | fabric_meta.spgw.pcc_rule_id : exact @name("pcc_rule_id"); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 134 | } |
| 135 | actions = { |
| 136 | set_pcc_info(); |
| 137 | } |
| 138 | const default_action = set_pcc_info(PCC_GATE_OPEN); |
| 139 | } |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 140 | #endif // WITH_SPGW_PCC_GATING |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 141 | |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 142 | apply { |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 143 | if (gtpu.isValid()) { |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 144 | // If here, pkt has outer IP dst on |
| 145 | // S1U_SGW_PREFIX/S1U_SGW_PREFIX_LEN subnet. |
| 146 | // TODO: check also that gtpu.msgtype == GTP_GPDU |
| 147 | if (!s1u_filter_table.apply().hit) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 148 | mark_to_drop(); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 149 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 150 | fabric_meta.spgw.direction = SPGW_DIR_UPLINK; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 151 | gtpu_decap(); |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 152 | } else if (dl_sess_lookup.apply().hit) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 153 | fabric_meta.spgw.direction = SPGW_DIR_DOWNLINK; |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 154 | } else { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 155 | fabric_meta.spgw.direction = SPGW_DIR_UNKNOWN; |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 156 | // No SPGW processing needed. |
| 157 | return; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 160 | #ifdef WITH_SPGW_PCC_GATING |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 161 | // Allow all traffic by default. |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 162 | fabric_meta.spgw.pcc_gate_status = PCC_GATE_OPEN; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 163 | |
| 164 | sdf_rule_lookup.apply(); |
| 165 | pcc_rule_lookup.apply(); |
| 166 | pcc_info_lookup.apply(); |
| 167 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 168 | if (fabric_meta.spgw.pcc_gate_status == PCC_GATE_CLOSED) { |
| 169 | mark_to_drop(); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 170 | } |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 171 | #endif // WITH_SPGW_PCC_GATING |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 172 | |
Carmelo Cascone | 274daef | 2018-02-14 20:32:49 -0800 | [diff] [blame] | 173 | // Don't ask why... we'll need this later. |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 174 | fabric_meta.spgw.ipv4_len = ipv4.total_len; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | |
| 179 | control spgw_egress( |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 180 | in ipv4_t ipv4, |
| 181 | inout ipv4_t gtpu_ipv4, |
| 182 | inout udp_t gtpu_udp, |
| 183 | inout gtpu_t gtpu, |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 184 | in fabric_metadata_t fabric_meta, |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 185 | in standard_metadata_t std_meta |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 186 | ) { |
| 187 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 188 | @hidden |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 189 | action gtpu_encap() { |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 190 | gtpu_ipv4.setValid(); |
| 191 | gtpu_ipv4.version = IP_VERSION_4; |
| 192 | gtpu_ipv4.ihl = IPV4_MIN_IHL; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 193 | gtpu_ipv4.dscp = 0; |
| 194 | gtpu_ipv4.ecn = 0; |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 195 | gtpu_ipv4.total_len = ipv4.total_len |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 196 | + (IPV4_HDR_SIZE + UDP_HDR_SIZE + GTP_HDR_SIZE); |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 197 | gtpu_ipv4.identification = 0x1513; /* From NGIC */ |
| 198 | gtpu_ipv4.flags = 0; |
| 199 | gtpu_ipv4.frag_offset = 0; |
| 200 | gtpu_ipv4.ttl = DEFAULT_IPV4_TTL; |
| 201 | gtpu_ipv4.protocol = PROTO_UDP; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 202 | gtpu_ipv4.dst_addr = fabric_meta.spgw.s1u_enb_addr; |
| 203 | gtpu_ipv4.src_addr = fabric_meta.spgw.s1u_sgw_addr; |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 204 | gtpu_ipv4.hdr_checksum = 0; // Updated later |
| 205 | |
| 206 | gtpu_udp.setValid(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 207 | gtpu_udp.sport = UDP_PORT_GTPU; |
| 208 | gtpu_udp.dport = UDP_PORT_GTPU; |
| 209 | gtpu_udp.len = fabric_meta.spgw.ipv4_len |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 210 | + (UDP_HDR_SIZE + GTP_HDR_SIZE); |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 211 | gtpu_udp.checksum = 0; // Updated later |
| 212 | |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 213 | gtpu.setValid(); |
| 214 | gtpu.version = GTPU_VERSION; |
| 215 | gtpu.pt = GTP_PROTOCOL_TYPE_GTP; |
| 216 | gtpu.spare = 0; |
| 217 | gtpu.ex_flag = 0; |
| 218 | gtpu.seq_flag = 0; |
| 219 | gtpu.npdu_flag = 0; |
| 220 | gtpu.msgtype = GTP_GPDU; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 221 | gtpu.msglen = fabric_meta.spgw.ipv4_len; |
| 222 | gtpu.teid = fabric_meta.spgw.teid; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | apply { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 226 | if (fabric_meta.spgw.direction == SPGW_DIR_DOWNLINK) { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 227 | gtpu_encap(); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 233 | control update_gtpu_checksum( |
| 234 | inout ipv4_t gtpu_ipv4, |
| 235 | inout udp_t gtpu_udp, |
| 236 | in gtpu_t gtpu, |
| 237 | in ipv4_t ipv4, |
| 238 | in udp_t udp |
| 239 | ) { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 240 | apply { |
| 241 | // Compute outer IPv4 checksum. |
| 242 | update_checksum(gtpu_ipv4.isValid(), |
| 243 | { |
| 244 | gtpu_ipv4.version, |
| 245 | gtpu_ipv4.ihl, |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 246 | gtpu_ipv4.dscp, |
| 247 | gtpu_ipv4.ecn, |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 248 | gtpu_ipv4.total_len, |
| 249 | gtpu_ipv4.identification, |
| 250 | gtpu_ipv4.flags, |
| 251 | gtpu_ipv4.frag_offset, |
| 252 | gtpu_ipv4.ttl, |
| 253 | gtpu_ipv4.protocol, |
| 254 | gtpu_ipv4.src_addr, |
| 255 | gtpu_ipv4.dst_addr |
| 256 | }, |
| 257 | gtpu_ipv4.hdr_checksum, |
| 258 | HashAlgorithm.csum16 |
| 259 | ); |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 260 | |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 261 | #ifdef WITH_SPGW_UDP_CSUM_UPDATE |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 262 | // Compute outer UDP checksum. |
| 263 | update_checksum_with_payload(gtpu_udp.isValid(), |
| 264 | { |
| 265 | gtpu_ipv4.src_addr, |
| 266 | gtpu_ipv4.dst_addr, |
| 267 | 8w0, |
| 268 | gtpu_ipv4.protocol, |
| 269 | gtpu_udp.len, |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 270 | gtpu_udp.sport, |
| 271 | gtpu_udp.dport, |
Carmelo Cascone | 14cde40 | 2018-01-25 01:57:18 -0800 | [diff] [blame] | 272 | gtpu_udp.len, |
| 273 | gtpu, |
| 274 | ipv4, |
| 275 | // FIXME: we are assuming only UDP for downlink packets |
| 276 | // How to conditionally switch between UDP/TCP/ICMP? |
| 277 | udp |
| 278 | }, |
| 279 | gtpu_udp.checksum, |
| 280 | HashAlgorithm.csum16 |
| 281 | ); |
Carmelo Cascone | 33e85c0 | 2018-02-01 13:30:18 -0800 | [diff] [blame] | 282 | #endif // WITH_SPGW_UDP_CSUM_UPDATE |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | #endif |