FD.io VPP  v21.06
Vector Packet Processing
ip_neighbor_api.c
Go to the documentation of this file.
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 
24 #include <vnet/ip/ip_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 
34 #define REPLY_MSG_ID_BASE msg_id_base
35 
37 
38 #include <vnet/format_fns.h>
39 
40 static vl_api_ip_neighbor_flags_t
42 {
43  vl_api_ip_neighbor_flags_t v = IP_API_NEIGHBOR_FLAG_NONE;
44 
45  if (f & IP_NEIGHBOR_FLAG_STATIC)
47  if (f & IP_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
65 {
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 */
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  {
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);
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  {
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!",
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 
134 {
138 
139 static walk_rc_t
141 {
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 =
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
163 {
166  int rv;
167 
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 
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 {
192 
194  f |= IP_NEIGHBOR_FLAG_STATIC;
196  f |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
197 
198  return (f);
199 }
200 
201 static void
203  vlib_main_t * vm)
204 {
207  u32 stats_index = ~0;
210  int rv;
211 
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 
236 
237  /* *INDENT-OFF* */
238  REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
239  ({
240  rmp->stats_index = htonl (stats_index);
241  }));
242  /* *INDENT-ON* */
243 }
244 
245 static void
247  mp)
248 {
249  vl_api_want_ip_neighbor_events_reply_t *rmp;
251  int rv = 0;
252 
253  if (mp->sw_if_index != ~0)
255  ip_address_decode2 (&mp->ip, &ip);
256 
257  ip_neighbor_watcher_t watch = {
258  .ipw_client = mp->client_index,
259  .ipw_pid = mp->pid,
260  .ipw_api_version = 1,
261  };
262 
263  if (mp->enable)
264  ip_neighbor_watch (&ip, ntohl (mp->sw_if_index), &watch);
265  else
266  ip_neighbor_unwatch (&ip, ntohl (mp->sw_if_index), &watch);
267 
269  REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_REPLY);
270 }
271 
272 static void
275 {
276  vl_api_want_ip_neighbor_events_reply_t *rmp;
278  int rv = 0;
279 
280  if (mp->sw_if_index != ~0)
282  ip_address_decode2 (&mp->ip, &ip);
283 
284  ip_neighbor_watcher_t watch = {
285  .ipw_client = mp->client_index,
286  .ipw_pid = mp->pid,
287  .ipw_api_version = 2,
288  };
289 
290  if (mp->enable)
291  ip_neighbor_watch (&ip, ntohl (mp->sw_if_index), &watch);
292  else
293  ip_neighbor_unwatch (&ip, ntohl (mp->sw_if_index), &watch);
294 
296  REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_V2_REPLY);
297 }
298 
299 static void
301 {
302  vl_api_ip_neighbor_config_reply_t *rmp;
304  int rv;
305 
306  rv = ip_address_family_decode (mp->af, &af);
307 
308  if (!rv)
309  rv = ip_neighbor_config (af,
310  ntohl (mp->max_number),
311  ntohl (mp->max_age), mp->recycle);
312 
313  REPLY_MACRO (VL_API_IP_NEIGHBOR_CONFIG_REPLY);
314 }
315 
316 static void
318  * mp)
319 {
320  vl_api_ip_neighbor_replace_begin_reply_t *rmp;
321  int rv = 0;
322 
325 
326  REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_BEGIN_REPLY);
327 }
328 
329 static void
331  mp)
332 {
333  vl_api_ip_neighbor_replace_end_reply_t *rmp;
334  int rv = 0;
335 
338 
339  REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_END_REPLY);
340 }
341 
342 static void
344 {
345  vl_api_ip_neighbor_flush_reply_t *rmp;
347  int rv;
348 
349  if (mp->sw_if_index != ~0)
351 
352  rv = ip_address_family_decode (mp->af, &af);
353 
354  if (!rv)
355  ip_neighbor_del_all (af, ntohl (mp->sw_if_index));
356 
358  REPLY_MACRO (VL_API_IP_NEIGHBOR_FLUSH_REPLY);
359 }
360 
361 #define vl_msg_name_crc_list
362 #include <vnet/ip-neighbor/ip_neighbor.api.h>
363 #undef vl_msg_name_crc_list
364 
365 #include <vnet/ip-neighbor/ip_neighbor.api.c>
366 
367 static clib_error_t *
369 {
370  /* Ask for a correctly-sized block of API message decode slots */
371  msg_id_base = setup_message_id_table ();
372 
373  return 0;
374 }
375 
377 
378 /*
379  * fd.io coding-style-patch-verification: ON
380  *
381  * Local Variables:
382  * eval: (c-set-style "gnu")
383  * End:
384  */
int ip_address_family_decode(vl_api_address_family_t af, ip_address_family_t *out)
Conversion functions to/from (decode/encode) API types to VPP internal types.
Definition: ip_types_api.c:34
Enable/disable periodic IP neighbor scan.
static void ip_neighbor_encode(vl_api_ip_neighbor_t *api, const ip_neighbor_t *ipn)
void ip_neighbor_handle_event(ip_neighbor_event_t *ipne)
From the watcher to the API to publish a new neighbor.
f64 ipn_time_last_updated
Aging related data.
void ip_address_encode2(const ip_address_t *in, vl_api_address_t *out)
Definition: ip_types_api.c:242
Dump IP neighbors.
Definition: ip_neighbor.api:88
vl_api_mac_address_t mac
Definition: l2.api:559
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
#define ntohs(x)
Definition: af_xdp.bpf.c:29
vl_api_address_family_t af
Definition: ip_neighbor.api:93
void mac_address_encode(const mac_address_t *in, u8 *out)
vl_api_ip_neighbor_t neighbor
Definition: ip_neighbor.api:67
static ip_neighbor_flags_t ip_neighbor_flags_decode(vl_api_ip_neighbor_flags_t v)
Tell client about an IP4 ARP resolution event or MAC/IP info from ARP requests in L2 BDs...
#define REPLY_MACRO2(t, body)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
void ip_neighbor_free(ip_neighbor_t *ipn)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vl_api_ip_neighbor_t neighbor
static walk_rc_t send_ip_neighbor_details(index_t ipni, void *arg)
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:43
vl_api_interface_index_t sw_if_index[default=0xffffffff]
ip_neighbor_event_flags_t ipne_flags
int ip_neighbor_add(const ip_address_t *ip, const mac_address_t *mac, u32 sw_if_index, ip_neighbor_flags_t flags, u32 *stats_index)
Definition: ip_neighbor.c:477
void * vl_msg_api_alloc(int nbytes)
int ip_neighbor_del(const ip_address_t *ip, u32 sw_if_index)
Definition: ip_neighbor.c:574
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
unsigned int u32
Definition: types.h:88
vlib_frame_t * f
enum walk_rc_t_ walk_rc_t
Walk return code.
static int vl_api_can_send_msg(vl_api_registration_t *rp)
Definition: api.h:48
#define ip_address_initializer
Definition: ip_types.h:88
Register for IP neighbour events (creation or deletion)
static void vl_api_ip_neighbor_config_t_handler(vl_api_ip_neighbor_config_t *mp)
A representation of an IP neighbour/peer.
static void vl_api_ip_neighbor_add_del_t_handler(vl_api_ip_neighbor_add_del_t *mp, vlib_main_t *vm)
void ip_neighbor_del_all(ip_address_family_t af, u32 sw_if_index)
Definition: ip_neighbor.c:616
int __clib_unused rv
Definition: application.c:491
void ip_neighbor_unwatch(const ip_address_t *ip, u32 sw_if_index, const ip_neighbor_watcher_t *watch)
void ip_neighbor_mark(ip_address_family_t af)
Definition: ip_neighbor.c:1240
void ip_neighbor_sweep(ip_address_family_t af)
Definition: ip_neighbor.c:1267
static clib_error_t * ip_neighbor_api_init(vlib_main_t *vm)
vl_api_interface_index_t sw_if_index[default=0xffffffff]
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
static void vl_api_want_ip_neighbor_events_v2_t_handler(vl_api_want_ip_neighbor_events_v2_t *mp)
VLIB_API_INIT_FUNCTION(ip_neighbor_api_init)
long ctx[MAX_CONNS]
Definition: main.c:144
unsigned short u16
Definition: types.h:57
static u16 msg_id_base
#define REPLY_MACRO(t)
mac_address_t ipn_mac
The learned MAC address of the neighbour.
static void vl_api_ip_neighbor_flush_t_handler(vl_api_ip_neighbor_flush_t *mp)
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
void ip_address_decode2(const vl_api_address_t *in, ip_address_t *out)
Definition: ip_types_api.c:192
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
static void vl_api_want_ip_neighbor_events_t_handler(vl_api_want_ip_neighbor_events_t *mp)
static void setup_message_id_table(api_main_t *am)
Definition: bfd_api.c:451
#define BAD_SW_IF_INDEX_LABEL
vl_api_interface_index_t sw_if_index[default=0xffffffff]
vl_api_registration_t * reg
#define clib_warning(format, args...)
Definition: error.h:59
u8 * format_ip_address(u8 *s, va_list *args)
Definition: ip_types.c:21
void ip_neighbor_watch(const ip_address_t *ip, u32 sw_if_index, const ip_neighbor_watcher_t *watch)
IP neighbor add / del reply.
Definition: ip_neighbor.api:75
Tell client about an IP4 ARP resolution event or MAC/IP info from ARP requests in L2 BDs...
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
static void vl_api_ip_neighbor_replace_end_t_handler(vl_api_ip_neighbor_replace_end_t *mp)
static vl_api_ip_neighbor_flags_t ip_neighbor_flags_encode(ip_neighbor_flags_t f)
void ip_neighbor_walk(ip_address_family_t af, u32 sw_if_index, ip_neighbor_walk_cb_t cb, void *ctx)
Definition: ip_neighbor.c:1041
u32 stats_index
Definition: ip.api:145
IP neighbor add / del request.
Definition: ip_neighbor.api:61
static f64 clib_host_to_net_f64(f64 x)
Definition: byte_order.h:178
#define REPLY_MSG_ID_BASE
ip_neighbor_key_t * ipn_key
The idempotent key.
static void vl_api_ip_neighbor_dump_t_handler(vl_api_ip_neighbor_dump_t *mp)
enum ip_neighbor_flags_t_ ip_neighbor_flags_t
IP neighbour replace end.
IP neighbors dump response.
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
enum ip_address_family_t_ ip_address_family_t
vl_api_ip_neighbor_t neighbor
vl_api_address_t ip
Definition: l2.api:558
Register for IP neighbour events creation.
vl_api_ip_neighbor_t neighbor
IP neighbor flush request - removes all neighbours.
struct ip_neighbor_dump_ctx_t_ ip_neighbor_dump_ctx_t
vl_api_interface_index_t sw_if_index[default=0xffffffff]
Definition: ip_neighbor.api:92
ip_neighbor_flags_t ipn_flags
Falgs for this object.
IP neighbour replace begin.
static void vl_api_ip_neighbor_replace_begin_t_handler(vl_api_ip_neighbor_replace_begin_t *mp)
ip_neighbor_t * ip_neighbor_get(index_t ipni)
Definition: ip_neighbor.c:89
vl_api_address_family_t af
vl_api_address_family_t af
vl_api_ip_neighbor_event_flags_t flags
import vnet interface_types api
Definition: sample.api:20
void mac_address_decode(const u8 *in, mac_address_t *out)
Conversion functions to/from (decode/encode) API types to VPP internal types.
int ip_neighbor_config(ip_address_family_t af, u32 limit, u32 age, bool recycle)
Definition: ip_neighbor.c:1687
ip_neighbor_watcher_t ipne_watch
#define VALIDATE_SW_IF_INDEX(mp)