a0ee9ad263b1c8439cf37f66a3fa8d1ae700e5ae
[vpp.git] / vnet / vnet / gre / gre.h
1 /*
2  * gre.h: types/functions for gre.
3  *
4  * Copyright (c) 2012 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef included_gre_h
19 #define included_gre_h
20
21 #include <vnet/vnet.h>
22 #include <vnet/gre/packet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ip/ip4.h>
25 #include <vnet/ip/ip4_packet.h>
26 #include <vnet/pg/pg.h>
27 #include <vnet/ip/format.h>
28 #include <vnet/adj/adj_types.h>
29
30 extern vnet_hw_interface_class_t gre_hw_interface_class;
31
32 typedef enum {
33 #define gre_error(n,s) GRE_ERROR_##n,
34 #include <vnet/gre/error.def>
35 #undef gre_error
36   GRE_N_ERROR,
37 } gre_error_t;
38
39 typedef struct {
40   /* Name (a c string). */
41   char * name;
42
43   /* GRE protocol type in host byte order. */
44   gre_protocol_t protocol;
45
46   /* Node which handles this type. */
47   u32 node_index;
48
49   /* Next index for this type. */
50   u32 next_index;
51 } gre_protocol_info_t;
52
53 typedef struct {
54   /**
55    * Linkage into the FIB object graph
56    */
57   fib_node_t node;
58
59   /**
60    * The tunnel's source/local address
61    */
62   ip4_address_t tunnel_src;
63   /**
64    * The tunnel's destination/remote address
65    */
66   ip4_address_t tunnel_dst;
67   /**
68    * The FIB in which the src.dst address are present
69    */
70   u32 outer_fib_index;
71   u32 hw_if_index;
72   u32 sw_if_index;
73   u8 teb;
74
75   /**
76    * The FIB entry sourced by the tunnel for its destination prefix
77    */
78   fib_node_index_t fib_entry_index;
79
80   /**
81    * The tunnel is a child of the FIB entry for its desintion. This is
82    * so it receives updates when the forwarding information for that entry
83    * changes.
84    * The tunnels sibling index on the FIB entry's dependency list.
85    */
86   u32 sibling_index;
87
88   /**
89    * on a L2 tunnel this is the VLIB arc from the L2-tx to the l2-midchain
90    */
91   u32 l2_tx_arc;
92
93   /**
94    * an L2 tunnel always rquires an L2 midchain. cache here for DP.
95    */
96   adj_index_t l2_adj_index;
97 } gre_tunnel_t;
98
99 typedef struct {
100   /* pool of tunnel instances */
101   gre_tunnel_t *tunnels;
102
103   gre_protocol_info_t * protocol_infos;
104
105   /* Hash tables mapping name/protocol to protocol info index. */
106   uword * protocol_info_by_name, * protocol_info_by_protocol;
107   /* Hash mapping src/dst addr pair to tunnel */
108   uword * tunnel_by_key;
109
110   /* Free vlib hw_if_indices */
111   u32 * free_gre_tunnel_hw_if_indices;
112
113   /* Mapping from sw_if_index to tunnel index */
114   u32 * tunnel_index_by_sw_if_index;
115
116   /* convenience */
117   vlib_main_t * vlib_main;
118   vnet_main_t * vnet_main;
119 } gre_main_t;
120
121 /**
122  * @brief IPv4 and GRE header.
123  *
124 */
125 typedef CLIB_PACKED (struct {
126   ip4_header_t ip4;
127   gre_header_t gre;
128 }) ip4_and_gre_header_t;
129
130 always_inline gre_protocol_info_t *
131 gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
132 {
133   uword * p = hash_get (em->protocol_info_by_protocol, protocol);
134   return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
135 }
136
137 gre_main_t gre_main;
138
139 /* Register given node index to take input for given gre type. */
140 void
141 gre_register_input_type (vlib_main_t * vm,
142                          gre_protocol_t protocol,
143                          u32 node_index);
144
145 extern  clib_error_t * gre_interface_admin_up_down (vnet_main_t * vnm,
146                                                     u32 hw_if_index,
147                                                     u32 flags);
148
149 extern void gre_tunnel_stack (adj_index_t ai);
150 extern void gre_update_adj (vnet_main_t * vnm,
151                             u32 sw_if_index,
152                             adj_index_t ai);
153
154 format_function_t format_gre_protocol;
155 format_function_t format_gre_header;
156 format_function_t format_gre_header_with_length;
157
158 extern vlib_node_registration_t gre_input_node;
159 extern vnet_device_class_t gre_device_class;
160 extern vnet_device_class_t gre_l2_device_class;
161
162 /* Parse gre protocol as 0xXXXX or protocol name.
163    In either host or network byte order. */
164 unformat_function_t unformat_gre_protocol_host_byte_order;
165 unformat_function_t unformat_gre_protocol_net_byte_order;
166
167 /* Parse gre header. */
168 unformat_function_t unformat_gre_header;
169 unformat_function_t unformat_pg_gre_header;
170
171 void
172 gre_register_input_protocol (vlib_main_t * vm,
173                              gre_protocol_t protocol,
174                              u32 node_index);
175
176 /* manually added to the interface output node in gre.c */
177 #define GRE_OUTPUT_NEXT_LOOKUP  1
178
179 typedef struct {
180   u8 is_add;
181
182   ip4_address_t src, dst;
183   u32 outer_fib_id;
184   u8 teb;
185 } vnet_gre_add_del_tunnel_args_t;
186
187 int vnet_gre_add_del_tunnel
188   (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp);
189
190 #endif /* included_gre_h */