int main(int argc, char *argv[]) { struct nl_cache_mngr *mngr; struct nl_cache *cache; int err, i; dp.dp_fd = stdout; signal(SIGINT, sigint); err = nl_cache_mngr_alloc(NULL, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr); if (err < 0) nl_cli_fatal(err, "Unable to allocate cache manager: %s", nl_geterror(err)); for (i = 1; i < argc; i++) { err = nl_cache_mngr_add(mngr, argv[i], &change_cb, NULL, &cache); if (err < 0) nl_cli_fatal(err, "Unable to add cache %s: %s", argv[i], nl_geterror(err)); } while (!quit) { int err = nl_cache_mngr_poll(mngr, 1000); if (err < 0 && err != -NLE_INTR) nl_cli_fatal(err, "Polling failed: %s", nl_geterror(err)); nl_cache_mngr_info(mngr, &dp); } nl_cache_mngr_free(mngr); return 0; }
void ind_ovs_port_init(void) { int nlerr; route_cache_sock = nl_socket_alloc(); if (route_cache_sock == NULL) { LOG_ERROR("nl_socket_alloc failed"); abort(); } if ((nlerr = nl_cache_mngr_alloc(route_cache_sock, NETLINK_ROUTE, 0, &route_cache_mngr)) < 0) { LOG_ERROR("nl_cache_mngr_alloc failed: %s", nl_geterror(nlerr)); abort(); } if ((nlerr = nl_cache_mngr_add(route_cache_mngr, "route/link", link_change_cb, NULL, &link_cache)) < 0) { LOG_ERROR("nl_cache_mngr_add failed: %s", nl_geterror(nlerr)); abort(); } if (ind_soc_socket_register(nl_cache_mngr_get_fd(route_cache_mngr), (ind_soc_socket_ready_callback_f)route_cache_mngr_socket_cb, NULL) < 0) { LOG_ERROR("failed to register socket"); abort(); } netlink_callbacks = nl_cb_alloc(NL_CB_DEFAULT); if (netlink_callbacks == NULL) { LOG_ERROR("failed to allocate netlink callbacks"); abort(); } }
int main(int argc, char *argv[]) { struct nl_cache_mngr *mngr; struct nl_sock *sock; struct nl_cache *ct; sock = nlt_socket_alloc(); mngr = nl_cache_mngr_alloc(sock, NETLINK_NETFILTER, NL_AUTO_PROVIDE); if (!mngr) { nl_perror("nl_cache_mngr_alloc"); return -1; } ct = nl_cache_mngr_add(mngr, "netfilter/ct", &change_cb); if (ct == NULL) { nl_perror("nl_cache_mngr_add(netfilter/ct)"); return -1; } for (;;) { int err = nl_cache_mngr_poll(mngr, 5000); if (err < 0) { nl_perror("nl_cache_mngr_poll()"); return -1; } } nl_cache_mngr_free(mngr); return 0; }
int main(int argc, char *argv[]) { struct nl_cache_mngr *mngr; struct nl_cache *lc, *nc, *ac, *rc; struct nl_sock *sock; int err; signal(SIGINT, sigint); sock = nlt_alloc_socket(); err = nl_cache_mngr_alloc(sock, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr); if (err < 0) fatal(err, "Unable to allocate cache manager: %s", nl_geterror(err)); if ((err = nl_cache_mngr_add(mngr, "route/link", &change_cb, &lc)) < 0) fatal(err, "Unable to add cache route/link: %s", nl_geterror(err)); if ((err = nl_cache_mngr_add(mngr, "route/neigh", &change_cb, &nc)) < 0) fatal(err, "Unable to add cache route/neigh: %s", nl_geterror(err)); if ((err = nl_cache_mngr_add(mngr, "route/addr", &change_cb, &ac)) < 0) fatal(err, "Unable to add cache route/addr: %s", nl_geterror(err)); if ((err = nl_cache_mngr_add(mngr, "route/route", &change_cb, &rc)) < 0) fatal(err, "Unable to add cache route/route: %s", nl_geterror(err)); while (!quit) { int err = nl_cache_mngr_poll(mngr, 5000); if (err < 0 && err != -NLE_INTR) fatal(err, "Polling failed: %s", nl_geterror(err)); } nl_cache_mngr_free(mngr); nl_socket_free(sock); return 0; }
int netlink_wrapper::open_channel() { auto_unlocker lock(m_cache_lock); nl_logdbg("opening netlink channel"); /* // build to subscriptions groups mask for indicating what type of events the kernel will send on channel unsigned subscriptions = ~RTMGRP_TC; if (netlink_route_group_mask & nlgrpLINK) { subscriptions |= (1 << (RTNLGRP_LINK - 1)); } if (netlink_route_group_mask & nlgrpADDRESS) { if (!m_preferred_family || m_preferred_family == AF_INET) subscriptions |= (1 << (RTNLGRP_IPV4_IFADDR - 1)); if (!m_preferred_family || m_preferred_family == AF_INET6) subscriptions |= (1 << (RTNLGRP_IPV6_IFADDR - 1)); } if (netlink_route_group_mask & nlgrpROUTE) { if (!m_preferred_family || m_preferred_family == AF_INET) subscriptions |= (1 << (RTNLGRP_IPV4_ROUTE - 1)); if (!m_preferred_family || m_preferred_family == AF_INET6) subscriptions |= (1 << (RTNLGRP_IPV4_ROUTE - 1)); } if (netlink_route_group_mask & nlgrpPREFIX) { if (!m_preferred_family || m_preferred_family == AF_INET6) subscriptions |= (1 << (RTNLGRP_IPV6_PREFIX - 1)); } if (netlink_route_group_mask & nlgrpNEIGH) { subscriptions |= (1 << (RTNLGRP_NEIGH - 1)); } */ // Allocate a new netlink handle m_handle = nl_handle_alloc(); BULLSEYE_EXCLUDE_BLOCK_START if (m_handle == NULL) { nl_logerr("failed to allocate netlink handle, nl_errno=%d", nl_get_errno()); return -1; } BULLSEYE_EXCLUDE_BLOCK_END // set internal structure to pass the handle with callbacks from netlink g_nl_rcv_arg.handle = m_handle; // if multiple handles being allocated then a unique netlink PID need to be provided // If port is 0, a unique port identifier will be generated automatically as a unique PID nl_socket_set_local_port(m_handle, 0); //Disables checking of sequence numbers on the netlink handle. //This is required to allow messages to be processed which were not requested by a preceding request message, e.g. netlink events. nl_disable_sequence_check(m_handle); //joining group //nl_join_groups(m_handle, 0); // Allocate a new cache manager for RTNETLINK // NL_AUTO_PROVIDE = automatically provide the caches added to the manager. m_mngr = nl_cache_mngr_alloc(m_handle, NETLINK_ROUTE, NL_AUTO_PROVIDE); BULLSEYE_EXCLUDE_BLOCK_START if (!m_mngr) { nl_logerr("Fail to allocate cac he manager"); return -1; } BULLSEYE_EXCLUDE_BLOCK_END nl_logdbg("netlink socket is open"); m_cache_neigh = nl_cache_mngr_add(m_mngr, "route/neigh", neigh_cache_callback); m_cache_link = nl_cache_mngr_add(m_mngr, "route/link", link_cache_callback); m_cache_route = nl_cache_mngr_add(m_mngr, "route/route", route_cache_callback); // set custom callback for every message to update message nl_socket_modify_cb(m_handle, NL_CB_MSG_IN, NL_CB_CUSTOM, nl_msg_rcv_cb ,NULL); // set the socket non-blocking BULLSEYE_EXCLUDE_BLOCK_START if (nl_socket_set_nonblocking(m_handle)) { nl_logerr("Failed to set the socket non-blocking"); return -1; } BULLSEYE_EXCLUDE_BLOCK_END return 0; }