Krishna iResearch Intelligent Cloud Platform - VIRtual Generic Os - VIRGO - Linux kernel extensions for cloud
 All Classes
virgo_cloudexec.c
1 /***************************************************************************************
2 VIRGO - a linux module extension with CPU and Memory pooling with cloud capabilities
3 
4 Copyright (C) 2009-2013 Ka.Shrinivaasan
5 
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 mail to: ka.shrinivaasan@gmail.com
20 *****************************************************************************************/
21 
22 #include <linux/virgo.h>
23 
24 /*
25 #include <linux/kernel.h>
26 #include <linux/kthread.h>
27 #include <linux/sched.h>
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/fcntl.h>
31 #include <linux/net.h>
32 #include <linux/in.h>
33 #include <linux/inet.h>
34 #include <linux/udp.h>
35 #include <linux/tcp.h>
36 #include <linux/string.h>
37 #include <linux/unistd.h>
38 #include <linux/slab.h>
39 #include <linux/netdevice.h>
40 #include <linux/skbuff.h>
41 #include <linux/file.h>
42 #include <linux/freezer.h>
43 #include <net/sock.h>
44 #include <net/checksum.h>
45 #include <net/ip.h>
46 #include <net/ipv6.h>
47 #include <net/tcp.h>
48 #include <net/tcp_states.h>
49 #include <asm/uaccess.h>
50 #include <asm/ioctls.h>
51 #include <trace/events/skb.h>
52 
53 #include <linux/module.h>
54 #include <linux/types.h>
55 #include <linux/uio.h>
56 #include <linux/unistd.h>
57 #include <linux/init.h>
58 
59 
60 #include <linux/sunrpc/types.h>
61 #include <linux/sunrpc/clnt.h>
62 #include <linux/sunrpc/xdr.h>
63 #include <linux/sunrpc/msg_prot.h>
64 #include <linux/sunrpc/svcsock.h>
65 #include <linux/sunrpc/stats.h>
66 #include <linux/sunrpc/xprt.h>
67 //#include "sunrpc.h"//
68 #include <linux/sunrpc/xprt.h>
69 //#include "netns.h"//
70 
71 #define BUF_SIZE 500
72 typedef int (*FPTR)(void *args);
73 
74 static int virgocloudexec_create(void);
75 static int virgocloudexec_recvfrom(void);
76 static int virgocloudexec_sendto(void);
77 
78 static struct svc_xprt_ops virgo_ops = {
79  .xpo_create = virgocloudexec_create,
80  .xpo_recvfrom = virgocloudexec_recvfrom,
81  .xpo_sendto = virgocloudexec_sendto
82 };
83 
84 static struct svc_xprt_class virgo_class = {
85  .xcl_name = "virgo",
86  .xcl_owner = THIS_MODULE,
87  .xcl_ops = &virgo_ops
88 };
89 
90 
91 
92 struct task_struct *task;
93 int (*cloneFunction_ptr)(void*);
94 char* cloneFunction;
95 int error;
96 char buffer[BUF_SIZE];
97 struct socket *sock;
98 struct sockaddr_in sin;
99 int len=0;
100 struct socket *clientsock;
101 struct kvec iov;
102 struct msghdr msg;
103 int buflen=BUF_SIZE;
104 int nr=0;
105 int args=0;
106 */
107 
108 int clone_func(void* args)
109 {
110  return 1;
111 }
112 
113 
114 FPTR get_function_ptr_from_str(char* cloneFunction)
115 {
116  return clone_func;
117 }
118 
119 static int __init
120 virgocloudexec_init(void)
121 {
122  printk(KERN_INFO "doing init() of virgocloudexec kernel module\n");
123  return 0;
124 }
125 EXPORT_SYMBOL(virgocloudexec_init);
126 
127 static int virgocloudexec_create(void)
128 {
129  memset(&sin, 0, sizeof(struct sockaddr_in));
130  sin.sin_family=PF_INET;
131  sin.sin_addr.s_addr=htonl(INADDR_ANY);
132  sin.sin_port=htons(10000);
133 
134  /*stack=kmalloc(65536, GFP_KERNEL);*/
135  iov.iov_base=(void*)buffer;
136  iov.iov_len=BUF_SIZE;
137  error = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
138  printk(KERN_INFO "sock_create() returns error code: %d\n",error);
139 
140  error = kernel_bind(sock, (struct sockaddr*)&sin, sizeof(struct sockaddr_in));
141  printk(KERN_INFO "kernel_bind() returns error code: %d\n",error);
142 
143  error = kernel_listen(sock, 2);
144  printk(KERN_INFO "kernel_listen() returns error code: %d\n", error);
145 
146  /*error = kernel_accept(sock, &clientsock, O_NONBLOCK);*/
147  error = kernel_accept(sock, &clientsock, 0);
148  if(error==-EAGAIN)
149  printk(KERN_INFO "kernel_accept() returns -EAGAIN\n");
150  printk(KERN_INFO "kernel_accept() returns error code: %d\n",error);
151  return 0;
152 }
153 EXPORT_SYMBOL(virgocloudexec_create);
154 
155 static int virgocloudexec_recvfrom(void)
156 {
157  /*
158  do kernel_recvmsg() to get the function data to be executed on a thread
159  */
160  len = kernel_recvmsg(clientsock, &msg, &iov, buflen, nr, msg.msg_flags);
161  printk(KERN_INFO "kernel_recvmsg() returns len: %d\n",len);
162  /*
163  parse the message and invoke kthread_create()
164  do kernel_sendmsg() with the results
165  */
166  cloneFunction = kstrdup(iov.iov_base,GFP_KERNEL);
167  cloneFunction_ptr = get_function_ptr_from_str(cloneFunction);
168  task=kthread_create(cloneFunction_ptr, (void*)args, "cloudclonethread");
169  strcpy(buffer,"cloudclonethread executed");
170  return 0;
171 }
172 EXPORT_SYMBOL(virgocloudexec_recvfrom);
173 
174 static int virgocloudexec_sendto(void)
175 {
176  iov.iov_base=(void*)buffer;
177  iov.iov_len=25;
178  kernel_sendmsg(clientsock, &msg, &iov, nr, buflen);
179 
180  /*
181  struct task_struct *task;
182  int error;
183  struct addrinfo hints;
184  struct socket* sock;
185  struct socket* client_sock;
186  struct addrinfo *result, *rp;
187  int sfd, s;
188  struct sockaddr_storage peer_addr;
189  socklen_t peer_addr_len;
190  ssize_t nread;
191  char buf[BUF_SIZE];
192  struct kvec iov;
193  struct msghdr msg = {
194  .msg_flags = MSG_DONTWAIT,
195  };
196 
197  memset(&hints, 0, sizeof(struct addrinfo));
198  hints.ai_family = AF_UNSPEC; / Allow IPv4 or IPv6 /
199  hints.ai_socktype = SOCK_STREAM; / Datagram socket /
200  hints.ai_flags = AI_PASSIVE; / For wildcard IP address /
201  hints.ai_protocol = 0; / Any protocol /
202  hints.ai_canonname = NULL;
203  hints.ai_addr = NULL;
204  hints.ai_next = NULL;
205 
206  char* cloud_clone_port=60000;
207 
208  stack=kmalloc(65536, GFP_KERNEL);
209  iov.iov_base=(void*)buf;
210  iov.iov_len=BUF_SIZE;
211  s = getaddrinfo(NULL, cloud_clone_port, &hints, &result);
212 
213  sock_create(rp->ai_family, rp->ai_socktype,
214  rp->ai_protocol, sock);
215 
216  kernel_bind(sock, rp->ai_addr, rp->ai_addrlen);
217  kernel_listen(sock,64);
218 
219  freeaddrinfo(result); / No longer needed /
220 
221  error = kernel_accept(sock, clientsock, O_NONBLOCK);
222 
223  for (;;) {
224  nread = kernel_recvmsg(clientsock, &msg, buflen, &iov, nr, msg.msg_flags);
225 
226  char* cloneFunction = kstrdup(iov.iov_base,GFP_KERNEL);
227 
228  int ((*cloneFunction_ptr)(void*));
229  cloneFunction_ptr = get_function_ptr_from_str(cloneFunction);
230  int *args=0;
231  task=kthread_create(cloneFunction_ptr, (void*)args, "cloudclonethread");
232  strcpy(buffer,"cloudclonethread executed");
233  iov.iov_base=(void*)buf;
234  iov.iov_len=BUF_SIZE;
235  kernel_sendmsg(clientsock, &msg, buflen, &iov, nr);
236  }
237  */
238 
239  return 0;
240 }
241 EXPORT_SYMBOL(virgocloudexec_sendto);
242 
243 
244 static void __exit
245 virgocloudexec_exit(void)
246 {
247  printk(KERN_INFO "exiting virgocloudexec kernel module \n");
248  do_exit(1);
249 }
250 EXPORT_SYMBOL(virgocloudexec_exit);
251 
252 
253 MODULE_LICENSE("GPL");
254 module_init(virgocloudexec_init);
255 module_exit(virgocloudexec_exit);