aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/feature/feature.api
diff options
context:
space:
mode:
authorFlorin Coras <fcoras@cisco.com>2021-06-22 20:04:46 -0700
committerDave Barach <openvpp@barachs.net>2021-06-29 15:22:05 +0000
commitfa3884f8a1cf5493dd7733404811b63ee9c1e4d9 (patch)
tree71dc67e6a46fcc05921132901227bc230b06f1df /src/vnet/feature/feature.api
parentfbab65bc1c186a3029804e32e678c0fe7f823f8e (diff)
vcl: improvements to epoll lt
Type: improvement Signed-off-by: Florin Coras <fcoras@cisco.com> Change-Id: If585b9c48f67599c56a8c4b4a5139375bc4287f6
Diffstat (limited to 'src/vnet/feature/feature.api')
0 files changed, 0 insertions, 0 deletions
>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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
/*
 * Copyright (c) 2020 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/fib/fib_table.h>
#include <vnet/dpo/drop_dpo.h>

#include <cnat/cnat_client.h>
#include <cnat/cnat_translation.h>

cnat_client_t *cnat_client_pool;

cnat_client_db_t cnat_client_db;

dpo_type_t cnat_client_dpo;

static_always_inline u8
cnat_client_is_clone (cnat_client_t * cc)
{
  return (FIB_NODE_INDEX_INVALID == cc->cc_fei);
}

static void
cnat_client_db_remove (cnat_client_t * cc)
{
  if (ip_addr_version (&cc->cc_ip) == AF_IP4)
    hash_unset (cnat_client_db.crd_cip4, ip_addr_v4 (&cc->cc_ip).as_u32);
  else
    hash_unset_mem_free (&cnat_client_db.crd_cip6, &ip_addr_v6 (&cc->cc_ip));
}

static void
cnat_client_destroy (cnat_client_t * cc)
{
  ASSERT (!cnat_client_is_clone (cc));
  if (!(cc->flags & CNAT_FLAG_EXCLUSIVE))
    {
      ASSERT (fib_entry_is_sourced (cc->cc_fei, cnat_fib_source));
      fib_table_entry_delete_index (cc->cc_fei, cnat_fib_source);
    }
  cnat_client_db_remove (cc);
  dpo_reset (&cc->cc_parent);
  pool_put (cnat_client_pool, cc);
}

void
cnat_client_free_by_ip (ip46_address_t * ip, u8 af)
{
  cnat_client_t *cc;
  cc = (AF_IP4 == af ?
	cnat_client_ip4_find (&ip->ip4) : cnat_client_ip6_find (&ip->ip6));
  ASSERT (NULL != cc);

  if ((0 == cnat_client_uncnt_session (cc))
      && (cc->flags & CNAT_FLAG_EXPIRES) && (0 == cc->tr_refcnt))
    cnat_client_destroy (cc);
}

void
cnat_client_throttle_pool_process ()
{
  /* This processes ips stored in the throttle pool
     to update session refcounts
     and should be called before cnat_client_free_by_ip */
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  cnat_client_t *cc;
  int nthreads;
  u32 *del_vec = NULL, *ai;
  ip_address_t *addr;
  nthreads = tm->n_threads + 1;
  for (int i = 0; i < nthreads; i++)
    {
      vec_reset_length (del_vec);
      clib_spinlock_lock (&cnat_client_db.throttle_pool_lock[i]);
      /* *INDENT-OFF* */
      pool_foreach(addr, cnat_client_db.throttle_pool[i], ({
	cc = (AF_IP4 == addr->version ?
	      cnat_client_ip4_find (&ip_addr_v4(addr)) :
	      cnat_client_ip6_find (&ip_addr_v6(addr)));
	/* Client might not already be created */
	if (NULL != cc)
	  {
	    cnat_client_cnt_session (cc);
	    vec_add1(del_vec, addr - cnat_client_db.throttle_pool[i]);
	  }
      }));
      /* *INDENT-ON* */
      vec_foreach (ai, del_vec)
      {
	addr = pool_elt_at_index (cnat_client_db.throttle_pool[i], *ai);
	pool_put (cnat_client_db.throttle_pool[i], addr);
      }
      clib_spinlock_unlock (&cnat_client_db.throttle_pool_lock[i]);
    }
}

void
cnat_client_translation_added (index_t cci)
{
  cnat_client_t *cc;
  if (INDEX_INVALID == cci)
    return;

  cc = cnat_client_get (cci);
  ASSERT (!(cc->flags & CNAT_FLAG_EXPIRES));
  cc->tr_refcnt++;
}

void
cnat_client_translation_deleted (index_t cci)
{
  cnat_client_t *cc;
  if (INDEX_INVALID == cci)
    return;

  cc = cnat_client_get (cci);
  ASSERT (!(cc->flags & CNAT_FLAG_EXPIRES));
  cc->tr_refcnt--;

  if (0 == cc->tr_refcnt && 0 == cc->session_refcnt)
    cnat_client_destroy (cc);
}

static void
cnat_client_db_add (cnat_client_t * cc)
{
  index_t cci;

  cci = cc - cnat_client_pool;

  if (ip_addr_version (&cc->cc_ip) == AF_IP4)
    hash_set (cnat_client_db.crd_cip4, ip_addr_v4 (&cc->cc_ip).as_u32, cci);
  else
    hash_set_mem_alloc (&cnat_client_db.crd_cip6,
			&ip_addr_v6 (&cc->cc_ip), cci);
}


index_t
cnat_client_add (const ip_address_t * ip, u8 flags)
{
  cnat_client_t *cc;
  dpo_id_t tmp = DPO_INVALID;
  fib_node_index_t fei;
  dpo_proto_t dproto;
  fib_prefix_t pfx;
  index_t cci;
  u32 fib_flags;

  /* check again if we need this client */
  cc = (AF_IP4 == ip->version ?
	cnat_client_ip4_find (&ip->ip.ip4) :
	cnat_client_ip6_find (&ip->ip.ip6));

  if (NULL != cc)
    return (cc - cnat_client_pool);


  pool_get_aligned (cnat_client_pool, cc, CLIB_CACHE_LINE_BYTES);
  cc->cc_locks = 1;
  cci = cc - cnat_client_pool;
  cc->parent_cci = cci;
  cc->flags = flags;
  cc->tr_refcnt = 0;
  cc->session_refcnt = 0;

  ip_address_copy (&cc->cc_ip, ip);
  cnat_client_db_add (cc);

  ip_address_to_fib_prefix (&cc->cc_ip, &pfx);

  dproto = fib_proto_to_dpo (pfx.fp_proto);
  dpo_set (&tmp, cnat_client_dpo, dproto, cci);
  dpo_stack (cnat_client_dpo, dproto, &cc->cc_parent, drop_dpo_get (dproto));

  fib_flags = FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT;
  fib_flags |= (flags & CNAT_FLAG_EXCLUSIVE) ?
    FIB_ENTRY_FLAG_EXCLUSIVE : FIB_ENTRY_FLAG_INTERPOSE;

  fei = fib_table_entry_special_dpo_add (CNAT_FIB_TABLE,
					 &pfx, cnat_fib_source, fib_flags,
					 &tmp);

  cc = pool_elt_at_index (cnat_client_pool, cci);
  cc->cc_fei = fei;

  return (cci);
}

void
cnat_client_learn (const cnat_learn_arg_t * l)
{
  /* RPC call to add a client from the dataplane */
  index_t cci;
  cnat_client_t *cc;
  cci = cnat_client_add (&l->addr, CNAT_FLAG_EXPIRES);
  cc = pool_elt_at_index (cnat_client_pool, cci);
  cnat_client_cnt_session (cc);
  /* Process throttled calls if any */
  cnat_client_throttle_pool_process ();
}

/**
 * Interpose a policy DPO
 */
static void
cnat_client_dpo_interpose (const dpo_id_t * original,
			   const dpo_id_t * parent, dpo_id_t * clone)
{
  cnat_client_t *cc, *cc_clone;

  pool_get_zero (cnat_client_pool, cc_clone);
  cc = cnat_client_get (original->dpoi_index);

  cc_clone->cc_fei = FIB_NODE_INDEX_INVALID;
  cc_clone->parent_cci = cc->parent_cci;
  cc_clone->flags = cc->flags;
  ip_address_copy (&cc_clone->cc_ip, &cc->cc_ip);

  /* stack the clone on the FIB provided parent */
  dpo_stack (cnat_client_dpo, original->dpoi_proto, &cc_clone->cc_parent,
	     parent);

  /* return the clone */
  dpo_set (clone,
	   cnat_client_dpo,
	   original->dpoi_proto, cc_clone - cnat_client_pool);
}

int
cnat_client_purge (void)
{
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  int nthreads;
  nthreads = tm->n_threads + 1;
  ASSERT (0 == hash_elts (cnat_client_db.crd_cip6));
  ASSERT (0 == hash_elts (cnat_client_db.crd_cip4));
  ASSERT (0 == pool_elts (cnat_client_pool));
  for (int i = 0; i < nthreads; i++)
    {
      ASSERT (0 == pool_elts (cnat_client_db.throttle_pool[i]));
    }
  return (0);
}

u8 *
format_cnat_client (u8 * s, va_list * args)
{
  index_t cci = va_arg (*args, index_t);
  u32 indent = va_arg (*args, u32);

  cnat_client_t *cc = pool_elt_at_index (cnat_client_pool, cci);

  s = format (s, "[%d] cnat-client:[%U] tr:%d sess:%d", cci,
	      format_ip_address, &cc->cc_ip,
	      cc->tr_refcnt, cc->session_refcnt);
  if (cc->flags & CNAT_FLAG_EXPIRES)
    s = format (s, " expires");

  if (cc->flags & CNAT_FLAG_EXCLUSIVE)
    s = format (s, " exclusive");

  if (cnat_client_is_clone (cc))
    s = format (s, "\n%Uclone of [%d]\n%U%U",
		format_white_space, indent + 2, cc->parent_cci,
		format_white_space, indent + 2,
		format_dpo_id, &cc->cc_parent, indent + 4);

  return (s);
}


static clib_error_t *
cnat_client_show (vlib_main_t * vm,
		  unformat_input_t * input, vlib_cli_command_t * cmd)
{
  index_t cci;

  cci = INDEX_INVALID;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%d", &cci))
	;
      else
	return (clib_error_return (0, "unknown input '%U'",
				   format_unformat_error, input));
    }

  if (INDEX_INVALID == cci)
    {
      /* *INDENT-OFF* */
      pool_foreach_index(cci, cnat_client_pool, ({
        vlib_cli_output(vm, "%U", format_cnat_client, cci, 0);
      }))
      /* *INDENT-ON* */

      vlib_cli_output (vm, "%d clients", pool_elts (cnat_client_pool));
      vlib_cli_output (vm, "%d timestamps", pool_elts (cnat_timestamps));
    }
  else
    {
      vlib_cli_output (vm, "Invalid policy ID:%d", cci);
    }

  return (NULL);
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (cnat_client_show_cmd_node, static) = {
  .path = "show cnat client",
  .function = cnat_client_show,
  .short_help = "show cnat client",
  .is_mp_safe = 1,
};
/* *INDENT-ON* */

const static char *const cnat_client_dpo_ip4_nodes[] = {
  "ip4-cnat-tx",
  NULL,
};

const static char *const cnat_client_dpo_ip6_nodes[] = {
  "ip6-cnat-tx",
  NULL,
};

const static char *const *const cnat_client_dpo_nodes[DPO_PROTO_NUM] = {
  [DPO_PROTO_IP4] = cnat_client_dpo_ip4_nodes,
  [DPO_PROTO_IP6] = cnat_client_dpo_ip6_nodes,
};

static void
cnat_client_dpo_lock (dpo_id_t * dpo)
{
  cnat_client_t *cc;

  cc = cnat_client_get (dpo->dpoi_index);

  cc->cc_locks++;
}

static void
cnat_client_dpo_unlock (dpo_id_t * dpo)
{
  cnat_client_t *cc;

  cc = cnat_client_get (dpo->dpoi_index);

  cc->cc_locks--;

  if (0 == cc->cc_locks)
    {
      ASSERT (cnat_client_is_clone (cc));
      pool_put (cnat_client_pool, cc);
    }
}

u8 *
format_cnat_client_dpo (u8 * s, va_list * ap)
{
  index_t cci = va_arg (*ap, index_t);
  u32 indent = va_arg (*ap, u32);

  s = format (s, "%U", format_cnat_client, cci, indent);

  return (s);
}

const static dpo_vft_t cnat_client_dpo_vft = {
  .dv_lock = cnat_client_dpo_lock,
  .dv_unlock = cnat_client_dpo_unlock,
  .dv_format = format_cnat_client_dpo,
  .dv_mk_interpose = cnat_client_dpo_interpose,
};

static clib_error_t *
cnat_client_init (vlib_main_t * vm)
{
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  int nthreads = tm->n_threads + 1;
  int i;
  cnat_client_dpo = dpo_register_new_type (&cnat_client_dpo_vft,
					   cnat_client_dpo_nodes);

  cnat_client_db.crd_cip6 = hash_create_mem (0,
					     sizeof (ip6_address_t),
					     sizeof (uword));

  vec_validate (cnat_client_db.throttle_pool, nthreads);
  vec_validate (cnat_client_db.throttle_pool_lock, nthreads);
  for (i = 0; i < nthreads; i++)
    clib_spinlock_init (&cnat_client_db.throttle_pool_lock[i]);

  return (NULL);
}

VLIB_INIT_FUNCTION (cnat_client_init);

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