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