blob: e079816f62c27b8048ac3c3b49832a0f2228c770 [file] [log] [blame]
Benoît Ganne30a81952021-02-26 13:47:41 +01001/*
2 * pcap_classify.h - Use the classifier to decide if a packet is captured
3 *
4 * Copyright (c) 2021 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include <vlib/vlib.h>
18#include <vnet/vnet.h>
19#include <vnet/classify/vnet_classify.h>
20#include <vnet/classify/trace_classify.h>
21
22/** @file pcap_classify.h
23 * Use the vpp classifier to decide whether to capture packets
24 */
25
26/** @brief vnet_is_packet_pcaped
27 * @param vlib_buffer_t *b - packet to capture
28 * @return 0 => no capture, 1 => capture
29 */
30
31static_always_inline int
32vnet_is_packet_pcaped (vnet_pcap_t *pp, vlib_buffer_t *b, u32 sw_if_index)
33{
34 const u32 pcap_sw_if_index = pp->pcap_sw_if_index;
35 const u32 filter_classify_table_index = pp->filter_classify_table_index;
Benoît Ganne5cfe4522021-03-03 17:37:25 +010036 const vlib_error_t pcap_error_index = pp->pcap_error_index;
Benoît Ganne30a81952021-02-26 13:47:41 +010037
38 if (pcap_sw_if_index != 0)
39 {
40 if (~0 == sw_if_index)
41 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
42 if (pcap_sw_if_index != sw_if_index)
43 return 0; /* wrong interface, skip */
44 }
45
Benoît Ganne5cfe4522021-03-03 17:37:25 +010046 if (pcap_error_index != (vlib_error_t) ~0 && pcap_error_index != b->error)
47 return 0; /* wrong error */
48
Benoît Ganne30a81952021-02-26 13:47:41 +010049 if (filter_classify_table_index != ~0 &&
50 vnet_is_packet_traced_inline (b, filter_classify_table_index,
51 0 /* full classify */) != 1)
52 return 0; /* not matching the filter, skip */
53
54 return 1; /* success */
55}
56
57/*
58 * fd.io coding-style-patch-verification: ON
59 *
60 * Local Variables:
61 * eval: (c-set-style "gnu")
62 * End:
63 */