FD.io VPP  v20.09-64-g4f7b92f0a
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 
41 static ip46_type_t
43 {
44  return (AF_IP4 == af ? IP46_TYPE_IP4 : IP46_TYPE_IP6);
45 }
46 
47 static vl_api_ip_neighbor_flags_t
49 {
50  vl_api_ip_neighbor_flags_t v = IP_API_NEIGHBOR_FLAG_NONE;
51 
52  if (f & IP_NEIGHBOR_FLAG_STATIC)
54  if (f & IP_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 
67  ipn->ipn_key->ipnk_type, &api->ip_address);
68  mac_address_encode (&ipn->ipn_mac, api->mac_address);
69 }
70 
71 void
73 {
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 */
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  {
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);
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  {
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!",
134  last_time = vlib_time_now (vlib_get_main ());
135  }
136  }
137 
138  ip_neighbor_free (ipn);
139 }
140 
142 {
146 
147 static walk_rc_t
149 {
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 =
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
171 {
174  int rv;
175 
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 
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 {
203 
205  f |= IP_NEIGHBOR_FLAG_STATIC;
207  f |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
208 
209  return (f);
210 }
211 
212 static void
214  vlib_main_t * vm)
215 {
218  u32 stats_index = ~0;
219  ip46_address_t ip = ip46_address_initializer;
222  int rv;
223 
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 
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
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)
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 
282  REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_REPLY);
283 }
284 
285 static void
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)
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 
310  REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_V2_REPLY);
311 }
312 
313 static void
315 {
316  vl_api_ip_neighbor_config_reply_t *rmp;
318  int rv;
319 
320  rv = ip_address_family_decode (mp->af, &af);
321 
322  if (!rv)
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
332  * mp)
333 {
334  vl_api_ip_neighbor_replace_begin_reply_t *rmp;
335  int rv = 0;
336 
339 
340  REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_BEGIN_REPLY);
341 }
342 
343 static void
345  mp)
346 {
347  vl_api_ip_neighbor_replace_end_reply_t *rmp;
348  int rv = 0;
349 
352 
353  REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_END_REPLY);
354 }
355 
356 static void
358 {
359  vl_api_ip_neighbor_flush_reply_t *rmp;
361  int rv;
362 
363  if (mp->sw_if_index != ~0)
365 
366  rv = ip_address_family_decode (mp->af, &af);
367 
368  if (!rv)
370 
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 *
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 
391 
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */
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.
Dump IP neighbors.
Definition: ip_neighbor.api:88
vl_api_mac_address_t mac
Definition: l2.api:502
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:103
#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
ip46_address_t ipnk_ip
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...
void ip_neighbor_del_all(ip46_type_t type, u32 sw_if_index)
Definition: ip_neighbor.c:600
int ip_neighbor_add(const ip46_address_t *ip, ip46_type_t type, const mac_address_t *mac, u32 sw_if_index, ip_neighbor_flags_t flags, u32 *stats_index)
Definition: ip_neighbor.c:458
void ip_neighbor_sweep(ip46_type_t type)
Definition: ip_neighbor.c:1270
#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:333
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:41
vl_api_interface_index_t sw_if_index[default=0xffffffff]
int ip_neighbor_config(ip46_type_t type, u32 limit, u32 age, bool recycle)
Definition: ip_neighbor.c:1681
vlib_main_t * vm
Definition: in2out_ed.c:1582
ip_neighbor_event_flags_t ipne_flags
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
int ip_neighbor_del(const ip46_address_t *ip, ip46_type_t type, u32 sw_if_index)
Definition: ip_neighbor.c:557
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
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)
unsigned int u32
Definition: types.h:88
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:160
void ip_neighbor_watch(const ip46_address_t *ip, ip46_type_t type, u32 sw_if_index, const ip_neighbor_watcher_t *watch)
vl_api_fib_path_type_t type
Definition: fib_types.api:123
static clib_error_t * ip_neighbor_api_init(vlib_main_t *vm)
vl_api_interface_index_t sw_if_index[default=0xffffffff]
static void vl_api_want_ip_neighbor_events_v2_t_handler(vl_api_want_ip_neighbor_events_v2_t *mp)
void ip_neighbor_mark(ip46_type_t type)
Definition: ip_neighbor.c:1243
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)
format_function_t format_ip46_address
Definition: ip46_address.h:50
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)
#define BAD_SW_IF_INDEX_LABEL
vl_api_interface_index_t sw_if_index[default=0xffffffff]
vl_api_registration_t * reg
void ip_neighbor_walk(ip46_type_t type, u32 sw_if_index, ip_neighbor_walk_cb_t cb, void *ctx)
Definition: ip_neighbor.c:1040
#define clib_warning(format, args...)
Definition: error.h:59
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)
u32 stats_index
Definition: ip.api:143
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:23
enum ip_address_family_t_ ip_address_family_t
vl_api_ip_neighbor_t neighbor
vl_api_address_t ip
Definition: l2.api:501
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.
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:194
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:88
vl_api_address_family_t af
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:2804
vl_api_address_family_t af
static ip46_type_t ip46_type_from_af(ip_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.
#define ip46_address_initializer
Definition: ip46_address.h:52
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
ip_neighbor_watcher_t ipne_watch
#define VALIDATE_SW_IF_INDEX(mp)
ip46_type_t
Definition: ip46_address.h:22
void ip_neighbor_unwatch(const ip46_address_t *ip, ip46_type_t type, u32 sw_if_index, const ip_neighbor_watcher_t *watch)