FD.io VPP  v20.01-48-g3e0dafb74
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 #include <vnet/ip/ip_types_api.h>
29 
30 #include <vnet/vnet_msg_enum.h>
31 
32 #define vl_typedefs /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
34 #undef vl_typedefs
35 
36 #define vl_endianfun /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_endianfun
39 
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #define vl_printfun
43 #include <vnet/vnet_all_api_h.h>
44 #undef vl_printfun
45 
47 
48 #define foreach_vpe_api_msg \
49 _(GRE_TUNNEL_ADD_DEL, gre_tunnel_add_del) \
50 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)
51 
52 static int
53 gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t * out)
54 {
55  switch (in)
56  {
57 #define _(n, v) \
58  case GRE_API_TUNNEL_TYPE_##n: \
59  *out = GRE_TUNNEL_TYPE_##n; \
60  return (0);
62 #undef _
63  }
64 
65  return (VNET_API_ERROR_INVALID_VALUE);
66 }
67 
68 static vl_api_gre_tunnel_type_t
70 {
71  vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
72 
73  switch (in)
74  {
75 #define _(n, v) \
76  case GRE_TUNNEL_TYPE_##n: \
77  out = GRE_API_TUNNEL_TYPE_##n; \
78  break;
80 #undef _
81  }
82 
83  return (out);
84 }
85 
86 static int
87 gre_tunnel_mode_decode (vl_api_gre_tunnel_mode_t in, gre_tunnel_mode_t * out)
88 {
89  switch (in)
90  {
91 #define _(n, v) \
92  case GRE_API_TUNNEL_MODE_##n: \
93  *out = GRE_TUNNEL_MODE_##n; \
94  return (0);
96 #undef _
97  }
98 
99  return (VNET_API_ERROR_INVALID_VALUE_2);
100 }
101 
102 static vl_api_gre_tunnel_mode_t
104 {
105  vl_api_gre_tunnel_mode_t out = GRE_API_TUNNEL_MODE_P2P;
106 
107  switch (in)
108  {
109 #define _(n, v) \
110  case GRE_TUNNEL_MODE_##n: \
111  out = GRE_API_TUNNEL_MODE_##n; \
112  break;
114 #undef _
115  }
116 
117  return (out);
118 }
119 
122 {
123  vnet_gre_tunnel_add_del_args_t _a = { }, *a = &_a;
125  u32 sw_if_index = ~0;
126  ip46_type_t itype[2];
127  int rv = 0;
128 
129  itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
130  itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
131 
132  if (itype[0] != itype[1])
133  {
134  rv = VNET_API_ERROR_INVALID_PROTOCOL;
135  goto out;
136  }
137 
138  if (ip46_address_is_equal (&a->src, &a->dst))
139  {
140  rv = VNET_API_ERROR_SAME_SRC_DST;
141  goto out;
142  }
143 
144  rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
145 
146  if (rv)
147  goto out;
148 
149  rv = gre_tunnel_mode_decode (mp->tunnel.mode, &a->mode);
150 
151  if (rv)
152  goto out;
153 
154  a->is_add = mp->is_add;
155  a->is_ipv6 = (itype[0] == IP46_TYPE_IP6);
156  a->instance = ntohl (mp->tunnel.instance);
157  a->session_id = ntohs (mp->tunnel.session_id);
158  a->outer_table_id = ntohl (mp->tunnel.outer_table_id);
159 
160  rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
161 
162 out:
163  /* *INDENT-OFF* */
164  REPLY_MACRO2(VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
165  ({
166  rmp->sw_if_index = ntohl (sw_if_index);
167  }));
168  /* *INDENT-ON* */
169 }
170 
171 static void send_gre_tunnel_details
172  (gre_tunnel_t * t, vl_api_registration_t * reg, u32 context)
173 {
175 
176  rmp = vl_msg_api_alloc (sizeof (*rmp));
177  clib_memset (rmp, 0, sizeof (*rmp));
178  rmp->_vl_msg_id = htons (VL_API_GRE_TUNNEL_DETAILS);
179 
182 
183  rmp->tunnel.outer_table_id =
186 
187  rmp->tunnel.type = gre_tunnel_type_encode (t->type);
188  rmp->tunnel.mode = gre_tunnel_mode_encode (t->mode);
189  rmp->tunnel.instance = htonl (t->user_instance);
190  rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
191  rmp->tunnel.session_id = htons (t->session_id);
192 
193  rmp->context = context;
194 
195  vl_api_send_msg (reg, (u8 *) rmp);
196 }
197 
198 static void
200 {
202  gre_main_t *gm = &gre_main;
203  gre_tunnel_t *t;
205 
207  if (!reg)
208  return;
209 
210  sw_if_index = ntohl (mp->sw_if_index);
211 
212  if (~0 == sw_if_index)
213  {
214  /* *INDENT-OFF* */
215  pool_foreach (t, gm->tunnels,
216  ({
217  send_gre_tunnel_details(t, reg, mp->context);
218  }));
219  /* *INDENT-ON* */
220  }
221  else
222  {
223  if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
224  (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
225  {
226  return;
227  }
229  send_gre_tunnel_details (t, reg, mp->context);
230  }
231 }
232 
233 /*
234  * gre_api_hookup
235  * Add vpe's API message handlers to the table.
236  * vlib has already mapped shared memory and
237  * added the client registration handlers.
238  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
239  */
240 #define vl_msg_name_crc_list
241 #include <vnet/vnet_all_api_h.h>
242 #undef vl_msg_name_crc_list
243 
244 static void
246 {
247 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
248  foreach_vl_msg_name_crc_gre;
249 #undef _
250 }
251 
252 static clib_error_t *
254 {
255  api_main_t *am = vlibapi_get_main ();
256 
257 #define _(N,n) \
258  vl_msg_api_set_handlers(VL_API_##N, #n, \
259  vl_api_##n##_t_handler, \
260  vl_noop_handler, \
261  vl_api_##n##_t_endian, \
262  vl_api_##n##_t_print, \
263  sizeof(vl_api_##n##_t), 1);
265 #undef _
266 
267  /*
268  * Set up the (msg_name, crc, message-id) table
269  */
271 
272  return 0;
273 }
274 
276 
277 /*
278  * fd.io coding-style-patch-verification: ON
279  *
280  * Local Variables:
281  * eval: (c-set-style "gnu")
282  * End:
283  */
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:212
GRE related global data.
Definition: gre.h:252
static clib_error_t * gre_api_hookup(vlib_main_t *vm)
Definition: gre_api.c:253
a
Definition: bitmap.h:538
static int gre_tunnel_type_decode(vl_api_gre_tunnel_type_t in, gre_tunnel_type_t *out)
Definition: gre_api.c:53
#define REPLY_MACRO2(t, body)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
VLIB_API_INIT_FUNCTION(gre_api_hookup)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
int vnet_gre_tunnel_add_del(vnet_gre_tunnel_add_del_args_t *a, u32 *sw_if_indexp)
Definition: interface.c:505
u32 outer_fib_index
The FIB in which the src.dst address are present.
Definition: gre.h:214
static int gre_tunnel_mode_decode(vl_api_gre_tunnel_mode_t in, gre_tunnel_mode_t *out)
Definition: gre_api.c:87
static vl_api_gre_tunnel_type_t gre_tunnel_type_encode(gre_tunnel_type_t in)
Definition: gre_api.c:69
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
u32 * tunnel_index_by_sw_if_index
Mapping from sw_if_index to tunnel index.
Definition: gre.h:287
static void vl_api_gre_tunnel_dump_t_handler(vl_api_gre_tunnel_dump_t *mp)
Definition: gre_api.c:199
u32 sw_if_index
Definition: gre.h:216
vl_api_interface_index_t sw_if_index
Definition: gre.api:76
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:498
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
gre_tunnel_mode_t mode
Definition: gre.h:218
unsigned int u32
Definition: types.h:88
ip46_address_t tunnel_src
The tunnel&#39;s source/local address.
Definition: gre.h:206
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Definition: ip_types_api.c:161
A representation of a GRE tunnel.
Definition: gre.h:196
static void send_gre_tunnel_details(gre_tunnel_t *t, vl_api_registration_t *reg, u32 context)
Definition: gre_api.c:172
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:226
#define gm
Definition: dlmalloc.c:1219
vlib_main_t * vm
Definition: in2out_ed.c:1810
static u8 ip46_address_is_equal(const ip46_address_t *ip46_1, const ip46_address_t *ip46_2)
Definition: ip46_address.h:93
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:225
vl_api_gre_tunnel_t tunnel
Definition: gre.api:89
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
gre_tunnel_type_t type
Definition: gre.h:217
vl_api_gre_tunnel_t tunnel
Definition: gre.api:69
static void setup_message_id_table(api_main_t *am)
Definition: gre_api.c:245
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
static vl_api_gre_tunnel_mode_t gre_tunnel_mode_encode(gre_tunnel_mode_t in)
Definition: gre_api.c:103
vl_api_interface_index_t sw_if_index
Definition: gre.api:83
static void vl_api_gre_tunnel_add_del_t_handler(vl_api_gre_tunnel_add_del_t *mp)
Definition: gre_api.c:121
u32 fib_table_get_table_id(u32 fib_index, fib_protocol_t proto)
Get the Table-ID of the FIB from protocol and index.
Definition: fib_table.c:1086
gre_tunnel_t * tunnels
pool of tunnel instances
Definition: gre.h:257
u32 user_instance
Definition: gre.h:240
enum gre_tunnel_mode_t_ gre_tunnel_mode_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define foreach_vpe_api_msg
Definition: gre_api.c:48
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:178
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:378
enum gre_tunnel_type_t_ gre_tunnel_type_t
The GRE tunnel type.
fib_prefix_t tunnel_dst
The tunnel&#39;s destination/remote address.
Definition: gre.h:210
gre_main_t gre_main
Definition: gre.c:26
u16 session_id
ERSPAN type 2 session ID, least significant 10 bits of u16.
Definition: gre.h:228
ip46_type_t
Definition: ip46_address.h:22