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