FD.io VPP  v20.01-48-g3e0dafb74
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/ip/ip_types_api.h>
26 
27 #include <vnet/ipip/ipip.api_enum.h>
28 #include <vnet/ipip/ipip.api_types.h>
29 
30 #define REPLY_MSG_ID_BASE im->msg_id_base
32 
33 static void
35 {
36  ipip_main_t *im = &ipip_main;
38  int rv = 0;
39  u32 fib_index, sw_if_index = ~0;
41  ip46_address_t src, dst;
42  ip46_type_t itype[2];
43 
44  itype[0] = ip_address_decode (&mp->tunnel.src, &src);
45  itype[1] = ip_address_decode (&mp->tunnel.dst, &dst);
46 
47  if (itype[0] != itype[1])
48  {
49  rv = VNET_API_ERROR_INVALID_PROTOCOL;
50  goto out;
51  }
52 
53  if (ip46_address_is_equal (&src, &dst))
54  {
55  rv = VNET_API_ERROR_SAME_SRC_DST;
56  goto out;
57  }
58 
59  rv = ipip_tunnel_flags_decode (mp->tunnel.flags, &flags);
60 
61  if (rv)
62  goto out;
63 
64  fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]),
65  ntohl (mp->tunnel.table_id));
66 
67  if (~0 == fib_index)
68  {
69  rv = VNET_API_ERROR_NO_SUCH_FIB;
70  }
71  else
72  {
73  rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ?
76  ntohl (mp->tunnel.instance), &src, &dst,
77  fib_index, flags,
78  ip_dscp_decode (mp->tunnel.dscp), &sw_if_index);
79  }
80 
81 out:
82  /* *INDENT-OFF* */
83  REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
84  ({
85  rmp->sw_if_index = ntohl(sw_if_index);
86  }));
87  /* *INDENT-ON* */
88 }
89 
90 static void
92 {
93  ipip_main_t *im = &ipip_main;
94  vl_api_ipip_del_tunnel_reply_t *rmp;
95 
96  int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
97 
98  REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
99 }
100 
101 static void
103 {
104  ipip_main_t *im = &ipip_main;
106  bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
107  fib_table_t *ft;
108  int rv = 0;
109 
110  ft = fib_table_get (t->fib_index, (is_ipv6 ? FIB_PROTOCOL_IP6 :
112 
113  /* *INDENT-OFF* */
114  REPLY_MACRO_DETAILS2(VL_API_IPIP_TUNNEL_DETAILS,
115  ({
118  rmp->tunnel.table_id = htonl (ft->ft_table_id);
119  rmp->tunnel.instance = htonl (t->user_instance);
120  rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
121  rmp->tunnel.dscp = ip_dscp_encode(t->dscp);
122  rmp->tunnel.flags = ipip_tunnel_flags_encode(t->flags);
123  }));
124  /* *INDENT-ON* */
125 }
126 
127 static void
129 {
130  ipip_main_t *im = &ipip_main;
131  ipip_tunnel_t *t;
133 
134  sw_if_index = ntohl (mp->sw_if_index);
135 
136  if (sw_if_index == ~0)
137  {
138  /* *INDENT-OFF* */
139  pool_foreach(t, im->tunnels,
140  ({
141  send_ipip_tunnel_details(t, mp);
142  }));
143  /* *INDENT-ON* */
144  }
145  else
146  {
147  t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
148  if (t)
149  send_ipip_tunnel_details (t, mp);
150  }
151 }
152 
153 static void
155 {
156  ipip_main_t *im = &ipip_main;
158  u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
159  int rv;
160 
161  ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
162  ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
163 
164  if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
165 
166  {
167  rv = VNET_API_ERROR_NO_SUCH_FIB;
168  }
169  else
170  {
171  rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address,
172  mp->ip6_prefix.len,
173  (ip4_address_t *) & mp->ip4_prefix.address,
174  mp->ip4_prefix.len,
175  (ip4_address_t *) & mp->ip4_src,
176  mp->security_check,
177  ip4_fib_index, ip6_fib_index,
178  &sixrd_tunnel_index);
179  }
180 
181  /* *INDENT-OFF* */
182  REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
183  ({
184  rmp->sw_if_index = htonl (sixrd_tunnel_index);
185  }));
186  /* *INDENT-ON* */
187 }
188 
189 static void
191 {
192  ipip_main_t *im = &ipip_main;
193  vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
194 
195  int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
196 
197  REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
198 }
199 
200 /*
201  * ipip_api_hookup
202  * Add vpe's API message handlers to the table.
203  * vlib has already mapped shared memory and
204  * added the client registration handlers.
205  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
206  */
207 /* API definitions */
208 #include <vnet/format_fns.h>
209 #include <vnet/ipip/ipip.api.c>
210 
211 static clib_error_t *
213 {
214  ipip_main_t *im = &ipip_main;
215 
216  /*
217  * Set up the (msg_name, crc, message-id) table
218  */
220 
221  return 0;
222 }
223 
225 
226 /*
227  * fd.io coding-style-patch-verification: ON
228  *
229  * Local Variables:
230  * eval: (c-set-style "gnu")
231  * End:
232  */
vl_api_interface_index_t sw_if_index
Definition: ipip.api:98
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:104
vl_api_interface_index_t sw_if_index
Definition: ipip.api:132
int ipip_tunnel_flags_decode(vl_api_ipip_tunnel_flags_t f, ipip_tunnel_flags_t *o)
A representation of a IPIP tunnel.
Definition: ipip.h:92
ip46_address_t tunnel_src
Definition: ipip.h:100
static void send_ipip_tunnel_details(ipip_tunnel_t *t, vl_api_ipip_tunnel_dump_t *mp)
Definition: ipip_api.c:102
int ipip_add_tunnel(ipip_transport_t transport, u32 instance, ip46_address_t *src, ip46_address_t *dst, u32 fib_index, ipip_tunnel_flags_t flags, ip_dscp_t dscp, u32 *sw_if_indexp)
Definition: ipip.c:414
vl_api_ipip_tunnel_t tunnel
Definition: ipip.api:81
vl_api_ip4_address_t ip4_src
Definition: ipip.api:112
#define REPLY_MACRO2(t, body)
static void vl_api_ipip_6rd_add_tunnel_t_handler(vl_api_ipip_6rd_add_tunnel_t *mp)
Definition: ipip_api.c:154
ipip_tunnel_flags_t flags
Definition: ipip.h:107
vl_api_address_t src
Definition: gre.api:60
static void vl_api_ipip_6rd_del_tunnel_t_handler(vl_api_ipip_6rd_del_tunnel_t *mp)
Definition: ipip_api.c:190
vl_api_interface_index_t sw_if_index
Definition: ipip.api:88
Delete an IP{v4,v6} over IP{v4,v6} tunnel.
Definition: ipip.api:94
static clib_error_t * ipip_api_hookup(vlib_main_t *vm)
Definition: ipip_api.c:212
#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
u32 user_instance
Definition: ipip.h:106
Create an IP{v4,v6} over IP{v4,v6} tunnel.
Definition: ipip.api:77
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:1097
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Definition: ip_types_api.c:161
ip_dscp_t ip_dscp_decode(u8 in)
Definition: ip_types_api.c:99
fib_protocol_t fib_proto_from_ip46(ip46_type_t iproto)
Convert from ip46_type to fib_protocol.
Definition: fib_types.c:326
List all IPIP tunnels.
Definition: ipip.api:138
static void vl_api_ipip_tunnel_dump_t_handler(vl_api_ipip_tunnel_dump_t *mp)
Definition: ipip_api.c:128
ipip_tunnel_t * ipip_tunnel_db_find_by_sw_if_index(u32 sw_if_index)
Definition: ipip.c:382
ipip_tunnel_t * tunnels
Definition: ipip.h:124
#define REPLY_MACRO(t)
int sixrd_del_tunnel(u32 sw_if_index)
Definition: sixrd.c:376
vl_api_address_t dst
Definition: gre.api:61
vlib_main_t * vm
Definition: in2out_ed.c:1810
vl_api_ip4_prefix_t ip4_prefix
Definition: ipip.api:111
static u8 ip46_address_is_equal(const ip46_address_t *ip46_1, const ip46_address_t *ip46_2)
Definition: ip46_address.h:93
ip_dscp_t dscp
Definition: ipip.h:108
#define REPLY_MACRO_DETAILS2(t, body)
ipip_main_t ipip_main
Definition: ipip.c:29
u32 flags
Definition: vhost_user.h:141
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
static void vl_api_ipip_del_tunnel_t_handler(vl_api_ipip_del_tunnel_t *mp)
Definition: ipip_api.c:91
u32 fib_index
Definition: ipip.h:102
ipip_transport_t transport
Definition: ipip.h:98
vl_api_ipip_tunnel_t tunnel
Definition: ipip.api:148
vl_api_interface_index_t sw_if_index
Definition: ipip.api:122
static void vl_api_ipip_add_tunnel_t_handler(vl_api_ipip_add_tunnel_t *mp)
Definition: ipip_api.c:34
enum ipip_tunnel_flags_t_ ipip_tunnel_flags_t
u32 sw_if_index
Definition: ipip.h:104
u16 msg_id_base
Definition: ipip.h:138
vl_api_ipip_tunnel_flags_t ipip_tunnel_flags_encode(ipip_tunnel_flags_t f)
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:29
int ipip_del_tunnel(u32 sw_if_index)
Definition: ipip.c:512
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:142
vl_api_ip6_prefix_t ip6_prefix
Definition: ipip.api:110
u8 ip_dscp_encode(ip_dscp_t dscp)
Definition: ip_types_api.c:105
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:3410
ip46_address_t tunnel_dst
Definition: ipip.h:101
Delete an IPv4 over IPv6 automatic tunnel (6RD)
Definition: ipip.api:128
A protocol Independent FIB table.
Definition: fib_table.h:71
ip46_type_t
Definition: ip46_address.h:22
u8 is_ipv6
Definition: acl_types.api:38