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