/* * Copyright (c) 2017-2019 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include __thread uword __vcl_worker_index = ~0; static int vcl_wait_for_segment (u64 segment_handle) { vcl_worker_t *wrk = vcl_worker_get_current (); u32 wait_for_seconds = 10, segment_index; f64 timeout; if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) return 0; timeout = clib_time_now (&wrk->clib_time) + wait_for_seconds; while (clib_time_now (&wrk->clib_time) < timeout) { segment_index = vcl_segment_table_lookup (segment_handle); if (segment_index != VCL_INVALID_SEGMENT_INDEX) return 0; usleep (10); } return 1; } static inline int vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg) { svm_msg_q_msg_t *msg; u32 n_msgs; int i; n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg); for (i = 0; i < n_msgs; i++) { vec_add2 (wrk->mq_msg_vector, msg, 1); svm_msg_q_sub_w_lock (mq, msg); } return n_msgs; } const char * vppcom_session_state_str (vcl_session_state_t state) { char *st; switch (state) { case STATE_START: st = "STATE_START"; break; case STATE_CONNECT: st = "STATE_CONNECT"; break; case STATE_LISTEN: st = "STATE_LISTEN"; break; case STATE_ACCEPT: st = "STATE_ACCEPT"; break; case STATE_VPP_CLOSING: st = "STATE_VPP_CLOSING"; break; case STATE_DISCONNECT: st = "STATE_DISCONNECT"; break; case STATE_FAILED: st = "STATE_FAILED"; break; case STATE_UPDATED: st = "STATE_UPDATED"; break; case STATE_LISTEN_NO_MQ: st = "STATE_LISTEN_NO_MQ"; break; default: st = "UNKNOWN_STATE"; break; } return st; } u8 * format_ip4_address (u8 * s, va_list * args) { u8 *a = va_arg (*args, u8 *); return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); } u8 * format_ip6_address (u8 * s, va_list * args) { ip6_address_t *a = va_arg (*args, ip6_address_t *); u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; i_max_n_zero = ARRAY_LEN (a->as_u16); max_n_zeros = 0; i_first_zero = i_max_n_zero; n_zeros = 0; for (i = 0; i < ARRAY_LEN (a->as_u16); i++) { u32 is_zero = a->as_u16[i] == 0; if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) { i_first_zero = i; n_zeros = 0; } n_zeros += is_zero; if ((!is_zero && n_zeros > max_n_zeros) || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) { i_max_n_zero = i_first_zero; max_n_zeros = n_zeros; i_first_zero = ARRAY_LEN (a->as_u16); n_zeros = 0; } } last_double_colon = 0; for (i = 0; i < ARRAY_LEN (a->as_u16); i++) { if (i == i_max_n_zero && max_n_zeros > 1) { s = format (s, "::"); i += max_n_zeros - 1; last_double_colon = 1; } else { s = format (s, "%s%x", (last_double_colon || i == 0) ? "" : ":", clib_net_to_host_u16 (a->as_u16[i])); last_double_colon = 0; } } return s; } /* Format an IP46 address. */ u8 * format_ip46_address (u8 * s, va_list * args) { ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); ip46_type_t type = va_arg (*args, ip46_type_t); int is_ip4 = 1; switch (type) { case IP46_TYPE_ANY: is_ip4 = ip46_address_is_ip4 (ip46); break; case IP46_TYPE_IP4: is_ip4 = 1; break; case IP46_TYPE_IP6: is_ip4 = 0; break; } return is_ip4 ? format (s, "%U", format_ip4_address, &ip46->ip4) : format (s, "%U", format_ip6_address, &ip46->ip6); } /* * VPPCOM Utility Functions */ static void vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_listen_msg_t *mp; svm_msg_q_t *mq; mq = vcl_worker_ctrl_mq (wrk); app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN); mp = (session_listen_msg_t *) app_evt->evt->data; memset (mp, 0, sizeof (*mp)); mp->client_index = wrk->my_client_index; mp->context = s->session_index; mp->wrk_index = wrk->vpp_wrk_index; mp->is_ip4 = s->transport.is_ip4; clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip)); mp->port = s->transport.lcl_port; mp->proto = s->session_type; app_send_ctrl_evt_to_vpp (mq, app_evt); } static void vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_connect_msg_t *mp; svm_msg_q_t *mq; mq = vcl_worker_ctrl_mq (wrk); app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT); mp = (session_connect_msg_t *) app_evt->evt->data; memset (mp, 0, sizeof (*mp)); mp->client_index = wrk->my_client_index; mp->context = s->session_index; mp->wrk_index = wrk->vpp_wrk_index; mp->is_ip4 = s->transport.is_ip4; mp->parent_handle = s->parent_handle; clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip)); mp->port = s->transport.rmt_port; mp->proto = s->session_type; app_send_ctrl_evt_to_vpp (mq, app_evt); } void vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_unlisten_msg_t *mp; svm_msg_q_t *mq; mq = vcl_worker_ctrl_mq (wrk); app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN); mp = (session_unlisten_msg_t *) app_evt->evt->data; memset (mp, 0, sizeof (*mp)); mp->client_index = wrk->my_client_index; mp->wrk_index = wrk->vpp_wrk_index; mp->handle = s->vpp_handle; mp->context = wrk->wrk_index; app_send_ctrl_evt_to_vpp (mq, app_evt); } static void vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_disconnect_msg_t *mp; svm_msg_q_t *mq; /* Send to thread that owns the session */ mq = s->vpp_evt_q; app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT); mp = (session_disconnect_msg_t *) app_evt->evt->data; memset (mp, 0, sizeof (*mp)); mp->client_index = wrk->my_client_index; mp->handle = s->vpp_handle; app_send_ctrl_evt_to_vpp (mq, app_evt); } static void vcl_send_app_detach (vcl_worker_t * wrk) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_app_detach_msg_t *mp; svm_msg_q_t *mq; mq = vcl_worker_ctrl_mq (wrk); app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH); mp = (session_app_detach_msg_t *) app_evt->evt->data; memset (mp, 0, sizeof (*mp)); mp->client_index = wrk->my_client_index; app_send_ctrl_evt_to_vpp (mq, app_evt); } static void vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context, session_handle_t handle, int retval) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_accepted_reply_msg_t *rmp; app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY); rmp = (session_accepted_reply_msg_t *) app_evt->evt->data; rmp->handle = handle; rmp->context = context; rmp->retval = retval; app_send_ctrl_evt_to_vpp (mq, app_evt); } static void vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context, session_handle_t handle, int retval) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_disconnected_reply_msg_t *rmp; app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECTED_REPLY); rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data; rmp->handle = handle; rmp->context = context; rmp->retval = retval; app_send_ctrl_evt_to_vpp (mq, app_evt); } static void vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context, session_handle_t handle, int retval) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_reset_reply_msg_t *rmp; app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY); rmp = (session_reset_reply_msg_t *) app_evt->evt->data; rmp->handle = handle; rmp->context = context; rmp->retval = retval; app_send_ctrl_evt_to_vpp (mq, app_evt); } void vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s, u32 wrk_index) { app_session_evt_t _app_evt, *app_evt = &_app_evt; session_worker_update_msg_t *mp; svm_msg_q_t *mq; mq = vcl_session_vpp_evt_q (wrk, s); app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE); mp = (session_worker_update_msg_t *) app_evt->evt->data; mp->client_index = wrk->my_client_index; mp->handle = s->vpp_handle; mp->req_wrk_index = wrk->vpp_wrk_index; mp->wrk_index = wrk_index; app_send_ctrl_evt_to_vpp (mq, app_evt); } static u32 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp, u32 ls_index) { vcl_session_t *session, *listen_session; svm_fifo_t *rx_fifo, *tx_fifo; u32 vpp_wrk_index; svm_msg_q_t *evt_q; session = vcl_session_alloc (wrk); listen_session = vcl_session_get (wrk, ls_index); if (listen_session->vpp_handle != mp->listener_handle) { VDBG (0, "ERROR: listener handle %lu does not match session %u", mp->listener_handle, ls_index); goto error; } if (vcl_wait_for_segment (mp->segment_handle)) { VDBG (0, "ERROR: segment for session %u couldn't be mounted!", session->session_index); goto error; } rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *); rx_fifo->client_session_index = session->session_index; tx_fifo->client_session_index = session->session_index; rx_fifo->client_thread_index = vcl_get_worker_index (); tx_fifo->client_thread_index = vcl_get_worker_index (); vpp_wrk_index = tx_fifo->master_thread_index; vec_validate (wrk->vpp_event_queues, vpp_wrk_index); wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q; session->vpp_handle = mp->handle; session->vpp_thread_index = rx_fifo->master_thread_index; session->rx_fifo = rx_fifo; session->tx_fifo = tx_fifo; session->session_state = STATE_ACCEPT; session->transport.rmt_port = mp->rmt.port; session->transport.is_ip4 = mp->rmt.is_ip4; clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip, sizeof (ip46_address_t)); vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index); session->transport.lcl_port = listen_session->transport.lcl_port; session->transport.lcl_ip = listen_session->transport.lcl_ip; session->session_type = listen_session->session_type; session->is_dgram = vcl_proto_is_dgram (session->session_type); session->listener_index = listen_session->session_index; listen_session->n_accepted_sessions++; VDBG (1, "session %u [0x%llx]: client accept request from %s address %U" " port %d queue %p!", session->session_index, mp->handle, mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip, mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q); vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index); vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context, session->vpp_handle, 0); return session->session_index; error: evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *); vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle, VNET_API_ERROR_INVALID_ARGUMENT); vcl_session_free (wrk, session); return VCL_INVALID_SESSION_INDEX; } static u32 vcl_session_connected_handler (vcl_worker_t * wrk, session_connected_msg_t * mp) { u32 session_index, vpp_wrk_index; svm_fifo_t *rx_fifo, *tx_fifo; vcl_session_t *session = 0; session_index = mp->context; session = vcl_session_get (wrk, session_index); if (!session) { VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!", mp->handle, session_index); return VCL_INVALID_SESSION_INDEX; } if (mp->retval) { VDBG (0, "ERROR: session index %u: connect failed! %U", session_index, format_api_error, ntohl (mp->retval)); session->session_state = STATE_FAILED; session->vpp_handle = mp->handle; return session_index; } rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); if (vcl_wait_for_segment (mp->segment_handle)) { VDBG (0, "segment for session %u couldn't be mounted!", session->session_index); session->session_state = STATE_FAILED; return VCL_INVALID_SESSION_INDEX; } rx_fifo->client_session_index = session_index; tx_fifo->client_session_index = session_index; rx_fifo->client_thread_index = vcl_get_worker_index (); tx_fifo->client_thread_index = vcl_get_worker_index (); session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *); vpp_wrk_index = tx_fifo->master_thread_index; vec_validate (wrk->vpp_event_queues, vpp_wrk_index); wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q; if (mp->ct_rx_fifo) { session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *); session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *); if (vcl_wait_for_segment (mp->ct_segment_handle)) { VDBG (0, "ct segment for session %u couldn't be mounted!", session->session_index); session->session_state = STATE_FAILED; return VCL_INVALID_SESSION_INDEX; } } session->rx_fifo = rx_fifo; session->tx_fifo = tx_fifo; session->vpp_handle = mp->handle; session->vpp_thread_index = rx_fifo->master_thread_index; session->transport.is_ip4 = mp->lcl.is_ip4; clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip, sizeof (session->transport.lcl_ip)); session->transport.lcl_port = mp->lcl.port; session->session_state = STATE_CONNECT; /* Add it to lookup table */ vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index); VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p," " refcnt %d", session_index, mp->handle, session->rx_fifo, session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt); return session_index; } static int vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags) { vcl_session_msg_t *accepted_msg; int i; for (i = 0; i < vec_len (session->accept_evts_fifo); i++) { accepted_msg = &session->accept_evts_fifo[i]; if (accepted_msg->accepted_msg.handle == handle) { accepted_msg->flags |= flags; return 1; } } return 0; } static u32 vcl_session_reset_handler (vcl_worker_t * wrk, session_reset_msg_t * reset_msg) { vcl_session_t *session; u32 sid; sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle); session = vcl_session_get (wrk, sid); if (!session) { VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle); return VCL_INVALID_SESSION_INDEX; } /* Caught a reset before actually accepting the session */ if (session->session_state == STATE_LISTEN) { if (!vcl_flag_accepted_session (session, reset_msg->handle, VCL_ACCEPTED_F_RESET)) VDBG (0, "session was not accepted!"); return VCL_INVALID_SESSION_INDEX; } session->session_state = STATE_DISCONNECT; VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle); return sid; } static u32 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp) { vcl_session_t *session; u32 sid = mp->context; session = vcl_session_get (wrk, sid); if (mp->retval) { VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle, format_api_error, mp->retval); if (session) { session->session_state = STATE_FAILED; session->vpp_handle = mp->handle; return sid; } else { VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!", sid, mp->handle); return VCL_INVALID_SESSION_INDEX; } } session->vpp_handle = mp->handle; session->transport.is_ip4 = mp->lcl_is_ip4; clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip, sizeof (ip46_address_t)); session->transport.lcl_port = mp->lcl_port; vcl_session_table_add_listener (wrk, mp->handle, sid); session->session_state = STATE_LISTEN; session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *); vec_validate (wrk->vpp_event_queues, 0); wrk->vpp_event_queues[0] = session->vpp_evt_q; if (session->is_dgram) { svm_fifo_t *rx_fifo, *tx_fifo; session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *); rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *); rx_fifo->client_session_index = sid; tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *); tx_fifo->client_session_index = sid; session->rx_fifo = rx_fifo; session->tx_fifo = tx_fifo; } VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle); return sid; } static void vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data) { session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data; vcl_session_t *s; s = vcl_session_get_w_vpp_handle (wrk, mp->handle); if (!s || s->session_state != STATE_DISCONNECT) { VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle); return; } if (mp->retval) VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U", s->session_index, mp->handle, format_api_error, ntohl (mp->retval)); if (mp->context != wrk->wrk_index) VDBG (0, "wrong context"); vcl_session_table_del_vpp_handle (wrk, mp->handle); vcl_session_free (wrk, s); } static vcl_session_t * vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg) { vcl_session_msg_t *vcl_msg; vcl_session_t *session; session = vcl_session_get_w_vpp_handle (wrk, msg->handle); if (PREDICT_FALSE (session != 0)) VWRN ("session overlap handle %lu state %u!", msg->handle, session->session_state); session = vcl_session_table_lookup_listener (wrk, msg->listener_handle); if (!session) { VERR ("couldn't find listen session: listener handle %llx", msg->listener_handle); return 0; } clib_fifo_add2 (session->accept_evts_fifo, vcl_msg); vcl_msg->accepted_msg = *msg; /* Session handle points to listener until fully accepted by app */ vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index); return session; } static vcl_session_t * vcl_session_disconnected_handler (vcl_worker_t * wrk, session_disconnected_msg_t * msg) { vcl_session_t *session; session = vcl_session_get_w_vpp_handle (wrk, msg->handle); if (!session) { VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle); return 0; } /* Caught a disconnect before actually accepting the session */ if (session->session_state == STATE_LISTEN) { if (!vcl_flag_accepted_session (session, msg->handle, VCL_ACCEPTED_F_CLOSED)) VDBG (0, "session was not accepted!"); return 0; } session->session_state = STATE_VPP_CLOSING; return session; } static void vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data) { session_req_worker_update_msg_t *msg; vcl_session_t *s; msg = (session_req_worker_update_msg_t *) data; s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle); if (!s) return; vec_add1 (wrk->pending_session_wrk_updates, s->session_index); } static void vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data) { session_worker_update_reply_msg_t *msg; vcl_session_t *s; msg = (session_worker_update_reply_msg_t *) data; s = vcl_session_get_w_vpp_handle (wrk, msg->handle); if (!s) { VDBG (0, "unknown handle 0x%llx", msg->handle); return; } if (vcl_wait_for_segment (msg->segment_handle)) { clib_warning ("segment for session %u couldn't be mounted!", s->session_index); return; } if (s->rx_fifo) { s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *); s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *); s->rx_fifo->client_session_index = s->session_index; s->tx_fifo->client_session_index = s->session_index; s->rx_fifo->client_thread_index = wrk->wrk_index; s->tx_fifo->client_thread_index = wrk->wrk_index; } s->session_state = STATE_UPDATED; VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index, s->vpp_handle, wrk->wrk_index); } static int vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e) { session_disconnected_msg_t *disconnected_msg; vcl_session_t *session; switch (e->event_type) { case SESSION_IO_EVT_RX: case SESSION_IO_EVT_TX: session = vcl_session_get (wrk, e->session_index); if (!session || !(session->session_state & STATE_OPEN)) break; vec_add1 (wrk->unhandled_evts_vector, *e); break; case SESSION_CTRL_EVT_ACCEPTED: vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data); break; case SESSION_CTRL_EVT_CONNECTED: vcl_session_connected_handler (wrk, (session_connected_msg_t *) e->data); break; case SESSION_CTRL_EVT_DISCONNECTED: disconnected_msg = (session_disconnected_msg_t *) e->data; session = vcl_session_disconnected_handler (wrk, disconnected_msg); if (!session) break; VDBG (0, "disconnected session %u [0x%llx]", session->session_index, session->vpp_handle); break; case SESSION_CTRL_EVT_RESET: vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data); break; case SESSION_CTRL_EVT_BOUND: vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data); break; case SESSION_CTRL_EVT_UNLISTEN_REPLY: vcl_session_unlisten_reply_handler (wrk, e->data); break; case SESSION_CTRL_EVT_REQ_WORKER_UPDATE: vcl_session_req_worker_update_handler (wrk, e->data); break; case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY: vcl_session_worker_update_reply_handler (wrk, e->data); break; default: clib_warning ("unhandled %u", e->event_type); } return VPPCOM_OK; } static int vppcom_wait_for_session_state_change (u32 session_index, vcl_session_state_t state, f64 wait_for_time) { vcl_worker_t *wrk = vcl_worker_get_current (); f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time; vcl_session_t *volatile session; svm_msg_q_msg_t msg; session_event_t *e; do { session = vcl_session_get (wrk, session_index); if (PREDICT_FALSE (!session)) { return VPPCOM_EBADFD; } if (session->session_state & state) { return VPPCOM_OK; } if (session->session_state & STATE_FAILED) { return VPPCOM_ECONNREFUSED; } if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0)) { usleep (100); continue; } e = svm_msg_q_msg_data (wrk->app_event_queue, &msg); vcl_handle_mq_event (wrk, e); svm_msg_q_free_msg (wrk->app_event_queue, &msg); } while (clib_time_now (&wrk->clib_time) < timeout); VDBG (0, "timeout waiting for state 0x%x (%s)", state, vppcom_session_state_str (state)); vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state); return VPPCOM_ETIMEDOUT; } static void vcl_handle_pending_wrk_updates (vcl_worker_t * wrk) { vcl_session_state_t state; vcl_session_t *s; u32 *sip; if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0)) return; vec_foreach (sip, wrk->pending_session_wrk_updates) { s = vcl_session_get (wrk, *sip); vcl_send_session_worker_update (wrk, s, wrk->wrk_index); state = s->session_state; vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5); s->session_state = state; } vec_reset_length (wrk->pending_session_wrk_updates); } void vcl_flush_mq_events (void) { vcl_worker_t *wrk = vcl_worker_get_current (); svm_msg_q_msg_t *msg; session_event_t *e; svm_msg_q_t *mq; int i; mq = wrk->app_event_queue; svm_msg_q_lock (mq); vcl_mq_dequeue_batch (wrk, mq, ~0); svm_msg_q_unlock (mq); for (i = 0; i < vec_len (wrk->mq_msg_vector); i++) { msg = vec_elt_at_index (wrk->mq_msg_vector, i); e = svm_msg_q_msg_data (mq, msg); vcl_handle_mq_event (wrk, e); svm_msg_q_free_msg (mq, msg); } vec_reset_length (wrk->mq_msg_vector); vcl_handle_pending_wrk_updates (wrk); } static int vppcom_app_session_enable (void) { int rv; if (vcm->app_state != STATE_APP_ENABLED) { vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ ); rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED); if (PREDICT_FALSE (rv)) { VDBG (0, "application session enable timed out! returning %d (%s)", rv, vppcom_retval_str (rv)); return rv; } } return VPPCOM_OK; } static int vppcom_app_attach (void) { int rv; vppcom_app_send_attach (); rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED); if (PREDICT_FALSE (rv)) { VDBG (0, "application attach timed out! returning %d (%s)", rv, vppcom_retval_str (rv)); return rv; } return VPPCOM_OK; } static int vppcom_session_unbind (u32 session_handle) { vcl_worker_t *wrk = vcl_worker_get_current (); session_accepted_msg_t *accepted_msg; vcl_session_t *session = 0; vcl_session_msg_t *evt; session = vcl_session_get_w_handle (wrk, session_handle); if (!session) return VPPCOM_EBADFD; /* Flush pending accept events, if any */ while (clib_fifo_elts (session->accept_evts_fifo)) { clib_fifo_sub2 (session->accept_evts_fifo, evt); accepted_msg = &evt->accepted_msg; vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle); vcl_send_session_accepted_reply (session->vpp_evt_q, accepted_msg->context, session->vpp_handle, -1); } clib_fifo_free (session->accept_evts_fifo); vcl_send_session_unlisten (wrk, session); VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index, session->vpp_handle); vcl_evt (VCL_EVT_UNBIND, session); session->vpp_handle = ~0; session->session_state = STATE_DISCONNECT; return VPPCOM_OK; } static int vppcom_session_disconnect (u32 session_handle) { vcl_worker_t *wrk = vcl_worker_get_current (); svm_msg_q_t *vpp_evt_q; vcl_session_t *session, *listen_session; vcl_session_state_t state; u64 vpp_handle; session = vcl_session_get_w_handle (wrk, session_handle); if (!session) return VPPCOM_EBADFD; vpp_handle = session->vpp_handle; state = session->session_state; VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index, vpp_handle, state, vppcom_session_state_str (state)); if (PREDICT_FALSE (state & STATE_LISTEN)) { VDBG (0, "ERROR: Cannot disconnect a listen socket!"); return VPPCOM_EBADFD; } if (state & STATE_VPP_CLOSING) { vpp_evt_q = vcl_session_vpp_evt_q (wrk, session); vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index, vpp_handle, 0); VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...", session->session_index, vpp_handle); } else { VDBG (1, "session %u [0x%llx]: sending disconnect...", session->session_index, vpp_handle); vcl_send_session_disconnect (wrk, session); } if (session->listener_index != VCL_INVALID_SESSION_INDEX) { listen_session = vcl_session_get (wrk, session->listener_index); listen_session->n_accepted_sessions--; } return VPPCOM_OK; } /** * Handle app exit * * Notify vpp of the disconnect and mark the worker as free. If we're the * last worker, do a full cleanup otherwise, since we're probably a forked * child, avoid syscalls as much as possible. We might've lost privileges. */ void vppcom_app_exit (void) { if (!pool_elts (vcm->workers)) return; vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ ); vcl_set_worker_index (~0); vcl_elog_stop (vcm); if (vec_len (vcm->workers) == 1) vppcom_disconnect_from_vpp (); else vl_client_send_disconnect (1 /* vpp should cleanup */ ); } /* * VPPCOM Public API functions */ int vppcom_app_create (char *app_name) { vppcom_cfg_t *vcl_cfg = &vcm->cfg; int rv; if (vcm->is_init) { VDBG (1, "already initialized"); return VPPCOM_EEXIST; } vcm->is_init = 1; vppcom_cfg (&vcm->cfg); vcl_cfg = &vcm->cfg; vcm->main_cpu = pthread_self (); vcm->main_pid = getpid (); vcm->app_name = format (0, "%s", app_name); vppcom_init_error_string_table (); fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva, 20 /* timeout in secs */ ); pool_alloc (vcm->workers, vcl_cfg->max_workers); clib_spinlock_init (&vcm->workers_lock); clib_rwlock_init (&vcm->segment_table_lock); atexit (vppcom_app_exit); /* Allocate default worker */ vcl_worker_alloc_and_init (); /* API hookup and connect to VPP */ vppcom_api_hookup (); vcl_elog_init (vcm); vcm->app_state = STATE_APP_START; rv = vppcom_connect_to_vpp (app_name); if (rv) { VERR ("couldn't connect to VPP!"); return rv; } VDBG (0, "sending session enable"); rv = vppcom_app_session_enable (); if (rv) { VERR ("vppcom_app_session_enable() failed!"); return rv; } VDBG (0, "sending app attach"); rv = vppcom_app_attach (); if (rv) { VERR ("vppcom_app_attach() failed!"); return rv; } VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name, vcm->workers[0].my_client_index, vcm->workers[0].my_client_index); return VPPCOM_OK; } void vppcom_app_destroy (void) { int rv; f64 orig_app_timeout; if (!pool_elts (vcm->workers)) return; vcl_evt (VCL_EVT_DETACH, vcm); if (pool_elts (vcm->workers) == 1) { vcl_send_app_detach (vcl_worker_get_current ()); orig_app_timeout = vcm->cfg.app_timeout; vcm->cfg.app_timeout = 2.0; rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED); vcm->cfg.app_timeout = orig_app_timeout; if (PREDICT_FALSE (rv)) VDBG (0, "application detach timed out! returning %d (%s)", rv, vppcom_retval_str (rv)); vec_free (vcm->app_name); vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ ); } else { vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ ); } vcl_set_worker_index (~0); vcl_elog_stop (vcm); vl_client_disconnect_from_vlib (); } int vppcom_session_create (u8 proto, u8 is_nonblocking) { vcl_worker_t *wrk = vcl_worker_get_current (); vcl_session_t *session; session = vcl_session_alloc (wrk); session->session_type = proto; session->session_state = STATE_START; session->vpp_handle = ~0; session->is_dgram = vcl_proto_is_dgram (proto); if (is_nonblocking) VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK); vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state, is_nonblocking, session_index); VDBG (0, "created session %u", session->session_index); return vcl_session_handle (session); } int vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session, vcl_session_handle_t sh, u8 do_disconnect) { vcl_session_state_t state; u32 next_sh, vep_sh; int rv = VPPCOM_OK; u64 vpp_handle; u8 is_vep; is_vep = session->is_vep; next_sh = session->vep.next_sh; vep_sh = session->vep.vep_sh; state = session->session_state; vpp_handle = session->vpp_handle; VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle); if (is_vep) { while (next_sh != ~0) { rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0); if (PREDICT_FALSE (rv < 0)) VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u" " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv, vppcom_retval_str (rv)); next_sh = session->vep.next_sh; } } else { if (session->is_vep_session) { rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0); if (rv < 0) VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u " "failed! rv %d (%s)", session->session_index, vpp_handle, vep_sh, rv, vppcom_retval_str (rv)); } if (!do_disconnect) { VDBG (1, "session %u [0x%llx] disconnect skipped", session->session_index, vpp_handle); goto cleanup; } if (state & STATE_LISTEN) { rv = vppcom_session_unbind (sh); if (PREDICT_FALSE (rv < 0)) VDBG (0, "session %u [0x%llx]: listener unbind failed! " "rv %d (%s)", session->session_index, vpp_handle, rv, vppcom_retval_str (rv)); return rv; } else if ((state & STATE_OPEN) || (vcl_session_is_connectable_listener (wrk, session))) { rv = vppcom_session_disconnect (sh); if (PREDICT_FALSE (rv < 0)) VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!" " rv %d (%s)", session->session_index, vpp_handle, rv, vppcom_retval_str (rv)); } else if (state == STATE_DISCONNECT) { svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session); vcl_send_session_reset_reply (mq, wrk->my_client_index, session->vpp_handle, 0); } } VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle); cleanup: vcl_session_table_del_vpp_handle (wrk, vpp_handle); vcl_session_free (wrk, session); vcl_evt (VCL_EVT_CLOSE
/*
 * Copyright (c) 2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/ip/ip6_link.h>
#include <vnet/ip/ip6_ll_table.h>

#include <vnet/ethernet/ethernet.h>
#include <vnet/mfib/ip6_mfib.h>
#include <vnet/adj/adj_mcast.h>

typedef struct ip6_link_delegate_t_
{
  u32 ild_sw_if_index;
  ip6_link_delegate_id_t ild_type;
  index_t ild_index;
} ip6_link_delegate_t;

const static ip6_link_delegate_t ip6_link_delegate_uninit = {
  .ild_sw_if_index = ~0,
};

typedef struct ip6_link_t_
{
  /** interface ip6 is enabled on */
  u32 il_sw_if_index;

  /** link-local address - if unset that IP6 is disabled*/
  ip6_address_t il_ll_addr;

  /** list of delegates */
  ip6_link_delegate_t *il_delegates;

  /** multicast adjacency for this link */
  adj_index_t il_mcast_adj;

  /** number of references to IP6 enabled on this link */
  u32 il_locks;
} ip6_link_t;

#define FOREACH_IP6_LINK_DELEGATE(_ild, _il, body)      \
{                                                       \
 if (NULL != _il) {                                     \
   vec_foreach (_ild, _il->il_delegates) {              \
     if (ip6_link_delegate_is_init(_ild))               \
       body;                                            \
   }                                                    \
 }                                                      \
}

#define FOREACH_IP6_LINK_DELEGATE_ID(_id) \
  for (_id = 0; _id < il_delegate_id; _id++)

/** last used delegate ID */
static ip6_link_delegate_id_t il_delegate_id;

/** VFT registered per-delegate type */
static ip6_link_delegate_vft_t *il_delegate_vfts;

/** Per interface configs */
static ip6_link_t *ip6_links;

/** Randomizer */
static u64 il_randomizer;

/** Logging */
static vlib_log_class_t ip6_link_logger;

#define IP6_LINK_DBG(...)                       \
    vlib_log_debug (ip6_link_logger, __VA_ARGS__);

#define IP6_LINK_INFO(...)                              \
    vlib_log_notice (ip6_link_logger, __VA_ARGS__);

static bool
ip6_link_delegate_is_init (const ip6_link_delegate_t * ild)
{
  return (~0 != ild->ild_sw_if_index);
}

static bool
ip6_link_is_enabled_i (const ip6_link_t * il)
{
  return (!ip6_address_is_zero (&il->il_ll_addr));
}

static void
ip6_link_local_address_from_mac (ip6_address_t * ip, const u8 * mac)
{
  ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
  /* Invert the "u" bit */
  ip->as_u8[8] = mac[0] ^ (1 << 1);
  ip->as_u8[9] = mac[1];
  ip->as_u8[10] = mac[2];
  ip->as_u8[11] = 0xFF;
  ip->as_u8[12] = 0xFE;
  ip->as_u8[13] = mac[3];
  ip->as_u8[14] = mac[4];
  ip->as_u8[15] = mac[5];
}

static void
ip6_mac_address_from_link_local (u8 * mac, const ip6_address_t * ip)
{
  /* Invert the previously inverted "u" bit */
  mac[0] = ip->as_u8[8] ^ (1 << 1);
  mac[1] = ip->as_u8[9];
  mac[2] = ip->as_u8[10];
  mac[3] = ip->as_u8[13];
  mac[4] = ip->as_u8[14];
  mac[5] = ip->as_u8[15];
}

static ip6_link_t *
ip6_link_get (u32 sw_if_index)
{
  ip6_link_t *il;

  if (sw_if_index >= vec_len (ip6_links))
    return (NULL);

  il = &ip6_links[sw_if_index];

  if (!ip6_link_is_enabled_i (il))
    return (NULL);

  return (il);
}

bool
ip6_link_is_enabled (u32 sw_if_index)
{
  return (NULL != ip6_link_get (sw_if_index));
}


int
ip6_link_enable (u32 sw_if_index, const ip6_address_t * link_local_addr)
{
  ip6_link_t *il;
  int rv;

  il = ip6_link_get (sw_if_index);

  if (NULL == il)
    {
      const vnet_sw_interface_t *sw, *sw_sup;
      const ethernet_interface_t *eth;
      vnet_main_t *vnm;

      vnm = vnet_get_main ();

      IP6_LINK_INFO ("enable: %U",
		     format_vnet_sw_if_index_name, vnm, sw_if_index);

      sw_sup = vnet_get_sup_sw_interface (vnm, sw_if_index);
      if (sw_sup->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
	{
	  rv = VNET_API_ERROR_UNSUPPORTED;
	  goto out;
	}

      eth = ethernet_get_interface (&ethernet_main, sw_sup->hw_if_index);

      if (NULL == eth)
	{
	  rv = VNET_API_ERROR_UNSUPPORTED;
	  goto out;
	}

      vec_validate (ip6_links, sw_if_index);

      il = &ip6_links[sw_if_index];
      il->il_locks = 1;
      il->il_sw_if_index = sw_if_index;

      sw = vnet_get_sup_sw_interface (vnm, sw_if_index);

      if (NULL != link_local_addr)
	ip6_address_copy (&il->il_ll_addr, link_local_addr);
      else if (sw->type == VNET_SW_INTERFACE_TYPE_SUB ||
	       sw->type == VNET_SW_INTERFACE_TYPE_PIPE ||
	       sw->type == VNET_SW_INTERFACE_TYPE_P2P)
	{
	  il->il_ll_addr.as_u64[0] =
	    clib_host_to_net_u64 (0xFE80000000000000ULL);

	  /* make up an interface id */
	  il->il_ll_addr.as_u64[1] = random_u64 (&il_randomizer);

	  /* clear u bit */
	  il->il_ll_addr.as_u8[8] &= 0xfd;
	}
      else
	{
	  ip6_link_local_address_from_mac (&il->il_ll_addr, eth->address);
	}

      {
	ip6_ll_prefix_t ilp = {
	  .ilp_addr = il->il_ll_addr,
	  .ilp_sw_if_index = sw_if_index,
	};

	ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
      }

      /* essentially "enables" ipv6 on this interface */
      ip6_mfib_interface_enable_disable (sw_if_index, 1);
      ip6_sw_interface_enable_disable (sw_if_index, 1);

      il->il_mcast_adj = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
						VNET_LINK_IP6, sw_if_index);

      /* inform all register clients */
      ip6_link_delegate_id_t id;
      FOREACH_IP6_LINK_DELEGATE_ID (id)
      {
	if (NULL != il_delegate_vfts[id].ildv_enable)
	  il_delegate_vfts[id].ildv_enable (il->il_sw_if_index);
      }

      rv = 0;
    }
  else
    {
      rv = VNET_API_ERROR_VALUE_EXIST;
    }

out:
  return (rv);
}

static void
ip6_link_delegate_flush (ip6_link_t * il)
{
  ip6_link_delegate_t *ild;

  /* *INDENT-OFF* */
  FOREACH_IP6_LINK_DELEGATE (ild, il,
  ({
    il_delegate_vfts[ild->ild_type].ildv_disable(ild->ild_index);
  }));
  /* *INDENT-ON* */

  vec_free (il->il_delegates);
  il->il_delegates = NULL;
}

static void
ip6_link_last_lock_gone (ip6_link_t * il)
{
  ip6_ll_prefix_t ilp = {
    .ilp_addr = il->il_ll_addr,
    .ilp_sw_if_index = il->il_sw_if_index,
  };

  IP6_LINK_INFO ("last-lock: %U",
		 format_vnet_sw_if_index_name,
		 vnet_get_main (), il->il_sw_if_index);

  ip6_link_delegate_flush (il);
  ip6_ll_table_entry_delete (&ilp);

  ip6_mfib_interface_enable_disable (il->il_sw_if_index, 0);
  ip6_sw_interface_enable_disable (il->il_sw_if_index, 0);

  ip6_address_set_zero (&il->il_ll_addr);
  adj_unlock (il->il_mcast_adj);
  il->il_mcast_adj = ADJ_INDEX_INVALID;
}

static void
ip6_link_unlock (ip6_link_t * il)
{
  if (NULL == il)
    return;

  il->il_locks--;

  if (0 == il->il_locks)
    ip6_link_last_lock_gone (il);
}

int
ip6_link_disable (u32 sw_if_index)
{
  ip6_link_t *il;

  il = ip6_link_get (sw_if_index);

  if (NULL == il)
    return (VNET_API_ERROR_IP6_NOT_ENABLED);

  IP6_LINK_INFO ("disable: %U",
		 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index);

  ip6_link_unlock (il);

  return (0);
}

const ip6_address_t *
ip6_get_link_local_address (u32 sw_if_index)
{
  const ip6_link_t *il;

  vec_validate (ip6_links, sw_if_index);

  il = &ip6_links[sw_if_index];

  return (&il->il_ll_addr);
}

adj_index_t
ip6_link_get_mcast_adj (u32 sw_if_index)
{
  const ip6_link_t *il;

  il = &ip6_links[sw_if_index];

  return (il->il_mcast_adj);
}

int
ip6_src_address_for_packet (u32 sw_if_index,
			    const ip6_address_t * dst, ip6_address_t * src)
{
  ip_lookup_main_t *lm;

  lm = &ip6_main.lookup_main;

  if (ip6_address_is_link_local_unicast (dst))
    {
      ip6_address_copy (src, ip6_get_link_local_address (sw_if_index));

      return (!0);
    }
  else
    {
      u32 if_add_index =
	lm->if_address_pool_index_by_sw_if_index[sw_if_index];
      if (PREDICT_TRUE (if_add_index != ~0))
	{
	  ip_interface_address_t *if_add =
	    pool_elt_at_index (lm->if_address_pool, if_add_index);
	  ip6_address_t *if_ip =
	    ip_interface_address_get_address (lm, if_add);
	  *src = *if_ip;
	  return (!0);
	}
    }

  src->as_u64[0] = 0;
  src->as_u64[1] = 0;

  return (0);
}

int
ip6_link_set_local_address (u32 sw_if_index, const ip6_address_t * address)
{
  ip6_link_delegate_t *ild;
  ip6_link_t *il;

  il = ip6_link_get (sw_if_index);

  if (NULL == il)
    return ip6_link_enable (sw_if_index, address);

  ip6_ll_prefix_t ilp = {
    .ilp_addr = il->il_ll_addr,
    .ilp_sw_if_index = sw_if_index,
  };

  IP6_LINK_INFO ("set-ll: %U -> %U",
		 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
		 format_ip6_address, address);

  ip6_ll_table_entry_delete (&ilp);
  ip6_address_copy (&il->il_ll_addr, address);
  ip6_address_copy (&ilp.ilp_addr, address);
  ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);

  /* *INDENT-OFF* */
  FOREACH_IP6_LINK_DELEGATE (ild, il,
  ({
    if (NULL != il_delegate_vfts[ild->ild_type].ildv_ll_change)
      il_delegate_vfts[ild->ild_type].ildv_ll_change(ild->ild_index,
                                                     &il->il_ll_addr);
  }));
  /* *INDENT-ON* */

  return (0);
}

ip6_link_delegate_id_t
ip6_link_delegate_register (const ip6_link_delegate_vft_t * vft)
{
  ip6_link_delegate_id_t rc = il_delegate_id++;

  ASSERT (vft->ildv_disable);

  vec_validate (il_delegate_vfts, rc);

  il_delegate_vfts[rc] = *vft;

  return (rc);
}

index_t
ip6_link_delegate_get (u32 sw_if_index, ip6_link_delegate_id_t id)
{
  ip6_link_t *il;

  il = ip6_link_get (sw_if_index);

  if (NULL == il)
    return (INDEX_INVALID);

  vec_validate_init_empty (il->il_delegates, id, ip6_link_delegate_uninit);

  if (!ip6_link_delegate_is_init (&il->il_delegates[id]))
    return (INDEX_INVALID);

  return (il->il_delegates[id].ild_index);
}

bool
ip6_link_delegate_update (u32 sw_if_index,
			  ip6_link_delegate_id_t id, index_t ii)
{
  ip6_link_t *il;

  il = ip6_link_get (sw_if_index);

  if (NULL == il)
    return (false);

  vec_validate_init_empty (il->il_delegates, id, ip6_link_delegate_uninit);

  il->il_delegates[id].ild_sw_if_index = sw_if_index;
  il->il_delegates[id].ild_type = id;
  il->il_delegates[id].ild_index = ii;

  return (true);
}

void
ip6_link_delegate_remove (u32 sw_if_index,
			  ip6_link_delegate_id_t id, index_t ii)
{
  ip6_link_t *il;

  il = ip6_link_get (sw_if_index);

  if (NULL != il)
    {
      if (vec_len (il->il_delegates) > id)
	{
	  clib_memcpy (&il->il_delegates[id],
		       &ip6_link_delegate_uninit,
		       sizeof (il->il_delegates[0]));
	}
    }
}

static void
ip6_link_add_del_address (ip6_main_t * im,
			  uword opaque,
			  u32 sw_if_index,
			  ip6_address_t * address,
			  u32 address_length,
			  u32 if_address_index, u32 is_delete)
{
  const ip6_link_delegate_t *ild;
  ip6_link_t *il;

  if (ip6_address_is_link_local_unicast (address))
    // only interested in global addresses here
    return;

  IP6_LINK_INFO ("addr-%s: %U -> %U",
		 (is_delete ? "del" : "add"),
		 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
		 format_ip6_address, address);

  il = ip6_link_get (sw_if_index);

  if (NULL == il)
    return;

  /* *INDENT-OFF* */
  FOREACH_IP6_LINK_DELEGATE (ild, il,
  ({
      if (is_delete)
        {
          if (NULL != il_delegate_vfts[ild->ild_type].ildv_addr_del)
            il_delegate_vfts[ild->ild_type].ildv_addr_del(ild->ild_index,
                                                          address, address_length);
        }
      else
        {
          if (NULL != il_delegate_vfts[ild->ild_type].ildv_addr_add)
            il_delegate_vfts[ild->ild_type].ildv_addr_add(ild->ild_index,
                                                          address, address_length);
        }
  }));
  /* *INDENT-ON* */
}

static clib_error_t *
ip6_link_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
{
  if (!is_add)
    {
      ip6_link_t *il;

      il = ip6_link_get (sw_if_index);

      IP6_LINK_DBG ("link-del: %U",
		    format_vnet_sw_if_index_name, vnet_get_main (),
		    sw_if_index);

      if (NULL != il)
	/* force cleanup */
	ip6_link_last_lock_gone (il);
    }

  return (NULL);
}

VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_link_interface_add_del);

static clib_error_t *
ip6_link_init (vlib_main_t * vm)
{
  il_randomizer = clib_cpu_time_now ();
  ip6_link_logger = vlib_log_register_class ("ip6", "link");

  {
    ip6_add_del_interface_address_callback_t cb = {
      .function = ip6_link_add_del_address,
    };
    vec_add1 (ip6_main.add_del_interface_address_callbacks, cb);
  }
  return (NULL);
}

VLIB_INIT_FUNCTION (ip6_link_init);


static clib_error_t *
test_ip6_link_command_fn (vlib_main_t * vm,
			  unformat_input_t * input, vlib_cli_command_t * cmd)
{
  u8 mac[6];
  ip6_address_t _a</