39d77855e9ffc5d2a65f69170df2536913cb7c95
[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 (const ip_neighbor_event_t * ipne)
65 {
66   vl_api_ip_neighbor_event_t *mp;
67   vl_api_registration_t *reg;
68   const ip_neighbor_t *ipn;
69
70   ipn = ip_neighbor_get (ipne->ipne_index);
71
72   if (NULL == ipn)
73     /* Client can cancel, die, etc. */
74     return;
75
76   /* Customer(s) requesting event for this neighbor */
77   reg = vl_api_client_index_to_registration (ipne->ipne_watch.ipw_client);
78   if (!reg)
79     return;
80
81   if (vl_api_can_send_msg (reg))
82     {
83       mp = vl_msg_api_alloc (sizeof (*mp));
84       clib_memset (mp, 0, sizeof (*mp));
85       mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_EVENT + REPLY_MSG_ID_BASE);
86       mp->client_index = ipne->ipne_watch.ipw_client;
87       mp->pid = ipne->ipne_watch.ipw_pid;
88
89       ip_neighbor_encode (&mp->neighbor, ipn);
90
91       vl_api_send_msg (reg, (u8 *) mp);
92     }
93   else
94     {
95       static f64 last_time;
96       /*
97        * Throttle syslog msgs.
98        * It's pretty tempting to just revoke the registration...
99        */
100       if (vlib_time_now (vlib_get_main ()) > last_time + 10.0)
101         {
102           clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
103                         format_ip_address, &ipn->ipn_key->ipnk_ip,
104                         ipne->ipne_watch.ipw_pid);
105           last_time = vlib_time_now (vlib_get_main ());
106         }
107     }
108 }
109
110 typedef struct ip_neighbor_dump_ctx_t_
111 {
112   vl_api_registration_t *reg;
113   u32 context;
114 } ip_neighbor_dump_ctx_t;
115
116 static walk_rc_t
117 send_ip_neighbor_details (index_t ipni, void *arg)
118 {
119   ip_neighbor_dump_ctx_t *ctx = arg;
120   vl_api_ip_neighbor_details_t *mp;
121   ip_neighbor_t *ipn;
122
123   ipn = ip_neighbor_get (ipni);
124   mp = vl_msg_api_alloc (sizeof (*mp));
125   clib_memset (mp, 0, sizeof (*mp));
126   mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS + REPLY_MSG_ID_BASE);
127   mp->context = ctx->context;
128   mp->age =
129     clib_host_to_net_f64 ((vlib_time_now (vlib_get_main ()) -
130                            ipn->ipn_time_last_updated));
131   ip_neighbor_encode (&mp->neighbor, ipn);
132
133   vl_api_send_msg (ctx->reg, (u8 *) mp);
134
135   return (WALK_CONTINUE);
136 }
137
138 static void
139 vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
140 {
141   vl_api_registration_t *reg;
142   ip_address_family_t af;
143   int rv;
144
145   reg = vl_api_client_index_to_registration (mp->client_index);
146   if (!reg)
147     return;
148
149   u32 sw_if_index = ntohl (mp->sw_if_index);
150
151   rv = ip_address_family_decode (mp->af, &af);
152
153   if (rv)
154     return;
155
156   ip_neighbor_dump_ctx_t ctx = {
157     .reg = reg,
158     .context = mp->context,
159   };
160
161   // walk all neighbours on all interfaces
162   ip_neighbor_walk (af, sw_if_index, send_ip_neighbor_details, &ctx);
163 }
164
165 static ip_neighbor_flags_t
166 ip_neighbor_flags_decode (vl_api_ip_neighbor_flags_t v)
167 {
168   ip_neighbor_flags_t f = IP_NEIGHBOR_FLAG_NONE;
169
170   if (v & IP_API_NEIGHBOR_FLAG_STATIC)
171     f |= IP_NEIGHBOR_FLAG_STATIC;
172   if (v & IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY)
173     f |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
174
175   return (f);
176 }
177
178 static void
179 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
180                                       vlib_main_t * vm)
181 {
182   vl_api_ip_neighbor_add_del_reply_t *rmp;
183   ip_neighbor_flags_t flags;
184   u32 stats_index = ~0;
185   ip_address_t ip = ip_address_initializer;
186   mac_address_t mac;
187   int rv;
188
189   VALIDATE_SW_IF_INDEX ((&mp->neighbor));
190
191   flags = ip_neighbor_flags_decode (mp->neighbor.flags);
192   ip_address_decode2 (&mp->neighbor.ip_address, &ip);
193   mac_address_decode (mp->neighbor.mac_address, &mac);
194
195   /* must be static or dynamic, default to dynamic */
196   if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
197       !(flags & IP_NEIGHBOR_FLAG_DYNAMIC))
198     flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
199
200   /*
201    * there's no validation here of the ND/ARP entry being added.
202    * The expectation is that the FIB will ensure that nothing bad
203    * will come of adding bogus entries.
204    */
205   if (mp->is_add)
206     rv = ip_neighbor_add (&ip, &mac,
207                           ntohl (mp->neighbor.sw_if_index),
208                           flags, &stats_index);
209   else
210     rv = ip_neighbor_del (&ip, ntohl (mp->neighbor.sw_if_index));
211
212   BAD_SW_IF_INDEX_LABEL;
213
214   /* *INDENT-OFF* */
215   REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
216   ({
217     rmp->stats_index = htonl (stats_index);
218   }));
219   /* *INDENT-ON* */
220 }
221
222 static void
223 vl_api_want_ip_neighbor_events_t_handler (vl_api_want_ip_neighbor_events_t *
224                                           mp)
225 {
226   vl_api_want_ip_neighbor_events_reply_t *rmp;
227   ip_address_t ip;
228   int rv = 0;
229
230   if (mp->sw_if_index != ~0)
231     VALIDATE_SW_IF_INDEX (mp);
232   ip_address_decode2 (&mp->ip, &ip);
233
234   ip_neighbor_watcher_t watch = {
235     .ipw_client = mp->client_index,
236     .ipw_pid = mp->pid,
237   };
238
239   if (mp->enable)
240     ip_neighbor_watch (&ip, ntohl (mp->sw_if_index), &watch);
241   else
242     ip_neighbor_unwatch (&ip, ntohl (mp->sw_if_index), &watch);
243
244   BAD_SW_IF_INDEX_LABEL;
245   REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_REPLY);
246 }
247
248 static void
249 vl_api_ip_neighbor_config_t_handler (vl_api_ip_neighbor_config_t * mp)
250 {
251   vl_api_ip_neighbor_config_reply_t *rmp;
252   ip_address_family_t af;
253   int rv;
254
255   rv = ip_address_family_decode (mp->af, &af);
256
257   if (!rv)
258     rv = ip_neighbor_config (af,
259                              ntohl (mp->max_number),
260                              ntohl (mp->max_age), mp->recycle);
261
262   REPLY_MACRO (VL_API_IP_NEIGHBOR_CONFIG_REPLY);
263 }
264
265 static void
266 vl_api_ip_neighbor_replace_begin_t_handler (vl_api_ip_neighbor_replace_begin_t
267                                             * mp)
268 {
269   vl_api_ip_neighbor_replace_begin_reply_t *rmp;
270   int rv = 0;
271
272   ip_neighbor_mark (AF_IP4);
273   ip_neighbor_mark (AF_IP6);
274
275   REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_BEGIN_REPLY);
276 }
277
278 static void
279 vl_api_ip_neighbor_replace_end_t_handler (vl_api_ip_neighbor_replace_end_t *
280                                           mp)
281 {
282   vl_api_ip_neighbor_replace_end_reply_t *rmp;
283   int rv = 0;
284
285   ip_neighbor_sweep (AF_IP4);
286   ip_neighbor_sweep (AF_IP6);
287
288   REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_END_REPLY);
289 }
290
291 static void
292 vl_api_ip_neighbor_flush_t_handler (vl_api_ip_neighbor_flush_t * mp)
293 {
294   vl_api_ip_neighbor_flush_reply_t *rmp;
295   ip_address_family_t af;
296   int rv;
297
298   if (mp->sw_if_index != ~0)
299     VALIDATE_SW_IF_INDEX (mp);
300
301   rv = ip_address_family_decode (mp->af, &af);
302
303   if (!rv)
304     ip_neighbor_del_all (af, ntohl (mp->sw_if_index));
305
306   BAD_SW_IF_INDEX_LABEL;
307   REPLY_MACRO (VL_API_IP_NEIGHBOR_FLUSH_REPLY);
308 }
309
310 #define vl_msg_name_crc_list
311 #include <vnet/ip-neighbor/ip_neighbor.api.h>
312 #undef vl_msg_name_crc_list
313
314 #include <vnet/ip-neighbor/ip_neighbor.api.c>
315
316 static clib_error_t *
317 ip_neighbor_api_init (vlib_main_t * vm)
318 {
319   /* Ask for a correctly-sized block of API message decode slots */
320   msg_id_base = setup_message_id_table ();
321
322   return 0;
323 }
324
325 VLIB_API_INIT_FUNCTION (ip_neighbor_api_init);
326
327 /*
328  * fd.io coding-style-patch-verification: ON
329  *
330  * Local Variables:
331  * eval: (c-set-style "gnu")
332  * End:
333  */