FD.io VPP  v19.08.3-2-gbabecb413
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 #include <vnet/ip/ip_types_api.h>
26 
27 #define vl_typedefs /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
29 #undef vl_typedefs
30 
31 #define vl_endianfun /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_endianfun
34 
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...) vlib_cli_output(handle, __VA_ARGS__)
37 #define vl_printfun
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_printfun
40 
42 
43 #define foreach_vpe_api_msg \
44  _(IPIP_ADD_TUNNEL, ipip_add_tunnel) \
45  _(IPIP_DEL_TUNNEL, ipip_del_tunnel) \
46  _(IPIP_6RD_ADD_TUNNEL, ipip_6rd_add_tunnel) \
47  _(IPIP_6RD_DEL_TUNNEL, ipip_6rd_del_tunnel) \
48  _(IPIP_TUNNEL_DUMP, ipip_tunnel_dump)
49 
50 static void
52 {
54  int rv = 0;
55  u32 fib_index, sw_if_index = ~0;
56  ip46_address_t src, dst;
57  ip46_type_t itype[2];
58 
59  itype[0] = ip_address_decode (&mp->tunnel.src, &src);
60  itype[1] = ip_address_decode (&mp->tunnel.dst, &dst);
61 
62  if (itype[0] != itype[1])
63  {
64  rv = VNET_API_ERROR_INVALID_PROTOCOL;
65  goto out;
66  }
67 
68  if (ip46_address_is_equal (&src, &dst))
69  {
70  rv = VNET_API_ERROR_SAME_SRC_DST;
71  goto out;
72  }
73 
74  fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]),
75  ntohl (mp->tunnel.table_id));
76 
77  if (~0 == fib_index)
78  {
79  rv = VNET_API_ERROR_NO_SUCH_FIB;
80  }
81  else
82  {
83  rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ?
86  ntohl (mp->tunnel.instance), &src, &dst,
87  fib_index, mp->tunnel.tc_tos, &sw_if_index);
88  }
89 
90 out:
91  /* *INDENT-OFF* */
92  REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
93  ({
94  rmp->sw_if_index = ntohl(sw_if_index);
95  }));
96  /* *INDENT-ON* */
97 }
98 
99 static void
101 {
102  vl_api_ipip_del_tunnel_reply_t *rmp;
103 
104  int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
105 
106  REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
107 }
108 
109 static void
112 {
114  bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
115  fib_table_t *ft;
116 
117  rmp = vl_msg_api_alloc (sizeof (*rmp));
118  clib_memset (rmp, 0, sizeof (*rmp));
119  rmp->_vl_msg_id = htons (VL_API_IPIP_TUNNEL_DETAILS);
120 
123 
124  ft = fib_table_get (t->fib_index, (is_ipv6 ? FIB_PROTOCOL_IP6 :
126 
127  rmp->tunnel.table_id = htonl (ft->ft_table_id);
128  rmp->tunnel.instance = htonl (t->user_instance);
129  rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
130  rmp->context = context;
131 
132  vl_api_send_msg (reg, (u8 *) rmp);
133 }
134 
135 static void
137 {
140  ipip_tunnel_t *t;
142 
144  if (!reg)
145  return;
146 
147  sw_if_index = ntohl (mp->sw_if_index);
148 
149  if (sw_if_index == ~0)
150  {
151  /* *INDENT-OFF* */
152  pool_foreach(t, gm->tunnels,
153  ({
154  send_ipip_tunnel_details(t, reg, mp->context);
155  }));
156  /* *INDENT-ON* */
157  }
158  else
159  {
160  t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
161  if (t)
162  send_ipip_tunnel_details (t, reg, mp->context);
163  }
164 }
165 
166 static void
168 {
170  u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
171  int rv;
172 
173  sixrd_tunnel_index = ~0;
174  ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
175  ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
176 
177  if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
178 
179  {
180  rv = VNET_API_ERROR_NO_SUCH_FIB;
181  }
182  else
183  {
184  rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address,
185  mp->ip6_prefix.len,
186  (ip4_address_t *) & mp->ip4_prefix.address,
187  mp->ip4_prefix.len,
188  (ip4_address_t *) & mp->ip4_src,
189  mp->security_check,
190  ip4_fib_index, ip6_fib_index,
191  &sixrd_tunnel_index);
192  }
193 
194  /* *INDENT-OFF* */
195  REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
196  ({
197  rmp->sw_if_index = htonl (sixrd_tunnel_index);
198  }));
199  /* *INDENT-ON* */
200 }
201 
202 static void
204 {
205  vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
206 
207  int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
208 
209  REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
210 }
211 
212 /*
213  * ipip_api_hookup
214  * Add vpe's API message handlers to the table.
215  * vlib has already mapped shared memory and
216  * added the client registration handlers.
217  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
218  */
219 #define vl_msg_name_crc_list
220 #include <vnet/vnet_all_api_h.h>
221 #undef vl_msg_name_crc_list
222 
223 static void
225 {
226 #define _(id, n, crc) vl_msg_api_add_msg_name_crc(am, #n "_" #crc, id);
227  foreach_vl_msg_name_crc_ipip;
228 #undef _
229 }
230 
231 static clib_error_t *
233 {
234  api_main_t *am = &api_main;
235 
236 #define _(N, n) \
237  vl_msg_api_set_handlers(VL_API_##N, #n, vl_api_##n##_t_handler, \
238  vl_noop_handler, vl_api_##n##_t_endian, \
239  vl_api_##n##_t_print, sizeof(vl_api_##n##_t), 1);
241 #undef _
242 
243  /*
244  * Set up the (msg_name, crc, message-id) table
245  */
247 
248  return 0;
249 }
250 
252 
253 /*
254  * fd.io coding-style-patch-verification: ON
255  *
256  * Local Variables:
257  * eval: (c-set-style "gnu")
258  * End:
259  */
vl_api_interface_index_t sw_if_index
Definition: ipip.api:94
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 ip4_fib_index, u32 ip6_fib_index, u32 *sw_if_index)
Definition: sixrd.c:271
Create an IPv4 over IPv6 automatic tunnel (6RD)
Definition: ipip.api:100
vl_api_interface_index_t sw_if_index
Definition: ipip.api:128
A representation of a IPIP tunnel.
Definition: ipip.h:70
int ipip_add_tunnel(ipip_transport_t transport, u32 instance, ip46_address_t *src, ip46_address_t *dst, u32 fib_index, u8 tc_tos, u32 *sw_if_indexp)
Definition: ipip.c:385
ip46_address_t tunnel_src
Definition: ipip.h:78
vl_api_ipip_tunnel_t tunnel
Definition: ipip.api:77
vl_api_ip4_address_t ip4_src
Definition: ipip.api:108
#define REPLY_MACRO2(t, body)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
static void vl_api_ipip_6rd_add_tunnel_t_handler(vl_api_ipip_6rd_add_tunnel_t *mp)
Definition: ipip_api.c:167
vl_api_address_t src
Definition: gre.api:51
static void vl_api_ipip_6rd_del_tunnel_t_handler(vl_api_ipip_6rd_del_tunnel_t *mp)
Definition: ipip_api.c:203
void * vl_msg_api_alloc(int nbytes)
vl_api_interface_index_t sw_if_index
Definition: ipip.api:84
unsigned char u8
Definition: types.h:56
Delete an IP{v4,v6} over IP{v4,v6} tunnel.
Definition: ipip.api:90
static clib_error_t * ipip_api_hookup(vlib_main_t *vm)
Definition: ipip_api.c:232
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
u32 user_instance
Definition: ipip.h:84
Create an IP{v4,v6} over IP{v4,v6} tunnel.
Definition: ipip.api:73
unsigned int u32
Definition: types.h:88
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:1080
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Definition: ip_types_api.c:161
static void send_ipip_tunnel_details(ipip_tunnel_t *t, vl_api_registration_t *reg, u32 context)
Definition: ipip_api.c:110
fib_protocol_t fib_proto_from_ip46(ip46_type_t iproto)
Convert from ip46_type to fib_protocol.
Definition: fib_types.c:313
List all IPIP tunnels.
Definition: ipip.api:134
#define gm
Definition: dlmalloc.c:1217
static void vl_api_ipip_tunnel_dump_t_handler(vl_api_ipip_tunnel_dump_t *mp)
Definition: ipip_api.c:136
ipip_tunnel_t * ipip_tunnel_db_find_by_sw_if_index(u32 sw_if_index)
Definition: ipip.c:353
ipip_tunnel_t * tunnels
Definition: ipip.h:101
#define REPLY_MACRO(t)
int sixrd_del_tunnel(u32 sw_if_index)
Definition: sixrd.c:376
vl_api_address_t dst
Definition: gre.api:52
vl_api_ip4_prefix_t ip4_prefix
Definition: ipip.api:107
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
ipip_main_t ipip_main
Definition: ipip.c:29
#define foreach_vpe_api_msg
Definition: ipip_api.c:43
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:89
vlib_main_t * vm
Definition: buffer.c:323
static void vl_api_ipip_del_tunnel_t_handler(vl_api_ipip_del_tunnel_t *mp)
Definition: ipip_api.c:100
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
u32 fib_index
Definition: ipip.h:80
ipip_transport_t transport
Definition: ipip.h:76
ip46_type_t
Definition: ip6_packet.h:70
vl_api_ipip_tunnel_t tunnel
Definition: ipip.api:144
vl_api_interface_index_t sw_if_index
Definition: ipip.api:118
static void vl_api_ipip_add_tunnel_t_handler(vl_api_ipip_add_tunnel_t *mp)
Definition: ipip_api.c:51
u32 sw_if_index
Definition: ipip.h:82
#define ip46_address_is_equal(a1, a2)
Definition: ip6_packet.h:94
static void setup_message_id_table(api_main_t *am)
Definition: ipip_api.c:224
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:477
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:178
VLIB_API_INIT_FUNCTION(ipip_api_hookup)
vl_api_interface_index_t sw_if_index
Definition: ipip.api:138
vl_api_ip6_prefix_t ip6_prefix
Definition: ipip.api:106
ip46_address_t tunnel_dst
Definition: ipip.h:79
u32 context
Definition: gre.api:45
api_main_t api_main
Definition: api_shared.c:35
Delete an IPv4 over IPv6 automatic tunnel (6RD)
Definition: ipip.api:124
A protocol Independent FIB table.
Definition: fib_table.h:69