summaryrefslogtreecommitdiffstats
path: root/src/vnet/bier/bier_fmask_db.c
blob: d0f5ba1c18ec1389ed942b380a2e3b2d36b48700 (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
/*
 * 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.
 */

#include <vnet/bier/bier_fmask_db.h>
#include <vnet/bier/bier_fmask.h>

/**
 * Global Table of fmask objects
 * The key into this table includes the table's key and the fmask's key,
 * so there could be a DB per-table. But it is more efficient
 * at forwarding time to extract the fmask from a single global table
 * which is hot in dcache.
 *
 * The table's key is part of this DB key, since the fmasks therein build up
 * their forwarding mask based on the routes that resolve through
 * it, so cross pollination would be bad.
 */
typedef struct bier_fmask_db_t_ {
    /**
     * hash table for underlying storage
     */
    uword *bfdb_hash;

    /**
     * Pool for memory
     */
    struct bier_fmask_t_ *bfdb_pool;
} bier_fmask_db_t;

/**
 * Single fmask DB
 */
static bier_fmask_db_t bier_fmask_db;


u32
bier_fmask_get_index (const bier_fmask_t *bfm)
{
    return (bfm - bier_fmask_db.bfdb_pool);
}

static void
bier_fmask_db_mk_key (index_t bti,
                      const fib_route_path_t *rpath,
                      bier_fmask_id_t *key)
{
    /*
     * Depending on what the ID is there may be padding.
     * This key will be memcmp'd in the mhash, so make sure it's all 0
     */
    clib_memset(key, 0, sizeof(*key));

    /*
     * Pick the attributes from the path that make the FMask unique
     */
    if (FIB_ROUTE_PATH_UDP_ENCAP & rpath->frp_flags)
    {
        key->bfmi_id = rpath->frp_udp_encap_id;
        key->bfmi_nh_type = BIER_NH_UDP;
    }
    else
    {
        memcpy(&key->bfmi_nh, &rpath->frp_addr, sizeof(rpath->frp_addr));
        key->bfmi_nh_type = BIER_NH_IP;
    }
    if (NULL == rpath->frp_label_stack)
    {
        key->bfmi_hdr_type = BIER_HDR_O_OTHER;
    }
    else
    {
        key->bfmi_hdr_type = BIER_HDR_O_MPLS;
    }
    key->bfmi_bti = bti;
}

u32
bier_fmask_db_find (index_t bti,
                    const fib_route_path_t *rpath)
{
    bier_fmask_id_t fmid;
    uword *p;

    bier_fmask_db_mk_key(bti, rpath, &fmid);
    p = hash_get_mem(bier_fmask_db.bfdb_hash, &fmid);

    if (NULL != p)
    {
        return (p[0]);
    }

    return (INDEX_INVALID);
}

u32
bier_fmask_db_find_or_create_and_lock (index_t bti,
                                       const fib_route_path_t *rpath)
{
    bier_fmask_id_t fmid;
    u32 index;
    uword *p;

    bier_fmask_db_mk_key(bti, rpath, &fmid);
    p = hash_get_mem(bier_fmask_db.bfdb_hash, &fmid);

    if (NULL == p)
    {
        bier_fmask_t *bfm;
        /*
         * adding a new fmask object
         */
        index = bier_fmask_create_and_lock(&fmid, rpath);
        bfm = bier_fmask_get(index);
        hash_set_mem(bier_fmask_db.bfdb_hash, bfm->bfm_id, index);
    }
    else
    {
        index = p[0];
        bier_fmask_lock(index);
    }

    return (index);
}

void
bier_fmask_db_remove (const bier_fmask_id_t *fmid)
{
    uword *p;

    p = hash_get_mem(bier_fmask_db.bfdb_hash, fmid);

    if (NULL == p) {
        /*
         * remove a non-existent entry - oops
         */
        ASSERT (!"remove non-existent fmask");
    } else {
        hash_unset(bier_fmask_db.bfdb_hash, fmid);
    }
}

void
bier_fmask_db_walk (bier_fmask_walk_fn_t fn, void *ctx)
{
    CLIB_UNUSED (bier_fmask_id_t *fmid);
    uword *bfmi;

    hash_foreach(fmid, bfmi, bier_fmask_db.bfdb_hash,
    ({
        if (WALK_STOP == fn(*bfmi, ctx))
            break;
    }));
}

clib_error_t *
bier_fmask_db_module_init (vlib_main_t *vm)
{
    bier_fmask_db.bfdb_hash = hash_create_mem(0,
                                              sizeof(bier_fmask_id_t),
                                              sizeof(index_t));

    return (NULL);
}

VLIB_INIT_FUNCTION (bier_fmask_db_module_init);
data_offset; hsp_worker_t *wrk; u8 *http_response; u8 is_file; u8 use_ptr; } hsp_main_t; typedef enum { HSP_CONNECT_FAILED = 1, HSP_TRANSPORT_CLOSED, HSP_REPLY_RECEIVED, } hsp_cli_signal_t; static hsp_main_t hsp_main; static inline hsp_worker_t * hsp_worker_get (u32 thread_index) { return &hsp_main.wrk[thread_index]; } static inline hsp_session_t * hsp_session_get (u32 session_index, u32 thread_index) { hsp_worker_t *wrk = hsp_worker_get (thread_index); return pool_elt_at_index (wrk->sessions, session_index); } static hsp_session_t * hsp_session_alloc (hsp_worker_t *wrk) { hsp_session_t *s; pool_get_zero (wrk->sessions, s); s->session_index = s - wrk->sessions; s->thread_index = wrk->thread_index; return s; } static int hsp_session_connected_callback (u32 app_index, u32 hsp_session_index, session_t *s, session_error_t err) { hsp_main_t *hspm = &hsp_main; hsp_session_t *hsp_session, *new_hsp_session; hsp_worker_t *wrk; http_header_t *headers = 0; http_msg_t msg; int rv; if (err) { clib_warning ("hsp_session_index[%d] connected error: %U", hsp_session_index, format_session_error, err); vlib_process_signal_event_mt (hspm->vlib_main, hspm->cli_node_index, HSP_CONNECT_FAILED, 0); return -1; } hsp_session = hsp_session_get (hsp_session_index, 0); wrk = hsp_worker_get (s->thread_index); new_hsp_session = hsp_session_alloc (wrk); clib_memcpy_fast (new_hsp_session, hsp_session, sizeof (*hsp_session)); hsp_session->vpp_session_index = s->session_index; if (hspm->is_file) { http_add_header ( &headers, http_header_name_token (HTTP_HEADER_CONTENT_TYPE), http_content_type_token (HTTP_CONTENT_APP_OCTET_STREAM)); } else { http_add_header ( &headers, http_header_name_token (HTTP_HEADER_CONTENT_TYPE), http_content_type_token (HTTP_CONTENT_APP_X_WWW_FORM_URLENCODED)); } hspm->headers_buf = http_serialize_headers (headers); vec_free (headers); msg.type = HTTP_MSG_REQUEST; msg.method_type = HTTP_REQ_POST; /* request target */ msg.data.target_form = HTTP_TARGET_ORIGIN_FORM; msg.data.target_path_len = vec_len (hspm->target); /* custom headers */ msg.data.headers_len = vec_len (hspm->headers_buf); /* request body */ msg.data.body_len = vec_len (hspm->data); /* total length */ msg.data.len = msg.data.target_path_len + msg.data.headers_len + msg.data.body_len; if (hspm->use_ptr) { uword target = pointer_to_uword (hspm->target); uword headers = pointer_to_uword (hspm->headers_buf); uword body = pointer_to_uword (hspm->data); msg.data.type = HTTP_MSG_DATA_PTR; svm_fifo_seg_t segs[4] = { { (u8 *) &msg, sizeof (msg) }, { (u8 *) &target, sizeof (target) }, { (u8 *) &headers, sizeof (headers) }, { (u8 *) &body, sizeof (body) }, }; rv = svm_fifo_enqueue_segments (s->tx_fifo, segs, 4, 0 /* allow partial */); ASSERT (rv == (sizeof (msg) + sizeof (target) + sizeof (headers) + sizeof (body))); goto done; } msg.data.type = HTTP_MSG_DATA_INLINE; msg.data.target_path_offset = 0; msg.data.headers_offset = msg.data.target_path_len; msg.data.body_offset = msg.data.headers_offset + msg.data.headers_len; rv = svm_fifo_enqueue (s->tx_fifo, sizeof (msg), (u8 *) &msg); ASSERT (rv == sizeof (msg)); rv = svm_fifo_enqueue (s->tx_fifo, vec_len (hspm->target), hspm->target); ASSERT (rv == vec_len (hspm->target)); rv = svm_fifo_enqueue (s->tx_fifo, vec_len (hspm->headers_buf), hspm->headers_buf); ASSERT (rv == msg.data.headers_len); rv = svm_fifo_enqueue (s->tx_fifo, vec_len (hspm->data), hspm->data); if (rv != vec_len (hspm->data)) { hspm->data_offset = (rv > 0) ? rv : 0; svm_fifo_add_want_deq_ntf (s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF); } done: if (svm_fifo_set_event (s->tx_fifo)) session_program_tx_io_evt (s->handle, SESSION_IO_EVT_TX); return 0; } static void hsp_session_disconnect_callback (session_t *s) { hsp_main_t *hspm = &hsp_main; vnet_disconnect_args_t _a = { 0 }, *a = &_a; int rv; a->handle = session_handle (s); a->app_index = hspm->app_index; if ((rv = vnet_disconnect_session (a))) clib_warning ("warning: disconnect returned: %U", format_session_error, rv); } static void hsp_session_transport_closed_callback (session_t *s) { hsp_main_t *hspm = &hsp_main; vlib_process_signal_event_mt (hspm->vlib_main, hspm->cli_node_index, HSP_TRANSPORT_CLOSED, 0); } static void hsp_session_reset_callback (session_t *s) { hsp_main_t *hspm = &hsp_main; hsp_session_t *hsp_session; vnet_disconnect_args_t _a = { 0 }, *a = &_a; int rv; hsp_session = hsp_session_get (s->opaque, s->thread_index); hsp_session->is_closed = 1; a->handle = session_handle (s); a->app_index = hspm->app_index; if ((rv = vnet_disconnect_session (a))) clib_warning ("warning: disconnect returned: %U", format_session_error, rv); } static int hsp_rx_callback (session_t *s) { hsp_main_t *hspm = &hsp_main; hsp_session_t *hsp_session; http_msg_t msg; int rv; hsp_session = hsp_session_get (s->opaque, s->thread_index); if (hsp_session->is_closed) { clib_warning ("hsp_session_index[%d] is closed", s->opaque); return -1; } rv = svm_fifo_dequeue (s->rx_fifo, sizeof (msg), (u8 *) &msg); ASSERT (rv == sizeof (msg)); if (msg.type != HTTP_MSG_REPLY) { clib_warning ("unexpected msg type %d", msg.type); return -1; } svm_fifo_dequeue_drop_all (s->rx_fifo); if (msg.code == HTTP_STATUS_OK) hspm->http_response = format (0, "request success"); else hspm->http_response = format (0, "request failed"); hsp_session_disconnect_callback (s); vlib_process_signal_event_mt (hspm->vlib_main, hspm->cli_node_index, HSP_REPLY_RECEIVED, 0); return 0; } static int hsp_tx_callback (session_t *s) { hsp_main_t *hspm = &hsp_main; u32 to_send; int rv; to_send = vec_len (hspm->data) - hspm->data_offset; rv = svm_fifo_enqueue (s->tx_fifo, to_send, hspm->data + hspm->data_offset); if (rv <= 0) { svm_fifo_add_want_deq_ntf (s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF); return 0; } if (rv < to_send) { hspm->data_offset += rv; svm_fifo_add_want_deq_ntf (s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF); } if (svm_fifo_set_event (s->tx_fifo)) session_program_tx_io_evt (s->handle, SESSION_IO_EVT_TX); return 0; } static session_cb_vft_t hsp_session_cb_vft = { .session_connected_callback = hsp_session_connected_callback, .session_disconnect_callback = hsp_session_disconnect_callback, .session_transport_closed_callback = hsp_session_transport_closed_callback, .session_reset_callback = hsp_session_reset_callback, .builtin_app_rx_callback = hsp_rx_callback, .builtin_app_tx_callback = hsp_tx_callback, }; static clib_error_t * hsp_attach () { hsp_main_t *hspm = &hsp_main; vnet_app_attach_args_t _a, *a = &_a; u64 options[18]; int rv; clib_memset (a, 0, sizeof (*a)); clib_memset (options, 0, sizeof (options)); a->api_client_index = APP_INVALID_INDEX; a->name = format (0, "http_simple_post"); a->session_cb_vft = &hsp_session_cb_vft; a->options = options; a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; if ((rv = vnet_application_attach (a))) return clib_error_return (0, "attach returned: %U", format_session_error, rv); hspm->app_index = a->app_index; vec_free (a->name); hspm->attached = 1; return 0; } static int hsp_connect_rpc (void *rpc_args) { vnet_connect_args_t *a = rpc_args; int rv; rv = vnet_connect (a); if (rv) clib_warning (0, "connect returned: %U", format_session_error, rv); vec_free (a); return rv; } static void hsp_connect () { hsp_main_t *hspm = &hsp_main; vnet_connect_args_t *a = 0; hsp_worker_t *wrk; hsp_session_t *hsp_session; vec_validate (a, 0); clib_memset (a, 0, sizeof (a[0])); clib_memcpy (&a->sep_ext, &hspm->connect_sep, sizeof (hspm->connect_sep)); a->app_index = hspm->app_index; /* allocate http session on main thread */ wrk = hsp_worker_get (0); hsp_session = hsp_session_alloc (wrk); a->api_context = hsp_session->session_index; session_send_rpc_evt_to_thread_force (transport_cl_thread (), hsp_connect_rpc, a); } static clib_error_t * hsp_run (vlib_main_t *vm) { hsp_main_t *hspm = &hsp_main; vlib_thread_main_t *vtm = vlib_get_thread_main (); u32 num_threads; hsp_worker_t *wrk; uword event_type, *event_data = 0; clib_error_t *err; num_threads = 1 /* main thread */ + vtm->n_threads; vec_validate (hspm->wrk, num_threads); vec_foreach (wrk, hspm->wrk) wrk->thread_index = wrk - hspm->wrk; if ((err = hsp_attach ())) return clib_error_return (0, "http simple post attach: %U", format_clib_error, err); hsp_connect (); vlib_process_wait_for_event_or_clock (vm, 10); event_type = vlib_process_get_events (vm, &event_data); switch (event_type) { case ~0: err = clib_error_return (0, "error: timeout"); break; case HSP_CONNECT_FAILED: err = clib_error_return (0, "error: failed to connect"); break; case HSP_TRANSPORT_CLOSED: err = clib_error_return (0, "error: transport closed"); break; case HSP_REPLY_RECEIVED: vlib_cli_output (vm, "%v", hspm->http_response); break; default: err = clib_error_return (0, "error: unexpected event %d", event_type); break; } vec_free (event_data); return err; } static int hsp_detach () { hsp_main_t *hspm = &hsp_main; vnet_app_detach_args_t _da, *da = &_da; int rv; if (!hspm->attached) return 0; da->app_index = hspm->app_index; da->api_client_index = APP_INVALID_INDEX; rv = vnet_application_detach (da); hspm->attached = 0; hspm->app_index = APP_INVALID_INDEX; return rv; } static void hcc_worker_cleanup (hsp_worker_t *wrk) { pool_free (wrk->sessions); } static void hsp_cleanup () { hsp_main_t *hspm = &hsp_main; hsp_worker_t *wrk; vec_foreach (wrk, hspm->wrk) hcc_worker_cleanup (wrk); vec_free (hspm->uri); vec_free (hspm->target); vec_free (hspm->headers_buf); vec_free (hspm->data); vec_free (hspm->http_response); vec_free (hspm->wrk); } static clib_error_t * hsp_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd) { hsp_main_t *hspm = &hsp_main; clib_error_t *err = 0; unformat_input_t _line_input, *line_input = &_line_input; u8 *path = 0; u8 *file_data; int rv; if (hspm->attached) return clib_error_return (0, "failed: already running!"); hspm->use_ptr = 0; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return clib_error_return (0, "expected required arguments"); while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "uri %s", &hspm->uri)) ; else if (unformat (line_input, "data %v", &hspm->data)) hspm->is_file = 0; else if (unformat (line_input, "target %s", &hspm->target)) ; else if (unformat (line_input, "file %s", &path)) hspm->is_file = 1; else if (unformat (line_input, "use-ptr")) hspm->use_ptr = 1; else { err = clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); goto done; } } if (!hspm->uri) { err = clib_error_return (0, "URI not defined"); goto done; } if (!hspm->target) { err = clib_error_return (0, "target not defined"); goto done; } if (!hspm->data) { if (path) { err = clib_file_contents ((char *) path, &file_data); if (err) goto done; hspm->data = file_data; } else { err = clib_error_return (0, "data not defined"); goto done; } } if ((rv = parse_uri ((char *) hspm->uri, &hspm->connect_sep))) { err = clib_error_return (0, "URI parse error: %U", format_session_error, rv); goto done; } session_enable_disable_args_t args = { .is_en = 1, .rt_engine_type = RT_BACKEND_ENGINE_RULE_TABLE }; vlib_worker_thread_barrier_sync (vm); vnet_session_enable_disable (vm, &args); vlib_worker_thread_barrier_release (vm); hspm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index; err = hsp_run (vm); if ((rv = hsp_detach ())) { /* don't override last error */ if (!err) err = clib_error_return (0, "detach returned: %U", format_session_error, rv); else clib_warning ("warning: detach returned: %U", format_session_error, rv); } done: hsp_cleanup (); unformat_free (line_input); return err; } VLIB_CLI_COMMAND (hsp_command, static) = { .path = "http post", .short_help = "uri http://<ip-addr> target <origin-form> " "[data <form-urlencoded> | file <file-path>] [use-ptr]", .function = hsp_command_fn, .is_mp_safe = 1, }; static clib_error_t * hsp_main_init (vlib_main_t *vm) { hsp_main_t *hspm = &hsp_main; hspm->app_index = APP_INVALID_INDEX; hspm->vlib_main = vm; return 0; } VLIB_INIT_FUNCTION (hsp_main_init);