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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vlib/vlib.h>
16 #include <vnet/feature/feature.h>
17 #include <vnet/classify/pcap_classify.h>
25 format_tracenode_trace (u8 *s, va_list *args)
27 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
28 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
29 vnet_main_t *vnm = vnet_get_main ();
30 tracenode_trace_t *t = va_arg (*args, tracenode_trace_t *);
32 s = format (s, "Packet traced from interface %U added",
33 format_vnet_sw_if_index_name, vnm, t->sw_if_index);
37 static_always_inline u32
38 tracenode_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
39 vlib_frame_t *frame, int is_pcap)
41 vnet_main_t *vnm = vnet_get_main ();
42 vnet_pcap_t *pp = &vnm->pcap;
43 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
44 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
45 u32 *from = vlib_frame_vector_args (frame), *from0 = from;
46 const u32 n_tot = frame->n_vectors;
49 vlib_get_buffers (vm, from, b, n_tot);
53 /* TODO: dual/quad loop */
55 /* enqueue b0 to the current next frame */
56 vnet_feature_next_u16 (next, b[0]);
58 /* buffer already traced */
59 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
62 if (is_pcap && vnet_is_packet_pcaped (pp, b[0], ~0))
64 pcap_add_buffer (&pp->pcap_main, vm, from0[0],
65 pp->max_bytes_per_pkt);
67 else if (!is_pcap && vlib_trace_buffer (vm, node, next[0], b[0],
68 1 /* follow_chain */))
70 tracenode_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof *tr);
71 tr->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
81 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_tot);
85 VLIB_NODE_FN (trace_filtering_node)
86 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
88 return tracenode_inline (vm, node, frame, 0 /* is_pcap */);
91 VLIB_NODE_FN (pcap_filtering_node)
92 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
94 return tracenode_inline (vm, node, frame, 1 /* is_pcap */);
97 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,
104 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,
111 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"),
118 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"),
125 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"),
132 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"),
140 * fd.io coding-style-patch-verification: ON
143 * eval: (c-set-style "gnu")