aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/dpo/interface_rx_dpo.c
blob: 82767e73fc74bc4093e5db744a21d19c5ac683e3 (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
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
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
/*
 * 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/dpo/interface_rx_dpo.h>
#include <vnet/fib/fib_node.h>
#include <vnet/l2/l2_input.h>

/*
 * The 'DB' of interface DPOs.
 * There is only one  per-interface per-protocol, so this is a per-interface
 * vector
 */
static index_t *interface_rx_dpo_db[DPO_PROTO_NUM];

static interface_rx_dpo_t *
interface_rx_dpo_alloc (void)
{
    interface_rx_dpo_t *ido;

    pool_get(interface_rx_dpo_pool, ido);

    return (ido);
}

static inline interface_rx_dpo_t *
interface_rx_dpo_get_from_dpo (const dpo_id_t *dpo)
{
    ASSERT(DPO_INTERFACE_RX == dpo->dpoi_type);

    return (interface_rx_dpo_get(dpo->dpoi_index));
}

static inline index_t
interface_rx_dpo_get_index (interface_rx_dpo_t *ido)
{
    return (ido - interface_rx_dpo_pool);
}

static void
interface_rx_dpo_lock (dpo_id_t *dpo)
{
    interface_rx_dpo_t *ido;

    ido = interface_rx_dpo_get_from_dpo(dpo);
    ido->ido_locks++;
}

static void
interface_rx_dpo_unlock (dpo_id_t *dpo)
{
    interface_rx_dpo_t *ido;

    ido = interface_rx_dpo_get_from_dpo(dpo);
    ido->ido_locks--;

    if (0 == ido->ido_locks)
    {
        interface_rx_dpo_db[ido->ido_proto][ido->ido_sw_if_index] =
            INDEX_INVALID;
        pool_put(interface_rx_dpo_pool, ido);
    }
}

/*
 * interface_rx_dpo_add_or_lock
 *
 * Add/create and lock a new or lock an existing for the interface DPO
 * on the interface and protocol given
 */
void
interface_rx_dpo_add_or_lock (dpo_proto_t proto,
                              u32 sw_if_index,
                              dpo_id_t *dpo)
{
    interface_rx_dpo_t *ido;

    vec_validate_init_empty(interface_rx_dpo_db[proto],
                            sw_if_index,
                            INDEX_INVALID);

    if (INDEX_INVALID == interface_rx_dpo_db[proto][sw_if_index])
    {
        ido = interface_rx_dpo_alloc();

        ido->ido_sw_if_index = sw_if_index;
        ido->ido_proto = proto;

        interface_rx_dpo_db[proto][sw_if_index] =
            interface_rx_dpo_get_index(ido);
    }
    else
    {
        ido = interface_rx_dpo_get(interface_rx_dpo_db[proto][sw_if_index]);
    }

    dpo_set(dpo, DPO_INTERFACE_RX, proto, interface_rx_dpo_get_index(ido));
}


static clib_error_t *
interface_rx_dpo_interface_state_change (vnet_main_t * vnm,
                                         u32 sw_if_index,
                                         u32 flags)
{
    /*
     */
    return (NULL);
}

VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(
    interface_rx_dpo_interface_state_change);

/**
 * @brief Registered callback for HW interface state changes
 */
static clib_error_t *
interface_rx_dpo_hw_interface_state_change (vnet_main_t * vnm,
                                            u32 hw_if_index,
                                            u32 flags)
{
    return (NULL);
}

VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
    interface_rx_dpo_hw_interface_state_change);

static clib_error_t *
interface_rx_dpo_interface_delete (vnet_main_t * vnm,
                                   u32 sw_if_index,
                                   u32 is_add)
{
    return (NULL);
}

VNET_SW_INTERFACE_ADD_DEL_FUNCTION(
    interface_rx_dpo_interface_delete);

u8*
format_interface_rx_dpo (u8* s, va_list *ap)
{
    index_t index = va_arg(*ap, index_t);
    CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
    vnet_main_t * vnm = vnet_get_main();
    interface_rx_dpo_t *ido = interface_rx_dpo_get(index);

    return (format(s, "%U-rx-dpo: %U",
                   format_vnet_sw_interface_name,
                   vnm,
                   vnet_get_sw_interface(vnm, ido->ido_sw_if_index),
                   format_dpo_proto, ido->ido_proto));
}

static void
interface_rx_dpo_mem_show (void)
{
    fib_show_memory_usage("Interface",
                          pool_elts(interface_rx_dpo_pool),
                          pool_len(interface_rx_dpo_pool),
                          sizeof(interface_rx_dpo_t));
}


const static dpo_vft_t interface_rx_dpo_vft = {
    .dv_lock = interface_rx_dpo_lock,
    .dv_unlock = interface_rx_dpo_unlock,
    .dv_format = format_interface_rx_dpo,
    .dv_mem_show = interface_rx_dpo_mem_show,
};

/**
 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
 *        object.
 *
 * this means that these graph nodes are ones from which a glean is the
 * parent object in the DPO-graph.
 */
const static char* const interface_rx_dpo_ip4_nodes[] =
{
    "interface-rx-dpo-ip4",
    NULL,
};
const static char* const interface_rx_dpo_ip6_nodes[] =
{
    "interface-rx-dpo-ip6",
    NULL,
};
const static char* const interface_rx_dpo_l2_nodes[] =
{
    "interface-rx-dpo-l2",
    NULL,
};

const static char* const * const interface_rx_dpo_nodes[DPO_PROTO_NUM] =
{
    [DPO_PROTO_IP4]  = interface_rx_dpo_ip4_nodes,
    [DPO_PROTO_IP6]  = interface_rx_dpo_ip6_nodes,
    [DPO_PROTO_ETHERNET]  = interface_rx_dpo_l2_nodes,
    [DPO_PROTO_MPLS] = NULL,
};

void
interface_rx_dpo_module_init (void)
{
    dpo_register(DPO_INTERFACE_RX,
                 &interface_rx_dpo_vft,
                 interface_rx_dpo_nodes);
}

/**
 * @brief Interface DPO trace data
 */
typedef struct interface_rx_dpo_trace_t_
{
    u32 sw_if_index;
} interface_rx_dpo_trace_t;

typedef enum interface_rx_dpo_next_t_
{
    INTERFACE_RX_DPO_DROP = 0,
    INTERFACE_RX_DPO_INPUT = 1,
} interface_rx_dpo_next_t;

always_inline uword
interface_rx_dpo_inline (vlib_main_t * vm,
                         vlib_node_runtime_t * node,
                         vlib_frame_t * from_frame,
			 u8 is_l2)
{
    u32 n_left_from, next_index, * from, * to_next;
    u32 thread_index = vm->thread_index;
    vnet_interface_main_t *im;

    im = &vnet_get_main ()->interface_main;
    from = vlib_frame_vector_args (from_frame);
    n_left_from = from_frame->n_vectors;

    next_index = INTERFACE_RX_DPO_INPUT;

    while (n_left_from > 0)
    {
        u32 n_left_to_next;

        vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);

        while (n_left_from >= 4 && n_left_to_next > 2)
        {
            const interface_rx_dpo_t *ido0, *ido1;
            u32 bi0, idoi0, bi1, idoi1;
            vlib_buffer_t *b0, *b1;

            bi0 = from[0];
            to_next[0] = bi0;
            bi1 = from[1];
            to_next[1] = bi1;
            from += 2;
            to_next += 2;
            n_left_from -= 2;
            n_left_to_next -= 2;

            b0 = vlib_get_buffer (vm, bi0);
            b1 = vlib_get_buffer (vm, bi1);

            idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
            idoi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
            ido0 = interface_rx_dpo_get(idoi0);
            ido1 = interface_rx_dpo_get(idoi1);

            vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
            vnet_buffer(b1)->sw_if_index[VLIB_RX] = ido1->ido_sw_if_index;

	    if (is_l2)
	    {
		vnet_update_l2_len (b0);
		vnet_update_l2_len (b1);
	    }

            vlib_increment_combined_counter (im->combined_sw_if_counters
                                             + VNET_INTERFACE_COUNTER_RX,
                                             thread_index,
                                             ido0->ido_sw_if_index,
                                             1,
                                             vlib_buffer_length_in_chain (vm, b0));
            vlib_increment_combined_counter (im->combined_sw_if_counters
                                             + VNET_INTERFACE_COUNTER_RX,
                                             thread_index,
                                             ido1->ido_sw_if_index,
                                             1,
                                             vlib_buffer_length_in_chain (vm, b1));

            if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
            {
                interface_rx_dpo_trace_t *tr0;

                tr0 = vlib_add_trace (vm, node, b0, sizeof (*tr0));
                tr0->sw_if_index = ido0->ido_sw_if_index;
            }
            if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
            {
                interface_rx_dpo_trace_t *tr1;

                tr1 = vlib_add_trace (vm, node, b1, sizeof (*tr1));
                tr1->sw_if_index = ido1->ido_sw_if_index;
            }
        }

        while (n_left_from > 0 && n_left_to_next > 0)
        {
            const interface_rx_dpo_t * ido0;
            vlib_buffer_t * b0;
            u32 bi0, idoi0;

            bi0 = from[0];
            to_next[0] = bi0;
            from += 1;
            to_next += 1;
            n_left_from -= 1;
            n_left_to_next -= 1;

            b0 = vlib_get_buffer (vm, bi0);

            idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
            ido0 = interface_rx_dpo_get(idoi0);

            /* Swap the RX interface of the packet to the one the
             * interface DPR represents */
            vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;

	    /* Update l2_len to make l2 tag rewrite work */
	    if (is_l2)
		vnet_update_l2_len (b0);

            /* Bump the interface's RX coutners */
            vlib_increment_combined_counter (im->combined_sw_if_counters
                                             + VNET_INTERFACE_COUNTER_RX,
                                             thread_index,
                                             ido0->ido_sw_if_index,
                                             1,
                                             vlib_buffer_length_in_chain (vm, b0));

            if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
            {
                interface_rx_dpo_trace_t *tr;

                tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
                tr->sw_if_index = ido0->ido_sw_if_index;
            }
        }
        vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }
    return from_frame->n_vectors;
}

static u8 *
format_interface_rx_dpo_trace (u8 * s, va_list * args)
{
    CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
    CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
    interface_rx_dpo_trace_t * t = va_arg (*args, interface_rx_dpo_trace_t *);
    u32 indent = format_get_indent (s);
    s = format (s, "%U sw_if_index:%d",
                format_white_space, indent,
                t->sw_if_index);
    return s;
}

static uword
interface_rx_dpo_ip4 (vlib_main_t * vm,
                      vlib_node_runtime_t * node,
                      vlib_frame_t * from_frame)
{
    return (interface_rx_dpo_inline(vm, node, from_frame, 0));
}

static uword
interface_rx_dpo_ip6 (vlib_main_t * vm,
                      vlib_node_runtime_t * node,
                      vlib_frame_t * from_frame)
{
    return (interface_rx_dpo_inline(vm, node, from_frame, 0));
}

static uword
interface_rx_dpo_l2 (vlib_main_t * vm,
                     vlib_node_runtime_t * node,
                     vlib_frame_t * from_frame)
{
    return (interface_rx_dpo_inline(vm, node, from_frame, 1));
}

VLIB_REGISTER_NODE (interface_rx_dpo_ip4_node) = {
    .function = interface_rx_dpo_ip4,
    .name = "interface-rx-dpo-ip4",
    .vector_size = sizeof (u32),
    .format_trace = format_interface_rx_dpo_trace,

    .n_next_nodes = 2,
    .next_nodes = {
        [INTERFACE_RX_DPO_DROP] = "ip4-drop",
        [INTERFACE_RX_DPO_INPUT] = "ip4-input",
    },
};

VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_ip4_node,
                              interface_rx_dpo_ip4)

VLIB_REGISTER_NODE (interface_rx_dpo_ip6_node) = {
    .function = interface_rx_dpo_ip6,
    .name = "interface-rx-dpo-ip6",
    .vector_size = sizeof (u32),
    .format_trace = format_interface_rx_dpo_trace,

    .n_next_nodes = 2,
    .next_nodes = {
        [INTERFACE_RX_DPO_DROP] = "ip6-drop",
        [INTERFACE_RX_DPO_INPUT] = "ip6-input",
    },
};

VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_ip6_node,
                              interface_rx_dpo_ip6)

VLIB_REGISTER_NODE (interface_rx_dpo_l2_node) = {
    .function = interface_rx_dpo_l2,
    .name = "interface-rx-dpo-l2",
    .vector_size = sizeof (u32),
    .format_trace = format_interface_rx_dpo_trace,

    .n_next_nodes = 2,
    .next_nodes = {
        [INTERFACE_RX_DPO_DROP] = "error-drop",
        [INTERFACE_RX_DPO_INPUT] = "l2-input",
    },
};

VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_l2_node,
                              interface_rx_dpo_l2)
an class="n">ip = (ip4_header_t *) & tp->ip4; udp = (udp_header_t *) (ip + 1); h = (ipfix_message_header_t *) (udp + 1); s = (ipfix_set_header_t *) (h + 1); t = (ipfix_template_header_t *) (s + 1); first_field = f = (ipfix_field_specifier_t *) (t + 1); ip->ip_version_and_header_length = 0x45; ip->ttl = 254; ip->protocol = IP_PROTOCOL_UDP; ip->src_address.as_u32 = src_address->as_u32; ip->dst_address.as_u32 = collector_address->as_u32; udp->src_port = clib_host_to_net_u16 (stream->src_port); udp->dst_port = clib_host_to_net_u16 (collector_port); udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip)); /* FIXUP: message header export_time */ /* FIXUP: message header sequence_number */ h->domain_id = clib_host_to_net_u32 (stream->domain_id); /* Add TLVs to the template */ f = flowprobe_template_common_fields (f); if (flags & FLOW_RECORD_L2) f = flowprobe_template_l2_fields (f); if (collect_ip4) f = flowprobe_template_ip4_fields (f); if (collect_ip6) f = flowprobe_template_ip6_fields (f); if (flags & FLOW_RECORD_L4) f = flowprobe_template_l4_fields (f); /* Back to the template packet... */ ip = (ip4_header_t *) & tp->ip4; udp = (udp_header_t *) (ip + 1); ASSERT (f - first_field); /* Field count in this template */ t->id_count = ipfix_id_count (fr->template_id, f - first_field); fm->template_size[flags] = (u8 *) f - (u8 *) s; /* set length in octets */ s->set_id_length = ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s); /* message length in octets */ h->version_length = version_length ((u8 *) f - (u8 *) h); ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip); ip->checksum = ip4_header_checksum (ip); return rewrite; } static u8 * flowprobe_template_rewrite_ip6 (flow_report_main_t * frm, flow_report_t * fr, ip4_address_t * collector_address, ip4_address_t * src_address, u16 collector_port) { return flowprobe_template_rewrite_inline (frm, fr, collector_address, src_address, collector_port, FLOW_VARIANT_IP6); } static u8 * flowprobe_template_rewrite_ip4 (flow_report_main_t * frm, flow_report_t * fr, ip4_address_t * collector_address, ip4_address_t * src_address, u16 collector_port) { return flowprobe_template_rewrite_inline (frm, fr, collector_address, src_address, collector_port, FLOW_VARIANT_IP4); } static u8 * flowprobe_template_rewrite_l2 (flow_report_main_t * frm, flow_report_t * fr, ip4_address_t * collector_address, ip4_address_t * src_address, u16 collector_port) { return flowprobe_template_rewrite_inline (frm, fr, collector_address, src_address, collector_port, FLOW_VARIANT_L2); } static u8 * flowprobe_template_rewrite_l2_ip4 (flow_report_main_t * frm, flow_report_t * fr, ip4_address_t * collector_address, ip4_address_t * src_address, u16 collector_port) { return flowprobe_template_rewrite_inline (frm, fr, collector_address, src_address, collector_port, FLOW_VARIANT_L2_IP4); } static u8 * flowprobe_template_rewrite_l2_ip6 (flow_report_main_t * frm, flow_report_t * fr, ip4_address_t * collector_address, ip4_address_t * src_address, u16 collector_port) { return flowprobe_template_rewrite_inline (frm, fr, collector_address, src_address, collector_port, FLOW_VARIANT_L2_IP6); } /** * @brief Flush accumulated data * @param frm flow_report_main_t * * @param fr flow_report_t * * @param f vlib_frame_t * * * <em>Notes:</em> * This function must simply return the incoming frame, or no template packets * will be sent. */ vlib_frame_t * flowprobe_data_callback_ip4 (flow_report_main_t * frm, flow_report_t * fr, vlib_frame_t * f, u32 * to_next, u32 node_index) { flowprobe_flush_callback_ip4 (); return f; } vlib_frame_t * flowprobe_data_callback_ip6 (flow_report_main_t * frm, flow_report_t * fr, vlib_frame_t * f, u32 * to_next, u32 node_index) { flowprobe_flush_callback_ip6 (); return f; } vlib_frame_t * flowprobe_data_callback_l2 (flow_report_main_t * frm, flow_report_t * fr, vlib_frame_t * f, u32 * to_next, u32 node_index) { flowprobe_flush_callback_l2 (); return f; } static int flowprobe_template_add_del (u32 domain_id, u16 src_port, flowprobe_record_t flags, vnet_flow_data_callback_t * flow_data_callback, vnet_flow_rewrite_callback_t * rewrite_callback, bool is_add, u16 * template_id) { flow_report_main_t *frm = &flow_report_main; vnet_flow_report_add_del_args_t a = { .rewrite_callback = rewrite_callback, .flow_data_callback = flow_data_callback, .is_add = is_add, .domain_id = domain_id, .src_port = src_port, .opaque.as_uword = flags, }; return vnet_flow_report_add_del (frm, &a, template_id); } static void flowprobe_expired_timer_callback (u32 * expired_timers) { vlib_main_t *vm = vlib_get_main (); flowprobe_main_t *fm = &flowprobe_main; u32 my_cpu_number = vm->thread_index; int i; u32 poolindex; for (i = 0; i < vec_len (expired_timers); i++) { poolindex = expired_timers[i] & 0x7FFFFFFF; vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex); } } static clib_error_t * flowprobe_create_state_tables (u32 active_timer) { flowprobe_main_t *fm = &flowprobe_main; vlib_thread_main_t *tm = &vlib_thread_main; vlib_main_t *vm = vlib_get_main (); clib_error_t *error = 0; u32 num_threads; int i; /* Decide how many worker threads we have */ num_threads = 1 /* main thread */ + tm->n_threads; /* Hash table per worker */ fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE; /* Init per worker flow state and timer wheels */ if (active_timer) { vec_validate (fm->timers_per_worker, num_threads - 1); vec_validate (fm->expired_passive_per_worker, num_threads - 1); vec_validate (fm->hash_per_worker, num_threads - 1); vec_validate (fm->pool_per_worker, num_threads - 1); for (i = 0; i < num_threads; i++) { int j; pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len); vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len); for (j = 0; j < (1 << fm->ht_log2len); j++) fm->hash_per_worker[i][j] = ~0; fm->timers_per_worker[i] = clib_mem_alloc (sizeof (TWT (tw_timer_wheel))); tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i], flowprobe_expired_timer_callback, 1.0, 1024); } fm->disabled = true; } else { f64 now = vlib_time_now (vm); vec_validate (fm->stateless_entry, num_threads - 1); for (i = 0; i < num_threads; i++) fm->stateless_entry[i].last_exported = now; fm->disabled = false; } fm->initialized = true; return error; } static int validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index, u8 which) { vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0); if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0) return -1; else if (fm->flow_per_interface[sw_if_index] != which) return 0; else return 1; } /** * @brief configure / deconfigure the IPFIX flow-per-packet * @param fm flowprobe_main_t * fm * @param sw_if_index u32 the desired interface * @param is_add int 1 to enable the feature, 0 to disable it * @returns 0 if successful, non-zero otherwise */ static int flowprobe_tx_interface_add_del_feature (flowprobe_main_t * fm, u32 sw_if_index, u8 which, int is_add) { vlib_main_t *vm = vlib_get_main (); int rv = 0; u16 template_id = 0; flowprobe_record_t flags = fm->record; fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0; fm->template_per_flow[which] += (is_add) ? 1 : -1; if (is_add && fm->template_per_flow[which] > 1) template_id = fm->template_reports[flags]; if ((is_add && fm->template_per_flow[which] == 1) || (!is_add && fm->template_per_flow[which] == 0)) { if (which == FLOW_VARIANT_L2) { if (fm->record & FLOW_RECORD_L2) { rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_l2, flowprobe_template_rewrite_l2, is_add, &template_id); } if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4) { rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_l2, flowprobe_template_rewrite_l2_ip4, is_add, &template_id); fm->template_reports[flags | FLOW_RECORD_L2_IP4] = (is_add) ? template_id : 0; rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_l2, flowprobe_template_rewrite_l2_ip6, is_add, &template_id); fm->template_reports[flags | FLOW_RECORD_L2_IP6] = (is_add) ? template_id : 0; /* Special case L2 */ fm->context[FLOW_VARIANT_L2_IP4].flags = flags | FLOW_RECORD_L2_IP4; fm->context[FLOW_VARIANT_L2_IP6].flags = flags | FLOW_RECORD_L2_IP6; fm->template_reports[flags] = template_id; } } else if (which == FLOW_VARIANT_IP4) rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_ip4, flowprobe_template_rewrite_ip4, is_add, &template_id); else if (which == FLOW_VARIANT_IP6) rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_ip6, flowprobe_template_rewrite_ip6, is_add, &template_id); } if (rv && rv != VNET_API_ERROR_VALUE_EXIST) { clib_warning ("vnet_flow_report_add_del returned %d", rv); return -1; } if (which != (u8) ~ 0) { fm->context[which].flags = fm->record; fm->template_reports[flags] = (is_add) ? template_id : 0; } if (which == FLOW_VARIANT_IP4) vnet_feature_enable_disable ("ip4-output", "flowprobe-ip4", sw_if_index, is_add, 0, 0); else if (which == FLOW_VARIANT_IP6) vnet_feature_enable_disable ("ip6-output", "flowprobe-ip6", sw_if_index, is_add, 0, 0); else if (which == FLOW_VARIANT_L2) vnet_feature_enable_disable ("interface-output", "flowprobe-l2", sw_if_index, is_add, 0, 0); /* Stateful flow collection */ if (is_add && !fm->initialized) { flowprobe_create_state_tables (fm->active_timer); if (fm->active_timer) vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0); } return 0; } /** * @brief API message handler * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message */ void vl_api_flowprobe_tx_interface_add_del_t_handler (vl_api_flowprobe_tx_interface_add_del_t * mp) { flowprobe_main_t *fm = &flowprobe_main; vl_api_flowprobe_tx_interface_add_del_reply_t *rmp; u32 sw_if_index = ntohl (mp->sw_if_index); int rv = 0; VALIDATE_SW_IF_INDEX (mp); if (mp->which != FLOW_VARIANT_IP4 && mp->which != FLOW_VARIANT_L2 && mp->which != FLOW_VARIANT_IP6) { rv = VNET_API_ERROR_UNIMPLEMENTED; goto out; } if (fm->record == 0) { clib_warning ("Please specify flowprobe params record first..."); rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE; goto out; } rv = validate_feature_on_interface (fm, sw_if_index, mp->which); if ((rv == 1 && mp->is_add == 1) || rv == 0) { rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE; goto out; } rv = flowprobe_tx_interface_add_del_feature (fm, sw_if_index, mp->which, mp->is_add); out: BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY); } /** * @brief API message custom-dump function * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message * @param handle void * print function handle * @returns u8 * output string */ static void *vl_api_flowprobe_tx_interface_add_del_t_print (vl_api_flowprobe_tx_interface_add_del_t * mp, void *handle) { u8 *s; s = format (0, "SCRIPT: flowprobe_tx_interface_add_del "); s = format (s, "sw_if_index %d is_add %d which %d ", clib_host_to_net_u32 (mp->sw_if_index), (int) mp->is_add, (int) mp->which); FINISH; } #define vec_neg_search(v,E) \ ({ \ word _v(i) = 0; \ while (_v(i) < vec_len(v) && v[_v(i)] == E) \ { \ _v(i)++; \ } \ if (_v(i) == vec_len(v)) \ _v(i) = ~0; \ _v(i); \ }) static int flowprobe_params (flowprobe_main_t * fm, u8 record_l2, u8 record_l3, u8 record_l4, u32 active_timer, u32 passive_timer) { flowprobe_record_t flags = 0; if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0) return ~0; if (record_l2) flags |= FLOW_RECORD_L2; if (record_l3) flags |= FLOW_RECORD_L3; if (record_l4) flags |= FLOW_RECORD_L4; fm->record = flags; /* * Timers: ~0 is default, 0 is off */ fm->active_timer = (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer); fm->passive_timer = (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer); return 0; } void vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp) { flowprobe_main_t *fm = &flowprobe_main; vl_api_flowprobe_params_reply_t *rmp; int rv = 0; rv = flowprobe_params (fm, mp->record_l2, mp->record_l3, mp->record_l4, clib_net_to_host_u32 (mp->active_timer), clib_net_to_host_u32 (mp->passive_timer)); REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY); } /* List of message types that this plugin understands */ #define foreach_flowprobe_plugin_api_msg \ _(FLOWPROBE_TX_INTERFACE_ADD_DEL, flowprobe_tx_interface_add_del) \ _(FLOWPROBE_PARAMS, flowprobe_params) /* *INDENT-OFF* */ VLIB_PLUGIN_REGISTER () = { .version = VPP_BUILD_VER, .description = "Flow per Packet", }; /* *INDENT-ON* */ u8 * format_flowprobe_entry (u8 * s, va_list * args) { flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *); s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index); s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac, format_ethernet_address, &e->key.dst_mac); s = format (s, " %U -> %U", format_ip46_address, &e->key.src_address, IP46_TYPE_ANY, format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY); s = format (s, " %d", e->key.protocol); s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port), clib_net_to_host_u16 (e->key.dst_port)); return s; } static clib_error_t * flowprobe_show_table_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cm) { flowprobe_main_t *fm = &flowprobe_main; int i; flowprobe_entry_t *e; vlib_cli_output (vm, "Dumping IPFIX table"); for (i = 0; i < vec_len (fm->pool_per_worker); i++) { /* *INDENT-OFF* */ pool_foreach (e, fm->pool_per_worker[i], ( { vlib_cli_output (vm, "%U", format_flowprobe_entry, e); })); /* *INDENT-ON* */ } return 0; } static clib_error_t * flowprobe_show_stats_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cm) { flowprobe_main_t *fm = &flowprobe_main; int i; vlib_cli_output (vm, "IPFIX table statistics"); vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t)); vlib_cli_output (vm, "Flow pool size per thread: %d\n", 0x1 << FLOWPROBE_LOG2_HASHSIZE); for (i = 0; i < vec_len (fm->pool_per_worker); i++) vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i, (100 * pool_elts (fm->pool_per_worker[i])) / (0x1 << FLOWPROBE_LOG2_HASHSIZE)); return 0; } static clib_error_t * flowprobe_tx_interface_add_del_feature_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { flowprobe_main_t *fm = &flowprobe_main; u32 sw_if_index = ~0; int is_add = 1; u8 which = FLOW_VARIANT_IP4; int rv; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "disable")) is_add = 0; else if (unformat (input, "%U", unformat_vnet_sw_interface, fm->vnet_main, &sw_if_index)); else if (unformat (input, "ip4")) which = FLOW_VARIANT_IP4; else if (unformat (input, "ip6")) which = FLOW_VARIANT_IP6; else if (unformat (input, "l2")) which = FLOW_VARIANT_L2; else break; } if (fm->record == 0) return clib_error_return (0, "Please specify flowprobe params record first..."); if (sw_if_index == ~0) return clib_error_return (0, "Please specify an interface..."); rv = validate_feature_on_interface (fm, sw_if_index, which); if (rv == 1) { if (is_add) return clib_error_return (0, "Datapath is already enabled for given interface..."); } else if (rv == 0) return clib_error_return (0, "Interface has enable different datapath ..."); rv = flowprobe_tx_interface_add_del_feature (fm, sw_if_index, which, is_add); switch (rv) { case 0: break; case VNET_API_ERROR_INVALID_SW_IF_INDEX: return clib_error_return (0, "Invalid interface, only works on physical ports"); break; case VNET_API_ERROR_UNIMPLEMENTED: return clib_error_return (0, "ip6 not supported"); break; default: return clib_error_return (0, "flowprobe_enable_disable returned %d", rv); } return 0; } static clib_error_t * flowprobe_params_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { flowprobe_main_t *fm = &flowprobe_main; bool record_l2 = false, record_l3 = false, record_l4 = false; u32 active_timer = ~0; u32 passive_timer = ~0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "active %d", &active_timer)) ; else if (unformat (input, "passive %d", &passive_timer)) ; else if (unformat (input, "record")) while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "l2")) record_l2 = true; else if (unformat (input, "l3")) record_l3 = true; else if (unformat (input, "l4")) record_l4 = true; else break; } else break; } if (passive_timer > 0 && active_timer > passive_timer) return clib_error_return (0, "Passive timer has to be greater than active one..."); if (flowprobe_params (fm, record_l2, record_l3, record_l4, active_timer, passive_timer)) return clib_error_return (0, "Couldn't change flowperpacket params when feature is enabled on some interface ..."); return 0; } /*? * '<em>flowprobe feature add-del</em>' commands to enable/disable * per-packet IPFIX flow record generation on an interface * * @cliexpar * @parblock * To enable per-packet IPFIX flow-record generation on an interface: * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0} * * To disable per-packet IPFIX flow-record generation on an interface: * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable} * @cliexend * @endparblock ?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = { .path = "flowprobe feature add-del", .short_help = "flowprobe feature add-del <interface-name> <l2|ip4|ip6> disable", .function = flowprobe_tx_interface_add_del_feature_command_fn, }; VLIB_CLI_COMMAND (flowprobe_params_command, static) = { .path = "flowprobe params", .short_help = "flowprobe params record <[l2] [l3] [l4]> [active <timer> passive <timer>]", .function = flowprobe_params_command_fn, }; VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = { .path = "show flowprobe table", .short_help = "show flowprobe table", .function = flowprobe_show_table_fn, }; VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = { .path = "show flowprobe statistics", .short_help = "show flowprobe statistics", .function = flowprobe_show_stats_fn, }; /* *INDENT-ON* */ /** * @brief Set up the API message handling tables * @param vm vlib_main_t * vlib main data structure pointer * @returns 0 to indicate all is well */ static clib_error_t * flowprobe_plugin_api_hookup (vlib_main_t * vm) { flowprobe_main_t *fm = &flowprobe_main; #define _(N,n) \ vl_msg_api_set_handlers((VL_API_##N + fm->msg_id_base), \ #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), 1); foreach_flowprobe_plugin_api_msg; #undef _ return 0; } #define vl_msg_name_crc_list #include <flowprobe/flowprobe_all_api_h.h> #undef vl_msg_name_crc_list static void setup_message_id_table (flowprobe_main_t * fm, api_main_t * am) { #define _(id,n,crc) \ vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + fm->msg_id_base); foreach_vl_msg_name_crc_flowprobe; #undef _ } /* * Main-core process, sending an interrupt to the per worker input * process that spins the per worker timer wheel. */ static uword timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) { uword *event_data = 0; vlib_main_t **worker_vms = 0, *worker_vm; flowprobe_main_t *fm = &flowprobe_main; /* Wait for Godot... */ vlib_process_wait_for_event_or_clock (vm, 1e9); uword event_type = vlib_process_get_events (vm, &event_data); if (event_type != 1) clib_warning ("bogus kickoff event received, %d", event_type); vec_reset_length (event_data); int i; if (vec_len (vlib_mains) == 0) vec_add1 (worker_vms, vm); else { for (i = 0; i < vec_len (vlib_mains); i++) { worker_vm = vlib_mains[i]; if (worker_vm) vec_add1 (worker_vms, worker_vm); } } f64 sleep_duration = 0.1; while (1) { /* Send an interrupt to each timer input node */ sleep_duration = 0.1; for (i = 0; i < vec_len (worker_vms); i++) { worker_vm = worker_vms[i]; if (worker_vm) { vlib_node_set_interrupt_pending (worker_vm, flowprobe_walker_node.index); sleep_duration = (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1; } } vlib_process_suspend (vm, sleep_duration); } return 0; /* or not */ } /* *INDENT-OFF* */ VLIB_REGISTER_NODE (flowprobe_timer_node,static) = { .function = timer_process, .name = "flowprobe-timer-process", .type = VLIB_NODE_TYPE_PROCESS, }; /* *INDENT-ON* */ /** * @brief Set up the API message handling tables * @param vm vlib_main_t * vlib main data structure pointer * @returns 0 to indicate all is well, or a clib_error_t */ static clib_error_t * flowprobe_init (vlib_main_t * vm) { flowprobe_main_t *fm = &flowprobe_main; vlib_thread_main_t *tm = &vlib_thread_main; clib_error_t *error = 0; u8 *name; u32 num_threads; int i; fm->vnet_main = vnet_get_main (); /* Construct the API name */ name = format (0, "flowprobe_%08x%c", api_version, 0); /* Ask for a correctly-sized block of API message decode slots */ fm->msg_id_base = vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE); /* Hook up message handlers */ error = flowprobe_plugin_api_hookup (vm); /* Add our API messages to the global name_crc hash table */ setup_message_id_table (fm, &api_main); vec_free (name); /* Set up time reference pair */ fm->vlib_time_0 = vlib_time_now (vm); fm->nanosecond_time_0 = unix_time_now_nsec (); memset (fm->template_reports, 0, sizeof (fm->template_reports)); memset (fm->template_size, 0, sizeof (fm->template_size)); memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow)); /* Decide how many worker threads we have */ num_threads = 1 /* main thread */ + tm->n_threads; /* Allocate per worker thread vectors per flavour */ for (i = 0; i < FLOW_N_VARIANTS; i++) { vec_validate (fm->context[i].buffers_per_worker, num_threads - 1); vec_validate (fm->context[i].frames_per_worker, num_threads - 1); vec_validate (fm->context[i].next_record_offset_per_worker, num_threads - 1); } fm->active_timer = FLOWPROBE_TIMER_ACTIVE; fm->passive_timer = FLOWPROBE_TIMER_PASSIVE; return error; } VLIB_INIT_FUNCTION (flowprobe_init); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */