c1b9ddd72bc4d056f8691276cc7e7d4227dffb71
[vpp.git] / vnet / vnet / gre / interface.c
1 /*
2  * gre_interface.c: gre interfaces
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 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/gre/gre.h>
21 #include <vnet/ip/format.h>
22
23 u8 * format_gre_tunnel (u8 * s, va_list * args)
24 {
25   gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
26   gre_main_t * gm = &gre_main;
27
28   s = format (s,
29               "[%d] %U (src) %U (dst) outer_fib_index %d",
30               t - gm->tunnels,
31               format_ip4_address, &t->tunnel_src,
32               format_ip4_address, &t->tunnel_dst,
33               t->outer_fib_index);
34   return s;
35 }
36
37 int vnet_gre_add_del_tunnel
38   (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
39 {
40   gre_main_t * gm = &gre_main;
41   vnet_main_t * vnm = gm->vnet_main;
42   ip4_main_t * im = &ip4_main;
43   gre_tunnel_t * t;
44   vnet_hw_interface_t * hi;
45   u32 hw_if_index, sw_if_index;
46   u32 slot;
47   u32 outer_fib_index;
48   uword * p;
49   u64 key;
50
51   key = (u64)a->src.as_u32 << 32 | (u64)a->dst.as_u32;
52   p = hash_get (gm->tunnel_by_key, key);
53
54   if (a->is_add) {
55     /* check if same src/dst pair exists */
56     if (p)
57       return VNET_API_ERROR_INVALID_VALUE;
58
59     p = hash_get (im->fib_index_by_table_id, a->outer_table_id);
60     if (! p)
61       return VNET_API_ERROR_NO_SUCH_FIB;
62
63     outer_fib_index = p[0];
64
65     pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
66     memset (t, 0, sizeof (*t));
67
68     if (vec_len (gm->free_vxlan_tunnel_hw_if_indices) > 0) {
69         vnet_interface_main_t * im = &vnm->interface_main;
70
71         hw_if_index = gm->free_vxlan_tunnel_hw_if_indices
72           [vec_len (gm->free_vxlan_tunnel_hw_if_indices)-1];
73           _vec_len (gm->free_vxlan_tunnel_hw_if_indices) -= 1;
74
75         hi = vnet_get_hw_interface (vnm, hw_if_index);
76         hi->dev_instance = t - gm->tunnels;
77         hi->hw_instance = hi->dev_instance;
78
79         /* clear old stats of freed tunnel before reuse */
80         sw_if_index = hi->sw_if_index;
81         vnet_interface_counter_lock(im);
82         vlib_zero_combined_counter
83           (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
84         vlib_zero_combined_counter
85           (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
86         vlib_zero_simple_counter
87           (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
88         vnet_interface_counter_unlock(im);
89     } else {
90         hw_if_index = vnet_register_interface
91           (vnm, gre_device_class.index, t - gm->tunnels,
92            gre_hw_interface_class.index,
93            t - gm->tunnels);
94         hi = vnet_get_hw_interface (vnm, hw_if_index);
95         sw_if_index = hi->sw_if_index;
96     }
97
98     t->hw_if_index = hw_if_index;
99     t->outer_fib_index = outer_fib_index;
100     t->sw_if_index = sw_if_index;
101
102     vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
103     gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
104
105     hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
106     hi->per_packet_overhead_bytes =
107       /* preamble */ 8 + /* inter frame gap */ 12;
108
109     /* Standard default gre MTU. */
110     hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
111
112     clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
113     clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
114
115     hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
116
117     slot = vlib_node_add_named_next_with_slot
118       (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
119
120     ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
121
122   } else { /* !is_add => delete */
123     /* tunnel needs to exist */
124     if (! p)
125       return VNET_API_ERROR_NO_SUCH_ENTRY;
126
127     t = pool_elt_at_index (gm->tunnels, p[0]);
128
129     sw_if_index = t->sw_if_index;
130     vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
131     /* make sure tunnel is removed from l2 bd or xconnect */
132     set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
133     vec_add1 (gm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
134     gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
135
136     hash_unset (gm->tunnel_by_key, key);
137     pool_put (gm->tunnels, t);
138   }
139
140   if (sw_if_indexp)
141     *sw_if_indexp = sw_if_index;
142
143   return 0;
144 }
145
146
147 static clib_error_t *
148 create_gre_tunnel_command_fn (vlib_main_t * vm,
149                  unformat_input_t * input,
150                  vlib_cli_command_t * cmd)
151 {
152   unformat_input_t _line_input, * line_input = &_line_input;
153   vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
154   ip4_address_t src, dst;
155   u32 outer_fib_id = 0;
156   int rv;
157   u32 num_m_args = 0;
158   u8 is_add = 1;
159
160   /* Get a line of input. */
161   if (! unformat_user (input, unformat_line_input, line_input))
162     return 0;
163
164   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
165     if (unformat (line_input, "del"))
166       is_add = 0;
167     else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
168       num_m_args++;
169     else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
170       num_m_args++;
171     else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
172       ;
173     else
174       return clib_error_return (0, "unknown input `%U'",
175                                 format_unformat_error, input);
176   }
177   unformat_free (line_input);
178
179   if (num_m_args < 2)
180       return clib_error_return (0, "mandatory argument(s) missing");
181
182   if (memcmp (&src, &dst, sizeof(src)) == 0)
183       return clib_error_return (0, "src and dst are identical");
184
185   memset (a, 0, sizeof (*a));
186   a->is_add = is_add;
187   a->outer_table_id = outer_fib_id;
188   clib_memcpy(&a->src, &src, sizeof(src));
189   clib_memcpy(&a->dst, &dst, sizeof(dst));
190
191   rv = vnet_gre_add_del_tunnel (a, 0);
192
193   switch(rv)
194     {
195     case 0:
196       break;
197     case VNET_API_ERROR_INVALID_VALUE:
198       return clib_error_return (0, "GRE tunnel already exists...");
199     case VNET_API_ERROR_NO_SUCH_FIB:
200       return clib_error_return (0, "outer fib ID %d doesn't exist\n",
201                                 outer_fib_id);
202     default:
203       return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
204     }
205
206   return 0;
207 }
208
209 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
210   .path = "create gre tunnel",
211   .short_help = "create gre tunnel src <addr> dst <addr> "
212                 "[outer-fib-id <fib>] [del]",
213   .function = create_gre_tunnel_command_fn,
214 };
215
216 static clib_error_t *
217 show_gre_tunnel_command_fn (vlib_main_t * vm,
218                             unformat_input_t * input,
219                             vlib_cli_command_t * cmd)
220 {
221   gre_main_t * gm = &gre_main;
222   gre_tunnel_t * t;
223
224   if (pool_elts (gm->tunnels) == 0)
225     vlib_cli_output (vm, "No GRE tunnels configured...");
226
227   pool_foreach (t, gm->tunnels,
228   ({
229     vlib_cli_output (vm, "%U", format_gre_tunnel, t);
230   }));
231
232   return 0;
233 }
234
235 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
236     .path = "show gre tunnel",
237     .function = show_gre_tunnel_command_fn,
238 };
239
240 /* force inclusion from application's main.c */
241 clib_error_t *gre_interface_init (vlib_main_t *vm)
242 {
243   return 0;
244 }
245 VLIB_INIT_FUNCTION(gre_interface_init);