blob: d444e52a7f21d89f0571b82fecc1720f27c7dc64 [file] [log] [blame]
Carmelo Casconeb81f4be2018-01-16 23:24:01 -08001/*
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 Cascone9b0171b2018-08-14 01:43:57 -070020control 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 Casconeb81f4be2018-01-16 23:24:01 -080041
42control spgw_ingress(
Carmelo Casconeb5324e72018-11-25 02:26:32 -080043 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 Casconeb81f4be2018-01-16 23:24:01 -080049 ) {
50
51 direct_counter(CounterType.packets_and_bytes) ue_counter;
52
Carmelo Casconeb5324e72018-11-25 02:26:32 -080053 @hidden
Carmelo Casconeb81f4be2018-01-16 23:24:01 -080054 action gtpu_decap() {
55 gtpu_ipv4.setInvalid();
56 gtpu_udp.setInvalid();
57 gtpu.setInvalid();
58 }
59
Carmelo Casconeb757dbc2018-01-25 17:53:17 -080060 action set_dl_sess_info(bit<32> teid,
61 bit<32> s1u_enb_addr,
62 bit<32> s1u_sgw_addr) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080063 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 Casconeb81f4be2018-01-16 23:24:01 -080066 ue_counter.count();
67 }
68
Carmelo Cascone9b0171b2018-08-14 01:43:57 -070069 table dl_sess_lookup {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -080070 key = {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -070071 // UE addr for downlink
Carmelo Casconeb5324e72018-11-25 02:26:32 -080072 ipv4.dst_addr : exact @name("ipv4_dst");
Carmelo Casconeb81f4be2018-01-16 23:24:01 -080073 }
74 actions = {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -070075 set_dl_sess_info();
Carmelo Casconeb5324e72018-11-25 02:26:32 -080076 @defaultonly nop();
Carmelo Casconeb81f4be2018-01-16 23:24:01 -080077 }
Carmelo Casconeb5324e72018-11-25 02:26:32 -080078 const default_action = nop();
Carmelo Cascone9b0171b2018-08-14 01:43:57 -070079 counters = ue_counter;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -080080 }
81
82 table s1u_filter_table {
83 key = {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -070084 // IP addresses of the S1U interfaces of this SPGW-U instance (when uplink)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080085 gtpu_ipv4.dst_addr : exact @name("gtp_ipv4_dst");
Carmelo Casconeb81f4be2018-01-16 23:24:01 -080086 }
87 actions = {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080088 nop();
Carmelo Casconeb81f4be2018-01-16 23:24:01 -080089 }
Carmelo Casconeb5324e72018-11-25 02:26:32 -080090 const default_action = nop();
Carmelo Casconeb81f4be2018-01-16 23:24:01 -080091 }
92
Carmelo Casconeb757dbc2018-01-25 17:53:17 -080093#ifdef WITH_SPGW_PCC_GATING
94 action set_sdf_rule_id(sdf_rule_id_t id) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080095 fabric_meta.spgw.sdf_rule_id = id;
Carmelo Casconeb757dbc2018-01-25 17:53:17 -080096 }
97
98 action set_pcc_rule_id(pcc_rule_id_t id) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080099 fabric_meta.spgw.pcc_rule_id = id;
Carmelo Casconeb757dbc2018-01-25 17:53:17 -0800100 }
101
102 action set_pcc_info(pcc_gate_status_t gate_status) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800103 fabric_meta.spgw.pcc_gate_status = gate_status;
Carmelo Casconeb757dbc2018-01-25 17:53:17 -0800104 }
105
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800106 table sdf_rule_lookup {
107 key = {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800108 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 Casconeb81f4be2018-01-16 23:24:01 -0800114 }
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 Casconeb5324e72018-11-25 02:26:32 -0800123 fabric_meta.spgw.sdf_rule_id : exact @name("sdf_rule_id");
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800124 }
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 Casconeb5324e72018-11-25 02:26:32 -0800133 fabric_meta.spgw.pcc_rule_id : exact @name("pcc_rule_id");
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800134 }
135 actions = {
136 set_pcc_info();
137 }
138 const default_action = set_pcc_info(PCC_GATE_OPEN);
139 }
Carmelo Casconeb757dbc2018-01-25 17:53:17 -0800140#endif // WITH_SPGW_PCC_GATING
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800141
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800142 apply {
Carmelo Casconeb757dbc2018-01-25 17:53:17 -0800143 if (gtpu.isValid()) {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700144 // 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 Casconeb5324e72018-11-25 02:26:32 -0800148 mark_to_drop();
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800149 }
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800150 fabric_meta.spgw.direction = SPGW_DIR_UPLINK;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800151 gtpu_decap();
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700152 } else if (dl_sess_lookup.apply().hit) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800153 fabric_meta.spgw.direction = SPGW_DIR_DOWNLINK;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700154 } else {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800155 fabric_meta.spgw.direction = SPGW_DIR_UNKNOWN;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700156 // No SPGW processing needed.
157 return;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800158 }
159
Carmelo Casconeb757dbc2018-01-25 17:53:17 -0800160#ifdef WITH_SPGW_PCC_GATING
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800161 // Allow all traffic by default.
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800162 fabric_meta.spgw.pcc_gate_status = PCC_GATE_OPEN;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800163
164 sdf_rule_lookup.apply();
165 pcc_rule_lookup.apply();
166 pcc_info_lookup.apply();
167
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800168 if (fabric_meta.spgw.pcc_gate_status == PCC_GATE_CLOSED) {
169 mark_to_drop();
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800170 }
Carmelo Casconeb757dbc2018-01-25 17:53:17 -0800171#endif // WITH_SPGW_PCC_GATING
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800172
Carmelo Cascone274daef2018-02-14 20:32:49 -0800173 // Don't ask why... we'll need this later.
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800174 fabric_meta.spgw.ipv4_len = ipv4.total_len;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800175 }
176}
177
178
179control spgw_egress(
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700180 in ipv4_t ipv4,
181 inout ipv4_t gtpu_ipv4,
182 inout udp_t gtpu_udp,
183 inout gtpu_t gtpu,
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800184 in fabric_metadata_t fabric_meta,
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700185 in standard_metadata_t std_meta
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800186 ) {
187
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800188 @hidden
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800189 action gtpu_encap() {
Carmelo Cascone14cde402018-01-25 01:57:18 -0800190 gtpu_ipv4.setValid();
191 gtpu_ipv4.version = IP_VERSION_4;
192 gtpu_ipv4.ihl = IPV4_MIN_IHL;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900193 gtpu_ipv4.dscp = 0;
194 gtpu_ipv4.ecn = 0;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700195 gtpu_ipv4.total_len = ipv4.total_len
Carmelo Cascone33e85c02018-02-01 13:30:18 -0800196 + (IPV4_HDR_SIZE + UDP_HDR_SIZE + GTP_HDR_SIZE);
Carmelo Cascone14cde402018-01-25 01:57:18 -0800197 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 Casconeb5324e72018-11-25 02:26:32 -0800202 gtpu_ipv4.dst_addr = fabric_meta.spgw.s1u_enb_addr;
203 gtpu_ipv4.src_addr = fabric_meta.spgw.s1u_sgw_addr;
Carmelo Cascone14cde402018-01-25 01:57:18 -0800204 gtpu_ipv4.hdr_checksum = 0; // Updated later
205
206 gtpu_udp.setValid();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800207 gtpu_udp.sport = UDP_PORT_GTPU;
208 gtpu_udp.dport = UDP_PORT_GTPU;
209 gtpu_udp.len = fabric_meta.spgw.ipv4_len
Carmelo Cascone33e85c02018-02-01 13:30:18 -0800210 + (UDP_HDR_SIZE + GTP_HDR_SIZE);
Carmelo Cascone14cde402018-01-25 01:57:18 -0800211 gtpu_udp.checksum = 0; // Updated later
212
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800213 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 Casconeb5324e72018-11-25 02:26:32 -0800221 gtpu.msglen = fabric_meta.spgw.ipv4_len;
222 gtpu.teid = fabric_meta.spgw.teid;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800223 }
224
225 apply {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800226 if (fabric_meta.spgw.direction == SPGW_DIR_DOWNLINK) {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800227 gtpu_encap();
228 }
229 }
230}
231
232
Carmelo Cascone14cde402018-01-25 01:57:18 -0800233control 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 Casconeb81f4be2018-01-16 23:24:01 -0800240 apply {
241 // Compute outer IPv4 checksum.
242 update_checksum(gtpu_ipv4.isValid(),
243 {
244 gtpu_ipv4.version,
245 gtpu_ipv4.ihl,
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900246 gtpu_ipv4.dscp,
247 gtpu_ipv4.ecn,
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800248 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 Cascone14cde402018-01-25 01:57:18 -0800260
Carmelo Cascone33e85c02018-02-01 13:30:18 -0800261#ifdef WITH_SPGW_UDP_CSUM_UPDATE
Carmelo Cascone14cde402018-01-25 01:57:18 -0800262 // 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 Casconeb5324e72018-11-25 02:26:32 -0800270 gtpu_udp.sport,
271 gtpu_udp.dport,
Carmelo Cascone14cde402018-01-25 01:57:18 -0800272 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 Cascone33e85c02018-02-01 13:30:18 -0800282#endif // WITH_SPGW_UDP_CSUM_UPDATE
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800283 }
284}
285
286#endif