<commit_before>/*
 * Monitoring code for network dropped packet alerts
 *
 * Copyright (C) 2009 <NAME> <<EMAIL>>
 */

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/string.h>
#include <linux/if_arp.h>
#include <linux/inetdevice.h>
#include <linux/inet.h>
#include <linux/interrupt.h>
#include <linux/netpoll.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/workqueue.h>
#include <linux/netlink.h>
#include <linux/net_dropmon.h>
#include <linux/percpu.h>
#include <linux/timer.h>
#include <linux/bitops.h>
#include <net/genetlink.h>

#include <trace/skb.h>

#include <asm/unaligned.h>

#define TRACE_ON 1
#define TRACE_OFF 0

static void send_dm_alert(struct work_struct *unused);


/*
 * Globals, our netlink socket pointer
 * and the work handle that will send up
 * netlink alerts
 */
struct sock *dm_sock;

struct per_cpu_dm_data {
	struct work_struct dm_alert_work;
	struct sk_buff *skb;
	atomic_t dm_hit_count;
	struct timer_list send_timer;
};

static struct genl_family net_drop_monitor_family = {
	.id             = GENL_ID_GENERATE,
	.hdrsize        = 0,
	.name           = "NET_DM",
	.version        = 1,
	.maxattr        = NET_DM_CMD_MAX,
};

static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);

static int dm_hit_limit = 64;
static int dm_delay = 1;


static void reset_per_cpu_data(struct per_cpu_dm_data *data)
{
	size_t al;
	struct net_dm_alert_msg *msg;

	al = sizeof(struct net_dm_alert_msg);
	al += dm_hit_limit * sizeof(struct net_dm_drop_point);
	data->skb = genlmsg_new(al, GFP_KERNEL);
	genlmsg_put(data->skb, 0, 0, &net_drop_monitor_family,
			0, NET_DM_CMD_ALERT);
	msg = __nla_reserve_nohdr(data->skb, sizeof(struct net_dm_alert_msg));
	memset(msg, 0, al);
	atomic_set(&data->dm_hit_count, dm_hit_limit);
}

static void send_dm_alert(struct work_struct *unused)
{
	struct sk_buff *skb;
	struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);

	/*
	 * Grab the skb we're about to send
	 */
	skb = data->skb;

	/*
	 * Replace it with a new one
	 */
	reset_per_cpu_data(data);

	/*
	 * Ship it!
	 */
	genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);

}

/*
 * This is the timer function to delay the sending of an alert
 * in the event that more drops will arrive during the
 * hysteresis period.  Note that it operates under the timer interrupt
 * so we don't need to disable preemption here
 */
static void sched_send_work(unsigned long unused)
{
	struct per_cpu_dm_data *data =  &__get_cpu_var(dm_cpu_data);

	schedule_work(&data->dm_alert_work);
}

static void trace_kfree_skb_hit(struct sk_buff *skb, void *location)
{
	struct net_dm_alert_msg *msg;
	struct nlmsghdr *nlh;
	int i;
	struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);


	if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
		/*
		 * we're already at zero, discard this hit
		 */
		goto out;
	}

	nlh = (struct nlmsghdr *)data->skb->data;
	msg = genlmsg_data(nlmsg_data(nlh));
	for (i = 0; i < msg->entries; i++) {
		if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
			msg->points[i].count++;
			goto out;
		}
	}

	/*
	 * We need to create a new entry
	 */
	__nla_reserve_nohdr(data->skb, sizeof(struct net_dm_drop_point));
	memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
	msg->points[msg->entries].count = 1;
	msg->entries++;

	if (!timer_pending(&data->send_timer)) {
		data->send_timer.expires = jiffies + dm_delay * HZ;
		add_timer_on(&data->send_timer, smp_processor_id());
	}

out:
	return;
}

static int set_all_monitor_traces(int state)
{
	int rc = 0;

	switch (state) {<commit_msg>drop_monitor: Update netlink protocol to include netlink attribute header in alert message<commit_after>/*
 * Monitoring code for network dropped packet alerts
 *
 * Copyright (C) 2009 <NAME> <<EMAIL>>
 */

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/string.h>
#include <linux/if_arp.h>
#include <linux/inetdevice.h>
#include <linux/inet.h>
#include <linux/interrupt.h>
#include <linux/netpoll.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/workqueue.h>
#include <linux/netlink.h>
#include <linux/net_dropmon.h>
#include <linux/percpu.h>
#include <linux/timer.h>
#include <linux/bitops.h>
#include <net/genetlink.h>

#include <trace/skb.h>

#include <asm/unaligned.h>

#define TRACE_ON 1
#define TRACE_OFF 0

static void send_dm_alert(struct work_struct *unused);


/*
 * Globals, our netlink socket pointer
 * and the work handle that will send up
 * netlink alerts
 */
struct sock *dm_sock;

struct per_cpu_dm_data {
	struct work_struct dm_alert_work;
	struct sk_buff *skb;
	atomic_t dm_hit_count;
	struct timer_list send_timer;
};

static struct genl_family net_drop_monitor_family = {
	.id             = GENL_ID_GENERATE,
	.hdrsize        = 0,
	.name           = "NET_DM",
	.version        = 2,
	.maxattr        = NET_DM_CMD_MAX,
};

static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);

static int dm_hit_limit = 64;
static int dm_delay = 1;


static void reset_per_cpu_data(struct per_cpu_dm_data *data)
{
	size_t al;
	struct net_dm_alert_msg *msg;
	struct nlattr *nla;

	al = sizeof(struct net_dm_alert_msg);
	al += dm_hit_limit * sizeof(struct net_dm_drop_point);
	al += sizeof(struct nlattr);

	data->skb = genlmsg_new(al, GFP_KERNEL);
	genlmsg_put(data->skb, 0, 0, &net_drop_monitor_family,
			0, NET_DM_CMD_ALERT);
	nla = nla_reserve(data->skb, NLA_UNSPEC, sizeof(struct net_dm_alert_msg));
	msg = nla_data(nla);
	memset(msg, 0, al);
	atomic_set(&data->dm_hit_count, dm_hit_limit);
}

static void send_dm_alert(struct work_struct *unused)
{
	struct sk_buff *skb;
	struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);

	/*
	 * Grab the skb we're about to send
	 */
	skb = data->skb;

	/*
	 * Replace it with a new one
	 */
	reset_per_cpu_data(data);

	/*
	 * Ship it!
	 */
	genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);

}

/*
 * This is the timer function to delay the sending of an alert
 * in the event that more drops will arrive during the
 * hysteresis period.  Note that it operates under the timer interrupt
 * so we don't need to disable preemption here
 */
static void sched_send_work(unsigned long unused)
{
	struct per_cpu_dm_data *data =  &__get_cpu_var(dm_cpu_data);

	schedule_work(&data->dm_alert_work);
}

static void trace_kfree_skb_hit(struct sk_buff *skb, void *location)
{
	struct net_dm_alert_msg *msg;
	struct nlmsghdr *nlh;
	struct nlattr *nla;
	int i;
	struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);


	if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
		/*
		 * we're already at zero, discard this hit
		 */
		goto out;
	}

	nlh = (struct nlmsghdr *)data->skb->data;
	nla = genlmsg_data(nlmsg_data(nlh));
	msg = nla_data(nla);
	for (i = 0; i < msg->entries; i++) {
		if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
			msg->points[i].count++;
			goto out;
		}
	}

	/*
	 * We need to create a new entry
	 */
	__nla_reserve_nohdr(data->skb, sizeof(struct net_dm_drop_point));
	nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
	memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
	msg->points[msg->entries].count = 1;
	msg->entries++;

	if (!timer_pending(&data->send_timer)) {
		data->send_timer.expires = jiffies + dm_delay * HZ;
		add_timer_on(&data->send_timer, smp_processor_id());
	}

out:
	return;
}

static int set_all_monitor_traces(int state)
{
	int rc = 0;

	switch (state) {
{'fraction_non_alphanumeric': 0.1289031224979984, 'fraction_numerical': 0.005604483586869495, 'mean_word_length': 4.403749098774333, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 92, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4476579', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2867, 'n_words': 680}