FD.io VPP  v21.06-3-gbb25fbf28
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/gre/gre.api_enum.h>
32 #include <vnet/gre/gre.api_types.h>
33 
34 #define REPLY_MSG_ID_BASE gre_main.msg_id_base
36 
37 static int
38 gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t * out)
39 {
40  switch (in)
41  {
42 #define _(n, v) \
43  case GRE_API_TUNNEL_TYPE_##n: \
44  *out = GRE_TUNNEL_TYPE_##n; \
45  return (0);
47 #undef _
48  }
49 
50  return (VNET_API_ERROR_INVALID_VALUE);
51 }
52 
53 static vl_api_gre_tunnel_type_t
55 {
56  vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
57 
58  switch (in)
59  {
60 #define _(n, v) \
61  case GRE_TUNNEL_TYPE_##n: \
62  out = GRE_API_TUNNEL_TYPE_##n; \
63  break;
65 #undef _
66  }
67 
68  return (out);
69 }
70 
73 {
74  vnet_gre_tunnel_add_del_args_t _a = { }, *a = &_a;
77  u32 sw_if_index = ~0;
78  ip46_type_t itype[2];
79  int rv = 0;
80 
81  itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
82  itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
83 
84  if (itype[0] != itype[1])
85  {
86  rv = VNET_API_ERROR_INVALID_PROTOCOL;
87  goto out;
88  }
89 
90  if (ip46_address_is_equal (&a->src, &a->dst))
91  {
92  rv = VNET_API_ERROR_SAME_SRC_DST;
93  goto out;
94  }
95 
96  rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
97 
98  if (rv)
99  goto out;
100 
101  rv = tunnel_mode_decode (mp->tunnel.mode, &a->mode);
102 
103  if (rv)
104  goto out;
105 
107 
108  if (rv)
109  goto out;
110 
111  a->is_add = mp->is_add;
112  a->is_ipv6 = (itype[0] == IP46_TYPE_IP6);
113  a->instance = ntohl (mp->tunnel.instance);
114  a->session_id = ntohs (mp->tunnel.session_id);
115  a->outer_table_id = ntohl (mp->tunnel.outer_table_id);
116  a->flags = flags;
117 
119 
120 out:
121  /* *INDENT-OFF* */
122  REPLY_MACRO2(VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
123  ({
124  rmp->sw_if_index = ntohl (sw_if_index);
125  }));
126  /* *INDENT-ON* */
127 }
128 
129 static void send_gre_tunnel_details
131 {
133  int rv = 0;
134 
135  /* *INDENT-OFF* */
136  REPLY_MACRO_DETAILS2(VL_API_GRE_TUNNEL_DETAILS,
137  ({
140 
141  rmp->tunnel.outer_table_id =
144 
145  rmp->tunnel.type = gre_tunnel_type_encode (t->type);
146  rmp->tunnel.mode = tunnel_mode_encode (t->mode);
147  rmp->tunnel.instance = htonl (t->user_instance);
148  rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
149  rmp->tunnel.session_id = htons (t->session_id);
150  }));
151  /* *INDENT-ON* */
152 }
153 
154 static void
156 {
158  gre_main_t *gm = &gre_main;
159  gre_tunnel_t *t;
161 
163  if (!reg)
164  return;
165 
166  sw_if_index = ntohl (mp->sw_if_index);
167 
168  if (~0 == sw_if_index)
169  {
170  /* *INDENT-OFF* */
171  pool_foreach (t, gm->tunnels)
172  {
174  }
175  /* *INDENT-ON* */
176  }
177 
178  else
179  {
180  if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
181  (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
182  {
183  return;
184  }
185  t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
186  send_gre_tunnel_details (t, mp);
187  }
188 }
189 
190 /*
191  * gre_api_hookup
192  * Add vpe's API message handlers to the table.
193  * vlib has already mapped shared memory and
194  * added the client registration handlers.
195  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
196  */
197 /* API definitions */
198 #include <vnet/format_fns.h>
199 #include <vnet/gre/gre.api.c>
200 
201 static clib_error_t *
203 {
204  /*
205  * Set up the (msg_name, crc, message-id) table
206  */
208 
209  return 0;
210 }
211 
213 
214 /*
215  * fd.io coding-style-patch-verification: ON
216  *
217  * Local Variables:
218  * eval: (c-set-style "gnu")
219  * End:
220  */
gre_tunnel_t::type
gre_tunnel_type_t type
Definition: gre.h:206
gre_tunnel_type_encode
static vl_api_gre_tunnel_type_t gre_tunnel_type_encode(gre_tunnel_type_t in)
Definition: gre_api.c:54
gre_tunnel_t::sw_if_index
u32 sw_if_index
Definition: gre.h:205
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
api.h
tunnel_mode_encode
vl_api_tunnel_mode_t tunnel_mode_encode(tunnel_mode_t in)
Definition: tunnel_types_api.c:85
ntohs
#define ntohs(x)
Definition: af_xdp.bpf.c:29
tunnel_encap_decap_flags_t
enum tunnel_encap_decap_flags_t_ tunnel_encap_decap_flags_t
vl_api_gre_tunnel_dump_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: gre.api:93
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
gre_main
gre_main_t gre_main
Definition: gre.c:26
fib_table_get_table_id
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:1095
gre_main_t
GRE related global data.
Definition: gre.h:242
fib_table.h
gre_api_hookup
static clib_error_t * gre_api_hookup(vlib_main_t *vm)
Definition: gre_api.c:202
tunnel_types_api.h
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vl_api_gre_tunnel_dump_t::client_index
u32 client_index
Definition: gre.api:91
vl_api_gre_tunnel_add_del_reply_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: gre.api:81
ip_address_decode
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:186
gre_tunnel_t::tunnel_dst
fib_prefix_t tunnel_dst
The tunnel's destination/remote address.
Definition: gre.h:199
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: bfd_api.c:451
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(gre_api_hookup)
vl_api_gre_tunnel_details_t
Details response for one of the requested GRE tunnels.
Definition: gre.api:100
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
tunnel_encap_decap_flags_decode
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.
Definition: tunnel_types_api.c:34
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
ip46_address_is_equal
static u8 ip46_address_is_equal(const ip46_address_t *ip46_1, const ip46_address_t *ip46_2)
Definition: ip46_address.h:93
vnet_gre_tunnel_add_del_args_t
Definition: gre.h:364
vl_api_gre_tunnel_add_del_reply_t
Add or delete a single GRE tunnel.
Definition: gre.api:77
gre_tunnel_type_t
enum gre_tunnel_type_t_ gre_tunnel_type_t
The GRE tunnel type.
send_gre_tunnel_details
static void send_gre_tunnel_details(gre_tunnel_t *t, vl_api_gre_tunnel_dump_t *mp)
Definition: gre_api.c:130
vl_api_gre_tunnel_dump_t_handler
static void vl_api_gre_tunnel_dump_t_handler(vl_api_gre_tunnel_dump_t *mp)
Definition: gre_api.c:155
GRE_API_TUNNEL_TYPE_L3
@ GRE_API_TUNNEL_TYPE_L3
Definition: gre.api:27
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
gre_tunnel_t
A representation of a GRE tunnel.
Definition: gre.h:185
interface.h
gre_tunnel_t::session_id
u16 session_id
ERSPAN type 2 session ID, least significant 10 bits of u16.
Definition: gre.h:218
vl_api_gre_tunnel_add_del_t_handler
static void vl_api_gre_tunnel_add_del_t_handler(vl_api_gre_tunnel_add_del_t *mp)
Definition: gre_api.c:72
gre_tunnel_t::mode
tunnel_mode_t mode
Definition: gre.h:207
gre_tunnel_t::tunnel_src
ip46_address_t tunnel_src
The tunnel's source/local address.
Definition: gre.h:195
IP46_TYPE_IP6
@ IP46_TYPE_IP6
Definition: ip46_address.h:27
fib_prefix_t_::fp_addr
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:225
vl_api_gre_tunnel_add_del_t
Add or delete a single GRE tunnel.
Definition: gre.api:64
gm
#define gm
Definition: dlmalloc.c:1219
REPLY_MACRO_DETAILS2
#define REPLY_MACRO_DETAILS2(t, body)
Definition: api_helper_macros.h:117
vl_api_gre_tunnel_details_t::tunnel
vl_api_gre_tunnel_t tunnel
Definition: gre.api:103
vl_api_gre_tunnel_add_del_t::is_add
bool is_add
Definition: gre.api:68
vnet_gre_tunnel_add_del
int vnet_gre_tunnel_add_del(vnet_gre_tunnel_add_del_args_t *a, u32 *sw_if_indexp)
Definition: interface.c:580
format_fns.h
foreach_gre_tunnel_type
@ foreach_gre_tunnel_type
Definition: gre.h:59
u32
unsigned int u32
Definition: types.h:88
api_helper_macros.h
fib_prefix_t_::fp_proto
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:211
vl_api_gre_tunnel_dump_t
Dump details of all or just a single GRE tunnel.
Definition: gre.api:89
vlib_main_t
Definition: main.h:102
ip_address_encode
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:220
clib_error_t
Definition: clib_error.h:21
a
a
Definition: bitmap.h:544
ip46_type_t
ip46_type_t
Definition: ip46_address.h:22
tunnel_mode_decode
int tunnel_mode_decode(vl_api_tunnel_mode_t in, tunnel_mode_t *out)
Definition: tunnel_types_api.c:69
rv
int __clib_unused rv
Definition: application.c:491
IP46_TYPE_ANY
@ IP46_TYPE_ANY
Definition: ip46_address.h:24
vnet.h
api_errno.h
gre_tunnel_t::outer_fib_index
u32 outer_fib_index
The FIB in which the src.dst address are present.
Definition: gre.h:203
gre_tunnel_type_decode
static int gre_tunnel_type_decode(vl_api_gre_tunnel_type_t in, gre_tunnel_type_t *out)
Definition: gre_api.c:38
gre_main_t::msg_id_base
u16 msg_id_base
Definition: gre.h:290
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
gre_tunnel_t::user_instance
u32 user_instance
Definition: gre.h:230
ip_types_api.h
vl_api_gre_tunnel_add_del_t::tunnel
vl_api_gre_tunnel_t tunnel
Definition: gre.api:69
gre.h
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105