FD.io VPP  v20.09-64-g4f7b92f0a
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>
29 #include <vnet/ip/ip_types_api.h>
30 
31 #include <vnet/vnet_msg_enum.h>
32 
33 #define vl_typedefs /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
35 #undef vl_typedefs
36 
37 #define vl_endianfun /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_endianfun
40 
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #define vl_printfun
44 #include <vnet/vnet_all_api_h.h>
45 #undef vl_printfun
46 
48 
49 #define foreach_vpe_api_msg \
50 _(GRE_TUNNEL_ADD_DEL, gre_tunnel_add_del) \
51 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)
52 
53 static int
54 gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t * out)
55 {
56  switch (in)
57  {
58 #define _(n, v) \
59  case GRE_API_TUNNEL_TYPE_##n: \
60  *out = GRE_TUNNEL_TYPE_##n; \
61  return (0);
63 #undef _
64  }
65 
66  return (VNET_API_ERROR_INVALID_VALUE);
67 }
68 
69 static vl_api_gre_tunnel_type_t
71 {
72  vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
73 
74  switch (in)
75  {
76 #define _(n, v) \
77  case GRE_TUNNEL_TYPE_##n: \
78  out = GRE_API_TUNNEL_TYPE_##n; \
79  break;
81 #undef _
82  }
83 
84  return (out);
85 }
86 
89 {
90  vnet_gre_tunnel_add_del_args_t _a = { }, *a = &_a;
93  u32 sw_if_index = ~0;
94  ip46_type_t itype[2];
95  int rv = 0;
96 
97  itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
98  itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
99 
100  if (itype[0] != itype[1])
101  {
102  rv = VNET_API_ERROR_INVALID_PROTOCOL;
103  goto out;
104  }
105 
106  if (ip46_address_is_equal (&a->src, &a->dst))
107  {
108  rv = VNET_API_ERROR_SAME_SRC_DST;
109  goto out;
110  }
111 
112  rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
113 
114  if (rv)
115  goto out;
116 
117  rv = tunnel_mode_decode (mp->tunnel.mode, &a->mode);
118 
119  if (rv)
120  goto out;
121 
122  rv = tunnel_encap_decap_flags_decode (mp->tunnel.flags, &flags);
123 
124  if (rv)
125  goto out;
126 
127  a->is_add = mp->is_add;
128  a->is_ipv6 = (itype[0] == IP46_TYPE_IP6);
129  a->instance = ntohl (mp->tunnel.instance);
130  a->session_id = ntohs (mp->tunnel.session_id);
131  a->outer_table_id = ntohl (mp->tunnel.outer_table_id);
132  a->flags = flags;
133 
134  rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
135 
136 out:
137  /* *INDENT-OFF* */
138  REPLY_MACRO2(VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
139  ({
140  rmp->sw_if_index = ntohl (sw_if_index);
141  }));
142  /* *INDENT-ON* */
143 }
144 
145 static void send_gre_tunnel_details
146  (gre_tunnel_t * t, vl_api_registration_t * reg, u32 context)
147 {
149 
150  rmp = vl_msg_api_alloc (sizeof (*rmp));
151  clib_memset (rmp, 0, sizeof (*rmp));
152  rmp->_vl_msg_id = htons (VL_API_GRE_TUNNEL_DETAILS);
153 
156 
157  rmp->tunnel.outer_table_id =
160 
161  rmp->tunnel.type = gre_tunnel_type_encode (t->type);
162  rmp->tunnel.mode = tunnel_mode_encode (t->mode);
163  rmp->tunnel.instance = htonl (t->user_instance);
164  rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
165  rmp->tunnel.session_id = htons (t->session_id);
166 
167  rmp->context = context;
168 
169  vl_api_send_msg (reg, (u8 *) rmp);
170 }
171 
172 static void
174 {
176  gre_main_t *gm = &gre_main;
177  gre_tunnel_t *t;
179 
181  if (!reg)
182  return;
183 
184  sw_if_index = ntohl (mp->sw_if_index);
185 
186  if (~0 == sw_if_index)
187  {
188  /* *INDENT-OFF* */
189  pool_foreach (t, gm->tunnels,
190  ({
191  send_gre_tunnel_details(t, reg, mp->context);
192  }));
193  /* *INDENT-ON* */
194  }
195  else
196  {
197  if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
198  (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
199  {
200  return;
201  }
203  send_gre_tunnel_details (t, reg, mp->context);
204  }
205 }
206 
207 /*
208  * gre_api_hookup
209  * Add vpe's API message handlers to the table.
210  * vlib has already mapped shared memory and
211  * added the client registration handlers.
212  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
213  */
214 #define vl_msg_name_crc_list
215 #include <vnet/vnet_all_api_h.h>
216 #undef vl_msg_name_crc_list
217 
218 static void
220 {
221 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
222  foreach_vl_msg_name_crc_gre;
223 #undef _
224 }
225 
226 static clib_error_t *
228 {
229  api_main_t *am = vlibapi_get_main ();
230 
231 #define _(N,n) \
232  vl_msg_api_set_handlers(VL_API_##N, #n, \
233  vl_api_##n##_t_handler, \
234  vl_noop_handler, \
235  vl_api_##n##_t_endian, \
236  vl_api_##n##_t_print, \
237  sizeof(vl_api_##n##_t), 1);
239 #undef _
240 
241  /*
242  * Set up the (msg_name, crc, message-id) table
243  */
245 
246  return 0;
247 }
248 
250 
251 /*
252  * fd.io coding-style-patch-verification: ON
253  *
254  * Local Variables:
255  * eval: (c-set-style "gnu")
256  * End:
257  */
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:212
GRE related global data.
Definition: gre.h:243
static clib_error_t * gre_api_hookup(vlib_main_t *vm)
Definition: gre_api.c:227
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:103
a
Definition: bitmap.h:538
#define ntohs(x)
Definition: af_xdp.bpf.c:29
static int gre_tunnel_type_decode(vl_api_gre_tunnel_type_t in, gre_tunnel_type_t *out)
Definition: gre_api.c:54
#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:526
u32 outer_fib_index
The FIB in which the src.dst address are present.
Definition: gre.h:204
vlib_main_t * vm
Definition: in2out_ed.c:1582
static vl_api_gre_tunnel_type_t gre_tunnel_type_encode(gre_tunnel_type_t in)
Definition: gre_api.c:70
void * vl_msg_api_alloc(int nbytes)
Dump details of all or just a single GRE tunnel.
Definition: gre.api:89
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:278
static void vl_api_gre_tunnel_dump_t_handler(vl_api_gre_tunnel_dump_t *mp)
Definition: gre_api.c:173
u32 sw_if_index
Definition: gre.h:206
vl_api_interface_index_t sw_if_index
Definition: gre.api:81
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
unsigned int u32
Definition: types.h:88
Details response for one of the requested GRE tunnels.
Definition: gre.api:100
ip46_address_t tunnel_src
The tunnel&#39;s source/local address.
Definition: gre.h:196
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:160
A representation of a GRE tunnel.
Definition: gre.h:186
Add or delete a single GRE tunnel.
Definition: gre.api:77
static void send_gre_tunnel_details(gre_tunnel_t *t, vl_api_registration_t *reg, u32 context)
Definition: gre_api.c:146
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
int tunnel_mode_decode(vl_api_tunnel_mode_t in, tunnel_mode_t *out)
enum tunnel_encap_decap_flags_t_ tunnel_encap_decap_flags_t
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:227
vl_api_gre_tunnel_t tunnel
Definition: gre.api:103
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
gre_tunnel_type_t type
Definition: gre.h:207
vl_api_gre_tunnel_t tunnel
Definition: gre.api:69
static void setup_message_id_table(api_main_t *am)
Definition: gre_api.c:219
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
vl_api_interface_index_t sw_if_index
Definition: gre.api:93
static void vl_api_gre_tunnel_add_del_t_handler(vl_api_gre_tunnel_add_del_t *mp)
Definition: gre_api.c:88
vl_api_tunnel_mode_t tunnel_mode_encode(tunnel_mode_t in)
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:248
tunnel_mode_t mode
Definition: gre.h:208
u32 user_instance
Definition: gre.h:231
Add or delete a single GRE tunnel.
Definition: gre.api:64
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define foreach_vpe_api_msg
Definition: gre_api.c:49
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:194
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
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:200
int tunnel_encap_decap_flags_decode(vl_api_tunnel_encap_decap_flags_t f, tunnel_encap_decap_flags_t *o)
Conversion functions to/from (decode/encode) API types to VPP internal types.
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:219
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
ip46_type_t
Definition: ip46_address.h:22