FD.io VPP  v17.07.01-10-g3be13f0
Vector Packet Processing
gre_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * gre_api.c - gre api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22 
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 
26 #include <vnet/gre/gre.h>
27 #include <vnet/fib/fib_table.h>
28 
29 #include <vnet/vnet_msg_enum.h>
30 
31 #define vl_typedefs /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_typedefs
34 
35 #define vl_endianfun /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_endianfun
38 
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <vnet/vnet_all_api_h.h>
43 #undef vl_printfun
44 
46 
47 #define foreach_vpe_api_msg \
48 _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel) \
49 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)
50 
53 {
55  int rv = 0;
57  u32 outer_fib_id;
58  u32 p;
59  u32 sw_if_index = ~0;
60 
62  ntohl (mp->outer_fib_id));
63  if (p == ~0)
64  {
65  rv = VNET_API_ERROR_NO_SUCH_FIB;
66  goto out;
67  }
68  outer_fib_id = p;
69 
70  /* Check src & dst are different */
71  if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
72  (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
73  {
74  rv = VNET_API_ERROR_SAME_SRC_DST;
75  goto out;
76  }
77  memset (a, 0, sizeof (*a));
78 
79  a->is_add = mp->is_add;
80  a->teb = mp->teb;
81  a->is_ipv6 = mp->is_ipv6;
82 
83  /* ip addresses sent in network byte order */
84  if (!mp->is_ipv6)
85  {
86  clib_memcpy (&(a->src.ip4), mp->src_address, 4);
87  clib_memcpy (&(a->dst.ip4), mp->dst_address, 4);
88  }
89  else
90  {
91  clib_memcpy (&(a->src.ip6), mp->src_address, 16);
92  clib_memcpy (&(a->dst.ip6), mp->dst_address, 16);
93  }
94 
95  a->outer_fib_id = outer_fib_id;
96  rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
97 
98 out:
99  /* *INDENT-OFF* */
100  REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
101  ({
102  rmp->sw_if_index = ntohl (sw_if_index);
103  }));
104  /* *INDENT-ON* */
105 }
106 
107 static void send_gre_tunnel_details
109 {
111  u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
112  fib_table_t *ft;
113 
114  rmp = vl_msg_api_alloc (sizeof (*rmp));
115  memset (rmp, 0, sizeof (*rmp));
116  rmp->_vl_msg_id = ntohs (VL_API_GRE_TUNNEL_DETAILS);
117  if (!is_ipv6)
118  {
119  clib_memcpy (rmp->src_address, &(t->tunnel_src.ip4.as_u8), 4);
120  clib_memcpy (rmp->dst_address, &(t->tunnel_dst.fp_addr.ip4.as_u8), 4);
122  rmp->outer_fib_id = ft->ft_table_id;
123  }
124  else
125  {
126  clib_memcpy (rmp->src_address, &(t->tunnel_src.ip6.as_u8), 16);
127  clib_memcpy (rmp->dst_address, &(t->tunnel_dst.fp_addr.ip6.as_u8), 16);
129  rmp->outer_fib_id = ft->ft_table_id;
130  }
131  rmp->teb = (GRE_TUNNEL_TYPE_TEB == t->type);
132  rmp->sw_if_index = htonl (t->sw_if_index);
133  rmp->context = context;
134  rmp->is_ipv6 = is_ipv6;
135 
136  vl_msg_api_send_shmem (q, (u8 *) & rmp);
137 }
138 
139 static void
141 {
143  gre_main_t *gm = &gre_main;
144  gre_tunnel_t *t;
145  u32 sw_if_index;
146 
148  if (q == 0)
149  {
150  return;
151  }
152 
153  sw_if_index = ntohl (mp->sw_if_index);
154 
155  if (~0 == sw_if_index)
156  {
157  /* *INDENT-OFF* */
158  pool_foreach (t, gm->tunnels,
159  ({
160  send_gre_tunnel_details(t, q, mp->context);
161  }));
162  /* *INDENT-ON* */
163  }
164  else
165  {
166  if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
167  (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
168  {
169  return;
170  }
171  t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
172  send_gre_tunnel_details (t, q, mp->context);
173  }
174 }
175 
176 /*
177  * gre_api_hookup
178  * Add vpe's API message handlers to the table.
179  * vlib has alread mapped shared memory and
180  * added the client registration handlers.
181  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
182  */
183 #define vl_msg_name_crc_list
184 #include <vnet/vnet_all_api_h.h>
185 #undef vl_msg_name_crc_list
186 
187 static void
189 {
190 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
191  foreach_vl_msg_name_crc_gre;
192 #undef _
193 }
194 
195 static clib_error_t *
197 {
198  api_main_t *am = &api_main;
199 
200 #define _(N,n) \
201  vl_msg_api_set_handlers(VL_API_##N, #n, \
202  vl_api_##n##_t_handler, \
203  vl_noop_handler, \
204  vl_api_##n##_t_endian, \
205  vl_api_##n##_t_print, \
206  sizeof(vl_api_##n##_t), 1);
208 #undef _
209 
210  /*
211  * Set up the (msg_name, crc, message-id) table
212  */
214 
215  return 0;
216 }
217 
219 
220 /*
221  * fd.io coding-style-patch-verification: ON
222  *
223  * Local Variables:
224  * eval: (c-set-style "gnu")
225  * End:
226  */
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:169
GRE related global data.
Definition: gre.h:128
static clib_error_t * gre_api_hookup(vlib_main_t *vm)
Definition: gre_api.c:196
a
Definition: bitmap.h:516
#define REPLY_MACRO2(t, body)
VLIB_API_INIT_FUNCTION(gre_api_hookup)
u32 outer_fib_index
The FIB in which the src.dst address are present.
Definition: gre.h:96
u32 * tunnel_index_by_sw_if_index
Mapping from sw_if_index to tunnel index.
Definition: gre.h:164
static void vl_api_gre_tunnel_dump_t_handler(vl_api_gre_tunnel_dump_t *mp)
Definition: gre_api.c:140
u32 sw_if_index
Definition: gre.h:98
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:376
static void vl_api_gre_add_del_tunnel_t_handler(vl_api_gre_add_del_tunnel_t *mp)
Definition: gre_api.c:52
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1025
ip46_address_t tunnel_src
The tunnel&#39;s source/local address.
Definition: gre.h:88
void * vl_msg_api_alloc(int nbytes)
A representation of a GRE tunnel.
Definition: gre.h:79
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:183
api_main_t api_main
Definition: api_shared.c:35
gre_tunnel_type_t type
Definition: gre.h:99
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:44
static void setup_message_id_table(api_main_t *am)
Definition: gre_api.c:188
#define clib_memcpy(a, b, c)
Definition: string.h:69
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
unsigned int u32
Definition: types.h:88
static void send_gre_tunnel_details(gre_tunnel_t *t, unix_shared_memory_queue_t *q, u32 context)
Definition: gre_api.c:108
gre_tunnel_t * tunnels
pool of tunnel instances
Definition: gre.h:132
int vnet_gre_add_del_tunnel(vnet_gre_add_del_tunnel_args_t *a, u32 *sw_if_indexp)
Definition: interface.c:494
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define foreach_vpe_api_msg
Definition: gre_api.c:47
unsigned char u8
Definition: types.h:56
Transparent Ethernet Bridging - the tunnel is in L2 mode.
Definition: gre.h:66
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:27
ip46_address_t dst
Definition: gre.h:244
fib_prefix_t tunnel_dst
The tunnel&#39;s destination/remote address.
Definition: gre.h:92
gre_main_t gre_main
Definition: gre.c:22
ip46_address_t src
Definition: gre.h:244
A protocol Independent FIB table.
Definition: fib_table.h:29
struct _unix_shared_memory_queue unix_shared_memory_queue_t