aboutsummaryrefslogtreecommitdiffstats
path: root/vnet/etc/scripts
diff options
context:
space:
mode:
authorNeale Ranns <nranns@cisco.com>2016-10-03 13:05:48 +0100
committerDamjan Marion <dmarion.lists@gmail.com>2016-10-07 21:32:24 +0000
commit3ee44040c66cbe47ff292ac7fb0badccbe2afe6d (patch)
treea52a4dd0750467845f237ee5e4e88aa95ea11bab /vnet/etc/scripts
parent4a7e58bf481adb843707eec4a81213776a6d5212 (diff)
unicast RPF for FIB2.0
In a heirarchical FIB performing a unicast RPF check would require the traversal of the data-plane graph to seek out all the adjacency objects and then read those to find their interface. This is not efficient. Instead, for each path-list we construct a list of unique input interfaces and link this uRPF-list against the entry in the prefix table. In the data-plane the uRPF list can be retrieved from the load-balance lookup result and the RPF check is a simple and efficient walk across the minimal interface list. The uRPF-list is maintained as the routing heirarchy changes, in a similar way to the data-plane object graph. We also provide a knob to allow an arbitrary prefix to pass the loose check. Change-Id: Ie7c0ae3c4483ef467cfd5b136ee0315ff98ec15b Signed-off-by: Neale Ranns <nranns@cisco.com>
Diffstat (limited to 'vnet/etc/scripts')
-rw-r--r--vnet/etc/scripts/urpf86
1 files changed, 86 insertions, 0 deletions
diff --git a/vnet/etc/scripts/urpf b/vnet/etc/scripts/urpf
new file mode 100644
index 00000000000..a4d875276ee
--- /dev/null
+++ b/vnet/etc/scripts/urpf
@@ -0,0 +1,86 @@
+
+create loop int
+
+set int state loop0 up
+set int ip addr loop0 10.10.10.10/24
+
+packet-generator new {
+ name transit-deny
+ limit 1
+ node ip4-input
+ size 64-64
+ no-recycle
+ data {
+ UDP: 1.2.3.4 -> 2.2.2.2
+ UDP: 3000 -> 3001
+ length 128 checksum 0 incrementing 1
+ }
+}
+
+packet-generator new {
+ name transit-allow
+ limit 1
+ node ip4-input
+ size 64-64
+ no-recycle
+ data {
+ UDP: 1.1.1.1 -> 2.2.2.2
+ UDP: 3000 -> 3001
+ length 128 checksum 0 incrementing 1
+ }
+}
+
+packet-generator new {
+ name transit-allow-from-excemption
+ limit 1
+ node ip4-input
+ size 64-64
+ no-recycle
+ data {
+ UDP: 11.11.12.13 -> 2.2.2.2
+ UDP: 6000 -> 6001
+ length 128 checksum 0 incrementing 1
+ }
+}
+
+packet-generator new {
+ name for-us-allow-from-excemption
+ limit 1
+ node ip4-input
+ size 64-64
+ no-recycle
+ data {
+ UDP: 11.11.12.13 -> 10.10.10.10
+ UDP: 6000 -> 6001
+ length 128 checksum 0 incrementing 1
+ }
+}
+
+packet-generator new {
+ name for-us-allow
+ limit 1
+ node ip4-input
+ size 64-64
+ no-recycle
+ data {
+ UDP: 1.1.1.1 -> 10.10.10.10
+ UDP: 3000 -> 3001
+ length 128 checksum 0 incrementing 1
+ }
+}
+
+tr add pg-input 100
+
+set int ip addr pg0 10.10.11.10/24
+
+set interface ip source-check pg0 strict
+
+ip route add 1.1.1.1/32 via 10.10.11.11 pg0
+ip route add 2.2.2.2/32 via 10.10.10.11 loop0
+
+ip urpf-accept 11.11.0.0/16
+
+#set interface ip source-check pg0 strict del
+#set interface ip source-check pg0 loose
+
+#ip urpf-accept del 11.11.0.0/16
d='n273' href='#n273'>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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
/*---------------------------------------------------------------------------
 * Copyright (c) 2009-2014 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.
 *---------------------------------------------------------------------------
 */

#ifndef __included_vppjni_bridge_domain_h__
#define __included_vppjni_bridge_domain_h__

#include <vlib/vlib.h>
#include <vppinfra/clib.h>
#include <vppinfra/mhash.h>

/*
 * The L2fib key is the mac address and bridge domain ID
 */
#define MAC_ADDRESS_SIZE  6

typedef struct {
  union {
    struct {
      u16 unused1;
      u8  mac[MAC_ADDRESS_SIZE]; 
    } fields;
    u64 raw;
  };
} l2fib_u64_mac_t;

/* 
 * The l2fib entry results
 */
typedef struct {
  u32 bd_id;
  l2fib_u64_mac_t mac_addr;
  u32 sw_if_index;
  u8  learned:1;
  u8  bvi:1;
  u8  filter:1;      // drop packets to/from this mac
  u8  unused1:5;
} bd_l2fib_oper_t;

typedef struct {
  u32 bd_id;
  u8 * bd_name;
} bd_local_cfg_t;

typedef struct {
  u32 bd_id;
  u32 sw_if_index;
  u32 shg;
} bd_sw_if_oper_t;

typedef struct {
  u32 bd_id;
  u8 flood:1;
  u8 forward:1;
  u8 learn:1;
  u8 uu_flood:1;
  u8 arp_term:1;
  u8 unused1:3;
  u32 bvi_sw_if_index;
  u32 n_sw_ifs;
  bd_sw_if_oper_t * bd_sw_if_oper;
  f64 last_sync_time;
  mhash_t l2fib_index_by_mac;
  bd_l2fib_oper_t * l2fib_oper;	// vector indexed by l2fib_index
} vjbd_oper_t;

#define BD_OPER_REFRESH_INTERVAL  	2.0
#define BD_OPER_L2FIB_REFRESH_INTERVAL	5.0

typedef struct {
  u32 next_bd_id;
  uword * bd_index_bitmap;
  uword * bd_index_by_id;
  mhash_t bd_id_by_name;
  bd_local_cfg_t * local_cfg;		// vector indexed by bd_index
  vjbd_oper_t * bd_oper;		// vector indexed by oper_bd_index
  f64 bd_oper_last_sync_all_time;
  bd_sw_if_oper_t * sw_if_oper;	// vector indexed by sw_if_index
  f64 l2fib_oper_last_sync_time;
  uword * bd_id_by_sw_if_index;
  uword * oper_bd_index_by_bd_id;
} vjbd_main_t;

extern vjbd_main_t vjbd_main;

always_inline
u64 l2fib_mac_to_u64 (u8 * mac_address) {
  u64 temp;

  // The mac address in memory is A:B:C:D:E:F
  // The bd id in register is H:L
#if CLIB_ARCH_IS_LITTLE_ENDIAN
  // Create the in-register key as F:E:D:C:B:A:H:L
  // In memory the key is L:H:A:B:C:D:E:F
  temp = *((u64 *)(mac_address - 2));
  temp = (temp & ~0xffff);
#else
  // Create the in-register key as H:L:A:B:C:D:E:F
  // In memory the key is H:L:A:B:C:D:E:F
  temp = *((u64 *)(mac_address)) >> 16;
#endif

  return temp;
}

static_always_inline void vjbd_main_init (vjbd_main_t *bdm)
{
  bdm->bd_index_by_id = hash_create (0, sizeof(uword));
  mhash_init_vec_string (&bdm->bd_id_by_name, sizeof (u32));
  bdm->bd_id_by_sw_if_index = hash_create (0, sizeof (u32));
  bdm->oper_bd_index_by_bd_id = hash_create (0, sizeof (u32));
}

static_always_inline u32 vjbd_id_is_valid (vjbd_main_t * bdm, u32 bd_id)
{
  return ((bd_id != 0) && (bd_id != ~0) && (bd_id <= bdm->next_bd_id));
}

static_always_inline u32 vjbd_index_is_free (vjbd_main_t * bdm, u16 bd_index)
{
  u32 bd_id = vec_elt_at_index(bdm->local_cfg, bd_index)->bd_id;

  return (!clib_bitmap_get (bdm->bd_index_bitmap, (bd_index)) &&
          (bd_index < vec_len (bdm->local_cfg)) &&
          ((bd_id == 0) || (bd_id == ~0)));
}

static_always_inline u32 vjbd_index_is_valid (vjbd_main_t * bdm, u16 bd_index)
{
  return (clib_bitmap_get (bdm->bd_index_bitmap, bd_index) &&
          (bd_index < vec_len (bdm->local_cfg)));
}

static_always_inline u32 vjbd_id_from_name (vjbd_main_t * bdm,
                                            const u8 * bd_name)
{
  u32 bd_id;
  uword * p;

  ASSERT (vec_c_string_is_terminated (bd_name));

  if (bdm->next_bd_id == 0)
    return ~0;

  p = mhash_get (&bdm->bd_id_by_name, (void *)bd_name);
  if (p)
    {
      bd_id = p[0];
      ASSERT (vjbd_id_is_valid (bdm, bd_id));
    }
  else
    bd_id = ~0;

  return bd_id;
}

static_always_inline u32 vjbd_index_from_id (vjbd_main_t * bdm, u32 bd_id)
{
  uword * p;
  u16 bd_index;

  ASSERT (vjbd_id_is_valid (bdm, bd_id));

  p = hash_get (bdm->bd_index_by_id, bd_id);

  ASSERT (p); // there is always an index associated with a valid bd_id
  bd_index = p[0];

  ASSERT (vjbd_index_is_valid (bdm, bd_index));

  return bd_index;
}

static_always_inline u32 vjbd_id_from_index (vjbd_main_t * bdm, u16 bd_index)
{
  u32 bd_id;

  ASSERT (vjbd_index_is_valid (bdm, bd_index));

  bd_id = vec_elt_at_index(bdm->local_cfg, bd_index)->bd_id;

  ASSERT (vjbd_id_is_valid (bdm, bd_id));

  return bd_id;
}

static_always_inline u8 * vjbd_name_from_id (vjbd_main_t * bdm, u32 bd_id)
{
  u16 bd_index = vjbd_index_from_id (bdm, bd_id);

  return vec_elt_at_index(bdm->local_cfg, bd_index)->bd_name;
}

static_always_inline u8 * vjbd_oper_name_from_id (vjbd_main_t * bdm, u32 bd_id)
{
    if (vjbd_id_is_valid (bdm, bd_id)) {
        return format(0, "%s", vjbd_name_from_id(bdm, bd_id));
    } else {
        return format(0, "BridgeDomainOper%d", bd_id);
    }
}

static_always_inline vjbd_oper_t * vjbd_oper_from_id (vjbd_main_t * bdm,
                                                  u32 bd_id)
{
  u16 bd_index = vjbd_index_from_id (bdm, bd_id);
  return vec_elt_at_index (bdm->bd_oper, bd_index);
}

static_always_inline void vjbd_oper_maybe_sync_from_vpp (vjbd_main_t * bdm, 
                                                       u32 bd_id)
{
#ifdef VPPJNI_OPER
  vppjni_vpe_api_msg_main_t *ovam = ovam_get_main ();

  if (bd_id == ~0)
    {
      if ((ovam_time_now (ovam) - bdm->bd_oper_last_sync_all_time) >
          BD_OPER_REFRESH_INTERVAL)
        {
          ovam_bridge_domain_dump (bd_id);
          bdm->bd_oper_last_sync_all_time = ovam_time_now (ovam);
        }
    }
    
  else 
    {
      vjbd_oper_t * bd_oper = vjbd_oper_from_id (bdm, bd_id);

      if ((ovam_time_now (ovam) - bd_oper->last_sync_time) >
          BD_OPER_REFRESH_INTERVAL)
        {
          ovam_bridge_domain_dump (bd_id);

          bd_oper->last_sync_time = ovam_time_now (ovam);
        }
    }
#endif
}

static_always_inline u32 vjbd_id_from_sw_if_index (vjbd_main_t * bdm,
                                                 u32 sw_if_index)
{
  bd_sw_if_oper_t * bd_sw_if_oper;
  u32 bd_id = ~0;

  vjbd_oper_maybe_sync_from_vpp (bdm, ~0);
  if (sw_if_index < vec_len (bdm->sw_if_oper))
    {
      bd_sw_if_oper = vec_elt_at_index (bdm->sw_if_oper, sw_if_index);
      bd_id = bd_sw_if_oper->bd_id;
    }

  return bd_id;
}

static_always_inline u8 * vjbd_name_from_sw_if_index (vjbd_main_t * bdm,
                                                    u32 sw_if_index)
{
  u32 bd_id, bd_index;
  u8 * bd_name = 0;
  
  /* DAW-FIXME:
  ASSERT (ovam_sw_if_index_valid (ovam_get_main(), sw_if_index));
  */
  vjbd_oper_maybe_sync_from_vpp (bdm, ~0);
  bd_id = vjbd_id_from_sw_if_index (bdm, sw_if_index);
  if (vjbd_id_is_valid (bdm, bd_id))
    {
      bd_index = vjbd_index_from_id (bdm, bd_id);
      bd_name = vec_elt_at_index (bdm->local_cfg, bd_index)->bd_name;
    }

  return bd_name;
}

static_always_inline u32 
vjbd_oper_l2fib_index_from_mac (vjbd_oper_t * bd_oper, u8 * mac)
{
  u32 l2fib_index;
  uword * p;

  p = mhash_get (&bd_oper->l2fib_index_by_mac, mac);
  if (p)
    {
      l2fib_index = p[0];
      ASSERT (l2fib_index < vec_len (bd_oper->l2fib_oper));
    }
  else
    l2fib_index = ~0;

  return l2fib_index;
}

static_always_inline u32 vjbd_local_cfg_next_id (vjbd_main_t * bdm, 
                                               u32 bd_id)
{
  u32 i, end = vec_len (bdm->local_cfg);
  u32 next_bd_id = 0;

  if ((bd_id == 0) || vjbd_id_is_valid (bdm, bd_id))
    for (i = 0; i < end; i++)
      {
        u32 curr_bd_id = bdm->local_cfg[i].bd_id;
        if ((curr_bd_id != ~0) && (curr_bd_id > bd_id) && 
            ((next_bd_id == 0) || (curr_bd_id < next_bd_id)))
          next_bd_id = curr_bd_id;
      }

  return next_bd_id;
}

static_always_inline u32 vjbd_sw_if_oper_next_index (vjbd_main_t * bdm, 
                                                   u32 start, u32 bd_id)
{
  u32 i, end = vec_len (bdm->sw_if_oper);

  if (vjbd_id_is_valid (bdm, bd_id))
    for (i = start; i < end; i++)
      if (bdm->sw_if_oper[i].bd_id == bd_id)
        return i;

  return ~0;
}

static_always_inline void 
vjbd_oper_l2fib_maybe_sync_from_vpp (vjbd_main_t * bdm)
{
#ifdef VPPJNI_OPER
  vppjni_vpe_api_msg_main_t *ovam = ovam_get_main ();
  if ((ovam_time_now (ovam) - bdm->l2fib_oper_last_sync_time) >
      BD_OPER_L2FIB_REFRESH_INTERVAL)
    {
      ovam_l2fib_table_dump ();
      bdm->l2fib_oper_last_sync_time = ovam_time_now (ovam);
    }
#endif
}

static_always_inline void vjbd_l2fib_oper_reset (vjbd_main_t * bdm)
{
  vjbd_oper_t * bd_oper;

  vec_foreach (bd_oper, bdm->bd_oper)
    {
      mhash_init (&bd_oper->l2fib_index_by_mac, sizeof (u32), MAC_ADDRESS_SIZE);
      vec_reset_length (bd_oper->l2fib_oper);      
    }
}

static_always_inline void vjbd_oper_reset (vjbd_main_t * bdm, u32 bd_id)
{
  u16 bd_index;
  u32 si, len;
  vjbd_oper_t * bd_oper;
  u32 end;

  if (!bdm->bd_oper)
    {
      ASSERT (vec_len (bdm->sw_if_oper) == 0);
      return;
    }

  if (bd_id == ~0)
    {
      bdm->bd_oper_last_sync_all_time = 0.0;
      bd_index = 0;
      end = vec_len (bdm->bd_oper);
    }
  else
    {
      bd_index = vjbd_index_from_id (bdm, bd_id);
      end = bd_index + 1;
    }

  for (; bd_index < end; bd_index++)
    {
      bd_oper = vec_elt_at_index (bdm->bd_oper, bd_index);
      bd_oper->last_sync_time = 0.0;

      len = vec_len (bdm->sw_if_oper);
      for (si = vjbd_sw_if_oper_next_index (bdm, 0, bd_id);
           (si != ~0) && (si < len);
           si = vjbd_sw_if_oper_next_index (bdm, si + 1, bd_id))
        {
          bd_sw_if_oper_t * bd_sw_if_oper;

          bd_sw_if_oper = vec_elt_at_index (bdm->sw_if_oper, si);
          bd_sw_if_oper->bd_id = ~0;
        }
    }
}

static_always_inline void 
vjbd_sw_if_add_del (u32 sw_if_index ,u32 bd_id, u8 bvi, u8 shg, u8 is_add)
{
  vjbd_main_t * bdm = &vjbd_main;
  u16 bd_index = vjbd_index_from_id (bdm, bd_id);
  vjbd_oper_t * bd_oper = vec_elt_at_index (bdm->bd_oper, bd_index);
  bd_sw_if_oper_t * bd_sw_if_oper;
  
  ASSERT (vjbd_id_is_valid (bdm, bd_id));
  /* DAW-FIXME
  ASSERT (ovam_sw_if_index_valid (ovam_get_main (), sw_if_index));
  */

  vec_validate (bdm->sw_if_oper, sw_if_index);
  bd_sw_if_oper = vec_elt_at_index (bdm->sw_if_oper, sw_if_index);
  if (is_add)
    {
      bd_sw_if_oper->bd_id = bd_id;
      bd_sw_if_oper->shg = shg;
      bd_oper->bvi_sw_if_index = bvi ? sw_if_index : ~0;
    }
  else
    {
      bd_sw_if_oper->bd_id = 0;
      bd_sw_if_oper->shg = 0;
      if (bd_oper->bvi_sw_if_index == sw_if_index)
        bd_oper->bvi_sw_if_index = ~0;
    }
}

static_always_inline u32 vjbd_id_sw_if_count (vjbd_main_t * bdm, u32 bd_id)
{
  u32 count = 0, i, end = vec_len (bdm->sw_if_oper);

  if (vjbd_id_is_valid (bdm, bd_id))
    for (count = i = 0; i < end; i++)
      if (bdm->sw_if_oper[i].bd_id == bd_id)
        count++;

  return count;
}

static_always_inline u32 vjbd_find_or_add_bd (vjbd_main_t * bdm, u8 * bd_name)
{
  u16 bd_index;
  u32 bd_id;
  bd_local_cfg_t * bd_local_cfg;
  uword mhash_val_bd_id;

  bd_id = vjbd_id_from_name (bdm, bd_name);
  if (bd_id != ~0)
    return bd_id;

  mhash_val_bd_id = bd_id = ++bdm->next_bd_id;
  mhash_set_mem (&bdm->bd_id_by_name, (void *)bd_name, &mhash_val_bd_id, 0);

  bd_index = clib_bitmap_first_clear (bdm->bd_index_bitmap);
  vec_validate (bdm->local_cfg, bd_index);
  vec_validate (bdm->bd_oper, bd_index);

  ASSERT (vjbd_index_is_free (bdm, bd_index));

  bd_local_cfg = vec_elt_at_index (bdm->local_cfg, bd_index);
  bd_local_cfg->bd_id = bd_id;
  vec_validate_init_c_string (bd_local_cfg->bd_name, bd_name,
                              vec_len (bd_name) - 1);
  hash_set (bdm->bd_index_by_id, bd_id, bd_index);
  bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap,
                                          bd_index, 1);
  return bd_id;
}

static_always_inline void vjbd_delete_bd (vjbd_main_t * bdm, u32 bd_id)
{
  u16 bd_index;
  bd_local_cfg_t * bd_local_cfg;

  ASSERT (vjbd_id_is_valid (bdm, bd_id));

  // bd must not have any members before deleting
  ASSERT (!vjbd_id_sw_if_count (bdm, bd_id));

  bd_index = vjbd_index_from_id (bdm, bd_id);
  bd_local_cfg = vec_elt_at_index (bdm->local_cfg, bd_index);
  vjbd_oper_reset (bdm, bd_id);

  mhash_unset (&bdm->bd_id_by_name, vjbd_name_from_id (bdm, bd_id), 0);
  bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap,
                                          bd_index, 0);
  hash_unset (bdm->bd_index_by_id, bd_id);
  bd_local_cfg->bd_id = ~0;
  vec_validate_init_c_string (bd_local_cfg->bd_name, "", 0);

  if (clib_bitmap_is_zero (bdm->bd_index_bitmap))
    {
      vec_reset_length (bdm->local_cfg);
      vec_reset_length (bdm->bd_oper);
    }

  /* Force a resync of all bd_oper data. */
  bdm->bd_oper_last_sync_all_time = 0.0;
  vjbd_oper_maybe_sync_from_vpp (bdm, ~0);
}

#endif /* __included_vppjni_vpp_bridge_domain_h__ */