+static bool
+l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
+ const l2_arp_term_publish_event_t * e2)
+{
+ if (e1 == NULL || e2 == NULL)
+ return false;
+ return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
+ (e1->sw_if_index == e2->sw_if_index) &&
+ (mac_address_equal (&e1->mac, &e2->mac)));
+}
+
+static uword
+l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
+ vlib_frame_t * f)
+{
+ /* These cross the longjmp boundary (vlib_process_wait_for_event)
+ * and need to be volatile - to prevent them from being optimized into
+ * a register - which could change during suspension */
+ volatile f64 last = vlib_time_now (vm);
+ volatile l2_arp_term_publish_event_t last_event = { };
+
+ l2_arp_term_main_t *l2am = &l2_arp_term_main;
+
+ while (1)
+ {
+ uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
+ vpe_client_registration_t *reg;
+ f64 now;
+
+ vlib_process_wait_for_event (vm);
+
+ vlib_process_get_event_data (vm, &event_type);
+ now = vlib_time_now (vm);
+
+ if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
+ {
+ l2_arp_term_publish_event_t *event;
+
+ vec_foreach (event, l2am->publish_events)
+ {
+ /* dampen duplicate events - cast away volatile */
+ if (l2_arp_term_publish_event_is_equal
+ (event, (l2_arp_term_publish_event_t *) & last_event) &&
+ (now - last) < 10.0)
+ {
+ continue;
+ }
+ last_event = *event;
+ last = now;
+
+ /* *INDENT-OFF* */
+ pool_foreach(reg, vpe_api_main.l2_arp_term_events_registrations,
+ ({
+ vl_api_registration_t *vl_reg;
+ vl_reg = vl_api_client_index_to_registration (reg->client_index);
+ ASSERT (vl_reg != NULL);
+
+ if (reg && vl_api_can_send_msg (vl_reg))
+ {
+ vl_api_l2_arp_term_event_t * vevent;
+ vevent = vl_msg_api_alloc (sizeof *vevent);
+ clib_memset (vevent, 0, sizeof *vevent);
+ vevent->_vl_msg_id = htons (VL_API_L2_ARP_TERM_EVENT);
+ vevent->client_index = reg->client_index;
+ vevent->pid = reg->client_pid;
+ ip_address_encode(&event->ip,
+ event->type,
+ &vevent->ip);
+ vevent->sw_if_index = htonl(event->sw_if_index);
+ mac_address_encode(&event->mac, vevent->mac);
+ vl_api_send_msg (vl_reg, (u8 *) vevent);
+ }
+ }));
+ /* *INDENT-ON* */
+ }
+ vec_reset_length (l2am->publish_events);
+ }
+ }
+
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
+ .function = l2_arp_term_process,
+ .type = VLIB_NODE_TYPE_PROCESS,
+ .name = "l2-arp-term-publisher",
+};
+/* *INDENT-ON* */
+
+static void
+vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
+ mp)
+{
+ vl_api_want_l2_arp_term_events_reply_t *rmp;
+ vpe_api_main_t *am = &vpe_api_main;
+ vpe_client_registration_t *rp;
+ int rv = 0;
+ uword *p;
+
+ p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
+
+ if (p)
+ {
+ if (mp->enable)
+ {
+ clib_warning ("pid %d: already enabled...", mp->pid);
+ rv = VNET_API_ERROR_INVALID_REGISTRATION;
+ goto reply;
+ }
+ else
+ {
+ rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
+ pool_put (am->l2_arp_term_events_registrations, rp);
+ hash_unset (am->l2_arp_term_events_registration_hash,
+ mp->client_index);
+ if (pool_elts (am->l2_arp_term_events_registrations) == 0)
+ l2_arp_term_set_publisher_node (false);
+ goto reply;
+ }
+ }
+ if (mp->enable == 0)
+ {
+ clib_warning ("pid %d: already disabled...", mp->pid);
+ rv = VNET_API_ERROR_INVALID_REGISTRATION;
+ goto reply;
+ }
+ pool_get (am->l2_arp_term_events_registrations, rp);
+ rp->client_index = mp->client_index;
+ rp->client_pid = mp->pid;
+ hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
+ rp - am->l2_arp_term_events_registrations);
+ l2_arp_term_set_publisher_node (true);
+
+reply:
+ REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
+}
+
+static clib_error_t *
+want_l2_arp_term_events_reaper (u32 client_index)
+{
+ vpe_client_registration_t *rp;
+ vpe_api_main_t *am;
+ uword *p;
+
+ am = &vpe_api_main;
+
+ /* remove from the registration hash */
+ p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
+
+ if (p)
+ {
+ rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
+ pool_put (am->l2_arp_term_events_registrations, rp);
+ hash_unset (am->l2_arp_term_events_registration_hash, client_index);
+ if (pool_elts (am->l2_arp_term_events_registrations) == 0)
+ l2_arp_term_set_publisher_node (false);
+ }
+ return (NULL);
+}
+
+VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
+