summaryrefslogtreecommitdiffstats
path: root/src/vcl
diff options
context:
space:
mode:
authorDave Wallace <dwallacelf@gmail.com>2017-10-24 20:47:45 -0400
committerDave Wallace <dwallacelf@gmail.com>2017-10-24 20:47:45 -0400
commit60f54822b78dea6b87cde5da82a4a135e7c2b960 (patch)
tree66e5cba28eb2f670bf88ebe9ce89e83a71922782 /src/vcl
parent5c7cf1cc5358d137160be1619981e7eea9a7402f (diff)
VCL-LDPRELOAD: Fix coverity warnings.
- CID 178225: Constant expression result - CID 178220: Logically dead code - CID 178222: Logically dead code - CID 178227: Logically dead code - CID 178223: Same on both sides - CID 178226: Same on both sides Change-Id: Ie0c80edb41390cf2308e54938be85d865e292138 Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
Diffstat (limited to 'src/vcl')
-rw-r--r--src/vcl/vcom.c3
-rw-r--r--src/vcl/vcom_socket.c16
-rw-r--r--src/vcl/vcom_socket_wrapper.c5
3 files changed, 12 insertions, 12 deletions
diff --git a/src/vcl/vcom.c b/src/vcl/vcom.c
index 6d98fdb75c0..9bd5e0e54a2 100644
--- a/src/vcl/vcom.c
+++ b/src/vcl/vcom.c
@@ -1282,8 +1282,7 @@ vcom_select (int __nfds, fd_set * __restrict __readfds,
* */
vcom_fd_set_merge (
/* dest, select sets */
- __readfds || __writefds || __exceptfds ?
- &new_nfds : NULL,
+ &new_nfds,
__readfds,
__writefds,
__exceptfds,
diff --git a/src/vcl/vcom_socket.c b/src/vcl/vcom_socket.c
index 8806ebd011a..1ccb05451ad 100644
--- a/src/vcl/vcom_socket.c
+++ b/src/vcl/vcom_socket.c
@@ -465,6 +465,7 @@ vcom_socket_read (int __fd, void *__buf, size_t __nbytes)
{
rv = vppcom_session_read (vsock->sid, __buf, __nbytes);
}
+ /* coverity[CONSTANT_EXPRESSION_RESULT] */
while (rv == -EAGAIN || rv == -EWOULDBLOCK);
return rv;
}
@@ -535,6 +536,7 @@ vcom_socket_readv (int __fd, const struct iovec * __iov, int __iovcnt)
}
}
}
+ /* coverity[CONSTANT_EXPRESSION_RESULT] */
while ((rv == -EAGAIN || rv == -EWOULDBLOCK) && total == 0);
return total;
}
@@ -1561,7 +1563,8 @@ vcom_socket_sendto (int __fd, const void *__buf, size_t __n,
return -EDESTADDRREQ;
}
/* not a vppcom supported address family */
- if ((__addr->sa_family != AF_INET) || (__addr->sa_family != AF_INET6))
+ if (!((__addr->sa_family == AF_INET) ||
+ (__addr->sa_family == AF_INET6)))
{
return -EINVAL;
}
@@ -3151,13 +3154,9 @@ vcom_socket_poll_vppcom_impl (struct pollfd *__fds, nfds_t __nfds,
{
time_to_wait = (double) 0;
}
- else if (__timeout < 0)
- {
- time_to_wait = ~0;
- }
else
{
- return -EBADF;
+ time_to_wait = ~0;
}
return vppcom_poll (__fds, __nfds, time_to_wait);
@@ -3332,9 +3331,10 @@ vcom_socket_main_destroy (void)
/* *INDENT-OFF* */
pool_flush (vepitem, vsm->vepitems,
({
- if (vepitem->type == FD_TYPE_EPOLL || FD_TYPE_VCOM_SOCKET)
+ if ((vepitem->type == FD_TYPE_EPOLL) ||
+ (vepitem->type == FD_TYPE_VCOM_SOCKET))
{
- vcom_socket_epoll_ctl1 (vepitem->epfd, EPOLL_CTL_DEL,
+ vcom_socket_epoll_ctl1 (vepitem->epfd, EPOLL_CTL_DEL,
vepitem->fd, NULL);
vepitem_init (vepitem);
}
diff --git a/src/vcl/vcom_socket_wrapper.c b/src/vcl/vcom_socket_wrapper.c
index 19a775de99e..8d546929218 100644
--- a/src/vcl/vcom_socket_wrapper.c
+++ b/src/vcl/vcom_socket_wrapper.c
@@ -101,6 +101,8 @@ static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
#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, ...)
@@ -113,13 +115,12 @@ PRINTF_ATTRIBUTE (3, 4);
{
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)
+ if (dbglvl <= swrap_log_lvl)
{
switch (dbglvl)
{
ing */ .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 */ }
/*
 * snat_det.h - deterministic NAT definitions
 *
 * 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.
 */
/**
 * @file
 * @brief deterministic NAT definitions
 */

#ifndef __included_nat_det_h__
#define __included_nat_det_h__

#include <vnet/ip/ip.h>
#include <nat/nat.h>
#include <nat/nat_ipfix_logging.h>


#define SNAT_DET_SES_PER_USER 1000


int snat_det_add_map (snat_main_t * sm, ip4_address_t * in_addr, u8 in_plen,
		      ip4_address_t * out_addr, u8 out_plen, int is_add);

always_inline int
is_addr_in_net (ip4_address_t * addr, ip4_address_t * net, u8 plen)
{
  if (net->as_u32 == (addr->as_u32 & ip4_main.fib_masks[plen]))
    return 1;
  return 0;
}

always_inline snat_det_map_t *
snat_det_map_by_user (snat_main_t * sm, ip4_address_t * user_addr)
{
  snat_det_map_t *dm;

  /* *INDENT-OFF* */
  pool_foreach (dm, sm->det_maps,
  ({
    if (is_addr_in_net(user_addr, &dm->in_addr, dm->in_plen))
      return dm;
  }));
  /* *INDENT-ON* */
  return 0;
}

always_inline snat_det_map_t *
snat_det_map_by_out (snat_main_t * sm, ip4_address_t * out_addr)
{
  snat_det_map_t *dm;

  /* *INDENT-OFF* */
  pool_foreach (dm, sm->det_maps,
  ({
    if (is_addr_in_net(out_addr, &dm->out_addr, dm->out_plen))
      return dm;
  }));
  /* *INDENT-ON* */
  return 0;
}

always_inline void
snat_det_forward (snat_det_map_t * dm, ip4_address_t * in_addr,
		  ip4_address_t * out_addr, u16 * lo_port)
{
  u32 in_offset, out_offset;

  in_offset = clib_net_to_host_u32 (in_addr->as_u32) -
    clib_net_to_host_u32 (dm->in_addr.as_u32);
  out_offset = in_offset / dm->sharing_ratio;
  out_addr->as_u32 =
    clib_host_to_net_u32 (clib_net_to_host_u32 (dm->out_addr.as_u32) +
			  out_offset);
  *lo_port = 1024 + dm->ports_per_host * (in_offset % dm->sharing_ratio);
}

always_inline void
snat_det_reverse (snat_det_map_t * dm, ip4_address_t * out_addr, u16 out_port,
		  ip4_address_t * in_addr)
{
  u32 in_offset1, in_offset2, out_offset;

  out_offset = clib_net_to_host_u32 (out_addr->as_u32) -
    clib_net_to_host_u32 (dm->out_addr.as_u32);
  in_offset1 = out_offset * dm->sharing_ratio;
  in_offset2 = (out_port - 1024) / dm->ports_per_host;
  in_addr->as_u32 =
    clib_host_to_net_u32 (clib_net_to_host_u32 (dm->in_addr.as_u32) +
			  in_offset1 + in_offset2);
}

always_inline u32
snat_det_user_ses_offset (ip4_address_t * addr, u8 plen)
{
  return (clib_net_to_host_u32 (addr->as_u32) & pow2_mask (32 - plen)) *
    SNAT_DET_SES_PER_USER;
}

always_inline snat_det_session_t *
snat_det_get_ses_by_out (snat_det_map_t * dm, ip4_address_t * in_addr,
			 u64 out_key)
{
  u32 user_offset;
  u16 i;

  user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
  for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
    {
      if (dm->sessions[i + user_offset].out.as_u64 == out_key)
	return &dm->sessions[i + user_offset];
    }

  return 0;
}

always_inline snat_det_session_t *
snat_det_find_ses_by_in (snat_det_map_t * dm, ip4_address_t * in_addr,
			 u16 in_port, snat_det_out_key_t out_key)
{
  snat_det_session_t *ses;
  u32 user_offset;
  u16 i;

  user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
  for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
    {
      ses = &dm->sessions[i + user_offset];
      if (ses->in_port == in_port &&
	  ses->out.ext_host_addr.as_u32 == out_key.ext_host_addr.as_u32 &&
	  ses->out.ext_host_port == out_key.ext_host_port)
	return &dm->sessions[i + user_offset];
    }

  return 0;
}

always_inline snat_det_session_t *
snat_det_ses_create (snat_det_map_t * dm, ip4_address_t * in_addr,
		     u16 in_port, snat_det_out_key_t * out)
{
  u32 user_offset;
  u16 i;

  user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);

  for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
    {
      if (!dm->sessions[i + user_offset].in_port)
	{
	  if (__sync_bool_compare_and_swap
	      (&dm->sessions[i + user_offset].in_port, 0, in_port))
	    {
	      dm->sessions[i + user_offset].out.as_u64 = out->as_u64;
	      dm->sessions[i + user_offset].state = SNAT_SESSION_UNKNOWN;
	      dm->sessions[i + user_offset].expire = 0;
	      __sync_add_and_fetch (&dm->ses_num, 1);
	      return &dm->sessions[i + user_offset];
	    }
	}
    }

  snat_ipfix_logging_max_entries_per_user (SNAT_DET_SES_PER_USER,
					   in_addr->as_u32);
  return 0;
}

always_inline void
snat_det_ses_close (snat_det_map_t * dm, snat_det_session_t * ses)
{
  if (__sync_bool_compare_and_swap (&ses->in_port, ses->in_port, 0))
    {
      ses->out.as_u64 = 0;
      __sync_add_and_fetch (&dm->ses_num, -1);
    }
}

#endif /* __included_nat_det_h__ */

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