http: fix client receiving large data
[vpp.git] / src / vnet / ip-neighbor / ip_neighbor_api.c
1 /*
2  *------------------------------------------------------------------
3  * ip_api.c - vnet ip 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 <stddef.h>
21
22 #include <vnet/ip-neighbor/ip_neighbor.h>
23 #include <vnet/ip-neighbor/ip_neighbor_watch.h>
24 #include <vnet/ip/ip_types_api.h>
25 #include <vnet/ethernet/ethernet_types_api.h>
26
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
29
30 #include <vnet/ip-neighbor/ip_neighbor.api_enum.h>
31 #include <vnet/ip-neighbor/ip_neighbor.api_types.h>
32
33 static u16 msg_id_base;
34 #define REPLY_MSG_ID_BASE msg_id_base
35
36 #include <vlibapi/api_helper_macros.h>
37
38 #include <vnet/format_fns.h>
39
40 static vl_api_ip_neighbor_flags_t
41 ip_neighbor_flags_encode (ip_neighbor_flags_t f)
42 {
43   vl_api_ip_neighbor_flags_t v = IP_API_NEIGHBOR_FLAG_NONE;
44
45   if (f & IP_NEIGHBOR_FLAG_STATIC)
46     v |= IP_API_NEIGHBOR_FLAG_STATIC;
47   if (f & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY)
48     v |= IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY;
49
50   return (v);
51 }
52
53 static void
54 ip_neighbor_encode (vl_api_ip_neighbor_t * api, const ip_neighbor_t * ipn)
55 {
56   api->sw_if_index = htonl (ipn->ipn_key->ipnk_sw_if_index);
57   api->flags = ip_neighbor_flags_encode (ipn->ipn_flags);
58
59   ip_address_encode2 (&ipn->ipn_key->ipnk_ip, &api->ip_address);
60   mac_address_encode (&ipn->ipn_mac, api->mac_address);
61 }
62
63 void
64 ip_neighbor_handle_event (ip_neighbor_event_t * ipne)
65 {
66   vl_api_registration_t *reg;
67   ip_neighbor_t *ipn;
68
69   ipn = &ipne->ipne_nbr;
70
71   if (NULL == ipn)
72     /* Client can cancel, die, etc. */
73     return;
74
75   /* Customer(s) requesting event for this neighbor */
76   reg = vl_api_client_index_to_registration (ipne->ipne_watch.ipw_client);
77   if (!reg)
78     return;
79
80   if (vl_api_can_send_msg (reg))
81     {
82       if (1 == ipne->ipne_watch.ipw_api_version)
83         {
84           vl_api_ip_neighbor_event_t *mp;
85
86           mp = vl_msg_api_alloc (sizeof (*mp));
87           clib_memset (mp, 0, sizeof (*mp));
88           mp->_vl_msg_id =
89             ntohs (VL_API_IP_NEIGHBOR_EVENT + REPLY_MSG_ID_BASE);
90           mp->client_index = ipne->ipne_watch.ipw_client;
91           mp->pid = ipne->ipne_watch.ipw_pid;
92
93           ip_neighbor_encode (&mp->neighbor, ipn);
94
95           vl_api_send_msg (reg, (u8 *) mp);
96         }
97       else if (2 == ipne->ipne_watch.ipw_api_version)
98         {
99           vl_api_ip_neighbor_event_v2_t *mp;
100
101           mp = vl_msg_api_alloc (sizeof (*mp));
102           clib_memset (mp, 0, sizeof (*mp));
103           mp->_vl_msg_id =
104             ntohs (VL_API_IP_NEIGHBOR_EVENT_V2 + REPLY_MSG_ID_BASE);
105           mp->client_index = ipne->ipne_watch.ipw_client;
106           mp->pid = ipne->ipne_watch.ipw_pid;
107           mp->flags = clib_host_to_net_u32 (ipne->ipne_flags);
108
109           ip_neighbor_encode (&mp->neighbor, ipn);
110
111           vl_api_send_msg (reg, (u8 *) mp);
112         }
113     }
114   else
115     {
116       static f64 last_time;
117       /*
118        * Throttle syslog msgs.
119        * It's pretty tempting to just revoke the registration...
120        */
121       if (vlib_time_now (vlib_get_main ()) > last_time + 10.0)
122         {
123           clib_warning ("neighbor event for %U to pid %d: queue stuffed!",
124                         format_ip_address, &ipn->ipn_key->ipnk_ip,
125                         ipne->ipne_watch.ipw_pid);
126           last_time = vlib_time_now (vlib_get_main ());
127         }
128     }
129
130   ip_neighbor_free (ipn);
131 }
132
133 typedef struct ip_neighbor_dump_ctx_t_
134 {
135   vl_api_registration_t *reg;
136   u32 context;
137 } ip_neighbor_dump_ctx_t;
138
139 static walk_rc_t
140 send_ip_neighbor_details (index_t ipni, void *arg)
141 {
142   ip_neighbor_dump_ctx_t *ctx = arg;
143   vl_api_ip_neighbor_details_t *mp;
144   ip_neighbor_t *ipn;
145
146   ipn = ip_neighbor_get (ipni);
147   mp = vl_msg_api_alloc (sizeof (*mp));
148   clib_memset (mp, 0, sizeof (*mp));
149   mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS + REPLY_MSG_ID_BASE);
150   mp->context = ctx->context;
151   mp->age =
152     clib_host_to_net_f64 ((vlib_time_now (vlib_get_main ()) -
153                            ipn->ipn_time_last_updated));
154   ip_neighbor_encode (&mp->neighbor, ipn);
155
156   vl_api_send_msg (ctx->reg, (u8 *) mp);
157
158   return (WALK_CONTINUE);
159 }
160
161 static void
162 vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
163 {
164   vl_api_registration_t *reg;
165   ip_address_family_t af;
166   int rv;
167
168   reg = vl_api_client_index_to_registration (mp->client_index);
169   if (!reg)
170     return;
171
172   u32 sw_if_index = ntohl (mp->sw_if_index);
173
174   rv = ip_address_family_decode (mp->af, &af);
175
176   if (rv)
177     return;
178
179   ip_neighbor_dump_ctx_t ctx = {
180     .reg = reg,
181     .context = mp->context,
182   };
183
184   // walk all neighbours on all interfaces
185   ip_neighbor_walk (af, sw_if_index, send_ip_neighbor_details, &ctx);
186 }
187
188 static ip_neighbor_flags_t
189 ip_neighbor_flags_decode (vl_api_ip_neighbor_flags_t v)
190 {
191   ip_neighbor_flags_t f = IP_NEIGHBOR_FLAG_NONE;
192
193   if (v & IP_API_NEIGHBOR_FLAG_STATIC)
194     f |= IP_NEIGHBOR_FLAG_STATIC;
195   if (v & IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY)
196     f |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
197
198   return (f);
199 }
200
201 static void
202 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
203                                       vlib_main_t * vm)
204 {
205   vl_api_ip_neighbor_add_del_reply_t *rmp;
206   ip_neighbor_flags_t flags;
207   u32 stats_index = ~0;
208   ip_address_t ip = ip_address_initializer;
209   mac_address_t mac;
210   int rv;
211
212   VALIDATE_SW_IF_INDEX ((&mp->neighbor));
213
214   flags = ip_neighbor_flags_decode (mp->neighbor.flags);
215   ip_address_decode2 (&mp->neighbor.ip_address, &ip);
216   mac_address_decode (mp->neighbor.mac_address, &mac);
217
218   /* must be static or dynamic, default to dynamic */
219   if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
220       !(flags & IP_NEIGHBOR_FLAG_DYNAMIC))
221     flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
222
223   /*
224    * there's no validation here of the ND/ARP entry being added.
225    * The expectation is that the FIB will ensure that nothing bad
226    * will come of adding bogus entries.
227    */
228   if (mp->is_add)
229     rv = ip_neighbor_add (&ip, &mac,
230                           ntohl (mp->neighbor.sw_if_index),
231                           flags, &stats_index);
232   else
233     rv = ip_neighbor_del (&ip, ntohl (mp->neighbor.sw_if_index));
234
235   BAD_SW_IF_INDEX_LABEL;
236
237   REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
238   ({
239     rmp->stats_index = htonl (stats_index);
240   }));
241 }
242
243 static void
244 vl_api_want_ip_neighbor_events_t_handler (vl_api_want_ip_neighbor_events_t *
245                                           mp)
246 {
247   vl_api_want_ip_neighbor_events_reply_t *rmp;
248   ip_address_t ip;
249   int rv = 0;
250
251   if (mp->sw_if_index != ~0)
252     VALIDATE_SW_IF_INDEX (mp);
253   ip_address_decode2 (&mp->ip, &ip);
254
255   ip_neighbor_watcher_t watch = {
256     .ipw_client = mp->client_index,
257     .ipw_pid = mp->pid,
258     .ipw_api_version = 1,
259   };
260
261   if (mp->enable)
262     ip_neighbor_watch (&ip, ntohl (mp->sw_if_index), &watch);
263   else
264     ip_neighbor_unwatch (&ip, ntohl (mp->sw_if_index), &watch);
265
266   BAD_SW_IF_INDEX_LABEL;
267   REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_REPLY);
268 }
269
270 static void
271   vl_api_want_ip_neighbor_events_v2_t_handler
272   (vl_api_want_ip_neighbor_events_v2_t * mp)
273 {
274   vl_api_want_ip_neighbor_events_reply_t *rmp;
275   ip_address_t ip;
276   int rv = 0;
277
278   if (mp->sw_if_index != ~0)
279     VALIDATE_SW_IF_INDEX (mp);
280   ip_address_decode2 (&mp->ip, &ip);
281
282   ip_neighbor_watcher_t watch = {
283     .ipw_client = mp->client_index,
284     .ipw_pid = mp->pid,
285     .ipw_api_version = 2,
286   };
287
288   if (mp->enable)
289     ip_neighbor_watch (&ip, ntohl (mp->sw_if_index), &watch);
290   else
291     ip_neighbor_unwatch (&ip, ntohl (mp->sw_if_index), &watch);
292
293   BAD_SW_IF_INDEX_LABEL;
294   REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_V2_REPLY);
295 }
296
297 static void
298 vl_api_ip_neighbor_config_t_handler (vl_api_ip_neighbor_config_t * mp)
299 {
300   vl_api_ip_neighbor_config_reply_t *rmp;
301   ip_address_family_t af;
302   int rv;
303
304   rv = ip_address_family_decode (mp->af, &af);
305
306   if (!rv)
307     rv = ip_neighbor_config (af,
308                              ntohl (mp->max_number),
309                              ntohl (mp->max_age), mp->recycle);
310
311   REPLY_MACRO (VL_API_IP_NEIGHBOR_CONFIG_REPLY);
312 }
313
314 static void
315 vl_api_ip_neighbor_config_get_t_handler (vl_api_ip_neighbor_config_get_t *mp)
316 {
317   vl_api_ip_neighbor_config_get_reply_t *rmp;
318   int rv;
319   ip_address_family_t af = AF_IP4;
320   u32 max_number = ~0;
321   u32 max_age = ~0;
322   bool recycle = false;
323
324   rv = ip_address_family_decode (mp->af, &af);
325
326   if (!rv)
327     rv = ip_neighbor_get_config (af, &max_number, &max_age, &recycle);
328
329   // clang-format off
330   REPLY_MACRO2 (VL_API_IP_NEIGHBOR_CONFIG_GET_REPLY,
331   ({
332     rmp->af = ip_address_family_encode (af);
333     rmp->max_number = htonl (max_number);
334     rmp->max_age = htonl (max_age);
335     rmp->recycle = recycle;
336   }));
337   // clang-format on
338 }
339
340 static void
341 vl_api_ip_neighbor_replace_begin_t_handler (vl_api_ip_neighbor_replace_begin_t
342                                             * mp)
343 {
344   vl_api_ip_neighbor_replace_begin_reply_t *rmp;
345   int rv = 0;
346
347   ip_neighbor_mark (AF_IP4);
348   ip_neighbor_mark (AF_IP6);
349
350   REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_BEGIN_REPLY);
351 }
352
353 static void
354 vl_api_ip_neighbor_replace_end_t_handler (vl_api_ip_neighbor_replace_end_t *
355                                           mp)
356 {
357   vl_api_ip_neighbor_replace_end_reply_t *rmp;
358   int rv = 0;
359
360   ip_neighbor_sweep (AF_IP4);
361   ip_neighbor_sweep (AF_IP6);
362
363   REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_END_REPLY);
364 }
365
366 static void
367 vl_api_ip_neighbor_flush_t_handler (vl_api_ip_neighbor_flush_t * mp)
368 {
369   vl_api_ip_neighbor_flush_reply_t *rmp;
370   ip_address_family_t af;
371   int rv;
372
373   if (mp->sw_if_index != ~0)
374     VALIDATE_SW_IF_INDEX (mp);
375
376   rv = ip_address_family_decode (mp->af, &af);
377
378   if (!rv)
379     ip_neighbor_del_all (af, ntohl (mp->sw_if_index));
380
381   BAD_SW_IF_INDEX_LABEL;
382   REPLY_MACRO (VL_API_IP_NEIGHBOR_FLUSH_REPLY);
383 }
384
385 #define vl_msg_name_crc_list
386 #include <vnet/ip-neighbor/ip_neighbor.api.h>
387 #undef vl_msg_name_crc_list
388
389 #include <vnet/ip-neighbor/ip_neighbor.api.c>
390
391 static clib_error_t *
392 ip_neighbor_api_init (vlib_main_t * vm)
393 {
394   /* Ask for a correctly-sized block of API message decode slots */
395   msg_id_base = setup_message_id_table ();
396
397   return 0;
398 }
399
400 VLIB_API_INIT_FUNCTION (ip_neighbor_api_init);
401
402 /*
403  * fd.io coding-style-patch-verification: ON
404  *
405  * Local Variables:
406  * eval: (c-set-style "gnu")
407  * End:
408  */