TOFFEE

VIDEOS :: Linux Kernel Programming

Written by: Kiran Kankipati
Published: 05-Jul-2016


Linux Kernel IPv4 stack packet fragmentation and defragmentation APIs

Linux Kernel Module to drop packets captured via netfilter hooks

Download this episode my entire kernel module sample code, make file, clean script HERE. And here is the source code for a quick reference.

/* pkt_drop.c
 * Author: Kiran Kankipati
 * Updated: 09-feb-2017
 */
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h> 
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>  
#include <linux/udp.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/inet.h>
#include <linux/inetdevice.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>
#include <linux/icmp.h>
#include <linux/netlink.h>
#include <linux/mroute.h>
#include <net/checksum.h>
#include <net/inet_ecn.h>
#include <net/xfrm.h>
#include <net/route.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/arp.h>
#include <net/udp.h>
#include <net/icmp.h>
#include <net/inetpeer.h>
#include <net/protocol.h>
#include <net/flow.h>
#include <asm/types.h>

static struct nf_hook_ops nfho_pre_routing;

unsigned int pre_routing_hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
	//kfree_skb(skb);  //- dont do this, this will crash your system.
	return NF_DROP; //this will drop the packet
	
	//return NF_ACCEPT; //this will accept the packet
}


static int hello_init(void)
{
	//Packet RX
	nfho_pre_routing.hook = pre_routing_hook_func;
	nfho_pre_routing.hooknum = NF_INET_PRE_ROUTING;
	nfho_pre_routing.pf = PF_INET;
	nfho_pre_routing.priority = NF_IP_PRI_FIRST;
	nf_register_hook(&nfho_pre_routing);

	return 0;
}

static void hello_exit(void) { nf_unregister_hook(&nfho_pre_routing); }

module_init(hello_init);
module_exit(hello_exit);

Linux Kernel - kmalloc() and vmalloc() performance research

Benchmarking performance of kmalloc() and vmalloc() allocated buffers

Download this episode my entire kernel module sample code, make file, clean script HERE. And here is the source code for a quick reference.

/* kmalloc_vs_vmalloc 
 * The Linux Channel
 * Author: Kiran Kankipati
 * Updated: 23-nov-2016
 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/vmalloc.h>
#include <asm/uaccess.h>


MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kiran Kankipati");
MODULE_DESCRIPTION("kmalloc vs vmalloc");
MODULE_VERSION("0.1");

unsigned char *kmallocbuf;
unsigned char *vmallocbuf;

#define BUFLEN 1000

static int __init kmalloc_vs_vmalloc_init(void)
{	unsigned long js, je, jdiff;
	int i=0,j=0;
   kmallocbuf = kmalloc( BUFLEN, GFP_KERNEL);
   if(kmallocbuf)
   {	sprintf(kmallocbuf, "This is a test (kmallocbuf)\n");
     	printk("%s", kmallocbuf);
     	
     	js = jiffies;
     	for(j=0;j<100000;j++){ for(i=0;i<BUFLEN;i++) { kmallocbuf[i] = i+j; } }
     	je = jiffies;
     	printk("dummy print kmallocbuf: %02x\n", kmallocbuf[15]);
     	jdiff = je - js;
     	printk("\nEnd Time [%lu] - Start Time [%lu] - Diff [%lu]\n",je,js, jdiff);
   }
   printk("-------------------------------\n");
   
   vmallocbuf = vmalloc(BUFLEN);
   if(vmallocbuf)
   {	sprintf(vmallocbuf, "This is a test (vmallocbuf)\n");
   	printk("%s", vmallocbuf);
   	
   	js = jiffies;
   	for(j=0;j<100000;j++){ for(i=0;i<BUFLEN;i++) { vmallocbuf[i] = i+j; } }
     	je = jiffies;
     	printk("dummy print vmallocbuf: %02x\n", vmallocbuf[15]);
     	jdiff = je - js;
     	printk("\nEnd Time [%lu] - Start Time [%lu] - Diff [%lu]\n",je,js, jdiff);
   }
   return 0;
}


static void __exit kmalloc_vs_vmalloc_exit(void)
{	
   kfree(kmallocbuf);
   vfree(vmallocbuf);
}

module_init(kmalloc_vs_vmalloc_init);
module_exit(kmalloc_vs_vmalloc_exit);

Linux Kernel /proc filesystem vs /dev character device drivers

GOTO Statements in Linux Kernel Source

Linux Kernel run-time performance vs GCC Compiler

How to stay in touch with latest Linux Kernel

Linux Kernel preemption model during Kernel Compilation

Linux Kernel atomic_t datatype - atomic variables and APIs

Linux Kernel kmalloc() vs vmalloc() kernel space memory allocation


Thank you, Kiran




Back to Top