229 lines
5.9 KiB
C
229 lines
5.9 KiB
C
#ifdef __KERNEL__
|
|
|
|
#include <linux/init.h>
|
|
#include <linux/module.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/gfp.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/device.h>
|
|
#include <linux/in.h>
|
|
#include <linux/proc_fs.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/spinlock.h>
|
|
|
|
#include "../proc_api/proc_api.h"
|
|
#include "../../common/cJSON/cJSON.h"
|
|
#include "obj_api.h"
|
|
|
|
#define OBJ_DEV_NAME ("isg_objs")
|
|
#define VERSION ("obj0.0.0.2")
|
|
#define SHM_MEM_SIZE (1024 * 1024)
|
|
|
|
typedef struct {
|
|
struct class *dev_class;
|
|
int dev_major;
|
|
atomic_t ref_count;
|
|
unsigned int mem_size;
|
|
unsigned char *mem_buf;
|
|
} OBJECT_DRV, *POBJECT_DRV;
|
|
|
|
static POBJECT_DRV g_obj_device = NULL;
|
|
DEFINE_RWLOCK(g_obj_lock);
|
|
PCMHI_OBJECT g_obj_data = NULL;
|
|
|
|
void obj_upgrade_dev_buffer(unsigned char *buf, unsigned int size)
|
|
{
|
|
if(buf && size < g_obj_device->mem_size - 1) {
|
|
memcpy(g_obj_device->mem_buf, buf, size);
|
|
g_obj_device->mem_buf[size] = 0;
|
|
} else {
|
|
printk(KERN_ERR "Buffer = %p, size = %u\n", buf, size);
|
|
}
|
|
}
|
|
|
|
static int obj_proc_init(void)
|
|
{
|
|
return server_proc_init();
|
|
}
|
|
|
|
static int obj_proc_uninit(void)
|
|
{
|
|
return server_proc_uninit();
|
|
}
|
|
|
|
const char *obj_version(void)
|
|
{
|
|
return VERSION;
|
|
}
|
|
|
|
PCMHI_OBJECT new_object(void)
|
|
{
|
|
PCMHI_OBJECT obj = (PCMHI_OBJECT)kmalloc(sizeof(CMHI_OBJECT), GFP_KERNEL);
|
|
|
|
if(obj == NULL) {
|
|
printk(KERN_ERR "Malloc CMHI_OBJECT Error\n");
|
|
} else {
|
|
memset(obj, 0, sizeof(CMHI_OBJECT));
|
|
rwlock_init(&obj->lock);
|
|
atomic_set(&obj->ref_count, 0);
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
void free_object(PCMHI_OBJECT pObj)
|
|
{
|
|
if(pObj) {
|
|
kfree(pObj);
|
|
}
|
|
}
|
|
|
|
void object_init(void)
|
|
{
|
|
PCMHI_OBJECT pObj = create_server_object("list1", OBJ_TYPE_SERVER);
|
|
|
|
if(!pObj) {
|
|
printk(KERN_ERR "%s(%d): Create Object Error\n", __FUNCTION__, __LINE__);
|
|
return;
|
|
}
|
|
|
|
HASH_ADD_STR(g_obj_data, name, pObj);
|
|
|
|
if(add_server_obj_data(pObj, 10000, 20000, IPPROTO_IP) != 0) {
|
|
printk(KERN_ERR "%s(%d): Add Server Object error\n", __FUNCTION__, __LINE__);
|
|
return;
|
|
}
|
|
|
|
if(add_server_obj_data(pObj, 80, 80, IPPROTO_TCP) != 0) {
|
|
printk(KERN_ERR "%s(%d): Add Server Object error\n", __FUNCTION__, __LINE__);
|
|
return;
|
|
}
|
|
|
|
if(add_server_obj_data(pObj, 443, 443, IPPROTO_UDP) != 0) {
|
|
printk(KERN_ERR "%s(%d): Add Server Object error\n", __FUNCTION__, __LINE__);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void object_uninit(void)
|
|
{
|
|
PCMHI_OBJECT obj, tmp;
|
|
|
|
HASH_ITER(hh, g_obj_data, obj, tmp) {
|
|
HASH_DEL(g_obj_data, obj);
|
|
cleanup_server_object(obj);
|
|
}
|
|
}
|
|
|
|
static int obj_open(struct inode *inode, struct file *filp)
|
|
{
|
|
atomic_inc(&g_obj_device->ref_count);
|
|
return 0;
|
|
}
|
|
|
|
static int obj_release(struct inode *inode, struct file *filp)
|
|
{
|
|
atomic_dec(&g_obj_device->ref_count);
|
|
return 0;
|
|
}
|
|
|
|
int obj_mmap(struct file *fd, struct vm_area_struct *vma)
|
|
{
|
|
int ret = remap_pfn_range(vma, vma->vm_start,
|
|
virt_to_phys(g_obj_device->mem_buf) >> PAGE_SHIFT,
|
|
g_obj_device->mem_size,
|
|
PAGE_SHARED);
|
|
return ret;
|
|
}
|
|
|
|
static struct file_operations obj_fops = {
|
|
.open = obj_open,
|
|
.release = obj_release,
|
|
.mmap = obj_mmap,
|
|
};
|
|
|
|
static int object_module_init(void)
|
|
{
|
|
struct page *p = NULL;
|
|
dev_t dev = 0;
|
|
|
|
printk(KERN_INFO "Hello ISG objects manager version: %s\n", VERSION);
|
|
|
|
if(alloc_chrdev_region(&dev, 0, 1, OBJ_DEV_NAME) != 0) {
|
|
printk(KERN_ERR "Alloc driver dev id error\n");
|
|
return -ENODEV;
|
|
}
|
|
|
|
g_obj_device = (POBJECT_DRV)kmalloc(sizeof(OBJECT_DRV), GFP_KERNEL);
|
|
|
|
if(g_obj_device == NULL) {
|
|
printk(KERN_ERR "Create isg objects error\n");
|
|
return -ENOMEM;
|
|
}
|
|
|
|
memset(g_obj_device, 0, sizeof(OBJECT_DRV));
|
|
g_obj_device->dev_major = MAJOR(dev);
|
|
atomic_set(&g_obj_device->ref_count, 0);
|
|
g_obj_device->mem_size = PAGE_ALIGN(SHM_MEM_SIZE);
|
|
g_obj_device->mem_buf = (unsigned char *)__get_free_pages(GFP_KERNEL, get_order(g_obj_device->mem_size));
|
|
|
|
if(g_obj_device->mem_buf == NULL) {
|
|
printk(KERN_ERR "Malloc %d pages error\n", get_order(g_obj_device->mem_size));
|
|
kfree(g_obj_device);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
for(p = virt_to_page(g_obj_device->mem_buf);
|
|
p < virt_to_page(g_obj_device->mem_buf + g_obj_device->mem_size);
|
|
p++) {
|
|
SetPageReserved(virt_to_page(p));
|
|
}
|
|
|
|
memset(g_obj_device->mem_buf, 0, g_obj_device->mem_size);
|
|
register_chrdev(g_obj_device->dev_major, OBJ_DEV_NAME, &obj_fops);
|
|
g_obj_device->dev_class = class_create(THIS_MODULE, "obj");
|
|
device_create(g_obj_device->dev_class, NULL, MKDEV(g_obj_device->dev_major, 0),
|
|
NULL, "isg_objs/dev%d", 0);
|
|
|
|
obj_proc_init();
|
|
object_init();
|
|
|
|
return 0;
|
|
}
|
|
module_init(object_module_init);
|
|
|
|
static void __exit object_module_exit(void)
|
|
{
|
|
struct page *p = NULL;
|
|
|
|
printk(KERN_INFO "Bye ISG objects manager version: %s\n", VERSION);
|
|
|
|
obj_proc_uninit();
|
|
object_uninit();
|
|
|
|
if(atomic_read(&g_obj_device->ref_count) != 0) {
|
|
printk(KERN_ERR "Device used, please close it first.\n");
|
|
return;
|
|
}
|
|
|
|
if(g_obj_device->mem_buf) {
|
|
for(p = virt_to_page(g_obj_device->mem_buf);
|
|
p < virt_to_page(g_obj_device->mem_buf + g_obj_device->mem_size);
|
|
p++) {
|
|
ClearPageReserved(virt_to_page(g_obj_device->mem_buf));
|
|
}
|
|
|
|
free_pages((unsigned long)g_obj_device->mem_buf, get_order(g_obj_device->mem_size));
|
|
}
|
|
|
|
device_destroy(g_obj_device->dev_class, MKDEV(g_obj_device->dev_major, 0));
|
|
class_destroy(g_obj_device->dev_class);
|
|
unregister_chrdev(g_obj_device->dev_major, OBJ_DEV_NAME);
|
|
|
|
kfree(g_obj_device);
|
|
}
|
|
module_exit(object_module_exit);
|
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|
|
#endif // __KERNEL__
|