blob: 43a39b82ef02ca2b9ebdf89d02b024637010085c [file] [log] [blame]
Neale Ranns810086d2017-11-05 16:26:46 -08001/*
Neale Rannsd0df49f2018-08-08 01:06:40 -07002 * Copyright (c) 2018 Cisco and/or its affiliates.
Neale Ranns810086d2017-11-05 16:26:46 -08003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/vnet.h>
17#include <vlibmemory/api.h>
18
19#include <vnet/udp/udp_encap.h>
20#include <vnet/fib/fib_table.h>
Neale Rannsd0df49f2018-08-08 01:06:40 -070021#include <vnet/ip/ip_types_api.h>
Neale Ranns810086d2017-11-05 16:26:46 -080022
23#include <vnet/vnet_msg_enum.h>
24
25#define vl_typedefs /* define message structures */
26#include <vnet/vnet_all_api_h.h>
27#undef vl_typedefs
28
29#define vl_endianfun /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_endianfun
32
33/* instantiate all the print functions we know about */
34#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35#define vl_printfun
36#include <vnet/vnet_all_api_h.h>
37#undef vl_printfun
38
39#include <vlibapi/api_helper_macros.h>
40
41
Neale Rannsd0df49f2018-08-08 01:06:40 -070042#define foreach_udp_api_msg \
43_(UDP_ENCAP_DEL, udp_encap_del) \
44_(UDP_ENCAP_ADD, udp_encap_add) \
Neale Ranns810086d2017-11-05 16:26:46 -080045_(UDP_ENCAP_DUMP, udp_encap_dump)
46
47static void
Florin Coras6c4dae22018-01-09 06:39:23 -080048send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg,
49 u32 context)
Neale Ranns810086d2017-11-05 16:26:46 -080050{
51 vl_api_udp_encap_details_t *mp;
Neale Ranns810086d2017-11-05 16:26:46 -080052
53 mp = vl_msg_api_alloc (sizeof (*mp));
54 memset (mp, 0, sizeof (*mp));
55 mp->_vl_msg_id = ntohs (VL_API_UDP_ENCAP_DETAILS);
56 mp->context = context;
57
Neale Ranns810086d2017-11-05 16:26:46 -080058 if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto)
59 {
Neale Rannsd0df49f2018-08-08 01:06:40 -070060 clib_memcpy (&mp->udp_encap.src_ip.un.ip4,
61 &ue->ue_hdrs.ip4.ue_ip4.src_address, 4);
62 clib_memcpy (&mp->udp_encap.dst_ip.un.ip4,
63 &ue->ue_hdrs.ip4.ue_ip4.dst_address, 4);
64 mp->udp_encap.dst_ip.af = clib_host_to_net_u32 (ADDRESS_IP4);
65 mp->udp_encap.src_ip.af = clib_host_to_net_u32 (ADDRESS_IP4);
66
67 /* ports aren't byte swapped because they are stored in network
68 * byte order */
69 mp->udp_encap.src_port = ue->ue_hdrs.ip4.ue_udp.src_port;
70 mp->udp_encap.dst_port = ue->ue_hdrs.ip4.ue_udp.dst_port;
Neale Ranns810086d2017-11-05 16:26:46 -080071 }
72 else
73 {
Neale Rannsd0df49f2018-08-08 01:06:40 -070074 clib_memcpy (&mp->udp_encap.src_ip.un.ip6,
75 &ue->ue_hdrs.ip6.ue_ip6.src_address, 16);
76 clib_memcpy (&mp->udp_encap.dst_ip.un.ip6,
77 &ue->ue_hdrs.ip6.ue_ip6.dst_address, 16);
78 mp->udp_encap.dst_ip.af = clib_host_to_net_u32 (ADDRESS_IP6);
79 mp->udp_encap.src_ip.af = clib_host_to_net_u32 (ADDRESS_IP6);
80
81 /* ports aren't byte swapped because they are stored in network
82 * byte order */
83 mp->udp_encap.src_port = ue->ue_hdrs.ip6.ue_udp.src_port;
84 mp->udp_encap.dst_port = ue->ue_hdrs.ip6.ue_udp.dst_port;
Neale Ranns810086d2017-11-05 16:26:46 -080085 }
86
Neale Rannsd0df49f2018-08-08 01:06:40 -070087 mp->udp_encap.table_id =
88 htonl (fib_table_get_table_id (ue->ue_fib_index, ue->ue_ip_proto));
89 mp->udp_encap.id = htonl (ue->ue_id);
Neale Ranns810086d2017-11-05 16:26:46 -080090
Florin Coras6c4dae22018-01-09 06:39:23 -080091 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns810086d2017-11-05 16:26:46 -080092}
93
94static void
95vl_api_udp_encap_dump_t_handler (vl_api_udp_encap_dump_t * mp,
96 vlib_main_t * vm)
97{
Florin Coras6c4dae22018-01-09 06:39:23 -080098 vl_api_registration_t *reg;
Neale Ranns810086d2017-11-05 16:26:46 -080099 udp_encap_t *ue;
100
Florin Coras6c4dae22018-01-09 06:39:23 -0800101 reg = vl_api_client_index_to_registration (mp->client_index);
102 if (!reg)
Neale Ranns810086d2017-11-05 16:26:46 -0800103 return;
104
105 /* *INDENT-OFF* */
106 pool_foreach(ue, udp_encap_pool,
107 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800108 send_udp_encap_details(ue, reg, mp->context);
Neale Ranns810086d2017-11-05 16:26:46 -0800109 }));
Florin Corase86a8ed2018-01-05 03:20:25 -0800110 /* *INDENT-ON* */
Neale Ranns810086d2017-11-05 16:26:46 -0800111}
112
113static void
Neale Rannsd0df49f2018-08-08 01:06:40 -0700114vl_api_udp_encap_add_t_handler (vl_api_udp_encap_add_t * mp, vlib_main_t * vm)
Neale Ranns810086d2017-11-05 16:26:46 -0800115{
Neale Rannsd0df49f2018-08-08 01:06:40 -0700116 vl_api_udp_encap_add_reply_t *rmp;
Neale Ranns810086d2017-11-05 16:26:46 -0800117 ip46_address_t src_ip, dst_ip;
118 u32 fib_index, table_id, ue_id;
119 fib_protocol_t fproto;
Neale Rannsd0df49f2018-08-08 01:06:40 -0700120 ip46_type_t itype;
Neale Ranns810086d2017-11-05 16:26:46 -0800121 int rv = 0;
122
Neale Rannsd0df49f2018-08-08 01:06:40 -0700123 ue_id = ntohl (mp->udp_encap.id);
124 table_id = ntohl (mp->udp_encap.table_id);
Neale Ranns810086d2017-11-05 16:26:46 -0800125
Neale Rannsd0df49f2018-08-08 01:06:40 -0700126 itype = ip_address_decode (&mp->udp_encap.src_ip, &src_ip);
127 itype = ip_address_decode (&mp->udp_encap.dst_ip, &dst_ip);
128 fproto = fib_proto_from_ip46 (itype);
Florin Corase86a8ed2018-01-05 03:20:25 -0800129 fib_index = fib_table_find (fproto, table_id);
Neale Ranns810086d2017-11-05 16:26:46 -0800130
131 if (~0 == fib_index)
132 {
133 rv = VNET_API_ERROR_NO_SUCH_TABLE;
134 goto done;
135 }
136
Neale Rannsd0df49f2018-08-08 01:06:40 -0700137 udp_encap_add_and_lock (ue_id, fproto, fib_index,
138 &src_ip, &dst_ip,
139 ntohs (mp->udp_encap.src_port),
140 ntohs (mp->udp_encap.dst_port),
141 UDP_ENCAP_FIXUP_NONE);
Neale Ranns810086d2017-11-05 16:26:46 -0800142
Florin Corase86a8ed2018-01-05 03:20:25 -0800143done:
Neale Rannsd0df49f2018-08-08 01:06:40 -0700144 REPLY_MACRO (VL_API_UDP_ENCAP_ADD_REPLY);
145}
146
147static void
148vl_api_udp_encap_del_t_handler (vl_api_udp_encap_del_t * mp, vlib_main_t * vm)
149{
150 vl_api_udp_encap_del_reply_t *rmp;
151 int rv = 0;
152
153 udp_encap_unlock (ntohl (mp->id));
154
155 REPLY_MACRO (VL_API_UDP_ENCAP_DEL_REPLY);
Neale Ranns810086d2017-11-05 16:26:46 -0800156}
157
158#define vl_msg_name_crc_list
159#include <vnet/udp/udp.api.h>
160#undef vl_msg_name_crc_list
161
162static void
163setup_message_id_table (api_main_t * am)
164{
165#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
166 foreach_vl_msg_name_crc_udp;
167#undef _
168}
169
170static clib_error_t *
171udp_api_hookup (vlib_main_t * vm)
172{
173 api_main_t *am = &api_main;
174
175#define _(N,n) \
176 vl_msg_api_set_handlers(VL_API_##N, #n, \
177 vl_api_##n##_t_handler, \
178 vl_noop_handler, \
179 vl_api_##n##_t_endian, \
180 vl_api_##n##_t_print, \
181 sizeof(vl_api_##n##_t), 1);
182 foreach_udp_api_msg;
183#undef _
184
185 /*
186 * Set up the (msg_name, crc, message-id) table
187 */
188 setup_message_id_table (am);
189
190 return 0;
191}
192
193VLIB_API_INIT_FUNCTION (udp_api_hookup);
194
195/*
196 * fd.io coding-style-patch-verification: ON
197 *
198 * Local Variables:
199 * eval: (c-set-style "gnu")
200 * End:
201 */