2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/tunnel/tunnel_types_api.h>
29 #include <vnet/ip/ip_types_api.h>
31 #include <gre/gre.api_enum.h>
32 #include <gre/gre.api_types.h>
34 #define REPLY_MSG_ID_BASE gre_main.msg_id_base
35 #include <vlibapi/api_helper_macros.h>
38 gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t *out)
43 case GRE_API_TUNNEL_TYPE_##n: \
44 *out = GRE_TUNNEL_TYPE_##n; \
46 foreach_gre_tunnel_type
50 return (VNET_API_ERROR_INVALID_VALUE);
53 static vl_api_gre_tunnel_type_t
54 gre_tunnel_type_encode (gre_tunnel_type_t in)
56 vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
61 case GRE_TUNNEL_TYPE_##n: \
62 out = GRE_API_TUNNEL_TYPE_##n; \
64 foreach_gre_tunnel_type
72 vl_api_gre_tunnel_add_del_t_handler (vl_api_gre_tunnel_add_del_t *mp)
74 vnet_gre_tunnel_add_del_args_t _a = {}, *a = &_a;
75 vl_api_gre_tunnel_add_del_reply_t *rmp;
76 tunnel_encap_decap_flags_t flags;
81 itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
82 itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
84 if (itype[0] != itype[1])
86 rv = VNET_API_ERROR_INVALID_PROTOCOL;
90 if (ip46_address_is_equal (&a->src, &a->dst))
92 rv = VNET_API_ERROR_SAME_SRC_DST;
96 rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
101 rv = tunnel_mode_decode (mp->tunnel.mode, &a->mode);
106 rv = tunnel_encap_decap_flags_decode (mp->tunnel.flags, &flags);
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);
118 rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
121 REPLY_MACRO2 (VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
122 ({ rmp->sw_if_index = ntohl (sw_if_index); }));
126 send_gre_tunnel_details (gre_tunnel_t *t, vl_api_gre_tunnel_dump_t *mp)
128 vl_api_gre_tunnel_details_t *rmp;
130 REPLY_MACRO_DETAILS2 (
131 VL_API_GRE_TUNNEL_DETAILS, ({
132 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
133 ip_address_encode (&t->tunnel_dst.fp_addr, IP46_TYPE_ANY,
136 rmp->tunnel.outer_table_id = htonl (
137 fib_table_get_table_id (t->outer_fib_index, t->tunnel_dst.fp_proto));
139 rmp->tunnel.type = gre_tunnel_type_encode (t->type);
140 rmp->tunnel.mode = tunnel_mode_encode (t->mode);
141 rmp->tunnel.flags = tunnel_encap_decap_flags_encode (t->flags);
142 rmp->tunnel.instance = htonl (t->user_instance);
143 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
144 rmp->tunnel.session_id = htons (t->session_id);
149 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t *mp)
151 vl_api_registration_t *reg;
152 gre_main_t *gm = &gre_main;
156 reg = vl_api_client_index_to_registration (mp->client_index);
160 sw_if_index = ntohl (mp->sw_if_index);
162 if (~0 == sw_if_index)
164 pool_foreach (t, gm->tunnels)
166 send_gre_tunnel_details (t, mp);
172 if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
173 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
177 t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
178 send_gre_tunnel_details (t, mp);
184 * Add vpe's API message handlers to the table.
185 * vlib has already mapped shared memory and
186 * added the client registration handlers.
187 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
189 /* API definitions */
190 #include <vnet/format_fns.h>
191 #include <gre/gre.api.c>
193 static clib_error_t *
194 gre_api_hookup (vlib_main_t *vm)
197 * Set up the (msg_name, crc, message-id) table
199 gre_main.msg_id_base = setup_message_id_table ();
204 VLIB_API_INIT_FUNCTION (gre_api_hookup);
207 * fd.io coding-style-patch-verification: ON
210 * eval: (c-set-style "gnu")