gtpu: support non-G-PDU packets and PDU Session
[vpp.git] / src / plugins / gtpu / gtpu.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Intel and/or its affiliates.
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
18 #ifndef included_vnet_gtpu_h
19 #define included_vnet_gtpu_h
20
21 #include <vppinfra/lock.h>
22 #include <vppinfra/error.h>
23 #include <vppinfra/hash.h>
24 #include <vnet/vnet.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/ip/vtep.h>
27 #include <vnet/l2/l2_input.h>
28 #include <vnet/l2/l2_output.h>
29 #include <vnet/l2/l2_bd.h>
30 #include <vnet/ethernet/ethernet.h>
31 #include <vnet/ip/ip4_packet.h>
32 #include <vnet/ip/ip6_packet.h>
33 #include <vnet/udp/udp_packet.h>
34 #include <vnet/udp/udp_local.h>
35 #include <vnet/dpo/dpo.h>
36 #include <vnet/adj/adj_types.h>
37 #include <vnet/fib/fib_table.h>
38
39 /**
40  *              Bits
41  * Octets       8       7       6       5       4       3       2       1
42  * 1                      Version       PT      (*)     E       S       PN
43  * 2            Message Type
44  * 3            Length (1st Octet)
45  * 4            Length (2nd Octet)
46  * 5            Tunnel Endpoint Identifier (1st Octet)
47  * 6            Tunnel Endpoint Identifier (2nd Octet)
48  * 7            Tunnel Endpoint Identifier (3rd Octet)
49  * 8            Tunnel Endpoint Identifier (4th Octet)
50  * 9            Sequence Number (1st Octet)1) 4)
51  * 10           Sequence Number (2nd Octet)1) 4)
52  * 11           N-PDU Number2) 4)
53  * 12           Next Extension Header Type3) 4)
54 **/
55
56 typedef CLIB_PACKED (struct {
57   u8 ver_flags;
58   u8 type;
59   u16 length;                   /* length in octets of the data following the fixed part of the header */
60   u32 teid;
61   /* The following fields exists if and only if one or more of E, S or PN
62    * are 1. */
63   u16 sequence;
64   u8 pdu_number;
65   u8 next_ext_type;
66 }) gtpu_header_t;
67
68 typedef CLIB_PACKED (struct {
69   u8 type;
70   u8 len;
71   u16 pad;
72 }) gtpu_ext_header_t;
73
74 /**
75  * DL PDU SESSION INFORMATION (PDU Type 0):
76  * (3GPP TS 38.415)
77  *              Bits
78  * Octets       8       7       6       5       4       3       2       1
79  * 1                                 type     qmp     snp           spare
80  * 2         ppp      rqi                                          qos_fi
81  *
82  * UL PDU SESSION INFORMATION (PDU Type 1):
83  *              Bits
84  * Octets       8       7       6       5       4       3       2       1
85  * 1                                 type     qmp   DL d.   UL d.     snp
86  * 2  n3/n9 delay  new IE                                          qos_fi
87  **/
88 typedef CLIB_PACKED (struct {
89   u8 oct0;
90   u8 oct1;
91   // Extensions are supported
92 }) pdu_session_container_t;
93
94 STATIC_ASSERT_SIZEOF (pdu_session_container_t, 2);
95 typedef CLIB_PACKED (struct {
96   u8 len;
97   pdu_session_container_t pdu;
98   u8 next_header;
99 }) gtpu_ext_with_pdu_session_header_t;
100
101 #define GTPU_V1_HDR_LEN 8
102
103 #define GTPU_VER_MASK (7<<5)
104 #define GTPU_PT_BIT   (1<<4)
105 #define GTPU_RES_BIT     (1 << 3)
106 #define GTPU_E_BIT    (1<<2)
107 #define GTPU_S_BIT    (1<<1)
108 #define GTPU_PN_BIT   (1<<0)
109 #define GTPU_E_S_PN_BIT  (7<<0)
110
111 #define GTPU_V1_VER   (1<<5)
112
113 #define GTPU_PT_GTP    (1<<4)
114 #define GTPU_TYPE_GTPU  255
115
116 #define GTPU_EXT_HDR_PDU_SESSION_CONTAINER 133
117 #define GTPU_NO_MORE_EXT_HDR               0
118 #define GTPU_PDU_DL_SESSION_TYPE           0
119 #define GTPU_PDU_UL_SESSION_TYPE           (1 << 4)
120
121 #define GTPU_FORWARD_BAD_HEADER   (1 << 0)
122 #define GTPU_FORWARD_UNKNOWN_TEID (1 << 1)
123 #define GTPU_FORWARD_UNKNOWN_TYPE (1 << 2)
124
125 /* the ipv4 addresses used for the forwarding tunnels. 127.0.0.127 - .129. */
126 #define GTPU_FORWARD_BAD_HEADER_ADDRESS_IPV4   0x7f00007fu
127 #define GTPU_FORWARD_UNKNOWN_TEID_ADDRESS_IPV4 0x8000007fu
128 #define GTPU_FORWARD_UNKNOWN_TYPE_ADDRESS_IPV4 0x8100007fu
129
130 /* the ipv6 addresses used for the forwarding tunnels.
131  * 2001:db8:ffff:ffff:ffff:ffff:ffff:fffd -
132  * 2001:db8:ffff:ffff:ffff:ffff:ffff:ffff*/
133 #define GTPU_FORWARD_BAD_HEADER_ADDRESS_IPV6                                  \
134   {                                                                           \
135     .as_u64[0] = 0xffffffffb80d0120ull, .as_u64[1] = 0xfdffffffffffffffull    \
136   }
137 #define GTPU_FORWARD_UNKNOWN_TEID_ADDRESS_IPV6                                \
138   {                                                                           \
139     .as_u64[0] = 0xffffffffb80d0120ull, .as_u64[1] = 0xfeffffffffffffffull    \
140   }
141 #define GTPU_FORWARD_UNKNOWN_TYPE_ADDRESS_IPV6                                \
142   {                                                                           \
143     .as_u64[0] = 0xffffffffb80d0120ull, .as_u64[1] = 0xffffffffffffffffull    \
144   }
145 /* *INDENT-OFF* */
146 typedef CLIB_PACKED(struct
147 {
148   ip4_header_t ip4;            /* 20 bytes */
149   udp_header_t udp;            /* 8 bytes */
150   gtpu_header_t gtpu;          /* 12 bytes */
151   gtpu_ext_with_pdu_session_header_t gtpu_ext; /* 4 bytes */
152 }) ip4_gtpu_header_t;
153 /* *INDENT-ON* */
154
155 /* *INDENT-OFF* */
156 typedef CLIB_PACKED(struct
157 {
158   ip6_header_t ip6;            /* 40 bytes */
159   udp_header_t udp;            /* 8 bytes */
160   gtpu_header_t gtpu;          /* 12 bytes */
161   gtpu_ext_with_pdu_session_header_t gtpu_ext; /* 4 bytes */
162 }) ip6_gtpu_header_t;
163 /* *INDENT-ON* */
164
165 /* *INDENT-OFF* */
166 typedef CLIB_PACKED
167 (struct {
168   /*
169    * Key fields: ip src and gtpu teid on incoming gtpu packet
170    * all fields in NET byte order
171    */
172   union {
173     struct {
174       u32 src;
175       u32 teid;
176     };
177     u64 as_u64;
178   };
179 }) gtpu4_tunnel_key_t;
180 /* *INDENT-ON* */
181
182 /* *INDENT-OFF* */
183 typedef CLIB_PACKED
184 (struct {
185   /*
186    * Key fields: ip src and gtpu teid on incoming gtpu packet
187    * all fields in NET byte order
188    */
189   ip6_address_t src;
190   u32 teid;
191 }) gtpu6_tunnel_key_t;
192 /* *INDENT-ON* */
193
194 typedef struct
195 {
196   /* Required for pool_get_aligned  */
197   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
198
199   /* Rewrite string */
200   u8 *rewrite;
201
202   /* FIB DPO for IP forwarding of gtpu encap packet */
203   dpo_id_t next_dpo;
204
205   /* gtpu local(rx) and remote(tx) TEIDs in HOST byte order */
206   u32 teid;
207   u32 tteid;
208
209   /* tunnel src and dst addresses */
210   ip46_address_t src;
211   ip46_address_t dst;
212
213   /* mcast packet output intf index (used only if dst is mcast) */
214   u32 mcast_sw_if_index;
215
216   /* decap next index */
217   u32 decap_next_index;
218
219   /* The FIB index for src/dst addresses */
220   u32 encap_fib_index;
221
222   /* vnet intfc index */
223   u32 sw_if_index;
224   u32 hw_if_index;
225
226   /* PDU session container extension enable/disable */
227   u8 pdu_extension;
228   u8 qfi;
229
230   /* The tunnel is used for forwarding */
231   u8 is_forwarding;
232   u8 forwarding_type;
233
234   /**
235    * Linkage into the FIB object graph
236    */
237   fib_node_t node;
238
239   /*
240    * The FIB entry for (depending on gtpu tunnel is unicast or mcast)
241    * sending unicast gtpu encap packets or receiving mcast gtpu packets
242    */
243   fib_node_index_t fib_entry_index;
244   adj_index_t mcast_adj_index;
245
246   /**
247    * The tunnel is a child of the FIB entry for its destination. This is
248    * so it receives updates when the forwarding information for that entry
249    * changes.
250    * The tunnels sibling index on the FIB entry's dependency list.
251    */
252   u32 sibling_index;
253
254   u32 flow_index;               /* infra flow index */
255 } gtpu_tunnel_t;
256
257 #define foreach_gtpu_input_next        \
258 _(DROP, "error-drop")                  \
259 _(L2_INPUT, "l2-input")                \
260 _(IP4_INPUT,  "ip4-input")             \
261 _(IP6_INPUT, "ip6-input" )
262
263 typedef enum
264 {
265 #define _(s,n) GTPU_INPUT_NEXT_##s,
266   foreach_gtpu_input_next
267 #undef _
268     GTPU_INPUT_N_NEXT,
269 } gtpu_input_next_t;
270
271 typedef enum
272 {
273 #define gtpu_error(n,s) GTPU_ERROR_##n,
274 #include <gtpu/gtpu_error.def>
275 #undef gtpu_error
276   GTPU_N_ERROR,
277 } gtpu_input_error_t;
278
279 typedef struct
280 {
281   /* vector of encap tunnel instances */
282   gtpu_tunnel_t *tunnels;
283
284   /* lookup tunnel by key */
285   uword *gtpu4_tunnel_by_key;   /* keyed on ipv4.dst + teid */
286   uword *gtpu6_tunnel_by_key;   /* keyed on ipv6.dst + teid */
287
288   /* local VTEP IPs ref count used by gtpu-bypass node to check if
289      received gtpu packet DIP matches any local VTEP address */
290   vtep_table_t vtep_table;
291
292   /* mcast shared info */
293   uword *mcast_shared;          /* keyed on mcast ip46 addr */
294
295   /* Free vlib hw_if_indices */
296   u32 *free_gtpu_tunnel_hw_if_indices;
297
298   /* Mapping from sw_if_index to tunnel index */
299   u32 *tunnel_index_by_sw_if_index;
300
301   /**
302    * Node type for registering to fib changes.
303    */
304   fib_node_type_t fib_node_type;
305
306   /* API message ID base */
307   u16 msg_id_base;
308
309   /* Handle GTP packets of unknown type like echo and error indication,
310    * unknown teid or bad version/header.
311    * All packets will be forwarded to a new IP address,
312    * so that they can be processes outside vpp.
313    * If not set then packets are dropped.
314    * One of more indexes can be unused (~0). */
315   u32 bad_header_forward_tunnel_index_ipv4;
316   u32 unknown_teid_forward_tunnel_index_ipv4;
317   u32 unknown_type_forward_tunnel_index_ipv4;
318   u32 bad_header_forward_tunnel_index_ipv6;
319   u32 unknown_teid_forward_tunnel_index_ipv6;
320   u32 unknown_type_forward_tunnel_index_ipv6;
321
322   /* convenience */
323   vlib_main_t *vlib_main;
324   vnet_main_t *vnet_main;
325   u32 flow_id_start;
326   /* cache for last 8 gtpu tunnel */
327   vtep4_cache_t vtep4_u512;
328
329 } gtpu_main_t;
330
331 extern gtpu_main_t gtpu_main;
332
333 extern vlib_node_registration_t gtpu4_input_node;
334 extern vlib_node_registration_t gtpu6_input_node;
335 extern vlib_node_registration_t gtpu4_encap_node;
336 extern vlib_node_registration_t gtpu6_encap_node;
337 extern vlib_node_registration_t gtpu4_flow_input_node;
338
339 u8 *format_gtpu_encap_trace (u8 * s, va_list * args);
340
341 typedef struct
342 {
343   u8 opn;
344 #define GTPU_DEL_TUNNEL 0
345 #define GTPU_ADD_TUNNEL 1
346 #define GTPU_UPD_TTEID  2
347   ip46_address_t src, dst;
348   u32 mcast_sw_if_index;
349   u32 encap_fib_index;
350   u32 decap_next_index;
351   u32 teid;                     /* local  or rx teid */
352   u32 tteid;                    /* remote or tx teid */
353   u8 pdu_extension;
354   u8 qfi;
355   u8 is_forwarding;
356   u8 forwarding_type;
357 } vnet_gtpu_add_mod_del_tunnel_args_t;
358
359 int vnet_gtpu_add_del_forwarding (vnet_gtpu_add_mod_del_tunnel_args_t *a,
360                                   u32 *sw_if_indexp);
361
362 int vnet_gtpu_add_mod_del_tunnel
363   (vnet_gtpu_add_mod_del_tunnel_args_t * a, u32 * sw_if_indexp);
364
365 typedef struct
366 {
367   u32 tunnel_index;
368   u32 tteid;
369   u8 pdu_extension;
370   u8 qfi;
371 } gtpu_encap_trace_t;
372
373 void vnet_int_gtpu_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable);
374 u32 vnet_gtpu_get_tunnel_index (u32 sw_if_index);
375 int vnet_gtpu_add_del_rx_flow (u32 hw_if_index, u32 t_imdex, int is_add);
376 int get_combined_counters (u32 sw_if_index, vlib_counter_t *result_rx,
377                            vlib_counter_t *result_tx);
378
379 #endif /* included_vnet_gtpu_h */
380
381
382 /*
383  * fd.io coding-style-patch-verification: ON
384  *
385  * Local Variables:
386  * eval: (c-set-style "gnu")
387  * End:
388  */