aboutsummaryrefslogtreecommitdiffstats
path: root/test/vpp_mpls_tunnel_interface.py
blob: a2ab1bf27cb819c6803adde19de11134c8f68764 (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
from vpp_interface import VppInterface


class VppMPLSTunnelInterface(VppInterface):
    """
    VPP MPLS Tunnel interface
    """

    def __init__(self, test, paths, is_multicast=0, is_l2=0):
        """ Create MPLS Tunnel interface """
        super(VppMPLSTunnelInterface, self).__init__(test)
        self.t_paths = paths
        self.is_multicast = is_multicast
        self.is_l2 = is_l2

    def add_vpp_config(self):
        sw_if_index = 0xffffffff
        for path in self.t_paths:
            lstack = path.encode_labels()

            reply = self.test.vapi.mpls_tunnel_add_del(
                sw_if_index,
                1,  # IPv4 next-hop
                path.nh_addr,
                path.nh_itf,
                path.nh_table_id,
                path.weight,
                next_hop_via_label=path.nh_via_label,
                next_hop_out_label_stack=lstack,
                next_hop_n_out_labels=len(lstack),
                is_multicast=self.is_multicast,
                l2_only=self.is_l2)
            sw_if_index = reply.sw_if_index
            self.tunnel_index = reply.tunnel_index
        self.set_sw_if_index(sw_if_index)
        self._test.registry.register(self, self._test.logger)

    def remove_vpp_config(self):
        for path in self.t_paths:
            lstack = path.encode_labels()

            self.test.vapi.mpls_tunnel_add_del(
                self.sw_if_index,
                1,  # IPv4 next-hop
                path.nh_addr,
                path.nh_itf,
                path.nh_table_id,
                path.weight,
                next_hop_via_label=path.nh_via_label,
                next_hop_out_label_stack=lstack,
                next_hop_n_out_labels=len(lstack),
                is_add=0)

    def query_vpp_config(self):
        dump = self._test.vapi.mpls_tunnel_dump()
        for t in dump:
            if self.sw_if_index == t.mt_sw_if_index and \
               self.tunnel_index == t.mt_tunnel_index:
                return True
        return False

    def __str__(self):
        return self.object_id()

    def object_id(self):
        return ("mpls-tunnel%d-%d" % (self.tunnel_index,
                                      self.sw_if_index))
ctx); return (BIHASH_WALK_CONTINUE); } void cnat_session_walk (cnat_session_walk_cb_t cb, void *ctx) { cnat_session_walk_ctx_t wctx = { .cb = cb, .ctx = ctx, }; BV (clib_bihash_foreach_key_value_pair) (&cnat_session_db, cnat_session_walk_cb, &wctx); } typedef struct cnat_session_purge_walk_t_ { clib_bihash_kv_40_48_t *keys; } cnat_session_purge_walk_ctx_t; static int cnat_session_purge_walk (BVT (clib_bihash_kv) * key, void *arg) { cnat_session_purge_walk_ctx_t *ctx = arg; vec_add1 (ctx->keys, *key); return (BIHASH_WALK_CONTINUE); } u8 * format_cnat_session (u8 * s, va_list * args) { cnat_session_t *sess = va_arg (*args, cnat_session_t *); CLIB_UNUSED (int verbose) = va_arg (*args, int); f64 ts = 0; if (!pool_is_free_index (cnat_timestamps, sess->value.cs_ts_index)) ts = cnat_timestamp_exp (sess->value.cs_ts_index); s = format (s, "session:[%U;%d -> %U;%d, %U] => %U;%d -> %U;%d lb:%d age:%f", format_ip46_address, &sess->key.cs_ip[VLIB_RX], IP46_TYPE_ANY, clib_host_to_net_u16 (sess->key.cs_port[VLIB_RX]), format_ip46_address, &sess->key.cs_ip[VLIB_TX], IP46_TYPE_ANY, clib_host_to_net_u16 (sess->key.cs_port[VLIB_TX]), format_ip_protocol, sess->key.cs_proto, format_ip46_address, &sess->value.cs_ip[VLIB_RX], IP46_TYPE_ANY, clib_host_to_net_u16 (sess->value.cs_port[VLIB_RX]), format_ip46_address, &sess->value.cs_ip[VLIB_TX], IP46_TYPE_ANY, clib_host_to_net_u16 (sess->value.cs_port[VLIB_TX]), sess->value.cs_lbi, ts); return (s); } static clib_error_t * cnat_session_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { u8 verbose = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose")) verbose = 1; else return (clib_error_return (0, "unknown input '%U'", format_unformat_error, input)); } vlib_cli_output (vm, "CNat Sessions: now:%f\n%U\n", vlib_time_now (vm), BV (format_bihash), &cnat_session_db, verbose); return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cnat_session_show_cmd_node, static) = { .path = "show cnat session", .function = cnat_session_show, .short_help = "show cnat session", .is_mp_safe = 1, }; /* *INDENT-ON* */ void cnat_session_free (cnat_session_t * session) { clib_bihash_kv_40_48_t *bkey = (clib_bihash_kv_40_48_t *) session; /* age it */ if (session->value.flags & CNAT_SESSION_FLAG_ALLOC_PORT) cnat_free_port_cb (session->value.cs_port[VLIB_RX], session->key.cs_proto); if (!(session->value.flags & CNAT_SESSION_FLAG_NO_CLIENT)) cnat_client_free_by_ip (&session->key.cs_ip[VLIB_TX], session->key.cs_af); cnat_timestamp_free (session->value.cs_ts_index); clib_bihash_add_del_40_48 (&cnat_session_db, bkey, 0 /* is_add */ ); } int cnat_session_purge (void) { /* flush all the session from the DB */ cnat_session_purge_walk_ctx_t ctx = { }; clib_bihash_kv_40_48_t *key; BV (clib_bihash_foreach_key_value_pair) (&cnat_session_db, cnat_session_purge_walk, &ctx); vec_foreach (key, ctx.keys) cnat_session_free ((cnat_session_t *) key); vec_free (ctx.keys); return (0); } u64 cnat_session_scan (vlib_main_t * vm, f64 start_time, int i) { BVT (clib_bihash) * h = &cnat_session_db; int j, k; /* Don't scan the l2 fib if it hasn't been instantiated yet */ if (alloc_arena (h) == 0) return 0.0; for ( /* caller saves starting point */ ; i < h->nbuckets; i++) { /* allow no more than 100us without a pause */ if ((vlib_time_now (vm) - start_time) > 10e-5) return (i); if (i < (h->nbuckets - 3)) { BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i + 3); CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD); b = BV (clib_bihash_get_bucket) (h, i + 1); if (!BV (clib_bihash_bucket_is_empty) (b)) { BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset); CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD); } } BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i); if (BV (clib_bihash_bucket_is_empty) (b)) continue; BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset); for (j = 0; j < (1 << b->log2_pages); j++) { for (k = 0; k < BIHASH_KVP_PER_PAGE; k++) { if (v->kvp[k].key[0] == ~0ULL && v->kvp[k].value[0] == ~0ULL) continue; cnat_session_t *session = (cnat_session_t *) & v->kvp[k]; if (start_time > cnat_timestamp_exp (session->value.cs_ts_index)) { /* age it */ cnat_session_free (session); /* * Note: we may have just freed the bucket's backing * storage, so check right here... */ if (BV (clib_bihash_bucket_is_empty) (b)) goto doublebreak; } } v++; } doublebreak: ; } /* start again */ return (0); } static clib_error_t * cnat_session_init (vlib_main_t * vm) { cnat_main_t *cm = &cnat_main; BV (clib_bihash_init) (&cnat_session_db, "CNat Session DB", cm->session_hash_buckets, cm->session_hash_memory); BV (clib_bihash_set_kvp_format_fn) (&cnat_session_db, format_cnat_session); return (NULL); } VLIB_INIT_FUNCTION (cnat_session_init); static clib_error_t * cnat_timestamp_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { cnat_timestamp_t *ts; clib_rwlock_reader_lock (&cnat_main.ts_lock); /* *INDENT-OFF* */ pool_foreach (ts, cnat_timestamps, ({ vlib_cli_output (vm, "[%d] last_seen:%f lifetime:%u ref:%u", ts - cnat_timestamps, ts->last_seen, ts->lifetime, ts->refcnt); })); /* *INDENT-ON* */ clib_rwlock_reader_unlock (&cnat_main.ts_lock); return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cnat_timestamp_show_cmd, static) = { .path = "show cnat timestamp", .function = cnat_timestamp_show, .short_help = "show cnat timestamp", .is_mp_safe = 1, }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */