aboutsummaryrefslogtreecommitdiffstats
path: root/extras/vcl-ldpreload/src/libvcl-ldpreload/vcom_socket_wrapper.c
blob: 087cd6bbc52045104bb3ec661561dafb53b49119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
/*
 * Copyright (c) 2024 Marvell.
 * SPDX-License-Identifier: Apache-2.0
 * https://spdx.org/licenses/Apache-2.0.html
 */

#include <vnet/vnet.h>
#include <vnet/dev/dev.h>
#include <vnet/dev/pci.h>
#include <vnet/dev/counters.h>
#include <dev_octeon/octeon.h>
#include <dev_octeon/common.h>

VLIB_REGISTER_LOG_CLASS (oct_log, static) = {
  .class_name = "oct",
  .subclass_name = "counters",
};

typedef enum
{
  OCT_PORT_CTR_RX_BYTES,
  OCT_PORT_CTR_TX_BYTES,
  OCT_PORT_CTR_RX_PACKETS,
  OCT_PORT_CTR_TX_PACKETS,
  OCT_PORT_CTR_RX_DROPS,
  OCT_PORT_CTR_TX_DROPS,
  OCT_PORT_CTR_RX_DROP_BYTES,
  OCT_PORT_CTR_RX_UCAST,
  OCT_PORT_CTR_TX_UCAST,
  OCT_PORT_CTR_RX_MCAST,
  OCT_PORT_CTR_TX_MCAST,
  OCT_PORT_CTR_RX_BCAST,
  OCT_PORT_CTR_TX_BCAST,
  OCT_PORT_CTR_RX_FCS,
  OCT_PORT_CTR_RX_ERR,
  OCT_PORT_CTR_RX_DROP_MCAST,
  OCT_PORT_CTR_RX_DROP_BCAST,
  OCT_PORT_CTR_RX_DROP_L3_MCAST,
  OCT_PORT_CTR_RX_DROP_L3_BCAST,
} oct_port_counter_id_t;

vnet_dev_counter_t oct_port_counters[] = {
  VNET_DEV_CTR_RX_BYTES (OCT_PORT_CTR_RX_BYTES),
  VNET_DEV_CTR_RX_PACKETS (OCT_PORT_CTR_RX_PACKETS),
  VNET_DEV_CTR_RX_DROPS (OCT_PORT_CTR_RX_DROPS),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_DROP_BYTES, RX, BYTES, "drop bytes"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_UCAST, RX, PACKETS, "unicast"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_MCAST, RX, PACKETS, "multicast"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_BCAST, RX, PACKETS, "broadcast"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_FCS, RX, PACKETS, "fcs"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_ERR, RX, PACKETS, "error"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_DROP_MCAST, RX, PACKETS,
		       "drop multicast"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_DROP_BCAST, RX, PACKETS,
		       "drop broadcast"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_DROP_L3_MCAST, RX, PACKETS,
		       "drop L3 multicast"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_RX_DROP_L3_BCAST, RX, PACKETS,
		       "drop L3 broadcast"),

  VNET_DEV_CTR_TX_BYTES (OCT_PORT_CTR_TX_BYTES),
  VNET_DEV_CTR_TX_PACKETS (OCT_PORT_CTR_TX_PACKETS),
  VNET_DEV_CTR_TX_DROPS (OCT_PORT_CTR_TX_DROPS),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_TX_UCAST, TX, PACKETS, "unicast"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_TX_MCAST, TX, PACKETS, "multicast"),
  VNET_DEV_CTR_VENDOR (OCT_PORT_CTR_TX_BCAST, TX, PACKETS, "broadcast"),
};

typedef enum
{
  OCT_RXQ_CTR_BYTES,
  OCT_RXQ_CTR_PKTS,
  OCT_RXQ_CTR_DROPS,
  OCT_RXQ_CTR_DROP_BYTES,
  OCT_RXQ_CTR_ERR,
} oct_rxq_counter_id_t;

vnet_dev_counter_t oct_rxq_counters[] = {
  VNET_DEV_CTR_RX_BYTES (OCT_RXQ_CTR_BYTES),
  VNET_DEV_CTR_RX_PACKETS (OCT_RXQ_CTR_PKTS),
  VNET_DEV_CTR_RX_DROPS (OCT_RXQ_CTR_DROPS),
  VNET_DEV_CTR_VENDOR (OCT_RXQ_CTR_DROP_BYTES, RX, BYTES, "drop bytes"),
  VNET_DEV_CTR_VENDOR (OCT_RXQ_CTR_ERR, RX, PACKETS, "error"),
};

typedef enum
{
  OCT_TXQ_CTR_BYTES,
  OCT_TXQ_CTR_PKTS,
  OCT_TXQ_CTR_DROPS,
  OCT_TXQ_CTR_DROP_BYTES,
} oct_txq_counter_id_t;

vnet_dev_counter_t oct_txq_counters[] = {
  VNET_DEV_CTR_TX_BYTES (OCT_TXQ_CTR_BYTES),
  VNET_DEV_CTR_TX_PACKETS (OCT_TXQ_CTR_PKTS),
  VNET_DEV_CTR_TX_DROPS (OCT_TXQ_CTR_DROPS),
  VNET_DEV_CTR_VENDOR (OCT_TXQ_CTR_DROP_BYTES, TX, BYTES, "drop bytes"),
};

static vnet_dev_rv_t
oct_roc_err (vnet_dev_t *dev, int rv, char *fmt, ...)
{
  u8 *s = 0;
  va_list va;

  va_start (va, fmt);
  s = va_format (s, fmt, &va);
  va_end (va);

  log_err (dev, "%v - ROC error %s (%d)", s, roc_error_msg_get (rv), rv);

  vec_free (s);
  return VNET_DEV_ERR_INTERNAL;
}

void
oct_port_add_counters (vlib_main_t *vm, vnet_dev_port_t *port)
{
  vnet_dev_port_add_counters (vm, port, oct_port_counters,
			      ARRAY_LEN (oct_port_counters));

  foreach_vnet_dev_port_rx_queue (rxq, port)
    {
      vnet_dev_rx_queue_add_counters (vm, rxq, oct_rxq_counters,
				      ARRAY_LEN (oct_rxq_counters));
    }

  foreach_vnet_dev_port_tx_queue (txq, port)
    {
      vnet_dev_tx_queue_add_counters (vm, txq, oct_txq_counters,
				      ARRAY_LEN (oct_txq_counters));
    }
}

vnet_dev_rv_t
oct_port_get_stats (vlib_main_t *vm, vnet_dev_port_t *port)
{
  vnet_dev_t *dev = port->dev;
  oct_device_t *cd = vnet_dev_get_data (dev);
  struct roc_nix *nix = cd->nix;
  int rrv;
  struct roc_nix_stats stats;

  if ((rrv = roc_nix_stats_get (nix, &stats)))
    return oct_roc_err (dev, rrv, "roc_nix_stats_get() failed");

  foreach_vnet_dev_counter (c, port->counter_main)
    {
      switch (c->user_data)
	{
	case OCT_PORT_CTR_RX_BYTES:
	  vnet_dev_counter_value_update (vm, c, stats.rx_octs);
	  break;
	case OCT_PORT_CTR_TX_BYTES:
	  vnet_dev_counter_value_update (vm, c, stats.tx_octs);
	  break;
	case OCT_PORT_CTR_RX_PACKETS:
	  vnet_dev_counter_value_update (
	    vm, c, stats.rx_ucast + stats.rx_bcast + stats.rx_mcast);
	  break;
	case OCT_PORT_CTR_TX_PACKETS:
	  vnet_dev_counter_value_update (
	    vm, c, stats.tx_ucast + stats.tx_bcast + stats.tx_mcast);
	  break;
	case OCT_PORT_CTR_RX_DROPS:
	  vnet_dev_counter_value_update (
/*
 * 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 <libvcl-ldpreload/vcom_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 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;
  unsigned int lvl = SWRAP_LOG_WARN;

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

  if (lvl >= dbglvl)
    {
      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
 *********************************************************/

#ifdef HAVE_ACCEPT4
typedef int (*__libc_accept4) (int sockfd,
			       struct sockaddr * addr,
			       socklen_t * addrlen, int flags);
#else
typedef int (*__libc_accept) (int sockfd,
			      struct sockaddr * addr, socklen_t * addrlen);
#endif
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 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);


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

struct swrap_libc_symbols
{
#ifdef HAVE_ACCEPT4
  SWRAP_SYMBOL_ENTRY (accept4);
#else
  SWRAP_SYMBOL_ENTRY (accept);
#endif
  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 (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);
};

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.
 */
#ifdef HAVE_ACCEPT4
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);
}

#else /* HAVE_ACCEPT4 */

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);
}
#endif /* HAVE_ACCEPT4 */

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);
}

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);
}

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);
}

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:
 */