FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
ipip_api.c
Go to the documentation of this file.
1 /*
2  * ipip_api.c - ipip api
3  *
4  * Copyright (c) 2018 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 <vlibmemory/api.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/interface.h>
22 #include <vnet/ipip/ipip.h>
23 #include <vnet/vnet.h>
24 #include <vnet/vnet_msg_enum.h>
25 
26 #define vl_typedefs /* define message structures */
27 #include <vnet/vnet_all_api_h.h>
28 #undef vl_typedefs
29 
30 #define vl_endianfun /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
32 #undef vl_endianfun
33 
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output(handle, __VA_ARGS__)
36 #define vl_printfun
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_printfun
39 
41 
42 #define foreach_vpe_api_msg \
43  _(IPIP_ADD_TUNNEL, ipip_add_tunnel) \
44  _(IPIP_DEL_TUNNEL, ipip_del_tunnel) \
45  _(IPIP_6RD_ADD_TUNNEL, ipip_6rd_add_tunnel) \
46  _(IPIP_6RD_DEL_TUNNEL, ipip_6rd_del_tunnel) \
47  _(IPIP_TUNNEL_DUMP, ipip_tunnel_dump)
48 
49 static void
51 {
53  int rv = 0;
54  u32 sw_if_index = ~0;
55  ip46_address_t src = ip46_address_initializer, dst =
57 
58  /* ip addresses sent in network byte order */
59  if (mp->is_ipv6)
60  {
61  clib_memcpy (&src.ip6, mp->src_address, 16);
62  clib_memcpy (&dst.ip6, mp->dst_address, 16);
63  }
64  else
65  {
66  clib_memcpy (&src.ip4, mp->src_address, 4);
67  clib_memcpy (&dst.ip4, mp->dst_address, 4);
68  }
69 
71  ntohl (mp->instance), &src, &dst,
72  ntohl (mp->fib_index), &sw_if_index);
73 
74  /* *INDENT-OFF* */
75  REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
76  ({ rmp->sw_if_index = ntohl(sw_if_index); }));
77  /* *INDENT-ON* */
78 }
79 
80 static void
82 {
83  vl_api_ipip_del_tunnel_reply_t *rmp;
84 
85  int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
86 
87  REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
88 }
89 
90 static void
92  vl_api_registration_t * reg, u32 context)
93 {
95  bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
96  fib_table_t *ft;
97 
98  rmp = vl_msg_api_alloc (sizeof (*rmp));
99  memset (rmp, 0, sizeof (*rmp));
100  rmp->_vl_msg_id = htons (VL_API_IPIP_TUNNEL_DETAILS);
101  if (is_ipv6)
102  {
103  clib_memcpy (rmp->src_address, &t->tunnel_src.ip6.as_u8, 16);
104  clib_memcpy (rmp->dst_address, &t->tunnel_dst.ip6.as_u8, 16);
106  rmp->fib_index = htonl (ft->ft_table_id);
107  }
108  else
109  {
110  clib_memcpy (rmp->src_address, &t->tunnel_src.ip4.as_u8, 4);
111  clib_memcpy (rmp->dst_address, &t->tunnel_dst.ip4.as_u8, 4);
113  rmp->fib_index = htonl (ft->ft_table_id);
114  }
115  rmp->instance = htonl (t->user_instance);
116  rmp->sw_if_index = htonl (t->sw_if_index);
117  rmp->context = context;
118  rmp->is_ipv6 = is_ipv6;
119 
120  vl_api_send_msg (reg, (u8 *) rmp);
121 }
122 
123 static void
125 {
127  ipip_main_t *gm = &ipip_main;
128  ipip_tunnel_t *t;
129  u32 sw_if_index;
130 
132  if (!reg)
133  return;
134 
135  sw_if_index = ntohl (mp->sw_if_index);
136 
137  if (sw_if_index == ~0)
138  {
139  /* *INDENT-OFF* */
140  pool_foreach(t, gm->tunnels,
141  ({ send_ipip_tunnel_details(t, reg, mp->context); }));
142  /* *INDENT-ON* */
143  }
144  else
145  {
146  t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
147  if (t)
148  send_ipip_tunnel_details (t, reg, mp->context);
149  }
150 }
151 
152 static void
154 {
156  u32 sixrd_tunnel_index;
157 
158  int rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix,
159  mp->ip6_prefix_len,
160  (ip4_address_t *) & mp->ip4_prefix,
161  mp->ip4_prefix_len,
162  (ip4_address_t *) & mp->ip4_src,
163  mp->security_check,
164  ntohl (mp->fib_index), &sixrd_tunnel_index);
165 
166  REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY, (
167  {
168  rmp->sw_if_index =
169  htonl
170  (sixrd_tunnel_index);}));
171 }
172 
173 static void
175 {
176  vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
177 
178  int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
179 
180  REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
181 }
182 
183 /*
184  * ipip_api_hookup
185  * Add vpe's API message handlers to the table.
186  * vlib has alread mapped shared memory and
187  * added the client registration handlers.
188  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
189  */
190 #define vl_msg_name_crc_list
191 #include <vnet/vnet_all_api_h.h>
192 #undef vl_msg_name_crc_list
193 
194 static void
196 {
197 #define _(id, n, crc) vl_msg_api_add_msg_name_crc(am, #n "_" #crc, id);
198  foreach_vl_msg_name_crc_ipip;
199 #undef _
200 }
201 
202 static clib_error_t *
204 {
205  api_main_t *am = &api_main;
206 
207 #define _(N, n) \
208  vl_msg_api_set_handlers(VL_API_##N, #n, vl_api_##n##_t_handler, \
209  vl_noop_handler, vl_api_##n##_t_endian, \
210  vl_api_##n##_t_print, sizeof(vl_api_##n##_t), 1);
212 #undef _
213 
214  /*
215  * Set up the (msg_name, crc, message-id) table
216  */
218 
219  return 0;
220 }
221 
223 
224 /*
225  * fd.io coding-style-patch-verification: ON
226  *
227  * Local Variables:
228  * eval: (c-set-style "gnu")
229  * End:
230  */
A representation of a IPIP tunnel.
Definition: ipip.h:69
ip46_address_t tunnel_src
Definition: ipip.h:77
#define REPLY_MACRO2(t, body)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:34
static void vl_api_ipip_6rd_add_tunnel_t_handler(vl_api_ipip_6rd_add_tunnel_t *mp)
Definition: ipip_api.c:153
static void vl_api_ipip_6rd_del_tunnel_t_handler(vl_api_ipip_6rd_del_tunnel_t *mp)
Definition: ipip_api.c:174
void * vl_msg_api_alloc(int nbytes)
static clib_error_t * ipip_api_hookup(vlib_main_t *vm)
Definition: ipip_api.c:203
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:440
u32 user_instance
Definition: ipip.h:83
Create or delete an IPIP tunnel.
Definition: ipip.api:27
static void send_ipip_tunnel_details(ipip_tunnel_t *t, vl_api_registration_t *reg, u32 context)
Definition: ipip_api.c:91
static void vl_api_ipip_tunnel_dump_t_handler(vl_api_ipip_tunnel_dump_t *mp)
Definition: ipip_api.c:124
ipip_tunnel_t * ipip_tunnel_db_find_by_sw_if_index(u32 sw_if_index)
Definition: ipip.c:313
int sixrd_add_tunnel(ip6_address_t *ip6_prefix, u8 ip6_prefix_len, ip4_address_t *ip4_prefix, u8 ip4_prefix_len, ip4_address_t *ip4_src, bool security_check, u32 fib_index, u32 *sw_if_index)
Definition: sixrd.c:281
ipip_tunnel_t * tunnels
Definition: ipip.h:107
#define REPLY_MACRO(t)
int sixrd_del_tunnel(u32 sw_if_index)
Definition: sixrd.c:385
#define ip46_address_initializer
Definition: ip6_packet.h:84
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:199
An API client registration, only in vpp/vlib.
Definition: api_common.h:44
ipip_main_t ipip_main
Definition: ipip.c:28
#define foreach_vpe_api_msg
Definition: ipip_api.c:42
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:89
vlib_main_t * vm
Definition: buffer.c:294
#define clib_memcpy(a, b, c)
Definition: string.h:75
static void vl_api_ipip_del_tunnel_t_handler(vl_api_ipip_del_tunnel_t *mp)
Definition: ipip_api.c:81
int ipip_add_tunnel(ipip_transport_t transport, u32 instance, ip46_address_t *src, ip46_address_t *dst, u32 fib_index, u32 *sw_if_indexp)
Definition: ipip.c:421
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
u32 fib_index
Definition: ipip.h:79
unsigned int u32
Definition: types.h:88
ipip_transport_t transport
Definition: ipip.h:75
static void vl_api_ipip_add_tunnel_t_handler(vl_api_ipip_add_tunnel_t *mp)
Definition: ipip_api.c:50
u32 sw_if_index
Definition: ipip.h:81
unsigned char u8
Definition: types.h:56
static void setup_message_id_table(api_main_t *am)
Definition: ipip_api.c:195
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
int ipip_del_tunnel(u32 sw_if_index)
Definition: ipip.c:521
VLIB_API_INIT_FUNCTION(ipip_api_hookup)
ip46_address_t tunnel_dst
Definition: ipip.h:78
api_main_t api_main
Definition: api_shared.c:35
A protocol Independent FIB table.
Definition: fib_table.h:69