diff --git a/README.md b/README.md index 2d87824..c5e3290 100644 --- a/README.md +++ b/README.md @@ -435,6 +435,12 @@ cat /sys/module/kyoutubeUnblock/parameters/parameters and check all the parameters configured. +You can check up the statistics of youtubeUnblock with + +```sh +sudo cat /proc/kyoutubeUnblock +``` + ### Building kernel module #### Building on host system diff --git a/src/args.c b/src/args.c index 6ada186..b8b66d4 100644 --- a/src/args.c +++ b/src/args.c @@ -26,6 +26,7 @@ #include "getopt.h" #include "raw_replacements.h" +struct statistics_data global_stats; /** * Logging definitions diff --git a/src/config.h b/src/config.h index c107fc6..36579d0 100644 --- a/src/config.h +++ b/src/config.h @@ -339,4 +339,13 @@ struct packet_data { struct ytb_conntrack yct; }; +struct statistics_data { + unsigned long all_packet_counter; + unsigned long packet_counter; + unsigned long target_counter; + unsigned long sent_counter; +}; + +extern struct statistics_data global_stats; + #endif /* YTB_CONFIG_H */ diff --git a/src/kytunblock.c b/src/kytunblock.c index ab27f2c..587152a 100644 --- a/src/kytunblock.c +++ b/src/kytunblock.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -329,6 +330,8 @@ erret_lc: return ret; } + ++global_stats.sent_counter; + int ipvx = netproto_version(pkt, pktlen); if (ipvx == IP4VERSION) { @@ -483,6 +486,8 @@ static NF_CALLBACK(ykb_nf_hook, skb) { struct config_t *config = cur_config; kref_get(&config->refcount); + ++global_stats.all_packet_counter; + if ((skb->mark & config->mark) == config->mark) { goto send_verdict; } @@ -523,12 +528,14 @@ static NF_CALLBACK(ykb_nf_hook, skb) { pd.payload_len = skb->len; int vrd = process_packet(config, &pd); + ++global_stats.packet_counter; switch(vrd) { case PKT_ACCEPT: nf_verdict = NF_ACCEPT; break; case PKT_DROP: + ++global_stats.target_counter; nf_verdict = NF_STOLEN; kfree_skb(skb); break; @@ -580,6 +587,22 @@ static struct pernet_operations ykb_pernet_ops = { }; #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0) */ +#ifdef CONFIG_PROC_FS + +static int stats_show(struct seq_file *s, void *v) { + seq_printf(s, "youtubeUnblock stats: \n" + "\tCatched: %ld packets\n" + "\tProcessed: %ld packets\n" + "\tTargetted: %ld packets\n" + "\tSent over socket %ld packets\n", + global_stats.all_packet_counter, global_stats.packet_counter, + global_stats.target_counter, global_stats.sent_counter); + + return 0; +} + +#endif + static int __init ykb_init(void) { int ret; @@ -615,6 +638,13 @@ static int __init ykb_init(void) { } #endif /* NO_IPV6 */ +#ifdef CONFIG_PROC_FS + if (!proc_create_single_data("kyoutubeUnblock", 0440, NULL, + stats_show, NULL)) { + lgwarning("kyoutubeUnblock procfs entry creation failed"); + } +#endif + #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0) ret = register_pernet_subsys(&ykb_pernet_ops); #else @@ -651,6 +681,10 @@ static void __exit ykb_destroy(void) { close_raw6_socket(); #endif +#ifdef CONFIG_PROC_FS + remove_proc_entry("kyoutubeUnblock", NULL); +#endif + close_raw_socket(); kref_put(&cur_config->refcount, config_release); lginfo("youtubeUnblock kernel module destroyed.\n"); diff --git a/src/youtubeUnblock.c b/src/youtubeUnblock.c index 7bb41ec..8c5bffe 100644 --- a/src/youtubeUnblock.c +++ b/src/youtubeUnblock.c @@ -46,6 +46,7 @@ #include #include #include +#include #include "config.h" #include "mangle.h" @@ -546,6 +547,7 @@ erret_lc: return ret; } + ++global_stats.sent_counter; int ipvx = netproto_version(pkt, pktlen); if (ipvx == IP4VERSION) { @@ -640,6 +642,8 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) { uint16_t l3num; uint32_t id; + ++global_stats.all_packet_counter; + if (nfq_nlmsg_parse(nlh, attr) < 0) { lgerror(-errno, "Attr parse"); return MNL_CB_ERROR; @@ -694,8 +698,11 @@ ct_out: ret = process_packet(cur_config, &packet); + ++global_stats.packet_counter; + switch (ret) { case PKT_DROP: + ++global_stats.target_counter; nfq_nlmsg_verdict_put(verdnlh, id, NF_DROP); break; default: @@ -875,6 +882,16 @@ struct instance_config_t instance_config = { .send_delayed_packet = delay_packet_send, }; +void sigint_handler(int s) { + lginfo("youtubeUnblock stats: catched %ld packets, " + "processed %ld packets, " + "targetted %ld packets, sent over socket %ld packets", + global_stats.all_packet_counter, global_stats.packet_counter, + global_stats.target_counter, global_stats.sent_counter); + + exit(EXIT_SUCCESS); +} + int main(int argc, char *argv[]) { int ret; struct config_t config; @@ -893,6 +910,8 @@ int main(int argc, char *argv[]) { parse_global_lgconf(&config); cur_config = &config; + signal(SIGINT, sigint_handler); + signal(SIGTERM, sigint_handler); if (open_raw_socket() < 0) { lgerror(-errno, "Unable to open raw socket");