Initial commit of vpp code.
[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
22 int
23 gre_register_interface (vnet_main_t * vnm,
24                         u32 dev_class_index,
25                         ip4_address_t *tunnel_src,
26                         ip4_address_t *tunnel_dst,
27                         u32 outer_fib_id,
28                         u32 * gi_index_return)
29 {
30   gre_main_t * gm = &gre_main;
31   ip4_main_t * im = &ip4_main;
32   gre_tunnel_t * t;
33   vnet_hw_interface_t * hi;
34   u32 hw_if_index;
35   u32 slot;
36   u32 outer_fib_index;
37   uword * p;
38
39   u64 key = (u64)tunnel_src->as_u32 << 32 | (u64)tunnel_dst->as_u32;
40
41   /* check if same src/dst pair exists */
42   if (hash_get (gm->tunnel_by_key, key))
43     return VNET_API_ERROR_INVALID_VALUE;
44
45   p = hash_get (im->fib_index_by_table_id, outer_fib_id);
46   if (! p)
47     return VNET_API_ERROR_NO_SUCH_FIB;
48
49   outer_fib_index = p[0];
50
51   pool_get (gm->tunnels, t);
52   memset (t, 0, sizeof (*t));
53
54   hw_if_index = vnet_register_interface
55     (vnm, gre_device_class.index, t - gm->tunnels,
56      gre_hw_interface_class.index,
57      t - gm->tunnels);
58
59   *gi_index_return = t - gm->tunnels;
60
61   t->hw_if_index = hw_if_index;
62   t->outer_fib_index = outer_fib_index;
63
64   hi = vnet_get_hw_interface (vnm, hw_if_index);
65
66   hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
67   hi->per_packet_overhead_bytes =
68     /* preamble */ 8 + /* inter frame gap */ 12;
69
70   /* Standard default gre MTU. */
71   hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 1500;
72
73   memcpy (&t->tunnel_src, tunnel_src, sizeof (t->tunnel_src));
74   memcpy (&t->tunnel_dst, tunnel_dst, sizeof (t->tunnel_dst));
75
76   hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
77
78   slot = vlib_node_add_named_next_with_slot
79     (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
80
81   ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
82
83   return 0;
84 }
85
86
87 static clib_error_t *
88 create_gre_tunnel_command_fn (vlib_main_t * vm,
89                  unformat_input_t * input,
90                  vlib_cli_command_t * cmd)
91 {
92   unformat_input_t _line_input, * line_input = &_line_input;
93   vnet_main_t * vnm = vnet_get_main();
94   ip4_address_t src, dst;
95   u32 outer_fib_id = 0;
96   int rv;
97   u32 gi_index;
98   u32 num_m_args = 0;
99
100   /* Get a line of input. */
101   if (! unformat_user (input, unformat_line_input, line_input))
102     return 0;
103
104   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
105     if (unformat (line_input, "src %U", unformat_ip4_address, &src))
106       num_m_args++;
107     else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
108       num_m_args++;
109     else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
110       ;
111     else
112       return clib_error_return (0, "unknown input `%U'",
113                                 format_unformat_error, input);
114   }
115   unformat_free (line_input);
116
117   if (num_m_args < 2)
118       return clib_error_return (0, "mandatory argument(s) missing");
119
120   rv = gre_register_interface (vnm, gre_hw_interface_class.index,
121                                       &src, &dst, outer_fib_id, &gi_index);
122
123  switch(rv)
124     {
125     case 0:
126       break;
127     case VNET_API_ERROR_INVALID_VALUE:
128       return clib_error_return (0, "GRE tunnel already exists...");
129     case VNET_API_ERROR_NO_SUCH_FIB:
130       return clib_error_return (0, "outer fib ID %d doesn't exist\n",
131                                 outer_fib_id);
132     default:
133       return clib_error_return (0, "gre_register_interface returned %d", rv);
134     }
135
136   return 0;
137 }
138
139 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
140   .path = "create gre tunnel",
141   .short_help = "create gre tunnel src <addr> dst <addr> [outer-fib-id <fib>]",
142   .function = create_gre_tunnel_command_fn,
143 };
144
145 /* force inclusion from application's main.c */
146 clib_error_t *gre_interface_init (vlib_main_t *vm)
147 {
148   return 0;
149 }
150 VLIB_INIT_FUNCTION(gre_interface_init);