Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 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> |
| 28 | |
| 29 | #include <vnet/vnet_msg_enum.h> |
| 30 | |
| 31 | #define vl_typedefs /* define message structures */ |
| 32 | #include <vnet/vnet_all_api_h.h> |
| 33 | #undef vl_typedefs |
| 34 | |
| 35 | #define vl_endianfun /* define message structures */ |
| 36 | #include <vnet/vnet_all_api_h.h> |
| 37 | #undef vl_endianfun |
| 38 | |
| 39 | /* instantiate all the print functions we know about */ |
| 40 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 41 | #define vl_printfun |
| 42 | #include <vnet/vnet_all_api_h.h> |
| 43 | #undef vl_printfun |
| 44 | |
| 45 | #include <vlibapi/api_helper_macros.h> |
| 46 | |
| 47 | #define foreach_vpe_api_msg \ |
| 48 | _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel) \ |
| 49 | _(GRE_TUNNEL_DUMP, gre_tunnel_dump) |
| 50 | |
| 51 | static void vl_api_gre_add_del_tunnel_t_handler |
| 52 | (vl_api_gre_add_del_tunnel_t * mp) |
| 53 | { |
| 54 | vl_api_gre_add_del_tunnel_reply_t *rmp; |
| 55 | int rv = 0; |
| 56 | vnet_gre_add_del_tunnel_args_t _a, *a = &_a; |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 57 | u32 sw_if_index = ~0; |
| 58 | |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 59 | /* Check src & dst are different */ |
| 60 | if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) || |
| 61 | (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0)) |
| 62 | { |
| 63 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 64 | goto out; |
| 65 | } |
| 66 | memset (a, 0, sizeof (*a)); |
| 67 | |
| 68 | a->is_add = mp->is_add; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 69 | a->tunnel_type = mp->tunnel_type; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 70 | a->is_ipv6 = mp->is_ipv6; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 71 | a->instance = ntohl (mp->instance); |
| 72 | a->session_id = ntohs (mp->session_id); |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 73 | |
| 74 | /* ip addresses sent in network byte order */ |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 75 | if (!mp->is_ipv6) |
| 76 | { |
| 77 | clib_memcpy (&(a->src.ip4), mp->src_address, 4); |
| 78 | clib_memcpy (&(a->dst.ip4), mp->dst_address, 4); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | clib_memcpy (&(a->src.ip6), mp->src_address, 16); |
| 83 | clib_memcpy (&(a->dst.ip6), mp->dst_address, 16); |
| 84 | } |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 85 | |
John Lo | 20e63bc | 2018-01-08 23:19:18 -0500 | [diff] [blame] | 86 | a->outer_fib_id = ntohl (mp->outer_fib_id); |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 87 | rv = vnet_gre_add_del_tunnel (a, &sw_if_index); |
| 88 | |
| 89 | out: |
| 90 | /* *INDENT-OFF* */ |
| 91 | REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY, |
| 92 | ({ |
| 93 | rmp->sw_if_index = ntohl (sw_if_index); |
| 94 | })); |
| 95 | /* *INDENT-ON* */ |
| 96 | } |
| 97 | |
| 98 | static void send_gre_tunnel_details |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 99 | (gre_tunnel_t * t, vl_api_registration_t * reg, u32 context) |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 100 | { |
| 101 | vl_api_gre_tunnel_details_t *rmp; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 102 | u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0; |
| 103 | fib_table_t *ft; |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 104 | |
| 105 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 106 | memset (rmp, 0, sizeof (*rmp)); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 107 | rmp->_vl_msg_id = htons (VL_API_GRE_TUNNEL_DETAILS); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 108 | if (!is_ipv6) |
| 109 | { |
| 110 | clib_memcpy (rmp->src_address, &(t->tunnel_src.ip4.as_u8), 4); |
| 111 | clib_memcpy (rmp->dst_address, &(t->tunnel_dst.fp_addr.ip4.as_u8), 4); |
| 112 | ft = fib_table_get (t->outer_fib_index, FIB_PROTOCOL_IP4); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 113 | rmp->outer_fib_id = htonl (ft->ft_table_id); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 114 | } |
| 115 | else |
| 116 | { |
| 117 | clib_memcpy (rmp->src_address, &(t->tunnel_src.ip6.as_u8), 16); |
| 118 | clib_memcpy (rmp->dst_address, &(t->tunnel_dst.fp_addr.ip6.as_u8), 16); |
| 119 | ft = fib_table_get (t->outer_fib_index, FIB_PROTOCOL_IP6); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 120 | rmp->outer_fib_id = htonl (ft->ft_table_id); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 121 | } |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 122 | rmp->tunnel_type = t->type; |
| 123 | rmp->instance = htonl (t->user_instance); |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 124 | rmp->sw_if_index = htonl (t->sw_if_index); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 125 | rmp->session_id = htons (t->session_id); |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 126 | rmp->context = context; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 127 | rmp->is_ipv6 = is_ipv6; |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 128 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 129 | vl_api_send_msg (reg, (u8 *) rmp); |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void |
| 133 | vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp) |
| 134 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 135 | vl_api_registration_t *reg; |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 136 | gre_main_t *gm = &gre_main; |
| 137 | gre_tunnel_t *t; |
| 138 | u32 sw_if_index; |
| 139 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 140 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 141 | if (!reg) |
| 142 | return; |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 143 | |
| 144 | sw_if_index = ntohl (mp->sw_if_index); |
| 145 | |
| 146 | if (~0 == sw_if_index) |
| 147 | { |
| 148 | /* *INDENT-OFF* */ |
| 149 | pool_foreach (t, gm->tunnels, |
| 150 | ({ |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 151 | send_gre_tunnel_details(t, reg, mp->context); |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 152 | })); |
| 153 | /* *INDENT-ON* */ |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) || |
| 158 | (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) |
| 159 | { |
| 160 | return; |
| 161 | } |
| 162 | t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]]; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 163 | send_gre_tunnel_details (t, reg, mp->context); |
Pavel Kotucek | 1099b0d | 2016-12-20 08:13:14 +0100 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * gre_api_hookup |
| 169 | * Add vpe's API message handlers to the table. |
| 170 | * vlib has alread mapped shared memory and |
| 171 | * added the client registration handlers. |
| 172 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 173 | */ |
| 174 | #define vl_msg_name_crc_list |
| 175 | #include <vnet/vnet_all_api_h.h> |
| 176 | #undef vl_msg_name_crc_list |
| 177 | |
| 178 | static void |
| 179 | setup_message_id_table (api_main_t * am) |
| 180 | { |
| 181 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 182 | foreach_vl_msg_name_crc_gre; |
| 183 | #undef _ |
| 184 | } |
| 185 | |
| 186 | static clib_error_t * |
| 187 | gre_api_hookup (vlib_main_t * vm) |
| 188 | { |
| 189 | api_main_t *am = &api_main; |
| 190 | |
| 191 | #define _(N,n) \ |
| 192 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 193 | vl_api_##n##_t_handler, \ |
| 194 | vl_noop_handler, \ |
| 195 | vl_api_##n##_t_endian, \ |
| 196 | vl_api_##n##_t_print, \ |
| 197 | sizeof(vl_api_##n##_t), 1); |
| 198 | foreach_vpe_api_msg; |
| 199 | #undef _ |
| 200 | |
| 201 | /* |
| 202 | * Set up the (msg_name, crc, message-id) table |
| 203 | */ |
| 204 | setup_message_id_table (am); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | VLIB_API_INIT_FUNCTION (gre_api_hookup); |
| 210 | |
| 211 | /* |
| 212 | * fd.io coding-style-patch-verification: ON |
| 213 | * |
| 214 | * Local Variables: |
| 215 | * eval: (c-set-style "gnu") |
| 216 | * End: |
| 217 | */ |