blob: ecb461bcdda8424b9a8ca827392344e719ac87bd [file] [log] [blame]
Neale Rannsb8d44812017-11-10 06:53:54 -08001/*
2 *------------------------------------------------------------------
3 * punt_api.c - Punt 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#include <vnet/ip/punt.h>
23
24#include <vnet/vnet_msg_enum.h>
25
26#define vl_typedefs /* define message structures */
27#include <vnet/vnet_all_api_h.h>
28#undef vl_typedefs
29
30#define vl_endianfun /* define message structures */
31#include <vnet/vnet_all_api_h.h>
32#undef vl_endianfun
33
34/* instantiate all the print functions we know about */
35#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36#define vl_printfun
37#include <vnet/vnet_all_api_h.h>
38#undef vl_printfun
39
40#include <vlibapi/api_helper_macros.h>
41
42#define foreach_punt_api_msg \
Pavel Kotuceke88865d2018-11-28 07:42:11 +010043_(SET_PUNT, set_punt) \
Neale Rannsb8d44812017-11-10 06:53:54 -080044_(PUNT_SOCKET_REGISTER, punt_socket_register) \
Pavel Kotuceke88865d2018-11-28 07:42:11 +010045_(PUNT_SOCKET_DEREGISTER, punt_socket_deregister) \
46_(PUNT_DUMP, punt_dump) \
47_(PUNT_SOCKET_DUMP, punt_socket_dump)
Neale Rannsb8d44812017-11-10 06:53:54 -080048
49static void
Pavel Kotuceke88865d2018-11-28 07:42:11 +010050vl_api_set_punt_t_handler (vl_api_set_punt_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -080051{
Pavel Kotuceke88865d2018-11-28 07:42:11 +010052 vl_api_set_punt_reply_t *rmp;
Neale Rannsb8d44812017-11-10 06:53:54 -080053 vlib_main_t *vm = vlib_get_main ();
54 int rv = 0;
55 clib_error_t *error;
56
Pavel Kotuceke88865d2018-11-28 07:42:11 +010057 error = vnet_punt_add_del (vm, mp->punt.ipv, mp->punt.l4_protocol,
58 ntohs (mp->punt.l4_port), mp->is_add);
Neale Rannsb8d44812017-11-10 06:53:54 -080059 if (error)
60 {
61 rv = -1;
62 clib_error_report (error);
63 }
64
Pavel Kotuceke88865d2018-11-28 07:42:11 +010065 REPLY_MACRO (VL_API_SET_PUNT_REPLY);
66}
67
68static void
69vl_api_punt_dump_t_handler (vl_api_punt_dump_t * mp)
70{
71
Neale Rannsb8d44812017-11-10 06:53:54 -080072}
73
74static void
75vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
76{
77 vl_api_punt_socket_register_reply_t *rmp;
78 vlib_main_t *vm = vlib_get_main ();
79 int rv = 0;
80 clib_error_t *error;
Florin Coras6c4dae22018-01-09 06:39:23 -080081 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -080082
Steven Luong99ae31d2019-03-05 15:42:21 -080083 error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
84 mp->punt.ipv, mp->punt.l4_protocol,
85 ntohs (mp->punt.l4_port),
86 (char *) mp->pathname);
Neale Rannsb8d44812017-11-10 06:53:54 -080087 if (error)
88 {
89 rv = -1;
90 clib_error_report (error);
91 }
92
Florin Coras6c4dae22018-01-09 06:39:23 -080093 reg = vl_api_client_index_to_registration (mp->client_index);
94 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -080095 return;
96
97 rmp = vl_msg_api_alloc (sizeof (*rmp));
98 rmp->_vl_msg_id = htons (VL_API_PUNT_SOCKET_REGISTER_REPLY);
99 rmp->context = mp->context;
100 rmp->retval = htonl (rv);
101 char *p = vnet_punt_get_server_pathname ();
102 /* Abstract pathnames start with \0 */
103 memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
Florin Coras6c4dae22018-01-09 06:39:23 -0800104 vl_api_send_msg (reg, (u8 *) rmp);
Neale Rannsb8d44812017-11-10 06:53:54 -0800105}
106
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100107void
108send_punt_socket_details (vl_api_registration_t * reg,
109 u32 context, punt_socket_detail_t * p)
110{
111 vl_api_punt_socket_details_t *mp;
112
113 mp = vl_msg_api_alloc (sizeof (*mp));
114 if (!mp)
115 return;
116
117 clib_memset (mp, 0, sizeof (*mp));
118 mp->_vl_msg_id = ntohs (VL_API_PUNT_SOCKET_DETAILS);
119 mp->context = context;
120 mp->punt.ipv = p->ipv;
121 mp->punt.l4_protocol = p->l4_protocol;
122 mp->punt.l4_port = htons (p->l4_port);
123 memcpy (mp->pathname, p->pathname, sizeof (p->pathname));
124
125 vl_api_send_msg (reg, (u8 *) mp);
126}
127
128static void
129vl_api_punt_socket_dump_t_handler (vl_api_punt_socket_dump_t * mp)
130{
131 vl_api_registration_t *reg;
132 punt_socket_detail_t *p, *ps;
133 int rv __attribute__ ((unused)) = 0;
134
135 reg = vl_api_client_index_to_registration (mp->client_index);
136 if (!reg)
137 return;
138
139 ps = punt_socket_entries (mp->is_ipv6);
140 /* *INDENT-OFF* */
141 vec_foreach (p, ps)
142 {
143 send_punt_socket_details (reg, mp->context, p);
144 }
145 /* *INDENT-ON* */
146 vec_free (ps);
147}
148
Neale Rannsb8d44812017-11-10 06:53:54 -0800149static void
150vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
151{
152 vl_api_punt_socket_deregister_reply_t *rmp;
153 vlib_main_t *vm = vlib_get_main ();
154 int rv = 0;
155 clib_error_t *error;
Florin Coras6c4dae22018-01-09 06:39:23 -0800156 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -0800157
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100158 error = vnet_punt_socket_del (vm, mp->punt.ipv, mp->punt.l4_protocol,
159 ntohs (mp->punt.l4_port));
Neale Rannsb8d44812017-11-10 06:53:54 -0800160 if (error)
161 {
162 rv = -1;
163 clib_error_report (error);
164 }
165
Florin Coras6c4dae22018-01-09 06:39:23 -0800166 reg = vl_api_client_index_to_registration (mp->client_index);
167 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -0800168 return;
169
170 rmp = vl_msg_api_alloc (sizeof (*rmp));
171 rmp->_vl_msg_id = htons (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
172 rmp->context = mp->context;
173 rmp->retval = htonl (rv);
Florin Coras6c4dae22018-01-09 06:39:23 -0800174 vl_api_send_msg (reg, (u8 *) rmp);
Neale Rannsb8d44812017-11-10 06:53:54 -0800175}
176
177#define vl_msg_name_crc_list
178#include <vnet/ip/punt.api.h>
179#undef vl_msg_name_crc_list
180
181static void
182setup_message_id_table (api_main_t * am)
183{
184#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
185 foreach_vl_msg_name_crc_punt;
186#undef _
187}
188
189static clib_error_t *
190punt_api_hookup (vlib_main_t * vm)
191{
192 api_main_t *am = &api_main;
193
194#define _(N,n) \
195 vl_msg_api_set_handlers(VL_API_##N, #n, \
196 vl_api_##n##_t_handler, \
197 vl_noop_handler, \
198 vl_api_##n##_t_endian, \
199 vl_api_##n##_t_print, \
200 sizeof(vl_api_##n##_t), 1);
201 foreach_punt_api_msg;
202#undef _
203
204 /*
205 * Set up the (msg_name, crc, message-id) table
206 */
207 setup_message_id_table (am);
208
209 return 0;
210}
211
212VLIB_API_INIT_FUNCTION (punt_api_hookup);
213
214
215/*
216 * fd.io coding-style-patch-verification: ON
217 *
218 * Local Variables:
219 * eval: (c-set-style "gnu")
220 * End:
221 */