FD.io VPP  v20.01-48-g3e0dafb74
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 
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 {
76  const ip_neighbor_t *ipn;
77 
78  ipn = ip_neighbor_get (ipne->ipne_index);
79 
80  if (NULL == ipn)
81  /* Client can cancel, die, etc. */
82  return;
83 
84  /* Customer(s) requesting event for this neighbor */
86  if (!reg)
87  return;
88 
89  if (vl_api_can_send_msg (reg))
90  {
91  mp = vl_msg_api_alloc (sizeof (*mp));
92  clib_memset (mp, 0, sizeof (*mp));
93  mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_EVENT + REPLY_MSG_ID_BASE);
95  mp->pid = ipne->ipne_watch.ipw_pid;
96 
97  ip_neighbor_encode (&mp->neighbor, ipn);
98 
99  vl_api_send_msg (reg, (u8 *) mp);
100  }
101  else
102  {
103  static f64 last_time;
104  /*
105  * Throttle syslog msgs.
106  * It's pretty tempting to just revoke the registration...
107  */
108  if (vlib_time_now (vlib_get_main ()) > last_time + 10.0)
109  {
110  clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
113  last_time = vlib_time_now (vlib_get_main ());
114  }
115  }
116 }
117 
119 {
123 
124 static walk_rc_t
126 {
129  ip_neighbor_t *ipn;
130 
131  ipn = ip_neighbor_get (ipni);
132  mp = vl_msg_api_alloc (sizeof (*mp));
133  clib_memset (mp, 0, sizeof (*mp));
134  mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS + REPLY_MSG_ID_BASE);
135  mp->context = ctx->context;
136  ip_neighbor_encode (&mp->neighbor, ipn);
137 
138  vl_api_send_msg (ctx->reg, (u8 *) mp);
139 
140  return (WALK_CONTINUE);
141 }
142 
143 static void
145 {
148  int rv;
149 
151  if (!reg)
152  return;
153 
154  u32 sw_if_index = ntohl (mp->sw_if_index);
155 
156  rv = ip_address_family_decode (mp->af, &af);
157 
158  if (rv)
159  return;
160 
162  .reg = reg,
163  .context = mp->context,
164  };
165 
166  // walk all neighbours on all interfaces
167  ip_neighbor_walk ((af == AF_IP4 ?
168  IP46_TYPE_IP4 :
169  IP46_TYPE_IP6),
170  sw_if_index, send_ip_neighbor_details, &ctx);
171 }
172 
173 static ip_neighbor_flags_t
174 ip_neighbor_flags_decode (vl_api_ip_neighbor_flags_t v)
175 {
177 
182 
183  return (f);
184 }
185 
186 static void
188  vlib_main_t * vm)
189 {
192  u32 stats_index = ~0;
193  ip46_address_t ip = ip46_address_initializer;
196  int rv;
197 
199 
200  flags = ip_neighbor_flags_decode (mp->neighbor.flags);
201  type = ip_address_decode (&mp->neighbor.ip_address, &ip);
202  mac_address_decode (mp->neighbor.mac_address, &mac);
203 
204  /* must be static or dynamic, default to dynamic */
205  if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
206  !(flags & IP_NEIGHBOR_FLAG_DYNAMIC))
207  flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
208 
209  /*
210  * there's no validation here of the ND/ARP entry being added.
211  * The expectation is that the FIB will ensure that nothing bad
212  * will come of adding bogus entries.
213  */
214  if (mp->is_add)
215  rv = ip_neighbor_add (&ip, type, &mac,
216  ntohl (mp->neighbor.sw_if_index),
217  flags, &stats_index);
218  else
219  rv = ip_neighbor_del (&ip, type, ntohl (mp->neighbor.sw_if_index));
220 
222 
223  /* *INDENT-OFF* */
224  REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
225  ({
226  rmp->stats_index = htonl (stats_index);
227  }));
228  /* *INDENT-ON* */
229 }
230 
231 static void
233  mp)
234 {
235  vl_api_want_ip_neighbor_events_reply_t *rmp;
236  ip46_address_t ip;
237  ip46_type_t itype;
238  int rv = 0;
239 
240  if (mp->sw_if_index != ~0)
242  itype = ip_address_decode (&mp->ip, &ip);
243 
244  ip_neighbor_watcher_t watch = {
245  .ipw_client = mp->client_index,
246  .ipw_pid = mp->pid,
247  };
248 
249  if (mp->enable)
250  ip_neighbor_watch (&ip, itype, ntohl (mp->sw_if_index), &watch);
251  else
252  ip_neighbor_unwatch (&ip, itype, ntohl (mp->sw_if_index), &watch);
253 
255  REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_REPLY);
256 }
257 
258 static void
260 {
261  vl_api_ip_neighbor_config_reply_t *rmp;
263  int rv;
264 
265  rv = ip_address_family_decode (mp->af, &af);
266 
267  if (!rv)
269  ntohl (mp->max_number),
270  ntohl (mp->max_age), mp->recycle);
271 
272  REPLY_MACRO (VL_API_IP_NEIGHBOR_CONFIG_REPLY);
273 }
274 
275 #define vl_msg_name_crc_list
276 #include <vnet/ip-neighbor/ip_neighbor.api.h>
277 #undef vl_msg_name_crc_list
278 
279 #include <vnet/ip-neighbor/ip_neighbor.api.c>
280 
281 static clib_error_t *
283 {
284  /* Ask for a correctly-sized block of API message decode slots */
286 
287  return 0;
288 }
289 
291 
292 /*
293  * fd.io coding-style-patch-verification: ON
294  *
295  * Local Variables:
296  * eval: (c-set-style "gnu")
297  * End:
298  */
Enable/disable periodic IP neighbor scan.
static void ip_neighbor_encode(vl_api_ip_neighbor_t *api, const ip_neighbor_t *ipn)
Dump IP neighboors.
Definition: ip_neighbor.api:88
vl_api_mac_address_t mac
Definition: l2.api:491
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)
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:446
#define REPLY_MACRO2(t, body)
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
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
int ip_neighbor_config(ip46_type_t type, u32 limit, u32 age, bool recycle)
Definition: ip_neighbor.c:1570
enum ip_address_family_t_ ip_address_family_t
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:543
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
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
static void vl_api_ip_neighbor_config_t_handler(vl_api_ip_neighbor_config_t *mp)
A representation of an IP neighbour/peer.
void ip_neighbor_handle_event(const ip_neighbor_event_t *ipne)
From the watcher to the API to publish a new neighbor.
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)
Definition: ip_types_api.c:161
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]
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.
vlib_main_t * vm
Definition: in2out_ed.c:1810
format_function_t format_ip46_address
Definition: ip46_address.h:50
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
static void vl_api_want_ip_neighbor_events_t_handler(vl_api_want_ip_neighbor_events_t *mp)
#define BAD_SW_IF_INDEX_LABEL
u32 flags
Definition: vhost_user.h:141
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:992
#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:57
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
import vnet interface_types api
Definition: sample.api:20
#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)
IP neighboors dump response.
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vl_api_ip_neighbor_t neighbor
vl_api_address_t ip
Definition: l2.api:490
Register for IP4 ARP resolution event on receing ARP reply or MAC/IP info from ARP requests in L2 BDs...
vl_api_ip_neighbor_t neighbor
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.
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:178
int ip_address_family_decode(int _af, ip_address_family_t *out)
Conversion functions to/from (decode/encode) API types to VPP internal types.
Definition: ip_types_api.c:34
ip_neighbor_t * ip_neighbor_get(index_t ipni)
Definition: ip_neighbor.c:88
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:3410
vl_api_address_family_t af
static ip46_type_t ip46_type_from_af(ip_address_family_t af)
enum ip_neighbor_flags_t_ ip_neighbor_flags_t
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
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)