Line data Source code
1 : /*
2 : * Copyright (c) 2023 Cisco and/or its affiliates.
3 : * Licensed under the Apache License, Version 2.0 (the "License");
4 : * you may not use this file except in compliance with the License.
5 : * You may obtain a copy of the License at:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
9 : * Unless required by applicable law or agreed to in writing, software
10 : * distributed under the License is distributed on an "AS IS" BASIS,
11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : * See the License for the specific language governing permissions and
13 : * limitations under the License.
14 : */
15 : #include <vlib/vlib.h>
16 : #include <vnet/feature/feature.h>
17 : #include <vnet/classify/pcap_classify.h>
18 :
19 : typedef struct
20 : {
21 : u32 sw_if_index;
22 : } tracenode_trace_t;
23 :
24 : static u8 *
25 0 : format_tracenode_trace (u8 *s, va_list *args)
26 : {
27 0 : CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
28 0 : CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
29 0 : vnet_main_t *vnm = vnet_get_main ();
30 0 : tracenode_trace_t *t = va_arg (*args, tracenode_trace_t *);
31 :
32 0 : s = format (s, "Packet traced from interface %U added",
33 : format_vnet_sw_if_index_name, vnm, t->sw_if_index);
34 0 : return s;
35 : }
36 :
37 : static_always_inline u32
38 6 : tracenode_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
39 : vlib_frame_t *frame, int is_pcap)
40 : {
41 6 : vnet_main_t *vnm = vnet_get_main ();
42 6 : vnet_pcap_t *pp = &vnm->pcap;
43 6 : vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
44 6 : u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
45 6 : u32 *from = vlib_frame_vector_args (frame), *from0 = from;
46 6 : const u32 n_tot = frame->n_vectors;
47 6 : u32 n_left = n_tot;
48 :
49 6 : vlib_get_buffers (vm, from, b, n_tot);
50 :
51 408 : while (n_left > 0)
52 : {
53 : /* TODO: dual/quad loop */
54 :
55 : /* enqueue b0 to the current next frame */
56 402 : vnet_feature_next_u16 (next, b[0]);
57 :
58 : /* buffer already traced */
59 402 : if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
60 0 : goto skip;
61 :
62 402 : if (is_pcap && vnet_is_packet_pcaped (pp, b[0], ~0))
63 : {
64 0 : pcap_add_buffer (&pp->pcap_main, vm, from0[0],
65 : pp->max_bytes_per_pkt);
66 : }
67 402 : else if (!is_pcap && vlib_trace_buffer (vm, node, next[0], b[0],
68 : 1 /* follow_chain */))
69 : {
70 67 : tracenode_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof *tr);
71 67 : tr->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
72 : }
73 :
74 335 : skip:
75 402 : b++;
76 402 : from0++;
77 402 : next++;
78 402 : n_left--;
79 : }
80 :
81 6 : vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_tot);
82 6 : return n_tot;
83 : }
84 :
85 2306 : VLIB_NODE_FN (trace_filtering_node)
86 : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
87 : {
88 6 : return tracenode_inline (vm, node, frame, 0 /* is_pcap */);
89 : }
90 :
91 2300 : VLIB_NODE_FN (pcap_filtering_node)
92 : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
93 : {
94 0 : return tracenode_inline (vm, node, frame, 1 /* is_pcap */);
95 : }
96 :
97 28223 : VLIB_REGISTER_NODE (trace_filtering_node) = {
98 : .name = "trace-filtering",
99 : .vector_size = sizeof (u32),
100 : .type = VLIB_NODE_TYPE_INTERNAL,
101 : .format_trace = format_tracenode_trace,
102 : };
103 :
104 28223 : VLIB_REGISTER_NODE (pcap_filtering_node) = {
105 : .name = "pcap-filtering",
106 : .vector_size = sizeof (u32),
107 : .type = VLIB_NODE_TYPE_INTERNAL,
108 : .format_trace = format_tracenode_trace,
109 : };
110 :
111 9791 : VNET_FEATURE_INIT (trace_filtering4, static) = {
112 : .arc_name = "ip4-unicast",
113 : .node_name = "trace-filtering",
114 : .runs_after = VNET_FEATURES ("ip4-full-reassembly-feature",
115 : "ip4-sv-reassembly-feature"),
116 : };
117 :
118 9791 : VNET_FEATURE_INIT (trace_filtering6, static) = {
119 : .arc_name = "ip6-unicast",
120 : .node_name = "trace-filtering",
121 : .runs_after = VNET_FEATURES ("ip6-full-reassembly-feature",
122 : "ip6-sv-reassembly-feature"),
123 : };
124 :
125 9791 : VNET_FEATURE_INIT (pcap_filtering4, static) = {
126 : .arc_name = "ip4-unicast",
127 : .node_name = "pcap-filtering",
128 : .runs_after = VNET_FEATURES ("ip4-full-reassembly-feature",
129 : "ip4-sv-reassembly-feature"),
130 : };
131 :
132 9791 : VNET_FEATURE_INIT (pcap_filtering6, static) = {
133 : .arc_name = "ip6-unicast",
134 : .node_name = "pcap-filtering",
135 : .runs_after = VNET_FEATURES ("ip6-full-reassembly-feature",
136 : "ip6-sv-reassembly-feature"),
137 : };
138 :
139 : /*
140 : * fd.io coding-style-patch-verification: ON
141 : *
142 : * Local Variables:
143 : * eval: (c-set-style "gnu")
144 : * End:
145 : */
|