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; |
| 57 | u32 outer_fib_id; |
| 58 | uword *p; |
| 59 | ip4_main_t *im = &ip4_main; |
| 60 | u32 sw_if_index = ~0; |
| 61 | |
| 62 | p = hash_get (im->fib_index_by_table_id, ntohl (mp->outer_fib_id)); |
| 63 | if (!p) |
| 64 | { |
| 65 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 66 | goto out; |
| 67 | } |
| 68 | outer_fib_id = p[0]; |
| 69 | |
| 70 | /* Check src & dst are different */ |
| 71 | if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) || |
| 72 | (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0)) |
| 73 | { |
| 74 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 75 | goto out; |
| 76 | } |
| 77 | memset (a, 0, sizeof (*a)); |
| 78 | |
| 79 | a->is_add = mp->is_add; |
| 80 | a->teb = mp->teb; |
| 81 | |
| 82 | /* ip addresses sent in network byte order */ |
| 83 | clib_memcpy (&(a->src), mp->src_address, 4); |
| 84 | clib_memcpy (&(a->dst), mp->dst_address, 4); |
| 85 | |
| 86 | a->outer_fib_id = outer_fib_id; |
| 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 |
| 99 | (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context) |
| 100 | { |
| 101 | vl_api_gre_tunnel_details_t *rmp; |
| 102 | ip4_main_t *im = &ip4_main; |
| 103 | |
| 104 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 105 | memset (rmp, 0, sizeof (*rmp)); |
| 106 | rmp->_vl_msg_id = ntohs (VL_API_GRE_TUNNEL_DETAILS); |
| 107 | clib_memcpy (rmp->src_address, &(t->tunnel_src), 4); |
| 108 | clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4); |
| 109 | rmp->outer_fib_id = htonl (im->fibs[t->outer_fib_index].ft_table_id); |
| 110 | rmp->teb = (GRE_TUNNEL_TYPE_TEB == t->type); |
| 111 | rmp->sw_if_index = htonl (t->sw_if_index); |
| 112 | rmp->context = context; |
| 113 | |
| 114 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp) |
| 119 | { |
| 120 | unix_shared_memory_queue_t *q; |
| 121 | gre_main_t *gm = &gre_main; |
| 122 | gre_tunnel_t *t; |
| 123 | u32 sw_if_index; |
| 124 | |
| 125 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 126 | if (q == 0) |
| 127 | { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | sw_if_index = ntohl (mp->sw_if_index); |
| 132 | |
| 133 | if (~0 == sw_if_index) |
| 134 | { |
| 135 | /* *INDENT-OFF* */ |
| 136 | pool_foreach (t, gm->tunnels, |
| 137 | ({ |
| 138 | send_gre_tunnel_details(t, q, mp->context); |
| 139 | })); |
| 140 | /* *INDENT-ON* */ |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) || |
| 145 | (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) |
| 146 | { |
| 147 | return; |
| 148 | } |
| 149 | t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]]; |
| 150 | send_gre_tunnel_details (t, q, mp->context); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * gre_api_hookup |
| 156 | * Add vpe's API message handlers to the table. |
| 157 | * vlib has alread mapped shared memory and |
| 158 | * added the client registration handlers. |
| 159 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 160 | */ |
| 161 | #define vl_msg_name_crc_list |
| 162 | #include <vnet/vnet_all_api_h.h> |
| 163 | #undef vl_msg_name_crc_list |
| 164 | |
| 165 | static void |
| 166 | setup_message_id_table (api_main_t * am) |
| 167 | { |
| 168 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 169 | foreach_vl_msg_name_crc_gre; |
| 170 | #undef _ |
| 171 | } |
| 172 | |
| 173 | static clib_error_t * |
| 174 | gre_api_hookup (vlib_main_t * vm) |
| 175 | { |
| 176 | api_main_t *am = &api_main; |
| 177 | |
| 178 | #define _(N,n) \ |
| 179 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 180 | vl_api_##n##_t_handler, \ |
| 181 | vl_noop_handler, \ |
| 182 | vl_api_##n##_t_endian, \ |
| 183 | vl_api_##n##_t_print, \ |
| 184 | sizeof(vl_api_##n##_t), 1); |
| 185 | foreach_vpe_api_msg; |
| 186 | #undef _ |
| 187 | |
| 188 | /* |
| 189 | * Set up the (msg_name, crc, message-id) table |
| 190 | */ |
| 191 | setup_message_id_table (am); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | VLIB_API_INIT_FUNCTION (gre_api_hookup); |
| 197 | |
| 198 | /* |
| 199 | * fd.io coding-style-patch-verification: ON |
| 200 | * |
| 201 | * Local Variables: |
| 202 | * eval: (c-set-style "gnu") |
| 203 | * End: |
| 204 | */ |