summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrazb <razb@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2005-04-01 21:33:36 +0000
committerrazb <razb@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2005-04-01 21:33:36 +0000
commitd0d98b811337aa18f05c911f1017b305156009e4 (patch)
treea2c3d1535db3da657f1cba407db31d2d419cd28d
parent35510d5b8bf25a8bd3e9bd7066fed560d95cbedb (diff)
downloadATCD-d0d98b811337aa18f05c911f1017b305156009e4.tar.gz
*** empty log message ***
-rw-r--r--examples/netlink_pingpong/Makefile51
-rw-r--r--examples/netlink_pingpong/driver/Makefile31
-rw-r--r--examples/netlink_pingpong/driver/netlink_pong.c119
-rw-r--r--examples/netlink_pingpong/netlinkgateway.h62
-rw-r--r--examples/netlink_pingpong/netlinkmsg.h28
-rw-r--r--examples/netlink_pingpong/singleton.h29
6 files changed, 320 insertions, 0 deletions
diff --git a/examples/netlink_pingpong/Makefile b/examples/netlink_pingpong/Makefile
new file mode 100644
index 00000000000..b1638507063
--- /dev/null
+++ b/examples/netlink_pingpong/Makefile
@@ -0,0 +1,51 @@
+MAKE=make
+CXX=g++
+
+CFG_INC += -I/usr/local/include/stlport
+#CFG_LIB += -L/usr/local/lib -lstlport_gcc
+CFG_LIB += -L/usr/local/lib
+
+OUTDIR=Debug
+OUTFILE=$(OUTDIR)/netlink_pingpong
+CFG_INC+=-I$(ACE_ROOT)
+
+#CFG_LIB+=-L$(ACE_ROOT)/ace/ -lstlport_gcc -lACE
+CFG_LIB+=-L$(ACE_ROOT)/ace/ -lACE
+CFG_OBJ=
+COMMON_OBJ=$(OUTDIR)/netlink_pingpong.o \
+ $(OUTDIR)/netlinkgateway.o
+
+OBJ=$(COMMON_OBJ) $(CFG_OBJ)
+ALL_OBJ=$(OUTDIR)/*.o
+
+COMPILE=$(CXX) -c -g $(CFG_INC) -O2 -pthread \
+ -DACE_NDEBUG \
+ -DACE_USE_RCSID=0 \
+ -DACE_HAS_EXCEPTIONS \
+ -D__ACE_INLINE__ \
+ $(CFG_INC) \
+ -o "$(OUTDIR)/$(*F).o" "$<"
+
+LINK=$(CXX) -g $(OUTDIR)/*.o $(CFG_LIB) -o "$(OUTFILE)"
+
+# Pattern rules
+$(OUTDIR)/%.o : %.cpp
+ $(COMPILE)
+
+# Build rules
+all: $(OUTFILE)
+
+$(OUTFILE): $(OUTDIR) $(OBJ)
+ $(LINK)
+
+$(OUTDIR):
+ mkdir -p "$(OUTDIR)"
+
+# Rebuild this project
+rebuild: cleanall all
+
+# Clean this project
+clean:
+ rm -f $(OUTFILE)
+ rm -f $(OBJ)
+
diff --git a/examples/netlink_pingpong/driver/Makefile b/examples/netlink_pingpong/driver/Makefile
new file mode 100644
index 00000000000..738d816647d
--- /dev/null
+++ b/examples/netlink_pingpong/driver/Makefile
@@ -0,0 +1,31 @@
+TARGET=netlink_pong
+OBJS=netlink_pong.o
+MDIR=drivers/misc
+
+EXTRA_CFLAGS = -DEXPORT_SYMTAB
+CURRENT = $(shell uname -r)
+KDIR = /lib/modules/$(CURRENT)/build
+PWD = $(shell pwd)
+PWD += ../
+DEST = /lib/modules/$(CURRENT)/kernel/$(MDIR)
+
+obj-m := $(TARGET).o
+uds-objs := $(OBJS)
+default:
+ make -C $(KDIR) SUBDIRS=$(PWD) modules
+
+$(TARGET).o: $(OBJS)
+ $(LD) $(LD_RFLAG) -r -o $@ $(OBJS)
+
+ifneq (,$(findstring 2.4.,$(CURRENT)))
+install:
+ su -c "cp -v $(TARGET).o $(DEST) && /sbin/depmod -a"
+else
+install:
+ su -c "cp -v $(TARGET).ko $(DEST) && /sbin/depmod -a"
+endif
+
+clean:
+ -rm -f *.o *.ko .*.cmd .*.flags *.mod.c
+
+-include $(KDIR)/Rules.make
diff --git a/examples/netlink_pingpong/driver/netlink_pong.c b/examples/netlink_pingpong/driver/netlink_pong.c
new file mode 100644
index 00000000000..a6727be3a5f
--- /dev/null
+++ b/examples/netlink_pingpong/driver/netlink_pong.c
@@ -0,0 +1,119 @@
+/*! \file netlink-drv-pong.c
+ \brief netlink test driver
+
+ this is an example driver for netlink message.
+ It pings back a message to the sender.
+
+ This driver was compiled and tested on linux kernel 2.6.5 ( i used fedora core 2 ) .
+ Please note that some some changes were made from 2.4.x to 2.6.x in the interafce.
+*/
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <asm/types.h>
+#include <net/sock.h>
+#include <linux/skbuff.h>
+#include <linux/netlink.h>
+#include "../netlinkmsg.h"
+
+#define NETLINK_UNUSED 14
+
+MODULE_DESCRIPTION("ace_netlink kernel module");
+MODULE_AUTHOR("raz ben yehuda");
+MODULE_LICENSE("GPL");
+
+static struct sock* ace_netlink_sock;
+
+
+static int ace_netlink_send_message(int type, void *message, int pid,int length, struct sock* socket)
+{
+ struct sk_buff *skb;
+ char *data_start;
+ int ret = 0 ;
+
+ if (!message){
+ printk("null message\n");
+ return -EINVAL;
+ }
+
+ if (length > PAGE_SIZE){
+ printk("length bigger than page size\n");
+ return -EINVAL;
+ }
+
+ skb = alloc_skb(length,GFP_ATOMIC);
+ if (!skb){
+ printk("failed to alloc skb\n");
+ return -ENOMEM;
+ }
+ data_start = skb_put(skb, length);
+ memcpy(data_start, message, length);
+ ret = netlink_unicast(socket,skb,pid,0);
+ printk("scipio_send_message ret = %d\n",ret);
+ return ret;
+}
+
+void ace_netlink_handle_msg(void* r_msg)
+{
+ NetlinkMsg* request = (NetlinkMsg*)r_msg;
+ if (0!=request->h.dw_pid){
+
+ int ret_bytes=0;
+ NetlinkMsg Reply;
+ Reply.h.dw_pid = request->h.dw_pid;
+ Reply.h.dw_num = ++request->h.dw_num;
+ printk("pong %d",Reply.h.dw_num);
+ ret_bytes = ace_netlink_send_message(0,(void*)&Reply,Reply.h.dw_pid,sizeof(Reply),ace_netlink_sock);
+ if (sizeof(Reply) != ret_bytes )
+ printk("ace_netlink_handle_msg send failed\n");
+ return;
+ }
+ printk("ace_netlink_handle_msg mesg error\n");
+}
+
+/*! \fn scipio_netlink_receive
+ \brief a call back issued by the nerlink module in the context of the sender.
+*/
+static void ace_netlink_netlink_receive(struct sock *sk, int length)
+{
+
+ struct sk_buff *skb;
+ while ((skb = skb_dequeue(&sk->sk_receive_queue) ) != NULL){
+
+ printk("ace_netlink_netlink_receive message recieved %d\n",skb->len);
+ ace_netlink_handle_msg(skb->data);
+ kfree_skb(skb); // always release the skb through the network slab
+ }
+ //printk("ace_netlink_netlink_receive out\n");
+}
+
+
+
+int ace_netlink_init(void)
+{
+ ace_netlink_sock = netlink_kernel_create(NETLINK_UNUSED,ace_netlink_netlink_receive);
+ //return netlink_attach(NETLINK_SCIPIO,scipio_netlink_receive);
+ if (!ace_netlink_sock) {
+ printk(KERN_ERR "unable to create netlink socket; aborting\n");
+ return -ENODEV;
+ }
+
+ return 0;
+}
+
+static int ace_netlink_init_module(void)
+{
+ printk( "Module ace_netlink init\n" );
+ return ace_netlink_init();
+}
+
+static void ace_netlink_exit_module(void)
+{
+ if (ace_netlink_sock)
+ sock_release(ace_netlink_sock->sk_socket);
+
+ printk( "Module ace_netlink exit\n" );
+}
+
+module_init(ace_netlink_init_module);
+module_exit(ace_netlink_exit_module);
diff --git a/examples/netlink_pingpong/netlinkgateway.h b/examples/netlink_pingpong/netlinkgateway.h
new file mode 100644
index 00000000000..b1208fe259d
--- /dev/null
+++ b/examples/netlink_pingpong/netlinkgateway.h
@@ -0,0 +1,62 @@
+// $Id$
+/*! \file netlinkgateway.h
+ \brief netlink getway class.
+
+ This class is used to pass buffer to the driver and back.
+
+ Why use netlink sockets as a communication facility
+
+*/
+#ifndef NETLINKGATEWAY_H
+#define NETLINKGATEWAY_H
+
+#include "ace/SOCK_Netlink.h"
+#include "ace/Reactor.h"
+#include "ace/SOCK_IO.h"
+#include "ace/Event_Handler.h"
+#include "singleton.h"
+#include "netlinkmsg.h"
+
+/*! \class CNetlinkGateway
+ \brief Reactor plug in class for netlink sockets.
+
+ Handles incoming buffers/messages and sends them back.
+ unlike its name ( lack of time ) it acts both as a gateways and a dispatcher.
+*/
+class CNetlinkGateway : public ACE_Event_Handler,
+ public CSingleton<CNetlinkGateway>
+{
+ ACE_Netlink_Addr m_Addr;
+ ACE_SOCK_Netlink m_Socket;
+
+protected:
+
+ CNetlinkGateway();
+
+public:
+ friend class CSingleton<CNetlinkGateway> ;
+
+ ~CNetlinkGateway();
+ //!< Get this handler's I/O handle.
+ virtual ACE_HANDLE get_handle (void) const{
+ return m_Socket.get_handle ();
+ }
+
+ int Send(char* buf,int dwSize){ return m_Socket.send(buf,dwSize,0);}
+ int Ping(NetlinkMsg& msg);
+ void Start() { NetlinkMsg msg={0}; Ping(msg); }
+ //
+ // Reacotr methods
+ //
+ virtual int open () ; //!<
+ //!< Called when input is available from the client.
+ virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);
+ //!< Called when output is possible.
+ virtual int handle_output (ACE_HANDLE fd = ACE_INVALID_HANDLE);
+ //!< Called when this handler is removed from the ACE_Reactor.
+ virtual int handle_close (ACE_HANDLE handle,ACE_Reactor_Mask close_mask);
+
+};
+
+
+#endif
diff --git a/examples/netlink_pingpong/netlinkmsg.h b/examples/netlink_pingpong/netlinkmsg.h
new file mode 100644
index 00000000000..8a271fba3e5
--- /dev/null
+++ b/examples/netlink_pingpong/netlinkmsg.h
@@ -0,0 +1,28 @@
+// $Id$
+/*! \file netlinmmsg.h
+ *
+ * \brief a message struct which is passed to the and from the driver
+ */
+#ifndef NETLINKMSG_H
+#define NETLINKMSG_H
+
+ /*! \class NetlinkMsgHeader
+ \brief a simple netlink message header
+*/
+typedef struct _NetlinkMsgHeader_{
+
+ int dw_pid; //!< message agent's pid
+ int dw_num; //!< message number
+}NetlinkMsgHeader;
+
+
+/*! \class NetlinkMsg
+ \brief a simple netlink message
+*/
+typedef struct _NetlinkMsg_{
+
+ NetlinkMsgHeader h;
+ char data[512];
+}NetlinkMsg;
+
+#endif
diff --git a/examples/netlink_pingpong/singleton.h b/examples/netlink_pingpong/singleton.h
new file mode 100644
index 00000000000..1b0c27edf64
--- /dev/null
+++ b/examples/netlink_pingpong/singleton.h
@@ -0,0 +1,29 @@
+// $Id$
+
+/*! \file NFSingleton.h
+ * \brief singleton template
+ *
+ */
+#ifndef _NFSINGLETON_H_
+#define _NFSINGLETON_H_
+
+/*! \class CNFSingleon
+ \brief singleton template
+*/
+template <class T> class CSingleton
+{
+protected:
+ static T* m_instance;
+ CSingleton(){};
+
+public:
+
+ static T* instance()
+ {
+ if (!m_instance)
+ m_instance = new T();
+ return m_instance;
+ }
+};
+
+#endif