gtpu: offload RX flow
[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/l2/l2_input.h>
27 #include <vnet/l2/l2_output.h>
28 #include <vnet/l2/l2_bd.h>
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/ip/ip4_packet.h>
31 #include <vnet/ip/ip6_packet.h>
32 #include <vnet/udp/udp.h>
33 #include <vnet/dpo/dpo.h>
34 #include <vnet/adj/adj_types.h>
35 #include <vnet/fib/fib_table.h>
36
37 /**
38  *              Bits
39  * Octets       8       7       6       5       4       3       2       1
40  * 1                      Version       PT      (*)     E       S       PN
41  * 2            Message Type
42  * 3            Length (1st Octet)
43  * 4            Length (2nd Octet)
44  * 5            Tunnel Endpoint Identifier (1st Octet)
45  * 6            Tunnel Endpoint Identifier (2nd Octet)
46  * 7            Tunnel Endpoint Identifier (3rd Octet)
47  * 8            Tunnel Endpoint Identifier (4th Octet)
48  * 9            Sequence Number (1st Octet)1) 4)
49  * 10           Sequence Number (2nd Octet)1) 4)
50  * 11           N-PDU Number2) 4)
51  * 12           Next Extension Header Type3) 4)
52 **/
53
54 typedef struct
55 {
56   u8 ver_flags;
57   u8 type;
58   u16 length;                   /* length in octets of the data following the fixed part of the header */
59   u32 teid;
60   u16 sequence;
61   u8 pdu_number;
62   u8 next_ext_type;
63 } gtpu_header_t;
64
65 #define GTPU_V1_HDR_LEN   8
66
67 #define GTPU_VER_MASK (7<<5)
68 #define GTPU_PT_BIT   (1<<4)
69 #define GTPU_E_BIT    (1<<2)
70 #define GTPU_S_BIT    (1<<1)
71 #define GTPU_PN_BIT   (1<<0)
72 #define GTPU_E_S_PN_BIT  (7<<0)
73
74 #define GTPU_V1_VER   (1<<5)
75
76 #define GTPU_PT_GTP    (1<<4)
77 #define GTPU_TYPE_GTPU  255
78
79 /* *INDENT-OFF* */
80 typedef CLIB_PACKED(struct
81 {
82   ip4_header_t ip4;            /* 20 bytes */
83   udp_header_t udp;            /* 8 bytes */
84   gtpu_header_t gtpu;          /* 12 bytes */
85 }) ip4_gtpu_header_t;
86 /* *INDENT-ON* */
87
88 /* *INDENT-OFF* */
89 typedef CLIB_PACKED(struct
90 {
91   ip6_header_t ip6;            /* 40 bytes */
92   udp_header_t udp;            /* 8 bytes */
93   gtpu_header_t gtpu;     /* 8 bytes */
94 }) ip6_gtpu_header_t;
95 /* *INDENT-ON* */
96
97 /* *INDENT-OFF* */
98 typedef CLIB_PACKED
99 (struct {
100   /*
101    * Key fields: ip src and gtpu teid on incoming gtpu packet
102    * all fields in NET byte order
103    */
104   union {
105     struct {
106       u32 src;
107       u32 teid;
108     };
109     u64 as_u64;
110   };
111 }) gtpu4_tunnel_key_t;
112 /* *INDENT-ON* */
113
114 /* *INDENT-OFF* */
115 typedef CLIB_PACKED
116 (struct {
117   /*
118    * Key fields: ip src and gtpu teid on incoming gtpu packet
119    * all fields in NET byte order
120    */
121   ip6_address_t src;
122   u32 teid;
123 }) gtpu6_tunnel_key_t;
124 /* *INDENT-ON* */
125
126 typedef struct
127 {
128   /* Required for pool_get_aligned  */
129   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
130
131   /* Rewrite string */
132   u8 *rewrite;
133
134   /* FIB DPO for IP forwarding of gtpu encap packet */
135   dpo_id_t next_dpo;
136
137   /* gtpu teid in HOST byte order */
138   u32 teid;
139
140   /* tunnel src and dst addresses */
141   ip46_address_t src;
142   ip46_address_t dst;
143
144   /* mcast packet output intf index (used only if dst is mcast) */
145   u32 mcast_sw_if_index;
146
147   /* decap next index */
148   u32 decap_next_index;
149
150   /* The FIB index for src/dst addresses */
151   u32 encap_fib_index;
152
153   /* vnet intfc index */
154   u32 sw_if_index;
155   u32 hw_if_index;
156
157   /**
158    * Linkage into the FIB object graph
159    */
160   fib_node_t node;
161
162   /*
163    * The FIB entry for (depending on gtpu tunnel is unicast or mcast)
164    * sending unicast gtpu encap packets or receiving mcast gtpu packets
165    */
166   fib_node_index_t fib_entry_index;
167   adj_index_t mcast_adj_index;
168
169   /**
170    * The tunnel is a child of the FIB entry for its destination. This is
171    * so it receives updates when the forwarding information for that entry
172    * changes.
173    * The tunnels sibling index on the FIB entry's dependency list.
174    */
175   u32 sibling_index;
176
177   u32 flow_index;               /* infra flow index */
178 } gtpu_tunnel_t;
179
180 #define foreach_gtpu_input_next        \
181 _(DROP, "error-drop")                  \
182 _(L2_INPUT, "l2-input")                \
183 _(IP4_INPUT,  "ip4-input")             \
184 _(IP6_INPUT, "ip6-input" )
185
186 typedef enum
187 {
188 #define _(s,n) GTPU_INPUT_NEXT_##s,
189   foreach_gtpu_input_next
190 #undef _
191     GTPU_INPUT_N_NEXT,
192 } gtpu_input_next_t;
193
194 typedef enum
195 {
196 #define gtpu_error(n,s) GTPU_ERROR_##n,
197 #include <gtpu/gtpu_error.def>
198 #undef gtpu_error
199   GTPU_N_ERROR,
200 } gtpu_input_error_t;
201
202 typedef struct
203 {
204   /* vector of encap tunnel instances */
205   gtpu_tunnel_t *tunnels;
206
207   /* lookup tunnel by key */
208   uword *gtpu4_tunnel_by_key;   /* keyed on ipv4.dst + teid */
209   uword *gtpu6_tunnel_by_key;   /* keyed on ipv6.dst + teid */
210
211   /* local VTEP IPs ref count used by gtpu-bypass node to check if
212      received gtpu packet DIP matches any local VTEP address */
213   uword *vtep4;                 /* local ip4 VTEPs keyed on their ip4 addr */
214   uword *vtep6;                 /* local ip6 VTEPs keyed on their ip6 addr */
215
216   /* mcast shared info */
217   uword *mcast_shared;          /* keyed on mcast ip46 addr */
218
219   /* Free vlib hw_if_indices */
220   u32 *free_gtpu_tunnel_hw_if_indices;
221
222   /* Mapping from sw_if_index to tunnel index */
223   u32 *tunnel_index_by_sw_if_index;
224
225   /**
226    * Node type for registering to fib changes.
227    */
228   fib_node_type_t fib_node_type;
229
230   /* API message ID base */
231   u16 msg_id_base;
232
233   /* convenience */
234   vlib_main_t *vlib_main;
235   vnet_main_t *vnet_main;
236   u32 flow_id_start;
237 } gtpu_main_t;
238
239 extern gtpu_main_t gtpu_main;
240
241 extern vlib_node_registration_t gtpu4_input_node;
242 extern vlib_node_registration_t gtpu6_input_node;
243 extern vlib_node_registration_t gtpu4_encap_node;
244 extern vlib_node_registration_t gtpu6_encap_node;
245 extern vlib_node_registration_t gtpu4_flow_input_node;
246
247 u8 *format_gtpu_encap_trace (u8 * s, va_list * args);
248
249 typedef struct
250 {
251   u8 is_add;
252   ip46_address_t src, dst;
253   u32 mcast_sw_if_index;
254   u32 encap_fib_index;
255   u32 decap_next_index;
256   u32 teid;
257 } vnet_gtpu_add_del_tunnel_args_t;
258
259 int vnet_gtpu_add_del_tunnel
260   (vnet_gtpu_add_del_tunnel_args_t * a, u32 * sw_if_indexp);
261
262 typedef struct
263 {
264   u32 tunnel_index;
265   u32 teid;
266 } gtpu_encap_trace_t;
267
268 void vnet_int_gtpu_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable);
269 u32 vnet_gtpu_get_tunnel_index (u32 sw_if_index);
270 int vnet_gtpu_add_del_rx_flow (u32 hw_if_index, u32 t_imdex, int is_add);
271
272 #endif /* included_vnet_gtpu_h */
273
274
275 /*
276  * fd.io coding-style-patch-verification: ON
277  *
278  * Local Variables:
279  * eval: (c-set-style "gnu")
280  * End:
281  */