aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lacp/cli.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/lacp/cli.c')
-rw-r--r--src/plugins/lacp/cli.c393
1 files changed, 393 insertions, 0 deletions
diff --git a/src/plugins/lacp/cli.c b/src/plugins/lacp/cli.c
new file mode 100644
index 00000000000..10627774f56
--- /dev/null
+++ b/src/plugins/lacp/cli.c
@@ -0,0 +1,393 @@
+/*
+ * Copyright (c) 2017 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.
+ */
+
+#define _GNU_SOURCE
+#include <vnet/bonding/node.h>
+#include <lacp/node.h>
+
+int
+lacp_dump_ifs (lacp_interface_details_t ** out_lacpifs)
+{
+ vnet_main_t *vnm = vnet_get_main ();
+ bond_main_t *bm = &bond_main;
+ slave_if_t *sif;
+ bond_if_t *bif;
+ vnet_hw_interface_t *hi;
+ lacp_interface_details_t *r_lacpifs = NULL;
+ lacp_interface_details_t *lacpif = NULL;
+
+ /* *INDENT-OFF* */
+ pool_foreach (sif, bm->neighbors,
+ if ((sif->port_enabled == 0) || (sif->lacp_enabled == 0))
+ continue;
+ vec_add2(r_lacpifs, lacpif, 1);
+ memset (lacpif, 0, sizeof (*lacpif));
+ lacpif->sw_if_index = sif->sw_if_index;
+ hi = vnet_get_hw_interface (vnm, sif->hw_if_index);
+ clib_memcpy(lacpif->interface_name, hi->name,
+ MIN (ARRAY_LEN (lacpif->interface_name) - 1,
+ strlen ((const char *) hi->name)));
+ bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
+ hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
+ clib_memcpy(lacpif->bond_interface_name, hi->name,
+ MIN (ARRAY_LEN (lacpif->bond_interface_name) - 1,
+ strlen ((const char *) hi->name)));
+ clib_memcpy (lacpif->actor_system, sif->actor.system, 6);
+ lacpif->actor_system_priority = sif->actor.system_priority;
+ lacpif->actor_key = sif->actor.key;
+ lacpif->actor_port_priority = sif->actor.port_priority;
+ lacpif->actor_port_number = sif->actor.port_number;
+ lacpif->actor_state = sif->actor.state;
+ clib_memcpy (lacpif->partner_system, sif->partner.system, 6);
+ lacpif->partner_system_priority = sif->partner.system_priority;
+ lacpif->partner_key = sif->partner.key;
+ lacpif->partner_port_priority = sif->partner.port_priority;
+ lacpif->partner_port_number = sif->partner.port_number;
+ lacpif->partner_state = sif->partner.state;
+ lacpif->rx_state = sif->rx_state;
+ lacpif->tx_state = sif->tx_state;
+ lacpif->ptx_state = sif->ptx_state;
+ lacpif->mux_state = sif->mux_state;
+ );
+ /* *INDENT-ON* */
+
+ *out_lacpifs = r_lacpifs;
+
+ return 0;
+}
+
+static void
+show_lacp (vlib_main_t * vm, u32 * sw_if_indices)
+{
+ int i;
+ slave_if_t *sif;
+ bond_if_t *bif;
+
+ if (!sw_if_indices)
+ return;
+
+ vlib_cli_output (vm, "%-55s %-32s %-32s", " ", "actor state",
+ "partner state");
+ vlib_cli_output (vm, "%-25s %-12s %-16s %-31s %-31s", "interface name",
+ "sw_if_index", "bond interface",
+ "exp/def/dis/col/syn/agg/tim/act",
+ "exp/def/dis/col/syn/agg/tim/act");
+
+ for (i = 0; i < vec_len (sw_if_indices); i++)
+ {
+ sif = bond_get_slave_by_sw_if_index (sw_if_indices[i]);
+ if (!sif || (sif->port_enabled == 0) || (sif->lacp_enabled == 0))
+ continue;
+ bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
+ vlib_cli_output (vm,
+ "%-25U %-12d %-16U %3x %3x %3x %3x %3x %3x %3x %3x "
+ "%4x %3x %3x %3x %3x %3x %3x %3x",
+ format_vnet_sw_if_index_name, vnet_get_main (),
+ sif->sw_if_index, sif->sw_if_index,
+ format_vnet_sw_if_index_name, vnet_get_main (),
+ bif->sw_if_index, lacp_bit_test (sif->actor.state, 7),
+ lacp_bit_test (sif->actor.state, 6),
+ lacp_bit_test (sif->actor.state, 5),
+ lacp_bit_test (sif->actor.state, 4),
+ lacp_bit_test (sif->actor.state, 3),
+ lacp_bit_test (sif->actor.state, 2),
+ lacp_bit_test (sif->actor.state, 1),
+ lacp_bit_test (sif->actor.state, 0),
+ lacp_bit_test (sif->partner.state, 7),
+ lacp_bit_test (sif->partner.state, 6),
+ lacp_bit_test (sif->partner.state, 5),
+ lacp_bit_test (sif->partner.state, 4),
+ lacp_bit_test (sif->partner.state, 3),
+ lacp_bit_test (sif->partner.state, 2),
+ lacp_bit_test (sif->partner.state, 1),
+ lacp_bit_test (sif->partner.state, 0));
+ vlib_cli_output (vm,
+ " LAG ID: "
+ "[(%04x,%02x-%02x-%02x-%02x-%02x-%02x,%04x,%04x,%04x), "
+ "(%04x,%02x-%02x-%02x-%02x-%02x-%02x,%04x,%04x,%04x)]",
+ ntohs (sif->actor.system_priority),
+ sif->actor.system[0], sif->actor.system[1],
+ sif->actor.system[2], sif->actor.system[3],
+ sif->actor.system[4], sif->actor.system[5],
+ ntohs (sif->actor.key),
+ ntohs (sif->actor.port_priority),
+ ntohs (sif->actor.port_number),
+ ntohs (sif->partner.system_priority),
+ sif->partner.system[0], sif->partner.system[1],
+ sif->partner.system[2], sif->partner.system[3],
+ sif->partner.system[4], sif->partner.system[5],
+ ntohs (sif->partner.key),
+ ntohs (sif->partner.port_priority),
+ ntohs (sif->partner.port_number));
+ vlib_cli_output (vm,
+ " RX-state: %U, TX-state: %U, "
+ "MUX-state: %U, PTX-state: %U",
+ format_rx_sm_state, sif->rx_state, format_tx_sm_state,
+ sif->tx_state, format_mux_sm_state, sif->mux_state,
+ format_ptx_sm_state, sif->ptx_state);
+ }
+}
+
+static void
+show_lacp_details (vlib_main_t * vm, u32 * sw_if_indices)
+{
+ slave_if_t *sif;
+ lacp_state_struct *state_entry;
+ int i;
+ f64 now;
+
+ if (!sw_if_indices)
+ return;
+
+ now = vlib_time_now (vm);
+ for (i = 0; i < vec_len (sw_if_indices); i++)
+ {
+ sif = bond_get_slave_by_sw_if_index (sw_if_indices[i]);
+ if (!sif || (sif->port_enabled == 0) || (sif->lacp_enabled == 0))
+ continue;
+ vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
+ vnet_get_main (), sif->sw_if_index);
+ vlib_cli_output (vm, " debug: %d", sif->debug);
+ vlib_cli_output (vm, " loopback port: %d", sif->loopback_port);
+ vlib_cli_output (vm, " port moved: %d", sif->port_moved);
+ vlib_cli_output (vm, " ready_n: %d", sif->ready_n);
+ vlib_cli_output (vm, " ready: %d", sif->ready);
+ vlib_cli_output (vm, " Actor");
+ vlib_cli_output (vm, " system: %U",
+ format_ethernet_address, sif->actor.system);
+ vlib_cli_output (vm, " system priority: %u",
+ ntohs (sif->actor.system_priority));
+ vlib_cli_output (vm, " key: %u", ntohs (sif->actor.key));
+ vlib_cli_output (vm, " port priority: %u",
+ ntohs (sif->actor.port_priority));
+ vlib_cli_output (vm, " port number: %u",
+ ntohs (sif->actor.port_number));
+ vlib_cli_output (vm, " state: 0x%x", sif->actor.state);
+
+ state_entry = (lacp_state_struct *) & lacp_state_array;
+ while (state_entry->str)
+ {
+ if (sif->actor.state & (1 << state_entry->bit))
+ vlib_cli_output (vm, " %s (%d)", state_entry->str,
+ state_entry->bit);
+ state_entry++;
+ }
+
+ vlib_cli_output (vm, " Partner");
+ vlib_cli_output (vm, " system: %U",
+ format_ethernet_address, sif->partner.system);
+ vlib_cli_output (vm, " system priority: %u",
+ ntohs (sif->partner.system_priority));
+ vlib_cli_output (vm, " key: %u", ntohs (sif->partner.key));
+ vlib_cli_output (vm, " port priority: %u",
+ ntohs (sif->partner.port_priority));
+ vlib_cli_output (vm, " port number: %u",
+ ntohs (sif->partner.port_number));
+ vlib_cli_output (vm, " state: 0x%x", sif->partner.state);
+
+ state_entry = (lacp_state_struct *) & lacp_state_array;
+ while (state_entry->str)
+ {
+ if (sif->partner.state & (1 << state_entry->bit))
+ vlib_cli_output (vm, " %s (%d)", state_entry->str,
+ state_entry->bit);
+ state_entry++;
+ }
+
+ if (!lacp_timer_is_running (sif->wait_while_timer))
+ vlib_cli_output (vm, " wait while timer: not running");
+ else
+ vlib_cli_output (vm, " wait while timer: %=10.2f seconds",
+ sif->wait_while_timer - now);
+ if (!lacp_timer_is_running (sif->current_while_timer))
+ vlib_cli_output (vm, " current while timer: not running");
+ else
+ vlib_cli_output (vm, " current while timer: %=10.2f seconds",
+ sif->current_while_timer - now);
+ if (!lacp_timer_is_running (sif->periodic_timer))
+ vlib_cli_output (vm, " periodic timer: not running");
+ else
+ vlib_cli_output (vm, " periodic timer: %=10.2f seconds",
+ sif->periodic_timer - now);
+ vlib_cli_output (vm, " RX-state: %U", format_rx_sm_state,
+ sif->rx_state);
+ vlib_cli_output (vm, " TX-state: %U", format_tx_sm_state,
+ sif->tx_state);
+ vlib_cli_output (vm, " MUX-state: %U", format_mux_sm_state,
+ sif->mux_state);
+ vlib_cli_output (vm, " PTX-state: %U", format_ptx_sm_state,
+ sif->ptx_state);
+ vlib_cli_output (vm, "\n");
+ }
+}
+
+static clib_error_t *
+show_lacp_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ bond_main_t *bm = &bond_main;
+ vnet_main_t *vnm = &vnet_main;
+ slave_if_t *sif;
+ clib_error_t *error = 0;
+ u8 details = 0;
+ u32 hw_if_index, *sw_if_indices = 0;
+ vnet_interface_main_t *im = &vnm->interface_main;
+ vnet_sw_interface_t *sw;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat
+ (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
+ {
+ sw = pool_elt_at_index (im->sw_interfaces, hw_if_index);
+ sif = bond_get_slave_by_sw_if_index (sw->sw_if_index);
+ if (!sif)
+ {
+ error = clib_error_return (0, "interface is not enslaved");
+ goto done;
+ }
+ vec_add1 (sw_if_indices, sif->sw_if_index);
+ }
+ else if (unformat (input, "details"))
+ details = 1;
+ else
+ {
+ error = clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ goto done;
+ }
+ }
+
+ if (vec_len (sw_if_indices) == 0)
+ {
+ pool_foreach (sif, bm->neighbors,
+ vec_add1 (sw_if_indices, sif->sw_if_index);
+ );
+ }
+
+ if (details)
+ show_lacp_details (vm, sw_if_indices);
+ else
+ show_lacp (vm, sw_if_indices);
+
+done:
+ vec_free (sw_if_indices);
+ return error;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_lacp_command, static) = {
+ .path = "show lacp",
+ .short_help = "show lacp [<interface>] [details]",
+ .function = show_lacp_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+debug_lacp_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ unformat_input_t _line_input, *line_input = &_line_input;
+ clib_error_t *error = NULL;
+ lacp_main_t *lm = &lacp_main;
+ u8 onoff = 0;
+ u8 input_found = 0;
+ u32 hw_if_index = ~0;
+ slave_if_t *sif;
+ vnet_main_t *vnm = vnet_get_main ();
+ vnet_interface_main_t *im = &vnm->interface_main;
+ vnet_sw_interface_t *sw;
+
+ /* Get a line of input. */
+ if (!unformat_user (input, unformat_line_input, line_input))
+ return clib_error_return (0, "missing argument");
+
+ while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (line_input, "%U",
+ unformat_vnet_hw_interface, vnm, &hw_if_index))
+ ;
+ if (input_found)
+ {
+ error = clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, line_input);
+ goto done;
+ }
+ else if (unformat (line_input, "on"))
+ {
+ input_found = 1;
+ onoff = 1;
+ }
+ else if (unformat (line_input, "off"))
+ {
+ input_found = 1;
+ onoff = 0;
+ }
+ else
+ {
+ error = clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, line_input);
+ goto done;
+ }
+ }
+
+ if (!input_found)
+ return clib_error_return (0, "must specify on or off");
+
+ if (hw_if_index != ~0)
+ {
+ sw = pool_elt_at_index (im->sw_interfaces, hw_if_index);
+ sif = bond_get_slave_by_sw_if_index (sw->sw_if_index);
+ if (!sif)
+ return (clib_error_return (0, "Please enslave the interface first"));
+ sif->debug = onoff;
+ }
+ else
+ lm->debug = onoff;
+
+done:
+ unformat_free (line_input);
+
+ return error;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (debug_lacp_command, static) = {
+ .path = "debug lacp",
+ .short_help = "debug lacp <interface> <on | off>",
+ .function = debug_lacp_command_fn,
+};
+/* *INDENT-ON* */
+
+clib_error_t *
+lacp_cli_init (vlib_main_t * vm)
+{
+ lacp_main_t *lm = &lacp_main;
+
+ lm->vlib_main = vm;
+ lm->vnet_main = vnet_get_main ();
+
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (lacp_cli_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
64'>764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
/*
 * Copyright (c) 2016 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.
 */

/*
 * Copyright (c) 2005-2008 Jelmer Vernooij <jelmer@samba.org>
 * Copyright (C) 2006-2014 Stefan Metzmacher <metze@samba.org>
 * Copyright (C) 2013-2014 Andreas Schneider <asn@samba.org>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

/*
   Socket wrapper library. Passes all socket communication over
   unix domain sockets if the environment variable SOCKET_WRAPPER_DIR
   is set.
*/

#include <signal.h>
#include <dlfcn.h>

#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <pthread.h>

#include <vcl/ldp_socket_wrapper.h>


enum swrap_dbglvl_e
{
  SWRAP_LOG_ERROR = 0,
  SWRAP_LOG_WARN,
  SWRAP_LOG_DEBUG,
  SWRAP_LOG_TRACE
};


/* Macros for accessing mutexes */
#define SWRAP_LOCK(m) do { \
        pthread_mutex_lock(&(m ## _mutex)); \
} while(0)

#define SWRAP_UNLOCK(m) do { \
        pthread_mutex_unlock(&(m ## _mutex)); \
} while(0)

/* Add new global locks here please */
#define SWRAP_LOCK_ALL \
        SWRAP_LOCK(libc_symbol_binding); \

#define SWRAP_UNLOCK_ALL \
        SWRAP_UNLOCK(libc_symbol_binding); \



/* The mutex for accessing the global libc.symbols */
static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;

/* Function prototypes */

#ifdef NDEBUG
#define SWRAP_LOG(...)
#else
static unsigned int swrap_log_lvl = SWRAP_LOG_WARN;

static void
swrap_log (enum swrap_dbglvl_e dbglvl, const char *func,
	   const char *format, ...)
PRINTF_ATTRIBUTE (3, 4);
#define SWRAP_LOG(dbglvl, ...) swrap_log((dbglvl), __func__, __VA_ARGS__)

     static void
       swrap_log (enum swrap_dbglvl_e dbglvl,
		  const char *func, const char *format, ...)
{
  char buffer[1024];
  va_list va;

  va_start (va, format);
  vsnprintf (buffer, sizeof (buffer), format, va);
  va_end (va);

  if (dbglvl <= swrap_log_lvl)
    {
      switch (dbglvl)
	{
	case SWRAP_LOG_ERROR:
	  fprintf (stderr,
		   "SWRAP_ERROR(%d) - %s: %s\n",
		   (int) getpid (), func, buffer);
	  break;
	case SWRAP_LOG_WARN:
	  fprintf (stderr,
		   "SWRAP_WARN(%d) - %s: %s\n",
		   (int) getpid (), func, buffer);
	  break;
	case SWRAP_LOG_DEBUG:
	  fprintf (stderr,
		   "SWRAP_DEBUG(%d) - %s: %s\n",
		   (int) getpid (), func, buffer);
	  break;
	case SWRAP_LOG_TRACE:
	  fprintf (stderr,
		   "SWRAP_TRACE(%d) - %s: %s\n",
		   (int) getpid (), func, buffer);
	  break;
	}
    }
}
#endif


/*********************************************************
 * SWRAP LOADING LIBC FUNCTIONS
 *********************************************************/

typedef int (*__libc_accept4) (int sockfd,
			       struct sockaddr * addr,
			       socklen_t * addrlen, int flags);
typedef int (*__libc_accept) (int sockfd,
			      struct sockaddr * addr, socklen_t * addrlen);
typedef int (*__libc_bind) (int sockfd,
			    const struct sockaddr * addr, socklen_t addrlen);
typedef int (*__libc_close) (int fd);
typedef int (*__libc_connect) (int sockfd,
			       const struct sockaddr * addr,
			       socklen_t addrlen);

#if 0
/* TBD: dup and dup2 to be implemented later */
typedef int (*__libc_dup) (int fd);
typedef int (*__libc_dup2) (int oldfd, int newfd);
#endif

typedef int (*__libc_fcntl) (int fd, int cmd, ...);
typedef FILE *(*__libc_fopen) (const char *name, const char *mode);
#ifdef HAVE_FOPEN64
typedef FILE *(*__libc_fopen64) (const char *name, const char *mode);
#endif
#ifdef HAVE_EVENTFD
typedef int (*__libc_eventfd) (int count, int flags);
#endif
typedef int (*__libc_getpeername) (int sockfd,
				   struct sockaddr * addr,
				   socklen_t * addrlen);
typedef int (*__libc_getsockname) (int sockfd,
				   struct sockaddr * addr,
				   socklen_t * addrlen);
typedef int (*__libc_getsockopt) (int sockfd,
				  int level,
				  int optname,
				  void *optval, socklen_t * optlen);
typedef int (*__libc_ioctl) (int d, unsigned long int request, ...);
typedef int (*__libc_listen) (int sockfd, int backlog);
typedef int (*__libc_open) (const char *pathname, int flags, mode_t mode);
#ifdef HAVE_OPEN64
typedef int (*__libc_open64) (const char *pathname, int flags, mode_t mode);
#endif /* HAVE_OPEN64 */
typedef int (*__libc_openat) (int dirfd, const char *path, int flags, ...);
typedef int (*__libc_pipe) (int pipefd[2]);
typedef int (*__libc_read) (int fd, void *buf, size_t count);
typedef ssize_t (*__libc_readv) (int fd, const struct iovec * iov,
				 int iovcnt);
typedef int (*__libc_recv) (int sockfd, void *buf, size_t len, int flags);
typedef int (*__libc_recvfrom) (int sockfd,
				void *buf,
				size_t len,
				int flags,
				struct sockaddr * src_addr,
				socklen_t * addrlen);
typedef int (*__libc_recvmsg) (int sockfd, const struct msghdr * msg,
			       int flags);
typedef int (*__libc_send) (int sockfd, const void *buf, size_t len,
			    int flags);
typedef ssize_t (*__libc_sendfile) (int out_fd, int in_fd, off_t * offset,
				    size_t len);
typedef int (*__libc_sendmsg) (int sockfd, const struct msghdr * msg,
			       int flags);
typedef int (*__libc_sendto) (int sockfd, const void *buf, size_t len,
			      int flags, const struct sockaddr * dst_addr,
			      socklen_t addrlen);
typedef int (*__libc_setsockopt) (int sockfd, int level, int optname,
				  const void *optval, socklen_t optlen);
#ifdef HAVE_SIGNALFD
typedef int (*__libc_signalfd) (int fd, const sigset_t * mask, int flags);
#endif
typedef int (*__libc_socket) (int domain, int type, int protocol);
typedef int (*__libc_socketpair) (int domain, int type, int protocol,
				  int sv[2]);
#ifdef HAVE_TIMERFD_CREATE
typedef int (*__libc_timerfd_create) (int clockid, int flags);
#endif
typedef ssize_t (*__libc_write) (int fd, const void *buf, size_t count);
typedef ssize_t (*__libc_writev) (int fd, const struct iovec * iov,
				  int iovcnt);

typedef int (*__libc_shutdown) (int fd, int how);

typedef int (*__libc_select) (int __nfds, fd_set * __restrict __readfds,
			      fd_set * __restrict __writefds,
			      fd_set * __restrict __exceptfds,
			      struct timeval * __restrict __timeout);

#ifdef __USE_XOPEN2K
typedef int (*__libc_pselect) (int __nfds, fd_set * __restrict __readfds,
			       fd_set * __restrict __writefds,
			       fd_set * __restrict __exceptfds,
			       const struct timespec * __restrict __timeout,
			       const __sigset_t * __restrict __sigmask);
#endif

typedef int (*__libc_epoll_create) (int __size);

typedef int (*__libc_epoll_create1) (int __flags);

typedef int (*__libc_epoll_ctl) (int __epfd, int __op, int __fd,
				 struct epoll_event * __event);

typedef int (*__libc_epoll_wait) (int __epfd, struct epoll_event * __events,
				  int __maxevents, int __timeout);

typedef int (*__libc_epoll_pwait) (int __epfd, struct epoll_event * __events,
				   int __maxevents, int __timeout,
				   const __sigset_t * __ss);

typedef int (*__libc_poll) (struct pollfd * __fds, nfds_t __nfds,
			    int __timeout);

#ifdef __USE_GNU
typedef int (*__libc_ppoll) (struct pollfd * __fds, nfds_t __nfds,
			     const struct timespec * __timeout,
			     const __sigset_t * __ss);
#endif


#define SWRAP_SYMBOL_ENTRY(i) \
        union { \
                __libc_##i f; \
                void *obj; \
        } _libc_##i

struct swrap_libc_symbols
{
  SWRAP_SYMBOL_ENTRY (accept4);
  SWRAP_SYMBOL_ENTRY (accept);
  SWRAP_SYMBOL_ENTRY (bind);
  SWRAP_SYMBOL_ENTRY (close);
  SWRAP_SYMBOL_ENTRY (connect);
#if 0
  /* TBD: dup and dup2 to be implemented later */
  SWRAP_SYMBOL_ENTRY (dup);
  SWRAP_SYMBOL_ENTRY (dup2);
#endif
  SWRAP_SYMBOL_ENTRY (fcntl);
  SWRAP_SYMBOL_ENTRY (fopen);
#ifdef HAVE_FOPEN64
  SWRAP_SYMBOL_ENTRY (fopen64);
#endif
#ifdef HAVE_EVENTFD
  SWRAP_SYMBOL_ENTRY (eventfd);
#endif
  SWRAP_SYMBOL_ENTRY (getpeername);
  SWRAP_SYMBOL_ENTRY (getsockname);
  SWRAP_SYMBOL_ENTRY (getsockopt);
  SWRAP_SYMBOL_ENTRY (ioctl);
  SWRAP_SYMBOL_ENTRY (listen);
  SWRAP_SYMBOL_ENTRY (open);
#ifdef HAVE_OPEN64
  SWRAP_SYMBOL_ENTRY (open64);
#endif
  SWRAP_SYMBOL_ENTRY (openat);
  SWRAP_SYMBOL_ENTRY (pipe);
  SWRAP_SYMBOL_ENTRY (read);
  SWRAP_SYMBOL_ENTRY (readv);
  SWRAP_SYMBOL_ENTRY (recv);
  SWRAP_SYMBOL_ENTRY (recvfrom);
  SWRAP_SYMBOL_ENTRY (recvmsg);
  SWRAP_SYMBOL_ENTRY (send);
  SWRAP_SYMBOL_ENTRY (sendfile);
  SWRAP_SYMBOL_ENTRY (sendmsg);
  SWRAP_SYMBOL_ENTRY (sendto);
  SWRAP_SYMBOL_ENTRY (setsockopt);
#ifdef HAVE_SIGNALFD
  SWRAP_SYMBOL_ENTRY (signalfd);
#endif
  SWRAP_SYMBOL_ENTRY (socket);
  SWRAP_SYMBOL_ENTRY (socketpair);
#ifdef HAVE_TIMERFD_CREATE
  SWRAP_SYMBOL_ENTRY (timerfd_create);
#endif
  SWRAP_SYMBOL_ENTRY (write);
  SWRAP_SYMBOL_ENTRY (writev);

  SWRAP_SYMBOL_ENTRY (shutdown);
  SWRAP_SYMBOL_ENTRY (select);
#ifdef __USE_XOPEN2K
  SWRAP_SYMBOL_ENTRY (pselect);
#endif
  SWRAP_SYMBOL_ENTRY (epoll_create);
  SWRAP_SYMBOL_ENTRY (epoll_create1);
  SWRAP_SYMBOL_ENTRY (epoll_ctl);
  SWRAP_SYMBOL_ENTRY (epoll_wait);
  SWRAP_SYMBOL_ENTRY (epoll_pwait);
  SWRAP_SYMBOL_ENTRY (poll);
#ifdef __USE_GNU
  SWRAP_SYMBOL_ENTRY (ppoll);
#endif
};

struct swrap
{
  struct
  {
    void *handle;
    void *socket_handle;
    struct swrap_libc_symbols symbols;
  } libc;
};

static struct swrap swrap;

#define LIBC_NAME "libc.so"

enum swrap_lib
{
  SWRAP_LIBC,
};

#ifndef NDEBUG
static const char *
swrap_str_lib (enum swrap_lib lib)
{
  switch (lib)
    {
    case SWRAP_LIBC:
      return "libc";
    }

  /* Compiler would warn us about unhandled enum value if we get here */
  return "unknown";
}
#endif

static void *
swrap_load_lib_handle (enum swrap_lib lib)
{
  int flags = RTLD_LAZY;
  void *handle = NULL;
  int i;

#ifdef RTLD_DEEPBIND
  flags |= RTLD_DEEPBIND;
#endif

  switch (lib)
    {
    case SWRAP_LIBC:
      handle = swrap.libc.handle;
#ifdef LIBC_SO
      if (handle == NULL)
	{
	  handle = dlopen (LIBC_SO, flags);

	  swrap.libc.handle = handle;
	}
#endif
      if (handle == NULL)
	{
	  for (i = 10; i >= 0; i--)
	    {
	      char soname[256] = { 0 };

	      snprintf (soname, sizeof (soname), "libc.so.%d", i);
	      handle = dlopen (soname, flags);
	      if (handle != NULL)
		{
		  break;
		}
	    }

	  swrap.libc.handle = handle;
	}
      break;
    }

  if (handle == NULL)
    {
      SWRAP_LOG (SWRAP_LOG_ERROR,
		 "Failed to dlopen library: %s\n", dlerror ());
      exit (-1);
    }

  return handle;
}

static void *
_swrap_bind_symbol (enum swrap_lib lib, const char *fn_name)
{
  void *handle;
  void *func;

  handle = swrap_load_lib_handle (lib);

  func = dlsym (handle, fn_name);
  if (func == NULL)
    {
      SWRAP_LOG (SWRAP_LOG_ERROR,
		 "Failed to find %s: %s\n", fn_name, dlerror ());
      exit (-1);
    }

  SWRAP_LOG (SWRAP_LOG_TRACE,
	     "Loaded %s from %s", fn_name, swrap_str_lib (lib));

  return func;
}

#define swrap_bind_symbol_libc(sym_name) \
        SWRAP_LOCK(libc_symbol_binding); \
        if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
                swrap.libc.symbols._libc_##sym_name.obj = \
                        _swrap_bind_symbol(SWRAP_LIBC, #sym_name); \
        } \
        SWRAP_UNLOCK(libc_symbol_binding)

/*
 * IMPORTANT
 *
 * Functions especially from libc need to be loaded individually, you can't load
 * all at once or gdb will segfault at startup. The same applies to valgrind and
 * has probably something todo with with the linker.
 * So we need load each function at the point it is called the first time.
 */
int
libc_accept4 (int sockfd,
	      struct sockaddr *addr, socklen_t * addrlen, int flags)
{
  swrap_bind_symbol_libc (accept4);

  return swrap.libc.symbols._libc_accept4.f (sockfd, addr, addrlen, flags);
}

int
libc_accept (int sockfd, struct sockaddr *addr, socklen_t * addrlen)
{
  swrap_bind_symbol_libc (accept);

  return swrap.libc.symbols._libc_accept.f (sockfd, addr, addrlen);
}

int
libc_bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
  swrap_bind_symbol_libc (bind);

  return swrap.libc.symbols._libc_bind.f (sockfd, addr, addrlen);
}

int
libc_close (int fd)
{
  swrap_bind_symbol_libc (close);

  return swrap.libc.symbols._libc_close.f (fd);
}

int
libc_connect (int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
  swrap_bind_symbol_libc (connect);

  return swrap.libc.symbols._libc_connect.f (sockfd, addr, addrlen);
}

#if 0
/* TBD: dup and dup2 to be implemented later */
int
libc_dup (int fd)
{
  swrap_bind_symbol_libc (dup);

  return swrap.libc.symbols._libc_dup.f (fd);
}

int
libc_dup2 (int oldfd, int newfd)
{
  swrap_bind_symbol_libc (dup2);

  return swrap.libc.symbols._libc_dup2.f (oldfd, newfd);
}
#endif

#ifdef HAVE_EVENTFD
int
libc_eventfd (int count, int flags)
{
  swrap_bind_symbol_libc (eventfd);

  return swrap.libc.symbols._libc_eventfd.f (count, flags);
}
#endif

DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE int
libc_vfcntl (int fd, int cmd, va_list ap)
{
  long int args[4];
  int rc;
  int i;

  swrap_bind_symbol_libc (fcntl);

  for (i = 0; i < 4; i++)
    {
      args[i] = va_arg (ap, long int);
    }

  rc = swrap.libc.symbols._libc_fcntl.f (fd,
					 cmd,
					 args[0], args[1], args[2], args[3]);

  return rc;
}

DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE int
libc_vioctl (int fd, int cmd, va_list ap)
{
  long int args[4];
  int rc;
  int i;

  swrap_bind_symbol_libc (ioctl);

  for (i = 0; i < 4; i++)
    {
      args[i] = va_arg (ap, long int);
    }

  rc = swrap.libc.symbols._libc_ioctl.f (fd,
					 cmd,
					 args[0], args[1], args[2], args[3]);

  return rc;
}

int
libc_getpeername (int sockfd, struct sockaddr *addr, socklen_t * addrlen)
{
  swrap_bind_symbol_libc (getpeername);

  return swrap.libc.symbols._libc_getpeername.f (sockfd, addr, addrlen);
}

int
libc_getsockname (int sockfd, struct sockaddr *addr, socklen_t * addrlen)
{
  swrap_bind_symbol_libc (getsockname);

  return swrap.libc.symbols._libc_getsockname.f (sockfd, addr, addrlen);
}

int
libc_getsockopt (int sockfd,
		 int level, int optname, void *optval, socklen_t * optlen)
{
  swrap_bind_symbol_libc (getsockopt);

  return swrap.libc.symbols._libc_getsockopt.f (sockfd,
						level,
						optname, optval, optlen);
}

int
libc_listen (int sockfd, int backlog)
{
  swrap_bind_symbol_libc (listen);

  return swrap.libc.symbols._libc_listen.f (sockfd, backlog);
}

/* TBD: libc_read() should return ssize_t not an int */
int
libc_read (int fd, void *buf, size_t count)
{
  swrap_bind_symbol_libc (read);

  return swrap.libc.symbols._libc_read.f (fd, buf, count);
}

ssize_t
libc_readv (int fd, const struct iovec * iov, int iovcnt)
{
  swrap_bind_symbol_libc (readv);

  return swrap.libc.symbols._libc_readv.f (fd, iov, iovcnt);
}

int
libc_recv (int sockfd, void *buf, size_t len, int flags)
{
  swrap_bind_symbol_libc (recv);

  return swrap.libc.symbols._libc_recv.f (sockfd, buf, len, flags);
}

int
libc_recvfrom (int sockfd,
	       void *buf,
	       size_t len,
	       int flags, struct sockaddr *src_addr, socklen_t * addrlen)
{
  swrap_bind_symbol_libc (recvfrom);

  return swrap.libc.symbols._libc_recvfrom.f (sockfd,
					      buf,
					      len, flags, src_addr, addrlen);
}

int
libc_recvmsg (int sockfd, struct msghdr *msg, int flags)
{
  swrap_bind_symbol_libc (recvmsg);

  return swrap.libc.symbols._libc_recvmsg.f (sockfd, msg, flags);
}

int
libc_send (int sockfd, const void *buf, size_t len, int flags)
{
  swrap_bind_symbol_libc (send);

  return swrap.libc.symbols._libc_send.f (sockfd, buf, len, flags);
}

ssize_t
libc_sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
{
  swrap_bind_symbol_libc (sendfile);

  return swrap.libc.symbols._libc_sendfile.f (out_fd, in_fd, offset, len);
}

int
libc_sendmsg (int sockfd, const struct msghdr *msg, int flags)
{
  swrap_bind_symbol_libc (sendmsg);

  return swrap.libc.symbols._libc_sendmsg.f (sockfd, msg, flags);
}

int
libc_sendto (int sockfd,
	     const void *buf,
	     size_t len,
	     int flags, const struct sockaddr *dst_addr, socklen_t addrlen)
{
  swrap_bind_symbol_libc (sendto);

  return swrap.libc.symbols._libc_sendto.f (sockfd,
					    buf,
					    len, flags, dst_addr, addrlen);
}

int
libc_setsockopt (int sockfd,
		 int level, int optname, const void *optval, socklen_t optlen)
{
  swrap_bind_symbol_libc (setsockopt);

  return swrap.libc.symbols._libc_setsockopt.f (sockfd,
						level,
						optname, optval, optlen);
}

int
libc_socket (int domain, int type, int protocol)
{
  swrap_bind_symbol_libc (socket);

  return swrap.libc.symbols._libc_socket.f (domain, type, protocol);
}

int
libc_socketpair (int domain, int type, int protocol, int sv[2])
{
  swrap_bind_symbol_libc (socketpair);

  return swrap.libc.symbols._libc_socketpair.f (domain, type, protocol, sv);
}

ssize_t
libc_write (int fd, const void *buf, size_t count)
{
  swrap_bind_symbol_libc (write);

  return swrap.libc.symbols._libc_write.f (fd, buf, count);
}

ssize_t
libc_writev (int fd, const struct iovec * iov, int iovcnt)
{
  swrap_bind_symbol_libc (writev);

  return swrap.libc.symbols._libc_writev.f (fd, iov, iovcnt);
}

int
libc_shutdown (int fd, int how)
{
  swrap_bind_symbol_libc (shutdown);

  return swrap.libc.symbols._libc_shutdown.f (fd, how);
}

int
libc_select (int __nfds, fd_set * __restrict __readfds,
	     fd_set * __restrict __writefds,
	     fd_set * __restrict __exceptfds,
	     struct timeval *__restrict __timeout)
{
  swrap_bind_symbol_libc (select);

  return swrap.libc.symbols._libc_select.f (__nfds, __readfds,
					    __writefds,
					    __exceptfds, __timeout);
}

#ifdef __USE_XOPEN2K
int
libc_pselect (int __nfds, fd_set * __restrict __readfds,
	      fd_set * __restrict __writefds,
	      fd_set * __restrict __exceptfds,
	      const struct timespec *__restrict __timeout,
	      const __sigset_t * __restrict __sigmask)
{
  swrap_bind_symbol_libc (pselect);

  return swrap.libc.symbols._libc_pselect.f (__nfds, __readfds,
					     __writefds,
					     __exceptfds,
					     __timeout, __sigmask);
}
#endif

int
libc_epoll_create (int __size)
{
  swrap_bind_symbol_libc (epoll_create);

  return swrap.libc.symbols._libc_epoll_create.f (__size);
}

int
libc_epoll_create1 (int __flags)
{
  swrap_bind_symbol_libc (epoll_create1);

  return swrap.libc.symbols._libc_epoll_create1.f (__flags);
}

int
libc_epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event)
{
  swrap_bind_symbol_libc (epoll_ctl);

  return swrap.libc.symbols._libc_epoll_ctl.f (__epfd, __op, __fd, __event);
}

int
libc_epoll_wait (int __epfd, struct epoll_event *__events,
		 int __maxevents, int __timeout)
{
  swrap_bind_symbol_libc (epoll_wait);

  return swrap.libc.symbols._libc_epoll_wait.f (__epfd, __events,
						__maxevents, __timeout);
}

int
libc_epoll_pwait (int __epfd, struct epoll_event *__events,
		  int __maxevents, int __timeout, const __sigset_t * __ss)
{
  swrap_bind_symbol_libc (epoll_pwait);

  return swrap.libc.symbols._libc_epoll_pwait.f (__epfd, __events,
						 __maxevents, __timeout,
						 __ss);
}

int
libc_poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
{
  swrap_bind_symbol_libc (poll);

  return swrap.libc.symbols._libc_poll.f (__fds, __nfds, __timeout);
}

#ifdef __USE_GNU
int
libc_ppoll (struct pollfd *__fds, nfds_t __nfds,
	    const struct timespec *__timeout, const __sigset_t * __ss)
{
  swrap_bind_symbol_libc (ppoll);

  return swrap.libc.symbols._libc_ppoll.f (__fds, __nfds, __timeout, __ss);
}
#endif

static void
swrap_thread_prepare (void)
{
  SWRAP_LOCK_ALL;
}

static void
swrap_thread_parent (void)
{
  SWRAP_UNLOCK_ALL;
}

static void
swrap_thread_child (void)
{
  SWRAP_UNLOCK_ALL;
}

/****************************
 * CONSTRUCTOR
 ***************************/
void
swrap_constructor (void)
{
  /*
   * If we hold a lock and the application forks, then the child
   * is not able to unlock the mutex and we are in a deadlock.
   * This should prevent such deadlocks.
   */
  pthread_atfork (&swrap_thread_prepare,
		  &swrap_thread_parent, &swrap_thread_child);
}

/****************************
 * DESTRUCTOR
 ***************************/

/*
 * This function is called when the library is unloaded and makes sure that
 * sockets get closed and the unix file for the socket are unlinked.
 */
void
swrap_destructor (void)
{
  if (swrap.libc.handle != NULL)
    {
      dlclose (swrap.libc.handle);
    }
  if (swrap.libc.socket_handle)
    {
      dlclose (swrap.libc.socket_handle);
    }
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */