|
| 1 | +From 405caa1424358032574230ec5479e64834869298 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Alfredo Cardigliano <cardigliano@ntop.org> |
| 3 | +Date: Thu, 13 Apr 2023 13:03:28 +0200 |
| 4 | +Subject: [PATCH] Implement probabilistic sampling |
| 5 | + |
| 6 | +--- |
| 7 | + kernel/linux/pf_ring.h | 4 +++- |
| 8 | + kernel/pf_ring.c | 34 ++++++++++++++++++++++++---------- |
| 9 | + 2 files changed, 27 insertions(+), 11 deletions(-) |
| 10 | + |
| 11 | +--- a/kernel/linux/pf_ring.h |
| 12 | ++++ b/kernel/linux/pf_ring.h |
| 13 | +@@ -1310,7 +1310,9 @@ struct pf_ring_socket { |
| 14 | + u_char *ring_slots; /* Points to ring_memory+sizeof(FlowSlotInfo) */ |
| 15 | + |
| 16 | + /* Packet Sampling */ |
| 17 | +- u_int32_t pktToSample, sample_rate; |
| 18 | ++ u_int32_t sample_rate; |
| 19 | ++ u_int32_t pkts_to_sample; |
| 20 | ++ u_int32_t sample_rnd_shift; |
| 21 | + |
| 22 | + /* Virtual Filtering Device */ |
| 23 | + virtual_filtering_device_element *v_filtering_dev; |
| 24 | +--- a/kernel/pf_ring.c |
| 25 | ++++ b/kernel/pf_ring.c |
| 26 | +@@ -3695,6 +3695,26 @@ int bpf_filter_skb(struct sk_buff *skb, |
| 27 | + |
| 28 | + /* ********************************** */ |
| 29 | + |
| 30 | ++int sample_packet(struct pf_ring_socket *pfr) { |
| 31 | ++ if(pfr->pkts_to_sample <= 1) { |
| 32 | ++ u_int32_t rnd = 0; |
| 33 | ++ |
| 34 | ++ get_random_bytes(&rnd, sizeof(u_int32_t)); |
| 35 | ++ rnd = rnd % pfr->sample_rate; |
| 36 | ++ |
| 37 | ++ pfr->pkts_to_sample = pfr->sample_rate - pfr->sample_rnd_shift + rnd; |
| 38 | ++ |
| 39 | ++ pfr->sample_rnd_shift = rnd; |
| 40 | ++ |
| 41 | ++ return 1; /* Pass packet */ |
| 42 | ++ } else { |
| 43 | ++ pfr->pkts_to_sample--; |
| 44 | ++ return 0; /* Discard packet */ |
| 45 | ++ } |
| 46 | ++} |
| 47 | ++ |
| 48 | ++/* ********************************** */ |
| 49 | ++ |
| 50 | + u_int32_t default_rehash_rss_func(struct sk_buff *skb, struct pfring_pkthdr *hdr) |
| 51 | + { |
| 52 | + return hash_pkt_header(hdr, 0); |
| 53 | +@@ -3805,12 +3825,9 @@ static int add_skb_to_ring(struct sk_buf |
| 54 | + if(pfr->sample_rate > 1) { |
| 55 | + spin_lock_bh(&pfr->ring_index_lock); |
| 56 | + |
| 57 | +- if(pfr->pktToSample <= 1) { |
| 58 | +- pfr->pktToSample = pfr->sample_rate; |
| 59 | +- } else { |
| 60 | ++ if(!sample_packet(pfr)) { |
| 61 | ++ /* Discard packet */ |
| 62 | + pfr->slots_info->tot_pkts++; |
| 63 | +- pfr->pktToSample--; |
| 64 | +- |
| 65 | + spin_unlock_bh(&pfr->ring_index_lock); |
| 66 | + atomic_dec(&pfr->num_ring_users); |
| 67 | + return(-1); |
| 68 | +@@ -4161,11 +4178,8 @@ int pf_ring_skb_ring_handler(struct sk_b |
| 69 | + |
| 70 | + if(pfr->sample_rate > 1) { |
| 71 | + spin_lock_bh(&pfr->ring_index_lock); |
| 72 | +- if(pfr->pktToSample <= 1) { |
| 73 | +- pfr->pktToSample = pfr->sample_rate; |
| 74 | +- } else { |
| 75 | ++ if (!sample_packet(pfr)) { |
| 76 | + pfr->slots_info->tot_pkts++; |
| 77 | +- pfr->pktToSample--; |
| 78 | + rc = 0; |
| 79 | + } |
| 80 | + spin_unlock_bh(&pfr->ring_index_lock); |
| 81 | +@@ -7957,7 +7971,7 @@ static int ring_getsockopt(struct socket |
| 82 | + if(copy_to_user(optval, lowest_if_mac, ETH_ALEN)) |
| 83 | + return(-EFAULT); |
| 84 | + } else { |
| 85 | +- char *dev_addr = pfr->ring_dev->dev->dev_addr; |
| 86 | ++ const char *dev_addr = pfr->ring_dev->dev->dev_addr; |
| 87 | + |
| 88 | + if (dev_addr == NULL) /* e.g. 'any' device */ |
| 89 | + dev_addr = empty_mac; |
0 commit comments